blob: c6c7e5189214fb7f8df1c3fb3659a673d80e8ffa [file] [log] [blame]
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001#include "../util.h"
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -02002#include "../cache.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03003#include "../../perf.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03004#include "libslang.h"
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -02005#include "ui.h"
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -02006#include "util.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03007#include <linux/compiler.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -03008#include <linux/list.h>
9#include <linux/rbtree.h>
10#include <stdlib.h>
11#include <sys/ttydefaults.h>
12#include "browser.h"
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030013#include "helpline.h"
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -020014#include "keysyms.h"
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030015#include "../color.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -030016
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020017static int ui_browser__percent_color(struct ui_browser *browser,
18 double percent, bool current)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030019{
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020020 if (current && (!browser->use_navkeypressed || browser->navkeypressed))
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030021 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;
27}
28
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030029int ui_browser__set_color(struct ui_browser *browser, int color)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030030{
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030031 int ret = browser->current_color;
32 browser->current_color = color;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030033 SLsmg_set_color(color);
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030034 return ret;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030035}
36
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030037void ui_browser__set_percent_color(struct ui_browser *browser,
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030038 double percent, bool current)
39{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030040 int color = ui_browser__percent_color(browser, percent, current);
41 ui_browser__set_color(browser, color);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030042}
43
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030044void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030045{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030046 SLsmg_gotorc(browser->y + y, browser->x + x);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030047}
48
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -030049void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg,
50 unsigned int width)
51{
52 slsmg_write_nstring(msg, width);
53}
54
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -030055void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...)
56{
57 va_list args;
58
59 va_start(args, fmt);
60 slsmg_vprintf(fmt, args);
61 va_end(args);
62}
63
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030064static struct list_head *
65ui_browser__list_head_filter_entries(struct ui_browser *browser,
66 struct list_head *pos)
67{
68 do {
69 if (!browser->filter || !browser->filter(browser, pos))
70 return pos;
71 pos = pos->next;
72 } while (pos != browser->entries);
73
74 return NULL;
75}
76
77static struct list_head *
78ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
79 struct list_head *pos)
80{
81 do {
82 if (!browser->filter || !browser->filter(browser, pos))
83 return pos;
84 pos = pos->prev;
85 } while (pos != browser->entries);
86
87 return NULL;
88}
89
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030090void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030091{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030092 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030093 struct list_head *pos;
94
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030095 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030096 return;
97
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030098 switch (whence) {
99 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300100 pos = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300101 break;
102 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300103 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300104 break;
105 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300106 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300107 break;
108 default:
109 return;
110 }
111
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300112 assert(pos != NULL);
113
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300114 if (offset > 0) {
115 while (offset-- != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300116 pos = ui_browser__list_head_filter_entries(browser, pos->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300117 } else {
118 while (offset++ != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300119 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300120 }
121
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300122 browser->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300123}
124
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300125void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300126{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300127 struct rb_root *root = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300128 struct rb_node *nd;
129
130 switch (whence) {
131 case SEEK_SET:
132 nd = rb_first(root);
133 break;
134 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300135 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300136 break;
137 case SEEK_END:
138 nd = rb_last(root);
139 break;
140 default:
141 return;
142 }
143
144 if (offset > 0) {
145 while (offset-- != 0)
146 nd = rb_next(nd);
147 } else {
148 while (offset++ != 0)
149 nd = rb_prev(nd);
150 }
151
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300152 browser->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300153}
154
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300155unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300156{
157 struct rb_node *nd;
158 int row = 0;
159
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300160 if (browser->top == NULL)
161 browser->top = rb_first(browser->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300162
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300163 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300164
165 while (nd != NULL) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300166 ui_browser__gotorc(browser, row, 0);
167 browser->write(browser, nd, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300168 if (++row == browser->rows)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300169 break;
170 nd = rb_next(nd);
171 }
172
173 return row;
174}
175
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300176bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300177{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300178 return browser->top_idx + row == browser->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300179}
180
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300181void ui_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300182{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300183 browser->width = SLtt_Screen_Cols - 1;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300184 browser->height = browser->rows = SLtt_Screen_Rows - 2;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300185 browser->y = 1;
186 browser->x = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300187}
188
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200189void ui_browser__handle_resize(struct ui_browser *browser)
190{
191 ui__refresh_dimensions(false);
192 ui_browser__show(browser, browser->title, ui_helpline__current);
193 ui_browser__refresh(browser);
194}
195
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200196int ui_browser__warning(struct ui_browser *browser, int timeout,
197 const char *format, ...)
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200198{
199 va_list args;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200200 char *text;
201 int key = 0, err;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200202
203 va_start(args, format);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200204 err = vasprintf(&text, format, args);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200205 va_end(args);
206
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200207 if (err < 0) {
208 va_start(args, format);
209 ui_helpline__vpush(format, args);
210 va_end(args);
211 } else {
zhangdianfang7f3e5082014-05-30 08:53:58 +0800212 while ((key = ui__question_window("Warning!", text,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200213 "Press any key...",
214 timeout)) == K_RESIZE)
215 ui_browser__handle_resize(browser);
216 free(text);
217 }
218
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200219 return key;
220}
221
222int ui_browser__help_window(struct ui_browser *browser, const char *text)
223{
224 int key;
225
226 while ((key = ui__help_window(text)) == K_RESIZE)
227 ui_browser__handle_resize(browser);
228
229 return key;
230}
231
232bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
233{
234 int key;
235
236 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
237 ui_browser__handle_resize(browser);
238
239 return key == K_ENTER || toupper(key) == 'Y';
240}
241
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300242void ui_browser__reset_index(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300243{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300244 browser->index = browser->top_idx = 0;
245 browser->seek(browser, 0, SEEK_SET);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300246}
247
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300248void __ui_browser__show_title(struct ui_browser *browser, const char *title)
249{
250 SLsmg_gotorc(0, 0);
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300251 ui_browser__set_color(browser, HE_COLORSET_ROOT);
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -0300252 ui_browser__write_nstring(browser, title, browser->width + 1);
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300253}
254
255void ui_browser__show_title(struct ui_browser *browser, const char *title)
256{
257 pthread_mutex_lock(&ui__lock);
258 __ui_browser__show_title(browser, title);
259 pthread_mutex_unlock(&ui__lock);
260}
261
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300262int ui_browser__show(struct ui_browser *browser, const char *title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300263 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300264{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300265 int err;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300266 va_list ap;
267
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300268 if (browser->refresh_dimensions == NULL)
269 browser->refresh_dimensions = ui_browser__refresh_dimensions;
270
271 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300272
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200273 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300274 __ui_browser__show_title(browser, title);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300275
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300276 browser->title = title;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300277 zfree(&browser->helpline);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300278
279 va_start(ap, helpline);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300280 err = vasprintf(&browser->helpline, helpline, ap);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300281 va_end(ap);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300282 if (err > 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300283 ui_helpline__push(browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200284 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300285 return err ? 0 : -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300286}
287
Arnaldo Carvalho de Melo3184c472013-12-19 16:25:17 -0300288void ui_browser__hide(struct ui_browser *browser)
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300289{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200290 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300291 ui_helpline__pop();
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300292 zfree(&browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200293 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300294}
295
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300296static void ui_browser__scrollbar_set(struct ui_browser *browser)
297{
298 int height = browser->height, h = 0, pct = 0,
299 col = browser->width,
Jiri Olsa89632972014-06-19 13:41:14 +0200300 row = 0;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300301
302 if (browser->nr_entries > 1) {
303 pct = ((browser->index * (browser->height - 1)) /
304 (browser->nr_entries - 1));
305 }
306
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200307 SLsmg_set_char_set(1);
308
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300309 while (h < height) {
310 ui_browser__gotorc(browser, row++, col);
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200311 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300312 ++h;
313 }
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200314
315 SLsmg_set_char_set(0);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300316}
317
318static int __ui_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300319{
320 int row;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200321 int width = browser->width;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300322
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300323 row = browser->refresh(browser);
324 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200325
326 if (!browser->use_navkeypressed || browser->navkeypressed)
327 ui_browser__scrollbar_set(browser);
328 else
329 width += 1;
330
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300331 SLsmg_fill_region(browser->y + row, browser->x,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200332 browser->height - row, width, ' ');
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300333
334 return 0;
335}
336
337int ui_browser__refresh(struct ui_browser *browser)
338{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200339 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300340 __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200341 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300342
343 return 0;
344}
345
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300346/*
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.
351 */
352void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
353{
354 off_t offset = nr_entries - browser->nr_entries;
355
356 browser->nr_entries = nr_entries;
357
358 if (offset < 0) {
359 if (browser->top_idx < (u64)-offset)
360 offset = -browser->top_idx;
361
362 browser->index += offset;
363 browser->top_idx += offset;
364 }
365
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300366 browser->top = NULL;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300367 browser->seek(browser, browser->top_idx, SEEK_SET);
368}
369
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300370int ui_browser__run(struct ui_browser *browser, int delay_secs)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300371{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300372 int err, key;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300373
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300374 while (1) {
375 off_t offset;
376
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300377 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300378 err = __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300379 SLsmg_refresh();
380 pthread_mutex_unlock(&ui__lock);
381 if (err < 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300382 break;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300383
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200384 key = ui__getch(delay_secs);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300385
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200386 if (key == K_RESIZE) {
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -0200387 ui__refresh_dimensions(false);
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300388 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300389 __ui_browser__show_title(browser, browser->title);
390 ui_helpline__puts(browser->helpline);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300391 continue;
392 }
393
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300394 if (browser->use_navkeypressed && !browser->navkeypressed) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200395 if (key == K_DOWN || key == K_UP ||
396 key == K_PGDN || key == K_PGUP ||
397 key == K_HOME || key == K_END ||
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200398 key == ' ') {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300399 browser->navkeypressed = true;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200400 continue;
401 } else
402 return key;
403 }
404
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300405 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200406 case K_DOWN:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300407 if (browser->index == browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300408 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300409 ++browser->index;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300410 if (browser->index == browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300411 ++browser->top_idx;
412 browser->seek(browser, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300413 }
414 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200415 case K_UP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300416 if (browser->index == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300417 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300418 --browser->index;
419 if (browser->index < browser->top_idx) {
420 --browser->top_idx;
421 browser->seek(browser, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300422 }
423 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200424 case K_PGDN:
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300425 case ' ':
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300426 if (browser->top_idx + browser->rows > browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300427 break;
428
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300429 offset = browser->rows;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300430 if (browser->index + offset > browser->nr_entries - 1)
431 offset = browser->nr_entries - 1 - browser->index;
432 browser->index += offset;
433 browser->top_idx += offset;
434 browser->seek(browser, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300435 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200436 case K_PGUP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300437 if (browser->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300438 break;
439
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300440 if (browser->top_idx < browser->rows)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300441 offset = browser->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300442 else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300443 offset = browser->rows;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300444
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300445 browser->index -= offset;
446 browser->top_idx -= offset;
447 browser->seek(browser, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300448 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200449 case K_HOME:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300450 ui_browser__reset_index(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300451 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200452 case K_END:
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300453 offset = browser->rows - 1;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300454 if (offset >= browser->nr_entries)
455 offset = browser->nr_entries - 1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300456
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300457 browser->index = browser->nr_entries - 1;
458 browser->top_idx = browser->index - offset;
459 browser->seek(browser, -offset, SEEK_END);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300460 break;
461 default:
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300462 return key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300463 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300464 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300465 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300466}
467
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300468unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300469{
470 struct list_head *pos;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300471 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300472 int row = 0;
473
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300474 if (browser->top == NULL || browser->top == browser->entries)
475 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300476
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300477 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300478
479 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300480 if (!browser->filter || !browser->filter(browser, pos)) {
481 ui_browser__gotorc(browser, row, 0);
482 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300483 if (++row == browser->rows)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300484 break;
485 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300486 }
487
488 return row;
489}
490
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300491static struct ui_browser_colorset {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200492 const char *name, *fg, *bg;
493 int colorset;
494} ui_browser__colorsets[] = {
495 {
496 .colorset = HE_COLORSET_TOP,
497 .name = "top",
498 .fg = "red",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200499 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200500 },
501 {
502 .colorset = HE_COLORSET_MEDIUM,
503 .name = "medium",
504 .fg = "green",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200505 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200506 },
507 {
508 .colorset = HE_COLORSET_NORMAL,
509 .name = "normal",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200510 .fg = "default",
511 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200512 },
513 {
514 .colorset = HE_COLORSET_SELECTED,
515 .name = "selected",
516 .fg = "black",
517 .bg = "lightgray",
518 },
519 {
520 .colorset = HE_COLORSET_CODE,
521 .name = "code",
522 .fg = "blue",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200523 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200524 },
525 {
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300526 .colorset = HE_COLORSET_ADDR,
527 .name = "addr",
528 .fg = "magenta",
529 .bg = "default",
530 },
531 {
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300532 .colorset = HE_COLORSET_ROOT,
533 .name = "root",
534 .fg = "white",
535 .bg = "blue",
536 },
537 {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200538 .name = NULL,
539 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300540};
541
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200542
543static int ui_browser__color_config(const char *var, const char *value,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300544 void *data __maybe_unused)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200545{
546 char *fg = NULL, *bg;
547 int i;
548
549 /* same dir for all commands */
550 if (prefixcmp(var, "colors.") != 0)
551 return 0;
552
553 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
554 const char *name = var + 7;
555
556 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
557 continue;
558
559 fg = strdup(value);
560 if (fg == NULL)
561 break;
562
563 bg = strchr(fg, ',');
564 if (bg == NULL)
565 break;
566
567 *bg = '\0';
568 while (isspace(*++bg));
569 ui_browser__colorsets[i].bg = bg;
570 ui_browser__colorsets[i].fg = fg;
571 return 0;
572 }
573
574 free(fg);
575 return -1;
576}
577
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200578void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
579{
580 switch (whence) {
581 case SEEK_SET:
582 browser->top = browser->entries;
583 break;
584 case SEEK_CUR:
585 browser->top = browser->top + browser->top_idx + offset;
586 break;
587 case SEEK_END:
Arnaldo Carvalho de Melo48d038f2013-11-14 15:30:41 -0300588 browser->top = browser->top + browser->nr_entries - 1 + offset;
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200589 break;
590 default:
591 return;
592 }
593}
594
595unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
596{
597 unsigned int row = 0, idx = browser->top_idx;
598 char **pos;
599
600 if (browser->top == NULL)
601 browser->top = browser->entries;
602
603 pos = (char **)browser->top;
604 while (idx < browser->nr_entries) {
605 if (!browser->filter || !browser->filter(browser, *pos)) {
606 ui_browser__gotorc(browser, row, 0);
607 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300608 if (++row == browser->rows)
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200609 break;
610 }
611
612 ++idx;
613 ++pos;
614 }
615
616 return row;
617}
618
Arnaldo Carvalho de Melo4656cca2012-05-03 13:07:05 -0300619void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
620 u16 start, u16 end)
621{
622 SLsmg_set_char_set(1);
623 ui_browser__gotorc(browser, start, column);
624 SLsmg_draw_vline(end - start + 1);
625 SLsmg_set_char_set(0);
626}
627
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300628void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
629 int graph)
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300630{
631 SLsmg_set_char_set(1);
632 SLsmg_write_char(graph);
633 SLsmg_set_char_set(0);
634}
635
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300636static void __ui_browser__line_arrow_up(struct ui_browser *browser,
637 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300638 u64 start, u64 end)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300639{
640 unsigned int row, end_row;
641
642 SLsmg_set_char_set(1);
643
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300644 if (start < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300645 row = start - browser->top_idx;
646 ui_browser__gotorc(browser, row, column);
647 SLsmg_write_char(SLSMG_LLCORN_CHAR);
648 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300649 SLsmg_draw_hline(2);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300650
651 if (row-- == 0)
652 goto out;
653 } else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300654 row = browser->rows - 1;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300655
656 if (end > browser->top_idx)
657 end_row = end - browser->top_idx;
658 else
659 end_row = 0;
660
661 ui_browser__gotorc(browser, end_row, column);
662 SLsmg_draw_vline(row - end_row + 1);
663
664 ui_browser__gotorc(browser, end_row, column);
665 if (end >= browser->top_idx) {
666 SLsmg_write_char(SLSMG_ULCORN_CHAR);
667 ui_browser__gotorc(browser, end_row, column + 1);
668 SLsmg_write_char(SLSMG_HLINE_CHAR);
669 ui_browser__gotorc(browser, end_row, column + 2);
670 SLsmg_write_char(SLSMG_RARROW_CHAR);
671 }
672out:
673 SLsmg_set_char_set(0);
674}
675
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300676static void __ui_browser__line_arrow_down(struct ui_browser *browser,
677 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300678 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300679{
680 unsigned int row, end_row;
681
682 SLsmg_set_char_set(1);
683
684 if (start >= browser->top_idx) {
685 row = start - browser->top_idx;
686 ui_browser__gotorc(browser, row, column);
687 SLsmg_write_char(SLSMG_ULCORN_CHAR);
688 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300689 SLsmg_draw_hline(2);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300690
691 if (row++ == 0)
692 goto out;
693 } else
694 row = 0;
695
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300696 if (end >= browser->top_idx + browser->rows)
697 end_row = browser->rows - 1;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300698 else
Davidlohr Buesob2222132013-11-12 22:24:24 -0800699 end_row = end - browser->top_idx;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300700
701 ui_browser__gotorc(browser, row, column);
702 SLsmg_draw_vline(end_row - row + 1);
703
704 ui_browser__gotorc(browser, end_row, column);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300705 if (end < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300706 SLsmg_write_char(SLSMG_LLCORN_CHAR);
707 ui_browser__gotorc(browser, end_row, column + 1);
708 SLsmg_write_char(SLSMG_HLINE_CHAR);
709 ui_browser__gotorc(browser, end_row, column + 2);
710 SLsmg_write_char(SLSMG_RARROW_CHAR);
711 }
712out:
713 SLsmg_set_char_set(0);
714}
715
716void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300717 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300718{
719 if (start > end)
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300720 __ui_browser__line_arrow_up(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300721 else
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300722 __ui_browser__line_arrow_down(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300723}
724
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300725void ui_browser__init(void)
726{
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200727 int i = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300728
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200729 perf_config(ui_browser__color_config, NULL);
730
731 while (ui_browser__colorsets[i].name) {
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300732 struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200733 sltt_set_color(c->colorset, c->name, c->fg, c->bg);
734 }
Arnaldo Carvalho de Meloc323cf02012-05-29 22:06:30 -0300735
736 annotate_browser__init();
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300737}