Initialize the project.
[apps/livebox/data-provider-master.git] / src / server.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20
21 #include <dlog.h>
22 #include <Evas.h>
23 #include <Ecore_Evas.h> /* fb.h */
24 #include <aul.h>
25 #include <Ecore.h>
26
27 #include <packet.h>
28 #include <com-core_packet.h>
29
30 #include "conf.h"
31 #include "debug.h"
32 #include "server.h"
33 #include "slave_life.h"
34 #include "slave_rpc.h"
35 #include "client_life.h"
36 #include "instance.h"
37 #include "client_rpc.h"
38 #include "package.h"
39 #include "script_handler.h"
40 #include "buffer_handler.h"
41 #include "util.h"
42 #include "fault_manager.h"
43 #include "fb.h" /* fb_type */
44 #include "group.h"
45 #include "xmonitor.h"
46 #include "abi.h"
47 #include "liveinfo.h"
48 #include "io.h"
49
50 static struct info {
51         int info_fd;
52         int client_fd;
53         int service_fd;
54         int slave_fd;
55 } s_info = {
56         .info_fd = -1,
57         .client_fd = -1,
58         .service_fd = -1,
59         .slave_fd = -1,
60 };
61
62 /* Share this with provider */
63 enum target_type {
64         TYPE_LB,
65         TYPE_PD,
66         TYPE_ERROR,
67 };
68
69 struct deleted_item {
70         struct client_node *client;
71         struct inst_info *inst;
72 };
73
74 static struct packet *client_acquire(pid_t pid, int handle, const struct packet *packet) /*!< timestamp, ret */
75 {
76         struct client_node *client;
77         struct packet *result;
78         double timestamp;
79         int ret;
80
81         client = client_find_by_pid(pid);
82         if (client) {
83                 ErrPrint("Client is already exists %d\n", pid);
84                 ret = -EEXIST;
85                 goto out;
86         }
87
88         if (packet_get(packet, "d", &timestamp) != 1) {
89                 ErrPrint("Invalid arguemnt\n");
90                 ret = -EINVAL;
91                 goto out;
92         }
93
94         DbgPrint("Acquired %lf\n", timestamp);
95
96         ret = 0;
97         /*!
98          * \note
99          * client_create will invoke the client created callback
100          */
101         client = client_create(pid, handle);
102         if (!client) {
103                 ErrPrint("Failed to create a new client for %d\n", pid);
104                 ret = -EFAULT;
105         }
106
107 out:
108         result = packet_create_reply(packet, "i", ret);
109         if (!result)
110                 ErrPrint("Failed to create a packet\n");
111
112         return result;
113 }
114
115 static struct packet *cilent_release(pid_t pid, int handle, const struct packet *packet) /*!< pid, ret */
116 {
117         struct client_node *client;
118         struct packet *result;
119         int ret;
120
121         client = client_find_by_pid(pid);
122         if (!client) {
123                 ErrPrint("Client %d is not exists\n", pid);
124                 ret = -ENOENT;
125                 goto out;
126         }
127
128         client_destroy(client);
129         ret = 0;
130
131 out:
132         result = packet_create_reply(packet, "i", ret);
133         if (!result)
134                 ErrPrint("Failed to create a packet\n");
135
136         return result;
137 }
138
139 /*!< pid, pkgname, filename, event, timestamp, x, y, ret */
140 static struct packet *client_clicked(pid_t pid, int handle, const struct packet *packet)
141 {
142         struct client_node *client;
143         const char *pkgname;
144         const char *id;
145         const char *event;
146         double timestamp;
147         double x;
148         double y;
149         int ret;
150         struct inst_info *inst;
151
152         client = client_find_by_pid(pid);
153         if (!client) {
154                 ErrPrint("Client %d is not exists\n", pid);
155                 ret = -ENOENT;
156                 goto out;
157         }
158
159         ret = packet_get(packet, "sssddd", &pkgname, &id, &event, &timestamp, &x, &y);
160         if (ret != 6) {
161                 ErrPrint("Parameter is not matched\n");
162                 ret = -EINVAL;
163                 goto out;
164         }
165
166         DbgPrint("pid[%d] pkgname[%s] id[%s] event[%s] timestamp[%lf] x[%lf] y[%lf]\n", pid, pkgname, id, event, timestamp, x, y);
167
168         /*!
169          * \NOTE:
170          * Trust the package name which are sent by the client.
171          * The package has to be a livebox package name.
172          */
173         inst = package_find_instance_by_id(pkgname, id);
174         if (!inst)
175                 ret = -ENOENT;
176         else if (package_is_fault(instance_package(inst)))
177                 ret = -EFAULT;
178         else
179                 ret = instance_clicked(inst, event, timestamp, x, y);
180
181 out:
182         /*! \note No reply packet */
183         return NULL;
184 }
185
186 /* pid, pkgname, filename, emission, source, s, sy, ex, ey, ret */
187 static struct packet *client_text_signal(pid_t pid, int handle, const struct packet *packet)
188 {
189         struct client_node *client;
190         struct packet *result;
191         const char *pkgname;
192         const char *id;
193         const char *emission;
194         const char *source;
195         double sx;
196         double sy;
197         double ex;
198         double ey;
199         struct inst_info *inst;
200         int ret;
201
202         client = client_find_by_pid(pid);
203         if (!client) {
204                 ErrPrint("Client %d is not exists\n", pid);
205                 ret = -ENOENT;
206                 goto out;
207         }
208
209         ret = packet_get(packet, "ssssdddd", &pkgname, &id, &emission, &source, &sx, &sy, &ex, &ey);
210         if (ret != 8) {
211                 ErrPrint("Parameter is not matched\n");
212                 ret = -EINVAL;
213                 goto out;
214         }
215
216         DbgPrint("pid[%d] pkgname[%s] id[%s] emission[%s] source[%s] sx[%lf] sy[%lf] ex[%lf] ey[%lf]\n", pid, pkgname, id, emission, source, sx, sy, ex, ey);
217
218         /*!
219          * \NOTE:
220          * Trust the package name which are sent by the client.
221          * The package has to be a livebox package name.
222          */
223         inst = package_find_instance_by_id(pkgname, id);
224         if (!inst)
225                 ret = -ENOENT;
226         else if (package_is_fault(instance_package(inst)))
227                 ret = -EFAULT;
228         else
229                 ret = instance_text_signal_emit(inst, emission, source, sx, sy, ex, ey);
230
231 out:
232         result = packet_create_reply(packet, "i", ret);
233         if (!result)
234                 ErrPrint("Failed to create a packet\n");
235
236         return result;
237 }
238
239 static Eina_Bool lazy_delete_cb(void *data)
240 {
241         struct deleted_item *item = data;
242
243         DbgPrint("Send delete event to the client\n");
244
245         /*!
246          * Before invoke this callback, the instance is able to already remove this client
247          * So check it again
248          */
249         if (instance_has_client(item->inst, item->client)) {
250                 instance_unicast_deleted_event(item->inst, item->client);
251                 instance_del_client(item->inst, item->client);
252         }
253
254         client_unref(item->client);
255         instance_unref(item->inst);
256         DbgFree(item);
257         return ECORE_CALLBACK_CANCEL;
258 }
259
260 static struct packet *client_delete(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
261 {
262         struct client_node *client;
263         struct packet *result;
264         const char *pkgname;
265         const char *id;
266         struct inst_info *inst;
267         int ret;
268
269         client = client_find_by_pid(pid);
270         if (!client) {
271                 ErrPrint("Client %d is not exists\n", pid);
272                 ret = -ENOENT;
273                 goto out;
274         }
275
276         ret = packet_get(packet, "ss", &pkgname, &id);
277         if (ret != 2) {
278                 ErrPrint("Parameter is not matched\n");
279                 ret = -EINVAL;
280                 goto out;
281         }
282
283         DbgPrint("pid[%d] pkgname[%s] id[%s]\n", pid, pkgname, id);
284
285         /*!
286          * \NOTE:
287          * Trust the package name which are sent by the client.
288          * The package has to be a livebox package name.
289          */
290         inst = package_find_instance_by_id(pkgname, id);
291         if (!inst) {
292                 ret = -ENOENT;
293         } else if (package_is_fault(instance_package(inst))) {
294                 ret = -EFAULT;
295         } else if (instance_client(inst) != client) {
296                 if (instance_has_client(inst, client)) {
297                         struct deleted_item *item;
298
299                         item = malloc(sizeof(*item));
300                         if (!item) {
301                                 ErrPrint("Heap: %s\n", strerror(errno));
302                                 ret = -ENOMEM;
303                         } else {
304                                 ret = 0;
305                                 /*!
306                                  * \NOTE:
307                                  * Send DELETED EVENT to the client.
308                                  * after return from this function.
309                                  *
310                                  * Client will prepare the deleted event after get this function's return value.
311                                  * So We have to make a delay to send a deleted event.
312                                  */
313
314                                 item->client = client_ref(client);
315                                 item->inst = instance_ref(inst);
316
317                                 if (!ecore_timer_add(DELAY_TIME, lazy_delete_cb, item)) {
318                                         ErrPrint("Failed to add a delayzed delete callback\n");
319                                         client_unref(client);
320                                         instance_unref(inst);
321                                         DbgFree(item);
322                                         ret = -EFAULT;
323                                 }
324                         }
325                 } else {
326                         ret = -EPERM;
327                 }
328         } else {
329                 ret = instance_destroy(inst);
330         }
331
332 out:
333         result = packet_create_reply(packet, "i", ret);
334         if (!result)
335                 ErrPrint("Failed to create a packet\n");
336
337         return result;
338 }
339
340 static struct packet *client_resize(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, w, h, ret */
341 {
342         struct client_node *client;
343         struct packet *result;
344         const char *pkgname;
345         const char *id;
346         int w;
347         int h;
348         struct inst_info *inst;
349         int ret;
350
351         client = client_find_by_pid(pid);
352         if (!client) {
353                 ErrPrint("Client %d is not exists\n", pid);
354                 ret = -ENOENT;
355                 goto out;
356         }
357
358         ret = packet_get(packet, "ssii", &pkgname, &id, &w, &h);
359         if (ret != 4) {
360                 ErrPrint("Parameter is not matched\n");
361                 ret = -EINVAL;
362                 goto out;
363         }
364
365         DbgPrint("pid[%d] pkgname[%s] id[%s] w[%d] h[%d]\n", pid, pkgname, id, w, h);
366
367         /*!
368          * \NOTE:
369          * Trust the package name which are sent by the client.
370          * The package has to be a livebox package name.
371          */
372         inst = package_find_instance_by_id(pkgname, id);
373         if (!inst) {
374                 ret = -ENOENT;
375         } else if (package_is_fault(instance_package(inst))) {
376                 ret = -EFAULT;
377         } else if (instance_client(inst) != client) {
378                 ret = -EPERM;
379         } else {
380                 ret = instance_resize(inst, w, h);
381         }
382
383 out:
384         result = packet_create_reply(packet, "i", ret);
385         if (!result)
386                 ErrPrint("Failed to create a packet\n");
387
388         return result;
389 }
390
391 static struct packet *client_new(pid_t pid, int handle, const struct packet *packet) /* pid, timestamp, pkgname, content, cluster, category, period, ret */
392 {
393         struct client_node *client;
394         struct packet *result;
395         const char *pkgname;
396         const char *content;
397         const char *cluster;
398         const char *category;
399         double period;
400         double timestamp;
401         int ret;
402         struct pkg_info *info;
403         int width;
404         int height;
405         char *lb_pkgname;
406
407         client = client_find_by_pid(pid);
408         if (!client) {
409                 ErrPrint("Client %d is not exists\n", pid);
410                 ret = -ENOENT;
411                 goto out;
412         }
413
414         ret = packet_get(packet, "dssssdii", &timestamp, &pkgname, &content, &cluster, &category, &period, &width, &height);
415         if (ret != 8) {
416                 ErrPrint("Parameter is not matched\n");
417                 ret = -EINVAL;
418                 goto out;
419         }
420
421         DbgPrint("pid[%d] period[%lf] pkgname[%s] content[%s] cluster[%s] category[%s] period[%lf]\n",
422                                                 pid, timestamp, pkgname, content, cluster, category, period);
423
424         lb_pkgname = package_lb_pkgname(pkgname);
425         if (!lb_pkgname) {
426                 ErrPrint("This %s has no livebox package\n", pkgname);
427                 ret = -EINVAL;
428                 goto out;
429         }
430
431         info = package_find(lb_pkgname);
432         if (!info)
433                 info = package_create(lb_pkgname);
434
435         if (!info) {
436                 ret = -EFAULT;
437         } else if (package_is_fault(info)) {
438                 ret = -EFAULT;
439         } else if (util_free_space(IMAGE_PATH) < MINIMUM_SPACE) {
440                 ErrPrint("Not enough space\n");
441                 ret = -ENOSPC;
442         } else {
443                 struct inst_info *inst;
444
445                 if (period > 0.0f && period < MINIMUM_PERIOD)
446                         period = MINIMUM_PERIOD;
447
448                 if (!strlen(content))
449                         content = DEFAULT_CONTENT;
450
451                 inst = instance_create(client, timestamp, lb_pkgname, content, cluster, category, period, width, height);
452                 /*!
453                  * \note
454                  * Using the "inst" without validate its value is at my disposal. ;)
455                  */
456                 ret = inst ? 0 : -EFAULT;
457         }
458
459         DbgFree(lb_pkgname);
460
461 out:
462         result = packet_create_reply(packet, "i", ret);
463         if (!result)
464                 ErrPrint("Failed to create a packet\n");
465
466         return result;
467 }
468
469 static struct packet *client_change_visibility(pid_t pid, int handle, const struct packet *packet)
470 {
471         struct client_node *client;
472         const char *pkgname;
473         const char *id;
474         enum livebox_visible_state state;
475         int ret;
476         struct inst_info *inst;
477
478         client = client_find_by_pid(pid);
479         if (!client) {
480                 ErrPrint("Client %d is not exists\n", pid);
481                 ret = -ENOENT;
482                 goto out;
483         }
484
485         ret = packet_get(packet, "ssi", &pkgname, &id, (int *)&state);
486         if (ret != 3) {
487                 ErrPrint("Parameter is not matched\n");
488                 ret = -EINVAL;
489                 goto out;
490         }
491
492         DbgPrint("pid[%d] pkgname[%s] id[%s] state[%d]\n", pid, pkgname, id, state);
493
494         /*!
495          * \NOTE:
496          * Trust the package name which are sent by the client.
497          * The package has to be a livebox package name.
498          */
499         inst = package_find_instance_by_id(pkgname, id);
500         if (!inst) {
501                 ret = -ENOENT;
502         } else if (package_is_fault(instance_package(inst))) {
503                 ret = -EFAULT;
504         } else if (instance_client(inst) != client) {
505                 ret = -EPERM;
506         } else {
507                 ret = instance_set_visible_state(inst, state);
508         }
509
510 out:
511         /*! \note No reply packet */
512         return NULL;
513 }
514
515 static struct packet *client_set_period(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, period, ret */
516 {
517         struct client_node *client;
518         struct packet *result;
519         const char *pkgname;
520         const char *id;
521         double period;
522         int ret;
523         struct inst_info *inst;
524
525         client = client_find_by_pid(pid);
526         if (!client) {
527                 ErrPrint("Client %d is not exists\n", pid);
528                 ret = -ENOENT;
529                 goto out;
530         }
531
532         ret = packet_get(packet, "ssd", &pkgname, &id, &period);
533         if (ret != 3) {
534                 ErrPrint("Parameter is not matched\n");
535                 ret = -EINVAL;
536                 goto out;
537         }
538
539         DbgPrint("pid[%d] pkgname[%s] id[%s] period[%lf]\n", pid, pkgname, id, period);
540
541         /*!
542          * \NOTE:
543          * Trust the package name which are sent by the client.
544          * The package has to be a livebox package name.
545          */
546         inst = package_find_instance_by_id(pkgname, id);
547         if (!inst) {
548                 ret = -ENOENT;
549         } else if (package_is_fault(instance_package(inst))) {
550                 ret = -EFAULT;
551         } else if (instance_client(inst) != client) {
552                 ret = -EPERM;
553         } else {
554                 ret = instance_set_period(inst, period);
555         }
556
557 out:
558         result = packet_create_reply(packet, "i", ret);
559         if (!result)
560                 ErrPrint("Failed to create a packet\n");
561
562         return result;
563 }
564
565 static struct packet *client_change_group(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, cluster, category, ret */
566 {
567         struct client_node *client;
568         struct packet *result;
569         const char *pkgname;
570         const char *id;
571         const char *cluster;
572         const char *category;
573         struct inst_info *inst;
574         int ret;
575
576         client = client_find_by_pid(pid);
577         if (!client) {
578                 ErrPrint("Client %d is not exists\n", pid);
579                 ret = -ENOENT;
580                 goto out;
581         }
582
583         ret = packet_get(packet, "ssss", &pkgname, &id, &cluster, &category);
584         if (ret != 4) {
585                 ErrPrint("Parameter is not matched\n");
586                 ret = -EINVAL;
587                 goto out;
588         }
589
590         DbgPrint("pid[%d] pkgname[%s] id[%s] cluster[%s] category[%s]\n", pid, pkgname, id, cluster, category);
591
592         /*!
593          * \NOTE:
594          * Trust the package name which are sent by the client.
595          * The package has to be a livebox package name.
596          */
597         inst = package_find_instance_by_id(pkgname, id);
598         if (!inst) {
599                 ret = -ENOENT;
600         } else if (package_is_fault(instance_package(inst))) {
601                 ret = -EFAULT;
602         } else if (instance_client(inst) != client) {
603                 ret = -EPERM;
604         } else {
605                 ret = instance_change_group(inst, cluster, category);
606         }
607
608 out:
609         result = packet_create_reply(packet, "i", ret);
610         if (!result)
611                 ErrPrint("Failed to create a packet\n");
612
613         return result;
614 }
615
616 static struct packet *client_pd_mouse_enter(pid_t pid, int handle, const struct packet *packet)
617 {
618         struct client_node *client;
619         const char *pkgname;
620         const char *id;
621         int ret;
622         int w;
623         int h;
624         double timestamp;
625         double x;
626         double y;
627         struct inst_info *inst;
628         const struct pkg_info *pkg;
629
630         client = client_find_by_pid(pid);
631         if (!client) {
632                 ErrPrint("Client %d is not exists\n", pid);
633                 ret = -ENOENT;
634                 goto out;
635         }
636
637         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
638         if (ret != 7) {
639                 ErrPrint("Invalid parameter\n");
640                 ret = -EINVAL;
641                 goto out;
642         }
643
644         /*!
645          * \NOTE:
646          * Trust the package name which are sent by the client.
647          * The package has to be a livebox package name.
648          */
649         inst = package_find_instance_by_id(pkgname, id);
650         if (!inst) {
651                 ErrPrint("Instance[%s] is not exists\n", id);
652                 ret = -ENOENT;
653                 goto out;
654         }
655
656         pkg = instance_package(inst);
657         if (!pkg) {
658                 ErrPrint("Package[%s] info is not found\n", pkgname);
659                 ret = -EFAULT;
660                 goto out;
661         }
662
663         if (package_is_fault(pkg)) {
664                 /*!
665                  * \note
666                  * If the package is registered as fault module,
667                  * slave has not load it, so we don't need to do anything at here!
668                  */
669                 DbgPrint("Package[%s] is faulted\n", pkgname);
670                 ret = -EFAULT;
671         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
672                 struct buffer_info *buffer;
673                 struct slave_node *slave;
674                 struct packet *packet;
675
676                 buffer = instance_pd_buffer(inst);
677                 if (!buffer) {
678                         ErrPrint("Instance[%s] has no buffer\n", id);
679                         ret = -EFAULT;
680                         goto out;
681                 }
682
683                 slave = package_slave(pkg);
684                 if (!slave) {
685                         ErrPrint("Package[%s] has no slave\n", pkgname);
686                         ret = -EINVAL;
687                         goto out;
688                 }
689
690                 packet = packet_create_noack("pd_mouse_enter", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
691                 if (!packet) {
692                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
693                         ret = -EFAULT;
694                         goto out;
695                 }
696
697                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
698         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
699                 struct script_info *script;
700                 Evas *e;
701
702                 script = instance_pd_script(inst);
703                 if (!script) {
704                         ret = -EFAULT;
705                         goto out;
706                 }
707
708                 e = script_handler_evas(script);
709                 if (!e) {
710                         ret = -EFAULT;
711                         goto out;
712                 }
713
714                 script_handler_update_pointer(script, x, y, -1);
715                 evas_event_feed_mouse_in(e, timestamp, NULL);
716                 ret = 0;
717         } else {
718                 ErrPrint("Unsupported package\n");
719                 ret = -EINVAL;
720         }
721
722 out:
723         /*! \note No reply packet */
724         return NULL;
725 }
726
727 static struct packet *client_pd_mouse_leave(pid_t pid, int handle, const struct packet *packet)
728 {
729         struct client_node *client;
730         const char *pkgname;
731         const char *id;
732         int ret;
733         int w;
734         int h;
735         double timestamp;
736         double x;
737         double y;
738         struct inst_info *inst;
739         const struct pkg_info *pkg;
740
741         client = client_find_by_pid(pid);
742         if (!client) {
743                 ErrPrint("Client %d is not exists\n", pid);
744                 ret = -ENOENT;
745                 goto out;
746         }
747
748         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
749         if (ret != 7) {
750                 ErrPrint("Parameter is not matched\n");
751                 ret = -EINVAL;
752                 goto out;
753         }
754
755         /*!
756          * \NOTE:
757          * Trust the package name which are sent by the client.
758          * The package has to be a livebox package name.
759          */
760         inst = package_find_instance_by_id(pkgname, id);
761         if (!inst) {
762                 ErrPrint("Instance[%s] is not exists\n", id);
763                 ret = -ENOENT;
764                 goto out;
765         }
766
767         pkg = instance_package(inst);
768         if (!pkg) {
769                 ErrPrint("Package[%s] info is not found\n", pkgname);
770                 ret = -EFAULT;
771                 goto out;
772         }
773
774         if (package_is_fault(pkg)) {
775                 /*!
776                  * \note
777                  * If the package is registered as fault module,
778                  * slave has not load it, so we don't need to do anything at here!
779                  */
780                 DbgPrint("Package[%s] is faulted\n", pkgname);
781                 ret = -EFAULT;
782         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
783                 struct buffer_info *buffer;
784                 struct slave_node *slave;
785                 struct packet *packet;
786
787                 buffer = instance_pd_buffer(inst);
788                 if (!buffer) {
789                         ErrPrint("Instance[%s] has no buffer\n", id);
790                         ret = -EFAULT;
791                         goto out;
792                 }
793
794                 slave = package_slave(pkg);
795                 if (!slave) {
796                         ErrPrint("Package[%s] has no slave\n", pkgname);
797                         ret = -EINVAL;
798                         goto out;
799                 }
800
801                 packet = packet_create_noack("pd_mouse_leave", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
802                 if (!packet) {
803                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
804                         ret = -EFAULT;
805                         goto out;
806                 }
807
808                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
809         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
810                 struct script_info *script;
811                 Evas *e;
812
813                 script = instance_pd_script(inst);
814                 if (!script) {
815                         ret = -EFAULT;
816                         goto out;
817                 }
818
819                 e = script_handler_evas(script);
820                 if (!e) {
821                         ret = -EFAULT;
822                         goto out;
823                 }
824
825                 script_handler_update_pointer(script, x, y, -1);
826                 evas_event_feed_mouse_out(e, timestamp, NULL);
827                 ret = 0;
828         } else {
829                 ErrPrint("Unsupported package\n");
830                 ret = -EINVAL;
831         }
832
833 out:
834         /*! \note No reply packet */
835         return NULL;
836 }
837
838 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 */
839 {
840         struct client_node *client;
841         const char *pkgname;
842         const char *id;
843         int ret;
844         int w;
845         int h;
846         double timestamp;
847         double x;
848         double y;
849         struct inst_info *inst;
850         const struct pkg_info *pkg;
851
852         client = client_find_by_pid(pid);
853         if (!client) {
854                 ErrPrint("Client %d is not exists\n", pid);
855                 ret = -ENOENT;
856                 goto out;
857         }
858
859         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
860         if (ret != 7) {
861                 ErrPrint("Parameter is not matched\n");
862                 ret = -EINVAL;
863                 goto out;
864         }
865
866         /*!
867          * \NOTE:
868          * Trust the package name which are sent by the client.
869          * The package has to be a livebox package name.
870          */
871         inst = package_find_instance_by_id(pkgname, id);
872         if (!inst) {
873                 ErrPrint("Instance[%s] is not exists\n", id);
874                 ret = -ENOENT;
875                 goto out;
876         }
877
878         pkg = instance_package(inst);
879         if (!pkg) {
880                 ErrPrint("Package[%s] info is not found\n", pkgname);
881                 ret = -EFAULT;
882                 goto out;
883         }
884
885         if (package_is_fault(pkg)) {
886                 /*!
887                  * \note
888                  * If the package is registered as fault module,
889                  * slave has not load it, so we don't need to do anything at here!
890                  */
891                 DbgPrint("Package[%s] is faulted\n", pkgname);
892                 ret = -EFAULT;
893         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
894                 struct buffer_info *buffer;
895                 struct slave_node *slave;
896                 struct packet *packet;
897
898                 buffer = instance_pd_buffer(inst);
899                 if (!buffer) {
900                         ErrPrint("Instance[%s] has no buffer\n", id);
901                         ret = -EFAULT;
902                         goto out;
903                 }
904
905                 slave = package_slave(pkg);
906                 if (!slave) {
907                         ErrPrint("Package[%s] has no slave\n", pkgname);
908                         ret = -EINVAL;
909                         goto out;
910                 }
911
912                 packet = packet_create_noack("pd_mouse_down", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
913                 if (!packet) {
914                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
915                         ret = -EFAULT;
916                         goto out;
917                 }
918
919                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
920         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
921                 struct script_info *script;
922                 Evas *e;
923
924                 script = instance_pd_script(inst);
925                 if (!script) {
926                         ret = -EFAULT;
927                         goto out;
928                 }
929
930                 e = script_handler_evas(script);
931                 if (!e) {
932                         ret = -EFAULT;
933                         goto out;
934                 }
935
936                 script_handler_update_pointer(script, x, y, 1);
937                 evas_event_feed_mouse_move(e, x * w, y * h, timestamp, NULL);
938                 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, timestamp + 0.01f, NULL);
939                 ret = 0;
940         } else {
941                 ErrPrint("Unsupported package\n");
942                 ret = -EINVAL;
943         }
944
945 out:
946         /*! \note No reply packet */
947         return NULL;
948 }
949
950 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 */
951 {
952         struct client_node *client;
953         const char *pkgname;
954         const char *id;
955         int ret;
956         int w;
957         int h;
958         double timestamp;
959         double x;
960         double y;
961         struct inst_info *inst;
962         const struct pkg_info *pkg;
963
964         client = client_find_by_pid(pid);
965         if (!client) {
966                 ErrPrint("Client %d is not exists\n", pid);
967                 ret = -ENOENT;
968                 goto out;
969         }
970
971         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
972         if (ret != 7) {
973                 ErrPrint("Parameter is not matched\n");
974                 ret = -EINVAL;
975                 goto out;
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         inst = package_find_instance_by_id(pkgname, id);
984         if (!inst) {
985                 ErrPrint("Instance[%s] is not exists\n", id);
986                 ret = -ENOENT;
987                 goto out;
988         }
989
990         pkg = instance_package(inst);
991         if (!pkg) {
992                 ErrPrint("Package[%s] info is not exists\n", pkgname);
993                 ret = -EFAULT;
994                 goto out;
995         }
996
997         if (package_is_fault(pkg)) {
998                 /*!
999                  * \note
1000                  * If the package is registered as fault module,
1001                  * slave has not load it, so we don't need to do anything at here!
1002                  */
1003                 DbgPrint("Package[%s] is faulted\n", pkgname);
1004                 ret = -EFAULT;
1005         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1006                 struct buffer_info *buffer;
1007                 struct slave_node *slave;
1008                 struct packet *packet;
1009
1010                 buffer = instance_pd_buffer(inst);
1011                 if (!buffer) {
1012                         ErrPrint("Instance[%s] has no buffer\n", id);
1013                         ret = -EFAULT;
1014                         goto out;
1015                 }
1016
1017                 slave = package_slave(pkg);
1018                 if (!slave) {
1019                         ErrPrint("Package[%s] has no slave\n", pkgname);
1020                         ret = -EINVAL;
1021                         goto out;
1022                 }
1023
1024                 packet = packet_create_noack("pd_mouse_up", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1025                 if (!packet) {
1026                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1027                         ret = -EFAULT;
1028                         goto out;
1029                 }
1030
1031                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
1032         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1033                 struct script_info *script;
1034                 Evas *e;
1035
1036                 script = instance_pd_script(inst);
1037                 if (!script) {
1038                         ret = -EFAULT;
1039                         goto out;
1040                 }
1041
1042                 e = script_handler_evas(script);
1043                 if (!e) {
1044                         ret = -EFAULT;
1045                         goto out;
1046                 }
1047
1048                 script_handler_update_pointer(script, x, y, 0);
1049                 evas_event_feed_mouse_move(e, x * w, y * h, timestamp, NULL);
1050                 evas_event_feed_mouse_up(e, 1, EVAS_BUTTON_NONE, timestamp + 0.1f, NULL);
1051                 ret = 0;
1052         } else {
1053                 ErrPrint("Unsupported package\n");
1054                 ret = -EINVAL;
1055         }
1056
1057 out:
1058         /*! \note No reply packet */
1059         return NULL;
1060 }
1061
1062 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 */
1063 {
1064         struct client_node *client;
1065         const char *pkgname;
1066         const char *id;
1067         int ret;
1068         int w;
1069         int h;
1070         double timestamp;
1071         double x;
1072         double y;
1073         struct inst_info *inst;
1074         const struct pkg_info *pkg;
1075
1076         client = client_find_by_pid(pid);
1077         if (!client) {
1078                 ErrPrint("Client %d is not exists\n", pid);
1079                 ret = -ENOENT;
1080                 goto out;
1081         }
1082
1083         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
1084         if (ret != 7) {
1085                 ErrPrint("Parameter is not matched\n");
1086                 ret = -EINVAL;
1087                 goto out;
1088         }
1089
1090         /*!
1091          * \NOTE:
1092          * Trust the package name which are sent by the client.
1093          * The package has to be a livebox package name.
1094          */
1095         inst = package_find_instance_by_id(pkgname, id);
1096         if (!inst) {
1097                 ErrPrint("Instance[%s] is not exists\n", id);
1098                 ret = -ENOENT;
1099                 goto out;
1100         }
1101
1102         pkg = instance_package(inst);
1103         if (!pkg) {
1104                 ErrPrint("Package[%s] info is not exists\n", pkgname);
1105                 ret = -EFAULT;
1106                 goto out;
1107         }
1108
1109         if (package_is_fault(pkg)) {
1110                 /*!
1111                  * \note
1112                  * If the package is registered as fault module,
1113                  * slave has not load it, so we don't need to do anything at here!
1114                  */
1115                 DbgPrint("Package[%s] is faulted\n", pkgname);
1116                 ret = -EFAULT;
1117         } else if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
1118                 struct buffer_info *buffer;
1119                 struct slave_node *slave;
1120                 //struct packet *packet;
1121
1122                 buffer = instance_pd_buffer(inst);
1123                 if (!buffer) {
1124                         ErrPrint("Instance[%s] has no buffer\n", id);
1125                         ret = -EFAULT;
1126                         goto out;
1127                 }
1128
1129                 slave = package_slave(pkg);
1130                 if (!slave) {
1131                         ErrPrint("Package[%s] has no slave\n", pkgname);
1132                         ret = -EINVAL;
1133                         goto out;
1134                 }
1135
1136                 /*!
1137                  * Reuse the packet.
1138                 packet = packet_create_noack("pd_mouse_move", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1139                 if (!packet) {
1140                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1141                         ret = -EFAULT;
1142                         goto out;
1143                 }
1144                  */
1145                 packet_ref((struct packet *)packet);
1146                 ret = slave_rpc_request_only(slave, pkgname, (struct packet *)packet, 0);
1147         } else if (package_pd_type(pkg) == PD_TYPE_SCRIPT) {
1148                 struct script_info *script;
1149                 Evas *e;
1150
1151                 script = instance_pd_script(inst);
1152                 if (!script) {
1153                         ret = -EFAULT;
1154                         goto out;
1155                 }
1156
1157                 e = script_handler_evas(script);
1158                 if (!e) {
1159                         ret = -EFAULT;
1160                         goto out;
1161                 }
1162
1163                 script_handler_update_pointer(script, x, y, -1);
1164                 evas_event_feed_mouse_move(e, x * w, y * h, timestamp, NULL);
1165                 ret = 0;
1166         } else {
1167                 ErrPrint("Unsupported package\n");
1168                 ret = -EINVAL;
1169         }
1170
1171 out:
1172         /*! \note No reply packet */
1173         return NULL;
1174 }
1175
1176 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 */
1177 {
1178         struct client_node *client;
1179         const char *pkgname;
1180         const char *id;
1181         int ret;
1182         int w;
1183         int h;
1184         double timestamp;
1185         double x;
1186         double y;
1187         struct inst_info *inst;
1188         const struct pkg_info *pkg;
1189
1190         client = client_find_by_pid(pid);
1191         if (!client) {
1192                 ErrPrint("Client %d is not exists\n", pid);
1193                 ret = -ENOENT;
1194                 goto out;
1195         }
1196
1197         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
1198         if (ret != 7) {
1199                 ErrPrint("Parameter is not matched\n");
1200                 ret = -EINVAL;
1201                 goto out;
1202         }
1203
1204         /*!
1205          * \NOTE:
1206          * Trust the package name which are sent by the client.
1207          * The package has to be a livebox package name.
1208          */
1209         inst = package_find_instance_by_id(pkgname, id);
1210         if (!inst) {
1211                 ErrPrint("Instance[%s] is not exists\n", id);
1212                 ret = -ENOENT;
1213                 goto out;
1214         }
1215
1216         pkg = instance_package(inst);
1217         if (!pkg) {
1218                 ErrPrint("Package[%s] info is not exists\n", pkgname);
1219                 ret = -EFAULT;
1220                 goto out;
1221         }
1222
1223         if (package_is_fault(pkg)) {
1224                 /*!
1225                  * \note
1226                  * If the package is registered as fault module,
1227                  * slave has not load it, so we don't need to do anything at here!
1228                  */
1229                 DbgPrint("Package[%s] is faulted\n", pkgname);
1230                 ret = -EFAULT;
1231         } else if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1232                 struct buffer_info *buffer;
1233                 struct slave_node *slave;
1234                 //struct packet *packet;
1235
1236                 buffer = instance_lb_buffer(inst);
1237                 if (!buffer) {
1238                         ErrPrint("Instance[%s] has no buffer\n", id);
1239                         ret = -EFAULT;
1240                         goto out;
1241                 }
1242
1243                 slave = package_slave(pkg);
1244                 if (!slave) {
1245                         ErrPrint("Package[%s] has no slave\n", pkgname);
1246                         ret = -EINVAL;
1247                         goto out;
1248                 }
1249
1250                 /*
1251                 packet = packet_create_noack("lb_mouse_move", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1252                 if (!packet) {
1253                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1254                         ret = -EFAULT;
1255                         goto out;
1256                 }
1257                 */
1258                 packet_ref((struct packet *)packet);
1259                 ret = slave_rpc_request_only(slave, pkgname, (struct packet *)packet, 0);
1260         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1261                 struct script_info *script;
1262                 Evas *e;
1263
1264                 script = instance_lb_script(inst);
1265                 if (!script) {
1266                         ret = -EFAULT;
1267                         goto out;
1268                 }
1269
1270                 e = script_handler_evas(script);
1271                 if (!e) {
1272                         ret = -EFAULT;
1273                         goto out;
1274                 }
1275
1276                 script_handler_update_pointer(script, x, y, -1);
1277                 evas_event_feed_mouse_move(e, x * w, y * h, timestamp, NULL);
1278                 ret = 0;
1279         } else {
1280                 ErrPrint("Unsupported package\n");
1281                 ret = -EINVAL;
1282         }
1283
1284 out:
1285         /*! \note No reply packet */
1286         return NULL;
1287 }
1288
1289 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 */
1290 {
1291         struct client_node *client;
1292         const char *pkgname;
1293         const char *id;
1294         int ret;
1295         int w;
1296         int h;
1297         double timestamp;
1298         double x;
1299         double y;
1300         struct inst_info *inst;
1301         const struct pkg_info *pkg;
1302
1303         client = client_find_by_pid(pid);
1304         if (!client) {
1305                 ErrPrint("Client %d is not exists\n", pid);
1306                 ret = -ENOENT;
1307                 goto out;
1308         }
1309
1310         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
1311         if (ret != 7) {
1312                 ErrPrint("Parameter is not matched\n");
1313                 ret = -EINVAL;
1314                 goto out;
1315         }
1316
1317         /*!
1318          * \NOTE:
1319          * Trust the package name which are sent by the client.
1320          * The package has to be a livebox package name.
1321          */
1322         inst = package_find_instance_by_id(pkgname, id);
1323         if (!inst) {
1324                 ErrPrint("Instance[%s] is not exists\n", id);
1325                 ret = -ENOENT;
1326                 goto out;
1327         }
1328
1329         pkg = instance_package(inst);
1330         if (!pkg) {
1331                 ErrPrint("Package[%s] info is not exists\n", pkgname);
1332                 ret = -EFAULT;
1333                 goto out;
1334         }
1335
1336         if (package_is_fault(pkg)) {
1337                 /*!
1338                  * \note
1339                  * If the package is registered as fault module,
1340                  * slave has not load it, so we don't need to do anything at here!
1341                  */
1342                 DbgPrint("Package[%s] is faulted\n", pkgname);
1343                 ret = -EFAULT;
1344         } else if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1345                 struct buffer_info *buffer;
1346                 struct slave_node *slave;
1347                 struct packet *packet;
1348
1349                 buffer = instance_lb_buffer(inst);
1350                 if (!buffer) {
1351                         ErrPrint("Instance[%s] has no buffer\n", id);
1352                         ret = -EFAULT;
1353                         goto out;
1354                 }
1355
1356                 slave = package_slave(pkg);
1357                 if (!slave) {
1358                         ErrPrint("Package[%s] has no slave\n", pkgname);
1359                         ret = -EINVAL;
1360                         goto out;
1361                 }
1362
1363                 packet = packet_create_noack("lb_mouse_enter", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1364                 if (!packet) {
1365                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1366                         ret = -EFAULT;
1367                         goto out;
1368                 }
1369
1370                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
1371         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1372                 struct script_info *script;
1373                 Evas *e;
1374
1375                 script = instance_lb_script(inst);
1376                 if (!script) {
1377                         ret = -EFAULT;
1378                         goto out;
1379                 }
1380
1381                 e = script_handler_evas(script);
1382                 if (!e) {
1383                         ret = -EFAULT;
1384                         goto out;
1385                 }
1386
1387                 script_handler_update_pointer(script, x, y, -1);
1388                 evas_event_feed_mouse_in(e, timestamp, NULL);
1389                 ret = 0;
1390         } else {
1391                 ErrPrint("Unsupported package\n");
1392                 ret = -EINVAL;
1393         }
1394
1395 out:
1396         /*! \note No reply packet */
1397         return NULL;
1398 }
1399
1400 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 */
1401 {
1402         struct client_node *client;
1403         const char *pkgname;
1404         const char *id;
1405         int ret;
1406         int w;
1407         int h;
1408         double timestamp;
1409         double x;
1410         double y;
1411         struct inst_info *inst;
1412         const struct pkg_info *pkg;
1413
1414         client = client_find_by_pid(pid);
1415         if (!client) {
1416                 ErrPrint("Client %d is not exists\n", pid);
1417                 ret = -ENOENT;
1418                 goto out;
1419         }
1420
1421         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
1422         if (ret != 7) {
1423                 ErrPrint("Parameter is not matched\n");
1424                 ret = -EINVAL;
1425                 goto out;
1426         }
1427
1428         /*!
1429          * \NOTE:
1430          * Trust the package name which are sent by the client.
1431          * The package has to be a livebox package name.
1432          */
1433         inst = package_find_instance_by_id(pkgname, id);
1434         if (!inst) {
1435                 ErrPrint("Instance[%s] is not exists\n", id);
1436                 ret = -ENOENT;
1437                 goto out;
1438         }
1439
1440         pkg = instance_package(inst);
1441         if (!pkg) {
1442                 ErrPrint("Package[%s] info is not exists\n", pkgname);
1443                 ret = -EFAULT;
1444                 goto out;
1445         }
1446
1447         if (package_is_fault(pkg)) {
1448                 /*!
1449                  * \note
1450                  * If the package is registered as fault module,
1451                  * slave has not load it, so we don't need to do anything at here!
1452                  */
1453                 DbgPrint("Package[%s] is faulted\n", pkgname);
1454                 ret = -EFAULT;
1455         } else if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1456                 struct buffer_info *buffer;
1457                 struct slave_node *slave;
1458                 struct packet *packet;
1459
1460                 buffer = instance_lb_buffer(inst);
1461                 if (!buffer) {
1462                         ErrPrint("Instance[%s] has no buffer\n", id);
1463                         ret = -EFAULT;
1464                         goto out;
1465                 }
1466
1467                 slave = package_slave(pkg);
1468                 if (!slave) {
1469                         ErrPrint("Package[%s] has no slave\n", pkgname);
1470                         ret = -EINVAL;
1471                         goto out;
1472                 }
1473
1474                 packet = packet_create_noack("lb_mouse_leave", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1475                 if (!packet) {
1476                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1477                         ret = -EFAULT;
1478                         goto out;
1479                 }
1480
1481                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
1482         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1483                 struct script_info *script;
1484                 Evas *e;
1485
1486                 script = instance_lb_script(inst);
1487                 if (!script) {
1488                         ret = -EFAULT;
1489                         goto out;
1490                 }
1491
1492                 e = script_handler_evas(script);
1493                 if (!e) {
1494                         ret = -EFAULT;
1495                         goto out;
1496                 }
1497
1498                 script_handler_update_pointer(script, x, y, -1);
1499                 evas_event_feed_mouse_out(e, timestamp, NULL);
1500                 ret = 0;
1501         } else {
1502                 ErrPrint("Unsupported package\n");
1503                 ret = -EINVAL;
1504         }
1505
1506 out:
1507         /*! \note No reply packet */
1508         return NULL;
1509 }
1510
1511 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 */
1512 {
1513         struct client_node *client;
1514         const char *pkgname;
1515         const char *id;
1516         int ret;
1517         int w;
1518         int h;
1519         double timestamp;
1520         double x;
1521         double y;
1522         struct inst_info *inst;
1523         const struct pkg_info *pkg;
1524
1525         client = client_find_by_pid(pid);
1526         if (!client) {
1527                 ErrPrint("Client %d is not exists\n", pid);
1528                 ret = -ENOENT;
1529                 goto out;
1530         }
1531
1532         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
1533         if (ret != 7) {
1534                 ErrPrint("Parameter is not matched\n");
1535                 ret = -EINVAL;
1536                 goto out;
1537         }
1538
1539         /*!
1540          * \NOTE:
1541          * Trust the package name which are sent by the client.
1542          * The package has to be a livebox package name.
1543          */
1544         inst = package_find_instance_by_id(pkgname, id);
1545         if (!inst) {
1546                 ErrPrint("Instance[%s] is not exists\n", id);
1547                 ret = -ENOENT;
1548                 goto out;
1549         }
1550
1551         pkg = instance_package(inst);
1552         if (!pkg) {
1553                 ErrPrint("Package[%s] info is not exists\n", pkgname);
1554                 ret = -EFAULT;
1555                 goto out;
1556         }
1557
1558         if (package_is_fault(pkg)) {
1559                 /*!
1560                  * \note
1561                  * If the package is registered as fault module,
1562                  * slave has not load it, so we don't need to do anything at here!
1563                  */
1564                 DbgPrint("Package[%s] is faulted\n", pkgname);
1565                 ret = -EFAULT;
1566         } else if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1567                 struct buffer_info *buffer;
1568                 struct slave_node *slave;
1569                 struct packet *packet;
1570
1571                 buffer = instance_lb_buffer(inst);
1572                 if (!buffer) {
1573                         ErrPrint("Instance[%s] has no buffer\n", id);
1574                         ret = -EFAULT;
1575                         goto out;
1576                 }
1577
1578                 slave = package_slave(pkg);
1579                 if (!slave) {
1580                         ErrPrint("Package[%s] has no slave\n", pkgname);
1581                         ret = -EINVAL;
1582                         goto out;
1583                 }
1584
1585                 packet = packet_create_noack("lb_mouse_down", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1586                 if (!packet) {
1587                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1588                         ret = -EFAULT;
1589                         goto out;
1590                 }
1591
1592                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
1593         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1594                 struct script_info *script;
1595                 Evas *e;
1596
1597                 script = instance_lb_script(inst);
1598                 if (!script) {
1599                         ret = -EFAULT;
1600                         goto out;
1601                 }
1602
1603                 e = script_handler_evas(script);
1604                 if (!e) {
1605                         ret = -EFAULT;
1606                         goto out;
1607                 }
1608
1609                 script_handler_update_pointer(script, x, y, 1);
1610                 evas_event_feed_mouse_move(e, x * w, y * h, timestamp, NULL);
1611                 evas_event_feed_mouse_down(e, 1, EVAS_BUTTON_NONE, timestamp + 0.01f, NULL);
1612                 ret = 0;
1613         } else {
1614                 ErrPrint("Unsupported package\n");
1615                 ret = -EINVAL;
1616         }
1617
1618 out:
1619         /*! \note No reply packet */
1620         return NULL;
1621 }
1622
1623 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 */
1624 {
1625         struct client_node *client;
1626         const char *pkgname;
1627         const char *id;
1628         int ret;
1629         int w;
1630         int h;
1631         double timestamp;
1632         double x;
1633         double y;
1634         struct inst_info *inst;
1635         const struct pkg_info *pkg;
1636
1637         client = client_find_by_pid(pid);
1638         if (!client) {
1639                 ErrPrint("Client %d is not exists\n", pid);
1640                 ret = -ENOENT;
1641                 goto out;
1642         }
1643
1644         ret = packet_get(packet, "ssiiddd", &pkgname, &id, &w, &h, &timestamp, &x, &y);
1645         if (ret != 7) {
1646                 ErrPrint("Parameter is not matched\n");
1647                 ret = -EINVAL;
1648                 goto out;
1649         }
1650
1651         /*!
1652          * \NOTE:
1653          * Trust the package name which are sent by the client.
1654          * The package has to be a livebox package name.
1655          */
1656         inst = package_find_instance_by_id(pkgname, id);
1657         if (!inst) {
1658                 ErrPrint("Instance[%s] is not exists\n", id);
1659                 ret = -ENOENT;
1660                 goto out;
1661         }
1662
1663         pkg = instance_package(inst);
1664         if (!pkg) {
1665                 ErrPrint("Package[%s] info is not exists\n", pkgname);
1666                 ret = -EFAULT;
1667                 goto out;
1668         }
1669
1670         if (package_is_fault(pkg)) {
1671                 /*!
1672                  * \note
1673                  * If the package is registered as fault module,
1674                  * slave has not load it, so we don't need to do anything at here!
1675                  */
1676                 DbgPrint("Package[%s] is faulted\n", pkgname);
1677                 ret = -EFAULT;
1678         } else if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
1679                 struct buffer_info *buffer;
1680                 struct slave_node *slave;
1681                 struct packet *packet;
1682
1683                 buffer = instance_lb_buffer(inst);
1684                 if (!buffer) {
1685                         ErrPrint("Instance[%s] has no buffer\n", id);
1686                         ret = -EFAULT;
1687                         goto out;
1688                 }
1689
1690                 slave = package_slave(pkg);
1691                 if (!slave) {
1692                         ErrPrint("Package[%s] has no slave\n", pkgname);
1693                         ret = -EINVAL;
1694                         goto out;
1695                 }
1696
1697                 packet = packet_create_noack("lb_mouse_up", "ssiiddd", pkgname, id, w, h, timestamp, x, y);
1698                 if (!packet) {
1699                         ErrPrint("Failed to create a packet[%s]\n", pkgname);
1700                         ret = -EFAULT;
1701                         goto out;
1702                 }
1703
1704                 ret = slave_rpc_request_only(slave, pkgname, packet, 0);
1705         } else if (package_lb_type(pkg) == LB_TYPE_SCRIPT) {
1706                 struct script_info *script;
1707                 Evas *e;
1708
1709                 script = instance_lb_script(inst);
1710                 if (!script) {
1711                         ret = -EFAULT;
1712                         goto out;
1713                 }
1714
1715                 e = script_handler_evas(script);
1716                 if (!e) {
1717                         ret = -EFAULT;
1718                         goto out;
1719                 }
1720
1721                 script_handler_update_pointer(script, x, y, 0);
1722                 evas_event_feed_mouse_move(e, x * w, y * h, timestamp, NULL);
1723                 evas_event_feed_mouse_up(e, 1, EVAS_BUTTON_NONE, timestamp + 0.1f, NULL);
1724                 ret = 0;
1725         } else {
1726                 ErrPrint("Unsupported package\n");
1727                 ret = -EINVAL;
1728         }
1729
1730 out:
1731         /*! \note No reply packet */
1732         return NULL;
1733 }
1734
1735 static int release_pixmap_cb(struct client_node *client, void *canvas)
1736 {
1737         DbgPrint("Forcely unref the \"buffer\"\n");
1738         buffer_handler_pixmap_unref(canvas);
1739         return -1; /* Delete this callback */
1740 }
1741
1742 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 */
1743 {
1744         struct packet *result;
1745         const char *pkgname;
1746         const char *id;
1747         struct client_node *client;
1748         struct inst_info *inst;
1749         int ret;
1750         int pixmap = 0;
1751         void *buf_ptr;
1752
1753         client = client_find_by_pid(pid);
1754         if (!client) {
1755                 ErrPrint("Client %d is not exists\n", pid);
1756                 goto out;
1757         }
1758
1759         ret = packet_get(packet, "ss", &pkgname, &id);
1760         if (ret != 2) {
1761                 ErrPrint("Parameter is not matched\n");
1762                 goto out;
1763         }
1764
1765         /*!
1766          * \NOTE:
1767          * Trust the package name which are sent by the client.
1768          * The package has to be a livebox package name.
1769          */
1770         inst = package_find_instance_by_id(pkgname, id);
1771         if (!inst) {
1772                 ErrPrint("Failed to find an instance (%s - %s)\n", pkgname, id);
1773                 goto out;
1774         }
1775
1776         DbgPrint("pid[%d] pkgname[%s] id[%s]\n", pid, pkgname, id);
1777
1778         buf_ptr = buffer_handler_pixmap_ref(instance_lb_buffer(inst));
1779         if (!buf_ptr) {
1780                 ErrPrint("Failed to ref pixmap\n");
1781                 goto out;
1782         }
1783
1784         ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
1785         if (ret < 0) {
1786                 ErrPrint("Failed to add a new client deactivate callback\n");
1787                 buffer_handler_pixmap_unref(buf_ptr);
1788                 pixmap = 0;
1789         } else {
1790                 pixmap = buffer_handler_pixmap(instance_lb_buffer(inst));
1791         }
1792
1793 out:
1794         result = packet_create_reply(packet, "i", pixmap);
1795         if (!result)
1796                 ErrPrint("Failed to create a reply packet\n");
1797
1798         return result;
1799 }
1800
1801 static struct packet *client_lb_release_pixmap(pid_t pid, int handle, const struct packet *packet)
1802 {
1803         const char *pkgname;
1804         const char *id;
1805         struct client_node *client;
1806         struct inst_info *inst;
1807         int pixmap;
1808         void *buf_ptr;
1809         int ret;
1810
1811         client = client_find_by_pid(pid);
1812         if (!client) {
1813                 ErrPrint("Client %d is not exists\n", pid);
1814                 goto out;
1815         }
1816
1817         ret = packet_get(packet, "ssi", &pkgname, &id, &pixmap);
1818         if (ret != 3) {
1819                 ErrPrint("Parameter is not matched\n");
1820                 goto out;
1821         }
1822         DbgPrint("pid[%d] pkgname[%s] id[%s] Pixmap[0x%X]\n", pid, pkgname, id, pixmap);
1823
1824         /*!
1825          * \NOTE:
1826          * Trust the package name which are sent by the client.
1827          * The package has to be a livebox package name.
1828          */
1829         inst = package_find_instance_by_id(pkgname, id);
1830         if (!inst) {
1831                 ErrPrint("Failed to find an instance (%s - %s)\n", pkgname, id);
1832                 goto out;
1833         }
1834
1835         buf_ptr = buffer_handler_pixmap_find(pixmap);
1836         if (!buf_ptr) {
1837                 ErrPrint("Failed to find a buf_ptr of 0x%X\n", pixmap);
1838                 goto out;
1839         }
1840
1841         if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr) == 0)
1842                 buffer_handler_pixmap_unref(buf_ptr);
1843
1844 out:
1845         /*! \note No reply packet */
1846         return NULL;
1847 }
1848
1849 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 */
1850 {
1851         struct packet *result;
1852         const char *pkgname;
1853         const char *id;
1854         struct client_node *client;
1855         struct inst_info *inst;
1856         int ret;
1857         int pixmap = 0;
1858         void *buf_ptr;
1859
1860         client = client_find_by_pid(pid);
1861         if (!client) {
1862                 ErrPrint("Client %d is not exists\n", pid);
1863                 goto out;
1864         }
1865
1866         ret = packet_get(packet, "ss", &pkgname, &id);
1867         if (ret != 2) {
1868                 ErrPrint("Parameter is not matched\n");
1869                 goto out;
1870         }
1871
1872         /*!
1873          * \NOTE:
1874          * Trust the package name which are sent by the client.
1875          * The package has to be a livebox package name.
1876          */
1877         inst = package_find_instance_by_id(pkgname, id);
1878         if (!inst) {
1879                 ErrPrint("Failed to find an instance (%s - %s)\n", pkgname, id);
1880                 goto out;
1881         }
1882
1883         DbgPrint("pid[%d] pkgname[%s] id[%s]\n", pid, pkgname, id);
1884
1885         buf_ptr = buffer_handler_pixmap_ref(instance_pd_buffer(inst));
1886         if (!buf_ptr) {
1887                 ErrPrint("Failed to ref pixmap\n");
1888                 goto out;
1889         }
1890
1891         ret = client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr);
1892         if (ret < 0)
1893                 buffer_handler_pixmap_unref(buf_ptr);
1894
1895         pixmap = buffer_handler_pixmap(instance_pd_buffer(inst));
1896 out:
1897         result = packet_create_reply(packet, "i", pixmap);
1898         if (!result)
1899                 ErrPrint("Failed to create a reply packet\n");
1900
1901         return result;
1902 }
1903
1904 static struct packet *client_pd_release_pixmap(pid_t pid, int handle, const struct packet *packet)
1905 {
1906         const char *pkgname;
1907         const char *id;
1908         struct client_node *client;
1909         struct inst_info *inst;
1910         int pixmap;
1911         void *buf_ptr;
1912         int ret;
1913
1914         client = client_find_by_pid(pid);
1915         if (!client) {
1916                 ErrPrint("Client %d is not exists\n", pid);
1917                 goto out;
1918         }
1919
1920         ret = packet_get(packet, "ssi", &pkgname, &id, &pixmap);
1921         if (ret != 3) {
1922                 ErrPrint("Parameter is not matched\n");
1923                 goto out;
1924         }
1925         DbgPrint("pid[%d] pkgname[%s] id[%s]\n", pid, pkgname, id);
1926
1927         /*!
1928          * \NOTE:
1929          * Trust the package name which are sent by the client.
1930          * The package has to be a livebox package name.
1931          */
1932         inst = package_find_instance_by_id(pkgname, id);
1933         if (!inst) {
1934                 ErrPrint("Failed to find an instance (%s - %s)\n", pkgname, id);
1935                 goto out;
1936         }
1937
1938         buf_ptr = buffer_handler_pixmap_find(pixmap);
1939         if (!buf_ptr) {
1940                 ErrPrint("Failed to find a buf_ptr of 0x%X\n", pixmap);
1941                 goto out;
1942         }
1943
1944         if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, release_pixmap_cb, buf_ptr) == 0)
1945                 buffer_handler_pixmap_unref(buf_ptr);
1946
1947 out:
1948         /*! \note No reply packet */
1949         return NULL;
1950 }
1951
1952 static struct packet *client_pinup_changed(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, pinup, ret */
1953 {
1954         struct client_node *client;
1955         struct packet *result;
1956         const char *pkgname;
1957         const char *id;
1958         int pinup;
1959         int ret;
1960         struct inst_info *inst;
1961
1962         client = client_find_by_pid(pid);
1963         if (!client) {
1964                 ErrPrint("Client %d is not exists\n", pid);
1965                 ret = -ENOENT;
1966                 pinup = 0;
1967                 goto out;
1968         }
1969
1970         ret = packet_get(packet, "ssi", &pkgname, &id, &pinup);
1971         if (ret != 3) {
1972                 ErrPrint("Parameter is not matched\n");
1973                 ret = -EINVAL;
1974                 pinup = 0;
1975                 goto out;
1976         }
1977
1978         DbgPrint("pid[%d] pkgname[%s] id[%s] pinup[%d]\n", pid, pkgname, id, pinup);
1979
1980         /*!
1981          * \NOTE:
1982          * Trust the package name which are sent by the client.
1983          * The package has to be a livebox package name.
1984          */
1985         inst = package_find_instance_by_id(pkgname, id);
1986         if (!inst)
1987                 ret = -ENOENT;
1988         else if (package_is_fault(instance_package(inst)))
1989                 ret = -EFAULT;
1990         else
1991                 ret = instance_set_pinup(inst, pinup);
1992
1993 out:
1994         result = packet_create_reply(packet, "i", ret);
1995         if (!result)
1996                 ErrPrint("Failed to create a packet\n");
1997
1998         return result;
1999 }
2000
2001 static Eina_Bool lazy_pd_created_cb(void *data)
2002 {
2003         DbgPrint("Send PD Create event\n");
2004         instance_client_pd_created(data, 0);
2005
2006         instance_unref(data);
2007         return ECORE_CALLBACK_CANCEL;
2008 }
2009
2010 static Eina_Bool lazy_pd_destroyed_cb(void *data)
2011 {
2012         DbgPrint("Send PD Destroy event\n");
2013         instance_client_pd_destroyed(data, 0);
2014
2015         instance_unref(data);
2016         return ECORE_CALLBACK_CANCEL;
2017 }
2018
2019 static struct packet *client_create_pd(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
2020 {
2021         struct client_node *client;
2022         struct packet *result;
2023         const char *pkgname;
2024         const char *id;
2025         int ret;
2026         struct inst_info *inst;
2027         double x;
2028         double y;
2029
2030         client = client_find_by_pid(pid);
2031         if (!client) {
2032                 ErrPrint("Client %d is not exists\n", pid);
2033                 ret = -ENOENT;
2034                 goto out;
2035         }
2036
2037         ret = packet_get(packet, "ssdd", &pkgname, &id, &x, &y);
2038         if (ret != 4) {
2039                 ErrPrint("Parameter is not matched\n");
2040                 ret = -EINVAL;
2041                 goto out;
2042         }
2043
2044         DbgPrint("pid[%d] pkgname[%s] id[%s]\n", pid, pkgname, id);
2045
2046         /*!
2047          * \NOTE:
2048          * Trust the package name which are sent by the client.
2049          * The package has to be a livebox package name.
2050          */
2051         inst = package_find_instance_by_id(pkgname, id);
2052         if (!inst)
2053                 ret = -ENOENT;
2054         else if (package_is_fault(instance_package(inst)))
2055                 ret = -EFAULT;
2056         else if (util_free_space(IMAGE_PATH) < MINIMUM_SPACE)
2057                 ret = -ENOSPC;
2058         else if (package_pd_type(instance_package(inst)) == PD_TYPE_BUFFER) {
2059                 instance_slave_set_pd_pos(inst, x, y);
2060                 ret = instance_slave_open_pd(inst, client);
2061                 ret = instance_signal_emit(inst,
2062                                 "pd,show", util_uri_to_path(instance_id(inst)),
2063                                 0.0, 0.0, 0.0, 0.0, x, y, 0);
2064                 /*!
2065                  * \note
2066                  * PD craeted event will be send by the acquire_buffer function.
2067                  * Because the slave will make request the acquire_buffer to
2068                  * render the PD
2069                  *
2070                  * instance_client_pd_created(inst);
2071                  */
2072         } else if (package_pd_type(instance_package(inst)) == PD_TYPE_SCRIPT) {
2073                 /*!
2074                  * \note
2075                  * ret value should be cared but in this case,
2076                  * we ignore this for this moment, so we have to handle this error later.
2077                  *
2078                  * if ret is less than 0, the slave has some problem.
2079                  * but the script mode doesn't need slave for rendering default view of PD
2080                  * so we can hanle it later.
2081                  */
2082                 instance_slave_set_pd_pos(inst, x, y);
2083                 ret = instance_slave_open_pd(inst, client);
2084                 script_handler_update_pointer(instance_pd_script(inst), x, y, 0);
2085                 ret = script_handler_load(instance_pd_script(inst), 1);
2086
2087                 /*!
2088                  * \note
2089                  * Send the PD created event to the clients,
2090                  */
2091                 if (ret == 0) {
2092                         /*!
2093                          * \note
2094                          * But the created event has to be send afte return
2095                          * from this function or the viewer couldn't care
2096                          * the event correctly.
2097                          */
2098                         inst = instance_ref(inst); /* To guarantee the inst */
2099                         if (!ecore_timer_add(DELAY_TIME, lazy_pd_created_cb, inst))
2100                                 instance_unref(inst);
2101                 }
2102         } else {
2103                 ErrPrint("Invalid PD TYPE\n");
2104                 ret = -EINVAL;
2105         }
2106
2107 out:
2108         result = packet_create_reply(packet, "i", ret);
2109         if (!result)
2110                 ErrPrint("Failed to create a packet\n");
2111
2112         return result;
2113 }
2114
2115 static struct packet *client_destroy_pd(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, filename, ret */
2116 {
2117         struct client_node *client;
2118         struct packet *result;
2119         const char *pkgname;
2120         const char *id;
2121         int ret;
2122         struct inst_info *inst;
2123
2124         client = client_find_by_pid(pid);
2125         if (!client) {
2126                 ErrPrint("Client %d is not exists\n", pid);
2127                 ret = -ENOENT;
2128                 goto out;
2129         }
2130
2131         ret = packet_get(packet, "ss", &pkgname, &id);
2132         if (ret != 2) {
2133                 ErrPrint("Parameter is not matched\n");
2134                 ret = -EINVAL;
2135                 goto out;
2136         }
2137
2138         DbgPrint("pid[%d] pkgname[%s] id[%s]\n", pid, pkgname, id);
2139
2140         /*!
2141          * \NOTE:
2142          * Trust the package name which are sent by the client.
2143          * The package has to be a livebox package name.
2144          */
2145         inst = package_find_instance_by_id(pkgname, id);
2146         if (!inst)
2147                 ret = -ENOENT;
2148         else if (package_is_fault(instance_package(inst)))
2149                 ret = -EFAULT;
2150         else if (package_pd_type(instance_package(inst)) == PD_TYPE_BUFFER) {
2151                 ret = instance_signal_emit(inst,
2152                                 "pd,hide", util_uri_to_path(instance_id(inst)),
2153                                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0);
2154                 ret = instance_slave_close_pd(inst, client);
2155
2156                 /*!
2157                  * \note
2158                  * release_buffer will be called by the slave after this.
2159                  * Then it will send the "pd_destroyed" event to the client
2160                  *
2161                  * instance_client_pd_destroyed(inst);
2162                  */
2163
2164         } else if (package_pd_type(instance_package(inst)) == PD_TYPE_SCRIPT) {
2165                 ret = instance_slave_close_pd(inst, client);
2166
2167                 ret = script_handler_unload(instance_pd_script(inst), 1);
2168
2169                 /*!
2170                  * \note
2171                  * Send the destroyed PD event to the client
2172                  */
2173                 if (ret == 0) {
2174                         inst = instance_ref(inst);
2175                         if (!ecore_timer_add(DELAY_TIME, lazy_pd_destroyed_cb, inst))
2176                                 instance_unref(inst);
2177                 }
2178         } else {
2179                 ErrPrint("Invalid PD TYPE\n");
2180                 ret = -EINVAL;
2181         }
2182
2183 out:
2184         result = packet_create_reply(packet, "i", ret);
2185         if (!result)
2186                 ErrPrint("Failed to create a packet\n");
2187
2188         return result;
2189 }
2190
2191 static struct packet *client_activate_package(pid_t pid, int handle, const struct packet *packet) /* pid, pkgname, ret */
2192 {
2193         struct client_node *client;
2194         struct packet *result;
2195         const char *pkgname;
2196         int ret;
2197         struct pkg_info *info;
2198
2199         client = client_find_by_pid(pid);
2200         if (!client) {
2201                 ErrPrint("Client %d is not exists\n", pid);
2202                 ret = -ENOENT;
2203                 pkgname = "";
2204                 goto out;
2205         }
2206
2207         ret = packet_get(packet, "s", &pkgname);
2208         if (ret != 1) {
2209                 ErrPrint("Parameter is not matched\n");
2210                 ret = -EINVAL;
2211                 pkgname = "";
2212                 goto out;
2213         }
2214
2215         DbgPrint("pid[%d] pkgname[%s]\n", pid, pkgname);
2216
2217         /*!
2218          * \NOTE:
2219          * Validate the livebox package name.
2220          */
2221         if (!package_is_lb_pkgname(pkgname)) {
2222                 ErrPrint("%s is not a valid livebox package\n", pkgname);
2223                 pkgname = "";
2224                 ret = -EINVAL;
2225                 goto out;
2226         }
2227
2228         info = package_find(pkgname);
2229         if (!info)
2230                 ret = -ENOENT;
2231         else
2232                 ret = package_clear_fault(info);
2233
2234 out:
2235         result = packet_create_reply(packet, "is", ret, pkgname);
2236         if (!result)
2237                 ErrPrint("Failed to create a packet\n");
2238
2239         return result;
2240 }
2241
2242 static struct packet *client_subscribed(pid_t pid, int handle, const struct packet *packet)
2243 {
2244         const char *cluster;
2245         const char *category;
2246         struct client_node *client;
2247         int ret;
2248
2249         client = client_find_by_pid(pid);
2250         if (!client) {
2251                 ErrPrint("Client %d is not exists\n", pid);
2252                 ret = -ENOENT;
2253                 goto out;
2254         }
2255
2256         ret = packet_get(packet, "ss", &cluster, &category);
2257         if (ret != 2) {
2258                 ErrPrint("Invalid argument\n");
2259                 ret = -EINVAL;
2260                 goto out;
2261         }
2262
2263         DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster, category);
2264         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
2265                 ErrPrint("Invalid cluster name\n");
2266                 goto out;
2267         }
2268
2269         /*!
2270          * \todo
2271          * SUBSCRIBE cluster & sub-cluster for a client.
2272          */
2273         ret = client_subscribe(client, cluster, category);
2274         if (ret == 0)
2275                 package_alter_instances_to_client(client, ALTER_CREATE);
2276
2277 out:
2278         /*! \note No reply packet */
2279         return NULL;
2280 }
2281
2282 static struct packet *client_delete_cluster(pid_t pid, int handle, const struct packet *packet)
2283 {
2284         const char *cluster;
2285         struct client_node *client;
2286         struct packet *result;
2287         int ret;
2288
2289         client = client_find_by_pid(pid);
2290         if (!client) {
2291                 ErrPrint("Client %d is not exists\n", pid);
2292                 ret = -ENOENT;
2293                 goto out;
2294         }
2295
2296         ret = packet_get(packet, "s", &cluster);
2297         if (ret != 1) {
2298                 ErrPrint("Invalid parameters\n");
2299                 ret = -EINVAL;
2300                 goto out;
2301         }
2302
2303         DbgPrint("pid[%d] cluster[%s]\n", pid, cluster);
2304
2305         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
2306                 ErrPrint("Invalid cluster: %s\n", cluster);
2307                 ret = -EINVAL;
2308                 goto out;
2309         }
2310
2311         /*!
2312          * \todo
2313          */
2314         ret = -ENOSYS;
2315
2316 out:
2317         result = packet_create_reply(packet, "i", ret);
2318         if (!result)
2319                 ErrPrint("Failed to create a packet\n");
2320         return result;
2321 }
2322
2323 static inline int update_pkg_cb(struct category *category, const char *pkgname)
2324 {
2325         const char *c_name;
2326         const char *s_name;
2327
2328         c_name = group_cluster_name_by_category(category);
2329         s_name = group_category_name(category);
2330
2331         if (!c_name || !s_name || !pkgname) {
2332                 ErrPrint("Name is not valid\n");
2333                 return EXIT_FAILURE;
2334         }
2335
2336         DbgPrint("Send refresh request: %s (%s/%s)\n", pkgname, c_name, s_name);
2337         slave_rpc_request_update(pkgname, "", c_name, s_name);
2338
2339         /* Just try to create a new package */
2340         if (util_free_space(IMAGE_PATH) > MINIMUM_SPACE) {
2341                 double timestamp;
2342                 struct inst_info *inst;
2343
2344                 timestamp = util_timestamp();
2345                 /*!
2346                  * \NOTE
2347                  * Don't need to check the subscribed clients.
2348                  * Because this callback is called by the requests of clients.
2349                  * It means. some clients wants to handle this instances ;)
2350                  */
2351                 inst = instance_create(NULL, timestamp, pkgname, DEFAULT_CONTENT, c_name, s_name, DEFAULT_PERIOD, 0, 0);
2352                 if (!inst)
2353                         ErrPrint("Failed to create a new instance\n");
2354         } else {
2355                 ErrPrint("Not enough space\n");
2356         }
2357         return EXIT_SUCCESS;
2358 }
2359
2360 static struct packet *client_refresh_group(pid_t pid, int handle, const struct packet *packet)
2361 {
2362         const char *cluster_id;
2363         const char *category_id;
2364         struct client_node *client;
2365         int ret;
2366         struct cluster *cluster;
2367         struct category *category;
2368         struct context_info *info;
2369         Eina_List *info_list;
2370         Eina_List *l;
2371
2372         client = client_find_by_pid(pid);
2373         if (!client) {
2374                 ErrPrint("Cilent %d is not exists\n", pid);
2375                 ret = -ENOMEM;
2376                 goto out;
2377         }
2378
2379         ret = packet_get(packet, "ss", &cluster_id, &category_id);
2380         if (ret != 2) {
2381                 ErrPrint("Invalid parameter\n");
2382                 ret = -EINVAL;
2383                 goto out;
2384         }
2385
2386         DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster_id, category_id);
2387
2388         if (!strlen(cluster_id) || !strcasecmp(cluster_id, DEFAULT_CLUSTER)) {
2389                 ErrPrint("Invalid cluster name: %s\n", cluster_id);
2390                 ret = -EINVAL;
2391                 goto out;
2392         }
2393
2394         cluster = group_find_cluster(cluster_id);
2395         if (!cluster) {
2396                 ErrPrint("Cluster [%s] is not registered\n", cluster_id);
2397                 ret = -EINVAL;
2398                 goto out;
2399         }
2400
2401         category = group_find_category(cluster, category_id);
2402         if (!category) {
2403                 ErrPrint("Category [%s] is not registered\n", category_id);
2404                 ret = -EINVAL;
2405                 goto out;
2406         }
2407
2408         info_list = group_context_info_list(category);
2409         EINA_LIST_FOREACH(info_list, l, info) {
2410                 update_pkg_cb(category, group_pkgname_from_context_info(info));
2411         }
2412
2413 out:
2414         /*! \note No reply packet */
2415         return NULL;
2416 }
2417
2418 static struct packet *client_delete_category(pid_t pid, int handle, const struct packet *packet)
2419 {
2420         const char *cluster;
2421         const char *category;
2422         struct client_node *client;
2423         struct packet *result;
2424         int ret;
2425
2426         client = client_find_by_pid(pid);
2427         if (!client) {
2428                 ErrPrint("Client %d is not exists\n", pid);
2429                 ret = -ENOENT;
2430                 goto out;
2431         }
2432
2433         ret = packet_get(packet, "ss", &cluster, &category);
2434         if (ret != 2) {
2435                 ErrPrint("Invalid paramenters\n");
2436                 ret = -EINVAL;
2437                 goto out;
2438         }
2439
2440         DbgPrint("pid[%d] cluster[%s] category[%s]\n", pid, cluster, category);
2441         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
2442                 ErrPrint("Invalid cluster: %s\n", cluster);
2443                 ret = -EINVAL;
2444                 goto out;
2445         }
2446
2447         /*!
2448          * \todo
2449          */
2450         ret = -ENOSYS;
2451
2452 out:
2453         result = packet_create_reply(packet, "i", ret);
2454         if (!result)
2455                 ErrPrint("Failed to create a packet\n");
2456         return result;
2457 }
2458
2459 static struct packet *client_unsubscribed(pid_t pid, int handle, const struct packet *packet)
2460 {
2461         const char *cluster;
2462         const char *category;
2463         struct client_node *client;
2464         int ret;
2465
2466         client = client_find_by_pid(pid);
2467         if (!client) {
2468                 ErrPrint("Client %d is not exists\n", pid);
2469                 ret = -ENOENT;
2470                 goto out;
2471         }
2472
2473         ret = packet_get(packet, "ss", &cluster, &category);
2474         if (ret != 2) {
2475                 ErrPrint("Invalid argument\n");
2476                 ret = -EINVAL;
2477                 goto out;
2478         }
2479
2480         DbgPrint("[%d] cluster[%s] category[%s]\n", pid, cluster, category);
2481
2482         if (!strlen(cluster) || !strcasecmp(cluster, DEFAULT_CLUSTER)) {
2483                 ErrPrint("Invalid cluster name: %s\n", cluster);
2484                 goto out;
2485         }
2486
2487         /*!
2488          * \todo
2489          * UNSUBSCRIBE cluster & sub-cluster for a client.
2490          */
2491         ret = client_unsubscribe(client, cluster, category);
2492         if (ret == 0)
2493                 package_alter_instances_to_client(client, ALTER_DESTROY);
2494
2495 out:
2496         /*! \note No reply packet */
2497         return NULL;
2498 }
2499
2500 static struct packet *slave_hello(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
2501 {
2502         struct slave_node *slave;
2503         const char *slavename;
2504         int ret;
2505
2506         ret = packet_get(packet, "s", &slavename);
2507         if (ret != 1) {
2508                 ErrPrint("Parameter is not matched\n");
2509                 ret = -EINVAL;
2510                 goto out;
2511         }
2512
2513         DbgPrint("New slave[%s](%d) is arrived\n", slavename, pid);
2514
2515         slave = slave_find_by_pid(pid);
2516         if (!slave) {
2517                 if (DEBUG_MODE) {
2518                         char pkgname[pathconf("/", _PC_PATH_MAX)];
2519                         const char *abi;
2520
2521                         if (aul_app_get_pkgname_bypid(pid, pkgname, sizeof(pkgname)) != AUL_R_OK) {
2522                                 ErrPrint("pid[%d] is not authroized provider package\n", pid);
2523                                 ret = -EINVAL;
2524                                 goto out;
2525                         }
2526
2527                         slave = slave_find_by_pkgname(pkgname);
2528                         if (!slave) {
2529                                 abi = abi_find_by_pkgname(pkgname);
2530                                 if (!abi)
2531                                         abi = "unknown";
2532
2533                                 slave = slave_create(slavename, 1, abi, pkgname);
2534                                 if (!slave) {
2535                                         ErrPrint("Failed to create a new slave for %s\n", slavename);
2536                                         ret = -EFAULT;
2537                                         goto out;
2538                                 }
2539
2540                                 DbgPrint("New slave is created\n");
2541                         } else {
2542                                 DbgPrint("Registered slave is replaced with this new one\n");
2543                                 abi = slave_abi(slave);
2544                         }
2545
2546                         slave_set_pid(slave, pid);
2547                         DbgPrint("Provider is forcely activated, pkgname(%s), abi(%s), slavename(%s)\n", pkgname, abi, slavename);
2548                 } else {
2549                         ErrPrint("Slave[%d] is not exists\n", pid);
2550                         ret = -ENOENT;
2551                         goto out;
2552                 }
2553         }
2554
2555         /*!
2556          * \note
2557          * After updating handle,
2558          * slave activated callback will be called.
2559          */
2560         slave_rpc_update_handle(slave, handle);
2561
2562 out:
2563         return NULL;
2564 }
2565
2566 static struct packet *slave_ping(pid_t pid, int handle, const struct packet *packet) /* slave_name, ret */
2567 {
2568         struct slave_node *slave;
2569         const char *slavename;
2570         int ret;
2571
2572         slave = slave_find_by_pid(pid);
2573         if (!slave) {
2574                 ErrPrint("Slave %d is not exists\n", pid);
2575                 ret = -ENOENT;
2576                 goto out;
2577         }
2578
2579         ret = packet_get(packet, "s", &slavename);
2580         if (ret != 1) {
2581                 ErrPrint("Parameter is not matched\n");
2582                 ret = -EINVAL;
2583                 goto out;
2584         }
2585
2586         slave_rpc_ping(slave);
2587
2588 out:
2589         return NULL;
2590 }
2591
2592 static struct packet *slave_faulted(pid_t pid, int handle, const struct packet *packet)
2593 {
2594         struct slave_node *slave;
2595         struct inst_info *inst;
2596         const char *slavename;
2597         const char *pkgname;
2598         const char *id;
2599         const char *func;
2600         int ret;
2601
2602         slave = slave_find_by_pid(pid);
2603         if (!slave) {
2604                 ErrPrint("Slave %d is not exists\n", pid);
2605                 ret = -ENOENT;
2606                 goto out;
2607         }
2608
2609         ret = packet_get(packet, "ssss", &slavename, &pkgname, &id, &func);
2610         if (ret != 4) {
2611                 ErrPrint("Parameter is not matched\n");
2612                 ret = -EINVAL;
2613                 goto out;
2614         }
2615
2616         ret = fault_info_set(slave, pkgname, id, func);
2617         DbgPrint("Slave Faulted: %s (%d)\n", slavename, ret);
2618
2619         inst = package_find_instance_by_id(pkgname, id);
2620         if (!inst) {
2621                 DbgPrint("There is no such instance: %s\n", id);
2622                 ret = -ENOENT;
2623         } else if (instance_state(inst) == INST_DESTROYED) {
2624                 ErrPrint("Instance is already destroyed (%s)\n", id);
2625                 ret = -EINVAL;
2626         } else {
2627                 DbgPrint("Destroy instance (%s)\n", id);
2628                 ret = instance_destroy(inst);
2629         }
2630
2631 out:
2632         return NULL;
2633 }
2634
2635 static struct packet *slave_call(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, function, ret */
2636 {
2637         struct slave_node *slave;
2638         const char *slavename;
2639         const char *pkgname;
2640         const char *id;
2641         const char *func;
2642         int ret;
2643
2644         slave = slave_find_by_pid(pid);
2645         if (!slave) {
2646                 ErrPrint("Slave %d is not exists\n", pid);
2647                 ret = -ENOENT;
2648                 goto out;
2649         }
2650
2651         ret = packet_get(packet, "ssss", &slavename, &pkgname, &id, &func);
2652         if (ret != 4) {
2653                 ErrPrint("Parameter is not matched\n");
2654                 ret = -EINVAL;
2655                 goto out;
2656         }
2657
2658         ret = fault_func_call(slave, pkgname, id, func);
2659         slave_give_more_ttl(slave);
2660
2661 out:
2662         return NULL;
2663 }
2664
2665 static struct packet *slave_ret(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, function, ret */
2666 {
2667         struct slave_node *slave;
2668         const char *slavename;
2669         const char *pkgname;
2670         const char *id;
2671         const char *func;
2672         int ret;
2673
2674         slave = slave_find_by_pid(pid);
2675         if (!slave) {
2676                 ErrPrint("Slave %d is not exists\n", pid);
2677                 ret = -ENOENT;
2678                 goto out;
2679         }
2680
2681         ret = packet_get(packet, "ssss", &slavename, &pkgname, &id, &func);
2682         if (ret != 4) {
2683                 ErrPrint("Parameter is not matched\n");
2684                 ret = -EINVAL;
2685                 goto out;
2686         }
2687
2688         ret = fault_func_ret(slave, pkgname, id, func);
2689         slave_give_more_ttl(slave);
2690
2691 out:
2692         return NULL;
2693 }
2694
2695 static inline char *get_file_kept_in_safe(const char *id)
2696 {
2697         const char *path;
2698         char *new_path;
2699         int len;
2700         int base_idx;
2701
2702         path = util_uri_to_path(id);
2703         if (!path) {
2704                 ErrPrint("Invalid URI(%s)\n", id);
2705                 return NULL;
2706         }
2707
2708         /*!
2709          * TODO: Remove me
2710          */
2711         if (OVERWRITE_CONTENT)
2712                 return strdup(path);
2713
2714         len = strlen(path);
2715         base_idx = len - 1;
2716
2717         while (base_idx > 0 && path[base_idx] != '/') base_idx--;
2718         base_idx += (path[base_idx] == '/');
2719
2720         new_path = malloc(len + 10);
2721         if (!new_path) {
2722                 ErrPrint("Heap: %s\n", strerror(errno));
2723                 return NULL;
2724         }
2725
2726         strncpy(new_path, path, base_idx);
2727         snprintf(new_path + base_idx, len + 10 - base_idx, "reader/%s", path + base_idx);
2728         return new_path;
2729 }
2730
2731 static struct packet *slave_updated(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, width, height, priority, ret */
2732 {
2733         struct slave_node *slave;
2734         const char *slavename;
2735         const char *pkgname;
2736         const char *id;
2737         const char *content_info;
2738         const char *title;
2739         int w;
2740         int h;
2741         double priority;
2742         int ret;
2743         struct inst_info *inst;
2744
2745         slave = slave_find_by_pid(pid);
2746         if (!slave) {
2747                 ErrPrint("Slave %d is not exists\n", pid);
2748                 ret = -ENOENT;
2749                 goto out;
2750         }
2751
2752         ret = packet_get(packet, "sssiidss", &slavename, &pkgname, &id,
2753                                                 &w, &h, &priority,
2754                                                 &content_info, &title);
2755         if (ret != 8) {
2756                 ErrPrint("Parameter is not matched\n");
2757                 ret = -EINVAL;
2758                 goto out;
2759         }
2760
2761         inst = package_find_instance_by_id(pkgname, id);
2762         if (!inst) {
2763                 ret = -ENOENT;
2764         } else if (package_is_fault(instance_package(inst))) {
2765                 ErrPrint("Faulted instance cannot make any event.\n");
2766                 ret = -EFAULT;
2767         } else if (instance_state(inst) == INST_DESTROYED) {
2768                 ErrPrint("Instance is already destroyed\n");
2769                 ret = -EINVAL;
2770         } else {
2771                 char *filename;
2772
2773                 instance_set_lb_info(inst, w, h, priority, content_info, title);
2774
2775                 switch (package_lb_type(instance_package(inst))) {
2776                 case LB_TYPE_SCRIPT:
2777                         script_handler_resize(instance_lb_script(inst), w, h);
2778
2779                         filename = get_file_kept_in_safe(id);
2780                         if (filename) {
2781                                 ret = script_handler_parse_desc(pkgname, id,
2782                                                                 filename, 0);
2783                                 DbgFree(filename);
2784                         } else {
2785                                 ret = script_handler_parse_desc(pkgname, id,
2786                                                         util_uri_to_path(id), 0);
2787                         }
2788                         break;
2789                 case LB_TYPE_BUFFER:
2790                         instance_lb_updated_by_instance(inst);
2791                         ret = 0;
2792                         break;
2793                 default:
2794                         /*!
2795                          * \check
2796                          * text format (inst)
2797                          */
2798                         instance_lb_updated_by_instance(inst);
2799                         ret = 0;
2800                         break;
2801                 }
2802
2803                 slave_give_more_ttl(slave);
2804         }
2805
2806 out:
2807         return NULL;
2808 }
2809
2810 static struct packet *slave_desc_updated(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, filename, decsfile, ret */
2811 {
2812         struct slave_node *slave;
2813         const char *slavename;
2814         const char *pkgname;
2815         const char *id;
2816         const char *descfile;
2817         int ret;
2818         struct inst_info *inst;
2819
2820         slave = slave_find_by_pid(pid);
2821         if (!slave) {
2822                 ErrPrint("Slave %d is not exists\n", pid);
2823                 ret = -ENOENT;
2824                 goto out;
2825         }
2826
2827         ret = packet_get(packet, "ssss", &slavename, &pkgname, &id, &descfile);
2828         if (ret != 4) {
2829                 ErrPrint("Parameter is not matched\n");
2830                 ret = -EINVAL;
2831                 goto out;
2832         }
2833
2834         inst = package_find_instance_by_id(pkgname, id);
2835         if (!inst) {
2836                 ret = -ENOENT;
2837         } else if (package_is_fault(instance_package(inst))) {
2838                 ErrPrint("Faulted package cannot make event\n");
2839                 ret = -EFAULT;
2840         } else if (instance_state(inst) == INST_DESTROYED) {
2841                 ErrPrint("Instance is already destroyed\n");
2842                 ret = -EINVAL;
2843         } else {
2844                 switch (package_pd_type(instance_package(inst))) {
2845                 case PD_TYPE_SCRIPT:
2846                         DbgPrint("Script (%s)\n", id);
2847                         if (script_handler_is_loaded(instance_pd_script(inst))) {
2848                                 ret = script_handler_parse_desc(pkgname, id,
2849                                                                 descfile, 1);
2850                         }
2851                         break;
2852                 case PD_TYPE_TEXT:
2853                         instance_set_pd_info(inst, 0, 0);
2854                 case PD_TYPE_BUFFER:
2855                         instance_pd_updated(pkgname, id, descfile);
2856                         ret = 0;
2857                         break;
2858                 default:
2859                         DbgPrint("Ignore updated DESC(%s - %s - %s)\n",
2860                                                         pkgname, id, descfile);
2861                         ret = 0;
2862                         break;
2863                 }
2864         }
2865
2866 out:
2867         return NULL;
2868 }
2869
2870 static struct packet *slave_deleted(pid_t pid, int handle, const struct packet *packet) /* slave_name, pkgname, id, ret */
2871 {
2872         struct slave_node *slave;
2873         const char *slavename;
2874         const char *pkgname;
2875         const char *id;
2876         int ret;
2877         struct inst_info *inst;
2878
2879         slave = slave_find_by_pid(pid);
2880         if (!slave) {
2881                 ErrPrint("Slave %d is not exists\n", pid);
2882                 ret = -ENOENT;
2883                 goto out;
2884         }
2885
2886         ret = packet_get(packet, "sss", &slavename, &pkgname, &id);
2887         if (ret != 3) {
2888                 ErrPrint("Parameter is not matched\n");
2889                 ret = -EINVAL;
2890                 goto out;
2891         }
2892
2893         inst = package_find_instance_by_id(pkgname, id);
2894         if (!inst)
2895                 ret = -ENOENT;
2896         else if (package_is_fault(instance_package(inst)))
2897                 ret = -EFAULT;
2898         else
2899                 ret = instance_destroyed(inst);
2900
2901 out:
2902         return NULL;
2903 }
2904
2905 /*!
2906  * \note for the BUFFER Type slave
2907  */
2908 static struct packet *slave_acquire_buffer(pid_t pid, int handle, const struct packet *packet) /* type, id, w, h, size */
2909 {
2910         enum target_type target;
2911         const char *slavename;
2912         const char *pkgname;
2913         const char *id;
2914         int w;
2915         int h;
2916         int pixel_size;
2917         struct packet *result;
2918         struct slave_node *slave;
2919         struct inst_info *inst;
2920         const struct pkg_info *pkg;
2921         int ret;
2922
2923         slave = slave_find_by_pid(pid);
2924         if (!slave) {
2925                 ErrPrint("Failed to find a slave\n");
2926                 id = "";
2927                 ret = -ENOENT;
2928                 goto out;
2929         }
2930
2931         ret = packet_get(packet, "isssiii", &target, &slavename, &pkgname, &id, &w, &h, &pixel_size);
2932         if (ret != 7) {
2933                 ErrPrint("Invalid argument\n");
2934                 id = "";
2935                 ret = -EINVAL;
2936                 goto out;
2937         }
2938
2939         if (util_free_space(IMAGE_PATH) < MINIMUM_SPACE) {
2940                 DbgPrint("No space\n");
2941                 ret = -ENOSPC;
2942                 id = "";
2943                 goto out;
2944         }
2945
2946         /* TODO: */
2947         inst = package_find_instance_by_id(pkgname, id);
2948         if (!inst) {
2949                 DbgPrint("Package[%s] Id[%s] is not found\n", pkgname, id);
2950                 ret = -EINVAL;
2951                 id = "";
2952                 goto out;
2953         }
2954
2955         pkg = instance_package(inst);
2956         id = "";
2957         ret = -EINVAL;
2958         if (target == TYPE_LB) {
2959                 if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
2960                         struct buffer_info *info;
2961
2962                         info = instance_lb_buffer(inst);
2963                         if (!info) {
2964                                 if (!instance_create_lb_buffer(inst)) {
2965                                         ErrPrint("Failed to create a LB buffer\n");
2966                                 } else {
2967                                         info = instance_lb_buffer(inst);
2968                                         if (!info) {
2969                                                 ErrPrint("LB buffer is not valid\n");
2970                                                 ret = -EINVAL;
2971                                                 id = "";
2972                                                 goto out;
2973                                         }
2974                                 }
2975                         }
2976
2977                         ret = buffer_handler_resize(info, w, h);
2978                         DbgPrint("Buffer resize returns %d\n", ret);
2979
2980                         ret = buffer_handler_load(info);
2981                         if (ret == 0) {
2982                                 id = buffer_handler_id(info);
2983                                 DbgPrint("Buffer handler ID: %s\n", id);
2984                         } else {
2985                                 DbgPrint("Failed to load a buffer(%d)\n", ret);
2986                         }
2987                 }
2988         } else if (target == TYPE_PD) {
2989                 if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
2990                         struct buffer_info *info;
2991
2992                         DbgPrint("Slave acquire buffer for PD\n");
2993
2994                         info = instance_pd_buffer(inst);
2995                         if (!info) {
2996                                 if (!instance_create_pd_buffer(inst)) {
2997                                         ErrPrint("Failed to create a PD buffer\n");
2998                                 } else {
2999                                         info = instance_pd_buffer(inst);
3000                                         if (!info) {
3001                                                 ErrPrint("PD buffer is not valid\n");
3002                                                 ret = -EINVAL;
3003                                                 id = "";
3004                                                 instance_client_pd_created(inst, ret);
3005                                                 goto out;
3006                                         }
3007                                 }
3008                         }
3009
3010                         ret = buffer_handler_resize(info, w, h);
3011                         DbgPrint("Buffer resize returns %d\n", ret);
3012
3013                         ret = buffer_handler_load(info);
3014                         if (ret == 0) {
3015                                 id = buffer_handler_id(info);
3016                                 DbgPrint("Buffer handler ID: %s\n", id);
3017                         } else {
3018                                 DbgPrint("Failed to load a buffer (%d)\n", ret);
3019                         }
3020
3021                         /*!
3022                          * Send the PD created event to the client
3023                          */
3024                         instance_client_pd_created(inst, ret);
3025                 }
3026         }
3027
3028 out:
3029         result = packet_create_reply(packet, "is", ret, id);
3030         if (!result)
3031                 ErrPrint("Failed to create a packet\n");
3032
3033         return result;
3034 }
3035
3036 static struct packet *slave_resize_buffer(pid_t pid, int handle, const struct packet *packet)
3037 {
3038         struct slave_node *slave;
3039         struct packet *result;
3040         enum target_type type;
3041         const char *slavename;
3042         const char *pkgname;
3043         const char *id;
3044         int w;
3045         int h;
3046         struct inst_info *inst;
3047         const struct pkg_info *pkg;
3048         int ret;
3049
3050         slave = slave_find_by_pid(pid);
3051         if (!slave) {
3052                 ErrPrint("Failed to find a slave\n");
3053                 ret = -ENOENT;
3054                 id = "";
3055                 goto out;
3056         }
3057
3058         if (util_free_space(IMAGE_PATH) < MINIMUM_SPACE) {
3059                 ErrPrint("Not enough space\n");
3060                 ret = -ENOSPC;
3061                 id = "";
3062                 goto out;
3063         }
3064
3065         ret = packet_get(packet, "isssii", &type, &slavename, &pkgname, &id, &w, &h);
3066         if (ret != 6) {
3067                 ErrPrint("Invalid argument\n");
3068                 ret = -EINVAL;
3069                 id = "";
3070                 goto out;
3071         }
3072
3073         inst = package_find_instance_by_id(pkgname, id);
3074         if (!inst) {
3075                 DbgPrint("Instance is not found[%s] [%s]\n", pkgname, id);
3076                 ret = -ENOENT;
3077                 id = "";
3078                 goto out;
3079         }
3080
3081         pkg = instance_package(inst);
3082         if (!pkg) {
3083                 /*!
3084                  * \note
3085                  * THIS statement should not be entered.
3086                  */
3087                 ErrPrint("PACKAGE INFORMATION IS NOT VALID\n");
3088                 ret = -EFAULT;
3089                 id = "";
3090                 goto out;
3091         }
3092
3093         ret = -EINVAL;
3094         /*!
3095          * \note
3096          * Reset "id", It will be re-used from here
3097          */
3098         id = "";
3099         if (type == TYPE_LB) {
3100                 if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
3101                         struct buffer_info *info;
3102                         info = instance_lb_buffer(inst);
3103                         ret = buffer_handler_resize(info, w, h);
3104                         /*!
3105                          * \note
3106                          * id is resued for newly assigned ID
3107                          */
3108                         if (!ret)
3109                                 id = buffer_handler_id(info);
3110                 }
3111         } else if (type == TYPE_PD) {
3112                 if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
3113                         struct buffer_info *info;
3114                         info = instance_pd_buffer(inst);
3115                         ret = buffer_handler_resize(info, w, h);
3116                         /*!
3117                          * \note
3118                          * id is resued for newly assigned ID
3119                          */
3120                         if (!ret)
3121                                 id = buffer_handler_id(info);
3122                 }
3123         }
3124
3125 out:
3126         result = packet_create_reply(packet, "is", ret, id);
3127         if (!result)
3128                 ErrPrint("Failed to create a packet\n");
3129
3130         return result;
3131 }
3132
3133 static struct packet *slave_release_buffer(pid_t pid, int handle, const struct packet *packet)
3134 {
3135         enum target_type type;
3136         const char *slavename;
3137         const char *pkgname;
3138         const char *id;
3139         struct packet *result;
3140         struct slave_node *slave;
3141         struct inst_info *inst;
3142         int ret;
3143
3144         slave = slave_find_by_pid(pid);
3145         if (!slave) {
3146                 ErrPrint("Failed to find a slave\n");
3147                 ret = -ENOENT;
3148                 goto out;
3149         }
3150
3151         if (packet_get(packet, "isss", &type, &slavename, &pkgname, &id) != 4) {
3152                 ErrPrint("Inavlid argument\n");
3153                 ret = -EINVAL;
3154                 goto out;
3155         }
3156
3157         inst = package_find_instance_by_id(pkgname, id);
3158         if (!inst) {
3159                 ErrPrint("Instance is not found [%s - %s]\n", pkgname, id);
3160                 ret = -ENOENT;
3161                 goto out;
3162         }
3163
3164         ret = -EINVAL;
3165         if (type == TYPE_LB) {
3166                 struct buffer_info *info;
3167
3168                 info = instance_lb_buffer(inst);
3169                 ret = buffer_handler_unload(info);
3170         } else if (type == TYPE_PD) {
3171                 struct buffer_info *info;
3172
3173                 DbgPrint("Slave release buffer for PD\n");
3174
3175                 info = instance_pd_buffer(inst);
3176                 ret = buffer_handler_unload(info);
3177
3178                 /*!
3179                  * \note
3180                  * Send the PD destroyed event to the client
3181                  */
3182                 instance_client_pd_destroyed(inst, ret);
3183         }
3184
3185 out:
3186         result = packet_create_reply(packet, "i", ret);
3187         if (!result)
3188                 ErrPrint("Failed to create a packet\n");
3189
3190         return result;
3191 }
3192
3193 static struct packet *service_update(pid_t pid, int handle, const struct packet *packet)
3194 {
3195         struct packet *result;
3196         const char *pkgname;
3197         const char *cluster;
3198         const char *category;
3199         char *lb_pkgname;
3200         int ret;
3201
3202         ret = packet_get(packet, "sss", &pkgname, &cluster, &category);
3203         if (ret != 3) {
3204                 ErrPrint("Invalid Packet\n");
3205                 ret = -EINVAL;
3206                 goto out;
3207         }
3208
3209         lb_pkgname = package_lb_pkgname(pkgname);
3210         if (!lb_pkgname) {
3211                 ErrPrint("Invalid package %s\n", pkgname);
3212                 ret = -EINVAL;
3213                 goto out;
3214         }
3215
3216         /*!
3217          * \TODO
3218          * Validate the update requstor.
3219          */
3220         slave_rpc_request_update(lb_pkgname, "", cluster, category);
3221         DbgFree(lb_pkgname);
3222         ret = 0;
3223
3224 out:
3225         result = packet_create_reply(packet, "i", ret);
3226         if (!result)
3227                 ErrPrint("Failed to create a packet\n");
3228
3229         return result;
3230 }
3231
3232 static struct packet *liveinfo_hello(pid_t pid, int handle, const struct packet *packet)
3233 {
3234         struct liveinfo *info;
3235         struct packet *result;
3236         int ret;
3237         const char *fifo_name;
3238         double timestamp;
3239
3240         DbgPrint("Request arrived from %d\n", pid);
3241
3242         if (packet_get(packet, "d", &timestamp) != 1) {
3243                 ErrPrint("Invalid packet\n");
3244                 fifo_name = "";
3245                 ret = -EINVAL;
3246                 goto out;
3247         }
3248
3249         info = liveinfo_create(pid, handle);
3250         if (!info) {
3251                 ErrPrint("Failed to create a liveinfo object\n");
3252                 fifo_name = "";
3253                 ret = -EINVAL;
3254                 goto out;
3255         }
3256
3257         ret = 0;
3258         fifo_name = liveinfo_filename(info);
3259         DbgPrint("FIFO Created: %s (Serve for %d)\n", fifo_name, pid);
3260
3261 out:
3262         result = packet_create_reply(packet, "si", fifo_name, ret);
3263         if (!result)
3264                 ErrPrint("Failed to create a result packet\n");
3265
3266         return result;
3267 }
3268
3269 static struct packet *liveinfo_slave_list(pid_t pid, int handle, const struct packet *packet)
3270 {
3271         Eina_List *l;
3272         Eina_List *list;
3273         struct liveinfo *info;
3274         struct slave_node *slave;
3275         FILE *fp;
3276         double timestamp;
3277
3278         if (packet_get(packet, "d", &timestamp) != 1) {
3279                 ErrPrint("Invalid argument\n");
3280                 goto out;
3281         }
3282
3283         info = liveinfo_find_by_pid(pid);
3284         if (!info) {
3285                 ErrPrint("Invalid request\n");
3286                 goto out;
3287         }
3288
3289         liveinfo_open_fifo(info);
3290
3291         fp = liveinfo_fifo(info);
3292         if (!fp)
3293                 goto out;
3294
3295         fprintf(fp, "----------------------------------------------------------------------[Slave List]------------------------------------------------------------------------------\n");
3296         fprintf(fp, "    pid          slave name                     package name                   abi     secured   refcnt   fault           state           inst   pkg     ttl    \n");
3297         fprintf(fp, "----------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
3298         list = (Eina_List *)slave_list();
3299         EINA_LIST_FOREACH(list, l, slave) {
3300                 fprintf(fp, "  %7d   %20s   %39s   %7s   %7s   %6d   %5d   %21s   %4d   %3d   %3.4lf  \n", 
3301                         slave_pid(slave),
3302                         slave_name(slave),
3303                         slave_pkgname(slave),
3304                         slave_abi(slave),
3305                         slave_is_secured(slave) ? "true" : "false",
3306                         slave_refcnt(slave),
3307                         slave_fault_count(slave),
3308                         slave_state_string(slave),
3309                         slave_loaded_instance(slave),
3310                         slave_loaded_package(slave),
3311                         slave_ttl(slave)
3312                 );
3313         }
3314         liveinfo_close_fifo(info);
3315
3316 out:
3317         return NULL;
3318 }
3319
3320 static struct packet *liveinfo_slave_load(pid_t pid, int handle, const struct packet *packet)
3321 {
3322         struct liveinfo *info;
3323         pid_t slave_pid;
3324         struct slave_node *slave;
3325         struct pkg_info *pkg;
3326         Eina_List *pkg_list;
3327         Eina_List *l;
3328         FILE *fp;
3329
3330         if (packet_get(packet, "i", &slave_pid) != 1) {
3331                 ErrPrint("Invalid argument\n");
3332                 goto out;
3333         }
3334
3335         info = liveinfo_find_by_pid(pid);
3336         if (!info) {
3337                 ErrPrint("Invalid request\n");
3338                 goto out;
3339         }
3340
3341         slave = slave_find_by_pid(slave_pid);
3342         if (!slave) {
3343                 ErrPrint("Slave is not exists\n");
3344                 goto out;
3345         }
3346
3347         liveinfo_open_fifo(info);
3348         fp = liveinfo_fifo(info);
3349         if (!fp)
3350                 goto out;
3351
3352         fprintf(fp, "%s = { ", slave_name(slave));
3353         pkg_list = (Eina_List *)package_list();
3354         EINA_LIST_FOREACH(pkg_list, l, pkg) {
3355                 if (package_slave(pkg) == slave)
3356                         fprintf(fp, "%s, ", package_name(pkg));
3357         }
3358         fprintf(fp, "}\n");
3359
3360         liveinfo_close_fifo(info);
3361
3362 out:
3363         return NULL;
3364 }
3365
3366 static inline const char *visible_state_string(enum livebox_visible_state state)
3367 {
3368         switch (state) {
3369         case LB_SHOW:
3370                 return "Show";
3371         case LB_HIDE:
3372                 return "Hide";
3373         case LB_HIDE_WITH_PAUSE:
3374                 return "Paused";
3375         default:
3376                 break;
3377         }
3378
3379         return "Unknown";
3380 }
3381
3382 static struct packet *liveinfo_inst_list(pid_t pid, int handle, const struct packet *packet)
3383 {
3384         const char *pkgname;
3385         struct liveinfo *info;
3386         struct pkg_info *pkg;
3387         Eina_List *l;
3388         Eina_List *inst_list;
3389         struct inst_info *inst;
3390         FILE *fp;
3391
3392         if (packet_get(packet, "s", &pkgname) != 1) {
3393                 ErrPrint("Invalid argument\n");
3394                 goto out;
3395         }
3396
3397         if (!package_is_lb_pkgname(pkgname)) {
3398                 ErrPrint("Invalid package name\n");
3399                 goto out;
3400         }
3401
3402         pkg = package_find(pkgname);
3403         if (!pkg) {
3404                 ErrPrint("Package is not exists\n");
3405                 goto out;
3406         }
3407
3408         info = liveinfo_find_by_pid(pid);
3409         if (!info) {
3410                 ErrPrint("Invalid request\n");
3411                 goto out;
3412         }
3413
3414         liveinfo_open_fifo(info);
3415
3416         fp = liveinfo_fifo(info);
3417         if (!fp) {
3418                 ErrPrint("Invalid fp\n");
3419                 goto out;
3420         }
3421
3422         fprintf(fp, "-----------------------------------------------[Instance List]---------------------------------------\n");
3423         fprintf(fp, "         ID         |      Cluster ID    |   Sub cluster ID   | Period | Visibility | Width | Height \n");
3424         fprintf(fp, "-----------------------------------------------------------------------------------------------------\n");
3425
3426         inst_list = package_instance_list(pkg);
3427         EINA_LIST_FOREACH(inst_list, l, inst) {
3428                 fprintf(fp, " %18s %18s %18s %3.3lf %10s %5d %6d\n",
3429                                                 instance_id(inst),
3430                                                 instance_cluster(inst),
3431                                                 instance_category(inst),
3432                                                 instance_period(inst),
3433                                                 visible_state_string(instance_visible_state(inst)),
3434                                                 instance_lb_width(inst),
3435                                                 instance_lb_height(inst));
3436         }
3437
3438         liveinfo_close_fifo(info);
3439
3440 out:
3441         return NULL;
3442 }
3443
3444 static struct packet *liveinfo_pkg_list(pid_t pid, int handle, const struct packet *packet)
3445 {
3446         Eina_List *l;
3447         Eina_List *list;
3448         Eina_List *inst_list;
3449         struct liveinfo *info;
3450         struct pkg_info *pkg;
3451         struct slave_node *slave;
3452         FILE *fp;
3453         const char *slavename;
3454         double timestamp;
3455
3456         if (packet_get(packet, "d", &timestamp) != 1) {
3457                 ErrPrint("Invalid argument\n");
3458                 goto out;
3459         }
3460
3461         info = liveinfo_find_by_pid(pid);
3462         if (!info) {
3463                 ErrPrint("Invalid request\n");
3464                 goto out;
3465         }
3466
3467         liveinfo_open_fifo(info);
3468
3469         fp = liveinfo_fifo(info);
3470         if (!fp)
3471                 goto out;
3472
3473         fprintf(fp, "+----------------------------------------------[Package List]------------------------------------------------+\n");
3474         fprintf(fp, "    pid          slave name                     package name                   abi     refcnt   fault   inst  \n");
3475         fprintf(fp, "+------------------------------------------------------------------------------------------------------------+\n");
3476         list = (Eina_List *)package_list();
3477         EINA_LIST_FOREACH(list, l, pkg) {
3478                 slave = package_slave(pkg);
3479
3480                 if (slave) {
3481                         slavename = slave_name(slave);
3482                         pid = slave_pid(slave);
3483                 } else {
3484                         pid = (pid_t)-1;
3485                         slavename = "";
3486                 }
3487
3488                 inst_list = (Eina_List *)package_instance_list(pkg);
3489
3490                 fprintf(fp, "  %7d   %20s   %39s   %7s   %6d   %5d   %4d  \n",
3491                         pid,
3492                         slavename,
3493                         package_name(pkg),
3494                         package_abi(pkg),
3495                         package_refcnt(pkg),
3496                         package_fault_count(pkg),
3497                         eina_list_count(inst_list)
3498                 );
3499         }
3500         liveinfo_close_fifo(info);
3501
3502 out:
3503         return NULL;
3504 }
3505
3506 static struct packet *liveinfo_slave_ctrl(pid_t pid, int handle, const struct packet *packet)
3507 {
3508         return NULL;
3509 }
3510
3511 static struct packet *liveinfo_pkg_ctrl(pid_t pid, int handle, const struct packet *packet)
3512 {
3513         return NULL;
3514 }
3515
3516 static struct method s_info_table[] = {
3517         {
3518                 .cmd = "liveinfo_hello",
3519                 .handler = liveinfo_hello,
3520         },
3521         {
3522                 .cmd = "slave_list",
3523                 .handler = liveinfo_slave_list,
3524         },
3525         {
3526                 .cmd = "pkg_list",
3527                 .handler = liveinfo_pkg_list,
3528         },
3529         {
3530                 .cmd = "inst_list",
3531                 .handler = liveinfo_inst_list,
3532         },
3533         {
3534                 .cmd = "slave_load",
3535                 .handler = liveinfo_slave_load,
3536         },
3537         {
3538                 .cmd = "slave_ctrl",
3539                 .handler = liveinfo_slave_ctrl,
3540         },
3541         {
3542                 .cmd = "pkg_ctrl",
3543                 .handler = liveinfo_pkg_ctrl,
3544         },
3545         {
3546                 .cmd = NULL,
3547                 .handler = NULL,
3548         },
3549 };
3550
3551 static struct method s_client_table[] = {
3552         {
3553                 .cmd = "pd_mouse_move",
3554                 .handler = client_pd_mouse_move, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3555         },
3556         {
3557                 .cmd = "lb_mouse_move",
3558                 .handler = client_lb_mouse_move, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3559         },
3560         {
3561                 .cmd = "pd_mouse_down",
3562                 .handler = client_pd_mouse_down, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
3563         },
3564         {
3565                 .cmd = "pd_mouse_up",
3566                 .handler = client_pd_mouse_up, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3567         },
3568         {
3569                 .cmd = "lb_mouse_down",
3570                 .handler = client_lb_mouse_down, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3571         },
3572         {
3573                 .cmd = "lb_mouse_up",
3574                 .handler = client_lb_mouse_up, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3575         },
3576         {
3577                 .cmd = "pd_mouse_enter",
3578                 .handler = client_pd_mouse_enter, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
3579         },
3580         {
3581                 .cmd = "pd_mouse_leave",
3582                 .handler = client_pd_mouse_leave, /* pid, pkgname, id, width, height, timestamp, x, y, ret */
3583         },
3584         {
3585                 .cmd = "lb_mouse_enter",
3586                 .handler = client_lb_mouse_enter, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3587         },
3588         {
3589                 .cmd = "lb_mouse_leave",
3590                 .handler = client_lb_mouse_leave, /* pid, pkgname, filename, width, height, timestamp, x, y, ret */
3591         },
3592         {
3593                 .cmd = "change,visibility",
3594                 .handler = client_change_visibility,
3595         },
3596         {
3597                 .cmd = "lb_acquire_pixmap",
3598                 .handler = client_lb_acquire_pixmap,
3599         },
3600         {
3601                 .cmd = "lb_release_pixmap",
3602                 .handler = client_lb_release_pixmap,
3603         },
3604         {
3605                 .cmd = "pd_acquire_pixmap",
3606                 .handler = client_pd_acquire_pixmap,
3607         },
3608         {
3609                 .cmd = "pd_release_pixmap",
3610                 .handler = client_pd_release_pixmap,
3611         },
3612         {
3613                 .cmd = "acquire",
3614                 .handler = client_acquire, /*!< pid, ret */
3615         },
3616         {
3617                 .cmd = "release",
3618                 .handler = cilent_release, /*!< pid, ret */
3619         },
3620         {
3621                 .cmd = "clicked",
3622                 .handler = client_clicked, /*!< pid, pkgname, filename, event, timestamp, x, y, ret */
3623         },
3624         {
3625                 .cmd = "text_signal",
3626                 .handler = client_text_signal, /* pid, pkgname, filename, emission, source, s, sy, ex, ey, ret */
3627         },
3628         {
3629                 .cmd = "delete",
3630                 .handler = client_delete, /* pid, pkgname, filename, ret */
3631         },
3632         {
3633                 .cmd = "resize",
3634                 .handler = client_resize, /* pid, pkgname, filename, w, h, ret */
3635         },
3636         {
3637                 .cmd = "new",
3638                 .handler = client_new, /* pid, timestamp, pkgname, content, cluster, category, period, ret */
3639         },
3640         {
3641                 .cmd = "set_period",
3642                 .handler = client_set_period, /* pid, pkgname, filename, period, ret, period */
3643         },
3644         {
3645                 .cmd = "change_group",
3646                 .handler = client_change_group, /* pid, pkgname, filename, cluster, category, ret */
3647         },
3648         {
3649                 .cmd = "pinup_changed",
3650                 .handler = client_pinup_changed, /* pid, pkgname, filename, pinup, ret */
3651         },
3652         {
3653                 .cmd = "create_pd",
3654                 .handler = client_create_pd, /* pid, pkgname, filename, ret */
3655         },
3656         {
3657                 .cmd = "destroy_pd",
3658                 .handler = client_destroy_pd, /* pid, pkgname, filename, ret */
3659         },
3660         {
3661                 .cmd = "activate_package",
3662                 .handler = client_activate_package, /* pid, pkgname, ret */
3663         },
3664         {
3665                 .cmd = "subscribe", /* pid, cluster, sub-cluster */
3666                 .handler = client_subscribed,
3667         },
3668         {
3669                 .cmd = "unsubscribe", /* pid, cluster, sub-cluster */
3670                 .handler = client_unsubscribed,
3671         },
3672         {
3673                 .cmd = "delete_cluster",
3674                 .handler = client_delete_cluster,
3675         },
3676         {
3677                 .cmd = "delete_category",
3678                 .handler = client_delete_category,
3679         },
3680         {
3681                 .cmd = "refresh_group",
3682                 .handler = client_refresh_group,
3683         },
3684         {
3685                 .cmd = NULL,
3686                 .handler = NULL,
3687         },
3688 };
3689
3690 static struct method s_service_table[] = {
3691         {
3692                 .cmd = "service_update",
3693                 .handler = service_update,
3694         },
3695         {
3696                 .cmd = NULL,
3697                 .handler = NULL,
3698         },
3699 };
3700
3701 static struct method s_slave_table[] = {
3702         {
3703                 .cmd = "hello",
3704                 .handler = slave_hello, /* slave_name, ret */
3705         },
3706         {
3707                 .cmd = "ping",
3708                 .handler = slave_ping, /* slave_name, ret */
3709         },
3710         {
3711                 .cmd = "call",
3712                 .handler = slave_call, /* slave_name, pkgname, filename, function, ret */
3713         },
3714         {
3715                 .cmd = "ret",
3716                 .handler = slave_ret, /* slave_name, pkgname, filename, function, ret */
3717         },
3718         {
3719                 .cmd = "updated",
3720                 .handler = slave_updated, /* slave_name, pkgname, filename, width, height, priority, ret */
3721         },
3722         {
3723                 .cmd = "desc_updated",
3724                 .handler = slave_desc_updated, /* slave_name, pkgname, filename, decsfile, ret */
3725         },
3726         {
3727                 .cmd = "deleted",
3728                 .handler = slave_deleted, /* slave_name, pkgname, filename, ret */
3729         },
3730         {
3731                 .cmd = "acquire_buffer",
3732                 .handler = slave_acquire_buffer, /* slave_name, id, w, h, size, - out - type, shmid */
3733         },
3734         {
3735                 .cmd = "resize_buffer",
3736                 .handler = slave_resize_buffer,
3737         },
3738         {
3739                 .cmd = "release_buffer",
3740                 .handler = slave_release_buffer, /* slave_name, id - ret */
3741         },
3742         {
3743                 .cmd = "faulted",
3744                 .handler = slave_faulted, /* slave_name, pkgname, id, funcname */
3745         },
3746         {
3747                 .cmd = NULL,
3748                 .handler = NULL,
3749         },
3750 };
3751
3752 HAPI int server_init(void)
3753 {
3754         com_core_packet_use_thread(COM_CORE_THREAD);
3755
3756         if (unlink(INFO_SOCKET) < 0)
3757                 ErrPrint("info socket: %s\n", strerror(errno));
3758
3759         if (unlink(SLAVE_SOCKET) < 0)
3760                 ErrPrint("slave socket: %s\n", strerror(errno));
3761
3762         if (unlink(CLIENT_SOCKET) < 0)
3763                 ErrPrint("client socket: %s\n", strerror(errno));
3764
3765         if (unlink(SERVICE_SOCKET) < 0)
3766                 ErrPrint("service socket: %s\n", strerror(errno));
3767
3768         s_info.info_fd = com_core_packet_server_init(INFO_SOCKET, s_info_table);
3769         if (s_info.info_fd < 0)
3770                 ErrPrint("Failed to create a info socket\n");
3771
3772         s_info.slave_fd = com_core_packet_server_init(SLAVE_SOCKET, s_slave_table);
3773         if (s_info.slave_fd < 0)
3774                 ErrPrint("Failed to create a slave socket\n");
3775
3776         s_info.client_fd = com_core_packet_server_init(CLIENT_SOCKET, s_client_table);
3777         if (s_info.client_fd < 0)
3778                 ErrPrint("Failed to create a client socket\n");
3779
3780         s_info.service_fd = com_core_packet_server_init(SERVICE_SOCKET, s_service_table);
3781         if (s_info.service_fd < 0)
3782                 ErrPrint("Faild to create a service socket\n");
3783
3784         if (chmod(INFO_SOCKET, 0600) < 0)
3785                 ErrPrint("info socket: %s\n", strerror(errno));
3786
3787         if (chmod(SLAVE_SOCKET, 0666) < 0)
3788                 ErrPrint("slave socket: %s\n", strerror(errno));
3789
3790         if (chmod(CLIENT_SOCKET, 0666) < 0)
3791                 ErrPrint("client socket: %s\n", strerror(errno));
3792
3793         if (chmod(SERVICE_SOCKET, 0666) < 0)
3794                 ErrPrint("service socket: %s\n", strerror(errno));
3795
3796         return 0;
3797 }
3798
3799 HAPI int server_fini(void)
3800 {
3801         if (s_info.info_fd > 0) {
3802                 com_core_packet_server_fini(s_info.info_fd);
3803                 s_info.info_fd = -1;
3804         }
3805
3806         if (s_info.slave_fd > 0) {
3807                 com_core_packet_server_fini(s_info.slave_fd);
3808                 s_info.slave_fd = -1;
3809         }
3810
3811         if (s_info.client_fd > 0) {
3812                 com_core_packet_server_fini(s_info.client_fd);
3813                 s_info.client_fd = -1;
3814         }
3815
3816         if (s_info.service_fd > 0) {
3817                 com_core_packet_server_fini(s_info.service_fd);
3818                 s_info.service_fd = -1;
3819         }
3820
3821         return 0;
3822 }
3823
3824 /* End of a file */