2 * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
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:
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.
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
26 #include "default_backend.h"
29 pui_backend_ani_func *ani_func = NULL;
30 Eina_Hash *_animations_hash = NULL;
31 pui_backend_ani_data *g_ani_data = NULL;
35 _ani_backend_ease_function_get_intensity(pui_effect_func func, double interval)
37 double intensity = interval;
45 intensity = 1 - cos(PI / 2 * interval);
48 intensity = sin(PI / 2 * interval);
51 intensity = interval * interval;
54 intensity = interval * (2 - interval);
64 _ani_backend_get_value(unsigned int end_frame, unsigned int start_frame, double interval)
68 // interval: frame ratio between key frame to key frame
69 // end_frame and start_frame is key frame
71 res = (end_frame - start_frame) * interval + start_frame;
78 _ani_backend_frame_cb(void *data, int serial)
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;
85 ani_data = pui_backend_ani_get_ani_data(ani);
86 default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
88 /* TODO : make use of ani_info */
91 buffer = pui_backend_ani_get_buffer(ani);
95 pui_err("Failed to get buffer !\n");
100 for(int i = 0; i<12; i++)
102 buffer->ptr[4*i] = 0;
103 buffer->ptr[4*i + 1] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_RED) >> 16;//R
104 buffer->ptr[4*i + 2] = (ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_GREEN) >> 8;//G
105 buffer->ptr[4*i + 3] = ani_info->frames[ani_info->frame_idx].leds[i].color & LED_MASK_BLUE;//B
108 e = pui_backend_ani_set_buffer(ani, buffer);
110 if (e != PUI_INT_ERROR_NONE)
112 pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
116 e = pui_backend_ani_update(ani);
118 if (e != PUI_INT_ERROR_NONE)
120 pui_err("Failed on updating animation !(e=%d)\n", e);
124 pui_info("... update (serial=%d)\n", serial);
130 get_ani_info_from_ani_collection(pui_id id)
132 default_ani_info *ani_info = NULL;
134 if (!_animations_hash)
137 pui_info("... id: %s\n", id);
139 ani_info = eina_hash_find(_animations_hash, id);
143 pui_err("ani_info has NOT been found ! (id:%s)\n", id);
147 pui_info("ani_info has been found ! (id:%s)\n", id);
153 _ani_start(pui_ani_t *ani, int repeat)
156 pui_int_error e = PUI_INT_ERROR_NONE;
157 pui_backend_ani_data *ani_data = NULL;
159 ani_data = pui_backend_ani_get_ani_data(ani);
160 default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
162 pui_info("... info->id: %s, repeat : %d\n", ani_info->id, repeat);
164 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
165 ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_frame_cb, (double)ani_info->interval / 1000);
169 pui_err("Failed to add frame callback !\n");
170 e = PUI_INT_ERROR_INVALID_RESOURCES;
177 _ani_stop(pui_ani_t *ani, pui_bool force)
179 pui_int_error e = PUI_INT_ERROR_NONE;
180 pui_backend_ani_data *ani_data = NULL;
182 ani_data = pui_backend_ani_get_ani_data(ani);
183 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
188 pui_info("... info->id: %s, force=%d\n", info->id, force);
190 pui_backend_ani_remove_frame_cb(ani);
193 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_PAUSED);
195 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
201 _read_json_file(const char *path, int *data_size)
203 FILE *fp = fopen(path, "rb");
206 ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
208 ret = fseek(fp, 0, SEEK_END);
209 ERROR_CHECK(ret == 0, goto error, "Failed to seek file. ret: %d\n", ret);
210 size = (long int)ftell(fp);
211 ERROR_CHECK(0 < size && size < INT_MAX, goto error, "Invalid file: %d size\n", size);
212 ret = fseek(fp, 0, SEEK_SET);
213 ERROR_CHECK(ret == 0, goto error, "Failed to seek file. ret: %d\n", ret);
215 buffer = (char *)calloc(sizeof(char), size + 1);
216 ERROR_CHECK(buffer, goto error, "Failed to allocate memory for buffer\n");
218 if (fread(buffer, size, 1, fp) < 1) {
228 if (buffer) free(buffer);
233 static default_ani_info *
234 _read_json(const char *path)
237 json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
238 default_ani_info *ani_info = NULL;
239 int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
240 int data_size = 0, i, j;
242 buffer = _read_json_file(path, &data_size);
243 ERROR_CHECK(buffer && data_size > 0, goto error, "File %s has no data\n", path);
244 root_obj = json_tokener_parse(buffer);
245 ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
247 ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
248 ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
250 data_obj = json_object_object_get(root_obj, "type");
251 //printf("type: %s\n", json_object_get_string(data_obj));
252 type = (char *)json_object_get_string(data_obj);
253 ani_info->id = strndup(type, strlen(type));
255 data_obj = json_object_object_get(root_obj, "interval");
256 //printf("interval: %d\n", json_object_get_int(data_obj));
257 ani_info->interval = json_object_get_int(data_obj);
259 data_obj = json_object_object_get(root_obj, "frame");
260 frame_len = json_object_array_length(data_obj);
262 ani_info->num_key_frames = frame_len;
263 ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
264 ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
266 for (i = 0; i < frame_len; i++) {
267 frame_obj = json_object_array_get_idx(data_obj, i);
269 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
270 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
271 frame_id = json_object_get_int(frame_data_obj);
273 frame_data_obj = json_object_object_get(frame_obj, "frame_duration");
275 ani_info->frames[frame_id - 1].frame_duration = json_object_get_int(frame_data_obj);
277 ani_info->frames[frame_id - 1].frame_duration = ani_info->interval;
279 frame_data_obj = json_object_object_get(frame_obj, "led");
280 led_len = json_object_array_length(frame_data_obj);
282 ani_info->frames[frame_id - 1].num_led = led_len;
283 if (ani_info->max_leds < led_len) ani_info->max_leds = led_len;
284 ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
285 ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
287 for (j = 0; j < led_len; j++) {
288 led_obj = json_object_array_get_idx(frame_data_obj, j);
290 led_data_obj = json_object_object_get(led_obj, "id");
291 //printf("\t\tid: %2d ", json_object_get_int(led_data_obj));
292 led_id = json_object_get_int(led_data_obj);
294 led_data_obj = json_object_object_get(led_obj, "color");
295 //printf("color: %s\n", json_object_get_string(led_data_obj));
296 ani_info->frames[frame_id - 1].leds[led_id - 1].color =
297 strtol(json_object_get_string(led_data_obj), NULL, 16);
305 if (buffer) free(buffer);
307 if (ani_info->frames) {
308 for (i = 0; i < ani_info->num_key_frames; i++) {
309 if (ani_info->frames[i].leds)
310 free(ani_info->frames[i].leds);
312 free(ani_info->frames);
321 _find_directory(const char *path, Eina_List **json_list)
325 int count = 0, ret_cnt = 0;
330 pui_err("Failed to open %s.\n", path);
334 while ((cur = readdir(dir)) != NULL)
336 char next_path[MAX_STR] = {0, };
337 if (cur->d_type == DT_DIR)
339 if (!strncmp(cur->d_name, ".", sizeof(".")) ||
340 !strncmp(cur->d_name, "..", sizeof("..")))
343 snprintf(next_path, MAX_STR, "%s/%s", path, cur->d_name);
344 ret_cnt = _find_directory(next_path, json_list);
349 if (strstr(cur->d_name, ".json"))
351 snprintf(next_path, MAX_STR, "%s/%s", path, cur->d_name);
352 *json_list = eina_list_append(*json_list, eina_stringshare_add(next_path));
364 _create_ani_collection(void)
366 pui_int_error e = PUI_INT_ERROR_NONE;
367 default_ani_info *ani_info;
368 Eina_List *json_list = NULL;
369 Eina_List *l, *l_next;
370 Eina_Stringshare *path;
372 // load and store all of animation data
374 _find_directory(ANI_COLLECTION_DIR, &json_list);
376 EINA_LIST_FOREACH_SAFE(json_list, l, l_next, path)
378 ani_info = _read_json(path);
380 eina_hash_add(_animations_hash, ani_info->id, ani_info);
381 pui_info("Success to load %s animation (id: %s)\n", path, ani_info->id);
383 eina_stringshare_del(path);
384 json_list = eina_list_remove_list(json_list, l);
393 _geometry_get(int *width, int *height)
395 if (!width || !height)
399 *width = DEFAULT_BACKEND_GEOM_WIDTH;
401 *height = DEFAULT_BACKEND_GEOM_HEIGHT;
407 _is_ani_supported(pui_id id)
409 pui_int_error e = PUI_INT_ERROR_NONE;
410 default_ani_info *ani_info;
413 /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
415 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
418 ani_info = eina_hash_find(_animations_hash, id);
421 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
427 _ani_info_cleanup(default_ani_info *ani_info)
434 if (ani_info->frames)
436 for (i = 0; i < ani_info->num_key_frames; i++)
438 if (ani_info->frames[i].leds)
439 free(ani_info->frames[i].leds);
442 free(ani_info->frames);
448 pui_backend_ani_data *
449 _ani_create(pui_id id)
451 pui_backend_ani_data *ani_data = NULL;
452 pui_backend_ani_func *ani_func = NULL;
454 /* backend's animation specific info */
455 default_ani_info *ani_info = NULL;
457 //TODO : return NULL if the animation correspond to the given id dones't exist.
458 if (PUI_INT_ERROR_NONE != _is_ani_supported(id))
460 pui_err("The animation(%s) doesn't supported !\n", id);
464 /* allocation of the structure of function pointers that will be called from pui_ani_control() */
465 ani_func = pui_backend_ani_alloc_ani_func();
469 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
473 /* get animation info associate with the given id from animation collection */
474 ani_info = get_ani_info_from_ani_collection(id);
478 pui_err("Failed to get ani info from animation collection !\n");
482 /* allocate backend ani_data and return it to pui ani core */
483 ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
487 pui_err("Failed to allocate memory for pui backend ani data !\n");
491 /* Assign each function pointer that corresponds to the given id if needed. */
492 if (!strncmp(ani_info->id, "system/easy_setup", sizeof("system/easy_setup")))
494 pui_default_backend_ani_easysetup_func_set(ani_func);
496 else if (!strncmp(ani_info->id, "system/processing", sizeof("system/processing")))
498 pui_default_backend_ani_system_processing_func_set(ani_func);
500 else if (!strncmp(ani_info->id, "system/sw_update_done", sizeof("system/sw_update_done")))
502 pui_default_backend_ani_swupdatedone_func_set(ani_func);
504 else if (!strncmp(ani_info->id, "system/mic_off", sizeof("system/mic_off")))
506 pui_default_backend_ani_micoff_func_set(ani_func);
508 else if (!strncmp(ani_info->id, "voice/listening", sizeof("voice/listening")))
510 pui_default_backend_ani_listening_func_set(ani_func);
512 else if (!strncmp(ani_info->id, "voice/streaming", sizeof("voice/streaming")))
514 pui_default_backend_ani_streaming_func_set(ani_func);
516 else if (!strncmp(ani_info->id, "voice/processing", sizeof("voice/processing")))
518 pui_default_backend_ani_processing_func_set(ani_func);
520 else if (!strncmp(ani_info->id, "voice/speaking", sizeof("voice/speaking")))
522 pui_default_backend_ani_speaking_func_set(ani_func);
524 else if (!strncmp(ani_info->id, "voice/timeout", sizeof("voice/timeout")))
526 pui_default_backend_ani_timeout_func_set(ani_func);
528 else if (!strncmp(ani_info->id, "notification/normal", sizeof("notification/normal")))
530 pui_default_backend_ani_normal_func_set(ani_func);
532 else if (!strncmp(ani_info->id, "notification/emergency", sizeof("notification/emergency")))
534 pui_default_backend_ani_emergency_func_set(ani_func);
536 else if (!strncmp(ani_info->id, "notification/network_error", sizeof("notification/network_error")))
538 pui_default_backend_ani_networkerror_func_set(ani_func);
540 else if (!strncmp(ani_info->id, "notification/error", sizeof("notification/error")))
542 pui_default_backend_ani_error_func_set(ani_func);
544 else if (!strncmp(ani_info->id, "notification/alarm", sizeof("notification/alarm")))
546 pui_default_backend_ani_alarm_func_set(ani_func);
548 else if (!strncmp(ani_info->id, "bt/pairing", sizeof("bt/pairing")))
550 pui_default_backend_ani_pairing_func_set(ani_func);
552 else if (!strncmp(ani_info->id, "bt/connected", sizeof("bt/connected")))
554 pui_default_backend_ani_connected_func_set(ani_func);
558 pui_info("%s animation has no animation handler, using default handler\n", ani_info->id);
559 /* Assign each function pointer that corresponds to the given id if needed. */
560 ani_func->ani_start = _ani_start;
561 ani_func->ani_stop = _ani_stop;
564 ani_data->ani_func = ani_func;
565 ani_data->ani_info = (pui_backend_ani_info *)ani_info;
567 g_ani_data = ani_data;
574 pui_backend_ani_free_ani_func(ani_func);
582 _ani_destroy(pui_backend_ani_data *ani_data)
587 if (ani_data->ani_func)
589 pui_backend_ani_free_ani_func(ani_data->ani_func);
590 ani_data->ani_func = NULL;
593 ani_data->ani_info = NULL;
598 _animation_data_free_cb(void *data)
600 default_ani_info *ani_info = (default_ani_info *)data;
602 _ani_info_cleanup(ani_info);
605 default_frame_info_t *
606 backend_util_alloc_frame(default_ani_info *ani_info)
608 default_frame_info_t *frame;
610 frame = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), 1);
611 ERROR_CHECK(frame, return NULL, "Failed to allocate memory for frame\n");
613 frame->leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), ani_info->max_leds);
614 ERROR_CHECK(frame->leds, goto error, "Failed to allocate memory for led\n");
616 frame->num_led = ani_info->max_leds;
626 backend_util_cleanup_frame(default_frame_info_t *frame)
628 ERROR_CHECK(frame, return, "Invalid frame to cleanup\n");
630 for (int i = 0; i < frame->num_led; i++)
632 frame->leds[i].color = 0x0;
634 frame->frame_duration = 0;
638 backend_util_free_frame(default_frame_info_t *frame)
642 if (frame->leds) free(frame->leds);
647 static pui_backend_module_data *
648 pui_default_backend_init(void)
650 pui_backend_module_data *backend_data = NULL;
652 backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
656 pui_err("Failed to allocate memory for pui backend module data !\n");
660 backend_data->create_ani_collection = _create_ani_collection;
661 backend_data->geometry_get = _geometry_get;
662 backend_data->ani_create = _ani_create;
663 backend_data->ani_destroy = _ani_destroy;
665 /* Allocate backend specific data if needed. Now it will be empty. */
666 backend_data->data = NULL;
667 _animations_hash = eina_hash_string_superfast_new(NULL);
673 pui_default_backend_deinit(pui_backend_module_data *backend_data)
676 default_ani_info *ani_info = NULL;
681 if (backend_data->data)
683 //TODO : free variables of backend_data
685 free(backend_data->data);
690 if (g_ani_data->ani_func)
692 pui_backend_ani_free_ani_func(g_ani_data->ani_func);
693 g_ani_data->ani_func = NULL;
696 g_ani_data->ani_info = NULL;
699 if (_animations_hash)
701 it = eina_hash_iterator_data_new(_animations_hash);
703 EINA_ITERATOR_FOREACH(it, ani_info)
705 _animation_data_free_cb(ani_info);
709 eina_iterator_free(it);
711 eina_hash_free(_animations_hash);
712 _animations_hash = NULL;
715 backend_data->create_ani_collection = NULL;
716 backend_data->geometry_get = NULL;
717 backend_data->ani_create = NULL;
718 backend_data->ani_destroy = NULL;
724 pui_backend_module pui_backend_module_info = {
725 "Tizen Reference Speaker Backend",
727 PUI_BACKEND_SET_ABI_VERSION(1, 0),
728 pui_default_backend_init,
729 pui_default_backend_deinit