c21d4b1574dd11b35eb16697b4be49c7caa47584
[monky] / src / i8k.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2007 Toni Spets
14  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15  *      (see AUTHORS)
16  * All rights reserved.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "text_object.h"
36 #include "temphelper.h"
37 #include "logging.h"
38
39 struct {
40         char *version;
41         char *bios;
42         char *serial;
43         char *cpu_temp;
44         char *left_fan_status;
45         char *right_fan_status;
46         char *left_fan_rpm;
47         char *right_fan_rpm;
48         char *ac_status;
49         char *buttons_status;
50 } i8k;
51
52 /* FIXME: there should be an ioctl interface to request specific data */
53 #define PROC_I8K "/proc/i8k"
54 #define I8K_DELIM " "
55 static char *i8k_procbuf = NULL;
56 int update_i8k(void)
57 {
58         FILE *fp;
59
60         if (!i8k_procbuf) {
61                 i8k_procbuf = (char *) malloc(128 * sizeof(char));
62         }
63         if ((fp = fopen(PROC_I8K, "r")) == NULL) {
64                 free(i8k_procbuf);
65                 i8k_procbuf = NULL;
66                 NORM_ERR("/proc/i8k doesn't exist! use insmod to make sure the kernel "
67                         "driver is loaded...");
68                 clean_up_without_threads(NULL, NULL);
69                 free(current_mail_spool);
70                 return 1;
71         }
72
73         memset(&i8k_procbuf[0], 0, 128);
74         if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
75                 NORM_ERR("something wrong with /proc/i8k...");
76         }
77
78         fclose(fp);
79
80         i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
81         i8k.bios = strtok(NULL, I8K_DELIM);
82         i8k.serial = strtok(NULL, I8K_DELIM);
83         i8k.cpu_temp = strtok(NULL, I8K_DELIM);
84         i8k.left_fan_status = strtok(NULL, I8K_DELIM);
85         i8k.right_fan_status = strtok(NULL, I8K_DELIM);
86         i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
87         i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
88         i8k.ac_status = strtok(NULL, I8K_DELIM);
89         i8k.buttons_status = strtok(NULL, I8K_DELIM);
90         return 0;
91 }
92
93 static const char *fan_status_to_string(int status)
94 {
95         switch(status) {
96                 case 0: return "off";
97                 case 1: return "low";
98                 case 2: return "high";
99         }
100         return "error";
101 }
102
103 void print_i8k_left_fan_status(struct text_object *obj, char *p, int p_max_size)
104 {
105         (void)obj;
106         snprintf(p, p_max_size, "%s",
107                  fan_status_to_string(atoi(i8k.left_fan_status)));
108 }
109
110 void print_i8k_cpu_temp(struct text_object *obj, char *p, int p_max_size)
111 {
112         int cpu_temp;
113
114         (void)obj;
115
116         sscanf(i8k.cpu_temp, "%d", &cpu_temp);
117         temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
118 }
119
120 void print_i8k_right_fan_status(struct text_object *obj, char *p, int p_max_size)
121 {
122         (void)obj;
123         snprintf(p, p_max_size, "%s",
124                  fan_status_to_string(atoi(i8k.right_fan_status)));
125 }
126
127 void print_i8k_ac_status(struct text_object *obj, char *p, int p_max_size)
128 {
129         int ac_status;
130
131         (void)obj;
132
133         sscanf(i8k.ac_status, "%d", &ac_status);
134         if (ac_status == -1) {
135                 snprintf(p, p_max_size, "disabled (read i8k docs)");
136         }
137         if (ac_status == 0) {
138                 snprintf(p, p_max_size, "off");
139         }
140         if (ac_status == 1) {
141                 snprintf(p, p_max_size, "on");
142         }
143 }
144
145 #define I8K_PRINT_GENERATOR(name) \
146 void print_i8k_##name(struct text_object *obj, char *p, int p_max_size) \
147 { \
148         (void)obj; \
149         snprintf(p, p_max_size, "%s", i8k.name); \
150 }
151
152 I8K_PRINT_GENERATOR(version)
153 I8K_PRINT_GENERATOR(bios)
154 I8K_PRINT_GENERATOR(serial)
155 I8K_PRINT_GENERATOR(left_fan_rpm)
156 I8K_PRINT_GENERATOR(right_fan_rpm)
157 I8K_PRINT_GENERATOR(buttons_status)
158
159 #undef I8K_PRINT_GENERATOR