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