blob: 9f6ce29b83b43244b8d425329fa0fb284a462418 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03002#include "../util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03003#include "../string2.h"
Taeung Song41840d22016-06-23 17:55:17 +09004#include "../config.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03005#include "../../perf.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03006#include "libslang.h"
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -02007#include "ui.h"
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -02008#include "util.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03009#include <linux/compiler.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030010#include <linux/list.h>
11#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -030012#include <linux/string.h>
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030013#include <stdlib.h>
14#include <sys/ttydefaults.h>
15#include "browser.h"
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030016#include "helpline.h"
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -020017#include "keysyms.h"
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030018#include "../color.h"
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030019#include "sane_ctype.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -030020
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020021static int ui_browser__percent_color(struct ui_browser *browser,
22 double percent, bool current)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030023{
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020024 if (current && (!browser->use_navkeypressed || browser->navkeypressed))
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030025 return HE_COLORSET_SELECTED;
26 if (percent >= MIN_RED)
27 return HE_COLORSET_TOP;
28 if (percent >= MIN_GREEN)
29 return HE_COLORSET_MEDIUM;
30 return HE_COLORSET_NORMAL;
31}
32
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030033int ui_browser__set_color(struct ui_browser *browser, int color)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030034{
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030035 int ret = browser->current_color;
36 browser->current_color = color;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030037 SLsmg_set_color(color);
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030038 return ret;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030039}
40
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030041void ui_browser__set_percent_color(struct ui_browser *browser,
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030042 double percent, bool current)
43{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030044 int color = ui_browser__percent_color(browser, percent, current);
45 ui_browser__set_color(browser, color);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030046}
47
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030048void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030049{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030050 SLsmg_gotorc(browser->y + y, browser->x + x);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030051}
52
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -030053void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg,
54 unsigned int width)
55{
56 slsmg_write_nstring(msg, width);
57}
58
Arnaldo Carvalho de Meloc5220242018-03-15 17:52:53 -030059void ui_browser__vprintf(struct ui_browser *browser __maybe_unused, const char *fmt, va_list args)
60{
61 slsmg_vprintf(fmt, args);
62}
63
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -030064void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...)
65{
66 va_list args;
67
68 va_start(args, fmt);
Arnaldo Carvalho de Meloc5220242018-03-15 17:52:53 -030069 ui_browser__vprintf(browser, fmt, args);
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -030070 va_end(args);
71}
72
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030073static struct list_head *
74ui_browser__list_head_filter_entries(struct ui_browser *browser,
75 struct list_head *pos)
76{
77 do {
78 if (!browser->filter || !browser->filter(browser, pos))
79 return pos;
80 pos = pos->next;
81 } while (pos != browser->entries);
82
83 return NULL;
84}
85
86static struct list_head *
87ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
88 struct list_head *pos)
89{
90 do {
91 if (!browser->filter || !browser->filter(browser, pos))
92 return pos;
93 pos = pos->prev;
94 } while (pos != browser->entries);
95
96 return NULL;
97}
98
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030099void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300100{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300101 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300102 struct list_head *pos;
103
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300104 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300105 return;
106
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300107 switch (whence) {
108 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300109 pos = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300110 break;
111 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300112 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300113 break;
114 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300115 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300116 break;
117 default:
118 return;
119 }
120
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300121 assert(pos != NULL);
122
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300123 if (offset > 0) {
124 while (offset-- != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300125 pos = ui_browser__list_head_filter_entries(browser, pos->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300126 } else {
127 while (offset++ != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300128 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300129 }
130
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300131 browser->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300132}
133
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300134void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300135{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300136 struct rb_root *root = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300137 struct rb_node *nd;
138
139 switch (whence) {
140 case SEEK_SET:
141 nd = rb_first(root);
142 break;
143 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300144 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300145 break;
146 case SEEK_END:
147 nd = rb_last(root);
148 break;
149 default:
150 return;
151 }
152
153 if (offset > 0) {
154 while (offset-- != 0)
155 nd = rb_next(nd);
156 } else {
157 while (offset++ != 0)
158 nd = rb_prev(nd);
159 }
160
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300161 browser->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300162}
163
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300164unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300165{
166 struct rb_node *nd;
167 int row = 0;
168
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300169 if (browser->top == NULL)
170 browser->top = rb_first(browser->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300171
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300172 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300173
174 while (nd != NULL) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300175 ui_browser__gotorc(browser, row, 0);
176 browser->write(browser, nd, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300177 if (++row == browser->rows)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300178 break;
179 nd = rb_next(nd);
180 }
181
182 return row;
183}
184
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300185bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300186{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300187 return browser->top_idx + row == browser->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300188}
189
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300190void ui_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300191{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300192 browser->width = SLtt_Screen_Cols - 1;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300193 browser->height = browser->rows = SLtt_Screen_Rows - 2;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300194 browser->y = 1;
195 browser->x = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300196}
197
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200198void ui_browser__handle_resize(struct ui_browser *browser)
199{
200 ui__refresh_dimensions(false);
201 ui_browser__show(browser, browser->title, ui_helpline__current);
202 ui_browser__refresh(browser);
203}
204
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200205int ui_browser__warning(struct ui_browser *browser, int timeout,
206 const char *format, ...)
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200207{
208 va_list args;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200209 char *text;
210 int key = 0, err;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200211
212 va_start(args, format);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200213 err = vasprintf(&text, format, args);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200214 va_end(args);
215
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200216 if (err < 0) {
217 va_start(args, format);
218 ui_helpline__vpush(format, args);
219 va_end(args);
220 } else {
zhangdianfang7f3e5082014-05-30 08:53:58 +0800221 while ((key = ui__question_window("Warning!", text,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200222 "Press any key...",
223 timeout)) == K_RESIZE)
224 ui_browser__handle_resize(browser);
225 free(text);
226 }
227
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200228 return key;
229}
230
231int ui_browser__help_window(struct ui_browser *browser, const char *text)
232{
233 int key;
234
235 while ((key = ui__help_window(text)) == K_RESIZE)
236 ui_browser__handle_resize(browser);
237
238 return key;
239}
240
241bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
242{
243 int key;
244
245 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
246 ui_browser__handle_resize(browser);
247
248 return key == K_ENTER || toupper(key) == 'Y';
249}
250
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300251void ui_browser__reset_index(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300252{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300253 browser->index = browser->top_idx = 0;
254 browser->seek(browser, 0, SEEK_SET);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300255}
256
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300257void __ui_browser__show_title(struct ui_browser *browser, const char *title)
258{
259 SLsmg_gotorc(0, 0);
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300260 ui_browser__set_color(browser, HE_COLORSET_ROOT);
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -0300261 ui_browser__write_nstring(browser, title, browser->width + 1);
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300262}
263
264void ui_browser__show_title(struct ui_browser *browser, const char *title)
265{
266 pthread_mutex_lock(&ui__lock);
267 __ui_browser__show_title(browser, title);
268 pthread_mutex_unlock(&ui__lock);
269}
270
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300271int ui_browser__show(struct ui_browser *browser, const char *title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300272 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300273{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300274 int err;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300275 va_list ap;
276
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300277 if (browser->refresh_dimensions == NULL)
278 browser->refresh_dimensions = ui_browser__refresh_dimensions;
279
280 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300281
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200282 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300283 __ui_browser__show_title(browser, title);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300284
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300285 browser->title = title;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300286 zfree(&browser->helpline);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300287
288 va_start(ap, helpline);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300289 err = vasprintf(&browser->helpline, helpline, ap);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300290 va_end(ap);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300291 if (err > 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300292 ui_helpline__push(browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200293 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300294 return err ? 0 : -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300295}
296
Arnaldo Carvalho de Melo3184c472013-12-19 16:25:17 -0300297void ui_browser__hide(struct ui_browser *browser)
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300298{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200299 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300300 ui_helpline__pop();
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300301 zfree(&browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200302 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300303}
304
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300305static void ui_browser__scrollbar_set(struct ui_browser *browser)
306{
307 int height = browser->height, h = 0, pct = 0,
308 col = browser->width,
Jiri Olsa89632972014-06-19 13:41:14 +0200309 row = 0;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300310
311 if (browser->nr_entries > 1) {
312 pct = ((browser->index * (browser->height - 1)) /
313 (browser->nr_entries - 1));
314 }
315
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200316 SLsmg_set_char_set(1);
317
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300318 while (h < height) {
319 ui_browser__gotorc(browser, row++, col);
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200320 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300321 ++h;
322 }
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200323
324 SLsmg_set_char_set(0);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300325}
326
327static int __ui_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300328{
329 int row;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200330 int width = browser->width;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300331
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300332 row = browser->refresh(browser);
333 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200334
335 if (!browser->use_navkeypressed || browser->navkeypressed)
336 ui_browser__scrollbar_set(browser);
337 else
338 width += 1;
339
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300340 SLsmg_fill_region(browser->y + row, browser->x,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200341 browser->height - row, width, ' ');
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300342
343 return 0;
344}
345
346int ui_browser__refresh(struct ui_browser *browser)
347{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200348 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300349 __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200350 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300351
352 return 0;
353}
354
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300355/*
356 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
357 * forget about any reference to any entry in the underlying data structure,
358 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
359 * after an output_resort and hist decay.
360 */
361void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
362{
363 off_t offset = nr_entries - browser->nr_entries;
364
365 browser->nr_entries = nr_entries;
366
367 if (offset < 0) {
368 if (browser->top_idx < (u64)-offset)
369 offset = -browser->top_idx;
370
371 browser->index += offset;
372 browser->top_idx += offset;
373 }
374
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300375 browser->top = NULL;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300376 browser->seek(browser, browser->top_idx, SEEK_SET);
377}
378
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300379int ui_browser__run(struct ui_browser *browser, int delay_secs)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300380{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300381 int err, key;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300382
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300383 while (1) {
384 off_t offset;
385
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300386 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300387 err = __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300388 SLsmg_refresh();
389 pthread_mutex_unlock(&ui__lock);
390 if (err < 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300391 break;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300392
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200393 key = ui__getch(delay_secs);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300394
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200395 if (key == K_RESIZE) {
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -0200396 ui__refresh_dimensions(false);
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300397 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300398 __ui_browser__show_title(browser, browser->title);
399 ui_helpline__puts(browser->helpline);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300400 continue;
401 }
402
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300403 if (browser->use_navkeypressed && !browser->navkeypressed) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200404 if (key == K_DOWN || key == K_UP ||
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300405 (browser->columns && (key == K_LEFT || key == K_RIGHT)) ||
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200406 key == K_PGDN || key == K_PGUP ||
407 key == K_HOME || key == K_END ||
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200408 key == ' ') {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300409 browser->navkeypressed = true;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200410 continue;
411 } else
412 return key;
413 }
414
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300415 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200416 case K_DOWN:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300417 if (browser->index == browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300418 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300419 ++browser->index;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300420 if (browser->index == browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300421 ++browser->top_idx;
422 browser->seek(browser, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300423 }
424 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200425 case K_UP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300426 if (browser->index == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300427 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300428 --browser->index;
429 if (browser->index < browser->top_idx) {
430 --browser->top_idx;
431 browser->seek(browser, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300432 }
433 break;
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300434 case K_RIGHT:
435 if (!browser->columns)
436 goto out;
437 if (browser->horiz_scroll < browser->columns - 1)
438 ++browser->horiz_scroll;
439 break;
440 case K_LEFT:
441 if (!browser->columns)
442 goto out;
443 if (browser->horiz_scroll != 0)
444 --browser->horiz_scroll;
445 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200446 case K_PGDN:
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300447 case ' ':
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300448 if (browser->top_idx + browser->rows > browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300449 break;
450
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300451 offset = browser->rows;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300452 if (browser->index + offset > browser->nr_entries - 1)
453 offset = browser->nr_entries - 1 - browser->index;
454 browser->index += offset;
455 browser->top_idx += offset;
456 browser->seek(browser, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300457 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200458 case K_PGUP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300459 if (browser->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300460 break;
461
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300462 if (browser->top_idx < browser->rows)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300463 offset = browser->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300464 else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300465 offset = browser->rows;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300466
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300467 browser->index -= offset;
468 browser->top_idx -= offset;
469 browser->seek(browser, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300470 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200471 case K_HOME:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300472 ui_browser__reset_index(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300473 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200474 case K_END:
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300475 offset = browser->rows - 1;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300476 if (offset >= browser->nr_entries)
477 offset = browser->nr_entries - 1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300478
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300479 browser->index = browser->nr_entries - 1;
480 browser->top_idx = browser->index - offset;
481 browser->seek(browser, -offset, SEEK_END);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300482 break;
483 default:
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300484 out:
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300485 return key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300486 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300487 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300488 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300489}
490
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300491unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300492{
493 struct list_head *pos;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300494 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300495 int row = 0;
496
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300497 if (browser->top == NULL || browser->top == browser->entries)
498 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300499
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300500 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300501
502 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300503 if (!browser->filter || !browser->filter(browser, pos)) {
504 ui_browser__gotorc(browser, row, 0);
505 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300506 if (++row == browser->rows)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300507 break;
508 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300509 }
510
511 return row;
512}
513
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300514static struct ui_browser_colorset {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200515 const char *name, *fg, *bg;
516 int colorset;
517} ui_browser__colorsets[] = {
518 {
519 .colorset = HE_COLORSET_TOP,
520 .name = "top",
521 .fg = "red",
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_MEDIUM,
526 .name = "medium",
527 .fg = "green",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200528 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200529 },
530 {
531 .colorset = HE_COLORSET_NORMAL,
532 .name = "normal",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200533 .fg = "default",
534 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200535 },
536 {
537 .colorset = HE_COLORSET_SELECTED,
538 .name = "selected",
539 .fg = "black",
Ingo Molnar8d7d3772015-03-05 11:32:13 +0100540 .bg = "yellow",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200541 },
542 {
Taeung Song78ce08d2016-01-08 17:16:11 +0900543 .colorset = HE_COLORSET_JUMP_ARROWS,
544 .name = "jump_arrows",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200545 .fg = "blue",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200546 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200547 },
548 {
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300549 .colorset = HE_COLORSET_ADDR,
550 .name = "addr",
551 .fg = "magenta",
552 .bg = "default",
553 },
554 {
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300555 .colorset = HE_COLORSET_ROOT,
556 .name = "root",
557 .fg = "white",
558 .bg = "blue",
559 },
560 {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200561 .name = NULL,
562 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300563};
564
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200565
566static int ui_browser__color_config(const char *var, const char *value,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300567 void *data __maybe_unused)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200568{
569 char *fg = NULL, *bg;
570 int i;
571
572 /* same dir for all commands */
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300573 if (!strstarts(var, "colors.") != 0)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200574 return 0;
575
576 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
577 const char *name = var + 7;
578
579 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
580 continue;
581
582 fg = strdup(value);
583 if (fg == NULL)
584 break;
585
586 bg = strchr(fg, ',');
587 if (bg == NULL)
588 break;
589
590 *bg = '\0';
Taeung Songe21600f2017-04-07 23:24:19 +0900591 bg = ltrim(++bg);
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200592 ui_browser__colorsets[i].bg = bg;
593 ui_browser__colorsets[i].fg = fg;
594 return 0;
595 }
596
597 free(fg);
598 return -1;
599}
600
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -0200601void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
602{
603 switch (whence) {
604 case SEEK_SET:
605 browser->top = browser->entries;
606 break;
607 case SEEK_CUR:
608 browser->top = browser->top + browser->top_idx + offset;
609 break;
610 case SEEK_END:
Arnaldo Carvalho de Melo48d038f2013-11-14 15:30:41 -0300611 browser->top = browser->top + browser->nr_entries - 1 + offset;
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -0200612 break;
613 default:
614 return;
615 }
616}
617
618unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
619{
620 unsigned int row = 0, idx = browser->top_idx;
621 char **pos;
622
623 if (browser->top == NULL)
624 browser->top = browser->entries;
625
626 pos = (char **)browser->top;
627 while (idx < browser->nr_entries) {
628 if (!browser->filter || !browser->filter(browser, *pos)) {
629 ui_browser__gotorc(browser, row, 0);
630 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300631 if (++row == browser->rows)
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -0200632 break;
633 }
634
635 ++idx;
636 ++pos;
637 }
638
639 return row;
640}
641
Arnaldo Carvalho de Melo4656cca2012-05-03 13:07:05 -0300642void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
643 u16 start, u16 end)
644{
645 SLsmg_set_char_set(1);
646 ui_browser__gotorc(browser, start, column);
647 SLsmg_draw_vline(end - start + 1);
648 SLsmg_set_char_set(0);
649}
650
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300651void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
652 int graph)
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300653{
654 SLsmg_set_char_set(1);
655 SLsmg_write_char(graph);
656 SLsmg_set_char_set(0);
657}
658
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300659static void __ui_browser__line_arrow_up(struct ui_browser *browser,
660 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300661 u64 start, u64 end)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300662{
663 unsigned int row, end_row;
664
665 SLsmg_set_char_set(1);
666
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300667 if (start < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300668 row = start - browser->top_idx;
669 ui_browser__gotorc(browser, row, column);
670 SLsmg_write_char(SLSMG_LLCORN_CHAR);
671 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300672 SLsmg_draw_hline(2);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300673
674 if (row-- == 0)
675 goto out;
676 } else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300677 row = browser->rows - 1;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300678
679 if (end > browser->top_idx)
680 end_row = end - browser->top_idx;
681 else
682 end_row = 0;
683
684 ui_browser__gotorc(browser, end_row, column);
685 SLsmg_draw_vline(row - end_row + 1);
686
687 ui_browser__gotorc(browser, end_row, column);
688 if (end >= browser->top_idx) {
689 SLsmg_write_char(SLSMG_ULCORN_CHAR);
690 ui_browser__gotorc(browser, end_row, column + 1);
691 SLsmg_write_char(SLSMG_HLINE_CHAR);
692 ui_browser__gotorc(browser, end_row, column + 2);
693 SLsmg_write_char(SLSMG_RARROW_CHAR);
694 }
695out:
696 SLsmg_set_char_set(0);
697}
698
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300699static void __ui_browser__line_arrow_down(struct ui_browser *browser,
700 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 unsigned int row, end_row;
704
705 SLsmg_set_char_set(1);
706
707 if (start >= browser->top_idx) {
708 row = start - browser->top_idx;
709 ui_browser__gotorc(browser, row, column);
710 SLsmg_write_char(SLSMG_ULCORN_CHAR);
711 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300712 SLsmg_draw_hline(2);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300713
Jin Yao80f62582017-06-08 14:01:44 +0800714 if (++row == 0)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300715 goto out;
716 } else
717 row = 0;
718
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300719 if (end >= browser->top_idx + browser->rows)
720 end_row = browser->rows - 1;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300721 else
Davidlohr Buesob2222132013-11-12 22:24:24 -0800722 end_row = end - browser->top_idx;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300723
724 ui_browser__gotorc(browser, row, column);
725 SLsmg_draw_vline(end_row - row + 1);
726
727 ui_browser__gotorc(browser, end_row, column);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300728 if (end < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300729 SLsmg_write_char(SLSMG_LLCORN_CHAR);
730 ui_browser__gotorc(browser, end_row, column + 1);
731 SLsmg_write_char(SLSMG_HLINE_CHAR);
732 ui_browser__gotorc(browser, end_row, column + 2);
733 SLsmg_write_char(SLSMG_RARROW_CHAR);
734 }
735out:
736 SLsmg_set_char_set(0);
737}
738
739void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300740 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300741{
742 if (start > end)
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300743 __ui_browser__line_arrow_up(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300744 else
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300745 __ui_browser__line_arrow_down(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300746}
747
Jin Yao7e63a132017-07-07 13:06:35 +0800748void ui_browser__mark_fused(struct ui_browser *browser, unsigned int column,
749 unsigned int row, bool arrow_down)
750{
751 unsigned int end_row;
752
753 if (row >= browser->top_idx)
754 end_row = row - browser->top_idx;
755 else
756 return;
757
758 SLsmg_set_char_set(1);
759
760 if (arrow_down) {
761 ui_browser__gotorc(browser, end_row, column - 1);
762 SLsmg_write_char(SLSMG_ULCORN_CHAR);
763 ui_browser__gotorc(browser, end_row, column);
764 SLsmg_draw_hline(2);
765 ui_browser__gotorc(browser, end_row + 1, column - 1);
766 SLsmg_write_char(SLSMG_LTEE_CHAR);
767 } else {
768 ui_browser__gotorc(browser, end_row, column - 1);
769 SLsmg_write_char(SLSMG_LTEE_CHAR);
770 ui_browser__gotorc(browser, end_row, column);
771 SLsmg_draw_hline(2);
772 }
773
774 SLsmg_set_char_set(0);
775}
776
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300777void ui_browser__init(void)
778{
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200779 int i = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300780
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200781 perf_config(ui_browser__color_config, NULL);
782
783 while (ui_browser__colorsets[i].name) {
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300784 struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200785 sltt_set_color(c->colorset, c->name, c->fg, c->bg);
786 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300787}