summaryrefslogtreecommitdiff
path: root/src/module/e_mod_main.c
blob: 293300ea7f708f83e6d2ab91931eba82e51ac8eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#include <e.h>
#include <Eina.h>
#include <emix.h>
#include "e_mod_main.h"
#include "e_mod_config.h"
#include "translation.h"

#define VOLUME_STEP 10

int _e_emix_log_domain;

/* module requirements */
EAPI E_Module_Api e_modapi =
   {
      E_MODULE_API_VERSION,
      "Emixer"
   };

/* necessary forward delcaration */
static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name,
                                 const char *id, const char *style);
static void             _gc_shutdown(E_Gadcon_Client *gcc);
static void             _gc_orient(E_Gadcon_Client *gcc,
                                   E_Gadcon_Orient orient);
static const char      *_gc_label(const E_Gadcon_Client_Class *client_class);
static Evas_Object     *_gc_icon(const E_Gadcon_Client_Class *client_class,
                                 Evas *evas);
static const char      *_gc_id_new(const E_Gadcon_Client_Class *client_class);

static const E_Gadcon_Client_Class _gadcon_class =
   {
      GADCON_CLIENT_CLASS_VERSION,
      "emix",
      {
         _gc_init, _gc_shutdown,
         _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL,
         e_gadcon_site_is_not_toolbar
      },
      E_GADCON_CLIENT_STYLE_PLAIN
   };

typedef struct _Context Context;
struct _Context
{
   char *theme;
   Ecore_Exe *emixer;
   Ecore_Event_Handler *desklock_handler;
   Ecore_Event_Handler *emix_event_handler;
   const Emix_Sink *sink_default;
   E_Module *module;
   Eina_List *instances;
   E_Menu *menu;
   unsigned int notification_id;

   struct {
      E_Action *incr;
      E_Action *decr;
      E_Action *mute;
   } actions;
};

typedef struct _Instance Instance;
struct _Instance
{
   E_Gadcon_Client *gcc;
   E_Gadcon_Orient orient;

   E_Gadcon_Popup *popup;
   Evas *evas;
   Evas_Object *gadget;
   Evas_Object *list;
   Evas_Object *slider;
   Evas_Object *check;

   Eina_Bool mute;
};

static Context *mixer_context = NULL;

static void
_notify_cb(void *data EINA_UNUSED, unsigned int id)
{
   mixer_context->notification_id = id;
}

static void
_notify(const int val)
{
   E_Notification_Notify n;
   char *icon, buf[56];
   int ret;

   if (!emix_config_notify_get())
     return;

   memset(&n, 0, sizeof(E_Notification_Notify));
   if (val > EMIX_VOLUME_MAX || val < 0)
     return;

   ret = snprintf(buf, (sizeof(buf) - 1), "%s: %d%%", _("New volume"), val);
   if ((ret < 0) || ((unsigned int)ret > sizeof(buf)))
     return;
   //Names are taken from FDO icon naming scheme
   if (val == 0)
     icon = "audio-volume-muted";
   else if ((val > 33) && (val < 66))
     icon = "audio-volume-medium";
   else if (val < 33)
     icon = "audio-volume-low";
   else
     icon = "audio-volume-high";

   n.app_name = _("Emix");
   n.replaces_id = mixer_context->notification_id;
   n.icon.icon = icon;
   n.summary = _("Volume changed");
   n.body = buf;
   n.timeout = 2000;
   e_notification_client_send(&n, _notify_cb, NULL);
}

static void
_mixer_popup_update(Instance *inst, int mute, int vol)
{
   elm_check_state_set(inst->check, !!mute);
   elm_slider_value_set(inst->slider, vol);
}

static void _popup_del(Instance *inst);

