setup.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <pthread.h>
  3. #include <dlfcn.h>
  4. #include "../util/cache.h"
  5. #include "../util/debug.h"
  6. #include "../util/hist.h"
  7. #include "../util/util.h"
  8. pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
  9. void *perf_gtk_handle;
  10. int use_browser = -1;
  11. #define PERF_GTK_DSO "libperf-gtk.so"
  12. #ifdef HAVE_GTK2_SUPPORT
  13. static int setup_gtk_browser(void)
  14. {
  15. int (*perf_ui_init)(void);
  16. if (perf_gtk_handle)
  17. return 0;
  18. perf_gtk_handle = dlopen(PERF_GTK_DSO, RTLD_LAZY);
  19. if (perf_gtk_handle == NULL) {
  20. char buf[PATH_MAX];
  21. scnprintf(buf, sizeof(buf), "%s/%s", LIBDIR, PERF_GTK_DSO);
  22. perf_gtk_handle = dlopen(buf, RTLD_LAZY);
  23. }
  24. if (perf_gtk_handle == NULL)
  25. return -1;
  26. perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
  27. if (perf_ui_init == NULL)
  28. goto out_close;
  29. if (perf_ui_init() == 0)
  30. return 0;
  31. out_close:
  32. dlclose(perf_gtk_handle);
  33. return -1;
  34. }
  35. static void exit_gtk_browser(bool wait_for_ok)
  36. {
  37. void (*perf_ui_exit)(bool);
  38. if (perf_gtk_handle == NULL)
  39. return;
  40. perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
  41. if (perf_ui_exit == NULL)
  42. goto out_close;
  43. perf_ui_exit(wait_for_ok);
  44. out_close:
  45. dlclose(perf_gtk_handle);
  46. perf_gtk_handle = NULL;
  47. }
  48. #else
  49. static inline int setup_gtk_browser(void) { return -1; }
  50. static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
  51. #endif
  52. int stdio__config_color(const struct option *opt __maybe_unused,
  53. const char *mode, int unset __maybe_unused)
  54. {
  55. perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
  56. return 0;
  57. }
  58. void setup_browser(bool fallback_to_pager)
  59. {
  60. if (use_browser < 2 && (!isatty(1) || dump_trace))
  61. use_browser = 0;
  62. /* default to TUI */
  63. if (use_browser < 0)
  64. use_browser = 1;
  65. switch (use_browser) {
  66. case 2:
  67. if (setup_gtk_browser() == 0)
  68. break;
  69. printf("GTK browser requested but could not find %s\n",
  70. PERF_GTK_DSO);
  71. sleep(1);
  72. /* fall through */
  73. case 1:
  74. use_browser = 1;
  75. if (ui__init() == 0)
  76. break;
  77. /* fall through */
  78. default:
  79. use_browser = 0;
  80. if (fallback_to_pager)
  81. setup_pager();
  82. break;
  83. }
  84. }
  85. void exit_browser(bool wait_for_ok)
  86. {
  87. switch (use_browser) {
  88. case 2:
  89. exit_gtk_browser(wait_for_ok);
  90. break;
  91. case 1:
  92. ui__exit(wait_for_ok);
  93. break;
  94. default:
  95. break;
  96. }
  97. }