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