blob: 111436658bb7388b9633d5c9e03bbd8546f80cba [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
Sanjay Singh Rawat83b37c32013-04-17 14:57:07 +0530140void sigwinch_handler(int signo)
141{
142 display_refresh(current_win, true);
143}
144
Daniel Lezcano28203df2011-06-15 15:45:12 +0200145static int display_show_unselection(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200146{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200147 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200148 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
149 return -1;
150
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200151 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200152}
153
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200154void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200155{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200156 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200157}
158
Daniel Lezcano28203df2011-06-15 15:45:12 +0200159static int display_select(void)
160{
161 if (windata[current_win].ops && windata[current_win].ops->select)
162 return windata[current_win].ops->select();
163
164 return 0;
165}
166
Shaojie Sun8ac4a2e2013-07-29 20:11:46 +0800167static int display_change(int keyvalue)
168{
169 if (!(windata[current_win].nrdata))
170 return 0;
171
172 if (windata[current_win].ops && windata[current_win].ops->change)
173 return windata[current_win].ops->change(keyvalue);
174
175 return 0;
176}
177
Daniel Lezcano28203df2011-06-15 15:45:12 +0200178static int display_next_panel(void)
179{
180 size_t array_size = sizeof(windata) / sizeof(windata[0]);
181
182 current_win++;
183 current_win %= array_size;
184
185 return current_win;
186}
187
188static int display_prev_panel(void)
189{
190 size_t array_size = sizeof(windata) / sizeof(windata[0]);
191
192 current_win--;
193 if (current_win < 0)
194 current_win = array_size - 1;
195
196 return current_win;
197}
198
199static int display_next_line(void)
200{
Daniel Lezcano83b28d02016-02-18 13:49:55 +0000201 int maxy;
Daniel Lezcano28203df2011-06-15 15:45:12 +0200202 int cursor = windata[current_win].cursor;
203 int nrdata = windata[current_win].nrdata;
204 int scrolling = windata[current_win].scrolling;
205 struct rowdata *rowdata = windata[current_win].rowdata;
206
Daniel Lezcano83b28d02016-02-18 13:49:55 +0000207 maxy = getmaxy(stdscr);
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200208
Daniel Lezcano28203df2011-06-15 15:45:12 +0200209 if (cursor >= nrdata)
210 return cursor;
211
212 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
213 if (cursor < nrdata - 1) {
214 if (cursor >= (maxy - 4 + scrolling))
215 scrolling++;
216 cursor++;
217 }
218
219 windata[current_win].scrolling = scrolling;
220 windata[current_win].cursor = cursor;
221
222 return cursor;
223}
224
225static int display_prev_line(void)
226{
227 int cursor = windata[current_win].cursor;
228 int nrdata = windata[current_win].nrdata;
229 int scrolling = windata[current_win].scrolling;
230 struct rowdata *rowdata = windata[current_win].rowdata;
231
232 if (cursor >= nrdata)
233 return cursor;
234
235 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
236 if (cursor > 0) {
237 if (cursor <= scrolling)
238 scrolling--;
239 cursor--;
240 }
241
242 windata[current_win].scrolling = scrolling;
243 windata[current_win].cursor = cursor;
244
245 return cursor;
246}
247
248static int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200249{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200250 struct rowdata *rowdata = windata[win].rowdata;
251
252 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200253 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
254 if (!rowdata)
255 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200256 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200257 }
258
259 rowdata[line].data = data;
260 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200261 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200262
263 return 0;
264}
265
Daniel Lezcanof6656822011-06-15 15:45:12 +0200266int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200267{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200268 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200269 werase(windata[win].pad);
270 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200271}
272
Sanjay Singh Rawat96f6e052014-05-26 11:35:02 +0530273void display_message(int win, char *buf)
274{
275 display_reset_cursor(win);
276 wattron(windata[win].pad, WA_BOLD);
277 wprintw(windata[win].pad, "%s\n", buf);
278 wattroff(windata[win].pad, WA_BOLD);
279 display_refresh_pad(win);
280}
281
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200282int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200283{
284 int attr = 0;
285
Amit Arora031263a2010-11-09 11:12:41 +0530286 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200287 attr |= WA_BOLD;
288
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200289 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200290 attr |= WA_STANDOUT;
291
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200292 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200293 return -1;
294
295 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200296 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200297
Daniel Lezcanof6656822011-06-15 15:45:12 +0200298 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200299
300 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200301 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200302
303 return 0;
304}
305
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200306static int display_find_keystroke(int fd, void *data);
307
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200308struct find_data {
309 size_t len;
310 char *string;
311 regex_t *reg;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200312 int ocursor;
Daniel Lezcano10c86452011-06-21 00:57:08 +0200313 int oscrolling;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200314};
315
Daniel Lezcano10c86452011-06-21 00:57:08 +0200316struct find_data *display_find_init(void)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200317{
318 const char *regexp = "^[a-z|0-9|_|-|.]";
319 struct find_data *findd;
320 const size_t len = 64;
321 regex_t *reg;
322 char *search4;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200323
324 reg = malloc(sizeof(*reg));
325 if (!reg)
326 return NULL;
327
328 if (regcomp(reg, regexp, REG_ICASE))
329 goto out_free_reg;
330
331 search4 = malloc(len);
332 if (!search4)
333 goto out_free_regcomp;
334 memset(search4, '\0', len);
335
336 findd = malloc(sizeof(*findd));
337 if (!findd)
338 goto out_free_search4;
339
340 findd->string = search4;
341 findd->reg = reg;
342 findd->len = len;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200343
344 /* save the location of the cursor on the main window in order to
345 * browse the search result
346 */
347 findd->ocursor = windata[current_win].cursor;
Daniel Lezcano10c86452011-06-21 00:57:08 +0200348 findd->oscrolling = windata[current_win].scrolling;
Daniel Lezcano96a64fb2011-06-21 00:57:08 +0200349
Daniel Lezcano10c86452011-06-21 00:57:08 +0200350 windata[current_win].cursor = 0;
351 windata[current_win].scrolling = 0;
352
353 curs_set(1);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200354out:
355 return findd;
356
357out_free_search4:
358 free(search4);
359out_free_regcomp:
360 regfree(reg);
361out_free_reg:
362 free(reg);
363
364 goto out;
365}
366
Daniel Lezcano10c86452011-06-21 00:57:08 +0200367static void display_find_fini(struct find_data *findd)
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200368{
Daniel Lezcano10c86452011-06-21 00:57:08 +0200369 windata[current_win].cursor = findd->ocursor;
370 windata[current_win].scrolling = findd->oscrolling;
371 regfree(findd->reg);
372 free(findd->string);
373 free(findd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200374 curs_set(0);
375}
376
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200377static int display_switch_to_find(int fd)
378{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200379 struct find_data *findd;
380
Daniel Lezcano10c86452011-06-21 00:57:08 +0200381 findd = display_find_init();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200382 if (!findd)
383 return -1;
384
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200385 if (mainloop_del(fd))
386 return -1;
387
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200388 if (mainloop_add(fd, display_find_keystroke, findd))
389 return -1;
390
391 if (display_show_footer(current_win, "find (esc to exit)?"))
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200392 return -1;
393
394 return 0;
395}
396
Daniel Lezcanodb145802011-06-21 00:57:08 +0200397static int display_keystroke(int fd, void *data)
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200398{
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200399 int keystroke = getch();
400
401 switch (keystroke) {
402
403 case KEY_RIGHT:
404 case '\t':
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200405 display_show_header(display_next_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200406 break;
407
408 case KEY_LEFT:
409 case KEY_BTAB:
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200410 display_show_header(display_prev_panel());
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200411 break;
412
413 case KEY_DOWN:
414 display_next_line();
415 break;
416
417 case KEY_UP:
418 display_prev_line();
419 break;
420
Shaojie Sunb32cbb92013-07-17 16:23:45 +0800421 case '\n':
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200422 case '\r':
423 display_select();
424 break;
425
Shaojie Sun8ac4a2e2013-07-29 20:11:46 +0800426 case 'v':
427 case 'V':
428 case 'd':
429 case 'D':
430 display_change(toupper(keystroke));
431 break;
432
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200433 case EOF:
434 case 'q':
435 case 'Q':
436 return 1;
437
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200438 case '/':
439 return display_switch_to_find(fd);
440
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200441 case 'r':
442 case 'R':
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200443 return display_refresh(current_win, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200444 default:
445 return 0;
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200446 }
447
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200448 display_refresh(current_win, false);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200449
450 return 0;
451}
452
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200453static int display_switch_to_main(int fd)
454{
455 if (mainloop_del(fd))
456 return -1;
457
458 if (mainloop_add(fd, display_keystroke, NULL))
459 return -1;
460
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200461 if (display_show_header(current_win))
462 return -1;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200463
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200464 if (display_show_footer(current_win, NULL))
465 return -1;
466
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200467 return display_refresh(current_win, false);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200468}
469
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200470static int display_find_keystroke(int fd, void *data)
471{
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200472 struct find_data *findd = data;
473 regex_t *reg = findd->reg;
474 char *string = findd->string;
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200475 int keystroke = getch();
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200476 char match[2] = { [0] = (char)keystroke, [1] = '\0' };
477 regmatch_t m[1];
478
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200479 switch (keystroke) {
480
481 case '\e':
Daniel Lezcano10c86452011-06-21 00:57:08 +0200482 display_find_fini(findd);
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200483 return display_switch_to_main(fd);
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200484
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200485 case KEY_DOWN:
486 display_next_line();
487 break;
488
489 case KEY_UP:
490 display_prev_line();
491 break;
492
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200493 case KEY_BACKSPACE:
494 if (strlen(string))
495 string[strlen(string) - 1] = '\0';
Daniel Lezcano10c86452011-06-21 00:57:08 +0200496
497 windata[current_win].cursor = 0;
498 windata[current_win].scrolling = 0;
499
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200500 break;
501
Shaojie Sunb32cbb92013-07-17 16:23:45 +0800502 case '\n':
Daniel Lezcano73b40022011-06-21 00:57:08 +0200503 case '\r':
504 if (!windata[current_win].ops || !windata[current_win].ops->selectf)
505 return 0;
506
507 if (windata[current_win].ops->selectf())
508 return -1;
509
Daniel Lezcanoe78bc072011-06-21 10:49:39 +0200510 windata[current_win].cursor = 0;
511 windata[current_win].scrolling = 0;
512
Daniel Lezcano73b40022011-06-21 00:57:08 +0200513 return 0;
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200514
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200515 default:
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200516
517 /* We don't want invalid characters for a name */
518 if (regexec(reg, match, 1, m, 0))
519 return 0;
520
521 if (strlen(string) < findd->len - 1)
522 string[strlen(string)] = (char)keystroke;
523
Daniel Lezcano10c86452011-06-21 00:57:08 +0200524 windata[current_win].cursor = 0;
525 windata[current_win].scrolling = 0;
526
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200527 break;
528 }
529
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200530 if (!windata[current_win].ops || !windata[current_win].ops->find)
531 return 0;
532
533 if (windata[current_win].ops->find(string))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200534 return -1;
535
Daniel Lezcanoa12163d2011-06-21 00:57:08 +0200536 if (display_show_header(current_win))
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200537 return -1;
538
539 if (display_show_footer(current_win, strlen(string) ? string :
540 "find (esc to exit)?"))
541 return -1;
542
Daniel Lezcanoe64c48e2011-06-21 00:57:08 +0200543 return 0;
544}
545
Daniel Lezcanodb145802011-06-21 00:57:08 +0200546int display_init(int wdefault)
547{
Daniel Lezcano0a8cc582011-06-21 00:57:08 +0200548 int i, maxx, maxy;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200549 size_t array_size = sizeof(windata) / sizeof(windata[0]);
550
551 current_win = wdefault;
552
553 if (mainloop_add(0, display_keystroke, NULL))
554 return -1;
555
556 if (!initscr())
557 return -1;
558
559 start_color();
560 use_default_colors();
561
562 keypad(stdscr, TRUE);
563 noecho();
564 cbreak();
565 curs_set(0);
566 nonl();
567
568 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
569 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
570 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
571 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
572 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
573 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
574 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
575 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
576 return -1;
577
578 if (atexit(display_fini))
579 return -1;
580
581 getmaxyx(stdscr, maxy, maxx);
582
583 for (i = 0; i < array_size; i++) {
584
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200585 main_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
586 if (!main_win)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200587 return -1;
588
589 windata[i].pad = newpad(maxrows, maxx);
590 if (!windata[i].pad)
591 return -1;
592
593 }
594
595 header_win = subwin(stdscr, 1, maxx, 0, 0);
596 if (!header_win)
597 return -1;
598
599 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
600 if (!footer_win)
601 return -1;
602
Daniel Lezcano653cb4a2011-06-21 00:57:08 +0200603 if (display_show_header(wdefault))
604 return -1;
605
Daniel Lezcano5000abd2011-06-21 00:57:08 +0200606 if (display_show_footer(wdefault, NULL))
Daniel Lezcanodb145802011-06-21 00:57:08 +0200607 return -1;
608
Daniel Lezcanod577aaa2011-06-21 00:57:08 +0200609 return display_refresh(wdefault, true);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200610}
611
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200612int display_column_name(const char *line)
Daniel Lezcanodb145802011-06-21 00:57:08 +0200613{
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200614 werase(main_win);
615 wattron(main_win, A_BOLD);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200616 mvwprintw(main_win, 0, 0, "%s", line);
Daniel Lezcanoc196d432011-06-21 00:57:08 +0200617 wattroff(main_win, A_BOLD);
618 wrefresh(main_win);
Daniel Lezcanodb145802011-06-21 00:57:08 +0200619
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200620 return 0;
Daniel Lezcanodb145802011-06-21 00:57:08 +0200621}
622
623int display_register(int win, struct display_ops *ops)
624{
625 size_t array_size = sizeof(windata) / sizeof(windata[0]);
626
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530627 if (win < 0 || win >= array_size) {
628 printf("error: invalid window");
Daniel Lezcanodb145802011-06-21 00:57:08 +0200629 return -1;
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530630 }
Daniel Lezcanodb145802011-06-21 00:57:08 +0200631
632 windata[win].ops = ops;
633
Daniel Lezcano176e69d2011-06-15 15:45:12 +0200634 return 0;
635}