blob: 8f442c19c57ea14f54edbe0f3d51e834354150fd [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 Lezcano10c86452011-06-21 00:57:08 +0200284 int oscrolling;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200285};
286
Daniel Lezcano10c86452011-06-21 00:57:08 +0200287struct find_data *display_find_init(void)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200288{
289 const char *regexp = "^[a-z|0-9|_|-|.]";
290 struct find_data *findd;
291 const size_t len = 64;
292 regex_t *reg;
293 char *search4;
294 int maxx, maxy;
295
296 getmaxyx(stdscr, maxy, maxx);
297
298 reg = malloc(sizeof(*reg));
299 if (!reg)
300 return NULL;
301
302 if (regcomp(reg, regexp, REG_ICASE))
303 goto out_free_reg;
304
305 search4 = malloc(len);
306 if (!search4)
307 goto out_free_regcomp;
308 memset(search4, '\0', len);
309
310 findd = malloc(sizeof(*findd));
311 if (!findd)
312 goto out_free_search4;
313
314 findd->string = search4;
315 findd->reg = reg;
316 findd->len = len;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200317
318 /* save the location of the cursor on the main window in order to
319 * browse the search result
320 */
321 findd->ocursor = windata[current_win].cursor;
Daniel Lezcano10c86452011-06-21 00:57:08 +0200322 findd->oscrolling = windata[current_win].scrolling;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200323
Daniel Lezcano10c86452011-06-21 00:57:08 +0200324 windata[current_win].cursor = 0;
325 windata[current_win].scrolling = 0;
326
327 curs_set(1);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200328out:
329 return findd;
330
331out_free_search4:
332 free(search4);
333out_free_regcomp:
334 regfree(reg);
335out_free_reg:
336 free(reg);
337
338 goto out;
339}
340
Daniel Lezcano10c86452011-06-21 00:57:08 +0200341static void display_find_fini(struct find_data *findd)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200342{
Daniel Lezcano10c86452011-06-21 00:57:08 +0200343 windata[current_win].cursor = findd->ocursor;
344 windata[current_win].scrolling = findd->oscrolling;
345 regfree(findd->reg);
346 free(findd->string);
347 free(findd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200348 curs_set(0);
349}
350
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200351static int display_switch_to_find(int fd)
352{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200353 struct find_data *findd;
354
Daniel Lezcano10c86452011-06-21 00:57:08 +0200355 findd = display_find_init();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200356 if (!findd)
357 return -1;
358
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200359 if (mainloop_del(fd))
360 return -1;
361
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200362 if (mainloop_add(fd, display_find_keystroke, findd))
363 return -1;
364
365 if (display_show_footer(current_win, "find (esc to exit)?"))
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200366 return -1;
367
368 return 0;
369}
370
Daniel Lezcanodb145802011-06-21 00:57:08 +0200371static int display_keystroke(int fd, void *data)
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200372{
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200373 int keystroke = getch();
374
375 switch (keystroke) {
376
377 case KEY_RIGHT:
378 case '\t':
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200379 display_show_header(display_next_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200380 break;
381
382 case KEY_LEFT:
383 case KEY_BTAB:
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200384 display_show_header(display_prev_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200385 break;
386
387 case KEY_DOWN:
388 display_next_line();
389 break;
390
391 case KEY_UP:
392 display_prev_line();
393 break;
394
395 case '\r':
396 display_select();
397 break;
398
399 case EOF:
400 case 'q':
401 case 'Q':
402 return 1;
403
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200404 case '/':
405 return display_switch_to_find(fd);
406
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200407 case 'r':
408 case 'R':
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200409 return display_refresh(current_win, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200410 default:
411 return 0;
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200412 }
413
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200414 display_refresh(current_win, false);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200415
416 return 0;
417}
418
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200419static int display_switch_to_main(int fd)
420{
421 if (mainloop_del(fd))
422 return -1;
423
424 if (mainloop_add(fd, display_keystroke, NULL))
425 return -1;
426
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200427 if (display_show_header(current_win))
428 return -1;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200429
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200430 if (display_show_footer(current_win, NULL))
431 return -1;
432
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200433 return display_refresh(current_win, false);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200434}
435
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200436static int display_find_keystroke(int fd, void *data)
437{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200438 struct find_data *findd = data;
439 regex_t *reg = findd->reg;
440 char *string = findd->string;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200441 int keystroke = getch();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200442 char match[2] = { [0] = (char)keystroke, [1] = '\0' };
443 regmatch_t m[1];
444
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200445 switch (keystroke) {
446
447 case '\e':
Daniel Lezcano10c86452011-06-21 00:57:08 +0200448 display_find_fini(findd);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200449 return display_switch_to_main(fd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200450
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200451 case KEY_DOWN:
452 display_next_line();
453 break;
454
455 case KEY_UP:
456 display_prev_line();
457 break;
458
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200459 case KEY_BACKSPACE:
460 if (strlen(string))
461 string[strlen(string) - 1] = '\0';
Daniel Lezcano10c86452011-06-21 00:57:08 +0200462
463 windata[current_win].cursor = 0;
464 windata[current_win].scrolling = 0;
465
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200466 break;
467
Daniel Lezcano73b40022011-06-21 00:57:08 +0200468 case '\r':
469 if (!windata[current_win].ops || !windata[current_win].ops->selectf)
470 return 0;
471
472 if (windata[current_win].ops->selectf())
473 return -1;
474
475 return 0;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200476
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200477 default:
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200478
479 /* We don't want invalid characters for a name */
480 if (regexec(reg, match, 1, m, 0))
481 return 0;
482
483 if (strlen(string) < findd->len - 1)
484 string[strlen(string)] = (char)keystroke;
485
Daniel Lezcano10c86452011-06-21 00:57:08 +0200486 windata[current_win].cursor = 0;
487 windata[current_win].scrolling = 0;
488
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200489 break;
490 }
491
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200492 if (!windata[current_win].ops || !windata[current_win].ops->find)
493 return 0;
494
495 if (windata[current_win].ops->find(string))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200496 return -1;
497
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200498 if (display_show_header(current_win))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200499 return -1;
500
501 if (display_show_footer(current_win, strlen(string) ? string :
502 "find (esc to exit)?"))
503 return -1;
504
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200505 return 0;
506}
507
Daniel Lezcanodb145802011-06-21 00:57:08 +0200508int display_init(int wdefault)
509{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200510 int i, maxx, maxy;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200511 size_t array_size = sizeof(windata) / sizeof(windata[0]);
512
513 current_win = wdefault;
514
515 if (mainloop_add(0, display_keystroke, NULL))
516 return -1;
517
518 if (!initscr())
519 return -1;
520
521 start_color();
522 use_default_colors();
523
524 keypad(stdscr, TRUE);
525 noecho();
526 cbreak();
527 curs_set(0);
528 nonl();
529
530 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
531 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
532 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
533 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
534 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
535 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
536 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
537 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
538 return -1;
539
540 if (atexit(display_fini))
541 return -1;
542
543 getmaxyx(stdscr, maxy, maxx);
544
545 for (i = 0; i < array_size; i++) {
546
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200547 main_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
548 if (!main_win)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200549 return -1;
550
551 windata[i].pad = newpad(maxrows, maxx);
552 if (!windata[i].pad)
553 return -1;
554
555 }
556
557 header_win = subwin(stdscr, 1, maxx, 0, 0);
558 if (!header_win)
559 return -1;
560
561 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
562 if (!footer_win)
563 return -1;
564
Daniel Lezcano653cb4a2011-06-21 00:57:08 +0200565 if (display_show_header(wdefault))
566 return -1;
567
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200568 if (display_show_footer(wdefault, NULL))
Daniel Lezcanodb145802011-06-21 00:57:08 +0200569 return -1;
570
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200571 return display_refresh(wdefault, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200572}
573
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200574int display_column_name(const char *line)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200575{
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200576 werase(main_win);
577 wattron(main_win, A_BOLD);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200578 mvwprintw(main_win, 0, 0, "%s", line);
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200579 wattroff(main_win, A_BOLD);
580 wrefresh(main_win);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200581
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200582 return 0;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200583}
584
585int display_register(int win, struct display_ops *ops)
586{
587 size_t array_size = sizeof(windata) / sizeof(windata[0]);
588
589 if (win < 0 || win >= array_size)
590 return -1;
591
592 windata[win].ops = ops;
593
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200594 return 0;
595}