blob: fa7d93a403b62c7387be3445ee10491dbcda92e6 [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)
Amit Arora47fd9182010-08-24 13:26:06 +053021
Daniel Lezcanoeeb13762011-03-26 22:06:17 +010022enum { PT_COLOR_DEFAULT = 1,
23 PT_COLOR_HEADER_BAR,
24 PT_COLOR_ERROR,
25 PT_COLOR_RED,
26 PT_COLOR_YELLOW,
27 PT_COLOR_GREEN,
28 PT_COLOR_BRIGHT,
29 PT_COLOR_BLUE,
30};
31
Amit Arora47fd9182010-08-24 13:26:06 +053032static WINDOW *header_win;
Amit Arora47fd9182010-08-24 13:26:06 +053033static WINDOW *footer_win;
34
35int maxx, maxy;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020036
37/* Number of lines in the virtual window */
38static const int maxrows = 1024;
39
Daniel Lezcano7b3da502011-06-15 15:45:12 +020040#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \
41 "'Right' , 'Up', 'Down', 'enter', , 'Esc'"
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 Lezcano7b3da502011-06-15 15:45:12 +020069static int show_header_footer(int win)
70{
71 int i;
72 int curr_pointer = 0;
73
74 wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
75 wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
76 werase(header_win);
77
78 print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION);
79 curr_pointer += 20;
80
81 for (i = 0; i < TOTAL_FEATURE_WINS; i++) {
82 if (win == i)
83 wattron(header_win, A_REVERSE);
84 else
85 wattroff(header_win, A_REVERSE);
86
87 print(header_win, curr_pointer, 0, " %s ", windata[i].name);
88 curr_pointer += strlen(windata[i].name) + 2;
89 }
90 wrefresh(header_win);
91 werase(footer_win);
92
93 wattron(footer_win, A_REVERSE);
94 print(footer_win, 0, 0, "%s", footer_label);
95 wattroff(footer_win, A_REVERSE);
96 wrefresh(footer_win);
97
98 return 0;
99}
100
101int display_init(int wdefault)
Amit Arora47fd9182010-08-24 13:26:06 +0530102{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200103 int i;
104 size_t array_size = sizeof(windata) / sizeof(windata[0]);
105
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100106 if (!initscr())
107 return -1;
108
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 start_color();
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100110 use_default_colors();
111
Amit Arora6e774cd2010-10-28 11:31:24 +0530112 keypad(stdscr, TRUE);
113 noecho();
114 cbreak();
115 curs_set(0);
116 nonl();
Amit Arora47fd9182010-08-24 13:26:06 +0530117
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100118 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
119 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
120 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
121 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
122 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
123 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
124 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
125 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
126 return -1;
Amit Arora47fd9182010-08-24 13:26:06 +0530127
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200128 if (atexit(display_fini))
129 return -1;
130
131 getmaxyx(stdscr, maxy, maxx);
132
Daniel Lezcanof6656822011-06-15 15:45:12 +0200133 for (i = 0; i < array_size; i++) {
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200134
Daniel Lezcanof6656822011-06-15 15:45:12 +0200135 windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0);
136 if (!windata[i].win)
137 return -1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200138
Daniel Lezcanof6656822011-06-15 15:45:12 +0200139 windata[i].pad = newpad(maxrows, maxx);
140 if (!windata[i].pad)
141 return -1;
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200142
Daniel Lezcanof6656822011-06-15 15:45:12 +0200143 }
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200144
145 header_win = subwin(stdscr, 1, maxx, 0, 0);
146 if (!header_win)
147 return -1;
148
149 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
150 if (!footer_win)
151 return -1;
152
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200153 return show_header_footer(wdefault);
Amit Arora47fd9182010-08-24 13:26:06 +0530154}
155
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200156void print_regulator_header(void)
Amit Arora47fd9182010-08-24 13:26:06 +0530157{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200158 WINDOW *regulator_win = windata[REGULATOR].win;
159
Amit Arora47fd9182010-08-24 13:26:06 +0530160 werase(regulator_win);
161 wattron(regulator_win, A_BOLD);
Amit Arorac93e0712010-10-07 13:51:53 +0530162 print(regulator_win, 0, 0, "Name");
163 print(regulator_win, 12, 0, "Status");
164 print(regulator_win, 24, 0, "State");
165 print(regulator_win, 36, 0, "Type");
166 print(regulator_win, 48, 0, "Users");
167 print(regulator_win, 60, 0, "Microvolts");
168 print(regulator_win, 72, 0, "Min u-volts");
169 print(regulator_win, 84, 0, "Max u-volts");
Amit Arora47fd9182010-08-24 13:26:06 +0530170 wattroff(regulator_win, A_BOLD);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200171 wrefresh(regulator_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200172
173 show_header_footer(REGULATOR);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200174}
175
176void print_clock_header(void)
177{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200178 WINDOW *clock_win = windata[CLOCK].win;
179
180 werase(clock_win);
181 wattron(clock_win, A_BOLD);
182 print(clock_win, 0, 0, "Name");
183 print(clock_win, 56, 0, "Flags");
184 print(clock_win, 75, 0, "Rate");
185 print(clock_win, 88, 0, "Usecount");
186 print(clock_win, 98, 0, "Children");
187 wattroff(clock_win, A_BOLD);
188 wrefresh(clock_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200189
190 show_header_footer(CLOCK);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200191}
192
Amit Arora728e0c92010-09-14 12:06:09 +0530193void print_sensor_header(void)
194{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200195 WINDOW *sensor_win = windata[SENSOR].win;
196
Amit Arora6e774cd2010-10-28 11:31:24 +0530197 werase(sensor_win);
198 wattron(sensor_win, A_BOLD);
199 print(sensor_win, 0, 0, "Name");
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200200 print(sensor_win, 36, 0, "Value");
Amit Arora6e774cd2010-10-28 11:31:24 +0530201 wattroff(sensor_win, A_BOLD);
Amit Arora728e0c92010-09-14 12:06:09 +0530202 wrefresh(sensor_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200203
204 show_header_footer(SENSOR);
Amit Arora728e0c92010-09-14 12:06:09 +0530205}
206
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200207int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530208{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200209 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200210 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200211}
Amit Aroraac4e8652010-11-09 11:16:53 +0530212
Daniel Lezcanof6656822011-06-15 15:45:12 +0200213static int inline display_un_select(int win, int line,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200214 bool highlight, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200215{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200216 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200217 highlight ? WA_STANDOUT :
218 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
219 return -1;
220
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200221 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200222}
223
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200224int display_select(int win, int line)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200225{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200226 return display_un_select(win, line, true, false);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200227}
228
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200229int display_unselect(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200230{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200231 return display_un_select(win, line, false, bold);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200232}
233
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200234void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200235{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200236 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200237}
238
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200239int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200240{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200241 struct rowdata *rowdata = windata[win].rowdata;
242
243 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200244 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
245 if (!rowdata)
246 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200247 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200248 }
249
250 rowdata[line].data = data;
251 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200252 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200253
254 return 0;
255}
256
Daniel Lezcanof6656822011-06-15 15:45:12 +0200257int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200258{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200259 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200260 werase(windata[win].pad);
261 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200262}
263
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200264int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200265{
266 int attr = 0;
267
Amit Arora031263a2010-11-09 11:12:41 +0530268 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200269 attr |= WA_BOLD;
270
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200271 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200272 attr |= WA_STANDOUT;
273
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200274 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200275 return -1;
276
277 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200278 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200279
Daniel Lezcanof6656822011-06-15 15:45:12 +0200280 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200281
282 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200283 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200284
285 return 0;
286}
287
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200288int display_next_line(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200289{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200290 int cursor = windata[win].cursor;
291 int nrdata = windata[win].nrdata;
292 int scrolling = windata[win].scrolling;
293 struct rowdata *rowdata = windata[win].rowdata;
294
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200295 if (cursor >= nrdata)
296 return cursor;
297
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200298 display_unselect(win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200299 if (cursor < nrdata - 1) {
300 if (cursor >= (maxy - 4 + scrolling))
301 scrolling++;
302 cursor++;
303 }
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200304 display_select(win, cursor);
305
306 windata[win].scrolling = scrolling;
307 windata[win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200308
309 return cursor;
310}
311
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200312int display_prev_line(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200313{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200314 int cursor = windata[win].cursor;
315 int nrdata = windata[win].nrdata;
316 int scrolling = windata[win].scrolling;
317 struct rowdata *rowdata = windata[win].rowdata;
318
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200319 if (cursor >= nrdata)
320 return cursor;
321
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200322 display_unselect(win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200323 if (cursor > 0) {
324 if (cursor <= scrolling)
325 scrolling--;
326 cursor--;
327 }
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200328 display_select(win, cursor);
329
330 windata[win].scrolling = scrolling;
331 windata[win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200332
333 return cursor;
Amit Arora728e0c92010-09-14 12:06:09 +0530334}