static void
_mixer_gadget_update(void)
{
   Edje_Message_Int_Set *msg;
   Instance *inst;
   Eina_List *l;

   EINA_LIST_FOREACH(mixer_context->instances, l, inst)
     {
        msg = alloca(sizeof(Edje_Message_Int_Set) + (2 * sizeof(int)));
        msg->count = 3;

        if (!mixer_context->sink_default)
          {
             msg->val[0] = EINA_FALSE;
             msg->val[1] = 0;
             msg->val[2] = 0;
             if (inst->popup)
               _popup_del(inst);
          }
        else
          {
             int vol = 0;
             unsigned int i = 0;
             for (i = 0; i <
                  mixer_context->sink_default->volume.channel_count; i++)
               vol += mixer_context->sink_default->volume.volumes[i];
             vol /= mixer_context->sink_default->volume.channel_count;
             msg->val[0] = mixer_context->sink_default->mute;
             msg->val[1] = vol;
             msg->val[2] = msg->val[1];
             if (inst->popup)
               _mixer_popup_update(inst, mixer_context->sink_default->mute,
                                    msg->val[1]);
          }
        edje_object_message_send(inst->gadget, EDJE_MESSAGE_INT_SET, 0, msg);
        edje_object_signal_emit(inst->gadget, "e,action,volume,change", "e");
     }
}

static void
_volume_increase_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED)
{
   unsigned int i;
   EINA_SAFETY_ON_NULL_RETURN(mixer_context->sink_default);
   Emix_Volume volume;

   Emix_Sink *s = (Emix_Sink *)mixer_context->sink_default;
   volume.channel_count = s->volume.channel_count;
   volume.volumes = calloc(s->volume.channel_count, sizeof(int));
   for (i = 0; i < volume.channel_count; i++)
     {
        if (s->volume.volumes[i] < EMIX_VOLUME_MAX - VOLUME_STEP)
          volume.volumes[i] = s->volume.volumes[i] + VOLUME_STEP;
        else if (s->volume.volumes[i] < EMIX_VOLUME_MAX)
          volume.volumes[i] = EMIX_VOLUME_MAX;
        else
          volume.volumes[i] = s->volume.volumes[i];
     }

   emix_sink_volume_set(s, volume);
   free(volume.volumes);
}

static void
_volume_decrease_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED)
{
   unsigned int i;
   EINA_SAFETY_ON_NULL_RETURN(mixer_context->sink_default);
   Emix_Volume volume;

   Emix_Sink *s = (Emix_Sink *)mixer_context->sink_default;
   volume.channel_count = s->volume.channel_count;
   volume.volumes = calloc(s->volume.channel_count, sizeof(int));
   for (i = 0; i < volume.channel_count; i++)
     {
        if (s->volume.volumes[i] > VOLUME_STEP)
          volume.volumes[i] = s->volume.volumes[i] - VOLUME_STEP;
        else if (s->volume.volumes[i] < VOLUME_STEP)
          volume.volumes[i] = 0;
        else
          volume.volumes[i] = s->volume.volumes[i];
     }

   emix_sink_volume_set(s, volume);
   free(volume.volumes);
}

static void
_volume_mute_cb(E_Object *obj EINA_UNUSED, const char *params EINA_UNUSED)
{
   EINA_SAFETY_ON_NULL_RETURN(mixer_context->sink_default);

   Emix_Sink *s = (Emix_Sink *)mixer_context->sink_default;
   Eina_Bool mute = !s->mute;
   emix_sink_mute_set(s, mute);
}

static void
_actions_register(void)
{
   mixer_context->actions.incr = e_action_add("volume_increase");
   if (mixer_context->actions.incr)
     {
        mixer_context->actions.incr->func.go = _volume_increase_cb;
        e_action_predef_name_set("EMixer", _("Increase Volume"),
                                 "volume_increase", NULL, NULL, 0);
     }

   mixer_context->actions.decr = e_action_add("volume_decrease");
   if (mixer_context->actions.decr)
     {
        mixer_context->actions.decr->func.go = _volume_decrease_cb;
        e_action_predef_name_set("EMixer", _("Decrease Volume"),
                                 "volume_decrease", NULL, NULL, 0);
     }

   mixer_context->actions.mute = e_action_add("volume_mute");
   if (mixer_context->actions.mute)
     {
        mixer_context->actions.mute->func.go = _volume_mute_cb;
        e_action_predef_name_set("EMixer", _("Mute volume"), "volume_mute",
                                 NULL, NULL, 0);
     }

   e_managers_keys_ungrab();
   e_managers_keys_grab();
}

