src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 26 Jul 2014 00:55:20 -0400
changeset 40 9f165553c592
parent 39 354ef2d7f999
child 41 f9d92daf587f
permissions -rw-r--r--
Fail unconditionally on SDL_INIT_EVENTTHREAD for now.
icculus@9
     1
/*
icculus@9
     2
  Simple DirectMedia Layer
icculus@9
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
icculus@9
     4
icculus@9
     5
  This software is provided 'as-is', without any express or implied
icculus@9
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@9
     7
  arising from the use of this software.
icculus@9
     8
icculus@9
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@9
    10
  including commercial applications, and to alter it and redistribute it
icculus@9
    11
  freely, subject to the following restrictions:
icculus@9
    12
icculus@9
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@9
    14
     claim that you wrote the original software. If you use this software
icculus@9
    15
     in a product, an acknowledgment in the product documentation would be
icculus@9
    16
     appreciated but is not required.
icculus@9
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@9
    18
     misrepresented as being the original software.
icculus@9
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@9
    20
*/
icculus@9
    21
icculus@9
    22
/* This file contains functions for backwards compatibility with SDL 1.2 */
icculus@9
    23
icculus@31
    24
#include "SDL20_include_wrapper.h"
icculus@9
    25
icculus@9
    26
#if !SDL_VERSION_ATLEAST(2,0,0)
icculus@9
    27
#error You need to compile against SDL 2.0 headers.
icculus@9
    28
#endif
icculus@9
    29
icculus@33
    30
/*
icculus@33
    31
 * We report the library version as 1.2.$(SDL12_COMPAT_VERSION). This number
icculus@33
    32
 *  should be way ahead of what SDL-1.2 Classic would report, so apps can
icculus@33
    33
 *  decide if they're running under the compat layer, if they really care.
icculus@33
    34
 */
icculus@33
    35
#define SDL12_COMPAT_VERSION 50
icculus@33
    36
icculus@11
    37
#include <stdarg.h>
icculus@11
    38
icculus@9
    39
// !!! IMPLEMENT_ME SDL_ConvertSurface
icculus@9
    40
// !!! IMPLEMENT_ME SDL_GetKeyName
icculus@9
    41
// !!! IMPLEMENT_ME SDL_GetKeyState
icculus@9
    42
// !!! IMPLEMENT_ME SDL_GetModState
icculus@9
    43
// !!! IMPLEMENT_ME SDL_GetRelativeMouseState
icculus@9
    44
// !!! IMPLEMENT_ME SDL_LockSurface
icculus@9
    45
// !!! IMPLEMENT_ME SDL_LowerBlit
icculus@9
    46
// !!! IMPLEMENT_ME SDL_SetColorKey
icculus@9
    47
// !!! IMPLEMENT_ME SDL_SetModState
icculus@9
    48
// !!! IMPLEMENT_ME SDL_SoftStretch
icculus@9
    49
// !!! IMPLEMENT_ME SDL_UnlockSurface
icculus@9
    50
// !!! IMPLEMENT_ME SDL_UpperBlit
icculus@9
    51
// !!! IMPLEMENT_ME X11_KeyToUnicode
icculus@9
    52
icculus@9
    53
icculus@9
    54
