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