static void
_actions_unregister(void)
{
   if (mixer_context->actions.incr)
     {
        e_action_predef_name_del("Enlightnment Mixer", _("Volume Increase"));
        e_action_del("volume_increase");
        mixer_context->actions.incr = NULL;
     }

   if (mixer_context->actions.decr)
     {
        e_action_predef_name_del("Enlightnment Mixer", _("Volume Mute"));
        e_action_del("volume_decrease");
        mixer_context->actions.decr = NULL;
     }

   if (mixer_context->actions.mute)
     {
        e_action_predef_name_del("Enlightnment Mixer", _("Volume Mute"));
        e_action_del("volume_mute");
        mixer_context->actions.mute = NULL;
     }

   e_managers_keys_ungrab();
   e_managers_keys_grab();
}

static void
_popup_del(Instance *inst)
{
   inst->slider = NULL;
   inst->check = NULL;
   E_FREE_FUNC(inst->popup, e_object_del);
}

static void
_popup_del_cb(void *obj)
{
   _popup_del(e_object_data_get(obj));
}

static void
_popup_comp_del_cb(void *data, Evas_Object *obj EINA_UNUSED)
{
   Instance *inst = data;

   E_FREE_FUNC(inst->popup, e_object_del);
}

static Eina_Bool
_emixer_del_cb(void *data EINA_UNUSED, int type EINA_UNUSED,
               void *info EINA_UNUSED)
{
   mixer_context->emixer = NULL;
   if (mixer_context->emix_event_handler)
      ecore_event_handler_del(mixer_context->emix_event_handler);

   return EINA_TRUE;
}

static void
_emixer_exec_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Instance *inst = data;

   _popup_del(inst);
   if (mixer_context->emixer)
      return;

   mixer_context->emixer = ecore_exe_run("emixer", NULL);
   if (mixer_context->emix_event_handler)
      ecore_event_handler_del(mixer_context->emix_event_handler);
   mixer_context->emix_event_handler =
      ecore_event_handler_add(ECORE_EXE_EVENT_DEL, _emixer_del_cb, NULL);
}

static void
_check_changed_cb(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED,
                  void *event EINA_UNUSED)
{
   Emix_Sink *s = (Emix_Sink *)mixer_context->sink_default;
   emix_sink_mute_set(s, !s->mute);
   /*
    *TODO: is it really necessary ? or it will be update
    *      with the sink changed hanlder
    */
   _mixer_gadget_update();
}

static void
_slider_changed_cb(void *data EINA_UNUSED, Evas_Object *obj,
                   void *event EINA_UNUSED)
{
   int val;
   Emix_Volume v;
   unsigned int i;
   Emix_Sink *s = (Emix_Sink *)mixer_context->sink_default;

   val = (int)elm_slider_value_get(obj);
   v.volumes = calloc(s->volume.channel_count, sizeof(int));
   v.channel_count = s->volume.channel_count;
   for (i = 0; i < s->volume.channel_count; i++)
     v.volumes[i] = val;

   emix_sink_volume_set(s, v);
}

static Evas_Object *
_popup_add_slider(void)
{
   unsigned int volume, i;

   Evas_Object *slider = elm_slider_add(e_comp->elm);
   evas_object_size_hint_align_set(slider, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_size_hint_weight_set(slider, EVAS_HINT_EXPAND, 0.0);

   for (volume = 0, i = 0; i <
        mixer_context->sink_default->volume.channel_count; i++)
     volume += mixer_context->sink_default->volume.volumes[i];

   volume = volume / i;

   evas_object_show(slider);
   elm_slider_min_max_set(slider, 0.0, (double) EMIX_VOLUME_MAX);
   evas_object_smart_callback_add(slider, "changed", _slider_changed_cb,
                                  NULL);

   elm_slider_value_set(slider, volume);
   return slider;
}

static void
_sink_selected_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Emix_Sink *s = data;

   mixer_context->sink_default = s;
   _mixer_gadget_update();
}

