Change the LOG_TAG
[platform/framework/web/livebox-viewer.git] / live.viewer / src / lb.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://www.tizenopensource.org/license
9  *
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.
15  */
16
17 #include <Elementary.h>
18 #include <Ecore_X.h>
19
20 #include <dlog.h>
21
22 #include <livebox.h>
23 #include <livebox-service.h>
24
25 #include "main.h"
26 #include "util.h"
27 #include "debug.h"
28 #include "lb.h"
29 #include "scroller.h"
30
31 #define FLICK_COND      100
32
33 static Evas_Object *create_canvas(Evas_Object *parent)
34 {
35         Evas_Object *canvas;
36
37         canvas = evas_object_image_filled_add(evas_object_evas_get(parent));
38         if (!canvas)
39                 return NULL;
40
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);
44         return canvas;
45 }
46
47 static int update_pd_canvas(struct livebox *handle, Evas_Object *image)
48 {
49         int w;
50         int h;
51
52         switch (livebox_pd_type(handle)) {
53         case PD_TYPE_BUFFER:
54         case PD_TYPE_PIXMAP:
55                 evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
56                 evas_object_image_alpha_set(image, EINA_TRUE);
57
58                 livebox_get_pdsize(handle, &w, &h);
59                 if (w > 0 && h > 0) {
60                         void *data;
61                         data = livebox_acquire_pdfb(handle);
62                         if (data) {
63                                 evas_object_image_size_set(image, w, h);
64                                 evas_object_image_data_copy_set(image, data);
65                                 livebox_release_pdfb(data);
66                         }
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);
70                 }
71                 break;
72         case PD_TYPE_TEXT:
73         default:
74                 break;
75         }
76
77         return 0;
78 }
79
80 static int update_canvas(struct livebox *handle, Evas_Object *image)
81 {
82         const char *filename;
83         int w;
84         int h;
85         int type;
86
87         switch (livebox_lb_type(handle)) {
88         case LB_TYPE_BUFFER:
89         case LB_TYPE_PIXMAP:
90                 evas_object_image_colorspace_set(image, EVAS_COLORSPACE_ARGB8888);
91                 evas_object_image_alpha_set(image, EINA_TRUE);
92
93                 w = h = 0;
94                 type = livebox_size(handle);
95                 livebox_service_get_size(type, &w, &h);
96                 if (w > 0 && h > 0) {
97                         void *data;
98                         data = livebox_acquire_fb(handle);
99                         if (data) {
100                                 evas_object_image_size_set(image, w, h);
101                                 evas_object_image_data_copy_set(image, data);
102                                 livebox_release_fb(data);
103                         }
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);
107                 }
108                 break;
109         case LB_TYPE_IMAGE:
110                 filename = livebox_filename(handle);
111                 if (filename) {
112                         const char *old;
113                         evas_object_image_file_get(image, &old, NULL);
114                         if (old && !strcmp(filename, old)) {
115                                 evas_object_image_reload(image);
116                         } else {
117                                 evas_object_image_file_set(image, filename, NULL);
118                         }
119
120                         w = h = 0;
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);
127                         }
128                 }
129                 break;
130         case LB_TYPE_TEXT:
131         default:
132                 break;
133         }
134
135         return 0;
136 }
137
138 static inline void prepend_log(struct livebox *handle, const char *buffer)
139 {
140         Evas_Object *layout;
141         Evas_Object *logger;
142
143         layout = livebox_get_data(handle);
144         if (!layout) {
145                 ErrPrint("Failed to get layout\n");
146                 return;
147         }
148
149         logger = elm_object_part_content_get(layout, "logger");
150         if (logger)
151                 elm_list_item_prepend(logger, buffer, NULL, NULL, NULL, NULL);
152 }
153
154 static int lb_event_cb(struct livebox *handle, enum livebox_event_type event, void *data)
155 {
156         Evas_Object *layout;
157         Evas_Object *sc;
158         Evas_Object *pd;
159         Evas_Object *image;
160
161         layout = (Evas_Object *)livebox_get_data(handle);
162         if (!layout)
163                 return -EFAULT;
164
165         switch (event) {
166         case LB_EVENT_LB_UPDATED:
167                 DbgPrint("Contents: [%s]\n", livebox_content(handle));
168                 image = elm_object_part_content_get(layout, "livebox");
169                 if (image)
170                         update_canvas(handle, image);
171                 break;
172         case LB_EVENT_PD_UPDATED:
173                 image = elm_object_part_content_get(layout, "pd");
174                 if (image)
175                         update_pd_canvas(handle, image);
176                 break;
177         case LB_EVENT_DELETED:
178                 sc = evas_object_data_del(layout, "sc");
179                 if (sc)
180                         scroller_peek_by_obj(sc, layout);
181
182                 evas_object_del(layout);
183                 break;
184         case LB_EVENT_PD_DESTROYED:
185                 pd = elm_object_part_content_unset(layout, "pd");
186                 if (pd)
187                         evas_object_del(pd);
188
189                 sc = evas_object_data_del(layout, "sc");
190                 if (sc)
191                         scroller_unlock(sc);
192                 break;
193         default:
194                 break;
195         }
196
197         return 0;
198 }
199
200 static int lb_fault_cb(enum livebox_fault_type event,
201                                         const char *pkgname, const char *filename,
202                                         const char *funcname, void *data)
203 {
204         DbgPrint("pkgname: %s\nfilename: %s\n:funcname: %s\n", pkgname, filename, funcname);
205         return 0;
206 }
207
208 static void del_cb(struct livebox *handle, int ret, void *data)
209 {
210         Evas_Object *layout = data;
211         Evas_Object *sc;
212
213         sc = evas_object_data_del(layout, "sc");
214         if (sc) {
215                 DbgPrint("Scroller: %p\n", sc);
216                 scroller_peek_by_obj(sc, layout);
217         }
218
219         DbgPrint("Delete a layout object\n");
220         evas_object_del(layout);
221 }
222
223 static void delete_btn_cb(void *handle, Evas_Object *obj, void *event_info)
224 {
225         int ret;
226         Evas_Object *layout;
227         layout = livebox_get_data(handle);
228         DbgPrint("Livebox Get Data %p - %p\n", handle, layout);
229         ret = livebox_del(handle, del_cb, layout);
230         if (ret < 0) {
231                 char buffer[256];
232                 snprintf(buffer, sizeof(buffer), "Delete returns: %d\n", ret);
233                 prepend_log(handle, buffer);
234         }
235 }
236
237 static void exit_cb(void *data, Evas_Object *obj, void *event_info)
238 {
239         evas_object_del(obj);
240 }
241
242 static void error_popup(Evas_Object *parent, struct livebox *handle, int ret)
243 {
244         Evas_Object *popup;
245         Evas_Object *button;
246         char buffer[256];
247
248         popup = elm_popup_add(parent);
249         if (!popup)
250                 return;
251
252         button = elm_button_add(parent);
253         if (!button) {
254                 evas_object_del(popup);
255                 return;
256         }
257
258         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_CENTER);
259         elm_object_part_text_set(popup, "title,text", "Unable to load a livebox");
260
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);
264
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);
269
270         return;
271 }
272
273 static void resize_cb(struct livebox *handle, int ret, void *data)
274 {
275         Evas_Object *layout;
276         Evas_Object *log_list;
277         char buffer[256];
278
279         layout = livebox_get_data(handle);
280         if (!layout)
281                 return;
282
283         log_list = elm_object_part_content_get(layout, "logger");
284         if (!log_list)
285                 return;
286
287         snprintf(buffer, sizeof(buffer) - 1, "Resize: %d", ret);
288         elm_list_item_prepend(log_list, buffer, NULL, NULL, NULL, NULL);
289 }
290
291 static void resize_click_cb(void *handle, Evas_Object *obj, void *event_info)
292 {
293         Elm_Object_Item *item;
294         const char *label;
295         int w;
296         int h;
297         int size_type;
298
299         item = elm_list_selected_item_get(obj);
300         if (!item)
301                 return;
302
303         label = elm_object_item_part_text_get(item, NULL);
304         if (!label)
305                 return;
306
307         sscanf(label, "%dx%d", &w, &h);
308         size_type = livebox_service_size_type(w, h);
309
310         livebox_resize(handle, size_type, resize_cb, NULL);
311 }
312
313 static void create_resize_controller(struct livebox *handle, Evas_Object *layout)
314 {
315         Evas_Object *size_list;
316         char buffer[256];
317         int sizes[7];
318         int cnt;
319         int i;
320         int w;
321         int h;
322
323         size_list = elm_list_add(layout);
324         cnt = 7;
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);
329
330                 elm_list_item_append(size_list, buffer, NULL, NULL, resize_click_cb, handle);
331         }
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);
337 }
338
339 static void create_logger(struct livebox *handle, Evas_Object *layout)
340 {
341         Evas_Object *log_list;
342
343         log_list = elm_list_add(layout);
344         if (!log_list)
345                 return;
346
347         elm_list_item_prepend(log_list, "Created", NULL, NULL, NULL, NULL);
348         evas_object_show(log_list);
349         elm_list_go(log_list);
350
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);
354 }
355
356 struct event_data {
357         Evas_Coord x;
358         Evas_Coord y;
359
360         enum flick {
361                 FLICK_UNKNOWN = 0x00,
362                 FLICK_DOWN = 0x01,
363                 FLICK_UP = 0x02,
364         } flick;
365 };
366
367 static void pd_closed_cb(struct livebox *handle, int ret, void *data)
368 {
369         evas_object_del(data);
370 }
371
372 static void pd_hide_done_cb(void *data, Evas_Object *obj, const char *emission, const char *source) 
373 {
374         Evas_Object *pd;
375         Evas_Object *sc;
376
377         sc = evas_object_data_get(obj, "sc");
378         scroller_unlock(sc);
379
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);
383 }
384
385 static void pd_mouse_up_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
386 {
387         Evas_Event_Mouse_Up *up = event_info;
388         Evas_Coord x, y, w, h;
389         double rx, ry;
390
391         evas_object_geometry_get(obj, &x, &y, &w, &h);
392
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);
396 }
397
398 static void pd_mouse_down_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
399 {
400         Evas_Event_Mouse_Down *down = event_info;
401         Evas_Coord x, y, w, h;
402         double rx, ry;
403
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);
408 }
409
410 static void pd_mouse_move_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
411 {
412         Evas_Event_Mouse_Move *move = event_info;
413         Evas_Coord x, y, w, h;
414         double rx, ry;
415
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);
420 }
421
422 static void pd_created_cb(struct livebox *handle, int ret, void *data)
423 {
424         Evas_Object *layout;
425         Evas_Object *pd_image;
426         Evas_Object *sc;
427
428         layout = (Evas_Object *)livebox_get_data(handle);
429         if (!layout)
430                 return;
431
432         sc = evas_object_data_get(layout, "sc");
433
434         pd_image = create_canvas(layout);
435         if (!pd_image)
436                 return;
437
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);
441
442         update_pd_canvas(handle, pd_image);
443
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");
447         scroller_lock(sc);
448 }
449
450 static void lb_mouse_up_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
451 {
452         Evas_Event_Mouse_Up *up = event_info;
453         struct event_data *evt;
454
455         evt = evas_object_data_del(obj, "evt");
456         if (!evt)
457                 return;
458
459         if (livebox_lb_type(handle) == LB_TYPE_PIXMAP || livebox_lb_type(handle) == LB_TYPE_BUFFER) {
460                 Evas_Coord x, y, w, h;
461                 double rx, ry;
462
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);
467         } else {
468                 Evas_Coord x, y, w, h;
469                 evas_object_geometry_get(obj, &x, &y, &w, &h);
470
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);
474                         }
475                 }
476         }
477
478         if (evt->flick == FLICK_DOWN && (up->canvas.y - evt->y) > (FLICK_COND>>1)) {
479                 int ret;
480                 /* Open PD */
481                 ret = livebox_create_pd_with_position(handle, 0.5, 0.0, pd_created_cb, NULL);
482         }
483
484         free(evt);
485 }
486
487 static void lb_mouse_down_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
488 {
489         struct event_data *evt;
490         Evas_Event_Mouse_Down *down = event_info;
491         Evas_Object *layout;
492         Evas_Object *sc;
493
494         layout = livebox_get_data(handle);
495         if (!layout)
496                 return;
497
498         sc = evas_object_data_get(layout, "sc");
499         if (!sc)
500                 return;
501
502         if (scroller_is_scrolling(sc))
503                 return;
504
505         if (livebox_lb_type(handle) == LB_TYPE_PIXMAP || livebox_lb_type(handle) == LB_TYPE_BUFFER) {
506                 Evas_Coord x, y, w, h;
507                 double rx, ry;
508
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);
513         }
514
515         evt = evas_object_data_get(obj, "evt");
516         if (evt) {
517                 ErrPrint("Huh?");
518         } else {
519                 evt = malloc(sizeof(*evt));
520                 if (!evt) {
521                         ErrPrint("Heap: %s\n", strerror(errno));
522                         return;
523                 }
524         }
525
526         evas_object_data_set(obj, "evt", evt);
527
528         evt->x = down->canvas.x;
529         evt->y = down->canvas.y;
530         evt->flick = FLICK_UNKNOWN;
531 }
532
533 static void lb_mouse_move_cb(void *handle, Evas *e, Evas_Object *obj, void *event_info)
534 {
535         Evas_Event_Mouse_Move *move = event_info;
536         struct event_data *evt;
537
538         evt = evas_object_data_get(obj, "evt");
539         if (!evt)
540                 return;
541
542         if (livebox_lb_type(handle) == LB_TYPE_PIXMAP || livebox_lb_type(handle) == LB_TYPE_BUFFER) {
543                 Evas_Coord x, y, w, h;
544                 double rx, ry;
545
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);
550         }
551
552         if ((move->cur.canvas.x - move->prev.canvas.x) > FLICK_COND) {
553                 evt->flick = FLICK_UNKNOWN;
554                 return;
555         } else if ((move->cur.canvas.x - move->prev.canvas.x) < -FLICK_COND) {
556                 evt->flick = FLICK_UNKNOWN;
557                 return;
558         }
559
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;
566         }
567 }
568
569 static int lb_update_begin(struct livebox *handle)
570 {
571         DbgPrint("%p\n", handle);
572
573         Evas_Object *layout;
574         Evas_Object *list;
575         char buffer[80];
576
577         layout = (Evas_Object *)livebox_get_data(handle);
578         if (!layout) {
579                 ErrPrint("Failed to get layout object\n");
580                 return 0;
581         }
582
583         list = elm_object_part_content_get(layout, "livebox");
584         if (!list) {
585                 ErrPrint("Failed to get list\n");
586                 return 0;
587         }
588
589         snprintf(buffer, sizeof(buffer), "begin");
590         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
591
592         return 0;
593 }
594
595 static int lb_update_end(struct livebox *handle)
596 {
597         DbgPrint("%p\n", handle);
598
599         Evas_Object *layout;
600         Evas_Object *list;
601         char buffer[80];
602
603         layout = (Evas_Object *)livebox_get_data(handle);
604         if (!layout) {
605                 ErrPrint("Failed to get layout object\n");
606                 return 0;
607         }
608
609         list = elm_object_part_content_get(layout, "livebox");
610         if (!list) {
611                 ErrPrint("Failed to get list\n");
612                 return 0;
613         }
614
615         snprintf(buffer, sizeof(buffer), "end");
616         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
617
618         return 0;
619 }
620
621 static int lb_update_text(struct livebox *handle, const char *id, const char *part, const char *data)
622 {
623         DbgPrint("%p\n", handle);
624         DbgPrint("id: [%s], part[%s], data[%s]\n", id, part, data);
625
626         Evas_Object *layout;
627         Evas_Object *list;
628         char buffer[80];
629
630         layout = (Evas_Object *)livebox_get_data(handle);
631         if (!layout) {
632                 ErrPrint("Failed to get layout object\n");
633                 return 0;
634         }
635
636         list = elm_object_part_content_get(layout, "livebox");
637         if (!list) {
638                 ErrPrint("Failed to get list\n");
639                 return 0;
640         }
641
642         snprintf(buffer, sizeof(buffer), "%s=%s", part, data);
643         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
644
645         return 0;
646 }
647
648 static int lb_update_image(struct livebox *handle, const char *id, const char *part, const char *data)
649 {
650         DbgPrint("%p\n", handle);
651         DbgPrint("id: [%s], part[%s], data[%s]\n", id, part, data);
652
653         Evas_Object *layout;
654         Evas_Object *list;
655         char buffer[80];
656
657         layout = (Evas_Object *)livebox_get_data(handle);
658         if (!layout) {
659                 ErrPrint("Failed to get layout object\n");
660                 return 0;
661         }
662
663         list = elm_object_part_content_get(layout, "livebox");
664         if (!list) {
665                 ErrPrint("Failed to get list\n");
666                 return 0;
667         }
668
669         snprintf(buffer, sizeof(buffer), "%s=%s", part, data);
670         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
671
672         return 0;
673 }
674
675 static int lb_update_script(struct livebox *handle, const char *id, const char *part, const char *file, const char *group)
676 {
677         DbgPrint("%p\n", handle);
678         DbgPrint("id: [%s], part[%s], file[%s], group[%s]\n", id, part, file, group);
679
680         Evas_Object *layout;
681         Evas_Object *list;
682         char buffer[80];
683
684         layout = (Evas_Object *)livebox_get_data(handle);
685         if (!layout) {
686                 ErrPrint("Failed to get layout object\n");
687                 return 0;
688         }
689
690         list = elm_object_part_content_get(layout, "livebox");
691         if (!list) {
692                 ErrPrint("Failed to get list\n");
693                 return 0;
694         }
695
696         snprintf(buffer, sizeof(buffer), "%s=%s, %s", part, file, group);
697         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
698
699         return 0;
700 }
701
702 static int lb_update_signal(struct livebox *handle, const char *id, const char *emission, const char *signal)
703 {
704         DbgPrint("%p\n", handle);
705         DbgPrint("id: [%s], emission[%s], signal[%s]\n", id, emission, signal);
706
707         Evas_Object *layout;
708         Evas_Object *list;
709         char buffer[80];
710
711         layout = (Evas_Object *)livebox_get_data(handle);
712         if (!layout) {
713                 ErrPrint("Failed to get layout object\n");
714                 return 0;
715         }
716
717         list = elm_object_part_content_get(layout, "livebox");
718         if (!list) {
719                 ErrPrint("Failed to get list\n");
720                 return 0;
721         }
722
723         snprintf(buffer, sizeof(buffer), "%s,%s", emission, signal);
724         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
725         return 0;
726 }
727
728 static int lb_update_drag(struct livebox *handle, const char *id, const char *part, double dx, double dy)
729 {
730         DbgPrint("%p\n", handle);
731         DbgPrint("id: [%s], part[%s], pos[%lfx%lf]\n", id, part, dx, dy);
732
733         Evas_Object *layout;
734         Evas_Object *list;
735         char buffer[80];
736
737         layout = (Evas_Object *)livebox_get_data(handle);
738         if (!layout) {
739                 ErrPrint("Failed to get layout object\n");
740                 return 0;
741         }
742
743         list = elm_object_part_content_get(layout, "livebox");
744         if (!list) {
745                 ErrPrint("Failed to get list\n");
746                 return 0;
747         }
748
749         snprintf(buffer, sizeof(buffer), "%s=%lfx%lf", part, dx, dy);
750         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
751         return 0;
752 }
753
754 static int lb_update_info_size(struct livebox *handle, const char *id, int w, int h)
755 {
756         DbgPrint("%p\n", handle);
757         DbgPrint("id: [%s], size[%dx%d]\n", id, w, h);
758
759         Evas_Object *layout;
760         Evas_Object *list;
761         char buffer[80];
762
763         layout = (Evas_Object *)livebox_get_data(handle);
764         if (!layout) {
765                 ErrPrint("Failed to get layout object\n");
766                 return 0;
767         }
768
769         list = elm_object_part_content_get(layout, "livebox");
770         if (!list) {
771                 ErrPrint("Failed to get list\n");
772                 return 0;
773         }
774
775         snprintf(buffer, sizeof(buffer), "%dx%d", w, h);
776         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
777         return 0;
778 }
779
780 static int lb_update_info_category(struct livebox *handle, const char *id, const char *category)
781 {
782         DbgPrint("%p\n", handle);
783         DbgPrint("id: [%s], category: [%s]\n", id, category);
784
785         Evas_Object *layout;
786         Evas_Object *list;
787         char buffer[80];
788
789         layout = (Evas_Object *)livebox_get_data(handle);
790         if (!layout) {
791                 ErrPrint("Failed to get layout object\n");
792                 return 0;
793         }
794
795         list = elm_object_part_content_get(layout, "livebox");
796         if (!list) {
797                 ErrPrint("Failed to get list\n");
798                 return 0;
799         }
800
801         snprintf(buffer, sizeof(buffer), "%s=%s", id, category);
802         elm_list_item_prepend(list, buffer, NULL, NULL, NULL, NULL);
803         return 0;
804 }
805
806 static void livebox_added_cb(struct livebox *handle, int ret, void *data)
807 {
808         Evas_Object *layout;
809         Evas_Object *btn;
810         int w;
811         int h;
812         int type;
813         int idx;
814
815         DbgPrint("%s - %d\n", livebox_pkgname(handle), ret);
816
817         if (ret != 0) {
818                 error_popup(data, handle, ret);
819                 return;
820         }
821
822         layout = elm_layout_add(main_get_window());
823         if (!layout) {
824                 ErrPrint("Failed to add a layout\n");
825                 return;
826         }
827
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);
831                 return;
832         }
833
834         if (livebox_lb_type(handle) == LB_TYPE_TEXT) {
835                 Evas_Object *list;
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,
846                 };
847
848                 list = elm_list_add(layout);
849                 if (!list) {
850                         evas_object_del(layout);
851                         return;
852                 }
853
854                 (void)livebox_set_text_handler(handle, &ops);
855                 elm_object_part_content_set(layout, "livebox", list);
856                 elm_list_go(list);
857         } else {
858                 Evas_Object *lb_image;
859                 lb_image = create_canvas(layout);
860                 if (!lb_image) {
861                         ErrPrint("Failed to create a canvas\n");
862                         evas_object_del(layout);
863                         return;
864                 }
865
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);
869
870                 w = 0;
871                 h = 0;
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);
876                 }
877                 evas_object_resize(lb_image, w, h);
878                 evas_object_show(lb_image);
879
880                 update_canvas(handle, lb_image);
881
882                 btn = elm_button_add(main_get_window());
883                 if (btn) {
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);
887                 }
888
889                 elm_object_part_content_set(layout, "livebox", lb_image);
890         }
891         evas_object_resize(layout, 720, 1280);
892         evas_object_show(layout);
893
894         create_resize_controller(handle, layout);
895         create_logger(handle, layout);
896
897         livebox_set_data(handle, layout);
898         DbgPrint("Livebox Set Data: %p - %p\n", handle, layout);
899         evas_object_data_set(layout, "sc", data);
900
901         scroller_append(data, layout);
902
903         idx = scroller_get_page_index(data, layout);
904         DbgPrint("Scroll to %d\n", idx);
905         scroller_scroll_to(data, idx);
906 }
907
908 int lb_add(Evas_Object *sc, const char *pkgname)
909 {
910         int w, h;
911         struct livebox *handle;
912
913         evas_object_geometry_get(sc, NULL, NULL, &w, &h);
914
915         DbgPrint("sc: %dx%d, package: %s\n", w, h, pkgname);
916         livebox_activate(pkgname, NULL, NULL);
917
918         handle = livebox_add(pkgname, "default", "user,created", "default",
919                                                 DEFAULT_PERIOD, livebox_added_cb, sc);
920         if (!handle) {
921                 ErrPrint("Failed to add a new livebox\n");
922                 return -EFAULT;
923         }
924
925         return 0;
926 }
927
928 int lb_init(void)
929 {
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);
933         return 0;
934 }
935
936 int lb_fini(void)
937 {
938         livebox_fini();
939         return 0;
940 }
941
942 /* End of a file */