blob: 4f75561424eda58ad554046ec7979924f9c7bc0d [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 Meloef9ff602018-04-02 15:48:18 -030048void ui_browser__gotorc_title(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 Meloef9ff602018-04-02 15:48:18 -030053void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
54{
55 SLsmg_gotorc(browser->y + y + browser->extra_title_lines, browser->x + x);
56}
57
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -030058void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg,
59 unsigned int width)
60{
61 slsmg_write_nstring(msg, width);
62}
63
Arnaldo Carvalho de Meloc5220242018-03-15 17:52:53 -030064void ui_browser__vprintf(struct ui_browser *browser __maybe_unused, const char *fmt, va_list args)
65{
66 slsmg_vprintf(fmt, args);
67}
68
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -030069void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...)
70{
71 va_list args;
72
73 va_start(args, fmt);
Arnaldo Carvalho de Meloc5220242018-03-15 17:52:53 -030074 ui_browser__vprintf(browser, fmt, args);
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -030075 va_end(args);
76}
77
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030078static struct list_head *
79ui_browser__list_head_filter_entries(struct ui_browser *browser,
80 struct list_head *pos)
81{
82 do {
83 if (!browser->filter || !browser->filter(browser, pos))
84 return pos;
85 pos = pos->next;
86 } while (pos != browser->entries);
87
88 return NULL;
89}
90
91static struct list_head *
92ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
93 struct list_head *pos)
94{
95 do {
96 if (!browser->filter || !browser->filter(browser, pos))
97 return pos;
98 pos = pos->prev;
99 } while (pos != browser->entries);
100
101 return NULL;
102}
103
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300104void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300105{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300106 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300107 struct list_head *pos;
108
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300109 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300110 return;
111
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300112 switch (whence) {
113 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300114 pos = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300115 break;
116 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300117 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300118 break;
119 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300120 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300121 break;
122 default:
123 return;
124 }
125
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300126 assert(pos != NULL);
127
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300128 if (offset > 0) {
129 while (offset-- != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300130 pos = ui_browser__list_head_filter_entries(browser, pos->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300131 } else {
132 while (offset++ != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300133 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300134 }
135
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300136 browser->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300137}
138
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300139void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300140{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300141 struct rb_root *root = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300142 struct rb_node *nd;
143
144 switch (whence) {
145 case SEEK_SET:
146 nd = rb_first(root);
147 break;
148 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300149 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300150 break;
151 case SEEK_END:
152 nd = rb_last(root);
153 break;
154 default:
155 return;
156 }
157
158 if (offset > 0) {
159 while (offset-- != 0)
160 nd = rb_next(nd);
161 } else {
162 while (offset++ != 0)
163 nd = rb_prev(nd);
164 }
165
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300166 browser->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300167}
168
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300169unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300170{
171 struct rb_node *nd;
172 int row = 0;
173
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300174 if (browser->top == NULL)
175 browser->top = rb_first(browser->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300176
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300177 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300178
179 while (nd != NULL) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300180 ui_browser__gotorc(browser, row, 0);
181 browser->write(browser, nd, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300182 if (++row == browser->rows)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300183 break;
184 nd = rb_next(nd);
185 }
186
187 return row;
188}
189
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300190bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300191{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300192 return browser->top_idx + row == browser->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300193}
194
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300195void ui_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300196{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300197 browser->width = SLtt_Screen_Cols - 1;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300198 browser->height = browser->rows = SLtt_Screen_Rows - 2;
Arnaldo Carvalho de Meloef9ff602018-04-02 15:48:18 -0300199 browser->rows -= browser->extra_title_lines;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300200 browser->y = 1;
201 browser->x = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300202}
203
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200204void ui_browser__handle_resize(struct ui_browser *browser)
205{
206 ui__refresh_dimensions(false);
207 ui_browser__show(browser, browser->title, ui_helpline__current);
208 ui_browser__refresh(browser);
209}
210
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200211int ui_browser__warning(struct ui_browser *browser, int timeout,
212 const char *format, ...)
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200213{
214 va_list args;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200215 char *text;
216 int key = 0, err;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200217
218 va_start(args, format);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200219 err = vasprintf(&text, format, args);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200220 va_end(args);
221
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200222 if (err < 0) {
223 va_start(args, format);
224 ui_helpline__vpush(format, args);
225 va_end(args);
226 } else {
zhangdianfang7f3e5082014-05-30 08:53:58 +0800227 while ((key = ui__question_window("Warning!", text,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200228 "Press any key...",
229 timeout)) == K_RESIZE)
230 ui_browser__handle_resize(browser);
231 free(text);
232 }
233
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200234 return key;
235}
236
237int ui_browser__help_window(struct ui_browser *browser, const char *text)
238{
239 int key;
240
241 while ((key = ui__help_window(text)) == K_RESIZE)
242 ui_browser__handle_resize(browser);
243
244 return key;
245}
246
247bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
248{
249 int key;
250
251 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
252 ui_browser__handle_resize(browser);
253
254 return key == K_ENTER || toupper(key) == 'Y';
255}
256
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300257void ui_browser__reset_index(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300258{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300259 browser->index = browser->top_idx = 0;
260 browser->seek(browser, 0, SEEK_SET);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300261}
262
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300263void __ui_browser__show_title(struct ui_browser *browser, const char *title)
264{
265 SLsmg_gotorc(0, 0);
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300266 ui_browser__set_color(browser, HE_COLORSET_ROOT);
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -0300267 ui_browser__write_nstring(browser, title, browser->width + 1);
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300268}
269
270void ui_browser__show_title(struct ui_browser *browser, const char *title)
271{
272 pthread_mutex_lock(&ui__lock);
273 __ui_browser__show_title(browser, title);
274 pthread_mutex_unlock(&ui__lock);
275}
276
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300277int ui_browser__show(struct ui_browser *browser, const char *title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300278 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300279{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300280 int err;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300281 va_list ap;
282
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300283 if (browser->refresh_dimensions == NULL)
284 browser->refresh_dimensions = ui_browser__refresh_dimensions;
285
286 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300287
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200288 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300289 __ui_browser__show_title(browser, title);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300290
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300291 browser->title = title;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300292 zfree(&browser->helpline);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300293
294 va_start(ap, helpline);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300295 err = vasprintf(&browser->helpline, helpline, ap);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300296 va_end(ap);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300297 if (err > 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300298 ui_helpline__push(browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200299 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300300 return err ? 0 : -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300301}
302
Arnaldo Carvalho de Melo3184c472013-12-19 16:25:17 -0300303void ui_browser__hide(struct ui_browser *browser)
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300304{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200305 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300306 ui_helpline__pop();
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300307 zfree(&browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200308 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300309}
310
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300311static void ui_browser__scrollbar_set(struct ui_browser *browser)
312{
313 int height = browser->height, h = 0, pct = 0,
314 col = browser->width,
Jiri Olsa89632972014-06-19 13:41:14 +0200315 row = 0;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300316
317 if (browser->nr_entries > 1) {
318 pct = ((browser->index * (browser->height - 1)) /
319 (browser->nr_entries - 1));
320 }
321
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200322 SLsmg_set_char_set(1);
323
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300324 while (h < height) {
325 ui_browser__gotorc(browser, row++, col);
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200326 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300327 ++h;
328 }
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200329
330 SLsmg_set_char_set(0);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300331}
332
333static int __ui_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300334{
335 int row;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200336 int width = browser->width;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300337
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300338 row = browser->refresh(browser);
339 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200340
341 if (!browser->use_navkeypressed || browser->navkeypressed)
342 ui_browser__scrollbar_set(browser);
343 else
344 width += 1;
345
Arnaldo Carvalho de Melocaf61de2018-04-05 11:43:38 -0300346 SLsmg_fill_region(browser->y + row + browser->extra_title_lines, browser->x,
347 browser->rows - row, width, ' ');
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300348
349 return 0;
350}
351
352int ui_browser__refresh(struct ui_browser *browser)
353{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200354 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300355 __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200356 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300357
358 return 0;
359}
360
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300361/*
362 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
363 * forget about any reference to any entry in the underlying data structure,
364 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
365 * after an output_resort and hist decay.
366 */
367void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
368{
369 off_t offset = nr_entries - browser->nr_entries;
370
371 browser->nr_entries = nr_entries;
372
373 if (offset < 0) {
374 if (browser->top_idx < (u64)-offset)
375 offset = -browser->top_idx;
376
377 browser->index += offset;
378 browser->top_idx += offset;
379 }
380
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300381 browser->top = NULL;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300382 browser->seek(browser, browser->top_idx, SEEK_SET);
383}
384
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300385int ui_browser__run(struct ui_browser *browser, int delay_secs)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300386{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300387 int err, key;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300388
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300389 while (1) {
390 off_t offset;
391
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300392 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300393 err = __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300394 SLsmg_refresh();
395 pthread_mutex_unlock(&ui__lock);
396 if (err < 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300397 break;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300398
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200399 key = ui__getch(delay_secs);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300400
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200401 if (key == K_RESIZE) {
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -0200402 ui__refresh_dimensions(false);
Arnaldo Carvalho de Melofa70b5d2014-07-01 16:34:42 -0300403 browser->refresh_dimensions(browser);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300404 __ui_browser__show_title(browser, browser->title);
405 ui_helpline__puts(browser->helpline);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300406 continue;
407 }
408
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300409 if (browser->use_navkeypressed && !browser->navkeypressed) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200410 if (key == K_DOWN || key == K_UP ||
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300411 (browser->columns && (key == K_LEFT || key == K_RIGHT)) ||
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200412 key == K_PGDN || key == K_PGUP ||
413 key == K_HOME || key == K_END ||
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200414 key == ' ') {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300415 browser->navkeypressed = true;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200416 continue;
417 } else
418 return key;
419 }
420
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300421 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200422 case K_DOWN:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300423 if (browser->index == browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300424 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300425 ++browser->index;
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300426 if (browser->index == browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300427 ++browser->top_idx;
428 browser->seek(browser, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300429 }
430 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200431 case K_UP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300432 if (browser->index == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300433 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300434 --browser->index;
435 if (browser->index < browser->top_idx) {
436 --browser->top_idx;
437 browser->seek(browser, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300438 }
439 break;
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300440 case K_RIGHT:
441 if (!browser->columns)
442 goto out;
443 if (browser->horiz_scroll < browser->columns - 1)
444 ++browser->horiz_scroll;
445 break;
446 case K_LEFT:
447 if (!browser->columns)
448 goto out;
449 if (browser->horiz_scroll != 0)
450 --browser->horiz_scroll;
451 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200452 case K_PGDN:
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300453 case ' ':
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300454 if (browser->top_idx + browser->rows > browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300455 break;
456
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300457 offset = browser->rows;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300458 if (browser->index + offset > browser->nr_entries - 1)
459 offset = browser->nr_entries - 1 - browser->index;
460 browser->index += offset;
461 browser->top_idx += offset;
462 browser->seek(browser, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300463 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200464 case K_PGUP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300465 if (browser->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300466 break;
467
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300468 if (browser->top_idx < browser->rows)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300469 offset = browser->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300470 else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300471 offset = browser->rows;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300472
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300473 browser->index -= offset;
474 browser->top_idx -= offset;
475 browser->seek(browser, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300476 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200477 case K_HOME:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300478 ui_browser__reset_index(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300479 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200480 case K_END:
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300481 offset = browser->rows - 1;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300482 if (offset >= browser->nr_entries)
483 offset = browser->nr_entries - 1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300484
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300485 browser->index = browser->nr_entries - 1;
486 browser->top_idx = browser->index - offset;
487 browser->seek(browser, -offset, SEEK_END);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300488 break;
489 default:
Arnaldo Carvalho de Melofaae6f62015-08-11 17:14:40 -0300490 out:
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300491 return key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300492 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300493 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300494 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300495}
496
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300497unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300498{
499 struct list_head *pos;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300500 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300501 int row = 0;
502
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300503 if (browser->top == NULL || browser->top == browser->entries)
504 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300505
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300506 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300507
508 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300509 if (!browser->filter || !browser->filter(browser, pos)) {
510 ui_browser__gotorc(browser, row, 0);
511 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300512 if (++row == browser->rows)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300513 break;
514 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300515 }
516
517 return row;
518}
519
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300520static struct ui_browser_colorset {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200521 const char *name, *fg, *bg;
522 int colorset;
523} ui_browser__colorsets[] = {
524 {
525 .colorset = HE_COLORSET_TOP,
526 .name = "top",
527 .fg = "red",
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_MEDIUM,
532 .name = "medium",
533 .fg = "green",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200534 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200535 },
536 {
537 .colorset = HE_COLORSET_NORMAL,
538 .name = "normal",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200539 .fg = "default",
540 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200541 },
542 {
543 .colorset = HE_COLORSET_SELECTED,
544 .name = "selected",
545 .fg = "black",
Ingo Molnar8d7d3772015-03-05 11:32:13 +0100546 .bg = "yellow",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200547 },
548 {
Taeung Song78ce08d2016-01-08 17:16:11 +0900549 .colorset = HE_COLORSET_JUMP_ARROWS,
550 .name = "jump_arrows",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200551 .fg = "blue",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200552 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200553 },
554 {
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300555 .colorset = HE_COLORSET_ADDR,
556 .name = "addr",
557 .fg = "magenta",
558 .bg = "default",
559 },
560 {
Arnaldo Carvalho de Melo6692c262013-03-28 11:34:10 -0300561 .colorset = HE_COLORSET_ROOT,
562 .name = "root",
563 .fg = "white",
564 .bg = "blue",
565 },
566 {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200567 .name = NULL,
568 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300569};
570
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200571
572static int ui_browser__color_config(const char *var, const char *value,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300573 void *data __maybe_unused)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200574{
575 char *fg = NULL, *bg;
576 int i;
577
578 /* same dir for all commands */
Arnaldo Carvalho de Melo8e99b6d2017-07-20 15:27:39 -0300579 if (!strstarts(var, "colors.") != 0)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200580 return 0;
581
582 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
583 const char *name = var + 7;
584
585 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
586 continue;
587
588 fg = strdup(value);
589 if (fg == NULL)
590 break;
591
592 bg = strchr(fg, ',');
593 if (bg == NULL)
594 break;
595
596 *bg = '\0';
Taeung Songe21600f2017-04-07 23:24:19 +0900597 bg = ltrim(++bg);
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200598 ui_browser__colorsets[i].bg = bg;
599 ui_browser__colorsets[i].fg = fg;
600 return 0;
601 }
602
603 free(fg);
604 return -1;
605}
606
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200607void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
608{
609 switch (whence) {
610 case SEEK_SET:
611 browser->top = browser->entries;
612 break;
613 case SEEK_CUR:
614 browser->top = browser->top + browser->top_idx + offset;
615 break;
616 case SEEK_END:
Arnaldo Carvalho de Melo48d038f2013-11-14 15:30:41 -0300617 browser->top = browser->top + browser->nr_entries - 1 + offset;
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200618 break;
619 default:
620 return;
621 }
622}
623
624unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
625{
626 unsigned int row = 0, idx = browser->top_idx;
627 char **pos;
628
629 if (browser->top == NULL)
630 browser->top = browser->entries;
631
632 pos = (char **)browser->top;
633 while (idx < browser->nr_entries) {
634 if (!browser->filter || !browser->filter(browser, *pos)) {
635 ui_browser__gotorc(browser, row, 0);
636 browser->write(browser, pos, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300637 if (++row == browser->rows)
Arnaldo Carvalho de Melo1056d3dd2011-10-26 07:11:03 -0200638 break;
639 }
640
641 ++idx;
642 ++pos;
643 }
644
645 return row;
646}
647
Arnaldo Carvalho de Melo4656cca2012-05-03 13:07:05 -0300648void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
649 u16 start, u16 end)
650{
651 SLsmg_set_char_set(1);
652 ui_browser__gotorc(browser, start, column);
653 SLsmg_draw_vline(end - start + 1);
654 SLsmg_set_char_set(0);
655}
656
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300657void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
658 int graph)
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300659{
660 SLsmg_set_char_set(1);
661 SLsmg_write_char(graph);
662 SLsmg_set_char_set(0);
663}
664
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300665static void __ui_browser__line_arrow_up(struct ui_browser *browser,
666 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300667 u64 start, u64 end)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300668{
669 unsigned int row, end_row;
670
671 SLsmg_set_char_set(1);
672
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300673 if (start < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300674 row = start - browser->top_idx;
675 ui_browser__gotorc(browser, row, column);
676 SLsmg_write_char(SLSMG_LLCORN_CHAR);
677 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300678 SLsmg_draw_hline(2);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300679
680 if (row-- == 0)
681 goto out;
682 } else
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300683 row = browser->rows - 1;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300684
685 if (end > browser->top_idx)
686 end_row = end - browser->top_idx;
687 else
688 end_row = 0;
689
690 ui_browser__gotorc(browser, end_row, column);
691 SLsmg_draw_vline(row - end_row + 1);
692
693 ui_browser__gotorc(browser, end_row, column);
694 if (end >= browser->top_idx) {
695 SLsmg_write_char(SLSMG_ULCORN_CHAR);
696 ui_browser__gotorc(browser, end_row, column + 1);
697 SLsmg_write_char(SLSMG_HLINE_CHAR);
698 ui_browser__gotorc(browser, end_row, column + 2);
699 SLsmg_write_char(SLSMG_RARROW_CHAR);
700 }
701out:
702 SLsmg_set_char_set(0);
703}
704
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300705static void __ui_browser__line_arrow_down(struct ui_browser *browser,
706 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300707 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300708{
709 unsigned int row, end_row;
710
711 SLsmg_set_char_set(1);
712
713 if (start >= browser->top_idx) {
714 row = start - browser->top_idx;
715 ui_browser__gotorc(browser, row, column);
716 SLsmg_write_char(SLSMG_ULCORN_CHAR);
717 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300718 SLsmg_draw_hline(2);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300719
Jin Yao80f62582017-06-08 14:01:44 +0800720 if (++row == 0)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300721 goto out;
722 } else
723 row = 0;
724
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300725 if (end >= browser->top_idx + browser->rows)
726 end_row = browser->rows - 1;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300727 else
Davidlohr Buesob2222132013-11-12 22:24:24 -0800728 end_row = end - browser->top_idx;
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300729
730 ui_browser__gotorc(browser, row, column);
731 SLsmg_draw_vline(end_row - row + 1);
732
733 ui_browser__gotorc(browser, end_row, column);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300734 if (end < browser->top_idx + browser->rows) {
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300735 SLsmg_write_char(SLSMG_LLCORN_CHAR);
736 ui_browser__gotorc(browser, end_row, column + 1);
737 SLsmg_write_char(SLSMG_HLINE_CHAR);
738 ui_browser__gotorc(browser, end_row, column + 2);
739 SLsmg_write_char(SLSMG_RARROW_CHAR);
740 }
741out:
742 SLsmg_set_char_set(0);
743}
744
745void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300746 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300747{
748 if (start > end)
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300749 __ui_browser__line_arrow_up(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300750 else
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300751 __ui_browser__line_arrow_down(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300752}
753
Jin Yao7e63a132017-07-07 13:06:35 +0800754void ui_browser__mark_fused(struct ui_browser *browser, unsigned int column,
755 unsigned int row, bool arrow_down)
756{
757 unsigned int end_row;
758
759 if (row >= browser->top_idx)
760 end_row = row - browser->top_idx;
761 else
762 return;
763
764 SLsmg_set_char_set(1);
765
766 if (arrow_down) {
767 ui_browser__gotorc(browser, end_row, column - 1);
768 SLsmg_write_char(SLSMG_ULCORN_CHAR);
769 ui_browser__gotorc(browser, end_row, column);
770 SLsmg_draw_hline(2);
771 ui_browser__gotorc(browser, end_row + 1, column - 1);
772 SLsmg_write_char(SLSMG_LTEE_CHAR);
773 } else {
774 ui_browser__gotorc(browser, end_row, column - 1);
775 SLsmg_write_char(SLSMG_LTEE_CHAR);
776 ui_browser__gotorc(browser, end_row, column);
777 SLsmg_draw_hline(2);
778 }
779
780 SLsmg_set_char_set(0);
781}
782
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300783void ui_browser__init(void)
784{
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200785 int i = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300786
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200787 perf_config(ui_browser__color_config, NULL);
788
789 while (ui_browser__colorsets[i].name) {
Arnaldo Carvalho de Melo7c3102b2013-01-18 16:55:52 -0300790 struct ui_browser_colorset *c = &ui_browser__colorsets[i++];
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200791 sltt_set_color(c->colorset, c->name, c->fg, c->bg);
792 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300793}