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