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