setup.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. void setup_browser(bool fallback_to_pager)
  49. {
  50. if (use_browser < 2 && (!isatty(1) || dump_trace))
  51. use_browser = 0;
  52. /* default to TUI */
  53. if (use_browser < 0)
  54. use_browser = 1;
  55. switch (use_browser) {
  56. case 2:
  57. if (setup_gtk_browser() == 0)
  58. break;
  59. printf("GTK browser requested but could not find %s\n",
  60. PERF_GTK_DSO);
  61. sleep(1);
  62. /* fall through */
  63. case 1:
  64. use_browser = 1;
  65. if (ui__init() == 0)
  66. break;
  67. /* fall through */
  68. default:
  69. use_browser = 0;
  70. if (fallback_to_pager)
  71. setup_pager();
  72. break;
  73. }
  74. }
  75. void exit_browser(bool wait_for_ok)
  76. {
  77. switch (use_browser) {
  78. case 2:
  79. exit_gtk_browser(wait_for_ok);
  80. break;
  81. case 1:
  82. ui__exit(wait_for_ok);
  83. break;
  84. default:
  85. break;
  86. }
  87. }