summaryrefslogtreecommitdiff
path: root/src/surface/wld.c
blob: 7843639e90cf8bb771e9efd037782d0a35c585a9 (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
/*
 * Copyright 2009 Vincent Sanders <vince@simtec.co.uk>
 *
 * This file is part of libnsfb, http://www.netsurf-browser.org/
 * Licenced under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

#define _XOPEN_SOURCE 500

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

#include <linux/input.h>
#include <wayland-client.h>

#include "libnsfb.h"
#include "libnsfb_event.h"
#include "libnsfb_plot.h"
#include "libnsfb_plot_util.h"

#include "nsfb.h"
#include "surface.h"
#include "plot.h"
#include "cursor.h"

struct wld_event {
    struct wld_event *next;

    nsfb_event_t event;
};

/** structure for display, registry and other global objects that
 * should be cached when connecting to a wayland instance
 */
struct wld_connection {
    struct wl_display *display; /**< connection object */
    struct wl_registry *registry; /**< registry object */

    /** compositor object, available once teh registry messages have
     * been processed
     */
    struct wl_compositor *compositor;

    /** shell object, available once the registry messages have been
     * processed
     */
    struct wl_shell *shell;

    /** shared memory object, available once the registry messages
     * have been processed
     */
    struct wl_shm *shm;

    /** shared memory formats available */
    uint32_t shm_formats;

    /** list of input seats */
    struct wl_list input_list;

    /** event queue */
    struct wld_event *event_head;
    struct wld_event *event_tail;
};

/** wayland input seat */
struct wld_input {
    struct wl_list link; /**< input list */

    struct wld_connection* connection; /**< connection to wayland server */

    struct wl_seat *seat; /**< The seat object */

    struct wl_pointer *pointer;
    struct wl_keyboard *keyboard;


};

/** wayland window encompasing the display and shell surfaces */
struct wld_window {
    struct wld_connection* connection; /**< connection to wayland server */

    struct wl_surface *surface; /**< drawing surface object */
    struct wl_shell_surface *shell_surface; /**< shell surface object */

    int width, height;
};

struct wld_shm_buffer {
    struct wl_buffer *buffer; /**< wayland buffer object */
    void *data; /**< mapped memory */
    int size; /**< size of mapped memory */
    bool inuse; /**< flag to indicate if the buffer has been released
		 * after commit to a surface.
		 */
};


typedef struct wldstate_s {
    struct wld_connection* connection; /**< connection to wayland server */
    struct wld_window *window;
    struct wld_shm_buffer *shm_buffer;
} wldstate_t;


#if 0

#if defined(NSFB_NEED_HINTS_ALLOC)
static xcb_size_hints_t *
xcb_alloc_size_hints(void)
{
    return calloc(1, sizeof(xcb_size_hints_t));
}

static void
xcb_free_size_hints(xcb_size_hints_t *hints)
{
    free(hints);
}
#endif

#if defined(NSFB_NEED_ICCCM_API_PREFIX)
#define xcb_size_hints_set_max_size xcb_icccm_size_hints_set_max_size
#define xcb_size_hints_set_min_size xcb_icccm_size_hints_set_min_size
#define xcb_set_wm_size_hints       xcb_icccm_set_wm_size_hints
#endif

#if (NSFB_XCBPROTO_MAJOR_VERSION > 1) || \
    (NSFB_XCBPROTO_MAJOR_VERSION == 1 && NSFB_XCBPROTO_MINOR_VERSION >= 6)
#define WM_NORMAL_HINTS XCB_ATOM_WM_NORMAL_HINTS
#endif

#define X_BUTTON_LEFT 1
#define X_BUTTON_MIDDLE 2
#define X_BUTTON_RIGHT 3
#define X_BUTTON_WHEELUP 4
#define X_BUTTON_WHEELDOWN 5

typedef struct xstate_s {
    xcb_connection_t *connection; /* The x server connection */
    xcb_screen_t *screen; /* The screen to put the window on */
    xcb_key_symbols_t *keysymbols; /* keysym mappings */

    xcb_shm_segment_info_t shminfo;

    xcb_image_t *image; /* The X image buffer */

    xcb_window_t window; /* The handle to the window */
    xcb_pixmap_t pmap; /* The handle to the backing pixmap */
    xcb_gcontext_t gc; /* The handle to the pixmap plotting graphics context */
    xcb_shm_seg_t segment; /* The handle to the image shared memory */
} xstate_t;

/* X keyboard codepage to nsfb mapping*/
static enum nsfb_key_code_e XCSKeyboardMap[256] = {
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_BACKSPACE, /* XK_BackSpace */
    NSFB_KEY_TAB, /* XK_Tab */
    NSFB_KEY_UNKNOWN, /* XK_Linefeed */
    NSFB_KEY_CLEAR, /* XK_Clear */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_RETURN, /* XK_Return */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x10 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_PAUSE, /* XK_Pause */
    NSFB_KEY_UNKNOWN, /* XK_Scroll_Lock */
    NSFB_KEY_UNKNOWN, /* XK_Sys_Req */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_ESCAPE, /* XK_Escape */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x20 */
    NSFB_KEY_UNKNOWN, /* XK_Multi_key */
    NSFB_KEY_UNKNOWN, /* XK_Kanji */
    NSFB_KEY_UNKNOWN, /* XK_Muhenkan */
    NSFB_KEY_UNKNOWN, /* XK_Henkan_Mode */
    NSFB_KEY_UNKNOWN, /* XK_Henkan */
    NSFB_KEY_UNKNOWN, /* XK_Romaji */
    NSFB_KEY_UNKNOWN, /* XK_Hiragana*/
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x30 */
    NSFB_KEY_UNKNOWN, /* XK_Eisu_toggle */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_Codeinput */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_SingleCandidate */
    NSFB_KEY_UNKNOWN, /* XK_MultipleCandidate */
    NSFB_KEY_UNKNOWN, /* XK_PreviousCandidate */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x40 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x50 */
    NSFB_KEY_HOME, /* XK_Home */
    NSFB_KEY_LEFT, /* XK_Left */
    NSFB_KEY_UP, /* XK_Up */
    NSFB_KEY_RIGHT, /* XK_Right */
    NSFB_KEY_DOWN, /* XK_Down */
    NSFB_KEY_PAGEUP, /* XK_Page_Up */
    NSFB_KEY_PAGEDOWN, /* XK_Page_Down */
    NSFB_KEY_END, /* XK_End */
    NSFB_KEY_UNKNOWN, /* XK_Begin */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x60 */
    NSFB_KEY_UNKNOWN, /* XK_Select */
    NSFB_KEY_UNKNOWN, /* XK_Print*/
    NSFB_KEY_UNKNOWN, /* XK_Execute*/
    NSFB_KEY_UNKNOWN, /* XK_Insert*/
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_Undo*/
    NSFB_KEY_UNKNOWN, /* XK_Redo */
    NSFB_KEY_UNKNOWN, /* XK_Menu */
    NSFB_KEY_UNKNOWN, /* XK_Find */
    NSFB_KEY_UNKNOWN, /* XK_Cancel */
    NSFB_KEY_UNKNOWN, /* XK_Help */
    NSFB_KEY_UNKNOWN, /* XK_Break*/
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x70 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_Mode_switch */
    NSFB_KEY_UNKNOWN, /* XK_Num_Lock */
    /* 0x80 */
    NSFB_KEY_UNKNOWN, /* XK_KP_Space */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_KP_Tab */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_KP_Enter */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0x90 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_KP_F1*/
    NSFB_KEY_UNKNOWN, /* XK_KP_F2*/
    NSFB_KEY_UNKNOWN, /* XK_KP_F3*/
    NSFB_KEY_UNKNOWN, /* XK_KP_F4*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Home*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Left*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Up*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Right*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Down*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Page_Up*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Page_Down*/
    NSFB_KEY_UNKNOWN, /* XK_KP_End*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Begin*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Insert*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Delete*/
    /* 0xa0 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_KP_Multiply*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Add*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Separator*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Subtract*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Decimal*/
    NSFB_KEY_UNKNOWN, /* XK_KP_Divide*/
    /* 0xb0 */
    NSFB_KEY_UNKNOWN, /* XK_KP_0 */
    NSFB_KEY_UNKNOWN, /* XK_KP_1 */
    NSFB_KEY_UNKNOWN, /* XK_KP_2 */
    NSFB_KEY_UNKNOWN, /* XK_KP_3 */
    NSFB_KEY_UNKNOWN, /* XK_KP_4 */
    NSFB_KEY_UNKNOWN, /* XK_KP_5 */
    NSFB_KEY_UNKNOWN, /* XK_KP_6 */
    NSFB_KEY_UNKNOWN, /* XK_KP_7 */
    NSFB_KEY_UNKNOWN, /* XK_KP_8 */
    NSFB_KEY_UNKNOWN, /* XK_KP_9 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_F1, /* XK_F1 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* XK_KP_Equal */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_F2, /* XK_F2 */
    /* 0xc0 */
    NSFB_KEY_F3, /* XK_F3*/
    NSFB_KEY_F4, /* XK_F4*/
    NSFB_KEY_F5, /* XK_F5*/
    NSFB_KEY_F6, /* XK_F6*/
    NSFB_KEY_F7, /* XK_F7*/
    NSFB_KEY_F8, /* XK_F8*/
    NSFB_KEY_F9, /* XK_F9*/
    NSFB_KEY_F10, /* XK_F10*/
    NSFB_KEY_F11, /* XK_F11*/
    NSFB_KEY_F12, /* XK_F12*/
    NSFB_KEY_F13, /* XK_F13 */
    NSFB_KEY_F14, /* XK_F14 */
    NSFB_KEY_F15, /* XK_F15 */
    NSFB_KEY_UNKNOWN, /* XK_F16 */
    NSFB_KEY_UNKNOWN, /* XK_F17 */
    NSFB_KEY_UNKNOWN, /* XK_F18*/
    /* 0xd0 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    /* 0xe0 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_LSHIFT, /* XK_Shift_L*/
    NSFB_KEY_RSHIFT, /* XK_Shift_R*/
    NSFB_KEY_UNKNOWN, /* XK_Control_L*/
    NSFB_KEY_UNKNOWN, /* XK_Control_R*/
    NSFB_KEY_UNKNOWN, /* XK_Caps_Lock*/
    NSFB_KEY_UNKNOWN, /* XK_Shift_Lock*/
    NSFB_KEY_UNKNOWN, /* XK_Meta_L*/
    NSFB_KEY_UNKNOWN, /* XK_Meta_R*/
    NSFB_KEY_UNKNOWN, /* XK_Alt_L */
    NSFB_KEY_UNKNOWN, /* XK_Alt_R*/
    NSFB_KEY_UNKNOWN, /* XK_Super_L*/
    NSFB_KEY_UNKNOWN, /* XK_Super_R*/
    NSFB_KEY_UNKNOWN, /* XK_Hyper_L*/
    NSFB_KEY_UNKNOWN, /* XK_Hyper_R*/
    NSFB_KEY_UNKNOWN, /* */
    /* 0xf0 */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
    NSFB_KEY_UNKNOWN, /* */
};

/* Convert an X keysym into a nsfb key code.
 *
 * Our approach is primarily to assume both codings are roughly ascii like.
 *
 * The Keysyms are defined in X11/keysymdef.h and our mapping for the
 * "keyboard" keys i.e. those from code set 255 is from there
 */
static enum nsfb_key_code_e
xkeysym_to_nsfbkeycode(xcb_keysym_t ks)
{
    enum nsfb_key_code_e nsfb_key = NSFB_KEY_UNKNOWN;
    uint8_t codeset = (ks & 0xFF00) >> 8;
    uint8_t chrcode = ks & 0xFF;

    if (ks != XCB_NO_SYMBOL) {

	switch (codeset) {
	case 0x00: /* Latin 1 */
	case 0x01: /* Latin 2 */
	case 0x02: /* Latin 3 */
	case 0x03: /* Latin 4 */
	case 0x04: /* Katakana */
	case 0x05: /* Arabic */
	case 0x06: /* Cyrillic */
	case 0x07: /* Greek */
	case 0x08: /* Technical */
	case 0x0A: /* Publishing */
	case 0x0C: /* Hebrew */
	case 0x0D: /* Thai */
	    /* this is somewhat incomplete, but the nsfb codes are lined up on
	     * the ascii codes and x seems to have done similar
	     */
	    nsfb_key = (enum nsfb_key_code_e)chrcode;
	    break;

	case 0xFF: /* Keyboard */
	    nsfb_key = XCSKeyboardMap[chrcode];
	    break;
	}
    }

    return nsfb_key;
}
/*
  static void
  set_palette(nsfb_t *nsfb)
  {
  X_Surface *x_screen = nsfb->surface_priv;
  X_Color palette[256];
  int rloop, gloop, bloop;
  int loop = 0;

  // build a linear R:3 G:3 B:2 colour cube palette.
  for (rloop = 0; rloop < 8; rloop++) {
  for (gloop = 0; gloop < 8; gloop++) {
  for (bloop = 0; bloop < 4; bloop++) {
  palette[loop].r = (rloop << 5) | (rloop << 2) | (rloop >> 1);
  palette[loop].g = (gloop << 5) | (gloop << 2) | (gloop >> 1);
  palette[loop].b = (bloop << 6) | (bloop << 4) | (bloop << 2) | (bloop);
  nsfb->palette[loop] = palette[loop].r |
  palette[loop].g << 8 |
  palette[loop].b << 16;
  loop++;
  }
  }
  }

  // Set palette
  //X_SetColors(x_screen, palette, 0, 256);

  }
*/
static int
update_pixmap(xstate_t *xstate, int x, int y, int width, int height)
{
    if (xstate->shminfo.shmseg == 0) {
	/* not using shared memory */
	xcb_put_image(xstate->connection,
		      xstate->image->format,
		      xstate->pmap,
		      xstate->gc,
		      xstate->image->width,
		      height,
		      0,
		      y,
		      0,
		      xstate->image->depth,
		      (height) * xstate->image->stride,
		      xstate->image->data + (y * xstate->image->stride));
    } else {
	/* shared memory */
	xcb_image_shm_put(xstate->connection,
			  xstate->pmap,
			  xstate->gc,
			  xstate->image,
			  xstate->shminfo,
			  x,y,
			  x,y,
			  width,height,0);
    }

    return 0;
}

static int
update_and_redraw_pixmap(xstate_t *xstate, int x, int y, int width, int height)
{
    update_pixmap(xstate, x, y, width, height);

    xcb_copy_area(xstate->connection,
		  xstate->pmap,
		  xstate->window,
		  xstate->gc,
		  x, y,
		  x, y,
		  width, height);

    xcb_flush(xstate->connection);

    return 0;
}


static bool
xcopy(nsfb_t *nsfb, nsfb_bbox_t *srcbox, nsfb_bbox_t *dstbox)
{
    xstate_t *xstate = nsfb->surface_priv;
    nsfb_bbox_t allbox;
    struct nsfb_cursor_s *cursor = nsfb->cursor;
    uint8_t *srcptr;
    uint8_t *dstptr;
    int srcx = srcbox->x0;
    int srcy = srcbox->y0;
    int dstx = dstbox->x0;
    int dsty = dstbox->y0;
    int width = dstbox->x1 - dstbox->x0;
    int height = dstbox->y1 - dstbox->y0;
    int hloop;

    nsfb_plot_add_rect(srcbox, dstbox, &allbox);

    /* clear the cursor if its within the region to be altered */
    if ((cursor != NULL) &&
	(cursor->plotted == true) &&
	(nsfb_plot_bbox_intersect(&allbox, &cursor->loc))) {

	nsfb_cursor_clear(nsfb, cursor);
	update_pixmap(xstate,
		      cursor->savloc.x0,
		      cursor->savloc.y0,
		      cursor->savloc.x1 - cursor->savloc.x0,
		      cursor->savloc.y1 - cursor->savloc.y0);

	/* must sync here or local framebuffer and remote pixmap will not be
	 * consistant
	 */
	xcb_aux_sync(xstate->connection);

    }

    /* copy the area on the server */
    xcb_copy_area(xstate->connection,
		  xstate->pmap,
		  xstate->pmap,
		  xstate->gc,
		  srcbox->x0,
		  srcbox->y0,
		  dstbox->x0,
		  dstbox->y0,
		  srcbox->x1 - srcbox->x0,
		  srcbox->y1 - srcbox->y0);

    /* do the copy in the local memory too */
    srcptr = (nsfb->ptr +
	      (srcy * nsfb->linelen) +
	      ((srcx * nsfb->bpp) / 8));

    dstptr = (nsfb->ptr +
	      (dsty * nsfb->linelen) +
	      ((dstx * nsfb->bpp) / 8));

    if (width == nsfb->width) {
	/* take shortcut and use memmove */
	memmove(dstptr, srcptr, (width * height * nsfb->bpp) / 8);
    } else {
	if (srcy > dsty) {
	    for (hloop = height; hloop > 0; hloop--) {
		memmove(dstptr, srcptr, (width * nsfb->bpp) / 8);
		srcptr += nsfb->linelen;
		dstptr += nsfb->linelen;
	    }
	} else {
	    srcptr += height * nsfb->linelen;
	    dstptr += height * nsfb->linelen;
	    for (hloop = height; hloop > 0; hloop--) {
		srcptr -= nsfb->linelen;
		dstptr -= nsfb->linelen;
		memmove(dstptr, srcptr, (width * nsfb->bpp) / 8);
	    }
	}
    }

    if ((cursor != NULL) &&
	(cursor->plotted == false)) {
	nsfb_cursor_plot(nsfb, cursor);
    }

    /* update the x window */
    xcb_copy_area(xstate->connection,
		  xstate->pmap,
		  xstate->window,
		  xstate->gc,
		  dstx, dsty,
		  dstx, dsty,
		  width, height);

    return true;

}

#endif


static int
wld_set_geometry(nsfb_t *nsfb, int width, int height, enum nsfb_format_e format)
{
    if (nsfb->surface_priv != NULL)
	return -1; /* if were already initialised fail */

    nsfb->width = width;
    nsfb->height = height;
    nsfb->format = format;

    /* select default sw plotters for format */
    select_plotters(nsfb);

}

#if 0


static xcb_format_t *
find_format(xcb_connection_t * c, uint8_t depth, uint8_t bpp)
{
    const xcb_setup_t *setup = xcb_get_setup(c);
    xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
    xcb_format_t *fmtend = fmt + xcb_setup_pixmap_formats_length(setup);

    for(; fmt != fmtend; ++fmt) {
	if((fmt->depth == depth) && (fmt->bits_per_pixel == bpp)) {
	    return fmt;
	}
    }
    return 0;
}

static xcb_image_t *
create_shm_image(xstate_t *xstate, int width, int height, int bpp)
{
    const xcb_setup_t *setup = xcb_get_setup(xstate->connection);
    unsigned char *image_data;
    xcb_format_t *fmt;
    int depth = bpp;
    uint32_t image_size;
    int shmid;

    xcb_shm_query_version_reply_t *rep;
    xcb_shm_query_version_cookie_t ck;
    xcb_void_cookie_t shm_attach_cookie;
    xcb_generic_error_t *generic_error;

    ck = xcb_shm_query_version(xstate->connection);
    rep = xcb_shm_query_version_reply(xstate->connection, ck , NULL);
    if (!rep) {
	fprintf (stderr, "Server has no shm support.\n");
	return NULL;
    }

    if ((rep->major_version < 1) ||
	(rep->major_version == 1 && rep->minor_version == 0)) {
	fprintf(stderr, "server SHM support is insufficient.\n");
	free(rep);
	return NULL;
    }
    free(rep);

    if (bpp == 32)
	depth = 24;

    fmt = find_format(xstate->connection, depth, bpp);
    if (fmt == NULL)
	return NULL;

    /* doing it this way ensures we deal with bpp smaller than 8 */
    image_size = (bpp * width * height) >> 3;

    /* get the shared memory segment */
    shmid = shmget(IPC_PRIVATE, image_size, IPC_CREAT|0777);
    if (shmid == -1)
	return NULL;

    xstate->shminfo.shmid = shmid;

    xstate->shminfo.shmaddr = shmat(xstate->shminfo.shmid, 0, 0);
    image_data = xstate->shminfo.shmaddr;

    xstate->shminfo.shmseg = xcb_generate_id(xstate->connection);
    shm_attach_cookie = xcb_shm_attach_checked(xstate->connection,
					       xstate->shminfo.shmseg,
					       xstate->shminfo.shmid,
					       0);
    generic_error = xcb_request_check(xstate->connection, shm_attach_cookie);

    /* either there is an error and the shm us no longer needed, or it now
     * belongs to the x server - regardless release local reference to shared
     * memory segment
     */
    shmctl(xstate->shminfo.shmid, IPC_RMID, 0);

    if (generic_error != NULL) {
	/* unable to attach shm */
	xstate->shminfo.shmseg = 0;

	free(generic_error);
	return NULL;
    }


    return xcb_image_create(width,
			    height,
			    XCB_IMAGE_FORMAT_Z_PIXMAP,
			    fmt->scanline_pad,
			    fmt->depth,
			    fmt->bits_per_pixel,
			    0,
			    setup->image_byte_order,
			    XCB_IMAGE_ORDER_LSB_FIRST,
			    image_data,
			    image_size,
			    image_data);
}


static xcb_image_t *
create_image(xcb_connection_t *c, int width, int height, int bpp)
{
    const xcb_setup_t *setup = xcb_get_setup(c);
    unsigned char *image_data;
    xcb_format_t *fmt;
    int depth = bpp;
    uint32_t image_size;

    if (bpp == 32)
	depth = 24;

    fmt = find_format(c, depth, bpp);
    if (fmt == NULL)
	return NULL;

    /* doing it this way ensures we deal with bpp smaller than 8 */
    image_size = (bpp * width * height) >> 3;

    image_data = calloc(1, image_size);
    if (image_data == NULL)
	return NULL;

    return xcb_image_create(width,
			    height,
			    XCB_IMAGE_FORMAT_Z_PIXMAP,
			    fmt->scanline_pad,
			    fmt->depth,
			    fmt->bits_per_pixel,
			    0,
			    setup->image_byte_order,
			    XCB_IMAGE_ORDER_LSB_FIRST,
			    image_data,
			    image_size,
			    image_data);
}

/**
 * Create a blank cursor.
 * The empty pixmaps is leaked.
 *
 * @param conn xcb connection
 * @param scr xcb XCB screen
 */
static xcb_cursor_t
create_blank_cursor(xcb_connection_t *conn, const xcb_screen_t *scr)
{
    xcb_cursor_t cur = xcb_generate_id(conn);
    xcb_pixmap_t pix = xcb_generate_id(conn);
    xcb_void_cookie_t ck;
    xcb_generic_error_t *err;

    ck = xcb_create_pixmap_checked (conn, 1, pix, scr->root, 1, 1);
    err = xcb_request_check (conn, ck);
    if (err) {
	fprintf (stderr, "Cannot create pixmap: %d", err->error_code);
	free (err);
    }
    ck = xcb_create_cursor_checked (conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 0, 0);
    err = xcb_request_check (conn, ck);
    if (err) {
	fprintf (stderr, "Cannot create cursor: %d", err->error_code);
	free (err);
    }
    return cur;
}
#endif


/** shared memory interface format available callback
 *
 * @param ctx context from when listener was added.
 * @param wl_shm The shared memory object.
 * @param format The available shared memory format. Found in
 *                 wayland-client-protocol.h there are currently only two
 *                 formats available (A|X)RGB8888 so using a bitfield is ok.
 */
static void
shm_format(void *ctx, struct wl_shm *wl_shm, uint32_t format)
{
    struct wld_connection* connection = ctx;

    connection->shm_formats |= (1 << format);
}

/** shared memory interface callback handlers */
struct wl_shm_listener shm_listenter = {
	shm_format
};

static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
		     uint32_t serial, struct wl_surface *surface,
		     wl_fixed_t sx_w, wl_fixed_t sy_w)
{
#if 0
	struct input *input = data;
	struct window *window;
	struct widget *widget;
	float sx = wl_fixed_to_double(sx_w);
	float sy = wl_fixed_to_double(sy_w);

	if (!surface) {
		/* enter event for a window we've just destroyed */
		return;
	}

	input->display->serial = serial;
	input->pointer_enter_serial = serial;
	input->pointer_focus = wl_surface_get_user_data(surface);
	window = input->pointer_focus;

	if (window->resizing) {
		window->resizing = 0;
		/* Schedule a redraw to free the pool */
		window_schedule_redraw(window);
	}

	input->sx = sx;
	input->sy = sy;

	widget = window_find_widget(window, sx, sy);
	input_set_focus_widget(input, widget, sx, sy);

#endif
}

