i8k: outsource code
authorPhil Sutter <phil@nwl.cc>
Mon, 16 Nov 2009 19:58:26 +0000 (20:58 +0100)
committerPhil Sutter <phil@nwl.cc>
Thu, 19 Nov 2009 22:04:09 +0000 (23:04 +0100)
src/Makefile.am
src/common.h
src/conky.c
src/core.c
src/i8k.c [new file with mode: 0644]
src/i8k.h [new file with mode: 0644]
src/linux.c
src/linux.h

index dc55c2d..0dc7241 100644 (file)
@@ -65,7 +65,7 @@ ibm = ibm.c ibm.h smapi.c smapi.h
 mpd = mpd.c mpd.h libmpdclient.c libmpdclient.h
 moc = moc.c moc.h
 xmms2 = xmms2.c xmms2.h
-linux = linux.c linux.h top.c top.h users.c sony.c sony.h
+linux = linux.c linux.h top.c top.h users.c sony.c sony.h i8k.c i8k.h
 solaris = solaris.c
 freebsd = freebsd.c freebsd.h
 netbsd = netbsd.c netbsd.h
index de9d669..a0f006a 100644 (file)
@@ -27,7 +27,6 @@ void update_total_processes(void);
 void update_uname(void);
 void update_threads(void);
 void update_running_processes(void);
-void update_i8k(void);
 void update_stuff(void);
 char get_freq(char *, size_t, const char *, int, unsigned int);
 char get_voltage(char *, size_t, const char *, int, unsigned int);     /* ptarjan */
index bd02170..de97dac 100644 (file)
@@ -80,6 +80,7 @@
 #include "diskio.h"
 #include "entropy.h"
 #include "exec.h"
+#include "i8k.h"
 #include "proc.h"
 #include "user.h"
 #ifdef X11
