From patchwork Mon Jun 20 22:58:10 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 2094 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 8820F23F4D for ; Mon, 20 Jun 2011 22:58:55 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id 41232A182D4 for ; Mon, 20 Jun 2011 22:58:55 +0000 (UTC) Received: by mail-vx0-f180.google.com with SMTP id 7so2987210vxd.11 for ; Mon, 20 Jun 2011 15:58:55 -0700 (PDT) Received: by 10.52.162.72 with SMTP id xy8mr273083vdb.87.1308610733771; Mon, 20 Jun 2011 15:58:53 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs32801vdc; Mon, 20 Jun 2011 15:58:53 -0700 (PDT) Received: by 10.217.6.81 with SMTP id x59mr61010wes.15.1308610731463; Mon, 20 Jun 2011 15:58:51 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp03.smtpout.orange.fr [80.12.242.125]) by mx.google.com with ESMTP id u17si10349499wec.118.2011.06.20.15.58.50; Mon, 20 Jun 2011 15:58:51 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.125; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.95.191]) by mwinf5d06 with ME id yNyp1g00347kPVY03NyqCg; Tue, 21 Jun 2011 00:58:50 +0200 From: Daniel Lezcano To: linaro-dev@lists.linaro.org Subject: [powerdebug 02/17] make use of the mainloop code Date: Tue, 21 Jun 2011 00:58:10 +0200 Message-Id: <1308610705-23281-2-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1308610705-23281-1-git-send-email-daniel.lezcano@linaro.org> References: <1308610705-23281-1-git-send-email-daniel.lezcano@linaro.org> Use the mainloop we added in the previous patch. Some reorg is needed with the functions, they are moved up and down to prevent forward declarations. Signed-off-by: Daniel Lezcano --- display.c | 265 ++++++++++++++++++++++++++++++--------------------------- display.h | 3 +- mainloop.c | 2 +- mainloop.h | 2 +- powerdebug.c | 43 ++-------- 5 files changed, 149 insertions(+), 166 deletions(-) diff --git a/display.c b/display.c index 257e540..45446cb 100644 --- a/display.c +++ b/display.c @@ -18,6 +18,7 @@ #include #include #include "powerdebug.h" +#include "mainloop.h" #include "regulator.h" #include "display.h" @@ -106,130 +107,14 @@ static int show_header_footer(int win) return 0; } -int display_init(int wdefault) +int display_refresh(int win) { - int i; - size_t array_size = sizeof(windata) / sizeof(windata[0]); - - current_win = wdefault; - - if (!initscr()) - return -1; + /* we are trying to refresh a window which is not showed */ + if (win != current_win) + return 0; - start_color(); - use_default_colors(); - - keypad(stdscr, TRUE); - noecho(); - cbreak(); - curs_set(0); - nonl(); - - if (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_WHITE, COLOR_BLACK) || - 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)) - return -1; - - if (atexit(display_fini)) - return -1; - - getmaxyx(stdscr, maxy, maxx); - - for (i = 0; i < array_size; i++) { - - windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0); - if (!windata[i].win) - return -1; - - windata[i].pad = newpad(maxrows, maxx); - if (!windata[i].pad) - return -1; - - } - - header_win = subwin(stdscr, 1, maxx, 0, 0); - if (!header_win) - return -1; - - footer_win = subwin(stdscr, 1, maxx, maxy-1, 0); - if (!footer_win) - return -1; - - return show_header_footer(wdefault); -} - -void print_regulator_header(void) -{ - WINDOW *regulator_win = windata[REGULATOR].win; - - werase(regulator_win); - wattron(regulator_win, A_BOLD); - print(regulator_win, 0, 0, "Name"); - print(regulator_win, 12, 0, "Status"); - print(regulator_win, 24, 0, "State"); - print(regulator_win, 36, 0, "Type"); - print(regulator_win, 48, 0, "Users"); - print(regulator_win, 60, 0, "Microvolts"); - print(regulator_win, 72, 0, "Min u-volts"); - print(regulator_win, 84, 0, "Max u-volts"); - wattroff(regulator_win, A_BOLD); - wrefresh(regulator_win); - - show_header_footer(REGULATOR); -} - -void print_clock_header(void) -{ - WINDOW *clock_win = windata[CLOCK].win; - - werase(clock_win); - wattron(clock_win, A_BOLD); - print(clock_win, 0, 0, "Name"); - print(clock_win, 56, 0, "Flags"); - print(clock_win, 75, 0, "Rate"); - print(clock_win, 88, 0, "Usecount"); - print(clock_win, 98, 0, "Children"); - wattroff(clock_win, A_BOLD); - wrefresh(clock_win); - - show_header_footer(CLOCK); -} - -void print_sensor_header(void) -{ - WINDOW *sensor_win = windata[SENSOR].win; - - werase(sensor_win); - wattron(sensor_win, A_BOLD); - print(sensor_win, 0, 0, "Name"); - print(sensor_win, 36, 0, "Value"); - wattroff(sensor_win, A_BOLD); - wrefresh(sensor_win); - - show_header_footer(SENSOR); -} - -int display_register(int win, struct display_ops *ops) -{ - size_t array_size = sizeof(windata) / sizeof(windata[0]); - - if (win < 0 || win >= array_size) - return -1; - - windata[win].ops = ops; - - return 0; -} - -int display_refresh(void) -{ - if (windata[current_win].ops && windata[current_win].ops->display) - return windata[current_win].ops->display(); + if (windata[win].ops && windata[win].ops->display) + return windata[win].ops->display(); return 0; } @@ -378,9 +263,8 @@ int display_print_line(int win, int line, char *str, int bold, void *data) return 0; } -int display_keystroke(void *data) +static int display_keystroke(int fd, void *data) { - int *tick = data; int keystroke = getch(); switch (keystroke) { @@ -414,10 +298,139 @@ int display_keystroke(void *data) case 'r': case 'R': - display_refresh(); - *tick = 3; + /* refresh will be done after */ break; + default: + return 0; + } + + display_refresh(current_win); + + return 0; +} + +int display_init(int wdefault) +{ + int i; + size_t array_size = sizeof(windata) / sizeof(windata[0]); + + current_win = wdefault; + + if (mainloop_add(0, display_keystroke, NULL)) + return -1; + + if (!initscr()) + return -1; + + start_color(); + use_default_colors(); + + keypad(stdscr, TRUE); + noecho(); + cbreak(); + curs_set(0); + nonl(); + + if (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_WHITE, COLOR_BLACK) || + 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)) + return -1; + + if (atexit(display_fini)) + return -1; + + getmaxyx(stdscr, maxy, maxx); + + for (i = 0; i < array_size; i++) { + + windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0); + if (!windata[i].win) + return -1; + + windata[i].pad = newpad(maxrows, maxx); + if (!windata[i].pad) + return -1; + } + header_win = subwin(stdscr, 1, maxx, 0, 0); + if (!header_win) + return -1; + + footer_win = subwin(stdscr, 1, maxx, maxy-1, 0); + if (!footer_win) + return -1; + + if (show_header_footer(wdefault)) + return -1; + + return display_refresh(wdefault); +} + +void print_regulator_header(void) +{ + WINDOW *regulator_win = windata[REGULATOR].win; + + werase(regulator_win); + wattron(regulator_win, A_BOLD); + print(regulator_win, 0, 0, "Name"); + print(regulator_win, 12, 0, "Status"); + print(regulator_win, 24, 0, "State"); + print(regulator_win, 36, 0, "Type"); + print(regulator_win, 48, 0, "Users"); + print(regulator_win, 60, 0, "Microvolts"); + print(regulator_win, 72, 0, "Min u-volts"); + print(regulator_win, 84, 0, "Max u-volts"); + wattroff(regulator_win, A_BOLD); + wrefresh(regulator_win); + + show_header_footer(REGULATOR); +} + +void print_clock_header(void) +{ + WINDOW *clock_win = windata[CLOCK].win; + + werase(clock_win); + wattron(clock_win, A_BOLD); + print(clock_win, 0, 0, "Name"); + print(clock_win, 56, 0, "Flags"); + print(clock_win, 75, 0, "Rate"); + print(clock_win, 88, 0, "Usecount"); + print(clock_win, 98, 0, "Children"); + wattroff(clock_win, A_BOLD); + wrefresh(clock_win); + + show_header_footer(CLOCK); +} + +void print_sensor_header(void) +{ + WINDOW *sensor_win = windata[SENSOR].win; + + werase(sensor_win); + wattron(sensor_win, A_BOLD); + print(sensor_win, 0, 0, "Name"); + print(sensor_win, 36, 0, "Value"); + wattroff(sensor_win, A_BOLD); + wrefresh(sensor_win); + + show_header_footer(SENSOR); +} + +int display_register(int win, struct display_ops *ops) +{ + size_t array_size = sizeof(windata) / sizeof(windata[0]); + + if (win < 0 || win >= array_size) + return -1; + + windata[win].ops = ops; + return 0; } diff --git a/display.h b/display.h index f354195..749d8a3 100644 --- a/display.h +++ b/display.h @@ -29,8 +29,7 @@ extern void *display_get_row_data(int window); extern int display_init(int wdefault); extern int display_register(int win, struct display_ops *ops); -extern int display_refresh(void); -extern int display_keystroke(void *data); +extern int display_refresh(int win); /* FIXME */ extern void print_sensor_header(void); diff --git a/mainloop.c b/mainloop.c index d63d703..02dda98 100644 --- a/mainloop.c +++ b/mainloop.c @@ -32,7 +32,7 @@ struct mainloop_data **mds; #define MAX_EVENTS 10 -int _mainloop(unsigned int timeout) +int mainloop(unsigned int timeout) { int i, nfds; struct epoll_event events[MAX_EVENTS]; diff --git a/mainloop.h b/mainloop.h index 3b4c253..cf03bbb 100644 --- a/mainloop.h +++ b/mainloop.h @@ -15,7 +15,7 @@ typedef int (*mainloop_callback_t)(int fd, void *data); -extern int _mainloop(unsigned int timeout); +extern int mainloop(unsigned int timeout); extern int mainloop_add(int fd, mainloop_callback_t cb, void *data); extern int mainloop_del(int fd); extern int mainloop_init(void); diff --git a/powerdebug.c b/powerdebug.c index 065fa31..b4575c4 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -24,6 +24,7 @@ #include "display.h" #include "clocks.h" #include "sensor.h" +#include "mainloop.h" #include "powerdebug.h" void usage(void) @@ -157,38 +158,6 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options) return 0; } -int mainloop(struct powerdebug_options *options) -{ - while (1) { - int ret; - struct timeval tval; - fd_set readfds; - - display_refresh(); - - FD_ZERO(&readfds); - FD_SET(0, &readfds); - tval.tv_sec = options->ticktime; - tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000; - - again: - ret = select(1, &readfds, NULL, NULL, &tval); - if (!ret) - continue; - - if (ret < 0) { - if (errno == EINTR) - goto again; - break; - } - - if (display_keystroke(&options->ticktime)) - break; - } - - return 0; -} - static int powerdebug_dump(struct powerdebug_options *options) { if (options->regulators) @@ -210,10 +179,7 @@ static int powerdebug_display(struct powerdebug_options *options) return -1; } - if (display_refresh()) - return -1; - - if (mainloop(options)) + if (mainloop(options->ticktime * 1000)) return -1; return 0; @@ -248,6 +214,11 @@ int main(int argc, char **argv) return 1; } + if (mainloop_init()) { + fprintf(stderr, "failed to initialize the mainloop\n"); + return 1; + } + if (regulator_init()) { printf("not enough memory to allocate regulators info\n"); options->regulators = false;