-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathsdl2_wrapper.cpp
2088 lines (1734 loc) · 62.9 KB
/
sdl2_wrapper.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
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <array>
#include <caml/alloc.h>
#include <caml/bigarray.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/threads.h>
#include "stb_image.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <SDL2/SDL_main.h>
#include <SDL2/SDL_config.h>
#include <SDL2/SDL_syswm.h>
#ifdef SDL_VIDEO_DRIVER_COCOA
#import <Cocoa/Cocoa.h>
#endif
#ifdef WIN32
#include <Windows.h>
#include <fcntl.h>
#include <io.h>
#endif
#include <glad/glad.h>
#define Val_none Val_int(0)
static value Val_some(value v) {
CAMLparam1(v);
CAMLlocal1(some);
some = caml_alloc(1, 0);
Store_field(some, 0, v);
CAMLreturn(some);
}
static value Val_ok(value v) {
CAMLparam1(v);
CAMLlocal1(some);
some = caml_alloc(1, 0);
Store_field(some, 0, v);
CAMLreturn(some);
}
static value Val_error(value v) {
CAMLparam1(v);
CAMLlocal1(some);
some = caml_alloc(1, 1);
Store_field(some, 0, v);
CAMLreturn(some);
}
extern "C" {
/* Create an OCaml value encapsulating the pointer p */
value resdl_wrapPointer(void *p) {
value v = caml_alloc(1, Abstract_tag);
*((void **) Data_abstract_val(v)) = p;
return v;
}
/* Extract the pointer encapsulated in the given OCaml value */
void *resdl_unwrapPointer(value v) {
return *((void **) Data_abstract_val(v));
}
CAMLprim value resdl_SDL_EnableScreenSaver() {
CAMLparam0();
SDL_EnableScreenSaver();
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_DisableScreenSaver() {
CAMLparam0();
SDL_DisableScreenSaver();
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_IsScreenSaverEnabled() {
CAMLparam0();
int result = SDL_IsScreenSaverEnabled() == SDL_TRUE;
CAMLreturn(Val_int(result));
}
CAMLprim value resdl_SDL_SetMainReady() {
SDL_SetMainReady();
return Val_unit;
}
CAMLprim value resdl_SDL_DestroyWindow(value vWin) {
CAMLparam1(vWin);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_DestroyWindow(win);
CAMLreturn(Val_unit);
}
SDL_HitTestResult resdl_hit_test(SDL_Window *win, const SDL_Point *area,
void *data) {
CAMLparam0();
CAMLlocal2(vWin, vRet);
static const value *hitTestCallback = NULL;
if (hitTestCallback == NULL) {
hitTestCallback = caml_named_value("__sdl2_caml_hittest__");
}
vWin = resdl_wrapPointer(win);
vRet = caml_callback3(*hitTestCallback, vWin, Val_int(area->x),
Val_int(area->y));
SDL_HitTestResult result;
switch (Int_val(vRet)) {
case 0:
result = SDL_HITTEST_NORMAL;
break;
case 1:
result = SDL_HITTEST_DRAGGABLE;
break;
case 2:
result = SDL_HITTEST_RESIZE_TOPLEFT;
break;
case 3:
result = SDL_HITTEST_RESIZE_TOP;
break;
case 4:
result = SDL_HITTEST_RESIZE_TOPRIGHT;
break;
case 5:
result = SDL_HITTEST_RESIZE_RIGHT;
break;
case 6:
result = SDL_HITTEST_RESIZE_BOTTOMRIGHT;
break;
case 7:
result = SDL_HITTEST_RESIZE_BOTTOM;
break;
case 8:
result = SDL_HITTEST_RESIZE_BOTTOMLEFT;
break;
case 9:
result = SDL_HITTEST_RESIZE_LEFT;
break;
default:
result = SDL_HITTEST_NORMAL;
break;
}
CAMLreturnT(SDL_HitTestResult, result);
};
CAMLprim value resdl_SDL_EnableHitTest(value vWin) {
CAMLparam1(vWin);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SetWindowHitTest(win, resdl_hit_test, NULL);
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_DisableHitTest(value vWin) {
CAMLparam1(vWin);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SetWindowHitTest(win, NULL, NULL);
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_Delay(value delay) {
CAMLparam1(delay);
int d = Int_val(delay);
SDL_Delay(d);
CAMLreturn(Val_unit);
}
#ifdef WIN32
typedef enum PROCESS_DPI_AWARENESS {
PROCESS_DPI_UNAWARE = 0,
PROCESS_SYSTEM_DPI_AWARE = 1,
PROCESS_PER_MONITOR_DPI_AWARE = 2
} PROCESS_DPI_AWARENESS;
HWND getHWNDFromSDLWindow(SDL_Window *win) {
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(win, &wmInfo);
return wmInfo.info.win.window;
};
#endif
CAMLprim value resdl_SDL_GetPlatform() {
CAMLparam0();
CAMLlocal1(ret);
const char *str = SDL_GetPlatform();
ret = caml_copy_string(str);
CAMLreturn(ret);
}
CAMLprim value resdl_SDL_GetVersion() {
CAMLparam0();
CAMLlocal1(ret);
const char *str;
#ifdef __APPLE__
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];
str = [version UTF8String];
#else
str = "??.??.??";
#endif
ret = caml_copy_string(str);
CAMLreturn(ret);
}
CAMLprim value resdl_SDL_GetNativeWindow(value vWin) {
CAMLparam1(vWin);
CAMLlocal1(vNativeWindow);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(win, &wmInfo);
void *pNativeWindow = NULL;
switch (wmInfo.subsystem) {
#ifdef SDL_VIDEO_DRIVER_WINDOWS
case SDL_SYSWM_WINDOWS:
pNativeWindow = (void *)wmInfo.info.win.window;
break;
#endif
#ifdef SDL_VIDEO_DRIVER_UIKIT
case SDL_SYSWM_UIKIT:
pNativeWindow = (void *)wmInfo.info.uikit.window;
break;
#endif
#ifdef SDL_VIDEO_DRIVER_COCOA
case SDL_SYSWM_COCOA:
pNativeWindow = (void *)wmInfo.info.cocoa.window;
break;
#endif
#ifdef SDL_VIDEO_DRIVER_ANDROID
case SDL_SYSWM_ANDROID:
pNativeWindow = (void *)wmInfo.info.android.window;
break;
#endif
#ifdef SDL_VIDEO_DRIVER_X11
case SDL_SYSWM_X11:
pNativeWindow = (void *)wmInfo.info.x11.window;
break;
#endif
#ifdef SDL_VIDEO_DRIVER_WAYLAND
case SDL_SYSWM_WAYLAND:
pNativeWindow = (void *)wmInfo.info.wl.surface;
break;
#endif
default:
break;
}
vNativeWindow = resdl_wrapPointer(pNativeWindow);
CAMLreturn(vNativeWindow);
};
#ifdef WIN32
// This method is calling after attach / alloc console
// to wire up the new stdin/stdout/stderr.
// See further details (thanks @dra27 for the help!)
// - https://github.com/ocaml/ocaml/issues/9252
void resdl_Win32AttachStdIO() {
int fd_in = _open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE),
_O_RDONLY | _O_BINARY);
int fd_out = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE),
_O_WRONLY | _O_BINARY);
int fd_err = _open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE),
_O_WRONLY | _O_BINARY);
if (fd_in) {
dup2(fd_in, 0);
close(fd_in);
SetStdHandle(STD_INPUT_HANDLE, (HANDLE)_get_osfhandle(0));
}
if (fd_out) {
dup2(fd_out, 1);
close(fd_out);
SetStdHandle(STD_OUTPUT_HANDLE, (HANDLE)_get_osfhandle(1));
}
if (fd_err) {
dup2(fd_err, 2);
close(fd_err);
SetStdHandle(STD_ERROR_HANDLE, (HANDLE)_get_osfhandle(2));
}
*stdin = *(fdopen(0, "rb"));
*stdout = *(fdopen(1, "wb"));
*stderr = *(fdopen(2, "wb"));
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
#endif
CAMLprim value resdl_SDL_WinAttachConsole() {
CAMLparam0();
int ret = 0;
#ifdef WIN32
// Only attach if we don't already have a stdout handle
if (GetStdHandle(STD_OUTPUT_HANDLE) == NULL) {
ret = AttachConsole(ATTACH_PARENT_PROCESS);
if (ret) {
resdl_Win32AttachStdIO();
}
} else {
// There's already a stdout handle available,
// so return success
ret = 1;
}
#endif
CAMLreturn(Val_int(ret));
}
CAMLprim value resdl_SDL_WinAllocConsole() {
CAMLparam0();
int ret = 0;
#ifdef WIN32
ret = AllocConsole();
if (ret) {
resdl_Win32AttachStdIO();
}
#endif
CAMLreturn(Val_int(ret));
}
CAMLprim value resdl_SDL_SetMacTitlebarHidden(value vWin) {
CAMLparam1(vWin);
#ifdef SDL_VIDEO_DRIVER_COCOA
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(win, &wmInfo);
NSWindow *nWindow = wmInfo.info.cocoa.window;
[nWindow
setStyleMask:[nWindow styleMask] | NSWindowStyleMaskDocModalWindow];
#endif
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_SetMacTitlebarTransparent(value vWin) {
CAMLparam1(vWin);
#ifdef SDL_VIDEO_DRIVER_COCOA
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(win, &wmInfo);
NSWindow *nWindow = wmInfo.info.cocoa.window;
[nWindow
setStyleMask:[nWindow styleMask] | NSWindowStyleMaskFullSizeContentView];
[nWindow setTitlebarAppearsTransparent:YES];
nWindow.titleVisibility = NSWindowTitleHidden;
#endif
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_GetMacTitlebarHeight(value vWin) {
CAMLparam1(vWin);
double titlebarHeight = 0.0;
#ifdef SDL_VIDEO_DRIVER_COCOA
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(win, &wmInfo);
NSWindow *nWindow = wmInfo.info.cocoa.window;
// Sourced from: https://stackoverflow.com/a/59323932/12701512
CGFloat windowFrameHeight = CGRectGetHeight([nWindow contentView].frame);
CGFloat contentLayoutRectHeight = CGRectGetHeight([nWindow contentLayoutRect]);
titlebarHeight = (double)(windowFrameHeight - contentLayoutRectHeight);
#endif
CAMLreturn(caml_copy_double(titlebarHeight));
}
CAMLprim value resdl_SDL_SetMacBackgroundColor(value vWin, value r, value g,
value b, value a) {
CAMLparam5(vWin, r, g, b, a);
#ifdef SDL_VIDEO_DRIVER_COCOA
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(win, &wmInfo);
double red = Double_val(r);
double green = Double_val(g);
double blue = Double_val(b);
double alpha = Double_val(a);
NSWindow *nWindow = wmInfo.info.cocoa.window;
NSColor *rgb =
[NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
[nWindow setBackgroundColor:rgb];
#endif
CAMLreturn(Val_unit);
}
CAMLprim value resdl_SDL_SetWin32ProcessDPIAware(value vWin) {
CAMLparam1(vWin);
#ifdef WIN32
void *userDLL;
BOOL(WINAPI * SetProcessDPIAware)(void); // Vista and later
void *shcoreDLL;
HRESULT(WINAPI * SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS dpiAwareness);
userDLL = SDL_LoadObject("USER32.DLL");
if (userDLL) {
SetProcessDPIAware =
(BOOL(WINAPI *)(void))SDL_LoadFunction(userDLL, "SetProcessDPIAware");
}
shcoreDLL = SDL_LoadObject("SHCORE.DLL");
if (shcoreDLL) {
SetProcessDpiAwareness =
(HRESULT(WINAPI *)(PROCESS_DPI_AWARENESS))SDL_LoadFunction(
shcoreDLL, "SetProcessDpiAwareness");
}
if (SetProcessDpiAwareness) {
// Try Windows 8.1+ version
HRESULT result = SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
} else if (SetProcessDPIAware) {
// Try Vista - Windows 8 version.
// This has a constant scale factor for all monitors.
BOOL success = SetProcessDPIAware();
}
#endif
CAMLreturn(Val_unit);
};
CAMLprim value resdl_SDL_GetWin32ScaleFactor(value vWin) {
CAMLparam1(vWin);
#ifdef WIN32
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
HWND hwnd = getHWNDFromSDLWindow(win);
HMONITOR hmon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
void *shcoreDLL;
HRESULT(WINAPI * GetScaleFactorForMonitor)(HMONITOR hmon, int *pScale);
shcoreDLL = SDL_LoadObject("SHCORE.DLL");
if (shcoreDLL) {
GetScaleFactorForMonitor =
(HRESULT(WINAPI *)(HMONITOR, int *))SDL_LoadFunction(
shcoreDLL, "GetScaleFactorForMonitor");
}
if (GetScaleFactorForMonitor) {
int pScale;
GetScaleFactorForMonitor(hmon, &pScale);
CAMLreturn(caml_copy_double(pScale / 100.0));
} else {
CAMLreturn(caml_copy_double(1.0));
}
#else
CAMLreturn(caml_copy_double(1.0));
#endif
};
CAMLprim value resdl_SDL_GetDisplayDPI(value vDisplay) {
CAMLparam1(vDisplay);
CAMLlocal1(ret);
int displayIndex = Int_val(vDisplay);
float ddpi, hdpi, vdpi;
SDL_GetDisplayDPI(displayIndex, &ddpi, &hdpi, &vdpi);
ret = caml_alloc(3, 0);
Store_double_field(ret, 0, ddpi);
Store_double_field(ret, 1, hdpi);
Store_double_field(ret, 2, vdpi);
CAMLreturn(ret);
};
CAMLprim value resdl_SDL_GetCurrentDisplayMode(value vDisplay) {
CAMLparam1(vDisplay);
CAMLlocal1(ret);
int displayIndex = Int_val(vDisplay);
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(displayIndex, ¤t);
ret = caml_alloc(4, 0);
Store_field(ret, 0, Val_int(current.format));
Store_field(ret, 1, Val_int(current.w));
Store_field(ret, 2, Val_int(current.h));
Store_field(ret, 3, Val_int(current.refresh_rate));
CAMLreturn(ret);
};
CAMLprim value resdl_SDL_GetDesktopDisplayMode(value vDisplay) {
CAMLparam1(vDisplay);
CAMLlocal1(ret);
int displayIndex = Int_val(vDisplay);
SDL_DisplayMode current;
SDL_GetDesktopDisplayMode(displayIndex, ¤t);
ret = caml_alloc(4, 0);
Store_field(ret, 0, Val_int(current.format));
Store_field(ret, 1, Val_int(current.w));
Store_field(ret, 2, Val_int(current.h));
Store_field(ret, 3, Val_int(current.refresh_rate));
CAMLreturn(ret);
};
CAMLprim value resdl_SDL_GetDisplayBounds(value vDisplay) {
CAMLparam1(vDisplay);
CAMLlocal1(rect);
int displayIndex = Int_val(vDisplay);
SDL_Rect sdlRect;
SDL_GetDisplayBounds(displayIndex, &sdlRect);
rect = caml_alloc(4, 0);
Store_field(rect, 0, Val_int(sdlRect.x));
Store_field(rect, 1, Val_int(sdlRect.y));
Store_field(rect, 2, Val_int(sdlRect.w));
Store_field(rect, 3, Val_int(sdlRect.h));
CAMLreturn(rect);
};
CAMLprim value resdl_SDL_GetDisplayName(value vDisplay) {
CAMLparam1(vDisplay);
CAMLlocal1(retStr);
int displayIndex = Int_val(vDisplay);
const char *szDisplayName = SDL_GetDisplayName(displayIndex);
if (!szDisplayName) {
szDisplayName = "(Null)";
};
retStr = caml_copy_string(szDisplayName);
CAMLreturn(retStr);
};
CAMLprim value resdl_SDL_GetDisplayUsableBounds(value vDisplay) {
CAMLparam1(vDisplay);
CAMLlocal1(rect);
int displayIndex = Int_val(vDisplay);
SDL_Rect sdlRect;
SDL_GetDisplayUsableBounds(displayIndex, &sdlRect);
rect = caml_alloc(4, 0);
Store_field(rect, 0, Val_int(sdlRect.x));
Store_field(rect, 1, Val_int(sdlRect.y));
Store_field(rect, 2, Val_int(sdlRect.w));
Store_field(rect, 3, Val_int(sdlRect.h));
CAMLreturn(rect);
};
CAMLprim value resdl_SDL_GetNumVideoDisplays(value vUnit) {
CAMLparam0();
int num = SDL_GetNumVideoDisplays();
CAMLreturn(Val_int(num));
};
CAMLprim value resdl_SDL_GetPixelFormatName(value vPixelFormat) {
CAMLparam1(vPixelFormat);
CAMLlocal1(ret);
Uint32 format = Int_val(vPixelFormat);
const char *szPixelFormatName = SDL_GetPixelFormatName(format);
if (!szPixelFormatName) {
ret = caml_copy_string("(null)");
} else {
ret = caml_copy_string(szPixelFormatName);
}
CAMLreturn(ret);
}
CAMLprim value resdl_SDL_GetWindowDisplayIndex(value vWin) {
CAMLparam1(vWin);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
int idx = SDL_GetWindowDisplayIndex(win);
CAMLreturn(Val_int(idx));
};
CAMLprim value resdl_SDL_GetWindowPixelFormat(value vWin) {
CAMLparam1(vWin);
SDL_Window *pWin = (SDL_Window *)resdl_unwrapPointer(vWin);
Uint32 format = SDL_GetWindowPixelFormat(pWin);
CAMLreturn(Val_int(format));
};
CAMLprim value resdl_SDL_GL_SetSwapInterval(value vInterval) {
int interval = Int_val(vInterval);
SDL_GL_SetSwapInterval(interval);
return Val_unit;
};
CAMLprim value resdl_SDL_GL_Setup(value vWin) {
CAMLparam1(vWin);
CAMLlocal1(vCtx);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_GLContext ctx = SDL_GL_CreateContext(win);
if (!ctx) {
SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "SDL_GL_Setup failed: %s\n",
SDL_GetError());
}
vCtx = resdl_wrapPointer(ctx);
CAMLreturn(vCtx);
}
typedef const GLubyte *(*glGetStringFunc)(GLenum);
CAMLprim value resdl_SDL_GL_GetString(value vStr) {
CAMLparam1(vStr);
CAMLlocal1(ret);
GLenum name = GL_VENDOR;
switch (Int_val(vStr)) {
case 0:
name = GL_VENDOR;
break;
case 1:
name = GL_RENDERER;
break;
case 2:
name = GL_VERSION;
break;
case 3:
name = GL_SHADING_LANGUAGE_VERSION;
break;
default:
break;
}
glGetStringFunc glGetString =
(glGetStringFunc)(SDL_GL_GetProcAddress("glGetString"));
if (!glGetString) {
ret = caml_copy_string("Unable to get OpenGL proc address for glGetString");
} else {
const char *sz = (const char *)((void *)glGetString(name));
if (!sz) {
ret = caml_copy_string("(null)");
} else {
ret = caml_copy_string(sz);
}
}
CAMLreturn(ret);
}
typedef void (*glGetIntegervFunc)(GLenum, GLint*);
CAMLprim value resdl_SDL_GL_GetFramebufferBinding(value vInt) {
CAMLparam1(vInt);
GLenum name = GL_FRAMEBUFFER_BINDING;
switch (Int_val(vInt)) {
case 0:
name = GL_FRAMEBUFFER_BINDING;
break;
default:
break;
}
GLint ret = -1;
glGetIntegervFunc glGetIntegerv =
(glGetIntegervFunc)(SDL_GL_GetProcAddress("glGetIntegerv"));
if (glGetIntegerv) {
glGetIntegerv(name, &ret);
}
CAMLreturn(Val_int(ret));
}
CAMLprim value resdl_SDL_GL_MakeCurrent(value vWin, value vContext) {
CAMLparam2(vWin, vContext);
SDL_Window *win = (SDL_Window *)resdl_unwrapPointer(vWin);
SDL_GLContext ctx = (SDL_GLContext)resdl_unwrapPointer(vContext);
SDL_GL_MakeCurrent(win, ctx);
CAMLreturn(Val_unit);
};
CAMLprim value resdl_SDL_GetClipboardText(value vUnit) {
CAMLparam0();
CAMLlocal1(ret);
char *clip = SDL_GetClipboardText();
if (clip == NULL) {
ret = Val_none;
} else {
ret = Val_some(caml_copy_string(clip));
}
CAMLreturn(ret);
}
CAMLprim value resdl_SDL_HasClipboardText(value vUnit) {
CAMLparam0();
SDL_bool res = SDL_HasClipboardText();
CAMLreturn(res == SDL_TRUE ? Val_bool(1) : Val_bool(0));
}
CAMLprim value resdl_SDL_SetClipboardText(value vText) {
CAMLparam1(vText);
SDL_SetClipboardText(String_val(vText));
CAMLreturn(Val_unit);
}
CAMLprim value Val_SDL_WindowEvent(int type, int windowID) {
CAMLparam0();
CAMLlocal2(ret, v);
v = caml_alloc(1, 0);
Store_field(v, 0, Val_int(windowID));
ret = caml_alloc(1, type);
Store_field(ret, 0, v);
CAMLreturn(ret);
}
CAMLprim value Val_SDL_WindowEventWithData(int type, int windowID, int data1,
int data2) {
CAMLparam0();
CAMLlocal2(ret, v);
v = caml_alloc(3, 0);
Store_field(v, 0, Val_int(windowID));
Store_field(v, 1, Val_int(data1));
Store_field(v, 2, Val_int(data2));
ret = caml_alloc(1, type);
Store_field(ret, 0, v);
CAMLreturn(ret);
}
void getNonFocusedMousePosition(SDL_Window *window, int *localMouseX, int *localMouseY) {
int globalMouseX, globalMouseY;
SDL_GetGlobalMouseState(&globalMouseX, &globalMouseY);
int windowX, windowY;
SDL_GetWindowPosition(window, &windowX, &windowY);
*localMouseX = globalMouseX - windowX;
*localMouseY = globalMouseY - windowY;
}
CAMLprim value Val_SDL_Event(SDL_Event *event) {
CAMLparam0();
CAMLlocal2(v, vInner);
int tag, mouseButton, mousePosX, mousePosY;
switch (event->type) {
case SDL_QUIT:
v = Val_int(0);
break;
case SDL_MOUSEMOTION:
vInner = caml_alloc(3, 0);
Store_field(vInner, 0, Val_int(event->motion.windowID));
Store_field(vInner, 1, Val_int(event->motion.x));
Store_field(vInner, 2, Val_int(event->motion.y));
v = caml_alloc(1, 0);
Store_field(v, 0, vInner);
break;
case SDL_MOUSEWHEEL:
v = caml_alloc(1, 1);
vInner = caml_alloc(4, 0);
Store_field(vInner, 0, Val_int(event->wheel.windowID));
Store_field(vInner, 1, Val_int(event->wheel.x));
Store_field(vInner, 2, Val_int(event->wheel.y));
Store_field(vInner, 3,
Val_bool(event->wheel.direction == SDL_MOUSEWHEEL_FLIPPED));
Store_field(v, 0, vInner);
break;
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
if (event->type == SDL_MOUSEBUTTONDOWN)
v = caml_alloc(1, 2);
else
v = caml_alloc(1, 3);
mouseButton = 0;
switch (event->button.button) {
case SDL_BUTTON_LEFT:
mouseButton = 0;
break;
case SDL_BUTTON_MIDDLE:
mouseButton = 1;
break;
case SDL_BUTTON_RIGHT:
mouseButton = 2;
break;
case SDL_BUTTON_X1:
mouseButton = 3;
break;
case SDL_BUTTON_X2:
mouseButton = 4;
break;
default:
mouseButton = 0;
break;
}
vInner = caml_alloc(5, 0);
Store_field(vInner, 0, Val_int(event->button.windowID));
Store_field(vInner, 1, Val_int(mouseButton));
Store_field(vInner, 2, Val_int(event->button.clicks));
Store_field(vInner, 3, Val_int(event->button.x));
Store_field(vInner, 4, Val_int(event->button.y));
Store_field(v, 0, vInner);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
v = caml_alloc(1, event->type == SDL_KEYDOWN ? 4 : 5);
vInner = caml_alloc(5, 0);
Store_field(vInner, 0, Val_int(event->key.windowID));
Store_field(vInner, 1, Val_bool(event->key.repeat));
Store_field(vInner, 2, Val_int(event->key.keysym.mod));
Store_field(vInner, 3, Val_int(event->key.keysym.scancode));
Store_field(vInner, 4, Val_int(event->key.keysym.sym));
Store_field(v, 0, vInner);
break;
case SDL_TEXTINPUT:
v = caml_alloc(1, 6);
vInner = caml_alloc(2, 0);
Store_field(vInner, 0, Val_int(event->text.windowID));
Store_field(vInner, 1, caml_copy_string(event->text.text));
Store_field(v, 0, vInner);
break;
case SDL_TEXTEDITING:
v = caml_alloc(1, 7);
vInner = caml_alloc(4, 0);
Store_field(vInner, 0, Val_int(event->edit.windowID));
Store_field(vInner, 1, caml_copy_string(event->edit.text));
Store_field(vInner, 2, Val_int(event->edit.start));
Store_field(vInner, 3, Val_int(event->edit.length));
Store_field(v, 0, vInner);
break;
case SDL_PANEVENT:
v = caml_alloc(1, 24);
vInner = caml_alloc(9, 0);
Store_field(vInner, 0, Val_int(event->window.windowID));
Store_field(vInner, 1, Val_int(event->pan.x));
Store_field(vInner, 2, Val_int(event->pan.y));
Store_field(vInner, 3, Val_bool(event->pan.contains_x));
Store_field(vInner, 4, Val_bool(event->pan.contains_y));
Store_field(vInner, 5, Val_bool(event->pan.fling));
Store_field(vInner, 6, Val_bool(event->pan.interrupt));
// verify this is the correct way of representing a ref to some WheelType.t
Store_field(vInner, 7, Val_int(event->pan.source_type));
Store_field(vInner, 8, Val_int(event->pan.timestamp));
Store_field(v, 0, vInner);
break;
case SDL_DROPTEXT:
v = caml_alloc(1, 25);
vInner = caml_alloc(5, 0);
getNonFocusedMousePosition(SDL_GetWindowFromID(event->drop.windowID), &mousePosX, &mousePosY);
Store_field(vInner, 0, Val_int(event->drop.windowID));
Store_field(vInner, 1, caml_copy_string(event->drop.file));
Store_field(vInner, 2, Val_int(event->drop.timestamp));
Store_field(vInner, 3, Val_int(mousePosX));
Store_field(vInner, 4, Val_int(mousePosY));
Store_field(v, 0, vInner);
SDL_free(event->drop.file);
break;
case SDL_DROPFILE:
v = caml_alloc(1, 26);
vInner = caml_alloc(5, 0);
getNonFocusedMousePosition(SDL_GetWindowFromID(event->drop.windowID), &mousePosX, &mousePosY);
Store_field(vInner, 0, Val_int(event->drop.windowID));
Store_field(vInner, 1, caml_copy_string(event->drop.file));
Store_field(vInner, 2, Val_int(event->drop.timestamp));
Store_field(vInner, 3, Val_int(mousePosX));
Store_field(vInner, 4, Val_int(mousePosY));
Store_field(v, 0, vInner);
SDL_free(event->drop.file);
break;
case SDL_DROPBEGIN:
v = caml_alloc(1, 27);
vInner = caml_alloc(4, 0);
getNonFocusedMousePosition(SDL_GetWindowFromID(event->drop.windowID), &mousePosX, &mousePosY);
Store_field(vInner, 0, Val_int(event->drop.windowID));
Store_field(vInner, 1, Val_int(event->drop.timestamp));
Store_field(vInner, 2, Val_int(mousePosX));
Store_field(vInner, 3, Val_int(mousePosY));
Store_field(v, 0, vInner);
SDL_free(event->drop.file);
break;
case SDL_DROPCOMPLETE:
v = caml_alloc(1, 28);
vInner = caml_alloc(4, 0);
getNonFocusedMousePosition(SDL_GetWindowFromID(event->drop.windowID), &mousePosX, &mousePosY);
Store_field(vInner, 0, Val_int(event->drop.windowID));
Store_field(vInner, 1, Val_int(event->drop.timestamp));
Store_field(vInner, 2, Val_int(mousePosX));
Store_field(vInner, 3, Val_int(mousePosY));
Store_field(v, 0, vInner);
SDL_free(event->drop.file);
break;
case SDL_KEYMAPCHANGED:
v = Val_int(2);
break;
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_SHOWN:
v = Val_SDL_WindowEvent(8, event->window.windowID);
break;
case SDL_WINDOWEVENT_HIDDEN:
v = Val_SDL_WindowEvent(9, event->window.windowID);
break;
case SDL_WINDOWEVENT_EXPOSED:
v = Val_SDL_WindowEvent(10, event->window.windowID);
break;
case SDL_WINDOWEVENT_MOVED:
v = Val_SDL_WindowEventWithData(11, event->window.windowID,
event->window.data1, event->window.data2);
break;
case SDL_WINDOWEVENT_RESIZED:
v = Val_SDL_WindowEventWithData(12, event->window.windowID,
event->window.data1, event->window.data2);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
v = Val_SDL_WindowEventWithData(13, event->window.windowID,
event->window.data1, event->window.data2);
break;
case SDL_WINDOWEVENT_MINIMIZED:
v = Val_SDL_WindowEvent(14, event->window.windowID);
break;
case SDL_WINDOWEVENT_MAXIMIZED:
v = Val_SDL_WindowEvent(15, event->window.windowID);
break;
case SDL_WINDOWEVENT_FULLSCREEN:
v = Val_SDL_WindowEvent(29, event->window.windowID);
break;
case SDL_WINDOWEVENT_RESTORED:
v = Val_SDL_WindowEvent(16, event->window.windowID);
break;
case SDL_WINDOWEVENT_ENTER:
v = Val_SDL_WindowEvent(17, event->window.windowID);
break;
case SDL_WINDOWEVENT_LEAVE:
v = Val_SDL_WindowEvent(18, event->window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
v = Val_SDL_WindowEvent(19, event->window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_LOST: