summaryrefslogtreecommitdiff
path: root/src/widget_instance.c
blob: f6110e248ec95793ccd2271dc7a88066ba2afee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
/*
 * Copyright 2015  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "widget_instance.h"
#include <dlog.h>
#include "debug.h"
#include <uuid/uuid.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <glib.h>
#include <aul.h>
#include <aul_svc.h>
#include <aul_app_com.h>
#include <widget_service.h>
#include <app.h>

#define USER_UID_MIN 5000
#define MAX_INSTANCE_ID_LEN 256
#define WIDGET_CLASS_DELIMITER "@"
#define QUERY_MAX_LEN 8192

#ifndef TIZEN_PATH_MAX
#define TIZEN_PATH_MAX 1024
#endif

enum {
	WIDGET_INSTANCE_CREATED,
	WIDGET_INSTANCE_RUNNING,
	WIDGET_INSTANCE_TERMINATED,
	WIDGET_INSTANCE_DELETED
};

struct _widget_instance {
	char *id;
	pid_t pid;
	char *widget_id;
	int w;
	int h;
	double period;
	bundle *content_info;
	int status;
	int stored;
	int ref;
};

struct widget_app {
	char *viewer_id;
	char *widget_id;
	GList *instances;
};

static char *wayland_display = NULL;
static char *xdg_runtime_dir = NULL;

static GList *_widget_instances = NULL;
static GList *_widget_apps = NULL;

static char *viewer_appid = NULL;
static aul_app_com_connection_h conn_viewer;
static aul_app_com_connection_h conn_status;

static GHashTable *lifecycle_tbl;
static int is_status_handler_connected;

struct lifecycle_local_s {
	char *widget_id;
	widget_instance_event_cb cb;
	void *data;
};

static GList *event_cbs;
struct event_cb_s {
	widget_instance_event_cb cb;
	void *data;
};


static int __fini(void)
{

	return 0;
}

static struct _widget_instance *__pick_instance(const char *widget_id, const char *instance_id)
{
	GList *apps = _widget_apps;
	GList *instances = NULL;
	struct widget_app *app = NULL;
	struct _widget_instance *instance = NULL;

	while (apps) {
		app = apps->data;
		if (app && g_strcmp0(widget_id, app->widget_id) == 0) {
			instances = app->instances;
			while (instances) {
				instance = instances->data;
				if (instance && g_strcmp0(instance_id, instance->id) == 0)
					return instance;
				instances = instances->next;
			}
		}
		apps = apps->next;
	}

	return NULL;
}

static struct widget_app *__pick_app(const char *widget_id)
{
	GList *apps = _widget_apps;
	struct widget_app *app = NULL;

	while (apps) {
		app = apps->data;
		if (app && app->widget_id && g_strcmp0(widget_id, app->widget_id) == 0)
			return app;

		apps = apps->next;
	}

	return NULL;
}

static struct widget_app *__add_app(const char *widget_id, const char *viewer_id)
{
	struct widget_app *app = NULL;

	app = (struct widget_app *)malloc(sizeof(struct widget_app));
	if (app == NULL) {
		_E("out of memory");
		return NULL;
	}

	app->viewer_id = strdup(viewer_id);
	app->widget_id = strdup(widget_id);
	app->instances = NULL;

	_widget_apps = g_list_append(_widget_apps, app);

	return app;
}

static char *__create_instance_id(const char *widget_id)
{
	char uuid[37];
	char instance_id[MAX_INSTANCE_ID_LEN];
	uuid_t u;
	struct widget_app *app = NULL;

	if (widget_id == NULL)
		return NULL;

	app = __pick_app(widget_id);

	if (app == NULL) {
		app = __add_app(widget_id, viewer_appid);
		if (app == NULL)
			return NULL;
	}

	uuid_generate(u);
	uuid_unparse(u, uuid);

	snprintf(instance_id, MAX_INSTANCE_ID_LEN, "%s:%s", uuid, widget_id);

	_D("new instance: %s", instance_id);

	return strdup(instance_id);
}

static struct _widget_instance *__add_instance(const char *id, const char *widget_id)
{
	struct _widget_instance *instance = NULL;
	struct widget_app *app = NULL;

	instance = (struct _widget_instance *)malloc(sizeof(struct _widget_instance));
	if (instance == NULL) {
		_E("out of memory");
		return NULL;
	}

	instance->status = WIDGET_INSTANCE_CREATED;
	instance->id = strdup(id);
	instance->pid = 0;
	instance->stored = 0;
	instance->widget_id = strdup(widget_id);
	instance->content_info = NULL;
	instance->ref = 0;

	_widget_instances = g_list_append(_widget_instances, instance);

	app = __pick_app(widget_id);
	if (!app)
		app = __add_app(widget_id, viewer_appid);

	if (app)
		app->instances = g_list_append(app->instances, instance);
	else
		_E("failed to find app: %s", widget_id);

	return instance;
}

static void __remove_instance(struct _widget_instance *instance)
{
	struct widget_app *app = NULL;

	if (instance == NULL) {
		_E("invalid argument");
		return;
	}

	if (instance->widget_id)
		app = __pick_app(instance->widget_id);

	if (app) {
		app->instances = g_list_remove(app->instances, instance);
		if (app->instances == NULL) {
			_widget_apps = g_list_remove(_widget_apps, app);
			free(app);
			app = NULL;
		}
	}

	_widget_instances = g_list_remove(_widget_instances, instance);

	if (instance->widget_id) {
		free(instance->widget_id);
		instance->widget_id = NULL;
	}

	if (instance->id) {
		free(instance->id);
		instance->id = NULL;
	}

	if (instance->content_info) {
		bundle_free(instance->content_info);
		instance->content_info = NULL;
	}

	free(instance);
}

EAPI int widget_instance_create(const char *widget_id, char **instance_id)
{
	char *id = NULL;
	struct _widget_instance *instance = NULL;

	_D("create: %s", widget_id);

	id = __create_instance_id(widget_id);
	if (!id) {
		_E("failed to get instance id for %s", widget_id);
		return -1;
	}

	instance = __add_instance(id, widget_id);

	free((gpointer)id);

	if (instance) {
		*instance_id = instance->id;
		_D("create done");
		return 0;
	}

	*instance_id = NULL;

	return -1;
}

static int __send_aul_cmd(const char *widget_id, const char *instance_id, bundle *extra)
{
	int ret = 0;
	const char *appid;
	const char *classid = widget_id;
	bundle *b = extra;

	appid = g_strstr_len(widget_id, strlen(widget_id), WIDGET_CLASS_DELIMITER) + 1;
	if (appid != (const char *)1) { /* move pointer next to delimiter */
		if (appid > widget_id + (sizeof(char) * strlen(widget_id)))
			return -1; /* bad ptr */
	} else {
		appid = widget_id;
	}

	if (b == NULL)
		b = bundle_create();

	bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
	bundle_add_str(b, WIDGET_K_CLASS, classid);
	bundle_add_str(b, AUL_K_WIDGET_VIEWER, viewer_appid);

	aul_svc_set_loader_id(b, 1);
	aul_svc_set_operation(b, AUL_SVC_OPERATION_LAUNCH_WIDGET);

	ret = aul_launch_app(appid, b);

	if (!extra) {
		bundle_free(b);
		b = NULL;
	}

	return ret;
}

