blob: e1d1325545b21661a3c85e438e025b977866d8a3 [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 Lezcanob3e6e812011-06-15 15:45:12 +020066};
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020067
Daniel Lezcano3abd8b12011-03-26 22:06:15 +010068static void display_fini(void)
69{
70 endwin();
71}
72
Daniel Lezcano653cb4a2011-06-21 00:57:08 +020073static int display_show_header(int win)
Daniel Lezcano7b3da502011-06-15 15:45:12 +020074{
75 int i;
76 int curr_pointer = 0;
Daniel Lezcano4120e262011-06-15 15:45:12 +020077 size_t array_size = sizeof(windata) / sizeof(windata[0]);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020078
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 Lezcanoc757e6d2011-06-21 00:57:08 +020083 mvwprintw(header_win, 0, curr_pointer, "PowerDebug %s", VERSION);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020084 curr_pointer += 20;
85
Daniel Lezcano4120e262011-06-15 15:45:12 +020086 for (i = 0; i < array_size; i++) {
Daniel Lezcano7b3da502011-06-15 15:45:12 +020087 if (win == i)
88 wattron(header_win, A_REVERSE);
89 else
90 wattroff(header_win, A_REVERSE);
91
Daniel Lezcanoc757e6d2011-06-21 00:57:08 +020092 mvwprintw(header_win, 0, curr_pointer, " %s ", windata[i].name);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020093 curr_pointer += strlen(windata[i].name) + 2;
94 }
95 wrefresh(header_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +020096
Daniel Lezcano653cb4a2011-06-21 00:57:08 +020097 return 0;
98}
99
100#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \
101 "'Right' , 'Up', 'Down', 'enter', , 'Esc'"
102
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200103static int display_show_footer(int win, char *string)
Daniel Lezcano653cb4a2011-06-21 00:57:08 +0200104{
105 werase(footer_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200106 wattron(footer_win, A_REVERSE);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200107 mvwprintw(footer_win, 0, 0, "%s", string ? string : footer_label);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200108 wattroff(footer_win, A_REVERSE);
109 wrefresh(footer_win);
110
111 return 0;
112}
113
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200114static int display_refresh(int win, bool read)
Amit Arora47fd9182010-08-24 13:26:06 +0530115{
Daniel Lezcanodb145802011-06-21 00:57:08 +0200116 /* we are trying to refresh a window which is not showed */
117 if (win != current_win)
118 return 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200119
Daniel Lezcanodb145802011-06-21 00:57:08 +0200120 if (windata[win].ops && windata[win].ops->display)
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200121 return windata[win].ops->display(read);
Daniel Lezcano971515a2011-06-15 15:45:12 +0200122
123 return 0;
124}
125
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200126int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530127{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200128 int maxx, maxy;
129
130 getmaxyx(stdscr, maxy, maxx);
131
Daniel Lezcanof6656822011-06-15 15:45:12 +0200132 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200133 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200134}
Amit Aroraac4e8652010-11-09 11:16:53 +0530135
Daniel Lezcano28203df2011-06-15 15:45:12 +0200136static int display_show_unselection(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200137{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200138 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200139 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
140 return -1;
141
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200142 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200143}
144
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200145void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200146{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200147 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200148}
149
Daniel Lezcano28203df2011-06-15 15:45:12 +0200150static int display_select(void)
151{
152 if (windata[current_win].ops && windata[current_win].ops->select)
153 return windata[current_win].ops->select();
154
155 return 0;
156}
157
158static int display_next_panel(void)
159{
160 size_t array_size = sizeof(windata) / sizeof(windata[0]);
161
162 current_win++;
163 current_win %= array_size;
164
165 return current_win;
166}
167
168static int display_prev_panel(void)
169{
170 size_t array_size = sizeof(windata) / sizeof(windata[0]);
171
172 current_win--;
173 if (current_win < 0)
174 current_win = array_size - 1;
175
176 return current_win;
177}
178
179static int display_next_line(void)
180{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200181 int maxx, maxy;
Daniel Lezcano28203df2011-06-15 15:45:12 +0200182 int cursor = windata[current_win].cursor;
183 int nrdata = windata[current_win].nrdata;
184 int scrolling = windata[current_win].scrolling;
185 struct rowdata *rowdata = windata[current_win].rowdata;
186
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200187 getmaxyx(stdscr, maxy, maxx);
188
Daniel Lezcano28203df2011-06-15 15:45:12 +0200189 if (cursor >= nrdata)
190 return cursor;
191
192 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
193 if (cursor < nrdata - 1) {
194 if (cursor >= (maxy - 4 + scrolling))
195 scrolling++;
196 cursor++;
197 }
198
199 windata[current_win].scrolling = scrolling;
200 windata[current_win].cursor = cursor;
201
202 return cursor;
203}
204
205static int display_prev_line(void)
206{
207 int cursor = windata[current_win].cursor;
208 int nrdata = windata[current_win].nrdata;
209 int scrolling = windata[current_win].scrolling;
210 struct rowdata *rowdata = windata[current_win].rowdata;
211
212 if (cursor >= nrdata)
213 return cursor;
214
215 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
216 if (cursor > 0) {
217 if (cursor <= scrolling)
218 scrolling--;
219 cursor--;
220 }
221
222 windata[current_win].scrolling = scrolling;
223 windata[current_win].cursor = cursor;
224
225 return cursor;
226}
227
228static int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200229{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200230 struct rowdata *rowdata = windata[win].rowdata;
231
232 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200233 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
234 if (!rowdata)
235 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200236 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200237 }
238
239 rowdata[line].data = data;
240 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200241 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200242
243 return 0;
244}
245
Daniel Lezcanof6656822011-06-15 15:45:12 +0200246int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200247{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200248 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200249 werase(windata[win].pad);
250 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200251}
252
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200253int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200254{
255 int attr = 0;
256
Amit Arora031263a2010-11-09 11:12:41 +0530257 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200258 attr |= WA_BOLD;
259
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200260 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200261 attr |= WA_STANDOUT;
262
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200263 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200264 return -1;
265
266 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200267 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200268
Daniel Lezcanof6656822011-06-15 15:45:12 +0200269 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200270
271 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200272 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200273
274 return 0;
275}
276
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200277static int display_find_keystroke(int fd, void *data);
278
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200279struct find_data {
280 size_t len;
281 char *string;
282 regex_t *reg;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200283 int ocursor;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200284};
285
286struct find_data *display_find_form_init(void)
287{
288 const char *regexp = "^[a-z|0-9|_|-|.]";
289 struct find_data *findd;
290 const size_t len = 64;
291 regex_t *reg;
292 char *search4;
293 int maxx, maxy;
294
295 getmaxyx(stdscr, maxy, maxx);
296
297 reg = malloc(sizeof(*reg));
298 if (!reg)
299 return NULL;
300
301 if (regcomp(reg, regexp, REG_ICASE))
302 goto out_free_reg;
303
304 search4 = malloc(len);
305 if (!search4)
306 goto out_free_regcomp;
307 memset(search4, '\0', len);
308
309 findd = malloc(sizeof(*findd));
310 if (!findd)
311 goto out_free_search4;
312
313 findd->string = search4;
314 findd->reg = reg;
315 findd->len = len;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200316
317 /* save the location of the cursor on the main window in order to
318 * browse the search result
319 */
320 findd->ocursor = windata[current_win].cursor;
321 windata[current_win].cursor = 0;
322
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200323out:
324 return findd;
325
326out_free_search4:
327 free(search4);
328out_free_regcomp:
329 regfree(reg);
330out_free_reg:
331 free(reg);
332
333 goto out;
334}
335
336static void display_find_form_fini(struct find_data *fd)
337{
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200338 windata[current_win].cursor = fd->ocursor;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200339 regfree(fd->reg);
340 free(fd->string);
341 free(fd);
342 curs_set(0);
343}
344
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200345static int display_switch_to_find(int fd)
346{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200347 struct find_data *findd;
348
349 findd = display_find_form_init();
350 if (!findd)
351 return -1;
352
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200353 if (mainloop_del(fd))
354 return -1;
355
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200356 if (mainloop_add(fd, display_find_keystroke, findd))
357 return -1;
358
359 if (display_show_footer(current_win, "find (esc to exit)?"))
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200360 return -1;
361
362 return 0;
363}
364
Daniel Lezcanodb145802011-06-21 00:57:08 +0200365static int display_keystroke(int fd, void *data)
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200366{
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200367 int keystroke = getch();
368
369 switch (keystroke) {
370
371 case KEY_RIGHT:
372 case '\t':
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200373 display_show_header(display_next_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200374 break;
375
376 case KEY_LEFT:
377 case KEY_BTAB:
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200378 display_show_header(display_prev_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200379 break;
380
381 case KEY_DOWN:
382 display_next_line();
383 break;
384
385 case KEY_UP:
386 display_prev_line();
387 break;
388
389 case '\r':
390 display_select();
391 break;
392
393 case EOF:
394 case 'q':
395 case 'Q':
396 return 1;
397
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200398 case '/':
399 return display_switch_to_find(fd);
400
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200401 case 'r':
402 case 'R':
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200403 return display_refresh(current_win, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200404 default:
405 return 0;
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200406 }
407
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200408 display_refresh(current_win, false);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200409
410 return 0;
411}
412
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200413static int display_switch_to_main(int fd)
414{
415 if (mainloop_del(fd))
416 return -1;
417
418 if (mainloop_add(fd, display_keystroke, NULL))
419 return -1;
420
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200421 if (display_show_header(current_win))
422 return -1;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200423
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200424 if (display_show_footer(current_win, NULL))
425 return -1;
426
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200427 return display_refresh(current_win, false);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200428}
429
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200430static int display_find_keystroke(int fd, void *data)
431{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200432 struct find_data *findd = data;
433 regex_t *reg = findd->reg;
434 char *string = findd->string;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200435 int keystroke = getch();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200436 char match[2] = { [0] = (char)keystroke, [1] = '\0' };
437 regmatch_t m[1];
438
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200439 switch (keystroke) {
440
441 case '\e':
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200442 display_find_form_fini(findd);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200443 return display_switch_to_main(fd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200444
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200445 case KEY_DOWN:
446 display_next_line();
447 break;
448
449 case KEY_UP:
450 display_prev_line();
451 break;
452
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200453 case KEY_BACKSPACE:
454 if (strlen(string))
455 string[strlen(string) - 1] = '\0';
456 break;
457
Daniel Lezcano73b40022011-06-21 00:57:08 +0200458 case '\r':
459 if (!windata[current_win].ops || !windata[current_win].ops->selectf)
460 return 0;
461
462 if (windata[current_win].ops->selectf())
463 return -1;
464
465 return 0;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200466
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200467 default:
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200468
469 /* We don't want invalid characters for a name */
470 if (regexec(reg, match, 1, m, 0))
471 return 0;
472
473 if (strlen(string) < findd->len - 1)
474 string[strlen(string)] = (char)keystroke;
475
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200476 break;
477 }
478
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200479 if (!windata[current_win].ops || !windata[current_win].ops->find)
480 return 0;
481
482 if (windata[current_win].ops->find(string))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200483 return -1;
484
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200485 if (display_show_header(current_win))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200486 return -1;
487
488 if (display_show_footer(current_win, strlen(string) ? string :
489 "find (esc to exit)?"))
490 return -1;
491
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200492 return 0;
493}
494
Daniel Lezcanodb145802011-06-21 00:57:08 +0200495int display_init(int wdefault)
496{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200497 int i, maxx, maxy;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200498 size_t array_size = sizeof(windata) / sizeof(windata[0]);
499
500 current_win = wdefault;
501
502 if (mainloop_add(0, display_keystroke, NULL))
503 return -1;
504
505 if (!initscr())
506 return -1;
507
508 start_color();
509 use_default_colors();
510
511 keypad(stdscr, TRUE);
512 noecho();
513 cbreak();
514 curs_set(0);
515 nonl();
516
517 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
518 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
519 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
520 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
521 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
522 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
523 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
524 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
525 return -1;
526
527 if (atexit(display_fini))
528 return -1;
529
530 getmaxyx(stdscr, maxy, maxx);
531
532 for (i = 0; i < array_size; i++) {
533
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200534 main_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
535 if (!main_win)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200536 return -1;
537
538 windata[i].pad = newpad(maxrows, maxx);
539 if (!windata[i].pad)
540 return -1;
541
542 }
543
544 header_win = subwin(stdscr, 1, maxx, 0, 0);
545 if (!header_win)
546 return -1;
547
548 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
549 if (!footer_win)
550 return -1;
551
Daniel Lezcano653cb4a2011-06-21 00:57:08 +0200552 if (display_show_header(wdefault))
553 return -1;
554
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200555 if (display_show_footer(wdefault, NULL))
Daniel Lezcanodb145802011-06-21 00:57:08 +0200556 return -1;
557
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200558 return display_refresh(wdefault, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200559}
560
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200561int display_column_name(const char *line)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200562{
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200563 werase(main_win);
564 wattron(main_win, A_BOLD);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200565 mvwprintw(main_win, 0, 0, "%s", line);
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200566 wattroff(main_win, A_BOLD);
567 wrefresh(main_win);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200568
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200569 return 0;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200570}
571
572int display_register(int win, struct display_ops *ops)
573{
574 size_t array_size = sizeof(windata) / sizeof(windata[0]);
575
576 if (win < 0 || win >= array_size)
577 return -1;
578
579 windata[win].ops = ops;
580
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200581 return 0;
582}