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