xref: /linux/tools/perf/ui/setup.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fc67297bSNamhyung Kim #include <dlfcn.h>
3*cd8ef949SIan Rogers #include <signal.h>
4fb71c86cSArnaldo Carvalho de Melo #include <unistd.h>
50985a948SNamhyung Kim 
6fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
7ea251d51SNamhyung Kim #include "../util/debug.h"
8ea251d51SNamhyung Kim #include "../util/hist.h"
9fa0d9846SArnaldo Carvalho de Melo #include "ui.h"
10aca7a94dSNamhyung Kim 
1182aff6ccSIan Rogers struct mutex ui__lock;
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 
setup_gtk_browser(void)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 
exit_gtk_browser(bool wait_for_ok)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
setup_gtk_browser(void)66fc67297bSNamhyung Kim static inline int setup_gtk_browser(void) { return -1; }
exit_gtk_browser(bool wait_for_ok __maybe_unused)67fc67297bSNamhyung Kim static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
68fc67297bSNamhyung Kim #endif
690985a948SNamhyung Kim 
stdio__config_color(const struct option * opt __maybe_unused,const char * mode,int unset __maybe_unused)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 
setup_browser(bool fallback_to_pager)77aca7a94dSNamhyung Kim void setup_browser(bool fallback_to_pager)
78aca7a94dSNamhyung Kim {
7982aff6ccSIan Rogers 	mutex_init(&ui__lock);
802b676bf0SNamhyung Kim 	if (use_browser < 2 && (!isatty(1) || dump_trace))
81aca7a94dSNamhyung Kim 		use_browser = 0;
82281ef544SNamhyung Kim 
83281ef544SNamhyung Kim 	/* default to TUI */
84281ef544SNamhyung Kim 	if (use_browser < 0)
85281ef544SNamhyung Kim 		use_browser = 1;
86281ef544SNamhyung Kim 
87281ef544SNamhyung Kim 	switch (use_browser) {
88281ef544SNamhyung Kim 	case 2:
89fc67297bSNamhyung Kim 		if (setup_gtk_browser() == 0)
90281ef544SNamhyung Kim 			break;
91fc67297bSNamhyung Kim 		printf("GTK browser requested but could not find %s\n",
92fc67297bSNamhyung Kim 		       PERF_GTK_DSO);
93fc67297bSNamhyung Kim 		sleep(1);
941cd8fa28SArnaldo Carvalho de Melo 		use_browser = 1;
95dc41b9b8SNamhyung Kim 		/* fall through */
96281ef544SNamhyung Kim 	case 1:
97dc41b9b8SNamhyung Kim 		if (ui__init() == 0)
98281ef544SNamhyung Kim 			break;
99dc41b9b8SNamhyung Kim 		/* fall through */
100281ef544SNamhyung Kim 	default:
10121f0d423SNamhyung Kim 		use_browser = 0;
102aca7a94dSNamhyung Kim 		if (fallback_to_pager)
103aca7a94dSNamhyung Kim 			setup_pager();
104281ef544SNamhyung Kim 		break;
105aca7a94dSNamhyung Kim 	}
106aca7a94dSNamhyung Kim }
107aca7a94dSNamhyung Kim 
exit_browser(bool wait_for_ok)108aca7a94dSNamhyung Kim void exit_browser(bool wait_for_ok)
109aca7a94dSNamhyung Kim {
110281ef544SNamhyung Kim 	switch (use_browser) {
111281ef544SNamhyung Kim 	case 2:
112fc67297bSNamhyung Kim 		exit_gtk_browser(wait_for_ok);
113281ef544SNamhyung Kim 		break;
114281ef544SNamhyung Kim 
115281ef544SNamhyung Kim 	case 1:
116281ef544SNamhyung Kim 		ui__exit(wait_for_ok);
117281ef544SNamhyung Kim 		break;
118281ef544SNamhyung Kim 
119281ef544SNamhyung Kim 	default:
120281ef544SNamhyung Kim 		break;
121aca7a94dSNamhyung Kim 	}
12282aff6ccSIan Rogers 	mutex_destroy(&ui__lock);
123aca7a94dSNamhyung Kim }
124*cd8ef949SIan Rogers 
pthread__block_sigwinch(void)125*cd8ef949SIan Rogers void pthread__block_sigwinch(void)
126*cd8ef949SIan Rogers {
127*cd8ef949SIan Rogers 	sigset_t set;
128*cd8ef949SIan Rogers 
129*cd8ef949SIan Rogers 	sigemptyset(&set);
130*cd8ef949SIan Rogers 	sigaddset(&set, SIGWINCH);
131*cd8ef949SIan Rogers 	pthread_sigmask(SIG_BLOCK, &set, NULL);
132*cd8ef949SIan Rogers }
133*cd8ef949SIan Rogers 
pthread__unblock_sigwinch(void)134*cd8ef949SIan Rogers void pthread__unblock_sigwinch(void)
135*cd8ef949SIan Rogers {
136*cd8ef949SIan Rogers 	sigset_t set;
137*cd8ef949SIan Rogers 
138*cd8ef949SIan Rogers 	sigemptyset(&set);
139*cd8ef949SIan Rogers 	sigaddset(&set, SIGWINCH);
140*cd8ef949SIan Rogers 	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
141*cd8ef949SIan Rogers }
142