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_image_load_size_set(img, w, h);
532 evas_object_image_load_region_set(img, (w - part_w) / 2, (h - part_h) / 2, part_w, part_h);
533 evas_object_image_fill_set(img, 0, 0, part_w, part_h);
534 evas_object_image_reload(img);
536 evas_object_image_fill_set(img, 0, 0, w, h);
537 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
538 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
541 if (img_opt.width >= 0 && img_opt.height >= 0) {
544 DbgPrint("Using given image size: %dx%d\n", w, h);
547 evas_object_image_fill_set(img, 0, 0, w, h);
548 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
549 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
554 * object will be shown by below statement automatically
556 DbgPrint("%s part swallow image %p (%dx%d)\n", part, img, w, h);
558 edje_object_part_swallow(edje, part, img);
559 obj_info->children = eina_list_append(obj_info->children, child);
564 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
566 struct info *handle = data;
578 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
579 edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
583 sx = (double)px / (double)w;
584 ex = (double)(px + pw) / (double)w;
589 sy = (double)py / (double)h;
590 ey = (double)(py + ph) / (double)h;
593 DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
594 script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
597 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
599 struct info *handle = _info;
600 struct obj_info *obj_info;
603 handle->obj_list = eina_list_remove(handle->obj_list, obj);
605 obj_info = evas_object_data_del(obj, "obj_info");
607 ErrPrint("Object info is not valid\n");
611 DbgPrint("delete object %s %p\n", obj_info->id, obj);
613 edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
615 EINA_LIST_FREE(obj_info->children, child) {
616 DbgPrint("delete object %s %p\n", child->part, child->obj);
618 evas_object_del(child->obj);
627 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)
629 struct info *handle = h;
632 struct obj_info *obj_info;
635 DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
637 edje = find_edje(handle, src_id);
639 ErrPrint("Edje is not exists\n");
643 obj_info = evas_object_data_get(edje, "obj_info");
645 ErrPrint("Object info is not valid\n");
649 obj = edje_object_part_swallow_get(edje, part);
654 edje_object_part_unswallow(edje, obj);
656 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
657 if (child->obj != obj)
660 obj_info->children = eina_list_remove(obj_info->children, child);
666 DbgPrint("delete object %s %p\n", part, obj);
667 evas_object_del(obj);
670 if (!path || !strlen(path) || access(path, R_OK) != 0) {
671 DbgPrint("SKIP - Path: [%s]\n", path);
675 obj = edje_object_add(e);
677 ErrPrint("Failed to add a new edje object\n");
681 if (!edje_object_file_set(obj, path, group)) {
685 err = edje_object_load_error_get(obj);
686 errmsg = edje_load_error_str(err);
687 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
688 evas_object_del(obj);
692 evas_object_show(obj);
694 obj_info = calloc(1, sizeof(*obj_info));
696 ErrPrint("Failed to add a obj_info\n");
697 evas_object_del(obj);
701 obj_info->id = strdup(target_id);
703 ErrPrint("Failed to add a obj_info\n");
705 evas_object_del(obj);
709 child = malloc(sizeof(*child));
711 ErrPrint("Error: %s\n", strerror(errno));
714 evas_object_del(obj);
718 child->part = strdup(part);
720 ErrPrint("Error: %s\n", strerror(errno));
724 evas_object_del(obj);
730 evas_object_data_set(obj, "obj_info", obj_info);
731 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
732 edje_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
733 handle->obj_list = eina_list_append(handle->obj_list, obj);
735 DbgPrint("%s part swallow edje %p\n", part, obj);
736 edje_object_part_swallow(edje, part, obj);
737 obj_info = evas_object_data_get(edje, "obj_info");
738 obj_info->children = eina_list_append(obj_info->children, child);
742 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
744 struct info *handle = h;
747 DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
749 edje = find_edje(handle, id);
753 edje_object_signal_emit(edje, signal, part);
757 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
759 struct info *handle = h;
762 DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
764 edje = find_edje(handle, id);
768 edje_object_part_drag_value_set(edje, part, x, y);
772 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
774 struct info *handle = han;
777 edje = find_edje(handle, id);
786 DbgPrint("Resize object to %dx%d\n", w, h);
787 evas_object_resize(edje, w, h);
791 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
793 struct info *handle = h;
795 DbgPrint("id[%s], category[%s]\n", id, category);
797 if (handle->category) {
798 free(handle->category);
799 handle->category = NULL;
805 handle->category = strdup(category);
806 if (!handle->category) {
807 ErrPrint("Error: %s\n", strerror(errno));
814 PUBLIC void *script_create(const char *file, const char *group)
818 DbgPrint("file[%s], group[%s]\n", file, group);
820 handle = calloc(1, sizeof(*handle));
822 ErrPrint("Error: %s\n", strerror(errno));
826 handle->file = strdup(file);
828 ErrPrint("Error: %s\n", strerror(errno));
833 handle->group = strdup(group);
834 if (!handle->group) {
835 ErrPrint("Error: %s\n", strerror(errno));
844 PUBLIC int script_destroy(void *_handle)
851 edje = eina_list_nth(handle->obj_list, 0);
853 evas_object_del(edje);
855 free(handle->category);
862 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
866 struct obj_info *obj_info;
870 obj_info = calloc(1, sizeof(*obj_info));
872 ErrPrint("Heap: %s\n", strerror(errno));
876 edje = edje_object_add(e);
878 ErrPrint("Failed to create an edje object\n");
883 DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
884 if (!edje_object_file_set(edje, handle->file, handle->group)) {
888 err = edje_object_load_error_get(edje);
889 errmsg = edje_load_error_str(err);
890 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
891 evas_object_del(edje);
900 edje_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
901 evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
902 evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
903 evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
904 evas_object_resize(edje, handle->w, handle->h);
905 evas_object_show(edje);
906 evas_object_data_set(edje, "obj_info", obj_info);
908 handle->obj_list = eina_list_append(handle->obj_list, edje);
912 PUBLIC int script_unload(void *_handle, Evas *e)
919 DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
920 edje = eina_list_nth(handle->obj_list, 0);
922 evas_object_del(edje);
927 static inline int update_font(const char *font)
933 cache = evas_common_font_cache_get();
934 evas_common_font_cache_set(0);
935 evas_common_font_flush();
937 list = edje_text_class_list();
938 EINA_LIST_FREE(list, text) {
939 if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
940 edje_text_class_del(text);
941 edje_text_class_set(text, s_info.font, s_info.size);
942 DbgPrint("Update text class %s (%s, %d)\n", text, s_info.font, s_info.size);
944 DbgPrint("Skip text class %s\n", text);
947 DbgPrint("Font for text_class is updated\n");
949 evas_common_font_cache_set(cache);
953 static Eina_Bool property_cb(void *data, int type, void *event)
955 Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
957 if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
960 font = vconf_get_str("db/setting/accessibility/font_name");
962 return ECORE_CALLBACK_PASS_ON;
964 if (s_info.font && !strcmp(s_info.font, font)) {
965 DbgPrint("Font is not changed (%s)\n", font);
967 return ECORE_CALLBACK_PASS_ON;
978 return ECORE_CALLBACK_PASS_ON;
981 static void font_name_cb(keynode_t *node, void *user_data)
988 font = vconf_keynode_get_str(node);
992 if (s_info.font && !strcmp(s_info.font, font)) {
993 DbgPrint("Font is not changed\n");
997 DbgPrint("Font changed to %s\n", font);
1002 s_info.font = strdup(font);
1004 ErrPrint("Heap: %s\n", strerror(errno));
1009 static void font_size_cb(keynode_t *node, void *user_data)
1017 DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
1020 PUBLIC int script_init(void)
1023 /* ecore is already initialized */
1025 edje_scale_set(scale_get());
1027 s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
1028 if (!s_info.property_handler)
1029 ErrPrint("Failed to add a property change event handler\n");
1031 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
1033 ErrPrint("Failed to add vconf for font size change\n");
1035 ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
1037 ErrPrint("Failed to add vconf for font name change\n");
1042 PUBLIC int script_fini(void)
1044 vconf_ignore_key_changed("db/setting/accessibility/font_name", font_name_cb);
1045 vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb);
1046 ecore_event_handler_del(s_info.property_handler);
1047 s_info.property_handler = NULL;