1 /* smapi.c: conky support for IBM Thinkpad smapi
3 * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
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.
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.
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
20 * vim: ts=4 sw=4 noet ai cindent syntax=c
24 #include "conky.h" /* text_buffer_size, PACKAGE_NAME, maybe more */
31 #include <sys/types.h>
33 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
35 int smapi_bat_installed(int idx)
41 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
42 if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
43 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
44 ret = (smapi_read_int(path) == 1) ? 1 : 0;
50 char *smapi_read_str(const char *path)
53 char str[256] = "failed";
54 if ((fp = fopen(path, "r")) != NULL) {
55 fscanf(fp, "%255s\n", str);
58 return strndup(str, text_buffer_size);
61 int smapi_read_int(const char *path)
65 if ((fp = fopen(path, "r")) != NULL) {
66 fscanf(fp, "%i\n", &i);
72 char *smapi_get_str(const char *fname)
75 if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
78 return smapi_read_str(path);
81 char *smapi_get_bat_str(int idx, const char *fname)
84 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
86 return smapi_read_str(path);
89 int smapi_get_bat_int(int idx, const char *fname)
92 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
94 return smapi_read_int(path);
97 char *smapi_get_bat_val(const char *args)
102 if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
103 snprintf(fname, 127, "%s", (args + cnt)) < 0) {
104 ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
108 if(!smapi_bat_installed(idx))
111 return smapi_get_bat_str(idx, fname);
114 char *smapi_get_val(const char *args)
118 if(!args || sscanf(args, "%127s", str) <= 0)
121 if(!strcmp(str, "bat"))
122 return smapi_get_bat_val(args + 4);
124 return smapi_get_str(str);