blob: 4aeb7d5df93933fd8babfafaadd47c755cbb9671 [file] [log] [blame]
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001#include "../util.h"
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -02002#include "../cache.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03003#include "../../perf.h"
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -03004#include "libslang.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03005#include <newt.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>
11#include <stdlib.h>
12#include <sys/ttydefaults.h>
13#include "browser.h"
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030014#include "helpline.h"
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -020015#include "keysyms.h"
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030016#include "../color.h"
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -030017
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020018static int ui_browser__percent_color(struct ui_browser *browser,
19 double percent, bool current)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030020{
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020021 if (current && (!browser->use_navkeypressed || browser->navkeypressed))
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030022 return HE_COLORSET_SELECTED;
23 if (percent >= MIN_RED)
24 return HE_COLORSET_TOP;
25 if (percent >= MIN_GREEN)
26 return HE_COLORSET_MEDIUM;
27 return HE_COLORSET_NORMAL;
28}
29
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030030int ui_browser__set_color(struct ui_browser *browser, int color)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030031{
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030032 int ret = browser->current_color;
33 browser->current_color = color;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030034 SLsmg_set_color(color);
Arnaldo Carvalho de Melo08709162012-04-02 12:48:56 -030035 return ret;
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030036}
37
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030038void ui_browser__set_percent_color(struct ui_browser *browser,
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030039 double percent, bool current)
40{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030041 int color = ui_browser__percent_color(browser, percent, current);
42 ui_browser__set_color(browser, color);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030043}
44
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030045void ui_browser__gotorc(struct ui_browser *browser, int y, int x)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030046{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030047 SLsmg_gotorc(browser->y + y, browser->x + x);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030048}
49
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030050static struct list_head *
51ui_browser__list_head_filter_entries(struct ui_browser *browser,
52 struct list_head *pos)
53{
54 do {
55 if (!browser->filter || !browser->filter(browser, pos))
56 return pos;
57 pos = pos->next;
58 } while (pos != browser->entries);
59
60 return NULL;
61}
62
63static struct list_head *
64ui_browser__list_head_filter_prev_entries(struct ui_browser *browser,
65 struct list_head *pos)
66{
67 do {
68 if (!browser->filter || !browser->filter(browser, pos))
69 return pos;
70 pos = pos->prev;
71 } while (pos != browser->entries);
72
73 return NULL;
74}
75
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030076void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030077{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030078 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030079 struct list_head *pos;
80
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030081 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030082 return;
83
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030084 switch (whence) {
85 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030086 pos = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030087 break;
88 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030089 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030090 break;
91 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030092 pos = ui_browser__list_head_filter_prev_entries(browser, head->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -030093 break;
94 default:
95 return;
96 }
97
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -030098 assert(pos != NULL);
99
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300100 if (offset > 0) {
101 while (offset-- != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300102 pos = ui_browser__list_head_filter_entries(browser, pos->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300103 } else {
104 while (offset++ != 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300105 pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300106 }
107
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300108 browser->top = pos;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300109}
110
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300111void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300112{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300113 struct rb_root *root = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300114 struct rb_node *nd;
115
116 switch (whence) {
117 case SEEK_SET:
118 nd = rb_first(root);
119 break;
120 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300121 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300122 break;
123 case SEEK_END:
124 nd = rb_last(root);
125 break;
126 default:
127 return;
128 }
129
130 if (offset > 0) {
131 while (offset-- != 0)
132 nd = rb_next(nd);
133 } else {
134 while (offset++ != 0)
135 nd = rb_prev(nd);
136 }
137
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300138 browser->top = nd;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300139}
140
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300141unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300142{
143 struct rb_node *nd;
144 int row = 0;
145
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300146 if (browser->top == NULL)
147 browser->top = rb_first(browser->entries);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300148
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300149 nd = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300150
151 while (nd != NULL) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300152 ui_browser__gotorc(browser, row, 0);
153 browser->write(browser, nd, row);
154 if (++row == browser->height)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300155 break;
156 nd = rb_next(nd);
157 }
158
159 return row;
160}
161
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300162bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300163{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300164 return browser->top_idx + row == browser->index;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300165}
166
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300167void ui_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300168{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300169 browser->width = SLtt_Screen_Cols - 1;
170 browser->height = SLtt_Screen_Rows - 2;
171 browser->y = 1;
172 browser->x = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300173}
174
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200175void ui_browser__handle_resize(struct ui_browser *browser)
176{
177 ui__refresh_dimensions(false);
178 ui_browser__show(browser, browser->title, ui_helpline__current);
179 ui_browser__refresh(browser);
180}
181
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200182int ui_browser__warning(struct ui_browser *browser, int timeout,
183 const char *format, ...)
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200184{
185 va_list args;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200186 char *text;
187 int key = 0, err;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200188
189 va_start(args, format);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200190 err = vasprintf(&text, format, args);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200191 va_end(args);
192
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200193 if (err < 0) {
194 va_start(args, format);
195 ui_helpline__vpush(format, args);
196 va_end(args);
197 } else {
198 while ((key == ui__question_window("Warning!", text,
199 "Press any key...",
200 timeout)) == K_RESIZE)
201 ui_browser__handle_resize(browser);
202 free(text);
203 }
204
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200205 return key;
206}
207
208int ui_browser__help_window(struct ui_browser *browser, const char *text)
209{
210 int key;
211
212 while ((key = ui__help_window(text)) == K_RESIZE)
213 ui_browser__handle_resize(browser);
214
215 return key;
216}
217
218bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text)
219{
220 int key;
221
222 while ((key = ui__dialog_yesno(text)) == K_RESIZE)
223 ui_browser__handle_resize(browser);
224
225 return key == K_ENTER || toupper(key) == 'Y';
226}
227
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300228void ui_browser__reset_index(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300229{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300230 browser->index = browser->top_idx = 0;
231 browser->seek(browser, 0, SEEK_SET);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300232}
233
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300234void __ui_browser__show_title(struct ui_browser *browser, const char *title)
235{
236 SLsmg_gotorc(0, 0);
237 ui_browser__set_color(browser, NEWT_COLORSET_ROOT);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300238 slsmg_write_nstring(title, browser->width + 1);
Arnaldo Carvalho de Melob210b3b2011-02-25 11:33:31 -0300239}
240
241void ui_browser__show_title(struct ui_browser *browser, const char *title)
242{
243 pthread_mutex_lock(&ui__lock);
244 __ui_browser__show_title(browser, title);
245 pthread_mutex_unlock(&ui__lock);
246}
247
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300248int ui_browser__show(struct ui_browser *browser, const char *title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300249 const char *helpline, ...)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300250{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300251 int err;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300252 va_list ap;
253
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300254 ui_browser__refresh_dimensions(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300255
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200256 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300257 __ui_browser__show_title(browser, title);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300258
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300259 browser->title = title;
260 free(browser->helpline);
261 browser->helpline = NULL;
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300262
263 va_start(ap, helpline);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300264 err = vasprintf(&browser->helpline, helpline, ap);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300265 va_end(ap);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300266 if (err > 0)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300267 ui_helpline__push(browser->helpline);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200268 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300269 return err ? 0 : -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300270}
271
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300272void ui_browser__hide(struct ui_browser *browser __maybe_unused)
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300273{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200274 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300275 ui_helpline__pop();
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200276 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300277}
278
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300279static void ui_browser__scrollbar_set(struct ui_browser *browser)
280{
281 int height = browser->height, h = 0, pct = 0,
282 col = browser->width,
283 row = browser->y - 1;
284
285 if (browser->nr_entries > 1) {
286 pct = ((browser->index * (browser->height - 1)) /
287 (browser->nr_entries - 1));
288 }
289
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200290 SLsmg_set_char_set(1);
291
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300292 while (h < height) {
293 ui_browser__gotorc(browser, row++, col);
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200294 SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300295 ++h;
296 }
Arnaldo Carvalho de Melo04581222011-10-26 08:19:05 -0200297
298 SLsmg_set_char_set(0);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300299}
300
301static int __ui_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300302{
303 int row;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200304 int width = browser->width;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300305
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300306 row = browser->refresh(browser);
307 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200308
309 if (!browser->use_navkeypressed || browser->navkeypressed)
310 ui_browser__scrollbar_set(browser);
311 else
312 width += 1;
313
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300314 SLsmg_fill_region(browser->y + row, browser->x,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200315 browser->height - row, width, ' ');
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300316
317 return 0;
318}
319
320int ui_browser__refresh(struct ui_browser *browser)
321{
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200322 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300323 __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -0200324 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300325
326 return 0;
327}
328
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300329/*
330 * Here we're updating nr_entries _after_ we started browsing, i.e. we have to
331 * forget about any reference to any entry in the underlying data structure,
332 * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser
333 * after an output_resort and hist decay.
334 */
335void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries)
336{
337 off_t offset = nr_entries - browser->nr_entries;
338
339 browser->nr_entries = nr_entries;
340
341 if (offset < 0) {
342 if (browser->top_idx < (u64)-offset)
343 offset = -browser->top_idx;
344
345 browser->index += offset;
346 browser->top_idx += offset;
347 }
348
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300349 browser->top = NULL;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300350 browser->seek(browser, browser->top_idx, SEEK_SET);
351}
352
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300353int ui_browser__run(struct ui_browser *browser, int delay_secs)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300354{
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300355 int err, key;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300356
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300357 while (1) {
358 off_t offset;
359
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300360 pthread_mutex_lock(&ui__lock);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300361 err = __ui_browser__refresh(browser);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300362 SLsmg_refresh();
363 pthread_mutex_unlock(&ui__lock);
364 if (err < 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300365 break;
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300366
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200367 key = ui__getch(delay_secs);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300368
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200369 if (key == K_RESIZE) {
Arnaldo Carvalho de Melo71172ed2011-10-25 13:45:16 -0200370 ui__refresh_dimensions(false);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300371 ui_browser__refresh_dimensions(browser);
372 __ui_browser__show_title(browser, browser->title);
373 ui_helpline__puts(browser->helpline);
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300374 continue;
375 }
376
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300377 if (browser->use_navkeypressed && !browser->navkeypressed) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200378 if (key == K_DOWN || key == K_UP ||
379 key == K_PGDN || key == K_PGUP ||
380 key == K_HOME || key == K_END ||
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200381 key == ' ') {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300382 browser->navkeypressed = true;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200383 continue;
384 } else
385 return key;
386 }
387
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300388 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200389 case K_DOWN:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300390 if (browser->index == browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300391 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300392 ++browser->index;
393 if (browser->index == browser->top_idx + browser->height) {
394 ++browser->top_idx;
395 browser->seek(browser, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300396 }
397 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200398 case K_UP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300399 if (browser->index == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300400 break;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300401 --browser->index;
402 if (browser->index < browser->top_idx) {
403 --browser->top_idx;
404 browser->seek(browser, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300405 }
406 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200407 case K_PGDN:
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300408 case ' ':
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300409 if (browser->top_idx + browser->height > browser->nr_entries - 1)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300410 break;
411
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300412 offset = browser->height;
413 if (browser->index + offset > browser->nr_entries - 1)
414 offset = browser->nr_entries - 1 - browser->index;
415 browser->index += offset;
416 browser->top_idx += offset;
417 browser->seek(browser, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300418 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200419 case K_PGUP:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300420 if (browser->top_idx == 0)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300421 break;
422
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300423 if (browser->top_idx < browser->height)
424 offset = browser->top_idx;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300425 else
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300426 offset = browser->height;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300427
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300428 browser->index -= offset;
429 browser->top_idx -= offset;
430 browser->seek(browser, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300431 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200432 case K_HOME:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300433 ui_browser__reset_index(browser);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300434 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200435 case K_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300436 offset = browser->height - 1;
437 if (offset >= browser->nr_entries)
438 offset = browser->nr_entries - 1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300439
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300440 browser->index = browser->nr_entries - 1;
441 browser->top_idx = browser->index - offset;
442 browser->seek(browser, -offset, SEEK_END);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300443 break;
444 default:
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300445 return key;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300446 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300447 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300448 return -1;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300449}
450
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300451unsigned int ui_browser__list_head_refresh(struct ui_browser *browser)
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300452{
453 struct list_head *pos;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300454 struct list_head *head = browser->entries;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300455 int row = 0;
456
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300457 if (browser->top == NULL || browser->top == browser->entries)
458 browser->top = ui_browser__list_head_filter_entries(browser, head->next);
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300459
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300460 pos = browser->top;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300461
462 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300463 if (!browser->filter || !browser->filter(browser, pos)) {
464 ui_browser__gotorc(browser, row, 0);
465 browser->write(browser, pos, row);
466 if (++row == browser->height)
Arnaldo Carvalho de Melo250611c2011-10-14 12:27:54 -0300467 break;
468 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300469 }
470
471 return row;
472}
473
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200474static struct ui_browser__colorset {
475 const char *name, *fg, *bg;
476 int colorset;
477} ui_browser__colorsets[] = {
478 {
479 .colorset = HE_COLORSET_TOP,
480 .name = "top",
481 .fg = "red",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200482 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200483 },
484 {
485 .colorset = HE_COLORSET_MEDIUM,
486 .name = "medium",
487 .fg = "green",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200488 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200489 },
490 {
491 .colorset = HE_COLORSET_NORMAL,
492 .name = "normal",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200493 .fg = "default",
494 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200495 },
496 {
497 .colorset = HE_COLORSET_SELECTED,
498 .name = "selected",
499 .fg = "black",
500 .bg = "lightgray",
501 },
502 {
503 .colorset = HE_COLORSET_CODE,
504 .name = "code",
505 .fg = "blue",
Arnaldo Carvalho de Melo82e0af82011-10-19 00:30:32 -0200506 .bg = "default",
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200507 },
508 {
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300509 .colorset = HE_COLORSET_ADDR,
510 .name = "addr",
511 .fg = "magenta",
512 .bg = "default",
513 },
514 {
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200515 .name = NULL,
516 }
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300517};
518
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200519
520static int ui_browser__color_config(const char *var, const char *value,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300521 void *data __maybe_unused)
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200522{
523 char *fg = NULL, *bg;
524 int i;
525
526 /* same dir for all commands */
527 if (prefixcmp(var, "colors.") != 0)
528 return 0;
529
530 for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
531 const char *name = var + 7;
532
533 if (strcmp(ui_browser__colorsets[i].name, name) != 0)
534 continue;
535
536 fg = strdup(value);
537 if (fg == NULL)
538 break;
539
540 bg = strchr(fg, ',');
541 if (bg == NULL)
542 break;
543
544 *bg = '\0';
545 while (isspace(*++bg));
546 ui_browser__colorsets[i].bg = bg;
547 ui_browser__colorsets[i].fg = fg;
548 return 0;
549 }
550
551 free(fg);
552 return -1;
553}
554
Arnaldo Carvalho de Melo1056d3d2011-10-26 07:11:03 -0200555void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
556{
557 switch (whence) {
558 case SEEK_SET:
559 browser->top = browser->entries;
560 break;
561 case SEEK_CUR:
562 browser->top = browser->top + browser->top_idx + offset;
563 break;
564 case SEEK_END:
565 browser->top = browser->top + browser->nr_entries + offset;
566 break;
567 default:
568 return;
569 }
570}
571
572unsigned int ui_browser__argv_refresh(struct ui_browser *browser)
573{
574 unsigned int row = 0, idx = browser->top_idx;
575 char **pos;
576
577 if (browser->top == NULL)
578 browser->top = browser->entries;
579
580 pos = (char **)browser->top;
581 while (idx < browser->nr_entries) {
582 if (!browser->filter || !browser->filter(browser, *pos)) {
583 ui_browser__gotorc(browser, row, 0);
584 browser->write(browser, pos, row);
585 if (++row == browser->height)
586 break;
587 }
588
589 ++idx;
590 ++pos;
591 }
592
593 return row;
594}
595
Arnaldo Carvalho de Melo4656cca2012-05-03 13:07:05 -0300596void __ui_browser__vline(struct ui_browser *browser, unsigned int column,
597 u16 start, u16 end)
598{
599 SLsmg_set_char_set(1);
600 ui_browser__gotorc(browser, start, column);
601 SLsmg_draw_vline(end - start + 1);
602 SLsmg_set_char_set(0);
603}
604
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300605void ui_browser__write_graph(struct ui_browser *browser __maybe_unused,
606 int graph)
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300607{
608 SLsmg_set_char_set(1);
609 SLsmg_write_char(graph);
610 SLsmg_set_char_set(0);
611}
612
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300613static void __ui_browser__line_arrow_up(struct ui_browser *browser,
614 unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300615 u64 start, u64 end)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300616{
617 unsigned int row, end_row;
618
619 SLsmg_set_char_set(1);
620
621 if (start < browser->top_idx + browser->height) {
622 row = start - browser->top_idx;
623 ui_browser__gotorc(browser, row, column);
624 SLsmg_write_char(SLSMG_LLCORN_CHAR);
625 ui_browser__gotorc(browser, row, column + 1);
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300626 SLsmg_draw_hline(2);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300627
628 if (row-- == 0)
629 goto out;
630 } else
631 row = browser->height - 1;
632
633 if (end > browser->top_idx)
634 end_row = end - browser->top_idx;
635 else
636 end_row = 0;
637
638 ui_browser__gotorc(browser, end_row, column);
639 SLsmg_draw_vline(row - end_row + 1);
640
641 ui_browser__gotorc(browser, end_row, column);
642 if (end >= browser->top_idx) {
643 SLsmg_write_char(SLSMG_ULCORN_CHAR);
644 ui_browser__gotorc(browser, end_row, column + 1);
645 SLsmg_write_char(SLSMG_HLINE_CHAR);
646 ui_browser__gotorc(browser, end_row, column + 2);
647 SLsmg_write_char(SLSMG_RARROW_CHAR);
648 }
649out:
650 SLsmg_set_char_set(0);
651}
652
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300653static void __ui_browser__line_arrow_down(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 Melo944e1ab2012-04-27 16:27:52 -0300656{
657 unsigned int row, end_row;
658
659 SLsmg_set_char_set(1);
660
661 if (start >= browser->top_idx) {
662 row = start - browser->top_idx;
663 ui_browser__gotorc(browser, row, column);
664 SLsmg_write_char(SLSMG_ULCORN_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 Melo944e1ab2012-04-27 16:27:52 -0300667
668 if (row++ == 0)
669 goto out;
670 } else
671 row = 0;
672
673 if (end >= browser->top_idx + browser->height)
674 end_row = browser->height - 1;
675 else
676 end_row = end - browser->top_idx;;
677
678 ui_browser__gotorc(browser, row, column);
679 SLsmg_draw_vline(end_row - row + 1);
680
681 ui_browser__gotorc(browser, end_row, column);
682 if (end < browser->top_idx + browser->height) {
683 SLsmg_write_char(SLSMG_LLCORN_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
693void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column,
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300694 u64 start, u64 end)
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300695{
696 if (start > end)
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300697 __ui_browser__line_arrow_up(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300698 else
Arnaldo Carvalho de Melo83b1f2a2012-05-03 13:12:49 -0300699 __ui_browser__line_arrow_down(browser, column, start, end);
Arnaldo Carvalho de Melo944e1ab2012-04-27 16:27:52 -0300700}
701
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300702void ui_browser__init(void)
703{
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200704 int i = 0;
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300705
Arnaldo Carvalho de Meloe039fc72011-10-18 15:50:51 -0200706 perf_config(ui_browser__color_config, NULL);
707
708 while (ui_browser__colorsets[i].name) {
709 struct ui_browser__colorset *c = &ui_browser__colorsets[i++];
710 sltt_set_color(c->colorset, c->name, c->fg, c->bg);
711 }
Arnaldo Carvalho de Meloc323cf02012-05-29 22:06:30 -0300712
713 annotate_browser__init();
Arnaldo Carvalho de Meloef8f34a2010-08-06 17:35:02 -0300714}