blob: 44fe824e96cd01ab686c4d7c9ee80ca9a4d1cbd6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Namhyung Kim0985a942012-08-16 17:14:54 +09002#include <pthread.h>
Namhyung Kimfc672972013-09-13 15:27:43 +09003#include <dlfcn.h>
Namhyung Kim0985a942012-08-16 17:14:54 +09004
Namhyung Kimea251d52012-09-03 11:53:06 +09005#include "../util/cache.h"
6#include "../util/debug.h"
7#include "../util/hist.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -03008#include "../util/util.h"
Arnaldo Carvalho de Melo3f7247e2011-10-18 13:45:16 -02009
Namhyung Kim0985a942012-08-16 17:14:54 +090010pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
Namhyung Kimfc672972013-09-13 15:27:43 +090011void *perf_gtk_handle;
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090012int use_browser = -1;
Namhyung Kimfc672972013-09-13 15:27:43 +090013
Arnaldo Carvalho de Melo5068b522017-04-26 15:31:57 -030014#define PERF_GTK_DSO "libperf-gtk.so"
15
Namhyung Kimfc672972013-09-13 15:27:43 +090016#ifdef HAVE_GTK2_SUPPORT
Arnaldo Carvalho de Melo5068b522017-04-26 15:31:57 -030017
Namhyung Kimfc672972013-09-13 15:27:43 +090018static 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
41out_close:
42 dlclose(perf_gtk_handle);
43 return -1;
44}
45
46static 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
59out_close:
60 dlclose(perf_gtk_handle);
61
62 perf_gtk_handle = NULL;
63}
64#else
65static inline int setup_gtk_browser(void) { return -1; }
66static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
67#endif
Namhyung Kim0985a942012-08-16 17:14:54 +090068
Arnaldo Carvalho de Meloc09615f2016-07-05 11:05:24 -030069int 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
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -020076void setup_browser(bool fallback_to_pager)
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030077{
Namhyung Kim2b676bf2013-02-07 18:02:08 +090078 if (use_browser < 2 && (!isatty(1) || dump_trace))
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030079 use_browser = 0;
Namhyung Kim281ef542012-04-30 13:55:08 +090080
81 /* default to TUI */
82 if (use_browser < 0)
83 use_browser = 1;
84
85 switch (use_browser) {
86 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +090087 if (setup_gtk_browser() == 0)
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090088 break;
Namhyung Kimfc672972013-09-13 15:27:43 +090089 printf("GTK browser requested but could not find %s\n",
90 PERF_GTK_DSO);
91 sleep(1);
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090092 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090093 case 1:
Namhyung Kimdc41b9b2012-04-30 13:55:09 +090094 use_browser = 1;
95 if (ui__init() == 0)
96 break;
97 /* fall through */
Namhyung Kim281ef542012-04-30 13:55:08 +090098 default:
Namhyung Kim21f0d422012-05-28 23:53:22 +090099 use_browser = 0;
Arnaldo Carvalho de Melo229ade92011-01-31 18:08:39 -0200100 if (fallback_to_pager)
101 setup_pager();
Namhyung Kim281ef542012-04-30 13:55:08 +0900102 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300103 }
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300104}
105
106void exit_browser(bool wait_for_ok)
107{
Namhyung Kim281ef542012-04-30 13:55:08 +0900108 switch (use_browser) {
109 case 2:
Namhyung Kimfc672972013-09-13 15:27:43 +0900110 exit_gtk_browser(wait_for_ok);
Namhyung Kim281ef542012-04-30 13:55:08 +0900111 break;
112
113 case 1:
114 ui__exit(wait_for_ok);
115 break;
116
117 default:
118 break;
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300119 }
120}