aboutsummaryrefslogblamecommitdiffstats
path: root/src/lib/efl_wl/efl_wl.c
blob: 16cd6627bc0ed847b28b633243bfe897eb3e1dd4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845

















                                                            
                                      


















































































                                                                                    
                            


















                                             


                                    



















                                   
                        
                        



















































































































































































                                                               
                           












































































                                           
          
                    


                                          


























                                                     















                                                       












                                                                 

                                           





                                                                                          

                                                          



                                                                               

                                                            




































































































































































































































































































































































































































































































































































                                                                                                                                
                



                                
               
 


                                               





































































































































                                                                                            





                                             
                             






                                                   






                                             
                             




                                                              























                                                                        

                                     






                                                                                               



                                                               










                                                                  
                                        
                                        












































                                                                                               
                                                     














































                                                                                                                                       
                                                                  








































                                                                                                                         

                                                                    




















                                                                




                                                                          































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                  
                            


































































































































































































































































                                                                                                          
                                                            





                                    
                                   


                                                        
                  
      





                                                                 
                                                       




                                                 



















































































































































                                                                                                                                    

                                                   
















































































































































                                                                                                                                                                         
                                                   
















































































































































































































































































































































                                                                                                                                                                                                            
                                                   















































                                                                                            
                                                   
































                                                                                                                                          
                                                                       























                                                                                                                       
 
           







                                                                                                                             
           







                                                                                                                             




























































































                                                                                                                                                   
                             












                                                                                    


































                                                                                                                                                                             
                                                                       



























































































































































































































                                                                                                                                                                                      
                                                   




































                                                                           
                

























                                                                                 







                                          








                                                                           



                                                       
                                           






                                                                






                                                                                                                   



































































































































                                                                                                                                                        
                                       

























                                                                                  



                                                                                                



























































































                                                                                                  
                                                   


































































































                                                                                      

                                                                













                                                                           

                                                   











































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                           



                                                                                   























































                                                                                                




                                                     




















































                                                                                                  

























                                                                                                                                                       


                                
                          






                                                





                                                   
                                            





                                                 






















                                                                                                   
                                                                            







































































                                                                                                         

                                           














































                                                                
                                                           


















































































                                                                  











                                                                                        










































                                                                                            











                                                                                                                                       
















                                                               




                                                                                              























                                              





                                             










































































                                                                       







                                                               
                                    





                                                                           







                                                               
                                    








                                                    










                                                                                                       






















































                                                                                                            
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-parameter"
#elif (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4
# pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

#include <unistd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <dlfcn.h>

#include <wayland-server.h>
#include "xdg-shell-unstable-v6-server-protocol.h"
#include "efl-hints-server-protocol.h"
#include "dmabuf.h"

#include "Ecore_Evas.h"
#include "Ecore_Wl2.h"
#include "Ecore_Input.h"
#include "Evas_GL.h"
# ifdef HAVE_ECORE_X
#include "Ecore_X.h"
#endif

#include "Efl_Wl.h"

#undef COORDS_INSIDE
#define COORDS_INSIDE(x, y, xx, yy, ww, hh) \
  (((x) < ((xx) + (ww))) && ((y) < ((yy) + (hh))) && ((x) >= (xx)) && ((y) >= (yy)))

#ifdef __linux__
# include <linux/input.h>
#else
# define BTN_LEFT 0x110
# define BTN_RIGHT 0x111
# define BTN_MIDDLE 0x112
# define BTN_SIDE 0x113
# define BTN_EXTRA 0x114
# define BTN_FORWARD 0x115
# define BTN_BACK 0x116
#endif

#undef container_of
#  define container_of(ptr, type, member) \
   ({ \
      const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
      (type *)(void *)( (char *)__mptr - offsetof(type,member) ); \
   })

#ifndef EGL_HEIGHT
# define EGL_HEIGHT			0x3056
#endif
#ifndef EGL_WIDTH
# define EGL_WIDTH			0x3057
#endif

#ifndef EGL_TEXTURE_FORMAT
# define EGL_TEXTURE_FORMAT		0x3080
#endif
#ifndef EGL_TEXTURE_RGBA
# define EGL_TEXTURE_RGBA		0x305E
#endif
#ifndef DRM_FORMAT_ARGB8888
# define DRM_FORMAT_ARGB8888           0x34325241
#endif
#ifndef DRM_FORMAT_XRGB8888
# define DRM_FORMAT_XRGB8888           0x34325258
#endif

#define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
		     WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
		     WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)


typedef struct Input_Sequence
{
   EINA_INLIST;
   uint32_t id;
   uint32_t down_serial;
   uint32_t down_time;
   uint32_t up_serial;
   uint32_t up_time;
   Eina_Bool pass : 1;
} Input_Sequence;

typedef struct Comp_Subsurface Comp_Subsurface;
typedef struct Comp_Surface Comp_Surface;

typedef struct Comp_Buffer
{
   struct wl_resource *res;
   Comp_Surface *cs;
   Eina_List *renders;
   Eina_List *post_renders;
   int x, y, w, h;
   struct wl_listener destroy_listener;
   struct wl_shm_buffer *shm_buffer;
   struct wl_shm_pool *pool;
   struct linux_dmabuf_buffer *dmabuf_buffer;
   Eina_Bool dbg : 1;
} Comp_Buffer;

typedef struct Comp
{
   Efl_Wl_Rotation rotation;
   double scale;
   char *env;
   Ecore_Wl2_Display *disp;
   Ecore_Wl2_Display *parent_disp;
   Ecore_Wl2_Display *client_disp;
   struct wl_display *display;

   double wayland_time_base;
   Evas_Object *obj;
   Evas_Object *clip;
   Evas_Object *events;

   Eina_Hash *exes;
   Ecore_Event_Handler *exe_handler;

   Eina_Inlist *surfaces;
   unsigned int surfaces_count;
   Eina_Hash *client_surfaces;
   Comp_Surface *active_surface;

   Eina_Inlist *shells;
   Eina_List *render_queue;
   Eina_List *post_render_queue;
   Evas *evas;
   Evas_GL *gl;
   Evas_GL_Config *glcfg;
   Evas_GL_Context *glctx;
   Evas_GL_Surface *glsfc;
   Evas_GL_API *glapi;
   Eina_List *output_resources;
   Eina_Inlist *seats;
   Eina_Bool rendering : 1;
   Eina_Bool data_device_proxy : 1;
   Eina_Bool x11_selection : 1;
   Eina_Bool rtl : 1;
   Eina_Bool aspect : 1;
   Eina_Bool minmax : 1;
} Comp;

typedef struct Comp_Data_Device_Source Comp_Data_Device_Source;

typedef struct Comp_Seat
{
   EINA_INLIST;
   Comp *c;
   Eina_Stringshare *name;
   struct wl_global *global;

   Ecore_Wl2_Input *seat;
   Ecore_Wl2_Input *client_seat;
   Ecore_Wl2_Offer *client_offer;
   uint32_t client_selection_serial;
   Eo *dev;
   Eina_List *resources;

   Eina_Hash *data_devices;
   struct
   {
      struct wl_resource *res;
      Comp_Data_Device_Source *source;
      Comp_Surface *surface;
      Comp_Surface *enter;
      uint32_t id;
      Ecore_Evas *proxy_win;
      Ecore_Window x11_owner;
      Eina_List *x11_types;
      Eina_Bool tch : 1;
   } drag;
   Comp_Data_Device_Source *selection_source;
   uint32_t selection_serial;
   Ecore_Window x11_selection_owner;

   struct wl_client *active_client;
   Comp_Surface *grab;

   struct
   {
      struct wl_array keys;
      struct
      {
         xkb_mod_mask_t depressed;
         xkb_mod_mask_t latched;
         xkb_mod_mask_t locked;
         xkb_layout_index_t group;
         Eina_Bool changed : 1;
      } mods;
      struct xkb_context *context;
      struct xkb_keymap *keymap;
      struct xkb_state *state;
      char *keymap_mem;
      int keymap_mem_size;
      int keymap_fd;
      int repeat_rate;
      int repeat_delay;
      Eina_Hash *resources;
      Comp_Surface *enter;
   } kbd;

   struct
   {
      Eina_Hash *resources;
      uint32_t button_mask;
      uint32_t enter_serial;
      Eina_Inlist *events;
      Comp_Surface *enter;
      struct
      {
         Comp_Surface *surface;
         int x, y;
      } cursor;
      struct
      {
         Evas_Object *obj;
         int layer;
         int x, y;
      } efl;
      Evas_Point pos;
      Eina_Bool in : 1;
   } ptr;

   struct
   {
      Eina_Hash *resources;
      Eina_Inlist *events;
      Comp_Surface *enter;
      Evas_Point pos;
   } tch;

   Eina_Bool pointer : 1;
   Eina_Bool keyboard : 1;
   Eina_Bool touch : 1;
   Eina_Bool focused : 1;
   Eina_Bool selection_changed : 1;
   Eina_Bool selection_exists : 1;
} Comp_Seat;

typedef struct Comp_Buffer_State
{
   Comp_Buffer *buffer;
   Eina_Tiler *opaque;
   Eina_Tiler *damages;
   Eina_Tiler *input;
   Eina_List *frames;
   Eina_Bool attach : 1;
   Eina_Bool set_opaque : 1;
   Eina_Bool set_input : 1;
} Comp_Buffer_State;

typedef struct Shell_Data
{
   EINA_INLIST;
   Comp *c;
   struct wl_resource *res;
   Eina_List *surfaces;
   Eina_Inlist *positioners;
   Eina_Bool ping : 1;
} Shell_Data;


typedef struct Shell_Positioner
{
   EINA_INLIST;
   Shell_Data *sd;
   struct wl_resource *res;
   Evas_Coord_Size size;
   Eina_Rectangle anchor_rect;
   enum zxdg_positioner_v6_anchor anchor;
   enum zxdg_positioner_v6_gravity gravity;
   enum zxdg_positioner_v6_constraint_adjustment constrain;
   Evas_Coord_Point offset;
} Shell_Positioner;

struct Comp_Surface
{
   EINA_INLIST;
   Comp *c;
   Evas_Object *obj;
   Evas_Object *clip;
   Evas_Object *img;
   Eina_Array *input_rects;
   Eina_Array *opaque_rects;
   Eina_List *proxies;
   struct wl_resource *res;
   struct wl_resource *role;
   Comp_Seat *drag; //drag surface
   Comp_Buffer *buffer[2]; // new, prev
   /* subsurface stacking order */
   Eina_List *subsurfaces;
   Eina_List *pending_subsurfaces;
   /* any child surface (xdg or subsurface */
   Eina_Inlist *children;
   Comp_Surface *parent;

   Eina_Tiler *opaque;
   Eina_Tiler *input;
   Eina_List *frames;
   Comp_Subsurface *subsurface;
   Comp_Buffer_State pending;
   struct
   {
      struct wl_resource *surface;
      Eina_Rectangle geom;
      Shell_Data *data;
      Eina_Stringshare *title;
      Eina_Stringshare *app_id;
      Shell_Positioner *positioner;
      Eina_List *grabs;
      Eina_Bool popup : 1;
      Eina_Bool new : 1;
      Eina_Bool activated : 1;
   } shell;
   Eina_Bool mapped : 1;
   Eina_Bool cursor : 1;
   Eina_Bool render_queue : 1;
   Eina_Bool post_render_queue : 1;
   Eina_Bool dead : 1;
   Eina_Bool commit : 1;
   Eina_Bool extracted : 1;
};

struct Comp_Subsurface
{
   Comp_Surface *surface;
   Comp_Buffer_State cache;
   Evas_Point offset;
   Evas_Point pending_offset;
   Eina_Bool set_offset : 1;
   Eina_Bool sync : 1;
   Eina_Bool cached : 1;
};

typedef enum Comp_Data_Device_Offer_Type
{
   COMP_DATA_DEVICE_OFFER_TYPE_DND,
   COMP_DATA_DEVICE_OFFER_TYPE_CLIPBOARD,
} Comp_Data_Device_Offer_Type;

typedef struct Comp_Data_Device_Offer
{
   Comp_Data_Device_Offer_Type type;
   struct wl_resource *res;
   Comp_Data_Device_Source *source;
   Ecore_Wl2_Offer *proxy_offer;
   uint32_t dnd_actions;
   uint32_t preferred_dnd_action;
   Eina_Bool in_ask : 1;
   Eina_Bool proxy : 1;
} Comp_Data_Device_Offer;

typedef struct Comp_Data_Device_Source
{
   struct wl_resource *res;
   Comp_Seat *seat;
   Comp_Data_Device_Offer *offer;
   Ecore_Window x11_owner;
   Eina_Inlist *transfers;
   Eina_Binbuf *reader_data;
   Ecore_Fd_Handler *reader;
   Eina_List *mime_types;
   uint32_t dnd_actions;
   uint32_t current_dnd_action;
   uint32_t compositor_action;
   Ecore_Event_Handler *proxy_send_handler;
   uint32_t proxy_serial;
   Eina_Bool actions_set : 1;
   Eina_Bool accepted : 1;
   Eina_Bool proxy : 1;
} Comp_Data_Device_Source;

typedef struct Comp_Data_Device_Transfer
{
   EINA_INLIST;
   Comp_Data_Device_Offer_Type type;
   Ecore_Fd_Handler *fdh;
   size_t offset;
   Eina_Stringshare *mime_type;
   Comp_Data_Device_Source *source;
} Comp_Data_Device_Transfer;

static Eina_List *comps;
static Eina_List *handlers;

static inline Eina_Tiler *
tiler_new(void)
{
   Eina_Tiler *t;

   t = eina_tiler_new(65535, 65535);
   eina_tiler_tile_size_set(t, 1, 1);
   return t;
}

static inline void
fdh_del(Ecore_Fd_Handler *fdh)
{
   int fd;
   if (!fdh) return;
   fd = ecore_main_fd_handler_fd_get(fdh);
   if (fd >= 0)
     close(fd);
   ecore_main_fd_handler_del(fdh);
}

#define PTR_SWAP(A, B) ptr_swap((void**)A, (void**)B)

static inline void
ptr_swap(void **a, void **b)
{
   void *c;

   c = *a;
   *a = *b;
   *b = c;
}

static inline void
array_clear(Eina_Array **arr)
{
   Eina_Array *a = *arr;

   if (!a) return;
   while (eina_array_count(a))
     evas_object_del(eina_array_pop(a));
   eina_array_free(a);
   *arr = NULL;
}

static inline Eina_Bool
client_allowed_check(Comp *c, struct wl_client *client)
{
   pid_t p;
   int32_t pid;
   Eina_Bool err;

   wl_client_get_credentials(client, &p, NULL, NULL);
   if (p == getpid()) return EINA_TRUE;
   pid = p;
   err = (!c->exes) || !eina_hash_find(c->exes, &pid);
   if (err)
     wl_client_post_no_memory(client);
   return !err;
}

static inline void
comp_data_device_source_reader_clear(Comp_Data_Device_Source *ds)
{
   fdh_del(ds->reader);
   ds->reader = NULL;
   eina_binbuf_free(ds->reader_data);
   ds->reader_data = NULL;
}

static inline void
comp_surface_reparent(Comp_Surface *cs, Comp_Surface *pcs)
{
   if (cs->parent == pcs) return;
   if (!cs->extracted)
     evas_object_smart_member_del(cs->obj);
   if (cs->parent)
     cs->parent->children = eina_inlist_remove(cs->parent->children, EINA_INLIST_GET(cs));
   if (pcs)
     {
        cs->c->surfaces = eina_inlist_remove(cs->c->surfaces, EINA_INLIST_GET(cs));
        cs->c->surfaces_count--;
        if (!cs->extracted)
          evas_object_smart_member_add(cs->obj, pcs->obj);
        pcs->children = eina_inlist_append(pcs->children, EINA_INLIST_GET(cs));
     }
   else
     {
        if (!cs->extracted)
          evas_object_smart_member_add(cs->obj, cs->c->obj);
        cs->c->surfaces = eina_inlist_append(cs->c->surfaces, EINA_INLIST_GET(cs));
        cs->c->surfaces_count++;
     }
   cs->parent = pcs;
}

static inline struct wl_resource *
data_device_find(Comp_Seat *s, struct wl_resource *resource)
{
   struct wl_client *client = wl_resource_get_client(resource);
   return eina_hash_find(s->data_devices, &client);
}

static inline void
seat_drag_end(Comp_Seat *s)
{
   s->drag.tch = 0;
   s->drag.id = 0;
   s->drag.surface = NULL;
   s->drag.res = NULL;
   s->drag.enter = NULL;
}

static inline Comp_Seat *
seat_find(Comp_Surface *cs, const Eo *dev)
{
   const Eo *seat = dev;
   Comp_Seat *s;

   if (!cs->c->parent_disp) return EINA_INLIST_CONTAINER_GET(cs->c->seats, Comp_Seat);
   if (evas_device_class_get(seat) != EVAS_DEVICE_CLASS_SEAT)
     seat = evas_device_parent_get(seat);
   EINA_INLIST_FOREACH(cs->c->seats, s)
     if (s->dev == seat) return s;
   return NULL;
}

static inline Comp_Seat *
comp_seat_find(Comp *c, const Eo *dev)
{
   const Eo *seat = dev;
   Comp_Seat *s;

   if (!c->parent_disp) return EINA_INLIST_CONTAINER_GET(c->seats, Comp_Seat);
   if (evas_device_class_get(seat) != EVAS_DEVICE_CLASS_SEAT)
     seat = evas_device_parent_get(seat);
   EINA_INLIST_FOREACH(c->seats, s)
     if (s->dev == seat) return s;
   return NULL;
}

static inline Eina_List *
seat_kbd_active_resources_get(Comp_Seat *s)
{
   Eina_List *l, *lcs, *llcs;
   Comp_Surface *cs;

   if (!s->active_client) return NULL;
   if (!s->kbd.resources) return NULL;
   l = eina_hash_find(s->kbd.resources, &s->active_client);
   if (!l) return NULL;
   lcs = eina_hash_find(s->c->client_surfaces, &s->active_client);
   if (!lcs) return NULL;
   EINA_LIST_REVERSE_FOREACH(lcs, llcs, cs)
     if (cs->role && (!cs->shell.popup)) return l;
   return NULL;
}

static inline Eina_List *
seat_ptr_resources_get(Comp_Seat *s, struct wl_client *client)
{
   return s->ptr.resources ? eina_hash_find(s->ptr.resources, &client) : NULL;
}

static inline Eina_List *
seat_tch_resources_get(Comp_Seat *s, struct wl_client *client)
{
   return s->tch.resources ? eina_hash_find(s->tch.resources, &client) : NULL;
}

static void comp_render_pre_proxied(Evas_Object *o, Evas *e, void *event_info);
static void comp_render_post_proxied(Comp_Surface *cs, Evas *e, void *event_info);
static void comp_surface_commit_image_state(Comp_Surface *cs, Comp_Buffer *buffer, Evas_Object *o);

static void
comp_surface_proxy_del(void *data, Evas *e, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Comp_Surface *cs = data;
   int i;

   cs->proxies = eina_list_remove(cs->proxies, obj);
   for (i = 0; i < 2; i++)
     {
        Comp_Buffer *buffer = cs->buffer[i];
        if (!buffer) continue;
        if (buffer->renders) buffer->renders = eina_list_remove(buffer->renders, e);
        if (buffer->post_renders) buffer->post_renders = eina_list_remove(buffer->post_renders, e);
     }
   evas_event_callback_del_full(e, EVAS_CALLBACK_RENDER_PRE, (Evas_Event_Cb)comp_render_pre_proxied, obj);
   evas_event_callback_del_full(e, EVAS_CALLBACK_RENDER_POST, (Evas_Event_Cb)comp_render_post_proxied, cs);
#ifdef HAVE_ECORE_X
   if (ecore_x_display_get())
     ecore_x_dnd_callback_pos_update_set(NULL, NULL);
#endif
}

static void
comp_surface_proxy_resize(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   int w, h;

   evas_object_geometry_get(obj, NULL, NULL, &w, &h);
   ecore_evas_resize(data, w, h);
}

static Eina_Bool
comp_surface_is_alpha(Comp_Surface *cs, Comp_Buffer *buffer)
{
   int format;
   if (buffer->shm_buffer)
     format = wl_shm_buffer_get_format(buffer->shm_buffer);
   else if (buffer->dmabuf_buffer)
     format = buffer->dmabuf_buffer->attributes.format;
   else if (cs->c->gl)
     cs->c->glapi->evasglQueryWaylandBuffer(cs->c->gl, buffer->res, EGL_TEXTURE_FORMAT, &format);
   else return EINA_FALSE; //not a real case

   switch (format)
     {
      case DRM_FORMAT_ARGB8888:
      case WL_SHM_FORMAT_ARGB8888:
      case EGL_TEXTURE_RGBA:
        return EINA_TRUE;
      default:
        break;
     }
   return EINA_FALSE;
}

static void
comp_surface_proxy_win_del(Ecore_Evas *ee)
{
   Comp_Seat *s = ecore_evas_data_get(ee, "comp_seat");

   s->drag.proxy_win = NULL;
   //fprintf(stderr, "PROXY WIN DEL\n");
}

static void
seat_drag_proxy_win_add(Comp_Seat *s)
{
   Evas_Object *o;

   if (s->drag.proxy_win) abort();
   evas_object_hide(s->drag.surface->obj);
   s->drag.proxy_win = ecore_evas_new(NULL, 0, 0, 1, 1, NULL);
   ecore_evas_data_set(s->drag.proxy_win, "comp_seat", s);
   ecore_evas_callback_destroy_set(s->drag.proxy_win, comp_surface_proxy_win_del);
   ecore_evas_alpha_set(s->drag.proxy_win, 1);
   ecore_evas_borderless_set(s->drag.proxy_win, 1);
   ecore_evas_override_set(s->drag.proxy_win, 1);
   ecore_evas_show(s->drag.proxy_win);
   o = evas_object_image_filled_add(ecore_evas_get(s->drag.proxy_win));
   evas_object_data_set(o, "comp_surface", s->drag.surface);
   evas_event_callback_add(ecore_evas_get(s->drag.proxy_win),
     EVAS_CALLBACK_RENDER_PRE, (Evas_Event_Cb)comp_render_pre_proxied, o);
   evas_event_callback_add(ecore_evas_get(s->drag.proxy_win),
     EVAS_CALLBACK_RENDER_POST, (Evas_Event_Cb)comp_render_post_proxied, s->drag.surface);
   evas_object_image_border_center_fill_set(o, EVAS_BORDER_FILL_SOLID);
   evas_object_image_colorspace_set(o, EVAS_COLORSPACE_ARGB8888);
   if (!s->drag.surface->render_queue)
     {
        comp_surface_commit_image_state(s->drag.surface, s->drag.surface->buffer[1], o);
        evas_object_image_alpha_set(o,
          comp_surface_is_alpha(s->drag.surface, s->drag.surface->buffer[1]));
        evas_object_image_data_update_add(o, 0, 0, 9999, 9999);
     }
   evas_object_show(o);
   evas_object_event_callback_add(o, EVAS_CALLBACK_DEL,
     comp_surface_proxy_del, s->drag.surface);
   evas_object_event_callback_add(o, EVAS_CALLBACK_RESIZE,
     comp_surface_proxy_resize, s->drag.proxy_win);
   evas_object_resize(o,
     s->drag.surface->buffer[!s->drag.surface->render_queue]->w,
     s->drag.surface->buffer[!s->drag.surface->render_queue]->h);
   s->drag.surface->proxies = eina_list_append(s->drag.surface->proxies, o);
}

