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