1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * smapi.c: conky support for IBM Thinkpad smapi
5 * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
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.
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.
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
22 * vim: ts=4 sw=4 noet ai cindent syntax=c
26 #include "conky.h" /* text_buffer_size, PACKAGE_NAME, maybe more */
33 #include <sys/types.h>
35 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
37 int smapi_bat_installed(int idx)
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;
52 char *smapi_read_str(const char *path)
55 char str[256] = "failed";
56 if ((fp = fopen(path, "r")) != NULL) {
57 fscanf(fp, "%255s\n", str);
60 return strndup(str, text_buffer_size);
63 int smapi_read_int(const char *path)
67 if ((fp = fopen(path, "r")) != NULL) {
68 fscanf(fp, "%i\n", &i);
74 char *smapi_get_str(const char *fname)
77 if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
80 return smapi_read_str(path);
83 char *smapi_get_bat_str(int idx, const char *fname)
86 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
88 return smapi_read_str(path);
91 int smapi_get_bat_int(int idx, const char *fname)
94 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
96 return smapi_read_int(path);
99 char *smapi_get_bat_val(const char *args)
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>'");
110 if(!smapi_bat_installed(idx))
113 return smapi_get_bat_str(idx, fname);
116 char *smapi_get_val(const char *args)
120 if(!args || sscanf(args, "%127s", str) <= 0)
123 if(!strcmp(str, "bat"))
124 return smapi_get_bat_val(args + 4);
126 return smapi_get_str(str);