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);
81 Ecore_Event_Handler *property_handler;
85 .property_handler = NULL,
92 * Reservce this for future use
93 static inline void common_cache_flush(void *evas)
100 file_cache = edje_file_cache_get();
101 collection_cache = edje_collection_cache_get();
102 image_cache = evas_image_cache_get(evas);
103 font_cache = evas_font_cache_get(evas);
105 edje_file_cache_set(file_cache);
106 edje_collection_cache_set(collection_cache);
107 evas_image_cache_set(evas, 0);
108 evas_font_cache_set(evas, 0);
110 evas_image_cache_flush(evas);
111 evas_render_idle_flush(evas);
112 evas_font_cache_flush(evas);
114 edje_file_cache_flush();
115 edje_collection_cache_flush();
117 edje_file_cache_set(file_cache);
118 edje_collection_cache_set(collection_cache);
119 evas_image_cache_set(evas, image_cache);
120 evas_font_cache_set(evas, font_cache);
126 static inline Evas_Object *find_edje(struct info *handle, const char *id)
130 struct obj_info *obj_info;
132 EINA_LIST_FOREACH(handle->obj_list, l, edje) {
133 obj_info = evas_object_data_get(edje, "obj_info");
135 ErrPrint("Object info is not valid\n");
144 } else if (!obj_info->id) {
148 if (!strcmp(obj_info->id, id))
152 DbgPrint("EDJE[%s] is not found\n", id);
156 PUBLIC const char *script_magic_id(void)
161 PUBLIC int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
163 struct info *handle = h;
165 int r[3], g[3], b[3], a[3];
168 edje = find_edje(handle, id);
172 ret = sscanf(rgba, "%d %d %d %d %d %d %d %d %d %d %d %d",
173 r, g, b, a, /* OBJECT */
174 r + 1, g + 1, b + 1, a + 1, /* OUTLINE */
175 r + 2, g + 2, b + 2, a + 2); /* SHADOW */
177 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
181 ret = edje_object_color_class_set(edje, part,
182 r[0], g[0], b[0], a[0], /* OBJECT */
183 r[1], g[1], b[1], a[1], /* OUTLINE */
184 r[2], g[2], b[2], a[2]); /* SHADOW */
186 DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
190 PUBLIC int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
192 struct info *handle = h;
195 edje = find_edje(handle, id);
199 edje_object_part_text_set(edje, part, text);
203 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
205 while (len > 0 && *value == ' ') {
213 img_opt->aspect = !strncasecmp(value, "true", 4);
214 DbgPrint("Parsed ASPECT: %d (%s)\n", img_opt->aspect, value);
217 static void parse_orient(struct image_option *img_opt, const char *value, int len)
219 while (len > 0 && *value == ' ') {
227 img_opt->orient = !strncasecmp(value, "true", 4);
228 DbgPrint("Parsed ORIENT: %d (%s)\n", img_opt->aspect, value);
231 static void parse_size(struct image_option *img_opt, const char *value, int len)
237 while (len > 0 && *value == ' ') {
242 buf = strndup(value, len);
244 ErrPrint("Heap: %s\n", strerror(errno));
248 if (sscanf(buf, "%dx%d", &width, &height) == 2) {
249 img_opt->width = width;
250 img_opt->height = height;
251 DbgPrint("Parsed size : %dx%d (%s)\n", width, height, buf);
253 DbgPrint("Invalid size tag[%s]\n", buf);
259 static void parse_fill(struct image_option *img_opt, const char *value, int len)
261 while (len > 0 && *value == ' ') {
266 if (!strncasecmp(value, "in-size", len))
267 img_opt->fill = FILL_IN_SIZE;
268 else if (!strncasecmp(value, "over-size", len))
269 img_opt->fill = FILL_OVER_SIZE;
271 img_opt->fill = FILL_DISABLE;
273 DbgPrint("Parsed FILL: %d (%s)\n", img_opt->fill, value);
276 static inline void parse_image_option(const char *option, struct image_option *img_opt)
283 void (*handler)(struct image_option *img_opt, const char *value, int len);
286 .cmd = "aspect", /* Keep the aspect ratio */
287 .handler = parse_aspect,
290 .cmd = "orient", /* Keep the orientation value: for the rotated images */
291 .handler = parse_orient,
294 .cmd = "fill", /* Fill the image to its container */
295 .handler = parse_fill, /* Value: in-size, over-size, disable(default) */
299 .handler = parse_size,
313 if (!option || !*option)
318 for (ptr = option; state != STATE_END; ptr++) {
333 cmd = cmd_list[tag].cmd;
339 } else if (*ptr == '\0') {
344 if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
349 state = STATE_IGNORE;
352 } else if (*ptr == '\0') {
354 } else if (cmd[idx] == *ptr) {
360 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
364 cmd = cmd_list[tag].cmd;
370 if (*ptr == ';' || *ptr == '\0') {
371 cmd_list[tag].handler(img_opt, value + 1, idx);
372 state = *ptr ? STATE_START : STATE_END;
380 else if (*ptr == '\0')
389 static void cropped_resize_cb(void *data, Evas *e, Evas_Object *obj, void *ei)
396 /* grab image size */
397 part_w = (int)evas_object_data_get(obj, "part_w");
398 part_h = (int)evas_object_data_get(obj, "part_h");
399 w = (int)evas_object_data_get(obj, "w");
400 h = (int)evas_object_data_get(obj, "h");
402 /* grab base object dimensions */
403 evas_object_resize(obj, part_w, part_h);
404 evas_object_image_fill_set(obj, 0, 0, w, h);
406 DbgPrint("Cropped: %dx%d - %dx%d, ImageSize(%dx%d)\n", (w - part_w) / 2, (h - part_h) / 2, part_w, part_h, w, h);
409 PUBLIC int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
411 struct info *handle = _h;
416 struct obj_info *obj_info;
418 struct image_option img_opt = {
421 .fill = FILL_DISABLE,
426 edje = find_edje(handle, id);
428 ErrPrint("No such object: %s\n", id);
432 obj_info = evas_object_data_get(edje, "obj_info");
434 ErrPrint("Object info is not available\n");
438 img = edje_object_part_swallow_get(edje, part);
443 edje_object_part_unswallow(edje, img);
445 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
446 if (child->obj != img)
449 obj_info->children = eina_list_remove(obj_info->children, child);
455 DbgPrint("delete object %s %p\n", part, img);
456 evas_object_del(img);
459 if (!path || !strlen(path) || access(path, R_OK) != 0) {
460 DbgPrint("SKIP - Path: [%s]\n", path);
464 child = malloc(sizeof(*child));
466 ErrPrint("Heap: %s\n", strerror(errno));
470 child->part = strdup(part);
472 ErrPrint("Heap: %s\n", strerror(errno));
477 img = evas_object_image_add(e);
479 ErrPrint("Failed to add an image object\n");
485 parse_image_option(option, &img_opt);
486 evas_object_image_load_orientation_set(img, img_opt.orient);
488 evas_object_image_file_set(img, path, NULL);
490 err = evas_object_image_load_error_get(img);
491 if (err != EVAS_LOAD_ERROR_NONE) {
492 ErrPrint("Load error: %s\n", evas_load_error_str(err));
493 evas_object_del(img);
499 evas_object_image_size_get(img, &w, &h);
500 if (img_opt.aspect) {
501 if (img_opt.fill == FILL_OVER_SIZE) {
507 if (img_opt.width >= 0 && img_opt.height >= 0) {
508 part_w = img_opt.width;
509 part_h = img_opt.height;
511 edje_object_part_geometry_get(edje, part, NULL, NULL, &part_w, &part_h);
513 evas_object_data_set(img, "part_w", (void *)part_w);
514 evas_object_data_set(img, "part_h", (void *)part_h);
515 DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
520 if (abs(tmp_w) > abs(tmp_h)) {
522 DbgPrint("Before: (h = %d * %d / %d)\n", part_w, h, w);
523 h = (int)((double)part_w * (double)h / (double)w);
525 DbgPrint("Size: %dx%d\n", w, h);
529 DbgPrint("Before: (w = %d * %d / %d)\n", part_h, w, h);
530 w = (int)((double)part_h * (double)w / (double)h);
532 DbgPrint("Size: %dx%d\n", w, h);
536 evas_object_data_set(img, "w", (void *)w);
537 evas_object_data_set(img, "h", (void *)h);
538 evas_object_image_load_size_set(img, w, h);
539 evas_object_image_load_region_set(img, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h);
540 evas_object_image_reload(img);
541 evas_object_event_callback_add(img, EVAS_CALLBACK_RESIZE, cropped_resize_cb, NULL);
543 evas_object_image_fill_set(img, 0, 0, w, h);
544 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
545 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
548 if (img_opt.width >= 0 && img_opt.height >= 0) {
551 DbgPrint("Using given image size: %dx%d\n", w, h);
554 evas_object_image_fill_set(img, 0, 0, w, h);
555 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
556 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
561 * object will be shown by below statement automatically
563 DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
565 edje_object_part_swallow(edje, part, img);
566 obj_info->children = eina_list_append(obj_info->children, child);
571 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
573 struct info *handle = data;
585 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
586 edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
590 sx = (double)px / (double)w;
591 ex = (double)(px + pw) / (double)w;
596 sy = (double)py / (double)h;
597 ey = (double)(py + ph) / (double)h;
600 DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
601 script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
604 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
606 struct info *handle = _info;
607 struct obj_info *obj_info;
610 handle->obj_list = eina_list_remove(handle->obj_list, obj);
612 obj_info = evas_object_data_del(obj, "obj_info");
614 ErrPrint("Object info is not valid\n");
618 DbgPrint("delete object %s %p\n", obj_info->id, obj);
620 edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
622 EINA_LIST_FREE(obj_info->children, child) {
623 DbgPrint("delete object %s %p\n", child->part, child->obj);
625 evas_object_del(child->obj);
634 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)
636 struct info *handle = h;
639 struct obj_info *obj_info;
642 DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
644 edje = find_edje(handle, src_id);
646 ErrPrint("Edje is not exists\n");
650 obj_info = evas_object_data_get(edje, "obj_info");
652 ErrPrint("Object info is not valid\n");
656 obj = edje_object_part_swallow_get(edje, part);
661 edje_object_part_unswallow(edje, obj);
663 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
664 if (child->obj != obj)
667 obj_info->children = eina_list_remove(obj_info->children, child);
673 DbgPrint("delete object %s %p\n", part, obj);
674 evas_object_del(obj);
677 if (!path || !strlen(path) || access(path, R_OK) != 0) {
678 DbgPrint("SKIP - Path: [%s]\n", path);
682 obj = edje_object_add(e);
684 ErrPrint("Failed to add a new edje object\n");
688 //edje_object_text_class_set(obj, TEXT_CLASS, s_info.font, s_info.size);
689 if (!edje_object_file_set(obj, path, group)) {
693 err = edje_object_load_error_get(obj);
694 errmsg = edje_load_error_str(err);
695 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
696 evas_object_del(obj);
700 evas_object_show(obj);
702 obj_info = calloc(1, sizeof(*obj_info));
704 ErrPrint("Failed to add a obj_info\n");
705 evas_object_del(obj);
709 obj_info->id = strdup(target_id);
711 ErrPrint("Failed to add a obj_info\n");
713 evas_object_del(obj);
717 child = malloc(sizeof(*child));
719 ErrPrint("Error: %s\n", strerror(errno));
722 evas_object_del(obj);
726 child->part = strdup(part);
728 ErrPrint("Error: %s\n", strerror(errno));
732 evas_object_del(obj);
738 evas_object_data_set(obj, "obj_info", obj_info);
739 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
740 edje_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
741 handle->obj_list = eina_list_append(handle->obj_list, obj);
743 DbgPrint("%s part swallow edje %p\n", part, obj);
744 edje_object_part_swallow(edje, part, obj);
745 obj_info = evas_object_data_get(edje, "obj_info");
746 obj_info->children = eina_list_append(obj_info->children, child);
750 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
752 struct info *handle = h;
755 DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
757 edje = find_edje(handle, id);
761 edje_object_signal_emit(edje, signal, part);
765 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
767 struct info *handle = h;
770 DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
772 edje = find_edje(handle, id);
776 edje_object_part_drag_value_set(edje, part, x, y);
780 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
782 struct info *handle = han;
785 edje = find_edje(handle, id);
794 DbgPrint("Resize object to %dx%d\n", w, h);
795 evas_object_resize(edje, w, h);
799 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
801 struct info *handle = h;
803 DbgPrint("id[%s], category[%s]\n", id, category);
805 if (handle->category) {
806 free(handle->category);
807 handle->category = NULL;
813 handle->category = strdup(category);
814 if (!handle->category) {
815 ErrPrint("Error: %s\n", strerror(errno));
822 PUBLIC void *script_create(const char *file, const char *group)
826 DbgPrint("file[%s], group[%s]\n", file, group);
828 handle = calloc(1, sizeof(*handle));
830 ErrPrint("Error: %s\n", strerror(errno));
834 handle->file = strdup(file);
836 ErrPrint("Error: %s\n", strerror(errno));
841 handle->group = strdup(group);
842 if (!handle->group) {
843 ErrPrint("Error: %s\n", strerror(errno));
852 PUBLIC int script_destroy(void *_handle)
859 edje = eina_list_nth(handle->obj_list, 0);
861 evas_object_del(edje);
863 free(handle->category);
870 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
874 struct obj_info *obj_info;
878 obj_info = calloc(1, sizeof(*obj_info));
880 ErrPrint("Heap: %s\n", strerror(errno));
884 edje = edje_object_add(e);
886 ErrPrint("Failed to create an edje object\n");
891 //edje_object_text_class_set(edje, TEXT_CLASS, s_info.font, s_info.size);
892 DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
893 if (!edje_object_file_set(edje, handle->file, handle->group)) {
897 err = edje_object_load_error_get(edje);
898 errmsg = edje_load_error_str(err);
899 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
900 evas_object_del(edje);
909 edje_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
910 evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
911 evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
912 evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
913 evas_object_resize(edje, handle->w, handle->h);
914 evas_object_show(edje);
915 evas_object_data_set(edje, "obj_info", obj_info);
917 handle->obj_list = eina_list_append(handle->obj_list, edje);
921 PUBLIC int script_unload(void *_handle, Evas *e)
928 DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
929 edje = eina_list_nth(handle->obj_list, 0);
931 evas_object_del(edje);
936 static Eina_Bool property_cb(void *data, int type, void *event)
938 Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
940 if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
946 font = vconf_get_str("db/setting/accessibility/font_name");
948 return ECORE_CALLBACK_PASS_ON;
955 cache = evas_common_font_cache_get();
956 evas_common_font_cache_set(0);
957 evas_common_font_flush();
959 list = edje_text_class_list();
960 EINA_LIST_FREE(list, text) {
961 if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
962 edje_text_class_del(text);
963 edje_text_class_set(text, s_info.font, s_info.size);
964 DbgPrint("Update text class %s (%s, %d)\n", text, s_info.font, s_info.size);
966 DbgPrint("Skip text class %s\n", text);
970 evas_common_font_cache_set(cache);
973 return ECORE_CALLBACK_PASS_ON;
976 static void font_name_cb(keynode_t *node, void *user_data)
983 font = vconf_keynode_get_str(node);
987 DbgPrint("Font changed to %s\n", font);
990 static void font_size_cb(keynode_t *node, void *user_data)
998 DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
1001 PUBLIC int script_init(void)
1004 /* ecore is already initialized */
1007 s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
1008 if (!s_info.property_handler)
1009 ErrPrint("Failed to add a property change event handler\n");
1011 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
1013 ErrPrint("Failed to add vconf for font size change\n");
1015 ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
1017 ErrPrint("Failed to add vconf for font name change\n");
1022 PUBLIC int script_fini(void)
1025 vconf_ignore_key_changed("db/setting/accessibility/font_name", font_name_cb);
1026 vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb);
1027 ecore_event_handler_del(s_info.property_handler);
1028 s_info.property_handler = NULL;