summaryrefslogtreecommitdiff
path: root/src/lib/ecore_x/xcb/ecore_xcb_window.c
blob: 087c39182b82416f356b0da03dd835ae4e6ef16b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
#include "ecore_xcb_private.h"
#ifdef ECORE_XCB_RENDER
# include <xcb/render.h>
#endif
#ifdef ECORE_XCB_SHAPE
# include <xcb/shape.h>
#endif
#ifdef ECORE_XCB_XPRINT
#include <xcb/xprint.h>
#endif

/* local function prototypes */
static Ecore_X_Window _ecore_xcb_window_argb_internal_new(Ecore_X_Window parent,
                                                          int            x,
                                                          int            y,
                                                          int            w,
                                                          int            h,
                                                          uint8_t        override_redirect,
                                                          uint8_t        save_under);
static Ecore_X_Window _ecore_xcb_window_at_xy_get(Ecore_X_Window  base,
                                                  int             bx,
                                                  int             by,
                                                  int             x,
                                                  int             y,
                                                  Ecore_X_Window *skip,
                                                  int             skip_num);
static int               _ecore_xcb_window_modifiers_get(unsigned int state);
static xcb_visualtype_t *_ecore_xcb_window_find_visual_by_id(xcb_visualid_t id);
#ifdef ECORE_XCB_XPRINT
static xcb_screen_t     *_ecore_xcb_window_screen_of_display(int screen);
#endif

/* local variables */
static int ignore_num = 0;
static Ecore_X_Window *ignore_list = NULL;

/* external variables */
int _ecore_xcb_button_grabs_num = 0;
int _ecore_xcb_key_grabs_num = 0;
Ecore_X_Window *_ecore_xcb_button_grabs = NULL;
Ecore_X_Window *_ecore_xcb_key_grabs = NULL;
Eina_Bool (*_ecore_xcb_window_grab_replay_func)(void *data,
                                                int   type,
                                                void *event);
void *_ecore_xcb_window_grab_replay_data;

/**
 * @defgroup Ecore_X_Window_Create_Group X Window Creation Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that can be used to create an X window.
 */

/**
 * Creates a new window.
 * @param   parent The parent window to use.  If @p parent is @c 0, the root
 *                 window of the default display is used.
 * @param   x      X position.
 * @param   y      Y position.
 * @param   w      Width.
 * @param   h      Height.
 * @return  The new window handle.
 * @ingroup Ecore_X_Window_Create_Group
 */
EAPI Ecore_X_Window
ecore_x_window_new(Ecore_X_Window parent,
                   int            x,
                   int            y,
                   int            w,
                   int            h)
{
   Ecore_X_Window win;
   uint32_t mask, mask_list[9];

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (parent == 0)
     parent = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   /* NB: Order here is very important due to xcb_cw_t enum */
   mask = (XCB_CW_BACK_PIXMAP | XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY |
           XCB_CW_WIN_GRAVITY | XCB_CW_BACKING_STORE |
           XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK |
           XCB_CW_DONT_PROPAGATE);

   mask_list[0] = XCB_BACK_PIXMAP_NONE;
   mask_list[1] = 0;
   mask_list[2] = XCB_GRAVITY_NORTH_WEST;
   mask_list[3] = XCB_GRAVITY_NORTH_WEST;
   mask_list[4] = XCB_BACKING_STORE_NOT_USEFUL;
   mask_list[5] = 0;
   mask_list[6] = 0;
   mask_list[7] = (XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
                   XCB_EVENT_MASK_BUTTON_PRESS |
                   XCB_EVENT_MASK_BUTTON_RELEASE |
                   XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
                   XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE |
                   XCB_EVENT_MASK_VISIBILITY_CHANGE |
                   XCB_EVENT_MASK_STRUCTURE_NOTIFY |
                   XCB_EVENT_MASK_FOCUS_CHANGE |
                   XCB_EVENT_MASK_PROPERTY_CHANGE |
                   XCB_EVENT_MASK_COLOR_MAP_CHANGE);
   mask_list[8] = XCB_EVENT_MASK_NO_EVENT;

   win = xcb_generate_id(_ecore_xcb_conn);
   xcb_create_window(_ecore_xcb_conn, XCB_COPY_FROM_PARENT,
                     win, parent, x, y, w, h, 0,
                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
                     XCB_COPY_FROM_PARENT, mask, mask_list);

   if (parent == ((xcb_screen_t *)_ecore_xcb_screen)->root)
     ecore_x_window_defaults_set(win);

   return win;
}

/**
 * Creates a window with the override redirect attribute set to @c True.
 * @param   parent The parent window to use.  If @p parent is @c 0, the root
 *                 window of the default display is used.
 * @param   x      X position.
 * @param   y      Y position.
 * @param   w      Width.
 * @param   h      Height.
 * @return  The new window handle.
 * @ingroup Ecore_X_Window_Create_Group
 */
EAPI Ecore_X_Window
ecore_x_window_override_new(Ecore_X_Window parent,
                            int            x,
                            int            y,
                            int            w,
                            int            h)
{
   Ecore_X_Window win;
   uint32_t mask, mask_list[9];

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (parent == 0)
     parent = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   /* NB: Order here is very important due to xcb_cw_t enum */
   mask = (XCB_CW_BACK_PIXMAP | XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY |
           XCB_CW_WIN_GRAVITY | XCB_CW_BACKING_STORE |
           XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER | XCB_CW_EVENT_MASK |
           XCB_CW_DONT_PROPAGATE);

   mask_list[0] = XCB_BACK_PIXMAP_NONE;
   mask_list[1] = 0;
   mask_list[2] = XCB_GRAVITY_NORTH_WEST;
   mask_list[3] = XCB_GRAVITY_NORTH_WEST;
   mask_list[4] = XCB_BACKING_STORE_NOT_USEFUL;
   mask_list[5] = 1;
   mask_list[6] = 0;
   mask_list[7] = (XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
                   XCB_EVENT_MASK_BUTTON_PRESS |
                   XCB_EVENT_MASK_BUTTON_RELEASE |
                   XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
                   XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE |
                   XCB_EVENT_MASK_VISIBILITY_CHANGE |
                   XCB_EVENT_MASK_STRUCTURE_NOTIFY |
                   XCB_EVENT_MASK_FOCUS_CHANGE |
                   XCB_EVENT_MASK_PROPERTY_CHANGE |
                   XCB_EVENT_MASK_COLOR_MAP_CHANGE);
   mask_list[8] = XCB_EVENT_MASK_NO_EVENT;

   win = xcb_generate_id(_ecore_xcb_conn);
   xcb_create_window(_ecore_xcb_conn, XCB_COPY_FROM_PARENT,
                     win, parent, x, y, w, h, 0,
                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
                     XCB_COPY_FROM_PARENT, mask, mask_list);

   return win;
}

/**
 * Creates a new input window.
 * @param   parent The parent window to use.    If @p parent is @c 0, the root
 *                 window of the default display is used.
 * @param   x      X position.
 * @param   y      Y position.
 * @param   w      Width.
 * @param   h      Height.
 * @return  The new window.
 * @ingroup Ecore_X_Window_Create_Group
 */
