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