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
|
/*
* HardInfo - Displays System Information
* Copyright (C) 2003-2006 Leandro A. F. Pereira <leandro@linuxmag.com.br>
*
* This program 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, version 2.
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
static void
scan_battery(void)
{
gchar *acpi_path;
gchar *present = NULL;
gchar *capacity = NULL;
gchar *technology = NULL;
gchar *voltage = NULL;
gchar *model = NULL, *serial = NULL, *type = NULL;
gchar *state = NULL, *rate = NULL;
gchar *remaining = NULL;
if (battery_list) {
g_free(battery_list);
}
battery_list = g_strdup("");
acpi_path = g_strdup("/proc/acpi/battery");
if (g_file_test(acpi_path, G_FILE_TEST_EXISTS)) {
GDir *acpi;
if ((acpi = g_dir_open(acpi_path, 0, NULL))) {
const gchar *entry;
while ((entry = g_dir_read_name(acpi))) {
gchar *path = g_strdup_printf("%s/%s/info", acpi_path, entry);
FILE *f;
gchar buffer[256];
gdouble charge_rate = 1.0;
f = fopen(path, "r");
g_free(path);
if (!f)
goto cleanup;
while (fgets(buffer, 256, f)) {
gchar **tmp = g_strsplit(buffer, ":", 2);
GET_STR("present", present);
GET_STR("design capacity", capacity);
GET_STR("battery technology", technology);
GET_STR("design voltage", voltage);
GET_STR("model number", model);
GET_STR("serial number", serial);
GET_STR("battery type", type);
g_strfreev(tmp);
}
fclose(f);
path = g_strdup_printf("%s/%s/state", acpi_path, entry);
f = fopen(path, "r");
g_free(path);
if (!f)
goto cleanup;
while (fgets(buffer, 256, f)) {
gchar **tmp = g_strsplit(buffer, ":", 2);
GET_STR("charging state", state);
GET_STR("present rate", rate);
GET_STR("remaining capacity", remaining);
g_strfreev(tmp);
}
fclose(f);
if (g_str_equal(present, "yes")) {
charge_rate = atof(remaining) / atof(capacity);
battery_list = g_strdup_printf("%s\n[Battery: %s]\n"
"State=%s (load: %s)\n"
"Capacity=%s / %s (%.2f%%)\n"
"Battery Technology=%s (%s)\n"
"Model Number=%s\n"
"Serial Number=%s\n",
battery_list,
entry,
state, rate,
remaining, capacity, charge_rate * 100.0,
technology, type,
model,
serial);
}
cleanup:
g_free(present);
g_free(capacity);
g_free(technology);
g_free(type);
g_free(model);
g_free(serial);
g_free(state);
g_free(remaining);
g_free(rate);
present = capacity = technology = type = \
model = serial = state = remaining = rate = NULL;
}
g_dir_close(acpi);
}
}
g_free(acpi_path);
}
|