static int __set_width(bundle *content_info, int w)
{
	char wbuf[6];
	if (content_info == NULL || w < 0)
		return -1;

	snprintf(wbuf, 6, "%d", w);
	bundle_add_str(content_info, WIDGET_K_WIDTH, wbuf);

	return 0;
}

static int __set_height(bundle *content_info, int h)
{
	char hbuf[6];
	if (content_info == NULL || h < 0)
		return -1;

	snprintf(hbuf, 6, "%d", h);
	bundle_add_str(content_info, WIDGET_K_HEIGHT, hbuf);

	return 0;
}

EAPI int widget_instance_launch(const char *widget_id, const char *instance_id, bundle *content_info, int w, int h)
{
	int ret = 0;
	char pid_buf[6];
	bundle *b = content_info;
	char *instance = (char *)instance_id;

	_D("launch: %s %s", widget_id, instance_id);

	if (widget_id == NULL) {
		_E("wrong arguments");
		return -1;
	}

	if (xdg_runtime_dir == NULL) {
		xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
		if (xdg_runtime_dir == NULL) {
			_E("failed to get XDG_RUNTIME_DIR");
			return -1;
		}
	}

	if (wayland_display == NULL) {
		wayland_display = getenv("WAYLAND_DISPLAY");
		if (wayland_display == NULL) {
			_E("unable to get wayland display port");
			return -1;
		}
	}

	if (instance == NULL) {
		ret = widget_instance_create(widget_id, &instance);
		if (ret < 0 || instance == NULL) {
			_E("failed to create instance for %s", widget_id);
			return -1;
		}
	} else {
		if (__pick_instance(widget_id, instance) == NULL) {
			__add_instance(instance, widget_id);
		}
	}

	if (b == NULL) {
		b = bundle_create();
		if (b == NULL) {
			_E("out of memory");
			return -1;
		}
	}

	snprintf(pid_buf, sizeof(pid_buf), "%d", getpid());
	bundle_add_str(b, WIDGET_K_CALLER, pid_buf);
	bundle_add_str(b, WIDGET_K_ENDPOINT, viewer_appid);
	bundle_add_str(b, AUL_K_WAYLAND_DISPLAY, wayland_display);
	bundle_add_str(b, AUL_K_WAYLAND_WORKING_DIR, xdg_runtime_dir);
	bundle_add_str(b, WIDGET_K_OPERATION, "create");

	__set_width(b, w);
	__set_height(b, h);

	ret = __send_aul_cmd(widget_id, instance, b);

	if (ret) {
		struct _widget_instance *i = __pick_instance(widget_id, instance);
		if (i)
			i->pid = ret;
	}

	if (content_info == NULL)
		bundle_free(b);

	return ret;
}

