defaul_backend: implement some animations - listening, processing, speaking, streaming
[platform/core/uifw/libpui.git] / backends / default_backend.c
1 /*
2  * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "default_backend.h"
27
28 pui_backend_ani_func *ani_func = NULL;
29 Eina_Hash *_animations_hash = NULL;
30 pui_backend_ani_data *g_ani_data = NULL;
31
32 #if 0
33 static double
34 _ani_backend_ease_function_get_intensity(pui_effect_func func, double interval)
35 {
36         double intensity = interval;
37         switch (func)
38         {
39                 case None:
40                         break;
41                 case Linear:
42                         break;
43                 case EaseInSine:
44                         intensity = 1 - cos(PI / 2 * interval);
45                         break;
46                 case EaseOutSine:
47                         intensity = sin(PI / 2 * interval);
48                         break;
49                 case EaseInQuart:
50                         intensity = interval * interval;
51                         break;
52                 case EaseOutQuart:
53                         intensity = interval * (2 - interval);
54                         break;
55                 default:
56                         break;
57         }
58
59         return intensity;
60 }
61
62 static unsigned int
63 _ani_backend_get_value(unsigned int end_frame, unsigned int start_frame, double interval)
64 {
65         double res = 0.0;
66
67         // interval: frame ratio between key frame to key frame
68         // end_frame and start_frame is key frame
69
70         res = (end_frame - start_frame) * interval + start_frame;
71
72         return res;
73 }
74 #endif
75
76 static pui_bool
77 _ani_backend_frame_cb(void *data, int serial)
78 {
79         pui_int_error e = PUI_INT_ERROR_NONE;
80         pui_ani_t *ani = (pui_ani_t *)data;
81         pui_backend_ani_data *ani_data = NULL;
82         pui_ani_control_buffer *buffer = NULL;
83         double now;
84
85         ani_data = pui_backend_ani_get_ani_data(ani);
86         default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
87
88         now = ecore_time_unix_get();
89
90         pui_info("[time:%.3f] serial=%d\n", now, serial);
91
92         /* TODO : make use of ani_info */
93         (void) ani_info;
94
95         buffer = pui_backend_ani_get_buffer(ani);
96
97         if (!buffer)
98         {
99                 pui_err("Failed to get buffer !\n");
100                 return 0;
101         }
102
103         /* test example */
104         for(int i = 0; i<12; i++)
105         {
106                 buffer->ptr[4*i] = 0;
107                 buffer->ptr[4*i + 1] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_RED) >> 16;//R
108                 buffer->ptr[4*i + 2] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_GREEN) >> 8;//G
109                 buffer->ptr[4*i + 3] = ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_BLUE;//B
110         }
111
112         e = pui_backend_ani_set_buffer(ani, buffer);
113
114         if (e != PUI_INT_ERROR_NONE)
115         {
116                 pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
117                 return (pui_bool)0;
118         }
119
120         e = pui_backend_ani_update(ani);
121
122         if (e != PUI_INT_ERROR_NONE)
123         {
124                 pui_err("Failed on updating animation !(e=%d)\n", e);
125                 return (pui_bool)0;
126         }
127
128         pui_info("... update (serial=%d)\n", serial);
129
130         return (pui_bool)1;
131 }
132
133 default_ani_info *
134 get_ani_info_from_ani_collection(pui_id id)
135 {
136         default_ani_info *ani_info = NULL;
137
138         if (!_animations_hash)
139                 return NULL;
140
141         pui_info("... id: %s\n", id);
142
143         ani_info = eina_hash_find(_animations_hash, id);
144
145         if (!ani_info)
146         {
147                 pui_err("ani_info has NOT been found ! (id:%s)\n", id);
148                 return NULL;
149         }
150
151         pui_info("ani_info has been found ! (id:%s)\n", id);
152
153         return ani_info;
154 }
155
156 pui_error
157 _ani_start(pui_ani_t *ani, int repeat)
158 {
159         pui_bool ret = 0;
160         pui_int_error e = PUI_INT_ERROR_NONE;
161         pui_backend_ani_data *ani_data = NULL;
162
163         ani_data = pui_backend_ani_get_ani_data(ani);
164         default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
165
166         pui_info("... info->id: %s, repeat : %d\n", ani_info->id, repeat);
167
168         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
169         ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_frame_cb, (double)ani_info->interval / 1000);
170
171         if (!ret)
172         {
173                 pui_err("Failed to add frame callback !\n");
174                 e = PUI_INT_ERROR_INVALID_RESOURCES;
175         }
176
177         return e;
178 }
179
180 pui_error
181 _ani_stop(pui_ani_t *ani)
182 {
183         pui_int_error e = PUI_INT_ERROR_NONE;
184         pui_backend_ani_data *ani_data = NULL;
185
186         ani_data = pui_backend_ani_get_ani_data(ani);
187         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
188
189         //TODO
190         (void) info;
191
192         pui_info("... info->id: %s\n", info->id);
193
194         pui_backend_ani_remove_frame_cb(ani);
195         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
196
197         return e;
198 }
199
200 static char *
201 _read_json_file(const char *path, int *data_size)
202 {
203         FILE *fp = fopen(path, "rb");
204         int size;
205         char *buffer;
206         ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
207
208         fseek(fp, 0, SEEK_END);
209         size = ftell(fp);
210         fseek(fp, 0, SEEK_SET);
211
212         buffer = (char *)calloc(sizeof(char), size + 1);
213
214         if (fread(buffer, size, 1, fp) < 1) {
215                 *data_size = 0;
216                 free(buffer);
217                 fclose(fp);
218                 return NULL;
219         }
220
221         *data_size = size;
222         fclose(fp);
223         return buffer;
224 }
225
226 static default_ani_info *
227 _read_json(const char *path)
228 {
229         char *buffer, *type;
230         json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
231         default_ani_info *ani_info = NULL;
232         int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
233         int data_size = 0, i, j;
234
235         buffer = _read_json_file(path, &data_size);
236         ERROR_CHECK(buffer && data_size > 0, return EINA_FALSE, "File %s has no data\n", path);
237         root_obj = json_tokener_parse(buffer);
238         ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
239
240         ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
241         ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
242
243         data_obj = json_object_object_get(root_obj, "type");
244         //printf("type: %s\n", json_object_get_string(data_obj));
245         type = (char *)json_object_get_string(data_obj);
246         ani_info->id = strndup(type, strlen(type));
247
248         data_obj = json_object_object_get(root_obj, "interval");
249         //printf("interval: %d\n", json_object_get_int(data_obj));
250         ani_info->interval = json_object_get_int(data_obj);
251
252         data_obj = json_object_object_get(root_obj, "frame");
253         frame_len = json_object_array_length(data_obj);
254
255         ani_info->num_key_frames = frame_len;
256         ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
257         ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
258         
259         for (i = 0; i < frame_len; i++) {
260                 frame_obj = json_object_array_get_idx(data_obj, i);
261
262                 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
263                 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
264                 frame_id = json_object_get_int(frame_data_obj);
265
266                 frame_data_obj = json_object_object_get(frame_obj, "led");
267                 led_len = json_object_array_length(frame_data_obj);
268                 
269                 ani_info->frames[frame_id - 1].num_led = led_len;
270                 ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
271                 ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
272                 
273                 for (j = 0; j < led_len; j++) {
274                         led_obj = json_object_array_get_idx(frame_data_obj, j);
275
276                         led_data_obj = json_object_object_get(led_obj, "id");
277                         //printf("\t\tid: %2d  ", json_object_get_int(led_data_obj));
278                         led_id = json_object_get_int(led_data_obj);
279
280                         led_data_obj = json_object_object_get(led_obj, "color");
281                         //printf("color: %s\n", json_object_get_string(led_data_obj));
282                         ani_info->frames[frame_id - 1].leds[led_id - 1].color =
283                                 strtol(json_object_get_string(led_data_obj), NULL, 16);
284                 }
285         }
286
287         free(buffer);
288         return ani_info;
289
290 error:
291         free(buffer);
292         if (ani_info) {
293                 if (ani_info->frames) {
294                         for (i = 0; i < ani_info->num_key_frames; i++) {
295                                 if (ani_info->frames[i].leds)
296                                         free(ani_info->frames[i].leds);
297                         }
298                         free(ani_info->frames);
299                 }
300                 free(ani_info->id);
301                 free(ani_info);
302         }
303         return NULL;
304 }
305
306 static int
307 _scandir_filter(const struct dirent *dirent)
308 {
309         if (!strncmp(dirent->d_name, ".", sizeof(".")) ||
310                 !strncmp(dirent->d_name, "..", sizeof("..")))
311                 return 0;
312         if (!strstr(dirent->d_name, ".json"))
313                 return 0;
314
315         return 1;
316 }
317
318
319 pui_int_error
320 _create_ani_collection(void)
321 {
322         pui_int_error e = PUI_INT_ERROR_NONE;
323         default_ani_info *ani_info;
324         struct dirent **files;
325         int count, i;
326         char file_path[MAX_STR] = {0, };
327
328         // load and store all of animation data
329
330         if ((count = scandir(ANI_COLLECTION_DIR, &files, _scandir_filter, alphasort)) == -1) {
331                 printf("Failed to get %s directory (%s)\n", ANI_COLLECTION_DIR, strerror(errno));
332                 e = PUI_INT_ERROR_INVALID_RESOURCES;
333                 return e;
334         }
335
336         for (i = 0; i < count; i++) {
337                 snprintf(file_path, sizeof(file_path), "%s%s", ANI_COLLECTION_DIR, files[i]->d_name);
338
339                 ani_info = _read_json(file_path);
340                 if (ani_info) {
341                         eina_hash_add(_animations_hash, ani_info->id, ani_info);
342                         printf("Success to load %s animation\n", files[i]->d_name);
343                 }
344         }
345
346         for (i = 0; i < count; i++) {
347                 free(files[i]);
348         }
349         free(files);
350
351         //TODO
352
353         return e;
354 }
355
356 pui_int_error
357 _is_ani_supported(pui_id id)
358 {
359         pui_int_error e = PUI_INT_ERROR_NONE;
360         default_ani_info *ani_info;
361
362         //TODO
363         /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
364         if (!id) {
365                 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
366                 return e;
367         }
368         ani_info = eina_hash_find(_animations_hash, id);
369
370         if (!ani_info)
371                 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
372
373         return e;
374 }
375
376 static void
377 _ani_info_cleanup(default_ani_info *ani_info)
378 {
379         int i;
380
381         if (!ani_info)
382                 return;
383
384         if (ani_info->frames)
385         {
386                 for (i = 0; i < ani_info->num_key_frames; i++)
387                 {
388                         if (ani_info->frames[i].leds)
389                                 free(ani_info->frames[i].leds);
390                 }
391
392                 free(ani_info->frames);
393         }
394
395         free(ani_info->id);
396 }
397
398 pui_backend_ani_data *
399 _ani_create(pui_id id)
400 {
401         pui_backend_ani_data *ani_data = NULL;
402         pui_backend_ani_func *ani_func = NULL;
403
404         /* backend's animation specific info */
405         default_ani_info *ani_info = NULL;
406
407         //TODO : return NULL if the animation correspond to the given id dones't exist.
408         if (PUI_INT_ERROR_NONE != _is_ani_supported(id))
409         {
410                 pui_err("The animation(%s) doesn't supported !\n", id);
411                 return NULL;
412         }
413
414         /* allocation of the structure of function pointers that will be called from pui_ani_control() */
415         ani_func = pui_backend_ani_alloc_ani_func();
416
417         if (!ani_func)
418         {
419                 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
420                 return NULL;
421         }
422
423         /* get animation info associate with the given id from animation collection */
424         ani_info = get_ani_info_from_ani_collection(id);
425         
426         if (!ani_info)
427         {
428                 pui_err("Failed to get ani info from animation collection !\n");
429                 goto err;
430         }
431
432         /* allocate backend ani_data and return it to pui ani core */
433         ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
434         
435         if (!ani_data)
436         {
437                 pui_err("Failed to allocate memory for pui backend ani data !\n");
438                 goto err;
439         }
440
441         /* Assign each function pointer that corresponds to the given id if needed. */
442         if (!strncmp(ani_info->id, "processing", sizeof("processing")))
443         {
444                 pui_default_backend_ani_processing_func_set(ani_func);
445         }
446         else if (!strncmp(ani_info->id, "emergency", sizeof("emergency")))
447         {
448                 pui_default_backend_ani_emergency_func_set(ani_func);
449         }
450         else if (!strncmp(ani_info->id, "listening", sizeof("listening")))
451         {
452                 pui_default_backend_ani_listening_func_set(ani_func);
453         }
454         else if (!strncmp(ani_info->id, "speaking", sizeof("speaking")))
455         {
456                 pui_default_backend_ani_speaking_func_set(ani_func);
457         }
458         else if (!strncmp(ani_info->id, "streaming", sizeof("streaming")))
459         {
460                 pui_default_backend_ani_streaming_func_set(ani_func);
461         }
462         else
463         {
464                 pui_info("%s animation has no animation handler, using default handler\n", ani_info->id);
465                 /* Assign each function pointer that corresponds to the given id if needed. */
466                 ani_func->ani_start = _ani_start;
467                 ani_func->ani_stop = _ani_stop;
468         }
469         
470         ani_data->ani_func = ani_func;
471         ani_data->ani_info = (pui_backend_ani_info *)ani_info;
472
473         g_ani_data = ani_data;
474
475         return ani_data;
476
477 err:
478         if (ani_func)
479         {
480                 pui_backend_ani_free_ani_func(ani_func);
481                 ani_func = NULL;
482         }
483
484         return NULL;
485 }
486
487 void
488 _ani_destroy(pui_backend_ani_data *ani_data)
489 {
490         if (!ani_data)
491                 return;
492
493         if (ani_data->ani_func)
494         {
495                 pui_backend_ani_free_ani_func(ani_data->ani_func);
496                 ani_data->ani_func = NULL;
497         }
498
499         ani_data->ani_info = NULL;
500         g_ani_data = NULL;
501 }
502
503 static void
504 _animation_data_free_cb(void *data)
505 {
506         default_ani_info *ani_info = (default_ani_info *)data;
507
508         _ani_info_cleanup(ani_info);
509 }
510
511 static pui_backend_module_data *
512 pui_default_backend_init(void)
513 {
514         pui_backend_module_data *backend_data = NULL;
515
516         backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
517
518         if (!backend_data)
519         {
520                 pui_err("Failed to allocate memory for pui backend module data !\n");
521                 return NULL;
522         }
523
524         backend_data->create_ani_collection = _create_ani_collection;
525         backend_data->ani_create = _ani_create;
526         backend_data->ani_destroy = _ani_destroy;
527
528         /* Allocate backend specific data if needed. Now it will be empty. */
529         backend_data->data = NULL;
530         _animations_hash = eina_hash_string_superfast_new(NULL);
531
532         return backend_data;
533 }
534
535 static void
536 pui_default_backend_deinit(pui_backend_module_data *backend_data)
537 {
538         Eina_Iterator *it;
539         default_ani_info *ani_info = NULL;
540
541         if (!backend_data)
542                 return;
543
544         if (backend_data->data)
545         {
546                 //TODO : free variables of backend_data
547
548                 free(backend_data->data);
549         }
550
551         if (g_ani_data)
552         {
553                 if (g_ani_data->ani_func)
554                 {
555                         pui_backend_ani_free_ani_func(g_ani_data->ani_func);
556                         g_ani_data->ani_func = NULL;
557                 }
558
559                 g_ani_data->ani_info = NULL;
560         }
561
562         if (_animations_hash)
563         {
564                 it = eina_hash_iterator_data_new(_animations_hash);
565
566                 EINA_ITERATOR_FOREACH(it, ani_info)
567                 {
568                         _animation_data_free_cb(ani_info);
569                         free(ani_info);
570                 }
571
572                 eina_iterator_free(it);
573
574                 eina_hash_free(_animations_hash);
575                 _animations_hash = NULL;
576         }
577
578         backend_data->create_ani_collection = NULL;
579         backend_data->ani_create = NULL;
580         backend_data->ani_destroy = NULL;
581
582         free(backend_data);
583         backend_data = NULL;
584 }
585
586 pui_backend_module pui_backend_module_info = {
587         "Tizen Reference Speaker Backend",
588         "Samsung",
589         PUI_BACKEND_SET_ABI_VERSION(1, 0),
590         pui_default_backend_init,
591         pui_default_backend_deinit
592 };
593