16674b7e5e664710c713f3b610c473cc7be2d788
[platform/framework/web/livebox-viewer.git] / src / livebox.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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 <stdio.h>
18 #include <errno.h>
19 #include <stdlib.h> /* malloc */
20 #include <string.h> /* strdup */
21 #include <math.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <gio/gio.h>
28 #include <aul.h>
29 #include <dlog.h>
30
31 #include <com-core_packet.h>
32 #include <packet.h>
33 #include <livebox-service.h>
34 #include <livebox-errno.h>
35
36 #include "debug.h"
37 #include "fb.h"
38 #include "livebox.h"
39 #include "livebox_internal.h"
40 #include "dlist.h"
41 #include "util.h"
42 #include "master_rpc.h"
43 #include "client.h"
44 #include "conf.h"
45
46 #define EAPI __attribute__((visibility("default")))
47
48 #if defined(FLOG)
49 FILE *__file_log_fp;
50 #endif
51
52 enum event_state {
53         INFO_STATE_CALLBACK_IN_IDLE = 0x00,
54         INFO_STATE_CALLBACK_IN_PROCESSING = 0x01,
55 };
56
57 static struct info {
58         struct dlist *livebox_list;
59         struct dlist *livebox_common_list;
60
61         struct dlist *event_list;
62         struct dlist *fault_list;
63
64         int init_count;
65         int prevent_overwrite;
66         enum event_state event_state;
67         enum event_state fault_state;
68         guint job_timer;
69         struct dlist *job_list;
70 } s_info = {
71         .livebox_list = NULL,
72         .event_list = NULL,
73         .fault_list = NULL,
74         .init_count = 0,
75         .prevent_overwrite = 0,
76         .event_state = INFO_STATE_CALLBACK_IN_IDLE,
77         .fault_state = INFO_STATE_CALLBACK_IN_IDLE,
78         .job_timer = 0,
79         .job_list = NULL,
80 };
81
82 struct cb_info {
83         ret_cb_t cb;
84         void *data;
85 };
86
87 struct event_info {
88         int is_deleted;
89         int (*handler)(struct livebox *handler, enum livebox_event_type event, void *data);
90         void *user_data;
91 };
92
93 struct fault_info {
94         int is_deleted;
95         int (*handler)(enum livebox_fault_type event, const char *pkgname, const char *filename, const char *func, void *data);
96         void *user_data;
97 };
98
99 static void lb_pixmap_acquired_cb(struct livebox *handler, const struct packet *result, void *data);
100 static void pd_pixmap_acquired_cb(struct livebox *handler, const struct packet *result, void *data);
101
102 static inline void default_create_cb(struct livebox *handler, int ret, void *data)
103 {
104         DbgPrint("Default created event handler: %d\n", ret);
105 }
106
107 static inline void default_delete_cb(struct livebox *handler, int ret, void *data)
108 {
109         DbgPrint("Default deleted event handler: %d\n", ret);
110 }
111
112 static inline void default_pinup_cb(struct livebox *handler, int ret, void *data)
113 {
114         DbgPrint("Default pinup event handler: %d\n", ret);
115 }
116
117 static inline void default_group_changed_cb(struct livebox *handler, int ret, void *data)
118 {
119         DbgPrint("Default group changed event handler: %d\n", ret);
120 }
121
122 static inline void default_period_changed_cb(struct livebox *handler, int ret, void *data)
123 {
124         DbgPrint("Default period changed event handler: %d\n", ret);
125 }
126
127 static inline void default_pd_created_cb(struct livebox *handler, int ret, void *data)
128 {
129         DbgPrint("Default PD created event handler: %d\n", ret);
130 }
131
132 static inline void default_pd_destroyed_cb(struct livebox *handler, int ret, void *data)
133 {
134         DbgPrint("Default PD destroyed event handler: %d\n", ret);
135 }
136
137 static inline void default_lb_size_changed_cb(struct livebox *handler, int ret, void *data)
138 {
139         DbgPrint("Default LB size changed event handler: %d\n", ret);
140 }
141
142 static inline void default_update_mode_cb(struct livebox *handler, int ret, void *data)
143 {
144         DbgPrint("Default update mode set event handler: %d\n", ret);
145 }
146
147 static inline void default_access_event_cb(struct livebox *handler, int ret, void *data)
148 {
149         DbgPrint("Default access event handler: %d\n", ret);
150 }
151
152 static inline void default_key_event_cb(struct livebox *handler, int ret, void *data)
153 {
154         DbgPrint("Default key event handler: %d\n", ret);
155 }
156
157 static inline __attribute__((always_inline)) struct cb_info *create_cb_info(ret_cb_t cb, void *data)
158 {
159         struct cb_info *info;
160
161         info = malloc(sizeof(*info));
162         if (!info) {
163                 ErrPrint("Heap: %s\n", strerror(errno));
164                 return NULL;
165         }
166
167         info->cb = cb;
168         info->data = data;
169         return info;
170 }
171
172 static inline void destroy_cb_info(struct cb_info *info)
173 {
174         free(info);
175 }
176
177 static int do_fb_lock(int fd)
178 {
179         struct flock flock;
180         int ret;
181
182         flock.l_type = F_RDLCK;
183         flock.l_whence = SEEK_SET;
184         flock.l_start = 0;
185         flock.l_len = 0;
186         flock.l_pid = getpid();
187
188         do {
189                 ret = fcntl(fd, F_SETLKW, &flock);
190                 if (ret < 0) {
191                         ret = errno;
192                         ErrPrint("fcntl: %s\n", strerror(errno));
193                 }
194         } while (ret == EINTR);
195
196         return ret;
197 }
198
199 static int do_fb_unlock(int fd)
200 {
201         struct flock flock;
202         int ret;
203
204         flock.l_type = F_UNLCK;
205         flock.l_whence = SEEK_SET;
206         flock.l_start = 0;
207         flock.l_len = 0;
208         flock.l_pid = getpid();
209
210         do {
211                 ret = fcntl(fd, F_SETLKW, &flock);
212                 if (ret < 0) {
213                         ret = errno;
214                         ErrPrint("fcntl: %s\n", strerror(errno));
215                 }
216         } while (ret == EINTR);
217
218         return ret;
219 }
220
221 int lb_destroy_lock_file(struct livebox_common *common, int is_pd)
222 {
223         if (is_pd) {
224                 if (!common->pd.lock) {
225                         return LB_STATUS_ERROR_INVALID;
226                 }
227
228                 if (close(common->pd.lock_fd) < 0) {
229                         ErrPrint("close: %s\n", strerror(errno));
230                 }
231                 common->pd.lock_fd = -1;
232
233                 if (unlink(common->pd.lock) < 0) {
234                         ErrPrint("unlink: %s\n", strerror(errno));
235                 }
236
237                 free(common->pd.lock);
238                 common->pd.lock = NULL;
239         } else {
240                 if (!common->lb.lock) {
241                         return LB_STATUS_ERROR_INVALID;
242                 }
243
244                 if (close(common->lb.lock_fd) < 0) {
245                         ErrPrint("close: %s\n", strerror(errno));
246                 }
247                 common->lb.lock_fd = -1;
248
249                 if (unlink(common->lb.lock) < 0) {
250                         ErrPrint("unlink: %s\n", strerror(errno));
251                 }
252
253                 free(common->lb.lock);
254                 common->lb.lock = NULL;
255         }
256
257         return LB_STATUS_SUCCESS;
258 }
259
260 int lb_create_lock_file(struct livebox_common *common, int is_pd)
261 {
262         int len;
263         char *file;
264
265         len = strlen(common->id);
266         file = malloc(len + 20);
267         if (!file) {
268                 ErrPrint("Heap: %s\n", strerror(errno));
269                 return LB_STATUS_ERROR_MEMORY;
270         }
271
272         snprintf(file, len + 20, "%s.%s.lck", util_uri_to_path(common->id), is_pd ? "pd" : "lb");
273
274         if (is_pd) {
275                 common->pd.lock_fd = open(file, O_RDONLY);
276                 if (common->pd.lock_fd < 0) {
277                         ErrPrint("open: %s\n", strerror(errno));
278                         free(file);
279                         return LB_STATUS_ERROR_IO;
280                 }
281
282                 common->pd.lock = file;
283         } else {
284                 common->lb.lock_fd = open(file, O_RDONLY);
285                 if (common->lb.lock_fd < 0) {
286                         ErrPrint("open: %s\n", strerror(errno));
287                         free(file);
288                         return LB_STATUS_ERROR_IO;
289                 }
290
291                 common->lb.lock = file;
292         }
293
294         return LB_STATUS_SUCCESS;
295 }
296
297 static void update_mode_cb(struct livebox *handler, const struct packet *result, void *data)
298 {
299         int ret;
300
301         if (!result) {
302                 ret = LB_STATUS_ERROR_FAULT;
303                 goto errout;
304         } else if (packet_get(result, "i", &ret) != 1) {
305                 ErrPrint("Invalid argument\n");
306                 ret = LB_STATUS_ERROR_INVALID;
307                 goto errout;
308         }
309
310         if (ret < 0) {
311                 ErrPrint("Resize request is failed: %d\n", ret);
312                 goto errout;
313         }
314
315         return;
316
317 errout:
318         handler->cbs.update_mode.cb(handler, ret, handler->cbs.update_mode.data);
319         handler->cbs.update_mode.cb = NULL;
320         handler->cbs.update_mode.data = NULL;
321         handler->common->request.update_mode = 0;
322
323         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
324                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
325                 lb_unref(handler, 1);
326         }
327 }
328
329 static void resize_cb(struct livebox *handler, const struct packet *result, void *data)
330 {
331         int ret;
332
333         if (!result) {
334                 ret = LB_STATUS_ERROR_FAULT;
335                 goto errout;
336         } else if (packet_get(result, "i", &ret) != 1) {
337                 ErrPrint("Invalid argument\n");
338                 ret = LB_STATUS_ERROR_INVALID;
339                 goto errout;
340         }
341
342         /*!
343          * \note
344          * In case of resize request,
345          * The livebox handler will not have resized value right after this callback,
346          * It can only get the new size when it makes updates.
347          *
348          * So the user can only get the resized value(result) from the first update event
349          * after this request.
350          */
351         if (ret < 0) {
352                 ErrPrint("Resize request is failed: %d\n", ret);
353                 goto errout;
354         }
355
356         return;
357
358 errout:
359         handler->cbs.size_changed.cb(handler, ret, handler->cbs.size_changed.data);
360         handler->cbs.size_changed.cb = NULL;
361         handler->cbs.size_changed.data = NULL;
362         handler->common->request.size_changed = 0;
363
364         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
365                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
366                 lb_unref(handler, 1);
367         }
368 }
369
370 static void text_signal_cb(struct livebox *handler, const struct packet *result, void *data)
371 {
372         int ret;
373         void *cbdata;
374         struct cb_info *info = data;
375         ret_cb_t cb;
376
377         cbdata = info->data;
378         cb = info->cb;
379         destroy_cb_info(info);
380
381         if (!result) {
382                 ret = LB_STATUS_ERROR_FAULT;
383         } else if (packet_get(result, "i", &ret) != 1) {
384                 ErrPrint("Invalid argument\n");
385                 ret = LB_STATUS_ERROR_INVALID;
386         }
387
388         if (cb) {
389                 cb(handler, ret, cbdata);
390         }
391         return;
392 }
393
394 static void set_group_ret_cb(struct livebox *handler, const struct packet *result, void *data)
395 {
396         int ret;
397
398         if (!result) {
399                 ret = LB_STATUS_ERROR_FAULT;
400                 goto errout;
401         } else if (packet_get(result, "i", &ret) != 1) {
402                 ErrPrint("Invalid argument\n");
403                 ret = LB_STATUS_ERROR_INVALID;
404                 goto errout;
405         }
406
407         if (ret < 0) {
408                 goto errout;
409         }
410
411         return;
412
413 errout:
414         handler->cbs.group_changed.cb(handler, ret, handler->cbs.group_changed.data);
415         handler->cbs.group_changed.cb = NULL;
416         handler->cbs.group_changed.data = NULL;
417         handler->common->request.group_changed = 0;
418
419         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
420                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
421                 lb_unref(handler, 1);
422         }
423 }
424
425 static void period_ret_cb(struct livebox *handler, const struct packet *result, void *data)
426 {
427         int ret;
428
429         if (!result) {
430                 ret = LB_STATUS_ERROR_FAULT;
431                 goto errout;
432         } else if (packet_get(result, "i", &ret) != 1) {
433                 ErrPrint("Invalid argument\n");
434                 ret = LB_STATUS_ERROR_INVALID;
435                 goto errout;
436         }
437
438         if (ret < 0) {
439                 goto errout;
440         }
441
442         return;
443
444 errout:
445         handler->cbs.period_changed.cb(handler, ret, handler->cbs.period_changed.data);
446         handler->cbs.period_changed.cb = NULL;
447         handler->cbs.period_changed.data = NULL;
448         handler->common->request.period_changed = 0;
449
450         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
451                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
452                 lb_unref(handler, 1);
453         }
454 }
455
456 static void del_ret_cb(struct livebox *handler, const struct packet *result, void *data)
457 {
458         struct cb_info *info = data;
459         int ret;
460         ret_cb_t cb;
461         void *cbdata;
462
463         cb = info->cb;
464         cbdata = info->data;
465         destroy_cb_info(info);
466
467         if (!result) {
468                 ErrPrint("Connection lost?\n");
469                 ret = LB_STATUS_ERROR_FAULT;
470         } else if (packet_get(result, "i", &ret) != 1) {
471                 ErrPrint("Invalid argument\n");
472                 ret = LB_STATUS_ERROR_INVALID;
473         }
474
475         if (ret == 0) {
476                 handler->cbs.deleted.cb = cb;
477                 handler->cbs.deleted.data = cbdata;
478         } else if (cb) {
479                 cb(handler, ret, cbdata);
480         }
481
482         /*!
483          * \note
484          * Do not call the deleted callback from here.
485          * master will send the "deleted" event.
486          * Then invoke this callback.
487          *
488          * if (handler->cbs.deleted.cb)
489          *      handler->cbs.deleted.cb(handler, ret, handler->cbs.deleted.data);
490          */
491 }
492
493 static void new_ret_cb(struct livebox *handler, const struct packet *result, void *data)
494 {
495         int ret;
496         struct cb_info *info = data;
497         ret_cb_t cb;
498         void *cbdata;
499
500         cb = info->cb;
501         cbdata = info->data;
502         destroy_cb_info(info);
503
504         if (!result) {
505                 ret = LB_STATUS_ERROR_FAULT;
506         } else if (packet_get(result, "i", &ret) != 1) {
507                 ret = LB_STATUS_ERROR_INVALID;
508         }
509
510         if (ret >= 0) {
511                 handler->cbs.created.cb = cb;
512                 handler->cbs.created.data = cbdata;
513
514                 /*!
515                  * \note
516                  * Don't go anymore ;)
517                  */
518                 return;
519         } else if (cb) {
520                 /*!
521                  * \note
522                  * It means the current instance is not created,
523                  * so user has to know about this.
524                  * notice it to user using "deleted" event.
525                  */
526                 cb(handler, ret, cbdata);
527         }
528
529         lb_unref(handler, 1);
530 }
531
532 static void pd_create_cb(struct livebox *handler, const struct packet *result, void *data)
533 {
534         int ret;
535
536         if (!result) {
537                 ret = LB_STATUS_ERROR_FAULT;
538                 goto errout;
539         } else if (packet_get(result, "i", &ret) != 1) {
540                 ret = LB_STATUS_ERROR_INVALID;
541                 goto errout;
542         }
543
544         if (ret < 0) {
545                 ErrPrint("Failed to create a PD[%d]\n", ret);
546                 goto errout;
547         }
548
549         return;
550
551 errout:
552         handler->cbs.pd_created.cb(handler, ret, handler->cbs.pd_created.data);
553         handler->cbs.pd_created.cb = NULL;
554         handler->cbs.pd_created.data = NULL;
555         handler->common->request.pd_created = 0;
556
557         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
558                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
559                 lb_unref(handler, 1);
560         }
561 }
562
563 static void activated_cb(struct livebox *handler, const struct packet *result, void *data)
564 {
565         int ret;
566         struct cb_info *info = data;
567         void *cbdata;
568         ret_cb_t cb;
569         const char *pkgname = "";
570
571         cbdata = info->data;
572         cb = info->cb;
573         destroy_cb_info(info);
574
575         if (!result) {
576                 ret = LB_STATUS_ERROR_FAULT;
577         } else if (packet_get(result, "is", &ret, &pkgname) != 2) {
578                 ret = LB_STATUS_ERROR_INVALID;
579         }
580
581         if (cb) {
582                 cb(handler, ret, cbdata);
583         }
584 }
585
586 static void pd_destroy_cb(struct livebox *handler, const struct packet *result, void *data)
587 {
588         int ret;
589         ret_cb_t cb;
590         void *cbdata;
591         struct cb_info *info = data;
592
593         cbdata = info->data;
594         cb = info->cb;
595         destroy_cb_info(info);
596
597         if (!result) {
598                 ErrPrint("Result is NIL (may connection lost)\n");
599                 ret = LB_STATUS_ERROR_FAULT;
600         } else if (packet_get(result, "i", &ret) != 1) {
601                 ErrPrint("Invalid parameter\n");
602                 ret = LB_STATUS_ERROR_INVALID;
603         }
604
605         if (ret == LB_STATUS_SUCCESS) {
606                 handler->cbs.pd_destroyed.cb = cb;
607                 handler->cbs.pd_destroyed.data = cbdata;
608         } else {
609                 handler->common->is_pd_created = 0;
610                 handler->common->request.pd_destroyed = 0;
611
612                 if (cb) {
613                         cb(handler, ret, cbdata);
614                 }
615         }
616 }
617
618 static void delete_cluster_cb(struct livebox *handler, const struct packet *result, void *data)
619 {
620         struct cb_info *info = data;
621         int ret;
622         ret_cb_t cb;
623         void *cbdata;
624
625         cb = info->cb;
626         cbdata = info->data;
627         destroy_cb_info(info);
628
629         if (!result) {
630                 ret = LB_STATUS_ERROR_FAULT;
631         } else if (packet_get(result, "i", &ret) != 1) {
632                 ret = LB_STATUS_ERROR_INVALID;
633         }
634
635         if (cb) {
636                 cb(handler, ret, cbdata);
637         }
638 }
639
640 static void delete_category_cb(struct livebox *handler, const struct packet *result, void *data)
641 {
642         struct cb_info *info = data;
643         int ret;
644         ret_cb_t cb;
645         void *cbdata;
646
647         cb = info->cb;
648         cbdata = info->data;
649         destroy_cb_info(info);
650
651         if (!result) {
652                 ret = LB_STATUS_ERROR_FAULT;
653         } else if (packet_get(result, "i", &ret) != 1) {
654                 ret = LB_STATUS_ERROR_INVALID;
655         }
656
657         if (cb) {
658                 cb(handler, ret, cbdata);
659         }
660 }
661
662 static int lb_acquire_lb_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
663 {
664         struct packet *packet;
665         struct cb_info *cbinfo;
666         const char *id;
667         int ret;
668
669         id = fb_id(handler->common->lb.fb);
670         if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
671                 return LB_STATUS_ERROR_INVALID;
672         }
673
674         packet = packet_create("lb_acquire_pixmap", "ss", handler->common->pkgname, handler->common->id);
675         if (!packet) {
676                 ErrPrint("Failed to build a param\n");
677                 return LB_STATUS_ERROR_FAULT;
678         }
679
680         cbinfo = create_cb_info(cb, data);
681         if (!cbinfo) {
682                 packet_destroy(packet);
683                 return LB_STATUS_ERROR_FAULT;
684         }
685
686         ret = master_rpc_async_request(handler, packet, 0, lb_pixmap_acquired_cb, cbinfo);
687         if (ret < 0) {
688                 destroy_cb_info(cbinfo);
689         }
690
691         return ret;
692 }
693
694 static void lb_pixmap_acquired_cb(struct livebox *handler, const struct packet *result, void *data)
695 {
696         int pixmap;
697         int ret = LB_STATUS_ERROR_INVALID;
698         ret_cb_t cb;
699         void *cbdata;
700         struct cb_info *info = data;
701
702         cb = info->cb;
703         cbdata = info->data;
704         destroy_cb_info(info);
705
706         if (!result) {
707                 pixmap = 0; /* PIXMAP 0 means error */
708         } else if (packet_get(result, "ii", &pixmap, &ret) != 2) {
709                 pixmap = 0;
710         }
711
712         if (ret == LB_STATUS_ERROR_BUSY) {
713                 ret = lb_acquire_lb_pixmap(handler, cb, cbdata);
714                 DbgPrint("Busy, Try again: %d\n", ret);
715                 /* Try again */
716         } else if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
717                 if (cb) {
718                         cb(handler, pixmap, cbdata);
719                 }
720
721                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
722                 lb_unref(handler, 1);
723         } else {
724                 if (cb) {
725                         cb(handler, pixmap, cbdata);
726                 }
727         }
728 }
729
730 static int lb_acquire_pd_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
731 {
732         struct packet *packet;
733         struct cb_info *cbinfo;
734         const char *id;
735         int ret;
736
737         id = fb_id(handler->common->pd.fb);
738         if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
739                 return LB_STATUS_ERROR_INVALID;
740         }
741
742         packet = packet_create("pd_acquire_pixmap", "ss", handler->common->pkgname, handler->common->id);
743         if (!packet) {
744                 ErrPrint("Failed to build a param\n");
745                 return LB_STATUS_ERROR_FAULT;
746         }
747
748         cbinfo = create_cb_info(cb, data);
749         if (!cbinfo) {
750                 packet_destroy(packet);
751                 return LB_STATUS_ERROR_FAULT;
752         }
753
754         ret = master_rpc_async_request(handler, packet, 0, pd_pixmap_acquired_cb, cbinfo);
755         if (ret < 0) {
756                 /*!
757                  * \note
758                  * Packet will be destroyed by master_rpc_async_request
759                  */
760                 destroy_cb_info(cbinfo);
761         }
762
763         return ret;
764 }
765
766 static void pd_pixmap_acquired_cb(struct livebox *handler, const struct packet *result, void *data)
767 {
768         int pixmap;
769         int ret;
770         ret_cb_t cb;
771         void *cbdata;
772         struct cb_info *info = data;
773
774         cb = info->cb;
775         cbdata = info->data;
776         destroy_cb_info(info);
777
778         if (!result) {
779                 pixmap = 0; /* PIXMAP 0 means error */
780                 ret = LB_STATUS_ERROR_FAULT;
781         } else if (packet_get(result, "ii", &pixmap, &ret) != 2) {
782                 pixmap = 0;
783                 ret = LB_STATUS_ERROR_INVALID;
784         }
785
786         if (ret == LB_STATUS_ERROR_BUSY) {
787                 ret = lb_acquire_pd_pixmap(handler, cb, cbdata);
788                 DbgPrint("Busy, Try again: %d\n", ret);
789                 /* Try again */
790         } else if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
791                 if (cb) {
792                         cb(handler, pixmap, cbdata);
793                 }
794                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
795                 lb_unref(handler, 1);
796         } else {
797                 if (cb) {
798                         DbgPrint("ret: %d, pixmap: %d\n", ret, pixmap);
799                         cb(handler, pixmap, cbdata);
800                 }
801         }
802 }
803
804 static void pinup_done_cb(struct livebox *handler, const struct packet *result, void *data)
805 {
806         int ret;
807
808         if (!result) {
809                 ret = LB_STATUS_ERROR_FAULT;
810                 goto errout;
811         } else if (packet_get(result, "i", &ret) != 1) {
812                 goto errout;
813         }
814
815         if (ret < 0) {
816                 goto errout;
817         }
818
819         return;
820
821 errout:
822         handler->cbs.pinup.cb(handler, ret, handler->cbs.pinup.data);
823         handler->cbs.pinup.cb = NULL;
824         handler->cbs.pinup.data = NULL;
825         handler->common->request.pinup = 0;
826
827         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
828                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
829                 lb_unref(handler, 1);
830         }
831 }
832
833 static void key_ret_cb(struct livebox *handler, const struct packet *result, void *data)
834 {
835         int ret;
836
837         if (!result) {
838                 ret = LB_STATUS_ERROR_FAULT;
839                 return;
840         }
841
842         if (packet_get(result, "i", &ret) != 1) {
843                 ret = LB_STATUS_ERROR_INVALID;
844                 return;
845         }
846
847         if (ret != LB_STATUS_SUCCESS) {
848                 goto errout;
849         }
850
851         return;
852 errout:
853         handler->cbs.key_event.cb(handler, ret, handler->cbs.key_event.data);
854         handler->cbs.key_event.cb = NULL;
855         handler->cbs.key_event.data = NULL;
856         handler->common->request.key_event = 0;
857
858         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
859                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
860                 lb_unref(handler, 1);
861         }
862 }
863
864 static void access_ret_cb(struct livebox *handler, const struct packet *result, void *data)
865 {
866         int ret;
867
868         if (!result) {
869                 ret = LB_STATUS_ERROR_FAULT;
870                 return;
871         }
872
873         if (packet_get(result, "i", &ret) != 1) {
874                 ret = LB_STATUS_ERROR_INVALID;
875                 return;
876         }
877
878         if (ret != LB_STATUS_SUCCESS) {
879                 goto errout;
880         }
881
882         return;
883
884 errout:
885         handler->cbs.access_event.cb(handler, ret, handler->cbs.access_event.data);
886         handler->cbs.access_event.cb = NULL;
887         handler->cbs.access_event.data = NULL;
888         handler->common->request.access_event = 0;
889
890         if (ret == LB_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
891                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
892                 lb_unref(handler, 1);
893         }
894 }
895
896 static int send_access_event(struct livebox *handler, const char *event, int x, int y)
897 {
898         struct packet *packet;
899         double timestamp;
900
901         timestamp = util_timestamp();
902
903         packet = packet_create(event, "ssdii", handler->common->pkgname, handler->common->id, timestamp, x, y);
904         if (!packet) {
905                 ErrPrint("Failed to build packet\n");
906                 return LB_STATUS_ERROR_FAULT;
907         }
908
909         return master_rpc_async_request(handler, packet, 0, access_ret_cb, NULL);
910 }
911
912 static int send_key_event(struct livebox *handler, const char *event, unsigned int keycode)
913 {
914         struct packet *packet;
915         double timestamp;
916
917         timestamp = util_timestamp();
918         packet = packet_create(event, "ssdi", handler->common->pkgname, handler->common->id, timestamp, keycode);
919         if (!packet) {
920                 ErrPrint("Failed to build packet\n");
921                 return LB_STATUS_ERROR_FAULT;
922         }
923
924         return master_rpc_async_request(handler, packet, 0, key_ret_cb, NULL);
925 }
926
927 static int send_mouse_event(struct livebox *handler, const char *event, int x, int y)
928 {
929         struct packet *packet;
930         double timestamp;
931
932         timestamp = util_timestamp();
933         packet = packet_create_noack(event, "ssdii", handler->common->pkgname, handler->common->id, timestamp, x, y);
934         if (!packet) {
935                 ErrPrint("Failed to build param\n");
936                 return LB_STATUS_ERROR_FAULT;
937         }
938
939         return master_rpc_request_only(handler, packet);
940 }
941
942 static void initialize_livebox(void *disp, int use_thread)
943 {
944 #if defined(FLOG)
945         char filename[BUFSIZ];
946         snprintf(filename, sizeof(filename), "/tmp/%d.box.log", getpid());
947         __file_log_fp = fopen(filename, "w+t");
948         if (!__file_log_fp) {
949                 __file_log_fp = fdopen(1, "w+t");
950         }
951 #endif
952         livebox_service_init();
953         fb_init(disp);
954
955         client_init(use_thread);
956
957         s_info.init_count++;
958 }
959
960 EAPI int livebox_init_with_options(void *disp, int prevent_overwrite, double event_filter, int use_thread)
961 {
962         if (s_info.init_count > 0) {
963                 s_info.init_count++;
964                 return LB_STATUS_SUCCESS;
965         }
966
967         /*!
968          * \note
969          * Some application doesn't want to use the environment value.
970          * So set them using arguments.
971          */
972         s_info.prevent_overwrite = prevent_overwrite;
973         conf_set_event_filter(event_filter);
974
975         initialize_livebox(disp, use_thread);
976         return LB_STATUS_SUCCESS;
977 }
978
979 EAPI int livebox_init(void *disp)
980 {
981         const char *env;
982
983         if (s_info.init_count > 0) {
984                 s_info.init_count++;
985                 return LB_STATUS_SUCCESS;
986         }
987
988         env = getenv("PROVIDER_DISABLE_PREVENT_OVERWRITE");
989         if (env && !strcasecmp(env, "true")) {
990                 s_info.prevent_overwrite = 1;
991         }
992
993         env = getenv("PROVIDER_EVENT_FILTER");
994         if (env) {
995                 double event_filter;
996                 if (sscanf(env, "%lf", &event_filter) == 1) {
997                         conf_set_event_filter(event_filter);
998                 }
999         }
1000
1001         initialize_livebox(disp, 0);
1002         return LB_STATUS_SUCCESS;
1003 }
1004
1005 EAPI int livebox_fini(void)
1006 {
1007         if (s_info.init_count <= 0) {
1008                 ErrPrint("Doesn't initialized\n");
1009                 return LB_STATUS_ERROR_INVALID;
1010         }
1011
1012         s_info.init_count--;
1013         if (s_info.init_count > 0) {
1014                 ErrPrint("init count : %d\n", s_info.init_count);
1015                 return LB_STATUS_SUCCESS;
1016         }
1017
1018         client_fini();
1019         fb_fini();
1020         livebox_service_fini();
1021         return LB_STATUS_SUCCESS;
1022 }
1023
1024 static inline char *lb_pkgname(const char *pkgname)
1025 {
1026         char *lb;
1027
1028         lb = livebox_service_pkgname(pkgname);
1029         if (!lb) {
1030                 if (util_validate_livebox_package(pkgname) == 0) {
1031                         return strdup(pkgname);
1032                 }
1033         }
1034
1035         return lb;
1036 }
1037
1038 static struct livebox_common *find_sharable_common_handle(const char *pkgname, const char *content, int w, int h, const char *cluster, const char *category)
1039 {
1040         struct dlist *l;
1041         struct livebox_common *common;
1042
1043         if (!conf_shared_content()) {
1044                 /*!
1045                  * Shared content option is turnned off.
1046                  */
1047                 return NULL;
1048         }
1049
1050         dlist_foreach(s_info.livebox_common_list, l, common) {
1051                 if (common->state != CREATE) {
1052                         continue;
1053                 }
1054
1055                 if (strcmp(common->pkgname, pkgname)) {
1056                         continue;
1057                 }
1058
1059                 if (strcmp(common->cluster, cluster)) {
1060                         DbgPrint("Cluster mismatched\n");
1061                         continue;
1062                 }
1063
1064                 if (strcmp(common->category, category)) {
1065                         DbgPrint("Category mismatched\n");
1066                         continue;
1067                 }
1068
1069                 if (common->content && content) {
1070                         if (strcmp(common->content, content)) {
1071                                 DbgPrint("%s Content ([%s] <> [%s])\n", common->pkgname, common->content, content);
1072                                 continue;       
1073                         }
1074                 } else {
1075                         int c1_len;
1076                         int c2_len;
1077
1078                         /*!
1079                          * \note
1080                          * We assumes "" (ZERO length string) to NULL
1081                          */
1082                         c1_len = common->content ? strlen(common->content) : 0;
1083                         c2_len = content ? strlen(content) : 0;
1084                         if (c1_len != c2_len) {
1085                                 DbgPrint("%s Content %p <> %p\n", common->pkgname, common->content, content);
1086                                 continue;
1087                         }
1088                 }
1089
1090                 if (common->request.size_changed) {
1091                         DbgPrint("Changing size\n");
1092                         /*!
1093                          * \note
1094                          * Do not re-use resizing instance.
1095                          * We will not use predicted size.
1096                          */
1097                         continue;
1098                 }
1099
1100                 if (common->request.created) {
1101                         DbgPrint("Creating now but re-use it (%s)\n", common->pkgname);
1102                 }
1103
1104                 if (common->lb.width != w || common->lb.height != h) {
1105                         DbgPrint("Size mismatched\n");
1106                         continue;
1107                 }
1108
1109                 DbgPrint("common handle is found: %p\n", common);
1110                 return common;
1111         }
1112
1113         return NULL;
1114 }
1115
1116 /*!
1117  * Just wrapping the livebox_add_with_size function.
1118  */
1119 EAPI struct livebox *livebox_add(const char *pkgname, const char *content, const char *cluster, const char *category, double period, ret_cb_t cb, void *data)
1120 {
1121         return livebox_add_with_size(pkgname, content, cluster, category, period, LB_SIZE_TYPE_UNKNOWN, cb, data);
1122 }
1123
1124 static gboolean job_execute_cb(void *data)
1125 {
1126         struct job_item *item;
1127         struct dlist *l;
1128
1129         l = dlist_nth(s_info.job_list, 0);
1130         if (!l) {
1131                 s_info.job_timer = 0;
1132                 return FALSE;
1133         }
1134
1135         item = dlist_data(l);
1136         s_info.job_list = dlist_remove(s_info.job_list, l);
1137
1138         if (item) {
1139                 item->cb(item->handle, item->ret, item->data);
1140                 lb_unref(item->handle, 1);
1141                 free(item);
1142         }
1143
1144         return TRUE;
1145 }
1146
1147 static int job_add(struct livebox *handle, ret_cb_t job_cb, int ret, void *data)
1148 {
1149         struct job_item *item;
1150
1151         if (!job_cb) {
1152                 ErrPrint("Invalid argument\n");
1153                 return LB_STATUS_ERROR_INVALID;
1154         }
1155
1156         item = malloc(sizeof(*item));
1157         if (!item) {
1158                 ErrPrint("Heap: %s\n", strerror(errno));
1159                 return LB_STATUS_ERROR_MEMORY;
1160         }
1161
1162         item->handle = lb_ref(handle);
1163         item->cb = job_cb;
1164         item->data = data;
1165         item->ret = ret;
1166
1167         s_info.job_list = dlist_append(s_info.job_list, item);
1168
1169         if (!s_info.job_timer) {
1170                 s_info.job_timer = g_timeout_add(1, job_execute_cb, NULL);
1171                 if (!s_info.job_timer) {
1172                         ErrPrint("Failed to create a job timer\n");
1173                 }
1174         }
1175
1176         return LB_STATUS_SUCCESS;
1177 }
1178
1179 static int create_real_instance(struct livebox *handler, ret_cb_t cb, void *data)
1180 {
1181         struct cb_info *cbinfo;
1182         struct packet *packet;
1183         struct livebox_common *common;
1184         int ret;
1185
1186         common = handler->common;
1187
1188         packet = packet_create("new", "dssssdii",
1189                                 common->timestamp, common->pkgname, common->content,
1190                                 common->cluster, common->category,
1191                                 common->lb.period, common->lb.width, common->lb.height);
1192         if (!packet) {
1193                 ErrPrint("Failed to create a new packet\n");
1194                 return LB_STATUS_ERROR_FAULT;
1195         }
1196
1197         cbinfo = create_cb_info(cb, data);
1198         if (!cbinfo) {
1199                 ErrPrint("Failed to create a cbinfo\n");
1200                 packet_destroy(packet);
1201                 return LB_STATUS_ERROR_MEMORY;
1202         }
1203
1204         /*!
1205          * \note
1206          * master_rpc_async_request will destroy the packet (decrease the refcnt)
1207          * So be aware the packet object after return from master_rpc_async_request.
1208          */
1209         ret = master_rpc_async_request(handler, packet, 0, new_ret_cb, cbinfo);
1210         if (ret < 0) {
1211                 ErrPrint("Failed to send a new packet\n");
1212                 destroy_cb_info(cbinfo);
1213                 return LB_STATUS_ERROR_FAULT;
1214         }
1215         handler->common->request.created = 1;
1216         return LB_STATUS_SUCCESS;
1217 }
1218
1219 static void create_cb(struct livebox *handle, int ret, void *data)
1220 {
1221         struct cb_info *cbinfo = data;
1222
1223         if (cbinfo->cb) {
1224                 cbinfo->cb(handle, ret, cbinfo->data);
1225         }
1226
1227         destroy_cb_info(cbinfo);
1228
1229         /*!
1230          * \note
1231          * Forcely generate "updated" event
1232          */
1233         lb_invoke_event_handler(handle, LB_EVENT_LB_UPDATED);
1234 }
1235
1236 static int create_fake_instance(struct livebox *handler, ret_cb_t cb, void *data)
1237 {
1238         struct cb_info *cbinfo;
1239
1240         cbinfo = create_cb_info(cb, data);
1241         if (!cbinfo) {
1242                 ErrPrint("Failed to create a cbinfo\n");
1243                 return LB_STATUS_ERROR_MEMORY;
1244         }
1245
1246         if (job_add(handler, create_cb, LB_STATUS_SUCCESS, cbinfo) != LB_STATUS_SUCCESS) {
1247                 destroy_cb_info(cbinfo);
1248         }
1249
1250         return LB_STATUS_SUCCESS;
1251 }
1252
1253 struct livebox_common *lb_create_common_handle(struct livebox *handle, const char *pkgname, const char *cluster, const char *category)
1254 {
1255         struct livebox_common *common;
1256
1257         common = calloc(1, sizeof(*common));
1258         if (!common) {
1259                 ErrPrint("Heap: %s\n", strerror(errno));
1260                 return NULL;
1261         }
1262
1263         common->pkgname = strdup(pkgname);
1264         if (!common->pkgname) {
1265                 free(common);
1266                 return NULL;
1267         }
1268
1269         common->cluster = strdup(cluster);
1270         if (!common->cluster) {
1271                 ErrPrint("Error: %s\n", strerror(errno));
1272                 free(common->pkgname);
1273                 free(common);
1274                 return NULL;
1275         }
1276
1277         common->category = strdup(category);
1278         if (!common->category) {
1279                 ErrPrint("Error: %s\n", strerror(errno));
1280                 free(common->cluster);
1281                 free(common->pkgname);
1282                 free(common);
1283                 return NULL;
1284         }
1285
1286         /* Data provider will set this */
1287         common->lb.type = _LB_TYPE_FILE;
1288         common->pd.type = _PD_TYPE_SCRIPT;
1289
1290         /* Used for handling the mouse event on a box */
1291         common->lb.mouse_event = livebox_service_mouse_event(common->pkgname);
1292
1293         /* Cluster infomration is not determined yet */
1294         common->nr_of_sizes = 0x01;
1295
1296         common->timestamp = util_timestamp();
1297         common->is_user = 1;
1298         common->delete_type = LB_DELETE_PERMANENTLY;
1299         common->pd.lock = NULL;
1300         common->pd.lock_fd = -1;
1301         common->lb.lock = NULL;
1302         common->lb.lock_fd = -1;
1303
1304         common->state = CREATE;
1305         common->visible = LB_SHOW;
1306
1307         s_info.livebox_common_list = dlist_append(s_info.livebox_common_list, common);
1308         return common;
1309 }
1310
1311 int lb_destroy_common_handle(struct livebox_common *common)
1312 {
1313         dlist_remove_data(s_info.livebox_common_list, common);
1314
1315         common->state = DESTROYED;
1316         free(common->cluster);
1317         free(common->category);
1318         free(common->id);
1319         free(common->pkgname);
1320         free(common->filename);
1321         free(common->lb.auto_launch);
1322         free(common->alt.icon);
1323         free(common->alt.name);
1324
1325         if (common->lb.fb) {
1326                 fb_destroy(common->lb.fb);
1327                 common->lb.fb = NULL;
1328         }
1329
1330         if (common->pd.fb) {
1331                 fb_destroy(common->pd.fb);
1332                 common->pd.fb = NULL;
1333         }
1334
1335         return 0;
1336 }
1337
1338 int lb_common_ref(struct livebox_common *common, struct livebox *handle)
1339 {
1340         common->livebox_list = dlist_append(common->livebox_list, handle);
1341         common->refcnt++;
1342
1343         return common->refcnt;
1344 }
1345
1346 int lb_common_unref(struct livebox_common *common, struct livebox *handle)
1347 {
1348         int refcnt;
1349         dlist_remove_data(common->livebox_list, handle);
1350         refcnt = --common->refcnt;
1351
1352         return refcnt;
1353 }
1354
1355 static void refresh_for_paused_updating_cb(struct livebox *handle, int ret, void *data)
1356 {
1357         if (handle->paused_updating == 0) {
1358                 DbgPrint("Paused updates are cleared\n");
1359                 return;
1360         }
1361
1362         DbgPrint("Pending updates are found\n");
1363         lb_invoke_event_handler(handle, LB_EVENT_LB_UPDATED);
1364 }
1365
1366 static int lb_set_visibility(struct livebox *handler, enum livebox_visible_state state)
1367 {
1368         struct packet *packet;
1369         int need_to_add_job = 0;
1370         int ret;
1371
1372         if (handler->common->visible != LB_SHOW && state == LB_SHOW) {
1373                 need_to_add_job = !!handler->paused_updating;
1374         } else if (handler->common->visible == LB_SHOW && state != LB_SHOW) {
1375                 struct dlist *l;
1376                 struct livebox *item;
1377
1378                 dlist_foreach(handler->common->livebox_list, l, item) {
1379                         if (item->visible == LB_SHOW) {
1380                                 DbgPrint("%s visibility is not changed\n", handler->common->pkgname);
1381                                 return LB_STATUS_SUCCESS;
1382                         }
1383                 }
1384         } else if (handler->common->visible == LB_SHOW && state == LB_SHOW && handler->paused_updating) {
1385                 if (job_add(handler, refresh_for_paused_updating_cb, LB_STATUS_SUCCESS, NULL) < 0) {
1386                         ErrPrint("Unable to add a new job for refreshing box\n");
1387                 }
1388
1389                 return LB_STATUS_SUCCESS;
1390         } else {
1391                 /*!
1392                  * \brief
1393                  * No need to send this to the master
1394                  */
1395                 return LB_STATUS_SUCCESS;
1396         }
1397
1398         packet = packet_create_noack("change,visibility", "ssi", handler->common->pkgname, handler->common->id, (int)state);
1399         if (!packet) {
1400                 ErrPrint("Failed to create a packet\n");
1401                 return LB_STATUS_ERROR_FAULT;
1402         }
1403
1404         ret = master_rpc_request_only(handler, packet);
1405         if (ret == LB_STATUS_SUCCESS) {
1406                 DbgPrint("[%s] visibility is changed 0x[%x]\n", handler->common->pkgname, state);
1407                 handler->common->visible = state;
1408
1409                 if (need_to_add_job) {
1410                         if (job_add(handler, refresh_for_paused_updating_cb, LB_STATUS_SUCCESS, NULL) < 0) {
1411                                 ErrPrint("Unable to add a new job for refreshing box\n");
1412                         }
1413                 }
1414         }
1415
1416         return ret;
1417 }
1418
1419 EAPI struct livebox *livebox_add_with_size(const char *pkgname, const char *content, const char *cluster, const char *category, double period, int type, ret_cb_t cb, void *data)
1420 {
1421         char *lbid;
1422         struct livebox *handler;
1423         int w = 0;
1424         int h = 0;
1425
1426         if (!pkgname || !cluster || !category) {
1427                 ErrPrint("Invalid arguments: pkgname[%p], cluster[%p], category[%p]\n",
1428                                                                 pkgname, cluster, category);
1429                 return NULL;
1430         }
1431
1432         lbid = lb_pkgname(pkgname);
1433         if (!lbid) {
1434                 ErrPrint("Invalid package: %s\n", pkgname);
1435                 return NULL;
1436         }
1437
1438         if (livebox_service_is_enabled(lbid) == 0) {
1439                 DbgPrint("Livebox [%s](%s) is disabled package\n", lbid, pkgname);
1440                 free(lbid);
1441                 return NULL;
1442         }
1443
1444         if (type != LB_SIZE_TYPE_UNKNOWN) {
1445                 (void)livebox_service_get_size(type, &w, &h);
1446         }
1447
1448         handler = calloc(1, sizeof(*handler));
1449         if (!handler) {
1450                 ErrPrint("Error: %s\n", strerror(errno));
1451                 free(lbid);
1452                 return NULL;
1453         }
1454
1455         if (!cb) {
1456                 cb = default_create_cb;
1457         }
1458
1459         handler->common = find_sharable_common_handle(lbid, content, w, h, cluster, category);
1460         if (!handler->common) {
1461                 handler->common = lb_create_common_handle(handler, lbid, cluster, category);
1462                 free(lbid);
1463                 if (!handler->common) {
1464                         ErrPrint("Failed to find common handle\n");
1465                         free(handler);
1466                         return NULL;
1467                 }
1468
1469                 if (!content || !strlen(content)) {
1470                         char *pc;
1471                         /*!
1472                          * \note
1473                          * I know the content should not be modified. use it temporarly without "const"
1474                          */
1475                         pc = livebox_service_content(handler->common->pkgname);
1476                         lb_set_content(handler->common, pc);
1477                         free(pc);
1478                 } else {
1479                         lb_set_content(handler->common, content);
1480                 }
1481
1482                 lb_set_period(handler->common, period);
1483                 lb_set_size(handler->common, w, h);
1484                 lb_common_ref(handler->common, handler);
1485
1486                 if (create_real_instance(handler, cb, data) < 0) {
1487                         if (lb_common_unref(handler->common, handler) == 0) {
1488                                 /*!
1489                                  * Delete common
1490                                  */
1491                                 lb_destroy_common_handle(handler->common);
1492                                 handler->common = NULL;
1493                         }
1494                         free(handler);
1495                         return NULL;
1496                 }
1497         } else {
1498                 free(lbid);
1499
1500                 lb_common_ref(handler->common, handler);
1501
1502                 if (handler->common->request.created) {
1503                         /*!
1504                          * If a box is in creating, wait its result too
1505                          */
1506                         handler->cbs.created.cb = cb;
1507                         handler->cbs.created.data = data;
1508                 } else {
1509                         /*!
1510                          * or fire the fake created_event
1511                          */
1512                         if (create_fake_instance(handler, cb, data) < 0) {
1513                                 if (lb_common_unref(handler->common, handler) == 0) {
1514                                         /*!
1515                                          * Delete common
1516                                          */
1517                                         lb_destroy_common_handle(handler->common);
1518                                 }
1519                                 free(handler);
1520                                 return NULL;
1521                         }
1522                 }
1523         }
1524
1525         handler->visible = LB_SHOW;
1526         handler->state = CREATE;
1527         handler = lb_ref(handler);
1528
1529         if (handler->common->visible != LB_SHOW) {
1530                 lb_set_visibility(handler, LB_SHOW);
1531         }
1532
1533         return handler;
1534 }
1535
1536 EAPI double livebox_period(struct livebox *handler)
1537 {
1538         if (!handler || handler->state != CREATE) {
1539                 ErrPrint("Handler is not valid\n");
1540                 return 0.0f;
1541         }
1542
1543         if (!handler->common || handler->common->state != CREATE) {
1544                 ErrPrint("Invalid handle\n");
1545                 return 0.0f;
1546         }
1547
1548         if (!handler->common->id) {
1549                 ErrPrint("Hnalder is not valid\n");
1550                 return 0.0f;
1551         }
1552
1553         return handler->common->lb.period;
1554 }
1555
1556 EAPI int livebox_set_period(struct livebox *handler, double period, ret_cb_t cb, void *data)
1557 {
1558         struct packet *packet;
1559         int ret;
1560
1561         if (!handler || handler->state != CREATE) {
1562                 ErrPrint("Handler is not valid\n");
1563                 return LB_STATUS_ERROR_INVALID;
1564         }
1565
1566         if (!handler->common || handler->common->state != CREATE) {
1567                 ErrPrint("Invalid handle\n");
1568                 return LB_STATUS_ERROR_INVALID;
1569         }
1570
1571         if (!handler->common->id) {
1572                 ErrPrint("Handler is not valid\n");
1573                 return LB_STATUS_ERROR_INVALID;
1574         }
1575
1576         if (handler->common->request.period_changed) {
1577                 ErrPrint("Previous request for changing period is not finished\n");
1578                 return LB_STATUS_ERROR_BUSY;
1579         }
1580
1581         if (!handler->common->is_user) {
1582                 ErrPrint("CA Livebox is not able to change the period\n");
1583                 return LB_STATUS_ERROR_PERMISSION;
1584         }
1585
1586         if (handler->common->lb.period == period) {
1587                 DbgPrint("No changes\n");
1588                 return LB_STATUS_ERROR_ALREADY;
1589         }
1590
1591         packet = packet_create("set_period", "ssd", handler->common->pkgname, handler->common->id, period);
1592         if (!packet) {
1593                 ErrPrint("Failed to build a packet %s\n", handler->common->pkgname);
1594                 return LB_STATUS_ERROR_FAULT;
1595         }
1596
1597         if (!cb) {
1598                 cb = default_period_changed_cb;
1599         }
1600
1601         ret = master_rpc_async_request(handler, packet, 0, period_ret_cb, NULL);
1602         if (ret == LB_STATUS_SUCCESS) {
1603                 handler->cbs.period_changed.cb = cb;
1604                 handler->cbs.period_changed.data = data;
1605                 handler->common->request.period_changed = 1;
1606         }
1607
1608         return ret;
1609 }
1610
1611 static void lb_update_visibility(struct livebox_common *old_common)
1612 {
1613         struct dlist *l;
1614         struct livebox *item;
1615
1616         item = NULL;
1617         dlist_foreach(old_common->livebox_list, l, item) {
1618                 if (item->visible == LB_SHOW) {
1619                         break;
1620                 }
1621
1622                 item = NULL;
1623         }
1624
1625         if (!item) {
1626                 l = dlist_nth(old_common->livebox_list, 0);
1627                 item = dlist_data(l);
1628
1629                 if (item) {
1630                         lb_set_visibility(item, LB_HIDE_WITH_PAUSE);
1631                 } else {
1632                         ErrPrint("Unable to get the valid handle from common handler\n");
1633                 }
1634         } else {
1635                 lb_set_visibility(item, LB_SHOW);
1636         }
1637 }
1638
1639 /*!
1640  * \note
1641  * The second parameter should be the "return value",
1642  * But in this case, we will use it for "type of deleting instance".
1643  */
1644 static void job_del_cb(struct livebox *handle, int type, void *data)
1645 {
1646         struct cb_info *cbinfo = data;
1647         ret_cb_t cb;
1648
1649         if (handle->visible == LB_SHOW) {
1650                 lb_update_visibility(handle->common);
1651         }
1652
1653         cb = cbinfo->cb;
1654         data = cbinfo->data;
1655         destroy_cb_info(cbinfo);
1656
1657         if (handle->common->state != CREATE) {
1658                 DbgPrint("[%s] %d\n", handle->common->pkgname, handle->refcnt);
1659                 if (cb) {
1660                         cb(handle, LB_STATUS_SUCCESS, data);
1661                 }
1662
1663                 return;
1664         }
1665
1666         if (handle->common->refcnt == 1) {
1667                 handle->common->delete_type = type;
1668                 handle->common->state = DELETE;
1669
1670                 if (!handle->common->id) {
1671                         /*!
1672                          * \note
1673                          * The id is not determined yet.
1674                          * It means a user didn't receive created event yet.
1675                          * Then just stop to delete procedure from here.
1676                          * Because the "created" event handle will release this.
1677                          * By the way, if the user adds any callback for getting return status of this,
1678                          * call it at here.
1679                          */
1680                         if (cb) {
1681                                 cb(handle, LB_STATUS_SUCCESS, data);
1682                         }
1683                 }
1684
1685                 DbgPrint("Send delete request\n");
1686                 lb_send_delete(handle, type, cb, data);
1687         } else {
1688                 if (cb) {
1689                         cb(handle, LB_STATUS_SUCCESS, data);
1690                 }
1691
1692                 DbgPrint("Before unref: %d\n", handle->common->refcnt);
1693                 lb_unref(handle, 1);
1694         }
1695 }
1696
1697 EAPI int livebox_del_NEW(struct livebox *handler, int type, ret_cb_t cb, void *data)
1698 {
1699         struct cb_info *cbinfo;
1700
1701         if (!handler) {
1702                 ErrPrint("Handler is NIL\n");
1703                 return LB_STATUS_ERROR_INVALID;
1704         }
1705
1706         if (handler->state != CREATE) {
1707                 ErrPrint("Handler is already deleted\n");
1708                 return LB_STATUS_ERROR_INVALID;
1709         }
1710
1711         handler->state = DELETE;
1712
1713         cbinfo = create_cb_info(cb, data);
1714         if (!cbinfo) {
1715                 ErrPrint("Failed to create a cbinfo\n");
1716                 return LB_STATUS_ERROR_MEMORY;
1717         }
1718
1719         if (job_add(handler, job_del_cb, type, cbinfo) != LB_STATUS_SUCCESS) {
1720                 ErrPrint("Failed to add a new job\n");
1721                 destroy_cb_info(cbinfo);
1722                 return LB_STATUS_ERROR_FAULT;
1723         }
1724
1725         return LB_STATUS_SUCCESS;
1726 }
1727
1728 EAPI int livebox_del(struct livebox *handler, ret_cb_t cb, void *data)
1729 {
1730         return livebox_del_NEW(handler, LB_DELETE_PERMANENTLY, cb, data);
1731 }
1732
1733 EAPI int livebox_set_fault_handler(int (*cb)(enum livebox_fault_type, const char *, const char *, const char *, void *), void *data)
1734 {
1735         struct fault_info *info;
1736
1737         if (!cb) {
1738                 return LB_STATUS_ERROR_INVALID;
1739         }
1740
1741         info = malloc(sizeof(*info));
1742         if (!info) {
1743                 ErrPrint("Heap: %s\n", strerror(errno));
1744                 return LB_STATUS_ERROR_MEMORY;
1745         }
1746
1747         info->handler = cb;
1748         info->user_data = data;
1749         info->is_deleted = 0;
1750
1751         s_info.fault_list = dlist_append(s_info.fault_list, info);
1752         return LB_STATUS_SUCCESS;
1753 }
1754
1755 EAPI void *livebox_unset_fault_handler(int (*cb)(enum livebox_fault_type, const char *, const char *, const char *, void *))
1756 {
1757         struct fault_info *info;
1758         struct dlist *l;
1759
1760         dlist_foreach(s_info.fault_list, l, info) {
1761                 if (info->handler == cb) {
1762                         void *data;
1763
1764                         data = info->user_data;
1765
1766                         if (s_info.fault_state == INFO_STATE_CALLBACK_IN_PROCESSING) {
1767                                 info->is_deleted = 1;
1768                         } else {
1769                                 s_info.fault_list = dlist_remove(s_info.fault_list, l);
1770                                 free(info);
1771                         }
1772
1773                         return data;
1774                 }
1775         }
1776
1777         return NULL;
1778 }
1779
1780 EAPI int livebox_set_event_handler(int (*cb)(struct livebox *, enum livebox_event_type, void *), void *data)
1781 {
1782         struct event_info *info;
1783
1784         if (!cb) {
1785                 ErrPrint("Invalid argument cb is nil\n");
1786                 return LB_STATUS_ERROR_INVALID;
1787         }
1788
1789         info = malloc(sizeof(*info));
1790         if (!info) {
1791                 ErrPrint("Heap: %s\n", strerror(errno));
1792                 return LB_STATUS_ERROR_MEMORY;
1793         }
1794
1795         info->handler = cb;
1796         info->user_data = data;
1797         info->is_deleted = 0;
1798
1799         s_info.event_list = dlist_append(s_info.event_list, info);
1800         return LB_STATUS_SUCCESS;
1801 }
1802
1803 EAPI void *livebox_unset_event_handler(int (*cb)(struct livebox *, enum livebox_event_type, void *))
1804 {
1805         struct event_info *info;
1806         struct dlist *l;
1807
1808         dlist_foreach(s_info.event_list, l, info) {
1809                 if (info->handler == cb) {
1810                         void *data;
1811
1812                         data = info->user_data;
1813
1814                         if (s_info.event_state == INFO_STATE_CALLBACK_IN_PROCESSING) {
1815                                 info->is_deleted = 1;
1816                         } else {
1817                                 s_info.event_list = dlist_remove(s_info.event_list, l);
1818                                 free(info);
1819                         }
1820
1821                         return data;
1822                 }
1823         }
1824
1825         return NULL;
1826 }
1827
1828 EAPI int livebox_set_update_mode(struct livebox *handler, int active_update, ret_cb_t cb, void *data)
1829 {
1830         struct packet *packet;
1831         int ret;
1832
1833         if (!handler || handler->state != CREATE) {
1834                 ErrPrint("Handler is Invalid\n");
1835                 return LB_STATUS_ERROR_INVALID;
1836         }
1837
1838         if (!handler->common || handler->common->state != CREATE) {
1839                 ErrPrint("Handler is Invalid\n");
1840                 return LB_STATUS_ERROR_INVALID;
1841         }
1842
1843         if (!handler->common->id) {
1844                 ErrPrint("Handler is Invalid\n");
1845                 return LB_STATUS_ERROR_INVALID;
1846         }
1847
1848         if (handler->common->request.update_mode) {
1849                 ErrPrint("Previous update_mode cb is not finished yet\n");
1850                 return LB_STATUS_ERROR_BUSY;
1851         }
1852
1853         if (handler->common->is_active_update == active_update) {
1854                 return LB_STATUS_ERROR_ALREADY;
1855         }
1856
1857         if (!handler->common->is_user) {
1858                 return LB_STATUS_ERROR_PERMISSION;
1859         }
1860
1861         packet = packet_create("update_mode", "ssi", handler->common->pkgname, handler->common->id, active_update);
1862         if (!packet) {
1863                 return LB_STATUS_ERROR_FAULT;
1864         }
1865
1866         if (!cb) {
1867                 cb = default_update_mode_cb;
1868         }
1869
1870         ret = master_rpc_async_request(handler, packet, 0, update_mode_cb, NULL);
1871         if (ret == LB_STATUS_SUCCESS) {
1872                 handler->cbs.update_mode.cb = cb;
1873                 handler->cbs.update_mode.data = data;
1874                 handler->common->request.update_mode = 1;
1875         }
1876
1877         return ret;
1878 }
1879
1880 EAPI int livebox_is_active_update(struct livebox *handler)
1881 {
1882         if (!handler || handler->state != CREATE) {
1883                 ErrPrint("Handler is Invalid\n");
1884                 return LB_STATUS_ERROR_INVALID;
1885         }
1886
1887         if (!handler->common || handler->common->state != CREATE) {
1888                 ErrPrint("Handler is Invalid\n");
1889                 return LB_STATUS_ERROR_INVALID;
1890         }
1891
1892         if (!handler->common->id) {
1893                 return LB_STATUS_ERROR_INVALID;
1894         }
1895
1896         return handler->common->is_active_update;
1897 }
1898
1899 static void resize_job_cb(struct livebox *handler, int ret, void *data)
1900 {
1901         struct cb_info *info = data;
1902
1903         if (info->cb) {
1904                 info->cb(handler, ret, info->data);
1905         }
1906
1907         free(info);
1908
1909         /*!
1910          * \note
1911          * Forcely update the box
1912          */
1913         lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
1914 }
1915
1916 EAPI int livebox_resize(struct livebox *handler, int type, ret_cb_t cb, void *data)
1917 {
1918         struct livebox_common *common;
1919         int w;
1920         int h;
1921         int ret;
1922
1923         /*!
1924          * \TODO
1925          * If this handle is host instance or link instance,
1926          * Create a new instance or find another linkable instance.
1927          */
1928
1929         if (!handler || handler->state != CREATE) {
1930                 ErrPrint("Handler is not valid\n");
1931                 return LB_STATUS_ERROR_INVALID;
1932         }
1933
1934         if (!handler->common || handler->common->state != CREATE) {
1935                 ErrPrint("Invalid handle\n");
1936                 return LB_STATUS_ERROR_INVALID;
1937         }
1938
1939         if (!handler->common->id) {
1940                 ErrPrint("Handler is not valid\n");
1941                 return LB_STATUS_ERROR_INVALID;
1942         }
1943
1944         /*!
1945          * \note
1946          * resize operation should be separated by each handler.
1947          * If a handler is resizing, the other handler can request resize too.
1948          * So we should not use the common->request.size_changed flag.
1949          */
1950         if (handler->cbs.size_changed.cb) {
1951                 ErrPrint("Previous resize request is not finished yet\n");
1952                 return LB_STATUS_ERROR_BUSY;
1953         }
1954
1955         if (livebox_service_get_size(type, &w, &h) != 0) {
1956                 ErrPrint("Invalid size type\n");
1957                 return LB_STATUS_ERROR_INVALID;
1958         }
1959
1960         if (handler->common->lb.width == w && handler->common->lb.height == h) {
1961                 DbgPrint("No changes\n");
1962                 return LB_STATUS_ERROR_ALREADY;
1963         }
1964
1965         if (!handler->common->is_user) {
1966                 ErrPrint("CA Livebox is not able to be resized\n");
1967                 return LB_STATUS_ERROR_PERMISSION;
1968         }
1969
1970         if (handler->common->refcnt <= 1) {
1971                 struct packet *packet;
1972
1973                 /* Only 1 instance */
1974                 packet = packet_create("resize", "ssii", handler->common->pkgname, handler->common->id, w, h);
1975                 if (!packet) {
1976                         ErrPrint("Failed to build param\n");
1977                         return LB_STATUS_ERROR_FAULT;
1978                 }
1979
1980                 if (!cb) {
1981                         cb = default_lb_size_changed_cb;
1982                 }
1983
1984                 ret = master_rpc_async_request(handler, packet, 0, resize_cb, NULL);
1985                 if (ret == LB_STATUS_SUCCESS) {
1986                         handler->cbs.size_changed.cb = cb;
1987                         handler->cbs.size_changed.data = data;
1988                         handler->common->request.size_changed = 1;
1989                 }
1990         } else {
1991                 common = find_sharable_common_handle(handler->common->pkgname, handler->common->content, w, h, handler->common->cluster, handler->common->category);
1992                 if (!common) {
1993                         struct livebox_common *old_common;
1994                         /*!
1995                          * \note
1996                          * If the common handler is in resizing,
1997                          * if user tries to resize a hander, then simply create new one even if the requested size is same with this.
1998
1999                         if (handler->common->request.size_changed) {
2000                         }
2001
2002                          */
2003
2004                         old_common = handler->common;
2005
2006                         common = lb_create_common_handle(handler, old_common->pkgname, old_common->cluster, old_common->category);
2007                         if (!common) {
2008                                 ErrPrint("Failed to create common handle\n");
2009                                 return LB_STATUS_ERROR_FAULT;
2010                         }
2011
2012                         lb_set_size(common, w, h);
2013                         lb_set_content(common, old_common->content);
2014                         lb_set_period(common, old_common->lb.period);
2015
2016                         /*!
2017                          * \note
2018                          * Disconnecting from old one.
2019                          */
2020                         if (lb_common_unref(old_common, handler) == 0) {
2021                                 /*!
2022                                  * \note
2023                                  * Impossible
2024                                  */
2025                                 ErrPrint("Common has no associated handler\n");
2026                         }
2027
2028                         lb_common_ref(common, handler);
2029
2030                         /*!
2031                          * Connect to a new one
2032                          */
2033                         handler->common = common;
2034
2035                         /*!
2036                          * \TODO
2037                          * Need to care, if it fails to create a common handle,
2038                          * the resize operation will be failed.
2039                          * in that case, we should reuse the old common handle
2040                          */
2041                         ret = create_real_instance(handler, cb, data);
2042                         if (ret < 0) {
2043                                 lb_common_unref(common, handler);
2044                                 lb_destroy_common_handle(common);
2045
2046                                 lb_common_ref(old_common, handler);
2047                                 handler->common = old_common;
2048                         } else {
2049                                 /*!
2050                                  * In this case, we should update visibility of old_common's liveboxes
2051                                  */
2052                                 if (handler->visible == LB_SHOW) {
2053                                         lb_update_visibility(old_common);
2054                                 }
2055                         }
2056                 } else {
2057                         struct cb_info *cbinfo;
2058
2059                         cbinfo = create_cb_info(cb, data);
2060                         if (!cbinfo) {
2061                                 ErrPrint("Failed to create a cbinfo\n");
2062                                 ret = LB_STATUS_ERROR_MEMORY;
2063                         } else {
2064                                 ret = job_add(handler, resize_job_cb, LB_STATUS_SUCCESS, cbinfo);
2065                                 if (ret == LB_STATUS_SUCCESS) {
2066                                         struct livebox_common *old_common;
2067
2068                                         old_common = handler->common;
2069
2070                                         if (lb_common_unref(handler->common, handler) == 0) {
2071                                                 ErrPrint("Old common has no associated handler\n");
2072                                         }
2073
2074                                         lb_common_ref(common, handler);
2075                                         handler->common = common;
2076
2077                                         if (handler->visible == LB_SHOW) {
2078                                                 lb_update_visibility(old_common); /* To update visibility: Show --> Paused */
2079                                                 lb_update_visibility(common);   /* To update visibility: Paused --> Show */
2080                                         }
2081                                 } else {
2082                                         destroy_cb_info(cbinfo);
2083                                 }
2084                         }
2085                 }
2086         }
2087
2088         return ret;
2089 }
2090
2091 EAPI int livebox_click(struct livebox *handler, double x, double y)
2092 {
2093         struct packet *packet;
2094         double timestamp;
2095         int ret;
2096
2097         if (!handler || handler->state != CREATE) {
2098                 ErrPrint("Handler is invalid\n");
2099                 return LB_STATUS_ERROR_INVALID;
2100         }
2101
2102         if (!handler->common || handler->common->state != CREATE) {
2103                 ErrPrint("Handler is invalid\n");
2104                 return LB_STATUS_ERROR_INVALID;
2105         }
2106
2107         if (!handler->common->id) {
2108                 ErrPrint("Handler is not valid\n");
2109                 return LB_STATUS_ERROR_INVALID;
2110         }
2111
2112         if (handler->common->lb.auto_launch) {
2113 /*
2114                 service_h service;
2115
2116                 DbgPrint("AUTO_LAUNCH [%s]\n", handler->common->lb.auto_launch);
2117
2118                 ret = service_create(&service);
2119                 if (ret == SERVICE_ERROR_NONE) {
2120                         service_set_package(service, handler->common->lb.auto_launch);
2121                         service_send_launch_request(service, NULL, NULL);
2122                         service_destroy(service);
2123                 } else {
2124                         ErrPrint("Failed to launch an app %s (%d)\n", handler->common->lb.auto_launch, ret);
2125                 }
2126 */
2127                 ret = aul_launch_app(handler->common->lb.auto_launch, NULL);
2128                 if (ret <= 0) {
2129                         ErrPrint("Failed to launch an app %s (%d)\n", handler->common->lb.auto_launch, ret);
2130                 }
2131         }
2132
2133         timestamp = util_timestamp();
2134         DbgPrint("CLICKED: %lf\n", timestamp);
2135
2136         packet = packet_create_noack("clicked", "sssddd", handler->common->pkgname, handler->common->id, "clicked", timestamp, x, y);
2137         if (!packet) {
2138                 ErrPrint("Failed to build param\n");
2139                 return LB_STATUS_ERROR_FAULT;
2140         }
2141
2142         ret = master_rpc_request_only(handler, packet);
2143
2144         if (!handler->common->lb.mouse_event && (handler->common->lb.type == _LB_TYPE_BUFFER || handler->common->lb.type == _LB_TYPE_SCRIPT)) {
2145                 int ret; /* Shadow variable */
2146                 ret = send_mouse_event(handler, "lb_mouse_down", x * handler->common->lb.width, y * handler->common->lb.height);
2147                 if (ret < 0) {
2148                         ErrPrint("Failed to send Down: %d\n", ret);
2149                 }
2150
2151                 ret = send_mouse_event(handler, "lb_mouse_move", x * handler->common->lb.width, y * handler->common->lb.height);
2152                 if (ret < 0) {
2153                         ErrPrint("Failed to send Move: %d\n", ret);
2154                 }
2155
2156                 ret = send_mouse_event(handler, "lb_mouse_up", x * handler->common->lb.width, y * handler->common->lb.height);
2157                 if (ret < 0) {
2158                         ErrPrint("Failed to send Up: %d\n", ret);
2159                 }
2160         }
2161
2162         return ret;
2163 }
2164
2165 EAPI int livebox_has_pd(struct livebox *handler)
2166 {
2167         if (!handler || handler->state != CREATE) {
2168                 ErrPrint("Handler is invalid\n");
2169                 return LB_STATUS_ERROR_INVALID;
2170         }
2171
2172         if (!handler->common || handler->common->state != CREATE) {
2173                 ErrPrint("Handler is invalid\n");
2174                 return LB_STATUS_ERROR_INVALID;
2175         }
2176
2177         if (!handler->common->id) {
2178                 ErrPrint("Handler is not valid\n");
2179                 return LB_STATUS_ERROR_INVALID;
2180         }
2181
2182         return !!handler->common->pd.fb;
2183 }
2184
2185 EAPI int livebox_pd_is_created(struct livebox *handler)
2186 {
2187         if (!handler || handler->state != CREATE) {
2188                 ErrPrint("Handler is invalid\n");
2189                 return LB_STATUS_ERROR_INVALID;
2190         }
2191
2192         if (!handler->common || handler->common->state != CREATE) {
2193                 ErrPrint("Handler is invalid\n");
2194                 return LB_STATUS_ERROR_INVALID;
2195         }
2196
2197         if (!handler->common->pd.fb || !handler->common->id) {
2198                 ErrPrint("Handler is not valid\n");
2199                 return LB_STATUS_ERROR_INVALID;
2200         }
2201
2202         return handler->common->is_pd_created;
2203 }
2204
2205 EAPI int livebox_create_pd(struct livebox *handler, ret_cb_t cb, void *data)
2206 {
2207         return livebox_create_pd_with_position(handler, -1.0, -1.0, cb, data);
2208 }
2209
2210 static void turn_off_pd_destroyed_flag_cb(struct livebox *handler, int ret, void *data)
2211 {
2212         if (handler->common->request.pd_destroyed) {
2213                 ret_cb_t cb;
2214                 void *data;
2215
2216                 DbgPrint("pd_destroyed request is canceled\n");
2217                 handler->common->request.pd_destroyed = 0;
2218                 cb = handler->cbs.pd_destroyed.cb;
2219                 data = handler->cbs.pd_destroyed.data;
2220                 handler->cbs.pd_destroyed.cb = NULL;
2221                 handler->cbs.pd_destroyed.data = NULL;
2222
2223                 if (cb) {
2224                         cb(handler, ret, data);
2225                 }
2226         }
2227 }
2228
2229 EAPI int livebox_create_pd_with_position(struct livebox *handler, double x, double y, ret_cb_t cb, void *data)
2230 {
2231         struct packet *packet;
2232         int ret;
2233
2234         if (!handler || handler->state != CREATE) {
2235                 ErrPrint("Handler is invalid\n");
2236                 return LB_STATUS_ERROR_INVALID;
2237         }
2238
2239         if (!handler->common || handler->common->state != CREATE) {
2240                 ErrPrint("Handler is invalid\n");
2241                 return LB_STATUS_ERROR_INVALID;
2242         }
2243
2244         if (!handler->common->pd.fb || !handler->common->id) {
2245                 ErrPrint("Handler is not valid\n");
2246                 return LB_STATUS_ERROR_INVALID;
2247         }
2248
2249         /*!
2250          * \note
2251          * Only one handler can have a PD
2252          */
2253         if (handler->common->is_pd_created) {
2254                 DbgPrint("PD is already created\n");
2255                 return LB_STATUS_SUCCESS;
2256         }
2257
2258         if (handler->common->request.pd_created) {
2259                 ErrPrint("Previous request is not completed yet\n");
2260                 return LB_STATUS_ERROR_BUSY;
2261         }
2262
2263         /*!
2264          * \note
2265          * Turn off the pd_destroyed request flag
2266          */
2267         if (handler->common->request.pd_destroyed) {
2268                 if (job_add(handler, turn_off_pd_destroyed_flag_cb, LB_STATUS_ERROR_CANCEL, NULL) < 0) {
2269                         ErrPrint("Failed to add pd_destroyed job\n");
2270                 }
2271         }
2272
2273         packet = packet_create("create_pd", "ssdd", handler->common->pkgname, handler->common->id, x, y);
2274         if (!packet) {
2275                 ErrPrint("Failed to build param\n");
2276                 return LB_STATUS_ERROR_FAULT;
2277         }
2278
2279         if (!cb) {
2280                 cb = default_pd_created_cb;
2281         }
2282
2283         DbgPrint("PERF_DBOX\n");
2284         ret = master_rpc_async_request(handler, packet, 0, pd_create_cb, NULL);
2285         if (ret == LB_STATUS_SUCCESS) {
2286                 handler->cbs.pd_created.cb = cb;
2287                 handler->cbs.pd_created.data = data;
2288                 handler->common->request.pd_created = 1;
2289         }
2290
2291         return ret;
2292 }
2293
2294 EAPI int livebox_move_pd(struct livebox *handler, double x, double y)
2295 {
2296         struct packet *packet;
2297
2298         if (!handler || handler->state != CREATE) {
2299                 ErrPrint("Handler is invalid\n");
2300                 return LB_STATUS_ERROR_INVALID;
2301         }
2302
2303         if (!handler->common || handler->common->state != CREATE) {
2304                 ErrPrint("Handler is invalid\n");
2305                 return LB_STATUS_ERROR_INVALID;
2306         }
2307
2308         if (!handler->common->pd.fb || !handler->common->id) {
2309                 ErrPrint("Handler is not valid\n");
2310                 return LB_STATUS_ERROR_INVALID;
2311         }
2312
2313         if (!handler->common->is_pd_created) {
2314                 ErrPrint("PD is not created\n");
2315                 return LB_STATUS_ERROR_INVALID;
2316         }
2317
2318         packet = packet_create_noack("pd_move", "ssdd", handler->common->pkgname, handler->common->id, x, y);
2319         if (!packet) {
2320                 ErrPrint("Failed to build param\n");
2321                 return LB_STATUS_ERROR_FAULT;
2322         }
2323
2324         return master_rpc_request_only(handler, packet);
2325 }
2326
2327 EAPI int livebox_activate(const char *pkgname, ret_cb_t cb, void *data)
2328 {
2329         struct packet *packet;
2330         struct cb_info *cbinfo;
2331         int ret;
2332
2333         if (!pkgname) {
2334                 return LB_STATUS_ERROR_INVALID;
2335         }
2336
2337         packet = packet_create("activate_package", "s", pkgname);
2338         if (!packet) {
2339                 ErrPrint("Failed to build a param\n");
2340                 return LB_STATUS_ERROR_FAULT;
2341         }
2342
2343         cbinfo = create_cb_info(cb, data);
2344         if (!cbinfo) {
2345                 ErrPrint("Unable to create cbinfo\n");
2346                 packet_destroy(packet);
2347                 return LB_STATUS_ERROR_FAULT;
2348         }
2349
2350         ret = master_rpc_async_request(NULL, packet, 0, activated_cb, cbinfo);
2351         if (ret < 0) {
2352                 destroy_cb_info(cbinfo);
2353         }
2354
2355         return ret;
2356 }
2357
2358 static void turn_off_pd_created_flag_cb(struct livebox *handler, int ret, void *data)
2359 {
2360         if (handler->common->request.pd_created) {
2361                 ret_cb_t cb;
2362                 void *data;
2363
2364                 DbgPrint("pd_created request is canceled\n");
2365                 handler->common->request.pd_created = 0;
2366                 cb = handler->cbs.pd_created.cb;
2367                 data = handler->cbs.pd_created.data;
2368                 handler->cbs.pd_created.cb = NULL;
2369                 handler->cbs.pd_created.data = NULL;
2370
2371                 if (cb) {
2372                         cb(handler, ret, data);
2373                 }
2374         }
2375 }
2376
2377 EAPI int livebox_destroy_pd(struct livebox *handler, ret_cb_t cb, void *data)
2378 {
2379         struct packet *packet;
2380         struct cb_info *cbinfo;
2381         int ret;
2382
2383         if (!handler || handler->state != CREATE) {
2384                 ErrPrint("Handler is invalid\n");
2385                 return LB_STATUS_ERROR_INVALID;
2386         }
2387
2388         if (!handler->common || handler->common->state != CREATE) {
2389                 ErrPrint("Handler is invalid\n");
2390                 return LB_STATUS_ERROR_INVALID;
2391         }
2392
2393         if (!handler->common->pd.fb || !handler->common->id) {
2394                 ErrPrint("Handler is not valid\n");
2395                 return LB_STATUS_ERROR_INVALID;
2396         }
2397
2398         /*!
2399          * \FIXME
2400          * Replace the callback check code.
2401          * Use the flag instead of callback.
2402          * the flag should be in the ADT "common"
2403          */
2404         if (!handler->common->is_pd_created && !handler->common->request.pd_created) {
2405                 ErrPrint("PD is not created\n");
2406                 return LB_STATUS_ERROR_INVALID;
2407         }
2408
2409         if (handler->common->request.pd_destroyed) {
2410                 ErrPrint("PD destroy request is already sent\n");
2411                 return LB_STATUS_ERROR_ALREADY;
2412         }
2413
2414         /*!
2415          * \note
2416          * Disable the pd_created request flag
2417          */
2418         if (handler->common->request.pd_created) {
2419                 if (job_add(handler, turn_off_pd_created_flag_cb, LB_STATUS_ERROR_CANCEL, NULL) < 0) {
2420                         ErrPrint("Failed to add a new job\n");
2421                 }
2422         }
2423
2424         DbgPrint("[%s]\n", handler->common->pkgname);
2425
2426         packet = packet_create("destroy_pd", "ss", handler->common->pkgname, handler->common->id);
2427         if (!packet) {
2428                 ErrPrint("Failed to build a param\n");
2429                 return LB_STATUS_ERROR_FAULT;
2430         }
2431
2432         if (!cb) {
2433                 cb = default_pd_destroyed_cb;
2434         }
2435
2436         cbinfo = create_cb_info(cb, data);
2437         if (!cbinfo) {
2438                 packet_destroy(packet);
2439                 return LB_STATUS_ERROR_FAULT;
2440         }
2441
2442         ret = master_rpc_async_request(handler, packet, 0, pd_destroy_cb, cbinfo);
2443         if (ret < 0) {
2444                 destroy_cb_info(cbinfo);
2445         } else {
2446                 handler->common->request.pd_destroyed = 1;
2447         }
2448
2449         return ret;
2450 }
2451
2452 EAPI int livebox_access_event(struct livebox *handler, enum access_event_type type, double x, double y, ret_cb_t cb, void *data)
2453 {
2454         int w = 1;
2455         int h = 1;
2456         char cmd[32] = { '\0', };
2457         char *ptr = cmd;
2458         int ret;
2459
2460         if (!handler || handler->state != CREATE) {
2461                 ErrPrint("Handler is invalid\n");
2462                 return LB_STATUS_ERROR_INVALID;
2463         }
2464
2465         if (!handler->common || handler->common->state != CREATE) {
2466                 ErrPrint("Handler is invalid\n");
2467                 return LB_STATUS_ERROR_INVALID;
2468         }
2469
2470         if (!handler->common->id) {
2471                 ErrPrint("Handler is not valid\n");
2472                 return LB_STATUS_ERROR_INVALID;
2473         }
2474
2475         if (handler->common->request.access_event) {
2476                 ErrPrint("Previous access event is not yet done\n");
2477                 return LB_STATUS_ERROR_BUSY;
2478         }
2479
2480         if (type & ACCESS_EVENT_PD_MASK) {
2481                 if (!handler->common->is_pd_created) {
2482                         ErrPrint("PD is not created\n");
2483                         return LB_STATUS_ERROR_INVALID;
2484                 }
2485                 *ptr++ = 'p';
2486                 *ptr++ = 'd';
2487                 w = handler->common->pd.width;
2488                 h = handler->common->pd.height;
2489         } else if (type & ACCESS_EVENT_LB_MASK) {
2490                 *ptr++ = 'l';
2491                 *ptr++ = 'b';
2492                 w = handler->common->lb.width;
2493                 h = handler->common->lb.height;
2494         } else {
2495                 ErrPrint("Invalid event type\n");
2496                 return LB_STATUS_ERROR_INVALID;
2497         }
2498
2499         switch (type & ~ACCESS_EVENT_PD_MASK) {
2500         case ACCESS_EVENT_HIGHLIGHT:
2501                 strcpy(ptr, "_access_hl");
2502                 break;
2503         case ACCESS_EVENT_HIGHLIGHT_NEXT:
2504                 strcpy(ptr, "_access_hl_next");
2505                 break;
2506         case ACCESS_EVENT_HIGHLIGHT_PREV:
2507                 strcpy(ptr, "_access_hl_prev");
2508                 break;
2509         case ACCESS_EVENT_ACTIVATE:
2510                 strcpy(ptr, "_access_activate");
2511                 break;
2512         case ACCESS_EVENT_ACTION_DOWN:
2513                 strcpy(ptr, "_access_action_down");
2514                 break;
2515         case ACCESS_EVENT_ACTION_UP:
2516                 strcpy(ptr, "_access_action_up");
2517                 break;
2518         case ACCESS_EVENT_UNHIGHLIGHT:
2519                 strcpy(ptr, "_access_unhighlight");
2520                 break;
2521         case ACCESS_EVENT_SCROLL_DOWN:
2522                 strcpy(ptr, "_access_scroll_down");
2523                 break;
2524         case ACCESS_EVENT_SCROLL_MOVE:
2525                 strcpy(ptr, "_access_scroll_move");
2526                 break;
2527         case ACCESS_EVENT_SCROLL_UP:
2528                 strcpy(ptr, "_access_scroll_up");
2529                 break;
2530         default:
2531                 return LB_STATUS_ERROR_INVALID;
2532         }
2533
2534         if (!cb) {
2535                 cb = default_access_event_cb;
2536         }
2537
2538         ret = send_access_event(handler, cmd, x * w, y * h);
2539         if (ret == LB_STATUS_SUCCESS) {
2540                 handler->cbs.access_event.cb = cb;
2541                 handler->cbs.access_event.data = data;
2542                 handler->common->request.access_event = 1;
2543         }
2544
2545         return ret;
2546 }
2547
2548 EAPI int livebox_content_event(struct livebox *handler, enum content_event_type type, double x, double y)
2549 {
2550         return livebox_mouse_event(handler, type, x, y);
2551 }
2552
2553 EAPI int livebox_mouse_event(struct livebox *handler, enum content_event_type type, double x, double y)
2554 {
2555         int w = 1;
2556         int h = 1;
2557         char cmd[32] = { '\0', };
2558         char *ptr = cmd;
2559
2560         if (!handler || handler->state != CREATE) {
2561                 ErrPrint("Handler is invalid\n");
2562                 return LB_STATUS_ERROR_INVALID;
2563         }
2564
2565         if (!handler->common || handler->common->state != CREATE) {
2566                 ErrPrint("Handler is invalid\n");
2567                 return LB_STATUS_ERROR_INVALID;
2568         }
2569
2570         if (!handler->common->id) {
2571                 ErrPrint("Handler is not valid\n");
2572                 return LB_STATUS_ERROR_INVALID;
2573         }
2574
2575         if (!(type & CONTENT_EVENT_MOUSE_MASK)) {
2576                 ErrPrint("Invalid content event is used\n");
2577                 return LB_STATUS_ERROR_INVALID;
2578         }
2579
2580         if (type & CONTENT_EVENT_PD_MASK) {
2581                 int flag = 1;
2582
2583                 if (!handler->common->is_pd_created) {
2584                         ErrPrint("PD is not created\n");
2585                         return LB_STATUS_ERROR_INVALID;
2586                 }
2587
2588                 if (!handler->common->pd.fb) {
2589                         ErrPrint("Handler is not valid\n");
2590                         return LB_STATUS_ERROR_INVALID;
2591                 }
2592
2593                 if (type & CONTENT_EVENT_MOUSE_MOVE) {
2594                         if (fabs(x - handler->common->pd.x) < conf_event_filter() && fabs(y - handler->common->pd.y) < conf_event_filter()) {
2595                                 return LB_STATUS_ERROR_BUSY;
2596                         }
2597                 } else if (type & CONTENT_EVENT_MOUSE_SET) {
2598                         flag = 0;
2599                 }
2600
2601                 if (flag) {
2602                         w = handler->common->pd.width;
2603                         h = handler->common->pd.height;
2604                         handler->common->pd.x = x;
2605                         handler->common->pd.y = y;
2606                 }
2607                 *ptr++ = 'p';
2608                 *ptr++ = 'd';
2609         } else if (type & CONTENT_EVENT_LB_MASK) {
2610                 int flag = 1;
2611
2612                 if (!handler->common->lb.mouse_event) {
2613                         return LB_STATUS_ERROR_INVALID;
2614                 }
2615
2616                 if (!handler->common->lb.fb) {
2617                         ErrPrint("Handler is not valid\n");
2618                         return LB_STATUS_ERROR_INVALID;
2619                 }
2620
2621                 if (type & CONTENT_EVENT_MOUSE_MOVE) {
2622                         if (fabs(x - handler->common->lb.x) < conf_event_filter() && fabs(y - handler->common->lb.y) < conf_event_filter()) {
2623                                 return LB_STATUS_ERROR_BUSY;
2624                         }
2625                 } else if (type & CONTENT_EVENT_MOUSE_SET) {
2626                         flag = 0;
2627                 }
2628
2629                 if (flag) {
2630                         w = handler->common->lb.width;
2631                         h = handler->common->lb.height;
2632                         handler->common->lb.x = x;
2633                         handler->common->lb.y = y;
2634                 }
2635                 *ptr++ = 'l';
2636                 *ptr++ = 'b';
2637         } else {
2638                 ErrPrint("Invalid event type\n");
2639                 return LB_STATUS_ERROR_INVALID;
2640         }
2641
2642         /*!
2643          * Must be short than 29 bytes.
2644          */
2645         switch ((type & ~(CONTENT_EVENT_PD_MASK | CONTENT_EVENT_LB_MASK))) {
2646         case CONTENT_EVENT_MOUSE_ENTER | CONTENT_EVENT_MOUSE_MASK:
2647                 strcpy(ptr, "_mouse_enter");
2648                 break;
2649         case CONTENT_EVENT_MOUSE_LEAVE | CONTENT_EVENT_MOUSE_MASK:
2650                 strcpy(ptr, "_mouse_leave");
2651                 break;
2652         case CONTENT_EVENT_MOUSE_UP | CONTENT_EVENT_MOUSE_MASK:
2653                 strcpy(ptr, "_mouse_up");
2654                 break;
2655         case CONTENT_EVENT_MOUSE_DOWN | CONTENT_EVENT_MOUSE_MASK:
2656                 strcpy(ptr, "_mouse_down");
2657                 break;
2658         case CONTENT_EVENT_MOUSE_MOVE | CONTENT_EVENT_MOUSE_MASK:
2659                 strcpy(ptr, "_mouse_move");
2660                 break;
2661         case CONTENT_EVENT_MOUSE_SET | CONTENT_EVENT_MOUSE_MASK:
2662                 strcpy(ptr, "_mouse_set");
2663                 break;
2664         case CONTENT_EVENT_MOUSE_UNSET | CONTENT_EVENT_MOUSE_MASK:
2665                 strcpy(ptr, "_mouse_unset");
2666                 break;
2667         default:
2668                 ErrPrint("Invalid event type\n");
2669                 return LB_STATUS_ERROR_INVALID;
2670         }
2671
2672         return send_mouse_event(handler, cmd, x * w, y * h);
2673 }
2674
2675 EAPI int livebox_key_event(struct livebox *handler, enum content_event_type type, unsigned int keycode, ret_cb_t cb, void *data)
2676 {
2677         char cmd[32] = { '\0', };
2678         char *ptr = cmd;
2679         int ret;
2680
2681         if (!handler || handler->state != CREATE) {
2682                 ErrPrint("Handler is invalid\n");
2683                 return LB_STATUS_ERROR_INVALID;
2684         }
2685
2686         if (!handler->common || handler->common->state != CREATE) {
2687                 ErrPrint("Handler is invalid\n");
2688                 return LB_STATUS_ERROR_INVALID;
2689         }
2690
2691         if (!handler->common->id) {
2692                 ErrPrint("Handler is not valid\n");
2693                 return LB_STATUS_ERROR_INVALID;
2694         }
2695
2696         if (!(type & CONTENT_EVENT_KEY_MASK)) {
2697                 ErrPrint("Invalid key event is used\n");
2698                 return LB_STATUS_ERROR_INVALID;
2699         }
2700
2701         if (handler->common->request.key_event) {
2702                 ErrPrint("Previous key event is not completed yet\n");
2703                 return LB_STATUS_ERROR_BUSY;
2704         }
2705
2706         if (type & CONTENT_EVENT_PD_MASK) {
2707                 if (!handler->common->is_pd_created) {
2708                         ErrPrint("PD is not created\n");
2709                         return LB_STATUS_ERROR_INVALID;
2710                 }
2711
2712                 if (!handler->common->pd.fb) {
2713                         ErrPrint("Handler is not valid\n");
2714                         return LB_STATUS_ERROR_INVALID;
2715                 }
2716
2717                 if (type & CONTENT_EVENT_KEY_DOWN) {
2718                         /*!
2719                          * \TODO
2720                          * filtering the reproduced events if it is too fast
2721                          */
2722                 } else if (type & CONTENT_EVENT_KEY_SET) {
2723                         /*!
2724                          * \TODO
2725                          * What can I do for this case?
2726                          */
2727                 }
2728
2729                 *ptr++ = 'p';
2730                 *ptr++ = 'd';
2731         } else if (type & CONTENT_EVENT_LB_MASK) {
2732                 if (!handler->common->lb.mouse_event) {
2733                         return LB_STATUS_ERROR_INVALID;
2734                 }
2735
2736                 if (!handler->common->lb.fb) {
2737                         ErrPrint("Handler is not valid\n");
2738                         return LB_STATUS_ERROR_INVALID;
2739                 }
2740
2741                 if (type & CONTENT_EVENT_KEY_DOWN) {
2742                         /*!
2743                          * \TODO
2744                          * filtering the reproduced events if it is too fast
2745                          */
2746                 } else if (type & CONTENT_EVENT_KEY_SET) {
2747                         /*!
2748                          * What can I do for this case?
2749                          */
2750                 }
2751
2752                 *ptr++ = 'l';
2753                 *ptr++ = 'b';
2754         } else {
2755                 ErrPrint("Invalid event type\n");
2756                 return LB_STATUS_ERROR_INVALID;
2757         }
2758
2759         /*!
2760          * Must be short than 29 bytes.
2761          */
2762         switch ((type & ~(CONTENT_EVENT_PD_MASK | CONTENT_EVENT_LB_MASK))) {
2763         case CONTENT_EVENT_KEY_FOCUS_IN | CONTENT_EVENT_KEY_MASK:
2764                 strcpy(ptr, "_key_focus_in");
2765                 break;
2766         case CONTENT_EVENT_KEY_FOCUS_OUT | CONTENT_EVENT_KEY_MASK:
2767                 strcpy(ptr, "_key_focus_out");
2768                 break;
2769         case CONTENT_EVENT_KEY_UP | CONTENT_EVENT_KEY_MASK:
2770                 strcpy(ptr, "_key_up");
2771                 break;
2772         case CONTENT_EVENT_KEY_DOWN | CONTENT_EVENT_KEY_MASK:
2773                 strcpy(ptr, "_key_down");
2774                 break;
2775         case CONTENT_EVENT_KEY_SET | CONTENT_EVENT_KEY_MASK:
2776                 strcpy(ptr, "_key_set");
2777                 break;
2778         case CONTENT_EVENT_KEY_UNSET | CONTENT_EVENT_KEY_MASK:
2779                 strcpy(ptr, "_key_unset");
2780                 break;
2781         default:
2782                 ErrPrint("Invalid event type\n");
2783                 return LB_STATUS_ERROR_INVALID;
2784         }
2785
2786         if (!cb) {
2787                 cb = default_key_event_cb;
2788         }
2789
2790         ret = send_key_event(handler, cmd, keycode);
2791         if (ret == LB_STATUS_SUCCESS) {
2792                 handler->cbs.key_event.cb = cb;
2793                 handler->cbs.key_event.data = data;
2794                 handler->common->request.key_event = 1;
2795         }
2796
2797         return ret;
2798 }
2799
2800 EAPI const char *livebox_filename(struct livebox *handler)
2801 {
2802         if (!handler || handler->state != CREATE) {
2803                 ErrPrint("Handler is invalid\n");
2804                 return NULL;
2805         }
2806
2807         if (!handler->common || handler->common->state != CREATE) {
2808                 ErrPrint("Handler is invalid\n");
2809                 return NULL;
2810         }
2811
2812         if (!handler->common->id) {
2813                 ErrPrint("Handler is not valid\n");
2814                 return NULL;
2815         }
2816
2817         if (handler->common->filename) {
2818                 return handler->common->filename;
2819         }
2820
2821         /* Oooops */
2822         return util_uri_to_path(handler->common->id);
2823 }
2824
2825 EAPI int livebox_get_pdsize(struct livebox *handler, int *w, int *h)
2826 {
2827         int _w;
2828         int _h;
2829
2830         if (!handler || handler->state != CREATE) {
2831                 ErrPrint("Handler is invalid\n");
2832                 return LB_STATUS_ERROR_INVALID;
2833         }
2834
2835         if (!handler->common || handler->common->state != CREATE) {
2836                 ErrPrint("Handler is invalid\n");
2837                 return LB_STATUS_ERROR_INVALID;
2838         }
2839
2840         if (!handler->common->id) {
2841                 ErrPrint("Handler is not valid\n");
2842                 return LB_STATUS_ERROR_INVALID;
2843         }
2844
2845         if (!w) {
2846                 w = &_w;
2847         }
2848         if (!h) {
2849                 h = &_h;
2850         }
2851
2852         if (!handler->common->is_pd_created) {
2853                 *w = handler->common->pd.default_width;
2854                 *h = handler->common->pd.default_height;
2855         } else {
2856                 *w = handler->common->pd.width;
2857                 *h = handler->common->pd.height;
2858         }
2859
2860         return LB_STATUS_SUCCESS;
2861 }
2862
2863 EAPI int livebox_size(struct livebox *handler)
2864 {
2865         int w;
2866         int h;
2867
2868         if (!handler || handler->state != CREATE) {
2869                 ErrPrint("Handler is invalid\n");
2870                 return LB_STATUS_ERROR_INVALID;
2871         }
2872
2873         if (!handler->common || handler->common->state != CREATE) {
2874                 ErrPrint("Handler is invalid\n");
2875                 return LB_STATUS_ERROR_INVALID;
2876         }
2877
2878         if (!handler->common->id) {
2879                 ErrPrint("Handler is not valid\n");
2880                 return LB_STATUS_ERROR_INVALID;
2881         }
2882
2883         w = handler->common->lb.width;
2884         h = handler->common->lb.height;
2885
2886         switch (handler->common->lb.type) {
2887         case _LB_TYPE_BUFFER:
2888         case _LB_TYPE_SCRIPT:
2889                 if (!fb_is_created(handler->common->lb.fb)) {
2890                         w = 0;
2891                         h = 0;
2892                 }
2893                 break;
2894         default:
2895                 break;
2896         }
2897
2898         return livebox_service_size_type(w, h);
2899 }
2900
2901 EAPI int livebox_set_group(struct livebox *handler, const char *cluster, const char *category, ret_cb_t cb, void *data)
2902 {
2903         struct packet *packet;
2904         int ret;
2905
2906         if (!handler) {
2907                 ErrPrint("Handler is NIL\n");
2908                 return LB_STATUS_ERROR_INVALID;
2909         }
2910
2911         if (!cluster || !category || handler->state != CREATE) {
2912                 ErrPrint("Invalid argument\n");
2913                 return LB_STATUS_ERROR_INVALID;
2914         }
2915
2916         if (!handler->common || handler->common->state != CREATE) {
2917                 ErrPrint("Invalid argument\n");
2918                 return LB_STATUS_ERROR_INVALID;
2919         }
2920
2921         if (!handler->common->id) {
2922                 ErrPrint("Invalid argument\n");
2923                 return LB_STATUS_ERROR_INVALID;
2924         }
2925
2926         if (handler->common->request.group_changed) {
2927                 ErrPrint("Previous group changing request is not finished yet\n");
2928                 return LB_STATUS_ERROR_BUSY;
2929         }
2930
2931         if (!handler->common->is_user) {
2932                 ErrPrint("CA Livebox is not able to change the group\n");
2933                 return LB_STATUS_ERROR_PERMISSION;
2934         }
2935
2936         if (!strcmp(handler->common->cluster, cluster) && !strcmp(handler->common->category, category)) {
2937                 DbgPrint("No changes\n");
2938                 return LB_STATUS_ERROR_ALREADY;
2939         }
2940
2941         packet = packet_create("change_group", "ssss", handler->common->pkgname, handler->common->id, cluster, category);
2942         if (!packet) {
2943                 ErrPrint("Failed to build a param\n");
2944                 return LB_STATUS_ERROR_FAULT;
2945         }
2946
2947         if (!cb) {
2948                 cb = default_group_changed_cb;
2949         }
2950
2951         ret = master_rpc_async_request(handler, packet, 0, set_group_ret_cb, NULL);
2952         if (ret == LB_STATUS_SUCCESS) {
2953                 handler->cbs.group_changed.cb = cb;
2954                 handler->cbs.group_changed.data = data; 
2955                 handler->common->request.group_changed = 1;
2956         }
2957
2958         return ret;
2959 }
2960
2961 EAPI int livebox_get_group(struct livebox *handler, const char **cluster, const char **category)
2962 {
2963         if (!handler) {
2964                 ErrPrint("Handler is NIL\n");
2965                 return LB_STATUS_ERROR_INVALID;
2966         }
2967
2968         if (!cluster || !category || handler->state != CREATE) {
2969                 ErrPrint("Invalid argument\n");
2970                 return LB_STATUS_ERROR_INVALID;
2971         }
2972
2973         if (!handler->common || handler->common->state != CREATE) {
2974                 ErrPrint("Invalid argument\n");
2975                 return LB_STATUS_ERROR_INVALID;
2976         }
2977
2978         if (!handler->common->id) {
2979                 ErrPrint("Invalid argument\n");
2980                 return LB_STATUS_ERROR_INVALID;
2981         }
2982
2983         *cluster = handler->common->cluster;
2984         *category = handler->common->category;
2985         return LB_STATUS_SUCCESS;
2986 }
2987
2988 EAPI int livebox_get_supported_sizes(struct livebox *handler, int *cnt, int *size_list)
2989 {
2990         register int i;
2991         register int j;
2992
2993         if (!handler || !size_list) {
2994                 ErrPrint("Invalid argument, handler(%p), size_list(%p)\n", handler, size_list);
2995                 return LB_STATUS_ERROR_INVALID;
2996         }
2997
2998         if (!cnt || handler->state != CREATE) {
2999                 ErrPrint("Handler is not valid\n");
3000                 return LB_STATUS_ERROR_INVALID;
3001         }
3002
3003         if (!handler->common || handler->common->state != CREATE) {
3004                 ErrPrint("Handler is not valid\n");
3005                 return LB_STATUS_ERROR_INVALID;
3006         }
3007
3008         if (!handler->common->id) {
3009                 ErrPrint("Handler is not valid\n");
3010                 return LB_STATUS_ERROR_INVALID;
3011         }
3012
3013         for (j = i = 0; i < NR_OF_SIZE_LIST; i++) {
3014                 if (handler->common->lb.size_list & (0x01 << i)) {
3015                         if (j == *cnt) {
3016                                 break;
3017                         }
3018
3019                         size_list[j++] = (0x01 << i);
3020                 }
3021         }
3022
3023         *cnt = j;
3024         return LB_STATUS_SUCCESS;
3025 }
3026
3027 EAPI const char *livebox_pkgname(struct livebox *handler)
3028 {
3029         if (!handler) {
3030                 ErrPrint("Handler is NIL\n");
3031                 return NULL;
3032         }
3033
3034         if (handler->state != CREATE) {
3035                 ErrPrint("Handler is not valid\n");
3036                 return NULL;
3037         }
3038
3039         if (!handler->common || handler->common->state != CREATE) {
3040                 ErrPrint("Handler is not valid\n");
3041                 return NULL;
3042         }
3043
3044         return handler->common->pkgname;
3045 }
3046
3047 EAPI double livebox_priority(struct livebox *handler)
3048 {
3049         if (!handler || handler->state != CREATE) {
3050                 ErrPrint("Handler is invalid\n");
3051                 return -1.0f;
3052         }
3053
3054         if (!handler->common || handler->common->state != CREATE) {
3055                 ErrPrint("Handler is invalid\n");
3056                 return -1.0f;
3057         }
3058
3059         if (!handler->common->id) {
3060                 ErrPrint("Handler is not valid (%p)\n", handler);
3061                 return -1.0f;
3062         }
3063
3064         return handler->common->lb.priority;
3065 }
3066
3067 EAPI int livebox_delete_cluster(const char *cluster, ret_cb_t cb, void *data)
3068 {
3069         struct packet *packet;
3070         struct cb_info *cbinfo;
3071         int ret;
3072
3073         packet = packet_create("delete_cluster", "s", cluster);
3074         if (!packet) {
3075                 ErrPrint("Failed to build a param\n");
3076                 return LB_STATUS_ERROR_FAULT;
3077         }
3078
3079         cbinfo = create_cb_info(cb, data);
3080         if (!cbinfo) {
3081                 packet_destroy(packet);
3082                 return LB_STATUS_ERROR_FAULT;
3083         }
3084
3085         ret = master_rpc_async_request(NULL, packet, 0, delete_cluster_cb, cbinfo);
3086         if (ret < 0) {
3087                 destroy_cb_info(cbinfo);
3088         }
3089
3090         return ret;
3091 }
3092
3093 EAPI int livebox_delete_category(const char *cluster, const char *category, ret_cb_t cb, void *data)
3094 {
3095         struct packet *packet;
3096         struct cb_info *cbinfo;
3097         int ret;
3098
3099         packet = packet_create("delete_category", "ss", cluster, category);
3100         if (!packet) {
3101                 ErrPrint("Failed to build a param\n");
3102                 return LB_STATUS_ERROR_FAULT;
3103         }
3104
3105         cbinfo = create_cb_info(cb, data);
3106         if (!cbinfo) {
3107                 packet_destroy(packet);
3108                 return LB_STATUS_ERROR_FAULT;
3109         }
3110
3111         ret = master_rpc_async_request(NULL, packet, 0, delete_category_cb, cbinfo);
3112         if (ret < 0) {
3113                 destroy_cb_info(cbinfo);
3114         }
3115
3116         return ret;
3117 }
3118
3119 EAPI enum livebox_lb_type livebox_lb_type(struct livebox *handler)
3120 {
3121         if (!handler || handler->state != CREATE) {
3122                 ErrPrint("Handler is invalid\n");
3123                 return LB_TYPE_INVALID;
3124         }
3125
3126         if (!handler->common || handler->common->state != CREATE) {
3127                 ErrPrint("Handler is invalid\n");
3128                 return LB_TYPE_INVALID;
3129         }
3130
3131         if (!handler->common->id) {
3132                 ErrPrint("Handler is not valid\n");
3133                 return LB_TYPE_INVALID;
3134         }
3135
3136         switch (handler->common->lb.type) {
3137         case _LB_TYPE_FILE:
3138                 return LB_TYPE_IMAGE;
3139         case _LB_TYPE_BUFFER:
3140         case _LB_TYPE_SCRIPT:
3141                 {
3142                         const char *id;
3143                         id = fb_id(handler->common->lb.fb);
3144                         if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
3145                                 return LB_TYPE_PIXMAP;
3146                         }
3147                 }
3148                 return LB_TYPE_BUFFER;
3149         case _LB_TYPE_TEXT:
3150                 return LB_TYPE_TEXT;
3151         default:
3152                 break;
3153         }
3154
3155         return LB_TYPE_INVALID;
3156 }
3157
3158 EAPI enum livebox_pd_type livebox_pd_type(struct livebox *handler)
3159 {
3160         if (!handler || handler->state != CREATE) {
3161                 ErrPrint("Handler is invalid\n");
3162                 return PD_TYPE_INVALID;
3163         }
3164
3165         if (!handler->common || handler->common->state != CREATE) {
3166                 ErrPrint("Handler is invalid\n");
3167                 return PD_TYPE_INVALID;
3168         }
3169
3170         if (!handler->common->id) {
3171                 ErrPrint("Handler is not valid\n");
3172                 return PD_TYPE_INVALID;
3173         }
3174
3175         switch (handler->common->pd.type) {
3176         case _PD_TYPE_TEXT:
3177                 return PD_TYPE_TEXT;
3178         case _PD_TYPE_BUFFER:
3179         case _PD_TYPE_SCRIPT:
3180                 {
3181                         const char *id;
3182                         id = fb_id(handler->common->pd.fb);
3183                         if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
3184                                 return PD_TYPE_PIXMAP;
3185                         }
3186                 }
3187                 return PD_TYPE_BUFFER;
3188         default:
3189                 break;
3190         }
3191
3192         return PD_TYPE_INVALID;
3193 }
3194
3195 EAPI int livebox_set_pd_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
3196 {
3197         if (!handler) {
3198                 ErrPrint("Handler is NIL\n");
3199                 return LB_STATUS_ERROR_INVALID;
3200         }
3201
3202         if (handler->state != CREATE) {
3203                 ErrPrint("Handler is not valid\n");
3204                 return LB_STATUS_ERROR_INVALID;
3205         }
3206
3207         memcpy(&handler->cbs.pd_ops, ops, sizeof(*ops));
3208         return LB_STATUS_SUCCESS;
3209 }
3210
3211 EAPI int livebox_set_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
3212 {
3213         if (!handler) {
3214                 ErrPrint("Handler is NIL\n");
3215                 return LB_STATUS_ERROR_INVALID;
3216         }
3217
3218         if (handler->state != CREATE) {
3219                 ErrPrint("Handler is not valid\n");
3220                 return LB_STATUS_ERROR_INVALID;
3221         }
3222
3223         memcpy(&handler->cbs.lb_ops, ops, sizeof(*ops));
3224         return LB_STATUS_SUCCESS;
3225 }
3226
3227 EAPI int livebox_acquire_lb_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
3228 {
3229         if (!handler || handler->state != CREATE) {
3230                 ErrPrint("Handler is invalid\n");
3231                 return LB_STATUS_ERROR_INVALID;
3232         }
3233
3234         if (!handler->common || handler->common->state != CREATE) {
3235                 ErrPrint("Handler is invalid\n");
3236                 return LB_STATUS_ERROR_INVALID;
3237         }
3238
3239         if (!handler->common->id) {
3240                 ErrPrint("Invalid handle\n");
3241                 return LB_STATUS_ERROR_INVALID;
3242         }
3243
3244         if (handler->common->lb.type != _LB_TYPE_SCRIPT && handler->common->lb.type != _LB_TYPE_BUFFER) {
3245                 ErrPrint("Handler is not valid type\n");
3246                 return LB_STATUS_ERROR_INVALID;
3247         }
3248
3249         return lb_acquire_lb_pixmap(handler, cb, data);
3250 }
3251
3252 EAPI int livebox_release_lb_pixmap(struct livebox *handler, int pixmap)
3253 {
3254         struct packet *packet;
3255
3256         if (!handler || pixmap == 0 || handler->state != CREATE) {
3257                 ErrPrint("Handler is invalid [%d]\n", pixmap);
3258                 return LB_STATUS_ERROR_INVALID;
3259         }
3260
3261         if (!handler->common || handler->common->state != CREATE) {
3262                 ErrPrint("Handler is invalid\n");
3263                 return LB_STATUS_ERROR_INVALID;
3264         }
3265
3266         if (!handler->common->id) {
3267                 ErrPrint("Invalid handle\n");
3268                 return LB_STATUS_ERROR_INVALID;
3269         }
3270
3271         if (handler->common->lb.type != _LB_TYPE_SCRIPT && handler->common->lb.type != _LB_TYPE_BUFFER) {
3272                 ErrPrint("Handler is not valid type\n");
3273                 return LB_STATUS_ERROR_INVALID;
3274         }
3275
3276         packet = packet_create_noack("lb_release_pixmap", "ssi", handler->common->pkgname, handler->common->id, pixmap);
3277         if (!packet) {
3278                 ErrPrint("Failed to build a param\n");
3279                 return LB_STATUS_ERROR_INVALID;
3280         }
3281
3282         return master_rpc_request_only(handler, packet);
3283 }
3284
3285 EAPI int livebox_acquire_pd_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
3286 {
3287         if (!handler || handler->state != CREATE) {
3288                 ErrPrint("Handler is invalid\n");
3289                 return LB_STATUS_ERROR_INVALID;
3290         }
3291
3292         if (!handler->common || handler->common->state != CREATE) {
3293                 ErrPrint("Handler is invalid\n");
3294                 return LB_STATUS_ERROR_INVALID;
3295         }
3296
3297         if (!handler->common->id) {
3298                 ErrPrint("Invalid handle\n");
3299                 return LB_STATUS_ERROR_INVALID;
3300         }
3301
3302         if (handler->common->pd.type != _PD_TYPE_SCRIPT && handler->common->pd.type != _PD_TYPE_BUFFER) {
3303                 ErrPrint("Handler is not valid type\n");
3304                 return LB_STATUS_ERROR_INVALID;
3305         }
3306
3307         return lb_acquire_pd_pixmap(handler, cb, data);
3308 }
3309
3310 EAPI int livebox_pd_pixmap(const struct livebox *handler)
3311 {
3312         const char *id;
3313         int pixmap = 0;
3314
3315         if (!handler || handler->state != CREATE) {
3316                 ErrPrint("Handler is invalid\n");
3317                 return 0;
3318         }
3319
3320         if (!handler->common || handler->common->state != CREATE) {
3321                 ErrPrint("Handler is invalid\n");
3322                 return 0;
3323         }
3324
3325         if (!handler->common->id) {
3326                 ErrPrint("Invalid handler\n");
3327                 return 0;
3328         }
3329
3330         if (handler->common->pd.type != _PD_TYPE_SCRIPT && handler->common->pd.type != _PD_TYPE_BUFFER) {
3331                 ErrPrint("Invalid handler\n");
3332                 return 0;
3333         }
3334
3335         id = fb_id(handler->common->pd.fb);
3336         if (id && sscanf(id, SCHEMA_PIXMAP "%u", (unsigned int *)&pixmap) != 1) {
3337                 ErrPrint("PIXMAP Id is not valid\n");
3338                 return 0;
3339         }
3340
3341         return pixmap;
3342 }
3343
3344 EAPI int livebox_lb_pixmap(const struct livebox *handler)
3345 {
3346         const char *id;
3347         int pixmap = 0;
3348
3349         if (!handler || handler->state != CREATE) {
3350                 ErrPrint("Handler is invalid\n");
3351                 return 0;
3352         }
3353
3354         if (!handler->common || handler->common->state != CREATE) {
3355                 ErrPrint("Handler is invalid\n");
3356                 return 0;
3357         }
3358
3359         if (!handler->common->id) {
3360                 ErrPrint("Invalid handler\n");
3361                 return 0;
3362         }
3363
3364         if (handler->common->lb.type != _LB_TYPE_SCRIPT && handler->common->lb.type != _LB_TYPE_BUFFER) {
3365                 ErrPrint("Invalid handler\n");
3366                 return 0;
3367         }
3368
3369         id = fb_id(handler->common->lb.fb);
3370         if (id && sscanf(id, SCHEMA_PIXMAP "%u", (unsigned int *)&pixmap) != 1) {
3371                 ErrPrint("PIXMAP Id is not valid\n");
3372                 return 0;
3373         }
3374
3375         return pixmap;
3376 }
3377
3378 EAPI int livebox_release_pd_pixmap(struct livebox *handler, int pixmap)
3379 {
3380         struct packet *packet;
3381
3382         if (!handler || pixmap == 0 || handler->state != CREATE) {
3383                 ErrPrint("Handler is invalid [%d]\n", pixmap);
3384                 return LB_STATUS_ERROR_INVALID;
3385         }
3386
3387         if (!handler->common || handler->common->state != CREATE) {
3388                 ErrPrint("Handler is invalid\n");
3389                 return LB_STATUS_ERROR_INVALID;
3390         }
3391
3392         if (!handler->common->id) {
3393                 ErrPrint("Invalid handle\n");
3394                 return LB_STATUS_ERROR_INVALID;
3395         }
3396
3397         if (handler->common->pd.type != _PD_TYPE_SCRIPT && handler->common->pd.type != _PD_TYPE_BUFFER) {
3398                 ErrPrint("Handler is not valid type\n");
3399                 return LB_STATUS_ERROR_INVALID;
3400         }
3401
3402         packet = packet_create_noack("pd_release_pixmap", "ssi", handler->common->pkgname, handler->common->id, pixmap);
3403         if (!packet) {
3404                 ErrPrint("Failed to build a param\n");
3405                 return LB_STATUS_ERROR_FAULT;
3406         }
3407
3408         return master_rpc_request_only(handler, packet);
3409 }
3410
3411 EAPI void *livebox_acquire_fb(struct livebox *handler)
3412 {
3413         if (!handler || handler->state != CREATE) {
3414                 ErrPrint("Handler is invalid\n");
3415                 return NULL;
3416         }
3417
3418         if (!handler->common || handler->common->state != CREATE) {
3419                 ErrPrint("Handler is invalid\n");
3420                 return NULL;
3421         }
3422
3423         if (!handler->common->id) {
3424                 ErrPrint("Invalid handle\n");
3425                 return NULL;
3426         }
3427
3428         if (handler->common->lb.type != _LB_TYPE_SCRIPT && handler->common->lb.type != _LB_TYPE_BUFFER) {
3429                 ErrPrint("Handler is not valid type\n");
3430                 return NULL;
3431         }
3432
3433         return fb_acquire_buffer(handler->common->lb.fb);
3434 }
3435
3436 EAPI int livebox_release_fb(void *buffer)
3437 {
3438         return fb_release_buffer(buffer);
3439 }
3440
3441 EAPI int livebox_fb_refcnt(void *buffer)
3442 {
3443         return fb_refcnt(buffer);
3444 }
3445
3446 EAPI void *livebox_acquire_pdfb(struct livebox *handler)
3447 {
3448         if (!handler || handler->state != CREATE) {
3449                 ErrPrint("Handler is invalid\n");
3450                 return NULL;
3451         }
3452
3453         if (!handler->common || handler->common->state != CREATE) {
3454                 ErrPrint("Handler is invalid\n");
3455                 return NULL;
3456         }
3457
3458         if (!handler->common->id) {
3459                 ErrPrint("Invalid handler\n");
3460                 return NULL;
3461         }
3462
3463         if (handler->common->pd.type != _PD_TYPE_SCRIPT && handler->common->pd.type != _PD_TYPE_BUFFER) {
3464                 ErrPrint("Handler is not valid type\n");
3465                 return NULL;
3466         }
3467
3468         return fb_acquire_buffer(handler->common->pd.fb);
3469 }
3470
3471 EAPI int livebox_release_pdfb(void *buffer)
3472 {
3473         return fb_release_buffer(buffer);
3474 }
3475
3476 EAPI int livebox_pdfb_refcnt(void *buffer)
3477 {
3478         return fb_refcnt(buffer);
3479 }
3480
3481 EAPI int livebox_pdfb_bufsz(struct livebox *handler)
3482 {
3483         if (!handler || handler->state != CREATE) {
3484                 ErrPrint("Handler is invalid\n");
3485                 return LB_STATUS_ERROR_INVALID;
3486         }
3487
3488         if (!handler->common || handler->common->state != CREATE) {
3489                 ErrPrint("Handler is invalid\n");
3490                 return LB_STATUS_ERROR_INVALID;
3491         }
3492
3493         if (!handler->common->id) {
3494                 ErrPrint("Invalid handler\n");
3495                 return LB_STATUS_ERROR_INVALID;
3496         }
3497
3498         return fb_size(handler->common->pd.fb);
3499 }
3500
3501 EAPI int livebox_lbfb_bufsz(struct livebox *handler)
3502 {
3503         if (!handler || handler->state != CREATE) {
3504                 ErrPrint("Handler is invalid\n");
3505                 return LB_STATUS_ERROR_INVALID;
3506         }
3507
3508         if (!handler->common || handler->common->state != CREATE) {
3509                 ErrPrint("Handler is invalid\n");
3510                 return LB_STATUS_ERROR_INVALID;
3511         }
3512
3513         if (!handler->common->id) {
3514                 ErrPrint("Invalid handler\n");
3515                 return LB_STATUS_ERROR_INVALID;
3516         }
3517
3518         return fb_size(handler->common->lb.fb);
3519 }
3520
3521 EAPI int livebox_is_user(struct livebox *handler)
3522 {
3523         if (!handler || handler->state != CREATE) {
3524                 ErrPrint("Handler is invalid\n");
3525                 return LB_STATUS_ERROR_INVALID;
3526         }
3527
3528         if (!handler->common || handler->common->state != CREATE) {
3529                 ErrPrint("Handler is invalid\n");
3530                 return LB_STATUS_ERROR_INVALID;
3531         }
3532
3533         if (!handler->common->id) {
3534                 ErrPrint("Invalid handler\n");
3535                 return LB_STATUS_ERROR_INVALID;
3536         }
3537
3538         return handler->common->is_user;
3539 }
3540
3541 EAPI int livebox_set_pinup(struct livebox *handler, int flag, ret_cb_t cb, void *data)
3542 {
3543         struct packet *packet;
3544         int ret;
3545
3546         if (!handler || handler->state != CREATE) {
3547                 ErrPrint("Handler is invalid\n");
3548                 return LB_STATUS_ERROR_INVALID;
3549         }
3550
3551         if (!handler->common || handler->common->state != CREATE) {
3552                 ErrPrint("Handler is invalid\n");
3553                 return LB_STATUS_ERROR_INVALID;
3554         }
3555
3556         if (!handler->common->id) {
3557                 ErrPrint("Invalid handler\n");
3558                 return LB_STATUS_ERROR_INVALID;
3559         }
3560
3561         if (handler->common->request.pinup) {
3562                 ErrPrint("Previous pinup request is not finished\n");
3563                 return LB_STATUS_ERROR_BUSY;
3564         }
3565
3566         if (handler->common->is_pinned_up == flag) {
3567                 DbgPrint("No changes\n");
3568                 return LB_STATUS_ERROR_ALREADY;
3569         }
3570
3571         packet = packet_create("pinup_changed", "ssi", handler->common->pkgname, handler->common->id, flag);
3572         if (!packet) {
3573                 ErrPrint("Failed to build a param\n");
3574                 return LB_STATUS_ERROR_FAULT;
3575         }
3576
3577         if (!cb) {
3578                 cb = default_pinup_cb;
3579         }
3580
3581         ret = master_rpc_async_request(handler, packet, 0, pinup_done_cb, NULL);
3582         if (ret == LB_STATUS_SUCCESS) {
3583                 handler->cbs.pinup.cb = cb;
3584                 handler->cbs.pinup.data = data;
3585                 handler->common->request.pinup = 1;
3586         }
3587
3588         return ret;
3589 }
3590
3591 EAPI int livebox_is_pinned_up(struct livebox *handler)
3592 {
3593         if (!handler || handler->state != CREATE) {
3594                 ErrPrint("Handler is invalid\n");
3595                 return LB_STATUS_ERROR_INVALID;
3596         }
3597
3598         if (!handler->common || handler->common->state != CREATE) {
3599                 ErrPrint("Handler is invalid\n");
3600                 return LB_STATUS_ERROR_INVALID;
3601         }
3602
3603         if (!handler->common->id) {
3604                 ErrPrint("Invalid handler\n");
3605                 return LB_STATUS_ERROR_INVALID;
3606         }
3607
3608         return handler->common->is_pinned_up;
3609 }
3610
3611 EAPI int livebox_has_pinup(struct livebox *handler)
3612 {
3613         if (!handler || handler->state != CREATE) {
3614                 ErrPrint("Handler is invalid\n");
3615                 return LB_STATUS_ERROR_INVALID;
3616         }
3617
3618         if (!handler->common || handler->common->state != CREATE) {
3619                 ErrPrint("Handler is invalid\n");
3620                 return LB_STATUS_ERROR_INVALID;
3621         }
3622
3623         if (!handler->common->id) {
3624                 ErrPrint("Invalid handler\n");
3625                 return LB_STATUS_ERROR_INVALID;
3626         }
3627
3628         return handler->common->lb.pinup_supported;
3629 }
3630
3631 EAPI int livebox_set_data(struct livebox *handler, void *data)
3632 {
3633         if (!handler) {
3634                 ErrPrint("Handler is NIL\n");
3635                 return LB_STATUS_ERROR_INVALID;
3636         }
3637
3638         if (handler->state != CREATE) {
3639                 ErrPrint("Handler is invalid\n");
3640                 return LB_STATUS_ERROR_INVALID;
3641         }
3642
3643         handler->data = data;
3644         return LB_STATUS_SUCCESS;
3645 }
3646
3647 EAPI void *livebox_get_data(struct livebox *handler)
3648 {
3649         if (!handler) {
3650                 ErrPrint("Handler is NIL\n");
3651                 return NULL;
3652         }
3653
3654         if (handler->state != CREATE) {
3655                 ErrPrint("Handler is invalid\n");
3656                 return NULL;
3657         }
3658
3659         return handler->data;
3660 }
3661
3662 EAPI int livebox_is_exists(const char *pkgname)
3663 {
3664         char *lb;
3665
3666         lb = lb_pkgname(pkgname);
3667         if (lb) {
3668                 free(lb);
3669                 return 1;
3670         }
3671
3672         return 0;
3673 }
3674
3675 EAPI const char *livebox_content(struct livebox *handler)
3676 {
3677         if (!handler || handler->state != CREATE) {
3678                 ErrPrint("Handler is invalid\n");
3679                 return NULL;
3680         }
3681
3682         if (!handler->common || handler->common->state != CREATE) {
3683                 ErrPrint("Invalid handle\n");
3684                 return NULL;
3685         }
3686
3687         return handler->common->content;
3688 }
3689
3690 EAPI const char *livebox_category_title(struct livebox *handler)
3691 {
3692         if (!handler || handler->state != CREATE) {
3693                 ErrPrint("Handler is invalid\n");
3694                 return NULL;
3695         }
3696
3697         if (!handler->common || handler->common->state != CREATE) {
3698                 ErrPrint("Invalid handle\n");
3699                 return NULL;
3700         }
3701
3702         return handler->common->title;
3703 }
3704
3705 EAPI int livebox_emit_text_signal(struct livebox *handler, const char *emission, const char *source, double sx, double sy, double ex, double ey, ret_cb_t cb, void *data)
3706 {
3707         struct packet *packet;
3708         struct cb_info *cbinfo;
3709         int ret;
3710
3711         if (!handler || handler->state != CREATE) {
3712                 ErrPrint("Handler is invalid\n");
3713                 return LB_STATUS_ERROR_INVALID;
3714         }
3715
3716         if (!handler->common || handler->common->state != CREATE) {
3717                 ErrPrint("Handler is invalid\n");
3718                 return LB_STATUS_ERROR_INVALID;
3719         }
3720
3721         if ((handler->common->lb.type != _LB_TYPE_TEXT && handler->common->pd.type != _PD_TYPE_TEXT) || !handler->common->id) {
3722                 ErrPrint("Handler is not valid\n");
3723                 return LB_STATUS_ERROR_INVALID;
3724         }
3725
3726         if (!emission) {
3727                 emission = "";
3728         }
3729
3730         if (!source) {
3731                 source = "";
3732         }
3733
3734         packet = packet_create("text_signal", "ssssdddd",
3735                                 handler->common->pkgname, handler->common->id, emission, source, sx, sy, ex, ey);
3736         if (!packet) {
3737                 ErrPrint("Failed to build a param\n");
3738                 return LB_STATUS_ERROR_FAULT;
3739         }
3740
3741         cbinfo = create_cb_info(cb, data);
3742         if (!cbinfo) {
3743                 packet_destroy(packet);
3744                 return LB_STATUS_ERROR_FAULT;
3745         }
3746
3747         ret = master_rpc_async_request(handler, packet, 0, text_signal_cb, cbinfo);
3748         if (ret < 0) {
3749                 destroy_cb_info(cbinfo);
3750         }
3751
3752         return ret;
3753 }
3754
3755 EAPI int livebox_subscribe_group(const char *cluster, const char *category)
3756 {
3757         struct packet *packet;
3758
3759         /*!
3760          * \todo
3761          * Validate the group info using DB
3762          * If the group info is not valid, do not send this request
3763          */
3764
3765         packet = packet_create_noack("subscribe", "ss", cluster ? cluster : "", category ? category : "");
3766         if (!packet) {
3767                 ErrPrint("Failed to create a packet\n");
3768                 return LB_STATUS_ERROR_FAULT;
3769         }
3770
3771         return master_rpc_request_only(NULL, packet);
3772 }
3773
3774 EAPI int livebox_unsubscribe_group(const char *cluster, const char *category)
3775 {
3776         struct packet *packet;
3777
3778         /*!
3779          * \todo
3780          * Validate the group info using DB
3781          * If the group info is not valid, do not send this request
3782          * AND Check the subscribed or not too
3783          */
3784
3785         packet = packet_create_noack("unsubscribe", "ss", cluster ? cluster : "", category ? category : "");
3786         if (!packet) {
3787                 ErrPrint("Failed to create a packet\n");
3788                 return LB_STATUS_ERROR_FAULT;
3789         }
3790
3791         return master_rpc_request_only(NULL, packet);
3792 }
3793
3794 EAPI int livebox_refresh(struct livebox *handler, int force)
3795 {
3796         struct packet *packet;
3797
3798         if (!handler || handler->state != CREATE) {
3799                 ErrPrint("Handler is invalid\n");
3800                 return LB_STATUS_ERROR_INVALID;
3801         }
3802
3803         if (!handler->common || handler->common->state != CREATE) {
3804                 ErrPrint("Handler is not valid\n");
3805                 return LB_STATUS_ERROR_INVALID;
3806         }
3807
3808         if (!handler->common->id) {
3809                 ErrPrint("Handler is not valid\n");
3810                 return LB_STATUS_ERROR_INVALID;
3811         }
3812
3813         packet = packet_create_noack("update", "ssi", handler->common->pkgname, handler->common->id, force);
3814         if (!packet) {
3815                 ErrPrint("Failed to create a packet\n");
3816                 return LB_STATUS_ERROR_FAULT;
3817         }
3818
3819         return master_rpc_request_only(handler, packet);
3820 }
3821
3822 EAPI int livebox_refresh_group(const char *cluster, const char *category, int force)
3823 {
3824         struct packet *packet;
3825
3826         if (!cluster || !category) {
3827                 ErrPrint("Invalid argument\n");
3828                 return LB_STATUS_ERROR_INVALID;
3829         }
3830
3831         packet = packet_create_noack("refresh_group", "ssi", cluster, category, force);
3832         if (!packet) {
3833                 ErrPrint("Failed to create a packet\n");
3834                 return LB_STATUS_ERROR_FAULT;
3835         }
3836
3837         return master_rpc_request_only(NULL, packet);
3838 }
3839
3840 EAPI int livebox_set_visibility(struct livebox *handler, enum livebox_visible_state state)
3841 {
3842         int old_state;
3843         int ret;
3844
3845         if (!handler || handler->state != CREATE) {
3846                 ErrPrint("Handler is invalid\n");
3847                 return LB_STATUS_ERROR_INVALID;
3848         }
3849
3850         if (!handler->common || handler->common->state != CREATE) {
3851                 ErrPrint("Handler is not valid\n");
3852                 return LB_STATUS_ERROR_INVALID;
3853         }
3854
3855         if (!handler->common->id) {
3856                 ErrPrint("Handler is not valid\n");
3857                 return LB_STATUS_ERROR_INVALID;
3858         }
3859
3860         if (!handler->common->is_user) {
3861                 /* System cluster livebox cannot be changed its visible states */
3862                 if (state == LB_HIDE_WITH_PAUSE) {
3863                         ErrPrint("CA Livebox is not able to change the visibility\n");
3864                         return LB_STATUS_ERROR_PERMISSION;
3865                 }
3866         }
3867
3868         DbgPrint("[%s] Change visiblity to 0x%x\n", handler->common->pkgname, state);
3869
3870         if (handler->visible == state) {
3871                 DbgPrint("%s has no changes\n", handler->common->pkgname);
3872                 return LB_STATUS_ERROR_ALREADY;
3873         }
3874
3875         old_state = handler->visible;
3876         handler->visible = state;
3877
3878         ret = lb_set_visibility(handler, state);
3879         if (ret < 0) {
3880                 handler->visible = old_state;
3881         }
3882
3883         return ret;
3884 }
3885
3886 EAPI enum livebox_visible_state livebox_visibility(struct livebox *handler)
3887 {
3888         if (!handler || handler->state != CREATE) {
3889                 ErrPrint("Handler is invalid\n");
3890                 return LB_VISIBLE_ERROR;
3891         }
3892
3893         if (!handler->common || handler->common->state != CREATE) {
3894                 ErrPrint("Handler is not valid\n");
3895                 return LB_VISIBLE_ERROR;
3896         }
3897
3898         if (!handler->common->id) {
3899                 ErrPrint("Handler is not valid\n");
3900                 return LB_VISIBLE_ERROR;
3901         }
3902
3903         return handler->visible;
3904 }
3905
3906 int lb_set_group(struct livebox_common *common, const char *cluster, const char *category)
3907 {
3908         void *pc = NULL;
3909         void *ps = NULL;
3910
3911         if (cluster) {
3912                 pc = strdup(cluster);
3913                 if (!pc) {
3914                         ErrPrint("Heap: %s (cluster: %s)\n", strerror(errno), cluster);
3915                         return LB_STATUS_ERROR_MEMORY;
3916                 }
3917         }
3918
3919         if (category) {
3920                 ps = strdup(category);
3921                 if (!ps) {
3922                         ErrPrint("Heap: %s (category: %s)\n", strerror(errno), category);
3923                         free(pc);
3924                         return LB_STATUS_ERROR_MEMORY;
3925                 }
3926         }
3927
3928         if (common->cluster) {
3929                 free(common->cluster);
3930         }
3931
3932         if (common->category) {
3933                 free(common->category);
3934         }
3935
3936         common->cluster = pc;
3937         common->category = ps;
3938
3939         return LB_STATUS_SUCCESS;
3940 }
3941
3942 void lb_set_size(struct livebox_common *common, int w, int h)
3943 {
3944         common->lb.width = w;
3945         common->lb.height = h;
3946 }
3947
3948 void lb_set_update_mode(struct livebox_common *common, int active_mode)
3949 {
3950         common->is_active_update = active_mode;
3951 }
3952
3953 void lb_set_pdsize(struct livebox_common *common, int w, int h)
3954 {
3955         common->pd.width = w;
3956         common->pd.height = h;
3957 }
3958
3959 void lb_set_default_pdsize(struct livebox_common *common, int w, int h)
3960 {
3961         common->pd.default_width = w;
3962         common->pd.default_height = h;
3963 }
3964
3965 void lb_invoke_fault_handler(enum livebox_fault_type event, const char *pkgname, const char *file, const char *func)
3966 {
3967         struct dlist *l;
3968         struct dlist *n;
3969         struct fault_info *info;
3970
3971         s_info.fault_state = INFO_STATE_CALLBACK_IN_PROCESSING;
3972
3973         dlist_foreach_safe(s_info.fault_list, l, n, info) {
3974                 if (!info->is_deleted && info->handler(event, pkgname, file, func, info->user_data) == EXIT_FAILURE) {
3975                         info->is_deleted = 1;
3976                 }
3977
3978                 if (info->is_deleted) {
3979                         s_info.fault_list = dlist_remove(s_info.fault_list, l);
3980                         free(info);
3981                 }
3982         }
3983
3984         s_info.fault_state &= ~INFO_STATE_CALLBACK_IN_PROCESSING;
3985 }
3986
3987 void lb_invoke_event_handler(struct livebox *handler, enum livebox_event_type event)
3988 {
3989         struct dlist *l;
3990         struct dlist *n;
3991         struct event_info *info;
3992
3993         if (event == LB_EVENT_LB_UPDATED && handler->common->refcnt > 1) {
3994                 if (handler->visible != LB_SHOW) {
3995                         DbgPrint("Update requested(pending) - %s\n", handler->common->pkgname);
3996                         handler->paused_updating++;
3997                         return;
3998                 } else {
3999                         handler->paused_updating = 0;
4000                 }
4001         }
4002
4003         s_info.event_state = INFO_STATE_CALLBACK_IN_PROCESSING;
4004
4005         dlist_foreach_safe(s_info.event_list, l, n, info) {
4006                 if (!info->is_deleted && info->handler(handler, event, info->user_data) == EXIT_FAILURE) {
4007                         DbgPrint("Event handler returns EXIT_FAILURE\n");
4008                         info->is_deleted = 1;
4009                 }
4010
4011                 if (info->is_deleted) {
4012                         s_info.event_list = dlist_remove(s_info.event_list, l);
4013                         free(info);
4014                 }
4015         }
4016
4017         s_info.event_state &= ~INFO_STATE_CALLBACK_IN_PROCESSING;
4018 }
4019
4020 struct livebox_common *lb_find_common_handle(const char *pkgname, const char *id)
4021 {
4022         struct dlist *l;
4023         struct livebox_common *common;
4024
4025         dlist_foreach(s_info.livebox_common_list, l, common) {
4026                 if (!common->id) {
4027                         continue;
4028                 }
4029
4030                 if (!strcmp(common->pkgname, pkgname) && !strcmp(common->id, id)) {
4031                         return common;
4032                 }
4033         }
4034
4035         return NULL;
4036 }
4037
4038 struct livebox_common *lb_find_common_handle_by_timestamp(double timestamp)
4039 {
4040         struct dlist *l;
4041         struct livebox_common *common;
4042
4043         dlist_foreach(s_info.livebox_common_list, l, common) {
4044                 if (common->timestamp == timestamp) {
4045                         return common;
4046                 }
4047         }
4048
4049         return NULL;
4050 }
4051
4052 struct livebox *lb_new_livebox(const char *pkgname, const char *id, double timestamp, const char *cluster, const char *category)
4053 {
4054         struct livebox *handler;
4055
4056         handler = calloc(1, sizeof(*handler));
4057         if (!handler) {
4058                 ErrPrint("Failed to create a new livebox\n");
4059                 return NULL;
4060         }
4061
4062         handler->common = lb_create_common_handle(handler, pkgname, cluster, category);
4063         if (!handler->common) {
4064                 ErrPrint("Heap: %s\n", strerror(errno));
4065                 free(handler);
4066                 return NULL;
4067         }
4068
4069         lb_common_ref(handler->common, handler);
4070         lb_set_id(handler->common, id);
4071         handler->common->timestamp = timestamp;
4072         handler->common->state = CREATE;
4073         handler->visible = LB_SHOW;
4074         s_info.livebox_list = dlist_append(s_info.livebox_list, handler);
4075
4076         return lb_ref(handler);
4077 }
4078
4079 int lb_delete_all(void)
4080 {
4081         struct dlist *l;
4082         struct dlist *n;
4083         struct livebox *handler;
4084
4085         dlist_foreach_safe(s_info.livebox_list, l, n, handler) {
4086                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
4087                 lb_unref(handler, 1);
4088         }
4089
4090         return LB_STATUS_SUCCESS;
4091 }
4092
4093 int lb_set_content(struct livebox_common *common, const char *content)
4094 {
4095         char *pc = NULL;
4096
4097         if (content) {
4098                 pc = strdup(content);
4099                 if (!pc) {
4100                         ErrPrint("heap: %s [%s]\n", strerror(errno), content);
4101                         return LB_STATUS_ERROR_MEMORY;
4102                 }
4103         }
4104
4105         free(common->content);
4106         common->content = pc;
4107         return LB_STATUS_SUCCESS;
4108 }
4109
4110 int lb_set_title(struct livebox_common *common, const char *title)
4111 {
4112         char *pt = NULL;
4113
4114         if (title) {
4115                 pt = strdup(title);
4116                 if (!pt) {
4117                         ErrPrint("heap: %s [%s]\n", strerror(errno), title);
4118                         return LB_STATUS_ERROR_MEMORY;
4119                 }
4120         }
4121
4122         free(common->title);
4123         common->title = pt;
4124         return LB_STATUS_SUCCESS;
4125 }
4126
4127 void lb_set_size_list(struct livebox_common *common, int size_list)
4128 {
4129         common->lb.size_list = size_list;
4130 }
4131
4132 void lb_set_auto_launch(struct livebox_common *common, const char *auto_launch)
4133 {
4134         char *pa = NULL;
4135
4136         if (!auto_launch || !strlen(auto_launch)) {
4137                 return;
4138         }
4139
4140         pa = strdup(auto_launch);
4141         if (!pa) {
4142                 ErrPrint("heap: %s, [%s]\n", strerror(errno), auto_launch);
4143                 return;
4144         }
4145
4146         free(common->lb.auto_launch);
4147         common->lb.auto_launch = pa;
4148 }
4149
4150 void lb_set_priority(struct livebox_common *common, double priority)
4151 {
4152         common->lb.priority = priority;
4153 }
4154
4155 void lb_set_id(struct livebox_common *common, const char *id)
4156 {
4157         char *pi = NULL;
4158
4159         if (id) {
4160                 pi = strdup(id);
4161                 if (!pi) {
4162                         ErrPrint("heap: %s [%s]\n", strerror(errno), pi);
4163                         return;
4164                 }
4165         }
4166
4167         free(common->id);
4168         common->id = pi;
4169 }
4170
4171 void lb_set_filename(struct livebox_common *common, const char *filename)
4172 {
4173         if (common->filename) {
4174                 if (common->lb.type == _LB_TYPE_FILE || common->lb.type == _LB_TYPE_TEXT) {
4175                         if (common->filename[0] && unlink(common->filename) < 0) {
4176                                 ErrPrint("unlink: %s (%s)\n", strerror(errno), common->filename);
4177                         }
4178                 }
4179
4180                 free(common->filename);
4181         }
4182
4183         common->filename = strdup(filename);
4184         if (!common->filename) {
4185                 ErrPrint("Heap: %s\n", strerror(errno));
4186         }
4187 }
4188
4189 void lb_set_alt_info(struct livebox_common *common, const char *icon, const char *name)
4190 {
4191         char *_icon = NULL;
4192         char *_name = NULL;
4193
4194         if (icon && strlen(icon)) {
4195                 _icon = strdup(icon);
4196                 if (!_icon) {
4197                         ErrPrint("Heap: %s\n", strerror(errno));
4198                 }
4199         }
4200
4201         if (name && strlen(name)) {
4202                 _name = strdup(name);
4203                 if (!_name) {
4204                         ErrPrint("Heap: %s\n", strerror(errno));
4205                 }
4206         }
4207
4208         free(common->alt.icon);
4209         common->alt.icon = _icon;
4210
4211         free(common->alt.name);
4212         common->alt.name = _name;
4213 }
4214
4215 int lb_set_lb_fb(struct livebox_common *common, const char *filename)
4216 {
4217         struct fb_info *fb;
4218
4219         if (!common) {
4220                 return LB_STATUS_ERROR_INVALID;
4221         }
4222
4223         fb = common->lb.fb;
4224         if (fb && !strcmp(fb_id(fb), filename)) { /*!< BUFFER is not changed, */
4225                 return LB_STATUS_SUCCESS;
4226         }
4227
4228         common->lb.fb = NULL;
4229
4230         if (!filename || filename[0] == '\0') {
4231                 if (fb) {
4232                         fb_destroy(fb);
4233                 }
4234                 return LB_STATUS_SUCCESS;
4235         }
4236
4237         common->lb.fb = fb_create(filename, common->lb.width, common->lb.height);
4238         if (!common->lb.fb) {
4239                 ErrPrint("Faield to create a FB\n");
4240                 if (fb) {
4241                         fb_destroy(fb);
4242                 }
4243                 return LB_STATUS_ERROR_FAULT;
4244         }
4245
4246         if (fb) {
4247                 fb_destroy(fb);
4248         }
4249
4250         return LB_STATUS_SUCCESS;
4251 }
4252
4253 int lb_set_pd_fb(struct livebox_common *common, const char *filename)
4254 {
4255         struct fb_info *fb;
4256
4257         if (!common || common->state != CREATE) {
4258                 return LB_STATUS_ERROR_INVALID;
4259         }
4260
4261         fb = common->pd.fb;
4262         if (fb && !strcmp(fb_id(fb), filename)) {
4263                 /* BUFFER is not changed, just update the content */
4264                 return LB_STATUS_ERROR_EXIST;
4265         }
4266         common->pd.fb = NULL;
4267
4268         if (!filename || filename[0] == '\0') {
4269                 if (fb) {
4270                         fb_destroy(fb);
4271                 }
4272                 return LB_STATUS_SUCCESS;
4273         }
4274
4275         common->pd.fb = fb_create(filename, common->pd.width, common->pd.height);
4276         if (!common->pd.fb) {
4277                 ErrPrint("Failed to create a FB\n");
4278                 if (fb) {
4279                         fb_destroy(fb);
4280                 }
4281                 return LB_STATUS_ERROR_FAULT;
4282         }
4283
4284         if (fb) {
4285                 fb_destroy(fb);
4286         }
4287         return LB_STATUS_SUCCESS;
4288 }
4289
4290 struct fb_info *lb_get_lb_fb(struct livebox_common *common)
4291 {
4292         return common->lb.fb;
4293 }
4294
4295 struct fb_info *lb_get_pd_fb(struct livebox_common *common)
4296 {
4297         return common->pd.fb;
4298 }
4299
4300 void lb_set_user(struct livebox_common *common, int user)
4301 {
4302         common->is_user = user;
4303 }
4304
4305 void lb_set_pinup(struct livebox_common *common, int pinup_supported)
4306 {
4307         common->lb.pinup_supported = pinup_supported;
4308 }
4309
4310 void lb_set_text_lb(struct livebox_common *common)
4311 {
4312         common->lb.type = _LB_TYPE_TEXT;
4313 }
4314
4315 void lb_set_text_pd(struct livebox_common *common)
4316 {
4317         common->pd.type = _PD_TYPE_TEXT;
4318 }
4319
4320 int lb_text_lb(struct livebox_common *common)
4321 {
4322         return common->lb.type == _LB_TYPE_TEXT;
4323 }
4324
4325 int lb_text_pd(struct livebox_common *common)
4326 {
4327         return common->pd.type == _PD_TYPE_TEXT;
4328 }
4329
4330 void lb_set_period(struct livebox_common *common, double period)
4331 {
4332         common->lb.period = period;
4333 }
4334
4335 struct livebox *lb_ref(struct livebox *handler)
4336 {
4337         if (!handler) {
4338                 return NULL;
4339         }
4340
4341         handler->refcnt++;
4342         return handler;
4343 }
4344
4345 struct livebox *lb_unref(struct livebox *handler, int destroy_common)
4346 {
4347         if (!handler) {
4348                 return NULL;
4349         }
4350
4351         handler->refcnt--;
4352         if (handler->refcnt > 0) {
4353                 return handler;
4354         }
4355
4356         if (handler->cbs.created.cb) {
4357                 handler->cbs.created.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.created.data);
4358                 handler->cbs.created.cb = NULL;
4359                 handler->cbs.created.data = NULL;
4360         }
4361
4362         if (handler->cbs.deleted.cb) {
4363                 handler->cbs.deleted.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.deleted.data);
4364                 handler->cbs.deleted.cb = NULL;
4365                 handler->cbs.deleted.data = NULL;
4366         }
4367
4368         if (handler->cbs.pinup.cb) {
4369                 handler->cbs.pinup.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.pinup.data);
4370                 handler->cbs.pinup.cb = NULL;
4371                 handler->cbs.pinup.data = NULL;
4372         }
4373
4374         if (handler->cbs.group_changed.cb) {
4375                 handler->cbs.group_changed.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.group_changed.data);
4376                 handler->cbs.group_changed.cb = NULL;
4377                 handler->cbs.group_changed.data = NULL;
4378         }
4379
4380         if (handler->cbs.period_changed.cb) {
4381                 handler->cbs.period_changed.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.period_changed.data);
4382                 handler->cbs.period_changed.cb = NULL;
4383                 handler->cbs.period_changed.data = NULL;
4384         }
4385
4386         if (handler->cbs.size_changed.cb) {
4387                 handler->cbs.size_changed.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.size_changed.data);
4388                 handler->cbs.size_changed.cb = NULL;
4389                 handler->cbs.size_changed.data = NULL;
4390         }
4391
4392         if (handler->cbs.pd_created.cb) {
4393                 handler->cbs.pd_created.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.pd_created.data);
4394                 handler->cbs.pd_created.cb = NULL;
4395                 handler->cbs.pd_created.data = NULL;
4396         }
4397
4398         if (handler->cbs.pd_destroyed.cb) {
4399                 handler->cbs.pd_destroyed.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.pd_destroyed.data);
4400                 handler->cbs.pd_destroyed.cb = NULL;
4401                 handler->cbs.pd_destroyed.data = NULL;
4402         }
4403
4404         if (handler->cbs.update_mode.cb) {
4405                 handler->cbs.update_mode.cb(handler, LB_STATUS_ERROR_FAULT, handler->cbs.update_mode.data);
4406                 handler->cbs.update_mode.cb = NULL;
4407                 handler->cbs.update_mode.data = NULL;
4408         }
4409
4410         if (handler->cbs.access_event.cb) {
4411                 handler->cbs.access_event.cb(handler, LB_ACCESS_STATUS_ERROR, handler->cbs.access_event.data);
4412                 handler->cbs.access_event.cb = NULL;
4413                 handler->cbs.access_event.data = NULL;
4414         }
4415
4416         if (handler->cbs.key_event.cb) {
4417                 handler->cbs.key_event.cb(handler, LB_KEY_STATUS_ERROR, handler->cbs.key_event.data);
4418                 handler->cbs.key_event.cb = NULL;
4419                 handler->cbs.key_event.data = NULL;
4420         }
4421
4422         if (handler->common->filename) {
4423                 (void)util_unlink(handler->common->filename);
4424         }
4425
4426         dlist_remove_data(s_info.livebox_list, handler);
4427
4428         handler->state = DESTROYED;
4429         if (lb_common_unref(handler->common, handler) == 0) {
4430                 if (destroy_common) {
4431                         /*!
4432                          * \note
4433                          * Lock file should be deleted after all callbacks are processed.
4434                          */
4435                         lb_destroy_lock_file(handler->common, 0);
4436                         lb_destroy_common_handle(handler->common);
4437                 }
4438         }
4439         free(handler);
4440         DbgPrint("Handler is released\n");
4441         return NULL;
4442 }
4443
4444 int lb_send_delete(struct livebox *handler, int type, ret_cb_t cb, void *data)
4445 {
4446         struct packet *packet;
4447         struct cb_info *cbinfo;
4448         int ret;
4449
4450         if (handler->common->request.deleted) {
4451                 ErrPrint("Already in-progress\n");
4452                 if (cb) {
4453                         cb(handler, LB_STATUS_SUCCESS, data);
4454                 }
4455                 return LB_STATUS_ERROR_BUSY;
4456         }
4457
4458         if (!cb) {
4459                 cb = default_delete_cb;
4460         }
4461
4462         packet = packet_create("delete", "ssi", handler->common->pkgname, handler->common->id, type);
4463         if (!packet) {
4464                 ErrPrint("Failed to build a param\n");
4465                 if (cb) {
4466                         cb(handler, LB_STATUS_ERROR_FAULT, data);
4467                 }
4468
4469                 return LB_STATUS_ERROR_FAULT;
4470         }
4471
4472         cbinfo = create_cb_info(cb, data);
4473         if (!cbinfo) {
4474                 packet_destroy(packet);
4475                 ErrPrint("Failed to create cbinfo\n");
4476                 if (cb) {
4477                         cb(handler, LB_STATUS_ERROR_FAULT, data);
4478                 }
4479
4480                 return LB_STATUS_ERROR_FAULT;
4481         }
4482
4483         ret = master_rpc_async_request(handler, packet, 0, del_ret_cb, cbinfo);
4484         if (ret < 0) {
4485                 /*!
4486                  * Packet is destroyed by master_rpc_async_request.
4487                  */
4488                 destroy_cb_info(cbinfo);
4489
4490                 if (cb) {
4491                         cb(handler, LB_STATUS_ERROR_FAULT, data);
4492                 }
4493         } else {
4494                 handler->common->request.deleted = 1;
4495         }
4496
4497         return ret;
4498 }
4499
4500 EAPI int livebox_client_paused(void)
4501 {
4502         struct packet *packet;
4503
4504         packet = packet_create_noack("client_paused", "d", util_timestamp());
4505         if (!packet) {
4506                 ErrPrint("Failed to create a pause packet\n");
4507                 return LB_STATUS_ERROR_FAULT;
4508         }
4509
4510         return master_rpc_request_only(NULL, packet);
4511 }
4512
4513 EAPI int livebox_client_resumed(void)
4514 {
4515         struct packet *packet;
4516
4517         packet = packet_create_noack("client_resumed", "d", util_timestamp());
4518         if (!packet) {
4519                 ErrPrint("Failed to create a resume packet\n");
4520                 return LB_STATUS_ERROR_FAULT;
4521         }
4522
4523         return master_rpc_request_only(NULL, packet);
4524 }
4525
4526 EAPI int livebox_sync_lb_fb(struct livebox *handler)
4527 {
4528         if (!handler || handler->state != CREATE) {
4529                 ErrPrint("Invalid handle\n");
4530                 return LB_STATUS_ERROR_INVALID;
4531         }
4532
4533         if (!handler->common || handler->common->state != CREATE) {
4534                 ErrPrint("Invalid handle\n");
4535                 return LB_STATUS_ERROR_INVALID;
4536         }
4537
4538         if (!handler->common->id) {
4539                 return LB_STATUS_ERROR_INVALID;
4540         }
4541
4542         return lb_sync_lb_fb(handler->common);
4543 }
4544
4545 int lb_sync_lb_fb(struct livebox_common *common)
4546 {
4547         int ret;
4548
4549         if (fb_type(lb_get_lb_fb(common)) == BUFFER_TYPE_FILE && common->lb.lock_fd >= 0) {
4550                 (void)do_fb_lock(common->lb.lock_fd);
4551                 ret = fb_sync(lb_get_lb_fb(common));
4552                 (void)do_fb_unlock(common->lb.lock_fd);
4553         } else {
4554                 ret = fb_sync(lb_get_lb_fb(common));
4555         }
4556
4557         return ret;
4558 }
4559
4560 int lb_sync_pd_fb(struct livebox_common *common)
4561 {
4562         int ret;
4563
4564         if (fb_type(lb_get_pd_fb(common)) == BUFFER_TYPE_FILE && common->pd.lock_fd >= 0) {
4565                 (void)do_fb_lock(common->pd.lock_fd);
4566                 ret = fb_sync(lb_get_pd_fb(common));
4567                 (void)do_fb_unlock(common->pd.lock_fd);
4568         } else {
4569                 ret = fb_sync(lb_get_pd_fb(common));
4570         }
4571
4572         return ret;
4573 }
4574
4575 EAPI int livebox_sync_pd_fb(struct livebox *handler)
4576 {
4577         if (!handler || handler->state != CREATE) {
4578                 ErrPrint("Invalid handle\n");
4579                 return LB_STATUS_ERROR_INVALID;
4580         }
4581
4582         if (!handler->common || handler->common->state != CREATE) {
4583                 ErrPrint("Invalid handle\n");
4584                 return LB_STATUS_ERROR_INVALID;
4585         }
4586
4587         if (!handler->common->id) {
4588                 ErrPrint("Invalid handle\n");
4589                 return LB_STATUS_ERROR_INVALID;
4590         }
4591
4592         return lb_sync_pd_fb(handler->common);
4593 }
4594
4595 EAPI const char *livebox_alt_icon(struct livebox *handler)
4596 {
4597         if (!handler || handler->state != CREATE) {
4598                 ErrPrint("Handler is not valid[%p]\n", handler);
4599                 return NULL;
4600         }
4601
4602         if (!handler->common || handler->common->state != CREATE) {
4603                 ErrPrint("Handler is not valid\n");
4604                 return NULL;
4605         }
4606
4607         return handler->common->alt.icon;
4608 }
4609
4610 EAPI const char *livebox_alt_name(struct livebox *handler)
4611 {
4612         if (!handler || handler->state != CREATE) {
4613                 ErrPrint("Handler is not valid[%p]\n", handler);
4614                 return NULL;
4615         }
4616
4617         if (!handler->common || handler->common->state != CREATE) {
4618                 ErrPrint("Handler is not valid\n");
4619                 return NULL;
4620         }
4621
4622         return handler->common->alt.name;
4623 }
4624
4625 EAPI int livebox_acquire_fb_lock(struct livebox *handler, int is_pd)
4626 {
4627         int ret = LB_STATUS_SUCCESS;
4628         int fd;
4629
4630         if (!handler || handler->state != CREATE) {
4631                 ErrPrint("Handler is not valid[%p]\n", handler);
4632                 return LB_STATUS_ERROR_INVALID;
4633         }
4634
4635         if (!handler->common || handler->common->state != CREATE) {
4636                 ErrPrint("Handler is not valid\n");
4637                 return LB_STATUS_ERROR_INVALID;
4638         }
4639
4640         if (!handler->common->id) {
4641                 ErrPrint("Handler is not valid[%p]\n", handler);
4642                 return LB_STATUS_ERROR_INVALID;
4643         }
4644
4645         if (is_pd) {
4646                 if (!handler->common->pd.lock || handler->common->pd.lock_fd < 0) {
4647                         DbgPrint("Lock: %s (%d)\n", handler->common->pd.lock, handler->common->pd.lock_fd);
4648                         return LB_STATUS_ERROR_INVALID;
4649                 }
4650
4651                 if (fb_type(lb_get_pd_fb(handler->common)) == BUFFER_TYPE_FILE) {
4652                         return LB_STATUS_SUCCESS;
4653                 }
4654
4655                 fd = handler->common->pd.lock_fd;
4656         } else {
4657                 if (!handler->common->lb.lock || handler->common->lb.lock_fd < 0) {
4658                         DbgPrint("Lock: %s (%d)\n", handler->common->lb.lock, handler->common->lb.lock_fd);
4659                         return LB_STATUS_ERROR_INVALID;
4660                 }
4661
4662                 if (fb_type(lb_get_lb_fb(handler->common)) == BUFFER_TYPE_FILE) {
4663                         return LB_STATUS_SUCCESS;
4664                 }
4665
4666                 fd = handler->common->lb.lock_fd;
4667         }
4668
4669         ret = do_fb_lock(fd);
4670
4671         return ret == 0 ? LB_STATUS_SUCCESS : LB_STATUS_ERROR_FAULT;
4672 }
4673
4674 EAPI int livebox_release_fb_lock(struct livebox *handler, int is_pd)
4675 {
4676         int ret = LB_STATUS_SUCCESS;
4677         int fd;
4678
4679         if (!handler || handler->state != CREATE) {
4680                 ErrPrint("Invalid handle\n");
4681                 return LB_STATUS_ERROR_INVALID;
4682         }
4683
4684         if (!handler->common || handler->common->state != CREATE) {
4685                 ErrPrint("Invalid handle\n");
4686                 return LB_STATUS_ERROR_INVALID;
4687         }
4688
4689         if (!handler->common->id) {
4690                 ErrPrint("Handler is not valid[%p]\n", handler);
4691                 return LB_STATUS_ERROR_INVALID;
4692         }
4693
4694         if (is_pd) {
4695                 if (!handler->common->pd.lock || handler->common->pd.lock_fd < 0) {
4696                         DbgPrint("Unlock: %s (%d)\n", handler->common->pd.lock, handler->common->pd.lock_fd);
4697                         return LB_STATUS_ERROR_INVALID;
4698                 }
4699
4700                 if (fb_type(lb_get_pd_fb(handler->common)) == BUFFER_TYPE_FILE) {
4701                         return LB_STATUS_SUCCESS;
4702                 }
4703
4704                 fd = handler->common->pd.lock_fd;
4705         } else {
4706                 if (!handler->common->lb.lock || handler->common->lb.lock_fd < 0) {
4707                         DbgPrint("Unlock: %s (%d)\n", handler->common->lb.lock, handler->common->lb.lock_fd);
4708                         return LB_STATUS_ERROR_INVALID;
4709                 }
4710
4711                 if (fb_type(lb_get_lb_fb(handler->common)) == BUFFER_TYPE_FILE) {
4712                         return LB_STATUS_SUCCESS;
4713                 }
4714
4715                 fd = handler->common->lb.lock_fd;
4716         }
4717
4718         ret = do_fb_unlock(fd);
4719
4720         return ret == 0 ? LB_STATUS_SUCCESS : LB_STATUS_ERROR_FAULT;
4721 }
4722
4723 EAPI int livebox_set_option(enum livebox_option_type option, int state)
4724 {
4725         int ret = LB_STATUS_SUCCESS;
4726
4727         switch (option) {
4728         case LB_OPTION_MANUAL_SYNC:
4729                 conf_set_manual_sync(state);
4730                 break;
4731         case LB_OPTION_FRAME_DROP_FOR_RESIZE:
4732                 conf_set_frame_drop_for_resizing(state);
4733                 break;
4734         case LB_OPTION_SHARED_CONTENT:
4735                 conf_set_shared_content(state);
4736                 break;
4737         default:
4738                 ret = LB_STATUS_ERROR_INVALID;
4739                 break;
4740         }
4741
4742         return ret;
4743 }
4744
4745 EAPI int livebox_option(enum livebox_option_type option)
4746 {
4747         int ret;
4748
4749         switch (option) {
4750         case LB_OPTION_MANUAL_SYNC:
4751                 ret = conf_manual_sync();
4752                 break;
4753         case LB_OPTION_FRAME_DROP_FOR_RESIZE:
4754                 ret = conf_frame_drop_for_resizing();
4755                 break;
4756         case LB_OPTION_SHARED_CONTENT:
4757                 ret = conf_shared_content();
4758                 break;
4759         default:
4760                 ret = LB_STATUS_ERROR_INVALID;
4761                 break;
4762         }
4763
4764         return ret;
4765 }
4766
4767 /* End of a file */