static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
		     uint32_t serial, struct wl_surface *surface)
{
#if 0
	struct input *input = data;

	input->display->serial = serial;
	input_remove_pointer_focus(input);
#endif
}

static void
enqueue_wld_event(struct wld_connection *connection, struct wld_event* event)
{
    event->next = NULL;
    if (connection->event_tail == NULL) {
	connection->event_head = event;
	connection->event_tail = event;
    } else {
	connection->event_tail->next = event;
	connection->event_tail = event;
    }
}

static struct wld_event*
dequeue_wld_event(struct wld_connection *connection)
{
    struct wld_event* event = connection->event_head;

    if (event != NULL) {
	connection->event_head = event->next;
	if (connection->event_head == NULL) {
	    connection->event_tail = connection->event_head;
	}
    }
    return event;
}

static void
pointer_handle_motion(void *data,
		      struct wl_pointer *pointer,
		      uint32_t time,
		      wl_fixed_t sx_w,
		      wl_fixed_t sy_w)
{
    struct wld_input *input = data;
    struct wld_event *event;

    event = calloc(1, sizeof(struct wld_event));

    event->event.type = NSFB_EVENT_MOVE_ABSOLUTE;
    event->event.value.vector.x = wl_fixed_to_int(sx_w);
    event->event.value.vector.y = wl_fixed_to_int(sy_w);
    event->event.value.vector.z = 0;

    enqueue_wld_event(input->connection, event);
}

