7a25788dd9bf3a89e344194e8c1622c57665d7cd
[monky] / src / sony.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Please see COPYING for details
4  *
5  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
6  * Copyright (c) 2009 Yeon-Hyeong Yang <lbird94@gmail.com>
7  *      (see AUTHORS)
8  * All rights reserved.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 /* conky support for information from sony_laptop kernel module
24  *   information from sony_laptop kernel module
25  *   /sys/devices/platform/sony-laptop
26  *   I mimicked the methods from ibm.c
27  * Yeon-Hyeong Yang <lbird94@gmail.com> */
28
29 #include "conky.h"
30 #include "config.h"
31 #include "sony.h"
32 #include "logging.h"
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #define SONY_LAPTOP_DIR "/sys/devices/platform/sony-laptop"
39
40 /* fanspeed in SONY_LAPTOP_DIR contains an integer value for fanspeed (0~255).
41  * I don't know the exact measurement unit, though. I may assume that 0 for
42  * 'fan stopped' and 255 for 'maximum fan speed'. */
43 void get_sony_fanspeed(char *p_client_buffer, size_t client_buffer_size)
44 {
45         FILE *fp;
46         unsigned int speed = 0;
47         char fan[128];
48
49         if (!p_client_buffer || client_buffer_size <= 0) {
50                 return;
51         }
52
53         snprintf(fan, 127, "%s/fanspeed", SONY_LAPTOP_DIR);
54
55         fp = fopen(fan, "r");
56         if (fp != NULL) {
57                 while (!feof(fp)) {
58                         char line[256];
59
60                         if (fgets(line, 255, fp) == NULL) {
61                                 break;
62                         }
63                         if (sscanf(line, "%u", &speed)) {
64                                 break;
65                         }
66                 }
67         } else {
68                 CRIT_ERR(NULL, NULL, "can't open '%s': %s\nEnable sony support or remove "
69                         "sony* from your "PACKAGE_NAME" config file.",
70                         fan, strerror(errno));
71         }
72
73         fclose(fp);
74         snprintf(p_client_buffer, client_buffer_size, "%d", speed);
75 }
76