EAPI Ecore_X_Window
ecore_x_window_input_new(Ecore_X_Window parent,
                         int            x,
                         int            y,
                         int            w,
                         int            h)
{
   Ecore_X_Window win;
   uint32_t mask, mask_list[3];

   LOGFN(__FILE__, __LINE__, __FUNCTION__)
   CHECK_XCB_CONN;

   if (parent == 0)
     parent = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   /* NB: Order here is very important due to xcb_cw_t enum */
   mask = (XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK |
           XCB_CW_DONT_PROPAGATE);

   mask_list[0] = 1;
   mask_list[1] = (XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
                   XCB_EVENT_MASK_BUTTON_PRESS |
                   XCB_EVENT_MASK_BUTTON_RELEASE |
                   XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
                   XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE |
                   XCB_EVENT_MASK_VISIBILITY_CHANGE |
                   XCB_EVENT_MASK_STRUCTURE_NOTIFY |
                   XCB_EVENT_MASK_FOCUS_CHANGE |
                   XCB_EVENT_MASK_PROPERTY_CHANGE |
                   XCB_EVENT_MASK_COLOR_MAP_CHANGE);
   mask_list[2] = XCB_EVENT_MASK_NO_EVENT;

   win = xcb_generate_id(_ecore_xcb_conn);
   xcb_create_window(_ecore_xcb_conn, XCB_COPY_FROM_PARENT,
                     win, parent, x, y, w, h, 0,
                     XCB_WINDOW_CLASS_INPUT_ONLY,
                     XCB_COPY_FROM_PARENT, mask, mask_list);

   return win;
}

/**
 * Creates a new window.
 * @param   parent The parent window to use.  If @p parent is @c 0, the root
 *                 window of the default display is used.
 * @param   x      X position.
 * @param   y      Y position.
 * @param   w      Width.
 * @param   h      Height.
 * @return  The new window handle.
 * @ingroup Ecore_X_Window_Create_Group
 */
EAPI Ecore_X_Window
ecore_x_window_manager_argb_new(Ecore_X_Window parent,
                                int            x,
                                int            y,
                                int            w,
                                int            h)
{
   Ecore_X_Window win = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   win = _ecore_xcb_window_argb_internal_new(parent, x, y, w, h, 1, 0);

   return win;
}

/**
 * Creates a new window.
 * @param   parent The parent window to use.  If @p parent is @c 0, the root
 *                 window of the default display is used.
 * @param   x      X position.
 * @param   y      Y position.
 * @param   w      Width.
 * @param   h      Height.
 * @return  The new window handle.
 * @ingroup Ecore_X_Window_Create_Group
 */
EAPI Ecore_X_Window
ecore_x_window_argb_new(Ecore_X_Window parent,
                        int            x,
                        int            y,
                        int            w,
                        int            h)
{
   Ecore_X_Window win = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   win = _ecore_xcb_window_argb_internal_new(parent, x, y, w, h, 0, 0);

   return win;
}

/**
 * Creates a window with the override redirect attribute set to @c True.
 * @param   parent The parent window to use.  If @p parent is @c 0, the root
 *                 window of the default display is used.
 * @param   x      X position.
 * @param   y      Y position.
 * @param   w      Width.
 * @param   h      Height.
 * @return  The new window handle.
 * @ingroup Ecore_X_Window_Create_Group
 */
EAPI Ecore_X_Window
ecore_x_window_override_argb_new(Ecore_X_Window parent,
                                 int            x,
                                 int            y,
                                 int            w,
                                 int            h)
{
   Ecore_X_Window win = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   win = _ecore_xcb_window_argb_internal_new(parent, x, y, w, h, 1, 0);

   return win;
}

/**
 * @defgroup Ecore_X_Window_Destroy_Group X Window Destroy Functions
 * @ingroup Ecore_X_Group
 *
 * Functions to destroy X windows.
 */

/**
 * Deletes the given window.
 * @param   win The given window.
 * @ingroup Ecore_X_Window_Destroy_Group
 */
EAPI void
ecore_x_window_free(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (win)
     {
        /* xcb_destroy_notify_event_t ev; */
        /* Ecore_X_Window root; */

          /* if (xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn)).rem == 1) */
          /*   root = ((xcb_screen_t *)_ecore_xcb_screen)->root; */
          /* else  */
          /*   { */
          /*      xcb_get_geometry_cookie_t cookie; */
          /*      xcb_get_geometry_reply_t *reply; */

     /*      cookie = xcb_get_geometry_unchecked(_ecore_xcb_conn, win); */
                 /*      reply = xcb_get_geometry_reply(_ecore_xcb_conn, cookie, NULL); */
     /*      if (!reply) return; */
                   /*      root = reply->root; */
     /*      free(reply); */
                     /*   } */

     /* memset(&ev, 0, sizeof(xcb_destroy_notify_event_t)); */

                       /* ev.response_type = XCB_DESTROY_NOTIFY; */
     /* ev.window = win; */
                         /* ev.event = root; */

     /* xcb_send_event(_ecore_xcb_conn, 0, root,  */
                           /*                XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |  */
     /*                XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,  */
                             /*                (const char *)&ev); */

        xcb_destroy_window(_ecore_xcb_conn, win);
//        ecore_x_flush();
     }
}

/**
 * Sends a delete request to the given window.
 * @param   win The given window.
 * @ingroup Ecore_X_Window_Destroy_Group
 */
EAPI void
ecore_x_window_delete_request_send(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!win) return;
   ecore_x_client_message32_send(win, ECORE_X_ATOM_WM_PROTOCOLS,
                                 XCB_EVENT_MASK_NO_EVENT,
                                 ECORE_X_ATOM_WM_DELETE_WINDOW,
                                 XCB_CURRENT_TIME, 0, 0, 0);
}

EAPI void
ecore_x_window_configure(Ecore_X_Window                win,
                         Ecore_X_Window_Configure_Mask mask,
                         int                           x,
                         int                           y,
                         int                           w,
                         int                           h,
                         int                           border_width,
                         Ecore_X_Window                sibling,
                         int                           stack_mode)
{
   uint16_t vmask = 0;
   uint32_t vlist[7];
   unsigned int i = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) return;

   if (mask & XCB_CONFIG_WINDOW_X)
     {
        vmask |= XCB_CONFIG_WINDOW_X;
        vlist[i++] = x;
     }
   if (mask & XCB_CONFIG_WINDOW_Y)
     {
        vmask |= XCB_CONFIG_WINDOW_Y;
        vlist[i++] = y;
     }
   if (mask & XCB_CONFIG_WINDOW_WIDTH)
     {
        vmask |= XCB_CONFIG_WINDOW_WIDTH;
        vlist[i++] = w;
     }
   if (mask & XCB_CONFIG_WINDOW_HEIGHT)
     {
        vmask |= XCB_CONFIG_WINDOW_HEIGHT;
        vlist[i++] = h;
     }
   if (mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
     {
        vmask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
        vlist[i++] = border_width;
     }
   if (mask & XCB_CONFIG_WINDOW_SIBLING)
     {
        vmask |= XCB_CONFIG_WINDOW_SIBLING;
        vlist[i++] = sibling;
     }
   if (mask & XCB_CONFIG_WINDOW_STACK_MODE)
     {
        vmask |= XCB_CONFIG_WINDOW_STACK_MODE;
        vlist[i++] = stack_mode;
     }

   xcb_configure_window(_ecore_xcb_conn, win, vmask,
                        (const uint32_t *)&vlist);
//   ecore_x_flush();
}

/**
 * @defgroup Ecore_X_Window_Geometry_Group X Window Geometry Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that change or retrieve the geometry of X windows.
 */

