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
|
/***************************************************************************
xlib_driver.h Copyright (C) 2000 Christoph Reichenbach
This program may be modified and copied freely according to the terms of
the GNU general public license (GPL), as long as the above copyright
notice and the licensing information contained herein are preserved.
Please refer to www.gnu.org for licensing details.
This work is provided AS IS, without warranty of any kind, expressed or
implied, including but not limited to the warranties of merchantibility,
noninfringement, and fitness for a specific purpose. The author will not
be held liable for any damage caused by this work or derivatives of it.
By using this source code, you agree to the licensing terms as stated
above.
Please contact the maintainer for bug reports or inquiries.
Current Maintainer:
Christoph Reichenbach (CR) <creichen@gmail.com>
***************************************************************************/
#include <sci_memory.h>
#include <gfx_driver.h>
#ifndef X_DISPLAY_MISSING
#include <gfx_tools.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#ifdef HAVE_MITSHM
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#if defined(HAVE_X11_EXTENSIONS_XRENDER_H)
# define HAVE_RENDER
# include <X11/extensions/Xrender.h>
# if defined(HAVE_X11_XFT_XFT_H)
# include <X11/Xft/Xft.h>
# endif
#endif
#include <errno.h>
#endif
#ifdef HAVE_XM_MWMUTIL_H
#include <Xm/MwmUtil.h>
#endif
#define SCI_XLIB_PIXMAP_HANDLE_NORMAL 0
#define SCI_XLIB_PIXMAP_HANDLE_GRABBED 1
#define SCI_XLIB_SWAP_CTRL_CAPS (1 << 0)
#define SCI_XLIB_INSERT_MODE (1 << 1)
#define SCI_XLIB_NLS (1 << 2) /* Non-US keyboard support, sacrificing shortcut keys */
#define X_COLOR_EXT(c) ((c << 8) | c)
/* In C++ mode, we re-name ``class'' to ``class_''. However, this screws up
** our interface to xlib, so we're not doing it in here.
*/
#ifdef class
# undef class
#endif
static int flags;
struct _xlib_state {
Display *display;
Window window;
GC gc;
XGCValues gc_values;
Colormap colormap;
Pixmap visual[3];
gfx_pixmap_t *priority[2];
#ifdef HAVE_MITSHM
XShmSegmentInfo *shm[4];
#endif
int use_render;
int buckystate;
XErrorHandler old_error_handler;
Cursor mouse_cursor;
byte *pointer_data[2];
int used_bytespp; /* bytes actually used to display stuff, rather than bytes occupied in data space */
XVisualInfo visinfo;
#ifdef HAVE_RENDER
Picture picture;
#endif
};
#define VISUAL S->visinfo.visual
#define S ((struct _xlib_state *)(drv->state))
#define XASS(foo) { int val = foo; if (!val) xlderror(drv, __LINE__); }
#define XFACT drv->mode->xfact
#define YFACT drv->mode->yfact
#define DEBUGB if (drv->debug_flags & GFX_DEBUG_BASIC && ((debugline = __LINE__))) xldprintf
#define DEBUGU if (drv->debug_flags & GFX_DEBUG_UPDATES && ((debugline = __LINE__))) xldprintf
#define DEBUGPXM if (drv->debug_flags & GFX_DEBUG_PIXMAPS && ((debugline = __LINE__))) xldprintf
#define DEBUGPTR if (drv->debug_flags & GFX_DEBUG_POINTER && ((debugline = __LINE__))) xldprintf
#define ERROR if ((debugline = __LINE__)) xldprintf
#ifdef HAVE_MITSHM
XShmSegmentInfo shminfo;
int have_shmem = 0;
int x11_error = 0;
static int check_for_xshm(Display *display)
{
int major, minor, ignore;
Bool pixmaps;
if (XQueryExtension(display, "MIT-SHM", &ignore, &ignore, &ignore)) {
if (XShmQueryVersion( display, &major, &minor, &pixmaps) == True) {
return (pixmaps == True) ? 2 : 1 ;
} else {
return 0;
}
}
return 0;
}
#endif
#ifdef HAVE_RENDER
static int x_have_render(Display *display)
{
int ignore, retval;
printf("Checking for X11 RENDER extension:");
retval = XQueryExtension(display, "RENDER", &ignore, &ignore, &ignore);
if (retval)
printf(" found.\n");
else
printf(" not found.\n");
return retval;
}
#endif
static int debugline = 0;
static void
xldprintf(const char *fmt, ...)
{
va_list argp;
fprintf(stderr,"GFX-XLIB %d:", debugline);
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
}
static void
xlderror(gfx_driver_t *drv, int line)
{
xldprintf("Xlib Error in line %d\n", line);
}
static unsigned long
xlib_map_color(gfx_driver_t *drv, gfx_color_t color)
{
gfx_mode_t *mode = drv->mode;
unsigned long temp;
unsigned long retval = 0;
if (drv->mode->palette)
return color.visual.global_index;
temp = color.visual.r;
temp |= temp << 8;
temp |= temp << 16;
retval |= (temp >> mode->red_shift) & (mode->red_mask);
temp = color.visual.g;
temp |= temp << 8;
temp |= temp << 16;
retval |= (temp >> mode->green_shift) & (mode->green_mask);
temp = color.visual.b;
temp |= temp << 8;
temp |= temp << 16;
retval |= (temp >> mode->blue_shift) & (mode->blue_mask);
return retval;
}
static int
xlib_error_handler(Display *display, XErrorEvent *error)
{
char errormsg[256];
#ifdef HAVE_MITSHM
x11_error = 1;
#endif
XGetErrorText(display, error->error_code, errormsg, 255);
ERROR(" X11: %s\n", errormsg);
return 0;
}
#define UPDATE_NLS_CAPABILITY \
if (flags & SCI_XLIB_NLS) \
drv->capabilities |= GFX_CAPABILITY_KEYTRANSLATE; \
else \
drv->capabilities &= ~GFX_CAPABILITY_KEYTRANSLATE
static int
xlib_set_parameter(struct _gfx_driver *drv, char *attribute, char *value)
{
if (!strncmp(attribute, "swap_ctrl_caps",17) ||
!strncmp(attribute, "swap_caps_ctrl",17)) {
if (string_truep(value))
flags |= SCI_XLIB_SWAP_CTRL_CAPS;
else
flags &= ~SCI_XLIB_SWAP_CTRL_CAPS;
return GFX_OK;
}
if (!strncmp(attribute, "localised_keyboard", 18)
|| !strncmp(attribute, "localized_keyboard", 18)) {
if (string_truep(value))
flags |= SCI_XLIB_NLS;
else
flags &= ~SCI_XLIB_NLS;
UPDATE_NLS_CAPABILITY;
return GFX_OK;
}
if (!strncmp(attribute, "disable_shmem", 14)) {
#ifdef HAVE_MITSHM
if (string_truep(value))
have_shmem = -1;
#endif
return GFX_OK;
}
ERROR("Attempt to set xlib parameter \"%s\" to \"%s\"\n", attribute, value);
return GFX_ERROR;
}
Cursor
x_empty_cursor(Display *display, Drawable drawable) /* Generates an empty X cursor */
{
byte cursor_data[] = {0};
XColor black = {0,0,0};
Pixmap cursor_map;
Cursor retval;
cursor_map = XCreateBitmapFromData(display, drawable, (char *) cursor_data, 1, 1);
retval = XCreatePixmapCursor(display, cursor_map, cursor_map, &black, &black, 0, 0);
XFreePixmap(display, cursor_map);
return retval;
}
static int
xlib_draw_filled_rect(struct _gfx_driver *drv, rect_t rect,
gfx_color_t color1, gfx_color_t color2,
gfx_rectangle_fill_t shade_mode);
static int
xlib_draw_pixmap(struct _gfx_driver *drv, gfx_pixmap_t *pxm, int priority,
rect_t src, rect_t dest, gfx_buffer_t buffer);
static int
xlib_init_specific(struct _gfx_driver *drv, int xfact, int yfact, int bytespp)
{
XVisualInfo xvisinfo;
XSetWindowAttributes win_attr;
int default_screen;
int vistype = (bytespp == 1)? 3 /* PseudoColor */ : 4 /* TrueColor */;
int red_shift, green_shift, blue_shift, alpha_shift;
int bytespp_physical;
int depth_mod; /* Number of bits to subtract from the depth while checking */
unsigned int alpha_mask;
int xsize, ysize;
XSizeHints *size_hints;
XClassHint *class_hint;
XImage *foo_image = NULL;
int reverse_endian = 0;
#ifdef HAVE_XM_MWMUTIL_H
PropMotifWmHints motif_hints;
Atom prop, proptype;
#endif
int i;
UPDATE_NLS_CAPABILITY;
if (!drv->state /* = S */)
drv->state = sci_malloc(sizeof(struct _xlib_state));
flags |= SCI_XLIB_INSERT_MODE;
S->display = XOpenDisplay(NULL);
if (!S->display) {
ERROR("Could not open X connection!\n");
return GFX_FATAL;
}
default_screen = DefaultScreen(S->display);
if (xfact == -1 && yfact == -1) { /* Detect (used INTERNALLY!) */
xfact = 2;
if (DisplayWidth(S->display, default_screen) < 640
|| DisplayHeight(S->display, default_screen) < 400)
xfact = 1;
yfact = xfact;
}
xsize = xfact * 320;
ysize = yfact * 200;
if (xfact < 1 || yfact < 1 || bytespp < 1 || bytespp > 4) {
ERROR("Internal error: Attempt to open window w/ scale factors (%d,%d) and bpp=%d!\n",
xfact, yfact, bytespp);
BREAKPOINT();
}
#ifdef HAVE_MITSHM
if (!have_shmem) {
have_shmem = check_for_xshm(S->display);
if (have_shmem) {
printf("Using the MIT-SHM extension (%d/%d)\n", have_shmem,
XShmPixmapFormat(S->display));
}
memset(&shminfo, 0, sizeof(XShmSegmentInfo));
}
for (i = 0; i < 4; i++)
S->shm[i] = NULL;
#endif
depth_mod = 0;
/* Try all bit size twiddling */
for (depth_mod = 0; depth_mod < 8; depth_mod++)
/* Try all viable depths */
for (; bytespp > 0; bytespp--)
/* Try all interesting visuals */
for (vistype = 4; vistype >= 3; vistype--)
if (XMatchVisualInfo(S->display, default_screen,
(bytespp << 3) - depth_mod, vistype, &xvisinfo))
goto found_visual;
found_visual:
S->visinfo = xvisinfo;
if (vistype < 3 || ((vistype == 3) && (bytespp != 1))) {
if (bytespp == 1) {
ERROR("Could not get an 8 bit Pseudocolor visual!\n");
} else {
ERROR("Could not get a %d bit TrueColor visual!\n", bytespp << 3);
}
return GFX_FATAL;
}
S->colormap = win_attr.colormap =
XCreateColormap(S->display, RootWindow(S->display, default_screen),
VISUAL, (bytespp == 1)? AllocAll : AllocNone);
win_attr.event_mask = PointerMotionMask | StructureNotifyMask | ButtonPressMask
| ButtonReleaseMask | KeyPressMask | KeyReleaseMask | ExposureMask;
win_attr.background_pixel = win_attr.border_pixel = 0;
S->window = XCreateWindow(S->display, RootWindow(S->display, default_screen),
0, 0, xsize, ysize, 0, xvisinfo.depth, InputOutput,
VISUAL, (CWBackPixel | CWBorderPixel | CWColormap | CWEventMask),
&win_attr);
if (!S->window) {
ERROR("Could not create window of size %dx%d!\n", 320*xfact, 200*yfact);
return GFX_FATAL;
}
XSync(S->display, False);
#ifdef HAVE_XM_MWMUTIL_H
motif_hints.flags = MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS;
motif_hints.decorations = MWM_DECOR_BORDER|MWM_DECOR_TITLE|MWM_DECOR_MENU|MWM_DECOR_MINIMIZE;
motif_hints.functions=0
#ifdef MWM_FUNC_MOVE
| MWM_FUNC_MOVE
#endif
#ifdef MWM_FUNC_MINIMIZE
| MWM_FUNC_MINIMIZE
#endif
#ifdef MWM_FUNC_CLOSE
| MWM_FUNC_CLOSE
#endif
#ifdef MWM_FUNC_QUIT_APP
| MWM_FUNC_QUIT_APP
#endif
;
prop = XInternAtom(S->display, "_MOTIF_WM_HINTS", True );
if (prop) {
proptype = prop;
XChangeProperty(S->display, S->window, prop, proptype, 32, PropModeReplace, (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
}
#endif
XStoreName(S->display, S->window, "FreeSCI");
XDefineCursor(S->display, S->window, (S->mouse_cursor = x_empty_cursor(S->display, S->window)));
XMapWindow(S->display, S->window);
S->buckystate = 0;
if (bytespp == 1)
red_shift = green_shift = blue_shift = 0;
else {
red_shift = 32 - ffs((~xvisinfo.red_mask >> 1) & (xvisinfo.red_mask));
green_shift = 32 - ffs((~xvisinfo.green_mask >> 1) & (xvisinfo.green_mask));
blue_shift = 32 - ffs((~xvisinfo.blue_mask >> 1) & (xvisinfo.blue_mask));
}
class_hint = XAllocClassHint();
class_hint->res_name = "FreeSCI";
class_hint->res_class = "FreeSCI";
XSetIconName(S->display, S->window, "FreeSCI");
XSetClassHint(S->display, S->window, class_hint);
XFree(class_hint);
size_hints = XAllocSizeHints();
size_hints->base_width = size_hints->min_width = size_hints->max_width = xsize;
size_hints->base_height = size_hints->min_height = size_hints->max_height = ysize;
size_hints->flags |= PMinSize | PMaxSize | PBaseSize;
XSetWMNormalHints(S->display, S->window, size_hints);
XFree(size_hints);
S->gc_values.foreground = BlackPixel(S->display, DefaultScreen(S->display));
S->gc = XCreateGC(S->display, S->window, GCForeground, &(S->gc_values));
for (i = 0; i < 2; i++) {
S->priority[i] = gfx_pixmap_alloc_index_data(gfx_new_pixmap(xsize, ysize, GFX_RESID_NONE, -i, -777));
if (!S->priority[i]) {
ERROR("Out of memory: Could not allocate priority maps! (%dx%d)\n",
xsize, ysize);
return GFX_FATAL;
}
}
foo_image = XCreateImage(S->display, VISUAL,
bytespp << 3, ZPixmap, 0, (char*)sci_malloc(23), 2, 2, 8, 0);
bytespp_physical = foo_image->bits_per_pixel >> 3;
#ifdef WORDS_BIGENDIAN
reverse_endian = foo_image->byte_order == LSBFirst;
#else
reverse_endian = foo_image->byte_order == MSBFirst;
#endif
XDestroyImage(foo_image);
#ifdef HAVE_MITSHM
/* set up and test the XSHM extension to make sure it's sane */
if (have_shmem) {
XErrorHandler old_handler;
x11_error = 0;
old_handler = XSetErrorHandler(xlib_error_handler);
foo_image = XShmCreateImage(S->display, VISUAL,
bytespp_physical << 3, ZPixmap, 0, &shminfo, 2, 2);
if (foo_image)
shminfo.shmid = shmget(IPC_PRIVATE,
foo_image->bytes_per_line *
foo_image->height,
IPC_CREAT | 0777);
if (-1 == shminfo.shmid) {
have_shmem = 0;
ERROR("System does not support SysV IPC, disabling XSHM\n");
perror("reason");
foo_image = NULL;
} else {
shminfo.shmaddr = (char *) shmat(shminfo.shmid, 0, 0);
if ((void *) -1 == shminfo.shmaddr) {
ERROR("Could not attach shared memory segment\n");
perror("reason");
if (foo_image)
XDestroyImage(foo_image);
return GFX_FATAL;
}
foo_image->data = shminfo.shmaddr;
shminfo.readOnly = False;
XShmAttach(S->display, &shminfo);
XSync(S->display, False);
shmctl(shminfo.shmid, IPC_RMID, 0);
if (x11_error) {
have_shmem = 0;
ERROR("System does not support Shared XImages, disabling\n");
shmdt(shminfo.shmaddr);
XDestroyImage(foo_image);
foo_image = NULL;
x11_error = 0;
}
XSetErrorHandler(old_handler);
}
}
#endif
alpha_mask = xvisinfo.red_mask | xvisinfo.green_mask | xvisinfo.blue_mask;
if (!reverse_endian && bytespp_physical == 4 && (!(alpha_mask & 0xff000000) || !(alpha_mask & 0xff))) {
if (alpha_mask & 0xff) {
alpha_mask = 0xff000000;
alpha_shift = 0;
} else { /* Lowest byte */
alpha_mask = 0x000000ff;
alpha_shift = 24;
}
} else {
alpha_mask = 0;
alpha_shift = 0;
}
/* create the visual buffers */
for (i = 0; i < 3; i++) {
#ifdef HAVE_MITSHM
XErrorHandler old_handler;
if (have_shmem && have_shmem != 2) {
ERROR("Shared memory pixmaps not supported. Reverting\n");
perror("reason");
have_shmem = 0;
}
if (have_shmem) {
old_handler = XSetErrorHandler(xlib_error_handler);
if ((S->shm[i] = (XShmSegmentInfo*)sci_malloc(sizeof(XShmSegmentInfo))) == 0) {
ERROR("AIEEEE! Malloc failed!\n");
return GFX_FATAL;
}
memset(S->shm[i], 0, sizeof(XShmSegmentInfo));
S->shm[i]->shmid = shmget(IPC_PRIVATE, xsize * ysize *
bytespp_physical,
IPC_CREAT | IPC_EXCL | 0666);
S->shm[i]->readOnly = False;
if (S->shm[i]->shmid == -1) {
have_shmem = 0;
ERROR("System does not support SysV IPC, disabling XSHM\n");
perror("reason");
}
}
if (have_shmem) {
S->shm[i]->shmaddr = (char *) shmat(S->shm[i]->shmid, 0, 0);
if (S->shm[i]->shmaddr == (void *) -1) {
ERROR("Could not attach shared memory segment\n");
perror("reason");
have_shmem = 0;
}
}
if (have_shmem) {
if (!XShmAttach(S->display, S->shm[i]) || x11_error) {
ERROR("ARGH! Can't attach shared memory segment\n");
have_shmem = 0;
}
XSync(S->display, False);
shmctl(S->shm[i]->shmid, IPC_RMID, 0);
}
if (have_shmem && !x11_error) {
S->visual[i] = XShmCreatePixmap(S->display, S->window,
S->shm[i]->shmaddr,
S->shm[i], xsize, ysize,
bytespp << 3);
XSync(S->display, False);
if (x11_error || !S->visual[i]) {
ERROR("Shared Memory Pixmaps not supported on this system. Disabling!\n");
have_shmem = 0;
XFreePixmap(S->display, S->visual[i]);
XShmDetach(S->display ,S->shm[i]);
XSync(S->display, False);
S->visual[i] = 0;
x11_error = 0;
shmdt(S->shm[i]->shmaddr);
sci_free(S->shm[i]);
}
}
XSetErrorHandler(old_handler);
if (!have_shmem)
#endif
S->visual[i] = XCreatePixmap(S->display, S->window, xsize, ysize, bytespp << 3);
XFillRectangle(S->display, S->visual[i], S->gc, 0, 0, xsize, ysize);
}
/** X RENDER handling **/
#ifdef HAVE_RENDER
S->use_render = x_have_render(S->display);
if (S->use_render) {
XRenderPictFormat * format
= XRenderFindVisualFormat (S->display,
VISUAL);
S->picture = XRenderCreatePicture (S->display,
(Drawable) S->visual[1],
format,
0, 0);
} else /* No Xrender */
drv->draw_filled_rect = xlib_draw_filled_rect;
#else
S->use_render = 0;
#endif
/** End of X RENDER handling **/
drv->mode = gfx_new_mode(xfact, yfact, bytespp_physical,
xvisinfo.red_mask, xvisinfo.green_mask,
xvisinfo.blue_mask, alpha_mask,
red_shift, green_shift, blue_shift, alpha_shift,
(bytespp == 1)? xvisinfo.colormap_size : 0,
(reverse_endian)? GFX_MODE_FLAG_REVERSE_ENDIAN : 0);
#ifdef HAVE_MITSHM
if (have_shmem) {
XShmDetach(S->display, &shminfo);
XDestroyImage(foo_image);
shmdt(shminfo.shmaddr);
}
#endif
S->used_bytespp = bytespp;
S->old_error_handler = (XErrorHandler) XSetErrorHandler(xlib_error_handler);
S->pointer_data[0] = NULL;
S->pointer_data[1] = NULL;
return GFX_OK;
}
static void
xlib_xdpy_info()
{
int i;
XVisualInfo foo;
XVisualInfo *visuals;
int visuals_nr;
Display *display = XOpenDisplay(NULL);
const char *vis_classes[6] = {"StaticGray", "GrayScale", "StaticColor",
"PseudoColor", "TrueColor", "DirectColor"};
printf("Visuals provided by X11 server:\n");
visuals = XGetVisualInfo(display, VisualNoMask, &foo, &visuals_nr);
if (!visuals_nr) {
printf(" None!\n");
}
for (i = 0; i < visuals_nr; i++) {
XVisualInfo *visual = visuals + i;
/* This works around an incompatibility between C++ and xlib: access visual->class */
int visual_class = *((int *) (((byte *)(&(visual->depth))) + sizeof(unsigned int)));
printf("%d:\t%d bpp %s(%d)\n"
"\tR:%08lx G:%08lx B:%08lx\n"
"\tbits_per_rgb=%d\n"
"\tcolormap_size=%d\n\n",
i,
visual->depth,
(visual_class < 0 || visual_class >5)?
"INVALID" :
vis_classes[visual_class],
visual_class,
visual->red_mask, visual->green_mask, visual->blue_mask,
visual->bits_per_rgb, visual->colormap_size);
}
if (visuals)
XFree(visuals);
}
static int
xlib_init(struct _gfx_driver *drv)
{
int i;
/* Try 32-bit mode last due to compiz issue with bit depth 32. */
for (i = 3; i > 0; i--)
if (!xlib_init_specific(drv, -1, -1, i))
return GFX_OK;
if (!xlib_init_specific(drv, -1, -1, 4))
return GFX_OK;
fprintf(stderr, "Could not find supported mode!\n");
xlib_xdpy_info();
return GFX_FATAL;
}
static void
xlib_exit(struct _gfx_driver *drv)
{
int i;
if (S) {
for (i = 0; i < 2; i++) {
gfx_free_pixmap(drv, S->priority[i]);
S->priority[i] = NULL;
}
for (i = 0; i < 3; i++) {
#ifdef HAVE_MITSHM
if (have_shmem && S->shm[i]) {
XFreePixmap(S->display, S->visual[i]);
XShmDetach(S->display, S->shm[i]);
if (S->shm[i]->shmid >=0)
shmctl(S->shm[i]->shmid, IPC_RMID, 0);
if (S->shm[i]->shmaddr)
shmdt(S->shm[i]->shmaddr);
sci_free(S->shm[i]);
S->shm[i] = NULL;
} else
#endif
XFreePixmap(S->display, S->visual[i]);
}
#ifdef HAVE_RENDER
XRenderFreePicture(S->display, S->picture);
#endif
XFreeGC(S->display, S->gc);
XDestroyWindow(S->display, S->window);
XCloseDisplay(S->display);
XSetErrorHandler((XErrorHandler) (S->old_error_handler));
sci_free(S);
drv->state /* = S */ = NULL;
gfx_free_mode(drv->mode);
}
}
/*** Drawing operations ***/
static int
xlib_draw_line(struct _gfx_driver *drv, point_t start, point_t end, gfx_color_t color,
gfx_line_mode_t line_mode, gfx_line_style_t line_style)
{
int linewidth = (line_mode == GFX_LINE_MODE_FINE)? 1:
(drv->mode->xfact + drv->mode->yfact) >> 1;
if (color.mask & GFX_MASK_VISUAL) {
int xmod = drv->mode->xfact >> 1;
int ymod = drv->mode->yfact >> 1;
if (line_mode == GFX_LINE_MODE_FINE)
xmod = ymod = 0;
S->gc_values.foreground = xlib_map_color(drv, color);
S->gc_values.line_width = linewidth;
S->gc_values.line_style = (line_style == GFX_LINE_STYLE_NORMAL)?
LineSolid : LineOnOffDash;
S->gc_values.cap_style = CapProjecting;
XChangeGC(S->display, S->gc, GCLineWidth | GCLineStyle | GCForeground | GCCapStyle, &(S->gc_values));
XASS(XDrawLine(S->display, S->visual[1], S->gc,
start.x + xmod, start.y + ymod,
end.x + xmod, end.y + ymod));
}
if (color.mask & GFX_MASK_PRIORITY) {
int xc, yc;
point_t nstart, nend;
linewidth--;
for (xc = 0; xc <= linewidth; xc++)
for (yc = -linewidth; yc <= linewidth; yc++) {
nstart.x = start.x + xc;
nstart.y = start.y + yc;
nend.x = end.x + xc;
nend.y = end.y + yc;
gfx_draw_line_pixmap_i(S->priority[0],
nstart, nend, color.priority);
}
}
return GFX_OK;
}
static int
xlib_draw_filled_rect(struct _gfx_driver *drv, rect_t rect,
gfx_color_t color1, gfx_color_t color2,
gfx_rectangle_fill_t shade_mode)
{
if (color1.mask & GFX_MASK_VISUAL) {
S->gc_values.foreground = xlib_map_color(drv, color1);
XChangeGC(S->display, S->gc, GCForeground, &(S->gc_values));
XASS(XFillRectangle(S->display, S->visual[1], S->gc, rect.x, rect.y,
rect.xl, rect.yl));
}
if (color1.mask & GFX_MASK_PRIORITY)
gfx_draw_box_pixmap_i(S->priority[0], rect, color1.priority);
return GFX_OK;
}
#ifdef HAVE_RENDER
static int
xlib_draw_filled_rect_RENDER(struct _gfx_driver *drv, rect_t rect,
gfx_color_t color1, gfx_color_t color2,
gfx_rectangle_fill_t shade_mode)
{
if (S->used_bytespp == 1) /* No room for alpha! */
return xlib_draw_filled_rect(drv, rect, color1, color2, shade_mode);
if (color1.mask & GFX_MASK_VISUAL) {
XRenderColor fg;
fg.red = X_COLOR_EXT(color1.visual.r);
fg.green = X_COLOR_EXT(color1.visual.g);
fg.blue = X_COLOR_EXT(color1.visual.b);
fg.alpha = 0xffff - X_COLOR_EXT(color1.alpha);
XRenderFillRectangle(S->display,
PictOpOver,
S->picture,
&fg,
rect.x, rect.y,
rect.xl, rect.yl);
}
if (color1.mask & GFX_MASK_PRIORITY)
gfx_draw_box_pixmap_i(S->priority[0], rect, color1.priority);
return GFX_OK;
}
#endif
/*** Pixmap operations ***/
static int
xlib_register_pixmap(struct _gfx_driver *drv, gfx_pixmap_t *pxm)
{
if (pxm->internal.info) {
ERROR("Attempt to register pixmap twice!\n");
return GFX_ERROR;
}
pxm->internal.info = XCreateImage(S->display, VISUAL,
S->used_bytespp << 3, ZPixmap, 0, (char *) pxm->data, pxm->xl,
pxm->yl, 8, 0);
DEBUGPXM("Registered pixmap %d/%d/%d at %p (%dx%d)\n", pxm->ID, pxm->loop, pxm->cel,
pxm->internal.info, pxm->xl, pxm->yl);
return GFX_OK;
}
static int
xlib_unregister_pixmap(struct _gfx_driver *drv, gfx_pixmap_t *pxm)
{
DEBUGPXM("Freeing pixmap %d/%d/%d at %p\n", pxm->ID, pxm->loop, pxm->cel,
pxm->internal.info);
if (!pxm->internal.info) {
ERROR("Attempt to unregister pixmap twice!\n");
return GFX_ERROR;
}
XDestroyImage((XImage *) pxm->internal.info);
pxm->internal.info = NULL;
pxm->data = NULL; /* Freed by XDestroyImage */
return GFX_OK;
}
static int
xlib_draw_pixmap(struct _gfx_driver *drv, gfx_pixmap_t *pxm, int priority,
rect_t src, rect_t dest, gfx_buffer_t buffer)
{
int bufnr = (buffer == GFX_BUFFER_STATIC)? 2:1;
int pribufnr = bufnr -1;
XImage *tempimg;
if (dest.xl != src.xl || dest.yl != src.yl) {
ERROR("Attempt to scale pixmap (%dx%d)->(%dx%d): Not supported\n",
src.xl, src.yl, dest.xl, dest.yl);
return GFX_ERROR;
}
if (pxm->internal.handle == SCI_XLIB_PIXMAP_HANDLE_GRABBED) {
XPutImage(S->display, S->visual[bufnr], S->gc, (XImage *) pxm->internal.info,
src.x, src.y, dest.x, dest.y, dest.xl, dest.yl);
return GFX_OK;
}
tempimg = XGetImage(S->display, S->visual[bufnr], dest.x, dest.y,
dest.xl, dest.yl, 0xffffffff, ZPixmap);
if (!tempimg) {
ERROR("Failed to grab X image!\n");
return GFX_ERROR;
}
gfx_crossblit_pixmap(drv->mode, pxm, priority, src, dest,
(byte *) tempimg->data, tempimg->bytes_per_line,
S->priority[pribufnr]->index_data,
S->priority[pribufnr]->index_xl, 1,
GFX_CROSSBLIT_FLAG_DATA_IS_HOMED);
XPutImage(S->display, S->visual[bufnr], S->gc, tempimg,
0, 0, dest.x, dest.y, dest.xl, dest.yl);
XDestroyImage(tempimg);
return GFX_OK;
}
static int
xlib_grab_pixmap(struct _gfx_driver *drv, rect_t src, gfx_pixmap_t *pxm,
gfx_map_mask_t map)
{
if (src.x < 0 || src.y < 0) {
ERROR("Attempt to grab pixmap from invalid coordinates (%d,%d)\n", src.x, src.y);
return GFX_ERROR;
}
if (!pxm->data) {
ERROR("Attempt to grab pixmap to unallocated memory\n");
return GFX_ERROR;
}
switch (map) {
case GFX_MASK_VISUAL:
pxm->xl = src.xl;
pxm->yl = src.yl;
pxm->internal.info = XGetImage(S->display, S->visual[1], src.x, src.y,
src.xl, src.yl, 0xffffffff, ZPixmap);
pxm->internal.handle = SCI_XLIB_PIXMAP_HANDLE_GRABBED;
pxm->flags |= GFX_PIXMAP_FLAG_INSTALLED | GFX_PIXMAP_FLAG_EXTERNAL_PALETTE | GFX_PIXMAP_FLAG_PALETTE_SET;
sci_free(pxm->data);
pxm->data = (byte *) ((XImage *)(pxm->internal.info))->data;
break;
case GFX_MASK_PRIORITY:
ERROR("FIXME: priority map grab not implemented yet!\n");
break;
default:
ERROR("Attempt to grab pixmap from invalid map 0x%02x\n", map);
return GFX_ERROR;
}
return GFX_OK;
}
/*** Buffer operations ***/
static int
xlib_update(struct _gfx_driver *drv, rect_t src, point_t dest, gfx_buffer_t buffer)
{
int data_source = (buffer == GFX_BUFFER_BACK)? 2 : 1;
int data_dest = data_source - 1;
if (src.x != dest.x || src.y != dest.y) {
DEBUGU("Updating %d (%d,%d)(%dx%d) to (%d,%d)\n", buffer, src.x, src.y,
src.xl, src.yl, dest.x, dest.y);
} else {
DEBUGU("Updating %d (%d,%d)(%dx%d)\n", buffer, src.x, src.y, src.xl, src.yl);
}
XCopyArea(S->display, S->visual[data_source], S->visual[data_dest], S->gc,
src.x, src.y, src.xl, src.yl, dest.x, dest.y);
if (buffer == GFX_BUFFER_BACK && (src.x == dest.x) && (src.y == dest.y))
gfx_copy_pixmap_box_i(S->priority[0], S->priority[1], src);
else {
gfx_color_t col;
col.mask = GFX_MASK_VISUAL;
col.visual.r = 0xff;
col.visual.g = 0;
col.visual.b = 0;
XCopyArea(S->display, S->visual[0], S->window, S->gc,
dest.x, dest.y, src.xl, src.yl, dest.x, dest.y);
}
return GFX_OK;
}
static int
xlib_set_static_buffer(struct _gfx_driver *drv, gfx_pixmap_t *pic, gfx_pixmap_t *priority)
{
if (!pic->internal.info) {
ERROR("Attempt to set static buffer with unregisterd pixmap!\n");
return GFX_ERROR;
}
XPutImage(S->display, S->visual[2], S->gc, (XImage *) pic->internal.info,
0, 0, 0, 0, 320 * XFACT, 200 * YFACT);
gfx_copy_pixmap_box_i(S->priority[1], priority, gfx_rect(0, 0, 320*XFACT, 200*YFACT));
return GFX_OK;
}
/*** Mouse pointer operations ***/
byte *
xlib_create_cursor_data(gfx_driver_t *drv, gfx_pixmap_t *pointer, int mode)
{
int linewidth = (pointer->xl + 7) >> 3;
int lines = pointer->yl;
int xc, yc;
int xfact = drv->mode->xfact;
byte *data = (byte*)sci_calloc(linewidth, lines);
byte *linebase = data, *pos;
byte *src = pointer->index_data;
for (yc = 0; yc < pointer->index_yl; yc++) {
int scalectr;
int bitc = 0;
pos = linebase;
for (xc = 0; xc < pointer->index_xl; xc++) {
int draw = mode?
(*src == 0) : (*src < 255);
for (scalectr = 0; scalectr < xfact; scalectr++) {
if (draw)
*pos |= (1 << bitc);
bitc++;
if (bitc == 8) {
bitc = 0;
pos++;
}
}
src++;
}
for (scalectr = 1; scalectr < drv->mode->yfact; scalectr++)
memcpy(linebase + linewidth * scalectr, linebase, linewidth);
linebase += linewidth * drv->mode->yfact;
}
return data;
}
static int
xlib_set_pointer(struct _gfx_driver *drv, gfx_pixmap_t *pointer)
{
int i;
XFreeCursor(S->display, S->mouse_cursor);
for (i = 0; i < 2; i++)
if (S->pointer_data[i]) {
sci_free(S->pointer_data[i]);
S->pointer_data[i] = NULL;
}
if (pointer == NULL)
S->mouse_cursor = x_empty_cursor(S->display, S->window);
else {
XColor cols[2];
Pixmap visual, mask;
byte *mask_data, *visual_data;
int real_xl = ((pointer->xl + 7) >> 3) << 3;
int i;
for (i = 0; i < 2; i++) {
cols[i].red = pointer->colors[i].r;
cols[i].red |= (cols[i].red << 8);
cols[i].green = pointer->colors[i].g;
cols[i].green |= (cols[i].green << 8);
cols[i].blue = pointer->colors[i].b;
cols[i].blue |= (cols[i].blue << 8);
}
S->pointer_data[0] = visual_data = xlib_create_cursor_data(drv, pointer, 1);
S->pointer_data[1] = mask_data = xlib_create_cursor_data(drv, pointer, 0);
S->pointer_data[0] = NULL;
S->pointer_data[1] = NULL;
visual = XCreateBitmapFromData(S->display, S->window, (char *) visual_data, real_xl, pointer->yl);
mask = XCreateBitmapFromData(S->display, S->window, (char *) mask_data, real_xl, pointer->yl);
S->mouse_cursor =
XCreatePixmapCursor(S->display, visual, mask,
&(cols[0]), &(cols[1]),
pointer->xoffset, pointer->yoffset);
XFreePixmap(S->display, visual);
XFreePixmap(S->display, mask);
sci_free(mask_data);
sci_free(visual_data);
}
XDefineCursor(S->display, S->window, S->mouse_cursor);
return 0;
}
/*** Palette operations ***/
static int
xlib_set_palette(struct _gfx_driver *drv, int index, byte red, byte green, byte blue)
{
char stringbuf[8];
sprintf(stringbuf, "#%02x%02x%02x", red, green, blue); /* Argh. */
XStoreNamedColor(S->display, S->colormap, stringbuf, index, DoRed | DoGreen | DoBlue);
/* Isn't there some way to do this without strings? */
return GFX_OK;
}
/*** Event management ***/
/*
int
x_unmap_key(gfx_driver_t *drv, int keycode)
{
KeySym xkey = XKeycodeToKeysym(S->display, keycode, 0);
return 0;
}
*/
int
x_map_key(gfx_driver_t *drv, XEvent *key_event, char *character)
{
KeySym xkey = XKeycodeToKeysym(S->display, key_event->xkey.keycode, 0);
*character = 0;
if (flags & SCI_XLIB_SWAP_CTRL_CAPS) {
switch (xkey) {
case XK_Control_L: xkey = XK_Caps_Lock; break;
case XK_Caps_Lock: xkey = XK_Control_L; break;
}
}
switch(xkey) {
case XK_BackSpace: return SCI_K_BACKSPACE;
case XK_Tab: return 9;
case XK_Escape: return SCI_K_ESC;
case XK_Return:
case XK_KP_Enter: return SCI_K_ENTER;
case XK_KP_Decimal:
case XK_KP_Delete: return SCI_K_DELETE;
case XK_KP_0:
case XK_KP_Insert: return SCI_K_INSERT;
case XK_End:
case XK_KP_End:
case XK_KP_1: return SCI_K_END;
case XK_Down:
case XK_KP_Down:
case XK_KP_2: return SCI_K_DOWN;
case XK_Page_Down:
case XK_KP_Page_Down:
case XK_KP_3: return SCI_K_PGDOWN;
case XK_Left:
case XK_KP_Left:
case XK_KP_4: return SCI_K_LEFT;
case XK_KP_Begin:
case XK_KP_5: return SCI_K_CENTER;
case XK_Right:
case XK_KP_Right:
case XK_KP_6: return SCI_K_RIGHT;
case XK_Home:
case XK_KP_Home:
case XK_KP_7: return SCI_K_HOME;
case XK_Up:
case XK_KP_Up:
case XK_KP_8: return SCI_K_UP;
case XK_Page_Up:
case XK_KP_Page_Up:
case XK_KP_9: return SCI_K_PGUP;
case XK_F1: return SCI_K_F1;
case XK_F2: return SCI_K_F2;
case XK_F3: return SCI_K_F3;
case XK_F4: return SCI_K_F4;
case XK_F5: return SCI_K_F5;
case XK_F6: return SCI_K_F6;
case XK_F7: return SCI_K_F7;
case XK_F8: return SCI_K_F8;
case XK_F9: return SCI_K_F9;
case XK_F10: return SCI_K_F10;
case XK_Control_L:
case XK_Control_R:/* S->buckystate |= SCI_EVM_CTRL; return 0; */
case XK_Alt_L:
case XK_Alt_R:/* S->buckystate |= SCI_EVM_ALT; return 0; */
case XK_Caps_Lock:
case XK_Shift_Lock:/* S->buckystate ^= SCI_EVM_CAPSLOCK; return 0; */
case XK_Scroll_Lock:/* S->buckystate ^= SCI_EVM_SCRLOCK; return 0; */
case XK_Num_Lock:/* S->buckystate ^= SCI_EVM_NUMLOCK; return 0; */
case XK_Shift_L:/* S->buckystate |= SCI_EVM_LSHIFT; return 0; */
case XK_Shift_R:/* S->buckystate |= SCI_EVM_RSHIFT; return 0; */
return 0;
default:
break;
}
if (flags & SCI_XLIB_NLS) {
/* Localised key lookup */
XLookupString(&(key_event->xkey), character, 1, &xkey, NULL);
}
if ((xkey >= ' ') && (xkey <= '~'))
return xkey; /* All printable ASCII characters */
switch (xkey) {
case XK_KP_Add: return '+';
case XK_KP_Divide: return '/';
case XK_KP_Subtract: return '-';
case XK_KP_Multiply: return '*';
}
if (*character)
return xkey; /* Should suffice for all practical purposes */
sciprintf("Unknown X keysym: %04x\n", xkey);
return 0;
}
void
x_get_event(gfx_driver_t *drv, int eventmask, long wait_usec, sci_event_t *sci_event)
{
int x_button_xlate[] = {0, 1, 3, 2, 4, 5};
XEvent event;
Window window = S->window;
Display *display = S->display;
struct timeval ctime, timeout_time, sleep_time;
long usecs_to_sleep;
eventmask |= ExposureMask; /* Always check for this */
gettimeofday(&timeout_time, NULL);
timeout_time.tv_usec += wait_usec;
/* Calculate wait time */
timeout_time.tv_sec += (timeout_time.tv_usec / 1000000);
timeout_time.tv_usec %= 1000000;
do {
int hasnext_event = 1;
while (hasnext_event) {
if (sci_event) { /* Capable of handling any event? */
hasnext_event = XPending(display);
if (hasnext_event)
XNextEvent(display, &event);
} else
hasnext_event = XCheckWindowEvent(display, window, eventmask, &event);
if (hasnext_event)
switch (event.type) {
case ReparentNotify:
case ConfigureNotify:
case MapNotify:
case UnmapNotify:
break;
case KeyPress: {
int modifiers = event.xkey.state;
char ch = 0;
sci_event->type = SCI_EVT_KEYBOARD;
S->buckystate = ((flags & SCI_XLIB_INSERT_MODE)? SCI_EVM_INSERT : 0)
| (((modifiers & LockMask)? SCI_EVM_LSHIFT | SCI_EVM_RSHIFT : 0)
| ((modifiers & ControlMask)? SCI_EVM_CTRL : 0)
| ((modifiers & (Mod1Mask | Mod4Mask))? SCI_EVM_ALT : 0)
| ((modifiers & Mod2Mask)? SCI_EVM_NUMLOCK : 0)
| ((modifiers & Mod5Mask)? SCI_EVM_SCRLOCK : 0))
^ ((modifiers & ShiftMask)? SCI_EVM_LSHIFT | SCI_EVM_RSHIFT : 0);
sci_event->buckybits = S->buckystate;
sci_event->data =
x_map_key(drv, &event, &ch);
if (ch)
sci_event->character = ch;
else
sci_event->character = sci_event->data;
if (sci_event->data == SCI_K_INSERT)
flags ^= SCI_XLIB_INSERT_MODE;
if (sci_event->data)
return;
break;
}
case KeyRelease:
/*x_unmap_key(drv, event.xkey.keycode);*/
break;
case ButtonPress: {
sci_event->type = SCI_EVT_MOUSE_PRESS;
sci_event->buckybits = S->buckystate;
sci_event->data = x_button_xlate[event.xbutton.button];
return;
}
case ButtonRelease: {
sci_event->type = SCI_EVT_MOUSE_RELEASE;
sci_event->buckybits = S->buckystate;
sci_event->data = x_button_xlate[event.xbutton.button];
return;
}
case MotionNotify: {
drv->pointer_x = event.xmotion.x;
drv->pointer_y = event.xmotion.y;
if (!sci_event)
/* Wake up from sleep */
return;
}
break;
case GraphicsExpose:
case Expose: {
XCopyArea(S->display, S->visual[0], S->window, S->gc,
event.xexpose.x, event.xexpose.y, event.xexpose.width, event.xexpose.height,
event.xexpose.x, event.xexpose.y);
}
break;
case NoExpose:
break;
default:
ERROR("Received unhandled X event %04x\n", event.type);
}
}
gettimeofday(&ctime, NULL);
usecs_to_sleep = (timeout_time.tv_sec > ctime.tv_sec)? 1000000 : 0;
usecs_to_sleep += timeout_time.tv_usec - ctime.tv_usec;
if (ctime.tv_sec > timeout_time.tv_sec) usecs_to_sleep = -1;
if (usecs_to_sleep > 0) {
if (usecs_to_sleep > 10000)
usecs_to_sleep = 10000; /* Sleep for a maximum of 10 ms */
sleep_time.tv_usec = usecs_to_sleep;
sleep_time.tv_sec = 0;
select(0, NULL, NULL, NULL, &sleep_time); /* Sleep. */
}
} while (usecs_to_sleep >= 0);
if (sci_event)
sci_event->type = SCI_EVT_NONE; /* No event. */
}
static sci_event_t
xlib_get_event(struct _gfx_driver *drv)
{
sci_event_t input;
x_get_event(drv, PointerMotionMask | StructureNotifyMask | ButtonPressMask
| ButtonReleaseMask | KeyPressMask | KeyReleaseMask,
0, &input);
return input;
}
static int
xlib_usec_sleep(struct _gfx_driver *drv, long usecs)
{
x_get_event(drv, PointerMotionMask | StructureNotifyMask, usecs, NULL);
return GFX_OK;
}
gfx_driver_t
gfx_driver_xlib = {
"xlib",
"0.6a",
SCI_GFX_DRIVER_MAGIC,
SCI_GFX_DRIVER_VERSION,
NULL,
0, 0,
GFX_CAPABILITY_STIPPLED_LINES | GFX_CAPABILITY_MOUSE_SUPPORT
| GFX_CAPABILITY_MOUSE_POINTER | GFX_CAPABILITY_PIXMAP_REGISTRY
| GFX_CAPABILITY_FINE_LINES | GFX_CAPABILITY_WINDOWED
| GFX_CAPABILITY_KEYTRANSLATE,
0/*GFX_DEBUG_POINTER | GFX_DEBUG_UPDATES | GFX_DEBUG_PIXMAPS | GFX_DEBUG_BASIC*/,
xlib_set_parameter,
xlib_init_specific,
xlib_init,
xlib_exit,
xlib_draw_line,
#ifdef HAVE_RENDER
xlib_draw_filled_rect_RENDER,
#else
xlib_draw_filled_rect,
#endif
xlib_register_pixmap,
xlib_unregister_pixmap,
xlib_draw_pixmap,
xlib_grab_pixmap,
xlib_update,
xlib_set_static_buffer,
xlib_set_pointer,
xlib_set_palette,
xlib_get_event,
xlib_usec_sleep,
NULL
};
#endif /* X_DISPLAY_MISSING */
|