From 0740e919115d82dd6e1f8d893ce17e8ff60df421 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Wed, 7 Oct 2009 22:44:17 +0200 Subject: [PATCH] diskio: convert to generic object payload --- src/conky.c | 20 +++++--------- src/core.c | 30 +++++---------------- src/diskio.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++ src/diskio.h | 7 +++++ src/text_object.h | 1 - 5 files changed, 85 insertions(+), 39 deletions(-) diff --git a/src/conky.c b/src/conky.c index fdc0a060..a1bee786 100644 --- a/src/conky.c +++ b/src/conky.c @@ -1145,32 +1145,24 @@ void generate_text_internal(char *p, int p_max_size, need_to_load_fonts = 1; } #endif /* X11 */ - /* TODO: move this correction from kB to kB/s elsewhere - * (or get rid of it??) */ OBJ(diskio) { - human_readable((obj->data.diskio->current / update_interval) * 1024LL, - p, p_max_size); + print_diskio(obj, 0, p, p_max_size); } OBJ(diskio_write) { - human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, - p, p_max_size); + print_diskio(obj, 1, p, p_max_size); } OBJ(diskio_read) { - human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, - p, p_max_size); + print_diskio(obj, -1, p, p_max_size); } #ifdef X11 OBJ(diskiograph) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, - obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b); + print_diskiograph(obj, 0, p); } OBJ(diskiograph_read) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, - obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b); + print_diskiograph(obj, -1, p); } OBJ(diskiograph_write) { - new_graph(p, obj->a, obj->b, obj->c, obj->d, - obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b); + print_diskiograph(obj, 1, p); } #endif /* X11 */ OBJ(downspeed) { diff --git a/src/core.c b/src/core.c index bb9d3132..6efab874 100644 --- a/src/core.c +++ b/src/core.c @@ -396,36 +396,18 @@ struct text_object *construct_text_object(const char *s, const char *arg, long } #endif /* X11 */ END OBJ(diskio, &update_diskio) - obj->data.diskio = prepare_diskio_stat(dev_name(arg)); + parse_diskio_arg(obj, arg); END OBJ(diskio_read, &update_diskio) - obj->data.diskio = prepare_diskio_stat(dev_name(arg)); + parse_diskio_arg(obj, arg); END OBJ(diskio_write, &update_diskio) - obj->data.diskio = prepare_diskio_stat(dev_name(arg)); + parse_diskio_arg(obj, arg); #ifdef X11 END OBJ(diskiograph, &update_diskio) - 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); - - obj->data.diskio = prepare_diskio_stat(dev_name(buf)); - if (buf) free(buf); + parse_diskiograph_arg(obj, arg); END OBJ(diskiograph_read, &update_diskio) - 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); - - obj->data.diskio = prepare_diskio_stat(dev_name(buf)); - if (buf) free(buf); + parse_diskiograph_arg(obj, arg); END OBJ(diskiograph_write, &update_diskio) - 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); - - obj->data.diskio = prepare_diskio_stat(dev_name(buf)); - if (buf) free(buf); + parse_diskiograph_arg(obj, arg); #endif /* X11 */ END OBJ(color, 0) #ifdef X11 diff --git a/src/diskio.c b/src/diskio.c index 1e4a7a76..147f5f70 100644 --- a/src/diskio.c +++ b/src/diskio.c @@ -30,9 +30,12 @@ #include "config.h" #include "conky.h" /* text_buffer_size */ +#include "core.h" #include "logging.h" #include "diskio.h" #include "common.h" +#include "specials.h" +#include "text_object.h" #include #include #include @@ -106,6 +109,69 @@ struct diskio_stat *prepare_diskio_stat(const char *s) return cur; } +void parse_diskio_arg(struct text_object *obj, const char *arg) +{ + obj->data.opaque = prepare_diskio_stat(arg); +} + +/* dir indicates the direction: + * -1: read + * 0: read + write + * 1: write + */ +void print_diskio(struct text_object *obj, int dir, char *p, int p_max_size) +{ + struct diskio_stat *diskio = obj->data.opaque; + double val; + + if (!diskio) + return; + + if (dir < 0) + val = diskio->current_read; + if (dir == 0) + val = diskio->current; + else + val = diskio->current_write; + + /* TODO: move this correction from kB to kB/s elsewhere + * (or get rid of it??) */ + human_readable((val / update_interval) * 1024LL, p, p_max_size); +} + +#ifdef X11 +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); + + obj->data.opaque = prepare_diskio_stat(dev_name(buf)); + if (buf) + free(buf); +} + +void print_diskiograph(struct text_object *obj, int dir, char *p) +{ + struct diskio_stat *diskio = obj->data.opaque; + double val; + + if (!diskio) + return; + + if (dir < 0) + val = diskio->current_read; + else if (dir == 0) + val = diskio->current; + 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); +} +#endif /* X11 */ + void update_diskio_values(struct diskio_stat *ds, unsigned int reads, unsigned int writes) { diff --git a/src/diskio.h b/src/diskio.h index 064b31b4..10c44b7e 100644 --- a/src/diskio.h +++ b/src/diskio.h @@ -51,4 +51,11 @@ void update_diskio(void); void clear_diskio_stats(void); void update_diskio_values(struct diskio_stat *, unsigned int, unsigned int); +void parse_diskio_arg(struct text_object *, const char *); +void print_diskio(struct text_object *, int, char *, int); +#ifdef X11 +void parse_diskiograph_arg(struct text_object *, const char *); +void print_diskiograph(struct text_object *, int, char *); +#endif /* X11 */ + #endif /* DISKIO_H_ */ diff --git a/src/text_object.h b/src/text_object.h index 3e82b0f2..1f408285 100644 --- a/src/text_object.h +++ b/src/text_object.h @@ -444,7 +444,6 @@ struct text_object { long l; /* some other integer */ unsigned int sensor; struct net_stat *net; - struct diskio_stat *diskio; unsigned char loadavg[3]; unsigned int cpu_index; struct mail_s *mail;