4 #include <PUI_backend.h>
6 #include <json-c/json.h>
12 #define ANI_COLLECTION_DIR "/usr/share/pui/"
15 #define ERROR_CHECK(exp, action, fmt, ...) \
19 printf(fmt, ##__VA_ARGS__); \
24 pui_backend_ani_func *ani_func = NULL;
25 Eina_Hash *_animations_hash = NULL;
37 typedef struct _default_ani_info default_ani_info;
38 typedef struct _default_frame_info_t default_frame_info_t;
39 typedef struct _default_led_info_t default_led_info_t;
41 struct _default_ani_info
44 pui_ani_status status;
45 pui_ani_control_buffer *buffer;
48 unsigned int key_frame_idx;
49 unsigned int num_key_frames;
50 default_frame_info_t *frames;
52 pui_effect_func effect_func;
55 struct _default_frame_info_t
57 default_led_info_t *leds;
61 struct _default_led_info_t
66 pui_backend_ani_data *g_ani_data = NULL;
69 _ani_backend_frame_cb(void *data, int serial)
71 pui_ani_t *ani = (pui_ani_t *)data;
76 //pui_backend_ani_get_buffer();
77 //pui_backend_ani_update();
79 pui_info("... serial=%d\n", serial);
85 get_ani_info_from_ani_collection(pui_id id)
87 default_ani_info *ani_info = NULL;
91 //ex> data->interval = 30;
93 if (!_animations_hash)
96 pui_info("... id: %s\n", id);
98 ani_info = eina_hash_find(_animations_hash, id);
102 pui_err("ani_info has NOT been found ! (id:%s)\n", id);
106 pui_info("ani_info has been found ! (id:%s)\n", id);
112 _ani_start(pui_ani_t *ani, int repeat)
115 pui_int_error e = PUI_INT_ERROR_NONE;
116 pui_backend_ani_data *ani_data = NULL;
118 ani_data = pui_backend_ani_get_ani_data(ani);
119 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
124 pui_info("... info->id: %s, repeat : %d\n", info->id, repeat);
126 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
127 ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_frame_cb, 0.1);
131 pui_err("Failed to add frame callback !\n");
132 e = PUI_INT_ERROR_INVALID_RESOURCES;
139 _ani_stop(pui_ani_t *ani)
141 pui_int_error e = PUI_INT_ERROR_NONE;
142 pui_backend_ani_data *ani_data = NULL;
144 ani_data = pui_backend_ani_get_ani_data(ani);
145 default_ani_info *info = (default_ani_info *)ani_data->ani_info;
150 pui_info("... info->id: %s\n", info->id);
152 pui_backend_ani_remove_frame_cb(ani);
153 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
159 _read_json_file(const char *path, int *data_size)
161 FILE *fp = fopen(path, "rb");
164 ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
166 fseek(fp, 0, SEEK_END);
168 fseek(fp, 0, SEEK_SET);
170 buffer = (char *)calloc(sizeof(char), size + 1);
172 if (fread(buffer, size, 1, fp) < 1) {
184 static default_ani_info *
185 _read_json(const char *path)
188 json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
189 default_ani_info *ani_info = NULL;
190 int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
191 int data_size = 0, i, j;
193 buffer = _read_json_file(path, &data_size);
194 ERROR_CHECK(buffer && data_size > 0, return EINA_FALSE, "File %s has no data\n", path);
195 root_obj = json_tokener_parse(buffer);
196 ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
198 ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
199 ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
201 data_obj = json_object_object_get(root_obj, "type");
202 //printf("type: %s\n", json_object_get_string(data_obj));
203 type = (char *)json_object_get_string(data_obj);
204 ani_info->id = strndup(type, strlen(type));
206 data_obj = json_object_object_get(root_obj, "interval");
207 //printf("interval: %d\n", json_object_get_int(data_obj));
208 ani_info->interval = json_object_get_int(data_obj);
210 data_obj = json_object_object_get(root_obj, "frame");
211 frame_len = json_object_array_length(data_obj);
213 ani_info->num_key_frames = frame_len;
214 ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
215 ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
217 for (i = 0; i < frame_len; i++) {
218 frame_obj = json_object_array_get_idx(data_obj, i);
220 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
221 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
222 frame_id = json_object_get_int(frame_data_obj);
224 frame_data_obj = json_object_object_get(frame_obj, "led");
225 led_len = json_object_array_length(frame_data_obj);
227 ani_info->frames[frame_id - 1].num_led = led_len;
228 ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
229 ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
231 for (j = 0; j < led_len; j++) {
232 led_obj = json_object_array_get_idx(frame_data_obj, j);
234 led_data_obj = json_object_object_get(led_obj, "id");
235 //printf("\t\tid: %2d ", json_object_get_int(led_data_obj));
236 led_id = json_object_get_int(led_data_obj);
238 led_data_obj = json_object_object_get(led_obj, "color");
239 //printf("color: %s\n", json_object_get_string(led_data_obj));
240 ani_info->frames[frame_id - 1].leds[led_id - 1].color =
241 strtol(json_object_get_string(led_data_obj), NULL, 16);
251 if (ani_info->frames) {
252 for (i = 0; i < ani_info->num_key_frames; i++) {
253 if (ani_info->frames[i].leds)
254 free(ani_info->frames[i].leds);
256 free(ani_info->frames);
265 _scandir_filter(const struct dirent *dirent)
267 if (!strncmp(dirent->d_name, ".", sizeof(".")) ||
268 !strncmp(dirent->d_name, "..", sizeof("..")))
270 if (!strstr(dirent->d_name, ".json"))
278 _create_ani_collection(void)
280 pui_int_error e = PUI_INT_ERROR_NONE;
281 default_ani_info *ani_info;
282 struct dirent **files;
284 char file_path[MAX_STR] = {0, };
286 // load and store all of animation data
288 if ((count = scandir(ANI_COLLECTION_DIR, &files, _scandir_filter, alphasort)) == -1) {
289 printf("Failed to get %s directory (%s)\n", ANI_COLLECTION_DIR, strerror(errno));
290 e = PUI_INT_ERROR_INVALID_RESOURCES;
294 for (i = 0; i < count; i++) {
295 snprintf(file_path, sizeof(file_path), "%s%s", ANI_COLLECTION_DIR, files[i]->d_name);
297 ani_info = _read_json(file_path);
299 eina_hash_add(_animations_hash, ani_info->id, ani_info);
300 printf("Success to load %s animation\n", files[i]->d_name);
304 for (i = 0; i < count; i++) {
315 _is_ani_supported(pui_id id)
317 pui_int_error e = PUI_INT_ERROR_NONE;
318 default_ani_info *ani_info;
321 /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
323 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
326 ani_info = eina_hash_find(_animations_hash, id);
329 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
335 _ani_info_cleanup(default_ani_info *ani_info)
342 if (ani_info->frames)
344 for (i = 0; i < ani_info->num_key_frames; i++)
346 if (ani_info->frames[i].leds)
347 free(ani_info->frames[i].leds);
350 free(ani_info->frames);
356 pui_backend_ani_data *
357 _ani_create(pui_id id)
359 pui_backend_ani_data *ani_data = NULL;
360 pui_backend_ani_func *ani_func = NULL;
362 /* backend's animation specific info */
363 default_ani_info *ani_info = NULL;
365 //TODO : return NULL if the animation correspond to the given id dones't exist.
366 if (PUI_INT_ERROR_NONE != _is_ani_supported(id))
368 pui_err("The animation(%s) doesn't supported !\n", id);
372 /* allocation of the structure of function pointers that will be called from pui_ani_control() */
373 ani_func = pui_backend_ani_alloc_ani_func();
377 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
381 /* Assign each function pointer that corresponds to the given id if needed. */
382 ani_func->ani_start = _ani_start;
383 ani_func->ani_stop = _ani_stop;
385 /* get animation info associate with the given id from animation collection */
386 ani_info = get_ani_info_from_ani_collection(id);
390 pui_err("Failed to get ani info from animation collection !\n");
394 /* allocate backend ani_data and return it to pui ani core */
395 ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
399 pui_err("Failed to allocate memory for pui backend ani data !\n");
403 ani_data->ani_func = ani_func;
404 ani_data->ani_info = (pui_backend_ani_info *)ani_info;
406 g_ani_data = ani_data;
413 pui_backend_ani_free_ani_func(ani_func);
421 _ani_destroy(pui_backend_ani_data *ani_data)
426 if (ani_data->ani_func)
428 pui_backend_ani_free_ani_func(ani_data->ani_func);
429 ani_data->ani_func = NULL;
432 ani_data->ani_info = NULL;
437 _animation_data_free_cb(void *data)
439 default_ani_info *ani_info = (default_ani_info *)data;
441 _ani_info_cleanup(ani_info);
444 static pui_backend_module_data *
445 pui_default_backend_init(void)
447 pui_backend_module_data *backend_data = NULL;
449 backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
453 pui_err("Failed to allocate memory for pui backend module data !\n");
457 backend_data->create_ani_collection = _create_ani_collection;
458 backend_data->ani_create = _ani_create;
459 backend_data->ani_destroy = _ani_destroy;
461 /* Allocate backend specific data if needed. Now it will be empty. */
462 backend_data->data = NULL;
463 _animations_hash = eina_hash_string_superfast_new(NULL);
469 pui_default_backend_deinit(pui_backend_module_data *backend_data)
472 default_ani_info *ani_info = NULL;
477 if (backend_data->data)
479 //TODO : free variables of backend_data
481 free(backend_data->data);
486 if (g_ani_data->ani_func)
488 pui_backend_ani_free_ani_func(g_ani_data->ani_func);
489 g_ani_data->ani_func = NULL;
492 g_ani_data->ani_info = NULL;
495 if (_animations_hash)
497 it = eina_hash_iterator_data_new(_animations_hash);
499 EINA_ITERATOR_FOREACH(it, ani_info)
501 _animation_data_free_cb(ani_info);
505 eina_iterator_free(it);
507 eina_hash_free(_animations_hash);
508 _animations_hash = NULL;
511 backend_data->create_ani_collection = NULL;
512 backend_data->ani_create = NULL;
513 backend_data->ani_destroy = NULL;
519 pui_backend_module pui_backend_module_info = {
520 "Tizen Reference Speaker Backend",
522 PUI_BACKEND_SET_ABI_VERSION(1, 0),
523 pui_default_backend_init,
524 pui_default_backend_deinit