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