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 */
27 #include "temphelper.h"
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);
129 void print_smapi(struct text_object *obj, char *p, int p_max_size)
136 s = smapi_get_val(obj->data.s);
137 snprintf(p, p_max_size, "%s", s);
141 void print_smapi_bat_perc(struct text_object *obj, char *p, int p_max_size)
144 if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
145 val = smapi_bat_installed(idx) ?
146 smapi_get_bat_int(idx, "remaining_percent") : 0;
147 percent_print(p, p_max_size, val);
149 NORM_ERR("argument to smapi_bat_perc must be an integer");
152 void print_smapi_bat_temp(struct text_object *obj, char *p, int p_max_size)
155 if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
156 val = smapi_bat_installed(idx) ?
157 smapi_get_bat_int(idx, "temperature") : 0;
158 /* temperature is in milli degree celsius */
159 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
161 NORM_ERR("argument to smapi_bat_temp must be an integer");
164 void print_smapi_bat_power(struct text_object *obj, char *p, int p_max_size)
167 if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
168 val = smapi_bat_installed(idx) ?
169 smapi_get_bat_int(idx, "power_now") : 0;
170 /* power_now is in mW, set to W with one digit precision */
171 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
173 NORM_ERR("argument to smapi_bat_power must be an integer");
176 void print_smapi_bat_bar(struct text_object *obj, char *p, int p_max_size)
181 if (obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
182 new_bar(obj, p, p_max_size, (int)
183 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
185 new_bar(obj, p, p_max_size, 0);