blob: d0c2007c307bbaf266a8a84d7f054c1d41994c72 [file] [log] [blame]
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001#include "../util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03002#include "../string2.h"
Taeung Song41840d22016-06-23 17:55:17 +09003#include "../config.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03004#include "../../perf.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03005#include "libslang.h"
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -02006#include "ui.h"
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -02007#include "util.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03008#include <linux/compiler.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -03009#include <linux/list.h>
10#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -030011#include <linux/string.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030012#include <stdlib.h>
13#include <sys/ttydefaults.h>
14#include "browser.h"
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030015#include "helpline.h"
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -020016#include "keysyms.h"
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030017#include "../color.h"
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030018#include "sane_ctype.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -030019
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020020static int ui_browser__percent_color(struct ui_browser *browser,
21 double percent, bool current)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030022{
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020023 if (current && (!browser->use_navkeypressed || browser->navkeypressed))
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030024 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;
30}
31
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030032int ui_browser__set_color(struct ui_browser *browser, int color)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030033{
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030034 int ret = browser->current_color;
35 browser->current_color = color;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030036 SLsmg_set_color(color);
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030037 return ret;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030038}
39
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030040void ui_browser__set_percent_color(struct ui_browser *browser,
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030041 double percent, bool current)
42{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030043 int color = ui_browser__percent_color(browser, percent, current);
44 ui_browser__set_color(browser, color);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030045}
46
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030047void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030048{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030049 SLsmg_gotorc(browser->y + y, browser->x + x);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030050}
51
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -030052void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg,
53 unsigned int width)
54{
55 slsmg_write_nstring(msg, width);
56}
57
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -030058void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...)
59{
60 va_list args;
61
62 va_start(args, fmt);
63 slsmg_vprintf(fmt, args);
64 va_end(args);
65}
66
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030067static struct list_head *
68ui_browser__list_head_filter_entries(struct ui_browser *browser,
69 struct list_head *pos)
70{
71 do {
72 if (!browser->filter || !browser->filter(browser, pos))
73 return pos;
74 pos = pos->next;
75 } while (pos != browser->entries);
76
77 return NULL;
78}
79
80static struct list_head *
81ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
82 struct list_head *pos)
83{
84 do {
85 if (!browser->filter || !browser->filter(browser, pos))
86 return pos;
87 pos = pos->prev;
88 } while (pos != browser->entries);
89
90 return NULL;
91}
92
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030093void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030094{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030095 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030096 struct list_head *pos;
97
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030098 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030099 return;
100
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300101 switch (whence) {
102 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300103 pos = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300104 break;
105 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300106 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300107 break;
108 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300109 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300110 break;
111 default:
112 return;
113 }
114
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300115 assert(pos != NULL);
116
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300117 if (offset > 0) {
118 while (offset-- != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300119 pos = ui_browser__list_head_filter_entries(browser, pos->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300120 } else {
121 while (offset++ != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300122 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300123 }
124
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300125 browser->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300126}
127
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300128void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300129{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300130 struct rb_root *root = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300131 struct rb_node *nd;
132
133 switch (whence) {
134 case SEEK_SET:
135 nd = rb_first(root);
136 break;
137 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300138 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300139 break;
140 case SEEK_END:
141 nd = rb_last(root);
142 break;
143 default:
144 return;
145 }
146
147 if (offset > 0) {
148 while (offset-- != 0)
149 nd = rb_next(nd);
150 } else {
151 while (offset++ != 0)
152 nd = rb_prev(nd);
153 }
154
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300155 browser->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300156}
157
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300158unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300159{
160 struct rb_node *nd;
161 int row = 0;
162
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300163 if (browser->top == NULL)
164 browser->top = rb_first(browser->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300165
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300166 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300167
168 while (nd != NULL) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300169 ui_browser__gotorc(browser, row, 0);
170 browser->write(browser, nd, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300171 if (++row == browser->rows)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300172 break;
173 nd = rb_next(nd);
174 }
175
176 return row;
177}
178
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300179bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300180{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300181 return browser->top_idx + row == browser->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300182}
183
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300184void ui_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300185{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300186 browser->width = SLtt_Screen_Cols - 1;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300187 browser->height = browser->rows = SLtt_Screen_Rows - 2;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300188 browser->y = 1;
189 browser->x = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300190}
191
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200192void ui_browser__handle_resize(struct ui_browser *browser)
193{
194 ui__refresh_dimensions(false);
195 ui_browser__show(browser, browser->title, ui_helpline__current);
196 ui_browser__refresh(browser);
197}
198
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200199int ui_browser__warning(struct ui_browser *browser, int timeout,
200 const char *format, ...)
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200201{
202 va_list args;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200203 char *text;
204 int key = 0, err;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200205
206 va_start(args, format);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200207 err = vasprintf(&text, format, args);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200208 va_end(args);
209
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200210 if (err < 0) {
211 va_start(args, format);
212 ui_helpline__vpush(format, args);
213 va_end(args);
214 } else {
zhangdianfang7f3e5082014-05-30 08:53:58 +0800215 while ((key = ui__question_window("Warning!", text,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200216 "Press any key...",
217 timeout)) == K_RESIZE)
218 ui_browser__handle_resize(browser);
219 free(text);
220 }
221
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200222 return key;
223}
224
225int ui_browser__help_window(struct ui_browser *browser, const char *text)
226{
227 int key;
228
229 while ((key = ui__help_window(text)) == K_RESIZE)
230 ui_browser__handle_resize(browser);
231
232 return key;
233}
234
235bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
236{
237 int key;
238
239 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
240 ui_browser__handle_resize(browser);
241
242 return key == K_ENTER || toupper(key) == 'Y';
243}
244
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300245void ui_browser__reset_index(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300246{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300247 browser->index = browser->top_idx = 0;
248 browser->seek(browser, 0, SEEK_SET);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300249}
250
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300251void __ui_browser__show_title(struct ui_browser *browser, const char *title)
252{
253 SLsmg_gotorc(0, 0);
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300254 ui_browser__set_color(browser, HE_COLORSET_ROOT);
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -0300255 ui_browser__write_nstring(browser, title, browser->width + 1);
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300256}
257
258void ui_browser__show_title(struct ui_browser *browser, const char *title)
259{
260 pthread_mutex_lock(&ui__lock);
261 __ui_browser__show_title(browser, title);
262 pthread_mutex_unlock(&ui__lock);
263}
264
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300265int ui_browser__show(struct ui_browser *browser, const char *title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300266 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300267{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300268 int err;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300269 va_list ap;
270
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300271 if (browser->refresh_dimensions == NULL)
272 browser->refresh_dimensions = ui_browser__refresh_dimensions;
273
274 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300275
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200276 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300277 __ui_browser__show_title(browser, title);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300278
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300279 browser->title = title;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300280 zfree(&browser->helpline);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300281
282 va_start(ap, helpline);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300283 err = vasprintf(&browser->helpline, helpline, ap);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300284 va_end(ap);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300285 if (err > 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300286 ui_helpline__push(browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200287 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300288 return err ? 0 : -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300289}
290
Arnaldo Carvalho de Melo3184c472013-12-19 16:25:17 -0300291void ui_browser__hide(struct ui_browser *browser)
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300292{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200293 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300294 ui_helpline__pop();
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300295 zfree(&browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200296 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300297}
298
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300299static void ui_browser__scrollbar_set(struct ui_browser *browser)
300{
301 int height = browser->height, h = 0, pct = 0,
302 col = browser->width,
Jiri Olsa89632972014-06-19 13:41:14 +0200303 row = 0;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300304
305 if (browser->nr_entries > 1) {
306 pct = ((browser->index * (browser->height - 1)) /
307 (browser->nr_entries - 1));
308 }
309
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200310 SLsmg_set_char_set(1);
311
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300312 while (h < height) {
313 ui_browser__gotorc(browser, row++, col);
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200314 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300315 ++h;
316 }
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200317
318 SLsmg_set_char_set(0);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300319}
320
321static int __ui_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300322{
323 int row;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200324 int width = browser->width;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300325
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300326 row = browser->refresh(browser);
327 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200328
329 if (!browser->use_navkeypressed || browser->navkeypressed)
330 ui_browser__scrollbar_set(browser);
331 else
332 width += 1;
333
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300334 SLsmg_fill_region(browser->y + row, browser->x,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200335 browser->height - row, width, ' ');
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300336
337 return 0;
338}
339
340int ui_browser__refresh(struct ui_browser *browser)
341{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200342 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300343 __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200344 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300345
346 return 0;
347}
348
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300349/*
350 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
351 * forget about any reference to any entry in the underlying data structure,
352 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
353 * after an output_resort and hist decay.
354 */
355void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
356{
357 off_t offset = nr_entries - browser->nr_entries;
358
359 browser->nr_entries = nr_entries;
360
361 if (offset < 0) {
362 if (browser->top_idx < (u64)-offset)
363 offset = -browser->top_idx;
364
365 browser->index += offset;
366 browser->top_idx += offset;
367 }
368
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300369 browser->top = NULL;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300370 browser->seek(browser, browser->top_idx, SEEK_SET);
371}
372
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300373int ui_browser__run(struct ui_browser *browser, int delay_secs)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300374{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300375 int err, key;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300376
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300377 while (1) {
378 off_t offset;
379
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300380 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300381 err = __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300382 SLsmg_refresh();
383 pthread_mutex_unlock(&ui__lock);
384 if (err < 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300385 break;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300386
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200387 key = ui__getch(delay_secs);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300388
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200389 if (key == K_RESIZE) {
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -0200390 ui__refresh_dimensions(false);
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300391 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300392 __ui_browser__show_title(browser, browser->title);
393 ui_helpline__puts(browser->helpline);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300394 continue;
395 }
396
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300397 if (browser->use_navkeypressed && !browser->navkeypressed) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200398 if (key == K_DOWN || key == K_UP ||
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300399 (browser->columns && (key == K_LEFT || key == K_RIGHT)) ||
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200400 key == K_PGDN || key == K_PGUP ||
401 key == K_HOME || key == K_END ||
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200402 key == ' ') {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300403 browser->navkeypressed = true;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200404 continue;
405 } else
406 return key;
407 }
408
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300409 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200410 case K_DOWN:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300411 if (browser->index == browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300412 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300413 ++browser->index;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300414 if (browser->index == browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300415 ++browser->top_idx;
416 browser->seek(browser, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300417 }
418 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200419 case K_UP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300420 if (browser->index == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300421 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300422 --browser->index;
423 if (browser->index < browser->top_idx) {
424 --browser->top_idx;
425 browser->seek(browser, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300426 }
427 break;
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300428 case K_RIGHT:
429 if (!browser->columns)
430 goto out;
431 if (browser->horiz_scroll < browser->columns - 1)
432 ++browser->horiz_scroll;
433 break;
434 case K_LEFT:
435 if (!browser->columns)
436 goto out;
437 if (browser->horiz_scroll != 0)
438 --browser->horiz_scroll;
439 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200440 case K_PGDN:
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300441 case ' ':
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300442 if (browser->top_idx + browser->rows > browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300443 break;
444
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300445 offset = browser->rows;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300446 if (browser->index + offset > browser->nr_entries - 1)
447 offset = browser->nr_entries - 1 - browser->index;
448 browser->index += offset;
449 browser->top_idx += offset;
450 browser->seek(browser, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300451 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200452 case K_PGUP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300453 if (browser->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300454 break;
455
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300456 if (browser->top_idx < browser->rows)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300457 offset = browser->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300458 else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300459 offset = browser->rows;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300460
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300461 browser->index -= offset;
462 browser->top_idx -= offset;
463 browser->seek(browser, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300464 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200465 case K_HOME:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300466 ui_browser__reset_index(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300467 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200468 case K_END:
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300469 offset = browser->rows - 1;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300470 if (offset >= browser->nr_entries)
471 offset = browser->nr_entries - 1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300472
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300473 browser->index = browser->nr_entries - 1;
474 browser->top_idx = browser->index - offset;
475 browser->seek(browser, -offset, SEEK_END);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300476 break;
477 default:
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300478 out:
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300479 return key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300480 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300481 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300482 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300483}
484
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300485unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300486{
487 struct list_head *pos;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300488 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300489 int row = 0;
490
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300491 if (browser->top == NULL || browser->top == browser->entries)
492 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300493
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300494 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300495
496 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300497 if (!browser->filter || !browser->filter(browser, pos)) {
498 ui_browser__gotorc(browser, row, 0);
499 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300500 if (++row == browser->rows)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300501 break;
502 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300503 }
504
505 return row;
506}
507
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300508static struct ui_browser_colorset {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200509 const char *name, *fg, *bg;
510 int colorset;
511} ui_browser__colorsets[] = {
512 {
513 .colorset = HE_COLORSET_TOP,
514 .name = "top",
515 .fg = "red",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200516 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200517 },
518 {
519 .colorset = HE_COLORSET_MEDIUM,
520 .name = "medium",
521 .fg = "green",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200522 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200523 },
524 {
525 .colorset = HE_COLORSET_NORMAL,
526 .name = "normal",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200527 .fg = "default",
528 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200529 },
530 {
531 .colorset = HE_COLORSET_SELECTED,
532 .name = "selected",
533 .fg = "black",
Ingo Molnar8d7d3772015-03-05 11:32:13 +0100534 .bg = "yellow",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200535 },
536 {
Taeung Song78ce08d2016-01-08 17:16:11 +0900537 .colorset = HE_COLORSET_JUMP_ARROWS,
538 .name = "jump_arrows",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200539 .fg = "blue",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200540 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200541 },
542 {
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300543 .colorset = HE_COLORSET_ADDR,
544 .name = "addr",
545 .fg = "magenta",
546 .bg = "default",
547 },
548 {
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300549 .colorset = HE_COLORSET_ROOT,
550 .name = "root",
551 .fg = "white",
552 .bg = "blue",
553 },
554 {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200555 .name = NULL,
556 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300557};
558
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200559
560static int ui_browser__color_config(const char *var, const char *value,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300561 void *data __maybe_unused)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200562{
563 char *fg = NULL, *bg;
564 int i;
565
566 /* same dir for all commands */
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300567 if (!strstarts(var, "colors.") != 0)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200568 return 0;
569
570 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
571 const char *name = var + 7;
572
573 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
574 continue;
575
576 fg = strdup(value);
577 if (fg == NULL)
578 break;
579
580 bg = strchr(fg, ',');
581 if (bg == NULL)
582 break;
583
584 *bg = '\0';
Taeung Songe21600f2017-04-07 23:24:19 +0900585 bg = ltrim(++bg);
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200586 ui_browser__colorsets[i].bg = bg;
587 ui_browser__colorsets[i].fg = fg;
588 return 0;
589 }
590
591 free(fg);
592 return -1;
593}
594
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200595void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
596{
597 switch (whence) {
598 case SEEK_SET:
599 browser->top = browser->entries;
600 break;
601 case SEEK_CUR:
602 browser->top = browser->top + browser->top_idx + offset;
603 break;
604 case SEEK_END:
Arnaldo Carvalho de Melo48d038f2013-11-14 15:30:41 -0300605 browser->top = browser->top + browser->nr_entries - 1 + offset;
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200606 break;
607 default:
608 return;
609 }
610}
611
612unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
613{
614 unsigned int row = 0, idx = browser->top_idx;
615 char **pos;
616
617 if (browser->top == NULL)
618 browser->top = browser->entries;
619
620 pos = (char **)browser->top;
621 while (idx < browser->nr_entries) {
622 if (!browser->filter || !browser->filter(browser, *pos)) {
623 ui_browser__gotorc(browser, row, 0);
624 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300625 if (++row == browser->rows)
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200626 break;
627 }
628
629 ++idx;
630 ++pos;
631 }
632
633 return row;
634}
635
Arnaldo Carvalho de Melo4656cca2012-05-03 13:07:05 -0300636void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
637 u16 start, u16 end)
638{
639 SLsmg_set_char_set(1);
640 ui_browser__gotorc(browser, start, column);
641 SLsmg_draw_vline(end - start + 1);
642 SLsmg_set_char_set(0);
643}
644
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300645void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
646 int graph)
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300647{
648 SLsmg_set_char_set(1);
649 SLsmg_write_char(graph);
650 SLsmg_set_char_set(0);
651}
652
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300653static void __ui_browser__line_arrow_up(struct ui_browser *browser,
654 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300655 u64 start, u64 end)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300656{
657 unsigned int row, end_row;
658
659 SLsmg_set_char_set(1);
660
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300661 if (start < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300662 row = start - browser->top_idx;
663 ui_browser__gotorc(browser, row, column);
664 SLsmg_write_char(SLSMG_LLCORN_CHAR);
665 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300666 SLsmg_draw_hline(2);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300667
668 if (row-- == 0)
669 goto out;
670 } else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300671 row = browser->rows - 1;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300672
673 if (end > browser->top_idx)
674 end_row = end - browser->top_idx;
675 else
676 end_row = 0;
677
678 ui_browser__gotorc(browser, end_row, column);
679 SLsmg_draw_vline(row - end_row + 1);
680
681 ui_browser__gotorc(browser, end_row, column);
682 if (end >= browser->top_idx) {
683 SLsmg_write_char(SLSMG_ULCORN_CHAR);
684 ui_browser__gotorc(browser, end_row, column + 1);
685 SLsmg_write_char(SLSMG_HLINE_CHAR);
686 ui_browser__gotorc(browser, end_row, column + 2);
687 SLsmg_write_char(SLSMG_RARROW_CHAR);
688 }
689out:
690 SLsmg_set_char_set(0);
691}
692
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300693static void __ui_browser__line_arrow_down(struct ui_browser *browser,
694 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300695 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300696{
697 unsigned int row, end_row;
698
699 SLsmg_set_char_set(1);
700
701 if (start >= browser->top_idx) {
702 row = start - browser->top_idx;
703 ui_browser__gotorc(browser, row, column);
704 SLsmg_write_char(SLSMG_ULCORN_CHAR);
705 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300706 SLsmg_draw_hline(2);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300707
Jin Yao80f62582017-06-08 14:01:44 +0800708 if (++row == 0)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300709 goto out;
710 } else
711 row = 0;
712
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300713 if (end >= browser->top_idx + browser->rows)
714 end_row = browser->rows - 1;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300715 else
Davidlohr Buesob2222132013-11-12 22:24:24 -0800716 end_row = end - browser->top_idx;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300717
718 ui_browser__gotorc(browser, row, column);
719 SLsmg_draw_vline(end_row - row + 1);
720
721 ui_browser__gotorc(browser, end_row, column);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300722 if (end < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300723 SLsmg_write_char(SLSMG_LLCORN_CHAR);
724 ui_browser__gotorc(browser, end_row, column + 1);
725 SLsmg_write_char(SLSMG_HLINE_CHAR);
726 ui_browser__gotorc(browser, end_row, column + 2);
727 SLsmg_write_char(SLSMG_RARROW_CHAR);
728 }
729out:
730 SLsmg_set_char_set(0);
731}
732
733void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300734 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300735{
736 if (start > end)
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300737 __ui_browser__line_arrow_up(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300738 else
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300739 __ui_browser__line_arrow_down(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300740}
741
Jin Yao7e63a132017-07-07 13:06:35 +0800742void ui_browser__mark_fused(struct ui_browser *browser, unsigned int column,
743 unsigned int row, bool arrow_down)
744{
745 unsigned int end_row;
746
747 if (row >= browser->top_idx)
748 end_row = row - browser->top_idx;
749 else
750 return;
751
752 SLsmg_set_char_set(1);
753
754 if (arrow_down) {
755 ui_browser__gotorc(browser, end_row, column - 1);
756 SLsmg_write_char(SLSMG_ULCORN_CHAR);
757 ui_browser__gotorc(browser, end_row, column);
758 SLsmg_draw_hline(2);
759 ui_browser__gotorc(browser, end_row + 1, column - 1);
760 SLsmg_write_char(SLSMG_LTEE_CHAR);
761 } else {
762 ui_browser__gotorc(browser, end_row, column - 1);
763 SLsmg_write_char(SLSMG_LTEE_CHAR);
764 ui_browser__gotorc(browser, end_row, column);
765 SLsmg_draw_hline(2);
766 }
767
768 SLsmg_set_char_set(0);
769}
770
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300771void ui_browser__init(void)
772{
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200773 int i = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300774
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200775 perf_config(ui_browser__color_config, NULL);
776
777 while (ui_browser__colorsets[i].name) {
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300778 struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200779 sltt_set_color(c->colorset, c->name, c->fg, c->bg);
780 }
Arnaldo Carvalho de Meloc323cf02012-05-29 22:06:30 -0300781
782 annotate_browser__init();
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300783}