xref: /linux/tools/perf/ui/setup.c (revision bf80eef2212a1e8451df13b52533f4bc31bb4f8e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dlfcn.h>
3 #include <unistd.h>
4 
5 #include <subcmd/pager.h>
6 #include "../util/debug.h"
7 #include "../util/hist.h"
8 #include "ui.h"
9 
10 struct mutex ui__lock;
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 	mutex_init(&ui__lock);
79 	if (use_browser < 2 && (!isatty(1) || dump_trace))
80 		use_browser = 0;
81 
82 	/* default to TUI */
83 	if (use_browser < 0)
84 		use_browser = 1;
85 
86 	switch (use_browser) {
87 	case 2:
88 		if (setup_gtk_browser() == 0)
89 			break;
90 		printf("GTK browser requested but could not find %s\n",
91 		       PERF_GTK_DSO);
92 		sleep(1);
93 		use_browser = 1;
94 		/* fall through */
95 	case 1:
96 		if (ui__init() == 0)
97 			break;
98 		/* fall through */
99 	default:
100 		use_browser = 0;
101 		if (fallback_to_pager)
102 			setup_pager();
103 		break;
104 	}
105 }
106 
107 void exit_browser(bool wait_for_ok)
108 {
109 	switch (use_browser) {
110 	case 2:
111 		exit_gtk_browser(wait_for_ok);
112 		break;
113 
114 	case 1:
115 		ui__exit(wait_for_ok);
116 		break;
117 
118 	default:
119 		break;
120 	}
121 	mutex_destroy(&ui__lock);
122 }
123