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