72dc5ed3087a35861fcf9f76fe701096f9651635
[monky] / src / smapi.c
1 /* smapi.c:  conky support for IBM Thinkpad smapi
2  *
3  * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
4  * 
5  * This library is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
18  * USA.
19  *
20  * $Id$
21  *
22  */
23 #include "conky.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
28
29 int smapi_bat_installed(int idx)
30 {
31         char path[128];
32         struct stat sb;
33         int ret = 0;
34
35         snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
36         if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
37                 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
38                 ret = (smapi_read_int(path) == 1) ? 1 : 0;
39         }
40         return ret;
41
42 }
43
44 char *smapi_read_str(char *path)
45 {
46         FILE *fp;
47         char *str = NULL;
48         if ((fp = fopen(path, "r")) != NULL) {
49                 fscanf(fp, "%as\n", &str);
50                 fclose(fp);
51         }
52         return str;
53 }
54
55 int smapi_read_int(char *path)
56 {
57         FILE *fp;
58         int i = 0;
59         if ((fp = fopen(path, "r")) != NULL) {
60                 fscanf(fp, "%i\n", &i);
61                 fclose(fp);
62         }
63         return i;
64 }
65
66 char *smapi_get_str(char *fname)
67 {
68         char path[128];
69         if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
70                 return NULL;
71
72         return smapi_read_str(path);
73 }
74
75 char *smapi_get_bat_str(int idx, char *fname)
76 {
77         char path[128];
78         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
79                 return NULL;
80         return smapi_read_str(path);
81 }
82
83 int smapi_get_bat_int(int idx, char *fname)
84 {
85         char path[128];
86         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
87                 return 0;
88         return smapi_read_int(path);
89 }
90
91 char *smapi_get_bat_val(char *args)
92 {
93         char fname[128];
94         int idx, cnt;
95
96         if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
97            snprintf(fname, 127, "%s", (args + cnt)) < 0) {
98                 ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
99                 return NULL;
100         }
101
102         if(!smapi_bat_installed(idx))
103                 return NULL;
104
105         return smapi_get_bat_str(idx, fname);
106 }
107
108 char *smapi_get_val(char *args)
109 {
110         char str[128];
111
112         if(!args || sscanf(args, "%127s", str) <= 0)
113                 return NULL;
114
115         if(!strcmp(str, "bat"))
116                 return smapi_get_bat_val(args + 4);
117
118         return smapi_get_str(str);
119 }