tizen 2.3 release
[apps/livebox/livebox-viewer.git] / dynamicbox_viewer / src / dynamicbox.c
1 /*
2  * Copyright 2014  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 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <gio/gio.h>
28 #include <aul.h>
29 #include <dlog.h>
30
31 #include <com-core_packet.h>
32 #include <packet.h>
33 #include <dynamicbox_service.h>
34 #include <dynamicbox_errno.h>
35 #include <dynamicbox_cmd_list.h>
36 #include <dynamicbox_buffer.h>
37
38 #include "debug.h"
39 #include "fb.h"
40 #include "dynamicbox.h"
41 #include "dynamicbox_internal.h"
42 #include "dlist.h"
43 #include "util.h"
44 #include "master_rpc.h"
45 #include "client.h"
46 #include "conf.h"
47
48 #define EAPI __attribute__((visibility("default")))
49
50 #if defined(FLOG)
51 FILE *__file_log_fp;
52 #endif
53
54 static int default_launch_handler(dynamicbox_h handler, const char *appid, void *data);
55
56 static struct info {
57     int init_count;
58     int prevent_overwrite;
59     guint job_timer;
60     struct dlist *job_list;
61
62     struct launch {
63         int (*handler)(dynamicbox_h handler, const char *appid, void *data);
64         void *data;
65     } launch;
66 } s_info = {
67     .init_count = 0,
68     .prevent_overwrite = 0,
69     .job_timer = 0,
70     .job_list = NULL,
71     .launch = {
72         .handler = default_launch_handler,
73         .data = NULL,
74     },
75 };
76
77 static void dbox_pixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data);
78 static void gbar_pixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data);
79 static void gbar_xpixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data);
80 static void dbox_xpixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data);
81
82 static int default_launch_handler(dynamicbox_h handler, const char *appid, void *data)
83 {
84     int ret;
85
86     ret = aul_launch_app(appid, NULL);
87     if (ret <= 0) {
88         ErrPrint("Failed to launch an app %s (%d)\n", appid, ret);
89     }
90
91     /*
92        app_control_h service;
93
94        DbgPrint("AUTO_LAUNCH [%s]\n", handler->common->dbox.auto_launch);
95
96        ret = app_control_create(&service);
97        if (ret == APP_CONTROL_ERROR_NONE) {
98        app_control_set_package(service, handler->common->dbox.auto_launch);
99        app_control_send_launch_request(service, NULL, NULL);
100        app_control_destroy(service);
101        } else {
102        ErrPrint("Failed to launch an app %s (%d)\n", handler->common->dbox.auto_launch, ret);
103        }
104      */
105
106     return ret > 0 ? DBOX_STATUS_ERROR_NONE : DBOX_STATUS_ERROR_FAULT;
107 }
108
109 static inline void default_create_cb(dynamicbox_h handler, int ret, void *data)
110 {
111     DbgPrint("Default created event handler: %d\n", ret);
112 }
113
114 static inline void default_pinup_cb(dynamicbox_h handler, int ret, void *data)
115 {
116     DbgPrint("Default pinup event handler: %d\n", ret);
117 }
118
119 static inline void default_group_changed_cb(dynamicbox_h handler, int ret, void *data)
120 {
121     DbgPrint("Default group changed event handler: %d\n", ret);
122 }
123
124 static inline void default_period_changed_cb(dynamicbox_h handler, int ret, void *data)
125 {
126     DbgPrint("Default period changed event handler: %d\n", ret);
127 }
128
129 static inline void default_gbar_created_cb(dynamicbox_h handler, int ret, void *data)
130 {
131     DbgPrint("Default GBAR created event handler: %d\n", ret);
132 }
133
134 static inline void default_gbar_destroyed_cb(dynamicbox_h handler, int ret, void *data)
135 {
136     DbgPrint("Default GBAR destroyed event handler: %d\n", ret);
137 }
138
139 static inline void default_dbox_size_changed_cb(dynamicbox_h handler, int ret, void *data)
140 {
141     DbgPrint("Default DBOX size changed event handler: %d\n", ret);
142 }
143
144 static inline void default_update_mode_cb(dynamicbox_h handler, int ret, void *data)
145 {
146     DbgPrint("Default update mode set event handler: %d\n", ret);
147 }
148
149 static inline void default_access_event_cb(dynamicbox_h handler, int ret, void *data)
150 {
151     DbgPrint("Default access event handler: %d\n", ret);
152 }
153
154 static inline void default_key_event_cb(dynamicbox_h handler, int ret, void *data)
155 {
156     DbgPrint("Default key event handler: %d\n", ret);
157 }
158
159 static void update_mode_cb(dynamicbox_h handler, const struct packet *result, void *data)
160 {
161     int ret;
162
163     if (!result) {
164         ret = DBOX_STATUS_ERROR_FAULT;
165         goto errout;
166     } else if (packet_get(result, "i", &ret) != 1) {
167         ErrPrint("Invalid argument\n");
168         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
169         goto errout;
170     }
171
172     if (ret < 0) {
173         ErrPrint("Resize request is failed: %d\n", ret);
174         goto errout;
175     }
176
177     return;
178
179 errout:
180     handler->cbs.update_mode.cb(handler, ret, handler->cbs.update_mode.data);
181     handler->cbs.update_mode.cb = NULL;
182     handler->cbs.update_mode.data = NULL;
183     handler->common->request.update_mode = 0;
184
185     if (handler->common->state != DBOX_STATE_DELETE) {
186         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
187             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
188             dbox_unref(handler, 1);
189         }
190     }
191 }
192
193 static void resize_cb(dynamicbox_h handler, const struct packet *result, void *data)
194 {
195     int ret;
196
197     if (!result) {
198         ret = DBOX_STATUS_ERROR_FAULT;
199         goto errout;
200     } else if (packet_get(result, "i", &ret) != 1) {
201         ErrPrint("Invalid argument\n");
202         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
203         goto errout;
204     }
205
206     /*!
207      * \note
208      * In case of resize request,
209      * The dynamicbox handler will not have resized value right after this callback,
210      * It can only get the new size when it makes updates.
211      *
212      * So the user can only get the resized value(result) from the first update event
213      * after this request.
214      */
215     if (ret < 0) {
216         ErrPrint("Resize request is failed: %d\n", ret);
217         goto errout;
218     }
219
220     return;
221
222 errout:
223     handler->cbs.size_changed.cb(handler, ret, handler->cbs.size_changed.data);
224     handler->cbs.size_changed.cb = NULL;
225     handler->cbs.size_changed.data = NULL;
226     handler->common->request.size_changed = 0;
227
228     if (handler->common->state != DBOX_STATE_DELETE) {
229         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
230             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
231             dbox_unref(handler, 1);
232         }
233     }
234 }
235
236 static void text_signal_cb(dynamicbox_h handler, const struct packet *result, void *data)
237 {
238     int ret;
239     void *cbdata;
240     struct cb_info *info = data;
241     dynamicbox_ret_cb cb;
242
243     cbdata = info->data;
244     cb = info->cb;
245     dbox_destroy_cb_info(info);
246
247     if (!result) {
248         ret = DBOX_STATUS_ERROR_FAULT;
249     } else if (packet_get(result, "i", &ret) != 1) {
250         ErrPrint("Invalid argument\n");
251         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
252     }
253
254     if (cb) {
255         cb(handler, ret, cbdata);
256     }
257     return;
258 }
259
260 static void set_group_ret_cb(dynamicbox_h handler, const struct packet *result, void *data)
261 {
262     int ret;
263
264     if (!result) {
265         ret = DBOX_STATUS_ERROR_FAULT;
266         goto errout;
267     } else if (packet_get(result, "i", &ret) != 1) {
268         ErrPrint("Invalid argument\n");
269         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
270         goto errout;
271     }
272
273     if (ret < 0) {
274         goto errout;
275     }
276
277     return;
278
279 errout:
280     handler->cbs.group_changed.cb(handler, ret, handler->cbs.group_changed.data);
281     handler->cbs.group_changed.cb = NULL;
282     handler->cbs.group_changed.data = NULL;
283     handler->common->request.group_changed = 0;
284
285     if (handler->common->state != DBOX_STATE_DELETE) {
286         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
287             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
288             dbox_unref(handler, 1);
289         }
290     }
291 }
292
293 static void period_ret_cb(dynamicbox_h handler, const struct packet *result, void *data)
294 {
295     int ret;
296
297     if (!result) {
298         ret = DBOX_STATUS_ERROR_FAULT;
299         goto errout;
300     } else if (packet_get(result, "i", &ret) != 1) {
301         ErrPrint("Invalid argument\n");
302         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
303         goto errout;
304     }
305
306     if (ret < 0) {
307         goto errout;
308     }
309
310     return;
311
312 errout:
313     handler->cbs.period_changed.cb(handler, ret, handler->cbs.period_changed.data);
314     handler->cbs.period_changed.cb = NULL;
315     handler->cbs.period_changed.data = NULL;
316     handler->common->request.period_changed = 0;
317
318     if (handler->common->state != DBOX_STATE_DELETE) {
319         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
320             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
321             dbox_unref(handler, 1);
322         }
323     }
324 }
325
326 static void gbar_create_cb(dynamicbox_h handler, const struct packet *result, void *data)
327 {
328     int ret;
329
330     if (!result) {
331         ret = DBOX_STATUS_ERROR_FAULT;
332         goto errout;
333     } else if (packet_get(result, "i", &ret) != 1) {
334         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
335         goto errout;
336     }
337
338     if (ret < 0) {
339         ErrPrint("Failed to create a GBAR[%d]\n", ret);
340         goto errout;
341     }
342
343     return;
344
345 errout:
346     handler->cbs.gbar_created.cb(handler, ret, handler->cbs.gbar_created.data);
347     handler->cbs.gbar_created.cb = NULL;
348     handler->cbs.gbar_created.data = NULL;
349     handler->common->request.gbar_created = 0;
350
351     if (handler->common->state != DBOX_STATE_DELETE) {
352         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
353             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
354             dbox_unref(handler, 1);
355         }
356     }
357 }
358
359 static void activated_cb(dynamicbox_h handler, const struct packet *result, void *data)
360 {
361     int ret;
362     struct cb_info *info = data;
363     void *cbdata;
364     dynamicbox_ret_cb cb;
365     const char *pkgname = "";
366
367     cbdata = info->data;
368     cb = info->cb;
369     dbox_destroy_cb_info(info);
370
371     if (!result) {
372         ret = DBOX_STATUS_ERROR_FAULT;
373     } else if (packet_get(result, "is", &ret, &pkgname) != 2) {
374         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
375     }
376
377     if (cb) {
378         cb(handler, ret, cbdata);
379     }
380 }
381
382 static void gbar_destroy_cb(dynamicbox_h handler, const struct packet *result, void *data)
383 {
384     int ret;
385     dynamicbox_ret_cb cb;
386     void *cbdata;
387     struct cb_info *info = data;
388
389     cbdata = info->data;
390     cb = info->cb;
391     dbox_destroy_cb_info(info);
392
393     if (!result) {
394         ErrPrint("Result is NIL (may connection lost)\n");
395         ret = DBOX_STATUS_ERROR_FAULT;
396     } else if (packet_get(result, "i", &ret) != 1) {
397         ErrPrint("Invalid parameter\n");
398         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
399     }
400
401     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
402         handler->cbs.gbar_destroyed.cb = cb;
403         handler->cbs.gbar_destroyed.data = cbdata;
404     } else {
405         handler->common->is_gbar_created = 0;
406         handler->common->request.gbar_destroyed = 0;
407
408         if (cb) {
409             cb(handler, ret, cbdata);
410         }
411     }
412 }
413
414 static void delete_cluster_cb(dynamicbox_h handler, const struct packet *result, void *data)
415 {
416     struct cb_info *info = data;
417     int ret;
418     dynamicbox_ret_cb cb;
419     void *cbdata;
420
421     cb = info->cb;
422     cbdata = info->data;
423     dbox_destroy_cb_info(info);
424
425     if (!result) {
426         ret = DBOX_STATUS_ERROR_FAULT;
427     } else if (packet_get(result, "i", &ret) != 1) {
428         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
429     }
430
431     if (cb) {
432         cb(handler, ret, cbdata);
433     }
434 }
435
436 static void delete_category_cb(dynamicbox_h handler, const struct packet *result, void *data)
437 {
438     struct cb_info *info = data;
439     int ret;
440     dynamicbox_ret_cb cb;
441     void *cbdata;
442
443     cb = info->cb;
444     cbdata = info->data;
445     dbox_destroy_cb_info(info);
446
447     if (!result) {
448         ret = DBOX_STATUS_ERROR_FAULT;
449     } else if (packet_get(result, "i", &ret) != 1) {
450         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
451     }
452
453     if (cb) {
454         cb(handler, ret, cbdata);
455     }
456 }
457
458 static int dbox_acquire_dbox_pixmap(dynamicbox_h handler, dynamicbox_ret_cb cb, void *data)
459 {
460     struct packet *packet;
461     struct cb_info *cbinfo;
462     const char *id;
463     unsigned int cmd = CMD_DBOX_ACQUIRE_PIXMAP;
464     int ret;
465
466     id = fb_id(handler->common->dbox.fb);
467     if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
468         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
469     }
470
471     packet = packet_create((const char *)&cmd, "ss", handler->common->pkgname, handler->common->id);
472     if (!packet) {
473         ErrPrint("Failed to build a param\n");
474         return DBOX_STATUS_ERROR_FAULT;
475     }
476
477     cbinfo = dbox_create_cb_info(cb, data);
478     if (!cbinfo) {
479         packet_destroy(packet);
480         return DBOX_STATUS_ERROR_FAULT;
481     }
482
483     ret = master_rpc_async_request(handler, packet, 0, dbox_pixmap_acquired_cb, cbinfo);
484     if (ret < 0) {
485         dbox_destroy_cb_info(cbinfo);
486     }
487
488     return ret;
489 }
490
491 static void dbox_pixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data)
492 {
493     int pixmap;
494     int ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
495     dynamicbox_ret_cb cb;
496     void *cbdata;
497     struct cb_info *info = data;
498
499     cb = info->cb;
500     cbdata = info->data;
501     dbox_destroy_cb_info(info);
502
503     if (!result) {
504         pixmap = 0; /* PIXMAP 0 means error */
505     } else if (packet_get(result, "ii", &pixmap, &ret) != 2) {
506         pixmap = 0;
507     }
508
509     if (ret == (int)DBOX_STATUS_ERROR_BUSY) {
510         ret = dbox_acquire_dbox_pixmap(handler, cb, cbdata);
511         DbgPrint("Busy, Try again: %d\n", ret);
512         /* Try again */
513     } else if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
514         if (cb) {
515             cb(handler, pixmap, cbdata);
516         }
517
518         if (handler->common->state != DBOX_STATE_DELETE) {
519             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
520             dbox_unref(handler, 1);
521         }
522     } else {
523         if (cb) {
524             cb(handler, pixmap, cbdata);
525         }
526     }
527 }
528
529 static void dbox_xpixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data)
530 {
531     int pixmap;
532     int ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
533     dynamicbox_ret_cb cb;
534     void *cbdata;
535     struct cb_info *info = data;
536
537     cb = info->cb;
538     cbdata = info->data;
539     dbox_destroy_cb_info(info);
540
541     if (!result) {
542         pixmap = 0;
543     } else if (packet_get(result, "ii", &pixmap, &ret) != 2) {
544         pixmap = 0;
545     }
546
547     if (cb) {
548         cb(handler, pixmap, cbdata);
549     }
550
551     if (handler->common->state != DBOX_STATE_DELETE) {
552         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
553             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
554             dbox_unref(handler, 1);
555         }
556     }
557 }
558
559 static int dbox_acquire_gbar_extra_pixmap(dynamicbox_h handler, int idx, dynamicbox_ret_cb cb, void *data)
560 {
561     struct packet *packet;
562     struct cb_info *cbinfo;
563     unsigned int cmd = CMD_GBAR_ACQUIRE_XPIXMAP;
564     int ret;
565
566     packet = packet_create((const char *)&cmd, "ssi", handler->common->pkgname, handler->common->id, idx);
567     if (!packet) {
568         ErrPrint("Failed to build a param\n");
569         return DBOX_STATUS_ERROR_FAULT;
570     }
571
572     cbinfo = dbox_create_cb_info(cb, data);
573     if (!cbinfo) {
574         packet_destroy(packet);
575         return DBOX_STATUS_ERROR_FAULT;
576     }
577
578     ret = master_rpc_async_request(handler, packet, 0, gbar_xpixmap_acquired_cb, cbinfo);
579     if (ret < 0) {
580         /*!
581          * \note
582          * Packet will be destroyed by master_rpc_async_request
583          */
584         dbox_destroy_cb_info(cbinfo);
585     }
586
587     return ret;
588 }
589
590 static int dbox_acquire_dbox_extra_pixmap(dynamicbox_h handler, int idx, dynamicbox_ret_cb cb, void *data)
591 {
592     struct packet *packet;
593     struct cb_info *cbinfo;
594     unsigned int cmd = CMD_DBOX_ACQUIRE_XPIXMAP;
595     int ret;
596
597     packet = packet_create((const char *)&cmd, "ssi", handler->common->pkgname, handler->common->id, idx);
598     if (!packet) {
599         ErrPrint("Failed to build a param\n");
600         return DBOX_STATUS_ERROR_FAULT;
601     }
602
603     cbinfo = dbox_create_cb_info(cb, data);
604     if (!cbinfo) {
605         packet_destroy(packet);
606         return DBOX_STATUS_ERROR_FAULT;
607     }
608
609     ret = master_rpc_async_request(handler, packet, 0, dbox_xpixmap_acquired_cb, cbinfo);
610     if (ret < 0) {
611         /*!
612          * \note
613          * Packet will be destroyed by master_rpc_async_request
614          */
615         dbox_destroy_cb_info(cbinfo);
616     }
617
618     return ret;
619 }
620
621 static int dbox_acquire_gbar_pixmap(dynamicbox_h handler, dynamicbox_ret_cb cb, void *data)
622 {
623     struct packet *packet;
624     struct cb_info *cbinfo;
625     unsigned int cmd = CMD_GBAR_ACQUIRE_PIXMAP;
626     const char *id;
627     int ret;
628
629     id = fb_id(handler->common->gbar.fb);
630     if (!id || strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
631         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
632     }
633
634     packet = packet_create((const char *)&cmd, "ss", handler->common->pkgname, handler->common->id);
635     if (!packet) {
636         ErrPrint("Failed to build a param\n");
637         return DBOX_STATUS_ERROR_FAULT;
638     }
639
640     cbinfo = dbox_create_cb_info(cb, data);
641     if (!cbinfo) {
642         packet_destroy(packet);
643         return DBOX_STATUS_ERROR_FAULT;
644     }
645
646     ret = master_rpc_async_request(handler, packet, 0, gbar_pixmap_acquired_cb, cbinfo);
647     if (ret < 0) {
648         /*!
649          * \note
650          * Packet will be destroyed by master_rpc_async_request
651          */
652         dbox_destroy_cb_info(cbinfo);
653     }
654
655     return ret;
656 }
657
658 static void gbar_xpixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data)
659 {
660     int pixmap;
661     int ret;
662     dynamicbox_ret_cb cb;
663     void *cbdata;
664     struct cb_info *info = data;
665
666     cb = info->cb;
667     cbdata = info->data;
668     dbox_destroy_cb_info(info);
669
670     if (!result) {
671         pixmap = 0; /* PIXMAP 0 means error */
672         ret = DBOX_STATUS_ERROR_FAULT;
673     } else if (packet_get(result, "ii", &pixmap, &ret) != 2) {
674         pixmap = 0;
675         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
676     }
677
678     if (cb) {
679         DbgPrint("ret: %x, pixmap: %d\n", ret, pixmap);
680         cb(handler, pixmap, cbdata);
681     }
682
683     if (handler->common->state != DBOX_STATE_DELETE) {
684         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
685             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
686             dbox_unref(handler, 1);
687         }
688     }
689 }
690
691 static void gbar_pixmap_acquired_cb(dynamicbox_h handler, const struct packet *result, void *data)
692 {
693     int pixmap;
694     int ret;
695     dynamicbox_ret_cb cb;
696     void *cbdata;
697     struct cb_info *info = data;
698
699     cb = info->cb;
700     cbdata = info->data;
701     dbox_destroy_cb_info(info);
702
703     if (!result) {
704         pixmap = 0; /* PIXMAP 0 means error */
705         ret = DBOX_STATUS_ERROR_FAULT;
706     } else if (packet_get(result, "ii", &pixmap, &ret) != 2) {
707         pixmap = 0;
708         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
709     }
710
711     if (ret == (int)DBOX_STATUS_ERROR_BUSY) {
712         ret = dbox_acquire_gbar_pixmap(handler, cb, cbdata);
713         DbgPrint("Busy, Try again: %d\n", ret);
714         /* Try again */
715     } else if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
716         if (cb) {
717             cb(handler, pixmap, cbdata);
718         }
719
720         if (handler->common->state != DBOX_STATE_DELETE) {
721             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
722             dbox_unref(handler, 1);
723         }
724     } else {
725         if (cb) {
726             DbgPrint("ret: %d, pixmap: %d\n", ret, pixmap);
727             cb(handler, pixmap, cbdata);
728         }
729     }
730 }
731
732 static void pinup_done_cb(dynamicbox_h handler, const struct packet *result, void *data)
733 {
734     int ret;
735
736     if (!result) {
737         ret = DBOX_STATUS_ERROR_FAULT;
738         goto errout;
739     } else if (packet_get(result, "i", &ret) != 1) {
740         goto errout;
741     }
742
743     if (ret < 0) {
744         goto errout;
745     }
746
747     return;
748
749 errout:
750     handler->cbs.pinup.cb(handler, ret, handler->cbs.pinup.data);
751     handler->cbs.pinup.cb = NULL;
752     handler->cbs.pinup.data = NULL;
753     handler->common->request.pinup = 0;
754
755     if (handler->common->state != DBOX_STATE_DELETE) {
756         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
757             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
758             dbox_unref(handler, 1);
759         }
760     }
761 }
762
763 static void key_ret_cb(dynamicbox_h handler, const struct packet *result, void *data)
764 {
765     int ret;
766
767     if (!result) {
768         ret = DBOX_STATUS_ERROR_FAULT;
769         return;
770     }
771
772     if (packet_get(result, "i", &ret) != 1) {
773         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
774         return;
775     }
776
777     if (ret != DBOX_STATUS_ERROR_NONE) {
778         goto errout;
779     }
780
781     return;
782 errout:
783     handler->cbs.key_event.cb(handler, ret, handler->cbs.key_event.data);
784     handler->cbs.key_event.cb = NULL;
785     handler->cbs.key_event.data = NULL;
786     handler->common->request.key_event = 0;
787
788     if (handler->common->state != DBOX_STATE_DELETE) {
789         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
790             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
791             dbox_unref(handler, 1);
792         }
793     }
794 }
795
796 static void access_ret_cb(dynamicbox_h handler, const struct packet *result, void *data)
797 {
798     int ret;
799
800     if (!result) {
801         ret = DBOX_STATUS_ERROR_FAULT;
802         return;
803     }
804
805     if (packet_get(result, "i", &ret) != 1) {
806         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
807         return;
808     }
809
810     if (ret != DBOX_STATUS_ERROR_NONE) {
811         goto errout;
812     }
813
814     return;
815
816 errout:
817     handler->cbs.access_event.cb(handler, ret, handler->cbs.access_event.data);
818     handler->cbs.access_event.cb = NULL;
819     handler->cbs.access_event.data = NULL;
820     handler->common->request.access_event = 0;
821
822     if (handler->common->state != DBOX_STATE_DELETE) {
823         if (ret == (int)DBOX_STATUS_ERROR_NOT_EXIST && handler->refcnt == 2) {
824             dbox_invoke_event_handler(handler, DBOX_EVENT_DELETED);
825             dbox_unref(handler, 1);
826         }
827     }
828 }
829
830 static int send_access_event(dynamicbox_h handler, const char *event, int x, int y, int type)
831 {
832     struct packet *packet;
833     double timestamp;
834
835     timestamp = util_timestamp();
836
837     packet = packet_create(event, "ssdiii", handler->common->pkgname, handler->common->id, timestamp, x, y, type);
838     if (!packet) {
839         ErrPrint("Failed to build packet\n");
840         return DBOX_STATUS_ERROR_FAULT;
841     }
842
843     return master_rpc_async_request(handler, packet, 0, access_ret_cb, NULL);
844 }
845
846 static int send_key_event(dynamicbox_h handler, const char *event, unsigned int keycode)
847 {
848     struct packet *packet;
849     double timestamp;
850
851     timestamp = util_timestamp();
852     packet = packet_create(event, "ssdi", handler->common->pkgname, handler->common->id, timestamp, keycode);
853     if (!packet) {
854         ErrPrint("Failed to build packet\n");
855         return DBOX_STATUS_ERROR_FAULT;
856     }
857
858     return master_rpc_async_request(handler, packet, 0, key_ret_cb, NULL);
859 }
860
861 static int send_mouse_event(dynamicbox_h handler, const char *event, int x, int y)
862 {
863     struct packet *packet;
864     double timestamp;
865
866     timestamp = util_timestamp();
867     packet = packet_create_noack(event, "ssdii", handler->common->pkgname, handler->common->id, timestamp, x, y);
868     if (!packet) {
869         ErrPrint("Failed to build param\n");
870         return DBOX_STATUS_ERROR_FAULT;
871     }
872
873     return master_rpc_request_only(handler, packet);
874 }
875
876 static int initialize_dynamicbox(void *disp, int use_thread)
877 {
878     int ret;
879 #if defined(FLOG)
880     char filename[BUFSIZ];
881     snprintf(filename, sizeof(filename), "/tmp/%d.box.log", getpid());
882     __file_log_fp = fopen(filename, "w+t");
883     if (!__file_log_fp) {
884         __file_log_fp = fdopen(1, "w+t");
885     }
886 #endif
887     ret = dynamicbox_service_init();
888     if (ret != DBOX_STATUS_ERROR_NONE) {
889         return ret;
890     }
891
892     ret = fb_init(disp);
893     if (ret != DBOX_STATUS_ERROR_NONE) {
894         dynamicbox_service_fini();
895         return ret;
896     }
897
898     ret = client_init(use_thread);
899     if (ret != DBOX_STATUS_ERROR_NONE) {
900         fb_fini();
901         dynamicbox_service_fini();
902         return ret;
903     }
904
905     s_info.init_count++;
906     return ret;
907 }
908
909 static inline char *dbox_pkgname(const char *pkgname)
910 {
911     char *dbox;
912
913     dbox = dynamicbox_service_dbox_id(pkgname);
914     if (!dbox) {
915         dbox = strdup(pkgname);
916     }
917
918     return dbox;
919 }
920
921 static gboolean job_execute_cb(void *data)
922 {
923     struct job_item *item;
924     struct dlist *l;
925
926     l = dlist_nth(s_info.job_list, 0);
927     if (!l) {
928         s_info.job_timer = 0;
929         return FALSE;
930     }
931
932     item = dlist_data(l);
933     s_info.job_list = dlist_remove(s_info.job_list, l);
934
935     if (item) {
936         item->cb(item->handle, item->ret, item->data);
937         dbox_unref(item->handle, 1);
938         free(item);
939     }
940
941     return TRUE;
942 }
943
944 static int job_add(dynamicbox_h handle, dynamicbox_ret_cb job_cb, int ret, void *data)
945 {
946     struct job_item *item;
947
948     if (!job_cb) {
949         ErrPrint("Invalid argument\n");
950         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
951     }
952
953     item = malloc(sizeof(*item));
954     if (!item) {
955         ErrPrint("Heap: %s\n", strerror(errno));
956         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
957     }
958
959     item->handle = dbox_ref(handle);
960     item->cb = job_cb;
961     item->data = data;
962     item->ret = ret;
963
964     s_info.job_list = dlist_append(s_info.job_list, item);
965
966     if (!s_info.job_timer) {
967         s_info.job_timer = g_timeout_add(1, job_execute_cb, NULL);
968         if (!s_info.job_timer) {
969             ErrPrint("Failed to create a job timer\n");
970         }
971     }
972
973     return DBOX_STATUS_ERROR_NONE;
974 }
975
976 static void new_ret_cb(dynamicbox_h handler, const struct packet *result, void *data)
977 {
978     int ret;
979     struct cb_info *info = data;
980     dynamicbox_ret_cb cb;
981     void *cbdata;
982
983     cb = info->cb;
984     cbdata = info->data;
985     dbox_destroy_cb_info(info);
986
987     if (!result) {
988         ret = DBOX_STATUS_ERROR_FAULT;
989     } else if (packet_get(result, "i", &ret) != 1) {
990         ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
991     }
992
993     if (ret >= 0) {
994         handler->cbs.created.cb = cb;
995         handler->cbs.created.data = cbdata;
996
997         /*!
998          * \note
999          * Don't go anymore ;)
1000          */
1001         return;
1002     } else if (cb) {
1003         /*!
1004          * \note
1005          * It means the current instance is not created,
1006          * so user has to know about this.
1007          * notice it to user using "deleted" event.
1008          */
1009         cb(handler, ret, cbdata);
1010     }
1011
1012     dbox_unref(handler, 1);
1013 }
1014
1015 static int create_real_instance(dynamicbox_h handler, dynamicbox_ret_cb cb, void *data)
1016 {
1017     struct cb_info *cbinfo;
1018     struct packet *packet;
1019     struct dynamicbox_common *common;
1020     unsigned int cmd = CMD_NEW;
1021     int ret;
1022
1023     common = handler->common;
1024
1025     packet = packet_create((const char *)&cmd, "dssssdii",
1026             common->timestamp, common->pkgname, common->content,
1027             common->cluster, common->category,
1028             common->dbox.period, common->dbox.width, common->dbox.height);
1029     if (!packet) {
1030         ErrPrint("Failed to create a new packet\n");
1031         dynamicbox_set_last_status(DBOX_STATUS_ERROR_FAULT);
1032         return DBOX_STATUS_ERROR_FAULT;
1033     }
1034
1035     cbinfo = dbox_create_cb_info(cb, data);
1036     if (!cbinfo) {
1037         ErrPrint("Failed to create a cbinfo\n");
1038         packet_destroy(packet);
1039         dynamicbox_set_last_status(DBOX_STATUS_ERROR_OUT_OF_MEMORY);
1040         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1041     }
1042
1043     /*!
1044      * \note
1045      * master_rpc_async_request will destroy the packet (decrease the refcnt)
1046      * So be aware the packet object after return from master_rpc_async_request.
1047      */
1048     ret = master_rpc_async_request(handler, packet, 0, new_ret_cb, cbinfo);
1049     if (ret < 0) {
1050         ErrPrint("Failed to send a new packet\n");
1051         dbox_destroy_cb_info(cbinfo);
1052         dynamicbox_set_last_status(DBOX_STATUS_ERROR_FAULT);
1053         return DBOX_STATUS_ERROR_FAULT;
1054     }
1055     handler->common->request.created = 1;
1056     return DBOX_STATUS_ERROR_NONE;
1057 }
1058
1059 static void create_cb(dynamicbox_h handle, int ret, void *data)
1060 {
1061     struct cb_info *cbinfo = data;
1062
1063     if (cbinfo->cb) {
1064         cbinfo->cb(handle, ret, cbinfo->data);
1065     }
1066
1067     dbox_destroy_cb_info(cbinfo);
1068
1069     /*!
1070      * \note
1071      * Forcely generate "updated" event
1072      */
1073     dbox_invoke_event_handler(handle, DBOX_EVENT_DBOX_UPDATED);
1074 }
1075
1076 static int create_fake_instance(dynamicbox_h handler, dynamicbox_ret_cb cb, void *data)
1077 {
1078     struct cb_info *cbinfo;
1079
1080     cbinfo = dbox_create_cb_info(cb, data);
1081     if (!cbinfo) {
1082         ErrPrint("Failed to create a cbinfo\n");
1083         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1084     }
1085
1086     if (job_add(handler, create_cb, DBOX_STATUS_ERROR_NONE, cbinfo) != DBOX_STATUS_ERROR_NONE) {
1087         dbox_destroy_cb_info(cbinfo);
1088     }
1089
1090     return DBOX_STATUS_ERROR_NONE;
1091 }
1092
1093 static void refresh_for_paused_updating_cb(dynamicbox_h handle, int ret, void *data)
1094 {
1095     if (handle->paused_updating == 0) {
1096         DbgPrint("Paused updates are cleared\n");
1097         return;
1098     }
1099
1100     DbgPrint("Pending updates are found\n");
1101     dbox_invoke_event_handler(handle, DBOX_EVENT_DBOX_UPDATED);
1102 }
1103
1104 static int dbox_set_visibility(dynamicbox_h handler, dynamicbox_visible_state_e state)
1105 {
1106     struct packet *packet;
1107     int need_to_add_job = 0;
1108     unsigned int cmd = CMD_CHANGE_VISIBILITY;
1109     int ret;
1110
1111     if (handler->common->visible != DBOX_SHOW && state == DBOX_SHOW) {
1112         need_to_add_job = !!handler->paused_updating;
1113     } else if (handler->common->visible == DBOX_SHOW && state != DBOX_SHOW) {
1114         if (!!dbox_find_dbox_in_show(handler->common)) {
1115             return DBOX_STATUS_ERROR_NONE;
1116         }
1117     } else if (handler->common->visible == DBOX_SHOW && state == DBOX_SHOW && handler->paused_updating) {
1118         if (job_add(handler, refresh_for_paused_updating_cb, DBOX_STATUS_ERROR_NONE, NULL) < 0) {
1119             ErrPrint("Unable to add a new job for refreshing box\n");
1120         }
1121
1122         return DBOX_STATUS_ERROR_NONE;
1123     } else {
1124         /*!
1125          * \brief
1126          * No need to send this to the master
1127          */
1128         return DBOX_STATUS_ERROR_NONE;
1129     }
1130
1131     packet = packet_create_noack((const char *)&cmd, "ssi", handler->common->pkgname, handler->common->id, (int)state);
1132     if (!packet) {
1133         ErrPrint("Failed to create a packet\n");
1134         return DBOX_STATUS_ERROR_FAULT;
1135     }
1136
1137     ret = master_rpc_request_only(handler, packet);
1138     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
1139         DbgPrint("[%s] visibility is changed 0x[%x]\n", handler->common->pkgname, state);
1140         handler->common->visible = state;
1141
1142         if (need_to_add_job) {
1143             if (job_add(handler, refresh_for_paused_updating_cb, DBOX_STATUS_ERROR_NONE, NULL) < 0) {
1144                 ErrPrint("Unable to add a new job for refreshing box\n");
1145             }
1146         }
1147     }
1148
1149     return ret;
1150 }
1151
1152 static void dbox_update_visibility(struct dynamicbox_common *old_common)
1153 {
1154     dynamicbox_h item;
1155
1156     item = dbox_find_dbox_in_show(old_common);
1157     if (!item) {
1158         item = dbox_get_dbox_nth(old_common, 0);
1159         if (item) {
1160             dbox_set_visibility(item, DBOX_HIDE_WITH_PAUSE);
1161         } else {
1162             ErrPrint("Unable to get the valid handle from common handler\n");
1163         }
1164     } else {
1165         dbox_set_visibility(item, DBOX_SHOW);
1166     }
1167 }
1168
1169 /*!
1170  * \note
1171  * The second parameter should be the "return value",
1172  * But in this case, we will use it for "type of deleting instance".
1173  */
1174 static void job_del_cb(dynamicbox_h handle, int type, void *data)
1175 {
1176     struct cb_info *cbinfo = data;
1177     dynamicbox_ret_cb cb;
1178
1179     if (handle->visible == DBOX_SHOW) {
1180         dbox_update_visibility(handle->common);
1181     }
1182
1183     cb = cbinfo->cb;
1184     data = cbinfo->data;
1185     dbox_destroy_cb_info(cbinfo);
1186
1187     if (handle->common->state != DBOX_STATE_CREATE) {
1188         DbgPrint("[%s] %d\n", handle->common->pkgname, handle->refcnt);
1189         if (cb) {
1190             cb(handle, DBOX_STATUS_ERROR_NONE, data);
1191         }
1192
1193         return;
1194     }
1195
1196     if (handle->common->refcnt == 1) {
1197         handle->common->delete_type = type;
1198         handle->common->state = DBOX_STATE_DELETE;
1199
1200         if (!handle->common->id) {
1201             /*!
1202              * \note
1203              * The id is not determined yet.
1204              * It means a user didn't receive created event yet.
1205              * Then just stop to delete procedure from here.
1206              * Because the "created" event handle will release this.
1207              * By the way, if the user adds any callback for getting return status of this,
1208              * call it at here.
1209              */
1210             if (cb) {
1211                 cb(handle, DBOX_STATUS_ERROR_NONE, data);
1212             }
1213         }
1214
1215         DbgPrint("Send delete request\n");
1216         dbox_send_delete(handle, type, cb, data);
1217     } else {
1218         if (cb) {
1219             cb(handle, DBOX_STATUS_ERROR_NONE, data);
1220         }
1221
1222         DbgPrint("Before unref: %d\n", handle->common->refcnt);
1223         dbox_unref(handle, 1);
1224     }
1225 }
1226
1227 static void resize_job_cb(dynamicbox_h handler, int ret, void *data)
1228 {
1229     struct cb_info *info = data;
1230
1231     if (info->cb) {
1232         info->cb(handler, ret, info->data);
1233     }
1234
1235     free(info);
1236
1237     /*!
1238      * \note
1239      * Forcely update the box
1240      */
1241     dbox_invoke_event_handler(handler, DBOX_EVENT_DBOX_UPDATED);
1242 }
1243
1244 static void turn_off_gbar_destroyed_flag_cb(dynamicbox_h handler, int ret, void *data)
1245 {
1246     if (handler->common->request.gbar_destroyed) {
1247         dynamicbox_ret_cb cb;
1248         void *data;
1249
1250         DbgPrint("gbar_destroyed request is canceled\n");
1251         handler->common->request.gbar_destroyed = 0;
1252         cb = handler->cbs.gbar_destroyed.cb;
1253         data = handler->cbs.gbar_destroyed.data;
1254         handler->cbs.gbar_destroyed.cb = NULL;
1255         handler->cbs.gbar_destroyed.data = NULL;
1256
1257         if (cb) {
1258             cb(handler, ret, data);
1259         }
1260     }
1261 }
1262
1263 static void turn_off_gbar_created_flag_cb(dynamicbox_h handler, int ret, void *data)
1264 {
1265     if (handler->common->request.gbar_created) {
1266         dynamicbox_ret_cb cb;
1267         void *data;
1268
1269         DbgPrint("gbar_created request is canceled\n");
1270         handler->common->request.gbar_created = 0;
1271         cb = handler->cbs.gbar_created.cb;
1272         data = handler->cbs.gbar_created.data;
1273         handler->cbs.gbar_created.cb = NULL;
1274         handler->cbs.gbar_created.data = NULL;
1275
1276         if (cb) {
1277             cb(handler, ret, data);
1278         }
1279     }
1280 }
1281
1282 EAPI int dynamicbox_init(void *disp, int prevent_overwrite, double event_filter, int use_thread)
1283 {
1284     if (s_info.init_count > 0) {
1285         s_info.init_count++;
1286         return DBOX_STATUS_ERROR_NONE;
1287     }
1288
1289     /*!
1290      * \note
1291      * Some application doesn't want to use the environment value.
1292      * So set them using arguments.
1293      */
1294     s_info.prevent_overwrite = prevent_overwrite;
1295     conf_set_event_filter(event_filter);
1296
1297     return initialize_dynamicbox(disp, use_thread);
1298 }
1299
1300 EAPI int dynamicbox_fini(void)
1301 {
1302     if (s_info.init_count <= 0) {
1303         ErrPrint("Doesn't initialized\n");
1304         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1305     }
1306
1307     s_info.init_count--;
1308     if (s_info.init_count > 0) {
1309         ErrPrint("init count : %d\n", s_info.init_count);
1310         return DBOX_STATUS_ERROR_NONE;
1311     }
1312
1313     client_fini();
1314     fb_fini();
1315     dynamicbox_service_fini();
1316     return DBOX_STATUS_ERROR_NONE;
1317 }
1318
1319 EAPI dynamicbox_h dynamicbox_add(const char *pkgname, const char *content, const char *cluster, const char *category, double period, dynamicbox_size_type_e type, dynamicbox_ret_cb cb, void *data)
1320 {
1321     char *dboxid;
1322     dynamicbox_h handler;
1323     int w = 0;
1324     int h = 0;
1325
1326     if (!pkgname || !cluster || !category) {
1327         ErrPrint("Invalid arguments: pkgname[%p], cluster[%p], category[%p]\n",
1328                 pkgname, cluster, category);
1329         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
1330         return NULL;
1331     }
1332
1333     dboxid = dbox_pkgname(pkgname);
1334     if (!dboxid) {
1335         ErrPrint("Invalid package: %s\n", pkgname);
1336         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
1337         return NULL;
1338     }
1339
1340     if (dynamicbox_service_is_enabled(dboxid) == 0) {
1341         DbgPrint("Livebox [%s](%s) is disabled package\n", dboxid, pkgname);
1342         free(dboxid);
1343         dynamicbox_set_last_status(DBOX_STATUS_ERROR_DISABLED);
1344         return NULL;
1345     }
1346
1347     if (type != DBOX_SIZE_TYPE_UNKNOWN) {
1348         (void)dynamicbox_service_get_size(type, &w, &h);
1349     }
1350
1351     handler = calloc(1, sizeof(*handler));
1352     if (!handler) {
1353         ErrPrint("Error: %s\n", strerror(errno));
1354         free(dboxid);
1355         dynamicbox_set_last_status(DBOX_STATUS_ERROR_OUT_OF_MEMORY);
1356         return NULL;
1357     }
1358
1359     if (!cb) {
1360         cb = default_create_cb;
1361     }
1362
1363     handler->common = dbox_find_sharable_common_handle(dboxid, content, w, h, cluster, category);
1364     if (!handler->common) {
1365         handler->common = dbox_create_common_handle(handler, dboxid, cluster, category);
1366         free(dboxid);
1367         if (!handler->common) {
1368             ErrPrint("Failed to find common handle\n");
1369             free(handler);
1370             return NULL;
1371         }
1372
1373         if (!content || !strlen(content)) {
1374             char *pc;
1375             /**
1376              * @note
1377              * I know the content should not be modified. use it temporarly without "const"
1378              */
1379             pc = dynamicbox_service_content(handler->common->pkgname);
1380             dbox_set_content(handler->common, pc);
1381             free(pc);
1382         } else {
1383             dbox_set_content(handler->common, content);
1384         }
1385
1386         dbox_set_period(handler->common, period);
1387         dbox_set_size(handler->common, w, h);
1388         dbox_common_ref(handler->common, handler);
1389
1390         if (create_real_instance(handler, cb, data) < 0) {
1391             if (dbox_common_unref(handler->common, handler) == 0) {
1392                 /*!
1393                  * Delete common
1394                  */
1395                 dbox_destroy_common_handle(handler->common);
1396                 handler->common = NULL;
1397             }
1398             free(handler);
1399             return NULL;
1400         }
1401     } else {
1402         free(dboxid);
1403
1404         dbox_common_ref(handler->common, handler);
1405
1406         if (handler->common->request.created) {
1407             /*!
1408              * If a box is in creating, wait its result too
1409              */
1410             handler->cbs.created.cb = cb;
1411             handler->cbs.created.data = data;
1412         } else {
1413             /*!
1414              * or fire the fake created_event
1415              */
1416             if (create_fake_instance(handler, cb, data) < 0) {
1417                 if (dbox_common_unref(handler->common, handler) == 0) {
1418                     /*!
1419                      * Delete common
1420                      */
1421                     dbox_destroy_common_handle(handler->common);
1422                 }
1423                 free(handler);
1424                 return NULL;
1425             }
1426         }
1427     }
1428
1429     handler->visible = DBOX_SHOW;
1430     handler->state = DBOX_STATE_CREATE;
1431     handler = dbox_ref(handler);
1432
1433     if (handler->common->visible != DBOX_SHOW) {
1434         dbox_set_visibility(handler, DBOX_SHOW);
1435     }
1436
1437     return handler;
1438 }
1439
1440 EAPI double dynamicbox_period(dynamicbox_h handler)
1441 {
1442     if (!handler || handler->state != DBOX_STATE_CREATE) {
1443         ErrPrint("Handler is not valid\n");
1444         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
1445         return -1.0f;
1446     }
1447
1448     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1449         ErrPrint("Invalid handle\n");
1450         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
1451         return -1.0f;
1452     }
1453
1454     if (!handler->common->id) {
1455         ErrPrint("Hnalder is not valid\n");
1456         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
1457         return -1.0f;
1458     }
1459
1460     return handler->common->dbox.period;
1461 }
1462
1463 EAPI int dynamicbox_set_period(dynamicbox_h handler, double period, dynamicbox_ret_cb cb, void *data)
1464 {
1465     struct packet *packet;
1466     unsigned int cmd = CMD_SET_PERIOD;
1467     int ret;
1468
1469     if (!handler || handler->state != DBOX_STATE_CREATE) {
1470         ErrPrint("Handler is not valid\n");
1471         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1472     }
1473
1474     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1475         ErrPrint("Invalid handle\n");
1476         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1477     }
1478
1479     if (!handler->common->id) {
1480         ErrPrint("Handler is not valid\n");
1481         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1482     }
1483
1484     if (handler->common->request.period_changed) {
1485         ErrPrint("Previous request for changing period is not finished\n");
1486         return DBOX_STATUS_ERROR_BUSY;
1487     }
1488
1489     if (!handler->common->is_user) {
1490         ErrPrint("CA Livebox is not able to change the period\n");
1491         return DBOX_STATUS_ERROR_PERMISSION_DENIED;
1492     }
1493
1494     if (handler->common->dbox.period == period) {
1495         DbgPrint("No changes\n");
1496         return DBOX_STATUS_ERROR_ALREADY;
1497     }
1498
1499     packet = packet_create((const char *)&cmd, "ssd", handler->common->pkgname, handler->common->id, period);
1500     if (!packet) {
1501         ErrPrint("Failed to build a packet %s\n", handler->common->pkgname);
1502         return DBOX_STATUS_ERROR_FAULT;
1503     }
1504
1505     if (!cb) {
1506         cb = default_period_changed_cb;
1507     }
1508
1509     ret = master_rpc_async_request(handler, packet, 0, period_ret_cb, NULL);
1510     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
1511         handler->cbs.period_changed.cb = cb;
1512         handler->cbs.period_changed.data = data;
1513         handler->common->request.period_changed = 1;
1514     }
1515
1516     return ret;
1517 }
1518
1519 EAPI int dynamicbox_del(dynamicbox_h handler, dynamicbox_delete_type_e type, dynamicbox_ret_cb cb, void *data)
1520 {
1521     struct cb_info *cbinfo;
1522
1523     if (!handler) {
1524         ErrPrint("Handler is NIL\n");
1525         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1526     }
1527
1528     if (handler->state != DBOX_STATE_CREATE) {
1529         ErrPrint("Handler is already deleted\n");
1530         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1531     }
1532
1533     handler->state = DBOX_STATE_DELETE;
1534
1535     cbinfo = dbox_create_cb_info(cb, data);
1536     if (!cbinfo) {
1537         ErrPrint("Failed to create a cbinfo\n");
1538         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1539     }
1540
1541     if (job_add(handler, job_del_cb, type, cbinfo) != DBOX_STATUS_ERROR_NONE) {
1542         ErrPrint("Failed to add a new job\n");
1543         dbox_destroy_cb_info(cbinfo);
1544         return DBOX_STATUS_ERROR_FAULT;
1545     }
1546
1547     return DBOX_STATUS_ERROR_NONE;
1548 }
1549
1550 EAPI int dynamicbox_add_fault_handler(dynamicbox_fault_handler_cb dbox_cb, void *data)
1551 {
1552     if (!dbox_cb) {
1553         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1554     }
1555
1556     return dbox_add_fault_handler(dbox_cb, data);
1557 }
1558
1559 EAPI void *dynamicbox_remove_fault_handler(dynamicbox_fault_handler_cb dbox_cb)
1560 {
1561     if (!dbox_cb) {
1562         return NULL;
1563     }
1564
1565     return dbox_remove_fault_handler(dbox_cb);
1566 }
1567
1568 EAPI int dynamicbox_add_event_handler(dynamicbox_event_handler_cb dbox_cb, void *data)
1569 {
1570     if (!dbox_cb) {
1571         ErrPrint("Invalid argument dbox_cb is nil\n");
1572         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1573     }
1574
1575     return dbox_add_event_handler(dbox_cb, data);
1576 }
1577
1578 EAPI void *dynamicbox_remove_event_handler(dynamicbox_event_handler_cb dbox_cb)
1579 {
1580     if (!dbox_cb) {
1581         return NULL;
1582     }
1583
1584     return dbox_remove_event_handler(dbox_cb);
1585 }
1586
1587 EAPI int dynamicbox_set_update_mode(dynamicbox_h handler, int active_update, dynamicbox_ret_cb cb, void *data)
1588 {
1589     struct packet *packet;
1590     unsigned int cmd = CMD_UPDATE_MODE;
1591     int ret;
1592
1593     if (!handler || handler->state != DBOX_STATE_CREATE) {
1594         ErrPrint("Handler is Invalid\n");
1595         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1596     }
1597
1598     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1599         ErrPrint("Handler is Invalid\n");
1600         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1601     }
1602
1603     if (!handler->common->id) {
1604         ErrPrint("Handler is Invalid\n");
1605         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1606     }
1607
1608     if (handler->common->request.update_mode) {
1609         ErrPrint("Previous update_mode cb is not finished yet\n");
1610         return DBOX_STATUS_ERROR_BUSY;
1611     }
1612
1613     if (handler->common->is_active_update == active_update) {
1614         return DBOX_STATUS_ERROR_ALREADY;
1615     }
1616
1617     if (!handler->common->is_user) {
1618         return DBOX_STATUS_ERROR_PERMISSION_DENIED;
1619     }
1620
1621     packet = packet_create((const char *)&cmd, "ssi", handler->common->pkgname, handler->common->id, active_update);
1622     if (!packet) {
1623         return DBOX_STATUS_ERROR_FAULT;
1624     }
1625
1626     if (!cb) {
1627         cb = default_update_mode_cb;
1628     }
1629
1630     ret = master_rpc_async_request(handler, packet, 0, update_mode_cb, NULL);
1631     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
1632         handler->cbs.update_mode.cb = cb;
1633         handler->cbs.update_mode.data = data;
1634         handler->common->request.update_mode = 1;
1635     }
1636
1637     return ret;
1638 }
1639
1640 EAPI int dynamicbox_is_active_update(dynamicbox_h handler)
1641 {
1642     if (!handler || handler->state != DBOX_STATE_CREATE) {
1643         ErrPrint("Handler is Invalid\n");
1644         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1645     }
1646
1647     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1648         ErrPrint("Handler is Invalid\n");
1649         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1650     }
1651
1652     if (!handler->common->id) {
1653         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1654     }
1655
1656     return handler->common->is_active_update;
1657 }
1658
1659 EAPI int dynamicbox_resize(dynamicbox_h handler, dynamicbox_size_type_e type, dynamicbox_ret_cb cb, void *data)
1660 {
1661     struct dynamicbox_common *common;
1662     int w;
1663     int h;
1664     int ret;
1665
1666     /*!
1667      * \TODO
1668      * If this handle is host instance or link instance,
1669      * Create a new instance or find another linkable instance.
1670      */
1671
1672     if (!handler || handler->state != DBOX_STATE_CREATE) {
1673         ErrPrint("Handler is not valid\n");
1674         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1675     }
1676
1677     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1678         ErrPrint("Invalid handle\n");
1679         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1680     }
1681
1682     if (!handler->common->id) {
1683         ErrPrint("Handler is not valid\n");
1684         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1685     }
1686
1687     /*!
1688      * \note
1689      * resize operation should be separated by each handler.
1690      * If a handler is resizing, the other handler can request resize too.
1691      * So we should not use the common->request.size_changed flag.
1692      */
1693     if (handler->cbs.size_changed.cb) {
1694         ErrPrint("Previous resize request is not finished yet\n");
1695         return DBOX_STATUS_ERROR_BUSY;
1696     }
1697
1698     if (dynamicbox_service_get_size(type, &w, &h) != 0) {
1699         ErrPrint("Invalid size type\n");
1700         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1701     }
1702
1703     if (handler->common->dbox.width == w && handler->common->dbox.height == h) {
1704         DbgPrint("No changes\n");
1705         return DBOX_STATUS_ERROR_ALREADY;
1706     }
1707
1708     if (!handler->common->is_user) {
1709         ErrPrint("CA Livebox is not able to be resized\n");
1710         return DBOX_STATUS_ERROR_PERMISSION_DENIED;
1711     }
1712
1713     if (handler->common->refcnt <= 1) {
1714         struct packet *packet;
1715         unsigned int cmd = CMD_RESIZE;
1716
1717         /* Only 1 instance */
1718         packet = packet_create((const char *)&cmd, "ssii", handler->common->pkgname, handler->common->id, w, h);
1719         if (!packet) {
1720             ErrPrint("Failed to build param\n");
1721             return DBOX_STATUS_ERROR_FAULT;
1722         }
1723
1724         if (!cb) {
1725             cb = default_dbox_size_changed_cb;
1726         }
1727
1728         ret = master_rpc_async_request(handler, packet, 0, resize_cb, NULL);
1729         if (ret == (int)DBOX_STATUS_ERROR_NONE) {
1730             handler->cbs.size_changed.cb = cb;
1731             handler->cbs.size_changed.data = data;
1732             handler->common->request.size_changed = 1;
1733         }
1734     } else {
1735         common = dbox_find_sharable_common_handle(handler->common->pkgname, handler->common->content, w, h, handler->common->cluster, handler->common->category);
1736         if (!common) {
1737             struct dynamicbox_common *old_common;
1738             /*!
1739              * \note
1740              * If the common handler is in resizing,
1741              * if user tries to resize a hander, then simply create new one even if the requested size is same with this.
1742
1743              if (handler->common->request.size_changed) {
1744              }
1745
1746              */
1747
1748             old_common = handler->common;
1749
1750             common = dbox_create_common_handle(handler, old_common->pkgname, old_common->cluster, old_common->category);
1751             if (!common) {
1752                 ErrPrint("Failed to create common handle\n");
1753                 return DBOX_STATUS_ERROR_FAULT;
1754             }
1755
1756             dbox_set_size(common, w, h);
1757             dbox_set_content(common, old_common->content);
1758             dbox_set_period(common, old_common->dbox.period);
1759
1760             /*!
1761              * \note
1762              * Disconnecting from old one.
1763              */
1764             if (dbox_common_unref(old_common, handler) == 0) {
1765                 /*!
1766                  * \note
1767                  * Impossible
1768                  */
1769                 ErrPrint("Common has no associated handler\n");
1770             }
1771
1772             dbox_common_ref(common, handler);
1773
1774             /*!
1775              * Connect to a new one
1776              */
1777             handler->common = common;
1778
1779             /*!
1780              * \TODO
1781              * Need to care, if it fails to create a common handle,
1782              * the resize operation will be failed.
1783              * in that case, we should reuse the old common handle
1784              */
1785             ret = create_real_instance(handler, cb, data);
1786             if (ret < 0) {
1787                 dbox_common_unref(common, handler);
1788                 dbox_destroy_common_handle(common);
1789
1790                 dbox_common_ref(old_common, handler);
1791                 handler->common = old_common;
1792             } else {
1793                 /*!
1794                  * In this case, we should update visibility of old_common's dynamicboxes
1795                  */
1796                 if (handler->visible == DBOX_SHOW) {
1797                     dbox_update_visibility(old_common);
1798                 }
1799             }
1800         } else {
1801             struct cb_info *cbinfo;
1802
1803             cbinfo = dbox_create_cb_info(cb, data);
1804             if (!cbinfo) {
1805                 ErrPrint("Failed to create a cbinfo\n");
1806                 ret = DBOX_STATUS_ERROR_OUT_OF_MEMORY;
1807             } else {
1808                 ret = job_add(handler, resize_job_cb, DBOX_STATUS_ERROR_NONE, cbinfo);
1809                 if (ret == (int)DBOX_STATUS_ERROR_NONE) {
1810                     struct dynamicbox_common *old_common;
1811
1812                     old_common = handler->common;
1813
1814                     if (dbox_common_unref(handler->common, handler) == 0) {
1815                         ErrPrint("Old common has no associated handler\n");
1816                     }
1817
1818                     dbox_common_ref(common, handler);
1819                     handler->common = common;
1820
1821                     if (handler->visible == DBOX_SHOW) {
1822                         dbox_update_visibility(old_common); /* To update visibility: Show --> Paused */
1823                         dbox_update_visibility(common);    /* To update visibility: Paused --> Show */
1824                     }
1825                 } else {
1826                     dbox_destroy_cb_info(cbinfo);
1827                 }
1828             }
1829         }
1830     }
1831
1832     return ret;
1833 }
1834
1835 EAPI int dynamicbox_click(dynamicbox_h handler, double x, double y)
1836 {
1837     struct packet *packet;
1838     double timestamp;
1839     unsigned int cmd = CMD_CLICKED;
1840     int ret;
1841
1842     if (!handler || handler->state != DBOX_STATE_CREATE) {
1843         ErrPrint("Handler is invalid\n");
1844         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1845     }
1846
1847     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1848         ErrPrint("Handler is invalid\n");
1849         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1850     }
1851
1852     if (!handler->common->id) {
1853         ErrPrint("Handler is not valid\n");
1854         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1855     }
1856
1857     if (handler->common->dbox.auto_launch) {
1858         if (s_info.launch.handler) {
1859             ret = s_info.launch.handler(handler, handler->common->dbox.auto_launch, s_info.launch.data);
1860             if (ret < 0) {
1861                 ErrPrint("launch handler app %s (%d)\n", handler->common->dbox.auto_launch, ret);
1862             }
1863         }
1864     }
1865
1866     timestamp = util_timestamp();
1867     DbgPrint("CLICKED: %lf\n", timestamp);
1868
1869     packet = packet_create_noack((const char *)&cmd, "sssddd", handler->common->pkgname, handler->common->id, "clicked", timestamp, x, y);
1870     if (!packet) {
1871         ErrPrint("Failed to build param\n");
1872         return DBOX_STATUS_ERROR_FAULT;
1873     }
1874
1875     ret = master_rpc_request_only(handler, packet);
1876     return ret;
1877 }
1878
1879 EAPI int dynamicbox_has_glance_bar(dynamicbox_h handler)
1880 {
1881     if (!handler || handler->state != DBOX_STATE_CREATE) {
1882         ErrPrint("Handler is invalid\n");
1883         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1884     }
1885
1886     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1887         ErrPrint("Handler is invalid\n");
1888         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1889     }
1890
1891     if (!handler->common->id) {
1892         ErrPrint("Handler is not valid\n");
1893         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1894     }
1895
1896     return !!handler->common->gbar.fb;
1897 }
1898
1899 EAPI int dynamicbox_glance_bar_is_created(dynamicbox_h handler)
1900 {
1901     if (!handler || handler->state != DBOX_STATE_CREATE) {
1902         ErrPrint("Handler is invalid\n");
1903         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1904     }
1905
1906     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1907         ErrPrint("Handler is invalid\n");
1908         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1909     }
1910
1911     if (!handler->common->gbar.fb || !handler->common->id) {
1912         ErrPrint("Handler is not valid\n");
1913         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1914     }
1915
1916     return handler->common->is_gbar_created;
1917 }
1918
1919 EAPI int dynamicbox_create_glance_bar(dynamicbox_h handler, double x, double y, dynamicbox_ret_cb cb, void *data)
1920 {
1921     struct packet *packet;
1922     unsigned int cmd = CMD_CREATE_GBAR;
1923     int ret;
1924
1925     if (!handler || handler->state != DBOX_STATE_CREATE) {
1926         ErrPrint("Handler is invalid\n");
1927         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1928     }
1929
1930     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1931         ErrPrint("Handler is invalid\n");
1932         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1933     }
1934
1935     if (!handler->common->gbar.fb || !handler->common->id) {
1936         ErrPrint("Handler is not valid\n");
1937         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1938     }
1939
1940     /*!
1941      * \note
1942      * Only one handler can have a GBAR
1943      */
1944     if (handler->common->is_gbar_created) {
1945         DbgPrint("GBAR is already created\n");
1946         return DBOX_STATUS_ERROR_NONE;
1947     }
1948
1949     if (handler->common->request.gbar_created) {
1950         ErrPrint("Previous request is not completed yet\n");
1951         return DBOX_STATUS_ERROR_BUSY;
1952     }
1953
1954     /*!
1955      * \note
1956      * Turn off the gbar_destroyed request flag
1957      */
1958     if (handler->common->request.gbar_destroyed) {
1959         if (job_add(handler, turn_off_gbar_destroyed_flag_cb, DBOX_STATUS_ERROR_CANCEL, NULL) < 0) {
1960             ErrPrint("Failed to add gbar_destroyed job\n");
1961         }
1962     }
1963
1964     packet = packet_create((const char *)&cmd, "ssdd", handler->common->pkgname, handler->common->id, x, y);
1965     if (!packet) {
1966         ErrPrint("Failed to build param\n");
1967         return DBOX_STATUS_ERROR_FAULT;
1968     }
1969
1970     if (!cb) {
1971         cb = default_gbar_created_cb;
1972     }
1973
1974     DbgPrint("PERF_DBOX\n");
1975     ret = master_rpc_async_request(handler, packet, 0, gbar_create_cb, NULL);
1976     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
1977         handler->cbs.gbar_created.cb = cb;
1978         handler->cbs.gbar_created.data = data;
1979         handler->common->request.gbar_created = 1;
1980     }
1981
1982     return ret;
1983 }
1984
1985 EAPI int dynamicbox_move_glance_bar(dynamicbox_h handler, double x, double y)
1986 {
1987     struct packet *packet;
1988     unsigned int cmd = CMD_GBAR_MOVE;
1989
1990     if (!handler || handler->state != DBOX_STATE_CREATE) {
1991         ErrPrint("Handler is invalid\n");
1992         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1993     }
1994
1995     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
1996         ErrPrint("Handler is invalid\n");
1997         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
1998     }
1999
2000     if (!handler->common->gbar.fb || !handler->common->id) {
2001         ErrPrint("Handler is not valid\n");
2002         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2003     }
2004
2005     if (!handler->common->is_gbar_created) {
2006         ErrPrint("GBAR is not created\n");
2007         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2008     }
2009
2010     packet = packet_create_noack((const char *)&cmd, "ssdd", handler->common->pkgname, handler->common->id, x, y);
2011     if (!packet) {
2012         ErrPrint("Failed to build param\n");
2013         return DBOX_STATUS_ERROR_FAULT;
2014     }
2015
2016     return master_rpc_request_only(handler, packet);
2017 }
2018
2019 EAPI int dynamicbox_activate(const char *pkgname, dynamicbox_ret_cb cb, void *data)
2020 {
2021     struct packet *packet;
2022     struct cb_info *cbinfo;
2023     unsigned int cmd = CMD_ACTIVATE_PACKAGE;
2024     int ret;
2025
2026     if (!pkgname) {
2027         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2028     }
2029
2030     packet = packet_create((const char *)&cmd, "s", pkgname);
2031     if (!packet) {
2032         ErrPrint("Failed to build a param\n");
2033         return DBOX_STATUS_ERROR_FAULT;
2034     }
2035
2036     cbinfo = dbox_create_cb_info(cb, data);
2037     if (!cbinfo) {
2038         ErrPrint("Unable to create cbinfo\n");
2039         packet_destroy(packet);
2040         return DBOX_STATUS_ERROR_FAULT;
2041     }
2042
2043     ret = master_rpc_async_request(NULL, packet, 0, activated_cb, cbinfo);
2044     if (ret < 0) {
2045         dbox_destroy_cb_info(cbinfo);
2046     }
2047
2048     return ret;
2049 }
2050
2051 EAPI int dynamicbox_destroy_glance_bar(dynamicbox_h handler, dynamicbox_ret_cb cb, void *data)
2052 {
2053     struct packet *packet;
2054     struct cb_info *cbinfo;
2055     unsigned int cmd = CMD_DESTROY_GBAR;
2056     int ret;
2057
2058     if (!handler || handler->state != DBOX_STATE_CREATE) {
2059         ErrPrint("Handler is invalid\n");
2060         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2061     }
2062
2063     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2064         ErrPrint("Handler is invalid\n");
2065         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2066     }
2067
2068     if (!handler->common->gbar.fb || !handler->common->id) {
2069         ErrPrint("Handler is not valid\n");
2070         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2071     }
2072
2073     /*!
2074      * \FIXME
2075      * Replace the callback check code.
2076      * Use the flag instead of callback.
2077      * the flag should be in the ADT "common"
2078      */
2079     if (!handler->common->is_gbar_created && !handler->common->request.gbar_created) {
2080         ErrPrint("GBAR is not created\n");
2081         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2082     }
2083
2084     if (handler->common->request.gbar_destroyed) {
2085         ErrPrint("GBAR destroy request is already sent\n");
2086         return DBOX_STATUS_ERROR_ALREADY;
2087     }
2088
2089     /*!
2090      * \note
2091      * Disable the gbar_created request flag
2092      */
2093     if (handler->common->request.gbar_created) {
2094         if (job_add(handler, turn_off_gbar_created_flag_cb, DBOX_STATUS_ERROR_CANCEL, NULL) < 0) {
2095             ErrPrint("Failed to add a new job\n");
2096         }
2097     }
2098
2099     DbgPrint("[%s]\n", handler->common->pkgname);
2100
2101     packet = packet_create((const char *)&cmd, "ss", handler->common->pkgname, handler->common->id);
2102     if (!packet) {
2103         ErrPrint("Failed to build a param\n");
2104         return DBOX_STATUS_ERROR_FAULT;
2105     }
2106
2107     if (!cb) {
2108         cb = default_gbar_destroyed_cb;
2109     }
2110
2111     cbinfo = dbox_create_cb_info(cb, data);
2112     if (!cbinfo) {
2113         packet_destroy(packet);
2114         return DBOX_STATUS_ERROR_FAULT;
2115     }
2116
2117     ret = master_rpc_async_request(handler, packet, 0, gbar_destroy_cb, cbinfo);
2118     if (ret < 0) {
2119         dbox_destroy_cb_info(cbinfo);
2120     } else {
2121         handler->common->request.gbar_destroyed = 1;
2122     }
2123
2124     return ret;
2125 }
2126
2127 EAPI int dynamicbox_feed_access_event(dynamicbox_h handler, dynamicbox_access_event_type_e type, dynamicbox_access_event_info_t info, dynamicbox_ret_cb cb, void *data)
2128 {
2129     int w = 1;
2130     int h = 1;
2131     unsigned int cmd;
2132     int ret = 0;    /* re-used for sending event type */
2133
2134     if (!handler || handler->state != DBOX_STATE_CREATE) {
2135         ErrPrint("Handler is invalid\n");
2136         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2137     }
2138
2139     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2140         ErrPrint("Handler is invalid\n");
2141         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2142     }
2143
2144     if (!handler->common->id) {
2145         ErrPrint("Handler is not valid\n");
2146         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2147     }
2148
2149     if (handler->common->request.access_event) {
2150         ErrPrint("Previous access event is not yet done\n");
2151         return DBOX_STATUS_ERROR_BUSY;
2152     }
2153
2154     if (type & DBOX_ACCESS_EVENT_GBAR_MASK) {
2155         if (!handler->common->is_gbar_created) {
2156             ErrPrint("GBAR is not created\n");
2157             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2158         }
2159
2160         w = handler->common->gbar.width;
2161         h = handler->common->gbar.height;
2162
2163         switch (type & ~(DBOX_ACCESS_EVENT_GBAR_MASK | DBOX_ACCESS_EVENT_DBOX_MASK)) {
2164             case DBOX_ACCESS_EVENT_HIGHLIGHT:
2165                 cmd = CMD_GBAR_ACCESS_HL;
2166                 ret = (int)info->type;
2167                 break;
2168             case DBOX_ACCESS_EVENT_ACTIVATE:
2169                 cmd = CMD_GBAR_ACCESS_ACTIVATE;
2170                 break;
2171             case DBOX_ACCESS_EVENT_ACTION:
2172                 cmd = CMD_GBAR_ACCESS_ACTION;
2173                 ret = (int)info->type;
2174                 break;
2175             case DBOX_ACCESS_EVENT_SCROLL:
2176                 cmd = CMD_GBAR_ACCESS_SCROLL;
2177                 ret = (int)info->type;
2178                 break;
2179             case DBOX_ACCESS_EVENT_VALUE_CHANGE:
2180                 cmd = CMD_GBAR_ACCESS_VALUE_CHANGE;
2181                 break;
2182             case DBOX_ACCESS_EVENT_MOUSE:
2183                 cmd = CMD_GBAR_ACCESS_MOUSE;
2184                 ret = (int)info->type;
2185                 break;
2186             case DBOX_ACCESS_EVENT_BACK:
2187                 cmd = CMD_GBAR_ACCESS_BACK;
2188                 break;
2189             case DBOX_ACCESS_EVENT_OVER:
2190                 cmd = CMD_GBAR_ACCESS_OVER;
2191                 break;
2192             case DBOX_ACCESS_EVENT_READ:
2193                 cmd = CMD_GBAR_ACCESS_READ;
2194                 break;
2195             case DBOX_ACCESS_EVENT_ENABLE:
2196                 cmd = CMD_GBAR_ACCESS_ENABLE;
2197                 ret = info->type;
2198                 break;
2199             default:
2200                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2201         }
2202
2203     } else if (type & DBOX_ACCESS_EVENT_DBOX_MASK) {
2204         w = handler->common->dbox.width;
2205         h = handler->common->dbox.height;
2206         switch (type & ~(DBOX_ACCESS_EVENT_GBAR_MASK | DBOX_ACCESS_EVENT_DBOX_MASK)) {
2207             case DBOX_ACCESS_EVENT_HIGHLIGHT:
2208                 cmd = CMD_DBOX_ACCESS_HL;
2209                 ret = (int)info->type;
2210                 break;
2211             case DBOX_ACCESS_EVENT_ACTIVATE:
2212                 cmd = CMD_DBOX_ACCESS_ACTIVATE;
2213                 break;
2214             case DBOX_ACCESS_EVENT_ACTION:
2215                 cmd = CMD_DBOX_ACCESS_ACTION;
2216                 ret = (int)info->type;
2217                 break;
2218             case DBOX_ACCESS_EVENT_SCROLL:
2219                 cmd = CMD_DBOX_ACCESS_SCROLL;
2220                 ret = (int)info->type;
2221                 break;
2222             case DBOX_ACCESS_EVENT_VALUE_CHANGE:
2223                 cmd = CMD_DBOX_ACCESS_VALUE_CHANGE;
2224                 break;
2225             case DBOX_ACCESS_EVENT_MOUSE:
2226                 cmd = CMD_DBOX_ACCESS_MOUSE;
2227                 ret = (int)info->type;
2228                 break;
2229             case DBOX_ACCESS_EVENT_BACK:
2230                 cmd = CMD_DBOX_ACCESS_BACK;
2231                 break;
2232             case DBOX_ACCESS_EVENT_OVER:
2233                 cmd = CMD_DBOX_ACCESS_OVER;
2234                 break;
2235             case DBOX_ACCESS_EVENT_READ:
2236                 cmd = CMD_DBOX_ACCESS_READ;
2237                 break;
2238             case DBOX_ACCESS_EVENT_ENABLE:
2239                 cmd = CMD_DBOX_ACCESS_ENABLE;
2240                 ret = info->type;
2241                 break;
2242             default:
2243                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2244         }
2245     } else {
2246         ErrPrint("Invalid event type\n");
2247         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2248     }
2249
2250     if (!cb) {
2251         cb = default_access_event_cb;
2252     }
2253
2254     ret = send_access_event(handler, (const char *)&cmd, info->x * w, info->y * h, ret);
2255     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
2256         handler->cbs.access_event.cb = cb;
2257         handler->cbs.access_event.data = data;
2258         handler->common->request.access_event = 1;
2259     }
2260
2261     return ret;
2262 }
2263
2264 EAPI int dynamicbox_feed_mouse_event(dynamicbox_h handler, dynamicbox_mouse_event_type_e type, dynamicbox_mouse_event_info_t info)
2265 {
2266     int w = 1;
2267     int h = 1;
2268     unsigned int cmd;
2269
2270     if (!handler || handler->state != DBOX_STATE_CREATE) {
2271         ErrPrint("Handler is invalid\n");
2272         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2273     }
2274
2275     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2276         ErrPrint("Handler is invalid\n");
2277         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2278     }
2279
2280     if (!handler->common->id) {
2281         ErrPrint("Handler is not valid\n");
2282         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2283     }
2284
2285     if (!(type & DBOX_MOUSE_EVENT_MASK)) {
2286         ErrPrint("Invalid content event is used\n");
2287         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2288     }
2289
2290     if (type & DBOX_MOUSE_EVENT_GBAR_MASK) {
2291         int flag = 1;
2292
2293         if (!handler->common->is_gbar_created) {
2294             ErrPrint("GBAR is not created\n");
2295             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2296         }
2297
2298         if (!handler->common->gbar.fb) {
2299             ErrPrint("Handler is not valid\n");
2300             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2301         }
2302
2303         if (type & DBOX_MOUSE_EVENT_MOVE) {
2304             if (fabs(info->x - handler->common->gbar.x) < conf_event_filter() && fabs(info->y - handler->common->gbar.y) < conf_event_filter()) {
2305                 return DBOX_STATUS_ERROR_BUSY;
2306             }
2307         } else if (type & DBOX_MOUSE_EVENT_SET) {
2308             flag = 0;
2309         }
2310
2311         if (flag) {
2312             handler->common->gbar.x = info->x;
2313             handler->common->gbar.y = info->y;
2314             w = handler->common->gbar.width;
2315             h = handler->common->gbar.height;
2316         }
2317
2318         switch ((type & ~(DBOX_MOUSE_EVENT_GBAR_MASK | DBOX_MOUSE_EVENT_DBOX_MASK))) {
2319             case DBOX_MOUSE_EVENT_ENTER | DBOX_MOUSE_EVENT_MASK:
2320                 cmd = CMD_GBAR_MOUSE_ENTER;
2321                 break;
2322             case DBOX_MOUSE_EVENT_LEAVE | DBOX_MOUSE_EVENT_MASK:
2323                 cmd = CMD_GBAR_MOUSE_LEAVE;
2324                 break;
2325             case DBOX_MOUSE_EVENT_UP | DBOX_MOUSE_EVENT_MASK:
2326                 cmd = CMD_GBAR_MOUSE_UP;
2327                 break;
2328             case DBOX_MOUSE_EVENT_DOWN | DBOX_MOUSE_EVENT_MASK:
2329                 cmd = CMD_GBAR_MOUSE_DOWN;
2330                 break;
2331             case DBOX_MOUSE_EVENT_MOVE | DBOX_MOUSE_EVENT_MASK:
2332                 cmd = CMD_GBAR_MOUSE_MOVE;
2333                 break;
2334             case DBOX_MOUSE_EVENT_SET | DBOX_MOUSE_EVENT_MASK:
2335                 cmd = CMD_GBAR_MOUSE_SET;
2336                 break;
2337             case DBOX_MOUSE_EVENT_UNSET | DBOX_MOUSE_EVENT_MASK:
2338                 cmd = CMD_GBAR_MOUSE_UNSET;
2339                 break;
2340             case DBOX_MOUSE_EVENT_ON_SCROLL | DBOX_MOUSE_EVENT_MASK:
2341                 cmd = CMD_GBAR_MOUSE_ON_SCROLL;
2342                 break;
2343             case DBOX_MOUSE_EVENT_ON_HOLD | DBOX_MOUSE_EVENT_MASK:
2344                 cmd = CMD_GBAR_MOUSE_ON_HOLD;
2345                 break;
2346             case DBOX_MOUSE_EVENT_OFF_SCROLL | DBOX_MOUSE_EVENT_MASK:
2347                 cmd = CMD_GBAR_MOUSE_OFF_SCROLL;
2348                 break;
2349             case DBOX_MOUSE_EVENT_OFF_HOLD | DBOX_MOUSE_EVENT_MASK:
2350                 cmd = CMD_GBAR_MOUSE_OFF_HOLD;
2351                 break;
2352             default:
2353                 ErrPrint("Invalid event type\n");
2354                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2355         }
2356
2357     } else if (type & DBOX_MOUSE_EVENT_DBOX_MASK) {
2358         int flag = 1;
2359
2360         if (!handler->common->dbox.fb) {
2361             ErrPrint("Handler is not valid\n");
2362             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2363         }
2364
2365         if (type & DBOX_MOUSE_EVENT_MOVE) {
2366             if (fabs(info->x - handler->common->dbox.x) < conf_event_filter() && fabs(info->y - handler->common->dbox.y) < conf_event_filter()) {
2367                 return DBOX_STATUS_ERROR_BUSY;
2368             }
2369         } else if (type & DBOX_MOUSE_EVENT_SET) {
2370             flag = 0;
2371         }
2372
2373         if (flag) {
2374             handler->common->dbox.x = info->x;
2375             handler->common->dbox.y = info->y;
2376             w = handler->common->dbox.width;
2377             h = handler->common->dbox.height;
2378         }
2379
2380         switch ((type & ~(DBOX_MOUSE_EVENT_GBAR_MASK | DBOX_MOUSE_EVENT_DBOX_MASK))) {
2381             case DBOX_MOUSE_EVENT_ENTER | DBOX_MOUSE_EVENT_MASK:
2382                 cmd = CMD_DBOX_MOUSE_ENTER;
2383                 break;
2384             case DBOX_MOUSE_EVENT_LEAVE | DBOX_MOUSE_EVENT_MASK:
2385                 cmd = CMD_DBOX_MOUSE_LEAVE;
2386                 break;
2387             case DBOX_MOUSE_EVENT_UP | DBOX_MOUSE_EVENT_MASK:
2388                 cmd = CMD_DBOX_MOUSE_UP;
2389                 break;
2390             case DBOX_MOUSE_EVENT_DOWN | DBOX_MOUSE_EVENT_MASK:
2391                 cmd = CMD_DBOX_MOUSE_DOWN;
2392                 break;
2393             case DBOX_MOUSE_EVENT_MOVE | DBOX_MOUSE_EVENT_MASK:
2394                 if (!handler->common->dbox.mouse_event) {
2395                     return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2396                 }
2397                 cmd = CMD_DBOX_MOUSE_MOVE;
2398                 break;
2399             case DBOX_MOUSE_EVENT_SET | DBOX_MOUSE_EVENT_MASK:
2400                 if (!handler->common->dbox.mouse_event) {
2401                     return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2402                 }
2403                 cmd = CMD_DBOX_MOUSE_SET;
2404                 break;
2405             case DBOX_MOUSE_EVENT_UNSET | DBOX_MOUSE_EVENT_MASK:
2406                 if (!handler->common->dbox.mouse_event) {
2407                     return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2408                 }
2409                 cmd = CMD_DBOX_MOUSE_UNSET;
2410                 break;
2411             case DBOX_MOUSE_EVENT_ON_SCROLL | DBOX_MOUSE_EVENT_MASK:
2412                 cmd = CMD_DBOX_MOUSE_ON_SCROLL;
2413                 break;
2414             case DBOX_MOUSE_EVENT_ON_HOLD | DBOX_MOUSE_EVENT_MASK:
2415                 cmd = CMD_DBOX_MOUSE_ON_HOLD;
2416                 break;
2417             case DBOX_MOUSE_EVENT_OFF_SCROLL | DBOX_MOUSE_EVENT_MASK:
2418                 cmd = CMD_DBOX_MOUSE_OFF_SCROLL;
2419                 break;
2420             case DBOX_MOUSE_EVENT_OFF_HOLD | DBOX_MOUSE_EVENT_MASK:
2421                 cmd = CMD_DBOX_MOUSE_OFF_HOLD;
2422                 break;
2423             default:
2424                 ErrPrint("Invalid event type\n");
2425                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2426         }
2427     } else {
2428         ErrPrint("Invalid event type\n");
2429         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2430     }
2431
2432     return send_mouse_event(handler, (const char *)&cmd, info->x * w, info->y * h);
2433 }
2434
2435 EAPI int dynamicbox_feed_key_event(dynamicbox_h handler, dynamicbox_key_event_type_e type, dynamicbox_key_event_info_t info, dynamicbox_ret_cb cb, void *data)
2436 {
2437     int ret;
2438     unsigned int cmd;
2439
2440     if (!handler || handler->state != DBOX_STATE_CREATE) {
2441         ErrPrint("Handler is invalid\n");
2442         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2443     }
2444
2445     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2446         ErrPrint("Handler is invalid\n");
2447         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2448     }
2449
2450     if (!handler->common->id) {
2451         ErrPrint("Handler is not valid\n");
2452         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2453     }
2454
2455     if (!(type & DBOX_KEY_EVENT_MASK)) {
2456         ErrPrint("Invalid key event is used\n");
2457         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2458     }
2459
2460     if (handler->common->request.key_event) {
2461         ErrPrint("Previous key event is not completed yet\n");
2462         return DBOX_STATUS_ERROR_BUSY;
2463     }
2464
2465     if (type & DBOX_MOUSE_EVENT_GBAR_MASK) {
2466         if (!handler->common->is_gbar_created) {
2467             ErrPrint("GBAR is not created\n");
2468             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2469         }
2470
2471         if (!handler->common->gbar.fb) {
2472             ErrPrint("Handler is not valid\n");
2473             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2474         }
2475
2476         if (type & DBOX_KEY_EVENT_DOWN) {
2477             /*!
2478              * \TODO
2479              * filtering the reproduced events if it is too fast
2480              */
2481         } else if (type & DBOX_KEY_EVENT_SET) {
2482             /*!
2483              * \TODO
2484              * What can I do for this case?
2485              */
2486         }
2487
2488         /*!
2489          * Must be short than 29 bytes.
2490          */
2491         switch ((type & ~(DBOX_MOUSE_EVENT_GBAR_MASK | DBOX_MOUSE_EVENT_DBOX_MASK))) {
2492             case DBOX_KEY_EVENT_FOCUS_IN | DBOX_KEY_EVENT_MASK:
2493                 cmd = CMD_GBAR_KEY_FOCUS_IN;
2494                 break;
2495             case DBOX_KEY_EVENT_FOCUS_OUT | DBOX_KEY_EVENT_MASK:
2496                 cmd = CMD_GBAR_KEY_FOCUS_OUT;
2497                 break;
2498             case DBOX_KEY_EVENT_UP | DBOX_KEY_EVENT_MASK:
2499                 cmd = CMD_GBAR_KEY_UP;
2500                 break;
2501             case DBOX_KEY_EVENT_DOWN | DBOX_KEY_EVENT_MASK:
2502                 cmd = CMD_GBAR_KEY_DOWN;
2503                 break;
2504             case DBOX_KEY_EVENT_SET | DBOX_KEY_EVENT_MASK:
2505                 cmd = CMD_GBAR_KEY_SET;
2506                 break;
2507             case DBOX_KEY_EVENT_UNSET | DBOX_KEY_EVENT_MASK:
2508                 cmd = CMD_GBAR_KEY_UNSET;
2509                 break;
2510             default:
2511                 ErrPrint("Invalid event type\n");
2512                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2513         }
2514
2515     } else if (type & DBOX_MOUSE_EVENT_DBOX_MASK) {
2516         if (!handler->common->dbox.fb) {
2517             ErrPrint("Handler is not valid\n");
2518             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2519         }
2520
2521         if (type & DBOX_KEY_EVENT_DOWN) {
2522             /*!
2523              * \TODO
2524              * filtering the reproduced events if it is too fast
2525              */
2526         } else if (type & DBOX_KEY_EVENT_SET) {
2527             /*!
2528              * What can I do for this case?
2529              */
2530         }
2531
2532         switch ((type & ~(DBOX_MOUSE_EVENT_GBAR_MASK | DBOX_MOUSE_EVENT_DBOX_MASK))) {
2533             case DBOX_KEY_EVENT_FOCUS_IN | DBOX_KEY_EVENT_MASK:
2534                 cmd = CMD_DBOX_KEY_FOCUS_IN;
2535                 break;
2536             case DBOX_KEY_EVENT_FOCUS_OUT | DBOX_KEY_EVENT_MASK:
2537                 cmd = CMD_DBOX_KEY_FOCUS_OUT;
2538                 break;
2539             case DBOX_KEY_EVENT_UP | DBOX_KEY_EVENT_MASK:
2540                 cmd = CMD_DBOX_KEY_UP;
2541                 break;
2542             case DBOX_KEY_EVENT_DOWN | DBOX_KEY_EVENT_MASK:
2543                 cmd = CMD_DBOX_KEY_DOWN;
2544                 break;
2545             case DBOX_KEY_EVENT_SET | DBOX_KEY_EVENT_MASK:
2546                 cmd = CMD_DBOX_KEY_SET;
2547                 break;
2548             case DBOX_KEY_EVENT_UNSET | DBOX_KEY_EVENT_MASK:
2549                 cmd = CMD_DBOX_KEY_UNSET;
2550                 break;
2551             default:
2552                 ErrPrint("Invalid event type\n");
2553                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2554         }
2555     } else {
2556         ErrPrint("Invalid event type\n");
2557         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2558     }
2559
2560     if (!cb) {
2561         cb = default_key_event_cb;
2562     }
2563
2564     ret = send_key_event(handler, (const char *)&cmd, info->keycode);
2565     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
2566         handler->cbs.key_event.cb = cb;
2567         handler->cbs.key_event.data = data;
2568         handler->common->request.key_event = 1;
2569     }
2570
2571     return ret;
2572 }
2573
2574 EAPI const char *dynamicbox_filename(dynamicbox_h handler)
2575 {
2576     if (!handler || handler->state != DBOX_STATE_CREATE) {
2577         ErrPrint("Handler is invalid\n");
2578         return NULL;
2579     }
2580
2581     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2582         ErrPrint("Handler is invalid\n");
2583         return NULL;
2584     }
2585
2586     if (!handler->common->id) {
2587         ErrPrint("Handler is not valid\n");
2588         return NULL;
2589     }
2590
2591     if (handler->common->filename) {
2592         return handler->common->filename;
2593     }
2594
2595     /* Oooops */
2596     dynamicbox_set_last_status(DBOX_STATUS_ERROR_NONE);
2597     return util_uri_to_path(handler->common->id);
2598 }
2599
2600 EAPI int dynamicbox_get_glance_bar_size(dynamicbox_h handler, int *w, int *h)
2601 {
2602     int _w;
2603     int _h;
2604
2605     if (!handler || handler->state != DBOX_STATE_CREATE) {
2606         ErrPrint("Handler is invalid\n");
2607         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2608     }
2609
2610     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2611         ErrPrint("Handler is invalid\n");
2612         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2613     }
2614
2615     if (!handler->common->id) {
2616         ErrPrint("Handler is not valid\n");
2617         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2618     }
2619
2620     if (!w) {
2621         w = &_w;
2622     }
2623     if (!h) {
2624         h = &_h;
2625     }
2626
2627     if (!handler->common->is_gbar_created) {
2628         *w = handler->common->gbar.default_width;
2629         *h = handler->common->gbar.default_height;
2630     } else {
2631         *w = handler->common->gbar.width;
2632         *h = handler->common->gbar.height;
2633     }
2634
2635     return DBOX_STATUS_ERROR_NONE;
2636 }
2637
2638 EAPI dynamicbox_size_type_e dynamicbox_size(dynamicbox_h handler)
2639 {
2640     int w;
2641     int h;
2642
2643     if (!handler || handler->state != DBOX_STATE_CREATE) {
2644         ErrPrint("Handler is invalid\n");
2645         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2646         return DBOX_SIZE_TYPE_UNKNOWN;
2647     }
2648
2649     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2650         ErrPrint("Handler is invalid\n");
2651         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2652         return DBOX_SIZE_TYPE_UNKNOWN;
2653     }
2654
2655     if (!handler->common->id) {
2656         ErrPrint("Handler is not valid\n");
2657         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2658         return DBOX_SIZE_TYPE_UNKNOWN;
2659     }
2660
2661     w = handler->common->dbox.width;
2662     h = handler->common->dbox.height;
2663
2664     switch (handler->common->dbox.type) {
2665         case DBOX_TYPE_BUFFER:
2666         case DBOX_TYPE_SCRIPT:
2667             if (!fb_is_created(handler->common->dbox.fb)) {
2668                 w = 0;
2669                 h = 0;
2670                 dynamicbox_set_last_status(DBOX_STATUS_ERROR_NOT_EXIST);
2671             }
2672             break;
2673         default:
2674             break;
2675     }
2676
2677     return dynamicbox_service_size_type(w, h);
2678 }
2679
2680 EAPI int dynamicbox_set_group(dynamicbox_h handler, const char *cluster, const char *category, dynamicbox_ret_cb cb, void *data)
2681 {
2682     struct packet *packet;
2683     unsigned int cmd = CMD_CHANGE_GROUP;
2684     int ret;
2685
2686     if (!handler) {
2687         ErrPrint("Handler is NIL\n");
2688         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2689     }
2690
2691     if (!cluster || !category || handler->state != DBOX_STATE_CREATE) {
2692         ErrPrint("Invalid argument\n");
2693         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2694     }
2695
2696     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2697         ErrPrint("Invalid argument\n");
2698         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2699     }
2700
2701     if (!handler->common->id) {
2702         ErrPrint("Invalid argument\n");
2703         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2704     }
2705
2706     if (handler->common->request.group_changed) {
2707         ErrPrint("Previous group changing request is not finished yet\n");
2708         return DBOX_STATUS_ERROR_BUSY;
2709     }
2710
2711     if (!handler->common->is_user) {
2712         ErrPrint("CA Livebox is not able to change the group\n");
2713         return DBOX_STATUS_ERROR_PERMISSION_DENIED;
2714     }
2715
2716     if (!strcmp(handler->common->cluster, cluster) && !strcmp(handler->common->category, category)) {
2717         DbgPrint("No changes\n");
2718         return DBOX_STATUS_ERROR_ALREADY;
2719     }
2720
2721     packet = packet_create((const char *)&cmd, "ssss", handler->common->pkgname, handler->common->id, cluster, category);
2722     if (!packet) {
2723         ErrPrint("Failed to build a param\n");
2724         return DBOX_STATUS_ERROR_FAULT;
2725     }
2726
2727     if (!cb) {
2728         cb = default_group_changed_cb;
2729     }
2730
2731     ret = master_rpc_async_request(handler, packet, 0, set_group_ret_cb, NULL);
2732     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
2733         handler->cbs.group_changed.cb = cb;
2734         handler->cbs.group_changed.data = data; 
2735         handler->common->request.group_changed = 1;
2736     }
2737
2738     return ret;
2739 }
2740
2741 EAPI int dynamicbox_get_group(dynamicbox_h handler, const char **cluster, const char **category)
2742 {
2743     if (!handler) {
2744         ErrPrint("Handler is NIL\n");
2745         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2746     }
2747
2748     if (!cluster || !category || handler->state != DBOX_STATE_CREATE) {
2749         ErrPrint("Invalid argument\n");
2750         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2751     }
2752
2753     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2754         ErrPrint("Invalid argument\n");
2755         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2756     }
2757
2758     if (!handler->common->id) {
2759         ErrPrint("Invalid argument\n");
2760         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2761     }
2762
2763     *cluster = handler->common->cluster;
2764     *category = handler->common->category;
2765     return DBOX_STATUS_ERROR_NONE;
2766 }
2767
2768 EAPI int dynamicbox_get_supported_sizes(dynamicbox_h handler, int *cnt, dynamicbox_size_type_e *size_list)
2769 {
2770     register int i;
2771     register int j;
2772
2773     if (!handler || !size_list) {
2774         ErrPrint("Invalid argument, handler(%p), size_list(%p)\n", handler, size_list);
2775         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2776     }
2777
2778     if (!cnt || handler->state != DBOX_STATE_CREATE) {
2779         ErrPrint("Handler is not valid\n");
2780         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2781     }
2782
2783     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2784         ErrPrint("Handler is not valid\n");
2785         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2786     }
2787
2788     if (!handler->common->id) {
2789         ErrPrint("Handler is not valid\n");
2790         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2791     }
2792
2793     for (j = i = 0; i < DBOX_NR_OF_SIZE_LIST; i++) {
2794         if (handler->common->dbox.size_list & (0x01 << i)) {
2795             if (j == *cnt) {
2796                 break;
2797             }
2798
2799             size_list[j++] = (dynamicbox_size_type_e)(0x01 << i);
2800         }
2801     }
2802
2803     *cnt = j;
2804     return DBOX_STATUS_ERROR_NONE;
2805 }
2806
2807 EAPI const char *dynamicbox_pkgname(dynamicbox_h handler)
2808 {
2809     if (!handler) {
2810         ErrPrint("Handler is NIL\n");
2811         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2812         return NULL;
2813     }
2814
2815     if (handler->state != DBOX_STATE_CREATE) {
2816         ErrPrint("Handler is not valid\n");
2817         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2818         return NULL;
2819     }
2820
2821     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2822         ErrPrint("Handler is not valid\n");
2823         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2824         return NULL;
2825     }
2826
2827     dynamicbox_set_last_status(DBOX_STATUS_ERROR_NONE);
2828     return handler->common->pkgname;
2829 }
2830
2831 EAPI double dynamicbox_priority(dynamicbox_h handler)
2832 {
2833     if (!handler || handler->state != DBOX_STATE_CREATE) {
2834         ErrPrint("Handler is invalid\n");
2835         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2836         return -1.0f;
2837     }
2838
2839     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2840         ErrPrint("Handler is invalid\n");
2841         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2842         return -1.0f;
2843     }
2844
2845     if (!handler->common->id) {
2846         ErrPrint("Handler is not valid (%p)\n", handler);
2847         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2848         return -1.0f;
2849     }
2850
2851     return handler->common->dbox.priority;
2852 }
2853
2854 EAPI int dynamicbox_delete_cluster(const char *cluster, dynamicbox_ret_cb cb, void *data)
2855 {
2856     struct packet *packet;
2857     struct cb_info *cbinfo;
2858     unsigned int cmd = CMD_DELETE_CLUSTER;
2859     int ret;
2860
2861     packet = packet_create((const char *)&cmd, "s", cluster);
2862     if (!packet) {
2863         ErrPrint("Failed to build a param\n");
2864         return DBOX_STATUS_ERROR_FAULT;
2865     }
2866
2867     cbinfo = dbox_create_cb_info(cb, data);
2868     if (!cbinfo) {
2869         packet_destroy(packet);
2870         return DBOX_STATUS_ERROR_FAULT;
2871     }
2872
2873     ret = master_rpc_async_request(NULL, packet, 0, delete_cluster_cb, cbinfo);
2874     if (ret < 0) {
2875         dbox_destroy_cb_info(cbinfo);
2876     }
2877
2878     return ret;
2879 }
2880
2881 EAPI int dynamicbox_delete_category(const char *cluster, const char *category, dynamicbox_ret_cb cb, void *data)
2882 {
2883     struct packet *packet;
2884     struct cb_info *cbinfo;
2885     unsigned int cmd = CMD_DELETE_CATEGORY;
2886     int ret;
2887
2888     packet = packet_create((const char *)&cmd, "ss", cluster, category);
2889     if (!packet) {
2890         ErrPrint("Failed to build a param\n");
2891         return DBOX_STATUS_ERROR_FAULT;
2892     }
2893
2894     cbinfo = dbox_create_cb_info(cb, data);
2895     if (!cbinfo) {
2896         packet_destroy(packet);
2897         return DBOX_STATUS_ERROR_FAULT;
2898     }
2899
2900     ret = master_rpc_async_request(NULL, packet, 0, delete_category_cb, cbinfo);
2901     if (ret < 0) {
2902         dbox_destroy_cb_info(cbinfo);
2903     }
2904
2905     return ret;
2906 }
2907
2908 EAPI dynamicbox_type_e dynamicbox_type(dynamicbox_h handler, int gbar)
2909 {
2910     if (!handler || handler->state != DBOX_STATE_CREATE) {
2911         ErrPrint("Handler is invalid\n");
2912         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2913         return DBOX_CONTENT_TYPE_INVALID;
2914     }
2915
2916     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
2917         ErrPrint("Handler is invalid\n");
2918         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2919         return DBOX_CONTENT_TYPE_INVALID;
2920     }
2921
2922     if (!handler->common->id) {
2923         ErrPrint("Handler is not valid\n");
2924         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2925         return DBOX_CONTENT_TYPE_INVALID;
2926     }
2927
2928     if (gbar) {
2929         switch (handler->common->gbar.type) {
2930             case GBAR_TYPE_TEXT:
2931                 return DBOX_CONTENT_TYPE_TEXT;
2932             case GBAR_TYPE_BUFFER:
2933             case GBAR_TYPE_SCRIPT:
2934                 {
2935                     const char *id;
2936                     id = fb_id(handler->common->gbar.fb);
2937                     if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
2938                         return DBOX_CONTENT_TYPE_RESOURCE_ID;
2939                     }
2940                 }
2941                 return DBOX_CONTENT_TYPE_BUFFER;
2942             case GBAR_TYPE_UIFW:
2943                 return DBOX_CONTENT_TYPE_UIFW;
2944             default:
2945                 break;
2946         }
2947     } else {
2948         switch (handler->common->dbox.type) {
2949             case DBOX_TYPE_FILE:
2950                 return DBOX_CONTENT_TYPE_IMAGE;
2951             case DBOX_TYPE_BUFFER:
2952             case DBOX_TYPE_SCRIPT:
2953                 {
2954                     const char *id;
2955                     id = fb_id(handler->common->dbox.fb);
2956                     if (id && !strncasecmp(id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
2957                         return DBOX_CONTENT_TYPE_RESOURCE_ID;
2958                     }
2959                 }
2960                 return DBOX_CONTENT_TYPE_BUFFER;
2961             case DBOX_TYPE_TEXT:
2962                 return DBOX_CONTENT_TYPE_TEXT;
2963             case DBOX_TYPE_UIFW:
2964                 return DBOX_CONTENT_TYPE_UIFW;
2965             default:
2966                 break;
2967         }
2968     }
2969
2970     dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
2971     return DBOX_CONTENT_TYPE_INVALID;
2972 }
2973
2974 EAPI int dynamicbox_set_text_handler(dynamicbox_h handler, int gbar, struct dynamicbox_script_operators *ops)
2975 {
2976     if (!handler) {
2977         ErrPrint("Handler is NIL\n");
2978         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2979     }
2980
2981     if (handler->state != DBOX_STATE_CREATE) {
2982         ErrPrint("Handler is not valid\n");
2983         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2984     }
2985
2986     if (gbar) {
2987         memcpy(&handler->cbs.gbar_ops, ops, sizeof(*ops));
2988     } else {
2989         memcpy(&handler->cbs.dbox_ops, ops, sizeof(*ops));
2990     }
2991
2992     return DBOX_STATUS_ERROR_NONE;
2993 }
2994
2995 EAPI int dynamicbox_acquire_extra_resource_id(dynamicbox_h handler, int gbar, int idx, dynamicbox_ret_cb cb, void *data)
2996 {
2997     if (idx < 0) {
2998         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
2999     }
3000
3001     if (!handler || handler->state != DBOX_STATE_CREATE) {
3002         ErrPrint("Handler is invalid\n");
3003         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3004     }
3005
3006     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3007         ErrPrint("Handler is invalid\n");
3008         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3009     }
3010
3011     if (!handler->common->id) {
3012         ErrPrint("Invalid handle\n");
3013         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3014     }
3015
3016     if (gbar) {
3017         /**
3018          * This can be called from extra_resource_created event.
3019          * and it can be called before get the created event.
3020          * then we didn't know this handler's buffer type yet
3021          * so we cannot use its type to validate handle 
3022          *
3023          * handler->common.gbar.type == unknown
3024          */
3025         if (!handler->common->gbar.extra_buffer) {
3026             return DBOX_STATUS_ERROR_NOT_EXIST;
3027         }
3028
3029         if (idx >= conf_extra_buffer_count()) {
3030             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3031         }
3032
3033         return dbox_acquire_gbar_extra_pixmap(handler, idx, cb, data);
3034     } else {
3035         /**
3036          * This can be called from extra_resource_created event.
3037          * and it can be called before get the created event.
3038          * then we didn't know this handler's buffer type yet
3039          * so we cannot use its type to validate handle 
3040          *
3041          * handler->common.dbox.type == unknown
3042          */
3043         if (!handler->common->dbox.extra_buffer) {
3044             ErrPrint("Extra buffer is not prepared\n");
3045             return DBOX_STATUS_ERROR_NOT_EXIST;
3046         }
3047
3048         if (idx >= conf_extra_buffer_count()) {
3049             ErrPrint("Invalid parameter: %d / %d\n", idx, conf_extra_buffer_count());
3050             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3051         }
3052
3053         return dbox_acquire_dbox_extra_pixmap(handler, idx, cb, data);
3054     }
3055 }
3056
3057 EAPI int dynamicbox_acquire_resource_id(dynamicbox_h handler, int gbar, dynamicbox_ret_cb cb, void *data)
3058 {
3059     if (!handler || handler->state != DBOX_STATE_CREATE) {
3060         ErrPrint("Handler is invalid\n");
3061         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3062     }
3063
3064     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3065         ErrPrint("Handler is invalid\n");
3066         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3067     }
3068
3069     if (!handler->common->id) {
3070         ErrPrint("Invalid handle\n");
3071         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3072     }
3073
3074     if (gbar) {
3075         if (handler->common->gbar.type != GBAR_TYPE_SCRIPT && handler->common->gbar.type != GBAR_TYPE_BUFFER) {
3076             ErrPrint("Handler is not valid type\n");
3077             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3078         }
3079
3080         return dbox_acquire_gbar_pixmap(handler, cb, data);
3081     } else {
3082         if (handler->common->dbox.type != DBOX_TYPE_SCRIPT && handler->common->dbox.type != DBOX_TYPE_BUFFER) {
3083             ErrPrint("Handler is not valid type\n");
3084             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3085         }
3086
3087         return dbox_acquire_dbox_pixmap(handler, cb, data);
3088     }
3089 }
3090
3091 /*!
3092  * \note
3093  * Do not check the state of handler and common-handler.
3094  * If this function is used in the deleted callback,
3095  * the handler and common-handler's state would be DELETE
3096  * if this function check the state of handles,
3097  * user cannot release the pixmap.
3098  */
3099 EAPI int dynamicbox_release_resource_id(dynamicbox_h handler, int gbar, unsigned int resource_id)
3100 {
3101     struct packet *packet;
3102     const char *pkgname;
3103     const char *id;
3104     unsigned int cmd;
3105
3106     if (resource_id == 0 /* || handler->state != DBOX_STATE_CREATE */) {
3107         ErrPrint("Pixmap is invalid [%d]\n", resource_id);
3108         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3109     }
3110
3111     if (gbar) {
3112         if (!handler) {
3113             /*!
3114              * \note
3115              * Even though the handler is NULL, we should send the release request to the master.
3116              * Because the resource_id resource can be released after the handler is destroyed.
3117              * Pixmap resource is used by client. and it cannot be guaranteed to release resource_id.
3118              * In some cases, the resource_id can be released after the handler is deleted.
3119              *
3120              * Its implementation is up to the viewer app.
3121              * But we cannot force it to use only with valid handler.
3122              */
3123             DbgPrint("Using NULL handler\n");
3124             pkgname = NULL;
3125             id = NULL;
3126             /*!
3127              * \note
3128              * Master will try to find the buffer handler using given resource_id. if the pkgname and id is not valid.
3129              */
3130         } else {
3131             if (!handler->common /* || handler-common->state != DBOX_STATE_CREATE */) {
3132                 ErrPrint("Handler is invalid\n");
3133                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3134             }
3135
3136             if (!handler->common->id) {
3137                 ErrPrint("Invalid handle\n");
3138                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3139             }
3140
3141             /**
3142              * This can be called from extra_resource_created event.
3143              * and it can be called before get the created event.
3144              * then we didn't know this handler's buffer type yet
3145              * so we cannot use its type to validate handle 
3146              *
3147              * handler->common.gbar.type == unknown
3148              */
3149
3150             pkgname = handler->common->pkgname;
3151             id = handler->common->id;
3152         }
3153
3154         cmd = CMD_GBAR_RELEASE_PIXMAP;
3155     } else {
3156         if (!handler) {
3157             /*!
3158              * \note
3159              * Even though the handler is NULL, we should send the release request to the master.
3160              * Because the resource_id resource can be released after the handler is destroyed.
3161              * Pixmap resource is used by client. and it cannot be guaranteed to release resource_id.
3162              * In some cases, the resource_id can be released after the handler is deleted.
3163              *
3164              * Its implementation is up to the viewer app.
3165              * But we cannot force it to use only with valid handler.
3166              */
3167             DbgPrint("Using NULL handler\n");
3168             pkgname = NULL;
3169             id = NULL;
3170             /*!
3171              * \note
3172              * Master will try to find the buffer handler using given resource_id. if the pkgname and id is not valid.
3173              */
3174         } else {
3175             if (!handler->common /* || handler->common->state != DBOX_STATE_CREATE */) {
3176                 ErrPrint("Handler is invalid\n");
3177                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3178             }
3179
3180             if (!handler->common->id) {
3181                 ErrPrint("Invalid handle\n");
3182                 return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3183             }
3184
3185             /**
3186              * This can be called from extra_resource_created event.
3187              * and it can be called before get the created event.
3188              * then we didn't know this handler's buffer type yet
3189              * so we cannot use its type to validate handle 
3190              *
3191              * handler->common.dbox.type == unknown
3192              */
3193
3194             pkgname = handler->common->pkgname;
3195             id = handler->common->id;
3196         }
3197
3198         cmd = CMD_DBOX_RELEASE_PIXMAP;
3199     }
3200
3201     packet = packet_create_noack((const char *)&cmd, "ssi", pkgname, id, resource_id);
3202     if (!packet) {
3203         ErrPrint("Failed to build a param\n");
3204         return DBOX_STATUS_ERROR_FAULT;
3205     }
3206
3207     return master_rpc_request_only(handler, packet);
3208 }
3209
3210 EAPI unsigned int dynamicbox_extra_resource_id(const dynamicbox_h handler, int gbar, int idx)
3211 {
3212     if (idx < 0) {
3213         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3214         return 0u;
3215     }
3216
3217     if (!handler || handler->state != DBOX_STATE_CREATE) {
3218         ErrPrint("Handler is invalid\n");
3219         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3220         return 0u;
3221     }
3222
3223     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3224         ErrPrint("Handler is invalid\n");
3225         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3226         return 0u;
3227     }
3228
3229     if (!handler->common->id) {
3230         ErrPrint("Invalid handler\n");
3231         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3232         return 0u;
3233     }
3234
3235     if (gbar) {
3236         /**
3237          * This can be called from extra_resource_created event.
3238          * and it can be called before get the created event.
3239          * then we didn't know this handler's buffer type yet
3240          * so we cannot use its type to validate handle 
3241          *
3242          * handler->common.gbar.type == unknown
3243          */
3244
3245         if (!handler->common->gbar.extra_buffer || handler->common->gbar.last_extra_buffer_idx < 0) {
3246             dynamicbox_set_last_status(DBOX_STATUS_ERROR_NOT_EXIST);
3247             return 0u;
3248         }
3249
3250         return handler->common->gbar.extra_buffer[handler->common->gbar.last_extra_buffer_idx];
3251     } else {
3252         /**
3253          * This can be called from extra_resource_created event.
3254          * and it can be called before get the created event.
3255          * then we didn't know this handler's buffer type yet
3256          * so we cannot use its type to validate handle 
3257          *
3258          * handler->common.dbox.type == unknown
3259          */
3260
3261         if (!handler->common->dbox.extra_buffer || handler->common->dbox.last_extra_buffer_idx < 0) {
3262             dynamicbox_set_last_status(DBOX_STATUS_ERROR_NOT_EXIST);
3263             return 0u;
3264         }
3265
3266         return handler->common->dbox.extra_buffer[handler->common->dbox.last_extra_buffer_idx];
3267     }
3268 }
3269
3270 EAPI unsigned int dynamicbox_resource_id(const dynamicbox_h handler, int gbar)
3271 {
3272     const char *id;
3273     unsigned int pixmap = 0u;
3274
3275     if (!handler || handler->state != DBOX_STATE_CREATE) {
3276         ErrPrint("Handler is invalid\n");
3277         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3278         return 0u;
3279     }
3280
3281     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3282         ErrPrint("Handler is invalid\n");
3283         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3284         return 0u;
3285     }
3286
3287     if (!handler->common->id) {
3288         ErrPrint("Invalid handler\n");
3289         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3290         return 0u;
3291     }
3292
3293     if (gbar) {
3294         if (handler->common->gbar.type != GBAR_TYPE_SCRIPT && handler->common->gbar.type != GBAR_TYPE_BUFFER) {
3295             ErrPrint("Invalid handler\n");
3296             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3297             return 0u;
3298         }
3299
3300         id = fb_id(handler->common->gbar.fb);
3301         if (id && sscanf(id, SCHEMA_PIXMAP "%u", &pixmap) != 1) {
3302             ErrPrint("PIXMAP Id is not valid\n");
3303             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3304             return 0u;
3305         }
3306     } else {
3307         if (handler->common->dbox.type != DBOX_TYPE_SCRIPT && handler->common->dbox.type != DBOX_TYPE_BUFFER) {
3308             ErrPrint("Invalid handler\n");
3309             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3310             return 0u;
3311         }
3312
3313         id = fb_id(handler->common->dbox.fb);
3314         if (id && sscanf(id, SCHEMA_PIXMAP "%u", &pixmap) != 1) {
3315             ErrPrint("PIXMAP Id is not valid\n");
3316             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3317             return 0u;
3318         }
3319     }
3320
3321     return pixmap;
3322 }
3323
3324 EAPI void *dynamicbox_acquire_buffer(dynamicbox_h handler, int gbar)
3325 {
3326     if (gbar) {
3327         if (!handler || handler->state != DBOX_STATE_CREATE) {
3328             ErrPrint("Handler is invalid\n");
3329             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3330             return NULL;
3331         }
3332
3333         if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3334             ErrPrint("Handler is invalid\n");
3335             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3336             return NULL;
3337         }
3338
3339         if (!handler->common->id) {
3340             ErrPrint("Invalid handler\n");
3341             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3342             return NULL;
3343         }
3344
3345         if (handler->common->gbar.type != GBAR_TYPE_SCRIPT && handler->common->gbar.type != GBAR_TYPE_BUFFER) {
3346             ErrPrint("Handler is not valid type\n");
3347             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3348             return NULL;
3349         }
3350
3351         return fb_acquire_buffer(handler->common->gbar.fb);
3352     } else {
3353         if (!handler || handler->state != DBOX_STATE_CREATE) {
3354             ErrPrint("Handler is invalid\n");
3355             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3356             return NULL;
3357         }
3358
3359         if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3360             ErrPrint("Handler is invalid\n");
3361             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3362             return NULL;
3363         }
3364
3365         if (!handler->common->id) {
3366             ErrPrint("Invalid handle\n");
3367             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3368             return NULL;
3369         }
3370
3371         if (handler->common->dbox.type != DBOX_TYPE_SCRIPT && handler->common->dbox.type != DBOX_TYPE_BUFFER) {
3372             ErrPrint("Handler is not valid type\n");
3373             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3374             return NULL;
3375         }
3376
3377         return fb_acquire_buffer(handler->common->dbox.fb);
3378     }
3379 }
3380
3381 EAPI int dynamicbox_release_buffer(void *buffer)
3382 {
3383     return fb_release_buffer(buffer);
3384 }
3385
3386 EAPI int dynamicbox_buffer_refcnt(void *buffer)
3387 {
3388     return fb_refcnt(buffer);
3389 }
3390
3391 EAPI int dynamicbox_buffer_size(dynamicbox_h handler, int gbar)
3392 {
3393     if (!handler || handler->state != DBOX_STATE_CREATE) {
3394         ErrPrint("Handler is invalid\n");
3395         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3396         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3397     }
3398
3399     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3400         ErrPrint("Handler is invalid\n");
3401         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3402         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3403     }
3404
3405     if (!handler->common->id) {
3406         ErrPrint("Invalid handler\n");
3407         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3408         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3409     }
3410
3411     if (gbar) {
3412         return fb_size(handler->common->gbar.fb);
3413     } else {
3414         return fb_size(handler->common->dbox.fb);
3415     }
3416 }
3417
3418 EAPI int dynamicbox_is_created_by_user(dynamicbox_h handler)
3419 {
3420     if (!handler || handler->state != DBOX_STATE_CREATE) {
3421         ErrPrint("Handler is invalid\n");
3422         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3423         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3424     }
3425
3426     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3427         ErrPrint("Handler is invalid\n");
3428         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3429         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3430     }
3431
3432     if (!handler->common->id) {
3433         ErrPrint("Invalid handler\n");
3434         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3435         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3436     }
3437
3438     return handler->common->is_user;
3439 }
3440
3441 EAPI int dynamicbox_set_pinup(dynamicbox_h handler, int flag, dynamicbox_ret_cb cb, void *data)
3442 {
3443     struct packet *packet;
3444     unsigned int cmd = CMD_PINUP_CHANGED;
3445     int ret;
3446
3447     if (!handler || handler->state != DBOX_STATE_CREATE) {
3448         ErrPrint("Handler is invalid\n");
3449         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3450     }
3451
3452     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3453         ErrPrint("Handler is invalid\n");
3454         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3455     }
3456
3457     if (!handler->common->id) {
3458         ErrPrint("Invalid handler\n");
3459         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3460     }
3461
3462     if (handler->common->request.pinup) {
3463         ErrPrint("Previous pinup request is not finished\n");
3464         return DBOX_STATUS_ERROR_BUSY;
3465     }
3466
3467     if (handler->common->is_pinned_up == flag) {
3468         DbgPrint("No changes\n");
3469         return DBOX_STATUS_ERROR_ALREADY;
3470     }
3471
3472     packet = packet_create((const char *)&cmd, "ssi", handler->common->pkgname, handler->common->id, flag);
3473     if (!packet) {
3474         ErrPrint("Failed to build a param\n");
3475         return DBOX_STATUS_ERROR_FAULT;
3476     }
3477
3478     if (!cb) {
3479         cb = default_pinup_cb;
3480     }
3481
3482     ret = master_rpc_async_request(handler, packet, 0, pinup_done_cb, NULL);
3483     if (ret == (int)DBOX_STATUS_ERROR_NONE) {
3484         handler->cbs.pinup.cb = cb;
3485         handler->cbs.pinup.data = data;
3486         handler->common->request.pinup = 1;
3487     }
3488
3489     return ret;
3490 }
3491
3492 EAPI int dynamicbox_is_pinned_up(dynamicbox_h handler)
3493 {
3494     if (!handler || handler->state != DBOX_STATE_CREATE) {
3495         ErrPrint("Handler is invalid\n");
3496         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3497         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3498     }
3499
3500     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3501         ErrPrint("Handler is invalid\n");
3502         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3503         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3504     }
3505
3506     if (!handler->common->id) {
3507         ErrPrint("Invalid handler\n");
3508         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3509         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3510     }
3511
3512     return handler->common->is_pinned_up;
3513 }
3514
3515 EAPI int dynamicbox_has_pinup(dynamicbox_h handler)
3516 {
3517     if (!handler || handler->state != DBOX_STATE_CREATE) {
3518         ErrPrint("Handler is invalid\n");
3519         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3520         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3521     }
3522
3523     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3524         ErrPrint("Handler is invalid\n");
3525         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3526         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3527     }
3528
3529     if (!handler->common->id) {
3530         ErrPrint("Invalid handler\n");
3531         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3532         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3533     }
3534
3535     return handler->common->dbox.pinup_supported;
3536 }
3537
3538 EAPI int dynamicbox_set_data(dynamicbox_h handler, void *data)
3539 {
3540     if (!handler) {
3541         ErrPrint("Handler is NIL\n");
3542         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3543     }
3544
3545     if (handler->state != DBOX_STATE_CREATE) {
3546         ErrPrint("Handler is invalid\n");
3547         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3548     }
3549
3550     handler->data = data;
3551     return DBOX_STATUS_ERROR_NONE;
3552 }
3553
3554 EAPI void *dynamicbox_data(dynamicbox_h handler)
3555 {
3556     if (!handler) {
3557         ErrPrint("Handler is NIL\n");
3558         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3559         return NULL;
3560     }
3561
3562     if (handler->state != DBOX_STATE_CREATE) {
3563         ErrPrint("Handler is invalid\n");
3564         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3565         return NULL;
3566     }
3567
3568     return handler->data;
3569 }
3570
3571 EAPI const char *dynamicbox_content(dynamicbox_h handler)
3572 {
3573     if (!handler || handler->state != DBOX_STATE_CREATE) {
3574         ErrPrint("Handler is invalid\n");
3575         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3576         return NULL;
3577     }
3578
3579     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3580         ErrPrint("Invalid handle\n");
3581         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3582         return NULL;
3583     }
3584
3585     dynamicbox_set_last_status(DBOX_STATUS_ERROR_NONE);
3586     return handler->common->content;
3587 }
3588
3589 EAPI const char *dynamicbox_title(dynamicbox_h handler)
3590 {
3591     if (!handler || handler->state != DBOX_STATE_CREATE) {
3592         ErrPrint("Handler is invalid\n");
3593         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3594         return NULL;
3595     }
3596
3597     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3598         ErrPrint("Invalid handle\n");
3599         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3600         return NULL;
3601     }
3602
3603     dynamicbox_set_last_status(DBOX_STATUS_ERROR_NONE);
3604     return handler->common->title;
3605 }
3606
3607 EAPI int dynamicbox_emit_text_signal(dynamicbox_h handler, dynamicbox_text_event_t event_info, dynamicbox_ret_cb cb, void *data)
3608 {
3609     struct packet *packet;
3610     struct cb_info *cbinfo;
3611     unsigned int cmd = CMD_TEXT_SIGNAL;
3612     int ret;
3613     const char *emission;
3614     const char *source;
3615
3616     if (!handler || handler->state != DBOX_STATE_CREATE) {
3617         ErrPrint("Handler is invalid\n");
3618         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3619     }
3620
3621     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3622         ErrPrint("Handler is invalid\n");
3623         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3624     }
3625
3626     if (handler->common->dbox.type != DBOX_TYPE_TEXT && handler->common->gbar.type != GBAR_TYPE_TEXT) {
3627         DbgPrint("Not a text box, but send signal\n");
3628     }
3629
3630     if (!handler->common->id) {
3631         ErrPrint("Handler is not valid\n");
3632         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3633     }
3634
3635     if (!event_info) {
3636         ErrPrint("Invalid event info\n");
3637         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3638     }
3639
3640     emission = event_info->emission;
3641     if (!emission) {
3642         emission = "";
3643     }
3644
3645     source = event_info->source;
3646     if (!source) {
3647         source = "";
3648     }
3649
3650     packet = packet_create((const char *)&cmd, "ssssdddd",
3651             handler->common->pkgname, handler->common->id,
3652             emission, source,
3653             event_info->geometry.sx, event_info->geometry.sy,
3654             event_info->geometry.ex, event_info->geometry.ey);
3655     if (!packet) {
3656         ErrPrint("Failed to build a param\n");
3657         return DBOX_STATUS_ERROR_FAULT;
3658     }
3659
3660     cbinfo = dbox_create_cb_info(cb, data);
3661     if (!cbinfo) {
3662         packet_destroy(packet);
3663         return DBOX_STATUS_ERROR_FAULT;
3664     }
3665
3666     ret = master_rpc_async_request(handler, packet, 0, text_signal_cb, cbinfo);
3667     if (ret < 0) {
3668         dbox_destroy_cb_info(cbinfo);
3669     }
3670
3671     return ret;
3672 }
3673
3674 EAPI int dynamicbox_subscribe_group(const char *cluster, const char *category)
3675 {
3676     struct packet *packet;
3677     unsigned int cmd = CMD_SUBSCRIBE;
3678
3679     /*!
3680      * \todo
3681      * Validate the group info using DB
3682      * If the group info is not valid, do not send this request
3683      */
3684
3685     packet = packet_create_noack((const char *)&cmd, "ss", cluster ? cluster : "", category ? category : "");
3686     if (!packet) {
3687         ErrPrint("Failed to create a packet\n");
3688         return DBOX_STATUS_ERROR_FAULT;
3689     }
3690
3691     return master_rpc_request_only(NULL, packet);
3692 }
3693
3694 EAPI int dynamicbox_unsubscribe_group(const char *cluster, const char *category)
3695 {
3696     struct packet *packet;
3697     unsigned int cmd = CMD_UNSUBSCRIBE;
3698
3699     /*!
3700      * \todo
3701      * Validate the group info using DB
3702      * If the group info is not valid, do not send this request
3703      * AND Check the subscribed or not too
3704      */
3705
3706     packet = packet_create_noack((const char *)&cmd, "ss", cluster ? cluster : "", category ? category : "");
3707     if (!packet) {
3708         ErrPrint("Failed to create a packet\n");
3709         return DBOX_STATUS_ERROR_FAULT;
3710     }
3711
3712     return master_rpc_request_only(NULL, packet);
3713 }
3714
3715 EAPI int dynamicbox_refresh(dynamicbox_h handler, int force)
3716 {
3717     struct packet *packet;
3718     unsigned int cmd = CMD_UPDATE;
3719
3720     if (!handler || handler->state != DBOX_STATE_CREATE) {
3721         ErrPrint("Handler is invalid\n");
3722         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3723     }
3724
3725     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3726         ErrPrint("Handler is not valid\n");
3727         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3728     }
3729
3730     if (!handler->common->id) {
3731         ErrPrint("Handler is not valid\n");
3732         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3733     }
3734
3735     packet = packet_create_noack((const char *)&cmd, "ssi", handler->common->pkgname, handler->common->id, force);
3736     if (!packet) {
3737         ErrPrint("Failed to create a packet\n");
3738         return DBOX_STATUS_ERROR_FAULT;
3739     }
3740
3741     return master_rpc_request_only(handler, packet);
3742 }
3743
3744 EAPI int dynamicbox_refresh_group(const char *cluster, const char *category, int force)
3745 {
3746     struct packet *packet;
3747     unsigned int cmd = CMD_REFRESH_GROUP;
3748
3749     if (!cluster || !category) {
3750         ErrPrint("Invalid argument\n");
3751         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3752     }
3753
3754     packet = packet_create_noack((const char *)&cmd, "ssi", cluster, category, force);
3755     if (!packet) {
3756         ErrPrint("Failed to create a packet\n");
3757         return DBOX_STATUS_ERROR_FAULT;
3758     }
3759
3760     return master_rpc_request_only(NULL, packet);
3761 }
3762
3763 EAPI int dynamicbox_set_visibility(dynamicbox_h handler, dynamicbox_visible_state_e state)
3764 {
3765     int old_state;
3766     int ret;
3767
3768     if (!handler || handler->state != DBOX_STATE_CREATE) {
3769         ErrPrint("Handler is invalid\n");
3770         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3771     }
3772
3773     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3774         ErrPrint("Handler is not valid\n");
3775         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3776     }
3777
3778     if (!handler->common->id) {
3779         ErrPrint("Handler is not valid\n");
3780         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3781     }
3782
3783     if (!handler->common->is_user) {
3784         /* System cluster dynamicbox cannot be changed its visible states */
3785         if (state == DBOX_HIDE_WITH_PAUSE) {
3786             ErrPrint("CA Livebox is not able to change the visibility\n");
3787             return DBOX_STATUS_ERROR_PERMISSION_DENIED;
3788         }
3789     }
3790
3791     if (handler->visible == state) {
3792         DbgPrint("%s has no changes\n", handler->common->pkgname);
3793         return DBOX_STATUS_ERROR_ALREADY;
3794     }
3795
3796     old_state = handler->visible;
3797     handler->visible = state;
3798
3799     ret = dbox_set_visibility(handler, state);
3800     if (ret < 0) {
3801         handler->visible = old_state;
3802     }
3803
3804     return ret;
3805 }
3806
3807 EAPI dynamicbox_visible_state_e dynamicbox_visibility(dynamicbox_h handler)
3808 {
3809     if (!handler || handler->state != DBOX_STATE_CREATE) {
3810         ErrPrint("Handler is invalid\n");
3811         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3812         return DBOX_VISIBLE_ERROR;
3813     }
3814
3815     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3816         ErrPrint("Handler is not valid\n");
3817         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3818         return DBOX_VISIBLE_ERROR;
3819     }
3820
3821     if (!handler->common->id) {
3822         ErrPrint("Handler is not valid\n");
3823         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3824         return DBOX_VISIBLE_ERROR;
3825     }
3826
3827     return handler->visible;
3828 }
3829
3830 EAPI int dynamicbox_viewer_set_paused(void)
3831 {
3832     struct packet *packet;
3833     unsigned int cmd = CMD_CLIENT_PAUSED;
3834
3835     packet = packet_create_noack((const char *)&cmd, "d", util_timestamp());
3836     if (!packet) {
3837         ErrPrint("Failed to create a pause packet\n");
3838         return DBOX_STATUS_ERROR_FAULT;
3839     }
3840
3841     return master_rpc_request_only(NULL, packet);
3842 }
3843
3844 EAPI int dynamicbox_viewer_set_resumed(void)
3845 {
3846     struct packet *packet;
3847     unsigned int cmd = CMD_CLIENT_RESUMED;
3848
3849     packet = packet_create_noack((const char *)&cmd, "d", util_timestamp());
3850     if (!packet) {
3851         ErrPrint("Failed to create a resume packet\n");
3852         return DBOX_STATUS_ERROR_FAULT;
3853     }
3854
3855     return master_rpc_request_only(NULL, packet);
3856 }
3857
3858 EAPI int dynamicbox_sync_buffer(dynamicbox_h handler, int gbar)
3859 {
3860     if (!handler || handler->state != DBOX_STATE_CREATE) {
3861         ErrPrint("Invalid handle\n");
3862         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3863     }
3864
3865     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3866         ErrPrint("Invalid handle\n");
3867         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3868     }
3869
3870     if (!handler->common->id) {
3871         ErrPrint("Invalid handle\n");
3872         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3873     }
3874
3875     if (gbar) {
3876         return dbox_sync_gbar_fb(handler->common);
3877     } else {
3878         return dbox_sync_dbox_fb(handler->common);
3879     }
3880 }
3881
3882 EAPI const char *dynamicbox_alternative_icon(dynamicbox_h handler)
3883 {
3884     if (!handler || handler->state != DBOX_STATE_CREATE) {
3885         ErrPrint("Handler is not valid[%p]\n", handler);
3886         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3887         return NULL;
3888     }
3889
3890     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3891         ErrPrint("Handler is not valid\n");
3892         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3893         return NULL;
3894     }
3895
3896     return handler->common->alt.icon;
3897 }
3898
3899 EAPI const char *dynamicbox_alternative_name(dynamicbox_h handler)
3900 {
3901     if (!handler || handler->state != DBOX_STATE_CREATE) {
3902         ErrPrint("Handler is not valid[%p]\n", handler);
3903         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3904         return NULL;
3905     }
3906
3907     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3908         ErrPrint("Handler is not valid\n");
3909         dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
3910         return NULL;
3911     }
3912
3913     return handler->common->alt.name;
3914 }
3915
3916 EAPI int dynamicbox_acquire_buffer_lock(dynamicbox_h handler, int is_gbar)
3917 {
3918     int ret = DBOX_STATUS_ERROR_NONE;
3919
3920     if (!handler || handler->state != DBOX_STATE_CREATE) {
3921         ErrPrint("Handler is not valid[%p]\n", handler);
3922         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3923     }
3924
3925     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3926         ErrPrint("Handler is not valid\n");
3927         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3928     }
3929
3930     if (!handler->common->id) {
3931         ErrPrint("Handler is not valid[%p]\n", handler);
3932         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3933     }
3934
3935     if (is_gbar) {
3936         ret = dynamicbox_service_acquire_lock(handler->common->gbar.lock);
3937     } else {
3938         ret = dynamicbox_service_acquire_lock(handler->common->dbox.lock);
3939     }
3940
3941     return ret == 0 ? DBOX_STATUS_ERROR_NONE : DBOX_STATUS_ERROR_FAULT;
3942 }
3943
3944 EAPI int dynamicbox_release_buffer_lock(dynamicbox_h handler, int is_gbar)
3945 {
3946     int ret = DBOX_STATUS_ERROR_NONE;
3947
3948     if (!handler || handler->state != DBOX_STATE_CREATE) {
3949         ErrPrint("Invalid handle\n");
3950         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3951     }
3952
3953     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
3954         ErrPrint("Invalid handle\n");
3955         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3956     }
3957
3958     if (!handler->common->id) {
3959         ErrPrint("Handler is not valid[%p]\n", handler);
3960         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
3961     }
3962
3963     if (is_gbar) {
3964         ret = dynamicbox_service_release_lock(handler->common->gbar.lock);
3965     } else {
3966         ret = dynamicbox_service_release_lock(handler->common->dbox.lock);
3967     }
3968
3969     return ret == 0 ? DBOX_STATUS_ERROR_NONE : DBOX_STATUS_ERROR_FAULT;
3970 }
3971
3972 EAPI int dynamicbox_set_option(dynamicbox_option_type_e option, int state)
3973 {
3974     int ret = DBOX_STATUS_ERROR_NONE;
3975
3976     switch (option) {
3977         case DBOX_OPTION_MANUAL_SYNC:
3978             conf_set_manual_sync(state);
3979             break;
3980         case DBOX_OPTION_FRAME_DROP_FOR_RESIZE:
3981             conf_set_frame_drop_for_resizing(state);
3982             break;
3983         case DBOX_OPTION_SHARED_CONTENT:
3984             conf_set_shared_content(state);
3985             break;
3986         case DBOX_OPTION_DIRECT_UPDATE:
3987             if (s_info.init_count) {
3988                 DbgPrint("Already intialized, this option is not applied\n");
3989             }
3990             conf_set_direct_update(state);
3991             break;
3992         case DBOX_OPTION_EXTRA_BUFFER_CNT:
3993             ErrPrint("Permission denied\n");
3994             ret = DBOX_STATUS_ERROR_PERMISSION_DENIED;
3995             break;
3996         default:
3997             ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
3998             break;
3999     }
4000
4001     return ret;
4002 }
4003
4004 EAPI int dynamicbox_option(dynamicbox_option_type_e option)
4005 {
4006     int ret;
4007
4008     dynamicbox_set_last_status(DBOX_STATUS_ERROR_NONE);
4009     switch (option) {
4010         case DBOX_OPTION_MANUAL_SYNC:
4011             ret = conf_manual_sync();
4012             break;
4013         case DBOX_OPTION_FRAME_DROP_FOR_RESIZE:
4014             ret = conf_frame_drop_for_resizing();
4015             break;
4016         case DBOX_OPTION_SHARED_CONTENT:
4017             ret = conf_shared_content();
4018             break;
4019         case DBOX_OPTION_DIRECT_UPDATE:
4020             ret = conf_direct_update();
4021             break;
4022         case DBOX_OPTION_EXTRA_BUFFER_CNT:
4023             ret = conf_extra_buffer_count();
4024             break;
4025         default:
4026             ret = DBOX_STATUS_ERROR_INVALID_PARAMETER;
4027             dynamicbox_set_last_status(DBOX_STATUS_ERROR_INVALID_PARAMETER);
4028             break;
4029     }
4030
4031     return ret;
4032 }
4033
4034 EAPI int dynamicbox_set_auto_launch_handler(dynamicbox_auto_launch_handler_cb dbox_launch_handler, void *data)
4035 {
4036     s_info.launch.handler = dbox_launch_handler;
4037     s_info.launch.data = data;
4038
4039     return DBOX_STATUS_ERROR_NONE;
4040 }
4041
4042 EAPI int dynamicbox_damage_region_get(dynamicbox_h handler, int gbar, const dynamicbox_damage_region_t *region)
4043 {
4044     if (!handler || handler->state != DBOX_STATE_CREATE) {
4045         ErrPrint("Invalid handle\n");
4046         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
4047     }
4048
4049     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
4050         ErrPrint("Invalid handle\n");
4051         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
4052     }
4053
4054     if (!handler->common->id) {
4055         ErrPrint("Handler is not valid[%p]\n", handler);
4056         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
4057     }
4058
4059     if (gbar) {
4060         region = &handler->common->dbox.last_damage;
4061     } else {
4062         region = &handler->common->gbar.last_damage;
4063     }
4064
4065     return DBOX_STATUS_ERROR_NONE;
4066 }
4067
4068 EAPI int dynamicbox_get_affected_extra_buffer(dynamicbox_h handler, int gbar, int *idx, unsigned int *resource_id)
4069 {
4070     int _idx;
4071     unsigned int _resource_id;
4072
4073     if (!handler || handler->state != DBOX_STATE_CREATE) {
4074         ErrPrint("Invalid handle\n");
4075         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
4076     }
4077
4078     if (!handler->common || handler->common->state != DBOX_STATE_CREATE) {
4079         ErrPrint("Invalid handle\n");
4080         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
4081     }
4082
4083     if (!handler->common->id) {
4084         ErrPrint("Handler is not valid[%p]\n", handler);
4085         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
4086     }
4087
4088     if (!idx) {
4089         idx = &_idx;
4090     }
4091
4092     if (!resource_id) {
4093         resource_id = &_resource_id;
4094     }
4095
4096     if (gbar) {
4097         if (!handler->common->gbar.extra_buffer || handler->common->gbar.last_extra_buffer_idx < 0) {
4098             return DBOX_STATUS_ERROR_NOT_EXIST;
4099         }
4100
4101         *idx = handler->common->gbar.last_extra_buffer_idx;
4102         *resource_id = handler->common->gbar.extra_buffer[*idx];
4103     } else {
4104         if (!handler->common->dbox.extra_buffer || handler->common->dbox.last_extra_buffer_idx < 0) {
4105             return DBOX_STATUS_ERROR_NOT_EXIST;
4106         }
4107
4108         *idx = handler->common->dbox.last_extra_buffer_idx;
4109         *resource_id = handler->common->dbox.extra_buffer[*idx];
4110     }
4111
4112     return DBOX_STATUS_ERROR_NONE;
4113 }
4114
4115 /* End of a file */