blob: 98dc95576e451962bc5626948811a730a5868ae8 [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;
34static WINDOW *regulator_win;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020035static WINDOW *clock_pad;
36static WINDOW *clock_labels;
Amit Arora728e0c92010-09-14 12:06:09 +053037static WINDOW *sensor_win;
Amit Arora47fd9182010-08-24 13:26:06 +053038static WINDOW *footer_win;
39
40int maxx, maxy;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020041
42/* Number of lines in the virtual window */
43static const int maxrows = 1024;
44
Daniel Lezcano68500e82011-06-08 23:30:00 +020045static char footer_items[NUM_FOOTER_ITEMS][64];
Amit Arora47fd9182010-08-24 13:26:06 +053046
Daniel Lezcanod5a2c6d2011-03-23 14:37:38 +010047static char *win_names[TOTAL_FEATURE_WINS] = {
48 "Clocks",
49 "Regulators",
50 "Sensors"
51};
Amit Arora47fd9182010-08-24 13:26:06 +053052
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020053struct rowdata {
54 int attr;
55 void *data;
56};
57
58static struct rowdata *rowdata;
59static int nrdata;
60static int scrolling;
61static int cursor;
62
Daniel Lezcano3abd8b12011-03-26 22:06:15 +010063static void display_fini(void)
64{
65 endwin();
66}
67
Daniel Lezcanoca17a722011-03-26 22:06:16 +010068int display_init(void)
Amit Arora47fd9182010-08-24 13:26:06 +053069{
Daniel Lezcanoca17a722011-03-26 22:06:16 +010070 if (!initscr())
71 return -1;
72
Amit Arora6e774cd2010-10-28 11:31:24 +053073 start_color();
Daniel Lezcanoca17a722011-03-26 22:06:16 +010074 use_default_colors();
75
Amit Arora6e774cd2010-10-28 11:31:24 +053076 keypad(stdscr, TRUE);
77 noecho();
78 cbreak();
79 curs_set(0);
80 nonl();
Amit Arora47fd9182010-08-24 13:26:06 +053081
Daniel Lezcanoca17a722011-03-26 22:06:16 +010082 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
83 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
84 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
85 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
86 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
87 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
88 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
89 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
90 return -1;
Amit Arora47fd9182010-08-24 13:26:06 +053091
Daniel Lezcano1c25df92011-06-08 23:30:00 +020092 if (atexit(display_fini))
93 return -1;
94
95 getmaxyx(stdscr, maxy, maxx);
96
97 regulator_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
98 if (!regulator_win)
99 return -1;
100
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200101 clock_labels = subwin(stdscr, maxy - 2, maxx, 1, 0);
102 if (!clock_labels)
103 return -1;
104
105 clock_pad = newpad(maxrows, maxx);
106 if (!clock_pad)
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200107 return -1;
108
109 sensor_win = subwin(stdscr, maxy - 2, maxx, 1, 0);
110 if (!sensor_win)
111 return -1;
112
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 Lezcano558a6d52011-03-23 14:37:41 +0100142void create_selectedwindow(int selectedwindow)
Amit Arora728e0c92010-09-14 12:06:09 +0530143{
Amit Arora6e774cd2010-10-28 11:31:24 +0530144 switch (selectedwindow) {
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200145 case REGULATOR:
146 wrefresh(regulator_win);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600147 break;
Amit Arora728e0c92010-09-14 12:06:09 +0530148
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200149 case SENSOR:
150 wrefresh(sensor_win);
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600151 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530152 }
Amit Arora728e0c92010-09-14 12:06:09 +0530153}
Amit Arora47fd9182010-08-24 13:26:06 +0530154
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100155void show_header(int selectedwindow)
Amit Arora47fd9182010-08-24 13:26:06 +0530156{
157 int i, j = 0;
Amit Kucheria0a4d0192011-01-12 04:54:37 +0530158 int curr_pointer = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530159
160 wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
161 wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
162 werase(header_win);
163
Amit Kucheria0a4d0192011-01-12 04:54:37 +0530164 print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION);
165 curr_pointer += 20;
Amit Arora47fd9182010-08-24 13:26:06 +0530166
Amit Arora6e774cd2010-10-28 11:31:24 +0530167 for (i = 0; i < TOTAL_FEATURE_WINS; i++) {
168 if (selectedwindow == i)
169 wattron(header_win, A_REVERSE);
170 else
171 wattroff(header_win, A_REVERSE);
Amit Arorac93e0712010-10-07 13:51:53 +0530172
Amit Kucheria0a4d0192011-01-12 04:54:37 +0530173 print(header_win, curr_pointer, 0, " %s ", win_names[i]);
174 curr_pointer += strlen(win_names[i]) + 2;
Amit Arora6e774cd2010-10-28 11:31:24 +0530175 }
176 wrefresh(header_win);
Amit Arora47fd9182010-08-24 13:26:06 +0530177 werase(footer_win);
178
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600179 for (i = 0; i < NUM_FOOTER_ITEMS; i++) {
180 if (strlen(footer_items[i]) == 0)
Amit Arora47fd9182010-08-24 13:26:06 +0530181 continue;
182 wattron(footer_win, A_REVERSE);
183 print(footer_win, j, 0, "%s", footer_items[i]);
184 wattroff(footer_win, A_REVERSE);
185 j+= strlen(footer_items[i])+1;
186 }
187 wrefresh(footer_win);
188}
189
190
Daniel Lezcano833b63a2011-03-26 22:06:00 +0100191void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbose)
Amit Arora47fd9182010-08-24 13:26:06 +0530192{
Amit Arorac93e0712010-10-07 13:51:53 +0530193 int i, count = 1;
Amit Arora47fd9182010-08-24 13:26:06 +0530194
Amit Arora6e774cd2010-10-28 11:31:24 +0530195 (void)verbose;
Amit Arorabf50db92010-10-07 14:09:05 +0530196
Amit Arora47fd9182010-08-24 13:26:06 +0530197 werase(regulator_win);
198 wattron(regulator_win, A_BOLD);
Amit Arorac93e0712010-10-07 13:51:53 +0530199 print(regulator_win, 0, 0, "Name");
200 print(regulator_win, 12, 0, "Status");
201 print(regulator_win, 24, 0, "State");
202 print(regulator_win, 36, 0, "Type");
203 print(regulator_win, 48, 0, "Users");
204 print(regulator_win, 60, 0, "Microvolts");
205 print(regulator_win, 72, 0, "Min u-volts");
206 print(regulator_win, 84, 0, "Max u-volts");
Amit Arora47fd9182010-08-24 13:26:06 +0530207 wattroff(regulator_win, A_BOLD);
208
Daniel Lezcano833b63a2011-03-26 22:06:00 +0100209 for (i = 0; i < nr_reg; i++) {
Amit Arora47fd9182010-08-24 13:26:06 +0530210 int col = 0;
211
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600212 if ((i + 2) > (maxy-2))
Amit Arora47fd9182010-08-24 13:26:06 +0530213 break;
214
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100215 if (reg_info[i].num_users > 0)
Amit Arora6e774cd2010-10-28 11:31:24 +0530216 wattron(regulator_win, WA_BOLD);
217 else
218 wattroff(regulator_win, WA_BOLD);
219
Amit Arora47fd9182010-08-24 13:26:06 +0530220 print(regulator_win, col, count, "%s",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100221 reg_info[i].name);
Amit Arora47fd9182010-08-24 13:26:06 +0530222 col += 12;
223 print(regulator_win, col, count, "%s",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100224 reg_info[i].status);
Amit Arora47fd9182010-08-24 13:26:06 +0530225 col += 12;
226 print(regulator_win, col, count, "%s",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100227 reg_info[i].state);
Amit Arora47fd9182010-08-24 13:26:06 +0530228 col += 12;
229 print(regulator_win, col, count, "%s",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100230 reg_info[i].type);
Amit Arora47fd9182010-08-24 13:26:06 +0530231 col += 12;
232 print(regulator_win, col, count, "%d",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100233 reg_info[i].num_users);
Amit Arora6a943ec2010-10-07 11:49:25 +0530234 col += 12;
235 print(regulator_win, col, count, "%d",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100236 reg_info[i].microvolts);
Amit Arora47fd9182010-08-24 13:26:06 +0530237 col += 12;
238 print(regulator_win, col, count, "%d",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100239 reg_info[i].min_microvolts);
Amit Arora47fd9182010-08-24 13:26:06 +0530240 col += 12;
241 print(regulator_win, col, count, "%d",
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100242 reg_info[i].max_microvolts);
Amit Arora47fd9182010-08-24 13:26:06 +0530243
244 count++;
245 }
246 wrefresh(regulator_win);
247}
248
Amit Arora728e0c92010-09-14 12:06:09 +0530249
Amit Aroraac4e8652010-11-09 11:16:53 +0530250void print_clock_header(void)
Amit Arora728e0c92010-09-14 12:06:09 +0530251{
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200252 werase(clock_labels);
253 wattron(clock_labels, A_BOLD);
254 print(clock_labels, 0, 0, "Name");
255 print(clock_labels, 56, 0, "Flags");
256 print(clock_labels, 75, 0, "Rate");
257 print(clock_labels, 88, 0, "Usecount");
258 print(clock_labels, 98, 0, "Children");
259 wattroff(clock_labels, A_BOLD);
260 wrefresh(clock_labels);
Amit Arora728e0c92010-09-14 12:06:09 +0530261}
262
263void print_sensor_header(void)
264{
Amit Arora6e774cd2010-10-28 11:31:24 +0530265 werase(sensor_win);
266 wattron(sensor_win, A_BOLD);
267 print(sensor_win, 0, 0, "Name");
268 print(sensor_win, 36, 0, "Temperature");
269 wattroff(sensor_win, A_BOLD);
270 wattron(sensor_win, A_BLINK);
271 print(sensor_win, 0, 1, "Currently Sensor information available"
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600272 " only in Dump mode!");
Amit Arora6e774cd2010-10-28 11:31:24 +0530273 wattroff(sensor_win, A_BLINK);
Amit Arora728e0c92010-09-14 12:06:09 +0530274 wrefresh(sensor_win);
275}
276
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200277int display_refresh_pad(void)
Amit Arora728e0c92010-09-14 12:06:09 +0530278{
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200279 return prefresh(clock_pad, scrolling, 0, 2, 0, maxy - 2, maxx);
280}
Amit Aroraac4e8652010-11-09 11:16:53 +0530281
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200282static int inline display_clock_un_select(int line, bool highlight, bool bold)
283{
284 if (mvwchgat(clock_pad, line, 0, -1,
285 highlight ? WA_STANDOUT :
286 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
287 return -1;
288
289 return display_refresh_pad();
290}
291
292int display_clock_select(int line)
293{
294 return display_clock_un_select(line, true, false);
295}
296
297int display_clock_unselect(int line, bool bold)
298{
299 return display_clock_un_select(line, false, bold);
300}
301
302void *display_get_row_data(void)
303{
304 return rowdata[cursor].data;
305}
306
307int display_set_row_data(int line, void *data, int attr)
308{
309 if (line >= nrdata) {
310 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
311 if (!rowdata)
312 return -1;
313 nrdata = line + 1;
314 }
315
316 rowdata[line].data = data;
317 rowdata[line].attr = attr;
318
319 return 0;
320}
321
322int display_reset_cursor(void)
323{
324 nrdata = 0;
325 werase(clock_pad);
326 return wmove(clock_pad, 0, 0);
327}
328
329int display_print_line(int line, char *str, int bold, void *data)
330{
331 int attr = 0;
332
Amit Arora031263a2010-11-09 11:12:41 +0530333 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200334 attr |= WA_BOLD;
335
336 if (line == cursor)
337 attr |= WA_STANDOUT;
338
339 if (display_set_row_data(line, data, attr))
340 return -1;
341
342 if (attr)
343 wattron(clock_pad, attr);
344
345 wprintw(clock_pad, "%s\n", str);
346
347 if (attr)
348 wattroff(clock_pad, attr);
349
350 return 0;
351}
352
353int display_next_line(void)
354{
355 if (cursor >= nrdata)
356 return cursor;
357
358 display_clock_unselect(cursor, rowdata[cursor].attr);
359 if (cursor < nrdata - 1) {
360 if (cursor >= (maxy - 4 + scrolling))
361 scrolling++;
362 cursor++;
363 }
364 display_clock_select(cursor);
365
366 return cursor;
367}
368
369int display_prev_line(void)
370{
371 if (cursor >= nrdata)
372 return cursor;
373
374 display_clock_unselect(cursor, rowdata[cursor].attr);
375 if (cursor > 0) {
376 if (cursor <= scrolling)
377 scrolling--;
378 cursor--;
379 }
380 display_clock_select(cursor);
381
382 return cursor;
Amit Arora728e0c92010-09-14 12:06:09 +0530383}