#define SDL20_SYM(rc,fn,params,args,ret) \
icculus@35
    55
    typedef rc (SDLCALL *SDL20_##fn##_t) params; \
icculus@9
    56
    static SDL20_##fn##_t SDL20_##fn = NULL;
icculus@9
    57
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
icculus@9
    58
    SDL20_SYM(rc,fn,params,args,ret)
icculus@9
    59
#include "SDL20_syms.h"
icculus@9
    60
#undef SDL20_SYM_PASSTHROUGH
icculus@9
    61
#undef SDL20_SYM
icculus@9
    62
icculus@35
    63
typedef int (SDLCALL *SDL20_SetError_t)(const char *fmt, ...);
icculus@11
    64
static SDL20_SetError_t SDL20_SetError = NULL;
icculus@9
    65
icculus@34
    66
/* Things that _should_ be binary compatible pass right through... */
icculus@34
    67
#define SDL20_SYM(rc,fn,params,args,ret)
icculus@34
    68
#define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) \
icculus@35
    69
    DECLSPEC rc SDLCALL SDL_##fn params { ret SDL20_##fn args; }
icculus@34
    70
#include "SDL20_syms.h"
icculus@34
    71
#undef SDL20_SYM_PASSTHROUGH
icculus@34
    72
#undef SDL20_SYM
icculus@34
    73
icculus@34
    74
icculus@34
    75
/* these are macros (etc) in the SDL headers, so make our own. */
icculus@34
    76
#define SDL20_OutOfMemory() SDL20_Error(SDL_ENOMEM)
icculus@34
    77
#define SDL20_Unsupported() SDL20_Error(SDL_UNSUPPORTED)
icculus@34
    78
#define SDL20_InvalidParamError(param) SDL20_SetError("Parameter '%s' is invalid", (param))
icculus@34
    79
#define SDL20_zero(x) SDL20_memset(&(x), 0, sizeof((x)))
icculus@34
    80
#define SDL20_zerop(x) SDL20_memset((x), 0, sizeof(*(x)))
icculus@34
    81
#define SDL_ReportAssertion SDL20_ReportAssertion
icculus@34
    82
icculus@34
    83
#define SDL12_DEFAULT_REPEAT_DELAY 500
icculus@34
    84
#define SDL12_DEFAULT_REPEAT_INTERVAL 30
icculus@9
    85
icculus@9
    86
#define SDL12_INIT_TIMER       0x00000001
icculus@9
    87
#define SDL12_INIT_AUDIO       0x00000010
icculus@9
    88
#define SDL12_INIT_VIDEO       0x00000020
icculus@9
    89
#define SDL12_INIT_CDROM       0x00000100
icculus@9
    90
#define SDL12_INIT_JOYSTICK    0x00000200
icculus@9
    91
#define SDL12_INIT_NOPARACHUTE 0x00100000
icculus@9
    92
#define SDL12_INIT_EVENTTHREAD 0x01000000
icculus@9
    93
#define SDL12_INIT_EVERYTHING  0x0000FFFF
icculus@9
    94
icculus@15
    95
typedef struct SDL12_Palette
icculus@15
    96
{
icculus@15
    97
    int       ncolors;
icculus@15
    98
    SDL_Color *colors;
icculus@15
    99
} SDL12_Palette;
icculus@15
   100
icculus@15
   101
typedef struct SDL12_PixelFormat
icculus@15
   102
{
icculus@15
   103
    SDL12_Palette *palette;
icculus@15
   104
    Uint8 BitsPerPixel;
icculus@15
   105
    Uint8 BytesPerPixel;
icculus@15
   106
    Uint8 Rloss;
icculus@15
   107
    Uint8 Gloss;
icculus@15
   108
    Uint8 Bloss;
icculus@15
   109
    Uint8 Aloss;
icculus@15
   110
    Uint8 Rshift;
icculus@15
   111
    Uint8 Gshift;
icculus@15
   112
    Uint8 Bshift;
icculus@15
   113
    Uint8 Ashift;
icculus@15
   114
    Uint32 Rmask;
icculus@15
   115
    Uint32 Gmask;
icculus@15
   116
    Uint32 Bmask;
icculus@15
   117
    Uint32 Amask;
icculus@15
   118
    Uint32 colorkey;
icculus@15
   119
    Uint8 alpha;
icculus@15
   120
} SDL12_PixelFormat;
icculus@15
   121
icculus@18
   122
typedef struct SDL12_Surface
icculus@18
   123
{
icculus@18
   124
    Uint32 flags;
icculus@18
   125
    SDL12_PixelFormat *format;
icculus@18
   126
    int w;
icculus@18
   127
    int h;
icculus@18
   128
    Uint16 pitch;
icculus@18
   129
    void *pixels;
icculus@18
   130
    int offset;
icculus@34
   131
    SDL_Surface *surface20; /* the real SDL 1.2 has an opaque pointer to a platform-specific thing here named "hwdata". */
icculus@18
   132
    SDL_Rect clip_rect;
icculus@18
   133
    Uint32 unused1;
icculus@18
   134
    Uint32 locked;
icculus@18
   135
    void *blitmap;
icculus@18
   136
    unsigned int format_version;
icculus@18
   137
    int refcount;
icculus@18
   138
} SDL12_Surface;
icculus@18
   139
icculus@9
   140
typedef struct
icculus@9
   141
{
icculus@15
   142
    Uint32 hw_available :1;
icculus@15
   143
    Uint32 wm_available :1;
icculus@15
   144
    Uint32 UnusedBits1  :6;
icculus@15
   145
    Uint32 UnusedBits2  :1;
icculus@15
   146
    Uint32 blit_hw      :1;
icculus@15
   147
    Uint32 blit_hw_CC   :1;
icculus@15
   148
    Uint32 blit_hw_A    :1;
icculus@15
   149
    Uint32 blit_sw      :1;
icculus@15
   150
    Uint32 blit_sw_CC   :1;
icculus@15
   151
    Uint32 blit_sw_A    :1;
icculus@15
   152
    Uint32 blit_fill    :1;
icculus@15
   153
    Uint32 UnusedBits3  :16;
icculus@15
   154
    Uint32 video_mem;
icculus@15
   155
    SDL_PixelFormat *vfmt;
icculus@15
   156
    int current_w;
icculus@15
   157
    int current_h;
icculus@9
   158
} SDL12_VideoInfo;
icculus@9
   159
icculus@9
   160
icculus@15
   161
#define SDL12_HWSURFACE 0x00000001
icculus@15
   162
#define SDL12_ASYNCBLIT 0x00000004
icculus@15
   163
#define SDL12_ANYFORMAT 0x10000000
icculus@15
   164
#define SDL12_HWPALETTE 0x20000000
icculus@15
   165
#define SDL12_DOUBLEBUF 0x40000000
icculus@15
   166
#define SDL12_FULLSCREEN 0x80000000
icculus@15
   167
#define SDL12_OPENGL 0x00000002
icculus@15
   168
#define SDL12_OPENGLBLIT 0x0000000A
icculus@15
   169
#define SDL12_RESIZABLE 0x00000010
icculus@15
   170
#define SDL12_NOFRAME 0x00000020
icculus@15
   171
#define SDL12_HWACCEL 0x00000100
icculus@15
   172
#define SDL12_SRCCOLORKEY 0x00001000
icculus@15
   173
#define SDL12_RLEACCELOK 0x00002000
icculus@15
   174
#define SDL12_RLEACCEL 0x00004000
icculus@15
   175
#define SDL12_SRCALPHA 0x00010000
icculus@15
   176
#define SDL12_PREALLOC 0x01000000
icculus@9
   177
icculus@18
   178
typedef enum
icculus@18
   179
{
icculus@18
   180
    SDL12_NOEVENT = 0,
icculus@18
   181
    SDL12_ACTIVEEVENT,
icculus@18
   182
    SDL12_KEYDOWN,
icculus@18
   183
    SDL12_KEYUP,
icculus@18
   184
    SDL12_MOUSEMOTION,
icculus@18
   185
    SDL12_MOUSEBUTTONDOWN,
icculus@18
   186
    SDL12_MOUSEBUTTONUP,
icculus@18
   187
    SDL12_JOYAXISMOTION,
icculus@18
   188
    SDL12_JOYBALLMOTION,
icculus@18
   189
    SDL12_JOYHATMOTION,
icculus@18
   190
    SDL12_JOYBUTTONDOWN,
icculus@18
   191
    SDL12_JOYBUTTONUP,
icculus@18
   192
    SDL12_QUIT,
icculus@18
   193
    SDL12_SYSWMEVENT,
icculus@18
   194
    SDL12_EVENT_RESERVEDA,
icculus@18
   195
    SDL12_EVENT_RESERVEDB,
icculus@18
   196
    SDL12_VIDEORESIZE,
icculus@18
   197
    SDL12_VIDEOEXPOSE,
icculus@18
   198
    SDL12_USEREVENT = 24,
icculus@18
   199
    SDL12_NUMEVENTS = 32
icculus@18
   200
} SDL12_EventType;
icculus@18
   201
icculus@31
   202
icculus@31
   203
#define SDL12_APPMOUSEFOCUS (1<<0)
icculus@31
   204
#define SDL12_APPINPUTFOCUS (1<<1)
icculus@31
   205
#define SDL12_APPACTIVE     (1<<2)
icculus@31
   206
icculus@18
   207
typedef struct
icculus@18
   208
{
icculus@18
   209
    Uint8 type;
icculus@18
   210
    Uint8 gain;
icculus@18
   211
    Uint8 state;
icculus@18
   212
} SDL12_ActiveEvent;
icculus@18
   213
icculus@18
   214
typedef struct
icculus@18
   215
{
icculus@18
   216
    Uint8 type;
icculus@18
   217
    Uint8 which;
icculus@18
   218
    Uint8 state;
icculus@31
   219
    //FIXME: SDL12_keysym keysym;
icculus@18
   220
} SDL12_KeyboardEvent;
icculus@18
   221
icculus@18
   222
typedef struct
icculus@18
   223
{
icculus@18
   224
    Uint8 type;
icculus@18
   225
    Uint8 which;
icculus@18
   226
    Uint8 state;
icculus@18
   227
    Uint16 x, y;
icculus@18
   228
    Sint16 xrel;
icculus@18
   229
    Sint16 yrel;
icculus@18
   230
} SDL12_MouseMotionEvent;
icculus@18
   231
icculus@18
   232
typedef struct
icculus@18
   233
{
icculus@18
   234
    Uint8 type;
icculus@18
   235
    Uint8 which;
icculus@18
   236
    Uint8 button;
icculus@18
   237
    Uint8 state;
icculus@18
   238
    Uint16 x, y;
icculus@18
   239
} SDL12_MouseButtonEvent;
icculus@18
   240
icculus@18
   241
typedef struct
icculus@18
   242
{
icculus@18
   243
    Uint8 type;
icculus@18
   244
    Uint8 which;
icculus@18
   245
    Uint8 axis;
icculus@18
   246
    Sint16 value;
icculus@18
   247
} SDL12_JoyAxisEvent;
icculus@18
   248
icculus@18
   249
typedef struct
icculus@18
   250
{
icculus@18
   251
    Uint8 type;
icculus@18
   252
    Uint8 which;
icculus@18
   253
    Uint8 ball;
icculus@18
   254
    Sint16 xrel;
icculus@18
   255
    Sint16 yrel;
icculus@18
   256
} SDL12_JoyBallEvent;
icculus@18
   257
icculus@18
   258
typedef struct
icculus@18
   259
{
icculus@18
   260
    Uint8 type;
icculus@18
   261
    Uint8 which;
icculus@18
   262
    Uint8 hat;
icculus@18
   263
    Uint8 value;
icculus@18
   264
} SDL12_JoyHatEvent;
icculus@18
   265
icculus@18
   266
typedef struct
icculus@18
   267
{
icculus@18
   268
    Uint8 type;
icculus@18
   269
    Uint8 which;
icculus@18
   270
    Uint8 button;
icculus@18
   271
    Uint8 state;
icculus@18
   272
} SDL12_JoyButtonEvent;
icculus@18
   273
icculus@18
   274
typedef struct
icculus@18
   275
{
icculus@18
   276
    Uint8 type;
icculus@18
   277
    int w;
icculus@18
   278
    int h;
icculus@18
   279
} SDL12_ResizeEvent;
icculus@18
   280
icculus@18
   281
typedef struct
icculus@18
   282
{
icculus@18
   283
    Uint8 type;
icculus@18
   284
} SDL12_ExposeEvent;
icculus@18
   285
icculus@18
   286
typedef struct
icculus@18
   287
{
icculus@18
   288
    Uint8 type;
icculus@18
   289
} SDL12_QuitEvent;
icculus@18
   290
icculus@18
   291
typedef struct
icculus@18
   292
{
icculus@18
   293
    Uint8 type;
icculus@18
   294
    int code;
icculus@18
   295
    void *data1;
icculus@18
   296
    void *data2;
icculus@18
   297
} SDL12_UserEvent;
icculus@18
   298
icculus@18
   299
typedef struct
icculus@18
   300
{
icculus@18
   301
    Uint8 type;
icculus@18
   302
    void *msg;
icculus@18
   303
} SDL12_SysWMEvent;
icculus@18
   304
icculus@18
   305
typedef union
icculus@18
   306
{
icculus@18
   307
    Uint8 type;
icculus@18
   308
    SDL12_ActiveEvent active;
icculus@18
   309
    SDL12_KeyboardEvent key;
icculus@18
   310
    SDL12_MouseMotionEvent motion;
icculus@18
   311
    SDL12_MouseButtonEvent button;
icculus@18
   312
    SDL12_JoyAxisEvent jaxis;
icculus@18
   313
    SDL12_JoyBallEvent jball;
icculus@18
   314
    SDL12_JoyHatEvent jhat;
icculus@18
   315
    SDL12_JoyButtonEvent jbutton;
icculus@18
   316
    SDL12_ResizeEvent resize;
icculus@18
   317
    SDL12_ExposeEvent expose;
icculus@18
   318
    SDL12_QuitEvent quit;
icculus@18
   319
    SDL12_UserEvent user;
icculus@18
   320
    SDL12_SysWMEvent syswm;
icculus@18
   321
} SDL12_Event;
icculus@9
   322
icculus@31
   323
typedef int (SDLCALL *SDL12_EventFilter)(const SDL12_Event *event12);
icculus@31
   324
static int EventFilter20to12(void *data, SDL_Event *event20);
icculus@31
   325
icculus@31
   326
typedef Uint32 (SDLCALL *SDL12_TimerCallback)(Uint32 interval);
icculus@31
   327
typedef SDL_TimerCallback SDL12_NewTimerCallback;
icculus@31
   328
icculus@23
   329
typedef struct
icculus@31
   330
{
icculus@23
   331
    SDL_Rect area;
icculus@23
   332
    Sint16 hot_x;
icculus@23
   333
    Sint16 hot_y;
icculus@23
   334
    Uint8 *data;
icculus@23
   335
    Uint8 *mask;
icculus@23
   336
    Uint8 *save[2];
icculus@23
   337
    SDL_Cursor *wm_cursor;  /* the real SDL 1.2 has an opaque pointer to a platform-specific cursor here. */
icculus@23
   338
} SDL12_Cursor;
icculus@23
   339
icculus@30
   340
typedef enum
icculus@30
   341
{
icculus@30
   342
    SDL12_GL_RED_SIZE,
icculus@30
   343
    SDL12_GL_GREEN_SIZE,
icculus@30
   344
    SDL12_GL_BLUE_SIZE,
icculus@30
   345
    SDL12_GL_ALPHA_SIZE,
icculus@30
   346
    SDL12_GL_BUFFER_SIZE,
icculus@30
   347
    SDL12_GL_DOUBLEBUFFER,
icculus@30
   348
    SDL12_GL_DEPTH_SIZE,
icculus@30
   349
    SDL12_GL_STENCIL_SIZE,
icculus@30
   350
    SDL12_GL_ACCUM_RED_SIZE,
icculus@30
   351
    SDL12_GL_ACCUM_GREEN_SIZE,
icculus@30
   352
    SDL12_GL_ACCUM_BLUE_SIZE,
icculus@30
   353
    SDL12_GL_ACCUM_ALPHA_SIZE,
icculus@30
   354
    SDL12_GL_STEREO,
icculus@30
   355
    SDL12_GL_MULTISAMPLEBUFFERS,
icculus@30
   356
    SDL12_GL_MULTISAMPLESAMPLES,
icculus@30
   357
    SDL12_GL_ACCELERATED_VISUAL,
icculus@30
   358
    SDL12_GL_SWAP_CONTROL,
icculus@30
   359
    SDL12_GL_MAX_ATTRIBUTE
icculus@30
   360
} SDL12_GLattr;
icculus@23
   361
icculus@39
   362
icculus@39
   363
typedef struct
icculus@39
   364
{
icculus@39
   365
    Uint32 format;
icculus@39
   366
    SDL_Rect *modeslist;
icculus@39
   367
    SDL_Rect **modes;  /* ptrs to each item in modeslist, for SDL_ListModes() */
icculus@39
   368
} VideoModeList;
icculus@39
   369
icculus@39
   370
static VideoModeList *VideoModes = NULL;
icculus@39
   371
static int VideoModesCount = 0;
icculus@17
   372
static SDL12_VideoInfo VideoInfo;
icculus@19
   373
static SDL_Window *VideoWindow20 = NULL;
icculus@19
   374
static SDL12_Surface *WindowSurface = NULL;
icculus@19
   375
static SDL12_Surface *VideoSurface = NULL;
icculus@19
   376
static SDL12_Surface *ShadowSurface = NULL;
icculus@19
   377
static SDL12_Surface *PublicSurface = NULL;
icculus@16
   378
static SDL_GLContext *VideoContext = NULL;
icculus@16
   379
static Uint32 VideoFlags = 0;
icculus@16
   380
static SDL_Rect VideoViewport;
icculus@16
   381
static char *WindowTitle = NULL;
icculus@16
   382
static char *WindowIconTitle = NULL;
icculus@16
   383
static SDL_Surface *VideoIcon;
icculus@16
   384
static int EnabledUnicode = 0;
icculus@16
   385
static int VideoDisplayIndex = 0;
icculus@16
   386
static int CDRomInit = 0;
icculus@18
   387
static SDL12_EventFilter EventFilter12 = NULL;
icculus@23
   388
static SDL12_Cursor *CurrentCursor = NULL;
icculus@27
   389
static Uint8 EventStates[SDL12_NUMEVENTS];
icculus@30
   390
static int SwapInterval = 0;
icculus@18
   391
icculus@18
   392
// !!! FIXME: need a mutex for the event queue.
icculus@18
   393
#define SDL12_MAXEVENTS 128
icculus@20
   394
typedef struct EventQueueType
icculus@20
   395
{
icculus@20
   396
    SDL12_Event event12;
icculus@20
   397
    struct EventQueueType *next;
icculus@20
   398
} EventQueueType;
icculus@20
   399
icculus@20
   400
static EventQueueType EventQueuePool[SDL12_MAXEVENTS];
icculus@20
   401
static EventQueueType *EventQueueHead = NULL;
icculus@20
   402
static EventQueueType *EventQueueTail = NULL;
icculus@20
   403
static EventQueueType *EventQueueAvailable = NULL;
icculus@9
   404
icculus@33
   405
icculus@9
   406
/* Obviously we can't use SDL_LoadObject() to load SDL2.  :)  */
icculus@9
   407
#if defined(_WINDOWS)
icculus@19
   408
    #define WIN32_LEAN_AND_MEAN 1
icculus@19
   409
    #include <windows.h>
icculus@9
   410
    #define SDL20_LIBNAME "SDL2.dll"
icculus@9
   411
    static HANDLE Loaded_SDL20 = NULL;
icculus@9
   412
    #define LoadSDL20Library() ((Loaded_SDL20 = LoadLibraryA(SDL20_LIBNAME)) != NULL)
icculus@9
   413
    #define LookupSDL20Sym(sym) GetProcAddress(Loaded_SDL20, sym)
icculus@9
   414
    #define CloseSDL20Library() { { if (Loaded_SDL20) { FreeLibrary(Loaded_SDL20); Loaded_SDL20 = NULL; } }
icculus@19
   415
#elif defined(unix) || defined(__APPLE__)
icculus@19
   416
    #include <dlfcn.h>
icculus@9
   417
    #ifdef __APPLE__
icculus@9
   418
    #define SDL20_LIBNAME "libSDL2.dylib"
icculus@9
   419
    #else
icculus@9
   420
    #define SDL20_LIBNAME "libSDL2-2.0.so.0"
icculus@9
   421
    #endif
icculus@9
   422
    static void *Loaded_SDL20 = NULL;
icculus@9
   423
    #define LoadSDL20Library() ((Loaded_SDL20 = dlopen(SDL20_LIBNAME, RTLD_LOCAL)) != NULL)
icculus@9
   424
    #define LookupSDL20Sym(sym) dlsym(Loaded_SDL20, sym)
icculus@9
   425
    #define CloseSDL20Library() { if (Loaded_SDL20) { dlclose(Loaded_SDL20); Loaded_SDL20 = NULL; } }
icculus@9
   426
#else
icculus@9
   427
    #error Please define your platform.
icculus@9
   428
#endif
icculus@9
   429
icculus@9
   430
static void *
icculus@9
   431
LoadSDL20Symbol(const char *fn, int *okay)
icculus@9
   432
{
icculus@9
   433
    void *retval = NULL;
icculus@9
   434
    if (*okay)  /* only bother trying if we haven't previously failed. */
icculus@9
   435
    {
icculus@9
   436
        retval = LookupSDL20Sym(fn);
icculus@9
   437
        if (retval == NULL)
icculus@9
   438
            *okay = 0;
icculus@9
   439
    }
icculus@9
   440
    return retval;
icculus@9
   441
}
icculus@9
   442
icculus@9
   443
static void
icculus@9
   444
UnloadSDL20(void)
icculus@9
   445
{
icculus@9
   446
    #define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = NULL;
icculus@34
   447
    #define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_SYM(rc,fn,params,args,ret)
icculus@9
   448
    #include "SDL20_syms.h"
icculus@9
   449
    #undef SDL20_SYM_PASSTHROUGH
icculus@9
   450
    #undef SDL20_SYM
icculus@11
   451
    SDL20_SetError = NULL;
icculus@9
   452
    CloseSDL20Library();
icculus@9
   453
}
icculus@9
   454
icculus@9
   455
static int
icculus@9
   456
LoadSDL20(void)
icculus@9
   457
{
icculus@9
   458
    int okay = 1;
icculus@9
   459
    if (!Loaded_SDL20)
icculus@9
   460
    {
icculus@9
   461
        okay = LoadSDL20Library();
icculus@9
   462
        #define SDL20_SYM(rc,fn,params,args,ret) SDL20_##fn = (SDL20_##fn##_t) LoadSDL20Symbol("SDL_" #fn, &okay);
icculus@9
   463
        #define SDL20_SYM_PASSTHROUGH(rc,fn,params,args,ret) SDL20_SYM(rc,fn,params,args,ret)
icculus@9
   464
        #include "SDL20_syms.h"
icculus@9
   465
        #undef SDL20_SYM_PASSTHROUGH
icculus@9
   466
        #undef SDL20_SYM
icculus@11
   467
        SDL20_SetError = (SDL20_SetError_t) LoadSDL20Symbol("SDL_SetError", &okay);
icculus@9
   468
        if (!okay)
icculus@9
   469
            UnloadSDL20();
icculus@9
   470
    }
icculus@9
   471
    return okay;
icculus@9
   472
}
icculus@9
   473
icculus@35
   474
DECLSPEC const SDL_version * SDLCALL
icculus@35
   475
SDL_Linked_Version(void)
icculus@35
   476
{
icculus@35
   477
    static const SDL_version version = { 1, 2, SDL12_COMPAT_VERSION };
icculus@35
   478
    return &version;
icculus@35
   479
}
icculus@9
   480
icculus@9
   481
static int
icculus@9
   482
GetVideoDisplay()
icculus@9
   483
{
icculus@9
   484
    // !!! FIXME: cache this value during SDL_Init() so it doesn't change.
icculus@9
   485
    const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
icculus@9
   486
    if ( !variable ) {
icculus@9
   487
        variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
icculus@9
   488
    }
icculus@9
   489
    if ( variable ) {
icculus@34
   490
        return SDL20_atoi(variable);
icculus@9
   491
    } else {
icculus@9
   492
        return 0;
icculus@9
   493
    }
icculus@9
   494
}
icculus@9
   495
icculus@39
   496
/* This sets up VideoModes and VideoModesCount. You end up with arrays by pixel
icculus@39
   497
    format, each with a value that 1.2's SDL_ListModes() can return. */
icculus@39
   498
static int
icculus@39
   499
Init12VidModes(void)
icculus@39
   500
{
icculus@39
   501
    const int total = SDL20_GetNumDisplayModes(VideoDisplayIndex);
icculus@39
   502
    VideoModeList *vmode = NULL;
icculus@39
   503
    int num_modes = 0;
icculus@39
   504
    void *ptr = NULL;
icculus@39
   505
    int i, j;
icculus@39
   506
icculus@39
   507
    SDL_assert(VideoModes == NULL);
icculus@39
   508
    SDL_assert(VideoModesCount == 0);
icculus@39
   509
icculus@39
   510
    for (i = 0; i < total; ++i) {
icculus@39
   511
        SDL_DisplayMode mode;
icculus@39
   512
icculus@39
   513
        if (SDL20_GetDisplayMode(VideoDisplayIndex, i, &mode) == -1) {
icculus@39
   514
            continue;
icculus@39
   515
        } else if (!mode.w || !mode.h) {
icculus@39
   516
            SDL_assert(0 && "Can this actually happen?");
icculus@39
   517
            continue;
icculus@39
   518
        }
icculus@39
   519
icculus@39
   520
        if (!vmode || (mode.format != vmode->format)) {  // SDL20_GetDisplayMode() sorts on bpp first. We know when to change arrays.
icculus@39
   521
            if (VideoModesCount > 0) {
icculus@39
   522
                VideoModes[VideoModesCount-1].modes[num_modes] = NULL;
icculus@39
   523
            }
icculus@39
   524
            ptr = (VideoModeList *) SDL20_realloc(VideoModes, sizeof (VideoModeList) * (VideoModesCount+1));
icculus@39
   525
            if (!ptr) {
icculus@39
   526
                return SDL20_OutOfMemory();
icculus@39
   527
            }
icculus@39
   528
            VideoModes = (VideoModeList *) ptr;
icculus@39
   529
            vmode = &VideoModes[VideoModesCount];
icculus@39
   530
            vmode->format = mode.format;
icculus@39
   531
            vmode->modeslist = NULL;
icculus@39
   532
            vmode->modes = NULL;
icculus@39
   533
            VideoModesCount++;
icculus@39
   534
            num_modes = 0;
icculus@39
   535
        }
icculus@39
   536
icculus@39
   537
        /* make sure we don't have this one already (with a different refresh rate, etc). */
icculus@39
   538
        for (j = 0; j < num_modes; j++) {
icculus@39
   539
            if ((vmode->modeslist[j].w == mode.w) && (vmode->modeslist[j].h == mode.h)) {
icculus@39
   540
                break;
icculus@39
   541
            }
icculus@39
   542
        }
icculus@39
   543
icculus@39
   544
        if (j < num_modes) {
icculus@39
   545
            continue;  /* already have this one. */
icculus@39
   546
        }
icculus@39
   547
icculus@39
   548
        ptr = SDL20_realloc(vmode->modes, sizeof (SDL_Rect *) * (num_modes + 2));
icculus@39
   549
        if (ptr == NULL) {
icculus@39
   550
            return SDL20_OutOfMemory();
icculus@39
   551
        }
icculus@39
   552
        vmode->modes = (SDL_Rect **) ptr;
icculus@39
   553
icculus@39
   554
        ptr = SDL20_realloc(vmode->modeslist, sizeof (SDL_Rect) * (num_modes + 1));
icculus@39
   555
        if (ptr == NULL) {
icculus@39
   556
            return SDL20_OutOfMemory();
icculus@39
   557
        }
icculus@39
   558
        vmode->modeslist = (SDL_Rect *) ptr;
icculus@39
   559
icculus@39
   560
        vmode->modeslist[num_modes].x = 0;
icculus@39
   561
        vmode->modeslist[num_modes].y = 0;
icculus@39
   562
        vmode->modeslist[num_modes].w = mode.w;
icculus@39
   563
        vmode->modeslist[num_modes].h = mode.h;
icculus@39
   564
icculus@39
   565
        vmode->modes[num_modes] = &vmode->modeslist[num_modes];
icculus@39
   566
icculus@39
   567
        num_modes++;
icculus@39
   568
    }
icculus@39
   569
icculus@39
   570
    if (VideoModesCount > 0) {
icculus@39
   571
        VideoModes[VideoModesCount-1].modes[num_modes] = NULL;
icculus@39
   572
    }
icculus@39
   573
icculus@39
   574
    return 0;
icculus@39
   575
}
icculus@39
   576
icculus@39
   577
static int
icculus@39
   578
Init12Video(void)
icculus@39
   579
{
icculus@39
   580
    int i;
icculus@39
   581
icculus@39
   582
    for (i = 0; i < SDL12_MAXEVENTS-1; i++)
icculus@39
   583
        EventQueuePool[i].next = &EventQueuePool[i+1];
icculus@39
   584
    EventQueuePool[SDL12_MAXEVENTS-1].next = NULL;
icculus@39
   585
icculus@39
   586
    EventQueueHead = EventQueueTail = NULL;
icculus@39
   587
    EventQueueAvailable = EventQueuePool;
icculus@39
   588
icculus@39
   589
    SDL_memset(EventStates, SDL_ENABLE, sizeof (EventStates)); /* on by default */
icculus@39
   590
    EventStates[SDL12_SYSWMEVENT] = SDL_IGNORE;  /* off by default. */
icculus@39
   591
icculus@39
   592
    SDL20_SetEventFilter(EventFilter20to12, NULL);
icculus@39
   593
icculus@39
   594
    VideoDisplayIndex = GetVideoDisplay();
icculus@39
   595
    SwapInterval = 0;
icculus@39
   596
icculus@39
   597
    if (Init12VidModes() == -1) {
icculus@39
   598
        return -1;
icculus@39
   599
    }
icculus@39
   600
icculus@39
   601
    return 0;
icculus@39
   602
}
icculus@39
   603
icculus@39
   604
icculus@37
   605
DECLSPEC int SDLCALL
icculus@37
   606
SDL_InitSubSystem(Uint32 sdl12flags)
icculus@9
   607
{
icculus@40
   608
    // !!! FIXME: there is never a parachute in SDL2, should we catch segfaults ourselves?
icculus@40
   609
icculus@40
   610
    // !!! FIXME: support event thread where it makes sense to do so?
icculus@40
   611
	if ( (sdl12flags & SDL12_INIT_EVENTTHREAD) == SDL12_INIT_EVENTTHREAD ) {
icculus@40
   612
		return SDL20_SetError("OS doesn't support threaded events");
icculus@40
   613
	}
icculus@40
   614
icculus@9
   615
    Uint32 sdl20flags = 0;
icculus@9
   616
    int rc;
icculus@9
   617
icculus@9
   618
    if (!LoadSDL20())
icculus@9
   619
        return -1;
icculus@9
   620
icculus@19
   621
    #define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
icculus@9
   622
    SETFLAG(TIMER);
icculus@9
   623
    SETFLAG(AUDIO);
icculus@9
   624
    SETFLAG(VIDEO);
icculus@9
   625
    SETFLAG(JOYSTICK);
icculus@9
   626
    SETFLAG(NOPARACHUTE);
icculus@11
   627
    #undef SETFLAG
icculus@11
   628
icculus@11
   629
    // There's no CDROM in 2.0, but we'll just pretend it succeeded.
icculus@11
   630
    if (sdl12flags & SDL12_INIT_CDROM)
icculus@16
   631
        CDRomInit = 1;
icculus@11
   632
icculus@11
   633
    // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
icculus@11
   634
icculus@37
   635
    rc = SDL20_Init(sdl20flags);
icculus@39
   636
    if ((rc == 0) && (sdl20flags & SDL_INIT_VIDEO)) {
icculus@39
   637
        if (Init12Video() == -1) {
icculus@39
   638
            return -1;  /* !!! FIXME: should we deinit other subsystems? */
icculus@39
   639
        }
icculus@18
   640
    }
icculus@18
   641
icculus@11
   642
    return rc;
icculus@11
   643
}
icculus@11
   644
icculus@35
   645
DECLSPEC int SDLCALL
icculus@11
   646
SDL_Init(Uint32 sdl12flags)
icculus@11
   647
{
icculus@37
   648
    return SDL_InitSubSubsystem(sdl12flags);   /* there's no difference betwee Init and InitSubSystem in SDL2. */
icculus@11
   649
}
icculus@11
   650
icculus@38
   651
icculus@38
   652
static void
icculus@38
   653
InitFlags12To20(const Uint32 flags12, Uint32 *_flags20, Uint32 *_extraflags)
icculus@11
   654
{
icculus@38
   655
    Uint32 flags20 = 0;
icculus@11
   656
    Uint32 extraflags = 0;
icculus@11
   657
icculus@38
   658
    #define SETFLAG(flag) if (flags12 & SDL12_INIT_##flag) flags20 |= SDL_INIT_##flag
icculus@11
   659
    SETFLAG(TIMER);
icculus@11
   660
    SETFLAG(AUDIO);
icculus@11
   661
    SETFLAG(VIDEO);
icculus@11
   662
    SETFLAG(JOYSTICK);
icculus@11
   663
    SETFLAG(NOPARACHUTE);
icculus@11
   664
    #undef SETFLAG
icculus@11
   665
icculus@38
   666
    if ((flags12 & SDL12_INIT_CDROM) && (CDRomInit)) {
icculus@11
   667
        extraflags |= SDL12_INIT_CDROM;
icculus@38
   668
    }
icculus@11
   669
icculus@11
   670
    // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
icculus@11
   671
icculus@38
   672
    *_flags20 = flags20;
icculus@38
   673
    *_extraflags = extraflags;
icculus@11
   674
}
icculus@11
   675
icculus@38
   676
static Uint32
icculus@38
   677
InitFlags20to12(const Uint32 flags20)
icculus@11
   678
{
icculus@38
   679
    Uint32 flags12 = 0;
icculus@38
   680
icculus@38
   681
    #define SETFLAG(flag) if (flags20 & SDL_INIT_##flag) flags12 |= SDL12_INIT_##flag
icculus@11
   682
    SETFLAG(TIMER);
icculus@11
   683
    SETFLAG(AUDIO);
icculus@11
   684
    SETFLAG(VIDEO);
icculus@11
   685
    SETFLAG(JOYSTICK);
icculus@11
   686
    SETFLAG(NOPARACHUTE);
icculus@9
   687
    #undef SETFLAG
icculus@9
   688
icculus@38
   689
    return flags12;
icculus@38
   690
}
icculus@38
   691
icculus@38
   692
icculus@38
   693
DECLSPEC Uint32 SDLCALL
icculus@38
   694
SDL_WasInit(Uint32 sdl12flags)
icculus@38
   695
{
icculus@38
   696
    Uint32 sdl20flags, extraflags;
icculus@38
   697
    InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
icculus@38
   698
icculus@38
   699
    return InitFlags20to12(SDL20_WasInit(sdl20flags)) | extraflags;
icculus@38
   700
}
icculus@38
   701
icculus@39
   702
static void
icculus@39
   703
Quit12Video(void)
icculus@39
   704
{
icculus@39
   705
    int i;
icculus@39
   706
icculus@39
   707
    for (i = 0; i < VideoModesCount; i++) {
icculus@39
   708
        SDL20_free(VideoModes[i].modeslist);
icculus@39
   709
        SDL20_free(VideoModes[i].modes);
icculus@39
   710
    }
icculus@39
   711
    SDL20_free(VideoModes);
icculus@39
   712
icculus@39
   713
    SDL20_FreeFormat(VideoInfo.vfmt);
icculus@39
   714
    SDL20_zero(VideoInfo);
icculus@39
   715
icculus@39
   716
    EventFilter12 = NULL;
icculus@39
   717
    EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
icculus@39
   718
    CurrentCursor = NULL;
icculus@39
   719
    VideoModes = NULL;
icculus@39
   720
    VideoModesCount = 0;
icculus@39
   721
}
icculus@39
   722
icculus@38
   723
DECLSPEC void SDLCALL
icculus@38
   724
SDL_QuitSubSystem(Uint32 sdl12flags)
icculus@38
   725
{
icculus@38
   726
    Uint32 sdl20flags, extraflags;
icculus@38
   727
    InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
icculus@38
   728
icculus@38
   729
    if (extraflags & SDL12_INIT_CDROM) {
icculus@16
   730
        CDRomInit = 0;
icculus@38
   731
    }
icculus@16
   732
icculus@17
   733
    // !!! FIXME: reset a bunch of other global variables too.
icculus@17
   734
    if (sdl12flags & SDL12_INIT_VIDEO) {
icculus@39
   735
        Quit12Video();
icculus@17
   736
    }
icculus@17
   737
icculus@9
   738
    // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
icculus@11
   739
    SDL20_QuitSubSystem(sdl20flags);
icculus@9
   740
icculus@11
   741
    // !!! FIXME: UnloadSDL20() ?
icculus@9
   742
}
icculus@9
   743
icculus@35
   744
DECLSPEC void SDLCALL
icculus@9
   745
SDL_Quit(void)
icculus@9
   746
{
icculus@17
   747
    // !!! FIXME: reset a bunch of other global variables too.
icculus@18
   748
    EventFilter12 = NULL;
icculus@20
   749
    EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
icculus@23
   750
    CurrentCursor = NULL;
icculus@17
   751
    SDL20_FreeFormat(VideoInfo.vfmt);
icculus@34
   752
    SDL20_zero(VideoInfo);
icculus@16
   753
    CDRomInit = 0;
icculus@9
   754
    SDL20_Quit();
icculus@9
   755
    UnloadSDL20();
icculus@9
   756
}
icculus@9
   757
icculus@35
   758
DECLSPEC void SDLCALL
icculus@11
   759
SDL_SetError(const char *fmt, ...)
icculus@11
   760
{
icculus@34
   761
    char ch;
icculus@12
   762
    char *str = NULL;
icculus@34
   763
    size_t len = 0;
icculus@12
   764
    va_list ap;
icculus@12
   765
    va_start(ap, fmt);
icculus@34
   766
    len = SDL20_vsnprintf(&ch, 1, fmt, ap);
icculus@12
   767
    va_end(ap);
icculus@34
   768
icculus@34
   769
    str = (char *) SDL20_malloc(len + 1);
icculus@12
   770
    if (!str)
icculus@12
   771
        SDL20_OutOfMemory();
icculus@12
   772
    else
icculus@12
   773
    {
icculus@34
   774
        va_start(ap, fmt);
icculus@34
   775
        SDL20_vsnprintf(str, len + 1, fmt, ap);
icculus@34
   776
        va_end(ap);
icculus@12
   777
        SDL20_SetError("%s", str);
icculus@34
   778
        SDL20_free(str);
icculus@12
   779
    }
icculus@11
   780
}
icculus@9
   781
icculus@35
   782
DECLSPEC const char * SDLCALL
icculus@9
   783
SDL_GetError(void)
icculus@9
   784
{
icculus@9
   785
    if (!Loaded_SDL20)
icculus@9
   786
    {
icculus@19
   787
        static const char noload_errstr[] = "Failed to load SDL 2.0 shared library";
icculus@9
   788
        return noload_errstr;
icculus@9
   789
    }
icculus@9
   790
    return SDL20_GetError();
icculus@9
   791
}
icculus@9
   792
icculus@9
   793
icculus@9
   794
static const char *
icculus@9
   795
GetDriverName(const char *name, char *namebuf, int maxlen)
icculus@9
   796
{
icculus@9
   797
    if (name) {
icculus@9
   798
        if (namebuf) {
icculus@9
   799
            SDL20_strlcpy(namebuf, name, maxlen);
icculus@9
   800
            return namebuf;
icculus@9
   801
        } else {
icculus@9
   802
            return name;
icculus@9
   803
        }
icculus@9
   804
    }
icculus@9
   805
    return NULL;
icculus@9
   806
}
icculus@9
   807
icculus@35
   808
DECLSPEC const char * SDLCALL
icculus@9
   809
SDL_AudioDriverName(char *namebuf, int maxlen)
icculus@9
   810
{
icculus@9
   811
    return GetDriverName(SDL20_GetCurrentAudioDriver(), namebuf, maxlen);
icculus@9
   812
}
icculus@9
   813
icculus@35
   814
DECLSPEC const char * SDLCALL
icculus@9
   815
SDL_VideoDriverName(char *namebuf, int maxlen)
icculus@9
   816
{
icculus@9
   817
    return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
icculus@9
   818
}
icculus@9
   819
icculus@35
   820
DECLSPEC int SDLCALL
icculus@31
   821
SDL_PollEvent(SDL12_Event *event12)
icculus@31
   822
{
icculus@31
   823
    EventQueueType *next;
icculus@31
   824
icculus@31
   825
    SDL20_PumpEvents();  /* this will run our filter and build our 1.2 queue. */
icculus@31
   826
icculus@31
   827
    if (EventQueueHead == NULL)
icculus@31
   828
        return 0;  /* no events at the moment. */
icculus@31
   829
icculus@31
   830
    SDL_memcpy(event12, &EventQueueHead->event12, sizeof (SDL12_Event));
icculus@31
   831
    next = EventQueueHead->next;
icculus@31
   832
    EventQueueHead->next = EventQueueAvailable;
icculus@31
   833
    EventQueueAvailable = EventQueueHead;
icculus@31
   834
    EventQueueHead = next;
icculus@31
   835
    return 1;
icculus@31
   836
}
icculus@31
   837
icculus@35
   838
DECLSPEC int SDLCALL
icculus@31
   839
SDL_PushEvent(SDL12_Event *event12)
icculus@31
   840
{
icculus@31
   841
    EventQueueType *item = EventQueueAvailable;
icculus@31
   842
    if (item == NULL)
icculus@31
   843
        return -1;  /* no space available at the moment. */
icculus@31
   844
icculus@31
   845
    EventQueueAvailable = item->next;
icculus@31
   846
    if (EventQueueTail)
icculus@31
   847
        EventQueueTail->next = item;
icculus@31
   848
    else
icculus@31
   849
        EventQueueHead = EventQueueTail = item;
icculus@31
   850
    item->next = NULL;
icculus@31
   851
icculus@31
   852
    SDL_memcpy(&item->event12, event12, sizeof (SDL12_Event));
icculus@31
   853
    return 0;
icculus@31
   854
}
icculus@31
   855
icculus@35
   856
DECLSPEC int SDLCALL
icculus@31
   857
SDL_PeepEvents(SDL12_Event *events12, int numevents, SDL_eventaction action, Uint32 mask)
icculus@31
   858
{
icculus@31
   859
    if (action == SDL_ADDEVENT)
icculus@31
   860
    {
icculus@31
   861
        int i;
icculus@31
   862
        for (i = 0; i < numevents; i++)
icculus@31
   863
        {
icculus@31
   864
            if (SDL_PushEvent(&events12[i]) == -1)
icculus@31
   865
                break;  /* out of space for more events. */
icculus@31
   866
        }
icculus@31
   867
        return i;
icculus@31
   868
    }
icculus@31
   869
    else if ((action == SDL_PEEKEVENT) || (action == SDL_GETEVENT))
icculus@31
   870
    {
icculus@31
   871
        const SDL_bool isGet = (action == SDL_GETEVENT);
icculus@31
   872
        EventQueueType *prev = NULL;
icculus@31
   873
        EventQueueType *item = EventQueueHead;
icculus@31
   874
        EventQueueType *next = NULL;
icculus@31
   875
        int chosen = 0;
icculus@31
   876
        while (chosen < numevents)
icculus@31
   877
        {
icculus@31
   878
            EventQueueType *nextPrev = item;
icculus@31
   879
            if (!item)
icculus@31
   880
                break;  /* no more events at the moment. */
icculus@31
   881
icculus@31
   882
            next = item->next;  /* copy, since we might overwrite item->next */
icculus@31
   883
icculus@31
   884
            if (mask & (1<<item->event12.type))
icculus@31
   885
            {
icculus@31
   886
                SDL_memcpy(&events12[chosen++], &item->event12, sizeof (SDL12_Event));
icculus@31
   887
                if (isGet)  /* remove from list? */
icculus@31
   888
                {
icculus@31
   889
                    if (prev != NULL)
icculus@31
   890
                        prev->next = next;
icculus@31
   891
                    if (item == EventQueueHead)
icculus@31
   892
                        EventQueueHead = next;
icculus@31
   893
                    if (item == EventQueueTail)
icculus@31
   894
                        EventQueueTail = prev;
icculus@31
   895
icculus@31
   896
                    /* put it back in the free pool. */
icculus@31
   897
                    item->next = EventQueueAvailable;
icculus@31
   898
                    EventQueueAvailable = item;
icculus@31
   899
                    nextPrev = prev;  /* previous item doesn't change. */
icculus@31
   900
                }
icculus@31
   901
            }
icculus@31
   902
icculus@31
   903
            item = next;
icculus@31
   904
            prev = nextPrev;
icculus@31
   905
        }
icculus@31
   906
        return chosen;
icculus@31
   907
    }
icculus@31
   908
icculus@31
   909
    return 0;
icculus@31
   910
}
icculus@31
   911
icculus@35
   912
DECLSPEC int SDLCALL
icculus@31
   913
SDL_WaitEvent(SDL12_Event *event12)
icculus@31
   914
{
icculus@31
   915
    /* In 1.2, this only fails (-1) if you haven't SDL_Init()'d. */
icculus@31
   916
    while (!SDL_PollEvent(event12))
icculus@31
   917
        SDL_Delay(10);
icculus@31
   918
    return 1;
icculus@31
   919
}
icculus@31
   920
icculus@32
   921
static SDL_bool
icculus@32
   922
PushEventIfNotFiltered(SDL12_Event *event12)
icculus@32
   923
{
icculus@32
   924
    if (event12->type != SDL12_NOEVENT)
icculus@32
   925
    {
icculus@32
   926
        if (EventStates[event12->type] != SDL_IGNORE)
icculus@32
   927
        {
icculus@32
   928
            if ((!EventFilter12) || (EventFilter12(event12)))
icculus@32
   929
                return (SDL_PushEvent(event12) == 0);
icculus@32
   930
        }
icculus@32
   931
    }
icculus@32
   932
    return SDL_FALSE;
icculus@32
   933
}
icculus@32
   934
icculus@35
   935
DECLSPEC Uint8 SDLCALL
icculus@31
   936
SDL_EventState(Uint8 type, int state)
icculus@31
   937
{
icculus@31
   938
    /* the values of "state" match between 1.2 and 2.0 */
icculus@31
   939
    const Uint8 retval = EventStates[type];
icculus@31
   940
    SDL12_Event e;
icculus@31
   941
icculus@31
   942
    if (state != SDL_QUERY)
icculus@31
   943
        EventStates[type] = state;
icculus@31
   944
    if (state == SDL_IGNORE)  /* drop existing events of this type. */
icculus@31
   945
        while (SDL_PeepEvents(&e, 1, SDL_GETEVENT, (1<<type))) {}
icculus@31
   946
icculus@31
   947
    return retval;
icculus@31
   948
}
icculus@31
   949
icculus@35
   950
DECLSPEC Uint8 SDLCALL
icculus@35
   951
SDL_GetMouseState(int *x, int *y)
icculus@35
   952
{
icculus@35
   953
    const Uint32 state20 = SDL20_GetMouseState(x, y);
icculus@35
   954
    Uint8 retval = (state20 & 0x7);  /* left, right, and middle will match. */
icculus@35
   955
icculus@35
   956
    /* the X[12] buttons are different in 1.2; mousewheel was in the way. */
icculus@35
   957
    if (state20 & SDL_BUTTON(SDL_BUTTON_X1))
icculus@35
   958
        retval |= (1<<5);
icculus@35
   959
    if (state20 & SDL_BUTTON(SDL_BUTTON_X2))
icculus@35
   960
        retval |= (1<<6);
icculus@35
   961
icculus@35
   962
    return retval;
icculus@35
   963
}
icculus@35
   964
icculus@18
   965
static int
icculus@18
   966
EventFilter20to12(void *data, SDL_Event *event20)
icculus@15
   967
{
icculus@18
   968
    const int maxUserEvents12 = SDL12_NUMEVENTS - SDL12_USEREVENT;
icculus@18
   969
    SDL12_Event event12;
icculus@32
   970
    int x, y;
icculus@18
   971
icculus@18
   972
    SDL_assert(data == NULL);  /* currently unused. */
icculus@18
   973
icculus@34
   974
    SDL20_zero(event12);
icculus@18
   975
icculus@18
   976
    switch (event20->type)
icculus@18
   977
    {
icculus@18
   978
        case SDL_QUIT:
icculus@31
   979
            event12.type = SDL12_QUIT;
icculus@18
   980
            break;
icculus@18
   981
icculus@18
   982
        case SDL_WINDOWEVENT:
icculus@18
   983
            switch (event20->window.event)
icculus@18
   984
            {
icculus@18
   985
                case SDL_WINDOWEVENT_CLOSE:
icculus@31
   986
                    event12.type = SDL12_QUIT;
icculus@18
   987
                    break;
icculus@18
   988
icculus@18
   989
                case SDL_WINDOWEVENT_SHOWN:
icculus@18
   990
                case SDL_WINDOWEVENT_EXPOSED:
icculus@31
   991
                    event12.type = SDL12_VIDEOEXPOSE;
icculus@18
   992
                    break;
icculus@18
   993
icculus@18
   994
                case SDL_WINDOWEVENT_RESIZED:
icculus@18
   995
                case SDL_WINDOWEVENT_SIZE_CHANGED:  // !!! FIXME: what's the difference between RESIZED and SIZE_CHANGED?
icculus@31
   996
                    event12.type = SDL12_VIDEORESIZE;
icculus@31
   997
                    event12.resize.w = event20->window.data1;
icculus@31
   998
                    event12.resize.h = event20->window.data2;
icculus@18
   999
                    break;
icculus@18
  1000
icculus@18
  1001
                case SDL_WINDOWEVENT_MINIMIZED:
icculus@31
  1002
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1003
                    event12.active.gain = 0;
icculus@31
  1004
                    event12.active.state = SDL12_APPACTIVE;
icculus@18
  1005
                    break;
icculus@18
  1006
icculus@18
  1007
                case SDL_WINDOWEVENT_RESTORED:
icculus@31
  1008
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1009
                    event12.active.gain = 1;
icculus@31
  1010
                    event12.active.state = SDL12_APPACTIVE;
icculus@18
  1011
                    break;
icculus@18
  1012
icculus@18
  1013
                case SDL_WINDOWEVENT_ENTER:
icculus@31
  1014
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1015
                    event12.active.gain = 1;
icculus@31
  1016
                    event12.active.state = SDL12_APPMOUSEFOCUS;
icculus@18
  1017
                    break;
icculus@18
  1018
icculus@18
  1019
                case SDL_WINDOWEVENT_LEAVE:
icculus@31
  1020
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1021
                    event12.active.gain = 0;
icculus@31
  1022
                    event12.active.state = SDL12_APPMOUSEFOCUS;
icculus@18
  1023
                    break;
icculus@18
  1024
icculus@18
  1025
                case SDL_WINDOWEVENT_FOCUS_GAINED:
icculus@31
  1026
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1027
                    event12.active.gain = 1;
icculus@31
  1028
                    event12.active.state = SDL12_APPINPUTFOCUS;
icculus@18
  1029
                    break;
icculus@18
  1030
icculus@18
  1031
                case SDL_WINDOWEVENT_FOCUS_LOST:
icculus@31
  1032
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1033
                    event12.active.gain = 0;
icculus@31
  1034
                    event12.active.state = SDL12_APPINPUTFOCUS;
icculus@18
  1035
                    break;
icculus@18
  1036
            }
icculus@18
  1037
            break;
icculus@18
  1038
icculus@18
  1039
        // !!! FIXME: this is sort of a mess to convert.
icculus@18
  1040
        //case SDL_SYSWMEVENT:
icculus@18
  1041
icculus@18
  1042
        // !!! FIXME: write me
icculus@18
  1043
        case SDL_KEYDOWN:
icculus@18
  1044
        case SDL_KEYUP:
icculus@18
  1045
            return 0;
icculus@18
  1046
icculus@18
  1047
        // !!! FIXME: write me
icculus@18
  1048
        case SDL_TEXTEDITING:
icculus@18
  1049
        case SDL_TEXTINPUT:
icculus@18
  1050
            return 0;
icculus@18
  1051
icculus@18
  1052
        case SDL_MOUSEMOTION:
icculus@31
  1053
        	event12.type = SDL12_MOUSEMOTION;
icculus@31
  1054
            event12.motion.which = (Uint8) event20->motion.which;
icculus@31
  1055
            event12.motion.state = event20->motion.state;
icculus@31
  1056
            event12.motion.x = (Uint16) event20->motion.x;
icculus@31
  1057
            event12.motion.y = (Uint16) event20->motion.y;
icculus@31
  1058
            event12.motion.xrel = (Sint16) event20->motion.xrel;
icculus@31
  1059
            event12.motion.yrel = (Sint16) event20->motion.yrel;
icculus@18
  1060
            break;
icculus@18
  1061
icculus@18
  1062
        case SDL_MOUSEBUTTONDOWN:
icculus@31
  1063
        	event12.type = SDL12_MOUSEBUTTONDOWN;
icculus@31
  1064
            event12.button.which = (Uint8) event20->button.which;
icculus@31
  1065
            event12.button.button = event20->button.button;
icculus@31
  1066
            event12.button.state = event20->button.state;
icculus@31
  1067
            event12.button.x = (Uint16) event20->button.x;
icculus@31
  1068
            event12.button.y = (Uint16) event20->button.y;
icculus@18
  1069
            break;
icculus@18
  1070
icculus@18
  1071
        case SDL_MOUSEBUTTONUP:
icculus@31
  1072
        	event12.type = SDL12_MOUSEBUTTONUP;
icculus@31
  1073
            event12.button.which = (Uint8) event20->button.which;
icculus@31
  1074
            event12.button.button = event20->button.button;
icculus@31
  1075
            event12.button.state = event20->button.state;
icculus@31
  1076
            event12.button.x = (Uint16) event20->button.x;
icculus@31
  1077
            event12.button.y = (Uint16) event20->button.y;
icculus@18
  1078
            break;
icculus@18
  1079
icculus@18
  1080
        case SDL_MOUSEWHEEL:
icculus@32
  1081
            if (event20->wheel.y == 0)
icculus@32
  1082
                break;  /* don't support horizontal wheels in 1.2. */
icculus@32
  1083
icculus@32
  1084
            event12.type = SDL12_MOUSEBUTTONDOWN;
icculus@32
  1085
            event12.button.which = (Uint8) event20->wheel.which;
icculus@32
  1086
            event12.button.button = (event20->wheel.y > 0) ? 4 : 5;  /* wheelup is 4, down is 5. */
icculus@32
  1087
            event12.button.state = SDL_GetMouseState(&x, &y);
icculus@32
  1088
            event12.button.x = (Uint16) x;
icculus@32
  1089
            event12.button.y = (Uint16) y;
icculus@32
  1090
            PushEventIfNotFiltered(&event12);
icculus@32
  1091
icculus@32
  1092
            event12.type = SDL12_MOUSEBUTTONUP;  /* immediately release mouse "button" at the end of this switch. */
icculus@18
  1093
            break;
icculus@18
  1094
icculus@18
  1095
        case SDL_JOYAXISMOTION:
icculus@31
  1096
            event12.type = SDL12_JOYAXISMOTION;
icculus@31
  1097
            event12.jaxis.which = (Uint8) event20->jaxis.which;
icculus@31
  1098
            event12.jaxis.axis = event20->jaxis.axis;
icculus@31
  1099
            event12.jaxis.value = event20->jaxis.value;
icculus@18
  1100
            break;
icculus@18
  1101
icculus@18
  1102
        case SDL_JOYBALLMOTION:
icculus@31
  1103
            event12.type = SDL12_JOYBALLMOTION;
icculus@31
  1104
            event12.jball.which = (Uint8) event20->jball.which;
icculus@31
  1105
            event12.jball.ball = event20->jball.ball;
icculus@31
  1106
            event12.jball.xrel = event20->jball.xrel;
icculus@31
  1107
            event12.jball.yrel = event20->jball.yrel;
icculus@18
  1108
            break;
icculus@18
  1109
icculus@18
  1110
        case SDL_JOYHATMOTION:
icculus@31
  1111
            event12.type = SDL12_JOYHATMOTION;
icculus@31
  1112
            event12.jhat.which = (Uint8) event20->jhat.which;
icculus@31
  1113
            event12.jhat.hat = event20->jhat.hat;
icculus@31
  1114
            event12.jhat.value = event20->jhat.value;
icculus@18
  1115
            break;
icculus@18
  1116
icculus@18
  1117
        case SDL_JOYBUTTONDOWN:
icculus@31
  1118
            event12.type = SDL12_JOYBUTTONDOWN;
icculus@31
  1119
            event12.jbutton.which = (Uint8) event20->jbutton.which;
icculus@31
  1120
            event12.jbutton.button = event20->jbutton.button;
icculus@31
  1121
            event12.jbutton.state = event20->jbutton.state;
icculus@18
  1122
            break;
icculus@18
  1123
icculus@18
  1124
        case SDL_JOYBUTTONUP:
icculus@31
  1125
            event12.type = SDL12_JOYBUTTONUP;
icculus@31
  1126
            event12.jbutton.which = (Uint8) event20->jbutton.which;
icculus@31
  1127
            event12.jbutton.button = event20->jbutton.button;
icculus@31
  1128
            event12.jbutton.state = event20->jbutton.state;
icculus@18
  1129
            break;
icculus@18
  1130
icculus@18
  1131
        //case SDL_JOYDEVICEADDED:
icculus@18
  1132
        //case SDL_JOYDEVICEREMOVED:
icculus@18
  1133
	    //case SDL_CONTROLLERAXISMOTION:
icculus@18
  1134
	    //case SDL_CONTROLLERBUTTONDOWN:
icculus@18
  1135
	    //case SDL_CONTROLLERBUTTONUP:
icculus@18
  1136
	    //case SDL_CONTROLLERDEVICEADDED:
icculus@18
  1137
	    //case SDL_CONTROLLERDEVICEREMOVED:
icculus@18
  1138
	    //case SDL_CONTROLLERDEVICEREMAPPED:
icculus@18
  1139
        //case SDL_FINGERDOWN:
icculus@18
  1140
        //case SDL_FINGERUP:
icculus@18
  1141
        //case SDL_FINGERMOTION:
icculus@18
  1142
        //case SDL_DOLLARGESTURE:
icculus@18
  1143
        //case SDL_DOLLARRECORD:
icculus@18
  1144
        //case SDL_MULTIGESTURE:
icculus@18
  1145
        //case SDL_CLIPBOARDUPDATE:
icculus@18
  1146
        //case SDL_DROPFILE:
icculus@18
  1147
icculus@18
  1148
        default:
icculus@18
  1149
            return 0;  /* drop everything else. */
icculus@18
  1150
    }
icculus@18
  1151
icculus@32
  1152
    PushEventIfNotFiltered(&event12);
icculus@18
  1153
icculus@18
  1154
    return 0;  /* always drop it from the 2.0 event queue. */
icculus@18
  1155
}
icculus@18
  1156
icculus@35
  1157
DECLSPEC void SDLCALL
icculus@18
  1158
SDL_SetEventFilter(SDL12_EventFilter filter12)
icculus@18
  1159
{
icculus@18
  1160
    /* We always have a filter installed, but will call the app's too. */
icculus@18
  1161
    EventFilter12 = filter12;
icculus@18
  1162
}
icculus@18
  1163
icculus@35
  1164
DECLSPEC SDL12_EventFilter SDLCALL
icculus@18
  1165
SDL_GetEventFilter(void)
icculus@18
  1166
{
icculus@18
  1167
    return EventFilter12;
icculus@18
  1168
}
icculus@18
  1169
icculus@15
  1170
icculus@34
  1171
static SDL12_Surface *
icculus@15
  1172
Surface20to12(SDL_Surface *surface20)
icculus@15
  1173
{
icculus@15
  1174
    SDL12_Surface *surface12 = NULL;
icculus@15
  1175
    SDL12_Palette *palette12 = NULL;
icculus@15
  1176
    SDL12_PixelFormat *format12 = NULL;
icculus@31
  1177
    Uint32 flags = 0;
icculus@15
  1178
icculus@15
  1179
    if (!surface20)
icculus@15
  1180
        return NULL;
icculus@15
  1181
icculus@15
  1182
    surface12 = (SDL12_Surface *) SDL20_malloc(sizeof (SDL12_Surface));
icculus@15
  1183
    if (!surface12)
icculus@15
  1184
        goto failed;
icculus@15
  1185
icculus@15
  1186
    palette12 = (SDL12_Palette *) SDL20_malloc(sizeof (SDL12_Palette));
icculus@15
  1187
    if (!palette12)
icculus@15
  1188
        goto failed;
icculus@15
  1189
icculus@15
  1190
    format12 = (SDL12_PixelFormat *) SDL20_malloc(sizeof (SDL12_PixelFormat));
icculus@15
  1191
    if (!format12)
icculus@15
  1192
        goto failed;
icculus@15
  1193
icculus@34
  1194
    SDL20_zerop(palette12);
icculus@31
  1195
    palette12->ncolors = surface20->format->palette->ncolors;
icculus@31
  1196
    palette12->colors = surface20->format->palette->colors;
icculus@15
  1197
icculus@34
  1198
    SDL20_zerop(format12);
icculus@15
  1199
    format12->palette = palette12;
icculus@15
  1200
    format12->BitsPerPixel = surface20->format->BitsPerPixel;
icculus@15
  1201
    format12->BytesPerPixel = surface20->format->BytesPerPixel;
icculus@15
  1202
    format12->Rloss = surface20->format->Rloss;
icculus@15
  1203
    format12->Gloss = surface20->format->Gloss;
icculus@15
  1204
    format12->Bloss = surface20->format->Bloss;
icculus@15
  1205
    format12->Aloss = surface20->format->Aloss;
icculus@15
  1206
    format12->Rshift = surface20->format->Rshift;
icculus@15
  1207
    format12->Gshift = surface20->format->Gshift;
icculus@15
  1208
    format12->Bshift = surface20->format->Bshift;
icculus@15
  1209
    format12->Ashift = surface20->format->Ashift;
icculus@15
  1210
    format12->Rmask = surface20->format->Rmask;
icculus@15
  1211
    format12->Gmask = surface20->format->Gmask;
icculus@15
  1212
    format12->Bmask = surface20->format->Bmask;
icculus@15
  1213
    format12->Amask = surface20->format->Amask;
icculus@15
  1214
    /* !!! FIXME: format12->colorkey; */
icculus@15
  1215
    /* !!! FIXME: format12->alpha; */
icculus@15
  1216
icculus@34
  1217
    SDL20_zerop(surface12);
icculus@15
  1218
    flags = surface20->flags;
icculus@15
  1219
    #define MAPSURFACEFLAGS(fl) { if (surface20->flags & SDL_##fl) { surface12->flags |= SDL12_##fl; flags &= ~SDL_##fl; } }
icculus@15
  1220
    MAPSURFACEFLAGS(PREALLOC);
icculus@15
  1221
    MAPSURFACEFLAGS(RLEACCEL);
icculus@34
  1222
    /*MAPSURFACEFLAGS(DONTFREE);*/
icculus@15
  1223
    #undef MAPSURFACEFLAGS
icculus@31
  1224
    SDL_assert(flags == 0);  /* non-zero if there's a flag we didn't map. */
icculus@15
  1225
icculus@15
  1226
    surface12->format = format12;
icculus@15
  1227
    surface12->w = surface20->w;
icculus@15
  1228
    surface12->h = surface20->h;
icculus@15
  1229
    surface12->pitch = (Uint16) surface20->pitch;  /* !!! FIXME: make sure this fits in a Uint16 */
icculus@15
  1230
    surface12->pixels = surface20->pixels;
icculus@15
  1231
    surface12->offset = 0;
icculus@34
  1232
    surface12->surface20 = surface20;
icculus@15
  1233
    SDL20_memcpy(&surface12->clip_rect, &surface20->clip_rect, sizeof (SDL_Rect));
icculus@15
  1234
    surface12->refcount = surface20->refcount;
icculus@15
  1235
icculus@15
  1236
    return surface12;
icculus@15
  1237
icculus@15
  1238
failed:
icculus@15
  1239
    SDL20_free(surface12);
icculus@15
  1240
    SDL20_free(palette12);
icculus@15
  1241
    SDL20_free(format12);
icculus@15
  1242
    return NULL;
icculus@15
  1243
}
icculus@15
  1244
icculus@35
  1245
DECLSPEC SDL12_Surface * SDLCALL
icculus@15
  1246
SDL_CreateRGBSurface(Uint32 sdl12flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
icculus@15
  1247
{
icculus@15
  1248
    SDL_Surface *surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
icculus@15
  1249
    SDL12_Surface *surface12 = Surface20to12(surface20);
icculus@15
  1250
    if (!surface12) {
icculus@15
  1251
        SDL20_FreeSurface(surface20);
icculus@15
  1252
        return NULL;
icculus@15
  1253
    }
icculus@15
  1254
icculus@31
  1255
    SDL_assert(surface12->flags == 0);  // shouldn't have prealloc, rleaccel, or dontfree.
icculus@15
  1256
    return surface12;
icculus@15
  1257
}
icculus@15
  1258
icculus@35
  1259
DECLSPEC SDL12_Surface * SDLCALL
icculus@15
  1260
SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
icculus@15
  1261
{
icculus@31
  1262
    SDL_Surface *surface20 = SDL20_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask);
icculus@15
  1263
    SDL12_Surface *surface12 = Surface20to12(surface20);
icculus@15
  1264
    if (!surface12) {
icculus@15
  1265
        SDL20_FreeSurface(surface20);
icculus@15
  1266
        return NULL;
icculus@15
  1267
    }
icculus@15
  1268
icculus@31
  1269
    SDL_assert(surface12->flags == SDL12_PREALLOC);  // should _only_ have prealloc.
icculus@15
  1270
    return surface12;
icculus@15
  1271
}
icculus@15
  1272
icculus@35
  1273
DECLSPEC void SDLCALL
icculus@35
  1274
SDL_FreeSurface(SDL12_Surface *surface12)
icculus@15
  1275
{
icculus@15
  1276
    if (surface12) {
icculus@34
  1277
        SDL20_FreeSurface(surface12->surface20);
icculus@15
  1278
        if (surface12->format) {
icculus@15
  1279
            SDL20_free(surface12->format->palette);
icculus@15
  1280
            SDL20_free(surface12->format);
icculus@15
  1281
        }
icculus@15
  1282
        SDL20_free(surface12);
icculus@15
  1283
    }
icculus@15
  1284
}
icculus@15
  1285
icculus@35
  1286
DECLSPEC void SDLCALL
icculus@24
  1287
SDL_GetClipRect(SDL12_Surface *surface12, SDL_Rect *rect)
icculus@24
  1288
{
icculus@24
  1289
    if (surface12 && rect)
icculus@24
  1290
	    SDL_memcpy(rect, &surface12->clip_rect, sizeof (SDL_Rect));
icculus@24
  1291
}
icculus@24
  1292
icculus@35
  1293
DECLSPEC SDL_bool SDLCALL
icculus@24
  1294
SDL_SetClipRect(SDL12_Surface *surface12, const SDL_Rect *rect)
icculus@24
  1295
{
icculus@24
  1296
    SDL_bool retval = SDL_FALSE;
icculus@24
  1297
    if (surface12)
icculus@24
  1298
    {
icculus@34
  1299
        retval = SDL20_SetClipRect(surface12->surface20, rect);
icculus@34
  1300
        SDL20_GetClipRect(surface12->surface20, &surface12->clip_rect);
icculus@24
  1301
    }
icculus@24
  1302
    return retval;
icculus@24
  1303
}
icculus@24
  1304
icculus@35
  1305
DECLSPEC int SDLCALL
icculus@25
  1306
SDL_FillRect(SDL12_Surface *dst, SDL_Rect *dstrect, Uint32 color)
icculus@25
  1307
{
icculus@25
  1308
    const SDL_Rect orig_dstrect = *dstrect;
icculus@34
  1309
    const int retval = SDL20_FillRect(dst->surface20, &orig_dstrect, color);
icculus@25
  1310
    if (retval != -1)
icculus@25
  1311
    {
icculus@25
  1312
        if (dstrect)  /* 1.2 stores the clip intersection in dstrect */
icculus@31
  1313
            SDL20_IntersectRect(&orig_dstrect, &dst->clip_rect, dstrect);
icculus@25
  1314
    }
icculus@25
  1315
    return retval;
icculus@25
  1316
}
icculus@25
  1317
icculus@25
  1318
icculus@17
  1319
static SDL_PixelFormat *
icculus@31
  1320
PixelFormat12to20(SDL_PixelFormat *format20, SDL_Palette *palette20, const SDL12_PixelFormat *format12)
icculus@17
  1321
{
icculus@17
  1322
    palette20->ncolors = format12->palette->ncolors;
icculus@17
  1323
    palette20->colors = format12->palette->colors;
icculus@17
  1324
    palette20->version = 1;
icculus@17
  1325
    palette20->refcount = 1;
icculus@34
  1326
    format20->format = SDL20_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
icculus@17
  1327
    format20->palette = palette20;
icculus@17
  1328
    format20->BitsPerPixel = format12->BitsPerPixel;
icculus@17
  1329
    format20->BytesPerPixel = format12->BytesPerPixel;
icculus@17
  1330
    format20->Rmask = format12->Rmask;
icculus@17
  1331
    format20->Gmask = format12->Gmask;
icculus@17
  1332
    format20->Bmask = format12->Bmask;
icculus@17
  1333
    format20->Amask = format12->Amask;
icculus@17
  1334
    format20->Rloss = format12->Rloss;
icculus@17
  1335
    format20->Gloss = format12->Gloss;
icculus@17
  1336
    format20->Bloss = format12->Bloss;
icculus@17
  1337
    format20->Aloss = format12->Aloss;
icculus@17
  1338
    format20->Rshift = format12->Rshift;
icculus@17
  1339
    format20->Gshift = format12->Gshift;
icculus@17
  1340
    format20->Bshift = format12->Bshift;
icculus@17
  1341
    format20->Ashift = format12->Ashift;
icculus@17
  1342
    format20->refcount = 1;
icculus@17
  1343
    format20->next = NULL;
icculus@17
  1344
    return format20;
icculus@17
  1345
}
icculus@17
  1346
icculus@35
  1347
DECLSPEC Uint32 SDLCALL
icculus@17
  1348
SDL_MapRGB(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b)
icculus@17
  1349
{
icculus@17
  1350
    /* This is probably way slower than apps expect. */
icculus@17
  1351
    SDL_PixelFormat format20;
icculus@17
  1352
    SDL_Palette palette20;
icculus@17
  1353
    return SDL20_MapRGB(PixelFormat12to20(&format20, &palette20, format12), r, g, b);
icculus@17
  1354
}
icculus@17
  1355
icculus@35
  1356
DECLSPEC Uint32 SDLCALL
icculus@17
  1357
SDL_MapRGBA(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
icculus@17
  1358
{
icculus@17
  1359
    /* This is probably way slower than apps expect. */
icculus@17
  1360
    SDL_PixelFormat format20;
icculus@17
  1361
    SDL_Palette palette20;
icculus@17
  1362
    return SDL20_MapRGBA(PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
icculus@17
  1363
}
icculus@17
  1364
icculus@35
  1365
DECLSPEC void SDLCALL
icculus@17
  1366
SDL_GetRGB(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b)
icculus@17
  1367
{
icculus@17
  1368
    /* This is probably way slower than apps expect. */
icculus@17
  1369
    SDL_PixelFormat format20;
icculus@17
  1370
    SDL_Palette palette20;
icculus@17
  1371
    return SDL20_GetRGB(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b);
icculus@17
  1372
}
icculus@17
  1373
icculus@35
  1374
DECLSPEC void SDLCALL
icculus@17
  1375
SDL_GetRGBA(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
icculus@17
  1376
{
icculus@17
  1377
    /* This is probably way slower than apps expect. */
icculus@17
  1378
    SDL_PixelFormat format20;
icculus@17
  1379
    SDL_Palette palette20;
icculus@31
  1380
    return SDL20_GetRGBA(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
icculus@17
  1381
}
icculus@15
  1382
icculus@35
  1383
DECLSPEC const SDL12_VideoInfo * SDLCALL
icculus@9
  1384
SDL_GetVideoInfo(void)
icculus@9
  1385
{
icculus@9
  1386
    SDL_DisplayMode mode;
icculus@9
  1387
icculus@39
  1388
    // !!! FIXME: calculate this in Init12Video(), then this just does: return VideoInfo.vfmt ? &VideoInfo : NULL;
icculus@39
  1389
icculus@17
  1390
    if (!VideoInfo.vfmt && SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode) == 0) {
icculus@17
  1391
        VideoInfo.vfmt = SDL20_AllocFormat(mode.format);
icculus@17
  1392
        VideoInfo.current_w = mode.w;
icculus@17
  1393
        VideoInfo.current_h = mode.h;
icculus@9
  1394
        // !!! FIXME
icculus@17
  1395
        //VideoInfo.wm_available = 1;
icculus@17
  1396
        //VideoInfo.video_mem = 1024 * 256;
icculus@9
  1397
    }
icculus@17
  1398
    return &VideoInfo;
icculus@9
  1399
}
icculus@9
  1400
icculus@35
  1401
DECLSPEC int SDLCALL
icculus@9
  1402
SDL_VideoModeOK(int width, int height, int bpp, Uint32 sdl12flags)
icculus@9
  1403
{
icculus@9
  1404
    int i, nummodes, actual_bpp = 0;
icculus@9
  1405
icculus@9
  1406
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@9
  1407
        return 0;
icculus@9
  1408
    }
icculus@9
  1409
icculus@9
  1410
    if (!(sdl12flags & SDL12_FULLSCREEN)) {
icculus@9
  1411
        SDL_DisplayMode mode;
icculus@16
  1412
        SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode);
icculus@9
  1413
        return SDL_BITSPERPIXEL(mode.format);
icculus@9
  1414
    }
icculus@9
  1415
icculus@16
  1416
    nummodes = SDL20_GetNumDisplayModes(VideoDisplayIndex);
icculus@9
  1417
    for (i = 0; i < nummodes; ++i) {
icculus@9
  1418
        SDL_DisplayMode mode;
icculus@16
  1419
        SDL20_GetDisplayMode(VideoDisplayIndex, i, &mode);
icculus@9
  1420
        if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
icculus@9
  1421
            if (!mode.format) {
icculus@9
  1422
                return bpp;
icculus@9
  1423
            }
icculus@9
  1424
            if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
icculus@9
  1425
                actual_bpp = SDL_BITSPERPIXEL(mode.format);
icculus@9
  1426
            }
icculus@9
  1427
        }
icculus@9
  1428
    }
icculus@9
  1429
    return actual_bpp;
icculus@9
  1430
}
icculus@9
  1431
