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 extern void evas_common_font_flush(void);
40 extern int evas_common_font_cache_get(void);
41 extern void evas_common_font_cache_set(int size);
71 Ecore_Event_Handler *property_handler;
75 .property_handler = NULL,
82 * Reservce this for future use
83 static inline void common_cache_flush(void *evas)
90 file_cache = edje_file_cache_get();
91 collection_cache = edje_collection_cache_get();
92 image_cache = evas_image_cache_get(evas);
93 font_cache = evas_font_cache_get(evas);
95 edje_file_cache_set(file_cache);
96 edje_collection_cache_set(collection_cache);
97 evas_image_cache_set(evas, 0);
98 evas_font_cache_set(evas, 0);
100 evas_image_cache_flush(evas);
101 evas_render_idle_flush(evas);
102 evas_font_cache_flush(evas);
104 edje_file_cache_flush();
105 edje_collection_cache_flush();
107 edje_file_cache_set(file_cache);
108 edje_collection_cache_set(collection_cache);
109 evas_image_cache_set(evas, image_cache);
110 evas_font_cache_set(evas, font_cache);
116 static inline Evas_Object *find_edje(struct info *handle, const char *id)
120 struct obj_info *obj_info;
122 EINA_LIST_FOREACH(handle->obj_list, l, edje) {
123 obj_info = evas_object_data_get(edje, "obj_info");
125 ErrPrint("Object info is not valid\n");
134 } else if (!obj_info->id) {
138 if (!strcmp(obj_info->id, id))
142 DbgPrint("EDJE[%s] is not found\n", id);
146 const char *script_magic_id(void)
151 int script_update_color(void *h, Evas *e, const char *id, const char *part, const char *rgba)
153 struct info *handle = h;
155 int r[3], g[3], b[3], a[3];
158 edje = find_edje(handle, id);
162 ret = sscanf(rgba, "%d %d %d %d %d %d %d %d %d %d %d %d",
163 r, g, b, a, /* OBJECT */
164 r + 1, g + 1, b + 1, a + 1, /* OUTLINE */
165 r + 2, g + 2, b + 2, a + 2); /* SHADOW */
167 DbgPrint("id[%s] part[%s] rgba[%s]\n", id, part, rgba);
171 ret = edje_object_color_class_set(edje, part,
172 r[0], g[0], b[0], a[0], /* OBJECT */
173 r[1], g[1], b[1], a[1], /* OUTLINE */
174 r[2], g[2], b[2], a[2]); /* SHADOW */
176 DbgPrint("EDJE[%s] color class is %s changed", id, ret == EINA_TRUE ? "successfully" : "not");
180 int script_update_text(void *h, Evas *e, const char *id, const char *part, const char *text)
182 struct info *handle = h;
185 edje = find_edje(handle, id);
189 edje_object_part_text_set(edje, part, text);
193 static void parse_aspect(struct image_option *img_opt, const char *value, int len)
195 while (len > 0 && *value == ' ') {
203 img_opt->aspect = !strncasecmp(value, "true", 4);
204 DbgPrint("Parsed ASPECT: %d\n", img_opt->aspect);
207 static void parse_orient(struct image_option *img_opt, const char *value, int len)
209 while (len > 0 && *value == ' ') {
217 img_opt->orient = !strncasecmp(value, "true", 4);
218 DbgPrint("Parsed ORIENT: %d\n", img_opt->aspect);
221 static inline void parse_image_option(const char *option, struct image_option *img_opt)
228 void (*handler)(struct image_option *img_opt, const char *value, int len);
232 .handler = parse_aspect,
236 .handler = parse_orient,
250 if (!option || !*option)
255 for (ptr = option; state != STATE_END; ptr++) {
270 cmd = cmd_list[tag].cmd;
276 } else if (*ptr == '\0') {
281 if (cmd[idx] == '\0' && (*ptr == ' ' || *ptr == '\t' || *ptr == '=')) {
286 state = STATE_IGNORE;
289 } else if (*ptr == '\0') {
291 } else if (cmd[idx] == *ptr) {
297 if (tag == sizeof(cmd_list) / sizeof(cmd_list[0])) {
301 cmd = cmd_list[tag].cmd;
307 if (*ptr == ';' || *ptr == '\0') {
308 cmd_list[tag].handler(img_opt, value + 1, idx);
309 state = *ptr ? STATE_START : STATE_END;
317 else if (*ptr == '\0')
326 int script_update_image(void *_h, Evas *e, const char *id, const char *part, const char *path, const char *option)
328 struct info *handle = _h;
333 struct obj_info *obj_info;
335 struct image_option img_opt = {
340 edje = find_edje(handle, id);
342 ErrPrint("No such object: %s\n", id);
346 obj_info = evas_object_data_get(edje, "obj_info");
348 ErrPrint("Object info is not available\n");
352 img = edje_object_part_swallow_get(edje, part);
357 edje_object_part_unswallow(edje, img);
359 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
360 if (child->obj != img)
363 obj_info->children = eina_list_remove(obj_info->children, child);
369 DbgPrint("delete object %s %p\n", part, img);
370 evas_object_del(img);
373 if (!path || !strlen(path) || access(path, R_OK) != 0) {
374 DbgPrint("SKIP - Path: [%s]\n", path);
378 child = malloc(sizeof(*child));
380 ErrPrint("Heap: %s\n", strerror(errno));
384 child->part = strdup(part);
386 ErrPrint("Heap: %s\n", strerror(errno));
391 img = evas_object_image_filled_add(e);
393 ErrPrint("Failed to add an image object\n");
399 parse_image_option(option, &img_opt);
400 evas_object_image_load_orientation_set(img, img_opt.orient);
404 evas_object_image_file_set(img, path, NULL);
406 err = evas_object_image_load_error_get(img);
407 if (err != EVAS_LOAD_ERROR_NONE) {
408 ErrPrint("Load error: %s\n", evas_load_error_str(err));
409 evas_object_del(img);
415 evas_object_image_size_get(img, &w, &h);
416 evas_object_image_fill_set(img, 0, 0, w, h);
417 evas_object_size_hint_fill_set(img, EVAS_HINT_FILL, EVAS_HINT_FILL);
419 evas_object_size_hint_aspect_set(img, EVAS_ASPECT_CONTROL_BOTH, w, h);
421 evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
422 evas_object_resize(img, w, h);
426 * object will be shown by below statement automatically
428 DbgPrint("%s part swallow image %p\n", part, img);
429 edje_object_part_swallow(edje, part, img);
430 obj_info->children = eina_list_append(obj_info->children, child);
435 static void script_signal_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
437 struct info *handle = data;
449 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
450 edje_object_part_geometry_get(obj, source, &px, &py, &pw, &ph);
454 sx = (double)px / (double)w;
455 ex = (double)(px + pw) / (double)w;
460 sy = (double)py / (double)h;
461 ey = (double)(py + ph) / (double)h;
464 DbgPrint("Signal emit: source[%s], emission[%s]\n", source, emission);
465 script_signal_emit(handle->e, source, emission, sx, sy, ex, ey);
468 static void edje_del_cb(void *_info, Evas *e, Evas_Object *obj, void *event_info)
470 struct info *handle = _info;
471 struct obj_info *obj_info;
474 handle->obj_list = eina_list_remove(handle->obj_list, obj);
476 obj_info = evas_object_data_del(obj, "obj_info");
478 ErrPrint("Object info is not valid\n");
482 DbgPrint("delete object %s %p\n", obj_info->id, obj);
484 edje_object_signal_callback_del_full(obj, "*", "*", script_signal_cb, handle);
486 EINA_LIST_FREE(obj_info->children, child) {
487 DbgPrint("delete object %s %p\n", child->part, child->obj);
489 evas_object_del(child->obj);
498 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)
500 struct info *handle = h;
503 struct obj_info *obj_info;
506 DbgPrint("src_id[%s] target_id[%s] part[%s] path[%s] group[%s]\n", src_id, target_id, part, path, group);
508 edje = find_edje(handle, src_id);
510 ErrPrint("Edje is not exists\n");
514 obj_info = evas_object_data_get(edje, "obj_info");
516 ErrPrint("Object info is not valid\n");
520 obj = edje_object_part_swallow_get(edje, part);
525 edje_object_part_unswallow(edje, obj);
527 EINA_LIST_FOREACH_SAFE(obj_info->children, l, n, child) {
528 if (child->obj != obj)
531 obj_info->children = eina_list_remove(obj_info->children, child);
537 DbgPrint("delete object %s %p\n", part, obj);
538 evas_object_del(obj);
541 if (!path || !strlen(path) || access(path, R_OK) != 0) {
542 DbgPrint("SKIP - Path: [%s]\n", path);
546 obj = edje_object_add(e);
548 ErrPrint("Failed to add a new edje object\n");
552 //edje_object_text_class_set(obj, TEXT_CLASS, s_info.font, s_info.size);
553 if (!edje_object_file_set(obj, path, group)) {
557 err = edje_object_load_error_get(obj);
558 errmsg = edje_load_error_str(err);
559 ErrPrint("Could not load %s from %s: %s\n", group, path, errmsg);
560 evas_object_del(obj);
564 evas_object_show(obj);
566 obj_info = calloc(1, sizeof(*obj_info));
568 ErrPrint("Failed to add a obj_info\n");
569 evas_object_del(obj);
573 obj_info->id = strdup(target_id);
575 ErrPrint("Failed to add a obj_info\n");
577 evas_object_del(obj);
581 child = malloc(sizeof(*child));
583 ErrPrint("Error: %s\n", strerror(errno));
586 evas_object_del(obj);
590 child->part = strdup(part);
592 ErrPrint("Error: %s\n", strerror(errno));
596 evas_object_del(obj);
602 evas_object_data_set(obj, "obj_info", obj_info);
603 evas_object_event_callback_add(obj, EVAS_CALLBACK_DEL, edje_del_cb, handle);
604 edje_object_signal_callback_add(obj, "*", "*", script_signal_cb, handle);
605 handle->obj_list = eina_list_append(handle->obj_list, obj);
607 DbgPrint("%s part swallow edje %p\n", part, obj);
608 edje_object_part_swallow(edje, part, obj);
609 obj_info = evas_object_data_get(edje, "obj_info");
610 obj_info->children = eina_list_append(obj_info->children, child);
614 int script_update_signal(void *h, Evas *e, const char *id, const char *part, const char *signal)
616 struct info *handle = h;
619 DbgPrint("id[%s], part[%s], signal[%s]\n", id, part, signal);
621 edje = find_edje(handle, id);
625 edje_object_signal_emit(edje, signal, part);
629 int script_update_drag(void *h, Evas *e, const char *id, const char *part, double x, double y)
631 struct info *handle = h;
634 DbgPrint("id[%s], part[%s], %lfx%lf\n", id, part, x, y);
636 edje = find_edje(handle, id);
640 edje_object_part_drag_value_set(edje, part, x, y);
644 int script_update_size(void *han, Evas *e, const char *id, int w, int h)
646 struct info *handle = han;
649 edje = find_edje(handle, id);
658 DbgPrint("Resize object to %dx%d\n", w, h);
659 evas_object_resize(edje, w, h);
663 int script_update_category(void *h, Evas *e, const char *id, const char *category)
665 struct info *handle = h;
667 DbgPrint("id[%s], category[%s]\n", id, category);
669 if (handle->category) {
670 free(handle->category);
671 handle->category = NULL;
677 handle->category = strdup(category);
678 if (!handle->category) {
679 ErrPrint("Error: %s\n", strerror(errno));
686 void *script_create(const char *file, const char *group)
690 DbgPrint("file[%s], group[%s]\n", file, group);
692 handle = calloc(1, sizeof(*handle));
694 ErrPrint("Error: %s\n", strerror(errno));
698 handle->file = strdup(file);
700 ErrPrint("Error: %s\n", strerror(errno));
705 handle->group = strdup(group);
706 if (!handle->group) {
707 ErrPrint("Error: %s\n", strerror(errno));
716 int script_destroy(void *_handle)
723 edje = eina_list_nth(handle->obj_list, 0);
725 evas_object_del(edje);
727 free(handle->category);
734 int script_load(void *_handle, Evas *e, int w, int h)
738 struct obj_info *obj_info;
742 obj_info = calloc(1, sizeof(*obj_info));
744 ErrPrint("Heap: %s\n", strerror(errno));
748 edje = edje_object_add(e);
750 ErrPrint("Failed to create an edje object\n");
755 //edje_object_text_class_set(edje, TEXT_CLASS, s_info.font, s_info.size);
756 DbgPrint("Load edje: %s - %s\n", handle->file, handle->group);
757 if (!edje_object_file_set(edje, handle->file, handle->group)) {
761 err = edje_object_load_error_get(edje);
762 errmsg = edje_load_error_str(err);
763 ErrPrint("Could not load %s from %s: %s\n", handle->group, handle->file, errmsg);
764 evas_object_del(edje);
773 edje_object_signal_callback_add(edje, "*", "*", script_signal_cb, handle);
774 evas_object_event_callback_add(edje, EVAS_CALLBACK_DEL, edje_del_cb, handle);
775 evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
776 evas_object_size_hint_fill_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
777 evas_object_resize(edje, handle->w, handle->h);
778 evas_object_show(edje);
779 evas_object_data_set(edje, "obj_info", obj_info);
781 handle->obj_list = eina_list_append(handle->obj_list, edje);
785 int script_unload(void *_handle, Evas *e)
792 DbgPrint("Unload edje: %s - %s\n", handle->file, handle->group);
793 edje = eina_list_nth(handle->obj_list, 0);
795 evas_object_del(edje);
800 static Eina_Bool property_cb(void *data, int type, void *event)
802 Ecore_X_Event_Window_Property *info = (Ecore_X_Event_Window_Property *)event;
804 if (info->atom == ecore_x_atom_get("FONT_TYPE_change") || info->atom == ecore_x_atom_get("BADA_FONT_change")) {
810 font = vconf_get_str("db/setting/accessibility/font_name");
812 return ECORE_CALLBACK_PASS_ON;
819 cache = evas_common_font_cache_get();
820 evas_common_font_cache_set(0);
821 evas_common_font_flush();
823 list = edje_text_class_list();
824 EINA_LIST_FREE(list, text) {
825 if (!strncasecmp(text, TEXT_CLASS, strlen(TEXT_CLASS))) {
826 edje_text_class_del(text);
827 edje_text_class_set(text, s_info.font, s_info.size);
828 DbgPrint("Update text class %s (%s, %d)\n", text, s_info.font, s_info.size);
830 DbgPrint("Skip text class %s\n", text);
834 evas_common_font_cache_set(cache);
837 return ECORE_CALLBACK_PASS_ON;
840 static void font_name_cb(keynode_t *node, void *user_data)
847 font = vconf_keynode_get_str(node);
851 DbgPrint("Font changed to %s\n", font);
854 static void font_size_cb(keynode_t *node, void *user_data)
862 DbgPrint("Size type: %d\n", vconf_keynode_get_int(node));
865 int script_init(void)
868 /* ecore is already initialized */
871 s_info.property_handler = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, property_cb, NULL);
872 if (!s_info.property_handler)
873 ErrPrint("Failed to add a property change event handler\n");
875 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb, NULL);
877 ErrPrint("Failed to add vconf for font size change\n");
879 ret = vconf_notify_key_changed("db/setting/accessibility/font_name", font_name_cb, NULL);
881 ErrPrint("Failed to add vconf for font name change\n");
886 int script_fini(void)
889 vconf_ignore_key_changed("db/setting/accessibility/font_name", font_name_cb);
890 vconf_ignore_key_changed(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, font_size_cb);
891 ecore_event_handler_del(s_info.property_handler);
892 s_info.property_handler = NULL;