Use ncurses to display

Use ncurses library and add option for "dump" mode.
diff --git a/Makefile b/Makefile
index 0226e34..b54141e 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 WARNFLAGS=-Wall -Wshadow -W -Wformat -Wimplicit-function-declaration -Wimplicit-int
-CFLAGS?=-O1 -g ${WARNFLAGS}
+CFLAGS?=-O1 -g ${WARNFLAGS} -lncurses
 CC?=gcc
 
-OBJS = powerdebug.o output.o sensor.o
+OBJS = powerdebug.o output.o sensor.o display.o
 
 
 powerdebug: $(OBJS) powerdebug.h
diff --git a/display.c b/display.c
new file mode 100644
index 0000000..371ea99
--- /dev/null
+++ b/display.c
@@ -0,0 +1,157 @@
+#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 *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 (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_HEADER_BAR, COLOR_BLACK, COLOR_WHITE);
+        init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED);
+        init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED);
+        init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW);
+        init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN);
+        init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE);
+        init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK);
+
+        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-3, maxx, 1, 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();
+
+}
+
+
+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);
+}
+
diff --git a/output.c b/output.c
index 4e251f2..b99f00a 100644
--- a/output.c
+++ b/output.c
@@ -5,6 +5,7 @@
 	printf("Usage: %s [OPTIONS]\n", argv[0]);
 	printf("  -r, --regulator 	Show regulator information\n");
 	printf("  -s, --sensor		Show sensor information\n");
+	printf("  -d, --dump		Dump information once (no refresh)\n");
 	printf("  -v, --verbose		Verbose mode (use with -r and/or -s)\n");
 	printf("  -V, --version		Show Version\n");
 	printf("  -h, --help 		Help\n");