EAPI int widget_instance_terminate(const char *widget_id, const char *instance_id)
{
	int ret = 0;
	bundle *b = NULL;
	struct _widget_instance *instance;

	if (widget_id == NULL || instance_id == NULL)
		return -1;

	instance = __pick_instance(widget_id, instance_id);
	if (!instance) {
		_E("illegal operation: termiante (instance not yet initialized: %s)", instance_id);
		return -1;
	}

	if (instance->status != WIDGET_INSTANCE_RUNNING) {
		_E("illegal operation: terminate (wrong status: %s %d)", instance_id, instance->status);
		return -1;
	}

	b = bundle_create();
	if (b == NULL)
		return -1;

	bundle_add_str(b, WIDGET_K_OPERATION, "terminate");

	ret = __send_aul_cmd(widget_id, instance_id, b);

	bundle_free(b);

	return ret;
}

EAPI int widget_instance_destroy(const char *widget_id, const char *instance_id)
{
	int ret = 0;
	bundle *b = NULL;
	struct _widget_instance *instance;

	if (widget_id == NULL || instance_id == NULL)
		return -1;

	instance = __pick_instance(widget_id, instance_id);
	if (!instance) {
		_E("illegal operation: destroy (instance not yet initialized: %s)", instance_id);
		return -1;
	}

	if (instance->status != WIDGET_INSTANCE_RUNNING) {
		_E("illegal operation: destroy (wrong status: %s %d)", instance_id, instance->status);
		return -1;
	}

	b = bundle_create();
	if (b == NULL)
		return -1;

	bundle_add_str(b, WIDGET_K_OPERATION, "destroy");

	ret = __send_aul_cmd(widget_id, instance_id, b);

	bundle_free(b);

	return ret;
}

