2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
27 #include <Ecore_Evas.h>
35 #include "script_port.h"
37 #define TEXT_CLASS "tizen"
39 #define PUBLIC __attribute__((visibility("default")))
41 extern void evas_common_font_flush(void);
42 extern int evas_common_font_cache_get(void);
43 extern void evas_common_font_cache_set(int size);
78 Ecore_Event_Handler *property_handler;
82 .property_handler = NULL,
89 * Reservce this for future use
90 static inline void common_cache_flush(void *evas)
97 file_cache = edje_file_cache_get();
98 collection_cache = edje_collection_cache_get();
99 image_cache = evas_image_cache_get(evas);
100 font_cache = evas_font_cache_get(evas);
102 edje_file_cache_set(file_cache);
103 edje_collection_cache_set(collection_cache);
104 evas_image_cache_set(evas, 0);
105 evas_font_cache_set(evas, 0);
107 evas_image_cache_flush(evas);
108 evas_render_idle_flush(evas);
109 evas_font_cache_flush(evas);
111 edje_file_cache_flush();
112 edje_collection_cache_flush();
114 edje_file_cache_set(file_cache);
115 edje_collection_cache_set(collection_cache);
116 evas_image_cache_set(evas, image_cache);
117 evas_font_cache_set(evas, font_cache);
123 static inline Evas_Object *find_edje(struct info *handle, const char *id)
127 struct obj_info *obj_info;
129 EINA_LIST_FOREACH(handle->obj_list, l, edje) {
130 obj_info = evas_object_data_get(edje, "obj_info");
132 ErrPrint("Object info is not valid\n");
141 } else if (!obj_info->id) {
145 if (!strcmp(obj_info->id, id))
149 DbgPrint("EDJE[%s] is not found\n", id);
153 PUBLIC const char *script_magic_id(void)
158 PUBLIC int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
160 struct info *handle = h;
162 int r[3], g[3], b[3], a[3];
165 edje = find_edje(handle, id);
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 */
174 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
178 ret = edje_object_color_class_set(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 */
183 DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
187 PUBLIC int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
189 struct info *handle = h;
192 edje = find_edje(handle, id);
196 edje_object_part_text_set(edje, part, text);
200 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
202 while (len > 0 && *value == ' ') {
210 img_opt->aspect = !strncasecmp(value, "true", 4);
211 DbgPrint("Parsed ASPECT: %d\n", img_opt->aspect);
214 static void parse_orient(struct image_option *img_opt, const char *value, int len)
216 while (len > 0 && *value == ' ') {
224 img_opt->orient = !strncasecmp(value, "true", 4);
225 DbgPrint("Parsed ORIENT: %d\n", img_opt->aspect);
228 static void parse_fill(struct image_option *img_opt, const char *value, int len)
230 while (len > 0 && *value == ' ') {
235 if (!strcasecmp(value, "in-size"))
236 img_opt->fill = FILL_IN_SIZE;
237 else if (!strcasecmp(value, "over-size"))
238 img_opt->fill = FILL_OVER_SIZE;
240 img_opt->fill = FILL_DISABLE;
242 DbgPrint("Parsed FILL: %d\n", img_opt->fill);
245 static inline void parse_image_option(const char *option, struct image_option *img_opt)
252 void (*handler)(struct image_option *img_opt, const char *value, int len);
255 .cmd = "aspect", /* Keep the aspect ratio */
256 .handler = parse_aspect,
259 .cmd = "orient", /* Keep the orientation value: for the rotated images */
260 .handler = parse_orient,
263 .cmd = "fill", /* Fill the image to its container */
264 .handler = parse_fill, /* Value: in-size, over-size, disable(default) */
278 if (!option || !*option)
283 for (ptr = option; state != STATE_END; ptr++) {
298 cmd = cmd_list[tag].cmd;
304 } else if (*ptr == '\0') {
309 if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
314 state = STATE_IGNORE;
317 } else if (*ptr == '\0') {
319 } else if (cmd[idx] == *ptr) {
325 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
329 cmd = cmd_list[tag].cmd;
335 if (*ptr == ';' || *ptr == '\0') {
336 cmd_list[tag].handler(img_opt, value + 1, idx);
337 state = *ptr ? STATE_START : STATE_END;
345 else if (*ptr == '\0')
354 PUBLIC int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
356 struct info *handle = _h;
361 struct obj_info *obj_info;
363 struct image_option img_opt = {
366 .fill = FILL_DISABLE,
369 edje = find_edje(handle, id);
371 ErrPrint("No such object: %s\n", id);
375 obj_info = evas_object_data_get(edje, "obj_info");
377 ErrPrint("Object info is not available\n");
381 img = edje_object_part_swallow_get(edje, part);
386 edje_object_part_unswallow(edje, img);
388 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
389 if (child->obj != img)
392 obj_info->children = eina_list_remove(obj_info->children, child);
398 DbgPrint("delete object %s %p\n", part, img);
399 evas_object_del(img);
402 if (!path || !strlen(path) || access(path, R_OK) != 0) {
403 DbgPrint("SKIP - Path: [%s]\n", path);
407 child = malloc(sizeof(*child));
409 ErrPrint("Heap: %s\n", strerror(errno));
413 child->part = strdup(part);
415 ErrPrint("Heap: %s\n", strerror(errno));
420 img = evas_object_image_filled_add(e);
422 ErrPrint("Failed to add an image object\n");
428 parse_image_option(option, &img_opt);
429 evas_object_image_load_orientation_set(img, img_opt.orient);
433 evas_object_image_file_set(img, path, NULL);
435 err = evas_object_image_load_error_get(img);
436 if (err != EVAS_LOAD_ERROR_NONE) {
437 ErrPrint("Load error: %s\n", evas_load_error_str(err));
438 evas_object_del(img);
444 evas_object_image_size_get(img, &w, &h);
445 if (img_opt.aspect) {
446 if (img_opt.fill == FILL_OVER_SIZE) {
452 edje_object_part_geometry_get(edje, part, NULL, NULL, &part_w, &part_h);
453 DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
458 if (abs(tmp_w) > abs(tmp_h)) {
461 h = (int)((double)part_w * (double)h / (double)w);
464 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_VERTICAL, w, h);
468 w = (int)((double)part_h * (double)w / (double)h);
471 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_HORIZONTAL, w, h);
474 evas_object_image_fill_set(img, 0, 0, w, h);
475 evas_object_size_hint_min_set(img, w, h);
476 evas_object_size_hint_max_set(img, w, h);
478 evas_object_image_fill_set(img, 0, 0, w, h);
479 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
480 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
483 evas_object_image_fill_set(img, 0, 0, w, h);
484 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
485 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
488 evas_object_resize(img, w, h);
492 * object will be shown by below statement automatically
494 DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
495 edje_object_part_swallow(edje, part, img);
496 obj_info->children = eina_list_append(obj_info->children, child);
501 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
503 struct info *handle = data;
515 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
516 edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
520 sx = (double)px / (double)w;
521 ex = (double)(px + pw) / (double)w;
526 sy = (double)py / (double)h;
527 ey = (double)(py + ph) / (double)h;
530 DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
531 script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
534 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
536 struct info *handle = _info;
537 struct obj_info *obj_info;
540 handle->obj_list = eina_list_remove(handle->obj_list, obj);
542 obj_info = evas_object_data_del(obj, "obj_info");
544 ErrPrint("Object info is not valid\n");
548 DbgPrint("delete object %s %p\n", obj_info->id, obj);
550 edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
552 EINA_LIST_FREE(obj_info->children, child) {
553 DbgPrint("delete object %s %p\n", child->part, child->obj);
555 evas_object_del(child->obj);
564 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)
566 struct info *handle = h;
569 struct obj_info *obj_info;
572 DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
574 edje = find_edje(handle, src_id);
576 ErrPrint("Edje is not exists\n");
580 obj_info = evas_object_data_get(edje, "obj_info");
582 ErrPrint("Object info is not valid\n");
586 obj = edje_object_part_swallow_get(edje, part);
591 edje_object_part_unswallow(edje, obj);
593 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
594 if (child->obj != obj)
597 obj_info->children = eina_list_remove(obj_info->children, child);
603 DbgPrint("delete object %s %p\n", part, obj);
604 evas_object_del(obj);
607 if (!path || !strlen(path) || access(path, R_OK) != 0) {
608 DbgPrint("SKIP - Path: [%s]\n", path);
612 obj = edje_object_add(e);
614 ErrPrint("Failed to add a new edje object\n");
618 //edje_object_text_class_set(obj, TEXT_CLASS, s_info.font, s_info.size);
619 if (!edje_object_file_set(obj, path, group)) {
623 err = edje_object_load_error_get(obj);
624 errmsg = edje_load_error_str(err);
625 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
626 evas_object_del(obj);
630 evas_object_show(obj);
632 obj_info = calloc(1, sizeof(*obj_info));
634 ErrPrint("Failed to add a obj_info\n");
635 evas_object_del(obj);
639 obj_info->id = strdup(target_id);
641 ErrPrint("Failed to add a obj_info\n");
643 evas_object_del(obj);
647 child = malloc(sizeof(*child));
649 ErrPrint("Error: %s\n", strerror(errno));
652 evas_object_del(obj);
656 child->part = strdup(part);
658 ErrPrint("Error: %s\n", strerror(errno));
662 evas_object_del(obj);
668 evas_object_data_set(obj, "obj_info", obj_info);
669 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
670 edje_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
671 handle->obj_list = eina_list_append(handle->obj_list, obj);
673 DbgPrint("%s part swallow edje %p\n", part, obj);
674 edje_object_part_swallow(edje, part, obj);
675 obj_info = evas_object_data_get(edje, "obj_info");
676 obj_info->children = eina_list_append(obj_info->children, child);
680 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
682 struct info *handle = h;
685 DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
687 edje = find_edje(handle, id);
691 edje_object_signal_emit(edje, signal, part);
695 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
697 struct info *handle = h;
700 DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
702 edje = find_edje(handle, id);
706 edje_object_part_drag_value_set(edje, part, x, y);
710 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
712 struct info *handle = han;
715 edje = find_edje(handle, id);
724 DbgPrint("Resize object to %dx%d\n", w, h);
725 evas_object_resize(edje, w, h);
729 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
731 struct info *handle = h;
733 DbgPrint("id[%s], category[%s]\n", id, category);
735 if (handle->category) {
736 free(handle->category);
737 handle->category = NULL;
743 handle->category = strdup(category);
744 if (!handle->category) {
745 ErrPrint("Error: %s\n", strerror(errno));
752 PUBLIC void *script_create(const char *file, const char *group)
756 DbgPrint("file[%s], group[%s]\n", file, group);
758 handle = calloc(1, sizeof(*handle));
760 ErrPrint("Error: %s\n", strerror(errno));
764 handle->file = strdup(file);
766 ErrPrint("Error: %s\n", strerror(errno));
771 handle->group = strdup(group);
772 if (!handle->group) {
773 ErrPrint("Error: %s\n", strerror(errno));
782 PUBLIC int script_destroy(void *_handle)
789 edje = eina_list_nth(handle->obj_list, 0);
791 evas_object_del(edje);
793 free(handle->category);
800 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
804 struct obj_info *obj_info;
808 obj_info = calloc(1, sizeof(*obj_info));
810 ErrPrint("Heap: %s\n", strerror(errno));
814 edje = edje_object_add(e);
816 ErrPrint("Failed to create an edje object\n");
821 //edje_object_text_class_set(edje, TEXT_CLASS, s_info.font, s_info.size);
822 DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
823 if (!edje_object_file_set(edje, handle->file, handle->group)) {
827 err = edje_object_load_error_get(edje);
828 errmsg = edje_load_error_str(err);
829 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
830 evas_object_del(edje);
839 edje_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
840 evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
841 evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
842 evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
843 evas_object_resize(edje, handle->w, handle->h);
844 evas_object_show(edje);
845 evas_object_data_set(edje, "obj_info", obj_info);
847 handle->obj_list = eina_list_append(handle->obj_list, edje);
851 PUBLIC int script_unload(void *_handle, Evas *e)
858 DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
859 edje = eina_list_nth(handle->obj_list, 0);
861 evas_object_del(edje);
866 static Eina_Bool property_cb(void *data, int type, void *event)
868 Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
870 if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
876 font = vconf_get_str("db/setting/accessibility/font_name");
878 return ECORE_CALLBACK_PASS_ON;
885 cache = evas_common_font_cache_get();
886 evas_common_font_cache_set(0);
887 evas_common_font_flush();
889 list = edje_text_class_list();
890 EINA_LIST_FREE(list, text) {
891 if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
892 edje_text_class_del(text);
893 edje_text_class_set(text, s_info.font, s_info.size);
894 DbgPrint("Update text class %s (%s, %d)\n", text, s_info.font, s_info.size);
896 DbgPrint("Skip text class %s\n", text);
900 evas_common_font_cache_set(cache);
903 return ECORE_CALLBACK_PASS_ON;
906 static void font_name_cb(keynode_t *node, void *user_data)
913 font = vconf_keynode_get_str(node);
917 DbgPrint("Font changed to %s\n", font);
920 static void font_size_cb(keynode_t *node, void *user_data)
928 DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
931 PUBLIC int script_init(void)
934 /* ecore is already initialized */
937 s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
938 if (!s_info.property_handler)
939 ErrPrint("Failed to add a property change event handler\n");
941 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
943 ErrPrint("Failed to add vconf for font size change\n");
945 ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
947 ErrPrint("Failed to add vconf for font name change\n");
952 PUBLIC int script_fini(void)
955 vconf_ignore_key_changed("db/setting/accessibility/font_name", font_name_cb);
956 vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb);
957 ecore_event_handler_del(s_info.property_handler);
958 s_info.property_handler = NULL;