Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- Makefile | 2 +- clocks.c | 62 +------------------------------------------------------------- utils.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 22 ++++++++++++++++++++++ 4 files changed, 79 insertions(+), 62 deletions(-) create mode 100644 utils.c create mode 100644 utils.h
diff --git a/Makefile b/Makefile index d88b8ff..1a53121 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ 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 +OBJS = powerdebug.o sensor.o clocks.o regulator.o display.o tree.o utils.o
default: powerdebug
diff --git a/clocks.c b/clocks.c index 603ebe4..4d78910 100644 --- a/clocks.c +++ b/clocks.c @@ -28,6 +28,7 @@ #include "powerdebug.h" #include "clocks.h" #include "tree.h" +#include "utils.h"
struct clock_info { int flags; @@ -75,67 +76,6 @@ static struct clock_info *clock_alloc(void) return ci; }
-/* - * This functions is a helper to read a specific file content and store - * the content inside a variable pointer passed as parameter, the format - * parameter gives the variable type to be read from the file. - * - * @path : directory path containing the file - * @name : name of the file to be read - * @format : the format of the format - * @value : a pointer to a variable to store the content of the file - * Returns 0 on success, -1 otherwise - */ -int file_read_value(const char *path, const char *name, - const char *format, void *value) -{ - FILE *file; - char *rpath; - int ret; - - ret = asprintf(&rpath, "%s/%s", path, name); - if (ret < 0) - return ret; - - file = fopen(rpath, "r"); - if (!file) { - ret = -1; - goto out_free; - } - - ret = fscanf(file, format, value) == EOF ? -1 : 0; - - fclose(file); -out_free: - free(rpath); - return ret; -} - -static int file_read_from_format(const char *file, int *value, - const char *format) -{ - FILE *f; - int ret; - - f = fopen(file, "r"); - if (!f) - return -1; - ret = fscanf(f, format, value); - fclose(f); - - return !ret ? -1 : 0; -} - -static inline int file_read_int(const char *file, int *value) -{ - return file_read_from_format(file, value, "%d"); -} - -static inline int file_read_hex(const char *file, int *value) -{ - return file_read_from_format(file, value, "%x"); -} - static inline const char *clock_rate(int *rate) { int r; diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..e47c58e --- /dev/null +++ b/utils.c @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (C) 2011, 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 + * + * Contributors: + * Daniel Lezcano daniel.lezcano@linaro.org (IBM Corporation) + * - initial API and implementation + *******************************************************************************/ + +#define _GNU_SOURCE +#include <stdio.h> +#undef _GNU_SOURCE +#include <stdlib.h> + +/* + * This functions is a helper to read a specific file content and store + * the content inside a variable pointer passed as parameter, the format + * parameter gives the variable type to be read from the file. + * + * @path : directory path containing the file + * @name : name of the file to be read + * @format : the format of the format + * @value : a pointer to a variable to store the content of the file + * Returns 0 on success, -1 otherwise + */ +int file_read_value(const char *path, const char *name, + const char *format, void *value) +{ + FILE *file; + char *rpath; + int ret; + + ret = asprintf(&rpath, "%s/%s", path, name); + if (ret < 0) + return ret; + + file = fopen(rpath, "r"); + if (!file) { + ret = -1; + goto out_free; + } + + ret = fscanf(file, format, value) == EOF ? -1 : 0; + + fclose(file); +out_free: + free(rpath); + return ret; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..d4ac65a --- /dev/null +++ b/utils.h @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (C) 2011, 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 + * + * Contributors: + * Daniel Lezcano daniel.lezcano@linaro.org (IBM Corporation) + * - initial API and implementation + *******************************************************************************/ +#ifndef __UTILS_H +#define __UTILS_H + +extern int file_read_value(const char *path, const char *name, + const char *format, void *value); + + +#endif
That allows to keep track of the different selections from the different windows.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 20 ++++++------ display.c | 96 ++++++++++++++++++++++++++++++++++++---------------------- powerdebug.c | 4 +- powerdebug.h | 18 +++++----- 4 files changed, 81 insertions(+), 57 deletions(-)
diff --git a/clocks.c b/clocks.c index 4d78910..c86a39e 100644 --- a/clocks.c +++ b/clocks.c @@ -163,15 +163,15 @@ void find_parents_for_clock(char *clkname, int complete)
strcat(name, clkname); sprintf(str, "Enter Clock Name : %s\n", name); - display_reset_cursor(); - display_print_line(0, str, 1, NULL); - display_refresh_pad(); + 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(); - display_print_line(0, name, 1, NULL); - display_refresh_pad(); + display_reset_cursor(CLOCK); + display_print_line(CLOCK, 0, name, 1, NULL); + display_refresh_pad(CLOCK); dump_all_parents(clkname); }
@@ -271,7 +271,7 @@ static int clock_print_info_cb(struct tree *t, void *data) if (!buffer) return -1;
- display_print_line(*line, buffer, clock->usecount, t); + display_print_line(CLOCK, *line, buffer, clock->usecount, t);
(*line)++;
@@ -286,18 +286,18 @@ static int clock_print_info(void)
print_clock_header();
- display_reset_cursor(); + display_reset_cursor(CLOCK);
ret = tree_for_each(clock_tree, clock_print_info_cb, &line);
- display_refresh_pad(); + display_refresh_pad(CLOCK);
return ret; }
int clock_toggle_expanded(void) { - struct tree *t = display_get_row_data(); + struct tree *t = display_get_row_data(CLOCK); struct clock_info *clk = t->private;
clk->expanded = !clk->expanded; diff --git a/display.c b/display.c index 98dc955..8712023 100644 --- a/display.c +++ b/display.c @@ -44,21 +44,24 @@ static const int maxrows = 1024;
static char footer_items[NUM_FOOTER_ITEMS][64];
-static char *win_names[TOTAL_FEATURE_WINS] = { - "Clocks", - "Regulators", - "Sensors" -}; - struct rowdata { int attr; void *data; };
-static struct rowdata *rowdata; -static int nrdata; -static int scrolling; -static int cursor; +struct windata { + struct rowdata *rowdata; + char *name; + int nrdata; + int scrolling; + int cursor; +}; + +struct windata windata[TOTAL_FEATURE_WINS] = { + { .name = "Clocks" }, + { .name = "Regulators" }, + { .name = "Sensors" }, +};
static void display_fini(void) { @@ -170,8 +173,8 @@ void show_header(int selectedwindow) else wattroff(header_win, A_REVERSE);
- print(header_win, curr_pointer, 0, " %s ", win_names[i]); - curr_pointer += strlen(win_names[i]) + 2; + print(header_win, curr_pointer, 0, " %s ", windata[i].name); + curr_pointer += strlen(windata[i].name) + 2; } wrefresh(header_win); werase(footer_win); @@ -274,69 +277,74 @@ void print_sensor_header(void) wrefresh(sensor_win); }
-int display_refresh_pad(void) +int display_refresh_pad(int win) { - return prefresh(clock_pad, scrolling, 0, 2, 0, maxy - 2, maxx); + return prefresh(clock_pad, windata[win].scrolling, + 0, 2, 0, maxy - 2, maxx); }
-static int inline display_clock_un_select(int line, bool highlight, bool bold) +static int inline display_clock_un_select(int win, int line, + bool highlight, bool bold) { if (mvwchgat(clock_pad, line, 0, -1, highlight ? WA_STANDOUT : bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) return -1;
- return display_refresh_pad(); + return display_refresh_pad(win); }
-int display_clock_select(int line) +int display_select(int win, int line) { - return display_clock_un_select(line, true, false); + return display_clock_un_select(win, line, true, false); }
-int display_clock_unselect(int line, bool bold) +int display_unselect(int win, int line, bool bold) { - return display_clock_un_select(line, false, bold); + return display_clock_un_select(win, line, false, bold); }
-void *display_get_row_data(void) +void *display_get_row_data(int win) { - return rowdata[cursor].data; + return windata[win].rowdata[windata[win].cursor].data; }
-int display_set_row_data(int line, void *data, int attr) +int display_set_row_data(int win, int line, void *data, int attr) { - if (line >= nrdata) { + struct rowdata *rowdata = windata[win].rowdata; + + if (line >= windata[win].nrdata) { rowdata = realloc(rowdata, sizeof(struct rowdata) * (line + 1)); if (!rowdata) return -1; - nrdata = line + 1; + windata[win].nrdata = line + 1; }
rowdata[line].data = data; rowdata[line].attr = attr; + windata[win].rowdata = rowdata;
return 0; }
-int display_reset_cursor(void) +int display_reset_cursor(win) { - nrdata = 0; + windata[win].nrdata = 0; werase(clock_pad); return wmove(clock_pad, 0, 0); }
-int display_print_line(int line, char *str, int bold, void *data) +int display_print_line(int win, int line, char *str, int bold, void *data) { int attr = 0;
if (bold) attr |= WA_BOLD;
- if (line == cursor) + if (line == windata[win].cursor) attr |= WA_STANDOUT;
- if (display_set_row_data(line, data, attr)) + if (display_set_row_data(win, line, data, attr)) return -1;
if (attr) @@ -350,34 +358,50 @@ int display_print_line(int line, char *str, int bold, void *data) return 0; }
-int display_next_line(void) +int display_next_line(int win) { + int cursor = windata[win].cursor; + int nrdata = windata[win].nrdata; + int scrolling = windata[win].scrolling; + struct rowdata *rowdata = windata[win].rowdata; + if (cursor >= nrdata) return cursor;
- display_clock_unselect(cursor, rowdata[cursor].attr); + display_unselect(win, cursor, rowdata[cursor].attr); if (cursor < nrdata - 1) { if (cursor >= (maxy - 4 + scrolling)) scrolling++; cursor++; } - display_clock_select(cursor); + display_select(win, cursor); + + windata[win].scrolling = scrolling; + windata[win].cursor = cursor;
return cursor; }
-int display_prev_line(void) +int display_prev_line(int win) { + int cursor = windata[win].cursor; + int nrdata = windata[win].nrdata; + int scrolling = windata[win].scrolling; + struct rowdata *rowdata = windata[win].rowdata; + if (cursor >= nrdata) return cursor;
- display_clock_unselect(cursor, rowdata[cursor].attr); + display_unselect(win, cursor, rowdata[cursor].attr); if (cursor > 0) { if (cursor <= scrolling) scrolling--; cursor--; } - display_clock_select(cursor); + display_select(win, cursor); + + windata[win].scrolling = scrolling; + windata[win].cursor = cursor;
return cursor; } diff --git a/powerdebug.c b/powerdebug.c index 1873b1b..3a2edac 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -177,12 +177,12 @@ int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, if (options->selectedwindow == CLOCK) {
if (keystroke == KEY_DOWN) { - display_next_line(); + display_next_line(CLOCK); *cont = true; }
if (keystroke == KEY_UP) { - display_prev_line(); + display_prev_line(CLOCK); *cont = true; }
diff --git a/powerdebug.h b/powerdebug.h index 502dcaa..8fd2775 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -35,18 +35,18 @@ extern int print_clock_info(int hrow, int selected); extern void print_string_val(char *name, char *val); extern void print_clock_header(void);
-extern int display_print_line(int line, char *str, int bold, void *data); +extern int display_print_line(int window, int line, char *str, + int bold, void *data);
-extern int display_refresh_pad(void); -extern int display_reset_cursor(void); -extern int display_next_line(void); -extern int display_prev_line(void); - -extern void *display_get_row_data(void); +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); -extern int display_clock_select(int line); -extern int display_clock_unselect(int line, bool bold); +extern int display_clock_select(int window, int line); +extern int display_clock_unselect(int window, int line, bool bold);
extern void get_sensor_info(char *path, char *name, char *sensor, int verbose); extern int read_and_print_sensor_info(int verbose);
Make the code consistent with the clocks and use the tree to build the regulators.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 50 ++++++---- powerdebug.c | 50 +++++---- powerdebug.h | 4 +- regulator.c | 322 ++++++++++++++++++++++++++++++---------------------------- regulator.h | 29 +----- 5 files changed, 227 insertions(+), 228 deletions(-)
diff --git a/display.c b/display.c index 8712023..3795547 100644 --- a/display.c +++ b/display.c @@ -190,13 +190,8 @@ void show_header(int selectedwindow) wrefresh(footer_win); }
- -void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbose) +void print_regulator_header(void) { - int i, count = 1; - - (void)verbose; - werase(regulator_win); wattron(regulator_win, A_BOLD); print(regulator_win, 0, 0, "Name"); @@ -208,6 +203,34 @@ void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbos print(regulator_win, 72, 0, "Min u-volts"); print(regulator_win, 84, 0, "Max u-volts"); wattroff(regulator_win, A_BOLD); + wrefresh(regulator_win); +} + +void print_clock_header(void) +{ + werase(clock_labels); + wattron(clock_labels, A_BOLD); + print(clock_labels, 0, 0, "Name"); + print(clock_labels, 56, 0, "Flags"); + print(clock_labels, 75, 0, "Rate"); + print(clock_labels, 88, 0, "Usecount"); + print(clock_labels, 98, 0, "Children"); + wattroff(clock_labels, A_BOLD); + wrefresh(clock_labels); +} + +#if 0 +void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbose) +{ + int i, count = 1; + + print_regulator_header(); + + wrefresh(regulator_win); + + return; + + (void)verbose;
for (i = 0; i < nr_reg; i++) { int col = 0; @@ -248,20 +271,7 @@ void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbos } wrefresh(regulator_win); } - - -void print_clock_header(void) -{ - werase(clock_labels); - wattron(clock_labels, A_BOLD); - print(clock_labels, 0, 0, "Name"); - print(clock_labels, 56, 0, "Flags"); - print(clock_labels, 75, 0, "Rate"); - print(clock_labels, 88, 0, "Usecount"); - print(clock_labels, 98, 0, "Children"); - wattroff(clock_labels, A_BOLD); - wrefresh(clock_labels); -} +#endif
void print_sensor_header(void) { diff --git a/powerdebug.c b/powerdebug.c index 3a2edac..a303757 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -174,6 +174,21 @@ int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, options->selectedwindow = TOTAL_FEATURE_WINS - 1; }
+#if 0 /* TODO */ + if (options->selectedwindow == REGULATOR) { + + if (keystroke == KEY_DOWN) { + display_next_line(); + *cont = true; + } + + if (keystroke == KEY_UP) { + display_prev_line(); + *cont = true; + } + + } +#endif if (options->selectedwindow == CLOCK) {
if (keystroke == KEY_DOWN) { @@ -242,8 +257,7 @@ int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, return 0; }
-int mainloop(struct powerdebug_options *options, - struct regulator_info *reg_info, int nr_reg) +int mainloop(struct powerdebug_options *options) { bool findparent_ncurses = false; bool refreshwin = false; @@ -264,11 +278,8 @@ int mainloop(struct powerdebug_options *options, create_selectedwindow(options->selectedwindow); }
- if (options->selectedwindow == REGULATOR) { - regulator_read_info(reg_info, nr_reg); - show_regulator_info(reg_info, nr_reg, - options->verbose); - } + if (options->selectedwindow == REGULATOR) + regulator_display();
if (options->selectedwindow == CLOCK) {
@@ -315,13 +326,10 @@ int mainloop(struct powerdebug_options *options, return 0; }
-static int powerdebug_dump(struct powerdebug_options *options, - struct regulator_info *reg_info, int nr_reg) +static int powerdebug_dump(struct powerdebug_options *options) { - if (options->regulators) { - regulator_read_info(reg_info, nr_reg); - regulator_print_info(reg_info, nr_reg, options->verbose); - } + if (options->regulators) + regulator_dump();
if (options->clocks) read_and_dump_clock_info(options->clkname); @@ -332,15 +340,14 @@ static int powerdebug_dump(struct powerdebug_options *options, return 0; }
-static int powerdebug_display(struct powerdebug_options *options, - struct regulator_info *reg_info, int nr_reg) +static int powerdebug_display(struct powerdebug_options *options) { if (display_init()) { printf("failed to initialize display\n"); return -1; }
- if (mainloop(options, reg_info, nr_reg)) + if (mainloop(options)) return -1;
return 0; @@ -362,8 +369,7 @@ static struct powerdebug_options *powerdebug_init(void) int main(int argc, char **argv) { struct powerdebug_options *options; - struct regulator_info *regulators_info; - int numregulators, ret; + int ret;
options = powerdebug_init(); if (!options) { @@ -376,8 +382,7 @@ int main(int argc, char **argv) return 1; }
- regulators_info = regulator_init(&numregulators); - if (!regulators_info) { + if (regulator_init()) { printf("not enough memory to allocate regulators info\n"); options->regulators = false; } @@ -387,9 +392,8 @@ int main(int argc, char **argv) options->clocks = false; }
- ret = options->dump ? - powerdebug_dump(options, regulators_info, numregulators) : - powerdebug_display(options, regulators_info, numregulators); + ret = options->dump ? powerdebug_dump(options) : + powerdebug_display(options);
return ret < 0; } diff --git a/powerdebug.h b/powerdebug.h index 8fd2775..ab1e6f0 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -57,6 +57,4 @@ extern void show_header(int selectedwindow); extern void create_windows(int selectedwindow); extern void create_selectedwindow(int selectedwindow);
-struct regulator_info; -extern void show_regulator_info(struct regulator_info *reg_info, - int nr_reg, int verbose); +extern int regulator_display(void); diff --git a/regulator.c b/regulator.c index 60529ed..d4b41e1 100644 --- a/regulator.c +++ b/regulator.c @@ -11,197 +11,209 @@ * Contributors: * Amit Arora amit.arora@linaro.org (IBM Corporation) * - initial API and implementation + * Daniel Lezcano daniel.lezcano@linaro.org (IBM Corporation) + * - rewrote code and API based on the tree *******************************************************************************/
#include "regulator.h"
#define SYSFS_REGULATOR "/sys/class/regulator" - -struct regulator_info *regulator_init(int *nr_regulators) +#define _GNU_SOURCE +#include <stdio.h> +#undef _GNU_SOURCE +#include <sys/types.h> +#include <stdbool.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include "powerdebug.h" +#include "tree.h" +#include "utils.h" + +struct regulator_info { + char name[NAME_MAX]; + char state[VALUE_MAX]; + char status[VALUE_MAX]; + char type[VALUE_MAX]; + char opmode[VALUE_MAX]; + int microvolts; + int min_microvolts; + int max_microvolts; + int microamps; + int min_microamps; + int max_microamps; + int requested_microamps; + int num_users; +}; + +struct regulator_data { + const char *name; + const char *ifmt; + const char *ofmt; + bool derefme; +}; + +static struct regulator_data regdata[] = { + { "name", "%s", "\tname: %s\n" }, + { "status", "%s", "\tstatus: %s\n" }, + { "state", "%s", "\tstate: %s\n" }, + { "type", "%s", "\ttype: %s\n" }, + { "num_users", "%d", "\tnum_users: %d\n", true }, + { "microvolts", "%d", "\tmicrovolts: %d\n", true }, + { "max_microvolts", "%d", "\tmax_microvolts: %d\n", true }, + { "min_microvolts", "%d", "\tmin_microvolts: %d\n", true }, +}; + +static struct tree *reg_tree; + +static struct regulator_info *regulator_alloc(void) { - DIR *regdir; - struct dirent *item; + struct regulator_info *regi;
- *nr_regulators = 0; + regi = malloc(sizeof(*regi)); + if (regi) + memset(regi, 0, sizeof(*regi));
- regdir = opendir(SYSFS_REGULATOR); - if (!regdir) { - fprintf(stderr, "failed to open '%s': %m\n", SYSFS_REGULATOR); - return NULL; - } + return regi; +}
- while ((item = readdir(regdir))) { +static int regulator_dump_cb(struct tree *tree, void *data) +{ + int i; + char buffer[NAME_MAX]; + size_t nregdata = sizeof(regdata) / sizeof(regdata[0]);
- if (!strcmp(item->d_name, ".")) - continue; + if (!strncmp("regulator.", tree->name, strlen("regulator."))) + printf("\n%s:\n", tree->name);
- if (!strcmp(item->d_name, "..")) + for (i = 0; i < nregdata; i++) { + + if (file_read_value(tree->path, regdata[i].name, + regdata[i].ifmt, buffer)) continue;
- (*nr_regulators)++; + printf(regdata[i].ofmt, regdata[i].derefme ? + *((int *)buffer) : buffer); }
- closedir(regdir); + return 0; +}
- return malloc(*nr_regulators * sizeof(struct regulator_info)); +int regulator_dump(void) +{ + printf("\nRegulator Information:\n"); + printf("*********************\n\n"); + + return tree_for_each(reg_tree, regulator_dump_cb, NULL); }
-static void print_string_val(char *name, char *val) +static int regulator_display_cb(struct tree *t, void *data) { - printf("\t%s=%s", name, val); - if (!strchr(val, '\n')) - printf("\n"); + struct regulator_info *reg = t->private; + int *line = data; + char *buf; + + /* we skip the root node of the tree */ + if (!t->parent) + return 0; + + if (!strlen(reg->name)) + return 0; + + if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11d %-11d %-11d %-12d", + reg->name, reg->status, reg->state, reg->type, + reg->num_users, reg->microvolts, reg->min_microvolts, + reg->max_microvolts) < 0) + return -1; + + display_print_line(REGULATOR, *line, buf, reg->num_users, t); + + (*line)++; + + free(buf); + + return 0; }
-void regulator_print_info(struct regulator_info *reg_info, int nr_reg, int verbose) +int regulator_display(void) { - int i; + int ret, line = 0;
- printf("\nRegulator Information:\n"); - printf("*********************\n\n"); + display_reset_cursor(REGULATOR);
- for (i = 0; i < nr_reg; i++) { - printf("Regulator %d:\n", i + 1); - print_string_val("name", reg_info[i].name); - if (strcmp(reg_info[i].status, "")) - print_string_val("status", reg_info[i].status); - if (strcmp(reg_info[i].state, "")) - print_string_val("state", reg_info[i].state); + print_regulator_header();
- if (!verbose) - continue; + ret = tree_for_each(reg_tree, regulator_display_cb, &line);
- if (strcmp(reg_info[i].type, "")) - print_string_val("type", reg_info[i].type); - if (strcmp(reg_info[i].opmode, "")) - print_string_val("opmode", reg_info[i].opmode); - - if (reg_info[i].microvolts) - printf("\tmicrovolts=%d\n", - reg_info[i].microvolts); - if (reg_info[i].min_microvolts) - printf("\tmin_microvolts=%d\n", - reg_info[i].min_microvolts); - if (reg_info[i].max_microvolts) - printf("\tmax_microvolts=%d\n", - reg_info[i].max_microvolts); - - if (reg_info[i].microamps) - printf("\tmicroamps=%d\n", - reg_info[i].microamps); - if (reg_info[i].min_microamps) - printf("\tmin_microamps=%d\n", - reg_info[i].min_microamps); - if (reg_info[i].max_microamps) - printf("\tmax_microamps=%d\n", - reg_info[i].max_microamps); - if (reg_info[i].requested_microamps) - printf("\trequested_microamps=%d\n", - reg_info[i].requested_microamps); - - if (reg_info[i].num_users) - printf("\tnum_users=%d\n", - reg_info[i].num_users); - printf("\n"); - } + display_refresh_pad(REGULATOR);
- if (!nr_reg && verbose) { - printf("Could not find regulator information!"); - printf(" Looks like %s is empty.\n\n", SYSFS_REGULATOR); - } + return ret; +}
- printf("\n\n"); +static int regulator_filter_cb(const char *name) +{ + /* let's ignore some directories in order to avoid to be + * pulled inside the sysfs circular symlinks mess/hell + * (choose the word which fit better) + */ + if (!strcmp(name, "device")) + return 1; + + if (!strcmp(name, "subsystem")) + return 1; + + if (!strcmp(name, "driver")) + return 1; + + return 0; }
-static void read_info_from_dirent(struct regulator_info *reg_info, - struct dirent *ritem, char *str, int idx) +static inline int read_regulator_cb(struct tree *t, void *data) { - if (!strcmp(ritem->d_name, "name")) - strcpy(reg_info[idx].name, str); - if (!strcmp(ritem->d_name, "state")) - strcpy(reg_info[idx].state, str); - if (!strcmp(ritem->d_name, "status")) - strcpy(reg_info[idx].status, str); - - if (!strcmp(ritem->d_name, "type")) - strcpy(reg_info[idx].type, str); - if (!strcmp(ritem->d_name, "opmode")) - strcpy(reg_info[idx].opmode, str); - - if (!strcmp(ritem->d_name, "microvolts")) - reg_info[idx].microvolts = atoi(str); - if (!strcmp(ritem->d_name, "min_microvolts")) - reg_info[idx].min_microvolts = atoi(str); - if (!strcmp(ritem->d_name, "max_microvolts")) - reg_info[idx].max_microvolts = atoi(str); - - if (!strcmp(ritem->d_name, "microamps")) - reg_info[idx].microamps = atoi(str); - if (!strcmp(ritem->d_name, "min_microamps")) - reg_info[idx].min_microamps = atoi(str); - if (!strcmp(ritem->d_name, "max_microamps")) - reg_info[idx].max_microamps = atoi(str); - if (!strcmp(ritem->d_name, "requested_microamps")) - reg_info[idx].requested_microamps = atoi(str); - - if (!strcmp(ritem->d_name, "num_users")) - reg_info[idx].num_users = atoi(str); + struct regulator_info *reg = t->private; + + file_read_value(t->path, "name", "%s", reg->name); + file_read_value(t->path, "state", "%s", reg->state); + file_read_value(t->path, "status", "%s", reg->status); + file_read_value(t->path, "type", "%s", reg->type); + file_read_value(t->path, "opmode", "%s", reg->opmode); + file_read_value(t->path, "num_users", "%d", ®->num_users); + file_read_value(t->path, "microvolts", "%d", ®->microvolts); + file_read_value(t->path, "min_microvolts", "%d", ®->min_microvolts); + file_read_value(t->path, "max_microvolts", "%d", ®->max_microvolts); + file_read_value(t->path, "microamps", "%d", ®->microamps); + file_read_value(t->path, "min_microamps", "%d", ®->min_microamps); + file_read_value(t->path, "max_microamps", "%d", ®->max_microamps); + + return 0; }
-int regulator_read_info(struct regulator_info *reg_info, int nr_reg) +static int fill_regulator_cb(struct tree *t, void *data) { - FILE *file = NULL; - DIR *regdir, *dir; - int len, count = 0, ret = 0; - char line[1024], filename[1024], *fptr; - struct dirent *item, *ritem; - - regdir = opendir(SYSFS_REGULATOR); - if (!regdir) - return(1); - while ((item = readdir(regdir))) { - if (strlen(item->d_name) < 3) - continue; + struct regulator_info *reg;
- if (strncmp(item->d_name, "regulator", 9)) - continue; + reg = regulator_alloc(); + if (!reg) + return -1; + t->private = reg;
- len = sprintf(filename, "%s/%s", SYSFS_REGULATOR, item->d_name); + /* we skip the root node but we set it expanded for its children */ + if (!t->parent) + return 0;
- dir = opendir(filename); - if (!dir) - continue; - count++; - - if (count > nr_reg) { - ret = 1; - goto exit; - } - - strcpy(reg_info[count-1].name, item->d_name); - while ((ritem = readdir(dir))) { - if (strlen(ritem->d_name) < 3) - continue; - - sprintf(filename + len, "/%s", ritem->d_name); - file = fopen(filename, "r"); - if (!file) - continue; - memset(line, 0, 1024); - fptr = fgets(line, 1024, file); - fclose(file); - if (!fptr) - continue; - - read_info_from_dirent(reg_info, ritem, - fptr, count - 1); - } - exit: - closedir(dir); - if (ret) - break; - } - closedir(regdir); + return read_regulator_cb(t, data); +}
- return ret; +static int fill_regulator_tree(void) +{ + return tree_for_each(reg_tree, fill_regulator_cb, NULL); +} + +int regulator_init(void) +{ + reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb); + if (!reg_tree) + return -1; + + return fill_regulator_tree(); } diff --git a/regulator.h b/regulator.h index 1633cfd..5f8c473 100644 --- a/regulator.h +++ b/regulator.h @@ -13,32 +13,7 @@ * - initial API and implementation *******************************************************************************/
-#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <dirent.h> -#include <getopt.h> - #define VALUE_MAX 16
-struct regulator_info { - char name[NAME_MAX]; - char state[VALUE_MAX]; - char status[VALUE_MAX]; - char type[VALUE_MAX]; - char opmode[VALUE_MAX]; - int microvolts; - int min_microvolts; - int max_microvolts; - int microamps; - int min_microamps; - int max_microamps; - int requested_microamps; - int num_users; -}; - -extern struct regulator_info *regulator_init(int *nr_regulators); -extern int regulator_read_info(struct regulator_info *reg_info, int nr_reg); -extern void regulator_print_info(struct regulator_info *reg_info, - int nr_reg, int verbose); +extern int regulator_init(void); +extern int regulator_dump(void);
Use the tree to build a sensor tree and make the code consistent with the other pm blocks.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 7 ++- powerdebug.h | 2 +- sensor.c | 261 ++++++++++++++++++++++++++++++++-------------------------- 3 files changed, 150 insertions(+), 120 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index a303757..641673b 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -335,7 +335,7 @@ static int powerdebug_dump(struct powerdebug_options *options) read_and_dump_clock_info(options->clkname);
if (options->sensors) - read_and_print_sensor_info(options->verbose); + sensor_dump();
return 0; } @@ -392,6 +392,11 @@ int main(int argc, char **argv) options->clocks = false; }
+ if (sensor_init()) { + printf("failed to initialize sensors\n"); + options->sensors = false; + } + ret = options->dump ? powerdebug_dump(options) : powerdebug_display(options);
diff --git a/powerdebug.h b/powerdebug.h index ab1e6f0..7175839 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -49,7 +49,7 @@ extern int display_clock_select(int window, int line); extern int display_clock_unselect(int window, int line, bool bold);
extern void get_sensor_info(char *path, char *name, char *sensor, int verbose); -extern int read_and_print_sensor_info(int verbose); +extern int sensor_dump(void); extern void print_sensor_header(void);
extern void killall_windows(int all); diff --git a/sensor.c b/sensor.c index df07593..f386f0c 100644 --- a/sensor.c +++ b/sensor.c @@ -15,164 +15,189 @@
#include "powerdebug.h" #include "sensor.h" +#include "tree.h" +#include "utils.h"
-static char *items_temp[32] = {"min", "max", "input", "label", ""}; -static char *suffix_temp[32] = {"°C", "°C", "°C", "", ""}; -static char *items_in[32] = {"min", "max", "input", "label", ""}; -static char *suffix_in[32] = {"Volts", "Volts", "Volts", "", ""}; -static char *items_fan[32] = {"min", "max", "input", "label", "div", "target", ""}; -static char *suffix_fan[32] = {"RPM", "RPM", "RPM", "", "", "RPM", ""}; -static char *items_pwm[32] = {"freq", "enable", "mode", ""}; -static char *suffix_pwm[32] = {"Hz", "", "", ""}; +#define SYSFS_SENSOR "/sys/class/hwmon"
-char *get_num(char *fname, char *sensor) -{ - char tmpstr[NAME_MAX]; - char *str; +static struct tree *sensor_tree;
- strcpy(tmpstr, (fname+strlen(sensor))); +struct temp_info { + char name[NAME_MAX]; + int temp; +};
- str = strrchr(tmpstr, '_'); - str[0] = '\0'; +struct fan_info { + char name[NAME_MAX]; + int rpms; +};
- str = strdup(tmpstr); - return str; -} +struct sensor_info { + char name[NAME_MAX]; + struct temp_info *temperatures; + struct fan_info *fans; + short nrtemps; + short nrfans; +};
-void get_sensor_info(char *path, char *fname, char *sensor, int verbose) +static int sensor_dump_cb(struct tree *tree, void *data) { - FILE *filep; - char filename[PATH_MAX]; - char **items = NULL, **suffix = NULL; - char *item, result[NAME_MAX], *num; - int ret, count = 0; + int i; + struct sensor_info *sensor = tree->private;
- (void)verbose; // get rid of warning + if (!strlen(sensor->name)) + return 0;
- sprintf(filename, "%s/%s", path, fname); + printf("%s\n", sensor->name);
- if (!strcmp(sensor, "in")) { - items = (char **)items_in; - suffix = (char **)suffix_in; - } + for (i = 0; i < sensor->nrtemps; i++) + printf(" %s %.1f °C\n", sensor->temperatures[i].name, + (float)sensor->temperatures[i].temp / 1000);
- if (!strcmp(sensor, "temp")) { - items = (char **)items_temp; - suffix = (char **)suffix_temp; - } - - if (!strcmp(sensor, "fan")) { - items = (char **)items_fan; - suffix = (char **)suffix_fan; - } - if (!strcmp(sensor, "pwm")) { - items = (char **)items_pwm; - suffix = (char **)suffix_pwm; - } + for (i = 0; i < sensor->nrfans; i++) + printf(" %s %d rpm\n", sensor->fans[i].name, + sensor->fans[i].rpms);
+ return 0; +}
- if (!items || !suffix) - return; - - item = strrchr(fname, '_'); - if (!item) - return; - - if (item) - item++; - - if (item > (fname + strlen(fname))) - return; +int sensor_dump(void) +{ + printf("\nSensor Information:\n"); + printf("*******************\n\n");
- num = get_num(fname, sensor); - filep = fopen(filename, "r"); + return tree_for_each(sensor_tree, sensor_dump_cb, NULL); +}
- if (!filep) - goto exit; - ret = fscanf(filep, "%s", result); - fclose(filep); +static struct sensor_info *sensor_alloc(void) +{ + struct sensor_info *sensor;
- if (ret != 1) - goto exit; + sensor = malloc(sizeof(*sensor)); + if (sensor) + memset(sensor, 0, sizeof(*sensor));
- while (strcmp(items[count], "")) { - if (!strcmp(items[count], item)) - printf("'temp' %s sensor %s\t\t%d%s\n", - num, items[count], atoi(result)/1000, - suffix[count]); - count++; - } -exit: - free(num); - return; + return sensor; }
-int read_and_print_sensor_info(int verbose) +static int read_sensor_cb(struct tree *tree, void *data) { - DIR *dir, *subdir; - int len, found = 0; - char filename[PATH_MAX], devpath[PATH_MAX]; - char device[PATH_MAX]; - struct dirent *item, *subitem; + DIR *dir; + int value; + struct dirent dirent, *direntp; + struct sensor_info *sensor = tree->private;
- printf("\nSensor Information:\n"); - printf("******************\n"); + int nrtemps = 0; + int nrfans = 0;
- sprintf(filename, "%s", "/sys/class/hwmon"); - dir = opendir(filename); + dir = opendir(tree->path); if (!dir) - return errno; + return -1; + + file_read_value(tree->path, "name", "%s", sensor->name);
- while ((item = readdir(dir))) { - if (item->d_name[0] == '.') /* skip the hidden files */ + while (!readdir_r(dir, &dirent, &direntp)) { + + if (!direntp) + break; + + if (direntp->d_type != DT_REG) continue;
- found = 1; + if (!strncmp(direntp->d_name, "temp", 4)) { + + if (file_read_value(tree->path, direntp->d_name, "%d", + &value)) + continue;
- sprintf(filename, "/sys/class/hwmon/%s", item->d_name); - sprintf(devpath, "%s/device", filename); + sensor->temperatures = + realloc(sensor->temperatures, + sizeof(struct temp_info) * (nrtemps + 1)); + if (!sensor->temperatures) + continue;
- len = readlink(devpath, device, PATH_MAX - 1); + strcpy(sensor->temperatures[nrtemps].name, + direntp->d_name); + sensor->temperatures[nrtemps].temp = value;
- if (len < 0) - strcpy(devpath, filename); - else - device[len] = '\0'; + nrtemps++; + }
- subdir = opendir(devpath); + if (!strncmp(direntp->d_name, "fan", 3)) {
- printf("\nSensor Information for %s :\n", item->d_name); - fflush(stdin); + if (file_read_value(tree->path, direntp->d_name, "%d", + &value)) + continue;
- while ((subitem = readdir(subdir))) { - if (subitem->d_name[0] == '.') /* skip hidden files */ + sensor->fans = + realloc(sensor->fans, + sizeof(struct temp_info) * (nrfans + 1)); + if (!sensor->fans) continue;
- if (!strncmp(subitem->d_name, "in", 2)) - get_sensor_info(devpath, subitem->d_name, "in", - verbose); - else if (!strncmp(subitem->d_name, "temp", 4)) - get_sensor_info(devpath, subitem->d_name, - "temp", verbose); - else if (!strncmp(subitem->d_name, "fan", 4)) - get_sensor_info(devpath, subitem->d_name, - "fan", verbose); - else if (!strncmp(subitem->d_name, "pwm", 4)) - get_sensor_info(devpath, subitem->d_name, - "pwm", verbose); + strcpy(sensor->fans[nrfans].name, + direntp->d_name); + sensor->fans[nrfans].rpms = value;
+ nrfans++; } - - closedir(subdir); } + + sensor->nrtemps = nrtemps; + sensor->nrfans = nrfans; + closedir(dir);
- if (!found && verbose) { - printf("Could not find sensor information!"); - printf(" Looks like /sys/class/hwmon is empty.\n"); - } + return 0; +}
- printf("\n"); +static int fill_sensor_cb(struct tree *t, void *data) +{ + struct sensor_info *sensor; + + sensor = sensor_alloc(); + if (!sensor) + return -1; + + t->private = sensor; + + if (!t->parent) + return 0; + + return read_sensor_cb(t, data); +} + +static int fill_sensor_tree(void) +{ + return tree_for_each(sensor_tree, fill_sensor_cb, NULL); +} + +static int sensor_filter_cb(const char *name) +{ + /* let's ignore some directories in order to avoid to be + * pulled inside the sysfs circular symlinks mess/hell + * (choose the word which fit better) + */ + if (!strcmp(name, "subsystem")) + return 1; + + if (!strcmp(name, "driver")) + return 1; + + if (!strcmp(name, "hwmon")) + return 1; + + if (!strcmp(name, "power")) + return 1;
return 0; } + +int sensor_init(void) +{ + sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb); + if (!sensor_tree) + return -1; + + return fill_sensor_tree(); +}
Create a pad for each pm blocks, so we can use the same code to scroll the values on the display.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 141 ++++++++++++++++++------------------------------------------- 1 files changed, 41 insertions(+), 100 deletions(-)
diff --git a/display.c b/display.c index 3795547..b417459 100644 --- a/display.c +++ b/display.c @@ -31,10 +31,6 @@ enum { PT_COLOR_DEFAULT = 1, };
static WINDOW *header_win; -static WINDOW *regulator_win; -static WINDOW *clock_pad; -static WINDOW *clock_labels; -static WINDOW *sensor_win; static WINDOW *footer_win;
int maxx, maxy; @@ -50,6 +46,8 @@ struct rowdata { };
struct windata { + WINDOW *win; + WINDOW *pad; struct rowdata *rowdata; char *name; int nrdata; @@ -70,6 +68,9 @@ static void display_fini(void)
int display_init(void) { + int i; + size_t array_size = sizeof(windata) / sizeof(windata[0]); + if (!initscr()) return -1;
@@ -97,21 +98,17 @@ int display_init(void)
getmaxyx(stdscr, maxy, maxx);
- regulator_win = subwin(stdscr, maxy - 2, maxx, 1, 0); - if (!regulator_win) - return -1; + for (i = 0; i < array_size; i++) {
- clock_labels = subwin(stdscr, maxy - 2, maxx, 1, 0); - if (!clock_labels) - return -1; + windata[i].win = subwin(stdscr, maxy - 2, maxx, 1, 0); + if (!windata[i].win) + return -1;
- clock_pad = newpad(maxrows, maxx); - if (!clock_pad) - return -1; + windata[i].pad = newpad(maxrows, maxx); + if (!windata[i].pad) + return -1;
- sensor_win = subwin(stdscr, maxy - 2, maxx, 1, 0); - if (!sensor_win) - return -1; + }
header_win = subwin(stdscr, 1, maxx, 0, 0); if (!header_win) @@ -142,17 +139,9 @@ void create_windows(int selectedwindow)
}
-void create_selectedwindow(int selectedwindow) +void create_selectedwindow(int win) { - switch (selectedwindow) { - case REGULATOR: - wrefresh(regulator_win); - break; - - case SENSOR: - wrefresh(sensor_win); - break; - } + wrefresh(windata[win].win); }
void show_header(int selectedwindow) @@ -192,6 +181,8 @@ void show_header(int selectedwindow)
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"); @@ -208,73 +199,23 @@ void print_regulator_header(void)
void print_clock_header(void) { - werase(clock_labels); - wattron(clock_labels, A_BOLD); - print(clock_labels, 0, 0, "Name"); - print(clock_labels, 56, 0, "Flags"); - print(clock_labels, 75, 0, "Rate"); - print(clock_labels, 88, 0, "Usecount"); - print(clock_labels, 98, 0, "Children"); - wattroff(clock_labels, A_BOLD); - wrefresh(clock_labels); + 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); }
-#if 0 -void show_regulator_info(struct regulator_info *reg_info, int nr_reg, int verbose) -{ - int i, count = 1; - - print_regulator_header(); - - wrefresh(regulator_win); - - return; - - (void)verbose; - - for (i = 0; i < nr_reg; i++) { - int col = 0; - - if ((i + 2) > (maxy-2)) - break; - - if (reg_info[i].num_users > 0) - wattron(regulator_win, WA_BOLD); - else - wattroff(regulator_win, WA_BOLD); - - print(regulator_win, col, count, "%s", - reg_info[i].name); - col += 12; - print(regulator_win, col, count, "%s", - reg_info[i].status); - col += 12; - print(regulator_win, col, count, "%s", - reg_info[i].state); - col += 12; - print(regulator_win, col, count, "%s", - reg_info[i].type); - col += 12; - print(regulator_win, col, count, "%d", - reg_info[i].num_users); - col += 12; - print(regulator_win, col, count, "%d", - reg_info[i].microvolts); - col += 12; - print(regulator_win, col, count, "%d", - reg_info[i].min_microvolts); - col += 12; - print(regulator_win, col, count, "%d", - reg_info[i].max_microvolts); - - count++; - } - wrefresh(regulator_win); -} -#endif - 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"); @@ -289,14 +230,14 @@ void print_sensor_header(void)
int display_refresh_pad(int win) { - return prefresh(clock_pad, windata[win].scrolling, + return prefresh(windata[win].pad, windata[win].scrolling, 0, 2, 0, maxy - 2, maxx); }
-static int inline display_clock_un_select(int win, int line, +static int inline display_un_select(int win, int line, bool highlight, bool bold) { - if (mvwchgat(clock_pad, line, 0, -1, + if (mvwchgat(windata[win].pad, line, 0, -1, highlight ? WA_STANDOUT : bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) return -1; @@ -306,12 +247,12 @@ static int inline display_clock_un_select(int win, int line,
int display_select(int win, int line) { - return display_clock_un_select(win, line, true, false); + return display_un_select(win, line, true, false); }
int display_unselect(int win, int line, bool bold) { - return display_clock_un_select(win, line, false, bold); + return display_un_select(win, line, false, bold); }
void *display_get_row_data(int win) @@ -337,11 +278,11 @@ int display_set_row_data(int win, int line, void *data, int attr) return 0; }
-int display_reset_cursor(win) +int display_reset_cursor(int win) { windata[win].nrdata = 0; - werase(clock_pad); - return wmove(clock_pad, 0, 0); + werase(windata[win].pad); + return wmove(windata[win].pad, 0, 0); }
int display_print_line(int win, int line, char *str, int bold, void *data) @@ -358,12 +299,12 @@ int display_print_line(int win, int line, char *str, int bold, void *data) return -1;
if (attr) - wattron(clock_pad, attr); + wattron(windata[win].pad, attr);
- wprintw(clock_pad, "%s\n", str); + wprintw(windata[win].pad, "%s\n", str);
if (attr) - wattroff(clock_pad, attr); + wattroff(windata[win].pad, attr);
return 0; }
Add the sensor display.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 6 +----- powerdebug.c | 33 +++++++++------------------------ sensor.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 29 deletions(-)
diff --git a/display.c b/display.c index b417459..c6a68a7 100644 --- a/display.c +++ b/display.c @@ -219,12 +219,8 @@ void print_sensor_header(void) werase(sensor_win); wattron(sensor_win, A_BOLD); print(sensor_win, 0, 0, "Name"); - print(sensor_win, 36, 0, "Temperature"); + print(sensor_win, 36, 0, "Value"); wattroff(sensor_win, A_BOLD); - wattron(sensor_win, A_BLINK); - print(sensor_win, 0, 1, "Currently Sensor information available" - " only in Dump mode!"); - wattroff(sensor_win, A_BLINK); wrefresh(sensor_win); }
diff --git a/powerdebug.c b/powerdebug.c index 641673b..24db8c8 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -174,32 +174,17 @@ int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, options->selectedwindow = TOTAL_FEATURE_WINS - 1; }
-#if 0 /* TODO */ - if (options->selectedwindow == REGULATOR) { - - if (keystroke == KEY_DOWN) { - display_next_line(); - *cont = true; - } - - if (keystroke == KEY_UP) { - display_prev_line(); - *cont = true; - } - + if (keystroke == KEY_DOWN) { + display_next_line(options->selectedwindow); + *cont = true; } -#endif - if (options->selectedwindow == CLOCK) {
- if (keystroke == KEY_DOWN) { - display_next_line(CLOCK); - *cont = true; - } + if (keystroke == KEY_UP) { + display_prev_line(options->selectedwindow); + *cont = true; + }
- if (keystroke == KEY_UP) { - display_prev_line(CLOCK); - *cont = true; - } + if (options->selectedwindow == CLOCK) {
#if 0 /* TODO : fix with a new panel applicable for all subsystems */ @@ -299,7 +284,7 @@ int mainloop(struct powerdebug_options *options) }
if (options->selectedwindow == SENSOR) - print_sensor_header(); + sensor_display();
FD_ZERO(&readfds); FD_SET(0, &readfds); diff --git a/sensor.c b/sensor.c index f386f0c..9c97e72 100644 --- a/sensor.c +++ b/sensor.c @@ -13,6 +13,15 @@ * - initial API and implementation *******************************************************************************/
+#define _GNU_SOURCE +#include <stdio.h> +#undef _GNU_SOURCE +#include <sys/types.h> +#include <stdbool.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> + #include "powerdebug.h" #include "sensor.h" #include "tree.h" @@ -193,6 +202,53 @@ static int sensor_filter_cb(const char *name) return 0; }
+static int sensor_display_cb(struct tree *t, void *data) +{ + struct sensor_info *sensor = t->private; + int *line = data; + char buf[1024]; + int i; + + if (!strlen(sensor->name)) + return 0; + + sprintf(buf, "%s", sensor->name); + display_print_line(SENSOR, *line, buf, 1, t); + + (*line)++; + + for (i = 0; i < sensor->nrtemps; i++) { + sprintf(buf, " %-35s%.1f °C", sensor->temperatures[i].name, + (float)sensor->temperatures[i].temp / 1000); + display_print_line(SENSOR, *line, buf, 0, t); + (*line)++; + } + + for (i = 0; i < sensor->nrfans; i++) { + sprintf(buf, " %-35s%d rpm", sensor->fans[i].name, + sensor->fans[i].rpms); + display_print_line(SENSOR, *line, buf, 0, t); + (*line)++; + } + + return 0; +} + +int sensor_display(void) +{ + int ret, line = 0; + + display_reset_cursor(SENSOR); + + print_sensor_header(); + + ret = tree_for_each(sensor_tree, sensor_display_cb, &line); + + display_refresh_pad(SENSOR); + + return ret; +} + int sensor_init(void) { sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb);
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 4 ++-- clocks.h | 4 ++-- powerdebug.c | 5 +++-- sensor.h | 3 +++ 4 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/clocks.c b/clocks.c index c86a39e..db219d8 100644 --- a/clocks.c +++ b/clocks.c @@ -332,7 +332,7 @@ int clock_init(void) * found in the files. Then print the result to the text based interface * Return 0 on success, < 0 otherwise */ -int read_and_print_clock_info(void) +int clock_display(void) { if (read_clock_info()) return -1; @@ -346,7 +346,7 @@ int read_and_print_clock_info(void) * @clk : a name for a specific clock we want to show * Return 0 on success, < 0 otherwise */ -int read_and_dump_clock_info(char *clk) +int clock_dump(char *clk) { int ret;
diff --git a/clocks.h b/clocks.h index d66c061..ab3bd36 100644 --- a/clocks.h +++ b/clocks.h @@ -13,6 +13,6 @@ * - initial API and implementation *******************************************************************************/
-extern int maxy; - extern int clock_init(void); +extern int clock_display(void); +extern int clock_dump(char *clk); diff --git a/powerdebug.c b/powerdebug.c index 24db8c8..0eee7c5 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -19,6 +19,7 @@ #include "regulator.h" #include "display.h" #include "clocks.h" +#include "sensor.h" #include "powerdebug.h"
void usage(void) @@ -275,7 +276,7 @@ int mainloop(struct powerdebug_options *options) if (enter_hit) clock_toggle_expanded();
- read_and_print_clock_info(); + clock_display(); enter_hit = false; } else find_parents_for_clock(clkname_str, @@ -317,7 +318,7 @@ static int powerdebug_dump(struct powerdebug_options *options) regulator_dump();
if (options->clocks) - read_and_dump_clock_info(options->clkname); + clock_dump(options->clkname);
if (options->sensors) sensor_dump(); diff --git a/sensor.h b/sensor.h index 8537149..4be90e6 100644 --- a/sensor.h +++ b/sensor.h @@ -13,3 +13,6 @@ * - initial API and implementation *******************************************************************************/
+extern int sensor_dump(void); +extern int sensor_display(void); +extern int sensor_init(void);
Remove some corner cases for the footer display we have the same display feature for all the pm blocks.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 104 +++++++++++++++++++++++---------------------------------- display.h | 2 +- powerdebug.c | 9 ++--- powerdebug.h | 20 ++--------- 4 files changed, 49 insertions(+), 86 deletions(-)
diff --git a/display.c b/display.c index c6a68a7..fa7d93a 100644 --- a/display.c +++ b/display.c @@ -18,7 +18,6 @@ #include "display.h"
#define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } while (0) -#define NUM_FOOTER_ITEMS 5
enum { PT_COLOR_DEFAULT = 1, PT_COLOR_HEADER_BAR, @@ -38,7 +37,8 @@ int maxx, maxy; /* Number of lines in the virtual window */ static const int maxrows = 1024;
-static char footer_items[NUM_FOOTER_ITEMS][64]; +#define footer_label " Q (Quit) R (Refresh) Other Keys: 'Left', " \ + "'Right' , 'Up', 'Down', 'enter', , 'Esc'"
struct rowdata { int attr; @@ -66,7 +66,39 @@ static void display_fini(void) endwin(); }
-int display_init(void) +static int show_header_footer(int win) +{ + int i; + int curr_pointer = 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, curr_pointer, 0, "PowerDebug %s", VERSION); + curr_pointer += 20; + + for (i = 0; i < TOTAL_FEATURE_WINS; i++) { + if (win == i) + wattron(header_win, A_REVERSE); + else + wattroff(header_win, A_REVERSE); + + print(header_win, curr_pointer, 0, " %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); + wattroff(footer_win, A_REVERSE); + wrefresh(footer_win); + + return 0; +} + +int display_init(int wdefault) { int i; size_t array_size = sizeof(windata) / sizeof(windata[0]); @@ -118,65 +150,7 @@ int display_init(void) if (!footer_win) return -1;
- return 0; -} - -void create_windows(int selectedwindow) -{ - strcpy(footer_items[0], " Q (Quit) "); - strcpy(footer_items[1], " R (Refresh) "); - - if (selectedwindow == CLOCK) - strcpy(footer_items[2], " Other Keys: 'Left', 'Right', 'Up', 'Down', 'enter', " - " '/', 'Esc' "); - else - strcpy(footer_items[2], " Other Keys: 'Left', 'Right' "); - - strcpy(footer_items[3], ""); - - werase(stdscr); - refresh(); - -} - -void create_selectedwindow(int win) -{ - wrefresh(windata[win].win); -} - -void show_header(int selectedwindow) -{ - int i, j = 0; - int curr_pointer = 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, curr_pointer, 0, "PowerDebug %s", VERSION); - curr_pointer += 20; - - for (i = 0; i < TOTAL_FEATURE_WINS; i++) { - if (selectedwindow == i) - wattron(header_win, A_REVERSE); - else - wattroff(header_win, A_REVERSE); - - print(header_win, curr_pointer, 0, " %s ", windata[i].name); - curr_pointer += strlen(windata[i].name) + 2; - } - 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); + return show_header_footer(wdefault); }
void print_regulator_header(void) @@ -195,6 +169,8 @@ void print_regulator_header(void) 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) @@ -210,6 +186,8 @@ void print_clock_header(void) print(clock_win, 98, 0, "Children"); wattroff(clock_win, A_BOLD); wrefresh(clock_win); + + show_header_footer(CLOCK); }
void print_sensor_header(void) @@ -222,6 +200,8 @@ void print_sensor_header(void) print(sensor_win, 36, 0, "Value"); wattroff(sensor_win, A_BOLD); wrefresh(sensor_win); + + show_header_footer(SENSOR); }
int display_refresh_pad(int win) diff --git a/display.h b/display.h index 047d674..0b407fb 100644 --- a/display.h +++ b/display.h @@ -13,4 +13,4 @@ * - initial API and implementation *******************************************************************************/
-extern int display_init(void); +extern int display_init(int wdefault); diff --git a/powerdebug.c b/powerdebug.c index 0eee7c5..4d94829 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -258,11 +258,8 @@ int mainloop(struct powerdebug_options *options) struct timeval tval; fd_set readfds;
- if (options->selectedwindow != CLOCK || !cont) { - create_windows(options->selectedwindow); - show_header(options->selectedwindow); - create_selectedwindow(options->selectedwindow); - } + /* if (options->selectedwindow != CLOCK || !cont) */ + /* show_header(options->selectedwindow); */
if (options->selectedwindow == REGULATOR) regulator_display(); @@ -328,7 +325,7 @@ static int powerdebug_dump(struct powerdebug_options *options)
static int powerdebug_display(struct powerdebug_options *options) { - if (display_init()) { + if (display_init(options->selectedwindow)) { printf("failed to initialize display\n"); return -1; } diff --git a/powerdebug.h b/powerdebug.h index 7175839..712acb2 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -28,13 +28,7 @@ enum {CLOCK, REGULATOR, SENSOR}; enum {CLOCK_SELECTED = 1, REFRESH_WINDOW};
-extern int read_and_dump_clock_info(char *clk); extern void find_parents_for_clock(char *clkname, int complete); -extern int read_and_print_clock_info(void); -extern int print_clock_info(int hrow, int selected); -extern void print_string_val(char *name, char *val); -extern void print_clock_header(void); - extern int display_print_line(int window, int line, char *str, int bold, void *data);
@@ -45,16 +39,8 @@ extern int display_prev_line(int window); extern void *display_get_row_data(int window);
extern int clock_toggle_expanded(void); -extern int display_clock_select(int window, int line); -extern int display_clock_unselect(int window, int line, bool bold); - -extern void get_sensor_info(char *path, char *name, char *sensor, int verbose); -extern int sensor_dump(void); +extern int regulator_display(void); extern void print_sensor_header(void); +extern void print_clock_header(void); +extern void print_regulator_header(void);
-extern void killall_windows(int all); -extern void show_header(int selectedwindow); -extern void create_windows(int selectedwindow); -extern void create_selectedwindow(int selectedwindow); - -extern int regulator_display(void);
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(-)
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);
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 30 ++++++++---------------------- 1 files changed, 8 insertions(+), 22 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index e702011..f73aafe 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -154,7 +154,7 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options) }
int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, - char *clkname_str, bool *refreshwin, bool *cont, + char *clkname_str, bool *refreshwin, struct powerdebug_options *options) { char keychar; @@ -170,15 +170,11 @@ int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, if (keystroke == KEY_LEFT || keystroke == KEY_BTAB) options->selectedwindow = display_prev_panel();
- if (keystroke == KEY_DOWN) { + if (keystroke == KEY_DOWN) display_next_line(); - *cont = true; - }
- if (keystroke == KEY_UP) { + if (keystroke == KEY_UP) display_prev_line(); - *cont = true; - }
if (options->selectedwindow == CLOCK) {
@@ -243,7 +239,6 @@ int mainloop(struct powerdebug_options *options) bool findparent_ncurses = false; bool refreshwin = false; bool enter_hit = false; - bool cont = false; char clkname_str[64];
strcpy(clkname_str, ""); @@ -260,20 +255,11 @@ int mainloop(struct powerdebug_options *options) sensor_display();
if (options->selectedwindow == CLOCK) { + if (enter_hit) + clock_toggle_expanded();
- if (!cont) { - - if (!findparent_ncurses) { - - if (enter_hit) - clock_toggle_expanded(); - - clock_display(); - enter_hit = false; - } else - find_parents_for_clock(clkname_str, - enter_hit); - } else cont = false; + clock_display(); + enter_hit = false; }
FD_ZERO(&readfds); @@ -293,7 +279,7 @@ int mainloop(struct powerdebug_options *options) }
if (keystroke_callback(&enter_hit, &findparent_ncurses, - clkname_str, &refreshwin, &cont, options)) + clkname_str, &refreshwin, options)) break;
}
Remove the old "findparent" code.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 60 +++++---------------------------------------------------- 1 files changed, 6 insertions(+), 54 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index f73aafe..5109c90 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -153,13 +153,10 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options) return 0; }
-int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, - char *clkname_str, bool *refreshwin, - struct powerdebug_options *options) +int keystroke_callback(bool *enter_hit, struct powerdebug_options *options) { char keychar; int keystroke = getch(); - int oldselectedwin = options->selectedwindow;
if (keystroke == EOF) exit(0); @@ -176,68 +173,24 @@ int keystroke_callback(bool *enter_hit, bool *findparent_ncurses, if (keystroke == KEY_UP) display_prev_line();
- if (options->selectedwindow == CLOCK) { - -#if 0 - /* TODO : fix with a new panel applicable for all subsystems */ - if (keystroke == '/') - *findparent_ncurses = true; -#endif - - if ((keystroke == '\e' || oldselectedwin != - options->selectedwindow) && *findparent_ncurses) { - *findparent_ncurses = false; - clkname_str[0] = '\0'; - } - - if (*findparent_ncurses && keystroke != '\r') { - int len = strlen(clkname_str); - char str[2]; - - if (keystroke == KEY_BACKSPACE) { - if (len > 0) - len--; - - clkname_str[len] = '\0'; - } else { - if (strlen(clkname_str) || - keystroke != '/') { - str[0] = keystroke; - str[1] = '\0'; - if (len < 63) - strcat(clkname_str, - str); - } - } - } - } - keychar = toupper(keystroke); -//#define DEBUG -#ifdef DEBUG - fini_curses(); - printf("key entered %d:%c\n", keystroke, keychar); - exit(1); -#endif
if (keystroke == '\r') *enter_hit = true;
- if (keychar == 'Q' && !*findparent_ncurses) + if (keychar == 'Q') return 1; + if (keychar == 'R') { - *refreshwin = true; + /* TODO refresh window */ options->ticktime = 3; - } else - *refreshwin = false; + }
return 0; }
int mainloop(struct powerdebug_options *options) { - bool findparent_ncurses = false; - bool refreshwin = false; bool enter_hit = false; char clkname_str[64];
@@ -278,8 +231,7 @@ int mainloop(struct powerdebug_options *options) break; }
- if (keystroke_callback(&enter_hit, &findparent_ncurses, - clkname_str, &refreshwin, options)) + if (keystroke_callback(&enter_hit, options)) break;
}
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index 5109c90..5ff675d 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -192,9 +192,6 @@ int keystroke_callback(bool *enter_hit, struct powerdebug_options *options) int mainloop(struct powerdebug_options *options) { bool enter_hit = false; - char clkname_str[64]; - - strcpy(clkname_str, "");
while (1) { int key = 0;
Let's create some ops to be registered by the pm blocks to the display.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 53 +++++++++++++++++++++++++++++++---------------------- display.c | 11 +++++++++++ display.h | 7 ++++++- regulator.c | 8 ++++++++ sensor.c | 8 ++++++++ 5 files changed, 64 insertions(+), 23 deletions(-)
diff --git a/clocks.c b/clocks.c index db219d8..86ae948 100644 --- a/clocks.c +++ b/clocks.c @@ -26,6 +26,7 @@ #include <sys/stat.h>
#include "powerdebug.h" +#include "display.h" #include "clocks.h" #include "tree.h" #include "utils.h" @@ -306,28 +307,6 @@ int clock_toggle_expanded(void) }
/* - * Initialize the clock framework - */ -int clock_init(void) -{ - char clk_dir_path[PATH_MAX]; - - if (locate_debugfs(clk_dir_path)) - return -1; - - sprintf(clk_dir_path, "%s/clock", clk_dir_path); - - if (access(clk_dir_path, F_OK)) - return -1; - - clock_tree = tree_load(clk_dir_path, NULL); - if (!clock_tree) - return -1; - - return fill_clock_tree(); -} - -/* * Read the clock information and fill the tree with the information * found in the files. Then print the result to the text based interface * Return 0 on success, < 0 otherwise @@ -366,3 +345,33 @@ int clock_dump(char *clk)
return ret; } + +static struct display_ops clock_ops = { + .display = clock_display, + .select = clock_toggle_expanded, +}; + +/* + * Initialize the clock framework + */ +int clock_init(void) +{ + char clk_dir_path[PATH_MAX]; + + if (display_register(CLOCK, &clock_ops)) + return -1; + + if (locate_debugfs(clk_dir_path)) + return -1; + + sprintf(clk_dir_path, "%s/clock", clk_dir_path); + + if (access(clk_dir_path, F_OK)) + return -1; + + clock_tree = tree_load(clk_dir_path, NULL); + if (!clock_tree) + return -1; + + return fill_clock_tree(); +} diff --git a/display.c b/display.c index 384714b..d55d748 100644 --- a/display.c +++ b/display.c @@ -49,6 +49,7 @@ struct rowdata { struct windata { WINDOW *win; WINDOW *pad; + struct display_ops *ops; struct rowdata *rowdata; char *name; int nrdata; @@ -207,6 +208,16 @@ void print_sensor_header(void) show_header_footer(SENSOR); }
+int display_register(int win, struct display_ops *ops) +{ + if (win < 0 || win >= TOTAL_FEATURE_WINS) + return -1; + + windata[win].ops = ops; + + return 0; +} + int display_next_panel(void) { current_win++; diff --git a/display.h b/display.h index 9c0e38a..1222b44 100644 --- a/display.h +++ b/display.h @@ -13,8 +13,13 @@ * - initial API and implementation *******************************************************************************/
-extern int display_init(int wdefault); +struct display_ops { + int (*display)(void); + int (*select)(void); +};
+extern int display_init(int wdefault); +extern int display_register(int win, struct display_ops *ops); extern int display_next_panel(void); extern int display_prev_panel(void); extern int display_next_line(void); diff --git a/regulator.c b/regulator.c index d4b41e1..4b9d36e 100644 --- a/regulator.c +++ b/regulator.c @@ -26,6 +26,7 @@ #include <dirent.h> #include <string.h> #include <stdlib.h> +#include "display.h" #include "powerdebug.h" #include "tree.h" #include "utils.h" @@ -209,8 +210,15 @@ static int fill_regulator_tree(void) return tree_for_each(reg_tree, fill_regulator_cb, NULL); }
+static struct display_ops regulator_ops = { + .display = regulator_display, +}; + int regulator_init(void) { + if (display_register(REGULATOR, ®ulator_ops)) + return -1; + reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb); if (!reg_tree) return -1; diff --git a/sensor.c b/sensor.c index 9c97e72..db58137 100644 --- a/sensor.c +++ b/sensor.c @@ -23,6 +23,7 @@ #include <stdlib.h>
#include "powerdebug.h" +#include "display.h" #include "sensor.h" #include "tree.h" #include "utils.h" @@ -249,8 +250,15 @@ int sensor_display(void) return ret; }
+static struct display_ops sensor_ops = { + .display = sensor_display, +}; + int sensor_init(void) { + if (display_register(SENSOR, &sensor_ops)) + return -1; + sensor_tree = tree_load(SYSFS_SENSOR, sensor_filter_cb); if (!sensor_tree) return -1;
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 36 ++++++++++++++++++++++++++---------- display.h | 15 +++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/display.c b/display.c index d55d748..92fc02a 100644 --- a/display.c +++ b/display.c @@ -218,6 +218,22 @@ int display_register(int win, struct display_ops *ops) return 0; }
+int display_refresh(void) +{ + if (windata[current_win].ops && windata[current_win].ops->display) + return windata[current_win].ops->display(); + + return 0; +} + +int display_select(void) +{ + if (windata[current_win].ops && windata[current_win].ops->select) + return windata[current_win].ops->select(); + + return 0; +} + int display_next_panel(void) { current_win++; @@ -241,8 +257,8 @@ int display_refresh_pad(int win) 0, 2, 0, maxy - 2, maxx); }
-static int inline display_un_select(int win, int line, - bool highlight, bool bold) +static int inline display_show_un_selection(int win, int line, + bool highlight, bool bold) { if (mvwchgat(windata[win].pad, line, 0, -1, highlight ? WA_STANDOUT : @@ -252,14 +268,14 @@ static int inline display_un_select(int win, int line, return display_refresh_pad(win); }
-int display_select(int win, int line) +int display_show_selection(int win, int line) { - return display_un_select(win, line, true, false); + return display_show_un_selection(win, line, true, false); }
-int display_unselect(int win, int line, bool bold) +int display_show_unselection(int win, int line, bool bold) { - return display_un_select(win, line, false, bold); + return display_show_un_selection(win, line, false, bold); }
void *display_get_row_data(int win) @@ -326,13 +342,13 @@ int display_next_line(void) if (cursor >= nrdata) return cursor;
- display_unselect(current_win, cursor, rowdata[cursor].attr); + display_show_unselection(current_win, cursor, rowdata[cursor].attr); if (cursor < nrdata - 1) { if (cursor >= (maxy - 4 + scrolling)) scrolling++; cursor++; } - display_select(current_win, cursor); + display_show_selection(current_win, cursor);
windata[current_win].scrolling = scrolling; windata[current_win].cursor = cursor; @@ -350,13 +366,13 @@ int display_prev_line(void) if (cursor >= nrdata) return cursor;
- display_unselect(current_win, cursor, rowdata[cursor].attr); + display_show_unselection(current_win, cursor, rowdata[cursor].attr); if (cursor > 0) { if (cursor <= scrolling) scrolling--; cursor--; } - display_select(current_win, cursor); + display_show_selection(current_win, cursor);
windata[current_win].scrolling = scrolling; windata[current_win].cursor = cursor; diff --git a/display.h b/display.h index 1222b44..7e6b199 100644 --- a/display.h +++ b/display.h @@ -13,11 +13,20 @@ * - initial API and implementation *******************************************************************************/
+#define TOTAL_FEATURE_WINS 3 /* Regulator, Clock and Sensor (for now) */ + struct display_ops { int (*display)(void); int (*select)(void); };
+extern int display_print_line(int window, int line, char *str, + int bold, void *data); + +extern int display_refresh_pad(int window); +extern int display_reset_cursor(int window); +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_next_panel(void); @@ -25,3 +34,9 @@ extern int display_prev_panel(void); extern int display_next_line(void); extern int display_prev_line(void); extern int display_refresh(void); +extern int display_select(void); + +/* FIXME */ +extern void print_sensor_header(void); +extern void print_clock_header(void); +extern void print_regulator_header(void);
We don't have anymore a specific case for each pm block as we use some ops now.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 12 ++++-------- 1 files changed, 4 insertions(+), 8 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index 5ff675d..28b23b4 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -198,20 +198,16 @@ int mainloop(struct powerdebug_options *options) struct timeval tval; fd_set readfds;
- if (options->selectedwindow == REGULATOR) - regulator_display(); - - if (options->selectedwindow == SENSOR) - sensor_display();
if (options->selectedwindow == CLOCK) { if (enter_hit) - clock_toggle_expanded(); - - clock_display(); + display_select(); enter_hit = false; }
+ display_refresh(); + + FD_ZERO(&readfds); FD_SET(0, &readfds); tval.tv_sec = options->ticktime;
Remove unused parameter for the keystroke callback.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 28 ++++++++++------------------ 1 files changed, 10 insertions(+), 18 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index 28b23b4..fccc08b 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -153,7 +153,7 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options) return 0; }
-int keystroke_callback(bool *enter_hit, struct powerdebug_options *options) +int keystroke_callback(struct powerdebug_options *options) { char keychar; int keystroke = getch(); @@ -162,10 +162,10 @@ int keystroke_callback(bool *enter_hit, struct powerdebug_options *options) exit(0);
if (keystroke == KEY_RIGHT || keystroke == '\t') - options->selectedwindow = display_next_panel(); + display_next_panel();
if (keystroke == KEY_LEFT || keystroke == KEY_BTAB) - options->selectedwindow = display_prev_panel(); + display_prev_panel();
if (keystroke == KEY_DOWN) display_next_line(); @@ -176,13 +176,13 @@ int keystroke_callback(bool *enter_hit, struct powerdebug_options *options) keychar = toupper(keystroke);
if (keystroke == '\r') - *enter_hit = true; + display_select();
if (keychar == 'Q') return 1;
if (keychar == 'R') { - /* TODO refresh window */ + display_refresh(); options->ticktime = 3; }
@@ -191,23 +191,11 @@ int keystroke_callback(bool *enter_hit, struct powerdebug_options *options)
int mainloop(struct powerdebug_options *options) { - bool enter_hit = false; - while (1) { int key = 0; struct timeval tval; fd_set readfds;
- - if (options->selectedwindow == CLOCK) { - if (enter_hit) - display_select(); - enter_hit = false; - } - - display_refresh(); - - FD_ZERO(&readfds); FD_SET(0, &readfds); tval.tv_sec = options->ticktime; @@ -224,9 +212,10 @@ int mainloop(struct powerdebug_options *options) break; }
- if (keystroke_callback(&enter_hit, options)) + if (keystroke_callback(options)) break;
+ display_refresh(); }
return 0; @@ -253,6 +242,9 @@ static int powerdebug_display(struct powerdebug_options *options) return -1; }
+ if (display_refresh()) + return -1; + if (mainloop(options)) return -1;
We can use a switch here to check the key which was stroke.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- powerdebug.c | 43 +++++++++++++++++++++++++------------------ 1 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/powerdebug.c b/powerdebug.c index fccc08b..0b25860 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -155,35 +155,42 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options)
int keystroke_callback(struct powerdebug_options *options) { - char keychar; int keystroke = getch();
- if (keystroke == EOF) - exit(0); + switch (keystroke) {
- if (keystroke == KEY_RIGHT || keystroke == '\t') + case KEY_RIGHT: + case '\t': display_next_panel(); + break;
- if (keystroke == KEY_LEFT || keystroke == KEY_BTAB) + case KEY_LEFT: + case KEY_BTAB: display_prev_panel(); + break;
- if (keystroke == KEY_DOWN) + case KEY_DOWN: display_next_line(); + break;
- if (keystroke == KEY_UP) + case KEY_UP: display_prev_line(); + break;
- keychar = toupper(keystroke); - - if (keystroke == '\r') + case '\r': display_select(); + break;
- if (keychar == 'Q') + case EOF: + case 'q': + case 'Q': return 1;
- if (keychar == 'R') { + case 'r': + case 'R': display_refresh(); options->ticktime = 3; + break; }
return 0; @@ -192,21 +199,23 @@ int keystroke_callback(struct powerdebug_options *options) int mainloop(struct powerdebug_options *options) { while (1) { - int key = 0; + 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: - key = select(1, &readfds, NULL, NULL, &tval); - if (!key) + ret = select(1, &readfds, NULL, NULL, &tval); + if (!ret) continue;
- if (key < 0) { + if (ret < 0) { if (errno == EINTR) goto again; break; @@ -214,8 +223,6 @@ int mainloop(struct powerdebug_options *options)
if (keystroke_callback(options)) break; - - display_refresh(); }
return 0;
Make some cleanup around the headers.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- clocks.c | 5 +++++ display.c | 4 ++++ powerdebug.c | 6 +++++- powerdebug.h | 24 +----------------------- regulator.c | 2 ++ regulator.h | 3 +-- 6 files changed, 18 insertions(+), 26 deletions(-)
diff --git a/clocks.c b/clocks.c index 86ae948..684bf88 100644 --- a/clocks.c +++ b/clocks.c @@ -22,6 +22,11 @@ #include <stdio.h> #undef _GNU_SOURCE #endif +#include <string.h> +#include <stdbool.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/param.h> #include <mntent.h> #include <sys/stat.h>
diff --git a/display.c b/display.c index 92fc02a..3c79737 100644 --- a/display.c +++ b/display.c @@ -13,6 +13,10 @@ * - initial API and implementation *******************************************************************************/
+#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <ncurses.h> #include "powerdebug.h" #include "regulator.h" #include "display.h" diff --git a/powerdebug.c b/powerdebug.c index 0b25860..5d834c7 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -15,7 +15,11 @@
#include <getopt.h> #include <stdbool.h> -#include <math.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <ncurses.h> #include "regulator.h" #include "display.h" #include "clocks.h" diff --git a/powerdebug.h b/powerdebug.h index df39a8a..fb066ce 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -13,32 +13,10 @@ * - initial API and implementation *******************************************************************************/
-#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <dirent.h> -#include <getopt.h> -#include <errno.h> -#include <ncurses.h> - #define VERSION "0.4.1"
-#define TOTAL_FEATURE_WINS 3 /* Regulator, Clock and Sensor (for now) */ -enum {CLOCK, REGULATOR, SENSOR}; -enum {CLOCK_SELECTED = 1, REFRESH_WINDOW}; +enum { CLOCK, REGULATOR, SENSOR };
extern void find_parents_for_clock(char *clkname, int complete); -extern int display_print_line(int window, int line, char *str, - int bold, void *data); - -extern int display_refresh_pad(int window); -extern int display_reset_cursor(int window); -extern void *display_get_row_data(int window);
-extern int clock_toggle_expanded(void); -extern int regulator_display(void); -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 4b9d36e..696ed10 100644 --- a/regulator.c +++ b/regulator.c @@ -18,6 +18,8 @@ #include "regulator.h"
#define SYSFS_REGULATOR "/sys/class/regulator" +#define VALUE_MAX 16 + #define _GNU_SOURCE #include <stdio.h> #undef _GNU_SOURCE diff --git a/regulator.h b/regulator.h index 5f8c473..1b59a57 100644 --- a/regulator.h +++ b/regulator.h @@ -13,7 +13,6 @@ * - initial API and implementation *******************************************************************************/
-#define VALUE_MAX 16 - +extern int regulator_display(void); extern int regulator_init(void); extern int regulator_dump(void);
The keystroke callback could be moved to the display code.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- display.h | 8 +------- powerdebug.c | 45 +-------------------------------------------- 3 files changed, 49 insertions(+), 52 deletions(-)
diff --git a/display.c b/display.c index 3c79737..5fad059 100644 --- a/display.c +++ b/display.c @@ -21,6 +21,8 @@ #include "regulator.h" #include "display.h"
+#define TOTAL_FEATURE_WINS 3 /* Regulator, Clock and Sensor (for now) */ + #define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } while (0)
enum { PT_COLOR_DEFAULT = 1, @@ -61,7 +63,7 @@ struct windata { int cursor; };
-struct windata windata[TOTAL_FEATURE_WINS] = { +struct windata windata[] = { { .name = "Clocks" }, { .name = "Regulators" }, { .name = "Sensors" }, @@ -383,3 +385,47 @@ int display_prev_line(void)
return cursor; } + +int display_keystroke(void *data) +{ + int *tick = data; + int keystroke = getch(); + + switch (keystroke) { + + case KEY_RIGHT: + case '\t': + display_next_panel(); + break; + + case KEY_LEFT: + case KEY_BTAB: + display_prev_panel(); + break; + + case KEY_DOWN: + display_next_line(); + break; + + case KEY_UP: + display_prev_line(); + break; + + case '\r': + display_select(); + break; + + case EOF: + case 'q': + case 'Q': + return 1; + + case 'r': + case 'R': + display_refresh(); + *tick = 3; + break; + } + + return 0; +} diff --git a/display.h b/display.h index 7e6b199..ebd501a 100644 --- a/display.h +++ b/display.h @@ -13,8 +13,6 @@ * - initial API and implementation *******************************************************************************/
-#define TOTAL_FEATURE_WINS 3 /* Regulator, Clock and Sensor (for now) */ - struct display_ops { int (*display)(void); int (*select)(void); @@ -29,12 +27,8 @@ 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_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); -extern int display_select(void); +extern int display_keystroke(void *data);
/* FIXME */ extern void print_sensor_header(void); diff --git a/powerdebug.c b/powerdebug.c index 5d834c7..065fa31 100644 --- a/powerdebug.c +++ b/powerdebug.c @@ -157,49 +157,6 @@ int getoptions(int argc, char *argv[], struct powerdebug_options *options) return 0; }
-int keystroke_callback(struct powerdebug_options *options) -{ - int keystroke = getch(); - - switch (keystroke) { - - case KEY_RIGHT: - case '\t': - display_next_panel(); - break; - - case KEY_LEFT: - case KEY_BTAB: - display_prev_panel(); - break; - - case KEY_DOWN: - display_next_line(); - break; - - case KEY_UP: - display_prev_line(); - break; - - case '\r': - display_select(); - break; - - case EOF: - case 'q': - case 'Q': - return 1; - - case 'r': - case 'R': - display_refresh(); - options->ticktime = 3; - break; - } - - return 0; -} - int mainloop(struct powerdebug_options *options) { while (1) { @@ -225,7 +182,7 @@ int mainloop(struct powerdebug_options *options) break; }
- if (keystroke_callback(options)) + if (display_keystroke(&options->ticktime)) break; }
The less we have define for pm blocks the easier is to add more pm blocks.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 24 +++++++++++++++--------- display.h | 2 ++ powerdebug.h | 4 ---- 3 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/display.c b/display.c index 5fad059..5fd14c7 100644 --- a/display.c +++ b/display.c @@ -21,8 +21,6 @@ #include "regulator.h" #include "display.h"
-#define TOTAL_FEATURE_WINS 3 /* Regulator, Clock and Sensor (for now) */ - #define print(w, x, y, fmt, args...) do { mvwprintw(w, y, x, fmt, ##args); } while (0)
enum { PT_COLOR_DEFAULT = 1, @@ -63,10 +61,11 @@ struct windata { int cursor; };
+/* Warning this is linked with the enum { CLOCK, REGULATOR, ... } */ struct windata windata[] = { - { .name = "Clocks" }, - { .name = "Regulators" }, - { .name = "Sensors" }, + [CLOCK] = { .name = "Clocks" }, + [REGULATOR] = { .name = "Regulators" }, + [SENSOR] = { .name = "Sensors" }, };
static void display_fini(void) @@ -78,6 +77,7 @@ static int show_header_footer(int win) { int i; int curr_pointer = 0; + size_t array_size = sizeof(windata) / sizeof(windata[0]);
wattrset(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR)); wbkgd(header_win, COLOR_PAIR(PT_COLOR_HEADER_BAR)); @@ -86,7 +86,7 @@ static int show_header_footer(int win) print(header_win, curr_pointer, 0, "PowerDebug %s", VERSION); curr_pointer += 20;
- for (i = 0; i < TOTAL_FEATURE_WINS; i++) { + for (i = 0; i < array_size; i++) { if (win == i) wattron(header_win, A_REVERSE); else @@ -216,7 +216,9 @@ void print_sensor_header(void)
int display_register(int win, struct display_ops *ops) { - if (win < 0 || win >= TOTAL_FEATURE_WINS) + size_t array_size = sizeof(windata) / sizeof(windata[0]); + + if (win < 0 || win >= array_size) return -1;
windata[win].ops = ops; @@ -242,17 +244,21 @@ int display_select(void)
int display_next_panel(void) { + size_t array_size = sizeof(windata) / sizeof(windata[0]); + current_win++; - current_win %= TOTAL_FEATURE_WINS; + current_win %= array_size;
return current_win; }
int display_prev_panel(void) { + size_t array_size = sizeof(windata) / sizeof(windata[0]); + current_win--; if (current_win < 0) - current_win = TOTAL_FEATURE_WINS - 1; + current_win = array_size - 1;
return current_win; } diff --git a/display.h b/display.h index ebd501a..f354195 100644 --- a/display.h +++ b/display.h @@ -13,6 +13,8 @@ * - initial API and implementation *******************************************************************************/
+enum { CLOCK, REGULATOR, SENSOR }; + struct display_ops { int (*display)(void); int (*select)(void); diff --git a/powerdebug.h b/powerdebug.h index fb066ce..73e3581 100644 --- a/powerdebug.h +++ b/powerdebug.h @@ -15,8 +15,4 @@
#define VERSION "0.4.1"
-enum { CLOCK, REGULATOR, SENSOR }; - -extern void find_parents_for_clock(char *clkname, int complete); -
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 16 +--------------- 1 files changed, 1 insertions(+), 15 deletions(-)
diff --git a/display.c b/display.c index 5fd14c7..1ba71e5 100644 --- a/display.c +++ b/display.c @@ -269,27 +269,15 @@ int display_refresh_pad(int win) 0, 2, 0, maxy - 2, maxx); }
-static int inline display_show_un_selection(int win, int line, - bool highlight, bool bold) +int display_show_unselection(int win, int line, bool bold) { if (mvwchgat(windata[win].pad, line, 0, -1, - highlight ? WA_STANDOUT : bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) return -1;
return display_refresh_pad(win); }
-int display_show_selection(int win, int line) -{ - return display_show_un_selection(win, line, true, false); -} - -int display_show_unselection(int win, int line, bool bold) -{ - return display_show_un_selection(win, line, false, bold); -} - void *display_get_row_data(int win) { return windata[win].rowdata[windata[win].cursor].data; @@ -360,7 +348,6 @@ int display_next_line(void) scrolling++; cursor++; } - display_show_selection(current_win, cursor);
windata[current_win].scrolling = scrolling; windata[current_win].cursor = cursor; @@ -384,7 +371,6 @@ int display_prev_line(void) scrolling--; cursor--; } - display_show_selection(current_win, cursor);
windata[current_win].scrolling = scrolling; windata[current_win].cursor = cursor;
Make some functions static as they are no longer out of the scope of the display code.
Signed-off-by: Daniel Lezcano daniel.lezcano@linaro.org --- display.c | 124 ++++++++++++++++++++++++++++++------------------------------ 1 files changed, 62 insertions(+), 62 deletions(-)
diff --git a/display.c b/display.c index 1ba71e5..257e540 100644 --- a/display.c +++ b/display.c @@ -234,7 +234,27 @@ int display_refresh(void) return 0; }
-int display_select(void) +int display_refresh_pad(int win) +{ + return prefresh(windata[win].pad, windata[win].scrolling, + 0, 2, 0, maxy - 2, maxx); +} + +static int display_show_unselection(int win, int line, bool bold) +{ + if (mvwchgat(windata[win].pad, line, 0, -1, + bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) + return -1; + + return display_refresh_pad(win); +} + +void *display_get_row_data(int win) +{ + return windata[win].rowdata[windata[win].cursor].data; +} + +static int display_select(void) { if (windata[current_win].ops && windata[current_win].ops->select) return windata[current_win].ops->select(); @@ -242,7 +262,7 @@ int display_select(void) return 0; }
-int display_next_panel(void) +static int display_next_panel(void) { size_t array_size = sizeof(windata) / sizeof(windata[0]);
@@ -252,7 +272,7 @@ int display_next_panel(void) return current_win; }
-int display_prev_panel(void) +static int display_prev_panel(void) { size_t array_size = sizeof(windata) / sizeof(windata[0]);
@@ -263,27 +283,53 @@ int display_prev_panel(void) return current_win; }
-int display_refresh_pad(int win) +static int display_next_line(void) { - return prefresh(windata[win].pad, windata[win].scrolling, - 0, 2, 0, maxy - 2, maxx); -} + 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;
-int display_show_unselection(int win, int line, bool bold) -{ - if (mvwchgat(windata[win].pad, line, 0, -1, - bold ? WA_BOLD: WA_NORMAL, 0, NULL) < 0) - return -1; + if (cursor >= nrdata) + return cursor;
- return display_refresh_pad(win); + display_show_unselection(current_win, cursor, rowdata[cursor].attr); + if (cursor < nrdata - 1) { + if (cursor >= (maxy - 4 + scrolling)) + scrolling++; + cursor++; + } + + windata[current_win].scrolling = scrolling; + windata[current_win].cursor = cursor; + + return cursor; }
-void *display_get_row_data(int win) +static int display_prev_line(void) { - return windata[win].rowdata[windata[win].cursor].data; + 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_show_unselection(current_win, cursor, rowdata[cursor].attr); + if (cursor > 0) { + if (cursor <= scrolling) + scrolling--; + cursor--; + } + + windata[current_win].scrolling = scrolling; + windata[current_win].cursor = cursor; + + return cursor; }
-int display_set_row_data(int win, int line, void *data, int attr) +static int display_set_row_data(int win, int line, void *data, int attr) { struct rowdata *rowdata = windata[win].rowdata;
@@ -332,52 +378,6 @@ int display_print_line(int win, int line, char *str, int bold, void *data) return 0; }
-int display_next_line(void) -{ - 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_show_unselection(current_win, cursor, rowdata[cursor].attr); - if (cursor < nrdata - 1) { - if (cursor >= (maxy - 4 + scrolling)) - scrolling++; - cursor++; - } - - windata[current_win].scrolling = scrolling; - windata[current_win].cursor = cursor; - - return cursor; -} - -int display_prev_line(void) -{ - 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_show_unselection(current_win, cursor, rowdata[cursor].attr); - if (cursor > 0) { - if (cursor <= scrolling) - scrolling--; - cursor--; - } - - windata[current_win].scrolling = scrolling; - windata[current_win].cursor = cursor; - - return cursor; -} - int display_keystroke(void *data) { int *tick = data;
all patches applied.