xref: /openbmc/linux/tools/perf/ui/setup.c (revision b78412b8)
1 #include <pthread.h>
2 #include <dlfcn.h>
3 
4 #include "../util/cache.h"
5 #include "../util/debug.h"
6 #include "../util/hist.h"
7 #include "../util/util.h"
8 
9 pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
10 void *perf_gtk_handle;
11 int use_browser = -1;
12 
13 #define PERF_GTK_DSO "libperf-gtk.so"
14 
15 #ifdef HAVE_GTK2_SUPPORT
16 
17 static int setup_gtk_browser(void)
18 {
19 	int (*perf_ui_init)(void);
20 
21 	if (perf_gtk_handle)
22 		return 0;
23 
24 	perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
25 	if (perf_gtk_handle == NULL) {
26 		char buf[PATH_MAX];
27 		scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
28 		perf_gtk_handle = dlopen(buf, RTLD_LAZY);
29 	}
30 	if (perf_gtk_handle == NULL)
31 		return -1;
32 
33 	perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
34 	if (perf_ui_init == NULL)
35 		goto out_close;
36 
37 	if (perf_ui_init() == 0)
38 		return 0;
39 
40 out_close:
41 	dlclose(perf_gtk_handle);
42 	return -1;
43 }
44 
45 static void exit_gtk_browser(bool wait_for_ok)
46 {
47 	void (*perf_ui_exit)(bool);
48 
49 	if (perf_gtk_handle == NULL)
50 		return;
51 
52 	perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
53 	if (perf_ui_exit == NULL)
54 		goto out_close;
55 
56 	perf_ui_exit(wait_for_ok);
57 
58 out_close:
59 	dlclose(perf_gtk_handle);
60 
61 	perf_gtk_handle = NULL;
62 }
63 #else
64 static inline int setup_gtk_browser(void) { return -1; }
65 static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
66 #endif
67 
68 int stdio__config_color(const struct option *opt __maybe_unused,
69 			const char *mode, int unset __maybe_unused)
70 {
71 	perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
72 	return 0;
73 }
74 
75 void setup_browser(bool fallback_to_pager)
76 {
77 	if (use_browser < 2 && (!isatty(1) || dump_trace))
78 		use_browser = 0;
79 
80 	/* default to TUI */
81 	if (use_browser < 0)
82 		use_browser = 1;
83 
84 	switch (use_browser) {
85 	case 2:
86 		if (setup_gtk_browser() == 0)
87 			break;
88 		printf("GTK browser requested but could not find %s\n",
89 		       PERF_GTK_DSO);
90 		sleep(1);
91 		/* fall through */
92 	case 1:
93 		use_browser = 1;
94 		if (ui__init() == 0)
95 			break;
96 		/* fall through */
97 	default:
98 		use_browser = 0;
99 		if (fallback_to_pager)
100 			setup_pager();
101 		break;
102 	}
103 }
104 
105 void exit_browser(bool wait_for_ok)
106 {
107 	switch (use_browser) {
108 	case 2:
109 		exit_gtk_browser(wait_for_ok);
110 		break;
111 
112 	case 1:
113 		ui__exit(wait_for_ok);
114 		break;
115 
116 	default:
117 		break;
118 	}
119 }
120