blob: 384714b44ad9b01905e151474060ce268b736b16 [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 Lezcanob3e6e812011-06-15 15:45:12 +020052 struct rowdata *rowdata;
53 char *name;
54 int nrdata;
55 int scrolling;
56 int cursor;
57};
58
59struct windata windata[TOTAL_FEATURE_WINS] = {
60 { .name = "Clocks" },
61 { .name = "Regulators" },
62 { .name = "Sensors" },
63};
Daniel Lezcano2adc48d2011-06-08 23:30:01 +020064
Daniel Lezcano3abd8b12011-03-26 22:06:15 +010065static void display_fini(void)
66{
67 endwin();
68}
69
Daniel Lezcano7b3da502011-06-15 15:45:12 +020070static int show_header_footer(int win)
71{
72 int i;
73 int curr_pointer = 0;
74
75 wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
76 wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
77 werase(header_win);
78
79 print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION);
80 curr_pointer += 20;
81
82 for (i = 0; i < TOTAL_FEATURE_WINS; i++) {
83 if (win == i)
84 wattron(header_win, A_REVERSE);
85 else
86 wattroff(header_win, A_REVERSE);
87
88 print(header_win, curr_pointer, 0, " %s ", windata[i].name);
89 curr_pointer += strlen(windata[i].name) + 2;
90 }
91 wrefresh(header_win);
92 werase(footer_win);
93
94 wattron(footer_win, A_REVERSE);
95 print(footer_win, 0, 0, "%s", footer_label);
96 wattroff(footer_win, A_REVERSE);
97 wrefresh(footer_win);
98
99 return 0;
100}
101
102int display_init(int wdefault)
Amit Arora47fd9182010-08-24 13:26:06 +0530103{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200104 int i;
105 size_t array_size = sizeof(windata) / sizeof(windata[0]);
106
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200107 current_win = wdefault;
108
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100109 if (!initscr())
110 return -1;
111
Amit Arora6e774cd2010-10-28 11:31:24 +0530112 start_color();
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100113 use_default_colors();
114
Amit Arora6e774cd2010-10-28 11:31:24 +0530115 keypad(stdscr, TRUE);
116 noecho();
117 cbreak();
118 curs_set(0);
119 nonl();
Amit Arora47fd9182010-08-24 13:26:06 +0530120
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100121 if (init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK) ||
122 init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED) ||
123 init_pair(PT_COLOR_HEADER_BAR, COLOR_WHITE, COLOR_BLACK) ||
124 init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW) ||
125 init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN) ||
126 init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK) ||
127 init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE) ||
128 init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED))
129 return -1;
Amit Arora47fd9182010-08-24 13:26:06 +0530130
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200131 if (atexit(display_fini))
132 return -1;
133
134 getmaxyx(stdscr, maxy, maxx);
135
Daniel Lezcanof6656822011-06-15 15:45:12 +0200136 for (i = 0; i < array_size; i++) {
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200137
Daniel Lezcanof6656822011-06-15 15:45:12 +0200138 windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0);
139 if (!windata[i].win)
140 return -1;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200141
Daniel Lezcanof6656822011-06-15 15:45:12 +0200142 windata[i].pad = newpad(maxrows, maxx);
143 if (!windata[i].pad)
144 return -1;
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200145
Daniel Lezcanof6656822011-06-15 15:45:12 +0200146 }
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200147
148 header_win = subwin(stdscr, 1, maxx, 0, 0);
149 if (!header_win)
150 return -1;
151
152 footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);
153 if (!footer_win)
154 return -1;
155
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200156 return show_header_footer(wdefault);
Amit Arora47fd9182010-08-24 13:26:06 +0530157}
158
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200159void print_regulator_header(void)
Amit Arora47fd9182010-08-24 13:26:06 +0530160{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200161 WINDOW *regulator_win = windata[REGULATOR].win;
162
Amit Arora47fd9182010-08-24 13:26:06 +0530163 werase(regulator_win);
164 wattron(regulator_win, A_BOLD);
Amit Arorac93e0712010-10-07 13:51:53 +0530165 print(regulator_win, 0, 0, "Name");
166 print(regulator_win, 12, 0, "Status");
167 print(regulator_win, 24, 0, "State");
168 print(regulator_win, 36, 0, "Type");
169 print(regulator_win, 48, 0, "Users");
170 print(regulator_win, 60, 0, "Microvolts");
171 print(regulator_win, 72, 0, "Min u-volts");
172 print(regulator_win, 84, 0, "Max u-volts");
Amit Arora47fd9182010-08-24 13:26:06 +0530173 wattroff(regulator_win, A_BOLD);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200174 wrefresh(regulator_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200175
176 show_header_footer(REGULATOR);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200177}
178
179void print_clock_header(void)
180{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200181 WINDOW *clock_win = windata[CLOCK].win;
182
183 werase(clock_win);
184 wattron(clock_win, A_BOLD);
185 print(clock_win, 0, 0, "Name");
186 print(clock_win, 56, 0, "Flags");
187 print(clock_win, 75, 0, "Rate");
188 print(clock_win, 88, 0, "Usecount");
189 print(clock_win, 98, 0, "Children");
190 wattroff(clock_win, A_BOLD);
191 wrefresh(clock_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200192
193 show_header_footer(CLOCK);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200194}
195
Amit Arora728e0c92010-09-14 12:06:09 +0530196void print_sensor_header(void)
197{
Daniel Lezcanof6656822011-06-15 15:45:12 +0200198 WINDOW *sensor_win = windata[SENSOR].win;
199
Amit Arora6e774cd2010-10-28 11:31:24 +0530200 werase(sensor_win);
201 wattron(sensor_win, A_BOLD);
202 print(sensor_win, 0, 0, "Name");
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200203 print(sensor_win, 36, 0, "Value");
Amit Arora6e774cd2010-10-28 11:31:24 +0530204 wattroff(sensor_win, A_BOLD);
Amit Arora728e0c92010-09-14 12:06:09 +0530205 wrefresh(sensor_win);
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200206
207 show_header_footer(SENSOR);
Amit Arora728e0c92010-09-14 12:06:09 +0530208}
209
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200210int display_next_panel(void)
211{
212 current_win++;
213 current_win %= TOTAL_FEATURE_WINS;
214
215 return current_win;
216}
217
218int display_prev_panel(void)
219{
220 current_win--;
221 if (current_win < 0)
222 current_win = TOTAL_FEATURE_WINS - 1;
223
224 return current_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 Lezcanod96731a2011-06-15 15:45:12 +0200308int display_next_line(void)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200309{
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200310 int cursor = windata[current_win].cursor;
311 int nrdata = windata[current_win].nrdata;
312 int scrolling = windata[current_win].scrolling;
313 struct rowdata *rowdata = windata[current_win].rowdata;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200314
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200315 if (cursor >= nrdata)
316 return cursor;
317
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200318 display_unselect(current_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 Lezcanod96731a2011-06-15 15:45:12 +0200324 display_select(current_win, cursor);
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200325
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200326 windata[current_win].scrolling = scrolling;
327 windata[current_win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200328
329 return cursor;
330}
331
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200332int display_prev_line(void)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200333{
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200334 int cursor = windata[current_win].cursor;
335 int nrdata = windata[current_win].nrdata;
336 int scrolling = windata[current_win].scrolling;
337 struct rowdata *rowdata = windata[current_win].rowdata;
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200338
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200339 if (cursor >= nrdata)
340 return cursor;
341
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200342 display_unselect(current_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 Lezcanod96731a2011-06-15 15:45:12 +0200348 display_select(current_win, cursor);
Daniel Lezcanob3e6e812011-06-15 15:45:12 +0200349
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200350 windata[current_win].scrolling = scrolling;
351 windata[current_win].cursor = cursor;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200352
353 return cursor;
Amit Arora728e0c92010-09-14 12:06:09 +0530354}