blob: d55d748c5398241aa753a54ac4bddac9a0f4b4a1 [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;
Daniel Lezcanod96731a2011-06-15 15:45:12 +020034static int current_win;
Amit Arora47fd9182010-08-24 13:26:06 +053035
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 Lezcano7b3da502011-06-15 15:45:12 +020041#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \
42 "'Right' , 'Up', 'Down', 'enter', , 'Esc'"
Amit Arora47fd9182010-08-24 13:26:06 +053043
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020044struct rowdata {
45 int attr;
46 void *data;
47};
48
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020049struct windata {
Daniel Lezcanof6656822011-06-15 15:45:12 +020050 WINDOW *win;
51 WINDOW *pad;
Daniel Lezcanob301b082011-06-15 15:45:12 +020052 struct display_ops *ops;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +020053 struct rowdata *rowdata;
54 char *name;
55 int nrdata;
56 int scrolling;
57 int cursor;
58};
59
60struct windata windata[TOTAL_FEATURE_WINS] = {
61 { .name = "Clocks" },
62 { .name = "Regulators" },
63 { .name = "Sensors" },
64};
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020065
Daniel Lezcano3abd8b12011-03-26 22:06:15 +010066static void display_fini(void)
67{
68 endwin();
69}
70
Daniel Lezcano7b3da502011-06-15 15:45:12 +020071static int show_header_footer(int win)
72{
73 int i;
74 int curr_pointer = 0;
75
76 wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
77 wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
78 werase(header_win);
79
80 print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION);
81 curr_pointer += 20;
82
83 for (i = 0; i < TOTAL_FEATURE_WINS; i++) {
84 if (win == i)
85 wattron(header_win, A_REVERSE);
86 else
87 wattroff(header_win, A_REVERSE);
88
89 print(header_win, curr_pointer, 0, " %s ", windata[i].name);
90 curr_pointer += strlen(windata[i].name) + 2;
91 }
92 wrefresh(header_win);
93 werase(footer_win);
94
95 wattron(footer_win, A_REVERSE);
96 print(footer_win, 0, 0, "%s", footer_label);
97 wattroff(footer_win, A_REVERSE);
98 wrefresh(footer_win);
99
100 return 0;
101}
102
103int display_init(int wdefault)
Amit Arora47fd9182010-08-24 13:26:06 +0530104{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200105 int i;
106 size_t array_size = sizeof(windata) / sizeof(windata[0]);
107
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200108 current_win = wdefault;
109
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100110 if (!initscr())
111 return -1;
112
Amit Arora6e774cd2010-10-28 11:31:24 +0530113 start_color();
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100114 use_default_colors();
115
Amit Arora6e774cd2010-10-28 11:31:24 +0530116 keypad(stdscr, TRUE);
117 noecho();
118 cbreak();
119 curs_set(0);
120 nonl();
Amit Arora47fd9182010-08-24 13:26:06 +0530121
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100122 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
123 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
124 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
125 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
126 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
127 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
128 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
129 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
130 return -1;
Amit Arora47fd9182010-08-24 13:26:06 +0530131
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200132 if (atexit(display_fini))
133 return -1;
134
135 getmaxyx(stdscr, maxy, maxx);
136
Daniel Lezcanof6656822011-06-15 15:45:12 +0200137 for (i = 0; i < array_size; i++) {
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200138
Daniel Lezcanof6656822011-06-15 15:45:12 +0200139 windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0);
140 if (!windata[i].win)
141 return -1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200142
Daniel Lezcanof6656822011-06-15 15:45:12 +0200143 windata[i].pad = newpad(maxrows, maxx);
144 if (!windata[i].pad)
145 return -1;
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200146
Daniel Lezcanof6656822011-06-15 15:45:12 +0200147 }
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200148
149 header_win = subwin(stdscr, 1, maxx, 0, 0);
150 if (!header_win)
151 return -1;
152
153 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
154 if (!footer_win)
155 return -1;
156
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200157 return show_header_footer(wdefault);
Amit Arora47fd9182010-08-24 13:26:06 +0530158}
159
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200160void print_regulator_header(void)
Amit Arora47fd9182010-08-24 13:26:06 +0530161{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200162 WINDOW *regulator_win = windata[REGULATOR].win;
163
Amit Arora47fd9182010-08-24 13:26:06 +0530164 werase(regulator_win);
165 wattron(regulator_win, A_BOLD);
Amit Arorac93e0712010-10-07 13:51:53 +0530166 print(regulator_win, 0, 0, "Name");
167 print(regulator_win, 12, 0, "Status");
168 print(regulator_win, 24, 0, "State");
169 print(regulator_win, 36, 0, "Type");
170 print(regulator_win, 48, 0, "Users");
171 print(regulator_win, 60, 0, "Microvolts");
172 print(regulator_win, 72, 0, "Min u-volts");
173 print(regulator_win, 84, 0, "Max u-volts");
Amit Arora47fd9182010-08-24 13:26:06 +0530174 wattroff(regulator_win, A_BOLD);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200175 wrefresh(regulator_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200176
177 show_header_footer(REGULATOR);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200178}
179
180void print_clock_header(void)
181{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200182 WINDOW *clock_win = windata[CLOCK].win;
183
184 werase(clock_win);
185 wattron(clock_win, A_BOLD);
186 print(clock_win, 0, 0, "Name");
187 print(clock_win, 56, 0, "Flags");
188 print(clock_win, 75, 0, "Rate");
189 print(clock_win, 88, 0, "Usecount");
190 print(clock_win, 98, 0, "Children");
191 wattroff(clock_win, A_BOLD);
192 wrefresh(clock_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200193
194 show_header_footer(CLOCK);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200195}
196
Amit Arora728e0c92010-09-14 12:06:09 +0530197void print_sensor_header(void)
198{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200199 WINDOW *sensor_win = windata[SENSOR].win;
200
Amit Arora6e774cd2010-10-28 11:31:24 +0530201 werase(sensor_win);
202 wattron(sensor_win, A_BOLD);
203 print(sensor_win, 0, 0, "Name");
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200204 print(sensor_win, 36, 0, "Value");
Amit Arora6e774cd2010-10-28 11:31:24 +0530205 wattroff(sensor_win, A_BOLD);
Amit Arora728e0c92010-09-14 12:06:09 +0530206 wrefresh(sensor_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200207
208 show_header_footer(SENSOR);
Amit Arora728e0c92010-09-14 12:06:09 +0530209}
210
Daniel Lezcanob301b082011-06-15 15:45:12 +0200211int display_register(int win, struct display_ops *ops)
212{
213 if (win < 0 || win >= TOTAL_FEATURE_WINS)
214 return -1;
215
216 windata[win].ops = ops;
217
218 return 0;
219}
220
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200221int display_next_panel(void)
222{
223 current_win++;
224 current_win %= TOTAL_FEATURE_WINS;
225
226 return current_win;
227}
228
229int display_prev_panel(void)
230{
231 current_win--;
232 if (current_win < 0)
233 current_win = TOTAL_FEATURE_WINS - 1;
234
235 return current_win;
236}
237
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200238int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530239{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200240 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200241 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200242}
Amit Aroraac4e8652010-11-09 11:16:53 +0530243
Daniel Lezcanof6656822011-06-15 15:45:12 +0200244static int inline display_un_select(int win, int line,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200245 bool highlight, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200246{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200247 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200248 highlight ? WA_STANDOUT :
249 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
250 return -1;
251
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200252 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200253}
254
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200255int display_select(int win, int line)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200256{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200257 return display_un_select(win, line, true, false);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200258}
259
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200260int display_unselect(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200261{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200262 return display_un_select(win, line, false, bold);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200263}
264
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200265void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200266{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200267 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200268}
269
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200270int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200271{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200272 struct rowdata *rowdata = windata[win].rowdata;
273
274 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200275 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
276 if (!rowdata)
277 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200278 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200279 }
280
281 rowdata[line].data = data;
282 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200283 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200284
285 return 0;
286}
287
Daniel Lezcanof6656822011-06-15 15:45:12 +0200288int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200289{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200290 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200291 werase(windata[win].pad);
292 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200293}
294
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200295int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200296{
297 int attr = 0;
298
Amit Arora031263a2010-11-09 11:12:41 +0530299 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200300 attr |= WA_BOLD;
301
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200302 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200303 attr |= WA_STANDOUT;
304
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200305 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200306 return -1;
307
308 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200309 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200310
Daniel Lezcanof6656822011-06-15 15:45:12 +0200311 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200312
313 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200314 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200315
316 return 0;
317}
318
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200319int display_next_line(void)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200320{
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200321 int cursor = windata[current_win].cursor;
322 int nrdata = windata[current_win].nrdata;
323 int scrolling = windata[current_win].scrolling;
324 struct rowdata *rowdata = windata[current_win].rowdata;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200325
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200326 if (cursor >= nrdata)
327 return cursor;
328
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200329 display_unselect(current_win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200330 if (cursor < nrdata - 1) {
331 if (cursor >= (maxy - 4 + scrolling))
332 scrolling++;
333 cursor++;
334 }
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200335 display_select(current_win, cursor);
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200336
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200337 windata[current_win].scrolling = scrolling;
338 windata[current_win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200339
340 return cursor;
341}
342
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200343int display_prev_line(void)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200344{
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200345 int cursor = windata[current_win].cursor;
346 int nrdata = windata[current_win].nrdata;
347 int scrolling = windata[current_win].scrolling;
348 struct rowdata *rowdata = windata[current_win].rowdata;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200349
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200350 if (cursor >= nrdata)
351 return cursor;
352
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200353 display_unselect(current_win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200354 if (cursor > 0) {
355 if (cursor <= scrolling)
356 scrolling--;
357 cursor--;
358 }
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200359 display_select(current_win, cursor);
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200360
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200361 windata[current_win].scrolling = scrolling;
362 windata[current_win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200363
364 return cursor;
Amit Arora728e0c92010-09-14 12:06:09 +0530365}