summaryrefslogtreecommitdiff
path: root/ivi-shell/ivi-layout.c
blob: 8683e7dd0228db7819745680253a90e7503a6cef (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
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
/*
 * Copyright (C) 2013 DENSO CORPORATION
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/**
 * Implementation of ivi-layout library. The actual view on ivi_screen is
 * not updated till calling ivi_layout_commit_changes. A overview from
 * calling API for updating properties of ivi_surface/ivi_layer to asking
 * compositor to compose them by using weston_compositor_schedule_repaint,
 * 0/ initialize this library by ivi_layout_init_with_compositor
 *    with (struct weston_compositor *ec) from ivi-shell.
 * 1/ When a API for updating properties of ivi_surface/ivi_layer, it updates
 *    pending prop of ivi_surface/ivi_layer/ivi_screen which are structure to
 *    store properties.
 * 2/ Before calling commitChanges, in case of calling a API to get a property,
 *    return current property, not pending property.
 * 3/ At the timing of calling ivi_layout_commitChanges, pending properties
 *    are applied to properties.
 *
 *    *) ivi_layout_commitChanges is also called by transition animation
 *    per each frame. See ivi-layout-transition.c in details. Transition
 *    animation interpolates frames between previous properties of ivi_surface
 *    and new ones.
 *    For example, when a property of ivi_surface is changed from invisibility
 *    to visibility, it behaves like fade-in. When ivi_layout_commitChange is
 *    called during transition animation, it cancels the transition and
 *    re-start transition to new properties from current properties of final
 *    frame just before the the cancellation.
 *
 * 4/ According properties, set transformation by using weston_matrix and
 *    weston_view per ivi_surfaces and ivi_layers in while loop.
 * 5/ Set damage and trigger transform by using weston_view_geometry_dirty.
 * 6/ Notify update of properties.
 * 7/ Trigger composition by weston_compositor_schedule_repaint.
 *
 */

#include <string.h>

#include "compositor.h"
#include "ivi-layout-export.h"
#include "ivi-layout-private.h"

struct link_layer {
	struct ivi_layout_layer *ivilayer;
	struct wl_list link;
	struct wl_list link_to_layer;
};

struct link_screen {
	struct ivi_layout_screen *iviscrn;
	struct wl_list link;
	struct wl_list link_to_screen;
};

struct listener_layout_notification {
	void *userdata;
	struct wl_listener listener;
};

struct ivi_layout;

struct ivi_layout_screen {
	struct wl_list link;
	struct wl_list link_to_layer;
	uint32_t id_screen;

	struct ivi_layout *layout;
	struct weston_output *output;

	uint32_t event_mask;

	struct {
		struct wl_list layer_list;
		struct wl_list link;
	} pending;

	struct {
		struct wl_list layer_list;
		struct wl_list link;
	} order;
};

struct ivi_layout_notification_callback {
	void *callback;
	void *data;
};

static struct ivi_layout ivilayout = {0};

struct ivi_layout *
get_instance(void)
{
	return &ivilayout;
}

/**
 * Internal API to add/remove a link to ivi_surface from ivi_layer.
 */
static void
add_link_to_surface(struct ivi_layout_layer *ivilayer,
		    struct link_layer *link_layer)
{
	struct link_layer *link = NULL;

	wl_list_for_each(link, &ivilayer->link_to_surface, link_to_layer) {
		if (link == link_layer)
			return;
	}

	wl_list_insert(&ivilayer->link_to_surface, &link_layer->link_to_layer);
}

static void
remove_link_to_surface(struct ivi_layout_layer *ivilayer)
{
	struct link_layer *link = NULL;
	struct link_layer *next = NULL;

	wl_list_for_each_safe(link, next, &ivilayer->link_to_surface, link_to_layer) {
		if (!wl_list_empty(&link->link_to_layer)) {
			wl_list_remove(&link->link_to_layer);
		}
		if (!wl_list_empty(&link->link)) {
			wl_list_remove(&link->link);
		}
		free(link);
	}

	wl_list_init(&ivilayer->link_to_surface);
}

/**
 * Internal API to add a link to ivi_layer from ivi_screen.
 */
static void
add_link_to_layer(struct ivi_layout_screen *iviscrn,
		  struct link_screen *link_screen)
{
	wl_list_init(&link_screen->link_to_screen);
	wl_list_insert(&iviscrn->link_to_layer, &link_screen->link_to_screen);
}

/**
 * Internal API to add/remove a ivi_surface from ivi_layer.
 */
static void
add_ordersurface_to_layer(struct ivi_layout_surface *ivisurf,
			  struct ivi_layout_layer *ivilayer)
{
	struct link_layer *link_layer = NULL;

	link_layer = malloc(sizeof *link_layer);
	if (link_layer == NULL) {
		weston_log("fails to allocate memory\n");
		return;
	}

	link_layer->ivilayer = ivilayer;
	wl_list_init(&link_layer->link);
	wl_list_insert(&ivisurf->layer_list, &link_layer->link);
	add_link_to_surface(ivilayer, link_layer);
}

static void
remove_ordersurface_from_layer(struct ivi_layout_surface *ivisurf)
{
	struct link_layer *link_layer = NULL;
	struct link_layer *next = NULL;

	wl_list_for_each_safe(link_layer, next, &ivisurf->layer_list, link) {
		if (!wl_list_empty(&link_layer->link)) {
			wl_list_remove(&link_layer->link);
		}
		if (!wl_list_empty(&link_layer->link_to_layer)) {
			wl_list_remove(&link_layer->link_to_layer);
		}
		free(link_layer);
	}
	wl_list_init(&ivisurf->layer_list);
}

/**
 * Internal API to add/remove a ivi_layer to/from ivi_screen.
 */
static void
add_orderlayer_to_screen(struct ivi_layout_layer *ivilayer,
			 struct ivi_layout_screen *iviscrn)
{
	struct link_screen *link_scrn = NULL;

	link_scrn = malloc(sizeof *link_scrn);
	if (link_scrn == NULL) {
		weston_log("fails to allocate memory\n");
		return;
	}

	link_scrn->iviscrn = iviscrn;
	wl_list_init(&link_scrn->link);
	wl_list_insert(&ivilayer->screen_list, &link_scrn->link);
	add_link_to_layer(iviscrn, link_scrn);
}

static void
remove_orderlayer_from_screen(struct ivi_layout_layer *ivilayer)
{
	struct link_screen *link_scrn = NULL;
	struct link_screen *next = NULL;

	wl_list_for_each_safe(link_scrn, next, &ivilayer->screen_list, link) {
		if (!wl_list_empty(&link_scrn->link)) {
			wl_list_remove(&link_scrn->link);
		}
		if (!wl_list_empty(&link_scrn->link_to_screen)) {
			wl_list_remove(&link_scrn->link_to_screen);
		}
		free(link_scrn);
	}
	wl_list_init(&ivilayer->screen_list);
}

/**
 * Internal API to add/remove a ivi_layer to/from ivi_screen.
 */
static struct ivi_layout_surface *
get_surface(struct wl_list *surf_list, uint32_t id_surface)
{
	struct ivi_layout_surface *ivisurf;

	wl_list_for_each(ivisurf, surf_list, link) {
		if (ivisurf->id_surface == id_surface) {
			return ivisurf;
		}
	}

	return NULL;
}

static struct ivi_layout_layer *
get_layer(struct wl_list *layer_list, uint32_t id_layer)
{
	struct ivi_layout_layer *ivilayer;

	wl_list_for_each(ivilayer, layer_list, link) {
		if (ivilayer->id_layer == id_layer) {
			return ivilayer;
		}
	}

	return NULL;
}

static void
remove_configured_listener(struct ivi_layout_surface *ivisurf)
{
	struct wl_listener *link = NULL;
	struct wl_listener *next = NULL;

	wl_list_for_each_safe(link, next, &ivisurf->configured.listener_list, link) {
		wl_list_remove(&link->link);
	}
}


static void
remove_all_notification(struct wl_list *listener_list)
{
	struct wl_listener *listener = NULL;
	struct wl_listener *next = NULL;

	wl_list_for_each_safe(listener, next, listener_list, link) {
		struct listener_layout_notification *notification = NULL;
		if (!wl_list_empty(&listener->link)) {
			wl_list_remove(&listener->link);
		}

		notification =
			container_of(listener,
				     struct listener_layout_notification,
				     listener);

		free(notification->userdata);
		free(notification);
	}
}

WL_EXPORT void
ivi_layout_surface_remove_notification(struct ivi_layout_surface *ivisurf)
{
	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_remove_notification: invalid argument\n");
		return;
	}

	remove_all_notification(&ivisurf->property_changed.listener_list);
}

void
ivi_layout_surface_remove(struct ivi_layout_surface *ivisurf)
{
	struct ivi_layout *layout = get_instance();

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_remove: invalid argument\n");
		return;
	}

	if (!wl_list_empty(&ivisurf->pending.link)) {
		wl_list_remove(&ivisurf->pending.link);
	}
	if (!wl_list_empty(&ivisurf->order.link)) {
		wl_list_remove(&ivisurf->order.link);
	}
	if (!wl_list_empty(&ivisurf->link)) {
		wl_list_remove(&ivisurf->link);
	}
	remove_ordersurface_from_layer(ivisurf);

	wl_signal_emit(&layout->surface_notification.removed, ivisurf);

	remove_configured_listener(ivisurf);

	ivi_layout_surface_remove_notification(ivisurf);

	free(ivisurf);
}

