Extend commit 25680305095bfcedaa46cb017182544183ab743b to the whole cpu object.
[monky] / src / ibm.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 "conky.h"
33 #include "config.h"
34 #include "ibm.h"
35 #include "logging.h"
36 #include "temphelper.h"
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <stdlib.h>
42
43 static int ibm_acpi_temps[8];
44
45 /* Here come the IBM ACPI-specific things. For reference, see
46  * http://ibm-acpi.sourceforge.net/README
47  * If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
48 bay
49 beep
50 bluetooth
51 brightness
52 cmos
53 dock
54 driver
55 ecdump
56 fan
57 hotkey
58 led
59 light
60 thermal
61 video
62 volume
63  * The content of these files is described in detail in the aforementioned
64  * README - some of them also in the following functions accessing them.
65  * Peter Tarjan (ptarjan@citromail.hu) */
66
67 #define IBM_ACPI_DIR "/proc/acpi/ibm"
68
69 /* get fan speed on IBM/Lenovo laptops running the ibm acpi.
70  * /proc/acpi/ibm/fan looks like this (3 lines):
71 status:         disabled
72 speed:          2944
73 commands:       enable, disable
74  * Peter Tarjan (ptarjan@citromail.hu) */
75
76 void get_ibm_acpi_fan(struct text_object *obj, char *p, int p_max_size)
77 {
78         FILE *fp;
79         unsigned int speed = 0;
80         char fan[128];
81
82         (void)obj;
83
84         if (!p || p_max_size <= 0) {
85                 return;
86         }
87
88         snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
89
90         fp = fopen(fan, "r");
91         if (fp != NULL) {
92                 while (!feof(fp)) {
93                         char line[256];
94
95                         if (fgets(line, 255, fp) == NULL) {
96                                 break;
97                         }
98                         if (sscanf(line, "speed: %u", &speed)) {
99                                 break;
100                         }
101                 }
102         } else {
103                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
104                         "ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno));
105         }
106
107         fclose(fp);
108         snprintf(p, p_max_size, "%d", speed);
109 }
110
111 /* get the measured temperatures from the temperature sensors
112  * on IBM/Lenovo laptops running the ibm acpi.
113  * There are 8 values in /proc/acpi/ibm/thermal, and according to
114  * http://ibm-acpi.sourceforge.net/README
115  * these mean the following (at least on an IBM R51...)
116  * 0:  CPU (also on the T series laptops)
117  * 1:  Mini PCI Module (?)
118  * 2:  HDD (?)
119  * 3:  GPU (also on the T series laptops)
120  * 4:  Battery (?)
121  * 5:  N/A
122  * 6:  Battery (?)
123  * 7:  N/A
124  * I'm not too sure about those with the question mark, but the values I'm
125  * reading from *my* thermal file (on a T42p) look realistic for the
126  * hdd and the battery.
127  * #5 and #7 are always -128.
128  * /proc/acpi/ibm/thermal looks like this (1 line):
129 temperatures:   41 43 31 46 33 -128 29 -128
130  * Peter Tarjan (ptarjan@citromail.hu) */
131
132 void get_ibm_acpi_temps(void)
133 {
134
135         FILE *fp;
136         char thermal[128];
137
138         snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
139         fp = fopen(thermal, "r");
140
141         if (fp != NULL) {
142                 while (!feof(fp)) {
143                         char line[256];
144
145                         if (fgets(line, 255, fp) == NULL) {
146                                 break;
147                         }
148                         if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
149                                         &ibm_acpi_temps[0], &ibm_acpi_temps[1], &ibm_acpi_temps[2],
150                                         &ibm_acpi_temps[3], &ibm_acpi_temps[4], &ibm_acpi_temps[5],
151                                         &ibm_acpi_temps[6], &ibm_acpi_temps[7])) {
152                                 break;
153                         }
154                 }
155         } else {
156                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
157                         "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
158         }
159
160         fclose(fp);
161 }
162
163 /* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
164  * "Volume" here is none of the mixer volumes, but a "master of masters"
165  * volume adjusted by the IBM volume keys.
166  * /proc/acpi/ibm/fan looks like this (4 lines):
167 level:          4
168 mute:           off
169 commands:       up, down, mute
170 commands:       level <level> (<level> is 0-15)
171  * Peter Tarjan (ptarjan@citromail.hu) */
172
173 void get_ibm_acpi_volume(struct text_object *obj, char *p, int p_max_size)
174 {
175         FILE *fp;
176         char volume[128];
177         unsigned int vol = -1;
178         char mute[3] = "";
179
180         (void)obj;
181
182         if (!p || p_max_size <= 0) {
183                 return;
184         }
185
186         snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
187
188         fp = fopen(volume, "r");
189         if (fp != NULL) {
190                 while (!feof(fp)) {
191                         char line[256];
192                         unsigned int read_vol = -1;
193
194                         if (fgets(line, 255, fp) == NULL) {
195                                 break;
196                         }
197                         if (sscanf(line, "level: %u", &read_vol)) {
198                                 vol = read_vol;
199                                 continue;
200                         }
201                         if (sscanf(line, "mute: %s", mute)) {
202                                 break;
203                         }
204                 }
205         } else {
206                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
207                         "ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno));
208         }
209
210         fclose(fp);
211
212         if (strcmp(mute, "on") == 0)
213                 snprintf(p, p_max_size, "%s", "mute");
214         else
215                 snprintf(p, p_max_size, "%d", vol);
216 }
217
218 /* static FILE *fp = NULL; */
219
220 /* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
221  * /proc/acpi/ibm/brightness looks like this (3 lines):
222 level:          7
223 commands:       up, down
224 commands:       level <level> (<level> is 0-7)
225  * Peter Tarjan (ptarjan@citromail.hu) */
226
227 void get_ibm_acpi_brightness(struct text_object *obj, char *p, int p_max_size)
228 {
229         FILE *fp;
230         unsigned int brightness = 0;
231         char filename[128];
232
233         (void)obj;
234
235         if (!p || p_max_size <= 0) {
236                 return;
237         }
238
239         snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
240
241         fp = fopen(filename, "r");
242         if (fp != NULL) {
243                 while (!feof(fp)) {
244                         char line[256];
245
246                         if (fgets(line, 255, fp) == NULL) {
247                                 break;
248                         }
249                         if (sscanf(line, "level: %u", &brightness)) {
250                                 break;
251                         }
252                 }
253         } else {
254                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
255                         "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
256         }
257
258         fclose(fp);
259
260         snprintf(p, p_max_size, "%d", brightness);
261 }
262
263 void parse_ibm_temps_arg(struct text_object *obj, const char *arg)
264 {
265         if (!isdigit(arg[0]) || strlen(arg) > 1 || atoi(&arg[0]) >= 8) {
266                 obj->data.l = 0;
267                 NORM_ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
268                                 "Using 0 (CPU temp sensor).");
269         } else
270                 obj->data.l = atoi(arg);
271 }
272
273 void print_ibm_temps(struct text_object *obj, char *p, int p_max_size)
274 {
275         temp_print(p, p_max_size, ibm_acpi_temps[obj->data.l], TEMP_CELSIUS);
276 }