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