/**
 * Called at destruction of ivi_surface
 */
static void
westonsurface_destroy_from_ivisurface(struct wl_listener *listener, void *data)
{
	struct ivi_layout_surface *ivisurf = NULL;

	ivisurf = container_of(listener, struct ivi_layout_surface,
			       surface_destroy_listener);

	wl_list_remove(&ivisurf->surface_rotation.link);
	wl_list_remove(&ivisurf->layer_rotation.link);
	wl_list_remove(&ivisurf->surface_pos.link);
	wl_list_remove(&ivisurf->layer_pos.link);
	wl_list_remove(&ivisurf->scaling.link);

	ivisurf->surface = NULL;
	ivi_layout_surface_remove(ivisurf);
}

/**
 * Internal API to check ivi_layer/ivi_surface already added in ivi_layer/ivi_screen.
 * Called by ivi_layout_layer_add_surface/ivi_layout_screenAddLayer
 */
static int
is_surface_in_layer(struct ivi_layout_surface *ivisurf,
		    struct ivi_layout_layer *ivilayer)
{
	struct ivi_layout_surface *surf = NULL;

	wl_list_for_each(surf, &ivilayer->pending.surface_list, pending.link) {
		if (surf->id_surface == ivisurf->id_surface) {
			return 1;
		}
	}

	return 0;
}

static int
is_layer_in_screen(struct ivi_layout_layer *ivilayer,
		   struct ivi_layout_screen *iviscrn)
{
	struct ivi_layout_layer *layer = NULL;

	wl_list_for_each(layer, &iviscrn->pending.layer_list, pending.link) {
		if (layer->id_layer == ivilayer->id_layer) {
			return 1;
		}
	}

	return 0;
}

/**
 * Internal API to initialize ivi_screens found from output_list of weston_compositor.
 * Called by ivi_layout_init_with_compositor.
 */
static void
create_screen(struct weston_compositor *ec)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_screen *iviscrn = NULL;
	struct weston_output *output = NULL;
	int32_t count = 0;

	wl_list_for_each(output, &ec->output_list, link) {
		iviscrn = calloc(1, sizeof *iviscrn);
		if (iviscrn == NULL) {
			weston_log("fails to allocate memory\n");
			continue;
		}

		wl_list_init(&iviscrn->link);
		iviscrn->layout = layout;

		iviscrn->id_screen = count;
		count++;

		iviscrn->output = output;
		iviscrn->event_mask = 0;

		wl_list_init(&iviscrn->pending.layer_list);
		wl_list_init(&iviscrn->pending.link);

		wl_list_init(&iviscrn->order.layer_list);
		wl_list_init(&iviscrn->order.link);

		wl_list_init(&iviscrn->link_to_layer);

		wl_list_insert(&layout->screen_list, &iviscrn->link);
	}
}

/**
 * Internal APIs to initialize properties of ivi_surface/ivi_layer when they are created.
 */
static void
init_layer_properties(struct ivi_layout_layer_properties *prop,
		      int32_t width, int32_t height)
{
	memset(prop, 0, sizeof *prop);
	prop->opacity = wl_fixed_from_double(1.0);
	prop->source_width = width;
	prop->source_height = height;
	prop->dest_width = width;
	prop->dest_height = height;
}

static void
init_surface_properties(struct ivi_layout_surface_properties *prop)
{
	memset(prop, 0, sizeof *prop);
	prop->opacity = wl_fixed_from_double(1.0);
}

/**
 * Internal APIs to be called from ivi_layout_commit_changes.
 */
static void
update_opacity(struct ivi_layout_layer *ivilayer,
	       struct ivi_layout_surface *ivisurf)
{
	double layer_alpha = wl_fixed_to_double(ivilayer->prop.opacity);
	double surf_alpha  = wl_fixed_to_double(ivisurf->prop.opacity);

	if ((ivilayer->event_mask & IVI_NOTIFICATION_OPACITY) ||
	    (ivisurf->event_mask  & IVI_NOTIFICATION_OPACITY)) {
		struct weston_view *tmpview = NULL;
		wl_list_for_each(tmpview, &ivisurf->surface->views, surface_link) {
			if (tmpview == NULL) {
				continue;
			}
			tmpview->alpha = layer_alpha * surf_alpha;
		}
	}
}

static void
update_surface_orientation(struct ivi_layout_layer *ivilayer,
			   struct ivi_layout_surface *ivisurf)
{
	struct weston_view *view;
	struct weston_matrix  *matrix = &ivisurf->surface_rotation.matrix;
	float width  = 0.0f;
	float height = 0.0f;
	float v_sin  = 0.0f;
	float v_cos  = 0.0f;
	float cx = 0.0f;
	float cy = 0.0f;
	float sx = 1.0f;
	float sy = 1.0f;

	wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
		if (view != NULL) {
			break;
		}
	}

	if (view == NULL) {
		return;
	}

	if ((ivilayer->prop.dest_width == 0) ||
	    (ivilayer->prop.dest_height == 0)) {
		return;
	}
	width  = (float)ivilayer->prop.dest_width;
	height = (float)ivilayer->prop.dest_height;

	switch (ivisurf->prop.orientation) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
		v_sin = 0.0f;
		v_cos = 1.0f;
		break;
	case WL_OUTPUT_TRANSFORM_90:
		v_sin = 1.0f;
		v_cos = 0.0f;
		sx = width / height;
		sy = height / width;
		break;
	case WL_OUTPUT_TRANSFORM_180:
		v_sin = 0.0f;
		v_cos = -1.0f;
		break;
	case WL_OUTPUT_TRANSFORM_270:
	default:
		v_sin = -1.0f;
		v_cos = 0.0f;
		sx = width / height;
		sy = height / width;
		break;
	}
	wl_list_remove(&ivisurf->surface_rotation.link);
	weston_view_geometry_dirty(view);

	weston_matrix_init(matrix);
	cx = 0.5f * width;
	cy = 0.5f * height;
	weston_matrix_translate(matrix, -cx, -cy, 0.0f);
	weston_matrix_rotate_xy(matrix, v_cos, v_sin);
	weston_matrix_scale(matrix, sx, sy, 1.0);
	weston_matrix_translate(matrix, cx, cy, 0.0f);
	wl_list_insert(&view->geometry.transformation_list,
		       &ivisurf->surface_rotation.link);

	weston_view_set_transform_parent(view, NULL);
	weston_view_update_transform(view);
}

static void
update_layer_orientation(struct ivi_layout_layer *ivilayer,
			 struct ivi_layout_surface *ivisurf)
{
	struct weston_surface *es = ivisurf->surface;
	struct weston_view    *view;
	struct weston_matrix  *matrix = &ivisurf->layer_rotation.matrix;
	struct weston_output  *output = NULL;
	float width  = 0.0f;
	float height = 0.0f;
	float v_sin  = 0.0f;
	float v_cos  = 0.0f;
	float cx = 0.0f;
	float cy = 0.0f;
	float sx = 1.0f;
	float sy = 1.0f;

	wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
		if (view != NULL) {
			break;
		}
	}

	if (es == NULL || view == NULL) {
		return;
	}

	output = es->output;
	if (output == NULL) {
		return;
	}
	if ((output->width == 0) || (output->height == 0)) {
		return;
	}
	width = (float)output->width;
	height = (float)output->height;

	switch (ivilayer->prop.orientation) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
		v_sin = 0.0f;
		v_cos = 1.0f;
		break;
	case WL_OUTPUT_TRANSFORM_90:
		v_sin = 1.0f;
		v_cos = 0.0f;
		sx = width / height;
		sy = height / width;
		break;
	case WL_OUTPUT_TRANSFORM_180:
		v_sin = 0.0f;
		v_cos = -1.0f;
		break;
	case WL_OUTPUT_TRANSFORM_270:
	default:
		v_sin = -1.0f;
		v_cos = 0.0f;
		sx = width / height;
		sy = height / width;
		break;
	}
	wl_list_remove(&ivisurf->layer_rotation.link);
	weston_view_geometry_dirty(view);

	weston_matrix_init(matrix);
	cx = 0.5f * width;
	cy = 0.5f * height;
	weston_matrix_translate(matrix, -cx, -cy, 0.0f);
	weston_matrix_rotate_xy(matrix, v_cos, v_sin);
	weston_matrix_scale(matrix, sx, sy, 1.0);
	weston_matrix_translate(matrix, cx, cy, 0.0f);
	wl_list_insert(&view->geometry.transformation_list,
		       &ivisurf->layer_rotation.link);

	weston_view_set_transform_parent(view, NULL);
	weston_view_update_transform(view);
}

