summaryrefslogtreecommitdiff
path: root/src/manager.c
blob: 5c9cbed032162570a332a2b363d575c2ddc2bca1 (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
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 *  UI Gadget
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jayoun Lee <airjany@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>

#ifndef WAYLAND
#include <utilX.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <Ecore_X.h>
#endif

#include <Ecore.h>

#include "ug.h"
#include "ug-manager.h"
#include "ug-engine.h"
#include "ug-dbg.h"

#define Idle_Cb Ecore_Cb

#define ugman_idler_add(func, data)  \
	ecore_job_add((Ecore_Cb) func, (void *)data);

struct ug_manager {
	ui_gadget_h root;
	ui_gadget_h fv_top;
	GSList *fv_list;

	void *win;

#ifndef WAYLAND
	Window win_id;
	Display *disp;
#endif

	void *conform;

	enum ug_option base_opt;
	enum ug_event last_rotate_evt;

	int walking;

	int is_initted:1;
	int is_landscape:1;
	int destroy_all:1;

	struct ug_engine *engine;
};

static struct ug_manager ug_man;

static inline void job_start(void);
static inline void job_end(void);

static int ug_relation_add(ui_gadget_h p, ui_gadget_h c)
{
	c->parent = p;
	/* prepend element to avoid the inefficiency,
		which is to traverse the entire list to find the end*/
	p->children = g_slist_prepend(p->children, c);

	return 0;
}

static int ug_relation_del(ui_gadget_h ug)
{
	ui_gadget_h p;

	p = ug->parent;
	if (!p) {
		_WRN("ug_relation_del failed: no parent");
		return -1;
	}

	if(p->children) {
		p->children = g_slist_remove(p->children, ug);
	}

	if (ug->children) {
		g_slist_free(ug->children);
		ug->children = NULL;
	}

	ug->parent = NULL;

	return 0;
}

static int ug_fvlist_add(ui_gadget_h c)
{
	ug_man.fv_list = g_slist_prepend(ug_man.fv_list, c);
	ug_man.fv_top = c;

	return 0;
}

static int ug_fvlist_del(ui_gadget_h c)
{
	ui_gadget_h t;

	ug_man.fv_list = g_slist_remove(ug_man.fv_list, c);

	/* update fullview top ug*/
	t = g_slist_nth_data(ug_man.fv_list, 0);
	ug_man.fv_top = t;

	return 0;
}

#ifndef WAYLAND
static int __ug_x_get_window_property(Display *dpy, Window win, Atom atom,
					  Atom type, unsigned int *val,
					  unsigned int len)
{
	unsigned char *prop_ret;
	Atom type_ret;
	unsigned long bytes_after;
	unsigned long  num_ret;
	int format_ret;
	unsigned int i;
	int num;

	prop_ret = NULL;
	if (XGetWindowProperty(dpy, win, atom, 0, 0x7fffffff, False,
			       type, &type_ret, &format_ret, &num_ret,
			       &bytes_after, &prop_ret) != Success)
		return -1;

	if (type_ret != type || format_ret != 32)
		num = -1;
	else if (num_ret == 0 || !prop_ret)
		num = 0;
	else {
		if (num_ret < len)
			len = num_ret;
		for (i = 0; i < len; i++) {
			val[i] = ((unsigned long *)prop_ret)[i];
		}
		num = len;
	}

	if (prop_ret)
		XFree(prop_ret);

	return num;
}
#endif

#ifndef WAYLAND
static enum ug_event __ug_x_rotation_get(Display *dpy, Window win)
{
	Window active_win;
	Window root_win;
	int rotation = -1;
	int ret = -1;
	enum ug_event func_ret;

	Atom atom_active_win;
	Atom atom_win_rotate_angle;

	root_win = XDefaultRootWindow(dpy);

	atom_active_win = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
	ret = __ug_x_get_window_property(dpy, root_win, atom_active_win,
					     XA_WINDOW,
					     (unsigned int *)&active_win, 1);
	if (ret < 0) {
		func_ret = UG_EVENT_ROTATE_PORTRAIT;
		goto func_out;
	}

	atom_win_rotate_angle =
		XInternAtom(dpy, "_E_ILLUME_ROTATE_ROOT_ANGLE", False);
	ret = __ug_x_get_window_property(dpy, root_win,
					  atom_win_rotate_angle, XA_CARDINAL,
					  (unsigned int *)&rotation, 1);

	_DBG("x_rotation_get / ret(%d),degree(%d)", ret, rotation);

	if (ret == -1)
		func_ret = UG_EVENT_ROTATE_PORTRAIT;
	else {
		switch (rotation) {
			case 0:
				func_ret = UG_EVENT_ROTATE_PORTRAIT;
				break;
			case 90:
				func_ret = UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN;
				break;
			case 180:
				func_ret = UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN;
				break;
			case 270:
				func_ret = UG_EVENT_ROTATE_LANDSCAPE;
				break;
			default:
				func_ret = UG_EVENT_ROTATE_PORTRAIT;
				break;
		}
	}

func_out:
	return func_ret;
}
#endif

static void ugman_tree_dump(ui_gadget_h ug)
{
	static int i;
	int lv;
	const char *name;
	GSList *child;
	ui_gadget_h c;

	if (!ug)
		return;

	name = ug->name;
	if (ug == ug_man.root) {
		i = 0;
		_DBG("============== TREE_DUMP =============");
		_DBG("ROOT: Manager");
		name = "Manager";
	}

	child = ug->children;
	if (!child)
		return;

	i++;
	lv = i;

	while (child) {
		c = child->data;
		_DBG("[%d] %s [%c] (mem : %s) (ug : %p) (PARENT:  %s)",
		     lv,
		     c && c->name ? c->name : "NO CHILD INFO FIXIT!!!",
		     c && c->mode == UG_MODE_FULLVIEW ? 'F' : 'f', 
			 c->module->addr, c, name);
		ugman_tree_dump(c);
		child = g_slist_next(child);
	}
}

static int ugman_ug_find(ui_gadget_h p, ui_gadget_h ug)
{
	GSList *child = NULL;

	if (!p || !ug)
		return 0;
	child = p->children;

	while (child) {
		if (child->data == ug)
			return 1;
		if (ugman_ug_find(child->data, ug))
			return 1;
		child = g_slist_next(child);
	}

	return 0;
}

static void ugman_ug_start(void *data)
{
	ui_gadget_h ug = data;
	struct ug_module_ops *ops = NULL;

	if (!ug) {
		_ERR("ug is null");
		return;
	} else if (ug->state != UG_STATE_CREATED) {
		_DBG("start cb will be not invoked because ug(%p) state(%d) is not created", ug, ug->state);
		return;
	}

	_DBG("ug=%p", ug);

	ug->state = UG_STATE_RUNNING;

	if (ug->module)
		ops = &ug->module->ops;

	if (ops && ops->start)
		ops->start(ug, ug->service, ops->priv);

	return;
}

static int ugman_ug_pause(void *data)
{
	ui_gadget_h ug = data;
	struct ug_module_ops *ops = NULL;
	GSList *child = NULL;

	job_start();

	if (!ug || ug->state != UG_STATE_RUNNING)
		goto end;

	ug->state = UG_STATE_STOPPED;

	if (ug->children) {
		child = ug->children;
		while (child) {
			ugman_ug_pause(child->data);
			child = g_slist_next(child);
		}
	}

	if (ug->module)
		ops = &ug->module->ops;

	if (ops && ops->pause)
		ops->pause(ug, ug->service, ops->priv);

 end:
	job_end();
	return 0;
}

static int ugman_ug_resume(void *data)
{
	ui_gadget_h ug = data;
	struct ug_module_ops *ops = NULL;
	GSList *child = NULL;

	job_start();

	if (!ug)
		goto end;

	_DBG("ug(%p)->state : %d", ug, ug->state);

	switch (ug->state) {
	case UG_STATE_CREATED:
		ugman_ug_start(ug);
		goto end;
	case UG_STATE_STOPPED:
		break;
	default:
		goto end;
	}

	ug->state = UG_STATE_RUNNING;

	if (ug->children) {
		child = ug->children;
		while (child) {
			ugman_ug_resume(child->data);
			child = g_slist_next(child);
		}
	}

	if (ug->module)
		ops = &ug->module->ops;

	if (ops && ops->resume)
		ops->resume(ug, ug->service, ops->priv);

 end:
	job_end();
	return 0;
}

static int ugman_indicator_overlap_update(enum ug_option opt)
{
	if (!ug_man.win) {
		_ERR("indicator update failed: no window");
		return -1;
	}

	if(GET_OPT_OVERLAP_VAL(opt)) {
		_DBG("update overlap indicator / opt(%d)", opt);
		elm_object_signal_emit(ug_man.conform, "elm,state,indicator,overlap", "");
	} else {
		_DBG("update no overlap indicator / opt(%d)", opt);
		elm_object_signal_emit(ug_man.conform, "elm,state,indicator,nooverlap", "");
	}

	return 0;
}

static int ugman_indicator_update(enum ug_option opt, enum ug_event event)
{
	int enable;
	int cur_state;

#ifndef WAYLAND
	cur_state = utilx_get_indicator_state(ug_man.disp, ug_man.win_id);
#else
	cur_state = -1; //state is -1 (unknown)
#endif

	_DBG("indicator update opt(%d) cur_state(%d)", opt, cur_state);

#ifndef ENABLE_UG_HANDLE_INDICATOR_HIDE
	enable = 1;
#else
	switch (GET_OPT_INDICATOR_VAL(opt)) {
		case UG_OPT_INDICATOR_ENABLE:
			if (event == UG_EVENT_NONE)
				enable = 1;
			else
				enable = cur_state ? 1 : 0;
			break;
		case UG_OPT_INDICATOR_PORTRAIT_ONLY:
			enable = ug_man.is_landscape ? 0 : 1;
			break;
		case UG_OPT_INDICATOR_LANDSCAPE_ONLY:
			enable = ug_man.is_landscape ? 1 : 0;
			break;
		case UG_OPT_INDICATOR_DISABLE:
			enable = 0;
			break;
		case UG_OPT_INDICATOR_MANUAL:
			return 0;
		default:
			_ERR("update failed: Invalid opt(%d)", opt);
			return -1;
	}
#endif

	if(cur_state != enable) {
		_DBG("set indicator as %d", enable);
#ifndef WAYLAND
		utilx_enable_indicator(ug_man.disp, ug_man.win_id, enable);
#endif
	}

	return 0;
}

static int ugman_ug_getopt(ui_gadget_h ug)
{
	if (!ug)
		return -1;

	/* Indicator Option */
	if (ug->mode == UG_MODE_FULLVIEW) {
		ugman_indicator_overlap_update(ug->opt);
		ugman_indicator_update(ug->opt, UG_EVENT_NONE);
	}

	return 0;
}

static int ugman_ug_event(ui_gadget_h ug, enum ug_event event)
{
	struct ug_module_ops *ops = NULL;
	GSList *child = NULL;

	if (!ug)
		return 0;

	if (ug->children) {
		child = ug->children;
		while (child) {
			ugman_ug_event(child->data, event);
			child = g_slist_next(child);
		}
	}

	if (ug->module)
		ops = &ug->module->ops;

	_DBG("ug_event_cb : ug(%p) / event(%d)", ug, event);

	if (ops && ops->event)
		ops->event(ug, event, ug->service, ops->priv);

	return 0;
}

static int ugman_ug_destroy(void *data)
{
	ui_gadget_h ug = data;
	struct ug_module_ops *ops = NULL;
	struct ug_cbs *cbs;

	job_start();

	if (!ug)
		goto end;

	_DBG("ug(%p) state(%d)", ug, ug->state);

	switch (ug->state) {
		case UG_STATE_CREATED:
		case UG_STATE_RUNNING:
		case UG_STATE_STOPPED:
		case UG_STATE_DESTROYING:
		case UG_STATE_PENDING_DESTROY:
			break;
		default:
			_WRN("ug(%p) state is already destroyed", ug);
			goto end;
	}

	ug->state = UG_STATE_DESTROYED;

	if((ug != ug_man.root) && (ug->layout) &&
		(ug->mode == UG_MODE_FULLVIEW) &&
		(ug->layout_state != UG_LAYOUT_DESTROY)) {
		/* ug_destroy_all case */
		struct ug_engine_ops *eng_ops = NULL;

		if (ug_man.engine)
			eng_ops = &ug_man.engine->ops;

		if (eng_ops && eng_ops->destroy)
			eng_ops->destroy(ug, NULL, NULL);
	}

	if (ug->module)
		ops = &ug->module->ops;

	if (ops && ops->destroy) {
		_DBG("ug(%p) module destory cb call", ug);
		ops->destroy(ug, ug->service, ops->priv);
	}

	cbs = &ug->cbs;
	if (cbs && cbs->end_cb) {
		_DBG("ug(%p) end cb will be invoked", ug);
		cbs->end_cb(ug, cbs->priv);
	}

	if((ug->parent) && (ug->parent->state == UG_STATE_PENDING_DESTROY)) {
		if((ug->parent->children) && (g_slist_length(ug->parent->children) == 1)) {
			_WRN("pended parent ug(%p) destroy job is added to loop", ug->parent);
			ugman_idler_add((Idle_Cb)ugman_ug_destroy, ug->parent);
		} else {
			_WRN("pended parent ug(%p) will be destroyed after another children is destroyed", ug->parent);
		}
	}

	if (ug != ug_man.root)
		ug_relation_del(ug);

	if (ug->mode == UG_MODE_FULLVIEW) {
		if (ug_man.fv_top == ug) {
			ug_fvlist_del(ug);
			if(!ug_man.destroy_all)
				ugman_ug_getopt(ug_man.fv_top);
		} else {
			ug_fvlist_del(ug);
		}
	}

	_DBG("free ug(%p)", ug);
	ug_free(ug);

	if (ug_man.root == ug)
		ug_man.root = NULL;

	ugman_tree_dump(ug_man.root);
 end:
	job_end();

	return 0;
}

static void ug_hide_end_cb(void *data)
{
	ui_gadget_h ug = data;
	if (ug->children) {
		_WRN("child ug is still destroying. parent ug(%p) will be destroyed later", ug);
		ug->state = UG_STATE_PENDING_DESTROY;
	} else {
		ugman_idler_add((Idle_Cb)ugman_ug_destroy, (void *)ug);
	}
}

static int ugman_ug_create(void *data)
{
	ui_gadget_h ug = data;
	struct ug_module_ops *ops = NULL;
	struct ug_cbs *cbs;
	struct ug_engine_ops *eng_ops = NULL;

	if (!ug || ug->state != UG_STATE_READY) {
		_ERR("ug(%p) input param error");
		return -1;
	}

	ug->state = UG_STATE_CREATED;

	if (ug->module)
		ops = &ug->module->ops;

	if (ug_man.engine)
		eng_ops = &ug_man.engine->ops;

	if (ops && ops->create) {
		ug->layout = ops->create(ug, ug->mode, ug->service, ops->priv);
		if (!ug->layout) {
			ug_relation_del(ug);
			_ERR("ug(%p) layout is null", ug);
			return -1;
		}
		if (ug->mode == UG_MODE_FULLVIEW) {
			if (eng_ops && eng_ops->create) {
				ug_man.conform = eng_ops->create(ug_man.win, ug, ugman_ug_start);
				if(!ug_man.conform)
					return -1;
			}
		}
		cbs = &ug->cbs;

		if (cbs && cbs->layout_cb)
			cbs->layout_cb(ug, ug->mode, cbs->priv);

		_DBG("after caller layout cb call");
		ugman_indicator_update(ug->opt, UG_EVENT_NONE);
	}

	if(ug_man.last_rotate_evt == UG_EVENT_NONE) {
#ifndef WAYLAND
		ug_man.last_rotate_evt = __ug_x_rotation_get(ug_man.disp, ug_man.win_id);
#endif
	}
	ugman_ug_event(ug, ug_man.last_rotate_evt);

	if(ug->mode == UG_MODE_FRAMEVIEW)
		ugman_ug_start(ug);

	ugman_tree_dump(ug_man.root);

	return 0;
}

int ugman_ug_add(ui_gadget_h parent, ui_gadget_h ug)
{
	if (!ug_man.is_initted) {
		_ERR("failed: manager is not initted");
		return -1;
	}

	if (!ug_man.root) {
		if (parent) {
			_ERR("failed: parent has to be NULL w/o root");
			errno = EINVAL;
			return -1;
		}

		ug_man.root = ug_root_create();
		if (!ug_man.root) {
			_ERR("failed : ug root create fail");
			return -1;
		}
		ug_man.root->opt = ug_man.base_opt;
		ug_man.root->layout = ug_man.win;
		ug_fvlist_add(ug_man.root);
	}

	if (!parent) {
		parent = ug_man.root;
	} else {
		switch (parent->state) {
			case UG_STATE_DESTROYING:
			case UG_STATE_PENDING_DESTROY:
			case UG_STATE_DESTROYED:
				_WRN("parent(%p) state(%d) error", parent, parent->state);
				return -1;
			default:;
		}
	}

	if (ug_relation_add(parent, ug)) {
		_ERR("failed : ug_relation_add fail");
		return -1;
	}

	if (ugman_ug_create(ug) == -1) {
		_ERR("failed : ugman_ug_create fail");
		return -1;
	}
	if (ug->mode == UG_MODE_FULLVIEW)
		ug_fvlist_add(ug);

	return 0;
}

ui_gadget_h ugman_ug_load(ui_gadget_h parent,
				const char *name,
				enum ug_mode mode,
				service_h service, struct ug_cbs *cbs)
{
	int r;
	ui_gadget_h ug;

	ug = calloc(1, sizeof(struct ui_gadget_s));
	if (!ug) {
		_ERR("ug_create() failed: Memory allocation failed");
		return NULL;
	}

	ug->module = ug_module_load(name);
	if (!ug->module) {
		_ERR("ug_create() failed: Module loading failed");
		goto load_fail;
	}

	ug->name = strdup(name);

	ug->mode = mode;
	service_clone(&ug->service, service);
	ug->opt = ug->module->ops.opt;
	ug->state = UG_STATE_READY;
	ug->children = NULL;

	if (cbs)
		memcpy(&ug->cbs, cbs, sizeof(struct ug_cbs));

	r = ugman_ug_add(parent, ug);
	if (r) {
		_ERR("ug_create() failed: Tree update failed");
		goto load_fail;
	}

	return ug;

 load_fail:
	ug_free(ug);
	return NULL;
}

int ugman_ug_destroying(ui_gadget_h ug)
{
	struct ug_module_ops *ops = NULL;

	_DBG("ugman_ug_destroying");

	ug->destroy_me = 1;
	ug->state = UG_STATE_DESTROYING;

	if (ug->module)
		ops = &ug->module->ops;

	if (ops && ops->destroying)
		ops->destroying(ug, ug->service, ops->priv);

	return 0;
}

int ugman_ug_del(ui_gadget_h ug)
{
	struct ug_engine_ops *eng_ops = NULL;

	if (!ug || !ugman_ug_exist(ug) || ug->state == UG_STATE_DESTROYED) {
		_ERR("ugman_ug_del failed: Invalid ug(%p)");
		errno = EINVAL;
		return -1;
	}

	_DBG("ugman_ug_del start ug(%p)", ug);

	if (ug->destroy_me) {
		_WRN("ugman_ug_del failed: ug is alreay on destroying");
		return -1;
	}

	if (!ug_man.is_initted) {
		_WRN("ugman_ug_del failed: manager is not initted");
		return -1;
	}

	if (!ug_man.root) {
		_ERR("ugman_ug_del failed: no root");
		return -1;
	}

	if (ug->children) {
		GSList *child, *trail;

		child = ug->children;
		_DBG("ugman_ug_del ug(%p) has child(%p)", ug, child->data);
		while (child) {
			trail = g_slist_next(child);
			ugman_ug_del(child->data);
			child = trail;
		}
	}

	ugman_ug_destroying(ug);

	/* pre call for indicator update time issue */
	bool is_update = false;
	ui_gadget_h t = NULL;
	if (ug_man.fv_top == ug) {
		is_update = true;
		t = g_slist_nth_data(ug_man.fv_list, 1);
	} else {
		if (ug->children) {
			GSList *child;
			child = g_slist_last(ug->children);
			if(ug_man.fv_top == (ui_gadget_h)child->data) {
				is_update = true;
				t = g_slist_nth_data(ug_man.fv_list,
					g_slist_index(ug_man.fv_list,(gconstpointer)ug)+1);
			}
		}
	}

	if((is_update)&&(t)) {
		ugman_ug_getopt(t);
	}

	if (ug_man.engine)
		eng_ops = &ug_man.engine->ops;

	if (ug->mode == UG_MODE_FULLVIEW) {
		if (eng_ops && eng_ops->destroy)
			eng_ops->destroy(ug, ug_man.fv_top, ug_hide_end_cb);
		else
			ugman_idler_add((Idle_Cb)ugman_ug_destroy, ug);
	} else {
		_DBG("ug(%p) mode is frameview", ug);
		ug_hide_end_cb(ug);
	}

	_DBG("ugman_ug_del(%p) end", ug);

	return 0;
}


int ugman_ug_del_child(ui_gadget_h ug)
{
	GSList *child, *trail;

	if (ug->children) {
		child = ug->children;
		_DBG("ug destroy all. ug(%p) has child(%p)", ug, child->data);
		while (child) {
			trail = g_slist_next(child);
			ugman_ug_del_child(child->data);
			child = trail;
		}
	}

	ugman_ug_destroy(ug);

	return 0;
}

int ugman_ug_del_all(void)
{
	/*  Terminate */
	if (!ug_man.is_initted) {
		_ERR("ugman_ug_del_all failed: manager is not initted");
		return -1;
	}

	if (!ug_man.root) {
		_ERR("ugman_ug_del_all failed: no root");
		return -1;
	}

	_DBG("ug_del_all. root(%p) walking(%d) ", ug_man.root, ug_man.walking);

	if (ug_man.walking > 0) {
		ug_man.destroy_all = 1;
	} else {
		ugman_ug_del_child(ug_man.root);
	}

	return 0;
}

#ifndef WAYLAND
int ugman_init(Display *disp, Window xid, void *win, enum ug_option opt)
{
	ug_man.win = win;
	ug_man.disp = disp;
	ug_man.win_id = xid;
	ug_man.base_opt = opt;
	ug_man.last_rotate_evt = UG_EVENT_NONE;

	if (!ug_man.is_initted) {
		ug_man.engine = ug_engine_load();
	}

	ug_man.is_initted = 1;

	return 0;
}
#endif

int ugman_init_efl(Evas_Object *win, enum ug_option opt)
{
#ifndef WAYLAND
    Ecore_X_Window xwin = elm_win_xwindow_get(win);
    if (xwin)
        return ugman_init((Display *)ecore_x_display_get(), xwin, win, opt);
#endif
    return -1;
}

int ugman_resume(void)
{
	/* RESUME */
	if (!ug_man.is_initted) {
		_ERR("ugman_resume failed: manager is not initted");
		return -1;
	}

	if (!ug_man.root) {
		_WRN("ugman_resume failed: no root");
		return -1;
	}

	_DBG("ugman_resume called");

	ugman_idler_add((Idle_Cb)ugman_ug_resume, ug_man.root);

	return 0;
}

int ugman_pause(void)
{
	/* PAUSE (Background) */
	if (!ug_man.is_initted) {
		_ERR("ugman_pause failed: manager is not initted");
		return -1;
	}

	if (!ug_man.root) {
		_WRN("ugman_pause failed: no root");
		return -1;
	}

	_DBG("ugman_pause called");

	ugman_idler_add((Idle_Cb)ugman_ug_pause, ug_man.root);

	return 0;
}

static int ugman_send_event_pre(void *data)
{
	job_start();

	ugman_ug_event(ug_man.root, (enum ug_event)data);

	job_end();

	return 0;
}

int ugman_send_event(enum ug_event event)
{
	int is_rotation = 1;

	/* Propagate event */
	if (!ug_man.is_initted) {
		_ERR("ugman_send_event failed: manager is not initted");
		return -1;
	}

	if (!ug_man.root) {
		_WRN("ugman_send_event failed: no root");
		return -1;
	}

	/* In case of rotation, indicator state has to be updated */
	switch (event) {
	case UG_EVENT_ROTATE_PORTRAIT:
	case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
		ug_man.last_rotate_evt = event;
		ug_man.is_landscape = 0;
		break;
	case UG_EVENT_ROTATE_LANDSCAPE:
	case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
		ug_man.last_rotate_evt = event;
		ug_man.is_landscape = 1;
		break;
	default:
		is_rotation = 0;
	}

	ugman_idler_add((Idle_Cb)ugman_send_event_pre, (void *)event);

	if (is_rotation && ug_man.fv_top)
		ugman_indicator_update(ug_man.fv_top->opt, event);

	return 0;
}

static int ugman_send_key_event_to_ug(ui_gadget_h ug,
				      enum ug_key_event event)
{
	struct ug_module_ops *ops = NULL;

	if (!ug)
		return -1;

	if (ug->module) {
		ops = &ug->module->ops;
	} else {
		return -1;
	}

	if (ops && ops->key_event) {
		ops->key_event(ug, event, ug->service, ops->priv);
	} else {
		return -1;
	}

	return 0;
}

int ugman_send_key_event(enum ug_key_event event)
{
	if (!ug_man.is_initted) {
		_ERR("ugman_send_key_event failed: manager is not initted");
		return -1;
	}

	if (!ug_man.fv_top || !ugman_ug_exist(ug_man.fv_top)
	    || ug_man.fv_top->state == UG_STATE_DESTROYED) {
		_ERR("ugman_send_key_event failed: full view top UG is invalid");
		return -1;
	}

	return ugman_send_key_event_to_ug(ug_man.fv_top, event);
}

int ugman_send_message(ui_gadget_h ug, service_h msg)
{
	struct ug_module_ops *ops = NULL;
	if (!ug || !ugman_ug_exist(ug) || ug->state == UG_STATE_DESTROYED) {
		_ERR("ugman_send_message failed: Invalid ug");
		errno = EINVAL;
		return -1;
	}

	if (!msg) {
		_ERR("ugman_send_message failed: Invalid msg");
		errno = EINVAL;
		return -1;
	}

	if (ug->module)
		ops = &ug->module->ops;

	if (ops && ops->message)
		ops->message(ug, msg, ug->service, ops->priv);

	return 0;
}

void *ugman_get_window(void)
{
	return ug_man.win;
}

void *ugman_get_conformant(void)
{
	struct ug_engine_ops *eng_ops = NULL;
	void* ret = NULL;

	if(ug_man.conform) {
		_DBG("return cached conform(%p) info", ug_man.conform);
		return ug_man.conform;
	}

	if (ug_man.engine) {
		eng_ops = &ug_man.engine->ops;
	} else {
		_WRN("ui engine is not loaded");
		return NULL;
	}

	if (eng_ops && eng_ops->create) {
		ret = eng_ops->request(ug_man.win, NULL, UG_UI_REQ_GET_CONFORMANT);
		ug_man.conform = ret;
	} else {
		_WRN("ui engine is not loaded");
	}

	return ret;
}

static inline void job_start(void)
{
	ug_man.walking++;
}

static inline void job_end(void)
{
	ug_man.walking--;

	if (!ug_man.walking && ug_man.destroy_all) {
		ug_man.destroy_all = 0;
		if (ug_man.root) {
			_DBG("ug_destroy_all pneding job exist. ug_destroy_all begin");
			ugman_ug_del_all();
		}
	}

	if (ug_man.walking < 0)
		ug_man.walking = 0;
}

int ugman_ug_exist(ui_gadget_h ug)
{
	return ugman_ug_find(ug_man.root, ug);
}