Add a mainloop code with registering of the callbacks and co.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- Makefile | 3 +- mainloop.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mainloop.h | 22 +++++++++++ 3 files changed, 146 insertions(+), 1 deletions(-) create mode 100644 mainloop.c create mode 100644 mainloop.h
diff --git a/Makefile b/Makefile index 1a53121..8d41b24 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ MANDIR=/usr/share/man/man8 CFLAGS?=-O1 -g -Wall -Wshadow CC?=gcc
-OBJS = powerdebug.o sensor.o clocks.o regulator.o display.o tree.o utils.o +OBJS = powerdebug.o sensor.o clocks.o regulator.o \ + display.o tree.o utils.o mainloop.o
default: powerdebug
diff --git a/mainloop.c b/mainloop.c new file mode 100644 index 0000000..d63d703 --- /dev/null +++ b/mainloop.c @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (C) 2010, Linaro Limited. + * + * 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 + * + * Author: + * Daniel Lezcano daniel.lezcano@linaro.org + * + *******************************************************************************/ + +#include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <sys/epoll.h> +#include "mainloop.h" + +static int epfd = -1; +static unsigned short nrhandler; + +struct mainloop_data { + mainloop_callback_t cb; + void *data; + int fd; +}; + +struct mainloop_data **mds; + +#define MAX_EVENTS 10 + +int _mainloop(unsigned int timeout) +{ + int i, nfds; + struct epoll_event events[MAX_EVENTS]; + struct mainloop_data *md; + + if (epfd < 0) + return -1; + + for (;;) { + + nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout); + if (nfds < 0) { + if (errno == EINTR) + continue; + return -1; + } + + for (i = 0; i < nfds; i++) { + md = events[i].data.ptr; + + if (md->cb(md->fd, md->data) > 0) + return 0; + } + + } +} + +int mainloop_add(int fd, mainloop_callback_t cb, void *data) +{ + struct epoll_event ev = { + .events = EPOLLIN, + }; + + struct mainloop_data *md; + + if (fd >= nrhandler) { + mds = realloc(mds, sizeof(*mds) * (fd + 1)); + if (!mds) + return -1; + nrhandler = fd + 1; + } + + md = malloc(sizeof(*md)); + if (!md) + return -1; + + md->data = data; + md->cb = cb; + md->fd = fd; + + mds[fd] = md; + ev.data.ptr = md; + + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) { + free(md); + return -1; + } + + return 0; +} + +int mainloop_del(int fd) +{ + if (fd >= nrhandler) + return -1; + + if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0) + return -1; + + free(mds[fd]); + + return 0; +} + +int mainloop_init(void) +{ + epfd = epoll_create(2); + if (epfd < 0) + return -1; + + return 0; +} + +void mainloop_fini(void) +{ + close(epfd); +} diff --git a/mainloop.h b/mainloop.h new file mode 100644 index 0000000..3b4c253 --- /dev/null +++ b/mainloop.h @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (C) 2010, Linaro Limited. + * + * 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 + * + * Author: + * Daniel Lezcano daniel.lezcano@linaro.org + * + *******************************************************************************/ + +typedef int (*mainloop_callback_t)(int fd, void *data); + +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); +extern void mainloop_fini(void);
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 daniel.lezcano@linaro.org --- 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 <stdlib.h> #include <ncurses.h> #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;
We don't need a window per pm block, just a main window refreshed at the right moment is needed.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 66 +++++++++++++++++++++++++++--------------------------------- 1 files changed, 30 insertions(+), 36 deletions(-)
diff --git a/display.c b/display.c index 45446cb..80663a7 100644 --- a/display.c +++ b/display.c @@ -36,6 +36,7 @@ enum { PT_COLOR_DEFAULT = 1,
static WINDOW *header_win; static WINDOW *footer_win; +static WINDOW *main_win; static int current_win;
int maxx, maxy; @@ -52,7 +53,6 @@ struct rowdata { };
struct windata { - WINDOW *win; WINDOW *pad; struct display_ops *ops; struct rowdata *rowdata; @@ -348,8 +348,8 @@ int display_init(int wdefault)
for (i = 0; i < array_size; i++) {
- windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0); - if (!windata[i].win) + main_win = subwin(stdscr, maxy - 2, maxx, 1, 0); + if (!main_win) return -1;
windata[i].pad = newpad(maxrows, maxx); @@ -374,51 +374,45 @@ int display_init(int 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); + werase(main_win); + wattron(main_win, A_BOLD); + print(main_win, 0, 0, "Name"); + print(main_win, 12, 0, "Status"); + print(main_win, 24, 0, "State"); + print(main_win, 36, 0, "Type"); + print(main_win, 48, 0, "Users"); + print(main_win, 60, 0, "Microvolts"); + print(main_win, 72, 0, "Min u-volts"); + print(main_win, 84, 0, "Max u-volts"); + wattroff(main_win, A_BOLD); + wrefresh(main_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); + werase(main_win); + wattron(main_win, A_BOLD); + print(main_win, 0, 0, "Name"); + print(main_win, 56, 0, "Flags"); + print(main_win, 75, 0, "Rate"); + print(main_win, 88, 0, "Usecount"); + print(main_win, 98, 0, "Children"); + wattroff(main_win, A_BOLD); + wrefresh(main_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); + werase(main_win); + wattron(main_win, A_BOLD); + print(main_win, 0, 0, "Name"); + print(main_win, 36, 0, "Value"); + wattroff(main_win, A_BOLD); + wrefresh(main_win);
show_header_footer(SENSOR); }
Remove the 'print' wrapper which inverts the screen coordinate. This is opposite to the ncurses function convention and may lead to wrong code.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 38 ++++++++++++++++++-------------------- 1 files changed, 18 insertions(+), 20 deletions(-)
diff --git a/display.c b/display.c index 80663a7..1af12a3 100644 --- a/display.c +++ b/display.c @@ -22,8 +22,6 @@ #include "regulator.h" #include "display.h"
-#define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } while (0) - enum { PT_COLOR_DEFAULT = 1, PT_COLOR_HEADER_BAR, PT_COLOR_ERROR, @@ -84,7 +82,7 @@ static int show_header_footer(int win) wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR)); werase(header_win);
- print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION); + mvwprintw(header_win, 0, curr_pointer, "PowerDebug %s", VERSION); curr_pointer += 20;
for (i = 0; i < array_size; i++) { @@ -93,14 +91,14 @@ static int show_header_footer(int win) else wattroff(header_win, A_REVERSE);
- print(header_win, curr_pointer, 0, " %s ", windata[i].name); + mvwprintw(header_win, 0, curr_pointer, " %s ", windata[i].name); curr_pointer += strlen(windata[i].name) + 2; } wrefresh(header_win); werase(footer_win);
wattron(footer_win, A_REVERSE); - print(footer_win, 0, 0, "%s", footer_label); + mvwprintw(footer_win, 0, 0, "%s", footer_label); wattroff(footer_win, A_REVERSE); wrefresh(footer_win);
@@ -376,14 +374,14 @@ void print_regulator_header(void) { werase(main_win); wattron(main_win, A_BOLD); - print(main_win, 0, 0, "Name"); - print(main_win, 12, 0, "Status"); - print(main_win, 24, 0, "State"); - print(main_win, 36, 0, "Type"); - print(main_win, 48, 0, "Users"); - print(main_win, 60, 0, "Microvolts"); - print(main_win, 72, 0, "Min u-volts"); - print(main_win, 84, 0, "Max u-volts"); + mvwprintw(main_win, 0, 0, "Name"); + mvwprintw(main_win, 0, 12, "Status"); + mvwprintw(main_win, 0, 24, "State"); + mvwprintw(main_win, 0, 36, "Type"); + mvwprintw(main_win, 0, 48, "Users"); + mvwprintw(main_win, 0, 60, "Microvolts"); + mvwprintw(main_win, 0, 72, "Min u-volts"); + mvwprintw(main_win, 0, 84, "Max u-volts"); wattroff(main_win, A_BOLD); wrefresh(main_win);
@@ -394,11 +392,11 @@ void print_clock_header(void) { werase(main_win); wattron(main_win, A_BOLD); - print(main_win, 0, 0, "Name"); - print(main_win, 56, 0, "Flags"); - print(main_win, 75, 0, "Rate"); - print(main_win, 88, 0, "Usecount"); - print(main_win, 98, 0, "Children"); + mvwprintw(main_win, 0, 0, "Name"); + mvwprintw(main_win, 0, 56, "Flags"); + mvwprintw(main_win, 0, 75, "Rate"); + mvwprintw(main_win, 0, 88, "Usecount"); + mvwprintw(main_win, 0, 98, "Children"); wattroff(main_win, A_BOLD); wrefresh(main_win);
@@ -409,8 +407,8 @@ void print_sensor_header(void) { werase(main_win); wattron(main_win, A_BOLD); - print(main_win, 0, 0, "Name"); - print(main_win, 36, 0, "Value"); + mvwprintw(main_win, 0, 0, "Name"); + mvwprintw(main_win, 0, 36, "Value"); wattroff(main_win, A_BOLD); wrefresh(main_win);
The maxx and maxy variables are already global functions defined in the ncurses library. They are accessible through the getmaxyx macro. Is it not needed to add two more global variables to our code, let's use the code ncurses gives to us.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/display.c b/display.c index 1af12a3..bd5971a 100644 --- a/display.c +++ b/display.c @@ -37,8 +37,6 @@ static WINDOW *footer_win; static WINDOW *main_win; static int current_win;
-int maxx, maxy; - /* Number of lines in the virtual window */ static const int maxrows = 1024;
@@ -119,6 +117,10 @@ int display_refresh(int win)
int display_refresh_pad(int win) { + int maxx, maxy; + + getmaxyx(stdscr, maxy, maxx); + return prefresh(windata[win].pad, windata[win].scrolling, 0, 2, 0, maxy - 2, maxx); } @@ -168,11 +170,14 @@ static int display_prev_panel(void)
static int display_next_line(void) { + int maxx, maxy; 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;
+ getmaxyx(stdscr, maxy, maxx); + if (cursor >= nrdata) return cursor;
@@ -309,7 +314,7 @@ static int display_keystroke(int fd, void *data)
int display_init(int wdefault) { - int i; + int i, maxx, maxy; size_t array_size = sizeof(windata) / sizeof(windata[0]);
current_win = wdefault;
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 20 ++++++++++++++++++-- display.c | 40 +++------------------------------------- display.h | 6 +----- regulator.c | 20 +++++++++++++++++++- sensor.c | 17 ++++++++++++++++- 5 files changed, 57 insertions(+), 46 deletions(-)
diff --git a/clocks.c b/clocks.c index 684bf88..411fcb2 100644 --- a/clocks.c +++ b/clocks.c @@ -286,14 +286,30 @@ static int clock_print_info_cb(struct tree *t, void *data) return 0; }
+static int clock_print_header(void) +{ + char *buf; + int ret; + + if (asprintf(&buf, "%-55s %-16s %-12s %-9s %-8s", + "Name", "Flags", "Rate", "Usecount", "Children") < 0) + return -1; + + ret = display_header_footer(CLOCK, buf); + + free(buf); + + return ret; +} + static int clock_print_info(void) { int ret, line = 0;
- print_clock_header(); - display_reset_cursor(CLOCK);
+ clock_print_header(); + ret = tree_for_each(clock_tree, clock_print_info_cb, &line);
display_refresh_pad(CLOCK); diff --git a/display.c b/display.c index bd5971a..d32783f 100644 --- a/display.c +++ b/display.c @@ -375,49 +375,15 @@ int display_init(int wdefault) return display_refresh(wdefault); }
-void print_regulator_header(void) +int display_header_footer(int win, const char *line) { werase(main_win); wattron(main_win, A_BOLD); - mvwprintw(main_win, 0, 0, "Name"); - mvwprintw(main_win, 0, 12, "Status"); - mvwprintw(main_win, 0, 24, "State"); - mvwprintw(main_win, 0, 36, "Type"); - mvwprintw(main_win, 0, 48, "Users"); - mvwprintw(main_win, 0, 60, "Microvolts"); - mvwprintw(main_win, 0, 72, "Min u-volts"); - mvwprintw(main_win, 0, 84, "Max u-volts"); + mvwprintw(main_win, 0, 0, "%s", line); wattroff(main_win, A_BOLD); wrefresh(main_win);
- show_header_footer(REGULATOR); -} - -void print_clock_header(void) -{ - werase(main_win); - wattron(main_win, A_BOLD); - mvwprintw(main_win, 0, 0, "Name"); - mvwprintw(main_win, 0, 56, "Flags"); - mvwprintw(main_win, 0, 75, "Rate"); - mvwprintw(main_win, 0, 88, "Usecount"); - mvwprintw(main_win, 0, 98, "Children"); - wattroff(main_win, A_BOLD); - wrefresh(main_win); - - show_header_footer(CLOCK); -} - -void print_sensor_header(void) -{ - werase(main_win); - wattron(main_win, A_BOLD); - mvwprintw(main_win, 0, 0, "Name"); - mvwprintw(main_win, 0, 36, "Value"); - wattroff(main_win, A_BOLD); - wrefresh(main_win); - - show_header_footer(SENSOR); + return show_header_footer(win); }
int display_register(int win, struct display_ops *ops) diff --git a/display.h b/display.h index 749d8a3..8586f5e 100644 --- a/display.h +++ b/display.h @@ -29,9 +29,5 @@ 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_header_footer(int win, const char *line); extern int display_refresh(int win); - -/* FIXME */ -extern void print_sensor_header(void); -extern void print_clock_header(void); -extern void print_regulator_header(void); diff --git a/regulator.c b/regulator.c index 696ed10..4151fdb 100644 --- a/regulator.c +++ b/regulator.c @@ -138,13 +138,31 @@ static int regulator_display_cb(struct tree *t, void *data) return 0; }
+static int regulator_print_header(void) +{ + char *buf; + int ret; + + if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s", + "Name", "Status", "State", "Type", "Users", "Microvolts", + "Min u-volts", "Max u-volts") < 0) + return -1; + + ret = display_header_footer(REGULATOR, buf); + + free(buf); + + return ret; + +} + int regulator_display(void) { int ret, line = 0;
display_reset_cursor(REGULATOR);
- print_regulator_header(); + regulator_print_header();
ret = tree_for_each(reg_tree, regulator_display_cb, &line);
diff --git a/sensor.c b/sensor.c index db58137..20602c0 100644 --- a/sensor.c +++ b/sensor.c @@ -235,13 +235,28 @@ static int sensor_display_cb(struct tree *t, void *data) return 0; }
+static int sensor_print_header(void) +{ + char *buf; + int ret; + + if (asprintf(&buf, "%-36s%s", "Name", "Value") < 0) + return -1; + + ret = display_header_footer(SENSOR, buf); + + free(buf); + + return ret; +} + int sensor_display(void) { int ret, line = 0;
display_reset_cursor(SENSOR);
- print_sensor_header(); + sensor_print_header();
ret = tree_for_each(sensor_tree, sensor_display_cb, &line);
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 4 ++-- clocks.h | 1 - regulator.c | 2 +- regulator.h | 1 - sensor.c | 2 +- sensor.h | 1 - 6 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/clocks.c b/clocks.c index 411fcb2..93dd4b3 100644 --- a/clocks.c +++ b/clocks.c @@ -317,7 +317,7 @@ static int clock_print_info(void) return ret; }
-int clock_toggle_expanded(void) +static int clock_toggle_expanded(void) { struct tree *t = display_get_row_data(CLOCK); struct clock_info *clk = t->private; @@ -332,7 +332,7 @@ int clock_toggle_expanded(void) * found in the files. Then print the result to the text based interface * Return 0 on success, < 0 otherwise */ -int clock_display(void) +static int clock_display(void) { if (read_clock_info()) return -1; diff --git a/clocks.h b/clocks.h index ab3bd36..aa94b3b 100644 --- a/clocks.h +++ b/clocks.h @@ -14,5 +14,4 @@ *******************************************************************************/
extern int clock_init(void); -extern int clock_display(void); extern int clock_dump(char *clk); diff --git a/regulator.c b/regulator.c index 4151fdb..c8cbe2d 100644 --- a/regulator.c +++ b/regulator.c @@ -156,7 +156,7 @@ static int regulator_print_header(void)
}
-int regulator_display(void) +static int regulator_display(void) { int ret, line = 0;
diff --git a/regulator.h b/regulator.h index 1b59a57..8b3ec06 100644 --- a/regulator.h +++ b/regulator.h @@ -13,6 +13,5 @@ * - initial API and implementation *******************************************************************************/
-extern int regulator_display(void); extern int regulator_init(void); extern int regulator_dump(void); diff --git a/sensor.c b/sensor.c index 20602c0..399605b 100644 --- a/sensor.c +++ b/sensor.c @@ -250,7 +250,7 @@ static int sensor_print_header(void) return ret; }
-int sensor_display(void) +static int sensor_display(void) { int ret, line = 0;
diff --git a/sensor.h b/sensor.h index 4be90e6..0a069ce 100644 --- a/sensor.h +++ b/sensor.h @@ -14,5 +14,4 @@ *******************************************************************************/
extern int sensor_dump(void); -extern int sensor_display(void); extern int sensor_init(void);
Switch the keyboard callback when we switch to the find mode.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/display.c b/display.c index d32783f..758d17e 100644 --- a/display.c +++ b/display.c @@ -17,6 +17,7 @@ #include <string.h> #include <stdlib.h> #include <ncurses.h> +#include <form.h> #include "powerdebug.h" #include "mainloop.h" #include "regulator.h" @@ -266,6 +267,19 @@ int display_print_line(int win, int line, char *str, int bold, void *data) return 0; }
+static int display_find_keystroke(int fd, void *data); + +static int display_switch_to_find(int fd) +{ + if (mainloop_del(fd)) + return -1; + + if (mainloop_add(fd, display_find_keystroke, NULL)) + return -1; + + return 0; +} + static int display_keystroke(int fd, void *data) { int keystroke = getch(); @@ -299,6 +313,9 @@ static int display_keystroke(int fd, void *data) case 'Q': return 1;
+ case '/': + return display_switch_to_find(fd); + case 'r': case 'R': /* refresh will be done after */ @@ -312,6 +329,35 @@ static int display_keystroke(int fd, void *data) return 0; }
+static int display_switch_to_main(int fd) +{ + if (mainloop_del(fd)) + return -1; + + if (mainloop_add(fd, display_keystroke, NULL)) + return -1; + + display_refresh(current_win); + + return 0; +} + + +static int display_find_keystroke(int fd, void *data) +{ + int keystroke = getch(); + + switch (keystroke) { + + case '\e': + return display_switch_to_main(fd); + default: + break; + } + + return 0; +} + int display_init(int wdefault) { int i, maxx, maxy;
Splitting the header and the footer will help to display a new footer like the search string.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 28 +++++++++++++++++++++------- 1 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/display.c b/display.c index 758d17e..43f3797 100644 --- a/display.c +++ b/display.c @@ -41,9 +41,6 @@ static int current_win; /* Number of lines in the virtual window */ static const int maxrows = 1024;
-#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ - "'Right' , 'Up', 'Down', 'enter', , 'Esc'" - struct rowdata { int attr; void *data; @@ -71,7 +68,7 @@ static void display_fini(void) endwin(); }
-static int show_header_footer(int win) +static int display_show_header(int win) { int i; int curr_pointer = 0; @@ -94,8 +91,16 @@ static int show_header_footer(int win) curr_pointer += strlen(windata[i].name) + 2; } wrefresh(header_win); - werase(footer_win);
+ return 0; +} + +#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ + "'Right' , 'Up', 'Down', 'enter', , 'Esc'" + +static int display_show_footer(int win) +{ + werase(footer_win); wattron(footer_win, A_REVERSE); mvwprintw(footer_win, 0, 0, "%s", footer_label); wattroff(footer_win, A_REVERSE); @@ -415,7 +420,10 @@ int display_init(int wdefault) if (!footer_win) return -1;
- if (show_header_footer(wdefault)) + if (display_show_header(wdefault)) + return -1; + + if (display_show_footer(wdefault)) return -1;
return display_refresh(wdefault); @@ -423,13 +431,19 @@ int display_init(int wdefault)
int display_header_footer(int win, const char *line) { + int ret; + werase(main_win); wattron(main_win, A_BOLD); mvwprintw(main_win, 0, 0, "%s", line); wattroff(main_win, A_BOLD); wrefresh(main_win);
- return show_header_footer(win); + ret = display_show_header(win); + if (ret) + return ret; + + return display_show_footer(win); }
int display_register(int win, struct display_ops *ops)
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 2 +- display.c | 14 ++++---------- display.h | 2 +- regulator.c | 2 +- sensor.c | 2 +- 5 files changed, 8 insertions(+), 14 deletions(-)
diff --git a/clocks.c b/clocks.c index 93dd4b3..59db8a7 100644 --- a/clocks.c +++ b/clocks.c @@ -295,7 +295,7 @@ static int clock_print_header(void) "Name", "Flags", "Rate", "Usecount", "Children") < 0) return -1;
- ret = display_header_footer(CLOCK, buf); + ret = display_column_name(buf);
free(buf);
diff --git a/display.c b/display.c index 43f3797..be78ce0 100644 --- a/display.c +++ b/display.c @@ -293,12 +293,12 @@ static int display_keystroke(int fd, void *data)
case KEY_RIGHT: case '\t': - display_next_panel(); + display_show_header(display_next_panel()); break;
case KEY_LEFT: case KEY_BTAB: - display_prev_panel(); + display_show_header(display_prev_panel()); break;
case KEY_DOWN: @@ -429,21 +429,15 @@ int display_init(int wdefault) return display_refresh(wdefault); }
-int display_header_footer(int win, const char *line) +int display_column_name(const char *line) { - int ret; - werase(main_win); wattron(main_win, A_BOLD); mvwprintw(main_win, 0, 0, "%s", line); wattroff(main_win, A_BOLD); wrefresh(main_win);
- ret = display_show_header(win); - if (ret) - return ret; - - return display_show_footer(win); + return 0; }
int display_register(int win, struct display_ops *ops) diff --git a/display.h b/display.h index 8586f5e..f9a762c 100644 --- a/display.h +++ b/display.h @@ -29,5 +29,5 @@ 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_header_footer(int win, const char *line); extern int display_refresh(int win); +extern int display_column_name(const char *line); diff --git a/regulator.c b/regulator.c index c8cbe2d..97ab15b 100644 --- a/regulator.c +++ b/regulator.c @@ -148,7 +148,7 @@ static int regulator_print_header(void) "Min u-volts", "Max u-volts") < 0) return -1;
- ret = display_header_footer(REGULATOR, buf); + ret = display_column_name(buf);
free(buf);
diff --git a/sensor.c b/sensor.c index 399605b..2a8f4bb 100644 --- a/sensor.c +++ b/sensor.c @@ -243,7 +243,7 @@ static int sensor_print_header(void) if (asprintf(&buf, "%-36s%s", "Name", "Value") < 0) return -1;
- ret = display_header_footer(SENSOR, buf); + ret = display_column_name(buf);
free(buf);
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 113 insertions(+), 8 deletions(-)
diff --git a/display.c b/display.c index be78ce0..12eb052 100644 --- a/display.c +++ b/display.c @@ -17,7 +17,8 @@ #include <string.h> #include <stdlib.h> #include <ncurses.h> -#include <form.h> +#include <sys/types.h> +#include <regex.h> #include "powerdebug.h" #include "mainloop.h" #include "regulator.h" @@ -98,11 +99,11 @@ static int display_show_header(int win) #define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ "'Right' , 'Up', 'Down', 'enter', , 'Esc'"
-static int display_show_footer(int win) +static int display_show_footer(int win, char *string) { werase(footer_win); wattron(footer_win, A_REVERSE); - mvwprintw(footer_win, 0, 0, "%s", footer_label); + mvwprintw(footer_win, 0, 0, "%s", string ? string : footer_label); wattroff(footer_win, A_REVERSE); wrefresh(footer_win);
@@ -274,12 +275,78 @@ int display_print_line(int win, int line, char *str, int bold, void *data)
static int display_find_keystroke(int fd, void *data);
+struct find_data { + size_t len; + char *string; + regex_t *reg; +}; + +struct find_data *display_find_form_init(void) +{ + const char *regexp = "^[a-z|0-9|_|-|.]"; + struct find_data *findd; + const size_t len = 64; + regex_t *reg; + char *search4; + int maxx, maxy; + + getmaxyx(stdscr, maxy, maxx); + + reg = malloc(sizeof(*reg)); + if (!reg) + return NULL; + + if (regcomp(reg, regexp, REG_ICASE)) + goto out_free_reg; + + search4 = malloc(len); + if (!search4) + goto out_free_regcomp; + memset(search4, '\0', len); + + findd = malloc(sizeof(*findd)); + if (!findd) + goto out_free_search4; + + findd->string = search4; + findd->reg = reg; + findd->len = len; +out: + return findd; + +out_free_search4: + free(search4); +out_free_regcomp: + regfree(reg); +out_free_reg: + free(reg); + + goto out; +} + +static void display_find_form_fini(struct find_data *fd) +{ + regfree(fd->reg); + free(fd->string); + free(fd); + curs_set(0); +} + static int display_switch_to_find(int fd) { + struct find_data *findd; + + findd = display_find_form_init(); + if (!findd) + return -1; + if (mainloop_del(fd)) return -1;
- if (mainloop_add(fd, display_find_keystroke, NULL)) + if (mainloop_add(fd, display_find_keystroke, findd)) + return -1; + + if (display_show_footer(current_win, "find (esc to exit)?")) return -1;
return 0; @@ -342,24 +409,62 @@ static int display_switch_to_main(int fd) if (mainloop_add(fd, display_keystroke, NULL)) return -1;
- display_refresh(current_win); + if (display_show_header(current_win)) + return -1;
- return 0; -} + if (display_show_footer(current_win, NULL)) + return -1;
+ return display_refresh(current_win); +}
static int display_find_keystroke(int fd, void *data) { + struct find_data *findd = data; + regex_t *reg = findd->reg; + char *string = findd->string; int keystroke = getch();
+ char match[2] = { [0] = (char)keystroke, [1] = '\0' }; + regmatch_t m[1]; + switch (keystroke) {
case '\e': + display_find_form_fini(findd); return display_switch_to_main(fd); + + case KEY_BACKSPACE: + if (strlen(string)) + string[strlen(string) - 1] = '\0'; + break; + + case KEY_ENTER: + /* next patch */ + break; + default: + + /* We don't want invalid characters for a name */ + if (regexec(reg, match, 1, m, 0)) + return 0; + + if (strlen(string) < findd->len - 1) + string[strlen(string)] = (char)keystroke; + break; }
+ if (display_show_header(current_win)) + return -1; + + if (display_refresh(current_win)) + return -1; + + if (display_show_footer(current_win, strlen(string) ? string : + "find (esc to exit)?")) + return -1; + return 0; }
@@ -423,7 +528,7 @@ int display_init(int wdefault) if (display_show_header(wdefault)) return -1;
- if (display_show_footer(wdefault)) + if (display_show_footer(wdefault, NULL)) return -1;
return display_refresh(wdefault);
and add a 'find' ops.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 87 ++++++++++++++++++++++++++++++++++-------------------------- display.c | 17 ++++++++++-- display.h | 1 + 3 files changed, 64 insertions(+), 41 deletions(-)
diff --git a/clocks.c b/clocks.c index 59db8a7..2ae2f5a 100644 --- a/clocks.c +++ b/clocks.c @@ -159,28 +159,6 @@ static int dump_all_parents(char *clkarg) return tree_for_each_parent(tree, dump_clock_cb, NULL); }
-void find_parents_for_clock(char *clkname, int complete) -{ - char name[256]; - - name[0] = '\0'; - if (!complete) { - char str[256]; - - strcat(name, clkname); - sprintf(str, "Enter Clock Name : %s\n", name); - display_reset_cursor(CLOCK); - display_print_line(CLOCK, 0, str, 1, NULL); - display_refresh_pad(CLOCK); - return; - } - sprintf(name, "Parents for "%s" Clock : \n", clkname); - display_reset_cursor(CLOCK); - display_print_line(CLOCK, 0, name, 1, NULL); - display_refresh_pad(CLOCK); - dump_all_parents(clkname); -} - static inline int read_clock_cb(struct tree *t, void *data) { struct clock_info *clk = t->private; @@ -192,9 +170,9 @@ static inline int read_clock_cb(struct tree *t, void *data) return 0; }
-static int read_clock_info(void) +static int read_clock_info(struct tree *tree) { - return tree_for_each(clock_tree, read_clock_cb, NULL); + return tree_for_each(tree, read_clock_cb, NULL); }
static int fill_clock_cb(struct tree *t, void *data) @@ -259,20 +237,12 @@ free_clkname: return clkline; }
-static int clock_print_info_cb(struct tree *t, void *data) +static int _clock_print_info_cb(struct tree *t, void *data) { struct clock_info *clock = t->private; int *line = data; char *buffer;
- /* we skip the root node of the tree */ - if (!t->parent) - return 0; - - /* show the clock when *all* its parent is expanded */ - if (tree_for_each_parent(t->parent, is_collapsed, NULL)) - return 0; - buffer = clock_line(t); if (!buffer) return -1; @@ -286,6 +256,19 @@ static int clock_print_info_cb(struct tree *t, void *data) return 0; }
+static int clock_print_info_cb(struct tree *t, void *data) +{ + /* we skip the root node of the tree */ + if (!t->parent) + return 0; + + /* show the clock when *all* its parent is expanded */ + if (tree_for_each_parent(t->parent, is_collapsed, NULL)) + return 0; + + return _clock_print_info_cb(t, data); +} + static int clock_print_header(void) { char *buf; @@ -302,7 +285,7 @@ static int clock_print_header(void) return ret; }
-static int clock_print_info(void) +static int clock_print_info(struct tree *tree) { int ret, line = 0;
@@ -310,7 +293,7 @@ static int clock_print_info(void)
clock_print_header();
- ret = tree_for_each(clock_tree, clock_print_info_cb, &line); + ret = tree_for_each(tree, clock_print_info_cb, &line);
display_refresh_pad(CLOCK);
@@ -334,10 +317,37 @@ static int clock_toggle_expanded(void) */ static int clock_display(void) { - if (read_clock_info()) + if (read_clock_info(clock_tree)) return -1;
- return clock_print_info(); + return clock_print_info(clock_tree); +} + +int clock_find(const char *name) +{ + struct tree **ptree = NULL; + int i, nr, line = 0, ret = 0; + + nr = tree_finds(clock_tree, name, &ptree); + + display_reset_cursor(CLOCK); + + for (i = 0; i < nr; i++) { + + ret = read_clock_info(ptree[i]); + if (ret) + break; + + ret = _clock_print_info_cb(ptree[i], &line); + if (ret) + break; + } + + display_refresh_pad(CLOCK); + + free(ptree); + + return ret; }
/* @@ -350,7 +360,7 @@ int clock_dump(char *clk) { int ret;
- if (read_clock_info()) + if (read_clock_info(clock_tree)) return -1;
if (clk) { @@ -370,6 +380,7 @@ int clock_dump(char *clk) static struct display_ops clock_ops = { .display = clock_display, .select = clock_toggle_expanded, + .find = clock_find, };
/* diff --git a/display.c b/display.c index 12eb052..1dc11dd 100644 --- a/display.c +++ b/display.c @@ -16,6 +16,7 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> +#include <ctype.h> #include <ncurses.h> #include <sys/types.h> #include <regex.h> @@ -424,7 +425,6 @@ static int display_find_keystroke(int fd, void *data) regex_t *reg = findd->reg; char *string = findd->string; int keystroke = getch(); - char match[2] = { [0] = (char)keystroke, [1] = '\0' }; regmatch_t m[1];
@@ -434,6 +434,14 @@ static int display_find_keystroke(int fd, void *data) display_find_form_fini(findd); return display_switch_to_main(fd);
+ case KEY_DOWN: + display_next_line(); + break; + + case KEY_UP: + display_prev_line(); + break; + case KEY_BACKSPACE: if (strlen(string)) string[strlen(string) - 1] = '\0'; @@ -455,10 +463,13 @@ static int display_find_keystroke(int fd, void *data) break; }
- if (display_show_header(current_win)) + if (!windata[current_win].ops || !windata[current_win].ops->find) + return 0; + + if (windata[current_win].ops->find(string)) return -1;
- if (display_refresh(current_win)) + if (display_show_header(current_win)) return -1;
if (display_show_footer(current_win, strlen(string) ? string : diff --git a/display.h b/display.h index f9a762c..fe084cb 100644 --- a/display.h +++ b/display.h @@ -18,6 +18,7 @@ enum { CLOCK, REGULATOR, SENSOR }; struct display_ops { int (*display)(void); int (*select)(void); + int (*find)(const char *); };
extern int display_print_line(int window, int line, char *str,
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 28 +++++++++++++++++++++------- display.c | 11 ++++++++--- display.h | 1 + 3 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/clocks.c b/clocks.c index 2ae2f5a..6943b70 100644 --- a/clocks.c +++ b/clocks.c @@ -243,6 +243,10 @@ static int _clock_print_info_cb(struct tree *t, void *data) int *line = data; char *buffer;
+ /* we skip the root node of the tree */ + if (!t->parent) + return 0; + buffer = clock_line(t); if (!buffer) return -1; @@ -258,10 +262,6 @@ static int _clock_print_info_cb(struct tree *t, void *data)
static int clock_print_info_cb(struct tree *t, void *data) { - /* we skip the root node of the tree */ - if (!t->parent) - return 0; - /* show the clock when *all* its parent is expanded */ if (tree_for_each_parent(t->parent, is_collapsed, NULL)) return 0; @@ -300,7 +300,7 @@ static int clock_print_info(struct tree *tree) return ret; }
-static int clock_toggle_expanded(void) +static int clock_select(void) { struct tree *t = display_get_row_data(CLOCK); struct clock_info *clk = t->private; @@ -323,7 +323,7 @@ static int clock_display(void) return clock_print_info(clock_tree); }
-int clock_find(const char *name) +static int clock_find(const char *name) { struct tree **ptree = NULL; int i, nr, line = 0, ret = 0; @@ -350,6 +350,19 @@ int clock_find(const char *name) return ret; }
+static int clock_selectf(void) +{ + struct tree *t = display_get_row_data(CLOCK); + int line = 0; + + display_reset_cursor(CLOCK); + + if (tree_for_each_parent(t, _clock_print_info_cb, &line)) + return -1; + + return display_refresh_pad(CLOCK); +} + /* * Read the clock information and fill the tree with the information * found in the files. Then dump to stdout a formatted result. @@ -379,8 +392,9 @@ int clock_dump(char *clk)
static struct display_ops clock_ops = { .display = clock_display, - .select = clock_toggle_expanded, + .select = clock_select, .find = clock_find, + .selectf = clock_selectf, };
/* diff --git a/display.c b/display.c index 1dc11dd..38596b0 100644 --- a/display.c +++ b/display.c @@ -447,9 +447,14 @@ static int display_find_keystroke(int fd, void *data) string[strlen(string) - 1] = '\0'; break;
- case KEY_ENTER: - /* next patch */ - break; + case '\r': + if (!windata[current_win].ops || !windata[current_win].ops->selectf) + return 0; + + if (windata[current_win].ops->selectf()) + return -1; + + return 0;
default:
diff --git a/display.h b/display.h index fe084cb..7fa5361 100644 --- a/display.h +++ b/display.h @@ -19,6 +19,7 @@ struct display_ops { int (*display)(void); int (*select)(void); int (*find)(const char *); + int (*selectf)(void); };
extern int display_print_line(int window, int line, char *str,
With this patch the content of the debugfs file is readen only when the 'refresh' button is hit and at the init of powerdebug.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 13 +++++++------ display.c | 13 ++++++------- display.h | 3 +-- regulator.c | 2 +- sensor.c | 2 +- 5 files changed, 16 insertions(+), 17 deletions(-)
diff --git a/clocks.c b/clocks.c index 6943b70..0bd0a0e 100644 --- a/clocks.c +++ b/clocks.c @@ -262,6 +262,10 @@ static int _clock_print_info_cb(struct tree *t, void *data)
static int clock_print_info_cb(struct tree *t, void *data) { + /* we skip the root node of the tree */ + if (!t->parent) + return 0; + /* show the clock when *all* its parent is expanded */ if (tree_for_each_parent(t->parent, is_collapsed, NULL)) return 0; @@ -315,9 +319,9 @@ static int clock_select(void) * found in the files. Then print the result to the text based interface * Return 0 on success, < 0 otherwise */ -static int clock_display(void) +static int clock_display(bool refresh) { - if (read_clock_info(clock_tree)) + if (refresh && read_clock_info(clock_tree)) return -1;
return clock_print_info(clock_tree); @@ -334,13 +338,10 @@ static int clock_find(const char *name)
for (i = 0; i < nr; i++) {
- ret = read_clock_info(ptree[i]); - if (ret) - break; - ret = _clock_print_info_cb(ptree[i], &line); if (ret) break; + }
display_refresh_pad(CLOCK); diff --git a/display.c b/display.c index 38596b0..23a4172 100644 --- a/display.c +++ b/display.c @@ -111,14 +111,14 @@ static int display_show_footer(int win, char *string) return 0; }
-int display_refresh(int win) +static int display_refresh(int win, bool read) { /* we are trying to refresh a window which is not showed */ if (win != current_win) return 0;
if (windata[win].ops && windata[win].ops->display) - return windata[win].ops->display(); + return windata[win].ops->display(read);
return 0; } @@ -391,13 +391,12 @@ static int display_keystroke(int fd, void *data)
case 'r': case 'R': - /* refresh will be done after */ - break; + return display_refresh(current_win, true); default: return 0; }
- display_refresh(current_win); + display_refresh(current_win, false);
return 0; } @@ -416,7 +415,7 @@ static int display_switch_to_main(int fd) if (display_show_footer(current_win, NULL)) return -1;
- return display_refresh(current_win); + return display_refresh(current_win, false); }
static int display_find_keystroke(int fd, void *data) @@ -547,7 +546,7 @@ int display_init(int wdefault) if (display_show_footer(wdefault, NULL)) return -1;
- return display_refresh(wdefault); + return display_refresh(wdefault, true); }
int display_column_name(const char *line) diff --git a/display.h b/display.h index 7fa5361..9e4e21a 100644 --- a/display.h +++ b/display.h @@ -16,7 +16,7 @@ enum { CLOCK, REGULATOR, SENSOR };
struct display_ops { - int (*display)(void); + int (*display)(bool refresh); int (*select)(void); int (*find)(const char *); int (*selectf)(void); @@ -31,5 +31,4 @@ 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(int win); extern int display_column_name(const char *line); diff --git a/regulator.c b/regulator.c index 97ab15b..849f906 100644 --- a/regulator.c +++ b/regulator.c @@ -156,7 +156,7 @@ static int regulator_print_header(void)
}
-static int regulator_display(void) +static int regulator_display(bool refresh) { int ret, line = 0;
diff --git a/sensor.c b/sensor.c index 2a8f4bb..d63510e 100644 --- a/sensor.c +++ b/sensor.c @@ -250,7 +250,7 @@ static int sensor_print_header(void) return ret; }
-static int sensor_display(void) +static int sensor_display(bool refresh) { int ret, line = 0;
That prevents to have the selections to collide between the search list result and the main window.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/display.c b/display.c index 23a4172..e1d1325 100644 --- a/display.c +++ b/display.c @@ -280,6 +280,7 @@ struct find_data { size_t len; char *string; regex_t *reg; + int ocursor; };
struct find_data *display_find_form_init(void) @@ -312,6 +313,13 @@ struct find_data *display_find_form_init(void) findd->string = search4; findd->reg = reg; findd->len = len; + + /* save the location of the cursor on the main window in order to + * browse the search result + */ + findd->ocursor = windata[current_win].cursor; + windata[current_win].cursor = 0; + out: return findd;
@@ -327,6 +335,7 @@ out_free_reg:
static void display_find_form_fini(struct find_data *fd) { + windata[current_win].cursor = fd->ocursor; regfree(fd->reg); free(fd->string); free(fd);
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- tree.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/tree.c b/tree.c index f95610a..aefe0fe 100644 --- a/tree.c +++ b/tree.c @@ -304,6 +304,9 @@ static int tree_finds_cb(struct tree *tree, void *data) { struct struct_find *sf = data;
+ if (!strlen(sf->name)) + return 0; + if (strncmp(sf->name, tree->name, strlen(sf->name))) return 0;
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 29 +++++++++++++++++++++-------- 1 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/display.c b/display.c index e1d1325..8f442c1 100644 --- a/display.c +++ b/display.c @@ -281,9 +281,10 @@ struct find_data { char *string; regex_t *reg; int ocursor; + int oscrolling; };
-struct find_data *display_find_form_init(void) +struct find_data *display_find_init(void) { const char *regexp = "^[a-z|0-9|_|-|.]"; struct find_data *findd; @@ -318,8 +319,12 @@ struct find_data *display_find_form_init(void) * browse the search result */ findd->ocursor = windata[current_win].cursor; + findd->oscrolling = windata[current_win].scrolling; + windata[current_win].cursor = 0; + windata[current_win].scrolling = 0;
+ curs_set(1); out: return findd;
@@ -333,12 +338,13 @@ out_free_reg: goto out; }
-static void display_find_form_fini(struct find_data *fd) +static void display_find_fini(struct find_data *findd) { - windata[current_win].cursor = fd->ocursor; - regfree(fd->reg); - free(fd->string); - free(fd); + windata[current_win].cursor = findd->ocursor; + windata[current_win].scrolling = findd->oscrolling; + regfree(findd->reg); + free(findd->string); + free(findd); curs_set(0); }
@@ -346,7 +352,7 @@ static int display_switch_to_find(int fd) { struct find_data *findd;
- findd = display_find_form_init(); + findd = display_find_init(); if (!findd) return -1;
@@ -439,7 +445,7 @@ static int display_find_keystroke(int fd, void *data) switch (keystroke) {
case '\e': - display_find_form_fini(findd); + display_find_fini(findd); return display_switch_to_main(fd);
case KEY_DOWN: @@ -453,6 +459,10 @@ static int display_find_keystroke(int fd, void *data) case KEY_BACKSPACE: if (strlen(string)) string[strlen(string) - 1] = '\0'; + + windata[current_win].cursor = 0; + windata[current_win].scrolling = 0; + break;
case '\r': @@ -473,6 +483,9 @@ static int display_find_keystroke(int fd, void *data) if (strlen(string) < findd->len - 1) string[strlen(string)] = (char)keystroke;
+ windata[current_win].cursor = 0; + windata[current_win].scrolling = 0; + break; }