about summary refs log tree commit diff stats
path: root/src/wrapped32/wrappedsdl1.c
blob: 8e437659f23fa2847c74033130919973ab738d6c (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <dlfcn.h>

#include "wrappedlibs.h"

#include "wrapper32.h"
#include "debug.h"
#include "bridge.h"
#include "librarian/library_private.h"
#include "x64emu.h"
#include "callback.h"
#include "librarian.h"
#include "emu/x64emu_private.h"
#include "box32context.h"
#include "sdl1rwops.h"
#include "gltools.h"

#include "x64trace.h"
#include "threads.h"

#include "sdl1align32.h"

extern const char* sdl1Name;
#define LIBNAME sdl1

static int sdl_Yes() {return 1;}
static int sdl_No() {return 0;}
EXPORT int my32_SDL_Has3DNow() __attribute__((alias("sdl_No")));
EXPORT int my32_SDL_Has3DNowExt() __attribute__((alias("sdl_No")));
EXPORT int my32_SDL_HasAltiVec() __attribute__((alias("sdl_No")));
EXPORT int my32_SDL_HasMMX() __attribute__((alias("sdl_Yes")));
EXPORT int my32_SDL_HasMMXExt() __attribute__((alias("sdl_Yes")));
EXPORT int my32_SDL_HasRDTSC() __attribute__((alias("sdl_Yes")));
EXPORT int my32_SDL_HasSSE() __attribute__((alias("sdl_Yes")));
EXPORT int my32_SDL_HasSSE2() __attribute__((alias("sdl_Yes")));

typedef struct {
  int32_t freq;
  uint16_t format;
  uint8_t channels;
  uint8_t silence;
  uint16_t samples;
  uint32_t size;
  void (*callback)(void *userdata, uint8_t *stream, int32_t len);
  void *userdata;
} SDL_AudioSpec;

typedef struct {    // removed packed attribute
  int32_t freq;
  uint16_t format;
  uint8_t channels;
  uint8_t silence;
  uint16_t samples;
  uint32_t size;
  ptr_t callback; //void (*callback)(void *userdata, uint8_t *stream, int32_t len);
  ptr_t userdata; //void *userdata;
} SDL_AudioSpec32;

EXPORT void my32_SDL_Quit();

#define ADDED_FUNCTIONS() \
    GO(SDL_AllocRW, sdl1_allocrw) \
    GO(SDL_FreeRW, sdl1_freerw)

#define ADDED_FINI() \
    my32_SDL_Quit();

#include "generated/wrappedsdl1types32.h"

#include "wrappercallback32.h"

// event filter. Needs to be global, but there is only one, so that's should be fine
static x64emu_t        *sdl1_evtfilter = NULL;
static void*           sdl1_evtfnc = NULL;
static int             sdl1_evtautofree = 0;
static int             sdl1_evtinside = 0;

#define SUPER() \
GO(0)   \
GO(1)   \
GO(2)   \
GO(3)   \
GO(4)

// AudioCallback ...
#define GO(A)   \
static uintptr_t my32_AudioCallback_fct_##A = 0;                                      \
static void my32_AudioCallback_##A(void *userdata, uint8_t *stream, int32_t len)      \
{                                                                                   \
    RunFunctionFmt(my32_AudioCallback_fct_##A, "ppi", userdata, stream, len);   \
}
SUPER()
#undef GO
static void* find_AudioCallback_Fct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my32_AudioCallback_fct_##A == (uintptr_t)fct) return my32_AudioCallback_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my32_AudioCallback_fct_##A == 0) {my32_AudioCallback_fct_##A = (uintptr_t)fct; return my32_AudioCallback_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for SDL1 AudioCallback callback\n");
    return NULL;
}
// TimerCallback ...
#define GO(A)   \
static uintptr_t my32_TimerCallback_fct_##A = 0;                                                  \
static uint32_t my32_TimerCallback_##A(uint32_t interval, void *userdata)                         \
{                                                                                               \
    return (uint32_t)RunFunctionFmt(my32_TimerCallback_fct_##A, "up", interval, userdata);  \
}
SUPER()
#undef GO
static void* find_TimerCallback_Fct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my32_TimerCallback_fct_##A == (uintptr_t)fct) return my32_TimerCallback_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my32_TimerCallback_fct_##A == 0) {my32_TimerCallback_fct_##A = (uintptr_t)fct; return my32_TimerCallback_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for SDL1 TimerCallback callback\n");
    return NULL;
}
// EvtFilter ...
#define GO(A)   \
static uintptr_t my32_EvtFilter_fct_##A = 0;                      \
static int my32_EvtFilter_##A(void* p)                            \
{                                                               \
    return RunFunctionFmt(my32_EvtFilter_fct_##A, "p", p); \
}
SUPER()
#undef GO
static void* find_EvtFilter_Fct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my32_EvtFilter_fct_##A == (uintptr_t)fct) return my32_EvtFilter_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my32_EvtFilter_fct_##A == 0) {my32_EvtFilter_fct_##A = (uintptr_t)fct; return my32_EvtFilter_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for SDL1 EvtFilter callback\n");
    return NULL;
}
static void* reverse_EvtFilterFct(void* fct)
{
    if(!fct) return fct;
    if(CheckBridged(my_lib->w.bridge, fct))
        return (void*)CheckBridged(my_lib->w.bridge, fct);
    #define GO(A) if(my32_EvtFilter_##A == fct) return (void*)my32_EvtFilter_fct_##A;
    SUPER()
    #undef GO
    return (void*)AddBridge(my_lib->w.bridge, iFp_32, fct, 0, NULL);
}
#undef SUPER

// TODO: track the memory for those callback
EXPORT int my32_SDL_OpenAudio(x64emu_t* emu, void* d, void* o)
{
    SDL_AudioSpec desired = {0};
    SDL_AudioSpec output = {0};
    SDL_AudioSpec32 *d_ = d;
    desired.channels = d_->channels;
    desired.format = d_->format;
    desired.freq = d_->freq;
    desired.samples = d_->samples;
    desired.silence = d_->silence;
    desired.size = d_->size;
    desired.userdata = from_ptrv(d_->userdata);
    desired.callback = find_AudioCallback_Fct(from_ptrv(d_->callback));
    int ret = my->SDL_OpenAudio(&desired, &output);
    if (ret!=0) {
        return ret;
    }
    // put back stuff in place?
    if (o) {
        SDL_AudioSpec32* o_ = o;
        o_->channels = output.channels;
        o_->format = output.format;
        o_->freq = output.freq;
        o_->samples = output.samples;
        o_->silence = output.silence;
        o_->size = output.size;
        o_->userdata = o_->callback = 0;
    }

    return ret;
}

EXPORT void *my32_SDL_LoadBMP_RW(x64emu_t* emu, void* a, int b)
{
    inplace_SDL_RWops_to_64(a);
    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
    void* r = my->SDL_LoadBMP_RW(rw, b);
    if(b==0) {
        RWNativeEnd(rw);
        inplace_SDL_RWops_to_32(a);
    }
    inplace_SDL_Surface_to_32(r);
    return r;
}
//EXPORT int32_t my32_SDL_SaveBMP_RW(x64emu_t* emu, void* a, void* b, int c)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    int32_t r = my->SDL_SaveBMP_RW(rw, b, c);
//    if(c==0)
//        RWNativeEnd(rw);
//    return r;
//}
//EXPORT void *my32_SDL_LoadWAV_RW(x64emu_t* emu, void* a, int b, void* c, void* d, void* e)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    void* r = my->SDL_LoadWAV_RW(rw, b, c, d, e);
//    if(b==0)
//        RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_ReadBE16(x64emu_t* emu, void* a)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_ReadBE16(rw);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_ReadBE32(x64emu_t* emu, void* a)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_ReadBE32(rw);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint64_t my32_SDL_ReadBE64(x64emu_t* emu, void* a)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint64_t r = my->SDL_ReadBE64(rw);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_ReadLE16(x64emu_t* emu, void* a)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_ReadLE16(rw);
//    RWNativeEnd(rw);
//    return r;
//}
EXPORT uint32_t my32_SDL_ReadLE32(x64emu_t* emu, void* a)
{
    inplace_SDL_RWops_to_64(a);
    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
    uint32_t r = my->SDL_ReadLE32(rw);
    RWNativeEnd(rw);
    inplace_SDL_RWops_to_32(a);
    return r;
}
//EXPORT uint64_t my32_SDL_ReadLE64(x64emu_t* emu, void* a)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint64_t r = my->SDL_ReadLE64(rw);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_WriteBE16(x64emu_t* emu, void* a, uint16_t v)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_WriteBE16(rw, v);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_WriteBE32(x64emu_t* emu, void* a, uint32_t v)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_WriteBE32(rw, v);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_WriteBE64(x64emu_t* emu, void* a, uint64_t v)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_WriteBE64(rw, v);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_WriteLE16(x64emu_t* emu, void* a, uint16_t v)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_WriteLE16(rw, v);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_WriteLE32(x64emu_t* emu, void* a, uint32_t v)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_WriteLE32(rw, v);
//    RWNativeEnd(rw);
//    return r;
//}
//EXPORT uint32_t my32_SDL_WriteLE64(x64emu_t* emu, void* a, uint64_t v)
//{
//    SDL1_RWops_t* rw = RWNativeStart(emu, (SDL1_RWops_t*)a);
//    uint32_t r = my->SDL_WriteLE64(rw, v);
//    RWNativeEnd(rw);
//    return r;
//}

// SDL1 doesn't really used rw_ops->type, but box64 does, so set sensible value (from SDL2)....
//EXPORT void *my32_SDL_RWFromConstMem(x64emu_t* emu, void* a, int b)
//{
//    SDL1_RWops_t* r = (SDL1_RWops_t*)my->SDL_RWFromConstMem(a, b);
//    RWSetType(r, 5);
//    return AddNativeRW(emu, r);
//}
//EXPORT void *my32_SDL_RWFromFP(x64emu_t* emu, void* a, int b)
//{
//    SDL1_RWops_t* r = (SDL1_RWops_t*)my->SDL_RWFromFP(a, b);
//    RWSetType(r, 2);
//    return AddNativeRW(emu, r);
//}
EXPORT void *my32_SDL_RWFromFile(x64emu_t* emu, void* a, void* b)
{
    SDL1_RWops_t* r = (SDL1_RWops_t*)my->SDL_RWFromFile(a, b);
    RWSetType(r, 2);
    void* ret = AddNativeRW(emu, r);
    inplace_SDL_RWops_to_32(ret);
    return ret;
}
EXPORT void *my32_SDL_RWFromMem(x64emu_t* emu, void* a, int b)
{
    SDL1_RWops_t* r = (SDL1_RWops_t*)my->SDL_RWFromMem(a, b);
    RWSetType(r, 4);
    void* ret = AddNativeRW(emu, r);
    inplace_SDL_RWops_to_32(ret);
    return ret;
}

EXPORT void my32_SDL_WM_SetIcon(void* s, void* mask)
{
    inplace_SDL_Surface_to_64(s);
    my->SDL_WM_SetIcon(s, mask);
    inplace_SDL_Surface_to_32(s);
}

EXPORT void my32_SDL_GetRGB(uint32_t pixel, void* format, void* r, void* g, void* b)
{
    inplace_SDL_PixelFormat_to_64(format);
    my->SDL_GetRGB(pixel, format, r, g, b);
    inplace_SDL_PixelFormat_to_32(format);
}

EXPORT void* my32_SDL_GetVideoInfo()
{
    static my_SDL_Palette_t vm_palette;
    static my_SDL_PixelFormat_t vm_format;
    static my_SDL_VideoInfo_32_t vm = {0};
    my_SDL_VideoInfo_t* r = my->SDL_GetVideoInfo();
    if(!r) return NULL;
    vm.hw_available = r->hw_available;
    vm.wm_available = r->wm_available;
    vm.blit_hw = r->blit_hw;
    vm.blit_hw_CC = r->blit_hw_CC;
    vm.blit_hw_A = r->blit_hw_A;
    vm.blit_sw = r->blit_sw;
    vm.blit_sw_CC = r->blit_sw_CC;
    vm.blit_sw_A = r->blit_sw_A;
    vm.blit_fill = r->blit_fill;
    vm.video_mem = r->video_mem;
    vm.current_h = r->current_h;
    vm.current_w = r->current_w;
    if(r->vfmt) {
        vm.vfmt = to_ptrv(&vm_format);
        memcpy(&vm_format, r->vfmt, sizeof(vm_format));
        if(r->vfmt->palette) {
            vm_format.palette = &vm_palette;
            memcpy(&vm_palette, r->vfmt->palette, sizeof(vm_palette));
        }
        inplace_SDL_PixelFormat_to_32(&vm_format);
    } else vm.vfmt = 0;
    return &vm;
}

EXPORT void *my32_SDL_AddTimer(x64emu_t* emu, uint32_t a, void* cb, void* p)
{
    return my->SDL_AddTimer(a, find_TimerCallback_Fct(cb), p);
}

EXPORT int my32_SDL_RemoveTimer(x64emu_t* emu, void *t)
{
    return my->SDL_RemoveTimer(t);
}

EXPORT int32_t my32_SDL_SetTimer(x64emu_t* emu, uint32_t t, void* p)
{
    return my->SDL_SetTimer(t, find_TimerCallback_Fct(p));
}
#if 0
EXPORT int32_t my32_SDL_BuildAudioCVT(x64emu_t* emu, void* a, uint32_t b, uint32_t c, int32_t d, uint32_t e, uint32_t f, int32_t g)
{
    printf_log(LOG_NONE, "Error, using Unimplemented SDL1 SDL_BuildAudioCVT\n");
    emu->quit = 1;
    return 0;
}

EXPORT int32_t my32_SDL_ConvertAudio(x64emu_t* emu, void* a)
{
    printf_log(LOG_NONE, "Error, using Unimplemented SDL1 SDL_ConvertAudio\n");
    emu->quit = 1;
    return 0;
}
#endif
//EXPORT void my32_SDL_SetEventFilter(x64emu_t* emu, void* a)
//{
//    my->SDL_SetEventFilter(find_EvtFilter_Fct(a));
//}
//EXPORT void *my32_SDL_GetEventFilter(x64emu_t* emu)
//{
//    return reverse_EvtFilterFct(my->SDL_GetEventFilter());
//}
void* my32_prepare_thread(x64emu_t *emu, void* f, void* arg, int ssize, void** pet);
EXPORT void *my32_SDL_CreateThread(x64emu_t* emu, void* cb, void* p)
{
    void* et = NULL;
    void* fnc = my32_prepare_thread(emu, cb, p, 0, &et);
    return my->SDL_CreateThread(fnc, et);
}

EXPORT void my32_SDL_KillThread(x64emu_t* emu, void* p)
{
    my->SDL_KillThread(p);
}

EXPORT void* my32_SDL_GL_GetProcAddress(x64emu_t* emu, void* name)
{
    khint_t k;
    const char* rname = (const char*)name;
    return getGLProcAddress32(emu, (glprocaddress_t)my->SDL_GL_GetProcAddress, rname);
}

// DL functions from wrappedlibdl.c
void* my_dlopen(x64emu_t* emu, void *filename, int flag);
int my_dlclose(x64emu_t* emu, void *handle);
void* my_dlsym(x64emu_t* emu, void *handle, void *symbol);
EXPORT void* my32_SDL_LoadObject(x64emu_t* emu, void* sofile)
{
    return my_dlopen(emu, sofile, 0);   // TODO: check correct flag value...
}
EXPORT void my32_SDL_UnloadObject(x64emu_t* emu, void* handle)
{
    my_dlclose(emu, handle);
}
EXPORT void* my32_SDL_LoadFunction(x64emu_t* emu, void* handle, void* name)
{
    return my_dlsym(emu, handle, name);
}

static my_SDL_Surface_t* sdl1_videomode_org = NULL;
static my_SDL_Palette_t sdl_vm_palette;
static my_SDL_PixelFormat_t sdl_vm_pixelformal;
static my_SDL_Surface_t sdl_vm_surface;
static void* wrapSurface(void* s)
{
    if(!s) return s;
    if(s==&sdl_vm_surface) {
        my_SDL_Surface_32_t* src = s;
        // refressh surface...
        sdl1_videomode_org->h = src->h;
        sdl1_videomode_org->w = src->w;
        sdl1_videomode_org->pitch = src->pitch;
        sdl1_videomode_org->flags = src->flags;
        sdl1_videomode_org->locked = src->locked;
        sdl1_videomode_org->refcount = src->refcount;
        sdl1_videomode_org->offset = src->offset;
        sdl1_videomode_org->unused1 = src->unused1;
        sdl1_videomode_org->format_version = src->format_version;
        sdl1_videomode_org->pixels = from_ptrv(src->pixels);
        sdl1_videomode_org->map = from_ptrv(src->map);
        sdl1_videomode_org->clip_rect.x = src->clip_rect.x;
        sdl1_videomode_org->clip_rect.y = src->clip_rect.y;
        sdl1_videomode_org->clip_rect.h = src->clip_rect.h;
        sdl1_videomode_org->clip_rect.w = src->clip_rect.w;
        return sdl1_videomode_org;
    }
    inplace_SDL_Surface_to_64(s);
    return s;
}
static void* unwrapSurface(void* s)
{
    if(!s) return s;
    if(s==&sdl_vm_surface || s==sdl1_videomode_org) {
        my_SDL_Surface_32_t* dst = (my_SDL_Surface_32_t*)&sdl_vm_surface;
        // refressh surface...
        dst->h = sdl1_videomode_org->h;
        dst->w = sdl1_videomode_org->w;
        dst->pitch = sdl1_videomode_org->pitch;
        dst->flags = sdl1_videomode_org->flags;
        dst->locked = sdl1_videomode_org->locked;
        dst->refcount = sdl1_videomode_org->refcount;
        dst->offset = sdl1_videomode_org->offset;
        dst->unused1 = sdl1_videomode_org->unused1;
        dst->format_version = sdl1_videomode_org->format_version;
        dst->pixels = to_ptrv(sdl1_videomode_org->pixels);
        dst->map = to_ptrv(sdl1_videomode_org->map);
        dst->clip_rect.x = sdl1_videomode_org->clip_rect.x;
        dst->clip_rect.y = sdl1_videomode_org->clip_rect.y;
        dst->clip_rect.h = sdl1_videomode_org->clip_rect.h;
        dst->clip_rect.w = sdl1_videomode_org->clip_rect.w;
        return s;
    }
    inplace_SDL_Surface_to_32(s);
    return s;
}

EXPORT void my32_SDL_Quit()
{
    sdl1_videomode_org = NULL;
    my->SDL_Quit();
}

EXPORT void*  my32_SDL_SetVideoMode(int width, int height, int bpp, uint32_t flags)
{
    my_SDL_Surface_t* ret = my->SDL_SetVideoMode(width, height, bpp, flags);
    if (!ret) return ret;
    sdl1_videomode_org = ret;
    memcpy(&sdl_vm_surface, ret, sizeof(sdl_vm_surface));
    if(ret->format) {
        memcpy(&sdl_vm_pixelformal, ret->format, sizeof(sdl_vm_pixelformal));
        if(ret->format->palette) {
            memcpy(&sdl_vm_palette, ret->format->palette, sizeof(sdl_vm_palette));
            sdl_vm_pixelformal.palette = &sdl_vm_palette;
        }
        sdl_vm_surface.format = &sdl_vm_pixelformal;
    }
    inplace_SDL_Surface_to_32(&sdl_vm_surface);
    return &sdl_vm_surface;
}

EXPORT int my32_SDL_LockSurface(void* s)
{
    int ret = my->SDL_LockSurface(wrapSurface(s));
    unwrapSurface(s);
    return ret;
}

EXPORT void my32_SDL_UnlockSurface(void* s)
{
    my->SDL_UnlockSurface(wrapSurface(s));
    unwrapSurface(s);
}

EXPORT int my32_SDL_Flip(void* s)
{
    int ret = my->SDL_Flip(wrapSurface(s));
    unwrapSurface(s);
    return ret;
}

EXPORT void* my32_SDL_GetVideoSurface()
{
    void* ret = my->SDL_GetVideoSurface();
    return unwrapSurface(ret);
}

EXPORT void* my32_SDL_CreateRGBSurfaceFrom(void* pixels, int width, int height, int depth, int pitch, uint32_t rmask, uint32_t gmask, uint32_t bmask, uint32_t amask)
{
    void* p = my->SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, rmask, gmask, bmask, amask);
    return unwrapSurface(p);
}


