update for beta release
[framework/uifw/e17.git] / src / modules / backlight / e_mod_main.c
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(E_Gadcon_Client_Class *client_class);
9 static Evas_Object *_gc_icon(E_Gadcon_Client_Class *client_class, Evas *evas);
10 static const char *_gc_id_new(E_Gadcon_Client_Class *client_class);
11
12 /* and actually define the gadcon class that this module provides (just 1) */
13 static const E_Gadcon_Client_Class _gadcon_class =
14 {
15    GADCON_CLIENT_CLASS_VERSION,
16      "backlight",
17      {
18         _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL, NULL
19      },
20    E_GADCON_CLIENT_STYLE_PLAIN
21 };
22
23 /* actual module specifics */
24 typedef struct _Instance Instance;
25
26 struct _Instance
27 {
28    E_Gadcon_Client *gcc;
29    Evas_Object     *o_backlight, *o_table, *o_slider;
30    E_Gadcon_Popup  *popup;
31    E_Menu          *menu;
32    double           val;
33    Ecore_X_Window   input_win;
34    Ecore_Event_Handler *hand_mouse_down;
35    Ecore_Event_Handler *hand_key_down;
36 };
37
38 static Eina_List *backlight_instances = NULL;
39 static E_Module *backlight_module = NULL;
40 static E_Action *act = NULL;
41
42 static void _backlight_popup_free(Instance *inst);
43
44 static void
45 _backlight_gadget_update(Instance *inst)
46 {
47    Edje_Message_Float msg;
48    
49    msg.val = inst->val;
50    if (msg.val < 0.0) msg.val = 0.0;
51    else if (msg.val > 1.0) msg.val = 1.0;
52    edje_object_message_send(inst->o_backlight, EDJE_MESSAGE_FLOAT, 0, &msg);
53 }
54
55 static void
56 _backlight_input_win_del(Instance *inst)
57 {
58    e_grabinput_release(0, inst->input_win);
59    ecore_x_window_free(inst->input_win);
60    inst->input_win = 0;
61    ecore_event_handler_del(inst->hand_mouse_down);
62    inst->hand_mouse_down = NULL;
63    ecore_event_handler_del(inst->hand_key_down);
64    inst->hand_key_down = NULL;
65 }
66
67 static Eina_Bool
68 _backlight_input_win_mouse_down_cb(void *data, int type __UNUSED__, void *event)
69 {
70    Ecore_Event_Mouse_Button *ev = event;
71    Instance *inst = data;
72    
73    if (ev->window != inst->input_win) return ECORE_CALLBACK_PASS_ON;
74    _backlight_popup_free(inst);
75    return ECORE_CALLBACK_PASS_ON;
76 }
77
78 static Eina_Bool
79 _backlight_input_win_key_down_cb(void *data, int type __UNUSED__, void *event)
80 {
81    Ecore_Event_Key *ev = event;
82    Instance *inst = data;
83    const char *keysym;
84    
85    if (ev->window != inst->input_win) return ECORE_CALLBACK_PASS_ON;
86    
87    keysym = ev->key;
88    if (!strcmp(keysym, "Escape"))
89       _backlight_popup_free(inst);
90    else if ((!strcmp(keysym, "Up")) ||
91             (!strcmp(keysym, "Left")) ||
92             (!strcmp(keysym, "KP_Up")) ||
93             (!strcmp(keysym, "KP_Left")) ||
94             (!strcmp(keysym, "w")) ||
95             (!strcmp(keysym, "d")) ||
96             (!strcmp(keysym, "bracketright")) ||
97             (!strcmp(keysym, "Prior")))
98      {
99         double v = inst->val + 0.1;
100         if (v > 1.0) v = 1.0;
101         e_widget_slider_value_double_set(inst->o_slider, v);
102         inst->val = v;
103         e_backlight_mode_set(inst->gcc->gadcon->zone, E_BACKLIGHT_MODE_NORMAL);
104         e_backlight_level_set(inst->gcc->gadcon->zone, v, 0.0);
105         _backlight_gadget_update(inst);
106      }
107    else if ((!strcmp(keysym, "Down")) ||
108             (!strcmp(keysym, "Right")) ||
109             (!strcmp(keysym, "KP_Down")) ||
110             (!strcmp(keysym, "KP_Right")) ||
111             (!strcmp(keysym, "s")) ||
112             (!strcmp(keysym, "a")) ||
113             (!strcmp(keysym, "bracketleft")) ||
114             (!strcmp(keysym, "Next")))
115      {
116         double v = inst->val - 0.1;
117         if (v < 0.0) v = 0.0;
118         e_widget_slider_value_double_set(inst->o_slider, v);
119         inst->val = v;
120         e_backlight_mode_set(inst->gcc->gadcon->zone, E_BACKLIGHT_MODE_NORMAL);
121         e_backlight_level_set(inst->gcc->gadcon->zone, v, 0.0);
122         _backlight_gadget_update(inst);
123      }
124    else if ((!strcmp(keysym, "0")) ||
125             (!strcmp(keysym, "1")) ||
126             (!strcmp(keysym, "2")) ||
127             (!strcmp(keysym, "3")) ||
128             (!strcmp(keysym, "4")) ||
129             (!strcmp(keysym, "5")) ||
130             (!strcmp(keysym, "6")) ||
131             (!strcmp(keysym, "7")) ||
132             (!strcmp(keysym, "8")) ||
133             (!strcmp(keysym, "9")))
134      {
135         double v = (double)atoi(keysym) / 9.0;
136         e_widget_slider_value_double_set(inst->o_slider, v);
137         inst->val = v;
138         e_backlight_mode_set(inst->gcc->gadcon->zone, E_BACKLIGHT_MODE_NORMAL);
139         e_backlight_level_set(inst->gcc->gadcon->zone, v, 0.0);
140         _backlight_gadget_update(inst);
141      }
142    else
143      {
144         Eina_List *l;
145         E_Config_Binding_Key *bind;
146         E_Binding_Modifier mod;
147         
148         for (l = e_config->key_bindings; l; l = l->next)
149           {
150              bind = l->data;
151              
152              if (bind->action && strcmp(bind->action, "backlight")) continue;
153              
154              mod = 0;
155              
156              if (ev->modifiers & ECORE_EVENT_MODIFIER_SHIFT)
157                 mod |= E_BINDING_MODIFIER_SHIFT;
158              if (ev->modifiers & ECORE_EVENT_MODIFIER_CTRL)
159                 mod |= E_BINDING_MODIFIER_CTRL;
160              if (ev->modifiers & ECORE_EVENT_MODIFIER_ALT)
161                 mod |= E_BINDING_MODIFIER_ALT;
162              if (ev->modifiers & ECORE_EVENT_MODIFIER_WIN)
163                 mod |= E_BINDING_MODIFIER_WIN;
164              
165              if (bind->key && (!strcmp(bind->key, ev->keyname)) &&
166                  ((bind->modifiers == (int)mod) || (bind->any_mod)))
167                {
168                   _backlight_popup_free(inst);
169                   break;
170                }
171           }
172      }
173    return ECORE_CALLBACK_PASS_ON;
174 }
175
176 static void
177 _backlight_input_win_new(Instance *inst)
178 {
179    Ecore_X_Window_Configure_Mask mask;
180    Ecore_X_Window w, popup_w;
181    E_Manager *man;
182    
183    man = inst->gcc->gadcon->zone->container->manager;
184    
185    w = ecore_x_window_input_new(man->root, 0, 0, man->w, man->h);
186    mask = (ECORE_X_WINDOW_CONFIGURE_MASK_STACK_MODE |
187            ECORE_X_WINDOW_CONFIGURE_MASK_SIBLING);
188    popup_w = inst->popup->win->evas_win;
189    ecore_x_window_configure(w, mask, 0, 0, 0, 0, 0, popup_w,
190                             ECORE_X_WINDOW_STACK_BELOW);
191    ecore_x_window_show(w);
192    
193    inst->hand_mouse_down =
194       ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN,
195                               _backlight_input_win_mouse_down_cb, inst);
196    inst->hand_key_down =
197       ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
198                               _backlight_input_win_key_down_cb, inst);
199    inst->input_win = w;
200    e_grabinput_get(0, 0, inst->input_win);
201 }
202
203 static void
204 _backlight_settings_cb(void *d1, void *d2 __UNUSED__)
205 {
206    Instance *inst = d1;
207    e_configure_registry_call("screen/power_management",
208                              inst->gcc->gadcon->zone->container, NULL);
209    _backlight_popup_free(inst);
210 }
211
212 static void
213 _slider_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
214 {
215    Instance *inst = data;
216    e_backlight_mode_set(inst->gcc->gadcon->zone, E_BACKLIGHT_MODE_NORMAL);
217    e_backlight_level_set(inst->gcc->gadcon->zone, inst->val, 0.0);
218    _backlight_gadget_update(inst);
219 }
220
221 static void
222 _backlight_popup_new(Instance *inst)
223 {
224    Evas *evas;
225    Evas_Object *o;
226    
227    if (inst->popup) return;
228
229    e_backlight_update();
230    e_backlight_mode_set(inst->gcc->gadcon->zone, E_BACKLIGHT_MODE_NORMAL);
231    inst->val = e_backlight_level_get(inst->gcc->gadcon->zone);
232    _backlight_gadget_update(inst);
233    
234    inst->popup = e_gadcon_popup_new(inst->gcc);
235    evas = inst->popup->win->evas;
236    
237    inst->o_table = e_widget_table_add(evas, 0);
238
239    o = e_widget_slider_add(evas, 0, 0, NULL, 0.0, 1.0, 0.05, 0, &(inst->val), NULL, 100);
240    evas_object_smart_callback_add(o, "changed", _slider_cb, inst);
241    inst->o_slider = o;
242    e_widget_table_object_align_append(inst->o_table, o, 
243                                       0, 0, 1, 1, 0, 0, 0, 0, 0.5, 0.5);
244    
245    o = e_widget_button_add(evas, NULL, "preferences-system",
246                            _backlight_settings_cb, inst, NULL);
247    e_widget_table_object_align_append(inst->o_table, o, 
248                                       0, 1, 1, 1, 0, 0, 0, 0, 0.5, 1.0);
249    
250    e_gadcon_popup_content_set(inst->popup, inst->o_table);
251    e_gadcon_popup_show(inst->popup);
252    _backlight_input_win_new(inst);
253 }
254
255 static void
256 _backlight_popup_free(Instance *inst)
257 {
258    if (!inst->popup) return;
259    if (inst->popup)
260      {
261         _backlight_input_win_del(inst);
262         e_object_del(E_OBJECT(inst->popup));
263         inst->popup = NULL;
264      }
265 }
266
267 static void
268 _backlight_menu_cb_post(void *data, E_Menu *menu __UNUSED__)
269 {
270    Instance *inst = data;
271    if ((!inst) || (!inst->menu))
272       return;
273    if (inst->menu)
274      {
275         e_object_del(E_OBJECT(inst->menu));
276         inst->menu = NULL;
277      }
278 }
279
280 static void
281 _backlight_menu_cb_cfg(void *data, E_Menu *menu __UNUSED__, E_Menu_Item *mi __UNUSED__)
282 {
283    Instance *inst = data;
284
285    _backlight_popup_free(inst);
286    e_configure_registry_call("screen/power_management",
287                              inst->gcc->gadcon->zone->container, NULL);
288 }
289
290 static void
291 _backlight_cb_mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
292 {
293    Instance *inst = data;
294    Evas_Event_Mouse_Down *ev = event;
295    
296    if (ev->button == 1)
297      {
298         if (inst->popup) _backlight_popup_free(inst);
299         else _backlight_popup_new(inst);
300      }
301    else if ((ev->button == 3) && (!inst->menu))
302      {
303         E_Zone *zone;
304         E_Menu *m;
305         E_Menu_Item *mi;
306         int x, y;
307         
308         zone = e_util_zone_current_get(e_manager_current_get());
309         
310         m = e_menu_new();
311         
312         mi = e_menu_item_new(m);
313         e_menu_item_label_set(mi, _("Settings"));
314         e_util_menu_item_theme_icon_set(mi, "configure");
315         e_menu_item_callback_set(mi, _backlight_menu_cb_cfg, inst);
316         
317         m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
318         e_menu_post_deactivate_callback_set(m, _backlight_menu_cb_post, inst);
319         inst->menu = m;
320         
321         e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon, &x, &y, NULL, NULL);
322         e_menu_activate_mouse(m, zone, x + ev->output.x, y + ev->output.y,
323                               1, 1, E_MENU_POP_DIRECTION_AUTO, ev->timestamp);
324         evas_event_feed_mouse_up(inst->gcc->gadcon->evas, ev->button,
325                                  EVAS_BUTTON_NONE, ev->timestamp, NULL);
326      }
327 }
328
329 static void
330 _backlight_level_decrease(Instance *inst)
331 {
332    double v = inst->val - 0.1;
333    if (v < 0.0) v = 0.0;
334    e_backlight_level_set(inst->gcc->gadcon->zone, v, 0.0);
335 }
336
337 static void
338 _backlight_level_increase(Instance *inst)
339 {
340    double v = inst->val + 0.1;
341    if (v > 1.0) v = 1.0;
342    e_backlight_level_set(inst->gcc->gadcon->zone, v, 0.0);
343 }
344
345 static void
346 _backlight_cb_mouse_wheel(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
347 {
348    Eina_List *l;
349    Evas_Event_Mouse_Wheel *ev = event;
350    double v;
351    Instance *inst = data;
352
353    inst->val = e_backlight_level_get(inst->gcc->gadcon->zone);
354    if (ev->z > 0)
355      _backlight_level_decrease(inst);
356    else if (ev->z < 0)
357      _backlight_level_increase(inst);
358    v = inst->val;
359
360    EINA_LIST_FOREACH(backlight_instances, l, inst)
361      {
362         inst->val = v;
363         _backlight_gadget_update(inst);
364      }
365 }
366
367 static E_Gadcon_Client *
368 _gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
369 {
370    Evas_Object *o;
371    E_Gadcon_Client *gcc;
372    Instance *inst;
373    
374    inst = E_NEW(Instance, 1);
375
376    o = edje_object_add(gc->evas);
377    e_theme_edje_object_set(o, "base/theme/modules/backlight",
378                            "e/modules/backlight/main");
379    evas_object_show(o);
380    
381    gcc = e_gadcon_client_new(gc, name, id, style, o);
382    gcc->data = inst;
383    
384    inst->gcc = gcc;
385    inst->o_backlight = o;
386
387    e_backlight_update();
388    inst->val = e_backlight_level_get(inst->gcc->gadcon->zone);
389    _backlight_gadget_update(inst);
390    
391    evas_object_event_callback_add(inst->o_backlight, 
392                                   EVAS_CALLBACK_MOUSE_DOWN,
393                                   _backlight_cb_mouse_down,
394                                   inst);
395    evas_object_event_callback_add(inst->o_backlight, 
396                                   EVAS_CALLBACK_MOUSE_WHEEL,
397                                   _backlight_cb_mouse_wheel,
398                                   inst);
399    
400    backlight_instances = eina_list_append(backlight_instances, inst);
401    return gcc;
402 }
403
404 static void
405 _gc_shutdown(E_Gadcon_Client *gcc)
406 {
407    Instance *inst;
408    
409    inst = gcc->data;
410    if (inst->menu)
411      {
412         e_menu_post_deactivate_callback_set(inst->menu, NULL, NULL);
413         _backlight_input_win_del(inst);
414         e_object_del(E_OBJECT(inst->menu));
415         inst->menu = NULL;
416      }
417    _backlight_popup_free(inst);
418    backlight_instances = eina_list_remove(backlight_instances, inst);
419    evas_object_del(inst->o_backlight);
420    free(inst);
421 }
422
423 static void
424 _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient __UNUSED__)
425 {
426    Instance *inst;
427    Evas_Coord mw, mh;
428    
429    inst = gcc->data;
430    mw = 0, mh = 0;
431    edje_object_size_min_get(inst->o_backlight, &mw, &mh);
432    if ((mw < 1) || (mh < 1))
433      edje_object_size_min_calc(inst->o_backlight, &mw, &mh);
434    if (mw < 4) mw = 4;
435    if (mh < 4) mh = 4;
436    e_gadcon_client_aspect_set(gcc, mw, mh);
437    e_gadcon_client_min_size_set(gcc, mw, mh);
438 }
439
440 static const char *
441 _gc_label(E_Gadcon_Client_Class *client_class __UNUSED__)
442 {
443    return _("Backlight");
444 }
445
446 static Evas_Object *
447 _gc_icon(E_Gadcon_Client_Class *client_class __UNUSED__, Evas *evas)
448 {
449    Evas_Object *o;
450    char buf[4096];
451    
452    o = edje_object_add(evas);
453    snprintf(buf, sizeof(buf), "%s/e-module-backlight.edj",
454             e_module_dir_get(backlight_module));
455    edje_object_file_set(o, buf, "icon");
456    return o;
457 }
458
459 static const char *
460 _gc_id_new(E_Gadcon_Client_Class *client_class __UNUSED__)
461 {
462    return _gadcon_class.name;
463 }
464
465 static void
466 _e_mod_action_cb(E_Object *obj __UNUSED__,
467                  const char *params __UNUSED__)
468 {
469    Eina_List *l;
470    Instance *inst;
471    
472    EINA_LIST_FOREACH(backlight_instances, l, inst)
473      {
474         if (inst->popup) _backlight_popup_free(inst);
475         else _backlight_popup_new(inst);
476      }
477 }
478
479 /* module setup */
480 EAPI E_Module_Api e_modapi =
481 {
482    E_MODULE_API_VERSION,
483      "Backlight"
484 };
485
486 EAPI void *
487 e_modapi_init(E_Module *m)
488 {
489    backlight_module = m;
490    e_gadcon_provider_register(&_gadcon_class);
491    act = e_action_add("backlight");
492    if (act)
493      {
494         act->func.go = _e_mod_action_cb;
495         e_action_predef_name_set(_("Screen"), _("Backlight Controls"), "backlight", NULL, NULL, 0);
496      }
497    return m;
498 }
499
500 EAPI int
501 e_modapi_shutdown(E_Module *m __UNUSED__)
502 {
503    if (act)
504      {
505         e_action_predef_name_del(_("Screen"), _("Backlight Controls"));
506         e_action_del("backlight");
507         act = NULL;
508      }
509    backlight_module = NULL;
510    e_gadcon_provider_unregister(&_gadcon_class);
511    return 1;
512 }
513
514 EAPI int
515 e_modapi_save(E_Module *m __UNUSED__)
516 {
517    return 1;
518 }