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

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/WindowStyle.hpp> // important to be included first (conflict with None)
#include <SFML/Window/Unix/WindowImplX11.hpp>
#include <SFML/Window/Unix/Display.hpp>
#include <SFML/Window/Unix/ScopedXcbPtr.hpp>
#include <SFML/System/Utf.hpp>
#include <SFML/System/Err.hpp>
#include <SFML/System/Mutex.hpp>
#include <SFML/System/Lock.hpp>
#include <xcb/xcb_image.h>
#include <xcb/randr.h>
#include <X11/Xlibint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>

// So we don't have to require xcb dri2 to be present
#define XCB_DRI2_BUFFER_SWAP_COMPLETE 0
#define XCB_DRI2_INVALIDATE_BUFFERS   1

#ifdef SFML_OPENGL_ES
    #include <SFML/Window/EglContext.hpp>
    typedef sf::priv::EglContext ContextType;
#else
    #include <SFML/Window/Unix/GlxContext.hpp>
    typedef sf::priv::GlxContext ContextType;
#endif

////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
    sf::priv::WindowImplX11*              fullscreenWindow = NULL;
    std::vector<sf::priv::WindowImplX11*> allWindows;
    sf::Mutex                             allWindowsMutex;
    sf::String                            windowManagerName;

    bool mapBuilt = false;

    // We use a simple array instead of a map => constant time lookup
    // xcb_keycode_t can only contain 256 distinct values
    sf::Keyboard::Key sfKeyMap[256];

    static const unsigned long            eventMask = XCB_EVENT_MASK_FOCUS_CHANGE   | XCB_EVENT_MASK_BUTTON_PRESS     |
                                                      XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_BUTTON_MOTION    |
                                                      XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_KEY_PRESS        |
                                                      XCB_EVENT_MASK_KEY_RELEASE    | XCB_EVENT_MASK_STRUCTURE_NOTIFY |
                                                      XCB_EVENT_MASK_ENTER_WINDOW   | XCB_EVENT_MASK_LEAVE_WINDOW;

    // Find the name of the current executable
    std::string findExecutableName()
    {
        // We use /proc/self/cmdline to get the command line
        // the user used to invoke this instance of the application
        int file = ::open("/proc/self/cmdline", O_RDONLY | O_NONBLOCK);

        if (file < 0)
            return "sfml";

        std::vector<char> buffer(256, 0);
        std::size_t offset = 0;
        ssize_t result = 0;

        while ((result = read(file, &buffer[offset], 256)) > 0)
        {
            buffer.resize(buffer.size() + result, 0);
            offset += result;
        }

        ::close(file);

        if (offset)
        {
            buffer[offset] = 0;

            // Remove the path to keep the executable name only
            return basename(&buffer[0]);
        }

        // Default fallback name
        return "sfml";
    }

    // Check if Extended Window Manager Hints are supported
    bool ewmhSupported()
    {
        static bool checked = false;
        static bool ewmhSupported = false;

        if (checked)
            return ewmhSupported;

        checked = true;

        xcb_connection_t* connection = sf::priv::OpenConnection();

        xcb_atom_t netSupportingWmCheck = sf::priv::getAtom("_NET_SUPPORTING_WM_CHECK", true);
        xcb_atom_t netSupported = sf::priv::getAtom("_NET_SUPPORTED", true);

        if (!netSupportingWmCheck || !netSupported)
            return false;

        sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);

        sf::priv::ScopedXcbPtr<xcb_get_property_reply_t> rootSupportingWindow(xcb_get_property_reply(
            connection,
            xcb_get_property(
                connection,
                0,
                sf::priv::XCBDefaultRootWindow(connection),
                netSupportingWmCheck,
                XCB_ATOM_WINDOW,
                0,
                1
            ),
            &error
        ));

        if (!rootSupportingWindow || rootSupportingWindow->length != 1)
        {
            sf::priv::CloseConnection(connection);
            return false;
        }

        xcb_window_t* rootWindow = reinterpret_cast<xcb_window_t*>(xcb_get_property_value(rootSupportingWindow.get()));

        if (!rootWindow)
        {
            sf::priv::CloseConnection(connection);
            return false;
        }

        sf::priv::ScopedXcbPtr<xcb_get_property_reply_t> childSupportingWindow(xcb_get_property_reply(
            connection,
            xcb_get_property(
                connection,
                0,
                *rootWindow,
                netSupportingWmCheck,
                XCB_ATOM_WINDOW,
                0,
                1
            ),
            &error
        ));

        if (!childSupportingWindow || childSupportingWindow->length != 1)
        {
            sf::priv::CloseConnection(connection);
            return false;
        }

        xcb_window_t* childWindow = reinterpret_cast<xcb_window_t*>(xcb_get_property_value(childSupportingWindow.get()));

        if (!childWindow)
        {
            sf::priv::CloseConnection(connection);
            return false;
        }

        // Conforming window managers should return the same window for both queries
        if (*rootWindow != *childWindow)
        {
            sf::priv::CloseConnection(connection);
            return false;
        }

        ewmhSupported = true;

        // We try to get the name of the window manager
        // for window manager specific workarounds
        xcb_atom_t netWmName = sf::priv::getAtom("_NET_WM_NAME", true);
        xcb_atom_t utf8StringType = sf::priv::getAtom("UTF8_STRING");

        if (!utf8StringType)
            utf8StringType = XCB_ATOM_STRING;

        if (!netWmName)
        {
            sf::priv::CloseConnection(connection);
            return true;
        }

        sf::priv::ScopedXcbPtr<xcb_get_property_reply_t> wmName(xcb_get_property_reply(
            connection,
            xcb_get_property(
                connection,
                0,
                *childWindow,
                netWmName,
                utf8StringType,
                0,
                0x7fffffff
            ),
            &error
        ));

        sf::priv::CloseConnection(connection);

        const char* name = reinterpret_cast<const char*>(xcb_get_property_value(wmName.get()));

        windowManagerName = name;

        return true;
    }

    xcb_query_extension_reply_t getDriExtension()
    {
        xcb_connection_t* connection = sf::priv::OpenConnection();

        sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);

        // Check if the DRI2 extension is present
        // We don't use xcb_get_extension_data here to avoid having to link to xcb_dri2
        static const std::string DRI2 = "DRI2";
        sf::priv::ScopedXcbPtr<xcb_query_extension_reply_t> driExt(xcb_query_extension_reply(
            connection,
            xcb_query_extension(
                connection,
                DRI2.size(),
                DRI2.c_str()
            ),
            &error
        ));

        // Close the connection with the X server
        sf::priv::CloseConnection(connection);

        if (error || !driExt || !driExt->present)
        {
            xcb_query_extension_reply_t reply;
            std::memset(&reply, 0, sizeof(reply));
            return reply;
        }

        return *driExt.get();
    }

    void dumpXcbExtensions()
    {
        xcb_connection_t* connection = sf::priv::OpenConnection();

        sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);

        // Check if the RandR extension is present
        sf::priv::ScopedXcbPtr<xcb_list_extensions_reply_t> extensions(xcb_list_extensions_reply(
            connection,
            xcb_list_extensions(
                connection
            ),
            &error
        ));

        if (error || !extensions)
        {
            // Close the connection with the X server
            sf::priv::CloseConnection(connection);

            sf::err() << "Couldn't get list of X extensions" << std::endl;
            return;
        }

        xcb_str_iterator_t iter = xcb_list_extensions_names_iterator(extensions.get());

        sf::err() << "X Extensions:" << std::endl;

        while (iter.rem)
        {
            std::string name(xcb_str_name(iter.data), xcb_str_name_length(iter.data));

            sf::priv::ScopedXcbPtr<xcb_generic_error_t> error(NULL);

            // Check if the RandR extension is present
            sf::priv::ScopedXcbPtr<xcb_query_extension_reply_t> extension(xcb_query_extension_reply(
                connection,
                xcb_query_extension(
                    connection,
                    name.size(),
                    name.c_str()
                ),
                &error
            ));

            if (error || !extension || !extension->present)
            {
                sf::err() << "\t" << name << " - Failed to query" << std::endl;
                continue;
            }

            int firstEvent = extension->first_event;

            sf::err() << "\t" << name << " - First event: " << firstEvent << std::endl;
            iter.data += xcb_str_name_length(iter.data) + 1;
            --iter.rem;
        }

        // Close the connection with the X server
        sf::priv::CloseConnection(connection);
    }

    void dumpUnhandledEvent(uint8_t type)
    {
        static std::vector<uint8_t> types;

        // Check if we already reported this type
        if (std::find(types.begin(), types.end(), type) != types.end())
            return;

        // Insert it if new
        types.push_back(type);

        static bool dumpedExtensions = false;

        if (!dumpedExtensions)
        {
            dumpXcbExtensions();
            dumpedExtensions = true;
        }

        sf::err() << "Unhandled event type: " << (static_cast<int>(type) & ~0x80) << std::endl
                  << "Report this to the SFML maintainers if possible" << std::endl;
    }

    xcb_keysym_t getKeysymFromKeycode(xcb_keycode_t keycode)
    {
        return sf::priv::getKeysymMap()[keycode];
    }

    sf::Keyboard::Key keysymToSF(xcb_keysym_t symbol)
    {
        switch (symbol)
        {
            case XK_Shift_L:      return sf::Keyboard::LShift;
            case XK_Shift_R:      return sf::Keyboard::RShift;
            case XK_Control_L:    return sf::Keyboard::LControl;
            case XK_Control_R:    return sf::Keyboard::RControl;
            case XK_Alt_L:        return sf::Keyboard::LAlt;
            case XK_Alt_R:        return sf::Keyboard::RAlt;
            case XK_Super_L:      return sf::Keyboard::LSystem;
            case XK_Super_R:      return sf::Keyboard::RSystem;
            case XK_Menu:         return sf::Keyboard::Menu;
            case XK_Escape:       return sf::Keyboard::Escape;
            case XK_semicolon:    return sf::Keyboard::SemiColon;
            case XK_slash:        return sf::Keyboard::Slash;
            case XK_equal:        return sf::Keyboard::Equal;
            case XK_minus:        return sf::Keyboard::Dash;
            case XK_bracketleft:  return sf::Keyboard::LBracket;
            case XK_bracketright: return sf::Keyboard::RBracket;
            case XK_comma:        return sf::Keyboard::Comma;
            case XK_period:       return sf::Keyboard::Period;
            case XK_apostrophe:   return sf::Keyboard::Quote;
            case XK_backslash:    return sf::Keyboard::BackSlash;
            case XK_grave:        return sf::Keyboard::Tilde;
            case XK_space:        return sf::Keyboard::Space;
            case XK_Return:       return sf::Keyboard::Return;
            case XK_KP_Enter:     return sf::Keyboard::Return;
            case XK_BackSpace:    return sf::Keyboard::BackSpace;
            case XK_Tab:          return sf::Keyboard::Tab;
            case XK_Prior:        return sf::Keyboard::PageUp;
            case XK_Next:         return sf::Keyboard::PageDown;
            case XK_End:          return sf::Keyboard::End;
            case XK_Home:         return sf::Keyboard::Home;
            case XK_Insert:       return sf::Keyboard::Insert;
            case XK_Delete:       return sf::Keyboard::Delete;
            case XK_KP_Add:       return sf::Keyboard::Add;
            case XK_KP_Subtract:  return sf::Keyboard::Subtract;
            case XK_KP_Multiply:  return sf::Keyboard::Multiply;
            case XK_KP_Divide:    return sf::Keyboard::Divide;
            case XK_Pause:        return sf::Keyboard::Pause;
            case XK_F1:           return sf::Keyboard::F1;
            case XK_F2:           return sf::Keyboard::F2;
            case XK_F3:           return sf::Keyboard::F3;
            case XK_F4:           return sf::Keyboard::F4;
            case XK_F5:           return sf::Keyboard::F5;
            case XK_F6:           return sf::Keyboard::F6;
            case XK_F7:           return sf::Keyboard::F7;
            case XK_F8:           return sf::Keyboard::F8;
            case XK_F9:           return sf::Keyboard::F9;
            case XK_F10:          return sf::Keyboard::F10;
            case XK_F11:          return sf::Keyboard::F11;
            case XK_F12:          return sf::Keyboard::F12;
            case XK_F13:          return sf::Keyboard::F13;
            case XK_F14:          return sf::Keyboard::F14;
            case XK_F15:          return sf::Keyboard::F15;
            case XK_Left:         return sf::Keyboard::Left;
            case XK_Right:        return sf::Keyboard::Right;
            case XK_Up:           return sf::Keyboard::Up;
            case XK_Down:         return sf::Keyboard::Down;
            case XK_KP_0:         return sf::Keyboard::Numpad0;
            case XK_KP_1:         return sf::Keyboard::Numpad1;
            case XK_KP_2:         return sf::Keyboard::Numpad2;
            case XK_KP_3:         return sf::Keyboard::Numpad3;
            case XK_KP_4:         return sf::Keyboard::Numpad4;
            case XK_KP_5:         return sf::Keyboard::Numpad5;
            case XK_KP_6:         return sf::Keyboard::Numpad6;
            case XK_KP_7:         return sf::Keyboard::Numpad7;
            case XK_KP_8:         return sf::Keyboard::Numpad8;
            case XK_KP_9:         return sf::Keyboard::Numpad9;
            case XK_a:            return sf::Keyboard::A;
            case XK_b:            return sf::Keyboard::B;
            case XK_c:            return sf::Keyboard::C;
            case XK_d:            return sf::Keyboard::D;
            case XK_e:            return sf::Keyboard::E;
            case XK_f:            return sf::Keyboard::F;
            case XK_g:            return sf::Keyboard::G;
            case XK_h:            return sf::Keyboard::H;
            case XK_i:            return sf::Keyboard::I;
            case XK_j:            return sf::Keyboard::J;
            case XK_k:            return sf::Keyboard::K;
            case XK_l:            return sf::Keyboard::L;
            case XK_m:            return sf::Keyboard::M;
            case XK_n:            return sf::Keyboard::N;
            case XK_o:            return sf::Keyboard::O;
            case XK_p:            return sf::Keyboard::P;
            case XK_q:            return sf::Keyboard::Q;
            case XK_r:            return sf::Keyboard::R;
            case XK_s:            return sf::Keyboard::S;
            case XK_t:            return sf::Keyboard::T;
            case XK_u:            return sf::Keyboard::U;
            case XK_v:            return sf::Keyboard::V;
            case XK_w:            return sf::Keyboard::W;
            case XK_x:            return sf::Keyboard::X;
            case XK_y:            return sf::Keyboard::Y;
            case XK_z:            return sf::Keyboard::Z;
            case XK_0:            return sf::Keyboard::Num0;
            case XK_1:            return sf::Keyboard::Num1;
            case XK_2:            return sf::Keyboard::Num2;
            case XK_3:            return sf::Keyboard::Num3;
            case XK_4:            return sf::Keyboard::Num4;
            case XK_5:            return sf::Keyboard::Num5;
            case XK_6:            return sf::Keyboard::Num6;
            case XK_7:            return sf::Keyboard::Num7;
            case XK_8:            return sf::Keyboard::Num8;
            case XK_9:            return sf::Keyboard::Num9;
        }

        return sf::Keyboard::Unknown;
    }

    void buildMap()
    {
        for (xcb_keycode_t i = 0; ; ++i)
        {
            sfKeyMap[i] = keysymToSF(getKeysymFromKeycode(i));

            if (i == 255)
                break;
        }

        mapBuilt = true;
    }

    sf::Keyboard::Key keycodeToSF(xcb_keycode_t keycode)
    {
        if (!mapBuilt)
            buildMap();

        return sfKeyMap[keycode];
    }
}


namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
WindowImplX11::WindowImplX11(WindowHandle handle) :
m_window         (0),
m_inputMethod    (NULL),
m_inputContext   (NULL),
m_isExternal     (true),
m_hiddenCursor   (0),
m_keyRepeat      (true),
m_previousSize   (-1, -1),
m_useSizeHints   (false),
m_fullscreen     (false)
{
    // Open a connection with the X server
    m_display = OpenDisplay();
    m_connection = XGetXCBConnection(m_display);

    std::memset(&m_oldVideoMode, 0, sizeof(m_oldVideoMode));

    if (!m_connection)
    {
        err() << "Failed cast Display object to an XCB connection object" << std::endl;
        return;
    }

    // Make sure to check for EWMH support before we do anything
    ewmhSupported();

    m_screen = XCBDefaultScreen(m_connection);
    XSetEventQueueOwner(m_display, XCBOwnsEventQueue);

    // Save the window handle
    m_window = handle;

    if (m_window)
    {
        // Make sure the window is listening to all the required events
        const uint32_t value_list[] = {static_cast<uint32_t>(eventMask)};

        xcb_change_window_attributes(
            m_connection,
            m_window,
            XCB_CW_EVENT_MASK,
            value_list
        );

        // Set the WM protocols
        setProtocols();

        // Do some common initializations
        initialize();
    }
}


