Skip to content

Latest commit

 

History

History
2159 lines (1914 loc) · 60.9 KB

SDL12_compat.c

File metadata and controls

2159 lines (1914 loc) · 60.9 KB
 
Mar 4, 2013
Mar 4, 2013
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
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 */
#include "SDL.h"
#if !SDL_VERSION_ATLEAST(2,0,0)
#error You need to compile against SDL 2.0 headers.
#endif
#include "SDL_syswm.h"
//#include "video/SDL_sysvideo.h"
//#include "video/SDL_pixels_c.h"
//#include "render/SDL_yuv_sw_c.h"
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
// !!! IMPLEMENT_ME SDL_AddTimer
// !!! IMPLEMENT_ME SDL_AllocRW
// !!! IMPLEMENT_ME SDL_AudioInit
// !!! IMPLEMENT_ME SDL_AudioQuit
// !!! IMPLEMENT_ME SDL_ConvertSurface
// !!! IMPLEMENT_ME SDL_CreateCursor
// !!! IMPLEMENT_ME SDL_CreateRGBSurface
// !!! IMPLEMENT_ME SDL_CreateRGBSurfaceFrom
// !!! IMPLEMENT_ME SDL_CreateThread
// !!! IMPLEMENT_ME SDL_Error
// !!! IMPLEMENT_ME SDL_EventState
// !!! IMPLEMENT_ME SDL_FillRect
// !!! IMPLEMENT_ME SDL_FreeCursor
// !!! IMPLEMENT_ME SDL_FreeRW
// !!! IMPLEMENT_ME SDL_FreeSurface
// !!! IMPLEMENT_ME SDL_GL_GetAttribute
// !!! IMPLEMENT_ME SDL_GL_GetProcAddress
// !!! IMPLEMENT_ME SDL_GL_LoadLibrary
// !!! IMPLEMENT_ME SDL_GL_Lock
// !!! IMPLEMENT_ME SDL_GL_SetAttribute
// !!! IMPLEMENT_ME SDL_GL_Unlock
// !!! IMPLEMENT_ME SDL_GL_UpdateRects
// !!! IMPLEMENT_ME SDL_GetClipRect
// !!! IMPLEMENT_ME SDL_GetCursor
// !!! IMPLEMENT_ME SDL_GetEventFilter
// !!! IMPLEMENT_ME SDL_GetKeyName
// !!! IMPLEMENT_ME SDL_GetKeyState
// !!! IMPLEMENT_ME SDL_GetModState
// !!! IMPLEMENT_ME SDL_GetMouseState
// !!! IMPLEMENT_ME SDL_GetRGB
// !!! IMPLEMENT_ME SDL_GetRGBA
// !!! IMPLEMENT_ME SDL_GetRelativeMouseState
// !!! IMPLEMENT_ME SDL_GetThreadID
// !!! IMPLEMENT_ME SDL_Init
// !!! IMPLEMENT_ME SDL_InitSubSystem
// !!! IMPLEMENT_ME SDL_LoadBMP_RW
// !!! IMPLEMENT_ME SDL_LoadWAV_RW
// !!! IMPLEMENT_ME SDL_LockSurface
// !!! IMPLEMENT_ME SDL_LowerBlit
// !!! IMPLEMENT_ME SDL_MapRGB
// !!! IMPLEMENT_ME SDL_MapRGBA
// !!! IMPLEMENT_ME SDL_PeepEvents
// !!! IMPLEMENT_ME SDL_PollEvent
// !!! IMPLEMENT_ME SDL_PumpEvents
// !!! IMPLEMENT_ME SDL_PushEvent
// !!! IMPLEMENT_ME SDL_QuitSubSystem
// !!! IMPLEMENT_ME SDL_RWFromConstMem
// !!! IMPLEMENT_ME SDL_RWFromFP
// !!! IMPLEMENT_ME SDL_RWFromFile
// !!! IMPLEMENT_ME SDL_RWFromMem
// !!! IMPLEMENT_ME SDL_ReadBE16
// !!! IMPLEMENT_ME SDL_ReadBE32
// !!! IMPLEMENT_ME SDL_ReadBE64
// !!! IMPLEMENT_ME SDL_ReadLE16
// !!! IMPLEMENT_ME SDL_ReadLE32
// !!! IMPLEMENT_ME SDL_ReadLE64
// !!! IMPLEMENT_ME SDL_RemoveTimer
// !!! IMPLEMENT_ME SDL_SaveBMP_RW
// !!! IMPLEMENT_ME SDL_SetClipRect
// !!! IMPLEMENT_ME SDL_SetColorKey
// !!! IMPLEMENT_ME SDL_SetCursor
// !!! IMPLEMENT_ME SDL_SetError
// !!! IMPLEMENT_ME SDL_SetEventFilter
// !!! IMPLEMENT_ME SDL_SetModState
// !!! IMPLEMENT_ME SDL_ShowCursor
// !!! IMPLEMENT_ME SDL_SoftStretch
// !!! IMPLEMENT_ME SDL_UnlockSurface
// !!! IMPLEMENT_ME SDL_UpperBlit
// !!! IMPLEMENT_ME SDL_VideoInit
// !!! IMPLEMENT_ME SDL_VideoQuit
// !!! IMPLEMENT_ME SDL_WaitEvent
// !!! IMPLEMENT_ME SDL_WasInit
// !!! IMPLEMENT_ME SDL_WriteBE16
// !!! IMPLEMENT_ME SDL_WriteBE32
// !!! IMPLEMENT_ME SDL_WriteBE64
// !!! IMPLEMENT_ME SDL_WriteLE16
// !!! IMPLEMENT_ME SDL_WriteLE32
// !!! IMPLEMENT_ME SDL_WriteLE64
// !!! IMPLEMENT_ME SDL_iconv
// !!! IMPLEMENT_ME SDL_iconv_string
// !!! IMPLEMENT_ME SDL_lltoa
// !!! IMPLEMENT_ME SDL_ltoa
// !!! IMPLEMENT_ME SDL_revcpy
// !!! IMPLEMENT_ME SDL_strlcat
// !!! IMPLEMENT_ME SDL_strlcpy
// !!! IMPLEMENT_ME SDL_strlwr
// !!! IMPLEMENT_ME SDL_strrev
// !!! IMPLEMENT_ME SDL_strupr
// !!! IMPLEMENT_ME SDL_ulltoa
// !!! IMPLEMENT_ME SDL_ultoa
// !!! IMPLEMENT_ME X11_KeyToUnicode
Mar 4, 2013
Mar 4, 2013
127
Mar 6, 2013
Mar 6, 2013
128
Mar 6, 2013
Mar 6, 2013
129
130
131
#define SDL20_SYM(rc,fn,params,args,ret) \
typedef rc (*SDL20_##fn##_t) params; \
static SDL20_##fn##_t SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
132
133
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
134
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
135
#undef SDL20_SYM_PASSTHROUGH
Mar 6, 2013
Mar 6, 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
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
#undef SDL20_SYM
#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
typedef struct
{
Uint32 hw_available :1; /**< Flag: Can you create hardware surfaces? */
Uint32 wm_available :1; /**< Flag: Can you talk to a window manager? */
Uint32 UnusedBits1 :6;
Uint32 UnusedBits2 :1;
Uint32 blit_hw :1; /**< Flag: Accelerated blits HW --> HW */
Uint32 blit_hw_CC :1; /**< Flag: Accelerated blits with Colorkey */
Uint32 blit_hw_A :1; /**< Flag: Accelerated blits with Alpha */
Uint32 blit_sw :1; /**< Flag: Accelerated blits SW --> HW */
Uint32 blit_sw_CC :1; /**< Flag: Accelerated blits with Colorkey */
Uint32 blit_sw_A :1; /**< Flag: Accelerated blits with Alpha */
Uint32 blit_fill :1; /**< Flag: Accelerated color fill */
Uint32 UnusedBits3 :16;
Uint32 video_mem; /**< The total amount of video memory (in K) */
SDL_PixelFormat *vfmt; /**< Value: The format of the video surface */
int current_w; /**< Value: The current video mode width */
int current_h; /**< Value: The current video mode height */
} SDL12_VideoInfo;
typedef struct SDL12_Palette {
int ncolors;
SDL_Color *colors;
} SDL12_Palette;
/** Everything in the pixel format structure is read-only */
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;
/** RGB color key information */
Uint32 colorkey;
/** Alpha value information (per-surface alpha) */
Uint8 alpha;
} SDL12_PixelFormat;
#define SDL12_SWSURFACE 0x00000000 /**< Surface is in system memory */
#define SDL12_HWSURFACE 0x00000001 /**< Surface is in video memory */
#define SDL12_ASYNCBLIT 0x00000004 /**< Use asynchronous blits if possible */
#define SDL12_ANYFORMAT 0x10000000 /**< Allow any video depth/pixel-format */
#define SDL12_HWPALETTE 0x20000000 /**< Surface has exclusive palette */
#define SDL12_DOUBLEBUF 0x40000000 /**< Set up double-buffered video mode */
#define SDL12_FULLSCREEN 0x80000000 /**< Surface is a full screen display */
#define SDL12_OPENGL 0x00000002 /**< Create an OpenGL rendering context */
#define SDL12_OPENGLBLIT 0x0000000A /**< Create an OpenGL rendering context and use it for blitting */
#define SDL12_RESIZABLE 0x00000010 /**< This video mode may be resized */
#define SDL12_NOFRAME 0x00000020 /**< No window caption or edge frame */
#define SDL12_HWACCEL 0x00000100 /**< Blit uses hardware acceleration */
#define SDL12_SRCCOLORKEY 0x00001000 /**< Blit uses a source color key */
#define SDL12_RLEACCELOK 0x00002000 /**< Private flag */
#define SDL12_RLEACCEL 0x00004000 /**< Surface is RLE encoded */
#define SDL12_SRCALPHA 0x00010000 /**< Blit uses source alpha blending */
#define SDL12_PREALLOC 0x01000000 /**< Surface uses preallocated memory */
Mar 4, 2013
Mar 4, 2013
218
219
220
221
222
223
224
225
226
227
static SDL_Window *SDL_VideoWindow = NULL;
static SDL_Surface *SDL_WindowSurface = NULL;
static SDL_Surface *SDL_VideoSurface = NULL;
static SDL_Surface *SDL_ShadowSurface = NULL;
static SDL_Surface *SDL_PublicSurface = NULL;
static SDL_GLContext *SDL_VideoContext = NULL;
static Uint32 SDL_VideoFlags = 0;
static SDL_Rect SDL_VideoViewport;
static char *wm_title = NULL;
Mar 6, 2013
Mar 6, 2013
228
static char *wm_icon_caption = NULL;
Mar 4, 2013
Mar 4, 2013
229
230
static SDL_Surface *SDL_VideoIcon;
static int SDL_enabled_UNICODE = 0;
Mar 6, 2013
Mar 6, 2013
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
static int SDL_VideoDisplayIndex = 0;
/* Obviously we can't use SDL_LoadObject() to load SDL2. :) */
#if defined(_WINDOWS)
#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; } }
#elif defined(unix)
#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
253
Mar 6, 2013
Mar 6, 2013
254
255
static void *
LoadSDL20Symbol(const char *fn, int *okay)
Mar 4, 2013
Mar 4, 2013
256
{
Mar 6, 2013
Mar 6, 2013
257
258
259
260
261
262
263
264
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
265
266
}
Mar 6, 2013
Mar 6, 2013
267
268
static void
UnloadSDL20(void)
Mar 4, 2013
Mar 4, 2013
269
{
Mar 6, 2013
Mar 6, 2013
270
#define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
271
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_##fn = NULL;
Mar 6, 2013
Mar 6, 2013
272
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
273
#undef SDL20_SYM_PASSTHROUGH
Mar 6, 2013
Mar 6, 2013
274
275
276
277
278
279
280
281
282
283
284
285
#undef SDL20_SYM
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
286
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_SYM(rc,fn,params,args,ret)
Mar 6, 2013
Mar 6, 2013
287
#include "SDL20_syms.h"
Mar 6, 2013
Mar 6, 2013
288
#undef SDL20_SYM_PASSTHROUGH
Mar 6, 2013
Mar 6, 2013
289
290
291
#undef SDL20_SYM
if (!okay)
UnloadSDL20();
Mar 4, 2013
Mar 4, 2013
292
}
Mar 6, 2013
Mar 6, 2013
293
return okay;
Mar 4, 2013
Mar 4, 2013
294
295
}
Mar 6, 2013
Mar 6, 2013
296
Mar 4, 2013
Mar 4, 2013
297
298
299
static int
GetVideoDisplay()
{
Mar 6, 2013
Mar 6, 2013
300
// !!! FIXME: cache this value during SDL_Init() so it doesn't change.
Mar 4, 2013
Mar 4, 2013
301
302
303
304
305
306
307
308
309
310
311
const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
if ( !variable ) {
variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
}
if ( variable ) {
return SDL_atoi(variable);
} else {
return 0;
}
}
Mar 6, 2013
Mar 6, 2013
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
int
SDL_Init(Uint32 sdl12flags)
{
Uint32 sdl20flags = 0;
int rc;
if (!LoadSDL20())
return -1;
#define SETFLAG(x) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag)
SETFLAG(TIMER);
SETFLAG(AUDIO);
SETFLAG(VIDEO);
SETFLAG(JOYSTICK);
SETFLAG(NOPARACHUTE);
// There's no CDROM in 2.0, but we'll just pretend it succeeded.
#undef SETFLAG
// !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
rc = SDL20_Init(sdl20flags);
if ((rc == 0) && (sdl20flags & SDL_INIT_VIDEO))
SDL_VideoDisplayIndex = GetVideoDisplay();
return rc;
}
Mar 6, 2013
Mar 6, 2013
338
339
340
341
342
343
344
345
void
SDL_Quit(void)
{
SDL20_Quit();
UnloadSDL20();
}
Mar 6, 2013
Mar 6, 2013
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
char *
SDL_GetError(void)
{
if (!Loaded_SDL20)
{
static char noload_errstr[] = "Failed to load SDL 2.0 shared library";
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;
}
const char *
SDL_AudioDriverName(char *namebuf, int maxlen)
{
return GetDriverName(SDL20_GetCurrentAudioDriver(), namebuf, maxlen);
}
const char *
SDL_VideoDriverName(char *namebuf, int maxlen)
{
return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
}
const SDL12_VideoInfo *
Mar 4, 2013
Mar 4, 2013
385
386
SDL_GetVideoInfo(void)
{
Mar 6, 2013
Mar 6, 2013
387
static SDL12_VideoInfo info;
Mar 4, 2013
Mar 4, 2013
388
389
390
SDL_DisplayMode mode;
/* !!! FIXME: Memory leak, compatibility code, who cares? */
Mar 6, 2013
Mar 6, 2013
391
392
if (!info.vfmt && SDL20_GetDesktopDisplayMode(SDL_VideoDisplayIndex, &mode) == 0) {
info.vfmt = SDL20_AllocFormat(mode.format);
Mar 4, 2013
Mar 4, 2013
393
394
info.current_w = mode.w;
info.current_h = mode.h;
Mar 6, 2013
Mar 6, 2013
395
396
397
// !!! FIXME
//info.wm_available = 1;
//info.video_mem = 1024 * 256;
Mar 4, 2013
Mar 4, 2013
398
399
400
401
402
}
return &info;
}
int
Mar 6, 2013
Mar 6, 2013
403
SDL_VideoModeOK(int width, int height, int bpp, Uint32 sdl12flags)
Mar 4, 2013
Mar 4, 2013
404
{
Mar 6, 2013
Mar 6, 2013
405
int i, nummodes, actual_bpp = 0;
Mar 4, 2013
Mar 4, 2013
406
Mar 6, 2013
Mar 6, 2013
407
if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
Mar 4, 2013
Mar 4, 2013
408
409
410
return 0;
}
Mar 6, 2013
Mar 6, 2013
411
if (!(sdl12flags & SDL12_FULLSCREEN)) {
Mar 4, 2013
Mar 4, 2013
412
SDL_DisplayMode mode;
Mar 6, 2013
Mar 6, 2013
413
SDL20_GetDesktopDisplayMode(SDL_VideoDisplayIndex, &mode);
Mar 4, 2013
Mar 4, 2013
414
415
416
return SDL_BITSPERPIXEL(mode.format);
}
Mar 6, 2013
Mar 6, 2013
417
418
nummodes = SDL20_GetNumDisplayModes(SDL_VideoDisplayIndex);
for (i = 0; i < nummodes; ++i) {
Mar 4, 2013
Mar 4, 2013
419
SDL_DisplayMode mode;
Mar 6, 2013
Mar 6, 2013
420
SDL20_GetDisplayMode(SDL_VideoDisplayIndex, i, &mode);
Mar 4, 2013
Mar 4, 2013
421
422
423
424
425
426
427
428
429
430
431
432
433
if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
if (!mode.format) {
return bpp;
}
if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
actual_bpp = SDL_BITSPERPIXEL(mode.format);
}
}
}
return actual_bpp;
}
SDL_Rect **
Mar 6, 2013
Mar 6, 2013
434
SDL_ListModes(const SDL12_PixelFormat * format, Uint32 flags)
Mar 4, 2013
Mar 4, 2013
435
436
437
438
{
int i, nmodes;
SDL_Rect **modes;
Mar 6, 2013
Mar 6, 2013
439
if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
Mar 4, 2013
Mar 4, 2013
440
441
442
return NULL;
}
Mar 6, 2013
Mar 6, 2013
443
if (!(flags & SDL12_FULLSCREEN)) {
Mar 4, 2013
Mar 4, 2013
444
445
446
447
448
449
450
451
452
453
return (SDL_Rect **) (-1);
}
if (!format) {
format = SDL_GetVideoInfo()->vfmt;
}
/* Memory leak, but this is a compatibility function, who cares? */
nmodes = 0;
modes = NULL;
Mar 6, 2013
Mar 6, 2013
454
for (i = 0; i < SDL20_GetNumDisplayModes(SDL_VideoDisplayIndex); ++i) {
Mar 4, 2013
Mar 4, 2013
455
456
457
SDL_DisplayMode mode;
int bpp;
Mar 6, 2013
Mar 6, 2013
458
SDL20_GetDisplayMode(SDL_VideoDisplayIndex, i, &mode);
Mar 4, 2013
Mar 4, 2013
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
if (!mode.w || !mode.h) {
return (SDL_Rect **) (-1);
}
/* Copied from src/video/SDL_pixels.c:SDL_PixelFormatEnumToMasks */
if (SDL_BYTESPERPIXEL(mode.format) <= 2) {
bpp = SDL_BITSPERPIXEL(mode.format);
} else {
bpp = SDL_BYTESPERPIXEL(mode.format) * 8;
}
if (bpp != format->BitsPerPixel) {
continue;
}
if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
&& modes[nmodes - 1]->h == mode.h) {
continue;
}
modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
if (!modes) {
return NULL;
}
modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
if (!modes[nmodes]) {
return NULL;
}
modes[nmodes]->x = 0;
modes[nmodes]->y = 0;
modes[nmodes]->w = mode.w;
modes[nmodes]->h = mode.h;
++nmodes;
}
if (modes) {
modes[nmodes] = NULL;
}
return modes;
}
static int
SDL_CompatEventFilter(void *userdata, SDL_Event * event)
{
SDL_Event fake;
switch (event->type) {
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:
if (!SDL_HasEvent(SDL_VIDEOEXPOSE)) {
fake.type = SDL_VIDEOEXPOSE;
SDL_PushEvent(&fake);
}
break;
case SDL_WINDOWEVENT_RESIZED:
SDL_FlushEvent(SDL_VIDEORESIZE);
/* We don't want to expose that the window width and height will
be different if we don't get the desired fullscreen mode.
*/
if (SDL_VideoWindow && !(SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN)) {
fake.type = SDL_VIDEORESIZE;
fake.resize.w = event->window.data1;
fake.resize.h = event->window.data2;
SDL_PushEvent(&fake);
}
break;
case SDL_WINDOWEVENT_MINIMIZED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPACTIVE;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_RESTORED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPACTIVE;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_ENTER:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPMOUSEFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_LEAVE:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPMOUSEFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPINPUTFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPINPUTFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_CLOSE:
fake.type = SDL_QUIT;
SDL_PushEvent(&fake);
break;
}
case SDL_KEYDOWN:
case SDL_KEYUP:
{
Uint32 unicode = 0;
if (event->key.type == SDL_KEYDOWN && event->key.keysym.sym < 256) {
unicode = event->key.keysym.sym;
if (unicode >= 'a' && unicode <= 'z') {
int shifted = !!(event->key.keysym.mod & KMOD_SHIFT);
int capslock = !!(event->key.keysym.mod & KMOD_CAPS);
if ((shifted ^ capslock) != 0) {
unicode = SDL_toupper(unicode);
}
}
}
if (unicode) {
event->key.keysym.unicode = unicode;
}
break;
}
case SDL_TEXTINPUT:
{
/* FIXME: Generate an old style key repeat event if needed */
//printf("TEXTINPUT: '%s'\n", event->text.text);
break;
}
case SDL_MOUSEMOTION:
{
event->motion.x -= SDL_VideoViewport.x;
event->motion.y -= SDL_VideoViewport.y;
break;
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
event->button.x -= SDL_VideoViewport.x;
event->button.y -= SDL_VideoViewport.y;
break;
}
case SDL_MOUSEWHEEL:
{
Uint8 button;
int x, y;
if (event->wheel.y == 0) {
break;
}
SDL_GetMouseState(&x, &y);
if (event->wheel.y > 0) {
button = SDL_BUTTON_WHEELUP;
} else {
button = SDL_BUTTON_WHEELDOWN;
}
fake.button.button = button;
fake.button.x = x;
fake.button.y = y;
fake.button.windowID = event->wheel.windowID;
fake.type = SDL_MOUSEBUTTONDOWN;
fake.button.state = SDL_PRESSED;
SDL_PushEvent(&fake);
fake.type = SDL_MOUSEBUTTONUP;
fake.button.state = SDL_RELEASED;
SDL_PushEvent(&fake);
break;
}
}
return 1;
}
static void
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
{
Mar 6, 2013
Mar 6, 2013
642
int display = SDL_VideoDisplayIndex;
Mar 4, 2013
Mar 4, 2013
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
const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
if (window) {
if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
return;
}
if (SDL_strcmp(window, "center") == 0) {
center = window;
}
}
if (center) {
*x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
*y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
}
}
static void
ClearVideoSurface()
{
if (SDL_ShadowSurface) {
SDL_FillRect(SDL_ShadowSurface, NULL,
SDL_MapRGB(SDL_ShadowSurface->format, 0, 0, 0));
}
SDL_FillRect(SDL_WindowSurface, NULL, 0);
SDL_UpdateWindowSurface(SDL_VideoWindow);
}
static void
SetupScreenSaver(int flags)
{
const char *env;
SDL_bool allow_screensaver;
/* Allow environment override of screensaver disable */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
if (env) {
allow_screensaver = SDL_atoi(env) ? SDL_TRUE : SDL_FALSE;
} else if (flags & SDL_FULLSCREEN) {
allow_screensaver = SDL_FALSE;
} else {
allow_screensaver = SDL_TRUE;
}
if (allow_screensaver) {
SDL_EnableScreenSaver();
} else {
SDL_DisableScreenSaver();
}
}
static int
SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
{
int w, h;
/* We can't resize something we don't have... */
if (!SDL_VideoSurface) {
return -1;
}
/* We probably have to recreate the window in fullscreen mode */
if (flags & SDL_FULLSCREEN) {
return -1;
}
/* I don't think there's any change we can gracefully make in flags */
if (flags != SDL_VideoFlags) {
return -1;
}
if (bpp != SDL_VideoSurface->format->BitsPerPixel) {
return -1;
}
/* Resize the window */
SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
if (w != width || h != height) {
SDL_SetWindowSize(SDL_VideoWindow, width, height);
}
/* If we're in OpenGL mode, just resize the stub surface and we're done! */
if (flags & SDL_OPENGL) {
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
return 0;
}
SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow);
if (!SDL_WindowSurface) {
return -1;
}
if (SDL_VideoSurface->format != SDL_WindowSurface->format) {
return -1;
}
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
SDL_VideoSurface->pixels = SDL_WindowSurface->pixels;
SDL_VideoSurface->pitch = SDL_WindowSurface->pitch;
SDL_SetClipRect(SDL_VideoSurface, NULL);
if (SDL_ShadowSurface) {
SDL_ShadowSurface->w = width;
SDL_ShadowSurface->h = height;
SDL_ShadowSurface->pitch = SDL_CalculatePitch(SDL_ShadowSurface);
SDL_ShadowSurface->pixels =
SDL_realloc(SDL_ShadowSurface->pixels,
SDL_ShadowSurface->h * SDL_ShadowSurface->pitch);
SDL_SetClipRect(SDL_ShadowSurface, NULL);
SDL_InvalidateMap(SDL_ShadowSurface->map);
} else {
SDL_PublicSurface = SDL_VideoSurface;
}
ClearVideoSurface();
return 0;
}
SDL_Surface *
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
{
SDL_DisplayMode desktop_mode;
Mar 6, 2013
Mar 6, 2013
763
int display = SDL_VideoDisplayIndex;
Mar 4, 2013
Mar 4, 2013
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
int window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
int window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
int window_w;
int window_h;
Uint32 window_flags;
Uint32 surface_flags;
if (!SDL_GetVideoDevice()) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
return NULL;
}
}
SDL_GetDesktopDisplayMode(display, &desktop_mode);
if (width == 0) {
width = desktop_mode.w;
}
if (height == 0) {
height = desktop_mode.h;
}
if (bpp == 0) {
bpp = SDL_BITSPERPIXEL(desktop_mode.format);
}
/* See if we can simply resize the existing window and surface */
if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
return SDL_PublicSurface;
}
/* Destroy existing window */
SDL_PublicSurface = NULL;
if (SDL_ShadowSurface) {
SDL_ShadowSurface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(SDL_ShadowSurface);
SDL_ShadowSurface = NULL;
}
if (SDL_VideoSurface) {
SDL_VideoSurface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(SDL_VideoSurface);
SDL_VideoSurface = NULL;
}
if (SDL_VideoContext) {
/* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
SDL_GL_DeleteContext(SDL_VideoContext);
SDL_VideoContext = NULL;
}
if (SDL_VideoWindow) {
SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
SDL_DestroyWindow(SDL_VideoWindow);
}
/* Set up the event filter */
if (!SDL_GetEventFilter(NULL, NULL)) {
SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
}
/* Create a new window */
window_flags = SDL_WINDOW_SHOWN;
if (flags & SDL_FULLSCREEN) {
window_flags |= SDL_WINDOW_FULLSCREEN;
}
if (flags & SDL_OPENGL) {
window_flags |= SDL_WINDOW_OPENGL;
}
if (flags & SDL_RESIZABLE) {
window_flags |= SDL_WINDOW_RESIZABLE;
}
if (flags & SDL_NOFRAME) {
window_flags |= SDL_WINDOW_BORDERLESS;
}
GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
SDL_VideoWindow =
SDL_CreateWindow(wm_title, window_x, window_y, width, height,
window_flags);
if (!SDL_VideoWindow) {
return NULL;
}
SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon);
SetupScreenSaver(flags);
window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
surface_flags = 0;
if (window_flags & SDL_WINDOW_FULLSCREEN) {
surface_flags |= SDL_FULLSCREEN;
}
if ((window_flags & SDL_WINDOW_OPENGL) && (flags & SDL_OPENGL)) {
surface_flags |= SDL_OPENGL;
}
if (window_flags & SDL_WINDOW_RESIZABLE) {
surface_flags |= SDL_RESIZABLE;
}
if (window_flags & SDL_WINDOW_BORDERLESS) {
surface_flags |= SDL_NOFRAME;
}
SDL_VideoFlags = flags;
/* If we're in OpenGL mode, just create a stub surface and we're done! */
if (flags & SDL_OPENGL) {
SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
if (!SDL_VideoContext) {
return NULL;
}
if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
return NULL;
}
SDL_VideoSurface =
SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
if (!SDL_VideoSurface) {
return NULL;
}
SDL_VideoSurface->flags |= surface_flags;
SDL_PublicSurface = SDL_VideoSurface;
return SDL_PublicSurface;
}
/* Create the screen surface */
SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow);
if (!SDL_WindowSurface) {
return NULL;
}
/* Center the public surface in the window surface */
SDL_GetWindowSize(SDL_VideoWindow, &window_w, &window_h);
SDL_VideoViewport.x = (window_w - width)/2;
SDL_VideoViewport.y = (window_h - height)/2;
SDL_VideoViewport.w = width;
SDL_VideoViewport.h = height;
SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
SDL_VideoSurface->flags |= surface_flags;
SDL_VideoSurface->flags |= SDL_DONTFREE;
SDL_FreeFormat(SDL_VideoSurface->format);
SDL_VideoSurface->format = SDL_WindowSurface->format;
SDL_VideoSurface->format->refcount++;
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
SDL_VideoSurface->pitch = SDL_WindowSurface->pitch;
SDL_VideoSurface->pixels = (void *)((Uint8 *)SDL_WindowSurface->pixels +
SDL_VideoViewport.y * SDL_VideoSurface->pitch +
SDL_VideoViewport.x * SDL_VideoSurface->format->BytesPerPixel);
SDL_SetClipRect(SDL_VideoSurface, NULL);
/* Create a shadow surface if necessary */
if ((bpp != SDL_VideoSurface->format->BitsPerPixel)
&& !(flags & SDL_ANYFORMAT)) {
SDL_ShadowSurface =
SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
if (!SDL_ShadowSurface) {
return NULL;
}
SDL_ShadowSurface->flags |= surface_flags;
SDL_ShadowSurface->flags |= SDL_DONTFREE;
/* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
if (SDL_ShadowSurface->format->palette) {
SDL_ShadowSurface->flags |= SDL_HWPALETTE;
SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
SDL_ShadowSurface->format->BitsPerPixel);
}
SDL_FillRect(SDL_ShadowSurface, NULL,
SDL_MapRGB(SDL_ShadowSurface->format, 0, 0, 0));
}
SDL_PublicSurface =
(SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
ClearVideoSurface();
/* We're finally done! */
return SDL_PublicSurface;
}
SDL_Surface *
SDL_GetVideoSurface(void)
{
return SDL_PublicSurface;
}
int
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
{
if (flag & SDL_SRCALPHA) {
/* According to the docs, value is ignored for alpha surfaces */
if (surface->format->Amask) {
value = 0xFF;
}
SDL_SetSurfaceAlphaMod(surface, value);
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
} else {
SDL_SetSurfaceAlphaMod(surface, 0xFF);
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
}
SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
return 0;
}
SDL_Surface *
SDL_DisplayFormat(SDL_Surface * surface)
{
SDL_PixelFormat *format;
if (!SDL_PublicSurface) {
SDL_SetError("No video mode has been set");
return NULL;
}
format = SDL_PublicSurface->format;
/* Set the flags appropriate for copying to display surface */
return SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
}
SDL_Surface *
SDL_DisplayFormatAlpha(SDL_Surface * surface)
{
SDL_PixelFormat *vf;
SDL_PixelFormat *format;
SDL_Surface *converted;
/* default to ARGB8888 */
Uint32 amask = 0xff000000;
Uint32 rmask = 0x00ff0000;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x000000ff;
if (!SDL_PublicSurface) {
SDL_SetError("No video mode has been set");
return NULL;
}
vf = SDL_PublicSurface->format;
switch (vf->BytesPerPixel) {
case 2:
/* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
For anything else (like ARGB4444) it doesn't matter
since we have no special code for it anyway */