xref: /linux/tools/perf/ui/setup.c (revision fc67297b16da335d610af2fac96233d51146300a)
10985a948SNamhyung Kim #include <pthread.h>
2*fc67297bSNamhyung 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;
9*fc67297bSNamhyung Kim void *perf_gtk_handle;
10*fc67297bSNamhyung Kim 
11*fc67297bSNamhyung Kim #ifdef HAVE_GTK2_SUPPORT
12*fc67297bSNamhyung Kim static int setup_gtk_browser(void)
13*fc67297bSNamhyung Kim {
14*fc67297bSNamhyung Kim 	int (*perf_ui_init)(void);
15*fc67297bSNamhyung Kim 
16*fc67297bSNamhyung Kim 	if (perf_gtk_handle)
17*fc67297bSNamhyung Kim 		return 0;
18*fc67297bSNamhyung Kim 
19*fc67297bSNamhyung Kim 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
20*fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL) {
21*fc67297bSNamhyung Kim 		char buf[PATH_MAX];
22*fc67297bSNamhyung Kim 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
23*fc67297bSNamhyung Kim 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
24*fc67297bSNamhyung Kim 	}
25*fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
26*fc67297bSNamhyung Kim 		return -1;
27*fc67297bSNamhyung Kim 
28*fc67297bSNamhyung Kim 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
29*fc67297bSNamhyung Kim 	if (perf_ui_init == NULL)
30*fc67297bSNamhyung Kim 		goto out_close;
31*fc67297bSNamhyung Kim 
32*fc67297bSNamhyung Kim 	if (perf_ui_init() == 0)
33*fc67297bSNamhyung Kim 		return 0;
34*fc67297bSNamhyung Kim 
35*fc67297bSNamhyung Kim out_close:
36*fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
37*fc67297bSNamhyung Kim 	return -1;
38*fc67297bSNamhyung Kim }
39*fc67297bSNamhyung Kim 
40*fc67297bSNamhyung Kim static void exit_gtk_browser(bool wait_for_ok)
41*fc67297bSNamhyung Kim {
42*fc67297bSNamhyung Kim 	void (*perf_ui_exit)(bool);
43*fc67297bSNamhyung Kim 
44*fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
45*fc67297bSNamhyung Kim 		return;
46*fc67297bSNamhyung Kim 
47*fc67297bSNamhyung Kim 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
48*fc67297bSNamhyung Kim 	if (perf_ui_exit == NULL)
49*fc67297bSNamhyung Kim 		goto out_close;
50*fc67297bSNamhyung Kim 
51*fc67297bSNamhyung Kim 	perf_ui_exit(wait_for_ok);
52*fc67297bSNamhyung Kim 
53*fc67297bSNamhyung Kim out_close:
54*fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
55*fc67297bSNamhyung Kim 
56*fc67297bSNamhyung Kim 	perf_gtk_handle = NULL;
57*fc67297bSNamhyung Kim }
58*fc67297bSNamhyung Kim #else
59*fc67297bSNamhyung Kim static inline int setup_gtk_browser(void) { return -1; }
60*fc67297bSNamhyung Kim static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
61*fc67297bSNamhyung Kim #endif
620985a948SNamhyung Kim 
63aca7a94dSNamhyung Kim void setup_browser(bool fallback_to_pager)
64aca7a94dSNamhyung Kim {
652b676bf0SNamhyung Kim 	if (use_browser < 2 && (!isatty(1) || dump_trace))
66aca7a94dSNamhyung Kim 		use_browser = 0;
67281ef544SNamhyung Kim 
68281ef544SNamhyung Kim 	/* default to TUI */
69281ef544SNamhyung Kim 	if (use_browser < 0)
70281ef544SNamhyung Kim 		use_browser = 1;
71281ef544SNamhyung Kim 
72281ef544SNamhyung Kim 	switch (use_browser) {
73281ef544SNamhyung Kim 	case 2:
74*fc67297bSNamhyung Kim 		if (setup_gtk_browser() == 0)
75281ef544SNamhyung Kim 			break;
76*fc67297bSNamhyung Kim 		printf("GTK browser requested but could not find %s\n",
77*fc67297bSNamhyung Kim 		       PERF_GTK_DSO);
78*fc67297bSNamhyung Kim 		sleep(1);
79dc41b9b8SNamhyung Kim 		/* fall through */
80281ef544SNamhyung Kim 	case 1:
81dc41b9b8SNamhyung Kim 		use_browser = 1;
82dc41b9b8SNamhyung Kim 		if (ui__init() == 0)
83281ef544SNamhyung Kim 			break;
84dc41b9b8SNamhyung Kim 		/* fall through */
85281ef544SNamhyung Kim 	default:
8621f0d423SNamhyung Kim 		use_browser = 0;
87aca7a94dSNamhyung Kim 		if (fallback_to_pager)
88aca7a94dSNamhyung Kim 			setup_pager();
89ea251d51SNamhyung Kim 
901d77822eSJiri Olsa 		perf_hpp__init();
91281ef544SNamhyung Kim 		break;
92aca7a94dSNamhyung Kim 	}
93aca7a94dSNamhyung Kim }
94aca7a94dSNamhyung Kim 
95aca7a94dSNamhyung Kim void exit_browser(bool wait_for_ok)
96aca7a94dSNamhyung Kim {
97281ef544SNamhyung Kim 	switch (use_browser) {
98281ef544SNamhyung Kim 	case 2:
99*fc67297bSNamhyung Kim 		exit_gtk_browser(wait_for_ok);
100281ef544SNamhyung Kim 		break;
101281ef544SNamhyung Kim 
102281ef544SNamhyung Kim 	case 1:
103281ef544SNamhyung Kim 		ui__exit(wait_for_ok);
104281ef544SNamhyung Kim 		break;
105281ef544SNamhyung Kim 
106281ef544SNamhyung Kim 	default:
107281ef544SNamhyung Kim 		break;
108aca7a94dSNamhyung Kim 	}
109aca7a94dSNamhyung Kim }
110