////////////////////////////////////////////////////////////
WindowImplX11::WindowImplX11(VideoMode mode, const String& title, unsigned long style, const ContextSettings& settings) :
m_window         (0),
m_inputMethod    (NULL),
m_inputContext   (NULL),
m_isExternal     (false),
m_hiddenCursor   (0),
m_keyRepeat      (true),
m_previousSize   (-1, -1),
m_useSizeHints   (false),
m_fullscreen     ((style & Style::Fullscreen) != 0)
{
    // Open a connection with the X server
    m_display = OpenDisplay();
    m_connection = XGetXCBConnection(m_display);

    std::memset(&m_oldVideoMode, 0, sizeof(m_oldVideoMode));

    if (!m_connection)
    {
        err() << "Failed cast Display object to an XCB connection object" << std::endl;
        return;
    }

    // Make sure to check for EWMH support before we do anything
    ewmhSupported();

    m_screen = XCBDefaultScreen(m_connection);
    XSetEventQueueOwner(m_display, XCBOwnsEventQueue);

    // Compute position and size
    int left = m_fullscreen ? 0 : (m_screen->width_in_pixels  - mode.width) / 2;
    int top = m_fullscreen ? 0 : (m_screen->height_in_pixels - mode.height) / 2;
    int width  = mode.width;
    int height = mode.height;

    // Choose the visual according to the context settings
    XVisualInfo visualInfo = ContextType::selectBestVisual(m_display, mode.bitsPerPixel, settings);

    // Define the window attributes
    xcb_colormap_t colormap = xcb_generate_id(m_connection);
    xcb_create_colormap(m_connection, XCB_COLORMAP_ALLOC_NONE, colormap, m_screen->root, visualInfo.visualid);
    const uint32_t value_list[] = {m_fullscreen && !ewmhSupported(), static_cast<uint32_t>(eventMask), colormap};

    // Create the window
    m_window = xcb_generate_id(m_connection);

    ScopedXcbPtr<xcb_generic_error_t> errptr(xcb_request_check(
        m_connection,
        xcb_create_window_checked(
            m_connection,
            static_cast<uint8_t>(visualInfo.depth),
            m_window,
            m_screen->root,
            left, top,
            width, height,
            0,
            XCB_WINDOW_CLASS_INPUT_OUTPUT,
            visualInfo.visualid,
            XCB_CW_EVENT_MASK | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_COLORMAP,
            value_list
        )
    ));

    if (errptr)
    {
        err() << "Failed to create window" << std::endl;
        return;
    }

    // Set the WM protocols
    setProtocols();

    // Set the WM initial state to the normal state
    WMHints hints;
    std::memset(&hints, 0, sizeof(hints));
    hints.initial_state = 1;
    hints.flags |= 1 << 1;
    setWMHints(hints);

    // If not in fullscreen, set the window's style (tell the window manager to
    // change our window's decorations and functions according to the requested style)
    if (!m_fullscreen)
        setMotifHints(style);

    WMSizeHints sizeHints;
    std::memset(&sizeHints, 0, sizeof(sizeHints));

    // This is a hack to force some windows managers to disable resizing
    // Fullscreen is bugged on Openbox. Unless size hints are set, there
    // will be a region of the window that is off-screen. We try to workaround
    // this by setting size hints even in fullscreen just for Openbox.
    if ((!m_fullscreen || (windowManagerName == "Openbox")) && !(style & Style::Resize))
    {
        m_useSizeHints = true;
        sizeHints.flags     |= ((1 << 4) | (1 << 5));
        sizeHints.min_width  = width;
        sizeHints.max_width  = width;
        sizeHints.min_height = height;
        sizeHints.max_height = height;
    }

    // Set the WM hints of the normal state
    setWMSizeHints(sizeHints);

    // Set the window's WM class (this can be used by window managers)
    // The WM_CLASS property actually consists of 2 parts,
    // the instance name and the class name both of which should be
    // null terminated strings.
    // The instance name should be something unique to this invokation
    // of the application but is rarely if ever used these days.
    // For simplicity, we retrieve it via the base executable name.
    // The class name identifies a class of windows that
    // "are of the same type". We simply use the initial window name as
    // the class name.
    std::string windowClass = findExecutableName();
    windowClass += '\0'; // Important to separate instance from class
    windowClass += title.toAnsiString();

    // We add 1 to the size of the string to include the null at the end
    if (!changeWindowProperty(XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, windowClass.size() + 1, windowClass.c_str()))
        sf::err() << "Failed to set WM_CLASS property" << std::endl;

    // Set the window's name
    setTitle(title);

    // Do some common initializations
    initialize();

    // Set fullscreen video mode and switch to fullscreen if necessary
    if (m_fullscreen)
    {
        setPosition(Vector2i(0, 0));
        setVideoMode(mode);
        switchToFullscreen();
    }
}


////////////////////////////////////////////////////////////
WindowImplX11::~WindowImplX11()
{
    // Cleanup graphical resources
    cleanup();

    // Destroy the cursor
    if (m_hiddenCursor)
        xcb_free_cursor(m_connection, m_hiddenCursor);

    // Destroy the input context
    if (m_inputContext)
        XDestroyIC(m_inputContext);
    // Destroy the window
    if (m_window && !m_isExternal)
    {
        xcb_destroy_window(m_connection, m_window);
        xcb_flush(m_connection);
    }

    // Close the input method
    if (m_inputMethod)
        XCloseIM(m_inputMethod);

    // Close the connection with the X server
    CloseDisplay(m_display);

    // Remove this window from the global list of windows (required for focus request)
    Lock lock(allWindowsMutex);
    allWindows.erase(std::find(allWindows.begin(), allWindows.end(), this));
}


////////////////////////////////////////////////////////////
WindowHandle WindowImplX11::getSystemHandle() const
{
    return m_window;
}


////////////////////////////////////////////////////////////
void WindowImplX11::processEvents()
{
    // Key repeat workaround: If key repeat is enabled, XCB will spawn two
    // events for each repeat interval: key release and key press. Both have
    // the same timestamp and key code. We are holding back the release event
    // to check for the matching key press event and if so, discard the release
    // event.

    xcb_generic_event_t* event = NULL;
    xcb_key_release_event_t* lastKeyReleaseEvent = NULL;
    uint8_t eventType = 0;

    if (!m_xcbEvents.empty())
    {
        event = m_xcbEvents.front();
        m_xcbEvents.pop_front();
    }
    else
    {
        event = xcb_poll_for_event(m_connection);
    }

    while (event)
    {
        eventType = event->response_type & ~0x80;

        // Key was pressed and one has been released prior to that.
        if (eventType == XCB_KEY_PRESS && lastKeyReleaseEvent)
        {
            // If the key press event matches the held back key release event,
            // then we have a key repeat and discard the held back release
            // event.
            if (lastKeyReleaseEvent->time == reinterpret_cast<xcb_key_press_event_t*>(event)->time &&
                lastKeyReleaseEvent->detail == reinterpret_cast<xcb_key_press_event_t*>(event)->detail)
            {
                free(lastKeyReleaseEvent);
                lastKeyReleaseEvent = NULL;

                // If key repeat is disabled, discard the matching key press event as well
                if (!m_keyRepeat)
                {
                    free(event);

                    if (!m_xcbEvents.empty())
                    {
                        event = m_xcbEvents.front();
                        m_xcbEvents.pop_front();
                    }
                    else
                    {
                        event = xcb_poll_for_event(m_connection);
                    }

                    continue;
                }
            }
        }

        // If there's still a key release event held back, process it now.
        if (lastKeyReleaseEvent)
        {
            if (processEvent(reinterpret_cast<xcb_generic_event_t*>(lastKeyReleaseEvent)))
                free(lastKeyReleaseEvent);

            lastKeyReleaseEvent = NULL;
        }

        if (eventType == XCB_KEY_RELEASE)
        {
            // Check if we're in charge of the key release
            if (!passEvent(event, reinterpret_cast<xcb_key_release_event_t*>(event)->event))
            {
                // Remember this key release event.
                lastKeyReleaseEvent = reinterpret_cast<xcb_key_release_event_t*>(event);
                event = NULL; // For safety reasons.
            }
        }
        else
        {
            if (processEvent(event))
                free(event);
        }

        if (!m_xcbEvents.empty())
        {
            event = m_xcbEvents.front();
            m_xcbEvents.pop_front();
        }
        else
        {
            event = xcb_poll_for_event(m_connection);
        }
    }

    // Process any held back release event.
    if (lastKeyReleaseEvent)
    {
        if (processEvent(reinterpret_cast<xcb_generic_event_t*>(lastKeyReleaseEvent)))
            free(lastKeyReleaseEvent);

        lastKeyReleaseEvent = NULL;
    }
}


////////////////////////////////////////////////////////////
Vector2i WindowImplX11::getPosition() const
{
    ::Window topLevelWindow = m_window;
    ::Window nextWindow = topLevelWindow;

    ScopedXcbPtr<xcb_generic_error_t> error(NULL);

    // Get "top level" window, i.e. the window with the root window as its parent.
    while (nextWindow != m_screen->root)
    {
        topLevelWindow = nextWindow;

        ScopedXcbPtr<xcb_query_tree_reply_t> treeReply(xcb_query_tree_reply(
            m_connection,
            xcb_query_tree(
                m_connection,
                topLevelWindow
            ),
            &error
        ));

        if (error)
        {
            err() << "Failed to get window position (query_tree)" << std::endl;
            return Vector2i(0, 0);
        }

        nextWindow = treeReply->parent;
    }

    ScopedXcbPtr<xcb_get_geometry_reply_t> geometryReply(xcb_get_geometry_reply(
        m_connection,
        xcb_get_geometry(
            m_connection,
            topLevelWindow
        ),
        &error
    ));

    if (error)
    {
        err() << "Failed to get window position (get_geometry)" << std::endl;
        return Vector2i(0, 0);
    }

    return Vector2i(geometryReply->x, geometryReply->y);
}


