Fix the EDJE scale factor problem.
[apps/livebox/livebox-edje.git] / src / script_port.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 <Evas.h>
24 #include <Edje.h>
25 #include <Eina.h>
26 #include <Ecore.h>
27 #include <Ecore_Evas.h>
28 #include <Eet.h>
29 #include <Ecore_X.h>
30
31 #include <dlog.h>
32 #include <debug.h>
33 #include <vconf.h>
34
35 #include "script_port.h"
36
37 #define TEXT_CLASS      "tizen"
38 #define BASE_WIDTH      720.0f
39
40 #define PUBLIC __attribute__((visibility("default")))
41
42 extern void evas_common_font_flush(void);
43 extern int evas_common_font_cache_get(void);
44 extern void evas_common_font_cache_set(int size);
45
46 struct image_option {
47         int orient;
48         int aspect;
49         enum {
50                 FILL_DISABLE,
51                 FILL_IN_SIZE,
52                 FILL_OVER_SIZE,
53         } fill;
54
55         int width;
56         int height;
57 };
58
59 struct info {
60         char *file;
61         char *group;
62         char *category;
63         int w;
64         int h;
65
66         Evas *e;
67
68         Eina_List *obj_list;
69 };
70
71 struct child {
72         Evas_Object *obj;
73         char *part;
74 };
75
76 struct obj_info {
77         char *id;
78         Eina_List *children;
79 };
80
81 static struct {
82         Ecore_Event_Handler *property_handler;
83         char *font;
84         int size;
85 } s_info = {
86         .property_handler = NULL,
87         .font = NULL,
88         .size = -100,
89 };
90
91 static inline double scale_get(void)
92 {
93         int width;
94         int height;
95         ecore_x_window_size_get(0, &width, &height);
96         return (double)width / BASE_WIDTH;
97 }
98
99 /*!
100  * \NOTE
101  * Reservce this for future use
102 static inline void common_cache_flush(void *evas)
103 {
104         int file_cache;
105         int collection_cache;
106         int image_cache;
107         int font_cache;
108
109         file_cache = edje_file_cache_get();
110         collection_cache = edje_collection_cache_get();
111         image_cache = evas_image_cache_get(evas);
112         font_cache = evas_font_cache_get(evas);
113
114         edje_file_cache_set(file_cache);
115         edje_collection_cache_set(collection_cache);
116         evas_image_cache_set(evas, 0);
117         evas_font_cache_set(evas, 0);
118
119         evas_image_cache_flush(evas);
120         evas_render_idle_flush(evas);
121         evas_font_cache_flush(evas);
122
123         edje_file_cache_flush();
124         edje_collection_cache_flush();
125
126         edje_file_cache_set(file_cache);
127         edje_collection_cache_set(collection_cache);
128         evas_image_cache_set(evas, image_cache);
129         evas_font_cache_set(evas, font_cache);
130
131         eet_clearcache();
132 }
133  */
134
135 static inline Evas_Object *find_edje(struct info *handle, const char *id)
136 {
137         Eina_List *l;
138         Evas_Object *edje;
139         struct obj_info *obj_info;
140
141         EINA_LIST_FOREACH(handle->obj_list, l, edje) {
142                 obj_info = evas_object_data_get(edje, "obj_info");
143                 if (!obj_info) {
144                         ErrPrint("Object info is not valid\n");
145                         continue;
146                 }
147
148                 if (!id) {
149                         if (!obj_info->id)
150                                 return edje;
151
152                         continue;
153                 } else if (!obj_info->id) {
154                         continue;
155                 }
156
157                 if (!strcmp(obj_info->id, id))
158                         return edje;
159         }
160
161         DbgPrint("EDJE[%s] is not found\n", id);
162         return NULL;
163 }
164
165 PUBLIC const char *script_magic_id(void)
166 {
167         return "edje";
168 }
169
170 PUBLIC int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
171 {
172         struct info *handle = h;
173         Evas_Object *edje;
174         int r[3], g[3], b[3], a[3];
175         int ret;
176
177         edje = find_edje(handle, id);
178         if (!edje)
179                 return -ENOENT;
180
181         ret = sscanf(rgba, "%d %d %d %d %d %d %d %d %d %d %d %d",
182                                         r, g, b, a,                     /* OBJECT */
183                                         r + 1, g + 1, b + 1, a + 1,     /* OUTLINE */
184                                         r + 2, g + 2, b + 2, a + 2);    /* SHADOW */
185         if (ret != 12) {
186                 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
187                 return -EINVAL;
188         }
189
190         ret = edje_object_color_class_set(edje, part,
191                                 r[0], g[0], b[0], a[0], /* OBJECT */
192                                 r[1], g[1], b[1], a[1], /* OUTLINE */
193                                 r[2], g[2], b[2], a[2]); /* SHADOW */
194
195         DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
196         return 0;
197 }
198
199 PUBLIC int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
200 {
201         struct info *handle = h;
202         Evas_Object *edje;
203
204         edje = find_edje(handle, id);
205         if (!edje)
206                 return -ENOENT;
207
208         edje_object_part_text_set(edje, part, text);
209         return 0;
210 }
211
212 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
213 {
214         while (len > 0 && *value == ' ') {
215                 value++;
216                 len--;
217         }
218
219         if (len < 4)
220                 return;
221
222         img_opt->aspect = !strncasecmp(value, "true", 4);
223         DbgPrint("Parsed ASPECT: %d (%s)\n", img_opt->aspect, value);
224 }
225
226 static void parse_orient(struct image_option *img_opt, const char *value, int len)
227 {
228         while (len > 0 && *value == ' ') {
229                 value++;
230                 len--;
231         }
232
233         if (len < 4)
234                 return;
235
236         img_opt->orient = !strncasecmp(value, "true", 4);
237         DbgPrint("Parsed ORIENT: %d (%s)\n", img_opt->aspect, value);
238 }
239
240 static void parse_size(struct image_option *img_opt, const char *value, int len)
241 {
242         int width;
243         int height;
244         char *buf;
245
246         while (len > 0 && *value == ' ') {
247                 value++;
248                 len--;
249         }
250
251         buf = strndup(value, len);
252         if (!buf) {
253                 ErrPrint("Heap: %s\n", strerror(errno));
254                 return;
255         }
256
257         if (sscanf(buf, "%dx%d", &width, &height) == 2) {
258                 img_opt->width = width;
259                 img_opt->height = height;
260                 DbgPrint("Parsed size : %dx%d (%s)\n", width, height, buf);
261         } else {
262                 DbgPrint("Invalid size tag[%s]\n", buf);
263         }
264
265         free(buf);
266 }
267
268 static void parse_fill(struct image_option *img_opt, const char *value, int len)
269 {
270         while (len > 0 && *value == ' ') {
271                 value++;
272                 len--;
273         }
274
275         if (!strncasecmp(value, "in-size", len))
276                 img_opt->fill = FILL_IN_SIZE;
277         else if (!strncasecmp(value, "over-size", len))
278                 img_opt->fill = FILL_OVER_SIZE;
279         else
280                 img_opt->fill = FILL_DISABLE;
281
282         DbgPrint("Parsed FILL: %d (%s)\n", img_opt->fill, value);
283 }
284
285 static inline void parse_image_option(const char *option, struct image_option *img_opt)
286 {
287         const char *ptr;
288         const char *cmd;
289         const char *value;
290         struct {
291                 const char *cmd;
292                 void (*handler)(struct image_option *img_opt, const char *value, int len);
293         } cmd_list[] = {
294                 {
295                         .cmd = "aspect", /* Keep the aspect ratio */
296                         .handler = parse_aspect,
297                 },
298                 {
299                         .cmd = "orient", /* Keep the orientation value: for the rotated images */
300                         .handler = parse_orient,
301                 },
302                 {
303                         .cmd = "fill", /* Fill the image to its container */
304                         .handler = parse_fill, /* Value: in-size, over-size, disable(default) */
305                 },
306                 {
307                         .cmd = "size",
308                         .handler = parse_size,
309                 },
310         };
311         enum {
312                 STATE_START,
313                 STATE_TOKEN,
314                 STATE_DATA,
315                 STATE_IGNORE,
316                 STATE_ERROR,
317                 STATE_END,
318         } state;
319         int idx;
320         int tag;
321
322         if (!option || !*option)
323                 return;
324
325         state = STATE_START;
326         /*!
327          * \note
328          * GCC 4.7 warnings uninitialized idx and tag value.
329          * But it will be initialized by the state machine. :(
330          * Anyway, I just reset idx and tag for reducing the GCC4.7 complains.
331          */
332         idx = 0;
333         tag = 0;
334         cmd = NULL;
335         value = NULL;
336
337         for (ptr = option; state != STATE_END; ptr++) {
338                 switch (state) {
339                 case STATE_START:
340                         if (*ptr == '\0') {
341                                 state = STATE_END;
342                                 continue;
343                         }
344
345                         if (isalpha(*ptr)) {
346                                 state = STATE_TOKEN;
347                                 ptr--;
348                         }
349                         tag = 0;
350                         idx = 0;
351
352                         cmd = cmd_list[tag].cmd;
353                         break;
354                 case STATE_IGNORE:
355                         if (*ptr == '=') {
356                                 state = STATE_DATA;
357                                 value = ptr;
358                         } else if (*ptr == '\0') {
359                                 state = STATE_END;
360                         }
361                         break;
362                 case STATE_TOKEN:
363                         if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
364                                 if (*ptr == '=') {
365                                         value = ptr;
366                                         state = STATE_DATA;
367                                 } else {
368                                         state = STATE_IGNORE;
369                                 }
370                                 idx = 0;
371                         } else if (*ptr == '\0') {
372                                 state = STATE_END;
373                         } else if (cmd[idx] == *ptr) {
374                                 idx++;
375                         } else {
376                                 ptr -= (idx + 1);
377
378                                 tag++;
379                                 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
380                                         tag = 0;
381                                         state = STATE_ERROR;
382                                 } else {
383                                         cmd = cmd_list[tag].cmd;
384                                 }
385                                 idx = 0;
386                         }
387                         break;
388                 case STATE_DATA:
389                         if (*ptr == ';' || *ptr == '\0') {
390                                 cmd_list[tag].handler(img_opt, value + 1, idx);
391                                 state = *ptr ? STATE_START : STATE_END;
392                         } else {
393                                 idx++;
394                         }
395                         break;
396                 case STATE_ERROR:
397                         if (*ptr == ';')
398                                 state = STATE_START;
399                         else if (*ptr == '\0')
400                                 state = STATE_END;
401                         break;
402                 default:
403                         break;
404                 }
405         }
406 }
407
408 PUBLIC int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
409 {
410         struct info *handle = _h;
411         Evas_Load_Error err;
412         Evas_Object *edje;
413         Evas_Object *img;
414         Evas_Coord w, h;
415         struct obj_info *obj_info;
416         struct child *child;
417         struct image_option img_opt = {
418                 .aspect = 0,
419                 .orient = 0,
420                 .fill = FILL_DISABLE,
421                 .width = -1,
422                 .height = -1,
423         };
424
425         edje = find_edje(handle, id);
426         if (!edje) {
427                 ErrPrint("No such object: %s\n", id);
428                 return -ENOENT;
429         }
430
431         obj_info = evas_object_data_get(edje, "obj_info");
432         if (!obj_info) {
433                 ErrPrint("Object info is not available\n");
434                 return -EFAULT;
435         }
436
437         img = edje_object_part_swallow_get(edje, part);
438         if (img) {
439                 Eina_List *l;
440                 Eina_List *n;
441
442                 edje_object_part_unswallow(edje, img);
443
444                 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
445                         if (child->obj != img)
446                                 continue;
447
448                         obj_info->children = eina_list_remove(obj_info->children, child);
449                         free(child->part);
450                         free(child);
451                         break;
452                 }
453
454                 DbgPrint("delete object %s %p\n", part, img);
455                 evas_object_del(img);
456         }
457
458         if (!path || !strlen(path) || access(path, R_OK) != 0) {
459                 DbgPrint("SKIP - Path: [%s]\n", path);
460                 return 0;
461         }
462
463         child = malloc(sizeof(*child));
464         if (!child) {
465                 ErrPrint("Heap: %s\n", strerror(errno));
466                 return -ENOMEM;
467         }
468
469         child->part = strdup(part);
470         if (!child->part) {
471                 ErrPrint("Heap: %s\n", strerror(errno));
472                 free(child);
473                 return -ENOMEM;
474         }
475
476         img = evas_object_image_add(e);
477         if (!img) {
478                 ErrPrint("Failed to add an image object\n");
479                 free(child->part);
480                 free(child);
481                 return -EFAULT;
482         }
483
484         evas_object_image_preload(img, EINA_FALSE);
485         parse_image_option(option, &img_opt);
486         evas_object_image_load_orientation_set(img, img_opt.orient);
487
488         evas_object_image_file_set(img, path, NULL);
489         err = evas_object_image_load_error_get(img);
490         if (err != EVAS_LOAD_ERROR_NONE) {
491                 ErrPrint("Load error: %s\n", evas_load_error_str(err));
492                 evas_object_del(img);
493                 free(child->part);
494                 free(child);
495                 return -EIO;
496         }
497
498         evas_object_image_size_get(img, &w, &h);
499         if (img_opt.aspect) {
500                 if (img_opt.fill == FILL_OVER_SIZE) {
501                         Evas_Coord part_w;
502                         Evas_Coord part_h;
503
504                         if (img_opt.width >= 0 && img_opt.height >= 0) {
505                                 part_w = img_opt.width * scale_get();
506                                 part_h = img_opt.height * scale_get();
507                         } else {
508                                 part_w = 0;
509                                 part_h = 0;
510                                 edje_object_part_geometry_get(edje, part, NULL, NULL, &part_w, &part_h);
511                         }
512                         DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
513
514                         if (part_w > w || part_h > h) {
515                                 double fw;
516                                 double fh;
517
518                                 fw = (double)part_w / (double)w;
519                                 fh = (double)part_h / (double)h;
520
521                                 if (fw > fh) {
522                                         w = part_w;
523                                         h = (double)h * fw;
524                                 } else {
525                                         h = part_h;
526                                         w = (double)w * fh;
527                                 }
528                         }
529                         DbgPrint("Size: %dx%d\n", w, h);
530
531                         evas_object_data_set(img, "part_w", (void *)part_w);
532                         evas_object_data_set(img, "part_h", (void *)part_h);
533                         evas_object_data_set(img, "w", (void *)w);
534                         evas_object_data_set(img, "h", (void *)h);
535
536                         evas_object_image_load_size_set(img, w, h);
537                         evas_object_image_load_region_set(img, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h);
538                         evas_object_image_fill_set(img, 0, 0, part_w, part_h);
539                         evas_object_image_reload(img);
540                 } else {
541                         evas_object_image_fill_set(img, 0, 0, w, h);
542                         evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
543                         evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
544                 }
545         } else {
546                 if (img_opt.width >= 0 && img_opt.height >= 0) {
547                         w = img_opt.width;
548                         h = img_opt.height;
549                         DbgPrint("Using given image size: %dx%d\n", w, h);
550                 }
551
552                 evas_object_image_fill_set(img, 0, 0, w, h);
553                 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
554                 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
555         }
556
557         /*!
558          * \note
559          * object will be shown by below statement automatically
560          */
561         DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
562         child->obj = img;
563         edje_object_part_swallow(edje, part, img);
564         obj_info->children = eina_list_append(obj_info->children, child);
565
566         return 0;
567 }
568
569 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
570 {
571         struct info *handle = data;
572         Evas_Coord w;
573         Evas_Coord h;
574         Evas_Coord px = 0;
575         Evas_Coord py = 0;
576         Evas_Coord pw = 0;
577         Evas_Coord ph = 0;
578         double sx;
579         double sy;
580         double ex;
581         double ey;
582
583         evas_object_geometry_get(obj, NULL, NULL, &w, &h);
584         edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
585
586         sx = ex = 0.0f;
587         if (w) {
588                 sx = (double)px / (double)w;
589                 ex = (double)(px + pw) / (double)w;
590         }
591
592         sy = ey = 0.0f;
593         if (h) {
594                 sy = (double)py / (double)h;
595                 ey = (double)(py + ph) / (double)h;
596         }
597
598         DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
599         script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
600 }
601
602 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
603 {
604         struct info *handle = _info;
605         struct obj_info *obj_info;
606         struct child *child;
607
608         handle->obj_list = eina_list_remove(handle->obj_list, obj);
609
610         obj_info = evas_object_data_del(obj, "obj_info");
611         if (!obj_info) {
612                 ErrPrint("Object info is not valid\n");
613                 return;
614         }
615
616         DbgPrint("delete object %s %p\n", obj_info->id, obj);
617
618         edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
619
620         EINA_LIST_FREE(obj_info->children, child) {
621                 DbgPrint("delete object %s %p\n", child->part, child->obj);
622                 if (child->obj)
623                         evas_object_del(child->obj);
624                 free(child->part);
625                 free(child);
626         }
627
628         free(obj_info->id);
629         free(obj_info);
630 }
631
632 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)
633 {
634         struct info *handle = h;
635         Evas_Object *edje;
636         Evas_Object *obj;
637         struct obj_info *obj_info;
638         struct child *child;
639
640         DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
641
642         edje = find_edje(handle, src_id);
643         if (!edje) {
644                 ErrPrint("Edje is not exists\n");
645                 return -ENOENT;
646         }
647
648         obj_info = evas_object_data_get(edje, "obj_info");
649         if (!obj_info) {
650                 ErrPrint("Object info is not valid\n");
651                 return -EINVAL;
652         }
653
654         obj = edje_object_part_swallow_get(edje, part);
655         if (obj) {
656                 Eina_List *l;
657                 Eina_List *n;
658
659                 edje_object_part_unswallow(edje, obj);
660
661                 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
662                         if (child->obj != obj)
663                                 continue;
664
665                         obj_info->children = eina_list_remove(obj_info->children, child);
666                         free(child->part);
667                         free(child);
668                         break;
669                 }
670
671                 DbgPrint("delete object %s %p\n", part, obj);
672                 evas_object_del(obj);
673         }
674
675         if (!path || !strlen(path) || access(path, R_OK) != 0) {
676                 DbgPrint("SKIP - Path: [%s]\n", path);
677                 return 0;
678         }
679
680         obj = edje_object_add(e);
681         if (!obj) {
682                 ErrPrint("Failed to add a new edje object\n");
683                 return -EFAULT;
684         }
685
686         //edje_object_text_class_set(obj, TEXT_CLASS, s_info.font, s_info.size);
687         if (!edje_object_file_set(obj, path, group)) {
688                 int err;
689                 const char *errmsg;
690
691                 err = edje_object_load_error_get(obj);
692                 errmsg = edje_load_error_str(err);
693                 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
694                 evas_object_del(obj);
695                 return -EIO;
696         }
697
698         evas_object_show(obj);
699
700         obj_info = calloc(1, sizeof(*obj_info));
701         if (!obj_info) {
702                 ErrPrint("Failed to add a obj_info\n");
703                 evas_object_del(obj);
704                 return -ENOMEM;
705         }
706
707         obj_info->id = strdup(target_id);
708         if (!obj_info->id) {
709                 ErrPrint("Failed to add a obj_info\n");
710                 free(obj_info);
711                 evas_object_del(obj);
712                 return -ENOMEM;
713         }
714
715         child = malloc(sizeof(*child));
716         if (!child) {
717                 ErrPrint("Error: %s\n", strerror(errno));
718                 free(obj_info->id);
719                 free(obj_info);
720                 evas_object_del(obj);
721                 return -ENOMEM;
722         }
723
724         child->part = strdup(part);
725         if (!child->part) {
726                 ErrPrint("Error: %s\n", strerror(errno));
727                 free(child);
728                 free(obj_info->id);
729                 free(obj_info);
730                 evas_object_del(obj);
731                 return -ENOMEM;
732         }
733
734         child->obj = obj;
735
736         evas_object_data_set(obj, "obj_info", obj_info);
737         evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
738         edje_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
739         handle->obj_list = eina_list_append(handle->obj_list, obj);
740
741         DbgPrint("%s part swallow edje %p\n", part, obj);
742         edje_object_part_swallow(edje, part, obj);
743         obj_info = evas_object_data_get(edje, "obj_info");
744         obj_info->children = eina_list_append(obj_info->children, child);
745         return 0;
746 }
747
748 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
749 {
750         struct info *handle = h;
751         Evas_Object *edje;
752
753         DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
754
755         edje = find_edje(handle, id);
756         if (!edje)
757                 return -ENOENT;
758
759         edje_object_signal_emit(edje, signal, part);
760         return 0;
761 }
762
763 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
764 {
765         struct info *handle = h;
766         Evas_Object *edje;
767
768         DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
769
770         edje = find_edje(handle, id);
771         if (!edje)
772                 return -ENOENT;
773
774         edje_object_part_drag_value_set(edje, part, x, y);
775         return 0;
776 }
777
778 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
779 {
780         struct info *handle = han;
781         Evas_Object *edje;
782
783         edje = find_edje(handle, id);
784         if (!edje)
785                 return -ENOENT;
786
787         if (!id) {
788                 handle->w = w;
789                 handle->h = h;
790         }
791
792         DbgPrint("Resize object to %dx%d\n", w, h);
793         evas_object_resize(edje, w, h);
794         return 0;
795 }
796
797 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
798 {
799         struct info *handle = h;
800
801         DbgPrint("id[%s], category[%s]\n", id, category);
802
803         if (handle->category) {
804                 free(handle->category);
805                 handle->category = NULL;
806         }
807
808         if (!category)
809                 return 0;
810
811         handle->category = strdup(category);
812         if (!handle->category) {
813                 ErrPrint("Error: %s\n", strerror(errno));
814                 return -ENOMEM;
815         }
816
817         return 0;
818 }
819
820 PUBLIC void *script_create(const char *file, const char *group)
821 {
822         struct info *handle;
823
824         DbgPrint("file[%s], group[%s]\n", file, group);
825
826         handle = calloc(1, sizeof(*handle));
827         if (!handle) {
828                 ErrPrint("Error: %s\n", strerror(errno));
829                 return NULL;
830         }
831
832         handle->file = strdup(file);
833         if (!handle->file) {
834                 ErrPrint("Error: %s\n", strerror(errno));
835                 free(handle);
836                 return NULL;
837         }
838
839         handle->group = strdup(group);
840         if (!handle->group) {
841                 ErrPrint("Error: %s\n", strerror(errno));
842                 free(handle->file);
843                 free(handle);
844                 return NULL;
845         }
846
847         return handle;
848 }
849
850 PUBLIC int script_destroy(void *_handle)
851 {
852         struct info *handle;
853         Evas_Object *edje;
854
855         handle = _handle;
856
857         edje = eina_list_nth(handle->obj_list, 0);
858         if (edje)
859                 evas_object_del(edje);
860
861         free(handle->category);
862         free(handle->file);
863         free(handle->group);
864         free(handle);
865         return 0;
866 }
867
868 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
869 {
870         struct info *handle;
871         Evas_Object *edje;
872         struct obj_info *obj_info;
873
874         handle = _handle;
875
876         obj_info = calloc(1, sizeof(*obj_info));
877         if (!obj_info) {
878                 ErrPrint("Heap: %s\n", strerror(errno));
879                 return -ENOMEM;
880         }
881
882         edje = edje_object_add(e);
883         if (!edje) {
884                 ErrPrint("Failed to create an edje object\n");
885                 free(obj_info);
886                 return -EFAULT;
887         }
888
889         //edje_object_text_class_set(edje, TEXT_CLASS, s_info.font, s_info.size);
890         DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
891         if (!edje_object_file_set(edje, handle->file, handle->group)) {
892                 int err;
893                 const char *errmsg;
894
895                 err = edje_object_load_error_get(edje);
896                 errmsg = edje_load_error_str(err);
897                 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
898                 evas_object_del(edje);
899                 free(obj_info);
900                 return -EIO;
901         }
902
903         handle->e = e;
904         handle->w = w;
905         handle->h = h;
906
907         edje_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
908         evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
909         evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
910         evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
911         evas_object_resize(edje, handle->w, handle->h);
912         evas_object_show(edje);
913         evas_object_data_set(edje, "obj_info", obj_info);
914
915         handle->obj_list = eina_list_append(handle->obj_list, edje);
916         return 0;
917 }
918
919 PUBLIC int script_unload(void *_handle, Evas *e)
920 {
921         struct info *handle;
922         Evas_Object *edje;
923
924         handle = _handle;
925
926         DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
927         edje = eina_list_nth(handle->obj_list, 0);
928         if (edje)
929                 evas_object_del(edje);
930         handle->e = NULL;
931         return 0;
932 }
933
934 static Eina_Bool property_cb(void *data, int type, void *event)
935 {
936         Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
937
938         if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
939                 Eina_List *list;
940                 char *text;
941                 char *font;
942                 int cache;
943
944                 font = vconf_get_str("db/setting/accessibility/font_name");
945                 if (!font)
946                         return ECORE_CALLBACK_PASS_ON;
947
948                 if (s_info.font)
949                         free(s_info.font);
950
951                 s_info.font = font;
952
953                 cache = evas_common_font_cache_get();
954                 evas_common_font_cache_set(0);
955                 evas_common_font_flush();
956
957                 list = edje_text_class_list();
958                 EINA_LIST_FREE(list, text) {
959                         if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
960                                 edje_text_class_del(text);
961                                 edje_text_class_set(text, s_info.font, s_info.size);
962                                 DbgPrint("Update text class %s (%s, %d)\n", text, s_info.font, s_info.size);
963                         } else {
964                                 DbgPrint("Skip text class %s\n", text);
965                         }
966                 }
967
968                 evas_common_font_cache_set(cache);
969         }
970
971         return ECORE_CALLBACK_PASS_ON;
972 }
973
974 static void font_name_cb(keynode_t *node, void *user_data)
975 {
976         const char *font;
977
978         if (!node)
979                 return;
980
981         font = vconf_keynode_get_str(node);
982         if (!font)
983                 return;
984
985         DbgPrint("Font changed to %s\n", font);
986 }
987
988 static void font_size_cb(keynode_t *node, void *user_data)
989 {
990         if (!node)
991                 return;
992         /*!
993          * \TODO
994          * Implementing me.
995          */
996         DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
997 }
998
999 PUBLIC int script_init(void)
1000 {
1001         int ret;
1002         /* ecore is already initialized */
1003         edje_init();
1004         edje_scale_set(scale_get());
1005
1006         s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
1007         if (!s_info.property_handler)
1008                 ErrPrint("Failed to add a property change event handler\n");
1009
1010         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
1011         if (ret < 0)
1012                 ErrPrint("Failed to add vconf for font size change\n");
1013
1014         ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
1015         if (ret < 0)
1016                 ErrPrint("Failed to add vconf for font name change\n");
1017
1018         return 0;
1019 }
1020
1021 PUBLIC int script_fini(void)
1022 {
1023         vconf_ignore_key_changed("db/setting/accessibility/font_name", font_name_cb);
1024         vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb);
1025         ecore_event_handler_del(s_info.property_handler);
1026         s_info.property_handler = NULL;
1027         edje_shutdown();
1028         return 0;
1029 }
1030
1031 /* End of a file */