From 1e3f6f3cae356572469cf297e83a21e605f5bec0 Mon Sep 17 00:00:00 2001 From: Alastair Poole Date: Sun, 22 Dec 2019 22:00:18 +0000 Subject: [PATCH] batman: Free battery when final instance removed. The battery status is shared between instances. The only time this should be freed is when only one instance remains. Now multiple instances can be added or removed without hindering the display of sysinfo and batman instances. Inline with recent FreeBSD and OpenBSD fixes. --- src/modules/sysinfo/batman/batman_udev.c | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/modules/sysinfo/batman/batman_udev.c b/src/modules/sysinfo/batman/batman_udev.c index e79c6d236..3281b2a9d 100644 --- a/src/modules/sysinfo/batman/batman_udev.c +++ b/src/modules/sysinfo/batman/batman_udev.c @@ -42,6 +42,9 @@ _batman_udev_stop(Instance *inst) { Ac_Adapter *ac; Battery *bat; + Eina_List *l; + Instance *child; + int bat_num = 0; /* This is a dummy battery we return here. */ if (inst->cfg->batman.have_battery != 1) @@ -54,6 +57,30 @@ _batman_udev_stop(Instance *inst) if (inst->cfg->batman.acwatch) E_FREE_FUNC(inst->cfg->batman.acwatch, eeze_udev_watch_del); + /* If this is NOT the last batman gadget then we return before + * freeing everything. This is NOT optimal but is allows us to have + * many gadgets and share the same data between multiple batman + * gadget instances. The instance will be deleted. + */ + + EINA_LIST_FOREACH(sysinfo_instances, l, child) + { + if (inst->cfg->esm == E_SYSINFO_MODULE_BATMAN || + inst->cfg->esm == E_SYSINFO_MODULE_SYSINFO) + { + if (child != inst) + { + bat_num++; + } + } + } + + /* This is not the last batman gadget. */ + if (bat_num > 0) return; + + /* We have no batman or sysinfo gadgets remaining. We can safely + remove these batteries and adapters. */ + EINA_LIST_FREE(batman_device_ac_adapters, ac) { E_FREE(ac);