summaryrefslogtreecommitdiff
path: root/src/modules/music-control/ui.c
blob: b32e96bcddf319be035b5431fb2ba4426bbf29a7 (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
#include "private.h"

extern Player music_player_players[];

static void
_play_state_update(E_Music_Control_Instance *inst, Eina_Bool without_delay)
{
   if (!inst->popup) return;
   if (inst->ctxt->playning)
     {
        edje_object_signal_emit(inst->content_popup, "btn,state,image,pause", "play");
        return;
     }

   if (without_delay)
     edje_object_signal_emit(inst->content_popup, "btn,state,image,play,no_delay", "play");
   else
     edje_object_signal_emit(inst->content_popup, "btn,state,image,play", "play");
}

void
music_control_state_update_all(E_Music_Control_Module_Context *ctxt)
{
   E_Music_Control_Instance *inst;
   Eina_List *list;

   EINA_LIST_FOREACH(ctxt->instances, list, inst)
     _play_state_update(inst, EINA_FALSE);
}

static void
_btn_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
{
   E_Music_Control_Instance *inst = data;
   if (!strcmp(source, "play"))
     media_player2_player_play_pause_call(inst->ctxt->mpris2_player);
   else if (!strcmp(source, "next"))
     media_player2_player_next_call(inst->ctxt->mpris2_player);
   else if (!strcmp(source, "previous"))
    media_player2_player_previous_call(inst->ctxt->mpris2_player);
}

static void
_label_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
{
   E_Music_Control_Instance *inst = data;
   music_control_popup_del(inst);
   mpris_media_player2_raise_call(inst->ctxt->mrpis2);
}

static void
_player_name_update(E_Music_Control_Instance *inst)
{
   Edje_Message_String msg;
   msg.str = (char *)music_player_players[inst->ctxt->config->player_selected].name;
   edje_object_message_send(inst->content_popup, EDJE_MESSAGE_STRING, 0, &msg);
}

static Eina_Bool
_popup_input_window_mouse_up_cb(void *data, int type __UNUSED__, void *event)
{
   Ecore_Event_Mouse_Button *ev = event;
   E_Music_Control_Instance *inst = data;
   if (ev->window == inst->win)
     music_control_popup_del(inst);
   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_popup_input_window_key_down_cb(void *data, int type __UNUSED__, void *event)
{
   Ecore_Event_Key *ev = event;
   E_Music_Control_Instance *inst = data;
   if (ev->window == inst->win)
     music_control_popup_del(inst);
   return ECORE_CALLBACK_PASS_ON;
}

static void
_popup_input_window_create(E_Music_Control_Instance *inst)
{
   Ecore_X_Window_Configure_Mask mask;
   Ecore_X_Window popup_w;
   E_Manager *man = e_manager_current_get();

   inst->win = ecore_x_window_input_new(man->root, 0, 0, man->w, man->h);
   mask = (ECORE_X_WINDOW_CONFIGURE_MASK_STACK_MODE |
           ECORE_X_WINDOW_CONFIGURE_MASK_SIBLING);
   popup_w = inst->popup->win->evas_win;
   ecore_x_window_configure(inst->win, mask, 0, 0, 0, 0, 0, popup_w,
                            ECORE_X_WINDOW_STACK_BELOW);
   ecore_x_window_show(inst->win);

   inst->mouse_up =
            ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP,
                                    _popup_input_window_mouse_up_cb, inst);
   inst->key_down =
            ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
                                    _popup_input_window_key_down_cb, inst);
   e_grabinput_get(0, 0, inst->win);
}

static void
_popup_input_window_destroy(E_Music_Control_Instance *inst)
{
   e_grabinput_release(0, inst->win);
   ecore_x_window_free(inst->win);
   ecore_event_handler_del(inst->mouse_up);
   ecore_event_handler_del(inst->key_down);
}

