xref: /linux/tools/perf/ui/setup.c (revision fa0d98462fae5d4951f22f3ac1090d48c53396d1)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20985a948SNamhyung Kim #include <pthread.h>
3fc67297bSNamhyung Kim #include <dlfcn.h>
40985a948SNamhyung Kim 
5*fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
6ea251d51SNamhyung Kim #include "../util/debug.h"
7ea251d51SNamhyung Kim #include "../util/hist.h"
89a3993d4SArnaldo Carvalho de Melo #include "../util/util.h"
9*fa0d9846SArnaldo Carvalho de Melo #include "ui.h"
10aca7a94dSNamhyung Kim 
110985a948SNamhyung Kim pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
12fc67297bSNamhyung Kim void *perf_gtk_handle;
13d25ed5d9SSoramichi AKIYAMA int use_browser = -1;
14fc67297bSNamhyung Kim 
155068b52fSArnaldo Carvalho de Melo #define PERF_GTK_DSO "libperf-gtk.so"
165068b52fSArnaldo Carvalho de Melo 
17fc67297bSNamhyung Kim #ifdef HAVE_GTK2_SUPPORT
185068b52fSArnaldo Carvalho de Melo 
19fc67297bSNamhyung Kim static int setup_gtk_browser(void)
20fc67297bSNamhyung Kim {
21fc67297bSNamhyung Kim 	int (*perf_ui_init)(void);
22fc67297bSNamhyung Kim 
23fc67297bSNamhyung Kim 	if (perf_gtk_handle)
24fc67297bSNamhyung Kim 		return 0;
25fc67297bSNamhyung Kim 
26fc67297bSNamhyung Kim 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
27fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL) {
28fc67297bSNamhyung Kim 		char buf[PATH_MAX];
29fc67297bSNamhyung Kim 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
30fc67297bSNamhyung Kim 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
31fc67297bSNamhyung Kim 	}
32fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
33fc67297bSNamhyung Kim 		return -1;
34fc67297bSNamhyung Kim 
35fc67297bSNamhyung Kim 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
36fc67297bSNamhyung Kim 	if (perf_ui_init == NULL)
37fc67297bSNamhyung Kim 		goto out_close;
38fc67297bSNamhyung Kim 
39fc67297bSNamhyung Kim 	if (perf_ui_init() == 0)
40fc67297bSNamhyung Kim 		return 0;
41fc67297bSNamhyung Kim 
42fc67297bSNamhyung Kim out_close:
43fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
44fc67297bSNamhyung Kim 	return -1;
45fc67297bSNamhyung Kim }
46fc67297bSNamhyung Kim 
47fc67297bSNamhyung Kim static void exit_gtk_browser(bool wait_for_ok)
48fc67297bSNamhyung Kim {
49fc67297bSNamhyung Kim 	void (*perf_ui_exit)(bool);
50fc67297bSNamhyung Kim 
51fc67297bSNamhyung Kim 	if (perf_gtk_handle == NULL)
52fc67297bSNamhyung Kim 		return;
53fc67297bSNamhyung Kim 
54fc67297bSNamhyung Kim 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
55fc67297bSNamhyung Kim 	if (perf_ui_exit == NULL)
56fc67297bSNamhyung Kim 		goto out_close;
57fc67297bSNamhyung Kim 
58fc67297bSNamhyung Kim 	perf_ui_exit(wait_for_ok);
59fc67297bSNamhyung Kim 
60fc67297bSNamhyung Kim out_close:
61fc67297bSNamhyung Kim 	dlclose(perf_gtk_handle);
62fc67297bSNamhyung Kim 
63fc67297bSNamhyung Kim 	perf_gtk_handle = NULL;
64fc67297bSNamhyung Kim }
65fc67297bSNamhyung Kim #else
66fc67297bSNamhyung Kim static inline int setup_gtk_browser(void) { return -1; }
67fc67297bSNamhyung Kim static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
68fc67297bSNamhyung Kim #endif
690985a948SNamhyung Kim 
70c09615f2SArnaldo Carvalho de Melo int stdio__config_color(const struct option *opt __maybe_unused,
71c09615f2SArnaldo Carvalho de Melo 			const char *mode, int unset __maybe_unused)
72c09615f2SArnaldo Carvalho de Melo {
73c09615f2SArnaldo Carvalho de Melo 	perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
74c09615f2SArnaldo Carvalho de Melo 	return 0;
75c09615f2SArnaldo Carvalho de Melo }
76c09615f2SArnaldo Carvalho de Melo 
77aca7a94dSNamhyung Kim void setup_browser(bool fallback_to_pager)
78aca7a94dSNamhyung Kim {
792b676bf0SNamhyung Kim 	if (use_browser < 2 && (!isatty(1) || dump_trace))
80aca7a94dSNamhyung Kim 		use_browser = 0;
81281ef544SNamhyung Kim 
82281ef544SNamhyung Kim 	/* default to TUI */
83281ef544SNamhyung Kim 	if (use_browser < 0)
84281ef544SNamhyung Kim 		use_browser = 1;
85281ef544SNamhyung Kim 
86281ef544SNamhyung Kim 	switch (use_browser) {
87281ef544SNamhyung Kim 	case 2:
88fc67297bSNamhyung Kim 		if (setup_gtk_browser() == 0)
89281ef544SNamhyung Kim 			break;
90fc67297bSNamhyung Kim 		printf("GTK browser requested but could not find %s\n",
91fc67297bSNamhyung Kim 		       PERF_GTK_DSO);
92fc67297bSNamhyung Kim 		sleep(1);
931cd8fa28SArnaldo Carvalho de Melo 		use_browser = 1;
94dc41b9b8SNamhyung Kim 		/* fall through */
95281ef544SNamhyung Kim 	case 1:
96dc41b9b8SNamhyung Kim 		if (ui__init() == 0)
97281ef544SNamhyung Kim 			break;
98dc41b9b8SNamhyung Kim 		/* fall through */
99281ef544SNamhyung Kim 	default:
10021f0d423SNamhyung Kim 		use_browser = 0;
101aca7a94dSNamhyung Kim 		if (fallback_to_pager)
102aca7a94dSNamhyung Kim 			setup_pager();
103281ef544SNamhyung Kim 		break;
104aca7a94dSNamhyung Kim 	}
105aca7a94dSNamhyung Kim }
106aca7a94dSNamhyung Kim 
107aca7a94dSNamhyung Kim void exit_browser(bool wait_for_ok)
108aca7a94dSNamhyung Kim {
109281ef544SNamhyung Kim 	switch (use_browser) {
110281ef544SNamhyung Kim 	case 2:
111fc67297bSNamhyung Kim 		exit_gtk_browser(wait_for_ok);
112281ef544SNamhyung Kim 		break;
113281ef544SNamhyung Kim 
114281ef544SNamhyung Kim 	case 1:
115281ef544SNamhyung Kim 		ui__exit(wait_for_ok);
116281ef544SNamhyung Kim 		break;
117281ef544SNamhyung Kim 
118281ef544SNamhyung Kim 	default:
119281ef544SNamhyung Kim 		break;
120aca7a94dSNamhyung Kim 	}
121aca7a94dSNamhyung Kim }
122