1 #include "e.h"
2 #include "e_mod_main.h"
3 
4 /* gadcon requirements */
5 static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style);
6 static void             _gc_shutdown(E_Gadcon_Client *gcc);
7 static void             _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
8 static const char      *_gc_label(const E_Gadcon_Client_Class *client_class);
9 static Evas_Object     *_gc_icon(const E_Gadcon_Client_Class *client_class, Evas *evas);
10 static const char      *_gc_id_new(const E_Gadcon_Client_Class *client_class);
11 
12 Eina_List *device_batteries;
13 Eina_List *device_ac_adapters;
14 double init_time;
15 
16 /* and actually define the gadcon class that this module provides (just 1) */
17 static const E_Gadcon_Client_Class _gadcon_class =
18 {
19    GADCON_CLIENT_CLASS_VERSION,
20    "battery",
21    {
22       _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL, NULL
23    },
24    E_GADCON_CLIENT_STYLE_PLAIN
25 };
26 
27 /* actual module specifics */
28 typedef struct _Instance Instance;
29 
30 struct _Instance
31 {
32    E_Gadcon_Client *gcc;
33    Evas_Object     *o_battery;
34    Evas_Object     *popup_battery;
35    E_Gadcon_Popup  *warning;
36    unsigned int     notification_id;
37 };
38 
39 static void      _battery_update(int full, int time_left, int time_full, Eina_Bool have_battery, Eina_Bool have_power);
40 static Eina_Bool _battery_cb_exe_data(void *data, int type, void *event);
41 static Eina_Bool _battery_cb_exe_del(void *data, int type, void *event);
42 static void      _button_cb_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
43 static void      _battery_face_level_set(Evas_Object *battery, double level);
44 static void      _battery_face_time_set(Evas_Object *battery, int time);
45 static void      _battery_face_cb_menu_powermanagement(void *data, E_Menu *m, E_Menu_Item *mi);
46 static void      _battery_face_cb_menu_configure(void *data, E_Menu *m, E_Menu_Item *mi);
47 
48 static Eina_Bool _battery_cb_warning_popup_timeout(void *data);
49 static void      _battery_cb_warning_popup_hide(void *data, Evas *e, Evas_Object *obj, void *event);
50 static void      _battery_warning_popup_destroy(Instance *inst);
51 static void      _battery_warning_popup(Instance *inst, int time, double percent);
52 
53 static Eina_Bool _powersave_cb_config_update(void *data, int type, void *event);
54 
55 static E_Config_DD *conf_edd = NULL;
56 static Ecore_Event_Handler *_handler = NULL;
57 
58 Config *battery_config = NULL;
59 
60 static E_Gadcon_Client *
_gc_init(E_Gadcon * gc,const char * name,const char * id,const char * style)61 _gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
62 {
63    Evas_Object *o;
64    E_Gadcon_Client *gcc;
65    Instance *inst;
66 
67    battery_config->full = -2;
68    battery_config->time_left = -2;
69    battery_config->time_full = -2;
70    battery_config->have_battery = -2;
71    battery_config->have_power = -2;
72 
73    inst = E_NEW(Instance, 1);
74 
75    o = edje_object_add(gc->evas);
76    e_theme_edje_object_set(o, "base/theme/modules/battery",
77                            "e/modules/battery/main");
78 
79    gcc = e_gadcon_client_new(gc, name, id, style, o);
80    gcc->data = inst;
81 
82    inst->gcc = gcc;
83    inst->o_battery = o;
84    inst->warning = NULL;
85    inst->popup_battery = NULL;
86 
87    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN,
88                                   _button_cb_mouse_down, inst);
89    battery_config->instances =
90      eina_list_append(battery_config->instances, inst);
91    _battery_config_updated();
92 
93    return gcc;
94 }
95 
96 static void
_gc_shutdown(E_Gadcon_Client * gcc)97 _gc_shutdown(E_Gadcon_Client *gcc)
98 {
99    Instance *inst;
100 
101    inst = gcc->data;
102    if (battery_config)
103      battery_config->instances =
104        eina_list_remove(battery_config->instances, inst);
105    evas_object_del(inst->o_battery);
106    if (inst->warning)
107      {
108         e_object_del(E_OBJECT(inst->warning));
109         inst->popup_battery = NULL;
110      }
111    E_FREE(inst);
112 }
113 
114 static void
_gc_orient(E_Gadcon_Client * gcc,E_Gadcon_Orient orient EINA_UNUSED)115 _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient EINA_UNUSED)
116 {
117    Instance *inst;
118    Evas_Coord mw, mh, mxw, mxh;
119 
120    inst = gcc->data;
121    mw = 0, mh = 0;
122    edje_object_size_min_get(inst->o_battery, &mw, &mh);
123    edje_object_size_max_get(inst->o_battery, &mxw, &mxh);
124    if ((mw < 1) || (mh < 1))
125      edje_object_size_min_calc(inst->o_battery, &mw, &mh);
126    if (mw < 4) mw = 4;
127    if (mh < 4) mh = 4;
128    if ((mxw > 0) && (mxh > 0))
129      e_gadcon_client_aspect_set(gcc, mxw, mxh);
130    e_gadcon_client_min_size_set(gcc, mw, mh);
131 }
132 
133 static const char *
_gc_label(const E_Gadcon_Client_Class * client_class EINA_UNUSED)134 _gc_label(const E_Gadcon_Client_Class *client_class EINA_UNUSED)
135 {
136    return _("Battery");
137 }
138 
139 static Evas_Object *
_gc_icon(const E_Gadcon_Client_Class * client_class EINA_UNUSED,Evas * evas)140 _gc_icon(const E_Gadcon_Client_Class *client_class EINA_UNUSED, Evas *evas)
141 {
142    Evas_Object *o;
143    char buf[4096];
144 
145    o = edje_object_add(evas);
146    snprintf(buf, sizeof(buf), "%s/e-module-battery.edj",
147             e_module_dir_get(battery_config->module));
148    edje_object_file_set(o, buf, "icon");
149    return o;
150 }
151 
152 static const char *
_gc_id_new(const E_Gadcon_Client_Class * client_class)153 _gc_id_new(const E_Gadcon_Client_Class *client_class)
154 {
155    static char buf[4096];
156 
157    snprintf(buf, sizeof(buf), "%s.%d", client_class->name,
158             eina_list_count(battery_config->instances) + 1);
159    return buf;
160 }
161 
162 static void
_button_cb_mouse_down(void * data,Evas * e,Evas_Object * obj,void * event_info)163 _button_cb_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
164 {
165    Instance *inst;
166    Evas_Event_Mouse_Down *ev;
167 
168    inst = data;
169    ev = event_info;
170    if (ev->button == 3)
171      {
172         E_Menu *m;
173         E_Menu_Item *mi;
174         int cx, cy;
175 
176         m = e_menu_new();
177         mi = e_menu_item_new(m);
178         e_menu_item_label_set(mi, _("Settings"));
179         e_util_menu_item_theme_icon_set(mi, "configure");
180         e_menu_item_callback_set(mi, _battery_face_cb_menu_configure, NULL);
181         if (e_configure_registry_exists("advanced/powermanagement"))
182           {
183              mi = e_menu_item_new(m);
184              e_menu_item_label_set(mi, _("Power Management Timing"));
185              e_util_menu_item_theme_icon_set(mi, "preferences-system-power-management");
186              e_menu_item_callback_set(mi, _battery_face_cb_menu_powermanagement, NULL);
187           }
188 
189         m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
190 
191         e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon,
192                                           &cx, &cy, NULL, NULL);
193         e_menu_activate_mouse(m,
194                               e_zone_current_get(),
195                               cx + ev->output.x, cy + ev->output.y, 1, 1,
196                               E_MENU_POP_DIRECTION_DOWN, ev->timestamp);
197         evas_event_feed_mouse_up(inst->gcc->gadcon->evas, ev->button,
198                                  EVAS_BUTTON_NONE, ev->timestamp, NULL);
199      }
200    if (ev->button == 1)
201      _battery_cb_warning_popup_hide(data, e, obj, event_info);
202 }
203 
204 static void
_battery_face_level_set(Evas_Object * battery,double level)205 _battery_face_level_set(Evas_Object *battery, double level)
206 {
207    Edje_Message_Float msg;
208    char buf[256];
209 
210    snprintf(buf, sizeof(buf), "%i", (int)(level * 100.0));
211    edje_object_part_text_set(battery, "e.text.reading", buf);
212 
213    if (level < 0.0) level = 0.0;
214    else if (level > 1.0)
215      level = 1.0;
216    msg.val = level;
217    edje_object_message_send(battery, EDJE_MESSAGE_FLOAT, 1, &msg);
218 }
219 
220 static void
_battery_face_time_set(Evas_Object * battery,int t)221 _battery_face_time_set(Evas_Object *battery, int t)
222 {
223    char buf[256];
224    int hrs, mins;
225 
226    if (t < 0) return;
227 
228    hrs = (t / 3600);
229    mins = ((t) / 60 - (hrs * 60));
230    if (mins < 0) mins = 0;
231    snprintf(buf, sizeof(buf), "%i:%02i", hrs, mins);
232    edje_object_part_text_set(battery, "e.text.time", buf);
233 }
234 
235 static void
_battery_face_cb_menu_powermanagement(void * data EINA_UNUSED,E_Menu * m EINA_UNUSED,E_Menu_Item * mi EINA_UNUSED)236 _battery_face_cb_menu_powermanagement(void *data EINA_UNUSED, E_Menu *m EINA_UNUSED, E_Menu_Item *mi EINA_UNUSED)
237 {
238    e_configure_registry_call("advanced/powermanagement", NULL, NULL);
239 }
240 
241 static void
_battery_face_cb_menu_configure(void * data EINA_UNUSED,E_Menu * m EINA_UNUSED,E_Menu_Item * mi EINA_UNUSED)242 _battery_face_cb_menu_configure(void *data EINA_UNUSED, E_Menu *m EINA_UNUSED, E_Menu_Item *mi EINA_UNUSED)
243 {
244    if (!battery_config) return;
245    if (battery_config->config_dialog) return;
246    e_int_config_battery_module(NULL, NULL);
247 }
248 
249 Battery *
_battery_battery_find(const char * udi)250 _battery_battery_find(const char *udi)
251 {
252    Eina_List *l;
253    Battery *bat;
254    EINA_LIST_FOREACH(device_batteries, l, bat)
255      { /* these are always stringshared */
256        if (udi == bat->udi) return bat;
257      }
258 
259    return NULL;
260 }
261 
262 Ac_Adapter *
_battery_ac_adapter_find(const char * udi)263 _battery_ac_adapter_find(const char *udi)
264 {
265    Eina_List *l;
266    Ac_Adapter *ac;
267    EINA_LIST_FOREACH(device_ac_adapters, l, ac)
268      { /* these are always stringshared */
269        if (udi == ac->udi) return ac;
270      }
271 
272    return NULL;
273 }
274 
275 void
_battery_device_update(void)276 _battery_device_update(void)
277 {
278    Eina_List *l;
279    Battery *bat;
280    Ac_Adapter *ac;
281    int full = -1;
282    int time_left = -1;
283    int time_full = -1;
284    int have_battery = 0;
285    int have_power = 0;
286    int charging = 0;
287 
288    int batnum = 0;
289    int acnum = 0;
290 
291    EINA_LIST_FOREACH(device_ac_adapters, l, ac)
292      {
293         if (ac->present)
294           {
295              acnum++;
296              have_power = 1;
297           }
298      }
299 
300    EINA_LIST_FOREACH(device_batteries, l, bat)
301      {
302         if ((!bat->got_prop) || (!bat->technology))
303           continue;
304         have_battery = 1;
305         batnum++;
306         if (bat->charging == 1) have_power = 1;
307         if (full == -1) full = 0;
308         if (bat->percent >= 0)
309           full += bat->percent;
310         else if (bat->last_full_charge > 0)
311           full += (bat->current_charge * 100) / bat->last_full_charge;
312         else if (bat->design_charge > 0)
313           full += (bat->current_charge * 100) / bat->design_charge;
314         if (bat->time_left > 0)
315           {
316              if (time_left < 0) time_left = bat->time_left;
317              else time_left += bat->time_left;
318           }
319         if (bat->time_full > 0)
320           {
321              if (time_full < 0) time_full = bat->time_full;
322              else time_full += bat->time_full;
323           }
324         charging += bat->charging;
325      }
326 
327    if ((device_batteries) && (batnum == 0))
328      return;  /* not ready yet, no properties received for any battery */
329 
330    if (batnum > 0) full /= batnum;
331    if ((full == 100) && have_power)
332      {
333         time_left = -1;
334         time_full = -1;
335      }
336    if (time_left < 1) time_left = -1;
337    if (time_full < 1) time_full = -1;
338 
339    _battery_update(full, time_left, time_full, have_battery, have_power);
340 }
341 
342 void
_battery_config_updated(void)343 _battery_config_updated(void)
344 {
345    Eina_List *l;
346    Instance *inst;
347    char buf[4096];
348    int ok = 0;
349 
350    if (!battery_config) return;
351 
352    if (battery_config->instances)
353      {
354         EINA_LIST_FOREACH(battery_config->instances, l, inst)
355           _battery_warning_popup_destroy(inst);
356      }
357    if (battery_config->batget_exe)
358      {
359         ecore_exe_terminate(battery_config->batget_exe);
360         ecore_exe_free(battery_config->batget_exe);
361         battery_config->batget_exe = NULL;
362      }
363 
364    if ((battery_config->force_mode == UNKNOWN) ||
365        (battery_config->force_mode == SUBSYSTEM))
366      {
367 #ifdef HAVE_EEZE
368         ok = _battery_udev_start();
369 #elif defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || defined __NetBSD__
370         ok = _battery_sysctl_start();
371 #else
372         ok = _battery_upower_start();
373 #endif
374      }
375    if (ok) return;
376 
377    if ((battery_config->force_mode == UNKNOWN) ||
378        (battery_config->force_mode == NOSUBSYSTEM))
379      {
380         snprintf(buf, sizeof(buf), "%s/%s/batget %i",
381                  e_module_dir_get(battery_config->module), MODULE_ARCH,
382                  battery_config->poll_interval);
383 
384         battery_config->batget_exe =
385           ecore_exe_pipe_run(buf, ECORE_EXE_PIPE_READ |
386                              ECORE_EXE_PIPE_READ_LINE_BUFFERED |
387                              ECORE_EXE_NOT_LEADER |
388                              ECORE_EXE_TERM_WITH_PARENT, NULL);
389      }
390 }
391 
392 static Eina_Bool
_battery_cb_warning_popup_timeout(void * data)393 _battery_cb_warning_popup_timeout(void *data)
394 {
395    Instance *inst;
396 
397    inst = data;
398    e_gadcon_popup_hide(inst->warning);
399    battery_config->alert_timer = NULL;
400    return ECORE_CALLBACK_CANCEL;
401 }
402 
403 static void
_battery_cb_warning_popup_hide(void * data,Evas * e EINA_UNUSED,Evas_Object * obj EINA_UNUSED,void * event EINA_UNUSED)404 _battery_cb_warning_popup_hide(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event EINA_UNUSED)
405 {
406    Instance *inst = NULL;
407 
408    inst = (Instance *)data;
409    if ((!inst) || (!inst->warning)) return;
410    e_gadcon_popup_hide(inst->warning);
411 }
412 
413 static void
_battery_warning_popup_destroy(Instance * inst)414 _battery_warning_popup_destroy(Instance *inst)
415 {
416    if (battery_config->alert_timer)
417      {
418         ecore_timer_del(battery_config->alert_timer);
419         battery_config->alert_timer = NULL;
420      }
421    if ((!inst) || (!inst->warning)) return;
422    E_FREE_FUNC(inst->popup_battery, evas_object_del);
423    E_FREE_FUNC(inst->warning, e_object_del);
424 }
425 
426 static void
_battery_warning_popup_cb(void * data,unsigned int id)427 _battery_warning_popup_cb(void *data, unsigned int id)
428 {
429    Instance *inst = data;
430 
431    inst->notification_id = id;
432 }
433 
434 static void
_battery_warning_popup(Instance * inst,int t,double percent)435 _battery_warning_popup(Instance *inst, int t, double percent)
436 {
437    Evas *e = NULL;
438    Evas_Object *popup_bg = NULL;
439    int x, y, w, h;
440 
441    if ((!inst) || (inst->warning)) return;
442 
443    if (battery_config->desktop_notifications)
444      {
445         E_Notification_Notify n;
446         memset(&n, 0, sizeof(E_Notification_Notify));
447         n.app_name = _("Battery");
448         n.replaces_id = 0;
449         n.icon.icon = "battery-low";
450         n.summary = _("Your battery is low!");
451         n.body = _("AC power is recommended.");
452         n.timeout = battery_config->alert_timeout * 1000;
453         e_notification_client_send(&n, _battery_warning_popup_cb, inst);
454         return;
455      }
456 
457    inst->warning = e_gadcon_popup_new(inst->gcc, 0);
458    if (!inst->warning) return;
459 
460    e = e_comp->evas;
461 
462    popup_bg = edje_object_add(e);
463    inst->popup_battery = edje_object_add(e);
464 
465    if ((!popup_bg) || (!inst->popup_battery))
466      {
467         e_object_free(E_OBJECT(inst->warning));
468         inst->warning = NULL;
469         return;
470      }
471 
472    e_theme_edje_object_set(popup_bg, "base/theme/modules/battery/popup",
473                            "e/modules/battery/popup");
474    e_theme_edje_object_set(inst->popup_battery, "base/theme/modules/battery",
475                            "e/modules/battery/main");
476    if (edje_object_part_exists(popup_bg, "e.swallow.battery"))
477      edje_object_part_swallow(popup_bg, "e.swallow.battery", inst->popup_battery);
478    else
479      edje_object_part_swallow(popup_bg, "battery", inst->popup_battery);
480 
481    edje_object_part_text_set(popup_bg, "e.text.title",
482                              _("Your battery is low!"));
483    edje_object_part_text_set(popup_bg, "e.text.label",
484                              _("AC power is recommended."));
485 
486    e_gadcon_popup_content_set(inst->warning, popup_bg);
487    e_gadcon_popup_show(inst->warning);
488 
489    evas_object_geometry_get(inst->warning->o_bg, &x, &y, &w, &h);
490    evas_object_event_callback_add(inst->warning->comp_object, EVAS_CALLBACK_MOUSE_DOWN,
491                                _battery_cb_warning_popup_hide, inst);
492 
493    _battery_face_time_set(inst->popup_battery, t);
494    _battery_face_level_set(inst->popup_battery, percent);
495    edje_object_signal_emit(inst->popup_battery, "e,state,discharging", "e");
496 
497    if ((battery_config->alert_timeout > 0) &&
498        (!battery_config->alert_timer))
499      {
500         battery_config->alert_timer =
501           ecore_timer_loop_add(battery_config->alert_timeout,
502                           _battery_cb_warning_popup_timeout, inst);
503      }
504 }
505 
506 static Eina_Bool
_powersave_cb_config_update(void * data EINA_UNUSED,int type EINA_UNUSED,void * event EINA_UNUSED)507 _powersave_cb_config_update(void *data EINA_UNUSED, int type EINA_UNUSED, void *event EINA_UNUSED)
508 {
509    if (!battery_config->have_battery)
510      e_powersave_mode_set(E_POWERSAVE_MODE_LOW);
511    else
512      {
513         if (battery_config->have_power)
514           e_powersave_mode_set(E_POWERSAVE_MODE_LOW);
515         else if (battery_config->full > 95)
516           e_powersave_mode_set(E_POWERSAVE_MODE_MEDIUM);
517         else if (battery_config->full > 30)
518           e_powersave_mode_set(E_POWERSAVE_MODE_HIGH);
519         else
520           e_powersave_mode_set(E_POWERSAVE_MODE_EXTREME);
521      }
522    return ECORE_CALLBACK_RENEW;
523 }
524 
525 static void
_battery_update(int full,int time_left,int time_full,Eina_Bool have_battery,Eina_Bool have_power)526 _battery_update(int full, int time_left, int time_full, Eina_Bool have_battery, Eina_Bool have_power)
527 {
528    Eina_List *l;
529    Instance *inst;
530    static double debounce_time = 0.0;
531 
532    EINA_LIST_FOREACH(battery_config->instances, l, inst)
533      {
534         if (have_power != battery_config->have_power)
535           {
536              if (have_power && (full < 100))
537                edje_object_signal_emit(inst->o_battery,
538                                        "e,state,charging",
539                                        "e");
540              else
541                {
542                   edje_object_signal_emit(inst->o_battery,
543                                           "e,state,discharging",
544                                           "e");
545                   if (inst->popup_battery)
546                     edje_object_signal_emit(inst->popup_battery,
547                                             "e,state,discharging", "e");
548                }
549           }
550         if (have_battery)
551           {
552              if (battery_config->full != full)
553                {
554                   double val;
555 
556                   if (full >= 100) val = 1.0;
557                   else val = (double)full / 100.0;
558                   _battery_face_level_set(inst->o_battery, val);
559                   if (inst->popup_battery)
560                     _battery_face_level_set(inst->popup_battery, val);
561                }
562           }
563         else
564           {
565              _battery_face_level_set(inst->o_battery, 0.0);
566              edje_object_part_text_set(inst->o_battery,
567                                        "e.text.reading",
568                                        _("N/A"));
569           }
570 
571         if ((time_full < 0) && (time_left != battery_config->time_left))
572           {
573              _battery_face_time_set(inst->o_battery, time_left);
574              if (inst->popup_battery)
575                _battery_face_time_set(inst->popup_battery,
576                                       time_left);
577           }
578         else if ((time_left < 0) && (time_full != battery_config->time_full))
579           {
580              _battery_face_time_set(inst->o_battery, time_full);
581              if (inst->popup_battery)
582                _battery_face_time_set(inst->popup_battery,
583                                       time_full);
584           }
585         if (have_battery &&
586             (!have_power) &&
587             (full < 100) &&
588             (
589               (
590                 (time_left > 0) &&
591                 battery_config->alert &&
592                 ((time_left / 60) <= battery_config->alert)
593               ) ||
594               (
595                 battery_config->alert_p &&
596                 (full <= battery_config->alert_p)
597               )
598             )
599             )
600           {
601              double t;
602 
603              printf("-------------------------------------- bat warn .. why below\n");
604              printf("have_battery = %i\n", (int)have_battery);
605              printf("have_power = %i\n", (int)have_power);
606              printf("full = %i\n", (int)full);
607              printf("time_left = %i\n", (int)time_left);
608              printf("battery_config->alert = %i\n", (int)battery_config->alert);
609              printf("battery_config->alert_p = %i\n", (int)battery_config->alert_p);
610              t = ecore_time_get();
611              if ((t - debounce_time) > 30.0)
612                {
613                   printf("t-debounce = %3.3f\n", (t - debounce_time));
614                   debounce_time = t;
615                   if ((t - init_time) > 5.0)
616                     _battery_warning_popup(inst, time_left, (double)full / 100.0);
617                }
618           }
619         else if (have_power || ((time_left / 60) > battery_config->alert))
620           _battery_warning_popup_destroy(inst);
621         if ((have_battery) && (!have_power) && (full >= 0) &&
622             (battery_config->suspend_below > 0) &&
623             (full < battery_config->suspend_below))
624           {
625              if (battery_config->suspend_method == SUSPEND)
626                e_sys_action_do(E_SYS_SUSPEND, NULL);
627              else if (battery_config->suspend_method == HIBERNATE)
628                e_sys_action_do(E_SYS_HIBERNATE, NULL);
629              else if (battery_config->suspend_method == SHUTDOWN)
630                e_sys_action_do(E_SYS_HALT, NULL);
631           }
632      }
633    if (!have_battery)
634      e_powersave_mode_set(E_POWERSAVE_MODE_LOW);
635    else
636      {
637         if (have_power)
638           e_powersave_mode_set(E_POWERSAVE_MODE_LOW);
639         else if (full > 95)
640           e_powersave_mode_set(E_POWERSAVE_MODE_MEDIUM);
641         else if (full > 30)
642           e_powersave_mode_set(E_POWERSAVE_MODE_HIGH);
643         else
644           e_powersave_mode_set(E_POWERSAVE_MODE_EXTREME);
645      }
646    battery_config->full = full;
647    battery_config->time_left = time_left;
648    battery_config->have_battery = have_battery;
649    battery_config->have_power = have_power;
650 }
651 
652 static Eina_Bool
_battery_cb_exe_data(void * data EINA_UNUSED,int type EINA_UNUSED,void * event)653 _battery_cb_exe_data(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
654 {
655    Ecore_Exe_Event_Data *ev;
656    Instance *inst;
657    Eina_List *l;
658    int i;
659 
660    ev = event;
661    if (ev->exe != battery_config->batget_exe) return ECORE_CALLBACK_PASS_ON;
662    if ((ev->lines) && (ev->lines[0].line))
663      {
664         for (i = 0; ev->lines[i].line; i++)
665           {
666              if (!strcmp(ev->lines[i].line, "ERROR"))
667                EINA_LIST_FOREACH(battery_config->instances, l, inst)
668                  {
669                     edje_object_signal_emit(inst->o_battery,
670                                             "e,state,unknown", "e");
671                     edje_object_part_text_set(inst->o_battery,
672                                               "e.text.reading", _("ERROR"));
673                     edje_object_part_text_set(inst->o_battery,
674                                               "e.text.time", _("ERROR"));
675 
676                     if (inst->popup_battery)
677                       {
678                          edje_object_signal_emit(inst->popup_battery,
679                                                  "e,state,unknown", "e");
680                          edje_object_part_text_set(inst->popup_battery,
681                                                    "e.text.reading", _("ERROR"));
682                          edje_object_part_text_set(inst->popup_battery,
683                                                    "e.text.time", _("ERROR"));
684                       }
685                  }
686              else
687                {
688                   int full = 0;
689                   int time_left = 0;
690                   int time_full = 0;
691                   int have_battery = 0;
692                   int have_power = 0;
693 
694                   if (sscanf(ev->lines[i].line, "%i %i %i %i %i", &full, &time_left, &time_full,
695                              &have_battery, &have_power) == 5)
696                     _battery_update(full, time_left, time_full,
697                                     have_battery, have_power);
698                   else
699                     e_powersave_mode_set(E_POWERSAVE_MODE_LOW);
700                }
701           }
702      }
703    return ECORE_CALLBACK_DONE;
704 }
705 
706 static Eina_Bool
_battery_cb_exe_del(void * data EINA_UNUSED,int type EINA_UNUSED,void * event)707 _battery_cb_exe_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
708 {
709    Ecore_Exe_Event_Del *ev;
710 
711    ev = event;
712    if (ev->exe != battery_config->batget_exe) return ECORE_CALLBACK_PASS_ON;
713    battery_config->batget_exe = NULL;
714    return ECORE_CALLBACK_PASS_ON;
715 }
716 
717 /* module setup */
718 E_API E_Module_Api e_modapi =
719 {
720    E_MODULE_API_VERSION, "Battery"
721 };
722 
723 E_API void *
e_modapi_init(E_Module * m)724 e_modapi_init(E_Module *m)
725 {
726    char buf[4096];
727 
728    conf_edd = E_CONFIG_DD_NEW("Battery_Config", Config);
729 #undef T
730 #undef D
731 #define T Config
732 #define D conf_edd
733    E_CONFIG_VAL(D, T, poll_interval, INT);
734    E_CONFIG_VAL(D, T, alert, INT);
735    E_CONFIG_VAL(D, T, alert_p, INT);
736    E_CONFIG_VAL(D, T, alert_timeout, INT);
737    E_CONFIG_VAL(D, T, suspend_below, INT);
738    E_CONFIG_VAL(D, T, force_mode, INT);
739 #if defined HAVE_EEZE || defined __OpenBSD__ || defined __NetBSD__
740    E_CONFIG_VAL(D, T, fuzzy, INT);
741 #endif
742    E_CONFIG_VAL(D, T, desktop_notifications, INT);
743 
744    battery_config = e_config_domain_load("module.battery", conf_edd);
745    if (!battery_config)
746      {
747         battery_config = E_NEW(Config, 1);
748         battery_config->poll_interval = 512;
749         battery_config->alert = 30;
750         battery_config->alert_p = 10;
751         battery_config->alert_timeout = 0;
752         battery_config->suspend_below = 0;
753         battery_config->force_mode = 0;
754 #if defined HAVE_EEZE || defined __OpenBSD__ || defined __NetBSD__
755         battery_config->fuzzy = 0;
756 #endif
757         battery_config->desktop_notifications = 0;
758      }
759    E_CONFIG_LIMIT(battery_config->poll_interval, 4, 4096);
760    E_CONFIG_LIMIT(battery_config->alert, 0, 60);
761    E_CONFIG_LIMIT(battery_config->alert_p, 0, 100);
762    E_CONFIG_LIMIT(battery_config->alert_timeout, 0, 300);
763    E_CONFIG_LIMIT(battery_config->suspend_below, 0, 50);
764    E_CONFIG_LIMIT(battery_config->force_mode, 0, 2);
765    E_CONFIG_LIMIT(battery_config->desktop_notifications, 0, 1);
766 
767    battery_config->module = m;
768    battery_config->full = -2;
769    battery_config->time_left = -2;
770    battery_config->time_full = -2;
771    battery_config->have_battery = -2;
772    battery_config->have_power = -2;
773 
774    battery_config->batget_data_handler =
775      ecore_event_handler_add(ECORE_EXE_EVENT_DATA,
776                              _battery_cb_exe_data, NULL);
777    battery_config->batget_del_handler =
778      ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
779                              _battery_cb_exe_del, NULL);
780    _handler = ecore_event_handler_add(E_EVENT_POWERSAVE_CONFIG_UPDATE,
781                                       _powersave_cb_config_update, NULL);
782 
783    e_gadcon_provider_register(&_gadcon_class);
784 
785    snprintf(buf, sizeof(buf), "%s/e-module-battery.edj", e_module_dir_get(m));
786    e_configure_registry_category_add("advanced", 80, _("Advanced"), NULL,
787                                      "preferences-advanced");
788    e_configure_registry_item_add("advanced/battery", 100, _("Battery Meter"),
789                                  NULL, buf, e_int_config_battery_module);
790 
791    return m;
792 }
793 
794 E_API int
e_modapi_shutdown(E_Module * m EINA_UNUSED)795 e_modapi_shutdown(E_Module *m EINA_UNUSED)
796 {
797    e_configure_registry_item_del("advanced/battery");
798    e_configure_registry_category_del("advanced");
799    e_gadcon_provider_unregister(&_gadcon_class);
800 
801    if (battery_config->alert_timer)
802      ecore_timer_del(battery_config->alert_timer);
803 
804    if (battery_config->batget_exe)
805      {
806         ecore_exe_terminate(battery_config->batget_exe);
807         ecore_exe_free(battery_config->batget_exe);
808         battery_config->batget_exe = NULL;
809      }
810 
811    if (battery_config->batget_data_handler)
812      {
813         ecore_event_handler_del(battery_config->batget_data_handler);
814         battery_config->batget_data_handler = NULL;
815      }
816    if (battery_config->batget_del_handler)
817      {
818         ecore_event_handler_del(battery_config->batget_del_handler);
819         battery_config->batget_del_handler = NULL;
820      }
821 
822    if (battery_config->config_dialog)
823      e_object_del(E_OBJECT(battery_config->config_dialog));
824 
825 #ifdef HAVE_EEZE
826    _battery_udev_stop();
827 #elif defined (__OpenBSD__) || defined (__DragonFly__) || defined (__FreeBSD__) || defined (__NetBSD__)
828    _battery_sysctl_stop();
829 #else
830    _battery_upower_stop();
831 #endif
832 
833    free(battery_config);
834    battery_config = NULL;
835    E_CONFIG_DD_FREE(conf_edd);
836    return 1;
837 }
838 
839 E_API int
e_modapi_save(E_Module * m EINA_UNUSED)840 e_modapi_save(E_Module *m EINA_UNUSED)
841 {
842    e_config_domain_save("module.battery", conf_edd, battery_config);
843    return 1;
844 }
845 
846