xref: /linux/tools/perf/ui/setup.c (revision 5068b52f732157385d9dccd205bc4043da3a3cce)
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"
79a3993d4SArnaldo Carvalho de Melo #include "../util/util.h"
8aca7a94dSNamhyung Kim 
90985a948SNamhyung Kim pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
10fc67297bSNamhyung Kim void *perf_gtk_handle;
11d25ed5d9SSoramichi AKIYAMA int use_browser = -1;
12fc67297bSNamhyung Kim 
13*5068b52fSArnaldo Carvalho de Melo #define PERF_GTK_DSO "libperf-gtk.so"
14*5068b52fSArnaldo Carvalho de Melo 
15fc67297bSNamhyung Kim #ifdef HAVE_GTK2_SUPPORT
16*5068b52fSArnaldo Carvalho de Melo 
17fc67297bSNamhyung Kim static int setup_gtk_browser(void)
18fc67297bSNamhyung Kim {
19fc67297bSNamhyung Kim 	int (*perf_ui_init)(void);
20fc67297bSNamhyung Kim 
21fc67297bSNamhyung Kim 	if (perf_gtk_handle)
22fc67297bSNamhyung Kim 		return 0;
23fc67297bSNamhyung Kim 
24fc67297bSNamhyung Kim 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
25fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL) {
26fc67297bSNamhyung Kim 		char buf[PATH_MAX];
27fc67297bSNamhyung Kim 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
28fc67297bSNamhyung Kim 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
29fc67297bSNamhyung Kim 	}
30fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
31fc67297bSNamhyung Kim 		return -1;
32fc67297bSNamhyung Kim 
33fc67297bSNamhyung Kim 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
34fc67297bSNamhyung Kim 	if (perf_ui_init == NULL)
35fc67297bSNamhyung Kim 		goto out_close;
36fc67297bSNamhyung Kim 
37fc67297bSNamhyung Kim 	if (perf_ui_init() == 0)
38fc67297bSNamhyung Kim 		return 0;
39fc67297bSNamhyung Kim 
40fc67297bSNamhyung Kim out_close:
41fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
42fc67297bSNamhyung Kim 	return -1;
43fc67297bSNamhyung Kim }
44fc67297bSNamhyung Kim 
45fc67297bSNamhyung Kim static void exit_gtk_browser(bool wait_for_ok)
46fc67297bSNamhyung Kim {
47fc67297bSNamhyung Kim 	void (*perf_ui_exit)(bool);
48fc67297bSNamhyung Kim 
49fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
50fc67297bSNamhyung Kim 		return;
51fc67297bSNamhyung Kim 
52fc67297bSNamhyung Kim 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
53fc67297bSNamhyung Kim 	if (perf_ui_exit == NULL)
54fc67297bSNamhyung Kim 		goto out_close;
55fc67297bSNamhyung Kim 
56fc67297bSNamhyung Kim 	perf_ui_exit(wait_for_ok);
57fc67297bSNamhyung Kim 
58fc67297bSNamhyung Kim out_close:
59fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
60fc67297bSNamhyung Kim 
61fc67297bSNamhyung Kim 	perf_gtk_handle = NULL;
62fc67297bSNamhyung Kim }
63fc67297bSNamhyung Kim #else
64fc67297bSNamhyung Kim static inline int setup_gtk_browser(void) { return -1; }
65fc67297bSNamhyung Kim static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
66fc67297bSNamhyung Kim #endif
670985a948SNamhyung Kim 
68c09615f2SArnaldo Carvalho de Melo int stdio__config_color(const struct option *opt __maybe_unused,
69c09615f2SArnaldo Carvalho de Melo 			const char *mode, int unset __maybe_unused)
70c09615f2SArnaldo Carvalho de Melo {
71c09615f2SArnaldo Carvalho de Melo 	perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
72c09615f2SArnaldo Carvalho de Melo 	return 0;
73c09615f2SArnaldo Carvalho de Melo }
74c09615f2SArnaldo Carvalho de Melo 
75aca7a94dSNamhyung Kim void setup_browser(bool fallback_to_pager)
76aca7a94dSNamhyung Kim {
772b676bf0SNamhyung Kim 	if (use_browser < 2 && (!isatty(1) || dump_trace))
78aca7a94dSNamhyung Kim 		use_browser = 0;
79281ef544SNamhyung Kim 
80281ef544SNamhyung Kim 	/* default to TUI */
81281ef544SNamhyung Kim 	if (use_browser < 0)
82281ef544SNamhyung Kim 		use_browser = 1;
83281ef544SNamhyung Kim 
84281ef544SNamhyung Kim 	switch (use_browser) {
85281ef544SNamhyung Kim 	case 2:
86fc67297bSNamhyung Kim 		if (setup_gtk_browser() == 0)
87281ef544SNamhyung Kim 			break;
88fc67297bSNamhyung Kim 		printf("GTK browser requested but could not find %s\n",
89fc67297bSNamhyung Kim 		       PERF_GTK_DSO);
90fc67297bSNamhyung Kim 		sleep(1);
91dc41b9b8SNamhyung Kim 		/* fall through */
92281ef544SNamhyung Kim 	case 1:
93dc41b9b8SNamhyung Kim 		use_browser = 1;
94dc41b9b8SNamhyung Kim 		if (ui__init() == 0)
95281ef544SNamhyung Kim 			break;
96dc41b9b8SNamhyung Kim 		/* fall through */
97281ef544SNamhyung Kim 	default:
9821f0d423SNamhyung Kim 		use_browser = 0;
99aca7a94dSNamhyung Kim 		if (fallback_to_pager)
100aca7a94dSNamhyung Kim 			setup_pager();
101281ef544SNamhyung Kim 		break;
102aca7a94dSNamhyung Kim 	}
103aca7a94dSNamhyung Kim }
104aca7a94dSNamhyung Kim 
105aca7a94dSNamhyung Kim void exit_browser(bool wait_for_ok)
106aca7a94dSNamhyung Kim {
107281ef544SNamhyung Kim 	switch (use_browser) {
108281ef544SNamhyung Kim 	case 2:
109fc67297bSNamhyung Kim 		exit_gtk_browser(wait_for_ok);
110281ef544SNamhyung Kim 		break;
111281ef544SNamhyung Kim 
112281ef544SNamhyung Kim 	case 1:
113281ef544SNamhyung Kim 		ui__exit(wait_for_ok);
114281ef544SNamhyung Kim 		break;
115281ef544SNamhyung Kim 
116281ef544SNamhyung Kim 	default:
117281ef544SNamhyung Kim 		break;
118aca7a94dSNamhyung Kim 	}
119aca7a94dSNamhyung Kim }
120