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