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);
73 Ecore_Event_Handler *property_handler;
77 .property_handler = NULL,
84 * Reservce this for future use
85 static inline void common_cache_flush(void *evas)
92 file_cache = edje_file_cache_get();
93 collection_cache = edje_collection_cache_get();
94 image_cache = evas_image_cache_get(evas);
95 font_cache = evas_font_cache_get(evas);
97 edje_file_cache_set(file_cache);
98 edje_collection_cache_set(collection_cache);
99 evas_image_cache_set(evas, 0);
100 evas_font_cache_set(evas, 0);
102 evas_image_cache_flush(evas);
103 evas_render_idle_flush(evas);
104 evas_font_cache_flush(evas);
106 edje_file_cache_flush();
107 edje_collection_cache_flush();
109 edje_file_cache_set(file_cache);
110 edje_collection_cache_set(collection_cache);
111 evas_image_cache_set(evas, image_cache);
112 evas_font_cache_set(evas, font_cache);
118 static inline Evas_Object *find_edje(struct info *handle, const char *id)
122 struct obj_info *obj_info;
124 EINA_LIST_FOREACH(handle->obj_list, l, edje) {
125 obj_info = evas_object_data_get(edje, "obj_info");
127 ErrPrint("Object info is not valid\n");
136 } else if (!obj_info->id) {
140 if (!strcmp(obj_info->id, id))
144 DbgPrint("EDJE[%s] is not found\n", id);
148 PUBLIC const char *script_magic_id(void)
153 PUBLIC int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
155 struct info *handle = h;
157 int r[3], g[3], b[3], a[3];
160 edje = find_edje(handle, id);
164 ret = sscanf(rgba, "%d %d %d %d %d %d %d %d %d %d %d %d",
165 r, g, b, a, /* OBJECT */
166 r + 1, g + 1, b + 1, a + 1, /* OUTLINE */
167 r + 2, g + 2, b + 2, a + 2); /* SHADOW */
169 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
173 ret = edje_object_color_class_set(edje, part,
174 r[0], g[0], b[0], a[0], /* OBJECT */
175 r[1], g[1], b[1], a[1], /* OUTLINE */
176 r[2], g[2], b[2], a[2]); /* SHADOW */
178 DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
182 PUBLIC int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
184 struct info *handle = h;
187 edje = find_edje(handle, id);
191 edje_object_part_text_set(edje, part, text);
195 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
197 while (len > 0 && *value == ' ') {
205 img_opt->aspect = !strncasecmp(value, "true", 4);
206 DbgPrint("Parsed ASPECT: %d\n", img_opt->aspect);
209 static void parse_orient(struct image_option *img_opt, const char *value, int len)
211 while (len > 0 && *value == ' ') {
219 img_opt->orient = !strncasecmp(value, "true", 4);
220 DbgPrint("Parsed ORIENT: %d\n", img_opt->aspect);
223 static inline void parse_image_option(const char *option, struct image_option *img_opt)
230 void (*handler)(struct image_option *img_opt, const char *value, int len);
234 .handler = parse_aspect,
238 .handler = parse_orient,
252 if (!option || !*option)
257 for (ptr = option; state != STATE_END; ptr++) {
272 cmd = cmd_list[tag].cmd;
278 } else if (*ptr == '\0') {
283 if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
288 state = STATE_IGNORE;
291 } else if (*ptr == '\0') {
293 } else if (cmd[idx] == *ptr) {
299 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
303 cmd = cmd_list[tag].cmd;
309 if (*ptr == ';' || *ptr == '\0') {
310 cmd_list[tag].handler(img_opt, value + 1, idx);
311 state = *ptr ? STATE_START : STATE_END;
319 else if (*ptr == '\0')
328 PUBLIC int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
330 struct info *handle = _h;
335 struct obj_info *obj_info;
337 struct image_option img_opt = {
342 edje = find_edje(handle, id);
344 ErrPrint("No such object: %s\n", id);
348 obj_info = evas_object_data_get(edje, "obj_info");
350 ErrPrint("Object info is not available\n");
354 img = edje_object_part_swallow_get(edje, part);
359 edje_object_part_unswallow(edje, img);
361 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
362 if (child->obj != img)
365 obj_info->children = eina_list_remove(obj_info->children, child);
371 DbgPrint("delete object %s %p\n", part, img);
372 evas_object_del(img);
375 if (!path || !strlen(path) || access(path, R_OK) != 0) {
376 DbgPrint("SKIP - Path: [%s]\n", path);
380 child = malloc(sizeof(*child));
382 ErrPrint("Heap: %s\n", strerror(errno));
386 child->part = strdup(part);
388 ErrPrint("Heap: %s\n", strerror(errno));
393 img = evas_object_image_filled_add(e);
395 ErrPrint("Failed to add an image object\n");
401 parse_image_option(option, &img_opt);
402 evas_object_image_load_orientation_set(img, img_opt.orient);
406 evas_object_image_file_set(img, path, NULL);
408 err = evas_object_image_load_error_get(img);
409 if (err != EVAS_LOAD_ERROR_NONE) {
410 ErrPrint("Load error: %s\n", evas_load_error_str(err));
411 evas_object_del(img);
417 evas_object_image_size_get(img, &w, &h);
418 evas_object_image_fill_set(img, 0, 0, w, h);
419 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
421 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
423 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
424 evas_object_resize(img, w, h);
428 * object will be shown by below statement automatically
430 DbgPrint("%s part swallow image %p\n", part, img);
431 edje_object_part_swallow(edje, part, img);
432 obj_info->children = eina_list_append(obj_info->children, child);
437 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
439 struct info *handle = data;
451 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
452 edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
456 sx = (double)px / (double)w;
457 ex = (double)(px + pw) / (double)w;
462 sy = (double)py / (double)h;
463 ey = (double)(py + ph) / (double)h;
466 DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
467 script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
470 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
472 struct info *handle = _info;
473 struct obj_info *obj_info;
476 handle->obj_list = eina_list_remove(handle->obj_list, obj);
478 obj_info = evas_object_data_del(obj, "obj_info");
480 ErrPrint("Object info is not valid\n");
484 DbgPrint("delete object %s %p\n", obj_info->id, obj);
486 edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
488 EINA_LIST_FREE(obj_info->children, child) {
489 DbgPrint("delete object %s %p\n", child->part, child->obj);
491 evas_object_del(child->obj);
500 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)
502 struct info *handle = h;
505 struct obj_info *obj_info;
508 DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
510 edje = find_edje(handle, src_id);
512 ErrPrint("Edje is not exists\n");
516 obj_info = evas_object_data_get(edje, "obj_info");
518 ErrPrint("Object info is not valid\n");
522 obj = edje_object_part_swallow_get(edje, part);
527 edje_object_part_unswallow(edje, obj);
529 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
530 if (child->obj != obj)
533 obj_info->children = eina_list_remove(obj_info->children, child);
539 DbgPrint("delete object %s %p\n", part, obj);
540 evas_object_del(obj);
543 if (!path || !strlen(path) || access(path, R_OK) != 0) {
544 DbgPrint("SKIP - Path: [%s]\n", path);
548 obj = edje_object_add(e);
550 ErrPrint("Failed to add a new edje object\n");
554 //edje_object_text_class_set(obj, TEXT_CLASS, s_info.font, s_info.size);
555 if (!edje_object_file_set(obj, path, group)) {
559 err = edje_object_load_error_get(obj);
560 errmsg = edje_load_error_str(err);
561 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
562 evas_object_del(obj);
566 evas_object_show(obj);
568 obj_info = calloc(1, sizeof(*obj_info));
570 ErrPrint("Failed to add a obj_info\n");
571 evas_object_del(obj);
575 obj_info->id = strdup(target_id);
577 ErrPrint("Failed to add a obj_info\n");
579 evas_object_del(obj);
583 child = malloc(sizeof(*child));
585 ErrPrint("Error: %s\n", strerror(errno));
588 evas_object_del(obj);
592 child->part = strdup(part);
594 ErrPrint("Error: %s\n", strerror(errno));
598 evas_object_del(obj);
604 evas_object_data_set(obj, "obj_info", obj_info);
605 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
606 edje_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
607 handle->obj_list = eina_list_append(handle->obj_list, obj);
609 DbgPrint("%s part swallow edje %p\n", part, obj);
610 edje_object_part_swallow(edje, part, obj);
611 obj_info = evas_object_data_get(edje, "obj_info");
612 obj_info->children = eina_list_append(obj_info->children, child);
616 PUBLIC int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
618 struct info *handle = h;
621 DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
623 edje = find_edje(handle, id);
627 edje_object_signal_emit(edje, signal, part);
631 PUBLIC int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
633 struct info *handle = h;
636 DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
638 edje = find_edje(handle, id);
642 edje_object_part_drag_value_set(edje, part, x, y);
646 PUBLIC int script_update_size(void *han, Evas *e, const char *id, int w, int h)
648 struct info *handle = han;
651 edje = find_edje(handle, id);
660 DbgPrint("Resize object to %dx%d\n", w, h);
661 evas_object_resize(edje, w, h);
665 PUBLIC int script_update_category(void *h, Evas *e, const char *id, const char *category)
667 struct info *handle = h;
669 DbgPrint("id[%s], category[%s]\n", id, category);
671 if (handle->category) {
672 free(handle->category);
673 handle->category = NULL;
679 handle->category = strdup(category);
680 if (!handle->category) {
681 ErrPrint("Error: %s\n", strerror(errno));
688 PUBLIC void *script_create(const char *file, const char *group)
692 DbgPrint("file[%s], group[%s]\n", file, group);
694 handle = calloc(1, sizeof(*handle));
696 ErrPrint("Error: %s\n", strerror(errno));
700 handle->file = strdup(file);
702 ErrPrint("Error: %s\n", strerror(errno));
707 handle->group = strdup(group);
708 if (!handle->group) {
709 ErrPrint("Error: %s\n", strerror(errno));
718 PUBLIC int script_destroy(void *_handle)
725 edje = eina_list_nth(handle->obj_list, 0);
727 evas_object_del(edje);
729 free(handle->category);
736 PUBLIC int script_load(void *_handle, Evas *e, int w, int h)
740 struct obj_info *obj_info;
744 obj_info = calloc(1, sizeof(*obj_info));
746 ErrPrint("Heap: %s\n", strerror(errno));
750 edje = edje_object_add(e);
752 ErrPrint("Failed to create an edje object\n");
757 //edje_object_text_class_set(edje, TEXT_CLASS, s_info.font, s_info.size);
758 DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
759 if (!edje_object_file_set(edje, handle->file, handle->group)) {
763 err = edje_object_load_error_get(edje);
764 errmsg = edje_load_error_str(err);
765 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
766 evas_object_del(edje);
775 edje_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
776 evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
777 evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
778 evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
779 evas_object_resize(edje, handle->w, handle->h);
780 evas_object_show(edje);
781 evas_object_data_set(edje, "obj_info", obj_info);
783 handle->obj_list = eina_list_append(handle->obj_list, edje);
787 PUBLIC int script_unload(void *_handle, Evas *e)
794 DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
795 edje = eina_list_nth(handle->obj_list, 0);
797 evas_object_del(edje);
802 static Eina_Bool property_cb(void *data, int type, void *event)
804 Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
806 if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
812 font = vconf_get_str("db/setting/accessibility/font_name");
814 return ECORE_CALLBACK_PASS_ON;
821 cache = evas_common_font_cache_get();
822 evas_common_font_cache_set(0);
823 evas_common_font_flush();
825 list = edje_text_class_list();
826 EINA_LIST_FREE(list, text) {
827 if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
828 edje_text_class_del(text);
829 edje_text_class_set(text, s_info.font, s_info.size);
830 DbgPrint("Update text class %s (%s, %d)\n", text, s_info.font, s_info.size);
832 DbgPrint("Skip text class %s\n", text);
836 evas_common_font_cache_set(cache);
839 return ECORE_CALLBACK_PASS_ON;
842 static void font_name_cb(keynode_t *node, void *user_data)
849 font = vconf_keynode_get_str(node);
853 DbgPrint("Font changed to %s\n", font);
856 static void font_size_cb(keynode_t *node, void *user_data)
864 DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
867 PUBLIC int script_init(void)
870 /* ecore is already initialized */
873 s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
874 if (!s_info.property_handler)
875 ErrPrint("Failed to add a property change event handler\n");
877 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
879 ErrPrint("Failed to add vconf for font size change\n");
881 ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
883 ErrPrint("Failed to add vconf for font name change\n");
888 PUBLIC int script_fini(void)
891 vconf_ignore_key_changed("db/setting/accessibility/font_name", font_name_cb);
892 vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb);
893 ecore_event_handler_del(s_info.property_handler);
894 s_info.property_handler = NULL;