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