diskio: convert to generic object payload
authorPhil Sutter <phil@nwl.cc>
Wed, 7 Oct 2009 20:44:17 +0000 (22:44 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 22:23:22 +0000 (23:23 +0100)
src/conky.c
src/core.c
src/diskio.c
src/diskio.h
src/text_object.h

index fdc0a06..a1bee78 100644 (file)
@@ -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) {
index bb9d313..6efab87 100644 (file)
@@ -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
index 1e4a7a7..147f5f7 100644 (file)
 
 #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 <stdlib.h>
 #include <limits.h>
 #include <sys/stat.h>
@@ -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)
 {
index 064b31b..10c44b7 100644 (file)
@@ -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_ */
index 3e82b0f..1f40828 100644 (file)
@@ -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;