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