////////////////////////////////////////////////////////////
void WindowImplX11::setPosition(const Vector2i& position)
{
    uint32_t values[] = {static_cast<uint32_t>(position.x), static_cast<uint32_t>(position.y)};
    xcb_configure_window(m_connection, m_window,
                         XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
                         values);
    xcb_flush(m_connection);
}


////////////////////////////////////////////////////////////
Vector2u WindowImplX11::getSize() const
{
    ScopedXcbPtr<xcb_get_geometry_reply_t> reply(xcb_get_geometry_reply(m_connection, xcb_get_geometry(m_connection, m_window), NULL));

    return Vector2u(reply->width, reply->height);
}


////////////////////////////////////////////////////////////
void WindowImplX11::setSize(const Vector2u& size)
{
    // If resizing is disable for the window we have to update the size hints (required by some window managers).
    if( m_useSizeHints ) {
        WMSizeHints sizeHints;
        std::memset(&sizeHints, 0, sizeof(sizeHints));

        sizeHints.flags     |= (1 << 4 | 1 << 5);
        sizeHints.min_width  = size.x;
        sizeHints.max_width  = size.x;
        sizeHints.min_height = size.y;
        sizeHints.max_height = size.y;

        setWMSizeHints(sizeHints);
    }

    uint32_t values[] = {size.x, size.y};

    ScopedXcbPtr<xcb_generic_error_t> configureWindowError(xcb_request_check(
        m_connection,
        xcb_configure_window(
            m_connection,
            m_window,
            XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
            values
        )
    ));

    if (configureWindowError)
        err() << "Failed to set window size" << std::endl;

    xcb_flush(m_connection);
}


////////////////////////////////////////////////////////////
void WindowImplX11::setTitle(const String& title)
{
    // XCB takes UTF-8-encoded strings.
    xcb_atom_t utf8StringType = getAtom("UTF8_STRING");

    if (!utf8StringType)
        utf8StringType = XCB_ATOM_STRING;

    std::string utf8String;
    Utf<32>::toUtf8(title.begin(), title.end(), std::back_inserter(utf8String));

    if (!changeWindowProperty(XCB_ATOM_WM_NAME, utf8StringType, 8, utf8String.length(), utf8String.c_str()))
        err() << "Failed to set window title" << std::endl;

    if (!changeWindowProperty(XCB_ATOM_WM_ICON_NAME, utf8StringType, 8, utf8String.length(), utf8String.c_str()))
        err() << "Failed to set WM_ICON_NAME property" << std::endl;

    if (ewmhSupported())
    {
        xcb_atom_t netWmName = getAtom("_NET_WM_NAME", true);
        xcb_atom_t netWmIconName = getAtom("_NET_WM_ICON_NAME", true);

        if (utf8StringType && netWmName)
        {
            if (!changeWindowProperty(netWmName, utf8StringType, 8, utf8String.length(), utf8String.c_str()))
                err() << "Failed to set _NET_WM_NAME property" << std::endl;
        }

        if (utf8StringType && netWmName)
        {
            if (!changeWindowProperty(netWmIconName, utf8StringType, 8, utf8String.length(), utf8String.c_str()))
                err() << "Failed to set _NET_WM_ICON_NAME property" << std::endl;
        }
    }

    xcb_flush(m_connection);
}


////////////////////////////////////////////////////////////
void WindowImplX11::setIcon(unsigned int width, unsigned int height, const Uint8* pixels)
{
    // X11 wants BGRA pixels: swap red and blue channels
    Uint8 iconPixels[width * height * 4];
    for (std::size_t i = 0; i < width * height; ++i)
    {
        iconPixels[i * 4 + 0] = pixels[i * 4 + 2];
        iconPixels[i * 4 + 1] = pixels[i * 4 + 1];
        iconPixels[i * 4 + 2] = pixels[i * 4 + 0];
        iconPixels[i * 4 + 3] = pixels[i * 4 + 3];
    }

    // Create the icon pixmap
    xcb_pixmap_t iconPixmap = xcb_generate_id(m_connection);

    ScopedXcbPtr<xcb_generic_error_t> createPixmapError(xcb_request_check(
        m_connection,
        xcb_create_pixmap_checked(
            m_connection,
            m_screen->root_depth,
            iconPixmap,
            m_screen->root,
            width,
            height
        )
    ));

    if (createPixmapError)
    {
        err() << "Failed to set the window's icon (create_pixmap): ";
        err() << "Error code " << static_cast<int>(createPixmapError->error_code) << std::endl;
        return;
    }

    xcb_gcontext_t iconGC = xcb_generate_id(m_connection);

    ScopedXcbPtr<xcb_generic_error_t> createGcError(xcb_request_check(
        m_connection,
        xcb_create_gc(
            m_connection,
            iconGC,
            iconPixmap,
            0,
            NULL
        )
    ));

    if (createGcError)
    {
        err() << "Failed to set the window's icon (create_gc): ";
        err() << "Error code " << static_cast<int>(createGcError->error_code) << std::endl;
        return;
    }

    ScopedXcbPtr<xcb_generic_error_t> putImageError(xcb_request_check(
        m_connection,
        xcb_put_image_checked(
            m_connection,
            XCB_IMAGE_FORMAT_Z_PIXMAP,
            iconPixmap,
            iconGC,
            width,
            height,
            0,
            0,
            0,
            m_screen->root_depth,
            sizeof(iconPixels),
            iconPixels
        )
    ));

    ScopedXcbPtr<xcb_generic_error_t> freeGcError(xcb_request_check(
        m_connection,
        xcb_free_gc(
            m_connection,
            iconGC
        )
    ));

    if (freeGcError)
    {
        err() << "Failed to free icon GC: ";
        err() << "Error code " << static_cast<int>(freeGcError->error_code) << std::endl;
    }

    if (putImageError)
    {
        err() << "Failed to set the window's icon (put_image): ";
        err() << "Error code " << static_cast<int>(putImageError->error_code) << std::endl;
        return;
    }

    // Create the mask pixmap (must have 1 bit depth)
    std::size_t pitch = (width + 7) / 8;
    static std::vector<Uint8> maskPixels(pitch * height, 0);
    for (std::size_t j = 0; j < height; ++j)
    {
        for (std::size_t i = 0; i < pitch; ++i)
        {
            for (std::size_t k = 0; k < 8; ++k)
            {
                if (i * 8 + k < width)
                {
                    Uint8 opacity = (pixels[(i * 8 + k + j * width) * 4 + 3] > 0) ? 1 : 0;
                    maskPixels[i + j * pitch] |= (opacity << k);
                }
            }
        }
    }

    xcb_pixmap_t maskPixmap = xcb_create_pixmap_from_bitmap_data(
        m_connection,
        m_window,
        reinterpret_cast<uint8_t*>(&maskPixels[0]),
        width,
        height,
        1,
        0,
        1,
        NULL
    );

    // Send our new icon to the window through the WMHints
    WMHints hints;
    std::memset(&hints, 0, sizeof(hints));
    hints.flags      |= ((1 << 2) | (1 << 5));
    hints.icon_pixmap = iconPixmap;
    hints.icon_mask   = maskPixmap;

    setWMHints(hints);

    xcb_flush(m_connection);

    ScopedXcbPtr<xcb_generic_error_t> freePixmapError(xcb_request_check(
        m_connection,
        xcb_free_pixmap_checked(
            m_connection,
            iconPixmap
        )
    ));

    if (freePixmapError)
    {
        err() << "Failed to free icon pixmap: ";
        err() << "Error code " << static_cast<int>(freePixmapError->error_code) << std::endl;
    }
}


////////////////////////////////////////////////////////////
void WindowImplX11::setVisible(bool visible)
{
    if (visible)
    {
        ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check(
            m_connection,
            xcb_map_window(
                m_connection,
                m_window
            )
        ));

        if (error)
            err() << "Failed to change window visibility" << std::endl;
    }
    else
    {
        ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check(
            m_connection,
            xcb_unmap_window(
                m_connection,
                m_window
            )
        ));

        if (error)
            err() << "Failed to change window visibility" << std::endl;
    }

    xcb_flush(m_connection);
}


////////////////////////////////////////////////////////////
void WindowImplX11::setMouseCursorVisible(bool visible)
{
    const uint32_t values = visible ? XCB_NONE : m_hiddenCursor;

    ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check(
        m_connection,
        xcb_change_window_attributes(
            m_connection,
            m_window,
            XCB_CW_CURSOR,
            &values
        )
    ));

    if (error)
        err() << "Failed to change mouse cursor visibility" << std::endl;

    xcb_flush(m_connection);
}


////////////////////////////////////////////////////////////
void WindowImplX11::setKeyRepeatEnabled(bool enabled)
{
    m_keyRepeat = enabled;
}


