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 | 0870916 | 2012-04-02 12:48:56 -0300 | [diff] [blame^] | 30 | int ui_browser__set_color(struct ui_browser *browser, int color) |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 31 | { |
Arnaldo Carvalho de Melo | 0870916 | 2012-04-02 12:48:56 -0300 | [diff] [blame^] | 32 | int ret = browser->current_color; |
| 33 | browser->current_color = color; |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 34 | SLsmg_set_color(color); |
Arnaldo Carvalho de Melo | 0870916 | 2012-04-02 12:48:56 -0300 | [diff] [blame^] | 35 | return ret; |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void ui_browser__set_percent_color(struct ui_browser *self, |
| 39 | double percent, bool current) |
| 40 | { |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 41 | int color = ui_browser__percent_color(self, percent, current); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 42 | ui_browser__set_color(self, color); |
| 43 | } |
| 44 | |
| 45 | void ui_browser__gotorc(struct ui_browser *self, int y, int x) |
| 46 | { |
| 47 | SLsmg_gotorc(self->y + y, self->x + x); |
| 48 | } |
| 49 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 50 | static struct list_head * |
| 51 | ui_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 | |
| 63 | static struct list_head * |
| 64 | ui_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 Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 76 | void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence) |
| 77 | { |
| 78 | struct list_head *head = self->entries; |
| 79 | struct list_head *pos; |
| 80 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 81 | if (self->nr_entries == 0) |
| 82 | return; |
| 83 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 84 | switch (whence) { |
| 85 | case SEEK_SET: |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 86 | pos = ui_browser__list_head_filter_entries(self, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 87 | break; |
| 88 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 89 | pos = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 90 | break; |
| 91 | case SEEK_END: |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 92 | 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] | 93 | break; |
| 94 | default: |
| 95 | return; |
| 96 | } |
| 97 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 98 | assert(pos != NULL); |
| 99 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 100 | if (offset > 0) { |
| 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_entries(self, pos->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 103 | } else { |
| 104 | while (offset++ != 0) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 105 | 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] | 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 108 | self->top = pos; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence) |
| 112 | { |
| 113 | struct rb_root *root = self->entries; |
| 114 | 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 Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 121 | nd = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 122 | 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 Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 138 | self->top = nd; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self) |
| 142 | { |
| 143 | struct rb_node *nd; |
| 144 | int row = 0; |
| 145 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 146 | if (self->top == NULL) |
| 147 | self->top = rb_first(self->entries); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 148 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 149 | nd = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 150 | |
| 151 | while (nd != NULL) { |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 152 | ui_browser__gotorc(self, row, 0); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 153 | self->write(self, nd, row); |
| 154 | if (++row == self->height) |
| 155 | break; |
| 156 | nd = rb_next(nd); |
| 157 | } |
| 158 | |
| 159 | return row; |
| 160 | } |
| 161 | |
| 162 | bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row) |
| 163 | { |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 164 | return self->top_idx + row == self->index; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void ui_browser__refresh_dimensions(struct ui_browser *self) |
| 168 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 169 | self->width = SLtt_Screen_Cols - 1; |
| 170 | self->height = SLtt_Screen_Rows - 2; |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 171 | self->y = 1; |
| 172 | self->x = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 173 | } |
| 174 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 175 | void 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 Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 182 | int ui_browser__warning(struct ui_browser *browser, int timeout, |
| 183 | const char *format, ...) |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 184 | { |
| 185 | va_list args; |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 186 | char *text; |
| 187 | int key = 0, err; |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 188 | |
| 189 | va_start(args, format); |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 190 | err = vasprintf(&text, format, args); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 191 | va_end(args); |
| 192 | |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 193 | 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 Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 205 | return key; |
| 206 | } |
| 207 | |
| 208 | int 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 | |
| 218 | bool 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 Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 228 | void ui_browser__reset_index(struct ui_browser *self) |
| 229 | { |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 230 | self->index = self->top_idx = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 231 | self->seek(self, 0, SEEK_SET); |
| 232 | } |
| 233 | |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 234 | void __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 Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 238 | slsmg_write_nstring(title, browser->width + 1); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void 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 Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 248 | int ui_browser__show(struct ui_browser *self, const char *title, |
| 249 | const char *helpline, ...) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 250 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 251 | int err; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 252 | va_list ap; |
| 253 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 254 | ui_browser__refresh_dimensions(self); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 255 | |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 256 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 257 | __ui_browser__show_title(self, title); |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 258 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 259 | self->title = title; |
| 260 | free(self->helpline); |
| 261 | self->helpline = NULL; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 262 | |
| 263 | va_start(ap, helpline); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 264 | err = vasprintf(&self->helpline, helpline, ap); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 265 | va_end(ap); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 266 | if (err > 0) |
| 267 | ui_helpline__push(self->helpline); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 268 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 269 | return err ? 0 : -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 270 | } |
| 271 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 272 | void ui_browser__hide(struct ui_browser *browser __used) |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 273 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 274 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 275 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 276 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 277 | } |
| 278 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 279 | static 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 Melo | 0458122 | 2011-10-26 08:19:05 -0200 | [diff] [blame] | 290 | SLsmg_set_char_set(1); |
| 291 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 292 | while (h < height) { |
| 293 | ui_browser__gotorc(browser, row++, col); |
Arnaldo Carvalho de Melo | 0458122 | 2011-10-26 08:19:05 -0200 | [diff] [blame] | 294 | SLsmg_write_char(h == pct ? SLSMG_DIAMOND_CHAR : SLSMG_CKBRD_CHAR); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 295 | ++h; |
| 296 | } |
Arnaldo Carvalho de Melo | 0458122 | 2011-10-26 08:19:05 -0200 | [diff] [blame] | 297 | |
| 298 | SLsmg_set_char_set(0); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static int __ui_browser__refresh(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 302 | { |
| 303 | int row; |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 304 | int width = browser->width; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 305 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 306 | row = browser->refresh(browser); |
| 307 | ui_browser__set_color(browser, HE_COLORSET_NORMAL); |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 308 | |
| 309 | if (!browser->use_navkeypressed || browser->navkeypressed) |
| 310 | ui_browser__scrollbar_set(browser); |
| 311 | else |
| 312 | width += 1; |
| 313 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 314 | SLsmg_fill_region(browser->y + row, browser->x, |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 315 | browser->height - row, width, ' '); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | int ui_browser__refresh(struct ui_browser *browser) |
| 321 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 322 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 323 | __ui_browser__refresh(browser); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 324 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 329 | /* |
| 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 | */ |
| 335 | void 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 Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 349 | browser->top = NULL; |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 350 | browser->seek(browser, browser->top_idx, SEEK_SET); |
| 351 | } |
| 352 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 353 | 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] | 354 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 355 | int err, key; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 356 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 357 | while (1) { |
| 358 | off_t offset; |
| 359 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 360 | pthread_mutex_lock(&ui__lock); |
| 361 | err = __ui_browser__refresh(self); |
| 362 | SLsmg_refresh(); |
| 363 | pthread_mutex_unlock(&ui__lock); |
| 364 | if (err < 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 365 | break; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 366 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 367 | key = ui__getch(delay_secs); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 368 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 369 | if (key == K_RESIZE) { |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 370 | ui__refresh_dimensions(false); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 371 | ui_browser__refresh_dimensions(self); |
| 372 | __ui_browser__show_title(self, self->title); |
| 373 | ui_helpline__puts(self->helpline); |
| 374 | continue; |
| 375 | } |
| 376 | |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 377 | if (self->use_navkeypressed && !self->navkeypressed) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 378 | if (key == K_DOWN || key == K_UP || |
| 379 | key == K_PGDN || key == K_PGUP || |
| 380 | key == K_HOME || key == K_END || |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 381 | key == ' ') { |
| 382 | self->navkeypressed = true; |
| 383 | continue; |
| 384 | } else |
| 385 | return key; |
| 386 | } |
| 387 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 388 | switch (key) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 389 | case K_DOWN: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 390 | if (self->index == self->nr_entries - 1) |
| 391 | break; |
| 392 | ++self->index; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 393 | if (self->index == self->top_idx + self->height) { |
| 394 | ++self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 395 | self->seek(self, +1, SEEK_CUR); |
| 396 | } |
| 397 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 398 | case K_UP: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 399 | if (self->index == 0) |
| 400 | break; |
| 401 | --self->index; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 402 | if (self->index < self->top_idx) { |
| 403 | --self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 404 | self->seek(self, -1, SEEK_CUR); |
| 405 | } |
| 406 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 407 | case K_PGDN: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 408 | case ' ': |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 409 | if (self->top_idx + self->height > self->nr_entries - 1) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 410 | break; |
| 411 | |
| 412 | offset = self->height; |
| 413 | if (self->index + offset > self->nr_entries - 1) |
| 414 | offset = self->nr_entries - 1 - self->index; |
| 415 | self->index += offset; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 416 | self->top_idx += offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 417 | self->seek(self, +offset, SEEK_CUR); |
| 418 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 419 | case K_PGUP: |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 420 | if (self->top_idx == 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 421 | break; |
| 422 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 423 | if (self->top_idx < self->height) |
| 424 | offset = self->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 425 | else |
| 426 | offset = self->height; |
| 427 | |
| 428 | self->index -= offset; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 429 | self->top_idx -= offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 430 | self->seek(self, -offset, SEEK_CUR); |
| 431 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 432 | case K_HOME: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 433 | ui_browser__reset_index(self); |
| 434 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 435 | case K_END: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 436 | offset = self->height - 1; |
| 437 | if (offset >= self->nr_entries) |
| 438 | offset = self->nr_entries - 1; |
| 439 | |
| 440 | self->index = self->nr_entries - 1; |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 441 | self->top_idx = self->index - offset; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 442 | self->seek(self, -offset, SEEK_END); |
| 443 | break; |
| 444 | default: |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 445 | return key; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 446 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 447 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 448 | return -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | unsigned int ui_browser__list_head_refresh(struct ui_browser *self) |
| 452 | { |
| 453 | struct list_head *pos; |
| 454 | struct list_head *head = self->entries; |
| 455 | int row = 0; |
| 456 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 457 | if (self->top == NULL || self->top == self->entries) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 458 | 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] | 459 | |
Arnaldo Carvalho de Melo | d247eb6 | 2010-08-07 13:56:04 -0300 | [diff] [blame] | 460 | pos = self->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 461 | |
| 462 | list_for_each_from(pos, head) { |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 463 | if (!self->filter || !self->filter(self, pos)) { |
| 464 | ui_browser__gotorc(self, row, 0); |
| 465 | self->write(self, pos, row); |
| 466 | if (++row == self->height) |
| 467 | break; |
| 468 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return row; |
| 472 | } |
| 473 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 474 | static 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 Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 482 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 483 | }, |
| 484 | { |
| 485 | .colorset = HE_COLORSET_MEDIUM, |
| 486 | .name = "medium", |
| 487 | .fg = "green", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 488 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 489 | }, |
| 490 | { |
| 491 | .colorset = HE_COLORSET_NORMAL, |
| 492 | .name = "normal", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 493 | .fg = "default", |
| 494 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 495 | }, |
| 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 Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 506 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 507 | }, |
| 508 | { |
| 509 | .name = NULL, |
| 510 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 511 | }; |
| 512 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 513 | |
| 514 | static int ui_browser__color_config(const char *var, const char *value, |
| 515 | void *data __used) |
| 516 | { |
| 517 | char *fg = NULL, *bg; |
| 518 | int i; |
| 519 | |
| 520 | /* same dir for all commands */ |
| 521 | if (prefixcmp(var, "colors.") != 0) |
| 522 | return 0; |
| 523 | |
| 524 | for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) { |
| 525 | const char *name = var + 7; |
| 526 | |
| 527 | if (strcmp(ui_browser__colorsets[i].name, name) != 0) |
| 528 | continue; |
| 529 | |
| 530 | fg = strdup(value); |
| 531 | if (fg == NULL) |
| 532 | break; |
| 533 | |
| 534 | bg = strchr(fg, ','); |
| 535 | if (bg == NULL) |
| 536 | break; |
| 537 | |
| 538 | *bg = '\0'; |
| 539 | while (isspace(*++bg)); |
| 540 | ui_browser__colorsets[i].bg = bg; |
| 541 | ui_browser__colorsets[i].fg = fg; |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | free(fg); |
| 546 | return -1; |
| 547 | } |
| 548 | |
Arnaldo Carvalho de Melo | 1056d3dd | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 549 | void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence) |
| 550 | { |
| 551 | switch (whence) { |
| 552 | case SEEK_SET: |
| 553 | browser->top = browser->entries; |
| 554 | break; |
| 555 | case SEEK_CUR: |
| 556 | browser->top = browser->top + browser->top_idx + offset; |
| 557 | break; |
| 558 | case SEEK_END: |
| 559 | browser->top = browser->top + browser->nr_entries + offset; |
| 560 | break; |
| 561 | default: |
| 562 | return; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | unsigned int ui_browser__argv_refresh(struct ui_browser *browser) |
| 567 | { |
| 568 | unsigned int row = 0, idx = browser->top_idx; |
| 569 | char **pos; |
| 570 | |
| 571 | if (browser->top == NULL) |
| 572 | browser->top = browser->entries; |
| 573 | |
| 574 | pos = (char **)browser->top; |
| 575 | while (idx < browser->nr_entries) { |
| 576 | if (!browser->filter || !browser->filter(browser, *pos)) { |
| 577 | ui_browser__gotorc(browser, row, 0); |
| 578 | browser->write(browser, pos, row); |
| 579 | if (++row == browser->height) |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | ++idx; |
| 584 | ++pos; |
| 585 | } |
| 586 | |
| 587 | return row; |
| 588 | } |
| 589 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 590 | void ui_browser__init(void) |
| 591 | { |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 592 | int i = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 593 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 594 | perf_config(ui_browser__color_config, NULL); |
| 595 | |
| 596 | while (ui_browser__colorsets[i].name) { |
| 597 | struct ui_browser__colorset *c = &ui_browser__colorsets[i++]; |
| 598 | sltt_set_color(c->colorset, c->name, c->fg, c->bg); |
| 599 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 600 | } |