blob: 92fc02a2223d4639e7fa3a83920eb03c35d0e9a8 [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 Lezcano971515a2011-06-15 15:45:12 +0200221int display_refresh(void)
222{
223 if (windata[current_win].ops && windata[current_win].ops->display)
224 return windata[current_win].ops->display();
225
226 return 0;
227}
228
229int display_select(void)
230{
231 if (windata[current_win].ops && windata[current_win].ops->select)
232 return windata[current_win].ops->select();
233
234 return 0;
235}
236
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200237int display_next_panel(void)
238{
239 current_win++;
240 current_win %= TOTAL_FEATURE_WINS;
241
242 return current_win;
243}
244
245int display_prev_panel(void)
246{
247 current_win--;
248 if (current_win < 0)
249 current_win = TOTAL_FEATURE_WINS - 1;
250
251 return current_win;
252}
253
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200254int display_refresh_pad(int win)
Amit Arora728e0c92010-09-14 12:06:09 +0530255{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200256 return prefresh(windata[win].pad, windata[win].scrolling,
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200257 0, 2, 0, maxy - 2, maxx);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200258}
Amit Aroraac4e8652010-11-09 11:16:53 +0530259
Daniel Lezcano971515a2011-06-15 15:45:12 +0200260static int inline display_show_un_selection(int win, int line,
261 bool highlight, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200262{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200263 if (mvwchgat(windata[win].pad, line, 0, -1,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200264 highlight ? WA_STANDOUT :
265 bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0)
266 return -1;
267
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200268 return display_refresh_pad(win);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200269}
270
Daniel Lezcano971515a2011-06-15 15:45:12 +0200271int display_show_selection(int win, int line)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200272{
Daniel Lezcano971515a2011-06-15 15:45:12 +0200273 return display_show_un_selection(win, line, true, false);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200274}
275
Daniel Lezcano971515a2011-06-15 15:45:12 +0200276int display_show_unselection(int win, int line, bool bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200277{
Daniel Lezcano971515a2011-06-15 15:45:12 +0200278 return display_show_un_selection(win, line, false, bold);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200279}
280
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200281void *display_get_row_data(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200282{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200283 return windata[win].rowdata[windata[win].cursor].data;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200284}
285
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200286int display_set_row_data(int win, int line, void *data, int attr)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200287{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200288 struct rowdata *rowdata = windata[win].rowdata;
289
290 if (line >= windata[win].nrdata) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200291 rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1));
292 if (!rowdata)
293 return -1;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200294 windata[win].nrdata = line + 1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200295 }
296
297 rowdata[line].data = data;
298 rowdata[line].attr = attr;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200299 windata[win].rowdata = rowdata;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200300
301 return 0;
302}
303
Daniel Lezcanof6656822011-06-15 15:45:12 +0200304int display_reset_cursor(int win)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200305{
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200306 windata[win].nrdata = 0;
Daniel Lezcanof6656822011-06-15 15:45:12 +0200307 werase(windata[win].pad);
308 return wmove(windata[win].pad, 0, 0);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200309}
310
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200311int display_print_line(int win, int line, char *str, int bold, void *data)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200312{
313 int attr = 0;
314
Amit Arora031263a2010-11-09 11:12:41 +0530315 if (bold)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200316 attr |= WA_BOLD;
317
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200318 if (line == windata[win].cursor)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200319 attr |= WA_STANDOUT;
320
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200321 if (display_set_row_data(win, line, data, attr))
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200322 return -1;
323
324 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200325 wattron(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200326
Daniel Lezcanof6656822011-06-15 15:45:12 +0200327 wprintw(windata[win].pad, "%s\n", str);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200328
329 if (attr)
Daniel Lezcanof6656822011-06-15 15:45:12 +0200330 wattroff(windata[win].pad, attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200331
332 return 0;
333}
334
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200335int display_next_line(void)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200336{
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200337 int cursor = windata[current_win].cursor;
338 int nrdata = windata[current_win].nrdata;
339 int scrolling = windata[current_win].scrolling;
340 struct rowdata *rowdata = windata[current_win].rowdata;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200341
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200342 if (cursor >= nrdata)
343 return cursor;
344
Daniel Lezcano971515a2011-06-15 15:45:12 +0200345 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200346 if (cursor < nrdata - 1) {
347 if (cursor >= (maxy - 4 + scrolling))
348 scrolling++;
349 cursor++;
350 }
Daniel Lezcano971515a2011-06-15 15:45:12 +0200351 display_show_selection(current_win, cursor);
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200352
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200353 windata[current_win].scrolling = scrolling;
354 windata[current_win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200355
356 return cursor;
357}
358
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200359int display_prev_line(void)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200360{
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200361 int cursor = windata[current_win].cursor;
362 int nrdata = windata[current_win].nrdata;
363 int scrolling = windata[current_win].scrolling;
364 struct rowdata *rowdata = windata[current_win].rowdata;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200365
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200366 if (cursor >= nrdata)
367 return cursor;
368
Daniel Lezcano971515a2011-06-15 15:45:12 +0200369 display_show_unselection(current_win, cursor, rowdata[cursor].attr);
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200370 if (cursor > 0) {
371 if (cursor <= scrolling)
372 scrolling--;
373 cursor--;
374 }
Daniel Lezcano971515a2011-06-15 15:45:12 +0200375 display_show_selection(current_win, cursor);
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200376
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200377 windata[current_win].scrolling = scrolling;
378 windata[current_win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200379
380 return cursor;
Amit Arora728e0c92010-09-14 12:06:09 +0530381}