Use clock_get_time instead of gettimeofday
[apps/livebox/livebox-edje.git] / src / script_port.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <libgen.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <ctype.h>
22
23 #include <Elementary.h>
24 #include <Evas.h>
25 #include <Edje.h>
26 #include <Eina.h>
27 #include <Ecore.h>
28 #include <Ecore_Evas.h>
29 #include <Eet.h>
30 #include <Ecore_X.h>
31
32 #include <system_settings.h>
33
34 #include <dlog.h>
35 #include <debug.h>
36 #include <vconf.h>
37 #include <livebox-errno.h>
38 #include <livebox-service.h>
39
40 #include "script_port.h"
41
42 #define TEXT_CLASS      "tizen"
43 #define DEFAULT_FONT_SIZE       -100
44
45 #define PUBLIC __attribute__((visibility("default")))
46
47 struct image_option {
48         int orient;
49         int aspect;
50         enum {
51                 FILL_DISABLE,
52                 FILL_IN_SIZE,
53                 FILL_OVER_SIZE
54         } fill;
55
56         int width;
57         int height;
58 };
59
60 struct info {
61         char *file;
62         char *group;
63         char *category;
64         int w;
65         int h;
66
67         Evas *e;
68
69         Eina_List *obj_list;
70 };
71
72 struct child {
73         Evas_Object *obj;
74         char *part;
75 };
76
77 struct obj_info {
78         char *id;
79         Eina_List *children;
80         Evas_Object *parent;
81         Eina_List *access_chain;
82 };
83
84 static struct {
85         char *font_name;
86         int font_size;
87
88         Eina_List *handle_list;
89 } s_info = {
90         .font_name = NULL,
91         .font_size = -100,
92
93         .handle_list = NULL,
94 };
95
96 static inline Evas_Object *find_edje(struct info *handle, const char *id)
97 {
98         Eina_List *l;
99         Evas_Object *edje;
100         struct obj_info *obj_info;
101
102         EINA_LIST_FOREACH(handle->obj_list, l, edje) {
103                 obj_info = evas_object_data_get(edje, "obj_info");
104                 if (!obj_info) {
105                         ErrPrint("Object info is not valid\n");
106                         continue;
107                 }
108
109                 if (!id) {
110                         if (!obj_info->id) {
111                                 return edje;
112                         }
113
114                         continue;
115                 } else if (!obj_info->id) {
116                         continue;
117                 }
118
119                 if (!strcmp(obj_info->id, id)) {
120                         return edje;
121                 }
122         }
123
124         DbgPrint("EDJE[%s] is not found\n", id);
125         return NULL;
126 }
127
128 static inline void rebuild_focus_chain(Evas_Object *obj)
129 {
130         struct obj_info *obj_info;
131         Evas_Object *ao;
132         Eina_List *l;
133
134         obj_info = evas_object_data_get(obj, "obj_info");
135         if (!obj_info) {
136                 ErrPrint("Object info is not available\n");
137                 return;
138         }
139
140         elm_object_focus_custom_chain_unset(obj);
141
142         EINA_LIST_FOREACH(obj_info->access_chain, l, ao) {
143                 DbgPrint("Append %p\n", ao);
144                 elm_object_focus_custom_chain_append(obj, ao, NULL);
145         }
146 }
147
148 PUBLIC const char *script_magic_id(void)
149 {
150         return "edje";
151 }
152
153 PUBLIC int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
154 {
155         struct info *handle = h;
156         Evas_Object *edje;
157         int r[3], g[3], b[3], a[3];
158         int ret;
159
160         edje = find_edje(handle, id);
161         if (!edje) {
162                 return LB_STATUS_ERROR_NOT_EXIST;
163         }
164
165         ret = sscanf(rgba, "%d %d %d %d %d %d %d %d %d %d %d %d",
166                                         r, g, b, a,                     /* OBJECT */
167                                         r + 1, g + 1, b + 1, a + 1,     /* OUTLINE */
168                                         r + 2, g + 2, b + 2, a + 2);    /* SHADOW */
169         if (ret != 12) {
170                 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
171                 return LB_STATUS_ERROR_INVALID;
172         }
173
174         ret = edje_object_color_class_set(elm_layout_edje_get(edje), part,
175                                 r[0], g[0], b[0], a[0], /* OBJECT */
176                                 r[1], g[1], b[1], a[1], /* OUTLINE */
177                                 r[2], g[2], b[2], a[2]); /* SHADOW */
178
179         DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
180         return LB_STATUS_SUCCESS;
181 }
182
183 static void activate_cb(void *data, Evas_Object *part_obj, Elm_Object_Item *item)
184 {
185         Evas_Object *ao;
186         Evas_Object *edje;
187         Evas *e;
188         int x;
189         int y;
190         int w;
191         int h;
192         double timestamp;
193
194         ao = evas_object_data_get(part_obj, "ao");
195         if (!ao) {
196                 return;
197         }
198
199         edje = evas_object_data_get(ao, "edje");
200         if (!edje) {
201                 return;
202         }
203
204         e = evas_object_evas_get(part_obj);
205         evas_object_geometry_get(part_obj, &x, &y, &w, &h);
206         x += w / 2;
207         y += h / 2;
208
209 #if defined(_USE_ECORE_TIME_GET)
210         timestamp = ecore_time_get();
211 #else
212         struct timeval tv;
213         if (gettimeofday(&tv, NULL) < 0) {
214                 ErrPrint("Failed to get time\n");
215                 timestamp = 0.0f;
216         } else {
217                 timestamp = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0f);
218         }
219 #endif
220
221         DbgPrint("Cursor is on %dx%d\n", x, y);
222         evas_event_feed_mouse_move(e, x, y, timestamp * 1000, NULL);
223         evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, (timestamp + 0.01f) * 1000, NULL);
224         evas_event_feed_mouse_move(e, x, y, (timestamp + 0.02f) * 1000, NULL);
225         evas_event_feed_mouse_up(e, 1, EVAS_BUTTON_NONE, (timestamp + 0.03f) * 1000, NULL);
226 }
227
228 PUBLIC int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
229 {
230         struct obj_info *obj_info;
231         struct info *handle = h;
232         Evas_Object *edje;
233         Evas_Object *to;
234
235         edje = find_edje(handle, id);
236         if (!edje) {
237                 ErrPrint("Failed to find EDJE\n");
238                 return LB_STATUS_ERROR_NOT_EXIST;
239         }
240
241         obj_info = evas_object_data_get(edje, "obj_info");
242         if (!obj_info) {
243                 ErrPrint("Object info is not available\n");
244                 return LB_STATUS_ERROR_FAULT;
245         }
246
247         elm_object_part_text_set(edje, part, text ? text : "");
248
249         to = (Evas_Object *)edje_object_part_object_get(elm_layout_edje_get(edje), part);
250         if (to) {
251                 Evas_Object *ao;
252                 char *utf8;
253
254                 ao = evas_object_data_get(to, "ao");
255                 if (!ao) {
256                         ao = elm_access_object_register(to, edje);
257                         if (!ao) {
258                                 ErrPrint("Unable to add ao: %s\n", part);
259                                 goto out;
260                         }
261                         obj_info->access_chain = eina_list_append(obj_info->access_chain, ao);
262                         evas_object_data_set(to, "ao", ao);
263                         evas_object_data_set(ao, "edje", edje);
264                         elm_access_activate_cb_set(ao, activate_cb, NULL);
265                         elm_object_focus_custom_chain_append(edje, ao, NULL);
266                 }
267
268                 if (!text || !strlen(text)) {
269                         obj_info->access_chain = eina_list_remove(obj_info->access_chain, ao);
270                         evas_object_data_del(to, "ao");
271                         evas_object_data_del(ao, "edje");
272                         elm_access_object_unregister(ao);
273                         DbgPrint("[%s] Remove access object\n", part);
274
275                         rebuild_focus_chain(edje);
276                         goto out;
277                 }
278
279                 utf8 = elm_entry_markup_to_utf8(text);
280                 if ((!utf8 || !strlen(utf8))) {
281                         free(utf8);
282
283                         obj_info->access_chain = eina_list_remove(obj_info->access_chain, ao);
284                         evas_object_data_del(to, "ao");
285                         evas_object_data_del(ao, "edje");
286                         elm_access_object_unregister(ao);
287                         DbgPrint("[%s] Remove access object\n", part);
288
289                         rebuild_focus_chain(edje);
290                         goto out;
291                 }
292
293                 elm_access_info_set(ao, ELM_ACCESS_INFO, utf8);
294                 free(utf8);
295         } else {
296                 ErrPrint("Unable to get text part[%s]\n", part);
297         }
298
299 out:
300         return LB_STATUS_SUCCESS;
301 }
302
303 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
304 {
305         while (len > 0 && *value == ' ') {
306                 value++;
307                 len--;
308         }
309
310         if (len < 4) {
311                 return;
312         }
313
314         img_opt->aspect = !strncasecmp(value, "true", 4);
315         DbgPrint("Parsed ASPECT: %d (%s)\n", img_opt->aspect, value);
316 }
317
318 static void parse_orient(struct image_option *img_opt, const char *value, int len)
319 {
320         while (len > 0 && *value == ' ') {
321                 value++;
322                 len--;
323         }
324
325         if (len < 4) {
326                 return;
327         }
328
329         img_opt->orient = !strncasecmp(value, "true", 4);
330         DbgPrint("Parsed ORIENT: %d (%s)\n", img_opt->orient, value);
331 }
332
333 static void parse_size(struct image_option *img_opt, const char *value, int len)
334 {
335         int width;
336         int height;
337         char *buf;
338
339         while (len > 0 && *value == ' ') {
340                 value++;
341                 len--;
342         }
343
344         buf = strndup(value, len);
345         if (!buf) {
346                 ErrPrint("Heap: %s\n", strerror(errno));
347                 return;
348         }
349
350         if (sscanf(buf, "%dx%d", &width, &height) == 2) {
351                 img_opt->width = width;
352                 img_opt->height = height;
353                 DbgPrint("Parsed size : %dx%d (%s)\n", width, height, buf);
354         } else {
355                 DbgPrint("Invalid size tag[%s]\n", buf);
356         }
357
358         free(buf);
359 }
360
361 static void parse_fill(struct image_option *img_opt, const char *value, int len)
362 {
363         while (len > 0 && *value == ' ') {
364                 value++;
365                 len--;
366         }
367
368         if (!strncasecmp(value, "in-size", len)) {
369                 img_opt->fill = FILL_IN_SIZE;
370         } else if (!strncasecmp(value, "over-size", len)) {
371                 img_opt->fill = FILL_OVER_SIZE;
372         } else {
373                 img_opt->fill = FILL_DISABLE;
374         }
375
376         DbgPrint("Parsed FILL: %d (%s)\n", img_opt->fill, value);
377 }
378
379 static inline void parse_image_option(const char *option, struct image_option *img_opt)
380 {
381         const char *ptr;
382         const char *cmd;
383         const char *value;
384         struct {
385                 const char *cmd;
386                 void (*handler)(struct image_option *img_opt, const char *value, int len);
387         } cmd_list[] = {
388                 {
389                         .cmd = "aspect", /* Keep the aspect ratio */
390                         .handler = parse_aspect,
391                 },
392                 {
393                         .cmd = "orient", /* Keep the orientation value: for the rotated images */
394                         .handler = parse_orient,
395                 },
396                 {
397                         .cmd = "fill", /* Fill the image to its container */
398                         .handler = parse_fill, /* Value: in-size, over-size, disable(default) */
399                 },
400                 {
401                         .cmd = "size",
402                         .handler = parse_size,
403                 },
404         };
405         enum {
406                 STATE_START,
407                 STATE_TOKEN,
408                 STATE_DATA,
409                 STATE_IGNORE,
410                 STATE_ERROR,
411                 STATE_END
412         } state;
413         int idx;
414         int tag;
415
416         if (!option || !*option) {
417                 return;
418         }
419
420         state = STATE_START;
421         /*!
422          * \note
423          * GCC 4.7 warnings uninitialized idx and tag value.
424          * But it will be initialized by the state machine. :(
425          * Anyway, I just reset idx and tag for reducing the GCC4.7 complains.
426          */
427         idx = 0;
428         tag = 0;
429         cmd = NULL;
430         value = NULL;
431
432         for (ptr = option; state != STATE_END; ptr++) {
433                 switch (state) {
434                 case STATE_START:
435                         if (*ptr == '\0') {
436                                 state = STATE_END;
437                                 continue;
438                         }
439
440                         if (isalpha(*ptr)) {
441                                 state = STATE_TOKEN;
442                                 ptr--;
443                         }
444                         tag = 0;
445                         idx = 0;
446
447                         cmd = cmd_list[tag].cmd;
448                         break;
449                 case STATE_IGNORE:
450                         if (*ptr == '=') {
451                                 state = STATE_DATA;
452                                 value = ptr;
453                         } else if (*ptr == '\0') {
454                                 state = STATE_END;
455                         }
456                         break;
457                 case STATE_TOKEN:
458                         if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
459                                 if (*ptr == '=') {
460                                         value = ptr;
461                                         state = STATE_DATA;
462                                 } else {
463                                         state = STATE_IGNORE;
464                                 }
465                                 idx = 0;
466                         } else if (*ptr == '\0') {
467                                 state = STATE_END;
468                         } else if (cmd[idx] == *ptr) {
469                                 idx++;
470                         } else {
471                                 ptr -= (idx + 1);
472
473                                 tag++;
474                                 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
475                                         tag = 0;
476                                         state = STATE_ERROR;
477                                 } else {
478                                         cmd = cmd_list[tag].cmd;
479                                 }
480                                 idx = 0;
481                         }
482                         break;
483                 case STATE_DATA:
484                         if (*ptr == ';' || *ptr == '\0') {
485                                 cmd_list[tag].handler(img_opt, value + 1, idx);
486                                 state = *ptr ? STATE_START : STATE_END;
487                         } else {
488                                 idx++;
489                         }
490                         break;
491                 case STATE_ERROR:
492                         if (*ptr == ';') {
493                                 state = STATE_START;
494                         } else if (*ptr == '\0') {
495                                 state = STATE_END;
496                         }
497                         break;
498                 default:
499                         break;
500                 }
501         }
502 }
503
504 PUBLIC int script_update_access(void *_h, Evas *e, const char *id, const char *part, const char *text, const char *option)
505 {
506         struct info *handle = _h;
507         Evas_Object *edje;
508         struct obj_info *obj_info;
509         Evas_Object *to;
510
511         edje = find_edje(handle, id);
512         if (!edje) {
513                 ErrPrint("No such object: %s\n", id);
514                 return LB_STATUS_ERROR_NOT_EXIST;
515         }
516
517         obj_info = evas_object_data_get(edje, "obj_info");
518         if (!obj_info) {
519                 ErrPrint("Object info is not available\n");
520                 return LB_STATUS_ERROR_FAULT;
521         }
522
523         to = (Evas_Object *)edje_object_part_object_get(elm_layout_edje_get(edje), part);
524         if (to) {
525                 Evas_Object *ao;
526
527                 ao = evas_object_data_get(to, "ao");
528                 if (ao) {
529                         if (text && strlen(text)) {
530                                 elm_access_info_set(ao, ELM_ACCESS_INFO, text);
531                         } else {
532                                 obj_info->access_chain = eina_list_remove(obj_info->access_chain, ao);
533                                 evas_object_data_del(to, "ao");
534                                 evas_object_data_del(ao, "edje");
535                                 elm_access_object_unregister(ao);
536                                 DbgPrint("Successfully unregistered\n");
537
538                                 rebuild_focus_chain(edje);
539                         }
540                 } else if (text && strlen(text)) {
541                         ao = elm_access_object_register(to, edje);
542                         if (!ao) {
543                                 ErrPrint("Unable to register access object\n");
544                         } else {
545                                 elm_access_info_set(ao, ELM_ACCESS_INFO, text);
546                                 obj_info->access_chain = eina_list_append(obj_info->access_chain, ao);
547                                 evas_object_data_set(to, "ao", ao);
548                                 elm_object_focus_custom_chain_append(edje, ao, NULL);
549                                 DbgPrint("[%s] Register access info: (%s)\n", part, text);
550                                 evas_object_data_set(ao, "edje", edje);
551                                 elm_access_activate_cb_set(ao, activate_cb, NULL);
552                         }
553                 }
554         } else {
555                 ErrPrint("[%s] is not exists\n", part);
556         }
557
558         return LB_STATUS_SUCCESS;
559 }
560
561 PUBLIC int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
562 {
563         struct info *handle = _h;
564         Evas_Load_Error err;
565         Evas_Object *edje;
566         Evas_Object *img;
567         Evas_Coord w, h;
568         struct obj_info *obj_info;
569         struct child *child;
570         struct image_option img_opt = {
571                 .aspect = 0,
572                 .orient = 0,
573                 .fill = FILL_DISABLE,
574                 .width = -1,
575                 .height = -1,
576         };
577
578         edje = find_edje(handle, id);
579         if (!edje) {
580                 ErrPrint("No such object: %s\n", id);
581                 return LB_STATUS_ERROR_NOT_EXIST;
582         }
583
584         obj_info = evas_object_data_get(edje, "obj_info");
585         if (!obj_info) {
586                 ErrPrint("Object info is not available\n");
587                 return LB_STATUS_ERROR_FAULT;
588         }
589
590         img = elm_object_part_content_unset(edje, part);
591         if (img) {
592                 Eina_List *l;
593                 Eina_List *n;
594                 Evas_Object *ao;
595
596                 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
597                         if (child->obj != img) {
598                                 continue;
599                         }
600
601                         obj_info->children = eina_list_remove(obj_info->children, child);
602                         free(child->part);
603                         free(child);
604                         break;
605                 }
606
607                 DbgPrint("delete object %s %p\n", part, img);
608                 ao = evas_object_data_del(img, "ao");
609                 if (ao) {
610                         obj_info->access_chain = eina_list_remove(obj_info->access_chain, ao);
611                         evas_object_data_del(ao, "edje");
612                         elm_access_object_unregister(ao);
613                         DbgPrint("Successfully unregistered\n");
614                 }
615                 evas_object_del(img);
616
617                 rebuild_focus_chain(edje);
618         }
619
620         if (!path || !strlen(path) || access(path, R_OK) != 0) {
621                 DbgPrint("SKIP - Path: [%s]\n", path);
622                 return LB_STATUS_SUCCESS;
623         }
624
625         child = malloc(sizeof(*child));
626         if (!child) {
627                 ErrPrint("Heap: %s\n", strerror(errno));
628                 return LB_STATUS_ERROR_MEMORY;
629         }
630
631         child->part = strdup(part);
632         if (!child->part) {
633                 ErrPrint("Heap: %s\n", strerror(errno));
634                 free(child);
635                 return LB_STATUS_ERROR_MEMORY;
636         }
637
638         img = evas_object_image_add(e);
639         if (!img) {
640                 ErrPrint("Failed to add an image object\n");
641                 free(child->part);
642                 free(child);
643                 return LB_STATUS_ERROR_FAULT;
644         }
645
646         evas_object_image_preload(img, EINA_FALSE);
647         parse_image_option(option, &img_opt);
648         evas_object_image_load_orientation_set(img, img_opt.orient);
649
650         evas_object_image_file_set(img, path, NULL);
651         err = evas_object_image_load_error_get(img);
652         if (err != EVAS_LOAD_ERROR_NONE) {
653                 ErrPrint("Load error: %s\n", evas_load_error_str(err));
654                 evas_object_del(img);
655                 free(child->part);
656                 free(child);
657                 return LB_STATUS_ERROR_IO;
658         }
659
660         evas_object_image_size_get(img, &w, &h);
661         if (img_opt.aspect) {
662                 if (img_opt.fill == FILL_OVER_SIZE) {
663                         Evas_Coord part_w;
664                         Evas_Coord part_h;
665
666                         if (img_opt.width >= 0 && img_opt.height >= 0) {
667                                 part_w = img_opt.width * elm_config_scale_get();
668                                 part_h = img_opt.height * elm_config_scale_get();
669                         } else {
670                                 part_w = 0;
671                                 part_h = 0;
672                                 edje_object_part_geometry_get(elm_layout_edje_get(edje), part, NULL, NULL, &part_w, &part_h);
673                         }
674                         DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
675
676                         if (part_w > w || part_h > h) {
677                                 double fw;
678                                 double fh;
679
680                                 fw = (double)part_w / (double)w;
681                                 fh = (double)part_h / (double)h;
682
683                                 if (fw > fh) {
684                                         w = part_w;
685                                         h = (double)h * fw;
686                                 } else {
687                                         h = part_h;
688                                         w = (double)w * fh;
689                                 }
690                         }
691
692                         if (!part_w || !part_h || !w || !h) {
693                                 evas_object_del(img);
694                                 free(child->part);
695                                 free(child);
696                                 return LB_STATUS_ERROR_INVALID;
697                         }
698
699                         if (evas_object_image_region_support_get(img)) {
700                                 evas_object_image_load_region_set(img, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h);
701                                 evas_object_image_load_size_set(img, part_w, part_h);
702                                 evas_object_image_filled_set(img, EINA_TRUE);
703                                 //evas_object_image_fill_set(img, 0, 0, part_w, part_h);
704                                 DbgPrint("Size: %dx%d (region: %dx%d - %dx%d)\n", w, h, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h);
705                         } else {
706                                 Ecore_Evas *ee;
707                                 Evas *e;
708                                 Evas_Object *src_img;
709                                 Evas_Coord rw, rh;
710                                 const void *data;
711
712                                 DbgPrint("Part loading is not supported\n");
713                                 ee = ecore_evas_buffer_new(part_w, part_h);
714                                 if (!ee) {
715                                         ErrPrint("Failed to create a EE\n");
716                                         evas_object_del(img);
717                                         free(child->part);
718                                         free(child);
719                                         return LB_STATUS_ERROR_FAULT;
720                                 }
721
722                                 ecore_evas_alpha_set(ee, EINA_TRUE);
723
724                                 e = ecore_evas_get(ee);
725                                 if (!e) {
726                                         ErrPrint("Unable to get Evas\n");
727                                         ecore_evas_free(ee);
728
729                                         evas_object_del(img);
730                                         free(child->part);
731                                         free(child);
732                                         return LB_STATUS_ERROR_FAULT;
733                                 }
734
735                                 src_img = evas_object_image_filled_add(e);
736                                 if (!src_img) {
737                                         ErrPrint("Unable to add an image\n");
738                                         ecore_evas_free(ee);
739
740                                         evas_object_del(img);
741                                         free(child->part);
742                                         free(child);
743                                         return LB_STATUS_ERROR_FAULT;
744                                 }
745
746                                 evas_object_image_alpha_set(src_img, EINA_TRUE);
747                                 evas_object_image_colorspace_set(src_img, EVAS_COLORSPACE_ARGB8888);
748                                 evas_object_image_smooth_scale_set(src_img, EINA_TRUE);
749                                 evas_object_image_load_orientation_set(src_img, img_opt.orient);
750                                 evas_object_image_file_set(src_img, path, NULL);
751                                 err = evas_object_image_load_error_get(src_img);
752                                 if (err != EVAS_LOAD_ERROR_NONE) {
753                                         ErrPrint("Load error: %s\n", evas_load_error_str(err));
754                                         evas_object_del(src_img);
755                                         ecore_evas_free(ee);
756
757                                         evas_object_del(img);
758                                         free(child->part);
759                                         free(child);
760                                         return LB_STATUS_ERROR_IO;
761                                 }
762                                 evas_object_image_size_get(src_img, &rw, &rh);
763                                 evas_object_image_fill_set(src_img, 0, 0, rw, rh);
764                                 evas_object_resize(src_img, w, h);
765                                 evas_object_move(src_img, -(w - part_w) / 2, -(h - part_h) / 2);
766                                 evas_object_show(src_img);
767
768                                 data = ecore_evas_buffer_pixels_get(ee);
769                                 if (!data) {
770                                         ErrPrint("Unable to get pixels\n");
771                                         evas_object_del(src_img);
772                                         ecore_evas_free(ee);
773
774                                         evas_object_del(img);
775                                         free(child->part);
776                                         free(child);
777                                         return LB_STATUS_ERROR_IO;
778                                 }
779
780                                 e = evas_object_evas_get(img);
781                                 evas_object_del(img);
782                                 img = evas_object_image_filled_add(e);
783                                 if (!img) {
784                                         evas_object_del(src_img);
785                                         ecore_evas_free(ee);
786
787                                         free(child->part);
788                                         free(child);
789                                         return LB_STATUS_ERROR_MEMORY;
790                                 }
791
792                                 evas_object_image_colorspace_set(img, EVAS_COLORSPACE_ARGB8888);
793                                 evas_object_image_smooth_scale_set(img, EINA_TRUE);
794                                 evas_object_image_alpha_set(img, EINA_TRUE);
795                                 evas_object_image_data_set(img, NULL);
796                                 evas_object_image_size_set(img, part_w, part_h);
797                                 evas_object_resize(img, part_w, part_h);
798                                 evas_object_image_data_copy_set(img, (void *)data);
799                                 evas_object_image_fill_set(img, 0, 0, part_w, part_h);
800                                 evas_object_image_data_update_add(img, 0, 0, part_w, part_h);
801
802                                 evas_object_del(src_img);
803                                 ecore_evas_free(ee);
804                         }
805                 } else if (img_opt.fill == FILL_IN_SIZE) {
806                         Evas_Coord part_w;
807                         Evas_Coord part_h;
808
809                         if (img_opt.width >= 0 && img_opt.height >= 0) {
810                                 part_w = img_opt.width * elm_config_scale_get();
811                                 part_h = img_opt.height * elm_config_scale_get();
812                         } else {
813                                 part_w = 0;
814                                 part_h = 0;
815                                 edje_object_part_geometry_get(elm_layout_edje_get(edje), part, NULL, NULL, &part_w, &part_h);
816                         }
817                         DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
818
819                         if (part_w > w || part_h > h) {
820                                 double fw;
821                                 double fh;
822
823                                 fw = (double)part_w / (double)w;
824                                 fh = (double)part_h / (double)h;
825
826                                 if (fw > fh) {
827                                         w = part_w;
828                                         h = (double)h * fw;
829                                 } else {
830                                         h = part_h;
831                                         w = (double)w * fh;
832                                 }
833                         }
834                         DbgPrint("Size: %dx%d\n", w, h);
835                         evas_object_image_fill_set(img, 0, 0, part_w, part_h);
836                         evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
837                         evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
838                 } else {
839                         evas_object_image_fill_set(img, 0, 0, w, h);
840                         evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
841                         evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
842                 }
843         } else {
844                 if (img_opt.width >= 0 && img_opt.height >= 0) {
845                         w = img_opt.width;
846                         h = img_opt.height;
847                         DbgPrint("Using given image size: %dx%d\n", w, h);
848                 }
849
850                 evas_object_image_fill_set(img, 0, 0, w, h);
851                 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
852                 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
853                 evas_object_image_filled_set(img, EINA_TRUE);
854         }
855
856         /*!
857          * \note
858          * object will be shown by below statement automatically
859          */
860         DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
861         child->obj = img;
862         elm_object_part_content_set(edje, part, img);
863         obj_info->children = eina_list_append(obj_info->children, child);
864
865         /*!
866          * \note
867          * This object is not registered as an access object.
868          * So the developer should add it to access list manually, using DESC_ACCESS block.
869          */
870         return LB_STATUS_SUCCESS;
871 }
872
873 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
874 {
875         struct info *handle = data;
876         Evas_Coord w;
877         Evas_Coord h;
878         Evas_Coord px = 0;
879         Evas_Coord py = 0;
880         Evas_Coord pw = 0;
881         Evas_Coord ph = 0;
882         double sx;
883         double sy;
884         double ex;
885         double ey;
886
887         evas_object_geometry_get(obj, NULL, NULL, &w, &h);
888         edje_object_part_geometry_get(elm_layout_edje_get(obj), source, &px, &py, &pw, &ph);
889
890         sx = ex = 0.0f;
891         if (w) {
892                 sx = (double)px / (double)w;
893                 ex = (double)(px + pw) / (double)w;
894         }
895
896         sy = ey = 0.0f;
897         if (h) {
898                 sy = (double)py / (double)h;
899                 ey = (double)(py + ph) / (double)h;
900         }
901
902         script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
903 }
904
905 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
906 {
907         struct info *handle = _info;
908         struct obj_info *obj_info;
909         struct obj_info *parent_obj_info;
910         struct child *child;
911         Evas_Object *ao;
912
913         handle->obj_list = eina_list_remove(handle->obj_list, obj);
914
915         obj_info = evas_object_data_del(obj, "obj_info");
916         if (!obj_info) {
917                 ErrPrint("Object info is not valid\n");
918                 return;
919         }
920
921         DbgPrint("delete object %s %p\n", obj_info->id, obj);
922         parent_obj_info = evas_object_data_get(obj_info->parent, "obj_info");
923         if (parent_obj_info) {
924                 Eina_List *l;
925                 Eina_List *n;
926
927                 EINA_LIST_FOREACH_SAFE(parent_obj_info->children, l, n, child) {
928                         if (child->obj != obj) {
929                                 continue;
930                         }
931
932                         /*!
933                          * \note
934                          * If this code is executed,
935                          * The parent is not deleted by desc, this object is deleted by itself.
936                          * It is not possible, but we care it.
937                          */
938                         DbgPrint("Parent's children is updated: %s\n", child->part);
939                         parent_obj_info->children = eina_list_remove(parent_obj_info->children, child);
940                         free(child->part);
941                         free(child);
942                         break;
943                 }
944         } else {
945                 DbgPrint("Parent EDJE\n");
946         }
947
948         elm_object_signal_callback_del(obj, "*", "*", script_signal_cb);
949
950         elm_object_focus_custom_chain_unset(obj);
951
952         EINA_LIST_FREE(obj_info->children, child) {
953                 DbgPrint("delete object %s %p\n", child->part, child->obj);
954                 if (child->obj) {
955                         Evas_Object *ao;
956                         ao = evas_object_data_del(child->obj, "ao");
957                         if (ao) {
958                                 obj_info->access_chain = eina_list_remove(obj_info->access_chain, ao);
959                                 evas_object_data_del(ao, "edje");
960                                 elm_access_object_unregister(ao);
961                         }
962                         evas_object_del(child->obj);
963                 }
964                 free(child->part);
965                 free(child);
966         }
967
968         EINA_LIST_FREE(obj_info->access_chain, ao) {
969                 evas_object_data_del(ao, "edje");
970                 elm_access_object_unregister(ao);
971         }
972
973         free(obj_info->id);
974         free(obj_info);
975 }
976
977 /*!
978         LB_ACCESS_HIGHLIGHT             0
979         LB_ACCESS_HIGHLIGHT_NEXT        1
980         LB_ACCESS_HIGHLIGHT_PREV        2
981         LB_ACCESS_ACTIVATE              3
982         LB_ACCESS_ACTION                4
983         LB_ACCESS_SCROLL                5
984 */
985 PUBLIC int script_feed_event(void *h, Evas *e, int event_type, int x, int y, int down, double timestamp)
986 {
987         struct info *handle = h;
988         Evas_Object *edje;
989         struct obj_info *obj_info;
990         int ret = LB_STATUS_SUCCESS;
991
992         edje = find_edje(handle, NULL); /*!< Get the base layout */
993         if (!edje) {
994                 ErrPrint("Base layout is not exist\n");
995                 return LB_STATUS_ERROR_NOT_EXIST;
996         }
997
998         obj_info = evas_object_data_get(edje, "obj_info");
999         if (!obj_info) {
1000                 ErrPrint("Object info is not valid\n");
1001                 return LB_STATUS_ERROR_INVALID;
1002         }
1003
1004         if (event_type & LB_SCRIPT_ACCESS_EVENT) {
1005                 Elm_Access_Action_Info info;
1006                 Elm_Access_Action_Type action;
1007
1008                 memset(&info, 0, sizeof(info));
1009
1010                 if ((event_type & LB_SCRIPT_ACCESS_HIGHLIGHT) == LB_SCRIPT_ACCESS_HIGHLIGHT) {
1011                         action = ELM_ACCESS_ACTION_HIGHLIGHT;
1012                         info.x = x;
1013                         info.y = y;
1014                         ret = elm_access_action(edje, action, &info);
1015                         DbgPrint("ACCESS_HIGHLIGHT: %dx%d returns %d\n", x, y, ret);
1016                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1017                 } else if ((event_type & LB_SCRIPT_ACCESS_HIGHLIGHT_NEXT) == LB_SCRIPT_ACCESS_HIGHLIGHT_NEXT) {
1018                         action = ELM_ACCESS_ACTION_HIGHLIGHT_NEXT;
1019                         info.highlight_cycle = EINA_FALSE;
1020                         ret = elm_access_action(edje, action, &info);
1021                         DbgPrint("ACCESS_HIGHLIGHT_NEXT, returns %d\n", ret);
1022                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_LAST : LB_ACCESS_STATUS_DONE;
1023                 } else if ((event_type & LB_SCRIPT_ACCESS_HIGHLIGHT_PREV) == LB_SCRIPT_ACCESS_HIGHLIGHT_PREV) {
1024                         action = ELM_ACCESS_ACTION_HIGHLIGHT_PREV;
1025                         info.highlight_cycle = EINA_FALSE;
1026                         ret = elm_access_action(edje, action, &info);
1027                         DbgPrint("ACCESS_HIGHLIGHT_PREV, returns %d\n", ret);
1028                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_FIRST : LB_ACCESS_STATUS_DONE;
1029                 } else if ((event_type & LB_SCRIPT_ACCESS_ACTIVATE) == LB_SCRIPT_ACCESS_ACTIVATE) {
1030                         action = ELM_ACCESS_ACTION_ACTIVATE;
1031                         ret = elm_access_action(edje, action, &info);
1032                         DbgPrint("ACCESS_ACTIVATE, returns %d\n", ret);
1033                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1034                 } else if ((event_type & LB_SCRIPT_ACCESS_ACTION) == LB_SCRIPT_ACCESS_ACTION) {
1035                         if (down == 0) {
1036                                 action = ELM_ACCESS_ACTION_UP;
1037                                 ret = elm_access_action(edje, action, &info);
1038                                 DbgPrint("ACCESS_ACTION(%d), returns %d\n", down, ret);
1039                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1040                         } else if (down == 1) {
1041                                 action = ELM_ACCESS_ACTION_DOWN;
1042                                 ret = elm_access_action(edje, action, &info);
1043                                 DbgPrint("ACCESS_ACTION(%d), returns %d\n", down, ret);
1044                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1045                         } else {
1046                                 ErrPrint("Invalid access event\n");
1047                                 ret = LB_ACCESS_STATUS_ERROR;
1048                         }
1049                 } else if ((event_type & LB_SCRIPT_ACCESS_SCROLL) == LB_SCRIPT_ACCESS_SCROLL) {
1050                         action = ELM_ACCESS_ACTION_SCROLL;
1051                         info.x = x;
1052                         info.y = y;
1053                         switch (down) {
1054                         case 0:
1055                                 info.mouse_type = 0;
1056                                 ret = elm_access_action(edje, action, &info);
1057                                 DbgPrint("ACCESS_HIGHLIGHT_SCROLL, returns %d\n", ret);
1058                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1059                                 break;
1060                         case -1:
1061                                 info.mouse_type = 1;
1062                                 ret = elm_access_action(edje, action, &info);
1063                                 DbgPrint("ACCESS_HIGHLIGHT_SCROLL, returns %d\n", ret);
1064                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1065                                 break;
1066                         case 1:
1067                                 info.mouse_type = 2;
1068                                 ret = elm_access_action(edje, action, &info);
1069                                 DbgPrint("ACCESS_HIGHLIGHT_SCROLL, returns %d\n", ret);
1070                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1071                                 break;
1072                         default:
1073                                 ret = LB_ACCESS_STATUS_ERROR;
1074                                 break;
1075                         }
1076                 } else if ((event_type & LB_SCRIPT_ACCESS_UNHIGHLIGHT) == LB_SCRIPT_ACCESS_UNHIGHLIGHT) {
1077                         action = ELM_ACCESS_ACTION_UNHIGHLIGHT;
1078                         ret = elm_access_action(edje, action, &info);
1079                         DbgPrint("ACCESS_UNHIGHLIGHT, returns %d\n", ret);
1080                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
1081                 } else {
1082                         DbgPrint("Invalid event\n");
1083                         ret = LB_ACCESS_STATUS_ERROR;
1084                 }
1085
1086         } else if (event_type & LB_SCRIPT_MOUSE_EVENT) {
1087                 switch (event_type) {
1088                 case LB_SCRIPT_MOUSE_DOWN:
1089                         evas_event_feed_mouse_move(e, x, y, timestamp * 1000, NULL);
1090                         evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, (timestamp + 0.01f) * 1000, NULL);
1091                         break;
1092                 case LB_SCRIPT_MOUSE_MOVE:
1093                         evas_event_feed_mouse_move(e, x, y, timestamp * 1000, NULL);
1094                         break;
1095                 case LB_SCRIPT_MOUSE_UP:
1096                         evas_event_feed_mouse_move(e, x, y, timestamp * 1000, NULL);
1097                         evas_event_feed_mouse_up(e, 1, EVAS_BUTTON_NONE, (timestamp + 0.1f) * 1000, NULL);
1098                         break;
1099                 case LB_SCRIPT_MOUSE_IN:
1100                         evas_event_feed_mouse_in(e, timestamp * 1000, NULL);
1101                         break;
1102                 case LB_SCRIPT_MOUSE_OUT:
1103                         evas_event_feed_mouse_out(e, timestamp * 1000, NULL);
1104                         break;
1105                 default:
1106                         return LB_STATUS_ERROR_INVALID;
1107                 }
1108         } else if (event_type & LB_SCRIPT_KEY_EVENT) {
1109                 DbgPrint("Key event is not implemented\n");
1110                 return LB_STATUS_ERROR_NOT_IMPLEMENTED;
1111         }
1112
1113         return ret;
1114 }
1115
1116 PUBLIC int script_update_script(void *h, Evas *e, const char *src_id, const char *target_id, const char *part, const char *path, const char *group)
1117 {
1118         struct info *handle = h;
1119         Evas_Object *edje;
1120         Evas_Object *obj;
1121         struct obj_info *obj_info;
1122         struct child *child;
1123         char _target_id[32];
1124
1125         edje = find_edje(handle, src_id);
1126         if (!edje) {
1127                 ErrPrint("Edje is not exists (%s)\n", src_id);
1128                 return LB_STATUS_ERROR_NOT_EXIST;
1129         }
1130
1131         obj_info = evas_object_data_get(edje, "obj_info");
1132         if (!obj_info) {
1133                 ErrPrint("Object info is not valid\n");
1134                 return LB_STATUS_ERROR_INVALID;
1135         }
1136
1137         obj = elm_object_part_content_unset(edje, part);
1138         if (obj) {
1139                 Eina_List *l;
1140                 Eina_List *n;
1141
1142                 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
1143                         if (child->obj != obj) {
1144                                 continue;
1145                         }
1146
1147                         obj_info->children = eina_list_remove(obj_info->children, child);
1148
1149                         free(child->part);
1150                         free(child);
1151                         break;
1152                 }
1153
1154                 DbgPrint("delete object %s %p\n", part, obj);
1155                 /*!
1156                  * \note
1157                  * This will call the edje_del_cb.
1158                  * It will delete all access objects
1159                  */
1160                 evas_object_del(obj);
1161         }
1162
1163         if (!path || !strlen(path) || access(path, R_OK) != 0) {
1164                 DbgPrint("SKIP - Path: [%s]\n", path);
1165                 return LB_STATUS_SUCCESS;
1166         }
1167
1168         if (!target_id) {
1169                 if (find_edje(handle, part)) {
1170                         double timestamp;
1171
1172                         do {
1173 #if defined(_USE_ECORE_TIME_GET)
1174                                 timestamp = ecore_time_get();
1175 #else
1176                                 struct timeval tv;
1177                                 if (gettimeofday(&tv, NULL) < 0) {
1178                                         static int local_idx = 0;
1179                                         timestamp = (double)(local_idx++);
1180                                 } else {
1181                                         timestamp = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0f);
1182                                 }
1183 #endif
1184
1185                                 snprintf(_target_id, sizeof(_target_id), "%lf", timestamp);
1186                         } while (find_edje(handle, _target_id));
1187
1188                         target_id = _target_id;
1189                 } else {
1190                         target_id = part;
1191                 }
1192
1193                 DbgPrint("Anonymouse target id: %s\n", target_id);
1194         }
1195
1196         obj = elm_layout_add(edje);
1197         if (!obj) {
1198                 ErrPrint("Failed to add a new edje object\n");
1199                 return LB_STATUS_ERROR_FAULT;
1200         }
1201
1202         edje_object_scale_set(elm_layout_edje_get(obj), elm_config_scale_get());
1203
1204         if (!elm_layout_file_set(obj, path, group)) {
1205                 int err;
1206                 err = edje_object_load_error_get(elm_layout_edje_get(obj));
1207                 ErrPrint("Could not load %s from %s: %s\n", group, path, edje_load_error_str(err));
1208                 evas_object_del(obj);
1209                 return LB_STATUS_ERROR_IO;
1210         }
1211
1212         evas_object_show(obj);
1213
1214         obj_info = calloc(1, sizeof(*obj_info));
1215         if (!obj_info) {
1216                 ErrPrint("Failed to add a obj_info\n");
1217                 evas_object_del(obj);
1218                 return LB_STATUS_ERROR_MEMORY;
1219         }
1220
1221         obj_info->id = strdup(target_id);
1222         if (!obj_info->id) {
1223                 ErrPrint("Failed to add a obj_info\n");
1224                 free(obj_info);
1225                 evas_object_del(obj);
1226                 return LB_STATUS_ERROR_MEMORY;
1227         }
1228
1229         obj_info->parent = edje;
1230
1231         child = malloc(sizeof(*child));
1232         if (!child) {
1233                 ErrPrint("Error: %s\n", strerror(errno));
1234                 free(obj_info->id);
1235                 free(obj_info);
1236                 evas_object_del(obj);
1237                 return LB_STATUS_ERROR_MEMORY;
1238         }
1239
1240         child->part = strdup(part);
1241         if (!child->part) {
1242                 ErrPrint("Error: %s\n", strerror(errno));
1243                 free(child);
1244                 free(obj_info->id);
1245                 free(obj_info);
1246                 evas_object_del(obj);
1247                 return LB_STATUS_ERROR_MEMORY;
1248         }
1249
1250         child->obj = obj;
1251
1252         evas_object_data_set(obj, "obj_info", obj_info);
1253         evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
1254         elm_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
1255         handle->obj_list = eina_list_append(handle->obj_list, obj);
1256
1257         DbgPrint("%s part swallow edje %p\n", part, obj);
1258         elm_object_part_content_set(edje, part, obj);
1259
1260         obj_info = evas_object_data_get(edje, "obj_info");
1261         obj_info->children = eina_list_append(obj_info->children, child);
1262         return LB_STATUS_SUCCESS;
1263 }
1264
1265 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
1266 {
1267         struct info *handle = h;
1268         Evas_Object *edje;
1269
1270         edje = find_edje(handle, id);
1271         if (!edje) {
1272                 return LB_STATUS_ERROR_NOT_EXIST;
1273         }
1274
1275         elm_object_signal_emit(edje, signal, part);
1276         return LB_STATUS_SUCCESS;
1277 }
1278
1279 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
1280 {
1281         struct info *handle = h;
1282         Evas_Object *edje;
1283
1284         edje = find_edje(handle, id);
1285         if (!edje) {
1286                 return LB_STATUS_ERROR_NOT_EXIST;
1287         }
1288
1289         edje_object_part_drag_value_set(elm_layout_edje_get(edje), part, x, y);
1290         return LB_STATUS_SUCCESS;
1291 }
1292
1293 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
1294 {
1295         struct info *handle = han;
1296         Evas_Object *edje;
1297
1298         edje = find_edje(handle, id);
1299         if (!edje) {
1300                 return LB_STATUS_ERROR_NOT_EXIST;
1301         }
1302
1303         if (!id) {
1304                 handle->w = w;
1305                 handle->h = h;
1306         }
1307
1308         DbgPrint("Resize object to %dx%d\n", w, h);
1309         evas_object_resize(edje, w, h);
1310         return LB_STATUS_SUCCESS;
1311 }
1312
1313 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
1314 {
1315         struct info *handle = h;
1316
1317         if (handle->category) {
1318                 free(handle->category);
1319                 handle->category = NULL;
1320         }
1321
1322         if (!category) {
1323                 return LB_STATUS_SUCCESS;
1324         }
1325
1326         handle->category = strdup(category);
1327         if (!handle->category) {
1328                 ErrPrint("Error: %s\n", strerror(errno));
1329                 return LB_STATUS_ERROR_MEMORY;
1330         }
1331
1332         return LB_STATUS_SUCCESS;
1333 }
1334
1335 PUBLIC void *script_create(const char *file, const char *group)
1336 {
1337         struct info *handle;
1338
1339         handle = calloc(1, sizeof(*handle));
1340         if (!handle) {
1341                 ErrPrint("Error: %s\n", strerror(errno));
1342                 return NULL;
1343         }
1344
1345         handle->file = strdup(file);
1346         if (!handle->file) {
1347                 ErrPrint("Error: %s\n", strerror(errno));
1348                 free(handle);
1349                 return NULL;
1350         }
1351
1352         handle->group = strdup(group);
1353         if (!handle->group) {
1354                 ErrPrint("Error: %s\n", strerror(errno));
1355                 free(handle->file);
1356                 free(handle);
1357                 return NULL;
1358         }
1359
1360         s_info.handle_list = eina_list_append(s_info.handle_list, handle);
1361
1362         return handle;
1363 }
1364
1365 PUBLIC int script_destroy(void *_handle)
1366 {
1367         struct info *handle;
1368         Evas_Object *edje;
1369
1370         handle = _handle;
1371
1372         if (!eina_list_data_find(s_info.handle_list, handle)) {
1373                 DbgPrint("Not found (already deleted?)\n");
1374                 return LB_STATUS_ERROR_NOT_EXIST;
1375         }
1376
1377         s_info.handle_list = eina_list_remove(s_info.handle_list, handle);
1378
1379         edje = eina_list_nth(handle->obj_list, 0);
1380         if (edje) {
1381                 evas_object_del(edje);
1382         }
1383
1384         free(handle->category);
1385         free(handle->file);
1386         free(handle->group);
1387         free(handle);
1388         return LB_STATUS_SUCCESS;
1389 }
1390
1391 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
1392 {
1393         struct info *handle;
1394         Evas_Object *edje;
1395         struct obj_info *obj_info;
1396
1397         handle = _handle;
1398
1399         obj_info = calloc(1, sizeof(*obj_info));
1400         if (!obj_info) {
1401                 ErrPrint("Heap: %s\n", strerror(errno));
1402                 return LB_STATUS_ERROR_MEMORY;
1403         }
1404
1405         obj_info->parent = evas_object_rectangle_add(e);
1406         if (!obj_info->parent) {
1407                 ErrPrint("Unable to create a parent box\n");
1408                 free(obj_info);
1409                 return LB_STATUS_ERROR_FAULT;
1410         }
1411
1412         edje = elm_layout_add(obj_info->parent);
1413         if (!edje) {
1414                 ErrPrint("Failed to create an edje object\n");
1415                 evas_object_del(obj_info->parent);
1416                 free(obj_info);
1417                 return LB_STATUS_ERROR_FAULT;
1418         }
1419
1420         edje_object_scale_set(elm_layout_edje_get(edje), elm_config_scale_get());
1421
1422         if (!elm_layout_file_set(edje, handle->file, handle->group)) {
1423                 int err;
1424
1425                 err = edje_object_load_error_get(elm_layout_edje_get(edje));
1426                 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, edje_load_error_str(err));
1427                 evas_object_del(edje);
1428                 evas_object_del(obj_info->parent);
1429                 free(obj_info);
1430                 return LB_STATUS_ERROR_IO;
1431         }
1432
1433         handle->e = e;
1434         handle->w = w;
1435         handle->h = h;
1436
1437         elm_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
1438         evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
1439         evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1440         evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
1441         evas_object_resize(edje, handle->w, handle->h);
1442         evas_object_show(edje);
1443         evas_object_data_set(edje, "obj_info", obj_info);
1444
1445         handle->obj_list = eina_list_append(handle->obj_list, edje);
1446         return LB_STATUS_SUCCESS;
1447 }
1448
1449 PUBLIC int script_unload(void *_handle, Evas *e)
1450 {
1451         struct info *handle;
1452         Evas_Object *edje;
1453         Evas_Object *parent = NULL;
1454
1455         handle = _handle;
1456
1457         edje = eina_list_nth(handle->obj_list, 0);
1458         if (edje) {
1459                 struct obj_info *obj_info;
1460
1461                 obj_info = evas_object_data_get(edje, "obj_info");
1462                 if (obj_info) {
1463                         parent = obj_info->parent;
1464                 }
1465                 evas_object_del(edje);
1466         }
1467
1468         if (parent) {
1469                 DbgPrint("Delete parent box\n");
1470                 evas_object_del(parent);
1471         }
1472
1473         handle->e = NULL;
1474         return LB_STATUS_SUCCESS;
1475 }
1476
1477 static void access_cb(keynode_t *node, void *user_data)
1478 {
1479         int state;
1480
1481         if (!node) {
1482                 if (vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &state) != 0) {
1483                         ErrPrint("Idle lock state is not valid\n");
1484                         state = 0; /* DISABLED */
1485                 }
1486         } else {
1487                 state = vconf_keynode_get_bool(node);
1488         }
1489
1490         DbgPrint("ELM CONFIG ACCESS: %d\n", state);
1491         elm_config_access_set(state);
1492 }
1493
1494 static void update_font_cb(void *data)
1495 {
1496         elm_config_font_overlay_set(TEXT_CLASS, s_info.font_name, DEFAULT_FONT_SIZE);
1497         DbgPrint("Update text class %s (%s, %d)\n", TEXT_CLASS, s_info.font_name, DEFAULT_FONT_SIZE);
1498 }
1499
1500 static void font_changed_cb(keynode_t *node, void *user_data)
1501 {
1502         char *font_name;
1503
1504         if (s_info.font_name) {
1505                 font_name = vconf_get_str("db/setting/accessibility/font_name");
1506                 if (!font_name) {
1507                         ErrPrint("Invalid font name (NULL)\n");
1508                         return;
1509                 }
1510
1511                 if (!strcmp(s_info.font_name, font_name)) {
1512                         DbgPrint("Font is not changed (Old: %s(%p) <> New: %s(%p))\n", s_info.font_name, s_info.font_name, font_name, font_name);
1513                         free(font_name);
1514                         return;
1515                 }
1516
1517                 DbgPrint("Release old font name: %s(%p)\n", s_info.font_name, s_info.font_name);
1518                 free(s_info.font_name);
1519         } else {
1520                 int ret;
1521
1522                 /*!
1523                  * Get the first font name using system_settings API.
1524                  */
1525                 font_name = NULL;
1526                 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_FONT_TYPE, &font_name);
1527                 if (ret != SYSTEM_SETTINGS_ERROR_NONE || !font_name) {
1528                         ErrPrint("System setting get: %d, font_name[%p]\n", ret, font_name);
1529                         return;
1530                 }
1531         }
1532
1533         s_info.font_name = font_name;
1534         DbgPrint("Font name is changed to %s(%p)\n", s_info.font_name, s_info.font_name);
1535
1536         /*!
1537          * \NOTE
1538          * Try to update all liveboxes
1539          */
1540         update_font_cb(NULL);
1541 }
1542
1543 static inline int convert_font_size(int size)
1544 {
1545         switch (size) {
1546         case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
1547                 size = -80;
1548                 break;
1549         case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
1550                 size = -100;
1551                 break;
1552         case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
1553                 size = -150;
1554                 break;
1555         case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
1556                 size = -190;
1557                 break;
1558         case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
1559                 size = -250;
1560                 break;
1561         default:
1562                 size = -100;
1563                 break;
1564         }
1565
1566         DbgPrint("Return size: %d\n", size);
1567         return size;
1568 }
1569
1570 static void font_size_cb(system_settings_key_e key, void *user_data)
1571 {
1572         int size;
1573
1574         if (system_settings_get_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, &size) != SYSTEM_SETTINGS_ERROR_NONE) {
1575                 return;
1576         }
1577
1578         size = convert_font_size(size);
1579
1580         if (size == s_info.font_size) {
1581                 DbgPrint("Font size is not changed\n");
1582                 return;
1583         }
1584
1585         s_info.font_size = size;
1586         DbgPrint("Font size is changed to %d, but don't update the font info\n", size);
1587 }
1588
1589 PUBLIC int script_init(double scale)
1590 {
1591         int ret;
1592         char *argv[] = {
1593                 "livebox.edje",
1594                 NULL,
1595         };
1596
1597         /* ecore is already initialized */
1598         elm_init(1, argv);
1599         elm_config_scale_set(scale);
1600         DbgPrint("Scale is updated: %lf\n", scale);
1601
1602         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, access_cb, NULL);
1603         DbgPrint("TTS changed: %d\n", ret);
1604
1605         ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_changed_cb, NULL);
1606         DbgPrint("System font is changed: %d\n", ret);
1607         
1608         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE, font_size_cb, NULL);
1609         DbgPrint("System font size is changed: %d\n", ret);
1610
1611         access_cb(NULL, NULL);
1612         font_changed_cb(NULL, NULL);
1613         font_size_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE, NULL);
1614         return LB_STATUS_SUCCESS;
1615 }
1616
1617 PUBLIC int script_fini(void)
1618 {
1619         int ret;
1620         Eina_List *l;
1621         Eina_List *n;
1622         struct info *handle;
1623
1624         EINA_LIST_FOREACH_SAFE(s_info.handle_list, l, n, handle) {
1625                 script_destroy(handle);
1626         }
1627
1628         ret = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE);
1629         DbgPrint("Unset font size change event callback: %d\n", ret);
1630
1631         ret = vconf_ignore_key_changed("db/setting/accessibility/font_name", font_changed_cb);
1632         DbgPrint("Unset font name change event callback: %d\n", ret);
1633
1634         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, access_cb);
1635         DbgPrint("Unset tts: %d\n", ret);
1636
1637         elm_shutdown();
1638
1639         free(s_info.font_name);
1640         s_info.font_name = NULL;
1641         return LB_STATUS_SUCCESS;
1642 }
1643
1644 /* End of a file */