aboutsummaryrefslogtreecommitdiff
path: root/display.c
blob: f2c9c7a30fd2bc0f3b7ba1df9de185094f480fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*******************************************************************************
 * Copyright (C) 2010, Linaro
 * Copyright (C) 2010, IBM Corporation
 *
 * This file is part of PowerDebug.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Amit Arora <amit.arora@linaro.org> (IBM Corporation)
 *       - initial API and implementation
 *******************************************************************************/

#include "powerdebug.h"

#define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } while (0)
#define NUM_FOOTER_ITEMS 5

static WINDOW *header_win;
static WINDOW *regulator_win;
static WINDOW *clock_win;
static WINDOW *sensor_win;
static WINDOW *footer_win;

int maxx, maxy;
char footer_items[NUM_FOOTER_ITEMS][64];


void fini_curses(void) {
	endwin();
}

void killall_windows(void)
{
	if (header_win) {
		delwin(header_win);
		header_win = NULL;
	}
	if (regulator_win) {
		delwin(regulator_win);
		regulator_win = NULL;
	}
	if (clock_win) {
		delwin(clock_win);
		clock_win = NULL;
	}
	if (sensor_win) {
		delwin(sensor_win);
		sensor_win = NULL;
	}
	if (footer_win) {
		delwin(footer_win);
		footer_win = NULL;
	}
}

void init_curses(void)
{
        initscr();
        start_color();
        keypad(stdscr, TRUE);
        noecho();
        cbreak();
        curs_set(0);
        nonl();
        use_default_colors();

        init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK);
        init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED);
        init_pair(PT_COLOR_HEADER_BAR, COLOR_BLACK, COLOR_WHITE);
        init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW);
        init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN);
        init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK);
        init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE);
        init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED);

        atexit(fini_curses);
}


void create_windows(void)
{

	getmaxyx(stdscr, maxy, maxx);
	killall_windows();

	header_win = subwin(stdscr, 1, maxx, 0, 0);
//	regulator_win = subwin(stdscr, maxy/2 - 2, maxx, 1, 0);
//	clock_win = subwin(stdscr, maxy/2 - 2, maxx, maxy/2, 0);

	footer_win = subwin(stdscr, 1, maxx, maxy-1, 0);

	strcpy(footer_items[0], " Q (Quit) ");
	strcpy(footer_items[1], " R (Refresh) ");

	werase(stdscr);
	refresh();

}

/*
 * maxrows is the MAXIMUM number of rows we need for this window 
 * pshare is the minimum number of rows we should have for this (in %age)
 * maxrows prevails in case of an argument !
 */
int create_regulator_win(int row, int maxrows, int *pshare)
{
        int numrows;
        int idealrows; // Based on pshare provided to us

        if (regulator_win) {
                delwin(regulator_win);
                regulator_win = NULL;
        }

        getmaxyx(stdscr, maxy, maxx);

        idealrows = ((maxy - 2) * (*pshare)) / 100;
        if (maxrows < idealrows) {
                numrows = maxrows;
                *pshare = (numrows * 100) / maxy;
        } else
                numrows = idealrows;
        regulator_win = subwin(stdscr, numrows, maxx, row, 0);

        refresh();

        return numrows + row;
}

int create_clock_win(int row, int maxrows, int *pshare)
{
        int numrows;
        int idealrows;

        if (clock_win) {
                delwin(clock_win);
                clock_win = NULL;
        }

        getmaxyx(stdscr, maxy, maxx);
        idealrows = ((maxy - 2) * (*pshare)) / 100;

        if (maxrows < idealrows)
                numrows = maxrows;
        else
                numrows = idealrows;
        clock_win = subwin(stdscr, numrows, maxx, row, 0);

        refresh();

        return numrows + row;
}

int create_sensor_win(int row, int maxrows, int *pshare)
{
        int numrows;
        int idealrows;

        if (sensor_win) {
                delwin(sensor_win);
                sensor_win = NULL;
        }

        getmaxyx(stdscr, maxy, maxx);
        idealrows = ((maxy - 2) * (*pshare)) / 100;

        if (maxrows < idealrows)
                numrows = maxrows;
        else
                numrows = idealrows;
        sensor_win = subwin(stdscr, numrows, maxx, row, 0);

        refresh();

        return numrows + row;
}

