setup.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <stdbool.h>
  5. #include <linux/kernel.h>
  6. #ifdef HAVE_BACKTRACE_SUPPORT
  7. #include <execinfo.h>
  8. #endif
  9. #include "../../util/cache.h"
  10. #include "../../util/debug.h"
  11. #include "../../util/util.h"
  12. #include "../browser.h"
  13. #include "../helpline.h"
  14. #include "../ui.h"
  15. #include "../util.h"
  16. #include "../libslang.h"
  17. #include "../keysyms.h"
  18. #include "tui.h"
  19. static volatile int ui__need_resize;
  20. extern struct perf_error_ops perf_tui_eops;
  21. extern bool tui_helpline__set;
  22. extern void hist_browser__init_hpp(void);
  23. void ui__refresh_dimensions(bool force)
  24. {
  25. if (force || ui__need_resize) {
  26. ui__need_resize = 0;
  27. pthread_mutex_lock(&ui__lock);
  28. SLtt_get_screen_size();
  29. SLsmg_reinit_smg();
  30. pthread_mutex_unlock(&ui__lock);
  31. }
  32. }
  33. static void ui__sigwinch(int sig __maybe_unused)
  34. {
  35. ui__need_resize = 1;
  36. }
  37. static void ui__setup_sigwinch(void)
  38. {
  39. static bool done;
  40. if (done)
  41. return;
  42. done = true;
  43. pthread__unblock_sigwinch();
  44. signal(SIGWINCH, ui__sigwinch);
  45. }
  46. int ui__getch(int delay_secs)
  47. {
  48. struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
  49. fd_set read_set;
  50. int err, key;
  51. ui__setup_sigwinch();
  52. FD_ZERO(&read_set);
  53. FD_SET(0, &read_set);
  54. if (delay_secs) {
  55. timeout.tv_sec = delay_secs;
  56. timeout.tv_usec = 0;
  57. }
  58. err = select(1, &read_set, NULL, NULL, ptimeout);
  59. if (err == 0)
  60. return K_TIMER;
  61. if (err == -1) {
  62. if (errno == EINTR)
  63. return K_RESIZE;
  64. return K_ERROR;
  65. }
  66. key = SLang_getkey();
  67. if (key != K_ESC)
  68. return key;
  69. FD_ZERO(&read_set);
  70. FD_SET(0, &read_set);
  71. timeout.tv_sec = 0;
  72. timeout.tv_usec = 20;
  73. err = select(1, &read_set, NULL, NULL, &timeout);
  74. if (err == 0)
  75. return K_ESC;
  76. SLang_ungetkey(key);
  77. return SLkp_getkey();
  78. }
  79. #ifdef HAVE_BACKTRACE_SUPPORT
  80. static void ui__signal_backtrace(int sig)
  81. {
  82. void *stackdump[32];
  83. size_t size;
  84. ui__exit(false);
  85. psignal(sig, "perf");
  86. printf("-------- backtrace --------\n");
  87. size = backtrace(stackdump, ARRAY_SIZE(stackdump));
  88. backtrace_symbols_fd(stackdump, size, STDOUT_FILENO);
  89. exit(0);
  90. }
  91. #else
  92. # define ui__signal_backtrace ui__signal
  93. #endif
  94. static void ui__signal(int sig)
  95. {
  96. ui__exit(false);
  97. psignal(sig, "perf");
  98. exit(0);
  99. }
  100. int ui__init(void)
  101. {
  102. int err;
  103. SLutf8_enable(-1);
  104. SLtt_get_terminfo();
  105. SLtt_get_screen_size();
  106. err = SLsmg_init_smg();
  107. if (err < 0)
  108. goto out;
  109. err = SLang_init_tty(-1, 0, 0);
  110. if (err < 0)
  111. goto out;
  112. err = SLkp_init();
  113. if (err < 0) {
  114. pr_err("TUI initialization failed.\n");
  115. goto out;
  116. }
  117. SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
  118. signal(SIGSEGV, ui__signal_backtrace);
  119. signal(SIGFPE, ui__signal_backtrace);
  120. signal(SIGINT, ui__signal);
  121. signal(SIGQUIT, ui__signal);
  122. signal(SIGTERM, ui__signal);
  123. perf_error__register(&perf_tui_eops);
  124. ui_helpline__init();
  125. ui_browser__init();
  126. tui_progress__init();
  127. hist_browser__init_hpp();
  128. out:
  129. return err;
  130. }
  131. void ui__exit(bool wait_for_ok)
  132. {
  133. if (wait_for_ok && tui_helpline__set)
  134. ui__question_window("Fatal Error",
  135. ui_helpline__last_msg,
  136. "Press any key...", 0);
  137. SLtt_set_cursor_visibility(1);
  138. SLsmg_refresh();
  139. SLsmg_reset_smg();
  140. SLang_reset_tty();
  141. perf_error__unregister(&perf_tui_eops);
  142. }