Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 1 | #include <newt.h> |
| 2 | #include <signal.h> |
| 3 | #include <stdbool.h> |
| 4 | |
| 5 | #include "../cache.h" |
| 6 | #include "../debug.h" |
| 7 | #include "browser.h" |
| 8 | #include "helpline.h" |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 9 | #include "ui.h" |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 10 | #include "util.h" |
Arnaldo Carvalho de Melo | 3f7247e | 2011-10-18 13:45:16 -0200 | [diff] [blame] | 11 | #include "libslang.h" |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 12 | #include "keysyms.h" |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 13 | |
| 14 | pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER; |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 16 | static volatile int ui__need_resize; |
| 17 | |
| 18 | void ui__refresh_dimensions(bool force) |
| 19 | { |
| 20 | if (force || ui__need_resize) { |
| 21 | ui__need_resize = 0; |
| 22 | pthread_mutex_lock(&ui__lock); |
| 23 | SLtt_get_screen_size(); |
| 24 | SLsmg_reinit_smg(); |
| 25 | pthread_mutex_unlock(&ui__lock); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | static void ui__sigwinch(int sig __used) |
| 30 | { |
| 31 | ui__need_resize = 1; |
| 32 | } |
| 33 | |
| 34 | static void ui__setup_sigwinch(void) |
| 35 | { |
| 36 | static bool done; |
| 37 | |
| 38 | if (done) |
| 39 | return; |
| 40 | |
| 41 | done = true; |
| 42 | pthread__unblock_sigwinch(); |
| 43 | signal(SIGWINCH, ui__sigwinch); |
| 44 | } |
| 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 | |
| 52 | ui__setup_sigwinch(); |
| 53 | |
| 54 | FD_ZERO(&read_set); |
| 55 | FD_SET(0, &read_set); |
| 56 | |
| 57 | if (delay_secs) { |
| 58 | timeout.tv_sec = delay_secs; |
| 59 | timeout.tv_usec = 0; |
| 60 | } |
| 61 | |
| 62 | err = select(1, &read_set, NULL, NULL, ptimeout); |
| 63 | |
| 64 | if (err == 0) |
| 65 | return K_TIMER; |
| 66 | |
| 67 | if (err == -1) { |
| 68 | if (errno == EINTR) |
| 69 | return K_RESIZE; |
| 70 | return K_ERROR; |
| 71 | } |
| 72 | |
| 73 | key = SLang_getkey(); |
| 74 | if (key != K_ESC) |
| 75 | return key; |
| 76 | |
| 77 | FD_ZERO(&read_set); |
| 78 | FD_SET(0, &read_set); |
| 79 | timeout.tv_sec = 0; |
| 80 | timeout.tv_usec = 20; |
| 81 | err = select(1, &read_set, NULL, NULL, &timeout); |
| 82 | if (err == 0) |
| 83 | return K_ESC; |
| 84 | |
| 85 | SLang_ungetkey(key); |
| 86 | return SLkp_getkey(); |
| 87 | } |
| 88 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 89 | static void newt_suspend(void *d __used) |
| 90 | { |
| 91 | newtSuspend(); |
| 92 | raise(SIGTSTP); |
| 93 | newtResume(); |
| 94 | } |
| 95 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 96 | static int ui__init(void) |
| 97 | { |
| 98 | int err = SLkp_init(); |
| 99 | |
| 100 | if (err < 0) |
| 101 | goto out; |
| 102 | |
| 103 | SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB); |
| 104 | out: |
| 105 | return err; |
| 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 3f7247e | 2011-10-18 13:45:16 -0200 | [diff] [blame] | 108 | static void ui__exit(void) |
| 109 | { |
| 110 | SLtt_set_cursor_visibility(1); |
| 111 | SLsmg_refresh(); |
| 112 | SLsmg_reset_smg(); |
| 113 | SLang_reset_tty(); |
| 114 | } |
| 115 | |
| 116 | static void ui__signal(int sig) |
| 117 | { |
| 118 | ui__exit(); |
| 119 | psignal(sig, "perf"); |
| 120 | exit(0); |
| 121 | } |
| 122 | |
Arnaldo Carvalho de Melo | 229ade9 | 2011-01-31 18:08:39 -0200 | [diff] [blame] | 123 | void setup_browser(bool fallback_to_pager) |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 124 | { |
| 125 | if (!isatty(1) || !use_browser || dump_trace) { |
| 126 | use_browser = 0; |
Arnaldo Carvalho de Melo | 229ade9 | 2011-01-31 18:08:39 -0200 | [diff] [blame] | 127 | if (fallback_to_pager) |
| 128 | setup_pager(); |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
| 132 | use_browser = 1; |
| 133 | newtInit(); |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 134 | ui__init(); |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 135 | newtSetSuspendCallback(newt_suspend, NULL); |
| 136 | ui_helpline__init(); |
| 137 | ui_browser__init(); |
Arnaldo Carvalho de Melo | 3f7247e | 2011-10-18 13:45:16 -0200 | [diff] [blame] | 138 | |
| 139 | signal(SIGSEGV, ui__signal); |
| 140 | signal(SIGFPE, ui__signal); |
| 141 | signal(SIGINT, ui__signal); |
| 142 | signal(SIGQUIT, ui__signal); |
| 143 | signal(SIGTERM, ui__signal); |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void exit_browser(bool wait_for_ok) |
| 147 | { |
| 148 | if (use_browser > 0) { |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 149 | if (wait_for_ok) |
| 150 | ui__question_window("Fatal Error", |
| 151 | ui_helpline__last_msg, |
| 152 | "Press any key...", 0); |
Arnaldo Carvalho de Melo | 3f7247e | 2011-10-18 13:45:16 -0200 | [diff] [blame] | 153 | ui__exit(); |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 154 | } |
| 155 | } |