Fix for segfault in top_name stuff.
[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 int 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 0;
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         return 0;
110 }
111
112 /* get the measured temperatures from the temperature sensors
113  * on IBM/Lenovo laptops running the ibm acpi.
114  * There are 8 values in /proc/acpi/ibm/thermal, and according to
115  * http://ibm-acpi.sourceforge.net/README
116  * these mean the following (at least on an IBM R51...)
117  * 0:  CPU (also on the T series laptops)
118  * 1:  Mini PCI Module (?)
119  * 2:  HDD (?)
120  * 3:  GPU (also on the T series laptops)
121  * 4:  Battery (?)
122  * 5:  N/A
123  * 6:  Battery (?)
124  * 7:  N/A
125  * I'm not too sure about those with the question mark, but the values I'm
126  * reading from *my* thermal file (on a T42p) look realistic for the
127  * hdd and the battery.
128  * #5 and #7 are always -128.
129  * /proc/acpi/ibm/thermal looks like this (1 line):
130 temperatures:   41 43 31 46 33 -128 29 -128
131  * Peter Tarjan (ptarjan@citromail.hu) */
132
133 int get_ibm_acpi_temps(void)
134 {
135
136         FILE *fp;
137         char thermal[128];
138
139         snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
140         fp = fopen(thermal, "r");
141
142         if (fp != NULL) {
143                 while (!feof(fp)) {
144                         char line[256];
145
146                         if (fgets(line, 255, fp) == NULL) {
147                                 break;
148                         }
149                         if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
150                                         &ibm_acpi_temps[0], &ibm_acpi_temps[1], &ibm_acpi_temps[2],
151                                         &ibm_acpi_temps[3], &ibm_acpi_temps[4], &ibm_acpi_temps[5],
152                                         &ibm_acpi_temps[6], &ibm_acpi_temps[7])) {
153                                 break;
154                         }
155                 }
156         } else {
157                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
158                         "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
159         }
160
161         fclose(fp);
162         return 0;
163 }
164
165 /* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
166  * "Volume" here is none of the mixer volumes, but a "master of masters"
167  * volume adjusted by the IBM volume keys.
168  * /proc/acpi/ibm/fan looks like this (4 lines):
169 level:          4
170 mute:           off
171 commands:       up, down, mute
172 commands:       level <level> (<level> is 0-15)
173  * Peter Tarjan (ptarjan@citromail.hu) */
174
175 void get_ibm_acpi_volume(struct text_object *obj, char *p, int p_max_size)
176 {
177         FILE *fp;
178         char volume[128];
179         unsigned int vol = -1;
180         char mute[3] = "";
181
182         (void)obj;
183
184         if (!p || p_max_size <= 0) {
185                 return;
186         }
187
188         snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
189
190         fp = fopen(volume, "r");
191         if (fp != NULL) {
192                 while (!feof(fp)) {
193                         char line[256];
194                         unsigned int read_vol = -1;
195
196                         if (fgets(line, 255, fp) == NULL) {
197                                 break;
198                         }
199                         if (sscanf(line, "level: %u", &read_vol)) {
200                                 vol = read_vol;
201                                 continue;
202                         }
203                         if (sscanf(line, "mute: %s", mute)) {
204                                 break;
205                         }
206                 }
207         } else {
208                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
209                         "ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno));
210         }
211
212         fclose(fp);
213
214         if (strcmp(mute, "on") == 0)
215                 snprintf(p, p_max_size, "%s", "mute");
216         else
217                 snprintf(p, p_max_size, "%d", vol);
218 }
219
220 /* static FILE *fp = NULL; */
221
222 /* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
223  * /proc/acpi/ibm/brightness looks like this (3 lines):
224 level:          7
225 commands:       up, down
226 commands:       level <level> (<level> is 0-7)
227  * Peter Tarjan (ptarjan@citromail.hu) */
228
229 void get_ibm_acpi_brightness(struct text_object *obj, char *p, int p_max_size)
230 {
231         FILE *fp;
232         unsigned int brightness = 0;
233         char filename[128];
234
235         (void)obj;
236
237         if (!p || p_max_size <= 0) {
238                 return;
239         }
240
241         snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
242
243         fp = fopen(filename, "r");
244         if (fp != NULL) {
245                 while (!feof(fp)) {
246                         char line[256];
247
248                         if (fgets(line, 255, fp) == NULL) {
249                                 break;
250                         }
251                         if (sscanf(line, "level: %u", &brightness)) {
252                                 break;
253                         }
254                 }
255         } else {
256                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
257                         "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
258         }
259
260         fclose(fp);
261
262         snprintf(p, p_max_size, "%d", brightness);
263 }
264
265 void parse_ibm_temps_arg(struct text_object *obj, const char *arg)
266 {
267         if (!isdigit(arg[0]) || strlen(arg) > 1 || atoi(&arg[0]) >= 8) {
268                 obj->data.l = 0;
269                 NORM_ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
270                                 "Using 0 (CPU temp sensor).");
271         } else
272                 obj->data.l = atoi(arg);
273 }
274
275 void print_ibm_temps(struct text_object *obj, char *p, int p_max_size)
276 {
277         temp_print(p, p_max_size, ibm_acpi_temps[obj->data.l], TEMP_CELSIUS);
278 }