blob: caf1ce6f51527ba5e542caacfb26d06c37829674 [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 Melo9a3993d2017-04-18 11:33:48 -03007#include "../util/util.h"
Arnaldo Carvalho de Melo3f7247e2011-10-18 13:45:16 -02008
Namhyung Kim0985a942012-08-16 17:14:54 +09009pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
Namhyung Kimfc672972013-09-13 15:27:43 +090010void *perf_gtk_handle;
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090011int use_browser = -1;
Namhyung Kimfc672972013-09-13 15:27:43 +090012
Arnaldo Carvalho de Melo5068b522017-04-26 15:31:57 -030013#define PERF_GTK_DSO "libperf-gtk.so"
14
Namhyung Kimfc672972013-09-13 15:27:43 +090015#ifdef HAVE_GTK2_SUPPORT
Arnaldo Carvalho de Melo5068b522017-04-26 15:31:57 -030016
Namhyung Kimfc672972013-09-13 15:27:43 +090017static int setup_gtk_browser(void)
18{
19 int (*perf_ui_init)(void);
20
21 if (perf_gtk_handle)
22 return 0;
23
24 perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
25 if (perf_gtk_handle == NULL) {
26 char buf[PATH_MAX];
27 scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
28 perf_gtk_handle = dlopen(buf, RTLD_LAZY);
29 }
30 if (perf_gtk_handle == NULL)
31 return -1;
32
33 perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
34 if (perf_ui_init == NULL)
35 goto out_close;
36
37 if (perf_ui_init() == 0)
38 return 0;
39
40out_close:
41 dlclose(perf_gtk_handle);
42 return -1;
43}
44
45static void exit_gtk_browser(bool wait_for_ok)
46{
47 void (*perf_ui_exit)(bool);
48
49 if (perf_gtk_handle == NULL)
50 return;
51
52 perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
53 if (perf_ui_exit == NULL)
54 goto out_close;
55
56 perf_ui_exit(wait_for_ok);
57
58out_close:
59 dlclose(perf_gtk_handle);
60
61 perf_gtk_handle = NULL;
62}
63#else
64static inline int setup_gtk_browser(void) { return -1; }
65static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
66#endif
Namhyung Kim0985a942012-08-16 17:14:54 +090067
Arnaldo Carvalho de Meloc09615f2016-07-05 11:05:24 -030068int stdio__config_color(const struct option *opt __maybe_unused,
69 const char *mode, int unset __maybe_unused)
70{
71 perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
72 return 0;
73}
74
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020075void setup_browser(bool fallback_to_pager)
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030076{
Namhyung Kim2b676bf2013-02-07 18:02:08 +090077 if (use_browser < 2 && (!isatty(1) || dump_trace))
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030078 use_browser = 0;
Namhyung Kim281ef542012-04-30 13:55:08 +090079
80 /* default to TUI */
81 if (use_browser < 0)
82 use_browser = 1;
83
84 switch (use_browser) {
85 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +090086 if (setup_gtk_browser() == 0)
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090087 break;
Namhyung Kimfc672972013-09-13 15:27:43 +090088 printf("GTK browser requested but could not find %s\n",
89 PERF_GTK_DSO);
90 sleep(1);
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090091 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090092 case 1:
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090093 use_browser = 1;
94 if (ui__init() == 0)
95 break;
96 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090097 default:
Namhyung Kim21f0d422012-05-28 23:53:22 +090098 use_browser = 0;
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020099 if (fallback_to_pager)
100 setup_pager();
Namhyung Kim281ef542012-04-30 13:55:08 +0900101 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300102 }
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300103}
104
105void exit_browser(bool wait_for_ok)
106{
Namhyung Kim281ef542012-04-30 13:55:08 +0900107 switch (use_browser) {
108 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +0900109 exit_gtk_browser(wait_for_ok);
Namhyung Kim281ef542012-04-30 13:55:08 +0900110 break;
111
112 case 1:
113 ui__exit(wait_for_ok);
114 break;
115
116 default:
117 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300118 }
119}