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