1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * smapi.c: conky support for IBM Thinkpad smapi
6 * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 #include "conky.h" /* text_buffer_size, PACKAGE_NAME, maybe more */
32 #include <sys/types.h>
34 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
36 int smapi_bat_installed(int idx)
42 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
43 if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
44 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
45 ret = (smapi_read_int(path) == 1) ? 1 : 0;
51 char *smapi_read_str(const char *path)
54 char str[256] = "failed";
55 if ((fp = fopen(path, "r")) != NULL) {
56 fscanf(fp, "%255s\n", str);
59 return strndup(str, text_buffer_size);
62 int smapi_read_int(const char *path)
66 if ((fp = fopen(path, "r")) != NULL) {
67 fscanf(fp, "%i\n", &i);
73 char *smapi_get_str(const char *fname)
76 if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
79 return smapi_read_str(path);
82 char *smapi_get_bat_str(int idx, const char *fname)
85 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
87 return smapi_read_str(path);
90 int smapi_get_bat_int(int idx, const char *fname)
93 if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
95 return smapi_read_int(path);
98 char *smapi_get_bat_val(const char *args)
103 if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
104 snprintf(fname, 127, "%s", (args + cnt)) < 0) {
105 NORM_ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
109 if(!smapi_bat_installed(idx))
112 return smapi_get_bat_str(idx, fname);
115 char *smapi_get_val(const char *args)
119 if(!args || sscanf(args, "%127s", str) <= 0)
122 if(!strcmp(str, "bat"))
123 return smapi_get_bat_val(args + 4);
125 return smapi_get_str(str);