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