3 #include "../../perf.h"
7 #include <linux/compiler.h>
8 #include <linux/list.h>
9 #include <linux/rbtree.h>
11 #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 void ui_browser__set_color(struct ui_browser *self __used, int color)
32 SLsmg_set_color(color);
35 void ui_browser__set_percent_color(struct ui_browser *self,
36 double percent, bool current)
38 int color = ui_browser__percent_color(self, percent, current);
39 ui_browser__set_color(self, color);
42 void ui_browser__gotorc(struct ui_browser *self, int y, int x)
44 SLsmg_gotorc(self->y + y, self->x + x);
47 static struct list_head *
48 ui_browser__list_head_filter_entries(struct ui_browser *browser,
49 struct list_head *pos)
52 if (!browser->filter || !browser->filter(browser, pos))
55 } while (pos != browser->entries);
60 static struct list_head *
61 ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
62 struct list_head *pos)
65 if (!browser->filter || !browser->filter(browser, pos))
68 } while (pos != browser->entries);
73 void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
75 struct list_head *head = self->entries;
76 struct list_head *pos;
78 if (self->nr_entries == 0)
83 pos = ui_browser__list_head_filter_entries(self, head->next);
89 pos = ui_browser__list_head_filter_prev_entries(self, head->prev);
99 pos = ui_browser__list_head_filter_entries(self, pos->next);
101 while (offset++ != 0)
102 pos = ui_browser__list_head_filter_prev_entries(self, pos->prev);
108 void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
110 struct rb_root *root = self->entries;
128 while (offset-- != 0)
131 while (offset++ != 0)
138 unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
143 if (self->top == NULL)
144 self->top = rb_first(self->entries);
149 ui_browser__gotorc(self, row, 0);
150 self->write(self, nd, row);
151 if (++row == self->height)
159 bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
161 return self->top_idx + row == self->index;
164 void ui_browser__refresh_dimensions(struct ui_browser *self)
166 self->width = SLtt_Screen_Cols - 1;
167 self->height = SLtt_Screen_Rows - 2;
172 void ui_browser__reset_index(struct ui_browser *self)
174 self->index = self->top_idx = 0;
175 self->seek(self, 0, SEEK_SET);
178 void __ui_browser__show_title(struct ui_browser *browser, const char *title)
181 ui_browser__set_color(browser, NEWT_COLORSET_ROOT);
182 slsmg_write_nstring(title, browser->width + 1);
185 void ui_browser__show_title(struct ui_browser *browser, const char *title)
187 pthread_mutex_lock(&ui__lock);
188 __ui_browser__show_title(browser, title);
189 pthread_mutex_unlock(&ui__lock);
192 int ui_browser__show(struct ui_browser *self, const char *title,
193 const char *helpline, ...)
198 ui_browser__refresh_dimensions(self);
200 pthread_mutex_lock(&ui__lock);
201 __ui_browser__show_title(self, title);
204 free(self->helpline);
205 self->helpline = NULL;
207 va_start(ap, helpline);
208 err = vasprintf(&self->helpline, helpline, ap);
211 ui_helpline__push(self->helpline);
212 pthread_mutex_unlock(&ui__lock);
216 void ui_browser__hide(struct ui_browser *browser __used)
218 pthread_mutex_lock(&ui__lock);
220 pthread_mutex_unlock(&ui__lock);
223 static void ui_browser__scrollbar_set(struct ui_browser *browser)
225 int height = browser->height, h = 0, pct = 0,
226 col = browser->width,
227 row = browser->y - 1;
229 if (browser->nr_entries > 1) {
230 pct = ((browser->index * (browser->height - 1)) /
231 (browser->nr_entries - 1));
235 ui_browser__gotorc(browser, row++, col);
236 SLsmg_set_char_set(1);
237 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_BOARD_CHAR);
238 SLsmg_set_char_set(0);
243 static int __ui_browser__refresh(struct ui_browser *browser)
246 int width = browser->width;
248 row = browser->refresh(browser);
249 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
251 if (!browser->use_navkeypressed || browser->navkeypressed)
252 ui_browser__scrollbar_set(browser);
256 SLsmg_fill_region(browser->y + row, browser->x,
257 browser->height - row, width, ' ');
262 int ui_browser__refresh(struct ui_browser *browser)
264 pthread_mutex_lock(&ui__lock);
265 __ui_browser__refresh(browser);
266 pthread_mutex_unlock(&ui__lock);
272 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
273 * forget about any reference to any entry in the underlying data structure,
274 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
275 * after an output_resort and hist decay.
277 void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
279 off_t offset = nr_entries - browser->nr_entries;
281 browser->nr_entries = nr_entries;
284 if (browser->top_idx < (u64)-offset)
285 offset = -browser->top_idx;
287 browser->index += offset;
288 browser->top_idx += offset;
292 browser->seek(browser, browser->top_idx, SEEK_SET);
295 int ui_browser__run(struct ui_browser *self, int delay_secs)
298 struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL;
300 pthread__unblock_sigwinch();
306 pthread_mutex_lock(&ui__lock);
307 err = __ui_browser__refresh(self);
309 pthread_mutex_unlock(&ui__lock);
314 FD_SET(0, &read_set);
317 timeout.tv_sec = delay_secs;
321 err = select(1, &read_set, NULL, NULL, ptimeout);
322 if (err > 0 && FD_ISSET(0, &read_set))
327 pthread_mutex_lock(&ui__lock);
328 SLtt_get_screen_size();
330 pthread_mutex_unlock(&ui__lock);
331 ui_browser__refresh_dimensions(self);
332 __ui_browser__show_title(self, self->title);
333 ui_helpline__puts(self->helpline);
337 if (self->use_navkeypressed && !self->navkeypressed) {
338 if (key == NEWT_KEY_DOWN || key == NEWT_KEY_UP ||
339 key == NEWT_KEY_PGDN || key == NEWT_KEY_PGUP ||
340 key == NEWT_KEY_HOME || key == NEWT_KEY_END ||
342 self->navkeypressed = true;
350 if (self->index == self->nr_entries - 1)
353 if (self->index == self->top_idx + self->height) {
355 self->seek(self, +1, SEEK_CUR);
359 if (self->index == 0)
362 if (self->index < self->top_idx) {
364 self->seek(self, -1, SEEK_CUR);
369 if (self->top_idx + self->height > self->nr_entries - 1)
372 offset = self->height;
373 if (self->index + offset > self->nr_entries - 1)
374 offset = self->nr_entries - 1 - self->index;
375 self->index += offset;
376 self->top_idx += offset;
377 self->seek(self, +offset, SEEK_CUR);
380 if (self->top_idx == 0)
383 if (self->top_idx < self->height)
384 offset = self->top_idx;
386 offset = self->height;
388 self->index -= offset;
389 self->top_idx -= offset;
390 self->seek(self, -offset, SEEK_CUR);
393 ui_browser__reset_index(self);
396 offset = self->height - 1;
397 if (offset >= self->nr_entries)
398 offset = self->nr_entries - 1;
400 self->index = self->nr_entries - 1;
401 self->top_idx = self->index - offset;
402 self->seek(self, -offset, SEEK_END);
411 unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
413 struct list_head *pos;
414 struct list_head *head = self->entries;
417 if (self->top == NULL || self->top == self->entries)
418 self->top = ui_browser__list_head_filter_entries(self, head->next);
422 list_for_each_from(pos, head) {
423 if (!self->filter || !self->filter(self, pos)) {
424 ui_browser__gotorc(self, row, 0);
425 self->write(self, pos, row);
426 if (++row == self->height)
434 static struct ui_browser__colorset {
435 const char *name, *fg, *bg;
437 } ui_browser__colorsets[] = {
439 .colorset = HE_COLORSET_TOP,
445 .colorset = HE_COLORSET_MEDIUM,
451 .colorset = HE_COLORSET_NORMAL,
457 .colorset = HE_COLORSET_SELECTED,
463 .colorset = HE_COLORSET_CODE,
474 static int ui_browser__color_config(const char *var, const char *value,
477 char *fg = NULL, *bg;
480 /* same dir for all commands */
481 if (prefixcmp(var, "colors.") != 0)
484 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
485 const char *name = var + 7;
487 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
494 bg = strchr(fg, ',');
499 while (isspace(*++bg));
500 ui_browser__colorsets[i].bg = bg;
501 ui_browser__colorsets[i].fg = fg;
509 void ui_browser__init(void)
513 perf_config(ui_browser__color_config, NULL);
515 while (ui_browser__colorsets[i].name) {
516 struct ui_browser__colorset *c = &ui_browser__colorsets[i++];
517 sltt_set_color(c->colorset, c->name, c->fg, c->bg);