static void
update_surface_position(struct ivi_layout_surface *ivisurf)
{
	struct weston_view *view;
	float tx  = (float)ivisurf->prop.dest_x;
	float ty  = (float)ivisurf->prop.dest_y;
	struct weston_matrix *matrix = &ivisurf->surface_pos.matrix;

	wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
		if (view != NULL) {
			break;
		}
	}

	if (view == NULL) {
		return;
	}

	wl_list_remove(&ivisurf->surface_pos.link);

	weston_matrix_init(matrix);
	weston_matrix_translate(matrix, tx, ty, 0.0f);
	wl_list_insert(&view->geometry.transformation_list,
		       &ivisurf->surface_pos.link);

	weston_view_set_transform_parent(view, NULL);
	weston_view_update_transform(view);
}

static void
update_layer_position(struct ivi_layout_layer *ivilayer,
		      struct ivi_layout_surface *ivisurf)
{
	struct weston_view *view;
	struct weston_matrix *matrix = &ivisurf->layer_pos.matrix;
	float tx  = (float)ivilayer->prop.dest_x;
	float ty  = (float)ivilayer->prop.dest_y;

	wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
		if (view != NULL) {
			break;
		}
	}

	if (view == NULL) {
		return;
	}

	wl_list_remove(&ivisurf->layer_pos.link);

	weston_matrix_init(matrix);
	weston_matrix_translate(matrix, tx, ty, 0.0f);
	wl_list_insert(&view->geometry.transformation_list,
		       &ivisurf->layer_pos.link);

	weston_view_set_transform_parent(view, NULL);
	weston_view_update_transform(view);
}

static void
update_scale(struct ivi_layout_layer *ivilayer,
	     struct ivi_layout_surface *ivisurf)
{
	struct weston_view *view;
	struct weston_matrix *matrix = &ivisurf->scaling.matrix;
	float sx = 0.0f;
	float sy = 0.0f;
	float lw = 0.0f;
	float sw = 0.0f;
	float lh = 0.0f;
	float sh = 0.0f;

	wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
		if (view != NULL) {
			break;
		}
	}

	if (view == NULL) {
		return;
	}

	if (ivisurf->prop.dest_width == 0 && ivisurf->prop.dest_height == 0) {
		ivisurf->prop.dest_width  = ivisurf->surface->width_from_buffer;
		ivisurf->prop.dest_height = ivisurf->surface->height_from_buffer;
	}

	lw = ((float)ivilayer->prop.dest_width  / (float)ivilayer->prop.source_width );
	sw = ((float)ivisurf->prop.dest_width	/ (float)ivisurf->prop.source_width  );
	lh = ((float)ivilayer->prop.dest_height / (float)ivilayer->prop.source_height);
	sh = ((float)ivisurf->prop.dest_height  / (float)ivisurf->prop.source_height );
	sx = sw * lw;
	sy = sh * lh;

	wl_list_remove(&ivisurf->scaling.link);
	weston_matrix_init(matrix);
	weston_matrix_scale(matrix, sx, sy, 1.0f);

	wl_list_insert(&view->geometry.transformation_list,
		       &ivisurf->scaling.link);

	weston_view_set_transform_parent(view, NULL);
	weston_view_update_transform(view);
}

static void
update_prop(struct ivi_layout_layer *ivilayer,
	    struct ivi_layout_surface *ivisurf)
{
	if (ivilayer->event_mask | ivisurf->event_mask) {
		struct weston_view *tmpview;
		update_opacity(ivilayer, ivisurf);
		update_layer_orientation(ivilayer, ivisurf);
		update_layer_position(ivilayer, ivisurf);
		update_surface_position(ivisurf);
		update_surface_orientation(ivilayer, ivisurf);
		update_scale(ivilayer, ivisurf);

		ivisurf->update_count++;

		wl_list_for_each(tmpview, &ivisurf->surface->views, surface_link) {
			if (tmpview != NULL) {
				break;
			}
		}

		if (tmpview != NULL) {
			weston_view_geometry_dirty(tmpview);
		}

		if (ivisurf->surface != NULL) {
			weston_surface_damage(ivisurf->surface);
		}
	}
}

static void
commit_changes(struct ivi_layout *layout)
{
	struct ivi_layout_screen  *iviscrn  = NULL;
	struct ivi_layout_layer   *ivilayer = NULL;
	struct ivi_layout_surface *ivisurf  = NULL;

	wl_list_for_each(iviscrn, &layout->screen_list, link) {
		wl_list_for_each(ivilayer, &iviscrn->order.layer_list, order.link) {
			wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
				update_prop(ivilayer, ivisurf);
			}
		}
	}
}

static void
commit_surface_list(struct ivi_layout *layout)
{
	struct ivi_layout_surface *ivisurf = NULL;
	int32_t dest_x = 0;
	int32_t dest_y = 0;
	int32_t dest_width = 0;
	int32_t dest_height = 0;
	int32_t configured = 0;

	wl_list_for_each(ivisurf, &layout->surface_list, link) {
		if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_DEFAULT) {
			dest_x = ivisurf->prop.dest_x;
			dest_y = ivisurf->prop.dest_y;
			dest_width = ivisurf->prop.dest_width;
			dest_height = ivisurf->prop.dest_height;

			ivi_layout_transition_move_resize_view(ivisurf,
							       ivisurf->pending.prop.dest_x,
							       ivisurf->pending.prop.dest_y,
							       ivisurf->pending.prop.dest_width,
							       ivisurf->pending.prop.dest_height,
							       ivisurf->pending.prop.transition_duration);

			if(ivisurf->pending.prop.visibility) {
				ivi_layout_transition_visibility_on(ivisurf, ivisurf->pending.prop.transition_duration);
			} else {
				ivi_layout_transition_visibility_off(ivisurf, ivisurf->pending.prop.transition_duration);
			}

			ivisurf->prop = ivisurf->pending.prop;
			ivisurf->prop.dest_x = dest_x;
			ivisurf->prop.dest_y = dest_y;
			ivisurf->prop.dest_width = dest_width;
			ivisurf->prop.dest_height = dest_height;
			ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
			ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;

		} else if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_DEST_RECT_ONLY){
			dest_x = ivisurf->prop.dest_x;
			dest_y = ivisurf->prop.dest_y;
			dest_width = ivisurf->prop.dest_width;
			dest_height = ivisurf->prop.dest_height;

			ivi_layout_transition_move_resize_view(ivisurf,
							       ivisurf->pending.prop.dest_x,
							       ivisurf->pending.prop.dest_y,
							       ivisurf->pending.prop.dest_width,
							       ivisurf->pending.prop.dest_height,
							       ivisurf->pending.prop.transition_duration);

			ivisurf->prop = ivisurf->pending.prop;
			ivisurf->prop.dest_x = dest_x;
			ivisurf->prop.dest_y = dest_y;
			ivisurf->prop.dest_width = dest_width;
			ivisurf->prop.dest_height = dest_height;

			ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
			ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;

		} else if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_FADE_ONLY){
			configured = 0;
			if(ivisurf->pending.prop.visibility) {
				ivi_layout_transition_visibility_on(ivisurf, ivisurf->pending.prop.transition_duration);
			} else {
				ivi_layout_transition_visibility_off(ivisurf, ivisurf->pending.prop.transition_duration);
			}

			if (ivisurf->prop.dest_width  != ivisurf->pending.prop.dest_width ||
			    ivisurf->prop.dest_height != ivisurf->pending.prop.dest_height) {
				configured = 1;
			}

			ivisurf->prop = ivisurf->pending.prop;
			ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
			ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;

			if (configured && !is_surface_transition(ivisurf))
				wl_signal_emit(&ivisurf->configured, ivisurf);
		} else {
			configured = 0;
			if (ivisurf->prop.dest_width  != ivisurf->pending.prop.dest_width ||
			    ivisurf->prop.dest_height != ivisurf->pending.prop.dest_height) {
				configured = 1;
			}

			ivisurf->prop = ivisurf->pending.prop;
			ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
			ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;

			if (configured && !is_surface_transition(ivisurf))
				wl_signal_emit(&ivisurf->configured, ivisurf);
		}
	}
}

static void
commit_layer_list(struct ivi_layout *layout)
{
	struct ivi_layout_layer   *ivilayer = NULL;
	struct ivi_layout_surface *ivisurf  = NULL;
	struct ivi_layout_surface *next     = NULL;

	wl_list_for_each(ivilayer, &layout->layer_list, link) {
		if(ivilayer->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_LAYER_MOVE) {
			ivi_layout_transition_move_layer(ivilayer, ivilayer->pending.prop.dest_x, ivilayer->pending.prop.dest_y, ivilayer->pending.prop.transition_duration);
		} else if(ivilayer->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_LAYER_FADE) {
			ivi_layout_transition_fade_layer(ivilayer,ivilayer->pending.prop.is_fade_in,
							 ivilayer->pending.prop.start_alpha,ivilayer->pending.prop.end_alpha,
							 NULL, NULL,
							 ivilayer->pending.prop.transition_duration);
		}
		ivilayer->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;

		ivilayer->prop = ivilayer->pending.prop;

		if (!(ivilayer->event_mask &
		      (IVI_NOTIFICATION_ADD | IVI_NOTIFICATION_REMOVE)) ) {
			continue;
		}

		if (ivilayer->event_mask & IVI_NOTIFICATION_REMOVE) {
			wl_list_for_each_safe(ivisurf, next,
				&ivilayer->order.surface_list, order.link) {
				remove_ordersurface_from_layer(ivisurf);

				if (!wl_list_empty(&ivisurf->order.link)) {
					wl_list_remove(&ivisurf->order.link);
				}

				wl_list_init(&ivisurf->order.link);
				ivisurf->event_mask |= IVI_NOTIFICATION_REMOVE;
			}

			wl_list_init(&ivilayer->order.surface_list);
		}

		if (ivilayer->event_mask & IVI_NOTIFICATION_ADD) {
			wl_list_for_each_safe(ivisurf, next,
					      &ivilayer->order.surface_list, order.link) {
				remove_ordersurface_from_layer(ivisurf);

				if (!wl_list_empty(&ivisurf->order.link)) {
					wl_list_remove(&ivisurf->order.link);
				}

				wl_list_init(&ivisurf->order.link);
			}

			wl_list_init(&ivilayer->order.surface_list);
			wl_list_for_each(ivisurf, &ivilayer->pending.surface_list,
					 pending.link) {
				if(!wl_list_empty(&ivisurf->order.link)){
					wl_list_remove(&ivisurf->order.link);
					wl_list_init(&ivisurf->order.link);
				}

				wl_list_insert(&ivilayer->order.surface_list,
					       &ivisurf->order.link);
				add_ordersurface_to_layer(ivisurf, ivilayer);
				ivisurf->event_mask |= IVI_NOTIFICATION_ADD;
			}
		}
	}
}

