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