3 #include "../../perf.h"
8 #include <linux/compiler.h>
9 #include <linux/list.h>
10 #include <linux/rbtree.h>
12 #include <sys/ttydefaults.h>
18 static int ui_browser__percent_color(struct ui_browser
*browser
,
19 double percent
, bool current
)
21 if (current
&& (!browser
->use_navkeypressed
|| browser
->navkeypressed
))
22 return HE_COLORSET_SELECTED
;
23 if (percent
>= MIN_RED
)
24 return HE_COLORSET_TOP
;
25 if (percent
>= MIN_GREEN
)
26 return HE_COLORSET_MEDIUM
;
27 return HE_COLORSET_NORMAL
;
30 int ui_browser__set_color(struct ui_browser
*browser
, int color
)
32 int ret
= browser
->current_color
;
33 browser
->current_color
= color
;
34 SLsmg_set_color(color
);
38 void ui_browser__set_percent_color(struct ui_browser
*browser
,
39 double percent
, bool current
)
41 int color
= ui_browser__percent_color(browser
, percent
, current
);
42 ui_browser__set_color(browser
, color
);
45 void ui_browser__gotorc(struct ui_browser
*browser
, int y
, int x
)
47 SLsmg_gotorc(browser
->y
+ y
, browser
->x
+ x
);
50 static struct list_head
*
51 ui_browser__list_head_filter_entries(struct ui_browser
*browser
,
52 struct list_head
*pos
)
55 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
58 } while (pos
!= browser
->entries
);
63 static struct list_head
*
64 ui_browser__list_head_filter_prev_entries(struct ui_browser
*browser
,
65 struct list_head
*pos
)
68 if (!browser
->filter
|| !browser
->filter(browser
, pos
))
71 } while (pos
!= browser
->entries
);
76 void ui_browser__list_head_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
78 struct list_head
*head
= browser
->entries
;
79 struct list_head
*pos
;
81 if (browser
->nr_entries
== 0)
86 pos
= ui_browser__list_head_filter_entries(browser
, head
->next
);
92 pos
= ui_browser__list_head_filter_prev_entries(browser
, head
->prev
);
101 while (offset
-- != 0)
102 pos
= ui_browser__list_head_filter_entries(browser
, pos
->next
);
104 while (offset
++ != 0)
105 pos
= ui_browser__list_head_filter_prev_entries(browser
, pos
->prev
);
111 void ui_browser__rb_tree_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
113 struct rb_root
*root
= browser
->entries
;
131 while (offset
-- != 0)
134 while (offset
++ != 0)
141 unsigned int ui_browser__rb_tree_refresh(struct ui_browser
*browser
)
146 if (browser
->top
== NULL
)
147 browser
->top
= rb_first(browser
->entries
);
152 ui_browser__gotorc(browser
, row
, 0);
153 browser
->write(browser
, nd
, row
);
154 if (++row
== browser
->height
)
162 bool ui_browser__is_current_entry(struct ui_browser
*browser
, unsigned row
)
164 return browser
->top_idx
+ row
== browser
->index
;
167 void ui_browser__refresh_dimensions(struct ui_browser
*browser
)
169 browser
->width
= SLtt_Screen_Cols
- 1;
170 browser
->height
= SLtt_Screen_Rows
- 2;
175 void ui_browser__handle_resize(struct ui_browser
*browser
)
177 ui__refresh_dimensions(false);
178 ui_browser__show(browser
, browser
->title
, ui_helpline__current
);
179 ui_browser__refresh(browser
);
182 int ui_browser__warning(struct ui_browser
*browser
, int timeout
,
183 const char *format
, ...)
189 va_start(args
, format
);
190 err
= vasprintf(&text
, format
, args
);
194 va_start(args
, format
);
195 ui_helpline__vpush(format
, args
);
198 while ((key
== ui__question_window("Warning!", text
,
200 timeout
)) == K_RESIZE
)
201 ui_browser__handle_resize(browser
);
208 int ui_browser__help_window(struct ui_browser
*browser
, const char *text
)
212 while ((key
= ui__help_window(text
)) == K_RESIZE
)
213 ui_browser__handle_resize(browser
);
218 bool ui_browser__dialog_yesno(struct ui_browser
*browser
, const char *text
)
222 while ((key
= ui__dialog_yesno(text
)) == K_RESIZE
)
223 ui_browser__handle_resize(browser
);
225 return key
== K_ENTER
|| toupper(key
) == 'Y';
228 void ui_browser__reset_index(struct ui_browser
*browser
)
230 browser
->index
= browser
->top_idx
= 0;
231 browser
->seek(browser
, 0, SEEK_SET
);
234 void __ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
237 ui_browser__set_color(browser
, NEWT_COLORSET_ROOT
);
238 slsmg_write_nstring(title
, browser
->width
+ 1);
241 void ui_browser__show_title(struct ui_browser
*browser
, const char *title
)
243 pthread_mutex_lock(&ui__lock
);
244 __ui_browser__show_title(browser
, title
);
245 pthread_mutex_unlock(&ui__lock
);
248 int ui_browser__show(struct ui_browser
*browser
, const char *title
,
249 const char *helpline
, ...)
254 ui_browser__refresh_dimensions(browser
);
256 pthread_mutex_lock(&ui__lock
);
257 __ui_browser__show_title(browser
, title
);
259 browser
->title
= title
;
260 free(browser
->helpline
);
261 browser
->helpline
= NULL
;
263 va_start(ap
, helpline
);
264 err
= vasprintf(&browser
->helpline
, helpline
, ap
);
267 ui_helpline__push(browser
->helpline
);
268 pthread_mutex_unlock(&ui__lock
);
272 void ui_browser__hide(struct ui_browser
*browser __used
)
274 pthread_mutex_lock(&ui__lock
);
276 pthread_mutex_unlock(&ui__lock
);
279 static void ui_browser__scrollbar_set(struct ui_browser
*browser
)
281 int height
= browser
->height
, h
= 0, pct
= 0,
282 col
= browser
->width
,
283 row
= browser
->y
- 1;
285 if (browser
->nr_entries
> 1) {
286 pct
= ((browser
->index
* (browser
->height
- 1)) /
287 (browser
->nr_entries
- 1));
290 SLsmg_set_char_set(1);
293 ui_browser__gotorc(browser
, row
++, col
);
294 SLsmg_write_char(h
== pct
? SLSMG_DIAMOND_CHAR
: SLSMG_CKBRD_CHAR
);
298 SLsmg_set_char_set(0);
301 static int __ui_browser__refresh(struct ui_browser
*browser
)
304 int width
= browser
->width
;
306 row
= browser
->refresh(browser
);
307 ui_browser__set_color(browser
, HE_COLORSET_NORMAL
);
309 if (!browser
->use_navkeypressed
|| browser
->navkeypressed
)
310 ui_browser__scrollbar_set(browser
);
314 SLsmg_fill_region(browser
->y
+ row
, browser
->x
,
315 browser
->height
- row
, width
, ' ');
320 int ui_browser__refresh(struct ui_browser
*browser
)
322 pthread_mutex_lock(&ui__lock
);
323 __ui_browser__refresh(browser
);
324 pthread_mutex_unlock(&ui__lock
);
330 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
331 * forget about any reference to any entry in the underlying data structure,
332 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
333 * after an output_resort and hist decay.
335 void ui_browser__update_nr_entries(struct ui_browser
*browser
, u32 nr_entries
)
337 off_t offset
= nr_entries
- browser
->nr_entries
;
339 browser
->nr_entries
= nr_entries
;
342 if (browser
->top_idx
< (u64
)-offset
)
343 offset
= -browser
->top_idx
;
345 browser
->index
+= offset
;
346 browser
->top_idx
+= offset
;
350 browser
->seek(browser
, browser
->top_idx
, SEEK_SET
);
353 int ui_browser__run(struct ui_browser
*browser
, int delay_secs
)
360 pthread_mutex_lock(&ui__lock
);
361 err
= __ui_browser__refresh(browser
);
363 pthread_mutex_unlock(&ui__lock
);
367 key
= ui__getch(delay_secs
);
369 if (key
== K_RESIZE
) {
370 ui__refresh_dimensions(false);
371 ui_browser__refresh_dimensions(browser
);
372 __ui_browser__show_title(browser
, browser
->title
);
373 ui_helpline__puts(browser
->helpline
);
377 if (browser
->use_navkeypressed
&& !browser
->navkeypressed
) {
378 if (key
== K_DOWN
|| key
== K_UP
||
379 key
== K_PGDN
|| key
== K_PGUP
||
380 key
== K_HOME
|| key
== K_END
||
382 browser
->navkeypressed
= true;
390 if (browser
->index
== browser
->nr_entries
- 1)
393 if (browser
->index
== browser
->top_idx
+ browser
->height
) {
395 browser
->seek(browser
, +1, SEEK_CUR
);
399 if (browser
->index
== 0)
402 if (browser
->index
< browser
->top_idx
) {
404 browser
->seek(browser
, -1, SEEK_CUR
);
409 if (browser
->top_idx
+ browser
->height
> browser
->nr_entries
- 1)
412 offset
= browser
->height
;
413 if (browser
->index
+ offset
> browser
->nr_entries
- 1)
414 offset
= browser
->nr_entries
- 1 - browser
->index
;
415 browser
->index
+= offset
;
416 browser
->top_idx
+= offset
;
417 browser
->seek(browser
, +offset
, SEEK_CUR
);
420 if (browser
->top_idx
== 0)
423 if (browser
->top_idx
< browser
->height
)
424 offset
= browser
->top_idx
;
426 offset
= browser
->height
;
428 browser
->index
-= offset
;
429 browser
->top_idx
-= offset
;
430 browser
->seek(browser
, -offset
, SEEK_CUR
);
433 ui_browser__reset_index(browser
);
436 offset
= browser
->height
- 1;
437 if (offset
>= browser
->nr_entries
)
438 offset
= browser
->nr_entries
- 1;
440 browser
->index
= browser
->nr_entries
- 1;
441 browser
->top_idx
= browser
->index
- offset
;
442 browser
->seek(browser
, -offset
, SEEK_END
);
451 unsigned int ui_browser__list_head_refresh(struct ui_browser
*browser
)
453 struct list_head
*pos
;
454 struct list_head
*head
= browser
->entries
;
457 if (browser
->top
== NULL
|| browser
->top
== browser
->entries
)
458 browser
->top
= ui_browser__list_head_filter_entries(browser
, head
->next
);
462 list_for_each_from(pos
, head
) {
463 if (!browser
->filter
|| !browser
->filter(browser
, pos
)) {
464 ui_browser__gotorc(browser
, row
, 0);
465 browser
->write(browser
, pos
, row
);
466 if (++row
== browser
->height
)
474 static struct ui_browser__colorset
{
475 const char *name
, *fg
, *bg
;
477 } ui_browser__colorsets
[] = {
479 .colorset
= HE_COLORSET_TOP
,
485 .colorset
= HE_COLORSET_MEDIUM
,
491 .colorset
= HE_COLORSET_NORMAL
,
497 .colorset
= HE_COLORSET_SELECTED
,
503 .colorset
= HE_COLORSET_CODE
,
509 .colorset
= HE_COLORSET_ADDR
,
520 static int ui_browser__color_config(const char *var
, const char *value
,
523 char *fg
= NULL
, *bg
;
526 /* same dir for all commands */
527 if (prefixcmp(var
, "colors.") != 0)
530 for (i
= 0; ui_browser__colorsets
[i
].name
!= NULL
; ++i
) {
531 const char *name
= var
+ 7;
533 if (strcmp(ui_browser__colorsets
[i
].name
, name
) != 0)
540 bg
= strchr(fg
, ',');
545 while (isspace(*++bg
));
546 ui_browser__colorsets
[i
].bg
= bg
;
547 ui_browser__colorsets
[i
].fg
= fg
;
555 void ui_browser__argv_seek(struct ui_browser
*browser
, off_t offset
, int whence
)
559 browser
->top
= browser
->entries
;
562 browser
->top
= browser
->top
+ browser
->top_idx
+ offset
;
565 browser
->top
= browser
->top
+ browser
->nr_entries
+ offset
;
572 unsigned int ui_browser__argv_refresh(struct ui_browser
*browser
)
574 unsigned int row
= 0, idx
= browser
->top_idx
;
577 if (browser
->top
== NULL
)
578 browser
->top
= browser
->entries
;
580 pos
= (char **)browser
->top
;
581 while (idx
< browser
->nr_entries
) {
582 if (!browser
->filter
|| !browser
->filter(browser
, *pos
)) {
583 ui_browser__gotorc(browser
, row
, 0);
584 browser
->write(browser
, pos
, row
);
585 if (++row
== browser
->height
)
596 void __ui_browser__vline(struct ui_browser
*browser
, unsigned int column
,
599 SLsmg_set_char_set(1);
600 ui_browser__gotorc(browser
, start
, column
);
601 SLsmg_draw_vline(end
- start
+ 1);
602 SLsmg_set_char_set(0);
605 void ui_browser__write_graph(struct ui_browser
*browser __used
, int graph
)
607 SLsmg_set_char_set(1);
608 SLsmg_write_char(graph
);
609 SLsmg_set_char_set(0);
612 static void __ui_browser__line_arrow_up(struct ui_browser
*browser
,
616 unsigned int row
, end_row
;
618 SLsmg_set_char_set(1);
620 if (start
< browser
->top_idx
+ browser
->height
) {
621 row
= start
- browser
->top_idx
;
622 ui_browser__gotorc(browser
, row
, column
);
623 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
624 ui_browser__gotorc(browser
, row
, column
+ 1);
630 row
= browser
->height
- 1;
632 if (end
> browser
->top_idx
)
633 end_row
= end
- browser
->top_idx
;
637 ui_browser__gotorc(browser
, end_row
, column
);
638 SLsmg_draw_vline(row
- end_row
+ 1);
640 ui_browser__gotorc(browser
, end_row
, column
);
641 if (end
>= browser
->top_idx
) {
642 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
643 ui_browser__gotorc(browser
, end_row
, column
+ 1);
644 SLsmg_write_char(SLSMG_HLINE_CHAR
);
645 ui_browser__gotorc(browser
, end_row
, column
+ 2);
646 SLsmg_write_char(SLSMG_RARROW_CHAR
);
649 SLsmg_set_char_set(0);
652 static void __ui_browser__line_arrow_down(struct ui_browser
*browser
,
656 unsigned int row
, end_row
;
658 SLsmg_set_char_set(1);
660 if (start
>= browser
->top_idx
) {
661 row
= start
- browser
->top_idx
;
662 ui_browser__gotorc(browser
, row
, column
);
663 SLsmg_write_char(SLSMG_ULCORN_CHAR
);
664 ui_browser__gotorc(browser
, row
, column
+ 1);
672 if (end
>= browser
->top_idx
+ browser
->height
)
673 end_row
= browser
->height
- 1;
675 end_row
= end
- browser
->top_idx
;;
677 ui_browser__gotorc(browser
, row
, column
);
678 SLsmg_draw_vline(end_row
- row
+ 1);
680 ui_browser__gotorc(browser
, end_row
, column
);
681 if (end
< browser
->top_idx
+ browser
->height
) {
682 SLsmg_write_char(SLSMG_LLCORN_CHAR
);
683 ui_browser__gotorc(browser
, end_row
, column
+ 1);
684 SLsmg_write_char(SLSMG_HLINE_CHAR
);
685 ui_browser__gotorc(browser
, end_row
, column
+ 2);
686 SLsmg_write_char(SLSMG_RARROW_CHAR
);
689 SLsmg_set_char_set(0);
692 void __ui_browser__line_arrow(struct ui_browser
*browser
, unsigned int column
,
696 __ui_browser__line_arrow_up(browser
, column
, start
, end
);
698 __ui_browser__line_arrow_down(browser
, column
, start
, end
);
701 void ui_browser__init(void)
705 perf_config(ui_browser__color_config
, NULL
);
707 while (ui_browser__colorsets
[i
].name
) {
708 struct ui_browser__colorset
*c
= &ui_browser__colorsets
[i
++];
709 sltt_set_color(c
->colorset
, c
->name
, c
->fg
, c
->bg
);
712 annotate_browser__init();