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_int_error e = PUI_INT_ERROR_NONE;
96 pui_ani_t *ani = (pui_ani_t *)data;
97 pui_backend_ani_data *ani_data = NULL;
98 pui_ani_control_buffer *buffer = NULL;
100 ani_data = pui_backend_ani_get_ani_data(ani);
101 default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
103 pui_info("... serial=%d\n", serial);
105 /* TODO : make use of ani_info */
108 buffer = pui_backend_ani_get_buffer(ani);
111 for(int i = 0; i<12; i++)
113 buffer->ptr[4*i] = 0;
114 buffer->ptr[4*i + 1] = (((serial%3) == 0) ? 0xFF:0); /* BLUE */
115 buffer->ptr[4*i + 2] = (((serial%3) == 1) ? 0xFF:0); /* GREEN */
116 buffer->ptr[4*i + 3] = (((serial%3) == 2) ? 0xFF:0); /* RED */
119 e = pui_backend_ani_set_buffer(ani, buffer);
121 if (e != PUI_INT_ERROR_NONE)
123 pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
127 e = pui_backend_ani_update(ani);
129 if (e != PUI_INT_ERROR_NONE)
131 pui_err("Failed on updating animation !(e=%d)\n", e);
135 pui_info("... update (serial=%d)\n", serial);
141 get_ani_info_from_ani_collection(pui_id id)
143 default_ani_info *ani_info = NULL;
147 //ex> data->interval = 30;
149 if (!_animations_hash)
152 pui_info("... id: %s\n", id);
154 ani_info = eina_hash_find(_animations_hash, id);
158 pui_err("ani_info has NOT been found ! (id:%s)\n", id);
162 pui_info("ani_info has been found ! (id:%s)\n", id);
168 _ani_start(pui_ani_t *ani, int repeat)
171 pui_int_error e = PUI_INT_ERROR_NONE;
172 pui_backend_ani_data *ani_data = NULL;
174 ani_data = pui_backend_ani_get_ani_data(ani);
175 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
180 pui_info("... info->id: %s, repeat : %d\n", info->id, repeat);
182 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
183 ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_frame_cb, 0.1);
187 pui_err("Failed to add frame callback !\n");
188 e = PUI_INT_ERROR_INVALID_RESOURCES;
195 _ani_stop(pui_ani_t *ani)
197 pui_int_error e = PUI_INT_ERROR_NONE;
198 pui_backend_ani_data *ani_data = NULL;
200 ani_data = pui_backend_ani_get_ani_data(ani);
201 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
206 pui_info("... info->id: %s\n", info->id);
208 pui_backend_ani_remove_frame_cb(ani);
209 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
215 _read_json_file(const char *path, int *data_size)
217 FILE *fp = fopen(path, "rb");
220 ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
222 fseek(fp, 0, SEEK_END);
224 fseek(fp, 0, SEEK_SET);
226 buffer = (char *)calloc(sizeof(char), size + 1);
228 if (fread(buffer, size, 1, fp) < 1) {
240 static default_ani_info *
241 _read_json(const char *path)
244 json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
245 default_ani_info *ani_info = NULL;
246 int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
247 int data_size = 0, i, j;
249 buffer = _read_json_file(path, &data_size);
250 ERROR_CHECK(buffer && data_size > 0, return EINA_FALSE, "File %s has no data\n", path);
251 root_obj = json_tokener_parse(buffer);
252 ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
254 ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
255 ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
257 data_obj = json_object_object_get(root_obj, "type");
258 //printf("type: %s\n", json_object_get_string(data_obj));
259 type = (char *)json_object_get_string(data_obj);
260 ani_info->id = strndup(type, strlen(type));
262 data_obj = json_object_object_get(root_obj, "interval");
263 //printf("interval: %d\n", json_object_get_int(data_obj));
264 ani_info->interval = json_object_get_int(data_obj);
266 data_obj = json_object_object_get(root_obj, "frame");
267 frame_len = json_object_array_length(data_obj);
269 ani_info->num_key_frames = frame_len;
270 ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
271 ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
273 for (i = 0; i < frame_len; i++) {
274 frame_obj = json_object_array_get_idx(data_obj, i);
276 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
277 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
278 frame_id = json_object_get_int(frame_data_obj);
280 frame_data_obj = json_object_object_get(frame_obj, "led");
281 led_len = json_object_array_length(frame_data_obj);
283 ani_info->frames[frame_id - 1].num_led = 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);
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 _scandir_filter(const struct dirent *dirent)
323 if (!strncmp(dirent->d_name, ".", sizeof(".")) ||
324 !strncmp(dirent->d_name, "..", sizeof("..")))
326 if (!strstr(dirent->d_name, ".json"))
334 _create_ani_collection(void)
336 pui_int_error e = PUI_INT_ERROR_NONE;
337 default_ani_info *ani_info;
338 struct dirent **files;
340 char file_path[MAX_STR] = {0, };
342 // load and store all of animation data
344 if ((count = scandir(ANI_COLLECTION_DIR, &files, _scandir_filter, alphasort)) == -1) {
345 printf("Failed to get %s directory (%s)\n", ANI_COLLECTION_DIR, strerror(errno));
346 e = PUI_INT_ERROR_INVALID_RESOURCES;
350 for (i = 0; i < count; i++) {
351 snprintf(file_path, sizeof(file_path), "%s%s", ANI_COLLECTION_DIR, files[i]->d_name);
353 ani_info = _read_json(file_path);
355 eina_hash_add(_animations_hash, ani_info->id, ani_info);
356 printf("Success to load %s animation\n", files[i]->d_name);
360 for (i = 0; i < count; i++) {
371 _is_ani_supported(pui_id id)
373 pui_int_error e = PUI_INT_ERROR_NONE;
374 default_ani_info *ani_info;
377 /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
379 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
382 ani_info = eina_hash_find(_animations_hash, id);
385 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
391 _ani_info_cleanup(default_ani_info *ani_info)
398 if (ani_info->frames)
400 for (i = 0; i < ani_info->num_key_frames; i++)
402 if (ani_info->frames[i].leds)
403 free(ani_info->frames[i].leds);
406 free(ani_info->frames);
412 pui_backend_ani_data *
413 _ani_create(pui_id id)
415 pui_backend_ani_data *ani_data = NULL;
416 pui_backend_ani_func *ani_func = NULL;
418 /* backend's animation specific info */
419 default_ani_info *ani_info = NULL;
421 //TODO : return NULL if the animation correspond to the given id dones't exist.
422 if (PUI_INT_ERROR_NONE != _is_ani_supported(id))
424 pui_err("The animation(%s) doesn't supported !\n", id);
428 /* allocation of the structure of function pointers that will be called from pui_ani_control() */
429 ani_func = pui_backend_ani_alloc_ani_func();
433 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
437 /* Assign each function pointer that corresponds to the given id if needed. */
438 ani_func->ani_start = _ani_start;
439 ani_func->ani_stop = _ani_stop;
441 /* get animation info associate with the given id from animation collection */
442 ani_info = get_ani_info_from_ani_collection(id);
446 pui_err("Failed to get ani info from animation collection !\n");
450 /* allocate backend ani_data and return it to pui ani core */
451 ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
455 pui_err("Failed to allocate memory for pui backend ani data !\n");
459 ani_data->ani_func = ani_func;
460 ani_data->ani_info = (pui_backend_ani_info *)ani_info;
462 g_ani_data = ani_data;
469 pui_backend_ani_free_ani_func(ani_func);
477 _ani_destroy(pui_backend_ani_data *ani_data)
482 if (ani_data->ani_func)
484 pui_backend_ani_free_ani_func(ani_data->ani_func);
485 ani_data->ani_func = NULL;
488 ani_data->ani_info = NULL;
493 _animation_data_free_cb(void *data)
495 default_ani_info *ani_info = (default_ani_info *)data;
497 _ani_info_cleanup(ani_info);
500 static pui_backend_module_data *
501 pui_default_backend_init(void)
503 pui_backend_module_data *backend_data = NULL;
505 backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
509 pui_err("Failed to allocate memory for pui backend module data !\n");
513 backend_data->create_ani_collection = _create_ani_collection;
514 backend_data->ani_create = _ani_create;
515 backend_data->ani_destroy = _ani_destroy;
517 /* Allocate backend specific data if needed. Now it will be empty. */
518 backend_data->data = NULL;
519 _animations_hash = eina_hash_string_superfast_new(NULL);
525 pui_default_backend_deinit(pui_backend_module_data *backend_data)
528 default_ani_info *ani_info = NULL;
533 if (backend_data->data)
535 //TODO : free variables of backend_data
537 free(backend_data->data);
542 if (g_ani_data->ani_func)
544 pui_backend_ani_free_ani_func(g_ani_data->ani_func);
545 g_ani_data->ani_func = NULL;
548 g_ani_data->ani_info = NULL;
551 if (_animations_hash)
553 it = eina_hash_iterator_data_new(_animations_hash);
555 EINA_ITERATOR_FOREACH(it, ani_info)
557 _animation_data_free_cb(ani_info);
561 eina_iterator_free(it);
563 eina_hash_free(_animations_hash);
564 _animations_hash = NULL;
567 backend_data->create_ani_collection = NULL;
568 backend_data->ani_create = NULL;
569 backend_data->ani_destroy = NULL;
575 pui_backend_module pui_backend_module_info = {
576 "Tizen Reference Speaker Backend",
578 PUI_BACKEND_SET_ABI_VERSION(1, 0),
579 pui_default_backend_init,
580 pui_default_backend_deinit