static void
commit_screen_list(struct ivi_layout *layout)
{
	struct ivi_layout_screen  *iviscrn  = NULL;
	struct ivi_layout_layer   *ivilayer = NULL;
	struct ivi_layout_layer   *next     = NULL;
	struct ivi_layout_surface *ivisurf  = NULL;

	wl_list_for_each(iviscrn, &layout->screen_list, link) {
		if (iviscrn->event_mask & IVI_NOTIFICATION_REMOVE) {
			wl_list_for_each_safe(ivilayer, next,
					      &iviscrn->order.layer_list, order.link) {
				remove_orderlayer_from_screen(ivilayer);

				if (!wl_list_empty(&ivilayer->order.link)) {
				    wl_list_remove(&ivilayer->order.link);
				}

				wl_list_init(&ivilayer->order.link);
				ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
			}
		}

		if (iviscrn->event_mask & IVI_NOTIFICATION_ADD) {
			wl_list_for_each_safe(ivilayer, next,
					      &iviscrn->order.layer_list, order.link) {
				remove_orderlayer_from_screen(ivilayer);

				if (!wl_list_empty(&ivilayer->order.link)) {
					wl_list_remove(&ivilayer->order.link);
				}

				wl_list_init(&ivilayer->order.link);
			}

			wl_list_init(&iviscrn->order.layer_list);
			wl_list_for_each(ivilayer, &iviscrn->pending.layer_list,
					 pending.link) {
				wl_list_insert(&iviscrn->order.layer_list,
					       &ivilayer->order.link);
				add_orderlayer_to_screen(ivilayer, iviscrn);
				ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
			}
		}

		iviscrn->event_mask = 0;

		/* Clear view list of layout ivi_layer */
		wl_list_init(&layout->layout_layer.view_list.link);

		wl_list_for_each(ivilayer, &iviscrn->order.layer_list, order.link) {
			if (ivilayer->prop.visibility == false)
				continue;

			wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
				struct weston_view *tmpview = NULL;
				wl_list_for_each(tmpview, &ivisurf->surface->views, surface_link) {
					if (tmpview != NULL) {
						break;
					}
				}

				if (ivisurf->prop.visibility == false)
					continue;
				if (ivisurf->surface == NULL || tmpview == NULL)
					continue;

				weston_layer_entry_insert(&layout->layout_layer.view_list,
							  &tmpview->layer_link);

				ivisurf->surface->output = iviscrn->output;
			}
		}

		break;
	}
}

static void
commit_transition(struct ivi_layout* layout)
{
	if(wl_list_empty(&layout->pending_transition_list)){
		return;
	}

	wl_list_insert_list(&layout->transitions->transition_list,
			    &layout->pending_transition_list);

	wl_list_init(&layout->pending_transition_list);

	wl_event_source_timer_update(layout->transitions->event_source, 1);
}

static void
send_surface_prop(struct ivi_layout_surface *ivisurf)
{
	wl_signal_emit(&ivisurf->property_changed, ivisurf);
	ivisurf->event_mask = 0;
}

static void
send_layer_prop(struct ivi_layout_layer *ivilayer)
{
	wl_signal_emit(&ivilayer->property_changed, ivilayer);
	ivilayer->event_mask = 0;
}

static void
send_prop(struct ivi_layout *layout)
{
	struct ivi_layout_layer   *ivilayer = NULL;
	struct ivi_layout_surface *ivisurf  = NULL;

	wl_list_for_each_reverse(ivilayer, &layout->layer_list, link) {
		send_layer_prop(ivilayer);
	}

	wl_list_for_each_reverse(ivisurf, &layout->surface_list, link) {
		send_surface_prop(ivisurf);
	}
}

static void
clear_surface_pending_list(struct ivi_layout_layer *ivilayer)
{
	struct ivi_layout_surface *surface_link = NULL;
	struct ivi_layout_surface *surface_next = NULL;

	wl_list_for_each_safe(surface_link, surface_next,
			      &ivilayer->pending.surface_list, pending.link) {
		if (!wl_list_empty(&surface_link->pending.link)) {
			wl_list_remove(&surface_link->pending.link);
		}

		wl_list_init(&surface_link->pending.link);
	}

	ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
}

static void
clear_surface_order_list(struct ivi_layout_layer *ivilayer)
{
	struct ivi_layout_surface *surface_link = NULL;
	struct ivi_layout_surface *surface_next = NULL;

	wl_list_for_each_safe(surface_link, surface_next,
			      &ivilayer->order.surface_list, order.link) {
		if (!wl_list_empty(&surface_link->order.link)) {
			wl_list_remove(&surface_link->order.link);
		}

		wl_list_init(&surface_link->order.link);
	}

	ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
}

static void
layer_created(struct wl_listener *listener, void *data)
{
	struct ivi_layout_layer *ivilayer = data;

	struct listener_layout_notification *notification =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *created_callback =
		notification->userdata;

	((layer_create_notification_func)created_callback->callback)
		(ivilayer, created_callback->data);
}

static void
layer_removed(struct wl_listener *listener, void *data)
{
	struct ivi_layout_layer *ivilayer = data;

	struct listener_layout_notification *notification =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *removed_callback =
		notification->userdata;

	((layer_remove_notification_func)removed_callback->callback)
		(ivilayer, removed_callback->data);
}

static void
layer_prop_changed(struct wl_listener *listener, void *data)
{
	struct ivi_layout_layer *ivilayer = data;

	struct listener_layout_notification *layout_listener =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *prop_callback =
		layout_listener->userdata;

	((layer_property_notification_func)prop_callback->callback)
		(ivilayer, &ivilayer->prop, ivilayer->event_mask, prop_callback->data);
}

static void
surface_created(struct wl_listener *listener, void *data)
{
	struct ivi_layout_surface *ivisurface = data;

	struct listener_layout_notification *notification =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *created_callback =
		notification->userdata;

	((surface_create_notification_func)created_callback->callback)
		(ivisurface, created_callback->data);
}

static void
surface_removed(struct wl_listener *listener, void *data)
{
	struct ivi_layout_surface *ivisurface = data;

	struct listener_layout_notification *notification =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *removed_callback =
		notification->userdata;

	((surface_remove_notification_func)removed_callback->callback)
		(ivisurface, removed_callback->data);
}

static void
surface_prop_changed(struct wl_listener *listener, void *data)
{
	struct ivi_layout_surface *ivisurf = data;

	struct listener_layout_notification *layout_listener =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *prop_callback =
		layout_listener->userdata;

	((surface_property_notification_func)prop_callback->callback)
		(ivisurf, &ivisurf->prop, ivisurf->event_mask, prop_callback->data);

	ivisurf->event_mask = 0;
}

static void
surface_configure_changed(struct wl_listener *listener,
			  void *data)
{
	struct ivi_layout_surface *ivisurface = data;

	struct listener_layout_notification *notification =
		container_of(listener,
			     struct listener_layout_notification,
			     listener);

	struct ivi_layout_notification_callback *configure_changed_callback =
		notification->userdata;

	((surface_configure_notification_func)configure_changed_callback->callback)
		(ivisurface, configure_changed_callback->data);
}

static int32_t
add_notification(struct wl_signal *signal,
		 wl_notify_func_t callback,
		 void *userdata)
{
	struct listener_layout_notification *notification = NULL;

	notification = malloc(sizeof *notification);
	if (notification == NULL) {
		weston_log("fails to allocate memory\n");
		free(userdata);
		return IVI_FAILED;
	}

	notification->listener.notify = callback;
	notification->userdata = userdata;

	wl_signal_add(signal, &notification->listener);

	return IVI_SUCCEEDED;
}

