Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / minicontrollers.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <minicontrol-viewer.h>
18 #include <Ecore.h>
19 #include <Eina.h>
20
21 #include "log.h"
22 #include "minicontrollers.h"
23
24 typedef struct {
25         const char *name;
26         int width, height;
27 } minicontroller_info_t;
28
29 static Eina_List *active_minicontroller;
30 int LOCKSCREEN_EVENT_MINICONTROLLERS_CHANGED, init_count;
31
32 static int _lockscreen_minicontroller_search(const void *data1, const void *data2);
33
34 static minicontroller_info_t *_minicontroller_create(const char *name, int w, int h)
35 {
36         minicontroller_info_t *ret = calloc(1, sizeof(minicontroller_info_t));
37
38         ret->name = eina_stringshare_add(name);
39         ret->width = w;
40         ret->height = h;
41
42         return ret;
43 }
44
45 static void _minicontroller_destroy(minicontroller_info_t *info)
46 {
47         eina_stringshare_del(info->name);
48         free(info);
49 }
50
51 static void _minicontroller_start_handle(const char *name, int w, int h)
52 {
53         /** FIXME Since minicontroller API do not allow to filter minicontrollers
54          * targeted for lockscreen we just asume that interesting minicontrollers
55          * has proper suffix in its name */
56         if (name && strstr(name, "LOCKSCREEN")) {
57                 minicontroller_info_t *info = eina_list_search_unsorted(active_minicontroller, _lockscreen_minicontroller_search, name);
58                 if (name) {
59                         info = _minicontroller_create(name, w, h);
60                         active_minicontroller = eina_list_append(active_minicontroller, info);
61                         ecore_event_add(LOCKSCREEN_EVENT_MINICONTROLLERS_CHANGED, NULL, NULL, NULL);
62                 }
63         }
64 }
65
66 static void _minicontroller_stop_handle(const char *name)
67 {
68         if (name) {
69                 minicontroller_info_t *info = eina_list_search_unsorted(active_minicontroller, _lockscreen_minicontroller_search, name);
70                 if (info) {
71                         active_minicontroller = eina_list_remove(active_minicontroller, info);
72                         _minicontroller_destroy(info);
73                         ecore_event_add(LOCKSCREEN_EVENT_MINICONTROLLERS_CHANGED, NULL, NULL, NULL);
74                 }
75         }
76 }
77
78 static void _minicontroller_geometry_from_bundle_get(bundle *event_arg, int *width, int *height)
79 {
80         int *val;
81         size_t val_size;
82         if (!event_arg)
83                 return;
84         int ret = bundle_get_byte(event_arg, "width", (void**)&val, &val_size);
85         if (ret == BUNDLE_ERROR_NONE) {
86                 if (width) *width = *val;
87         }
88         ret = bundle_get_byte(event_arg, "height", (void**)&val, &val_size);
89         if (ret == BUNDLE_ERROR_NONE) {
90                 if (height) *height = *val;
91         }
92 }
93
94 static void _minicontroler_event(minicontrol_event_e event, const char *minicontrol_name, bundle *event_arg, void *data)
95 {
96         int w = 0, h = 0;
97         if (!minicontrol_name)
98                 return;
99
100         DBG("Available minicontroller: %s", minicontrol_name);
101
102         switch (event) {
103                 case MINICONTROL_EVENT_START:
104                         _minicontroller_geometry_from_bundle_get(event_arg, &w, &h);
105                         _minicontroller_start_handle(minicontrol_name, w, h);
106                         break;
107                 case MINICONTROL_EVENT_STOP:
108                         _minicontroller_stop_handle(minicontrol_name);
109                         break;
110                 default:
111                         DBG("Unahandled minicontroller event: %d for %s", event, minicontrol_name);
112                         break;
113         }
114 }
115
116 int lockscreen_minicontrollers_init(void)
117 {
118         if (!init_count) {
119                 LOCKSCREEN_EVENT_MINICONTROLLERS_CHANGED = ecore_event_type_new();
120                 int ret = minicontrol_viewer_set_event_cb(_minicontroler_event, NULL);
121                 if (ret != MINICONTROL_ERROR_NONE) {
122                         ERR("minicontrol_viewer_set_event_cb failed: %s", get_error_message(ret));
123                         return 1;
124                 }
125         }
126         init_count++;
127         return 0;
128 }
129
130 void lockscreen_minicontrollers_shutdown(void)
131 {
132         minicontroller_info_t *info;
133         if (init_count) {
134                 init_count--;
135                 if (!init_count) {
136                         int ret = minicontrol_viewer_unset_event_cb();
137                         if (ret != MINICONTROL_ERROR_NONE) {
138                                 ERR("minicontrol_viewer_unset_event_cb failed: %s", get_error_message(ret));
139                         }
140                         EINA_LIST_FREE(active_minicontroller, info) {
141                                 _minicontroller_destroy(info);
142                         }
143                         active_minicontroller = NULL;
144                 }
145         }
146 }
147
148 static int _lockscreen_minicontroller_search(const void *data1, const void *data2)
149 {
150         const minicontroller_info_t *info = data1;
151         const char *name = data2;
152         return strcmp(name, info->name);
153 }
154
155 Evas_Object *lockscreen_minicontrollers_minicontroller_create(const char *name, Evas_Object *parent)
156 {
157         minicontroller_info_t *info = eina_list_search_unsorted(active_minicontroller, _lockscreen_minicontroller_search, name);
158  
159         if (!info) {
160                 ERR("Invalid minicontroller name: %s", name);
161                 return NULL;
162         }
163
164         Evas_Object *ret = minicontrol_viewer_add(parent, info->name);
165         evas_object_size_hint_min_set(ret, info->width, info->height);
166         evas_object_show(ret);
167         return ret;
168 }
169
170 Eina_List *lockscreen_minicontrollers_list_get(void)
171 {
172         Eina_List *ret = NULL, *l;
173         minicontroller_info_t *info;
174
175         EINA_LIST_FOREACH(active_minicontroller, l, info) {
176                 ret = eina_list_append(ret, info->name);
177         }
178         return ret;
179 }
180
181 bool lockscreen_minicontrollers_minicontroller_stop(const char *name)
182 {
183         int ret = minicontrol_viewer_send_event(name, MINICONTROL_VIEWER_EVENT_HIDE, NULL);
184         if (ret != MINICONTROL_ERROR_NONE) {
185                 ERR("minicontrol_viewer_send_event failed: %s", get_error_message(ret));
186                 return false;
187         }
188         return true;
189 }