135a1944285511f964321b07bb54a01a628ede67
[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 #include <limits.h>
28
29 pui_backend_ani_func *ani_func = NULL;
30 Eina_Hash *_animations_hash = NULL;
31 pui_backend_ani_data *g_ani_data = NULL;
32
33 #if 0
34 static double
35 _ani_backend_ease_function_get_intensity(pui_effect_func func, double interval)
36 {
37         double intensity = interval;
38         switch (func)
39         {
40                 case None:
41                         break;
42                 case Linear:
43                         break;
44                 case EaseInSine:
45                         intensity = 1 - cos(PI / 2 * interval);
46                         break;
47                 case EaseOutSine:
48                         intensity = sin(PI / 2 * interval);
49                         break;
50                 case EaseInQuart:
51                         intensity = interval * interval;
52                         break;
53                 case EaseOutQuart:
54                         intensity = interval * (2 - interval);
55                         break;
56                 default:
57                         break;
58         }
59
60         return intensity;
61 }
62
63 static unsigned int
64 _ani_backend_get_value(unsigned int end_frame, unsigned int start_frame, double interval)
65 {
66         double res = 0.0;
67
68         // interval: frame ratio between key frame to key frame
69         // end_frame and start_frame is key frame
70
71         res = (end_frame - start_frame) * interval + start_frame;
72
73         return res;
74 }
75 #endif
76
77 static pui_bool
78 _ani_backend_frame_cb(void *data, int serial)
79 {
80         pui_int_error e = PUI_INT_ERROR_NONE;
81         pui_ani_t *ani = (pui_ani_t *)data;
82         pui_backend_ani_data *ani_data = NULL;
83         pui_ani_control_buffer *buffer = NULL;
84         double now;
85
86         ani_data = pui_backend_ani_get_ani_data(ani);
87         default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
88
89         now = ecore_time_unix_get();
90
91         pui_info("[time:%.3f] serial=%d\n", now, serial);
92
93         /* TODO : make use of ani_info */
94         (void) ani_info;
95
96         buffer = pui_backend_ani_get_buffer(ani);
97
98         if (!buffer)
99         {
100                 pui_err("Failed to get buffer !\n");
101                 return 0;
102         }
103
104         /* test example */
105         for(int i = 0; i<12; i++)
106         {
107                 buffer->ptr[4*i] = 0;
108                 buffer->ptr[4*i + 1] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_RED) >> 16;//R
109                 buffer->ptr[4*i + 2] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_GREEN) >> 8;//G
110                 buffer->ptr[4*i + 3] = ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_BLUE;//B
111         }
112
113         e = pui_backend_ani_set_buffer(ani, buffer);
114
115         if (e != PUI_INT_ERROR_NONE)
116         {
117                 pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
118                 return (pui_bool)0;
119         }
120
121         e = pui_backend_ani_update(ani);
122
123         if (e != PUI_INT_ERROR_NONE)
124         {
125                 pui_err("Failed on updating animation !(e=%d)\n", e);
126                 return (pui_bool)0;
127         }
128
129         pui_info("... update (serial=%d)\n", serial);
130
131         return (pui_bool)1;
132 }
133
134 default_ani_info *
135 get_ani_info_from_ani_collection(pui_id id)
136 {
137         default_ani_info *ani_info = NULL;
138
139         if (!_animations_hash)
140                 return NULL;
141
142         pui_info("... id: %s\n", id);
143
144         ani_info = eina_hash_find(_animations_hash, id);
145
146         if (!ani_info)
147         {
148                 pui_err("ani_info has NOT been found ! (id:%s)\n", id);
149                 return NULL;
150         }
151
152         pui_info("ani_info has been found ! (id:%s)\n", id);
153
154         return ani_info;
155 }
156
157 pui_error
158 _ani_start(pui_ani_t *ani, int repeat)
159 {
160         pui_bool ret = 0;
161         pui_int_error e = PUI_INT_ERROR_NONE;
162         pui_backend_ani_data *ani_data = NULL;
163
164         ani_data = pui_backend_ani_get_ani_data(ani);
165         default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
166
167         pui_info("... info->id: %s, repeat : %d\n", ani_info->id, repeat);
168
169         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
170         ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_frame_cb, (double)ani_info->interval / 1000);
171
172         if (!ret)
173         {
174                 pui_err("Failed to add frame callback !\n");
175                 e = PUI_INT_ERROR_INVALID_RESOURCES;
176         }
177
178         return e;
179 }
180
181 pui_error
182 _ani_stop(pui_ani_t *ani, pui_bool force)
183 {
184         pui_int_error e = PUI_INT_ERROR_NONE;
185         pui_backend_ani_data *ani_data = NULL;
186
187         ani_data = pui_backend_ani_get_ani_data(ani);
188         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
189
190         //TODO
191         (void) info;
192
193         pui_info("... info->id: %s, force=%d\n", info->id, force);
194
195         pui_backend_ani_remove_frame_cb(ani);
196
197         if (force)
198                 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_PAUSED);
199         else
200                 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
201
202         return e;
203 }
204
205 static char *
206 _read_json_file(const char *path, int *data_size)
207 {
208         FILE *fp = fopen(path, "rb");
209         int size, ret;
210         char *buffer = NULL;
211         ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
212
213         ret = fseek(fp, 0, SEEK_END);
214         ERROR_CHECK(ret == 0, goto error, "Failed to seek file. ret: %d\n", ret);
215         size = (long int)ftell(fp);
216         ERROR_CHECK(0 < size && size < INT_MAX, goto error, "Invalid file: %d size\n", size);
217         ret = fseek(fp, 0, SEEK_SET);
218         ERROR_CHECK(ret == 0, goto error, "Failed to seek file. ret: %d\n", ret);
219
220         buffer = (char *)calloc(sizeof(char), size + 1);
221         ERROR_CHECK(buffer, goto error, "Failed to allocate memory for buffer\n");
222
223         if (fread(buffer, size, 1, fp) < 1) {
224                 goto error;
225         }
226
227         *data_size = size;
228         fclose(fp);
229         return buffer;
230
231 error:
232         *data_size = 0;
233         if (buffer) free(buffer);
234         fclose(fp);
235         return NULL;
236 }
237
238 static default_ani_info *
239 _read_json(const char *path)
240 {
241         char *buffer, *type;
242         json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
243         default_ani_info *ani_info = NULL;
244         int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
245         int data_size = 0, i, j;
246
247         buffer = _read_json_file(path, &data_size);
248         ERROR_CHECK(buffer && data_size > 0, goto error, "File %s has no data\n", path);
249         root_obj = json_tokener_parse(buffer);
250         ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
251
252         ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
253         ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
254
255         data_obj = json_object_object_get(root_obj, "type");
256         //printf("type: %s\n", json_object_get_string(data_obj));
257         type = (char *)json_object_get_string(data_obj);
258         ani_info->id = strndup(type, strlen(type));
259
260         data_obj = json_object_object_get(root_obj, "interval");
261         //printf("interval: %d\n", json_object_get_int(data_obj));
262         ani_info->interval = json_object_get_int(data_obj);
263
264         data_obj = json_object_object_get(root_obj, "frame");
265         frame_len = json_object_array_length(data_obj);
266
267         ani_info->num_key_frames = frame_len;
268         ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
269         ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
270
271         for (i = 0; i < frame_len; i++) {
272                 frame_obj = json_object_array_get_idx(data_obj, i);
273
274                 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
275                 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
276                 frame_id = json_object_get_int(frame_data_obj);
277
278                 frame_data_obj = json_object_object_get(frame_obj, "frame_duration");
279                 if (frame_data_obj)
280                         ani_info->frames[frame_id - 1].frame_duration = json_object_get_int(frame_data_obj);
281                 else
282                         ani_info->frames[frame_id - 1].frame_duration = ani_info->interval;
283
284                 frame_data_obj = json_object_object_get(frame_obj, "led");
285                 led_len = json_object_array_length(frame_data_obj);
286
287                 ani_info->frames[frame_id - 1].num_led = led_len;
288                 ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
289                 ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
290
291                 for (j = 0; j < led_len; j++) {
292                         led_obj = json_object_array_get_idx(frame_data_obj, j);
293
294                         led_data_obj = json_object_object_get(led_obj, "id");
295                         //printf("\t\tid: %2d  ", json_object_get_int(led_data_obj));
296                         led_id = json_object_get_int(led_data_obj);
297
298                         led_data_obj = json_object_object_get(led_obj, "color");
299                         //printf("color: %s\n", json_object_get_string(led_data_obj));
300                         ani_info->frames[frame_id - 1].leds[led_id - 1].color =
301                                 strtol(json_object_get_string(led_data_obj), NULL, 16);
302                 }
303         }
304
305         free(buffer);
306         return ani_info;
307
308 error:
309         if (buffer) free(buffer);
310         if (ani_info) {
311                 if (ani_info->frames) {
312                         for (i = 0; i < ani_info->num_key_frames; i++) {
313                                 if (ani_info->frames[i].leds)
314                                         free(ani_info->frames[i].leds);
315                         }
316                         free(ani_info->frames);
317                 }
318                 free(ani_info->id);
319                 free(ani_info);
320         }
321         return NULL;
322 }
323
324 int
325 _find_directory(const char *path, Eina_List **json_list)
326 {
327         DIR *dir;
328         struct dirent *cur;
329         int count = 0, ret_cnt = 0;
330
331         dir = opendir(path);
332         if (!dir)
333         {
334                 pui_err("Failed to open %s.\n", path);
335                 return count;
336         }
337
338         while ((cur = readdir(dir)) != NULL)
339         {
340                 char next_path[MAX_STR] = {0, };
341                 if (cur->d_type == DT_DIR)
342                 {
343                         if (!strncmp(cur->d_name, ".", sizeof(".")) ||
344                                 !strncmp(cur->d_name, "..", sizeof("..")))
345                                 continue;
346
347                         snprintf(next_path, MAX_STR, "%s/%s", path, cur->d_name);
348                         ret_cnt = _find_directory(next_path, json_list);
349                         count += ret_cnt;
350                 }
351                 else
352                 {
353                         if (strstr(cur->d_name, ".json"))
354                         {
355                                 snprintf(next_path, MAX_STR, "%s/%s", path, cur->d_name);
356                                 *json_list = eina_list_append(*json_list, eina_stringshare_add(next_path));
357                                 count++;
358                         }
359                 }
360         }
361         closedir(dir);
362
363         return count;
364 }
365
366
367 pui_int_error
368 _create_ani_collection(void)
369 {
370         pui_int_error e = PUI_INT_ERROR_NONE;
371         default_ani_info *ani_info;
372         Eina_List *json_list = NULL;
373         Eina_List *l, *l_next;
374         Eina_Stringshare *path;
375
376         // load and store all of animation data
377
378         _find_directory(ANI_COLLECTION_DIR, &json_list);
379
380         EINA_LIST_FOREACH_SAFE(json_list, l, l_next, path)
381         {
382                 ani_info = _read_json(path);
383                 if (ani_info) {
384                         eina_hash_add(_animations_hash, ani_info->id, ani_info);
385                         pui_info("Success to load %s animation (id: %s)\n", path, ani_info->id);
386                 }
387                 eina_stringshare_del(path);
388                 json_list = eina_list_remove_list(json_list, l);
389         }
390
391         //TODO
392
393         return e;
394 }
395
396 pui_bool
397 _geometry_get(int *width, int *height)
398 {
399         if (!width || !height)
400                 return 0;
401
402         if (width)
403                 *width = DEFAULT_BACKEND_GEOM_WIDTH;
404         if (height)
405                 *height = DEFAULT_BACKEND_GEOM_HEIGHT;
406
407         return 1;
408 }
409
410 pui_int_error
411 _is_ani_supported(pui_id id)
412 {
413         pui_int_error e = PUI_INT_ERROR_NONE;
414         default_ani_info *ani_info;
415
416         //TODO
417         /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
418         if (!id) {
419                 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
420                 return e;
421         }
422         ani_info = eina_hash_find(_animations_hash, id);
423
424         if (!ani_info)
425                 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
426
427         return e;
428 }
429
430 static void
431 _ani_info_cleanup(default_ani_info *ani_info)
432 {
433         int i;
434
435         if (!ani_info)
436                 return;
437
438         if (ani_info->frames)
439         {
440                 for (i = 0; i < ani_info->num_key_frames; i++)
441                 {
442                         if (ani_info->frames[i].leds)
443                                 free(ani_info->frames[i].leds);
444                 }
445
446                 free(ani_info->frames);
447         }
448
449         free(ani_info->id);
450 }
451
452 pui_backend_ani_data *
453 _ani_create(pui_id id)
454 {
455         pui_backend_ani_data *ani_data = NULL;
456         pui_backend_ani_func *ani_func = NULL;
457
458         /* backend's animation specific info */
459         default_ani_info *ani_info = NULL;
460
461         //TODO : return NULL if the animation correspond to the given id dones't exist.
462         if (PUI_INT_ERROR_NONE != _is_ani_supported(id))
463         {
464                 pui_err("The animation(%s) doesn't supported !\n", id);
465                 return NULL;
466         }
467
468         /* allocation of the structure of function pointers that will be called from pui_ani_control() */
469         ani_func = pui_backend_ani_alloc_ani_func();
470
471         if (!ani_func)
472         {
473                 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
474                 return NULL;
475         }
476
477         /* get animation info associate with the given id from animation collection */
478         ani_info = get_ani_info_from_ani_collection(id);
479
480         if (!ani_info)
481         {
482                 pui_err("Failed to get ani info from animation collection !\n");
483                 goto err;
484         }
485
486         /* allocate backend ani_data and return it to pui ani core */
487         ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
488
489         if (!ani_data)
490         {
491                 pui_err("Failed to allocate memory for pui backend ani data !\n");
492                 goto err;
493         }
494
495         /* Assign each function pointer that corresponds to the given id if needed. */
496         if (!strncmp(ani_info->id, "system/easy_setup", sizeof("system/easy_setup")))
497         {
498                 pui_default_backend_ani_easysetup_func_set(ani_func);
499         }
500         else if (!strncmp(ani_info->id, "system/processing", sizeof("system/processing")))
501         {
502                 pui_default_backend_ani_system_processing_func_set(ani_func);
503         }
504         else if (!strncmp(ani_info->id, "system/sw_update_done", sizeof("system/sw_update_done")))
505         {
506                 pui_default_backend_ani_swupdatedone_func_set(ani_func);
507         }
508         else if (!strncmp(ani_info->id, "system/mic_off", sizeof("system/mic_off")))
509         {
510                 pui_default_backend_ani_micoff_func_set(ani_func);
511         }
512         else if (!strncmp(ani_info->id, "voice/listening", sizeof("voice/listening")))
513         {
514                 pui_default_backend_ani_listening_func_set(ani_func);
515         }
516         else if (!strncmp(ani_info->id, "voice/streaming", sizeof("voice/streaming")))
517         {
518                 pui_default_backend_ani_streaming_func_set(ani_func);
519         }
520         else if (!strncmp(ani_info->id, "voice/processing", sizeof("voice/processing")))
521         {
522                 pui_default_backend_ani_processing_func_set(ani_func);
523         }
524         else if (!strncmp(ani_info->id, "voice/speaking", sizeof("voice/speaking")))
525         {
526                 pui_default_backend_ani_speaking_func_set(ani_func);
527         }
528         else if (!strncmp(ani_info->id, "voice/timeout", sizeof("voice/timeout")))
529         {
530                 pui_default_backend_ani_timeout_func_set(ani_func);
531         }
532         else if (!strncmp(ani_info->id, "notification/normal", sizeof("notification/normal")))
533         {
534                 pui_default_backend_ani_normal_func_set(ani_func);
535         }
536         else if (!strncmp(ani_info->id, "notification/emergency", sizeof("notification/emergency")))
537         {
538                 pui_default_backend_ani_emergency_func_set(ani_func);
539         }
540         else if (!strncmp(ani_info->id, "notification/network_error", sizeof("notification/network_error")))
541         {
542                 pui_default_backend_ani_networkerror_func_set(ani_func);
543         }
544         else if (!strncmp(ani_info->id, "notification/error", sizeof("notification/error")))
545         {
546                 pui_default_backend_ani_error_func_set(ani_func);
547         }
548         else if (!strncmp(ani_info->id, "notification/alarm", sizeof("notification/alarm")))
549         {
550                 pui_default_backend_ani_alarm_func_set(ani_func);
551         }
552         else if (!strncmp(ani_info->id, "bt/pairing", sizeof("bt/pairing")))
553         {
554                 pui_default_backend_ani_pairing_func_set(ani_func);
555         }
556         else if (!strncmp(ani_info->id, "bt/connected", sizeof("bt/connected")))
557         {
558                 pui_default_backend_ani_connected_func_set(ani_func);
559         }
560         else
561         {
562                 pui_info("%s animation has no animation handler, using default handler\n", ani_info->id);
563                 /* Assign each function pointer that corresponds to the given id if needed. */
564                 ani_func->ani_start = _ani_start;
565                 ani_func->ani_stop = _ani_stop;
566         }
567
568         ani_data->ani_func = ani_func;
569         ani_data->ani_info = (pui_backend_ani_info *)ani_info;
570
571         g_ani_data = ani_data;
572
573         return ani_data;
574
575 err:
576         if (ani_func)
577         {
578                 pui_backend_ani_free_ani_func(ani_func);
579                 ani_func = NULL;
580         }
581
582         return NULL;
583 }
584
585 void
586 _ani_destroy(pui_backend_ani_data *ani_data)
587 {
588         if (!ani_data)
589                 return;
590
591         if (ani_data->ani_func)
592         {
593                 pui_backend_ani_free_ani_func(ani_data->ani_func);
594                 ani_data->ani_func = NULL;
595         }
596
597         ani_data->ani_info = NULL;
598         g_ani_data = NULL;
599 }
600
601 static void
602 _animation_data_free_cb(void *data)
603 {
604         default_ani_info *ani_info = (default_ani_info *)data;
605
606         _ani_info_cleanup(ani_info);
607 }
608
609 static pui_backend_module_data *
610 pui_default_backend_init(void)
611 {
612         pui_backend_module_data *backend_data = NULL;
613
614         backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
615
616         if (!backend_data)
617         {
618                 pui_err("Failed to allocate memory for pui backend module data !\n");
619                 return NULL;
620         }
621
622         backend_data->create_ani_collection = _create_ani_collection;
623         backend_data->geometry_get = _geometry_get;
624         backend_data->ani_create = _ani_create;
625         backend_data->ani_destroy = _ani_destroy;
626
627         /* Allocate backend specific data if needed. Now it will be empty. */
628         backend_data->data = NULL;
629         _animations_hash = eina_hash_string_superfast_new(NULL);
630
631         return backend_data;
632 }
633
634 static void
635 pui_default_backend_deinit(pui_backend_module_data *backend_data)
636 {
637         Eina_Iterator *it;
638         default_ani_info *ani_info = NULL;
639
640         if (!backend_data)
641                 return;
642
643         if (backend_data->data)
644         {
645                 //TODO : free variables of backend_data
646
647                 free(backend_data->data);
648         }
649
650         if (g_ani_data)
651         {
652                 if (g_ani_data->ani_func)
653                 {
654                         pui_backend_ani_free_ani_func(g_ani_data->ani_func);
655                         g_ani_data->ani_func = NULL;
656                 }
657
658                 g_ani_data->ani_info = NULL;
659         }
660
661         if (_animations_hash)
662         {
663                 it = eina_hash_iterator_data_new(_animations_hash);
664
665                 EINA_ITERATOR_FOREACH(it, ani_info)
666                 {
667                         _animation_data_free_cb(ani_info);
668                         free(ani_info);
669                 }
670
671                 eina_iterator_free(it);
672
673                 eina_hash_free(_animations_hash);
674                 _animations_hash = NULL;
675         }
676
677         backend_data->create_ani_collection = NULL;
678         backend_data->geometry_get = NULL;
679         backend_data->ani_create = NULL;
680         backend_data->ani_destroy = NULL;
681
682         free(backend_data);
683         backend_data = NULL;
684 }
685
686 pui_backend_module pui_backend_module_info = {
687         "Tizen Reference Speaker Backend",
688         "Samsung",
689         PUI_BACKEND_SET_ABI_VERSION(1, 0),
690         pui_default_backend_init,
691         pui_default_backend_deinit
692 };
693