all repos — tint2 @ 1a411591422ff71c3c8e902564f07a459911faa1

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/battery/battery.c (raw)

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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**************************************************************************
*
* Tint2 : battery
*
* Copyright (C) 2009 Sebastian Reichel <elektranox@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* or any later version as published by the Free Software Foundation.
*
* 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <pango/pangocairo.h>

#if defined(__OpenBSD__) || defined(__NetBSD__)
#include <machine/apmvar.h>
#include <err.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif

#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

#include "window.h"
#include "server.h"
#include "panel.h"
#include "battery.h"
#include "timer.h"
#include "common.h"

PangoFontDescription *bat1_font_desc;
PangoFontDescription *bat2_font_desc;
struct batstate battery_state;
int battery_enabled;
int percentage_hide;
static timeout* battery_timeout;

static char buf_bat_percentage[10];
static char buf_bat_time[20];

int8_t battery_low_status;
unsigned char battery_low_cmd_send;
char *battery_low_cmd;
char *path_energy_now;
char *path_energy_full;
char *path_current_now;
char *path_status;
int battery_found;

#if defined(__OpenBSD__) || defined(__NetBSD__)
int apm_fd;
#endif

void update_battery_tick(void* arg)
{
	if (!battery_enabled)
		return;

	int old_found = battery_found;
	int old_percentage = battery_state.percentage;
	int16_t old_hours = battery_state.time.hours;
	int8_t old_minutes = battery_state.time.minutes;
	
	if (update_battery() != 0) {
		// Reconfigure
		init_battery();
		// Try again
		update_battery();
	}
	if (old_found == battery_found &&
		old_percentage == battery_state.percentage &&
		old_hours == battery_state.time.hours &&
		old_minutes == battery_state.time.minutes) {
		return;
	}

	int i;
	for (i = 0; i < nb_panel; i++) {
		if (!battery_found && panel1[i].battery.area.on_screen == 1) {
			hide(&panel1[i].battery.area);
			panel_refresh = 1;
		} else if (battery_state.percentage >= percentage_hide && panel1[i].battery.area.on_screen == 1) {
			hide(&panel1[i].battery.area);
			panel_refresh = 1;
		} else if (battery_state.percentage < percentage_hide && panel1[i].battery.area.on_screen == 0) {
			show(&panel1[i].battery.area);
			panel_refresh = 1;
		}
		if (panel1[i].battery.area.on_screen == 1) {
			panel1[i].battery.area.resize = 1;
			panel_refresh = 1;
		}
	}
}

void default_battery()
{
	battery_enabled = 0;
	battery_found = 0;
	percentage_hide = 101;
	battery_low_cmd_send = 0;
	battery_timeout = 0;
	bat1_font_desc = 0;
	bat2_font_desc = 0;
	battery_low_cmd = 0;
	path_energy_now = 0;
	path_energy_full = 0;
	path_current_now = 0;
	path_status = 0;
	battery_state.percentage = 0;
	battery_state.time.hours = 0;
	battery_state.time.minutes = 0;
#if defined(__OpenBSD__) || defined(__NetBSD__)
	apm_fd = -1;
#endif
}

void cleanup_battery()
{
	pango_font_description_free(bat1_font_desc);
	bat1_font_desc = 0;
	pango_font_description_free(bat2_font_desc);
	bat2_font_desc = 0;
	g_free(path_energy_now);
	path_energy_now = 0;
	g_free(path_energy_full);
	path_energy_full = 0;
	g_free(path_current_now);
	path_current_now = 0;
	g_free(path_status);
	path_status = 0;
	g_free(battery_low_cmd);
	battery_low_cmd = 0;
	stop_timeout(battery_timeout);
	battery_timeout = 0;
	battery_found = 0;

#if defined(__OpenBSD__) || defined(__NetBSD__)
	if ((apm_fd != -1) && (close(apm_fd) == -1))
		warn("cannot close /dev/apm");
	apm_fd = -1;
#endif
}

