sysfs objects: merge init and print routines
authorPhil Sutter <phil@nwl.cc>
Sun, 4 Oct 2009 16:05:47 +0000 (18:05 +0200)
committerPhil Sutter <phil@nwl.cc>
Mon, 12 Oct 2009 19:33:02 +0000 (21:33 +0200)
src/conky.c
src/core.c
src/linux.c
src/linux.h

index 5469fa8..73288dc 100644 (file)
@@ -1935,52 +1935,13 @@ static void generate_text_internal(char *p, int p_max_size,
                        }
 #ifdef __linux__
                        OBJ(i2c) {
-                               double r;
-
-                               r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
-                                       obj->data.sysfs.devtype, obj->data.sysfs.type);
-
-                               r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
-
-                               if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
-                                       temp_print(p, p_max_size, r, TEMP_CELSIUS);
-                               } else if (r >= 100.0 || r == 0) {
-                                       snprintf(p, p_max_size, "%d", (int) r);
-                               } else {
-                                       snprintf(p, p_max_size, "%.1f", r);
-                               }
+                               print_sysfs_sensor(obj, p, p_max_size);
                        }
                        OBJ(platform) {
-                               double r;
-
-                               r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
-                                       obj->data.sysfs.devtype, obj->data.sysfs.type);
-
-                               r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
-
-                               if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
-                                       temp_print(p, p_max_size, r, TEMP_CELSIUS);
-                               } else if (r >= 100.0 || r == 0) {
-                                       snprintf(p, p_max_size, "%d", (int) r);
-                               } else {
-                                       snprintf(p, p_max_size, "%.1f", r);
-                               }
+                               print_sysfs_sensor(obj, p, p_max_size);
                        }
                        OBJ(hwmon) {
-                               double r;
-
-                               r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
-                                       obj->data.sysfs.devtype, obj->data.sysfs.type);
-
-                               r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
-
-                               if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
-                                       temp_print(p, p_max_size, r, TEMP_CELSIUS);
-                               } else if (r >= 100.0 || r == 0) {
-                                       snprintf(p, p_max_size, "%d", (int) r);
-                               } else {
-                                       snprintf(p, p_max_size, "%.1f", r);
-                               }
+                               print_sysfs_sensor(obj, p, p_max_size);
                        }
 #endif /* __linux__ */
                        OBJ(alignr) {
index 31a0bea..0e37afd 100644 (file)
@@ -772,79 +772,12 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
 
 #ifdef __linux__
        END OBJ_ARG(i2c, 0, "i2c needs arguments")
-               char buf1[64], buf2[64];
-               float factor, offset;
-               int n, found = 0;
-
-#define HWMON_RESET() {\
-               buf1[0] = 0; \
-               factor = 1.0; \
-               offset = 0.0; }
-
-               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
-
-               if (!found) {
-                       NORM_ERR("i2c failed to parse arguments");
-                       obj->type = OBJ_text;
-                       return NULL;
-               }
-               DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
-               obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-               strncpy(obj->data.sysfs.type, buf2, 63);
-               obj->data.sysfs.factor = factor;
-               obj->data.sysfs.offset = offset;
-
+               parse_i2c_sensor(obj, arg);
        END OBJ_ARG(platform, 0, "platform needs arguments")
-               char buf1[64], buf2[64];
-               float factor, offset;
-               int n, found = 0;
-
-               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
-
-               if (!found) {
-                       NORM_ERR("platform failed to parse arguments");
-                       obj->type = OBJ_text;
-                       return NULL;
-               }
-               DBGP("parsed platform args: '%s' '%s' %d %f %f", buf1, buf2, n, factor, offset);
-               obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-               strncpy(obj->data.sysfs.type, buf2, 63);
-               obj->data.sysfs.factor = factor;
-               obj->data.sysfs.offset = offset;
-
+               parse_platform_sensor(obj, arg);
        END OBJ_ARG(hwmon, 0, "hwmon needs argumanets")