static void comp_surface_send_data_device_leave(Comp_Surface *cs, Comp_Seat *s);
static void comp_surface_send_data_device_enter(Comp_Surface *cs, Comp_Seat *s);

static void
dnd_motion(Comp_Seat *s, int ex, int ey)
{
   struct wl_resource *res;
   Eina_List *l, *lcs;
   Comp_Surface *cs;

   s->ptr.pos.x = ex;
   s->ptr.pos.y = ey;
   if (!s->active_client) return;
   l = eina_hash_find(s->c->client_surfaces, &s->active_client);
   EINA_LIST_REVERSE_FOREACH(l, lcs, cs)
     {
        int x, y, w, h;
        if ((!cs->mapped) || (!cs->role)) continue;
        evas_object_geometry_get(cs->obj, &x, &y, &w, &h);
        if (!COORDS_INSIDE(ex, ey, x, y, w, h)) continue;
        if (s->drag.enter != cs)
          {
             if (s->drag.enter)
               comp_surface_send_data_device_leave(s->drag.enter, s);
             s->drag.enter = cs;
             if (s->drag.source)
               comp_surface_send_data_device_enter(cs, s);
          }
        if (!s->drag.source) break;
        res = eina_hash_find(s->data_devices, &s->active_client);
        wl_data_device_send_motion(res,
          (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff),
          wl_fixed_from_int(ex - x), wl_fixed_from_int(ey - y));
        break;
     }
}
#ifdef HAVE_ECORE_X
static void x11_send_send(Comp_Data_Device_Source *source, const char* mime_type, int32_t fd, Comp_Data_Device_Offer_Type type);
#endif
#include "copiedfromweston.x"
#ifdef HAVE_ECORE_X
# include "x11.x"
#endif

static void
resource_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
{
   wl_resource_destroy(resource);
}

#define CONTAINS(x, y, w, h, xx, yy, ww, hh) \
  (((xx) >= (x)) && (((x) + (w)) >= ((xx) + (ww))) && ((yy) >= (y)) && (((y) + (h)) >= ((yy) + (hh))))
#define CONSTRAINED(W, H, X, Y) \
   !CONTAINS(zx, zy, zw, zh, (X), (Y), (W), (H))

static int
_apply_positioner_x(int x, Shell_Positioner *sp, Eina_Bool invert)
{
   enum zxdg_positioner_v6_anchor an = ZXDG_POSITIONER_V6_ANCHOR_NONE;
   enum zxdg_positioner_v6_gravity grav = ZXDG_POSITIONER_V6_GRAVITY_NONE;

   if (invert)
     {
        if (sp->anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT)
          an |= ZXDG_POSITIONER_V6_ANCHOR_RIGHT;
        else if (sp->anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT)
          an |= ZXDG_POSITIONER_V6_ANCHOR_LEFT;
        if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT)
          grav |= ZXDG_POSITIONER_V6_GRAVITY_RIGHT;
        else if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT)
          grav |= ZXDG_POSITIONER_V6_GRAVITY_LEFT;
     }
   else
     {
        an = sp->anchor;
        grav = sp->gravity;
     }

   /* left edge */
   if (an & ZXDG_POSITIONER_V6_ANCHOR_LEFT)
     x += sp->anchor_rect.x;
   /* right edge */
   else if (an & ZXDG_POSITIONER_V6_ANCHOR_RIGHT)
     x += sp->anchor_rect.x + sp->anchor_rect.w;
   /* center */
   else
     x += sp->anchor_rect.x + (sp->anchor_rect.w / 2);

   /* flip left over anchor */
   if (grav & ZXDG_POSITIONER_V6_GRAVITY_LEFT)
     x -= sp->size.w;
   /* center on anchor */
   else if (!(grav & ZXDG_POSITIONER_V6_GRAVITY_RIGHT))
     x -= sp->size.w / 2;
   return x;
}

static int
_apply_positioner_y(int y, Shell_Positioner *sp, Eina_Bool invert)
{
   enum zxdg_positioner_v6_anchor an = ZXDG_POSITIONER_V6_ANCHOR_NONE;
   enum zxdg_positioner_v6_gravity grav = ZXDG_POSITIONER_V6_GRAVITY_NONE;

   if (invert)
     {
        if (sp->anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP)
          an |= ZXDG_POSITIONER_V6_ANCHOR_BOTTOM;
        else if (sp->anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)
          an |= ZXDG_POSITIONER_V6_ANCHOR_TOP;
        if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP)
          grav |= ZXDG_POSITIONER_V6_GRAVITY_BOTTOM;
        else if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)
          grav |= ZXDG_POSITIONER_V6_GRAVITY_TOP;
     }
   else
     {
        an = sp->anchor;
        grav = sp->gravity;
     }

   /* up edge */
   if (an & ZXDG_POSITIONER_V6_ANCHOR_TOP)
     y += sp->anchor_rect.y;
   /* bottom edge */
   else if (an & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)
     y += sp->anchor_rect.y + sp->anchor_rect.h;
   /* center */
   else
     y += sp->anchor_rect.y + (sp->anchor_rect.h / 2);

   /* flip up over anchor */
   if (grav & ZXDG_POSITIONER_V6_GRAVITY_TOP)
     y -= sp->size.h;
   /* center on anchor */
   else if (!(grav & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM))
     y -= sp->size.h / 2;
   return y;
}

static Eina_Bool
_apply_positioner_slide(Comp_Surface *cs, Shell_Positioner *sp, int *x, int *y, int *w, int *h, int zx, int zy, int zw, int zh)
{
   int px, py, cx, cy;

   evas_object_geometry_get(cs->parent->obj, &px, &py, NULL, NULL);
   evas_object_geometry_get(cs->c->obj, &cx, &cy, NULL, NULL);
   px -= cx, py -= cy;
   if ((sp->constrain & ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_SLIDE_X) &&
       (!CONTAINS(zx, zy, zw, zh, *x, zy, *w, 1)))
     {
        int sx = *x;

        if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT)
          {
             if (*x + *w > zx + zw)
               sx = MAX(zx + zw - *w, px + sp->anchor_rect.x - *w);
             else if (*x < zx)
               sx = MIN(zx, px + sp->anchor_rect.x + sp->anchor_rect.w);
          }
        else if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT)
          {
             if (*x < zx)
               sx = MIN(zx, *x + sp->anchor_rect.x + sp->anchor_rect.w);
             else if (*x + *w > zx + zw)
               sx = MAX(zx + zw - *w, px + sp->anchor_rect.x - *w);
          }
        if (CONTAINS(zx, zy, zw, zh, sx, zy, *w, 1))
          *x = sx;
     }
   if (!CONSTRAINED(*w, *h, *x, *y)) return EINA_TRUE;
   if ((sp->constrain & ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_SLIDE_Y) &&
       (!CONTAINS(zx, zy, zw, zh, zx, *y, 1, *h)))
     {
        int sy = *y;

        if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP)
          {
             if (*y + *h > zy + zh)
               sy = MAX(zy + zh - *h, py + sp->anchor_rect.y - *h);
             else if (*y < zy)
               sy = MIN(zy, py + sp->anchor_rect.y + sp->anchor_rect.h);
          }
        else if (sp->gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)
          {
             if (*y < zy)
               sy = MIN(zy, py + sp->anchor_rect.y + sp->anchor_rect.h);
             else if (*y + *h > zy + zh)
               sy = MAX(zy + zh - *h, py + sp->anchor_rect.y - *h);
          }
        if (CONTAINS(zx, zy, zw, zh, zx, sy, 1, *h))
          *y = sy;
     }
   return !CONSTRAINED(*w, *h, *x, *y);
}

static void
_apply_positioner(Comp_Surface *cs, Shell_Positioner *sp)
{
   int ix, iy, x, y, w = -1, h = -1, px, py, cx, cy;
   int zx, zy, zw, zh;
   /* apply base geometry:
    * coords are relative to parent
    */
   evas_object_geometry_get(cs->parent->obj, &px, &py, NULL, NULL);
   evas_object_geometry_get(cs->c->obj, &cx, &cy, NULL, NULL);
   px -= cx, py -= cy;
   ix = x = px + sp->offset.x;
   iy = y = py + sp->offset.y;

   if (sp->size.w && sp->size.h)
     {
        w = sp->size.w;
        h = sp->size.h;
     }

   /* apply policies in order:
    - anchor (add anchor_rect using anchor point)
    - gravity (perform flips if gravity is not right|bottom)
    - constrain (adjust if popup does not fit)
    */
   x = _apply_positioner_x(x, sp, 0);
   y = _apply_positioner_y(y, sp, 0);

   evas_object_geometry_get(cs->c->obj, &zx, &zy, &zw, &zh);

   if (!CONSTRAINED(w, h, x, y))
     {
        evas_object_move(cs->obj, zx + x, zy + y);
        if (w > 0) evas_object_resize(cs->obj, w, h);
        return;
     }

   /* assume smart placement:
    - flip
    - slide
    - resize
    */
   if ((sp->constrain & ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_FLIP_X) &&
       (!CONTAINS(zx, zy, zw, zh, x, zy, w, 1)))
     {
        int fx;

        fx = _apply_positioner_x(ix, sp, 1);
        if (CONTAINS(zx, zy, zw, zh, fx, zy, w, 1))
          x = fx;
     }
   if (!CONSTRAINED(w, h, x, y))
     {
        evas_object_move(cs->obj, zx + x, zy + y);
        if (w > 0) evas_object_resize(cs->obj, w, h);
        return;
     }
   if ((sp->constrain & ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_FLIP_Y) &&
       (!CONTAINS(zx, zy, zw, zh, zx, y, 1, h)))
     {
        int fy;

        fy = _apply_positioner_y(iy, sp, 1);
        if (CONTAINS(zx, zy, zw, zh, zx, fy, 1, h))
          y = fy;
     }
   if (!CONSTRAINED(w, h, x, y))
     {
        evas_object_move(cs->obj, zx + x, zy + y);
        if (w > 0) evas_object_resize(cs->obj, w, h);
        return;
     }
   if (_apply_positioner_slide(cs, sp, &x, &y, &w, &h, zx, zy, zw, zh))
     {
        evas_object_move(cs->obj, zx + x, zy + y);
        if (w > 0) evas_object_resize(cs->obj, w, h);
        return;
     }

   if (!CONSTRAINED(w, h, x, y))
     {
        evas_object_move(cs->obj, zx + x, zy + y);
        if (w > 0) evas_object_resize(cs->obj, w, h);
        return;
     }

   if ((sp->constrain & ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_RESIZE_X) &&
       (!CONTAINS(zx, zy, zw, zh, x, zy, w, 1)))
     {
        w = zx + zw - x;
        if (!CONSTRAINED(w, h, x, y))
          {
             evas_object_move(cs->obj, zx + x, zy + y);
             if (w > 0) evas_object_resize(cs->obj, w, h);
             return;
          }
     }
   if ((sp->constrain & ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_RESIZE_Y) &&
       (!CONTAINS(zx, zy, zw, zh, zx, y, 1, h)))
     {
        h = zy + zh - y;
     }
   evas_object_move(cs->obj, zx + x, zy + y);
   if (w > 0) evas_object_resize(cs->obj, w, h);
}

static const struct wl_data_offer_interface data_device_offer_interface =
{
   data_device_offer_accept,
   data_device_offer_receive,
   resource_destroy,
   data_device_offer_finish,
   data_device_offer_set_actions,
};

static void
data_device_offer_create(Comp_Data_Device_Source *source, struct wl_resource *resource)
{
   Comp_Data_Device_Offer *off;
   Eina_List *l;
   Eina_Stringshare *type;

   off = calloc(1, sizeof(Comp_Data_Device_Offer));
   off->res = wl_resource_create(wl_resource_get_client(resource), &wl_data_offer_interface,
     wl_resource_get_version(resource), 0);
   wl_resource_set_implementation(off->res, &data_device_offer_interface, off, data_device_offer_impl_destroy);
   off->source = source;
   source->offer = off;
   off->proxy = source->proxy;
   wl_data_device_send_data_offer(resource, off->res);
   EINA_LIST_FOREACH(source->mime_types, l, type)
     wl_data_offer_send_offer(off->res, type);
}

static void
comp_buffer_state_alloc(Comp_Buffer_State *cbs)
{
   cbs->damages = tiler_new();
   cbs->opaque = tiler_new();
   cbs->input = tiler_new();
}

static void
comp_buffer_state_clear(Comp_Buffer_State *cbs)
{
   eina_tiler_free(cbs->damages);
   eina_tiler_free(cbs->opaque);
   eina_tiler_free(cbs->input);
   while (cbs->frames)
     wl_resource_destroy(eina_list_data_get(cbs->frames));
}

static void
comp_seat_send_modifiers(Comp_Seat *s, struct wl_resource *res, uint32_t serial)
{
   wl_keyboard_send_modifiers(res, serial, s->kbd.mods.depressed,
                              s->kbd.mods.latched,
                              s->kbd.mods.locked,
                              s->kbd.mods.group);
   s->kbd.mods.changed = 1;
}

static Eina_Bool
data_device_selection_read(void *d, Ecore_Fd_Handler *fdh)
{
   Comp_Data_Device_Source *ds = d;
   int len = -1;

   do
     {
        unsigned char buf[2048];
        int fd;

        fd = ecore_main_fd_handler_fd_get(fdh);
        if (fd < 0) break;
        len = read(fd, buf, sizeof(buf));
        if (len > 0)
          {
             if (!ds->reader_data)
               ds->reader_data = eina_binbuf_new();
             eina_binbuf_append_length(ds->reader_data, buf, len);
             return ECORE_CALLBACK_RENEW;
          }
     } while (0);
   fdh_del(fdh);
   ds->reader = NULL;
   if (len < 0)
     {
        eina_binbuf_free(ds->reader_data);
        ds->reader_data = NULL;
     }
   return ECORE_CALLBACK_RENEW;
}

static void
comp_seat_kbd_data_device_enter(Comp_Seat *s)
{
   struct wl_resource *res;
   Comp_Data_Device_Source *ds = s->selection_source;
   int fd[2];

   if (!s->kbd.enter) return;
   res = data_device_find(s, s->kbd.enter->res);
   if (!res) return;
   if (ds)
     {
        data_device_offer_create(ds, res);
        ds->offer->type = COMP_DATA_DEVICE_OFFER_TYPE_CLIPBOARD;
     }
   if (ds)
     wl_data_device_send_selection(res, ds->offer ? ds->offer->res : NULL);
   if ((!ds) || (!ds->mime_types)) return;
   EINA_SAFETY_ON_TRUE_RETURN(pipe2(fd, O_CLOEXEC) < 0);
   wl_data_source_send_send(ds->res, eina_list_data_get(ds->mime_types), fd[1]);
   close(fd[1]);
   ds->reader = ecore_main_fd_handler_file_add(fd[0], ECORE_FD_READ | ECORE_FD_ERROR,
     data_device_selection_read, ds, NULL, NULL);
}

static void
comp_seats_redo_enter(Comp *c, Comp_Surface *cs)
{
   Comp_Seat *s;
   uint32_t serial;
   struct wl_client *client = NULL;

   serial = wl_display_next_serial(c->display);
   if (cs)
     client = wl_resource_get_client(cs->res);
   EINA_INLIST_FOREACH(c->seats, s)
     {
        Eina_List *l, *ll;
        struct wl_resource *res;
        if (c->active_surface && (cs != c->active_surface))
          {
             l = seat_kbd_active_resources_get(s);
             EINA_LIST_FOREACH(l, ll, res)
               wl_keyboard_send_leave(res, serial, c->active_surface->res);
          }
        s->active_client = client;
        if (cs)
          {
             l = seat_kbd_active_resources_get(s);
             EINA_LIST_FOREACH(l, ll, res)
               {
                  wl_keyboard_send_enter(res, serial, cs->res, &s->kbd.keys);
                  comp_seat_send_modifiers(s, res, serial);
               }
          }
        s->kbd.enter = cs;
        if (s->kbd.enter && s->selection_source)
          comp_seat_kbd_data_device_enter(s);
     }
   c->active_surface = cs;
}

static void shell_surface_send_configure(Comp_Surface *cs);

static void
shell_surface_deactivate_recurse(Comp_Surface *cs)
{
   Comp_Surface *ccs;

   EINA_INLIST_FOREACH(cs->children, ccs)
     {
        if (!ccs->shell.popup) continue;
        shell_surface_deactivate_recurse(ccs);
        if (ccs->shell.grabs) evas_object_hide(ccs->obj);
     }
}

static void
shell_surface_activate_recurse(Comp_Surface *cs)
{
   Comp_Surface *lcs, *parent = cs->parent;
   Eina_List *l, *parents = NULL;
   Eina_Inlist *i;

   if (parent)
     {
        /* apply focus to toplevel in case where focus is reverted */
        while (parent)
          {
             parents = eina_list_append(parents, parent);
             if (!parent->shell.popup) break;
             parent = parent->parent;
          }
     }
   EINA_INLIST_FOREACH_SAFE(cs->c->surfaces, i, lcs)
     if (lcs->shell.activated && (lcs != cs))
       {
          if ((!parents) || (!eina_list_data_find(parents, lcs)))
            {
               lcs->shell.activated = 0;
               shell_surface_send_configure(lcs);
               cs->c->surfaces = eina_inlist_promote(cs->c->surfaces, EINA_INLIST_GET(lcs));
            }
       }
   /* last item is the toplevel */
   EINA_LIST_REVERSE_FOREACH(parents, l, lcs)
     {
        if (lcs->shell.activated) continue;
        lcs->shell.activated = 1;
        if (!lcs->shell.popup)
          shell_surface_send_configure(lcs);
     }
   eina_list_free(parents);
}

static void
shell_surface_minmax_update(Comp_Surface *cs)
{
   int w, h;

   if (!cs) return;
   if (!cs->c->minmax) return;
   if (cs->extracted) return;
   evas_object_size_hint_min_get(cs->obj, &w, &h);
   evas_object_size_hint_min_set(cs->c->obj, w, h);
   evas_object_size_hint_max_get(cs->obj, &w, &h);
   evas_object_size_hint_max_set(cs->c->obj, w, h);
}

static void
shell_surface_aspect_update(Comp_Surface *cs)
{
   Evas_Aspect_Control aspect;
   int w, h;

   if (!cs) return;
   if (!cs->c->aspect) return;
   if (cs->extracted) return;
   evas_object_size_hint_aspect_get(cs->obj, &aspect, &w, &h);
   evas_object_size_hint_aspect_set(cs->c->obj, aspect, w, h);
}

static void
shell_surface_send_configure(Comp_Surface *cs)
{
   uint32_t serial, *s;
   struct wl_array states;
   int w, h;

   cs->shell.new = 0;
   if (cs->shell.popup)
     {
        int x, y, px, py;
        evas_object_geometry_get(cs->obj, &x, &y, &w, &h);
        evas_object_geometry_get(cs->parent->obj, &px, &py, NULL, NULL);
        serial = wl_display_next_serial(cs->c->display);
        zxdg_popup_v6_send_configure(cs->role, x - px, y - py, w, h);
        zxdg_surface_v6_send_configure(cs->shell.surface, serial);
        return;
     }
   wl_array_init(&states);
   s = wl_array_add(&states, sizeof(uint32_t));
   *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN;
   if (cs->shell.activated)
     {
        s = wl_array_add(&states, sizeof(uint32_t));
        *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED;
        if (!cs->extracted)
          evas_object_raise(cs->obj);
        if (cs->parent)
          cs->parent->children = eina_inlist_demote(cs->parent->children, EINA_INLIST_GET(cs));
        else
          cs->c->surfaces = eina_inlist_demote(cs->c->surfaces, EINA_INLIST_GET(cs));
        shell_surface_activate_recurse(cs);
     }
   serial = wl_display_next_serial(cs->c->display);
   if (cs->extracted)
     evas_object_geometry_get(cs->obj, NULL, NULL, &w, &h);
   else
     evas_object_geometry_get(cs->c->clip, NULL, NULL, &w, &h);
   zxdg_toplevel_v6_send_configure(cs->role, w, h, &states);
   zxdg_surface_v6_send_configure(cs->shell.surface, serial);
   wl_array_release(&states);
   if (cs->shell.activated)
     {
        Comp_Surface *ccs;

        comp_seats_redo_enter(cs->c, cs);
        EINA_INLIST_FOREACH(cs->children, ccs)
          if (ccs->shell.surface && ccs->role && ccs->shell.popup)
            ccs->shell.activated = cs->shell.activated;
        shell_surface_aspect_update(cs);
        shell_surface_minmax_update(cs);
     }
   else
     shell_surface_deactivate_recurse(cs);
}

static void
shell_surface_init(Comp_Surface *cs)
{
   /* update activate status: newest window is always activated */
   Comp_Surface *parent = cs->parent;

   if (cs->c->active_surface && parent && (!parent->shell.activated))
     {
        /* child windows/popups cannot steal focus from toplevel */
        shell_surface_send_configure(cs);
        return;
     }

   cs->shell.activated = 1;
   shell_surface_send_configure(cs);
}

static void
comp_surface_output_enter(Comp_Surface *cs)
{
   Eina_List *l;
   struct wl_resource *res;

   EINA_LIST_FOREACH(cs->c->output_resources, l, res)
     if (wl_resource_get_client(res) == wl_resource_get_client(cs->res))
       wl_surface_send_enter(cs->res, res);
}

static void
comp_surface_buffer_detach(Comp_Buffer **pbuffer)
{
   Comp_Buffer *buffer;

   buffer = *pbuffer;
   if (!buffer) return;
   if (buffer->post_renders)
     fprintf(stderr, "CRASH %u\n", wl_resource_get_id(buffer->res));
   eina_list_free(buffer->renders);
   wl_list_remove(&buffer->destroy_listener.link);
   //if (buffer->dbg) fprintf(stderr, "BUFFER(%d) RELEASE\n", wl_resource_get_id(buffer->res));
   if (buffer->pool) wl_shm_pool_unref(buffer->pool);
   wl_resource_queue_event(buffer->res, WL_BUFFER_RELEASE);
   free(buffer);
   *pbuffer = NULL;
}

static void
comp_surface_buffer_post_render(Comp_Surface *cs)
{
   double t = ecore_loop_time_get() - cs->c->wayland_time_base;

   //if (cs->subsurface)
     //fprintf(stderr, "FRAME(%d)\n", wl_resource_get_id(cs->res));
   while (cs->frames)
     {
        struct wl_resource *frame = eina_list_data_get(cs->frames);

        wl_callback_send_done(frame, t * 1000);
        wl_resource_destroy(frame);
     }
}

static void
comp_surface_pixels_get(void *data, Evas_Object *obj)
{
   Comp_Surface *cs = data;
   Comp_Buffer *buffer;

   evas_object_image_pixels_dirty_set(obj, 0);
   evas_object_image_pixels_get_callback_set(obj, NULL, NULL);
   buffer = cs->buffer[!cs->render_queue];
   //if (cs->cursor || (buffer->w == 32)) fprintf(stderr, "RENDER(%d) %dx%d\n", wl_resource_get_id(buffer->res), buffer->w, buffer->h);
   evas_object_image_size_set(obj, buffer->w, buffer->h);
   evas_object_image_data_set(obj, wl_shm_buffer_get_data(buffer->shm_buffer));
}