static void
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
		      uint32_t time, uint32_t button, uint32_t state_w)
{
    struct wld_input *input = data;
    struct wld_event *event;
    enum wl_pointer_button_state state = state_w;

    event = calloc(1, sizeof(struct wld_event));

    if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
	event->event.type = NSFB_EVENT_KEY_DOWN;
    } else {
	event->event.type = NSFB_EVENT_KEY_UP;
    }

    switch (button) {
    case BTN_LEFT:
	event->event.value.keycode = NSFB_KEY_MOUSE_1;
	break;
    case BTN_MIDDLE:
	event->event.value.keycode = NSFB_KEY_MOUSE_2;
	break;
    case BTN_RIGHT:
	event->event.value.keycode = NSFB_KEY_MOUSE_3;
	break;
    case BTN_FORWARD:
	event->event.value.keycode = NSFB_KEY_MOUSE_4;
	break;
    case BTN_BACK:
	event->event.value.keycode = NSFB_KEY_MOUSE_5;
	break;
    }

    enqueue_wld_event(input->connection, event);


#if 0
	struct input *input = data;
	struct widget *widget;
	enum wl_pointer_button_state state = state_w;

	input->display->serial = serial;
	if (input->focus_widget && input->grab == NULL &&
	    state == WL_POINTER_BUTTON_STATE_PRESSED)
		input_grab(input, input->focus_widget, button);

	widget = input->grab;
	if (widget && widget->button_handler)
		(*widget->button_handler)(widget,
					  input, time,
					  button, state,
					  input->grab->user_data);

	if (input->grab && input->grab_button == button &&
	    state == WL_POINTER_BUTTON_STATE_RELEASED)
		input_ungrab(input);
