[powerdebug,09/22] Encapsulate the display (2)

Message ID 1308145856-6112-9-git-send-email-daniel.lezcano@linaro.org
State Accepted
Headers show

Commit Message

Daniel Lezcano June 15, 2011, 1:50 p.m.
Remove the specific cases in the mainloop and encapsulate the
display functions.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 display.c    |   56 ++++++++++++++++++++++++++++++++++++++------------------
 display.h    |    6 ++++++
 powerdebug.c |   26 +++++++++-----------------
 powerdebug.h |    2 --
 4 files changed, 53 insertions(+), 37 deletions(-)

Patch

diff --git a/display.c b/display.c
index fa7d93a..384714b 100644
--- a/display.c
+++ b/display.c
@@ -31,6 +31,7 @@  enum { PT_COLOR_DEFAULT = 1,
 
 static WINDOW *header_win;
 static WINDOW *footer_win;
+static int current_win;
 
 int maxx, maxy;
 
@@ -103,6 +104,8 @@  int display_init(int wdefault)
 	int i;
 	size_t array_size = sizeof(windata) / sizeof(windata[0]);
 
+	current_win = wdefault;
+
 	if (!initscr())
 		return -1;
 
@@ -204,6 +207,23 @@  void print_sensor_header(void)
 	show_header_footer(SENSOR);
 }
 
+int display_next_panel(void)
+{
+	current_win++;
+	current_win %= TOTAL_FEATURE_WINS;
+
+	return current_win;
+}
+
+int display_prev_panel(void)
+{
+	current_win--;
+	if (current_win < 0)
+		current_win = TOTAL_FEATURE_WINS - 1;
+
+	return current_win;
+}
+
 int display_refresh_pad(int win)
 {
 	return prefresh(windata[win].pad, windata[win].scrolling,
@@ -285,50 +305,50 @@  int display_print_line(int win, int line, char *str, int bold, void *data)
 	return 0;
 }
 
-int display_next_line(int win)
+int display_next_line(void)
 {
-	int cursor = windata[win].cursor;
-	int nrdata = windata[win].nrdata;
-	int scrolling = windata[win].scrolling;
-	struct rowdata *rowdata = windata[win].rowdata;
+	int cursor = windata[current_win].cursor;
+	int nrdata = windata[current_win].nrdata;
+	int scrolling = windata[current_win].scrolling;
+	struct rowdata *rowdata = windata[current_win].rowdata;
 
 	if (cursor >= nrdata)
 		return cursor;
 
-	display_unselect(win, cursor, rowdata[cursor].attr);
+	display_unselect(current_win, cursor, rowdata[cursor].attr);
 	if (cursor < nrdata - 1) {
 		if (cursor >= (maxy - 4 + scrolling))
 			scrolling++;
 		cursor++;
 	}
-	display_select(win, cursor);
+	display_select(current_win, cursor);
 
-	windata[win].scrolling = scrolling;
-	windata[win].cursor = cursor;
+	windata[current_win].scrolling = scrolling;
+	windata[current_win].cursor = cursor;
 
 	return cursor;
 }
 
-int display_prev_line(int win)
+int display_prev_line(void)
 {
-	int cursor = windata[win].cursor;
-	int nrdata = windata[win].nrdata;
-	int scrolling = windata[win].scrolling;
-	struct rowdata *rowdata = windata[win].rowdata;
+	int cursor = windata[current_win].cursor;
+	int nrdata = windata[current_win].nrdata;
+	int scrolling = windata[current_win].scrolling;
+	struct rowdata *rowdata = windata[current_win].rowdata;
 
 	if (cursor >= nrdata)
 		return cursor;
 
-	display_unselect(win, cursor, rowdata[cursor].attr);
+	display_unselect(current_win, cursor, rowdata[cursor].attr);
 	if (cursor > 0) {
 		if (cursor <= scrolling)
 			scrolling--;
 		cursor--;
 	}
-	display_select(win, cursor);
+	display_select(current_win, cursor);
 
-	windata[win].scrolling = scrolling;
-	windata[win].cursor = cursor;
+	windata[current_win].scrolling = scrolling;
+	windata[current_win].cursor = cursor;
 
 	return cursor;
 }
diff --git a/display.h b/display.h
index 0b407fb..9c0e38a 100644
--- a/display.h
+++ b/display.h
@@ -14,3 +14,9 @@ 
  *******************************************************************************/
 
 extern int display_init(int wdefault);
+
+extern int display_next_panel(void);
+extern int display_prev_panel(void);
+extern int display_next_line(void);
+extern int display_prev_line(void);
+extern int display_refresh(void);
diff --git a/powerdebug.c b/powerdebug.c
index 4d94829..e702011 100644
--- a/powerdebug.c
+++ b/powerdebug.c
@@ -164,24 +164,19 @@  int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
 	if (keystroke == EOF)
 		exit(0);
 
-	if (keystroke == KEY_RIGHT || keystroke == '\t') {
-		options->selectedwindow++;
-		options->selectedwindow %= TOTAL_FEATURE_WINS;
-	}
+	if (keystroke == KEY_RIGHT || keystroke == '\t')
+		 options->selectedwindow = display_next_panel();
 
-	if (keystroke == KEY_LEFT || keystroke == KEY_BTAB) {
-		options->selectedwindow--;
-		if (options->selectedwindow < 0)
-			options->selectedwindow = TOTAL_FEATURE_WINS - 1;
-	}
+	if (keystroke == KEY_LEFT || keystroke == KEY_BTAB)
+		 options->selectedwindow = display_prev_panel();
 
 	if (keystroke == KEY_DOWN) {
-		display_next_line(options->selectedwindow);
+		display_next_line();
 		*cont = true;
 	}
 
 	if (keystroke == KEY_UP) {
-		display_prev_line(options->selectedwindow);
+		display_prev_line();
 		*cont = true;
 	}
 
@@ -258,12 +253,12 @@  int mainloop(struct powerdebug_options *options)
 		struct timeval tval;
 		fd_set readfds;
 
-		/* if (options->selectedwindow != CLOCK || !cont) */
-		/* 	show_header(options->selectedwindow); */
-
 		if (options->selectedwindow == REGULATOR)
 			regulator_display();
 
+		if (options->selectedwindow == SENSOR)
+			sensor_display();
+
 		if (options->selectedwindow == CLOCK) {
 
 			if (!cont) {
@@ -281,9 +276,6 @@  int mainloop(struct powerdebug_options *options)
 			} else cont = false;
 		}
 
-		if (options->selectedwindow == SENSOR)
-			sensor_display();
-
 		FD_ZERO(&readfds);
 		FD_SET(0, &readfds);
 		tval.tv_sec = options->ticktime;
diff --git a/powerdebug.h b/powerdebug.h
index 712acb2..df39a8a 100644
--- a/powerdebug.h
+++ b/powerdebug.h
@@ -34,8 +34,6 @@  extern int display_print_line(int window, int line, char *str,
 
 extern int display_refresh_pad(int window);
 extern int display_reset_cursor(int window);
-extern int display_next_line(int window);
-extern int display_prev_line(int window);
 extern void *display_get_row_data(int window);
 
 extern int clock_toggle_expanded(void);