void show_header(void)
{
	int i, j = 0;

	wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
	wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR));
	werase(header_win);

	print(header_win, 0, 0, "PowerDebug version %s         (C) Linaro",
	      VERSION);

	wrefresh(header_win);

	werase(footer_win);

	for (i=0; i<NUM_FOOTER_ITEMS; i++) {
		if (strlen(footer_items[i])==0)
			continue;
		wattron(footer_win, A_REVERSE);
		print(footer_win, j, 0, "%s", footer_items[i]);
		wattroff(footer_win, A_REVERSE);
		j+= strlen(footer_items[i])+1;
	}
	wrefresh(footer_win);
}


void show_regulator_info(int verbose)
{
	int i, count = 2;

	werase(regulator_win);
	wattron(regulator_win, A_BOLD);
	wattron(regulator_win, A_STANDOUT);
	print(regulator_win, 0, 0, "Regulator Information");
	wattroff(regulator_win, A_STANDOUT);
	print(regulator_win, 0, 1, "Name");
	print(regulator_win, 12, 1, "Status");
	print(regulator_win, 24, 1, "State");
	print(regulator_win, 36, 1, "Type");
	print(regulator_win, 48, 1, "Microvolts");
	print(regulator_win, 60, 1, "Min u-volts");
	print(regulator_win, 72, 1, "Max u-volts");
	wattroff(regulator_win, A_BOLD);

	for (i=0; i<numregulators; i++) {
		int col = 0;

		if((i + 2) > (maxy-2))
			break;

		if(!verbose && !strncmp(regulators_info[i].state, "disabled", 8))
			continue;

		print(regulator_win, col, count, "%s",
		      regulators_info[i].name);
		col += 12;
		print(regulator_win, col, count, "%s",
		      regulators_info[i].status);
		col += 12;
		print(regulator_win, col, count, "%s",
		      regulators_info[i].state);
		col += 12;
		print(regulator_win, col, count, "%s",
		      regulators_info[i].type);
		col += 12;
		print(regulator_win, col, count, "%d",
		      regulators_info[i].microvolts);
		col += 12;
		print(regulator_win, col, count, "%d",
		      regulators_info[i].min_microvolts);
		col += 12;
		print(regulator_win, col, count, "%d",
		      regulators_info[i].max_microvolts);

		count++;
	}
	wrefresh(regulator_win);
}


void print_clock_header(int level)
{
        char lev[NAME_MAX];

        sprintf(lev, "(Level %d)\n", level);
        werase(clock_win);
        wattron(clock_win, A_BOLD);
        wattron(clock_win, A_STANDOUT);
        print(clock_win, 0, 0, "Clock Information");
        wattroff(clock_win, A_STANDOUT);
        print(clock_win, 0, 1, "Name");
        print(clock_win, 24, 1, "Flags");
        print(clock_win, 36, 1, "Rate");
        print(clock_win, 48, 1, "Usecount");
        print(clock_win, 60, 1, lev);
        wattroff(clock_win, A_BOLD);
	wrefresh(clock_win);
}

void print_sensor_header(void)
{
        werase(sensor_win);
        wattron(sensor_win, A_BOLD);
        wattron(sensor_win, A_STANDOUT);
        print(sensor_win, 0, 0, "Sensor Information");
        wattroff(sensor_win, A_STANDOUT);
        print(sensor_win, 0, 1, "Name");
        print(sensor_win, 36, 1, "Temperature");
        wattroff(sensor_win, A_BOLD);
        wattron(sensor_win, A_BLINK);
        print(sensor_win, 0, 2, "Currently Sensor information available"
                                  " only in Dump mode!");
        wattroff(sensor_win, A_BLINK);
	wrefresh(sensor_win);
}

void print_clock_info_line(int line, char *clockname, int flags, int rate,
                           int usecount, int highlight)
{
        if (highlight)
                wattron(clock_win, WA_BOLD|WA_STANDOUT);
        else
                wattron(clock_win, WA_DIM);

        print(clock_win, 0, line + 2, "%s", clockname); 
        if (strcmp(clockname, "..")) {
                print(clock_win, 24, line + 2, "%d", flags); 
                print(clock_win, 36, line + 2, "%d", rate); 
                print(clock_win, 48, line + 2, "%d", usecount);
        }

        if (highlight)
                wattroff(clock_win, WA_BOLD|WA_STANDOUT);
        else
                wattroff(clock_win, WA_DIM);

        wrefresh(clock_win);
}