static void
comp_surface_commit_image_state(Comp_Surface *cs, Comp_Buffer *buffer, Evas_Object *o)
{
   if ((!buffer->renders) || (!eina_list_data_find(buffer->renders, evas_object_evas_get(o))))
     buffer->renders = eina_list_append(buffer->renders, evas_object_evas_get(o));
   evas_object_image_pixels_dirty_set(o, 1);

   if (buffer->shm_buffer)
     {
        //if (cs->subsurface)
          //fprintf(stderr, "SET CB\n");
        evas_object_image_pixels_get_callback_set(o, comp_surface_pixels_get, cs);
        buffer->pool = wl_shm_buffer_ref_pool(buffer->shm_buffer);
     }
   else
     {
        Evas_Native_Surface ns;

        evas_object_image_pixels_get_callback_set(o, NULL, NULL);
        if (buffer->dmabuf_buffer)
          {
             ns.type = EVAS_NATIVE_SURFACE_WL_DMABUF;
             ns.version = EVAS_NATIVE_SURFACE_VERSION;

             ns.data.wl_dmabuf.attr = &buffer->dmabuf_buffer->attributes;
             ns.data.wl_dmabuf.resource = buffer->res;
          }
        else
          {
             ns.type = EVAS_NATIVE_SURFACE_WL;
             ns.version = EVAS_NATIVE_SURFACE_VERSION;
             ns.data.wl.legacy_buffer = buffer->res;
          }
        evas_object_image_native_surface_set(o, &ns);
     }
}

static void
comp_surface_commit_state(Comp_Surface *cs, Comp_Buffer_State *state)
{
   int x, y;
   Eina_List *l;
   Evas_Object *o;
   Comp_Buffer *buffer = NULL;

   if (state->attach)
     {
        comp_surface_buffer_detach(&cs->buffer[0]);
        buffer = cs->buffer[0] = state->buffer;

        if (buffer)
          {
             //if (cs->subsurface)
               //fprintf(stderr, "BUFFER(%d) COMMIT %d\n", wl_resource_get_id(buffer->res), wl_resource_get_id(cs->res));
             if ((!cs->c->rendering) && (!cs->post_render_queue) &&
               ((!cs->buffer[1]) || (!cs->buffer[1]->post_renders)))
               comp_surface_buffer_detach(&cs->buffer[1]);
          }
        else
          {
             evas_object_hide(cs->obj);
             EINA_LIST_FOREACH(cs->proxies, l, o)
               evas_object_hide(o);
          }
     }
   evas_object_geometry_get(cs->obj, &x, &y, NULL, NULL);

   if (buffer && (!cs->mapped))
     {
        if (cs->role)
          evas_object_show(cs->obj);
     }

   if (state->attach && state->buffer)
     {
        evas_object_move(cs->img, x + buffer->x, y + buffer->y);
        evas_object_resize(cs->obj, buffer->w, buffer->h);
        if (cs->shell.popup)
          {
             evas_object_size_hint_min_set(cs->obj, buffer->w, buffer->h);
             evas_object_size_hint_max_set(cs->obj, buffer->w, buffer->h);
          }
     }
   else if (cs->shell.new)
     shell_surface_init(cs);

   state->attach = 0;
   state->buffer = NULL;

   cs->frames = eina_list_merge(cs->frames, state->frames);
   state->frames = NULL;

   if (eina_tiler_empty(state->damages))
     {
        comp_surface_buffer_detach(&buffer);
        comp_surface_buffer_post_render(cs);
        if (!cs->post_render_queue)
          {
             evas_object_image_pixels_dirty_set(cs->img, 0);
             EINA_LIST_FOREACH(cs->proxies, l, o)
               evas_object_image_pixels_dirty_set(o, 0);
          }
     }
   else if (buffer)
     {
        Eina_Iterator *it;
        Eina_Rectangle *rect;

        comp_surface_commit_image_state(cs, buffer, cs->img);
        EINA_LIST_FOREACH(cs->proxies, l, o)
          comp_surface_commit_image_state(cs, buffer, o);

        it = eina_tiler_iterator_new(state->damages);
        EINA_ITERATOR_FOREACH(it, rect)
          {
               //if (cs->subsurface) fprintf(stderr, "BUFFER(%d) DAMAGE %d\n", wl_resource_get_id(buffer->res), wl_resource_get_id(cs->res));
             evas_object_image_data_update_add(cs->img, rect->x, rect->y, rect->w, rect->h);
             EINA_LIST_FOREACH(cs->proxies, l, o)
               evas_object_image_data_update_add(o, rect->x, rect->y, rect->w, rect->h);
          }
        eina_iterator_free(it);
        if (!cs->render_queue)
          cs->c->render_queue = eina_list_append(cs->c->render_queue, cs);
        cs->render_queue = 1;
     }
   eina_tiler_clear(state->damages);

   if (state->set_opaque && (!eina_tiler_equal(cs->opaque, state->opaque)))
     {
        array_clear(&cs->opaque_rects);
        if (eina_tiler_empty(state->opaque))
          {
             evas_object_image_border_set(cs->img, 0, 0, 0, 0);
             EINA_LIST_FOREACH(cs->proxies, l, o)
               evas_object_image_border_set(o, 0, 0, 0, 0);
          }
        else /* FIXME: proxied opaque regions */
          {
             Eina_Iterator *it;
             Eina_Rectangle *rect;
             Evas_Object *r;

             it = eina_tiler_iterator_new(state->opaque);
             cs->opaque_rects  = eina_array_new(1);
             EINA_ITERATOR_FOREACH(it, rect)
               {
                  r = evas_object_rectangle_add(cs->c->evas);
                  evas_object_name_set(r, "opaque_rect");
                  evas_object_pass_events_set(r, 1);
                  evas_object_color_set(r, 0, 0, 0, 255);
                  evas_object_smart_member_add(r, cs->obj);
                  evas_object_geometry_set(r, x + rect->x, y + rect->y, rect->w, rect->h);
                  evas_object_stack_below(r, cs->img);
                  evas_object_show(r);
                  eina_array_push(cs->opaque_rects, r);
               }
             /* FIXME: maybe use image border here */

             eina_iterator_free(it);
          }
        PTR_SWAP(&cs->opaque, &state->opaque);
     }
   eina_tiler_clear(state->opaque);
   state->set_opaque = 0;

   if (state->set_input)
     {
        if (eina_tiler_empty(state->input))
          {
             array_clear(&cs->input_rects);
             evas_object_pass_events_set(cs->img, 0);
             evas_object_pointer_mode_set(cs->img, EVAS_OBJECT_POINTER_MODE_NOGRAB);
          }
        else if (!eina_tiler_equal(cs->input, state->input))
          {
             Eina_Iterator *it;
             Eina_Rectangle *rect;
             Evas_Object *r;

             array_clear(&cs->input_rects);
             it = eina_tiler_iterator_new(state->input);
             cs->input_rects  = eina_array_new(1);
             EINA_ITERATOR_FOREACH(it, rect)
               {
                  r = evas_object_rectangle_add(cs->c->evas);
                  evas_object_name_set(r, "input_rect");
                  evas_object_color_set(r, 0, 0, 0, 0);
                  evas_object_smart_member_add(r, cs->obj);
                  evas_object_geometry_set(r, x + rect->x, y + rect->y, rect->w, rect->h);
                  evas_object_stack_above(r, cs->img);
                  evas_object_pointer_mode_set(r, EVAS_OBJECT_POINTER_MODE_NOGRAB);
                  evas_object_show(r);
                  evas_object_show(r);
                  eina_array_push(cs->input_rects, r);
               }
             evas_object_pass_events_set(cs->img, 1);
             eina_iterator_free(it);
          }
        PTR_SWAP(&cs->input, &state->input);
     }
   eina_tiler_clear(state->input);
   state->set_input = 0;

   if (cs->pending_subsurfaces)
     {
        cs->subsurfaces = eina_list_free(cs->subsurfaces);
        PTR_SWAP(&cs->subsurfaces, &cs->pending_subsurfaces);
     }
   if (cs->subsurface)
     {
        if (cs->subsurface->set_offset)
          {
             cs->subsurface->offset.x = cs->subsurface->pending_offset.x;
             cs->subsurface->offset.y = cs->subsurface->pending_offset.y;
             cs->subsurface->pending_offset.x = cs->subsurface->pending_offset.y = 0;
             cs->subsurface->set_offset = 0;
             evas_object_geometry_get(cs->parent->obj, &x, &y, NULL, NULL);
             evas_object_move(cs->obj, x + cs->subsurface->offset.x, y + cs->subsurface->offset.y);
          }
     }
}

static void
comp_surface_destroy(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   if (cs)
     {
        if (cs->post_render_queue)
          {
             cs->dead = 1;
             evas_object_pass_events_set(cs->obj, 1);
             return;
          }
        cs->res = NULL;
     }
   wl_resource_destroy(resource);
}

static void
comp_surface_buffer_destroy(struct wl_listener *listener, void *data EINA_UNUSED)
{
   Comp_Buffer *buffer;
   Comp_Surface *cs;

   buffer = container_of(listener, Comp_Buffer, destroy_listener);
   cs = buffer->cs;
   if (cs)
     {
        if (cs->buffer[0] == buffer) cs->buffer[0] = NULL;
        else if (cs->buffer[1] == buffer) cs->buffer[1] = NULL;
        else if (cs->pending.buffer == buffer) cs->pending.buffer = NULL;
        else if (cs->subsurface)
          {
             if (cs->subsurface->cache.buffer == buffer) cs->subsurface->cache.buffer = NULL;
          }
     }
   free(buffer);
}

static void
comp_surface_attach(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *buffer_resource, int32_t x, int32_t y)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   Comp_Buffer *buffer;
   struct wl_shm_buffer *shmbuff;
   struct linux_dmabuf_buffer *dmabuf;

   comp_surface_buffer_detach(&cs->pending.buffer);
   cs->pending.attach = 1;
   if (!buffer_resource) return;

   buffer = calloc(1, sizeof(Comp_Buffer));
   if (cs->subsurface)
     {
        //fprintf(stderr, "BUFFER(%d) HELD BY %d\n", wl_resource_get_id(buffer_resource), wl_resource_get_id(resource));
        buffer->dbg = 1;
     }
   buffer->cs = cs;
   buffer->x = x;
   buffer->y = y;
   shmbuff = wl_shm_buffer_get(buffer_resource);
   dmabuf = linux_dmabuf_buffer_get(buffer_resource);
   if (shmbuff)
     {
        buffer->w = wl_shm_buffer_get_width(shmbuff);
        buffer->h = wl_shm_buffer_get_height(shmbuff);
     }
   else if (dmabuf)
     {
        buffer->w = dmabuf->attributes.width;
        buffer->h = dmabuf->attributes.height;
     }
   else if (cs->c->gl)
     {
        cs->c->glapi->evasglQueryWaylandBuffer(cs->c->gl, buffer_resource, EGL_WIDTH, &buffer->w);
        cs->c->glapi->evasglQueryWaylandBuffer(cs->c->gl, buffer_resource, EGL_HEIGHT, &buffer->h);
     }
   buffer->shm_buffer = shmbuff;
   buffer->dmabuf_buffer = dmabuf;

   buffer->res = buffer_resource;
   buffer->destroy_listener.notify = comp_surface_buffer_destroy;
   wl_resource_add_destroy_listener(buffer_resource, &buffer->destroy_listener);
   cs->pending.buffer = buffer;
}

static void
comp_surface_damage_buffer(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y, int32_t w, int32_t h)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   eina_tiler_rect_add(cs->pending.damages, &(Eina_Rectangle){x, y, w, h});
}

/*
 * Currently damage and damage_buffer are the same because we don't support
 * buffer_scale, transform, or viewport.  Once we support those we'll have
 * to make surface_cb_damage handle damage in surface co-ordinates.
 */
static void
comp_surface_damage(struct wl_client *client, struct wl_resource *resource, int32_t x, int32_t y, int32_t w, int32_t h)
{
   comp_surface_damage_buffer(client, resource, x, y, w, h);
}

static void
comp_surface_frame_impl_destroy(struct wl_resource *resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   if (!cs) return;
   if (cs->frames)
     cs->frames = eina_list_remove(cs->frames, resource);
   if (cs->pending.frames)
     cs->pending.frames = eina_list_remove(cs->pending.frames, resource);

   if (cs->subsurface && cs->subsurface->cached)
     cs->subsurface->cache.frames = eina_list_remove(cs->subsurface->cache.frames, resource);
}

static void
comp_surface_frame(struct wl_client *client, struct wl_resource *resource, uint32_t callback)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   struct wl_resource *res;

   res = wl_resource_create(client, &wl_callback_interface, 1, callback);
   wl_resource_set_implementation(res, NULL, cs, comp_surface_frame_impl_destroy);
   cs->pending.frames = eina_list_append(cs->pending.frames, res);
}

static void
comp_surface_set_opaque_region(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *region_resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   cs->pending.set_opaque = 1;
   eina_tiler_clear(cs->pending.opaque);
   if (region_resource)
     eina_tiler_union(cs->pending.opaque, wl_resource_get_user_data(region_resource));
}

static void
comp_surface_set_input_region(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *region_resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   cs->pending.set_input = 1;
   eina_tiler_clear(cs->pending.input);
   if (region_resource)
     eina_tiler_union(cs->pending.input, wl_resource_get_user_data(region_resource));
}

static void
subcomp_subsurface_cache_merge(Comp_Subsurface *css)
{
   //fprintf(stderr, "CACHE MERGE\n");
   css->cached = 1;
   if (css->cache.frames || css->surface->pending.frames)
     css->cache.frames = eina_list_merge(css->cache.frames, css->surface->pending.frames);
   css->surface->pending.frames = NULL;
   eina_tiler_union(css->cache.damages, css->surface->pending.damages);
   eina_tiler_clear(css->surface->pending.damages);
   css->cache.set_input = css->surface->pending.set_input;
   if (css->surface->pending.set_input)
     {
        eina_tiler_clear(css->cache.input);
        PTR_SWAP(&css->cache.input, &css->surface->pending.input);
     }
   css->cache.set_opaque = css->surface->pending.set_opaque;
   if (css->surface->pending.set_opaque)
     {
        eina_tiler_clear(css->cache.opaque);
        PTR_SWAP(&css->cache.opaque, &css->surface->pending.opaque);
     }
   css->surface->pending.set_input = 0;
   css->surface->pending.set_opaque = 0;
   if (!css->surface->pending.attach) return;
   css->cache.attach = 1;
   comp_surface_buffer_detach(&css->cache.buffer);
   PTR_SWAP(&css->cache.buffer, &css->surface->pending.buffer);
   css->surface->pending.attach = 0;
}

static void
comp_surface_commit(struct wl_client *client, struct wl_resource *resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   Comp_Subsurface *css;
   Eina_List *l;
   Comp_Buffer_State *cbs = &cs->pending;

   cs->commit = 1;
   if (cs->subsurface)
     {
        Comp_Surface *parent;

        css = cs->subsurface;
        parent = cs->parent;
        if ((!parent->commit) && css->sync)
          {
             subcomp_subsurface_cache_merge(css);
             return;
          }
        while (parent && (!parent->commit) && parent->subsurface)
          {
             Comp_Subsurface *pss = parent->subsurface;

             if (pss->sync)
               {
                  subcomp_subsurface_cache_merge(css);
                  return;
               }
             parent = parent->parent;
          }


        subcomp_subsurface_cache_merge(css);
        cbs = &css->cache;
     }

   comp_surface_commit_state(cs, cbs);
   EINA_LIST_FOREACH(cs->subsurfaces, l, css)
     if (css->sync) comp_surface_commit(client, css->surface->res);
   cs->commit = 0;
}

static void
comp_surface_set_buffer_transform(struct wl_client *client, struct wl_resource *resource, int32_t transform)
{
   //Comp_Surface *cs = wl_resource_get_user_data(resource);
}

static void
comp_surface_set_buffer_scale(struct wl_client *client, struct wl_resource *resource, int32_t scale)
{
   //Comp_Surface *cs = wl_resource_get_user_data(resource);
}

static const struct wl_surface_interface comp_surface_interface =
{
   comp_surface_destroy,
   comp_surface_attach,
   comp_surface_damage,
   comp_surface_frame,
   comp_surface_set_opaque_region,
   comp_surface_set_input_region,
   comp_surface_commit,
   comp_surface_set_buffer_transform,
   comp_surface_set_buffer_scale,
   comp_surface_damage_buffer
};

static void
comp_surface_impl_destroy(struct wl_resource *resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   Eina_List *subsurfaces;
   Comp_Subsurface *css;
   Comp_Seat *s;
   struct wl_client *client = wl_resource_get_client(resource);

   EINA_INLIST_FOREACH(cs->c->seats, s)
     {
        if (s->kbd.enter == cs) s->kbd.enter = NULL;
        if (s->ptr.enter == cs) s->ptr.enter = NULL;
        if (s->ptr.cursor.surface == cs) s->ptr.cursor.surface = NULL;
        if (s->drag.surface == cs) s->drag.surface = NULL;
     }
   eina_hash_list_remove(cs->c->client_surfaces, &client, cs);
   if (cs->render_queue)
     cs->c->render_queue = eina_list_remove(cs->c->render_queue, cs);
   subsurfaces = cs->pending_subsurfaces ?: cs->subsurfaces;
   EINA_LIST_FREE(subsurfaces, css)
     {
        evas_object_hide(css->surface->obj);
        comp_surface_reparent(css->surface, NULL);
     }
   if (cs->pending_subsurfaces) eina_list_free(cs->subsurfaces);
   cs->pending_subsurfaces = cs->subsurfaces = NULL;
   comp_surface_buffer_detach(&cs->buffer[0]);
   cs->res = NULL;
   if (cs->post_render_queue && (!cs->dead))
     {
        Eina_List *l;
        Evas_Object *o;

        cs->dead = 1;
        evas_object_hide(cs->obj);
        EINA_LIST_FOREACH(cs->proxies, l, o)
          evas_object_hide(o);
     }
   else
     {
        comp_surface_buffer_detach(&cs->buffer[1]);
        if (!cs->dead) evas_object_del(cs->obj);
     }
}


static Evas_Smart *comp_surface_smart = NULL;

static inline Eina_Bool
comp_surface_check_grab(Comp_Surface *cs, Comp_Seat *s)
{
   Comp_Surface *parent;
   if (!s->grab) return EINA_TRUE;
   if (cs == s->grab) return EINA_TRUE;
   parent = s->grab->parent;
   while (parent)
     {
        if (cs == parent) return EINA_TRUE;
        parent = parent->parent;
     }
   return EINA_FALSE;
}

static void
comp_surface_input_event(Eina_Inlist **list, uint32_t id, uint32_t serial, uint32_t time, Eina_Bool up)
{
   Input_Sequence *ev;

   if (up)
     {
        EINA_INLIST_FOREACH(*list, ev)
          if (ev->id == id)
            {
               ev->up_serial = serial;
               ev->up_time = time;
               return;
            }
        return;
     }
   ev = calloc(1, sizeof(Input_Sequence));
   ev->id = id;
   ev->down_serial = serial;
   ev->down_time = time;
   *list = eina_inlist_append(*list, EINA_INLIST_GET(ev));
}

static void
comp_surface_send_data_device_enter(Comp_Surface *cs, Comp_Seat *s)
{
   struct wl_resource *offer = NULL;
   int x, y, cx, cy;
   uint32_t serial;
   struct wl_resource *res = data_device_find(s, cs->res);


   if (!res) return;
   evas_object_geometry_get(cs->obj, &x, &y, NULL, NULL);
   if (s->drag.tch)
     cx = s->tch.pos.x, cy = s->tch.pos.y;
   else
     cx = s->ptr.pos.x, cy = s->ptr.pos.y;
   if (s->drag.source)
     {
        data_device_offer_create(s->drag.source, res);
        s->drag.source->offer->type = COMP_DATA_DEVICE_OFFER_TYPE_DND;
        offer = s->drag.source->offer->res;
     }
   s->drag.enter = cs;
   s->ptr.enter_serial = serial = wl_display_next_serial(cs->c->display);
   wl_data_device_send_enter(res, serial, cs->res,
       wl_fixed_from_int(cx - x), wl_fixed_from_int(cy - y), offer);
}

static void
comp_surface_send_pointer_enter(Comp_Surface *cs, Comp_Seat *s, int cx, int cy)
{
   Eina_List *l, *ll;
   struct wl_resource *res;
   uint32_t serial;
   int x, y;

   if (s->ptr.enter && (cs != s->grab)) return;
   if (!comp_surface_check_grab(cs, s)) return;
   s->ptr.enter = cs;
   if (cs->dead) return;
   if (s->drag.res && (!s->drag.tch))
     {
        comp_surface_send_data_device_enter(cs, s);
        return;
     }
   l = seat_ptr_resources_get(s, wl_resource_get_client(cs->res));
   if (!l) return;
   s->ptr.enter_serial = serial = wl_display_next_serial(cs->c->display);
   //fprintf(stderr, "ENTER %s\n", cs->shell.popup ? "POPUP" : "TOPLEVEL");
   evas_object_geometry_get(cs->obj, &x, &y, NULL, NULL);
   EINA_LIST_FOREACH(l, ll, res)
     wl_pointer_send_enter(res, serial, cs->res,
       wl_fixed_from_int(cx - x), wl_fixed_from_int(cy - y));
}

static void
comp_surface_mouse_in(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Mouse_In *ev = event_info;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   comp_surface_send_pointer_enter(data, seat_find(data, ev->dev), ev->canvas.x, ev->canvas.y);
}

static void
comp_surface_send_data_device_leave(Comp_Surface *cs, Comp_Seat *s)
{
   struct wl_resource *res = data_device_find(s, cs->res);
   if (!res) return;
   if (s->drag.source)
     {
        s->drag.source->offer->source = NULL;
        s->drag.source->offer = NULL;
     }
   s->drag.enter = NULL;
   wl_data_device_send_leave(res);
}

static void
comp_surface_send_pointer_leave(Comp_Surface *cs, Comp_Seat *s)
{
   Eina_List *l, *ll;
   struct wl_resource *res;
   uint32_t serial;

   if (s->ptr.enter != cs) return;
   if (!comp_surface_check_grab(cs, s)) return;
   s->ptr.enter = NULL;
   if (cs->dead) return;
   if (s->drag.res)
     {
        comp_surface_send_data_device_leave(cs, s);
        return;
     }
   l = seat_ptr_resources_get(s, wl_resource_get_client(cs->res));
   if (!l) return;
   serial = wl_display_next_serial(cs->c->display);
   //fprintf(stderr, "LEAVE %s\n", cs->shell.popup ? "POPUP" : "TOPLEVEL");
   EINA_LIST_FOREACH(l, ll, res)
     wl_pointer_send_leave(res, serial, cs->res);
}

static void
comp_surface_mouse_out(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Mouse_Out *ev = event_info;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   comp_surface_send_pointer_leave(data, seat_find(data, ev->dev));
}