#endif
}

static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
		    uint32_t time, uint32_t axis, wl_fixed_t value)
{
#if 0
	struct input *input = data;
	struct widget *widget;

	widget = input->focus_widget;
	if (input->grab)
		widget = input->grab;
	if (widget && widget->axis_handler)
		(*widget->axis_handler)(widget,
					input, time,
					axis, value,
					widget->user_data);
#endif
}

static const struct wl_pointer_listener pointer_listener = {
	pointer_handle_enter,
	pointer_handle_leave,
	pointer_handle_motion,
	pointer_handle_button,
	pointer_handle_axis,
};

static void
seat_handle_capabilities(void *data,
			 struct wl_seat *seat,
			 enum wl_seat_capability caps)
{
    struct wld_input *input = data;

    if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
	input->pointer = wl_seat_get_pointer(seat);

	wl_pointer_add_listener(input->pointer, &pointer_listener, input);

    } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {

	wl_pointer_destroy(input->pointer);
	input->pointer = NULL;
    }

#if 0
    if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
	input->keyboard = wl_seat_get_keyboard(seat);

	wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);

    } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {

	wl_keyboard_destroy(input->keyboard);
	input->keyboard = NULL;
    }
#endif
}

static const struct wl_seat_listener seat_listener = {
	seat_handle_capabilities,
};

