360f43fd54008eb7e259a34d60baaeeb64d0cf3d
[linux-flexiantxendom0-3.2.10.git] / tools / perf / util / ui / util.c
1 #include "../util.h"
2 #include <signal.h>
3 #include <stdbool.h>
4 #include <string.h>
5 #include <sys/ttydefaults.h>
6
7 #include "../cache.h"
8 #include "../debug.h"
9 #include "browser.h"
10 #include "keysyms.h"
11 #include "helpline.h"
12 #include "ui.h"
13 #include "util.h"
14 #include "libslang.h"
15
16 static void ui_browser__argv_write(struct ui_browser *browser,
17                                    void *entry, int row)
18 {
19         char **arg = entry;
20         bool current_entry = ui_browser__is_current_entry(browser, row);
21
22         ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
23                                                        HE_COLORSET_NORMAL);
24         slsmg_write_nstring(*arg, browser->width);
25 }
26
27 static int popup_menu__run(struct ui_browser *menu)
28 {
29         int key;
30
31         if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
32                 return -1;
33
34         while (1) {
35                 key = ui_browser__run(menu, 0);
36
37                 switch (key) {
38                 case K_RIGHT:
39                 case K_ENTER:
40                         key = menu->index;
41                         break;
42                 case K_LEFT:
43                 case K_ESC:
44                 case 'q':
45                 case CTRL('c'):
46                         key = -1;
47                         break;
48                 default:
49                         continue;
50                 }
51
52                 break;
53         }
54
55         ui_browser__hide(menu);
56         return key;
57 }
58
59 int ui__popup_menu(int argc, char * const argv[])
60 {
61         struct ui_browser menu = {
62                 .entries    = (void *)argv,
63                 .refresh    = ui_browser__argv_refresh,
64                 .seek       = ui_browser__argv_seek,
65                 .write      = ui_browser__argv_write,
66                 .nr_entries = argc,
67         };
68
69         return popup_menu__run(&menu);
70 }
71
72 int ui_browser__input_window(const char *title, const char *text, char *input,
73                              const char *exit_msg, int delay_secs)
74 {
75         int x, y, len, key;
76         int max_len = 60, nr_lines = 0;
77         static char buf[50];
78         const char *t;
79
80         t = text;
81         while (1) {
82                 const char *sep = strchr(t, '\n');
83
84                 if (sep == NULL)
85                         sep = strchr(t, '\0');
86                 len = sep - t;
87                 if (max_len < len)
88                         max_len = len;
89                 ++nr_lines;
90                 if (*sep == '\0')
91                         break;
92                 t = sep + 1;
93         }
94
95         max_len += 2;
96         nr_lines += 8;
97         y = SLtt_Screen_Rows / 2 - nr_lines / 2;
98         x = SLtt_Screen_Cols / 2 - max_len / 2;
99
100         SLsmg_set_color(0);
101         SLsmg_draw_box(y, x++, nr_lines, max_len);
102         if (title) {
103                 SLsmg_gotorc(y, x + 1);
104                 SLsmg_write_string((char *)title);
105         }
106         SLsmg_gotorc(++y, x);
107         nr_lines -= 7;
108         max_len -= 2;
109         SLsmg_write_wrapped_string((unsigned char *)text, y, x,
110                                    nr_lines, max_len, 1);
111         y += nr_lines + 1;
112         SLsmg_set_color(0);
113         SLsmg_draw_box(y - 1, x + 1, 3, max_len - 2);
114
115         SLsmg_gotorc(y + 3, x);
116         SLsmg_write_nstring((char *)exit_msg, max_len);
117         SLsmg_refresh();
118
119         x += 2;
120         len = 0;
121         key = ui__getch(delay_secs);
122         while (key != K_TIMER && key != K_ENTER && key != K_ESC) {
123                 if (key == K_BKSPC) {
124                         if (len == 0)
125                                 goto next_key;
126                         SLsmg_gotorc(y, x + --len);
127                         SLsmg_write_char(' ');
128                 } else {
129                         buf[len] = key;
130                         SLsmg_gotorc(y, x + len++);
131                         SLsmg_write_char(key);
132                 }
133                 SLsmg_refresh();
134
135                 /* XXX more graceful overflow handling needed */
136                 if (len == sizeof(buf) - 1) {
137                         ui_helpline__push("maximum size of symbol name reached!");
138                         key = K_ENTER;
139                         break;
140                 }
141 next_key:
142                 key = ui__getch(delay_secs);
143         }
144
145         buf[len] = '\0';
146         strncpy(input, buf, len+1);
147         return key;
148 }
149
150 int ui__question_window(const char *title, const char *text,
151                         const char *exit_msg, int delay_secs)
152 {
153         int x, y;
154         int max_len = 0, nr_lines = 0;
155         const char *t;
156
157         t = text;
158         while (1) {
159                 const char *sep = strchr(t, '\n');
160                 int len;
161
162                 if (sep == NULL)
163                         sep = strchr(t, '\0');
164                 len = sep - t;
165                 if (max_len < len)
166                         max_len = len;
167                 ++nr_lines;
168                 if (*sep == '\0')
169                         break;
170                 t = sep + 1;
171         }
172
173         max_len += 2;
174         nr_lines += 4;
175         y = SLtt_Screen_Rows / 2 - nr_lines / 2,
176         x = SLtt_Screen_Cols / 2 - max_len / 2;
177
178         SLsmg_set_color(0);
179         SLsmg_draw_box(y, x++, nr_lines, max_len);
180         if (title) {
181                 SLsmg_gotorc(y, x + 1);
182                 SLsmg_write_string((char *)title);
183         }
184         SLsmg_gotorc(++y, x);
185         nr_lines -= 2;
186         max_len -= 2;
187         SLsmg_write_wrapped_string((unsigned char *)text, y, x,
188                                    nr_lines, max_len, 1);
189         SLsmg_gotorc(y + nr_lines - 2, x);
190         SLsmg_write_nstring((char *)" ", max_len);
191         SLsmg_gotorc(y + nr_lines - 1, x);
192         SLsmg_write_nstring((char *)exit_msg, max_len);
193         SLsmg_refresh();
194         return ui__getch(delay_secs);
195 }
196
197 int ui__help_window(const char *text)
198 {
199         return ui__question_window("Help", text, "Press any key...", 0);
200 }
201
202 int ui__dialog_yesno(const char *msg)
203 {
204         return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
205 }
206
207 int __ui__warning(const char *title, const char *format, va_list args)
208 {
209         char *s;
210
211         if (use_browser > 0 && vasprintf(&s, format, args) > 0) {
212                 int key;
213
214                 pthread_mutex_lock(&ui__lock);
215                 key = ui__question_window(title, s, "Press any key...", 0);
216                 pthread_mutex_unlock(&ui__lock);
217                 free(s);
218                 return key;
219         }
220
221         fprintf(stderr, "%s:\n", title);
222         vfprintf(stderr, format, args);
223         return K_ESC;
224 }
225
226 int ui__warning(const char *format, ...)
227 {
228         int key;
229         va_list args;
230
231         va_start(args, format);
232         key = __ui__warning("Warning", format, args);
233         va_end(args);
234         return key;
235 }
236
237 int ui__error(const char *format, ...)
238 {
239         int key;
240         va_list args;
241
242         va_start(args, format);
243         key = __ui__warning("Error", format, args);
244         va_end(args);
245         return key;
246 }