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
|
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <libacpi.h>
#include "error.h"
void emit_ts(void)
{
time_t now = time(NULL);
printf("%s", ctime(&now));
}
void header(char *header_shown)
{
if (!*header_shown)
{
emit_ts();
*header_shown = 1;
}
}
void emit_battery(global_t *globals, char *header_shown)
{
int loop;
static int *prevs = NULL;
if (!prevs)
{
int bytes = sizeof(int) * globals -> batt_count;
prevs = malloc(bytes);
memset(prevs, 0x00, bytes);
}
for(loop=0; loop<globals -> batt_count; loop++)
{
int rc = read_acpi_batt(loop);
if (rc == ITEM_EXCEED)
break;
if (rc == ALLOC_ERR)
error_exit("reac_acpi_batt(%d) failed\n", loop);
if (prevs[loop] != batteries[loop].percentage)
{
header(header_shown);
printf("%s remaining capacity: %d%% (%d minutes)\n", batteries[loop].name, batteries[loop].percentage, batteries[loop].remaining_time);
prevs[loop] = batteries[loop].percentage;
}
}
}
char * power_state_str(power_state_t ps)
{
switch(ps)
{
case P_AC:
return "AC";
case P_BATT:
return "batteries";
case P_ERR:
return "error";
}
return "?";
}
void emit_acstate(global_t *globals, char *header_shown)
{
static int prev_state = -1;
read_acpi_acstate(globals);
if (prev_state != globals -> adapt.ac_state)
{
header(header_shown);
printf("Adapter %s: %s\n", globals -> adapt.name, power_state_str(globals -> adapt.ac_state));
prev_state = globals -> adapt.ac_state;
}
}
char * thermal_state_str(thermal_state_t ts)
{
switch(ts)
{
case T_CRIT:
return "critical temperature, will switch to S4";
case T_HOT:
return "high temperature, will shutdown immediately";
case T_PASS:
return "passive cooling";
case T_ACT:
return "active cooling";
case T_OK:
return "ok";
case T_ERR:
return "error";
}
return "?";
}
void emit_zone(global_t *globals, char *header_shown)
{
int loop;
static int *prevs = NULL;
static int *prevs_ts = NULL;
if (!prevs)
{
int bytes = sizeof(int) * globals -> thermal_count;
prevs = malloc(bytes);
prevs_ts = malloc(bytes);
memset(prevs, 0x00, bytes);
memset(prevs_ts, 0x00, bytes);
}
for(loop=0; loop<globals -> thermal_count; loop++)
{
int rc = read_acpi_zone(loop, globals);
if (rc == ITEM_EXCEED)
break;
if (rc == ALLOC_ERR)
error_exit("reac_acpi_zone(%d) failed\n", loop);
if (prevs[loop] != thermals[loop].temperature ||
prevs_ts[loop] != thermals[loop].therm_state)
{
header(header_shown);
printf("temperature %s: %d (%s)\n", thermals[loop].name, thermals[loop].temperature, thermal_state_str(thermals[loop].therm_state));
prevs[loop] = thermals[loop].temperature;
prevs_ts[loop] = thermals[loop].therm_state;
}
}
}
char * fan_state_str(fan_state_t fs)
{
switch(fs)
{
case F_ON:
return "on";
case F_OFF:
return "off";
case F_ERR:
return "error state";
}
return "?";
}
void emit_fan(global_t *globals, char *header_shown)
{
int loop;
static int *prevs = NULL;
if (!prevs)
{
int bytes = sizeof(int) * globals -> fan_count;
prevs = malloc(bytes);
memset(prevs, 0x00, bytes);
}
for(loop=0; loop<globals -> fan_count; loop++)
{
int rc = read_acpi_fan(loop);
if (rc == ITEM_EXCEED)
break;
if (rc == ALLOC_ERR)
error_exit("reac_acpi_fan(%d) failed\n", loop);
if (prevs[loop] != fans[loop].fan_state)
{
header(header_shown);
printf("fan %s: %s\n", fans[loop].name, fan_state_str(fans[loop].fan_state));
prevs[loop] = fans[loop].fan_state;
}
}
}
|