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
26 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
28 int smapi_bat_installed(int idx)
34 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
35 if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
36 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
37 ret = (smapi_read_int(path) == 1) ? 1 : 0;
43 char *smapi_read_str(const char *path)
46 char str[256] = "failed";
47 if ((fp = fopen(path, "r")) != NULL) {
48 fscanf(fp, "%255s\n", str);
51 return strndup(str, text_buffer_size);
54 int smapi_read_int(const char *path)
58 if ((fp = fopen(path, "r")) != NULL) {
59 fscanf(fp, "%i\n", &i);
65 char *smapi_get_str(const char *fname)
68 if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
71 return smapi_read_str(path);
74 char *smapi_get_bat_str(int idx, const char *fname)
77 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
79 return smapi_read_str(path);
82 int smapi_get_bat_int(int idx, const char *fname)
85 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
87 return smapi_read_int(path);
90 char *smapi_get_bat_val(const char *args)
95 if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
96 snprintf(fname, 127, "%s", (args + cnt)) < 0) {
97 ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
101 if(!smapi_bat_installed(idx))
104 return smapi_get_bat_str(idx, fname);
107 char *smapi_get_val(const char *args)
111 if(!args || sscanf(args, "%127s", str) <= 0)
114 if(!strcmp(str, "bat"))
115 return smapi_get_bat_val(args + 4);
117 return smapi_get_str(str);