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