Add refresh screen information event
[platform/core/uifw/mmi-manager.git] / src / mmimgr / mmi-provider.c
1 /*
2 * Copyright © 2021 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "mmi-provider-iface.h"
25 #include "mmi-manager-dbg.h"
26 #include "mmi-common.h"
27 #include "mmi-provider.h"
28
29 #include <Ecore.h>
30 #include <dirent.h>
31 #include <stdio.h>
32 #include <dlfcn.h>
33 #include <string.h>
34
35 Eina_List *_provider_list = NULL;
36 Ecore_Event_Handler *_provider_event_handlers[5];
37
38 static Eina_Bool
39 _key_provider_event_cb(void *data EINA_UNUSED, int type, void *event)
40 {
41         mmi_provider_event_key *ev = (mmi_provider_event_key *)event;
42
43         LOGI("[MMI_PROVIDER_EVENT_KEY] type:%d, timestamp:%d, duration:%d, confidence=%.2f\n",
44                 ev->type, ev->timestamp, ev->duration, ev->confidence);
45         LOGI("... keycode:%d, key_down:%d, keyname=%s, source=%s\n",
46                 ev->keycode, ev->key_down, ev->keyname, ev->source);
47
48         return ECORE_CALLBACK_PASS_ON;
49 }
50
51 static Eina_Bool
52 _gesture_provider_event_cb(void *data EINA_UNUSED, int type, void *event)
53 {
54         mmi_provider_event_gesture *ev = (mmi_provider_event_gesture *)event;
55
56         LOGI("[MMI_PROVIDER_EVENT_GESTURE] type:%d, timestamp:%d, duration:%d, confidence=%.2f\n",
57                 ev->type, ev->timestamp, ev->duration, ev->confidence);
58         LOGI("... source=%s\n", ev->source);
59
60         return ECORE_CALLBACK_PASS_ON;
61 }
62
63 static Eina_Bool
64 _voice_provider_event_cb(void *data EINA_UNUSED, int type, void *event)
65 {
66         mmi_provider_event_voice *ev = (mmi_provider_event_voice *)event;
67
68         LOGI("[MMI_PROVIDER_EVENT_VOICE] type:%d, timestamp:%d, duration:%d, confidence=%.2f\n",
69                 ev->type, ev->timestamp, ev->duration, ev->confidence);
70         LOGI("... cmd:%s, nargs:%d, source:%s\n", ev->cmd, ev->nargs, ev->source);
71
72         return ECORE_CALLBACK_PASS_ON;
73 }
74
75 static Eina_Bool
76 _vision_provider_event_cb(void *data EINA_UNUSED, int type, void *event)
77 {
78         mmi_provider_event_vision *ev = (mmi_provider_event_vision *)event;
79
80         LOGI("[MMI_PROVIDER_EVENT_VISION] type:%d, timestamp:%d, duration:%d, confidence=%.2f\n",
81                 ev->type, ev->timestamp, ev->duration, ev->confidence);
82         LOGI("... cmd:%s, nargs:%d, source:%s\n", ev->cmd, ev->nargs, ev->source);
83
84         mmi_vision_state state = ev->type;
85         LOGI("[MMI_PROVIDER_EVENT_VISION] state = %d", state);
86
87         switch(state)
88         {
89                 case 0:
90                         LOGI("LIKE\n");
91                 break;
92                 case 1:
93                         LOGI("OKAY\n");
94                 break;
95                 case 2:
96                         LOGI("NO\n");
97                         LOGI("type : %d, confidence : %f\n", ev->type, ev->confidence);
98                         break;
99         }
100         return ECORE_CALLBACK_PASS_ON;
101 }
102
103 static Eina_Bool
104 _screen_analyzer_provider_event_cb(void *data EINA_UNUSED, int type, void *event)
105 {
106         // TOODO: implement
107         return ECORE_CALLBACK_PASS_ON;
108 }
109
110 int
111 mmi_provider_set_op_mode_on_caps(unsigned long long caps, mmi_provider_op_mode mode)
112 {
113         Eina_List *l = NULL;
114         mmi_provider_handle *provider = NULL;
115
116         if (!caps)
117         {
118                 LOGE("Given caps in empty !\n");
119                 return -1;
120         }
121
122         EINA_LIST_FOREACH(_provider_list, l, provider)
123         {
124                 if (provider->capabilities & caps)
125                 {
126                         LOGD("Set op_mode(%d) on provider(%s) !\n", mode, provider->module_info->name);
127                         provider->module_data->set_mode(mode);
128                 }
129         }
130
131         return 0;
132 }
133
134 int
135 mmi_provider_register_caps(mmi_provider_handle *provider, unsigned long long caps)
136 {
137         if (!provider)
138         {
139                 LOGE("Given provider is invalid !\n");
140                 return -1;
141         }
142
143         if (!caps)
144         {
145                 LOGE("Given caps is invalid ! (caps=%lld)\n", caps);
146                 return -1;
147         }
148
149         /* Right now, we add the given caps to the existing caps. */
150         provider->capabilities |= caps;
151         return 0;
152 }
153
154 void *
155 mmi_provider_alloc_event(int ev_type)
156 {
157         (void) ev_type;
158
159         return (void *)0;
160 }
161
162 void
163 _provider_load_module(const char *provider_name)
164 {
165         mmi_provider_handle *provider = (mmi_provider_handle *)calloc(1, sizeof(mmi_provider_handle));
166
167         if (!provider)
168         {
169                 LOGE("Failed to alloc provider handle");
170                 return;
171         }
172
173         void *provider_info = NULL;
174         mmi_provider_module *mmi_provider_module_info = NULL;
175         mmi_provider_module_data *provider_module_data = NULL;
176
177         provider_info = dlopen(provider_name, RTLD_LAZY);
178
179         if(!provider_info)
180         {
181                 LOGE("Failed to dlopen(error :%s, path :%s)!\n", dlerror(), provider_name);
182                 goto err;
183         }
184
185         mmi_provider_module_info = dlsym(provider_info, "mmi_provider_module_info");
186
187         if(!mmi_provider_module_info)
188         {
189                 LOGE("Module(%s) dosen't have mmi_provider_module_info \n", provider_name);
190                 goto err;
191         }
192
193         if(!mmi_provider_module_info->provider_init || !mmi_provider_module_info->provider_deinit)
194         {
195                 LOGE("Module(%s) doesn't have init/deinit function\n", provider_name);
196                 goto err;
197         }
198
199         provider_module_data = mmi_provider_module_info->provider_init();
200
201         if(!provider_module_data)
202         {
203                 LOGE("Failed to init module (%s) !\n", provider_name);
204                 goto err;
205         }
206
207         LOGI("moudle info name = %s\n", mmi_provider_module_info->name);
208         provider->provider_info = provider_info;
209         provider->module_info = mmi_provider_module_info;
210         provider->module_data = provider_module_data;
211         provider->capabilities = mmi_provider_module_info->capabilities;
212         provider->op_mode = provider_module_data->get_mode();
213
214         _provider_list = eina_list_append(_provider_list, provider);
215         return;
216
217 err:
218         if(mmi_provider_module_info && mmi_provider_module_info->provider_deinit)
219                 mmi_provider_module_info->provider_deinit(provider_module_data);
220
221         if(provider_info)
222                 dlclose(provider_info);
223
224         if(provider)
225                 free(provider);
226         return;
227 }
228
229 void
230 _modality_providers_lookup(void)
231 {
232         DIR *dir;
233         struct dirent *entry;
234
235         dir = opendir(PROVIDER_PATH);
236
237         if(!dir)
238         {
239                 LOGE("no dir\n");
240                 return;
241         }
242
243         while((entry = readdir(dir)) != NULL)
244         {
245                 char provider_name[512];
246                 if(strstr(entry->d_name, "libmmi_modality") != NULL)
247                 {
248                         snprintf(provider_name, sizeof(provider_name), "%s%s", PROVIDER_PATH, entry->d_name);
249                         LOGD("Provider full path = %s\n", provider_name);
250                         _provider_load_module(provider_name);
251                 }
252         }
253
254         closedir(dir);
255 }
256
257 static void
258 _event_handler_init()
259 {
260         MMI_PROVIDER_EVENT_KEY = ecore_event_type_new();
261         MMI_PROVIDER_EVENT_GESTURE = ecore_event_type_new();
262         MMI_PROVIDER_EVENT_VOICE = ecore_event_type_new();
263         MMI_PROVIDER_EVENT_VISION = ecore_event_type_new();
264         MMI_PROVIDER_EVENT_SCREEN_ANALYZER = ecore_event_type_new();
265         MMI_VISION_EVENT_PROPAGATE = ecore_event_type_new();
266         MMI_VISION_EVENT_DROP = ecore_event_type_new();
267         MMI_VISION_EVENT_FINISH = ecore_event_type_new();
268         MMI_EVENT_REFRESH_SCREEN_INFORM = ecore_event_type_new();
269
270         LOGD("MMI_PROVIDER_EVENT_KEY=%d\n", MMI_PROVIDER_EVENT_KEY);
271         LOGD("MMI_PROVIDER_EVENT_GESTURE=%d\n", MMI_PROVIDER_EVENT_GESTURE);
272         LOGD("MMI_PROVIDER_EVENT_VOICE=%d\n", MMI_PROVIDER_EVENT_VOICE);
273         LOGD("MMI_PROVIDER_EVENT_VISION=%d\n", MMI_PROVIDER_EVENT_VISION);
274         LOGD("MMI_PROVIDER_EVENT_SCREEN_ANALYZER=%d\n", MMI_PROVIDER_EVENT_SCREEN_ANALYZER);
275
276         //For debugging
277         _provider_event_handlers[0] = ecore_event_handler_add(MMI_PROVIDER_EVENT_KEY,
278                         _key_provider_event_cb, NULL);
279         _provider_event_handlers[1] = ecore_event_handler_add(MMI_PROVIDER_EVENT_GESTURE,
280                         _gesture_provider_event_cb, NULL);
281         _provider_event_handlers[2] = ecore_event_handler_add(MMI_PROVIDER_EVENT_VOICE,
282                         _voice_provider_event_cb, NULL);
283         _provider_event_handlers[3] = ecore_event_handler_add(MMI_PROVIDER_EVENT_VISION,
284                         _vision_provider_event_cb, NULL);
285         _provider_event_handlers[4] = ecore_event_handler_add(MMI_PROVIDER_EVENT_SCREEN_ANALYZER,
286                         _screen_analyzer_provider_event_cb, NULL);
287 }
288
289 static void
290 _event_handler_shutdown()
291 {
292         //For debugging
293         ecore_event_handler_del(_provider_event_handlers[0]);
294         ecore_event_handler_del(_provider_event_handlers[1]);
295         ecore_event_handler_del(_provider_event_handlers[2]);
296         ecore_event_handler_del(_provider_event_handlers[3]);
297         ecore_event_handler_del(_provider_event_handlers[4]);
298         _provider_event_handlers[0] = NULL;
299         _provider_event_handlers[1] = NULL;
300         _provider_event_handlers[2] = NULL;
301         _provider_event_handlers[3] = NULL;
302         _provider_event_handlers[4] = NULL;
303
304         MMI_PROVIDER_EVENT_KEY = -1;
305         MMI_PROVIDER_EVENT_GESTURE = -1;
306         MMI_PROVIDER_EVENT_VOICE = -1;
307         MMI_PROVIDER_EVENT_VISION = -1;
308         MMI_PROVIDER_EVENT_SCREEN_ANALYZER = -1;
309
310         LOGD("MMI_PROVIDER_EVENT_KEY=%d\n", MMI_PROVIDER_EVENT_KEY);
311         LOGD("MMI_PROVIDER_EVENT_GESTURE=%d\n", MMI_PROVIDER_EVENT_GESTURE);
312         LOGD("MMI_PROVIDER_EVENT_VOICE=%d\n", MMI_PROVIDER_EVENT_VOICE);
313         LOGD("MMI_PROVIDER_EVENT_VISION=%d\n", MMI_PROVIDER_EVENT_VISION);
314         LOGD("MMI_PROVIDER_EVENT_SCREEN_ANALYZER=%d\n", MMI_PROVIDER_EVENT_SCREEN_ANALYZER);
315 }
316
317 void
318 modality_providers_init(void)
319 {
320         LOGD("PROVIDER_PATH=%s\n", PROVIDER_PATH);
321
322         //Lookup and init modality providers
323         _event_handler_init();
324         _modality_providers_lookup();
325 }
326
327 void
328 modality_providers_shutdown(void)
329 {
330         _event_handler_shutdown();
331
332         //Do shutdown for intialized providers
333         if(!_provider_list)
334                 return;
335
336         Eina_List *l = NULL;
337         mmi_provider_handle *provider = NULL;
338         EINA_LIST_FOREACH(_provider_list, l, provider)
339         {
340                 if(provider->module_info)
341                 {
342                         if(provider->module_data)
343                                 provider->module_info->provider_deinit(provider->module_data);
344                         provider->module_data = NULL;
345                         provider->module_info = NULL;
346                 }
347                 if(provider->provider_info)
348                         dlclose(provider->provider_info);
349         }
350
351         _provider_list = eina_list_free(_provider_list);
352         _provider_list = NULL;
353 }