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 | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 306 | static int display_find_keystroke(int fd, void *data); |
| 307 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 308 | struct find_data { |
| 309 | size_t len; |
| 310 | char *string; |
| 311 | regex_t *reg; |
Daniel Lezcano | 96a64fb | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 312 | int ocursor; |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 313 | int oscrolling; |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 314 | }; |
| 315 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 316 | struct find_data *display_find_init(void) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 317 | { |
| 318 | const char *regexp = "^[a-z|0-9|_|-|.]"; |
| 319 | struct find_data *findd; |
| 320 | const size_t len = 64; |
| 321 | regex_t *reg; |
| 322 | char *search4; |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 323 | |
| 324 | reg = malloc(sizeof(*reg)); |
| 325 | if (!reg) |
| 326 | return NULL; |
| 327 | |
| 328 | if (regcomp(reg, regexp, REG_ICASE)) |
| 329 | goto out_free_reg; |
| 330 | |
| 331 | search4 = malloc(len); |
| 332 | if (!search4) |
| 333 | goto out_free_regcomp; |
| 334 | memset(search4, '\0', len); |
| 335 | |
| 336 | findd = malloc(sizeof(*findd)); |
| 337 | if (!findd) |
| 338 | goto out_free_search4; |
| 339 | |
| 340 | findd->string = search4; |
| 341 | findd->reg = reg; |
| 342 | findd->len = len; |
Daniel Lezcano | 96a64fb | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 343 | |
| 344 | /* save the location of the cursor on the main window in order to |
| 345 | * browse the search result |
| 346 | */ |
| 347 | findd->ocursor = windata[current_win].cursor; |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 348 | findd->oscrolling = windata[current_win].scrolling; |
Daniel Lezcano | 96a64fb | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 349 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 350 | windata[current_win].cursor = 0; |
| 351 | windata[current_win].scrolling = 0; |
| 352 | |
| 353 | curs_set(1); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 354 | out: |
| 355 | return findd; |
| 356 | |
| 357 | out_free_search4: |
| 358 | free(search4); |
| 359 | out_free_regcomp: |
| 360 | regfree(reg); |
| 361 | out_free_reg: |
| 362 | free(reg); |
| 363 | |
| 364 | goto out; |
| 365 | } |
| 366 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 367 | static void display_find_fini(struct find_data *findd) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 368 | { |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 369 | windata[current_win].cursor = findd->ocursor; |
| 370 | windata[current_win].scrolling = findd->oscrolling; |
| 371 | regfree(findd->reg); |
| 372 | free(findd->string); |
| 373 | free(findd); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 374 | curs_set(0); |
| 375 | } |
| 376 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 377 | static int display_switch_to_find(int fd) |
| 378 | { |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 379 | struct find_data *findd; |
| 380 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 381 | findd = display_find_init(); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 382 | if (!findd) |
| 383 | return -1; |
| 384 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 385 | if (mainloop_del(fd)) |
| 386 | return -1; |
| 387 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 388 | if (mainloop_add(fd, display_find_keystroke, findd)) |
| 389 | return -1; |
| 390 | |
| 391 | if (display_show_footer(current_win, "find (esc to exit)?")) |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 392 | return -1; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 397 | static int display_keystroke(int fd, void *data) |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 398 | { |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 399 | int keystroke = getch(); |
| 400 | |
| 401 | switch (keystroke) { |
| 402 | |
| 403 | case KEY_RIGHT: |
| 404 | case '\t': |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 405 | display_show_header(display_next_panel()); |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 406 | break; |
| 407 | |
| 408 | case KEY_LEFT: |
| 409 | case KEY_BTAB: |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 410 | display_show_header(display_prev_panel()); |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 411 | break; |
| 412 | |
| 413 | case KEY_DOWN: |
| 414 | display_next_line(); |
| 415 | break; |
| 416 | |
| 417 | case KEY_UP: |
| 418 | display_prev_line(); |
| 419 | break; |
| 420 | |
Shaojie Sun | b32cbb9 | 2013-07-17 16:23:45 +0800 | [diff] [blame] | 421 | case '\n': |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 422 | case '\r': |
| 423 | display_select(); |
| 424 | break; |
| 425 | |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 426 | case 'v': |
| 427 | case 'V': |
| 428 | case 'd': |
| 429 | case 'D': |
| 430 | display_change(toupper(keystroke)); |
| 431 | break; |
| 432 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 433 | case EOF: |
| 434 | case 'q': |
| 435 | case 'Q': |
| 436 | return 1; |
| 437 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 438 | case '/': |
| 439 | return display_switch_to_find(fd); |
| 440 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 441 | case 'r': |
| 442 | case 'R': |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 443 | return display_refresh(current_win, true); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 444 | default: |
| 445 | return 0; |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 448 | display_refresh(current_win, false); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 453 | static int display_switch_to_main(int fd) |
| 454 | { |
| 455 | if (mainloop_del(fd)) |
| 456 | return -1; |
| 457 | |
| 458 | if (mainloop_add(fd, display_keystroke, NULL)) |
| 459 | return -1; |
| 460 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 461 | if (display_show_header(current_win)) |
| 462 | return -1; |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 463 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 464 | if (display_show_footer(current_win, NULL)) |
| 465 | return -1; |
| 466 | |
Daniel Lezcano | d577aaa | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 467 | return display_refresh(current_win, false); |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 470 | static int display_find_keystroke(int fd, void *data) |
| 471 | { |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 472 | struct find_data *findd = data; |
| 473 | regex_t *reg = findd->reg; |
| 474 | char *string = findd->string; |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 475 | int keystroke = getch(); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 476 | char match[2] = { [0] = (char)keystroke, [1] = '\0' }; |
| 477 | regmatch_t m[1]; |
| 478 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 479 | switch (keystroke) { |
| 480 | |
| 481 | case '\e': |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 482 | display_find_fini(findd); |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 483 | return display_switch_to_main(fd); |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 484 | |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 485 | case KEY_DOWN: |
| 486 | display_next_line(); |
| 487 | break; |
| 488 | |
| 489 | case KEY_UP: |
| 490 | display_prev_line(); |
| 491 | break; |
| 492 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 493 | case KEY_BACKSPACE: |
| 494 | if (strlen(string)) |
| 495 | string[strlen(string) - 1] = '\0'; |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 496 | |
| 497 | windata[current_win].cursor = 0; |
| 498 | windata[current_win].scrolling = 0; |
| 499 | |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 500 | break; |
| 501 | |
Shaojie Sun | b32cbb9 | 2013-07-17 16:23:45 +0800 | [diff] [blame] | 502 | case '\n': |
Daniel Lezcano | 73b4002 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 503 | case '\r': |
| 504 | if (!windata[current_win].ops || !windata[current_win].ops->selectf) |
| 505 | return 0; |
| 506 | |
| 507 | if (windata[current_win].ops->selectf()) |
| 508 | return -1; |
| 509 | |
Daniel Lezcano | e78bc07 | 2011-06-21 10:49:39 +0200 | [diff] [blame] | 510 | windata[current_win].cursor = 0; |
| 511 | windata[current_win].scrolling = 0; |
| 512 | |
Daniel Lezcano | 73b4002 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 513 | return 0; |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 514 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 515 | default: |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 516 | |
| 517 | /* We don't want invalid characters for a name */ |
| 518 | if (regexec(reg, match, 1, m, 0)) |
| 519 | return 0; |
| 520 | |
| 521 | if (strlen(string) < findd->len - 1) |
| 522 | string[strlen(string)] = (char)keystroke; |
| 523 | |
Daniel Lezcano | 10c8645 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 524 | windata[current_win].cursor = 0; |
| 525 | windata[current_win].scrolling = 0; |
| 526 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 530 | if (!windata[current_win].ops || !windata[current_win].ops->find) |
| 531 | return 0; |
| 532 | |
| 533 | if (windata[current_win].ops->find(string)) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 534 | return -1; |
| 535 | |
Daniel Lezcano | a12163d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 536 | if (display_show_header(current_win)) |
Daniel Lezcano | 5000abd | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 537 | return -1; |
| 538 | |
| 539 | if (display_show_footer(current_win, strlen(string) ? string : |
| 540 | "find (esc to exit)?")) |
| 541 | return -1; |
| 542 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Daniel Lezcano | 5e659d7 | 2016-02-19 20:22:12 +0000 | [diff] [blame] | 546 | int display_init(struct powerdebug_options *options) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 547 | { |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 548 | int i, maxx, maxy; |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 549 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 550 | |
Daniel Lezcano | 5e659d7 | 2016-02-19 20:22:12 +0000 | [diff] [blame] | 551 | current_win = 1 << (ffs(options->flags & DEFAULT_OPTION) - 1); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 552 | |
Daniel Lezcano | 3109e16 | 2016-02-19 21:28:23 +0000 | [diff] [blame] | 553 | signal(SIGWINCH, sigwinch_handler); |
| 554 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 555 | if (mainloop_add(0, display_keystroke, NULL)) |
| 556 | return -1; |
| 557 | |
| 558 | if (!initscr()) |
| 559 | return -1; |
| 560 | |
| 561 | start_color(); |
| 562 | use_default_colors(); |
| 563 | |
| 564 | keypad(stdscr, TRUE); |
| 565 | noecho(); |
| 566 | cbreak(); |
| 567 | curs_set(0); |
| 568 | nonl(); |
| 569 | |
| 570 | if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) || |
| 571 | init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) || |
| 572 | init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) || |
| 573 | init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) || |
| 574 | init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) || |
| 575 | init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) || |
| 576 | init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) || |
| 577 | init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED)) |
| 578 | return -1; |
| 579 | |
| 580 | if (atexit(display_fini)) |
| 581 | return -1; |
| 582 | |
| 583 | getmaxyx(stdscr, maxy, maxx); |
| 584 | |
| 585 | for (i = 0; i < array_size; i++) { |
| 586 | |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 587 | main_win = subwin(stdscr, maxy - 2, maxx, 1, 0); |
| 588 | if (!main_win) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 589 | return -1; |
| 590 | |
| 591 | windata[i].pad = newpad(maxrows, maxx); |
| 592 | if (!windata[i].pad) |
| 593 | return -1; |
| 594 | |
| 595 | } |
| 596 | |
| 597 | header_win = subwin(stdscr, 1, maxx, 0, 0); |
| 598 | if (!header_win) |
| 599 | return -1; |
| 600 | |
| 601 | footer_win = subwin(stdscr, 1, maxx, maxy-1, 0); |
| 602 | if (!footer_win) |
| 603 | return -1; |
| 604 | |
Daniel Lezcano | 5e659d7 | 2016-02-19 20:22:12 +0000 | [diff] [blame] | 605 | if (display_show_header(current_win)) |
Daniel Lezcano | 653cb4a | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 606 | return -1; |
| 607 | |
Daniel Lezcano | 5e659d7 | 2016-02-19 20:22:12 +0000 | [diff] [blame] | 608 | if (display_show_footer(current_win, NULL)) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 609 | return -1; |
| 610 | |
Daniel Lezcano | 5e659d7 | 2016-02-19 20:22:12 +0000 | [diff] [blame] | 611 | return display_refresh(current_win, true); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 614 | int display_column_name(const char *line) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 615 | { |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 616 | werase(main_win); |
| 617 | wattron(main_win, A_BOLD); |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 618 | mvwprintw(main_win, 0, 0, "%s", line); |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 619 | wattroff(main_win, A_BOLD); |
| 620 | wrefresh(main_win); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 621 | |
Daniel Lezcano | 372ffba | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 622 | return 0; |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | int display_register(int win, struct display_ops *ops) |
| 626 | { |
| 627 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 628 | |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 629 | if (win < 0 || win >= array_size) { |
| 630 | printf("error: invalid window"); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 631 | return -1; |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 632 | } |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 633 | |
| 634 | windata[win].ops = ops; |
| 635 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 636 | return 0; |
| 637 | } |