static void
comp_surface_mouse_button(Comp_Surface *cs, Comp_Seat *s, uint32_t timestamp, uint32_t button_id, uint32_t state)
{
   uint32_t serial, btn;
   Eina_List *l, *ll;
   struct wl_resource *res;

   switch (button_id)
     {
      case 1:
        btn = BTN_LEFT;
        break;
      case 2:
        btn = BTN_MIDDLE;
        break;
      case 3:
        btn = BTN_RIGHT;
        break;
      case 4:
      case 5:
      case 6:
      case 7:
        /* these are supposedly axis events */
        return;
      default:
        btn = button_id + BTN_SIDE - 8;
        break;
     }
   if (s->ptr.enter != cs) return;
   if (!comp_surface_check_grab(cs, s)) return;
   if (state == WL_POINTER_BUTTON_STATE_PRESSED)
     s->ptr.button_mask |= 1 << button_id;
   else
     {
        if (!(s->ptr.button_mask & (1 << button_id))) return;
        s->ptr.button_mask &= ~(1 << button_id);
        if (s->drag.res && (!s->drag.tch))
          {
             drag_grab_button(s, timestamp, button_id, WL_POINTER_BUTTON_STATE_RELEASED);
             comp_surface_input_event(&s->ptr.events, button_id, 0, timestamp, state == WL_POINTER_BUTTON_STATE_RELEASED);
             s->ptr.enter = NULL;
             comp_surface_send_pointer_enter(cs, s, s->ptr.pos.x, s->ptr.pos.y);
             return;
          }
     }

   if (cs->dead)
     {
        comp_surface_input_event(&s->ptr.events, button_id, 0, timestamp, state == WL_POINTER_BUTTON_STATE_RELEASED);
        return;
     }

   l = seat_ptr_resources_get(s, wl_resource_get_client(cs->res));
   if (!l) return;
   serial = wl_display_next_serial(s->c->display);
   comp_surface_input_event(&s->ptr.events, button_id, serial, timestamp, state == WL_POINTER_BUTTON_STATE_RELEASED);

   EINA_LIST_FOREACH(l, ll, res)
     wl_pointer_send_button(res, serial, timestamp, btn, state);
}

static void
comp_surface_mouse_down(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Mouse_Down *ev = event_info;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   comp_surface_mouse_button(data, seat_find(data, ev->dev), ev->timestamp, ev->button, WL_POINTER_BUTTON_STATE_PRESSED);
}

static void
comp_surface_mouse_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Mouse_Down *ev = event_info;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   comp_surface_mouse_button(data, seat_find(data, ev->dev), ev->timestamp, ev->button, WL_POINTER_BUTTON_STATE_RELEASED);
}

static void
comp_surface_mouse_move(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Mouse_Move *ev = event_info;
   Comp_Surface *cs = data;
   Eina_List *l = NULL, *ll;
   struct wl_resource *res;
   int x, y;
   Comp_Seat *s;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   if (cs->dead) return;
   s = seat_find(data, ev->dev);
   if (s->ptr.enter != cs) return;
   if (!comp_surface_check_grab(cs, s)) return;
   if (s->drag.res)
     {
        if (s->drag.enter != cs) return;
        res = data_device_find(s, cs->res);
        if (!res) return;
     }
   else
     {
        l = seat_ptr_resources_get(s, wl_resource_get_client(cs->res));
        if (!l) return;
     }
   evas_object_geometry_get(cs->obj, &x, &y, NULL, NULL);
   //fprintf(stderr, "MOTION %s\n", cs->shell.popup ? "POPUP" : "TOPLEVEL");
   if (s->drag.res)
     wl_data_device_send_motion(res, ev->timestamp,
            wl_fixed_from_int(ev->cur.canvas.x - x), wl_fixed_from_int(ev->cur.canvas.y - y));
   else
     {
      //int n = 0;
        EINA_LIST_FOREACH(l, ll, res)
          {
           //fprintf(stderr, "motion %d\n", n++);
             wl_pointer_send_motion(res, ev->timestamp,
               wl_fixed_from_int(ev->cur.canvas.x - x), wl_fixed_from_int(ev->cur.canvas.y - y));
          }
     }
}

static void
comp_surface_mouse_wheel(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event)
{
   Evas_Event_Mouse_Wheel *ev = event;
   Comp_Surface *cs = data;
   Eina_List *l, *ll;
   struct wl_resource *res;
   uint32_t axis, dir;
   Comp_Seat *s;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   if (ev->direction == 0)
     axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
   else
     axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;

   if (ev->z < 0)
     dir = -wl_fixed_from_int(abs(ev->z));
   else
     dir = wl_fixed_from_int(ev->z);

   if (cs->dead) return;
   s = seat_find(data, ev->dev);
   if (s->ptr.enter != cs) return;
   if (!comp_surface_check_grab(cs, s)) return;
   l = seat_ptr_resources_get(s, wl_resource_get_client(cs->res));
   if (!l) return;
   EINA_LIST_FOREACH(l, ll, res)
     wl_pointer_send_axis(res, ev->timestamp, axis, dir);
}

static void
comp_surface_multi_down(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Multi_Down *ev = event_info;
   Comp_Surface *cs = data;
   Eina_List *l, *ll;
   struct wl_resource *res;
   uint32_t serial;
   int x, y;
   Comp_Seat *s;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   if (cs->dead) return;
   s = seat_find(data, ev->dev);
   if (!comp_surface_check_grab(cs, s)) return;
   s->tch.enter = cs;
   l = seat_tch_resources_get(s, wl_resource_get_client(cs->res));
   if (!l)
     {
        comp_surface_input_event(&s->tch.events, ev->device, 0, ev->timestamp, 0);
        return;
     }
   evas_object_geometry_get(cs->obj, &x, &y, NULL, NULL);
   serial = wl_display_next_serial(cs->c->display);
   comp_surface_input_event(&s->tch.events, ev->device, serial, ev->timestamp, 0);
   EINA_LIST_FOREACH(l, ll, res)
     wl_touch_send_down(res, serial, ev->timestamp, cs->res, ev->device,
       wl_fixed_from_int(ev->canvas.x - x), wl_fixed_from_int(ev->canvas.y - y));
}

static void
comp_surface_multi_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Multi_Up *ev = event_info;
   Comp_Surface *cs = data;
   Eina_List *l, *ll;
   struct wl_resource *res;
   uint32_t serial;
   Comp_Seat *s;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   if (cs->dead) return;
   s = seat_find(data, ev->dev);
   if (!comp_surface_check_grab(cs, s)) return;
   if (!ev->device)
     s->tch.enter = NULL;
   l = seat_tch_resources_get(s, wl_resource_get_client(cs->res));
   if ((!l) || (s->drag.tch && ((uint32_t)ev->device == s->drag.id)))
     {
        if (s->drag.tch)
          wl_data_device_send_drop(data_device_find(s, cs->res));
        comp_surface_input_event(&s->tch.events, ev->device, 0, ev->timestamp, 1);
        return;
     }
   serial = wl_display_next_serial(cs->c->display);
   comp_surface_input_event(&s->tch.events, ev->device, serial, ev->timestamp, 1);
   EINA_LIST_FOREACH(l, ll, res)
     wl_touch_send_up(res, serial, ev->timestamp, ev->device);
}

static void
comp_surface_multi_move(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Multi_Move *ev = event_info;
   Comp_Surface *cs = data;
   struct wl_resource *res;
   int x, y;
   Comp_Seat *s;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   if (cs->dead) return;
   s = seat_find(data, ev->dev);
   if (!comp_surface_check_grab(cs, s)) return;
   evas_object_geometry_get(cs->obj, &x, &y, NULL, NULL);
   if (s->drag.tch)
     {
        if (s->drag.enter != cs) return;
        res = data_device_find(s, cs->res);
        if (res)
          wl_data_device_send_motion(res, ev->timestamp,
            wl_fixed_from_int(ev->cur.canvas.x - x), wl_fixed_from_int(ev->cur.canvas.y - y));
        return;
     }
   else
     {
        Eina_List *l, *ll;

        l = seat_tch_resources_get(s, wl_resource_get_client(cs->res));
        if (!l) return;
        EINA_LIST_FOREACH(l, ll, res)
          wl_touch_send_motion(res, ev->timestamp, ev->device,
            wl_fixed_from_int(ev->cur.canvas.x - x), wl_fixed_from_int(ev->cur.canvas.y - y));
     }
}

static void
comp_surface_smart_add(Evas_Object *obj)
{
   Comp_Surface *cs;
   Evas *e;

   cs = calloc(1, sizeof(Comp_Surface));
   evas_object_smart_data_set(obj, cs);
   cs->obj = obj;
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MOUSE_DOWN, comp_surface_mouse_down, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MOUSE_UP, comp_surface_mouse_up, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MOUSE_IN, comp_surface_mouse_in, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MOUSE_OUT, comp_surface_mouse_out, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MOUSE_MOVE, comp_surface_mouse_move, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MOUSE_WHEEL, comp_surface_mouse_wheel, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MULTI_DOWN, comp_surface_multi_down, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MULTI_UP, comp_surface_multi_up, cs);
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_MULTI_MOVE, comp_surface_multi_move, cs);
   e = evas_object_evas_get(obj);
   cs->img = evas_object_image_filled_add(e);
   evas_object_show(cs->img);
   cs->clip = evas_object_rectangle_add(e);

   evas_object_smart_member_add(cs->img, cs->obj);
   evas_object_smart_member_add(cs->clip, cs->obj);
   evas_object_image_border_center_fill_set(cs->img, EVAS_BORDER_FILL_SOLID);
   evas_object_image_colorspace_set(cs->img, EVAS_COLORSPACE_ARGB8888);
}

static void
comp_surface_smart_del(Evas_Object *obj)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);

   array_clear(&cs->input_rects);
   array_clear(&cs->opaque_rects);
   eina_tiler_free(cs->opaque);
   eina_tiler_free(cs->input);
   comp_buffer_state_clear(&cs->pending);
   while (cs->proxies)
     evas_object_del(eina_list_data_get(cs->proxies));
   if (cs->res)
     {
        cs->dead = 1;
        wl_resource_destroy(cs->res);
     }
   evas_object_del(cs->img);
   evas_object_del(cs->clip);
   cs->c->surfaces = eina_inlist_remove(cs->c->surfaces, EINA_INLIST_GET(cs));
   cs->c->surfaces_count--;
   free(cs);
}

static void
comp_surface_smart_move(Evas_Object *obj, int x, int y)
{
   Eina_List *l;
   Evas_Object *o;
   int px, py, cx, cy;

   evas_object_geometry_get(obj, &px, &py, NULL, NULL);
   //{
      //Comp_Surface *cs = evas_object_smart_data_get(obj);
      //if (cs->cursor)
      //fprintf(stderr, "COMP %sSURFACE(%p) %d,%d\n", cs->subsurface ? "SUB" : "", cs, x, y);
   //}
   l = evas_object_smart_members_get(obj);
   EINA_LIST_FREE(l, o)
     {
        evas_object_geometry_get(o, &cx, &cy, NULL, NULL);
        evas_object_move(o, x + (cx - px), y + (cy - py));
        //fprintf(stderr, "SUBOBJ %d,%d\n", x + (cx - px), y + (cy - py));
     }
}

static void
comp_surface_smart_resize(Evas_Object *obj, int w, int h)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);
   evas_object_resize(cs->clip, w, h);
   //if (cs->cursor) fprintf(stderr, "COMP %sSURFACE(%p) %dx%d\n", cs->subsurface ? "SUB" : "", cs, w, h);
   if (cs->drag)
     evas_object_move(cs->obj, cs->drag->ptr.pos.x, cs->drag->ptr.pos.y);
}

static void
comp_surface_smart_show(Evas_Object *obj)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);
   evas_object_show(cs->clip);
   cs->mapped = 1;
}

static void
comp_surface_smart_hide(Evas_Object *obj)
{
   Comp_Surface *lcs, *cs = evas_object_smart_data_get(obj);

   evas_object_hide(cs->clip);
   cs->mapped = 0;

   if (!cs->shell.activated) return;
   cs->shell.activated = 0;
   if (cs->shell.popup && cs->role)
     zxdg_popup_v6_send_popup_done(cs->role);
   if (cs->parent && cs->shell.popup) return;
   /* attempt to revert focus based on stacking order */
   if (cs->parent)
     {
        EINA_INLIST_REVERSE_FOREACH(cs->parent->children, lcs)
          {
             if (lcs == cs) continue;
             if (!evas_object_visible_get(lcs->obj)) continue;
             if ((!lcs->shell.surface) || (!lcs->role)) continue;
             lcs->shell.activated = 1;
             if (lcs->shell.popup && (!lcs->extracted))
               evas_object_raise(lcs->obj);
             else
               shell_surface_send_configure(lcs);
             return;
          }
     }
   if (cs->c->seats)
     comp_seats_redo_enter(cs->c, NULL);
}

static void
comp_surface_smart_clip_set(Evas_Object *obj, Evas_Object *clip)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);
   evas_object_clip_set(cs->clip, clip);
}

static void
comp_surface_smart_clip_unset(Evas_Object *obj)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);
   evas_object_clip_unset(cs->clip);
}

static void
comp_surface_smart_member_add(Evas_Object *obj, Evas_Object *child)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);
   if (child != cs->clip) evas_object_clip_set(child, cs->clip);
}

static void
comp_surface_smart_member_del(Evas_Object *obj, Evas_Object *child)
{
   Comp_Surface *cs = evas_object_smart_data_get(obj);
   if (child != cs->clip) evas_object_clip_unset(child);
}

static void
comp_surface_smart_init(void)
{
   if (comp_surface_smart) return;
   {
      static const Evas_Smart_Class sc =
      {
         "comp_surface",
         EVAS_SMART_CLASS_VERSION,
         comp_surface_smart_add,
         comp_surface_smart_del,
         comp_surface_smart_move,
         comp_surface_smart_resize,
         comp_surface_smart_show,
         comp_surface_smart_hide,
         NULL, //color_set
         comp_surface_smart_clip_set,
         comp_surface_smart_clip_unset,
         NULL,
         comp_surface_smart_member_add,
         comp_surface_smart_member_del,

         NULL,
         NULL,
         NULL,
         NULL
      };
      comp_surface_smart = evas_smart_class_new(&sc);
   }
}

static void
comp_surface_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   struct wl_resource *res;
   Comp_Surface *cs;
   Comp *c = wl_resource_get_user_data(resource);
   Evas_Object *obj;
   int x, y;

   res = wl_resource_create(client, &wl_surface_interface, wl_resource_get_version(resource), id);
   comp_surface_smart_init();
   obj = evas_object_smart_add(c->evas, comp_surface_smart);
   cs = evas_object_smart_data_get(obj);
   cs->res = res;
   evas_object_smart_member_add(cs->obj, c->obj);
   cs->c = c;
   evas_object_geometry_get(c->obj, &x, &y, NULL, NULL);
   evas_object_move(cs->obj, x, y);

   c->surfaces = eina_inlist_prepend(c->surfaces, EINA_INLIST_GET(cs));
   c->surfaces_count++;
   eina_hash_list_append(c->client_surfaces, &client, cs);
   comp_surface_output_enter(cs);

   cs->opaque = tiler_new();
   cs->input = tiler_new();
   comp_buffer_state_alloc(&cs->pending);

   wl_resource_set_implementation(res, &comp_surface_interface, cs, comp_surface_impl_destroy);
}

/////////////////////////////////////////////////////////////////

static void
comp_region_add(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y, int32_t w, int32_t h)
{
   Eina_Tiler *tiler = wl_resource_get_user_data(resource);
   eina_tiler_rect_add(tiler, &(Eina_Rectangle){x, y, w, h});
}

static void
comp_region_subtract(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y, int32_t w, int32_t h)
{
   Eina_Tiler *tiler = wl_resource_get_user_data(resource);
   eina_tiler_rect_del(tiler, &(Eina_Rectangle){x, y, w, h});
}

static const struct wl_region_interface comp_region_interface =
{
   resource_destroy,
   comp_region_add,
   comp_region_subtract
};

static void
comp_region_impl_destroy(struct wl_resource *resource)
{
   eina_tiler_free(wl_resource_get_user_data(resource));
}

static void
comp_region_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   Eina_Tiler *tiler;
   struct wl_resource *res;

   tiler = tiler_new();
   res = wl_resource_create(client, &wl_region_interface, 1, id);
   wl_resource_set_implementation(res, &comp_region_interface, tiler, comp_region_impl_destroy);
}

/////////////////////////////////////////////////////////////////

static const struct wl_compositor_interface comp_interface =
{
   comp_surface_create,
   comp_region_create,
};

static void
comp_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   struct wl_resource *res;

   if (!client_allowed_check(data, client)) return;

   res = wl_resource_create(client, &wl_compositor_interface, version, id);
   wl_resource_set_implementation(res, &comp_interface, data, NULL);
}

/////////////////////////////////////////////////////////////////


static void
subcomp_subsurface_impl_destroy(struct wl_resource *resource)
{
   Comp_Subsurface *css = wl_resource_get_user_data(resource);

   evas_object_hide(css->surface->obj);
   if (css->surface->parent)
     {
        css->surface->parent->subsurfaces = eina_list_remove(css->surface->parent->subsurfaces, css);
        if (css->surface->parent->pending_subsurfaces)
          css->surface->parent->pending_subsurfaces = eina_list_remove(css->surface->parent->pending_subsurfaces, css);
     }
   comp_surface_reparent(css->surface, NULL);
   comp_buffer_state_clear(&css->cache);
   comp_surface_buffer_detach(&css->cache.buffer);
   css->surface->subsurface = NULL;
   css->surface->role = NULL;
   free(css);
}

static void
subcomp_subsurface_set_position(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y)
{
   Comp_Subsurface *css = wl_resource_get_user_data(resource);

   css->pending_offset.x = x;
   css->pending_offset.y = y;
   css->set_offset = 1;
}

static void
subcomp_subsurface_place_above(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *sibling_resource)
{
   Comp_Subsurface *css = wl_resource_get_user_data(resource);
   Comp_Subsurface *css2 = wl_resource_get_user_data(sibling_resource);

   if ((!css) || (!css2))
     {
        wl_resource_post_error( (!css) ? resource : sibling_resource,
                               WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "surface is not a valid subsurface");
        return;
     }

   if (!css->surface->parent->pending_subsurfaces)
     css->surface->parent->pending_subsurfaces = eina_list_clone(css->surface->parent->subsurfaces);
   css->surface->parent->pending_subsurfaces = eina_list_remove(css->surface->parent->pending_subsurfaces, css);
   css->surface->parent->pending_subsurfaces = eina_list_append_relative(css->surface->parent->pending_subsurfaces, css, css2);
}

static void
subcomp_subsurface_place_below(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *sibling_resource)
{
   Comp_Subsurface *css = wl_resource_get_user_data(resource);
   Comp_Subsurface *css2 = wl_resource_get_user_data(sibling_resource);

   if ((!css) || (!css2))
     {
        wl_resource_post_error( (!css) ? resource : sibling_resource,
                               WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE, "surface is not a valid subsurface");
        return;
     }

   if (!css->surface->parent->pending_subsurfaces)
     css->surface->parent->pending_subsurfaces = eina_list_clone(css->surface->parent->subsurfaces);
   css->surface->parent->pending_subsurfaces = eina_list_remove(css->surface->parent->pending_subsurfaces, css);
   css->surface->parent->pending_subsurfaces = eina_list_prepend_relative(css->surface->parent->pending_subsurfaces, css, css2);
}

static void
subcomp_subsurface_set_sync(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
{
   Comp_Subsurface *css = wl_resource_get_user_data(resource);
   css->sync = 1;
}

static void
subcomp_subsurface_set_desync(struct wl_client *client EINA_UNUSED, struct wl_resource *resource)
{
   Comp_Subsurface *css = wl_resource_get_user_data(resource);
   css->sync = 0;
}

static const struct wl_subsurface_interface subcomp_subsurface_interface =
{
   resource_destroy,
   subcomp_subsurface_set_position,
   subcomp_subsurface_place_above,
   subcomp_subsurface_place_below,
   subcomp_subsurface_set_sync,
   subcomp_subsurface_set_desync
};

static void
subcomp_subsurface_create(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *surface_resource, struct wl_resource *parent_resource)
{
   Comp_Subsurface *css;
   Comp_Surface *cs = wl_resource_get_user_data(surface_resource);
   Comp_Surface *pcs = wl_resource_get_user_data(parent_resource);

   if (surface_resource == parent_resource)
     {
        wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
          "cannot create subsurface as its own child");
        return;
     }

   if (cs->role)
     {
        wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
                               "surface already has a role");
        return;
     }

   css = cs->subsurface = calloc(1, sizeof(Comp_Subsurface));
   comp_buffer_state_alloc(&css->cache);
   css->sync = 1;
   evas_object_name_set(cs->img, "subsurface");
   css->surface = cs;
   if (!pcs->pending_subsurfaces)
     pcs->pending_subsurfaces = eina_list_clone(pcs->subsurfaces);
   pcs->pending_subsurfaces = eina_list_append(pcs->pending_subsurfaces, css);
   comp_surface_reparent(cs, pcs);

   cs->role = wl_resource_create(client, &wl_subsurface_interface, 1, id);
   wl_resource_set_implementation(cs->role, &subcomp_subsurface_interface, css, subcomp_subsurface_impl_destroy);
}

static const struct wl_subcompositor_interface subcomp_interface =
{
   resource_destroy,
   subcomp_subsurface_create,
};

static void
subcomp_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   struct wl_resource *res;

   if (!client_allowed_check(data, client)) return;
   res = wl_resource_create(client, &wl_subcompositor_interface, version, id);
   wl_resource_set_implementation(res, &subcomp_interface, data, NULL);
}

/////////////////////////////////////////////////////////////////

static void
data_device_source_offer(struct wl_client *client, struct wl_resource *resource, const char *type)
{
   Comp_Data_Device_Source *ds = wl_resource_get_user_data(resource);

   if ((!ds->proxy) || (!ds->x11_owner))
     ds->mime_types = eina_list_append(ds->mime_types, eina_stringshare_add(type));
}

static void
data_device_source_set_actions(struct wl_client *client, struct wl_resource *resource, uint32_t dnd_actions)
{
   Comp_Data_Device_Source *ds = wl_resource_get_user_data(resource);

   if (ds->actions_set)
     {
        wl_resource_post_error(ds->res, WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
          "dnd_actions already set");
        return;
     }

    if (dnd_actions & ~ALL_ACTIONS)
      {
         wl_resource_post_error(ds->res, WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
           "invalid dnd_actions");
         return;
      }

   if (ds->seat && (!ds->proxy))
     {
        wl_resource_post_error(ds->res, WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
        "drag already begun");
        return;
     }

   ds->dnd_actions = dnd_actions;
   ds->actions_set = 1;
}

static void
data_device_source_impl_destroy(struct wl_resource *resource)
{
   Comp_Data_Device_Source *ds = wl_resource_get_user_data(resource);
   Eina_Stringshare *type;

   EINA_LIST_FREE(ds->mime_types, type) eina_stringshare_del(type);
   while (ds->transfers)
     {
        Comp_Data_Device_Transfer *dt = EINA_INLIST_CONTAINER_GET(ds->transfers, Comp_Data_Device_Transfer);
        fdh_del(dt->fdh);
        ds->transfers = eina_inlist_remove(ds->transfers, EINA_INLIST_GET(dt));
        free(dt);
     }
   if (ds->seat && (ds->seat->selection_source == ds)) ds->seat->selection_source = NULL;
   if (ds->seat && (ds->seat->drag.source == ds)) ds->seat->drag.source = NULL;
   comp_data_device_source_reader_clear(ds);
   ecore_event_handler_del(ds->proxy_send_handler);
   if (ds->offer) ds->offer->source = NULL;
   free(ds);
}

static const struct wl_data_source_interface data_device_source_interface =
{
   data_device_source_offer,
   resource_destroy,
   data_device_source_set_actions,
};

