xref: /linux/tools/perf/ui/setup.c (revision 1cd8fa288eb83c1fe0dfa492b09d228a8d802fbf)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20985a948SNamhyung Kim #include <pthread.h>
3fc67297bSNamhyung Kim #include <dlfcn.h>
40985a948SNamhyung Kim 
5ea251d51SNamhyung Kim #include "../util/cache.h"
6ea251d51SNamhyung Kim #include "../util/debug.h"
7ea251d51SNamhyung Kim #include "../util/hist.h"
89a3993d4SArnaldo Carvalho de Melo #include "../util/util.h"
9aca7a94dSNamhyung Kim 
100985a948SNamhyung Kim pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
11fc67297bSNamhyung Kim void *perf_gtk_handle;
12d25ed5d9SSoramichi AKIYAMA int use_browser = -1;
13fc67297bSNamhyung Kim 
145068b52fSArnaldo Carvalho de Melo #define PERF_GTK_DSO "libperf-gtk.so"
155068b52fSArnaldo Carvalho de Melo 
16fc67297bSNamhyung Kim #ifdef HAVE_GTK2_SUPPORT
175068b52fSArnaldo Carvalho de Melo 
18fc67297bSNamhyung Kim static int setup_gtk_browser(void)
19fc67297bSNamhyung Kim {
20fc67297bSNamhyung Kim 	int (*perf_ui_init)(void);
21fc67297bSNamhyung Kim 
22fc67297bSNamhyung Kim 	if (perf_gtk_handle)
23fc67297bSNamhyung Kim 		return 0;
24fc67297bSNamhyung Kim 
25fc67297bSNamhyung Kim 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
26fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL) {
27fc67297bSNamhyung Kim 		char buf[PATH_MAX];
28fc67297bSNamhyung Kim 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
29fc67297bSNamhyung Kim 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
30fc67297bSNamhyung Kim 	}
31fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
32fc67297bSNamhyung Kim 		return -1;
33fc67297bSNamhyung Kim 
34fc67297bSNamhyung Kim 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
35fc67297bSNamhyung Kim 	if (perf_ui_init == NULL)
36fc67297bSNamhyung Kim 		goto out_close;
37fc67297bSNamhyung Kim 
38fc67297bSNamhyung Kim 	if (perf_ui_init() == 0)
39fc67297bSNamhyung Kim 		return 0;
40fc67297bSNamhyung Kim 
41fc67297bSNamhyung Kim out_close:
42fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
43fc67297bSNamhyung Kim 	return -1;
44fc67297bSNamhyung Kim }
45fc67297bSNamhyung Kim 
46fc67297bSNamhyung Kim static void exit_gtk_browser(bool wait_for_ok)
47fc67297bSNamhyung Kim {
48fc67297bSNamhyung Kim 	void (*perf_ui_exit)(bool);
49fc67297bSNamhyung Kim 
50fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
51fc67297bSNamhyung Kim 		return;
52fc67297bSNamhyung Kim 
53fc67297bSNamhyung Kim 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
54fc67297bSNamhyung Kim 	if (perf_ui_exit == NULL)
55fc67297bSNamhyung Kim 		goto out_close;
56fc67297bSNamhyung Kim 
57fc67297bSNamhyung Kim 	perf_ui_exit(wait_for_ok);
58fc67297bSNamhyung Kim 
59fc67297bSNamhyung Kim out_close:
60fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
61fc67297bSNamhyung Kim 
62fc67297bSNamhyung Kim 	perf_gtk_handle = NULL;
63fc67297bSNamhyung Kim }
64fc67297bSNamhyung Kim #else
65fc67297bSNamhyung Kim static inline int setup_gtk_browser(void) { return -1; }
66fc67297bSNamhyung Kim static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
67fc67297bSNamhyung Kim #endif
680985a948SNamhyung Kim 
69c09615f2SArnaldo Carvalho de Melo int stdio__config_color(const struct option *opt __maybe_unused,
70c09615f2SArnaldo Carvalho de Melo 			const char *mode, int unset __maybe_unused)
71c09615f2SArnaldo Carvalho de Melo {
72c09615f2SArnaldo Carvalho de Melo 	perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
73c09615f2SArnaldo Carvalho de Melo 	return 0;
74c09615f2SArnaldo Carvalho de Melo }
75c09615f2SArnaldo Carvalho de Melo 
76aca7a94dSNamhyung Kim void setup_browser(bool fallback_to_pager)
77aca7a94dSNamhyung Kim {
782b676bf0SNamhyung Kim 	if (use_browser < 2 && (!isatty(1) || dump_trace))
79aca7a94dSNamhyung Kim 		use_browser = 0;
80281ef544SNamhyung Kim 
81281ef544SNamhyung Kim 	/* default to TUI */
82281ef544SNamhyung Kim 	if (use_browser < 0)
83281ef544SNamhyung Kim 		use_browser = 1;
84281ef544SNamhyung Kim 
85281ef544SNamhyung Kim 	switch (use_browser) {
86281ef544SNamhyung Kim 	case 2:
87fc67297bSNamhyung Kim 		if (setup_gtk_browser() == 0)
88281ef544SNamhyung Kim 			break;
89fc67297bSNamhyung Kim 		printf("GTK browser requested but could not find %s\n",
90fc67297bSNamhyung Kim 		       PERF_GTK_DSO);
91fc67297bSNamhyung Kim 		sleep(1);
92*1cd8fa28SArnaldo Carvalho de Melo 		use_browser = 1;
93dc41b9b8SNamhyung Kim 		/* fall through */
94281ef544SNamhyung Kim 	case 1:
95dc41b9b8SNamhyung Kim 		if (ui__init() == 0)
96281ef544SNamhyung Kim 			break;
97dc41b9b8SNamhyung Kim 		/* fall through */
98281ef544SNamhyung Kim 	default:
9921f0d423SNamhyung Kim 		use_browser = 0;
100aca7a94dSNamhyung Kim 		if (fallback_to_pager)
101aca7a94dSNamhyung Kim 			setup_pager();
102281ef544SNamhyung Kim 		break;
103aca7a94dSNamhyung Kim 	}
104aca7a94dSNamhyung Kim }
105aca7a94dSNamhyung Kim 
106aca7a94dSNamhyung Kim void exit_browser(bool wait_for_ok)
107aca7a94dSNamhyung Kim {
108281ef544SNamhyung Kim 	switch (use_browser) {
109281ef544SNamhyung Kim 	case 2:
110fc67297bSNamhyung Kim 		exit_gtk_browser(wait_for_ok);
111281ef544SNamhyung Kim 		break;
112281ef544SNamhyung Kim 
113281ef544SNamhyung Kim 	case 1:
114281ef544SNamhyung Kim 		ui__exit(wait_for_ok);
115281ef544SNamhyung Kim 		break;
116281ef544SNamhyung Kim 
117281ef544SNamhyung Kim 	default:
118281ef544SNamhyung Kim 		break;
119aca7a94dSNamhyung Kim 	}
120aca7a94dSNamhyung Kim }
121