/**
 * Moves a window to the position @p x, @p y.
 *
 * The position is relative to the upper left hand corner of the
 * parent window.
 *
 * @param   win The window to move.
 * @param   x   X position.
 * @param   y   Y position.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI void
ecore_x_window_move(Ecore_X_Window win,
                    int            x,
                    int            y)
{
   uint32_t list[2], mask;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) return;

   mask = (XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y);
   list[0] = x;
   list[1] = y;

   xcb_configure_window(_ecore_xcb_conn, win, mask,
                        (const uint32_t *)&list);
//   ecore_x_flush();
}

/**
 * Resizes a window.
 * @param   win The window to resize.
 * @param   w   New width of the window.
 * @param   h   New height of the window.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI void
ecore_x_window_resize(Ecore_X_Window win,
                      int            w,
                      int            h)
{
   uint32_t list[2], mask;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) return;
   if (w < 1) w = 1;
   if (h < 1) h = 1;

   mask = (XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT);
   list[0] = w;
   list[1] = h;

   xcb_configure_window(_ecore_xcb_conn, win, mask,
                        (const uint32_t *)&list);
//   ecore_x_flush();
}

/**
 * Moves and resizes a window.
 * @param   win The window to move and resize.
 * @param   x   New X position of the window.
 * @param   y   New Y position of the window.
 * @param   w   New width of the window.
 * @param   h   New height of the window.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI void
ecore_x_window_move_resize(Ecore_X_Window win,
                           int            x,
                           int            y,
                           int            w,
                           int            h)
{
   uint32_t list[4], mask;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) return;
   if (w < 1) w = 1;
   if (h < 1) h = 1;

   mask = (XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
           XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT);
   list[0] = x;
   list[1] = y;
   list[2] = w;
   list[3] = h;

   xcb_configure_window(_ecore_xcb_conn, win, mask,
                        (const uint32_t *)&list);
//   ecore_x_flush();
}

/**
 * Retrieves the width of the border of the given window.
 * @param   win The given window.
 * @return  Width of the border of @p win.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI int
ecore_x_window_border_width_get(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (!win) return 0;
   return ecore_x_drawable_border_width_get(win);
}

/**
 * Sets the width of the border of the given window.
 * @param   win The given window.
 * @param   width The new border width.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI void
ecore_x_window_border_width_set(Ecore_X_Window win,
                                int            border_width)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) return;

   list = border_width;

   xcb_configure_window(_ecore_xcb_conn, win,
                        XCB_CONFIG_WINDOW_BORDER_WIDTH, &list);
//   ecore_x_flush();
}

/**
 * @defgroup Ecore_X_Window_Z_Order_Group X Window Z Order Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that change the Z order of X windows.
 */

/**
 * Raises the given window.
 * @param   win The window to raise.
 * @ingroup Ecore_X_Window_Z_Order_Group
 */
EAPI void
ecore_x_window_raise(Ecore_X_Window win)
{
   uint32_t list[] = { XCB_STACK_MODE_ABOVE };

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   xcb_configure_window(_ecore_xcb_conn, win,
                        XCB_CONFIG_WINDOW_STACK_MODE, list);
//   ecore_x_flush();
}

/**
 * Lowers the given window.
 * @param   win The window to lower.
 * @ingroup Ecore_X_Window_Z_Order_Group
 */
EAPI void
ecore_x_window_lower(Ecore_X_Window win)
{
   uint32_t list[] = { XCB_STACK_MODE_BELOW };

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   xcb_configure_window(_ecore_xcb_conn, win,
                        XCB_CONFIG_WINDOW_STACK_MODE, list);
//   ecore_x_flush();
}

/**
 * Retrieves the depth of the given window.
 * @param  win The given window.
 * @return Depth of the window.
 */
EAPI int
ecore_x_window_depth_get(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   return ecore_x_drawable_depth_get(win);
}

/**
 * @defgroup Ecore_X_Window_Properties_Group X Window Property Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that set window properties.
 */

/**
 * Sets the default properties for the given window.
 *
 * The default properties set for the window are @c WM_CLIENT_MACHINE and
 * @c _NET_WM_PID.
 *
 * @param   win The given window.
 * @ingroup Ecore_X_Window_Properties_Group
 */
EAPI void
ecore_x_window_defaults_set(Ecore_X_Window win)
{
   char buff[MAXHOSTNAMELEN], **argv;
   int argc;
   pid_t pid;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   gethostname(buff, MAXHOSTNAMELEN);
   buff[MAXHOSTNAMELEN - 1] = '\0';

   xcb_change_property(_ecore_xcb_conn, XCB_PROP_MODE_REPLACE, win,
                       ECORE_X_ATOM_WM_CLIENT_MACHINE, ECORE_X_ATOM_STRING,
                       8, strlen(buff), buff);

   pid = getpid();
   ecore_x_netwm_pid_set(win, pid);
   ecore_x_netwm_window_type_set(win, ECORE_X_WINDOW_TYPE_NORMAL);
   ecore_app_args_get(&argc, &argv);
   ecore_x_icccm_command_set(win, argc, argv);
}

/**
 * @defgroup Ecore_X_Window_Visibility_Group X Window Visibility Functions
 * @ingroup Ecore_X_Group
 *
 * Functions to access and change the visibility of X windows.
 */

/**
 * Shows a window.
 *
 * Synonymous to "mapping" a window in X Window System terminology.
 *
 * @param   win The window to show.
 * @ingroup Ecore_X_Window_Visibility
 */
EAPI void
ecore_x_window_show(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (win)
     xcb_map_window(_ecore_xcb_conn, win);
}

/**
 * Hides a window.
 *
 * Synonymous to "unmapping" a window in X Window System terminology.
 *
 * @param   win The window to hide.
 * @ingroup Ecore_X_Window_Visibility
 */
EAPI void
ecore_x_window_hide(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (win)
     {
        xcb_unmap_notify_event_t ev;
        Ecore_X_Window root;

        if (xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn)).rem == 1)
          root = ((xcb_screen_t *)_ecore_xcb_screen)->root;
        else
          {
             xcb_get_geometry_cookie_t cookie;
             xcb_get_geometry_reply_t *reply;

             cookie = xcb_get_geometry_unchecked(_ecore_xcb_conn, win);
             reply = xcb_get_geometry_reply(_ecore_xcb_conn, cookie, NULL);
             if (!reply) return;
             root = reply->root;
             free(reply);
          }

        xcb_unmap_window(_ecore_xcb_conn, win);
        memset(&ev, 0, sizeof(xcb_unmap_notify_event_t));

        ev.response_type = XCB_UNMAP_NOTIFY;
        ev.window = win;
        ev.event = root;
        ev.from_configure = 0;

        xcb_send_event(_ecore_xcb_conn, 0, root,
                       (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
                        XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT),
                       (const char *)&ev);

//        ecore_x_flush();
     }
}

/**
 * @defgroup Ecore_X_Window_Focus_Functions X Window Focus Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that give the focus to an X Window.
 */

/**
 * Sets the focus to the window @p win.
 * @param   win The window to focus.
 * @ingroup Ecore_X_Window_Focus_Functions
 */
EAPI void
ecore_x_window_focus(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) win = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   xcb_set_input_focus(_ecore_xcb_conn,
                       XCB_INPUT_FOCUS_PARENT, win, XCB_CURRENT_TIME);
//   ecore_x_flush();
}

/**
 * Sets the focus to the given window at a specific time.
 * @param   win The window to focus.
 * @param   t   When to set the focus to the window.
 * @ingroup Ecore_X_Window_Focus_Functions
 */
EAPI void
ecore_x_window_focus_at_time(Ecore_X_Window win,
                             Ecore_X_Time   time EINA_UNUSED)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) win = ((xcb_screen_t *)_ecore_xcb_screen)->root;
   xcb_set_input_focus(_ecore_xcb_conn, 
                       XCB_INPUT_FOCUS_PARENT, win, XCB_CURRENT_TIME);
//   ecore_x_flush();
}

/**
 * @defgroup Ecore_X_Window_Parent_Group X Window Parent Functions
 * @ingroup Ecore_X_Group
 *
 * Functions that retrieve or changes the parent window of a window.
 */

/**
 * Moves a window to within another window at a given position.
 * @param   win        The window to reparent.
 * @param   new_parent The new parent window.
 * @param   x          X position within new parent window.
 * @param   y          Y position within new parent window.
 * @ingroup Ecore_X_Window_Parent_Group
 */
EAPI void
ecore_x_window_reparent(Ecore_X_Window win,
                        Ecore_X_Window parent,
                        int            x,
                        int            y)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (parent == 0)
     parent = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   xcb_reparent_window(_ecore_xcb_conn, win, parent, x, y);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_pixmap_set(Ecore_X_Window win,
                          Ecore_X_Pixmap pixmap)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = pixmap;

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_BACK_PIXMAP, &list);
//   ecore_x_flush();
}

