xref: /linux/tools/perf/ui/tui/setup.c (revision 26b0d14106954ae46d2f4f7eec3481828a210f7d)
1 #include <newt.h>
2 #include <signal.h>
3 #include <stdbool.h>
4 
5 #include "../../util/cache.h"
6 #include "../../util/debug.h"
7 #include "../browser.h"
8 #include "../helpline.h"
9 #include "../ui.h"
10 #include "../util.h"
11 #include "../libslang.h"
12 #include "../keysyms.h"
13 
14 pthread_mutex_t ui__lock = PTHREAD_MUTEX_INITIALIZER;
15 
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 
89 static void newt_suspend(void *d __used)
90 {
91 	newtSuspend();
92 	raise(SIGTSTP);
93 	newtResume();
94 }
95 
96 static void ui__signal(int sig)
97 {
98 	ui__exit(false);
99 	psignal(sig, "perf");
100 	exit(0);
101 }
102 
103 int ui__init(void)
104 {
105 	int err;
106 
107 	newtInit();
108 	err = SLkp_init();
109 	if (err < 0) {
110 		pr_err("TUI initialization failed.\n");
111 		goto out;
112 	}
113 
114 	SLkp_define_keysym((char *)"^(kB)", SL_KEY_UNTAB);
115 
116 	newtSetSuspendCallback(newt_suspend, NULL);
117 	ui_helpline__init();
118 	ui_browser__init();
119 
120 	signal(SIGSEGV, ui__signal);
121 	signal(SIGFPE, ui__signal);
122 	signal(SIGINT, ui__signal);
123 	signal(SIGQUIT, ui__signal);
124 	signal(SIGTERM, ui__signal);
125 out:
126 	return err;
127 }
128 
129 void ui__exit(bool wait_for_ok)
130 {
131 	if (wait_for_ok)
132 		ui__question_window("Fatal Error",
133 				    ui_helpline__last_msg,
134 				    "Press any key...", 0);
135 
136 	SLtt_set_cursor_visibility(1);
137 	SLsmg_refresh();
138 	SLsmg_reset_smg();
139 	SLang_reset_tty();
140 }
141