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
28 #include <PUI_backend.h>
30 #include <json-c/json.h>
36 #define ANI_COLLECTION_DIR "/usr/share/pui/"
39 #define ERROR_CHECK(exp, action, fmt, ...) \
43 printf(fmt, ##__VA_ARGS__); \
48 pui_backend_ani_func *ani_func = NULL;
49 Eina_Hash *_animations_hash = NULL;
61 typedef struct _default_ani_info default_ani_info;
62 typedef struct _default_frame_info_t default_frame_info_t;
63 typedef struct _default_led_info_t default_led_info_t;
65 struct _default_ani_info
68 pui_ani_status status;
69 pui_ani_control_buffer *buffer;
72 unsigned int key_frame_idx;
73 unsigned int num_key_frames;
74 default_frame_info_t *frames;
76 pui_effect_func effect_func;
79 struct _default_frame_info_t
81 default_led_info_t *leds;
85 struct _default_led_info_t
90 pui_backend_ani_data *g_ani_data = NULL;
93 _ani_backend_frame_cb(void *data, int serial)
95 pui_ani_t *ani = (pui_ani_t *)data;
100 //pui_backend_ani_get_buffer();
101 //pui_backend_ani_update();
103 pui_info("... serial=%d\n", serial);
109 get_ani_info_from_ani_collection(pui_id id)
111 default_ani_info *ani_info = NULL;
115 //ex> data->interval = 30;
117 if (!_animations_hash)
120 pui_info("... id: %s\n", id);
122 ani_info = eina_hash_find(_animations_hash, id);
126 pui_err("ani_info has NOT been found ! (id:%s)\n", id);
130 pui_info("ani_info has been found ! (id:%s)\n", id);
136 _ani_start(pui_ani_t *ani, int repeat)
139 pui_int_error e = PUI_INT_ERROR_NONE;
140 pui_backend_ani_data *ani_data = NULL;
142 ani_data = pui_backend_ani_get_ani_data(ani);
143 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
148 pui_info("... info->id: %s, repeat : %d\n", info->id, repeat);
150 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
151 ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_frame_cb, 0.1);
155 pui_err("Failed to add frame callback !\n");
156 e = PUI_INT_ERROR_INVALID_RESOURCES;
163 _ani_stop(pui_ani_t *ani)
165 pui_int_error e = PUI_INT_ERROR_NONE;
166 pui_backend_ani_data *ani_data = NULL;
168 ani_data = pui_backend_ani_get_ani_data(ani);
169 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
174 pui_info("... info->id: %s\n", info->id);
176 pui_backend_ani_remove_frame_cb(ani);
177 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
183 _read_json_file(const char *path, int *data_size)
185 FILE *fp = fopen(path, "rb");
188 ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
190 fseek(fp, 0, SEEK_END);
192 fseek(fp, 0, SEEK_SET);
194 buffer = (char *)calloc(sizeof(char), size + 1);
196 if (fread(buffer, size, 1, fp) < 1) {
208 static default_ani_info *
209 _read_json(const char *path)
212 json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
213 default_ani_info *ani_info = NULL;
214 int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
215 int data_size = 0, i, j;
217 buffer = _read_json_file(path, &data_size);
218 ERROR_CHECK(buffer && data_size > 0, return EINA_FALSE, "File %s has no data\n", path);
219 root_obj = json_tokener_parse(buffer);
220 ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
222 ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
223 ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
225 data_obj = json_object_object_get(root_obj, "type");
226 //printf("type: %s\n", json_object_get_string(data_obj));
227 type = (char *)json_object_get_string(data_obj);
228 ani_info->id = strndup(type, strlen(type));
230 data_obj = json_object_object_get(root_obj, "interval");
231 //printf("interval: %d\n", json_object_get_int(data_obj));
232 ani_info->interval = json_object_get_int(data_obj);
234 data_obj = json_object_object_get(root_obj, "frame");
235 frame_len = json_object_array_length(data_obj);
237 ani_info->num_key_frames = frame_len;
238 ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
239 ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
241 for (i = 0; i < frame_len; i++) {
242 frame_obj = json_object_array_get_idx(data_obj, i);
244 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
245 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
246 frame_id = json_object_get_int(frame_data_obj);
248 frame_data_obj = json_object_object_get(frame_obj, "led");
249 led_len = json_object_array_length(frame_data_obj);
251 ani_info->frames[frame_id - 1].num_led = led_len;
252 ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
253 ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
255 for (j = 0; j < led_len; j++) {
256 led_obj = json_object_array_get_idx(frame_data_obj, j);
258 led_data_obj = json_object_object_get(led_obj, "id");
259 //printf("\t\tid: %2d ", json_object_get_int(led_data_obj));
260 led_id = json_object_get_int(led_data_obj);
262 led_data_obj = json_object_object_get(led_obj, "color");
263 //printf("color: %s\n", json_object_get_string(led_data_obj));
264 ani_info->frames[frame_id - 1].leds[led_id - 1].color =
265 strtol(json_object_get_string(led_data_obj), NULL, 16);
275 if (ani_info->frames) {
276 for (i = 0; i < ani_info->num_key_frames; i++) {
277 if (ani_info->frames[i].leds)
278 free(ani_info->frames[i].leds);
280 free(ani_info->frames);
289 _scandir_filter(const struct dirent *dirent)
291 if (!strncmp(dirent->d_name, ".", sizeof(".")) ||
292 !strncmp(dirent->d_name, "..", sizeof("..")))
294 if (!strstr(dirent->d_name, ".json"))
302 _create_ani_collection(void)
304 pui_int_error e = PUI_INT_ERROR_NONE;
305 default_ani_info *ani_info;
306 struct dirent **files;
308 char file_path[MAX_STR] = {0, };
310 // load and store all of animation data
312 if ((count = scandir(ANI_COLLECTION_DIR, &files, _scandir_filter, alphasort)) == -1) {
313 printf("Failed to get %s directory (%s)\n", ANI_COLLECTION_DIR, strerror(errno));
314 e = PUI_INT_ERROR_INVALID_RESOURCES;
318 for (i = 0; i < count; i++) {
319 snprintf(file_path, sizeof(file_path), "%s%s", ANI_COLLECTION_DIR, files[i]->d_name);
321 ani_info = _read_json(file_path);
323 eina_hash_add(_animations_hash, ani_info->id, ani_info);
324 printf("Success to load %s animation\n", files[i]->d_name);
328 for (i = 0; i < count; i++) {
339 _is_ani_supported(pui_id id)
341 pui_int_error e = PUI_INT_ERROR_NONE;
342 default_ani_info *ani_info;
345 /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
347 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
350 ani_info = eina_hash_find(_animations_hash, id);
353 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
359 _ani_info_cleanup(default_ani_info *ani_info)
366 if (ani_info->frames)
368 for (i = 0; i < ani_info->num_key_frames; i++)
370 if (ani_info->frames[i].leds)
371 free(ani_info->frames[i].leds);
374 free(ani_info->frames);
380 pui_backend_ani_data *
381 _ani_create(pui_id id)
383 pui_backend_ani_data *ani_data = NULL;
384 pui_backend_ani_func *ani_func = NULL;
386 /* backend's animation specific info */
387 default_ani_info *ani_info = NULL;
389 //TODO : return NULL if the animation correspond to the given id dones't exist.
390 if (PUI_INT_ERROR_NONE != _is_ani_supported(id))
392 pui_err("The animation(%s) doesn't supported !\n", id);
396 /* allocation of the structure of function pointers that will be called from pui_ani_control() */
397 ani_func = pui_backend_ani_alloc_ani_func();
401 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
405 /* Assign each function pointer that corresponds to the given id if needed. */
406 ani_func->ani_start = _ani_start;
407 ani_func->ani_stop = _ani_stop;
409 /* get animation info associate with the given id from animation collection */
410 ani_info = get_ani_info_from_ani_collection(id);
414 pui_err("Failed to get ani info from animation collection !\n");
418 /* allocate backend ani_data and return it to pui ani core */
419 ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
423 pui_err("Failed to allocate memory for pui backend ani data !\n");
427 ani_data->ani_func = ani_func;
428 ani_data->ani_info = (pui_backend_ani_info *)ani_info;
430 g_ani_data = ani_data;
437 pui_backend_ani_free_ani_func(ani_func);
445 _ani_destroy(pui_backend_ani_data *ani_data)
450 if (ani_data->ani_func)
452 pui_backend_ani_free_ani_func(ani_data->ani_func);
453 ani_data->ani_func = NULL;
456 ani_data->ani_info = NULL;
461 _animation_data_free_cb(void *data)
463 default_ani_info *ani_info = (default_ani_info *)data;
465 _ani_info_cleanup(ani_info);
468 static pui_backend_module_data *
469 pui_default_backend_init(void)
471 pui_backend_module_data *backend_data = NULL;
473 backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
477 pui_err("Failed to allocate memory for pui backend module data !\n");
481 backend_data->create_ani_collection = _create_ani_collection;
482 backend_data->ani_create = _ani_create;
483 backend_data->ani_destroy = _ani_destroy;
485 /* Allocate backend specific data if needed. Now it will be empty. */
486 backend_data->data = NULL;
487 _animations_hash = eina_hash_string_superfast_new(NULL);
493 pui_default_backend_deinit(pui_backend_module_data *backend_data)
496 default_ani_info *ani_info = NULL;
501 if (backend_data->data)
503 //TODO : free variables of backend_data
505 free(backend_data->data);
510 if (g_ani_data->ani_func)
512 pui_backend_ani_free_ani_func(g_ani_data->ani_func);
513 g_ani_data->ani_func = NULL;
516 g_ani_data->ani_info = NULL;
519 if (_animations_hash)
521 it = eina_hash_iterator_data_new(_animations_hash);
523 EINA_ITERATOR_FOREACH(it, ani_info)
525 _animation_data_free_cb(ani_info);
529 eina_iterator_free(it);
531 eina_hash_free(_animations_hash);
532 _animations_hash = NULL;
535 backend_data->create_ani_collection = NULL;
536 backend_data->ani_create = NULL;
537 backend_data->ani_destroy = NULL;
543 pui_backend_module pui_backend_module_info = {
544 "Tizen Reference Speaker Backend",
546 PUI_BACKEND_SET_ABI_VERSION(1, 0),
547 pui_default_backend_init,
548 pui_default_backend_deinit