@@ -1002,70 +1003,34 @@ void generate_text_internal(char *p, int p_max_size,
                                                get_disk_protect_queue(obj->data.s));
                        }
                        OBJ(i8k_version) {
-                               snprintf(p, p_max_size, "%s", i8k.version);
+                               print_i8k_version(obj, p, p_max_size);
                        }
                        OBJ(i8k_bios) {
-                               snprintf(p, p_max_size, "%s", i8k.bios);
+                               print_i8k_bios(obj, p, p_max_size);
                        }
                        OBJ(i8k_serial) {
-                               snprintf(p, p_max_size, "%s", i8k.serial);
+                               print_i8k_serial(obj, p, p_max_size);
                        }
                        OBJ(i8k_cpu_temp) {
-                               int cpu_temp;
-
-                               sscanf(i8k.cpu_temp, "%d", &cpu_temp);
-                               temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
+                               print_i8k_cpu_temp(obj, p, p_max_size);
                        }
                        OBJ(i8k_left_fan_status) {
-                               int left_fan_status;
-
-                               sscanf(i8k.left_fan_status, "%d", &left_fan_status);
-                               if (left_fan_status == 0) {
-                                       snprintf(p, p_max_size, "off");
-                               }
-                               if (left_fan_status == 1) {
-                                       snprintf(p, p_max_size, "low");
-                               }
-                               if (left_fan_status == 2) {
-                                       snprintf(p, p_max_size, "high");
-                               }
+                               print_i8k_left_fan_status(obj, p, p_max_size);
                        }
                        OBJ(i8k_right_fan_status) {
-                               int right_fan_status;
-
-                               sscanf(i8k.right_fan_status, "%d", &right_fan_status);
-                               if (right_fan_status == 0) {
-                                       snprintf(p, p_max_size, "off");
-                               }
-                               if (right_fan_status == 1) {
-                                       snprintf(p, p_max_size, "low");
-                               }
-                               if (right_fan_status == 2) {
-                                       snprintf(p, p_max_size, "high");
-                               }
+                               print_i8k_right_fan_status(obj, p, p_max_size);
                        }
                        OBJ(i8k_left_fan_rpm) {
-                               snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
+                               print_i8k_left_fan_rpm(obj, p, p_max_size);
                        }
                        OBJ(i8k_right_fan_rpm) {
-                               snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
+                               print_i8k_right_fan_rpm(obj, p, p_max_size);
                        }
                        OBJ(i8k_ac_status) {
-                               int ac_status;
-
-                               sscanf(i8k.ac_status, "%d", &ac_status);
-                               if (ac_status == -1) {
-                                       snprintf(p, p_max_size, "disabled (read i8k docs)");
-                               }
-                               if (ac_status == 0) {
-                                       snprintf(p, p_max_size, "off");
-                               }
-                               if (ac_status == 1) {
-                                       snprintf(p, p_max_size, "on");
-                               }
+                               print_i8k_ac_status(obj, p, p_max_size);
                        }
                        OBJ(i8k_buttons_status) {
-                               snprintf(p, p_max_size, "%s", i8k.buttons_status);
+                               print_i8k_buttons_status(obj, p, p_max_size);
                        }
 #if defined(IBM)
                        OBJ(ibm_fan) {
index c0f5cd5..98e9b0e 100644 (file)
@@ -38,6 +38,7 @@
 #include "diskio.h"
 #include "entropy.h"
 #include "exec.h"
+#include "i8k.h"
 #include "proc.h"
 #ifdef X11
 #include "fonts.h"
diff --git a/src/i8k.c b/src/i8k.c
new file mode 100644 (file)
index 0000000..0705658
--- /dev/null
+++ b/src/i8k.c
@@ -0,0 +1,153 @@
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
+ *
+ * Conky, a system monitor, based on torsmo
+ *
+ * Any original torsmo code is licensed under the BSD license
+ *
+ * All code written since the fork of torsmo is licensed under the GPL
+ *
+ * Please see COPYING for details
+ *
+ * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
+ * Copyright (c) 2007 Toni Spets
+ * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ *     (see AUTHORS)
+ * All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "logging.h"
+#include "temphelper.h"
+#include "text_object.h"
+
+struct {
+       char *version;
+       char *bios;
+       char *serial;
+       char *cpu_temp;
+       char *left_fan_status;
+       char *right_fan_status;
+       char *left_fan_rpm;
+       char *right_fan_rpm;
+       char *ac_status;
+       char *buttons_status;
+} i8k;
+
+/* FIXME: there should be an ioctl interface to request specific data */
+#define PROC_I8K "/proc/i8k"
+#define I8K_DELIM " "
+static char *i8k_procbuf = NULL;
+void update_i8k(void)
+{
+       FILE *fp;
+
+       if (!i8k_procbuf) {
+               i8k_procbuf = (char *) malloc(128 * sizeof(char));
+       }
+       if ((fp = fopen(PROC_I8K, "r")) == NULL) {
+               CRIT_ERR(NULL, NULL, "/proc/i8k doesn't exist! use insmod to make sure the kernel "
+                       "driver is loaded...");
+       }
+
+       memset(&i8k_procbuf[0], 0, 128);
+       if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
+               NORM_ERR("something wrong with /proc/i8k...");
+       }
+
+       fclose(fp);
+
+       i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
+       i8k.bios = strtok(NULL, I8K_DELIM);
+       i8k.serial = strtok(NULL, I8K_DELIM);
+       i8k.cpu_temp = strtok(NULL, I8K_DELIM);
+       i8k.left_fan_status = strtok(NULL, I8K_DELIM);
+       i8k.right_fan_status = strtok(NULL, I8K_DELIM);
+       i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
+       i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
+       i8k.ac_status = strtok(NULL, I8K_DELIM);
+       i8k.buttons_status = strtok(NULL, I8K_DELIM);
+}
+
+static const char *fan_status_to_string(int status)
+{
+       switch(status) {
+               case 0: return "off";
+               case 1: return "low";
+               case 2: return "high";
+       }
+       return "error";
+}
+
+void print_i8k_left_fan_status(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       snprintf(p, p_max_size, "%s",
+                fan_status_to_string(atoi(i8k.left_fan_status)));
+}
+
+void print_i8k_cpu_temp(struct text_object *obj, char *p, int p_max_size)
+{
+       int cpu_temp;
+
+       (void)obj;
+
+       sscanf(i8k.cpu_temp, "%d", &cpu_temp);
+       temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
+}
+
+void print_i8k_right_fan_status(struct text_object *obj, char *p, int p_max_size)
+{
+       (void)obj;
+       snprintf(p, p_max_size, "%s",
+                fan_status_to_string(atoi(i8k.right_fan_status)));
+}
+
+void print_i8k_ac_status(struct text_object *obj, char *p, int p_max_size)
+{
+       int ac_status;
+
+       (void)obj;
+
+       sscanf(i8k.ac_status, "%d", &ac_status);
+       if (ac_status == -1) {
+               snprintf(p, p_max_size, "disabled (read i8k docs)");
+       }
+       if (ac_status == 0) {
+               snprintf(p, p_max_size, "off");
+       }
+       if (ac_status == 1) {
+               snprintf(p, p_max_size, "on");
+       }
+}
+
+#define I8K_PRINT_GENERATOR(name) \
+void print_i8k_##name(struct text_object *obj, char *p, int p_max_size) \
+{ \
+       (void)obj; \
+       snprintf(p, p_max_size, "%s", i8k.name); \
+}
+
+I8K_PRINT_GENERATOR(version)
+I8K_PRINT_GENERATOR(bios)
+I8K_PRINT_GENERATOR(serial)
+I8K_PRINT_GENERATOR(left_fan_rpm)
+I8K_PRINT_GENERATOR(right_fan_rpm)
+I8K_PRINT_GENERATOR(buttons_status)
+
+#undef I8K_PRINT_GENERATOR
diff --git a/src/i8k.h b/src/i8k.h
new file mode 100644 (file)
index 0000000..0e1bf42
--- /dev/null
+++ b/src/i8k.h
@@ -0,0 +1,47 @@
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
+ *
+ * Conky, a system monitor, based on torsmo
+ *
+ * Any original torsmo code is licensed under the BSD license
+ *
+ * All code written since the fork of torsmo is licensed under the GPL
+ *
+ * Please see COPYING for details
+ *
+ * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
+ * Copyright (c) 2007 Toni Spets
+ * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ *     (see AUTHORS)
+ * All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef _I8K_H
+#define _I8K_H
+
+void update_i8k(void);
+void print_i8k_left_fan_status(struct text_object *, char *, int);
+void print_i8k_cpu_temp(struct text_object *, char *, int);
+void print_i8k_right_fan_status(struct text_object *, char *, int);
+void print_i8k_ac_status(struct text_object *, char *, int);
+void print_i8k_version(struct text_object *, char *, int);
+void print_i8k_bios(struct text_object *, char *, int);
+void print_i8k_serial(struct text_object *, char *, int);
+void print_i8k_left_fan_rpm(struct text_object *, char *, int);
+void print_i8k_right_fan_rpm(struct text_object *, char *, int);
+void print_i8k_buttons_status(struct text_object *, char *, int);
+
+#endif /* _I8K_H */
index 1180bee..f70117f 100644 (file)
@@ -819,40 +819,6 @@ void update_load_average(void)
        }
 }
 
