Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / perf / ui / setup.c
blob44fe824e96cd01ab686c4d7c9ee80ca9a4d1cbd6
1 // SPDX-License-Identifier: GPL-2.0
2 #include <pthread.h>
3 #include <dlfcn.h>
5 #include "../util/cache.h"
6 #include "../util/debug.h"
7 #include "../util/hist.h"
8 #include "../util/util.h"
10 pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
11 void *perf_gtk_handle;
12 int use_browser = -1;
14 #define PERF_GTK_DSO "libperf-gtk.so"
16 #ifdef HAVE_GTK2_SUPPORT
18 static int setup_gtk_browser(void)
20 int (*perf_ui_init)(void);
22 if (perf_gtk_handle)
23 return 0;
25 perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
26 if (perf_gtk_handle == NULL) {
27 char buf[PATH_MAX];
28 scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
29 perf_gtk_handle = dlopen(buf, RTLD_LAZY);
31 if (perf_gtk_handle == NULL)
32 return -1;
34 perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
35 if (perf_ui_init == NULL)
36 goto out_close;
38 if (perf_ui_init() == 0)
39 return 0;
41 out_close:
42 dlclose(perf_gtk_handle);
43 return -1;
46 static void exit_gtk_browser(bool wait_for_ok)
48 void (*perf_ui_exit)(bool);
50 if (perf_gtk_handle == NULL)
51 return;
53 perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
54 if (perf_ui_exit == NULL)
55 goto out_close;
57 perf_ui_exit(wait_for_ok);
59 out_close:
60 dlclose(perf_gtk_handle);
62 perf_gtk_handle = NULL;
64 #else
65 static inline int setup_gtk_browser(void) { return -1; }
66 static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
67 #endif
69 int stdio__config_color(const struct option *opt __maybe_unused,
70 const char *mode, int unset __maybe_unused)
72 perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
73 return 0;
76 void setup_browser(bool fallback_to_pager)
78 if (use_browser < 2 && (!isatty(1) || dump_trace))
79 use_browser = 0;
81 /* default to TUI */
82 if (use_browser < 0)
83 use_browser = 1;
85 switch (use_browser) {
86 case 2:
87 if (setup_gtk_browser() == 0)
88 break;
89 printf("GTK browser requested but could not find %s\n",
90 PERF_GTK_DSO);
91 sleep(1);
92 /* fall through */
93 case 1:
94 use_browser = 1;
95 if (ui__init() == 0)
96 break;
97 /* fall through */
98 default:
99 use_browser = 0;
100 if (fallback_to_pager)
101 setup_pager();
102 break;
106 void exit_browser(bool wait_for_ok)
108 switch (use_browser) {
109 case 2:
110 exit_gtk_browser(wait_for_ok);
111 break;
113 case 1:
114 ui__exit(wait_for_ok);
115 break;
117 default:
118 break;