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