static void
data_device_manager_source_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   Comp_Data_Device_Source *ds;
   pid_t pid;
   Comp_Seat *s;
   Comp *c;

   ds = calloc(1, sizeof(Comp_Data_Device_Source));
   c = wl_resource_get_user_data(resource);
   wl_client_get_credentials(client, &pid, NULL, NULL);
   ds->proxy = c->client_disp && (pid == getpid());
   ds->res = wl_resource_create(client, &wl_data_source_interface, MIN(wl_resource_get_version(resource), 3), id);
   wl_resource_set_implementation(ds->res, &data_device_source_interface, ds, data_device_source_impl_destroy);
   if (!ds->proxy) return;
   EINA_INLIST_FOREACH(c->seats, s)
     {
        int x, y;
        struct wl_resource *res;
        Eina_Array *arr;
        Eina_Array_Iterator it;
        unsigned int i;
        char *type;
        if (((!s->client_offer) && (!s->drag.x11_owner)) || s->drag.res) continue;

        //proxied drag
        s->drag.res = resource;
        s->drag.source = ds;
        ds->seat = s;
        if (!s->drag.enter) return;
        evas_object_geometry_get(s->drag.enter->obj, &x, &y, NULL, NULL);
        if (s->client_offer)
          {
             arr = ecore_wl2_offer_mimes_get(s->client_offer);
             EINA_ARRAY_ITER_NEXT(arr, i, type, it)
               ds->mime_types = eina_list_append(ds->mime_types, eina_stringshare_add(type));
          }
        else if (s->drag.x11_owner)
          {
             PTR_SWAP(&s->drag.x11_types, &ds->mime_types);
             ds->x11_owner = s->drag.x11_owner;
             s->drag.x11_owner = 0;
          }
        comp_surface_send_data_device_enter(s->drag.enter, s);
        res = data_device_find(s, s->drag.enter->res);
        if (!res) return;
        wl_data_device_send_motion(res,
          (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff),
          wl_fixed_from_int(s->ptr.pos.x - x), wl_fixed_from_int(s->ptr.pos.y - y));
     }
}

static void
data_device_manager_device_impl_destroy(struct wl_resource *resource)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_client *client = wl_resource_get_client(resource);
   eina_hash_del_by_key(s->data_devices, &client);
}

static void
data_device_start_drag(struct wl_client *client, struct wl_resource *resource, struct wl_resource *source_resource, struct wl_resource *origin_resource, struct wl_resource *icon_resource, uint32_t serial)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   Comp_Data_Device_Source *ds = NULL;
   Comp_Surface *cs = wl_resource_get_user_data(origin_resource);
   Comp_Surface *ics = wl_resource_get_user_data(icon_resource);
   Comp_Surface *enter;
   Input_Sequence *ev;
   Eina_Bool found = EINA_FALSE, up = EINA_FALSE;
   int cx, cy;

   if (source_resource) ds = wl_resource_get_user_data(source_resource);
   if ((cs != s->ptr.enter) && (cs != s->tch.enter)) return;
   enter = s->ptr.enter;
   if (enter) comp_surface_send_pointer_leave(enter, s);
   EINA_INLIST_FOREACH(s->tch.events, ev)
     {
        if (ev->down_serial == serial)
          {
             s->drag.tch = 1;
             up = !!ev->up_time;
             found = 1;
             s->drag.id = ev->id;
             break;
          }
     }
   if (!found)
     {
        EINA_INLIST_FOREACH(s->ptr.events, ev)
          {
             if (ev->down_serial == serial)
               {
                  up = !!ev->up_time;
                  found = 1;
                  s->drag.id = ev->id;
                  break;
               }
          }
     }
   if (!found) return;
   if (s->ptr.enter && (!s->drag.tch))
     comp_surface_send_pointer_leave(s->ptr.enter, s);
   s->drag.res = resource;
   s->drag.source = ds;
   if (ds) ds->seat = s;
   s->drag.surface = ics;
   if (ds && ds->proxy && s->drag.x11_owner)
     ds->x11_owner = s->drag.x11_owner;
   s->drag.x11_owner = 0;

   if (s->drag.tch)
     {
        cx = s->tch.pos.x;
        cy = s->tch.pos.y;
     }
   else
     {
        cx = s->ptr.pos.x;
        cy = s->ptr.pos.y;
     }
   if (ics)
     {
        int w, h;

        ics->cursor = 1;
        ics->drag = s;
        evas_object_smart_member_del(ics->obj);
        evas_object_pass_events_set(ics->obj, 1);
        evas_object_layer_set(ics->obj, EVAS_LAYER_MAX - 1);
        evas_object_geometry_get(ics->obj, NULL, NULL, &w, &h);
        evas_object_move(ics->obj, cx, cy);
        evas_object_show(ics->obj);
     }
   if (s->drag.tch)
     {
        if (s->tch.enter)
          comp_surface_send_data_device_enter(s->tch.enter, s);
     }
   else if (enter)
     comp_surface_send_pointer_enter(enter, s, cx, cy);
#ifdef HAVE_ECORE_X
   if (ecore_x_display_get())
     ecore_x_pointer_grab(ecore_evas_window_get(ecore_evas_ecore_evas_get(s->c->evas)));
#endif
   if (up) drag_grab_button(s, ev->up_time, ev->id, WL_POINTER_BUTTON_STATE_RELEASED);
}

static Eina_Bool
data_device_proxy_send_send(void *d, int t EINA_UNUSED, void *event)
{
   Comp_Data_Device_Source *ds = d;
   Ecore_Wl2_Event_Data_Source_Send *ev = event;

   if (ds->proxy_serial != ev->serial) return ECORE_CALLBACK_RENEW;
   if (ev->display == ds->seat->c->parent_disp)
     {
        if (ecore_wl2_input_seat_id_get(ds->seat->seat) != ev->seat) return ECORE_CALLBACK_RENEW;
        wl_data_source_send_send(ds->res, ev->type, ev->fd);
        close(ev->fd);
     }
   return ECORE_CALLBACK_RENEW;
}

static void
data_device_set_selection(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *source_resource, uint32_t serial)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   Comp_Data_Device_Source *ds = NULL;

   if (source_resource)
     ds = wl_resource_get_user_data(source_resource);

   if (ds && ds->actions_set)
     {
        wl_resource_post_error(source_resource, WL_DATA_SOURCE_ERROR_INVALID_SOURCE, "invalid source");
        return;
     }
   if (s->selection_source && (s->selection_serial - serial < UINT32_MAX / 2))
     return;
   if (s->selection_source)
     {
        if (!s->selection_source->transfers)
          comp_data_device_source_reader_clear(s->selection_source);
        ecore_event_handler_del(s->selection_source->proxy_send_handler);
        s->selection_source->proxy_send_handler = NULL;
     }
   if (ds) ds->seat = s;
   s->selection_source = ds;
   s->selection_serial = serial;
   comp_seat_kbd_data_device_enter(s);
   if (ds && ds->proxy && s->x11_selection_owner)
     ds->x11_owner = s->x11_selection_owner;
   s->x11_selection_owner = 0;
   if (1)
   //if (s->c->data_device_proxy)
     {
        if (s->c->parent_disp) //wayland
          {
             if (ds && ds->mime_types)
               {
                  char *t, *types[eina_list_count(ds->mime_types) + 1];
                  Eina_List *l;
                  int i = 0;

                  EINA_LIST_FOREACH(ds->mime_types, l, t)
                    types[i++] = t;
                  types[i] = NULL;
                  ds->proxy_serial = ecore_wl2_dnd_selection_set(s->seat, (const char**)types);
                  ds->proxy_send_handler =
                    ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND,
                      data_device_proxy_send_send, ds);
               }
             else if (ds)
               ds->proxy_serial = ecore_wl2_dnd_selection_clear(s->seat);
          }
#ifdef HAVE_ECORE_X
        else if (ds && (!ds->proxy) && ecore_x_display_get())
          {
             Eina_List *l;
             Comp *c;
             Ecore_X_Time t = ecore_x_current_time_get();

             EINA_LIST_FOREACH(comps, l, c)
               c->x11_selection = 0;
             s->c->x11_selection = 1;
             ecore_x_selection_owner_set(ecore_evas_window_get(ecore_evas_ecore_evas_get(s->c->evas)),
               ECORE_X_ATOM_SELECTION_CLIPBOARD, t);
          }
#endif
     }
   s->client_selection_serial = 0;
}

static const struct wl_data_device_interface data_device_interface =
{
   data_device_start_drag,
   data_device_set_selection,
   resource_destroy,
};

static void
data_device_manager_device_create(struct wl_client *client, struct wl_resource *manager_resource, uint32_t id, struct wl_resource *seat_resource)
{
   Comp_Seat *s = wl_resource_get_user_data(seat_resource);
   struct wl_resource *res;

   res = wl_resource_create(client, &wl_data_device_interface, wl_resource_get_version(manager_resource), id);
   wl_resource_set_implementation(res, &data_device_interface, s, data_device_manager_device_impl_destroy);
   eina_hash_add(s->data_devices, &client, res);
}

static const struct wl_data_device_manager_interface data_device_manager_interface =
{
   data_device_manager_source_create,
   data_device_manager_device_create
};

static void
data_device_manager_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   struct wl_resource *res;

   if (!client_allowed_check(data, client)) return;
   res = wl_resource_create(client, &wl_data_device_manager_interface, MIN(3, version), id);
   wl_resource_set_implementation(res, &data_device_manager_interface, data, NULL);
}

/////////////////////////////////////////////////////////////////

static void
output_resize(Comp *c, struct wl_resource *res)
{
   int w, h;
   int rot[][4] =
   {
    {
     [EFL_WL_ROTATION_0] = WL_OUTPUT_TRANSFORM_NORMAL,
     [EFL_WL_ROTATION_90] = WL_OUTPUT_TRANSFORM_90,
     [EFL_WL_ROTATION_180] = WL_OUTPUT_TRANSFORM_180,
     [EFL_WL_ROTATION_270] = WL_OUTPUT_TRANSFORM_270,
    },
    {
     [EFL_WL_ROTATION_0] = WL_OUTPUT_TRANSFORM_FLIPPED,
     [EFL_WL_ROTATION_90] = WL_OUTPUT_TRANSFORM_FLIPPED_90,
     [EFL_WL_ROTATION_180] = WL_OUTPUT_TRANSFORM_FLIPPED_180,
     [EFL_WL_ROTATION_270] = WL_OUTPUT_TRANSFORM_FLIPPED_270,
    },
   };

   evas_object_geometry_get(c->clip, NULL, NULL, &w, &h);
   /* FIXME: transform */
   wl_output_send_geometry(res, 0, 0, w, h, 0, "", "", rot[c->rtl][c->rotation]);
   wl_output_send_mode(res, WL_OUTPUT_MODE_CURRENT, w, h, 60 * 1000);
   if (wl_resource_get_version(res) >= WL_OUTPUT_DONE_SINCE_VERSION)
     wl_output_send_done(res);
}

static void
output_unbind(struct wl_resource *resource)
{
   Comp *c = wl_resource_get_user_data(resource);
   c->output_resources = eina_list_remove(c->output_resources, resource);
}

static void
output_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   Comp *c = data;
   Comp_Surface *cs;
   struct wl_resource *res;

   if (!client_allowed_check(data, client)) return;
   res = wl_resource_create(client, &wl_output_interface, version, id);
   c->output_resources = eina_list_append(c->output_resources, res);
   wl_resource_set_implementation(res, NULL, data, output_unbind);
   if (wl_resource_get_version(res) >= WL_OUTPUT_SCALE_SINCE_VERSION)
     wl_output_send_scale(res, lround(c->scale));
   output_resize(c, res);
   EINA_INLIST_FOREACH(c->surfaces, cs)
     if (wl_resource_get_client(cs->res) == client)
       comp_surface_output_enter(cs);
}

/////////////////////////////////////////////////////////////////

static void
shell_surface_toplevel_impl_destroy(struct wl_resource *resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   cs->role = NULL;
   evas_object_hide(cs->obj);
   if (!cs->parent) return;
   comp_surface_reparent(cs, NULL);
}

static void
shell_surface_toplevel_set_parent(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *parent_resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   Comp_Surface *pcs = NULL;

   if (parent_resource) pcs = wl_resource_get_user_data(parent_resource);

   comp_surface_reparent(cs, pcs);
   evas_object_smart_callback_call(cs->c->obj, "child_added", cs->obj);
}

static void
shell_surface_toplevel_set_title(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, const char *title)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   eina_stringshare_replace(&cs->shell.title, title);
}

static void
shell_surface_toplevel_set_app_id(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, const char *id)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   eina_stringshare_replace(&cs->shell.app_id, id);
}

static void
shell_surface_toplevel_show_window_menu(){}
static void
shell_surface_toplevel_move(){}
static void
shell_surface_toplevel_resize(){}

static void
shell_surface_toplevel_set_max_size(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t w, int32_t h)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   evas_object_size_hint_max_set(cs->obj, w, h);
   if (cs == cs->c->active_surface)
     shell_surface_minmax_update(cs);
}

static void
shell_surface_toplevel_set_min_size(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t w, int32_t h)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   evas_object_size_hint_min_set(cs->obj, w, h);
   if (cs == cs->c->active_surface)
     shell_surface_minmax_update(cs);
}

static void
shell_surface_toplevel_set_maximized(){}
static void
shell_surface_toplevel_unset_maximized(){}
static void
shell_surface_toplevel_set_fullscreen(){}
static void
shell_surface_toplevel_unset_fullscreen(){}
static void
shell_surface_toplevel_set_minimized(){}

static const struct zxdg_toplevel_v6_interface shell_surface_toplevel_interface =
{
   resource_destroy,
   shell_surface_toplevel_set_parent,
   shell_surface_toplevel_set_title,
   shell_surface_toplevel_set_app_id,
   shell_surface_toplevel_show_window_menu,
   shell_surface_toplevel_move,
   shell_surface_toplevel_resize,
   shell_surface_toplevel_set_max_size,
   shell_surface_toplevel_set_min_size,
   shell_surface_toplevel_set_maximized,
   shell_surface_toplevel_unset_maximized,
   shell_surface_toplevel_set_fullscreen,
   shell_surface_toplevel_unset_fullscreen,
   shell_surface_toplevel_set_minimized,
};

static void
shell_surface_toplevel_create(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, uint32_t id)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   if (cs->buffer[0] || cs->pending.buffer)
     {
        wl_resource_post_error(resource, ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
                               "buffer attached/committed before configure");
        return;
     }
   if (cs->role)
     {
        wl_resource_post_error(resource, ZXDG_SHELL_V6_ERROR_ROLE,
                               "surface already has assigned role");
        return;
     }

   cs->role = wl_resource_create(client, &zxdg_toplevel_v6_interface, 1, id);
   wl_resource_set_implementation(cs->role, &shell_surface_toplevel_interface, cs, shell_surface_toplevel_impl_destroy);
   cs->shell.new = 1;
}

static void
shell_surface_popup_grab(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, struct wl_resource *seat, uint32_t serial EINA_UNUSED)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   Comp_Seat *s = wl_resource_get_user_data(seat);

   if (cs->dead || (!cs->role) || (!cs->shell.surface))
     {
        wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, "can't grab for this resource");
        return;
     }
   if (cs->mapped)
     {
        wl_resource_post_error(resource, ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
                                "grab requested on mapped popup");
        return;
     }

   if (cs->parent->shell.popup && (s->grab != cs->parent))
     {
        wl_resource_post_error(resource, ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
                                "grab requested on ungrabbed nested popup");
        return;
     }
   s->grab = cs;
   cs->shell.grabs = eina_list_append(cs->shell.grabs, s);
}

static const struct zxdg_popup_v6_interface shell_surface_popup_interface =
{
   resource_destroy,
   shell_surface_popup_grab,
};

static void
shell_surface_popup_impl_destroy(struct wl_resource *resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);
   Comp_Seat *s;

   cs->role = NULL;
   evas_object_hide(cs->obj);
   cs->shell.popup = 0;
   EINA_LIST_FREE(cs->shell.grabs, s)
     if (s->grab == cs)
       {
          if (cs->parent->shell.grabs &&
              eina_list_data_find(cs->parent->shell.grabs, s))
            s->grab = cs->parent;
          else
            s->grab = NULL;
       }
   if (cs->children)
     wl_resource_post_error(cs->shell.surface, ZXDG_SHELL_V6_ERROR_DEFUNCT_SURFACES,
                            "popups dismissed out of order");
   if (cs->parent)
     comp_surface_reparent(cs, NULL);
}

static void
shell_surface_popup_create(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *parent_resource, struct wl_resource *positioner_resource)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   if (cs->buffer[0] || cs->pending.buffer)
     {
        wl_resource_post_error(resource, ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
                               "buffer attached/committed before configure");
        return;
     }
   if (cs->role)
     {
        wl_resource_post_error(resource, ZXDG_SHELL_V6_ERROR_ROLE,
                               "surface already has assigned role");
        return;
     }
   if (!parent_resource)
     {
        wl_resource_post_error(resource, ZXDG_SHELL_V6_ERROR_INVALID_POPUP_PARENT,
                               "popup surface has no parent");
        return;
     }

   cs->role = wl_resource_create(client, &zxdg_popup_v6_interface, 1, id);
   wl_resource_set_implementation(cs->role, &shell_surface_popup_interface, cs, shell_surface_popup_impl_destroy);
   cs->shell.new = 1;
   cs->shell.popup = 1;
   comp_surface_reparent(cs, wl_resource_get_user_data(parent_resource));
   cs->shell.positioner = wl_resource_get_user_data(positioner_resource);
   _apply_positioner(cs, cs->shell.positioner);
   evas_object_smart_callback_call(cs->c->obj, "popup_added", cs->obj);
}

static void
_validate_size(struct wl_resource *resource, int32_t value)
{
   if (value <= 0)
     wl_resource_post_error(resource, ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, "Invalid size passed");
}

static void
shell_positioner_set_size(struct wl_client *wl_client EINA_UNUSED, struct wl_resource *resource, int32_t w, int32_t h)
{
   Shell_Positioner *p = wl_resource_get_user_data(resource);

   _validate_size(resource, w);
   _validate_size(resource, h);

   p->size.w = w;
   p->size.h = h;
}

static void
shell_positioner_set_anchor_rect(struct wl_client *wl_client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y, int32_t w, int32_t h)
{
   Shell_Positioner *p = wl_resource_get_user_data(resource);

   _validate_size(resource, w);
   _validate_size(resource, h);

   EINA_RECTANGLE_SET(&p->anchor_rect, x, y, w, h);
}

static void
shell_positioner_set_anchor(struct wl_client *wl_client EINA_UNUSED, struct wl_resource *resource, enum zxdg_positioner_v6_anchor anchor)
{
   Shell_Positioner *p = wl_resource_get_user_data(resource);

   if ((anchor & (ZXDG_POSITIONER_V6_ANCHOR_TOP | ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)) ==
       (ZXDG_POSITIONER_V6_ANCHOR_TOP | ZXDG_POSITIONER_V6_ANCHOR_BOTTOM))
     wl_resource_post_error(resource, ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, "Invalid anchor values passed");
   else if ((anchor & (ZXDG_POSITIONER_V6_ANCHOR_LEFT | ZXDG_POSITIONER_V6_ANCHOR_RIGHT)) ==
       (ZXDG_POSITIONER_V6_ANCHOR_LEFT | ZXDG_POSITIONER_V6_ANCHOR_RIGHT))
     wl_resource_post_error(resource, ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, "Invalid anchor values passed");
   else
     p->anchor = anchor;
}

static void
shell_positioner_set_gravity(struct wl_client *wl_client EINA_UNUSED, struct wl_resource *resource, enum zxdg_positioner_v6_gravity gravity)
{
   Shell_Positioner *p = wl_resource_get_user_data(resource);

   if ((gravity & (ZXDG_POSITIONER_V6_GRAVITY_TOP | ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)) ==
       (ZXDG_POSITIONER_V6_GRAVITY_TOP | ZXDG_POSITIONER_V6_GRAVITY_BOTTOM))
     wl_resource_post_error(resource, ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, "Invalid gravity values passed");
   else if ((gravity & (ZXDG_POSITIONER_V6_GRAVITY_LEFT | ZXDG_POSITIONER_V6_GRAVITY_RIGHT)) ==
       (ZXDG_POSITIONER_V6_GRAVITY_LEFT | ZXDG_POSITIONER_V6_GRAVITY_RIGHT))
     wl_resource_post_error(resource, ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, "Invalid gravity values passed");
   else
     p->gravity = gravity;
}

static void
shell_positioner_set_constraint_adjustment(struct wl_client *wl_client EINA_UNUSED, struct wl_resource *resource, enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment)
{
   Shell_Positioner *p = wl_resource_get_user_data(resource);

   p->constrain = constraint_adjustment;
}

static void
shell_positioner_set_offset(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y)
{
   Shell_Positioner *p = wl_resource_get_user_data(resource);

   p->offset.x = x;
   p->offset.y = y;
}

static const struct zxdg_positioner_v6_interface shell_positioner_interface =
{
   resource_destroy,
   shell_positioner_set_size,
   shell_positioner_set_anchor_rect,
   shell_positioner_set_anchor,
   shell_positioner_set_gravity,
   shell_positioner_set_constraint_adjustment,
   shell_positioner_set_offset,
};

static void
shell_positioner_impl_destroy(struct wl_resource *resource)
{
   Shell_Positioner *sp;

   sp = wl_resource_get_user_data(resource);
   if (!sp) return;
   if (sp->sd) sp->sd->positioners = eina_inlist_remove(sp->sd->positioners, EINA_INLIST_GET(sp));
   free(sp);
}

static void
shell_positioner_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   struct wl_resource *res;
   Shell_Data *sd;
   Shell_Positioner *sp;

   sd = wl_resource_get_user_data(resource);
   res = wl_resource_create(client, &zxdg_positioner_v6_interface, 1, id);
   sp = calloc(1, sizeof(Shell_Positioner));
   sp->sd = sd;
   sp->res = res;
   sd->positioners = eina_inlist_append(sd->positioners, EINA_INLIST_GET(sp));
   wl_resource_set_implementation(res, &shell_positioner_interface, sp, shell_positioner_impl_destroy);
}

static void
shell_surface_set_window_geometry(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, int32_t x, int32_t y, int32_t w, int32_t h)
{
   Comp_Surface *cs = wl_resource_get_user_data(resource);

   EINA_RECTANGLE_SET(&cs->shell.geom, x, y, w, h);
}

static void
shell_surface_ack_configure(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, uint32_t serial)
{
}

static const struct zxdg_surface_v6_interface shell_surface_interface =
{
   resource_destroy,
   shell_surface_toplevel_create,
   shell_surface_popup_create,
   shell_surface_set_window_geometry,
   shell_surface_ack_configure,
};

static void
shell_surface_impl_destroy(struct wl_resource *resource)
{
   Comp_Surface *ccs, *cs = wl_resource_get_user_data(resource);

   if (cs->role)
     {
        wl_resource_post_error(resource, ZXDG_SHELL_V6_ERROR_DEFUNCT_SURFACES, "shell surface destroyed before role surfaces");
        wl_resource_destroy(cs->role);
     }
   cs->shell.surface = NULL;
   cs->shell.data->surfaces = eina_list_remove(cs->shell.data->surfaces, cs);
   cs->shell.data = NULL;
   while (cs->children)
     {
        ccs = EINA_INLIST_CONTAINER_GET(cs->children, Comp_Surface);
        evas_object_hide(ccs->obj);
        comp_surface_reparent(ccs, cs->parent);
     }
}