EXPORT void my32_SDL_FreeSurface(void* s)
{
    my->SDL_FreeSurface(wrapSurface(s));
    if(s==&sdl_vm_surface)
        sdl1_videomode_org = NULL;
}

EXPORT int my32_SDL_SetColorKey(void* s, uint32_t flags, uint32_t key)
{
    int ret = my->SDL_SetColorKey(wrapSurface(s), flags, key);
    unwrapSurface(s);
    return ret;
}

EXPORT int my32_SDL_PollEvent(my_SDL_Event_32_t* evt)
{
    my_SDL_Event_t event;
    int ret = my->SDL_PollEvent(evt?(&event):NULL);
    if(ret && evt) {
        convert_SDL_Event_to_32(evt, &event);
    }
    return ret;
}

EXPORT int my32_SDL_PeepEvents(my_SDL_Event_32_t* evts, int num, uint32_t action, uint32_t mask)
{
    my_SDL_Event_t events[num];
    int ret = my->SDL_PeepEvents(events, num, action, mask);
    for(int i=0; i<ret; ++i) convert_SDL_Event_to_32(evts+i, events+i);
    return ret;
}

EXPORT int my32_SDL_PushEvent(my_SDL_Event_32_t* evt)
{
    my_SDL_Event_t event;
    convert_SDL_Event_to_64(&event, evt);
    return my->SDL_PushEvent(&event);
}

