summaryrefslogtreecommitdiff
path: root/src/bin/sources_view.c
blob: bbf40084cfa750c9da18b5e04ba7f51fffa07217 (plain) (blame)
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
#include "sources_view.h"

#include <emix.h>

#define SOURCES_KEY "sources.key"

struct Sources_View
{
   Evas_Object *self;
   Evas_Object *genlist;
   Elm_Genlist_Item_Class *itc;

   Ecore_Event_Handler *disconnected;
   Ecore_Event_Handler *source_added;
   Ecore_Event_Handler *source_changed;
   Ecore_Event_Handler *source_removed;
};

static Eina_Bool
_disconnected_cb(void *data, int type EINA_UNUSED, void *info EINA_UNUSED)
{
   struct Sources_View *sv = data;
   elm_genlist_clear(sv->genlist);

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_source_add_cb(void *data, int type EINA_UNUSED, void *info)
{
   struct Sources_View *sv = data;
   Emix_Source *source= info;

   elm_genlist_item_append(sv->genlist, sv->itc, source, NULL,
                           ELM_GENLIST_ITEM_NONE, NULL, sv);

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_source_removed_cb(void *data, int type EINA_UNUSED, void *info)
{
   struct Sources_View *sv = data;
   Emix_Source *source = info;
   Elm_Object_Item *item = elm_genlist_first_item_get(sv->genlist);
   if (source == elm_object_item_data_get(item))
     elm_object_item_del(item);

   while ((item = elm_genlist_item_next_get(item)))
     {
        if (source == elm_object_item_data_get(item))
          elm_object_item_del(item);
     }

   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_source_changed_cb(void *data, int type EINA_UNUSED, void *info)
{
   struct Sources_View *sv = data;
   Emix_Source *source = info;
   Elm_Object_Item *item = elm_genlist_first_item_get(sv->genlist);
   Evas_Object *it;

   while (item)
     {
        if (source == elm_object_item_data_get(item))
          {
             it = elm_object_item_part_content_get(item, "item");
             if (it)
               elm_slider_value_set(it, source->volume.volumes[0]);

             it = elm_object_item_part_content_get(item, "mute");
             if (it)
               elm_check_state_set(it, source->mute);
             break;
          }
        item = elm_genlist_item_next_get(item);
     }

   return ECORE_CALLBACK_PASS_ON;
}

static void
_del_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *o EINA_UNUSED,
        void *event_info EINA_UNUSED)
{
   struct Sources_View *sv = data;

#define ECORE_EVENT_HANDLER_DEL(_handle)        \
   if (sv->_handle)                             \
     {                                          \
        ecore_event_handler_del(sv->_handle);   \
        sv->_handle = NULL;                     \
     }

   ECORE_EVENT_HANDLER_DEL(disconnected)
   ECORE_EVENT_HANDLER_DEL(source_removed)
   ECORE_EVENT_HANDLER_DEL(source_changed)
   ECORE_EVENT_HANDLER_DEL(source_added)
#undef ECORE_EVENT_HANDLER_DEL
}

static char *
_item_text_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part)
{
   Emix_Source *source = data;

   if (!strcmp(part, "name"))
     {
        return strdup(source->name);
     }

   return NULL;
}

static void
_volume_changed_cb(void *data, Evas_Object *o, void *event_info EINA_UNUSED)
{
   Emix_Source *source = data;
   double val = elm_slider_value_get(o);
   Emix_Volume volume;
   unsigned int i;

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

   emix_source_volume_set(source, volume);
   free(volume.volumes);
}

static void
_mute_changed_cb(void *data, Evas_Object *o EINA_UNUSED,
                 void *event_info EINA_UNUSED)
{
   Emix_Source *source = data;

   emix_source_mute_set(source, source->mute);
}

static Evas_Object *
_item_content_get(void *data, Evas_Object *obj, const char *part)
{
   Evas_Object *item = NULL;
   Emix_Source *source = data;

   if (!strcmp(part, "slider"))
     {
        item = elm_slider_add(obj);
        EINA_SAFETY_ON_NULL_RETURN_VAL(item, NULL);

        elm_slider_step_set(item, 1.0/BASE_VOLUME_STEP);
        elm_slider_unit_format_set(item, "%1.0f");
        elm_slider_indicator_format_set(item, "%1.0f");
        elm_slider_span_size_set(item, 120);
        elm_slider_min_max_set(item, 0.0, 100.0);
        elm_slider_value_set(item, source->volume.volumes[0]);
        evas_object_smart_callback_add(item, "delay,changed",
                                       _volume_changed_cb, source);
     }
   else if (!strcmp(part, "mute"))
     {
        item = elm_check_add(obj);
        EINA_SAFETY_ON_NULL_RETURN_VAL(item, NULL);

        elm_object_style_set(item, "toggle");
        elm_object_part_text_set(item, "off", "Mute");
        elm_object_part_text_set(item, "on", "Unmute");

        elm_check_state_set(item, source->mute);
        elm_check_state_pointer_set(item, &source->mute);
        evas_object_smart_callback_add(item, "changed", _mute_changed_cb,
                                       source);
     }

   return item;
}

Evas_Object *
sources_view_add(Evas_Object *parent)
{
   Evas_Object *layout;
   struct Sources_View *sv;

   sv = calloc(1, sizeof(struct Sources_View));
   EINA_SAFETY_ON_NULL_RETURN_VAL(sv, NULL);

   layout = epulse_layout_add(parent, "sources", "default");
   EINA_SAFETY_ON_NULL_GOTO(layout, err);

   evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL, _del_cb, sv);

   sv->genlist = elm_genlist_add(layout);
   EINA_SAFETY_ON_NULL_GOTO(sv->genlist, err_genlist);

   sv->disconnected = ecore_event_handler_add(EMIX_DISCONNECTED_EVENT,
                                              _disconnected_cb, sv);
   sv->source_added = ecore_event_handler_add(EMIX_SOURCE_ADDED_EVENT,
                                              _source_add_cb, sv);
   sv->source_added = ecore_event_handler_add(EMIX_SOURCE_CHANGED_EVENT,
                                              _source_changed_cb, sv);
   sv->source_removed = ecore_event_handler_add(EMIX_SOURCE_REMOVED_EVENT,
                                              _source_removed_cb, sv);

   sv->itc = elm_genlist_item_class_new();
   EINA_SAFETY_ON_NULL_GOTO(sv->itc, err_genlist);
   sv->itc->item_style = "sources";
   sv->itc->func.text_get = _item_text_get;
   sv->itc->func.content_get = _item_content_get;

   evas_object_data_set(layout, SOURCES_KEY, sv);
   elm_layout_content_set(layout, "list", sv->genlist);

   evas_object_size_hint_weight_set(sv->genlist, EVAS_HINT_EXPAND,
                                    EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(sv->genlist, EVAS_HINT_FILL,
                                   EVAS_HINT_FILL);

   return layout;

 err_genlist:
   ecore_event_handler_del(sv->disconnected);
   ecore_event_handler_del(sv->source_added);
   ecore_event_handler_del(sv->source_changed);
   ecore_event_handler_del(sv->source_removed);
   free(layout);
 err:
   free(sv);

   return NULL;
}