static void
_popup_new(Instance *inst)
{
   Evas_Object *button, *list, *icon;
   Emix_Sink *s;
   Eina_List *l;

   EINA_SAFETY_ON_NULL_RETURN(mixer_context->sink_default);

   inst->popup = e_gadcon_popup_new(inst->gcc, 0);
   list = elm_box_add(e_comp->elm);

   inst->list = elm_list_add(e_comp->elm);
   evas_object_size_hint_align_set(inst->list, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_size_hint_weight_set(inst->list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_show(inst->list);

   EINA_LIST_FOREACH((Eina_List *)emix_sinks_get(), l, s)
     {
        Elm_Object_Item *it;

        it = elm_list_item_append(inst->list, s->name, NULL, NULL, _sink_selected_cb, s);
        if (mixer_context->sink_default == s)
          elm_list_item_selected_set(it, EINA_TRUE);
     }
   elm_box_pack_end(list, inst->list);

   inst->slider = _popup_add_slider();
   elm_box_pack_end(list, inst->slider);
   evas_object_show(inst->slider);

   inst->mute = (int) mixer_context->sink_default->mute;

   inst->check = elm_check_add(e_comp->elm);
   elm_object_text_set(inst->check, _("Mute"));
   elm_check_state_pointer_set(inst->check, &(inst->mute));
   evas_object_smart_callback_add(inst->check, "changed", _check_changed_cb,
                                  NULL);
   elm_box_pack_end(list, inst->check);
   evas_object_show(inst->check);

   icon = elm_icon_add(e_comp->elm);
   elm_icon_standard_set(icon, "preferences-system");

   button = elm_button_add(e_comp->elm);
   evas_object_size_hint_align_set(button, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_size_hint_weight_set(button, EVAS_HINT_EXPAND, 0.0);
   elm_object_part_content_set(button, "icon", icon);
   evas_object_smart_callback_add(button, "clicked", _emixer_exec_cb, inst);
   elm_box_pack_end(list, button);
   evas_object_show(button);

   evas_object_size_hint_min_set(list, 208, 208);


   e_gadcon_popup_content_set(inst->popup, list);
   e_comp_object_util_autoclose(inst->popup->comp_object,
     _popup_comp_del_cb, NULL, inst);
   e_gadcon_popup_show(inst->popup);
   e_object_data_set(E_OBJECT(inst->popup), inst);
   E_OBJECT_DEL_SET(inst->popup, _popup_del_cb);
}

static void
_menu_cb(void *data, E_Menu *menu EINA_UNUSED, E_Menu_Item *mi EINA_UNUSED)
{
   _emixer_exec_cb(data, NULL, NULL);
}

static void
_settings_cb(void *data EINA_UNUSED, E_Menu *menu EINA_UNUSED,
             E_Menu_Item *mi EINA_UNUSED)
{
   emix_config_popup_new(NULL, NULL);
}

static void
_menu_new(Instance *inst, Evas_Event_Mouse_Down *ev)
{
   E_Zone *zone;
   E_Menu *m;
   E_Menu_Item *mi;
   int x, y;

   zone = e_zone_current_get();

   m = e_menu_new();

   mi = e_menu_item_new(m);
   e_menu_item_label_set(mi, _("Advanced"));
   e_util_menu_item_theme_icon_set(mi, "configure");
   e_menu_item_callback_set(mi, _menu_cb, inst);

   mi = e_menu_item_new(m);
   e_menu_item_label_set(mi, _("Settings"));
   e_util_menu_item_theme_icon_set(mi, "configure");
   e_menu_item_callback_set(mi, _settings_cb, inst);

   m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);

   e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon, &x, &y, NULL, NULL);
   e_menu_activate_mouse(m, zone, x + ev->output.x, y + ev->output.y,
                         1, 1, E_MENU_POP_DIRECTION_AUTO, ev->timestamp);
   evas_event_feed_mouse_up(inst->gcc->gadcon->evas, ev->button,
                            EVAS_BUTTON_NONE, ev->timestamp, NULL);
}

static void
_mouse_down_cb(void *data, Evas *evas EINA_UNUSED,
               Evas_Object *obj EINA_UNUSED, void *event)
{
   Instance *inst = data;
   Evas_Event_Mouse_Down *ev = event;

   if (ev->button == 1)
     {
        if (!inst->popup)
          _popup_new(inst);
     }
   else if (ev->button == 2)
     {
        _volume_mute_cb(NULL, NULL);
     }
   else if (ev->button == 3)
     {
        _menu_new(inst, ev);
     }
}

static void
_mouse_wheel_cb(void *data EINA_UNUSED, Evas *evas EINA_UNUSED,
                Evas_Object *obj EINA_UNUSED, void *event)
{
   Evas_Event_Mouse_Wheel *ev = event;

   if (ev->z > 0)
     _volume_decrease_cb(NULL, NULL);
   else if (ev->z < 0)
     _volume_increase_cb(NULL, NULL);
}

/*
 * Gadcon functions
 */
static E_Gadcon_Client *
_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
{
   E_Gadcon_Client *gcc;
   Instance *inst;

   inst = E_NEW(Instance, 1);

   inst->gadget = edje_object_add(gc->evas);
   inst->evas = gc->evas;
   edje_object_file_set(inst->gadget, mixer_context->theme,
                        "e/modules/mixer/main");

   gcc = e_gadcon_client_new(gc, name, id, style, inst->gadget);
   gcc->data = inst;
   inst->gcc = gcc;

   evas_object_event_callback_add(inst->gadget, EVAS_CALLBACK_MOUSE_DOWN,
                                  _mouse_down_cb, inst);
   evas_object_event_callback_add(inst->gadget, EVAS_CALLBACK_MOUSE_WHEEL,
                                  _mouse_wheel_cb, inst);
   mixer_context->instances = eina_list_append(mixer_context->instances, inst);

   if (mixer_context->sink_default)
     _mixer_gadget_update();

   return gcc;
}

static void
_gc_shutdown(E_Gadcon_Client *gcc)
{
   Instance *inst;

   inst = gcc->data;
   evas_object_del(inst->gadget);
   mixer_context->instances = eina_list_remove(mixer_context->instances, inst);
   free(inst);
}

static void
_gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient)
{
   Instance *inst;
   int w, h;

   inst = gcc->data;
   if (orient != E_GADCON_ORIENT_LAST)
      inst->orient = orient;

   switch (inst->orient)
     {
     case E_GADCON_ORIENT_VERT:
     case E_GADCON_ORIENT_LEFT:
     case E_GADCON_ORIENT_RIGHT:
     case E_GADCON_ORIENT_CORNER_LT:
     case E_GADCON_ORIENT_CORNER_RT:
     case E_GADCON_ORIENT_CORNER_LB:
     case E_GADCON_ORIENT_CORNER_RB:
        w = 16;
        h = 16;
     default:
        break;
     }

   e_gadcon_client_aspect_set(gcc, w, h);
   e_gadcon_client_min_size_set(gcc, w, h);
}