////////////////////////////////////////////////////////////
void WindowImplX11::requestFocus()
{
    // Focus is only stolen among SFML windows, not between applications
    // Check the global list of windows to find out whether an SFML window has the focus
    // Note: can't handle console and other non-SFML windows belonging to the application.
    bool sfmlWindowFocused = false;

    {
        Lock lock(allWindowsMutex);
        for (std::vector<WindowImplX11*>::iterator itr = allWindows.begin(); itr != allWindows.end(); ++itr)
        {
            if ((*itr)->hasFocus())
            {
                sfmlWindowFocused = true;
                break;
            }
        }
    }

    ScopedXcbPtr<xcb_generic_error_t> error(NULL);

    // Check if window is viewable (not on other desktop, ...)
    // TODO: Check also if minimized
    ScopedXcbPtr<xcb_get_window_attributes_reply_t> attributes(xcb_get_window_attributes_reply(
        m_connection,
        xcb_get_window_attributes(
            m_connection,
            m_window
        ),
        &error
    ));

    if (error || !attributes)
    {
        err() << "Failed to check if window is viewable while requesting focus" << std::endl;
        return; // error getting attribute
    }

    bool windowViewable = (attributes->map_state == XCB_MAP_STATE_VIEWABLE);

    if (sfmlWindowFocused && windowViewable)
    {
        // Another SFML window of this application has the focus and the current window is viewable:
        // steal focus (i.e. bring window to the front and give it input focus)
        grabFocus();
    }
    else
    {
        // Get current WM hints.
        ScopedXcbPtr<xcb_get_property_reply_t> hintsReply(xcb_get_property_reply(
            m_connection,
            xcb_get_property(m_connection, 0, m_window, XCB_ATOM_WM_HINTS, XCB_ATOM_WM_HINTS, 0, 9),
            &error
        ));

        if (error || !hintsReply)
        {
            err() << "Failed to get WM hints while requesting focus" << std::endl;
            return;
        }

        WMHints* hints = reinterpret_cast<WMHints*>(xcb_get_property_value(hintsReply.get()));

        if (!hints)
        {
            err() << "Failed to get WM hints while requesting focus" << std::endl;
            return;
        }

        // Even if no hints were returned, we can simply set the proper flags we need and go on. This is
        // different from Xlib where XAllocWMHints() has to be called.
        hints->flags |= (1 << 8);
        setWMHints(*hints);
    }
}


////////////////////////////////////////////////////////////
bool WindowImplX11::hasFocus() const
{
    ScopedXcbPtr<xcb_generic_error_t> error(NULL);

    ScopedXcbPtr<xcb_get_input_focus_reply_t> reply(xcb_get_input_focus_reply(
        m_connection,
        xcb_get_input_focus_unchecked(
            m_connection
        ),
        &error
    ));

    if (error)
        err() << "Failed to check if window has focus" << std::endl;

    return (reply->focus == m_window);
}


////////////////////////////////////////////////////////////
void WindowImplX11::grabFocus()
{
    xcb_atom_t netActiveWindow = XCB_ATOM_NONE;

    if (ewmhSupported())
        netActiveWindow = getAtom("_NET_ACTIVE_WINDOW");

    if (netActiveWindow)
    {
        xcb_client_message_event_t event;
        std::memset(&event, 0, sizeof(event));

        event.response_type = XCB_CLIENT_MESSAGE;
        event.window = m_window;
        event.format = 32;
        event.sequence = 0;
        event.type = netActiveWindow;
        event.data.data32[0] = 1; // Normal application
        event.data.data32[1] = XCB_CURRENT_TIME;
        event.data.data32[2] = 0; // We don't know the currently active window

        ScopedXcbPtr<xcb_generic_error_t> activeWindowError(xcb_request_check(
            m_connection,
            xcb_send_event_checked(
                m_connection,
                0,
                XCBDefaultRootWindow(m_connection),
                XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
                reinterpret_cast<char*>(&event)
            )
        ));

        if (activeWindowError)
            err() << "Setting fullscreen failed, could not send \"_NET_ACTIVE_WINDOW\" event" << std::endl;
    }
    else
    {
        ScopedXcbPtr<xcb_generic_error_t> setInputFocusError(xcb_request_check(
            m_connection,
            xcb_set_input_focus(
                m_connection,
                XCB_INPUT_FOCUS_POINTER_ROOT,
                m_window,
                XCB_CURRENT_TIME
            )
        ));

        if (setInputFocusError)
        {
            err() << "Failed to change active window (set_input_focus)" << std::endl;
            return;
        }

        const uint32_t values[] = {XCB_STACK_MODE_ABOVE};

        ScopedXcbPtr<xcb_generic_error_t> configureWindowError(xcb_request_check(
            m_connection,
            xcb_configure_window(
                m_connection,
                m_window,
                XCB_CONFIG_WINDOW_STACK_MODE,
                values
            )
        ));

        if (configureWindowError)
            err() << "Failed to change active window (configure_window)" << std::endl;
    }
}


////////////////////////////////////////////////////////////
void WindowImplX11::setVideoMode(const VideoMode& mode)
{
    // Skip mode switching if the new mode is equal to the desktop mode
    if (mode == VideoMode::getDesktopMode())
        return;

    ScopedXcbPtr<xcb_generic_error_t> error(NULL);

    // Check if the RandR extension is present
    const xcb_query_extension_reply_t* randrExt = xcb_get_extension_data(m_connection, &xcb_randr_id);

    if (!randrExt || !randrExt->present)
    {
        // RandR extension is not supported: we cannot use fullscreen mode
        err() << "Fullscreen is not supported, switching to window mode" << std::endl;
        return;
    }

    // Load RandR and check its version
    ScopedXcbPtr<xcb_randr_query_version_reply_t> randrVersion(xcb_randr_query_version_reply(
        m_connection,
        xcb_randr_query_version(
            m_connection,
            1,
            1
        ),
        &error
    ));

    if (error)
    {
        err() << "Failed to load RandR, switching to window mode" << std::endl;
        return;
    }

    // Get the current configuration
    ScopedXcbPtr<xcb_randr_get_screen_info_reply_t> config(xcb_randr_get_screen_info_reply(
        m_connection,
        xcb_randr_get_screen_info(
            m_connection,
            m_screen->root
        ),
        &error
    ));

    if (error || !config)
    {
        // Failed to get the screen configuration
        err() << "Failed to get the current screen configuration for fullscreen mode, switching to window mode" << std::endl;
        return;
    }

    // Save the current video mode before we switch to fullscreen
    m_oldVideoMode = *config.get();

    // Get the available screen sizes
    xcb_randr_screen_size_t* sizes = xcb_randr_get_screen_info_sizes(config.get());

    if (!sizes || !config->nSizes)
    {
        err() << "Failed to get the fullscreen sizes, switching to window mode" << std::endl;
        return;
    }

    // Search for a matching size
    for (int i = 0; i < config->nSizes; ++i)
    {
        if (config->rotation == XCB_RANDR_ROTATION_ROTATE_90 ||
            config->rotation == XCB_RANDR_ROTATION_ROTATE_270)
            std::swap(sizes[i].height, sizes[i].width);

        if ((sizes[i].width  == static_cast<int>(mode.width)) &&
            (sizes[i].height == static_cast<int>(mode.height)))
        {
            // Switch to fullscreen mode
            ScopedXcbPtr<xcb_randr_set_screen_config_reply_t> setScreenConfig(xcb_randr_set_screen_config_reply(
                m_connection,
                xcb_randr_set_screen_config(
                    m_connection,
                    config->root,
                    XCB_CURRENT_TIME,
                    config->config_timestamp,
                    i,
                    config->rotation,
                    config->rate
                ),
                &error
            ));

            if (error)
                err() << "Failed to set new screen configuration" << std::endl;

            // Set "this" as the current fullscreen window
            fullscreenWindow = this;
            return;
        }
    }

    err() << "Failed to find matching fullscreen size, switching to window mode" << std::endl;
}


////////////////////////////////////////////////////////////
void WindowImplX11::resetVideoMode()
{
    if (fullscreenWindow == this)
    {
        // Get current screen info
        ScopedXcbPtr<xcb_generic_error_t> error(NULL);

        // Reset the video mode
        ScopedXcbPtr<xcb_randr_set_screen_config_reply_t> setScreenConfig(xcb_randr_set_screen_config_reply(
            m_connection,
            xcb_randr_set_screen_config(
                m_connection,
                m_oldVideoMode.root,
                XCB_CURRENT_TIME,
                m_oldVideoMode.config_timestamp,
                m_oldVideoMode.sizeID,
                m_oldVideoMode.rotation,
                m_oldVideoMode.rate
            ),
            &error
        ));

        if (error)
            err() << "Failed to reset old screen configuration" << std::endl;

        // Reset the fullscreen window
        fullscreenWindow = NULL;
    }
}


