Battery: Handle Linux kernel events

The Kernel sends notifications for AC (un)plug and some
other important power supply events, so that we can
instantly update the widget. Apart from that it sends
notifications for any added or removed power supplies,
so that the battery support can be reinitialized (useful
on systems with removable batteries).
This commit is contained in:
Sebastian Reichel 2015-08-08 05:53:10 +02:00
parent 46a6d2c2ad
commit 0d0b1249c7
2 changed files with 30 additions and 0 deletions

View file

@ -94,6 +94,7 @@ void default_battery();
// freed memory
void cleanup_battery();
void update_battery_tick(void* arg);
int update_battery();
void init_battery();

View file

@ -23,6 +23,7 @@
#include "common.h"
#include "battery.h"
#include "uevent.h"
enum psy_type {
PSY_UNKNOWN,
@ -59,6 +60,28 @@ struct psy_mains {
gboolean online;
};
static void uevent_battery_update() {
update_battery_tick(NULL);
}
static struct uevent_notify psy_change = {
UEVENT_CHANGE,
"power_supply",
NULL,
uevent_battery_update
};
static void uevent_battery_plug() {
printf("reinitialize batteries after HW change\n");
cleanup_battery();
init_battery();
}
static struct uevent_notify psy_plug = {
UEVENT_ADD | UEVENT_REMOVE,
"power_supply",
NULL,
uevent_battery_plug
};
#define RETURN_ON_ERROR(err) if(error) { g_error_free(err); return FALSE; }
static GList *batteries = NULL;
@ -173,6 +196,9 @@ static gboolean init_linux_mains(struct psy_mains *ac) {
void battery_os_free() {
GList *l = batteries;
uevent_unregister_notifier(&psy_change);
uevent_unregister_notifier(&psy_plug);
while (l != NULL) {
GList *next = l->next;
struct psy_battery *bat = l->data;
@ -257,6 +283,9 @@ gboolean battery_os_init() {
g_dir_close(directory);
uevent_register_notifier(&psy_change);
uevent_register_notifier(&psy_plug);
return batteries != NULL;
}