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.
17 #include <Elementary.h>
23 #include <livebox-service.h>
31 #define FLICK_COND 100
33 static Evas_Object *create_canvas(Evas_Object *parent)
37 canvas = evas_object_image_filled_add(evas_object_evas_get(parent));
41 evas_object_image_colorspace_set(canvas, EVAS_COLORSPACE_ARGB8888);
42 evas_object_image_alpha_set(canvas, EINA_TRUE);
43 evas_object_move(canvas, 0, 0);
47 static int update_pd_canvas(struct livebox *handle, Evas_Object *image)
52 switch (livebox_pd_type(handle)) {
55 evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
56 evas_object_image_alpha_set(image, EINA_TRUE);
58 livebox_get_pdsize(handle, &w, &h);
61 data = livebox_acquire_pdfb(handle);
63 evas_object_image_size_set(image, w, h);
64 evas_object_image_data_copy_set(image, data);
65 livebox_release_pdfb(data);
67 evas_object_resize(image, w, h);
68 //evas_object_size_hint_min_set(image, w, h);
69 evas_object_size_hint_max_set(image, w, h);
80 static int update_canvas(struct livebox *handle, Evas_Object *image)
87 switch (livebox_lb_type(handle)) {
90 evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
91 evas_object_image_alpha_set(image, EINA_TRUE);
94 type = livebox_size(handle);
95 livebox_service_get_size(type, &w, &h);
98 data = livebox_acquire_fb(handle);
100 evas_object_image_size_set(image, w, h);
101 evas_object_image_data_copy_set(image, data);
102 livebox_release_fb(data);
104 evas_object_resize(image, w, h);
105 evas_object_size_hint_min_set(image, w, h);
106 evas_object_size_hint_max_set(image, w, h);
110 filename = livebox_filename(handle);
113 evas_object_image_file_get(image, &old, NULL);
114 if (old && !strcmp(filename, old)) {
115 evas_object_image_reload(image);
117 evas_object_image_file_set(image, filename, NULL);
121 type = livebox_size(handle);
122 livebox_service_get_size(type, &w, &h);
123 if (w > 0 && h > 0) {
124 evas_object_resize(image, w, h);
125 evas_object_size_hint_min_set(image, w, h);
126 evas_object_size_hint_max_set(image, w, h);
138 static inline void prepend_log(struct livebox *handle, const char *buffer)
143 layout = livebox_get_data(handle);
145 ErrPrint("Failed to get layout\n");
149 logger = elm_object_part_content_get(layout, "logger");
151 elm_list_item_prepend(logger, buffer, NULL, NULL, NULL, NULL);
154 static int lb_event_cb(struct livebox *handle, enum livebox_event_type event, void *data)
161 layout = (Evas_Object *)livebox_get_data(handle);
166 case LB_EVENT_LB_UPDATED:
167 DbgPrint("Contents: [%s]\n", livebox_content(handle));
168 image = elm_object_part_content_get(layout, "livebox");
170 update_canvas(handle, image);
172 case LB_EVENT_PD_UPDATED:
173 image = elm_object_part_content_get(layout, "pd");
175 update_pd_canvas(handle, image);
177 case LB_EVENT_DELETED:
178 sc = evas_object_data_del(layout, "sc");
180 scroller_peek_by_obj(sc, layout);
182 evas_object_del(layout);
184 case LB_EVENT_PD_DESTROYED:
185 pd = elm_object_part_content_unset(layout, "pd");
189 sc = evas_object_data_del(layout, "sc");
200 static int lb_fault_cb(enum livebox_fault_type event,
201 const char *pkgname, const char *filename,
202 const char *funcname, void *data)
204 DbgPrint("pkgname: %s\nfilename: %s\n:funcname: %s\n", pkgname, filename, funcname);
208 static void del_cb(struct livebox *handle, int ret, void *data)
210 Evas_Object *layout = data;
213 sc = evas_object_data_del(layout, "sc");
215 DbgPrint("Scroller: %p\n", sc);
216 scroller_peek_by_obj(sc, layout);
219 DbgPrint("Delete a layout object\n");
220 evas_object_del(layout);
223 static void delete_btn_cb(void *handle, Evas_Object *obj, void *event_info)
227 layout = livebox_get_data(handle);
228 DbgPrint("Livebox Get Data %p - %p\n", handle, layout);
229 ret = livebox_del(handle, del_cb, layout);
232 snprintf(buffer, sizeof(buffer), "Delete returns: %d\n", ret);
233 prepend_log(handle, buffer);
237 static void exit_cb(void *data, Evas_Object *obj, void *event_info)
239 evas_object_del(obj);
242 static void error_popup(Evas_Object *parent, struct livebox *handle, int ret)
248 popup = elm_popup_add(parent);
252 button = elm_button_add(parent);
254 evas_object_del(popup);
258 elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
259 elm_object_part_text_set(popup, "title,text", "Unable to load a livebox");
261 elm_object_text_set(button, "Okay");
262 elm_object_part_content_set(popup, "button2", button);
263 evas_object_smart_callback_add(button, "clicked", exit_cb, popup);
265 snprintf(buffer, sizeof(buffer) - 1,
266 "%s(%s): %d", livebox_pkgname(handle), livebox_content(handle), ret);
267 elm_object_part_text_set(popup, "default", buffer);
268 evas_object_show(popup);
273 static void resize_cb(struct livebox *handle, int ret, void *data)
276 Evas_Object *log_list;
279 layout = livebox_get_data(handle);
283 log_list = elm_object_part_content_get(layout, "logger");
287 snprintf(buffer, sizeof(buffer) - 1, "Resize: %d", ret);
288 elm_list_item_prepend(log_list, buffer, NULL, NULL, NULL, NULL);
291 static void resize_click_cb(void *handle, Evas_Object *obj, void *event_info)
293 Elm_Object_Item *item;
299 item = elm_list_selected_item_get(obj);
303 label = elm_object_item_part_text_get(item, NULL);
307 sscanf(label, "%dx%d", &w, &h);
308 size_type = livebox_service_size_type(w, h);
310 livebox_resize(handle, size_type, resize_cb, NULL);
313 static void create_resize_controller(struct livebox *handle, Evas_Object *layout)
315 Evas_Object *size_list;
323 size_list = elm_list_add(layout);
325 livebox_get_supported_sizes(handle, &cnt, sizes);
326 for (i = 0; i < cnt; i++) {
327 livebox_service_get_size(sizes[i], &w, &h);
328 snprintf(buffer, sizeof(buffer) - 1, "%dx%d", w, h);
330 elm_list_item_append(size_list, buffer, NULL, NULL, resize_click_cb, handle);
332 evas_object_show(size_list);
333 elm_list_go(size_list);
334 evas_object_size_hint_weight_set(size_list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
335 evas_object_size_hint_align_set(size_list, EVAS_HINT_FILL, EVAS_HINT_FILL);
336 elm_object_part_content_set(layout, "controller", size_list);
339 static void create_logger(struct livebox *handle, Evas_Object *layout)
341 Evas_Object *log_list;
343 log_list = elm_list_add(layout);
347 elm_list_item_prepend(log_list, "Created", NULL, NULL, NULL, NULL);
348 evas_object_show(log_list);
349 elm_list_go(log_list);
351 evas_object_size_hint_weight_set(log_list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
352 evas_object_size_hint_align_set(log_list, EVAS_HINT_FILL, EVAS_HINT_FILL);
353 elm_object_part_content_set(layout, "logger", log_list);
361 FLICK_UNKNOWN = 0x00,
367 static void pd_closed_cb(struct livebox *handle, int ret, void *data)
369 evas_object_del(data);
372 static void pd_hide_done_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
377 sc = evas_object_data_get(obj, "sc");
380 elm_object_signal_callback_del(obj, emission, source, pd_hide_done_cb);
381 pd = elm_object_part_content_unset(obj, "pd");
382 livebox_destroy_pd(data, pd_closed_cb, pd);
385 static void pd_mouse_up_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
387 Evas_Event_Mouse_Up *up = event_info;
388 Evas_Coord x, y, w, h;
391 evas_object_geometry_get(obj, &x, &y, &w, &h);
393 rx = (double)(up->canvas.x - x) / (double)w;
394 ry = (double)(up->canvas.y - y) / (double)h;
395 livebox_content_event(handle, PD_MOUSE_UP, rx, ry);
398 static void pd_mouse_down_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
400 Evas_Event_Mouse_Down *down = event_info;
401 Evas_Coord x, y, w, h;
404 evas_object_geometry_get(obj, &x, &y, &w, &h);
405 rx = (double)(down->canvas.x - x) / (double)w;
406 ry = (double)(down->canvas.y - y) / (double)h;
407 livebox_content_event(handle, PD_MOUSE_DOWN, rx, ry);
410 static void pd_mouse_move_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
412 Evas_Event_Mouse_Move *move = event_info;
413 Evas_Coord x, y, w, h;
416 evas_object_geometry_get(obj, &x, &y, &w, &h);
417 rx = (double)(move->cur.canvas.x - x) / (double)w;
418 ry = (double)(move->cur.canvas.y - y) / (double)h;
419 livebox_content_event(handle, PD_MOUSE_MOVE, rx, ry);
422 static void pd_created_cb(struct livebox *handle, int ret, void *data)
425 Evas_Object *pd_image;
428 layout = (Evas_Object *)livebox_get_data(handle);
432 sc = evas_object_data_get(layout, "sc");
434 pd_image = create_canvas(layout);
438 evas_object_event_callback_add(pd_image, EVAS_CALLBACK_MOUSE_UP, pd_mouse_up_cb, handle);
439 evas_object_event_callback_add(pd_image, EVAS_CALLBACK_MOUSE_DOWN, pd_mouse_down_cb, handle);
440 evas_object_event_callback_add(pd_image, EVAS_CALLBACK_MOUSE_MOVE, pd_mouse_move_cb, handle);
442 update_pd_canvas(handle, pd_image);
444 elm_object_signal_callback_add(layout, "hide,done", "pd", pd_hide_done_cb, handle);
445 elm_object_part_content_set(layout, "pd", pd_image);
446 elm_object_signal_emit(layout, "open", "pd");
450 static void lb_mouse_up_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
452 Evas_Event_Mouse_Up *up = event_info;
453 struct event_data *evt;
455 evt = evas_object_data_del(obj, "evt");
459 if (livebox_lb_type(handle) == LB_TYPE_PIXMAP || livebox_lb_type(handle) == LB_TYPE_BUFFER) {
460 Evas_Coord x, y, w, h;
463 evas_object_geometry_get(obj, &x, &y, &w, &h);
464 rx = (double)(up->canvas.x - x) / (double)w;
465 ry = (double)(up->canvas.y - y) / (double)h;
466 livebox_content_event(handle, LB_MOUSE_UP, rx, ry);
468 Evas_Coord x, y, w, h;
469 evas_object_geometry_get(obj, &x, &y, &w, &h);
471 if (x < up->canvas.x && up->canvas.x < x + w) {
472 if (y < up->canvas.y && up->canvas.y < y + h) {
473 livebox_click(handle, (double)x / (double)w, (double)y / (double)h);
478 if (evt->flick == FLICK_DOWN && (up->canvas.y - evt->y) > (FLICK_COND>>1)) {
481 ret = livebox_create_pd_with_position(handle, 0.5, 0.0, pd_created_cb, NULL);
487 static void lb_mouse_down_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
489 struct event_data *evt;
490 Evas_Event_Mouse_Down *down = event_info;
494 layout = livebox_get_data(handle);
498 sc = evas_object_data_get(layout, "sc");
502 if (scroller_is_scrolling(sc))
505 if (livebox_lb_type(handle) == LB_TYPE_PIXMAP || livebox_lb_type(handle) == LB_TYPE_BUFFER) {
506 Evas_Coord x, y, w, h;
509 evas_object_geometry_get(obj, &x, &y, &w, &h);
510 rx = (double)(down->canvas.x - x) / (double)w;
511 ry = (double)(down->canvas.y - y) / (double)h;
512 livebox_content_event(handle, LB_MOUSE_DOWN, rx, ry);
515 evt = evas_object_data_get(obj, "evt");
519 evt = malloc(sizeof(*evt));
521 ErrPrint("Heap: %s\n", strerror(errno));
526 evas_object_data_set(obj, "evt", evt);
528 evt->x = down->canvas.x;
529 evt->y = down->canvas.y;
530 evt->flick = FLICK_UNKNOWN;
533 static void lb_mouse_move_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
535 Evas_Event_Mouse_Move *move = event_info;
536 struct event_data *evt;
538 evt = evas_object_data_get(obj, "evt");
542 if (livebox_lb_type(handle) == LB_TYPE_PIXMAP || livebox_lb_type(handle) == LB_TYPE_BUFFER) {
543 Evas_Coord x, y, w, h;
546 evas_object_geometry_get(obj, &x, &y, &w, &h);
547 rx = (double)(move->cur.canvas.x - x) / (double)w;
548 ry = (double)(move->cur.canvas.y - y) / (double)h;
549 livebox_content_event(handle, LB_MOUSE_MOVE, rx, ry);
552 if ((move->cur.canvas.x - move->prev.canvas.x) > FLICK_COND) {
553 evt->flick = FLICK_UNKNOWN;
555 } else if ((move->cur.canvas.x - move->prev.canvas.x) < -FLICK_COND) {
556 evt->flick = FLICK_UNKNOWN;
560 if ((move->cur.canvas.y - move->prev.canvas.y) > 0) {
561 if (evt->flick != FLICK_DOWN)
562 evt->flick = FLICK_DOWN;
563 } else if ((move->cur.canvas.y - move->prev.canvas.y) < 0) {
564 if (evt->flick != FLICK_UP)
565 evt->flick = FLICK_UP;
569 static int lb_update_begin(struct livebox *handle)
571 DbgPrint("%p\n", handle);
577 layout = (Evas_Object *)livebox_get_data(handle);
579 ErrPrint("Failed to get layout object\n");
583 list = elm_object_part_content_get(layout, "livebox");
585 ErrPrint("Failed to get list\n");
589 snprintf(buffer, sizeof(buffer), "begin");
590 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
595 static int lb_update_end(struct livebox *handle)
597 DbgPrint("%p\n", handle);
603 layout = (Evas_Object *)livebox_get_data(handle);
605 ErrPrint("Failed to get layout object\n");
609 list = elm_object_part_content_get(layout, "livebox");
611 ErrPrint("Failed to get list\n");
615 snprintf(buffer, sizeof(buffer), "end");
616 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
621 static int lb_update_text(struct livebox *handle, const char *id, const char *part, const char *data)
623 DbgPrint("%p\n", handle);
624 DbgPrint("id: [%s], part[%s], data[%s]\n", id, part, data);
630 layout = (Evas_Object *)livebox_get_data(handle);
632 ErrPrint("Failed to get layout object\n");
636 list = elm_object_part_content_get(layout, "livebox");
638 ErrPrint("Failed to get list\n");
642 snprintf(buffer, sizeof(buffer), "%s=%s", part, data);
643 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
648 static int lb_update_image(struct livebox *handle, const char *id, const char *part, const char *data)
650 DbgPrint("%p\n", handle);
651 DbgPrint("id: [%s], part[%s], data[%s]\n", id, part, data);
657 layout = (Evas_Object *)livebox_get_data(handle);
659 ErrPrint("Failed to get layout object\n");
663 list = elm_object_part_content_get(layout, "livebox");
665 ErrPrint("Failed to get list\n");
669 snprintf(buffer, sizeof(buffer), "%s=%s", part, data);
670 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
675 static int lb_update_script(struct livebox *handle, const char *id, const char *part, const char *file, const char *group)
677 DbgPrint("%p\n", handle);
678 DbgPrint("id: [%s], part[%s], file[%s], group[%s]\n", id, part, file, group);
684 layout = (Evas_Object *)livebox_get_data(handle);
686 ErrPrint("Failed to get layout object\n");
690 list = elm_object_part_content_get(layout, "livebox");
692 ErrPrint("Failed to get list\n");
696 snprintf(buffer, sizeof(buffer), "%s=%s, %s", part, file, group);
697 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
702 static int lb_update_signal(struct livebox *handle, const char *id, const char *emission, const char *signal)
704 DbgPrint("%p\n", handle);
705 DbgPrint("id: [%s], emission[%s], signal[%s]\n", id, emission, signal);
711 layout = (Evas_Object *)livebox_get_data(handle);
713 ErrPrint("Failed to get layout object\n");
717 list = elm_object_part_content_get(layout, "livebox");
719 ErrPrint("Failed to get list\n");
723 snprintf(buffer, sizeof(buffer), "%s,%s", emission, signal);
724 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
728 static int lb_update_drag(struct livebox *handle, const char *id, const char *part, double dx, double dy)
730 DbgPrint("%p\n", handle);
731 DbgPrint("id: [%s], part[%s], pos[%lfx%lf]\n", id, part, dx, dy);
737 layout = (Evas_Object *)livebox_get_data(handle);
739 ErrPrint("Failed to get layout object\n");
743 list = elm_object_part_content_get(layout, "livebox");
745 ErrPrint("Failed to get list\n");
749 snprintf(buffer, sizeof(buffer), "%s=%lfx%lf", part, dx, dy);
750 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
754 static int lb_update_info_size(struct livebox *handle, const char *id, int w, int h)
756 DbgPrint("%p\n", handle);
757 DbgPrint("id: [%s], size[%dx%d]\n", id, w, h);
763 layout = (Evas_Object *)livebox_get_data(handle);
765 ErrPrint("Failed to get layout object\n");
769 list = elm_object_part_content_get(layout, "livebox");
771 ErrPrint("Failed to get list\n");
775 snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
776 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
780 static int lb_update_info_category(struct livebox *handle, const char *id, const char *category)
782 DbgPrint("%p\n", handle);
783 DbgPrint("id: [%s], category: [%s]\n", id, category);
789 layout = (Evas_Object *)livebox_get_data(handle);
791 ErrPrint("Failed to get layout object\n");
795 list = elm_object_part_content_get(layout, "livebox");
797 ErrPrint("Failed to get list\n");
801 snprintf(buffer, sizeof(buffer), "%s=%s", id, category);
802 elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
806 static void livebox_added_cb(struct livebox *handle, int ret, void *data)
815 DbgPrint("%s - %d\n", livebox_pkgname(handle), ret);
818 error_popup(data, handle, ret);
822 layout = elm_layout_add(main_get_window());
824 ErrPrint("Failed to add a layout\n");
828 if (elm_layout_file_set(layout, PKGROOT "/res/edje/live-viewer.edj", "layout") == EINA_FALSE) {
829 DbgPrint("Failed to add a layout\n");
830 evas_object_del(layout);
834 if (livebox_lb_type(handle) == LB_TYPE_TEXT) {
836 static struct livebox_script_operators ops = {
837 .update_begin = lb_update_begin,
838 .update_end = lb_update_end,
839 .update_text = lb_update_text,
840 .update_image = lb_update_image,
841 .update_script = lb_update_script,
842 .update_signal = lb_update_signal,
843 .update_drag = lb_update_drag,
844 .update_info_size = lb_update_info_size,
845 .update_info_category = lb_update_info_category,
848 list = elm_list_add(layout);
850 evas_object_del(layout);
854 (void)livebox_set_text_handler(handle, &ops);
855 elm_object_part_content_set(layout, "livebox", list);
858 Evas_Object *lb_image;
859 lb_image = create_canvas(layout);
861 ErrPrint("Failed to create a canvas\n");
862 evas_object_del(layout);
866 evas_object_event_callback_add(lb_image, EVAS_CALLBACK_MOUSE_UP, lb_mouse_up_cb, handle);
867 evas_object_event_callback_add(lb_image, EVAS_CALLBACK_MOUSE_DOWN, lb_mouse_down_cb, handle);
868 evas_object_event_callback_add(lb_image, EVAS_CALLBACK_MOUSE_MOVE, lb_mouse_move_cb, handle);
872 type = livebox_size(handle);
873 if (type != LB_SIZE_TYPE_UNKNOWN) {
874 livebox_service_get_size(type, &w, &h);
875 DbgPrint("%dx%d\n", w, h);
877 evas_object_resize(lb_image, w, h);
878 evas_object_show(lb_image);
880 update_canvas(handle, lb_image);
882 btn = elm_button_add(main_get_window());
884 elm_object_text_set(btn, "Delete");
885 evas_object_smart_callback_add(btn, "clicked", delete_btn_cb, handle);
886 elm_object_part_content_set(layout, "delete,btn", btn);
889 elm_object_part_content_set(layout, "livebox", lb_image);
891 evas_object_resize(layout, 720, 1280);
892 evas_object_show(layout);
894 create_resize_controller(handle, layout);
895 create_logger(handle, layout);
897 livebox_set_data(handle, layout);
898 DbgPrint("Livebox Set Data: %p - %p\n", handle, layout);
899 evas_object_data_set(layout, "sc", data);
901 scroller_append(data, layout);
903 idx = scroller_get_page_index(data, layout);
904 DbgPrint("Scroll to %d\n", idx);
905 scroller_scroll_to(data, idx);
908 int lb_add(Evas_Object *sc, const char *pkgname)
911 struct livebox *handle;
913 evas_object_geometry_get(sc, NULL, NULL, &w, &h);
915 DbgPrint("sc: %dx%d, package: %s\n", w, h, pkgname);
916 livebox_activate(pkgname, NULL, NULL);
918 handle = livebox_add(pkgname, "default", "user,created", "default",
919 DEFAULT_PERIOD, livebox_added_cb, sc);
921 ErrPrint("Failed to add a new livebox\n");
930 livebox_init(ecore_x_display_get());
931 livebox_set_event_handler(lb_event_cb, NULL);
932 livebox_set_fault_handler(lb_fault_cb, NULL);