static const char *
_gc_label(const E_Gadcon_Client_Class *client_class EINA_UNUSED)
{
   return "EMixer";
}

static Evas_Object *
_gc_icon(const E_Gadcon_Client_Class *client_class EINA_UNUSED, Evas *evas)
{
   Evas_Object *o;
   char buf[4096] = { 0 };

   o = edje_object_add(evas);
   snprintf(buf, sizeof(buf), "%s/mixer.edj",
            e_module_dir_get(mixer_context->module));
   edje_object_file_set(o, buf, "icon");

   return o;
}

static const char *
_gc_id_new(const E_Gadcon_Client_Class *client_class EINA_UNUSED)
{
   return _gadcon_class.name;
}

static void
_sink_event(int type, void *info)
{
   Emix_Sink *sink = info;
   const Eina_List *l;

   if (type == EMIX_SINK_REMOVED_EVENT)
     {
        if (sink == mixer_context->sink_default)
          {
             l = emix_sinks_get();
             mixer_context->sink_default = l->data;
             _mixer_gadget_update();
          }
     }
   else if (type == EMIX_SINK_CHANGED_EVENT)
     {
        if (mixer_context->sink_default == sink)
          {
             _mixer_gadget_update();
             _notify(sink->mute ? 0 : sink->volume.volumes[0]);
          }
     }
   else
     {
        DBG("Sink added");
     }
}

static void
_disconnected(void)
{
   mixer_context->sink_default = NULL;
   _mixer_gadget_update();
}