-#define PROC_I8K "/proc/i8k"
-#define I8K_DELIM " "
-static char *i8k_procbuf = NULL;
-void update_i8k(void)
-{
-       FILE *fp;
-
-       if (!i8k_procbuf) {
-               i8k_procbuf = (char *) malloc(128 * sizeof(char));
-       }
-       if ((fp = fopen(PROC_I8K, "r")) == NULL) {
-               CRIT_ERR(NULL, NULL, "/proc/i8k doesn't exist! use insmod to make sure the kernel "
-                       "driver is loaded...");
-       }
-
-       memset(&i8k_procbuf[0], 0, 128);
-       if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
-               NORM_ERR("something wrong with /proc/i8k...");
-       }
-
-       fclose(fp);
-
-       i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
-       i8k.bios = strtok(NULL, I8K_DELIM);
-       i8k.serial = strtok(NULL, I8K_DELIM);
-       i8k.cpu_temp = strtok(NULL, I8K_DELIM);
-       i8k.left_fan_status = strtok(NULL, I8K_DELIM);
-       i8k.right_fan_status = strtok(NULL, I8K_DELIM);
-       i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
-       i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
-       i8k.ac_status = strtok(NULL, I8K_DELIM);
-       i8k.buttons_status = strtok(NULL, I8K_DELIM);
-}
-
 /***********************************************************/
 /***********************************************************/
 /***********************************************************/
index 37e4910..c6f651f 100644 (file)
@@ -7,21 +7,6 @@
 
 const char *get_disk_protect_queue(const char *);
 
-struct i8k_struct {
-       char *version;
-       char *bios;
-       char *serial;
-       char *cpu_temp;
-       char *left_fan_status;
-       char *right_fan_status;
-       char *left_fan_rpm;
-       char *right_fan_rpm;
-       char *ac_status;
-       char *buttons_status;
-};
-
-struct i8k_struct i8k;
-
 char *get_ioscheduler(char *);
 int get_laptop_mode(void);