static void
shell_surface_create(struct wl_client *client, struct wl_resource *resource, uint32_t id, struct wl_resource *surface_resource)
{
   Shell_Data *sd = wl_resource_get_user_data(resource);
   Comp_Surface *cs = wl_resource_get_user_data(surface_resource);

   if (cs->role || cs->shell.surface)
     {
        wl_resource_post_error(surface_resource, WL_DISPLAY_ERROR_INVALID_OBJECT, "surface already has role");
        return;
     }

   cs->shell.surface = wl_resource_create(client, &zxdg_surface_v6_interface, 1, id);
   cs->shell.data = sd;
   wl_resource_set_implementation(cs->shell.surface, &shell_surface_interface, cs, shell_surface_impl_destroy);
   sd->surfaces = eina_list_append(sd->surfaces, cs);
}

static void
shell_pong(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, uint32_t serial EINA_UNUSED)
{
   Shell_Data *sd = wl_resource_get_user_data(resource);

   sd->ping = 0;
}

static const struct zxdg_shell_v6_interface shell_interface =
{
   resource_destroy,
   shell_positioner_create,
   shell_surface_create,
   shell_pong
};

static void
shell_unbind(struct wl_resource *resource)
{
   Shell_Data *sd = wl_resource_get_user_data(resource);

   sd->c->shells = eina_inlist_remove(sd->c->shells, EINA_INLIST_GET(sd));
   while (sd->surfaces)
     {
        Comp_Surface *cs = eina_list_data_get(sd->surfaces);
        if (cs->shell.surface)
          {
             if (cs->role)
               wl_resource_destroy(cs->role);
             wl_resource_destroy(cs->shell.surface);
          }
     }
   free(sd);
}

static void
shell_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   Comp *c = data;
   struct wl_resource *res;
   Shell_Data *sd;

   if (!client_allowed_check(data, client)) return;
   sd = calloc(1, sizeof(Shell_Data));
   sd->c = c;
   c->shells = eina_inlist_append(c->shells, EINA_INLIST_GET(sd));

   res = wl_resource_create(client, &zxdg_shell_v6_interface, version, id);
   sd->res = res;
   wl_resource_set_implementation(res, &shell_interface, sd, shell_unbind);
}

/////////////////////////////////////////////////////////////////

static void
seat_update_caps(Comp_Seat *s, struct wl_resource *res)
{
   enum wl_seat_capability caps = 0;
   Eina_List *l;

   if (s->pointer)
     caps |= WL_SEAT_CAPABILITY_POINTER;
   if (s->keyboard)
     caps |= WL_SEAT_CAPABILITY_KEYBOARD;
   if (s->touch)
     caps |= WL_SEAT_CAPABILITY_TOUCH;

   if (!caps) return;
   if (res)
     wl_seat_send_capabilities(res, caps);
   else
     EINA_LIST_FOREACH(s->resources, l, res)
       wl_seat_send_capabilities(res, caps);
}

static void
seat_keymap_update(Comp_Seat *s)
{
   char *str;
   Eina_Tmpstr *file;
   Eina_List *l;
   Eina_Iterator *it;
   xkb_mod_mask_t latched = 0, locked = 0;

   if (s->kbd.keymap_mem) munmap(s->kbd.keymap_mem, s->kbd.keymap_mem_size);
   if (s->kbd.keymap_fd > -1) close(s->kbd.keymap_fd);

   if (s->kbd.state)
     {
        latched = xkb_state_serialize_mods(s->kbd.state, XKB_STATE_MODS_LATCHED);
        locked = xkb_state_serialize_mods(s->kbd.state, XKB_STATE_MODS_LOCKED);
        xkb_state_unref(s->kbd.state);
     }
   if (!s->kbd.keymap)
     {
        s->kbd.state = NULL;
        s->kbd.keymap_fd = -1;
        s->kbd.keymap_mem = NULL;
        return;
     }

   s->kbd.state = xkb_state_new(s->kbd.keymap);
   xkb_state_update_mask(s->kbd.state, 0, latched, locked, 0, 0, 0);

   str = xkb_map_get_as_string(s->kbd.keymap);
   s->kbd.keymap_mem_size = strlen(str) + 1;
   s->kbd.keymap_fd = eina_file_mkstemp("comp-keymapXXXXXX", &file);
   if (s->kbd.keymap_fd < 0)
     {
        EINA_LOG_ERR("mkstemp failed!\n");
        s->kbd.keymap_fd = -1;
        xkb_state_unref(s->kbd.state);
        s->kbd.state = NULL;
        return;
     }
   if (!eina_file_close_on_exec(s->kbd.keymap_fd, 1))
     {
        EINA_LOG_ERR("Failed to set CLOEXEC on fd %d\n", s->kbd.keymap_fd);
        close(s->kbd.keymap_fd);
        s->kbd.keymap_fd = -1;
        xkb_state_unref(s->kbd.state);
        s->kbd.state = NULL;
        return;
     }
   ftruncate(s->kbd.keymap_fd, s->kbd.keymap_mem_size);
   eina_file_unlink(file);
   eina_tmpstr_del(file);
   s->kbd.keymap_mem =
     mmap(NULL, s->kbd.keymap_mem_size + 1,
       PROT_READ | PROT_WRITE, MAP_SHARED, s->kbd.keymap_fd, 0);

   memcpy(s->kbd.keymap_mem, str, s->kbd.keymap_mem_size);
   s->kbd.keymap_mem[s->kbd.keymap_mem_size] = 0;
   free(str);

   it = eina_hash_iterator_data_new(s->kbd.resources);
   EINA_ITERATOR_FOREACH(it, l)
     {
        Eina_List *ll;
        struct wl_resource *res;
        EINA_LIST_FOREACH(l, ll, res)
          wl_keyboard_send_keymap(res, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, s->kbd.keymap_fd, s->kbd.keymap_mem_size);
     }
   eina_iterator_free(it);
}

static inline void
seat_kbd_repeat_rate_update(Comp_Seat *s)
{
   double rate, delay;

   if (s->seat)
     {
        if (ecore_wl2_input_keyboard_repeat_get(s->seat, &rate, &delay))
          {
             s->kbd.repeat_rate = lround(1 / rate);
             s->kbd.repeat_delay = lround(delay * 1000);
          }
        else
          s->kbd.repeat_rate = s->kbd.repeat_delay = 0;
     }
   else
     {
        s->kbd.repeat_rate = 40;
        s->kbd.repeat_delay = 400;
     }
}

static void
seat_keymap_create(Comp_Seat *s)
{
   struct xkb_rule_names names;

   memset(&names, 0, sizeof(names));
   names.rules = "evdev";
   names.model = "pc105";
   names.layout = "us";
   s->kbd.context = xkb_context_new(0);
   s->kbd.keymap = xkb_map_new_from_names(s->kbd.context, &names, 0);
}

static Eina_Bool
seat_mods_update(Comp_Seat *s)
{
   xkb_mod_mask_t mod;
   xkb_layout_index_t grp;

   mod = xkb_state_serialize_mods(s->kbd.state, XKB_STATE_DEPRESSED);
   s->kbd.mods.changed |= mod != s->kbd.mods.depressed;
   s->kbd.mods.depressed = mod;
   mod = xkb_state_serialize_mods(s->kbd.state, XKB_STATE_MODS_LATCHED);
   s->kbd.mods.changed |= mod != s->kbd.mods.latched;
   s->kbd.mods.latched = mod;
   mod = xkb_state_serialize_mods(s->kbd.state, XKB_STATE_MODS_LOCKED);
   s->kbd.mods.changed |= mod != s->kbd.mods.locked;
   s->kbd.mods.locked = mod;
   grp = xkb_state_serialize_layout(s->kbd.state, XKB_STATE_LAYOUT_EFFECTIVE);
   s->kbd.mods.changed |= grp != s->kbd.mods.group;
   s->kbd.mods.group = grp;
   return s->kbd.mods.changed;
}

static const struct wl_keyboard_interface seat_kbd_interface =
{
   resource_destroy
};

static void
seat_kbd_unbind(struct wl_resource *resource)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_client *client = wl_resource_get_client(resource);

   eina_hash_list_remove(s->kbd.resources, &client, resource);
}

static void
seat_kbd_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_resource *res;
   Eina_List *l, *ll;
   uint32_t serial;

   res = wl_resource_create(client, &wl_keyboard_interface, wl_resource_get_version(resource), id);
   wl_resource_set_implementation(res, &seat_kbd_interface, s, seat_kbd_unbind);
   if (!s->kbd.resources) s->kbd.resources = eina_hash_pointer_new(NULL);
   eina_hash_list_append(s->kbd.resources, &client, res);

   wl_keyboard_send_keymap(res, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, s->kbd.keymap_fd, s->kbd.keymap_mem_size);

   if (wl_resource_get_version(res) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION)
     wl_keyboard_send_repeat_info(res, s->kbd.repeat_rate, s->kbd.repeat_delay);

   if (s->active_client != client) return;
   l = seat_kbd_active_resources_get(s);
   if (!l) return;
   serial = wl_display_next_serial(s->c->display);
   EINA_LIST_FOREACH(l, ll, res)
     {
        if (s->c->active_surface)
          wl_keyboard_send_enter(res, serial, s->c->active_surface->res, &s->kbd.keys);
        comp_seat_send_modifiers(s, res, serial);
     }
}

static void
seat_ptr_del(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Comp_Seat *s = data;

   evas_object_event_callback_del_full(s->ptr.efl.obj, EVAS_CALLBACK_DEL, seat_ptr_del, s);
   s->ptr.efl.obj = NULL;
}

static void
seat_ptr_inherit(Comp_Seat *s, Eo *dev)
{
   ecore_evas_cursor_device_get(ecore_evas_ecore_evas_get(s->c->evas), dev, &s->ptr.efl.obj,
              &s->ptr.efl.layer, &s->ptr.efl.x, &s->ptr.efl.y);
   if (s->ptr.efl.obj)
     evas_object_event_callback_add(s->ptr.efl.obj, EVAS_CALLBACK_DEL, seat_ptr_del, s);
}

static void
seat_ptr_set_cursor(struct wl_client *client, struct wl_resource *resource, uint32_t serial, struct wl_resource *surface_resource, int32_t x, int32_t y)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   Comp_Surface *cs = NULL;

   if (!s->active_client) return;
   if (surface_resource && (s->active_client != wl_resource_get_client(surface_resource))) return;
   if (s->ptr.enter_serial - serial > UINT32_MAX / 2) return;
   if (surface_resource)
     cs = wl_resource_get_user_data(surface_resource);
   if (cs && cs->role && (!cs->cursor))
     {
        wl_resource_post_error(surface_resource,
                               WL_POINTER_ERROR_ROLE, "surface already has role");
        return;
     }
   if (s->ptr.cursor.surface == cs) return;
   if (cs)
     {
        cs->cursor = 1;
        evas_object_pass_events_set(cs->obj, 1);
     }
   if (s->ptr.cursor.surface) s->ptr.cursor.surface->cursor = 0;

   if (s->ptr.in)
     {
        const Eina_List *l;
        Eo *dev;
        Ecore_Evas *ee = ecore_evas_ecore_evas_get(s->c->evas);

        EINA_LIST_FOREACH(evas_device_list(s->c->evas, s->dev), l, dev)
          if (evas_device_class_get(dev) == EVAS_DEVICE_CLASS_MOUSE)
            {
               if ((!s->ptr.efl.obj) && (!s->ptr.cursor.surface))
                 seat_ptr_inherit(s, dev);
               ecore_evas_cursor_device_unset(ee, dev);
               if (cs)
                 {
                    cs->role = cs->res;
                    ecore_evas_object_cursor_device_set(ee, dev, cs->obj, EVAS_LAYER_MAX, x, y);
                 }
            }
     }
   if (cs)
     evas_object_smart_member_del(cs->obj);
   s->ptr.cursor.surface = cs;
   s->ptr.cursor.x = x;
   s->ptr.cursor.y = y;
}

static const struct wl_pointer_interface seat_ptr_interface =
{
   seat_ptr_set_cursor,
   resource_destroy
};

static void
seat_ptr_unbind(struct wl_resource *resource)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_client *client = wl_resource_get_client(resource);

   eina_hash_list_remove(s->ptr.resources, &client, resource);
}

static void
seat_ptr_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_resource *res;
   Comp_Surface *cs;
   int x, y;

   res = wl_resource_create(client, &wl_pointer_interface, wl_resource_get_version(resource), id);
   wl_resource_set_implementation(res, &seat_ptr_interface, s, seat_ptr_unbind);
   if (!s->ptr.resources) s->ptr.resources = eina_hash_pointer_new(NULL);
   eina_hash_list_append(s->ptr.resources, &client, res);
   if (!s->ptr.enter) return;
   cs = s->ptr.enter;
   if (wl_resource_get_client(cs->res) != client) return;
   s->ptr.enter = NULL;
   evas_pointer_canvas_xy_get(s->c->evas, &x, &y);
   comp_surface_send_pointer_enter(cs, s, x, y);
}

static const struct wl_touch_interface seat_tch_interface =
{
   resource_destroy
};

static void
seat_tch_unbind(struct wl_resource *resource)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_client *client = wl_resource_get_client(resource);

   eina_hash_list_remove(s->tch.resources, &client, resource);
}

static void
seat_tch_create(struct wl_client *client, struct wl_resource *resource, uint32_t id)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);
   struct wl_resource *res;

   res = wl_resource_create(client, &wl_pointer_interface, wl_resource_get_version(resource), id);
   wl_resource_set_implementation(res, &seat_tch_interface, s, seat_tch_unbind);
   if (!s->tch.resources) s->tch.resources = eina_hash_pointer_new(NULL);
   eina_hash_list_append(s->tch.resources, &client, res);
}

static const struct wl_seat_interface seat_interface =
{
   seat_ptr_create,
   seat_kbd_create,
   seat_tch_create,
   resource_destroy,
};

static void
seat_unbind(struct wl_resource *resource)
{
   Comp_Seat *s = wl_resource_get_user_data(resource);

   s->resources = eina_list_remove(s->resources, resource);
}

static void
seat_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   struct wl_resource *res;
   Comp_Seat *s = data;

   if (!client_allowed_check(s->c, client)) return;
   res = wl_resource_create(client, &wl_seat_interface, version, id);
   s->resources = eina_list_append(s->resources, res);
   if (s->c->active_surface)
     s->active_client = wl_resource_get_client(s->c->active_surface->res);

   wl_resource_set_implementation(res, &seat_interface, s, seat_unbind);

   seat_update_caps(s, res);
   if (s->name && (version >= WL_SEAT_NAME_SINCE_VERSION))
     wl_seat_send_name(res, s->name);
}

static void
seat_resource_hash_free(Eina_Hash *h)
{
   Eina_Iterator *it;
   Eina_List *l;
   void **key;

   if (!h) return;
   while (eina_hash_population(h))
     {
        it = eina_hash_iterator_key_new(h);
        EINA_ITERATOR_FOREACH(it, key)
          {
             struct wl_resource *res;
             l = eina_hash_set(h, key, NULL);

             EINA_LIST_FREE(l, res)
               wl_resource_destroy(res);
             break;
          }
        eina_iterator_free(it);
     }
   eina_hash_free(h);
}

static void
seat_destroy(Comp_Seat *s)
{
   Eina_Stringshare *type;
   seat_resource_hash_free(s->kbd.resources);
   seat_resource_hash_free(s->ptr.resources);
   seat_resource_hash_free(s->tch.resources);
   while (s->resources)
     wl_resource_destroy(eina_list_data_get(s->resources));
   eina_stringshare_del(s->name);
   if (s->kbd.state) xkb_state_unref(s->kbd.state);
   if (s->kbd.keymap) xkb_keymap_unref(s->kbd.keymap);
   if (s->kbd.context) xkb_context_unref(s->kbd.context);
   if (s->kbd.keymap_mem) munmap(s->kbd.keymap_mem, s->kbd.keymap_mem_size);
   if (s->kbd.keymap_fd > -1) close(s->kbd.keymap_fd);
   wl_array_release(&s->kbd.keys);
   efl_unref(s->dev);
   s->c->seats = eina_inlist_remove(s->c->seats, EINA_INLIST_GET(s));
   eina_hash_free(s->data_devices);
   EINA_LIST_FREE(s->drag.x11_types, type) eina_stringshare_del(type);
   while (s->ptr.events)
     {
        Input_Sequence *ev = EINA_INLIST_CONTAINER_GET(s->ptr.events, Input_Sequence);
        s->ptr.events = eina_inlist_remove(s->ptr.events, s->ptr.events);
        free(ev);
     }
   while (s->tch.events)
     {
        Input_Sequence *ev = EINA_INLIST_CONTAINER_GET(s->tch.events, Input_Sequence);
        s->tch.events = eina_inlist_remove(s->tch.events, s->tch.events);
        free(ev);
     }
   wl_global_destroy(s->global);
   free(s);

}

/////////////////////////////////////////////////////////////////

static void
comp_gl_shutdown(Comp *c)
{
   if (c->glapi->evasglUnbindWaylandDisplay)
     c->glapi->evasglUnbindWaylandDisplay(c->gl, c->display);
   evas_gl_surface_destroy(c->gl, c->glsfc);
   evas_gl_context_destroy(c->gl, c->glctx);
   evas_gl_free(c->gl);
   evas_gl_config_free(c->glcfg);
   c->glsfc = NULL;
   c->glctx = NULL;
   c->glcfg = NULL;
   c->gl = NULL;
}

static void
comp_gl_init(Comp *c)
{
   c->glctx = evas_gl_context_create(c->gl, NULL);
   c->glcfg = evas_gl_config_new();
   c->glsfc = evas_gl_surface_create(c->gl, c->glcfg, 1, 1);
   evas_gl_make_current(c->gl, c->glsfc, c->glctx);
   c->glapi = evas_gl_context_api_get(c->gl, c->glctx);
   if ((!c->glapi->evasglBindWaylandDisplay) ||
       (!c->glapi->evasglBindWaylandDisplay(c->gl, c->display)))
     comp_gl_shutdown(c);
}

static void
comp_render_pre(Comp *c, Evas *e EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Comp_Surface *cs;
   Eina_List *l, *ll;

   c->rendering = 1;
   EINA_LIST_FOREACH_SAFE(c->render_queue, l, ll, cs)
     {
        Comp_Buffer *buffer;
//if (cs->subsurface) fprintf(stderr, "RENDER PRE\n");

        comp_surface_buffer_detach(&cs->buffer[1]);
        cs->buffer[1] = cs->buffer[0];
        cs->buffer[0] = NULL;
        cs->render_queue = 0;
        buffer = cs->buffer[1];

        if (!buffer)
          {
             c->render_queue = eina_list_remove_list(c->render_queue, l);
             evas_object_image_pixels_dirty_set(cs->img, 0);
             continue;
          }
        //if (cs->proxies) fprintf(stderr, "RENDER %d\n", wl_resource_get_id(buffer->res));
        cs->post_render_queue = 1;

        evas_object_image_alpha_set(cs->img, comp_surface_is_alpha(cs, buffer));
        evas_object_resize(cs->img, buffer->w, buffer->h);
        evas_object_image_size_set(cs->img, buffer->w, buffer->h);
     }
   c->post_render_queue = c->render_queue;
   c->render_queue = NULL;
}

static void
comp_render_pre_proxied(Evas_Object *o, Evas *e, void *event_info)
{
   Comp_Surface *cs = evas_object_data_get(o, "comp_surface");
   Comp_Buffer *buffer = cs->buffer[!cs->render_queue];

   //fprintf(stderr, "PROXY RENDER_PRE %d\n", buffer ? wl_resource_get_id(buffer->res) : -1);
   if (buffer)
     {
        buffer->renders = eina_list_remove(buffer->renders, e);
        buffer->post_renders = eina_list_append(buffer->post_renders, e);
        evas_object_image_size_set(o, buffer->w, buffer->h);
        evas_object_image_alpha_set(o, comp_surface_is_alpha(cs, buffer));
        evas_object_resize(o, buffer->w, buffer->h);
     }
   else
     evas_object_image_pixels_dirty_set(o, 0);
}

static void
comp_render_post_proxied(Comp_Surface *cs, Evas *e, void *event_info)
{
   Comp_Buffer *buffer;

   buffer = cs->buffer[!cs->render_queue];
   //fprintf(stderr, "PROXY RENDER_POST %d\n", buffer ? wl_resource_get_id(buffer->res) : -1);
   buffer->post_renders = eina_list_remove(buffer->post_renders, e);
   if (buffer->post_renders) return;
   if (cs->buffer[0] || cs->dead) comp_surface_buffer_detach(&cs->buffer[1]);
   comp_surface_buffer_post_render(cs);
}

static void
comp_render_post(Comp *c, Evas *e, void *event_info EINA_UNUSED)
{
   Comp_Surface *cs;
   Comp_Seat *s;

   c->rendering = 0;
   EINA_LIST_FREE(c->post_render_queue, cs)
     {
        //fprintf(stderr, "POST(%d)\n", wl_resource_get_id(cs->res));
        cs->buffer[1]->renders = eina_list_remove(cs->buffer[1]->renders, e);
        if ((!cs->buffer[1]->renders) && (!cs->buffer[1]->post_renders))
          {
             if (cs->buffer[0] || cs->dead) comp_surface_buffer_detach(&cs->buffer[1]);
             comp_surface_buffer_post_render(cs);
          }
        cs->post_render_queue = 0;
        if (!cs->dead) continue;
        if (cs->res) wl_resource_destroy(cs->res);
        else evas_object_del(cs->obj);
     }
   EINA_INLIST_FOREACH(c->seats, s)
     {
        Input_Sequence *ev;
        Eina_Inlist *l;

        EINA_INLIST_FOREACH_SAFE(s->ptr.events, l, ev)
          {
             if (!ev->up_serial) continue;
             if (!ev->pass++) continue;
             s->ptr.events = eina_inlist_remove(s->ptr.events, EINA_INLIST_GET(ev));
             free(ev);
          }
        EINA_INLIST_FOREACH_SAFE(s->tch.events, l, ev)
          {
             if (!ev->up_serial) continue;
             if (!ev->pass++) continue;
             s->tch.events = eina_inlist_remove(s->tch.events, EINA_INLIST_GET(ev));
             free(ev);
          }
     }
}

static Evas_Smart *comp_smart = NULL;

static void
comp_seat_selection_update(Comp_Seat *s)
{
   Ecore_Wl2_Offer *offer;
   Eina_Array *arr;
   unsigned int i;
   Eina_Array_Iterator it;
   char *type;

   if (!s->client_seat) return;
   s->selection_changed = 0;
   if (!s->selection_exists)
     {
        s->client_selection_serial = ecore_wl2_dnd_selection_clear(s->client_seat);
        return;
     }
   if (!s->seat) return; /* x11_fixes_selection_notify() */
   offer = ecore_wl2_dnd_selection_get(s->seat);
   if (!offer) return;
   arr = ecore_wl2_offer_mimes_get(offer);
   {
      char *types[eina_array_count(arr) + 1];

      EINA_ARRAY_ITER_NEXT(arr, i, type, it)
        types[i] = type;
      types[i] = NULL;
      s->client_selection_serial = ecore_wl2_dnd_selection_set(s->client_seat, (const char**)types);
   }
}

static void
seat_drag_offer_update(Comp_Seat *s)
{
   Eina_Array *arr;
   char **types;
   unsigned int i;
   Eina_Array_Iterator it;
   char *type;

    // drag.id = probably double proxied
   if (s->drag.id || (!s->client_offer)) return;
   arr = ecore_wl2_offer_mimes_get(s->client_offer);
   types = alloca(sizeof(void*) * (eina_array_count(arr) + 1));

   EINA_ARRAY_ITER_NEXT(arr, i, type, it)
     types[i] = type;
   types[i] = NULL;

   ecore_wl2_dnd_drag_types_set(s->client_seat, (const char**)types);
   ecore_wl2_dnd_set_actions(s->client_seat);
}

