Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 1 | #include "../util.h" |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 2 | #include "../cache.h" |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 3 | #include "../../perf.h" |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 4 | #include "libslang.h" |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 5 | #include <newt.h> |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 6 | #include "ui.h" |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 7 | #include "util.h" |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 9 | #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 Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 14 | #include "helpline.h" |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 15 | #include "keysyms.h" |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 16 | #include "../color.h" |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 17 | |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 18 | static int ui_browser__percent_color(struct ui_browser *browser, |
| 19 | double percent, bool current) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 20 | { |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 21 | if (current && (!browser->use_navkeypressed || browser->navkeypressed)) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 22 | 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 Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 30 | void ui_browser__set_color(struct ui_browser *self __used, int color) |
| 31 | { |
| 32 | SLsmg_set_color(color); |
| 33 | } |
| 34 | |
| 35 | void ui_browser__set_percent_color(struct ui_browser *self, |
| 36 | double percent, bool current) |
| 37 | { |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 38 | int color = ui_browser__percent_color(self, percent, current); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 39 | ui_browser__set_color(self, color); |
| 40 | } |
| 41 | |
| 42 | void ui_browser__gotorc(struct ui_browser *self, int y, int x) |
| 43 | { |
| 44 | SLsmg_gotorc(self->y + y, self->x + x); |
| 45 | } |
| 46 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 47 | static struct list_head * |
| 48 | ui_browser__list_head_filter_entries(struct ui_browser *browser, |
| 49 | struct list_head *pos) |
| 50 | { |
| 51 | do { |
| 52 | if (!browser->filter || !browser->filter(browser, pos)) |
| 53 | return pos; |
| 54 | pos = pos->next; |
| 55 | } while (pos != browser->entries); |
| 56 | |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | static struct list_head * |
| 61 | ui_browser__list_head_filter_prev_entries(struct ui_browser *browser, |
| 62 | struct list_head *pos) |
| 63 | { |
| 64 | do { |
| 65 | if (!browser->filter || !browser->filter(browser, pos)) |
| 66 | return pos; |
| 67 | pos = pos->prev; |
| 68 | } while (pos != browser->entries); |
| 69 | |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 73 | void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence) |
| 74 | { |
| 75 | struct list_head *head = self->entries; |
| 76 | struct list_head *pos; |
| 77 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 78 | if (self->nr_entries == 0) |
| 79 | return; |
| 80 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 81 | switch (whence) { |
| 82 | case SEEK_SET: |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 83 | pos = ui_browser__list_head_filter_entries(self, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 84 | break; |
| 85 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 86 | pos = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 87 | break; |
| 88 | case SEEK_END: |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 89 | pos = ui_browser__list_head_filter_prev_entries(self, head->prev); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 90 | break; |
| 91 | default: |
| 92 | return; |
| 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 95 | assert(pos != NULL); |
| 96 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 97 | if (offset > 0) { |
| 98 | while (offset-- != 0) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 99 | pos = ui_browser__list_head_filter_entries(self, pos->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 100 | } else { |
| 101 | while (offset++ != 0) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 102 | pos = ui_browser__list_head_filter_prev_entries(self, pos->prev); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 103 | } |
| 104 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 105 | self->top = pos; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence) |
| 109 | { |
| 110 | struct rb_root *root = self->entries; |
| 111 | struct rb_node *nd; |
| 112 | |
| 113 | switch (whence) { |
| 114 | case SEEK_SET: |
| 115 | nd = rb_first(root); |
| 116 | break; |
| 117 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 118 | nd = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 119 | break; |
| 120 | case SEEK_END: |
| 121 | nd = rb_last(root); |
| 122 | break; |
| 123 | default: |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (offset > 0) { |
| 128 | while (offset-- != 0) |
| 129 | nd = rb_next(nd); |
| 130 | } else { |
| 131 | while (offset++ != 0) |
| 132 | nd = rb_prev(nd); |
| 133 | } |
| 134 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 135 | self->top = nd; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self) |
| 139 | { |
| 140 | struct rb_node *nd; |
| 141 | int row = 0; |
| 142 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 143 | if (self->top == NULL) |
| 144 | self->top = rb_first(self->entries); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 145 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 146 | nd = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 147 | |
| 148 | while (nd != NULL) { |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 149 | ui_browser__gotorc(self, row, 0); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 150 | self->write(self, nd, row); |
| 151 | if (++row == self->height) |
| 152 | break; |
| 153 | nd = rb_next(nd); |
| 154 | } |
| 155 | |
| 156 | return row; |
| 157 | } |
| 158 | |
| 159 | bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row) |
| 160 | { |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 161 | return self->top_idx + row == self->index; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void ui_browser__refresh_dimensions(struct ui_browser *self) |
| 165 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 166 | self->width = SLtt_Screen_Cols - 1; |
| 167 | self->height = SLtt_Screen_Rows - 2; |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 168 | self->y = 1; |
| 169 | self->x = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void ui_browser__reset_index(struct ui_browser *self) |
| 173 | { |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 174 | self->index = self->top_idx = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 175 | self->seek(self, 0, SEEK_SET); |
| 176 | } |
| 177 | |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 178 | void __ui_browser__show_title(struct ui_browser *browser, const char *title) |
| 179 | { |
| 180 | SLsmg_gotorc(0, 0); |
| 181 | ui_browser__set_color(browser, NEWT_COLORSET_ROOT); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 182 | slsmg_write_nstring(title, browser->width + 1); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void ui_browser__show_title(struct ui_browser *browser, const char *title) |
| 186 | { |
| 187 | pthread_mutex_lock(&ui__lock); |
| 188 | __ui_browser__show_title(browser, title); |
| 189 | pthread_mutex_unlock(&ui__lock); |
| 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 192 | int ui_browser__show(struct ui_browser *self, const char *title, |
| 193 | const char *helpline, ...) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 194 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 195 | int err; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 196 | va_list ap; |
| 197 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 198 | ui_browser__refresh_dimensions(self); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 199 | |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 200 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 201 | __ui_browser__show_title(self, title); |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 202 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 203 | self->title = title; |
| 204 | free(self->helpline); |
| 205 | self->helpline = NULL; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 206 | |
| 207 | va_start(ap, helpline); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 208 | err = vasprintf(&self->helpline, helpline, ap); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 209 | va_end(ap); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 210 | if (err > 0) |
| 211 | ui_helpline__push(self->helpline); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 212 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 213 | return err ? 0 : -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 214 | } |
| 215 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 216 | void ui_browser__hide(struct ui_browser *browser __used) |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 217 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 218 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 219 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 220 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 221 | } |
| 222 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 223 | static void ui_browser__scrollbar_set(struct ui_browser *browser) |
| 224 | { |
| 225 | int height = browser->height, h = 0, pct = 0, |
| 226 | col = browser->width, |
| 227 | row = browser->y - 1; |
| 228 | |
| 229 | if (browser->nr_entries > 1) { |
| 230 | pct = ((browser->index * (browser->height - 1)) / |
| 231 | (browser->nr_entries - 1)); |
| 232 | } |
| 233 | |
| 234 | while (h < height) { |
| 235 | ui_browser__gotorc(browser, row++, col); |
| 236 | SLsmg_set_char_set(1); |
| 237 | SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_BOARD_CHAR); |
| 238 | SLsmg_set_char_set(0); |
| 239 | ++h; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static int __ui_browser__refresh(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 244 | { |
| 245 | int row; |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 246 | int width = browser->width; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 247 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 248 | row = browser->refresh(browser); |
| 249 | ui_browser__set_color(browser, HE_COLORSET_NORMAL); |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 250 | |
| 251 | if (!browser->use_navkeypressed || browser->navkeypressed) |
| 252 | ui_browser__scrollbar_set(browser); |
| 253 | else |
| 254 | width += 1; |
| 255 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 256 | SLsmg_fill_region(browser->y + row, browser->x, |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 257 | browser->height - row, width, ' '); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | int ui_browser__refresh(struct ui_browser *browser) |
| 263 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 264 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 265 | __ui_browser__refresh(browser); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 266 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 271 | /* |
| 272 | * Here we're updating nr_entries _after_ we started browsing, i.e. we have to |
| 273 | * forget about any reference to any entry in the underlying data structure, |
| 274 | * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser |
| 275 | * after an output_resort and hist decay. |
| 276 | */ |
| 277 | void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries) |
| 278 | { |
| 279 | off_t offset = nr_entries - browser->nr_entries; |
| 280 | |
| 281 | browser->nr_entries = nr_entries; |
| 282 | |
| 283 | if (offset < 0) { |
| 284 | if (browser->top_idx < (u64)-offset) |
| 285 | offset = -browser->top_idx; |
| 286 | |
| 287 | browser->index += offset; |
| 288 | browser->top_idx += offset; |
| 289 | } |
| 290 | |
Arnaldo Carvalho de Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 291 | browser->top = NULL; |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 292 | browser->seek(browser, browser->top_idx, SEEK_SET); |
| 293 | } |
| 294 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 295 | int ui_browser__run(struct ui_browser *self, int delay_secs) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 296 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 297 | int err, key; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 298 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 299 | while (1) { |
| 300 | off_t offset; |
| 301 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 302 | pthread_mutex_lock(&ui__lock); |
| 303 | err = __ui_browser__refresh(self); |
| 304 | SLsmg_refresh(); |
| 305 | pthread_mutex_unlock(&ui__lock); |
| 306 | if (err < 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 307 | break; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 308 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 309 | key = ui__getch(delay_secs); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 310 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 311 | if (key == K_RESIZE) { |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 312 | ui__refresh_dimensions(false); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 313 | ui_browser__refresh_dimensions(self); |
| 314 | __ui_browser__show_title(self, self->title); |
| 315 | ui_helpline__puts(self->helpline); |
| 316 | continue; |
| 317 | } |
| 318 | |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 319 | if (self->use_navkeypressed && !self->navkeypressed) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 320 | if (key == K_DOWN || key == K_UP || |
| 321 | key == K_PGDN || key == K_PGUP || |
| 322 | key == K_HOME || key == K_END || |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 323 | key == ' ') { |
| 324 | self->navkeypressed = true; |
| 325 | continue; |
| 326 | } else |
| 327 | return key; |
| 328 | } |
| 329 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 330 | switch (key) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 331 | case K_DOWN: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 332 | if (self->index == self->nr_entries - 1) |
| 333 | break; |
| 334 | ++self->index; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 335 | if (self->index == self->top_idx + self->height) { |
| 336 | ++self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 337 | self->seek(self, +1, SEEK_CUR); |
| 338 | } |
| 339 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 340 | case K_UP: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 341 | if (self->index == 0) |
| 342 | break; |
| 343 | --self->index; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 344 | if (self->index < self->top_idx) { |
| 345 | --self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 346 | self->seek(self, -1, SEEK_CUR); |
| 347 | } |
| 348 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 349 | case K_PGDN: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 350 | case ' ': |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 351 | if (self->top_idx + self->height > self->nr_entries - 1) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 352 | break; |
| 353 | |
| 354 | offset = self->height; |
| 355 | if (self->index + offset > self->nr_entries - 1) |
| 356 | offset = self->nr_entries - 1 - self->index; |
| 357 | self->index += offset; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 358 | self->top_idx += offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 359 | self->seek(self, +offset, SEEK_CUR); |
| 360 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 361 | case K_PGUP: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 362 | if (self->top_idx == 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 363 | break; |
| 364 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 365 | if (self->top_idx < self->height) |
| 366 | offset = self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 367 | else |
| 368 | offset = self->height; |
| 369 | |
| 370 | self->index -= offset; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 371 | self->top_idx -= offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 372 | self->seek(self, -offset, SEEK_CUR); |
| 373 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 374 | case K_HOME: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 375 | ui_browser__reset_index(self); |
| 376 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 377 | case K_END: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 378 | offset = self->height - 1; |
| 379 | if (offset >= self->nr_entries) |
| 380 | offset = self->nr_entries - 1; |
| 381 | |
| 382 | self->index = self->nr_entries - 1; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 383 | self->top_idx = self->index - offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 384 | self->seek(self, -offset, SEEK_END); |
| 385 | break; |
| 386 | default: |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 387 | return key; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 388 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 389 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 390 | return -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | unsigned int ui_browser__list_head_refresh(struct ui_browser *self) |
| 394 | { |
| 395 | struct list_head *pos; |
| 396 | struct list_head *head = self->entries; |
| 397 | int row = 0; |
| 398 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 399 | if (self->top == NULL || self->top == self->entries) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 400 | self->top = ui_browser__list_head_filter_entries(self, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 401 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 402 | pos = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 403 | |
| 404 | list_for_each_from(pos, head) { |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 405 | if (!self->filter || !self->filter(self, pos)) { |
| 406 | ui_browser__gotorc(self, row, 0); |
| 407 | self->write(self, pos, row); |
| 408 | if (++row == self->height) |
| 409 | break; |
| 410 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | return row; |
| 414 | } |
| 415 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 416 | static struct ui_browser__colorset { |
| 417 | const char *name, *fg, *bg; |
| 418 | int colorset; |
| 419 | } ui_browser__colorsets[] = { |
| 420 | { |
| 421 | .colorset = HE_COLORSET_TOP, |
| 422 | .name = "top", |
| 423 | .fg = "red", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 424 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 425 | }, |
| 426 | { |
| 427 | .colorset = HE_COLORSET_MEDIUM, |
| 428 | .name = "medium", |
| 429 | .fg = "green", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 430 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 431 | }, |
| 432 | { |
| 433 | .colorset = HE_COLORSET_NORMAL, |
| 434 | .name = "normal", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 435 | .fg = "default", |
| 436 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 437 | }, |
| 438 | { |
| 439 | .colorset = HE_COLORSET_SELECTED, |
| 440 | .name = "selected", |
| 441 | .fg = "black", |
| 442 | .bg = "lightgray", |
| 443 | }, |
| 444 | { |
| 445 | .colorset = HE_COLORSET_CODE, |
| 446 | .name = "code", |
| 447 | .fg = "blue", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 448 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 449 | }, |
| 450 | { |
| 451 | .name = NULL, |
| 452 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 453 | }; |
| 454 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 455 | |
| 456 | static int ui_browser__color_config(const char *var, const char *value, |
| 457 | void *data __used) |
| 458 | { |
| 459 | char *fg = NULL, *bg; |
| 460 | int i; |
| 461 | |
| 462 | /* same dir for all commands */ |
| 463 | if (prefixcmp(var, "colors.") != 0) |
| 464 | return 0; |
| 465 | |
| 466 | for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) { |
| 467 | const char *name = var + 7; |
| 468 | |
| 469 | if (strcmp(ui_browser__colorsets[i].name, name) != 0) |
| 470 | continue; |
| 471 | |
| 472 | fg = strdup(value); |
| 473 | if (fg == NULL) |
| 474 | break; |
| 475 | |
| 476 | bg = strchr(fg, ','); |
| 477 | if (bg == NULL) |
| 478 | break; |
| 479 | |
| 480 | *bg = '\0'; |
| 481 | while (isspace(*++bg)); |
| 482 | ui_browser__colorsets[i].bg = bg; |
| 483 | ui_browser__colorsets[i].fg = fg; |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | free(fg); |
| 488 | return -1; |
| 489 | } |
| 490 | |
Arnaldo Carvalho de Melo | 1056d3dd | 2011-10-26 07:11:03 -0200 | [diff] [blame^] | 491 | void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence) |
| 492 | { |
| 493 | switch (whence) { |
| 494 | case SEEK_SET: |
| 495 | browser->top = browser->entries; |
| 496 | break; |
| 497 | case SEEK_CUR: |
| 498 | browser->top = browser->top + browser->top_idx + offset; |
| 499 | break; |
| 500 | case SEEK_END: |
| 501 | browser->top = browser->top + browser->nr_entries + offset; |
| 502 | break; |
| 503 | default: |
| 504 | return; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | unsigned int ui_browser__argv_refresh(struct ui_browser *browser) |
| 509 | { |
| 510 | unsigned int row = 0, idx = browser->top_idx; |
| 511 | char **pos; |
| 512 | |
| 513 | if (browser->top == NULL) |
| 514 | browser->top = browser->entries; |
| 515 | |
| 516 | pos = (char **)browser->top; |
| 517 | while (idx < browser->nr_entries) { |
| 518 | if (!browser->filter || !browser->filter(browser, *pos)) { |
| 519 | ui_browser__gotorc(browser, row, 0); |
| 520 | browser->write(browser, pos, row); |
| 521 | if (++row == browser->height) |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | ++idx; |
| 526 | ++pos; |
| 527 | } |
| 528 | |
| 529 | return row; |
| 530 | } |
| 531 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 532 | void ui_browser__init(void) |
| 533 | { |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 534 | int i = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 535 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 536 | perf_config(ui_browser__color_config, NULL); |
| 537 | |
| 538 | while (ui_browser__colorsets[i].name) { |
| 539 | struct ui_browser__colorset *c = &ui_browser__colorsets[i++]; |
| 540 | sltt_set_color(c->colorset, c->name, c->fg, c->bg); |
| 541 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 542 | } |