Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Power debug tool (powerdebug) |
Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 3 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016, Linaro Limited. |
Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 5 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 10 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | */ |
Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 21 | |
Daniel Lezcano | 1562748 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 25 | #include <ctype.h> |
Daniel Lezcano | 1562748 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 26 | #include <ncurses.h> |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <regex.h> |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 29 | #include "powerdebug.h" |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 30 | #include "mainloop.h" |
Amit Arora | ed3e565 | 2010-10-27 12:02:53 +0530 | [diff] [blame] | 31 | #include "display.h" |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 32 | |
Daniel Lezcano | eeb1376 | 2011-03-26 22:06:17 +0100 | [diff] [blame] | 33 | enum { PT_COLOR_DEFAULT = 1, |
| 34 | PT_COLOR_HEADER_BAR, |
| 35 | PT_COLOR_ERROR, |
| 36 | PT_COLOR_RED, |
| 37 | PT_COLOR_YELLOW, |
| 38 | PT_COLOR_GREEN, |
| 39 | PT_COLOR_BRIGHT, |
| 40 | PT_COLOR_BLUE, |
| 41 | }; |
| 42 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 43 | static WINDOW *header_win; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 44 | static WINDOW *footer_win; |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 45 | static WINDOW *main_win; |
Daniel Lezcano | d96731a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 46 | static int current_win; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 47 | |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 48 | /* Number of lines in the virtual window */ |
| 49 | static const int maxrows = 1024; |
| 50 | |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 51 | struct rowdata { |
| 52 | int attr; |
| 53 | void *data; |
| 54 | }; |
| 55 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 56 | struct windata { |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 57 | WINDOW *pad; |
Daniel Lezcano | b301b08 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 58 | struct display_ops *ops; |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 59 | struct rowdata *rowdata; |
| 60 | char *name; |
| 61 | int nrdata; |
| 62 | int scrolling; |
| 63 | int cursor; |
| 64 | }; |
| 65 | |
Daniel Lezcano | 4120e26 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 66 | /* Warning this is linked with the enum { CLOCK, REGULATOR, ... } */ |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 67 | struct windata windata[] = { |
Daniel Lezcano | 4120e26 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 68 | [CLOCK] = { .name = "Clocks" }, |
| 69 | [REGULATOR] = { .name = "Regulators" }, |
| 70 | [SENSOR] = { .name = "Sensors" }, |
Daniel Lezcano | 269de4f | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 71 | [GPIO] = { .name = "Gpio" }, |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 72 | }; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 73 | |
Daniel Lezcano | 3abd8b1 | 2011-03-26 22:06:15 +0100 | [diff] [blame] | 74 | static void display_fini(void) |
| 75 | { |
| 76 | endwin(); |
| 77 | } |
| 78 | |
Daniel Lezcano | 653cb4a | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 79 | static int display_show_header(int win) |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 80 | { |
| 81 | int i; |
| 82 | int curr_pointer = 0; |
Daniel Lezcano | 4120e26 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 83 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 84 | |
| 85 | wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR)); |
| 86 | wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR)); |
| 87 | werase(header_win); |
| 88 | |
Daniel Lezcano | c757e6d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 89 | mvwprintw(header_win, 0, curr_pointer, "PowerDebug %s", VERSION); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 90 | curr_pointer += 20; |
| 91 | |
Daniel Lezcano | 4120e26 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 92 | for (i = 0; i < array_size; i++) { |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 93 | if (win == i) |
| 94 | wattron(header_win, A_REVERSE); |
| 95 | else |
| 96 | wattroff(header_win, A_REVERSE); |
| 97 | |
Daniel Lezcano | c757e6d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 98 | mvwprintw(header_win, 0, curr_pointer, " %s ", windata[i].name); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 99 | curr_pointer += strlen(windata[i].name) + 2; |
| 100 | } |
| 101 | wrefresh(header_win); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 102 | |
Daniel Lezcano | 653cb4a | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | #define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ |
| 107 | "'Right' , 'Up', 'Down', 'enter', , 'Esc'" |
| 108 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 109 | static int display_show_footer(int win, char *string) |
Daniel Lezcano | 653cb4a | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 110 | { |
| 111 | werase(footer_win); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 112 | wattron(footer_win, A_REVERSE); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 113 | mvwprintw(footer_win, 0, 0, "%s", string ? string : footer_label); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 114 | wattroff(footer_win, A_REVERSE); |
| 115 | wrefresh(footer_win); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 120 | static int display_refresh(int win, bool read) |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 121 | { |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 122 | /* we are trying to refresh a window which is not showed */ |
| 123 | if (win != current_win) |
| 124 | return 0; |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 125 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 126 | if (windata[win].ops && windata[win].ops->display) |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 127 | return windata[win].ops->display(read); |
Daniel Lezcano | 971515a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 128 | |
Daniel Lezcano | caafece | 2011-06-27 22:59:17 +0200 | [diff] [blame] | 129 | if (werase(main_win)) |
| 130 | return -1; |
| 131 | |
| 132 | return wrefresh(main_win); |
Daniel Lezcano | 971515a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 135 | int display_refresh_pad(int win) |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 136 | { |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 137 | int maxx, maxy; |
| 138 | |
| 139 | getmaxyx(stdscr, maxy, maxx); |
| 140 | |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 141 | return prefresh(windata[win].pad, windata[win].scrolling, |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 142 | 0, 2, 0, maxy - 2, maxx); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 143 | } |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 144 | |
Sanjay Singh Rawat | 83b37c3 | 2013-04-17 14:57:07 +0530 | [diff] [blame] | 145 | void sigwinch_handler(int signo) |
| 146 | { |
| 147 | display_refresh(current_win, true); |
| 148 | } |
| 149 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 150 | static int display_show_unselection(int win, int line, bool bold) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 151 | { |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 152 | if (mvwchgat(windata[win].pad, line, 0, -1, |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 153 | bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) |
| 154 | return -1; |
| 155 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 156 | return display_refresh_pad(win); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 159 | void *display_get_row_data(int win) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 160 | { |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 161 | return windata[win].rowdata[windata[win].cursor].data; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 164 | static int display_select(void) |
| 165 | { |
| 166 | if (windata[current_win].ops && windata[current_win].ops->select) |
| 167 | return windata[current_win].ops->select(); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 172 | static int display_change(int keyvalue) |
| 173 | { |
| 174 | if (!(windata[current_win].nrdata)) |
| 175 | return 0; |
| 176 | |
| 177 | if (windata[current_win].ops && windata[current_win].ops->change) |
| 178 | return windata[current_win].ops->change(keyvalue); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 183 | static int display_next_panel(void) |
| 184 | { |
| 185 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 186 | |
| 187 | current_win++; |
| 188 | current_win %= array_size; |
| 189 | |
| 190 | return current_win; |
| 191 | } |
| 192 | |
| 193 | static int display_prev_panel(void) |
| 194 | { |
| 195 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 196 | |
| 197 | current_win--; |
| 198 | if (current_win < 0) |
| 199 | current_win = array_size - 1; |
| 200 | |
| 201 | return current_win; |
| 202 | } |
| 203 | |
| 204 | static int display_next_line(void) |
| 205 | { |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 206 | int maxy; |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 207 | int cursor = windata[current_win].cursor; |
| 208 | int nrdata = windata[current_win].nrdata; |
| 209 | int scrolling = windata[current_win].scrolling; |
| 210 | struct rowdata *rowdata = windata[current_win].rowdata; |
| 211 | |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 212 | maxy = getmaxy(stdscr); |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 213 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 214 | if (cursor >= nrdata) |
| 215 | return cursor; |
| 216 | |
| 217 | display_show_unselection(current_win, cursor, rowdata[cursor].attr); |
| 218 | if (cursor < nrdata - 1) { |
| 219 | if (cursor >= (maxy - 4 + scrolling)) |
| 220 | scrolling++; |
| 221 | cursor++; |
| 222 | } |
| 223 | |
| 224 | windata[current_win].scrolling = scrolling; |
| 225 | windata[current_win].cursor = cursor; |
| 226 | |
| 227 | return cursor; |
| 228 | } |
| 229 | |
| 230 | static int display_prev_line(void) |
| 231 | { |
| 232 | int cursor = windata[current_win].cursor; |
| 233 | int nrdata = windata[current_win].nrdata; |
| 234 | int scrolling = windata[current_win].scrolling; |
| 235 | struct rowdata *rowdata = windata[current_win].rowdata; |
| 236 | |
| 237 | if (cursor >= nrdata) |
| 238 | return cursor; |
| 239 | |
| 240 | display_show_unselection(current_win, cursor, rowdata[cursor].attr); |
| 241 | if (cursor > 0) { |
| 242 | if (cursor <= scrolling) |
| 243 | scrolling--; |
| 244 | cursor--; |
| 245 | } |
| 246 | |
| 247 | windata[current_win].scrolling = scrolling; |
| 248 | windata[current_win].cursor = cursor; |
| 249 | |
| 250 | return cursor; |
| 251 | } |
| 252 | |
| 253 | static int display_set_row_data(int win, int line, void *data, int attr) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 254 | { |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 255 | struct rowdata *rowdata = windata[win].rowdata; |
| 256 | |
| 257 | if (line >= windata[win].nrdata) { |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 258 | rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1)); |
| 259 | if (!rowdata) |
| 260 | return -1; |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 261 | windata[win].nrdata = line + 1; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | rowdata[line].data = data; |
| 265 | rowdata[line].attr = attr; |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 266 | windata[win].rowdata = rowdata; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 271 | int display_reset_cursor(int win) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 272 | { |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 273 | windata[win].nrdata = 0; |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 274 | werase(windata[win].pad); |
| 275 | return wmove(windata[win].pad, 0, 0); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Sanjay Singh Rawat | 96f6e05 | 2014-05-26 11:35:02 +0530 | [diff] [blame] | 278 | void display_message(int win, char *buf) |
| 279 | { |
| 280 | display_reset_cursor(win); |
| 281 | wattron(windata[win].pad, WA_BOLD); |
| 282 | wprintw(windata[win].pad, "%s\n", buf); |
| 283 | wattroff(windata[win].pad, WA_BOLD); |
| 284 | display_refresh_pad(win); |
| 285 | } |
| 286 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 287 | int display_print_line(int win, int line, char *str, int bold, void *data) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 288 | { |
| 289 | int attr = 0; |
| 290 | |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 291 | if (bold) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 292 | attr |= WA_BOLD; |
| 293 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 294 | if (line == windata[win].cursor) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 295 | attr |= WA_STANDOUT; |
| 296 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 297 | if (display_set_row_data(win, line, data, attr)) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 298 | return -1; |
| 299 | |
| 300 | if (attr) |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 301 | wattron(windata[win].pad, attr); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 302 | |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 303 | wprintw(windata[win].pad, "%s\n", str); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 304 | |
| 305 | if (attr) |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 306 | wattroff(windata[win].pad, attr); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 311 | static int display_find_keystroke(int fd, void *data); |
| 312 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 313 | struct find_data { |
| 314 | size_t len; |
| 315 | char *string; |
| 316 | regex_t *reg; |
Daniel Lezcano | 96a64fb | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 317 | int ocursor; |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 318 | int oscrolling; |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 319 | }; |
| 320 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 321 | struct find_data *display_find_init(void) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 322 | { |
| 323 | const char *regexp = "^[a-z|0-9|_|-|.]"; |
| 324 | struct find_data *findd; |
| 325 | const size_t len = 64; |
| 326 | regex_t *reg; |
| 327 | char *search4; |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 328 | |
| 329 | reg = malloc(sizeof(*reg)); |
| 330 | if (!reg) |
| 331 | return NULL; |
| 332 | |
| 333 | if (regcomp(reg, regexp, REG_ICASE)) |
| 334 | goto out_free_reg; |
| 335 | |
| 336 | search4 = malloc(len); |
| 337 | if (!search4) |
| 338 | goto out_free_regcomp; |
| 339 | memset(search4, '\0', len); |
| 340 | |
| 341 | findd = malloc(sizeof(*findd)); |
| 342 | if (!findd) |
| 343 | goto out_free_search4; |
| 344 | |
| 345 | findd->string = search4; |
| 346 | findd->reg = reg; |
| 347 | findd->len = len; |
Daniel Lezcano | 96a64fb | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 348 | |
| 349 | /* save the location of the cursor on the main window in order to |
| 350 | * browse the search result |
| 351 | */ |
| 352 | findd->ocursor = windata[current_win].cursor; |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 353 | findd->oscrolling = windata[current_win].scrolling; |
Daniel Lezcano | 96a64fb | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 354 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 355 | windata[current_win].cursor = 0; |
| 356 | windata[current_win].scrolling = 0; |
| 357 | |
| 358 | curs_set(1); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 359 | out: |
| 360 | return findd; |
| 361 | |
| 362 | out_free_search4: |
| 363 | free(search4); |
| 364 | out_free_regcomp: |
| 365 | regfree(reg); |
| 366 | out_free_reg: |
| 367 | free(reg); |
| 368 | |
| 369 | goto out; |
| 370 | } |
| 371 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 372 | static void display_find_fini(struct find_data *findd) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 373 | { |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 374 | windata[current_win].cursor = findd->ocursor; |
| 375 | windata[current_win].scrolling = findd->oscrolling; |
| 376 | regfree(findd->reg); |
| 377 | free(findd->string); |
| 378 | free(findd); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 379 | curs_set(0); |
| 380 | } |
| 381 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 382 | static int display_switch_to_find(int fd) |
| 383 | { |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 384 | struct find_data *findd; |
| 385 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 386 | findd = display_find_init(); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 387 | if (!findd) |
| 388 | return -1; |
| 389 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 390 | if (mainloop_del(fd)) |
| 391 | return -1; |
| 392 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 393 | if (mainloop_add(fd, display_find_keystroke, findd)) |
| 394 | return -1; |
| 395 | |
| 396 | if (display_show_footer(current_win, "find (esc to exit)?")) |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 397 | return -1; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 402 | static int display_keystroke(int fd, void *data) |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 403 | { |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 404 | int keystroke = getch(); |
| 405 | |
| 406 | switch (keystroke) { |
| 407 | |
| 408 | case KEY_RIGHT: |
| 409 | case '\t': |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 410 | display_show_header(display_next_panel()); |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 411 | break; |
| 412 | |
| 413 | case KEY_LEFT: |
| 414 | case KEY_BTAB: |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 415 | display_show_header(display_prev_panel()); |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | case KEY_DOWN: |
| 419 | display_next_line(); |
| 420 | break; |
| 421 | |
| 422 | case KEY_UP: |
| 423 | display_prev_line(); |
| 424 | break; |
| 425 | |
Shaojie Sun | b32cbb9 | 2013-07-17 16:23:45 +0800 | [diff] [blame] | 426 | case '\n': |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 427 | case '\r': |
| 428 | display_select(); |
| 429 | break; |
| 430 | |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 431 | case 'v': |
| 432 | case 'V': |
| 433 | case 'd': |
| 434 | case 'D': |
| 435 | display_change(toupper(keystroke)); |
| 436 | break; |
| 437 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 438 | case EOF: |
| 439 | case 'q': |
| 440 | case 'Q': |
| 441 | return 1; |
| 442 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 443 | case '/': |
| 444 | return display_switch_to_find(fd); |
| 445 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 446 | case 'r': |
| 447 | case 'R': |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 448 | return display_refresh(current_win, true); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 449 | default: |
| 450 | return 0; |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 453 | display_refresh(current_win, false); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 458 | static int display_switch_to_main(int fd) |
| 459 | { |
| 460 | if (mainloop_del(fd)) |
| 461 | return -1; |
| 462 | |
| 463 | if (mainloop_add(fd, display_keystroke, NULL)) |
| 464 | return -1; |
| 465 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 466 | if (display_show_header(current_win)) |
| 467 | return -1; |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 468 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 469 | if (display_show_footer(current_win, NULL)) |
| 470 | return -1; |
| 471 | |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 472 | return display_refresh(current_win, false); |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 475 | static int display_find_keystroke(int fd, void *data) |
| 476 | { |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 477 | struct find_data *findd = data; |
| 478 | regex_t *reg = findd->reg; |
| 479 | char *string = findd->string; |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 480 | int keystroke = getch(); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 481 | char match[2] = { [0] = (char)keystroke, [1] = '\0' }; |
| 482 | regmatch_t m[1]; |
| 483 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 484 | switch (keystroke) { |
| 485 | |
| 486 | case '\e': |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 487 | display_find_fini(findd); |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 488 | return display_switch_to_main(fd); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 489 | |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 490 | case KEY_DOWN: |
| 491 | display_next_line(); |
| 492 | break; |
| 493 | |
| 494 | case KEY_UP: |
| 495 | display_prev_line(); |
| 496 | break; |
| 497 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 498 | case KEY_BACKSPACE: |
| 499 | if (strlen(string)) |
| 500 | string[strlen(string) - 1] = '\0'; |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 501 | |
| 502 | windata[current_win].cursor = 0; |
| 503 | windata[current_win].scrolling = 0; |
| 504 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 505 | break; |
| 506 | |
Shaojie Sun | b32cbb9 | 2013-07-17 16:23:45 +0800 | [diff] [blame] | 507 | case '\n': |
Daniel Lezcano | 73b4002 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 508 | case '\r': |
| 509 | if (!windata[current_win].ops || !windata[current_win].ops->selectf) |
| 510 | return 0; |
| 511 | |
| 512 | if (windata[current_win].ops->selectf()) |
| 513 | return -1; |
| 514 | |
Daniel Lezcano | e78bc07 | 2011-06-21 10:49:39 +0200 | [diff] [blame] | 515 | windata[current_win].cursor = 0; |
| 516 | windata[current_win].scrolling = 0; |
| 517 | |
Daniel Lezcano | 73b4002 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 518 | return 0; |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 519 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 520 | default: |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 521 | |
| 522 | /* We don't want invalid characters for a name */ |
| 523 | if (regexec(reg, match, 1, m, 0)) |
| 524 | return 0; |
| 525 | |
| 526 | if (strlen(string) < findd->len - 1) |
| 527 | string[strlen(string)] = (char)keystroke; |
| 528 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 529 | windata[current_win].cursor = 0; |
| 530 | windata[current_win].scrolling = 0; |
| 531 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 535 | if (!windata[current_win].ops || !windata[current_win].ops->find) |
| 536 | return 0; |
| 537 | |
| 538 | if (windata[current_win].ops->find(string)) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 539 | return -1; |
| 540 | |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 541 | if (display_show_header(current_win)) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 542 | return -1; |
| 543 | |
| 544 | if (display_show_footer(current_win, strlen(string) ? string : |
| 545 | "find (esc to exit)?")) |
| 546 | return -1; |
| 547 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 551 | int display_init(int wdefault) |
| 552 | { |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 553 | int i, maxx, maxy; |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 554 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 555 | |
| 556 | current_win = wdefault; |
| 557 | |
| 558 | if (mainloop_add(0, display_keystroke, NULL)) |
| 559 | return -1; |
| 560 | |
| 561 | if (!initscr()) |
| 562 | return -1; |
| 563 | |
| 564 | start_color(); |
| 565 | use_default_colors(); |
| 566 | |
| 567 | keypad(stdscr, TRUE); |
| 568 | noecho(); |
| 569 | cbreak(); |
| 570 | curs_set(0); |
| 571 | nonl(); |
| 572 | |
| 573 | if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) || |
| 574 | init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) || |
| 575 | init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) || |
| 576 | init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) || |
| 577 | init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) || |
| 578 | init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) || |
| 579 | init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) || |
| 580 | init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED)) |
| 581 | return -1; |
| 582 | |
| 583 | if (atexit(display_fini)) |
| 584 | return -1; |
| 585 | |
| 586 | getmaxyx(stdscr, maxy, maxx); |
| 587 | |
| 588 | for (i = 0; i < array_size; i++) { |
| 589 | |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 590 | main_win = subwin(stdscr, maxy - 2, maxx, 1, 0); |
| 591 | if (!main_win) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 592 | return -1; |
| 593 | |
| 594 | windata[i].pad = newpad(maxrows, maxx); |
| 595 | if (!windata[i].pad) |
| 596 | return -1; |
| 597 | |
| 598 | } |
| 599 | |
| 600 | header_win = subwin(stdscr, 1, maxx, 0, 0); |
| 601 | if (!header_win) |
| 602 | return -1; |
| 603 | |
| 604 | footer_win = subwin(stdscr, 1, maxx, maxy-1, 0); |
| 605 | if (!footer_win) |
| 606 | return -1; |
| 607 | |
Daniel Lezcano | 653cb4a | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 608 | if (display_show_header(wdefault)) |
| 609 | return -1; |
| 610 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 611 | if (display_show_footer(wdefault, NULL)) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 612 | return -1; |
| 613 | |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 614 | return display_refresh(wdefault, true); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 617 | int display_column_name(const char *line) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 618 | { |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 619 | werase(main_win); |
| 620 | wattron(main_win, A_BOLD); |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 621 | mvwprintw(main_win, 0, 0, "%s", line); |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 622 | wattroff(main_win, A_BOLD); |
| 623 | wrefresh(main_win); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 624 | |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 625 | return 0; |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | int display_register(int win, struct display_ops *ops) |
| 629 | { |
| 630 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 631 | |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 632 | if (win < 0 || win >= array_size) { |
| 633 | printf("error: invalid window"); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 634 | return -1; |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 635 | } |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 636 | |
| 637 | windata[win].ops = ops; |
| 638 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 639 | return 0; |
| 640 | } |