-
Notifications
You must be signed in to change notification settings - Fork 2
/
bat_tray.c
305 lines (247 loc) · 8.32 KB
/
bat_tray.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "bat_tray.h"
#include "common.h"
#include "defaults.h"
#include <gtk/gtk.h>
#include <stdbool.h>
#include <string.h>
#include <libintl.h>
static GtkStatusIcon* tray;
static char tooltip_text[1024];
static int old_rounded = -1;
static int old_battery_state = -1;
static int bat_num; // Shortcoming: we only detect one battery
static char CHARGE_VALUE_PATH[1024];
static char CHARGE_STATE_PATH[1024];
/***********************************************************************
* Return the battery level percentage
**********************************************************************/
int get_bat_percent()
{
return get_int_value_from_file(CHARGE_VALUE_PATH);
}
/***********************************************************************
* Get battery level percentage, rounded to one of: 0, 20, 40, 60, 80
* or 100. These values match the icon files
**********************************************************************/
int get_bat_percent_rounded()
{
int rounded = 0;
rounded = 20 * (int)((get_bat_percent() + 10) / 20);
/* cap rounded value to safe bounds */
if (rounded < 0)
rounded = 0;
if (rounded > 100)
rounded = 100;
return rounded;
}
/***********************************************************************
* Return number of seconds until battery is fully charged/discharged
**********************************************************************/
long get_bat_seconds_left()
{
int now = 0;
int full = 0;
int rate = 0;
/* FIXME this now/full/rate logic has probably earned its own function.
* Not because of DRY, but just for readability's sake perhaps */
/* Kernel battery info is reported in one of two formats:
* - charge units
* - energy units
* Charge Units | Energy Units
* ------------------+---------------
* charge_now (µAh) | energy_now (µWh)
* charge_full (µAh) | energy_full (µWh)
* current_now (µA) | power_now (µW)
*
* Simple arithmetic produces the desired time value (time to fill, time
* to empty).
*
* If a valid (non-negative) value from energy_now is received, we assume
* all values are in energy units and proceed from there. If an invalid
* value is received, we assume all values are in charge units etc.
*/
/* Attempt to read current ENERGY level */
now = get_int_value_from_filef(POWERDIR"BAT%d/energy_now", bat_num);
/* FIXME battery's full charge/energy is only required when charging */
/* Energy units or charge units */
if (now >= 0)
{
full = get_int_value_from_filef(POWERDIR"BAT%d/energy_full", bat_num);
rate = get_int_value_from_filef(POWERDIR"BAT%d/power_now", bat_num);
} else {
full = get_int_value_from_filef(POWERDIR"BAT%d/charge_full", bat_num);
now = get_int_value_from_filef(POWERDIR"BAT%d/charge_now", bat_num);
rate = get_int_value_from_filef(POWERDIR"BAT%d/current_now", bat_num);
}
/* Note the '<=' for rate (we divide by rate) */
if (full < 0 || now < 0 || rate <= 0)
return -1;
switch (get_battery_state())
{
case STATE_CHARGING:
return (3600*(long long)(full - now))/rate;
break;
case STATE_DISCHARGING:
return (3600*(long long)now)/rate;
break;
default:
return -1;
}
}
/***********************************************************************
* Shows/updates the battery tray tooltip
**********************************************************************/
static gboolean show_tooltip(GtkStatusIcon* status_icon, gint x, gint y, gboolean keyboard_mode, GtkTooltip* tooltip, gpointer data)
{
gtk_tooltip_set_text(tooltip, tooltip_text);
return true;
}
/***********************************************************************
* Updates the battery tray tooltip's cached text
**********************************************************************/
static void update_tooltip_cache()
{
char msg[512];
char time_left[64];
int seconds_left = get_bat_seconds_left();
if (seconds_left < 0)
snprintf(time_left, sizeof(time_left), _("Unknown time left"));
else
snprintf(time_left, sizeof(time_left), _("%02d:%02d left"), (int)(seconds_left/3600), (int)((seconds_left%3600)/60));
switch (get_battery_state())
{
case STATE_DISCHARGING:
snprintf(msg, sizeof(msg), _("Discharging (%d%%)\n%s"), get_bat_percent(), time_left);
break;
case STATE_CHARGING:
snprintf(msg, sizeof(msg), _("Charging (%d%%)\n%s"), get_bat_percent(), time_left);
break;
case STATE_CHARGED:
snprintf(msg, sizeof(msg), _("Fully charged") );
break;
default:
snprintf(msg, sizeof(msg), _("Unknown status") );
break;
}
strncpy(tooltip_text, msg, sizeof(tooltip_text));
tooltip_text[sizeof(tooltip_text)-1] = '\0';
}
/***********************************************************************
* Updates the battery tray icon based upon battery percent
* Also updates the cached tooltip text
**********************************************************************/
static gboolean update()
{
char icon_file[1024];
int rounded = 0;
int battery_state = 0;
update_tooltip_cache();
/* if charge and status hasn't changed, don't spend time changing icon */
battery_state = get_battery_state();
rounded = get_bat_percent_rounded();
if (rounded == old_rounded && battery_state == old_battery_state) {
debug("Short-circuiting\n");
return true;
}
old_rounded = rounded;
old_battery_state = battery_state;
switch (battery_state)
{
case STATE_DISCHARGING:
debug("discharging\n");
snprintf(icon_file, sizeof(icon_file), "%s/bat-%d.png", DEFAULT_THEME, rounded);
break;
case STATE_CHARGING:
debug("charging\n");
snprintf(icon_file, sizeof(icon_file), "%s/bat-%d-charging.png", DEFAULT_THEME, rounded);
break;
default:
debug("unknown/charged\n");
snprintf(icon_file, sizeof(icon_file), "%s/bat-charged.png", DEFAULT_THEME);
break;
}
gtk_status_icon_set_from_file(tray, icon_file);
return true;
}
/***********************************************************************
* Initialise the tray and related variables
**********************************************************************/
void bat_tray_init()
{
char icon_file[1024];
/* Get the battery number, store it for later */
bat_num = get_bat_num();
/* Set up battery info filenames/paths */
snprintf(CHARGE_VALUE_PATH, sizeof(CHARGE_STATE_PATH), "/sys/class/power_supply/BAT%d/capacity", bat_num);
snprintf(CHARGE_STATE_PATH, sizeof(CHARGE_STATE_PATH), "/sys/class/power_supply/BAT%d/status", bat_num);
tray = gtk_status_icon_new();
snprintf(icon_file, sizeof(icon_file), "%s/bat-charged.png", DEFAULT_THEME);
gtk_status_icon_set_from_file(tray, icon_file);
gtk_status_icon_set_has_tooltip (tray, TRUE);
g_signal_connect(G_OBJECT(tray), "query-tooltip", G_CALLBACK(show_tooltip), NULL);
g_timeout_add(BAT_TRAY_UPDATE_INTERVAL, update, NULL);
/* pre-load cached tooltip text */
update();
}
/***********************************************************************
* Show the battery tray
**********************************************************************/
void bat_tray_show()
{
gtk_status_icon_set_visible(tray, TRUE);
}
/***********************************************************************
* Hide the battery tray
**********************************************************************/
void bat_tray_hide()
{
gtk_status_icon_set_visible(tray, FALSE);
}
/***********************************************************************
* Return the battery state
**********************************************************************/
int get_battery_state()
{
char state[1024];
FILE* fd = NULL;
if (!(fd = fopen(CHARGE_STATE_PATH, "r")))
return STATE_UNKNOWN;
if (!fgets(state, sizeof(state), fd))
{
fclose(fd);
return STATE_UNKNOWN;
}
fclose(fd);
chomp(state);
if (strcmp(state, "Discharging") == 0)
return STATE_DISCHARGING;
if (strcmp(state, "Full") == 0)
return STATE_CHARGED;
if (strcmp(state, "Charging") == 0)
return STATE_CHARGING;
return STATE_UNKNOWN;
}
/***********************************************************************
* Get the number of the first (who has more than one?) battery
* Returns -1 if no battery present
**********************************************************************/
int get_bat_num()
{
FILE* fd = NULL;
char filename[1024];
int i = 0;
for (i = 0; i < 10; i++)
{
snprintf(filename, sizeof(filename), "/sys/class/power_supply/BAT%d/present", i);
if ((fd = fopen(filename, "r")))
{
if (fgetc(fd) == '1')
{
fclose(fd);
return i;
}
fclose(fd);
}
}
return -1;
}