icculus@35
  1432
DECLSPEC SDL_Rect ** SDLCALL
icculus@39
  1433
SDL_ListModes(const SDL12_PixelFormat *format12, Uint32 flags)
icculus@9
  1434
{
icculus@39
  1435
    Uint32 fmt;
icculus@39
  1436
    int i;
icculus@9
  1437
icculus@9
  1438
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@9
  1439
        return NULL;
icculus@9
  1440
    }
icculus@9
  1441
icculus@39
  1442
    if ((!format12) && (!VideoInfo.vfmt)) {
icculus@39
  1443
        SDL20_SetError("No pixel format specified");
icculus@39
  1444
        return NULL;
icculus@39
  1445
    }
icculus@39
  1446
icculus@9
  1447
    if (!(flags & SDL12_FULLSCREEN)) {
icculus@39
  1448
        return (SDL_Rect **) (-1);  /* any resolution is fine. */
icculus@9
  1449
    }
icculus@9
  1450
icculus@39
  1451
    if (format12) {
icculus@39
  1452
        fmt = SDL20_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
icculus@39
  1453
    } else {
icculus@39
  1454
        fmt = VideoInfo.vfmt->format;
icculus@9
  1455
    }
icculus@9
  1456
icculus@39
  1457
    for (i = 0; i < VideoModesCount; i++) {
icculus@39
  1458
        VideoModeList *modes = &VideoModes[i];
icculus@39
  1459
        if (modes->format == fmt) {
icculus@39
  1460
            return modes->modes;
icculus@9
  1461
        }
icculus@9
  1462
    }
