da52f0fdfe22c408eca22670c6fb3525cb4f7ec8
[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 *win;
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                         DbgPrint("Size: %dx%d\n", w, h);
686
687                         evas_object_image_load_size_set(img, w, h);
688                         evas_object_image_load_region_set(img, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h);
689                         evas_object_image_fill_set(img, 0, 0, part_w, part_h);
690                         evas_object_image_reload(img);
691                 } else if (img_opt.fill == FILL_IN_SIZE) {
692                         Evas_Coord part_w;
693                         Evas_Coord part_h;
694
695                         if (img_opt.width >= 0 && img_opt.height >= 0) {
696                                 part_w = img_opt.width * scale_get();
697                                 part_h = img_opt.height * scale_get();
698                         } else {
699                                 part_w = 0;
700                                 part_h = 0;
701                                 edje_object_part_geometry_get(elm_layout_edje_get(edje), part, NULL, NULL, &part_w, &part_h);
702                         }
703                         DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
704
705                         if (part_w > w || part_h > h) {
706                                 double fw;
707                                 double fh;
708
709                                 fw = (double)part_w / (double)w;
710                                 fh = (double)part_h / (double)h;
711
712                                 if (fw > fh) {
713                                         w = part_w;
714                                         h = (double)h * fw;
715                                 } else {
716                                         h = part_h;
717                                         w = (double)w * fh;
718                                 }
719                         }
720                         DbgPrint("Size: %dx%d\n", w, h);
721                         evas_object_image_fill_set(img, 0, 0, part_w, part_h);
722                         evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
723                         evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
724                 } else {
725                         evas_object_image_fill_set(img, 0, 0, w, h);
726                         evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
727                         evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
728                 }
729         } else {
730                 if (img_opt.width >= 0 && img_opt.height >= 0) {
731                         w = img_opt.width;
732                         h = img_opt.height;
733                         DbgPrint("Using given image size: %dx%d\n", w, h);
734                 }
735
736                 evas_object_image_fill_set(img, 0, 0, w, h);
737                 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
738                 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
739         }
740
741         /*!
742          * \note
743          * object will be shown by below statement automatically
744          */
745         DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
746         child->obj = img;
747         elm_object_part_content_set(edje, part, img);
748         obj_info->children = eina_list_append(obj_info->children, child);
749
750         /*!
751          * \note
752          * This object is not registered as an access object.
753          * So the developer should add it to access list manually, using DESC_ACCESS block.
754          */
755         return LB_STATUS_SUCCESS;
756 }
757
758 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
759 {
760         struct info *handle = data;
761         Evas_Coord w;
762         Evas_Coord h;
763         Evas_Coord px = 0;
764         Evas_Coord py = 0;
765         Evas_Coord pw = 0;
766         Evas_Coord ph = 0;
767         double sx;
768         double sy;
769         double ex;
770         double ey;
771
772         evas_object_geometry_get(obj, NULL, NULL, &w, &h);
773         edje_object_part_geometry_get(elm_layout_edje_get(obj), source, &px, &py, &pw, &ph);
774
775         sx = ex = 0.0f;
776         if (w) {
777                 sx = (double)px / (double)w;
778                 ex = (double)(px + pw) / (double)w;
779         }
780
781         sy = ey = 0.0f;
782         if (h) {
783                 sy = (double)py / (double)h;
784                 ey = (double)(py + ph) / (double)h;
785         }
786
787         DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
788         script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
789 }
790
791 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
792 {
793         struct info *handle = _info;
794         struct obj_info *obj_info;
795         struct child *child;
796         Evas_Object *ao;
797
798         handle->obj_list = eina_list_remove(handle->obj_list, obj);
799
800         obj_info = evas_object_data_del(obj, "obj_info");
801         if (!obj_info) {
802                 ErrPrint("Object info is not valid\n");
803                 return;
804         }
805
806         DbgPrint("delete object %s %p\n", obj_info->id, obj);
807
808         elm_object_signal_callback_del(obj, "*", "*", script_signal_cb);
809
810         elm_object_focus_custom_chain_unset(obj);
811
812         EINA_LIST_FREE(obj_info->children, child) {
813                 DbgPrint("delete object %s %p\n", child->part, child->obj);
814                 if (child->obj) {
815                         Evas_Object *ao;
816                         ao = evas_object_data_del(child->obj, "ao");
817                         if (ao) {
818                                 obj_info->access_chain = eina_list_remove(obj_info->access_chain, ao);
819                                 evas_object_data_del(ao, "edje");
820                                 elm_access_object_unregister(ao);
821                         }
822                         evas_object_del(child->obj);
823                 }
824                 free(child->part);
825                 free(child);
826         }
827
828         EINA_LIST_FREE(obj_info->access_chain, ao) {
829                 evas_object_data_del(ao, "edje");
830                 elm_access_object_unregister(ao);
831         }
832
833         free(obj_info->id);
834         free(obj_info);
835 }
836
837 /*!
838         LB_ACCESS_HIGHLIGHT             0
839         LB_ACCESS_HIGHLIGHT_NEXT        1
840         LB_ACCESS_HIGHLIGHT_PREV        2
841         LB_ACCESS_ACTIVATE              3
842         LB_ACCESS_ACTION                4
843         LB_ACCESS_SCROLL                5
844 */
845 PUBLIC int script_feed_event(void *h, Evas *e, int event_type, int x, int y, int down, double timestamp)
846 {
847         struct info *handle = h;
848         Evas_Object *edje;
849         struct obj_info *obj_info;
850         int ret = LB_STATUS_SUCCESS;
851
852         DbgPrint("event: %d, x: %d, y: %d\n", event_type, x, y);
853
854         edje = find_edje(handle, NULL); /*!< Get the base layout */
855         if (!edje) {
856                 ErrPrint("Base layout is not exist\n");
857                 return LB_STATUS_ERROR_NOT_EXIST;
858         }
859
860         obj_info = evas_object_data_get(edje, "obj_info");
861         if (!obj_info) {
862                 ErrPrint("Object info is not valid\n");
863                 return LB_STATUS_ERROR_INVALID;
864         }
865
866         if (event_type & LB_SCRIPT_ACCESS_EVENT) {
867                 Elm_Access_Action_Info *info;
868                 Elm_Access_Action_Type action;
869                 const Eina_List *chain;
870
871                 info = calloc(1, sizeof(*info));
872                 if (!info) {
873                         ErrPrint("Error: %s\n", strerror(errno));
874                         return LB_STATUS_ERROR_MEMORY;
875                 }
876
877                 chain = elm_object_focus_custom_chain_get(edje);
878                 DbgPrint("Focus chain : %d\n", eina_list_count(chain));
879
880                 if ((event_type & LB_SCRIPT_ACCESS_HIGHLIGHT) == LB_SCRIPT_ACCESS_HIGHLIGHT) {
881                         action = ELM_ACCESS_ACTION_HIGHLIGHT;
882                         info->x = x;
883                         info->y = y;
884                         ret = elm_access_action(edje, action, info);
885                         DbgPrint("ACCESS_HIGHLIGHT: %dx%d returns %d\n", x, y, ret);
886                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
887                 } else if ((event_type & LB_SCRIPT_ACCESS_HIGHLIGHT_NEXT) == LB_SCRIPT_ACCESS_HIGHLIGHT_NEXT) {
888                         action = ELM_ACCESS_ACTION_HIGHLIGHT_NEXT;
889                         info->highlight_cycle = EINA_FALSE;
890                         ret = elm_access_action(edje, action, info);
891                         DbgPrint("ACCESS_HIGHLIGHT_NEXT, returns %d\n", ret);
892                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_LAST : LB_ACCESS_STATUS_DONE;
893                 } else if ((event_type & LB_SCRIPT_ACCESS_HIGHLIGHT_PREV) == LB_SCRIPT_ACCESS_HIGHLIGHT_PREV) {
894                         action = ELM_ACCESS_ACTION_HIGHLIGHT_PREV;
895                         info->highlight_cycle = EINA_FALSE;
896                         ret = elm_access_action(edje, action, info);
897                         DbgPrint("ACCESS_HIGHLIGHT_PREV, returns %d\n", ret);
898                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_FIRST : LB_ACCESS_STATUS_DONE;
899                 } else if ((event_type & LB_SCRIPT_ACCESS_ACTIVATE) == LB_SCRIPT_ACCESS_ACTIVATE) {
900                         action = ELM_ACCESS_ACTION_ACTIVATE;
901                         ret = elm_access_action(edje, action, info);
902                         DbgPrint("ACCESS_ACTIVATE, returns %d\n", ret);
903                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
904                 } else if ((event_type & LB_SCRIPT_ACCESS_ACTION) == LB_SCRIPT_ACCESS_ACTION) {
905                         if (down == 0) {
906                                 action = ELM_ACCESS_ACTION_UP;
907                                 ret = elm_access_action(edje, action, info);
908                                 DbgPrint("ACCESS_ACTION(%d), returns %d\n", down, ret);
909                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
910                         } else if (down == 1) {
911                                 action = ELM_ACCESS_ACTION_DOWN;
912                                 ret = elm_access_action(edje, action, info);
913                                 DbgPrint("ACCESS_ACTION(%d), returns %d\n", down, ret);
914                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
915                         } else {
916                                 ErrPrint("Invalid access event\n");
917                                 ret = LB_ACCESS_STATUS_ERROR;
918                         }
919                 } else if ((event_type & LB_SCRIPT_ACCESS_SCROLL) == LB_SCRIPT_ACCESS_SCROLL) {
920                         action = ELM_ACCESS_ACTION_SCROLL;
921                         info->x = x;
922                         info->y = y;
923                         switch (down) {
924                         case 0:
925                                 info->mouse_type = 0;
926                                 ret = elm_access_action(edje, action, info);
927                                 DbgPrint("ACCESS_HIGHLIGHT_SCROLL, returns %d\n", ret);
928                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
929                                 break;
930                         case -1:
931                                 info->mouse_type = 1;
932                                 ret = elm_access_action(edje, action, info);
933                                 DbgPrint("ACCESS_HIGHLIGHT_SCROLL, returns %d\n", ret);
934                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
935                                 break;
936                         case 1:
937                                 info->mouse_type = 2;
938                                 ret = elm_access_action(edje, action, info);
939                                 DbgPrint("ACCESS_HIGHLIGHT_SCROLL, returns %d\n", ret);
940                                 ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
941                                 break;
942                         default:
943                                 ret = LB_ACCESS_STATUS_ERROR;
944                                 break;
945                         }
946                 } else if ((event_type & LB_SCRIPT_ACCESS_UNHIGHLIGHT) == LB_SCRIPT_ACCESS_UNHIGHLIGHT) {
947                         action = ELM_ACCESS_ACTION_UNHIGHLIGHT;
948                         ret = elm_access_action(edje, action, info);
949                         DbgPrint("ACCESS_UNHIGHLIGHT, returns %d\n", ret);
950                         ret = (ret == EINA_FALSE) ? LB_ACCESS_STATUS_ERROR : LB_ACCESS_STATUS_DONE;
951                 } else {
952                         DbgPrint("Invalid event\n");
953                         ret = LB_ACCESS_STATUS_ERROR;
954                 }
955
956                 free(info);
957         } else if (event_type & LB_SCRIPT_MOUSE_EVENT) {
958                 switch (event_type) {
959                 case LB_SCRIPT_MOUSE_DOWN:
960                         evas_event_feed_mouse_move(e, x, y, timestamp, NULL);
961                         evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, timestamp + 0.01f, NULL);
962                         break;
963                 case LB_SCRIPT_MOUSE_MOVE:
964                         evas_event_feed_mouse_move(e, x, y, timestamp, NULL);
965                         break;
966                 case LB_SCRIPT_MOUSE_UP:
967                         evas_event_feed_mouse_move(e, x, y, timestamp, NULL);
968                         evas_event_feed_mouse_up(e, 1, EVAS_BUTTON_NONE, timestamp + 0.1f, NULL);
969                         break;
970                 case LB_SCRIPT_MOUSE_IN:
971                         evas_event_feed_mouse_in(e, timestamp, NULL);
972                         break;
973                 case LB_SCRIPT_MOUSE_OUT:
974                         evas_event_feed_mouse_out(e, timestamp, NULL);
975                         break;
976                 default:
977                         return LB_STATUS_ERROR_INVALID;
978                 }
979         } else if (event_type & LB_SCRIPT_KEY_EVENT) {
980                 DbgPrint("Key event is not implemented\n");
981                 return LB_STATUS_ERROR_NOT_IMPLEMENTED;
982         }
983
984         return ret;
985 }
986
987 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)
988 {
989         struct info *handle = h;
990         Evas_Object *edje;
991         Evas_Object *obj;
992         struct obj_info *obj_info;
993         struct child *child;
994
995         DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
996
997         edje = find_edje(handle, src_id);
998         if (!edje) {
999                 ErrPrint("Edje is not exists\n");
1000                 return LB_STATUS_ERROR_NOT_EXIST;
1001         }
1002
1003         obj_info = evas_object_data_get(edje, "obj_info");
1004         if (!obj_info) {
1005                 ErrPrint("Object info is not valid\n");
1006                 return LB_STATUS_ERROR_INVALID;
1007         }
1008
1009         obj = elm_object_part_content_unset(edje, part);
1010         if (obj) {
1011                 Eina_List *l;
1012                 Eina_List *n;
1013
1014                 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
1015                         if (child->obj != obj)
1016                                 continue;
1017
1018                         obj_info->children = eina_list_remove(obj_info->children, child);
1019
1020                         free(child->part);
1021                         free(child);
1022                         break;
1023                 }
1024
1025                 DbgPrint("delete object %s %p\n", part, obj);
1026                 /*!
1027                  * \note
1028                  * This will call the edje_del_cb.
1029                  * It will delete all access objects
1030                  */
1031                 evas_object_del(obj);
1032         }
1033
1034         if (!path || !strlen(path) || access(path, R_OK) != 0) {
1035                 DbgPrint("SKIP - Path: [%s]\n", path);
1036                 return LB_STATUS_SUCCESS;
1037         }
1038
1039         obj = elm_layout_add(edje);
1040         if (!obj) {
1041                 ErrPrint("Failed to add a new edje object\n");
1042                 return LB_STATUS_ERROR_FAULT;
1043         }
1044
1045         if (!elm_layout_file_set(obj, path, group)) {
1046                 int err;
1047                 const char *errmsg;
1048
1049                 err = edje_object_load_error_get(elm_layout_edje_get(obj));
1050                 errmsg = edje_load_error_str(err);
1051                 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
1052                 evas_object_del(obj);
1053                 return LB_STATUS_ERROR_IO;
1054         }
1055
1056         evas_object_show(obj);
1057
1058         obj_info = calloc(1, sizeof(*obj_info));
1059         if (!obj_info) {
1060                 ErrPrint("Failed to add a obj_info\n");
1061                 evas_object_del(obj);
1062                 return LB_STATUS_ERROR_MEMORY;
1063         }
1064
1065         obj_info->id = strdup(target_id);
1066         if (!obj_info->id) {
1067                 ErrPrint("Failed to add a obj_info\n");
1068                 free(obj_info);
1069                 evas_object_del(obj);
1070                 return LB_STATUS_ERROR_MEMORY;
1071         }
1072
1073         child = malloc(sizeof(*child));
1074         if (!child) {
1075                 ErrPrint("Error: %s\n", strerror(errno));
1076                 free(obj_info->id);
1077                 free(obj_info);
1078                 evas_object_del(obj);
1079                 return LB_STATUS_ERROR_MEMORY;
1080         }
1081
1082         child->part = strdup(part);
1083         if (!child->part) {
1084                 ErrPrint("Error: %s\n", strerror(errno));
1085                 free(child);
1086                 free(obj_info->id);
1087                 free(obj_info);
1088                 evas_object_del(obj);
1089                 return LB_STATUS_ERROR_MEMORY;
1090         }
1091
1092         child->obj = obj;
1093
1094         evas_object_data_set(obj, "obj_info", obj_info);
1095         evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
1096         elm_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
1097         handle->obj_list = eina_list_append(handle->obj_list, obj);
1098
1099         DbgPrint("%s part swallow edje %p\n", part, obj);
1100         elm_object_part_content_set(edje, part, obj);
1101         obj_info = evas_object_data_get(edje, "obj_info");
1102         obj_info->children = eina_list_append(obj_info->children, child);
1103         return LB_STATUS_SUCCESS;
1104 }
1105
1106 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
1107 {
1108         struct info *handle = h;
1109         Evas_Object *edje;
1110
1111         DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
1112
1113         edje = find_edje(handle, id);
1114         if (!edje)
1115                 return LB_STATUS_ERROR_NOT_EXIST;
1116
1117         elm_object_signal_emit(edje, signal, part);
1118         return LB_STATUS_SUCCESS;
1119 }
1120
1121 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
1122 {
1123         struct info *handle = h;
1124         Evas_Object *edje;
1125
1126         DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
1127
1128         edje = find_edje(handle, id);
1129         if (!edje)
1130                 return LB_STATUS_ERROR_NOT_EXIST;
1131
1132         edje_object_part_drag_value_set(elm_layout_edje_get(edje), part, x, y);
1133         return LB_STATUS_SUCCESS;
1134 }
1135
1136 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
1137 {
1138         struct info *handle = han;
1139         Evas_Object *edje;
1140
1141         edje = find_edje(handle, id);
1142         if (!edje)
1143                 return LB_STATUS_ERROR_NOT_EXIST;
1144
1145         if (!id) {
1146                 handle->w = w;
1147                 handle->h = h;
1148         }
1149
1150         DbgPrint("Resize object to %dx%d\n", w, h);
1151         evas_object_resize(edje, w, h);
1152         return LB_STATUS_SUCCESS;
1153 }
1154
1155 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
1156 {
1157         struct info *handle = h;
1158
1159         DbgPrint("id[%s], category[%s]\n", id, category);
1160
1161         if (handle->category) {
1162                 free(handle->category);
1163                 handle->category = NULL;
1164         }
1165
1166         if (!category)
1167                 return LB_STATUS_SUCCESS;
1168
1169         handle->category = strdup(category);
1170         if (!handle->category) {
1171                 ErrPrint("Error: %s\n", strerror(errno));
1172                 return LB_STATUS_ERROR_MEMORY;
1173         }
1174
1175         return LB_STATUS_SUCCESS;
1176 }
1177
1178 PUBLIC void *script_create(const char *file, const char *group)
1179 {
1180         struct info *handle;
1181
1182         DbgPrint("file[%s], group[%s]\n", file, group);
1183
1184         handle = calloc(1, sizeof(*handle));
1185         if (!handle) {
1186                 ErrPrint("Error: %s\n", strerror(errno));
1187                 return NULL;
1188         }
1189
1190         handle->file = strdup(file);
1191         if (!handle->file) {
1192                 ErrPrint("Error: %s\n", strerror(errno));
1193                 free(handle);
1194                 return NULL;
1195         }
1196
1197         handle->group = strdup(group);
1198         if (!handle->group) {
1199                 ErrPrint("Error: %s\n", strerror(errno));
1200                 free(handle->file);
1201                 free(handle);
1202                 return NULL;
1203         }
1204
1205         return handle;
1206 }
1207
1208 PUBLIC int script_destroy(void *_handle)
1209 {
1210         struct info *handle;
1211         Evas_Object *edje;
1212
1213         handle = _handle;
1214
1215         edje = eina_list_nth(handle->obj_list, 0);
1216         if (edje)
1217                 evas_object_del(edje);
1218
1219         free(handle->category);
1220         free(handle->file);
1221         free(handle->group);
1222         free(handle);
1223         return LB_STATUS_SUCCESS;
1224 }
1225
1226 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
1227 {
1228         struct info *handle;
1229         Evas_Object *edje;
1230         struct obj_info *obj_info;
1231
1232         handle = _handle;
1233
1234         obj_info = calloc(1, sizeof(*obj_info));
1235         if (!obj_info) {
1236                 ErrPrint("Heap: %s\n", strerror(errno));
1237                 return LB_STATUS_ERROR_MEMORY;
1238         }
1239
1240         obj_info->win = evas_object_rectangle_add(e);
1241         if (!obj_info->win) {
1242                 free(obj_info);
1243                 return LB_STATUS_ERROR_FAULT;
1244         }
1245
1246         edje = elm_layout_add(obj_info->win);
1247         if (!edje) {
1248                 ErrPrint("Failed to create an edje object\n");
1249                 evas_object_del(obj_info->win);
1250                 free(obj_info);
1251                 return LB_STATUS_ERROR_FAULT;
1252         }
1253
1254         DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
1255         if (!elm_layout_file_set(edje, handle->file, handle->group)) {
1256                 int err;
1257                 const char *errmsg;
1258
1259                 err = edje_object_load_error_get(elm_layout_edje_get(edje));
1260                 errmsg = edje_load_error_str(err);
1261                 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
1262                 evas_object_del(edje);
1263                 evas_object_del(obj_info->win);
1264                 free(obj_info);
1265                 return LB_STATUS_ERROR_IO;
1266         }
1267
1268         handle->e = e;
1269         handle->w = w;
1270         handle->h = h;
1271
1272         elm_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
1273         evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
1274         evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1275         evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
1276         evas_object_resize(edje, handle->w, handle->h);
1277         evas_object_show(edje);
1278         evas_object_data_set(edje, "obj_info", obj_info);
1279
1280         handle->obj_list = eina_list_append(handle->obj_list, edje);
1281         return LB_STATUS_SUCCESS;
1282 }
1283
1284 PUBLIC int script_unload(void *_handle, Evas *e)
1285 {
1286         struct info *handle;
1287         Evas_Object *edje;
1288
1289         handle = _handle;
1290
1291         DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
1292         edje = eina_list_nth(handle->obj_list, 0);
1293         if (edje)
1294                 evas_object_del(edje);
1295         handle->e = NULL;
1296         return LB_STATUS_SUCCESS;
1297 }
1298
1299 static void access_cb(keynode_t *node, void *user_data)
1300 {
1301         int state;
1302
1303         if (!node) {
1304                 if (vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &state) != 0) {
1305                         ErrPrint("Idle lock state is not valid\n");
1306                         state = 0; /* DISABLED */
1307                 }
1308         } else {
1309                 state = vconf_keynode_get_bool(node);
1310         }
1311
1312         DbgPrint("ELM CONFIG ACCESS: %d\n", state);
1313         elm_config_access_set(state);
1314 }
1315
1316 static void update_font_cb(void *data)
1317 {
1318         elm_config_font_overlay_set(TEXT_CLASS, s_info.font_name, DEFAULT_FONT_SIZE);
1319         DbgPrint("Update text class %s (%s, %d)\n", TEXT_CLASS, s_info.font_name, DEFAULT_FONT_SIZE);
1320 }
1321
1322 static void font_changed_cb(keynode_t *node, void *user_data)
1323 {
1324         char *font_name;
1325
1326         font_name = vconf_get_str("db/setting/accessibility/font_name");
1327         if (!font_name) {
1328                 ErrPrint("Invalid font name (NULL)\n");
1329                 return;
1330         }
1331
1332         if (s_info.font_name && !strcmp(s_info.font_name, font_name)) {
1333                 DbgPrint("Font is not changed (Old: %s(%p) <> New: %s(%p))\n", s_info.font_name, s_info.font_name, font_name, font_name);
1334                 free(font_name);
1335                 return;
1336         }
1337
1338         if (s_info.font_name) {
1339                 DbgPrint("Release old font name: %s(%p)\n", s_info.font_name, s_info.font_name);
1340                 free(s_info.font_name);
1341                 s_info.font_name = NULL;
1342         }
1343
1344         s_info.font_name = font_name;
1345         DbgPrint("Font name is changed to %s(%p)\n", s_info.font_name, s_info.font_name);
1346
1347         /*!
1348          * \NOTE
1349          * Try to update all liveboxes
1350          */
1351         update_font_cb(NULL);
1352 }
1353
1354 static inline int convert_font_size(int size)
1355 {
1356         switch (size) {
1357         case SYSTEM_SETTINGS_FONT_SIZE_SMALL:
1358                 size = -80;
1359                 break;
1360         case SYSTEM_SETTINGS_FONT_SIZE_NORMAL:
1361                 size = -100;
1362                 break;
1363         case SYSTEM_SETTINGS_FONT_SIZE_LARGE:
1364                 size = -150;
1365                 break;
1366         case SYSTEM_SETTINGS_FONT_SIZE_HUGE:
1367                 size = -190;
1368                 break;
1369         case SYSTEM_SETTINGS_FONT_SIZE_GIANT:
1370                 size = -250;
1371                 break;
1372         default:
1373                 size = -100;
1374                 break;
1375         }
1376
1377         DbgPrint("Return size: %d\n", size);
1378         return size;
1379 }
1380
1381 static void font_size_cb(system_settings_key_e key, void *user_data)
1382 {
1383         int size;
1384
1385         if (system_settings_get_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, &size) != SYSTEM_SETTINGS_ERROR_NONE)
1386                 return;
1387
1388         size = convert_font_size(size);
1389
1390         if (size == s_info.font_size) {
1391                 DbgPrint("Font size is not changed\n");
1392                 return;
1393         }
1394
1395         s_info.font_size = size;
1396         DbgPrint("Font size is changed to %d\n", size);
1397
1398         update_font_cb(NULL);
1399 }
1400
1401 PUBLIC int script_init(void)
1402 {
1403         int ret;
1404         int size = DEFAULT_FONT_SIZE;
1405         char *argv[] = {
1406                 "livebox.edje",
1407                 NULL,
1408         };
1409         /* ecore is already initialized */
1410         elm_init(1, argv);
1411         elm_config_scale_set(scale_get());
1412
1413         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, access_cb, NULL);
1414         if (ret < 0)
1415                 ErrPrint("Failed to access cb\n");
1416
1417         access_cb(NULL, NULL);
1418
1419         ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_changed_cb, NULL);
1420         DbgPrint("System font is changed: %d\n", ret);
1421         
1422         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE, font_size_cb, NULL);
1423         DbgPrint("System font size is changed: %d\n", ret);
1424
1425         s_info.font_name = vconf_get_str("db/setting/accessibility/font_name");
1426         DbgPrint("Current font: %s\n", s_info.font_name);
1427
1428         ret = system_settings_get_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, &size);
1429         s_info.font_size = convert_font_size(size);
1430         DbgPrint("Current size: %d\n", s_info.font_size);
1431
1432         return LB_STATUS_SUCCESS;
1433 }
1434
1435 PUBLIC int script_fini(void)
1436 {
1437         int ret;
1438         ret = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_FONT_SIZE);
1439         ret = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_FONT_TYPE);
1440         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, access_cb);
1441         elm_shutdown();
1442         return LB_STATUS_SUCCESS;
1443 }
1444
1445 /* End of a file */