static void
_popup_new(E_Music_Control_Instance *inst)
{
   Evas_Object *o;
   inst->popup = e_gadcon_popup_new(inst->gcc);

   o = edje_object_add(inst->popup->win->evas);
   e_theme_edje_object_set(o, "base/theme/modules/music-control",
                           "modules/music-control/popup");
   edje_object_signal_callback_add(o, "btn,clicked", "*", _btn_clicked, inst);
   edje_object_signal_callback_add(o, "label,clicked", "player_name", _label_clicked, inst);

   e_gadcon_popup_content_set(inst->popup, o);
   inst->content_popup = o;

   _player_name_update(inst);
   _play_state_update(inst, EINA_TRUE);
   e_gadcon_popup_show(inst->popup);
   _popup_input_window_create(inst);
}

void
music_control_popup_del(E_Music_Control_Instance *inst)
{
   e_gadcon_popup_hide(inst->popup);
   _popup_input_window_destroy(inst);
   e_object_del(E_OBJECT(inst->popup));
   inst->popup = NULL;
}

struct _E_Config_Dialog_Data
{
   int index;
};

static Evas_Object *
_cfg_widgets_create(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
   Evas_Object *o, *of, *ob;
   E_Radio_Group *rg;
   int i;
   E_Music_Control_Instance *inst = cfd->data;
   int player_selected = inst->ctxt->config->player_selected;

   o = e_widget_list_add(evas, 0, 0);

   of = e_widget_framelist_add(evas, "Music Player", 0);
   e_widget_framelist_content_align_set(of, 0.0, 0.0);
   rg = e_widget_radio_group_new(&(cfdata->index));
   for (i = 0; music_player_players[i].dbus_name; i++)
     {
        ob = e_widget_radio_add(evas, music_player_players[i].name, i, rg);
        e_widget_framelist_object_append(of, ob);
        if (i == player_selected)
          e_widget_radio_toggle_set(ob, EINA_TRUE);
     }
   ob = e_widget_label_add(evas, "* Your player must be configured to export the DBus interface MPRIS2.");
   e_widget_framelist_object_append(of, ob);
   e_widget_list_object_append(o, of, 1, 1, 0.5);

   return o;
}

static void *
_cfg_data_create(E_Config_Dialog *cfd)
{
   E_Music_Control_Instance *inst = cfd->data;
   E_Config_Dialog_Data *cfdata = calloc(1, sizeof(E_Config_Dialog_Data));
   cfdata->index = inst->ctxt->config->player_selected;
   return cfdata;
}

static void
_cfg_data_free(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
   free(cfdata);
}

static int
_cfg_data_apply(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
   E_Music_Control_Instance *inst = cfd->data;
   if (inst->ctxt->config->player_selected == cfdata->index)
     return 1;

   inst->ctxt->config->player_selected = cfdata->index;
   inst->ctxt->playning = EINA_FALSE;
   mpris_media_player2_proxy_unref(inst->ctxt->mpris2_player);
   media_player2_player_proxy_unref(inst->ctxt->mrpis2);
   music_control_dbus_init(inst->ctxt,
          music_player_players[inst->ctxt->config->player_selected].dbus_name);
   return 1;
}

static void
_cb_menu_cfg(void *data, E_Menu *m, E_Menu_Item *mi)
{
   E_Config_Dialog_View *v;

   v = calloc(1, sizeof(E_Config_Dialog_View));
   v->create_cfdata = _cfg_data_create;
   v->free_cfdata = _cfg_data_free;
   v->basic.create_widgets = _cfg_widgets_create;
   v->basic.apply_cfdata = _cfg_data_apply;

   e_config_dialog_new(m->zone->container, "Music control Settings", "E",
                       "_e_mod_music_config_dialog",
                       NULL, 0, v, data);
}

void
music_control_mouse_down_cb(void *data, Evas *evas, Evas_Object *obj, void *event)
{
   E_Music_Control_Instance *inst = data;
   Evas_Event_Mouse_Down *ev = event;

   if (ev->button == 1)
     {
        if (!inst->popup)
          _popup_new(inst);
        else
          music_control_popup_del(inst);
     }
   else if (ev->button == 3)
     {
        E_Menu *m;
        E_Menu_Item *mi;
        E_Zone *zone = e_util_zone_current_get(e_manager_current_get());
        int x, y;

        if (inst->popup)
          music_control_popup_del(inst);

        m = e_menu_new();
        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, _cb_menu_cfg, 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);
     }
}