icculus@39
  1463
icculus@39
  1464
    SDL20_SetError("No modes support requested pixel format");
icculus@39
  1465
    return NULL;
icculus@9
  1466
}
icculus@9
  1467
icculus@35
  1468
DECLSPEC void SDLCALL
icculus@31
  1469
SDL_FreeCursor(SDL12_Cursor *cursor12)
icculus@31
  1470
{
icculus@31
  1471
    if (cursor12)
icculus@31
  1472
    {
icculus@31
  1473
        if (cursor12->wm_cursor)
icculus@31
  1474
            SDL20_FreeCursor(cursor12->wm_cursor);
icculus@31
  1475
        SDL20_free(cursor12->data);
icculus@31
  1476
        SDL20_free(cursor12->mask);
icculus@31
  1477
        SDL20_free(cursor12);
icculus@31
  1478
    }
icculus@31
  1479
}
icculus@23
  1480
icculus@35
  1481
DECLSPEC SDL12_Cursor * SDLCALL
icculus@23
  1482
SDL_CreateCursor(Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
icculus@23
  1483
{
icculus@23
  1484
    const size_t datasize = h * (w / 8);
icculus@23
  1485
    SDL_Cursor *cursor20 = NULL;
icculus@23
  1486
    SDL12_Cursor *retval = NULL;
icculus@23
  1487
icculus@23
  1488
    retval = (SDL12_Cursor *) SDL20_malloc(sizeof (SDL12_Cursor));
icculus@23
  1489
    if (!retval)
icculus@23
  1490
        goto outofmem;
icculus@23
  1491
icculus@34
  1492
    SDL20_zerop(retval);
icculus@23
  1493
icculus@23
  1494
    retval->data = (Uint8 *) SDL20_malloc(datasize);
icculus@31
  1495
    if (!retval->data)
icculus@23
  1496
        goto outofmem;
icculus@23
  1497
icculus@23
  1498
    retval->mask = (Uint8 *) SDL20_malloc(datasize);
icculus@31
  1499
    if (!retval->mask)
icculus@23
  1500
        goto outofmem;
icculus@23
  1501
icculus@23
  1502
    cursor20 = SDL20_CreateCursor(data, mask, w, h, hot_x, hot_y);
icculus@23
  1503
    if (!cursor20)
icculus@23
  1504
        goto failed;
icculus@23
  1505
icculus@23
  1506
    retval->area.w = w;
icculus@23
  1507
    retval->area.h = h;
icculus@23
  1508
    retval->hot_x = hot_x;
icculus@23
  1509
    retval->hot_y = hot_y;
icculus@23
  1510
    retval->wm_cursor = cursor20;
icculus@23
  1511
    /* we always leave retval->save as null pointers. */
icculus@23
  1512
icculus@23
  1513
    SDL20_memcpy(retval->data, data, datasize);
icculus@23
  1514
    SDL20_memcpy(retval->mask, mask, datasize);
icculus@23
  1515
icculus@23
  1516
    return retval;
icculus@23
  1517
icculus@23
  1518
outofmem:
icculus@31
  1519
    SDL20_OutOfMemory();
icculus@23
  1520
icculus@23
  1521
failed:
icculus@23
  1522
    SDL_FreeCursor(retval);
icculus@23
  1523
    return NULL;
icculus@23
  1524
}
icculus@23
  1525
icculus@35
  1526
DECLSPEC void SDLCALL
icculus@23
  1527
SDL_SetCursor(SDL12_Cursor *cursor)
icculus@23
  1528
{
icculus@23
  1529
    CurrentCursor = cursor;
icculus@23
  1530
    SDL20_SetCursor(cursor ? cursor->wm_cursor : NULL);
icculus@23
  1531
}
icculus@23
  1532
icculus@35
  1533
DECLSPEC SDL12_Cursor * SDLCALL
icculus@23
  1534
SDL_GetCursor(void)
icculus@23
  1535
{
icculus@23
  1536
    return CurrentCursor;
icculus@23
  1537
}
icculus@23
  1538
icculus@9
  1539
static void
icculus@9
  1540
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
icculus@9
  1541
{
icculus@16
  1542
    int display = VideoDisplayIndex;
icculus@9
  1543
    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
icculus@9
  1544
    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
icculus@9
  1545
    if (window) {
icculus@9
  1546
        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
icculus@9
  1547
            return;
icculus@9
  1548
        }
icculus@9
  1549
        if (SDL_strcmp(window, "center") == 0) {
icculus@9
  1550
            center = window;
icculus@9
  1551
        }
icculus@9
  1552
    }
icculus@9
  1553
    if (center) {
icculus@9
  1554
        *x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
icculus@9
  1555
        *y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
icculus@9
  1556
    }
icculus@9
  1557
}
icculus@9
  1558
