xref: /linux/tools/perf/ui/setup.c (revision 82aff6cc070417f26f9b02b26e63c17ff43b4044)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fc67297bSNamhyung Kim #include <dlfcn.h>
3fb71c86cSArnaldo Carvalho de Melo #include <unistd.h>
40985a948SNamhyung Kim 
5fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
6ea251d51SNamhyung Kim #include "../util/debug.h"
7ea251d51SNamhyung Kim #include "../util/hist.h"
8fa0d9846SArnaldo Carvalho de Melo #include "ui.h"
9aca7a94dSNamhyung Kim 
10*82aff6ccSIan Rogers struct mutex ui__lock;
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 {
78*82aff6ccSIan Rogers 	mutex_init(&ui__lock);
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 	}
121*82aff6ccSIan Rogers 	mutex_destroy(&ui__lock);
122aca7a94dSNamhyung Kim }
123