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