void init_battery()
{
	if (!battery_enabled)
		return;
	battery_found = 0;

#if defined(__OpenBSD__) || defined(__NetBSD__)
	if (apm_fd > 0)
		close(apm_fd);
	apm_fd = open("/dev/apm", O_RDONLY);
	if (apm_fd < 0) {
		warn("ERROR: battery applet cannot open /dev/apm.");
		battery_found = 0;
	} else {
		battery_found = 1;
	}
#elif defined(__FreeBSD__)
	// Nothing to do
#else // Linux
	GDir *directory = 0;
	GError *error = NULL;
	const char *entryname;
	char *battery_dir = 0;

	directory = g_dir_open("/sys/class/power_supply", 0, &error);
	if (error) {
		g_error_free(error);
	} else {
		while ((entryname = g_dir_read_name(directory))) {
			if (strncmp(entryname, "AC", 2) == 0)
				continue;

			gchar *path1 = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
			if (g_file_test(path1, G_FILE_TEST_EXISTS)) {
				g_free(path1);
				battery_dir = g_build_filename("/sys/class/power_supply", entryname, NULL);
				break;
			}
			g_free(path1);
		}
	}
	if (directory)
		g_dir_close(directory);
	if (!battery_dir) {
		fprintf(stderr, "ERROR: battery applet cannot find any battery\n");
		battery_found = 0;
	} else {
		battery_found = 1;

		g_free(path_energy_now);
		path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
		if (!g_file_test(path_energy_now, G_FILE_TEST_EXISTS)) {
			g_free(path_energy_now);
			path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
		}
		if (!g_file_test(path_energy_now, G_FILE_TEST_EXISTS)) {
			fprintf(stderr, "ERROR: battery applet cannot find energy_now nor charge_now\n");
			g_free(path_energy_now);
			path_energy_now = NULL;
		}

		g_free(path_energy_full);
		path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
		if (!g_file_test(path_energy_full, G_FILE_TEST_EXISTS)) {
			g_free(path_energy_full);
			path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
		}
		if (!g_file_test(path_energy_full, G_FILE_TEST_EXISTS)) {
			fprintf(stderr, "ERROR: battery applet cannot find energy_now nor charge_now\n");
			g_free(path_energy_full);
			path_energy_full = NULL;
		}

		g_free(path_current_now);
		path_current_now = g_build_filename(battery_dir, "power_now", NULL);
		if (!g_file_test(path_current_now, G_FILE_TEST_EXISTS)) {
			g_free(path_current_now);
			path_current_now = g_build_filename(battery_dir, "current_now", NULL);
		}
		if (!g_file_test(path_current_now, G_FILE_TEST_EXISTS)) {
			fprintf(stderr, "ERROR: battery applet cannot find power_now nor current_now\n");
			g_free(path_current_now);
			path_current_now = NULL;
		}

		g_free(path_status);
		path_status = g_build_filename(battery_dir, "status", NULL);
		if (!g_file_test(path_status, G_FILE_TEST_EXISTS)) {
			fprintf(stderr, "ERROR: battery applet cannot find battery status\n");
			g_free(path_status);
			path_status = NULL;
		}

		g_free(battery_dir);
		battery_dir = NULL;
	}

	if (!path_status) {
		battery_found = 0;
		fprintf(stderr, "ERROR: battery applet cannot find any batteries\n");
	}
#endif

	if (battery_timeout == 0) {
		battery_timeout = add_timeout(10, 10000, update_battery_tick, 0);
	}
}


void init_battery_panel(void *p)
{
	Panel *panel = (Panel*)p;
	Battery *battery = &panel->battery;

	if (!battery_enabled)
		return;

	if (battery->area.bg == 0)
		battery->area.bg = &g_array_index(backgrounds, Background, 0);

	battery->area.parent = p;
	battery->area.panel = p;
	battery->area._draw_foreground = draw_battery;
	battery->area.size_mode = SIZE_BY_CONTENT;
	battery->area._resize = resize_battery;
	battery->area.on_screen = 1;
	battery->area.resize = 1;
}