static void
seat_proxy_update(Comp_Seat *s)
{
   Eina_Iterator *it;
   Ecore_Wl2_Input *input;

   if (!s->name) return;
   it = ecore_wl2_display_inputs_get(s->c->client_disp);
   EINA_ITERATOR_FOREACH(it, input)
     {
        const char *name = ecore_wl2_input_name_get(input);

        if (!name) continue;
        if (!eina_streq(s->name, name)) continue;
        s->client_seat = input;
        if (s->selection_changed)
          comp_seat_selection_update(s);
        if (s->client_offer)
          seat_drag_offer_update(s);
        break;
     }
   eina_iterator_free(it);
}

static void
comp_seat_proxy_update(Comp_Seat *s)
{
   Eina_Iterator *it;
   Ecore_Wl2_Input *input;
   Eina_Bool drm;

   if (s->seat) return;
   drm = !ecore_evas_wayland2_window_get(ecore_evas_ecore_evas_get(s->c->evas));
   it = ecore_wl2_display_inputs_get(s->c->parent_disp);
   EINA_ITERATOR_FOREACH(it, input)
     {
        if (drm)
          {
             if (eina_streq(ecore_wl2_input_name_get(input), evas_device_name_get(s->dev)))
               {
                  s->seat = input;
                  break;
               }
          }
        else
          {
             if (ecore_wl2_input_seat_id_get(input) == evas_device_seat_id_get(s->dev))
               {
                  s->seat = input;
                  break;
               }
          }
     }
   eina_iterator_free(it);
}

static void
comp_device_caps_update(Comp_Seat *s)
{
   const Eina_List *l;
   Eo *dev;
   Eina_Bool kbd;

   if (!s) return;
   kbd = s->keyboard;
   s->keyboard = s->touch = s->pointer = 0;
   EINA_LIST_FOREACH(evas_device_list(s->c->evas, s->dev), l, dev)
     {
        Evas_Device_Class clas = evas_device_class_get(dev);
        s->keyboard |= clas == EVAS_DEVICE_CLASS_KEYBOARD;
        s->touch |= clas == EVAS_DEVICE_CLASS_TOUCH;
        s->pointer |= (clas == EVAS_DEVICE_CLASS_MOUSE ||
                     clas == EVAS_DEVICE_CLASS_TOUCH ||
                     clas == EVAS_DEVICE_CLASS_PEN ||
                     clas == EVAS_DEVICE_CLASS_POINTER ||
                     clas == EVAS_DEVICE_CLASS_WAND);
     }
   if (s->keyboard != kbd)
     {
        if (s->keyboard)
          {
             seat_keymap_create(s);
             seat_kbd_repeat_rate_update(s);
          }
        else
          {
             xkb_keymap_unref(s->kbd.keymap);
             s->kbd.keymap = NULL;
          }
        seat_keymap_update(s);
        s->keyboard = !!s->kbd.state;
     }
   seat_update_caps(s, NULL);
}

static void
comp_seats_proxy(Comp *c)
{
   const Eina_List *l, *ll;
   Eo *dev;

   l = evas_device_list(c->evas, NULL);
   EINA_LIST_FOREACH(l, ll, dev)
     {
        Comp_Seat *s;
        Eina_Bool skip = EINA_FALSE;

        if (evas_device_class_get(dev) != EVAS_DEVICE_CLASS_SEAT) continue;
        EINA_INLIST_FOREACH(c->seats, s)
          if (s->dev == dev)
            {
               skip = EINA_TRUE;
               if (c->parent_disp)
                 comp_seat_proxy_update(s);
               break;
            }
        if (skip) continue;

        s = calloc(1, sizeof(Comp_Seat));
        s->c = c;
        s->dev = dev;
        efl_ref(s->dev);
        if (c->parent_disp)
          comp_seat_proxy_update(s);
        s->name = eina_stringshare_ref(evas_device_name_get(dev));
        s->data_devices = eina_hash_pointer_new(NULL);
        c->seats = eina_inlist_append(c->seats, EINA_INLIST_GET(s));
        wl_array_init(&s->kbd.keys);
        if (s->seat)
          {
             Ecore_Wl2_Seat_Capabilities caps = ecore_wl2_input_seat_capabilities_get(s->seat);
             s->keyboard = !!(caps & ECORE_WL2_SEAT_CAPABILITIES_KEYBOARD);
             s->pointer = !!(caps & ECORE_WL2_SEAT_CAPABILITIES_POINTER);
             s->touch = !!(caps & ECORE_WL2_SEAT_CAPABILITIES_TOUCH);
             if (s->keyboard)
               {
                  if (s->seat)
                    {
                       s->kbd.keymap = ecore_wl2_input_keymap_get(s->seat);
                       if (s->kbd.keymap) xkb_keymap_ref(s->kbd.keymap);
                    }
                  else
                    seat_keymap_create(s);
                  seat_kbd_repeat_rate_update(s);
                  seat_keymap_update(s);
                  s->keyboard = !!s->kbd.state;
               }

          }
        else if (!c->parent_disp)
          comp_device_caps_update(s);
        s->global = wl_global_create(c->display, &wl_seat_interface, 4, s, seat_bind);
        if (ecore_wl2_display_sync_is_done(c->client_disp))
          seat_proxy_update(s);
     }
}

static void
comp_wayland_features_init(Comp *c)
{
   if (ecore_wl2_display_dmabuf_get(c->parent_disp))
     linux_dmabuf_setup(c->display, c);
}

