-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathos_linux.odin
2551 lines (2274 loc) · 96 KB
/
os_linux.odin
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
#+build linux
#+private
package zephr
import "base:runtime"
import "core:container/bit_array"
import "core:container/queue"
import "core:fmt"
import "core:log"
import m "core:math/linalg/glsl"
import "core:mem"
import "core:mem/virtual"
import "core:net"
import "core:os"
import "core:hash"
import "core:strings"
import "core:sys/linux"
import "core:sys/posix"
import "core:time"
import gl "vendor:OpenGL"
import "vendor:stb/image"
import x11 "vendor:x11/xlib"
import "3rdparty/evdev"
import "3rdparty/glx"
import "3rdparty/inotify"
import "3rdparty/udev"
// TODO: support controller-specific stuff like haptics and adaptive triggers on DualSense
// TODO: support controller audio devices (DS4, DualSense, Xbox Series)
// TODO: support changing controller LEDs through evdev (maybe not lol cuz idk how this would work in Windows, I think
// we would have to have controller specific stuff which would be a lot)
@(private = "file")
LinuxEvdevBinding :: struct {
// this maps to struct input_event in <linux/input.h>
type: u16,
code: u16,
is_positive: bool,
}
#assert(size_of(LinuxEvdevRange) == 8)
@(private = "file")
LinuxEvdevRange :: struct {
// this maps to struct input_event in <linux/input.h>
min: i32,
max: i32,
}
#assert(size_of(LinuxInputDevice) == OS_INPUT_DEVICE_BACKEND_SIZE)
@(private = "file")
LinuxInputDevice :: struct {
mouse_devnode: string,
touchpad_devnode: string,
keyboard_devnode: string,
gamepad_devnode: string,
motion_sensor_devnode: string,
mouse_evdev: ^evdev.libevdev,
touchpad_evdev: ^evdev.libevdev,
keyboard_evdev: ^evdev.libevdev,
gamepad_evdev: ^evdev.libevdev,
motion_sensor_evdev: ^evdev.libevdev,
gamepad_bindings: Bindings,
gamepad_action_ranges: [cast(int)GamepadAction.COUNT + 1]LinuxEvdevRange,
gamepad_rumble_id: i16,
}
@(private = "file")
XdndState :: struct {
transient: struct {
exchange_started: bool,
source_window: x11.Window,
proposed_type: x11.Atom,
},
xdnd_version: u32,
xdnd_aware: x11.Atom,
xdnd_enter: x11.Atom,
xdnd_leave: x11.Atom,
xdnd_drop: x11.Atom,
xdnd_status: x11.Atom,
xdnd_finished: x11.Atom,
xdnd_position: x11.Atom,
xdnd_selection: x11.Atom,
XDND_DATA: x11.Atom,
xdnd_type_list: x11.Atom,
supported_types: [6]x11.Atom,
}
@(private = "file")
Os :: struct {
x11_display: ^x11.Display,
x11_window: x11.Window,
x11_colormap: x11.Colormap,
xkb: x11.XkbDescPtr,
xim: x11.XIM,
glx_context: glx.Context,
window_delete_atom: x11.Atom,
xdnd: XdndState,
xinput_opcode: i32,
udevice: ^udev.udev,
udev_monitor: ^udev.udev_monitor,
inotify_fd: os.Handle,
}
Bindings :: [GamepadAction]LinuxEvdevBinding
OsCursor :: x11.Cursor
@(private = "file")
XA_STRING :: x11.Atom(31)
@(private = "file")
XA_CARDINAL :: x11.Atom(6)
@(private = "file")
XDND_PROTOCOL_VERSION :: 5
@(private = "file")
l_os: Os
// TODO: No rumble support over wireless. We get a ENOSYS
// Seems to be a driver issue that we can't do anything about.
@(private = "file")
Xbox_Series_S_Bindings_Wireless :: #partial Bindings {
.DPAD_LEFT = {type = EV_ABS, code = ABS_HAT0X, is_positive = false},
.DPAD_DOWN = {type = EV_ABS, code = ABS_HAT0Y, is_positive = true},
.DPAD_RIGHT = {type = EV_ABS, code = ABS_HAT0X, is_positive = true},
.DPAD_UP = {type = EV_ABS, code = ABS_HAT0Y, is_positive = false},
.FACE_LEFT = {type = EV_KEY, code = BTN_NORTH, is_positive = true},
.FACE_DOWN = {type = EV_KEY, code = BTN_SOUTH, is_positive = true},
.FACE_RIGHT = {type = EV_KEY, code = BTN_EAST, is_positive = true},
.FACE_UP = {type = EV_KEY, code = BTN_WEST, is_positive = true},
.START = {type = EV_KEY, code = BTN_START, is_positive = true},
.SELECT = {type = EV_KEY, code = BTN_SELECT, is_positive = true},
.STICK_LEFT = {type = EV_KEY, code = BTN_THUMBL, is_positive = true},
.STICK_RIGHT = {type = EV_KEY, code = BTN_THUMBR, is_positive = true},
.SHOULDER_LEFT = {type = EV_KEY, code = BTN_TL, is_positive = true},
.SHOULDER_RIGHT = {type = EV_KEY, code = BTN_TR, is_positive = true},
.STICK_LEFT_X_WEST = {type = EV_ABS, code = ABS_X, is_positive = false},
.STICK_LEFT_X_EAST = {type = EV_ABS, code = ABS_X, is_positive = true},
.STICK_LEFT_Y_NORTH = {type = EV_ABS, code = ABS_Y, is_positive = false},
.STICK_LEFT_Y_SOUTH = {type = EV_ABS, code = ABS_Y, is_positive = true},
.STICK_RIGHT_X_WEST = {type = EV_ABS, code = ABS_Z, is_positive = false},
.STICK_RIGHT_X_EAST = {type = EV_ABS, code = ABS_Z, is_positive = true},
.STICK_RIGHT_Y_NORTH = {type = EV_ABS, code = ABS_RZ, is_positive = false},
.STICK_RIGHT_Y_SOUTH = {type = EV_ABS, code = ABS_RZ, is_positive = true},
.TRIGGER_LEFT = {type = EV_ABS, code = ABS_BRAKE, is_positive = true},
.TRIGGER_RIGHT = {type = EV_ABS, code = ABS_GAS, is_positive = true},
.SYSTEM = {type = EV_KEY, code = BTN_MODE, is_positive = true},
}
@(private = "file")
Xbox_Series_S_Bindings_Wired :: #partial Bindings {
.DPAD_LEFT = {type = EV_ABS, code = ABS_HAT0X, is_positive = false},
.DPAD_DOWN = {type = EV_ABS, code = ABS_HAT0Y, is_positive = true},
.DPAD_RIGHT = {type = EV_ABS, code = ABS_HAT0X, is_positive = true},
.DPAD_UP = {type = EV_ABS, code = ABS_HAT0Y, is_positive = false},
.FACE_LEFT = {type = EV_KEY, code = BTN_NORTH, is_positive = true},
.FACE_DOWN = {type = EV_KEY, code = BTN_SOUTH, is_positive = true},
.FACE_RIGHT = {type = EV_KEY, code = BTN_EAST, is_positive = true},
.FACE_UP = {type = EV_KEY, code = BTN_WEST, is_positive = true},
.START = {type = EV_KEY, code = BTN_START, is_positive = true},
.SELECT = {type = EV_KEY, code = BTN_SELECT, is_positive = true},
.STICK_LEFT = {type = EV_KEY, code = BTN_THUMBL, is_positive = true},
.STICK_RIGHT = {type = EV_KEY, code = BTN_THUMBR, is_positive = true},
.SHOULDER_LEFT = {type = EV_KEY, code = BTN_TL, is_positive = true},
.SHOULDER_RIGHT = {type = EV_KEY, code = BTN_TR, is_positive = true},
.STICK_LEFT_X_WEST = {type = EV_ABS, code = ABS_X, is_positive = false},
.STICK_LEFT_X_EAST = {type = EV_ABS, code = ABS_X, is_positive = true},
.STICK_LEFT_Y_NORTH = {type = EV_ABS, code = ABS_Y, is_positive = false},
.STICK_LEFT_Y_SOUTH = {type = EV_ABS, code = ABS_Y, is_positive = true},
.STICK_RIGHT_X_WEST = {type = EV_ABS, code = ABS_RX, is_positive = false},
.STICK_RIGHT_X_EAST = {type = EV_ABS, code = ABS_RX, is_positive = true},
.STICK_RIGHT_Y_NORTH = {type = EV_ABS, code = ABS_RY, is_positive = false},
.STICK_RIGHT_Y_SOUTH = {type = EV_ABS, code = ABS_RY, is_positive = true},
.TRIGGER_LEFT = {type = EV_ABS, code = ABS_Z, is_positive = true},
.TRIGGER_RIGHT = {type = EV_ABS, code = ABS_RZ, is_positive = true},
.SYSTEM = {type = EV_KEY, code = BTN_MODE, is_positive = true},
}
@(private = "file")
DualShock_3_Bindings :: #partial Bindings {
.DPAD_LEFT = {type = EV_KEY, code = BTN_DPAD_LEFT, is_positive = true},
.DPAD_DOWN = {type = EV_KEY, code = BTN_DPAD_DOWN, is_positive = true},
.DPAD_RIGHT = {type = EV_KEY, code = BTN_DPAD_RIGHT, is_positive = true},
.DPAD_UP = {type = EV_KEY, code = BTN_DPAD_UP, is_positive = true},
.FACE_LEFT = {type = EV_KEY, code = BTN_WEST, is_positive = true},
.FACE_DOWN = {type = EV_KEY, code = BTN_SOUTH, is_positive = true},
.FACE_RIGHT = {type = EV_KEY, code = BTN_EAST, is_positive = true},
.FACE_UP = {type = EV_KEY, code = BTN_NORTH, is_positive = true},
.START = {type = EV_KEY, code = BTN_START, is_positive = true},
.SELECT = {type = EV_KEY, code = BTN_SELECT, is_positive = true},
.STICK_LEFT = {type = EV_KEY, code = BTN_THUMBL, is_positive = true},
.STICK_RIGHT = {type = EV_KEY, code = BTN_THUMBR, is_positive = true},
.SHOULDER_LEFT = {type = EV_KEY, code = BTN_TL, is_positive = true},
.SHOULDER_RIGHT = {type = EV_KEY, code = BTN_TR, is_positive = true},
.STICK_LEFT_X_WEST = {type = EV_ABS, code = ABS_X, is_positive = false},
.STICK_LEFT_X_EAST = {type = EV_ABS, code = ABS_X, is_positive = true},
.STICK_LEFT_Y_NORTH = {type = EV_ABS, code = ABS_Y, is_positive = false},
.STICK_LEFT_Y_SOUTH = {type = EV_ABS, code = ABS_Y, is_positive = true},
.STICK_RIGHT_X_WEST = {type = EV_ABS, code = ABS_RX, is_positive = false},
.STICK_RIGHT_X_EAST = {type = EV_ABS, code = ABS_RX, is_positive = true},
.STICK_RIGHT_Y_NORTH = {type = EV_ABS, code = ABS_RY, is_positive = false},
.STICK_RIGHT_Y_SOUTH = {type = EV_ABS, code = ABS_RY, is_positive = true},
.TRIGGER_LEFT = {type = EV_ABS, code = ABS_Z, is_positive = true},
.TRIGGER_RIGHT = {type = EV_ABS, code = ABS_RZ, is_positive = true},
.SYSTEM = {type = EV_KEY, code = BTN_MODE, is_positive = true},
}
@(private = "file")
DualShock_4_Bindings :: #partial Bindings {
.DPAD_LEFT = {type = EV_ABS, code = ABS_HAT0X, is_positive = false},
.DPAD_DOWN = {type = EV_ABS, code = ABS_HAT0Y, is_positive = true},
.DPAD_RIGHT = {type = EV_ABS, code = ABS_HAT0X, is_positive = true},
.DPAD_UP = {type = EV_ABS, code = ABS_HAT0Y, is_positive = false},
.FACE_LEFT = {type = EV_KEY, code = BTN_WEST, is_positive = true},
.FACE_DOWN = {type = EV_KEY, code = BTN_SOUTH, is_positive = true},
.FACE_RIGHT = {type = EV_KEY, code = BTN_EAST, is_positive = true},
.FACE_UP = {type = EV_KEY, code = BTN_NORTH, is_positive = true},
.START = {type = EV_KEY, code = BTN_START, is_positive = true},
.SELECT = {type = EV_KEY, code = BTN_SELECT, is_positive = true},
.STICK_LEFT = {type = EV_KEY, code = BTN_THUMBL, is_positive = true},
.STICK_RIGHT = {type = EV_KEY, code = BTN_THUMBR, is_positive = true},
.SHOULDER_LEFT = {type = EV_KEY, code = BTN_TL, is_positive = true},
.SHOULDER_RIGHT = {type = EV_KEY, code = BTN_TR, is_positive = true},
.STICK_LEFT_X_WEST = {type = EV_ABS, code = ABS_X, is_positive = false},
.STICK_LEFT_X_EAST = {type = EV_ABS, code = ABS_X, is_positive = true},
.STICK_LEFT_Y_NORTH = {type = EV_ABS, code = ABS_Y, is_positive = false},
.STICK_LEFT_Y_SOUTH = {type = EV_ABS, code = ABS_Y, is_positive = true},
.STICK_RIGHT_X_WEST = {type = EV_ABS, code = ABS_RX, is_positive = false},
.STICK_RIGHT_X_EAST = {type = EV_ABS, code = ABS_RX, is_positive = true},
.STICK_RIGHT_Y_NORTH = {type = EV_ABS, code = ABS_RY, is_positive = false},
.STICK_RIGHT_Y_SOUTH = {type = EV_ABS, code = ABS_RY, is_positive = true},
.TRIGGER_LEFT = {type = EV_ABS, code = ABS_Z, is_positive = true},
.TRIGGER_RIGHT = {type = EV_ABS, code = ABS_RZ, is_positive = true},
.SYSTEM = {type = EV_KEY, code = BTN_MODE, is_positive = true},
}
@(private = "file")
DualSense_Bindings :: DualShock_4_Bindings
@(private = "file")
Switch_Pro_Bindings :: #partial Bindings {
.DPAD_LEFT = {type = EV_ABS, code = ABS_HAT0X, is_positive = false},
.DPAD_DOWN = {type = EV_ABS, code = ABS_HAT0Y, is_positive = true},
.DPAD_RIGHT = {type = EV_ABS, code = ABS_HAT0X, is_positive = true},
.DPAD_UP = {type = EV_ABS, code = ABS_HAT0Y, is_positive = false},
.FACE_LEFT = {type = EV_KEY, code = BTN_WEST, is_positive = true},
.FACE_DOWN = {type = EV_KEY, code = BTN_SOUTH, is_positive = true},
.FACE_RIGHT = {type = EV_KEY, code = BTN_EAST, is_positive = true},
.FACE_UP = {type = EV_KEY, code = BTN_NORTH, is_positive = true},
.START = {type = EV_KEY, code = BTN_START, is_positive = true},
.SELECT = {type = EV_KEY, code = BTN_SELECT, is_positive = true},
.STICK_LEFT = {type = EV_KEY, code = BTN_THUMBL, is_positive = true},
.STICK_RIGHT = {type = EV_KEY, code = BTN_THUMBR, is_positive = true},
.SHOULDER_LEFT = {type = EV_KEY, code = BTN_TL, is_positive = true},
.SHOULDER_RIGHT = {type = EV_KEY, code = BTN_TR, is_positive = true},
.STICK_LEFT_X_WEST = {type = EV_ABS, code = ABS_X, is_positive = false},
.STICK_LEFT_X_EAST = {type = EV_ABS, code = ABS_X, is_positive = true},
.STICK_LEFT_Y_NORTH = {type = EV_ABS, code = ABS_Y, is_positive = false},
.STICK_LEFT_Y_SOUTH = {type = EV_ABS, code = ABS_Y, is_positive = true},
.STICK_RIGHT_X_WEST = {type = EV_ABS, code = ABS_RX, is_positive = false},
.STICK_RIGHT_X_EAST = {type = EV_ABS, code = ABS_RX, is_positive = true},
.STICK_RIGHT_Y_NORTH = {type = EV_ABS, code = ABS_RY, is_positive = false},
.STICK_RIGHT_Y_SOUTH = {type = EV_ABS, code = ABS_RY, is_positive = true},
.TRIGGER_LEFT = {type = EV_KEY, code = BTN_TL2, is_positive = true},
.TRIGGER_RIGHT = {type = EV_KEY, code = BTN_TR2, is_positive = true},
.SYSTEM = {type = EV_KEY, code = BTN_MODE, is_positive = true},
}
@(private = "file")
SupportedControllers := map[u32]Bindings {
0x045E_0B12 = Xbox_Series_S_Bindings_Wired, // XBox Series S|X Controller Wired
0x045E_0B13 = Xbox_Series_S_Bindings_Wireless, // XBox Series S|X Controller Wireless
//0x0E6F_0162 = PDP_Xbox_One_Wired, // Performance Designed Products' Xbox One Controller
0x054C_0268 = DualShock_3_Bindings, // Genuine Sony DualShock 3 (Wired & Wireless)
0x054C_05C4 = DualShock_4_Bindings, // Genuine Sony DualShock 4 Gen1 (Wired & Wireless)
0x054C_09CC = DualShock_4_Bindings, // Genuine Sony DualShock 4 Gen2 (Wired & Wireless)
0x054C_0CE6 = DualSense_Bindings, // Genuine Sony DualSense
0x057E_2009 = Switch_Pro_Bindings, // Switch Pro Controller
}
@(private = "file")
evdev_scancode_to_zephr_scancode_map := []Scancode {
0 = .NULL,
1 = .ESCAPE,
2 = .KEY_1,
3 = .KEY_2,
4 = .KEY_3,
5 = .KEY_4,
6 = .KEY_5,
7 = .KEY_6,
8 = .KEY_7,
9 = .KEY_8,
10 = .KEY_9,
11 = .KEY_0,
12 = .MINUS,
13 = .EQUALS,
14 = .BACKSPACE,
15 = .TAB,
16 = .Q,
17 = .W,
18 = .E,
19 = .R,
20 = .T,
21 = .Y,
22 = .U,
23 = .I,
24 = .O,
25 = .P,
26 = .LEFT_BRACKET,
27 = .RIGHT_BRACKET,
28 = .ENTER,
29 = .LEFT_CTRL,
30 = .A,
31 = .S,
32 = .D,
33 = .F,
34 = .G,
35 = .H,
36 = .J,
37 = .K,
38 = .L,
39 = .SEMICOLON,
40 = .APOSTROPHE,
41 = .GRAVE,
42 = .LEFT_SHIFT,
43 = .BACKSLASH,
44 = .Z,
45 = .X,
46 = .C,
47 = .V,
48 = .B,
49 = .N,
50 = .M,
51 = .COMMA,
52 = .PERIOD,
53 = .SLASH,
54 = .RIGHT_SHIFT,
55 = .KP_MULTIPLY,
56 = .LEFT_ALT,
57 = .SPACE,
58 = .CAPS_LOCK,
59 = .F1,
60 = .F2,
61 = .F3,
62 = .F4,
63 = .F5,
64 = .F6,
65 = .F7,
66 = .F8,
67 = .F9,
68 = .F10,
69 = .NUM_LOCK_OR_CLEAR,
70 = .SCROLL_LOCK,
71 = .KP_7,
72 = .KP_8,
73 = .KP_9,
74 = .KP_MINUS,
75 = .KP_4,
76 = .KP_5,
77 = .KP_6,
78 = .KP_PLUS,
79 = .KP_1,
80 = .KP_2,
81 = .KP_3,
82 = .KP_0,
83 = .KP_PERIOD,
// 84
85 = .LANG5, // KEY_ZENKAKUHANKAKU
86 = .NON_US_BACKSLASH, // KEY_102ND
87 = .F11,
88 = .F12,
89 = .INTERNATIONAL1, // KEY_RO,
90 = .LANG3, // KEY_KATAKANA
91 = .LANG4, // KEY_HIRAGANA
92 = .INTERNATIONAL4, // KEY_HENKAN
93 = .INTERNATIONAL2, // KEY_KATAKANAHIRAGANA
94 = .INTERNATIONAL5, // KEY_MUHENKAN
95 = .INTERNATIONAL5, // KEY_KPJOCOMMA
96 = .KP_ENTER,
97 = .RIGHT_CTRL,
98 = .KP_DIVIDE,
99 = .SYSREQ,
100 = .RIGHT_ALT,
101 = .NULL, // KEY_LINEFEED
102 = .HOME,
103 = .UP,
104 = .PAGE_UP,
105 = .LEFT,
106 = .RIGHT,
107 = .END,
108 = .DOWN,
109 = .PAGE_DOWN,
110 = .INSERT,
111 = .DELETE,
112 = .NULL, // KEY_MACRO
113 = .MUTE,
114 = .VOLUME_DOWN,
115 = .VOLUME_UP,
116 = .POWER,
117 = .KP_EQUALS,
118 = .KP_PLUS_MINUS,
119 = .PAUSE,
// 120
121 = .KP_COMMA,
122 = .LANG1, // KEY_HANGUEL
123 = .LANG2, // KEY_HANJA
124 = .INTERNATIONAL3, // KEY_YEN
125 = .LEFT_META,
126 = .RIGHT_META,
127 = .APPLICATION, // KEY_COMPOSE
128 = .STOP,
129 = .AGAIN,
130 = .NULL, // KEY_PROPS
131 = .UNDO,
132 = .NULL, // KEY_FRONT
133 = .COPY,
134 = .NULL, // KEY_OPEN
135 = .PASTE,
136 = .FIND,
137 = .CUT,
138 = .HELP,
139 = .MENU,
140 = .NULL, // CALCULATOR
141 = .NULL, // KEY_SETUP
142 = .NULL, // SLEEP
143 = .NULL, // KEY_WAKEUP
144 = .NULL, // KEY_FILE
145 = .NULL, // KEY_SENDFILE
146 = .NULL, // KEY_DELETEFILE
147 = .NULL, // KEY_XFER
148 = .NULL, // KEY_PROG1
149 = .NULL, // KEY_PROG2
150 = .NULL, // WWW
151 = .NULL, // KEY_MSDOS
152 = .NULL, // KEY_COFFEE
153 = .NULL, // KEY_DIRECTION
154 = .NULL, // KEY_CYCLEWINDOWS
155 = .NULL, // MAIL
156 = .NULL, // AC_BOOKMARKS
157 = .NULL, // COMPUTER
158 = .NULL, // AC_BACK
159 = .NULL, // AC_FORWARD
160 = .NULL, // KEY_CLOSECD
161 = .NULL, // EJECT
162 = .NULL, // KEY_EJECTCLOSECD
163 = .NULL, // AUDIO_NEXT
164 = .NULL, // AUDIO_PLAY
165 = .NULL, // AUDIO_PREV
166 = .NULL, // AUDIO_STOP
167 = .NULL, // KEY_RECORD
168 = .NULL, // AUDIO_REWIND
169 = .NULL, // KEY_PHONE
170 = .NULL, // KEY_ISO
171 = .NULL, // KEY_CONFIG
172 = .NULL, // AC_HOME
173 = .NULL, // AC_REFRESH
174 = .NULL, // KEY_EXIT
175 = .NULL, // KEY_MOVE
176 = .NULL, // KEY_EDIT
177 = .NULL, // KEY_SCROLLUP
178 = .NULL, // KEY_SCROLLDOWN
179 = .KP_LEFT_PAREN,
180 = .KP_RIGHT_PAREN,
181 = .NULL, // KEY_NEW
182 = .NULL, // KEY_REDO
183 = .F13,
184 = .F14,
185 = .F15,
186 = .F16,
187 = .F17,
188 = .F18,
189 = .F19,
190 = .F20,
191 = .F21,
192 = .F22,
193 = .F23,
194 = .F24,
// 195-199
200 = .NULL, // KEY_PLAYCD
201 = .NULL, // KEY_PAUSECD
202 = .NULL, // KEY_PROG3
203 = .NULL, // KEY_PROG4
// 204
205 = .NULL, // KEY_SUSPEND
206 = .NULL, // KEY_CLOSE
207 = .NULL, // KEY_PLAY
208 = .NULL, // AUDIO_FASTFORWARD
209 = .NULL, // KEY_BASSBOOST
210 = .NULL, // KEY_PRINT
211 = .NULL, // KEY_HP
212 = .NULL, // KEY_CAMERA
213 = .NULL, // KEY_SOUND
214 = .NULL, // KEY_QUESTION
215 = .NULL, // KEY_EMAIL
216 = .NULL, // KEY_CHAT
217 = .NULL, // AC_SEARCH
218 = .NULL, // KEY_CONNECT
219 = .NULL, // KEY_FINANCE
220 = .NULL, // KEY_SPORT
221 = .NULL, // KEY_SHOP
222 = .ALT_ERASE,
223 = .CANCEL,
224 = .NULL, // BRIGHTNESS_DOWN
225 = .NULL, // BRIGHNESS_UP
226 = .NULL, // KEY_MEDIA
227 = .NULL, // DISPLAY_SWITCH
228 = .NULL, // KBD_ILLUM_TOGGLE
229 = .NULL, // KBD_ILLUM_DOWN
230 = .NULL, // KBD_ILLUM_UP
231 = .NULL, // KEY_SEND
232 = .NULL, // KEY_REPLY
233 = .NULL, // KEY_FORWARDEMAIL
234 = .NULL, // KEY_SAVE
235 = .NULL, // KEY_DOCUMENTS
236 = .NULL, // KEY_BATTERY
}
keyboard_fn_keysym_to_keycode := []Keycode {
0x08 = .BACKSPACE,
0x09 = .TAB,
0x0b = .CLEAR,
0x0d = .ENTER,
0x13 = .PAUSE,
0x14 = .SCROLL_LOCK,
0x15 = .SYSREQ,
0x1b = .ESCAPE,
0x50 = .HOME,
0x51 = .LEFT,
0x52 = .UP,
0x53 = .RIGHT,
0x54 = .DOWN,
0x55 = .PAGE_UP,
0x56 = .PAGE_DOWN,
0x57 = .END,
0x60 = .SELECT,
0x61 = .PRINT_SCREEN,
0x62 = .EXECUTE,
0x63 = .INSERT,
0x65 = .UNDO,
0x67 = .MENU,
0x68 = .FIND,
0x69 = .CANCEL,
0x6a = .HELP,
0x7f = .NUM_LOCK_OR_CLEAR,
0x80 = .KP_SPACE,
0x89 = .KP_TAB,
0x8d = .KP_ENTER,
0xbd = .KP_EQUALS,
0xaa = .KP_MULTIPLY,
0xab = .KP_PLUS,
0xad = .KP_MINUS,
0xae = .KP_DECIMAL,
0xaf = .KP_DIVIDE,
0xb0 = .KP_0,
0xb1 = .KP_1,
0xb2 = .KP_2,
0xb3 = .KP_3,
0xb4 = .KP_4,
0xb5 = .KP_5,
0xb6 = .KP_6,
0xb7 = .KP_7,
0xb8 = .KP_8,
0xb9 = .KP_9,
0xbe = .F1,
0xbf = .F2,
0xc0 = .F3,
0xc1 = .F4,
0xc2 = .F5,
0xc3 = .F6,
0xc4 = .F7,
0xc5 = .F8,
0xc6 = .F9,
0xc7 = .F10,
0xc8 = .F11,
0xc9 = .F12,
0xca = .F13,
0xcb = .F14,
0xcc = .F15,
0xcd = .F16,
0xce = .F17,
0xcf = .F18,
0xd0 = .F19,
0xd1 = .F20,
0xd2 = .F21,
0xd3 = .F22,
0xd4 = .F23,
0xd5 = .F24,
0xe1 = .LEFT_SHIFT,
0xe2 = .RIGHT_SHIFT,
0xe3 = .LEFT_CTRL,
0xe4 = .RIGHT_CTRL,
0xe5 = .CAPS_LOCK,
0xe6 = .CAPS_LOCK,
0xe7 = .LEFT_META,
0xe8 = .RIGHT_META,
0xe9 = .LEFT_ALT,
0xea = .RIGHT_ALT,
0xeb = .LEFT_META,
0xec = .RIGHT_META,
0xff = .DELETE,
}
keyboard_fn_ex_keysym_to_keycode := []Keycode {
0x03 = .RIGHT_ALT,
0xff = .NULL,
}
keyboard_latin_1_fn_ex_keysym_to_keycode := []Keycode {
0x20 = .SPACE,
0x27 = .APOSTROPHE,
0x2c = .COMMA,
0x2d = .MINUS,
0x2e = .PERIOD,
0x2f = .SLASH,
0x30 = .KEY_0,
0x31 = .KEY_1,
0x32 = .KEY_2,
0x33 = .KEY_3,
0x34 = .KEY_4,
0x35 = .KEY_5,
0x36 = .KEY_6,
0x37 = .KEY_7,
0x38 = .KEY_8,
0x39 = .KEY_9,
0x3b = .SEMICOLON,
0x3d = .EQUALS,
0x5b = .LEFT_BRACKET,
0x5c = .BACKSLASH,
0x5d = .RIGHT_BRACKET,
0x60 = .GRAVE,
0x61 = .A,
0x62 = .B,
0x63 = .C,
0x64 = .D,
0x65 = .E,
0x66 = .F,
0x67 = .G,
0x68 = .H,
0x69 = .I,
0x6a = .J,
0x6b = .K,
0x6c = .L,
0x6d = .M,
0x6e = .N,
0x6f = .O,
0x70 = .P,
0x71 = .Q,
0x72 = .R,
0x73 = .S,
0x74 = .T,
0x75 = .U,
0x76 = .V,
0x77 = .W,
0x78 = .X,
0x79 = .Y,
0x7a = .Z,
0xff = .NULL,
}
@(private = "file")
x11_go_fullscreen :: proc() {
// remove the resizing constraint before going fullscreen so WMs such as gnome
// can add the _NET_WM_ACTION_FULLSCREEN action to the _NET_WM_ALLOWED_ACTIONS atom
// and properly fullscreen the window
size_hints := x11.AllocSizeHints()
zephr_ctx.window.pre_fullscreen_size = zephr_ctx.window.size
if (size_hints != nil) {
size_hints.flags = {.PPosition, .PSize}
size_hints.width = cast(i32)zephr_ctx.window.size.x
size_hints.height = cast(i32)zephr_ctx.window.size.y
x11.SetWMNormalHints(l_os.x11_display, l_os.x11_window, size_hints)
x11.Free(size_hints)
}
xev: x11.XEvent
wm_state := x11.InternAtom(l_os.x11_display, "_NET_WM_STATE", false)
fullscreen := x11.InternAtom(l_os.x11_display, "_NET_WM_STATE_FULLSCREEN", false)
xev.type = .ClientMessage
xev.xclient.window = l_os.x11_window
xev.xclient.message_type = wm_state
xev.xclient.format = 32
xev.xclient.data.l[0] = 1 // _NET_WM_STATE_ADD
xev.xclient.data.l[1] = cast(int)fullscreen
xev.xclient.data.l[2] = 0
x11.SendEvent(
l_os.x11_display,
x11.DefaultRootWindow(l_os.x11_display),
false,
{.SubstructureNotify, .SubstructureRedirect},
&xev,
)
}
@(private = "file")
x11_return_fullscreen :: proc() {
xev: x11.XEvent
wm_state := x11.InternAtom(l_os.x11_display, "_NET_WM_STATE", false)
fullscreen := x11.InternAtom(l_os.x11_display, "_NET_WM_STATE_FULLSCREEN", false)
xev.type = .ClientMessage
xev.xclient.window = l_os.x11_window
xev.xclient.message_type = wm_state
xev.xclient.format = 32
xev.xclient.data.l[0] = 0 // _NET_WM_STATE_REMOVE
xev.xclient.data.l[1] = cast(int)fullscreen
xev.xclient.data.l[2] = 0
x11.SendEvent(
l_os.x11_display,
x11.DefaultRootWindow(l_os.x11_display),
false,
{.SubstructureNotify, .SubstructureRedirect},
&xev,
)
// restore the resizing constraint as well as the pre-fullscreen window size
// when returning from fullscreen
size_hints := x11.AllocSizeHints()
if (size_hints != nil) {
size_hints.flags = {.PPosition, .PSize}
size_hints.width = cast(i32)zephr_ctx.window.pre_fullscreen_size.x
size_hints.height = cast(i32)zephr_ctx.window.pre_fullscreen_size.y
if (zephr_ctx.window.non_resizable) {
size_hints.flags |= {.PMinSize, .PMaxSize}
size_hints.min_width = cast(i32)zephr_ctx.window.pre_fullscreen_size.x
size_hints.min_height = cast(i32)zephr_ctx.window.pre_fullscreen_size.y
size_hints.max_width = cast(i32)zephr_ctx.window.pre_fullscreen_size.x
size_hints.max_height = cast(i32)zephr_ctx.window.pre_fullscreen_size.y
}
x11.SetWMNormalHints(l_os.x11_display, l_os.x11_window, size_hints)
x11.Free(size_hints)
}
}
backend_toggle_fullscreen :: proc(fullscreen: bool) {
if fullscreen {
x11_return_fullscreen()
} else {
x11_go_fullscreen()
}
}
@(private = "file")
x11_assign_window_icon :: proc(icon_path: cstring, window_title: cstring) {
icon_width, icon_height: i32
icon_data := image.load(icon_path, &icon_width, &icon_height, nil, 4)
defer image.image_free(icon_data)
log.assert(icon_data != nil, "Failed to load icon image")
target_size := 2 + icon_width * icon_height
data := make([]u64, target_size)
defer delete(data)
// first two elements are width and height
data[0] = cast(u64)icon_width
data[1] = cast(u64)icon_height
for i in 0 ..< (icon_width * icon_height) {
data[i + 2] =
(cast(u64)icon_data[i * 4] << 16) |
(cast(u64)icon_data[i * 4 + 1] << 8) |
(cast(u64)icon_data[i * 4 + 2] << 0) |
(cast(u64)icon_data[i * 4 + 3] << 24)
}
net_wm_icon := x11.InternAtom(l_os.x11_display, "_NET_WM_ICON", false)
x11.ChangeProperty(
l_os.x11_display,
l_os.x11_window,
net_wm_icon,
XA_CARDINAL,
32,
x11.PropModeReplace,
raw_data(data),
target_size,
)
}
backend_get_screen_size :: proc() -> m.vec2 {
size := get_active_monitor_dims(x11.DefaultRootWindow(l_os.x11_display))
return m.vec2{size.z, size.w}
}
@(private = "file")
get_active_monitor_dims :: proc(root: x11.Window) -> m.vec4 {
c_pos := get_cursor_pos(root)
scr_resources := x11.XRRGetScreenResources(l_os.x11_display, root)
log.assert(scr_resources != nil, "Failed to get X11 screen resources")
defer x11.XRRFreeScreenResources(scr_resources)
dims: m.vec4
for i in 0 ..< scr_resources.noutput {
output_info := x11.XRRGetOutputInfo(l_os.x11_display, scr_resources, scr_resources.outputs[i])
if output_info.connection == .RR_Connected {
if output_info.crtc == 0 { // ??? idk lol gets rid of a crtc error and the displays work fine anyway
x11.XRRFreeOutputInfo(output_info)
continue
}
crtc_info := x11.XRRGetCrtcInfo(l_os.x11_display, scr_resources, output_info.crtc)
if crtc_info != nil {
if cast(i32)c_pos.x >= crtc_info.x &&
cast(i32)c_pos.x <= cast(i32)crtc_info.width + crtc_info.x &&
cast(i32)c_pos.y >= crtc_info.y &&
cast(i32)c_pos.y <= cast(i32)crtc_info.height + crtc_info.y {
log.infof("Active Monitor is %d with Resolution %dx%d", i, crtc_info.width, crtc_info.height)
dims = {
cast(f32)crtc_info.x,
cast(f32)crtc_info.y,
cast(f32)crtc_info.width,
cast(f32)crtc_info.height,
}
} else {
log.infof("Found Monitor %d with Resolution %dx%d", i, crtc_info.width, crtc_info.height)
}
x11.XRRFreeCrtcInfo(crtc_info)
}
}
x11.XRRFreeOutputInfo(output_info)
}
if dims == {0, 0, 0, 0} {
log.error("Failed to find the active monitor")
}
return dims
}
@(private = "file")
x11_create_window :: proc(window_title: cstring, window_size: m.vec2, icon_path: cstring, window_non_resizable: bool) {
screen_num := x11.DefaultScreen(l_os.x11_display)
// Load up GL/GLX before creating the window to be able to use the FBConfig's visual info when creating the window.
gl.load_up_to(
3,
3,
proc(p: rawptr, name: cstring) {(cast(^rawptr)p)^ = glx.GetProcAddressARB(raw_data(string(name)))},
)
glx_major: i32
glx_minor: i32
glx.QueryVersion(l_os.x11_display, &glx_major, &glx_minor)
log.infof("Loaded GLX: %d.%d", glx_major, glx_minor)
//odinfmt: disable
visual_attributes := []i32 {
glx.X_RENDERABLE, 1,
glx.DRAWABLE_TYPE, glx.WINDOW_BIT,
glx.X_VISUAL_TYPE, glx.TRUE_COLOR,
glx.RENDER_TYPE, glx.RGBA_BIT,
glx.BUFFER_SIZE, 8,
glx.RED_SIZE, 8,
glx.GREEN_SIZE, 8,
glx.BLUE_SIZE, 8,
glx.ALPHA_SIZE, 8,
glx.DEPTH_SIZE, 24,
glx.DOUBLEBUFFER, 1,
x11.None,
}
//odinfmt: enable
num_fbc: i32
fbc := glx.ChooseFBConfig(l_os.x11_display, screen_num, raw_data(visual_attributes), &num_fbc)
visual_info := glx.GetVisualFromFBConfig(l_os.x11_display, fbc[0])
defer x11.Free(visual_info)
defer x11.Free(fbc)
//odinfmt: disable
context_attributes := []i32 {
glx.CONTEXT_MAJOR_VERSION_ARB, 3,
glx.CONTEXT_MINOR_VERSION_ARB, 3,
glx.CONTEXT_PROFILE_MASK_ARB, glx.CONTEXT_CORE_PROFILE_BIT_ARB,
x11.None,
}
//odinfmt: enable
l_os.glx_context = glx.CreateContextAttribsARB(l_os.x11_display, fbc[0], nil, true, raw_data(context_attributes))
if l_os.glx_context == nil {
log.error("Failed to create GLX context")
return
}
root := x11.RootWindow(l_os.x11_display, visual_info.screen)
active_monitor_dims := get_active_monitor_dims(root)
l_os.x11_colormap = x11.CreateColormap(l_os.x11_display, root, visual_info.visual, x11.ColormapAlloc.AllocNone)
attributes: x11.XSetWindowAttributes
attributes.event_mask = {
.Exposure,
.KeyPress,
.KeyRelease,
.StructureNotify,
.ButtonPress,
.ButtonRelease,
.PointerMotion,
.EnterWindow,
.LeaveWindow,
}
attributes.colormap = l_os.x11_colormap
window_start_x :=
(cast(i32)active_monitor_dims.z / 2 - cast(i32)window_size.x / 2) + cast(i32)active_monitor_dims.x
window_start_y :=
(cast(i32)active_monitor_dims.w / 2 - cast(i32)window_size.y / 2) + cast(i32)active_monitor_dims.y
l_os.x11_window = x11.CreateWindow(
l_os.x11_display,
root,
window_start_x,
window_start_y,
cast(u32)window_size.x,
cast(u32)window_size.y,
0,
visual_info.depth,
.InputOutput,
visual_info.visual,
{.CWColormap, .CWEventMask},
&attributes,
)
if (icon_path != "") {
x11_assign_window_icon(icon_path, window_title)
}
// Hints to the WM that the window is a normal window
// Of course this is only a hint and the WM can ignore it
net_wm_window_type := x11.InternAtom(l_os.x11_display, "_NET_WM_WINDOW_TYPE", false)
net_wm_window_type_normal := x11.InternAtom(l_os.x11_display, "_NET_WM_WINDOW_TYPE_NORMAL", false)
x11.ChangeProperty(
l_os.x11_display,
l_os.x11_window,
net_wm_window_type,
x11.XA_ATOM,
32,
x11.PropModeReplace,
&net_wm_window_type_normal,
1,
)
// define Xdnd atoms
l_os.xdnd.xdnd_enter = x11.InternAtom(l_os.x11_display, "XdndEnter", false)
l_os.xdnd.xdnd_leave = x11.InternAtom(l_os.x11_display, "XdndLeave", false)
l_os.xdnd.xdnd_drop = x11.InternAtom(l_os.x11_display, "XdndDrop", false)
l_os.xdnd.xdnd_status = x11.InternAtom(l_os.x11_display, "XdndStatus", false)
l_os.xdnd.xdnd_finished = x11.InternAtom(l_os.x11_display, "XdndFinished", false)
l_os.xdnd.xdnd_position = x11.InternAtom(l_os.x11_display, "XdndPosition", false)
l_os.xdnd.xdnd_selection = x11.InternAtom(l_os.x11_display, "XdndSelection", false)
l_os.xdnd.XDND_DATA = x11.InternAtom(l_os.x11_display, "XDND_DATA", false)
l_os.xdnd.xdnd_type_list = x11.InternAtom(l_os.x11_display, "XdndTypeList", false)
l_os.xdnd.supported_types = {
x11.InternAtom(l_os.x11_display, "text/plain", false),
x11.InternAtom(l_os.x11_display, "text/plain;charset=utf-8", false),
x11.InternAtom(l_os.x11_display, "text/uri-list", false),
x11.InternAtom(l_os.x11_display, "UTF8_STRING", false),
x11.InternAtom(l_os.x11_display, "TEXT", false),
x11.InternAtom(l_os.x11_display, "STRING", false),
}
l_os.xdnd.xdnd_version = XDND_PROTOCOL_VERSION
// Add XdndAware property to the window
l_os.xdnd.xdnd_aware = x11.InternAtom(l_os.x11_display, "XdndAware", false)
x11.ChangeProperty(
l_os.x11_display,
l_os.x11_window,
l_os.xdnd.xdnd_aware,
x11.XA_ATOM,
32,
x11.PropModeReplace,
&l_os.xdnd.xdnd_version,
1,
)
wm_delete_window := x11.InternAtom(l_os.x11_display, "WM_DELETE_WINDOW", false)
x11.SetWMProtocols(l_os.x11_display, l_os.x11_window, &wm_delete_window, 1)
l_os.window_delete_atom = wm_delete_window
// set window name
{
UTF8_STRING := x11.InternAtom(l_os.x11_display, "UTF8_STRING", false)
x11.StoreName(l_os.x11_display, l_os.x11_window, window_title)
text_property: x11.XTextProperty
text_property.value = raw_data(string(window_title))
text_property.format = 8
text_property.encoding = UTF8_STRING
text_property.nitems = len(window_title)
x11.SetWMName(l_os.x11_display, l_os.x11_window, &text_property)
net_wm_name := x11.InternAtom(l_os.x11_display, "_NET_WM_NAME", false)
wm_class := x11.InternAtom(l_os.x11_display, "WM_CLASS", false)
x11.ChangeProperty(
l_os.x11_display,