From bbec0db4986381eaef2e6ba41d97b80a5dcb2aa6 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 29 Oct 2009 03:08:08 +0100 Subject: [PATCH] specials: convert graph objects to new style --- src/common.c | 7 +--- src/conky.c | 14 ++----- src/core.c | 22 +++------- src/diskio.c | 7 +--- src/exec.c | 52 ++++++++++-------------- src/exec.h | 1 + src/net_stat.c | 10 ++--- src/specials.c | 108 +++++++++++++++++++++++++++---------------------- src/specials.h | 6 +-- 9 files changed, 101 insertions(+), 126 deletions(-) diff --git a/src/common.c b/src/common.c index e87dda65..3ad8f458 100644 --- a/src/common.c +++ b/src/common.c @@ -448,16 +448,13 @@ void scan_loadgraph_arg(struct text_object *obj, const char *arg) { char *buf = 0; - SIZE_DEFAULTS(graph); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); if (buf) free(buf); } void print_loadgraph(struct text_object *obj, char *p) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, info.loadavg[0], - obj->e, 1, obj->char_a, obj->char_b); + new_graph(obj, p, info.loadavg[0]); } #endif /* X11 */ diff --git a/src/conky.c b/src/conky.c index aef2c537..1e1c8063 100644 --- a/src/conky.c +++ b/src/conky.c @@ -935,9 +935,7 @@ void generate_text_internal(char *p, int p_max_size, } #ifdef X11 OBJ(cpugraph) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, - round_to_int(cur->cpu_usage[obj->data.i] * 100), - 100, 1, obj->char_a, obj->char_b); + new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100)); } OBJ(loadgraph) { print_loadgraph(obj, p); @@ -1332,8 +1330,7 @@ void generate_text_internal(char *p, int p_max_size, OBJ(lua_graph) { double per; if (llua_getnumber(obj->data.s, &per)) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, - per, obj->e, 1, obj->char_a, obj->char_b); + new_graph(obj, p, per); } } OBJ(lua_gauge) { @@ -1483,9 +1480,7 @@ void generate_text_internal(char *p, int p_max_size, } #ifdef X11 OBJ(memgraph) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, - cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0, - 100, 1, obj->char_a, obj->char_b); + new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0); } #endif /* X11 */ /* mixer stuff */ @@ -2262,8 +2257,7 @@ void generate_text_internal(char *p, int p_max_size, OBJ(apcupsd_loadgraph) { double progress; progress = atof(cur->apcupsd.items[APCUPSD_LOAD]); - new_graph(p, obj->a, obj->b, obj->c, obj->d, - (int)progress, 100, 1, obj->char_a, obj->char_b); + new_graph(obj, p, (int)progress); } OBJ(apcupsd_loadgauge) { double progress; diff --git a/src/core.c b/src/core.c index 4a372a31..00b740b0 100644 --- a/src/core.c +++ b/src/core.c @@ -357,10 +357,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #ifdef X11 END OBJ(cpugraph, &update_cpu_usage) char *buf = 0; - SIZE_DEFAULTS(graph); SCAN_CPU(arg, obj->data.i); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); DBGP2("Adding $cpugraph for CPU %d", obj->data.i); if (buf) free(buf); END OBJ(loadgraph, &update_load_average) @@ -477,15 +475,13 @@ struct text_object *construct_text_object(const char *s, const char *arg, long SIZE_DEFAULTS(gauge); scan_exec_arg(obj, arg); END OBJ(execgraph, 0) - SIZE_DEFAULTS(graph); - scan_exec_arg(obj, arg); + scan_execgraph_arg(obj, arg); #endif /* X11 */ END OBJ_ARG(execibar, 0, "execibar needs arguments") scan_execi_arg(obj, arg); #ifdef X11 END OBJ_ARG(execigraph, 0, "execigraph needs arguments") - SIZE_DEFAULTS(graph); - scan_execi_arg(obj, arg); + scan_execgraph_arg(obj, arg); END OBJ_ARG(execigauge, 0, "execigauge needs arguments") SIZE_DEFAULTS(gauge); scan_execi_arg(obj, arg); @@ -649,9 +645,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #ifdef X11 END OBJ(memgraph, &update_meminfo) char *buf = 0; - SIZE_DEFAULTS(graph); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); if (buf) free(buf); #endif /* X11*/ @@ -951,9 +945,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #ifdef X11 END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]") char *buf = 0; - SIZE_DEFAULTS(graph); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); if (buf) { obj->data.s = buf; } else { @@ -1036,9 +1028,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #ifdef X11 END OBJ(apcupsd_loadgraph, &update_apcupsd) char* buf = 0; - SIZE_DEFAULTS(graph); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); if (buf) free(buf); END OBJ(apcupsd_loadgauge, &update_apcupsd) SIZE_DEFAULTS(gauge); diff --git a/src/diskio.c b/src/diskio.c index 147f5f70..c0421395 100644 --- a/src/diskio.c +++ b/src/diskio.c @@ -143,9 +143,7 @@ void print_diskio(struct text_object *obj, int dir, char *p, int p_max_size) void parse_diskiograph_arg(struct text_object *obj, const char *arg) { char *buf = 0; - SIZE_DEFAULTS(graph); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); obj->data.opaque = prepare_diskio_stat(dev_name(buf)); if (buf) @@ -167,8 +165,7 @@ void print_diskiograph(struct text_object *obj, int dir, char *p) else val = diskio->current_write; - new_graph(p, obj->a, obj->b, obj->c, obj->d, - val, obj->e, 1, obj->char_a, obj->char_b); + new_graph(obj, p, val); } #endif /* X11 */ diff --git a/src/exec.c b/src/exec.c index d612bd42..179e8d35 100644 --- a/src/exec.c +++ b/src/exec.c @@ -235,6 +235,23 @@ void scan_execi_arg(struct text_object *obj, const char *arg) obj->data.opaque = ed; } +void scan_execgraph_arg(struct text_object *obj, const char *arg) +{ + struct execi_data *ed; + char *buf; + + ed = malloc(sizeof(struct execi_data)); + memset(ed, 0, sizeof(struct execi_data)); + + buf = scan_graph(obj, arg); + if (!buf) { + NORM_ERR("missing command argument to execgraph object"); + return; + } + ed->cmd = buf; + obj->data.opaque = ed; +} + void print_exec(struct text_object *obj, char *p, int p_max_size) { read_exec(obj->data.s, p, p_max_size); @@ -353,31 +370,17 @@ void print_execgauge(struct text_object *obj, char *p, int p_max_size) void print_execgraph(struct text_object *obj, char *p, int p_max_size) { - char showaslog = FALSE; - char tempgrad = FALSE; double barnum; struct execi_data *ed = obj->data.opaque; - char *cmd; if (!ed) return; - cmd = ed->cmd; - - if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) { - tempgrad = TRUE; - cmd += strlen(" "TEMPGRAD); - } - if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) { - showaslog = TRUE; - cmd += strlen(" "LOGGRAPH); - } - read_exec(cmd, p, p_max_size); + read_exec(ed->cmd, p, p_max_size); barnum = get_barnum(p); if (barnum > 0) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum), - 100, 1, showaslog, tempgrad); + new_graph(obj, p, round_to_int(barnum)); } } @@ -390,21 +393,8 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size) if (time_to_update(ed)) { double barnum; - char showaslog = FALSE; - char tempgrad = FALSE; - char *cmd = ed->cmd; - if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) { - tempgrad = TRUE; - cmd += strlen(" "TEMPGRAD); - } - if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) { - showaslog = TRUE; - cmd += strlen(" "LOGGRAPH); - } - obj->char_a = showaslog; - obj->char_b = tempgrad; - read_exec(cmd, p, p_max_size); + read_exec(ed->cmd, p, p_max_size); barnum = get_barnum(p); if (barnum >= 0.0) { @@ -412,7 +402,7 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size) } ed->last_update = current_update_time; } - new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b); + new_graph(obj, p, (int) (obj->f)); } void print_execigauge(struct text_object *obj, char *p, int p_max_size) diff --git a/src/exec.h b/src/exec.h index 1a174772..193399ef 100644 --- a/src/exec.h +++ b/src/exec.h @@ -36,6 +36,7 @@ extern pid_t childpid; void scan_exec_arg(struct text_object *, const char *); void scan_pre_exec_arg(struct text_object *, const char *); void scan_execi_arg(struct text_object *, const char *); +void scan_execgraph_arg(struct text_object *, const char *); void print_exec(struct text_object *, char *, int); void print_execp(struct text_object *, char *, int); void print_execi(struct text_object *, char *, int); diff --git a/src/net_stat.c b/src/net_stat.c index 8fd3c0ae..3a143f9b 100644 --- a/src/net_stat.c +++ b/src/net_stat.c @@ -193,9 +193,7 @@ void print_addrs(struct text_object *obj, char *p, int p_max_size) void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash) { char *buf = 0; - SIZE_DEFAULTS(graph); - buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, - &obj->e, &obj->char_a, &obj->char_b); + buf = scan_graph(obj, arg); // default to DEFAULTNETDEV if (buf) { @@ -213,8 +211,7 @@ void print_downspeedgraph(struct text_object *obj, char *p) if (!ns) return; - new_graph(p, obj->a, obj->b, obj->c, obj->d, - ns->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b); + new_graph(obj, p, ns->recv_speed / 1024.0); } void print_upspeedgraph(struct text_object *obj, char *p) @@ -224,8 +221,7 @@ void print_upspeedgraph(struct text_object *obj, char *p) if (!ns) return; - new_graph(p, obj->a, obj->b, obj->c, obj->d, - ns->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b); + new_graph(obj, p, ns->trans_speed / 1024.0); } #endif /* X11 */ diff --git a/src/specials.c b/src/specials.c index 8c4b4aea..9c1bc174 100644 --- a/src/specials.c +++ b/src/specials.c @@ -59,6 +59,13 @@ struct bar { int width, height; }; +struct graph { + int width, height; + unsigned int first_colour, last_colour; + unsigned int scale, showaslog; + char tempgrad; +}; + /* * Scanning arguments to various special text objects */ @@ -120,74 +127,78 @@ char *scan_font(const char *args) return NULL; } -char *scan_graph(const char *args, int *w, int *h, - unsigned int *first_colour, unsigned int *last_colour, - unsigned int *scale, char *showaslog, char *tempgrad) +char *scan_graph(struct text_object *obj, const char *args) { + struct graph *g; char buf[1024]; memset(buf, 0, 1024); + g = malloc(sizeof(struct graph)); + memset(g, 0, sizeof(struct graph)); + obj->special_data = g; + /* zero width means all space that is available */ - *w = default_graph_width; - *h = default_graph_height; - *first_colour = 0; - *last_colour = 0; - *scale = 0; - *tempgrad = FALSE; - *showaslog = FALSE; + g->width = default_graph_width; + g->height = default_graph_height; + g->first_colour = 0; + g->last_colour = 0; + g->scale = 0; + g->tempgrad = FALSE; + g->showaslog = FALSE; if (args) { if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) { - *tempgrad = TRUE; + g->tempgrad = TRUE; } if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) { - *showaslog = TRUE; + g->showaslog = TRUE; } - if (sscanf(args, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) { + if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) { return NULL; } - *scale = 0; - if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) { + g->scale = 0; + if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) { return NULL; } - if (sscanf(args, "%1023s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) { + if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) { return strndup(buf, text_buffer_size); } - *scale = 0; - if (sscanf(args, "%1023s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) { + g->scale = 0; + if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) { return strndup(buf, text_buffer_size); } buf[0] = '\0'; - *h = 25; - *w = 0; - if (sscanf(args, "%x %x %u", first_colour, last_colour, scale) == 3) { + g->height = 25; + g->width = 0; + if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) { return NULL; } - *scale = 0; - if (sscanf(args, "%x %x", first_colour, last_colour) == 2) { + g->scale = 0; + if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) { return NULL; } - if (sscanf(args, "%1023s %x %x %u", buf, first_colour, last_colour, scale) == 4) { + if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) { return strndup(buf, text_buffer_size); } - *scale = 0; - if (sscanf(args, "%1023s %x %x", buf, first_colour, last_colour) == 3) { + g->scale = 0; + if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) { return strndup(buf, text_buffer_size); } buf[0] = '\0'; - *first_colour = 0; - *last_colour = 0; - if (sscanf(args, "%d,%d %u", h, w, scale) == 3) { + g->first_colour = 0; + g->last_colour = 0; + if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) { return NULL; } - *scale = 0; - if (sscanf(args, "%d,%d", h, w) == 2) { + g->scale = 0; + if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) { return NULL; } - if (sscanf(args, "%1023s %d,%d %u", buf, h, w, scale) < 4) { - *scale = 0; + if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) { + g->scale = 0; //TODO: check the return value and throw an error? - sscanf(args, "%1023s %d,%d", buf, h, w); + sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width); } +#undef g return strndup(buf, text_buffer_size); } @@ -280,7 +291,7 @@ static void graph_append(struct special_t *graph, double f, char showaslog) f = log10(f + 1); #endif } - + if (!graph->scaled && f > graph->graph_scale) { f = graph->graph_scale; } @@ -300,17 +311,20 @@ static void graph_append(struct special_t *graph, double f, char showaslog) } } -void new_graph(char *buf, int w, int h, unsigned int first_colour, - unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad) +void new_graph(struct text_object *obj, char *buf, double val) { struct special_t *s = 0; + struct graph *g = obj->special_data; if ((output_methods & TO_X) == 0) return; + if (!g) + return; + s = new_special(buf, GRAPH); - s->width = w; + s->width = g->width; if (s->graph == NULL) { if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) { // subtract 2 for the box @@ -322,30 +336,28 @@ void new_graph(char *buf, int w, int h, unsigned int first_colour, memset(s->graph, 0, s->graph_width * sizeof(double)); s->graph_scale = 100; } - s->height = h; - s->first_colour = adjust_colours(first_colour); - s->last_colour = adjust_colours(second_colour); - if (scale != 0) { + s->height = g->height; + s->first_colour = adjust_colours(g->first_colour); + s->last_colour = adjust_colours(g->last_colour); + if (g->scale != 0) { s->scaled = 0; - s->graph_scale = scale; + s->graph_scale = g->scale; s->show_scale = 0; } else { s->scaled = 1; s->graph_scale = 1; s->show_scale = 1; } - s->tempgrad = tempgrad; + s->tempgrad = g->tempgrad; /* if (s->width) { s->graph_width = s->width - 2; // subtract 2 for rectangle around } */ #ifdef MATH - if (showaslog) { + if (g->showaslog) { s->graph_scale = log10(s->graph_scale + 1); } #endif - if (append) { - graph_append(s, i, showaslog); - } + graph_append(s, val, g->showaslog); } void new_hr(char *buf, int a) diff --git a/src/specials.h b/src/specials.h index a09bda15..54bb358d 100644 --- a/src/specials.h +++ b/src/specials.h @@ -103,15 +103,13 @@ const char *scan_bar(struct text_object *, const char *); #ifdef X11 const char *scan_gauge(const char *, int *, int *); char *scan_font(const char *); -char *scan_graph(const char *, int *, int *, unsigned int *, - unsigned int *, unsigned int *, char *, char *); +char *scan_graph(struct text_object *, const char *); /* printing specials */ void new_gauge(char *, int, int, int); void new_bar(struct text_object *, char *, int); void new_font(char *, char *); -void new_graph(char *, int, int, unsigned int, - unsigned int, double, int, int, char, char); +void new_graph(struct text_object *, char *, double); void new_hr(char *, int); void new_stippled_hr(char *, int, int); #endif