blob: 1f6b0994f4f4190c8498292771f4ecc3bb09c277 [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;
10
11#ifdef HAVE_GTK2_SUPPORT
12static int setup_gtk_browser(void)
13{
14 int (*perf_ui_init)(void);
15
16 if (perf_gtk_handle)
17 return 0;
18
19 perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
20 if (perf_gtk_handle == NULL) {
21 char buf[PATH_MAX];
22 scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
23 perf_gtk_handle = dlopen(buf, RTLD_LAZY);
24 }
25 if (perf_gtk_handle == NULL)
26 return -1;
27
28 perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
29 if (perf_ui_init == NULL)
30 goto out_close;
31
32 if (perf_ui_init() == 0)
33 return 0;
34
35out_close:
36 dlclose(perf_gtk_handle);
37 return -1;
38}
39
40static void exit_gtk_browser(bool wait_for_ok)
41{
42 void (*perf_ui_exit)(bool);
43
44 if (perf_gtk_handle == NULL)
45 return;
46
47 perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
48 if (perf_ui_exit == NULL)
49 goto out_close;
50
51 perf_ui_exit(wait_for_ok);
52
53out_close:
54 dlclose(perf_gtk_handle);
55
56 perf_gtk_handle = NULL;
57}
58#else
59static inline int setup_gtk_browser(void) { return -1; }
60static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
61#endif
Namhyung Kim0985a942012-08-16 17:14:54 +090062
Arnaldo Carvalho de Meloc09615f2016-07-05 11:05:24 -030063int stdio__config_color(const struct option *opt __maybe_unused,
64 const char *mode, int unset __maybe_unused)
65{
66 perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
67 return 0;
68}
69
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020070void setup_browser(bool fallback_to_pager)
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030071{
Namhyung Kim2b676bf2013-02-07 18:02:08 +090072 if (use_browser < 2 && (!isatty(1) || dump_trace))
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030073 use_browser = 0;
Namhyung Kim281ef542012-04-30 13:55:08 +090074
75 /* default to TUI */
76 if (use_browser < 0)
77 use_browser = 1;
78
79 switch (use_browser) {
80 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +090081 if (setup_gtk_browser() == 0)
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090082 break;
Namhyung Kimfc672972013-09-13 15:27:43 +090083 printf("GTK browser requested but could not find %s\n",
84 PERF_GTK_DSO);
85 sleep(1);
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090086 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090087 case 1:
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090088 use_browser = 1;
89 if (ui__init() == 0)
90 break;
91 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090092 default:
Namhyung Kim21f0d422012-05-28 23:53:22 +090093 use_browser = 0;
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020094 if (fallback_to_pager)
95 setup_pager();
Namhyung Kim281ef542012-04-30 13:55:08 +090096 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030097 }
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030098}
99
100void exit_browser(bool wait_for_ok)
101{
Namhyung Kim281ef542012-04-30 13:55:08 +0900102 switch (use_browser) {
103 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +0900104 exit_gtk_browser(wait_for_ok);
Namhyung Kim281ef542012-04-30 13:55:08 +0900105 break;
106
107 case 1:
108 ui__exit(wait_for_ok);
109 break;
110
111 default:
112 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300113 }
114}