static void
remove_notification(struct wl_list *listener_list, void *callback, void *userdata)
{
	struct wl_listener *listener = NULL;
	struct wl_listener *next = NULL;

	wl_list_for_each_safe(listener, next, listener_list, link) {
		struct listener_layout_notification *notification =
			container_of(listener,
				     struct listener_layout_notification,
				     listener);

		struct ivi_layout_notification_callback *notification_callback =
			notification->userdata;

		if ((notification_callback->callback != callback) ||
		    (notification_callback->data != userdata)) {
			continue;
		}

		if (!wl_list_empty(&listener->link)) {
			wl_list_remove(&listener->link);
		}

		free(notification->userdata);
		free(notification);
	}
}

/**
 * Exported APIs of ivi-layout library are implemented from here.
 * Brief of APIs is described in ivi-layout-export.h.
 */
WL_EXPORT int32_t
ivi_layout_add_notification_create_layer(layer_create_notification_func callback,
					 void *userdata)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_notification_callback *created_callback = NULL;

	if (callback == NULL) {
		weston_log("ivi_layout_add_notification_create_layer: invalid argument\n");
		return IVI_FAILED;
	}

	created_callback = malloc(sizeof *created_callback);
	if (created_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	created_callback->callback = callback;
	created_callback->data = userdata;

	return add_notification(&layout->layer_notification.created,
				layer_created,
				created_callback);
}

WL_EXPORT void
ivi_layout_remove_notification_create_layer(layer_create_notification_func callback,
					    void *userdata)
{
	struct ivi_layout *layout = get_instance();
	remove_notification(&layout->layer_notification.created.listener_list, callback, userdata);
}

WL_EXPORT int32_t
ivi_layout_add_notification_remove_layer(layer_remove_notification_func callback,
					 void *userdata)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_notification_callback *removed_callback = NULL;

	if (callback == NULL) {
		weston_log("ivi_layout_add_notification_remove_layer: invalid argument\n");
		return IVI_FAILED;
	}

	removed_callback = malloc(sizeof *removed_callback);
	if (removed_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	removed_callback->callback = callback;
	removed_callback->data = userdata;
	return add_notification(&layout->layer_notification.removed,
				layer_removed,
				removed_callback);
}

WL_EXPORT void
ivi_layout_remove_notification_remove_layer(layer_remove_notification_func callback,
					    void *userdata)
{
	struct ivi_layout *layout = get_instance();
	remove_notification(&layout->layer_notification.removed.listener_list, callback, userdata);
}

WL_EXPORT int32_t
ivi_layout_add_notification_create_surface(surface_create_notification_func callback,
					   void *userdata)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_notification_callback *created_callback = NULL;

	if (callback == NULL) {
		weston_log("ivi_layout_add_notification_create_surface: invalid argument\n");
		return IVI_FAILED;
	}

	created_callback = malloc(sizeof *created_callback);
	if (created_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	created_callback->callback = callback;
	created_callback->data = userdata;

	return add_notification(&layout->surface_notification.created,
				surface_created,
				created_callback);
}

WL_EXPORT void
ivi_layout_remove_notification_create_surface(surface_create_notification_func callback,
					      void *userdata)
{
	struct ivi_layout *layout = get_instance();
	remove_notification(&layout->surface_notification.created.listener_list, callback, userdata);
}

WL_EXPORT int32_t
ivi_layout_add_notification_remove_surface(surface_remove_notification_func callback,
					   void *userdata)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_notification_callback *removed_callback = NULL;

	if (callback == NULL) {
		weston_log("ivi_layout_add_notification_remove_surface: invalid argument\n");
		return IVI_FAILED;
	}

	removed_callback = malloc(sizeof *removed_callback);
	if (removed_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	removed_callback->callback = callback;
	removed_callback->data = userdata;

	return add_notification(&layout->surface_notification.removed,
				surface_removed,
				removed_callback);
}

WL_EXPORT void
ivi_layout_remove_notification_remove_surface(surface_remove_notification_func callback,
					      void *userdata)
{
	struct ivi_layout *layout = get_instance();
	remove_notification(&layout->surface_notification.removed.listener_list, callback, userdata);
}

WL_EXPORT int32_t
ivi_layout_add_notification_configure_surface(surface_configure_notification_func callback,
					      void *userdata)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_notification_callback *configure_changed_callback = NULL;
	if (callback == NULL) {
		weston_log("ivi_layout_add_notification_configure_surface: invalid argument\n");
		return IVI_FAILED;
	}

	configure_changed_callback = malloc(sizeof *configure_changed_callback);
	if (configure_changed_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	configure_changed_callback->callback = callback;
	configure_changed_callback->data = userdata;

	return add_notification(&layout->surface_notification.configure_changed,
				surface_configure_changed,
				configure_changed_callback);
}

WL_EXPORT void
ivi_layout_remove_notification_configure_surface(surface_configure_notification_func callback,
						 void *userdata)
{
	struct ivi_layout *layout = get_instance();
	remove_notification(&layout->surface_notification.configure_changed.listener_list, callback, userdata);
}

WL_EXPORT uint32_t
ivi_layout_get_id_of_surface(struct ivi_layout_surface *ivisurf)
{
	return ivisurf->id_surface;
}

WL_EXPORT uint32_t
ivi_layout_get_id_of_layer(struct ivi_layout_layer *ivilayer)
{
	return ivilayer->id_layer;
}

struct ivi_layout_layer *
ivi_layout_get_layer_from_id(uint32_t id_layer)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_layer *ivilayer = NULL;

	wl_list_for_each(ivilayer, &layout->layer_list, link) {
		if (ivilayer->id_layer == id_layer) {
			return ivilayer;
		}
	}

	return NULL;
}

WL_EXPORT struct ivi_layout_surface *
ivi_layout_get_surface_from_id(uint32_t id_surface)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_surface *ivisurf = NULL;

	wl_list_for_each(ivisurf, &layout->surface_list, link) {
		if (ivisurf->id_surface == id_surface) {
			return ivisurf;
		}
	}

	return NULL;
}

WL_EXPORT struct ivi_layout_screen *
ivi_layout_get_screen_from_id(uint32_t id_screen)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_screen *iviscrn = NULL;

	wl_list_for_each(iviscrn, &layout->screen_list, link) {
/* FIXME : select iviscrn from screen_list by id_screen */
		return iviscrn;
		break;
	}

	return NULL;
}

