Update SMACK, Fix a crash of terminating sequence, etc, ...
[platform/framework/web/data-provider-master.git] / src / instance.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <errno.h>
20
21 #include <dlog.h>
22 #include <Ecore_Evas.h>
23 #include <Eina.h>
24 #include <gio/gio.h>
25 #include <Ecore.h>
26
27 #include <packet.h>
28 #include <com-core_packet.h>
29 #include <livebox-service.h>
30 #include <livebox-errno.h>
31
32 #include "conf.h"
33 #include "util.h"
34 #include "debug.h"
35 #include "slave_life.h"
36 #include "slave_rpc.h"
37 #include "client_life.h"
38 #include "instance.h"
39 #include "client_rpc.h"
40 #include "package.h"
41 #include "script_handler.h"
42 #include "buffer_handler.h"
43 #include "fb.h"
44 #include "setting.h"
45
46 int errno;
47
48 static struct info {
49         enum buffer_type env_buf_type;
50 } s_info = {
51         .env_buf_type = BUFFER_TYPE_FILE,
52 };
53
54 struct set_pinup_cbdata {
55         struct inst_info *inst;
56         int pinup;
57 };
58
59 struct resize_cbdata {
60         struct inst_info *inst;
61         int w;
62         int h;
63 };
64
65 struct update_mode_cbdata {
66         struct inst_info *inst;
67         int active_update;
68 };
69
70 struct change_group_cbdata {
71         struct inst_info *inst;
72         char *cluster;
73         char *category;
74 };
75
76 struct period_cbdata {
77         struct inst_info *inst;
78         double period;
79 };
80
81 struct event_item {
82         int (*event_cb)(struct inst_info *inst, void *data);
83         void *data;
84 };
85
86 struct tag_item {
87         char *tag;
88         void *data;
89 };
90
91 struct inst_info {
92         struct pkg_info *info;
93
94         enum instance_state state; /*!< Represents current state */
95         enum instance_state requested_state; /*!< Only ACTIVATED | DESTROYED is acceptable */
96         int changing_state;
97
98         char *id;
99         double timestamp;
100
101         char *content;
102         char *cluster;
103         char *category;
104         char *title;
105         int is_pinned_up;
106         double sleep_at;
107         int scroll_locked; /*!< Scroller which is in viewer is locked. */
108         int active_update; /*!< Viewer will reload the buffer by itself, so the provider doesn't need to send the updated event */
109
110         enum livebox_visible_state visible;
111
112         struct {
113                 int width;
114                 int height;
115                 double priority;
116
117                 union {
118                         struct script_info *script;
119                         struct buffer_info *buffer;
120                 } canvas;
121
122                 const char *auto_launch;
123                 int timeout;
124                 double period;
125         } lb;
126
127         struct {
128                 int width;
129                 int height;
130                 double x;
131                 double y;
132
133                 union {
134                         struct script_info *script;
135                         struct buffer_info *buffer;
136                 } canvas;
137
138                 struct client_node *owner;
139                 int is_opened_for_reactivate;
140                 int need_to_send_close_event;
141                 char *pended_update_desc;
142                 int pended_update_cnt;
143         } pd;
144
145         struct client_node *client; /*!< Owner - creator */
146         Eina_List *client_list; /*!< Viewer list */
147         int refcnt;
148
149         Ecore_Timer *update_timer; /*!< Only used for secured livebox */
150
151         Eina_List *delete_event_list;
152
153         Eina_List *data_list;
154 };
155
156 #define CLIENT_SEND_EVENT(instance, packet)     ((instance)->client ? client_rpc_async_request((instance)->client, (packet)) : client_broadcast((instance), (packet)))
157
158 static Eina_Bool update_timer_cb(void *data);
159
160 static inline void timer_thaw(struct inst_info *inst)
161 {
162         double pending;
163         double period;
164         double delay;
165         double sleep_time;
166
167         ecore_timer_thaw(inst->update_timer);
168         period = ecore_timer_interval_get(inst->update_timer);
169         pending = ecore_timer_pending_get(inst->update_timer);
170         delay = util_time_delay_for_compensation(period) - pending;
171         ecore_timer_delay(inst->update_timer, delay);
172         DbgPrint("Compensated: %lf\n", delay);
173
174         if (inst->sleep_at == 0.0f)
175                 return;
176
177         sleep_time = util_timestamp() - inst->sleep_at;
178         if (sleep_time > pending) {
179                 DbgPrint("Update time elapsed\n");
180                 (void)update_timer_cb(inst);
181         }
182
183         inst->sleep_at = 0.0f;
184 }
185
186 static inline void timer_freeze(struct inst_info *inst)
187 {
188         struct timeval tv;
189         ecore_timer_freeze(inst->update_timer);
190
191         if (ecore_timer_interval_get(inst->update_timer) <= 1.0f)
192                 return;
193
194         if (gettimeofday(&tv, NULL) < 0) {
195                 ErrPrint("gettimeofday: %s\n", strerror(errno));
196                 tv.tv_sec = 0;
197                 tv.tv_usec = 0;
198         }
199         inst->sleep_at = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
200 }
201
202
203 static int viewer_deactivated_cb(struct client_node *client, void *data)
204 {
205         struct inst_info *inst = data;
206
207         DbgPrint("%d is deleted from the list of viewer of %s(%s)\n", client_pid(client), package_name(instance_package(inst)), instance_id(inst));
208         if (!eina_list_data_find(inst->client_list, client)) {
209                 DbgPrint("Not found\n");
210                 return LB_STATUS_ERROR_NOT_EXIST;
211         }
212
213         inst->client_list = eina_list_remove(inst->client_list, client);
214         if (!inst->client_list && !inst->client) {
215                 DbgPrint("Has no clients\n");
216                 instance_destroy(inst);
217         }
218
219         instance_unref(inst);
220         return -1; /*!< Remove this callback from the cb list */
221 }
222
223 static inline int pause_livebox(struct inst_info *inst)
224 {
225         struct packet *packet;
226
227         packet = packet_create_noack("lb_pause", "ss", package_name(inst->info), inst->id);
228         if (!packet) {
229                 ErrPrint("Failed to create a new packet\n");
230                 return LB_STATUS_ERROR_FAULT;
231         }
232
233         return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
234 }
235
236 /*! \TODO Wake up the freeze'd timer */
237 static inline int resume_livebox(struct inst_info *inst)
238 {
239         struct packet *packet;
240
241         packet = packet_create_noack("lb_resume", "ss", package_name(inst->info), inst->id);
242         if (!packet) {
243                 ErrPrint("Failed to create a new packet\n");
244                 return LB_STATUS_ERROR_FAULT;
245         }
246
247         return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
248 }
249
250 static inline int instance_recover_visible_state(struct inst_info *inst)
251 {
252         int ret;
253
254         switch (inst->visible) {
255         case LB_SHOW:
256         case LB_HIDE:
257                 instance_thaw_updator(inst);
258
259                 ret = 0;
260                 break;
261         case LB_HIDE_WITH_PAUSE:
262                 ret = pause_livebox(inst);
263
264                 instance_freeze_updator(inst);
265                 break;
266         default:
267                 ret = LB_STATUS_ERROR_INVALID;
268                 break;
269         }
270
271         DbgPrint("Visible state is recovered to %d\n", ret);
272         return ret;
273 }
274
275 static inline void instance_send_update_mode_event(struct inst_info *inst, int active_mode, int status)
276 {
277         struct packet *packet;
278         const char *pkgname;
279
280         if (!inst->info) {
281                 ErrPrint("Instance info is not ready to use\n");
282                 return;
283         }
284
285         pkgname = package_name(inst->info);
286
287         packet = packet_create_noack("update_mode", "ssii", pkgname, inst->id, status, active_mode);
288         if (packet)
289                 CLIENT_SEND_EVENT(inst, packet);
290         else
291                 ErrPrint("Failed to send update mode event\n");
292 }
293
294 static inline void instance_send_resized_event(struct inst_info *inst, int is_pd, int w, int h, int status)
295 {
296         struct packet *packet;
297         enum lb_type lb_type;
298         const char *pkgname;
299         const char *id;
300
301         if (!inst->info) {
302                 ErrPrint("Instance info is not ready to use\n");
303                 return;
304         }
305
306         pkgname = package_name(inst->info);
307
308         lb_type = package_lb_type(inst->info);
309         if (lb_type == LB_TYPE_SCRIPT)
310                 id = fb_id(script_handler_fb(inst->lb.canvas.script));
311         else if (lb_type == LB_TYPE_BUFFER)
312                 id = buffer_handler_id(inst->lb.canvas.buffer);
313         else
314                 id = "";
315
316         DbgPrint("Size is changed to %dx%d (%s) %s\n", w, h, id, is_pd ? "pd" : "lb");
317
318         packet = packet_create_noack("size_changed", "sssiiii", pkgname, inst->id, id, is_pd, w, h, status);
319         if (packet)
320                 CLIENT_SEND_EVENT(inst, packet);
321         else
322                 ErrPrint("Failed to send size changed event\n");
323 }
324
325 static void update_mode_cb(struct slave_node *slave, const struct packet *packet, void *data)
326 {
327         struct update_mode_cbdata *cbdata = data;
328         int ret;
329
330         if (!packet) {
331                 ErrPrint("Invalid packet\n");
332                 instance_send_update_mode_event(cbdata->inst, cbdata->active_update, LB_STATUS_ERROR_FAULT);
333                 instance_unref(cbdata->inst);
334                 DbgFree(cbdata);
335                 return;
336         }
337
338         if (packet_get(packet, "i", &ret) != 1) {
339                 ErrPrint("Invalid parameters\n");
340                 instance_send_update_mode_event(cbdata->inst, cbdata->active_update, LB_STATUS_ERROR_INVALID);
341                 instance_unref(cbdata->inst);
342                 DbgFree(cbdata);
343                 return;
344         }
345
346         if (ret == LB_STATUS_SUCCESS)
347                 cbdata->inst->active_update = cbdata->active_update;
348
349         instance_send_update_mode_event(cbdata->inst, cbdata->active_update, ret);
350
351         instance_unref(cbdata->inst);
352         DbgFree(cbdata);
353 }
354
355 HAPI int instance_unicast_created_event(struct inst_info *inst, struct client_node *client)
356 {
357         struct packet *packet;
358         enum lb_type lb_type;
359         enum pd_type pd_type;
360         const char *lb_file;
361         const char *pd_file;
362
363         if (!client) {
364                 client = inst->client;
365                 if (!client)
366                         return LB_STATUS_SUCCESS;
367         }
368
369         lb_type = package_lb_type(inst->info);
370         pd_type = package_pd_type(inst->info);
371
372         if (lb_type == LB_TYPE_SCRIPT)
373                 lb_file = fb_id(script_handler_fb(inst->lb.canvas.script));
374         else if (lb_type == LB_TYPE_BUFFER)
375                 lb_file = buffer_handler_id(inst->lb.canvas.buffer);
376         else
377                 lb_file = "";
378
379         if (pd_type == PD_TYPE_SCRIPT)
380                 pd_file = fb_id(script_handler_fb(inst->pd.canvas.script));
381         else if (pd_type == PD_TYPE_BUFFER)
382                 pd_file = buffer_handler_id(inst->pd.canvas.buffer);
383         else
384                 pd_file = "";
385
386         packet = packet_create_noack("created", "dsssiiiisssssdiiiiidsi",
387                         inst->timestamp,
388                         package_name(inst->info), inst->id, inst->content,
389                         inst->lb.width, inst->lb.height,
390                         inst->pd.width, inst->pd.height,
391                         inst->cluster, inst->category,
392                         lb_file, pd_file,
393                         inst->lb.auto_launch,
394                         inst->lb.priority,
395                         package_size_list(inst->info),
396                         !!inst->client,
397                         package_pinup(inst->info),
398                         lb_type, pd_type,
399                         inst->lb.period, inst->title,
400                         inst->is_pinned_up);
401         if (!packet) {
402                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
403                 return LB_STATUS_ERROR_FAULT;
404         }
405
406         return client_rpc_async_request(client, packet);
407 }
408
409 static int update_client_list(struct client_node *client, void *data)
410 {
411         struct inst_info *inst = data;
412
413         if (!instance_has_client(inst, client))
414                 instance_add_client(inst, client);
415
416         return LB_STATUS_SUCCESS;
417 }
418
419 static int instance_broadcast_created_event(struct inst_info *inst)
420 {
421         struct packet *packet;
422         enum lb_type lb_type;
423         enum pd_type pd_type;
424         const char *lb_file;
425         const char *pd_file;
426
427         lb_type = package_lb_type(inst->info);
428         pd_type = package_pd_type(inst->info);
429
430         if (lb_type == LB_TYPE_SCRIPT)
431                 lb_file = fb_id(script_handler_fb(inst->lb.canvas.script));
432         else if (lb_type == LB_TYPE_BUFFER)
433                 lb_file = buffer_handler_id(inst->lb.canvas.buffer);
434         else
435                 lb_file = "";
436
437         if (pd_type == PD_TYPE_SCRIPT)
438                 pd_file = fb_id(script_handler_fb(inst->pd.canvas.script));
439         else if (pd_type == PD_TYPE_BUFFER)
440                 pd_file = buffer_handler_id(inst->pd.canvas.buffer);
441         else
442                 pd_file = "";
443
444         if (!inst->client)
445                 client_browse_list(inst->cluster, inst->category, update_client_list, inst);
446
447         packet = packet_create_noack("created", "dsssiiiisssssdiiiiidsi", 
448                         inst->timestamp,
449                         package_name(inst->info), inst->id, inst->content,
450                         inst->lb.width, inst->lb.height,
451                         inst->pd.width, inst->pd.height,
452                         inst->cluster, inst->category,
453                         lb_file, pd_file,
454                         inst->lb.auto_launch,
455                         inst->lb.priority,
456                         package_size_list(inst->info),
457                         !!inst->client,
458                         package_pinup(inst->info),
459                         lb_type, pd_type,
460                         inst->lb.period, inst->title,
461                         inst->is_pinned_up);
462         if (!packet) {
463                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
464                 return LB_STATUS_ERROR_FAULT;
465         }
466
467         return CLIENT_SEND_EVENT(inst, packet);
468 }
469
470 HAPI int instance_unicast_deleted_event(struct inst_info *inst, struct client_node *client)
471 {
472         struct packet *packet;
473
474         if (!client) {
475                 client = inst->client;
476                 if (!client)
477                         return LB_STATUS_ERROR_INVALID;
478         }
479
480         packet = packet_create_noack("deleted", "ssd", package_name(inst->info), inst->id, inst->timestamp);
481         if (!packet) {
482                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
483                 return LB_STATUS_ERROR_FAULT;
484         }
485                 
486         return client_rpc_async_request(client, packet);
487 }
488
489 static int instance_broadcast_deleted_event(struct inst_info *inst)
490 {
491         struct packet *packet;
492         struct client_node *client;
493         Eina_List *l;
494         Eina_List *n;
495         int ret;
496
497         packet = packet_create_noack("deleted", "ssd", package_name(inst->info), inst->id, inst->timestamp);
498         if (!packet) {
499                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
500                 return LB_STATUS_ERROR_FAULT;
501         }
502                 
503         ret = CLIENT_SEND_EVENT(inst, packet);
504
505         EINA_LIST_FOREACH_SAFE(inst->client_list, l, n, client) {
506                 instance_del_client(inst, client);
507         }
508
509         return ret;
510 }
511
512 static int client_deactivated_cb(struct client_node *client, void *data)
513 {
514         struct inst_info *inst = data;
515         instance_destroy(inst);
516         return LB_STATUS_SUCCESS;
517 }
518
519 static int send_pd_destroyed_to_client(struct inst_info *inst, int status)
520 {
521         struct packet *packet;
522
523         packet = packet_create_noack("pd_destroyed", "ssi", package_name(inst->info), inst->id, status);
524         if (!packet) {
525                 ErrPrint("Failed to create a packet\n");
526                 return LB_STATUS_ERROR_FAULT;
527         }
528
529         return CLIENT_SEND_EVENT(inst, packet);
530 }
531
532 static inline void invoke_delete_callbacks(struct inst_info *inst)
533 {
534         Eina_List *l;
535         Eina_List *n;
536         struct event_item *item;
537
538         EINA_LIST_FOREACH_SAFE(inst->delete_event_list, l, n, item) {
539                 if (item->event_cb(inst, item->data) < 0) {
540                         if (eina_list_data_find(inst->delete_event_list, item)) {
541                                 inst->delete_event_list = eina_list_remove(inst->delete_event_list, item);
542                                 free(item);
543                         }
544                 }
545         }
546 }
547
548 HAPI int instance_event_callback_add(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data), void *data)
549 {
550         struct event_item *item;
551
552         if (!event_cb)
553                 return LB_STATUS_ERROR_INVALID;
554
555         switch (type) {
556         case INSTANCE_EVENT_DESTROY:
557                 item = malloc(sizeof(*item));
558                 if (!item) {
559                         ErrPrint("Heap: %s\n", strerror(errno));
560                         return LB_STATUS_ERROR_MEMORY;
561                 }
562
563                 item->event_cb = event_cb;
564                 item->data = data;
565
566                 inst->delete_event_list = eina_list_append(inst->delete_event_list, item);
567                 break;
568         default:
569                 return LB_STATUS_ERROR_INVALID;
570         }
571
572         return LB_STATUS_SUCCESS;
573 }
574
575 HAPI int instance_event_callback_del(struct inst_info *inst, enum instance_event type, int (*event_cb)(struct inst_info *inst, void *data))
576 {
577         Eina_List *l;
578         Eina_List *n;
579         struct event_item *item;
580
581         switch (type) {
582         case INSTANCE_EVENT_DESTROY:
583                 EINA_LIST_FOREACH_SAFE(inst->delete_event_list, l, n, item) {
584                         if (item->event_cb == event_cb) {
585                                 inst->delete_event_list = eina_list_remove(inst->delete_event_list, item);
586                                 free(item);
587                                 return LB_STATUS_SUCCESS;
588                         }
589                 }
590                 break;
591         default:
592                 break;
593         }
594
595         return LB_STATUS_ERROR_NOT_EXIST;
596 }
597
598 static inline void destroy_instance(struct inst_info *inst)
599 {
600         struct pkg_info *pkg;
601         enum lb_type lb_type;
602         enum pd_type pd_type;
603         struct slave_node *slave;
604         struct event_item *item;
605         struct tag_item *tag_item;
606
607         invoke_delete_callbacks(inst);
608
609         pkg = inst->info;
610
611         lb_type = package_lb_type(pkg);
612         pd_type = package_pd_type(pkg);
613         slave = package_slave(inst->info);
614
615         DbgPrint("Instance is destroyed (%p), slave(%p)\n", inst, slave);
616
617         if (inst->pd.need_to_send_close_event)
618                 send_pd_destroyed_to_client(inst, 0);
619
620         if (lb_type == LB_TYPE_SCRIPT) {
621                 script_handler_unload(inst->lb.canvas.script, 0);
622                 script_handler_destroy(inst->lb.canvas.script);
623         } else if (lb_type == LB_TYPE_BUFFER) {
624                 buffer_handler_unload(inst->lb.canvas.buffer);
625                 buffer_handler_destroy(inst->lb.canvas.buffer);
626         }
627
628         if (pd_type == PD_TYPE_SCRIPT) {
629                 script_handler_unload(inst->pd.canvas.script, 1);
630                 script_handler_destroy(inst->pd.canvas.script);
631         } else if (pd_type == PD_TYPE_BUFFER) {
632                 buffer_handler_unload(inst->pd.canvas.buffer);
633                 buffer_handler_destroy(inst->pd.canvas.buffer);
634         }
635
636         if (inst->client) {
637                 client_event_callback_del(inst->client, CLIENT_EVENT_DEACTIVATE, client_deactivated_cb, inst);
638                 client_unref(inst->client);
639         }
640
641         if (inst->update_timer)
642                 ecore_timer_del(inst->update_timer);
643
644         EINA_LIST_FREE(inst->data_list, tag_item) {
645                 DbgPrint("Tagged item[%s] %p\n", tag_item->tag, tag_item->data);
646                 free(tag_item->tag);
647                 free(tag_item);
648         }
649
650         /*!
651          * \note
652          *
653          * inst->lb.auto_launch
654          *
655          * will be released by the package object
656          * it is readonly value for instances
657          */
658         EINA_LIST_FREE(inst->delete_event_list, item) {
659                 free(item);
660         }
661         DbgFree(inst->category);
662         DbgFree(inst->cluster);
663         DbgFree(inst->content);
664         DbgFree(inst->title);
665         util_unlink(util_uri_to_path(inst->id));
666         DbgFree(inst->id);
667         package_del_instance(inst->info, inst);
668         DbgFree(inst);
669
670         slave = slave_unload_instance(slave);
671 }
672
673 static Eina_Bool update_timer_cb(void *data)
674 {
675         struct inst_info *inst = (struct inst_info *)data;
676
677         DbgPrint("Update instance %s (%s) %s/%s\n", package_name(inst->info), inst->id, inst->cluster, inst->category);
678         slave_rpc_request_update(package_name(inst->info), inst->id, inst->cluster, inst->category);
679         return ECORE_CALLBACK_RENEW;
680 }
681
682 static inline int fork_package(struct inst_info *inst, const char *pkgname)
683 {
684         struct pkg_info *info;
685         int len;
686
687         info = package_find(pkgname);
688         if (!info) {
689                 ErrPrint("%s is not found\n", pkgname);
690                 return LB_STATUS_ERROR_NOT_EXIST;
691         }
692
693         len = strlen(SCHEMA_FILE "%s%s_%d_%lf.png") + strlen(IMAGE_PATH) + strlen(package_name(info)) + 50;
694         inst->id = malloc(len);
695         if (!inst->id) {
696                 ErrPrint("Heap: %s\n", strerror(errno));
697                 return LB_STATUS_ERROR_MEMORY;
698         }
699
700         snprintf(inst->id, len, SCHEMA_FILE "%s%s_%d_%lf.png", IMAGE_PATH, package_name(info), client_pid(inst->client), inst->timestamp);
701         inst->lb.auto_launch = package_auto_launch(info);
702
703         instance_set_pd_size(inst, package_pd_width(info), package_pd_height(info));
704
705         inst->lb.timeout = package_timeout(info);
706         inst->lb.period = package_period(info);
707
708         inst->info = info;
709
710         if (package_secured(info)) {
711                 if (inst->lb.period > 0.0f) {
712                         DbgPrint("Register the update timer for secured livebox [%s]\n", package_name(info));
713                         inst->update_timer = util_timer_add(inst->lb.period, update_timer_cb, inst);
714                         if (!inst->update_timer)
715                                 ErrPrint("Failed to add an update timer for instance %s\n", inst->id);
716                         else
717                                 timer_freeze(inst); /* Freeze the update timer as default */
718                 } else {
719                         DbgPrint("secured livebox [%s] has no period (%lf)\n", package_name(info), inst->lb.period);
720                         inst->update_timer = NULL;
721                 }
722         }
723
724         return LB_STATUS_SUCCESS;
725 }
726
727 HAPI struct inst_info *instance_create(struct client_node *client, double timestamp, const char *pkgname, const char *content, const char *cluster, const char *category, double period, int width, int height)
728 {
729         struct inst_info *inst;
730
731         inst = calloc(1, sizeof(*inst));
732         if (!inst) {
733                 ErrPrint("Heap: %s\n", strerror(errno));
734                 return NULL;
735         }
736
737         inst->timestamp = timestamp;
738         inst->lb.width = width;
739         inst->lb.height = height;
740
741         inst->content = strdup(content);
742         if (!inst->content) {
743                 ErrPrint("Heap: %s\n", strerror(errno));
744                 DbgFree(inst);
745                 return NULL;
746         }
747
748         inst->cluster = strdup(cluster);
749         if (!inst->cluster) {
750                 ErrPrint("Heap: %s\n", strerror(errno));
751                 DbgFree(inst->content);
752                 DbgFree(inst);
753                 return NULL;
754         }
755
756         inst->category = strdup(category);
757         if (!inst->category) {
758                 ErrPrint("Heap: %s\n", strerror(errno));
759                 DbgFree(inst->cluster);
760                 DbgFree(inst->content);
761                 DbgFree(inst);
762                 return NULL;
763         }
764
765         inst->title = strdup(DEFAULT_TITLE); /*!< Use the DEFAULT Title "" */
766         if (!inst->title) {
767                 ErrPrint("Heap: %s\n", strerror(errno));
768                 DbgFree(inst->category);
769                 DbgFree(inst->cluster);
770                 DbgFree(inst->content);
771                 DbgFree(inst);
772                 return NULL;
773         }
774
775         if (client) {
776                 inst->client = client_ref(client);
777                 client_event_callback_add(inst->client, CLIENT_EVENT_DEACTIVATE, client_deactivated_cb, inst);
778         }
779
780         if (fork_package(inst, pkgname) < 0) {
781                 client_event_callback_del(inst->client, CLIENT_EVENT_DEACTIVATE, client_deactivated_cb, inst);
782                 client_unref(inst->client);
783                 DbgFree(inst->title);
784                 DbgFree(inst->category);
785                 DbgFree(inst->cluster);
786                 DbgFree(inst->content);
787                 DbgFree(inst);
788                 return NULL;
789         }
790
791         inst->state = INST_INIT;
792         inst->requested_state = INST_INIT;
793         instance_ref(inst);
794
795         if (package_add_instance(inst->info, inst) < 0) {
796                 instance_state_reset(inst);
797                 instance_destroy(inst);
798                 return NULL;
799         }
800
801         slave_load_instance(package_slave(inst->info));
802
803         if (instance_activate(inst) < 0) {
804                 instance_state_reset(inst);
805                 instance_destroy(inst);
806                 inst = NULL;
807         }
808
809         return inst;
810 }
811
812 HAPI struct inst_info *instance_ref(struct inst_info *inst)
813 {
814         if (!inst)
815                 return NULL;
816
817         inst->refcnt++;
818         return inst;
819 }
820
821 HAPI struct inst_info *instance_unref(struct inst_info *inst)
822 {
823         if (!inst)
824                 return NULL;
825
826         if (inst->refcnt == 0) {
827                 ErrPrint("Instance refcnt is not valid\n");
828                 return NULL;
829         }
830
831         inst->refcnt--;
832         if (inst->refcnt == 0) {
833                 destroy_instance(inst);
834                 inst = NULL;
835         }
836
837         return inst;
838 }
839
840 static void deactivate_cb(struct slave_node *slave, const struct packet *packet, void *data)
841 {
842         struct inst_info *inst = data;
843         struct pkg_info *info;
844         int ret;
845
846         /*!
847          * \note
848          * In this callback, we cannot trust the "client" information.
849          * It could be cleared before reach to here.
850          */
851
852         if (!packet) {
853                 DbgPrint("Consuming a request of a dead process\n");
854                 /*!
855                  * \note
856                  * The instance_reload will care this.
857                  * And it will be called from the slave activate callback.
858                  */
859                 inst->changing_state = 0;
860                 instance_unref(inst);
861                 return;
862         }
863
864         if (packet_get(packet, "i", &ret) != 1) {
865                 ErrPrint("Invalid argument\n");
866                 inst->changing_state = 0;
867                 instance_unref(inst);
868                 return;
869         }
870
871         if (inst->state == INST_DESTROYED) {
872                 /*!
873                  * \note
874                  * Already destroyed.
875                  * Do nothing at here anymore.
876                  */
877                 inst->changing_state = 0;
878                 instance_unref(inst);
879                 return;
880         }
881
882         switch (ret) {
883         case 0:
884                 /*!
885                  * \note
886                  * Successfully unloaded
887                  */
888                 switch (inst->requested_state) {
889                 case INST_ACTIVATED:
890                         DbgPrint("REQ: ACTIVATED\n");
891                         instance_state_reset(inst);
892                         instance_reactivate(inst);
893                         break;
894                 case INST_DESTROYED:
895                         info = inst->info;
896                         instance_broadcast_deleted_event(inst);
897                         instance_state_reset(inst);
898                         instance_destroy(inst);
899                         DbgPrint("== %s\n", package_name(info));
900                 default:
901                         /*!< Unable to reach here */
902                         break;
903                 }
904
905                 break;
906         case LB_STATUS_ERROR_INVALID:
907                 /*!
908                  * \note
909                  * Slave has no instance of this package.
910                  */
911         case LB_STATUS_ERROR_NOT_EXIST:
912                 /*!
913                  * \note
914                  * This instance's previous state is only can be the INST_ACTIVATED.
915                  * So we should care the slave_unload_instance from here.
916                  * And we should send notification to clients, about this is deleted.
917                  */
918                 /*!
919                  * \note
920                  * Slave has no instance of this.
921                  * In this case, ignore the requested_state
922                  * Because, this instance is already met a problem.
923                  */
924         default:
925                 /*!
926                  * \note
927                  * Failed to unload this instance.
928                  * This is not possible, slave will always return LB_STATUS_ERROR_NOT_EXIST, LB_STATUS_ERROR_INVALID, or 0.
929                  * but care this exceptional case.
930                  */
931                 DbgPrint("[%s] instance destroying ret(%d)\n", package_name(inst->info), ret);
932                 info = inst->info;
933                 instance_broadcast_deleted_event(inst);
934                 instance_state_reset(inst);
935                 instance_destroy(inst);
936                 break;
937         }
938
939         inst->changing_state = 0;
940         instance_unref(inst);
941 }
942
943 static void reactivate_cb(struct slave_node *slave, const struct packet *packet, void *data)
944 {
945         struct inst_info *inst = data;
946         struct pkg_info *info;
947         enum lb_type lb_type;
948         enum pd_type pd_type;
949         int ret;
950         const char *content;
951         const char *title;
952         int is_pinned_up;
953
954         if (!packet) {
955                 DbgPrint("Consuming a request of a dead process\n");
956                 /*!
957                  * \note
958                  * instance_reload function will care this.
959                  * and it will be called from the slave_activate callback
960                  */
961                 goto out;
962         }
963
964         if (packet_get(packet, "issi", &ret, &content, &title, &is_pinned_up) != 4) {
965                 ErrPrint("Invalid parameter\n");
966                 goto out;
967         }
968
969         if (strlen(content)) {
970                 char *tmp;
971
972                 tmp = strdup(content);
973                 if (!tmp) {
974                         ErrPrint("Heap: %s\n", strerror(errno));
975                         goto out;
976                 }
977
978                 DbgFree(inst->content);
979                 inst->content = tmp;
980
981                 DbgPrint("Update content info %s\n", tmp);
982         }
983
984         if (strlen(title)) {
985                 char *tmp;
986
987                 tmp = strdup(title);
988                 if (!tmp) {
989                         ErrPrint("Heap: %s\n", strerror(errno));
990                         goto out;
991                 }
992
993                 DbgFree(inst->title);
994                 inst->title = tmp;
995
996                 DbgPrint("Update title info %s\n", tmp);
997         }
998
999         if (inst->state == INST_DESTROYED) {
1000                 /*!
1001                  * \note
1002                  * Already destroyed.
1003                  * Do nothing at here anymore.
1004                  */
1005                 goto out;
1006         }
1007
1008         switch (ret) {
1009         case 0: /*!< normally created */
1010                 inst->state = INST_ACTIVATED;
1011                 switch (inst->requested_state) {
1012                 case INST_DESTROYED:
1013                         instance_destroy(inst);
1014                         break;
1015                 case INST_ACTIVATED:
1016                         inst->is_pinned_up = is_pinned_up;
1017                         info = inst->info;
1018                         lb_type = package_lb_type(info);
1019                         pd_type = package_pd_type(info);
1020
1021                         /*!
1022                          * \note
1023                          * Optimization point.
1024                          *   In case of the BUFFER type,
1025                          *   the slave will request the buffer to render its contents.
1026                          *   so the buffer will be automatcially recreated when it gots the
1027                          *   buffer request packet.
1028                          *   so load a buffer from here is not neccessary.
1029                          *   I should to revise it and concrete the concept.
1030                          *   Just leave it only for now.
1031                          */
1032
1033                         if (lb_type == LB_TYPE_SCRIPT && inst->lb.canvas.script) {
1034                                 script_handler_load(inst->lb.canvas.script, 0);
1035                         } else if (lb_type == LB_TYPE_BUFFER && inst->lb.canvas.buffer) {
1036                                 buffer_handler_load(inst->lb.canvas.buffer);
1037                         }
1038
1039                         if (pd_type == PD_TYPE_SCRIPT && inst->pd.canvas.script && inst->pd.is_opened_for_reactivate) {
1040                                 double x, y;
1041                                 /*!
1042                                  * \note
1043                                  * We should to send a request to open a PD to slave.
1044                                  * if we didn't send it, the slave will not recognize the state of a PD.
1045                                  * We have to keep the view of PD seamless even if the livebox is reactivated.
1046                                  * To do that, send open request from here.
1047                                  */
1048                                 ret = instance_slave_open_pd(inst, NULL);
1049                                 instance_slave_get_pd_pos(inst, &x, &y);
1050
1051                                 /*!
1052                                  * \note
1053                                  * In this case, master already loads the PD script.
1054                                  * So just send the pd,show event to the slave again.
1055                                  */
1056                                 ret = instance_signal_emit(inst, "pd,show", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
1057                         } else if (pd_type == PD_TYPE_BUFFER && inst->pd.canvas.buffer && inst->pd.is_opened_for_reactivate) {
1058                                 double x, y;
1059
1060                                 buffer_handler_load(inst->pd.canvas.buffer);
1061                                 instance_slave_get_pd_pos(inst, &x, &y);
1062
1063                                 /*!
1064                                  * \note
1065                                  * We should to send a request to open a PD to slave.
1066                                  * if we didn't send it, the slave will not recognize the state of a PD.
1067                                  * We have to keep the view of PD seamless even if the livebox is reactivated.
1068                                  * To do that, send open request from here.
1069                                  */
1070                                 ret = instance_slave_open_pd(inst, NULL);
1071
1072                                 /*!
1073                                  * \note
1074                                  * In this case, just send the pd,show event for keeping the compatibility
1075                                  */
1076                                 ret = instance_signal_emit(inst, "pd,show", instance_id(inst), 0.0, 0.0, 0.0, 0.0, x, y, 0);
1077                         }
1078
1079                         /*!
1080                          * \note
1081                          * After create an instance again,
1082                          * Send resize request to the livebox.
1083                          * instance_resize(inst, inst->lb.width, inst->lb.height);
1084                          *
1085                          * renew request will resize the livebox while creating it again
1086                          */
1087
1088                         /*!
1089                          * \note
1090                          * This function will check the visiblity of a livebox and
1091                          * make decision whether it thaw the update timer or not.
1092                          */
1093                         instance_recover_visible_state(inst);
1094                 default:
1095                         break;
1096                 }
1097                 break;
1098         default:
1099                 info = inst->info;
1100                 DbgPrint("[%s] instance destroying ret(%d)\n", package_name(info), ret);
1101                 instance_broadcast_deleted_event(inst);
1102                 instance_state_reset(inst);
1103                 instance_destroy(inst);
1104                 break;
1105         }
1106
1107 out:
1108         inst->changing_state = 0;
1109         instance_unref(inst);
1110 }
1111
1112 static void activate_cb(struct slave_node *slave, const struct packet *packet, void *data)
1113 {
1114         struct inst_info *inst = data;
1115         int ret;
1116         int w;
1117         int h;
1118         double priority;
1119         char *content;
1120         char *title;
1121         int is_pinned_up;
1122
1123         if (!packet) {
1124                 DbgPrint("Consuming a request of a dead process\n");
1125                 /*!
1126                  * \note
1127                  * instance_reload will care this
1128                  * it will be called from the slave_activate callback
1129                  */
1130                 goto out;
1131         }
1132
1133         if (packet_get(packet, "iiidssi", &ret, &w, &h, &priority, &content, &title, &is_pinned_up) != 7) {
1134                 ErrPrint("Invalid parameter\n");
1135                 goto out;
1136         }
1137
1138         if (inst->state == INST_DESTROYED) {
1139                 /*!
1140                  * \note
1141                  * Already destroyed.
1142                  * Do nothing at here anymore.
1143                  */
1144                 goto out;
1145         }
1146
1147         switch (ret) {
1148         case 1: /*!< need to create */
1149                 if (util_free_space(IMAGE_PATH) > MINIMUM_SPACE) {
1150                         struct inst_info *new_inst;
1151                         new_inst = instance_create(inst->client, util_timestamp(), package_name(inst->info),
1152                                                         inst->content, inst->cluster, inst->category,
1153                                                         inst->lb.period, 0, 0);
1154                         if (!new_inst)
1155                                 ErrPrint("Failed to create a new instance\n");
1156                 } else {
1157                         ErrPrint("Not enough space\n");
1158                 }
1159         case 0: /*!< normally created */
1160                 /*!
1161                  * \note
1162                  * Anyway this instance is loaded to the slave,
1163                  * just increase the loaded instance counter
1164                  * And then reset jobs.
1165                  */
1166                 instance_set_lb_size(inst, w, h);
1167                 instance_set_lb_info(inst, priority, content, title);
1168
1169                 inst->state = INST_ACTIVATED;
1170
1171                 switch (inst->requested_state) {
1172                 case INST_DESTROYED:
1173                         instance_unicast_deleted_event(inst, NULL);
1174                         instance_state_reset(inst);
1175                         instance_destroy(inst);
1176                         break;
1177                 case INST_ACTIVATED:
1178                 default:
1179                         /*!
1180                          * \note
1181                          * LB should be created at the create time
1182                          */
1183                         inst->is_pinned_up = is_pinned_up;
1184                         if (package_lb_type(inst->info) == LB_TYPE_SCRIPT) {
1185                                 if (inst->lb.width == 0 && inst->lb.height == 0)
1186                                         livebox_service_get_size(LB_SIZE_TYPE_1x1, &inst->lb.width, &inst->lb.height);
1187
1188                                 inst->lb.canvas.script = script_handler_create(inst,
1189                                                                 package_lb_path(inst->info),
1190                                                                 package_lb_group(inst->info),
1191                                                                 inst->lb.width, inst->lb.height);
1192
1193                                 if (!inst->lb.canvas.script)
1194                                         ErrPrint("Failed to create LB\n");
1195                                 else
1196                                         script_handler_load(inst->lb.canvas.script, 0);
1197                         } else if (package_lb_type(inst->info) == LB_TYPE_BUFFER) {
1198                                 instance_create_lb_buffer(inst);
1199                         }
1200
1201                         if (package_pd_type(inst->info) == PD_TYPE_SCRIPT) {
1202                                 if (inst->pd.width == 0 && inst->pd.height == 0)
1203                                         instance_set_pd_size(inst, package_pd_width(inst->info), package_pd_height(inst->info));
1204
1205                                 inst->pd.canvas.script = script_handler_create(inst,
1206                                                                 package_pd_path(inst->info),
1207                                                                 package_pd_group(inst->info),
1208                                                                 inst->pd.width, inst->pd.height);
1209
1210                                 if (!inst->pd.canvas.script)
1211                                         ErrPrint("Failed to create PD\n");
1212                         } else if (package_pd_type(inst->info) == PD_TYPE_BUFFER) {
1213                                 instance_create_pd_buffer(inst);
1214                         }
1215
1216                         instance_broadcast_created_event(inst);
1217
1218                         instance_thaw_updator(inst);
1219                         break;
1220                 }
1221                 break;
1222         default:
1223                 DbgPrint("[%s] instance destroying ret(%d)\n", package_name(inst->info), ret);
1224                 instance_unicast_deleted_event(inst, NULL);
1225                 instance_state_reset(inst);
1226                 instance_destroy(inst);
1227                 break;
1228         }
1229
1230 out:
1231         inst->changing_state = 0;
1232         instance_unref(inst);
1233 }
1234
1235 HAPI int instance_create_pd_buffer(struct inst_info *inst)
1236 {
1237         if (inst->pd.width == 0 && inst->pd.height == 0)
1238                 instance_set_pd_size(inst, package_pd_width(inst->info), package_pd_height(inst->info));
1239
1240         if (!inst->pd.canvas.buffer) {
1241                 inst->pd.canvas.buffer = buffer_handler_create(inst, s_info.env_buf_type, inst->pd.width, inst->pd.height, sizeof(int));
1242                 if (!inst->pd.canvas.buffer)
1243                         ErrPrint("Failed to create PD Buffer\n");
1244         }
1245
1246         return !!inst->pd.canvas.buffer;
1247 }
1248
1249 HAPI int instance_create_lb_buffer(struct inst_info *inst)
1250 {
1251         if (inst->lb.width == 0 && inst->lb.height == 0)
1252                 livebox_service_get_size(LB_SIZE_TYPE_1x1, &inst->lb.width, &inst->lb.height);
1253
1254         if (!inst->lb.canvas.buffer) {
1255                 /*!
1256                  * \note
1257                  * Slave doesn't call the acquire_buffer.
1258                  * In this case, create the buffer from here.
1259                  */
1260                 inst->lb.canvas.buffer = buffer_handler_create(inst, s_info.env_buf_type, inst->lb.width, inst->lb.height, sizeof(int));
1261                 if (!inst->lb.canvas.buffer)
1262                         ErrPrint("Failed to create LB\n");
1263         }
1264
1265         return !!inst->lb.canvas.buffer;
1266 }
1267
1268 HAPI int instance_destroy(struct inst_info *inst)
1269 {
1270         struct packet *packet;
1271
1272         if (!inst) {
1273                 ErrPrint("Invalid instance handle\n");
1274                 return LB_STATUS_ERROR_INVALID;
1275         }
1276
1277         switch (inst->state) {
1278         case INST_REQUEST_TO_ACTIVATE:
1279         case INST_REQUEST_TO_DESTROY:
1280         case INST_REQUEST_TO_REACTIVATE:
1281                 inst->requested_state = INST_DESTROYED;
1282                 return LB_STATUS_SUCCESS;
1283         case INST_INIT:
1284                 inst->state = INST_DESTROYED;
1285                 inst->requested_state = INST_DESTROYED;
1286                 instance_unref(inst);
1287                 return LB_STATUS_SUCCESS;
1288         case INST_DESTROYED:
1289                 inst->requested_state = INST_DESTROYED;
1290                 return LB_STATUS_SUCCESS;
1291         default:
1292                 break;
1293         }
1294
1295         packet = packet_create("delete", "ss", package_name(inst->info), inst->id);
1296         if (!packet) {
1297                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
1298                 return LB_STATUS_ERROR_FAULT;
1299         }
1300
1301         inst->requested_state = INST_DESTROYED;
1302         inst->state = INST_REQUEST_TO_DESTROY;
1303         inst->changing_state = 1;
1304         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, deactivate_cb, instance_ref(inst), 0);
1305 }
1306
1307 /* Client Deactivated Callback */
1308 static int pd_buffer_close_cb(struct client_node *client, void *inst)
1309 {
1310         int ret;
1311
1312         ret = instance_slave_close_pd(inst, client);
1313         DbgPrint("Forcely close the PD ret: %d\n", ret);
1314
1315         instance_unref(inst);
1316
1317         return -1; /* Delete this callback */
1318 }
1319
1320 /* Client Deactivated Callback */
1321 static int pd_script_close_cb(struct client_node *client, void *inst)
1322 {
1323         int ret;
1324
1325         ret = script_handler_unload(instance_pd_script(inst), 1);
1326         DbgPrint("Unload script: %d\n", ret);
1327
1328         ret = instance_slave_close_pd(inst, client);
1329         DbgPrint("Forcely close the PD ret: %d\n", ret);
1330
1331         instance_unref(inst);
1332
1333         return -1; /* Delete this callback */
1334 }
1335
1336 static inline void release_resource_for_closing_pd(struct pkg_info *info, struct inst_info *inst, struct client_node *client)
1337 {
1338         if (!client) {
1339                 client = inst->pd.owner;
1340                 if (!client)
1341                         return;
1342         }
1343
1344         /*!
1345          * \note
1346          * Clean up the resources
1347          */
1348         if (package_pd_type(info) == PD_TYPE_BUFFER) {
1349                 if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, pd_buffer_close_cb, inst) == 0) {
1350                         /*!
1351                          * \note
1352                          * Only if this function succeed to remove the pd_buffer_close_cb,
1353                          * Decrease the reference count of this instance
1354                          */
1355                 }
1356                 instance_unref(inst);
1357         } else if (package_pd_type(info) == PD_TYPE_SCRIPT) {
1358                 if (client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, pd_script_close_cb, inst) == 0) {
1359                         /*!
1360                          * \note
1361                          * Only if this function succeed to remove the script_close_cb,
1362                          * Decrease the reference count of this instance
1363                          */
1364                 }
1365                 instance_unref(inst);
1366         } else {
1367                 ErrPrint("Unknown PD type\n");
1368         }
1369
1370 }
1371
1372 HAPI int instance_state_reset(struct inst_info *inst)
1373 {
1374         enum lb_type lb_type;
1375         enum pd_type pd_type;
1376
1377         if (!inst) {
1378                 ErrPrint("Invalid instance handle\n");
1379                 return LB_STATUS_ERROR_INVALID;
1380         }
1381
1382         if (inst->state == INST_DESTROYED)
1383                 goto out;
1384
1385         lb_type = package_lb_type(inst->info);
1386         pd_type = package_pd_type(inst->info);
1387
1388         if (lb_type == LB_TYPE_SCRIPT && inst->lb.canvas.script)
1389                 script_handler_unload(inst->lb.canvas.script, 0);
1390         else if (lb_type == LB_TYPE_BUFFER && inst->lb.canvas.buffer)
1391                 buffer_handler_unload(inst->lb.canvas.buffer);
1392
1393         if (pd_type == PD_TYPE_SCRIPT && inst->pd.canvas.script) {
1394                 inst->pd.is_opened_for_reactivate = script_handler_is_loaded(inst->pd.canvas.script);
1395                 release_resource_for_closing_pd(instance_package(inst), inst, NULL);
1396                 script_handler_unload(inst->pd.canvas.script, 1);
1397         } else if (pd_type == PD_TYPE_BUFFER && inst->pd.canvas.buffer) {
1398                 inst->pd.is_opened_for_reactivate = buffer_handler_is_loaded(inst->pd.canvas.buffer);
1399                 release_resource_for_closing_pd(instance_package(inst), inst, NULL);
1400                 buffer_handler_unload(inst->pd.canvas.buffer);
1401         }
1402
1403 out:
1404         inst->state = INST_INIT;
1405         inst->requested_state = INST_INIT;
1406         return LB_STATUS_SUCCESS;
1407 }
1408
1409 HAPI int instance_reactivate(struct inst_info *inst)
1410 {
1411         struct packet *packet;
1412         int ret;
1413
1414         if (!inst) {
1415                 ErrPrint("Invalid instance handle\n");
1416                 return LB_STATUS_ERROR_INVALID;
1417         }
1418
1419         if (package_is_fault(inst->info)) {
1420                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
1421                 return LB_STATUS_ERROR_FAULT;
1422         }
1423
1424         switch (inst->state) {
1425         case INST_REQUEST_TO_DESTROY:
1426         case INST_REQUEST_TO_ACTIVATE:
1427         case INST_REQUEST_TO_REACTIVATE:
1428                 inst->requested_state = INST_ACTIVATED;
1429                 return LB_STATUS_SUCCESS;
1430         case INST_DESTROYED:
1431         case INST_ACTIVATED:
1432                 return LB_STATUS_SUCCESS;
1433         case INST_INIT:
1434         default:
1435                 break;
1436         }
1437
1438         packet = packet_create("renew", "sssiidssiisii",
1439                         package_name(inst->info),
1440                         inst->id,
1441                         inst->content,
1442                         inst->lb.timeout,
1443                         !!package_lb_path(inst->info),
1444                         inst->lb.period,
1445                         inst->cluster,
1446                         inst->category,
1447                         inst->lb.width, inst->lb.height,
1448                         package_abi(inst->info),
1449                         inst->scroll_locked,
1450                         inst->active_update);
1451         if (!packet) {
1452                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
1453                 return LB_STATUS_ERROR_FAULT;
1454         }
1455
1456         ret = slave_activate(package_slave(inst->info));
1457         if (ret < 0 && ret != LB_STATUS_ERROR_ALREADY) {
1458                 /*!
1459                  * \note
1460                  * If the master failed to launch the slave,
1461                  * Do not send any requests to the slave.
1462                  */
1463                 ErrPrint("Failed to launch the slave\n");
1464                 packet_destroy(packet);
1465                 return ret;
1466         }
1467
1468         inst->requested_state = INST_ACTIVATED;
1469         inst->state = INST_REQUEST_TO_REACTIVATE;
1470         inst->changing_state = 1;
1471
1472         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, reactivate_cb, instance_ref(inst), 1);
1473 }
1474
1475 HAPI int instance_activate(struct inst_info *inst)
1476 {
1477         struct packet *packet;
1478         int ret;
1479
1480         if (!inst) {
1481                 ErrPrint("Invalid instance handle\n");
1482                 return LB_STATUS_ERROR_INVALID;
1483         }
1484
1485         if (package_is_fault(inst->info)) {
1486                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
1487                 return LB_STATUS_ERROR_FAULT;
1488         }
1489
1490         switch (inst->state) {
1491         case INST_REQUEST_TO_REACTIVATE:
1492         case INST_REQUEST_TO_ACTIVATE:
1493         case INST_REQUEST_TO_DESTROY:
1494                 inst->requested_state = INST_ACTIVATED;
1495                 return LB_STATUS_SUCCESS;
1496         case INST_ACTIVATED:
1497         case INST_DESTROYED:
1498                 return LB_STATUS_SUCCESS;
1499         case INST_INIT:
1500         default:
1501                 break;
1502         }
1503
1504         packet = packet_create("new", "sssiidssisii",
1505                         package_name(inst->info),
1506                         inst->id,
1507                         inst->content,
1508                         inst->lb.timeout,
1509                         !!package_lb_path(inst->info),
1510                         inst->lb.period,
1511                         inst->cluster,
1512                         inst->category,
1513                         !!inst->client,
1514                         package_abi(inst->info),
1515                         inst->lb.width,
1516                         inst->lb.height);
1517         if (!packet) {
1518                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
1519                 return LB_STATUS_ERROR_FAULT;
1520         }
1521
1522         ret = slave_activate(package_slave(inst->info));
1523         if (ret < 0 && ret != LB_STATUS_ERROR_ALREADY) {
1524                 /*!
1525                  * \note
1526                  * If the master failed to launch the slave,
1527                  * Do not send any requests to the slave.
1528                  */
1529                 ErrPrint("Failed to launch the slave\n");
1530                 packet_destroy(packet);
1531                 return ret;
1532         }
1533
1534         inst->state = INST_REQUEST_TO_ACTIVATE;
1535         inst->requested_state = INST_ACTIVATED;
1536         inst->changing_state = 1;
1537
1538         /*!
1539          * \note
1540          * Try to activate a slave if it is not activated
1541          */
1542         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, activate_cb, instance_ref(inst), 1);
1543 }
1544
1545 HAPI int instance_lb_update_begin(struct inst_info *inst, double priority, const char *content, const char *title)
1546 {
1547         struct packet *packet;
1548         const char *fbfile;
1549
1550         if (!inst->active_update) {
1551                 ErrPrint("Invalid request [%s]\n", inst->id);
1552                 return LB_STATUS_ERROR_INVALID;
1553         }
1554
1555         switch (package_lb_type(inst->info)) {
1556         case LB_TYPE_BUFFER:
1557                 if (!inst->lb.canvas.buffer) {
1558                         ErrPrint("Buffer is null [%s]\n", inst->id);
1559                         return LB_STATUS_ERROR_INVALID;
1560                 }
1561                 fbfile = buffer_handler_id(inst->lb.canvas.buffer);
1562                 break;
1563         case LB_TYPE_SCRIPT:
1564                 if (!inst->lb.canvas.script) {
1565                         ErrPrint("Script is null [%s]\n", inst->id);
1566                         return LB_STATUS_ERROR_INVALID;
1567                 }
1568                 fbfile = fb_id(script_handler_fb(inst->lb.canvas.script));
1569                 break;
1570         default:
1571                 ErrPrint("Invalid request[%s]\n", inst->id);
1572                 return LB_STATUS_ERROR_INVALID;
1573         }
1574
1575         packet = packet_create_noack("lb_update_begin", "ssdsss", package_name(inst->info), inst->id, priority, content, title, fbfile);
1576         if (!packet) {
1577                 ErrPrint("Unable to create a packet\n");
1578                 return LB_STATUS_ERROR_FAULT;
1579         }
1580
1581         return CLIENT_SEND_EVENT(inst, packet);
1582 }
1583
1584 HAPI int instance_lb_update_end(struct inst_info *inst)
1585 {
1586         struct packet *packet;
1587
1588         if (!inst->active_update) {
1589                 ErrPrint("Invalid request [%s]\n", inst->id);
1590                 return LB_STATUS_ERROR_INVALID;
1591         }
1592
1593         switch (package_lb_type(inst->info)) {
1594         case LB_TYPE_BUFFER:
1595                 if (!inst->lb.canvas.buffer) {
1596                         ErrPrint("Buffer is null [%s]\n", inst->id);
1597                         return LB_STATUS_ERROR_INVALID;
1598                 }
1599                 break;
1600         case LB_TYPE_SCRIPT:
1601                 if (!inst->lb.canvas.script) {
1602                         ErrPrint("Script is null [%s]\n", inst->id);
1603                         return LB_STATUS_ERROR_INVALID;
1604                 }
1605                 break;
1606         default:
1607                 ErrPrint("Invalid request[%s]\n", inst->id);
1608                 return LB_STATUS_ERROR_INVALID;
1609         }
1610
1611         packet = packet_create_noack("lb_update_end", "ss", package_name(inst->info), inst->id);
1612         if (!packet) {
1613                 ErrPrint("Unable to create a packet\n");
1614                 return LB_STATUS_ERROR_FAULT;
1615         }
1616
1617         return CLIENT_SEND_EVENT(inst, packet);
1618 }
1619
1620 HAPI int instance_pd_update_begin(struct inst_info *inst)
1621 {
1622         struct packet *packet;
1623         const char *fbfile;
1624
1625         if (!inst->active_update) {
1626                 ErrPrint("Invalid request [%s]\n", inst->id);
1627                 return LB_STATUS_ERROR_INVALID;
1628         }
1629
1630         switch (package_pd_type(inst->info)) {
1631         case PD_TYPE_BUFFER:
1632                 if (!inst->pd.canvas.buffer) {
1633                         ErrPrint("Buffer is null [%s]\n", inst->id);
1634                         return LB_STATUS_ERROR_INVALID;
1635                 }
1636                 fbfile = buffer_handler_id(inst->pd.canvas.buffer);
1637                 break;
1638         case PD_TYPE_SCRIPT:
1639                 if (!inst->pd.canvas.script) {
1640                         ErrPrint("Script is null [%s]\n", inst->id);
1641                         return LB_STATUS_ERROR_INVALID;
1642                 }
1643                 fbfile = fb_id(script_handler_fb(inst->pd.canvas.script));
1644                 break;
1645         default:
1646                 ErrPrint("Invalid request[%s]\n", inst->id);
1647                 return LB_STATUS_ERROR_INVALID;
1648         }
1649
1650         packet = packet_create_noack("pd_update_begin", "sss", package_name(inst->info), inst->id, fbfile);
1651         if (!packet) {
1652                 ErrPrint("Unable to create a packet\n");
1653                 return LB_STATUS_ERROR_FAULT;
1654         }
1655
1656         return CLIENT_SEND_EVENT(inst, packet);
1657 }
1658
1659 HAPI int instance_pd_update_end(struct inst_info *inst)
1660 {
1661         struct packet *packet;
1662
1663         if (!inst->active_update) {
1664                 ErrPrint("Invalid request [%s]\n", inst->id);
1665                 return LB_STATUS_ERROR_INVALID;
1666         }
1667
1668         switch (package_pd_type(inst->info)) {
1669         case PD_TYPE_BUFFER:
1670                 if (!inst->pd.canvas.buffer) {
1671                         ErrPrint("Buffer is null [%s]\n", inst->id);
1672                         return LB_STATUS_ERROR_INVALID;
1673                 }
1674                 break;
1675         case PD_TYPE_SCRIPT:
1676                 if (!inst->pd.canvas.script) {
1677                         ErrPrint("Script is null [%s]\n", inst->id);
1678                         return LB_STATUS_ERROR_INVALID;
1679                 }
1680                 break;
1681         default:
1682                 ErrPrint("Invalid request[%s]\n", inst->id);
1683                 return LB_STATUS_ERROR_INVALID;
1684         }
1685
1686         packet = packet_create_noack("pd_update_end", "ss", package_name(inst->info), inst->id);
1687         if (!packet) {
1688                 ErrPrint("Unable to create a packet\n");
1689                 return LB_STATUS_ERROR_FAULT;
1690         }
1691
1692         return CLIENT_SEND_EVENT(inst, packet);
1693 }
1694
1695 HAPI void instance_lb_updated(const char *pkgname, const char *id)
1696 {
1697         struct inst_info *inst;
1698
1699         inst = package_find_instance_by_id(pkgname, id);
1700         if (!inst)
1701                 return;
1702
1703         instance_lb_updated_by_instance(inst);
1704 }
1705
1706 HAPI void instance_lb_updated_by_instance(struct inst_info *inst)
1707 {
1708         struct packet *packet;
1709         const char *id;
1710         enum lb_type lb_type;
1711         const char *title;
1712         const char *content;
1713
1714         if (inst->client && inst->visible != LB_SHOW) {
1715                 if (inst->visible == LB_HIDE) {
1716                         DbgPrint("Ignore update event %s(HIDE)\n", inst->id);
1717                         return;
1718                 }
1719                 DbgPrint("Livebox(%s) is PAUSED. But content is updated.\n", inst->id);
1720         }
1721
1722         lb_type = package_lb_type(inst->info);
1723         if (lb_type == LB_TYPE_SCRIPT)
1724                 id = fb_id(script_handler_fb(inst->lb.canvas.script));
1725         else if (lb_type == LB_TYPE_BUFFER)
1726                 id = buffer_handler_id(inst->lb.canvas.buffer);
1727         else
1728                 id = "";
1729
1730         if (inst->content)
1731                 content = inst->content;
1732         else
1733                 content = "";
1734
1735         if (inst->title)
1736                 title = inst->title;
1737         else
1738                 title = "";
1739
1740         packet = packet_create_noack("lb_updated", "sssiidss",
1741                         package_name(inst->info), inst->id, id,
1742                         inst->lb.width, inst->lb.height, inst->lb.priority, content, title);
1743         if (!packet) {
1744                 ErrPrint("Failed to create param (%s - %s)\n", package_name(inst->info), inst->id);
1745                 return;
1746         }
1747
1748         (void)CLIENT_SEND_EVENT(inst, packet);
1749 }
1750
1751 HAPI int instance_hold_scroll(struct inst_info *inst, int hold)
1752 {
1753         struct packet *packet;
1754
1755         if (inst->scroll_locked == hold) {
1756                 DbgPrint("[HOLD] There is changes for hold state: %d\n", hold);
1757                 return LB_STATUS_ERROR_ALREADY;
1758         }
1759
1760         packet = packet_create_noack("scroll", "ssi", package_name(inst->info), inst->id, hold);
1761         if (!packet) {
1762                 ErrPrint("[HOLD] Failed to build a packet\n");
1763                 return LB_STATUS_ERROR_FAULT;
1764         }
1765
1766         DbgPrint("[HOLD] (%s) %d\n", inst->id, hold);
1767         inst->scroll_locked = hold;
1768         return CLIENT_SEND_EVENT(inst, packet);
1769 }
1770
1771 HAPI void instance_pd_updated_by_instance(struct inst_info *inst, const char *descfile)
1772 {
1773         struct packet *packet;
1774         const char *id;
1775
1776         if (inst->client && inst->visible != LB_SHOW) {
1777                 DbgPrint("Livebox is hidden. ignore update event\n");
1778                 return;
1779         }
1780
1781         if (!inst->pd.need_to_send_close_event) {
1782                 DbgPrint("PD is not created yet. Ignore update event - %s\n", descfile);
1783
1784                 if (inst->pd.pended_update_desc) {
1785                         DbgFree(inst->pd.pended_update_desc);
1786                         inst->pd.pended_update_desc = NULL;
1787                 }
1788
1789                 if (descfile) {
1790                         inst->pd.pended_update_desc = strdup(descfile);
1791                         if (!inst->pd.pended_update_desc)
1792                                 ErrPrint("Heap: %s\n", strerror(errno));
1793                 }
1794
1795                 inst->pd.pended_update_cnt++;
1796                 return;
1797         }
1798
1799         if (!descfile)
1800                 descfile = inst->id;
1801
1802         switch (package_pd_type(inst->info)) {
1803         case PD_TYPE_SCRIPT:
1804                 id = fb_id(script_handler_fb(inst->pd.canvas.script));
1805                 break;
1806         case PD_TYPE_BUFFER:
1807                 id = buffer_handler_id(inst->pd.canvas.buffer);
1808                 break;
1809         case PD_TYPE_TEXT:
1810         default:
1811                 id = "";
1812                 break;
1813         }
1814
1815         packet = packet_create_noack("pd_updated", "ssssii",
1816                         package_name(inst->info), inst->id, descfile, id,
1817                         inst->pd.width, inst->pd.height);
1818         if (!packet) {
1819                 ErrPrint("Failed to create param (%s - %s)\n", package_name(inst->info), inst->id);
1820                 return;
1821         }
1822
1823         (void)CLIENT_SEND_EVENT(inst, packet);
1824 }
1825
1826 HAPI void instance_pd_updated(const char *pkgname, const char *id, const char *descfile)
1827 {
1828         struct inst_info *inst;
1829
1830         inst = package_find_instance_by_id(pkgname, id);
1831         if (!inst)
1832                 return;
1833
1834         instance_pd_updated_by_instance(inst, descfile);
1835 }
1836
1837 HAPI int instance_set_update_mode(struct inst_info *inst, int active_update)
1838 {
1839         struct packet *packet;
1840         struct update_mode_cbdata *cbdata;
1841
1842         if (package_is_fault(inst->info)) {
1843                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
1844                 return LB_STATUS_ERROR_FAULT;
1845         }
1846
1847         if (inst->active_update == active_update) {
1848                 DbgPrint("Active update is not changed: %d\n", inst->active_update);
1849                 return LB_STATUS_ERROR_ALREADY;
1850         }
1851
1852         cbdata = malloc(sizeof(*cbdata));
1853         if (!cbdata) {
1854                 ErrPrint("Heap: %s\n", strerror(errno));
1855                 return LB_STATUS_ERROR_MEMORY;
1856         }
1857
1858         cbdata->inst = instance_ref(inst);
1859         cbdata->active_update = active_update;
1860
1861         /* NOTE: param is resued from here */
1862         packet = packet_create("update_mode", "ssi", package_name(inst->info), inst->id, active_update);
1863         if (!packet) {
1864                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
1865                 instance_unref(cbdata->inst);
1866                 free(cbdata);
1867                 return LB_STATUS_ERROR_FAULT;
1868         }
1869
1870         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, update_mode_cb, cbdata, 0);
1871 }
1872
1873 HAPI int instance_active_update(struct inst_info *inst)
1874 {
1875         return inst->active_update;
1876 }
1877
1878 HAPI void instance_set_lb_info(struct inst_info *inst, double priority, const char *content, const char *title)
1879 {
1880         char *_content = NULL;
1881         char *_title = NULL;
1882
1883         if (content && strlen(content)) {
1884                 _content = strdup(content);
1885                 if (!_content)
1886                         ErrPrint("Heap: %s\n", strerror(errno));
1887         }
1888
1889         if (title && strlen(title)) {
1890                 _title = strdup(title);
1891                 if (!_title)
1892                         ErrPrint("Heap: %s\n", strerror(errno));
1893         }
1894
1895         if (_content) {
1896                 DbgFree(inst->content);
1897                 inst->content= _content;
1898         }
1899
1900         if (_title) {
1901                 DbgFree(inst->title);
1902                 inst->title = _title;
1903         }
1904
1905         if (priority >= 0.0f && priority <= 1.0f)
1906                 inst->lb.priority = priority;
1907 }
1908
1909 HAPI void instance_set_lb_size(struct inst_info *inst, int w, int h)
1910 {
1911         if (inst->lb.width != w || inst->lb.height != h)
1912                 instance_send_resized_event(inst, IS_LB, w, h, LB_STATUS_SUCCESS);
1913
1914         inst->lb.width = w;
1915         inst->lb.height = h;
1916 }
1917
1918 HAPI void instance_set_pd_size(struct inst_info *inst, int w, int h)
1919 {
1920         if (inst->pd.width != w || inst->pd.height != h)
1921                 instance_send_resized_event(inst, IS_PD, w, h, LB_STATUS_SUCCESS);
1922
1923         inst->pd.width = w;
1924         inst->pd.height = h;
1925 }
1926
1927 static void pinup_cb(struct slave_node *slave, const struct packet *packet, void *data)
1928 {
1929         struct set_pinup_cbdata *cbdata = data;
1930         const char *content;
1931         struct packet *result;
1932         int ret;
1933
1934         if (!packet) {
1935                 /*!
1936                  * \todo
1937                  * Send pinup failed event to client.
1938                  */
1939                 ret = LB_STATUS_ERROR_INVALID;
1940                 goto out;
1941         }
1942
1943         if (packet_get(packet, "is", &ret, &content) != 2) {
1944                 /*!
1945                  * \todo
1946                  * Send pinup failed event to client
1947                  */
1948                 ret = LB_STATUS_ERROR_INVALID;
1949                 goto out;
1950         }
1951
1952         if (ret == 0) {
1953                 char *new_content;
1954
1955                 new_content = strdup(content);
1956                 if (!new_content) {
1957                         ErrPrint("Heap: %s\n", strerror(errno));
1958                         /*!
1959                          * \note
1960                          * send pinup failed event to client
1961                          */
1962                         ret = LB_STATUS_ERROR_MEMORY;
1963                         goto out;
1964                 }
1965         
1966                 cbdata->inst->is_pinned_up = cbdata->pinup;
1967                 DbgFree(cbdata->inst->content);
1968
1969                 cbdata->inst->content = new_content;
1970         }
1971
1972 out:
1973         /*!
1974          * \node
1975          * Send PINUP Result to client.
1976          * Client should wait this event.
1977          */
1978         result = packet_create_noack("pinup", "iisss", ret, cbdata->inst->is_pinned_up,
1979                                                         package_name(cbdata->inst->info), cbdata->inst->id, cbdata->inst->content);
1980         if (result)
1981                 (void)CLIENT_SEND_EVENT(cbdata->inst, result);
1982         else
1983                 ErrPrint("Failed to build a packet for %s\n", package_name(cbdata->inst->info));
1984
1985         instance_unref(cbdata->inst);
1986         DbgFree(cbdata);
1987 }
1988
1989 HAPI int instance_set_pinup(struct inst_info *inst, int pinup)
1990 {
1991         struct set_pinup_cbdata *cbdata;
1992         struct packet *packet;
1993
1994         if (!inst) {
1995                 ErrPrint("Invalid instance handle\n");
1996                 return LB_STATUS_ERROR_INVALID;
1997         }
1998
1999         if (package_is_fault(inst->info)) {
2000                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
2001                 return LB_STATUS_ERROR_FAULT;
2002         }
2003
2004         if (!package_pinup(inst->info))
2005                 return LB_STATUS_ERROR_INVALID;
2006
2007         if (pinup == inst->is_pinned_up)
2008                 return LB_STATUS_ERROR_INVALID;
2009
2010         cbdata = malloc(sizeof(*cbdata));
2011         if (!cbdata)
2012                 return LB_STATUS_ERROR_MEMORY;
2013
2014         cbdata->inst = instance_ref(inst);
2015         cbdata->pinup = pinup;
2016
2017         packet = packet_create("pinup", "ssi", package_name(inst->info), inst->id, pinup);
2018         if (!packet) {
2019                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2020                 instance_unref(cbdata->inst);
2021                 DbgFree(cbdata);
2022                 return LB_STATUS_ERROR_FAULT;
2023         }
2024
2025         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, pinup_cb, cbdata, 0);
2026 }
2027
2028 HAPI int instance_freeze_updator(struct inst_info *inst)
2029 {
2030         if (!inst->update_timer)
2031                 return LB_STATUS_ERROR_INVALID;
2032
2033         DbgPrint("Freeze the update timer (%s)\n", inst->id);
2034         timer_freeze(inst);
2035         return LB_STATUS_SUCCESS;
2036 }
2037
2038 HAPI int instance_thaw_updator(struct inst_info *inst)
2039 {
2040         if (!inst->update_timer)
2041                 return LB_STATUS_ERROR_INVALID;
2042
2043         if (client_is_all_paused() || setting_is_lcd_off()) {
2044                 DbgPrint("Skip thaw (%s)\n", inst->id);
2045                 return LB_STATUS_ERROR_INVALID;
2046         }
2047
2048         if (inst->visible == LB_HIDE_WITH_PAUSE) {
2049                 DbgPrint("Live box is invisible (%s)\n", inst->id);
2050                 return LB_STATUS_ERROR_INVALID;
2051         }
2052
2053         DbgPrint("Thaw the update timer (%s)\n", inst->id);
2054         timer_thaw(inst);
2055         return LB_STATUS_SUCCESS;
2056 }
2057
2058 HAPI enum livebox_visible_state instance_visible_state(struct inst_info *inst)
2059 {
2060         return inst->visible;
2061 }
2062
2063 HAPI int instance_set_visible_state(struct inst_info *inst, enum livebox_visible_state state)
2064 {
2065         if (inst->visible == state) {
2066                 DbgPrint("Visibility has no changed\n");
2067                 return LB_STATUS_SUCCESS;
2068         }
2069
2070         switch (state) {
2071         case LB_SHOW:
2072         case LB_HIDE:
2073                 if (inst->visible == LB_HIDE_WITH_PAUSE) {
2074                         if (resume_livebox(inst) == 0)
2075                                 inst->visible = state;
2076
2077                         instance_thaw_updator(inst);
2078                 } else {
2079                         inst->visible = state;
2080                 }
2081                 break;
2082
2083         case LB_HIDE_WITH_PAUSE:
2084                 if (pause_livebox(inst) == 0)
2085                         inst->visible = LB_HIDE_WITH_PAUSE;
2086
2087                 instance_freeze_updator(inst);
2088                 break;
2089
2090         default:
2091                 return LB_STATUS_ERROR_INVALID;
2092         }
2093
2094         return LB_STATUS_SUCCESS;
2095 }
2096
2097 static void resize_cb(struct slave_node *slave, const struct packet *packet, void *data)
2098 {
2099         struct resize_cbdata *cbdata = data;
2100         int ret;
2101
2102         if (!packet) {
2103                 ErrPrint("RESIZE: Invalid packet\n");
2104                 instance_send_resized_event(cbdata->inst, IS_LB, cbdata->inst->lb.width, cbdata->inst->lb.height, LB_STATUS_ERROR_FAULT);
2105                 instance_unref(cbdata->inst);
2106                 DbgFree(cbdata);
2107                 return;
2108         }
2109
2110         if (packet_get(packet, "i", &ret) != 1) {
2111                 ErrPrint("RESIZE: Invalid parameter\n");
2112                 instance_send_resized_event(cbdata->inst, IS_LB, cbdata->inst->lb.width, cbdata->inst->lb.height, LB_STATUS_ERROR_INVALID);
2113                 instance_unref(cbdata->inst);
2114                 DbgFree(cbdata);
2115                 return;
2116         }
2117
2118         if (ret == LB_STATUS_SUCCESS) {
2119                 /*!
2120                  * \note
2121                  * else waiting the first update with new size
2122                  */
2123                 if (cbdata->inst->lb.width == cbdata->w && cbdata->inst->lb.height == cbdata->h) {
2124                         /*!
2125                          * \note
2126                          * Right after the viewer adds a new box,
2127                          * Box has no size information, then it will try to use the default size,
2128                          * After a box returns created event.
2129                          *
2130                          * A box will start to generate default size content.
2131                          * But the viewer doesn't know it,.
2132                          *
2133                          * So the viewer will try to change the size of a box.
2134                          *
2135                          * At that time, the provider gots the size changed event from the box.
2136                          * So it sent the size changed event to the viewer.
2137                          * But the viewer ignores it. if it doesn't care the size changed event.
2138                          * (even if it cares the size changed event, there is a timing issue)
2139                          *
2140                          * And the provider receives resize request,
2141                          * right before send the size changed event.
2142                          * but there is no changes about the size.
2143                          *
2144                          * Now the view will waits size changed event forever.
2145                          * To resolve this timing issue.
2146                          *
2147                          * Check the size of a box from here.
2148                          * And if the size is already updated, send the ALREADY event to the viewer
2149                          * to get the size changed event callback correctly.
2150                          */
2151                         instance_send_resized_event(cbdata->inst, IS_LB, cbdata->inst->lb.width, cbdata->inst->lb.height, LB_STATUS_ERROR_ALREADY);
2152                         DbgPrint("RESIZE: Livebox is already resized [%s - %dx%d]\n", instance_id(cbdata->inst), cbdata->w, cbdata->h);
2153                 } else {
2154                         DbgPrint("RESIZE: Request is successfully sent [%s - %dx%d]\n", instance_id(cbdata->inst), cbdata->w, cbdata->h);
2155                 }
2156         } else {
2157                 DbgPrint("RESIZE: Livebox rejects the new size: %s - %dx%d (%d)\n", instance_id(cbdata->inst), cbdata->w, cbdata->h, ret);
2158                 instance_send_resized_event(cbdata->inst, IS_LB, cbdata->inst->lb.width, cbdata->inst->lb.height, ret);
2159         }
2160
2161         instance_unref(cbdata->inst);
2162         DbgFree(cbdata);
2163 }
2164
2165 HAPI int instance_resize(struct inst_info *inst, int w, int h)
2166 {
2167         struct resize_cbdata *cbdata;
2168         struct packet *packet;
2169         int ret;
2170
2171         if (!inst) {
2172                 ErrPrint("Invalid instance handle\n");
2173                 return LB_STATUS_ERROR_INVALID;
2174         }
2175
2176         if (package_is_fault(inst->info)) {
2177                 ErrPrint("Fault package: %s\n", package_name(inst->info));
2178                 return LB_STATUS_ERROR_FAULT;
2179         }
2180
2181         cbdata = malloc(sizeof(*cbdata));
2182         if (!cbdata) {
2183                 ErrPrint("Heap: %s\n", strerror(errno));
2184                 return LB_STATUS_ERROR_MEMORY;
2185         }
2186
2187         cbdata->inst = instance_ref(inst);
2188         cbdata->w = w;
2189         cbdata->h = h;
2190
2191         /* NOTE: param is resued from here */
2192         packet = packet_create("resize", "ssii", package_name(inst->info), inst->id, w, h);
2193         if (!packet) {
2194                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2195                 instance_unref(cbdata->inst);
2196                 DbgFree(cbdata);
2197                 return LB_STATUS_ERROR_FAULT;
2198         }
2199
2200         DbgPrint("RESIZE: INSTANCE[%s] Request resize[%dx%d] box\n", instance_id(inst), w, h);
2201         ret = slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, resize_cb, cbdata, 0);
2202         return ret;
2203 }
2204
2205 static void set_period_cb(struct slave_node *slave, const struct packet *packet, void *data)
2206 {
2207         int ret;
2208         struct period_cbdata *cbdata = data;
2209         struct packet *result;
2210
2211         if (!packet) {
2212                 ret = LB_STATUS_ERROR_FAULT;
2213                 goto out;
2214         }
2215
2216         if (packet_get(packet, "i", &ret) != 1) {
2217                 ret = LB_STATUS_ERROR_INVALID;
2218                 goto out;
2219         }
2220
2221         if (ret == 0)
2222                 cbdata->inst->lb.period = cbdata->period;
2223         else
2224                 ErrPrint("Failed to set period %d\n", ret);
2225
2226 out:
2227         result = packet_create_noack("period_changed", "idss", ret, cbdata->inst->lb.period, package_name(cbdata->inst->info), cbdata->inst->id);
2228         if (result)
2229                 (void)CLIENT_SEND_EVENT(cbdata->inst, result);
2230         else
2231                 ErrPrint("Failed to build a packet for %s\n", package_name(cbdata->inst->info));
2232
2233         instance_unref(cbdata->inst);
2234         DbgFree(cbdata);
2235         return;
2236 }
2237
2238 static Eina_Bool timer_updator_cb(void *data)
2239 {
2240         struct period_cbdata *cbdata = data;
2241         struct inst_info *inst;
2242         double period;
2243         struct packet *result;
2244
2245         period = cbdata->period;
2246         inst = cbdata->inst;
2247         DbgFree(cbdata);
2248
2249         DbgPrint("Update period is changed to %lf from %lf\n", period, inst->lb.period);
2250
2251         inst->lb.period = period;
2252         if (inst->update_timer) {
2253                 if (inst->lb.period == 0.0f) {
2254                         ecore_timer_del(inst->update_timer);
2255                         inst->update_timer = NULL;
2256                 } else {
2257                         util_timer_interval_set(inst->update_timer, inst->lb.period);
2258                 }
2259         } else if (inst->lb.period > 0.0f) {
2260                 inst->update_timer = util_timer_add(inst->lb.period, update_timer_cb, inst);
2261                 if (!inst->update_timer)
2262                         ErrPrint("Failed to add an update timer for instance %s\n", inst->id);
2263                 else
2264                         timer_freeze(inst); /* Freeze the update timer as default */
2265         }
2266
2267         result = packet_create_noack("period_changed", "idss", 0, inst->lb.period, package_name(inst->info), inst->id);
2268         if (result)
2269                 (void)CLIENT_SEND_EVENT(inst, result);
2270         else
2271                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2272
2273         instance_unref(inst);
2274         return ECORE_CALLBACK_CANCEL;
2275 }
2276
2277 HAPI int instance_set_period(struct inst_info *inst, double period)
2278 {
2279         struct packet *packet;
2280         struct period_cbdata *cbdata;
2281
2282         if (!inst) {
2283                 ErrPrint("Invalid instance handle\n");
2284                 return LB_STATUS_ERROR_INVALID;
2285         }
2286
2287         if (package_is_fault(inst->info)) {
2288                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
2289                 return LB_STATUS_ERROR_FAULT;
2290         }
2291
2292         if (period < 0.0f) { /* Use the default period */
2293                 period = package_period(inst->info);
2294         } else if (period > 0.0f && period < MINIMUM_PERIOD) {
2295                 period = MINIMUM_PERIOD; /* defined at conf.h */
2296         }
2297
2298         cbdata = malloc(sizeof(*cbdata));
2299         if (!cbdata) {
2300                 ErrPrint("Heap: %s\n", strerror(errno));
2301                 return LB_STATUS_ERROR_MEMORY;
2302         }
2303
2304         cbdata->period = period;
2305         cbdata->inst = instance_ref(inst);
2306
2307         if (package_secured(inst->info)) {
2308                 /*!
2309                  * \note
2310                  * Secured livebox doesn't need to send its update period to the slave.
2311                  * Slave has no local timer for updating liveboxes
2312                  *
2313                  * So update its timer at here.
2314                  */
2315                 if (!ecore_timer_add(DELAY_TIME, timer_updator_cb, cbdata))
2316                         timer_updator_cb(cbdata);
2317                 return LB_STATUS_SUCCESS;
2318         }
2319
2320         packet = packet_create("set_period", "ssd", package_name(inst->info), inst->id, period);
2321         if (!packet) {
2322                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2323                 instance_unref(cbdata->inst);
2324                 DbgFree(cbdata);
2325                 return LB_STATUS_ERROR_FAULT;
2326         }
2327
2328         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, set_period_cb, cbdata, 0);
2329 }
2330
2331 HAPI int instance_clicked(struct inst_info *inst, const char *event, double timestamp, double x, double y)
2332 {
2333         struct packet *packet;
2334
2335         if (!inst) {
2336                 ErrPrint("Invalid instance handle\n");
2337                 return LB_STATUS_ERROR_INVALID;
2338         }
2339
2340         if (package_is_fault(inst->info)) {
2341                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
2342                 return LB_STATUS_ERROR_FAULT;
2343         }
2344
2345         /* NOTE: param is resued from here */
2346         packet = packet_create_noack("clicked", "sssddd", package_name(inst->info), inst->id, event, timestamp, x, y);
2347         if (!packet) {
2348                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2349                 return LB_STATUS_ERROR_FAULT;
2350         }
2351
2352         return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
2353 }
2354
2355 HAPI int instance_signal_emit(struct inst_info *inst, const char *signal, const char *part, double sx, double sy, double ex, double ey, double x, double y, int down)
2356 {
2357         const char *pkgname;
2358         const char *id;
2359         struct slave_node *slave;
2360         struct packet *packet;
2361         struct pkg_info *pkg;
2362
2363         pkg = instance_package(inst);
2364         pkgname = package_name(pkg);
2365         id = instance_id(inst);
2366         if (!pkgname || !id)
2367                 return LB_STATUS_ERROR_INVALID;
2368
2369         slave = package_slave(pkg);
2370         if (!slave)
2371                 return LB_STATUS_ERROR_INVALID;
2372
2373         packet = packet_create_noack("script", "ssssddddddi",
2374                         pkgname, id,
2375                         signal, part,
2376                         sx, sy, ex, ey,
2377                         x, y, down);
2378         if (!packet)
2379                 return LB_STATUS_ERROR_FAULT;
2380
2381         return slave_rpc_request_only(slave, pkgname, packet, 0); 
2382 }
2383
2384 HAPI int instance_text_signal_emit(struct inst_info *inst, const char *emission, const char *source, double sx, double sy, double ex, double ey)
2385 {
2386         struct packet *packet;
2387
2388         if (!inst) {
2389                 ErrPrint("Invalid instance handle\n");
2390                 return LB_STATUS_ERROR_INVALID;
2391         }
2392
2393         if (package_is_fault(inst->info)) {
2394                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
2395                 return LB_STATUS_ERROR_FAULT;
2396         }
2397
2398         packet = packet_create_noack("text_signal", "ssssdddd", package_name(inst->info), inst->id, emission, source, sx, sy, ex, ey);
2399         if (!packet) {
2400                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2401                 return LB_STATUS_ERROR_FAULT;
2402         }
2403
2404         return slave_rpc_request_only(package_slave(inst->info), package_name(inst->info), packet, 0);
2405 }
2406
2407 static void change_group_cb(struct slave_node *slave, const struct packet *packet, void *data)
2408 {
2409         struct change_group_cbdata *cbdata = data;
2410         struct packet *result;
2411         int ret;
2412
2413         if (!packet) {
2414                 DbgFree(cbdata->cluster);
2415                 DbgFree(cbdata->category);
2416                 ret = LB_STATUS_ERROR_FAULT;
2417                 goto out;
2418         }
2419
2420         if (packet_get(packet, "i", &ret) != 1) {
2421                 ErrPrint("Invalid packet\n");
2422                 DbgFree(cbdata->cluster);
2423                 DbgFree(cbdata->category);
2424                 ret = LB_STATUS_ERROR_INVALID;
2425                 goto out;
2426         }
2427
2428         if (ret == 0) {
2429                 DbgFree(cbdata->inst->cluster);
2430                 cbdata->inst->cluster = cbdata->cluster;
2431
2432                 DbgFree(cbdata->inst->category);
2433                 cbdata->inst->category = cbdata->category;
2434         } else {
2435                 DbgFree(cbdata->cluster);
2436                 DbgFree(cbdata->category);
2437         }
2438
2439 out:
2440         result = packet_create_noack("group_changed", "ssiss",
2441                                 package_name(cbdata->inst->info), cbdata->inst->id, ret,
2442                                 cbdata->inst->cluster, cbdata->inst->category);
2443         if (!result)
2444                 ErrPrint("Failed to build a packet %s\n", package_name(cbdata->inst->info));
2445         else
2446                 (void)CLIENT_SEND_EVENT(cbdata->inst, result);
2447
2448         instance_unref(cbdata->inst);
2449         DbgFree(cbdata);
2450 }
2451
2452 HAPI int instance_change_group(struct inst_info *inst, const char *cluster, const char *category)
2453 {
2454         struct packet *packet;
2455         struct change_group_cbdata *cbdata;
2456
2457         if (!inst) {
2458                 ErrPrint("Invalid instance handle\n");
2459                 return LB_STATUS_ERROR_INVALID;
2460         }
2461
2462         if (package_is_fault(inst->info)) {
2463                 DbgPrint("Fault package [%s]\n", package_name(inst->info));
2464                 return LB_STATUS_ERROR_FAULT;
2465         }
2466
2467         cbdata = malloc(sizeof(*cbdata));
2468         if (!cbdata) {
2469                 ErrPrint("Heap: %s\n", strerror(errno));
2470                 return LB_STATUS_ERROR_MEMORY;
2471         }
2472
2473         cbdata->cluster = strdup(cluster);
2474         if (!cbdata->cluster) {
2475                 ErrPrint("Heap: %s\n", strerror(errno));
2476                 DbgFree(cbdata);
2477                 return LB_STATUS_ERROR_MEMORY;
2478         }
2479
2480         cbdata->category = strdup(category);
2481         if (!cbdata->category) {
2482                 ErrPrint("Heap: %s\n", strerror(errno));
2483                 DbgFree(cbdata->cluster);
2484                 DbgFree(cbdata);
2485                 return LB_STATUS_ERROR_MEMORY;
2486         }
2487
2488         cbdata->inst = instance_ref(inst);
2489
2490         packet = packet_create("change_group","ssss", package_name(inst->info), inst->id, cluster, category);
2491         if (!packet) {
2492                 ErrPrint("Failed to build a packet for %s\n", package_name(inst->info));
2493                 instance_unref(cbdata->inst);
2494                 DbgFree(cbdata->category);
2495                 DbgFree(cbdata->cluster);
2496                 DbgFree(cbdata);
2497                 return LB_STATUS_ERROR_FAULT;
2498         }
2499
2500         return slave_rpc_async_request(package_slave(inst->info), package_name(inst->info), packet, change_group_cb, cbdata, 0);
2501 }
2502
2503 HAPI const char * const instance_auto_launch(const struct inst_info *inst)
2504 {
2505         return inst->lb.auto_launch;
2506 }
2507
2508 HAPI const int const instance_priority(const struct inst_info *inst)
2509 {
2510         return inst->lb.priority;
2511 }
2512
2513 HAPI const struct client_node *const instance_client(const struct inst_info *inst)
2514 {
2515         return inst->client;
2516 }
2517
2518 HAPI const int const instance_timeout(const struct inst_info *inst)
2519 {
2520         return inst->lb.timeout;
2521 }
2522
2523 HAPI const double const instance_period(const struct inst_info *inst)
2524 {
2525         return inst->lb.period;
2526 }
2527
2528 HAPI const int const instance_lb_width(const struct inst_info *inst)
2529 {
2530         return inst->lb.width;
2531 }
2532
2533 HAPI const int const instance_lb_height(const struct inst_info *inst)
2534 {
2535         return inst->lb.height;
2536 }
2537
2538 HAPI const int const instance_pd_width(const struct inst_info *inst)
2539 {
2540         return inst->pd.width;
2541 }
2542
2543 HAPI const int const instance_pd_height(const struct inst_info *inst)
2544 {
2545         return inst->pd.height;
2546 }
2547
2548 HAPI struct pkg_info *const instance_package(const struct inst_info *inst)
2549 {
2550         return inst->info;
2551 }
2552
2553 HAPI struct script_info *const instance_lb_script(const struct inst_info *inst)
2554 {
2555         return (package_lb_type(inst->info) == LB_TYPE_SCRIPT) ? inst->lb.canvas.script : NULL;
2556 }
2557
2558 HAPI struct script_info *const instance_pd_script(const struct inst_info *inst)
2559 {
2560         return (package_pd_type(inst->info) == PD_TYPE_SCRIPT) ? inst->pd.canvas.script : NULL;
2561 }
2562
2563 HAPI struct buffer_info *const instance_lb_buffer(const struct inst_info *inst)
2564 {
2565         return (package_lb_type(inst->info) == LB_TYPE_BUFFER) ? inst->lb.canvas.buffer : NULL;
2566 }
2567
2568 HAPI struct buffer_info *const instance_pd_buffer(const struct inst_info *inst)
2569 {
2570         return (package_pd_type(inst->info) == PD_TYPE_BUFFER) ? inst->pd.canvas.buffer : NULL;
2571 }
2572
2573 HAPI const char *const instance_id(const struct inst_info *inst)
2574 {
2575         return inst->id;
2576 }
2577
2578 HAPI const char *const instance_content(const struct inst_info *inst)
2579 {
2580         return inst->content;
2581 }
2582
2583 HAPI const char *const instance_category(const struct inst_info *inst)
2584 {
2585         return inst->category;
2586 }
2587
2588 HAPI const char *const instance_cluster(const struct inst_info *inst)
2589 {
2590         return inst->cluster;
2591 }
2592
2593 HAPI const char * const instance_title(const struct inst_info *inst)
2594 {
2595         return inst->title;
2596 }
2597
2598 HAPI const double const instance_timestamp(const struct inst_info *inst)
2599 {
2600         return inst->timestamp;
2601 }
2602
2603 HAPI const enum instance_state const instance_state(const struct inst_info *inst)
2604 {
2605         return inst->state;
2606 }
2607
2608 HAPI int instance_destroyed(struct inst_info *inst)
2609 {
2610         switch (inst->state) {
2611         case INST_INIT:
2612         case INST_REQUEST_TO_ACTIVATE:
2613                 /*!
2614                  * \note
2615                  * No other clients know the existence of this instance,
2616                  * only who added this knows it.
2617                  * So send deleted event to only it.
2618                  */
2619                 DbgPrint("Send deleted event - unicast - %p\n", inst->client);
2620                 instance_unicast_deleted_event(inst, NULL);
2621                 instance_state_reset(inst);
2622                 instance_destroy(inst);
2623                 break;
2624         case INST_REQUEST_TO_REACTIVATE:
2625         case INST_REQUEST_TO_DESTROY:
2626         case INST_ACTIVATED:
2627                 DbgPrint("Send deleted event - multicast\n");
2628                 instance_broadcast_deleted_event(inst);
2629                 instance_state_reset(inst);
2630                 instance_destroy(inst);
2631         case INST_DESTROYED:
2632                 break;
2633         default:
2634                 return LB_STATUS_ERROR_INVALID;
2635         }
2636
2637         return LB_STATUS_SUCCESS;
2638 }
2639
2640 /*!
2641  * Invoked when a slave is activated
2642  */
2643 HAPI int instance_recover_state(struct inst_info *inst)
2644 {
2645         int ret = 0;
2646
2647         if (inst->changing_state) {
2648                 DbgPrint("Doesn't need to recover the state\n");
2649                 return LB_STATUS_SUCCESS;
2650         }
2651
2652         switch (inst->state) {
2653         case INST_ACTIVATED:
2654         case INST_REQUEST_TO_REACTIVATE:
2655         case INST_REQUEST_TO_DESTROY:
2656                 switch (inst->requested_state) {
2657                 case INST_ACTIVATED:
2658                         DbgPrint("Req. to RE-ACTIVATED (%s)\n", package_name(inst->info));
2659                         instance_state_reset(inst);
2660                         instance_reactivate(inst);
2661                         ret = 1;
2662                         break;
2663                 case INST_DESTROYED:
2664                         DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
2665                         instance_state_reset(inst);
2666                         instance_destroy(inst);
2667                         break;
2668                 default:
2669                         break;
2670                 }
2671                 break;
2672         case INST_INIT:
2673         case INST_REQUEST_TO_ACTIVATE:
2674                 switch (inst->requested_state) {
2675                 case INST_ACTIVATED:
2676                 case INST_INIT:
2677                         DbgPrint("Req. to ACTIVATED (%s)\n", package_name(inst->info));
2678                         instance_state_reset(inst);
2679                         if (instance_activate(inst) < 0) {
2680                                 DbgPrint("Failed to reactivate the instance\n");
2681                                 instance_broadcast_deleted_event(inst);
2682                                 instance_state_reset(inst);
2683                                 instance_destroy(inst);
2684                         } else {
2685                                 ret = 1;
2686                         }
2687                         break;
2688                 case INST_DESTROYED:
2689                         DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
2690                         instance_state_reset(inst);
2691                         instance_destroy(inst);
2692                         ret = 1;
2693                         break;
2694                 default:
2695                         break;
2696                 }
2697                 break;
2698         case INST_DESTROYED:
2699         default:
2700                 break;
2701         }
2702
2703         return ret;
2704 }
2705
2706 /*!
2707  * Invoked when a slave is deactivated
2708  */
2709 HAPI int instance_need_slave(struct inst_info *inst)
2710 {
2711         int ret = 0;
2712
2713         if (inst->client && client_is_faulted(inst->client)) {
2714                 /*!
2715                  * \note
2716                  * In this case, the client is faulted(disconnected)
2717                  * when the client is deactivated, its liveboxes should be removed too.
2718                  * So if the current inst is created by the faulted client,
2719                  * remove it and don't try to recover its states
2720                  */
2721
2722                 DbgPrint("CLIENT FAULT: Req. to DESTROYED (%s)\n", package_name(inst->info));
2723                 switch (inst->state) {
2724                 case INST_INIT:
2725                 case INST_ACTIVATED:
2726                 case INST_REQUEST_TO_REACTIVATE:
2727                 case INST_REQUEST_TO_DESTROY:
2728                 case INST_REQUEST_TO_ACTIVATE:
2729                         instance_state_reset(inst);
2730                         instance_destroy(inst);
2731                         break;
2732                 case INST_DESTROYED:
2733                         break;
2734                 }
2735
2736                 return LB_STATUS_SUCCESS;
2737         }
2738
2739         switch (inst->state) {
2740         case INST_ACTIVATED:
2741         case INST_REQUEST_TO_REACTIVATE:
2742         case INST_REQUEST_TO_DESTROY:
2743                 switch (inst->requested_state) {
2744                 case INST_INIT:
2745                 case INST_ACTIVATED:
2746                         DbgPrint("Req. to ACTIVATED (%s)\n", package_name(inst->info));
2747                         ret = 1;
2748                         break;
2749                 case INST_DESTROYED:
2750                         DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
2751                         instance_state_reset(inst);
2752                         instance_destroy(inst);
2753                         break;
2754                 default:
2755                         break;
2756                 }
2757                 break;
2758         case INST_INIT:
2759         case INST_REQUEST_TO_ACTIVATE:
2760                 switch (inst->requested_state) {
2761                 case INST_INIT:
2762                 case INST_ACTIVATED:
2763                         DbgPrint("Req. to ACTIVATED (%s)\n", package_name(inst->info));
2764                         ret = 1;
2765                         break;
2766                 case INST_DESTROYED:
2767                         DbgPrint("Req. to DESTROYED (%s)\n", package_name(inst->info));
2768                         instance_state_reset(inst);
2769                         instance_destroy(inst);
2770                         break;
2771                 default:
2772                         break;
2773                 }
2774                 break;
2775         case INST_DESTROYED:
2776         default:
2777                 break;
2778         }
2779
2780         return ret;
2781 }
2782
2783 HAPI int instance_forward_packet(struct inst_info *inst, struct packet *packet)
2784 {
2785         return CLIENT_SEND_EVENT(inst, packet);
2786 }
2787
2788 HAPI int instance_send_access_status(struct inst_info *inst, int status)
2789 {
2790         struct packet *packet;
2791
2792         packet = packet_create_noack("access_status", "ssi", package_name(inst->info), inst->id, status);
2793         if (!packet) {
2794                 ErrPrint("Failed to build a packet\n");
2795                 return LB_STATUS_ERROR_FAULT;
2796         }
2797
2798         return CLIENT_SEND_EVENT(inst, packet);
2799 }
2800
2801 HAPI void instance_slave_set_pd_pos(struct inst_info *inst, double x, double y)
2802 {
2803         inst->pd.x = x;
2804         inst->pd.y = y;
2805 }
2806
2807 HAPI void instance_slave_get_pd_pos(struct inst_info *inst, double *x, double *y)
2808 {
2809         if (x)
2810                 *x = inst->pd.x;
2811         if (y)
2812                 *y = inst->pd.y;
2813 }
2814
2815 HAPI int instance_slave_open_pd(struct inst_info *inst, struct client_node *client)
2816 {
2817         const char *pkgname;
2818         const char *id;
2819         struct packet *packet;
2820         struct slave_node *slave;
2821         const struct pkg_info *info;
2822         int ret;
2823
2824         if (!client) {
2825                 client = inst->pd.owner;
2826                 if (!client) {
2827                         ErrPrint("Client is not valid\n");
2828                         return LB_STATUS_ERROR_INVALID;
2829                 }
2830         } else if (inst->pd.owner) {
2831                 if (inst->pd.owner != client) {
2832                         ErrPrint("Client is already owned\n");
2833                         return LB_STATUS_ERROR_ALREADY;
2834                 }
2835         }
2836
2837         slave = package_slave(instance_package(inst));
2838         if (!slave)
2839                 return LB_STATUS_ERROR_FAULT;
2840
2841         info = instance_package(inst);
2842         if (!info)
2843                 return LB_STATUS_ERROR_INVALID;
2844
2845         pkgname = package_name(info);
2846         id = instance_id(inst);
2847
2848         if (!pkgname || !id)
2849                 return LB_STATUS_ERROR_INVALID;
2850
2851         packet = packet_create_noack("pd_show", "ssiidd", pkgname, id, instance_pd_width(inst), instance_pd_height(inst), inst->pd.x, inst->pd.y);
2852         if (!packet) {
2853                 ErrPrint("Failed to create a packet\n");
2854                 return LB_STATUS_ERROR_FAULT;
2855         }
2856
2857         /*!
2858          * \note
2859          * Do not return from here even though we failed to freeze the TTL timer.
2860          * Because the TTL timer is not able to be exists.
2861          * So we can ignore this error.
2862          */
2863         (void)slave_freeze_ttl(slave);
2864
2865         ret = slave_rpc_request_only(slave, pkgname, packet, 0);
2866         if (ret < 0) {
2867                 ErrPrint("Unable to send request to slave\n");
2868                 /*!
2869                  * \note
2870                  * Also we can ignore the TTL timer at here too ;)
2871                  */
2872                 (void)slave_thaw_ttl(slave);
2873                 return ret;
2874         }
2875
2876         /*!
2877          * \note
2878          * If a client is disconnected, the slave has to close the PD
2879          * So the pd_buffer_close_cb/pd_script_close_cb will catch the disconnection event
2880          * then it will send the close request to the slave
2881          */
2882         if (package_pd_type(info) == PD_TYPE_BUFFER) {
2883                 instance_ref(inst);
2884                 if (client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, pd_buffer_close_cb, inst) < 0)
2885                         instance_unref(inst);
2886         } else if (package_pd_type(info) == PD_TYPE_SCRIPT) {
2887                 instance_ref(inst);
2888                 if (client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, pd_script_close_cb, inst) < 0)
2889                         instance_unref(inst);
2890         }
2891
2892         inst->pd.owner = client;
2893         DbgPrint("pd,show event is sent\n");
2894         return ret;
2895 }
2896
2897 HAPI int instance_slave_close_pd(struct inst_info *inst, struct client_node *client)
2898 {
2899         const char *pkgname;
2900         const char *id;
2901         struct packet *packet;
2902         struct slave_node *slave;
2903         struct pkg_info *pkg;
2904         int ret;
2905
2906         if (inst->pd.owner != client)
2907                 return LB_STATUS_ERROR_INVALID;
2908
2909         slave = package_slave(instance_package(inst));
2910         if (!slave)
2911                 return LB_STATUS_ERROR_FAULT;
2912
2913         pkg = instance_package(inst);
2914         if (!pkg)
2915                 return LB_STATUS_ERROR_INVALID;
2916
2917         pkgname = package_name(pkg);
2918         id = instance_id(inst);
2919
2920         if (!pkgname || !id)
2921                 return LB_STATUS_ERROR_INVALID;
2922
2923         packet = packet_create_noack("pd_hide", "ss", pkgname, id);
2924         if (!packet)
2925                 return LB_STATUS_ERROR_FAULT;
2926
2927         slave_thaw_ttl(slave);
2928
2929         ret = slave_rpc_request_only(slave, pkgname, packet, 0);
2930         release_resource_for_closing_pd(pkg, inst, client);
2931         inst->pd.owner = NULL;
2932         return ret;
2933 }
2934
2935 HAPI int instance_client_pd_created(struct inst_info *inst, int status)
2936 {
2937         struct packet *packet;
2938         const char *buf_id;
2939         int ret;
2940
2941         if (inst->pd.need_to_send_close_event) {
2942                 DbgPrint("PD is already created\n");
2943                 return LB_STATUS_ERROR_INVALID;
2944         }
2945
2946         switch (package_pd_type(inst->info)) {
2947         case PD_TYPE_SCRIPT:
2948                 buf_id = fb_id(script_handler_fb(inst->pd.canvas.script));
2949                 break;
2950         case PD_TYPE_BUFFER:
2951                 buf_id = buffer_handler_id(inst->pd.canvas.buffer);
2952                 break;
2953         case PD_TYPE_TEXT:
2954         default:
2955                 buf_id = "";
2956                 break;
2957         }
2958
2959         inst->pd.need_to_send_close_event = (status == 0);
2960
2961         packet = packet_create_noack("pd_created", "sssiii", 
2962                         package_name(inst->info), inst->id, buf_id,
2963                         inst->pd.width, inst->pd.height, status);
2964         if (!packet) {
2965                 ErrPrint("Failed to create a packet\n");
2966                 return LB_STATUS_ERROR_FAULT;
2967         }
2968
2969         ret = CLIENT_SEND_EVENT(inst, packet);
2970
2971         if (inst->pd.need_to_send_close_event && inst->pd.pended_update_cnt) {
2972                 DbgPrint("Apply pended desc(%d) - %s\n", inst->pd.pended_update_cnt, inst->pd.pended_update_desc);
2973                 instance_pd_updated_by_instance(inst, inst->pd.pended_update_desc);
2974                 inst->pd.pended_update_cnt = 0;
2975                 DbgFree(inst->pd.pended_update_desc);
2976                 inst->pd.pended_update_desc = NULL;
2977         }
2978
2979         return ret;
2980 }
2981
2982 HAPI int instance_client_pd_destroyed(struct inst_info *inst, int status)
2983 {
2984         if (!inst->pd.need_to_send_close_event) {
2985                 DbgPrint("PD is not created\n");
2986                 return LB_STATUS_ERROR_INVALID;
2987         }
2988
2989         inst->pd.need_to_send_close_event = 0;
2990
2991         return send_pd_destroyed_to_client(inst, status);
2992 }
2993
2994 HAPI int instance_add_client(struct inst_info *inst, struct client_node *client)
2995 {
2996         if (inst->client == client) {
2997                 ErrPrint("Owner cannot be the viewer\n");
2998                 return LB_STATUS_ERROR_INVALID;
2999         }
3000
3001         DbgPrint("%d is added to the list of viewer of %s(%s)\n", client_pid(client), package_name(instance_package(inst)), instance_id(inst));
3002         if (client_event_callback_add(client, CLIENT_EVENT_DEACTIVATE, viewer_deactivated_cb, inst) < 0) {
3003                 ErrPrint("Failed to add a deactivate callback\n");
3004                 return LB_STATUS_ERROR_FAULT;
3005         }
3006
3007         instance_ref(inst);
3008         inst->client_list = eina_list_append(inst->client_list, client);
3009         return LB_STATUS_SUCCESS;
3010 }
3011
3012 HAPI int instance_del_client(struct inst_info *inst, struct client_node *client)
3013 {
3014         if (inst->client == client) {
3015                 ErrPrint("Owner is not in the viewer list\n");
3016                 return LB_STATUS_ERROR_INVALID;
3017         }
3018
3019         client_event_callback_del(client, CLIENT_EVENT_DEACTIVATE, viewer_deactivated_cb, inst);
3020         viewer_deactivated_cb(client, inst);
3021         return LB_STATUS_SUCCESS;
3022 }
3023
3024 HAPI int instance_has_client(struct inst_info *inst, struct client_node *client)
3025 {
3026         return !!eina_list_data_find(inst->client_list, client);
3027 }
3028
3029 HAPI void *instance_client_list(struct inst_info *inst)
3030 {
3031         return inst->client_list;
3032 }
3033
3034 HAPI int instance_init(void)
3035 {
3036         if (!strcasecmp(PROVIDER_METHOD, "shm"))
3037                 s_info.env_buf_type = BUFFER_TYPE_SHM;
3038         else if (!strcasecmp(PROVIDER_METHOD, "pixmap"))
3039                 s_info.env_buf_type = BUFFER_TYPE_PIXMAP;
3040         /* Default method is BUFFER_TYPE_FILE */
3041
3042         return LB_STATUS_SUCCESS;
3043 }
3044
3045 HAPI int instance_fini(void)
3046 {
3047         return LB_STATUS_SUCCESS;
3048 }
3049
3050 static inline struct tag_item *find_tag_item(struct inst_info *inst, const char *tag)
3051 {
3052         struct tag_item *item;
3053         Eina_List *l;
3054
3055         EINA_LIST_FOREACH(inst->data_list, l, item) {
3056                 if (!strcmp(item->tag, tag))
3057                         return item;
3058         }
3059
3060         return NULL;
3061 }
3062
3063 HAPI int instance_set_data(struct inst_info *inst, const char *tag, void *data)
3064 {
3065         struct tag_item *item;
3066
3067         item = find_tag_item(inst, tag);
3068         if (!item) {
3069                 item = malloc(sizeof(*item));
3070                 if (!item) {
3071                         ErrPrint("Heap: %s\n", strerror(errno));
3072                         return LB_STATUS_ERROR_MEMORY;
3073                 }
3074
3075                 item->tag = strdup(tag);
3076                 if (!item->tag) {
3077                         ErrPrint("Heap: %s\n", strerror(errno));
3078                         free(item);
3079                         return LB_STATUS_ERROR_MEMORY;
3080                 }
3081
3082                 inst->data_list = eina_list_append(inst->data_list, item);
3083         }
3084
3085         if (!data) {
3086                 inst->data_list = eina_list_remove(inst->data_list, item);
3087                 free(item->tag);
3088                 free(item);
3089         } else {
3090                 item->data = data;
3091         }
3092
3093         return LB_STATUS_SUCCESS;
3094 }
3095
3096 HAPI void *instance_del_data(struct inst_info *inst, const char *tag)
3097 {
3098         struct tag_item *item;
3099         void *data;
3100
3101         item = find_tag_item(inst, tag);
3102         if (!item)
3103                 return NULL;
3104
3105         inst->data_list = eina_list_remove(inst->data_list, item);
3106         data = item->data;
3107         free(item->tag);
3108         free(item);
3109
3110         return data;
3111 }
3112
3113 HAPI void *instance_get_data(struct inst_info *inst, const char *tag)
3114 {
3115         struct tag_item *item;
3116
3117         item = find_tag_item(inst, tag);
3118         if (!item)
3119                 return NULL;
3120
3121         return item->data;
3122 }
3123
3124 /* End of a file */