setup.c 2.1 KB

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