summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Poole <netstar@gmail.com>2019-11-30 20:46:09 +0000
committerAlastair Poole <netstar@gmail.com>2019-11-30 20:46:09 +0000
commit6b70114c6d19d74374bc7c5b614279a724dce561 (patch)
tree0e941d5b72fb4fddc633225752850baeec11cad0
parent2c5c243b9eab8938c23c6f2a690be01b2d7490d4 (diff)
battery/batman: Various Improvements.
This adds support for multiple batteries on FreeBSD in line with Linux and OpenBSD. Also display the vendor and model where applicable. There are also fixes specific to the sysctl interface. Fixes a potential crash and let's us have 100 batman gadgets if wanted.
-rw-r--r--src/modules/battery/e_mod_sysctl.c5
-rw-r--r--src/modules/sysinfo/batman/batman.c14
-rw-r--r--src/modules/sysinfo/batman/batman.h7
-rw-r--r--src/modules/sysinfo/batman/batman_sysctl.c149
4 files changed, 94 insertions, 81 deletions
diff --git a/src/modules/battery/e_mod_sysctl.c b/src/modules/battery/e_mod_sysctl.c
index 293ee42..ed6ee10 100644
--- a/src/modules/battery/e_mod_sysctl.c
+++ b/src/modules/battery/e_mod_sysctl.c
@@ -126,7 +126,8 @@ _battery_sysctl_start(void)
126# endif 126# endif
127 _battery_sysctl_battery_update(); 127 _battery_sysctl_battery_update();
128 128
129 bat->last_update = ecore_time_get(); 129 if (bat)
130 bat->last_update = ecore_time_get();
130 131
131 return 1; 132 return 1;
132} 133}
@@ -184,6 +185,8 @@ _battery_sysctl_battery_update()
184 ecore_poller_poller_interval_set(bat->poll, 185 ecore_poller_poller_interval_set(bat->poll,
185 battery_config->poll_interval); 186 battery_config->poll_interval);
186# if defined(__OpenBSD__) || defined(__NetBSD__) 187# if defined(__OpenBSD__) || defined(__NetBSD__)
188 charge = 0;
189
187 /* last full capacity */ 190 /* last full capacity */
188 bat->mib[3] = 7; 191 bat->mib[3] = 7;
189 bat->mib[4] = 0; 192 bat->mib[4] = 0;
diff --git a/src/modules/sysinfo/batman/batman.c b/src/modules/sysinfo/batman/batman.c
index 75a6425..9a9858a 100644
--- a/src/modules/sysinfo/batman/batman.c
+++ b/src/modules/sysinfo/batman/batman.c
@@ -98,7 +98,9 @@ _batman_popup_create(Instance *inst)
98 elm_box_pack_end(box, label); 98 elm_box_pack_end(box, label);
99 evas_object_show(label); 99 evas_object_show(label);
100 100
101 if (inst->cfg->batman.have_power && (inst->cfg->batman.full < 99)) 101 if (eina_list_count(batman_device_batteries) == 0)
102 snprintf(buf, sizeof(buf), _("No Battery Found"));
103 else if (inst->cfg->batman.have_power && (inst->cfg->batman.full < 99))
102 snprintf(buf, sizeof(buf), _("Battery Charging")); 104 snprintf(buf, sizeof(buf), _("Battery Charging"));
103 else if (inst->cfg->batman.full >= 99) 105 else if (inst->cfg->batman.full >= 99)
104 snprintf(buf, sizeof(buf), _("Battery Fully Charged")); 106 snprintf(buf, sizeof(buf), _("Battery Fully Charged"));
@@ -112,6 +114,16 @@ _batman_popup_create(Instance *inst)
112 if (!eina_list_count(udis) || !eina_list_data_find(udis, bat->udi)) 114 if (!eina_list_count(udis) || !eina_list_data_find(udis, bat->udi))
113 { 115 {
114 udis = eina_list_append(udis, bat->udi); 116 udis = eina_list_append(udis, bat->udi);
117 if (bat->vendor && bat->vendor[0] && bat->model && bat->model[0])
118 {
119 label = elm_label_add(box);
120 E_EXPAND(label); E_ALIGN(label, 0.5, 0.5);
121 snprintf(buf, sizeof(buf), "%s (%s)", bat->vendor, bat->model);
122 elm_object_text_set(label, buf);
123 elm_box_pack_end(box, label);
124 evas_object_show(label);
125 }
126
115 pbar = elm_progressbar_add(box); 127 pbar = elm_progressbar_add(box);
116 E_EXPAND(pbar); E_FILL(pbar); 128 E_EXPAND(pbar); E_FILL(pbar);
117 elm_progressbar_span_size_set(pbar, 200 * e_scale); 129 elm_progressbar_span_size_set(pbar, 200 * e_scale);
diff --git a/src/modules/sysinfo/batman/batman.h b/src/modules/sysinfo/batman/batman.h
index 68f1d91..31085a8 100644
--- a/src/modules/sysinfo/batman/batman.h
+++ b/src/modules/sysinfo/batman/batman.h
@@ -58,13 +58,6 @@ struct _Battery
58 Eina_Bool got_prop E_BITFIELD; 58 Eina_Bool got_prop E_BITFIELD;
59 Eldbus_Proxy *proxy; 59 Eldbus_Proxy *proxy;
60 int * mib; 60 int * mib;
61#if defined(__FreeBSD__) || defined(__DragonFly__)
62 int * mib_state;
63 int * mib_units;
64 int * mib_time;
65 int batteries;
66 int time_min;
67#endif
68}; 61};
69 62
70struct _Ac_Adapter 63struct _Ac_Adapter
diff --git a/src/modules/sysinfo/batman/batman_sysctl.c b/src/modules/sysinfo/batman/batman_sysctl.c
index 2ef3913..a912c30 100644
--- a/src/modules/sysinfo/batman/batman_sysctl.c
+++ b/src/modules/sysinfo/batman/batman_sysctl.c
@@ -3,6 +3,11 @@
3#include <sys/types.h> 3#include <sys/types.h>
4#include <sys/sysctl.h> 4#include <sys/sysctl.h>
5 5
6#if defined(__FreeBSD__) || defined(__DragonFly__)
7# include <dev/acpica/acpiio.h>
8# include <sys/ioctl.h>
9#endif
10
6#if defined(__OpenBSD__) || defined(__NetBSD__) 11#if defined(__OpenBSD__) || defined(__NetBSD__)
7# include <sys/param.h> 12# include <sys/param.h>
8# include <sys/sensors.h> 13# include <sys/sensors.h>
@@ -25,10 +30,13 @@ _batman_sysctl_start(Instance *inst)
25 struct sensordev snsrdev; 30 struct sensordev snsrdev;
26 size_t sdlen = sizeof(struct sensordev); 31 size_t sdlen = sizeof(struct sensordev);
27 char name[256]; 32 char name[256];
28#elif defined(__FreeBSD__) || defined(__DragonFly__) 33
29 size_t len; 34 if (eina_list_count(batman_device_batteries) != 0)
30#endif 35 {
31#if defined(__OpenBSD__) || defined(__NetBSD__) 36 _batman_sysctl_battery_update(inst);
37 return 1;
38 }
39
32 for (devn = 0;; devn++) 40 for (devn = 0;; devn++)
33 { 41 {
34 mib[2] = devn; 42 mib[2] = devn;
@@ -79,41 +87,49 @@ _batman_sysctl_start(Instance *inst)
79 } 87 }
80 } 88 }
81#elif defined(__FreeBSD__) || defined(__DragonFly__) 89#elif defined(__FreeBSD__) || defined(__DragonFly__)
82 if ((sysctlbyname("hw.acpi.battery.life", NULL, &len, NULL, 0)) != -1) 90 size_t len;
91 int i, count, fd;
92 union acpi_battery_ioctl_arg battio;
93
94 if (eina_list_count(batman_device_batteries) != 0)
83 { 95 {
84 Battery *bat = E_NEW(Battery, 1); 96 _batman_sysctl_battery_update(inst);
85 if (!bat) 97 return 1;
86 return 0;
87 bat->inst = inst;
88 bat->mib = malloc(sizeof(int) * 4);
89 if (!bat->mib) return 0;
90 sysctlnametomib("hw.acpi.battery.life", bat->mib, &len);
91
92 bat->mib_state = malloc(sizeof(int) * 4);
93 if (!bat->mib_state) return 0;
94 sysctlnametomib("hw.acpi.battery.state", bat->mib_state, &len);
95
96 bat->mib_time = malloc(sizeof(int) * 4);
97 if (!bat->mib_time) return 0;
98 sysctlnametomib("hw.acpi.battery.time", bat->mib_time, &len);
99
100 bat->mib_units = malloc(sizeof(int) * 4);
101 if (!bat->mib_units) return 0;
102 sysctlnametomib("hw.acpi.battery.units", bat->mib_units, &len);
103
104 bat->last_update = ecore_time_get();
105 bat->udi = eina_stringshare_add("hw.acpi.battery");
106 bat->technology = eina_stringshare_add("Unknown");
107 bat->model = eina_stringshare_add("Unknown");
108 bat->vendor = eina_stringshare_add("Unknown");
109
110 bat->poll = ecore_poller_add(ECORE_POLLER_CORE,
111 inst->cfg->batman.poll_interval,
112 _batman_sysctl_battery_update_poll, inst);
113
114 batman_device_batteries = eina_list_append(batman_device_batteries, bat);
115 } 98 }
116 99
100 if ((fd = open("/dev/acpi", O_RDONLY)) == -1)
101 return 0;
102
103 if (ioctl(fd, ACPIIO_BATT_GET_UNITS, &count) == -1)
104 {
105 close(fd);
106 return 0;
107 }
108
109 for (i = 0; i < count; i++)
110 {
111 battio.unit = i;
112 if (ioctl(fd, ACPIIO_BATT_GET_BIF, &battio) != -1)
113 {
114 Battery *bat = E_NEW(Battery, 1);
115 if (!bat) return 0;
116
117 bat->inst = inst;
118 bat->last_update = ecore_time_get();
119 bat->last_full_charge = battio.bif.lfcap;
120 bat->model = eina_stringshare_add(battio.bif.model);
121 bat->vendor = eina_stringshare_add(battio.bif.oeminfo);
122 bat->technology = eina_stringshare_add(battio.bif.type);
123 bat->poll = ecore_poller_add(ECORE_POLLER_CORE,
124 inst->cfg->batman.poll_interval,
125 _batman_sysctl_battery_update_poll, inst);
126
127 batman_device_batteries = eina_list_append(batman_device_batteries, bat);
128 }
129 }
130
131 close(fd);
132
117 if ((sysctlbyname("hw.acpi.acline", NULL, &len, NULL, 0)) != -1) 133 if ((sysctlbyname("hw.acpi.acline", NULL, &len, NULL, 0)) != -1)
118 { 134 {
119 Ac_Adapter *ac = E_NEW(Ac_Adapter, 1); 135 Ac_Adapter *ac = E_NEW(Ac_Adapter, 1);
@@ -155,11 +171,6 @@ _batman_sysctl_stop(void)
155 E_FREE_FUNC(bat->model, eina_stringshare_del); 171 E_FREE_FUNC(bat->model, eina_stringshare_del);
156 E_FREE_FUNC(bat->vendor, eina_stringshare_del); 172 E_FREE_FUNC(bat->vendor, eina_stringshare_del);
157 E_FREE_FUNC(bat->poll, ecore_poller_del); 173 E_FREE_FUNC(bat->poll, ecore_poller_del);
158#if defined(__FreeBSD__) || defined(__DragonFly__)
159 E_FREE(bat->mib_state);
160 E_FREE(bat->mib_time);
161 E_FREE(bat->mib_units);
162#endif
163 E_FREE(bat->mib); 174 E_FREE(bat->mib);
164 E_FREE(bat); 175 E_FREE(bat);
165 } 176 }
@@ -189,9 +200,9 @@ _batman_sysctl_battery_update(Instance *inst)
189 struct sensor s; 200 struct sensor s;
190 size_t slen = sizeof(struct sensor); 201 size_t slen = sizeof(struct sensor);
191#elif defined(__FreeBSD__) || defined(__DragonFly__) 202#elif defined(__FreeBSD__) || defined(__DragonFly__)
192 double _time; 203 union acpi_battery_ioctl_arg battio;
193 int value;
194 size_t len; 204 size_t len;
205 int value, fd, i = 0;
195#endif 206#endif
196 EINA_LIST_FOREACH(batman_device_batteries, l, bat) 207 EINA_LIST_FOREACH(batman_device_batteries, l, bat)
197 { 208 {
@@ -284,44 +295,38 @@ _batman_sysctl_battery_update(Instance *inst)
284 } 295 }
285 _batman_device_update(bat->inst); 296 _batman_device_update(bat->inst);
286#elif defined(__FreeBSD__) || defined(__DragonFly__) 297#elif defined(__FreeBSD__) || defined(__DragonFly__)
287 len = sizeof(value);
288 if ((sysctl(bat->mib, 4, &value, &len, NULL, 0)) == -1)
289 {
290 return EINA_FALSE;
291 }
292
293 bat->percent = value;
294
295 _time = ecore_time_get();
296 bat->last_update = _time;
297 298
298 len = sizeof(value); 299 if ((fd = open("/dev/acpi", O_RDONLY)) != -1)
299 if ((sysctl(bat->mib_state, 4, &value, &len, NULL, 0)) == -1)
300 { 300 {
301 return EINA_FALSE; 301 battio.unit = i;
302 }
303 302
304 bat->charging = !value; 303 if (ioctl(fd, ACPIIO_BATT_GET_BATTINFO, &battio) != -1)
305 bat->got_prop = 1; 304 {
305 bat->got_prop = 1;
306 306
307 bat->time_full = -1; 307 bat->percent = battio.battinfo.cap;
308 bat->time_left = -1;
309 308
310 len = sizeof(bat->time_min); 309 if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
311 if ((sysctl(bat->mib_time, 4, &bat->time_min, &len, NULL, 0)) == -1) 310 bat->charging = EINA_TRUE;
312 { 311 else
313 bat->time_min = -1; 312 bat->charging = EINA_FALSE;
314 }
315 313
316 len = sizeof(bat->batteries); 314 bat->time_left = battio.battinfo.min * 60;
317 if ((sysctl(bat->mib_units, 4, &bat->batteries, &len, NULL, 0)) == -1) 315 if (bat->charge_rate > 0)
318 { 316 {
319 bat->batteries = 1; 317 bat->time_full = (bat->last_full_charge - bat->current_charge) / bat->charge_rate;
318 }
319 }
320 else
321 {
322 bat->time_full = bat->time_left = -1;
323 }
324
325 close(fd);
320 } 326 }
321 327
322 if (bat->time_min >= 0) bat->time_left = bat->time_min * 60;
323 if (bat->batteries == 1) bat->time_left = -1;
324 _batman_device_update(inst); 328 _batman_device_update(inst);
329 i++;
325#endif 330#endif
326 } 331 }
327 332