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