int update_battery() {
	int64_t energy_now = 0,
			energy_full = 0;
	int seconds = 0;
	int8_t new_percentage = 0;
	int errors = 0;

	battery_state.state = BATTERY_UNKNOWN;

#if defined(__OpenBSD__) || defined(__NetBSD__)
	struct apm_power_info info;
	if (apm_fd > 0 && ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) == 0) {
		// best attempt at mapping to Linux battery states
		switch (info.battery_state) {
		case APM_BATT_CHARGING:
			battery_state.state = BATTERY_CHARGING;
			break;
		default:
			battery_state.state = BATTERY_DISCHARGING;
			break;
		}

		if (info.battery_life == 100)
			battery_state.state = BATTERY_FULL;

		// no mapping for openbsd really
		energy_full = 0;
		energy_now = 0;

		if (info.minutes_left != -1)
			seconds = info.minutes_left * 60;
		else
			seconds = -1;

		new_percentage = info.battery_life;
	} else {
		warn("power update: APM_IOC_GETPOWER");
		errors = 1;
	}
#elif defined(__FreeBSD__)
	int sysctl_out = 0;
	size_t len = sizeof(sysctl_out);

	if (sysctlbyname("hw.acpi.battery.state", &sysctl_out, &len, NULL, 0) == 0) {
		// attemp to map the battery state to Linux
		battery_state.state = BATTERY_UNKNOWN;
		switch(sysctl_out) {
		case 1:
			battery_state.state = BATTERY_DISCHARGING;
			break;
		case 2:
			battery_state.state = BATTERY_CHARGING;
			break;
		default:
			battery_state.state = BATTERY_FULL;
			break;
		}
	} else {
		fprintf(stderr, "power update: no such sysctl");
		errors = 1;
	}

	// no mapping for freebsd
	energy_full = 0;
	energy_now = 0;

	if (sysctlbyname("hw.acpi.battery.time", &sysctl_out, &len, NULL, 0) != 0)
		seconds = -1;
	else
		seconds = sysctl_out * 60;

	// charging or error
	if (seconds < 0)
		seconds = 0;

	if (sysctlbyname("hw.acpi.battery.life", &sysctl_out, &len, NULL, 0) != 0)
		new_percentage = -1;
	else
		new_percentage = sysctl_out;
#else
	FILE *fp = NULL;
	char tmp[25] = "";
	int64_t current_now = 0;
	if (path_status) {
		fp = fopen(path_status, "r");
		if (fp != NULL) {
			if (fgets(tmp, sizeof(tmp), fp)) {
				if (strcasecmp(tmp, "Charging\n") == 0)
					battery_state.state = BATTERY_CHARGING;
				if (strcasecmp(tmp, "Discharging\n") == 0)
					battery_state.state = BATTERY_DISCHARGING;
				if (strcasecmp(tmp, "Full\n") == 0)
					battery_state.state = BATTERY_FULL;
			}
			fclose(fp);
		} else {
			errors = 1;
		}
	} else {
		errors = 1;
	}

	if (path_energy_now) {
		fp = fopen(path_energy_now, "r");
		if (fp != NULL) {
			if (fgets(tmp, sizeof tmp, fp))
				energy_now = atoi(tmp);
			fclose(fp);
		} else {
			errors = 1;
		}
	} else {
		errors = 1;
	}

	if (path_energy_full) {
		fp = fopen(path_energy_full, "r");
		if (fp != NULL) {
			if (fgets(tmp, sizeof tmp, fp))
				energy_full = atoi(tmp);
			fclose(fp);
		} else {
			errors = 1;
		}
	} else {
		errors = 1;
	}

	if (path_current_now) {
		fp = fopen(path_current_now, "r");
		if (fp != NULL) {
			if (fgets(tmp, sizeof tmp, fp))
				current_now = atoi(tmp);
			fclose(fp);
		} else {
			errors = 1;
		}
	} else {
		errors = 1;
	}

	if (current_now > 0) {
		switch (battery_state.state) {
		case BATTERY_CHARGING:
			seconds = 3600 * (energy_full - energy_now) / current_now;
			break;
		case BATTERY_DISCHARGING:
			seconds = 3600 * energy_now / current_now;
			break;
		default:
			seconds = 0;
			break;
		}
	} else {
		seconds = 0;
	}
