Various patches are applied
[apps/livebox/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
23 #include <aul.h>
24 #include <dlog.h>
25
26 #include <com-core_packet.h>
27 #include <packet.h>
28 #include <livebox-service.h>
29 #include <livebox-errno.h>
30
31 #include "debug.h"
32 #include "fb.h"
33 #include "livebox.h"
34 #include "livebox_internal.h"
35 #include "dlist.h"
36 #include "util.h"
37 #include "master_rpc.h"
38 #include "client.h"
39 #include "critical_log.h"
40
41 #define EAPI __attribute__((visibility("default")))
42 #define MINIMUM_EVENT   s_info.event_filter
43
44 #if defined(FLOG)
45 FILE *__file_log_fp;
46 #endif
47
48 static struct info {
49         struct dlist *livebox_list;
50         struct dlist *event_list;
51         struct dlist *fault_list;
52         int init_count;
53         int prevent_overwrite;
54         double event_filter;
55 } s_info = {
56         .livebox_list = NULL,
57         .event_list = NULL,
58         .fault_list = NULL,
59         .init_count = 0,
60         .prevent_overwrite = 0,
61         .event_filter = 0.02f,
62 };
63
64 struct cb_info {
65         ret_cb_t cb;
66         void *data;
67 };
68
69 struct event_info {
70         int (*handler)(struct livebox *handler, enum livebox_event_type event, void *data);
71         void *user_data;
72 };
73
74 struct fault_info {
75         int (*handler)(enum livebox_fault_type event, const char *pkgname, const char *filename, const char *func, void *data);
76         void *user_data;
77 };
78
79 static inline void default_create_cb(struct livebox *handler, int ret, void *data)
80 {
81         DbgPrint("Default created event handler: %d\n", ret);
82 }
83
84 static inline void default_delete_cb(struct livebox *handler, int ret, void *data)
85 {
86         DbgPrint("Default deleted event handler: %d\n", ret);
87 }
88
89 static inline void default_pinup_cb(struct livebox *handler, int ret, void *data)
90 {
91         DbgPrint("Default pinup event handler: %d\n", ret);
92 }
93
94 static inline void default_group_changed_cb(struct livebox *handler, int ret, void *data)
95 {
96         DbgPrint("Default group changed event handler: %d\n", ret);
97 }
98
99 static inline void default_period_changed_cb(struct livebox *handler, int ret, void *data)
100 {
101         DbgPrint("Default period changed event handler: %d\n", ret);
102 }
103
104 static inline void default_pd_created_cb(struct livebox *handler, int ret, void *data)
105 {
106         DbgPrint("Default PD created event handler: %d\n", ret);
107 }
108
109 static inline void default_pd_destroyed_cb(struct livebox *handler, int ret, void *data)
110 {
111         DbgPrint("Default PD destroyed event handler: %d\n", ret);
112 }
113
114 static inline void default_lb_size_changed_cb(struct livebox *handler, int ret, void *data)
115 {
116         DbgPrint("Default LB size changed event handler: %d\n", ret);
117 }
118
119 static inline void default_update_mode_cb(struct livebox *handler, int ret, void *data)
120 {
121         DbgPrint("Default update mode set event handler: %d\n", ret);
122 }
123
124 static inline void default_access_event_cb(struct livebox *handler, int ret, void *data)
125 {
126         DbgPrint("Default access event handler: %d\n", ret);
127 }
128
129 static inline __attribute__((always_inline)) struct cb_info *create_cb_info(ret_cb_t cb, void *data)
130 {
131         struct cb_info *info;
132
133         info = malloc(sizeof(*info));
134         if (!info) {
135                 CRITICAL_LOG("Heap: %s\n", strerror(errno));
136                 return NULL;
137         }
138
139         info->cb = cb;
140         info->data = data;
141         return info;
142 }
143
144 static inline void destroy_cb_info(struct cb_info *info)
145 {
146         free(info);
147 }
148
149 static void update_mode_cb(struct livebox *handler, const struct packet *result, void *data)
150 {
151         int ret;
152
153         if (!result) {
154                 ret = LB_STATUS_ERROR_FAULT;
155                 goto errout;
156         } else if (packet_get(result, "i", &ret) != 1) {
157                 ErrPrint("Invalid argument\n");
158                 ret = LB_STATUS_ERROR_INVALID;
159                 goto errout;
160         }
161
162         if (ret < 0) {
163                 DbgPrint("Resize request is failed: %d\n", ret);
164                 goto errout;
165         }
166
167         return;
168
169 errout:
170         handler->update_mode_cb(handler, ret, handler->update_mode_cbdata);
171         handler->update_mode_cb = NULL;
172         handler->update_mode_cbdata = NULL;
173         return;
174 }
175
176 static void resize_cb(struct livebox *handler, const struct packet *result, void *data)
177 {
178         int ret;
179         struct cb_info *info = data;
180         ret_cb_t cb;
181         void *cbdata;
182
183         cb = info->cb;
184         cbdata = info->data;
185         destroy_cb_info(info);
186
187         if (!result) {
188                 ret = LB_STATUS_ERROR_FAULT;
189                 goto errout;
190         } else if (packet_get(result, "i", &ret) != 1) {
191                 ErrPrint("Invalid argument\n");
192                 ret = LB_STATUS_ERROR_INVALID;
193                 goto errout;
194         }
195
196         /*!
197          * \note
198          * In case of resize request,
199          * The livebox handler will not have resized value right after this callback,
200          * It can only get the new size when it makes updates.
201          *
202          * So the user can only get the resized value(result) from the first update event
203          * after this request.
204          */
205         if (ret < 0) {
206                 DbgPrint("Resize request is failed: %d\n", ret);
207                 goto errout;
208         }
209
210         DbgPrint("Resize request is successfully sent\n");
211         return;
212
213 errout:
214         handler->size_changed_cb(handler, ret, handler->size_cbdata);
215         handler->size_changed_cb = NULL;
216         handler->size_cbdata = NULL;
217 }
218
219 static void text_signal_cb(struct livebox *handler, const struct packet *result, void *data)
220 {
221         int ret;
222         void *cbdata;
223         struct cb_info *info = data;
224         ret_cb_t cb;
225
226         cbdata = info->data;
227         cb = info->cb;
228         destroy_cb_info(info);
229
230         if (!result) {
231                 ret = LB_STATUS_ERROR_FAULT;
232         } else if (packet_get(result, "i", &ret) != 1) {
233                 ErrPrint("Invalid argument\n");
234                 ret = LB_STATUS_ERROR_INVALID;
235         }
236
237         if (cb)
238                 cb(handler, ret, cbdata);
239         return;
240 }
241
242 static void set_group_ret_cb(struct livebox *handler, const struct packet *result, void *data)
243 {
244         int ret;
245
246         if (!result) {
247                 ret = LB_STATUS_ERROR_FAULT;
248                 goto errout;
249         } else if (packet_get(result, "i", &ret) != 1) {
250                 ErrPrint("Invalid argument\n");
251                 ret = LB_STATUS_ERROR_INVALID;
252                 goto errout;
253         }
254
255         if (ret < 0)
256                 goto errout;
257
258         return;
259
260 errout:
261         handler->group_changed_cb(handler, ret, handler->group_cbdata);
262         handler->group_changed_cb = NULL;
263         handler->group_cbdata = NULL;
264 }
265
266 static void period_ret_cb(struct livebox *handler, const struct packet *result, void *data)
267 {
268         int ret;
269
270         if (!result) {
271                 ret = LB_STATUS_ERROR_FAULT;
272                 goto errout;
273         } else if (packet_get(result, "i", &ret) != 1) {
274                 ErrPrint("Invalid argument\n");
275                 ret = LB_STATUS_ERROR_INVALID;
276                 goto errout;
277         }
278
279         if (ret < 0)
280                 goto errout;
281
282         DbgPrint("Succeed to send period change request, waiting result\n");
283         return;
284
285 errout:
286         handler->period_changed_cb(handler, ret, handler->period_cbdata);
287         handler->period_changed_cb = NULL;
288         handler->period_cbdata = NULL;
289 }
290
291 static void del_ret_cb(struct livebox *handler, const struct packet *result, void *data)
292 {
293         struct cb_info *info = data;
294         int ret;
295         ret_cb_t cb;
296         void *cbdata;
297
298         cb = info->cb;
299         cbdata = info->data;
300         destroy_cb_info(info);
301
302         if (!result) {
303                 ErrPrint("Connection lost?\n");
304                 ret = LB_STATUS_ERROR_FAULT;
305         } else if (packet_get(result, "i", &ret) != 1) {
306                 ErrPrint("Invalid argument\n");
307                 ret = LB_STATUS_ERROR_INVALID;
308         }
309
310         if (ret == 0) {
311                 DbgPrint("Returns %d (waiting deleted event)\n", ret);
312                 handler->deleted_cb = cb;
313                 handler->deleted_cbdata = cbdata;
314         } else if (cb) {
315                 cb(handler, ret, cbdata);
316         }
317
318         /*!
319          * \note
320          * Do not call the deleted callback from here.
321          * master will send the "deleted" event.
322          * Then invoke this callback.
323          *
324          * if (handler->deleted_cb)
325          *      handler->deleted_cb(handler, ret, handler->deleted_cbdata);
326          */
327 }
328
329 static void new_ret_cb(struct livebox *handler, const struct packet *result, void *data)
330 {
331         int ret;
332         struct cb_info *info = data;
333         ret_cb_t cb;
334         void *cbdata;
335
336         cb = info->cb;
337         cbdata = info->data;
338         destroy_cb_info(info);
339
340         if (!result) {
341                 ret = LB_STATUS_ERROR_FAULT;
342         } else if (packet_get(result, "i", &ret) != 1) {
343                 ret = LB_STATUS_ERROR_INVALID;
344         }
345
346         if (ret >= 0) {
347                 DbgPrint("new request is sent, just waiting the created event\n");
348                 handler->created_cb = cb;
349                 handler->created_cbdata = cbdata;
350
351                 /*!
352                  * \note
353                  * Don't go anymore ;)
354                  */
355                 return;
356         } else if (cb) {
357                 /*!
358                  * \note
359                  * It means the current instance is not created,
360                  * so user has to know about this.
361                  * notice it to user using "deleted" event.
362                  */
363                 cb(handler, ret, cbdata);
364         }
365
366         lb_unref(handler);
367 }
368
369 static void pd_create_cb(struct livebox *handler, const struct packet *result, void *data)
370 {
371         int ret;
372
373         if (!result) {
374                 ret = LB_STATUS_ERROR_FAULT;
375                 goto errout;
376         } else if (packet_get(result, "i", &ret) != 1) {
377                 ret = LB_STATUS_ERROR_INVALID;
378                 goto errout;
379         }
380
381         if (ret < 0) {
382                 DbgPrint("Failed to create a PD\n");
383                 goto errout;
384         }
385
386         return;
387
388 errout:
389         handler->pd_created_cb(handler, ret, handler->pd_created_cbdata);
390         handler->pd_created_cb = NULL;
391         handler->pd_created_cbdata = NULL;
392 }
393
394 static void activated_cb(struct livebox *handler, const struct packet *result, void *data)
395 {
396         int ret;
397         struct cb_info *info = data;
398         void *cbdata;
399         ret_cb_t cb;
400         const char *pkgname = "";
401
402         cbdata = info->data;
403         cb = info->cb;
404         destroy_cb_info(info);
405
406         if (!result) {
407                 ret = LB_STATUS_ERROR_FAULT;
408         } else if (packet_get(result, "is", &ret, &pkgname) != 2) {
409                 ret = LB_STATUS_ERROR_INVALID;
410         }
411
412         if (cb)
413                 cb(handler, ret, cbdata);
414 }
415
416 static void pd_destroy_cb(struct livebox *handler, const struct packet *result, void *data)
417 {
418         int ret;
419         ret_cb_t cb;
420         void *cbdata;
421         struct cb_info *info = data;
422
423         cbdata = info->data;
424         cb = info->cb;
425         destroy_cb_info(info);
426
427         if (!result) {
428                 DbgPrint("Result is NIL (may connection lost)\n");
429                 ret = LB_STATUS_ERROR_FAULT;
430         } else if (packet_get(result, "i", &ret) != 1) {
431                 DbgPrint("Invalid parameter\n");
432                 ret = LB_STATUS_ERROR_INVALID;
433         }
434
435         if (ret == 0) {
436                 DbgPrint("PD Destroyed callback prepared\n");
437                 handler->pd_destroyed_cb = cb;
438                 handler->pd_destroyed_cbdata = cbdata;
439         } else if (cb) {
440                 DbgPrint("PD is not desroyed (forcely reset, pd flag)\n");
441                 handler->is_pd_created = 0;
442                 cb(handler, ret, cbdata);
443         }
444 }
445
446 static void delete_cluster_cb(struct livebox *handler, const struct packet *result, void *data)
447 {
448         struct cb_info *info = data;
449         int ret;
450         ret_cb_t cb;
451         void *cbdata;
452
453         cb = info->cb;
454         cbdata = info->data;
455         destroy_cb_info(info);
456
457         if (!result) {
458                 ret = LB_STATUS_ERROR_FAULT;
459         } else if (packet_get(result, "i", &ret) != 1) {
460                 ret = LB_STATUS_ERROR_INVALID;
461         }
462
463         DbgPrint("Delete category returns: %d\n", ret);
464
465         if (cb)
466                 cb(handler, ret, cbdata);
467 }
468
469 static void delete_category_cb(struct livebox *handler, const struct packet *result, void *data)
470 {
471         struct cb_info *info = data;
472         int ret;
473         ret_cb_t cb;
474         void *cbdata;
475
476         cb = info->cb;
477         cbdata = info->data;
478         destroy_cb_info(info);
479
480         if (!result)
481                 ret = LB_STATUS_ERROR_FAULT;
482         else if (packet_get(result, "i", &ret) != 1)
483                 ret = LB_STATUS_ERROR_INVALID;
484
485         DbgPrint("Delete category returns: %d\n", ret);
486
487         if (cb)
488                 cb(handler, ret, cbdata);
489 }
490
491 static void pixmap_acquired_cb(struct livebox *handler, const struct packet *result, void *data)
492 {
493         int ret;
494         ret_cb_t cb;
495         void *cbdata;
496         struct cb_info *info = data;
497
498         cb = info->cb;
499         cbdata = info->data;
500         destroy_cb_info(info);
501
502         if (!result)
503                 ret = 0; /* PIXMAP 0 means error */
504         else if (packet_get(result, "i", &ret) != 1)
505                 ret = 0;
506
507         if (cb)
508                 cb(handler, ret, cbdata);
509 }
510
511 static void pinup_done_cb(struct livebox *handler, const struct packet *result, void *data)
512 {
513         int ret;
514
515         if (!result) {
516                 ret = LB_STATUS_ERROR_FAULT;
517                 goto errout;
518         } else if (packet_get(result, "i", &ret) != 1) {
519                 goto errout;
520         }
521
522         if (ret < 0)
523                 goto errout;
524
525         return;
526
527 errout:
528         handler->pinup_cb(handler, ret, handler->pinup_cbdata);
529         handler->pinup_cb = NULL;
530         handler->pinup_cbdata = NULL;
531 }
532
533 static void access_ret_cb(struct livebox *handler, const struct packet *result, void *data)
534 {
535         int ret;
536
537         if (!result) {
538                 ret = LB_STATUS_ERROR_FAULT;
539                 return;
540         }
541
542         if (packet_get(result, "i", &ret) != 1) {
543                 ret = LB_STATUS_ERROR_INVALID;
544                 return;
545         }
546
547         if (ret != LB_STATUS_SUCCESS) {
548                 goto errout;
549         }
550
551         DbgPrint("Access event is successfully sent. waiting result\n");
552         return;
553
554 errout:
555         handler->access_event_cb(handler, ret, handler->access_event_cbdata);
556         handler->access_event_cb = NULL;
557         handler->access_event_cbdata = NULL;
558         return;
559 }
560
561 static int send_access_event(struct livebox *handler, const char *event, int x, int y)
562 {
563         struct packet *packet;
564         double timestamp;
565
566         timestamp = util_timestamp();
567
568         packet = packet_create(event, "ssdii", handler->pkgname, handler->id, timestamp, x, y);
569         if (!packet) {
570                 ErrPrint("Failed to build packet\n");
571                 return LB_STATUS_ERROR_FAULT;
572         }
573
574         DbgPrint("Send: %dx%d\n", x, y);
575
576         return master_rpc_async_request(handler, packet, 0, access_ret_cb, NULL);
577 }
578
579 static int send_mouse_event(struct livebox *handler, const char *event, int x, int y)
580 {
581         struct packet *packet;
582         double timestamp;
583
584         timestamp = util_timestamp();
585         packet = packet_create_noack(event, "ssdii", handler->pkgname, handler->id, timestamp, x, y);
586         if (!packet) {
587                 ErrPrint("Failed to build param\n");
588                 return LB_STATUS_ERROR_FAULT;
589         }
590
591         DbgPrint("Send: %dx%d\n", x, y);
592
593         return master_rpc_request_only(handler, packet);
594 }
595
596 EAPI int livebox_init(void *disp)
597 {
598         const char *env;
599
600         if (s_info.init_count > 0) {
601                 s_info.init_count++;
602                 return LB_STATUS_SUCCESS;
603         }
604         env = getenv("PROVIDER_DISABLE_PREVENT_OVERWRITE");
605         if (env && !strcasecmp(env, "true"))
606                 s_info.prevent_overwrite = 1;
607
608         env = getenv("PROVIDER_EVENT_FILTER");
609         if (env)
610                 sscanf(env, "%lf", &MINIMUM_EVENT);
611
612 #if defined(FLOG)
613         char filename[BUFSIZ];
614         snprintf(filename, sizeof(filename), "/tmp/%d.box.log", getpid());
615         __file_log_fp = fopen(filename, "w+t");
616         if (!__file_log_fp)
617                 __file_log_fp = fdopen(1, "w+t");
618 #endif
619         critical_log_init("viewer");
620         livebox_service_init();
621         fb_init(disp);
622
623         client_init();
624
625         s_info.init_count++;
626         return LB_STATUS_SUCCESS;
627 }
628
629 EAPI int livebox_fini(void)
630 {
631         if (s_info.init_count <= 0) {
632                 DbgPrint("Didn't initialized\n");
633                 return LB_STATUS_ERROR_INVALID;
634         }
635
636         s_info.init_count--;
637         if (s_info.init_count > 0) {
638                 DbgPrint("init count : %d\n", s_info.init_count);
639                 return LB_STATUS_SUCCESS;
640         }
641
642         client_fini();
643         fb_fini();
644         livebox_service_fini();
645         critical_log_fini();
646         return LB_STATUS_SUCCESS;
647 }
648
649 static inline char *lb_pkgname(const char *pkgname)
650 {
651         char *lb;
652
653         lb = livebox_service_pkgname(pkgname);
654         if (!lb) {
655                 if (util_validate_livebox_package(pkgname) == 0)
656                         return strdup(pkgname);
657         }
658
659         return lb;
660 }
661
662 /*!
663  * Just wrapping the livebox_add_with_size function.
664  */
665 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)
666 {
667         return livebox_add_with_size(pkgname, content, cluster, category, period, LB_SIZE_TYPE_UNKNOWN, cb, data);
668 }
669
670 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)
671 {
672         struct livebox *handler;
673         struct packet *packet;
674         int ret;
675         int width = 0;
676         int height = 0;
677         struct cb_info *cbinfo;
678
679         if (!pkgname || !cluster || !category) {
680                 ErrPrint("Invalid arguments: pkgname[%p], cluster[%p], category[%p]\n",
681                                                                 pkgname, cluster, category);
682                 return NULL;
683         }
684
685         if (type != LB_SIZE_TYPE_UNKNOWN)
686                 livebox_service_get_size(type, &width, &height);
687
688         handler = calloc(1, sizeof(*handler));
689         if (!handler) {
690                 ErrPrint("Error: %s\n", strerror(errno));
691                 return NULL;
692         }
693
694         handler->pkgname = lb_pkgname(pkgname);
695         if (!handler->pkgname) {
696                 free(handler);
697                 return NULL;
698         }
699
700         if (livebox_service_is_enabled(handler->pkgname) == 0) {
701                 DbgPrint("Livebox [%s](%s) is disabled package\n", handler->pkgname, pkgname);
702                 free(handler->pkgname);
703                 free(handler);
704                 return NULL;
705         }
706
707         if (content) {
708                 handler->content = strdup(content);
709                 if (!handler->content) {
710                         ErrPrint("Error: %s\n", strerror(errno));
711                         free(handler->pkgname);
712                         free(handler);
713                         return NULL;
714                 }
715         } else {
716                 handler->content = livebox_service_content(handler->pkgname);
717         }
718
719         handler->cluster = strdup(cluster);
720         if (!handler->cluster) {
721                 ErrPrint("Error: %s\n", strerror(errno));
722                 free(handler->content);
723                 free(handler->pkgname);
724                 free(handler);
725                 return NULL;
726         }
727
728         handler->category = strdup(category);
729         if (!handler->category) {
730                 ErrPrint("Error: %s\n", strerror(errno));
731                 free(handler->cluster);
732                 free(handler->content);
733                 free(handler->pkgname);
734                 free(handler);
735                 return NULL;
736         }
737
738         if (!cb)
739                 cb = default_create_cb;
740
741         /* Data provider will set this */
742         handler->lb.type = _LB_TYPE_FILE;
743         handler->pd.type = _PD_TYPE_SCRIPT;
744         handler->lb.period = period;
745
746         /* Used for handling the mouse event on a box */
747         handler->lb.mouse_event = livebox_service_mouse_event(handler->pkgname);
748
749         /* Cluster infomration is not determined yet */
750         handler->nr_of_sizes = 0x01;
751
752         handler->timestamp = util_timestamp();
753         handler->is_user = 1;
754         handler->visible = LB_SHOW;
755
756         s_info.livebox_list = dlist_append(s_info.livebox_list, handler);
757
758         packet = packet_create("new", "dssssdii", handler->timestamp, handler->pkgname, handler->content, cluster, category, period, width, height);
759         if (!packet) {
760                 ErrPrint("Failed to create a new packet\n");
761                 free(handler->category);
762                 free(handler->cluster);
763                 free(handler->content);
764                 free(handler->pkgname);
765                 free(handler);
766                 return NULL;
767         }
768
769         cbinfo = create_cb_info(cb, data);
770         if (!cbinfo) {
771                 ErrPrint("Failed to create a cbinfo\n");
772                 packet_destroy(packet);
773                 free(handler->category);
774                 free(handler->cluster);
775                 free(handler->content);
776                 free(handler->pkgname);
777                 free(handler);
778                 return NULL;
779         }
780
781         ret = master_rpc_async_request(handler, packet, 0, new_ret_cb, cbinfo);
782         if (ret < 0) {
783                 ErrPrint("Failed to send a new packet\n");
784                 destroy_cb_info(cbinfo);
785                 free(handler->category);
786                 free(handler->cluster);
787                 free(handler->content);
788                 free(handler->pkgname);
789                 free(handler);
790                 return NULL;
791         }
792
793         DbgPrint("Successfully sent a new request ([%lf] %s)\n", handler->timestamp, handler->pkgname);
794         handler->state = CREATE;
795         return lb_ref(handler);
796 }
797
798 EAPI double livebox_period(struct livebox *handler)
799 {
800         if (!handler || handler->state != CREATE || !handler->id) {
801                 ErrPrint("Handler is not valid\n");
802                 return 0.0f;
803         }
804
805         return handler->lb.period;
806 }
807
808 EAPI int livebox_set_period(struct livebox *handler, double period, ret_cb_t cb, void *data)
809 {
810         struct packet *packet;
811         int ret;
812
813         if (!handler || handler->state != CREATE || !handler->id) {
814                 ErrPrint("Handler is not valid\n");
815                 return LB_STATUS_ERROR_INVALID;
816         }
817
818         if (handler->period_changed_cb) {
819                 ErrPrint("Previous request for changing period is not finished\n");
820                 return LB_STATUS_ERROR_BUSY;
821         }
822
823         if (!handler->is_user) {
824                 ErrPrint("CA Livebox is not able to change the period\n");
825                 return LB_STATUS_ERROR_PERMISSION;
826         }
827
828         if (handler->lb.period == period) {
829                 DbgPrint("No changes\n");
830                 return LB_STATUS_ERROR_ALREADY;
831         }
832
833         packet = packet_create("set_period", "ssd", handler->pkgname, handler->id, period);
834         if (!packet) {
835                 ErrPrint("Failed to build a packet %s\n", handler->pkgname);
836                 return LB_STATUS_ERROR_FAULT;
837         }
838
839         if (!cb)
840                 cb = default_period_changed_cb;
841
842         handler->period_changed_cb = cb;
843         handler->period_cbdata = data;
844
845         ret = master_rpc_async_request(handler, packet, 0, period_ret_cb, NULL);
846         if (ret < 0) {
847                 handler->period_changed_cb = NULL;
848                 handler->period_cbdata = NULL;
849         }
850
851         return ret;
852 }
853
854 EAPI int livebox_del(struct livebox *handler, ret_cb_t cb, void *data)
855 {
856         if (!handler) {
857                 ErrPrint("Handler is NIL\n");
858                 return LB_STATUS_ERROR_INVALID;
859         }
860
861         if (handler->state != CREATE) {
862                 ErrPrint("Handler is already deleted\n");
863                 return LB_STATUS_ERROR_INVALID;
864         }
865
866         handler->state = DELETE;
867
868         if (!handler->id) {
869                 /*!
870                  * \note
871                  * The id is not determined yet.
872                  * It means a user didn't receive created event yet.
873                  * Then just stop to delete procedure from here.
874                  * Because the "created" event handler will release this.
875                  * By the way, if the user adds any callback for getting return status of this,
876                  * call it at here.
877                  */
878                 if (cb)
879                         cb(handler, 0, data);
880                 return LB_STATUS_SUCCESS;
881         }
882
883         if (!cb)
884                 cb = default_delete_cb;
885
886         return lb_send_delete(handler, cb, data);
887 }
888
889 EAPI int livebox_set_fault_handler(int (*cb)(enum livebox_fault_type, const char *, const char *, const char *, void *), void *data)
890 {
891         struct fault_info *info;
892
893         if (!cb)
894                 return LB_STATUS_ERROR_INVALID;
895
896         info = malloc(sizeof(*info));
897         if (!info) {
898                 CRITICAL_LOG("Heap: %s\n", strerror(errno));
899                 return LB_STATUS_ERROR_MEMORY;
900         }
901
902         info->handler = cb;
903         info->user_data = data;
904
905         s_info.fault_list = dlist_append(s_info.fault_list, info);
906         return LB_STATUS_SUCCESS;
907 }
908
909 EAPI void *livebox_unset_fault_handler(int (*cb)(enum livebox_fault_type, const char *, const char *, const char *, void *))
910 {
911         struct fault_info *info;
912         struct dlist *l;
913
914         dlist_foreach(s_info.fault_list, l, info) {
915                 if (info->handler == cb) {
916                         void *data;
917                         s_info.fault_list = dlist_remove(s_info.fault_list, l);
918                         data = info->user_data;
919                         free(info);
920
921                         return data;
922                 }
923         }
924
925         return NULL;
926 }
927
928 EAPI int livebox_set_event_handler(int (*cb)(struct livebox *, enum livebox_event_type, void *), void *data)
929 {
930         struct event_info *info;
931
932         if (!cb) {
933                 ErrPrint("Invalid argument cb is nil\n");
934                 return LB_STATUS_ERROR_INVALID;
935         }
936
937         info = malloc(sizeof(*info));
938         if (!info) {
939                 CRITICAL_LOG("Heap: %s\n", strerror(errno));
940                 return LB_STATUS_ERROR_MEMORY;
941         }
942
943         info->handler = cb;
944         info->user_data = data;
945
946         s_info.event_list = dlist_append(s_info.event_list, info);
947         return LB_STATUS_SUCCESS;
948 }
949
950 EAPI void *livebox_unset_event_handler(int (*cb)(struct livebox *, enum livebox_event_type, void *))
951 {
952         struct event_info *info;
953         struct dlist *l;
954
955         dlist_foreach(s_info.event_list, l, info) {
956                 if (info->handler == cb) {
957                         void *data;
958
959                         s_info.event_list = dlist_remove(s_info.event_list, l);
960                         data = info->user_data;
961                         free(info);
962
963                         return data;
964                 }
965         }
966
967         return NULL;
968 }
969
970 EAPI int livebox_set_update_mode(struct livebox *handler, int active_update, ret_cb_t cb, void *data)
971 {
972         struct packet *packet;
973         int ret;
974
975         if (!handler) {
976                 ErrPrint("Handler is NIL\n");
977                 return LB_STATUS_ERROR_INVALID;
978         }
979
980         if (handler->state != CREATE || !handler->id)
981                 return LB_STATUS_ERROR_INVALID;
982
983         if (handler->update_mode_cb) {
984                 ErrPrint("Previous update_mode cb is not finished yet\n");
985                 return LB_STATUS_ERROR_BUSY;
986         }
987
988         if (handler->is_active_update == active_update)
989                 return LB_STATUS_ERROR_ALREADY;
990
991         if (!handler->is_user)
992                 return LB_STATUS_ERROR_PERMISSION;
993
994         packet = packet_create("update_mode", "ssi", handler->pkgname, handler->id, active_update);
995         if (!packet)
996                 return LB_STATUS_ERROR_FAULT;
997
998         if (!cb)
999                 cb = default_update_mode_cb;
1000
1001         handler->update_mode_cb = cb;
1002         handler->update_mode_cbdata = data;
1003
1004         ret = master_rpc_async_request(handler, packet, 0, update_mode_cb, NULL);
1005         if (ret < 0) {
1006                 handler->update_mode_cb = NULL;
1007                 handler->update_mode_cbdata = NULL;
1008         }
1009
1010         return ret;
1011 }
1012
1013 EAPI int livebox_is_active_update(struct livebox *handler)
1014 {
1015         if (!handler) {
1016                 ErrPrint("Handler is NIL\n");
1017                 return LB_STATUS_ERROR_INVALID;
1018         }
1019
1020         if (handler->state != CREATE || !handler->id)
1021                 return LB_STATUS_ERROR_INVALID;
1022
1023         return handler->is_active_update;
1024 }
1025
1026 EAPI int livebox_resize(struct livebox *handler, int type, ret_cb_t cb, void *data)
1027 {
1028         struct packet *packet;
1029         int w;
1030         int h;
1031         int ret;
1032
1033         if (!handler) {
1034                 ErrPrint("Handler is NIL\n");
1035                 return LB_STATUS_ERROR_INVALID;
1036         }
1037
1038         if (handler->state != CREATE || !handler->id) {
1039                 ErrPrint("Handler is not valid\n");
1040                 return LB_STATUS_ERROR_INVALID;
1041         }
1042
1043         if (handler->size_changed_cb) {
1044                 ErrPrint("Previous resize request is not finished yet\n");
1045                 return LB_STATUS_ERROR_BUSY;
1046         }
1047
1048         if (!handler->is_user) {
1049                 ErrPrint("CA Livebox is not able to be resized\n");
1050                 return LB_STATUS_ERROR_PERMISSION;
1051         }
1052
1053         if (livebox_service_get_size(type, &w, &h) != 0) {
1054                 ErrPrint("Invalid size type\n");
1055                 return LB_STATUS_ERROR_INVALID;
1056         }
1057
1058         if (handler->lb.width == w && handler->lb.height == h) {
1059                 DbgPrint("No changes\n");
1060                 return LB_STATUS_ERROR_ALREADY;;
1061         }
1062
1063         packet = packet_create("resize", "ssii", handler->pkgname, handler->id, w, h);
1064         if (!packet) {
1065                 ErrPrint("Failed to build param\n");
1066                 return LB_STATUS_ERROR_FAULT;
1067         }
1068
1069         if (!cb)
1070                 cb = default_lb_size_changed_cb;
1071
1072         handler->size_changed_cb = cb;
1073         handler->size_cbdata = data;
1074
1075         ret = master_rpc_async_request(handler, packet, 0, resize_cb, NULL);
1076         if (ret < 0) {
1077                 handler->size_changed_cb = NULL;
1078                 handler->size_cbdata = NULL;
1079         }
1080
1081         return ret;
1082 }
1083
1084 EAPI int livebox_click(struct livebox *handler, double x, double y)
1085 {
1086         struct packet *packet;
1087         double timestamp;
1088         int ret;
1089
1090         if (!handler) {
1091                 ErrPrint("Handler is NIL\n");
1092                 return LB_STATUS_ERROR_INVALID;
1093         }
1094
1095         if (handler->state != CREATE || !handler->id) {
1096                 ErrPrint("Handler is not valid\n");
1097                 return LB_STATUS_ERROR_INVALID;
1098         }
1099
1100         if (handler->lb.auto_launch) {
1101                 DbgPrint("Auto-launch enabled: %s\n", handler->lb.auto_launch);
1102                 if (aul_launch_app(handler->lb.auto_launch, NULL) < 0)
1103                         ErrPrint("Failed to launch app %s\n", handler->lb.auto_launch);
1104         }
1105
1106         timestamp = util_timestamp();
1107         packet = packet_create_noack("clicked", "sssddd", handler->pkgname, handler->id, "clicked", timestamp, x, y);
1108         if (!packet) {
1109                 ErrPrint("Failed to build param\n");
1110                 return LB_STATUS_ERROR_FAULT;
1111         }
1112
1113         ret = master_rpc_request_only(handler, packet);
1114         DbgPrint("Click request: %d\n", ret);
1115
1116         if (!handler->lb.mouse_event && (handler->lb.type == _LB_TYPE_BUFFER || handler->lb.type == _LB_TYPE_SCRIPT)) {
1117                 int ret; /* Shadow variable */
1118                 ret = send_mouse_event(handler, "lb_mouse_down", x * handler->lb.width, y * handler->lb.height);
1119                 if (ret < 0)
1120                         DbgPrint("Failed to send Down: %d\n", ret);
1121
1122                 ret = send_mouse_event(handler, "lb_mouse_move", x * handler->lb.width, y * handler->lb.height);
1123                 if (ret < 0)
1124                         DbgPrint("Failed to send Move: %d\n", ret);
1125
1126                 ret = send_mouse_event(handler, "lb_mouse_up", x * handler->lb.width, y * handler->lb.height);
1127                 if (ret < 0)
1128                         DbgPrint("Failed to send Up: %d\n", ret);
1129         }
1130
1131         return ret;
1132 }
1133
1134 EAPI int livebox_has_pd(struct livebox *handler)
1135 {
1136         if (!handler) {
1137                 ErrPrint("Handler is NIL\n");
1138                 return LB_STATUS_ERROR_INVALID;
1139         }
1140
1141         if (handler->state != CREATE || !handler->id) {
1142                 ErrPrint("Handler is not valid\n");
1143                 return LB_STATUS_ERROR_INVALID;
1144         }
1145
1146         return !!handler->pd.data.fb;
1147 }
1148
1149 EAPI int livebox_pd_is_created(struct livebox *handler)
1150 {
1151         if (!handler) {
1152                 ErrPrint("Handler is NIL\n");
1153                 return LB_STATUS_ERROR_INVALID;
1154         }
1155
1156         if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1157                 ErrPrint("Handler is not valid\n");
1158                 return LB_STATUS_ERROR_INVALID;
1159         }
1160
1161         return handler->is_pd_created;
1162 }
1163
1164 EAPI int livebox_create_pd(struct livebox *handler, ret_cb_t cb, void *data)
1165 {
1166         return livebox_create_pd_with_position(handler, -1.0, -1.0, cb, data);
1167 }
1168
1169 EAPI int livebox_create_pd_with_position(struct livebox *handler, double x, double y, ret_cb_t cb, void *data)
1170 {
1171         struct packet *packet;
1172         int ret;
1173
1174         if (!handler) {
1175                 ErrPrint("Handler is NIL\n");
1176                 return LB_STATUS_ERROR_INVALID;
1177         }
1178
1179         if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1180                 ErrPrint("Handler is not valid\n");
1181                 return LB_STATUS_ERROR_INVALID;
1182         }
1183
1184         if (handler->is_pd_created == 1) {
1185                 DbgPrint("PD already created\n");
1186                 return LB_STATUS_SUCCESS;
1187         }
1188
1189         if (handler->pd_created_cb) {
1190                 ErrPrint("Previous request is not completed yet\n");
1191                 return LB_STATUS_ERROR_BUSY;
1192         }
1193
1194         packet = packet_create("create_pd", "ssdd", handler->pkgname, handler->id, x, y);
1195         if (!packet) {
1196                 ErrPrint("Failed to build param\n");
1197                 return LB_STATUS_ERROR_FAULT;
1198         }
1199
1200         if (!cb)
1201                 cb = default_pd_created_cb;
1202
1203         handler->pd_created_cb = cb;
1204         handler->pd_created_cbdata = data;
1205
1206         ret = master_rpc_async_request(handler, packet, 0, pd_create_cb, NULL);
1207         if (ret < 0) {
1208                 handler->pd_created_cb = NULL;
1209                 handler->pd_created_cbdata = NULL;
1210         }
1211
1212         return ret;
1213 }
1214
1215 EAPI int livebox_move_pd(struct livebox *handler, double x, double y)
1216 {
1217         struct packet *packet;
1218
1219         if (!handler) {
1220                 ErrPrint("Handler is NIL\n");
1221                 return LB_STATUS_ERROR_INVALID;
1222         }
1223
1224         if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1225                 ErrPrint("Handler is not valid\n");
1226                 return LB_STATUS_ERROR_INVALID;
1227         }
1228
1229         if (!handler->is_pd_created) {
1230                 DbgPrint("PD is not created\n");
1231                 return LB_STATUS_ERROR_INVALID;
1232         }
1233
1234         packet = packet_create_noack("pd_move", "ssdd", handler->pkgname, handler->id, x, y);
1235         if (!packet) {
1236                 ErrPrint("Failed to build param\n");
1237                 return LB_STATUS_ERROR_FAULT;
1238         }
1239
1240         return master_rpc_request_only(handler, packet);
1241 }
1242
1243 EAPI int livebox_activate(const char *pkgname, ret_cb_t cb, void *data)
1244 {
1245         struct packet *packet;
1246         struct cb_info *cbinfo;
1247         int ret;
1248
1249         if (!pkgname)
1250                 return LB_STATUS_ERROR_INVALID;
1251
1252         packet = packet_create("activate_package", "s", pkgname);
1253         if (!packet) {
1254                 ErrPrint("Failed to build a param\n");
1255                 return LB_STATUS_ERROR_FAULT;
1256         }
1257
1258         cbinfo = create_cb_info(cb, data);
1259         if (!cbinfo) {
1260                 ErrPrint("Unable to create cbinfo\n");
1261                 packet_destroy(packet);
1262                 return LB_STATUS_ERROR_FAULT;
1263         }
1264
1265         ret = master_rpc_async_request(NULL, packet, 0, activated_cb, cbinfo);
1266         if (ret < 0)
1267                 destroy_cb_info(cbinfo);
1268
1269         return ret;
1270 }
1271
1272 EAPI int livebox_destroy_pd(struct livebox *handler, ret_cb_t cb, void *data)
1273 {
1274         struct packet *packet;
1275         struct cb_info *cbinfo;
1276         int ret;
1277
1278         if (!handler) {
1279                 ErrPrint("Handler is NIL\n");
1280                 return LB_STATUS_ERROR_INVALID;
1281         }
1282
1283         if (!handler->pd.data.fb || handler->state != CREATE || !handler->id) {
1284                 ErrPrint("Handler is not valid\n");
1285                 return LB_STATUS_ERROR_INVALID;
1286         }
1287
1288         if (!handler->is_pd_created) {
1289                 ErrPrint("PD is not created\n");
1290                 return LB_STATUS_ERROR_INVALID;
1291         }
1292
1293         packet = packet_create("destroy_pd", "ss", handler->pkgname, handler->id);
1294         if (!packet) {
1295                 ErrPrint("Failed to build a param\n");
1296                 return LB_STATUS_ERROR_FAULT;
1297         }
1298
1299         if (!cb)
1300                 cb = default_pd_destroyed_cb;
1301
1302         cbinfo = create_cb_info(cb, data);
1303         if (!cbinfo) {
1304                 packet_destroy(packet);
1305                 return LB_STATUS_ERROR_FAULT;
1306         }
1307
1308         ret = master_rpc_async_request(handler, packet, 0, pd_destroy_cb, cbinfo);
1309         if (ret < 0)
1310                 destroy_cb_info(cbinfo);
1311
1312         return ret;
1313 }
1314
1315 EAPI int livebox_access_event(struct livebox *handler, enum access_event_type type, double x, double y, ret_cb_t cb, void *data)
1316 {
1317         int w = 1;
1318         int h = 1;
1319         char cmd[32] = { '\0', };
1320         char *ptr = cmd;
1321         int ret;
1322
1323         if (!handler) {
1324                 ErrPrint("Handler is NIL\n");
1325                 return LB_STATUS_ERROR_INVALID;
1326         }
1327
1328         if (handler->state != CREATE || !handler->id) {
1329                 ErrPrint("Handler is not valid\n");
1330                 return LB_STATUS_ERROR_INVALID;
1331         }
1332
1333         if (handler->access_event_cb) {
1334                 ErrPrint("Previous access event is not yet done\n");
1335                 return LB_STATUS_ERROR_BUSY;
1336         }
1337
1338         if (type & ACCESS_EVENT_PD_MASK) {
1339                 if (!handler->is_pd_created) {
1340                         ErrPrint("PD is not created\n");
1341                         return LB_STATUS_ERROR_INVALID;
1342                 }
1343                 *ptr++ = 'p';
1344                 *ptr++ = 'd';
1345                 w = handler->pd.width;
1346                 h = handler->pd.height;
1347         } else if (type & ACCESS_EVENT_LB_MASK) {
1348                 *ptr++ = 'l';
1349                 *ptr++ = 'b';
1350                 w = handler->lb.width;
1351                 h = handler->lb.height;
1352         } else {
1353                 ErrPrint("Invalid event type\n");
1354                 return LB_STATUS_ERROR_INVALID;
1355         }
1356
1357         switch (type & ~ACCESS_EVENT_PD_MASK) {
1358         case ACCESS_EVENT_HIGHLIGHT:
1359                 strcpy(ptr, "_access_hl");
1360                 break;
1361         case ACCESS_EVENT_HIGHLIGHT_NEXT:
1362                 strcpy(ptr, "_access_hl_next");
1363                 break;
1364         case ACCESS_EVENT_HIGHLIGHT_PREV:
1365                 strcpy(ptr, "_access_hl_prev");
1366                 break;
1367         case ACCESS_EVENT_ACTIVATE:
1368                 strcpy(ptr, "_access_activate");
1369                 break;
1370         case ACCESS_EVENT_ACTION_DOWN:
1371                 strcpy(ptr, "_access_action_down");
1372                 break;
1373         case ACCESS_EVENT_ACTION_UP:
1374                 strcpy(ptr, "_access_action_up");
1375                 break;
1376         case ACCESS_EVENT_UNHIGHLIGHT:
1377                 strcpy(ptr, "_access_unhighlight");
1378                 break;
1379         case ACCESS_EVENT_SCROLL_DOWN:
1380                 strcpy(ptr, "_access_scroll_down");
1381                 break;
1382         case ACCESS_EVENT_SCROLL_MOVE:
1383                 strcpy(ptr, "_access_scroll_move");
1384                 break;
1385         case ACCESS_EVENT_SCROLL_UP:
1386                 strcpy(ptr, "_access_scroll_up");
1387                 break;
1388         default:
1389                 return LB_STATUS_ERROR_INVALID;
1390         }
1391
1392         if (!cb)
1393                 cb = default_access_event_cb;
1394
1395         handler->access_event_cb = cb;
1396         handler->access_event_cbdata = data;
1397
1398         ret = send_access_event(handler, cmd, x * w, y * h);
1399         if (ret < 0) {
1400                 handler->access_event_cb = NULL;
1401                 handler->access_event_cbdata = NULL;
1402         }
1403
1404         return ret;
1405 }
1406
1407 EAPI int livebox_content_event(struct livebox *handler, enum content_event_type type, double x, double y)
1408 {
1409         int w = 1;
1410         int h = 1;
1411         char cmd[32] = { '\0', };
1412         char *ptr = cmd;
1413
1414         if (!handler) {
1415                 ErrPrint("Handler is NIL\n");
1416                 return LB_STATUS_ERROR_INVALID;
1417         }
1418
1419         if (handler->state != CREATE || !handler->id) {
1420                 ErrPrint("Handler is not valid\n");
1421                 return LB_STATUS_ERROR_INVALID;
1422         }
1423
1424         if (type & CONTENT_EVENT_PD_MASK) {
1425                 int flag = 1;
1426
1427                 if (!handler->is_pd_created) {
1428                         ErrPrint("PD is not created\n");
1429                         return LB_STATUS_ERROR_INVALID;
1430                 }
1431
1432                 if (type & CONTENT_EVENT_MOUSE_MASK) {
1433                         if (!handler->pd.data.fb) {
1434                                 ErrPrint("Handler is not valid\n");
1435                                 return LB_STATUS_ERROR_INVALID;
1436                         }
1437
1438                         if (type & CONTENT_EVENT_MOUSE_MOVE) {
1439                                 if (fabs(x - handler->pd.x) < MINIMUM_EVENT && fabs(y - handler->pd.y) < MINIMUM_EVENT)
1440                                         return LB_STATUS_ERROR_BUSY;
1441                         } else if (type & CONTENT_EVENT_MOUSE_SET) {
1442                                 flag = 0;
1443                         }
1444                 }
1445
1446                 if (flag) {
1447                         w = handler->pd.width;
1448                         h = handler->pd.height;
1449                         handler->pd.x = x;
1450                         handler->pd.y = y;
1451                 }
1452                 *ptr++ = 'p';
1453                 *ptr++ = 'd';
1454         } else if (type & CONTENT_EVENT_LB_MASK) {
1455                 int flag = 1;
1456
1457                 if (type & CONTENT_EVENT_MOUSE_MASK) {
1458                         if (!handler->lb.mouse_event) {
1459                                 ErrPrint("Box is not support the mouse event\n");
1460                                 return LB_STATUS_ERROR_INVALID;
1461                         }
1462
1463                         if (!handler->lb.data.fb) {
1464                                 ErrPrint("Handler is not valid\n");
1465                                 return LB_STATUS_ERROR_INVALID;
1466                         }
1467
1468                         if (type & CONTENT_EVENT_MOUSE_MOVE) {
1469                                 if (fabs(x - handler->lb.x) < MINIMUM_EVENT && fabs(y - handler->lb.y) < MINIMUM_EVENT)
1470                                         return LB_STATUS_ERROR_BUSY;
1471                         } else if (type & CONTENT_EVENT_MOUSE_SET) {
1472                                 flag = 0;
1473                         }
1474                 }
1475
1476                 if (flag) {
1477                         w = handler->lb.width;
1478                         h = handler->lb.height;
1479                         handler->lb.x = x;
1480                         handler->lb.y = y;
1481                 }
1482                 *ptr++ = 'l';
1483                 *ptr++ = 'b';
1484         } else {
1485                 ErrPrint("Invalid event type\n");
1486                 return LB_STATUS_ERROR_INVALID;
1487         }
1488
1489         /*!
1490          * Must be short than 29 bytes.
1491          */
1492         switch ((type & ~(CONTENT_EVENT_PD_MASK | CONTENT_EVENT_LB_MASK))) {
1493         case CONTENT_EVENT_MOUSE_ENTER | CONTENT_EVENT_MOUSE_MASK:
1494                 strcpy(ptr, "_mouse_enter");
1495                 break;
1496         case CONTENT_EVENT_MOUSE_LEAVE | CONTENT_EVENT_MOUSE_MASK:
1497                 strcpy(ptr, "_mouse_leave");
1498                 break;
1499         case CONTENT_EVENT_MOUSE_UP | CONTENT_EVENT_MOUSE_MASK:
1500                 strcpy(ptr, "_mouse_up");
1501                 break;
1502         case CONTENT_EVENT_MOUSE_DOWN | CONTENT_EVENT_MOUSE_MASK:
1503                 strcpy(ptr, "_mouse_down");
1504                 break;
1505         case CONTENT_EVENT_MOUSE_MOVE | CONTENT_EVENT_MOUSE_MASK:
1506                 strcpy(ptr, "_mouse_move");
1507                 break;
1508         case CONTENT_EVENT_MOUSE_SET | CONTENT_EVENT_MOUSE_MASK:
1509                 strcpy(ptr, "_mouse_set");
1510                 break;
1511         case CONTENT_EVENT_MOUSE_UNSET | CONTENT_EVENT_MOUSE_MASK:
1512                 strcpy(ptr, "_mouse_unset");
1513                 break;
1514         case CONTENT_EVENT_KEY_DOWN | CONTENT_EVENT_KEY_MASK:
1515                 strcpy(ptr, "_key_down");
1516                 break;
1517         case CONTENT_EVENT_KEY_UP | CONTENT_EVENT_KEY_MASK:
1518                 strcpy(ptr, "_key_up");
1519                 break;
1520         default:
1521                 ErrPrint("Invalid event type\n");
1522                 return LB_STATUS_ERROR_INVALID;
1523         }
1524
1525         return send_mouse_event(handler, cmd, x * w, y * h);
1526 }
1527
1528 EAPI const char *livebox_filename(struct livebox *handler)
1529 {
1530         if (!handler) {
1531                 ErrPrint("Handler is NIL\n");
1532                 return NULL;
1533         }
1534
1535         if (handler->state != CREATE || !handler->id) {
1536                 ErrPrint("Handler is not valid\n");
1537                 return NULL;
1538         }
1539
1540         if (handler->filename)
1541                 return handler->filename;
1542
1543         /* Oooops */
1544         return util_uri_to_path(handler->id);
1545 }
1546
1547 EAPI int livebox_get_pdsize(struct livebox *handler, int *w, int *h)
1548 {
1549         int _w;
1550         int _h;
1551
1552         if (!handler) {
1553                 ErrPrint("Handler is NIL\n");
1554                 return LB_STATUS_ERROR_INVALID;
1555         }
1556
1557         if (handler->state != CREATE || !handler->id) {
1558                 ErrPrint("Handler is not valid\n");
1559                 return LB_STATUS_ERROR_INVALID;
1560         }
1561
1562         if (!w)
1563                 w = &_w;
1564         if (!h)
1565                 h = &_h;
1566
1567         if (!handler->is_pd_created) {
1568                 DbgPrint("Buffer is not created yet [%dx%d]\n", *w, *h);
1569                 *w = handler->pd.default_width;
1570                 *h = handler->pd.default_height;
1571         } else {
1572                 *w = handler->pd.width;
1573                 *h = handler->pd.height;
1574         }
1575
1576         return LB_STATUS_SUCCESS;
1577 }
1578
1579 EAPI int livebox_size(struct livebox *handler)
1580 {
1581         int w;
1582         int h;
1583
1584         if (!handler) {
1585                 ErrPrint("Handler is NIL\n");
1586                 return LB_STATUS_ERROR_INVALID;
1587         }
1588
1589         if (handler->state != CREATE || !handler->id) {
1590                 ErrPrint("Handler is not valid\n");
1591                 return LB_STATUS_ERROR_INVALID;
1592         }
1593
1594         w = handler->lb.width;
1595         h = handler->lb.height;
1596
1597         switch (handler->lb.type) {
1598         case _LB_TYPE_BUFFER:
1599         case _LB_TYPE_SCRIPT:
1600                 if (!fb_is_created(handler->lb.data.fb)) {
1601                         DbgPrint("Buffer is not created yet - reset size\n");
1602                         w = 0;
1603                         h = 0;
1604                 }
1605                 break;
1606         default:
1607                 break;
1608         }
1609
1610         return livebox_service_size_type(w, h);
1611 }
1612
1613 EAPI int livebox_set_group(struct livebox *handler, const char *cluster, const char *category, ret_cb_t cb, void *data)
1614 {
1615         struct packet *packet;
1616         int ret;
1617
1618         if (!handler) {
1619                 ErrPrint("Handler is NIL\n");
1620                 return LB_STATUS_ERROR_INVALID;
1621         }
1622
1623         if (!cluster || !category || handler->state != CREATE || !handler->id) {
1624                 ErrPrint("Invalid argument\n");
1625                 return LB_STATUS_ERROR_INVALID;
1626         }
1627
1628         if (handler->group_changed_cb) {
1629                 ErrPrint("Previous group changing request is not finished yet\n");
1630                 return LB_STATUS_ERROR_BUSY;
1631         }
1632
1633         if (!handler->is_user) {
1634                 ErrPrint("CA Livebox is not able to change the group\n");
1635                 return LB_STATUS_ERROR_PERMISSION;
1636         }
1637
1638         if (!strcmp(handler->cluster, cluster) && !strcmp(handler->category, category)) {
1639                 DbgPrint("No changes\n");
1640                 return LB_STATUS_ERROR_ALREADY;
1641         }
1642
1643         packet = packet_create("change_group", "ssss", handler->pkgname, handler->id, cluster, category);
1644         if (!packet) {
1645                 ErrPrint("Failed to build a param\n");
1646                 return LB_STATUS_ERROR_FAULT;
1647         }
1648
1649         if (!cb)
1650                 cb = default_group_changed_cb;
1651
1652         handler->group_changed_cb = cb;
1653         handler->group_cbdata = data; 
1654
1655         ret = master_rpc_async_request(handler, packet, 0, set_group_ret_cb, NULL);
1656         if (ret < 0) {
1657                 handler->group_changed_cb = NULL;
1658                 handler->group_cbdata = NULL;
1659         }
1660
1661         return ret;
1662 }
1663
1664 EAPI int livebox_get_group(struct livebox *handler, char ** const cluster, char ** const category)
1665 {
1666         if (!handler) {
1667                 ErrPrint("Handler is NIL\n");
1668                 return LB_STATUS_ERROR_INVALID;
1669         }
1670
1671         if (!cluster || !category || handler->state != CREATE || !handler->id) {
1672                 ErrPrint("Invalid argument\n");
1673                 return LB_STATUS_ERROR_INVALID;
1674         }
1675
1676         *cluster = handler->cluster;
1677         *category = handler->category;
1678         return LB_STATUS_SUCCESS;
1679 }
1680
1681 EAPI int livebox_get_supported_sizes(struct livebox *handler, int *cnt, int *size_list)
1682 {
1683         register int i;
1684         register int j;
1685
1686         if (!handler || !size_list) {
1687                 ErrPrint("Invalid argument, handler(%p), size_list(%p)\n", handler, size_list);
1688                 return LB_STATUS_ERROR_INVALID;
1689         }
1690
1691         if (!cnt || handler->state != CREATE || !handler->id) {
1692                 ErrPrint("Handler is not valid\n");
1693                 return LB_STATUS_ERROR_INVALID;
1694         }
1695
1696         for (j = i = 0; i < NR_OF_SIZE_LIST; i++) {
1697                 if (handler->lb.size_list & (0x01 << i)) {
1698                         if (j == *cnt)
1699                                 break;
1700
1701                         size_list[j++] = (0x01 << i);
1702                 }
1703         }
1704
1705         *cnt = j;
1706         return LB_STATUS_SUCCESS;
1707 }
1708
1709 EAPI const char *livebox_pkgname(struct livebox *handler)
1710 {
1711         if (!handler) {
1712                 ErrPrint("Handler is NIL\n");
1713                 return NULL;
1714         }
1715
1716         if (handler->state != CREATE) {
1717                 ErrPrint("Handler is not valid\n");
1718                 return NULL;
1719         }
1720
1721         return handler->pkgname;
1722 }
1723
1724 EAPI double livebox_priority(struct livebox *handler)
1725 {
1726         if (!handler) {
1727                 ErrPrint("Handler is NIL\n");
1728                 return 0.0f;
1729         }
1730
1731         if (handler->state != CREATE || !handler->id) {
1732                 ErrPrint("Handler is not valid (%p)\n", handler);
1733                 return -1.0f;
1734         }
1735
1736         return handler->lb.priority;
1737 }
1738
1739 EAPI int livebox_delete_cluster(const char *cluster, ret_cb_t cb, void *data)
1740 {
1741         struct packet *packet;
1742         struct cb_info *cbinfo;
1743         int ret;
1744
1745         packet = packet_create("delete_cluster", "s", cluster);
1746         if (!packet) {
1747                 ErrPrint("Failed to build a param\n");
1748                 return LB_STATUS_ERROR_FAULT;
1749         }
1750
1751         cbinfo = create_cb_info(cb, data);
1752         if (!cbinfo) {
1753                 packet_destroy(packet);
1754                 return LB_STATUS_ERROR_FAULT;
1755         }
1756
1757         ret = master_rpc_async_request(NULL, packet, 0, delete_cluster_cb, cbinfo);
1758         if (ret < 0)
1759                 destroy_cb_info(cbinfo);
1760
1761         return ret;
1762 }
1763
1764 EAPI int livebox_delete_category(const char *cluster, const char *category, ret_cb_t cb, void *data)
1765 {
1766         struct packet *packet;
1767         struct cb_info *cbinfo;
1768         int ret;
1769
1770         packet = packet_create("delete_category", "ss", cluster, category);
1771         if (!packet) {
1772                 ErrPrint("Failed to build a param\n");
1773                 return LB_STATUS_ERROR_FAULT;
1774         }
1775
1776         cbinfo = create_cb_info(cb, data);
1777         if (!cbinfo) {
1778                 packet_destroy(packet);
1779                 return LB_STATUS_ERROR_FAULT;
1780         }
1781
1782         ret = master_rpc_async_request(NULL, packet, 0, delete_category_cb, cbinfo);
1783         if (ret < 0)
1784                 destroy_cb_info(cbinfo);
1785
1786         return ret;
1787 }
1788
1789 EAPI enum livebox_lb_type livebox_lb_type(struct livebox *handler)
1790 {
1791         if (!handler) {
1792                 ErrPrint("Handler is NIL\n");
1793                 return LB_TYPE_INVALID;
1794         }
1795
1796         if (handler->state != CREATE || !handler->id) {
1797                 ErrPrint("Handler is not valid\n");
1798                 return LB_TYPE_INVALID;
1799         }
1800
1801         switch (handler->lb.type) {
1802         case _LB_TYPE_FILE:
1803                 return LB_TYPE_IMAGE;
1804         case _LB_TYPE_BUFFER:
1805         case _LB_TYPE_SCRIPT:
1806                 {
1807                         const char *id;
1808                         id = fb_id(handler->lb.data.fb);
1809                         if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1810                                 return LB_TYPE_PIXMAP;
1811                 }
1812                 return LB_TYPE_BUFFER;
1813         case _LB_TYPE_TEXT:
1814                 return LB_TYPE_TEXT;
1815         default:
1816                 break;
1817         }
1818
1819         return LB_TYPE_INVALID;
1820 }
1821
1822 EAPI enum livebox_pd_type livebox_pd_type(struct livebox *handler)
1823 {
1824         if (!handler) {
1825                 ErrPrint("Handler is NIL\n");
1826                 return PD_TYPE_INVALID;
1827         }
1828
1829         if (handler->state != CREATE || !handler->id) {
1830                 ErrPrint("Handler is not valid\n");
1831                 return PD_TYPE_INVALID;
1832         }
1833
1834         switch (handler->pd.type) {
1835         case _PD_TYPE_TEXT:
1836                 return PD_TYPE_TEXT;
1837         case _PD_TYPE_BUFFER:
1838         case _PD_TYPE_SCRIPT:
1839                 {
1840                         const char *id;
1841                         id = fb_id(handler->pd.data.fb);
1842                         if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1843                                 return PD_TYPE_PIXMAP;
1844                 }
1845                 return PD_TYPE_BUFFER;
1846         default:
1847                 break;
1848         }
1849
1850         return PD_TYPE_INVALID;
1851 }
1852
1853 EAPI int livebox_set_pd_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
1854 {
1855         if (!handler) {
1856                 ErrPrint("Handler is NIL\n");
1857                 return LB_STATUS_ERROR_INVALID;
1858         }
1859
1860         if (handler->state != CREATE) {
1861                 ErrPrint("Handler is not valid\n");
1862                 return LB_STATUS_ERROR_INVALID;
1863         }
1864
1865         memcpy(&handler->pd.data.ops, ops, sizeof(*ops));
1866         return LB_STATUS_SUCCESS;
1867 }
1868
1869 EAPI int livebox_set_text_handler(struct livebox *handler, struct livebox_script_operators *ops)
1870 {
1871         if (!handler) {
1872                 ErrPrint("Handler is NIL\n");
1873                 return LB_STATUS_ERROR_INVALID;
1874         }
1875
1876         if (handler->state != CREATE) {
1877                 ErrPrint("Handler is not valid\n");
1878                 return LB_STATUS_ERROR_INVALID;
1879         }
1880
1881         memcpy(&handler->lb.data.ops, ops, sizeof(*ops));
1882         return LB_STATUS_SUCCESS;
1883 }
1884
1885 EAPI int livebox_acquire_lb_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
1886 {
1887         struct packet *packet;
1888         struct cb_info *cbinfo;
1889         const char *id;
1890         int ret;
1891
1892         if (!handler) {
1893                 ErrPrint("Handler is NIL\n");
1894                 return LB_STATUS_ERROR_INVALID;
1895         }
1896
1897         if (handler->state != CREATE || !handler->id) {
1898                 ErrPrint("Invalid handle\n");
1899                 return LB_STATUS_ERROR_INVALID;
1900         }
1901
1902         if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1903                 ErrPrint("Handler is not valid type\n");
1904                 return LB_STATUS_ERROR_INVALID;
1905         }
1906
1907         id = fb_id(handler->lb.data.fb);
1908         if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1909                 return LB_STATUS_ERROR_INVALID;
1910
1911         packet = packet_create("lb_acquire_pixmap", "ss", handler->pkgname, handler->id);
1912         if (!packet) {
1913                 ErrPrint("Failed to build a param\n");
1914                 return LB_STATUS_ERROR_FAULT;
1915         }
1916
1917         cbinfo = create_cb_info(cb, data);
1918         if (!cbinfo) {
1919                 packet_destroy(packet);
1920                 return LB_STATUS_ERROR_FAULT;
1921         }
1922
1923         ret = master_rpc_async_request(handler, packet, 0, pixmap_acquired_cb, cbinfo);
1924         if (ret < 0)
1925                 destroy_cb_info(cbinfo);
1926
1927         return ret;
1928 }
1929
1930 EAPI int livebox_release_lb_pixmap(struct livebox *handler, int pixmap)
1931 {
1932         struct packet *packet;
1933
1934         if (!handler) {
1935                 ErrPrint("Handler is NIL\n");
1936                 return LB_STATUS_ERROR_INVALID;
1937         }
1938
1939         if (handler->state != CREATE || !handler->id) {
1940                 ErrPrint("Invalid handle\n");
1941                 return LB_STATUS_ERROR_INVALID;
1942         }
1943
1944         if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
1945                 ErrPrint("Handler is not valid type\n");
1946                 return LB_STATUS_ERROR_INVALID;
1947         }
1948
1949         packet = packet_create_noack("lb_release_pixmap", "ssi", handler->pkgname, handler->id, pixmap);
1950         if (!packet) {
1951                 ErrPrint("Failed to build a param\n");
1952                 return LB_STATUS_ERROR_INVALID;
1953         }
1954
1955         return master_rpc_request_only(handler, packet);
1956 }
1957
1958 EAPI int livebox_acquire_pd_pixmap(struct livebox *handler, ret_cb_t cb, void *data)
1959 {
1960         struct packet *packet;
1961         struct cb_info *cbinfo;
1962         const char *id;
1963         int ret;
1964
1965         if (!handler) {
1966                 ErrPrint("Handler is NIL\n");
1967                 return LB_STATUS_ERROR_INVALID;
1968         }
1969
1970         if (handler->state != CREATE || !handler->id) {
1971                 ErrPrint("Invalid handle\n");
1972                 return LB_STATUS_ERROR_INVALID;
1973         }
1974
1975         if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
1976                 ErrPrint("Handler is not valid type\n");
1977                 return LB_STATUS_ERROR_INVALID;
1978         }
1979
1980         id = fb_id(handler->pd.data.fb);
1981         if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)))
1982                 return LB_STATUS_ERROR_INVALID;
1983
1984         packet = packet_create("pd_acquire_pixmap", "ss", handler->pkgname, handler->id);
1985         if (!packet) {
1986                 ErrPrint("Failed to build a param\n");
1987                 return LB_STATUS_ERROR_FAULT;
1988         }
1989
1990         cbinfo = create_cb_info(cb, data);
1991         if (!cbinfo) {
1992                 packet_destroy(packet);
1993                 return LB_STATUS_ERROR_FAULT;
1994         }
1995
1996         ret = master_rpc_async_request(handler, packet, 0, pixmap_acquired_cb, cbinfo);
1997         if (ret < 0)
1998                 destroy_cb_info(cbinfo);
1999
2000         return ret;
2001 }
2002
2003 EAPI int livebox_pd_pixmap(const struct livebox *handler)
2004 {
2005         const char *id;
2006         int pixmap = 0;
2007
2008         if (!handler) {
2009                 ErrPrint("Handler is NIL\n");
2010                 return 0;
2011         }
2012
2013         if (handler->state != CREATE || !handler->id) {
2014                 ErrPrint("Invalid handler\n");
2015                 return 0;
2016         }
2017
2018         if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
2019                 ErrPrint("Invalid handler\n");
2020                 return 0;
2021         }
2022
2023         id = fb_id(handler->pd.data.fb);
2024         if (id && sscanf(id, SCHEMA_PIXMAP "%d", &pixmap) != 1) {
2025                 ErrPrint("PIXMAP Id is not valid\n");
2026                 return 0;
2027         }
2028
2029         return pixmap;
2030 }
2031
2032 EAPI int livebox_lb_pixmap(const struct livebox *handler)
2033 {
2034         const char *id;
2035         int pixmap = 0;
2036
2037         if (!handler) {
2038                 ErrPrint("Handler is NIL\n");
2039                 return 0;
2040         }
2041
2042         if (handler->state != CREATE || !handler->id) {
2043                 ErrPrint("Invalid handler\n");
2044                 return 0;
2045         }
2046
2047         if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
2048                 ErrPrint("Invalid handler\n");
2049                 return 0;
2050         }
2051
2052         id = fb_id(handler->lb.data.fb);
2053         if (id && sscanf(id, SCHEMA_PIXMAP "%d", &pixmap) != 1) {
2054                 ErrPrint("PIXMAP Id is not valid\n");
2055                 return 0;
2056         }
2057
2058         return pixmap;
2059 }
2060
2061 EAPI int livebox_release_pd_pixmap(struct livebox *handler, int pixmap)
2062 {
2063         struct packet *packet;
2064
2065         if (!handler) {
2066                 ErrPrint("Handler is NIL\n");
2067                 return LB_STATUS_ERROR_INVALID;
2068         }
2069
2070         if (handler->state != CREATE || !handler->id) {
2071                 ErrPrint("Invalid handle\n");
2072                 return LB_STATUS_ERROR_INVALID;
2073         }
2074
2075         if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
2076                 ErrPrint("Handler is not valid type\n");
2077                 return LB_STATUS_ERROR_INVALID;
2078         }
2079
2080         packet = packet_create_noack("pd_release_pixmap", "ssi", handler->pkgname, handler->id, pixmap);
2081         if (!packet) {
2082                 ErrPrint("Failed to build a param\n");
2083                 return LB_STATUS_ERROR_FAULT;
2084         }
2085
2086         return master_rpc_request_only(handler, packet);
2087 }
2088
2089 EAPI void *livebox_acquire_fb(struct livebox *handler)
2090 {
2091         if (!handler) {
2092                 ErrPrint("Handler is NIL\n");
2093                 return NULL;
2094         }
2095
2096         if (handler->state != CREATE || !handler->id) {
2097                 ErrPrint("Invalid handle\n");
2098                 return NULL;
2099         }
2100
2101         if (handler->lb.type != _LB_TYPE_SCRIPT && handler->lb.type != _LB_TYPE_BUFFER) {
2102                 ErrPrint("Handler is not valid type\n");
2103                 return NULL;
2104         }
2105
2106         return fb_acquire_buffer(handler->lb.data.fb);
2107 }
2108
2109 EAPI int livebox_release_fb(void *buffer)
2110 {
2111         return fb_release_buffer(buffer);
2112 }
2113
2114 EAPI int livebox_fb_refcnt(void *buffer)
2115 {
2116         return fb_refcnt(buffer);
2117 }
2118
2119 EAPI void *livebox_acquire_pdfb(struct livebox *handler)
2120 {
2121         if (!handler) {
2122                 ErrPrint("Handler is NIL\n");
2123                 return NULL;
2124         }
2125
2126         if (handler->state != CREATE || !handler->id) {
2127                 ErrPrint("Invalid handler\n");
2128                 return NULL;
2129         }
2130
2131         if (handler->pd.type != _PD_TYPE_SCRIPT && handler->pd.type != _PD_TYPE_BUFFER) {
2132                 ErrPrint("Handler is not valid type\n");
2133                 return NULL;
2134         }
2135
2136         return fb_acquire_buffer(handler->pd.data.fb);
2137 }
2138
2139 EAPI int livebox_release_pdfb(void *buffer)
2140 {
2141         return fb_release_buffer(buffer);
2142 }
2143
2144 EAPI int livebox_pdfb_refcnt(void *buffer)
2145 {
2146         return fb_refcnt(buffer);
2147 }
2148
2149 EAPI int livebox_pdfb_bufsz(struct livebox *handler)
2150 {
2151         if (!handler) {
2152                 ErrPrint("Handler is NIL\n");
2153                 return LB_STATUS_ERROR_INVALID;
2154         }
2155
2156         if (handler->state != CREATE || !handler->id) {
2157                 ErrPrint("Handler is not valid\n");
2158                 return LB_STATUS_ERROR_INVALID;
2159         }
2160
2161         return fb_size(handler->pd.data.fb);
2162 }
2163
2164 EAPI int livebox_lbfb_bufsz(struct livebox *handler)
2165 {
2166         if (!handler) {
2167                 ErrPrint("Handler is NIL\n");
2168                 return LB_STATUS_ERROR_INVALID;
2169         }
2170
2171         if (handler->state != CREATE || !handler->id) {
2172                 ErrPrint("Handler is not valid\n");
2173                 return LB_STATUS_ERROR_INVALID;
2174         }
2175
2176         return fb_size(handler->lb.data.fb);
2177 }
2178
2179 EAPI int livebox_is_user(struct livebox *handler)
2180 {
2181         if (!handler) {
2182                 ErrPrint("Handler is NIL\n");
2183                 return LB_STATUS_ERROR_INVALID;
2184         }
2185
2186         if (handler->state != CREATE) {
2187                 ErrPrint("Handler is invalid\n");
2188                 return LB_STATUS_ERROR_INVALID;
2189         }
2190
2191         return handler->is_user;
2192 }
2193
2194 EAPI int livebox_set_pinup(struct livebox *handler, int flag, ret_cb_t cb, void *data)
2195 {
2196         struct packet *packet;
2197         int ret;
2198
2199         if (!handler) {
2200                 ErrPrint("Handler is NIL\n");
2201                 return LB_STATUS_ERROR_INVALID;
2202         }
2203
2204         if (handler->state != CREATE || !handler->id) {
2205                 ErrPrint("Handler is not valid\n");
2206                 return LB_STATUS_ERROR_INVALID;
2207         }
2208
2209         if (handler->pinup_cb) {
2210                 ErrPrint("Previous pinup request is not finished\n");
2211                 return LB_STATUS_ERROR_BUSY;
2212         }
2213
2214         if (handler->is_pinned_up == flag) {
2215                 DbgPrint("No changes\n");
2216                 return LB_STATUS_ERROR_ALREADY;
2217         }
2218
2219         packet = packet_create("pinup_changed", "ssi", handler->pkgname, handler->id, flag);
2220         if (!packet) {
2221                 ErrPrint("Failed to build a param\n");
2222                 return LB_STATUS_ERROR_FAULT;
2223         }
2224
2225         if (!cb)
2226                 cb = default_pinup_cb;
2227
2228         handler->pinup_cb = cb;
2229         handler->pinup_cbdata = data;
2230
2231         ret = master_rpc_async_request(handler, packet, 0, pinup_done_cb, NULL);
2232         if (ret < 0) {
2233                 handler->pinup_cb = NULL;
2234                 handler->pinup_cbdata = NULL;
2235         }
2236
2237         return ret;
2238 }
2239
2240 EAPI int livebox_is_pinned_up(struct livebox *handler)
2241 {
2242         if (!handler) {
2243                 ErrPrint("Handler is NIL\n");
2244                 return LB_STATUS_ERROR_INVALID;
2245         }
2246
2247         if (handler->state != CREATE || !handler->id)
2248                 return LB_STATUS_ERROR_INVALID;
2249
2250         return handler->is_pinned_up;
2251 }
2252
2253 EAPI int livebox_has_pinup(struct livebox *handler)
2254 {
2255         if (!handler) {
2256                 ErrPrint("Handler is NIL\n");
2257                 return LB_STATUS_ERROR_INVALID;
2258         }
2259
2260         if (handler->state != CREATE || !handler->id)
2261                 return LB_STATUS_ERROR_INVALID;
2262
2263         return handler->lb.pinup_supported;
2264 }
2265
2266 EAPI int livebox_set_data(struct livebox *handler, void *data)
2267 {
2268         if (!handler) {
2269                 ErrPrint("Handler is NIL\n");
2270                 return LB_STATUS_ERROR_INVALID;
2271         }
2272
2273         if (handler->state != CREATE)
2274                 return LB_STATUS_ERROR_INVALID;
2275
2276         handler->data = data;
2277         return LB_STATUS_SUCCESS;
2278 }
2279
2280 EAPI void *livebox_get_data(struct livebox *handler)
2281 {
2282         if (!handler) {
2283                 ErrPrint("Handler is NIL\n");
2284                 return NULL;
2285         }
2286
2287         if (handler->state != CREATE)
2288                 return NULL;
2289
2290         return handler->data;
2291 }
2292
2293 EAPI int livebox_is_exists(const char *pkgname)
2294 {
2295         char *lb;
2296
2297         lb = lb_pkgname(pkgname);
2298         if (lb) {
2299                 free(lb);
2300                 return 1;
2301         }
2302
2303         return 0;
2304 }
2305
2306 EAPI const char *livebox_content(struct livebox *handler)
2307 {
2308         if (!handler) {
2309                 ErrPrint("Handler is NIL\n");
2310                 return NULL;
2311         }
2312
2313         if (handler->state != CREATE)
2314                 return NULL;
2315
2316         return handler->content;
2317 }
2318
2319 EAPI const char *livebox_category_title(struct livebox *handler)
2320 {
2321         if (!handler) {
2322                 ErrPrint("Handler is NIL\n");
2323                 return NULL;
2324         }
2325
2326         if (handler->state != CREATE)
2327                 return NULL;
2328
2329         return handler->title;
2330 }
2331
2332 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)
2333 {
2334         struct packet *packet;
2335         struct cb_info *cbinfo;
2336         int ret;
2337
2338         if (!handler) {
2339                 ErrPrint("Handler is NIL\n");
2340                 return LB_STATUS_ERROR_INVALID;
2341         }
2342
2343         if ((handler->lb.type != _LB_TYPE_TEXT && handler->pd.type != _PD_TYPE_TEXT) || handler->state != CREATE || !handler->id) {
2344                 ErrPrint("Handler is not valid\n");
2345                 return LB_STATUS_ERROR_INVALID;
2346         }
2347
2348         if (!emission)
2349                 emission = "";
2350
2351         if (!source)
2352                 source = "";
2353
2354         packet = packet_create("text_signal", "ssssdddd",
2355                                 handler->pkgname, handler->id, emission, source, sx, sy, ex, ey);
2356         if (!packet) {
2357                 ErrPrint("Failed to build a param\n");
2358                 return LB_STATUS_ERROR_FAULT;
2359         }
2360
2361         cbinfo = create_cb_info(cb, data);
2362         if (!cbinfo) {
2363                 packet_destroy(packet);
2364                 return LB_STATUS_ERROR_FAULT;
2365         }
2366
2367         ret = master_rpc_async_request(handler, packet, 0, text_signal_cb, cbinfo);
2368         if (ret < 0)
2369                 destroy_cb_info(cbinfo);
2370
2371         return LB_STATUS_ERROR_FAULT;
2372 }
2373
2374 EAPI int livebox_subscribe_group(const char *cluster, const char *category)
2375 {
2376         struct packet *packet;
2377
2378         /*!
2379          * \TODO
2380          * Validate the group info using DB
2381          * If the group info is not valid, do not send this request
2382          */
2383
2384         packet = packet_create_noack("subscribe", "ss", cluster ? cluster : "", category ? category : "");
2385         if (!packet) {
2386                 ErrPrint("Failed to create a packet\n");
2387                 return LB_STATUS_ERROR_FAULT;
2388         }
2389
2390         return master_rpc_request_only(NULL, packet);
2391 }
2392
2393 EAPI int livebox_unsubscribe_group(const char *cluster, const char *category)
2394 {
2395         struct packet *packet;
2396
2397         /*!
2398          * \TODO
2399          * Validate the group info using DB
2400          * If the group info is not valid, do not send this request
2401          * AND Check the subscribed or not too
2402          */
2403
2404         packet = packet_create_noack("unsubscribe", "ss", cluster ? cluster : "", category ? category : "");
2405         if (!packet) {
2406                 ErrPrint("Failed to create a packet\n");
2407                 return LB_STATUS_ERROR_FAULT;
2408         }
2409
2410         return master_rpc_request_only(NULL, packet);
2411 }
2412
2413 EAPI int livebox_refresh(struct livebox *handler)
2414 {
2415         struct packet *packet;
2416
2417         if (!handler) {
2418                 ErrPrint("Hnalder is NIL\n");
2419                 return LB_STATUS_ERROR_INVALID;
2420         }
2421
2422         if (handler->state != CREATE || !handler->id)
2423                 return LB_STATUS_ERROR_INVALID;
2424
2425         packet = packet_create_noack("update", "ss", handler->pkgname, handler->id);
2426         if (!packet) {
2427                 ErrPrint("Failed to create a packet\n");
2428                 return LB_STATUS_ERROR_FAULT;
2429         }
2430
2431         return master_rpc_request_only(handler, packet);
2432 }
2433
2434 EAPI int livebox_refresh_group(const char *cluster, const char *category)
2435 {
2436         struct packet *packet;
2437
2438         if (!cluster || !category) {
2439                 ErrPrint("Invalid argument\n");
2440                 return LB_STATUS_ERROR_INVALID;
2441         }
2442
2443         packet = packet_create_noack("refresh_group", "ss", cluster, category);
2444         if (!packet) {
2445                 ErrPrint("Failed to create a packet\n");
2446                 return LB_STATUS_ERROR_FAULT;
2447         }
2448
2449         return master_rpc_request_only(NULL, packet);
2450 }
2451
2452 EAPI int livebox_set_visibility(struct livebox *handler, enum livebox_visible_state state)
2453 {
2454         struct packet *packet;
2455         int ret;
2456
2457         if (!handler) {
2458                 ErrPrint("Handler is NIL\n");
2459                 return LB_STATUS_ERROR_INVALID;
2460         }
2461
2462         if (handler->state != CREATE || !handler->id)
2463                 return LB_STATUS_ERROR_INVALID;
2464
2465         if (!handler->is_user) {
2466                 /* System cluster livebox cannot be changed its visible states */
2467                 if (state == LB_HIDE_WITH_PAUSE) {
2468                         ErrPrint("CA Livebox is not able to change the visibility\n");
2469                         return LB_STATUS_ERROR_PERMISSION;
2470                 }
2471         }
2472
2473         DbgPrint("Change the visibility %d <> %d, %s\n", handler->visible, state, handler->id);
2474
2475         if (handler->visible == state)
2476                 return LB_STATUS_ERROR_ALREADY;
2477
2478         packet = packet_create_noack("change,visibility", "ssi", handler->pkgname, handler->id, (int)state);
2479         if (!packet) {
2480                 ErrPrint("Failed to create a packet\n");
2481                 return LB_STATUS_ERROR_FAULT;
2482         }
2483
2484         ret = master_rpc_request_only(handler, packet);
2485         if (ret == 0)
2486                 handler->visible = state;
2487
2488         return ret;
2489 }
2490
2491 EAPI enum livebox_visible_state livebox_visibility(struct livebox *handler)
2492 {
2493         if (!handler) {
2494                 ErrPrint("Handler is NIL\n");
2495                 return LB_VISIBLE_ERROR;
2496         }
2497
2498         if (handler->state != CREATE || !handler->id)
2499                 return LB_VISIBLE_ERROR;
2500
2501         return handler->visible;
2502 }
2503
2504 int lb_set_group(struct livebox *handler, const char *cluster, const char *category)
2505 {
2506         void *pc = NULL;
2507         void *ps = NULL;
2508
2509         if (cluster) {
2510                 pc = strdup(cluster);
2511                 if (!pc) {
2512                         CRITICAL_LOG("Heap: %s (cluster: %s)\n", strerror(errno), cluster);
2513                         return LB_STATUS_ERROR_MEMORY;
2514                 }
2515         }
2516
2517         if (category) {
2518                 ps = strdup(category);
2519                 if (!ps) {
2520                         CRITICAL_LOG("Heap: %s (category: %s)\n", strerror(errno), category);
2521                         free(pc);
2522                         return LB_STATUS_ERROR_MEMORY;
2523                 }
2524         }
2525
2526         if (handler->cluster)
2527                 free(handler->cluster);
2528
2529         if (handler->category)
2530                 free(handler->category);
2531
2532         handler->cluster = pc;
2533         handler->category = ps;
2534
2535         return LB_STATUS_SUCCESS;
2536 }
2537
2538 void lb_set_size(struct livebox *handler, int w, int h)
2539 {
2540         handler->lb.width = w;
2541         handler->lb.height = h;
2542 }
2543
2544 void lb_set_update_mode(struct livebox *handle, int active_mode)
2545 {
2546         handle->is_active_update = active_mode;
2547 }
2548
2549 void lb_set_pdsize(struct livebox *handler, int w, int h)
2550 {
2551         handler->pd.width = w;
2552         handler->pd.height = h;
2553 }
2554
2555 void lb_set_default_pdsize(struct livebox *handler, int w, int h)
2556 {
2557         handler->pd.default_width = w;
2558         handler->pd.default_height = h;
2559 }
2560
2561 void lb_invoke_fault_handler(enum livebox_fault_type event, const char *pkgname, const char *file, const char *func)
2562 {
2563         struct dlist *l;
2564         struct dlist *n;
2565         struct fault_info *info;
2566
2567         dlist_foreach_safe(s_info.fault_list, l, n, info) {
2568                 if (info->handler(event, pkgname, file, func, info->user_data) == EXIT_FAILURE)
2569                         s_info.fault_list = dlist_remove(s_info.fault_list, l);
2570         }
2571 }
2572
2573 void lb_invoke_event_handler(struct livebox *handler, enum livebox_event_type event)
2574 {
2575         struct dlist *l;
2576         struct dlist *n;
2577         struct event_info *info;
2578
2579         dlist_foreach_safe(s_info.event_list, l, n, info) {
2580                 if (info->handler(handler, event, info->user_data) == EXIT_FAILURE)
2581                         s_info.event_list = dlist_remove(s_info.event_list, l);
2582         }
2583 }
2584
2585 struct livebox *lb_find_livebox(const char *pkgname, const char *id)
2586 {
2587         struct dlist *l;
2588         struct livebox *handler;
2589
2590         dlist_foreach(s_info.livebox_list, l, handler) {
2591                 if (!handler->id)
2592                         continue;
2593
2594                 if (!strcmp(handler->pkgname, pkgname) && !strcmp(handler->id, id))
2595                         return handler;
2596         }
2597
2598         return NULL;
2599 }
2600
2601 struct livebox *lb_find_livebox_by_timestamp(double timestamp)
2602 {
2603         struct dlist *l;
2604         struct livebox *handler;
2605
2606         dlist_foreach(s_info.livebox_list, l, handler) {
2607                 if (handler->timestamp == timestamp)
2608                         return handler;
2609         }
2610
2611         return NULL;
2612 }
2613
2614 static inline char *get_file_kept_in_safe(const char *id)
2615 {
2616         const char *path;
2617         char *new_path;
2618         int len;
2619         int base_idx;
2620
2621         path = util_uri_to_path(id);
2622         if (!path) {
2623                 ErrPrint("Invalid URI(%s)\n", id);
2624                 return NULL;
2625         }
2626
2627         /*!
2628          * \TODO: REMOVE ME
2629          */
2630         if (s_info.prevent_overwrite) {
2631                 new_path = strdup(path);
2632                 if (!new_path)
2633                         ErrPrint("Heap: %s\n", strerror(errno));
2634
2635                 return new_path;
2636         }
2637
2638
2639         len = strlen(path);
2640         base_idx = len - 1;
2641
2642         while (base_idx > 0 && path[base_idx] != '/') base_idx--;
2643         base_idx += (path[base_idx] == '/');
2644
2645         new_path = malloc(len + 10);
2646         if (!new_path) {
2647                 ErrPrint("Heap: %s\n", strerror(errno));
2648                 return NULL;
2649         }
2650
2651         strncpy(new_path, path, base_idx);
2652         snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
2653         return new_path;
2654 }
2655
2656 struct livebox *lb_new_livebox(const char *pkgname, const char *id, double timestamp)
2657 {
2658         struct livebox *handler;
2659
2660         handler = calloc(1, sizeof(*handler));
2661         if (!handler) {
2662                 ErrPrint("Failed to create a new livebox\n");
2663                 return NULL;
2664         }
2665
2666         handler->pkgname = strdup(pkgname);
2667         if (!handler->pkgname) {
2668                 ErrPrint("%s\n", strerror(errno));
2669                 free(handler);
2670                 return NULL;
2671         }
2672
2673         handler->id = strdup(id);
2674         if (!handler->id) {
2675                 ErrPrint("%s\n", strerror(errno));
2676                 free(handler->pkgname);
2677                 free(handler);
2678                 return NULL;
2679         }
2680
2681         handler->filename = get_file_kept_in_safe(id);
2682         if (!handler->filename) {
2683                 handler->filename = strdup(util_uri_to_path(id));
2684                 if (!handler->filename)
2685                         ErrPrint("Error: %s\n", strerror(errno));
2686         }
2687
2688         handler->timestamp = timestamp;
2689         handler->lb.type = _LB_TYPE_FILE;
2690         handler->pd.type = _PD_TYPE_SCRIPT;
2691         handler->state = CREATE;
2692         handler->visible = LB_SHOW;
2693
2694         s_info.livebox_list = dlist_append(s_info.livebox_list, handler);
2695         lb_ref(handler);
2696         return handler;
2697 }
2698
2699 int lb_delete_all(void)
2700 {
2701         struct dlist *l;
2702         struct dlist *n;
2703         struct livebox *handler;
2704
2705         dlist_foreach_safe(s_info.livebox_list, l, n, handler) {
2706                 lb_invoke_event_handler(handler, LB_EVENT_DELETED);
2707                 lb_unref(handler);
2708         }
2709
2710         return LB_STATUS_SUCCESS;
2711 }
2712
2713 int lb_set_content(struct livebox *handler, const char *content)
2714 {
2715         if (handler->content) {
2716                 DbgPrint("Release content: %s\n", content);
2717                 free(handler->content);
2718                 handler->content = NULL;
2719         }
2720
2721         if (content) {
2722                 DbgPrint("Update Content: [%s]\n", content);
2723                 handler->content = strdup(content);
2724                 if (!handler->content) {
2725                         CRITICAL_LOG("Heap: %s (content: %s)\n", strerror(errno), content);
2726                         return LB_STATUS_ERROR_MEMORY;
2727                 }
2728         }
2729
2730         return LB_STATUS_SUCCESS;
2731 }
2732
2733 int lb_set_title(struct livebox *handler, const char *title)
2734 {
2735         if (handler->title) {
2736                 free(handler->title);
2737                 handler->title = NULL;
2738         }
2739
2740         if (title) {
2741                 handler->title = strdup(title);
2742                 if (!handler->title) {
2743                         CRITICAL_LOG("Heap: %s (title: %s)\n", strerror(errno), title);
2744                         return LB_STATUS_ERROR_MEMORY;
2745                 }
2746         }
2747
2748         return LB_STATUS_SUCCESS;
2749 }
2750
2751 void lb_set_size_list(struct livebox *handler, int size_list)
2752 {
2753         handler->lb.size_list = size_list;
2754 }
2755
2756 void lb_set_auto_launch(struct livebox *handler, const char *auto_launch)
2757 {
2758         if (!strlen(auto_launch))
2759                 return;
2760
2761         handler->lb.auto_launch = strdup(auto_launch);
2762         if (!handler->lb.auto_launch)
2763                 ErrPrint("Heap: %s\n", strerror(errno));
2764 }
2765
2766 void lb_set_priority(struct livebox *handler, double priority)
2767 {
2768         handler->lb.priority = priority;
2769 }
2770
2771 void lb_set_id(struct livebox *handler, const char *id)
2772 {
2773         if (handler->id)
2774                 free(handler->id);
2775
2776         handler->id = strdup(id);
2777         if (!handler->id)
2778                 ErrPrint("Error: %s\n", strerror(errno));
2779
2780         if (handler->filename)
2781                 free(handler->filename);
2782
2783         handler->filename = get_file_kept_in_safe(id);
2784         if (!handler->filename) {
2785                 handler->filename = strdup(util_uri_to_path(id));
2786                 if (!handler->filename)
2787                         ErrPrint("Error: %s\n", strerror(errno));
2788         }
2789 }
2790
2791 int lb_set_lb_fb(struct livebox *handler, const char *filename)
2792 {
2793         struct fb_info *fb;
2794
2795         if (!handler)
2796                 return LB_STATUS_ERROR_INVALID;
2797
2798         fb = handler->lb.data.fb;
2799         if (fb && !strcmp(fb_id(fb), filename)) /*!< BUFFER is not changed, */
2800                 return LB_STATUS_SUCCESS;
2801
2802         handler->lb.data.fb = NULL;
2803
2804         if (!filename || filename[0] == '\0') {
2805                 if (fb)
2806                         fb_destroy(fb);
2807                 return LB_STATUS_SUCCESS;
2808         }
2809
2810         handler->lb.data.fb = fb_create(filename, handler->lb.width, handler->lb.height);
2811         if (!handler->lb.data.fb) {
2812                 ErrPrint("Faield to create a FB\n");
2813                 if (fb)
2814                         fb_destroy(fb);
2815                 return LB_STATUS_ERROR_FAULT;
2816         }
2817
2818         if (fb)
2819                 fb_destroy(fb);
2820
2821         return LB_STATUS_SUCCESS;
2822 }
2823
2824 int lb_set_pd_fb(struct livebox *handler, const char *filename)
2825 {
2826         struct fb_info *fb;
2827
2828         if (!handler)
2829                 return LB_STATUS_ERROR_INVALID;
2830
2831         fb = handler->pd.data.fb;
2832         if (fb && !strcmp(fb_id(fb), filename)) {
2833                 /* BUFFER is not changed, just update the content */
2834                 return LB_STATUS_ERROR_EXIST;
2835         }
2836         handler->pd.data.fb = NULL;
2837
2838         if (!filename || filename[0] == '\0') {
2839                 if (fb)
2840                         fb_destroy(fb);
2841                 return LB_STATUS_SUCCESS;
2842         }
2843
2844         handler->pd.data.fb = fb_create(filename, handler->pd.width, handler->pd.height);
2845         if (!handler->pd.data.fb) {
2846                 ErrPrint("Failed to create a FB\n");
2847                 if (fb)
2848                         fb_destroy(fb);
2849                 return LB_STATUS_ERROR_FAULT;
2850         }
2851
2852         if (fb)
2853                 fb_destroy(fb);
2854         return LB_STATUS_SUCCESS;
2855 }
2856
2857 struct fb_info *lb_get_lb_fb(struct livebox *handler)
2858 {
2859         return handler->lb.data.fb;
2860 }
2861
2862 struct fb_info *lb_get_pd_fb(struct livebox *handler)
2863 {
2864         return handler->pd.data.fb;
2865 }
2866
2867 void lb_set_user(struct livebox *handler, int user)
2868 {
2869         handler->is_user = user;
2870 }
2871
2872 void lb_set_pinup(struct livebox *handler, int pinup_supported)
2873 {
2874         handler->lb.pinup_supported = pinup_supported;
2875 }
2876
2877 void lb_set_text_lb(struct livebox *handler)
2878 {
2879         handler->lb.type = _LB_TYPE_TEXT;
2880 }
2881
2882 void lb_set_text_pd(struct livebox *handler)
2883 {
2884         handler->pd.type = _PD_TYPE_TEXT;
2885 }
2886
2887 int lb_text_lb(struct livebox *handler)
2888 {
2889         return handler->lb.type == _LB_TYPE_TEXT;
2890 }
2891
2892 int lb_text_pd(struct livebox *handler)
2893 {
2894         return handler->pd.type == _PD_TYPE_TEXT;
2895 }
2896
2897 void lb_set_period(struct livebox *handler, double period)
2898 {
2899         handler->lb.period = period;
2900 }
2901
2902 struct livebox *lb_ref(struct livebox *handler)
2903 {
2904         if (!handler)
2905                 return NULL;
2906
2907         handler->refcnt++;
2908         return handler;
2909 }
2910
2911 struct livebox *lb_unref(struct livebox *handler)
2912 {
2913         if (!handler)
2914                 return NULL;
2915
2916         handler->refcnt--;
2917         if (handler->refcnt > 0)
2918                 return handler;
2919
2920         if (handler->created_cb) {
2921                 DbgPrint("Clear created event callback\n");
2922                 handler->created_cb(handler, LB_STATUS_ERROR_FAULT, handler->created_cbdata);
2923                 handler->created_cb = NULL;
2924                 handler->created_cbdata = NULL;
2925         }
2926
2927         if (handler->deleted_cb) {
2928                 DbgPrint("Clear deleted event callback\n");
2929                 handler->deleted_cb(handler, LB_STATUS_ERROR_FAULT, handler->deleted_cbdata);
2930                 handler->deleted_cb = NULL;
2931                 handler->deleted_cbdata = NULL;
2932         }
2933
2934         if (handler->pinup_cb) {
2935                 DbgPrint("Clear pinup event callback\n");
2936                 handler->pinup_cb(handler, LB_STATUS_ERROR_FAULT, handler->pinup_cbdata);
2937                 handler->pinup_cb = NULL;
2938                 handler->pinup_cbdata = NULL;
2939         }
2940
2941         if (handler->group_changed_cb) {
2942                 DbgPrint("Clear group changed event callback\n");
2943                 handler->group_changed_cb(handler, LB_STATUS_ERROR_FAULT, handler->group_cbdata);
2944                 handler->group_changed_cb = NULL;
2945                 handler->group_cbdata = NULL;
2946         }
2947
2948         if (handler->period_changed_cb) {
2949                 DbgPrint("Clear period changed event callback\n");
2950                 handler->period_changed_cb(handler, LB_STATUS_ERROR_FAULT, handler->period_cbdata);
2951                 handler->period_changed_cb = NULL;
2952                 handler->period_cbdata = NULL;
2953         }
2954
2955         if (handler->size_changed_cb) {
2956                 DbgPrint("Clear size changed event callback\n");
2957                 handler->size_changed_cb(handler, LB_STATUS_ERROR_FAULT, handler->size_cbdata);
2958                 handler->size_changed_cb = NULL;
2959                 handler->size_cbdata = NULL;
2960         }
2961
2962         if (handler->pd_created_cb) {
2963                 DbgPrint("Clear pd created event callback\n");
2964                 handler->pd_created_cb(handler, LB_STATUS_ERROR_FAULT, handler->pd_created_cbdata);
2965                 handler->pd_created_cb = NULL;
2966                 handler->pd_created_cbdata = NULL;
2967         }
2968
2969         if (handler->pd_destroyed_cb) {
2970                 DbgPrint("Clear pd destroyed event callback\n");
2971                 handler->pd_destroyed_cb(handler, LB_STATUS_ERROR_FAULT, handler->pd_destroyed_cbdata);
2972                 handler->pd_destroyed_cb = NULL;
2973                 handler->pd_destroyed_cbdata = NULL;
2974         }
2975
2976         if (handler->update_mode_cb) {
2977                 DbgPrint("Clear update mode callback\n");
2978                 handler->update_mode_cb(handler, LB_STATUS_ERROR_FAULT, handler->update_mode_cbdata);
2979                 handler->update_mode_cb = NULL;
2980                 handler->update_mode_cbdata = NULL;
2981         }
2982
2983         if (handler->access_event_cb) {
2984                 DbgPrint("Clear access status callback\n");
2985                 handler->access_event_cb(handler, LB_STATUS_ERROR_FAULT, handler->access_event_cbdata);
2986                 handler->access_event_cb = NULL;
2987                 handler->access_event_cbdata = NULL;
2988         }
2989
2990         if (handler->filename)
2991                 util_unlink(handler->filename);
2992
2993         dlist_remove_data(s_info.livebox_list, handler);
2994
2995         handler->state = DESTROYED;
2996         free(handler->cluster);
2997         free(handler->category);
2998         free(handler->id);
2999         free(handler->pkgname);
3000         free(handler->filename);
3001         free(handler->lb.auto_launch);
3002
3003         if (handler->lb.data.fb) {
3004                 fb_destroy(handler->lb.data.fb);
3005                 handler->lb.data.fb = NULL;
3006         }
3007
3008         if (handler->pd.data.fb) {
3009                 fb_destroy(handler->pd.data.fb);
3010                 handler->pd.data.fb = NULL;
3011         }
3012
3013         free(handler);
3014         return NULL;
3015 }
3016
3017 int lb_send_delete(struct livebox *handler, ret_cb_t cb, void *data)
3018 {
3019         struct packet *packet;
3020         struct cb_info *cbinfo;
3021         int ret;
3022
3023         if (!cb && !!data) {
3024                 ErrPrint("Invalid argument\n");
3025                 return LB_STATUS_ERROR_INVALID;
3026         }
3027
3028         if (handler->deleted_cb) {
3029                 ErrPrint("Already in-progress\n");
3030                 return LB_STATUS_ERROR_BUSY;
3031         }
3032
3033         packet = packet_create("delete", "ss", handler->pkgname, handler->id);
3034         if (!packet) {
3035                 ErrPrint("Failed to build a param\n");
3036                 if (cb)
3037                         cb(handler, LB_STATUS_ERROR_FAULT, data);
3038
3039                 return LB_STATUS_ERROR_FAULT;
3040         }
3041
3042         if (!cb)
3043                 cb = default_delete_cb;
3044
3045         cbinfo = create_cb_info(cb, data);
3046         if (!cbinfo) {
3047                 packet_destroy(packet);
3048                 return LB_STATUS_ERROR_FAULT;
3049         }
3050
3051         ret = master_rpc_async_request(handler, packet, 0, del_ret_cb, cbinfo);
3052         if (ret < 0)
3053                 destroy_cb_info(cbinfo);
3054
3055         return ret;
3056 }
3057
3058 EAPI int livebox_client_paused(void)
3059 {
3060         struct packet *packet;
3061
3062         packet = packet_create_noack("client_paused", "d", util_timestamp());
3063         if (!packet) {
3064                 ErrPrint("Failed to create a pause packet\n");
3065                 return LB_STATUS_ERROR_FAULT;
3066         }
3067
3068         return master_rpc_request_only(NULL, packet);
3069 }
3070
3071 EAPI int livebox_client_resumed(void)
3072 {
3073         struct packet *packet;
3074
3075         packet = packet_create_noack("client_resumed", "d", util_timestamp());
3076         if (!packet) {
3077                 ErrPrint("Failed to create a resume packet\n");
3078                 return LB_STATUS_ERROR_FAULT;
3079         }
3080
3081         return master_rpc_request_only(NULL, packet);
3082 }
3083
3084 /* End of a file */