3 #include "../../perf.h"
7 #include <linux/compiler.h>
8 #include <linux/list.h>
9 #include <linux/rbtree.h>
11 #include <sys/ttydefaults.h>
17 static int ui_browser__percent_color(struct ui_browser
*browser
,
18 double percent
, bool current
)
20 if (current
&& (!browser
->use_navkeypressed
|| browser
->navkeypressed
))
21 return HE_COLORSET_SELECTED
;
22 if (percent
>= MIN_RED
)
23 return HE_COLORSET_TOP
;
24 if (percent
>= MIN_GREEN
)
25 return HE_COLORSET_MEDIUM
;
26 return HE_COLORSET_NORMAL
;
29 int ui_browser__set_color(struct ui_browser
*browser
, int color
)
31 int ret
= browser
->current_color
;
32 browser
->current_color
= color
;
33 SLsmg_set_color(color
);
37 void ui_browser__set_percent_color(struct ui_browser
*browser
,
38 double percent
, bool current
)
40 int color
= ui_browser__percent_color(browser
, percent
, current
);
41 ui_browser__set_color(browser
, color
);
44 void ui_browser__gotorc(struct ui_browser
*browser
, int y
, int x
)
46 SLsmg_gotorc(browser
->y
+ y
, browser
->x
+ x
);
49 static struct list_head
*
50 ui_browser__list_head_filter_entries(struct ui_browser
*browser
,
51 struct list_head
*pos
)
54 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
57 } while (pos
!= browser
->entries
);
62 static struct list_head
*
63 ui_browser__list_head_filter_prev_entries(struct ui_browser
*browser
,
64 struct list_head
*pos
)
67 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
70 } while (pos
!= browser
->entries
);
75 void ui_browser__list_head_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
77 struct list_head
*head
= browser
->entries
;
78 struct list_head
*pos
;
80 if (browser
->nr_entries
== 0)
85 pos
= ui_browser__list_head_filter_entries(browser
, head
->next
);
91 pos
= ui_browser__list_head_filter_prev_entries(browser
, head
->prev
);
100 while (offset
-- != 0)
101 pos
= ui_browser__list_head_filter_entries(browser
, pos
->next
);
103 while (offset
++ != 0)
104 pos
= ui_browser__list_head_filter_prev_entries(browser
, pos
->prev
);
110 void ui_browser__rb_tree_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
112 struct rb_root
*root
= browser
->entries
;
130 while (offset
-- != 0)
133 while (offset
++ != 0)
140 unsigned int ui_browser__rb_tree_refresh(struct ui_browser
*browser
)
145 if (browser
->top
== NULL
)
146 browser
->top
= rb_first(browser
->entries
);
151 ui_browser__gotorc(browser
, row
, 0);
152 browser
->write(browser
, nd
, row
);
153 if (++row
== browser
->rows
)
161 bool ui_browser__is_current_entry(struct ui_browser
*browser
, unsigned row
)
163 return browser
->top_idx
+ row
== browser
->index
;
166 void ui_browser__refresh_dimensions(struct ui_browser
*browser
)
168 browser
->width
= SLtt_Screen_Cols
- 1;
169 browser
->height
= browser
->rows
= SLtt_Screen_Rows
- 2;
174 void ui_browser__handle_resize(struct ui_browser
*browser
)
176 ui__refresh_dimensions(false);
177 ui_browser__show(browser
, browser
->title
, ui_helpline__current
);
178 ui_browser__refresh(browser
);
181 int ui_browser__warning(struct ui_browser
*browser
, int timeout
,
182 const char *format
, ...)
188 va_start(args
, format
);
189 err
= vasprintf(&text
, format
, args
);
193 va_start(args
, format
);
194 ui_helpline__vpush(format
, args
);
197 while ((key
= ui__question_window("Warning!", text
,
199 timeout
)) == K_RESIZE
)
200 ui_browser__handle_resize(browser
);
207 int ui_browser__help_window(struct ui_browser
*browser
, const char *text
)
211 while ((key
= ui__help_window(text
)) == K_RESIZE
)
212 ui_browser__handle_resize(browser
);
217 bool ui_browser__dialog_yesno(struct ui_browser
*browser
, const char *text
)
221 while ((key
= ui__dialog_yesno(text
)) == K_RESIZE
)
222 ui_browser__handle_resize(browser
);
224 return key
== K_ENTER
|| toupper(key
) == 'Y';
227 void ui_browser__reset_index(struct ui_browser
*browser
)
229 browser
->index
= browser
->top_idx
= 0;
230 browser
->seek(browser
, 0, SEEK_SET
);
233 void __ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
236 ui_browser__set_color(browser
, HE_COLORSET_ROOT
);
237 slsmg_write_nstring(title
, browser
->width
+ 1);
240 void ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
242 pthread_mutex_lock(&ui__lock
);
243 __ui_browser__show_title(browser
, title
);
244 pthread_mutex_unlock(&ui__lock
);
247 int ui_browser__show(struct ui_browser
*browser
, const char *title
,
248 const char *helpline
, ...)
253 if (browser
->refresh_dimensions
== NULL
)
254 browser
->refresh_dimensions
= ui_browser__refresh_dimensions
;
256 browser
->refresh_dimensions(browser
);
258 pthread_mutex_lock(&ui__lock
);
259 __ui_browser__show_title(browser
, title
);
261 browser
->title
= title
;
262 zfree(&browser
->helpline
);
264 va_start(ap
, helpline
);
265 err
= vasprintf(&browser
->helpline
, helpline
, ap
);
268 ui_helpline__push(browser
->helpline
);
269 pthread_mutex_unlock(&ui__lock
);
273 void ui_browser__hide(struct ui_browser
*browser
)
275 pthread_mutex_lock(&ui__lock
);
277 zfree(&browser
->helpline
);
278 pthread_mutex_unlock(&ui__lock
);
281 static void ui_browser__scrollbar_set(struct ui_browser
*browser
)
283 int height
= browser
->height
, h
= 0, pct
= 0,
284 col
= browser
->width
,
287 if (browser
->nr_entries
> 1) {
288 pct
= ((browser
->index
* (browser
->height
- 1)) /
289 (browser
->nr_entries
- 1));
292 SLsmg_set_char_set(1);
295 ui_browser__gotorc(browser
, row
++, col
);
296 SLsmg_write_char(h
== pct
? SLSMG_DIAMOND_CHAR
: SLSMG_CKBRD_CHAR
);
300 SLsmg_set_char_set(0);
303 static int __ui_browser__refresh(struct ui_browser
*browser
)
306 int width
= browser
->width
;
308 row
= browser
->refresh(browser
);
309 ui_browser__set_color(browser
, HE_COLORSET_NORMAL
);
311 if (!browser
->use_navkeypressed
|| browser
->navkeypressed
)
312 ui_browser__scrollbar_set(browser
);
316 SLsmg_fill_region(browser
->y
+ row
, browser
->x
,
317 browser
->height
- row
, width
, ' ');
322 int ui_browser__refresh(struct ui_browser
*browser
)
324 pthread_mutex_lock(&ui__lock
);
325 __ui_browser__refresh(browser
);
326 pthread_mutex_unlock(&ui__lock
);
332 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
333 * forget about any reference to any entry in the underlying data structure,
334 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
335 * after an output_resort and hist decay.
337 void ui_browser__update_nr_entries(struct ui_browser
*browser
, u32 nr_entries
)
339 off_t offset
= nr_entries
- browser
->nr_entries
;
341 browser
->nr_entries
= nr_entries
;
344 if (browser
->top_idx
< (u64
)-offset
)
345 offset
= -browser
->top_idx
;
347 browser
->index
+= offset
;
348 browser
->top_idx
+= offset
;
352 browser
->seek(browser
, browser
->top_idx
, SEEK_SET
);
355 int ui_browser__run(struct ui_browser
*browser
, int delay_secs
)
362 pthread_mutex_lock(&ui__lock
);
363 err
= __ui_browser__refresh(browser
);
365 pthread_mutex_unlock(&ui__lock
);
369 key
= ui__getch(delay_secs
);
371 if (key
== K_RESIZE
) {
372 ui__refresh_dimensions(false);
373 browser
->refresh_dimensions(browser
);
374 __ui_browser__show_title(browser
, browser
->title
);
375 ui_helpline__puts(browser
->helpline
);
379 if (browser
->use_navkeypressed
&& !browser
->navkeypressed
) {
380 if (key
== K_DOWN
|| key
== K_UP
||
381 key
== K_PGDN
|| key
== K_PGUP
||
382 key
== K_HOME
|| key
== K_END
||
384 browser
->navkeypressed
= true;
392 if (browser
->index
== browser
->nr_entries
- 1)
395 if (browser
->index
== browser
->top_idx
+ browser
->rows
) {
397 browser
->seek(browser
, +1, SEEK_CUR
);
401 if (browser
->index
== 0)
404 if (browser
->index
< browser
->top_idx
) {
406 browser
->seek(browser
, -1, SEEK_CUR
);
411 if (browser
->top_idx
+ browser
->rows
> browser
->nr_entries
- 1)
414 offset
= browser
->rows
;
415 if (browser
->index
+ offset
> browser
->nr_entries
- 1)
416 offset
= browser
->nr_entries
- 1 - browser
->index
;
417 browser
->index
+= offset
;
418 browser
->top_idx
+= offset
;
419 browser
->seek(browser
, +offset
, SEEK_CUR
);
422 if (browser
->top_idx
== 0)
425 if (browser
->top_idx
< browser
->rows
)
426 offset
= browser
->top_idx
;
428 offset
= browser
->rows
;
430 browser
->index
-= offset
;
431 browser
->top_idx
-= offset
;
432 browser
->seek(browser
, -offset
, SEEK_CUR
);
435 ui_browser__reset_index(browser
);
438 offset
= browser
->rows
- 1;
439 if (offset
>= browser
->nr_entries
)
440 offset
= browser
->nr_entries
- 1;
442 browser
->index
= browser
->nr_entries
- 1;
443 browser
->top_idx
= browser
->index
- offset
;
444 browser
->seek(browser
, -offset
, SEEK_END
);
453 unsigned int ui_browser__list_head_refresh(struct ui_browser
*browser
)
455 struct list_head
*pos
;
456 struct list_head
*head
= browser
->entries
;
459 if (browser
->top
== NULL
|| browser
->top
== browser
->entries
)
460 browser
->top
= ui_browser__list_head_filter_entries(browser
, head
->next
);
464 list_for_each_from(pos
, head
) {
465 if (!browser
->filter
|| !browser
->filter(browser
, pos
)) {
466 ui_browser__gotorc(browser
, row
, 0);
467 browser
->write(browser
, pos
, row
);
468 if (++row
== browser
->rows
)
476 static struct ui_browser_colorset
{
477 const char *name
, *fg
, *bg
;
479 } ui_browser__colorsets
[] = {
481 .colorset
= HE_COLORSET_TOP
,
487 .colorset
= HE_COLORSET_MEDIUM
,
493 .colorset
= HE_COLORSET_NORMAL
,
499 .colorset
= HE_COLORSET_SELECTED
,
505 .colorset
= HE_COLORSET_CODE
,
511 .colorset
= HE_COLORSET_ADDR
,
517 .colorset
= HE_COLORSET_ROOT
,
528 static int ui_browser__color_config(const char *var
, const char *value
,
529 void *data __maybe_unused
)
531 char *fg
= NULL
, *bg
;
534 /* same dir for all commands */
535 if (prefixcmp(var
, "colors.") != 0)
538 for (i
= 0; ui_browser__colorsets
[i
].name
!= NULL
; ++i
) {
539 const char *name
= var
+ 7;
541 if (strcmp(ui_browser__colorsets
[i
].name
, name
) != 0)
548 bg
= strchr(fg
, ',');
553 while (isspace(*++bg
));
554 ui_browser__colorsets
[i
].bg
= bg
;
555 ui_browser__colorsets
[i
].fg
= fg
;
563 void ui_browser__argv_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
567 browser
->top
= browser
->entries
;
570 browser
->top
= browser
->top
+ browser
->top_idx
+ offset
;
573 browser
->top
= browser
->top
+ browser
->nr_entries
- 1 + offset
;
580 unsigned int ui_browser__argv_refresh(struct ui_browser
*browser
)
582 unsigned int row
= 0, idx
= browser
->top_idx
;
585 if (browser
->top
== NULL
)
586 browser
->top
= browser
->entries
;
588 pos
= (char **)browser
->top
;
589 while (idx
< browser
->nr_entries
) {
590 if (!browser
->filter
|| !browser
->filter(browser
, *pos
)) {
591 ui_browser__gotorc(browser
, row
, 0);
592 browser
->write(browser
, pos
, row
);
593 if (++row
== browser
->rows
)
604 void __ui_browser__vline(struct ui_browser
*browser
, unsigned int column
,
607 SLsmg_set_char_set(1);
608 ui_browser__gotorc(browser
, start
, column
);
609 SLsmg_draw_vline(end
- start
+ 1);
610 SLsmg_set_char_set(0);
613 void ui_browser__write_graph(struct ui_browser
*browser __maybe_unused
,
616 SLsmg_set_char_set(1);
617 SLsmg_write_char(graph
);
618 SLsmg_set_char_set(0);
621 static void __ui_browser__line_arrow_up(struct ui_browser
*browser
,
625 unsigned int row
, end_row
;
627 SLsmg_set_char_set(1);
629 if (start
< browser
->top_idx
+ browser
->rows
) {
630 row
= start
- browser
->top_idx
;
631 ui_browser__gotorc(browser
, row
, column
);
632 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
633 ui_browser__gotorc(browser
, row
, column
+ 1);
639 row
= browser
->rows
- 1;
641 if (end
> browser
->top_idx
)
642 end_row
= end
- browser
->top_idx
;
646 ui_browser__gotorc(browser
, end_row
, column
);
647 SLsmg_draw_vline(row
- end_row
+ 1);
649 ui_browser__gotorc(browser
, end_row
, column
);
650 if (end
>= browser
->top_idx
) {
651 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
652 ui_browser__gotorc(browser
, end_row
, column
+ 1);
653 SLsmg_write_char(SLSMG_HLINE_CHAR
);
654 ui_browser__gotorc(browser
, end_row
, column
+ 2);
655 SLsmg_write_char(SLSMG_RARROW_CHAR
);
658 SLsmg_set_char_set(0);
661 static void __ui_browser__line_arrow_down(struct ui_browser
*browser
,
665 unsigned int row
, end_row
;
667 SLsmg_set_char_set(1);
669 if (start
>= browser
->top_idx
) {
670 row
= start
- browser
->top_idx
;
671 ui_browser__gotorc(browser
, row
, column
);
672 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
673 ui_browser__gotorc(browser
, row
, column
+ 1);
681 if (end
>= browser
->top_idx
+ browser
->rows
)
682 end_row
= browser
->rows
- 1;
684 end_row
= end
- browser
->top_idx
;
686 ui_browser__gotorc(browser
, row
, column
);
687 SLsmg_draw_vline(end_row
- row
+ 1);
689 ui_browser__gotorc(browser
, end_row
, column
);
690 if (end
< browser
->top_idx
+ browser
->rows
) {
691 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
692 ui_browser__gotorc(browser
, end_row
, column
+ 1);
693 SLsmg_write_char(SLSMG_HLINE_CHAR
);
694 ui_browser__gotorc(browser
, end_row
, column
+ 2);
695 SLsmg_write_char(SLSMG_RARROW_CHAR
);
698 SLsmg_set_char_set(0);
701 void __ui_browser__line_arrow(struct ui_browser
*browser
, unsigned int column
,
705 __ui_browser__line_arrow_up(browser
, column
, start
, end
);
707 __ui_browser__line_arrow_down(browser
, column
, start
, end
);
710 void ui_browser__init(void)
714 perf_config(ui_browser__color_config
, NULL
);
716 while (ui_browser__colorsets
[i
].name
) {
717 struct ui_browser_colorset
*c
= &ui_browser__colorsets
[i
++];
718 sltt_set_color(c
->colorset
, c
->name
, c
->fg
, c
->bg
);
721 annotate_browser__init();