xref: /linux/tools/perf/ui/setup.c (revision d25ed5d9fad98fd36ffca0bb5c3a0761a11e3f3c)
10985a948SNamhyung Kim #include <pthread.h>
2fc67297bSNamhyung Kim #include <dlfcn.h>
30985a948SNamhyung Kim 
4ea251d51SNamhyung Kim #include "../util/cache.h"
5ea251d51SNamhyung Kim #include "../util/debug.h"
6ea251d51SNamhyung Kim #include "../util/hist.h"
7aca7a94dSNamhyung Kim 
80985a948SNamhyung Kim pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
9fc67297bSNamhyung Kim void *perf_gtk_handle;
10*d25ed5d9SSoramichi AKIYAMA int use_browser = -1;
11fc67297bSNamhyung Kim 
12fc67297bSNamhyung Kim #ifdef HAVE_GTK2_SUPPORT
13fc67297bSNamhyung Kim static int setup_gtk_browser(void)
14fc67297bSNamhyung Kim {
15fc67297bSNamhyung Kim 	int (*perf_ui_init)(void);
16fc67297bSNamhyung Kim 
17fc67297bSNamhyung Kim 	if (perf_gtk_handle)
18fc67297bSNamhyung Kim 		return 0;
19fc67297bSNamhyung Kim 
20fc67297bSNamhyung Kim 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
21fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL) {
22fc67297bSNamhyung Kim 		char buf[PATH_MAX];
23fc67297bSNamhyung Kim 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
24fc67297bSNamhyung Kim 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
25fc67297bSNamhyung Kim 	}
26fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
27fc67297bSNamhyung Kim 		return -1;
28fc67297bSNamhyung Kim 
29fc67297bSNamhyung Kim 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
30fc67297bSNamhyung Kim 	if (perf_ui_init == NULL)
31fc67297bSNamhyung Kim 		goto out_close;
32fc67297bSNamhyung Kim 
33fc67297bSNamhyung Kim 	if (perf_ui_init() == 0)
34fc67297bSNamhyung Kim 		return 0;
35fc67297bSNamhyung Kim 
36fc67297bSNamhyung Kim out_close:
37fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
38fc67297bSNamhyung Kim 	return -1;
39fc67297bSNamhyung Kim }
40fc67297bSNamhyung Kim 
41fc67297bSNamhyung Kim static void exit_gtk_browser(bool wait_for_ok)
42fc67297bSNamhyung Kim {
43fc67297bSNamhyung Kim 	void (*perf_ui_exit)(bool);
44fc67297bSNamhyung Kim 
45fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
46fc67297bSNamhyung Kim 		return;
47fc67297bSNamhyung Kim 
48fc67297bSNamhyung Kim 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
49fc67297bSNamhyung Kim 	if (perf_ui_exit == NULL)
50fc67297bSNamhyung Kim 		goto out_close;
51fc67297bSNamhyung Kim 
52fc67297bSNamhyung Kim 	perf_ui_exit(wait_for_ok);
53fc67297bSNamhyung Kim 
54fc67297bSNamhyung Kim out_close:
55fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
56fc67297bSNamhyung Kim 
57fc67297bSNamhyung Kim 	perf_gtk_handle = NULL;
58fc67297bSNamhyung Kim }
59fc67297bSNamhyung Kim #else
60fc67297bSNamhyung Kim static inline int setup_gtk_browser(void) { return -1; }
61fc67297bSNamhyung Kim static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
62fc67297bSNamhyung Kim #endif
630985a948SNamhyung Kim 
64c09615f2SArnaldo Carvalho de Melo int stdio__config_color(const struct option *opt __maybe_unused,
65c09615f2SArnaldo Carvalho de Melo 			const char *mode, int unset __maybe_unused)
66c09615f2SArnaldo Carvalho de Melo {
67c09615f2SArnaldo Carvalho de Melo 	perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
68c09615f2SArnaldo Carvalho de Melo 	return 0;
69c09615f2SArnaldo Carvalho de Melo }
70c09615f2SArnaldo Carvalho de Melo 
71aca7a94dSNamhyung Kim void setup_browser(bool fallback_to_pager)
72aca7a94dSNamhyung Kim {
732b676bf0SNamhyung Kim 	if (use_browser < 2 && (!isatty(1) || dump_trace))
74aca7a94dSNamhyung Kim 		use_browser = 0;
75281ef544SNamhyung Kim 
76281ef544SNamhyung Kim 	/* default to TUI */
77281ef544SNamhyung Kim 	if (use_browser < 0)
78281ef544SNamhyung Kim 		use_browser = 1;
79281ef544SNamhyung Kim 
80281ef544SNamhyung Kim 	switch (use_browser) {
81281ef544SNamhyung Kim 	case 2:
82fc67297bSNamhyung Kim 		if (setup_gtk_browser() == 0)
83281ef544SNamhyung Kim 			break;
84fc67297bSNamhyung Kim 		printf("GTK browser requested but could not find %s\n",
85fc67297bSNamhyung Kim 		       PERF_GTK_DSO);
86fc67297bSNamhyung Kim 		sleep(1);
87dc41b9b8SNamhyung Kim 		/* fall through */
88281ef544SNamhyung Kim 	case 1:
89dc41b9b8SNamhyung Kim 		use_browser = 1;
90dc41b9b8SNamhyung Kim 		if (ui__init() == 0)
91281ef544SNamhyung Kim 			break;
92dc41b9b8SNamhyung Kim 		/* fall through */
93281ef544SNamhyung Kim 	default:
9421f0d423SNamhyung Kim 		use_browser = 0;
95aca7a94dSNamhyung Kim 		if (fallback_to_pager)
96aca7a94dSNamhyung Kim 			setup_pager();
97281ef544SNamhyung Kim 		break;
98aca7a94dSNamhyung Kim 	}
99aca7a94dSNamhyung Kim }
100aca7a94dSNamhyung Kim 
101aca7a94dSNamhyung Kim void exit_browser(bool wait_for_ok)
102aca7a94dSNamhyung Kim {
103281ef544SNamhyung Kim 	switch (use_browser) {
104281ef544SNamhyung Kim 	case 2:
105fc67297bSNamhyung Kim 		exit_gtk_browser(wait_for_ok);
106281ef544SNamhyung Kim 		break;
107281ef544SNamhyung Kim 
108281ef544SNamhyung Kim 	case 1:
109281ef544SNamhyung Kim 		ui__exit(wait_for_ok);
110281ef544SNamhyung Kim 		break;
111281ef544SNamhyung Kim 
112281ef544SNamhyung Kim 	default:
113281ef544SNamhyung Kim 		break;
114aca7a94dSNamhyung Kim 	}
115aca7a94dSNamhyung Kim }
116