/
smapi.cc
195 lines (168 loc) · 4.88 KB
/
smapi.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
*
* 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" /* text_buffer_size, PACKAGE_NAME, maybe more */
#include <errno.h>
#include "temphelper.h"
#include "logging.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
static int smapi_read_int(const char *path)
{
FILE *fp;
int i = 0;
if ((fp = fopen(path, "r"))) {
if (fscanf(fp, "%i\n", &i) < 0)
perror("fscanf()");
fclose(fp);
}
return i;
}
static int smapi_bat_installed_internal(int idx)
{
char path[128];
struct stat sb;
int ret = 0;
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);
ret = (smapi_read_int(path) == 1) ? 1 : 0;
}
return ret;
}
static char *smapi_read_str(const char *path)
{
FILE *fp;
char str[256] = "failed";
if ((fp = fopen(path, "r")) != NULL) {
if (fscanf(fp, "%255s\n", str) < 0)
perror("fscanf()");
fclose(fp);
}
return strndup(str, text_buffer_size.get(*state));
}
static char *smapi_get_str(const char *fname)
{
char path[128];
if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
return NULL;
return smapi_read_str(path);
}
static char *smapi_get_bat_str(int idx, const 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);
}
static int smapi_get_bat_int(int idx, const 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);
}
static char *smapi_get_bat_val(const char *args)
{
char fname[128];
int idx, cnt;
if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
snprintf(fname, 127, "%s", (args + cnt)) < 0) {
NORM_ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
return NULL;
}
if(!smapi_bat_installed_internal(idx))
return NULL;
return smapi_get_bat_str(idx, fname);
}
static char *smapi_get_val(const char *args)
{
char str[128];
if(!args || sscanf(args, "%127s", str) <= 0)
return NULL;
if(!strcmp(str, "bat"))
return smapi_get_bat_val(args + 4);
return smapi_get_str(str);
}
void print_smapi(struct text_object *obj, char *p, int p_max_size)
{
char *s;
if (!obj->data.s)
return;
s = smapi_get_val(obj->data.s);
snprintf(p, p_max_size, "%s", s);
free(s);
}
uint8_t smapi_bat_percentage(struct text_object *obj)
{
int idx, val = 0;
if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
val = smapi_bat_installed_internal(idx) ?
smapi_get_bat_int(idx, "remaining_percent") : 0;
} else
NORM_ERR("argument to smapi_bat_perc must be an integer");
return val;
}
void print_smapi_bat_temp(struct text_object *obj, char *p, int p_max_size)
{
int idx, val;
if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
val = smapi_bat_installed_internal(idx) ?
smapi_get_bat_int(idx, "temperature") : 0;
/* temperature is in milli degree celsius */
temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
} else
NORM_ERR("argument to smapi_bat_temp must be an integer");
}
void print_smapi_bat_power(struct text_object *obj, char *p, int p_max_size)
{
int idx, val;
if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
val = smapi_bat_installed_internal(idx) ?
smapi_get_bat_int(idx, "power_now") : 0;
/* power_now is in mW, set to W with one digit precision */
snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
} else
NORM_ERR("argument to smapi_bat_power must be an integer");
}
double smapi_bat_barval(struct text_object *obj)
{
if (obj->data.i >= 0 && smapi_bat_installed_internal(obj->data.i))
return smapi_get_bat_int(obj->data.i, "remaining_percent");
return 0;
}
int smapi_bat_installed(struct text_object *obj)
{
int idx;
if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
if(!smapi_bat_installed_internal(idx)) {
return 0;
}
} else
NORM_ERR("argument to if_smapi_bat_installed must be an integer");
return 1;
}