#endif

	battery_state.time.hours = seconds / 3600;
	seconds -= 3600 * battery_state.time.hours;
	battery_state.time.minutes = seconds / 60;
	seconds -= 60 * battery_state.time.minutes;
	battery_state.time.seconds = seconds;

	if (energy_full > 0)
		new_percentage = ((energy_now <= energy_full ? energy_now : energy_full) * 100) / energy_full;

	if (battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
		tint_exec(battery_low_cmd);
		battery_low_cmd_send = 1;
	}
	if (battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) {
		battery_low_cmd_send = 0;
	}
	battery_state.percentage = new_percentage;

	// clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
	if (battery_state.percentage > 100) {
		battery_state.percentage = 100;
	}

	return errors;
}


void draw_battery (void *obj, cairo_t *c)
{
	Battery *battery = obj;
	PangoLayout *layout;

	layout = pango_cairo_create_layout (c);

	// draw layout
	pango_layout_set_font_description(layout, bat1_font_desc);
	pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
	pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
	pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));

	cairo_set_source_rgba(c, battery->font.color[0], battery->font.color[1], battery->font.color[2], battery->font.alpha);

	pango_cairo_update_layout(c, layout);
	cairo_move_to(c, 0, battery->bat1_posy);
	pango_cairo_show_layout(c, layout);

	pango_layout_set_font_description(layout, bat2_font_desc);
	pango_layout_set_indent(layout, 0);
	pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
	pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);

	pango_cairo_update_layout(c, layout);
	cairo_move_to(c, 0, battery->bat2_posy);
	pango_cairo_show_layout(c, layout);

	g_object_unref(layout);
}


int resize_battery(void *obj)
{
	Battery *battery = obj;
	Panel *panel = battery->area.panel;
	int bat_percentage_height, bat_percentage_width, bat_percentage_height_ink;
	int bat_time_height, bat_time_width, bat_time_height_ink;
	int ret = 0;

	battery->area.redraw = 1;
	
	snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
	if (battery_state.state == BATTERY_FULL) {
		strcpy(buf_bat_time, "Full");
	} else {
		snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
	}
	get_text_size2(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, &bat_percentage_width,
				   panel->area.height, panel->area.width, buf_bat_percentage, strlen(buf_bat_percentage));
	get_text_size2(bat2_font_desc, &bat_time_height_ink, &bat_time_height, &bat_time_width,
				   panel->area.height, panel->area.width, buf_bat_time, strlen(buf_bat_time));

	if (panel_horizontal) {
		int new_size = (bat_percentage_width > bat_time_width) ? bat_percentage_width : bat_time_width;
		new_size += 2 * battery->area.paddingxlr + 2 * battery->area.bg->border.width;
		if (new_size > battery->area.width ||
			new_size < battery->area.width - 2) {
			// we try to limit the number of resize
			battery->area.width = new_size;
			battery->bat1_posy = (battery->area.height - bat_percentage_height - bat_time_height) / 2;
			battery->bat2_posy = battery->bat1_posy + bat_percentage_height;
			ret = 1;
		}
	} else {
		int new_size = bat_percentage_height + bat_time_height +
					   (2 * (battery->area.paddingxlr + battery->area.bg->border.width));
		if (new_size > battery->area.height ||
			new_size < battery->area.height - 2) {
			battery->area.height =  new_size;
			battery->bat1_posy = (battery->area.height - bat_percentage_height - bat_time_height - 2) / 2;
			battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2;
			ret = 1;
		}
	}
	return ret;
}