/**
 * Sets the background color of the given window.
 * @param win   The given window
 * @param r     red value (0...65536, 16 bits)
 * @param g     green value (0...65536, 16 bits)
 * @param b     blue value (0...65536, 16 bits)
 */
EAPI void
ecore_x_window_background_color_set(Ecore_X_Window win,
                                    unsigned short red,
                                    unsigned short green,
                                    unsigned short blue)
{
   xcb_alloc_color_cookie_t cookie;
   xcb_alloc_color_reply_t *reply;
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie =
     xcb_alloc_color_unchecked(_ecore_xcb_conn,
                               ((xcb_screen_t *)_ecore_xcb_screen)->default_colormap,
                               red, green, blue);
   reply = xcb_alloc_color_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return;
   list = reply->pixel;
   free(reply);

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_BACK_PIXEL, &list);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_pixel_gravity_set(Ecore_X_Window  win,
                                 Ecore_X_Gravity gravity)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = gravity;

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_BIT_GRAVITY, &list);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_gravity_set(Ecore_X_Window  win,
                           Ecore_X_Gravity gravity)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = gravity;

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_WIN_GRAVITY, &list);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_override_set(Ecore_X_Window win,
                            Eina_Bool      override)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = override;

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_OVERRIDE_REDIRECT, &list);
//   ecore_x_flush();
}

/**
 * @brief Show the cursor on a window of type Ecore_X_Window.
 * @param win The window for which the cursor will be showed.
 * @param show Enables the show of the cursor on the window if equals EINA_TRUE, disables if equals EINA_FALSE.
 */
EAPI void
ecore_x_window_cursor_show(Ecore_X_Window win,
                           Eina_Bool      show)
{
   uint32_t list = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) win = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   if (!show)
     {
        Ecore_X_Cursor cursor;
        Ecore_X_Pixmap p, m;
        Ecore_X_GC gc;
        xcb_point_t point;

        p = xcb_generate_id(_ecore_xcb_conn);
        xcb_create_pixmap(_ecore_xcb_conn, 1, p, win, 1, 1);
        m = xcb_generate_id(_ecore_xcb_conn);
        xcb_create_pixmap(_ecore_xcb_conn, 1, m, win, 1, 1);
        gc = xcb_generate_id(_ecore_xcb_conn);
        xcb_create_gc(_ecore_xcb_conn, gc, win, 0, NULL);
        xcb_change_gc(_ecore_xcb_conn, gc, XCB_GC_FOREGROUND, &list);
        point.x = 0;
        point.y = 0;
        xcb_poly_point(_ecore_xcb_conn, XCB_COORD_MODE_ORIGIN,
                       win, gc, 1, &point);
        xcb_free_gc(_ecore_xcb_conn, gc);

        cursor = xcb_generate_id(_ecore_xcb_conn);
        xcb_create_cursor(_ecore_xcb_conn, cursor,
                          p, m, 0, 0, 0, 0, 0, 0, 0, 0);
        list = cursor;

        xcb_change_window_attributes(_ecore_xcb_conn, win,
                                     XCB_CW_CURSOR, &list);

        xcb_free_cursor(_ecore_xcb_conn, cursor);
        xcb_free_pixmap(_ecore_xcb_conn, m);
        xcb_free_pixmap(_ecore_xcb_conn, p);
     }
   else
     {
        xcb_change_window_attributes(_ecore_xcb_conn, win,
                                     XCB_CW_CURSOR, &list);
     }
//   ecore_x_flush();
}

EAPI void
ecore_x_window_cursor_set(Ecore_X_Window win,
                          Ecore_X_Cursor cursor)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = cursor;

   xcb_change_window_attributes(_ecore_xcb_conn, win, XCB_CW_CURSOR, &list);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_container_manage(Ecore_X_Window win)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = (XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
           XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY);

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_EVENT_MASK, &list);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_client_manage(Ecore_X_Window win)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = (XCB_EVENT_MASK_VISIBILITY_CHANGE |
           XCB_EVENT_MASK_FOCUS_CHANGE |
           XCB_EVENT_MASK_PROPERTY_CHANGE |
           XCB_EVENT_MASK_COLOR_MAP_CHANGE |
           XCB_EVENT_MASK_STRUCTURE_NOTIFY |
           XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY);

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_EVENT_MASK, &list);

#ifdef ECORE_XCB_SHAPE
   xcb_shape_select_input(_ecore_xcb_conn, win, EINA_TRUE);
#endif
//   ecore_x_flush();
}

EAPI void
ecore_x_window_sniff(Ecore_X_Window win)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
           XCB_EVENT_MASK_PROPERTY_CHANGE);

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_EVENT_MASK, &list);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_client_sniff(Ecore_X_Window win)
{
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   list = (XCB_EVENT_MASK_VISIBILITY_CHANGE |
           XCB_EVENT_MASK_STRUCTURE_NOTIFY |
           XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
           XCB_EVENT_MASK_FOCUS_CHANGE |
           XCB_EVENT_MASK_PROPERTY_CHANGE |
           XCB_EVENT_MASK_COLOR_MAP_CHANGE);

   xcb_change_window_attributes(_ecore_xcb_conn, win,
                                XCB_CW_EVENT_MASK, &list);
#ifdef ECORE_XCB_SHAPE
   xcb_shape_select_input(_ecore_xcb_conn, win, EINA_TRUE);
#endif
//   ecore_x_flush();
}

EAPI void
ecore_x_window_area_clear(Ecore_X_Window win,
                          int            x,
                          int            y,
                          int            w,
                          int            h)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   xcb_clear_area(_ecore_xcb_conn, 0, win, x, y, w, h);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_area_expose(Ecore_X_Window win,
                           int            x,
                           int            y,
                           int            w,
                           int            h)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   xcb_clear_area(_ecore_xcb_conn, 1, win, x, y, w, h);
//   ecore_x_flush();
}

EAPI void
ecore_x_window_save_set_add(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   xcb_change_save_set(_ecore_xcb_conn, XCB_SET_MODE_INSERT, win);
}

EAPI void
ecore_x_window_save_set_del(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   xcb_change_save_set(_ecore_xcb_conn, XCB_SET_MODE_DELETE, win);
}

/**
 * gets the window that has focus.
 * @return  The window that has focus.
 * @ingroup Ecore_X_Window_Focus_Functions
 */
EAPI Ecore_X_Window
ecore_x_window_focus_get(void)
{
   xcb_get_input_focus_cookie_t cookie;
   xcb_get_input_focus_reply_t *reply;
   Ecore_X_Window focus = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie = xcb_get_input_focus_unchecked(_ecore_xcb_conn);
   reply = xcb_get_input_focus_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return 0;
   focus = reply->focus;
   free(reply);
   return focus;
}

EAPI int
ecore_x_window_argb_get(Ecore_X_Window win)
{
   uint8_t ret = 0;
#ifdef ECORE_XCB_RENDER
   Ecore_X_Visual visual;
#endif

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

//   if (!win) return ret;

#ifdef ECORE_XCB_RENDER
   /* grab the window's visual */
   visual = _ecore_xcb_window_visual_get(win);

   /* check if this visual supports alpha */
   ret = _ecore_xcb_render_visual_supports_alpha(visual);
#endif

   return ret;
}