////////////////////////////////////////////////////////////
void WindowImplX11::switchToFullscreen()
{
    grabFocus();

    if (ewmhSupported())
    {
        xcb_atom_t netWmBypassCompositor = getAtom("_NET_WM_BYPASS_COMPOSITOR");

        if (netWmBypassCompositor)
        {
            static const Uint32 bypassCompositor = 1;

            // Not being able to bypass the compositor is not a fatal error
            if (!changeWindowProperty(netWmBypassCompositor, XCB_ATOM_CARDINAL, 32, 1, &bypassCompositor))
                err() << "xcb_change_property failed, unable to set _NET_WM_BYPASS_COMPOSITOR" << std::endl;
        }

        xcb_atom_t netWmState = getAtom("_NET_WM_STATE", true);
        xcb_atom_t netWmStateFullscreen = getAtom("_NET_WM_STATE_FULLSCREEN", true);

        if (!netWmState || !netWmStateFullscreen)
        {
            err() << "Setting fullscreen failed. Could not get required atoms" << std::endl;
            return;
        }

        xcb_client_message_event_t event;
        std::memset(&event, 0, sizeof(event));

        event.response_type = XCB_CLIENT_MESSAGE;
        event.window = m_window;
        event.format = 32;
        event.sequence = 0;
        event.type = netWmState;
        event.data.data32[0] = 1; // _NET_WM_STATE_ADD
        event.data.data32[1] = netWmStateFullscreen;
        event.data.data32[2] = 0; // No second property
        event.data.data32[3] = 1; // Normal window

        ScopedXcbPtr<xcb_generic_error_t> wmStateError(xcb_request_check(
            m_connection,
            xcb_send_event_checked(
                m_connection,
                0,
                XCBDefaultRootWindow(m_connection),
                XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
                reinterpret_cast<char*>(&event)
            )
        ));

        if (wmStateError)
            err() << "Setting fullscreen failed. Could not send \"_NET_WM_STATE\" event" << std::endl;
    }
}


////////////////////////////////////////////////////////////
void WindowImplX11::setProtocols()
{
    xcb_atom_t wmProtocols = getAtom("WM_PROTOCOLS");
    xcb_atom_t wmDeleteWindow = getAtom("WM_DELETE_WINDOW");

    if (!wmProtocols)
    {
        err() << "Failed to request WM_PROTOCOLS atom." << std::endl;
        return;
    }

    std::vector<xcb_atom_t> atoms;

    if (wmDeleteWindow)
    {
        atoms.push_back(wmDeleteWindow);
    }
    else
    {
        err() << "Failed to request WM_DELETE_WINDOW atom." << std::endl;
    }

    xcb_atom_t netWmPing = XCB_ATOM_NONE;
    xcb_atom_t netWmPid = XCB_ATOM_NONE;

    if (ewmhSupported())
    {
        netWmPing = getAtom("_NET_WM_PING", true);
        netWmPid = getAtom("_NET_WM_PID", true);
    }

    ScopedXcbPtr<xcb_generic_error_t> error(NULL);

    if (netWmPing && netWmPid)
    {
        uint32_t pid = getpid();

        if (changeWindowProperty(netWmPid, XCB_ATOM_CARDINAL, 32, 1, &pid))
            atoms.push_back(netWmPing);
    }

    if (!atoms.empty())
    {
        if (!changeWindowProperty(wmProtocols, XCB_ATOM_ATOM, 32, atoms.size(), &atoms[0]))
            err() << "Failed to set window protocols" << std::endl;
    }
    else
    {
        err() << "Didn't set any window protocols" << std::endl;
    }
}


////////////////////////////////////////////////////////////
void WindowImplX11::setMotifHints(unsigned long style)
{
    ScopedXcbPtr<xcb_generic_error_t> error(NULL);

    static const std::string MOTIF_WM_HINTS = "_MOTIF_WM_HINTS";
    ScopedXcbPtr<xcb_intern_atom_reply_t> hintsAtomReply(xcb_intern_atom_reply(
        m_connection,
        xcb_intern_atom(
            m_connection,
            0,
            MOTIF_WM_HINTS.size(),
            MOTIF_WM_HINTS.c_str()
        ),
        &error
    ));

    if (!error && hintsAtomReply)
    {
        static const unsigned long MWM_HINTS_FUNCTIONS   = 1 << 0;
        static const unsigned long MWM_HINTS_DECORATIONS = 1 << 1;

        //static const unsigned long MWM_DECOR_ALL         = 1 << 0;
        static const unsigned long MWM_DECOR_BORDER      = 1 << 1;
        static const unsigned long MWM_DECOR_RESIZEH     = 1 << 2;
        static const unsigned long MWM_DECOR_TITLE       = 1 << 3;
        static const unsigned long MWM_DECOR_MENU        = 1 << 4;
        static const unsigned long MWM_DECOR_MINIMIZE    = 1 << 5;
        static const unsigned long MWM_DECOR_MAXIMIZE    = 1 << 6;

        //static const unsigned long MWM_FUNC_ALL          = 1 << 0;
        static const unsigned long MWM_FUNC_RESIZE       = 1 << 1;
        static const unsigned long MWM_FUNC_MOVE         = 1 << 2;
        static const unsigned long MWM_FUNC_MINIMIZE     = 1 << 3;
        static const unsigned long MWM_FUNC_MAXIMIZE     = 1 << 4;
        static const unsigned long MWM_FUNC_CLOSE        = 1 << 5;

        struct MotifWMHints
        {
            uint32_t flags;
            uint32_t functions;
            uint32_t decorations;
            int32_t  inputMode;
            uint32_t state;
        };

        MotifWMHints hints;
        hints.flags       = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
        hints.decorations = 0;
        hints.functions   = 0;
        hints.inputMode   = 0;
        hints.state       = 0;

        if (style & Style::Titlebar)
        {
            hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_TITLE | MWM_DECOR_MINIMIZE | MWM_DECOR_MENU;
            hints.functions   |= MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE;
        }
        if (style & Style::Resize)
        {
            hints.decorations |= MWM_DECOR_MAXIMIZE | MWM_DECOR_RESIZEH;
            hints.functions   |= MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE;
        }
        if (style & Style::Close)
        {
            hints.decorations |= 0;
            hints.functions   |= MWM_FUNC_CLOSE;
        }

        if (!changeWindowProperty(hintsAtomReply->atom, hintsAtomReply->atom, 32, 5, &hints))
            err() << "xcb_change_property failed, could not set window hints" << std::endl;
    }
    else
    {
        err() << "Failed to request _MOTIF_WM_HINTS atom." << std::endl;
    }
}


////////////////////////////////////////////////////////////
void WindowImplX11::setWMHints(const WMHints& hints)
{
    if (!changeWindowProperty(XCB_ATOM_WM_HINTS, XCB_ATOM_WM_HINTS, 32, sizeof(hints) / 4, &hints))
        sf::err() << "Failed to set WM_HINTS property" << std::endl;
}


////////////////////////////////////////////////////////////
void WindowImplX11::setWMSizeHints(const WMSizeHints& hints)
{
    if (!changeWindowProperty(XCB_ATOM_WM_NORMAL_HINTS, XCB_ATOM_WM_SIZE_HINTS, 32, sizeof(hints) / 4, &hints))
        sf::err() << "Failed to set XCB_ATOM_WM_NORMAL_HINTS property" << std::endl;
}


////////////////////////////////////////////////////////////
bool WindowImplX11::changeWindowProperty(xcb_atom_t property, xcb_atom_t type, uint8_t format, uint32_t length, const void* data)
{
    ScopedXcbPtr<xcb_generic_error_t> error(xcb_request_check(
        m_connection,
        xcb_change_property_checked(
            m_connection,
            XCB_PROP_MODE_REPLACE,
            m_window,
            property,
            type,
            format,
            length,
            data
        )
    ));

    return !error;
}


////////////////////////////////////////////////////////////
void WindowImplX11::initialize()
{
    // Create the input context
    m_inputMethod = XOpenIM(m_display, NULL, NULL, NULL);

    if (m_inputMethod)
    {
        m_inputContext = XCreateIC(
            m_inputMethod,
            XNClientWindow,
            m_window,
            XNFocusWindow,
            m_window,
            XNInputStyle,
            XIMPreeditNothing | XIMStatusNothing,
            reinterpret_cast<void*>(NULL)
        );
    }
    else
    {
        m_inputContext = NULL;
    }

    if (!m_inputContext)
        err() << "Failed to create input context for window -- TextEntered event won't be able to return unicode" << std::endl;

    // Show the window
    setVisible(true);

    // Raise the window and grab input focus
    grabFocus();

    // Create the hidden cursor
    createHiddenCursor();

    // Flush the commands queue
    xcb_flush(m_connection);

    // Add this window to the global list of windows (required for focus request)
    Lock lock(allWindowsMutex);
    allWindows.push_back(this);
}


