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