1 // SPDX-License-Identifier: GPL-2.0
3 #include "../string2.h"
5 #include "../../perf.h"
9 #include <linux/compiler.h>
10 #include <linux/list.h>
11 #include <linux/rbtree.h>
12 #include <linux/string.h>
14 #include <sys/ttydefaults.h>
19 #include "sane_ctype.h"
21 static int ui_browser__percent_color(struct ui_browser
*browser
,
22 double percent
, bool current
)
24 if (current
&& (!browser
->use_navkeypressed
|| browser
->navkeypressed
))
25 return HE_COLORSET_SELECTED
;
26 if (percent
>= MIN_RED
)
27 return HE_COLORSET_TOP
;
28 if (percent
>= MIN_GREEN
)
29 return HE_COLORSET_MEDIUM
;
30 return HE_COLORSET_NORMAL
;
33 int ui_browser__set_color(struct ui_browser
*browser
, int color
)
35 int ret
= browser
->current_color
;
36 browser
->current_color
= color
;
37 SLsmg_set_color(color
);
41 void ui_browser__set_percent_color(struct ui_browser
*browser
,
42 double percent
, bool current
)
44 int color
= ui_browser__percent_color(browser
, percent
, current
);
45 ui_browser__set_color(browser
, color
);
48 void ui_browser__gotorc(struct ui_browser
*browser
, int y
, int x
)
50 SLsmg_gotorc(browser
->y
+ y
, browser
->x
+ x
);
53 void ui_browser__write_nstring(struct ui_browser
*browser __maybe_unused
, const char *msg
,
56 slsmg_write_nstring(msg
, width
);
59 void ui_browser__printf(struct ui_browser
*browser __maybe_unused
, const char *fmt
, ...)
64 slsmg_vprintf(fmt
, args
);
68 static struct list_head
*
69 ui_browser__list_head_filter_entries(struct ui_browser
*browser
,
70 struct list_head
*pos
)
73 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
76 } while (pos
!= browser
->entries
);
81 static struct list_head
*
82 ui_browser__list_head_filter_prev_entries(struct ui_browser
*browser
,
83 struct list_head
*pos
)
86 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
89 } while (pos
!= browser
->entries
);
94 void ui_browser__list_head_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
96 struct list_head
*head
= browser
->entries
;
97 struct list_head
*pos
;
99 if (browser
->nr_entries
== 0)
104 pos
= ui_browser__list_head_filter_entries(browser
, head
->next
);
110 pos
= ui_browser__list_head_filter_prev_entries(browser
, head
->prev
);
119 while (offset
-- != 0)
120 pos
= ui_browser__list_head_filter_entries(browser
, pos
->next
);
122 while (offset
++ != 0)
123 pos
= ui_browser__list_head_filter_prev_entries(browser
, pos
->prev
);
129 void ui_browser__rb_tree_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
131 struct rb_root
*root
= browser
->entries
;
149 while (offset
-- != 0)
152 while (offset
++ != 0)
159 unsigned int ui_browser__rb_tree_refresh(struct ui_browser
*browser
)
164 if (browser
->top
== NULL
)
165 browser
->top
= rb_first(browser
->entries
);
170 ui_browser__gotorc(browser
, row
, 0);
171 browser
->write(browser
, nd
, row
);
172 if (++row
== browser
->rows
)
180 bool ui_browser__is_current_entry(struct ui_browser
*browser
, unsigned row
)
182 return browser
->top_idx
+ row
== browser
->index
;
185 void ui_browser__refresh_dimensions(struct ui_browser
*browser
)
187 browser
->width
= SLtt_Screen_Cols
- 1;
188 browser
->height
= browser
->rows
= SLtt_Screen_Rows
- 2;
193 void ui_browser__handle_resize(struct ui_browser
*browser
)
195 ui__refresh_dimensions(false);
196 ui_browser__show(browser
, browser
->title
, ui_helpline__current
);
197 ui_browser__refresh(browser
);
200 int ui_browser__warning(struct ui_browser
*browser
, int timeout
,
201 const char *format
, ...)
207 va_start(args
, format
);
208 err
= vasprintf(&text
, format
, args
);
212 va_start(args
, format
);
213 ui_helpline__vpush(format
, args
);
216 while ((key
= ui__question_window("Warning!", text
,
218 timeout
)) == K_RESIZE
)
219 ui_browser__handle_resize(browser
);
226 int ui_browser__help_window(struct ui_browser
*browser
, const char *text
)
230 while ((key
= ui__help_window(text
)) == K_RESIZE
)
231 ui_browser__handle_resize(browser
);
236 bool ui_browser__dialog_yesno(struct ui_browser
*browser
, const char *text
)
240 while ((key
= ui__dialog_yesno(text
)) == K_RESIZE
)
241 ui_browser__handle_resize(browser
);
243 return key
== K_ENTER
|| toupper(key
) == 'Y';
246 void ui_browser__reset_index(struct ui_browser
*browser
)
248 browser
->index
= browser
->top_idx
= 0;
249 browser
->seek(browser
, 0, SEEK_SET
);
252 void __ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
255 ui_browser__set_color(browser
, HE_COLORSET_ROOT
);
256 ui_browser__write_nstring(browser
, title
, browser
->width
+ 1);
259 void ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
261 pthread_mutex_lock(&ui__lock
);
262 __ui_browser__show_title(browser
, title
);
263 pthread_mutex_unlock(&ui__lock
);
266 int ui_browser__show(struct ui_browser
*browser
, const char *title
,
267 const char *helpline
, ...)
272 if (browser
->refresh_dimensions
== NULL
)
273 browser
->refresh_dimensions
= ui_browser__refresh_dimensions
;
275 browser
->refresh_dimensions(browser
);
277 pthread_mutex_lock(&ui__lock
);
278 __ui_browser__show_title(browser
, title
);
280 browser
->title
= title
;
281 zfree(&browser
->helpline
);
283 va_start(ap
, helpline
);
284 err
= vasprintf(&browser
->helpline
, helpline
, ap
);
287 ui_helpline__push(browser
->helpline
);
288 pthread_mutex_unlock(&ui__lock
);
292 void ui_browser__hide(struct ui_browser
*browser
)
294 pthread_mutex_lock(&ui__lock
);
296 zfree(&browser
->helpline
);
297 pthread_mutex_unlock(&ui__lock
);
300 static void ui_browser__scrollbar_set(struct ui_browser
*browser
)
302 int height
= browser
->height
, h
= 0, pct
= 0,
303 col
= browser
->width
,
306 if (browser
->nr_entries
> 1) {
307 pct
= ((browser
->index
* (browser
->height
- 1)) /
308 (browser
->nr_entries
- 1));
311 SLsmg_set_char_set(1);
314 ui_browser__gotorc(browser
, row
++, col
);
315 SLsmg_write_char(h
== pct
? SLSMG_DIAMOND_CHAR
: SLSMG_CKBRD_CHAR
);
319 SLsmg_set_char_set(0);
322 static int __ui_browser__refresh(struct ui_browser
*browser
)
325 int width
= browser
->width
;
327 row
= browser
->refresh(browser
);
328 ui_browser__set_color(browser
, HE_COLORSET_NORMAL
);
330 if (!browser
->use_navkeypressed
|| browser
->navkeypressed
)
331 ui_browser__scrollbar_set(browser
);
335 SLsmg_fill_region(browser
->y
+ row
, browser
->x
,
336 browser
->height
- row
, width
, ' ');
341 int ui_browser__refresh(struct ui_browser
*browser
)
343 pthread_mutex_lock(&ui__lock
);
344 __ui_browser__refresh(browser
);
345 pthread_mutex_unlock(&ui__lock
);
351 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
352 * forget about any reference to any entry in the underlying data structure,
353 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
354 * after an output_resort and hist decay.
356 void ui_browser__update_nr_entries(struct ui_browser
*browser
, u32 nr_entries
)
358 off_t offset
= nr_entries
- browser
->nr_entries
;
360 browser
->nr_entries
= nr_entries
;
363 if (browser
->top_idx
< (u64
)-offset
)
364 offset
= -browser
->top_idx
;
366 browser
->index
+= offset
;
367 browser
->top_idx
+= offset
;
371 browser
->seek(browser
, browser
->top_idx
, SEEK_SET
);
374 int ui_browser__run(struct ui_browser
*browser
, int delay_secs
)
381 pthread_mutex_lock(&ui__lock
);
382 err
= __ui_browser__refresh(browser
);
384 pthread_mutex_unlock(&ui__lock
);
388 key
= ui__getch(delay_secs
);
390 if (key
== K_RESIZE
) {
391 ui__refresh_dimensions(false);
392 browser
->refresh_dimensions(browser
);
393 __ui_browser__show_title(browser
, browser
->title
);
394 ui_helpline__puts(browser
->helpline
);
398 if (browser
->use_navkeypressed
&& !browser
->navkeypressed
) {
399 if (key
== K_DOWN
|| key
== K_UP
||
400 (browser
->columns
&& (key
== K_LEFT
|| key
== K_RIGHT
)) ||
401 key
== K_PGDN
|| key
== K_PGUP
||
402 key
== K_HOME
|| key
== K_END
||
404 browser
->navkeypressed
= true;
412 if (browser
->index
== browser
->nr_entries
- 1)
415 if (browser
->index
== browser
->top_idx
+ browser
->rows
) {
417 browser
->seek(browser
, +1, SEEK_CUR
);
421 if (browser
->index
== 0)
424 if (browser
->index
< browser
->top_idx
) {
426 browser
->seek(browser
, -1, SEEK_CUR
);
430 if (!browser
->columns
)
432 if (browser
->horiz_scroll
< browser
->columns
- 1)
433 ++browser
->horiz_scroll
;
436 if (!browser
->columns
)
438 if (browser
->horiz_scroll
!= 0)
439 --browser
->horiz_scroll
;
443 if (browser
->top_idx
+ browser
->rows
> browser
->nr_entries
- 1)
446 offset
= browser
->rows
;
447 if (browser
->index
+ offset
> browser
->nr_entries
- 1)
448 offset
= browser
->nr_entries
- 1 - browser
->index
;
449 browser
->index
+= offset
;
450 browser
->top_idx
+= offset
;
451 browser
->seek(browser
, +offset
, SEEK_CUR
);
454 if (browser
->top_idx
== 0)
457 if (browser
->top_idx
< browser
->rows
)
458 offset
= browser
->top_idx
;
460 offset
= browser
->rows
;
462 browser
->index
-= offset
;
463 browser
->top_idx
-= offset
;
464 browser
->seek(browser
, -offset
, SEEK_CUR
);
467 ui_browser__reset_index(browser
);
470 offset
= browser
->rows
- 1;
471 if (offset
>= browser
->nr_entries
)
472 offset
= browser
->nr_entries
- 1;
474 browser
->index
= browser
->nr_entries
- 1;
475 browser
->top_idx
= browser
->index
- offset
;
476 browser
->seek(browser
, -offset
, SEEK_END
);
486 unsigned int ui_browser__list_head_refresh(struct ui_browser
*browser
)
488 struct list_head
*pos
;
489 struct list_head
*head
= browser
->entries
;
492 if (browser
->top
== NULL
|| browser
->top
== browser
->entries
)
493 browser
->top
= ui_browser__list_head_filter_entries(browser
, head
->next
);
497 list_for_each_from(pos
, head
) {
498 if (!browser
->filter
|| !browser
->filter(browser
, pos
)) {
499 ui_browser__gotorc(browser
, row
, 0);
500 browser
->write(browser
, pos
, row
);
501 if (++row
== browser
->rows
)
509 static struct ui_browser_colorset
{
510 const char *name
, *fg
, *bg
;
512 } ui_browser__colorsets
[] = {
514 .colorset
= HE_COLORSET_TOP
,
520 .colorset
= HE_COLORSET_MEDIUM
,
526 .colorset
= HE_COLORSET_NORMAL
,
532 .colorset
= HE_COLORSET_SELECTED
,
538 .colorset
= HE_COLORSET_JUMP_ARROWS
,
539 .name
= "jump_arrows",
544 .colorset
= HE_COLORSET_ADDR
,
550 .colorset
= HE_COLORSET_ROOT
,
561 static int ui_browser__color_config(const char *var
, const char *value
,
562 void *data __maybe_unused
)
564 char *fg
= NULL
, *bg
;
567 /* same dir for all commands */
568 if (!strstarts(var
, "colors.") != 0)
571 for (i
= 0; ui_browser__colorsets
[i
].name
!= NULL
; ++i
) {
572 const char *name
= var
+ 7;
574 if (strcmp(ui_browser__colorsets
[i
].name
, name
) != 0)
581 bg
= strchr(fg
, ',');
587 ui_browser__colorsets
[i
].bg
= bg
;
588 ui_browser__colorsets
[i
].fg
= fg
;
596 void ui_browser__argv_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
600 browser
->top
= browser
->entries
;
603 browser
->top
= browser
->top
+ browser
->top_idx
+ offset
;
606 browser
->top
= browser
->top
+ browser
->nr_entries
- 1 + offset
;
613 unsigned int ui_browser__argv_refresh(struct ui_browser
*browser
)
615 unsigned int row
= 0, idx
= browser
->top_idx
;
618 if (browser
->top
== NULL
)
619 browser
->top
= browser
->entries
;
621 pos
= (char **)browser
->top
;
622 while (idx
< browser
->nr_entries
) {
623 if (!browser
->filter
|| !browser
->filter(browser
, *pos
)) {
624 ui_browser__gotorc(browser
, row
, 0);
625 browser
->write(browser
, pos
, row
);
626 if (++row
== browser
->rows
)
637 void __ui_browser__vline(struct ui_browser
*browser
, unsigned int column
,
640 SLsmg_set_char_set(1);
641 ui_browser__gotorc(browser
, start
, column
);
642 SLsmg_draw_vline(end
- start
+ 1);
643 SLsmg_set_char_set(0);
646 void ui_browser__write_graph(struct ui_browser
*browser __maybe_unused
,
649 SLsmg_set_char_set(1);
650 SLsmg_write_char(graph
);
651 SLsmg_set_char_set(0);
654 static void __ui_browser__line_arrow_up(struct ui_browser
*browser
,
658 unsigned int row
, end_row
;
660 SLsmg_set_char_set(1);
662 if (start
< browser
->top_idx
+ browser
->rows
) {
663 row
= start
- browser
->top_idx
;
664 ui_browser__gotorc(browser
, row
, column
);
665 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
666 ui_browser__gotorc(browser
, row
, column
+ 1);
672 row
= browser
->rows
- 1;
674 if (end
> browser
->top_idx
)
675 end_row
= end
- browser
->top_idx
;
679 ui_browser__gotorc(browser
, end_row
, column
);
680 SLsmg_draw_vline(row
- end_row
+ 1);
682 ui_browser__gotorc(browser
, end_row
, column
);
683 if (end
>= browser
->top_idx
) {
684 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
685 ui_browser__gotorc(browser
, end_row
, column
+ 1);
686 SLsmg_write_char(SLSMG_HLINE_CHAR
);
687 ui_browser__gotorc(browser
, end_row
, column
+ 2);
688 SLsmg_write_char(SLSMG_RARROW_CHAR
);
691 SLsmg_set_char_set(0);
694 static void __ui_browser__line_arrow_down(struct ui_browser
*browser
,
698 unsigned int row
, end_row
;
700 SLsmg_set_char_set(1);
702 if (start
>= browser
->top_idx
) {
703 row
= start
- browser
->top_idx
;
704 ui_browser__gotorc(browser
, row
, column
);
705 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
706 ui_browser__gotorc(browser
, row
, column
+ 1);
714 if (end
>= browser
->top_idx
+ browser
->rows
)
715 end_row
= browser
->rows
- 1;
717 end_row
= end
- browser
->top_idx
;
719 ui_browser__gotorc(browser
, row
, column
);
720 SLsmg_draw_vline(end_row
- row
+ 1);
722 ui_browser__gotorc(browser
, end_row
, column
);
723 if (end
< browser
->top_idx
+ browser
->rows
) {
724 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
725 ui_browser__gotorc(browser
, end_row
, column
+ 1);
726 SLsmg_write_char(SLSMG_HLINE_CHAR
);
727 ui_browser__gotorc(browser
, end_row
, column
+ 2);
728 SLsmg_write_char(SLSMG_RARROW_CHAR
);
731 SLsmg_set_char_set(0);
734 void __ui_browser__line_arrow(struct ui_browser
*browser
, unsigned int column
,
738 __ui_browser__line_arrow_up(browser
, column
, start
, end
);
740 __ui_browser__line_arrow_down(browser
, column
, start
, end
);
743 void ui_browser__mark_fused(struct ui_browser
*browser
, unsigned int column
,
744 unsigned int row
, bool arrow_down
)
746 unsigned int end_row
;
748 if (row
>= browser
->top_idx
)
749 end_row
= row
- browser
->top_idx
;
753 SLsmg_set_char_set(1);
756 ui_browser__gotorc(browser
, end_row
, column
- 1);
757 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
758 ui_browser__gotorc(browser
, end_row
, column
);
760 ui_browser__gotorc(browser
, end_row
+ 1, column
- 1);
761 SLsmg_write_char(SLSMG_LTEE_CHAR
);
763 ui_browser__gotorc(browser
, end_row
, column
- 1);
764 SLsmg_write_char(SLSMG_LTEE_CHAR
);
765 ui_browser__gotorc(browser
, end_row
, column
);
769 SLsmg_set_char_set(0);
772 void ui_browser__init(void)
776 perf_config(ui_browser__color_config
, NULL
);
778 while (ui_browser__colorsets
[i
].name
) {
779 struct ui_browser_colorset
*c
= &ui_browser__colorsets
[i
++];
780 sltt_set_color(c
->colorset
, c
->name
, c
->fg
, c
->bg
);
783 annotate_browser__init();