EAPI Eina_Bool
ecore_x_window_manage(Ecore_X_Window win)
{
   xcb_get_window_attributes_cookie_t cookie;
   xcb_get_window_attributes_reply_t *reply;
   xcb_void_cookie_t change_cookie;
   xcb_generic_error_t *err;
   uint32_t list;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie = xcb_get_window_attributes(_ecore_xcb_conn, win);
   reply = xcb_get_window_attributes_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return EINA_FALSE;

   ecore_x_sync(); // needed

   list = (XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
           XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_RESIZE_REDIRECT |
           XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
           XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
           XCB_EVENT_MASK_STRUCTURE_NOTIFY |
           XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
           reply->your_event_mask);
   free(reply);

   change_cookie = xcb_change_window_attributes(_ecore_xcb_conn, win,
                                                XCB_CW_EVENT_MASK, &list);

   ecore_x_sync(); // needed

   err = xcb_request_check(_ecore_xcb_conn, change_cookie);
   if (err)
     {
        _ecore_xcb_error_handle(err);
        free(err);
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

EAPI Eina_Bool
ecore_x_window_attributes_get(Ecore_X_Window             win,
                              Ecore_X_Window_Attributes *att_ret)
{
   xcb_get_window_attributes_cookie_t cookie;
   xcb_get_window_attributes_reply_t *reply;
   xcb_get_geometry_cookie_t gcookie;
   xcb_get_geometry_reply_t *greply;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie = xcb_get_window_attributes_unchecked(_ecore_xcb_conn, win);
   reply = xcb_get_window_attributes_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return EINA_FALSE;

   memset(att_ret, 0, sizeof(Ecore_X_Window_Attributes));

   if (reply->map_state != XCB_MAP_STATE_UNMAPPED)
     att_ret->visible = EINA_TRUE;

   if (reply->map_state == XCB_MAP_STATE_VIEWABLE)
     att_ret->viewable = EINA_TRUE;

   if (reply->override_redirect)
     att_ret->override = EINA_TRUE;

   if (reply->_class == XCB_WINDOW_CLASS_INPUT_ONLY)
     att_ret->input_only = EINA_TRUE;

   if (reply->save_under)
     att_ret->save_under = EINA_TRUE;

   att_ret->event_mask.mine = reply->your_event_mask;
   att_ret->event_mask.all = reply->all_event_masks;
   att_ret->event_mask.no_propagate = reply->do_not_propagate_mask;
   att_ret->window_gravity = reply->win_gravity;
   att_ret->pixel_gravity = reply->bit_gravity;
   att_ret->colormap = reply->colormap;
   att_ret->visual = _ecore_xcb_window_find_visual_by_id(reply->visual);

   free(reply);

   gcookie = xcb_get_geometry_unchecked(_ecore_xcb_conn, win);
   greply = xcb_get_geometry_reply(_ecore_xcb_conn, gcookie, NULL);
   if (!greply) return EINA_TRUE;

   /* xcb_translate_coordinates_reply_t *trans; */
   /* xcb_query_tree_cookie_t tcookie; */
   /* xcb_query_tree_reply_t *treply; */

   /* tcookie = xcb_query_tree(_ecore_xcb_conn, win); */
   /* treply = xcb_query_tree_reply(_ecore_xcb_conn, tcookie, NULL); */

   /* trans =  */
   /*   xcb_translate_coordinates_reply(_ecore_xcb_conn,  */
   /*                                   xcb_translate_coordinates(_ecore_xcb_conn,  */
   /*                                                             win, treply->parent, greply->x, greply->y), NULL); */
   /* free(treply); */

   att_ret->root = greply->root;
   att_ret->depth = greply->depth;
//   att_ret->x = trans->dst_x;
//   att_ret->y = trans->dst_y;
   att_ret->x = greply->x;
   att_ret->y = greply->y;
   att_ret->w = greply->width;
   att_ret->h = greply->height;
   att_ret->border = greply->border_width;

//   free(trans);

   free(greply);
   return EINA_TRUE;
}

/**
 * Retrieves the size of the given window.
 * @param   win The given window.
 * @param   w   Pointer to an integer into which the width is to be stored.
 * @param   h   Pointer to an integer into which the height is to be stored.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI void
ecore_x_window_size_get(Ecore_X_Window win,
                        int           *width,
                        int           *height)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) win = ((xcb_screen_t *)_ecore_xcb_screen)->root;
   ecore_x_drawable_geometry_get(win, NULL, NULL, width, height);
}

/**
 * Set if a window should be ignored.
 * @param   win The given window.
 * @param   ignore if to ignore
 */
EAPI void
ecore_x_window_ignore_set(Ecore_X_Window win,
                          int            ignore)
{
   int i = 0, j = 0, count = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (ignore)
     {
        if (ignore_list)
          {
             for (i = 0; i < ignore_num; i++)
               if (win == ignore_list[i]) return;

             ignore_list =
               realloc(ignore_list, (ignore_num + 1) * sizeof(Ecore_X_Window));
             if (!ignore_list) return;

             ignore_list[ignore_num++] = win;
          }
        else
          {
             ignore_num = 0;
             ignore_list = malloc(sizeof(Ecore_X_Window));
             if (!ignore_list) return;
             ignore_list[ignore_num++] = win;
          }
     }
   else
     {
        if (!ignore_list) return;
        for (count = ignore_num, i = 0, j = 0; i < count; i++)
          {
             if (win != ignore_list[i])
               ignore_list[j++] = ignore_list[i];
             else
               ignore_num--;
          }
        if (ignore_num <= 0)
          {
             free(ignore_list);
             ignore_list = NULL;
             return;
          }

        ignore_list =
          realloc(ignore_list, ignore_num * sizeof(Ecore_X_Window));
     }
}

/**
 * Get the ignore list
 * @param   num number of windows in the list
 * @return  list of windows to ignore
 */
EAPI Ecore_X_Window *
ecore_x_window_ignore_list(int *num)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);

   if (num) *num = ignore_num;
   return ignore_list;
}

/**
 * Get a list of all the root windows on the server.
 *
 * @note   The returned array will need to be freed after use.
 * @param  num_ret Pointer to integer to put number of windows returned in.
 * @return An array of all the root windows.  @c NULL is returned if memory
 *         could not be allocated for the list, or if @p num_ret is @c NULL.
 */
EAPI Ecore_X_Window *
ecore_x_window_root_list(int *num_ret)
{
   xcb_screen_iterator_t iter;
   uint8_t i, num;
   Ecore_X_Window *roots = NULL;
#ifdef ECORE_XCB_XPRINT
   const xcb_query_extension_reply_t *ext_reply;
#endif

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!num_ret) return NULL;
   if (num_ret) *num_ret = 0;

   /* if (xcb_connection_has_error(_ecore_xcb_conn))  */
   /*   { */
   /*      DBG("XCB Connection Has Error !!!"); */
   /*      return NULL; */
   /*   } */

   num = ecore_x_screen_count_get();

#ifdef ECORE_XCB_XPRINT
   ext_reply = xcb_get_extension_data(_ecore_xcb_conn, &xcb_x_print_id);
   if ((ext_reply) && (ext_reply->present))
     {
        xcb_x_print_print_query_screens_cookie_t cookie;
        xcb_x_print_print_query_screens_reply_t *reply;

        cookie = xcb_x_print_print_query_screens_unchecked(_ecore_xcb_conn);
        reply =
          xcb_x_print_print_query_screens_reply(_ecore_xcb_conn, cookie, NULL);
        if (reply)
          {
             xcb_window_t *screens;
             int psnum = 0, overlap = 0, j = 0, k = 0;

             psnum = xcb_x_print_print_query_screens_roots_length(reply);
             screens = xcb_x_print_print_query_screens_roots(reply);
             for (i = 0; i < num; i++)
               {
                  for (j = 0; j < psnum; j++)
                    {
                       xcb_screen_t *s;

                       if ((s = _ecore_xcb_window_screen_of_display(i)))
                         {
                            if (s->root == screens[j])
                              overlap++;
                         }
                    }
               }
             if (!(roots = malloc((num - overlap)
                                  * sizeof(Ecore_X_Window)))) return NULL;
             for (i = 0; i < num; i++)
               {
                  Eina_Bool is_print = EINA_FALSE;

                  for (j = 0; j < psnum; j++)
                    {
                       xcb_screen_t *s;

                       if ((s = _ecore_xcb_window_screen_of_display(i)))
                         {
                            if (s->root == screens[j])
                              {
                                 is_print = EINA_TRUE;
                                 break;
                              }
                         }
                    }
                  if (!is_print)
                    {
                       xcb_screen_t *s;

                       if ((s = _ecore_xcb_window_screen_of_display(i)))
                         {
                            roots[k] = s->root;
                            k++;
                         }
                    }
               }
             if (num_ret) *num_ret = k;
             free(reply);
          }
        else
          {
             /* Fallback to default method */
              iter =
                xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn));
              if (!(roots = malloc(num * sizeof(Ecore_X_Window)))) return NULL;
              if (num_ret) *num_ret = num;
              for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
                roots[i] = iter.data->root;
          }
     }
   else
     {
        /* Fallback to default method */
         iter =
           xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn));
         if (!(roots = malloc(num * sizeof(Ecore_X_Window)))) return NULL;
         if (num_ret) *num_ret = num;
         for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
           roots[i] = iter.data->root;
     }
