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