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