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
22 #include "conky.h" /* text_buffer_size, PACKAGE_NAME, maybe more */
29 #include <sys/types.h>
31 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
33 int smapi_bat_installed(int idx)
39 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
40 if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
41 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
42 ret = (smapi_read_int(path) == 1) ? 1 : 0;
48 char *smapi_read_str(const char *path)
51 char str[256] = "failed";
52 if ((fp = fopen(path, "r")) != NULL) {
53 fscanf(fp, "%255s\n", str);
56 return strndup(str, text_buffer_size);
59 int smapi_read_int(const char *path)
63 if ((fp = fopen(path, "r")) != NULL) {
64 fscanf(fp, "%i\n", &i);
70 char *smapi_get_str(const char *fname)
73 if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
76 return smapi_read_str(path);
79 char *smapi_get_bat_str(int idx, const char *fname)
82 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
84 return smapi_read_str(path);
87 int smapi_get_bat_int(int idx, const char *fname)
90 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
92 return smapi_read_int(path);
95 char *smapi_get_bat_val(const char *args)
100 if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
101 snprintf(fname, 127, "%s", (args + cnt)) < 0) {
102 ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
106 if(!smapi_bat_installed(idx))
109 return smapi_get_bat_str(idx, fname);
112 char *smapi_get_val(const char *args)
116 if(!args || sscanf(args, "%127s", str) <= 0)
119 if(!strcmp(str, "bat"))
120 return smapi_get_bat_val(args + 4);
122 return smapi_get_str(str);