-               char buf1[64], buf2[64];
-               float factor, offset;
-               int n, found = 0;
-
-               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
-
-#undef HWMON_RESET
-
-               if (!found) {
-                       NORM_ERR("hwmon failed to parse arguments");
-                       obj->type = OBJ_text;
-                       return NULL;
-               }
-               DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
-               obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-               strncpy(obj->data.sysfs.type, buf2, 63);
-               obj->data.sysfs.factor = factor;
-               obj->data.sysfs.offset = offset;
-
-#endif /* !__OpenBSD__ */
+               parse_hwmon_sensor(obj, arg);
+#endif /* __linux__ */
 
        END
        /* we have four different types of top (top, top_mem, top_time and top_io). To
index 0542dda..ec7c75d 100644 (file)
@@ -34,6 +34,7 @@
 #include "common.h"
 #include "linux.h"
 #include "diskio.h"
+#include "temphelper.h"
 #include <dirent.h>
 #include <ctype.h>
 #include <errno.h>
@@ -836,7 +837,7 @@ static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
        }
 }
 
-int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
+static int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
                int *divisor, char *devtype)
 {
        char path[256];
@@ -933,7 +934,7 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
        return fd;
 }
 
-double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
+static double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
 {
        int val = 0;
 
@@ -992,6 +993,63 @@ double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
        }
 }
 
+#define HWMON_RESET() {\
+               buf1[0] = 0; \
+               factor = 1.0; \
+               offset = 0.0; }
+
+static void parse_sysfs_sensor(struct text_object *obj, const char *arg, const char *path, const char *type)
+{
+       char buf1[64], buf2[64];
+       float factor, offset;
+       int n, found = 0;
+
+       if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
+       if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
+       if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
+       if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
+
+       if (!found) {
+               NORM_ERR("i2c failed to parse arguments");
+               obj->type = OBJ_text;
+               return;
+       }
+       DBGP("parsed %s args: '%s' '%s' %d %f %f\n", type, buf1, buf2, n, factor, offset);
+       obj->data.sysfs.fd = open_sysfs_sensor(path, (*buf1) ? buf1 : 0, buf2, n,
+                       &obj->data.sysfs.arg, obj->data.sysfs.devtype);
+       strncpy(obj->data.sysfs.type, buf2, 63);
+       obj->data.sysfs.factor = factor;
+       obj->data.sysfs.offset = offset;
+}
+
+#define PARSER_GENERATOR(name, path)                                \
+void parse_##name##_sensor(struct text_object *obj, const char *arg) \
+{                                                                   \
+       parse_sysfs_sensor(obj, arg, path, #name);           \
+}
+
+PARSER_GENERATOR(i2c, "/sys/bus/i2c/devices/")
+PARSER_GENERATOR(hwmon, "/sys/class/hwmon/")
+PARSER_GENERATOR(platform, "/sys/bus/platform/devices/")
+
+void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size)
+{
+       double r;
+
+       r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
+                       obj->data.sysfs.devtype, obj->data.sysfs.type);
+
+       r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
+
+       if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
+               temp_print(p, p_max_size, r, TEMP_CELSIUS);
+       } else if (r >= 100.0 || r == 0) {
+               snprintf(p, p_max_size, "%d", (int) r);
+       } else {
+               snprintf(p, p_max_size, "%.1f", r);
+       }
+}
+
 /* Prior to kernel version 2.6.12, the CPU fan speed was available in
  * ADT746X_FAN_OLD, whereas later kernel versions provide this information in
  * ADT746X_FAN. */
index 8b6bea8..85596a0 100644 (file)
@@ -29,16 +29,9 @@ void update_gateway_info(void);
 enum { PB_BATT_STATUS, PB_BATT_PERCENT, PB_BATT_TIME };
 void get_powerbook_batt_info(char *, size_t, int);
 
-int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
-       int *divisor, char *devtype);
-
-#define open_i2c_sensor(dev, type, n, divisor, devtype) \
-       open_sysfs_sensor("/sys/bus/i2c/devices/", dev, type, n, divisor, devtype)
-#define open_platform_sensor(dev, type, n, divisor, devtype) \
-       open_sysfs_sensor("/sys/bus/platform/devices/", dev, type, n, divisor, devtype)
-#define open_hwmon_sensor(dev, type, n, divisor, devtype) \
-       open_sysfs_sensor("/sys/class/hwmon/", dev, type, n, divisor, devtype)
-
-double get_sysfs_info(int *fd, int arg, char *devtype, char *type);
+void parse_i2c_sensor(struct text_object *, const char *);
+void parse_hwmon_sensor(struct text_object *, const char *);
+void parse_platform_sensor(struct text_object *, const char *);
+void print_sysfs_sensor(struct text_object *, char *, int );
 
 #endif /* _LINUX_H */