/**
 * new seat added
 */
static struct wld_input *
new_input_seat(struct wld_connection *connection,
	       struct wl_registry *registry,
	       uint32_t id)
{
    struct wld_input *input;

    input = calloc(1, sizeof(struct wld_input));
    if (input == NULL) {
	return NULL;
    }

    input->connection = connection;

    input->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
    if (input->seat == NULL) {
	free(input);
	return NULL;
    }

    wl_seat_add_listener(input->seat, &seat_listener, input);

    return input;
}

/** registry global addition callback
 *
 * @param ctx context from when listener was added
 */
static void
registry_handle_global(void *ctx,
		       struct wl_registry *registry,
		       uint32_t id,
		       const char *interface,
		       uint32_t version)
{
    struct wld_connection* connection = ctx;

    /* process new interfaces appearing on the global registry */

    if (strcmp(interface, "wl_compositor") == 0) {
	/* compositor interface is available. Bind the interface */
	connection->compositor = wl_registry_bind(registry,
						  id,
						  &wl_compositor_interface,
						  1);

    } else if (strcmp(interface, "wl_shell") == 0) {
	/* shell interface is available. Bind the interface */
	connection->shell = wl_registry_bind(registry,
					     id,
					     &wl_shell_interface,
					     1);

    } else if (strcmp(interface, "wl_shm") == 0) {
	/* shared memory interface is available. Bind the interface
	 * and add a listener for the shared memory callbacks
	 */
	connection->shm = wl_registry_bind(registry,
					   id,
					   &wl_shm_interface,
					   1);
	if (connection->shm != NULL) {
	    connection->shm_formats = 0;
	    wl_shm_add_listener(connection->shm, &shm_listenter, connection);
	}

    } else if (strcmp(interface, "wl_seat") == 0) {
	struct wld_input *input;

	input = new_input_seat(connection, registry, id);
	if (input != NULL) {
	    wl_list_insert(connection->input_list.prev, &input->link);
	}
    }
}

/** registry global removal callback */
static void
registry_handle_global_remove(void *data,
			      struct wl_registry *registry,
			      uint32_t name)
{
}

/** registry global callback handlers */
static const struct wl_registry_listener registry_listener = {
    registry_handle_global,
    registry_handle_global_remove
};


static void
free_connection(struct wld_connection* connection)
{
    if (connection->compositor != NULL) {
	wl_compositor_destroy(connection->compositor);
    }

    if (connection->shell != NULL) {
	wl_shell_destroy(connection->shell);
    }

    if (connection->shm != NULL) {
	wl_shm_destroy(connection->shm);
    }

    wl_registry_destroy(connection->registry);

    wl_display_flush(connection->display);

    wl_display_disconnect(connection->display);

    free(connection);
}

/** construct a new connection to the wayland instance and aquire all
 * necessary global objects
 */