EAPI int widget_instance_resume(const char  *widget_id, const char *instance_id)
{
	int ret = 0;
	bundle *b = NULL;
	struct _widget_instance *instance;

	if (widget_id == NULL || instance_id == NULL)
		return -1;

	instance = __pick_instance(widget_id, instance_id);
	if (!instance) {
		_E("illegal operation: resume (instance not yet initialized: %s)", instance_id);
		return -1;
	}

	if (instance->status != WIDGET_INSTANCE_RUNNING) {
		_E("illegal operation: resume (wrong status: %s %d)", instance_id, instance->status);
		return -1;
	}

	b = bundle_create();
	if (b == NULL)
		return -1;

	bundle_add_str(b, WIDGET_K_OPERATION, "resume");

	ret = __send_aul_cmd(widget_id, instance_id, b);

	bundle_free(b);

	return ret;
}

EAPI int widget_instance_pause(const char *widget_id, const char *instance_id)
{
	int ret = 0;
	bundle *b = NULL;
	struct _widget_instance *instance;

	if (widget_id == NULL || instance_id == NULL)
		return -1;

	instance = __pick_instance(widget_id, instance_id);
	if (!instance) {
		_E("illegal operation: pause (instance not yet initialized: %s)", instance_id);
		return -1;
	}

	if (instance->status != WIDGET_INSTANCE_RUNNING) {
		_E("illegal operation: pause (wrong status: %s %d)", instance_id, instance->status);
		return -1;
	}

	b = bundle_create();
	if (b == NULL)
		return -1;

	bundle_add_str(b, WIDGET_K_OPERATION, "pause");

	ret = __send_aul_cmd(widget_id, instance_id, b);

	bundle_free(b);

	return ret;
}

EAPI int widget_instance_resize(const char *widget_id, const char *instance_id, int w, int h)
{
	int ret = 0;
	bundle *b = NULL;
	struct _widget_instance *instance;

	if (widget_id == NULL || instance_id == NULL)
		return -1;

	instance = __pick_instance(widget_id, instance_id);
	if (!instance) {
		_E("illegal operation: resize (instance not yet initialized: %s)", instance_id);
		return -1;
	}

	if (instance->status != WIDGET_INSTANCE_RUNNING) {
		_E("illegal operation: resize (wrong status: %s %d)", instance_id, instance->status);
		return -1;
	}

	b = bundle_create();
	if (b == NULL)
		return -1;

	bundle_add_str(b, WIDGET_K_OPERATION, "resize");

	__set_width(b, w);
	__set_height(b, h);

	ret = __send_aul_cmd(widget_id, instance_id, b);

	bundle_free(b);

	return ret;
}

EAPI int widget_instance_foreach(const char *widget_id, widget_instance_foreach_cb cb, void *data)
{
	GList *apps = _widget_apps;
	GList *instances = NULL;
	struct widget_app *app = NULL;
	struct _widget_instance *instance = NULL;

	if (widget_id == NULL || cb == NULL)
		return -1;

	while (apps) {
		app = apps->data;
		if (app && g_strcmp0(app->widget_id, widget_id) == 0) {
			instances = app->instances;
			while (instances) {
				instance = instances->data;
				if (instance && cb(instance, data) < 0)
					break;

				instances = instances->next;
			}
		}
		apps = apps->next;
	}

	return 0;
}

static void __notify_event(int event, const char *widget_id, const char *instance_id)
{
	struct event_cb_s *cb_info;
	GList *iter = event_cbs;

	while (iter) {
		cb_info = (struct event_cb_s *)iter->data;
		if (cb_info && cb_info->cb)
			cb_info->cb(widget_id, instance_id, event, cb_info->data);

		iter = iter->next;
	}
}

