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