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"
38 #define BASE_WIDTH 720.0f
40 #define PUBLIC __attribute__((visibility("default")))
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);
82 Ecore_Event_Handler *property_handler;
86 .property_handler = NULL,
91 static inline double scale_get(void)
95 ecore_x_window_size_get(0, &width, &height);
96 return (double)width / BASE_WIDTH;
101 * Reservce this for future use
102 static inline void common_cache_flush(void *evas)
105 int collection_cache;
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);
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);
119 evas_image_cache_flush(evas);
120 evas_render_idle_flush(evas);
121 evas_font_cache_flush(evas);
123 edje_file_cache_flush();
124 edje_collection_cache_flush();
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);
135 static inline Evas_Object *find_edje(struct info *handle, const char *id)
139 struct obj_info *obj_info;
141 EINA_LIST_FOREACH(handle->obj_list, l, edje) {
142 obj_info = evas_object_data_get(edje, "obj_info");
144 ErrPrint("Object info is not valid\n");
153 } else if (!obj_info->id) {
157 if (!strcmp(obj_info->id, id))
161 DbgPrint("EDJE[%s] is not found\n", id);
165 PUBLIC const char *script_magic_id(void)
170 PUBLIC int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
172 struct info *handle = h;
174 int r[3], g[3], b[3], a[3];
177 edje = find_edje(handle, id);
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 */
186 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
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 */
195 DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
199 PUBLIC int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
201 struct info *handle = h;
204 edje = find_edje(handle, id);
208 edje_object_part_text_set(edje, part, text);
212 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
214 while (len > 0 && *value == ' ') {
222 img_opt->aspect = !strncasecmp(value, "true", 4);
223 DbgPrint("Parsed ASPECT: %d (%s)\n", img_opt->aspect, value);
226 static void parse_orient(struct image_option *img_opt, const char *value, int len)
228 while (len > 0 && *value == ' ') {
236 img_opt->orient = !strncasecmp(value, "true", 4);
237 DbgPrint("Parsed ORIENT: %d (%s)\n", img_opt->aspect, value);
240 static void parse_size(struct image_option *img_opt, const char *value, int len)
246 while (len > 0 && *value == ' ') {
251 buf = strndup(value, len);
253 ErrPrint("Heap: %s\n", strerror(errno));
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);
262 DbgPrint("Invalid size tag[%s]\n", buf);
268 static void parse_fill(struct image_option *img_opt, const char *value, int len)
270 while (len > 0 && *value == ' ') {
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;
280 img_opt->fill = FILL_DISABLE;
282 DbgPrint("Parsed FILL: %d (%s)\n", img_opt->fill, value);
285 static inline void parse_image_option(const char *option, struct image_option *img_opt)
292 void (*handler)(struct image_option *img_opt, const char *value, int len);
295 .cmd = "aspect", /* Keep the aspect ratio */
296 .handler = parse_aspect,
299 .cmd = "orient", /* Keep the orientation value: for the rotated images */
300 .handler = parse_orient,
303 .cmd = "fill", /* Fill the image to its container */
304 .handler = parse_fill, /* Value: in-size, over-size, disable(default) */
308 .handler = parse_size,
322 if (!option || !*option)
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.
337 for (ptr = option; state != STATE_END; ptr++) {
352 cmd = cmd_list[tag].cmd;
358 } else if (*ptr == '\0') {
363 if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
368 state = STATE_IGNORE;
371 } else if (*ptr == '\0') {
373 } else if (cmd[idx] == *ptr) {
379 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
383 cmd = cmd_list[tag].cmd;
389 if (*ptr == ';' || *ptr == '\0') {
390 cmd_list[tag].handler(img_opt, value + 1, idx);
391 state = *ptr ? STATE_START : STATE_END;
399 else if (*ptr == '\0')
408 PUBLIC int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
410 struct info *handle = _h;
415 struct obj_info *obj_info;
417 struct image_option img_opt = {
420 .fill = FILL_DISABLE,
425 edje = find_edje(handle, id);
427 ErrPrint("No such object: %s\n", id);
431 obj_info = evas_object_data_get(edje, "obj_info");
433 ErrPrint("Object info is not available\n");
437 img = edje_object_part_swallow_get(edje, part);
442 edje_object_part_unswallow(edje, img);
444 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
445 if (child->obj != img)
448 obj_info->children = eina_list_remove(obj_info->children, child);
454 DbgPrint("delete object %s %p\n", part, img);
455 evas_object_del(img);
458 if (!path || !strlen(path) || access(path, R_OK) != 0) {
459 DbgPrint("SKIP - Path: [%s]\n", path);
463 child = malloc(sizeof(*child));
465 ErrPrint("Heap: %s\n", strerror(errno));
469 child->part = strdup(part);
471 ErrPrint("Heap: %s\n", strerror(errno));
476 img = evas_object_image_add(e);
478 ErrPrint("Failed to add an image object\n");
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);
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);
498 evas_object_image_size_get(img, &w, &h);
499 if (img_opt.aspect) {
500 if (img_opt.fill == FILL_OVER_SIZE) {
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();
510 edje_object_part_geometry_get(edje, part, NULL, NULL, &part_w, &part_h);
512 DbgPrint("Original %dx%d (part: %dx%d)\n", w, h, part_w, part_h);
514 if (part_w > w || part_h > h) {
518 fw = (double)part_w / (double)w;
519 fh = (double)part_h / (double)h;
529 DbgPrint("Size: %dx%d\n", w, h);
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);
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);
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);
546 if (img_opt.width >= 0 && img_opt.height >= 0) {
549 DbgPrint("Using given image size: %dx%d\n", w, h);
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);
559 * object will be shown by below statement automatically
561 DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
563 edje_object_part_swallow(edje, part, img);
564 obj_info->children = eina_list_append(obj_info->children, child);
569 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
571 struct info *handle = data;
583 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
584 edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
588 sx = (double)px / (double)w;
589 ex = (double)(px + pw) / (double)w;
594 sy = (double)py / (double)h;
595 ey = (double)(py + ph) / (double)h;
598 DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
599 script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
602 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
604 struct info *handle = _info;
605 struct obj_info *obj_info;
608 handle->obj_list = eina_list_remove(handle->obj_list, obj);
610 obj_info = evas_object_data_del(obj, "obj_info");
612 ErrPrint("Object info is not valid\n");
616 DbgPrint("delete object %s %p\n", obj_info->id, obj);
618 edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
620 EINA_LIST_FREE(obj_info->children, child) {
621 DbgPrint("delete object %s %p\n", child->part, child->obj);
623 evas_object_del(child->obj);
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)
634 struct info *handle = h;
637 struct obj_info *obj_info;
640 DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
642 edje = find_edje(handle, src_id);
644 ErrPrint("Edje is not exists\n");
648 obj_info = evas_object_data_get(edje, "obj_info");
650 ErrPrint("Object info is not valid\n");
654 obj = edje_object_part_swallow_get(edje, part);
659 edje_object_part_unswallow(edje, obj);
661 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
662 if (child->obj != obj)
665 obj_info->children = eina_list_remove(obj_info->children, child);
671 DbgPrint("delete object %s %p\n", part, obj);
672 evas_object_del(obj);
675 if (!path || !strlen(path) || access(path, R_OK) != 0) {
676 DbgPrint("SKIP - Path: [%s]\n", path);
680 obj = edje_object_add(e);
682 ErrPrint("Failed to add a new edje object\n");
686 //edje_object_text_class_set(obj, TEXT_CLASS, s_info.font, s_info.size);
687 if (!edje_object_file_set(obj, path, group)) {
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);
698 evas_object_show(obj);
700 obj_info = calloc(1, sizeof(*obj_info));
702 ErrPrint("Failed to add a obj_info\n");
703 evas_object_del(obj);
707 obj_info->id = strdup(target_id);
709 ErrPrint("Failed to add a obj_info\n");
711 evas_object_del(obj);
715 child = malloc(sizeof(*child));
717 ErrPrint("Error: %s\n", strerror(errno));
720 evas_object_del(obj);
724 child->part = strdup(part);
726 ErrPrint("Error: %s\n", strerror(errno));
730 evas_object_del(obj);
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);
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);
748 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
750 struct info *handle = h;
753 DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
755 edje = find_edje(handle, id);
759 edje_object_signal_emit(edje, signal, part);
763 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
765 struct info *handle = h;
768 DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
770 edje = find_edje(handle, id);
774 edje_object_part_drag_value_set(edje, part, x, y);
778 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
780 struct info *handle = han;
783 edje = find_edje(handle, id);
792 DbgPrint("Resize object to %dx%d\n", w, h);
793 evas_object_resize(edje, w, h);
797 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
799 struct info *handle = h;
801 DbgPrint("id[%s], category[%s]\n", id, category);
803 if (handle->category) {
804 free(handle->category);
805 handle->category = NULL;
811 handle->category = strdup(category);
812 if (!handle->category) {
813 ErrPrint("Error: %s\n", strerror(errno));
820 PUBLIC void *script_create(const char *file, const char *group)
824 DbgPrint("file[%s], group[%s]\n", file, group);
826 handle = calloc(1, sizeof(*handle));
828 ErrPrint("Error: %s\n", strerror(errno));
832 handle->file = strdup(file);
834 ErrPrint("Error: %s\n", strerror(errno));
839 handle->group = strdup(group);
840 if (!handle->group) {
841 ErrPrint("Error: %s\n", strerror(errno));
850 PUBLIC int script_destroy(void *_handle)
857 edje = eina_list_nth(handle->obj_list, 0);
859 evas_object_del(edje);
861 free(handle->category);
868 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
872 struct obj_info *obj_info;
876 obj_info = calloc(1, sizeof(*obj_info));
878 ErrPrint("Heap: %s\n", strerror(errno));
882 edje = edje_object_add(e);
884 ErrPrint("Failed to create an edje object\n");
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)) {
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);
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);
915 handle->obj_list = eina_list_append(handle->obj_list, edje);
919 PUBLIC int script_unload(void *_handle, Evas *e)
926 DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
927 edje = eina_list_nth(handle->obj_list, 0);
929 evas_object_del(edje);
934 static Eina_Bool property_cb(void *data, int type, void *event)
936 Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
938 if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
944 font = vconf_get_str("db/setting/accessibility/font_name");
946 return ECORE_CALLBACK_PASS_ON;
953 cache = evas_common_font_cache_get();
954 evas_common_font_cache_set(0);
955 evas_common_font_flush();
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);
964 DbgPrint("Skip text class %s\n", text);
968 evas_common_font_cache_set(cache);
971 return ECORE_CALLBACK_PASS_ON;
974 static void font_name_cb(keynode_t *node, void *user_data)
981 font = vconf_keynode_get_str(node);
985 DbgPrint("Font changed to %s\n", font);
988 static void font_size_cb(keynode_t *node, void *user_data)
996 DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
999 PUBLIC int script_init(void)
1002 /* ecore is already initialized */
1004 edje_scale_set(scale_get());
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");
1010 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
1012 ErrPrint("Failed to add vconf for font size change\n");
1014 ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
1016 ErrPrint("Failed to add vconf for font name change\n");
1021 PUBLIC int script_fini(void)
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;