fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / ui / tui / setup.c
blob2f612562978cdc13c7e89b6dbddd24c9f928d626
1 #include <signal.h>
2 #include <stdbool.h>
4 #include "../../util/cache.h"
5 #include "../../util/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 #include "tui.h"
14 static volatile int ui__need_resize;
16 extern struct perf_error_ops perf_tui_eops;
18 extern void hist_browser__init_hpp(void);
20 void ui__refresh_dimensions(bool force)
22 if (force || ui__need_resize) {
23 ui__need_resize = 0;
24 pthread_mutex_lock(&ui__lock);
25 SLtt_get_screen_size();
26 SLsmg_reinit_smg();
27 pthread_mutex_unlock(&ui__lock);
31 static void ui__sigwinch(int sig __maybe_unused)
33 ui__need_resize = 1;
36 static void ui__setup_sigwinch(void)
38 static bool done;
40 if (done)
41 return;
43 done = true;
44 pthread__unblock_sigwinch();
45 signal(SIGWINCH, ui__sigwinch);
48 int ui__getch(int delay_secs)
50 struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
51 fd_set read_set;
52 int err, key;
54 ui__setup_sigwinch();
56 FD_ZERO(&read_set);
57 FD_SET(0, &read_set);
59 if (delay_secs) {
60 timeout.tv_sec = delay_secs;
61 timeout.tv_usec = 0;
64 err = select(1, &read_set, NULL, NULL, ptimeout);
66 if (err == 0)
67 return K_TIMER;
69 if (err == -1) {
70 if (errno == EINTR)
71 return K_RESIZE;
72 return K_ERROR;
75 key = SLang_getkey();
76 if (key != K_ESC)
77 return key;
79 FD_ZERO(&read_set);
80 FD_SET(0, &read_set);
81 timeout.tv_sec = 0;
82 timeout.tv_usec = 20;
83 err = select(1, &read_set, NULL, NULL, &timeout);
84 if (err == 0)
85 return K_ESC;
87 SLang_ungetkey(key);
88 return SLkp_getkey();
91 static void ui__signal(int sig)
93 ui__exit(false);
94 psignal(sig, "perf");
95 exit(0);
98 int ui__init(void)
100 int err;
102 SLutf8_enable(-1);
103 SLtt_get_terminfo();
104 SLtt_get_screen_size();
106 err = SLsmg_init_smg();
107 if (err < 0)
108 goto out;
109 err = SLang_init_tty(0, 0, 0);
110 if (err < 0)
111 goto out;
113 err = SLkp_init();
114 if (err < 0) {
115 pr_err("TUI initialization failed.\n");
116 goto out;
119 SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
121 ui_helpline__init();
122 ui_browser__init();
123 tui_progress__init();
125 signal(SIGSEGV, ui__signal);
126 signal(SIGFPE, ui__signal);
127 signal(SIGINT, ui__signal);
128 signal(SIGQUIT, ui__signal);
129 signal(SIGTERM, ui__signal);
131 perf_error__register(&perf_tui_eops);
133 hist_browser__init_hpp();
134 out:
135 return err;
138 void ui__exit(bool wait_for_ok)
140 if (wait_for_ok)
141 ui__question_window("Fatal Error",
142 ui_helpline__last_msg,
143 "Press any key...", 0);
145 SLtt_set_cursor_visibility(1);
146 SLsmg_refresh();
147 SLsmg_reset_smg();
148 SLang_reset_tty();
150 perf_error__unregister(&perf_tui_eops);