#else
   iter =
     xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn));
   if (!(roots = malloc(num * sizeof(Ecore_X_Window)))) return NULL;
   if (num_ret) *num_ret = num;
   for (i = 0; iter.rem; xcb_screen_next(&iter), i++)
     roots[i] = iter.data->root;
#endif

   return roots;
}

EAPI Ecore_X_Window *
ecore_x_window_children_get(Ecore_X_Window win,
                            int           *num)
{
   xcb_query_tree_cookie_t cookie;
   xcb_query_tree_reply_t *reply;
   Ecore_X_Window *windows = NULL;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (num) *num = 0;
   cookie = xcb_query_tree_unchecked(_ecore_xcb_conn, win);
   reply = xcb_query_tree_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return NULL;

   if (num) *num = reply->children_len;
   if (reply->children_len > 0)
     {
        windows = malloc(sizeof(Ecore_X_Window) * reply->children_len);
        if (windows)
          {
             unsigned int i = 0;
             xcb_window_t *w;

             w = xcb_query_tree_children(reply);
             for (i = 0; i < reply->children_len; i++)
               windows[i] = w[i];
          }
     }

   free(reply);
   return windows;
}

/**
 * Retrieves the root window a given window is on.
 * @param   win The window to get the root window of
 * @return  The root window of @p win
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI Ecore_X_Window
ecore_x_window_root_get(Ecore_X_Window win)
{
   xcb_get_geometry_cookie_t gcookie;
   xcb_get_geometry_reply_t *greply;
   Ecore_X_Window window = 0;

   /* LOGFN(__FILE__, __LINE__, __FUNCTION__); */
   CHECK_XCB_CONN;

   gcookie = xcb_get_geometry_unchecked(_ecore_xcb_conn, win);
   greply = xcb_get_geometry_reply(_ecore_xcb_conn, gcookie, NULL);
   if (!greply) return 0;
   window = greply->root;
   free(greply);

   return window;
}

EAPI Ecore_X_Window
ecore_x_window_root_first_get(void)
{
   return ((xcb_screen_t *)_ecore_xcb_screen)->root;
}

/**
 * Retrieves the geometry of the given window.
 *
 * Note that the x & y coordingates are relative to your parent.  In
 * particular for reparenting window managers - relative to you window border.
 * If you want screen coordinates either walk the window tree to the root,
 * else for ecore_evas applications see ecore_evas_geometry_get().  Elementary
 * applications can use elm_win_screen_position_get().
 *
 * @param   win The given window.
 * @param   x   Pointer to an integer in which the X position is to be stored.
 * @param   y   Pointer to an integer in which the Y position is to be stored.
 * @param   w   Pointer to an integer in which the width is to be stored.
 * @param   h   Pointer to an integer in which the height is to be stored.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI void
ecore_x_window_geometry_get(Ecore_X_Window win,
                            int           *x,
                            int           *y,
                            int           *w,
                            int           *h)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!win) win = ((xcb_screen_t *)_ecore_xcb_screen)->root;
   ecore_x_drawable_geometry_get(win, x, y, w, h);
}

/**
 * Retrieves the top, visible window at the given location.
 * @param   x The given X position.
 * @param   y The given Y position.
 * @return  The window at that position.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI Ecore_X_Window
ecore_x_window_at_xy_get(int x,
                         int y)
{
   Ecore_X_Window root, win = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   root = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   ecore_x_grab();
   win = _ecore_xcb_window_at_xy_get(root, 0, 0, x, y, NULL, 0);
   ecore_x_ungrab();

   return win ? win : root;
}

/**
 * Retrieves the top, visible window at the given location,
 * but skips the windows in the list.
 * @param   x The given X position.
 * @param   y The given Y position.
 * @param   skip The list of windows to be skipped.
 * @param   skip_num The number of windows to be skipped.
 * @return  The window at that position.
 * @ingroup Ecore_X_Window_Geometry_Group
 */
EAPI Ecore_X_Window
ecore_x_window_at_xy_with_skip_get(int             x,
                                   int             y,
                                   Ecore_X_Window *skip,
                                   int             skip_num)
{
   Ecore_X_Window root, win = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   root = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   ecore_x_grab();
   win = _ecore_xcb_window_at_xy_get(root, 0, 0, x, y, skip, skip_num);
   ecore_x_ungrab();

   return win ? win : root;
}

EAPI Ecore_X_Window
ecore_x_window_at_xy_begin_get(Ecore_X_Window begin,
                               int            x,
                               int            y)
{
   Ecore_X_Window win = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   ecore_x_grab();
   win = _ecore_xcb_window_at_xy_get(begin, 0, 0, x, y, NULL, 0);
   ecore_x_ungrab();

   return win ? win : begin;
}

/**
 * Retrieves the parent window of the given window.
 * @param   win The given window.
 * @return  The parent window of @p win.
 * @ingroup Ecore_X_Window_Parent_Group
 */
EAPI Ecore_X_Window
ecore_x_window_parent_get(Ecore_X_Window win)
{
   xcb_query_tree_cookie_t cookie;
   xcb_query_tree_reply_t *reply;
   Ecore_X_Window window = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

//   if (!win) return 0;
   cookie = xcb_query_tree(_ecore_xcb_conn, win);
   reply = xcb_query_tree_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return 0;
   window = reply->parent;
   free(reply);

   return window;
}

/**
 * Finds out whether the given window is currently visible.
 * @param   win The given window.
 * @return  1 if the window is visible, otherwise 0.
 * @ingroup Ecore_X_Window_Visibility_Group
 */
EAPI int
ecore_x_window_visible_get(Ecore_X_Window win)
{
   xcb_get_window_attributes_cookie_t cookie;
   xcb_get_window_attributes_reply_t *reply;
   int ret = EINA_FALSE;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   cookie = xcb_get_window_attributes_unchecked(_ecore_xcb_conn, win);
   reply = xcb_get_window_attributes_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return EINA_FALSE;

   if (reply->map_state == XCB_MAP_STATE_VIEWABLE)
     ret = EINA_TRUE;

   free(reply);
   return ret;
}

