forked from lockie/racket-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.rkt
4271 lines (3203 loc) · 119 KB
/
pretty.rkt
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
#lang racket/base
(require
(for-syntax racket/base
racket/format
racket/syntax
racket/string)
ffi/unsafe
ffi/unsafe/define
(submod racket/performance-hint begin-encourage-inline)
(rename-in racket/contract
[-> contract/->])
sdl2/private/lib-path)
(provide
(except-out
(all-defined-out)
convention:uglify
convention:uglify-minor
sdl2-lib
define-sdl2
define-sdl2-vararg))
(define-syntax (convention:uglify id)
(define str
(string-replace
(string-replace
(symbol->string (syntax-e id))
"!" "")
"*" ""))
(when (string-suffix? str "?")
(set! str (string-append
"is-" (string-replace str "?" ""))))
(when (and (> (length (string-split str "-")) 2) (string-suffix? str "-rw"))
(set! str (string-replace str "-rw" "-_-rw")))
(when (string-prefix? str "gl-")
(set! str (string-replace str "gl-" "gl-_-")))
(when (string-prefix? str "vulkan-")
(set! str (string-replace str "vulkan-" "vulkan-_-")))
(define uppercase-exceptions
'("gl" "rw" "le16" "be16" "le32" "be32" "le64" "be64" "id" "guid" "tls"
"rgb" "rgba" "yuv" "bmp" "rle" "wm" "cvt" "rt" "cas" "wav" "fp"
"cpu" "ram" "rdtsc" "mmx" "sse" "sse2" "sse3" "sse41" "sse42"
"avx" "avx2" "avx512" "neon" "d3d9" "direct3d9" "dxgi" "jni"
"tv" "fs" "utf8" "unicode"))
(format-id id
(apply string-append
"SDL_"
(map
(lambda (s)
(cond
[(member s uppercase-exceptions)
(string-upcase s)]
[(string=? s "iphone") "iPhone"]
[(string=? s "dex") "DeX"]
[else (string-titlecase s)]))
(string-split str "-")))))
(define-syntax (convention:uglify-minor id)
(define str
(string-replace
(symbol->string (syntax-e id))
"*" ""))
(when (string-suffix? str "?")
(set! str (string-append
"is" (string-replace str "?" ""))))
(format-id id
(string-append
"SDL_"
(string-replace str "-" "_"))))
(define sdl2-lib (ffi-lib (lib-path) '("0" #f)))
(define-ffi-definer define-sdl2 sdl2-lib #:make-c-id convention:uglify)
(define-ffi-definer define-sdl2-minor sdl2-lib #:make-c-id convention:uglify-minor)
(define-cpointer-type _uint8*)
(define-cpointer-type _int8*)
(define-cpointer-type _uint16*)
(define-cpointer-type _int16*)
(define-cpointer-type _uint32*)
(define-cpointer-type _int32*)
(define-cpointer-type _uint*)
(define-cpointer-type _int*)
(define-cpointer-type _size*)
(define-cpointer-type _float*)
(define-syntax (define-sdl2-vararg stx)
(syntax-case stx ()
[(_ name orig-name (arg-types ...) ret-type)
(with-syntax*
([name-str (~a (syntax->datum #'orig-name))]
[arglist
(map
(lambda (arg)
(gensym (syntax->datum arg)))
(syntax->list #'(arg-types ...)))]
[full-arglist (append (syntax->list #'arglist) 'args)]
[final-call (append '(apply fun) (syntax->list #'arglist) '(args))])
#'(define name
(let ([interfaces (make-hash)])
(lambda full-arglist
(define itypes
(append (list arg-types ...)
(map (lambda (x)
(cond
[(and (integer? x) (exact? x)) _int]
[(and (number? x) (real? x)) _double*]
[(string? x) _string]
[(bytes? x) _bytes]
[(symbol? x) _symbol]
[else
(error
"don't know how to deal with ~e" x)]))
args)))
(let ([fun (hash-ref
interfaces itypes
(lambda ()
(let ([i (get-ffi-obj
name-str sdl2-lib
(_cprocedure itypes ret-type))])
(hash-set! interfaces itypes i)
i)))])
final-call)))))]))
;; SDL_stdinc.h
(define arraysize array-length)
(define tablesize arraysize)
(define-syntax-rule (stringify-arg arg)
(~a arg))
(define (fourcc A B C D)
(let ([a (if (char? A) (char->integer A) A)]
[b (if (char? B) (char->integer B) B)]
[c (if (char? C) (char->integer C) C)]
[d (if (char? D) (char->integer D) D)])
(bitwise-ior
(bitwise-and #xffffffff (arithmetic-shift (bitwise-and #xff a) 0))
(bitwise-and #xffffffff (arithmetic-shift (bitwise-and #xff b) 8))
(bitwise-and #xffffffff (arithmetic-shift (bitwise-and #xff c) 16))
(bitwise-and #xffffffff (arithmetic-shift (bitwise-and #xff d) 24)))))
(define false 0)
(define true 1)
(define max-sint8 #x7f)
(define min-sint8 (bitwise-not #x7f))
(define max-uint8 #xff)
(define min-uint8 #x00)
(define max-sint16 #x7fff)
(define min-sint16 (bitwise-not #x7fff))
(define max-uint16 #xffff)
(define min-uint16 #x0000)
(define max-sint32 #x7fffffff)
(define min-sint32 (bitwise-not #x7fffffff))
(define max-uint32 #xffffffff)
(define min-uint32 #x00000000)
(define max-sint64 #x7fffffffffffffff)
(define min-sint64 (bitwise-not #x7fffffffffffffff))
(define max-uint64 #xffffffffffffffff)
(define min-uint64 #x0000000000000000)
(define-sdl2-minor malloc* (_fun _size -> _pointer))
(define-sdl2-minor calloc* (_fun _size _size -> _pointer))
(define-sdl2-minor realloc* (_fun _pointer _size -> _pointer))
(define-sdl2-minor free* (_fun _pointer -> _void))
(define stack-alloc malloc)
(define stack-free free)
(define _malloc-func (_fun _size -> _pointer))
(define _calloc-func (_fun _size _size -> _pointer))
(define _realloc-func (_fun _pointer _size -> _pointer))
(define _free-func (_fun _pointer -> _void))
(define-sdl2 get-memory-functions
(_fun _malloc-func _calloc-func _realloc-func _free-func -> _void)
#:make-fail make-not-available)
(define-sdl2 set-memory-functions!
(_fun _malloc-func _calloc-func _realloc-func _free-func -> _int)
#:make-fail make-not-available)
(define-sdl2 get-num-allocations (_fun -> _int)
#:make-fail make-not-available)
(define-sdl2-minor getenv (_fun _string -> _string))
(define-sdl2-minor setenv (_fun _string _string _int -> _int))
(define-sdl2-minor qsort (_fun _pointer _size _size (_fun _pointer _pointer -> _int) -> _void))
(define-sdl2-minor abs (_fun _int -> _int))
(define-syntax-rule (min x y)
(if (< x y) x y))
(define-syntax-rule (max x y)
(if (> x y) x y))
(define-sdl2-minor digit? (_fun _int -> _int))
(define-sdl2-minor space? (_fun _int -> _int))
(define-sdl2-minor toupper (_fun _int -> _int))
(define-sdl2-minor tolower (_fun _int -> _int))
(define-sdl2-minor memset* (_fun _pointer _int _size -> _pointer))
(define-sdl2-minor memcpy* (_fun _pointer _pointer _size -> _pointer))
(define-sdl2-minor memmove* (_fun _pointer _pointer _size -> _pointer))
(define-sdl2-minor memcmp (_fun _pointer _pointer _size -> _int))
(define-sdl2-minor wcsdup (_fun _string/ucs-4 -> _string/ucs-4)
#:make-fail make-not-available)
(define-sdl2-minor wcslen (_fun _string/ucs-4 -> _size))
(define-sdl2-minor wcslcpy (_fun _string/ucs-4 _string/ucs-4 _size -> _size))
(define-sdl2-minor wcslcat (_fun _string/ucs-4 _string/ucs-4 _size -> _size))
(define-sdl2-minor wcscmp (_fun _string/ucs-4 _string/ucs-4 -> _int)
#:make-fail make-not-available)
(define-sdl2-minor strlen (_fun _string -> _size))
(define-sdl2-minor strlcpy (_fun _string _string _size -> _size))
(define-sdl2-minor utf8strlcpy (_fun _string _string _size -> _size))
(define-sdl2-minor strlcat (_fun _string _string _size -> _size))
(define-sdl2-minor strdup (_fun _string -> _string))
(define-sdl2-minor strrev (_fun _string -> _string))
(define-sdl2-minor strupr (_fun _string -> _string))
(define-sdl2-minor strlwr (_fun _string -> _string))
(define-sdl2-minor strchr (_fun _string _int -> _string))
(define-sdl2-minor strrchr (_fun _string _int -> _string))
(define-sdl2-minor strstr (_fun _string _string -> _string))
(define-sdl2-minor utf8strlen (_fun _string -> _size)
#:make-fail make-not-available)
(define-sdl2-minor itoa (_fun _int _string _int -> _string))
(define-sdl2-minor uitoa (_fun _uint _string _int -> _string))
(define-sdl2-minor ltoa (_fun _long _string _int -> _string))
(define-sdl2-minor ultoa (_fun _ulong _string _int -> _string))
(define-sdl2-minor lltoa (_fun _sint64 _string _int -> _string))
(define-sdl2-minor ulltoa (_fun _uint64 _string _int -> _string))
(define-sdl2-minor atoi (_fun _string -> _int))
(define-sdl2-minor atof (_fun _string -> _double))
(define-sdl2-minor strtol (_fun _string (_ptr i _string) _int -> _long))
(define-sdl2-minor strtoul (_fun _string (_ptr i _string) _int -> _ulong))
(define-sdl2-minor strtoll (_fun _string (_ptr i _string) _int -> _sint64))
(define-sdl2-minor strtoull (_fun _string (_ptr i _string) _int -> _uint64))
(define-sdl2-minor strtod (_fun _string (_ptr i _string) -> _double))
(define-sdl2-minor strcmp (_fun _string _string -> _int))
(define-sdl2-minor strncmp (_fun _string _string _size -> _int))
(define-sdl2-minor strcasecmp (_fun _string _string -> _int))
(define-sdl2-minor strncasecmp (_fun _string _string _size -> _int))
(define-sdl2-minor acos (_fun _double -> _double)
#:make-fail make-not-available)
(define-sdl2-minor acosf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor asin (_fun _double -> _double)
#:make-fail make-not-available)
(define-sdl2-minor asinf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor atan (_fun _double -> _double))
(define-sdl2-minor atanf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor atan2 (_fun _double _double -> _double))
(define-sdl2-minor atan2f (_fun _float _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor ceil (_fun _double -> _double))
(define-sdl2-minor ceilf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor copysign (_fun _double _double -> _double))
(define-sdl2-minor copysignf (_fun _float _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor cos (_fun _double -> _double))
(define-sdl2-minor cosf (_fun _float -> _float))
(define-sdl2-minor exp (_fun _double -> _double)
#:make-fail make-not-available)
(define-sdl2-minor expf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor fabs (_fun _double -> _double))
(define-sdl2-minor fabsf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor floor (_fun _double -> _double))
(define-sdl2-minor floorf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor fmod (_fun _double _double -> _double)
#:make-fail make-not-available)
(define-sdl2-minor fmodf (_fun _float _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor log (_fun _double -> _double))
(define-sdl2-minor logf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor log10 (_fun _double -> _double)
#:make-fail make-not-available)
(define-sdl2-minor log10f (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor pow (_fun _double _double -> _double))
(define-sdl2-minor powf (_fun _float _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor scalbn (_fun _double _int -> _double))
(define-sdl2-minor scalbnf (_fun _float _int -> _float)
#:make-fail make-not-available)
(define-sdl2-minor sin (_fun _double -> _double))
(define-sdl2-minor sinf (_fun _float -> _float))
(define-sdl2-minor sqrt (_fun _double -> _double))
(define-sdl2-minor sqrtf (_fun _float -> _float)
#:make-fail make-not-available)
(define-sdl2-minor tan (_fun _double -> _double)
#:make-fail make-not-available)
(define-sdl2-minor tanf (_fun _float -> _float)
#:make-fail make-not-available)
(define ERROR (cast -1 _intptr _size))
(define E2BIG (cast -2 _intptr _size))
(define EILSEQ (cast -3 _intptr _size))
(define EINVAL (cast -4 _intptr _size))
(define-cpointer-type _iconv_t*)
(define-sdl2-minor iconv-open (_fun _string _string -> _iconv_t*))
(define-sdl2-minor iconv-close (_fun _iconv_t* -> _int))
(define-sdl2-minor iconv (_fun _iconv_t* (_ptr i _string) _size* (_ptr o _string) _size* -> _size))
(define-sdl2-minor iconv-string (_fun _string _string _string _size -> _string))
(define (iconv-utf8-locale s)
(iconv-string "" "UTF-8" s (+ (strlen s) 1)))
(define (iconv-utf8-ucs2 s)
(cast
(iconv-string "UCS-2-INTERNAL" "UTF-8" s (+ (strlen s) 1))
_string
_uint16*/null))
(define (iconv-utf8-ucs4 s)
(cast
(iconv-string "UCS-4-INTERNAL" "UTF-8" s (+ (strlen s) 1))
_string
_uint32*/null))
(define (memcpy4 dst src dwords)
(memcpy dst src (* dwords 4)))
;; SDL_assert.h
(define _assert-state
(_enum
'(retry
break
abort
ignore
always-ignore)))
(define-cstruct _assert-data
([always-ignore _int]
[trigger-count _uint]
[condition _string]
[filename _string]
[linenum _int]
[function _string]
[next _pointer]))
(define _assert-data* _assert-data-pointer)
(define-sdl2 report-assertion (_fun _assert-data* _string _string _int -> _assert-state))
(define _assertion-handler (_fun _assert-data* _pointer -> _assert-state))
(define-sdl2 set-assertion-handler! (_fun _assertion-handler _pointer -> _void))
(define-sdl2 get-default-assertion-handler (_fun -> _assertion-handler)
#:make-fail make-not-available)
(define-sdl2 get-assertion-handler (_fun (_ptr o _pointer) -> _assertion-handler)
#:make-fail make-not-available)
(define-sdl2 get-assertion-report (_fun -> _assert-data*))
(define-sdl2 reset-assertion-report! (_fun -> _void))
;; SDL_atomic.h
(define _spin-lock _int)
(define _spin-lock* _int*)
(define-sdl2 atomic-try-lock! (_fun _spin-lock* -> _bool))
(define-sdl2 atomic-lock! (_fun _spin-lock* -> _void))
(define-sdl2 atomic-unlock! (_fun _spin-lock* -> _void))
(define (compiler-barrier)
(define tmp (cast (malloc (ctype-sizeof _int)) _pointer _int*))
(ptr-set! tmp _int 0)
(atomic-lock! tmp)
(atomic-unlock! tmp))
(define memory-barrier-release! compiler-barrier)
(define memory-barrier-acquire! compiler-barrier)
(define-cstruct _atomic-t
([value _int]))
(define _atomic-t* _atomic-t-pointer)
(define-sdl2 atomic-cas! (_fun _atomic-t* _int _int -> _bool))
(define-sdl2 atomic-get (_fun _atomic-t* -> _int)
#:make-fail make-not-available)
(define-sdl2 atomic-add! (_fun _atomic-t* _int -> _int)
#:make-fail make-not-available)
(define (atomic-inc-ref! a) (atomic-add! a 1))
(define (atomic-dec-ref! a) (= 1 (atomic-add! a -1)))
(define-sdl2 atomic-cas-ptr! (_fun (_ptr io _pointer) _pointer _pointer -> _bool))
(define-sdl2 atomic-set-ptr! (_fun (_ptr io _pointer) _pointer -> _pointer)
#:make-fail make-not-available)
(define-sdl2 atomic-get-ptr (_fun (_ptr i _pointer) -> _pointer)
#:make-fail make-not-available)
;; SDL_rwops.h
(define unknown 0)
(define winfile 1)
(define stdfile 2)
(define jnifile 3)
(define memory 4)
(define memory-ro 5)
(define-cstruct _rw-ops
([size (_fun _pointer -> _sint64)]
[seek (_fun _pointer _sint64 _int -> _sint64)]
[read (_fun _pointer _pointer _size _size -> _size)]
[write (_fun _pointer _pointer _size _size -> _size)]
[close (_fun _pointer -> _int)]
[type _uint32]
;; XXX be on the safe side and occupy maximum space for the hidden union
[hidden-ptr1 _pointer]
[hidden-ptr2 _pointer]
[hidden-ptr3 _pointer]
[hidden-ptr4 _pointer]
[hidden-ptr5 _pointer]
[hidden-ptr6 _pointer]
[hidden-ptr7 _pointer]
[hidden-ptr8 _pointer]
[hidden-ptr9 _pointer]))
(define _rw-ops* _rw-ops-pointer)
(define _rw-ops*/null _rw-ops-pointer/null)
(define-sdl2 rw-from-file (_fun _string _string -> _rw-ops*/null))
(define-sdl2 rw-from-fp (_fun _pointer _bool -> _rw-ops*/null))
(define-sdl2 rw-from-mem (_fun _pointer _int -> _rw-ops*/null))
(define-sdl2 rw-from-const-mem (_fun _pointer _int -> _rw-ops*/null))
(define-sdl2 alloc-rw (_fun -> _rw-ops*/null))
(define-sdl2 free-rw! (_fun _rw-ops* -> _void))
(define seek-set 0)
(define seek-cur 1)
(define seek-end 2)
(define (rw-size ctx) ((rw-ops-size ctx) ctx))
(define (rw-seek ctx offset whence) ((rw-ops-seek ctx) ctx offset whence))
(define (rw-tell ctx) ((rw-ops-seek ctx) 0 seek-cur))
(define (rw-read ctx ptr size n) ((rw-ops-read ctx) ctx ptr size n))
(define (rw-write ctx ptr size n) ((rw-ops-write ctx) ctx ptr size n))
(define (rw-close! ctx) ((rw-ops-close ctx) ctx))
(define-sdl2 load-file-rw (_fun _rw-ops* _size _int -> _pointer)
#:make-fail make-not-available)
(define (load-file file data-size)
(load-file-rw
(rw-from-file file "rb")
data-size 1))
(define-sdl2 read-u8 (_fun _rw-ops* -> _uint8 ))
(define-sdl2 read-le16 (_fun _rw-ops* -> _uint16))
(define-sdl2 read-be16 (_fun _rw-ops* -> _uint16))
(define-sdl2 read-le32 (_fun _rw-ops* -> _uint32))
(define-sdl2 read-be32 (_fun _rw-ops* -> _uint32))
(define-sdl2 read-le64 (_fun _rw-ops* -> _uint64))
(define-sdl2 read-be64 (_fun _rw-ops* -> _uint64))
(define-sdl2 write-u8 (_fun _rw-ops* _uint8 -> _size))
(define-sdl2 write-le16 (_fun _rw-ops* _uint16 -> _size))
(define-sdl2 write-be16 (_fun _rw-ops* _uint16 -> _size))
(define-sdl2 write-le32 (_fun _rw-ops* _uint32 -> _size))
(define-sdl2 write-be32 (_fun _rw-ops* _uint32 -> _size))
(define-sdl2 write-le64 (_fun _rw-ops* _uint64 -> _size))
(define-sdl2 write-be64 (_fun _rw-ops* _uint64 -> _size))
;; SDL_audio.h
(define _audio-format _uint16)
(define audio-mask-bitsize #xff)
(define audio-mask-datatype (arithmetic-shift 1 8))
(define audio-mask-endian (arithmetic-shift 1 12))
(define audio-mask-signed (arithmetic-shift 1 15))
(define (audio-bitsize x) (bitwise-and x audio-mask-bitsize))
(define (audio-float? x) (bitwise-and x audio-mask-datatype))
(define (audio-bigendian? x) (bitwise-and x audio-mask-endian))
(define (audio-signed? x) (bitwise-and x audio-mask-signed))
(define (audio-int? x) (bitwise-not (audio-float? x)))
(define (audio-littleendian? x) (bitwise-not (audio-bigendian? x)))
(define (audio-unsigned? x) (bitwise-not (audio-signed? x)))
(define audio-u8 #x0008)
(define audio-s8 #x8008)
(define audio-u16-lsb #x0010)
(define audio-s16-lsb #x8010)
(define audio-u16-msb #x1010)
(define audio-s16-msb #x9010)
(define audio-u16 audio-u16-lsb)
(define audio-s16 audio-s16-lsb)
(define audio-s32-lsb #x8020)
(define audio-s32-msb #x9020)
(define audio-s32 audio-s32-lsb)
(define audio-f32-lsb #x8120)
(define audio-f32-msb #x9120)
(define audio-f32 audio-f32-lsb)
(define audio-u16-sys (if (system-big-endian?) audio-u16-msb audio-u16-lsb))
(define audio-s16-sys (if (system-big-endian?) audio-s16-msb audio-u16-lsb))
(define audio-s32-sys (if (system-big-endian?) audio-s32-msb audio-u16-lsb))
(define audio-f32-sys (if (system-big-endian?) audio-f32-msb audio-u16-lsb))
(define audio-allow-frequency-change #x00000001)
(define audio-allow-format-change #x00000002)
(define audio-allow-channels-change #x00000004)
(define audio-allow-samples-change #x00000008)
(define audio-allow-any-change (bitwise-ior
audio-allow-frequency-change
audio-allow-format-change
audio-allow-channels-change
audio-allow-samples-change))
(define _audio-callback (_fun _pointer _uint8* _int -> _void))
(define-cstruct _audio-spec
([freq _int]
[format _audio-format]
[channels _uint8]
[silence _uint8]
[samples _uint16]
[padding _uint16]
[size _uint32]
[callback _audio-callback]
[userdata _pointer]))
(define _audio-spec* _audio-spec-pointer)
(define _audio-spec*/null _audio-spec-pointer/null)
(define _audio-filter (_fun _pointer _audio-format -> _void))
(define audio-cvt-max-filters 9)
(define-cstruct _audio-cvt
([needed _int]
[src-format _audio-format]
[dst-format _audio-format]
[rate-incr _double]
[buf _uint8*]
[len _int]
[len-cvt _int]
[len-mult _int]
[len-ratio _double]
[filters (_array _audio-filter (+ audio-cvt-max-filters 1))]
[filter-index _int])
#:alignment 1)
(define _audio-cvt* _audio-cvt-pointer)
(define-sdl2 get-num-audio-drivers (_fun -> _int))
(define-sdl2 get-audio-driver (_fun _int -> _string))
(define-sdl2 audio-init (_fun _string -> _int))
(define-sdl2 audio-quit (_fun -> _void))
(define-sdl2 get-current-audio-driver (_fun -> _string))
(define-sdl2 open-audio (_fun _audio-spec* _audio-spec*/null -> _int))
(define _audio-device-id _uint32)
(define-sdl2 get-num-audio-devices (_fun _int -> _int))
(define-sdl2 get-audio-device-name (_fun _int _int -> _string))
(define-sdl2 open-audio-device
(_fun _string _int _audio-spec* _audio-spec* _int -> _audio-device-id))
(define _audio-status
(_enum
'(stopped = 0
playing
paused)))
(define-sdl2 get-audio-status (_fun -> _audio-status))
(define-sdl2 get-audio-device-status (_fun _audio-device-id -> _audio-status))
(define-sdl2 pause-audio (_fun _int -> _void))
(define-sdl2 pause-audio-device (_fun _audio-device-id _int -> _void))
(define-sdl2 load-wav-rw
(_fun _rw-ops* _int _audio-spec* (_ptr o _uint8*) _uint32* -> _audio-spec*/null))
(define (load-wav file spec audio-buf audio-len)
(load-wav-rw
(rw-from-file file "rb")
1 spec audio-buf audio-len))
(define-sdl2 free-wav! (_fun _uint8* -> _void))
(define-sdl2 build-audio-cvt
(_fun _audio-cvt* _audio-format _uint8 _int _audio-format _uint8 _int -> _int))
(define-sdl2 convert-audio (_fun _audio-cvt* -> _int))
(define-cpointer-type _audio-stream*)
(define-sdl2 new-audio-stream
(_fun _audio-format _uint8 _int _audio-format _uint8 _int -> _audio-stream*)
#:make-fail make-not-available)
(define-sdl2 audio-stream-put (_fun _audio-stream* _pointer _int -> _int)
#:make-fail make-not-available)
(define-sdl2 audio-stream-get (_fun _audio-stream* _pointer _int -> _int)
#:make-fail make-not-available)
(define-sdl2 audio-stream-available (_fun _audio-stream* -> _int)
#:make-fail make-not-available)
(define-sdl2 audio-stream-flush (_fun _audio-stream* -> _int)
#:make-fail make-not-available)
(define-sdl2 audio-stream-clear (_fun _audio-stream* -> _void)
#:make-fail make-not-available)
(define-sdl2 free-audio-stream! (_fun _audio-stream* -> _void)
#:make-fail make-not-available)
(define mix-maxvolume 128)
(define-sdl2 mix-audio (_fun _uint8* _uint8* _uint32 _int -> _void))
(define-sdl2 mix-audio-format (_fun _uint8* _uint8* _audio-format _uint32 _int -> _void))
(define-sdl2 queue-audio (_fun _audio-device-id _pointer _uint32 -> _int)
#:make-fail make-not-available)
(define-sdl2 dequeue-audio (_fun _audio-device-id _pointer _uint32 -> _uint32)
#:make-fail make-not-available)
(define-sdl2 get-queued-audio-size (_fun _audio-device-id -> _uint32)
#:make-fail make-not-available)
(define-sdl2 clear-queued-audio! (_fun _audio-device-id -> _void)
#:make-fail make-not-available)
(define-sdl2 lock-audio (_fun -> _void))
(define-sdl2 lock-audio-device (_fun _audio-device-id -> _void))
(define-sdl2 unlock-audio (_fun -> _void))
(define-sdl2 unlock-audio-device (_fun -> _void))
(define-sdl2 close-audio (_fun -> _void))
(define-sdl2 close-audio-device (_fun _audio-device-id -> _void))
;; SDL_clipboard.h
(define-sdl2 set-clipboard-text! (_fun _string -> _int))
(define-sdl2 get-clipboard-text (_fun -> _string))
(define-sdl2 has-clipboard-text (_fun -> _bool))
;; SDL_cpuinfo.h
(define cacheline-size 128)
(define-sdl2 get-cpu-count (_fun -> _int))
(define-sdl2 get-cpu-cache-line-size (_fun -> _int))
(define-sdl2 has-rdtsc (_fun -> _bool))
(define-sdl2 has-alti-vec (_fun -> _bool))
(define-sdl2 has-mmx (_fun -> _bool))
(define-sdl2 has-3d-now (_fun -> _bool))
(define-sdl2 has-sse (_fun -> _bool))
(define-sdl2 has-sse2 (_fun -> _bool))
(define-sdl2 has-sse3 (_fun -> _bool))
(define-sdl2 has-sse41 (_fun -> _bool))
(define-sdl2 has-sse42 (_fun -> _bool))
(define-sdl2 has-avx (_fun -> _bool)
#:make-fail make-not-available)
(define-sdl2 has-avx2 (_fun -> _bool)
#:make-fail make-not-available)
(define-sdl2 has-avx512-f (_fun -> _bool)
#:make-fail make-not-available)
(define-sdl2 has-neon (_fun -> _bool)
#:make-fail make-not-available)
(define-sdl2 get-system-ram (_fun -> _int)
#:make-fail make-not-available)
;; SDL_endian.h
(define lil-endian 1234)
(define big-endian 4321)
(define byteorder (if (system-big-endian?) big-endian lil-endian))
(begin-encourage-inline
(define/contract (swap16 x)
(contract/->
(and/c exact-integer? (lambda (n) (<= (integer-length n) 16)))
(and/c exact-integer? (lambda (n) (<= (integer-length n) 16))))
(bitwise-and
(bitwise-ior
(arithmetic-shift x 8)
(arithmetic-shift x -8))
#xffff))
(define/contract (swap32 x)
(contract/->
(and/c exact-integer? (lambda (n) (<= (integer-length n) 32)))
(and/c exact-integer? (lambda (n) (<= (integer-length n) 32))))
(bitwise-and
(bitwise-ior
(arithmetic-shift x 24)
(bitwise-and (arithmetic-shift x 8) #x00FF0000)
(bitwise-and (arithmetic-shift x -8) #x0000FF00)
(arithmetic-shift x -24))
#xffffffff))
(define/contract (swap64 x)
(contract/->
(and/c exact-integer? (lambda (n) (<= (integer-length n) 64)))
(and/c exact-integer? (lambda (n) (<= (integer-length n) 64))))
(bitwise-and
(bitwise-ior
(arithmetic-shift
(swap32 (bitwise-and x #xffffffff))
32)
(arithmetic-shift
(swap32 (bitwise-and (arithmetic-shift x -32) #xffffffff))
32))
#xffffffffffffffff))
(define/contract (swap-float x)
(contract/->
single-flonum? flonum?)
;; TODO : somehow convert _float to single-flonum
(define _swapper (_union _float _uint32))
(define s (cast (list x) (_list-struct _float) _swapper))
(union-set! s 1 (swap32 (union-ref s 1)))
(union-ref s 0)))
(define (swap-le16 x) (if (system-big-endian?) (swap16 x) x))
(define (swap-le32 x) (if (system-big-endian?) (swap32 x) x))
(define (swap-le64 x) (if (system-big-endian?) (swap64 x) x))
(define (swap-float-le x) (if (system-big-endian?) (swap-float x) x))
(define (swap-be16 x) (if (system-big-endian?) x (swap16 x)))
(define (swap-be32 x) (if (system-big-endian?) x (swap32 x)))
(define (swap-be64 x) (if (system-big-endian?) x (swap64 x)))
(define (swap-float-be x) (if (system-big-endian?) x (swap-float x)))
;; SDL_error.h
(define-sdl2-vararg set-error! SDL_SetError (_string) _int)
(define-sdl2 get-error (_fun -> _string))
(define-sdl2 clear-error! (_fun -> _void))
;; SDL_scancode.h
(define _scancode
(_enum
'(unknown = 0
a = 4
b = 5
c = 6
d = 7
e = 8
f = 9
g = 10
h = 11
i = 12
j = 13
k = 14
l = 15
m = 16
n = 17
o = 18
p = 19
q = 20
r = 21
s = 22
t = 23
u = 24
v = 25
w = 26
x = 27
y = 28
z = 29
n-1 = 30
n-2 = 31
n-3 = 32
n-4 = 33
n-5 = 34
n-6 = 35
n-7 = 36
n-8 = 37
n-9 = 38
n-0 = 39
return = 40
escape = 41
backspace = 42
tab = 43
space = 44
minus = 45
equals = 46
left-bracket = 47
right-bracket = 48
backslash = 49
non-us-hash = 50
semicolon = 51
apostrophe = 52
grave = 53
comma = 54
period = 55
slash = 56
caps-lock = 57
f1 = 58
f2 = 59
f3 = 60
f4 = 61
f5 = 62
f6 = 63
f7 = 64
f8 = 65
f9 = 66
f10 = 67
f11 = 68
f12 = 69
print-screen = 70
scroll-lock = 71
pause = 72
insert = 73
home = 74
page-up = 75
delete = 76
end = 77
page-down = 78
right = 79
left = 80
down = 81
up = 82
num-lock-clear = 83
kp-divide = 84
kp-multiply = 85
kp-minus = 86
kp-plus = 87
kp-enter = 88
kp-1 = 89
kp-2 = 90
kp-3 = 91
kp-4 = 92
kp-5 = 93
kp-6 = 94
kp-7 = 95
kp-8 = 96
kp-9 = 97
kp-0 = 98
kp-period = 99
non-us-backslash = 100
application = 101
power = 102
kp-equals = 103
f13 = 104
f14 = 105
f15 = 106
f16 = 107
f17 = 108
f18 = 109
f19 = 110
f20 = 111
f21 = 112
f22 = 113
f23 = 114
f24 = 115
execute = 116
help = 117
menu = 118
select = 119
stop = 120
again = 121
undo = 122
cut = 123
copy = 124
paste = 125
find = 126
mute = 127
volume-up = 128
volume-down = 129
kp-comma = 133
kp-equals-as400 = 134
international1 = 135
international2 = 136
international3 = 137
international4 = 138
international5 = 139
international6 = 140
international7 = 141
international8 = 142
international9 = 143
lang1 = 144
lang2 = 145
lang3 = 146
lang4 = 147
lang5 = 148
lang6 = 149
lang7 = 150
lang8 = 151
lang9 = 152
alt-erase = 153
sys-req = 154
cancel = 155
clear = 156
prior = 157
return2 = 158
separator = 159
out = 160
oper = 161
clear-again = 162
crsel = 163
exsel = 164
kp-00 = 176
kp-000 = 177
thousands-separator = 178