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