EAPI void
ecore_x_window_button_grab(Ecore_X_Window     win,
                           int                button,
                           Ecore_X_Event_Mask mask,
                           int                mod,
                           int                any_mod)
{
   int i = 0;
   uint16_t m, locks[8], ev;
   uint8_t b;
   Ecore_X_Window *t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   b = button;
   if (b == 0)
     b = XCB_BUTTON_INDEX_ANY;

   m = _ecore_xcb_window_modifiers_get(mod);
   if (any_mod) m = XCB_MOD_MASK_ANY;

   locks[0] = 0;
   locks[1] = ECORE_X_LOCK_CAPS;
   locks[2] = ECORE_X_LOCK_NUM;
   locks[3] = ECORE_X_LOCK_SCROLL;
   locks[4] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM;
   locks[5] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_SCROLL;
   locks[6] = ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;
   locks[7] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;

   ev = mask;
   for (i = 0; i < 8; i++)
     xcb_grab_button(_ecore_xcb_conn, 0, win, ev,
                     XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
                     XCB_NONE, XCB_NONE, b, m | locks[i]);

   _ecore_xcb_button_grabs_num++;
   t = realloc(_ecore_xcb_button_grabs,
               _ecore_xcb_button_grabs_num * sizeof(Ecore_X_Window));
   if (!t) return;

   _ecore_xcb_button_grabs = t;
   _ecore_xcb_button_grabs[_ecore_xcb_button_grabs_num - 1] = win;
}

EAPI void
ecore_x_window_button_ungrab(Ecore_X_Window win,
                             int            button,
                             int            mod,
                             int            any_mod)
{
   int i = 0;
   uint16_t m = 0, locks[8];
   uint8_t b;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   b = button;
   if (b == 0) b = XCB_BUTTON_INDEX_ANY;

   m = _ecore_xcb_window_modifiers_get(mod);
   if (any_mod) m = XCB_MOD_MASK_ANY;

   locks[0] = 0;
   locks[1] = ECORE_X_LOCK_CAPS;
   locks[2] = ECORE_X_LOCK_NUM;
   locks[3] = ECORE_X_LOCK_SCROLL;
   locks[4] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM;
   locks[5] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_SCROLL;
   locks[6] = ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;
   locks[7] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;

   for (i = 0; i < 8; i++)
     xcb_ungrab_button(_ecore_xcb_conn, b, win, m | locks[i]);

   _ecore_xcb_sync_magic_send(1, win);
}

EAPI void
ecore_x_window_key_grab(Ecore_X_Window win,
                        const char    *key,
                        int            mod,
                        int            any_mod)
{
   xcb_keycode_t keycode = XCB_NO_SYMBOL;
   uint16_t m = 0, locks[8];
   int i = 0;
   Ecore_X_Window *t;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   keycode = _ecore_xcb_keymap_string_to_keycode(key);
   if (keycode == XCB_NO_SYMBOL) return;

   m = _ecore_xcb_window_modifiers_get(mod);
   if (any_mod) m = XCB_MOD_MASK_ANY;

   locks[0] = 0;
   locks[1] = ECORE_X_LOCK_CAPS;
   locks[2] = ECORE_X_LOCK_NUM;
   locks[3] = ECORE_X_LOCK_SCROLL;
   locks[4] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM;
   locks[5] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_SCROLL;
   locks[6] = ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;
   locks[7] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;

   for (i = 0; i < 8; i++)
     xcb_grab_key(_ecore_xcb_conn, 0, win, m | locks[i],
                  keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
   _ecore_xcb_key_grabs_num++;
   t = realloc(_ecore_xcb_key_grabs,
               _ecore_xcb_key_grabs_num * sizeof(Ecore_X_Window));
   if (!t) return;
   _ecore_xcb_key_grabs = t;
   _ecore_xcb_key_grabs[_ecore_xcb_key_grabs_num - 1] = win;
}

EAPI void
ecore_x_window_key_ungrab(Ecore_X_Window win,
                          const char    *key,
                          int            mod,
                          int            any_mod)
{
   xcb_keycode_t keycode = XCB_NO_SYMBOL;
   uint16_t m = 0, locks[8];
   int i = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   keycode = _ecore_xcb_keymap_string_to_keycode(key);
   if (keycode == XCB_NO_SYMBOL) return;

   m = _ecore_xcb_window_modifiers_get(mod);
   if (any_mod) m = XCB_MOD_MASK_ANY;

   locks[0] = 0;
   locks[1] = ECORE_X_LOCK_CAPS;
   locks[2] = ECORE_X_LOCK_NUM;
   locks[3] = ECORE_X_LOCK_SCROLL;
   locks[4] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM;
   locks[5] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_SCROLL;
   locks[6] = ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;
   locks[7] = ECORE_X_LOCK_CAPS | ECORE_X_LOCK_NUM | ECORE_X_LOCK_SCROLL;

   for (i = 0; i < 8; i++)
     xcb_ungrab_key(_ecore_xcb_conn, keycode, win, m | locks[i]);

   _ecore_xcb_sync_magic_send(2, win);
}

/* local functions */
Ecore_X_Window
_ecore_xcb_window_root_of_screen_get(int screen)
{
   xcb_screen_iterator_t iter;

   CHECK_XCB_CONN;
   iter = xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn));
   for (; iter.rem; --screen, xcb_screen_next(&iter))
     if (screen == 0)
       {
          xcb_screen_t *s;

          if ((s = iter.data))
            return s->root;
       }
   return 0;
}

static Ecore_X_Window
_ecore_xcb_window_argb_internal_new(Ecore_X_Window parent,
                                    int            x,
                                    int            y,
                                    int            w,
                                    int            h,
                                    uint8_t        override_redirect,
                                    uint8_t        save_under)
{
   Ecore_X_Window win = 0;
#ifdef ECORE_XCB_RENDER
   uint32_t value_list[10];
   uint32_t value_mask;
   uint32_t vis;
   Ecore_X_Colormap colormap;
#endif

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

#ifdef ECORE_XCB_RENDER
   if (parent == 0)
     parent = ((xcb_screen_t *)_ecore_xcb_screen)->root;

   vis =
     _ecore_xcb_render_find_visual_id(XCB_RENDER_PICT_TYPE_DIRECT, EINA_TRUE);

   colormap = xcb_generate_id(_ecore_xcb_conn);
   xcb_create_colormap(_ecore_xcb_conn, XCB_COLORMAP_ALLOC_NONE,
                       colormap, parent, vis);

   value_mask = (XCB_CW_BACK_PIXMAP | XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY |
                 XCB_CW_WIN_GRAVITY | XCB_CW_BACKING_STORE |
                 XCB_CW_OVERRIDE_REDIRECT | XCB_CW_SAVE_UNDER |
                 XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE | XCB_CW_COLORMAP);

   value_list[0] = XCB_BACK_PIXMAP_NONE;
   value_list[1] = 0;
   value_list[2] = XCB_GRAVITY_NORTH_WEST;
   value_list[3] = XCB_GRAVITY_NORTH_WEST;
   value_list[4] = XCB_BACKING_STORE_NOT_USEFUL;
   value_list[5] = override_redirect;
   value_list[6] = save_under;
   value_list[7] = (XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
                    XCB_EVENT_MASK_BUTTON_PRESS |
                    XCB_EVENT_MASK_BUTTON_RELEASE |
                    XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW |
                    XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE |
                    XCB_EVENT_MASK_VISIBILITY_CHANGE |
                    XCB_EVENT_MASK_STRUCTURE_NOTIFY |
                    XCB_EVENT_MASK_FOCUS_CHANGE |
                    XCB_EVENT_MASK_PROPERTY_CHANGE |
                    XCB_EVENT_MASK_COLOR_MAP_CHANGE);
   value_list[8] = XCB_EVENT_MASK_NO_EVENT;
   value_list[9] = colormap;

   win = xcb_generate_id(_ecore_xcb_conn);
   xcb_create_window(_ecore_xcb_conn, 32, win, parent, x, y, w, h, 0,
                     XCB_WINDOW_CLASS_INPUT_OUTPUT, vis, value_mask,
                     value_list);

   xcb_free_colormap(_ecore_xcb_conn, colormap);

   if (parent == ((xcb_screen_t *)_ecore_xcb_screen)->root)
     ecore_x_window_defaults_set(win);
#endif

   return win;
}