icculus@34
  1559
#if SANITY_CHECK_THIS_CODE
icculus@9
  1560
static void
icculus@9
  1561
ClearVideoSurface()
icculus@9
  1562
{
icculus@16
  1563
    if (ShadowSurface) {
icculus@17
  1564
        SDL20_FillRect(ShadowSurface, NULL,
icculus@17
  1565
            SDL20_MapRGB(ShadowSurface->format, 0, 0, 0));
icculus@9
  1566
    }
icculus@17
  1567
    SDL20_FillRect(WindowSurface, NULL, 0);
icculus@19
  1568
    SDL20_UpdateWindowSurface(VideoWindow20);
icculus@9
  1569
}
icculus@34
  1570
#endif
icculus@9
  1571
icculus@9
  1572
static void
icculus@19
  1573
SetupScreenSaver(int flags12)
icculus@9
  1574
{
icculus@9
  1575
    const char *env;
icculus@9
  1576
    SDL_bool allow_screensaver;
icculus@9
  1577
icculus@9
  1578
    /* Allow environment override of screensaver disable */
icculus@9
  1579
    env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
icculus@9
  1580
    if (env) {
icculus@34
  1581
        allow_screensaver = SDL20_atoi(env) ? SDL_TRUE : SDL_FALSE;
icculus@19
  1582
    } else if (flags12 & SDL12_FULLSCREEN) {
icculus@9
  1583
        allow_screensaver = SDL_FALSE;
icculus@9
  1584
    } else {
icculus@9
  1585
        allow_screensaver = SDL_TRUE;
icculus@9
  1586
    }
icculus@9
  1587
    if (allow_screensaver) {
icculus@17
  1588
        SDL20_EnableScreenSaver();
icculus@9
  1589
    } else {
icculus@17
  1590
        SDL20_DisableScreenSaver();
icculus@9
  1591
    }
icculus@9
  1592
}
icculus@9
  1593
icculus@34
  1594
#if SANITY_CHECK_THIS_CODE
icculus@9
  1595
static int
icculus@19
  1596
ResizeVideoMode(int width, int height, int bpp, Uint32 flags12)
icculus@9
  1597
{
icculus@9
  1598
    int w, h;
icculus@9
  1599
icculus@9
  1600
    /* We can't resize something we don't have... */
icculus@16
  1601
    if (!VideoSurface) {
icculus@9
  1602
        return -1;
icculus@9
  1603
    }
icculus@9
  1604
icculus@9
  1605
    /* We probably have to recreate the window in fullscreen mode */
icculus@19
  1606
    if (flags12 & SDL12_FULLSCREEN) {
icculus@9
  1607
        return -1;
icculus@9
  1608
    }
icculus@9
  1609
icculus@9
  1610
    /* I don't think there's any change we can gracefully make in flags */
icculus@19
  1611
    if (flags12 != VideoFlags) {
icculus@9
  1612
        return -1;
icculus@9
  1613
    }
icculus@16
  1614
    if (bpp != VideoSurface->format->BitsPerPixel) {
icculus@9
  1615
        return -1;
icculus@9
  1616
    }
icculus@9
  1617
icculus@9
  1618
    /* Resize the window */
icculus@19
  1619
    SDL20_GetWindowSize(VideoWindow20, &w, &h);
icculus@9
  1620
    if (w != width || h != height) {
icculus@19
  1621
        SDL20_SetWindowSize(VideoWindow20, width, height);
icculus@9
  1622
    }
icculus@9
  1623
icculus@9
  1624
    /* If we're in OpenGL mode, just resize the stub surface and we're done! */
icculus@19
  1625
    if (flags12 & SDL12_OPENGL) {
icculus@16
  1626
        VideoSurface->w = width;
icculus@16
  1627
        VideoSurface->h = height;
icculus@9
  1628
        return 0;
icculus@9
  1629
    }
icculus@9
  1630
icculus@19
  1631
    WindowSurface = SDL20_GetWindowSurface(VideoWindow20);
icculus@16
  1632
    if (!WindowSurface) {
icculus@9
  1633
        return -1;
icculus@9
  1634
    }
icculus@16
  1635
    if (VideoSurface->format != WindowSurface->format) {
icculus@9
  1636
        return -1;
icculus@9
  1637
    }
icculus@16
  1638
    VideoSurface->w = width;
icculus@16
  1639
    VideoSurface->h = height;
icculus@16
  1640
    VideoSurface->pixels = WindowSurface->pixels;
icculus@16
  1641
    VideoSurface->pitch = WindowSurface->pitch;
icculus@19
  1642
    SDL20_SetClipRect(VideoSurface, NULL);
icculus@9
  1643
icculus@16
  1644
    if (ShadowSurface) {
icculus@16
  1645
        ShadowSurface->w = width;
icculus@16
  1646
        ShadowSurface->h = height;
icculus@19
  1647
        ShadowSurface->pitch = SDL20_CalculatePitch(ShadowSurface);
icculus@16
  1648
        ShadowSurface->pixels =
icculus@19
  1649
            SDL20_realloc(ShadowSurface->pixels,
icculus@16
  1650
                        ShadowSurface->h * ShadowSurface->pitch);
icculus@19
  1651
        SDL20_SetClipRect(ShadowSurface, NULL);
icculus@19
  1652
        SDL20_InvalidateMap(ShadowSurface->map);
icculus@9
  1653
    } else {
icculus@16
  1654
        PublicSurface = VideoSurface;
icculus@9
  1655
    }
icculus@9
  1656
icculus@9
  1657
    ClearVideoSurface();
icculus@9
  1658
icculus@9
  1659
    return 0;
icculus@9
  1660
}
icculus@9
  1661
icculus@35
  1662
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1663
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
icculus@9
  1664
{
icculus@9
  1665
    SDL_DisplayMode desktop_mode;
icculus@16
  1666
    int display = VideoDisplayIndex;
icculus@9
  1667
    int window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
icculus@9
  1668
    int window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
icculus@9
  1669
    int window_w;
icculus@9
  1670
    int window_h;
icculus@19
  1671
    Uint32 window_flags20;
icculus@19
  1672
    Uint32 surface_flags12;
icculus@19
  1673
icculus@19
  1674
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@19
  1675
        if (SDL_Init(SDL12_INIT_VIDEO | SDL12_INIT_NOPARACHUTE) < 0) {
icculus@9
  1676
            return NULL;
icculus@9
  1677
        }
icculus@9
  1678
    }
icculus@9
  1679
icculus@19
  1680
    SDL20_GetDesktopDisplayMode(display, &desktop_mode);
icculus@9
  1681
icculus@9
  1682
    if (width == 0) {
icculus@9
  1683
        width = desktop_mode.w;
icculus@9
  1684
    }
icculus@9
  1685
    if (height == 0) {
icculus@9
  1686
        height = desktop_mode.h;
icculus@9
  1687
    }
icculus@9
  1688
    if (bpp == 0) {
icculus@9
  1689
        bpp = SDL_BITSPERPIXEL(desktop_mode.format);
icculus@9
  1690
    }
icculus@9
  1691
icculus@9
  1692
    /* See if we can simply resize the existing window and surface */
icculus@19
  1693
    if (ResizeVideoMode(width, height, bpp, flags12) == 0) {
icculus@16
  1694
        return PublicSurface;
icculus@9
  1695
    }
icculus@9
  1696
icculus@9
  1697
    /* Destroy existing window */
icculus@16
  1698
    PublicSurface = NULL;
icculus@16
  1699
    if (ShadowSurface) {
icculus@16
  1700
        ShadowSurface->flags &= ~SDL_DONTFREE;
icculus@19
  1701
        SDL20_FreeSurface(ShadowSurface);
icculus@16
  1702
        ShadowSurface = NULL;
icculus@9
  1703
    }
icculus@16
  1704
    if (VideoSurface) {
icculus@16
  1705
        VideoSurface->flags &= ~SDL_DONTFREE;
icculus@19
  1706
        SDL20_FreeSurface(VideoSurface);
icculus@16
  1707
        VideoSurface = NULL;
icculus@9
  1708
    }
icculus@16
  1709
    if (VideoContext) {
icculus@9
  1710
        /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
icculus@19
  1711
        SDL20_GL_DeleteContext(VideoContext);
icculus@16
  1712
        VideoContext = NULL;
icculus@9
  1713
    }