////////////////////////////////////////////////////////////
void WindowImplX11::createHiddenCursor()
{
    xcb_pixmap_t cursorPixmap = xcb_generate_id(m_connection);

    // Create the cursor's pixmap (1x1 pixels)
    ScopedXcbPtr<xcb_generic_error_t> createPixmapError(xcb_request_check(
        m_connection,
        xcb_create_pixmap(
            m_connection,
            1,
            cursorPixmap,
            m_window,
            1,
            1
        )
    ));

    if (createPixmapError)
    {
        err() << "Failed to create pixmap for hidden cursor" << std::endl;
        return;
    }

    m_hiddenCursor = xcb_generate_id(m_connection);

    // Create the cursor, using the pixmap as both the shape and the mask of the cursor
    ScopedXcbPtr<xcb_generic_error_t> createCursorError(xcb_request_check(
        m_connection,
        xcb_create_cursor(
            m_connection,
            m_hiddenCursor,
            cursorPixmap,
            cursorPixmap,
            0, 0, 0, // Foreground RGB color
            0, 0, 0, // Background RGB color
            0,       // X
            0        // Y
        )
    ));

    if (createCursorError)
        err() << "Failed to create hidden cursor" << std::endl;

    // We don't need the pixmap any longer, free it
    ScopedXcbPtr<xcb_generic_error_t> freePixmapError(xcb_request_check(
        m_connection,
        xcb_free_pixmap(
            m_connection,
            cursorPixmap
        )
    ));

    if (freePixmapError)
        err() << "Failed to free pixmap for hidden cursor" << std::endl;
}


////////////////////////////////////////////////////////////
void WindowImplX11::cleanup()
{
    // Restore the previous video mode (in case we were running in fullscreen)
    resetVideoMode();

    // Unhide the mouse cursor (in case it was hidden)
    setMouseCursorVisible(true);
}


