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