1 // SPDX-License-Identifier: GPL-2.0
2 #include "../util/string2.h"
3 #include "../util/config.h"
7 #include <linux/compiler.h>
8 #include <linux/list.h>
9 #include <linux/rbtree.h>
10 #include <linux/string.h>
12 #include <sys/ttydefaults.h>
16 #include "../util/color.h"
17 #include <linux/ctype.h>
18 #include <linux/zalloc.h>
20 static int ui_browser__percent_color(struct ui_browser *browser,
21 double percent, bool current)
23 if (current && (!browser->use_navkeypressed || browser->navkeypressed))
24 return HE_COLORSET_SELECTED;
25 if (percent >= MIN_RED)
26 return HE_COLORSET_TOP;
27 if (percent >= MIN_GREEN)
28 return HE_COLORSET_MEDIUM;
29 return HE_COLORSET_NORMAL;
32 int ui_browser__set_color(struct ui_browser *browser, int color)
34 int ret = browser->current_color;
35 browser->current_color = color;
36 SLsmg_set_color(color);
40 void ui_browser__set_percent_color(struct ui_browser *browser,
41 double percent, bool current)
43 int color = ui_browser__percent_color(browser, percent, current);
44 ui_browser__set_color(browser, color);
47 void ui_browser__gotorc_title(struct ui_browser *browser, int y, int x)
49 SLsmg_gotorc(browser->y + y, browser->x + x);
52 void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
54 SLsmg_gotorc(browser->y + y + browser->extra_title_lines, browser->x + x);
57 void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg,
60 slsmg_write_nstring(msg, width);
63 void ui_browser__vprintf(struct ui_browser *browser __maybe_unused, const char *fmt, va_list args)
65 slsmg_vprintf(fmt, args);
68 void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...)
73 ui_browser__vprintf(browser, fmt, args);
77 static struct list_head *
78 ui_browser__list_head_filter_entries(struct ui_browser *browser,
79 struct list_head *pos)
82 if (!browser->filter || !browser->filter(browser, pos))
85 } while (pos != browser->entries);
90 static struct list_head *
91 ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
92 struct list_head *pos)
95 if (!browser->filter || !browser->filter(browser, pos))
98 } while (pos != browser->entries);
103 void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
105 struct list_head *head = browser->entries;
106 struct list_head *pos;
108 if (browser->nr_entries == 0)
113 pos = ui_browser__list_head_filter_entries(browser, head->next);
119 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
128 while (offset-- != 0)
129 pos = ui_browser__list_head_filter_entries(browser, pos->next);
131 while (offset++ != 0)
132 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
138 void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
140 struct rb_root *root = browser->entries;
158 while (offset-- != 0)
161 while (offset++ != 0)
168 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
173 if (browser->top == NULL)
174 browser->top = rb_first(browser->entries);
179 ui_browser__gotorc(browser, row, 0);
180 browser->write(browser, nd, row);
181 if (++row == browser->rows)
189 bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
191 return browser->top_idx + row == browser->index;
194 void ui_browser__refresh_dimensions(struct ui_browser *browser)
196 browser->width = SLtt_Screen_Cols - 1;
197 browser->height = browser->rows = SLtt_Screen_Rows - 2;
198 browser->rows -= browser->extra_title_lines;
203 void ui_browser__handle_resize(struct ui_browser *browser)
205 ui__refresh_dimensions(false);
206 ui_browser__show(browser, browser->title, ui_helpline__current);
207 ui_browser__refresh(browser);
210 int ui_browser__warning(struct ui_browser *browser, int timeout,
211 const char *format, ...)
217 va_start(args, format);
218 err = vasprintf(&text, format, args);
222 va_start(args, format);
223 ui_helpline__vpush(format, args);
226 while ((key = ui__question_window("Warning!", text,
228 timeout)) == K_RESIZE)
229 ui_browser__handle_resize(browser);
236 int ui_browser__help_window(struct ui_browser *browser, const char *text)
240 while ((key = ui__help_window(text)) == K_RESIZE)
241 ui_browser__handle_resize(browser);
246 bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
250 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
251 ui_browser__handle_resize(browser);
253 return key == K_ENTER || toupper(key) == 'Y';
256 void ui_browser__reset_index(struct ui_browser *browser)
258 browser->index = browser->top_idx = 0;
259 browser->seek(browser, 0, SEEK_SET);
262 void __ui_browser__show_title(struct ui_browser *browser, const char *title)
265 ui_browser__set_color(browser, HE_COLORSET_ROOT);
266 ui_browser__write_nstring(browser, title, browser->width + 1);
269 void ui_browser__show_title(struct ui_browser *browser, const char *title)
271 pthread_mutex_lock(&ui__lock);
272 __ui_browser__show_title(browser, title);
273 pthread_mutex_unlock(&ui__lock);
276 int ui_browser__show(struct ui_browser *browser, const char *title,
277 const char *helpline, ...)
282 if (browser->refresh_dimensions == NULL)
283 browser->refresh_dimensions = ui_browser__refresh_dimensions;
285 browser->refresh_dimensions(browser);
287 pthread_mutex_lock(&ui__lock);
288 __ui_browser__show_title(browser, title);
290 browser->title = title;
291 zfree(&browser->helpline);
293 va_start(ap, helpline);
294 err = vasprintf(&browser->helpline, helpline, ap);
297 ui_helpline__push(browser->helpline);
298 pthread_mutex_unlock(&ui__lock);
302 void ui_browser__hide(struct ui_browser *browser)
304 pthread_mutex_lock(&ui__lock);
306 zfree(&browser->helpline);
307 pthread_mutex_unlock(&ui__lock);
310 static void ui_browser__scrollbar_set(struct ui_browser *browser)
312 int height = browser->height, h = 0, pct = 0,
313 col = browser->width,
316 if (browser->nr_entries > 1) {
317 pct = ((browser->index * (browser->height - 1)) /
318 (browser->nr_entries - 1));
321 SLsmg_set_char_set(1);
324 ui_browser__gotorc(browser, row++, col);
325 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
329 SLsmg_set_char_set(0);
332 static int __ui_browser__refresh(struct ui_browser *browser)
335 int width = browser->width;
337 row = browser->refresh(browser);
338 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
340 if (!browser->use_navkeypressed || browser->navkeypressed)
341 ui_browser__scrollbar_set(browser);
345 SLsmg_fill_region(browser->y + row + browser->extra_title_lines, browser->x,
346 browser->rows - row, width, ' ');
348 if (browser->nr_entries == 0 && browser->no_samples_msg)
349 __ui__info_window(NULL, browser->no_samples_msg, NULL);
353 int ui_browser__refresh(struct ui_browser *browser)
355 pthread_mutex_lock(&ui__lock);
356 __ui_browser__refresh(browser);
357 pthread_mutex_unlock(&ui__lock);
363 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
364 * forget about any reference to any entry in the underlying data structure,
365 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
366 * after an output_resort and hist decay.
368 void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
370 off_t offset = nr_entries - browser->nr_entries;
372 browser->nr_entries = nr_entries;
375 if (browser->top_idx < (u64)-offset)
376 offset = -browser->top_idx;
378 browser->index += offset;
379 browser->top_idx += offset;
383 browser->seek(browser, browser->top_idx, SEEK_SET);
386 int ui_browser__run(struct ui_browser *browser, int delay_secs)
393 pthread_mutex_lock(&ui__lock);
394 err = __ui_browser__refresh(browser);
396 pthread_mutex_unlock(&ui__lock);
400 key = ui__getch(delay_secs);
402 if (key == K_RESIZE) {
403 ui__refresh_dimensions(false);
404 browser->refresh_dimensions(browser);
405 __ui_browser__show_title(browser, browser->title);
406 ui_helpline__puts(browser->helpline);
410 if (browser->use_navkeypressed && !browser->navkeypressed) {
411 if (key == K_DOWN || key == K_UP ||
412 (browser->columns && (key == K_LEFT || key == K_RIGHT)) ||
413 key == K_PGDN || key == K_PGUP ||
414 key == K_HOME || key == K_END ||
416 browser->navkeypressed = true;
424 if (browser->index == browser->nr_entries - 1)
427 if (browser->index == browser->top_idx + browser->rows) {
429 browser->seek(browser, +1, SEEK_CUR);
433 if (browser->index == 0)
436 if (browser->index < browser->top_idx) {
438 browser->seek(browser, -1, SEEK_CUR);
442 if (!browser->columns)
444 if (browser->horiz_scroll < browser->columns - 1)
445 ++browser->horiz_scroll;
448 if (!browser->columns)
450 if (browser->horiz_scroll != 0)
451 --browser->horiz_scroll;
455 if (browser->top_idx + browser->rows > browser->nr_entries - 1)
458 offset = browser->rows;
459 if (browser->index + offset > browser->nr_entries - 1)
460 offset = browser->nr_entries - 1 - browser->index;
461 browser->index += offset;
462 browser->top_idx += offset;
463 browser->seek(browser, +offset, SEEK_CUR);
466 if (browser->top_idx == 0)
469 if (browser->top_idx < browser->rows)
470 offset = browser->top_idx;
472 offset = browser->rows;
474 browser->index -= offset;
475 browser->top_idx -= offset;
476 browser->seek(browser, -offset, SEEK_CUR);
479 ui_browser__reset_index(browser);
482 offset = browser->rows - 1;
483 if (offset >= browser->nr_entries)
484 offset = browser->nr_entries - 1;
486 browser->index = browser->nr_entries - 1;
487 browser->top_idx = browser->index - offset;
488 browser->seek(browser, -offset, SEEK_END);
498 unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
500 struct list_head *pos;
501 struct list_head *head = browser->entries;
504 if (browser->top == NULL || browser->top == browser->entries)
505 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
509 list_for_each_from(pos, head) {
510 if (!browser->filter || !browser->filter(browser, pos)) {
511 ui_browser__gotorc(browser, row, 0);
512 browser->write(browser, pos, row);
513 if (++row == browser->rows)
521 static struct ui_browser_colorset {
522 const char *name, *fg, *bg;
524 } ui_browser__colorsets[] = {
526 .colorset = HE_COLORSET_TOP,
532 .colorset = HE_COLORSET_MEDIUM,
538 .colorset = HE_COLORSET_NORMAL,
544 .colorset = HE_COLORSET_SELECTED,
550 .colorset = HE_COLORSET_JUMP_ARROWS,
551 .name = "jump_arrows",
556 .colorset = HE_COLORSET_ADDR,
562 .colorset = HE_COLORSET_ROOT,
573 static int ui_browser__color_config(const char *var, const char *value,
574 void *data __maybe_unused)
576 char *fg = NULL, *bg;
579 /* same dir for all commands */
580 if (!strstarts(var, "colors.") != 0)
583 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
584 const char *name = var + 7;
586 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
593 bg = strchr(fg, ',');
598 bg = skip_spaces(bg + 1);
599 ui_browser__colorsets[i].bg = bg;
600 ui_browser__colorsets[i].fg = fg;
608 void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
612 browser->top = browser->entries;
615 browser->top = (char **)browser->top + offset;
618 browser->top = (char **)browser->entries + browser->nr_entries - 1 + offset;
623 assert((char **)browser->top < (char **)browser->entries + browser->nr_entries);
624 assert((char **)browser->top >= (char **)browser->entries);
627 unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
629 unsigned int row = 0, idx = browser->top_idx;
632 if (browser->top == NULL)
633 browser->top = browser->entries;
635 pos = (char **)browser->top;
636 while (idx < browser->nr_entries &&
637 row < (unsigned)SLtt_Screen_Rows - 1) {
638 assert(pos < (char **)browser->entries + browser->nr_entries);
639 if (!browser->filter || !browser->filter(browser, *pos)) {
640 ui_browser__gotorc(browser, row, 0);
641 browser->write(browser, pos, row);
642 if (++row == browser->rows)
653 void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
656 SLsmg_set_char_set(1);
657 ui_browser__gotorc(browser, start, column);
658 SLsmg_draw_vline(end - start + 1);
659 SLsmg_set_char_set(0);
662 void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
665 SLsmg_set_char_set(1);
666 SLsmg_write_char(graph);
667 SLsmg_set_char_set(0);
670 static void __ui_browser__line_arrow_up(struct ui_browser *browser,
674 unsigned int row, end_row;
676 SLsmg_set_char_set(1);
678 if (start < browser->top_idx + browser->rows) {
679 row = start - browser->top_idx;
680 ui_browser__gotorc(browser, row, column);
681 SLsmg_write_char(SLSMG_LLCORN_CHAR);
682 ui_browser__gotorc(browser, row, column + 1);
688 row = browser->rows - 1;
690 if (end > browser->top_idx)
691 end_row = end - browser->top_idx;
695 ui_browser__gotorc(browser, end_row, column);
696 SLsmg_draw_vline(row - end_row + 1);
698 ui_browser__gotorc(browser, end_row, column);
699 if (end >= browser->top_idx) {
700 SLsmg_write_char(SLSMG_ULCORN_CHAR);
701 ui_browser__gotorc(browser, end_row, column + 1);
702 SLsmg_write_char(SLSMG_HLINE_CHAR);
703 ui_browser__gotorc(browser, end_row, column + 2);
704 SLsmg_write_char(SLSMG_RARROW_CHAR);
707 SLsmg_set_char_set(0);
710 static void __ui_browser__line_arrow_down(struct ui_browser *browser,
714 unsigned int row, end_row;
716 SLsmg_set_char_set(1);
718 if (start >= browser->top_idx) {
719 row = start - browser->top_idx;
720 ui_browser__gotorc(browser, row, column);
721 SLsmg_write_char(SLSMG_ULCORN_CHAR);
722 ui_browser__gotorc(browser, row, column + 1);
730 if (end >= browser->top_idx + browser->rows)
731 end_row = browser->rows - 1;
733 end_row = end - browser->top_idx;
735 ui_browser__gotorc(browser, row, column);
736 SLsmg_draw_vline(end_row - row + 1);
738 ui_browser__gotorc(browser, end_row, column);
739 if (end < browser->top_idx + browser->rows) {
740 SLsmg_write_char(SLSMG_LLCORN_CHAR);
741 ui_browser__gotorc(browser, end_row, column + 1);
742 SLsmg_write_char(SLSMG_HLINE_CHAR);
743 ui_browser__gotorc(browser, end_row, column + 2);
744 SLsmg_write_char(SLSMG_RARROW_CHAR);
747 SLsmg_set_char_set(0);
750 void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
754 __ui_browser__line_arrow_up(browser, column, start, end);
756 __ui_browser__line_arrow_down(browser, column, start, end);
759 void ui_browser__mark_fused(struct ui_browser *browser, unsigned int column,
760 unsigned int row, bool arrow_down)
762 unsigned int end_row;
764 if (row >= browser->top_idx)
765 end_row = row - browser->top_idx;
769 SLsmg_set_char_set(1);
772 ui_browser__gotorc(browser, end_row, column - 1);
773 SLsmg_write_char(SLSMG_ULCORN_CHAR);
774 ui_browser__gotorc(browser, end_row, column);
776 ui_browser__gotorc(browser, end_row + 1, column - 1);
777 SLsmg_write_char(SLSMG_LTEE_CHAR);
779 ui_browser__gotorc(browser, end_row, column - 1);
780 SLsmg_write_char(SLSMG_LTEE_CHAR);
781 ui_browser__gotorc(browser, end_row, column);
785 SLsmg_set_char_set(0);
788 void ui_browser__init(void)
792 perf_config(ui_browser__color_config, NULL);
794 while (ui_browser__colorsets[i].name) {
795 struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
796 sltt_set_color(c->colorset, c->name, c->fg, c->bg);