@@ -30,7 +31,7 @@
 	int i;
 
 	for (i=0; i<numregulators; i++) {
-		printf("Regulator # %d\n", i+1);
+		printf("Regulator %d:\n", i+1);
 			print_string_val("name", regulators_info[i].name);
 		if (strcmp(regulators_info[i].status, ""))
 			print_string_val("status", regulators_info[i].status);
diff --git a/powerdebug.c b/powerdebug.c
index aad8349..54267c4 100644
--- a/powerdebug.c
+++ b/powerdebug.c
@@ -1,9 +1,9 @@
 #include <getopt.h>
-
 #include "powerdebug.h"
 
-
 int numregulators;
+int dump;
+int ticktime=3;  /* in seconds */
 
 int init_regulator_ds(void)
 {
@@ -115,8 +115,41 @@
 	return 0;
 }
 
+void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
+{
+	if (!strcmp(ritem->d_name, "name"))
+		strcpy(regulators_info[idx].name, str);
+	if (!strcmp(ritem->d_name, "state"))
+		strcpy(regulators_info[idx].state, str);
+	if (!strcmp(ritem->d_name, "status"))
+		strcpy(regulators_info[idx].status, str);
 
-int read_regulator_info(int verbose)
+	if (!strcmp(ritem->d_name, "type"))
+		strcpy(regulators_info[idx].type, str);
+	if (!strcmp(ritem->d_name, "opmode"))
+		strcpy(regulators_info[idx].opmode, str);
+
+	if (!strcmp(ritem->d_name, "microvolts"))
+		regulators_info[idx].microvolts = atoi(str);
+	if (!strcmp(ritem->d_name, "min_microvolts"))
+		regulators_info[idx].min_microvolts = atoi(str);
+	if (!strcmp(ritem->d_name, "max_microvolts"))
+		regulators_info[idx].max_microvolts = atoi(str);
+
+	if (!strcmp(ritem->d_name, "microamps"))
+		regulators_info[idx].microamps = atoi(str);
+	if (!strcmp(ritem->d_name, "min_microamps"))
+		regulators_info[idx].min_microamps = atoi(str);
+	if (!strcmp(ritem->d_name, "max_microamps"))
+		regulators_info[idx].max_microamps = atoi(str);
+	if (!strcmp(ritem->d_name, "requested_microamps"))
+		regulators_info[idx].requested_microamps = atoi(str);
+
+	if (!strcmp(ritem->d_name, "num_users"))
+		regulators_info[idx].num_users = atoi(str);
+}
+
+int read_regulator_info(void)
 {
 	FILE *file = NULL;
 	DIR *regdir, *dir;
@@ -164,40 +197,7 @@
 			if (!fptr)
 				continue;
 
-			if (!strcmp(ritem->d_name, "name"))
-				strcpy(regulators_info[count-1].name, fptr);
-			if (!strcmp(ritem->d_name, "state"))
-				strcpy(regulators_info[count-1].state, fptr);
-			if (!strcmp(ritem->d_name, "status"))
-				strcpy(regulators_info[count-1].status, fptr);
-
-			/* Read following _only_ if verbose option specified */
-			if(!verbose)
-				continue;
-
-			if (!strcmp(ritem->d_name, "type"))
-				strcpy(regulators_info[count-1].type, fptr);
-			if (!strcmp(ritem->d_name, "opmode"))
-				strcpy(regulators_info[count-1].opmode, fptr);
-
-			if (!strcmp(ritem->d_name, "microvolts"))
-				regulators_info[count-1].microvolts = atoi(fptr);
-			if (!strcmp(ritem->d_name, "min_microvolts"))
-				regulators_info[count-1].min_microvolts = atoi(fptr);
-			if (!strcmp(ritem->d_name, "max_microvolts"))
-				regulators_info[count-1].max_microvolts = atoi(fptr);
-
-			if (!strcmp(ritem->d_name, "microamps"))
-				regulators_info[count-1].microamps = atoi(fptr);
-			if (!strcmp(ritem->d_name, "min_microamps"))
-				regulators_info[count-1].min_microamps = atoi(fptr);
-			if (!strcmp(ritem->d_name, "max_microamps"))
-				regulators_info[count-1].max_microamps = atoi(fptr);
-			if (!strcmp(ritem->d_name, "requested_microamps"))
-				regulators_info[count-1].requested_microamps = atoi(fptr);
-
-			if (!strcmp(ritem->d_name, "num_users"))
-				regulators_info[count-1].num_users = atoi(fptr);
+                        read_info_from_dirent(ritem, fptr, count - 1);
 		}
 exit:
 		closedir(dir);
@@ -213,12 +213,14 @@
 int main(int argc, char **argv)
 {
 	int c;
+	int firsttime = 1;
 	int regulators = 0, sensors = 0, verbose = 0;
 
 	/*
 	 * Options:
 	 * -r, --regulator	: regulator
 	 * -s, --sensor		: sensors
+	 * -d, --dump		: dump
 	 * -v, --verbose	: verbose
 	 * -V, --version	: version
 	 * -h, --help		: help
@@ -230,13 +232,14 @@
 		static struct option long_options[] = {
 			{"regulator", 0, 0, 'r'},
 			{"sensor", 0, 0, 's'},
+			{"dump", 0, 0, 'd'},
 			{"verbose", 0, 0, 'v'},
 			{"version", 0, 0, 'V'},
 			{"help", 0, 0, 'h'},
 			{0, 0, 0, 0}
 		};
 
-		c = getopt_long(argc, argv, "rsvVh", long_options, &optindex);
+		c = getopt_long(argc, argv, "rsdvVh", long_options, &optindex);
 		if (c == -1)
 			break;
 
@@ -247,6 +250,9 @@
 			case 's':
 				sensors = 1;
 				break;
+			case 'd':
+				dump = 1;
+				break;
 			case 'v':
 				verbose = 1;
 				break;
@@ -274,13 +280,56 @@
 
 	init_regulator_ds();
 
-	if (regulators) {
-		read_regulator_info(verbose);
-		print_regulator_info(verbose);
-	}
+	while(1) {
+		int key = 0;
+		struct timeval tval;
+		fd_set readfds;
 
-	if (sensors) {
-		read_and_print_sensor_info(verbose);
+		if (!dump) {
+			if(firsttime) {
+				init_curses();
+				firsttime = 0;
+			}
+			create_windows();
+			show_header();
+		}
+	
+		if (regulators) {
+			read_regulator_info();
+			if (!dump)
+				show_regulator_info(verbose);
+			else
+				print_regulator_info(verbose);
+		}
+
+
+		if (sensors) {
+			read_and_print_sensor_info(verbose);
+		}
+
+		if (dump)
+			break;
+
+		FD_ZERO(&readfds);
+		FD_SET(0, &readfds);
+		tval.tv_sec = ticktime;
+		tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
+
+		key = select(1, &readfds, NULL, NULL, &tval);
+
+		if (key)  {
+			char keychar;
+
+			int keystroke = fgetc(stdin);
+			if (keystroke == EOF)
+				exit(0);
+
+			keychar = toupper(keystroke);
+			if (keychar == 'Q')
+				exit(0);
+			if (keychar == 'R')
+				ticktime = 3;
+		}
 	}
 
 	exit(0);
diff --git a/powerdebug.h b/powerdebug.h
index f1dc382..56e1d85 100644
--- a/powerdebug.h
+++ b/powerdebug.h
@@ -5,6 +5,7 @@
 #include <dirent.h>
 #include <getopt.h>
 #include <errno.h>
+#include <ncurses.h>
 
 #define VERSION "1.0"
 
@@ -27,9 +28,28 @@
 } *regulators_info;
 
 extern int numregulators;
+extern int dump;
 
 extern void usage(char **argv);
 extern void version(void);
 extern void print_regulator_info(int verbose);
 extern void get_sensor_info(char *path, char *name, char *sensor, int verbose);
 extern void print_string_val(char *name, char *val);
+
+#define PT_COLOR_DEFAULT    1
+#define PT_COLOR_HEADER_BAR 2
+#define PT_COLOR_ERROR      3
+#define PT_COLOR_RED        4
+#define PT_COLOR_YELLOW     5
+#define PT_COLOR_GREEN      6
+#define PT_COLOR_BRIGHT     7
+#define PT_COLOR_BLUE       8
+
+
+
+extern void init_curses(void);
+extern void fini_curses(void);
+extern void killall_windows(void);
+extern void show_header(void);
+extern void create_windows(void);
+extern void show_regulator_info(int verbose);