-
Notifications
You must be signed in to change notification settings - Fork 26
/
x11.cpp
2145 lines (1835 loc) · 42.1 KB
/
x11.cpp
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
#ifdef __linux__
// PLATFORM: LINUX
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h> // compile with -pthread
#include <pty.h> // link with -lutil
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/extensions/Xinerama.h>
// better than Xinerama but incompatible
// with _NET_WM_FULLSCREEN_MONITORS
//#include <X11/extensions/Xrandr.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h> // usleep
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include "rgba8.h"
#include "gl.h"
#include <GL/glx.h>
#include <GL/glxext.h>
#include "platform.h"
/*
Bool __glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx, int line)
{
printf("%d\n",line);
return glXMakeCurrent(dpy, drawable, ctx);
}
#define glXMakeCurrent(_dpy, _dr, _rc) __glXMakeCurrent(_dpy, _dr, _rc, __LINE__)
*/
Display* dpy=0;
A3D_WND* wnd_head=0;
A3D_WND* wnd_tail=0;
struct A3D_WND
{
A3D_WND* prev;
A3D_WND* next;
XIM im;
XIC ic;
Window win;
GLXContext rc;
void* cookie;
bool mapped;
WndMode wndmode;
int wndrect[4];
bool wnddirty;
PlatformInterface platform_api;
int mouse_b;
int mouse_x;
int mouse_y;
bool track;
bool closing;
int force_key;
int gwa_width;
int gwa_height;
};
A3D_PTY* head_pty = 0;
A3D_PTY* tail_pty = 0;
struct A3D_PTY
{
int fd;
int pd[2];
pid_t pid;
A3D_PTY* next;
A3D_PTY* prev;
A3D_VT* vt;
};
const char* caps[]=
{
"A3D_NONE",
"A3D_BACKSPACE",
"A3D_TAB",
"A3D_ENTER",
"A3D_PAUSE",
"A3D_ESCAPE",
"A3D_SPACE",
"A3D_PAGEUP",
"A3D_PAGEDOWN",
"A3D_END",
"A3D_HOME",
"A3D_LEFT",
"A3D_UP",
"A3D_RIGHT",
"A3D_DOWN",
"A3D_PRINT",
"A3D_INSERT",
"A3D_DELETE",
"A3D_0",
"A3D_1",
"A3D_2",
"A3D_3",
"A3D_4",
"A3D_5",
"A3D_6",
"A3D_7",
"A3D_8",
"A3D_9",
"A3D_A",
"A3D_B",
"A3D_C",
"A3D_D",
"A3D_E",
"A3D_F",
"A3D_G",
"A3D_H",
"A3D_I",
"A3D_J",
"A3D_K",
"A3D_L",
"A3D_M",
"A3D_N",
"A3D_O",
"A3D_P",
"A3D_Q",
"A3D_R",
"A3D_S",
"A3D_T",
"A3D_U",
"A3D_V",
"A3D_W",
"A3D_X",
"A3D_Y",
"A3D_Z",
"A3D_LWIN",
"A3D_RWIN",
"A3D_APPS",
"A3D_NUMPAD_0",
"A3D_NUMPAD_1",
"A3D_NUMPAD_2",
"A3D_NUMPAD_3",
"A3D_NUMPAD_4",
"A3D_NUMPAD_5",
"A3D_NUMPAD_6",
"A3D_NUMPAD_7",
"A3D_NUMPAD_8",
"A3D_NUMPAD_9",
"A3D_NUMPAD_MULTIPLY",
"A3D_NUMPAD_DIVIDE",
"A3D_NUMPAD_ADD",
"A3D_NUMPAD_SUBTRACT",
"A3D_NUMPAD_DECIMAL",
"A3D_NUMPAD_ENTER",
"A3D_F1",
"A3D_F2",
"A3D_F3",
"A3D_F4",
"A3D_F5",
"A3D_F6",
"A3D_F7",
"A3D_F8",
"A3D_F9",
"A3D_F10",
"A3D_F11",
"A3D_F12",
"A3D_F13",
"A3D_F14",
"A3D_F15",
"A3D_F16",
"A3D_F17",
"A3D_F18",
"A3D_F19",
"A3D_F20",
"A3D_F21",
"A3D_F22",
"A3D_F23",
"A3D_F24",
"A3D_CAPSLOCK",
"A3D_NUMLOCK",
"A3D_SCROLLLOCK",
"A3D_LSHIFT",
"A3D_RSHIFT",
"A3D_LCTRL",
"A3D_RCTRL",
"A3D_LALT",
"A3D_RALT",
"A3D_OEM_COLON",
"A3D_OEM_PLUS",
"A3D_OEM_COMMA",
"A3D_OEM_MINUS",
"A3D_OEM_PERIOD",
"A3D_OEM_SLASH",
"A3D_OEM_TILDE",
"A3D_OEM_OPEN",
"A3D_OEM_CLOSE",
"A3D_OEM_BACKSLASH",
"A3D_OEM_QUOTATION",
};
static const unsigned char ki_to_kc[] =
{
0, // A3D_NONE
22, // A3D_BACKSPACE
23, // A3D_TAB
36, // A3D_ENTER
127, // A3D_PAUSE
9, // A3D_ESCAPE
65, // A3D_SPACE
112, // A3D_PAGEUP
117, // A3D_PAGEDOWN
115, // A3D_END
110, // A3D_HOME
113, // A3D_LEFT
111, // A3D_UP
114, // A3D_RIGHT
116, // A3D_DOWN
0, // A3D_PRINT
118, // A3D_INSERT
119, // A3D_DELETE
19, // A3D_0
10, // A3D_1
11, // A3D_2
12, // A3D_3
13, // A3D_4
14, // A3D_5
15, // A3D_6
16, // A3D_7
17, // A3D_8
18, // A3D_9
38, // A3D_A
56, // A3D_B
54, // A3D_C
40, // A3D_D
26, // A3D_E
41, // A3D_F
42, // A3D_G
43, // A3D_H
31, // A3D_I
44, // A3D_J
45, // A3D_K
46, // A3D_L
58, // A3D_M
57, // A3D_N
32, // A3D_O
33, // A3D_P
24, // A3D_Q
27, // A3D_R
39, // A3D_S
28, // A3D_T
30, // A3D_U
55, // A3D_V
25, // A3D_W
53, // A3D_X
29, // A3D_Y
52, // A3D_Z
0, // A3D_LWIN
0, // A3D_RWIN
0, // A3D_APPS
90, // A3D_NUMPAD_0
87, // A3D_NUMPAD_1
88, // A3D_NUMPAD_2
89, // A3D_NUMPAD_3
83, // A3D_NUMPAD_4
84, // A3D_NUMPAD_5
85, // A3D_NUMPAD_6
79, // A3D_NUMPAD_7
80, // A3D_NUMPAD_8
81, // A3D_NUMPAD_9
63, // A3D_NUMPAD_MULTIPLY
106, // A3D_NUMPAD_DIVIDE
86, // A3D_NUMPAD_ADD
82, // A3D_NUMPAD_SUBTRACT
91, // A3D_NUMPAD_DECIMAL
104, // A3D_NUMPAD_ENTER
67, // A3D_F1
68, // A3D_F2
69, // A3D_F3
70, // A3D_F4
71, // A3D_F5
72, // A3D_F6
73, // A3D_F7
74, // A3D_F8
75, // A3D_F9
76, // A3D_F10
95, // A3D_F11
96, // A3D_F12
0, // A3D_F13
0, // A3D_F14
0, // A3D_F15
0, // A3D_F16
0, // A3D_F17
0, // A3D_F18
0, // A3D_F19
0, // A3D_F20
0, // A3D_F21
0, // A3D_F22
0, // A3D_F23
0, // A3D_F24
66, // A3D_CAPSLOCK
77, // A3D_NUMLOCK
78, // A3D_SCROLLLOCK
50, // A3D_LSHIFT
62, // A3D_RSHIFT
37, // A3D_LCTRL
105, // A3D_RCTRL
64, // A3D_LALT
108, // A3D_RALT
47, // A3D_OEM_COLON
21, // A3D_OEM_PLUS
59, // A3D_OEM_COMMA
20, // A3D_OEM_MINUS
60, // A3D_OEM_PERIOD
61, // A3D_OEM_SLASH
49, // A3D_OEM_TILDE
34, // A3D_OEM_OPEN
35, // A3D_OEM_CLOSE
51, // A3D_OEM_BACKSLASH
48, // A3D_OEM_QUOTATION
};
static const unsigned char kc_to_ki[128]=
{
// ok
0,0,0,0,0,0,0,0,
0, // 8
A3D_ESCAPE, // 9
A3D_1, // 10
A3D_2, // 11
A3D_3, // 12
A3D_4, // 13
A3D_5, // 14
A3D_6, // 15
A3D_7, // 16
A3D_8, // 17
A3D_9, // 18
A3D_0, // 19
A3D_OEM_MINUS, // 20
A3D_OEM_PLUS, // 21
A3D_BACKSPACE, // 22
A3D_TAB, // 23
A3D_Q, // 24
A3D_W, // 25
A3D_E, // 26
A3D_R, // 27
A3D_T, // 28
A3D_Y, // 29
A3D_U, // 30
A3D_I, // 31
A3D_O, // 32
A3D_P, // 33
A3D_OEM_OPEN, // 34
A3D_OEM_CLOSE, // 35
A3D_ENTER, // 36
A3D_LCTRL, // 37
A3D_A, // 38
A3D_S, // 39
A3D_D, // 40
A3D_F, // 41
A3D_G, // 42
A3D_H, // 43
A3D_J, // 44
A3D_K, // 45
A3D_L, // 46
A3D_OEM_COLON, // 47
A3D_OEM_QUOTATION, // 48
A3D_OEM_TILDE, // 49
A3D_LSHIFT, // 50
A3D_OEM_BACKSLASH, // 51
A3D_Z, // 52
A3D_X, // 53
A3D_C, // 54
A3D_V, // 55
A3D_B, // 56
A3D_N, // 57
A3D_M,//58 // 58
A3D_OEM_COMMA, // 59
A3D_OEM_PERIOD, // 60
A3D_OEM_SLASH, // 61
A3D_RSHIFT, // 62
A3D_NUMPAD_MULTIPLY, // 63
A3D_LALT, // 64
A3D_SPACE, // 65
A3D_CAPSLOCK, // 66
A3D_F1, // 67
A3D_F2, // 68
A3D_F3, // 69
A3D_F4, // 70
A3D_F5, // 71
A3D_F6, // 72
A3D_F7, // 73
A3D_F8, // 74
A3D_F9, // 75
A3D_F10, // 76
A3D_NUMLOCK, // 77
A3D_SCROLLLOCK, // 78
A3D_NUMPAD_7, // 79
A3D_NUMPAD_8, // 80
A3D_NUMPAD_9, // 81
A3D_NUMPAD_SUBTRACT, // 82
A3D_NUMPAD_4, // 83
A3D_NUMPAD_5, // 84
A3D_NUMPAD_6, // 85
A3D_NUMPAD_ADD, // 86
A3D_NUMPAD_1, // 87
A3D_NUMPAD_2, // 88
A3D_NUMPAD_3, // 89
A3D_NUMPAD_0, // 90
A3D_NUMPAD_DECIMAL, // 91
0, // 92
0, // 93
0, // 94
A3D_F11, // 95
A3D_F12, // 96
// bad
0, // 97
0, // 98
0, // 99
0, // 100
0, // 101
0, // 102
0, // 103
A3D_NUMPAD_ENTER, // 104
A3D_RCTRL, // 105
A3D_NUMPAD_DIVIDE, // 106
0, // 107
A3D_RALT, // 108
0, // 109
A3D_HOME, // 110
A3D_UP, // 111
A3D_PAGEUP, // 112
A3D_LEFT, // 113
A3D_RIGHT, // 114
A3D_END, // 115
A3D_DOWN, // 116
A3D_PAGEDOWN, // 117
A3D_INSERT, // 118
A3D_DELETE, // 119
0, // 120
0, // 121
0, // 122
0, // 123
0, // 124
0, // 125
0, // 126
// ok
A3D_PAUSE, // 127
};
void a3dPushContext(A3D_PUSH_CONTEXT* ctx)
{
ctx->data[0] = dpy;
ctx->data[1] = (void*)glXGetCurrentDrawable();
ctx->data[2] = glXGetCurrentContext();
}
void a3dPopContext(const A3D_PUSH_CONTEXT* ctx)
{
if (dpy)
glXMakeCurrent((Display*)ctx->data[0], (GLXDrawable)ctx->data[1], (GLXContext)ctx->data[2]);
}
void a3dSwitchContext(const A3D_WND* wnd)
{
if (dpy)
glXMakeCurrent(dpy, wnd->win, wnd->rc);
}
// creates window & initialized GL
A3D_WND* a3dOpen(const PlatformInterface* pi, const GraphicsDesc* gd, A3D_WND* share)
{
struct PUSH
{
PUSH()
{
dr = 0;
rc = 0;
if (dpy)
{
pop=true;
rc = glXGetCurrentContext();
dr = glXGetCurrentDrawable();
}
else
pop=false;
}
~PUSH()
{
if (dpy && pop)
glXMakeCurrent(dpy, dr, rc);
}
bool pop;
GLXDrawable dr;
GLXContext rc;
} push;
if (!pi || !gd)
return 0;
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)
glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
if (!glXCreateContextAttribsARB)
return 0;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;
int wndrect[4];
XIM im = 0;
bool im_ok = false;
if (XSupportsLocale())
{
if (XSetLocaleModifiers("@im=none"))
{
im_ok = true;
}
}
// dpy is global
if (!dpy)
{
dpy = XOpenDisplay(NULL);
if (!dpy)
return 0;
}
Bool DetectableAutoRepeat = false;
XkbSetDetectableAutoRepeat(dpy, True, &DetectableAutoRepeat);
Atom wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
Window root = DefaultRootWindow(dpy);
if (!root)
{
XCloseDisplay(dpy);
return 0;
}
XVisualInfo* vi = glXChooseVisual(dpy, 0, att);
if (!vi)
{
XCloseDisplay(dpy);
return 0;
}
Colormap cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
XSetWindowAttributes swa;
swa.colormap = cmap;
swa.event_mask =
StructureNotifyMask |
PropertyChangeMask |
VisibilityChangeMask |
FocusChangeMask |
ExposureMask |
PointerMotionMask |
KeyPressMask |
KeyReleaseMask |
ButtonPressMask |
ButtonReleaseMask |
EnterWindowMask |
LeaveWindowMask;
if (gd->wnd_xywh)
{
wndrect[0] = gd->wnd_xywh[0];
wndrect[1] = gd->wnd_xywh[1];
wndrect[2] = gd->wnd_xywh[2];
wndrect[3] = gd->wnd_xywh[3];
}
else
{
wndrect[0] = 0;
wndrect[1] = 0;
wndrect[2] = 800;
wndrect[3] = 600;
}
Window win = XCreateWindow(dpy, /*share ? share->win :*/ root, wndrect[0]+wndrect[2]/2 - 400, wndrect[1]+wndrect[3]/2 - 300, 800, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XFreeColormap(dpy, cmap); // swa (including cmap) is no longer used
if (!win)
{
XFree(vi);
XCloseDisplay(dpy);
return 0;
}
XSetWMProtocols(dpy, win, &wm_delete_window, 1);
char app_name[]="asciiid";
XStoreName(dpy, win, app_name);
// surpress 'UNKNOWN'
// name will be taken from 'Name' property set in ~/.local/share/application/*.desktop file
// if there is no such file it will use class name 'A3D'
/*
[Desktop Entry]
Name=ASCIIID <--- this will be used
Exec=/home/user/asciiid/.run/asciiid
Path=/home/user/asciiid
Icon=/home/user/asciiid/icons/app.png
Type=Application
Categories=Games;Utilities
*/
XClassHint* ch = XAllocClassHint();
char cls_name[] = "A3D";
ch->res_class=cls_name;
ch->res_name=app_name;
XSetClassHint(dpy, win, ch);
XFree(ch);
int nelements;
GLXFBConfig *fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy), 0, &nelements);
int attribs[] = {
GLX_CONTEXT_FLAGS_ARB, gd->flags & GraphicsDesc::DEBUG_CONTEXT ? GLX_CONTEXT_DEBUG_BIT_ARB : 0,
GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
GLX_CONTEXT_MINOR_VERSION_ARB, 5,
0};
if (!fbc)
{
XFree(vi);
printf("CANNOT CHOOSE FBCONFIG\n");
return 0;
}
glc = glXCreateContextAttribsARB(dpy, *fbc, share ? share->rc : 0, true, attribs);
//glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
XFree(fbc);
XFree(vi);
if (!glc)
{
printf("CANNOT CREATE GL CONTEXT\n");
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}
if (!glXMakeCurrent(dpy, win, glc))
{
printf("CANNOT MAKE GL CONTEXT CURRENT\n");
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}
char* ver = (char*)glGetString(GL_VERSION);
if (ver[0] < '4' || ver[0] > '9' || ver[1] != '.' || ver[0] == '4' && (ver[2] < '5' || ver[2] > '9'))
{
printf("GL_VERSION (4.5.x) requirement is not met by %s\n", ver);
glXMakeCurrent(dpy, 0, 0);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}
// try to connect to IM, if anything fails here
// we'd simply stick ascii codes
XIC ic = 0;
if (im_ok)
{
im = XOpenIM(dpy, NULL, NULL, NULL);
if (im)
{
char *failed_arg = 0;
XIMStyles *styles = 0;
failed_arg = XGetIMValues(im, XNQueryInputStyle, &styles, NULL);
if (styles)
XFree(styles);
if (!failed_arg)
{
ic = XCreateIC(im, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, win, NULL);
if (!ic)
{
XCloseIM(im);
im = 0;
im_ok = false;
}
else
{
XSetICFocus(ic);
}
}
else
{
XFree(failed_arg);
XCloseIM(im);
im = 0;
im_ok = false;
}
}
}
/*
// HAS NO EFFECT, only going fullscreen on all monitors at once results in FLIP mode
// anything else uses BLIT mode even with this hint enabled :(
Atom bypass_wm_atom = XInternAtom(dpy,"_NET_WM_BYPASS_COMPOSITOR",False);
if (bypass_wm_atom)
{
static const unsigned long bypassCompositor = 1;
XChangeProperty(dpy,
win,
bypass_wm_atom,
XA_CARDINAL,
32,
PropModeReplace,
(const unsigned char*)&bypassCompositor,
1);
}
*/
A3D_WND* wnd = (A3D_WND*)malloc(sizeof(A3D_WND));
// init wnd fields!
wnd->platform_api = *pi;
wnd->cookie = 0;
wnd->win = win;
wnd->rc = glc;
wnd->ic = ic;
wnd->im = im;
wnd->mapped = false;
wnd->wndmode = gd->wnd_mode == A3D_WND_CURRENT ? A3D_WND_NORMAL : gd->wnd_mode;
wnd->wndrect[0] = wndrect[0];
wnd->wndrect[1] = wndrect[1];
wnd->wndrect[2] = wndrect[2];
wnd->wndrect[3] = wndrect[3];
wnd->wnddirty = true;
wnd->mouse_b = 0;
wnd->mouse_x = 0;
wnd->mouse_y = 0;
wnd->track = false;
wnd->force_key = A3D_NONE;
wnd->prev = wnd_tail;
wnd->next = 0;
if (wnd_tail)
wnd_tail->next = wnd;
else
wnd_head = wnd;
wnd_tail = wnd;
if (wnd->platform_api.init)
wnd->platform_api.init(wnd);
XSync(dpy,False);
// send initial notifications:
XGetWindowAttributes(dpy, win, &gwa);
wnd->gwa_width = gwa.width;
wnd->gwa_height = gwa.height;
if (wnd->platform_api.resize)
wnd->platform_api.resize(wnd,wnd->gwa_width,wnd->gwa_height);
return wnd;
}
void a3dLoop()
{
XSync(dpy,False);
// for all created wnds, force size notifications
A3D_WND* wnd = wnd_head;
while (wnd)
{
wnd->platform_api.resize(wnd, wnd->gwa_width, wnd->gwa_height);
wnd = wnd->next;
}
Window win;
XEvent xev;
Atom wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
Bool DetectableAutoRepeat = false;
XkbSetDetectableAutoRepeat(dpy, True, &DetectableAutoRepeat);
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)
glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
while (wnd_head)
{
while (XPending(dpy))
{
XNextEvent(dpy, &xev);
win = xev.xany.window;
if (XFilterEvent(&xev, win))
printf("%s","XFilter CONSUMED!\n");
// find wnd
A3D_WND* wnd = wnd_head;
while (wnd)
{
if (wnd->win == win)
break;
wnd=wnd->next;
}
if (!wnd)
continue;
glXMakeCurrent(dpy, win, wnd->rc);
if (xev.type == ClientMessage)
{
if ((Atom)xev.xclient.data.l[0] == wm_delete_window)
{
if (wnd->platform_api.close)
wnd->platform_api.close(wnd);
else
{
if (wnd->ic)
XDestroyIC(wnd->ic);
if (wnd->im)
XCloseIM(wnd->im);
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, wnd->rc);
XDestroyWindow(dpy,win);
if (wnd->prev)
wnd->prev->next = wnd->next;
else
wnd_head = wnd->next;
if (wnd->next)
wnd->next->prev = wnd->prev;
else
wnd_tail = wnd->prev;
free(wnd);
break;
}
}
}
else
if (xev.type == MappingNotify)
{
XRefreshKeyboardMapping(&xev.xmapping);
}
else
if (xev.type == ConfigureNotify)
{
if (xev.xconfigure.width != wnd->gwa_width || xev.xconfigure.height != wnd->gwa_height)
{
wnd->gwa_width = xev.xconfigure.width;
wnd->gwa_height = xev.xconfigure.height;
if (wnd->platform_api.resize)
wnd->platform_api.resize(wnd, wnd->gwa_width, wnd->gwa_height);
}
}
else
if (xev.type == FocusIn)
{
if (wnd->platform_api.keyb_focus)
wnd->platform_api.keyb_focus(wnd,true);
if (wnd->ic)
XSetICFocus(wnd->ic);
}
else
if (xev.type == FocusOut)
{
if (wnd->platform_api.keyb_focus)
wnd->platform_api.keyb_focus(wnd,false);
if (wnd->ic)
XUnsetICFocus(wnd->ic);
}
else
if (xev.type == Expose)
{
/*
if (wnd->platform_api.render && mapped)
wnd->platform_api.render(wnd);
*/
}
else
if(xev.type == EnterNotify)
{
int state = 0;
state |= xev.xcrossing.state & Button1Mask ? MouseInfo::LEFT : 0;
state |= xev.xcrossing.state & Button3Mask ? MouseInfo::RIGHT : 0;
state |= xev.xcrossing.state & Button2Mask ? MouseInfo::MIDDLE : 0;
if (wnd->platform_api.mouse)
wnd->platform_api.mouse(wnd,xev.xcrossing.x,xev.xcrossing.y,(MouseInfo)(MouseInfo::ENTER | state));
}
else
if(xev.type == LeaveNotify)
{
int state = 0;
state |= xev.xcrossing.state & Button1Mask ? MouseInfo::LEFT : 0;
state |= xev.xcrossing.state & Button3Mask ? MouseInfo::RIGHT : 0;
state |= xev.xcrossing.state & Button2Mask ? MouseInfo::MIDDLE : 0;
if (wnd->platform_api.mouse)
wnd->platform_api.mouse(wnd,xev.xcrossing.x,xev.xcrossing.y, (MouseInfo)(MouseInfo::LEAVE | state));
}
else
if(xev.type == KeyPress)
{
if (wnd->platform_api.keyb_key)
{
int kc = xev.xkey.keycode;
if (kc>=0 && kc<128)
{
wnd->force_key = kc_to_ki[kc];
wnd->platform_api.keyb_key(wnd,(KeyInfo)kc_to_ki[kc],true);
wnd->force_key = A3D_NONE;
}
}
if (wnd->ic)
{
int count = 0;
KeySym keysym = 0;
char buf[20];
Status status = 0;
count = Xutf8LookupString(wnd->ic, (XKeyPressedEvent*)&xev, buf, 20, &keysym, &status);
if (count)
{
if (wnd->platform_api.keyb_char)
{
uint8_t* c = (uint8_t*)buf;
for (int i=0; i<count;)
{
uint8_t* c = (uint8_t*)buf + i;
wchar_t w = 0;
if (*c==0)
break;
else
if (*c<0x80)
{
// 7bits
w = c[0]&0x7F;
i+=1;