static int __status_handler(const char *endpoint, aul_app_com_result_e e, bundle *envelope, void *user_data)
{
	char *widget_id;
	char *instance_id;
	int *status;
	size_t status_sz = 0;
	struct lifecycle_local_s *cb_info;

	bundle_get_str(envelope, WIDGET_K_ID, &widget_id);
	bundle_get_str(envelope, WIDGET_K_INSTANCE, &instance_id);
	bundle_get_byte(envelope, WIDGET_K_STATUS, (void **)&status, &status_sz);

	if (widget_id == NULL || instance_id == NULL || status == NULL) {
		_E("undefined class or instance %s of %s", instance_id, widget_id);
		if (status != NULL)
			_E("status: %d", *status);

		return 0;
	}

	cb_info = (struct lifecycle_local_s *)g_hash_table_lookup(lifecycle_tbl, "NULL");
	if (cb_info)
		cb_info->cb(widget_id, instance_id, *status, cb_info->data);

	cb_info = (struct lifecycle_local_s *)g_hash_table_lookup(lifecycle_tbl, widget_id);
	if (cb_info)
		cb_info->cb(widget_id, instance_id, *status, cb_info->data);

	return 0;
}

static int __connect_status_handler()
{
	if (is_status_handler_connected)
		return 0;

	if (aul_app_com_create("widget.status", NULL, __status_handler, NULL, &conn_status) < 0) {
		_E("failed to create status");
		return -1;
	}

	is_status_handler_connected = 1;

	return 0;
}

static int __widget_handler(const char *viewer_id, aul_app_com_result_e e, bundle *envelope, void *user_data)
{
	char *widget_id = NULL;
	char *instance_id = NULL;
	int *status = NULL;
	size_t status_sz = 0;
	int cmd = 0;
	struct _widget_instance *instance;

	bundle_get_str(envelope, WIDGET_K_ID, &widget_id);
	bundle_get_str(envelope, WIDGET_K_INSTANCE, &instance_id);
	bundle_get_byte(envelope, WIDGET_K_STATUS, (void **)&status, &status_sz);

	if (widget_id == NULL || instance_id == NULL || status == NULL) {
		_E("undefined class or instance %s of %s", instance_id, widget_id);
		if (status != NULL)
			_E("cmd: %d", *status);

		return 0;
	}

	_D("update status %s on %d", instance_id, *status);

	instance = __pick_instance(widget_id, instance_id);

	if (instance == NULL) {
		_E("undefined instance id: %s of %s", instance_id, widget_id);
		return 0;
	}

	cmd = *status;

	switch (cmd) {
	case WIDGET_INSTANCE_EVENT_CREATE:
		if (instance->content_info)
			bundle_free(instance->content_info);

		instance->content_info = bundle_dup(envelope);
		instance->status = WIDGET_INSTANCE_RUNNING;
		break;
	case WIDGET_INSTANCE_EVENT_TERMINATE:
		if (instance->content_info)
			bundle_free(instance->content_info);

		instance->content_info = bundle_dup(envelope);
		instance->status = WIDGET_INSTANCE_TERMINATED;
		break;
	case WIDGET_INSTANCE_EVENT_DESTROY:
		instance->status = WIDGET_INSTANCE_DELETED;
		break;
	case WIDGET_INSTANCE_EVENT_PAUSE:
		break;
	case WIDGET_INSTANCE_EVENT_RESUME:
		break;
	case WIDGET_INSTANCE_EVENT_UPDATE:
		break;
	case WIDGET_INSTANCE_EVENT_EXTRA_UPDATED:
		if (instance->content_info)
			bundle_free(instance->content_info);

		instance->content_info = bundle_dup(envelope);
		break;
	case WIDGET_INSTANCE_EVENT_FAULT:

		break;
	default:
		_E("unknown command: %d", cmd);
		break;
	}

	__notify_event(cmd, widget_id, instance_id);

	return 0;
}

