setup.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <dlfcn.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <subcmd/pager.h>
  6. #include "../util/debug.h"
  7. #include "../util/hist.h"
  8. #include "ui.h"
  9. struct mutex ui__lock;
  10. void *perf_gtk_handle;
  11. int use_browser = -1;
  12. #define PERF_GTK_DSO "libperf-gtk.so"
  13. #ifdef HAVE_GTK2_SUPPORT
  14. static int setup_gtk_browser(void)
  15. {
  16. int (*perf_ui_init)(void);
  17. if (perf_gtk_handle)
  18. return 0;
  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. perf_ui_init = dlsym(perf_gtk_handle, "perf_gtk__init");
  28. if (perf_ui_init == NULL)
  29. goto out_close;
  30. if (perf_ui_init() == 0)
  31. return 0;
  32. out_close:
  33. dlclose(perf_gtk_handle);
  34. return -1;
  35. }
  36. static void exit_gtk_browser(bool wait_for_ok)
  37. {
  38. void (*perf_ui_exit)(bool);
  39. if (perf_gtk_handle == NULL)
  40. return;
  41. perf_ui_exit = dlsym(perf_gtk_handle, "perf_gtk__exit");
  42. if (perf_ui_exit == NULL)
  43. goto out_close;
  44. perf_ui_exit(wait_for_ok);
  45. out_close:
  46. dlclose(perf_gtk_handle);
  47. perf_gtk_handle = NULL;
  48. }
  49. #else
  50. static inline int setup_gtk_browser(void) { return -1; }
  51. static inline void exit_gtk_browser(bool wait_for_ok __maybe_unused) {}
  52. #endif
  53. int stdio__config_color(const struct option *opt __maybe_unused,
  54. const char *mode, int unset __maybe_unused)
  55. {
  56. perf_use_color_default = perf_config_colorbool("color.ui", mode, -1);
  57. return 0;
  58. }
  59. void setup_browser(bool fallback_to_pager)
  60. {
  61. mutex_init(&ui__lock);
  62. if (use_browser < 2 && (!isatty(1) || dump_trace))
  63. use_browser = 0;
  64. /* default to TUI */
  65. if (use_browser < 0)
  66. use_browser = 1;
  67. switch (use_browser) {
  68. case 2:
  69. if (setup_gtk_browser() == 0)
  70. break;
  71. printf("GTK browser requested but could not find %s\n",
  72. PERF_GTK_DSO);
  73. sleep(1);
  74. use_browser = 1;
  75. /* fall through */
  76. case 1:
  77. if (ui__init() == 0)
  78. break;
  79. /* fall through */
  80. default:
  81. use_browser = 0;
  82. if (fallback_to_pager)
  83. setup_pager();
  84. break;
  85. }
  86. }
  87. void exit_browser(bool wait_for_ok)
  88. {
  89. switch (use_browser) {
  90. case 2:
  91. exit_gtk_browser(wait_for_ok);
  92. break;
  93. case 1:
  94. ui__exit(wait_for_ok);
  95. break;
  96. default:
  97. break;
  98. }
  99. mutex_destroy(&ui__lock);
  100. }
  101. void pthread__block_sigwinch(void)
  102. {
  103. sigset_t set;
  104. sigemptyset(&set);
  105. sigaddset(&set, SIGWINCH);
  106. pthread_sigmask(SIG_BLOCK, &set, NULL);
  107. }
  108. void pthread__unblock_sigwinch(void)
  109. {
  110. sigset_t set;
  111. sigemptyset(&set);
  112. sigaddset(&set, SIGWINCH);
  113. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  114. }