blob: 50d13e58210f0d7dc4b59421914317604757ac10 [file] [log] [blame]
Namhyung Kim0985a942012-08-16 17:14:54 +09001#include <pthread.h>
Namhyung Kimfc672972013-09-13 15:27:43 +09002#include <dlfcn.h>
Namhyung Kim0985a942012-08-16 17:14:54 +09003
Namhyung Kimea251d52012-09-03 11:53:06 +09004#include "../util/cache.h"
5#include "../util/debug.h"
6#include "../util/hist.h"
Arnaldo Carvalho de Melo3f7247e2011-10-18 13:45:16 -02007
Namhyung Kim0985a942012-08-16 17:14:54 +09008pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
Namhyung Kimfc672972013-09-13 15:27:43 +09009void *perf_gtk_handle;
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090010int use_browser = -1;
Namhyung Kimfc672972013-09-13 15:27:43 +090011
12#ifdef HAVE_GTK2_SUPPORT
13static int setup_gtk_browser(void)
14{
15 int (*perf_ui_init)(void);
16
17 if (perf_gtk_handle)
18 return 0;
19
20 perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
21 if (perf_gtk_handle == NULL) {
22 char buf[PATH_MAX];
23 scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
24 perf_gtk_handle = dlopen(buf, RTLD_LAZY);
25 }
26 if (perf_gtk_handle == NULL)
27 return -1;
28
29 perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
30 if (perf_ui_init == NULL)
31 goto out_close;
32
33 if (perf_ui_init() == 0)
34 return 0;
35
36out_close:
37 dlclose(perf_gtk_handle);
38 return -1;
39}
40
41static void exit_gtk_browser(bool wait_for_ok)
42{
43 void (*perf_ui_exit)(bool);
44
45 if (perf_gtk_handle == NULL)
46 return;
47
48 perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
49 if (perf_ui_exit == NULL)
50 goto out_close;
51
52 perf_ui_exit(wait_for_ok);
53
54out_close:
55 dlclose(perf_gtk_handle);
56
57 perf_gtk_handle = NULL;
58}
59#else
60static inline int setup_gtk_browser(void) { return -1; }
61static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
62#endif
Namhyung Kim0985a942012-08-16 17:14:54 +090063
Arnaldo Carvalho de Meloc09615f2016-07-05 11:05:24 -030064int stdio__config_color(const struct option *opt __maybe_unused,
65 const char *mode, int unset __maybe_unused)
66{
67 perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
68 return 0;
69}
70
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020071void setup_browser(bool fallback_to_pager)
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030072{
Namhyung Kim2b676bf2013-02-07 18:02:08 +090073 if (use_browser < 2 && (!isatty(1) || dump_trace))
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030074 use_browser = 0;
Namhyung Kim281ef542012-04-30 13:55:08 +090075
76 /* default to TUI */
77 if (use_browser < 0)
78 use_browser = 1;
79
80 switch (use_browser) {
81 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +090082 if (setup_gtk_browser() == 0)
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090083 break;
Namhyung Kimfc672972013-09-13 15:27:43 +090084 printf("GTK browser requested but could not find %s\n",
85 PERF_GTK_DSO);
86 sleep(1);
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090087 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090088 case 1:
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090089 use_browser = 1;
90 if (ui__init() == 0)
91 break;
92 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090093 default:
Namhyung Kim21f0d422012-05-28 23:53:22 +090094 use_browser = 0;
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020095 if (fallback_to_pager)
96 setup_pager();
Namhyung Kim281ef542012-04-30 13:55:08 +090097 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030098 }
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030099}
100
101void exit_browser(bool wait_for_ok)
102{
Namhyung Kim281ef542012-04-30 13:55:08 +0900103 switch (use_browser) {
104 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +0900105 exit_gtk_browser(wait_for_ok);
Namhyung Kim281ef542012-04-30 13:55:08 +0900106 break;
107
108 case 1:
109 ui__exit(wait_for_ok);
110 break;
111
112 default:
113 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300114 }
115}