static int __fault_handler(int pid, void *data)
{
	GList *iter;
	struct _widget_instance *instance;

	iter = _widget_instances;

	while (iter) {
		instance = (struct _widget_instance *)iter->data;
		if (instance && instance->pid == pid) {
			instance->pid = 0;
			instance->status = WIDGET_INSTANCE_TERMINATED;
			__notify_event(WIDGET_INSTANCE_EVENT_FAULT, instance->widget_id, instance->id);
		}
		iter = iter->next;
	}

	return 0;
}

EAPI int widget_instance_init(const char *viewer_id)
{
	if (viewer_id == NULL)
		return -1;

	viewer_appid = strdup(viewer_id);

	__connect_status_handler();
	aul_listen_app_dead_signal(__fault_handler, NULL);

	if (aul_app_com_create(viewer_id, NULL, __widget_handler, NULL, &conn_viewer) < 0) {
		_E("failed to create app com endpoint");
		return -1;
	}

	return 0;
}

EAPI int widget_instance_fini()
{

	if (conn_viewer) {
		if (aul_app_com_leave(conn_viewer) < 0)
			_E("failed to leave app com endpoint viewer");
	}

	if (conn_status) {
		if (aul_app_com_leave(conn_status) < 0)
			_E("failed to leave app com endpoint status");
	}

	__fini();

	if (viewer_appid) {
		free(viewer_appid);
		viewer_appid = NULL;
	}

	return 0;
}

EAPI int widget_instance_get_id(widget_instance_h instance, char **id)
{
	if (instance == NULL || id == NULL)
		return -1;

	*id = (char *)instance->id;
	return 0;
}

EAPI int widget_instance_get_content(widget_instance_h instance, bundle **content)
{
	if (instance == NULL || content == NULL)
		return -1;

	*content = instance->content_info;
	return 0;
}

EAPI int widget_instance_get_width(widget_instance_h instance, int *w)
{
	if (instance == NULL || w == NULL)
		return -1;

	*w = instance->w;
	return 0;
}

EAPI int widget_instance_get_height(widget_instance_h instance, int *h)
{
	if (instance == NULL || h == NULL)
		return -1;

	*h = instance->h;
	return 0;
}

EAPI int widget_instance_get_period(widget_instance_h instance, double *period)
{
	if (instance == NULL || period == NULL)
		return -1;

	*period = instance->period;
	return 0;
}

EAPI int widget_instance_change_period(widget_instance_h instance, double period)
{
	int ret;
	bundle *b;

	if (!instance)
		return -1;

	b = bundle_create();
	if (!b) {
		_E("out of memory");
		return -1;
	}

	bundle_add_str(b, WIDGET_K_OPERATION, "period");
	bundle_add_byte(b, WIDGET_K_PERIOD, &period, sizeof(double));

	ret = __send_aul_cmd(instance->widget_id, instance->id, b);

	bundle_free(b);

	if (ret > 0) {
		if (instance->pid != ret) {
			_E("instance %s(%d) has been launched with different pid.");
			instance->pid = ret;
		}
	}

	return ret;
}

EAPI int widget_instance_trigger_update(widget_instance_h instance, bundle *b, int force)
{
	int ret;
	bundle *kb = b;

	if (!instance)
		return -1;

	if (!kb) {
		kb = bundle_create();
		if (!kb) {
			_E("out of memory");
			return -1;
		}
	}

	bundle_add_str(kb, WIDGET_K_OPERATION, "update");

	if (force)
		bundle_add_str(kb, WIDGET_K_FORCE, "true");

	ret = __send_aul_cmd(instance->widget_id, instance->id, kb);

	if (!b)
		bundle_free(kb);

	if (ret > 0) {
		if (instance->pid != ret) {
			_E("instance %s(%d) has been launched with different pid.");
			instance->pid = ret;
		}
	}

	return ret;
}

