backend: add default_backend initial codes
[platform/core/uifw/libpui.git] / backends / default_backend.c
1 //#include <PUI.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <PUI_backend.h>
5
6 #include <json-c/json.h>
7 #include <dirent.h>
8 #include <errno.h>
9 #include <Eina.h>
10
11 #define ANI_COLLECTION_DIR "/run/pui/"
12 #define MAX_STR 1024
13
14 #define ERROR_CHECK(exp, action, fmt, ...) \
15         do { \
16                 if (!(exp)) \
17                 { \
18                         printf(fmt, ##__VA_ARGS__);     \
19                         action; \
20                 } \
21         } while (0)
22
23 pui_backend_ani_func *ani_func = NULL;
24 Eina_Hash *_animations_hash = NULL;
25
26 typedef enum
27 {
28         None,
29         Linear,
30         EaseInSine,
31         EaseOutSine,
32         EaseInQuart,
33         EaseOutQuart
34 } pui_effect_func;
35
36 typedef struct _default_ani_info default_ani_info;
37 typedef struct _default_frame_info_t default_frame_info_t;
38 typedef struct _default_led_info_t default_led_info_t;
39
40 struct _default_ani_info
41 {
42         pui_id id;
43         pui_ani_status status;
44         pui_ani_control_buffer *buffer;
45         unsigned int repeat;
46
47         unsigned int key_frame_idx;
48         unsigned int num_key_frames;
49         default_frame_info_t *frames;
50         int interval;
51         pui_effect_func effect_func;
52 };
53
54 struct _default_frame_info_t
55 {
56         default_led_info_t *leds;
57         int num_led;
58 };
59
60 struct _default_led_info_t
61 {
62         unsigned int color;
63 };
64
65 pui_backend_ani_data *g_ani_data = NULL;
66
67 static pui_bool
68 _frame_cb(void *data, int serial)
69 {
70         pui_ani_t *ani = (pui_ani_t *)data;
71
72         //TODO
73         (void) ani;
74         //_get_next_frame();
75         //pui_backend_ani_get_buffer();
76         //pui_backend_ani_update();
77
78         return (pui_bool)1;
79 }
80
81 pui_int_error
82 get_ani_info_from_ani_collection(default_ani_info *info, pui_id id)
83 {
84         pui_int_error e = PUI_INT_ERROR_NONE;
85
86         //TODO
87         //ex> data->id = id;
88         //ex> data->interval = 30;
89
90         return e;
91 }
92
93 pui_error
94 _ani_start(pui_ani_t *ani, int repeat)
95 {
96         pui_int_error e = PUI_INT_ERROR_NONE;
97         pui_backend_ani_data *ani_data = NULL;
98
99         ani_data = pui_backend_ani_get_ani_data(ani);
100         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
101
102         //TODO
103         (void) info;
104
105         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
106         pui_backend_ani_add_frame_cb(ani, _frame_cb, 0.1);
107
108         return e;
109 }
110
111 pui_error
112 _ani_stop(pui_ani_t *ani)
113 {
114         pui_int_error e = PUI_INT_ERROR_NONE;
115         pui_backend_ani_data *ani_data = NULL;
116
117         ani_data = pui_backend_ani_get_ani_data(ani);
118         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
119
120         //TODO
121         (void) info;
122
123         pui_backend_ani_remove_frame_cb(ani);
124         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
125
126         return e;
127 }
128
129 static char *
130 _read_json_file(const char *path, int *data_size)
131 {
132         FILE *fp = fopen(path, "rb");
133         int size;
134         char *buffer;
135         ERROR_CHECK(fp, return NULL, "Failed to open file: %s\n", path);
136
137         fseek(fp, 0, SEEK_END);
138         size = ftell(fp);
139         fseek(fp, 0, SEEK_SET);
140
141         buffer = (char *)calloc(sizeof(char), size + 1);
142
143         if (fread(buffer, size, 1, fp) < 1) {
144                 *data_size = 0;
145                 free(buffer);
146                 fclose(fp);
147                 return NULL;
148         }
149
150         *data_size = size;
151         fclose(fp);
152         return buffer;
153 }
154
155 static default_ani_info *
156 _read_json(const char *path)
157 {
158         char *buffer, *type;
159         json_object *root_obj, *data_obj, *frame_obj, *frame_data_obj, *led_obj, *led_data_obj;
160         default_ani_info *ani_info = NULL;
161         int frame_id = 0, frame_len = 0, led_id = 0, led_len = 0;
162         int data_size = 0, i, j;
163
164         buffer = _read_json_file(path, &data_size);
165         ERROR_CHECK(buffer && data_size > 0, return EINA_FALSE, "File %s has no data\n", path);
166         root_obj = json_tokener_parse(buffer);
167         ERROR_CHECK(root_obj, goto error, "Failed to tokenize json object\n");
168
169         ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
170         ERROR_CHECK(ani_info, goto error, "Failed to alloc for animation info\n");
171
172         data_obj = json_object_object_get(root_obj, "type");
173         //printf("type: %s\n", json_object_get_string(data_obj));
174         type = (char *)json_object_get_string(data_obj);
175         ani_info->id = strndup(type, strlen(type));
176
177         data_obj = json_object_object_get(root_obj, "interval");
178         //printf("interval: %d\n", json_object_get_int(data_obj));
179         ani_info->interval = json_object_get_int(data_obj);
180
181         data_obj = json_object_object_get(root_obj, "frame");
182         frame_len = json_object_array_length(data_obj);
183
184         ani_info->num_key_frames = frame_len;
185         ani_info->frames = (default_frame_info_t *)calloc(sizeof(default_frame_info_t), frame_len);
186         ERROR_CHECK(ani_info->frames, goto error, "Failed to alloc for default_frame_info_t\n");
187         
188         for (i = 0; i < frame_len; i++) {
189                 frame_obj = json_object_array_get_idx(data_obj, i);
190
191                 frame_data_obj = json_object_object_get(frame_obj, "frame_id");
192                 //printf("\tframe id: %d\n", json_object_get_int(frame_data_obj));
193                 frame_id = json_object_get_int(frame_data_obj);
194
195                 frame_data_obj = json_object_object_get(frame_obj, "led");
196                 led_len = json_object_array_length(frame_data_obj);
197                 
198                 ani_info->frames[frame_id - 1].num_led = led_len;
199                 ani_info->frames[frame_id - 1].leds = (default_led_info_t *)calloc(sizeof(default_led_info_t), led_len);
200                 ERROR_CHECK(ani_info->frames[frame_id - 1].leds, goto error, "Failed to alloc for default_led_info_t\n");
201                 
202                 for (j = 0; j < led_len; j++) {
203                         led_obj = json_object_array_get_idx(frame_data_obj, j);
204
205                         led_data_obj = json_object_object_get(led_obj, "id");
206                         //printf("\t\tid: %2d  ", json_object_get_int(led_data_obj));
207                         led_id = json_object_get_int(led_data_obj);
208
209                         led_data_obj = json_object_object_get(led_obj, "color");
210                         //printf("color: %s\n", json_object_get_string(led_data_obj));
211                         ani_info->frames[frame_id - 1].leds[led_id - 1].color =
212                                 strtol(json_object_get_string(led_data_obj), NULL, 16);
213                 }
214         }
215
216         free(buffer);
217         return ani_info;
218
219 error:
220         free(buffer);
221         if (ani_info) {
222                 if (ani_info->frames) {
223                         for (i = 0; i < ani_info->num_key_frames; i++) {
224                                 if (ani_info->frames[i].leds)
225                                         free(ani_info->frames[i].leds);
226                         }
227                         free(ani_info->frames);
228                 }
229                 free(ani_info->id);
230                 free(ani_info);
231         }
232         return NULL;
233 }
234
235 static int
236 _scandir_filter(const struct dirent *dirent)
237 {
238         if (!strncmp(dirent->d_name, ".", sizeof(".")) ||
239                 !strncmp(dirent->d_name, "..", sizeof("..")))
240                 return 0;
241         if (!strstr(dirent->d_name, ".json"))
242                 return 0;
243
244         return 1;
245 }
246
247
248 pui_int_error
249 _create_ani_collection(void)
250 {
251         pui_int_error e = PUI_INT_ERROR_NONE;
252         default_ani_info *ani_info;
253         struct dirent **files;
254         int count, i;
255         char file_path[MAX_STR] = {0, };
256
257         // load and store all of animation data
258
259         if ((count = scandir(ANI_COLLECTION_DIR, &files, _scandir_filter, alphasort)) == -1) {
260                 printf("Failed to get %s directory (%s)\n", ANI_COLLECTION_DIR, strerror(errno));
261                 e = PUI_INT_ERROR_INVALID_RESOURCES;
262                 return e;
263         }
264
265         for (i = 0; i < count; i++) {
266                 snprintf(file_path, sizeof(file_path), "%s%s", ANI_COLLECTION_DIR, files[i]->d_name);
267
268                 ani_info = _read_json(file_path);
269                 if (ani_info) {
270                         eina_hash_add(_animations_hash, ani_info->id, ani_info);
271                         printf("Success to load %s animation\n", files[i]->d_name);
272                 }
273                 memset(file_path, 0, sizeof(file_path));
274         }
275
276         for (i = 0; i < count; i++) {
277                 free(files[i]);
278         }
279         free(files);
280
281         //TODO
282
283         return e;
284 }
285
286 pui_int_error
287 _is_ani_supported(pui_id id)
288 {
289         pui_int_error e = PUI_INT_ERROR_NONE;
290         default_ani_info *ani_info;
291
292         //TODO
293         /* if the given id is not supported, return PUI_INT_ERROR_ID_NOT_SUPPORTED. */
294         if (!id) {
295                 e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
296                 return e;
297         }
298         ani_info = eina_hash_find(_animations_hash, id);
299         if (!ani_info) e = PUI_INT_ERROR_ID_NOT_SUPPORTED;
300
301         return e;
302 }
303
304 static void
305 _ani_info_cleanup(default_ani_info *ani_info)
306 {
307         int i;
308
309         if (!ani_info) return;
310
311         eina_hash_del(_animations_hash, ani_info->id, ani_info);
312         if (ani_info->frames) {
313                 for (i = 0; i < ani_info->num_key_frames; i++) {
314                         if (ani_info->frames[i].leds)
315                                 free(ani_info->frames[i].leds);
316                 }
317                 free(ani_info->frames);
318         }
319         free(ani_info->id);
320         free(ani_info);
321 }
322
323 pui_backend_ani_data *
324 _ani_create(pui_id id)
325 {
326         pui_int_error e = PUI_INT_ERROR_NONE;
327
328         pui_backend_ani_data *ani_data = NULL;
329         pui_backend_ani_func *ani_func = NULL;
330
331         /* backend's animation specific info */
332         default_ani_info *ani_info = NULL;
333
334         //TODO : return NULL if the animation correspond to the given id dones't exist.
335
336         /* allocation of the structure of function pointers that will be called from pui_ani_control() */
337         ani_func = pui_backend_ani_alloc_ani_func();
338
339         if (!ani_func)
340         {
341                 pui_err("Failed to allocate memory ! (pui backend ani func)\n");
342                 return NULL;
343         }
344
345         /* Assign each function pointer that corresponds to the given id if needed. */
346         ani_func->ani_start = _ani_start;
347         ani_func->ani_stop = _ani_stop;
348
349         ani_func->reserved1 = NULL;
350         ani_func->reserved2 = NULL;
351         ani_func->reserved3 = NULL;
352         ani_func->reserved4 = NULL;
353         ani_func->reserved5 = NULL;
354         ani_func->reserved6 = NULL;
355         ani_func->reserved7 = NULL;
356         ani_func->reserved8 = NULL;
357         ani_func->reserved9 = NULL;
358         ani_func->reserved10 = NULL;
359
360         /* backend's animation specific info */
361         ani_info = (default_ani_info *)calloc(1, sizeof(default_ani_info));
362
363         if (!ani_info)
364         {
365                 pui_err("Failed to allocate memory ! (backend's ani specific info)\n");
366                 goto err;
367         }
368
369         /* fill animation info associate with the given id from animation collection */
370         e = get_ani_info_from_ani_collection(ani_info, id);
371         
372         if (PUI_INT_ERROR_NONE != e)
373         {
374                 pui_err("Failed to get ani info from animation collection !\n");
375                 goto err;
376         }
377
378         /* allocate backend ani_data and return it to pui ani core */
379         ani_data = (pui_backend_ani_data *)calloc(1, sizeof(pui_backend_ani_data));
380         
381         if (!ani_data)
382         {
383                 pui_err("Failed to allocate memory for pui backend ani data !\n");
384                 goto err;
385         }
386         
387         ani_data->ani_func = ani_func;
388         ani_data->ani_info = (pui_backend_ani_info *)ani_info;
389
390         g_ani_data = ani_data;
391
392         return ani_data;
393
394 err:
395         if (ani_func)
396         {
397                 pui_backend_ani_free_ani_func(ani_func);
398                 ani_func = NULL;
399         }
400
401         if (ani_info)
402         {
403                 _ani_info_cleanup(ani_info);
404         }
405
406         return NULL;
407 }
408
409 void
410 _ani_destroy(pui_backend_ani_data *ani_data)
411 {
412         if (!ani_data)
413                 return;
414
415         pui_backend_ani_free_ani_func(ani_data->ani_func);
416
417         //TODO : free if anything needs to be done with ani_info
418         free(ani_data->ani_info);
419
420         ani_data->ani_func = NULL;
421         if (ani_data->ani_info) {
422                 _ani_info_cleanup(ani_data->ani_info);
423                 ani_data->ani_info = NULL;
424         }
425
426         g_ani_data = NULL;
427 }
428
429 static void
430 _animation_data_free_cb(void *data)
431 {
432         int i;
433         default_ani_info *ani_info = (default_ani_info *)data;
434
435         _ani_info_cleanup(ani_info);
436 }
437
438 static pui_backend_module_data *
439 pui_default_backend_init(void)
440 {
441         pui_backend_module_data *backend_data = NULL;
442
443         backend_data = (pui_backend_module_data *)calloc(1, sizeof(pui_backend_module_data));
444
445         if (!backend_data)
446         {
447                 pui_err("Failed to allocate memory for pui backend module data !\n");
448                 return NULL;
449         }
450
451         backend_data->create_ani_collection = _create_ani_collection;
452         backend_data->ani_create = _ani_create;
453         backend_data->ani_destroy = _ani_destroy;
454
455         /* Allocate backend specific data if needed. Now it will be empty. */
456         backend_data->data = NULL;
457         _animations_hash = eina_hash_string_superfast_new(_animation_data_free_cb);
458
459         return backend_data;
460 }
461
462 static void
463 pui_default_backend_deinit(pui_backend_module_data *backend_data)
464 {
465         if (!backend_data)
466                 return;
467
468         if (backend_data->data)
469         {
470                 //TODO : free variables of backend_data
471
472                 free(backend_data->data);
473         }
474
475         if (g_ani_data)
476         {
477                 if (g_ani_data->ani_func)
478                 {
479                         pui_backend_ani_free_ani_func(g_ani_data->ani_func);
480                         g_ani_data->ani_func = NULL;
481                 }
482
483                 if (g_ani_data->ani_info)
484                 {
485                         _ani_info_cleanup(g_ani_data->ani_info);
486                         g_ani_data->ani_info = NULL;
487                 }
488         }
489
490         backend_data->create_ani_collection = NULL;
491         backend_data->ani_create = NULL;
492         backend_data->ani_destroy = NULL;
493
494         free(backend_data);
495         backend_data = NULL;
496 }
497
498 pui_backend_module pui_backend_module_info = {
499         "Tizen Reference Speaker Backend",
500         "Samsung",
501         PUI_BACKEND_SET_ABI_VERSION(1, 0),
502         pui_default_backend_init,
503         pui_default_backend_deinit
504 };
505