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> |
| 19 | #include <ncurses.h> |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame^] | 20 | #include <form.h> |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 21 | #include "powerdebug.h" |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 22 | #include "mainloop.h" |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 23 | #include "regulator.h" |
Amit Arora | ed3e565 | 2010-10-27 12:02:53 +0530 | [diff] [blame] | 24 | #include "display.h" |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 25 | |
Daniel Lezcano | eeb1376 | 2011-03-26 22:06:17 +0100 | [diff] [blame] | 26 | enum { PT_COLOR_DEFAULT = 1, |
| 27 | PT_COLOR_HEADER_BAR, |
| 28 | PT_COLOR_ERROR, |
| 29 | PT_COLOR_RED, |
| 30 | PT_COLOR_YELLOW, |
| 31 | PT_COLOR_GREEN, |
| 32 | PT_COLOR_BRIGHT, |
| 33 | PT_COLOR_BLUE, |
| 34 | }; |
| 35 | |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 36 | static WINDOW *header_win; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 37 | static WINDOW *footer_win; |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 38 | static WINDOW *main_win; |
Daniel Lezcano | d96731a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 39 | static int current_win; |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 40 | |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 41 | /* Number of lines in the virtual window */ |
| 42 | static const int maxrows = 1024; |
| 43 | |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 44 | #define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ |
| 45 | "'Right' , 'Up', 'Down', 'enter', , 'Esc'" |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 46 | |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 47 | struct rowdata { |
| 48 | int attr; |
| 49 | void *data; |
| 50 | }; |
| 51 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 52 | struct windata { |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 53 | WINDOW *pad; |
Daniel Lezcano | b301b08 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 54 | struct display_ops *ops; |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 55 | struct rowdata *rowdata; |
| 56 | char *name; |
| 57 | int nrdata; |
| 58 | int scrolling; |
| 59 | int cursor; |
| 60 | }; |
| 61 | |
Daniel Lezcano | 4120e26 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 62 | /* Warning this is linked with the enum { CLOCK, REGULATOR, ... } */ |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 63 | struct windata windata[] = { |
Daniel Lezcano | 4120e26 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 64 | [CLOCK] = { .name = "Clocks" }, |
| 65 | [REGULATOR] = { .name = "Regulators" }, |
| 66 | [SENSOR] = { .name = "Sensors" }, |
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 | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 74 | static int show_header_footer(int win) |
| 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); |
| 97 | werase(footer_win); |
| 98 | |
| 99 | wattron(footer_win, A_REVERSE); |
Daniel Lezcano | c757e6d | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 100 | mvwprintw(footer_win, 0, 0, "%s", footer_label); |
Daniel Lezcano | 7b3da50 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 101 | wattroff(footer_win, A_REVERSE); |
| 102 | wrefresh(footer_win); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 107 | int display_refresh(int win) |
Amit Arora | 47fd918 | 2010-08-24 13:26:06 +0530 | [diff] [blame] | 108 | { |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 109 | /* we are trying to refresh a window which is not showed */ |
| 110 | if (win != current_win) |
| 111 | return 0; |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 112 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 113 | if (windata[win].ops && windata[win].ops->display) |
| 114 | return windata[win].ops->display(); |
Daniel Lezcano | 971515a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 119 | int display_refresh_pad(int win) |
Amit Arora | 728e0c9 | 2010-09-14 12:06:09 +0530 | [diff] [blame] | 120 | { |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 121 | int maxx, maxy; |
| 122 | |
| 123 | getmaxyx(stdscr, maxy, maxx); |
| 124 | |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 125 | return prefresh(windata[win].pad, windata[win].scrolling, |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 126 | 0, 2, 0, maxy - 2, maxx); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 127 | } |
Amit Arora | ac4e865 | 2010-11-09 11:16:53 +0530 | [diff] [blame] | 128 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 129 | static int display_show_unselection(int win, int line, bool bold) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 130 | { |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 131 | if (mvwchgat(windata[win].pad, line, 0, -1, |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 132 | bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) |
| 133 | return -1; |
| 134 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 135 | return display_refresh_pad(win); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 138 | void *display_get_row_data(int win) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 139 | { |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 140 | return windata[win].rowdata[windata[win].cursor].data; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 143 | static int display_select(void) |
| 144 | { |
| 145 | if (windata[current_win].ops && windata[current_win].ops->select) |
| 146 | return windata[current_win].ops->select(); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int display_next_panel(void) |
| 152 | { |
| 153 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 154 | |
| 155 | current_win++; |
| 156 | current_win %= array_size; |
| 157 | |
| 158 | return current_win; |
| 159 | } |
| 160 | |
| 161 | static int display_prev_panel(void) |
| 162 | { |
| 163 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 164 | |
| 165 | current_win--; |
| 166 | if (current_win < 0) |
| 167 | current_win = array_size - 1; |
| 168 | |
| 169 | return current_win; |
| 170 | } |
| 171 | |
| 172 | static int display_next_line(void) |
| 173 | { |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 174 | int maxx, maxy; |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 175 | int cursor = windata[current_win].cursor; |
| 176 | int nrdata = windata[current_win].nrdata; |
| 177 | int scrolling = windata[current_win].scrolling; |
| 178 | struct rowdata *rowdata = windata[current_win].rowdata; |
| 179 | |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 180 | getmaxyx(stdscr, maxy, maxx); |
| 181 | |
Daniel Lezcano | 28203df | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 182 | if (cursor >= nrdata) |
| 183 | return cursor; |
| 184 | |
| 185 | display_show_unselection(current_win, cursor, rowdata[cursor].attr); |
| 186 | if (cursor < nrdata - 1) { |
| 187 | if (cursor >= (maxy - 4 + scrolling)) |
| 188 | scrolling++; |
| 189 | cursor++; |
| 190 | } |
| 191 | |
| 192 | windata[current_win].scrolling = scrolling; |
| 193 | windata[current_win].cursor = cursor; |
| 194 | |
| 195 | return cursor; |
| 196 | } |
| 197 | |
| 198 | static int display_prev_line(void) |
| 199 | { |
| 200 | int cursor = windata[current_win].cursor; |
| 201 | int nrdata = windata[current_win].nrdata; |
| 202 | int scrolling = windata[current_win].scrolling; |
| 203 | struct rowdata *rowdata = windata[current_win].rowdata; |
| 204 | |
| 205 | if (cursor >= nrdata) |
| 206 | return cursor; |
| 207 | |
| 208 | display_show_unselection(current_win, cursor, rowdata[cursor].attr); |
| 209 | if (cursor > 0) { |
| 210 | if (cursor <= scrolling) |
| 211 | scrolling--; |
| 212 | cursor--; |
| 213 | } |
| 214 | |
| 215 | windata[current_win].scrolling = scrolling; |
| 216 | windata[current_win].cursor = cursor; |
| 217 | |
| 218 | return cursor; |
| 219 | } |
| 220 | |
| 221 | 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] | 222 | { |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 223 | struct rowdata *rowdata = windata[win].rowdata; |
| 224 | |
| 225 | if (line >= windata[win].nrdata) { |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 226 | rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1)); |
| 227 | if (!rowdata) |
| 228 | return -1; |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 229 | windata[win].nrdata = line + 1; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | rowdata[line].data = data; |
| 233 | rowdata[line].attr = attr; |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 234 | windata[win].rowdata = rowdata; |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 239 | int display_reset_cursor(int win) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 240 | { |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 241 | windata[win].nrdata = 0; |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 242 | werase(windata[win].pad); |
| 243 | return wmove(windata[win].pad, 0, 0); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | int attr = 0; |
| 249 | |
Amit Arora | 031263a | 2010-11-09 11:12:41 +0530 | [diff] [blame] | 250 | if (bold) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 251 | attr |= WA_BOLD; |
| 252 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 253 | if (line == windata[win].cursor) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 254 | attr |= WA_STANDOUT; |
| 255 | |
Daniel Lezcano | b3e6e81 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 256 | if (display_set_row_data(win, line, data, attr)) |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 257 | return -1; |
| 258 | |
| 259 | if (attr) |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 260 | wattron(windata[win].pad, attr); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 261 | |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 262 | wprintw(windata[win].pad, "%s\n", str); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 263 | |
| 264 | if (attr) |
Daniel Lezcano | f665682 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 265 | wattroff(windata[win].pad, attr); |
Daniel Lezcano | 2adc48d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame^] | 270 | static int display_find_keystroke(int fd, void *data); |
| 271 | |
| 272 | static int display_switch_to_find(int fd) |
| 273 | { |
| 274 | if (mainloop_del(fd)) |
| 275 | return -1; |
| 276 | |
| 277 | if (mainloop_add(fd, display_find_keystroke, NULL)) |
| 278 | return -1; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 283 | static int display_keystroke(int fd, void *data) |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 284 | { |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 285 | int keystroke = getch(); |
| 286 | |
| 287 | switch (keystroke) { |
| 288 | |
| 289 | case KEY_RIGHT: |
| 290 | case '\t': |
| 291 | display_next_panel(); |
| 292 | break; |
| 293 | |
| 294 | case KEY_LEFT: |
| 295 | case KEY_BTAB: |
| 296 | display_prev_panel(); |
| 297 | break; |
| 298 | |
| 299 | case KEY_DOWN: |
| 300 | display_next_line(); |
| 301 | break; |
| 302 | |
| 303 | case KEY_UP: |
| 304 | display_prev_line(); |
| 305 | break; |
| 306 | |
| 307 | case '\r': |
| 308 | display_select(); |
| 309 | break; |
| 310 | |
| 311 | case EOF: |
| 312 | case 'q': |
| 313 | case 'Q': |
| 314 | return 1; |
| 315 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame^] | 316 | case '/': |
| 317 | return display_switch_to_find(fd); |
| 318 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 319 | case 'r': |
| 320 | case 'R': |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 321 | /* refresh will be done after */ |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 322 | break; |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 323 | default: |
| 324 | return 0; |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 327 | display_refresh(current_win); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Daniel Lezcano | e64c48e | 2011-06-21 00:57:08 +0200 | [diff] [blame^] | 332 | static int display_switch_to_main(int fd) |
| 333 | { |
| 334 | if (mainloop_del(fd)) |
| 335 | return -1; |
| 336 | |
| 337 | if (mainloop_add(fd, display_keystroke, NULL)) |
| 338 | return -1; |
| 339 | |
| 340 | display_refresh(current_win); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static int display_find_keystroke(int fd, void *data) |
| 347 | { |
| 348 | int keystroke = getch(); |
| 349 | |
| 350 | switch (keystroke) { |
| 351 | |
| 352 | case '\e': |
| 353 | return display_switch_to_main(fd); |
| 354 | default: |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 361 | int display_init(int wdefault) |
| 362 | { |
Daniel Lezcano | 0a8cc58 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 363 | int i, maxx, maxy; |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 364 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 365 | |
| 366 | current_win = wdefault; |
| 367 | |
| 368 | if (mainloop_add(0, display_keystroke, NULL)) |
| 369 | return -1; |
| 370 | |
| 371 | if (!initscr()) |
| 372 | return -1; |
| 373 | |
| 374 | start_color(); |
| 375 | use_default_colors(); |
| 376 | |
| 377 | keypad(stdscr, TRUE); |
| 378 | noecho(); |
| 379 | cbreak(); |
| 380 | curs_set(0); |
| 381 | nonl(); |
| 382 | |
| 383 | if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) || |
| 384 | init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) || |
| 385 | init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) || |
| 386 | init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) || |
| 387 | init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) || |
| 388 | init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) || |
| 389 | init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) || |
| 390 | init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED)) |
| 391 | return -1; |
| 392 | |
| 393 | if (atexit(display_fini)) |
| 394 | return -1; |
| 395 | |
| 396 | getmaxyx(stdscr, maxy, maxx); |
| 397 | |
| 398 | for (i = 0; i < array_size; i++) { |
| 399 | |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 400 | main_win = subwin(stdscr, maxy - 2, maxx, 1, 0); |
| 401 | if (!main_win) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 402 | return -1; |
| 403 | |
| 404 | windata[i].pad = newpad(maxrows, maxx); |
| 405 | if (!windata[i].pad) |
| 406 | return -1; |
| 407 | |
| 408 | } |
| 409 | |
| 410 | header_win = subwin(stdscr, 1, maxx, 0, 0); |
| 411 | if (!header_win) |
| 412 | return -1; |
| 413 | |
| 414 | footer_win = subwin(stdscr, 1, maxx, maxy-1, 0); |
| 415 | if (!footer_win) |
| 416 | return -1; |
| 417 | |
| 418 | if (show_header_footer(wdefault)) |
| 419 | return -1; |
| 420 | |
| 421 | return display_refresh(wdefault); |
| 422 | } |
| 423 | |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 424 | int display_header_footer(int win, const char *line) |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 425 | { |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 426 | werase(main_win); |
| 427 | wattron(main_win, A_BOLD); |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 428 | mvwprintw(main_win, 0, 0, "%s", line); |
Daniel Lezcano | c196d43 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 429 | wattroff(main_win, A_BOLD); |
| 430 | wrefresh(main_win); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 431 | |
Daniel Lezcano | fa45333 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 432 | return show_header_footer(win); |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | int display_register(int win, struct display_ops *ops) |
| 436 | { |
| 437 | size_t array_size = sizeof(windata) / sizeof(windata[0]); |
| 438 | |
| 439 | if (win < 0 || win >= array_size) |
| 440 | return -1; |
| 441 | |
| 442 | windata[win].ops = ops; |
| 443 | |
Daniel Lezcano | 176e69d | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 444 | return 0; |
| 445 | } |