EAPI widget_instance_h widget_instance_get_instance(const char *widget_id, const char *instance_id)
{
	widget_instance_h instance;

	if (widget_id == NULL || instance_id == NULL)
		return NULL;

	if (_widget_apps && _widget_instances) {
		instance = __pick_instance(widget_id, instance_id);
		return widget_instance_ref(instance);
	}

	return NULL;
}

EAPI int widget_instance_get_instance_list(const char *widget_id, widget_instance_list_cb cb, void *data)
{
	widget_instance_h instance;
	struct widget_app *app;
	GList *head = NULL;

	if (widget_id == NULL)
		return -1;

	if (_widget_apps)
		app = __pick_app(widget_id);
	else
		return -2;

	if (app) {
		head = app->instances;

		while (head) {
			instance = (widget_instance_h)head->data;
			if (cb(instance->widget_id, instance->id, data) != 0)
				break;

			head = head->next;
		}
	}

	return 0;
}

EAPI void widget_instance_unref(widget_instance_h instance)
{
	if (instance == NULL)
		return;

	instance->ref--;

	if (instance->ref > -1)
		return;

	__remove_instance(instance);
}

EAPI widget_instance_h widget_instance_ref(widget_instance_h instance)
{
	if (instance)
		instance->ref++;

	return instance;
}

EAPI int widget_instance_listen_event(widget_instance_event_cb cb, void *data)
{
	struct event_cb_s *cb_info;

	cb_info = (struct event_cb_s *)malloc(sizeof(struct event_cb_s));
	if (!cb_info) {
		_E("out of memory");
		return -1;
	}

	cb_info->cb = cb;
	cb_info->data = data;

	event_cbs = g_list_append(event_cbs, cb_info);
	if (!event_cbs) {
		_E("out of memory");
		return -1;
	}

	return 0;
}

EAPI int widget_instance_unlisten_event(widget_instance_event_cb cb)
{
	struct event_cb_s *cb_info;
	GList *iter = event_cbs;

	if (!cb) {
		_E("invalid parameter");
		return -1;
	}

	while (iter) {
		cb_info = (struct event_cb_s *)iter->data;
		if (cb_info && cb_info->cb == cb) {
			event_cbs = g_list_remove_link(event_cbs, iter);
			free(cb_info);
			g_list_free(iter);
			return 0;
		}
		iter = iter->next;
	}

	_E("wrong argument");
	return -1;
}

/* within package only */
EAPI int widget_instance_listen_status(const char *widget_id, widget_instance_event_cb cb, void *data)
{
	struct lifecycle_local_s *cb_info;

	if (!widget_id)
		widget_id = "NULL"; /* listen all widget */

	cb_info = (struct lifecycle_local_s *)malloc(sizeof(struct lifecycle_local_s));
	if (!cb_info) {
		_E("out of memory");
		return -1;
	}

	cb_info->widget_id = strdup(widget_id);
	cb_info->cb = cb;
	cb_info->data = data;
	if (!lifecycle_tbl) {
		lifecycle_tbl = g_hash_table_new(g_str_hash, g_str_equal);
		if (!lifecycle_tbl) {
			free(cb_info);
			return -1;
		}
	}

	g_hash_table_insert(lifecycle_tbl, cb_info->widget_id, cb_info);

	__connect_status_handler();

	return 0;
}

EAPI int widget_instance_unlisten_status(const char *widget_id)
{
	struct lifecycle_local_s *cb_info;

	if (!widget_id)
		widget_id = "NULL";

	cb_info = (struct lifecycle_local_s *)g_hash_table_lookup(lifecycle_tbl, widget_id);
	if (!cb_info)
		return -1;

	g_hash_table_remove(lifecycle_tbl, widget_id);
	free(cb_info->widget_id);
	free(cb_info);

	return 0;
}