blob: bbc782e364b0831f04b12ce417b22ae6b5948b16 [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 Melo250611c2011-10-14 12:27:54 -030049static struct list_head *
50ui_browser__list_head_filter_entries(struct ui_browser *browser,
51 struct list_head *pos)
52{
53 do {
54 if (!browser->filter || !browser->filter(browser, pos))
55 return pos;
56 pos = pos->next;
57 } while (pos != browser->entries);
58
59 return NULL;
60}
61
62static struct list_head *
63ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
64 struct list_head *pos)
65{
66 do {
67 if (!browser->filter || !browser->filter(browser, pos))
68 return pos;
69 pos = pos->prev;
70 } while (pos != browser->entries);
71
72 return NULL;
73}
74
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030075void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030076{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030077 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030078 struct list_head *pos;
79
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030080 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030081 return;
82
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030083 switch (whence) {
84 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030085 pos = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030086 break;
87 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030088 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030089 break;
90 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030091 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030092 break;
93 default:
94 return;
95 }
96
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030097 assert(pos != NULL);
98
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030099 if (offset > 0) {
100 while (offset-- != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300101 pos = ui_browser__list_head_filter_entries(browser, pos->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300102 } else {
103 while (offset++ != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300104 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300105 }
106
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300107 browser->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300108}
109
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300110void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300111{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300112 struct rb_root *root = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300113 struct rb_node *nd;
114
115 switch (whence) {
116 case SEEK_SET:
117 nd = rb_first(root);
118 break;
119 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300120 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300121 break;
122 case SEEK_END:
123 nd = rb_last(root);
124 break;
125 default:
126 return;
127 }
128
129 if (offset > 0) {
130 while (offset-- != 0)
131 nd = rb_next(nd);
132 } else {
133 while (offset++ != 0)
134 nd = rb_prev(nd);
135 }
136
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300137 browser->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300138}
139
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300140unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300141{
142 struct rb_node *nd;
143 int row = 0;
144
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300145 if (browser->top == NULL)
146 browser->top = rb_first(browser->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300147
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300148 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300149
150 while (nd != NULL) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300151 ui_browser__gotorc(browser, row, 0);
152 browser->write(browser, nd, row);
153 if (++row == browser->height)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300154 break;
155 nd = rb_next(nd);
156 }
157
158 return row;
159}
160
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300161bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300162{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300163 return browser->top_idx + row == browser->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300164}
165
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300166void ui_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300167{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300168 browser->width = SLtt_Screen_Cols - 1;
169 browser->height = SLtt_Screen_Rows - 2;
170 browser->y = 1;
171 browser->x = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300172}
173
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200174void ui_browser__handle_resize(struct ui_browser *browser)
175{
176 ui__refresh_dimensions(false);
177 ui_browser__show(browser, browser->title, ui_helpline__current);
178 ui_browser__refresh(browser);
179}
180
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200181int ui_browser__warning(struct ui_browser *browser, int timeout,
182 const char *format, ...)
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200183{
184 va_list args;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200185 char *text;
186 int key = 0, err;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200187
188 va_start(args, format);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200189 err = vasprintf(&text, format, args);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200190 va_end(args);
191
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200192 if (err < 0) {
193 va_start(args, format);
194 ui_helpline__vpush(format, args);
195 va_end(args);
196 } else {
197 while ((key == ui__question_window("Warning!", text,
198 "Press any key...",
199 timeout)) == K_RESIZE)
200 ui_browser__handle_resize(browser);
201 free(text);
202 }
203
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200204 return key;
205}
206
207int ui_browser__help_window(struct ui_browser *browser, const char *text)
208{
209 int key;
210
211 while ((key = ui__help_window(text)) == K_RESIZE)
212 ui_browser__handle_resize(browser);
213
214 return key;
215}
216
217bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
218{
219 int key;
220
221 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
222 ui_browser__handle_resize(browser);
223
224 return key == K_ENTER || toupper(key) == 'Y';
225}
226
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300227void ui_browser__reset_index(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300228{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300229 browser->index = browser->top_idx = 0;
230 browser->seek(browser, 0, SEEK_SET);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300231}
232
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300233void __ui_browser__show_title(struct ui_browser *browser, const char *title)
234{
235 SLsmg_gotorc(0, 0);
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300236 ui_browser__set_color(browser, HE_COLORSET_ROOT);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300237 slsmg_write_nstring(title, browser->width + 1);
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300238}
239
240void ui_browser__show_title(struct ui_browser *browser, const char *title)
241{
242 pthread_mutex_lock(&ui__lock);
243 __ui_browser__show_title(browser, title);
244 pthread_mutex_unlock(&ui__lock);
245}
246
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300247int ui_browser__show(struct ui_browser *browser, const char *title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300248 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300249{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300250 int err;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300251 va_list ap;
252
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300253 ui_browser__refresh_dimensions(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300254
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200255 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300256 __ui_browser__show_title(browser, title);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300257
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300258 browser->title = title;
259 free(browser->helpline);
260 browser->helpline = NULL;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300261
262 va_start(ap, helpline);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300263 err = vasprintf(&browser->helpline, helpline, ap);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300264 va_end(ap);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300265 if (err > 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300266 ui_helpline__push(browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200267 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300268 return err ? 0 : -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300269}
270
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300271void ui_browser__hide(struct ui_browser *browser __maybe_unused)
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300272{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200273 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300274 ui_helpline__pop();
Arnaldo Carvalho de Melo56ab7142013-01-24 22:45:21 -0300275 free(browser->helpline);
276 browser->helpline = NULL;
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200277 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300278}
279
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300280static void ui_browser__scrollbar_set(struct ui_browser *browser)
281{
282 int height = browser->height, h = 0, pct = 0,
283 col = browser->width,
284 row = browser->y - 1;
285
286 if (browser->nr_entries > 1) {
287 pct = ((browser->index * (browser->height - 1)) /
288 (browser->nr_entries - 1));
289 }
290
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200291 SLsmg_set_char_set(1);
292
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300293 while (h < height) {
294 ui_browser__gotorc(browser, row++, col);
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200295 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300296 ++h;
297 }
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200298
299 SLsmg_set_char_set(0);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300300}
301
302static int __ui_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300303{
304 int row;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200305 int width = browser->width;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300306
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300307 row = browser->refresh(browser);
308 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200309
310 if (!browser->use_navkeypressed || browser->navkeypressed)
311 ui_browser__scrollbar_set(browser);
312 else
313 width += 1;
314
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300315 SLsmg_fill_region(browser->y + row, browser->x,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200316 browser->height - row, width, ' ');
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300317
318 return 0;
319}
320
321int ui_browser__refresh(struct ui_browser *browser)
322{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200323 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300324 __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200325 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300326
327 return 0;
328}
329
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300330/*
331 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
332 * forget about any reference to any entry in the underlying data structure,
333 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
334 * after an output_resort and hist decay.
335 */
336void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
337{
338 off_t offset = nr_entries - browser->nr_entries;
339
340 browser->nr_entries = nr_entries;
341
342 if (offset < 0) {
343 if (browser->top_idx < (u64)-offset)
344 offset = -browser->top_idx;
345
346 browser->index += offset;
347 browser->top_idx += offset;
348 }
349
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300350 browser->top = NULL;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300351 browser->seek(browser, browser->top_idx, SEEK_SET);
352}
353
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300354int ui_browser__run(struct ui_browser *browser, int delay_secs)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300355{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300356 int err, key;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300357
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300358 while (1) {
359 off_t offset;
360
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300361 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300362 err = __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300363 SLsmg_refresh();
364 pthread_mutex_unlock(&ui__lock);
365 if (err < 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300366 break;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300367
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200368 key = ui__getch(delay_secs);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300369
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200370 if (key == K_RESIZE) {
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -0200371 ui__refresh_dimensions(false);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300372 ui_browser__refresh_dimensions(browser);
373 __ui_browser__show_title(browser, browser->title);
374 ui_helpline__puts(browser->helpline);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300375 continue;
376 }
377
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300378 if (browser->use_navkeypressed && !browser->navkeypressed) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200379 if (key == K_DOWN || key == K_UP ||
380 key == K_PGDN || key == K_PGUP ||
381 key == K_HOME || key == K_END ||
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200382 key == ' ') {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300383 browser->navkeypressed = true;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200384 continue;
385 } else
386 return key;
387 }
388
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300389 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200390 case K_DOWN:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300391 if (browser->index == browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300392 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300393 ++browser->index;
394 if (browser->index == browser->top_idx + browser->height) {
395 ++browser->top_idx;
396 browser->seek(browser, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300397 }
398 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200399 case K_UP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300400 if (browser->index == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300401 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300402 --browser->index;
403 if (browser->index < browser->top_idx) {
404 --browser->top_idx;
405 browser->seek(browser, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300406 }
407 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200408 case K_PGDN:
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300409 case ' ':
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300410 if (browser->top_idx + browser->height > browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300411 break;
412
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300413 offset = browser->height;
414 if (browser->index + offset > browser->nr_entries - 1)
415 offset = browser->nr_entries - 1 - browser->index;
416 browser->index += offset;
417 browser->top_idx += offset;
418 browser->seek(browser, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300419 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200420 case K_PGUP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300421 if (browser->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300422 break;
423
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300424 if (browser->top_idx < browser->height)
425 offset = browser->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300426 else
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300427 offset = browser->height;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300428
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300429 browser->index -= offset;
430 browser->top_idx -= offset;
431 browser->seek(browser, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300432 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200433 case K_HOME:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300434 ui_browser__reset_index(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300435 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200436 case K_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300437 offset = browser->height - 1;
438 if (offset >= browser->nr_entries)
439 offset = browser->nr_entries - 1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300440
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300441 browser->index = browser->nr_entries - 1;
442 browser->top_idx = browser->index - offset;
443 browser->seek(browser, -offset, SEEK_END);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300444 break;
445 default:
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300446 return key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300447 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300448 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300449 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300450}
451
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300452unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300453{
454 struct list_head *pos;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300455 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300456 int row = 0;
457
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300458 if (browser->top == NULL || browser->top == browser->entries)
459 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300460
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300461 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300462
463 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300464 if (!browser->filter || !browser->filter(browser, pos)) {
465 ui_browser__gotorc(browser, row, 0);
466 browser->write(browser, pos, row);
467 if (++row == browser->height)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300468 break;
469 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300470 }
471
472 return row;
473}
474
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300475static struct ui_browser_colorset {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200476 const char *name, *fg, *bg;
477 int colorset;
478} ui_browser__colorsets[] = {
479 {
480 .colorset = HE_COLORSET_TOP,
481 .name = "top",
482 .fg = "red",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200483 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200484 },
485 {
486 .colorset = HE_COLORSET_MEDIUM,
487 .name = "medium",
488 .fg = "green",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200489 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200490 },
491 {
492 .colorset = HE_COLORSET_NORMAL,
493 .name = "normal",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200494 .fg = "default",
495 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200496 },
497 {
498 .colorset = HE_COLORSET_SELECTED,
499 .name = "selected",
500 .fg = "black",
501 .bg = "lightgray",
502 },
503 {
504 .colorset = HE_COLORSET_CODE,
505 .name = "code",
506 .fg = "blue",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200507 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200508 },
509 {
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300510 .colorset = HE_COLORSET_ADDR,
511 .name = "addr",
512 .fg = "magenta",
513 .bg = "default",
514 },
515 {
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300516 .colorset = HE_COLORSET_ROOT,
517 .name = "root",
518 .fg = "white",
519 .bg = "blue",
520 },
521 {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200522 .name = NULL,
523 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300524};
525
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200526
527static int ui_browser__color_config(const char *var, const char *value,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300528 void *data __maybe_unused)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200529{
530 char *fg = NULL, *bg;
531 int i;
532
533 /* same dir for all commands */
534 if (prefixcmp(var, "colors.") != 0)
535 return 0;
536
537 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
538 const char *name = var + 7;
539
540 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
541 continue;
542
543 fg = strdup(value);
544 if (fg == NULL)
545 break;
546
547 bg = strchr(fg, ',');
548 if (bg == NULL)
549 break;
550
551 *bg = '\0';
552 while (isspace(*++bg));
553 ui_browser__colorsets[i].bg = bg;
554 ui_browser__colorsets[i].fg = fg;
555 return 0;
556 }
557
558 free(fg);
559 return -1;
560}
561
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -0200562void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
563{
564 switch (whence) {
565 case SEEK_SET:
566 browser->top = browser->entries;
567 break;
568 case SEEK_CUR:
569 browser->top = browser->top + browser->top_idx + offset;
570 break;
571 case SEEK_END:
572 browser->top = browser->top + browser->nr_entries + offset;
573 break;
574 default:
575 return;
576 }
577}
578
579unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
580{
581 unsigned int row = 0, idx = browser->top_idx;
582 char **pos;
583
584 if (browser->top == NULL)
585 browser->top = browser->entries;
586
587 pos = (char **)browser->top;
588 while (idx < browser->nr_entries) {
589 if (!browser->filter || !browser->filter(browser, *pos)) {
590 ui_browser__gotorc(browser, row, 0);
591 browser->write(browser, pos, row);
592 if (++row == browser->height)
593 break;
594 }
595
596 ++idx;
597 ++pos;
598 }
599
600 return row;
601}
602
Arnaldo Carvalho de Melo4656cca2012-05-03 13:07:05 -0300603void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
604 u16 start, u16 end)
605{
606 SLsmg_set_char_set(1);
607 ui_browser__gotorc(browser, start, column);
608 SLsmg_draw_vline(end - start + 1);
609 SLsmg_set_char_set(0);
610}
611
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300612void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
613 int graph)
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300614{
615 SLsmg_set_char_set(1);
616 SLsmg_write_char(graph);
617 SLsmg_set_char_set(0);
618}
619
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300620static void __ui_browser__line_arrow_up(struct ui_browser *browser,
621 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300622 u64 start, u64 end)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300623{
624 unsigned int row, end_row;
625
626 SLsmg_set_char_set(1);
627
628 if (start < browser->top_idx + browser->height) {
629 row = start - browser->top_idx;
630 ui_browser__gotorc(browser, row, column);
631 SLsmg_write_char(SLSMG_LLCORN_CHAR);
632 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300633 SLsmg_draw_hline(2);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300634
635 if (row-- == 0)
636 goto out;
637 } else
638 row = browser->height - 1;
639
640 if (end > browser->top_idx)
641 end_row = end - browser->top_idx;
642 else
643 end_row = 0;
644
645 ui_browser__gotorc(browser, end_row, column);
646 SLsmg_draw_vline(row - end_row + 1);
647
648 ui_browser__gotorc(browser, end_row, column);
649 if (end >= browser->top_idx) {
650 SLsmg_write_char(SLSMG_ULCORN_CHAR);
651 ui_browser__gotorc(browser, end_row, column + 1);
652 SLsmg_write_char(SLSMG_HLINE_CHAR);
653 ui_browser__gotorc(browser, end_row, column + 2);
654 SLsmg_write_char(SLSMG_RARROW_CHAR);
655 }
656out:
657 SLsmg_set_char_set(0);
658}
659
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300660static void __ui_browser__line_arrow_down(struct ui_browser *browser,
661 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300662 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300663{
664 unsigned int row, end_row;
665
666 SLsmg_set_char_set(1);
667
668 if (start >= browser->top_idx) {
669 row = start - browser->top_idx;
670 ui_browser__gotorc(browser, row, column);
671 SLsmg_write_char(SLSMG_ULCORN_CHAR);
672 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300673 SLsmg_draw_hline(2);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300674
675 if (row++ == 0)
676 goto out;
677 } else
678 row = 0;
679
680 if (end >= browser->top_idx + browser->height)
681 end_row = browser->height - 1;
682 else
683 end_row = end - browser->top_idx;;
684
685 ui_browser__gotorc(browser, row, column);
686 SLsmg_draw_vline(end_row - row + 1);
687
688 ui_browser__gotorc(browser, end_row, column);
689 if (end < browser->top_idx + browser->height) {
690 SLsmg_write_char(SLSMG_LLCORN_CHAR);
691 ui_browser__gotorc(browser, end_row, column + 1);
692 SLsmg_write_char(SLSMG_HLINE_CHAR);
693 ui_browser__gotorc(browser, end_row, column + 2);
694 SLsmg_write_char(SLSMG_RARROW_CHAR);
695 }
696out:
697 SLsmg_set_char_set(0);
698}
699
700void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300701 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300702{
703 if (start > end)
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300704 __ui_browser__line_arrow_up(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300705 else
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300706 __ui_browser__line_arrow_down(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300707}
708
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300709void ui_browser__init(void)
710{
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200711 int i = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300712
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200713 perf_config(ui_browser__color_config, NULL);
714
715 while (ui_browser__colorsets[i].name) {
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300716 struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200717 sltt_set_color(c->colorset, c->name, c->fg, c->bg);
718 }
Arnaldo Carvalho de Meloc323cf02012-05-29 22:06:30 -0300719
720 annotate_browser__init();
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300721}