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