static struct wld_connection*
new_connection(void)
{
    struct wld_connection* connection;

    connection = calloc(1, sizeof(struct wld_connection));

    if (connection == NULL) {
	return NULL;
    }

    /* initialise lists */
    wl_list_init(&connection->input_list);

    /* make a connection to the wayland server */
    connection->display = wl_display_connect(NULL);
    if (connection->display == NULL) {
	fprintf(stderr, "Unable to open display\n");
	free(connection);
	return NULL;
    }

    /* create the registry object and attach its callback handlers */
    connection->registry = wl_display_get_registry(connection->display);
    if (connection->registry == NULL) {
	fprintf(stderr, "Unable create registry\n");
	wl_display_flush(connection->display);
	wl_display_disconnect(connection->display);
	free(connection);
	return NULL;
    }

    wl_registry_add_listener(connection->registry,
			     &registry_listener,
			     connection);
    /* perform a round trip to ensure registry messages have been processed */
    wl_display_roundtrip(connection->display);

    /* all global objects from the registry should now be available. */

    if (connection->shm == NULL) {
	/* The shared memory interface is available so fail. */
	fprintf(stderr, "No shared memory interface available\n");

	free_connection(connection);

	return NULL;
    }

    /* perform a round trip to ensure interface messages have been processed */
    wl_display_roundtrip(connection->display);

    /* check the XRGB8888 shared memory format is available */
    if (!(connection->shm_formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
	fprintf(stderr, "WL_SHM_FORMAT_XRGB8888 not available\n");

	free_connection(connection);

	return NULL;
    }

    return connection;
}



static int
update_and_redraw(struct wldstate_s *wldstate,
		  int x,
		  int y,
		  int width,
		  int height)
{
    wl_surface_attach(wldstate->window->surface,
		      wldstate->shm_buffer->buffer,
		      0,
		      0);

    wl_surface_damage(wldstate->window->surface, x, y, width, height);

    wl_surface_commit(wldstate->window->surface);
    wldstate->shm_buffer->inuse = true;

    /* force syncronisation to cause the update */
    wl_display_roundtrip(wldstate->connection->display);

    return 0;
}

static void
handle_ping(void *data, struct wl_shell_surface *shell_surface,
							uint32_t serial)
{
	wl_shell_surface_pong(shell_surface, serial);
}

static void
handle_configure(void *data, struct wl_shell_surface *shell_surface,
		 uint32_t edges, int32_t width, int32_t height)
{
}

static void
handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
{
}

static const struct wl_shell_surface_listener shell_surface_listener = {
	handle_ping,
	handle_configure,
	handle_popup_done
};

static struct wld_window *
new_window(struct wld_connection *connection, int width, int height)
{
    struct wld_window *window;

    window = calloc(1, sizeof *window);
    if (window == NULL) {
	return NULL;
    }

    window->connection = connection;
    window->width = width;
    window->height = height;

    window->surface = wl_compositor_create_surface(connection->compositor);
    if (window->surface == NULL) {
	fprintf(stderr, "failed to create compositor surface\n");
	free(window);
	return NULL;
    }

    window->shell_surface = wl_shell_get_shell_surface(connection->shell,
						       window->surface);
    if (window->shell_surface == NULL) {
	fprintf(stderr, "failed to create shell surface\n");

	wl_surface_destroy(window->surface);

	free(window);
	return NULL;
    }

    wl_shell_surface_add_listener(window->shell_surface,
				  &shell_surface_listener, window);

    wl_shell_surface_set_title(window->shell_surface, "nsfb");

    wl_shell_surface_set_toplevel(window->shell_surface);

    return window;
}

static void
free_window(struct wld_window *window)
{

    wl_shell_surface_destroy(window->shell_surface);
    wl_surface_destroy(window->surface);

    free(window);
}

/*
 * Create a new, unique, anonymous file of the given size, and
 * return the file descriptor for it. The file descriptor is set
 * CLOEXEC. The file is immediately suitable for mmap()'ing
 * the given size at offset zero.
 *
 * The file should not have a permanent backing store like a disk,
 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
 *
 * The file name is deleted from the file system.
 *
 * The file is suitable for buffer sharing between processes by
 * transmitting the file descriptor over Unix sockets using the
 * SCM_RIGHTS methods.
 */
static int
os_create_anonymous_file(off_t size)
{
	static const char template[] = "/weston-shared-XXXXXX";
	const char *path;
	char *name;
	int fd;

	path = getenv("XDG_RUNTIME_DIR");
	if (!path) {
		errno = ENOENT;
		return -1;
	}

	name = malloc(strlen(path) + sizeof(template));
	if (!name)
		return -1;

	strcpy(name, path);
	strcat(name, template);

	fd = mkostemp(name, O_CLOEXEC);
	if (fd >= 0)
		unlink(name);

	free(name);

	if (fd < 0)
		return -1;

	if (ftruncate(fd, size) < 0) {
		close(fd);
		return -1;
	}

	return fd;
}


static void
buffer_release(void *data, struct wl_buffer *buffer)
{
	struct wld_shm_buffer *shmbuf = data;

	shmbuf->inuse = false;
}

static const struct wl_buffer_listener buffer_listener = {
	buffer_release
};

static struct wld_shm_buffer *
new_shm_buffer(struct wl_shm *shm,
	       int width,
	       int height,
	       uint32_t format)
{
    struct wl_shm_pool *pool;
    struct wld_shm_buffer *shmbuff;
    int fd;
    int stride;

    shmbuff = calloc(1, sizeof(struct wld_shm_buffer));
    if (shmbuff == NULL) {
	return NULL;
    }

    stride = width * 4;
    shmbuff->size = stride * height;

    fd = os_create_anonymous_file(shmbuff->size);
    if (fd < 0) {
	free(shmbuff);
	return NULL;
    }

    shmbuff->data = mmap(NULL,
			 shmbuff->size,
			 PROT_READ | PROT_WRITE,
			 MAP_SHARED,
			 fd,
			 0);
    if (shmbuff->data == MAP_FAILED) {
	close(fd);
	free(shmbuff);
	return NULL;
    }

    pool = wl_shm_create_pool(shm, fd, shmbuff->size);
    shmbuff->buffer = wl_shm_pool_create_buffer(pool,
						0,
						width,
						height,
						stride,
						format);
    wl_shm_pool_destroy(pool);
    close(fd);

    if (shmbuff->buffer == NULL) {
	munmap(shmbuff->data, shmbuff->size);
	free(shmbuff);
	return NULL;
    }

    wl_buffer_add_listener(shmbuff->buffer, &buffer_listener, shmbuff);

    return shmbuff;
}

static void free_shm_buffer(struct wld_shm_buffer *shmbuf)
{
    munmap(shmbuf->data, shmbuf->size);
    free(shmbuf);
}

static int wld_initialise(nsfb_t *nsfb)
{
    wldstate_t *wldstate = nsfb->surface_priv;

    if (wldstate != NULL)
	return -1; /* already initialised */

    /* check bpp is what we can support. */
    if (nsfb->bpp != 32) {
	return -1;
    }

    wldstate = calloc(1, sizeof(wldstate_t));
    if (wldstate == NULL) {
	return -1; /* no memory */
    }

    wldstate->connection = new_connection();
    if (wldstate->connection == NULL) {
	fprintf(stderr, "Error initialising wayland connection\n");

	free(wldstate);

	return -1; /* error */
    }

    wldstate->window = new_window(wldstate->connection,
				  nsfb->width,
				  nsfb->height);
    if (wldstate->window == NULL) {
	fprintf(stderr, "Error creating wayland window\n");

	free_connection(wldstate->connection);

	free(wldstate);

	return -1; /* error */
    }

    wldstate->shm_buffer = new_shm_buffer(wldstate->connection->shm,
					  nsfb->width,
					  nsfb->height,
					  WL_SHM_FORMAT_XRGB8888);
    if (wldstate->shm_buffer == NULL) {
	fprintf(stderr, "Error creating wayland shared memory\n");

	free_window(wldstate->window);

	free_connection(wldstate->connection);

	free(wldstate);

	return -1; /* error */
    }

    nsfb->ptr = wldstate->shm_buffer->data;
    nsfb->linelen = nsfb->width * 4;

    update_and_redraw(wldstate,0,0, nsfb->width, nsfb->height);

    nsfb->surface_priv = wldstate;

    return 0;
}

static int wld_finalise(nsfb_t *nsfb)
{
    wldstate_t *wldstate = nsfb->surface_priv;

    if (wldstate == NULL) {
	return 0; /* not initialised */
    }

    free_shm_buffer(wldstate->shm_buffer);

    free_window(wldstate->window);

    free_connection(wldstate->connection);
}

#if 0
static int x_initialise(nsfb_t *nsfb)
{
    uint32_t mask;
    uint32_t values[3];
    xcb_size_hints_t *hints;
    xstate_t *xstate = nsfb->surface_priv;
    xcb_cursor_t blank_cursor;

    if (xstate != NULL)
	return -1; /* already initialised */

    /* sanity check bpp. */
    if ((nsfb->bpp != 32) && (nsfb->bpp != 16) && (nsfb->bpp != 8))
	return -1;

    xstate = calloc(1, sizeof(xstate_t));
    if (xstate == NULL)
	return -1; /* no memory */

    /* open connection with the server */
    xstate->connection = xcb_connect(NULL, NULL);
    if (xstate->connection == NULL) {
	fprintf(stderr, "Memory error opening display\n");
	free(xstate);
	return -1; /* no memory */
    }

    if (xcb_connection_has_error(xstate->connection) != 0) {
	fprintf(stderr, "Error opening display\n");
	free(xstate);
	return -1; /* no memory */
    }

    /* get screen */
    xstate->screen = xcb_setup_roots_iterator(xcb_get_setup(xstate->connection)).data;

    /* create image */
    xstate->image = create_shm_image(xstate, nsfb->width, nsfb->height, nsfb->bpp);

    if (xstate->image == NULL)
	xstate->image = create_image(xstate->connection, nsfb->width, nsfb->height, nsfb->bpp);

    if (xstate->image == NULL) {
	fprintf(stderr, "Unable to create image\n");
	free(xstate);
	xcb_disconnect(xstate->connection);
	return -1;
    }

    /* ensure plotting information is stored */
    nsfb->surface_priv = xstate;
    nsfb->ptr = xstate->image->data;
    nsfb->linelen = xstate->image->stride;

    /* get blank cursor */
    blank_cursor = create_blank_cursor(xstate->connection, xstate->screen);

    /* get keysymbol maps */
    xstate->keysymbols = xcb_key_symbols_alloc(xstate->connection);

    /* create window */
    mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
    values[0] = xstate->screen->white_pixel;
    values[1] = XCB_EVENT_MASK_EXPOSURE |
		XCB_EVENT_MASK_KEY_PRESS |
		XCB_EVENT_MASK_KEY_RELEASE |
		XCB_EVENT_MASK_BUTTON_PRESS |
		XCB_EVENT_MASK_BUTTON_RELEASE |
		XCB_EVENT_MASK_POINTER_MOTION;
    values[2] = blank_cursor;

    xstate->window = xcb_generate_id(xstate->connection);
    xcb_create_window (xstate->connection,
		       XCB_COPY_FROM_PARENT,
		       xstate->window,
		       xstate->screen->root,
		       0, 0, xstate->image->width, xstate->image->height, 1,
		       XCB_WINDOW_CLASS_INPUT_OUTPUT,
		       xstate->screen->root_visual,
		       mask, values);
    /* set size hits on window */
    hints = xcb_alloc_size_hints();
    xcb_size_hints_set_max_size(hints, xstate->image->width, xstate->image->height);
    xcb_size_hints_set_min_size(hints, xstate->image->width, xstate->image->height);
    xcb_set_wm_size_hints(xstate->connection, xstate->window, WM_NORMAL_HINTS, hints);
    xcb_free_size_hints(hints);

    /* create backing pixmap */
    xstate->pmap = xcb_generate_id(xstate->connection);
    xcb_create_pixmap(xstate->connection, 24, xstate->pmap, xstate->window, xstate->image->width, xstate->image->height);

    /* create pixmap plot gc */
    mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
    values[0] = xstate->screen->black_pixel;
    values[1] = 0xffffff;

    xstate->gc = xcb_generate_id (xstate->connection);
    xcb_create_gc(xstate->connection, xstate->gc, xstate->pmap, mask, values);

    /*    if (nsfb->bpp == 8)
	  set_palette(nsfb);
    */

    /* put the image into the pixmap */
    update_and_redraw_pixmap(xstate, 0, 0, xstate->image->width, xstate->image->height);


    /* show the window */
    xcb_map_window (xstate->connection, xstate->window);
    xcb_flush(xstate->connection);

    return 0;
}

static int x_finalise(nsfb_t *nsfb)
{
    xstate_t *xstate = nsfb->surface_priv;
    if (xstate == NULL)
	return 0;

    xcb_key_symbols_free(xstate->keysymbols);

    /* free pixmap */
    xcb_free_pixmap(xstate->connection, xstate->pmap);

    /* close connection to server */
    xcb_disconnect(xstate->connection);

    return 0;
}
#endif

static bool wld_input(nsfb_t *nsfb, nsfb_event_t *event, int timeout)
{
    wldstate_t *wldstate = nsfb->surface_priv;
    int ret = 0; /* number of events dispatched */
    struct wld_event* wldevent; /* input event off queue */

    if (wldstate == NULL) {
	return false;
    }

    /* flush any pending outgoing messages to server */
    wl_display_flush(wldstate->connection->display);

    /* if there are queued input events, return them first */
    wldevent = dequeue_wld_event(wldstate->connection);
    if (wldevent != NULL) {
	*event = wldevent->event;
	free(wldevent);
	return true;
    }

    if (timeout < 0) {
	/* caller wants to wait forever for an event */
	ret = wl_display_dispatch(wldstate->connection->display);
    } else {
	int confd;
	fd_set rfds;
	struct timeval tv;
	int retval;

	confd = wl_display_get_fd(wldstate->connection->display);

	FD_ZERO(&rfds);
	FD_SET(confd, &rfds);

	tv.tv_sec = timeout / 1000;
	tv.tv_usec = timeout % 1000;

	retval = select(confd + 1, &rfds, NULL, NULL, &tv);
	if (retval == 0) {
	    /* timeout, nothing ready to read */
	    ret = wl_display_dispatch_pending(wldstate->connection->display);
	} else {
	    ret = wl_display_dispatch(wldstate->connection->display);
	}
    }

    /* check for connection error */
    if (ret == -1) {
	/* exit on conenction error */
	event->type = NSFB_EVENT_CONTROL;
	event->value.controlcode = NSFB_CONTROL_QUIT;
	return true;

    } else if (ret == 0) {
	/* timeout and no messages were processed and the input queue was
	 * empty on entry
	 */
	event->type = NSFB_EVENT_CONTROL;
	event->value.controlcode = NSFB_CONTROL_TIMEOUT;
	return true;
    }

    /* messages were processed, they might have been input events */

    wldevent = dequeue_wld_event(wldstate->connection);
    if (wldevent == NULL) {
	/* messages were not input events so signal no event */
	return false;
    }

    *event = wldevent->event;

    free(wldevent);

    return true;
}

#if 0
static bool x_input(nsfb_t *nsfb, nsfb_event_t *event, int timeout)
{
    xcb_generic_event_t *e;
    xcb_expose_event_t *ee;
    xcb_motion_notify_event_t *emn;
    xcb_button_press_event_t *ebp;
    xcb_key_press_event_t *ekp;
    xcb_key_press_event_t *ekr;
    xstate_t *xstate = nsfb->surface_priv;

    if (xstate == NULL)
	return false;

    xcb_flush(xstate->connection);

    /* try and retrive an event immediately */
    e = xcb_poll_for_event(xstate->connection);

    if ((e == NULL) && (timeout != 0)) {
	if (timeout > 0) {
	    int confd;
	    fd_set rfds;
	    struct timeval tv;
	    int retval;

	    confd = xcb_get_file_descriptor(xstate->connection);
	    FD_ZERO(&rfds);
	    FD_SET(confd, &rfds);

	    tv.tv_sec = timeout / 1000;
	    tv.tv_usec = timeout % 1000;

	    retval = select(confd + 1, &rfds, NULL, NULL, &tv);
	    if (retval == 0) {
		    /* timeout, nothing happened */
		    event->type = NSFB_EVENT_CONTROL;
		    event->value.controlcode = NSFB_CONTROL_TIMEOUT;
		    return true;
	    }
	}
	e = xcb_wait_for_event(xstate->connection);
    }

    if (e == NULL) {
	if (xcb_connection_has_error(xstate->connection) != 0) {
	    /* connection closed quiting time */
	    event->type = NSFB_EVENT_CONTROL;
	    event->value.controlcode = NSFB_CONTROL_QUIT;
	    return true;
	} else {
	    return false; /* no event */
	}
    }

    event->type = NSFB_EVENT_NONE;

    switch (e->response_type) {
    case XCB_EXPOSE:
	ee = (xcb_expose_event_t *)e;
	xcb_copy_area(xstate->connection,
		      xstate->pmap,
		      xstate->window,
		      xstate->gc,
		      ee->x, ee->y,
		      ee->x, ee->y,
		      ee->width, ee->height);
	xcb_flush (xstate->connection);
	break;

    case XCB_MOTION_NOTIFY:
	emn = (xcb_motion_notify_event_t *)e;
	event->type = NSFB_EVENT_MOVE_ABSOLUTE;
	event->value.vector.x = emn->event_x;
	event->value.vector.y = emn->event_y;
	event->value.vector.z = 0;
	break;


    case XCB_BUTTON_PRESS:
	ebp = (xcb_button_press_event_t *)e;
	event->type = NSFB_EVENT_KEY_DOWN;

	switch (ebp->detail) {

	case X_BUTTON_LEFT:
	    event->value.keycode = NSFB_KEY_MOUSE_1;
	    break;

	case X_BUTTON_MIDDLE:
	    event->value.keycode = NSFB_KEY_MOUSE_2;
	    break;

	case X_BUTTON_RIGHT:
	    event->value.keycode = NSFB_KEY_MOUSE_3;
	    break;

	case X_BUTTON_WHEELUP:
	    event->value.keycode = NSFB_KEY_MOUSE_4;
	    break;

	case X_BUTTON_WHEELDOWN:
	    event->value.keycode = NSFB_KEY_MOUSE_5;
	    break;
	}
	break;

    case XCB_BUTTON_RELEASE:
	ebp = (xcb_button_press_event_t *)e;
	event->type = NSFB_EVENT_KEY_UP;

	switch (ebp->detail) {

	case X_BUTTON_LEFT:
	    event->value.keycode = NSFB_KEY_MOUSE_1;
	    break;

	case X_BUTTON_MIDDLE:
	    event->value.keycode = NSFB_KEY_MOUSE_2;
	    break;

	case X_BUTTON_RIGHT:
	    event->value.keycode = NSFB_KEY_MOUSE_3;
	    break;

	case X_BUTTON_WHEELUP:
	    event->value.keycode = NSFB_KEY_MOUSE_4;
	    break;

	case X_BUTTON_WHEELDOWN:
	    event->value.keycode = NSFB_KEY_MOUSE_5;
	    break;
	}
	break;


    case XCB_KEY_PRESS:
	ekp = (xcb_key_press_event_t *)e;
	event->type = NSFB_EVENT_KEY_DOWN;
	event->value.keycode = xkeysym_to_nsfbkeycode(xcb_key_symbols_get_keysym(xstate->keysymbols, ekp->detail, 0));
	break;

    case XCB_KEY_RELEASE:
	ekr = (xcb_key_release_event_t *)e;
	event->type = NSFB_EVENT_KEY_UP;
	event->value.keycode = xkeysym_to_nsfbkeycode(xcb_key_symbols_get_keysym(xstate->keysymbols, ekr->detail, 0));
	break;

    }

    free(e);

    return true;
}
#endif

static int wld_claim(nsfb_t *nsfb, nsfb_bbox_t *box)
{
    struct nsfb_cursor_s *cursor = nsfb->cursor;

    if ((cursor != NULL) &&
	(cursor->plotted == true) &&
	(nsfb_plot_bbox_intersect(box, &cursor->loc))) {
	nsfb_cursor_clear(nsfb, cursor);
    }
    return 0;
}

static int
wld_cursor(nsfb_t *nsfb, struct nsfb_cursor_s *cursor)
{
    wldstate_t *wldstate = nsfb->surface_priv;
    nsfb_bbox_t redraw;
    nsfb_bbox_t fbarea;

    if ((cursor != NULL) && (cursor->plotted == true)) {

	nsfb_plot_add_rect(&cursor->savloc, &cursor->loc, &redraw);

	/* screen area */
	fbarea.x0 = 0;
	fbarea.y0 = 0;
	fbarea.x1 = nsfb->width;
	fbarea.y1 = nsfb->height;

	nsfb_plot_clip(&fbarea, &redraw);

	nsfb_cursor_clear(nsfb, cursor);

	nsfb_cursor_plot(nsfb, cursor);

	/* TODO: This is hediously ineficient - should keep the pointer image
	 * as a surface and composite server side
	 */
	update_and_redraw(wldstate, redraw.x0, redraw.y0, redraw.x1 - redraw.x0, redraw.y1 - redraw.y0);

    }
    return true;

}


static int wld_update(nsfb_t *nsfb, nsfb_bbox_t *box)
{
    wldstate_t *wldstate = nsfb->surface_priv;
    struct nsfb_cursor_s *cursor = nsfb->cursor;

    if ((cursor != NULL) && (cursor->plotted == false)) {
	nsfb_cursor_plot(nsfb, cursor);
    }

    if (wldstate != NULL) {
	update_and_redraw(wldstate, box->x0, box->y0, box->x1 - box->x0, box->y1 - box->y0);
    }
    return 0;
}


const nsfb_surface_rtns_t wld_rtns = {
    .initialise = wld_initialise,
    .finalise = wld_finalise,
    .input = wld_input,
    .claim = wld_claim,
    .update = wld_update,
    .cursor = wld_cursor,
    .geometry = wld_set_geometry,
};

NSFB_SURFACE_DEF(wld, NSFB_SURFACE_WL, &wld_rtns)

/*
 * Local variables:
 *  c-basic-offset: 4
 *  tab-width: 8
 * End:
 */