EXPORT void* my32_SDL_ListModes(my_SDL_PixelFormat_32_t* fmt, uint32_t flags)
{
    my_SDL_PixelFormat_t format;
    my_SDL_Palette_t palette;
    if(fmt) {
        memcpy(&format, fmt, sizeof(format));
        inplace_SDL_PixelFormat_to_64_nopalette(&format);
        if(fmt->palette) {
            memcpy(&palette, from_ptrv(fmt->palette), sizeof(palette));
            format.palette = &palette;
            inplace_SDL_Palette_to_64(&palette);
        }
    }
    void** ret = my->SDL_ListModes(fmt?(&format):NULL, flags);
    if(!ret)
        return ret;
    if(ret==(void**)-1LL)
        return ret;
    static ptr_t available[256];
    void** p = ret;
    int idx = 0;
    while((*p) && (idx<255)) {
        available[idx++] = to_ptrv(*p);
        ++p;
    }
    available[idx++] = 0;
    return &available;
}

EXPORT uint32_t my32_SDL_MapRGB(my_SDL_PixelFormat_32_t* fmt, uint8_t r, uint8_t g, uint8_t b)
{
    my_SDL_PixelFormat_t format;
    my_SDL_Palette_t palette;
    if(fmt) {
        memcpy(&format, fmt, sizeof(format));
        inplace_SDL_PixelFormat_to_64_nopalette(&format);
        if(fmt->palette) {
            memcpy(&palette, from_ptrv(fmt->palette), sizeof(palette));
            format.palette = &palette;
            inplace_SDL_Palette_to_64(&palette);
        }
    }
    return my->SDL_MapRGB(fmt?(&format):NULL, r, g, b);
}

