blob: ebc4de61b72c256b9062292ad708ce31bea1181b [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
Daniel Lezcanocaafece2011-06-27 22:59:17 +0200123 if (werase(main_win))
124 return -1;
125
126 return wrefresh(main_win);
Daniel Lezcano971515a2011-06-15 15:45:12 +0200127}
128
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200129int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530130{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200131 int maxx, maxy;
132
133 getmaxyx(stdscr, maxy, maxx);
134
Daniel Lezcanof6656822011-06-15 15:45:12 +0200135 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200136 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200137}
Amit Aroraac4e8652010-11-09 11:16:53 +0530138
Daniel Lezcano28203df2011-06-15 15:45:12 +0200139static int display_show_unselection(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200140{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200141 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200142 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
143 return -1;
144
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200145 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200146}
147
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200148void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200149{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200150 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200151}
152
Daniel Lezcano28203df2011-06-15 15:45:12 +0200153static int display_select(void)
154{
155 if (windata[current_win].ops && windata[current_win].ops->select)
156 return windata[current_win].ops->select();
157
158 return 0;
159}
160
161static int display_next_panel(void)
162{
163 size_t array_size = sizeof(windata) / sizeof(windata[0]);
164
165 current_win++;
166 current_win %= array_size;
167
168 return current_win;
169}
170
171static int display_prev_panel(void)
172{
173 size_t array_size = sizeof(windata) / sizeof(windata[0]);
174
175 current_win--;
176 if (current_win < 0)
177 current_win = array_size - 1;
178
179 return current_win;
180}
181
182static int display_next_line(void)
183{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200184 int maxx, maxy;
Daniel Lezcano28203df2011-06-15 15:45:12 +0200185 int cursor = windata[current_win].cursor;
186 int nrdata = windata[current_win].nrdata;
187 int scrolling = windata[current_win].scrolling;
188 struct rowdata *rowdata = windata[current_win].rowdata;
189
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200190 getmaxyx(stdscr, maxy, maxx);
191
Daniel Lezcano28203df2011-06-15 15:45:12 +0200192 if (cursor >= nrdata)
193 return cursor;
194
195 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
196 if (cursor < nrdata - 1) {
197 if (cursor >= (maxy - 4 + scrolling))
198 scrolling++;
199 cursor++;
200 }
201
202 windata[current_win].scrolling = scrolling;
203 windata[current_win].cursor = cursor;
204
205 return cursor;
206}
207
208static int display_prev_line(void)
209{
210 int cursor = windata[current_win].cursor;
211 int nrdata = windata[current_win].nrdata;
212 int scrolling = windata[current_win].scrolling;
213 struct rowdata *rowdata = windata[current_win].rowdata;
214
215 if (cursor >= nrdata)
216 return cursor;
217
218 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
219 if (cursor > 0) {
220 if (cursor <= scrolling)
221 scrolling--;
222 cursor--;
223 }
224
225 windata[current_win].scrolling = scrolling;
226 windata[current_win].cursor = cursor;
227
228 return cursor;
229}
230
231static int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200232{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200233 struct rowdata *rowdata = windata[win].rowdata;
234
235 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200236 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
237 if (!rowdata)
238 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200239 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200240 }
241
242 rowdata[line].data = data;
243 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200244 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200245
246 return 0;
247}
248
Daniel Lezcanof6656822011-06-15 15:45:12 +0200249int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200250{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200251 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200252 werase(windata[win].pad);
253 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200254}
255
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200256int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200257{
258 int attr = 0;
259
Amit Arora031263a2010-11-09 11:12:41 +0530260 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200261 attr |= WA_BOLD;
262
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200263 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200264 attr |= WA_STANDOUT;
265
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200266 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200267 return -1;
268
269 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200270 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200271
Daniel Lezcanof6656822011-06-15 15:45:12 +0200272 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200273
274 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200275 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200276
277 return 0;
278}
279
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200280static int display_find_keystroke(int fd, void *data);
281
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200282struct find_data {
283 size_t len;
284 char *string;
285 regex_t *reg;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200286 int ocursor;
Daniel Lezcano10c86452011-06-21 00:57:08 +0200287 int oscrolling;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200288};
289
Daniel Lezcano10c86452011-06-21 00:57:08 +0200290struct find_data *display_find_init(void)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200291{
292 const char *regexp = "^[a-z|0-9|_|-|.]";
293 struct find_data *findd;
294 const size_t len = 64;
295 regex_t *reg;
296 char *search4;
297 int maxx, maxy;
298
299 getmaxyx(stdscr, maxy, maxx);
300
301 reg = malloc(sizeof(*reg));
302 if (!reg)
303 return NULL;
304
305 if (regcomp(reg, regexp, REG_ICASE))
306 goto out_free_reg;
307
308 search4 = malloc(len);
309 if (!search4)
310 goto out_free_regcomp;
311 memset(search4, '\0', len);
312
313 findd = malloc(sizeof(*findd));
314 if (!findd)
315 goto out_free_search4;
316
317 findd->string = search4;
318 findd->reg = reg;
319 findd->len = len;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200320
321 /* save the location of the cursor on the main window in order to
322 * browse the search result
323 */
324 findd->ocursor = windata[current_win].cursor;
Daniel Lezcano10c86452011-06-21 00:57:08 +0200325 findd->oscrolling = windata[current_win].scrolling;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200326
Daniel Lezcano10c86452011-06-21 00:57:08 +0200327 windata[current_win].cursor = 0;
328 windata[current_win].scrolling = 0;
329
330 curs_set(1);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200331out:
332 return findd;
333
334out_free_search4:
335 free(search4);
336out_free_regcomp:
337 regfree(reg);
338out_free_reg:
339 free(reg);
340
341 goto out;
342}
343
Daniel Lezcano10c86452011-06-21 00:57:08 +0200344static void display_find_fini(struct find_data *findd)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200345{
Daniel Lezcano10c86452011-06-21 00:57:08 +0200346 windata[current_win].cursor = findd->ocursor;
347 windata[current_win].scrolling = findd->oscrolling;
348 regfree(findd->reg);
349 free(findd->string);
350 free(findd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200351 curs_set(0);
352}
353
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200354static int display_switch_to_find(int fd)
355{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200356 struct find_data *findd;
357
Daniel Lezcano10c86452011-06-21 00:57:08 +0200358 findd = display_find_init();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200359 if (!findd)
360 return -1;
361
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200362 if (mainloop_del(fd))
363 return -1;
364
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200365 if (mainloop_add(fd, display_find_keystroke, findd))
366 return -1;
367
368 if (display_show_footer(current_win, "find (esc to exit)?"))
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200369 return -1;
370
371 return 0;
372}
373
Daniel Lezcanodb145802011-06-21 00:57:08 +0200374static int display_keystroke(int fd, void *data)
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200375{
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200376 int keystroke = getch();
377
378 switch (keystroke) {
379
380 case KEY_RIGHT:
381 case '\t':
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200382 display_show_header(display_next_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200383 break;
384
385 case KEY_LEFT:
386 case KEY_BTAB:
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200387 display_show_header(display_prev_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200388 break;
389
390 case KEY_DOWN:
391 display_next_line();
392 break;
393
394 case KEY_UP:
395 display_prev_line();
396 break;
397
398 case '\r':
399 display_select();
400 break;
401
402 case EOF:
403 case 'q':
404 case 'Q':
405 return 1;
406
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200407 case '/':
408 return display_switch_to_find(fd);
409
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200410 case 'r':
411 case 'R':
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200412 return display_refresh(current_win, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200413 default:
414 return 0;
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200415 }
416
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200417 display_refresh(current_win, false);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200418
419 return 0;
420}
421
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200422static int display_switch_to_main(int fd)
423{
424 if (mainloop_del(fd))
425 return -1;
426
427 if (mainloop_add(fd, display_keystroke, NULL))
428 return -1;
429
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200430 if (display_show_header(current_win))
431 return -1;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200432
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200433 if (display_show_footer(current_win, NULL))
434 return -1;
435
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200436 return display_refresh(current_win, false);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200437}
438
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200439static int display_find_keystroke(int fd, void *data)
440{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200441 struct find_data *findd = data;
442 regex_t *reg = findd->reg;
443 char *string = findd->string;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200444 int keystroke = getch();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200445 char match[2] = { [0] = (char)keystroke, [1] = '\0' };
446 regmatch_t m[1];
447
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200448 switch (keystroke) {
449
450 case '\e':
Daniel Lezcano10c86452011-06-21 00:57:08 +0200451 display_find_fini(findd);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200452 return display_switch_to_main(fd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200453
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200454 case KEY_DOWN:
455 display_next_line();
456 break;
457
458 case KEY_UP:
459 display_prev_line();
460 break;
461
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200462 case KEY_BACKSPACE:
463 if (strlen(string))
464 string[strlen(string) - 1] = '\0';
Daniel Lezcano10c86452011-06-21 00:57:08 +0200465
466 windata[current_win].cursor = 0;
467 windata[current_win].scrolling = 0;
468
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200469 break;
470
Daniel Lezcano73b40022011-06-21 00:57:08 +0200471 case '\r':
472 if (!windata[current_win].ops || !windata[current_win].ops->selectf)
473 return 0;
474
475 if (windata[current_win].ops->selectf())
476 return -1;
477
Daniel Lezcanoe78bc072011-06-21 10:49:39 +0200478 windata[current_win].cursor = 0;
479 windata[current_win].scrolling = 0;
480
Daniel Lezcano73b40022011-06-21 00:57:08 +0200481 return 0;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200482
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200483 default:
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200484
485 /* We don't want invalid characters for a name */
486 if (regexec(reg, match, 1, m, 0))
487 return 0;
488
489 if (strlen(string) < findd->len - 1)
490 string[strlen(string)] = (char)keystroke;
491
Daniel Lezcano10c86452011-06-21 00:57:08 +0200492 windata[current_win].cursor = 0;
493 windata[current_win].scrolling = 0;
494
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200495 break;
496 }
497
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200498 if (!windata[current_win].ops || !windata[current_win].ops->find)
499 return 0;
500
501 if (windata[current_win].ops->find(string))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200502 return -1;
503
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200504 if (display_show_header(current_win))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200505 return -1;
506
507 if (display_show_footer(current_win, strlen(string) ? string :
508 "find (esc to exit)?"))
509 return -1;
510
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200511 return 0;
512}
513
Daniel Lezcanodb145802011-06-21 00:57:08 +0200514int display_init(int wdefault)
515{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200516 int i, maxx, maxy;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200517 size_t array_size = sizeof(windata) / sizeof(windata[0]);
518
519 current_win = wdefault;
520
521 if (mainloop_add(0, display_keystroke, NULL))
522 return -1;
523
524 if (!initscr())
525 return -1;
526
527 start_color();
528 use_default_colors();
529
530 keypad(stdscr, TRUE);
531 noecho();
532 cbreak();
533 curs_set(0);
534 nonl();
535
536 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
537 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
538 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
539 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
540 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
541 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
542 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
543 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
544 return -1;
545
546 if (atexit(display_fini))
547 return -1;
548
549 getmaxyx(stdscr, maxy, maxx);
550
551 for (i = 0; i < array_size; i++) {
552
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200553 main_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
554 if (!main_win)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200555 return -1;
556
557 windata[i].pad = newpad(maxrows, maxx);
558 if (!windata[i].pad)
559 return -1;
560
561 }
562
563 header_win = subwin(stdscr, 1, maxx, 0, 0);
564 if (!header_win)
565 return -1;
566
567 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
568 if (!footer_win)
569 return -1;
570
Daniel Lezcano653cb4a2011-06-21 00:57:08 +0200571 if (display_show_header(wdefault))
572 return -1;
573
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200574 if (display_show_footer(wdefault, NULL))
Daniel Lezcanodb145802011-06-21 00:57:08 +0200575 return -1;
576
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200577 return display_refresh(wdefault, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200578}
579
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200580int display_column_name(const char *line)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200581{
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200582 werase(main_win);
583 wattron(main_win, A_BOLD);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200584 mvwprintw(main_win, 0, 0, "%s", line);
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200585 wattroff(main_win, A_BOLD);
586 wrefresh(main_win);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200587
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200588 return 0;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200589}
590
591int display_register(int win, struct display_ops *ops)
592{
593 size_t array_size = sizeof(windata) / sizeof(windata[0]);
594
595 if (win < 0 || win >= array_size)
596 return -1;
597
598 windata[win].ops = ops;
599
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200600 return 0;
601}