blob: ff800047e697f1335737405543fb14d4742a9b10 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Namhyung Kimfc672972013-09-13 15:27:43 +09002#include <dlfcn.h>
Ian Rogerscd8ef942023-04-10 09:25:06 -07003#include <signal.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -03004#include <unistd.h>
Namhyung Kim0985a942012-08-16 17:14:54 +09005
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -03006#include <subcmd/pager.h>
Namhyung Kimea251d52012-09-03 11:53:06 +09007#include "../util/debug.h"
8#include "../util/hist.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -03009#include "ui.h"
Arnaldo Carvalho de Melo3f7247e2011-10-18 13:45:16 -020010
Ian Rogers82aff6c2022-08-26 09:42:33 -070011struct mutex ui__lock;
Namhyung Kimfc672972013-09-13 15:27:43 +090012void *perf_gtk_handle;
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090013int use_browser = -1;
Namhyung Kimfc672972013-09-13 15:27:43 +090014
Arnaldo Carvalho de Melo5068b522017-04-26 15:31:57 -030015#define PERF_GTK_DSO "libperf-gtk.so"
16
Namhyung Kimfc672972013-09-13 15:27:43 +090017#ifdef HAVE_GTK2_SUPPORT
Arnaldo Carvalho de Melo5068b522017-04-26 15:31:57 -030018
Namhyung Kimfc672972013-09-13 15:27:43 +090019static int setup_gtk_browser(void)
20{
21 int (*perf_ui_init)(void);
22
23 if (perf_gtk_handle)
24 return 0;
25
26 perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
27 if (perf_gtk_handle == NULL) {
28 char buf[PATH_MAX];
29 scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
30 perf_gtk_handle = dlopen(buf, RTLD_LAZY);
31 }
32 if (perf_gtk_handle == NULL)
33 return -1;
34
35 perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
36 if (perf_ui_init == NULL)
37 goto out_close;
38
39 if (perf_ui_init() == 0)
40 return 0;
41
42out_close:
43 dlclose(perf_gtk_handle);
44 return -1;
45}
46
47static void exit_gtk_browser(bool wait_for_ok)
48{
49 void (*perf_ui_exit)(bool);
50
51 if (perf_gtk_handle == NULL)
52 return;
53
54 perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
55 if (perf_ui_exit == NULL)
56 goto out_close;
57
58 perf_ui_exit(wait_for_ok);
59
60out_close:
61 dlclose(perf_gtk_handle);
62
63 perf_gtk_handle = NULL;
64}
65#else
66static inline int setup_gtk_browser(void) { return -1; }
67static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
68#endif
Namhyung Kim0985a942012-08-16 17:14:54 +090069
Arnaldo Carvalho de Meloc09615f2016-07-05 11:05:24 -030070int stdio__config_color(const struct option *opt __maybe_unused,
71 const char *mode, int unset __maybe_unused)
72{
73 perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
74 return 0;
75}
76
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020077void setup_browser(bool fallback_to_pager)
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030078{
Ian Rogers82aff6c2022-08-26 09:42:33 -070079 mutex_init(&ui__lock);
Namhyung Kim2b676bf2013-02-07 18:02:08 +090080 if (use_browser < 2 && (!isatty(1) || dump_trace))
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030081 use_browser = 0;
Namhyung Kim281ef542012-04-30 13:55:08 +090082
83 /* default to TUI */
84 if (use_browser < 0)
85 use_browser = 1;
86
87 switch (use_browser) {
88 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +090089 if (setup_gtk_browser() == 0)
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090090 break;
Namhyung Kimfc672972013-09-13 15:27:43 +090091 printf("GTK browser requested but could not find %s\n",
92 PERF_GTK_DSO);
93 sleep(1);
Arnaldo Carvalho de Melo1cd8fa22019-08-13 12:07:14 -030094 use_browser = 1;
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090095 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090096 case 1:
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090097 if (ui__init() == 0)
98 break;
99 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +0900100 default:
Namhyung Kim21f0d422012-05-28 23:53:22 +0900101 use_browser = 0;
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -0200102 if (fallback_to_pager)
103 setup_pager();
Namhyung Kim281ef542012-04-30 13:55:08 +0900104 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300105 }
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300106}
107
108void exit_browser(bool wait_for_ok)
109{
Namhyung Kim281ef542012-04-30 13:55:08 +0900110 switch (use_browser) {
111 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +0900112 exit_gtk_browser(wait_for_ok);
Namhyung Kim281ef542012-04-30 13:55:08 +0900113 break;
114
115 case 1:
116 ui__exit(wait_for_ok);
117 break;
118
119 default:
120 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300121 }
Ian Rogers82aff6c2022-08-26 09:42:33 -0700122 mutex_destroy(&ui__lock);
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300123}
Ian Rogerscd8ef942023-04-10 09:25:06 -0700124
125void pthread__block_sigwinch(void)
126{
127 sigset_t set;
128
129 sigemptyset(&set);
130 sigaddset(&set, SIGWINCH);
131 pthread_sigmask(SIG_BLOCK, &set, NULL);
132}
133
134void pthread__unblock_sigwinch(void)
135{
136 sigset_t set;
137
138 sigemptyset(&set);
139 sigaddset(&set, SIGWINCH);
140 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
141}