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