1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 04:17:33 +00:00

adding missing files

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@1012 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
Brenden Matthews 2008-03-18 17:09:47 +00:00
parent b77da1255a
commit 6928262418
2 changed files with 157 additions and 0 deletions

125
src/smapi.c Normal file
View File

@ -0,0 +1,125 @@
/* smapi.c: conky support for IBM Thinkpad smapi
*
* Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
*
* 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.
*
*/
#include "conky.h"
#include <stdio.h>
#include <stdlib.h>
#define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
int smapi_bat_installed(int idx)
{
char path[128];
struct stat sb;
char *str;
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);
str = smapi_read_str(path);
if(str && ! strcmp(str, "1"))
return 1;
}
return 0;
}
char *smapi_read_str(char *path)
{
FILE *fp;
char *str;
if ((fp = fopen(path, "r")) == NULL)
return NULL;
if (fscanf(fp, "%as\n", &str) != 1) {
fclose(fp);
return NULL;
}
fclose(fp);
return str;
}
int smapi_read_int(char *path)
{
FILE *fp;
int i;
if ((fp = fopen(path, "r")) == NULL)
return 0;
if (fscanf(fp, "%i\n", &i) != 1) {
fclose(fp);
return 0;
}
fclose(fp);
return i;
}
char *smapi_get_str(char *fname)
{
char path[128];
if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
return NULL;
return smapi_read_str(path);
}
char *smapi_get_bat_str(int idx, char *fname)
{
char path[128];
if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
return NULL;
return smapi_read_str(path);
}
int smapi_get_bat_int(int idx, char *fname)
{
char path[128];
if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
return 0;
return smapi_read_int(path);
}
char *smapi_get_bat_val(char *args)
{
char *fname;
int idx, cnt;
if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
!(fname = strdup(args + cnt))) {
ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
return NULL;
}
if(!smapi_bat_installed(idx))
return NULL;
return smapi_get_bat_str(idx, fname);
}
char *smapi_get_val(char *args)
{
char *str;
if(!args || sscanf(args, "%as", &str) <= 0)
return NULL;
if(!strcmp(str, "bat"))
return smapi_get_bat_val(args + 4);
return smapi_get_str(str);
}

32
src/smapi.h Normal file
View File

@ -0,0 +1,32 @@
/* smapi.h: conky support for IBM Thinkpad smapi
*
* Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
*
* 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.
*
*/
int smapi_bat_installed(int);
char *smapi_read_str(char *);
int smapi_read_int(char *);
char *smapi_get_str(char *);
char *smapi_get_val(char *);
char *smapi_get_bat_str(int, char *);
int smapi_get_bat_int(int, char *);
char *smapi_get_bat_val(char *);