setup.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <newt.h>
  2. #include <signal.h>
  3. #include <stdbool.h>
  4. #include "../cache.h"
  5. #include "../debug.h"
  6. #include "browser.h"
  7. #include "helpline.h"
  8. #include "ui.h"
  9. #include "util.h"
  10. #include "libslang.h"
  11. #include "keysyms.h"
  12. pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
  13. static volatile int ui__need_resize;
  14. void ui__refresh_dimensions(bool force)
  15. {
  16. if (force || ui__need_resize) {
  17. ui__need_resize = 0;
  18. pthread_mutex_lock(&ui__lock);
  19. SLtt_get_screen_size();
  20. SLsmg_reinit_smg();
  21. pthread_mutex_unlock(&ui__lock);
  22. }
  23. }
  24. static void ui__sigwinch(int sig __used)
  25. {
  26. ui__need_resize = 1;
  27. }
  28. static void ui__setup_sigwinch(void)
  29. {
  30. static bool done;
  31. if (done)
  32. return;
  33. done = true;
  34. pthread__unblock_sigwinch();
  35. signal(SIGWINCH, ui__sigwinch);
  36. }
  37. int ui__getch(int delay_secs)
  38. {
  39. struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
  40. fd_set read_set;
  41. int err, key;
  42. ui__setup_sigwinch();
  43. FD_ZERO(&read_set);
  44. FD_SET(0, &read_set);
  45. if (delay_secs) {
  46. timeout.tv_sec = delay_secs;
  47. timeout.tv_usec = 0;
  48. }
  49. err = select(1, &read_set, NULL, NULL, ptimeout);
  50. if (err == 0)
  51. return K_TIMER;
  52. if (err == -1) {
  53. if (errno == EINTR)
  54. return K_RESIZE;
  55. return K_ERROR;
  56. }
  57. key = SLang_getkey();
  58. if (key != K_ESC)
  59. return key;
  60. FD_ZERO(&read_set);
  61. FD_SET(0, &read_set);
  62. timeout.tv_sec = 0;
  63. timeout.tv_usec = 20;
  64. err = select(1, &read_set, NULL, NULL, &timeout);
  65. if (err == 0)
  66. return K_ESC;
  67. SLang_ungetkey(key);
  68. return SLkp_getkey();
  69. }
  70. static void newt_suspend(void *d __used)
  71. {
  72. newtSuspend();
  73. raise(SIGTSTP);
  74. newtResume();
  75. }
  76. static int ui__init(void)
  77. {
  78. int err = SLkp_init();
  79. if (err < 0)
  80. goto out;
  81. SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
  82. out:
  83. return err;
  84. }
  85. static void ui__exit(void)
  86. {
  87. SLtt_set_cursor_visibility(1);
  88. SLsmg_refresh();
  89. SLsmg_reset_smg();
  90. SLang_reset_tty();
  91. }
  92. static void ui__signal(int sig)
  93. {
  94. ui__exit();
  95. psignal(sig, "perf");
  96. exit(0);
  97. }
  98. void setup_browser(bool fallback_to_pager)
  99. {
  100. if (!isatty(1) || !use_browser || dump_trace) {
  101. use_browser = 0;
  102. if (fallback_to_pager)
  103. setup_pager();
  104. return;
  105. }
  106. use_browser = 1;
  107. newtInit();
  108. ui__init();
  109. newtSetSuspendCallback(newt_suspend, NULL);
  110. ui_helpline__init();
  111. ui_browser__init();
  112. signal(SIGSEGV, ui__signal);
  113. signal(SIGFPE, ui__signal);
  114. signal(SIGINT, ui__signal);
  115. signal(SIGQUIT, ui__signal);
  116. signal(SIGTERM, ui__signal);
  117. }
  118. void exit_browser(bool wait_for_ok)
  119. {
  120. if (use_browser > 0) {
  121. if (wait_for_ok)
  122. ui__question_window("Fatal Error",
  123. ui_helpline__last_msg,
  124. "Press any key...", 0);
  125. ui__exit();
  126. }
  127. }