static Ecore_X_Window
_ecore_xcb_window_at_xy_get(Ecore_X_Window  base,
                            int             bx,
                            int             by,
                            int             x,
                            int             y,
                            Ecore_X_Window *skip,
                            int             skip_num)
{
   xcb_query_tree_cookie_t cookie;
   xcb_query_tree_reply_t *reply;
   Ecore_X_Window *windows = NULL;
   int wx, wy, ww, wh, num, i = 0;
   Eina_Bool skipit = EINA_FALSE;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (!ecore_x_window_visible_get(base)) return 0;

   ecore_x_window_geometry_get(base, &wx, &wy, &ww, &wh);
   wx += bx;
   wy += by;

   if (!((x >= wx) && (y >= wy) && (x < (wx + ww)) && (y < (wy + wh))))
     return 0;

   cookie = xcb_query_tree_unchecked(_ecore_xcb_conn, base);
   reply = xcb_query_tree_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return 0;

   num = reply->children_len;
   windows = xcb_query_tree_children(reply);

   for (i = (num - 1); i >= 0; --i)
     {
        skipit = EINA_FALSE;

        if (skip)
          {
             int j = 0;

             for (j = 0; j < skip_num; j++)
               {
                  if (windows[i] == skip[j])
                    {
                       skipit = EINA_TRUE;
                       goto onward;
                    }
               }
          }
onward:
        if (!skipit)
          {
             Ecore_X_Window child = 0;

             child =
               _ecore_xcb_window_at_xy_get(windows[i],
                                           wx, wy, x, y, skip, skip_num);
             if (child)
               {
                  if (reply) free(reply);
                  return child;
               }
          }
     }

   if (reply) free(reply);
   return base;
}

Ecore_X_Visual
_ecore_xcb_window_visual_get(Ecore_X_Window win)
{
   xcb_get_window_attributes_cookie_t cookie;
   xcb_get_window_attributes_reply_t *reply;
   Ecore_X_Visual visual = 0;

   CHECK_XCB_CONN;

   cookie = xcb_get_window_attributes(_ecore_xcb_conn, win);
   reply = xcb_get_window_attributes_reply(_ecore_xcb_conn, cookie, NULL);
   if (!reply) return 0;
   visual = _ecore_xcb_window_find_visual_by_id(reply->visual);
   free(reply);

   return visual;
}

void
_ecore_xcb_window_button_grab_remove(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (_ecore_xcb_button_grabs_num > 0)
     {
        int i = 0, shuffle = 0;

        for (i = 0; i < _ecore_xcb_button_grabs_num; i++)
          {
             if (shuffle)
               _ecore_xcb_button_grabs[i - 1] = _ecore_xcb_button_grabs[i];

             if ((!shuffle) && (_ecore_xcb_button_grabs[i] == win))
               shuffle = 1;
          }

        if (shuffle)
          {
             Ecore_X_Window *t;

             _ecore_xcb_button_grabs_num--;
             if (_ecore_xcb_button_grabs_num <= 0)
               {
                  free(_ecore_xcb_button_grabs);
                  _ecore_xcb_button_grabs = NULL;
                  return;
               }

             t = realloc(_ecore_xcb_button_grabs,
                         _ecore_xcb_button_grabs_num * sizeof(Ecore_X_Window));
             if (!t) return;
             _ecore_xcb_button_grabs = t;
          }
     }
}

void
_ecore_xcb_window_key_grab_remove(Ecore_X_Window win)
{
   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   if (_ecore_xcb_key_grabs_num > 0)
     {
        int i = 0, shuffle = 0;

        for (i = 0; i < _ecore_xcb_key_grabs_num; i++)
          {
             if (shuffle)
               _ecore_xcb_key_grabs[i - 1] = _ecore_xcb_key_grabs[i];

             if ((!shuffle) && (_ecore_xcb_key_grabs[i] == win))
               shuffle = 1;
          }

        if (shuffle)
          {
             Ecore_X_Window *t;

             _ecore_xcb_key_grabs_num--;
             if (_ecore_xcb_key_grabs_num <= 0)
               {
                  free(_ecore_xcb_key_grabs);
                  _ecore_xcb_key_grabs = NULL;
                  return;
               }

             t = realloc(_ecore_xcb_key_grabs,
                         _ecore_xcb_key_grabs_num * sizeof(Ecore_X_Window));
             if (!t) return;
             _ecore_xcb_key_grabs = t;
          }
     }
}

void
_ecore_xcb_window_grab_allow_events(Ecore_X_Window event_win,
                                    Ecore_X_Window child_win,
                                    int            type,
                                    void          *event,
                                    Ecore_X_Time   timestamp)
{
   int i = 0;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   CHECK_XCB_CONN;

   for (i = 0; i < _ecore_xcb_button_grabs_num; i++)
     {
        if ((_ecore_xcb_button_grabs[i] == event_win) ||
            (_ecore_xcb_button_grabs[i] == child_win))
          {
             Eina_Bool replay = EINA_FALSE;

             if (_ecore_xcb_window_grab_replay_func)
               {
                  replay =
                    _ecore_xcb_window_grab_replay_func(_ecore_xcb_window_grab_replay_data,
                                                       type, event);
               }
             if (replay)
               {
                  xcb_allow_events(_ecore_xcb_conn,
                                   XCB_ALLOW_REPLAY_POINTER, timestamp);
               }
             else
               {
                  xcb_allow_events(_ecore_xcb_conn,
                                   XCB_ALLOW_ASYNC_POINTER, timestamp);
               }
             break;
          }
     }
}

static int
_ecore_xcb_window_modifiers_get(unsigned int state)
{
   int xmodifiers = 0;

   if (state & ECORE_EVENT_MODIFIER_SHIFT)
     xmodifiers |= ECORE_X_MODIFIER_SHIFT;
   if (state & ECORE_EVENT_MODIFIER_CTRL)
     xmodifiers |= ECORE_X_MODIFIER_CTRL;
   if (state & ECORE_EVENT_MODIFIER_ALT)
     xmodifiers |= ECORE_X_MODIFIER_ALT;
   if (state & ECORE_EVENT_MODIFIER_WIN)
     xmodifiers |= ECORE_X_MODIFIER_WIN;
   if (state & ECORE_EVENT_MODIFIER_ALTGR)
     xmodifiers |= ECORE_X_MODIFIER_ALTGR;
   if (state & ECORE_EVENT_LOCK_SCROLL)
     xmodifiers |= ECORE_X_LOCK_SCROLL;
   if (state & ECORE_EVENT_LOCK_NUM)
     xmodifiers |= ECORE_X_LOCK_NUM;
   if (state & ECORE_EVENT_LOCK_CAPS)
     xmodifiers |= ECORE_X_LOCK_CAPS;
   if (state & ECORE_EVENT_LOCK_SHIFT)
     xmodifiers |= ECORE_X_LOCK_SHIFT;

   return xmodifiers;
}

static xcb_visualtype_t *
_ecore_xcb_window_find_visual_by_id(xcb_visualid_t id)
{
   xcb_depth_iterator_t diter;
   xcb_visualtype_iterator_t viter;

   CHECK_XCB_CONN;
   diter = xcb_screen_allowed_depths_iterator(_ecore_xcb_screen);
   for (; diter.rem; xcb_depth_next(&diter))
     {
        viter = xcb_depth_visuals_iterator(diter.data);
        for (; viter.rem; xcb_visualtype_next(&viter))
          {
             if (viter.data->visual_id == id)
               return viter.data;
          }
     }
   return 0;
}

#ifdef ECORE_XCB_XPRINT
static xcb_screen_t *
_ecore_xcb_window_screen_of_display(int screen)
{
   xcb_screen_iterator_t iter;

   CHECK_XCB_CONN;
   iter = xcb_setup_roots_iterator(xcb_get_setup(_ecore_xcb_conn));
   for (; iter.rem; --screen, xcb_screen_next(&iter))
     if (screen == 0)
       return iter.data;

   return NULL;
}

#endif