blob: f06387ccfd350fe11ac04db348f02aa9abe15bac [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora39f29542010-09-14 12:03:22 +05303 *
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 Lezcano15627482011-06-15 15:45:12 +020016#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
Daniel Lezcanoa12163d2011-06-21 00:57:08 +020019#include <ctype.h>
Daniel Lezcano15627482011-06-15 15:45:12 +020020#include <ncurses.h>
Daniel Lezcano5000abd2011-06-21 00:57:08 +020021#include <sys/types.h>
22#include <regex.h>
Amit Arora47fd9182010-08-24 13:26:06 +053023#include "powerdebug.h"
Daniel Lezcanodb145802011-06-21 00:57:08 +020024#include "mainloop.h"
Amit Arora17552782010-12-02 12:23:14 +053025#include "regulator.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053026#include "display.h"
Amit Arora47fd9182010-08-24 13:26:06 +053027
Daniel Lezcanoeeb13762011-03-26 22:06:17 +010028enum { 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 Arora47fd9182010-08-24 13:26:06 +053038static WINDOW *header_win;
Amit Arora47fd9182010-08-24 13:26:06 +053039static WINDOW *footer_win;
Daniel Lezcanoc196d432011-06-21 00:57:08 +020040static WINDOW *main_win;
Daniel Lezcanod96731a2011-06-15 15:45:12 +020041static int current_win;
Amit Arora47fd9182010-08-24 13:26:06 +053042
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020043/* Number of lines in the virtual window */
44static const int maxrows = 1024;
45
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020046struct rowdata {
47 int attr;
48 void *data;
49};
50
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020051struct windata {
Daniel Lezcanof6656822011-06-15 15:45:12 +020052 WINDOW *pad;
Daniel Lezcanob301b082011-06-15 15:45:12 +020053 struct display_ops *ops;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020054 struct rowdata *rowdata;
55 char *name;
56 int nrdata;
57 int scrolling;
58 int cursor;
59};
60
Daniel Lezcano4120e262011-06-15 15:45:12 +020061/* Warning this is linked with the enum { CLOCK, REGULATOR, ... } */
Daniel Lezcano176e69d2011-06-15 15:45:12 +020062struct windata windata[] = {
Daniel Lezcano4120e262011-06-15 15:45:12 +020063 [CLOCK] = { .name = "Clocks" },
64 [REGULATOR] = { .name = "Regulators" },
65 [SENSOR] = { .name = "Sensors" },
Daniel Lezcano269de4f2011-08-25 15:46:13 +020066 [GPIO] = { .name = "Gpio" },
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020067};
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020068
Daniel Lezcano3abd8b12011-03-26 22:06:15 +010069static void display_fini(void)
70{
71 endwin();
72}
73
Daniel Lezcano653cb4a2011-06-21 00:57:08 +020074static int display_show_header(int win)
Daniel Lezcano7b3da502011-06-15 15:45:12 +020075{
76 int i;
77 int curr_pointer = 0;
Daniel Lezcano4120e262011-06-15 15:45:12 +020078 size_t array_size = sizeof(windata) / sizeof(windata[0]);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020079
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 Lezcanoc757e6d2011-06-21 00:57:08 +020084 mvwprintw(header_win, 0, curr_pointer, "PowerDebug %s", VERSION);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020085 curr_pointer += 20;
86
Daniel Lezcano4120e262011-06-15 15:45:12 +020087 for (i = 0; i < array_size; i++) {
Daniel Lezcano7b3da502011-06-15 15:45:12 +020088 if (win == i)
89 wattron(header_win, A_REVERSE);
90 else
91 wattroff(header_win, A_REVERSE);
92
Daniel Lezcanoc757e6d2011-06-21 00:57:08 +020093 mvwprintw(header_win, 0, curr_pointer, " %s ", windata[i].name);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020094 curr_pointer += strlen(windata[i].name) + 2;
95 }
96 wrefresh(header_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020097
Daniel Lezcano653cb4a2011-06-21 00:57:08 +020098 return 0;
99}
100
101#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \
102 "'Right' , 'Up', 'Down', 'enter', , 'Esc'"
103
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200104static int display_show_footer(int win, char *string)
Daniel Lezcano653cb4a2011-06-21 00:57:08 +0200105{
106 werase(footer_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200107 wattron(footer_win, A_REVERSE);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200108 mvwprintw(footer_win, 0, 0, "%s", string ? string : footer_label);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200109 wattroff(footer_win, A_REVERSE);
110 wrefresh(footer_win);
111
112 return 0;
113}
114
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200115static int display_refresh(int win, bool read)
Amit Arora47fd9182010-08-24 13:26:06 +0530116{
Daniel Lezcanodb145802011-06-21 00:57:08 +0200117 /* we are trying to refresh a window which is not showed */
118 if (win != current_win)
119 return 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200120
Daniel Lezcanodb145802011-06-21 00:57:08 +0200121 if (windata[win].ops && windata[win].ops->display)
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200122 return windata[win].ops->display(read);
Daniel Lezcano971515a2011-06-15 15:45:12 +0200123
Daniel Lezcanocaafece2011-06-27 22:59:17 +0200124 if (werase(main_win))
125 return -1;
126
127 return wrefresh(main_win);
Daniel Lezcano971515a2011-06-15 15:45:12 +0200128}
129
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200130int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530131{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200132 int maxx, maxy;
133
134 getmaxyx(stdscr, maxy, maxx);
135
Daniel Lezcanof6656822011-06-15 15:45:12 +0200136 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200137 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200138}
Amit Aroraac4e8652010-11-09 11:16:53 +0530139
Daniel Lezcano28203df2011-06-15 15:45:12 +0200140static int display_show_unselection(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200141{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200142 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200143 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
144 return -1;
145
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200146 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200147}
148
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200149void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200150{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200151 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200152}
153
Daniel Lezcano28203df2011-06-15 15:45:12 +0200154static 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
162static 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
172static 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
183static int display_next_line(void)
184{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200185 int maxx, maxy;
Daniel Lezcano28203df2011-06-15 15:45:12 +0200186 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 Lezcano0a8cc582011-06-21 00:57:08 +0200191 getmaxyx(stdscr, maxy, maxx);
192
Daniel Lezcano28203df2011-06-15 15:45:12 +0200193 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
209static 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
232static int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200233{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200234 struct rowdata *rowdata = windata[win].rowdata;
235
236 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200237 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
238 if (!rowdata)
239 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200240 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200241 }
242
243 rowdata[line].data = data;
244 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200245 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200246
247 return 0;
248}
249
Daniel Lezcanof6656822011-06-15 15:45:12 +0200250int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200251{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200252 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200253 werase(windata[win].pad);
254 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200255}
256
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200257int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200258{
259 int attr = 0;
260
Amit Arora031263a2010-11-09 11:12:41 +0530261 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200262 attr |= WA_BOLD;
263
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200264 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200265 attr |= WA_STANDOUT;
266
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200267 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200268 return -1;
269
270 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200271 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200272
Daniel Lezcanof6656822011-06-15 15:45:12 +0200273 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200274
275 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200276 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200277
278 return 0;
279}
280
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200281static int display_find_keystroke(int fd, void *data);
282
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200283struct find_data {
284 size_t len;
285 char *string;
286 regex_t *reg;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200287 int ocursor;
Daniel Lezcano10c86452011-06-21 00:57:08 +0200288 int oscrolling;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200289};
290
Daniel Lezcano10c86452011-06-21 00:57:08 +0200291struct find_data *display_find_init(void)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200292{
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 Lezcano96a64fb2011-06-21 00:57:08 +0200321
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 Lezcano10c86452011-06-21 00:57:08 +0200326 findd->oscrolling = windata[current_win].scrolling;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200327
Daniel Lezcano10c86452011-06-21 00:57:08 +0200328 windata[current_win].cursor = 0;
329 windata[current_win].scrolling = 0;
330
331 curs_set(1);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200332out:
333 return findd;
334
335out_free_search4:
336 free(search4);
337out_free_regcomp:
338 regfree(reg);
339out_free_reg:
340 free(reg);
341
342 goto out;
343}
344
Daniel Lezcano10c86452011-06-21 00:57:08 +0200345static void display_find_fini(struct find_data *findd)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200346{
Daniel Lezcano10c86452011-06-21 00:57:08 +0200347 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 Lezcano5000abd2011-06-21 00:57:08 +0200352 curs_set(0);
353}
354
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200355static int display_switch_to_find(int fd)
356{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200357 struct find_data *findd;
358
Daniel Lezcano10c86452011-06-21 00:57:08 +0200359 findd = display_find_init();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200360 if (!findd)
361 return -1;
362
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200363 if (mainloop_del(fd))
364 return -1;
365
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200366 if (mainloop_add(fd, display_find_keystroke, findd))
367 return -1;
368
369 if (display_show_footer(current_win, "find (esc to exit)?"))
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200370 return -1;
371
372 return 0;
373}
374
Daniel Lezcanodb145802011-06-21 00:57:08 +0200375static int display_keystroke(int fd, void *data)
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200376{
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200377 int keystroke = getch();
378
379 switch (keystroke) {
380
381 case KEY_RIGHT:
382 case '\t':
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200383 display_show_header(display_next_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200384 break;
385
386 case KEY_LEFT:
387 case KEY_BTAB:
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200388 display_show_header(display_prev_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200389 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 Lezcanoe64c48e2011-06-21 00:57:08 +0200408 case '/':
409 return display_switch_to_find(fd);
410
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200411 case 'r':
412 case 'R':
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200413 return display_refresh(current_win, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200414 default:
415 return 0;
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200416 }
417
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200418 display_refresh(current_win, false);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200419
420 return 0;
421}
422
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200423static 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 Lezcano5000abd2011-06-21 00:57:08 +0200431 if (display_show_header(current_win))
432 return -1;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200433
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200434 if (display_show_footer(current_win, NULL))
435 return -1;
436
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200437 return display_refresh(current_win, false);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200438}
439
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200440static int display_find_keystroke(int fd, void *data)
441{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200442 struct find_data *findd = data;
443 regex_t *reg = findd->reg;
444 char *string = findd->string;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200445 int keystroke = getch();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200446 char match[2] = { [0] = (char)keystroke, [1] = '\0' };
447 regmatch_t m[1];
448
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200449 switch (keystroke) {
450
451 case '\e':
Daniel Lezcano10c86452011-06-21 00:57:08 +0200452 display_find_fini(findd);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200453 return display_switch_to_main(fd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200454
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200455 case KEY_DOWN:
456 display_next_line();
457 break;
458
459 case KEY_UP:
460 display_prev_line();
461 break;
462
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200463 case KEY_BACKSPACE:
464 if (strlen(string))
465 string[strlen(string) - 1] = '\0';
Daniel Lezcano10c86452011-06-21 00:57:08 +0200466
467 windata[current_win].cursor = 0;
468 windata[current_win].scrolling = 0;
469
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200470 break;
471
Daniel Lezcano73b40022011-06-21 00:57:08 +0200472 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 Lezcanoe78bc072011-06-21 10:49:39 +0200479 windata[current_win].cursor = 0;
480 windata[current_win].scrolling = 0;
481
Daniel Lezcano73b40022011-06-21 00:57:08 +0200482 return 0;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200483
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200484 default:
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200485
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 Lezcano10c86452011-06-21 00:57:08 +0200493 windata[current_win].cursor = 0;
494 windata[current_win].scrolling = 0;
495
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200496 break;
497 }
498
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200499 if (!windata[current_win].ops || !windata[current_win].ops->find)
500 return 0;
501
502 if (windata[current_win].ops->find(string))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200503 return -1;
504
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200505 if (display_show_header(current_win))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200506 return -1;
507
508 if (display_show_footer(current_win, strlen(string) ? string :
509 "find (esc to exit)?"))
510 return -1;
511
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200512 return 0;
513}
514
Daniel Lezcanodb145802011-06-21 00:57:08 +0200515int display_init(int wdefault)
516{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200517 int i, maxx, maxy;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200518 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 Lezcanoc196d432011-06-21 00:57:08 +0200554 main_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
555 if (!main_win)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200556 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 Lezcano653cb4a2011-06-21 00:57:08 +0200572 if (display_show_header(wdefault))
573 return -1;
574
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200575 if (display_show_footer(wdefault, NULL))
Daniel Lezcanodb145802011-06-21 00:57:08 +0200576 return -1;
577
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200578 return display_refresh(wdefault, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200579}
580
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200581int display_column_name(const char *line)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200582{
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200583 werase(main_win);
584 wattron(main_win, A_BOLD);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200585 mvwprintw(main_win, 0, 0, "%s", line);
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200586 wattroff(main_win, A_BOLD);
587 wrefresh(main_win);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200588
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200589 return 0;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200590}
591
592int 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 Lezcano176e69d2011-06-15 15:45:12 +0200601 return 0;
602}