WL_EXPORT int32_t
ivi_layout_get_screen_resolution(struct ivi_layout_screen *iviscrn,
				 int32_t *pWidth, int32_t *pHeight)
{
	struct weston_output *output = NULL;

	if (pWidth == NULL || pHeight == NULL) {
		weston_log("ivi_layout_get_screen_resolution: invalid argument\n");
		return IVI_FAILED;
	}

	output   = iviscrn->output;
	*pWidth  = output->current_mode->width;
	*pHeight = output->current_mode->height;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_surface_add_notification(struct ivi_layout_surface *ivisurf,
				    surface_property_notification_func callback,
				    void *userdata)
{
	struct listener_layout_notification* notification = NULL;
	struct ivi_layout_notification_callback *prop_callback = NULL;

	if (ivisurf == NULL || callback == NULL) {
		weston_log("ivi_layout_surface_add_notification: invalid argument\n");
		return IVI_FAILED;
	}

	notification = malloc(sizeof *notification);
	if (notification == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	prop_callback = malloc(sizeof *prop_callback);
	if (prop_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	prop_callback->callback = callback;
	prop_callback->data = userdata;

	notification->listener.notify = surface_prop_changed;
	notification->userdata = prop_callback;

	wl_signal_add(&ivisurf->property_changed, &notification->listener);

	return IVI_SUCCEEDED;
}

WL_EXPORT const struct ivi_layout_layer_properties *
ivi_layout_get_properties_of_layer(struct ivi_layout_layer *ivilayer)
{
	if (ivilayer == NULL) {
		weston_log("ivi_layout_get_properties_of_layer: invalid argument\n");
		return NULL;
	}

	return &ivilayer->prop;
}

WL_EXPORT int32_t
ivi_layout_get_screens(int32_t *pLength, struct ivi_layout_screen ***ppArray)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_screen *iviscrn = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_get_screens: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&layout->screen_list);

	if (length != 0){
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_screen *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(iviscrn, &layout->screen_list, link) {
			(*ppArray)[n++] = iviscrn;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_get_screens_under_layer(struct ivi_layout_layer *ivilayer,
				   int32_t *pLength,
				   struct ivi_layout_screen ***ppArray)
{
	struct link_screen *link_scrn = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (ivilayer == NULL || pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_get_screens_under_layer: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&ivilayer->screen_list);

	if (length != 0){
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_screen *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(link_scrn, &ivilayer->screen_list, link) {
			(*ppArray)[n++] = link_scrn->iviscrn;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_get_layers(int32_t *pLength, struct ivi_layout_layer ***ppArray)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_layer *ivilayer = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_get_layers: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&layout->layer_list);

	if (length != 0){
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(ivilayer, &layout->layer_list, link) {
			(*ppArray)[n++] = ivilayer;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_get_layers_on_screen(struct ivi_layout_screen *iviscrn,
				int32_t *pLength,
				struct ivi_layout_layer ***ppArray)
{
	struct ivi_layout_layer *ivilayer = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (iviscrn == NULL || pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_get_layers_on_screen: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&iviscrn->order.layer_list);

	if (length != 0){
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(ivilayer, &iviscrn->order.layer_list, link) {
			(*ppArray)[n++] = ivilayer;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_get_layers_under_surface(struct ivi_layout_surface *ivisurf,
				    int32_t *pLength,
				    struct ivi_layout_layer ***ppArray)
{
	struct link_layer *link_layer = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (ivisurf == NULL || pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_getLayers: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&ivisurf->layer_list);

	if (length != 0){
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(link_layer, &ivisurf->layer_list, link) {
			(*ppArray)[n++] = link_layer->ivilayer;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_get_surfaces(int32_t *pLength, struct ivi_layout_surface ***ppArray)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_surface *ivisurf = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_get_surfaces: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&layout->surface_list);

	if (length != 0){
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_surface *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(ivisurf, &layout->surface_list, link) {
			(*ppArray)[n++] = ivisurf;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_get_surfaces_on_layer(struct ivi_layout_layer *ivilayer,
				 int32_t *pLength,
				 struct ivi_layout_surface ***ppArray)
{
	struct ivi_layout_surface *ivisurf = NULL;
	int32_t length = 0;
	int32_t n = 0;

	if (ivilayer == NULL || pLength == NULL || ppArray == NULL) {
		weston_log("ivi_layout_getSurfaceIDsOnLayer: invalid argument\n");
		return IVI_FAILED;
	}

	length = wl_list_length(&ivilayer->order.surface_list);

	if (length != 0) {
		/* the Array must be free by module which called this function */
		*ppArray = calloc(length, sizeof(struct ivi_layout_surface *));
		if (*ppArray == NULL) {
			weston_log("fails to allocate memory\n");
			return IVI_FAILED;
		}

		wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
			(*ppArray)[n++] = ivisurf;
		}
	}

	*pLength = length;

	return IVI_SUCCEEDED;
}

WL_EXPORT struct ivi_layout_layer *
ivi_layout_layer_create_with_dimension(uint32_t id_layer,
				       int32_t width, int32_t height)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_layer *ivilayer = NULL;

	ivilayer = get_layer(&layout->layer_list, id_layer);
	if (ivilayer != NULL) {
		weston_log("id_layer is already created\n");
		return ivilayer;
	}

	ivilayer = calloc(1, sizeof *ivilayer);
	if (ivilayer == NULL) {
		weston_log("fails to allocate memory\n");
		return NULL;
	}

	wl_list_init(&ivilayer->link);
	wl_signal_init(&ivilayer->property_changed);
	wl_list_init(&ivilayer->screen_list);
	wl_list_init(&ivilayer->link_to_surface);
	ivilayer->layout = layout;
	ivilayer->id_layer = id_layer;

	init_layer_properties(&ivilayer->prop, width, height);
	ivilayer->event_mask = 0;

	wl_list_init(&ivilayer->pending.surface_list);
	wl_list_init(&ivilayer->pending.link);
	ivilayer->pending.prop = ivilayer->prop;

	wl_list_init(&ivilayer->order.surface_list);
	wl_list_init(&ivilayer->order.link);

	wl_list_insert(&layout->layer_list, &ivilayer->link);

	wl_signal_emit(&layout->layer_notification.created, ivilayer);

	return ivilayer;
}

WL_EXPORT void
ivi_layout_layer_remove_notification(struct ivi_layout_layer *ivilayer)
{
	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_remove_notification: invalid argument\n");
		return;
	}

	remove_all_notification(&ivilayer->property_changed.listener_list);
}

WL_EXPORT void
ivi_layout_layer_remove(struct ivi_layout_layer *ivilayer)
{
	struct ivi_layout *layout = get_instance();

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_remove: invalid argument\n");
		return;
	}

	wl_signal_emit(&layout->layer_notification.removed, ivilayer);

	clear_surface_pending_list(ivilayer);
	clear_surface_order_list(ivilayer);

	if (!wl_list_empty(&ivilayer->pending.link)) {
		wl_list_remove(&ivilayer->pending.link);
	}
	if (!wl_list_empty(&ivilayer->order.link)) {
		wl_list_remove(&ivilayer->order.link);
	}
	if (!wl_list_empty(&ivilayer->link)) {
		wl_list_remove(&ivilayer->link);
	}
	remove_orderlayer_from_screen(ivilayer);
	remove_link_to_surface(ivilayer);
	ivi_layout_layer_remove_notification(ivilayer);

	free(ivilayer);
}

WL_EXPORT int32_t
ivi_layout_layer_set_visibility(struct ivi_layout_layer *ivilayer,
				bool newVisibility)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_visibility: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;
	prop->visibility = newVisibility;

	ivilayer->event_mask |= IVI_NOTIFICATION_VISIBILITY;

	return IVI_SUCCEEDED;
}

bool
ivi_layout_layer_get_visibility(struct ivi_layout_layer *ivilayer)
{
	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_get_visibility: invalid argument\n");
		return false;
	}

	return ivilayer->prop.visibility;
}

WL_EXPORT int32_t
ivi_layout_layer_set_opacity(struct ivi_layout_layer *ivilayer,
			     wl_fixed_t opacity)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_opacity: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;
	prop->opacity = opacity;

	ivilayer->event_mask |= IVI_NOTIFICATION_OPACITY;

	return IVI_SUCCEEDED;
}

WL_EXPORT wl_fixed_t
ivi_layout_layer_get_opacity(struct ivi_layout_layer *ivilayer)
{
	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_get_opacity: invalid argument\n");
		return wl_fixed_from_double(0.0);
	}

	return ivilayer->prop.opacity;
}

WL_EXPORT int32_t
ivi_layout_layer_set_source_rectangle(struct ivi_layout_layer *ivilayer,
				      int32_t x, int32_t y,
				      int32_t width, int32_t height)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_source_rectangle: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;
	prop->source_x = x;
	prop->source_y = y;
	prop->source_width = width;
	prop->source_height = height;

	ivilayer->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_layer_set_destination_rectangle(struct ivi_layout_layer *ivilayer,
					   int32_t x, int32_t y,
					   int32_t width, int32_t height)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_destination_rectangle: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;
	prop->dest_x = x;
	prop->dest_y = y;
	prop->dest_width = width;
	prop->dest_height = height;

	ivilayer->event_mask |= IVI_NOTIFICATION_DEST_RECT;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_layer_get_dimension(struct ivi_layout_layer *ivilayer,
			       int32_t *dest_width, int32_t *dest_height)
{
	if (ivilayer == NULL || dest_width == NULL || dest_height == NULL) {
		weston_log("ivi_layout_layer_get_dimension: invalid argument\n");
		return IVI_FAILED;
	}

	*dest_width = ivilayer->prop.dest_width;
	*dest_height = ivilayer->prop.dest_height;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_layer_set_dimension(struct ivi_layout_layer *ivilayer,
			       int32_t dest_width, int32_t dest_height)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_dimension: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;

	prop->dest_width  = dest_width;
	prop->dest_height = dest_height;

	ivilayer->event_mask |= IVI_NOTIFICATION_DIMENSION;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_layer_get_position(struct ivi_layout_layer *ivilayer,
			      int32_t *dest_x, int32_t *dest_y)
{
	if (ivilayer == NULL || dest_x == NULL || dest_y == NULL) {
		weston_log("ivi_layout_layer_get_position: invalid argument\n");
		return IVI_FAILED;
	}

	*dest_x = ivilayer->prop.dest_x;
	*dest_y = ivilayer->prop.dest_y;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_layer_set_position(struct ivi_layout_layer *ivilayer,
			      int32_t dest_x, int32_t dest_y)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_position: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;
	prop->dest_x = dest_x;
	prop->dest_y = dest_y;

	ivilayer->event_mask |= IVI_NOTIFICATION_POSITION;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_layer_set_orientation(struct ivi_layout_layer *ivilayer,
				 enum wl_output_transform orientation)
{
	struct ivi_layout_layer_properties *prop = NULL;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_orientation: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivilayer->pending.prop;
	prop->orientation = orientation;

	ivilayer->event_mask |= IVI_NOTIFICATION_ORIENTATION;

	return IVI_SUCCEEDED;
}

enum wl_output_transform
ivi_layout_layer_get_orientation(struct ivi_layout_layer *ivilayer)
{
	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_get_orientation: invalid argument\n");
		return 0;
	}

	return ivilayer->prop.orientation;
}

WL_EXPORT int32_t
ivi_layout_layer_set_render_order(struct ivi_layout_layer *ivilayer,
				  struct ivi_layout_surface **pSurface,
				  int32_t number)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_surface *ivisurf = NULL;
	struct ivi_layout_surface *next = NULL;
	uint32_t *id_surface = NULL;
	int32_t i = 0;

	if (ivilayer == NULL) {
		weston_log("ivi_layout_layer_set_render_order: invalid argument\n");
		return IVI_FAILED;
	}

	if (pSurface == NULL) {
		wl_list_for_each_safe(ivisurf, next, &ivilayer->pending.surface_list, pending.link) {
			if (!wl_list_empty(&ivisurf->pending.link)) {
				wl_list_remove(&ivisurf->pending.link);
			}

			wl_list_init(&ivisurf->pending.link);
		}
		ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
		return IVI_SUCCEEDED;
	}

	for (i = 0; i < number; i++) {
		id_surface = &pSurface[i]->id_surface;

		wl_list_for_each_safe(ivisurf, next, &layout->surface_list, link) {
			if (*id_surface != ivisurf->id_surface) {
				continue;
			}

			if (!wl_list_empty(&ivisurf->pending.link)) {
				wl_list_remove(&ivisurf->pending.link);
			}
			wl_list_init(&ivisurf->pending.link);
			wl_list_insert(&ivilayer->pending.surface_list,
				       &ivisurf->pending.link);
			break;
		}
	}

	ivilayer->event_mask |= IVI_NOTIFICATION_ADD;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_surface_set_visibility(struct ivi_layout_surface *ivisurf,
				  bool newVisibility)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_visibility: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->visibility = newVisibility;

	ivisurf->event_mask |= IVI_NOTIFICATION_VISIBILITY;

	return IVI_SUCCEEDED;
}

WL_EXPORT bool
ivi_layout_surface_get_visibility(struct ivi_layout_surface *ivisurf)
{
	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_get_visibility: invalid argument\n");
		return false;
	}

	return ivisurf->prop.visibility;
}

WL_EXPORT int32_t
ivi_layout_surface_set_opacity(struct ivi_layout_surface *ivisurf,
			       wl_fixed_t opacity)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_opacity: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->opacity = opacity;

	ivisurf->event_mask |= IVI_NOTIFICATION_OPACITY;

	return IVI_SUCCEEDED;
}

WL_EXPORT wl_fixed_t
ivi_layout_surface_get_opacity(struct ivi_layout_surface *ivisurf)
{
	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_get_opacity: invalid argument\n");
		return wl_fixed_from_double(0.0);
	}

	return ivisurf->prop.opacity;
}

WL_EXPORT int32_t
ivi_layout_surface_set_destination_rectangle(struct ivi_layout_surface *ivisurf,
					     int32_t x, int32_t y,
					     int32_t width, int32_t height)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_destination_rectangle: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->start_x = prop->dest_x;
	prop->start_y = prop->dest_y;
	prop->dest_x = x;
	prop->dest_y = y;
	prop->start_width = prop->dest_width;
	prop->start_height = prop->dest_height;
	prop->dest_width = width;
	prop->dest_height = height;

	ivisurf->event_mask |= IVI_NOTIFICATION_DEST_RECT;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_surface_set_dimension(struct ivi_layout_surface *ivisurf,
				 int32_t dest_width, int32_t dest_height)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_dimension: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->dest_width  = dest_width;
	prop->dest_height = dest_height;

	ivisurf->event_mask |= IVI_NOTIFICATION_DIMENSION;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_surface_get_dimension(struct ivi_layout_surface *ivisurf,
				 int32_t *dest_width, int32_t *dest_height)
{
	if (ivisurf == NULL || dest_width == NULL ||  dest_height == NULL) {
		weston_log("ivi_layout_surface_get_dimension: invalid argument\n");
		return IVI_FAILED;
	}

	*dest_width = ivisurf->prop.dest_width;
	*dest_height = ivisurf->prop.dest_height;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_surface_set_position(struct ivi_layout_surface *ivisurf,
				int32_t dest_x, int32_t dest_y)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_position: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->dest_x = dest_x;
	prop->dest_y = dest_y;

	ivisurf->event_mask |= IVI_NOTIFICATION_POSITION;

	return IVI_SUCCEEDED;
}

int32_t
ivi_layout_surface_get_position(struct ivi_layout_surface *ivisurf,
				int32_t *dest_x, int32_t *dest_y)
{
	if (ivisurf == NULL || dest_x == NULL || dest_y == NULL) {
		weston_log("ivi_layout_surface_get_position: invalid argument\n");
		return IVI_FAILED;
	}

	*dest_x = ivisurf->prop.dest_x;
	*dest_y = ivisurf->prop.dest_y;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_surface_set_orientation(struct ivi_layout_surface *ivisurf,
				   enum wl_output_transform orientation)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_orientation: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->orientation = orientation;

	ivisurf->event_mask |= IVI_NOTIFICATION_ORIENTATION;

	return IVI_SUCCEEDED;
}

enum wl_output_transform
ivi_layout_surface_get_orientation(struct ivi_layout_surface *ivisurf)
{
	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_get_orientation: invalid argument\n");
		return 0;
	}

	return ivisurf->prop.orientation;
}

WL_EXPORT int32_t
ivi_layout_screen_add_layer(struct ivi_layout_screen *iviscrn,
			    struct ivi_layout_layer *addlayer)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_layer *ivilayer = NULL;
	struct ivi_layout_layer *next = NULL;
	int is_layer_in_scrn = 0;

	if (iviscrn == NULL || addlayer == NULL) {
		weston_log("ivi_layout_screen_add_layer: invalid argument\n");
		return IVI_FAILED;
	}

	is_layer_in_scrn = is_layer_in_screen(addlayer, iviscrn);
	if (is_layer_in_scrn == 1) {
		weston_log("ivi_layout_screen_add_layer: addlayer is already available\n");
		return IVI_SUCCEEDED;
	}

	wl_list_for_each_safe(ivilayer, next, &layout->layer_list, link) {
		if (ivilayer->id_layer == addlayer->id_layer) {
			if (!wl_list_empty(&ivilayer->pending.link)) {
				wl_list_remove(&ivilayer->pending.link);
			}
			wl_list_init(&ivilayer->pending.link);
			wl_list_insert(&iviscrn->pending.layer_list,
				       &ivilayer->pending.link);
			break;
		}
	}

	iviscrn->event_mask |= IVI_NOTIFICATION_ADD;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_screen_set_render_order(struct ivi_layout_screen *iviscrn,
				   struct ivi_layout_layer **pLayer,
				   const int32_t number)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_layer *ivilayer = NULL;
	struct ivi_layout_layer *next = NULL;
	uint32_t *id_layer = NULL;
	int32_t i = 0;

	if (iviscrn == NULL) {
		weston_log("ivi_layout_screen_set_render_order: invalid argument\n");
		return IVI_FAILED;
	}

	wl_list_for_each_safe(ivilayer, next,
			      &iviscrn->pending.layer_list, pending.link) {
		wl_list_init(&ivilayer->pending.link);
	}

	wl_list_init(&iviscrn->pending.layer_list);

	if (pLayer == NULL) {
		wl_list_for_each_safe(ivilayer, next, &iviscrn->pending.layer_list, pending.link) {
			if (!wl_list_empty(&ivilayer->pending.link)) {
				wl_list_remove(&ivilayer->pending.link);
			}

			wl_list_init(&ivilayer->pending.link);
		}

		iviscrn->event_mask |= IVI_NOTIFICATION_REMOVE;
		return IVI_SUCCEEDED;
	}

	for (i = 0; i < number; i++) {
		id_layer = &pLayer[i]->id_layer;
		wl_list_for_each(ivilayer, &layout->layer_list, link) {
			if (*id_layer != ivilayer->id_layer) {
				continue;
			}

			if (!wl_list_empty(&ivilayer->pending.link)) {
				wl_list_remove(&ivilayer->pending.link);
			}
			wl_list_init(&ivilayer->pending.link);
			wl_list_insert(&iviscrn->pending.layer_list,
				       &ivilayer->pending.link);
			break;
		}
	}

	iviscrn->event_mask |= IVI_NOTIFICATION_ADD;

	return IVI_SUCCEEDED;
}

WL_EXPORT struct weston_output *
ivi_layout_screen_get_output(struct ivi_layout_screen *iviscrn)
{
	return iviscrn->output;
}

/**
 * This function is used by the additional ivi-module because of dumping ivi_surface sceenshot.
 * The ivi-module, e.g. ivi-controller.so, is in wayland-ivi-extension of Genivi's Layer Management.
 * This function is used to get the result of drawing by clients.
 */
WL_EXPORT struct weston_surface *
ivi_layout_surface_get_weston_surface(struct ivi_layout_surface *ivisurf)
{
	return ivisurf != NULL ? ivisurf->surface : NULL;
}

WL_EXPORT int32_t
ivi_layout_layer_add_notification(struct ivi_layout_layer *ivilayer,
				  layer_property_notification_func callback,
				  void *userdata)
{
	struct ivi_layout_notification_callback *prop_callback = NULL;

	if (ivilayer == NULL || callback == NULL) {
		weston_log("ivi_layout_layer_add_notification: invalid argument\n");
		return IVI_FAILED;
	}

	prop_callback = malloc(sizeof *prop_callback);
	if (prop_callback == NULL) {
		weston_log("fails to allocate memory\n");
		return IVI_FAILED;
	}

	prop_callback->callback = callback;
	prop_callback->data = userdata;

	return add_notification(&ivilayer->property_changed,
				layer_prop_changed,
				prop_callback);
}

WL_EXPORT const struct ivi_layout_surface_properties *
ivi_layout_get_properties_of_surface(struct ivi_layout_surface *ivisurf)
{
	if (ivisurf == NULL) {
		weston_log("ivi_layout_get_properties_of_surface: invalid argument\n");
		return NULL;
	}

	return &ivisurf->prop;
}

WL_EXPORT int32_t
ivi_layout_layer_add_surface(struct ivi_layout_layer *ivilayer,
			     struct ivi_layout_surface *addsurf)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_surface *ivisurf = NULL;
	struct ivi_layout_surface *next = NULL;
	int is_surf_in_layer = 0;

	if (ivilayer == NULL || addsurf == NULL) {
		weston_log("ivi_layout_layer_add_surface: invalid argument\n");
		return IVI_FAILED;
	}

	is_surf_in_layer = is_surface_in_layer(addsurf, ivilayer);
	if (is_surf_in_layer == 1) {
		weston_log("ivi_layout_layer_add_surface: addsurf is already available\n");
		return IVI_SUCCEEDED;
	}

	wl_list_for_each_safe(ivisurf, next, &layout->surface_list, link) {
		if (ivisurf->id_surface == addsurf->id_surface) {
			if (!wl_list_empty(&ivisurf->pending.link)) {
				wl_list_remove(&ivisurf->pending.link);
			}
			wl_list_init(&ivisurf->pending.link);
			wl_list_insert(&ivilayer->pending.surface_list,
				       &ivisurf->pending.link);
			break;
		}
	}

	ivilayer->event_mask |= IVI_NOTIFICATION_ADD;

	return IVI_SUCCEEDED;
}

WL_EXPORT void
ivi_layout_layer_remove_surface(struct ivi_layout_layer *ivilayer,
				struct ivi_layout_surface *remsurf)
{
	struct ivi_layout_surface *ivisurf = NULL;
	struct ivi_layout_surface *next = NULL;

	if (ivilayer == NULL || remsurf == NULL) {
		weston_log("ivi_layout_layer_remove_surface: invalid argument\n");
		return;
	}

	wl_list_for_each_safe(ivisurf, next,
			      &ivilayer->pending.surface_list, pending.link) {
		if (ivisurf->id_surface == remsurf->id_surface) {
			if (!wl_list_empty(&ivisurf->pending.link)) {
				wl_list_remove(&ivisurf->pending.link);
			}
			wl_list_init(&ivisurf->pending.link);
			break;
		}
	}

	remsurf->event_mask |= IVI_NOTIFICATION_REMOVE;
}

WL_EXPORT int32_t
ivi_layout_surface_set_source_rectangle(struct ivi_layout_surface *ivisurf,
					int32_t x, int32_t y,
					int32_t width, int32_t height)
{
	struct ivi_layout_surface_properties *prop = NULL;

	if (ivisurf == NULL) {
		weston_log("ivi_layout_surface_set_source_rectangle: invalid argument\n");
		return IVI_FAILED;
	}

	prop = &ivisurf->pending.prop;
	prop->source_x = x;
	prop->source_y = y;
	prop->source_width = width;
	prop->source_height = height;

	ivisurf->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;

	return IVI_SUCCEEDED;
}

WL_EXPORT int32_t
ivi_layout_commit_changes(void)
{
	struct ivi_layout *layout = get_instance();

	commit_surface_list(layout);
	commit_layer_list(layout);
	commit_screen_list(layout);

	commit_transition(layout);

	commit_changes(layout);
	send_prop(layout);
	weston_compositor_schedule_repaint(layout->compositor);

	return IVI_SUCCEEDED;
}

/***called from ivi-shell**/
static struct weston_view *
ivi_layout_get_weston_view(struct ivi_layout_surface *surface)
{
	struct weston_view *tmpview = NULL;

	if(surface == NULL)
		return NULL;

	wl_list_for_each(tmpview, &surface->surface->views, surface_link)
	{
		if (tmpview != NULL) {
			break;
		}
	}
	return tmpview;
}

static void
ivi_layout_surface_configure(struct ivi_layout_surface *ivisurf,
			     int32_t width, int32_t height)
{
	struct ivi_layout *layout = get_instance();
	int32_t in_init = 0;
	ivisurf->surface->width_from_buffer  = width;
	ivisurf->surface->height_from_buffer = height;

	if (ivisurf->prop.source_width == 0 || ivisurf->prop.source_height == 0) {
		in_init = 1;
	}

	/* FIXME: when sourceHeight/Width is used as clipping range in image buffer */
	/* if (ivisurf->prop.sourceWidth == 0 || ivisurf->prop.sourceHeight == 0) { */
		ivisurf->pending.prop.source_width = width;
		ivisurf->pending.prop.source_height = height;
		ivisurf->prop.source_width = width;
		ivisurf->prop.source_height = height;
	/* } */

	ivisurf->event_mask |= IVI_NOTIFICATION_CONFIGURE;

	if (in_init) {
		wl_signal_emit(&layout->surface_notification.configure_changed, ivisurf);
	} else {
		ivi_layout_commit_changes();
	}
}

WL_EXPORT int32_t
ivi_layout_surface_set_content_observer(struct ivi_layout_surface *ivisurf,
					ivi_controller_surface_content_callback callback,
					void* userdata)
{
	int32_t ret = IVI_FAILED;

	if (ivisurf != NULL) {
		ivisurf->content_observer.callback = callback;
		ivisurf->content_observer.userdata = userdata;
		ret = IVI_SUCCEEDED;
	}
	return ret;
}

static struct ivi_layout_surface*
ivi_layout_surface_create(struct weston_surface *wl_surface,
			  uint32_t id_surface)
{
	struct ivi_layout *layout = get_instance();
	struct ivi_layout_surface *ivisurf = NULL;
	struct weston_view *tmpview = NULL;

	if (wl_surface == NULL) {
		weston_log("ivi_layout_surface_create: invalid argument\n");
		return NULL;
	}

	ivisurf = get_surface(&layout->surface_list, id_surface);
	if (ivisurf != NULL) {
		if (ivisurf->surface != NULL) {
			weston_log("id_surface(%d) is already created\n", id_surface);
			return NULL;
		}
	}

	ivisurf = calloc(1, sizeof *ivisurf);
	if (ivisurf == NULL) {
		weston_log("fails to allocate memory\n");
		return NULL;
	}

	wl_list_init(&ivisurf->link);
	wl_signal_init(&ivisurf->property_changed);
	wl_signal_init(&ivisurf->configured);
	wl_list_init(&ivisurf->layer_list);
	ivisurf->id_surface = id_surface;
	ivisurf->layout = layout;

	ivisurf->surface = wl_surface;
	ivisurf->surface_destroy_listener.notify =
		westonsurface_destroy_from_ivisurface;
	wl_resource_add_destroy_listener(wl_surface->resource,
					 &ivisurf->surface_destroy_listener);

	tmpview = weston_view_create(wl_surface);
	if (tmpview == NULL) {
		weston_log("fails to allocate memory\n");
	}

	ivisurf->surface->width_from_buffer  = 0;
	ivisurf->surface->height_from_buffer = 0;

	weston_matrix_init(&ivisurf->surface_rotation.matrix);
	weston_matrix_init(&ivisurf->layer_rotation.matrix);
	weston_matrix_init(&ivisurf->surface_pos.matrix);
	weston_matrix_init(&ivisurf->layer_pos.matrix);
	weston_matrix_init(&ivisurf->scaling.matrix);

	wl_list_init(&ivisurf->surface_rotation.link);
	wl_list_init(&ivisurf->layer_rotation.link);
	wl_list_init(&ivisurf->surface_pos.link);
	wl_list_init(&ivisurf->layer_pos.link);
	wl_list_init(&ivisurf->scaling.link);

	init_surface_properties(&ivisurf->prop);
	ivisurf->event_mask = 0;

	ivisurf->pending.prop = ivisurf->prop;
	wl_list_init(&ivisurf->pending.link);

	wl_list_init(&ivisurf->order.link);
	wl_list_init(&ivisurf->order.layer_list);

	wl_list_insert(&layout->surface_list, &ivisurf->link);

	wl_signal_emit(&layout->surface_notification.created, ivisurf);

	return ivisurf;
}

static void
ivi_layout_init_with_compositor(struct weston_compositor *ec)
{
	struct ivi_layout *layout = get_instance();

	layout->compositor = ec;

	wl_list_init(&layout->surface_list);
	wl_list_init(&layout->layer_list);
	wl_list_init(&layout->screen_list);

	wl_signal_init(&layout->layer_notification.created);
	wl_signal_init(&layout->layer_notification.removed);

	wl_signal_init(&layout->surface_notification.created);
	wl_signal_init(&layout->surface_notification.removed);
	wl_signal_init(&layout->surface_notification.configure_changed);

	/* Add layout_layer at the last of weston_compositor.layer_list */
	weston_layer_init(&layout->layout_layer, ec->layer_list.prev);

	create_screen(ec);

	layout->transitions = ivi_layout_transition_set_create(ec);
	wl_list_init(&layout->pending_transition_list);
}


static void
ivi_layout_surface_add_configured_listener(struct ivi_layout_surface* ivisurf,
					   struct wl_listener* listener)
{
	wl_signal_add(&ivisurf->configured, listener);
}

WL_EXPORT struct ivi_layout_interface ivi_layout_interface = {
	.get_weston_view = ivi_layout_get_weston_view,
	.surface_configure = ivi_layout_surface_configure,
	.surface_create = ivi_layout_surface_create,
	.init_with_compositor = ivi_layout_init_with_compositor,
	.get_surface_dimension = ivi_layout_surface_get_dimension,
	.add_surface_configured_listener = ivi_layout_surface_add_configured_listener
};