blob: c6a68a7a335eb67b3944a32158e852e379c28433 [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
Amit Arora47fd9182010-08-24 13:26:06 +053016#include "powerdebug.h"
Amit Arora17552782010-12-02 12:23:14 +053017#include "regulator.h"
Amit Aroraed3e5652010-10-27 12:02:53 +053018#include "display.h"
Amit Arora47fd9182010-08-24 13:26:06 +053019
20#define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } while (0)
21#define NUM_FOOTER_ITEMS 5
22
Daniel Lezcanoeeb13762011-03-26 22:06:17 +010023enum { PT_COLOR_DEFAULT = 1,
24 PT_COLOR_HEADER_BAR,
25 PT_COLOR_ERROR,
26 PT_COLOR_RED,
27 PT_COLOR_YELLOW,
28 PT_COLOR_GREEN,
29 PT_COLOR_BRIGHT,
30 PT_COLOR_BLUE,
31};
32
Amit Arora47fd9182010-08-24 13:26:06 +053033static WINDOW *header_win;
Amit Arora47fd9182010-08-24 13:26:06 +053034static WINDOW *footer_win;
35
36int maxx, maxy;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020037
38/* Number of lines in the virtual window */
39static const int maxrows = 1024;
40
Daniel Lezcano68500e82011-06-08 23:30:00 +020041static char footer_items[NUM_FOOTER_ITEMS][64];
Amit Arora47fd9182010-08-24 13:26:06 +053042
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020043struct rowdata {
44 int attr;
45 void *data;
46};
47
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020048struct windata {
Daniel Lezcanof6656822011-06-15 15:45:12 +020049 WINDOW *win;
50 WINDOW *pad;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020051 struct rowdata *rowdata;
52 char *name;
53 int nrdata;
54 int scrolling;
55 int cursor;
56};
57
58struct windata windata[TOTAL_FEATURE_WINS] = {
59 { .name = "Clocks" },
60 { .name = "Regulators" },
61 { .name = "Sensors" },
62};
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020063
Daniel Lezcano3abd8b12011-03-26 22:06:15 +010064static void display_fini(void)
65{
66 endwin();
67}
68
Daniel Lezcanoca17a722011-03-26 22:06:16 +010069int display_init(void)
Amit Arora47fd9182010-08-24 13:26:06 +053070{
Daniel Lezcanof6656822011-06-15 15:45:12 +020071 int i;
72 size_t array_size = sizeof(windata) / sizeof(windata[0]);
73
Daniel Lezcanoca17a722011-03-26 22:06:16 +010074 if (!initscr())
75 return -1;
76
Amit Arora6e774cd2010-10-28 11:31:24 +053077 start_color();
Daniel Lezcanoca17a722011-03-26 22:06:16 +010078 use_default_colors();
79
Amit Arora6e774cd2010-10-28 11:31:24 +053080 keypad(stdscr, TRUE);
81 noecho();
82 cbreak();
83 curs_set(0);
84 nonl();
Amit Arora47fd9182010-08-24 13:26:06 +053085
Daniel Lezcanoca17a722011-03-26 22:06:16 +010086 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
87 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
88 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
89 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
90 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
91 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
92 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
93 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
94 return -1;
Amit Arora47fd9182010-08-24 13:26:06 +053095
Daniel Lezcano1c25df92011-06-08 23:30:00 +020096 if (atexit(display_fini))
97 return -1;
98
99 getmaxyx(stdscr, maxy, maxx);
100
Daniel Lezcanof6656822011-06-15 15:45:12 +0200101 for (i = 0; i < array_size; i++) {
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200102
Daniel Lezcanof6656822011-06-15 15:45:12 +0200103 windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0);
104 if (!windata[i].win)
105 return -1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200106
Daniel Lezcanof6656822011-06-15 15:45:12 +0200107 windata[i].pad = newpad(maxrows, maxx);
108 if (!windata[i].pad)
109 return -1;
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200110
Daniel Lezcanof6656822011-06-15 15:45:12 +0200111 }
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200112
113 header_win = subwin(stdscr, 1, maxx, 0, 0);
114 if (!header_win)
115 return -1;
116
117 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
118 if (!footer_win)
119 return -1;
120
121 return 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530122}
123
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100124void create_windows(int selectedwindow)
Amit Arora47fd9182010-08-24 13:26:06 +0530125{
Amit Arora47fd9182010-08-24 13:26:06 +0530126 strcpy(footer_items[0], " Q (Quit) ");
127 strcpy(footer_items[1], " R (Refresh) ");
128
Amit Arora83ae6cb2010-12-02 12:22:19 +0530129 if (selectedwindow == CLOCK)
130 strcpy(footer_items[2], " Other Keys: 'Left', 'Right', 'Up', 'Down', 'enter', "
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600131 " '/', 'Esc' ");
Amit Arora83ae6cb2010-12-02 12:22:19 +0530132 else
133 strcpy(footer_items[2], " Other Keys: 'Left', 'Right' ");
134
135 strcpy(footer_items[3], "");
136
Amit Arora47fd9182010-08-24 13:26:06 +0530137 werase(stdscr);
138 refresh();
139
140}
141
Daniel Lezcanof6656822011-06-15 15:45:12 +0200142void create_selectedwindow(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530143{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200144 wrefresh(windata[win].win);
Amit Arora728e0c92010-09-14 12:06:09 +0530145}
Amit Arora47fd9182010-08-24 13:26:06 +0530146
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100147void show_header(int selectedwindow)
Amit Arora47fd9182010-08-24 13:26:06 +0530148{
149 int i, j = 0;
Amit Kucheria0a4d0192011-01-12 04:54:37 +0530150 int curr_pointer = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530151
152 wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
153 wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
154 werase(header_win);
155
Amit Kucheria0a4d0192011-01-12 04:54:37 +0530156 print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION);
157 curr_pointer += 20;
Amit Arora47fd9182010-08-24 13:26:06 +0530158
Amit Arora6e774cd2010-10-28 11:31:24 +0530159 for (i = 0; i < TOTAL_FEATURE_WINS; i++) {
160 if (selectedwindow == i)
161 wattron(header_win, A_REVERSE);
162 else
163 wattroff(header_win, A_REVERSE);
Amit Arorac93e0712010-10-07 13:51:53 +0530164
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200165 print(header_win, curr_pointer, 0, " %s ", windata[i].name);
166 curr_pointer += strlen(windata[i].name) + 2;
Amit Arora6e774cd2010-10-28 11:31:24 +0530167 }
168 wrefresh(header_win);
Amit Arora47fd9182010-08-24 13:26:06 +0530169 werase(footer_win);
170
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600171 for (i = 0; i < NUM_FOOTER_ITEMS; i++) {
172 if (strlen(footer_items[i]) == 0)
Amit Arora47fd9182010-08-24 13:26:06 +0530173 continue;
174 wattron(footer_win, A_REVERSE);
175 print(footer_win, j, 0, "%s", footer_items[i]);
176 wattroff(footer_win, A_REVERSE);
177 j+= strlen(footer_items[i])+1;
178 }
179 wrefresh(footer_win);
180}
181
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200182void print_regulator_header(void)
Amit Arora47fd9182010-08-24 13:26:06 +0530183{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200184 WINDOW *regulator_win = windata[REGULATOR].win;
185
Amit Arora47fd9182010-08-24 13:26:06 +0530186 werase(regulator_win);
187 wattron(regulator_win, A_BOLD);
Amit Arorac93e0712010-10-07 13:51:53 +0530188 print(regulator_win, 0, 0, "Name");
189 print(regulator_win, 12, 0, "Status");
190 print(regulator_win, 24, 0, "State");
191 print(regulator_win, 36, 0, "Type");
192 print(regulator_win, 48, 0, "Users");
193 print(regulator_win, 60, 0, "Microvolts");
194 print(regulator_win, 72, 0, "Min u-volts");
195 print(regulator_win, 84, 0, "Max u-volts");
Amit Arora47fd9182010-08-24 13:26:06 +0530196 wattroff(regulator_win, A_BOLD);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200197 wrefresh(regulator_win);
198}
199
200void print_clock_header(void)
201{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200202 WINDOW *clock_win = windata[CLOCK].win;
203
204 werase(clock_win);
205 wattron(clock_win, A_BOLD);
206 print(clock_win, 0, 0, "Name");
207 print(clock_win, 56, 0, "Flags");
208 print(clock_win, 75, 0, "Rate");
209 print(clock_win, 88, 0, "Usecount");
210 print(clock_win, 98, 0, "Children");
211 wattroff(clock_win, A_BOLD);
212 wrefresh(clock_win);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200213}
214
Amit Arora728e0c92010-09-14 12:06:09 +0530215void print_sensor_header(void)
216{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200217 WINDOW *sensor_win = windata[SENSOR].win;
218
Amit Arora6e774cd2010-10-28 11:31:24 +0530219 werase(sensor_win);
220 wattron(sensor_win, A_BOLD);
221 print(sensor_win, 0, 0, "Name");
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200222 print(sensor_win, 36, 0, "Value");
Amit Arora6e774cd2010-10-28 11:31:24 +0530223 wattroff(sensor_win, A_BOLD);
Amit Arora728e0c92010-09-14 12:06:09 +0530224 wrefresh(sensor_win);
225}
226
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200227int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530228{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200229 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200230 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200231}
Amit Aroraac4e8652010-11-09 11:16:53 +0530232
Daniel Lezcanof6656822011-06-15 15:45:12 +0200233static int inline display_un_select(int win, int line,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200234 bool highlight, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200235{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200236 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200237 highlight ? WA_STANDOUT :
238 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
239 return -1;
240
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200241 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200242}
243
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200244int display_select(int win, int line)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200245{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200246 return display_un_select(win, line, true, false);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200247}
248
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200249int display_unselect(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200250{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200251 return display_un_select(win, line, false, bold);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200252}
253
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200254void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200255{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200256 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200257}
258
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200259int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200260{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200261 struct rowdata *rowdata = windata[win].rowdata;
262
263 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200264 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
265 if (!rowdata)
266 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200267 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200268 }
269
270 rowdata[line].data = data;
271 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200272 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200273
274 return 0;
275}
276
Daniel Lezcanof6656822011-06-15 15:45:12 +0200277int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200278{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200279 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200280 werase(windata[win].pad);
281 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200282}
283
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200284int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200285{
286 int attr = 0;
287
Amit Arora031263a2010-11-09 11:12:41 +0530288 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200289 attr |= WA_BOLD;
290
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200291 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200292 attr |= WA_STANDOUT;
293
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200294 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200295 return -1;
296
297 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200298 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200299
Daniel Lezcanof6656822011-06-15 15:45:12 +0200300 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200301
302 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200303 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200304
305 return 0;
306}
307
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200308int display_next_line(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200309{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200310 int cursor = windata[win].cursor;
311 int nrdata = windata[win].nrdata;
312 int scrolling = windata[win].scrolling;
313 struct rowdata *rowdata = windata[win].rowdata;
314
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200315 if (cursor >= nrdata)
316 return cursor;
317
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200318 display_unselect(win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200319 if (cursor < nrdata - 1) {
320 if (cursor >= (maxy - 4 + scrolling))
321 scrolling++;
322 cursor++;
323 }
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200324 display_select(win, cursor);
325
326 windata[win].scrolling = scrolling;
327 windata[win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200328
329 return cursor;
330}
331
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200332int display_prev_line(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200333{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200334 int cursor = windata[win].cursor;
335 int nrdata = windata[win].nrdata;
336 int scrolling = windata[win].scrolling;
337 struct rowdata *rowdata = windata[win].rowdata;
338
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200339 if (cursor >= nrdata)
340 return cursor;
341
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200342 display_unselect(win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200343 if (cursor > 0) {
344 if (cursor <= scrolling)
345 scrolling--;
346 cursor--;
347 }
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200348 display_select(win, cursor);
349
350 windata[win].scrolling = scrolling;
351 windata[win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200352
353 return cursor;
Amit Arora728e0c92010-09-14 12:06:09 +0530354}