From b6d1ff6c4213f47cc2edfdde16b3d9115ba9f47b Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Mon, 16 Nov 2009 20:58:26 +0100 Subject: [PATCH] i8k: outsource code --- src/Makefile.am | 2 +- src/common.h | 1 - src/conky.c | 57 ++++-------------- src/core.c | 1 + src/i8k.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++ src/i8k.h | 47 +++++++++++++++ src/linux.c | 34 ----------- src/linux.h | 15 ----- 8 files changed, 213 insertions(+), 97 deletions(-) create mode 100644 src/i8k.c create mode 100644 src/i8k.h diff --git a/src/Makefile.am b/src/Makefile.am index dc55c2d2..0dc7241f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/common.h b/src/common.h index de9d6694..a0f006ab 100644 --- a/src/common.h +++ b/src/common.h @@ -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 */ diff --git a/src/conky.c b/src/conky.c index bd021705..de97dacd 100644 --- a/src/conky.c +++ b/src/conky.c @@ -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) { diff --git a/src/core.c b/src/core.c index c0f5cd56..98e9b0ee 100644 --- a/src/core.c +++ b/src/core.c @@ -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 index 00000000..07056587 --- /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 . + * + */ + +#include +#include +#include +#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 index 00000000..0e1bf422 --- /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 . + * + */ + +#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 */ diff --git a/src/linux.c b/src/linux.c index 1180bee7..f70117f9 100644 --- a/src/linux.c +++ b/src/linux.c @@ -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); -} - /***********************************************************/ /***********************************************************/ /***********************************************************/ diff --git a/src/linux.h b/src/linux.h index 37e49105..c6f651f7 100644 --- a/src/linux.h +++ b/src/linux.h @@ -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);