Add vim modelines.
[monky] / src / ibm.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2007 Toni Spets
11  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12  *      (see AUTHORS)
13  * All rights reserved.
14  *
15  * This program is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  * You should have received a copy of the GNU General Public License
25  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26  *
27  * vim: ts=4 sw=4 noet ai cindent syntax=c
28  *
29  */
30
31 #include "conky.h"
32 #include "config.h"
33 #include "ibm.h"
34 #include "logging.h"
35 #include <stdio.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 /* Here come the IBM ACPI-specific things. For reference, see
41  * http://ibm-acpi.sourceforge.net/README
42  * If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
43 bay
44 beep
45 bluetooth
46 brightness
47 cmos
48 dock
49 driver
50 ecdump
51 fan
52 hotkey
53 led
54 light
55 thermal
56 video
57 volume
58  * The content of these files is described in detail in the aforementioned
59  * README - some of them also in the following functions accessing them.
60  * Peter Tarjan (ptarjan@citromail.hu) */
61
62 #define IBM_ACPI_DIR "/proc/acpi/ibm"
63
64 /* get fan speed on IBM/Lenovo laptops running the ibm acpi.
65  * /proc/acpi/ibm/fan looks like this (3 lines):
66 status:         disabled
67 speed:          2944
68 commands:       enable, disable
69  * Peter Tarjan (ptarjan@citromail.hu) */
70
71 void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
72 {
73         FILE *fp;
74         unsigned int speed = 0;
75         char fan[128];
76
77         if (!p_client_buffer || client_buffer_size <= 0) {
78                 return;
79         }
80
81         snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
82
83         fp = fopen(fan, "r");
84         if (fp != NULL) {
85                 while (!feof(fp)) {
86                         char line[256];
87
88                         if (fgets(line, 255, fp) == NULL) {
89                                 break;
90                         }
91                         if (sscanf(line, "speed: %u", &speed)) {
92                                 break;
93                         }
94                 }
95         } else {
96                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
97                         "ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno));
98         }
99
100         fclose(fp);
101         snprintf(p_client_buffer, client_buffer_size, "%d", speed);
102 }
103
104 /* get the measured temperatures from the temperature sensors
105  * on IBM/Lenovo laptops running the ibm acpi.
106  * There are 8 values in /proc/acpi/ibm/thermal, and according to
107  * http://ibm-acpi.sourceforge.net/README
108  * these mean the following (at least on an IBM R51...)
109  * 0:  CPU (also on the T series laptops)
110  * 1:  Mini PCI Module (?)
111  * 2:  HDD (?)
112  * 3:  GPU (also on the T series laptops)
113  * 4:  Battery (?)
114  * 5:  N/A
115  * 6:  Battery (?)
116  * 7:  N/A
117  * I'm not too sure about those with the question mark, but the values I'm
118  * reading from *my* thermal file (on a T42p) look realistic for the
119  * hdd and the battery.
120  * #5 and #7 are always -128.
121  * /proc/acpi/ibm/thermal looks like this (1 line):
122 temperatures:   41 43 31 46 33 -128 29 -128
123  * Peter Tarjan (ptarjan@citromail.hu) */
124
125 static double last_ibm_acpi_temp_time;
126 void get_ibm_acpi_temps(void)
127 {
128
129         FILE *fp;
130         char thermal[128];
131
132         /* don't update too often */
133         if (current_update_time - last_ibm_acpi_temp_time < 10.00) {
134                 return;
135         }
136         last_ibm_acpi_temp_time = current_update_time;
137
138         /* if (!p_client_buffer || client_buffer_size <= 0) {
139                 return;
140         } */
141
142         snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
143         fp = fopen(thermal, "r");
144
145         if (fp != NULL) {
146                 while (!feof(fp)) {
147                         char line[256];
148
149                         if (fgets(line, 255, fp) == NULL) {
150                                 break;
151                         }
152                         if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
153                                         &ibm_acpi.temps[0], &ibm_acpi.temps[1], &ibm_acpi.temps[2],
154                                         &ibm_acpi.temps[3], &ibm_acpi.temps[4], &ibm_acpi.temps[5],
155                                         &ibm_acpi.temps[6], &ibm_acpi.temps[7])) {
156                                 break;
157                         }
158                 }
159         } else {
160                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
161                         "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
162         }
163
164         fclose(fp);
165 }
166
167 /* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
168  * "Volume" here is none of the mixer volumes, but a "master of masters"
169  * volume adjusted by the IBM volume keys.
170  * /proc/acpi/ibm/fan looks like this (4 lines):
171 level:          4
172 mute:           off
173 commands:       up, down, mute
174 commands:       level <level> (<level> is 0-15)
175  * Peter Tarjan (ptarjan@citromail.hu) */
176
177 void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
178 {
179         FILE *fp;
180         char volume[128];
181         unsigned int vol = -1;
182         char mute[3] = "";
183
184         if (!p_client_buffer || client_buffer_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_client_buffer, client_buffer_size, "%s", "mute");
216                 return;
217         } else {
218                 snprintf(p_client_buffer, client_buffer_size, "%d", vol);
219                 return;
220         }
221 }
222
223 /* static FILE *fp = NULL; */
224
225 /* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
226  * /proc/acpi/ibm/brightness looks like this (3 lines):
227 level:          7
228 commands:       up, down
229 commands:       level <level> (<level> is 0-7)
230  * Peter Tarjan (ptarjan@citromail.hu) */
231
232 void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
233 {
234         FILE *fp;
235         unsigned int brightness = 0;
236         char filename[128];
237
238         if (!p_client_buffer || client_buffer_size <= 0) {
239                 return;
240         }
241
242         snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
243
244         fp = fopen(filename, "r");
245         if (fp != NULL) {
246                 while (!feof(fp)) {
247                         char line[256];
248
249                         if (fgets(line, 255, fp) == NULL) {
250                                 break;
251                         }
252                         if (sscanf(line, "level: %u", &brightness)) {
253                                 break;
254                         }
255                 }
256         } else {
257                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
258                         "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
259         }
260
261         fclose(fp);
262
263         snprintf(p_client_buffer, client_buffer_size, "%d", brightness);
264 }
265