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 void ui_browser__write_nstring(struct ui_browser
*browser __maybe_unused
, const char *msg
,
52 slsmg_write_nstring(msg
, width
);
55 void ui_browser__printf(struct ui_browser
*browser __maybe_unused
, const char *fmt
, ...)
60 slsmg_vprintf(fmt
, args
);
64 static struct list_head
*
65 ui_browser__list_head_filter_entries(struct ui_browser
*browser
,
66 struct list_head
*pos
)
69 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
72 } while (pos
!= browser
->entries
);
77 static struct list_head
*
78 ui_browser__list_head_filter_prev_entries(struct ui_browser
*browser
,
79 struct list_head
*pos
)
82 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
85 } while (pos
!= browser
->entries
);
90 void ui_browser__list_head_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
92 struct list_head
*head
= browser
->entries
;
93 struct list_head
*pos
;
95 if (browser
->nr_entries
== 0)
100 pos
= ui_browser__list_head_filter_entries(browser
, head
->next
);
106 pos
= ui_browser__list_head_filter_prev_entries(browser
, head
->prev
);
115 while (offset
-- != 0)
116 pos
= ui_browser__list_head_filter_entries(browser
, pos
->next
);
118 while (offset
++ != 0)
119 pos
= ui_browser__list_head_filter_prev_entries(browser
, pos
->prev
);
125 void ui_browser__rb_tree_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
127 struct rb_root
*root
= browser
->entries
;
145 while (offset
-- != 0)
148 while (offset
++ != 0)
155 unsigned int ui_browser__rb_tree_refresh(struct ui_browser
*browser
)
160 if (browser
->top
== NULL
)
161 browser
->top
= rb_first(browser
->entries
);
166 ui_browser__gotorc(browser
, row
, 0);
167 browser
->write(browser
, nd
, row
);
168 if (++row
== browser
->rows
)
176 bool ui_browser__is_current_entry(struct ui_browser
*browser
, unsigned row
)
178 return browser
->top_idx
+ row
== browser
->index
;
181 void ui_browser__refresh_dimensions(struct ui_browser
*browser
)
183 browser
->width
= SLtt_Screen_Cols
- 1;
184 browser
->height
= browser
->rows
= SLtt_Screen_Rows
- 2;
189 void ui_browser__handle_resize(struct ui_browser
*browser
)
191 ui__refresh_dimensions(false);
192 ui_browser__show(browser
, browser
->title
, ui_helpline__current
);
193 ui_browser__refresh(browser
);
196 int ui_browser__warning(struct ui_browser
*browser
, int timeout
,
197 const char *format
, ...)
203 va_start(args
, format
);
204 err
= vasprintf(&text
, format
, args
);
208 va_start(args
, format
);
209 ui_helpline__vpush(format
, args
);
212 while ((key
= ui__question_window("Warning!", text
,
214 timeout
)) == K_RESIZE
)
215 ui_browser__handle_resize(browser
);
222 int ui_browser__help_window(struct ui_browser
*browser
, const char *text
)
226 while ((key
= ui__help_window(text
)) == K_RESIZE
)
227 ui_browser__handle_resize(browser
);
232 bool ui_browser__dialog_yesno(struct ui_browser
*browser
, const char *text
)
236 while ((key
= ui__dialog_yesno(text
)) == K_RESIZE
)
237 ui_browser__handle_resize(browser
);
239 return key
== K_ENTER
|| toupper(key
) == 'Y';
242 void ui_browser__reset_index(struct ui_browser
*browser
)
244 browser
->index
= browser
->top_idx
= 0;
245 browser
->seek(browser
, 0, SEEK_SET
);
248 void __ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
251 ui_browser__set_color(browser
, HE_COLORSET_ROOT
);
252 ui_browser__write_nstring(browser
, title
, browser
->width
+ 1);
255 void ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
257 pthread_mutex_lock(&ui__lock
);
258 __ui_browser__show_title(browser
, title
);
259 pthread_mutex_unlock(&ui__lock
);
262 int ui_browser__show(struct ui_browser
*browser
, const char *title
,
263 const char *helpline
, ...)
268 if (browser
->refresh_dimensions
== NULL
)
269 browser
->refresh_dimensions
= ui_browser__refresh_dimensions
;
271 browser
->refresh_dimensions(browser
);
273 pthread_mutex_lock(&ui__lock
);
274 __ui_browser__show_title(browser
, title
);
276 browser
->title
= title
;
277 zfree(&browser
->helpline
);
279 va_start(ap
, helpline
);
280 err
= vasprintf(&browser
->helpline
, helpline
, ap
);
283 ui_helpline__push(browser
->helpline
);
284 pthread_mutex_unlock(&ui__lock
);
288 void ui_browser__hide(struct ui_browser
*browser
)
290 pthread_mutex_lock(&ui__lock
);
292 zfree(&browser
->helpline
);
293 pthread_mutex_unlock(&ui__lock
);
296 static void ui_browser__scrollbar_set(struct ui_browser
*browser
)
298 int height
= browser
->height
, h
= 0, pct
= 0,
299 col
= browser
->width
,
302 if (browser
->nr_entries
> 1) {
303 pct
= ((browser
->index
* (browser
->height
- 1)) /
304 (browser
->nr_entries
- 1));
307 SLsmg_set_char_set(1);
310 ui_browser__gotorc(browser
, row
++, col
);
311 SLsmg_write_char(h
== pct
? SLSMG_DIAMOND_CHAR
: SLSMG_CKBRD_CHAR
);
315 SLsmg_set_char_set(0);
318 static int __ui_browser__refresh(struct ui_browser
*browser
)
321 int width
= browser
->width
;
323 row
= browser
->refresh(browser
);
324 ui_browser__set_color(browser
, HE_COLORSET_NORMAL
);
326 if (!browser
->use_navkeypressed
|| browser
->navkeypressed
)
327 ui_browser__scrollbar_set(browser
);
331 SLsmg_fill_region(browser
->y
+ row
, browser
->x
,
332 browser
->height
- row
, width
, ' ');
337 int ui_browser__refresh(struct ui_browser
*browser
)
339 pthread_mutex_lock(&ui__lock
);
340 __ui_browser__refresh(browser
);
341 pthread_mutex_unlock(&ui__lock
);
347 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
348 * forget about any reference to any entry in the underlying data structure,
349 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
350 * after an output_resort and hist decay.
352 void ui_browser__update_nr_entries(struct ui_browser
*browser
, u32 nr_entries
)
354 off_t offset
= nr_entries
- browser
->nr_entries
;
356 browser
->nr_entries
= nr_entries
;
359 if (browser
->top_idx
< (u64
)-offset
)
360 offset
= -browser
->top_idx
;
362 browser
->index
+= offset
;
363 browser
->top_idx
+= offset
;
367 browser
->seek(browser
, browser
->top_idx
, SEEK_SET
);
370 int ui_browser__run(struct ui_browser
*browser
, int delay_secs
)
377 pthread_mutex_lock(&ui__lock
);
378 err
= __ui_browser__refresh(browser
);
380 pthread_mutex_unlock(&ui__lock
);
384 key
= ui__getch(delay_secs
);
386 if (key
== K_RESIZE
) {
387 ui__refresh_dimensions(false);
388 browser
->refresh_dimensions(browser
);
389 __ui_browser__show_title(browser
, browser
->title
);
390 ui_helpline__puts(browser
->helpline
);
394 if (browser
->use_navkeypressed
&& !browser
->navkeypressed
) {
395 if (key
== K_DOWN
|| key
== K_UP
||
396 (browser
->columns
&& (key
== K_LEFT
|| key
== K_RIGHT
)) ||
397 key
== K_PGDN
|| key
== K_PGUP
||
398 key
== K_HOME
|| key
== K_END
||
400 browser
->navkeypressed
= true;
408 if (browser
->index
== browser
->nr_entries
- 1)
411 if (browser
->index
== browser
->top_idx
+ browser
->rows
) {
413 browser
->seek(browser
, +1, SEEK_CUR
);
417 if (browser
->index
== 0)
420 if (browser
->index
< browser
->top_idx
) {
422 browser
->seek(browser
, -1, SEEK_CUR
);
426 if (!browser
->columns
)
428 if (browser
->horiz_scroll
< browser
->columns
- 1)
429 ++browser
->horiz_scroll
;
432 if (!browser
->columns
)
434 if (browser
->horiz_scroll
!= 0)
435 --browser
->horiz_scroll
;
439 if (browser
->top_idx
+ browser
->rows
> browser
->nr_entries
- 1)
442 offset
= browser
->rows
;
443 if (browser
->index
+ offset
> browser
->nr_entries
- 1)
444 offset
= browser
->nr_entries
- 1 - browser
->index
;
445 browser
->index
+= offset
;
446 browser
->top_idx
+= offset
;
447 browser
->seek(browser
, +offset
, SEEK_CUR
);
450 if (browser
->top_idx
== 0)
453 if (browser
->top_idx
< browser
->rows
)
454 offset
= browser
->top_idx
;
456 offset
= browser
->rows
;
458 browser
->index
-= offset
;
459 browser
->top_idx
-= offset
;
460 browser
->seek(browser
, -offset
, SEEK_CUR
);
463 ui_browser__reset_index(browser
);
466 offset
= browser
->rows
- 1;
467 if (offset
>= browser
->nr_entries
)
468 offset
= browser
->nr_entries
- 1;
470 browser
->index
= browser
->nr_entries
- 1;
471 browser
->top_idx
= browser
->index
- offset
;
472 browser
->seek(browser
, -offset
, SEEK_END
);
482 unsigned int ui_browser__list_head_refresh(struct ui_browser
*browser
)
484 struct list_head
*pos
;
485 struct list_head
*head
= browser
->entries
;
488 if (browser
->top
== NULL
|| browser
->top
== browser
->entries
)
489 browser
->top
= ui_browser__list_head_filter_entries(browser
, head
->next
);
493 list_for_each_from(pos
, head
) {
494 if (!browser
->filter
|| !browser
->filter(browser
, pos
)) {
495 ui_browser__gotorc(browser
, row
, 0);
496 browser
->write(browser
, pos
, row
);
497 if (++row
== browser
->rows
)
505 static struct ui_browser_colorset
{
506 const char *name
, *fg
, *bg
;
508 } ui_browser__colorsets
[] = {
510 .colorset
= HE_COLORSET_TOP
,
516 .colorset
= HE_COLORSET_MEDIUM
,
522 .colorset
= HE_COLORSET_NORMAL
,
528 .colorset
= HE_COLORSET_SELECTED
,
534 .colorset
= HE_COLORSET_CODE
,
540 .colorset
= HE_COLORSET_ADDR
,
546 .colorset
= HE_COLORSET_ROOT
,
557 static int ui_browser__color_config(const char *var
, const char *value
,
558 void *data __maybe_unused
)
560 char *fg
= NULL
, *bg
;
563 /* same dir for all commands */
564 if (prefixcmp(var
, "colors.") != 0)
567 for (i
= 0; ui_browser__colorsets
[i
].name
!= NULL
; ++i
) {
568 const char *name
= var
+ 7;
570 if (strcmp(ui_browser__colorsets
[i
].name
, name
) != 0)
577 bg
= strchr(fg
, ',');
582 while (isspace(*++bg
));
583 ui_browser__colorsets
[i
].bg
= bg
;
584 ui_browser__colorsets
[i
].fg
= fg
;
592 void ui_browser__argv_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
596 browser
->top
= browser
->entries
;
599 browser
->top
= browser
->top
+ browser
->top_idx
+ offset
;
602 browser
->top
= browser
->top
+ browser
->nr_entries
- 1 + offset
;
609 unsigned int ui_browser__argv_refresh(struct ui_browser
*browser
)
611 unsigned int row
= 0, idx
= browser
->top_idx
;
614 if (browser
->top
== NULL
)
615 browser
->top
= browser
->entries
;
617 pos
= (char **)browser
->top
;
618 while (idx
< browser
->nr_entries
) {
619 if (!browser
->filter
|| !browser
->filter(browser
, *pos
)) {
620 ui_browser__gotorc(browser
, row
, 0);
621 browser
->write(browser
, pos
, row
);
622 if (++row
== browser
->rows
)
633 void __ui_browser__vline(struct ui_browser
*browser
, unsigned int column
,
636 SLsmg_set_char_set(1);
637 ui_browser__gotorc(browser
, start
, column
);
638 SLsmg_draw_vline(end
- start
+ 1);
639 SLsmg_set_char_set(0);
642 void ui_browser__write_graph(struct ui_browser
*browser __maybe_unused
,
645 SLsmg_set_char_set(1);
646 SLsmg_write_char(graph
);
647 SLsmg_set_char_set(0);
650 static void __ui_browser__line_arrow_up(struct ui_browser
*browser
,
654 unsigned int row
, end_row
;
656 SLsmg_set_char_set(1);
658 if (start
< browser
->top_idx
+ browser
->rows
) {
659 row
= start
- browser
->top_idx
;
660 ui_browser__gotorc(browser
, row
, column
);
661 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
662 ui_browser__gotorc(browser
, row
, column
+ 1);
668 row
= browser
->rows
- 1;
670 if (end
> browser
->top_idx
)
671 end_row
= end
- browser
->top_idx
;
675 ui_browser__gotorc(browser
, end_row
, column
);
676 SLsmg_draw_vline(row
- end_row
+ 1);
678 ui_browser__gotorc(browser
, end_row
, column
);
679 if (end
>= browser
->top_idx
) {
680 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
681 ui_browser__gotorc(browser
, end_row
, column
+ 1);
682 SLsmg_write_char(SLSMG_HLINE_CHAR
);
683 ui_browser__gotorc(browser
, end_row
, column
+ 2);
684 SLsmg_write_char(SLSMG_RARROW_CHAR
);
687 SLsmg_set_char_set(0);
690 static void __ui_browser__line_arrow_down(struct ui_browser
*browser
,
694 unsigned int row
, end_row
;
696 SLsmg_set_char_set(1);
698 if (start
>= browser
->top_idx
) {
699 row
= start
- browser
->top_idx
;
700 ui_browser__gotorc(browser
, row
, column
);
701 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
702 ui_browser__gotorc(browser
, row
, column
+ 1);
710 if (end
>= browser
->top_idx
+ browser
->rows
)
711 end_row
= browser
->rows
- 1;
713 end_row
= end
- browser
->top_idx
;
715 ui_browser__gotorc(browser
, row
, column
);
716 SLsmg_draw_vline(end_row
- row
+ 1);
718 ui_browser__gotorc(browser
, end_row
, column
);
719 if (end
< browser
->top_idx
+ browser
->rows
) {
720 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
721 ui_browser__gotorc(browser
, end_row
, column
+ 1);
722 SLsmg_write_char(SLSMG_HLINE_CHAR
);
723 ui_browser__gotorc(browser
, end_row
, column
+ 2);
724 SLsmg_write_char(SLSMG_RARROW_CHAR
);
727 SLsmg_set_char_set(0);
730 void __ui_browser__line_arrow(struct ui_browser
*browser
, unsigned int column
,
734 __ui_browser__line_arrow_up(browser
, column
, start
, end
);
736 __ui_browser__line_arrow_down(browser
, column
, start
, end
);
739 void ui_browser__init(void)
743 perf_config(ui_browser__color_config
, NULL
);
745 while (ui_browser__colorsets
[i
].name
) {
746 struct ui_browser_colorset
*c
= &ui_browser__colorsets
[i
++];
747 sltt_set_color(c
->colorset
, c
->name
, c
->fg
, c
->bg
);
750 annotate_browser__init();