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