icculus@19
  1714
    if (VideoWindow20) {
icculus@19
  1715
        SDL20_GetWindowPosition(VideoWindow20, &window_x, &window_y);
icculus@19
  1716
        SDL20_DestroyWindow(VideoWindow20);
icculus@9
  1717
    }
icculus@9
  1718
icculus@19
  1719
    /* Create a new window */
icculus@19
  1720
    window_flags20 = SDL_WINDOW_SHOWN;
icculus@19
  1721
    if (flags12 & SDL12_FULLSCREEN) {
icculus@19
  1722
        window_flags20 |= SDL_WINDOW_FULLSCREEN;
icculus@9
  1723
    }
icculus@19
  1724
    if (flags12 & SDL12_OPENGL) {
icculus@19
  1725
        window_flags20 |= SDL_WINDOW_OPENGL;
icculus@9
  1726
    }
icculus@19
  1727
    if (flags12 & SDL12_RESIZABLE) {
icculus@19
  1728
        window_flags20 |= SDL_WINDOW_RESIZABLE;
icculus@9
  1729
    }
icculus@19
  1730
    if (flags12 & SDL12_NOFRAME) {
icculus@19
  1731
        window_flags20 |= SDL_WINDOW_BORDERLESS;
icculus@9
  1732
    }
icculus@9
  1733
    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
icculus@19
  1734
    VideoWindow20 =
icculus@19
  1735
        SDL20_CreateWindow(WindowTitle, window_x, window_y, width, height,
icculus@19
  1736
                         window_flags20);
icculus@19
  1737
    if (!VideoWindow20) {
icculus@9
  1738
        return NULL;
icculus@9
  1739
    }
icculus@19
  1740
    SDL20_SetWindowIcon(VideoWindow20, VideoIcon);
icculus@19
  1741
icculus@19
  1742
    SetupScreenSaver(flags12);
icculus@19
  1743
icculus@34
  1744
    window_flags20 = SDL20_GetWindowFlags(VideoWindow20);
icculus@19
  1745
    surface_flags12 = 0;
icculus@19
  1746
    if (window_flags20 & SDL_WINDOW_FULLSCREEN) {
icculus@19
  1747
        surface_flags12 |= SDL_FULLSCREEN;
icculus@9
  1748
    }
icculus@19
  1749
    if ((window_flags & SDL_WINDOW_OPENGL) && (flags12 & SDL_OPENGL)) {
icculus@19
  1750
        surface_flags12 |= SDL_OPENGL;
icculus@9
  1751
    }
icculus@9
  1752
    if (window_flags & SDL_WINDOW_RESIZABLE) {
icculus@19
  1753
        surface_flags12 |= SDL_RESIZABLE;
icculus@9
  1754
    }
icculus@9
  1755
    if (window_flags & SDL_WINDOW_BORDERLESS) {
icculus@19
  1756
        surface_flags12 |= SDL_NOFRAME;
icculus@9
  1757
    }
icculus@9
  1758
icculus@19
  1759
    VideoFlags = flags12;
icculus@9
  1760
icculus@9
  1761
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
icculus@19
  1762
    if (flags12 & SDL_OPENGL) {
icculus@31
  1763
icculus@19
  1764
        VideoContext = SDL_GL_CreateContext(VideoWindow20);
icculus@16
  1765
        if (!VideoContext) {
icculus@9
  1766
            return NULL;
icculus@9
  1767
        }
icculus@19
  1768
        if (SDL_GL_MakeCurrent(VideoWindow20, VideoContext) < 0) {
icculus@9
  1769
            return NULL;
icculus@9
  1770
        }
icculus@30
  1771
icculus@31
  1772
        SDL20_GL_SetSwapInterval(SwapInterval);  /* don't care if this fails. */
icculus@30
  1773
icculus@16
  1774
        VideoSurface =
icculus@9
  1775
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
icculus@16
  1776
        if (!VideoSurface) {
icculus@9
  1777
            return NULL;
icculus@9
  1778
        }
icculus@19
  1779
        VideoSurface->flags |= surface_flags12;
icculus@16
  1780
        PublicSurface = VideoSurface;
icculus@16
  1781
        return PublicSurface;
icculus@9
  1782
    }
icculus@9
  1783
icculus@9
  1784
    /* Create the screen surface */
icculus@19
  1785
    WindowSurface = SDL_GetWindowSurface(VideoWindow20);
icculus@16
  1786
    if (!WindowSurface) {
icculus@9
  1787
        return NULL;
icculus@9
  1788
    }
icculus@9
  1789
icculus@9
  1790
    /* Center the public surface in the window surface */
icculus@19
  1791
    SDL_GetWindowSize(VideoWindow20, &window_w, &window_h);
icculus@16
  1792
    VideoViewport.x = (window_w - width)/2;
icculus@16
  1793
    VideoViewport.y = (window_h - height)/2;
icculus@16
  1794
    VideoViewport.w = width;
icculus@16
  1795
    VideoViewport.h = height;
icculus@9
  1796
icculus@16
  1797
    VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
icculus@19
  1798
    VideoSurface->flags |= surface_flags12;
icculus@19
  1799
    VideoSurface->flags |= SDL12_DONTFREE;
icculus@16
  1800
    SDL_FreeFormat(VideoSurface->format);
icculus@16
  1801
    VideoSurface->format = WindowSurface->format;
icculus@16
  1802
    VideoSurface->format->refcount++;
icculus@16
  1803
    VideoSurface->w = width;
icculus@16
  1804
    VideoSurface->h = height;
icculus@16
  1805
    VideoSurface->pitch = WindowSurface->pitch;
icculus@16
  1806
    VideoSurface->pixels = (void *)((Uint8 *)WindowSurface->pixels +
icculus@16
  1807
        VideoViewport.y * VideoSurface->pitch +
icculus@16
  1808
        VideoViewport.x  * VideoSurface->format->BytesPerPixel);
icculus@16
  1809
    SDL_SetClipRect(VideoSurface, NULL);
icculus@9
  1810
icculus@9
  1811
    /* Create a shadow surface if necessary */
icculus@16
  1812
    if ((bpp != VideoSurface->format->BitsPerPixel)
icculus@19
  1813
        && !(flags12 & SDL12_ANYFORMAT)) {
icculus@16
  1814
        ShadowSurface =
icculus@9
  1815
            SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
icculus@16
  1816
        if (!ShadowSurface) {
icculus@9
  1817
            return NULL;
icculus@9
  1818
        }
icculus@19
  1819
        ShadowSurface->flags |= surface_flags12;
icculus@19
  1820
        ShadowSurface->flags |= SDL12_DONTFREE;
icculus@9
  1821
icculus@16
  1822
        /* 8-bit ShadowSurface surfaces report that they have exclusive palette */
icculus@16
  1823
        if (ShadowSurface->format->palette) {
icculus@19
  1824
            ShadowSurface->flags |= SDL12_HWPALETTE;
icculus@16
  1825
            SDL_DitherColors(ShadowSurface->format->palette->colors,
icculus@16
  1826
                             ShadowSurface->format->BitsPerPixel);
icculus@9
  1827
        }
icculus@16
  1828
        SDL_FillRect(ShadowSurface, NULL,
icculus@16
  1829
            SDL_MapRGB(ShadowSurface->format, 0, 0, 0));
icculus@9
  1830
    }
icculus@16
  1831
    PublicSurface =
icculus@16
  1832
        (ShadowSurface ? ShadowSurface : VideoSurface);
icculus@9
  1833
icculus@9
  1834
    ClearVideoSurface();
icculus@9
  1835
icculus@9
  1836
    /* We're finally done! */
icculus@16
  1837
    return PublicSurface;
icculus@9
  1838
}
icculus@9
  1839
icculus@35
  1840
DECLSPEC SDL12_Surface * SDLCALL
icculus@9
  1841
SDL_GetVideoSurface(void)
icculus@9
  1842
{
icculus@16
  1843
    return PublicSurface;
icculus@9
  1844
}
icculus@9
  1845
icculus@35
  1846
DECLSPEC int SDLCALL
icculus@35
  1847
SDL_SetAlpha(SDL12_Surface * surface, Uint32 flag, Uint8 value)
icculus@9
  1848
{
icculus@9
  1849
    if (flag & SDL_SRCALPHA) {
icculus@9
  1850
        /* According to the docs, value is ignored for alpha surfaces */
icculus@9
  1851
        if (surface->format->Amask) {
icculus@9
  1852
            value = 0xFF;
icculus@9
  1853
        }
icculus@9
  1854
        SDL_SetSurfaceAlphaMod(surface, value);
icculus@9
  1855
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
icculus@9
  1856
    } else {
icculus@9
  1857
        SDL_SetSurfaceAlphaMod(surface, 0xFF);
icculus@9
  1858
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
icculus@9
  1859
    }
icculus@9
  1860
    SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
icculus@9
  1861
icculus@9
  1862
    return 0;
icculus@9
  1863
}
icculus@9
  1864
icculus@35
  1865
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1866
SDL_DisplayFormat(SDL12_Surface *surface12)
icculus@9
  1867
{
icculus@19
  1868
    SDL12_PixelFormat *format;
icculus@9
  1869
icculus@16
  1870
    if (!PublicSurface) {
icculus@19
  1871
        SDL20_SetError("No video mode has been set");
icculus@9
  1872
        return NULL;
icculus@9
  1873
    }
icculus@16
  1874
    format = PublicSurface->format;
icculus@9
  1875
icculus@9
  1876
    /* Set the flags appropriate for copying to display surface */
icculus@19
  1877
    return SDL_ConvertSurface(surface, format, SDL12_RLEACCEL);
icculus@9
  1878
}
icculus@9
  1879
icculus@35
  1880
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1881
SDL_DisplayFormatAlpha(SDL12_Surface *surface)
icculus@9
  1882
{
icculus@9
  1883
    SDL_PixelFormat *vf;
icculus@9
  1884
    SDL_PixelFormat *format;
icculus@9
  1885
    SDL_Surface *converted;
icculus@9
  1886
    /* default to ARGB8888 */
icculus@9
  1887
    Uint32 amask = 0xff000000;
icculus@9
  1888
    Uint32 rmask = 0x00ff0000;
icculus@9
  1889
    Uint32 gmask = 0x0000ff00;
icculus@9
  1890
    Uint32 bmask = 0x000000ff;
icculus@9
  1891
icculus@16
  1892
    if (!PublicSurface) {
icculus@34
  1893
        SDL20_SetError("No video mode has been set");
icculus@9
  1894
        return NULL;
icculus@9
  1895
    }
icculus@16
  1896
    vf = PublicSurface->format;
icculus@9
  1897
icculus@9
  1898
    switch (vf->BytesPerPixel) {
icculus@9
  1899
    case 2:
icculus@9
  1900
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
icculus@9
  1901
           For anything else (like ARGB4444) it doesn't matter
icculus@9
  1902
           since we have no special code for it anyway */
icculus@9
  1903
        if ((vf->Rmask == 0x1f) &&
icculus@9
  1904
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
icculus@9
  1905
            rmask = 0xff;
icculus@9
  1906
            bmask = 0xff0000;
icculus@9
  1907
        }
icculus@9
  1908
        break;
icculus@9
  1909
icculus@9
  1910
    case 3:
icculus@9
  1911
    case 4:
icculus@9
  1912
        /* Keep the video format, as long as the high 8 bits are
icculus@9
  1913
           unused or alpha */
icculus@9
  1914
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
icculus@9
  1915
            rmask = 0xff;
icculus@9
  1916
            bmask = 0xff0000;
icculus@9
  1917
        }
icculus@9
  1918
        break;
icculus@9
  1919
icculus@9
  1920
    default:
icculus@9
  1921
        /* We have no other optimised formats right now. When/if a new
icculus@9
  1922
           optimised alpha format is written, add the converter here */
icculus@9
  1923
        break;
icculus@9
  1924
    }
icculus@9
  1925
    format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask,
icculus@9
  1926
                                                            gmask,
icculus@9
  1927
                                                            bmask,
icculus@9
  1928
                                                            amask));
icculus@9
  1929
    if (!format) {
icculus@9
  1930
        return NULL;
icculus@9
  1931
    }
icculus@9
  1932
    converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
icculus@9
  1933
    SDL_FreeFormat(format);
icculus@9
  1934
    return converted;
icculus@9
  1935
}
icculus@9
  1936
icculus@35
  1937
DECLSPEC void SDLCALL
icculus@9
  1938
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
icculus@9
  1939
{
icculus@9
  1940
    int i;
icculus@9
  1941
icculus@16
  1942
    if (screen == ShadowSurface) {
icculus@9
  1943
        for (i = 0; i < numrects; ++i) {
icculus@16
  1944
            SDL_BlitSurface(ShadowSurface, &rects[i], VideoSurface,
icculus@9
  1945
                            &rects[i]);
icculus@9
  1946
        }
icculus@9
  1947
icculus@9
  1948
        /* Fall through to video surface update */
icculus@16
  1949
        screen = VideoSurface;
icculus@9
  1950
    }
icculus@16
  1951
    if (screen == VideoSurface) {
icculus@16
  1952
        if (VideoViewport.x || VideoViewport.y) {
icculus@9
  1953
            SDL_Rect *stackrects = SDL_stack_alloc(SDL_Rect, numrects);
icculus@9
  1954
            SDL_Rect *stackrect;
icculus@9
  1955
            const SDL_Rect *rect;
icculus@9
  1956
            
icculus@9
  1957
            /* Offset all the rectangles before updating */
icculus@9
  1958
            for (i = 0; i < numrects; ++i) {
icculus@9
  1959
                rect = &rects[i];
icculus@9
  1960
                stackrect = &stackrects[i];
icculus@16
  1961
                stackrect->x = VideoViewport.x + rect->x;
icculus@16
  1962
                stackrect->y = VideoViewport.y + rect->y;
icculus@9
  1963
                stackrect->w = rect->w;
icculus@9
  1964
                stackrect->h = rect->h;
icculus@9
  1965
            }
icculus@19
  1966
            SDL_UpdateWindowSurfaceRects(VideoWindow20, stackrects, numrects);
icculus@9
  1967
            SDL_stack_free(stackrects);
icculus@9
  1968
        } else {
icculus@19
  1969
            SDL_UpdateWindowSurfaceRects(VideoWindow20, rects, numrects);
icculus@9
  1970
        }
icculus@9
  1971
    }
icculus@9
  1972
}
icculus@34
  1973
#endif
icculus@9
  1974
icculus@35
  1975
DECLSPEC void SDLCALL
icculus@35
  1976
SDL_UpdateRect(SDL12_Surface *screen12, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
icculus@31
  1977
{
icculus@35
  1978
    if (screen12) {
icculus@31
  1979
        SDL_Rect rect;
icculus@31
  1980
        rect.x = (int) x;
icculus@31
  1981
        rect.y = (int) y;
icculus@35
  1982
        rect.w = (int) (w ? w : screen12->w);
icculus@35
  1983
        rect.h = (int) (h ? h : screen12->h);
icculus@35
  1984
        SDL_UpdateRects(screen12, 1, &rect);
icculus@31
  1985
    }
icculus@31
  1986
}
icculus@31
  1987
icculus@35
  1988
DECLSPEC int SDLCALL
icculus@35
  1989
SDL_Flip(SDL12_Surface *screen12)
icculus@31
  1990
{
icculus@35
  1991
    SDL_UpdateRect(screen12, 0, 0, 0, 0);
icculus@31
  1992
    return 0;
icculus@31
  1993
}
icculus@31
  1994
icculus@35
  1995
DECLSPEC void SDLCALL
icculus@9
  1996
SDL_WM_SetCaption(const char *title, const char *icon)
icculus@9
  1997
{
icculus@16
  1998
    if (WindowTitle) {
icculus@31
  1999
        SDL20_free(WindowTitle);
icculus@9
  2000
    }
icculus@16
  2001
    if (WindowIconTitle) {
icculus@31
  2002
        SDL20_free(WindowIconTitle);
icculus@9
  2003
    }
icculus@16
  2004
    WindowTitle = title ? SDL_strdup(title) : NULL;
icculus@16
  2005
    WindowIconTitle = icon ? SDL_strdup(icon) : NULL;
icculus@34
  2006
    SDL20_SetWindowTitle(VideoWindow20, WindowTitle);
icculus@9
  2007
}
icculus@9
  2008
icculus@35
  2009
DECLSPEC void SDLCALL
icculus@9
  2010
SDL_WM_GetCaption(const char **title, const char **icon)
icculus@9
  2011
{
icculus@9
  2012
    if (title) {
icculus@16
  2013
        *title = WindowTitle;
icculus@9
  2014
    }
icculus@9
  2015
    if (icon) {
icculus@16
  2016
        *icon = WindowIconTitle;
icculus@9
  2017
    }
icculus@9
  2018
}
icculus@9
  2019
icculus@34
  2020
#if SANITY_CHECK_THIS_CODE
icculus@35
  2021
DECLSPEC void SDLCALL
icculus@34
  2022
SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
icculus@9
  2023
{
icculus@9
  2024
    // !!! FIXME: free previous icon?
icculus@16
  2025
    VideoIcon = icon;
icculus@16
  2026
    ++VideoIcon->refcount;
icculus@9
  2027
}
icculus@34
  2028
#endif
icculus@9
  2029
icculus@35
  2030
DECLSPEC int SDLCALL
icculus@9
  2031
SDL_WM_IconifyWindow(void)
icculus@9
  2032
{
icculus@34
  2033
    SDL20_MinimizeWindow(VideoWindow20);
icculus@9
  2034
    return 0;
icculus@9
  2035
}
icculus@9
  2036
icculus@34
  2037
#if SANITY_CHECK_THIS_CODE
icculus@35
  2038
DECLSPEC int SDLCALL
icculus@34
  2039
SDL_WM_ToggleFullScreen(SDL_Surface *surface)
icculus@9
  2040
{
icculus@9
  2041
    int length;
icculus@9
  2042
    void *pixels;
icculus@9
  2043
    Uint8 *src, *dst;
icculus@9
  2044
    int row;
icculus@9
  2045
    int window_w;
icculus@9
  2046
    int window_h;
icculus@9
  2047
icculus@16
  2048
    if (!PublicSurface) {
icculus@34
  2049
        SDL20_SetError("SDL_SetVideoMode() hasn't been called");
icculus@9
  2050
        return 0;
icculus@9
  2051
    }
icculus@9
  2052
icculus@9
  2053
    /* Copy the old bits out */
icculus@16
  2054
    length = PublicSurface->w * PublicSurface->format->BytesPerPixel;
icculus@31
  2055
    pixels = SDL20_malloc(PublicSurface->h * length);
icculus@16
  2056
    if (pixels && PublicSurface->pixels) {
icculus@16
  2057
        src = (Uint8*)PublicSurface->pixels;
icculus@9
  2058
        dst = (Uint8*)pixels;
icculus@16
  2059
        for (row = 0; row < PublicSurface->h; ++row) {
icculus@9
  2060
            SDL_memcpy(dst, src, length);
icculus@16
  2061
            src += PublicSurface->pitch;
icculus@9
  2062
            dst += length;
icculus@9
  2063
        }
icculus@9
  2064
    }
icculus@9
  2065
icculus@9
  2066
    /* Do the physical mode switch */
icculus@34
  2067
    if (SDL20_GetWindowFlags(VideoWindow20) & SDL_WINDOW_FULLSCREEN) {
icculus@34
  2068
        if (SDL20_SetWindowFullscreen(VideoWindow20, 0) < 0) {
icculus@9
  2069
            return 0;
icculus@9
  2070
        }
icculus@16
  2071
        PublicSurface->flags &= ~SDL_FULLSCREEN;
icculus@9
  2072
    } else {
icculus@34
  2073
        if (SDL20_SetWindowFullscreen(VideoWindow20, 1) < 0) {
icculus@9
  2074
            return 0;
icculus@9
  2075
        }
icculus@16
  2076
        PublicSurface->flags |= SDL_FULLSCREEN;
icculus@9
  2077
    }
icculus@9
  2078
icculus@9
  2079
    /* Recreate the screen surface */
icculus@34
  2080
    WindowSurface = SDL20_GetWindowSurface(VideoWindow20);
icculus@16
  2081
    if (!WindowSurface) {
icculus@9
  2082
        /* We're totally hosed... */
icculus@9
  2083
        return 0;
icculus@9
  2084
    }
icculus@9
  2085
icculus@9
  2086
    /* Center the public surface in the window surface */
icculus@19
  2087
    SDL_GetWindowSize(VideoWindow20, &window_w, &window_h);
icculus@16
  2088
    VideoViewport.x = (window_w - VideoSurface->w)/2;
icculus@16
  2089
    VideoViewport.y = (window_h - VideoSurface->h)/2;
icculus@16
  2090
    VideoViewport.w = VideoSurface->w;
icculus@16
  2091
    VideoViewport.h = VideoSurface->h;
icculus@9
  2092
icculus@9
  2093
    /* Do some shuffling behind the application's back if format changes */
icculus@16
  2094
    if (VideoSurface->format->format != WindowSurface->format->format) {
icculus@16
  2095
        if (ShadowSurface) {
icculus@16
  2096
            if (ShadowSurface->format->format == WindowSurface->format->format) {
icculus@9
  2097
                /* Whee!  We don't need a shadow surface anymore! */
icculus@16
  2098
                VideoSurface->flags &= ~SDL_DONTFREE;
icculus@16
  2099
                SDL_FreeSurface(VideoSurface);
icculus@31
  2100
                SDL20_free(ShadowSurface->pixels);
icculus@16
  2101
                VideoSurface = ShadowSurface;
icculus@16
  2102
                VideoSurface->flags |= SDL_PREALLOC;
icculus@16
  2103
                ShadowSurface = NULL;
icculus@9
  2104
            } else {
icculus@9
  2105
                /* No problem, just change the video surface format */
icculus@16
  2106
                SDL_FreeFormat(VideoSurface->format);
icculus@16
  2107
                VideoSurface->format = WindowSurface->format;
icculus@16
  2108
                VideoSurface->format->refcount++;
icculus@16
  2109
                SDL_InvalidateMap(ShadowSurface->map);
icculus@9
  2110
            }
icculus@9
  2111
        } else {
icculus@9
  2112
            /* We can make the video surface the shadow surface */
icculus@16
  2113
            ShadowSurface = VideoSurface;
icculus@16
  2114
            ShadowSurface->pitch = SDL_CalculatePitch(ShadowSurface);
icculus@31
  2115
            ShadowSurface->pixels = SDL20_malloc(ShadowSurface->h * ShadowSurface->pitch);
icculus@16
  2116
            if (!ShadowSurface->pixels) {
icculus@9
  2117
                /* Uh oh, we're hosed */
icculus@16
  2118
                ShadowSurface = NULL;
icculus@9
  2119
                return 0;
icculus@9
  2120
            }
icculus@16
  2121
            ShadowSurface->flags &= ~SDL_PREALLOC;
icculus@9
  2122
icculus@16
  2123
            VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
icculus@16
  2124
            VideoSurface->flags = ShadowSurface->flags;
icculus@16
  2125
            VideoSurface->flags |= SDL_PREALLOC;
icculus@16
  2126
            SDL_FreeFormat(VideoSurface->format);
icculus@16
  2127
            VideoSurface->format = WindowSurface->format;
icculus@16
  2128
            VideoSurface->format->refcount++;
icculus@16
  2129
            VideoSurface->w = ShadowSurface->w;
icculus@16
  2130
            VideoSurface->h = ShadowSurface->h;
icculus@9
  2131
        }
icculus@9
  2132
    }
icculus@9
  2133
icculus@9
  2134
    /* Update the video surface */
icculus@16
  2135
    VideoSurface->pitch = WindowSurface->pitch;
icculus@16
  2136
    VideoSurface->pixels = (void *)((Uint8 *)WindowSurface->pixels +
icculus@16
  2137
        VideoViewport.y * VideoSurface->pitch +
icculus@16
  2138
        VideoViewport.x  * VideoSurface->format->BytesPerPixel);
icculus@16
  2139
    SDL_SetClipRect(VideoSurface, NULL);
icculus@9
  2140
icculus@9
  2141
    /* Copy the old bits back */
icculus@9
  2142
    if (pixels) {
icculus@9
  2143
        src = (Uint8*)pixels;
icculus@16
  2144
        dst = (Uint8*)PublicSurface->pixels;
icculus@16
  2145
        for (row = 0; row < PublicSurface->h; ++row) {
icculus@9
  2146
            SDL_memcpy(dst, src, length);
icculus@9
  2147
            src += length;
icculus@16
  2148
            dst += PublicSurface->pitch;
icculus@9
  2149
        }
icculus@16
  2150
        SDL_Flip(PublicSurface);
icculus@31
  2151
        SDL20_free(pixels);
icculus@9
  2152
    }
icculus@9
  2153
icculus@9
  2154
    /* We're done! */
icculus@9
  2155
    return 1;
icculus@9
  2156
}
icculus@34
  2157
#endif
icculus@9
  2158
icculus@31
  2159
typedef enum
icculus@9
  2160
{
icculus@31
  2161
    SDL12_GRAB_QUERY = -1,
icculus@31
  2162
    SDL12_GRAB_OFF = 0,
icculus@31
  2163
    SDL12_GRAB_ON = 1
icculus@31
  2164
} SDL12_GrabMode;
icculus@31
  2165
icculus@35
  2166
DECLSPEC SDL12_GrabMode SDLCALL
icculus@31
  2167
SDL_WM_GrabInput(SDL12_GrabMode mode)
icculus@31
  2168
{
icculus@31
  2169
    if (mode != SDL12_GRAB_QUERY) {
icculus@34
  2170
        SDL20_SetWindowGrab(VideoWindow20, (mode == SDL12_GRAB_ON));
icculus@9
  2171
    }
icculus@34
  2172
    return SDL20_GetWindowGrab(VideoWindow20) ? SDL12_GRAB_ON : SDL12_GRAB_OFF;
icculus@9
  2173
}
icculus@9
  2174
icculus@35
  2175
DECLSPEC void SDLCALL
icculus@9
  2176
SDL_WarpMouse(Uint16 x, Uint16 y)
icculus@9
  2177
{
icculus@34
  2178
    SDL20_WarpMouseInWindow(VideoWindow20, x, y);
icculus@9
  2179
}
icculus@9
  2180
icculus@35
  2181
DECLSPEC Uint8 SDLCALL
icculus@9
  2182
SDL_GetAppState(void)
icculus@9
  2183
{
icculus@34
  2184
    Uint8 state12 = 0;
icculus@34
  2185
    Uint32 flags20 = 0;
icculus@34
  2186
icculus@34
  2187
    flags20 = SDL20_GetWindowFlags(VideoWindow20);
icculus@34
  2188
    if ((flags20 & SDL_WINDOW_SHOWN) && !(flags20 & SDL_WINDOW_MINIMIZED)) {
icculus@34
  2189
        state12 |= SDL12_APPACTIVE;
icculus@9
  2190
    }
icculus@34
  2191
    if (flags20 & SDL_WINDOW_INPUT_FOCUS) {
icculus@34
  2192
        state12 |= SDL12_APPINPUTFOCUS;
icculus@9
  2193
    }
icculus@34
  2194
    if (flags20 & SDL_WINDOW_MOUSE_FOCUS) {
icculus@34
  2195
        state12 |= SDL12_APPMOUSEFOCUS;
icculus@9
  2196
    }
icculus@34
  2197
    return state12;
icculus@9
  2198
}
icculus@9
  2199
icculus@34
  2200
#if SANITY_CHECK_THIS_CODE
icculus@35
  2201
DECLSPEC int SDLCALL
icculus@35
  2202
SDL_SetPalette(SDL12_Surface *surface12, int flags, const SDL_Color *colors,
icculus@9
  2203
               int firstcolor, int ncolors)
icculus@9
  2204
{
icculus@35
  2205
    return SDL_SetColors(surface12, colors, firstcolor, ncolors);
icculus@9
  2206
}
icculus@9
  2207
icculus@35
  2208
DECLSPEC int SDLCALL
icculus@35
  2209
SDL_SetColors(SDL12_Surface *surface12, const SDL_Color * colors, int firstcolor,
icculus@9
  2210
              int ncolors)
icculus@9
  2211
{
icculus@35
  2212
    if (SDL20_SetPaletteColors
icculus@9
  2213
        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
icculus@9
  2214
        return 1;
icculus@9
  2215
    } else {
icculus@9
  2216
        return 0;
icculus@9
  2217
    }
icculus@9
  2218
}
icculus@9
  2219
icculus@35
  2220
DECLSPEC int SDLCALL
icculus@9
  2221
SDL_GetWMInfo(SDL_SysWMinfo * info)
icculus@9
  2222
{
icculus@19
  2223
    return SDL_GetWindowWMInfo(VideoWindow20, info);
icculus@9
  2224
}
icculus@9
  2225
icculus@9
  2226
struct private_yuvhwdata
icculus@9
  2227
{
icculus@9
  2228
    SDL_SW_YUVTexture *texture;
icculus@9
  2229
    SDL_Surface *display;
icculus@9
  2230
    Uint32 display_format;
icculus@9
  2231
};
icculus@9
  2232
icculus@35
  2233
DECLSPEC SDL_Overlay * SDLCALL
icculus@35
  2234
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL12_Surface *display)
icculus@9
  2235
{
icculus@9
  2236
    SDL_Overlay *overlay;
icculus@9
  2237
    Uint32 texture_format;
icculus@9
  2238
    SDL_SW_YUVTexture *texture;
icculus@9
  2239
icculus@9
  2240
    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
icculus@34
  2241
        SDL20_SetError("YUV overlays are not supported in OpenGL mode");
icculus@9
  2242
        return NULL;
icculus@9
  2243
    }
icculus@9
  2244
icculus@16
  2245
    if (display != PublicSurface) {
icculus@34
  2246
        SDL20_SetError("YUV display is only supported on the screen surface");
icculus@9
  2247
        return NULL;
icculus@9
  2248
    }
icculus@9
  2249
icculus@9
  2250
    switch (format) {
icculus@9
  2251
    case SDL_YV12_OVERLAY:
icculus@9
  2252
        texture_format = SDL_PIXELFORMAT_YV12;
icculus@9
  2253
        break;
icculus@9
  2254
    case SDL_IYUV_OVERLAY:
icculus@9
  2255
        texture_format = SDL_PIXELFORMAT_IYUV;
icculus@9
  2256
        break;
icculus@9
  2257
    case SDL_YUY2_OVERLAY:
icculus@9
  2258
        texture_format = SDL_PIXELFORMAT_YUY2;
icculus@9
  2259
        break;
icculus@9
  2260
    case SDL_UYVY_OVERLAY:
icculus@9
  2261
        texture_format = SDL_PIXELFORMAT_UYVY;
icculus@9
  2262
        break;
icculus@9
  2263
    case SDL_YVYU_OVERLAY:
icculus@9
  2264
        texture_format = SDL_PIXELFORMAT_YVYU;
icculus@9
  2265
        break;
icculus@9
  2266
    default:
icculus@34
  2267
        SDL20_SetError("Unknown YUV format");
icculus@9
  2268
        return NULL;
icculus@9
  2269
    }
icculus@9
  2270
icculus@31
  2271
    overlay = (SDL_Overlay *) SDL20_malloc(sizeof(*overlay));
icculus@9
  2272
    if (!overlay) {
icculus@31
  2273
        SDL20_OutOfMemory();
icculus@9
  2274
        return NULL;
icculus@9
  2275
    }
icculus@34
  2276
    SDL20_zerop(overlay);
icculus@9
  2277
icculus@9
  2278
    overlay->hwdata =
icculus@31
  2279
        (struct private_yuvhwdata *) SDL20_malloc(sizeof(*overlay->hwdata));
icculus@9
  2280
    if (!overlay->hwdata) {
icculus@31
  2281
        SDL20_free(overlay);
icculus@31
  2282
        SDL20_OutOfMemory();
icculus@9
  2283
        return NULL;
icculus@9
  2284
    }
icculus@9
  2285
icculus@9
  2286
    texture = SDL_SW_CreateYUVTexture(texture_format, w, h);
icculus@9
  2287
    if (!texture) {
icculus@31
  2288
        SDL20_free(overlay->hwdata);
icculus@31
  2289
        SDL20_free(overlay);
icculus@9
  2290
        return NULL;
icculus@9
  2291
    }
icculus@9
  2292
    overlay->hwdata->texture = texture;
icculus@9
  2293
    overlay->hwdata->display = NULL;
icculus@9
  2294
    overlay->hwdata->display_format = SDL_PIXELFORMAT_UNKNOWN;
icculus@9
  2295
icculus@9
  2296
    overlay->format = format;
icculus@9
  2297
    overlay->w = w;
icculus@9
  2298
    overlay->h = h;
icculus@9
  2299
    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
icculus@9
  2300
        overlay->planes = 3;
icculus@9
  2301
    } else {
icculus@9
  2302
        overlay->planes = 1;
icculus@9
  2303
    }
icculus@9
  2304
    overlay->pitches = texture->pitches;
icculus@9
  2305
    overlay->pixels = texture->planes;
icculus@9
  2306
icculus@9
  2307
    return overlay;
icculus@9
  2308
}
icculus@9
  2309
icculus@35
  2310
DECLSPEC int SDLCALL
icculus@9
  2311
SDL_LockYUVOverlay(SDL_Overlay * overlay)
icculus@9
  2312
{
icculus@9
  2313
    SDL_Rect rect;
icculus@9
  2314
    void *pixels;
icculus@9
  2315
    int pitch;
icculus@9
  2316
icculus@9
  2317
    if (!overlay) {
icculus@34
  2318
        return SDL20_SetError("Passed a NULL overlay");
icculus@9
  2319
    }
icculus@9
  2320
icculus@9
  2321
    rect.x = 0;
icculus@9
  2322
    rect.y = 0;
icculus@9
  2323
    rect.w = overlay->w;
icculus@9
  2324
    rect.h = overlay->h;
icculus@9
  2325
icculus@9
  2326
    if (SDL_SW_LockYUVTexture(overlay->hwdata->texture, &rect, &pixels, &pitch) < 0) {
icculus@9
  2327
        return -1;
icculus@9
  2328
    }
icculus@9
  2329
icculus@9
  2330
    overlay->pixels[0] = (Uint8 *) pixels;
icculus@9
  2331
    overlay->pitches[0] = pitch;
icculus@9
  2332
    switch (overlay->format) {
icculus@9
  2333
    case SDL_YV12_OVERLAY:
icculus@9
  2334
    case SDL_IYUV_OVERLAY:
icculus@9
  2335
        overlay->pitches[1] = pitch / 2;
icculus@9
  2336
        overlay->pitches[2] = pitch / 2;
icculus@9
  2337
        overlay->pixels[1] =
icculus@9
  2338
            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
icculus@9
  2339
        overlay->pixels[2] =
icculus@9
  2340
            overlay->pixels[1] + overlay->pitches[1] * overlay->h / 2;
icculus@9
  2341
        break;
icculus@9
  2342
    case SDL_YUY2_OVERLAY:
icculus@9
  2343
    case SDL_UYVY_OVERLAY:
icculus@9
  2344
    case SDL_YVYU_OVERLAY:
icculus@9
  2345
        break;
icculus@9
  2346
    }
icculus@9
  2347
    return 0;
icculus@9
  2348
}
icculus@9
  2349
icculus@35
  2350
DECLSPEC void SDLCALL
icculus@9
  2351
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
icculus@9
  2352
{
icculus@9
  2353
    if (!overlay) {
icculus@9
  2354
        return;
icculus@9
  2355
    }
icculus@9
  2356
icculus@9
  2357
    SDL_SW_UnlockYUVTexture(overlay->hwdata->texture);
icculus@9
  2358
}
icculus@9
  2359
icculus@35
  2360
DECLSPEC int SDLCALL
icculus@9
  2361
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
icculus@9
  2362
{
icculus@9
  2363
    SDL_Surface *display;
icculus@9
  2364
    SDL_Rect src_rect;
icculus@9
  2365
    SDL_Rect dst_rect;
icculus@9
  2366
    void *pixels;
icculus@9
  2367
icculus@9
  2368
    if (!overlay || !dstrect) {
icculus@34
  2369
        return SDL20_SetError("Passed a NULL overlay or dstrect");
icculus@9
  2370
    }
icculus@9
  2371
icculus@9
  2372
    display = overlay->hwdata->display;
icculus@16
  2373
    if (display != VideoSurface) {
icculus@16
  2374
        overlay->hwdata->display = display = VideoSurface;
icculus@9
  2375
        overlay->hwdata->display_format = SDL_MasksToPixelFormatEnum(
icculus@9
  2376
                                                display->format->BitsPerPixel,
icculus@9
  2377
                                                display->format->Rmask,
icculus@9
  2378
                                                display->format->Gmask,
icculus@9
  2379
                                                display->format->Bmask,
icculus@9
  2380
                                                display->format->Amask);
icculus@9
  2381
    }
icculus@9
  2382
icculus@9
  2383
    src_rect.x = 0;
icculus@9
  2384
    src_rect.y = 0;
icculus@9
  2385
    src_rect.w = overlay->w;
icculus@9
  2386
    src_rect.h = overlay->h;
icculus@9
  2387
icculus@9
  2388
    if (!SDL_IntersectRect(&display->clip_rect, dstrect, &dst_rect)) {
icculus@9
  2389
        return 0;
icculus@9
  2390
    }
icculus@9
  2391
     
icculus@9
  2392
    pixels = (void *)((Uint8 *)display->pixels +
icculus@9
  2393
                        dst_rect.y * display->pitch +
icculus@9
  2394
                        dst_rect.x * display->format->BytesPerPixel);
icculus@9
  2395
icculus@9
  2396
    if (SDL_SW_CopyYUVToRGB(overlay->hwdata->texture, &src_rect,
icculus@9
  2397
                            overlay->hwdata->display_format,
icculus@9
  2398
                            dst_rect.w, dst_rect.h,
icculus@9
  2399
                            pixels, display->pitch) < 0) {
icculus@9
  2400
        return -1;
icculus@9
  2401
    }
icculus@19
  2402
    SDL_UpdateWindowSurface(VideoWindow20);
icculus@9
  2403
    return 0;
icculus@9
  2404
}
icculus@9
  2405
