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