static void
_ready(void)
{
   if (emix_sink_default_support())
     mixer_context->sink_default = emix_sink_default_get();
   else
     mixer_context->sink_default = emix_sinks_get()->data;

   _mixer_gadget_update();
}

static void
_events_cb(void *data EINA_UNUSED, enum Emix_Event type, void *event_info)
{
   switch (type)
     {
      case EMIX_SINK_ADDED_EVENT:
      case EMIX_SINK_CHANGED_EVENT:
      case EMIX_SINK_REMOVED_EVENT:
         _sink_event(type, event_info);
         break;
      case EMIX_DISCONNECTED_EVENT:
         _disconnected();
         break;
      case EMIX_READY_EVENT:
         _ready();
         break;
      default:
         break;
     }
}

static Eina_Bool
_desklock_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *info)
{
   E_Event_Desklock *ev = info;
   static Eina_Bool _was_mute = EINA_FALSE;

   if (emix_config_desklock_mute_get() == EINA_FALSE)
     return ECORE_CALLBACK_PASS_ON;

   if (ev->on)
     {
        _was_mute = mixer_context->sink_default->mute;
        if (!_was_mute)
          emix_sink_mute_set((Emix_Sink *)mixer_context->sink_default, EINA_TRUE);
     }
   else
     {
        if (!_was_mute)
          emix_sink_mute_set((Emix_Sink *)mixer_context->sink_default, EINA_FALSE);
     }

   return ECORE_CALLBACK_PASS_ON;
}

static void
_backend_changed(const char *backend, void *data EINA_UNUSED)
{
   _disconnected();

   if (emix_backend_set(backend) == EINA_FALSE)
     ERR("Could not load backend: %s", backend);
}

EAPI void *
e_modapi_init(E_Module *m)
{
   Eina_List *l;
   char buf[4096];
   const char *backend;
   Eina_Bool backend_loaded = EINA_FALSE;

   _e_emix_log_domain = eina_log_domain_register("emix_module", EINA_COLOR_RED);

   if (!mixer_context)
     {
        mixer_context = E_NEW(Context, 1);

        mixer_context->desklock_handler =
           ecore_event_handler_add(E_EVENT_DESKLOCK, _desklock_cb, NULL);
        mixer_context->module = m;
        snprintf(buf, sizeof(buf), "%s/mixer.edj",
                 e_module_dir_get(mixer_context->module));
        mixer_context->theme = strdup(buf);
     }


   EINA_SAFETY_ON_FALSE_RETURN_VAL(emix_init(), NULL);
   emix_config_init(_backend_changed, NULL);
   emix_event_callback_add(_events_cb, NULL);

   backend = emix_config_backend_get();
   if (backend && emix_backend_set(backend) == EINA_TRUE)
      backend_loaded = EINA_TRUE;
   else
     {
        if (backend)
           WRN("Could not load %s, trying another one ...", backend);
        EINA_LIST_FOREACH((Eina_List *)emix_backends_available(), l,
                          backend)
          {
             if (emix_backend_set(backend) == EINA_TRUE)
               {
                  DBG("Loaded backend: %s!", backend);
                  backend_loaded = EINA_TRUE;
                  emix_config_backend_set(backend);
                  break;
               }
          }
     }

   if (backend_loaded == EINA_FALSE)
      goto err;

   e_configure_registry_item_add("extensions/emix", 150, "Emix", NULL,
                                 NULL, emix_config_popup_new);

   if (emix_sink_default_support())
      mixer_context->sink_default = emix_sink_default_get();

   e_gadcon_provider_register(&_gadcon_class);
   _actions_register();

   return m;

err:
   emix_config_shutdown();
   emix_shutdown();
   return NULL;
}

EAPI int
e_modapi_shutdown(E_Module *m EINA_UNUSED)
{
   _actions_unregister();
   e_gadcon_provider_unregister((const E_Gadcon_Client_Class *)&_gadcon_class);

   if (!mixer_context)
     {
        if (mixer_context->theme)
          free(mixer_context->theme);

        E_FREE(mixer_context);
     }

   emix_shutdown();
   emix_config_shutdown();
   return 1;
}

EAPI int
e_modapi_save(E_Module *m EINA_UNUSED)
{
   return 1;
}