static Eina_Bool
comp_sync_done(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Sync_Done *ev)
{
   Eina_List *l;
   Comp *c;

   EINA_LIST_FOREACH(comps, l, c)
     {
        if (c->parent_disp == ev->display)
          {
             c->client_disp = ecore_wl2_display_connect(c->env);
             if (!c->seats)
               comp_seats_proxy(c);
             comp_wayland_features_init(c);
          }
        else if (c->client_disp == ev->display)
          {
             Eina_Iterator *it;
             Ecore_Wl2_Input *input;

             it = ecore_wl2_display_inputs_get(c->client_disp);
             EINA_ITERATOR_FOREACH(it, input)
               {
                  Comp_Seat *s;
                  EINA_INLIST_FOREACH(c->seats, s)
                    {
                       if (!eina_streq(s->name, ecore_wl2_input_name_get(input)))
                         continue;
                       s->client_seat = input;
                       if (s->selection_changed)
                         comp_seat_selection_update(s);
                       if (s->client_offer)
                         seat_drag_offer_update(s);
                       break;
                    }
               }
             eina_iterator_free(it);
          }
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_seat_selection_changed(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Seat_Selection *ev)
{
   Eina_List *l;
   Comp *c;
   Comp_Seat *s;

   EINA_LIST_FOREACH(comps, l, c)
     if (c->parent_disp == ev->display)
       EINA_INLIST_FOREACH(c->seats, s)
         {
            if (s->seat && (ecore_wl2_input_seat_id_get(s->seat) != ev->seat)) continue;
            s->selection_changed = 1;
            s->selection_exists = !!ecore_wl2_dnd_selection_get(s->seat);
            if (!s->focused) continue;
            comp_seat_selection_update(s);
         }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_seat_caps_handler(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Seat_Capabilities *ev)
{
   Eina_List *l;
   Comp *c;
   Comp_Seat *s;

   EINA_LIST_FOREACH(comps, l, c)
     if (c->parent_disp == ev->display)
       EINA_INLIST_FOREACH(c->seats, s)
         {
            if (ecore_wl2_input_seat_id_get(s->seat) != ev->id) continue;
            s->pointer = ev->pointer_enabled;
            if (s->keyboard != ev->keyboard_enabled)
              {
                 if (ev->keyboard_enabled)
                   {
                      s->kbd.keymap = ecore_wl2_input_keymap_get(s->seat);
                      if (s->kbd.keymap) xkb_keymap_ref(s->kbd.keymap);
                      seat_kbd_repeat_rate_update(s);
                   }
                 else
                   {
                      xkb_keymap_unref(s->kbd.keymap);
                      s->kbd.keymap = NULL;
                   }
                 seat_keymap_update(s);
              }
            s->keyboard = !!ev->keyboard_enabled && !!s->kbd.state;
            s->touch = ev->touch_enabled;
            seat_update_caps(s, NULL);
         }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_seat_name_changed(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Seat_Name *ev)
{
   Eina_List *l;
   Comp *c;
   Comp_Seat *s;

   EINA_LIST_FOREACH(comps, l, c)
     if (c->parent_disp == ev->display)
       EINA_INLIST_FOREACH(c->seats, s)
         {
            Eina_List *lll;
            struct wl_resource *res;

            if (ecore_wl2_input_seat_id_get(s->seat) != ev->id) continue;
            eina_stringshare_replace(&s->name, ev->name);
            EINA_LIST_FOREACH(s->resources, lll, res)
              if (wl_resource_get_version(res) >= WL_SEAT_NAME_SINCE_VERSION)
                wl_seat_send_name(res, s->name);
            seat_proxy_update(s);
            break;
         }
     else if (c->client_disp == ev->display)
       EINA_INLIST_FOREACH(c->seats, s)
         if (!s->client_seat) seat_proxy_update(s);
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_seat_keymap_changed(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Seat_Keymap_Changed *ev)
{
   Eina_List *l;
   Comp *c;
   Comp_Seat *s;

   EINA_LIST_FOREACH(comps, l, c)
     if (c->parent_disp == ev->display)
       EINA_INLIST_FOREACH(c->seats, s)
         {
            struct xkb_keymap *keymap;

            if (ecore_wl2_input_seat_id_get(s->seat) != ev->id) continue;

            if (s->kbd.keymap) xkb_map_unref(s->kbd.keymap);
            s->kbd.keymap = NULL;
            keymap = ecore_wl2_input_keymap_get(s->seat);
            if (keymap) s->kbd.keymap = xkb_keymap_ref(keymap);
            seat_keymap_update(s);
            if (s->keyboard != (!!s->kbd.state))
              {
                 s->keyboard = !!s->kbd.state;
                 seat_update_caps(s, NULL);
              }
         }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_seat_keyboard_repeat_changed(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Seat_Keyboard_Repeat_Changed *ev)
{
   Eina_List *l;
   Comp *c;
   Comp_Seat *s;

   EINA_LIST_FOREACH(comps, l, c)
     if (c->parent_disp == ev->display)
       EINA_INLIST_FOREACH(c->seats, s)
         {
            Eina_List *ll, *lll;
            struct wl_resource *res;
            Eina_Iterator *it;

            if (ecore_wl2_input_seat_id_get(s->seat) != ev->id) continue;

            seat_kbd_repeat_rate_update(s);
            it = eina_hash_iterator_data_new(s->kbd.resources);
            EINA_ITERATOR_FOREACH(it, ll)
              EINA_LIST_FOREACH(ll, lll, res)
                if (wl_resource_get_version(res) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION)
                  wl_keyboard_send_repeat_info(res, s->kbd.repeat_rate, s->kbd.repeat_delay);
            eina_iterator_free(it);
         }
   return ECORE_CALLBACK_RENEW;
}

static void
comp_seat_key_update(Comp_Seat *s, xkb_keycode_t key, int dir, unsigned int timestamp)
{
   Eina_List *l, *ll;
   struct wl_resource *res;
   uint32_t serial = wl_display_next_serial(s->c->display);
   uint32_t xkb[] = { XKB_KEY_DOWN, XKB_KEY_UP };
   uint32_t wl[] = { WL_KEYBOARD_KEY_STATE_PRESSED, WL_KEYBOARD_KEY_STATE_RELEASED };
   Eina_Bool mods = EINA_FALSE;

   if (xkb_state_update_key(s->kbd.state, key + 8, xkb[dir]))
     mods = seat_mods_update(s);
   if (!s->focused) return;
   l = seat_kbd_active_resources_get(s);

   EINA_LIST_FOREACH(l, ll, res)
     {
        wl_keyboard_send_key(res, serial, timestamp, key, wl[dir]);
        if (mods) comp_seat_send_modifiers(s, res, serial);
     }
}

static Eina_Bool
comp_key_down(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Key *ev)
{
   uint32_t keycode;
   Comp *c;
   Eina_List *l;
   Comp_Seat *s;
   const Eo *dev = evas_device_parent_get(ev->dev);

   keycode = (ev->keycode - 8);
   EINA_LIST_FOREACH(comps, l, c)
     EINA_INLIST_FOREACH(c->seats, s)
       {
          uint32_t *end, *k;

          if (c->parent_disp && (dev != s->dev)) continue;
          end = (uint32_t*)s->kbd.keys.data + (s->kbd.keys.size / sizeof(uint32_t));

          for (k = s->kbd.keys.data; k < end; k++)
            if (*k == keycode) return ECORE_CALLBACK_RENEW;

          s->kbd.keys.size = (char*)end - (char*)s->kbd.keys.data;
          k = wl_array_add(&s->kbd.keys, sizeof(uint32_t));
          *k = keycode;
          comp_seat_key_update(s, keycode, 0, ev->timestamp);
       }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_key_up(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Event_Key *ev)
{
   uint32_t keycode;
   Comp *c;
   Eina_List *l;
   Comp_Seat *s;
   const Eo *dev = evas_device_parent_get(ev->dev);

   keycode = (ev->keycode - 8);
   EINA_LIST_FOREACH(comps, l, c)
     EINA_INLIST_FOREACH(c->seats, s)
       {
          uint32_t *end, *k;

          if (c->parent_disp && (dev != s->dev)) continue;
          end = (uint32_t*)s->kbd.keys.data + (s->kbd.keys.size / sizeof(uint32_t));

          for (k = s->kbd.keys.data; k < end; k++)
            if (*k == keycode)
              {
                 *k = end[-1];
                 s->kbd.keys.size = (char*)end - (char*)s->kbd.keys.data - 1;
                 break;
              }

          comp_seat_key_update(s, keycode, 1, ev->timestamp);
       }
   return ECORE_CALLBACK_RENEW;
}

static void
comp_device_add(void *d, const Efl_Event *ev)
{
   Comp *c = d;

   if (efl_input_device_type_get(ev->info) == EFL_INPUT_DEVICE_TYPE_SEAT)
     comp_seats_proxy(c);
   else if (!c->parent_disp)
     comp_device_caps_update(comp_seat_find(c, ev->info));
}

static void
comp_device_del(void *d, const Efl_Event *ev)
{
   Comp *c = d;
   Eo *seat;
   Eina_Inlist *l;
   Comp_Seat *s;

   if (efl_input_device_type_get(ev->info) == EFL_INPUT_DEVICE_TYPE_SEAT)
     seat = ev->info;
   else
     seat = efl_input_device_seat_get(ev->info);
   EINA_INLIST_FOREACH_SAFE(c->seats, l, s)
     if (seat == s->dev)
       {
          if (ev->info == seat)
            seat_destroy(s);
          else if (!c->parent_disp)
            comp_device_caps_update(s);
       }
}

static Eina_Bool
comp_data_device_dnd_leave(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Dnd_Leave *ev)
{
   Comp *c;
   Comp_Seat *s;
   Eina_List *l;
   EINA_LIST_FOREACH(comps, l, c)
     {
        if (ev->display != c->parent_disp) continue;
        EINA_INLIST_FOREACH(c->seats, s)
          {
             if (ecore_wl2_input_seat_id_get(s->seat) != ev->seat) continue;
             if (s->drag.id) continue; // probably double proxied
             if (s->drag.source)
               wl_data_source_send_cancelled(s->drag.source->res);
             if (s->client_seat && s->drag.res)
               {
                  ecore_wl2_dnd_drag_end(s->client_seat);
                  s->client_offer = NULL;
               }
             if (s->drag.proxy_win)
               {
                  if (s->seat)
                    ecore_wl2_dnd_drag_end(s->seat);
                  ecore_evas_free(s->drag.proxy_win);
               }
             seat_drag_end(s);
             s->drag.source = NULL;
          }
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_data_device_dnd_end(void *d, int t, Ecore_Wl2_Event_Data_Source_End *ev)
{
   Comp *c;
   Comp_Seat *s;
   Eina_List *l;

   EINA_LIST_FOREACH(comps, l, c)
     {
        if (ev->display != c->parent_disp) continue;
        EINA_INLIST_FOREACH(c->seats, s)
          {
             if (ecore_wl2_input_seat_id_get(s->seat) != ev->seat) continue;
             if (!s->drag.source) continue;
             if (s->drag.source->proxy_serial != ev->serial) continue;
             if (ev->cancelled)
               wl_data_source_send_cancelled(s->drag.source->res);
             else
               data_source_notify_finish(s->drag.source);
             if (s->drag.proxy_win)
               {
                  if (s->seat)
                    ecore_wl2_dnd_drag_end(s->seat);
                  ecore_evas_free(s->drag.proxy_win);
               }
             seat_drag_end(s);
             s->drag.source = NULL;
             s->client_offer = NULL;
          }
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_data_device_dnd_enter(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Dnd_Enter *ev)
{
   Comp *c;
   Comp_Seat *s;
   Eina_List *l;

   EINA_LIST_FOREACH(comps, l, c)
     {
        if (ev->display != c->parent_disp) continue;
        EINA_INLIST_FOREACH(c->seats, s)
          {
             if (ecore_wl2_input_seat_id_get(s->seat) != ev->seat) continue;
             s->client_offer = ev->offer;
             seat_drag_offer_update(s);
          }
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_data_device_dnd_motion(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Dnd_Motion *ev)
{
   Comp *c;
   Comp_Seat *s;
   Eina_List *l;
   int x, y, w, h, ex, ey;
   Eina_Bool found = EINA_FALSE;

   EINA_LIST_FOREACH(comps, l, c)
     {
        int fx, fy;

        if (ev->display != c->parent_disp) continue;
        evas_output_framespace_get(c->evas, &fx, &fy, NULL, NULL);
        ex = ev->x - fx;
        ey = ev->y - fy;
        evas_object_geometry_get(c->obj, &x, &y, &w, &h);
        if (!COORDS_INSIDE(ex, ey, x, y, w, h)) continue;
        found = EINA_TRUE;
        break;
     }
   if (!found) return ECORE_CALLBACK_RENEW;

   EINA_INLIST_FOREACH(c->seats, s)
     if (ecore_wl2_input_seat_id_get(s->seat) == ev->seat)
       {
          dnd_motion(s, ex, ey);
          break;
       }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
comp_data_device_dnd_drop(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Wl2_Event_Dnd_Drop *ev)
{
   Comp *c;
   Comp_Seat *s;
   Eina_List *l;

   EINA_LIST_FOREACH(comps, l, c)
     {
        if (c->parent_disp != ev->display) continue;
        EINA_INLIST_FOREACH(c->seats, s)
          if (ecore_wl2_input_seat_id_get(s->seat) == ev->seat)
            {
               if (s->drag.enter)
                 {
                    drag_grab_button(s, 0, s->drag.id, WL_POINTER_BUTTON_STATE_RELEASED);
                    if (s->drag.proxy_win)
                      {
                         ecore_wl2_dnd_drag_end(s->seat);
                         ecore_evas_free(s->drag.proxy_win);
                      }
                    return ECORE_CALLBACK_RENEW;
                 }
            }
     }
   return ECORE_CALLBACK_RENEW;
}

static void
comp_handlers_init(void)
{
   Ecore_Event_Handler *h;

   h = ecore_event_handler_add(ECORE_WL2_EVENT_SEAT_CAPABILITIES_CHANGED,
     (Ecore_Event_Handler_Cb)comp_seat_caps_handler, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_SEAT_KEYMAP_CHANGED,
     (Ecore_Event_Handler_Cb)comp_seat_keymap_changed, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_SEAT_NAME_CHANGED,
     (Ecore_Event_Handler_Cb)comp_seat_name_changed, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_SYNC_DONE,
            (Ecore_Event_Handler_Cb)comp_sync_done, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_SEAT_KEYBOARD_REPEAT_CHANGED,
            (Ecore_Event_Handler_Cb)comp_seat_keyboard_repeat_changed, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_SEAT_SELECTION,
            (Ecore_Event_Handler_Cb)comp_seat_selection_changed, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_DND_MOTION,
            (Ecore_Event_Handler_Cb)comp_data_device_dnd_motion, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_DND_ENTER,
            (Ecore_Event_Handler_Cb)comp_data_device_dnd_enter, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_DND_LEAVE,
            (Ecore_Event_Handler_Cb)comp_data_device_dnd_leave, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_END,
            (Ecore_Event_Handler_Cb)comp_data_device_dnd_end, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_WL2_EVENT_DND_DROP,
            (Ecore_Event_Handler_Cb)comp_data_device_dnd_drop, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
            (Ecore_Event_Handler_Cb)comp_key_down, NULL);
   handlers = eina_list_append(handlers, h);
   h = ecore_event_handler_add(ECORE_EVENT_KEY_UP,
            (Ecore_Event_Handler_Cb)comp_key_up, NULL);
   handlers = eina_list_append(handlers, h);
}

static void
comp_focus_in(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Comp *c = data;
   //Efl_Input_Focus_Data *ev = event_info;
   Comp_Seat *s;

   EINA_INLIST_FOREACH(c->seats, s)
     {
        Eina_List *l, *ll;
        struct wl_resource *res;
        uint32_t serial;

        //if (ev->device != s->dev) continue;
        if (evas_seat_focus_get(c->evas, s->dev) != c->obj) continue;
        s->focused = 1;
        if (s->selection_changed)
          comp_seat_selection_update(s);
        if (!s->kbd.keymap) continue;
        if (!seat_mods_update(s)) continue;
        l = seat_kbd_active_resources_get(s);
        if (!l) continue;
        serial = wl_display_next_serial(s->c->display);
        EINA_LIST_FOREACH(l, ll, res)
          comp_seat_send_modifiers(s, res, serial);
     }
}

static void
comp_focus_out(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Comp *c = data;
   //Efl_Input_Focus_Data *ev = event_info;
   Comp_Seat *s;

   EINA_INLIST_FOREACH(c->seats, s)
     {
        //if (ev->device != s->dev) continue;
        if (evas_seat_focus_get(c->evas, s->dev) != c->obj)
          s->focused = 0;
     }
}

static void
comp_mouse_in(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
   Comp *c = data;
   Evas_Event_Mouse_In *ev = event_info;
   Comp_Seat *s;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   s = comp_seat_find(c, ev->dev);
   if (s->drag.proxy_win)
     {
        ecore_evas_free(s->drag.proxy_win);
#ifdef HAVE_ECORE_X
        if (ecore_x_display_get())
          {
             ecore_x_dnd_abort(ecore_evas_window_get(ecore_evas_ecore_evas_get(s->c->evas)));
             s->drag.x11_owner = 0;
          }
#endif
        if (s->ptr.cursor.surface)
          evas_object_show(s->ptr.cursor.surface->obj);
     }
   if (s->drag.surface)
     evas_object_show(s->drag.surface->obj);
   s->ptr.in = 1;
   seat_ptr_inherit(s, ev->dev);
   ecore_evas_cursor_device_unset(ecore_evas_ecore_evas_get(e), ev->dev);
   if (s->ptr.efl.obj) evas_object_hide(s->ptr.efl.obj);
   if (!s->ptr.cursor.surface) return;
   s->ptr.cursor.surface->role = s->ptr.cursor.surface->res;
   ecore_evas_object_cursor_device_set(ecore_evas_ecore_evas_get(e), ev->dev,
     s->ptr.cursor.surface->obj, EVAS_LAYER_MAX, s->ptr.cursor.x, s->ptr.cursor.y);
}

static void
comp_multi_move(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Comp *c = data;
   Evas_Event_Multi_Move *ev = event_info;
   Comp_Seat *s;
   int w, h;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   s = comp_seat_find(c, ev->dev);
   s->tch.pos.x = ev->cur.canvas.x;
   s->tch.pos.y = ev->cur.canvas.y;
   if ((!s->drag.tch) || (!s->drag.surface)) return;
   evas_object_geometry_get(s->drag.surface->obj, NULL, NULL, &w, &h);
   evas_object_move(s->drag.surface->obj, ev->cur.canvas.x, ev->cur.canvas.y);
}

static void
comp_mouse_move(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Comp *c = data;
   Evas_Event_Mouse_Move *ev = event_info;
   Comp_Seat *s;
   int w, h;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   s = comp_seat_find(c, ev->dev);
   s->ptr.pos.x = ev->cur.canvas.x;
   s->ptr.pos.y = ev->cur.canvas.y;
   if (s->drag.tch || (!s->drag.surface)) return;
   evas_object_geometry_get(s->drag.surface->obj, NULL, NULL, &w, &h);
   evas_object_move(s->drag.surface->obj, ev->cur.canvas.x, ev->cur.canvas.y);
}

static void
comp_mouse_out(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info)
{
   Comp *c = data;
   Evas_Event_Mouse_Out *ev = event_info;
   Comp_Seat *s;
   Eina_List *l;
   const char **types, *type;
   unsigned int i = 0;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   s = comp_seat_find(c, ev->dev);
   s->ptr.in = 0;
   ecore_evas_cursor_device_unset(ecore_evas_ecore_evas_get(e), ev->dev);
   if (s->ptr.efl.obj)
     {
        ecore_evas_object_cursor_device_set(ecore_evas_ecore_evas_get(e), ev->dev,
          s->ptr.efl.obj, s->ptr.efl.layer, s->ptr.efl.x, s->ptr.efl.y);
        seat_ptr_del(s, NULL, NULL, NULL);
     }
   if (s->ptr.cursor.surface)
     {
        s->ptr.cursor.surface->role = NULL;
        evas_object_hide(s->ptr.cursor.surface->obj);
     }
   if ((!s->drag.res) || (!s->drag.source) || s->drag.source->proxy) return;
   if (s->drag.enter) comp_surface_send_data_device_leave(s->drag.enter, s);
   if (s->drag.surface)
     seat_drag_proxy_win_add(s);
   types = alloca(sizeof(void*) * (eina_list_count(s->drag.source->mime_types) + 1));
   EINA_LIST_FOREACH(s->drag.source->mime_types, l, type)
     types[i++] = type;
   types[i] = NULL;
   if (s->c->parent_disp)
     {
        Ecore_Wl2_Window *win = NULL;

        if (s->drag.proxy_win)
          win = ecore_evas_wayland2_window_get(s->drag.proxy_win);
        ecore_wl2_dnd_drag_types_set(s->seat, types);
        s->drag.source->proxy_serial = ecore_wl2_dnd_drag_start(s->seat,
          ecore_evas_wayland2_window_get(ecore_evas_ecore_evas_get(s->c->evas)),
          win);
        s->drag.source->proxy_send_handler =
          ecore_event_handler_add(ECORE_WL2_EVENT_DATA_SOURCE_SEND,
                           data_device_proxy_send_send, s->drag.source);
     }
#ifdef HAVE_ECORE_X
   else if (ecore_x_display_get())
     {
        Ecore_Window win = ecore_evas_window_get(ecore_evas_ecore_evas_get(s->c->evas));
        Ecore_Window xwin = ecore_evas_window_get(s->drag.proxy_win);
        Ecore_X_Atom actions[] = { ECORE_X_DND_ACTION_MOVE, ECORE_X_DND_ACTION_COPY };

        if (s->drag.proxy_win && s->drag.surface)
          {
             int x, y, ex, ey;
             ecore_evas_geometry_get(ecore_evas_ecore_evas_get(s->c->evas), &ex, &ey, NULL, NULL);
             evas_object_geometry_get(s->drag.surface->obj, &x, &y, NULL, NULL);
             x += ex, y += ey;
             ecore_x_window_ignore_set(xwin, 1);
             ecore_evas_move(s->drag.proxy_win, x, y);
          }
        s->drag.x11_owner = win;

        ecore_x_dnd_types_set(win, types, i);
        ecore_x_dnd_actions_set(win, actions, EINA_C_ARRAY_LENGTH(actions));
        ecore_x_dnd_self_begin(win, NULL, 0);
        ecore_x_dnd_callback_pos_update_set(x11_dnd_move, s->drag.proxy_win);
     }
#endif
}

EFL_CALLBACKS_ARRAY_DEFINE(comp_device_cbs,
  { EFL_CANVAS_EVENT_DEVICE_ADDED, comp_device_add },
  { EFL_CANVAS_EVENT_DEVICE_REMOVED, comp_device_del });

static void
hints_set_aspect(struct wl_client *client, struct wl_resource *resource, struct wl_resource *surface, uint32_t width, uint32_t height, uint32_t aspect)
{
   Comp_Surface *cs = wl_resource_get_user_data(surface);
   evas_object_size_hint_aspect_set(cs->obj, aspect, width, height);
   if (cs == cs->c->active_surface)
     shell_surface_aspect_update(cs);
}

static const struct efl_hints_interface hints_interface =
{
   hints_set_aspect,
};

static void
efl_hints_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
   struct wl_resource *res;

   if (!client_allowed_check(data, client)) return;
   res = wl_resource_create(client, &efl_hints_interface, version, id);
   wl_resource_set_implementation(res, &hints_interface, data, NULL);
}



static void
comp_smart_add(Evas_Object *obj)
{
   Comp *c;
   char *env, *dbg = NULL;

   c = calloc(1, sizeof(Comp));
   c->wayland_time_base = ecore_loop_time_get();
   c->obj = obj;
   evas_object_smart_data_set(obj, c);
   env = getenv("WAYLAND_DISPLAY");
   if (env) env = strdup(env);

   if (getenv("EFL_WL_DEBUG"))
     {
        dbg = eina_strdup(getenv("WAYLAND_DEBUG"));
        setenv("WAYLAND_DEBUG", "1", 1);
     }
   c->disp = ecore_wl2_display_create(NULL);
   if (getenv("EFL_WL_DEBUG"))
     {
        if (dbg) setenv("WAYLAND_DEBUG", dbg, 1);
        else unsetenv("WAYLAND_DEBUG");
        free(dbg);
     }
   c->env = eina_strdup(getenv("WAYLAND_DISPLAY"));
   if (env) setenv("WAYLAND_DISPLAY", env, 1);
   else unsetenv("WAYLAND_DISPLAY");
   c->display = ecore_wl2_display_get(c->disp);
   c->client_surfaces = eina_hash_pointer_new(NULL);
   c->evas = evas_object_evas_get(obj);
   c->clip = evas_object_rectangle_add(c->evas);
   evas_object_smart_member_add(c->clip, obj);
   c->events = evas_object_rectangle_add(c->evas);
   evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_IN, comp_mouse_in, c);
   evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_MOVE, comp_mouse_move, c);
   evas_object_event_callback_add(obj, EVAS_CALLBACK_MULTI_MOVE, comp_multi_move, c);
   evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_OUT, comp_mouse_out, c);
   evas_object_color_set(c->events, 0, 0, 0, 0);
   evas_object_smart_member_add(c->events, obj);
   evas_object_show(c->events);
   evas_object_lower(c->events);

   wl_global_create(c->display, &wl_compositor_interface, 4, c, comp_bind);
   wl_global_create(c->display, &wl_subcompositor_interface, 1, c, subcomp_bind);
   wl_global_create(c->display, &wl_output_interface, 2, c, output_bind);
   wl_global_create(c->display, &zxdg_shell_v6_interface, 1, c, shell_bind);
   wl_global_create(c->display, &wl_data_device_manager_interface, 3, c, data_device_manager_bind);
   wl_global_create(c->display, &efl_hints_interface, 1, c, efl_hints_bind);
   wl_display_init_shm(c->display);

   if (env)
     {
        c->parent_disp = ecore_wl2_display_connect(env);
        if (ecore_wl2_display_sync_is_done(c->parent_disp))
          {
             c->client_disp = ecore_wl2_display_connect(c->env);
             comp_seats_proxy(c);
             comp_wayland_features_init(c);
          }
     }
   else
     {
        c->client_disp = ecore_wl2_display_connect(c->env);
        comp_seats_proxy(c);
#ifdef HAVE_ECORE_X
        if (ecore_x_display_get())
          {
             // if proxiedallowed
             ecore_x_dnd_aware_set(ecore_evas_window_get(ecore_evas_ecore_evas_get(c->evas)), EINA_TRUE);
             if (!comps) x11_init();
          }
#endif
     }

   c->gl = evas_gl_new(c->evas);
   if (c->gl) comp_gl_init(c);
   evas_event_callback_add(c->evas, EVAS_CALLBACK_RENDER_PRE, (Evas_Event_Cb)comp_render_pre, c);
   evas_event_callback_add(c->evas, EVAS_CALLBACK_RENDER_POST, (Evas_Event_Cb)comp_render_post, c);
   efl_event_callback_array_add(c->evas, comp_device_cbs(), c);

   evas_object_event_callback_add(c->obj, EVAS_CALLBACK_FOCUS_IN, comp_focus_in, c);
   evas_object_event_callback_add(c->obj, EVAS_CALLBACK_FOCUS_OUT, comp_focus_out, c);

   if (!comps) comp_handlers_init();
   comps = eina_list_append(comps, c);
   free(env);
}

static void
comp_smart_del(Evas_Object *obj)
{
   Comp *c = evas_object_smart_data_get(obj);

   evas_object_del(c->clip);
   evas_object_del(c->events);
   free(c->env);
   if (c->gl) comp_gl_shutdown(c);
   while (c->shells)
     {
        Shell_Data *sd = EINA_INLIST_CONTAINER_GET(c->shells, Shell_Data);
        wl_resource_destroy(sd->res);
     }
   while (c->seats)
     {
        Comp_Seat *s = EINA_INLIST_CONTAINER_GET(c->seats, Comp_Seat);
        seat_destroy(s);
     }
   while (c->surfaces)
     {
        Comp_Surface *cs = EINA_INLIST_CONTAINER_GET(c->surfaces, Comp_Surface);
        evas_object_del(cs->obj);
     }
   eina_hash_free(c->client_surfaces);
   if (c->parent_disp) ecore_wl2_display_disconnect(c->parent_disp);
   ecore_wl2_display_destroy(c->disp);
   ecore_wl2_display_disconnect(c->client_disp);

   evas_event_callback_del_full(c->evas, EVAS_CALLBACK_RENDER_PRE, (Evas_Event_Cb)comp_render_pre, c);
   evas_event_callback_del_full(c->evas, EVAS_CALLBACK_RENDER_POST, (Evas_Event_Cb)comp_render_post, c);
   efl_event_callback_array_del(c->evas, comp_device_cbs(), c);
   ecore_event_handler_del(c->exe_handler);
   eina_hash_free(c->exes);
   comps = eina_list_remove(comps, c);
   free(c);
   if (!comps)
     {
        void *h;
        EINA_LIST_FREE(handlers, h)
          ecore_event_handler_del(h);
#ifdef HAVE_ECORE_X
        x11_shutdown();
#endif
     }
   ecore_wl2_shutdown();
}

static void
comp_smart_move(Evas_Object *obj, int x, int y)
{
   Comp_Surface *cs;
   Comp *c = evas_object_smart_data_get(obj);
   int dx, dy;
   evas_object_geometry_get(obj, &dx, &dy, NULL, NULL);
   dx = x - dx;
   dy = y - dy;
   evas_object_move(c->clip, x, y);
   evas_object_move(c->events, x, y);
   EINA_INLIST_FOREACH(c->surfaces, cs)
     {
        int cx, cy;
        evas_object_geometry_get(cs->obj, &cx, &cy, NULL, NULL);
        evas_object_move(cs->obj, cx + dx, cy + dy);
     }
}

static void
comp_smart_resize(Evas_Object *obj, int w, int h)
{
   Comp *c = evas_object_smart_data_get(obj);
   Eina_List *l;
   Comp_Surface *cs;
   struct wl_resource *res;

   evas_object_resize(c->clip, w, h);
   evas_object_resize(c->events, w, h);
   EINA_LIST_FOREACH(c->output_resources, l, res)
     output_resize(c, res);
   //fprintf(stderr, "COMP %dx%d\n", w, h);
   EINA_INLIST_FOREACH(c->surfaces, cs)
     if (cs->shell.surface && cs->role && (!cs->extracted))
       shell_surface_send_configure(cs);
}

static void
comp_smart_show(Evas_Object *obj)
{
   Comp *c = evas_object_smart_data_get(obj);
   evas_object_show(c->clip);
}

static void
comp_smart_hide(Evas_Object *obj)
{
   Comp *c = evas_object_smart_data_get(obj);
   evas_object_hide(c->clip);
}

static void
comp_smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
{
   Comp *c = evas_object_smart_data_get(obj);
   evas_object_color_set(c->clip, r, g, b, a);
}

static void
comp_smart_clip_set(Evas_Object *obj, Evas_Object *clip)
{
   Comp *c = evas_object_smart_data_get(obj);
   evas_object_clip_set(c->clip, clip);
}

static void
comp_smart_clip_unset(Evas_Object *obj)
{
   Comp *c = evas_object_smart_data_get(obj);
   evas_object_clip_unset(c->clip);
}

static void
comp_smart_member_add(Evas_Object *obj, Evas_Object *child)
{
   Comp *c = evas_object_smart_data_get(obj);
   if (child != c->clip) evas_object_clip_set(child, c->clip);
}

static void
comp_smart_member_del(Evas_Object *obj, Evas_Object *child)
{
   Comp *c = evas_object_smart_data_get(obj);
   if (child != c->clip) evas_object_clip_unset(child);
}

static void
comp_smart_init(void)
{
   if (comp_smart) return;
   {
      static const Evas_Smart_Class sc =
      {
         "comp",
         EVAS_SMART_CLASS_VERSION,
         comp_smart_add,
         comp_smart_del,
         comp_smart_move,
         comp_smart_resize,
         comp_smart_show,
         comp_smart_hide,
         comp_smart_color_set,
         comp_smart_clip_set,
         comp_smart_clip_unset,
         NULL,
         comp_smart_member_add,
         comp_smart_member_del,

         NULL,
         NULL,
         NULL,
         NULL
      };
      comp_smart = evas_smart_class_new(&sc);
   }
}

static Eina_Bool
exe_event_del(void *data, int t EINA_UNUSED, Ecore_Exe_Event_Del *ev)
{
   Comp *c = data;
   int32_t pid = ev->pid;

   if (!eina_streq(ecore_exe_tag_get(ev->exe), "__efl_wl")) return ECORE_CALLBACK_RENEW;

   eina_hash_del_by_key(c->exes, &pid);
   return ECORE_CALLBACK_RENEW;
}

# ifdef __GNUC__
#  if __GNUC__ >= 4
__attribute__ ((visibility("hidden")))
# endif
#endif
Eina_Bool
comp_dmabuf_test(struct linux_dmabuf_buffer *dmabuf)
{
   Evas_Native_Surface ns;
   Evas_Object *test;
   int size;
   void *data;
   Comp *c = dmabuf->compositor;

   if (c->gl)
     {
        Eina_Bool ret;
        ns.type = EVAS_NATIVE_SURFACE_WL_DMABUF;
        ns.version = EVAS_NATIVE_SURFACE_VERSION;
        ns.data.wl_dmabuf.attr = &dmabuf->attributes;
        ns.data.wl_dmabuf.resource = NULL;
        test = evas_object_image_add(c->evas);
        evas_object_image_native_surface_set(test, &ns);
        ret = evas_object_image_load_error_get(test) == EVAS_LOAD_ERROR_NONE;
        evas_object_del(test);
        if (!ns.data.wl_dmabuf.attr) return EINA_FALSE;
        return ret;
     }

   /* TODO: Software rendering for multi-plane formats */
   if (dmabuf->attributes.n_planes != 1) return EINA_FALSE;
   if (dmabuf->attributes.format != DRM_FORMAT_ARGB8888 &&
       dmabuf->attributes.format != DRM_FORMAT_XRGB8888) return EINA_FALSE;

   /* This is only legit for ARGB8888 */
   size = dmabuf->attributes.height * dmabuf->attributes.stride[0];
   data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf->attributes.fd[0], 0);
   if (data == MAP_FAILED) return EINA_FALSE;
   munmap(data, size);

   return EINA_TRUE;
}

void
comp_dmabuf_formats_query(void *compositor EINA_UNUSED, int **formats EINA_UNUSED, int *num_formats)
{
   *num_formats = 0;
}

void
comp_dmabuf_modifiers_query(void *compositor EINA_UNUSED, int format EINA_UNUSED, uint64_t **modifiers EINA_UNUSED, int *num_modifiers)
{
   *num_modifiers = 0;
}

Evas_Object *
efl_wl_add(Evas *e)
{
   comp_smart_init();
   EINA_SAFETY_ON_TRUE_RETURN_VAL(!ecore_wl2_init(), NULL);
   return evas_object_smart_add(e, comp_smart);
}

Ecore_Exe *
efl_wl_run(Evas_Object *obj, const char *cmd)
{
   char *env, *disp, *gl = NULL;
   Comp *c;
   Ecore_Exe *exe;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   if (!c->exes)
     c->exes = eina_hash_int32_new(NULL);
   if (!c->exe_handler)
     c->exe_handler =
       ecore_event_handler_add(ECORE_EXE_EVENT_DEL, (Ecore_Event_Handler_Cb)exe_event_del, c);
   disp = getenv("DISPLAY");
   if (disp) disp = strdup(disp);
   unsetenv("DISPLAY");
   env = getenv("WAYLAND_DISPLAY");
   if (env) env = strdup(env);
   setenv("WAYLAND_DISPLAY", c->env, 1);
   if (c->gl)
     {
        gl = getenv("ELM_ACCEL");
        if (gl) gl = strdup(gl);
        setenv("ELM_ACCEL", "gl", 1);
     }
   exe = ecore_exe_run(cmd, c);
   if (disp) setenv("DISPLAY", disp, 1);
   if (env) setenv("WAYLAND_DISPLAY", env, 1);
   else unsetenv("WAYLAND_DISPLAY");
   if (c->gl)
     {
        if (gl) setenv("ELM_ACCEL", gl, 1);
        else unsetenv("ELM_ACCEL");
        free(gl);
     }
   free(env);
   free(disp);
   if (exe)
     {
        int32_t pid = ecore_exe_pid_get(exe);
        ecore_exe_tag_set(exe, "__efl_wl");
        eina_hash_add(c->exes, &pid, exe);
     }
   return exe;
}

Eina_Bool
efl_wl_next(Evas_Object *obj)
{
   Comp *c;
   Comp_Surface *cs;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   if (c->surfaces_count < 2) return EINA_FALSE;
   EINA_INLIST_REVERSE_FOREACH(c->surfaces, cs)
     {
        if (cs->shell.activated) continue;
        if ((!cs->role) || (!cs->shell.surface) || cs->dead) continue;
        cs->shell.activated = 1;
        shell_surface_send_configure(cs);
        return EINA_TRUE;
     }

   return EINA_FALSE;
}

Eina_Bool
efl_wl_prev(Evas_Object *obj)
{
   Comp *c;
   Comp_Surface *cs;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   if (c->surfaces_count < 2) return EINA_FALSE;
   EINA_INLIST_FOREACH(c->surfaces, cs)
     {
        if (cs->shell.activated) continue;
        if ((!cs->role) || (!cs->shell.surface) || cs->dead) continue;
        cs->shell.activated = 1;
        shell_surface_send_configure(cs);
        return EINA_TRUE;
     }

   return EINA_FALSE;
}

void
efl_wl_rotate(Evas_Object *obj, Efl_Wl_Rotation rot, Eina_Bool rtl)
{
   Comp *c;
   Eina_List *l;
   struct wl_resource *res;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   c->rtl = !!rtl;
   c->rotation = rot;
   EINA_LIST_FOREACH(c->output_resources, l, res)
     output_resize(c, res);
}

void
efl_wl_scale_set(Evas_Object *obj, double scale)
{
   Comp *c;
   Eina_List *l;
   struct wl_resource *res;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   c->scale = scale;

   EINA_LIST_FOREACH(c->output_resources, l, res)
     if (wl_resource_get_version(res) >= WL_OUTPUT_SCALE_SINCE_VERSION)
       wl_output_send_scale(res, lround(c->scale));
}

void
efl_wl_aspect_set(Evas_Object *obj, Eina_Bool set)
{
   Comp *c;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   if (c->aspect == (!!set)) return;
   c->aspect = !!set;
   if (c->aspect)
     shell_surface_aspect_update(c->active_surface);
   else
     evas_object_size_hint_aspect_set(obj, EVAS_ASPECT_CONTROL_NONE, 0, 0);
}

void
efl_wl_minmax_set(Evas_Object *obj, Eina_Bool set)
{
   Comp *c;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   c = evas_object_smart_data_get(obj);
   if (c->minmax == (!!set)) return;
   c->minmax = !!set;
   if (c->minmax)
     shell_surface_minmax_update(c->active_surface);
   else
     {
        evas_object_size_hint_min_set(obj, 0, 0);
        evas_object_size_hint_max_set(obj, -1, -1);
     }
}

EAPI void *
efl_wl_global_add(Evas_Object *obj, const void *interface, uint32_t version, void *data, void *bind_cb)
{
   Comp *c;

   if (!eina_streq(evas_object_type_get(obj), "comp")) abort();
   EINA_SAFETY_ON_NULL_RETURN_VAL(interface, NULL);
   c = evas_object_smart_data_get(obj);
   return wl_global_create(c->display, interface, version, data, bind_cb);
}

static void
extracted_focus(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Comp_Surface *cs = data;

   if (cs->dead) return;
   if (!cs->shell.popup)
     {
        cs->shell.activated = 1;
        shell_surface_send_configure(data);
     }
   evas_object_focus_set(cs->c->obj, 1);
}

static void
extracted_unfocus(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Comp_Surface *cs = data;
   Evas_Object *focus;

   if (cs->dead) return;
   focus = evas_focus_get(cs->c->evas);
   if ((!focus) || (focus == cs->c->obj)) return;
   cs->shell.activated = 0;
   shell_surface_send_configure(data);
}

static void
extracted_changed(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Comp_Surface *cs = data;

   if (cs->dead) return;
   shell_surface_send_configure(data);
}

EAPI Eina_Bool
efl_wl_surface_extract(Evas_Object *surface)
{
   Comp_Surface *cs;
   if (!eina_streq(evas_object_type_get(surface), "comp_surface")) abort();
   cs = evas_object_smart_data_get(surface);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(cs->extracted, EINA_FALSE);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(cs->dead, EINA_FALSE);
   cs->extracted = 1;
   if (!cs->shell.popup)
     {
        evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_RESIZE, extracted_changed, cs);
        evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_FOCUS_OUT, extracted_unfocus, cs);
     }
   evas_object_event_callback_add(cs->obj, EVAS_CALLBACK_FOCUS_IN, extracted_focus, cs);
   evas_object_smart_member_del(surface);
   return EINA_TRUE;
}