EXPORT int my32_SDL_BuildAudioCVT(my_SDL_AudioCVT_32_t* cvt, uint16_t src_fmt, uint16_t src_chn, int src_rate, uint16_t dst_fmt, uint16_t dst_ch, int dst_rate)
{
    my_SDL_AudioCVT_t l_cvt;
    int ret = my->SDL_BuildAudioCVT(&l_cvt, src_fmt, src_chn, src_rate, dst_fmt, dst_ch, dst_rate);
    if(ret!=-1)
        convert_AudioCVT_to_32(cvt, &l_cvt);
    return ret;
}

EXPORT int my32_SDL_ConvertAudio(my_SDL_AudioCVT_32_t* cvt)
{
    my_SDL_AudioCVT_t l_cvt;
    convert_AudioCVT_to_64(&l_cvt, cvt);
    int ret = my->SDL_ConvertAudio(&l_cvt);
    convert_AudioCVT_to_32(cvt, &l_cvt);
    return ret;
}

//EXPORT int32_t my32_SDL_GetWMInfo(x64emu_t* emu, void* p)
//{
//    // does SDL_SysWMinfo needs alignment?
//    int ret = my->SDL_GetWMInfo(p);
//    my32_SDL_SysWMinfo *info = (my32_SDL_SysWMinfo*)p;
//    if(info->info.x11.lock_func)
//        info->info.x11.lock_func = (void*)AddCheckBridge(emu->context->system, vFv_32, info->info.x11.lock_func, 0, NULL);
//    if(info->info.x11.unlock_func)
//        info->info.x11.unlock_func = (void*)AddCheckBridge(emu->context->system, vFv_32, info->info.x11.unlock_func, 0, NULL);
//    return ret;
//}

#define CUSTOM_INIT \
    box64->sdl1allocrw = my->SDL_AllocRW;   \
    box64->sdl1freerw  = my->SDL_FreeRW;

#define NEEDED_LIBS "libm.so.6", "libdl.so.2", "librt.so.1"

#define CUSTOM_FINI \
    my_context->sdl1allocrw = NULL;         \
    my_context->sdl1freerw = NULL;

#include "wrappedlib_init32.h"