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