2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
2009-09-12 10:50:51 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* smapi.c: conky support for IBM Thinkpad smapi
|
2008-03-18 17:09:47 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
|
2008-06-15 18:38:33 +00:00
|
|
|
*
|
2008-03-18 17:09:47 +00:00
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
*/
|
2008-12-15 21:40:24 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "conky.h" /* text_buffer_size, PACKAGE_NAME, maybe more */
|
2008-06-14 18:41:12 +00:00
|
|
|
#include "smapi.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "logging.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2008-03-18 17:09:47 +00:00
|
|
|
|
|
|
|
#define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
|
|
|
|
|
|
|
|
int smapi_bat_installed(int idx)
|
|
|
|
{
|
|
|
|
char path[128];
|
|
|
|
struct stat sb;
|
2008-03-22 18:08:47 +00:00
|
|
|
int ret = 0;
|
2008-03-18 17:09:47 +00:00
|
|
|
|
|
|
|
snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
|
|
|
|
if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
|
|
|
|
snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
|
2008-03-22 18:08:47 +00:00
|
|
|
ret = (smapi_read_int(path) == 1) ? 1 : 0;
|
2008-03-18 17:09:47 +00:00
|
|
|
}
|
2008-03-22 18:08:47 +00:00
|
|
|
return ret;
|
2008-03-18 17:09:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
char *smapi_read_str(const char *path)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2008-03-29 10:11:16 +00:00
|
|
|
char str[256] = "failed";
|
2008-03-22 18:08:47 +00:00
|
|
|
if ((fp = fopen(path, "r")) != NULL) {
|
2008-03-29 11:49:10 +00:00
|
|
|
fscanf(fp, "%255s\n", str);
|
2008-03-18 17:09:47 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2008-04-02 18:44:49 +00:00
|
|
|
return strndup(str, text_buffer_size);
|
2008-03-18 17:09:47 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
int smapi_read_int(const char *path)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2008-03-22 18:08:47 +00:00
|
|
|
int i = 0;
|
|
|
|
if ((fp = fopen(path, "r")) != NULL) {
|
|
|
|
fscanf(fp, "%i\n", &i);
|
2008-03-18 17:09:47 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
char *smapi_get_str(const char *fname)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
|
|
|
char path[128];
|
|
|
|
if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return smapi_read_str(path);
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
char *smapi_get_bat_str(int idx, const char *fname)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
|
|
|
char path[128];
|
|
|
|
if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
|
|
|
|
return NULL;
|
|
|
|
return smapi_read_str(path);
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
int smapi_get_bat_int(int idx, const char *fname)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
|
|
|
char path[128];
|
|
|
|
if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
|
|
|
|
return 0;
|
|
|
|
return smapi_read_int(path);
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
char *smapi_get_bat_val(const char *args)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
2008-03-22 19:39:43 +00:00
|
|
|
char fname[128];
|
2008-03-18 17:09:47 +00:00
|
|
|
int idx, cnt;
|
|
|
|
|
|
|
|
if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
|
2008-03-22 19:39:43 +00:00
|
|
|
snprintf(fname, 127, "%s", (args + cnt)) < 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
|
2008-03-18 17:09:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!smapi_bat_installed(idx))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return smapi_get_bat_str(idx, fname);
|
|
|
|
}
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
char *smapi_get_val(const char *args)
|
2008-03-18 17:09:47 +00:00
|
|
|
{
|
2008-03-22 18:08:47 +00:00
|
|
|
char str[128];
|
2008-03-18 17:09:47 +00:00
|
|
|
|
2008-03-22 18:08:47 +00:00
|
|
|
if(!args || sscanf(args, "%127s", str) <= 0)
|
2008-03-18 17:09:47 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if(!strcmp(str, "bat"))
|
|
|
|
return smapi_get_bat_val(args + 4);
|
|
|
|
|
|
|
|
return smapi_get_str(str);
|
|
|
|
}
|