perf ui browser: Handle SIGWINCH
[deliverable/linux.git] / tools / perf / util / ui / browser.c
1 #include "../util.h"
2 #include "../../perf.h"
3 #include "libslang.h"
4 #include <newt.h>
5 #include "ui.h"
6 #include <linux/compiler.h>
7 #include <linux/list.h>
8 #include <linux/rbtree.h>
9 #include <stdlib.h>
10 #include <sys/ttydefaults.h>
11 #include "browser.h"
12 #include "helpline.h"
13 #include "../color.h"
14
15 int newtGetKey(void);
16
17 static int ui_browser__percent_color(double percent, bool current)
18 {
19 if (current)
20 return HE_COLORSET_SELECTED;
21 if (percent >= MIN_RED)
22 return HE_COLORSET_TOP;
23 if (percent >= MIN_GREEN)
24 return HE_COLORSET_MEDIUM;
25 return HE_COLORSET_NORMAL;
26 }
27
28 void ui_browser__set_color(struct ui_browser *self __used, int color)
29 {
30 SLsmg_set_color(color);
31 }
32
33 void ui_browser__set_percent_color(struct ui_browser *self,
34 double percent, bool current)
35 {
36 int color = ui_browser__percent_color(percent, current);
37 ui_browser__set_color(self, color);
38 }
39
40 void ui_browser__gotorc(struct ui_browser *self, int y, int x)
41 {
42 SLsmg_gotorc(self->y + y, self->x + x);
43 }
44
45 void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
46 {
47 struct list_head *head = self->entries;
48 struct list_head *pos;
49
50 switch (whence) {
51 case SEEK_SET:
52 pos = head->next;
53 break;
54 case SEEK_CUR:
55 pos = self->top;
56 break;
57 case SEEK_END:
58 pos = head->prev;
59 break;
60 default:
61 return;
62 }
63
64 if (offset > 0) {
65 while (offset-- != 0)
66 pos = pos->next;
67 } else {
68 while (offset++ != 0)
69 pos = pos->prev;
70 }
71
72 self->top = pos;
73 }
74
75 void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
76 {
77 struct rb_root *root = self->entries;
78 struct rb_node *nd;
79
80 switch (whence) {
81 case SEEK_SET:
82 nd = rb_first(root);
83 break;
84 case SEEK_CUR:
85 nd = self->top;
86 break;
87 case SEEK_END:
88 nd = rb_last(root);
89 break;
90 default:
91 return;
92 }
93
94 if (offset > 0) {
95 while (offset-- != 0)
96 nd = rb_next(nd);
97 } else {
98 while (offset++ != 0)
99 nd = rb_prev(nd);
100 }
101
102 self->top = nd;
103 }
104
105 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
106 {
107 struct rb_node *nd;
108 int row = 0;
109
110 if (self->top == NULL)
111 self->top = rb_first(self->entries);
112
113 nd = self->top;
114
115 while (nd != NULL) {
116 ui_browser__gotorc(self, row, 0);
117 self->write(self, nd, row);
118 if (++row == self->height)
119 break;
120 nd = rb_next(nd);
121 }
122
123 return row;
124 }
125
126 bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
127 {
128 return self->top_idx + row == self->index;
129 }
130
131 void ui_browser__refresh_dimensions(struct ui_browser *self)
132 {
133 self->width = SLtt_Screen_Cols - 1;
134 self->height = SLtt_Screen_Rows - 2;
135 self->y = 1;
136 self->x = 0;
137 }
138
139 void ui_browser__reset_index(struct ui_browser *self)
140 {
141 self->index = self->top_idx = 0;
142 self->seek(self, 0, SEEK_SET);
143 }
144
145 void ui_browser__add_exit_key(struct ui_browser *browser __used, int key __used)
146 {
147 }
148
149 void ui_browser__add_exit_keys(struct ui_browser *self, int keys[])
150 {
151 int i = 0;
152
153 while (keys[i] && i < 64) {
154 ui_browser__add_exit_key(self, keys[i]);
155 ++i;
156 }
157 }
158
159 void __ui_browser__show_title(struct ui_browser *browser, const char *title)
160 {
161 SLsmg_gotorc(0, 0);
162 ui_browser__set_color(browser, NEWT_COLORSET_ROOT);
163 slsmg_write_nstring(title, browser->width + 1);
164 }
165
166 void ui_browser__show_title(struct ui_browser *browser, const char *title)
167 {
168 pthread_mutex_lock(&ui__lock);
169 __ui_browser__show_title(browser, title);
170 pthread_mutex_unlock(&ui__lock);
171 }
172
173 int ui_browser__show(struct ui_browser *self, const char *title,
174 const char *helpline, ...)
175 {
176 int err;
177 va_list ap;
178 int keys[] = { NEWT_KEY_UP, NEWT_KEY_DOWN, NEWT_KEY_PGUP,
179 NEWT_KEY_PGDN, NEWT_KEY_HOME, NEWT_KEY_END, ' ',
180 NEWT_KEY_LEFT, NEWT_KEY_ESCAPE, 'q', CTRL('c'), 0 };
181
182 ui_browser__refresh_dimensions(self);
183
184 pthread_mutex_lock(&ui__lock);
185 __ui_browser__show_title(self, title);
186
187 ui_browser__add_exit_keys(self, keys);
188 self->title = title;
189 free(self->helpline);
190 self->helpline = NULL;
191
192 va_start(ap, helpline);
193 err = vasprintf(&self->helpline, helpline, ap);
194 va_end(ap);
195 if (err > 0)
196 ui_helpline__push(self->helpline);
197 pthread_mutex_unlock(&ui__lock);
198 return err ? 0 : -1;
199 }
200
201 void ui_browser__hide(struct ui_browser *browser __used)
202 {
203 pthread_mutex_lock(&ui__lock);
204 ui_helpline__pop();
205 pthread_mutex_unlock(&ui__lock);
206 }
207
208 static void ui_browser__scrollbar_set(struct ui_browser *browser)
209 {
210 int height = browser->height, h = 0, pct = 0,
211 col = browser->width,
212 row = browser->y - 1;
213
214 if (browser->nr_entries > 1) {
215 pct = ((browser->index * (browser->height - 1)) /
216 (browser->nr_entries - 1));
217 }
218
219 while (h < height) {
220 ui_browser__gotorc(browser, row++, col);
221 SLsmg_set_char_set(1);
222 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_BOARD_CHAR);
223 SLsmg_set_char_set(0);
224 ++h;
225 }
226 }
227
228 static int __ui_browser__refresh(struct ui_browser *browser)
229 {
230 int row;
231
232 row = browser->refresh(browser);
233 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
234 SLsmg_fill_region(browser->y + row, browser->x,
235 browser->height - row, browser->width, ' ');
236 ui_browser__scrollbar_set(browser);
237
238 return 0;
239 }
240
241 int ui_browser__refresh(struct ui_browser *browser)
242 {
243 pthread_mutex_lock(&ui__lock);
244 __ui_browser__refresh(browser);
245 pthread_mutex_unlock(&ui__lock);
246
247 return 0;
248 }
249
250 /*
251 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
252 * forget about any reference to any entry in the underlying data structure,
253 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
254 * after an output_resort and hist decay.
255 */
256 void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
257 {
258 off_t offset = nr_entries - browser->nr_entries;
259
260 browser->nr_entries = nr_entries;
261
262 if (offset < 0) {
263 if (browser->top_idx < (u64)-offset)
264 offset = -browser->top_idx;
265
266 browser->index += offset;
267 browser->top_idx += offset;
268 }
269
270 browser->seek(browser, browser->top_idx, SEEK_SET);
271 }
272
273 int ui_browser__run(struct ui_browser *self, int delay_secs)
274 {
275 int err, key;
276 struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
277
278 pthread__unblock_sigwinch();
279
280 while (1) {
281 off_t offset;
282 fd_set read_set;
283
284 pthread_mutex_lock(&ui__lock);
285 err = __ui_browser__refresh(self);
286 SLsmg_refresh();
287 pthread_mutex_unlock(&ui__lock);
288 if (err < 0)
289 break;
290
291 FD_ZERO(&read_set);
292 FD_SET(0, &read_set);
293
294 if (delay_secs) {
295 timeout.tv_sec = delay_secs;
296 timeout.tv_usec = 0;
297 }
298
299 err = select(1, &read_set, NULL, NULL, ptimeout);
300 if (err > 0 && FD_ISSET(0, &read_set))
301 key = newtGetKey();
302 else if (err == 0)
303 break;
304 else {
305 pthread_mutex_lock(&ui__lock);
306 SLtt_get_screen_size();
307 SLsmg_reinit_smg();
308 pthread_mutex_unlock(&ui__lock);
309 ui_browser__refresh_dimensions(self);
310 __ui_browser__show_title(self, self->title);
311 ui_helpline__puts(self->helpline);
312 continue;
313 }
314
315 switch (key) {
316 case NEWT_KEY_DOWN:
317 if (self->index == self->nr_entries - 1)
318 break;
319 ++self->index;
320 if (self->index == self->top_idx + self->height) {
321 ++self->top_idx;
322 self->seek(self, +1, SEEK_CUR);
323 }
324 break;
325 case NEWT_KEY_UP:
326 if (self->index == 0)
327 break;
328 --self->index;
329 if (self->index < self->top_idx) {
330 --self->top_idx;
331 self->seek(self, -1, SEEK_CUR);
332 }
333 break;
334 case NEWT_KEY_PGDN:
335 case ' ':
336 if (self->top_idx + self->height > self->nr_entries - 1)
337 break;
338
339 offset = self->height;
340 if (self->index + offset > self->nr_entries - 1)
341 offset = self->nr_entries - 1 - self->index;
342 self->index += offset;
343 self->top_idx += offset;
344 self->seek(self, +offset, SEEK_CUR);
345 break;
346 case NEWT_KEY_PGUP:
347 if (self->top_idx == 0)
348 break;
349
350 if (self->top_idx < self->height)
351 offset = self->top_idx;
352 else
353 offset = self->height;
354
355 self->index -= offset;
356 self->top_idx -= offset;
357 self->seek(self, -offset, SEEK_CUR);
358 break;
359 case NEWT_KEY_HOME:
360 ui_browser__reset_index(self);
361 break;
362 case NEWT_KEY_END:
363 offset = self->height - 1;
364 if (offset >= self->nr_entries)
365 offset = self->nr_entries - 1;
366
367 self->index = self->nr_entries - 1;
368 self->top_idx = self->index - offset;
369 self->seek(self, -offset, SEEK_END);
370 break;
371 default:
372 return key;
373 }
374 }
375 return -1;
376 }
377
378 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
379 {
380 struct list_head *pos;
381 struct list_head *head = self->entries;
382 int row = 0;
383
384 if (self->top == NULL || self->top == self->entries)
385 self->top = head->next;
386
387 pos = self->top;
388
389 list_for_each_from(pos, head) {
390 ui_browser__gotorc(self, row, 0);
391 self->write(self, pos, row);
392 if (++row == self->height)
393 break;
394 }
395
396 return row;
397 }
398
399 static struct ui_browser__colors {
400 const char *topColorFg, *topColorBg;
401 const char *mediumColorFg, *mediumColorBg;
402 const char *normalColorFg, *normalColorBg;
403 const char *selColorFg, *selColorBg;
404 const char *codeColorFg, *codeColorBg;
405 } ui_browser__default_colors = {
406 "red", "lightgray",
407 "green", "lightgray",
408 "black", "lightgray",
409 "lightgray", "magenta",
410 "blue", "lightgray",
411 };
412
413 void ui_browser__init(void)
414 {
415 struct ui_browser__colors *c = &ui_browser__default_colors;
416
417 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
418 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
419 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
420 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
421 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
422 }
This page took 0.054203 seconds and 6 git commands to generate.