icculus@35
  2406
DECLSPEC void SDLCALL
icculus@9
  2407
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
icculus@9
  2408
{
icculus@9
  2409
    if (!overlay) {
icculus@9
  2410
        return;
icculus@9
  2411
    }
icculus@9
  2412
    if (overlay->hwdata) {
icculus@9
  2413
        if (overlay->hwdata->texture) {
icculus@9
  2414
            SDL_SW_DestroyYUVTexture(overlay->hwdata->texture);
icculus@9
  2415
        }
icculus@31
  2416
        SDL20_free(overlay->hwdata);
icculus@9
  2417
    }
icculus@31
  2418
    SDL20_free(overlay);
icculus@9
  2419
}
icculus@34
  2420
#endif
icculus@9
  2421
icculus@35
  2422
DECLSPEC int SDLCALL
icculus@30
  2423
SDL_GL_SetAttribute(SDL12_GLattr attr, int value)
icculus@30
  2424
{
icculus@30
  2425
    if (attr >= SDL12_GL_MAX_ATTRIBUTE)
icculus@34
  2426
        return SDL20_SetError("Unknown GL attribute");
icculus@30
  2427
icculus@30
  2428
    /* swap control was moved out of this API, everything else lines up. */
icculus@30
  2429
    if (attr == SDL12_GL_SWAP_CONTROL)
icculus@30
  2430
    {
icculus@30
  2431
        SwapInterval = value;
icculus@30
  2432
        return 0;
icculus@30
  2433
    }
icculus@30
  2434
icculus@30
  2435
    return SDL20_GL_SetAttribute((SDL_GLattr) attr, value);
icculus@30
  2436
}
icculus@30
  2437
icculus@35
  2438
DECLSPEC int SDLCALL
icculus@30
  2439
SDL_GL_GetAttribute(SDL12_GLattr attr, int* value)
icculus@30
  2440
{
icculus@30
  2441
    if (attr >= SDL12_GL_MAX_ATTRIBUTE)
icculus@34
  2442
        return SDL20_SetError("Unknown GL attribute");
icculus@30
  2443
icculus@30
  2444
    /* swap control was moved out of this API, everything else lines up. */
icculus@30
  2445
    if (attr == SDL12_GL_SWAP_CONTROL)
icculus@30
  2446
    {
icculus@30
  2447
        *value = SDL20_GL_GetSwapInterval();
icculus@30
  2448
        return 0;
icculus@30
  2449
    }
icculus@30
  2450
icculus@30
  2451
    return SDL20_GL_GetAttribute((SDL_GLattr) attr, value);
icculus@30
  2452
}
icculus@30
  2453
icculus@30
  2454
icculus@35
  2455
DECLSPEC void SDLCALL
icculus@9
  2456
SDL_GL_SwapBuffers(void)
icculus@9
  2457
{
icculus@34
  2458
    if (VideoWindow20)
icculus@34
  2459
        SDL20_GL_SwapWindow(VideoWindow20);
icculus@9
  2460
}
icculus@9
  2461
icculus@35
  2462
DECLSPEC int SDLCALL
icculus@9
  2463
SDL_SetGamma(float red, float green, float blue)
icculus@9
  2464
{
icculus@9
  2465
    Uint16 red_ramp[256];
icculus@9
  2466
    Uint16 green_ramp[256];
icculus@9
  2467
    Uint16 blue_ramp[256];
icculus@9
  2468
icculus@34
  2469
    SDL20_CalculateGammaRamp(red, red_ramp);
icculus@9
  2470
    if (green == red) {
icculus@34
  2471
        SDL20_memcpy(green_ramp, red_ramp, sizeof(red_ramp));
icculus@9
  2472
    } else {
icculus@34
  2473
        SDL20_CalculateGammaRamp(green, green_ramp);
icculus@9
  2474
    }
icculus@9
  2475
    if (blue == red) {
icculus@34
  2476
        SDL20_memcpy(blue_ramp, red_ramp, sizeof(red_ramp));
icculus@34
  2477
    } else if (blue == green) {
icculus@34
  2478
        SDL20_memcpy(blue_ramp, green_ramp, sizeof(green_ramp));
icculus@9
  2479
    } else {
icculus@34
  2480
        SDL20_CalculateGammaRamp(blue, blue_ramp);
icculus@9
  2481
    }
icculus@34
  2482
    return SDL20_SetWindowGammaRamp(VideoWindow20, red_ramp, green_ramp, blue_ramp);
icculus@9
  2483
}
icculus@9
  2484
icculus@35
  2485
DECLSPEC int SDLCALL
icculus@34
  2486
SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue)
icculus@9
  2487
{
icculus@34
  2488
    return SDL20_SetWindowGammaRamp(VideoWindow20, red, green, blue);
icculus@9
  2489
}
icculus@9
  2490
icculus@35
  2491
DECLSPEC int SDLCALL
icculus@34
  2492
SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
icculus@9
  2493
{
icculus@34
  2494
    return SDL20_GetWindowGammaRamp(VideoWindow20, red, green, blue);
icculus@9
  2495
}
icculus@9
  2496
icculus@35
  2497
DECLSPEC int SDLCALL
icculus@9
  2498
SDL_EnableKeyRepeat(int delay, int interval)
icculus@9
  2499
{
icculus@9
  2500
    return 0;
icculus@9
  2501
}
icculus@9
  2502
icculus@35
  2503
DECLSPEC void SDLCALL
icculus@9
  2504
SDL_GetKeyRepeat(int *delay, int *interval)
icculus@9
  2505
{
icculus@9
  2506
    if (delay) {
icculus@34
  2507
        *delay = SDL12_DEFAULT_REPEAT_DELAY;
icculus@9
  2508
    }
icculus@9
  2509
    if (interval) {
icculus@34
  2510
        *interval = SDL12_DEFAULT_REPEAT_INTERVAL;
icculus@9
  2511
    }
icculus@9
  2512
}
icculus@9
  2513
icculus@34
  2514
#if SANITY_CHECK_THIS_CODE
icculus@35
  2515
DECLSPEC int SDLCALL
icculus@9
  2516
SDL_EnableUNICODE(int enable)
icculus@9
  2517
{
icculus@16
  2518
    int previous = EnabledUnicode;
icculus@9
  2519
icculus@9
  2520
    switch (enable) {
icculus@9
  2521
    case 1:
icculus@16
  2522
        EnabledUnicode = 1;
icculus@34
  2523
        SDL20_StartTextInput();
icculus@9
  2524
        break;
icculus@9
  2525
    case 0:
icculus@16
  2526
        EnabledUnicode = 0;
icculus@34
  2527
        SDL20_StopTextInput();
icculus@9
  2528
        break;
icculus@9
  2529
    }
icculus@9
  2530
    return previous;
icculus@9
  2531
}
icculus@34
  2532
#endif
icculus@9
  2533
icculus@9
  2534
static Uint32
icculus@34
  2535
SetTimerOld_Callback(Uint32 interval, void* param)
icculus@9
  2536
{
icculus@31
  2537
    return ((SDL12_TimerCallback)param)(interval);
icculus@9
  2538
}
icculus@9
  2539
icculus@35
  2540
DECLSPEC int SDLCALL
icculus@31
  2541
SDL_SetTimer(Uint32 interval, SDL12_TimerCallback callback)
icculus@9
  2542
{
icculus@9
  2543
    static SDL_TimerID compat_timer;
icculus@9
  2544
icculus@9
  2545
    if (compat_timer) {
icculus@10
  2546
        SDL20_RemoveTimer(compat_timer);
icculus@9
  2547
        compat_timer = 0;
icculus@9
  2548
    }
icculus@9
  2549
icculus@9
  2550
    if (interval && callback) {
icculus@34
  2551
        compat_timer = SDL20_AddTimer(interval, SetTimerOld_Callback, callback);
icculus@9
  2552
        if (!compat_timer) {
icculus@9
  2553
            return -1;
icculus@9
  2554
        }
icculus@9
  2555
    }
icculus@9
  2556
    return 0;
icculus@9
  2557
}
icculus@9
  2558
icculus@35
  2559
DECLSPEC int SDLCALL
icculus@9
  2560
SDL_putenv(const char *_var)
icculus@9
  2561
{
icculus@9
  2562
    char *ptr = NULL;
icculus@34
  2563
    char *var = SDL20_strdup(_var);
icculus@9
  2564
    if (var == NULL) {
icculus@9
  2565
        return -1;  /* we don't set errno. */
icculus@9
  2566
    }
icculus@9
  2567
icculus@34
  2568
    ptr = SDL20_strchr(var, '=');
icculus@9
  2569
    if (ptr == NULL) {
icculus@31
  2570
        SDL20_free(var);
icculus@9
  2571
        return -1;
icculus@9
  2572
    }
icculus@9
  2573
icculus@9
  2574
    *ptr = '\0';  /* split the string into name and value. */
icculus@34
  2575
    SDL20_setenv(var, ptr + 1, 1);
icculus@31
  2576
    SDL20_free(var);
icculus@9
  2577
    return 0;
icculus@9
  2578
}
icculus@9
  2579
icculus@9
  2580
icculus@9
  2581
icculus@9
  2582
/* CD-ROM support is gone from SDL 2.0, so just have stubs that fail. */
icculus@9
  2583
icculus@9
  2584
typedef void *SDL12_CD;  /* close enough.  :) */
icculus@9
  2585
typedef int SDL12_CDstatus;  /* close enough.  :) */
icculus@9
  2586
icculus@35
  2587
DECLSPEC int SDLCALL
icculus@9
  2588
SDL_CDNumDrives(void)
icculus@9
  2589
{
icculus@9
  2590
    return 0;  /* !!! FIXME: should return -1 without SDL_INIT_CDROM */
icculus@9
  2591
}
icculus@9
  2592
icculus@35
  2593
DECLSPEC const char *SDLCALL SDL_CDName(int drive) { SDL20_Unsupported(); return NULL; }
icculus@35
  2594
DECLSPEC SDL12_CD *SDLCALL SDL_CDOpen(int drive) { SDL20_Unsupported(); return NULL; }
icculus@35
  2595
DECLSPEC SDL12_CDstatus SDLCALL SDL_CDStatus(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2596
DECLSPEC int SDLCALL SDL_CDPlayTracks(SDL12_CD *cdrom, int start_track, int start_frame, int ntracks, int nframes) { return SDL20_Unsupported(); }
icculus@35
  2597
DECLSPEC int SDLCALL SDL_CDPlay(SDL12_CD *cdrom, int start, int length) { return SDL20_Unsupported(); }
icculus@35
  2598
DECLSPEC int SDLCALL SDL_CDPause(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2599
DECLSPEC int SDLCALL SDL_CDResume(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2600
DECLSPEC int SDLCALL SDL_CDStop(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2601
DECLSPEC int SDLCALL SDL_CDEject(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2602
DECLSPEC void SDLCALL SDL_CDClose(SDL12_CD *cdrom) {}
icculus@9
  2603
icculus@26
  2604
icculus@26
  2605
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
icculus@35
  2606
DECLSPEC SDL_Thread * SDLCALL
icculus@26
  2607
SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
icculus@26
  2608
{
icculus@26
  2609
    return SDL20_CreateThread(fn, NULL, data, pfnBeginThread, pfnEndThread);
icculus@26
  2610
}
icculus@26
  2611
#else
icculus@35
  2612
DECLSPEC SDL_Thread * SDLCALL
icculus@26
  2613
SDL_CreateThread(int (SDLCALL *fn)(void *), void *data)
icculus@26
  2614
{
icculus@26
  2615
    return SDL20_CreateThread(fn, NULL, data);
icculus@26
  2616
}
icculus@26
  2617
#endif
icculus@26
  2618
icculus@26
  2619
icculus@9
  2620
/* !!! FIXME: Removed from 2.0; do nothing. We can't even report failure. */
icculus@35
  2621
DECLSPEC void SDLCALL SDL_KillThread(SDL_Thread *thread) {}
icculus@9
  2622
icculus@10
  2623
/* This changed from an opaque pointer to an int in 2.0. */
icculus@10
  2624
typedef struct _SDL12_TimerID *SDL12_TimerID;
icculus@10
  2625
SDL_COMPILE_TIME_ASSERT(timer, sizeof(SDL12_TimerID) >= sizeof(SDL_TimerID));
icculus@10
  2626
icculus@31
  2627
icculus@35
  2628
DECLSPEC SDL12_TimerID SDLCALL
icculus@31
  2629
SDL_AddTimer(Uint32 interval, SDL12_NewTimerCallback callback, void *param)
icculus@10
  2630
{
icculus@10
  2631
    return (SDL12_TimerID) ((size_t) SDL20_AddTimer(interval, callback, param));
icculus@10
  2632
}
icculus@10
  2633
icculus@35
  2634
DECLSPEC SDL_bool SDLCALL
icculus@10
  2635
SDL_RemoveTimer(SDL12_TimerID id)
icculus@10
  2636
{
icculus@10
  2637
    return SDL20_RemoveTimer((SDL_TimerID) ((size_t)id));
icculus@10
  2638
}
icculus@10
  2639
icculus@10
  2640
icculus@10
  2641
typedef struct SDL12_RWops {
icculus@31
  2642
    int (SDLCALL *seek)(struct SDL12_RWops *context, int offset, int whence);
icculus@31
  2643
    int (SDLCALL *read)(struct SDL12_RWops *context, void *ptr, int size, int maxnum);
icculus@31
  2644
    int (SDLCALL *write)(struct SDL12_RWops *context, const void *ptr, int size, int num);
icculus@31
  2645
    int (SDLCALL *close)(struct SDL12_RWops *context);
icculus@10
  2646
    Uint32 type;
icculus@10
  2647
    void *padding[8];
icculus@10
  2648
    SDL_RWops *rwops20;
icculus@10
  2649
} SDL12_RWops;
icculus@10
  2650
icculus@10
  2651
icculus@35
  2652
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2653
SDL_AllocRW(void)
icculus@10
  2654
{
icculus@31
  2655
    SDL12_RWops *rwops = (SDL12_RWops *) SDL20_malloc(sizeof (SDL12_RWops));
icculus@10
  2656
    if (!rwops)
icculus@10
  2657
        SDL20_OutOfMemory();
icculus@10
  2658
    return rwops;
icculus@10
  2659
}
icculus@10
  2660
icculus@35
  2661
DECLSPEC void SDLCALL
icculus@10
  2662
SDL_FreeRW(SDL12_RWops *rwops12)
icculus@10
  2663
{
icculus@31
  2664
    SDL20_free(rwops12);
icculus@10
  2665
}
icculus@10
  2666
icculus@10
  2667
static int SDLCALL
icculus@31
  2668
RWops20to12_seek(struct SDL12_RWops *rwops12, int offset, int whence)
icculus@10
  2669
{
icculus@10
  2670
    return rwops12->rwops20->seek(rwops12->rwops20, offset, whence);
icculus@10
  2671
}
icculus@10
  2672
icculus@10
  2673
static int SDLCALL
icculus@31
  2674
RWops20to12_read(struct SDL12_RWops *rwops12, void *ptr, int size, int maxnum)
icculus@10
  2675
{
icculus@10
  2676
    return rwops12->rwops20->read(rwops12->rwops20, ptr, size, maxnum);
icculus@10
  2677
}
icculus@10
  2678
icculus@10
  2679
static int SDLCALL
icculus@31
  2680
RWops20to12_write(struct SDL12_RWops *rwops12, const void *ptr, int size, int num)
icculus@10
  2681
{
icculus@10
  2682
    return rwops12->rwops20->write(rwops12->rwops20, ptr, size, num);
icculus@10
  2683
}
icculus@10
  2684
icculus@10
  2685
static int SDLCALL
icculus@31
  2686
RWops20to12_close(struct SDL12_RWops *rwops12)
icculus@10
  2687
{
icculus@10
  2688
    int rc = 0;
icculus@10
  2689
    if (rwops12)
icculus@10
  2690
    {
icculus@10
  2691
        rc = rwops12->rwops20->close(rwops12->rwops20);
icculus@10
  2692
        if (rc == 0)
icculus@10
  2693
            SDL_FreeRW(rwops12);
icculus@10
  2694
    }
icculus@10
  2695
    return rc;
icculus@10
  2696
}
icculus@10
  2697
icculus@10
  2698
static SDL12_RWops *
icculus@31
  2699
RWops20to12(SDL_RWops *rwops20)
icculus@10
  2700
{
icculus@31
  2701
    SDL12_RWops *rwops12;
icculus@31
  2702
icculus@10
  2703
    if (!rwops20)
icculus@10
  2704
        return NULL;
icculus@31
  2705
icculus@31
  2706
    rwops12 = SDL_AllocRW();
icculus@31
  2707
    if (!rwops12)
icculus@31
  2708
        return NULL;
icculus@31
  2709
icculus@34
  2710
    SDL20_zerop(rwops12);
icculus@10
  2711
    rwops12->type = rwops20->type;
icculus@10
  2712
    rwops12->rwops20 = rwops20;
icculus@31
  2713
    rwops12->seek = RWops20to12_seek;
icculus@31
  2714
    rwops12->read = RWops20to12_read;
icculus@31
  2715
    rwops12->write = RWops20to12_write;
icculus@31
  2716
    rwops12->close = RWops20to12_close;
icculus@31
  2717
icculus@12
  2718
    return rwops12;
icculus@10
  2719
}
icculus@10
  2720
icculus@35
  2721
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2722
SDL_RWFromFile(const char *file, const char *mode)
icculus@10
  2723
{
icculus@31
  2724
    return RWops20to12(SDL20_RWFromFile(file, mode));
icculus@10
  2725
}
icculus@10
  2726
icculus@35
  2727
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2728
SDL_RWFromFP(FILE *io, int autoclose)
icculus@10
  2729
{
icculus@31
  2730
    return RWops20to12(SDL20_RWFromFP(io, autoclose));
icculus@10
  2731
}
icculus@10
  2732
icculus@35
  2733
DE