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