////////////////////////////////////////////////////////////
bool WindowImplX11::processEvent(xcb_generic_event_t* windowEvent)
{
    // Convert the X11 event to a sf::Event
    switch (windowEvent->response_type & ~0x80)
    {
        // Destroy event
        case XCB_DESTROY_NOTIFY:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_destroy_notify_event_t*>(windowEvent)->window))
                return false;

            // The window is about to be destroyed: we must cleanup resources
            cleanup();
            break;
        }

        // Gain focus event
        case XCB_FOCUS_IN:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_focus_in_event_t*>(windowEvent)->event))
                return false;

            // Update the input context
            if (m_inputContext)
                XSetICFocus(m_inputContext);

            Event event;
            event.type = Event::GainedFocus;
            pushEvent(event);

            // If the window has been previously marked urgent (notification) as a result of a focus request, undo that
            ScopedXcbPtr<xcb_generic_error_t> error(NULL);

            ScopedXcbPtr<xcb_get_property_reply_t> hintsReply(xcb_get_property_reply(
                m_connection,
                xcb_get_property(m_connection, 0, m_window, XCB_ATOM_WM_HINTS, XCB_ATOM_WM_HINTS, 0, 9),
                &error
            ));

            if (error || !hintsReply)
            {
                err() << "Failed to get WM hints in XCB_FOCUS_IN" << std::endl;
                break;
            }

            WMHints* hints = reinterpret_cast<WMHints*>(xcb_get_property_value(hintsReply.get()));

            if (!hints)
            {
                err() << "Failed to get WM hints in XCB_FOCUS_IN" << std::endl;
                break;
            }

            // Remove urgency (notification) flag from hints
            hints->flags &= ~(1 << 8);

            setWMHints(*hints);

            break;
        }

        // Lost focus event
        case XCB_FOCUS_OUT:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_focus_out_event_t*>(windowEvent)->event))
                return false;

            // Update the input context
            if (m_inputContext)
                XUnsetICFocus(m_inputContext);

            Event event;
            event.type = Event::LostFocus;
            pushEvent(event);
            break;
        }

        // Resize event
        case XCB_CONFIGURE_NOTIFY:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_configure_notify_event_t*>(windowEvent)->window))
                return false;

            xcb_configure_notify_event_t* e = reinterpret_cast<xcb_configure_notify_event_t*>(windowEvent);
            Event event;
            event.type        = Event::Resized;
            event.size.width  = e->width;
            event.size.height = e->height;
            pushEvent(event);
            break;
        }

        // Close event
        case XCB_CLIENT_MESSAGE:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_client_message_event_t*>(windowEvent)->window))
                return false;

            xcb_client_message_event_t* e = reinterpret_cast<xcb_client_message_event_t*>(windowEvent);

            static xcb_atom_t wmProtocols = getAtom("WM_PROTOCOLS");

            // Handle window manager protocol messages we support
            if (e->type == wmProtocols)
            {
                static xcb_atom_t wmDeleteWindow = getAtom("WM_DELETE_WINDOW");
                static xcb_atom_t netWmPing = ewmhSupported() ? getAtom("_NET_WM_PING", true) : XCB_ATOM_NONE;

                if ((e->format == 32) && (e->data.data32[0]) == static_cast<long>(wmDeleteWindow))
                {
                    // Handle the WM_DELETE_WINDOW message
                    Event event;
                    event.type = Event::Closed;
                    pushEvent(event);
                }
                else if (netWmPing && (e->format == 32) && (e->data.data32[0]) == static_cast<long>(netWmPing))
                {
                    // Handle the _NET_WM_PING message, send pong back to WM to show that we are responsive
                    e->window = XCBDefaultRootWindow(m_connection);

                    ScopedXcbPtr<xcb_generic_error_t> pongError(xcb_request_check(
                        m_connection,
                        xcb_send_event_checked(
                            m_connection,
                            0,
                            XCBDefaultRootWindow(m_connection),
                            XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
                            reinterpret_cast<char*>(e)
                        )
                    ));

                    if (pongError)
                        err() << "Could not send pong event back to WM" << std::endl;
                }
            }
            break;
        }

        // Key down event
        case XCB_KEY_PRESS:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_key_press_event_t*>(windowEvent)->event))
                return false;

            xcb_key_press_event_t* e = reinterpret_cast<xcb_key_press_event_t*>(windowEvent);

            // Fill the event parameters
            // TODO: if modifiers are wrong, use XGetModifierMapping to retrieve the actual modifiers mapping
            Event event;
            event.type        = Event::KeyPressed;
            event.key.code    = keycodeToSF(e->detail);
            event.key.alt     = e->state & XCB_MOD_MASK_1;
            event.key.control = e->state & XCB_MOD_MASK_CONTROL;
            event.key.shift   = e->state & XCB_MOD_MASK_SHIFT;
            event.key.system  = e->state & XCB_MOD_MASK_4;
            pushEvent(event);

            XEvent fakeEvent;
            fakeEvent.type = KeyPress;
            fakeEvent.xany.display = m_display;
            fakeEvent.xany.window  = e->event;
            fakeEvent.xkey.state   = e->state;
            fakeEvent.xkey.keycode = e->detail;

            // Generate a TextEntered event
            if (!XFilterEvent(&fakeEvent, None))
            {
                #ifdef X_HAVE_UTF8_STRING
                if (m_inputContext)
                {
                    Status status;
                    Uint8  keyBuffer[16];

                    int length = Xutf8LookupString(
                        m_inputContext,
                        &fakeEvent.xkey,
                        reinterpret_cast<char*>(keyBuffer),
                        sizeof(keyBuffer),
                        NULL,
                        &status
                    );

                    if (length > 0)
                    {
                        Uint32 unicode = 0;
                        Utf8::decode(keyBuffer, keyBuffer + length, unicode, 0);
                        if (unicode != 0)
                        {
                            Event textEvent;
                            textEvent.type         = Event::TextEntered;
                            textEvent.text.unicode = unicode;
                            pushEvent(textEvent);
                        }
                    }
                }
                else
                #endif
                {
                    static XComposeStatus status;
                    char keyBuffer[16];
                    if (XLookupString(&fakeEvent.xkey, keyBuffer, sizeof(keyBuffer), NULL, &status))
                    {
                        Event textEvent;
                        textEvent.type         = Event::TextEntered;
                        textEvent.text.unicode = static_cast<Uint32>(keyBuffer[0]);
                        pushEvent(textEvent);
                    }
                }
            }

            break;
        }

        // Key up event
        case XCB_KEY_RELEASE:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_key_release_event_t*>(windowEvent)->event))
                return false;

            xcb_key_release_event_t* e = reinterpret_cast<xcb_key_release_event_t*>(windowEvent);

            // Fill the event parameters
            Event event;
            event.type        = Event::KeyReleased;
            event.key.code    = keycodeToSF(e->detail);
            event.key.alt     = e->state & XCB_MOD_MASK_1;
            event.key.control = e->state & XCB_MOD_MASK_CONTROL;
            event.key.shift   = e->state & XCB_MOD_MASK_SHIFT;
            event.key.system  = e->state & XCB_MOD_MASK_4;
            pushEvent(event);

            break;
        }

        // Mouse button pressed
        case XCB_BUTTON_PRESS:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_button_press_event_t*>(windowEvent)->event))
                return false;

            xcb_button_press_event_t* e = reinterpret_cast<xcb_button_press_event_t*>(windowEvent);

            // XXX: Why button 8 and 9?
            // Because 4 and 5 are the vertical wheel and 6 and 7 are horizontal wheel ;)
            xcb_button_t button = e->detail;
            if ((button == XCB_BUTTON_INDEX_1) ||
                (button == XCB_BUTTON_INDEX_2) ||
                (button == XCB_BUTTON_INDEX_3) ||
                (button == 8) ||
                (button == 9))
            {
                Event event;
                event.type          = Event::MouseButtonPressed;
                event.mouseButton.x = e->event_x;
                event.mouseButton.y = e->event_y;
                switch(button)
                {
                case XCB_BUTTON_INDEX_1: event.mouseButton.button = Mouse::Left;     break;
                case XCB_BUTTON_INDEX_2: event.mouseButton.button = Mouse::Middle;   break;
                case XCB_BUTTON_INDEX_3: event.mouseButton.button = Mouse::Right;    break;
                case 8:                  event.mouseButton.button = Mouse::XButton1; break;
                case 9:                  event.mouseButton.button = Mouse::XButton2; break;
                }
                pushEvent(event);
            }
            break;
        }

        // Mouse button released
        case XCB_BUTTON_RELEASE:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_button_release_event_t*>(windowEvent)->event))
                return false;

            xcb_button_release_event_t* e = reinterpret_cast<xcb_button_press_event_t*>(windowEvent);

            xcb_button_t button = e->detail;
            if ((button == XCB_BUTTON_INDEX_1) ||
                (button == XCB_BUTTON_INDEX_2) ||
                (button == XCB_BUTTON_INDEX_3) ||
                (button == 8) ||
                (button == 9))
            {
                Event event;
                event.type          = Event::MouseButtonReleased;
                event.mouseButton.x = e->event_x;
                event.mouseButton.y = e->event_y;
                switch(button)
                {
                case XCB_BUTTON_INDEX_1: event.mouseButton.button = Mouse::Left;     break;
                case XCB_BUTTON_INDEX_2: event.mouseButton.button = Mouse::Middle;   break;
                case XCB_BUTTON_INDEX_3: event.mouseButton.button = Mouse::Right;    break;
                case 8:                  event.mouseButton.button = Mouse::XButton1; break;
                case 9:                  event.mouseButton.button = Mouse::XButton2; break;
                }
                pushEvent(event);
            }
            else if ((button == XCB_BUTTON_INDEX_4) || (button == XCB_BUTTON_INDEX_5))
            {
                Event event;

                event.type             = Event::MouseWheelMoved;
                event.mouseWheel.delta = (button == XCB_BUTTON_INDEX_4) ? 1 : -1;
                event.mouseWheel.x     = e->event_x;
                event.mouseWheel.y     = e->event_y;
                pushEvent(event);

                event.type                   = Event::MouseWheelScrolled;
                event.mouseWheelScroll.wheel = Mouse::VerticalWheel;
                event.mouseWheelScroll.delta = (button == XCB_BUTTON_INDEX_4) ? 1 : -1;
                event.mouseWheelScroll.x     = e->event_x;
                event.mouseWheelScroll.y     = e->event_y;
                pushEvent(event);
            }
            else if ((button == 6) || (button == 7))
            {
                Event event;
                event.type                   = Event::MouseWheelScrolled;
                event.mouseWheelScroll.wheel = Mouse::HorizontalWheel;
                event.mouseWheelScroll.delta = (button == 6) ? 1 : -1;
                event.mouseWheelScroll.x     = e->event_x;
                event.mouseWheelScroll.y     = e->event_y;
                pushEvent(event);
            }
            break;
        }

        // Mouse moved
        case XCB_MOTION_NOTIFY:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_motion_notify_event_t*>(windowEvent)->event))
                return false;

            xcb_motion_notify_event_t* e = reinterpret_cast<xcb_motion_notify_event_t*>(windowEvent);
            Event event;
            event.type        = Event::MouseMoved;
            event.mouseMove.x = e->event_x;
            event.mouseMove.y = e->event_y;
            pushEvent(event);
            break;
        }

        // Mouse entered
        case XCB_ENTER_NOTIFY:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_enter_notify_event_t*>(windowEvent)->event))
                return false;

            xcb_enter_notify_event_t* enterNotifyEvent = reinterpret_cast<xcb_enter_notify_event_t*>(windowEvent);

            if (enterNotifyEvent->mode == NotifyNormal)
            {
                Event event;
                event.type = Event::MouseEntered;
                pushEvent(event);
            }
            break;
        }

        // Mouse left
        case XCB_LEAVE_NOTIFY:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_leave_notify_event_t*>(windowEvent)->event))
                return false;

            xcb_leave_notify_event_t* leaveNotifyEvent = reinterpret_cast<xcb_leave_notify_event_t*>(windowEvent);

            if (leaveNotifyEvent->mode == NotifyNormal)
            {
                Event event;
                event.type = Event::MouseLeft;
                pushEvent(event);
            }
            break;
        }

        // Parent window changed
        case XCB_REPARENT_NOTIFY:
        {
            if (passEvent(windowEvent, reinterpret_cast<xcb_reparent_notify_event_t*>(windowEvent)->window))
                return false;

            // Catch reparent events to properly apply fullscreen on
            // some "strange" window managers (like Awesome) which
            // seem to make use of temporary parents during mapping
            if (m_fullscreen)
                switchToFullscreen();

            xcb_flush(m_connection); // Discard remaining events
            break;
        }

        // The stuff that might pop up but we don't care about
        // Whitelist more when necessary
        // Window visibility changed (hide/unhide)
        case XCB_MAP_NOTIFY:
        case XCB_UNMAP_NOTIFY:
        {
            break;
        }

        // Handle the rest
        default:
        {
            uint8_t responseType = windowEvent->response_type & ~0x80;

            // Handle any extension events first

            // DRI2
            static xcb_query_extension_reply_t driExtension = getDriExtension();
            if (driExtension.present)
            {
                // Because we are using the XCB event queue instead of the Xlib event
                // queue, Mesa breaks a bit (VSync among other things) because DRI2 still
                // expects certain Xlib events to come its way. We work around this by
                // emulating the old Xlib event queue for these specific wire events.
                // Sources (retrieved 27 Mar 2015):
                // http://wrl.illest.net/post/45342765813/code-tip-glx-and-xcbownseventqueue
                // https://bugs.freedesktop.org/show_bug.cgi?id=42131
                // https://bugs.freedesktop.org/show_bug.cgi?id=35945
                // Mesa src/glx/dri2.c
                // QtBase src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.cpp
                // QtBase Commit bb22b4965070409df4658f16fdf549f0362e8a9c
                // Qt Change-Id I3b4ef3f6e3efbae25f49f161e229e9b15e951778
                // QtBase Commit 13c5c81cfa934c9b610720fe79e07465b00ebc8d
                // Qt Change-Id Ia93eb8be1cbbc3d8ae7913a934c195af6b5ec538
                // QtBase Commit decb88693c8a7f0c073889b91151f01a850e3adf
                // Qt Change-Id Ic466ff26487937b03f072a57e0ee4df335492a5f
                if ((responseType == driExtension.first_event + XCB_DRI2_BUFFER_SWAP_COMPLETE) ||
                    (responseType == driExtension.first_event + XCB_DRI2_INVALIDATE_BUFFERS))
                {
                    // DRI2 BufferSwapComplete and InvalidateBuffers

                    // We lock/unlock the display to protect against concurrent access
                    XLockDisplay(m_display);

                    typedef Bool (*wireEventHandler)(Display*, XEvent*, xEvent*);

                    // Probe for any handlers that are registered for this event type
                    wireEventHandler handler = XESetWireToEvent(m_display, responseType, 0);

                    if (handler)
                    {
                        // Restore the previous handler if one was registered
                        XESetWireToEvent(m_display, responseType, handler);

                        XEvent event;
                        windowEvent->sequence = LastKnownRequestProcessed(m_display);

                        // Pretend to be the Xlib event queue
                        handler(m_display, &event, reinterpret_cast<xEvent*>(windowEvent));
                    }

                    XUnlockDisplay(m_display);

                    return true;
                }
            }

            // Print any surprises to stderr (would be nice if people report when this happens)
            dumpUnhandledEvent(responseType);

            break;
        }
    }

    return true;
}


////////////////////////////////////////////////////////////
bool WindowImplX11::passEvent(xcb_generic_event_t* windowEvent, xcb_window_t window)
{
    // Check if this is our event
    if (window == m_window)
        return false;

    Lock lock(allWindowsMutex);

    // If we are the only window, there is nobody else to pass to
    if (allWindows.size() == 1)
        return false;

    for (std::vector<WindowImplX11*>::iterator i = allWindows.begin(); i != allWindows.end(); ++i)
    {
        if ((*i)->m_window == window)
        {
            (*i)->m_xcbEvents.push_back(windowEvent);
            return true;
        }
    }

    // It seems nobody wants the event, we'll just handle it ourselves
    return false;
}

} // namespace priv

} // namespace sf