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 | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 5 | #include "ui.h" |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 6 | #include "util.h" |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 7 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 8 | #include <linux/list.h> |
| 9 | #include <linux/rbtree.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <sys/ttydefaults.h> |
| 12 | #include "browser.h" |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 13 | #include "helpline.h" |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 14 | #include "keysyms.h" |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 15 | #include "../color.h" |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 17 | static int ui_browser__percent_color(struct ui_browser *browser, |
| 18 | double percent, bool current) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 19 | { |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 20 | if (current && (!browser->use_navkeypressed || browser->navkeypressed)) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 21 | return HE_COLORSET_SELECTED; |
| 22 | if (percent >= MIN_RED) |
| 23 | return HE_COLORSET_TOP; |
| 24 | if (percent >= MIN_GREEN) |
| 25 | return HE_COLORSET_MEDIUM; |
| 26 | return HE_COLORSET_NORMAL; |
| 27 | } |
| 28 | |
Arnaldo Carvalho de Melo | 0870916 | 2012-04-02 12:48:56 -0300 | [diff] [blame] | 29 | 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] | 30 | { |
Arnaldo Carvalho de Melo | 0870916 | 2012-04-02 12:48:56 -0300 | [diff] [blame] | 31 | int ret = browser->current_color; |
| 32 | browser->current_color = color; |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 33 | SLsmg_set_color(color); |
Arnaldo Carvalho de Melo | 0870916 | 2012-04-02 12:48:56 -0300 | [diff] [blame] | 34 | return ret; |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 35 | } |
| 36 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 37 | void ui_browser__set_percent_color(struct ui_browser *browser, |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 38 | double percent, bool current) |
| 39 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 40 | int color = ui_browser__percent_color(browser, percent, current); |
| 41 | ui_browser__set_color(browser, color); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 42 | } |
| 43 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 44 | void ui_browser__gotorc(struct ui_browser *browser, int y, int x) |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 45 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 46 | SLsmg_gotorc(browser->y + y, browser->x + x); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | 26270a0 | 2015-08-11 12:24:27 -0300 | [diff] [blame] | 49 | void ui_browser__write_nstring(struct ui_browser *browser __maybe_unused, const char *msg, |
| 50 | unsigned int width) |
| 51 | { |
| 52 | slsmg_write_nstring(msg, width); |
| 53 | } |
| 54 | |
Arnaldo Carvalho de Melo | 517dfdb | 2015-08-11 12:50:55 -0300 | [diff] [blame] | 55 | void ui_browser__printf(struct ui_browser *browser __maybe_unused, const char *fmt, ...) |
| 56 | { |
| 57 | va_list args; |
| 58 | |
| 59 | va_start(args, fmt); |
| 60 | slsmg_vprintf(fmt, args); |
| 61 | va_end(args); |
| 62 | } |
| 63 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 64 | static struct list_head * |
| 65 | ui_browser__list_head_filter_entries(struct ui_browser *browser, |
| 66 | struct list_head *pos) |
| 67 | { |
| 68 | do { |
| 69 | if (!browser->filter || !browser->filter(browser, pos)) |
| 70 | return pos; |
| 71 | pos = pos->next; |
| 72 | } while (pos != browser->entries); |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | static struct list_head * |
| 78 | ui_browser__list_head_filter_prev_entries(struct ui_browser *browser, |
| 79 | struct list_head *pos) |
| 80 | { |
| 81 | do { |
| 82 | if (!browser->filter || !browser->filter(browser, pos)) |
| 83 | return pos; |
| 84 | pos = pos->prev; |
| 85 | } while (pos != browser->entries); |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 90 | void ui_browser__list_head_seek(struct ui_browser *browser, off_t offset, int whence) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 91 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 92 | struct list_head *head = browser->entries; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 93 | struct list_head *pos; |
| 94 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 95 | if (browser->nr_entries == 0) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 96 | return; |
| 97 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 98 | switch (whence) { |
| 99 | case SEEK_SET: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 100 | pos = ui_browser__list_head_filter_entries(browser, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 101 | break; |
| 102 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 103 | pos = browser->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 104 | break; |
| 105 | case SEEK_END: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 106 | pos = ui_browser__list_head_filter_prev_entries(browser, head->prev); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | return; |
| 110 | } |
| 111 | |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 112 | assert(pos != NULL); |
| 113 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 114 | if (offset > 0) { |
| 115 | while (offset-- != 0) |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 116 | pos = ui_browser__list_head_filter_entries(browser, pos->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 117 | } else { |
| 118 | while (offset++ != 0) |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 119 | pos = ui_browser__list_head_filter_prev_entries(browser, pos->prev); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 120 | } |
| 121 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 122 | browser->top = pos; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 123 | } |
| 124 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 125 | void ui_browser__rb_tree_seek(struct ui_browser *browser, off_t offset, int whence) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 126 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 127 | struct rb_root *root = browser->entries; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 128 | struct rb_node *nd; |
| 129 | |
| 130 | switch (whence) { |
| 131 | case SEEK_SET: |
| 132 | nd = rb_first(root); |
| 133 | break; |
| 134 | case SEEK_CUR: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 135 | nd = browser->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 136 | break; |
| 137 | case SEEK_END: |
| 138 | nd = rb_last(root); |
| 139 | break; |
| 140 | default: |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | if (offset > 0) { |
| 145 | while (offset-- != 0) |
| 146 | nd = rb_next(nd); |
| 147 | } else { |
| 148 | while (offset++ != 0) |
| 149 | nd = rb_prev(nd); |
| 150 | } |
| 151 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 152 | browser->top = nd; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 153 | } |
| 154 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 155 | unsigned int ui_browser__rb_tree_refresh(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 156 | { |
| 157 | struct rb_node *nd; |
| 158 | int row = 0; |
| 159 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 160 | if (browser->top == NULL) |
| 161 | browser->top = rb_first(browser->entries); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 162 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 163 | nd = browser->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 164 | |
| 165 | while (nd != NULL) { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 166 | ui_browser__gotorc(browser, row, 0); |
| 167 | browser->write(browser, nd, row); |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 168 | if (++row == browser->rows) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 169 | break; |
| 170 | nd = rb_next(nd); |
| 171 | } |
| 172 | |
| 173 | return row; |
| 174 | } |
| 175 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 176 | bool ui_browser__is_current_entry(struct ui_browser *browser, unsigned row) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 177 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 178 | return browser->top_idx + row == browser->index; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 179 | } |
| 180 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 181 | void ui_browser__refresh_dimensions(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 182 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 183 | browser->width = SLtt_Screen_Cols - 1; |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 184 | browser->height = browser->rows = SLtt_Screen_Rows - 2; |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 185 | browser->y = 1; |
| 186 | browser->x = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 189 | void ui_browser__handle_resize(struct ui_browser *browser) |
| 190 | { |
| 191 | ui__refresh_dimensions(false); |
| 192 | ui_browser__show(browser, browser->title, ui_helpline__current); |
| 193 | ui_browser__refresh(browser); |
| 194 | } |
| 195 | |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 196 | int ui_browser__warning(struct ui_browser *browser, int timeout, |
| 197 | const char *format, ...) |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 198 | { |
| 199 | va_list args; |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 200 | char *text; |
| 201 | int key = 0, err; |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 202 | |
| 203 | va_start(args, format); |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 204 | err = vasprintf(&text, format, args); |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 205 | va_end(args); |
| 206 | |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 207 | if (err < 0) { |
| 208 | va_start(args, format); |
| 209 | ui_helpline__vpush(format, args); |
| 210 | va_end(args); |
| 211 | } else { |
zhangdianfang | 7f3e508 | 2014-05-30 08:53:58 +0800 | [diff] [blame] | 212 | while ((key = ui__question_window("Warning!", text, |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 213 | "Press any key...", |
| 214 | timeout)) == K_RESIZE) |
| 215 | ui_browser__handle_resize(browser); |
| 216 | free(text); |
| 217 | } |
| 218 | |
Arnaldo Carvalho de Melo | 4610e41 | 2011-10-26 12:04:37 -0200 | [diff] [blame] | 219 | return key; |
| 220 | } |
| 221 | |
| 222 | int ui_browser__help_window(struct ui_browser *browser, const char *text) |
| 223 | { |
| 224 | int key; |
| 225 | |
| 226 | while ((key = ui__help_window(text)) == K_RESIZE) |
| 227 | ui_browser__handle_resize(browser); |
| 228 | |
| 229 | return key; |
| 230 | } |
| 231 | |
| 232 | bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text) |
| 233 | { |
| 234 | int key; |
| 235 | |
| 236 | while ((key = ui__dialog_yesno(text)) == K_RESIZE) |
| 237 | ui_browser__handle_resize(browser); |
| 238 | |
| 239 | return key == K_ENTER || toupper(key) == 'Y'; |
| 240 | } |
| 241 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 242 | void ui_browser__reset_index(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 243 | { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 244 | browser->index = browser->top_idx = 0; |
| 245 | browser->seek(browser, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 246 | } |
| 247 | |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 248 | void __ui_browser__show_title(struct ui_browser *browser, const char *title) |
| 249 | { |
| 250 | SLsmg_gotorc(0, 0); |
Arnaldo Carvalho de Melo | 6692c26 | 2013-03-28 11:34:10 -0300 | [diff] [blame] | 251 | ui_browser__set_color(browser, HE_COLORSET_ROOT); |
Arnaldo Carvalho de Melo | 26270a0 | 2015-08-11 12:24:27 -0300 | [diff] [blame] | 252 | ui_browser__write_nstring(browser, title, browser->width + 1); |
Arnaldo Carvalho de Melo | b210b3b | 2011-02-25 11:33:31 -0300 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void ui_browser__show_title(struct ui_browser *browser, const char *title) |
| 256 | { |
| 257 | pthread_mutex_lock(&ui__lock); |
| 258 | __ui_browser__show_title(browser, title); |
| 259 | pthread_mutex_unlock(&ui__lock); |
| 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 262 | int ui_browser__show(struct ui_browser *browser, const char *title, |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 263 | const char *helpline, ...) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 264 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 265 | int err; |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 266 | va_list ap; |
| 267 | |
Arnaldo Carvalho de Melo | fa70b5d | 2014-07-01 16:34:42 -0300 | [diff] [blame] | 268 | if (browser->refresh_dimensions == NULL) |
| 269 | browser->refresh_dimensions = ui_browser__refresh_dimensions; |
| 270 | |
| 271 | browser->refresh_dimensions(browser); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 272 | |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 273 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 274 | __ui_browser__show_title(browser, title); |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 275 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 276 | browser->title = title; |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 277 | zfree(&browser->helpline); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 278 | |
| 279 | va_start(ap, helpline); |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 280 | err = vasprintf(&browser->helpline, helpline, ap); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 281 | va_end(ap); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 282 | if (err > 0) |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 283 | ui_helpline__push(browser->helpline); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 284 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 285 | return err ? 0 : -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 286 | } |
| 287 | |
Arnaldo Carvalho de Melo | 3184c47 | 2013-12-19 16:25:17 -0300 | [diff] [blame] | 288 | void ui_browser__hide(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 289 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 290 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 291 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 292 | zfree(&browser->helpline); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 293 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 294 | } |
| 295 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 296 | static void ui_browser__scrollbar_set(struct ui_browser *browser) |
| 297 | { |
| 298 | int height = browser->height, h = 0, pct = 0, |
| 299 | col = browser->width, |
Jiri Olsa | 8963297 | 2014-06-19 13:41:14 +0200 | [diff] [blame] | 300 | row = 0; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 301 | |
| 302 | if (browser->nr_entries > 1) { |
| 303 | pct = ((browser->index * (browser->height - 1)) / |
| 304 | (browser->nr_entries - 1)); |
| 305 | } |
| 306 | |
Arnaldo Carvalho de Melo | 0458122 | 2011-10-26 08:19:05 -0200 | [diff] [blame] | 307 | SLsmg_set_char_set(1); |
| 308 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 309 | while (h < height) { |
| 310 | ui_browser__gotorc(browser, row++, col); |
Arnaldo Carvalho de Melo | 0458122 | 2011-10-26 08:19:05 -0200 | [diff] [blame] | 311 | 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] | 312 | ++h; |
| 313 | } |
Arnaldo Carvalho de Melo | 0458122 | 2011-10-26 08:19:05 -0200 | [diff] [blame] | 314 | |
| 315 | SLsmg_set_char_set(0); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int __ui_browser__refresh(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 319 | { |
| 320 | int row; |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 321 | int width = browser->width; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 322 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 323 | row = browser->refresh(browser); |
| 324 | ui_browser__set_color(browser, HE_COLORSET_NORMAL); |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 325 | |
| 326 | if (!browser->use_navkeypressed || browser->navkeypressed) |
| 327 | ui_browser__scrollbar_set(browser); |
| 328 | else |
| 329 | width += 1; |
| 330 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 331 | SLsmg_fill_region(browser->y + row, browser->x, |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 332 | browser->height - row, width, ' '); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | int ui_browser__refresh(struct ui_browser *browser) |
| 338 | { |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 339 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 340 | __ui_browser__refresh(browser); |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 341 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 346 | /* |
| 347 | * Here we're updating nr_entries _after_ we started browsing, i.e. we have to |
| 348 | * forget about any reference to any entry in the underlying data structure, |
| 349 | * that is why we do a SEEK_SET. Think about 'perf top' in the hists browser |
| 350 | * after an output_resort and hist decay. |
| 351 | */ |
| 352 | void ui_browser__update_nr_entries(struct ui_browser *browser, u32 nr_entries) |
| 353 | { |
| 354 | off_t offset = nr_entries - browser->nr_entries; |
| 355 | |
| 356 | browser->nr_entries = nr_entries; |
| 357 | |
| 358 | if (offset < 0) { |
| 359 | if (browser->top_idx < (u64)-offset) |
| 360 | offset = -browser->top_idx; |
| 361 | |
| 362 | browser->index += offset; |
| 363 | browser->top_idx += offset; |
| 364 | } |
| 365 | |
Arnaldo Carvalho de Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 366 | browser->top = NULL; |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 367 | browser->seek(browser, browser->top_idx, SEEK_SET); |
| 368 | } |
| 369 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 370 | int ui_browser__run(struct ui_browser *browser, int delay_secs) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 371 | { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 372 | int err, key; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 373 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 374 | while (1) { |
| 375 | off_t offset; |
| 376 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 377 | pthread_mutex_lock(&ui__lock); |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 378 | err = __ui_browser__refresh(browser); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 379 | SLsmg_refresh(); |
| 380 | pthread_mutex_unlock(&ui__lock); |
| 381 | if (err < 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 382 | break; |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 383 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 384 | key = ui__getch(delay_secs); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 385 | |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 386 | if (key == K_RESIZE) { |
Arnaldo Carvalho de Melo | 71172ed | 2011-10-25 13:45:16 -0200 | [diff] [blame] | 387 | ui__refresh_dimensions(false); |
Arnaldo Carvalho de Melo | fa70b5d | 2014-07-01 16:34:42 -0300 | [diff] [blame] | 388 | browser->refresh_dimensions(browser); |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 389 | __ui_browser__show_title(browser, browser->title); |
| 390 | ui_helpline__puts(browser->helpline); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 391 | continue; |
| 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 394 | if (browser->use_navkeypressed && !browser->navkeypressed) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 395 | if (key == K_DOWN || key == K_UP || |
Arnaldo Carvalho de Melo | faae6f6 | 2015-08-11 17:14:40 -0300 | [diff] [blame] | 396 | (browser->columns && (key == K_LEFT || key == K_RIGHT)) || |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 397 | key == K_PGDN || key == K_PGUP || |
| 398 | key == K_HOME || key == K_END || |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 399 | key == ' ') { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 400 | browser->navkeypressed = true; |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 401 | continue; |
| 402 | } else |
| 403 | return key; |
| 404 | } |
| 405 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 406 | switch (key) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 407 | case K_DOWN: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 408 | if (browser->index == browser->nr_entries - 1) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 409 | break; |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 410 | ++browser->index; |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 411 | if (browser->index == browser->top_idx + browser->rows) { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 412 | ++browser->top_idx; |
| 413 | browser->seek(browser, +1, SEEK_CUR); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 414 | } |
| 415 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 416 | case K_UP: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 417 | if (browser->index == 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 418 | break; |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 419 | --browser->index; |
| 420 | if (browser->index < browser->top_idx) { |
| 421 | --browser->top_idx; |
| 422 | browser->seek(browser, -1, SEEK_CUR); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 423 | } |
| 424 | break; |
Arnaldo Carvalho de Melo | faae6f6 | 2015-08-11 17:14:40 -0300 | [diff] [blame] | 425 | case K_RIGHT: |
| 426 | if (!browser->columns) |
| 427 | goto out; |
| 428 | if (browser->horiz_scroll < browser->columns - 1) |
| 429 | ++browser->horiz_scroll; |
| 430 | break; |
| 431 | case K_LEFT: |
| 432 | if (!browser->columns) |
| 433 | goto out; |
| 434 | if (browser->horiz_scroll != 0) |
| 435 | --browser->horiz_scroll; |
| 436 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 437 | case K_PGDN: |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 438 | case ' ': |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 439 | if (browser->top_idx + browser->rows > browser->nr_entries - 1) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 440 | break; |
| 441 | |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 442 | offset = browser->rows; |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 443 | if (browser->index + offset > browser->nr_entries - 1) |
| 444 | offset = browser->nr_entries - 1 - browser->index; |
| 445 | browser->index += offset; |
| 446 | browser->top_idx += offset; |
| 447 | browser->seek(browser, +offset, SEEK_CUR); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 448 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 449 | case K_PGUP: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 450 | if (browser->top_idx == 0) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 451 | break; |
| 452 | |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 453 | if (browser->top_idx < browser->rows) |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 454 | offset = browser->top_idx; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 455 | else |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 456 | offset = browser->rows; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 457 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 458 | browser->index -= offset; |
| 459 | browser->top_idx -= offset; |
| 460 | browser->seek(browser, -offset, SEEK_CUR); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 461 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 462 | case K_HOME: |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 463 | ui_browser__reset_index(browser); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 464 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 465 | case K_END: |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 466 | offset = browser->rows - 1; |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 467 | if (offset >= browser->nr_entries) |
| 468 | offset = browser->nr_entries - 1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 469 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 470 | browser->index = browser->nr_entries - 1; |
| 471 | browser->top_idx = browser->index - offset; |
| 472 | browser->seek(browser, -offset, SEEK_END); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 473 | break; |
| 474 | default: |
Arnaldo Carvalho de Melo | faae6f6 | 2015-08-11 17:14:40 -0300 | [diff] [blame] | 475 | out: |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 476 | return key; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 477 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 478 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 479 | return -1; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 480 | } |
| 481 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 482 | unsigned int ui_browser__list_head_refresh(struct ui_browser *browser) |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 483 | { |
| 484 | struct list_head *pos; |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 485 | struct list_head *head = browser->entries; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 486 | int row = 0; |
| 487 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 488 | if (browser->top == NULL || browser->top == browser->entries) |
| 489 | browser->top = ui_browser__list_head_filter_entries(browser, head->next); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 490 | |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 491 | pos = browser->top; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 492 | |
| 493 | list_for_each_from(pos, head) { |
Arnaldo Carvalho de Melo | 05e8b08 | 2012-05-29 22:42:18 -0300 | [diff] [blame] | 494 | if (!browser->filter || !browser->filter(browser, pos)) { |
| 495 | ui_browser__gotorc(browser, row, 0); |
| 496 | browser->write(browser, pos, row); |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 497 | if (++row == browser->rows) |
Arnaldo Carvalho de Melo | 250611c | 2011-10-14 12:27:54 -0300 | [diff] [blame] | 498 | break; |
| 499 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | return row; |
| 503 | } |
| 504 | |
Arnaldo Carvalho de Melo | 7c3102b | 2013-01-18 16:55:52 -0300 | [diff] [blame] | 505 | static struct ui_browser_colorset { |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 506 | const char *name, *fg, *bg; |
| 507 | int colorset; |
| 508 | } ui_browser__colorsets[] = { |
| 509 | { |
| 510 | .colorset = HE_COLORSET_TOP, |
| 511 | .name = "top", |
| 512 | .fg = "red", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 513 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 514 | }, |
| 515 | { |
| 516 | .colorset = HE_COLORSET_MEDIUM, |
| 517 | .name = "medium", |
| 518 | .fg = "green", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 519 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 520 | }, |
| 521 | { |
| 522 | .colorset = HE_COLORSET_NORMAL, |
| 523 | .name = "normal", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 524 | .fg = "default", |
| 525 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 526 | }, |
| 527 | { |
| 528 | .colorset = HE_COLORSET_SELECTED, |
| 529 | .name = "selected", |
| 530 | .fg = "black", |
Ingo Molnar | 8d7d377 | 2015-03-05 11:32:13 +0100 | [diff] [blame] | 531 | .bg = "yellow", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 532 | }, |
| 533 | { |
Taeung Song | 78ce08d | 2016-01-08 17:16:11 +0900 | [diff] [blame] | 534 | .colorset = HE_COLORSET_JUMP_ARROWS, |
| 535 | .name = "jump_arrows", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 536 | .fg = "blue", |
Arnaldo Carvalho de Melo | 82e0af8 | 2011-10-19 00:30:32 -0200 | [diff] [blame] | 537 | .bg = "default", |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 538 | }, |
| 539 | { |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 540 | .colorset = HE_COLORSET_ADDR, |
| 541 | .name = "addr", |
| 542 | .fg = "magenta", |
| 543 | .bg = "default", |
| 544 | }, |
| 545 | { |
Arnaldo Carvalho de Melo | 6692c26 | 2013-03-28 11:34:10 -0300 | [diff] [blame] | 546 | .colorset = HE_COLORSET_ROOT, |
| 547 | .name = "root", |
| 548 | .fg = "white", |
| 549 | .bg = "blue", |
| 550 | }, |
| 551 | { |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 552 | .name = NULL, |
| 553 | } |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 554 | }; |
| 555 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 556 | |
| 557 | static int ui_browser__color_config(const char *var, const char *value, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 558 | void *data __maybe_unused) |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 559 | { |
| 560 | char *fg = NULL, *bg; |
| 561 | int i; |
| 562 | |
| 563 | /* same dir for all commands */ |
| 564 | if (prefixcmp(var, "colors.") != 0) |
| 565 | return 0; |
| 566 | |
| 567 | for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) { |
| 568 | const char *name = var + 7; |
| 569 | |
| 570 | if (strcmp(ui_browser__colorsets[i].name, name) != 0) |
| 571 | continue; |
| 572 | |
| 573 | fg = strdup(value); |
| 574 | if (fg == NULL) |
| 575 | break; |
| 576 | |
| 577 | bg = strchr(fg, ','); |
| 578 | if (bg == NULL) |
| 579 | break; |
| 580 | |
| 581 | *bg = '\0'; |
| 582 | while (isspace(*++bg)); |
| 583 | ui_browser__colorsets[i].bg = bg; |
| 584 | ui_browser__colorsets[i].fg = fg; |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | free(fg); |
| 589 | return -1; |
| 590 | } |
| 591 | |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 592 | void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence) |
| 593 | { |
| 594 | switch (whence) { |
| 595 | case SEEK_SET: |
| 596 | browser->top = browser->entries; |
| 597 | break; |
| 598 | case SEEK_CUR: |
| 599 | browser->top = browser->top + browser->top_idx + offset; |
| 600 | break; |
| 601 | case SEEK_END: |
Arnaldo Carvalho de Melo | 48d038f | 2013-11-14 15:30:41 -0300 | [diff] [blame] | 602 | browser->top = browser->top + browser->nr_entries - 1 + offset; |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 603 | break; |
| 604 | default: |
| 605 | return; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | unsigned int ui_browser__argv_refresh(struct ui_browser *browser) |
| 610 | { |
| 611 | unsigned int row = 0, idx = browser->top_idx; |
| 612 | char **pos; |
| 613 | |
| 614 | if (browser->top == NULL) |
| 615 | browser->top = browser->entries; |
| 616 | |
| 617 | pos = (char **)browser->top; |
| 618 | while (idx < browser->nr_entries) { |
| 619 | if (!browser->filter || !browser->filter(browser, *pos)) { |
| 620 | ui_browser__gotorc(browser, row, 0); |
| 621 | browser->write(browser, pos, row); |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 622 | if (++row == browser->rows) |
Arnaldo Carvalho de Melo | 1056d3d | 2011-10-26 07:11:03 -0200 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | |
| 626 | ++idx; |
| 627 | ++pos; |
| 628 | } |
| 629 | |
| 630 | return row; |
| 631 | } |
| 632 | |
Arnaldo Carvalho de Melo | 4656cca | 2012-05-03 13:07:05 -0300 | [diff] [blame] | 633 | void __ui_browser__vline(struct ui_browser *browser, unsigned int column, |
| 634 | u16 start, u16 end) |
| 635 | { |
| 636 | SLsmg_set_char_set(1); |
| 637 | ui_browser__gotorc(browser, start, column); |
| 638 | SLsmg_draw_vline(end - start + 1); |
| 639 | SLsmg_set_char_set(0); |
| 640 | } |
| 641 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 642 | void ui_browser__write_graph(struct ui_browser *browser __maybe_unused, |
| 643 | int graph) |
Arnaldo Carvalho de Melo | 59d038d | 2012-04-20 16:26:14 -0300 | [diff] [blame] | 644 | { |
| 645 | SLsmg_set_char_set(1); |
| 646 | SLsmg_write_char(graph); |
| 647 | SLsmg_set_char_set(0); |
| 648 | } |
| 649 | |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 650 | static void __ui_browser__line_arrow_up(struct ui_browser *browser, |
| 651 | unsigned int column, |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 652 | u64 start, u64 end) |
Arnaldo Carvalho de Melo | a3f895b | 2012-04-24 14:24:28 -0300 | [diff] [blame] | 653 | { |
| 654 | unsigned int row, end_row; |
| 655 | |
| 656 | SLsmg_set_char_set(1); |
| 657 | |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 658 | if (start < browser->top_idx + browser->rows) { |
Arnaldo Carvalho de Melo | a3f895b | 2012-04-24 14:24:28 -0300 | [diff] [blame] | 659 | row = start - browser->top_idx; |
| 660 | ui_browser__gotorc(browser, row, column); |
| 661 | SLsmg_write_char(SLSMG_LLCORN_CHAR); |
| 662 | ui_browser__gotorc(browser, row, column + 1); |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 663 | SLsmg_draw_hline(2); |
Arnaldo Carvalho de Melo | a3f895b | 2012-04-24 14:24:28 -0300 | [diff] [blame] | 664 | |
| 665 | if (row-- == 0) |
| 666 | goto out; |
| 667 | } else |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 668 | row = browser->rows - 1; |
Arnaldo Carvalho de Melo | a3f895b | 2012-04-24 14:24:28 -0300 | [diff] [blame] | 669 | |
| 670 | if (end > browser->top_idx) |
| 671 | end_row = end - browser->top_idx; |
| 672 | else |
| 673 | end_row = 0; |
| 674 | |
| 675 | ui_browser__gotorc(browser, end_row, column); |
| 676 | SLsmg_draw_vline(row - end_row + 1); |
| 677 | |
| 678 | ui_browser__gotorc(browser, end_row, column); |
| 679 | if (end >= browser->top_idx) { |
| 680 | SLsmg_write_char(SLSMG_ULCORN_CHAR); |
| 681 | ui_browser__gotorc(browser, end_row, column + 1); |
| 682 | SLsmg_write_char(SLSMG_HLINE_CHAR); |
| 683 | ui_browser__gotorc(browser, end_row, column + 2); |
| 684 | SLsmg_write_char(SLSMG_RARROW_CHAR); |
| 685 | } |
| 686 | out: |
| 687 | SLsmg_set_char_set(0); |
| 688 | } |
| 689 | |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 690 | static void __ui_browser__line_arrow_down(struct ui_browser *browser, |
| 691 | unsigned int column, |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 692 | u64 start, u64 end) |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 693 | { |
| 694 | unsigned int row, end_row; |
| 695 | |
| 696 | SLsmg_set_char_set(1); |
| 697 | |
| 698 | if (start >= browser->top_idx) { |
| 699 | row = start - browser->top_idx; |
| 700 | ui_browser__gotorc(browser, row, column); |
| 701 | SLsmg_write_char(SLSMG_ULCORN_CHAR); |
| 702 | ui_browser__gotorc(browser, row, column + 1); |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 703 | SLsmg_draw_hline(2); |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 704 | |
| 705 | if (row++ == 0) |
| 706 | goto out; |
| 707 | } else |
| 708 | row = 0; |
| 709 | |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 710 | if (end >= browser->top_idx + browser->rows) |
| 711 | end_row = browser->rows - 1; |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 712 | else |
Davidlohr Bueso | b222213 | 2013-11-12 22:24:24 -0800 | [diff] [blame] | 713 | end_row = end - browser->top_idx; |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 714 | |
| 715 | ui_browser__gotorc(browser, row, column); |
| 716 | SLsmg_draw_vline(end_row - row + 1); |
| 717 | |
| 718 | ui_browser__gotorc(browser, end_row, column); |
Arnaldo Carvalho de Melo | 62c95ae | 2014-07-01 11:07:54 -0300 | [diff] [blame] | 719 | if (end < browser->top_idx + browser->rows) { |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 720 | SLsmg_write_char(SLSMG_LLCORN_CHAR); |
| 721 | ui_browser__gotorc(browser, end_row, column + 1); |
| 722 | SLsmg_write_char(SLSMG_HLINE_CHAR); |
| 723 | ui_browser__gotorc(browser, end_row, column + 2); |
| 724 | SLsmg_write_char(SLSMG_RARROW_CHAR); |
| 725 | } |
| 726 | out: |
| 727 | SLsmg_set_char_set(0); |
| 728 | } |
| 729 | |
| 730 | void __ui_browser__line_arrow(struct ui_browser *browser, unsigned int column, |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 731 | u64 start, u64 end) |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 732 | { |
| 733 | if (start > end) |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 734 | __ui_browser__line_arrow_up(browser, column, start, end); |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 735 | else |
Arnaldo Carvalho de Melo | 83b1f2a | 2012-05-03 13:12:49 -0300 | [diff] [blame] | 736 | __ui_browser__line_arrow_down(browser, column, start, end); |
Arnaldo Carvalho de Melo | 944e1ab | 2012-04-27 16:27:52 -0300 | [diff] [blame] | 737 | } |
| 738 | |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 739 | void ui_browser__init(void) |
| 740 | { |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 741 | int i = 0; |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 742 | |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 743 | perf_config(ui_browser__color_config, NULL); |
| 744 | |
| 745 | while (ui_browser__colorsets[i].name) { |
Arnaldo Carvalho de Melo | 7c3102b | 2013-01-18 16:55:52 -0300 | [diff] [blame] | 746 | struct ui_browser_colorset *c = &ui_browser__colorsets[i++]; |
Arnaldo Carvalho de Melo | e039fc7 | 2011-10-18 15:50:51 -0200 | [diff] [blame] | 747 | sltt_set_color(c->colorset, c->name, c->fg, c->bg); |
| 748 | } |
Arnaldo Carvalho de Melo | c323cf0 | 2012-05-29 22:06:30 -0300 | [diff] [blame] | 749 | |
| 750 | annotate_browser__init(); |
Arnaldo Carvalho de Melo | ef8f34a | 2010-08-06 17:35:02 -0300 | [diff] [blame] | 751 | } |