Shut gcc compile warning.
[platform/framework/web/data-provider-master.git] / src / server.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20
21 #include <dlog.h>
22 #include <Evas.h>
23 #include <Ecore_Evas.h> /* fb.h */
24 #include <aul.h>
25 #include <Ecore.h>
26 #include <ail.h>
27
28 #include <packet.h>
29 #include <com-core_packet.h>
30 #include <livebox-errno.h>
31 #include <livebox-service.h>
32
33 #include "critical_log.h"
34 #include "conf.h"
35 #include "debug.h"
36 #include "server.h"
37 #include "slave_life.h"
38 #include "slave_rpc.h"
39 #include "client_life.h"
40 #include "instance.h"
41 #include "client_rpc.h"
42 #include "package.h"
43 #include "script_handler.h"
44 #include "buffer_handler.h"
45 #include "util.h"
46 #include "fault_manager.h"
47 #include "fb.h" /* fb_type */
48 #include "group.h"
49 #include "xmonitor.h"
50 #include "abi.h"
51 #include "liveinfo.h"
52 #include "io.h"
53 #include "event.h"
54
55 static struct info {
56         int info_fd;
57         int client_fd;
58         int service_fd;
59         int slave_fd;
60         int remote_client_fd;
61 } s_info = {
62         .info_fd = -1,
63         .client_fd = -1,
64         .service_fd = -1,
65         .slave_fd = -1,
66         .remote_client_fd = -1,
67 };
68
69 /* Share this with provider */
70 enum target_type {
71         TYPE_LB,
72         TYPE_PD,
73         TYPE_ERROR
74 };
75
76 struct access_cbdata {
77         int status;
78         struct inst_info *inst;
79 };
80
81 struct deleted_item {
82         struct client_node *client;
83         struct inst_info *inst;
84 };
85
86 static inline int forward_lb_event_packet(const struct pkg_info *pkg, struct inst_info *inst, const struct packet *packet)
87 {
88         struct buffer_info *buffer;
89         struct slave_node *slave;
90         int ret;
91
92         buffer = instance_lb_buffer(inst);
93         if (!buffer) {
94                 ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
95                 ret = LB_STATUS_ERROR_FAULT;
96                 goto out;
97         }
98
99         slave = package_slave(pkg);
100         if (!slave) {
101                 ErrPrint("Package[%s] has no slave\n", package_name(pkg));
102                 ret = LB_STATUS_ERROR_INVALID;
103                 goto out;
104         }
105
106         packet_ref((struct packet *)packet);
107         ret = slave_rpc_request_only(slave, package_name(pkg), (struct packet *)packet, 0);
108
109 out:
110         return ret;
111 }
112
113 static inline int forward_pd_event_packet(const struct pkg_info *pkg, struct inst_info *inst, const struct packet *packet)
114 {
115         struct buffer_info *buffer;
116         struct slave_node *slave;
117         int ret;
118
119         buffer = instance_pd_buffer(inst);
120         if (!buffer) {
121                 ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
122                 ret = LB_STATUS_ERROR_FAULT;
123                 goto out;
124         }
125
126         slave = package_slave(pkg);
127         if (!slave) {
128                 ErrPrint("Package[%s] has no slave\n", package_name(pkg));
129                 ret = LB_STATUS_ERROR_INVALID;
130                 goto out;
131         }
132
133         packet_ref((struct packet *)packet);
134         ret = slave_rpc_request_only(slave, package_name(pkg), (struct packet *)packet, 0);
135
136 out:
137         return ret;
138 }
139
140 static inline int forward_pd_access_packet(const struct pkg_info *pkg, struct inst_info *inst, const char *command, double timestamp, int x, int y)
141 {
142         int ret;
143         struct buffer_info *buffer;
144         struct slave_node *slave;
145         struct packet *p;
146
147         buffer = instance_pd_buffer(inst);
148         if (!buffer) {
149                 ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
150                 ret = LB_STATUS_ERROR_FAULT;
151                 goto out;
152         }
153
154         slave = package_slave(pkg);
155         if (!slave) {
156                 ErrPrint("Package[%s] has no slave\n", package_name(pkg));
157                 ret = LB_STATUS_ERROR_INVALID;
158                 goto out;
159         }
160
161         p = packet_create_noack(command, "ssdii", package_name(pkg), instance_id(inst), timestamp, x, y);
162         ret = slave_rpc_request_only(slave, package_name(pkg), p, 0);
163
164 out:
165         return ret;
166 }
167
168 static int forward_lb_access_packet(const struct pkg_info *pkg, struct inst_info *inst, const char *command, double timestamp, int x, int y)
169 {
170         int ret;
171         struct buffer_info *buffer;
172         struct slave_node *slave;
173         struct packet *p;
174
175         buffer = instance_lb_buffer(inst);
176         if (!buffer) {
177                 ErrPrint("Instance[%s] has no buffer\n", instance_id(inst));
178                 ret = LB_STATUS_ERROR_FAULT;
179                 goto out;
180         }
181
182         slave = package_slave(pkg);
183         if (!slave) {
184                 ErrPrint("Package[%s] has no slave\n", package_name(pkg));
185                 ret = LB_STATUS_ERROR_INVALID;
186                 goto out;
187         }
188
189         p = packet_create_noack(command, "ssdii", package_name(pkg), instance_id(inst), timestamp, x, y);
190         ret = slave_rpc_request_only(slave, package_name(pkg), p, 0);
191
192 out:
193         return ret;
194 }
195
196 static Eina_Bool lazy_access_status_cb(void *data)
197 {
198         struct access_cbdata *cbdata = data;
199
200         if (instance_unref(cbdata->inst)) {
201                 instance_send_access_status(cbdata->inst, cbdata->status);
202         } else {
203                 DbgPrint("Skip sending access status (%d)\n", cbdata->status);
204         }
205         /*!
206          * If instance_unref returns NULL,
207          * The instance is destroyed. it means, we don't need to send event to the viewer
208          */
209         DbgFree(cbdata);
210         return ECORE_CALLBACK_CANCEL;
211 }
212
213 static int slave_fault_open_script_cb(struct slave_node *slave, void *data)
214 {
215         Ecore_Timer *timer;
216
217         (void)script_handler_unload(instance_pd_script(data), 1);
218         (void)instance_slave_close_pd(data, instance_pd_owner(data));
219         (void)instance_client_pd_created(data, LB_STATUS_ERROR_FAULT);
220
221         timer = instance_del_data(data, "lazy,pd,open");
222         if (timer) {
223                 ecore_timer_del(timer);
224         }
225
226         (void)instance_unref(data);
227
228         return -1; /* remove this handler */
229 }
230
231 static int slave_fault_open_buffer_cb(struct slave_node *slave, void *data)
232 {
233         Ecore_Timer *timer;
234
235         (void)instance_slave_close_pd(data, instance_pd_owner(data));
236         (void)instance_client_pd_created(data, LB_STATUS_ERROR_FAULT);
237
238         timer = instance_del_data(data, "pd,open,monitor");
239         if (timer) {
240                 ecore_timer_del(timer);
241         }
242
243         (void)instance_unref(data);
244
245         return -1; /* remove this handler */
246 }
247
248 static int slave_fault_close_script_cb(struct slave_node *slave, void *data)
249 {
250         Ecore_Timer *timer;
251
252         (void)instance_client_pd_destroyed(data, LB_STATUS_ERROR_FAULT);
253
254         timer = instance_del_data(data, "lazy,pd,close");
255         if (timer) {
256                 ecore_timer_del(timer);
257         }
258
259         (void)instance_unref(data);
260
261         return -1; /* remove this handler */
262 }
263
264 static int slave_fault_close_buffer_cb(struct slave_node *slave, void *data)
265 {
266         Ecore_Timer *timer;
267
268         (void)instance_client_pd_destroyed(data, LB_STATUS_ERROR_FAULT);
269
270         timer = instance_del_data(data, "lazy,pd,close");
271         if (!timer) {
272                 timer = instance_del_data(data, "pd,close,monitor");
273         }
274
275         if (timer) {
276                 ecore_timer_del(timer);
277         }
278
279         (void)instance_unref(data);
280
281         return -1; /* remove this handler */
282 }
283
284 static int slave_fault_resize_buffer_cb(struct slave_node *slave, void *data)
285 {
286         Ecore_Timer *timer;
287
288         (void)instance_slave_close_pd(data, instance_pd_owner(data));
289         (void)instance_client_pd_destroyed(data, LB_STATUS_ERROR_FAULT);
290
291         timer = instance_del_data(data, "pd,resize,monitor");
292         if (timer) {
293                 ecore_timer_del(timer);
294         }
295
296         (void)instance_unref(data);
297
298         return -1; /* remove this handler */
299 }
300
301 static int event_lb_route_cb(enum event_state state, struct event_data *event_info, void *data)
302 {
303         struct inst_info *inst = data;
304         const struct pkg_info *pkg;
305         struct slave_node *slave;
306         struct packet *packet;
307         const char *cmdstr;
308
309         pkg = instance_package(inst);
310         if (!pkg) {
311                 return LB_STATUS_ERROR_INVALID;
312         }
313
314         slave = package_slave(pkg);
315         if (!slave) {
316                 return LB_STATUS_ERROR_INVALID;
317         }
318
319         switch (state) {
320         case EVENT_STATE_ACTIVATE:
321                 cmdstr = "lb_mouse_down";
322                 break;
323         case EVENT_STATE_ACTIVATED:
324                 cmdstr = "lb_mouse_move";
325                 break;
326         case EVENT_STATE_DEACTIVATE:
327                 cmdstr = "lb_mouse_up";
328                 break;
329         default:
330                 return LB_STATUS_ERROR_INVALID;
331         }
332
333         packet = packet_create_noack(cmdstr, "ssdii", package_name(pkg), instance_id(inst), util_timestamp(), event_info->x, event_info->y);
334         if (!packet) {
335                 return LB_STATUS_ERROR_FAULT;
336         }
337
338         return slave_rpc_request_only(slave, package_name(pkg), packet, 0);
339 }
340
341 static int event_lb_consume_cb(enum event_state state, struct event_data *event_info, void *data)
342 {
343         struct script_info *script;
344         struct inst_info *inst = data;
345         const struct pkg_info *pkg;
346         double timestamp;
347         Evas *e;
348
349         pkg = instance_package(inst);
350         if (!pkg) {
351                 return 0;
352         }
353
354         script = instance_lb_script(inst);
355         if (!script) {
356                 return LB_STATUS_ERROR_FAULT;
357         }
358
359         e = script_handler_evas(script);
360         if (!e) {
361                 return LB_STATUS_ERROR_FAULT;
362         }
363
364         timestamp = util_timestamp();
365
366         switch (state) {
367         case EVENT_STATE_ACTIVATE:
368                 script_handler_update_pointer(script, event_info->x, event_info->y, 1);
369                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_DOWN, timestamp);
370                 break;
371         case EVENT_STATE_ACTIVATED:
372                 script_handler_update_pointer(script, event_info->x, event_info->y, -1);
373                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_MOVE, timestamp);
374                 break;
375         case EVENT_STATE_DEACTIVATE:
376                 script_handler_update_pointer(script, event_info->x, event_info->y, 0);
377                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_UP, timestamp);
378                 break;
379         default:
380                 break;
381         }
382
383         return 0;
384 }
385
386 static int event_pd_route_cb(enum event_state state, struct event_data *event_info, void *data)
387 {
388         struct inst_info *inst = data;
389         const struct pkg_info *pkg;
390         struct slave_node *slave;
391         struct packet *packet;
392         const char *cmdstr;
393
394         pkg = instance_package(inst);
395         if (!pkg) {
396                 return LB_STATUS_ERROR_INVALID;
397         }
398
399         slave = package_slave(pkg);
400         if (!slave) {
401                 return LB_STATUS_ERROR_INVALID;
402         }
403
404         switch (state) {
405         case EVENT_STATE_ACTIVATE:
406                 cmdstr = "pd_mouse_down";
407                 break;
408         case EVENT_STATE_ACTIVATED:
409                 cmdstr = "pd_mouse_move";
410                 break;
411         case EVENT_STATE_DEACTIVATE:
412                 cmdstr = "pd_mouse_up";
413                 break;
414         default:
415                 return LB_STATUS_ERROR_INVALID;
416         }
417
418         packet = packet_create_noack(cmdstr, "ssdii", package_name(pkg), instance_id(inst), util_timestamp(), event_info->x, event_info->y);
419         if (!packet) {
420                 return LB_STATUS_ERROR_FAULT;
421         }
422
423         return slave_rpc_request_only(slave, package_name(pkg), packet, 0);
424 }
425
426 static int event_pd_consume_cb(enum event_state state, struct event_data *event_info, void *data)
427 {
428         struct script_info *script;
429         struct inst_info *inst = data;
430         const struct pkg_info *pkg;
431         double timestamp;
432         Evas *e;
433
434         pkg = instance_package(inst);
435         if (!pkg) {
436                 return 0;
437         }
438
439         script = instance_pd_script(inst);
440         if (!script) {
441                 return LB_STATUS_ERROR_FAULT;
442         }
443
444         e = script_handler_evas(script);
445         if (!e) {
446                 return LB_STATUS_ERROR_FAULT;
447         }
448
449         timestamp = util_timestamp();
450
451         switch (state) {
452         case EVENT_STATE_ACTIVATE:
453                 script_handler_update_pointer(script, event_info->x, event_info->y, 1);
454                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_DOWN, timestamp);
455                 break;
456         case EVENT_STATE_ACTIVATED:
457                 script_handler_update_pointer(script, event_info->x, event_info->y, -1);
458                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_MOVE, timestamp);
459                 break;
460         case EVENT_STATE_DEACTIVATE:
461                 script_handler_update_pointer(script, event_info->x, event_info->y, 0);
462                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_UP, timestamp);
463                 break;
464         default:
465                 break;
466         }
467         return 0;
468 }
469
470 static struct packet *client_acquire(pid_t pid, int handle, const struct packet *packet) /*!< timestamp, ret */
471 {
472         struct client_node *client;
473         struct packet *result;
474         double timestamp;
475         int ret;
476
477         client = client_find_by_rpc_handle(handle);
478         if (client) {
479                 ErrPrint("Client is already exists %d\n", pid);
480                 ret = LB_STATUS_ERROR_EXIST;
481                 goto out;
482         }
483
484         if (packet_get(packet, "d", &timestamp) != 1) {
485                 ErrPrint("Invalid arguemnt\n");
486                 ret = LB_STATUS_ERROR_INVALID;
487                 goto out;
488         }
489
490         ret = 0;
491         /*!
492          * \note
493          * client_create will invoke the client created callback
494          */
495         client = client_create(pid, handle);
496         if (!client) {
497                 ErrPrint("Failed to create a new client for %d\n", pid);
498                 ret = LB_STATUS_ERROR_FAULT;
499         }
500
501 out:
502         result = packet_create_reply(packet, "i", ret);
503         if (!result) {
504                 ErrPrint("Failed to create a packet\n");
505         }
506
507         return result;
508 }
509
510 static struct packet *cilent_release(pid_t pid, int handle, const struct packet *packet) /*!< pid, ret */
511 {
512         struct client_node *client;
513         struct packet *result;
514         int ret;
515
516         client = client_find_by_rpc_handle(handle);
517         if (!client) {
518                 ErrPrint("Client %d is not exists\n", pid);
519                 ret = LB_STATUS_ERROR_NOT_EXIST;
520                 goto out;
521         }
522
523         client_destroy(client);
524         ret = 0;
525
526 out:
527         result = packet_create_reply(packet, "i", ret);
528         if (!result) {
529                 ErrPrint("Failed to create a packet\n");
530         }
531
532         return result;
533 }
534
535 static int validate_request(const char *pkgname, const char *id, struct inst_info **out_inst, const struct pkg_info **out_pkg)
536 {
537         struct inst_info *inst;
538         const struct pkg_info *pkg;
539
540         inst = package_find_instance_by_id(pkgname, id);
541         if (!inst) {
542                 ErrPrint("Instance is not exists\n");
543                 return LB_STATUS_ERROR_NOT_EXIST;
544         }
545
546         pkg = instance_package(inst);
547         if (!pkg) {
548                 ErrPrint("System error - instance has no package?\n");
549                 return LB_STATUS_ERROR_INVALID;
550         }
551
552         if (package_is_fault(pkg)) {
553                 ErrPrint("Faulted package: %s\n", pkgname);
554                 return LB_STATUS_ERROR_FAULT;
555         }
556
557         if (out_inst) {
558                 *out_inst = inst;
559         }
560
561         if (out_pkg) {
562                 *out_pkg = pkg;
563         }
564
565         return LB_STATUS_SUCCESS;
566 }
567
568 /*!< pid, pkgname, filename, event, timestamp, x, y, ret */
569 static struct packet *client_clicked(pid_t pid, int handle, const struct packet *packet)
570 {
571         struct client_node *client;
572         const char *pkgname;
573         const char *id;
574         const char *event;
575         double timestamp;
576         double x;
577         double y;
578         int ret;
579         struct inst_info *inst = NULL;
580
581         client = client_find_by_rpc_handle(handle);
582         if (!client) {
583                 ErrPrint("Client %d is not exists\n", pid);
584                 goto out;
585         }
586
587         ret = packet_get(packet, "sssddd", &pkgname, &id, &event, &timestamp, &x, &y);
588         if (ret != 6) {
589                 ErrPrint("Parameter is not matched\n");
590                 goto out;
591         }
592
593         /*!
594          * \NOTE:
595          * Trust the package name which are sent by the client.
596          * The package has to be a livebox package name.
597          */
598         ret = validate_request(pkgname, id, &inst, NULL);
599         if (ret == LB_STATUS_SUCCESS) {
600                 (void)instance_clicked(inst, event, timestamp, x, y);
601         }
602
603 out:
604         /*! \note No reply packet */
605         return NULL;
606 }
607
608 static struct packet *client_update_mode(pid_t pid, int handle, const struct packet *packet)
609 {
610         struct packet *result;
611         struct client_node *client;
612         int active_update;
613         const char *pkgname;
614         const char *id;
615         int ret;
616         struct inst_info *inst = NULL;
617
618         client = client_find_by_rpc_handle(handle);
619         if (!client) {
620                 ErrPrint("Client %d is not exists\n", pid);
621                 ret = LB_STATUS_ERROR_INVALID;
622                 goto out;
623         }
624
625         ret = packet_get(packet, "ssi", &pkgname, &id, &active_update);
626         if (ret != 3) {
627                 ErrPrint("Invalid argument\n");
628                 ret = LB_STATUS_ERROR_INVALID;
629                 goto out;
630         }
631
632         ret = validate_request(pkgname, id, &inst, NULL);
633         if (ret == LB_STATUS_SUCCESS) {
634                 /*!
635                  * \note
636                  * Send change update mode request to a slave
637                  */
638                 ret = instance_set_update_mode(inst, active_update);
639         }
640
641 out:
642         result = packet_create_reply(packet, "i", ret);
643         if (!result) {
644                 ErrPrint("Failed to create a packet\n");
645         }
646
647         return result;
648 }
649
650 /* pid, pkgname, filename, emission, source, s, sy, ex, ey, ret */
651 static struct packet *client_text_signal(pid_t pid, int handle, const struct packet *packet)
652 {
653         struct client_node *client;
654         struct packet *result;
655         const char *pkgname;
656         const char *id;
657         const char *emission;
658         const char *source;
659         double sx;
660         double sy;
661         double ex;
662         double ey;
663         struct inst_info *inst = NULL;
664         int ret;
665
666         client = client_find_by_rpc_handle(handle);
667         if (!client) {
668                 ErrPrint("Client %d is not exists\n", pid);
669                 ret = LB_STATUS_ERROR_NOT_EXIST;
670                 goto out;
671         }
672
673         ret = packet_get(packet, "ssssdddd", &pkgname, &id, &emission, &source, &sx, &sy, &ex, &ey);
674         if (ret != 8) {
675                 ErrPrint("Parameter is not matched\n");
676                 ret = LB_STATUS_ERROR_INVALID;
677                 goto out;
678         }
679
680         /*!
681          * \NOTE:
682          * Trust the package name which are sent by the client.
683          * The package has to be a livebox package name.
684          */
685         ret = validate_request(pkgname, id, &inst, NULL);
686         if (ret == LB_STATUS_SUCCESS) {
687                 ret = instance_text_signal_emit(inst, emission, source, sx, sy, ex, ey);
688         }
689
690 out:
691         result = packet_create_reply(packet, "i", ret);
692         if (!result) {
693                 ErrPrint("Failed to create a packet\n");
694         }
695
696         return result;
697 }
698
699 static Eina_Bool lazy_delete_cb(void *data)
700 {
701         struct deleted_item *item = data;
702
703         /*!
704          * Before invoke this callback, the instance is able to already remove this client
705          * So check it again
706          */
707         if (instance_has_client(item->inst, item->client)) {
708                 (void)instance_unicast_deleted_event(item->inst, item->client);
709                 (void)instance_del_client(item->inst, item->client);
710         }
711
712         (void)client_unref(item->client);
713         (void)instance_unref(item->inst);
714         DbgFree(item);
715         return ECORE_CALLBACK_CANCEL;
716 }
717
718 static struct packet *client_delete(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
719 {
720         struct client_node *client;
721         struct packet *result;
722         const char *pkgname;
723         const char *id;
724         struct inst_info *inst = NULL;
725         int ret;
726
727         client = client_find_by_rpc_handle(handle);
728         if (!client) {
729                 ErrPrint("Client %d is not exists\n", pid);
730                 ret = LB_STATUS_ERROR_NOT_EXIST;
731                 goto out;
732         }
733
734         ret = packet_get(packet, "ss", &pkgname, &id);
735         if (ret != 2) {
736                 ErrPrint("Parameter is not matched\n");
737                 ret = LB_STATUS_ERROR_INVALID;
738                 goto out;
739         }
740
741         /*!
742          * \NOTE:
743          * Trust the package name which are sent by the client.
744          * The package has to be a livebox package name.
745          */
746         ret = validate_request(pkgname, id, &inst, NULL);
747         if (ret != LB_STATUS_SUCCESS) {
748                 goto out;
749         }
750
751         if (instance_client(inst) != client) {
752                 if (instance_has_client(inst, client)) {
753                         struct deleted_item *item;
754
755                         item = malloc(sizeof(*item));
756                         if (!item) {
757                                 ErrPrint("Heap: %s\n", strerror(errno));
758                                 ret = LB_STATUS_ERROR_MEMORY;
759                         } else {
760                                 /*!
761                                  * \NOTE:
762                                  * Send DELETED EVENT to the client.
763                                  * after return from this function.
764                                  *
765                                  * Client will prepare the deleted event after get this function's return value.
766                                  * So We have to make a delay to send a deleted event.
767                                  */
768
769                                 item->client = client_ref(client);
770                                 item->inst = instance_ref(inst);
771
772                                 if (!ecore_timer_add(DELAY_TIME, lazy_delete_cb, item)) {
773                                         ErrPrint("Failed to add a delayzed delete callback\n");
774                                         (void)client_unref(client);
775                                         (void)instance_unref(inst);
776                                         DbgFree(item);
777                                         ret = LB_STATUS_ERROR_FAULT;
778                                 } else {
779                                         ret = LB_STATUS_SUCCESS;
780                                 }
781                         }
782                 } else {
783                         ErrPrint("Client has no permission\n");
784                         ret = LB_STATUS_ERROR_PERMISSION;
785                 }
786         } else {
787                 ret = instance_destroy(inst, INSTANCE_DESTROY_DEFAULT);
788         }
789
790 out:
791         result = packet_create_reply(packet, "i", ret);
792         if (!result) {
793                 ErrPrint("Failed to create a packet\n");
794         }
795
796         return result;
797 }
798
799 static struct packet *client_resize(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, w, h, ret */
800 {
801         struct client_node *client;
802         struct packet *result;
803         const char *pkgname;
804         const char *id;
805         int w;
806         int h;
807         struct inst_info *inst = NULL;
808         int ret;
809
810         client = client_find_by_rpc_handle(handle);
811         if (!client) {
812                 ErrPrint("Client %d is not exists\n", pid);
813                 ret = LB_STATUS_ERROR_NOT_EXIST;
814                 goto out;
815         }
816
817         ret = packet_get(packet, "ssii", &pkgname, &id, &w, &h);
818         if (ret != 4) {
819                 ErrPrint("Parameter is not matched\n");
820                 ret = LB_STATUS_ERROR_INVALID;
821                 goto out;
822         }
823
824         DbgPrint("RESIZE: Client request resize to %dx%d (pid: %d, pkgname: %s)\n", w, h, pid, pkgname);
825
826         /*!
827          * \NOTE:
828          * Trust the package name which are sent by the client.
829          * The package has to be a livebox package name.
830          */
831         ret = validate_request(pkgname, id, &inst, NULL);
832         if (ret != LB_STATUS_SUCCESS) {
833                 goto out;
834         }
835
836         if (instance_client(inst) != client) {
837                 ret = LB_STATUS_ERROR_PERMISSION;
838         } else {
839                 ret = instance_resize(inst, w, h);
840         }
841
842 out:
843         result = packet_create_reply(packet, "i", ret);
844         if (!result) {
845                 ErrPrint("Failed to create a packet\n");
846         }
847
848         return result;
849 }
850
851 static struct packet *client_new(pid_t pid, int handle, const struct packet *packet) /* pid, timestamp, pkgname, content, cluster, category, period, ret */
852 {
853         struct client_node *client;
854         struct packet *result;
855         const char *pkgname;
856         const char *content;
857         const char *cluster;
858         const char *category;
859         double period;
860         double timestamp;
861         int ret;
862         struct pkg_info *info;
863         int width;
864         int height;
865         char *lbid;
866         char *mainappid;
867
868         client = client_find_by_rpc_handle(handle);
869         if (!client) {
870                 ErrPrint("Client %d is not exists\n", pid);
871                 ret = LB_STATUS_ERROR_NOT_EXIST;
872                 goto out;
873         }
874
875         ret = packet_get(packet, "dssssdii", &timestamp, &pkgname, &content, &cluster, &category, &period, &width, &height);
876         if (ret != 8) {
877                 ErrPrint("Parameter is not matched\n");
878                 ret = LB_STATUS_ERROR_INVALID;
879                 goto out;
880         }
881
882         DbgPrint("pid[%d] period[%lf] pkgname[%s] content[%s] cluster[%s] category[%s] period[%lf]\n",
883                                                 pid, timestamp, pkgname, content, cluster, category, period);
884
885         lbid = package_lb_pkgname(pkgname);
886         if (!lbid) {
887                 ErrPrint("This %s has no livebox package\n", pkgname);
888                 ret = LB_STATUS_ERROR_INVALID;
889                 goto out;
890         }
891
892         mainappid = livebox_service_mainappid(lbid);
893         if (!package_is_enabled(mainappid)) {
894                 DbgFree(mainappid);
895                 DbgFree(lbid);
896                 ret = LB_STATUS_ERROR_DISABLED;
897                 goto out;
898         }
899         DbgFree(mainappid);
900
901         info = package_find(lbid);
902         if (!info) {
903                 char *pkgid;
904                 pkgid = livebox_service_appid(lbid);
905                 if (!pkgid) {
906                         DbgFree(mainappid);
907                         DbgFree(lbid);
908                         ret = LB_STATUS_ERROR_FAULT;
909                         goto out;
910                 }
911
912                 info = package_create(pkgid, lbid);
913                 DbgFree(pkgid);
914         }
915
916         if (!info) {
917                 ret = LB_STATUS_ERROR_FAULT;
918         } else if (package_is_fault(info)) {
919                 ret = LB_STATUS_ERROR_FAULT;
920         } else if (util_free_space(IMAGE_PATH) < MINIMUM_SPACE) {
921                 ErrPrint("Not enough space\n");
922                 ret = LB_STATUS_ERROR_NO_SPACE;
923         } else {
924                 struct inst_info *inst;
925
926                 if (period > 0.0f && period < MINIMUM_PERIOD) {
927                         period = MINIMUM_PERIOD;
928                 }
929
930                 inst = instance_create(client, timestamp, lbid, content, cluster, category, period, width, height);
931                 /*!
932                  * \note
933                  * Using the "inst" without validate its value is at my disposal. ;)
934                  */
935                 ret = inst ? 0 : LB_STATUS_ERROR_FAULT;
936         }
937
938         DbgFree(lbid);
939
940 out:
941         result = packet_create_reply(packet, "i", ret);
942         if (!result) {
943                 ErrPrint("Failed to create a packet\n");
944         }
945
946         return result;
947 }
948
949 static struct packet *client_change_visibility(pid_t pid, int handle, const struct packet *packet)
950 {
951         struct client_node *client;
952         const char *pkgname;
953         const char *id;
954         enum livebox_visible_state state;
955         int ret;
956         struct inst_info *inst = NULL;
957
958         client = client_find_by_rpc_handle(handle);
959         if (!client) {
960                 ErrPrint("Client %d is not exists\n", pid);
961                 ret = LB_STATUS_ERROR_NOT_EXIST;
962                 goto out;
963         }
964
965         ret = packet_get(packet, "ssi", &pkgname, &id, (int *)&state);
966         if (ret != 3) {
967                 ErrPrint("Parameter is not matched\n");
968                 ret = LB_STATUS_ERROR_INVALID;
969                 goto out;
970         }
971
972         /*!
973          * \NOTE:
974          * Trust the package name which are sent by the client.
975          * The package has to be a livebox package name.
976          */
977         ret = validate_request(pkgname, id, &inst, NULL);
978         if (ret != LB_STATUS_SUCCESS) {
979                 goto out;
980         }
981
982         if (instance_client(inst) != client) {
983                 ret = LB_STATUS_ERROR_PERMISSION;
984         } else {
985                 ret = instance_set_visible_state(inst, state);
986         }
987
988 out:
989         /*! \note No reply packet */
990         return NULL;
991 }
992
993 static struct packet *client_set_period(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, period, ret */
994 {
995         struct client_node *client;
996         struct packet *result;
997         const char *pkgname;
998         const char *id;
999         double period;
1000         int ret;
1001         struct inst_info *inst = NULL;
1002
1003         client = client_find_by_rpc_handle(handle);
1004         if (!client) {
1005                 ErrPrint("Client %d is not exists\n", pid);
1006                 ret = LB_STATUS_ERROR_NOT_EXIST;
1007                 goto out;
1008         }
1009
1010         ret = packet_get(packet, "ssd", &pkgname, &id, &period);
1011         if (ret != 3) {
1012                 ErrPrint("Parameter is not matched\n");
1013                 ret = LB_STATUS_ERROR_INVALID;
1014                 goto out;
1015         }
1016
1017         DbgPrint("pid[%d] pkgname[%s] period[%lf]\n", pid, pkgname, period);
1018
1019         /*!
1020          * \NOTE:
1021          * Trust the package name which are sent by the client.
1022          * The package has to be a livebox package name.
1023          */
1024         ret = validate_request(pkgname, id, &inst, NULL);
1025         if (ret != LB_STATUS_SUCCESS) {
1026                 goto out;
1027         }
1028
1029         if (instance_client(inst) != client) {
1030                 ret = LB_STATUS_ERROR_PERMISSION;
1031         } else {
1032                 ret = instance_set_period(inst, period);
1033         }
1034
1035 out:
1036         result = packet_create_reply(packet, "i", ret);
1037         if (!result) {
1038                 ErrPrint("Failed to create a packet\n");
1039         }
1040
1041         return result;
1042 }
1043
1044 static struct packet *client_change_group(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, cluster, category, ret */
1045 {
1046         struct client_node *client;
1047         struct packet *result;
1048         const char *pkgname;
1049         const char *id;
1050         const char *cluster;
1051         const char *category;
1052         struct inst_info *inst = NULL;
1053         int ret;
1054
1055         client = client_find_by_rpc_handle(handle);
1056         if (!client) {
1057                 ErrPrint("Client %d is not exists\n", pid);
1058                 ret = LB_STATUS_ERROR_NOT_EXIST;
1059                 goto out;
1060         }
1061
1062         ret = packet_get(packet, "ssss", &pkgname, &id, &cluster, &category);
1063         if (ret != 4) {
1064                 ErrPrint("Parameter is not matched\n");
1065                 ret = LB_STATUS_ERROR_INVALID;
1066                 goto out;
1067         }
1068
1069         DbgPrint("pid[%d] pkgname[%s] cluster[%s] category[%s]\n", pid, pkgname, cluster, category);
1070
1071         /*!
1072          * \NOTE:
1073          * Trust the package name which are sent by the client.
1074          * The package has to be a livebox package name.
1075          */
1076         ret = validate_request(pkgname, id, &inst, NULL);
1077         if (ret != LB_STATUS_SUCCESS) {
1078                 goto out;
1079         }
1080
1081         if (instance_client(inst) != client) {
1082                 ret = LB_STATUS_ERROR_PERMISSION;
1083         } else {
1084                 ret = instance_change_group(inst, cluster, category);
1085         }
1086
1087 out:
1088         result = packet_create_reply(packet, "i", ret);
1089         if (!result) {
1090                 ErrPrint("Failed to create a packet\n");
1091         }
1092
1093         return result;
1094 }
1095
1096 static struct packet *client_pd_mouse_enter(pid_t pid, int handle, const struct packet *packet)
1097 {
1098         struct client_node *client;
1099         const char *pkgname;
1100         const char *id;
1101         int ret;
1102         double timestamp;
1103         int x;
1104         int y;
1105         struct inst_info *inst = NULL;
1106         const struct pkg_info *pkg = NULL;
1107
1108         client = client_find_by_rpc_handle(handle);
1109         if (!client) {
1110                 ErrPrint("Client %d is not exists\n", pid);
1111                 ret = LB_STATUS_ERROR_NOT_EXIST;
1112                 goto out;
1113         }
1114
1115         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1116         if (ret != 5) {
1117                 ErrPrint("Invalid parameter\n");
1118                 ret = LB_STATUS_ERROR_INVALID;
1119                 goto out;
1120         }
1121
1122         ret = validate_request(pkgname, id, &inst, &pkg);
1123         if (ret != LB_STATUS_SUCCESS) {
1124                 goto out;
1125         }
1126
1127         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1128                 ret = forward_pd_event_packet(pkg, inst, packet);
1129         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1130                 struct script_info *script;
1131                 Evas *e;
1132
1133                 script = instance_pd_script(inst);
1134                 if (!script) {
1135                         ret = LB_STATUS_ERROR_FAULT;
1136                         goto out;
1137                 }
1138
1139                 e = script_handler_evas(script);
1140                 if (!e) {
1141                         ret = LB_STATUS_ERROR_FAULT;
1142                         goto out;
1143                 }
1144
1145                 script_handler_update_pointer(script, x, y, -1);
1146                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_IN, timestamp);
1147                 ret = 0;
1148         } else {
1149                 ErrPrint("Unsupported package\n");
1150                 ret = LB_STATUS_ERROR_INVALID;
1151         }
1152
1153 out:
1154         /*! \note No reply packet */
1155         return NULL;
1156 }
1157
1158 static struct packet *client_pd_mouse_leave(pid_t pid, int handle, const struct packet *packet)
1159 {
1160         struct client_node *client;
1161         const char *pkgname;
1162         const char *id;
1163         int ret;
1164         double timestamp;
1165         int x;
1166         int y;
1167         struct inst_info *inst = NULL;
1168         const struct pkg_info *pkg = NULL;
1169
1170         client = client_find_by_rpc_handle(handle);
1171         if (!client) {
1172                 ErrPrint("Client %d is not exists\n", pid);
1173                 ret = LB_STATUS_ERROR_NOT_EXIST;
1174                 goto out;
1175         }
1176
1177         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1178         if (ret != 5) {
1179                 ErrPrint("Parameter is not matched\n");
1180                 ret = LB_STATUS_ERROR_INVALID;
1181                 goto out;
1182         }
1183
1184         ret = validate_request(pkgname, id, &inst, &pkg);
1185         if (ret != LB_STATUS_SUCCESS) {
1186                 goto out;
1187         }
1188
1189         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1190                 ret = forward_pd_event_packet(pkg, inst, packet);
1191         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1192                 struct script_info *script;
1193                 Evas *e;
1194
1195                 script = instance_pd_script(inst);
1196                 if (!script) {
1197                         ret = LB_STATUS_ERROR_FAULT;
1198                         goto out;
1199                 }
1200
1201                 e = script_handler_evas(script);
1202                 if (!e) {
1203                         ret = LB_STATUS_ERROR_FAULT;
1204                         goto out;
1205                 }
1206
1207                 script_handler_update_pointer(script, x, y, -1);
1208                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_OUT, timestamp);
1209                 ret = 0;
1210         } else {
1211                 ErrPrint("Unsupported package\n");
1212                 ret = LB_STATUS_ERROR_INVALID;
1213         }
1214
1215 out:
1216         /*! \note No reply packet */
1217         return NULL;
1218 }
1219
1220 static struct packet *client_pd_mouse_down(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, id, width, height, timestamp, x, y, ret */
1221 {
1222         struct client_node *client;
1223         const char *pkgname;
1224         const char *id;
1225         int ret;
1226         double timestamp;
1227         int x;
1228         int y;
1229         struct inst_info *inst = NULL;
1230         const struct pkg_info *pkg = NULL;
1231
1232         client = client_find_by_rpc_handle(handle);
1233         if (!client) {
1234                 ErrPrint("Client %d is not exists\n", pid);
1235                 ret = LB_STATUS_ERROR_NOT_EXIST;
1236                 goto out;
1237         }
1238
1239         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1240         if (ret != 5) {
1241                 ErrPrint("Parameter is not matched\n");
1242                 ret = LB_STATUS_ERROR_INVALID;
1243                 goto out;
1244         }
1245
1246         ret = validate_request(pkgname, id, &inst, &pkg);
1247         if (ret != LB_STATUS_SUCCESS) {
1248                 goto out;
1249         }
1250
1251         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1252                 ret = forward_pd_event_packet(pkg, inst, packet);
1253         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1254                 struct script_info *script;
1255                 Evas *e;
1256
1257                 script = instance_pd_script(inst);
1258                 if (!script) {
1259                         ret = LB_STATUS_ERROR_FAULT;
1260                         goto out;
1261                 }
1262
1263                 e = script_handler_evas(script);
1264                 if (!e) {
1265                         ret = LB_STATUS_ERROR_FAULT;
1266                         goto out;
1267                 }
1268
1269                 script_handler_update_pointer(script, x, y, 1);
1270                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_DOWN, timestamp);
1271                 ret = 0;
1272         } else {
1273                 ErrPrint("Unsupported package\n");
1274                 ret = LB_STATUS_ERROR_INVALID;
1275         }
1276
1277 out:
1278         /*! \note No reply packet */
1279         return NULL;
1280 }
1281
1282 static struct packet *client_pd_mouse_up(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1283 {
1284         struct client_node *client;
1285         const char *pkgname;
1286         const char *id;
1287         int ret;
1288         double timestamp;
1289         int x;
1290         int y;
1291         struct inst_info *inst = NULL;
1292         const struct pkg_info *pkg = NULL;
1293
1294         client = client_find_by_rpc_handle(handle);
1295         if (!client) {
1296                 ErrPrint("Client %d is not exists\n", pid);
1297                 ret = LB_STATUS_ERROR_NOT_EXIST;
1298                 goto out;
1299         }
1300
1301         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1302         if (ret != 5) {
1303                 ErrPrint("Parameter is not matched\n");
1304                 ret = LB_STATUS_ERROR_INVALID;
1305                 goto out;
1306         }
1307
1308         ret = validate_request(pkgname, id, &inst, &pkg);
1309         if (ret != LB_STATUS_SUCCESS) {
1310                 goto out;
1311         }
1312
1313         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1314                 ret = forward_pd_event_packet(pkg, inst, packet);
1315         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1316                 struct script_info *script;
1317                 Evas *e;
1318
1319                 script = instance_pd_script(inst);
1320                 if (!script) {
1321                         ret = LB_STATUS_ERROR_FAULT;
1322                         goto out;
1323                 }
1324
1325                 e = script_handler_evas(script);
1326                 if (!e) {
1327                         ret = LB_STATUS_ERROR_FAULT;
1328                         goto out;
1329                 }
1330
1331                 script_handler_update_pointer(script, x, y, 0);
1332                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_UP, timestamp);
1333                 ret = 0;
1334         } else {
1335                 ErrPrint("Unsupported package\n");
1336                 ret = LB_STATUS_ERROR_INVALID;
1337         }
1338
1339 out:
1340         /*! \note No reply packet */
1341         return NULL;
1342 }
1343
1344 static struct packet *client_pd_mouse_move(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1345 {
1346         struct client_node *client;
1347         const char *pkgname;
1348         const char *id;
1349         int ret;
1350         double timestamp;
1351         int x;
1352         int y;
1353         struct inst_info *inst = NULL;
1354         const struct pkg_info *pkg = NULL;
1355
1356         client = client_find_by_rpc_handle(handle);
1357         if (!client) {
1358                 ErrPrint("Client %d is not exists\n", pid);
1359                 ret = LB_STATUS_ERROR_NOT_EXIST;
1360                 goto out;
1361         }
1362
1363         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1364         if (ret != 5) {
1365                 ErrPrint("Parameter is not matched\n");
1366                 ret = LB_STATUS_ERROR_INVALID;
1367                 goto out;
1368         }
1369
1370         ret = validate_request(pkgname, id, &inst, &pkg);
1371         if (ret != LB_STATUS_SUCCESS) {
1372                 goto out;
1373         }
1374
1375         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1376                 ret = forward_pd_event_packet(pkg, inst, packet);
1377         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1378                 struct script_info *script;
1379                 Evas *e;
1380
1381                 script = instance_pd_script(inst);
1382                 if (!script) {
1383                         ret = LB_STATUS_ERROR_FAULT;
1384                         goto out;
1385                 }
1386
1387                 e = script_handler_evas(script);
1388                 if (!e) {
1389                         ret = LB_STATUS_ERROR_FAULT;
1390                         goto out;
1391                 }
1392
1393                 script_handler_update_pointer(script, x, y, -1);
1394                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_MOVE, timestamp);
1395                 ret = 0;
1396         } else {
1397                 ErrPrint("Unsupported package\n");
1398                 ret = LB_STATUS_ERROR_INVALID;
1399         }
1400
1401 out:
1402         /*! \note No reply packet */
1403         return NULL;
1404 }
1405
1406 static struct packet *client_lb_mouse_move(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1407 {
1408         struct client_node *client;
1409         const char *pkgname;
1410         const char *id;
1411         int ret;
1412         double timestamp;
1413         int x;
1414         int y;
1415         struct inst_info *inst = NULL;
1416         const struct pkg_info *pkg = NULL;
1417
1418         client = client_find_by_rpc_handle(handle);
1419         if (!client) {
1420                 ErrPrint("Client %d is not exists\n", pid);
1421                 ret = LB_STATUS_ERROR_NOT_EXIST;
1422                 goto out;
1423         }
1424
1425         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1426         if (ret != 5) {
1427                 ErrPrint("Parameter is not matched\n");
1428                 ret = LB_STATUS_ERROR_INVALID;
1429                 goto out;
1430         }
1431
1432         ret = validate_request(pkgname, id, &inst, &pkg);
1433         if (ret != LB_STATUS_SUCCESS) {
1434                 goto out;
1435         }
1436
1437         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1438                 ret = forward_lb_event_packet(pkg, inst, packet);
1439         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1440                 struct script_info *script;
1441                 Evas *e;
1442
1443                 script = instance_lb_script(inst);
1444                 if (!script) {
1445                         ret = LB_STATUS_ERROR_FAULT;
1446                         goto out;
1447                 }
1448
1449                 e = script_handler_evas(script);
1450                 if (!e) {
1451                         ret = LB_STATUS_ERROR_FAULT;
1452                         goto out;
1453                 }
1454
1455                 script_handler_update_pointer(script, x, y, -1);
1456                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_MOVE, timestamp);
1457                 ret = 0;
1458         } else {
1459                 ErrPrint("Unsupported package\n");
1460                 ret = LB_STATUS_ERROR_INVALID;
1461         }
1462
1463 out:
1464         /*! \note No reply packet */
1465         return NULL;
1466 }
1467
1468 static int inst_del_cb(struct inst_info *inst, void *data)
1469 {
1470         (void)event_deactivate(data, inst);
1471         return -1; /* Delete this callback */
1472 }
1473
1474 static struct packet *client_lb_mouse_set(pid_t pid, int handle, const struct packet *packet)
1475 {
1476         struct client_node *client;
1477         const char *pkgname;
1478         const char *id;
1479         int ret;
1480         double timestamp;
1481         int x;
1482         int y;
1483         struct inst_info *inst = NULL;
1484         const struct pkg_info *pkg = NULL;
1485
1486         client = client_find_by_rpc_handle(handle);
1487         if (!client) {
1488                 ErrPrint("Client %d is not exists\n", pid);
1489                 ret = LB_STATUS_ERROR_NOT_EXIST;
1490                 goto out;
1491         }
1492
1493         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1494         if (ret != 5) {
1495                 ErrPrint("Parameter is not matched\n");
1496                 ret = LB_STATUS_ERROR_INVALID;
1497                 goto out;
1498         }
1499
1500         ret = validate_request(pkgname, id, &inst, &pkg);
1501         if (ret != LB_STATUS_SUCCESS) {
1502                 goto out;
1503         }
1504
1505         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1506                 ret = event_activate(x, y, event_lb_route_cb, inst);
1507                 if (ret == 0) {
1508                         instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, event_lb_route_cb);
1509                 }
1510         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1511                 ret = event_activate(x, y, event_lb_consume_cb, inst);
1512                 if (ret == 0) {
1513                         instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, event_lb_consume_cb);
1514                 }
1515         } else {
1516                 ErrPrint("Unsupported package\n");
1517                 ret = LB_STATUS_ERROR_INVALID;
1518         }
1519 out:
1520         return NULL;
1521 }
1522
1523 static struct packet *client_lb_mouse_unset(pid_t pid, int handle, const struct packet *packet)
1524 {
1525         struct client_node *client;
1526         const char *pkgname;
1527         const char *id;
1528         int ret;
1529         double timestamp;
1530         int x;
1531         int y;
1532         struct inst_info *inst = NULL;
1533         const struct pkg_info *pkg = NULL;
1534
1535         client = client_find_by_rpc_handle(handle);
1536         if (!client) {
1537                 ErrPrint("Client %d is not exists\n", pid);
1538                 ret = LB_STATUS_ERROR_NOT_EXIST;
1539                 goto out;
1540         }
1541
1542         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1543         if (ret != 5) {
1544                 ErrPrint("Parameter is not matched\n");
1545                 ret = LB_STATUS_ERROR_INVALID;
1546                 goto out;
1547         }
1548
1549         ret = validate_request(pkgname, id, &inst, &pkg);
1550         if (ret != LB_STATUS_SUCCESS) {
1551                 goto out;
1552         }
1553
1554         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1555                 ret = event_deactivate(event_lb_route_cb, inst);
1556                 if (ret == 0) {
1557                         instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb);
1558                 }
1559         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1560                 ret = event_deactivate(event_lb_consume_cb, inst);
1561                 if (ret == 0) {
1562                         instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb);
1563                 }
1564         } else {
1565                 ErrPrint("Unsupported package\n");
1566                 ret = LB_STATUS_ERROR_INVALID;
1567         }
1568 out:
1569         return NULL;
1570 }
1571
1572 static struct packet *client_pd_mouse_set(pid_t pid, int handle, const struct packet *packet)
1573 {
1574         struct client_node *client;
1575         const char *pkgname;
1576         const char *id;
1577         int ret;
1578         double timestamp;
1579         int x;
1580         int y;
1581         struct inst_info *inst = NULL;
1582         const struct pkg_info *pkg = NULL;
1583
1584         client = client_find_by_rpc_handle(handle);
1585         if (!client) {
1586                 ErrPrint("Client %d is not exists\n", pid);
1587                 ret = LB_STATUS_ERROR_NOT_EXIST;
1588                 goto out;
1589         }
1590
1591         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1592         if (ret != 5) {
1593                 ErrPrint("Parameter is not matched\n");
1594                 ret = LB_STATUS_ERROR_INVALID;
1595                 goto out;
1596         }
1597
1598         ret = validate_request(pkgname, id, &inst, &pkg);
1599         if (ret != LB_STATUS_SUCCESS) {
1600                 goto out;
1601         }
1602
1603         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1604                 ret = event_activate(x, y, event_pd_route_cb, inst);
1605                 if (ret == 0) {
1606                         instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, event_pd_route_cb);
1607                 }
1608         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1609                 ret = event_activate(x, y, event_pd_consume_cb, inst);
1610                 if (ret == 0) {
1611                         instance_event_callback_add(inst, INSTANCE_EVENT_DESTROY, inst_del_cb, event_pd_consume_cb);
1612                 }
1613         } else {
1614                 ErrPrint("Unsupported package\n");
1615                 ret = LB_STATUS_ERROR_INVALID;
1616         }
1617
1618 out:
1619         return NULL;
1620 }
1621
1622 static struct packet *client_pd_mouse_unset(pid_t pid, int handle, const struct packet *packet)
1623 {
1624         struct client_node *client;
1625         const char *pkgname;
1626         const char *id;
1627         int ret;
1628         double timestamp;
1629         int x;
1630         int y;
1631         struct inst_info *inst = NULL;
1632         const struct pkg_info *pkg = NULL;
1633
1634         client = client_find_by_rpc_handle(handle);
1635         if (!client) {
1636                 ErrPrint("Client %d is not exists\n", pid);
1637                 ret = LB_STATUS_ERROR_NOT_EXIST;
1638                 goto out;
1639         }
1640
1641         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1642         if (ret != 5) {
1643                 ErrPrint("Parameter is not matched\n");
1644                 ret = LB_STATUS_ERROR_INVALID;
1645                 goto out;
1646         }
1647
1648         ret = validate_request(pkgname, id, &inst, &pkg);
1649         if (ret != LB_STATUS_SUCCESS) {
1650                 goto out;
1651         }
1652
1653         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1654                 ret = event_deactivate(event_pd_route_cb, inst);
1655                 if (ret == 0) {
1656                         instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb);
1657                 }
1658         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1659                 ret = event_deactivate(event_pd_consume_cb, inst);
1660                 if (ret == 0) {
1661                         instance_event_callback_del(inst, INSTANCE_EVENT_DESTROY, inst_del_cb);
1662                 }
1663         } else {
1664                 ErrPrint("Unsupported package\n");
1665                 ret = LB_STATUS_ERROR_INVALID;
1666         }
1667 out:
1668         return NULL;
1669 }
1670
1671 static struct packet *client_lb_mouse_enter(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1672 {
1673         struct client_node *client;
1674         const char *pkgname;
1675         const char *id;
1676         int ret;
1677         double timestamp;
1678         int x;
1679         int y;
1680         struct inst_info *inst = NULL;
1681         const struct pkg_info *pkg = NULL;
1682
1683         client = client_find_by_rpc_handle(handle);
1684         if (!client) {
1685                 ErrPrint("Client %d is not exists\n", pid);
1686                 ret = LB_STATUS_ERROR_NOT_EXIST;
1687                 goto out;
1688         }
1689
1690         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1691         if (ret != 5) {
1692                 ErrPrint("Parameter is not matched\n");
1693                 ret = LB_STATUS_ERROR_INVALID;
1694                 goto out;
1695         }
1696
1697         ret = validate_request(pkgname, id, &inst, &pkg);
1698         if (ret != LB_STATUS_SUCCESS) {
1699                 goto out;
1700         }
1701
1702         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1703                 ret = forward_lb_event_packet(pkg, inst, packet);
1704         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1705                 struct script_info *script;
1706                 Evas *e;
1707
1708                 script = instance_lb_script(inst);
1709                 if (!script) {
1710                         ret = LB_STATUS_ERROR_FAULT;
1711                         goto out;
1712                 }
1713
1714                 e = script_handler_evas(script);
1715                 if (!e) {
1716                         ret = LB_STATUS_ERROR_FAULT;
1717                         goto out;
1718                 }
1719
1720                 script_handler_update_pointer(script, x, y, -1);
1721                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_IN, timestamp);
1722                 ret = 0;
1723         } else {
1724                 ErrPrint("Unsupported package\n");
1725                 ret = LB_STATUS_ERROR_INVALID;
1726         }
1727
1728 out:
1729         /*! \note No reply packet */
1730         return NULL;
1731 }
1732
1733 static struct packet *client_lb_mouse_leave(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1734 {
1735         struct client_node *client;
1736         const char *pkgname;
1737         const char *id;
1738         int ret;
1739         double timestamp;
1740         int x;
1741         int y;
1742         struct inst_info *inst = NULL;
1743         const struct pkg_info *pkg = NULL;
1744
1745         client = client_find_by_rpc_handle(handle);
1746         if (!client) {
1747                 ErrPrint("Client %d is not exists\n", pid);
1748                 ret = LB_STATUS_ERROR_NOT_EXIST;
1749                 goto out;
1750         }
1751
1752         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1753         if (ret != 5) {
1754                 ErrPrint("Parameter is not matched\n");
1755                 ret = LB_STATUS_ERROR_INVALID;
1756                 goto out;
1757         }
1758
1759         ret = validate_request(pkgname, id, &inst, &pkg);
1760         if (ret != LB_STATUS_SUCCESS) {
1761                 goto out;
1762         }
1763
1764         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1765                 ret = forward_lb_event_packet(pkg, inst, packet);
1766         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1767                 struct script_info *script;
1768                 Evas *e;
1769
1770                 script = instance_lb_script(inst);
1771                 if (!script) {
1772                         ret = LB_STATUS_ERROR_FAULT;
1773                         goto out;
1774                 }
1775
1776                 e = script_handler_evas(script);
1777                 if (!e) {
1778                         ret = LB_STATUS_ERROR_FAULT;
1779                         goto out;
1780                 }
1781
1782                 script_handler_update_pointer(script, x, y, -1);
1783                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_OUT, timestamp);
1784                 ret = 0;
1785         } else {
1786                 ErrPrint("Unsupported package\n");
1787                 ret = LB_STATUS_ERROR_INVALID;
1788         }
1789
1790 out:
1791         /*! \note No reply packet */
1792         return NULL;
1793 }
1794
1795 static struct packet *client_lb_mouse_down(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1796 {
1797         struct client_node *client;
1798         const char *pkgname;
1799         const char *id;
1800         int ret;
1801         double timestamp;
1802         int x;
1803         int y;
1804         struct inst_info *inst = NULL;
1805         const struct pkg_info *pkg = NULL;
1806
1807         client = client_find_by_rpc_handle(handle);
1808         if (!client) {
1809                 ErrPrint("Client %d is not exists\n", pid);
1810                 ret = LB_STATUS_ERROR_NOT_EXIST;
1811                 goto out;
1812         }
1813
1814         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1815         if (ret != 5) {
1816                 ErrPrint("Parameter is not matched\n");
1817                 ret = LB_STATUS_ERROR_INVALID;
1818                 goto out;
1819         }
1820
1821         ret = validate_request(pkgname, id, &inst, &pkg);
1822         if (ret != LB_STATUS_SUCCESS) {
1823                 goto out;
1824         }
1825
1826         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1827                 ret = forward_lb_event_packet(pkg, inst, packet);
1828         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1829                 struct script_info *script;
1830                 Evas *e;
1831
1832                 script = instance_lb_script(inst);
1833                 if (!script) {
1834                         ret = LB_STATUS_ERROR_FAULT;
1835                         goto out;
1836                 }
1837
1838                 e = script_handler_evas(script);
1839                 if (!e) {
1840                         ret = LB_STATUS_ERROR_FAULT;
1841                         goto out;
1842                 }
1843
1844                 script_handler_update_pointer(script, x, y, 1);
1845                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_DOWN, timestamp);
1846                 ret = 0;
1847         } else {
1848                 ErrPrint("Unsupported package\n");
1849                 ret = LB_STATUS_ERROR_INVALID;
1850         }
1851
1852 out:
1853         /*! \note No reply packet */
1854         return NULL;
1855 }
1856
1857 static struct packet *client_lb_mouse_up(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
1858 {
1859         struct client_node *client;
1860         const char *pkgname;
1861         const char *id;
1862         int ret;
1863         double timestamp;
1864         int x;
1865         int y;
1866         struct inst_info *inst = NULL;
1867         const struct pkg_info *pkg = NULL;
1868
1869         client = client_find_by_rpc_handle(handle);
1870         if (!client) {
1871                 ErrPrint("Client %d is not exists\n", pid);
1872                 ret = LB_STATUS_ERROR_NOT_EXIST;
1873                 goto out;
1874         }
1875
1876         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1877         if (ret != 5) {
1878                 ErrPrint("Parameter is not matched\n");
1879                 ret = LB_STATUS_ERROR_INVALID;
1880                 goto out;
1881         }
1882
1883         ret = validate_request(pkgname, id, &inst, &pkg);
1884         if (ret != LB_STATUS_SUCCESS) {
1885                 goto out;
1886         }
1887
1888         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1889                 ret = forward_lb_event_packet(pkg, inst, packet);
1890         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1891                 struct script_info *script;
1892                 Evas *e;
1893
1894                 script = instance_lb_script(inst);
1895                 if (!script) {
1896                         ret = LB_STATUS_ERROR_FAULT;
1897                         goto out;
1898                 }
1899
1900                 e = script_handler_evas(script);
1901                 if (!e) {
1902                         ret = LB_STATUS_ERROR_FAULT;
1903                         goto out;
1904                 }
1905
1906                 script_handler_update_pointer(script, x, y, 0);
1907                 script_handler_feed_event(script, LB_SCRIPT_MOUSE_UP, timestamp);
1908                 ret = 0;
1909         } else {
1910                 ErrPrint("Unsupported package\n");
1911                 ret = LB_STATUS_ERROR_INVALID;
1912         }
1913
1914 out:
1915         /*! \note No reply packet */
1916         return NULL;
1917 }
1918
1919 static struct packet *client_pd_access_action_up(pid_t pid, int handle, const struct packet *packet)
1920 {
1921         struct packet *result;
1922         struct client_node *client;
1923         const char *pkgname;
1924         const char *id;
1925         int ret;
1926         double timestamp;
1927         int x;
1928         int y;
1929         struct inst_info *inst = NULL;
1930         const struct pkg_info *pkg = NULL;
1931
1932         client = client_find_by_rpc_handle(handle);
1933         if (!client) {
1934                 ErrPrint("Client %d is not exists\n", pid);
1935                 ret = LB_STATUS_ERROR_NOT_EXIST;
1936                 goto out;
1937         }
1938
1939         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
1940         if (ret != 5) {
1941                 ErrPrint("Invalid parameter\n");
1942                 ret = LB_STATUS_ERROR_INVALID;
1943                 goto out;
1944         }
1945
1946         ret = validate_request(pkgname, id, &inst, &pkg);
1947         if (ret != LB_STATUS_SUCCESS) {
1948                 goto out;
1949         }
1950
1951         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1952                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
1953         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1954                 struct script_info *script;
1955                 Evas *e;
1956
1957                 script = instance_pd_script(inst);
1958                 if (!script) {
1959                         ret = LB_STATUS_ERROR_FAULT;
1960                         goto out;
1961                 }
1962
1963                 e = script_handler_evas(script);
1964                 if (!e) {
1965                         ret = LB_STATUS_ERROR_FAULT;
1966                         goto out;
1967                 }
1968
1969                 script_handler_update_pointer(script, x, y, 0);
1970                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_ACTION, timestamp);
1971                 if (ret >= 0) {
1972                         struct access_cbdata *cbdata;
1973
1974                         cbdata = malloc(sizeof(*cbdata));
1975                         if (!cbdata) {
1976                                 ret = LB_STATUS_ERROR_MEMORY;
1977                         } else {
1978                                 cbdata->inst = instance_ref(inst);
1979                                 cbdata->status = ret;
1980
1981                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
1982                                         (void)instance_unref(cbdata->inst);
1983                                         DbgFree(cbdata);
1984                                         ret = LB_STATUS_ERROR_FAULT;
1985                                 } else {
1986                                         ret = LB_STATUS_SUCCESS;
1987                                 }
1988                         }
1989                 }
1990         } else {
1991                 ErrPrint("Unsupported package\n");
1992                 ret = LB_STATUS_ERROR_INVALID;
1993         }
1994
1995 out:
1996         result = packet_create_reply(packet, "i", ret);
1997         if (!result) {
1998                 ErrPrint("Failed to create a reply packet\n");
1999         }
2000
2001         return result;
2002 }
2003
2004 static struct packet *client_pd_access_action_down(pid_t pid, int handle, const struct packet *packet)
2005 {
2006         struct packet *result;
2007         struct client_node *client;
2008         const char *pkgname;
2009         const char *id;
2010         int ret;
2011         double timestamp;
2012         int x;
2013         int y;
2014         struct inst_info *inst = NULL;
2015         const struct pkg_info *pkg = NULL;
2016
2017         client = client_find_by_rpc_handle(handle);
2018         if (!client) {
2019                 ErrPrint("Client %d is not exists\n", pid);
2020                 ret = LB_STATUS_ERROR_NOT_EXIST;
2021                 goto out;
2022         }
2023
2024         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2025         if (ret != 5) {
2026                 ErrPrint("Invalid parameter\n");
2027                 ret = LB_STATUS_ERROR_INVALID;
2028                 goto out;
2029         }
2030
2031         ret = validate_request(pkgname, id, &inst, &pkg);
2032         if (ret != LB_STATUS_SUCCESS) {
2033                 goto out;
2034         }
2035
2036         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2037                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2038         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2039                 struct script_info *script;
2040                 Evas *e;
2041
2042                 script = instance_pd_script(inst);
2043                 if (!script) {
2044                         ret = LB_STATUS_ERROR_FAULT;
2045                         goto out;
2046                 }
2047
2048                 e = script_handler_evas(script);
2049                 if (!e) {
2050                         ret = LB_STATUS_ERROR_FAULT;
2051                         goto out;
2052                 }
2053
2054                 script_handler_update_pointer(script, x, y, 1);
2055                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_ACTION, timestamp);
2056                 if (ret >= 0) {
2057                         struct access_cbdata *cbdata;
2058
2059                         cbdata = malloc(sizeof(*cbdata));
2060                         if (!cbdata) {
2061                                 ret = LB_STATUS_ERROR_MEMORY;
2062                         } else {
2063                                 cbdata->inst = instance_ref(inst);
2064                                 cbdata->status = ret;
2065
2066                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2067                                         (void)instance_unref(cbdata->inst);
2068                                         DbgFree(cbdata);
2069                                         ret = LB_STATUS_ERROR_FAULT;
2070                                 } else {
2071                                         ret = LB_STATUS_SUCCESS;
2072                                 }
2073                         }
2074                 }
2075         } else {
2076                 ErrPrint("Unsupported package\n");
2077                 ret = LB_STATUS_ERROR_INVALID;
2078         }
2079
2080 out:
2081         result = packet_create_reply(packet, "i", ret);
2082         if (!result) {
2083                 ErrPrint("Failed to create a reply packet\n");
2084         }
2085
2086         return result;
2087 }
2088
2089 static struct packet *client_pd_access_scroll_down(pid_t pid, int handle, const struct packet *packet)
2090 {
2091         struct packet *result;
2092         struct client_node *client;
2093         const char *pkgname;
2094         const char *id;
2095         int ret;
2096         double timestamp;
2097         int x;
2098         int y;
2099         struct inst_info *inst = NULL;
2100         const struct pkg_info *pkg = NULL;
2101
2102         client = client_find_by_rpc_handle(handle);
2103         if (!client) {
2104                 ErrPrint("Client %d is not exists\n", pid);
2105                 ret = LB_STATUS_ERROR_NOT_EXIST;
2106                 goto out;
2107         }
2108
2109         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2110         if (ret != 5) {
2111                 ErrPrint("Invalid parameter\n");
2112                 ret = LB_STATUS_ERROR_INVALID;
2113                 goto out;
2114         }
2115
2116         ret = validate_request(pkgname, id, &inst, &pkg);
2117         if (ret != LB_STATUS_SUCCESS) {
2118                 goto out;
2119         }
2120
2121         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2122                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2123         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2124                 struct script_info *script;
2125                 Evas *e;
2126
2127                 script = instance_pd_script(inst);
2128                 if (!script) {
2129                         ret = LB_STATUS_ERROR_FAULT;
2130                         goto out;
2131                 }
2132
2133                 e = script_handler_evas(script);
2134                 if (!e) {
2135                         ret = LB_STATUS_ERROR_FAULT;
2136                         goto out;
2137                 }
2138
2139                 script_handler_update_pointer(script, x, y, 1);
2140                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_SCROLL, timestamp);
2141                 if (ret >= 0) {
2142                         struct access_cbdata *cbdata;
2143
2144                         cbdata = malloc(sizeof(*cbdata));
2145                         if (!cbdata) {
2146                                 ret = LB_STATUS_ERROR_MEMORY;
2147                         } else {
2148                                 cbdata->inst = instance_ref(inst);
2149                                 cbdata->status = ret;
2150
2151                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2152                                         (void)instance_unref(cbdata->inst);
2153                                         DbgFree(cbdata);
2154                                         ret = LB_STATUS_ERROR_FAULT;
2155                                 } else {
2156                                         ret = LB_STATUS_SUCCESS;
2157                                 }
2158                         }
2159                 }
2160         } else {
2161                 ErrPrint("Unsupported package\n");
2162                 ret = LB_STATUS_ERROR_INVALID;
2163         }
2164
2165 out:
2166         result = packet_create_reply(packet, "i", ret);
2167         if (!result) {
2168                 ErrPrint("Failed to create a reply packet\n");
2169         }
2170
2171         return result;
2172 }
2173
2174 static struct packet *client_pd_access_scroll_move(pid_t pid, int handle, const struct packet *packet)
2175 {
2176         struct packet *result;
2177         struct client_node *client;
2178         const char *pkgname;
2179         const char *id;
2180         int ret;
2181         double timestamp;
2182         int x;
2183         int y;
2184         struct inst_info *inst = NULL;
2185         const struct pkg_info *pkg = NULL;
2186
2187         client = client_find_by_rpc_handle(handle);
2188         if (!client) {
2189                 ErrPrint("Client %d is not exists\n", pid);
2190                 ret = LB_STATUS_ERROR_NOT_EXIST;
2191                 goto out;
2192         }
2193
2194         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2195         if (ret != 5) {
2196                 ErrPrint("Invalid parameter\n");
2197                 ret = LB_STATUS_ERROR_INVALID;
2198                 goto out;
2199         }
2200
2201         ret = validate_request(pkgname, id, &inst, &pkg);
2202         if (ret != LB_STATUS_SUCCESS) {
2203                 goto out;
2204         }
2205
2206         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2207                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2208         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2209                 struct script_info *script;
2210                 Evas *e;
2211
2212                 script = instance_pd_script(inst);
2213                 if (!script) {
2214                         ret = LB_STATUS_ERROR_FAULT;
2215                         goto out;
2216                 }
2217
2218                 e = script_handler_evas(script);
2219                 if (!e) {
2220                         ret = LB_STATUS_ERROR_FAULT;
2221                         goto out;
2222                 }
2223
2224                 script_handler_update_pointer(script, x, y, -1);
2225                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_SCROLL, timestamp);
2226                 if (ret >= 0) {
2227                         struct access_cbdata *cbdata;
2228
2229                         cbdata = malloc(sizeof(*cbdata));
2230                         if (!cbdata) {
2231                                 ret = LB_STATUS_ERROR_MEMORY;
2232                         } else {
2233                                 cbdata->inst = instance_ref(inst);
2234                                 cbdata->status = ret;
2235
2236                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2237                                         (void)instance_unref(cbdata->inst);
2238                                         DbgFree(cbdata);
2239                                         ret = LB_STATUS_ERROR_FAULT;
2240                                 } else {
2241                                         ret = LB_STATUS_SUCCESS;
2242                                 }
2243                         }
2244                 }
2245         } else {
2246                 ErrPrint("Unsupported package\n");
2247                 ret = LB_STATUS_ERROR_INVALID;
2248         }
2249
2250 out:
2251         result = packet_create_reply(packet, "i", ret);
2252         if (!result) {
2253                 ErrPrint("Failed to create a reply packet\n");
2254         }
2255
2256         return result;
2257 }
2258
2259 static struct packet *client_pd_access_scroll_up(pid_t pid, int handle, const struct packet *packet)
2260 {
2261         struct packet *result;
2262         struct client_node *client;
2263         const char *pkgname;
2264         const char *id;
2265         int ret;
2266         double timestamp;
2267         int x;
2268         int y;
2269         struct inst_info *inst = NULL;
2270         const struct pkg_info *pkg = NULL;
2271
2272         client = client_find_by_rpc_handle(handle);
2273         if (!client) {
2274                 ErrPrint("Client %d is not exists\n", pid);
2275                 ret = LB_STATUS_ERROR_NOT_EXIST;
2276                 goto out;
2277         }
2278
2279         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2280         if (ret != 5) {
2281                 ErrPrint("Invalid parameter\n");
2282                 ret = LB_STATUS_ERROR_INVALID;
2283                 goto out;
2284         }
2285
2286         ret = validate_request(pkgname, id, &inst, &pkg);
2287         if (ret != LB_STATUS_SUCCESS) {
2288                 goto out;
2289         }
2290
2291         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2292                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2293         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2294                 struct script_info *script;
2295                 Evas *e;
2296
2297                 script = instance_pd_script(inst);
2298                 if (!script) {
2299                         ret = LB_STATUS_ERROR_FAULT;
2300                         goto out;
2301                 }
2302
2303                 e = script_handler_evas(script);
2304                 if (!e) {
2305                         ret = LB_STATUS_ERROR_FAULT;
2306                         goto out;
2307                 }
2308
2309                 script_handler_update_pointer(script, x, y, 0);
2310                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_SCROLL, timestamp);
2311                 if (ret >= 0) {
2312                         struct access_cbdata *cbdata;
2313
2314                         cbdata = malloc(sizeof(*cbdata));
2315                         if (!cbdata) {
2316                                 ret = LB_STATUS_ERROR_MEMORY;
2317                         } else {
2318                                 cbdata->inst = instance_ref(inst);
2319                                 cbdata->status = ret;
2320
2321                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2322                                         (void)instance_unref(cbdata->inst);
2323                                         DbgFree(cbdata);
2324                                         ret = LB_STATUS_ERROR_FAULT;
2325                                 } else {
2326                                         ret = LB_STATUS_SUCCESS;
2327                                 }
2328                         }
2329                 }
2330         } else {
2331                 ErrPrint("Unsupported package\n");
2332                 ret = LB_STATUS_ERROR_INVALID;
2333         }
2334
2335 out:
2336         result = packet_create_reply(packet, "i", ret);
2337         if (!result) {
2338                 ErrPrint("Failed to create a reply packet\n");
2339         }
2340
2341         return result;
2342 }
2343
2344 static struct packet *client_pd_access_unhighlight(pid_t pid, int handle, const struct packet *packet)
2345 {
2346         struct packet *result;
2347         struct client_node *client;
2348         const char *pkgname;
2349         const char *id;
2350         int x;
2351         int y;
2352         struct inst_info *inst = NULL;
2353         const struct pkg_info *pkg = NULL;
2354         int ret;
2355         double timestamp;
2356
2357         client = client_find_by_rpc_handle(handle);
2358         if (!client) {
2359                 ErrPrint("Client %d is not exists\n", pid);
2360                 ret = LB_STATUS_ERROR_NOT_EXIST;
2361                 goto out;
2362         }
2363
2364         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2365         if (ret != 5) {
2366                 ErrPrint("Invalid parameter\n");
2367                 ret = LB_STATUS_ERROR_INVALID;
2368                 goto out;
2369         }
2370
2371         ret = validate_request(pkgname, id, &inst, &pkg);
2372         if (ret != LB_STATUS_SUCCESS) {
2373                 goto out;
2374         }
2375
2376         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2377                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2378         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2379                 struct script_info *script;
2380                 Evas *e;
2381
2382                 script = instance_pd_script(inst);
2383                 if (!script) {
2384                         ret = LB_STATUS_ERROR_FAULT;
2385                         goto out;
2386                 }
2387
2388                 e = script_handler_evas(script);
2389                 if (!e) {
2390                         ret = LB_STATUS_ERROR_FAULT;
2391                         goto out;
2392                 }
2393
2394                 script_handler_update_pointer(script, x, y, -1);
2395                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_UNHIGHLIGHT, timestamp);
2396                 if (ret >= 0) {
2397                         struct access_cbdata *cbdata;
2398
2399                         cbdata = malloc(sizeof(*cbdata));
2400                         if (!cbdata) {
2401                                 ret = LB_STATUS_ERROR_MEMORY;
2402                         } else {
2403                                 cbdata->inst = instance_ref(inst);
2404                                 cbdata->status = ret;
2405
2406                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2407                                         (void)instance_unref(cbdata->inst);
2408                                         DbgFree(cbdata);
2409                                         ret = LB_STATUS_ERROR_FAULT;
2410                                 } else {
2411                                         ret = LB_STATUS_SUCCESS;
2412                                 }
2413                         }
2414                 }
2415         } else {
2416                 ErrPrint("Unsupported package\n");
2417                 ret = LB_STATUS_ERROR_INVALID;
2418         }
2419 out:
2420         result = packet_create_reply(packet, "i", ret);
2421         if (!result) {
2422                 ErrPrint("Failed to create a reply packet\n");
2423         }
2424
2425         return result;
2426 }
2427
2428 static struct packet *client_pd_access_hl(pid_t pid, int handle, const struct packet *packet)
2429 {
2430         struct packet *result;
2431         struct client_node *client;
2432         const char *pkgname;
2433         const char *id;
2434         int ret;
2435         double timestamp;
2436         int x;
2437         int y;
2438         struct inst_info *inst = NULL;
2439         const struct pkg_info *pkg = NULL;
2440
2441         client = client_find_by_rpc_handle(handle);
2442         if (!client) {
2443                 ErrPrint("Client %d is not exists\n", pid);
2444                 ret = LB_STATUS_ERROR_NOT_EXIST;
2445                 goto out;
2446         }
2447
2448         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2449         if (ret != 5) {
2450                 ErrPrint("Invalid parameter\n");
2451                 ret = LB_STATUS_ERROR_INVALID;
2452                 goto out;
2453         }
2454
2455         ret = validate_request(pkgname, id, &inst, &pkg);
2456         if (ret != LB_STATUS_SUCCESS) {
2457                 goto out;
2458         }
2459
2460         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2461                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2462         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2463                 struct script_info *script;
2464                 Evas *e;
2465
2466                 script = instance_pd_script(inst);
2467                 if (!script) {
2468                         ret = LB_STATUS_ERROR_FAULT;
2469                         goto out;
2470                 }
2471
2472                 e = script_handler_evas(script);
2473                 if (!e) {
2474                         ret = LB_STATUS_ERROR_FAULT;
2475                         goto out;
2476                 }
2477
2478                 script_handler_update_pointer(script, x, y, -1);
2479                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_HIGHLIGHT, timestamp);
2480                 if (ret >= 0) {
2481                         struct access_cbdata *cbdata;
2482
2483                         cbdata = malloc(sizeof(*cbdata));
2484                         if (!cbdata) {
2485                                 ret = LB_STATUS_ERROR_MEMORY;
2486                         } else {
2487                                 cbdata->inst = instance_ref(inst);
2488                                 cbdata->status = ret;
2489
2490                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2491                                         (void)instance_unref(cbdata->inst);
2492                                         DbgFree(cbdata);
2493                                         ret = LB_STATUS_ERROR_FAULT;
2494                                 } else {
2495                                         ret = LB_STATUS_SUCCESS;
2496                                 }
2497                         }
2498                 }
2499         } else {
2500                 ErrPrint("Unsupported package\n");
2501                 ret = LB_STATUS_ERROR_INVALID;
2502         }
2503
2504 out:
2505         result = packet_create_reply(packet, "i", ret);
2506         if (!result) {
2507                 ErrPrint("Failed to create a reply packet\n");
2508         }
2509
2510         return result;
2511 }
2512
2513 static struct packet *client_pd_access_hl_prev(pid_t pid, int handle, const struct packet *packet)
2514 {
2515         struct packet *result;
2516         struct client_node *client;
2517         const char *pkgname;
2518         const char *id;
2519         int ret;
2520         double timestamp;
2521         int x;
2522         int y;
2523         struct inst_info *inst = NULL;
2524         const struct pkg_info *pkg = NULL;
2525
2526         client = client_find_by_rpc_handle(handle);
2527         if (!client) {
2528                 ErrPrint("Client %d is not exists\n", pid);
2529                 ret = LB_STATUS_ERROR_NOT_EXIST;
2530                 goto out;
2531         }
2532
2533         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2534         if (ret != 5) {
2535                 ErrPrint("Invalid parameter\n");
2536                 ret = LB_STATUS_ERROR_INVALID;
2537                 goto out;
2538         }
2539
2540         ret = validate_request(pkgname, id, &inst, &pkg);
2541         if (ret != LB_STATUS_SUCCESS) {
2542                 goto out;
2543         }
2544
2545         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2546                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2547         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2548                 struct script_info *script;
2549                 Evas *e;
2550
2551                 script = instance_pd_script(inst);
2552                 if (!script) {
2553                         ret = LB_STATUS_ERROR_FAULT;
2554                         goto out;
2555                 }
2556
2557                 e = script_handler_evas(script);
2558                 if (!e) {
2559                         ret = LB_STATUS_ERROR_FAULT;
2560                         goto out;
2561                 }
2562
2563                 script_handler_update_pointer(script, x, y, -1);
2564                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_HIGHLIGHT_PREV, timestamp);
2565                 if (ret >= 0) {
2566                         struct access_cbdata *cbdata;
2567
2568                         cbdata = malloc(sizeof(*cbdata));
2569                         if (!cbdata) {
2570                                 ret = LB_STATUS_ERROR_MEMORY;
2571                         } else {
2572                                 cbdata->inst = instance_ref(inst);
2573                                 cbdata->status = ret;
2574
2575                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2576                                         (void)instance_unref(cbdata->inst);
2577                                         DbgFree(cbdata);
2578                                         ret = LB_STATUS_ERROR_FAULT;
2579                                 } else {
2580                                         ret = LB_STATUS_SUCCESS;
2581                                 }
2582                         }
2583                 }
2584         } else {
2585                 ErrPrint("Unsupported package\n");
2586                 ret = LB_STATUS_ERROR_INVALID;
2587         }
2588
2589 out:
2590         result = packet_create_reply(packet, "i", ret);
2591         if (!result) {
2592                 ErrPrint("Failed to create a reply packet\n");
2593         }
2594
2595         return result;
2596 }
2597
2598 static struct packet *client_pd_access_hl_next(pid_t pid, int handle, const struct packet *packet)
2599 {
2600         struct packet *result;
2601         struct client_node *client;
2602         const char *pkgname;
2603         const char *id;
2604         int ret;
2605         double timestamp;
2606         int x;
2607         int y;
2608         struct inst_info *inst = NULL;
2609         const struct pkg_info *pkg = NULL;
2610
2611         client = client_find_by_rpc_handle(handle);
2612         if (!client) {
2613                 ErrPrint("Client %d is not exists\n", pid);
2614                 ret = LB_STATUS_ERROR_NOT_EXIST;
2615                 goto out;
2616         }
2617
2618         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2619         if (ret != 5) {
2620                 ErrPrint("Invalid parameter\n");
2621                 ret = LB_STATUS_ERROR_INVALID;
2622                 goto out;
2623         }
2624
2625         ret = validate_request(pkgname, id, &inst, &pkg);
2626         if (ret != LB_STATUS_SUCCESS) {
2627                 goto out;
2628         }
2629
2630         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2631                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2632         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2633                 struct script_info *script;
2634                 Evas *e;
2635
2636                 script = instance_pd_script(inst);
2637                 if (!script) {
2638                         ErrPrint("Script is not created yet\n");
2639                         ret = LB_STATUS_ERROR_FAULT;
2640                         goto out;
2641                 }
2642
2643                 e = script_handler_evas(script);
2644                 if (!e) {
2645                         ErrPrint("Evas is not exists\n");
2646                         ret = LB_STATUS_ERROR_FAULT;
2647                         goto out;
2648                 }
2649
2650                 script_handler_update_pointer(script, x, y, -1);
2651                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_HIGHLIGHT_NEXT, timestamp);
2652                 if (ret >= 0) {
2653                         struct access_cbdata *cbdata;
2654
2655                         cbdata = malloc(sizeof(*cbdata));
2656                         if (!cbdata) {
2657                                 ErrPrint("Heap: %s\n", strerror(errno));
2658                                 ret = LB_STATUS_ERROR_MEMORY;
2659                         } else {
2660                                 cbdata->inst = instance_ref(inst);
2661                                 cbdata->status = ret;
2662
2663                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2664                                         ErrPrint("Failed to add timer\n");
2665                                         (void)instance_unref(cbdata->inst);
2666                                         DbgFree(cbdata);
2667                                         ret = LB_STATUS_ERROR_FAULT;
2668                                 } else {
2669                                         ret = LB_STATUS_SUCCESS;
2670                                 }
2671                         }
2672                 } else {
2673                         DbgPrint("Returns: %d\n", ret);
2674                 }
2675         } else {
2676                 ErrPrint("Unsupported package\n");
2677                 ret = LB_STATUS_ERROR_INVALID;
2678         }
2679
2680 out:
2681         result = packet_create_reply(packet, "i", ret);
2682         if (!result) {
2683                 ErrPrint("Failed to create a reply packet\n");
2684         }
2685
2686         return result;
2687 }
2688
2689 static struct packet *client_pd_access_activate(pid_t pid, int handle, const struct packet *packet)
2690 {
2691         struct packet *result;
2692         struct client_node *client;
2693         const char *pkgname;
2694         const char *id;
2695         int ret;
2696         double timestamp;
2697         int x;
2698         int y;
2699         struct inst_info *inst = NULL;
2700         const struct pkg_info *pkg = NULL;
2701
2702         client = client_find_by_rpc_handle(handle);
2703         if (!client) {
2704                 ErrPrint("Client %d is not exists\n", pid);
2705                 ret = LB_STATUS_ERROR_NOT_EXIST;
2706                 goto out;
2707         }
2708
2709         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2710         if (ret != 5) {
2711                 ErrPrint("Invalid parameter\n");
2712                 ret = LB_STATUS_ERROR_INVALID;
2713                 goto out;
2714         }
2715
2716         ret = validate_request(pkgname, id, &inst, &pkg);
2717         if (ret != LB_STATUS_SUCCESS) {
2718                 goto out;
2719         }
2720
2721         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2722                 ret = forward_pd_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2723         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2724                 struct script_info *script;
2725                 Evas *e;
2726
2727                 script = instance_pd_script(inst);
2728                 if (!script) {
2729                         ret = LB_STATUS_ERROR_FAULT;
2730                         goto out;
2731                 }
2732
2733                 e = script_handler_evas(script);
2734                 if (!e) {
2735                         ret = LB_STATUS_ERROR_FAULT;
2736                         goto out;
2737                 }
2738
2739                 script_handler_update_pointer(script, x, y, -1);
2740                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_ACTIVATE, timestamp);
2741                 if (ret >= 0) {
2742                         struct access_cbdata *cbdata;
2743
2744                         cbdata = malloc(sizeof(*cbdata));
2745                         if (!cbdata) {
2746                                 ret = LB_STATUS_ERROR_MEMORY;
2747                         } else {
2748                                 cbdata->inst = instance_ref(inst);
2749                                 cbdata->status = ret;
2750
2751                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
2752                                         (void)instance_unref(cbdata->inst);
2753                                         DbgFree(cbdata);
2754                                         ret = LB_STATUS_ERROR_FAULT;
2755                                 } else {
2756                                         ret = LB_STATUS_SUCCESS;
2757                                 }
2758                         }
2759                 }
2760         } else {
2761                 ErrPrint("Unsupported package\n");
2762                 ret = LB_STATUS_ERROR_INVALID;
2763         }
2764
2765 out:
2766         result = packet_create_reply(packet, "i", ret);
2767         if (!result) {
2768                 ErrPrint("Failed to create a reply packet\n");
2769         }
2770
2771         return result;
2772 }
2773
2774 static struct packet *client_pd_key_down(pid_t pid, int handle, const struct packet *packet)
2775 {
2776         struct client_node *client;
2777         const char *pkgname;
2778         const char *id;
2779         int ret;
2780         double timestamp;
2781         int x;
2782         int y;
2783         struct inst_info *inst = NULL;
2784         const struct pkg_info *pkg = NULL;
2785
2786         client = client_find_by_rpc_handle(handle);
2787         if (!client) {
2788                 ErrPrint("Client %d is not exists\n", pid);
2789                 ret = LB_STATUS_ERROR_NOT_EXIST;
2790                 goto out;
2791         }
2792
2793         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2794         if (ret != 5) {
2795                 ErrPrint("Invalid parameter\n");
2796                 ret = LB_STATUS_ERROR_INVALID;
2797                 goto out;
2798         }
2799
2800         ret = validate_request(pkgname, id, &inst, &pkg);
2801         if (ret != LB_STATUS_SUCCESS) {
2802                 goto out;
2803         }
2804
2805         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2806                 ret = forward_pd_event_packet(pkg, inst, packet);
2807         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2808                 struct script_info *script;
2809                 Evas *e;
2810
2811                 script = instance_pd_script(inst);
2812                 if (!script) {
2813                         ret = LB_STATUS_ERROR_FAULT;
2814                         goto out;
2815                 }
2816
2817                 e = script_handler_evas(script);
2818                 if (!e) {
2819                         ret = LB_STATUS_ERROR_FAULT;
2820                         goto out;
2821                 }
2822
2823                 script_handler_update_pointer(script, x, y, -1);
2824                 /*!
2825                  * \TODO: Push up the KEY_DOWN event
2826                  */
2827                 ret = 0;
2828         } else {
2829                 ErrPrint("Unsupported package\n");
2830                 ret = LB_STATUS_ERROR_INVALID;
2831         }
2832
2833 out:
2834         /*! \note No reply packet */
2835         return NULL;
2836 }
2837
2838 static struct packet *client_pause_request(pid_t pid, int handle, const struct packet *packet)
2839 {
2840         struct client_node *client;
2841         double timestamp;
2842         int ret;
2843
2844         client = client_find_by_rpc_handle(handle);
2845         if (!client) {
2846                 ErrPrint("Client %d is paused - manually reported\n", pid);
2847                 ret = LB_STATUS_ERROR_NOT_EXIST;
2848                 goto out;
2849         }
2850
2851         ret = packet_get(packet, "d", &timestamp);
2852         if (ret != 1) {
2853                 ErrPrint("Invalid parameter\n");
2854                 ret = LB_STATUS_ERROR_INVALID;
2855                 goto out;
2856         }
2857
2858         if (USE_XMONITOR) {
2859                 DbgPrint("XMONITOR enabled. ignore client paused request\n");
2860         } else {
2861                 xmonitor_pause(client);
2862         }
2863
2864 out:
2865         return NULL;
2866 }
2867
2868 static struct packet *client_resume_request(pid_t pid, int handle, const struct packet *packet)
2869 {
2870         struct client_node *client;
2871         double timestamp;
2872         int ret;
2873
2874         client = client_find_by_rpc_handle(handle);
2875         if (!client) {
2876                 ErrPrint("Client %d is not exists\n", pid);
2877                 goto out;
2878         }
2879
2880         ret = packet_get(packet, "d", &timestamp);
2881         if (ret != 1) {
2882                 ErrPrint("Invalid parameter\n");
2883                 goto out;
2884         }
2885
2886         if (USE_XMONITOR) {
2887                 DbgPrint("XMONITOR enabled. ignore client resumed request\n");
2888         } else {
2889                 xmonitor_resume(client);
2890         }
2891
2892 out:
2893         return NULL;
2894 }
2895
2896 static struct packet *client_pd_key_up(pid_t pid, int handle, const struct packet *packet)
2897 {
2898         struct client_node *client;
2899         const char *pkgname;
2900         const char *id;
2901         int ret;
2902         double timestamp;
2903         int x;
2904         int y;
2905         struct inst_info *inst = NULL;
2906         const struct pkg_info *pkg = NULL;
2907
2908         client = client_find_by_rpc_handle(handle);
2909         if (!client) {
2910                 ErrPrint("Client %d is not exists\n", pid);
2911                 ret = LB_STATUS_ERROR_NOT_EXIST;
2912                 goto out;
2913         }
2914
2915         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2916         if (ret != 7) {
2917                 ErrPrint("Invalid parameter\n");
2918                 ret = LB_STATUS_ERROR_INVALID;
2919                 goto out;
2920         }
2921
2922         ret = validate_request(pkgname, id, &inst, &pkg);
2923         if (ret != LB_STATUS_SUCCESS) {
2924                 goto out;
2925         }
2926
2927         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2928                 ret = forward_pd_event_packet(pkg, inst, packet);
2929         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
2930                 struct script_info *script;
2931                 Evas *e;
2932
2933                 script = instance_pd_script(inst);
2934                 if (!script) {
2935                         ret = LB_STATUS_ERROR_FAULT;
2936                         goto out;
2937                 }
2938
2939                 e = script_handler_evas(script);
2940                 if (!e) {
2941                         ret = LB_STATUS_ERROR_FAULT;
2942                         goto out;
2943                 }
2944
2945                 script_handler_update_pointer(script, x, y, -1);
2946                 /*!
2947                  * \TODO: Push up the KEY_UP event
2948                  */
2949                 ret = 0;
2950         } else {
2951                 ErrPrint("Unsupported package\n");
2952                 ret = LB_STATUS_ERROR_INVALID;
2953         }
2954
2955 out:
2956         /*! \note No reply packet */
2957         return NULL;
2958 }
2959
2960 static struct packet *client_lb_access_hl(pid_t pid, int handle, const struct packet *packet)
2961 {
2962         struct packet *result;
2963         struct client_node *client;
2964         const char *pkgname;
2965         const char *id;
2966         int ret;
2967         double timestamp;
2968         int x;
2969         int y;
2970         struct inst_info *inst = NULL;
2971         const struct pkg_info *pkg = NULL;
2972
2973         client = client_find_by_rpc_handle(handle);
2974         if (!client) {
2975                 ErrPrint("Client %d is not exists\n", pid);
2976                 ret = LB_STATUS_ERROR_NOT_EXIST;
2977                 goto out;
2978         }
2979
2980         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
2981         if (ret != 5) {
2982                 ErrPrint("Parameter is not matched\n");
2983                 ret = LB_STATUS_ERROR_INVALID;
2984                 goto out;
2985         }
2986
2987         ret = validate_request(pkgname, id, &inst, &pkg);
2988         if (ret != LB_STATUS_SUCCESS) {
2989                 goto out;
2990         }
2991
2992         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
2993                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
2994         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
2995                 struct script_info *script;
2996                 Evas *e;
2997
2998                 script = instance_lb_script(inst);
2999                 if (!script) {
3000                         ret = LB_STATUS_ERROR_FAULT;
3001                         goto out;
3002                 }
3003
3004                 e = script_handler_evas(script);
3005                 if (!e) {
3006                         ret = LB_STATUS_ERROR_FAULT;
3007                         goto out;
3008                 }
3009
3010                 script_handler_update_pointer(script, x, y, -1);
3011                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_HIGHLIGHT, timestamp);
3012                 if (ret >= 0) {
3013                         struct access_cbdata *cbdata;
3014
3015                         cbdata = malloc(sizeof(*cbdata));
3016                         if (!cbdata) {
3017                                 ret = LB_STATUS_ERROR_MEMORY;
3018                         } else {
3019                                 cbdata->inst = instance_ref(inst);
3020                                 cbdata->status = ret;
3021
3022                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3023                                         (void)instance_unref(cbdata->inst);
3024                                         DbgFree(cbdata);
3025                                         ret = LB_STATUS_ERROR_FAULT;
3026                                 } else {
3027                                         ret = LB_STATUS_SUCCESS;
3028                                 }
3029                         }
3030                 }
3031         } else {
3032                 ErrPrint("Unsupported package\n");
3033                 ret = LB_STATUS_ERROR_INVALID;
3034         }
3035
3036 out:
3037         result = packet_create_reply(packet, "i", ret);
3038         if (!result) {
3039                 ErrPrint("Failed to create a reply packet\n");
3040         }
3041
3042         return result;
3043 }
3044
3045 static struct packet *client_lb_access_hl_prev(pid_t pid, int handle, const struct packet *packet)
3046 {
3047         struct packet *result;
3048         struct client_node *client;
3049         const char *pkgname;
3050         const char *id;
3051         int ret;
3052         double timestamp;
3053         int x;
3054         int y;
3055         struct inst_info *inst = NULL;
3056         const struct pkg_info *pkg = NULL;
3057
3058         client = client_find_by_rpc_handle(handle);
3059         if (!client) {
3060                 ErrPrint("Client %d is not exists\n", pid);
3061                 ret = LB_STATUS_ERROR_NOT_EXIST;
3062                 goto out;
3063         }
3064
3065         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3066         if (ret != 5) {
3067                 ErrPrint("Parameter is not matched\n");
3068                 ret = LB_STATUS_ERROR_INVALID;
3069                 goto out;
3070         }
3071
3072         ret = validate_request(pkgname, id, &inst, &pkg);
3073         if (ret != LB_STATUS_SUCCESS) {
3074                 goto out;
3075         }
3076
3077         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3078                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3079         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3080                 struct script_info *script;
3081                 Evas *e;
3082
3083                 script = instance_lb_script(inst);
3084                 if (!script) {
3085                         ret = LB_STATUS_ERROR_FAULT;
3086                         goto out;
3087                 }
3088
3089                 e = script_handler_evas(script);
3090                 if (!e) {
3091                         ret = LB_STATUS_ERROR_FAULT;
3092                         goto out;
3093                 }
3094
3095                 script_handler_update_pointer(script, x, y, -1);
3096                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_HIGHLIGHT_PREV, timestamp);
3097                 if (ret >= 0) {
3098                         struct access_cbdata *cbdata;
3099
3100                         cbdata = malloc(sizeof(*cbdata));
3101                         if (!cbdata) {
3102                                 ret = LB_STATUS_ERROR_MEMORY;
3103                         } else {
3104                                 cbdata->inst = instance_ref(inst);
3105                                 cbdata->status = ret;
3106
3107                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3108                                         (void)instance_unref(cbdata->inst);
3109                                         DbgFree(cbdata);
3110                                         ret = LB_STATUS_ERROR_FAULT;
3111                                 } else {
3112                                         ret = LB_STATUS_SUCCESS;
3113                                 }
3114                         }
3115                 }
3116         } else {
3117                 ErrPrint("Unsupported package\n");
3118                 ret = LB_STATUS_ERROR_INVALID;
3119         }
3120
3121 out:
3122         result = packet_create_reply(packet, "i", ret);
3123         if (!result) {
3124                 ErrPrint("Failed to create a reply packet\n");
3125         }
3126
3127         return result;
3128 }
3129
3130 static struct packet *client_lb_access_hl_next(pid_t pid, int handle, const struct packet *packet)
3131 {
3132         struct packet *result;
3133         struct client_node *client;
3134         const char *pkgname;
3135         const char *id;
3136         int ret;
3137         double timestamp;
3138         int x;
3139         int y;
3140         struct inst_info *inst = NULL;
3141         const struct pkg_info *pkg = NULL;
3142
3143         client = client_find_by_rpc_handle(handle);
3144         if (!client) {
3145                 ErrPrint("Client %d is not exists\n", pid);
3146                 ret = LB_STATUS_ERROR_NOT_EXIST;
3147                 goto out;
3148         }
3149
3150         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3151         if (ret != 5) {
3152                 ErrPrint("Parameter is not matched\n");
3153                 ret = LB_STATUS_ERROR_INVALID;
3154                 goto out;
3155         }
3156
3157         ret = validate_request(pkgname, id, &inst, &pkg);
3158         if (ret != LB_STATUS_SUCCESS) {
3159                 goto out;
3160         }
3161
3162         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3163                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3164         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3165                 struct script_info *script;
3166                 Evas *e;
3167
3168                 script = instance_lb_script(inst);
3169                 if (!script) {
3170                         ret = LB_STATUS_ERROR_FAULT;
3171                         goto out;
3172                 }
3173
3174                 e = script_handler_evas(script);
3175                 if (!e) {
3176                         ret = LB_STATUS_ERROR_FAULT;
3177                         goto out;
3178                 }
3179
3180                 script_handler_update_pointer(script, x, y, -1);
3181                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_HIGHLIGHT_NEXT, timestamp);
3182                 if (ret >= 0) {
3183                         struct access_cbdata *cbdata;
3184
3185                         cbdata = malloc(sizeof(*cbdata));
3186                         if (!cbdata) {
3187                                 ret = LB_STATUS_ERROR_MEMORY;
3188                         } else {
3189                                 cbdata->inst = instance_ref(inst);
3190                                 cbdata->status = ret;
3191
3192                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3193                                         (void)instance_unref(cbdata->inst);
3194                                         DbgFree(cbdata);
3195                                         ret = LB_STATUS_ERROR_FAULT;
3196                                 } else {
3197                                         ret = LB_STATUS_SUCCESS;
3198                                 }
3199                         }
3200                 }
3201         } else {
3202                 ErrPrint("Unsupported package\n");
3203                 ret = LB_STATUS_ERROR_INVALID;
3204         }
3205
3206 out:
3207         result = packet_create_reply(packet, "i", ret);
3208         if (!result) {
3209                 ErrPrint("Failed to create a reply packet\n");
3210         }
3211
3212         return result;
3213 }
3214
3215 static struct packet *client_lb_access_action_up(pid_t pid, int handle, const struct packet *packet)
3216 {
3217         struct packet *result;
3218         struct client_node *client;
3219         const char *pkgname;
3220         const char *id;
3221         int ret;
3222         double timestamp;
3223         struct inst_info *inst = NULL;
3224         const struct pkg_info *pkg = NULL;
3225         int x;
3226         int y;
3227
3228         client = client_find_by_rpc_handle(handle);
3229         if (!client) {
3230                 ErrPrint("Client %d is not exist\n", pid);
3231                 ret = LB_STATUS_ERROR_NOT_EXIST;
3232                 goto out;
3233         }
3234
3235         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3236         if (ret != 5) {
3237                 ErrPrint("Parameter is not matched\n");
3238                 ret = LB_STATUS_ERROR_INVALID;
3239                 goto out;
3240         }
3241
3242         ret = validate_request(pkgname, id, &inst, &pkg);
3243         if (ret != LB_STATUS_SUCCESS) {
3244                 goto out;
3245         }
3246
3247         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3248                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3249                 /*!
3250                  * Enen if it fails to send packet,
3251                  * The packet will be unref'd
3252                  * So we don't need to check the ret value.
3253                  */
3254         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3255                 struct script_info *script;
3256                 Evas *e;
3257
3258                 script = instance_lb_script(inst);
3259                 if (!script) {
3260                         ErrPrint("Instance has no script\n");
3261                         ret = LB_STATUS_ERROR_FAULT;
3262                         goto out;
3263                 }
3264
3265                 e = script_handler_evas(script);
3266                 if (!e) {
3267                         ErrPrint("Script has no evas\n");
3268                         ret = LB_STATUS_ERROR_INVALID;
3269                         goto out;
3270                 }
3271
3272                 script_handler_update_pointer(script, x, y, 0);
3273                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_ACTION, timestamp);
3274                 if (ret >= 0) {
3275                         struct access_cbdata *cbdata;
3276
3277                         cbdata = malloc(sizeof(*cbdata));
3278                         if (!cbdata) {
3279                                 ret = LB_STATUS_ERROR_MEMORY;
3280                         } else {
3281                                 cbdata->inst = instance_ref(inst);
3282                                 cbdata->status = ret;
3283
3284                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3285                                         (void)instance_unref(cbdata->inst);
3286                                         DbgFree(cbdata);
3287                                         ret = LB_STATUS_ERROR_FAULT;
3288                                 } else {
3289                                         ret = LB_STATUS_SUCCESS;
3290                                 }
3291                         }
3292                 }
3293         } else {
3294                 ErrPrint("Unsupported package\n");
3295                 ret = LB_STATUS_ERROR_INVALID;
3296         }
3297
3298 out:
3299         result = packet_create_reply(packet, "i", ret);
3300         if (!result) {
3301                 ErrPrint("Failed to create a reply packet\n");
3302         }
3303
3304         return result;
3305 }
3306
3307 static struct packet *client_lb_access_action_down(pid_t pid, int handle, const struct packet *packet)
3308 {
3309         struct packet *result;
3310         struct client_node *client;
3311         const char *pkgname;
3312         const char *id;
3313         int ret;
3314         double timestamp;
3315         struct inst_info *inst = NULL;
3316         const struct pkg_info *pkg = NULL;
3317         int x;
3318         int y;
3319
3320         client = client_find_by_rpc_handle(handle);
3321         if (!client) {
3322                 ErrPrint("Client %d is not exist\n", pid);
3323                 ret = LB_STATUS_ERROR_NOT_EXIST;
3324                 goto out;
3325         }
3326
3327         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3328         if (ret != 5) {
3329                 ErrPrint("Parameter is not matched\n");
3330                 ret = LB_STATUS_ERROR_INVALID;
3331                 goto out;
3332         }
3333
3334         ret = validate_request(pkgname, id, &inst, &pkg);
3335         if (ret != LB_STATUS_SUCCESS) {
3336                 goto out;
3337         }
3338
3339         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3340                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3341                 /*!
3342                  * Enen if it fails to send packet,
3343                  * The packet will be unref'd
3344                  * So we don't need to check the ret value.
3345                  */
3346         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3347                 struct script_info *script;
3348                 Evas *e;
3349
3350                 script = instance_lb_script(inst);
3351                 if (!script) {
3352                         ErrPrint("Instance has no script\n");
3353                         ret = LB_STATUS_ERROR_FAULT;
3354                         goto out;
3355                 }
3356
3357                 e = script_handler_evas(script);
3358                 if (!e) {
3359                         ErrPrint("Script has no evas\n");
3360                         ret = LB_STATUS_ERROR_INVALID;
3361                         goto out;
3362                 }
3363
3364                 script_handler_update_pointer(script, x, y, 1);
3365                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_ACTION, timestamp);
3366                 if (ret >= 0) {
3367                         struct access_cbdata *cbdata;
3368
3369                         cbdata = malloc(sizeof(*cbdata));
3370                         if (!cbdata) {
3371                                 ret = LB_STATUS_ERROR_MEMORY;
3372                         } else {
3373                                 cbdata->inst = instance_ref(inst);
3374                                 cbdata->status = ret;
3375
3376                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3377                                         (void)instance_unref(cbdata->inst);
3378                                         DbgFree(cbdata);
3379                                         ret = LB_STATUS_ERROR_FAULT;
3380                                 } else {
3381                                         ret = LB_STATUS_SUCCESS;
3382                                 }
3383                         }
3384                 }
3385         } else {
3386                 ErrPrint("Unsupported package\n");
3387                 ret = LB_STATUS_ERROR_INVALID;
3388         }
3389
3390 out:
3391         result = packet_create_reply(packet, "i", ret);
3392         if (!result) {
3393                 ErrPrint("Failed to create a reply packet\n");
3394         }
3395
3396         return result;
3397 }
3398
3399 static struct packet *client_lb_access_unhighlight(pid_t pid, int handle, const struct packet *packet)
3400 {
3401         struct packet *result;
3402         struct client_node *client;
3403         const char *pkgname;
3404         const char *id;
3405         int ret;
3406         double timestamp;
3407         int x;
3408         int y;
3409         struct inst_info *inst = NULL;
3410         const struct pkg_info *pkg = NULL;
3411
3412         client = client_find_by_rpc_handle(handle);
3413         if (!client) {
3414                 ErrPrint("Client %d is not exists\n", pid);
3415                 ret = LB_STATUS_ERROR_NOT_EXIST;
3416                 goto out;
3417         }
3418
3419         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3420         if (ret != 5) {
3421                 ErrPrint("Parameter is not matched\n");
3422                 ret = LB_STATUS_ERROR_INVALID;
3423                 goto out;
3424         }
3425
3426         ret = validate_request(pkgname, id, &inst, &pkg);
3427         if (ret != LB_STATUS_SUCCESS) {
3428                 goto out;
3429         }
3430
3431         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3432                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3433         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3434                 struct script_info *script;
3435                 Evas *e;
3436
3437                 script = instance_lb_script(inst);
3438                 if (!script) {
3439                         ret = LB_STATUS_ERROR_FAULT;
3440                         goto out;
3441                 }
3442
3443                 e = script_handler_evas(script);
3444                 if (!e) {
3445                         ret = LB_STATUS_ERROR_FAULT;
3446                         goto out;
3447                 }
3448
3449                 script_handler_update_pointer(script, x, y, -1);
3450                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_UNHIGHLIGHT, timestamp);
3451                 if (ret >= 0) {
3452                         struct access_cbdata *cbdata;
3453
3454                         cbdata = malloc(sizeof(*cbdata));
3455                         if (!cbdata) {
3456                                 ret = LB_STATUS_ERROR_MEMORY;
3457                         } else {
3458                                 cbdata->inst = instance_ref(inst);
3459                                 cbdata->status = ret;
3460
3461                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3462                                         (void)instance_unref(cbdata->inst);
3463                                         DbgFree(cbdata);
3464                                         ret = LB_STATUS_ERROR_FAULT;
3465                                 } else {
3466                                         ret = LB_STATUS_SUCCESS;
3467                                 }
3468                         }
3469                 }
3470         } else {
3471                 ErrPrint("Unsupported package\n");
3472                 ret = LB_STATUS_ERROR_INVALID;
3473         }
3474
3475 out:
3476         result = packet_create_reply(packet, "i", ret);
3477         if (!result) {
3478                 ErrPrint("Failed to create a reply packet\n");
3479         }
3480
3481         return result;
3482 }
3483
3484 static struct packet *client_lb_access_scroll_down(pid_t pid, int handle, const struct packet *packet)
3485 {
3486         struct packet *result;
3487         struct client_node *client;
3488         const char *pkgname;
3489         const char *id;
3490         int ret;
3491         double timestamp;
3492         struct inst_info *inst = NULL;
3493         const struct pkg_info *pkg = NULL;
3494         int x;
3495         int y;
3496
3497         client = client_find_by_rpc_handle(handle);
3498         if (!client) {
3499                 ErrPrint("Client %d is not exist\n", pid);
3500                 ret = LB_STATUS_ERROR_NOT_EXIST;
3501                 goto out;
3502         }
3503
3504         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3505         if (ret != 5) {
3506                 ErrPrint("Parameter is not matched\n");
3507                 ret = LB_STATUS_ERROR_INVALID;
3508                 goto out;
3509         }
3510
3511         ret = validate_request(pkgname, id, &inst, &pkg);
3512         if (ret != LB_STATUS_SUCCESS) {
3513                 goto out;
3514         }
3515
3516         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3517                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3518                 /*!
3519                  * Enen if it fails to send packet,
3520                  * The packet will be unref'd
3521                  * So we don't need to check the ret value.
3522                  */
3523         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3524                 struct script_info *script;
3525                 Evas *e;
3526
3527                 script = instance_lb_script(inst);
3528                 if (!script) {
3529                         ErrPrint("Instance has no script\n");
3530                         ret = LB_STATUS_ERROR_FAULT;
3531                         goto out;
3532                 }
3533
3534                 e = script_handler_evas(script);
3535                 if (!e) {
3536                         ErrPrint("Instance has no evas\n");
3537                         ret = LB_STATUS_ERROR_INVALID;
3538                         goto out;
3539                 }
3540
3541                 script_handler_update_pointer(script, x, y, 1);
3542                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_SCROLL, timestamp);
3543                 if (ret >= 0) {
3544                         struct access_cbdata *cbdata;
3545
3546                         cbdata = malloc(sizeof(*cbdata));
3547                         if (!cbdata) {
3548                                 ret = LB_STATUS_ERROR_MEMORY;
3549                         } else {
3550                                 cbdata->inst = instance_ref(inst);
3551                                 cbdata->status = ret;
3552
3553                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3554                                         (void)instance_unref(cbdata->inst);
3555                                         DbgFree(cbdata);
3556                                         ret = LB_STATUS_ERROR_FAULT;
3557                                 } else {
3558                                         ret = LB_STATUS_SUCCESS;
3559                                 }
3560                         }
3561                 }
3562         } else {
3563                 ErrPrint("Unsupported package\n");
3564                 ret = LB_STATUS_ERROR_INVALID;
3565         }
3566
3567 out:
3568         result = packet_create_reply(packet, "i", ret);
3569         if (!result) {
3570                 ErrPrint("Failed to create a reply packet\n");
3571         }
3572
3573         return result;
3574 }
3575
3576 static struct packet *client_lb_access_scroll_move(pid_t pid, int handle, const struct packet *packet)
3577 {
3578         struct packet *result;
3579         struct client_node *client;
3580         const char *pkgname;
3581         const char *id;
3582         int ret;
3583         double timestamp;
3584         struct inst_info *inst = NULL;
3585         const struct pkg_info *pkg = NULL;
3586         int x;
3587         int y;
3588
3589         client = client_find_by_rpc_handle(handle);
3590         if (!client) {
3591                 ErrPrint("Client %d is not exist\n", pid);
3592                 ret = LB_STATUS_ERROR_NOT_EXIST;
3593                 goto out;
3594         }
3595
3596         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3597         if (ret != 5) {
3598                 ErrPrint("Parameter is not matched\n");
3599                 ret = LB_STATUS_ERROR_INVALID;
3600                 goto out;
3601         }
3602
3603         ret = validate_request(pkgname, id, &inst, &pkg);
3604         if (ret != LB_STATUS_SUCCESS) {
3605                 goto out;
3606         }
3607
3608         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3609                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3610                 /*!
3611                  * Enen if it fails to send packet,
3612                  * The packet will be unref'd
3613                  * So we don't need to check the ret value.
3614                  */
3615         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3616                 struct script_info *script;
3617                 Evas *e;
3618
3619                 script = instance_lb_script(inst);
3620                 if (!script) {
3621                         ErrPrint("Instance has no script\n");
3622                         ret = LB_STATUS_ERROR_FAULT;
3623                         goto out;
3624                 }
3625
3626                 e = script_handler_evas(script);
3627                 if (!e) {
3628                         ErrPrint("Instance has no evas\n");
3629                         ret = LB_STATUS_ERROR_INVALID;
3630                         goto out;
3631                 }
3632
3633                 script_handler_update_pointer(script, x, y, -1);
3634                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_SCROLL, timestamp);
3635                 if (ret >= 0) {
3636                         struct access_cbdata *cbdata;
3637
3638                         cbdata = malloc(sizeof(*cbdata));
3639                         if (!cbdata) {
3640                                 ret = LB_STATUS_ERROR_MEMORY;
3641                         } else {
3642                                 cbdata->inst = instance_ref(inst);
3643                                 cbdata->status = ret;
3644
3645                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3646                                         (void)instance_unref(cbdata->inst);
3647                                         DbgFree(cbdata);
3648                                         ret = LB_STATUS_ERROR_FAULT;
3649                                 } else {
3650                                         ret = LB_STATUS_SUCCESS;
3651                                 }
3652                         }
3653                 }
3654         } else {
3655                 ErrPrint("Unsupported package\n");
3656                 ret = LB_STATUS_ERROR_INVALID;
3657         }
3658
3659 out:
3660         result = packet_create_reply(packet, "i", ret);
3661         if (!result) {
3662                 ErrPrint("Failed to create a reply packet\n");
3663         }
3664
3665         return result;
3666 }
3667
3668 static struct packet *client_lb_access_scroll_up(pid_t pid, int handle, const struct packet *packet)
3669 {
3670         struct packet *result;
3671         struct client_node *client;
3672         const char *pkgname;
3673         const char *id;
3674         int ret;
3675         double timestamp;
3676         struct inst_info *inst = NULL;
3677         const struct pkg_info *pkg = NULL;
3678         int x;
3679         int y;
3680
3681         client = client_find_by_rpc_handle(handle);
3682         if (!client) {
3683                 ErrPrint("Client %d is not exist\n", pid);
3684                 ret = LB_STATUS_ERROR_NOT_EXIST;
3685                 goto out;
3686         }
3687
3688         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3689         if (ret != 5) {
3690                 ErrPrint("Parameter is not matched\n");
3691                 ret = LB_STATUS_ERROR_INVALID;
3692                 goto out;
3693         }
3694
3695         ret = validate_request(pkgname, id, &inst, &pkg);
3696         if (ret != LB_STATUS_SUCCESS) {
3697                 goto out;
3698         }
3699
3700         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3701                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3702                 /*!
3703                  * Enen if it fails to send packet,
3704                  * The packet will be unref'd
3705                  * So we don't need to check the ret value.
3706                  */
3707         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3708                 struct script_info *script;
3709                 Evas *e;
3710
3711                 script = instance_lb_script(inst);
3712                 if (!script) {
3713                         ErrPrint("Instance has no script\n");
3714                         ret = LB_STATUS_ERROR_FAULT;
3715                         goto out;
3716                 }
3717
3718                 e = script_handler_evas(script);
3719                 if (!e) {
3720                         ErrPrint("Instance has no evas\n");
3721                         ret = LB_STATUS_ERROR_INVALID;
3722                         goto out;
3723                 }
3724
3725                 script_handler_update_pointer(script, x, y, 0);
3726                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_SCROLL, timestamp);
3727                 if (ret >= 0) {
3728                         struct access_cbdata *cbdata;
3729
3730                         cbdata = malloc(sizeof(*cbdata));
3731                         if (!cbdata) {
3732                                 ret = LB_STATUS_ERROR_MEMORY;
3733                         } else {
3734                                 cbdata->inst = instance_ref(inst);
3735                                 cbdata->status = ret;
3736
3737                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3738                                         (void)instance_unref(cbdata->inst);
3739                                         DbgFree(cbdata);
3740                                         ret = LB_STATUS_ERROR_FAULT;
3741                                 } else {
3742                                         ret = LB_STATUS_SUCCESS;
3743                                 }
3744                         }
3745                 }
3746         } else {
3747                 ErrPrint("Unsupported package\n");
3748                 ret = LB_STATUS_ERROR_INVALID;
3749         }
3750
3751 out:
3752         result = packet_create_reply(packet, "i", ret);
3753         if (!result) {
3754                 ErrPrint("Failed to create a reply packet\n");
3755         }
3756
3757         return result;
3758 }
3759
3760 static struct packet *client_lb_access_activate(pid_t pid, int handle, const struct packet *packet)
3761 {
3762         struct packet *result;
3763         struct client_node *client;
3764         const char *pkgname;
3765         const char *id;
3766         int ret;
3767         double timestamp;
3768         int x;
3769         int y;
3770         struct inst_info *inst = NULL;
3771         const struct pkg_info *pkg = NULL;
3772
3773         client = client_find_by_rpc_handle(handle);
3774         if (!client) {
3775                 ErrPrint("Client %d is not exists\n", pid);
3776                 ret = LB_STATUS_ERROR_NOT_EXIST;
3777                 goto out;
3778         }
3779
3780         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3781         if (ret != 5) {
3782                 ErrPrint("Parameter is not matched\n");
3783                 ret = LB_STATUS_ERROR_INVALID;
3784                 goto out;
3785         }
3786
3787         ret = validate_request(pkgname, id, &inst, &pkg);
3788         if (ret != LB_STATUS_SUCCESS) {
3789                 goto out;
3790         }
3791
3792         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3793                 ret = forward_lb_access_packet(pkg, inst, packet_command(packet), timestamp, x, y);
3794         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3795                 struct script_info *script;
3796                 Evas *e;
3797
3798                 script = instance_lb_script(inst);
3799                 if (!script) {
3800                         ErrPrint("Instance has no script\n");
3801                         ret = LB_STATUS_ERROR_FAULT;
3802                         goto out;
3803                 }
3804
3805                 e = script_handler_evas(script);
3806                 if (!e) {
3807                         ErrPrint("Script has no Evas\n");
3808                         ret = LB_STATUS_ERROR_INVALID;
3809                         goto out;
3810                 }
3811
3812                 script_handler_update_pointer(script, x, y, -1);
3813                 ret = script_handler_feed_event(script, LB_SCRIPT_ACCESS_ACTIVATE, timestamp);
3814                 if (ret >= 0) {
3815                         struct access_cbdata *cbdata;
3816
3817                         cbdata = malloc(sizeof(*cbdata));
3818                         if (!cbdata) {
3819                                 ret = LB_STATUS_ERROR_MEMORY;
3820                         } else {
3821                                 cbdata->inst = instance_ref(inst);
3822                                 cbdata->status = ret;
3823
3824                                 if (!ecore_timer_add(DELAY_TIME, lazy_access_status_cb, cbdata)) {
3825                                         (void)instance_unref(cbdata->inst);
3826                                         DbgFree(cbdata);
3827                                         ret = LB_STATUS_ERROR_FAULT;
3828                                 } else {
3829                                         ret = LB_STATUS_SUCCESS;
3830                                 }
3831                         }
3832                 }
3833         } else {
3834                 ErrPrint("Unsupported package\n");
3835                 ret = LB_STATUS_ERROR_INVALID;
3836         }
3837
3838 out:
3839         result = packet_create_reply(packet, "i", ret);
3840         if (!result) {
3841                 ErrPrint("Failed to create a reply packet\n");
3842         }
3843
3844         return result;
3845 }
3846
3847 static struct packet *client_lb_key_down(pid_t pid, int handle, const struct packet *packet)
3848 {
3849         struct client_node *client;
3850         const char *pkgname;
3851         const char *id;
3852         int ret;
3853         double timestamp;
3854         int x;
3855         int y;
3856         struct inst_info *inst = NULL;
3857         const struct pkg_info *pkg = NULL;
3858
3859         client = client_find_by_rpc_handle(handle);
3860         if (!client) {
3861                 ErrPrint("Client %d is not exists\n", pid);
3862                 ret = LB_STATUS_ERROR_NOT_EXIST;
3863                 goto out;
3864         }
3865
3866         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3867         if (ret != 7) {
3868                 ErrPrint("Parameter is not matched\n");
3869                 ret = LB_STATUS_ERROR_INVALID;
3870                 goto out;
3871         }
3872
3873         ret = validate_request(pkgname, id, &inst, &pkg);
3874         if (ret != LB_STATUS_SUCCESS) {
3875                 goto out;
3876         }
3877
3878         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3879                 ret = forward_lb_event_packet(pkg, inst, packet);
3880         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3881                 struct script_info *script;
3882                 Evas *e;
3883
3884                 script = instance_lb_script(inst);
3885                 if (!script) {
3886                         ret = LB_STATUS_ERROR_FAULT;
3887                         goto out;
3888                 }
3889
3890                 e = script_handler_evas(script);
3891                 if (!e) {
3892                         ret = LB_STATUS_ERROR_FAULT;
3893                         goto out;
3894                 }
3895
3896                 script_handler_update_pointer(script, x, y, -1);
3897
3898                 /*!
3899                  * \TODO: Feed up this KEY_DOWN event
3900                  */
3901                 ret = 0;
3902         } else {
3903                 ErrPrint("Unsupported package\n");
3904                 ret = LB_STATUS_ERROR_INVALID;
3905         }
3906
3907 out:
3908         /*! \note No reply packet */
3909         return NULL;
3910 }
3911
3912 static struct packet *client_lb_key_up(pid_t pid, int handle, const struct packet *packet)
3913 {
3914         struct client_node *client;
3915         const char *pkgname;
3916         const char *id;
3917         int ret;
3918         double timestamp;
3919         int x;
3920         int y;
3921         struct inst_info *inst = NULL;
3922         const struct pkg_info *pkg = NULL;
3923
3924         client = client_find_by_rpc_handle(handle);
3925         if (!client) {
3926                 ErrPrint("Client %d is not exists\n", pid);
3927                 ret = LB_STATUS_ERROR_NOT_EXIST;
3928                 goto out;
3929         }
3930
3931         ret = packet_get(packet, "ssdii", &pkgname, &id, &timestamp, &x, &y);
3932         if (ret != 7) {
3933                 ErrPrint("Parameter is not matched\n");
3934                 ret = LB_STATUS_ERROR_INVALID;
3935                 goto out;
3936         }
3937
3938         ret = validate_request(pkgname, id, &inst, &pkg);
3939         if (ret != LB_STATUS_SUCCESS) {
3940                 goto out;
3941         }
3942
3943         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3944                 ret = forward_lb_event_packet(pkg, inst, packet);
3945         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
3946                 struct script_info *script;
3947                 Evas *e;
3948
3949                 script = instance_lb_script(inst);
3950                 if (!script) {
3951                         ret = LB_STATUS_ERROR_FAULT;
3952                         goto out;
3953                 }
3954
3955                 e = script_handler_evas(script);
3956                 if (!e) {
3957                         ret = LB_STATUS_ERROR_FAULT;
3958                         goto out;
3959                 }
3960
3961                 script_handler_update_pointer(script, x, y, -1);
3962
3963                 /*!
3964                  * \TODO: Feed up this KEY_UP event
3965                  */
3966                 ret = 0;
3967         } else {
3968                 ErrPrint("Unsupported package\n");
3969                 ret = LB_STATUS_ERROR_INVALID;
3970         }
3971
3972 out:
3973         /*! \note No reply packet */
3974         return NULL;
3975 }
3976
3977 static int release_pixmap_cb(struct client_node *client, void *canvas)
3978 {
3979         DbgPrint("Forcely unref the \"buffer\"\n");
3980         buffer_handler_pixmap_unref(canvas);
3981         return -1; /* Delete this callback */
3982 }
3983
3984 static struct packet *client_lb_acquire_pixmap(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3985 {
3986         struct packet *result;
3987         const char *pkgname;
3988         const char *id;
3989         struct client_node *client;
3990         struct inst_info *inst = NULL;
3991         int ret;
3992         int pixmap = 0;
3993         void *buf_ptr;
3994         struct buffer_info *buffer;
3995
3996         client = client_find_by_rpc_handle(handle);
3997         if (!client) {
3998                 ErrPrint("Client %d is not exists\n", pid);
3999                 ret = LB_STATUS_ERROR_INVALID;
4000                 goto out;
4001         }
4002
4003         ret = packet_get(packet, "ss", &pkgname, &id);
4004         if (ret != 2) {
4005                 ErrPrint("Parameter is not matched\n");
4006                 ret = LB_STATUS_ERROR_INVALID;
4007                 goto out;
4008         }
4009
4010         ret = validate_request(pkgname, id, &inst, NULL);
4011         if (ret != LB_STATUS_SUCCESS) {
4012                 goto out;
4013         }
4014
4015         buffer = instance_lb_buffer(inst);
4016         if (!buffer) {
4017                 struct script_info *script_info;
4018                 struct fb_info *fb_info;
4019
4020                 script_info = instance_lb_script(inst);
4021                 if (!script_info) {
4022                         ErrPrint("Unable to get LB buffer: %s\n", id);
4023                         ret = LB_STATUS_ERROR_FAULT;
4024                         goto out;
4025                 }
4026
4027                 fb_info = script_handler_fb(script_info);
4028                 if (!fb_info) {
4029                         ErrPrint("Unable to get fb_info: %s\n", id);
4030                         ret = LB_STATUS_ERROR_FAULT;
4031                         goto out;
4032                 }
4033
4034                 buffer = fb_buffer_info(fb_info);
4035                 if (!buffer) {
4036                         ErrPrint("Unable to get buffer_info: %s\n", id);
4037                         ret = LB_STATUS_ERROR_FAULT;
4038                         goto out;
4039                 }
4040         }
4041
4042         buf_ptr = buffer_handler_pixmap_ref(buffer);
4043         if (!buf_ptr) {
4044                 ErrPrint("Failed to ref pixmap\n");
4045                 ret = LB_STATUS_ERROR_FAULT;
4046                 goto out;
4047         }
4048
4049         ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
4050         if (ret < 0) {
4051                 ErrPrint("Failed to add a new client deactivate callback\n");
4052                 buffer_handler_pixmap_unref(buf_ptr);
4053         } else {
4054                 pixmap = buffer_handler_pixmap(buffer);
4055                 ret = LB_STATUS_SUCCESS;
4056         }
4057
4058 out:
4059         result = packet_create_reply(packet, "ii", pixmap, ret);
4060         if (!result) {
4061                 ErrPrint("Failed to create a reply packet\n");
4062         }
4063
4064         return result;
4065 }
4066
4067 static struct packet *client_lb_release_pixmap(pid_t pid, int handle, const struct packet *packet)
4068 {
4069         const char *pkgname;
4070         const char *id;
4071         struct client_node *client;
4072         struct inst_info *inst = NULL;
4073         int pixmap;
4074         void *buf_ptr;
4075         int ret;
4076
4077         client = client_find_by_rpc_handle(handle);
4078         if (!client) {
4079                 ErrPrint("Client %d is not exists\n", pid);
4080                 goto out;
4081         }
4082
4083         ret = packet_get(packet, "ssi", &pkgname, &id, &pixmap);
4084         if (ret != 3) {
4085                 ErrPrint("Parameter is not matched\n");
4086                 goto out;
4087         }
4088
4089         ret = validate_request(pkgname, id, &inst, NULL);
4090         if (ret != LB_STATUS_SUCCESS) {
4091                 goto out;
4092         }
4093
4094         buf_ptr = buffer_handler_pixmap_find(pixmap);
4095         if (!buf_ptr) {
4096                 ErrPrint("Failed to find a buf_ptr of 0x%X\n", pixmap);
4097                 goto out;
4098         }
4099
4100         if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr) == 0) {
4101                 buffer_handler_pixmap_unref(buf_ptr);
4102         }
4103
4104 out:
4105         /*! \note No reply packet */
4106         return NULL;
4107 }
4108
4109 static struct packet *client_pd_acquire_pixmap(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
4110 {
4111         struct packet *result;
4112         const char *pkgname;
4113         const char *id;
4114         struct client_node *client;
4115         struct inst_info *inst = NULL;
4116         int ret;
4117         int pixmap = 0;
4118         void *buf_ptr;
4119         struct buffer_info *buffer;
4120
4121         client = client_find_by_rpc_handle(handle);
4122         if (!client) {
4123                 ret = LB_STATUS_ERROR_INVALID;
4124                 ErrPrint("Client %d is not exists\n", pid);
4125                 goto out;
4126         }
4127
4128         ret = packet_get(packet, "ss", &pkgname, &id);
4129         if (ret != 2) {
4130                 ret = LB_STATUS_ERROR_INVALID;
4131                 ErrPrint("Parameter is not matched\n");
4132                 goto out;
4133         }
4134
4135         ret = validate_request(pkgname, id, &inst, NULL);
4136         if (ret != LB_STATUS_SUCCESS) {
4137                 goto out;
4138         }
4139
4140         if (instance_get_data(inst, "pd,resize,monitor")) {
4141                 ret = LB_STATUS_ERROR_BUSY;
4142                 goto out;
4143         }
4144
4145         buffer = instance_pd_buffer(inst);
4146         if (!buffer) {
4147                 struct script_info *script_info;
4148                 struct fb_info *fb_info;
4149
4150                 script_info = instance_pd_script(inst);
4151                 if (!script_info) {
4152                         ErrPrint("Unable to get LB buffer: %s\n", id);
4153                         ret = LB_STATUS_ERROR_FAULT;
4154                         goto out;
4155                 }
4156
4157                 fb_info = script_handler_fb(script_info);
4158                 if (!fb_info) {
4159                         ErrPrint("Unable to get fb_info: %s\n", id);
4160                         ret = LB_STATUS_ERROR_FAULT;
4161                         goto out;
4162                 }
4163
4164                 buffer = fb_buffer_info(fb_info);
4165                 if (!buffer) {
4166                         ErrPrint("Unable to get buffer_info: %s\n", id);
4167                         ret = LB_STATUS_ERROR_FAULT;
4168                         goto out;
4169                 }
4170         }
4171
4172         buf_ptr = buffer_handler_pixmap_ref(buffer);
4173         if (!buf_ptr) {
4174                 ErrPrint("Failed to ref pixmap\n");
4175                 ret = LB_STATUS_ERROR_FAULT;
4176                 goto out;
4177         }
4178
4179         ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
4180         if (ret < 0) {
4181                 buffer_handler_pixmap_unref(buf_ptr);
4182         }
4183
4184         pixmap = buffer_handler_pixmap(buffer);
4185 out:
4186         result = packet_create_reply(packet, "ii", pixmap, ret);
4187         if (!result) {
4188                 ErrPrint("Failed to create a reply packet\n");
4189         }
4190
4191         return result;
4192 }
4193
4194 static struct packet *client_pd_release_pixmap(pid_t pid, int handle, const struct packet *packet)
4195 {
4196         const char *pkgname;
4197         const char *id;
4198         struct client_node *client;
4199         int pixmap;
4200         void *buf_ptr;
4201         int ret;
4202
4203         client = client_find_by_rpc_handle(handle);
4204         if (!client) {
4205                 ErrPrint("Client %d is not exists\n", pid);
4206                 goto out;
4207         }
4208
4209         ret = packet_get(packet, "ssi", &pkgname, &id, &pixmap);
4210         if (ret != 3) {
4211                 ErrPrint("Parameter is not matched\n");
4212                 goto out;
4213         }
4214
4215         ret = validate_request(pkgname, id, NULL, NULL);
4216         if (ret != LB_STATUS_SUCCESS) {
4217                 goto out;
4218         }
4219
4220         buf_ptr = buffer_handler_pixmap_find(pixmap);
4221         if (!buf_ptr) {
4222                 ErrPrint("Failed to find a buf_ptr of 0x%X\n", pixmap);
4223                 goto out;
4224         }
4225
4226         if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr) == 0) {
4227                 buffer_handler_pixmap_unref(buf_ptr);
4228         }
4229
4230 out:
4231         /*! \note No reply packet */
4232         return NULL;
4233 }
4234
4235 static struct packet *client_pinup_changed(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, pinup, ret */
4236 {
4237         struct client_node *client;
4238         struct packet *result;
4239         const char *pkgname;
4240         const char *id;
4241         int pinup;
4242         int ret;
4243         struct inst_info *inst = NULL;
4244
4245         client = client_find_by_rpc_handle(handle);
4246         if (!client) {
4247                 ErrPrint("Client %d is not exists\n", pid);
4248                 ret = LB_STATUS_ERROR_NOT_EXIST;
4249                 pinup = 0;
4250                 goto out;
4251         }
4252
4253         ret = packet_get(packet, "ssi", &pkgname, &id, &pinup);
4254         if (ret != 3) {
4255                 ErrPrint("Parameter is not matched\n");
4256                 ret = LB_STATUS_ERROR_INVALID;
4257                 pinup = 0;
4258                 goto out;
4259         }
4260
4261         ret = validate_request(pkgname, id, &inst, NULL);
4262         if (ret == LB_STATUS_SUCCESS) {
4263                 ret = instance_set_pinup(inst, pinup);
4264         }
4265
4266 out:
4267         result = packet_create_reply(packet, "i", ret);
4268         if (!result) {
4269                 ErrPrint("Failed to create a packet\n");
4270         }
4271
4272         return result;
4273 }
4274
4275 static Eina_Bool lazy_pd_created_cb(void *inst)
4276 {
4277         struct pkg_info *pkg;
4278
4279         if (!instance_del_data(inst, "lazy,pd,open")) {
4280                 ErrPrint("lazy,pd,open is already deleted.\n");
4281                 return ECORE_CALLBACK_CANCEL;
4282         }
4283
4284         pkg = instance_package(inst);
4285         if (pkg) {
4286                 struct slave_node *slave;
4287
4288                 slave = package_slave(pkg);
4289                 if (slave) {
4290                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_script_cb, inst);
4291                 }
4292         }
4293
4294         /*!
4295          * After unref instance first,
4296          * if the instance is not destroyed, try to notify the created PD event to the client.
4297          */
4298         if (instance_unref(inst)) {
4299                 int ret;
4300                 ret = instance_client_pd_created(inst, LB_STATUS_SUCCESS);
4301                 DbgPrint("Send PD Create event (%d) to client\n", ret);
4302         }
4303
4304         return ECORE_CALLBACK_CANCEL;
4305 }
4306
4307 static Eina_Bool lazy_pd_destroyed_cb(void *inst)
4308 {
4309         struct pkg_info *pkg;
4310         struct slave_node *slave;
4311
4312         if (!instance_del_data(inst, "lazy,pd,close")) {
4313                 ErrPrint("lazy,pd,close is already deleted.\n");
4314                 return ECORE_CALLBACK_CANCEL;
4315         }
4316
4317         pkg = instance_package(inst);
4318         if (pkg) {
4319                 slave = package_slave(pkg);
4320                 if (slave) {
4321                         if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
4322                                 slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_script_cb, inst);
4323                         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
4324                                 slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
4325                         }
4326                 }
4327         }
4328
4329         if (instance_unref(inst)) {
4330                 int ret;
4331                 ret = instance_client_pd_destroyed(inst, LB_STATUS_SUCCESS);
4332                 if (ret < 0) {
4333                         ErrPrint("Failed sending PD Destroy event (%d)\n", ret);
4334                 }
4335         }
4336
4337         return ECORE_CALLBACK_CANCEL;
4338 }
4339
4340 static struct packet *client_pd_move(pid_t pid, int handle, const struct packet *packet) /* pkgname, id, x, y */
4341 {
4342         struct client_node *client;
4343         struct inst_info *inst = NULL;
4344         const struct pkg_info *pkg = NULL;
4345         const char *pkgname;
4346         const char *id;
4347         double x = 0.0f;
4348         double y = 0.0f;
4349         int ret;
4350
4351         client = client_find_by_rpc_handle(handle);
4352         if (!client) {
4353                 ErrPrint("Client %d is not exists\n", pid);
4354                 ret = LB_STATUS_ERROR_NOT_EXIST;
4355                 goto out;
4356         }
4357
4358         ret = packet_get(packet, "ssdd", &pkgname, &id, &x, &y);
4359         if (ret != 4) {
4360                 ErrPrint("Parameter is not correct\n");
4361                 ret = LB_STATUS_ERROR_INVALID;
4362                 goto out;
4363         }
4364
4365         ret = validate_request(pkgname, id, &inst, &pkg);
4366         if (ret != LB_STATUS_SUCCESS) {
4367                 goto out;
4368         }
4369
4370         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
4371                 instance_slave_set_pd_pos(inst, x, y);
4372                 ret = instance_signal_emit(inst, "pd,move", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
4373         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
4374                 int ix;
4375                 int iy;
4376
4377                 instance_slave_set_pd_pos(inst, x, y);
4378                 ix = x * instance_pd_width(inst);
4379                 iy = y * instance_pd_height(inst);
4380                 script_handler_update_pointer(instance_pd_script(inst), ix, iy, 0);
4381                 ret = instance_signal_emit(inst, "pd,move", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
4382         } else {
4383                 ErrPrint("Invalid PD type\n");
4384                 ret = LB_STATUS_ERROR_INVALID;
4385         }
4386 out:
4387         DbgPrint("Update PD position: %d\n", ret);
4388         return NULL;
4389 }
4390
4391 static Eina_Bool pd_open_monitor_cb(void *inst)
4392 {
4393         int ret;
4394         struct pkg_info *pkg;
4395
4396         pkg = instance_package(inst);
4397         if (pkg) {
4398                 struct slave_node *slave;
4399
4400                 slave = package_slave(pkg);
4401                 if (slave) {
4402                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst);
4403                 }
4404         }
4405
4406         ret = instance_slave_close_pd(inst, instance_pd_owner(inst));
4407         ret = instance_client_pd_created(inst, LB_STATUS_ERROR_TIMEOUT);
4408         (void)instance_del_data(inst, "pd,open,monitor");
4409         (void)instance_unref(inst);
4410         ErrPrint("PD Open request is timed-out (%lf), ret: %d\n", PD_REQUEST_TIMEOUT, ret);
4411         return ECORE_CALLBACK_CANCEL;
4412 }
4413
4414 static Eina_Bool pd_close_monitor_cb(void *inst)
4415 {
4416         int ret;
4417         struct pkg_info *pkg;
4418
4419         pkg = instance_package(inst);
4420         if (pkg) {
4421                 struct slave_node *slave;
4422
4423                 slave = package_slave(pkg);
4424                 if (slave) {
4425                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
4426                 }
4427         }
4428
4429         ret = instance_client_pd_destroyed(inst, LB_STATUS_ERROR_TIMEOUT);
4430         (void)instance_del_data(inst, "pd,close,monitor");
4431         (void)instance_unref(inst);
4432         ErrPrint("PD Close request is not processed in %lf seconds (%d)\n", PD_REQUEST_TIMEOUT, ret);
4433         return ECORE_CALLBACK_CANCEL;
4434 }
4435
4436 static Eina_Bool pd_resize_monitor_cb(void *inst)
4437 {
4438         int ret;
4439         struct pkg_info *pkg;
4440
4441         pkg = instance_package(inst);
4442         if (pkg) {
4443                 struct slave_node *slave;
4444                 slave = package_slave(pkg);
4445                 if (slave) {
4446                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst);
4447                 }
4448         }
4449
4450         ret = instance_slave_close_pd(inst, instance_pd_owner(inst));
4451         ret = instance_client_pd_destroyed(inst, LB_STATUS_ERROR_TIMEOUT);
4452         (void)instance_del_data(inst, "pd,resize,monitor");
4453         (void)instance_unref(inst);
4454         ErrPrint("PD Resize request is not processed in %lf seconds (%d)\n", PD_REQUEST_TIMEOUT, ret);
4455         return ECORE_CALLBACK_CANCEL;
4456 }
4457
4458 static struct packet *client_create_pd(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
4459 {
4460         struct client_node *client;
4461         struct packet *result;
4462         const char *pkgname;
4463         const char *id;
4464         int ret;
4465         struct inst_info *inst = NULL;
4466         const struct pkg_info *pkg = NULL;
4467         Ecore_Timer *pd_monitor;
4468         double x;
4469         double y;
4470
4471         DbgPrint("PERF_DBOX\n");
4472
4473         client = client_find_by_rpc_handle(handle);
4474         if (!client) {
4475                 ErrPrint("Client %d is not exists\n", pid);
4476                 ret = LB_STATUS_ERROR_NOT_EXIST;
4477                 goto out;
4478         }
4479
4480         ret = packet_get(packet, "ssdd", &pkgname, &id, &x, &y);
4481         if (ret != 4) {
4482                 ErrPrint("Parameter is not matched\n");
4483                 ret = LB_STATUS_ERROR_INVALID;
4484                 goto out;
4485         }
4486
4487         ret = validate_request(pkgname, id, &inst, &pkg);
4488         if (ret != LB_STATUS_SUCCESS) {
4489                 goto out;
4490         }
4491
4492         if (instance_pd_owner(inst)) {
4493                 ErrPrint("PD is already owned\n");
4494                 ret = LB_STATUS_ERROR_ALREADY;
4495         } else if (package_pd_type(instance_package(inst)) == PD_TYPE_BUFFER) {
4496                 pd_monitor = instance_get_data(inst, "lazy,pd,close");
4497                 if (pd_monitor) {
4498                         ecore_timer_del(pd_monitor);
4499                         /* This timer attribute will be deleted */
4500                         lazy_pd_destroyed_cb(inst);
4501                 }
4502                 
4503                 if (instance_get_data(inst, "pd,open,monitor")) {
4504                         DbgPrint("PD Open request is already processed\n");
4505                         ret = LB_STATUS_ERROR_ALREADY;
4506                         goto out;
4507                 }
4508
4509                 if (instance_get_data(inst, "pd,close,monitor")) {
4510                         DbgPrint("PD Close request is already in process\n");
4511                         ret = LB_STATUS_ERROR_BUSY;
4512                         goto out;
4513                 }
4514
4515                 if (instance_get_data(inst, "pd,resize,monitor")) {
4516                         DbgPrint("PD resize request is already in process\n");
4517                         ret = LB_STATUS_ERROR_BUSY;
4518                         goto out;
4519                 }
4520
4521                 instance_slave_set_pd_pos(inst, x, y);
4522                 /*!
4523                  * \note
4524                  * Send request to the slave.
4525                  * The SLAVE must has to repsonse this via "release_buffer" method.
4526                  */
4527                 ret = instance_slave_open_pd(inst, client);
4528                 if (ret == LB_STATUS_SUCCESS) {
4529                         ret = instance_signal_emit(inst, "pd,show", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
4530                         if (ret != LB_STATUS_SUCCESS) {
4531                                 int tmp_ret;
4532
4533                                 tmp_ret = instance_slave_close_pd(inst, client);
4534                                 ErrPrint("Unable to send script event for openning PD [%s], %d\n", pkgname, tmp_ret);
4535                         } else {
4536                                 pd_monitor = ecore_timer_add(PD_REQUEST_TIMEOUT, pd_open_monitor_cb, instance_ref(inst));
4537                                 if (!pd_monitor) {
4538                                         (void)instance_unref(inst);
4539                                         ErrPrint("Failed to create a timer for PD Open monitor\n");
4540                                 } else {
4541                                         struct slave_node *slave;
4542
4543                                         (void)instance_set_data(inst, "pd,open,monitor", pd_monitor);
4544
4545                                         slave = package_slave(pkg);
4546                                         if (!slave) {
4547                                                 ErrPrint("Failed to get slave(%s)\n", pkgname);
4548                                                 goto out;
4549                                         }
4550
4551                                         if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst) != LB_STATUS_SUCCESS) {
4552                                                 ErrPrint("Failed to add fault handler: %s\n");
4553                                         }
4554                                 }
4555                         }
4556                 } else {
4557                         ErrPrint("Unable to send request for openning PD [%s]\n", pkgname);
4558                 }
4559
4560                 /*!
4561                  * \note
4562                  * PD craeted event will be send by the acquire_buffer function.
4563                  * Because the slave will make request the acquire_buffer to
4564                  * render the PD
4565                  *
4566                  * instance_client_pd_created(inst);
4567                  */
4568         } else if (package_pd_type(instance_package(inst)) == PD_TYPE_SCRIPT) {
4569                 int ix;
4570                 int iy;
4571
4572                 pd_monitor = instance_get_data(inst, "lazy,pd,close");
4573                 if (pd_monitor) {
4574                         ecore_timer_del(pd_monitor);
4575                         /* lazy,pd,close will be deleted */
4576                         lazy_pd_destroyed_cb(inst);
4577                 }
4578
4579                 /*!
4580                  * \note
4581                  * ret value should be cared but in this case,
4582                  * we ignore this for this moment, so we have to handle this error later.
4583                  *
4584                  * if ret is less than 0, the slave has some problem.
4585                  * but the script mode doesn't need slave for rendering default view of PD
4586                  * so we can hanle it later.
4587                  */
4588                 instance_slave_set_pd_pos(inst, x, y);
4589                 ix = x * instance_pd_width(inst);
4590                 iy = y * instance_pd_height(inst);
4591
4592                 script_handler_update_pointer(instance_pd_script(inst), ix, iy, 0);
4593
4594                 ret = instance_slave_open_pd(inst, client);
4595                 if (ret == LB_STATUS_SUCCESS) {
4596                         ret = script_handler_load(instance_pd_script(inst), 1);
4597
4598                         /*!
4599                          * \note
4600                          * Send the PD created event to the clients,
4601                          */
4602                         if (ret == LB_STATUS_SUCCESS) {
4603
4604                                 /*!
4605                                  * \note
4606                                  * But the created event has to be send afte return
4607                                  * from this function or the viewer couldn't care
4608                                  * the event correctly.
4609                                  */
4610                                 inst = instance_ref(inst); /* To guarantee the inst */
4611
4612                                 /*!
4613                                  * \note
4614                                  * At here, we don't need to rememeber the timer object.
4615                                  * Even if the timer callback is called, after the instance is destroyed.
4616                                  * lazy_pd_created_cb will decrease the instance refcnt first.
4617                                  * At that time, if the instance is released, the timer callback will do nothing.
4618                                  *
4619                                  * 13-05-28
4620                                  * I change my mind. There is no requirements to keep the timer handler.
4621                                  * But I just add it to the tagged-data of the instance.
4622                                  * Just reserve for future-use.
4623                                  */
4624                                 pd_monitor = ecore_timer_add(DELAY_TIME, lazy_pd_created_cb, inst);
4625                                 if (!pd_monitor) {
4626                                         ret = script_handler_unload(instance_pd_script(inst), 1);
4627                                         ErrPrint("Unload script: %d\n", ret);
4628
4629                                         ret = instance_slave_close_pd(inst, client);
4630                                         ErrPrint("Close PD %d\n", ret);
4631
4632                                         inst = instance_unref(inst);
4633                                         if (!inst) {
4634                                                 DbgPrint("Instance destroyed\n");
4635                                         }
4636
4637                                         ErrPrint("Instance: %s\n", pkgname);
4638
4639                                         ret = LB_STATUS_ERROR_FAULT;
4640                                 } else {
4641                                         struct slave_node *slave;
4642
4643                                         (void)instance_set_data(inst, "lazy,pd,open", pd_monitor);
4644
4645                                         slave = package_slave(pkg);
4646                                         if (!slave) {
4647                                                 ErrPrint("Failed to get slave: %s\n", pkgname);
4648                                                 goto out;
4649                                         }
4650
4651                                         if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_script_cb, inst) != LB_STATUS_SUCCESS) {
4652                                                 ErrPrint("Failed to add fault callback: %s\n", pkgname);
4653                                         }
4654                                 }
4655                         } else {
4656                                 int tmp_ret;
4657                                 tmp_ret = instance_slave_close_pd(inst, client);
4658                                 ErrPrint("Unable to load script: %d, (close: %d)\n", ret, tmp_ret);
4659                         }
4660                 } else {
4661                         ErrPrint("Unable open PD(%s): %d\n", pkgname, ret);
4662                 }
4663         } else {
4664                 ErrPrint("Invalid PD TYPE\n");
4665                 ret = LB_STATUS_ERROR_INVALID;
4666         }
4667
4668 out:
4669         result = packet_create_reply(packet, "i", ret);
4670         if (!result) {
4671                 ErrPrint("Failed to create a packet\n");
4672         }
4673
4674         return result;
4675 }
4676
4677 static struct packet *client_destroy_pd(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
4678 {
4679         struct client_node *client;
4680         struct packet *result;
4681         const char *pkgname;
4682         const char *id;
4683         int ret;
4684         struct inst_info *inst = NULL;
4685         const struct pkg_info *pkg = NULL;
4686         Ecore_Timer *pd_monitor;
4687         struct slave_node *slave;
4688
4689         DbgPrint("PERF_DBOX\n");
4690
4691         client = client_find_by_rpc_handle(handle);
4692         if (!client) {
4693                 ErrPrint("Client %d is not exists\n", pid);
4694                 ret = LB_STATUS_ERROR_NOT_EXIST;
4695                 goto out;
4696         }
4697
4698         ret = packet_get(packet, "ss", &pkgname, &id);
4699         if (ret != 2) {
4700                 ErrPrint("Parameter is not matched\n");
4701                 ret = LB_STATUS_ERROR_INVALID;
4702                 goto out;
4703         }
4704
4705         ret = validate_request(pkgname, id, &inst, &pkg);
4706         if (ret != LB_STATUS_SUCCESS) {
4707                 goto out;
4708         }
4709
4710         slave = package_slave(pkg);
4711         if (!slave) {
4712                 ret = LB_STATUS_ERROR_INVALID;
4713                 goto out;
4714         }
4715
4716         if (instance_pd_owner(inst) != client) {
4717                 if (instance_pd_owner(inst) == NULL) {
4718                         ErrPrint("PD looks already closed\n");
4719                         ret = LB_STATUS_ERROR_ALREADY;
4720                 } else {
4721                         ErrPrint("PD owner mimatched\n");
4722                         ret = LB_STATUS_ERROR_PERMISSION;
4723                 }
4724         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
4725                 int resize_aborted = 0;
4726
4727                 pd_monitor = instance_del_data(inst, "pd,open,monitor");
4728                 if (pd_monitor) {
4729                         
4730                         ErrPrint("PD Open request is found. cancel it [%s]\n", pkgname);
4731
4732                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst);
4733
4734                         /*!
4735                          * \note
4736                          * We should return negative value
4737                          * Or we have to send "destroyed" event to the client.
4738                          * If we didn't send destroyed event after return SUCCESS from here,
4739                          * The client will permanently waiting destroyed event.
4740                          * Because they understand that the destroy request is successfully processed.
4741                          */
4742                         ret = LB_STATUS_ERROR_CANCEL;
4743                         ret = instance_client_pd_created(inst, ret);
4744                         if (ret < 0) {
4745                                 ErrPrint("PD client create event: %d\n", ret);
4746                         }
4747
4748                         ret = instance_client_pd_destroyed(inst, LB_STATUS_SUCCESS);
4749                         if (ret < 0) {
4750                                 ErrPrint("PD client destroy event: %d\n", ret);
4751                         }
4752
4753                         ret = instance_signal_emit(inst, "pd,hide", instance_id(inst), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
4754                         if (ret < 0) {
4755                                 ErrPrint("PD close signal emit failed: %d\n", ret);
4756                         }
4757
4758                         ret = instance_slave_close_pd(inst, client);
4759                         if (ret < 0) {
4760                                 ErrPrint("PD close request failed: %d\n", ret);
4761                         }
4762
4763                         ecore_timer_del(pd_monitor);
4764                         (void)instance_unref(inst);
4765                         goto out;
4766                 }
4767
4768                 if (instance_get_data(inst, "lazy,pd,close") || instance_get_data(inst, "pd,close,monitor")) {
4769                         ret = LB_STATUS_ERROR_ALREADY;
4770                         goto out;
4771                 }
4772
4773                 pd_monitor = instance_del_data(inst, "pd,resize,monitor");
4774                 if (pd_monitor) {
4775                         ErrPrint("PD Resize request is found. clear it [%s]\n", pkgname);
4776                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst);
4777
4778                         ecore_timer_del(pd_monitor);
4779
4780                         inst = instance_unref(inst);
4781                         if (!inst) {
4782                                 goto out;
4783                         }
4784
4785                         resize_aborted = 1;
4786                 }
4787
4788                 ret = instance_signal_emit(inst, "pd,hide", instance_id(inst), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
4789                 if (ret < 0) {
4790                         ErrPrint("PD close signal emit failed: %d\n", ret);
4791                 }
4792
4793                 ret = instance_slave_close_pd(inst, client);
4794                 if (ret < 0) {
4795                         ErrPrint("PD close request failed: %d\n", ret);
4796                 } else if (resize_aborted) {
4797                         pd_monitor = ecore_timer_add(DELAY_TIME, lazy_pd_destroyed_cb, instance_ref(inst));
4798                         if (!pd_monitor) {
4799                                 ErrPrint("Failed to create a timer: %s\n", pkgname);
4800                                 (void)instance_unref(inst);
4801                         } else {
4802                                 (void)instance_set_data(inst, "lazy,pd,close", pd_monitor);
4803                                 slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
4804                         }
4805                 } else {
4806                         pd_monitor = ecore_timer_add(PD_REQUEST_TIMEOUT, pd_close_monitor_cb, instance_ref(inst));
4807                         if (!pd_monitor) {
4808                                 (void)instance_unref(inst);
4809                                 ErrPrint("Failed to add pd close monitor\n");
4810                         } else {
4811                                 (void)instance_set_data(inst, "pd,close,monitor", pd_monitor);
4812                                 slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
4813                         }
4814                 }
4815                 /*!
4816                  * \note
4817                  * release_buffer will be called by the slave after this.
4818                  * Then it will send the "pd_destroyed" event to the client
4819                  *
4820                  * instance_client_pd_destroyed(inst);
4821                  */
4822         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
4823                 pd_monitor = instance_get_data(inst, "lazy,pd,open");
4824                 if (pd_monitor) {
4825                         ecore_timer_del(pd_monitor);
4826                         lazy_pd_created_cb(inst);
4827                 }
4828
4829                 ret = script_handler_unload(instance_pd_script(inst), 1);
4830                 if (ret < 0) {
4831                         ErrPrint("Unable to unload the script: %s, %d\n", pkgname, ret);
4832                 }
4833
4834                 /*!
4835                  * \note
4836                  * Send request to the slave.
4837                  * The SLAVE must has to repsonse this via "release_buffer" method.
4838                  */
4839                 ret = instance_slave_close_pd(inst, client);
4840                 if (ret < 0) {
4841                         ErrPrint("Unable to close the PD: %s, %d\n", pkgname, ret);
4842                 }
4843
4844                 /*!
4845                  * \note
4846                  * Send the destroyed PD event to the client
4847                  */
4848                 if (ret == LB_STATUS_SUCCESS) {
4849                         /*!
4850                          * \note
4851                          * 13-05-28
4852                          * I change my mind. There is no requirements to keep the timer handler.
4853                          * But I just add it to the tagged-data of the instance.
4854                          * Just reserve for future-use.
4855                          */
4856                         pd_monitor = ecore_timer_add(DELAY_TIME, lazy_pd_destroyed_cb, instance_ref(inst));
4857                         if (!pd_monitor) {
4858                                 ErrPrint("Failed to create a timer: %s\n", pkgname);
4859                                 (void)instance_unref(inst);
4860                         } else {
4861                                 (void)instance_set_data(inst, "lazy,pd,close", pd_monitor);
4862                                 slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_script_cb, inst);
4863                         }
4864                 }
4865         } else {
4866                 ErrPrint("Invalid PD TYPE\n");
4867                 ret = LB_STATUS_ERROR_INVALID;
4868         }
4869
4870 out:
4871         result = packet_create_reply(packet, "i", ret);
4872         if (!result) {
4873                 ErrPrint("Failed to create a packet\n");
4874         }
4875
4876         return result;
4877 }
4878
4879 static struct packet *client_activate_package(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, ret */
4880 {
4881         struct client_node *client;
4882         struct packet *result;
4883         const char *pkgname;
4884         int ret;
4885         struct pkg_info *info;
4886
4887         client = client_find_by_rpc_handle(handle);
4888         if (!client) {
4889                 ErrPrint("Client %d is not exists\n", pid);
4890                 ret = LB_STATUS_ERROR_NOT_EXIST;
4891                 pkgname = "";
4892                 goto out;
4893         }
4894
4895         ret = packet_get(packet, "s", &pkgname);
4896         if (ret != 1) {
4897                 ErrPrint("Parameter is not matched\n");
4898                 ret = LB_STATUS_ERROR_INVALID;
4899                 pkgname = "";
4900                 goto out;
4901         }
4902
4903         DbgPrint("pid[%d] pkgname[%s]\n", pid, pkgname);
4904
4905         /*!
4906          * \NOTE:
4907          * Validate the livebox package name.
4908          */
4909         if (!package_is_lb_pkgname(pkgname)) {
4910                 ErrPrint("%s is not a valid livebox package\n", pkgname);
4911                 pkgname = "";
4912                 ret = LB_STATUS_ERROR_INVALID;
4913                 goto out;
4914         }
4915
4916         info = package_find(pkgname);
4917         if (!info) {
4918                 ret = LB_STATUS_ERROR_NOT_EXIST;
4919         } else {
4920                 ret = package_clear_fault(info);
4921         }
4922
4923 out:
4924         result = packet_create_reply(packet, "is", ret, pkgname);
4925         if (!result) {
4926                 ErrPrint("Failed to create a packet\n");
4927         }
4928
4929         return result;
4930 }
4931
4932 static struct packet *client_subscribed(pid_t pid, int handle, const struct packet *packet)
4933 {
4934         const char *cluster;
4935         const char *category;
4936         struct client_node *client;
4937         int ret;
4938
4939         client = client_find_by_rpc_handle(handle);
4940         if (!client) {
4941                 ErrPrint("Client %d is not exists\n", pid);
4942                 ret = LB_STATUS_ERROR_NOT_EXIST;
4943                 goto out;
4944         }
4945
4946         ret = packet_get(packet, "ss", &cluster, &category);
4947         if (ret != 2) {
4948                 ErrPrint("Invalid argument\n");
4949                 ret = LB_STATUS_ERROR_INVALID;
4950                 goto out;
4951         }
4952
4953         DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster, category);
4954         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
4955                 ErrPrint("Invalid cluster name\n");
4956                 goto out;
4957         }
4958
4959         /*!
4960          * \todo
4961          * SUBSCRIBE cluster & sub-cluster for a client.
4962          */
4963         ret = client_subscribe(client, cluster, category);
4964         if (ret == 0) {
4965                 package_alter_instances_to_client(client, ALTER_CREATE);
4966         }
4967
4968 out:
4969         /*! \note No reply packet */
4970         return NULL;
4971 }
4972
4973 static struct packet *client_delete_cluster(pid_t pid, int handle, const struct packet *packet)
4974 {
4975         const char *cluster;
4976         struct client_node *client;
4977         struct packet *result;
4978         int ret;
4979
4980         client = client_find_by_rpc_handle(handle);
4981         if (!client) {
4982                 ErrPrint("Client %d is not exists\n", pid);
4983                 ret = LB_STATUS_ERROR_NOT_EXIST;
4984                 goto out;
4985         }
4986
4987         ret = packet_get(packet, "s", &cluster);
4988         if (ret != 1) {
4989                 ErrPrint("Invalid parameters\n");
4990                 ret = LB_STATUS_ERROR_INVALID;
4991                 goto out;
4992         }
4993
4994         DbgPrint("pid[%d] cluster[%s]\n", pid, cluster);
4995
4996         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
4997                 ErrPrint("Invalid cluster: %s\n", cluster);
4998                 ret = LB_STATUS_ERROR_INVALID;
4999                 goto out;
5000         }
5001
5002         /*!
5003          * \todo
5004          */
5005         ret = LB_STATUS_ERROR_NOT_IMPLEMENTED;
5006
5007 out:
5008         result = packet_create_reply(packet, "i", ret);
5009         if (!result) {
5010                 ErrPrint("Failed to create a packet\n");
5011         }
5012
5013         return result;
5014 }
5015
5016 static inline int update_pkg_cb(struct category *category, const char *pkgname)
5017 {
5018         const char *c_name;
5019         const char *s_name;
5020
5021         c_name = group_cluster_name_by_category(category);
5022         s_name = group_category_name(category);
5023
5024         if (!c_name || !s_name || !pkgname) {
5025                 ErrPrint("Name is not valid\n");
5026                 return EXIT_FAILURE;
5027         }
5028
5029         DbgPrint("Send refresh request: %s (%s/%s)\n", pkgname, c_name, s_name);
5030         slave_rpc_request_update(pkgname, "", c_name, s_name);
5031
5032         /* Just try to create a new package */
5033         if (util_free_space(IMAGE_PATH) > MINIMUM_SPACE) {
5034                 double timestamp;
5035                 struct inst_info *inst;
5036
5037                 timestamp = util_timestamp();
5038                 /*!
5039                  * \NOTE
5040                  * Don't need to check the subscribed clients.
5041                  * Because this callback is called by the requests of clients.
5042                  * It means. some clients wants to handle this instances ;)
5043                  */
5044                 inst = instance_create(NULL, timestamp, pkgname, "", c_name, s_name, DEFAULT_PERIOD, 0, 0);
5045                 if (!inst) {
5046                         ErrPrint("Failed to create a new instance\n");
5047                 }
5048         } else {
5049                 ErrPrint("Not enough space\n");
5050         }
5051         return EXIT_SUCCESS;
5052 }
5053
5054 static struct packet *client_update(pid_t pid, int handle, const struct packet *packet)
5055 {
5056         struct inst_info *inst = NULL;
5057         struct client_node *client;
5058         const char *pkgname;
5059         const char *id;
5060         int ret;
5061
5062         client = client_find_by_rpc_handle(handle);
5063         if (!client) {
5064                 ErrPrint("Cilent %d is not exists\n", pid);
5065                 goto out;
5066         }
5067
5068         ret = packet_get(packet, "ss", &pkgname, &id);
5069         if (ret != 2) {
5070                 ErrPrint("Invalid argument\n");
5071                 goto out;
5072         }
5073
5074         ret = validate_request(pkgname, id, &inst, NULL);
5075         if (ret != LB_STATUS_SUCCESS) {
5076                 goto out;
5077         }
5078
5079         if (instance_client(inst) != client) {
5080                 /* PERMISSIONS */
5081                 ErrPrint("Insufficient permissions [%s] - %d\n", pkgname, pid);
5082         } else {
5083                 slave_rpc_request_update(pkgname, id, instance_cluster(inst), instance_category(inst));
5084         }
5085
5086 out:
5087         /*! \note No reply packet */
5088         return NULL;
5089 }
5090
5091 static struct packet *client_refresh_group(pid_t pid, int handle, const struct packet *packet)
5092 {
5093         const char *cluster_id;
5094         const char *category_id;
5095         struct client_node *client;
5096         int ret;
5097         struct cluster *cluster;
5098         struct category *category;
5099         struct context_info *info;
5100         Eina_List *info_list;
5101         Eina_List *l;
5102
5103         client = client_find_by_rpc_handle(handle);
5104         if (!client) {
5105                 ErrPrint("Cilent %d is not exists\n", pid);
5106                 goto out;
5107         }
5108
5109         ret = packet_get(packet, "ss", &cluster_id, &category_id);
5110         if (ret != 2) {
5111                 ErrPrint("Invalid parameter\n");
5112                 goto out;
5113         }
5114
5115         DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster_id, category_id);
5116
5117         if (!strlen(cluster_id) || !strcasecmp(cluster_id, DEFAULT_CLUSTER)) {
5118                 ErrPrint("Invalid cluster name: %s\n", cluster_id);
5119                 goto out;
5120         }
5121
5122         cluster = group_find_cluster(cluster_id);
5123         if (!cluster) {
5124                 ErrPrint("Cluster [%s] is not registered\n", cluster_id);
5125                 goto out;
5126         }
5127
5128         category = group_find_category(cluster, category_id);
5129         if (!category) {
5130                 ErrPrint("Category [%s] is not registered\n", category_id);
5131                 goto out;
5132         }
5133
5134         info_list = group_context_info_list(category);
5135         EINA_LIST_FOREACH(info_list, l, info) {
5136                 update_pkg_cb(category, group_pkgname_from_context_info(info));
5137         }
5138
5139 out:
5140         /*! \note No reply packet */
5141         return NULL;
5142 }
5143
5144 static struct packet *client_delete_category(pid_t pid, int handle, const struct packet *packet)
5145 {
5146         const char *cluster;
5147         const char *category;
5148         struct client_node *client;
5149         struct packet *result;
5150         int ret;
5151
5152         client = client_find_by_rpc_handle(handle);
5153         if (!client) {
5154                 ErrPrint("Client %d is not exists\n", pid);
5155                 ret = LB_STATUS_ERROR_NOT_EXIST;
5156                 goto out;
5157         }
5158
5159         ret = packet_get(packet, "ss", &cluster, &category);
5160         if (ret != 2) {
5161                 ErrPrint("Invalid paramenters\n");
5162                 ret = LB_STATUS_ERROR_INVALID;
5163                 goto out;
5164         }
5165
5166         DbgPrint("pid[%d] cluster[%s] category[%s]\n", pid, cluster, category);
5167         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
5168                 ErrPrint("Invalid cluster: %s\n", cluster);
5169                 ret = LB_STATUS_ERROR_INVALID;
5170                 goto out;
5171         }
5172
5173         /*!
5174          * \todo
5175          */
5176         ret = LB_STATUS_ERROR_NOT_IMPLEMENTED;
5177
5178 out:
5179         result = packet_create_reply(packet, "i", ret);
5180         if (!result) {
5181                 ErrPrint("Failed to create a packet\n");
5182         }
5183
5184         return result;
5185 }
5186
5187 static struct packet *client_unsubscribed(pid_t pid, int handle, const struct packet *packet)
5188 {
5189         const char *cluster;
5190         const char *category;
5191         struct client_node *client;
5192         int ret;
5193
5194         client = client_find_by_rpc_handle(handle);
5195         if (!client) {
5196                 ErrPrint("Client %d is not exists\n", pid);
5197                 ret = LB_STATUS_ERROR_NOT_EXIST;
5198                 goto out;
5199         }
5200
5201         ret = packet_get(packet, "ss", &cluster, &category);
5202         if (ret != 2) {
5203                 ErrPrint("Invalid argument\n");
5204                 ret = LB_STATUS_ERROR_INVALID;
5205                 goto out;
5206         }
5207
5208         DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster, category);
5209
5210         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
5211                 ErrPrint("Invalid cluster name: %s\n", cluster);
5212                 goto out;
5213         }
5214
5215         /*!
5216          * \todo
5217          * UNSUBSCRIBE cluster & sub-cluster for a client.
5218          */
5219         ret = client_unsubscribe(client, cluster, category);
5220         if (ret == 0) {
5221                 package_alter_instances_to_client(client, ALTER_DESTROY);
5222         }
5223
5224 out:
5225         /*! \note No reply packet */
5226         return NULL;
5227 }
5228
5229 static struct packet *slave_hello(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
5230 {
5231         struct slave_node *slave;
5232         const char *slavename;
5233         int ret;
5234
5235         ret = packet_get(packet, "s", &slavename);
5236         if (ret != 1) {
5237                 ErrPrint("Parameter is not matched\n");
5238                 goto out;
5239         }
5240
5241         DbgPrint("New slave[%s](%d) is arrived\n", slavename, pid);
5242
5243         slave = slave_find_by_name(slavename);
5244
5245         if (!slave) { /* Try again to find a slave using pid */
5246                 slave = slave_find_by_pid(pid);
5247         }
5248
5249         if (!slave) {
5250                 if (DEBUG_MODE) {
5251                         char pkgname[pathconf("/", _PC_PATH_MAX)];
5252                         const char *abi;
5253
5254                         if (aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname)) != AUL_R_OK) {
5255                                 ErrPrint("pid[%d] is not authroized provider package, try to find it using its name[%s]\n", pid, slavename);
5256                                 slave = slave_find_by_name(slavename);
5257                                 pkgname[0] = '\0'; /* Reset the pkgname */
5258                         } else {
5259                                 slave = slave_find_by_pkgname(pkgname);
5260                         }
5261
5262                         if (!slave) {
5263                                 abi = abi_find_by_pkgname(pkgname);
5264                                 if (!abi) {
5265                                         abi = DEFAULT_ABI;
5266                                         DbgPrint("Slave pkgname is invalid, ABI is replaced with '%s'(default)\n", abi);
5267                                 }
5268
5269                                 slave = slave_create(slavename, 1, abi, pkgname, 0);
5270                                 if (!slave) {
5271                                         ErrPrint("Failed to create a new slave for %s\n", slavename);
5272                                         goto out;
5273                                 }
5274
5275                                 DbgPrint("New slave is created (net: 0)\n");
5276                         } else {
5277                                 DbgPrint("Registered slave is replaced with this new one\n");
5278                                 abi = slave_abi(slave);
5279                                 if (!abi) {
5280                                         ErrPrint("ABI is not valid: %s\n", slavename);
5281                                         abi = DEFAULT_ABI;
5282                                 }
5283                         }
5284
5285                         slave_set_pid(slave, pid);
5286                         DbgPrint("Provider is forcely activated, pkgname(%s), abi(%s), slavename(%s)\n", pkgname, abi, slavename);
5287                 } else {
5288                         ErrPrint("Slave[%d, %s] is not exists\n", pid, slavename);
5289                         goto out;
5290                 }
5291         } else {
5292                 if (slave_pid(slave) != pid) {
5293                         if (slave_pid(slave) > 0) {
5294                                 CRITICAL_LOG("Slave(%s) is already assigned to %d\n", slave_name(slave), slave_pid(slave));
5295                                 if (pid > 0) {
5296                                         ret = aul_terminate_pid(pid);
5297                                         CRITICAL_LOG("Terminate %d (ret: %d)\n", pid, ret);
5298                                 }
5299                                 goto out;
5300                         }
5301                         CRITICAL_LOG("PID of slave(%s) is updated (%d -> %d)\n", slave_name(slave), slave_pid(slave), pid);
5302                         slave_set_pid(slave, pid);
5303                 }
5304         }
5305
5306         /*!
5307          * \note
5308          * After updating handle,
5309          * slave activated callback will be called.
5310          */
5311         slave_rpc_update_handle(slave, handle);
5312
5313 out:
5314         return NULL;
5315 }
5316
5317 static struct packet *slave_ping(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
5318 {
5319         struct slave_node *slave;
5320         const char *slavename;
5321         int ret;
5322
5323         slave = slave_find_by_pid(pid);
5324         if (!slave) {
5325                 ErrPrint("Slave %d is not exists\n", pid);
5326                 goto out;
5327         }
5328
5329         ret = packet_get(packet, "s", &slavename);
5330         if (ret != 1) {
5331                 ErrPrint("Parameter is not matched\n");
5332         } else {
5333                 slave_rpc_ping(slave);
5334         }
5335
5336 out:
5337         return NULL;
5338 }
5339
5340 static struct packet *slave_faulted(pid_t pid, int handle, const struct packet *packet)
5341 {
5342         struct slave_node *slave;
5343         struct inst_info *inst;
5344         const char *pkgname;
5345         const char *id;
5346         const char *func;
5347         int ret;
5348
5349         slave = slave_find_by_pid(pid);
5350         if (!slave) {
5351                 ErrPrint("Slave %d is not exists\n", pid);
5352                 goto out;
5353         }
5354
5355         ret = packet_get(packet, "sss", &pkgname, &id, &func);
5356         if (ret != 3) {
5357                 ErrPrint("Parameter is not matched\n");
5358                 goto out;
5359         }
5360
5361         ret = fault_info_set(slave, pkgname, id, func);
5362         DbgPrint("Slave Faulted: %s (%d)\n", slave_name(slave), ret);
5363
5364         inst = package_find_instance_by_id(pkgname, id);
5365         if (!inst) {
5366                 DbgPrint("There is a no such instance(%s)\n", id);
5367         } else if (instance_state(inst) == INST_DESTROYED) {
5368                 ErrPrint("Instance(%s) is already destroyed\n", id);
5369         } else {
5370                 ret = instance_destroy(inst, INSTANCE_DESTROY_FAULT);
5371         }
5372
5373 out:
5374         return NULL;
5375 }
5376
5377 static struct packet *slave_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
5378 {
5379         struct slave_node *slave;
5380         struct inst_info *inst = NULL;
5381         const struct pkg_info *pkg = NULL;
5382         const char *pkgname;
5383         const char *id;
5384         double priority;
5385         const char *content;
5386         const char *title;
5387         int ret;
5388
5389         slave = slave_find_by_pid(pid);
5390         if (!slave) {
5391                 ErrPrint("Slave %d is not exists\n", pid);
5392                 goto out;
5393         }
5394
5395         ret = packet_get(packet, "ssdss", &pkgname, &id, &priority, &content, &title);
5396         if (ret != 5) {
5397                 ErrPrint("Invalid parameters\n");
5398                 goto out;
5399         }
5400
5401         ret = validate_request(pkgname, id, &inst, &pkg);
5402         if (ret != LB_STATUS_SUCCESS) {
5403                 goto out;
5404         }
5405
5406         if (instance_state(inst) == INST_DESTROYED) {
5407                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5408                 goto out;
5409         }
5410
5411         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
5412                 ret = instance_lb_update_begin(inst, priority, content, title);
5413                 if (ret == LB_STATUS_SUCCESS) {
5414                         slave_freeze_ttl(slave);
5415                 }
5416         } else {
5417                 ErrPrint("Invalid request[%s]\n", id);
5418         }
5419
5420 out:
5421         return NULL;
5422 }
5423
5424 static struct packet *slave_lb_update_end(pid_t pid, int handle, const struct packet *packet)
5425 {
5426         struct slave_node *slave;
5427         struct inst_info *inst = NULL;
5428         const struct pkg_info *pkg = NULL;
5429         const char *pkgname;
5430         const char *id;
5431         int ret;
5432
5433         slave = slave_find_by_pid(pid);
5434         if (!slave) {
5435                 ErrPrint("Slave %d is not exists\n", pid);
5436                 goto out;
5437         }
5438
5439         ret = packet_get(packet, "ss", &pkgname, &id);
5440         if (ret != 2) {
5441                 ErrPrint("Invalid parameters\n");
5442                 goto out;
5443         }
5444
5445         ret = validate_request(pkgname, id, &inst, &pkg);
5446         if (ret != LB_STATUS_SUCCESS) {
5447                 goto out;
5448         }
5449
5450         if (instance_state(inst) == INST_DESTROYED) {
5451                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5452                 goto out;
5453         }
5454
5455         if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
5456                 ret = instance_lb_update_end(inst);
5457                 if (ret == LB_STATUS_SUCCESS) {
5458                         slave_thaw_ttl(slave);
5459                 }
5460         } else {
5461                 ErrPrint("Invalid request[%s]\n", id);
5462         }
5463
5464 out:
5465         return NULL;
5466 }
5467
5468 static struct packet *slave_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
5469 {
5470         struct slave_node *slave;
5471         const struct pkg_info *pkg = NULL;
5472         struct inst_info *inst = NULL;
5473         const char *pkgname;
5474         const char *id;
5475         int ret;
5476
5477         slave = slave_find_by_pid(pid);
5478         if (!slave) {
5479                 ErrPrint("Slave %d is not exists\n", pid);
5480                 goto out;
5481         }
5482
5483         ret = packet_get(packet, "ss", &pkgname, &id);
5484         if (ret != 2) {
5485                 ErrPrint("Invalid parameters\n");
5486                 goto out;
5487         }
5488
5489         ret = validate_request(pkgname, id, &inst, &pkg);
5490         if (ret != LB_STATUS_SUCCESS) {
5491                 goto out;
5492         }
5493
5494         if (instance_state(inst) == INST_DESTROYED) {
5495                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5496                 goto out;
5497         }
5498
5499         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
5500                 (void)instance_pd_update_begin(inst);
5501         } else {
5502                 ErrPrint("Invalid request[%s]\n", id);
5503         }
5504
5505 out:
5506         return NULL;
5507 }
5508
5509 static struct packet *slave_access_status(pid_t pid, int handle, const struct packet *packet)
5510 {
5511         struct slave_node *slave;
5512         struct inst_info *inst = NULL;
5513         const char *pkgname;
5514         const char *id;
5515         int status;
5516         int ret;
5517
5518         slave = slave_find_by_pid(pid);
5519         if (!slave) {
5520                 ErrPrint("Slave %d is not exists\n", pid);
5521                 goto out;
5522         }
5523
5524         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
5525         if (ret != 3) {
5526                 ErrPrint("Invalid parameters\n");
5527                 goto out;
5528         }
5529
5530         ret = validate_request(pkgname, id, &inst, NULL);
5531         if (ret == LB_STATUS_SUCCESS) {
5532                 if (instance_state(inst) == INST_DESTROYED) {
5533                         ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5534                 } else {
5535                         (void)instance_forward_packet(inst, packet_ref((struct packet *)packet));
5536                 }
5537         }
5538
5539 out:
5540         return NULL;
5541 }
5542
5543 static struct packet *slave_pd_update_end(pid_t pid, int handle, const struct packet *packet)
5544 {
5545         struct slave_node *slave;
5546         const struct pkg_info *pkg = NULL;
5547         struct inst_info *inst = NULL;
5548         const char *pkgname;
5549         const char *id;
5550         int ret;
5551
5552         slave = slave_find_by_pid(pid);
5553         if (!slave) {
5554                 ErrPrint("Slave %d is not exists\n", pid);
5555                 goto out;
5556         }
5557
5558         ret = packet_get(packet, "ss", &pkgname, &id);
5559         if (ret != 2) {
5560                 ErrPrint("Invalid parameters\n");
5561                 goto out;
5562         }
5563
5564         ret = validate_request(pkgname, id, &inst, &pkg);
5565         if (ret != LB_STATUS_SUCCESS) {
5566                 goto out;
5567         }
5568
5569         if (instance_state(inst) == INST_DESTROYED) {
5570                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5571                 goto out;
5572         }
5573
5574         if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
5575                 (void)instance_pd_update_end(inst);
5576         } else {
5577                 ErrPrint("Invalid request[%s]\n", id);
5578         }
5579
5580 out:
5581         return NULL;
5582 }
5583
5584 static struct packet *slave_call(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, function, ret */
5585 {
5586         struct slave_node *slave;
5587         const char *pkgname;
5588         const char *id;
5589         const char *func;
5590         int ret;
5591
5592         slave = slave_find_by_pid(pid);
5593         if (!slave) {
5594                 ErrPrint("Slave %d is not exists\n", pid);
5595                 goto out;
5596         }
5597
5598         ret = packet_get(packet, "sss", &pkgname, &id, &func);
5599         if (ret != 3) {
5600                 ErrPrint("Parameter is not matched\n");
5601                 goto out;
5602         }
5603
5604         ret = fault_func_call(slave, pkgname, id, func);
5605         slave_give_more_ttl(slave);
5606
5607 out:
5608         return NULL;
5609 }
5610
5611 static struct packet *slave_ret(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, function, ret */
5612 {
5613         struct slave_node *slave;
5614         const char *pkgname;
5615         const char *id;
5616         const char *func;
5617         int ret;
5618
5619         slave = slave_find_by_pid(pid);
5620         if (!slave) {
5621                 ErrPrint("Slave %d is not exists\n", pid);
5622                 goto out;
5623         }
5624
5625         ret = packet_get(packet, "sss", &pkgname, &id, &func);
5626         if (ret != 3) {
5627                 ErrPrint("Parameter is not matched\n");
5628                 goto out;
5629         }
5630
5631         ret = fault_func_ret(slave, pkgname, id, func);
5632         slave_give_more_ttl(slave);
5633
5634 out:
5635         return NULL;
5636 }
5637
5638 static struct packet *slave_updated(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, width, height, priority, ret */
5639 {
5640         struct slave_node *slave;
5641         const char *pkgname;
5642         const char *safe_filename;
5643         const char *id;
5644         const char *content_info;
5645         const char *title;
5646         int w;
5647         int h;
5648         double priority;
5649         int ret;
5650         struct inst_info *inst = NULL;
5651
5652         slave = slave_find_by_pid(pid);
5653         if (!slave) {
5654                 ErrPrint("Slave %d is not exists\n", pid);
5655                 goto out;
5656         }
5657
5658         ret = packet_get(packet, "ssiidsss", &pkgname, &id,
5659                                                 &w, &h, &priority,
5660                                                 &content_info, &title,
5661                                                 &safe_filename);
5662         if (ret != 8) {
5663                 ErrPrint("Parameter is not matched\n");
5664                 goto out;
5665         }
5666
5667         ret = validate_request(pkgname, id, &inst, NULL);
5668         if (ret == LB_STATUS_SUCCESS) {
5669                 if (instance_state(inst) == INST_DESTROYED) {
5670                         ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5671                         goto out;
5672                 }
5673
5674                 instance_set_lb_info(inst, priority, content_info, title);
5675
5676                 switch (package_lb_type(instance_package(inst))) {
5677                 case LB_TYPE_SCRIPT:
5678                         script_handler_resize(instance_lb_script(inst), w, h);
5679                         if (safe_filename) {
5680                                 (void)script_handler_parse_desc(inst, safe_filename, 0);
5681                         } else {
5682                                 safe_filename = util_uri_to_path(id);
5683                                 (void)script_handler_parse_desc(inst, safe_filename, 0);
5684                         }
5685
5686                         if (unlink(safe_filename) < 0) {
5687                                 ErrPrint("unlink: %s - %s\n", strerror(errno), safe_filename);
5688                         }
5689                         break;
5690                 case LB_TYPE_BUFFER:
5691                 default:
5692                         /*!
5693                          * \check
5694                          * text format (inst)
5695                          */
5696                         instance_set_lb_size(inst, w, h);
5697                         instance_lb_updated_by_instance(inst, safe_filename);
5698                         break;
5699                 }
5700
5701                 slave_give_more_ttl(slave);
5702         }
5703
5704 out:
5705         return NULL;
5706 }
5707
5708 static struct packet *slave_hold_scroll(pid_t pid, int handle, const struct packet *packet)
5709 {
5710         struct slave_node *slave;
5711         struct inst_info *inst = NULL;
5712         const char *pkgname;
5713         const char *id;
5714         int seize;
5715         int ret;
5716
5717         slave = slave_find_by_pid(pid);
5718         if (!slave) {
5719                 ErrPrint("Slave %d is not exists\n", pid);
5720                 goto out;
5721         }
5722
5723         ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
5724         if (ret != 3) {
5725                 ErrPrint("Parameter is not matched\n");
5726                 goto out;
5727         }
5728
5729         ret = validate_request(pkgname, id, &inst, NULL);
5730         if (ret == LB_STATUS_SUCCESS) {
5731                 if (instance_state(inst) == INST_DESTROYED) {
5732                         ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5733                 } else {
5734                         (void)instance_hold_scroll(inst, seize);
5735                 }
5736         }
5737
5738 out:
5739         return NULL;
5740 }
5741
5742 static struct packet *slave_desc_updated(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, decsfile, ret */
5743 {
5744         struct slave_node *slave;
5745         const char *pkgname;
5746         const char *id;
5747         const char *descfile;
5748         int ret;
5749         struct inst_info *inst = NULL;
5750
5751         slave = slave_find_by_pid(pid);
5752         if (!slave) {
5753                 ErrPrint("Slave %d is not exists\n", pid);
5754                 goto out;
5755         }
5756
5757         ret = packet_get(packet, "sss", &pkgname, &id, &descfile);
5758         if (ret != 3) {
5759                 ErrPrint("Parameter is not matched\n");
5760                 goto out;
5761         }
5762
5763         ret = validate_request(pkgname, id, &inst, NULL);
5764         if (ret != LB_STATUS_SUCCESS) {
5765                 goto out;
5766         }
5767
5768         if (instance_state(inst) == INST_DESTROYED) {
5769                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5770                 goto out;
5771         }
5772
5773         switch (package_pd_type(instance_package(inst))) {
5774         case PD_TYPE_SCRIPT:
5775                 if (script_handler_is_loaded(instance_pd_script(inst))) {
5776                         (void)script_handler_parse_desc(inst, descfile, 1);
5777                 }
5778                 break;
5779         case PD_TYPE_TEXT:
5780                 instance_set_pd_size(inst, 0, 0);
5781         case PD_TYPE_BUFFER:
5782                 instance_pd_updated(pkgname, id, descfile);
5783                 break;
5784         default:
5785                 DbgPrint("Ignore updated DESC(%s)\n", pkgname);
5786                 break;
5787         }
5788
5789 out:
5790         return NULL;
5791 }
5792
5793 static struct packet *slave_deleted(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, id, ret */
5794 {
5795         struct slave_node *slave;
5796         const char *pkgname;
5797         const char *id;
5798         int ret;
5799         struct inst_info *inst = NULL;
5800
5801         slave = slave_find_by_pid(pid);
5802         if (!slave) {
5803                 ErrPrint("Slave %d is not exists\n", pid);
5804                 goto out;
5805         }
5806
5807         ret = packet_get(packet, "ss", &pkgname, &id);
5808         if (ret != 2) {
5809                 ErrPrint("Parameter is not matched\n");
5810                 goto out;
5811         }
5812
5813         ret = validate_request(pkgname, id, &inst, NULL);
5814         if (ret == LB_STATUS_SUCCESS) {
5815                 ret = instance_destroyed(inst);
5816         }
5817
5818 out:
5819         return NULL;
5820 }
5821
5822 /*!
5823  * \note for the BUFFER Type slave
5824  */
5825 static struct packet *slave_acquire_buffer(pid_t pid, int handle, const struct packet *packet) /* type, id, w, h, size */
5826 {
5827         enum target_type target;
5828         const char *pkgname;
5829         const char *id;
5830         int w;
5831         int h;
5832         int pixel_size;
5833         struct packet *result;
5834         struct slave_node *slave;
5835         struct inst_info *inst = NULL;
5836         const struct pkg_info *pkg = NULL;
5837         int ret;
5838
5839         slave = slave_find_by_pid(pid);
5840         if (!slave) {
5841                 ErrPrint("Failed to find a slave\n");
5842                 id = "";
5843                 ret = LB_STATUS_ERROR_NOT_EXIST;
5844                 goto out;
5845         }
5846
5847         ret = packet_get(packet, "issiii", &target, &pkgname, &id, &w, &h, &pixel_size);
5848         if (ret != 6) {
5849                 ErrPrint("Invalid argument\n");
5850                 id = "";
5851                 ret = LB_STATUS_ERROR_INVALID;
5852                 goto out;
5853         }
5854
5855         ret = validate_request(pkgname, id, &inst, &pkg);
5856         id = "";
5857
5858         if (ret != LB_STATUS_SUCCESS) {
5859                 goto out;
5860         }
5861
5862         ret = LB_STATUS_ERROR_INVALID;
5863
5864         if (instance_state(inst) == INST_DESTROYED) {
5865                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
5866                 goto out;
5867         }
5868
5869         if (target == TYPE_LB && package_lb_type(pkg) == LB_TYPE_BUFFER) {
5870                 struct buffer_info *info;
5871
5872                 info = instance_lb_buffer(inst);
5873                 if (!info) {
5874                         if (!instance_create_lb_buffer(inst)) {
5875                                 ErrPrint("Failed to create a LB buffer\n");
5876                                 ret = LB_STATUS_ERROR_FAULT;
5877                                 goto out;
5878                         }
5879
5880                         info = instance_lb_buffer(inst);
5881                         if (!info) {
5882                                 ErrPrint("LB buffer is not valid\n");
5883                                 /*!
5884                                  * \NOTE
5885                                  * ret value should not be changed.
5886                                  */
5887                                 goto out;
5888                         }
5889                 }
5890
5891                 ret = buffer_handler_resize(info, w, h);
5892                 ret = buffer_handler_load(info);
5893                 if (ret == 0) {
5894                         instance_set_lb_size(inst, w, h);
5895                         instance_set_lb_info(inst, PRIORITY_NO_CHANGE, CONTENT_NO_CHANGE, TITLE_NO_CHANGE);
5896                         id = buffer_handler_id(info);
5897                 } else {
5898                         ErrPrint("Failed to load a buffer(%d)\n", ret);
5899                 }
5900         } else if (target == TYPE_PD && package_pd_type(pkg) == PD_TYPE_BUFFER) {
5901                 struct buffer_info *info;
5902                 Ecore_Timer *pd_monitor;
5903                 int is_resize;
5904
5905                 is_resize = 0;
5906                 pd_monitor = instance_del_data(inst, "pd,open,monitor");
5907                 if (!pd_monitor) {
5908                         pd_monitor = instance_del_data(inst, "pd,resize,monitor");
5909                         is_resize = !!pd_monitor;
5910                         if (!is_resize) {
5911                                 /* Invalid request. Reject this */
5912                                 ErrPrint("Invalid request\n");
5913                                 goto out;
5914                         }
5915
5916                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst);
5917                 } else {
5918                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_open_buffer_cb, inst);
5919                 }
5920
5921                 ecore_timer_del(pd_monitor);
5922                 inst = instance_unref(inst);
5923                 if (!inst) {
5924                         ErrPrint("Instance refcnt is ZERO: %s\n", pkgname);
5925                         goto out;
5926                 }
5927
5928                 info = instance_pd_buffer(inst);
5929                 if (!info) {
5930                         if (!instance_create_pd_buffer(inst)) {
5931                                 ErrPrint("Failed to create a PD buffer\n");
5932                                 ret = LB_STATUS_ERROR_FAULT;
5933                                 instance_client_pd_created(inst, ret);
5934                                 goto out;
5935                         }
5936
5937                         info = instance_pd_buffer(inst);
5938                         if (!info) {
5939                                 ErrPrint("PD buffer is not valid\n");
5940                                 /*!
5941                                  * \NOTE
5942                                  * ret value should not be changed.
5943                                  */
5944                                 instance_client_pd_created(inst, ret);
5945                                 goto out;
5946                         }
5947                 }
5948
5949                 ret = buffer_handler_resize(info, w, h);
5950                 ret = buffer_handler_load(info);
5951                 if (ret == 0) {
5952                         instance_set_pd_size(inst, w, h);
5953                         id = buffer_handler_id(info);
5954                 } else {
5955                         ErrPrint("Failed to load a buffer (%d)\n", ret);
5956                 }
5957
5958                 /*!
5959                  * Send the PD created event to the client
5960                  */
5961                 if (!is_resize) {
5962                         instance_client_pd_created(inst, ret);
5963                 }
5964         }
5965
5966 out:
5967         result = packet_create_reply(packet, "is", ret, id);
5968         if (!result) {
5969                 ErrPrint("Failed to create a packet\n");
5970         }
5971
5972         return result;
5973 }
5974
5975 static struct packet *slave_resize_buffer(pid_t pid, int handle, const struct packet *packet)
5976 {
5977         struct slave_node *slave;
5978         struct packet *result;
5979         enum target_type type;
5980         const char *pkgname;
5981         const char *id;
5982         int w;
5983         int h;
5984         struct inst_info *inst = NULL;
5985         const struct pkg_info *pkg = NULL;
5986         int ret;
5987
5988         slave = slave_find_by_pid(pid);
5989         if (!slave) {
5990                 ErrPrint("Failed to find a slave\n");
5991                 ret = LB_STATUS_ERROR_NOT_EXIST;
5992                 id = "";
5993                 goto out;
5994         }
5995
5996         ret = packet_get(packet, "issii", &type, &pkgname, &id, &w, &h);
5997         if (ret != 5) {
5998                 ErrPrint("Invalid argument\n");
5999                 ret = LB_STATUS_ERROR_INVALID;
6000                 id = "";
6001                 goto out;
6002         }
6003
6004         ret = validate_request(pkgname, id, &inst, &pkg);
6005         id = "";
6006         if (ret != LB_STATUS_SUCCESS) {
6007                 goto out;
6008         }
6009
6010         ret = LB_STATUS_ERROR_INVALID;
6011         /*!
6012          * \note
6013          * Reset "id", It will be re-used from here
6014          */
6015
6016         if (instance_state(inst) == INST_DESTROYED) {
6017                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
6018                 goto out;
6019         }
6020
6021         if (type == TYPE_LB && package_lb_type(pkg) == LB_TYPE_BUFFER) {
6022                 struct buffer_info *info;
6023
6024                 info = instance_lb_buffer(inst);
6025                 if (!info) {
6026                         goto out;
6027                 }
6028
6029                 ret = buffer_handler_resize(info, w, h);
6030                 /*!
6031                  * \note
6032                  * id is resued for newly assigned ID
6033                  */
6034                 if (ret == LB_STATUS_SUCCESS) {
6035                         id = buffer_handler_id(info);
6036                         instance_set_lb_size(inst, w, h);
6037                         instance_set_lb_info(inst, PRIORITY_NO_CHANGE, CONTENT_NO_CHANGE, TITLE_NO_CHANGE);
6038                 }
6039         } else if (type == TYPE_PD && package_pd_type(pkg) == PD_TYPE_BUFFER) {
6040                 struct buffer_info *info;
6041
6042                 info = instance_pd_buffer(inst);
6043                 if (!info) {
6044                         goto out;
6045                 }
6046
6047                 ret = buffer_handler_resize(info, w, h);
6048                 /*!
6049                  * \note
6050                  * id is resued for newly assigned ID
6051                  */
6052                 if (ret == LB_STATUS_SUCCESS) {
6053                         id = buffer_handler_id(info);
6054                         instance_set_pd_size(inst, w, h);
6055                 }
6056         }
6057
6058 out:
6059         result = packet_create_reply(packet, "is", ret, id);
6060         if (!result) {
6061                 ErrPrint("Failed to create a packet\n");
6062         }
6063
6064         return result;
6065 }
6066
6067 static struct packet *slave_release_buffer(pid_t pid, int handle, const struct packet *packet)
6068 {
6069         enum target_type type;
6070         const char *pkgname;
6071         const char *id;
6072         struct packet *result;
6073         struct slave_node *slave;
6074         struct inst_info *inst = NULL;
6075         const struct pkg_info *pkg = NULL;
6076         int ret;
6077
6078         slave = slave_find_by_pid(pid);
6079         if (!slave) {
6080                 ErrPrint("Failed to find a slave\n");
6081                 ret = LB_STATUS_ERROR_NOT_EXIST;
6082                 goto out;
6083         }
6084
6085         if (packet_get(packet, "iss", &type, &pkgname, &id) != 3) {
6086                 ErrPrint("Inavlid argument\n");
6087                 ret = LB_STATUS_ERROR_INVALID;
6088                 goto out;
6089         }
6090
6091         ret = validate_request(pkgname, id, &inst, &pkg);
6092         if (ret != LB_STATUS_SUCCESS) {
6093                 goto out;
6094         }
6095
6096         ret = LB_STATUS_ERROR_INVALID;
6097
6098         if (type == TYPE_LB && package_lb_type(pkg) == LB_TYPE_BUFFER) {
6099                 struct buffer_info *info;
6100
6101                 info = instance_lb_buffer(inst);
6102                 ret = buffer_handler_unload(info);
6103         } else if (type == TYPE_PD && package_pd_type(pkg) == PD_TYPE_BUFFER) {
6104                 struct buffer_info *info;
6105                 Ecore_Timer *pd_monitor;
6106
6107                 pd_monitor = instance_del_data(inst, "pd,close,monitor");
6108                 if (!pd_monitor) {
6109                         ErrPrint("Slave requests to release a buffer\n");
6110                         /*!
6111                          * \note
6112                          * In this case just keep going to release buffer,
6113                          * Even if a user(client) doesn't wants to destroy the PD.
6114                          *
6115                          * If the slave tries to destroy PD buffer, it should be
6116                          * released and reported to the client about its status.
6117                          *
6118                          * Even if the pd is destroyed by timeout handler,
6119                          * instance_client_pd_destroyed function will be ignored
6120                          * by pd.need_to_send_close_event flag.
6121                          * which will be checked by instance_client_pd_destroyed function.
6122                          */
6123
6124                         /*!
6125                          * \note
6126                          * provider can try to resize the buffer size.
6127                          * in that case, it will release the buffer first.
6128                          * Then even though the client doesn't request to close the PD,
6129                          * the provider can release it.
6130                          * If we send the close event to the client,
6131                          * The client will not able to allocate PD again.
6132                          * In this case, add the pd,monitor again. from here.
6133                          * to wait the re-allocate buffer.
6134                          * If the client doesn't request buffer reallocation,
6135                          * Treat it as a fault. and close the PD.
6136                          */
6137                         info = instance_pd_buffer(inst);
6138                         ret = buffer_handler_unload(info);
6139
6140                         if (ret == LB_STATUS_SUCCESS) {
6141                                 pd_monitor = ecore_timer_add(PD_REQUEST_TIMEOUT, pd_resize_monitor_cb, instance_ref(inst));
6142                                 if (!pd_monitor) {
6143                                         (void)instance_unref(inst);
6144                                         ErrPrint("Failed to create a timer for PD Open monitor\n");
6145                                 } else {
6146                                         (void)instance_set_data(inst, "pd,resize,monitor", pd_monitor);
6147                                         if (slave_event_callback_add(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_resize_buffer_cb, inst) != LB_STATUS_SUCCESS) {
6148                                                 ErrPrint("Failed to add event handler: %s\n", pkgname);
6149                                         }
6150                                 }
6151                         }
6152                 } else {
6153                         slave_event_callback_del(slave, SLAVE_EVENT_DEACTIVATE, slave_fault_close_buffer_cb, inst);
6154
6155                         ecore_timer_del(pd_monitor);
6156                         inst = instance_unref(inst);
6157                         if (!inst) {
6158                                 ErrPrint("Instance is released: %s\n", pkgname);
6159                                 ret = LB_STATUS_ERROR_FAULT;
6160                                 goto out;
6161                         }
6162
6163                         info = instance_pd_buffer(inst);
6164                         ret = buffer_handler_unload(info);
6165
6166                         /*!
6167                          * \note
6168                          * Send the PD destroyed event to the client
6169                          */
6170                         instance_client_pd_destroyed(inst, ret);
6171                 }
6172         }
6173
6174 out:
6175         result = packet_create_reply(packet, "i", ret);
6176         if (!result) {
6177                 ErrPrint("Failed to create a packet\n");
6178         }
6179
6180         return result;
6181 }
6182
6183 static struct packet *service_change_period(pid_t pid, int handle, const struct packet *packet)
6184 {
6185         struct inst_info *inst = NULL;
6186         struct packet *result;
6187         const char *pkgname;
6188         const char *id;
6189         double period;
6190         int ret;
6191
6192         ret = packet_get(packet, "ssd", &pkgname, &id, &period);
6193         if (ret != 3) {
6194                 ErrPrint("Invalid packet\n");
6195                 ret = LB_STATUS_ERROR_INVALID;
6196                 goto out;
6197         }
6198
6199         if (!strlen(id)) {
6200                 struct pkg_info *pkg;
6201
6202                 pkg = package_find(pkgname);
6203                 if (!pkg) {
6204                         ret = LB_STATUS_ERROR_NOT_EXIST;
6205                 } else if (package_is_fault(pkg)) {
6206                         ret = LB_STATUS_ERROR_FAULT;
6207                 } else {
6208                         Eina_List *inst_list;
6209                         Eina_List *l;
6210
6211                         inst_list = package_instance_list(pkg);
6212                         EINA_LIST_FOREACH(inst_list, l, inst) {
6213                                 ret = instance_set_period(inst, period);
6214                                 if (ret < 0) {
6215                                         ErrPrint("Failed to change the period of %s to (%lf)\n", pkgname, period);
6216                                 }
6217                         }
6218                 }
6219         } else {
6220                 ret = validate_request(pkgname, id, &inst, NULL);
6221                 if (ret == LB_STATUS_SUCCESS) {
6222                         if (instance_state(inst) == INST_DESTROYED) {
6223                                 ErrPrint("Package[%s] instance is already destroyed\n", pkgname);
6224                                 ret = LB_STATUS_ERROR_INVALID;
6225                                 goto out;
6226                         }
6227
6228                         ret = instance_set_period(inst, period);
6229                 }
6230         }
6231
6232         DbgPrint("Change the update period: %s, %lf : %d\n", pkgname, period, ret);
6233 out:
6234         result = packet_create_reply(packet, "i", ret);
6235         if (!result) {
6236                 ErrPrint("Failed to create a packet\n");
6237         }
6238
6239         return result;
6240 }
6241
6242 static struct packet *service_update(pid_t pid, int handle, const struct packet *packet)
6243 {
6244         Eina_List *inst_list;
6245         struct pkg_info *pkg;
6246         struct packet *result;
6247         const char *pkgname;
6248         const char *id;
6249         const char *cluster;
6250         const char *category;
6251         char *lbid;
6252         int ret;
6253
6254         ret = packet_get(packet, "ssss", &pkgname, &id, &cluster, &category);
6255         if (ret != 4) {
6256                 ErrPrint("Invalid Packet\n");
6257                 ret = LB_STATUS_ERROR_INVALID;
6258                 goto out;
6259         }
6260
6261         lbid = package_lb_pkgname(pkgname);
6262         if (!lbid) {
6263                 ErrPrint("Invalid package %s\n", pkgname);
6264                 ret = LB_STATUS_ERROR_INVALID;
6265                 goto out;
6266         }
6267
6268         pkg = package_find(lbid);
6269         if (!pkg) {
6270                 ret = LB_STATUS_ERROR_NOT_EXIST;
6271                 DbgFree(lbid);
6272                 goto out;
6273         }
6274
6275         if (package_is_fault(pkg)) {
6276                 ret = LB_STATUS_ERROR_FAULT;
6277                 DbgFree(lbid);
6278                 goto out;
6279         }
6280
6281         inst_list = package_instance_list(pkg);
6282         if (!eina_list_count(inst_list)) {
6283                 ret = LB_STATUS_ERROR_NOT_EXIST;
6284                 DbgFree(lbid);
6285                 goto out;
6286         }
6287
6288         if (id && strlen(id)) {
6289                 Eina_List *l;
6290                 struct inst_info *inst;
6291
6292                 ret = LB_STATUS_ERROR_NOT_EXIST;
6293                 EINA_LIST_FOREACH(inst_list, l, inst) {
6294                         if (!strcmp(instance_id(inst), id)) {
6295                                 ret = LB_STATUS_SUCCESS;
6296                                 break;
6297                         }
6298                 }
6299
6300                 if (ret == LB_STATUS_ERROR_NOT_EXIST) {
6301                         DbgFree(lbid);
6302                         goto out;
6303                 }
6304         }
6305
6306         /*!
6307          * \TODO
6308          * Validate the update requstor.
6309          */
6310         slave_rpc_request_update(lbid, id, cluster, category);
6311         DbgFree(lbid);
6312         ret = LB_STATUS_SUCCESS;
6313
6314 out:
6315         result = packet_create_reply(packet, "i", ret);
6316         if (!result) {
6317                 ErrPrint("Failed to create a packet\n");
6318         }
6319
6320         return result;
6321 }
6322
6323 static struct packet *liveinfo_hello(pid_t pid, int handle, const struct packet *packet)
6324 {
6325         struct liveinfo *info;
6326         struct packet *result;
6327         int ret;
6328         const char *fifo_name;
6329         double timestamp;
6330
6331         DbgPrint("Request arrived from %d\n", pid);
6332
6333         if (packet_get(packet, "d", &timestamp) != 1) {
6334                 ErrPrint("Invalid packet\n");
6335                 fifo_name = "";
6336                 ret = LB_STATUS_ERROR_INVALID;
6337                 goto out;
6338         }
6339
6340         info = liveinfo_create(pid, handle);
6341         if (!info) {
6342                 ErrPrint("Failed to create a liveinfo object\n");
6343                 fifo_name = "";
6344                 ret = LB_STATUS_ERROR_INVALID;
6345                 goto out;
6346         }
6347
6348         ret = 0;
6349         fifo_name = liveinfo_filename(info);
6350         DbgPrint("FIFO Created: %s (Serve for %d)\n", fifo_name, pid);
6351
6352 out:
6353         result = packet_create_reply(packet, "si", fifo_name, ret);
6354         if (!result) {
6355                 ErrPrint("Failed to create a result packet\n");
6356         }
6357
6358         return result;
6359 }
6360
6361 static struct packet *liveinfo_slave_list(pid_t pid, int handle, const struct packet *packet)
6362 {
6363         Eina_List *l;
6364         Eina_List *list;
6365         struct liveinfo *info;
6366         struct slave_node *slave;
6367         FILE *fp;
6368         double timestamp;
6369
6370         if (packet_get(packet, "d", &timestamp) != 1) {
6371                 ErrPrint("Invalid argument\n");
6372                 goto out;
6373         }
6374
6375         info = liveinfo_find_by_pid(pid);
6376         if (!info) {
6377                 ErrPrint("Invalid request\n");
6378                 goto out;
6379         }
6380
6381         liveinfo_open_fifo(info);
6382         fp = liveinfo_fifo(info);
6383         if (!fp) {
6384                 liveinfo_close_fifo(info);
6385                 goto out;
6386         }
6387
6388         list = (Eina_List *)slave_list();
6389         EINA_LIST_FOREACH(list, l, slave) {
6390                 fprintf(fp, "%d %s %s %s %d %d %d %s %d %d %lf\n", 
6391                         slave_pid(slave),
6392                         slave_name(slave),
6393                         slave_pkgname(slave),
6394                         slave_abi(slave),
6395                         slave_is_secured(slave),
6396                         slave_refcnt(slave),
6397                         slave_fault_count(slave),
6398                         slave_state_string(slave),
6399                         slave_loaded_instance(slave),
6400                         slave_loaded_package(slave),
6401                         slave_ttl(slave)
6402                 );
6403         }
6404
6405         fprintf(fp, "EOD\n");
6406         liveinfo_close_fifo(info);
6407 out:
6408         return NULL;
6409 }
6410
6411 static inline const char *visible_state_string(enum livebox_visible_state state)
6412 {
6413         switch (state) {
6414         case LB_SHOW:
6415                 return "Show";
6416         case LB_HIDE:
6417                 return "Hide";
6418         case LB_HIDE_WITH_PAUSE:
6419                 return "Paused";
6420         default:
6421                 break;
6422         }
6423
6424         return "Unknown";
6425 }
6426
6427 static struct packet *liveinfo_inst_list(pid_t pid, int handle, const struct packet *packet)
6428 {
6429         const char *pkgname;
6430         struct liveinfo *info;
6431         struct pkg_info *pkg;
6432         Eina_List *l;
6433         Eina_List *inst_list;
6434         struct inst_info *inst;
6435         FILE *fp;
6436
6437         if (packet_get(packet, "s", &pkgname) != 1) {
6438                 ErrPrint("Invalid argument\n");
6439                 goto out;
6440         }
6441
6442         info = liveinfo_find_by_pid(pid);
6443         if (!info) {
6444                 ErrPrint("Invalid request\n");
6445                 goto out;
6446         }
6447
6448         liveinfo_open_fifo(info);
6449         fp = liveinfo_fifo(info);
6450         if (!fp) {
6451                 ErrPrint("Invalid fp\n");
6452                 liveinfo_close_fifo(info);
6453                 goto out;
6454         }
6455
6456         if (!package_is_lb_pkgname(pkgname)) {
6457                 ErrPrint("Invalid package name\n");
6458                 goto close_out;
6459         }
6460
6461         pkg = package_find(pkgname);
6462         if (!pkg) {
6463                 ErrPrint("Package is not exists\n");
6464                 goto close_out;
6465         }
6466
6467         inst_list = package_instance_list(pkg);
6468         EINA_LIST_FOREACH(inst_list, l, inst) {
6469                 fprintf(fp, "%s %s %s %lf %s %d %d\n",
6470                         instance_id(inst),
6471                         instance_cluster(inst),
6472                         instance_category(inst),
6473                         instance_period(inst),
6474                         visible_state_string(instance_visible_state(inst)),
6475                         instance_lb_width(inst),
6476                         instance_lb_height(inst));
6477         }
6478
6479 close_out:
6480         fprintf(fp, "EOD\n");
6481         liveinfo_close_fifo(info);
6482
6483 out:
6484         return NULL;
6485 }
6486
6487 static struct packet *liveinfo_pkg_list(pid_t pid, int handle, const struct packet *packet)
6488 {
6489         Eina_List *l;
6490         Eina_List *list;
6491         Eina_List *inst_list;
6492         struct liveinfo *info;
6493         struct pkg_info *pkg;
6494         struct slave_node *slave;
6495         FILE *fp;
6496         const char *slavename;
6497         double timestamp;
6498
6499         if (packet_get(packet, "d", &timestamp) != 1) {
6500                 ErrPrint("Invalid argument\n");
6501                 goto out;
6502         }
6503
6504         info = liveinfo_find_by_pid(pid);
6505         if (!info) {
6506                 ErrPrint("Invalid request\n");
6507                 goto out;
6508         }
6509
6510         liveinfo_open_fifo(info);
6511         fp = liveinfo_fifo(info);
6512         if (!fp) {
6513                 liveinfo_close_fifo(info);
6514                 goto out;
6515         }
6516
6517         list = (Eina_List *)package_list();
6518         EINA_LIST_FOREACH(list, l, pkg) {
6519                 slave = package_slave(pkg);
6520
6521                 if (slave) {
6522                         slavename = slave_name(slave);
6523                         pid = slave_pid(slave);
6524                 } else {
6525                         pid = (pid_t)-1;
6526                         slavename = "";
6527                 }
6528
6529                 inst_list = (Eina_List *)package_instance_list(pkg);
6530                 fprintf(fp, "%d %s %s %s %d %d %d\n",
6531                         pid,
6532                         strlen(slavename) ? slavename : "(none)",
6533                         package_name(pkg),
6534                         package_abi(pkg),
6535                         package_refcnt(pkg),
6536                         package_fault_count(pkg),
6537                         eina_list_count(inst_list)
6538                 );
6539         }
6540
6541         fprintf(fp, "EOD\n");
6542         liveinfo_close_fifo(info);
6543 out:
6544         return NULL;
6545 }
6546
6547 static struct packet *liveinfo_slave_ctrl(pid_t pid, int handle, const struct packet *packet)
6548 {
6549         return NULL;
6550 }
6551
6552 static struct packet *liveinfo_pkg_ctrl(pid_t pid, int handle, const struct packet *packet)
6553 {
6554         struct liveinfo *info;
6555         FILE *fp;
6556         char *cmd;
6557         char *pkgname;
6558         char *id;
6559
6560         if (packet_get(packet, "sss", &cmd, &pkgname, &id) != 3) {
6561                 ErrPrint("Invalid argument\n");
6562                 goto out;
6563         }
6564
6565         info = liveinfo_find_by_pid(pid);
6566         if (!info) {
6567                 ErrPrint("Invalid request\n");
6568                 goto out;
6569         }
6570
6571         liveinfo_open_fifo(info);
6572         fp = liveinfo_fifo(info);
6573         if (!fp) {
6574                 liveinfo_close_fifo(info);
6575                 goto out;
6576         }
6577
6578         if (!strcmp(cmd, "rmpack")) {
6579                 fprintf(fp, "%d\n", ENOSYS);
6580         } else if (!strcmp(cmd, "rminst")) {
6581                 struct inst_info *inst;
6582                 inst = package_find_instance_by_id(pkgname, id);
6583                 if (!inst) {
6584                         fprintf(fp, "%d\n", ENOENT);
6585                 } else {
6586                         (void)instance_destroy(inst, INSTANCE_DESTROY_DEFAULT);
6587                         fprintf(fp, "%d\n", 0);
6588                 }
6589         } else if (!strcmp(cmd, "faultinst")) {
6590                 struct inst_info *inst;
6591                 inst = package_find_instance_by_id(pkgname, id);
6592                 if (!inst) {
6593                         fprintf(fp, "%d\n", ENOENT);
6594                 } else {
6595                         struct pkg_info *pkg;
6596
6597                         pkg = instance_package(inst);
6598                         if (!pkg) {
6599                                 fprintf(fp, "%d\n", EFAULT);
6600                         } else {
6601                                 (void)package_faulted(pkg);
6602                                 fprintf(fp, "%d\n", 0);
6603                         }
6604                 }
6605         }
6606
6607         fprintf(fp, "EOD\n");
6608         liveinfo_close_fifo(info);
6609
6610 out:
6611         return NULL;
6612 }
6613
6614 static struct packet *liveinfo_master_ctrl(pid_t pid, int handle, const struct packet *packet)
6615 {
6616         struct liveinfo *info;
6617         char *cmd;
6618         char *var;
6619         char *val;
6620         FILE *fp;
6621         int ret = LB_STATUS_ERROR_INVALID;
6622
6623         if (packet_get(packet, "sss", &cmd, &var, &val) != 3) {
6624                 ErrPrint("Invalid argument\n");
6625                 goto out;
6626         }
6627
6628         info = liveinfo_find_by_pid(pid);
6629         if (!info) {
6630                 ErrPrint("Invalid request\n");
6631                 goto out;
6632         }
6633
6634         if (!strcasecmp(var, "debug")) {
6635                 if (!strcasecmp(cmd, "set")) {
6636                         g_conf.debug_mode = !strcasecmp(val, "on");
6637                 } else if (!strcasecmp(cmd, "get")) {
6638                 }
6639                 ret = g_conf.debug_mode;
6640         } else if (!strcasecmp(var, "slave_max_load")) {
6641                 if (!strcasecmp(cmd, "set")) {
6642                         g_conf.slave_max_load = atoi(val);
6643                 } else if (!strcasecmp(cmd, "get")) {
6644                 }
6645                 ret = g_conf.slave_max_load;
6646         }
6647
6648         liveinfo_open_fifo(info);
6649         fp = liveinfo_fifo(info);
6650         if (!fp) {
6651                 liveinfo_close_fifo(info);
6652                 goto out;
6653         }
6654         fprintf(fp, "%d\nEOD\n", ret);
6655         liveinfo_close_fifo(info);
6656
6657 out:
6658         return NULL;
6659 }
6660
6661 static struct method s_info_table[] = {
6662         {
6663                 .cmd = "liveinfo_hello",
6664                 .handler = liveinfo_hello,
6665         },
6666         {
6667                 .cmd = "slave_list",
6668                 .handler = liveinfo_slave_list,
6669         },
6670         {
6671                 .cmd = "pkg_list",
6672                 .handler = liveinfo_pkg_list,
6673         },
6674         {
6675                 .cmd = "inst_list",
6676                 .handler = liveinfo_inst_list,
6677         },
6678         {
6679                 .cmd = "slave_ctrl",
6680                 .handler = liveinfo_slave_ctrl,
6681         },
6682         {
6683                 .cmd = "pkg_ctrl",
6684                 .handler = liveinfo_pkg_ctrl,
6685         },
6686         {
6687                 .cmd = "master_ctrl",
6688                 .handler = liveinfo_master_ctrl,
6689         },
6690         {
6691                 .cmd = NULL,
6692                 .handler = NULL,
6693         },
6694 };
6695
6696 static struct method s_client_table[] = {
6697         {
6698                 .cmd = "pd_mouse_move",
6699                 .handler = client_pd_mouse_move, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6700         },
6701         {
6702                 .cmd = "lb_mouse_move",
6703                 .handler = client_lb_mouse_move, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6704         },
6705         {
6706                 .cmd = "pd_mouse_down",
6707                 .handler = client_pd_mouse_down, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
6708         },
6709         {
6710                 .cmd = "pd_mouse_up",
6711                 .handler = client_pd_mouse_up, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6712         },
6713         {
6714                 .cmd = "lb_mouse_down",
6715                 .handler = client_lb_mouse_down, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6716         },
6717         {
6718                 .cmd = "lb_mouse_up",
6719                 .handler = client_lb_mouse_up, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6720         },
6721         {
6722                 .cmd = "pd_mouse_enter",
6723                 .handler = client_pd_mouse_enter, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
6724         },
6725         {
6726                 .cmd = "pd_mouse_leave",
6727                 .handler = client_pd_mouse_leave, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
6728         },
6729         {
6730                 .cmd = "lb_mouse_enter",
6731                 .handler = client_lb_mouse_enter, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6732         },
6733         {
6734                 .cmd = "lb_mouse_leave",
6735                 .handler = client_lb_mouse_leave, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
6736         },
6737         {
6738                 .cmd = "lb_mouse_set",
6739                 .handler = client_lb_mouse_set,
6740         },
6741         {
6742                 .cmd = "lb_mouse_unset",
6743                 .handler = client_lb_mouse_unset,
6744         },
6745         {
6746                 .cmd = "pd_mouse_set",
6747                 .handler = client_pd_mouse_set,
6748         },
6749         {
6750                 .cmd = "pd_mouse_unset",
6751                 .handler = client_pd_mouse_unset,
6752         },
6753         {
6754                 .cmd = "change,visibility",
6755                 .handler = client_change_visibility,
6756         },
6757         {
6758                 .cmd = "lb_acquire_pixmap",
6759                 .handler = client_lb_acquire_pixmap,
6760         },
6761         {
6762                 .cmd = "lb_release_pixmap",
6763                 .handler = client_lb_release_pixmap,
6764         },
6765         {
6766                 .cmd = "pd_acquire_pixmap",
6767                 .handler = client_pd_acquire_pixmap,
6768         },
6769         {
6770                 .cmd = "pd_release_pixmap",
6771                 .handler = client_pd_release_pixmap,
6772         },
6773         {
6774                 .cmd = "acquire",
6775                 .handler = client_acquire, /*!< pid, ret */
6776         },
6777         {
6778                 .cmd = "release",
6779                 .handler = cilent_release, /*!< pid, ret */
6780         },
6781         {
6782                 .cmd = "clicked",
6783                 .handler = client_clicked, /*!< pid, pkgname, filename, event, timestamp, x, y, ret */
6784         },
6785         {
6786                 .cmd = "text_signal",
6787                 .handler = client_text_signal, /* pid, pkgname, filename, emission, source, s, sy, ex, ey, ret */
6788         },
6789         {
6790                 .cmd = "delete",
6791                 .handler = client_delete, /* pid, pkgname, filename, ret */
6792         },
6793         {
6794                 .cmd = "resize",
6795                 .handler = client_resize, /* pid, pkgname, filename, w, h, ret */
6796         },
6797         {
6798                 .cmd = "new",
6799                 .handler = client_new, /* pid, timestamp, pkgname, content, cluster, category, period, ret */
6800         },
6801         {
6802                 .cmd = "set_period",
6803                 .handler = client_set_period, /* pid, pkgname, filename, period, ret, period */
6804         },
6805         {
6806                 .cmd = "change_group",
6807                 .handler = client_change_group, /* pid, pkgname, filename, cluster, category, ret */
6808         },
6809         {
6810                 .cmd = "pinup_changed",
6811                 .handler = client_pinup_changed, /* pid, pkgname, filename, pinup, ret */
6812         },
6813         {
6814                 .cmd = "create_pd",
6815                 .handler = client_create_pd, /* pid, pkgname, filename, ret */
6816         },
6817         {
6818                 .cmd = "pd_move",
6819                 .handler = client_pd_move, /* pkgname, id, x, y */
6820         },
6821         {
6822                 .cmd = "destroy_pd",
6823                 .handler = client_destroy_pd, /* pid, pkgname, filename, ret */
6824         },
6825         {
6826                 .cmd = "activate_package",
6827                 .handler = client_activate_package, /* pid, pkgname, ret */
6828         },
6829         {
6830                 .cmd = "subscribe", /* pid, cluster, sub-cluster */
6831                 .handler = client_subscribed,
6832         },
6833         {
6834                 .cmd = "unsubscribe", /* pid, cluster, sub-cluster */
6835                 .handler = client_unsubscribed,
6836         },
6837         {
6838                 .cmd = "delete_cluster",
6839                 .handler = client_delete_cluster,
6840         },
6841         {
6842                 .cmd = "delete_category",
6843                 .handler = client_delete_category,
6844         },
6845         {
6846                 .cmd = "refresh_group",
6847                 .handler = client_refresh_group,
6848         },
6849         {
6850                 .cmd = "update",
6851                 .handler = client_update,
6852         },
6853
6854         {
6855                 .cmd = "pd_access_hl",
6856                 .handler = client_pd_access_hl,
6857         },
6858         {
6859                 .cmd = "pd_access_hl_prev",
6860                 .handler = client_pd_access_hl_prev,
6861         },
6862         {
6863                 .cmd = "pd_access_hl_next",
6864                 .handler = client_pd_access_hl_next,
6865         },
6866         {
6867                 .cmd = "pd_access_activate",
6868                 .handler = client_pd_access_activate,
6869         },
6870         {
6871                 .cmd = "pd_access_action_up",
6872                 .handler = client_pd_access_action_up,
6873         },
6874         {
6875                 .cmd = "pd_access_action_down",
6876                 .handler = client_pd_access_action_down,
6877         },
6878         {
6879                 .cmd = "pd_access_unhighlight",
6880                 .handler = client_pd_access_unhighlight,
6881         },
6882         {
6883                 .cmd = "pd_access_scroll_down",
6884                 .handler = client_pd_access_scroll_down,
6885         },
6886         {
6887                 .cmd = "pd_access_scroll_move",
6888                 .handler = client_pd_access_scroll_move,
6889         },
6890         {
6891                 .cmd = "pd_access_scroll_up",
6892                 .handler = client_pd_access_scroll_up,
6893         },
6894
6895         {
6896                 .cmd = "lb_access_hl",
6897                 .handler = client_lb_access_hl,
6898         },
6899         {
6900                 .cmd = "lb_access_hl_prev",
6901                 .handler = client_lb_access_hl_prev,
6902         },
6903         {
6904                 .cmd = "lb_access_hl_next",
6905                 .handler = client_lb_access_hl_next,
6906         },
6907         {
6908                 .cmd = "lb_access_activate",
6909                 .handler = client_lb_access_activate,
6910         },
6911         {
6912                 .cmd = "lb_access_action_up",
6913                 .handler = client_lb_access_action_up,
6914         },
6915         {
6916                 .cmd = "lb_access_action_down",
6917                 .handler = client_lb_access_action_down,
6918         },
6919         {
6920                 .cmd = "lb_access_unhighlight",
6921                 .handler = client_lb_access_unhighlight,
6922         },
6923         {
6924                 .cmd = "lb_access_scroll_down",
6925                 .handler = client_lb_access_scroll_down,
6926         },
6927         {
6928                 .cmd = "lb_access_scroll_move",
6929                 .handler = client_lb_access_scroll_move,
6930         },
6931         {
6932                 .cmd = "lb_access_scroll_up",
6933                 .handler = client_lb_access_scroll_up,
6934         },
6935
6936         {
6937                 .cmd = "lb_key_down",
6938                 .handler = client_lb_key_down,
6939         },
6940         {
6941                 .cmd = "lb_key_up",
6942                 .handler = client_lb_key_up,
6943         },
6944
6945         {
6946                 .cmd = "pd_key_down",
6947                 .handler = client_pd_key_down,
6948         },
6949         {
6950                 .cmd = "pd_key_up",
6951                 .handler = client_pd_key_up,
6952         },
6953
6954         {
6955                 .cmd = "client_paused",
6956                 .handler = client_pause_request,
6957         },
6958         {
6959                 .cmd = "client_resumed",
6960                 .handler = client_resume_request,
6961         },
6962
6963         {
6964                 .cmd = "update_mode",
6965                 .handler = client_update_mode,
6966         },
6967
6968         {
6969                 .cmd = NULL,
6970                 .handler = NULL,
6971         },
6972 };
6973
6974 static struct method s_service_table[] = {
6975         {
6976                 .cmd = "service_update",
6977                 .handler = service_update,
6978         },
6979         {
6980                 .cmd = "service_change_period",
6981                 .handler = service_change_period,
6982         },
6983         {
6984                 .cmd = NULL,
6985                 .handler = NULL,
6986         },
6987 };
6988
6989 static struct method s_slave_table[] = {
6990         {
6991                 .cmd = "call",
6992                 .handler = slave_call, /* slave_name, pkgname, filename, function, ret */
6993         },
6994         {
6995                 .cmd = "ret",
6996                 .handler = slave_ret, /* slave_name, pkgname, filename, function, ret */
6997         },
6998         {
6999                 .cmd = "updated",
7000                 .handler = slave_updated, /* slave_name, pkgname, filename, width, height, priority, ret */
7001         },
7002         {
7003                 .cmd = "desc_updated",
7004                 .handler = slave_desc_updated, /* slave_name, pkgname, filename, decsfile, ret */
7005         },
7006         {
7007                 .cmd = "deleted",
7008                 .handler = slave_deleted, /* slave_name, pkgname, filename, ret */
7009         },
7010         {
7011                 .cmd = "acquire_buffer",
7012                 .handler = slave_acquire_buffer, /* slave_name, id, w, h, size, - out - type, shmid */
7013         },
7014         {
7015                 .cmd = "resize_buffer",
7016                 .handler = slave_resize_buffer,
7017         },
7018         {
7019                 .cmd = "release_buffer",
7020                 .handler = slave_release_buffer, /* slave_name, id - ret */
7021         },
7022         {
7023                 .cmd = "faulted",
7024                 .handler = slave_faulted, /* slave_name, pkgname, id, funcname */
7025         },
7026         {
7027                 .cmd = "scroll",
7028                 .handler = slave_hold_scroll, /* slave_name, pkgname, id, seize */
7029         },
7030
7031         {
7032                 .cmd = "lb_update_begin",
7033                 .handler = slave_lb_update_begin,
7034         },
7035         {
7036                 .cmd = "lb_update_end",
7037                 .handler = slave_lb_update_end,
7038         },
7039         {
7040                 .cmd = "pd_update_begin",
7041                 .handler = slave_pd_update_begin,
7042         },
7043         {
7044                 .cmd = "pd_update_end",
7045                 .handler = slave_pd_update_end,
7046         },
7047
7048         {
7049                 .cmd = "access_status",
7050                 .handler = slave_access_status,
7051         },
7052
7053         {
7054                 .cmd = "hello",
7055                 .handler = slave_hello, /* slave_name, ret */
7056         },
7057         {
7058                 .cmd = "ping",
7059                 .handler = slave_ping, /* slave_name, ret */
7060         },
7061
7062         {
7063                 .cmd = NULL,
7064                 .handler = NULL,
7065         },
7066 };
7067
7068 HAPI int server_init(void)
7069 {
7070         com_core_packet_use_thread(COM_CORE_THREAD);
7071
7072         if (unlink(INFO_SOCKET) < 0) {
7073                 ErrPrint("info socket: %s\n", strerror(errno));
7074         }
7075
7076         if (unlink(SLAVE_SOCKET) < 0) {
7077                 ErrPrint("slave socket: %s\n", strerror(errno));
7078         }
7079
7080         if (unlink(CLIENT_SOCKET) < 0) {
7081                 ErrPrint("client socket: %s\n", strerror(errno));
7082         }
7083
7084         if (unlink(SERVICE_SOCKET) < 0) {
7085                 ErrPrint("service socket: %s\n", strerror(errno));
7086         }
7087
7088         s_info.info_fd = com_core_packet_server_init(INFO_SOCKET, s_info_table);
7089         if (s_info.info_fd < 0) {
7090                 ErrPrint("Failed to create a info socket\n");
7091         }
7092
7093         s_info.slave_fd = com_core_packet_server_init(SLAVE_SOCKET, s_slave_table);
7094         if (s_info.slave_fd < 0) {
7095                 ErrPrint("Failed to create a slave socket\n");
7096         }
7097
7098         s_info.client_fd = com_core_packet_server_init(CLIENT_SOCKET, s_client_table);
7099         if (s_info.client_fd < 0) {
7100                 ErrPrint("Failed to create a client socket\n");
7101         }
7102
7103         /*!
7104          * \note
7105          * remote://:8208
7106          * Skip address to use the NULL.
7107          */
7108         s_info.remote_client_fd = com_core_packet_server_init("remote://:"CLIENT_PORT, s_client_table);
7109         if (s_info.client_fd < 0) {
7110                 ErrPrint("Failed to create a remote client socket\n");
7111         }
7112
7113         s_info.service_fd = com_core_packet_server_init(SERVICE_SOCKET, s_service_table);
7114         if (s_info.service_fd < 0) {
7115                 ErrPrint("Faild to create a service socket\n");
7116         }
7117
7118         if (chmod(INFO_SOCKET, 0600) < 0) {
7119                 ErrPrint("info socket: %s\n", strerror(errno));
7120         }
7121
7122         if (chmod(SLAVE_SOCKET, 0666) < 0) {
7123                 ErrPrint("slave socket: %s\n", strerror(errno));
7124         }
7125
7126         if (chmod(CLIENT_SOCKET, 0666) < 0) {
7127                 ErrPrint("client socket: %s\n", strerror(errno));
7128         }
7129
7130         if (chmod(SERVICE_SOCKET, 0666) < 0) {
7131                 ErrPrint("service socket: %s\n", strerror(errno));
7132         }
7133
7134         return 0;
7135 }
7136
7137 HAPI int server_fini(void)
7138 {
7139         if (s_info.info_fd > 0) {
7140                 com_core_packet_server_fini(s_info.info_fd);
7141                 s_info.info_fd = -1;
7142         }
7143
7144         if (s_info.slave_fd > 0) {
7145                 com_core_packet_server_fini(s_info.slave_fd);
7146                 s_info.slave_fd = -1;
7147         }
7148
7149         if (s_info.client_fd > 0) {
7150                 com_core_packet_server_fini(s_info.client_fd);
7151                 s_info.client_fd = -1;
7152         }
7153
7154         if (s_info.remote_client_fd > 0) {
7155                 com_core_packet_server_fini(s_info.remote_client_fd);
7156                 s_info.remote_client_fd = -1;
7157         }
7158
7159         if (s_info.service_fd > 0) {
7160                 com_core_packet_server_fini(s_info.service_fd);
7161                 s_info.service_fd = -1;
7162         }
7163
7164         return 0;
7165 }
7166
7167 /* End of a file */