Skip to content

Latest commit

 

History

History
2880 lines (2478 loc) · 79.6 KB

SDL12_compat.c

File metadata and controls

2880 lines (2478 loc) · 79.6 KB
 
Mar 4, 2013
Mar 4, 2013
1
2
/*
Simple DirectMedia Layer
Feb 10, 2019
Feb 10, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
Mar 4, 2013
Mar 4, 2013
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* This file contains functions for backwards compatibility with SDL 1.2 */
Apr 14, 2013
Apr 14, 2013
24
#include "SDL20_include_wrapper.h"
Mar 4, 2013
Mar 4, 2013
25
26
27
28
29
#if !SDL_VERSION_ATLEAST(2,0,0)
#error You need to compile against SDL 2.0 headers.
#endif
Apr 14, 2013
Apr 14, 2013
30
31
32
33
34
35
36
/*
* We report the library version as 1.2.$(SDL12_COMPAT_VERSION). This number
* should be way ahead of what SDL-1.2 Classic would report, so apps can
* decide if they're running under the compat layer, if they really care.
*/
#define SDL12_COMPAT_VERSION 50
Mar 7, 2013
Mar 7, 2013
37
38
#include <stdarg.h>
39
40
41
42
43
44
45
46
47
48
49
50
51
// !!! IMPLEMENT_ME SDL_ConvertSurface
// !!! IMPLEMENT_ME SDL_GetKeyName
// !!! IMPLEMENT_ME SDL_GetKeyState
// !!! IMPLEMENT_ME SDL_GetModState
// !!! IMPLEMENT_ME SDL_GetRelativeMouseState
// !!! IMPLEMENT_ME SDL_LockSurface
// !!! IMPLEMENT_ME SDL_LowerBlit
// !!! IMPLEMENT_ME SDL_SetColorKey
// !!! IMPLEMENT_ME SDL_SetModState
// !!! IMPLEMENT_ME SDL_SoftStretch
// !!! IMPLEMENT_ME SDL_UnlockSurface
// !!! IMPLEMENT_ME SDL_UpperBlit
// !!! IMPLEMENT_ME X11_KeyToUnicode
Mar 4, 2013
Mar 4, 2013
52
Mar 6, 2013
Mar 6, 2013
53
Mar 6, 2013
Mar 6, 2013
54
#define SDL20_SYM(rc,fn,params,args,ret) \
Apr 14, 2013
Apr 14, 2013
55
typedef rc (SDLCALL *SDL20_##fn##_t) params; \
Mar 6, 2013
Mar 6, 2013
56
static SDL20_##fn##_t SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
57
58
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
59
60
#include "SDL20_syms.h"
Feb 10, 2019
Feb 10, 2019
61
/* this doesn't get handled in SDL20_syms.h because it uses varargs. */
Apr 14, 2013
Apr 14, 2013
62
typedef int (SDLCALL *SDL20_SetError_t)(const char *fmt, ...);
Mar 7, 2013
Mar 7, 2013
63
static SDL20_SetError_t SDL20_SetError = NULL;
Mar 6, 2013
Mar 6, 2013
64
Apr 14, 2013
Apr 14, 2013
65
66
/* Things that _should_ be binary compatible pass right through... */
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
Apr 14, 2013
Apr 14, 2013
67
DECLSPEC rc SDLCALL SDL_##fn params { ret SDL20_##fn args; }
Apr 14, 2013
Apr 14, 2013
68
69
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
70
Apr 14, 2013
Apr 14, 2013
71
72
73
74
75
76
77
78
79
80
/* these are macros (etc) in the SDL headers, so make our own. */
#define SDL20_OutOfMemory() SDL20_Error(SDL_ENOMEM)
#define SDL20_Unsupported() SDL20_Error(SDL_UNSUPPORTED)
#define SDL20_InvalidParamError(param) SDL20_SetError("Parameter '%s' is invalid", (param))
#define SDL20_zero(x) SDL20_memset(&(x), 0, sizeof((x)))
#define SDL20_zerop(x) SDL20_memset((x), 0, sizeof(*(x)))
#define SDL_ReportAssertion SDL20_ReportAssertion
#define SDL12_DEFAULT_REPEAT_DELAY 500
#define SDL12_DEFAULT_REPEAT_INTERVAL 30
Mar 6, 2013
Mar 6, 2013
81
82
83
84
85
86
87
88
89
90
#define SDL12_INIT_TIMER 0x00000001
#define SDL12_INIT_AUDIO 0x00000010
#define SDL12_INIT_VIDEO 0x00000020
#define SDL12_INIT_CDROM 0x00000100
#define SDL12_INIT_JOYSTICK 0x00000200
#define SDL12_INIT_NOPARACHUTE 0x00100000
#define SDL12_INIT_EVENTTHREAD 0x01000000
#define SDL12_INIT_EVERYTHING 0x0000FFFF
Mar 8, 2013
Mar 8, 2013
91
typedef struct SDL12_Palette
Mar 6, 2013
Mar 6, 2013
92
{
Mar 8, 2013
Mar 8, 2013
93
94
int ncolors;
SDL_Color *colors;
Mar 6, 2013
Mar 6, 2013
95
96
} SDL12_Palette;
Mar 8, 2013
Mar 8, 2013
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
typedef struct SDL12_PixelFormat
{
SDL12_Palette *palette;
Uint8 BitsPerPixel;
Uint8 BytesPerPixel;
Uint8 Rloss;
Uint8 Gloss;
Uint8 Bloss;
Uint8 Aloss;
Uint8 Rshift;
Uint8 Gshift;
Uint8 Bshift;
Uint8 Ashift;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
Uint32 colorkey;
Uint8 alpha;
Mar 6, 2013
Mar 6, 2013
116
117
} SDL12_PixelFormat;
Apr 1, 2013
Apr 1, 2013
118
119
120
121
122
123
124
125
126
typedef struct SDL12_Surface
{
Uint32 flags;
SDL12_PixelFormat *format;
int w;
int h;
Uint16 pitch;
void *pixels;
int offset;
Apr 14, 2013
Apr 14, 2013
127
SDL_Surface *surface20; /* the real SDL 1.2 has an opaque pointer to a platform-specific thing here named "hwdata". */
Apr 1, 2013
Apr 1, 2013
128
129
130
131
132
133
134
135
SDL_Rect clip_rect;
Uint32 unused1;
Uint32 locked;
void *blitmap;
unsigned int format_version;
int refcount;
} SDL12_Surface;
Mar 8, 2013
Mar 8, 2013
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
typedef struct
{
Uint32 hw_available :1;
Uint32 wm_available :1;
Uint32 UnusedBits1 :6;
Uint32 UnusedBits2 :1;
Uint32 blit_hw :1;
Uint32 blit_hw_CC :1;
Uint32 blit_hw_A :1;
Uint32 blit_sw :1;
Uint32 blit_sw_CC :1;
Uint32 blit_sw_A :1;
Uint32 blit_fill :1;
Uint32 UnusedBits3 :16;
Uint32 video_mem;
SDL_PixelFormat *vfmt;
int current_w;
int current_h;
} SDL12_VideoInfo;
#define SDL12_HWSURFACE 0x00000001
#define SDL12_ASYNCBLIT 0x00000004
#define SDL12_ANYFORMAT 0x10000000
#define SDL12_HWPALETTE 0x20000000
#define SDL12_DOUBLEBUF 0x40000000
#define SDL12_FULLSCREEN 0x80000000
#define SDL12_OPENGL 0x00000002
#define SDL12_OPENGLBLIT 0x0000000A
#define SDL12_RESIZABLE 0x00000010
#define SDL12_NOFRAME 0x00000020
#define SDL12_HWACCEL 0x00000100
#define SDL12_SRCCOLORKEY 0x00001000
#define SDL12_RLEACCELOK 0x00002000
#define SDL12_RLEACCEL 0x00004000
#define SDL12_SRCALPHA 0x00010000
#define SDL12_PREALLOC 0x01000000
Apr 1, 2013
Apr 1, 2013
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
typedef enum
{
SDL12_NOEVENT = 0,
SDL12_ACTIVEEVENT,
SDL12_KEYDOWN,
SDL12_KEYUP,
SDL12_MOUSEMOTION,
SDL12_MOUSEBUTTONDOWN,
SDL12_MOUSEBUTTONUP,
SDL12_JOYAXISMOTION,
SDL12_JOYBALLMOTION,
SDL12_JOYHATMOTION,
SDL12_JOYBUTTONDOWN,
SDL12_JOYBUTTONUP,
SDL12_QUIT,
SDL12_SYSWMEVENT,
SDL12_EVENT_RESERVEDA,
SDL12_EVENT_RESERVEDB,
SDL12_VIDEORESIZE,
SDL12_VIDEOEXPOSE,
SDL12_USEREVENT = 24,
SDL12_NUMEVENTS = 32
} SDL12_EventType;
Apr 14, 2013
Apr 14, 2013
198
199
200
201
202
#define SDL12_APPMOUSEFOCUS (1<<0)
#define SDL12_APPINPUTFOCUS (1<<1)
#define SDL12_APPACTIVE (1<<2)
Apr 1, 2013
Apr 1, 2013
203
204
205
206
207
208
209
210
211
212
213
214
typedef struct
{
Uint8 type;
Uint8 gain;
Uint8 state;
} SDL12_ActiveEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 state;
Apr 14, 2013
Apr 14, 2013
215
//FIXME: SDL12_keysym keysym;
Apr 1, 2013
Apr 1, 2013
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
} SDL12_KeyboardEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 state;
Uint16 x, y;
Sint16 xrel;
Sint16 yrel;
} SDL12_MouseMotionEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 button;
Uint8 state;
Uint16 x, y;
} SDL12_MouseButtonEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 axis;
Sint16 value;
} SDL12_JoyAxisEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 ball;
Sint16 xrel;
Sint16 yrel;
} SDL12_JoyBallEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 hat;
Uint8 value;
} SDL12_JoyHatEvent;
typedef struct
{
Uint8 type;
Uint8 which;
Uint8 button;
Uint8 state;
} SDL12_JoyButtonEvent;
typedef struct
{
Uint8 type;
int w;
int h;
} SDL12_ResizeEvent;
typedef struct
{
Uint8 type;
} SDL12_ExposeEvent;
typedef struct
{
Uint8 type;
} SDL12_QuitEvent;
typedef struct
{
Uint8 type;
int code;
void *data1;
void *data2;
} SDL12_UserEvent;
typedef struct
{
Uint8 type;
void *msg;
} SDL12_SysWMEvent;
typedef union
{
Uint8 type;
SDL12_ActiveEvent active;
SDL12_KeyboardEvent key;
SDL12_MouseMotionEvent motion;
SDL12_MouseButtonEvent button;
SDL12_JoyAxisEvent jaxis;
SDL12_JoyBallEvent jball;
SDL12_JoyHatEvent jhat;
SDL12_JoyButtonEvent jbutton;
SDL12_ResizeEvent resize;
SDL12_ExposeEvent expose;
SDL12_QuitEvent quit;
SDL12_UserEvent user;
SDL12_SysWMEvent syswm;
} SDL12_Event;
Mar 8, 2013
Mar 8, 2013
318
Apr 14, 2013
Apr 14, 2013
319
320
321
322
323
324
typedef int (SDLCALL *SDL12_EventFilter)(const SDL12_Event *event12);
static int EventFilter20to12(void *data, SDL_Event *event20);
typedef Uint32 (SDLCALL *SDL12_TimerCallback)(Uint32 interval);
typedef SDL_TimerCallback SDL12_NewTimerCallback;
Apr 11, 2013
Apr 11, 2013
325
typedef struct
Apr 14, 2013
Apr 14, 2013
326
{
Apr 11, 2013
Apr 11, 2013
327
328
329
330
331
332
333
334
335
SDL_Rect area;
Sint16 hot_x;
Sint16 hot_y;
Uint8 *data;
Uint8 *mask;
Uint8 *save[2];
SDL_Cursor *wm_cursor; /* the real SDL 1.2 has an opaque pointer to a platform-specific cursor here. */
} SDL12_Cursor;
Apr 13, 2013
Apr 13, 2013
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
typedef enum
{
SDL12_GL_RED_SIZE,
SDL12_GL_GREEN_SIZE,
SDL12_GL_BLUE_SIZE,
SDL12_GL_ALPHA_SIZE,
SDL12_GL_BUFFER_SIZE,
SDL12_GL_DOUBLEBUFFER,
SDL12_GL_DEPTH_SIZE,
SDL12_GL_STENCIL_SIZE,
SDL12_GL_ACCUM_RED_SIZE,
SDL12_GL_ACCUM_GREEN_SIZE,
SDL12_GL_ACCUM_BLUE_SIZE,
SDL12_GL_ACCUM_ALPHA_SIZE,
SDL12_GL_STEREO,
SDL12_GL_MULTISAMPLEBUFFERS,
SDL12_GL_MULTISAMPLESAMPLES,
SDL12_GL_ACCELERATED_VISUAL,
SDL12_GL_SWAP_CONTROL,
SDL12_GL_MAX_ATTRIBUTE
} SDL12_GLattr;
Apr 11, 2013
Apr 11, 2013
357
Jul 26, 2014
Jul 26, 2014
358
359
360
361
362
363
364
365
366
367
typedef struct
{
Uint32 format;
SDL_Rect *modeslist;
SDL_Rect **modes; /* ptrs to each item in modeslist, for SDL_ListModes() */
} VideoModeList;
static VideoModeList *VideoModes = NULL;
static int VideoModesCount = 0;
Mar 8, 2013
Mar 8, 2013
368
static SDL12_VideoInfo VideoInfo;
Apr 1, 2013
Apr 1, 2013
369
370
371
372
373
static SDL_Window *VideoWindow20 = NULL;
static SDL12_Surface *WindowSurface = NULL;
static SDL12_Surface *VideoSurface = NULL;
static SDL12_Surface *ShadowSurface = NULL;
static SDL12_Surface *PublicSurface = NULL;
Mar 8, 2013
Mar 8, 2013
374
375
376
377
378
379
380
381
382
static SDL_GLContext *VideoContext = NULL;
static Uint32 VideoFlags = 0;
static SDL_Rect VideoViewport;
static char *WindowTitle = NULL;
static char *WindowIconTitle = NULL;
static SDL_Surface *VideoIcon;
static int EnabledUnicode = 0;
static int VideoDisplayIndex = 0;
static int CDRomInit = 0;
Apr 1, 2013
Apr 1, 2013
383
static SDL12_EventFilter EventFilter12 = NULL;
Apr 11, 2013
Apr 11, 2013
384
static SDL12_Cursor *CurrentCursor = NULL;
Apr 13, 2013
Apr 13, 2013
385
static Uint8 EventStates[SDL12_NUMEVENTS];
Apr 13, 2013
Apr 13, 2013
386
static int SwapInterval = 0;
Apr 1, 2013
Apr 1, 2013
387
388
389
// !!! FIXME: need a mutex for the event queue.
#define SDL12_MAXEVENTS 128
Apr 10, 2013
Apr 10, 2013
390
391
392
393
394
395
396
397
398
399
typedef struct EventQueueType
{
SDL12_Event event12;
struct EventQueueType *next;
} EventQueueType;
static EventQueueType EventQueuePool[SDL12_MAXEVENTS];
static EventQueueType *EventQueueHead = NULL;
static EventQueueType *EventQueueTail = NULL;
static EventQueueType *EventQueueAvailable = NULL;
Mar 6, 2013
Mar 6, 2013
400
Apr 14, 2013
Apr 14, 2013
401
Mar 6, 2013
Mar 6, 2013
402
403
/* Obviously we can't use SDL_LoadObject() to load SDL2. :) */
#if defined(_WINDOWS)
Apr 1, 2013
Apr 1, 2013
404
405
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
Mar 6, 2013
Mar 6, 2013
406
407
408
409
410
#define SDL20_LIBNAME "SDL2.dll"
static HANDLE Loaded_SDL20 = NULL;
#define LoadSDL20Library() ((Loaded_SDL20 = LoadLibraryA(SDL20_LIBNAME)) != NULL)
#define LookupSDL20Sym(sym) GetProcAddress(Loaded_SDL20, sym)
#define CloseSDL20Library() { { if (Loaded_SDL20) { FreeLibrary(Loaded_SDL20); Loaded_SDL20 = NULL; } }
Apr 1, 2013
Apr 1, 2013
411
412
#elif defined(unix) || defined(__APPLE__)
#include <dlfcn.h>
Mar 6, 2013
Mar 6, 2013
413
414
415
416
417
418
419
420
421
422
423
424
#ifdef __APPLE__
#define SDL20_LIBNAME "libSDL2.dylib"
#else
#define SDL20_LIBNAME "libSDL2-2.0.so.0"
#endif
static void *Loaded_SDL20 = NULL;
#define LoadSDL20Library() ((Loaded_SDL20 = dlopen(SDL20_LIBNAME, RTLD_LOCAL)) != NULL)
#define LookupSDL20Sym(sym) dlsym(Loaded_SDL20, sym)
#define CloseSDL20Library() { if (Loaded_SDL20) { dlclose(Loaded_SDL20); Loaded_SDL20 = NULL; } }
#else
#error Please define your platform.
#endif
Mar 4, 2013
Mar 4, 2013
425
Mar 6, 2013
Mar 6, 2013
426
427
static void *
LoadSDL20Symbol(const char *fn, int *okay)
Mar 4, 2013
Mar 4, 2013
428
{
Mar 6, 2013
Mar 6, 2013
429
430
431
432
433
434
435
436
void *retval = NULL;
if (*okay) /* only bother trying if we haven't previously failed. */
{
retval = LookupSDL20Sym(fn);
if (retval == NULL)
*okay = 0;
}
return retval;
Mar 4, 2013
Mar 4, 2013
437
438
}
Mar 6, 2013
Mar 6, 2013
439
440
static void
UnloadSDL20(void)
Mar 4, 2013
Mar 4, 2013
441
{
Mar 6, 2013
Mar 6, 2013
442
#define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = NULL;
Apr 14, 2013
Apr 14, 2013
443
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
444
#include "SDL20_syms.h"
Mar 7, 2013
Mar 7, 2013
445
SDL20_SetError = NULL;
Mar 6, 2013
Mar 6, 2013
446
447
448
449
450
451
452
453
454
455
456
CloseSDL20Library();
}
static int
LoadSDL20(void)
{
int okay = 1;
if (!Loaded_SDL20)
{
okay = LoadSDL20Library();
#define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = (SDL20_##fn##_t) LoadSDL20Symbol("SDL_" #fn, &okay);
Mar 6, 2013
Mar 6, 2013
457
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
458
#include "SDL20_syms.h"
Mar 7, 2013
Mar 7, 2013
459
SDL20_SetError = (SDL20_SetError_t) LoadSDL20Symbol("SDL_SetError", &okay);
Mar 6, 2013
Mar 6, 2013
460
461
if (!okay)
UnloadSDL20();
Mar 4, 2013
Mar 4, 2013
462
}
Mar 6, 2013
Mar 6, 2013
463
return okay;
Mar 4, 2013
Mar 4, 2013
464
465
}
Apr 14, 2013
Apr 14, 2013
466
467
468
469
470
471
DECLSPEC const SDL_version * SDLCALL
SDL_Linked_Version(void)
{
static const SDL_version version = { 1, 2, SDL12_COMPAT_VERSION };
return &version;
}
Mar 6, 2013
Mar 6, 2013
472
Mar 4, 2013
Mar 4, 2013
473
474
475
static int
GetVideoDisplay()
{
Mar 6, 2013
Mar 6, 2013
476
// !!! FIXME: cache this value during SDL_Init() so it doesn't change.
Mar 4, 2013
Mar 4, 2013
477
478
479
480
481
const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
if ( !variable ) {
variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
}
if ( variable ) {
Apr 14, 2013
Apr 14, 2013
482
return SDL20_atoi(variable);
Mar 4, 2013
Mar 4, 2013
483
484
485
486
487
} else {
return 0;
}
}
Jul 26, 2014
Jul 26, 2014
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
/* This sets up VideoModes and VideoModesCount. You end up with arrays by pixel
format, each with a value that 1.2's SDL_ListModes() can return. */
static int
Init12VidModes(void)
{
const int total = SDL20_GetNumDisplayModes(VideoDisplayIndex);
VideoModeList *vmode = NULL;
int num_modes = 0;
void *ptr = NULL;
int i, j;
SDL_assert(VideoModes == NULL);
SDL_assert(VideoModesCount == 0);
for (i = 0; i < total; ++i) {
SDL_DisplayMode mode;
if (SDL20_GetDisplayMode(VideoDisplayIndex, i, &mode) == -1) {
continue;
} else if (!mode.w || !mode.h) {
SDL_assert(0 && "Can this actually happen?");
continue;
}
if (!vmode || (mode.format != vmode->format)) { // SDL20_GetDisplayMode() sorts on bpp first. We know when to change arrays.
if (VideoModesCount > 0) {
VideoModes[VideoModesCount-1].modes[num_modes] = NULL;
}
ptr = (VideoModeList *) SDL20_realloc(VideoModes, sizeof (VideoModeList) * (VideoModesCount+1));
if (!ptr) {
return SDL20_OutOfMemory();
}
VideoModes = (VideoModeList *) ptr;
vmode = &VideoModes[VideoModesCount];
vmode->format = mode.format;
vmode->modeslist = NULL;
vmode->modes = NULL;
VideoModesCount++;
num_modes = 0;
}
/* make sure we don't have this one already (with a different refresh rate, etc). */
for (j = 0; j < num_modes; j++) {
if ((vmode->modeslist[j].w == mode.w) && (vmode->modeslist[j].h == mode.h)) {
break;
}
}
if (j < num_modes) {
continue; /* already have this one. */
}
ptr = SDL20_realloc(vmode->modes, sizeof (SDL_Rect *) * (num_modes + 2));
if (ptr == NULL) {
return SDL20_OutOfMemory();
}
vmode->modes = (SDL_Rect **) ptr;
ptr = SDL20_realloc(vmode->modeslist, sizeof (SDL_Rect) * (num_modes + 1));
if (ptr == NULL) {
return SDL20_OutOfMemory();
}
vmode->modeslist = (SDL_Rect *) ptr;
vmode->modeslist[num_modes].x = 0;
vmode->modeslist[num_modes].y = 0;
vmode->modeslist[num_modes].w = mode.w;
vmode->modeslist[num_modes].h = mode.h;
vmode->modes[num_modes] = &vmode->modeslist[num_modes];
num_modes++;
}
if (VideoModesCount > 0) {
VideoModes[VideoModesCount-1].modes[num_modes] = NULL;
}
return 0;
}
static int
Init12Video(void)
{
int i;
for (i = 0; i < SDL12_MAXEVENTS-1; i++)
EventQueuePool[i].next = &EventQueuePool[i+1];
EventQueuePool[SDL12_MAXEVENTS-1].next = NULL;
EventQueueHead = EventQueueTail = NULL;
EventQueueAvailable = EventQueuePool;
SDL_memset(EventStates, SDL_ENABLE, sizeof (EventStates)); /* on by default */
EventStates[SDL12_SYSWMEVENT] = SDL_IGNORE; /* off by default. */
SDL20_SetEventFilter(EventFilter20to12, NULL);
VideoDisplayIndex = GetVideoDisplay();
SwapInterval = 0;
if (Init12VidModes() == -1) {
return -1;
}
return 0;
}
Jul 18, 2014
Jul 18, 2014
597
598
DECLSPEC int SDLCALL
SDL_InitSubSystem(Uint32 sdl12flags)
Mar 6, 2013
Mar 6, 2013
599
{
Jul 26, 2014
Jul 26, 2014
600
601
602
603
604
605
606
// !!! FIXME: there is never a parachute in SDL2, should we catch segfaults ourselves?
// !!! FIXME: support event thread where it makes sense to do so?
if ( (sdl12flags & SDL12_INIT_EVENTTHREAD) == SDL12_INIT_EVENTTHREAD ) {
return SDL20_SetError("OS doesn't support threaded events");
}
Mar 6, 2013
Mar 6, 2013
607
608
609
610
611
612
Uint32 sdl20flags = 0;
int rc;
if (!LoadSDL20())
return -1;
Apr 1, 2013
Apr 1, 2013
613
#define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
Mar 6, 2013
Mar 6, 2013
614
615
616
617
618
619
620
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
#undef SETFLAG
Mar 7, 2013
Mar 7, 2013
621
622
// There's no CDROM in 2.0, but we'll just pretend it succeeded.
if (sdl12flags & SDL12_INIT_CDROM)
Mar 8, 2013
Mar 8, 2013
623
CDRomInit = 1;
Mar 7, 2013
Mar 7, 2013
624
Mar 6, 2013
Mar 6, 2013
625
626
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
Jul 18, 2014
Jul 18, 2014
627
rc = SDL20_Init(sdl20flags);
Jul 26, 2014
Jul 26, 2014
628
629
630
631
if ((rc == 0) && (sdl20flags & SDL_INIT_VIDEO)) {
if (Init12Video() == -1) {
return -1; /* !!! FIXME: should we deinit other subsystems? */
}
Apr 1, 2013
Apr 1, 2013
632
633
}
Mar 6, 2013
Mar 6, 2013
634
635
636
return rc;
}
Apr 14, 2013
Apr 14, 2013
637
DECLSPEC int SDLCALL
Mar 7, 2013
Mar 7, 2013
638
639
SDL_Init(Uint32 sdl12flags)
{
Jul 26, 2014
Jul 26, 2014
640
return SDL_InitSubSystem(sdl12flags); /* there's no difference betwee Init and InitSubSystem in SDL2. */
Mar 7, 2013
Mar 7, 2013
641
642
}
Jul 21, 2014
Jul 21, 2014
643
644
645
static void
InitFlags12To20(const Uint32 flags12, Uint32 *_flags20, Uint32 *_extraflags)
Mar 7, 2013
Mar 7, 2013
646
{
Jul 21, 2014
Jul 21, 2014
647
Uint32 flags20 = 0;
Mar 7, 2013
Mar 7, 2013
648
649
Uint32 extraflags = 0;
Jul 21, 2014
Jul 21, 2014
650
#define SETFLAG(flag) if (flags12 & SDL12_INIT_##flag) flags20 |= SDL_INIT_##flag
Mar 7, 2013
Mar 7, 2013
651
652
653
654
655
656
657
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
#undef SETFLAG
Jul 21, 2014
Jul 21, 2014
658
if ((flags12 & SDL12_INIT_CDROM) && (CDRomInit)) {
Mar 7, 2013
Mar 7, 2013
659
extraflags |= SDL12_INIT_CDROM;
Jul 21, 2014
Jul 21, 2014
660
}
Mar 7, 2013
Mar 7, 2013
661
662
663
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
Jul 21, 2014
Jul 21, 2014
664
665
*_flags20 = flags20;
*_extraflags = extraflags;
Mar 7, 2013
Mar 7, 2013
666
667
}
Jul 21, 2014
Jul 21, 2014
668
669
static Uint32
InitFlags20to12(const Uint32 flags20)
Mar 7, 2013
Mar 7, 2013
670
{
Jul 21, 2014
Jul 21, 2014
671
Uint32 flags12 = 0;
Mar 7, 2013
Mar 7, 2013
672
Jul 21, 2014
Jul 21, 2014
673
#define SETFLAG(flag) if (flags20 & SDL_INIT_##flag) flags12 |= SDL12_INIT_##flag
Mar 7, 2013
Mar 7, 2013
674
675
676
677
678
679
680
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
#undef SETFLAG
Jul 21, 2014
Jul 21, 2014
681
682
683
684
685
686
687
688
689
690
691
692
693
return flags12;
}
DECLSPEC Uint32 SDLCALL
SDL_WasInit(Uint32 sdl12flags)
{
Uint32 sdl20flags, extraflags;
InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
return InitFlags20to12(SDL20_WasInit(sdl20flags)) | extraflags;
}
Jul 26, 2014
Jul 26, 2014
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
static void
Quit12Video(void)
{
int i;
for (i = 0; i < VideoModesCount; i++) {
SDL20_free(VideoModes[i].modeslist);
SDL20_free(VideoModes[i].modes);
}
SDL20_free(VideoModes);
SDL20_FreeFormat(VideoInfo.vfmt);
SDL20_zero(VideoInfo);
EventFilter12 = NULL;
EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
CurrentCursor = NULL;
VideoModes = NULL;
VideoModesCount = 0;
}
Jul 21, 2014
Jul 21, 2014
715
716
717
718
719
720
721
DECLSPEC void SDLCALL
SDL_QuitSubSystem(Uint32 sdl12flags)
{
Uint32 sdl20flags, extraflags;
InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
if (extraflags & SDL12_INIT_CDROM) {
Mar 8, 2013
Mar 8, 2013
722
CDRomInit = 0;
Jul 21, 2014
Jul 21, 2014
723
}
Mar 8, 2013
Mar 8, 2013
724
Mar 8, 2013
Mar 8, 2013
725
726
// !!! FIXME: reset a bunch of other global variables too.
if (sdl12flags & SDL12_INIT_VIDEO) {
Jul 26, 2014
Jul 26, 2014
727
Quit12Video();
Mar 8, 2013
Mar 8, 2013
728
729
}
Mar 7, 2013
Mar 7, 2013
730
731
732
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
SDL20_QuitSubSystem(sdl20flags);
Jul 26, 2014
Jul 26, 2014
733
734
735
736
if ((SDL20_WasInit(0) == 0) && (!CDRomInit)) {
SDL20_Quit();
UnloadSDL20();
}
Mar 7, 2013
Mar 7, 2013
737
738
}
Apr 14, 2013
Apr 14, 2013
739
DECLSPEC void SDLCALL
Mar 6, 2013
Mar 6, 2013
740
741
SDL_Quit(void)
{
Jul 26, 2014
Jul 26, 2014
742
SDL_QuitSubSystem(SDL_WasInit(0) | SDL12_INIT_CDROM);
Mar 6, 2013
Mar 6, 2013
743
744
}
Apr 14, 2013
Apr 14, 2013
745
DECLSPEC void SDLCALL
Mar 7, 2013
Mar 7, 2013
746
747
SDL_SetError(const char *fmt, ...)
{
Apr 14, 2013
Apr 14, 2013
748
char ch;
Mar 7, 2013
Mar 7, 2013
749
char *str = NULL;
Apr 14, 2013
Apr 14, 2013
750
size_t len = 0;
Mar 7, 2013
Mar 7, 2013
751
752
va_list ap;
va_start(ap, fmt);
Apr 14, 2013
Apr 14, 2013
753
len = SDL20_vsnprintf(&ch, 1, fmt, ap);
Mar 7, 2013
Mar 7, 2013
754
va_end(ap);
Apr 14, 2013
Apr 14, 2013
755
756
str = (char *) SDL20_malloc(len + 1);
Mar 7, 2013
Mar 7, 2013
757
758
759
760
if (!str)
SDL20_OutOfMemory();
else
{
Apr 14, 2013
Apr 14, 2013
761
762
763
va_start(ap, fmt);
SDL20_vsnprintf(str, len + 1, fmt, ap);
va_end(ap);
Mar 7, 2013
Mar 7, 2013
764
SDL20_SetError("%s", str);
Apr 14, 2013
Apr 14, 2013
765
SDL20_free(str);
Mar 7, 2013
Mar 7, 2013
766
}
Mar 7, 2013
Mar 7, 2013
767
}
Mar 6, 2013
Mar 6, 2013
768
Apr 14, 2013
Apr 14, 2013
769
DECLSPEC const char * SDLCALL
Mar 6, 2013
Mar 6, 2013
770
771
772
773
SDL_GetError(void)
{
if (!Loaded_SDL20)
{
Apr 1, 2013
Apr 1, 2013
774
static const char noload_errstr[] = "Failed to load SDL 2.0 shared library";
Mar 6, 2013
Mar 6, 2013
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
return noload_errstr;
}
return SDL20_GetError();
}
static const char *
GetDriverName(const char *name, char *namebuf, int maxlen)
{
if (name) {
if (namebuf) {
SDL20_strlcpy(namebuf, name, maxlen);
return namebuf;
} else {
return name;
}
}
return NULL;
}
Apr 14, 2013
Apr 14, 2013
795
DECLSPEC const char * SDLCALL
Mar 6, 2013
Mar 6, 2013
796
797
798
799
800
SDL_AudioDriverName(char *namebuf, int maxlen)
{
return GetDriverName(SDL20_GetCurrentAudioDriver(), namebuf, maxlen);
}
Apr 14, 2013
Apr 14, 2013
801
DECLSPEC const char * SDLCALL
Mar 6, 2013
Mar 6, 2013
802
803
804
805
806
SDL_VideoDriverName(char *namebuf, int maxlen)
{
return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
}
Apr 14, 2013
Apr 14, 2013
807
DECLSPEC int SDLCALL
Apr 14, 2013
Apr 14, 2013
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
SDL_PollEvent(SDL12_Event *event12)
{
EventQueueType *next;
SDL20_PumpEvents(); /* this will run our filter and build our 1.2 queue. */
if (EventQueueHead == NULL)
return 0; /* no events at the moment. */
SDL_memcpy(event12, &EventQueueHead->event12, sizeof (SDL12_Event));
next = EventQueueHead->next;
EventQueueHead->next = EventQueueAvailable;
EventQueueAvailable = EventQueueHead;
EventQueueHead = next;
return 1;
}
Apr 14, 2013
Apr 14, 2013
825
DECLSPEC int SDLCALL
Apr 14, 2013
Apr 14, 2013
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
SDL_PushEvent(SDL12_Event *event12)
{
EventQueueType *item = EventQueueAvailable;
if (item == NULL)
return -1; /* no space available at the moment. */
EventQueueAvailable = item->next;
if (EventQueueTail)
EventQueueTail->next = item;
else
EventQueueHead = EventQueueTail = item;
item->next = NULL;
SDL_memcpy(&item->event12, event12, sizeof (SDL12_Event));
return 0;
}
Apr 14, 2013
Apr 14, 2013
843
DECLSPEC int SDLCALL
Apr 14, 2013
Apr 14, 2013
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
SDL_PeepEvents(SDL12_Event *events12, int numevents, SDL_eventaction action, Uint32 mask)
{
if (action == SDL_ADDEVENT)
{
int i;
for (i = 0; i < numevents; i++)
{
if (SDL_PushEvent(&events12[i]) == -1)
break; /* out of space for more events. */
}
return i;
}
else if ((action == SDL_PEEKEVENT) || (action == SDL_GETEVENT))
{
const SDL_bool isGet = (action == SDL_GETEVENT);
EventQueueType *prev = NULL;
EventQueueType *item = EventQueueHead;
EventQueueType *next = NULL;
int chosen = 0;
while (chosen < numevents)
{
EventQueueType *nextPrev = item;
if (!item)
break; /* no more events at the moment. */
next = item->next; /* copy, since we might overwrite item->next */
if (mask & (1<<item->event12.type))
{
SDL_memcpy(&events12[chosen++], &item->event12, sizeof (SDL12_Event));
if (isGet) /* remove from list? */
{
if (prev != NULL)
prev->next = next;
if (item == EventQueueHead)
EventQueueHead = next;
if (item == EventQueueTail)
EventQueueTail = prev;
/* put it back in the free pool. */
item->next = EventQueueAvailable;
EventQueueAvailable = item;
nextPrev = prev; /* previous item doesn't change. */
}
}
item = next;
prev = nextPrev;
}
return chosen;
}
return 0;
}
Apr 14, 2013
Apr 14, 2013
899
DECLSPEC int SDLCALL
Apr 14, 2013
Apr 14, 2013
900
901
902
903
904
905
906
907
SDL_WaitEvent(SDL12_Event *event12)
{
/* In 1.2, this only fails (-1) if you haven't SDL_Init()'d. */
while (!SDL_PollEvent(event12))
SDL_Delay(10);
return 1;
}
Apr 14, 2013
Apr 14, 2013
908
909
910
911
912
913
914
915
916
917
918
919
920
921
static SDL_bool
PushEventIfNotFiltered(SDL12_Event *event12)
{
if (event12->type != SDL12_NOEVENT)
{
if (EventStates[event12->type] != SDL_IGNORE)
{
if ((!EventFilter12) || (EventFilter12(event12)))
return (SDL_PushEvent(event12) == 0);
}
}
return SDL_FALSE;
}
Apr 14, 2013
Apr 14, 2013
922
DECLSPEC Uint8 SDLCALL
Apr 14, 2013
Apr 14, 2013
923
924
925
926
927
928
929
930
931
932
933
934
935
936
SDL_EventState(Uint8 type, int state)
{
/* the values of "state" match between 1.2 and 2.0 */
const Uint8 retval = EventStates[type];
SDL12_Event e;
if (state != SDL_QUERY)
EventStates[type] = state;
if (state == SDL_IGNORE) /* drop existing events of this type. */
while (SDL_PeepEvents(&e, 1, SDL_GETEVENT, (1<<type))) {}
return retval;
}
Apr 14, 2013
Apr 14, 2013
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
DECLSPEC Uint8 SDLCALL
SDL_GetMouseState(int *x, int *y)
{
const Uint32 state20 = SDL20_GetMouseState(x, y);
Uint8 retval = (state20 & 0x7); /* left, right, and middle will match. */
/* the X[12] buttons are different in 1.2; mousewheel was in the way. */
if (state20 & SDL_BUTTON(SDL_BUTTON_X1))
retval |= (1<<5);
if (state20 & SDL_BUTTON(SDL_BUTTON_X2))
retval |= (1<<6);
return retval;
}
Apr 1, 2013
Apr 1, 2013
952
953
static int
EventFilter20to12(void *data, SDL_Event *event20)
Mar 8, 2013
Mar 8, 2013
954
{
Jul 26, 2014
Jul 26, 2014
955
//const int maxUserEvents12 = SDL12_NUMEVENTS - SDL12_USEREVENT;
Apr 1, 2013
Apr 1, 2013
956
SDL12_Event event12;
Apr 14, 2013
Apr 14, 2013
957
int x, y;
Apr 1, 2013
Apr 1, 2013
958
959
960
SDL_assert(data == NULL); /* currently unused. */
Apr 14, 2013
Apr 14, 2013
961
SDL20_zero(event12);
Apr 1, 2013
Apr 1, 2013
962
963
964
965
switch (event20->type)
{
case SDL_QUIT:
Apr 14, 2013
Apr 14, 2013
966
event12.type = SDL12_QUIT;
Apr 1, 2013
Apr 1, 2013
967
968
969
970
971
972
break;
case SDL_WINDOWEVENT:
switch (event20->window.event)
{
case SDL_WINDOWEVENT_CLOSE:
Apr 14, 2013
Apr 14, 2013
973
event12.type = SDL12_QUIT;
Apr 1, 2013
Apr 1, 2013
974
975
976
977
break;
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_EXPOSED:
Apr 14, 2013
Apr 14, 2013
978
event12.type = SDL12_VIDEOEXPOSE;
Apr 1, 2013
Apr 1, 2013
979
980
981
982
break;
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_SIZE_CHANGED: // !!! FIXME: what's the difference between RESIZED and SIZE_CHANGED?
Apr 14, 2013
Apr 14, 2013
983
984
985
event12.type = SDL12_VIDEORESIZE;
event12.resize.w = event20->window.data1;
event12.resize.h = event20->window.data2;
Apr 1, 2013
Apr 1, 2013
986
987
988
break;
case SDL_WINDOWEVENT_MINIMIZED:
Apr 14, 2013
Apr 14, 2013
989
990
991
event12.type = SDL12_ACTIVEEVENT;
event12.active.gain = 0;
event12.active.state = SDL12_APPACTIVE;
Apr 1, 2013
Apr 1, 2013
992
993
994
break;
case SDL_WINDOWEVENT_RESTORED:
Apr 14, 2013
Apr 14, 2013
995
996
997
event12.type = SDL12_ACTIVEEVENT;
event12.active.gain = 1;
event12.active.state = SDL12_APPACTIVE;
Apr 1, 2013
Apr 1, 2013
998
999
1000
break;
case SDL_WINDOWEVENT_ENTER: