src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 26 Jul 2014 00:54:42 -0400
changeset 39 354ef2d7f999
parent 38 3f4404bfd179
child 40 9f165553c592
permissions -rw-r--r--
Reworked SDL_ListModes() to be API correct.
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@9
   608
    Uint32 sdl20flags = 0;
icculus@9
   609
    int rc;
icculus@9
   610
icculus@9
   611
    if (!LoadSDL20())
icculus@9
   612
        return -1;
icculus@9
   613
icculus@19
   614
    #define SETFLAG(flag) if (sdl12flags & SDL12_INIT_##flag) sdl20flags |= SDL_INIT_##flag
icculus@9
   615
    SETFLAG(TIMER);
icculus@9
   616
    SETFLAG(AUDIO);
icculus@9
   617
    SETFLAG(VIDEO);
icculus@9
   618
    SETFLAG(JOYSTICK);
icculus@9
   619
    SETFLAG(NOPARACHUTE);
icculus@11
   620
    #undef SETFLAG
icculus@11
   621
icculus@11
   622
    // There's no CDROM in 2.0, but we'll just pretend it succeeded.
icculus@11
   623
    if (sdl12flags & SDL12_INIT_CDROM)
icculus@16
   624
        CDRomInit = 1;
icculus@11
   625
icculus@11
   626
    // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
icculus@11
   627
icculus@37
   628
    rc = SDL20_Init(sdl20flags);
icculus@39
   629
    if ((rc == 0) && (sdl20flags & SDL_INIT_VIDEO)) {
icculus@39
   630
        if (Init12Video() == -1) {
icculus@39
   631
            return -1;  /* !!! FIXME: should we deinit other subsystems? */
icculus@39
   632
        }
icculus@18
   633
    }
icculus@18
   634
icculus@11
   635
    return rc;
icculus@11
   636
}
icculus@11
   637
icculus@35
   638
DECLSPEC int SDLCALL
icculus@11
   639
SDL_Init(Uint32 sdl12flags)
icculus@11
   640
{
icculus@37
   641
    return SDL_InitSubSubsystem(sdl12flags);   /* there's no difference betwee Init and InitSubSystem in SDL2. */
icculus@11
   642
}
icculus@11
   643
icculus@38
   644
icculus@38
   645
static void
icculus@38
   646
InitFlags12To20(const Uint32 flags12, Uint32 *_flags20, Uint32 *_extraflags)
icculus@11
   647
{
icculus@38
   648
    Uint32 flags20 = 0;
icculus@11
   649
    Uint32 extraflags = 0;
icculus@11
   650
icculus@38
   651
    #define SETFLAG(flag) if (flags12 & SDL12_INIT_##flag) flags20 |= SDL_INIT_##flag
icculus@11
   652
    SETFLAG(TIMER);
icculus@11
   653
    SETFLAG(AUDIO);
icculus@11
   654
    SETFLAG(VIDEO);
icculus@11
   655
    SETFLAG(JOYSTICK);
icculus@11
   656
    SETFLAG(NOPARACHUTE);
icculus@11
   657
    #undef SETFLAG
icculus@11
   658
icculus@38
   659
    if ((flags12 & SDL12_INIT_CDROM) && (CDRomInit)) {
icculus@11
   660
        extraflags |= SDL12_INIT_CDROM;
icculus@38
   661
    }
icculus@11
   662
icculus@11
   663
    // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
icculus@11
   664
icculus@38
   665
    *_flags20 = flags20;
icculus@38
   666
    *_extraflags = extraflags;
icculus@11
   667
}
icculus@11
   668
icculus@38
   669
static Uint32
icculus@38
   670
InitFlags20to12(const Uint32 flags20)
icculus@11
   671
{
icculus@38
   672
    Uint32 flags12 = 0;
icculus@38
   673
icculus@38
   674
    #define SETFLAG(flag) if (flags20 & SDL_INIT_##flag) flags12 |= SDL12_INIT_##flag
icculus@11
   675
    SETFLAG(TIMER);
icculus@11
   676
    SETFLAG(AUDIO);
icculus@11
   677
    SETFLAG(VIDEO);
icculus@11
   678
    SETFLAG(JOYSTICK);
icculus@11
   679
    SETFLAG(NOPARACHUTE);
icculus@9
   680
    #undef SETFLAG
icculus@9
   681
icculus@38
   682
    return flags12;
icculus@38
   683
}
icculus@38
   684
icculus@38
   685
icculus@38
   686
DECLSPEC Uint32 SDLCALL
icculus@38
   687
SDL_WasInit(Uint32 sdl12flags)
icculus@38
   688
{
icculus@38
   689
    Uint32 sdl20flags, extraflags;
icculus@38
   690
    InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
icculus@38
   691
icculus@38
   692
    return InitFlags20to12(SDL20_WasInit(sdl20flags)) | extraflags;
icculus@38
   693
}
icculus@38
   694
icculus@39
   695
static void
icculus@39
   696
Quit12Video(void)
icculus@39
   697
{
icculus@39
   698
    int i;
icculus@39
   699
icculus@39
   700
    for (i = 0; i < VideoModesCount; i++) {
icculus@39
   701
        SDL20_free(VideoModes[i].modeslist);
icculus@39
   702
        SDL20_free(VideoModes[i].modes);
icculus@39
   703
    }
icculus@39
   704
    SDL20_free(VideoModes);
icculus@39
   705
icculus@39
   706
    SDL20_FreeFormat(VideoInfo.vfmt);
icculus@39
   707
    SDL20_zero(VideoInfo);
icculus@39
   708
icculus@39
   709
    EventFilter12 = NULL;
icculus@39
   710
    EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
icculus@39
   711
    CurrentCursor = NULL;
icculus@39
   712
    VideoModes = NULL;
icculus@39
   713
    VideoModesCount = 0;
icculus@39
   714
}
icculus@39
   715
icculus@38
   716
DECLSPEC void SDLCALL
icculus@38
   717
SDL_QuitSubSystem(Uint32 sdl12flags)
icculus@38
   718
{
icculus@38
   719
    Uint32 sdl20flags, extraflags;
icculus@38
   720
    InitFlags12To20(sdl12flags, &sdl20flags, &extraflags);
icculus@38
   721
icculus@38
   722
    if (extraflags & SDL12_INIT_CDROM) {
icculus@16
   723
        CDRomInit = 0;
icculus@38
   724
    }
icculus@16
   725
icculus@17
   726
    // !!! FIXME: reset a bunch of other global variables too.
icculus@17
   727
    if (sdl12flags & SDL12_INIT_VIDEO) {
icculus@39
   728
        Quit12Video();
icculus@17
   729
    }
icculus@17
   730
icculus@9
   731
    // !!! FIXME: do something about SDL12_INIT_EVENTTHREAD
icculus@11
   732
    SDL20_QuitSubSystem(sdl20flags);
icculus@9
   733
icculus@11
   734
    // !!! FIXME: UnloadSDL20() ?
icculus@9
   735
}
icculus@9
   736
icculus@35
   737
DECLSPEC void SDLCALL
icculus@9
   738
SDL_Quit(void)
icculus@9
   739
{
icculus@17
   740
    // !!! FIXME: reset a bunch of other global variables too.
icculus@18
   741
    EventFilter12 = NULL;
icculus@20
   742
    EventQueueAvailable = EventQueueHead = EventQueueTail = NULL;
icculus@23
   743
    CurrentCursor = NULL;
icculus@17
   744
    SDL20_FreeFormat(VideoInfo.vfmt);
icculus@34
   745
    SDL20_zero(VideoInfo);
icculus@16
   746
    CDRomInit = 0;
icculus@9
   747
    SDL20_Quit();
icculus@9
   748
    UnloadSDL20();
icculus@9
   749
}
icculus@9
   750
icculus@35
   751
DECLSPEC void SDLCALL
icculus@11
   752
SDL_SetError(const char *fmt, ...)
icculus@11
   753
{
icculus@34
   754
    char ch;
icculus@12
   755
    char *str = NULL;
icculus@34
   756
    size_t len = 0;
icculus@12
   757
    va_list ap;
icculus@12
   758
    va_start(ap, fmt);
icculus@34
   759
    len = SDL20_vsnprintf(&ch, 1, fmt, ap);
icculus@12
   760
    va_end(ap);
icculus@34
   761
icculus@34
   762
    str = (char *) SDL20_malloc(len + 1);
icculus@12
   763
    if (!str)
icculus@12
   764
        SDL20_OutOfMemory();
icculus@12
   765
    else
icculus@12
   766
    {
icculus@34
   767
        va_start(ap, fmt);
icculus@34
   768
        SDL20_vsnprintf(str, len + 1, fmt, ap);
icculus@34
   769
        va_end(ap);
icculus@12
   770
        SDL20_SetError("%s", str);
icculus@34
   771
        SDL20_free(str);
icculus@12
   772
    }
icculus@11
   773
}
icculus@9
   774
icculus@35
   775
DECLSPEC const char * SDLCALL
icculus@9
   776
SDL_GetError(void)
icculus@9
   777
{
icculus@9
   778
    if (!Loaded_SDL20)
icculus@9
   779
    {
icculus@19
   780
        static const char noload_errstr[] = "Failed to load SDL 2.0 shared library";
icculus@9
   781
        return noload_errstr;
icculus@9
   782
    }
icculus@9
   783
    return SDL20_GetError();
icculus@9
   784
}
icculus@9
   785
icculus@9
   786
icculus@9
   787
static const char *
icculus@9
   788
GetDriverName(const char *name, char *namebuf, int maxlen)
icculus@9
   789
{
icculus@9
   790
    if (name) {
icculus@9
   791
        if (namebuf) {
icculus@9
   792
            SDL20_strlcpy(namebuf, name, maxlen);
icculus@9
   793
            return namebuf;
icculus@9
   794
        } else {
icculus@9
   795
            return name;
icculus@9
   796
        }
icculus@9
   797
    }
icculus@9
   798
    return NULL;
icculus@9
   799
}
icculus@9
   800
icculus@35
   801
DECLSPEC const char * SDLCALL
icculus@9
   802
SDL_AudioDriverName(char *namebuf, int maxlen)
icculus@9
   803
{
icculus@9
   804
    return GetDriverName(SDL20_GetCurrentAudioDriver(), namebuf, maxlen);
icculus@9
   805
}
icculus@9
   806
icculus@35
   807
DECLSPEC const char * SDLCALL
icculus@9
   808
SDL_VideoDriverName(char *namebuf, int maxlen)
icculus@9
   809
{
icculus@9
   810
    return GetDriverName(SDL20_GetCurrentVideoDriver(), namebuf, maxlen);
icculus@9
   811
}
icculus@9
   812
icculus@35
   813
DECLSPEC int SDLCALL
icculus@31
   814
SDL_PollEvent(SDL12_Event *event12)
icculus@31
   815
{
icculus@31
   816
    EventQueueType *next;
icculus@31
   817
icculus@31
   818
    SDL20_PumpEvents();  /* this will run our filter and build our 1.2 queue. */
icculus@31
   819
icculus@31
   820
    if (EventQueueHead == NULL)
icculus@31
   821
        return 0;  /* no events at the moment. */
icculus@31
   822
icculus@31
   823
    SDL_memcpy(event12, &EventQueueHead->event12, sizeof (SDL12_Event));
icculus@31
   824
    next = EventQueueHead->next;
icculus@31
   825
    EventQueueHead->next = EventQueueAvailable;
icculus@31
   826
    EventQueueAvailable = EventQueueHead;
icculus@31
   827
    EventQueueHead = next;
icculus@31
   828
    return 1;
icculus@31
   829
}
icculus@31
   830
icculus@35
   831
DECLSPEC int SDLCALL
icculus@31
   832
SDL_PushEvent(SDL12_Event *event12)
icculus@31
   833
{
icculus@31
   834
    EventQueueType *item = EventQueueAvailable;
icculus@31
   835
    if (item == NULL)
icculus@31
   836
        return -1;  /* no space available at the moment. */
icculus@31
   837
icculus@31
   838
    EventQueueAvailable = item->next;
icculus@31
   839
    if (EventQueueTail)
icculus@31
   840
        EventQueueTail->next = item;
icculus@31
   841
    else
icculus@31
   842
        EventQueueHead = EventQueueTail = item;
icculus@31
   843
    item->next = NULL;
icculus@31
   844
icculus@31
   845
    SDL_memcpy(&item->event12, event12, sizeof (SDL12_Event));
icculus@31
   846
    return 0;
icculus@31
   847
}
icculus@31
   848
icculus@35
   849
DECLSPEC int SDLCALL
icculus@31
   850
SDL_PeepEvents(SDL12_Event *events12, int numevents, SDL_eventaction action, Uint32 mask)
icculus@31
   851
{
icculus@31
   852
    if (action == SDL_ADDEVENT)
icculus@31
   853
    {
icculus@31
   854
        int i;
icculus@31
   855
        for (i = 0; i < numevents; i++)
icculus@31
   856
        {
icculus@31
   857
            if (SDL_PushEvent(&events12[i]) == -1)
icculus@31
   858
                break;  /* out of space for more events. */
icculus@31
   859
        }
icculus@31
   860
        return i;
icculus@31
   861
    }
icculus@31
   862
    else if ((action == SDL_PEEKEVENT) || (action == SDL_GETEVENT))
icculus@31
   863
    {
icculus@31
   864
        const SDL_bool isGet = (action == SDL_GETEVENT);
icculus@31
   865
        EventQueueType *prev = NULL;
icculus@31
   866
        EventQueueType *item = EventQueueHead;
icculus@31
   867
        EventQueueType *next = NULL;
icculus@31
   868
        int chosen = 0;
icculus@31
   869
        while (chosen < numevents)
icculus@31
   870
        {
icculus@31
   871
            EventQueueType *nextPrev = item;
icculus@31
   872
            if (!item)
icculus@31
   873
                break;  /* no more events at the moment. */
icculus@31
   874
icculus@31
   875
            next = item->next;  /* copy, since we might overwrite item->next */
icculus@31
   876
icculus@31
   877
            if (mask & (1<<item->event12.type))
icculus@31
   878
            {
icculus@31
   879
                SDL_memcpy(&events12[chosen++], &item->event12, sizeof (SDL12_Event));
icculus@31
   880
                if (isGet)  /* remove from list? */
icculus@31
   881
                {
icculus@31
   882
                    if (prev != NULL)
icculus@31
   883
                        prev->next = next;
icculus@31
   884
                    if (item == EventQueueHead)
icculus@31
   885
                        EventQueueHead = next;
icculus@31
   886
                    if (item == EventQueueTail)
icculus@31
   887
                        EventQueueTail = prev;
icculus@31
   888
icculus@31
   889
                    /* put it back in the free pool. */
icculus@31
   890
                    item->next = EventQueueAvailable;
icculus@31
   891
                    EventQueueAvailable = item;
icculus@31
   892
                    nextPrev = prev;  /* previous item doesn't change. */
icculus@31
   893
                }
icculus@31
   894
            }
icculus@31
   895
icculus@31
   896
            item = next;
icculus@31
   897
            prev = nextPrev;
icculus@31
   898
        }
icculus@31
   899
        return chosen;
icculus@31
   900
    }
icculus@31
   901
icculus@31
   902
    return 0;
icculus@31
   903
}
icculus@31
   904
icculus@35
   905
DECLSPEC int SDLCALL
icculus@31
   906
SDL_WaitEvent(SDL12_Event *event12)
icculus@31
   907
{
icculus@31
   908
    /* In 1.2, this only fails (-1) if you haven't SDL_Init()'d. */
icculus@31
   909
    while (!SDL_PollEvent(event12))
icculus@31
   910
        SDL_Delay(10);
icculus@31
   911
    return 1;
icculus@31
   912
}
icculus@31
   913
icculus@32
   914
static SDL_bool
icculus@32
   915
PushEventIfNotFiltered(SDL12_Event *event12)
icculus@32
   916
{
icculus@32
   917
    if (event12->type != SDL12_NOEVENT)
icculus@32
   918
    {
icculus@32
   919
        if (EventStates[event12->type] != SDL_IGNORE)
icculus@32
   920
        {
icculus@32
   921
            if ((!EventFilter12) || (EventFilter12(event12)))
icculus@32
   922
                return (SDL_PushEvent(event12) == 0);
icculus@32
   923
        }
icculus@32
   924
    }
icculus@32
   925
    return SDL_FALSE;
icculus@32
   926
}
icculus@32
   927
icculus@35
   928
DECLSPEC Uint8 SDLCALL
icculus@31
   929
SDL_EventState(Uint8 type, int state)
icculus@31
   930
{
icculus@31
   931
    /* the values of "state" match between 1.2 and 2.0 */
icculus@31
   932
    const Uint8 retval = EventStates[type];
icculus@31
   933
    SDL12_Event e;
icculus@31
   934
icculus@31
   935
    if (state != SDL_QUERY)
icculus@31
   936
        EventStates[type] = state;
icculus@31
   937
    if (state == SDL_IGNORE)  /* drop existing events of this type. */
icculus@31
   938
        while (SDL_PeepEvents(&e, 1, SDL_GETEVENT, (1<<type))) {}
icculus@31
   939
icculus@31
   940
    return retval;
icculus@31
   941
}
icculus@31
   942
icculus@35
   943
DECLSPEC Uint8 SDLCALL
icculus@35
   944
SDL_GetMouseState(int *x, int *y)
icculus@35
   945
{
icculus@35
   946
    const Uint32 state20 = SDL20_GetMouseState(x, y);
icculus@35
   947
    Uint8 retval = (state20 & 0x7);  /* left, right, and middle will match. */
icculus@35
   948
icculus@35
   949
    /* the X[12] buttons are different in 1.2; mousewheel was in the way. */
icculus@35
   950
    if (state20 & SDL_BUTTON(SDL_BUTTON_X1))
icculus@35
   951
        retval |= (1<<5);
icculus@35
   952
    if (state20 & SDL_BUTTON(SDL_BUTTON_X2))
icculus@35
   953
        retval |= (1<<6);
icculus@35
   954
icculus@35
   955
    return retval;
icculus@35
   956
}
icculus@35
   957
icculus@18
   958
static int
icculus@18
   959
EventFilter20to12(void *data, SDL_Event *event20)
icculus@15
   960
{
icculus@18
   961
    const int maxUserEvents12 = SDL12_NUMEVENTS - SDL12_USEREVENT;
icculus@18
   962
    SDL12_Event event12;
icculus@32
   963
    int x, y;
icculus@18
   964
icculus@18
   965
    SDL_assert(data == NULL);  /* currently unused. */
icculus@18
   966
icculus@34
   967
    SDL20_zero(event12);
icculus@18
   968
icculus@18
   969
    switch (event20->type)
icculus@18
   970
    {
icculus@18
   971
        case SDL_QUIT:
icculus@31
   972
            event12.type = SDL12_QUIT;
icculus@18
   973
            break;
icculus@18
   974
icculus@18
   975
        case SDL_WINDOWEVENT:
icculus@18
   976
            switch (event20->window.event)
icculus@18
   977
            {
icculus@18
   978
                case SDL_WINDOWEVENT_CLOSE:
icculus@31
   979
                    event12.type = SDL12_QUIT;
icculus@18
   980
                    break;
icculus@18
   981
icculus@18
   982
                case SDL_WINDOWEVENT_SHOWN:
icculus@18
   983
                case SDL_WINDOWEVENT_EXPOSED:
icculus@31
   984
                    event12.type = SDL12_VIDEOEXPOSE;
icculus@18
   985
                    break;
icculus@18
   986
icculus@18
   987
                case SDL_WINDOWEVENT_RESIZED:
icculus@18
   988
                case SDL_WINDOWEVENT_SIZE_CHANGED:  // !!! FIXME: what's the difference between RESIZED and SIZE_CHANGED?
icculus@31
   989
                    event12.type = SDL12_VIDEORESIZE;
icculus@31
   990
                    event12.resize.w = event20->window.data1;
icculus@31
   991
                    event12.resize.h = event20->window.data2;
icculus@18
   992
                    break;
icculus@18
   993
icculus@18
   994
                case SDL_WINDOWEVENT_MINIMIZED:
icculus@31
   995
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
   996
                    event12.active.gain = 0;
icculus@31
   997
                    event12.active.state = SDL12_APPACTIVE;
icculus@18
   998
                    break;
icculus@18
   999
icculus@18
  1000
                case SDL_WINDOWEVENT_RESTORED:
icculus@31
  1001
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1002
                    event12.active.gain = 1;
icculus@31
  1003
                    event12.active.state = SDL12_APPACTIVE;
icculus@18
  1004
                    break;
icculus@18
  1005
icculus@18
  1006
                case SDL_WINDOWEVENT_ENTER:
icculus@31
  1007
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1008
                    event12.active.gain = 1;
icculus@31
  1009
                    event12.active.state = SDL12_APPMOUSEFOCUS;
icculus@18
  1010
                    break;
icculus@18
  1011
icculus@18
  1012
                case SDL_WINDOWEVENT_LEAVE:
icculus@31
  1013
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1014
                    event12.active.gain = 0;
icculus@31
  1015
                    event12.active.state = SDL12_APPMOUSEFOCUS;
icculus@18
  1016
                    break;
icculus@18
  1017
icculus@18
  1018
                case SDL_WINDOWEVENT_FOCUS_GAINED:
icculus@31
  1019
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1020
                    event12.active.gain = 1;
icculus@31
  1021
                    event12.active.state = SDL12_APPINPUTFOCUS;
icculus@18
  1022
                    break;
icculus@18
  1023
icculus@18
  1024
                case SDL_WINDOWEVENT_FOCUS_LOST:
icculus@31
  1025
                    event12.type = SDL12_ACTIVEEVENT;
icculus@31
  1026
                    event12.active.gain = 0;
icculus@31
  1027
                    event12.active.state = SDL12_APPINPUTFOCUS;
icculus@18
  1028
                    break;
icculus@18
  1029
            }
icculus@18
  1030
            break;
icculus@18
  1031
icculus@18
  1032
        // !!! FIXME: this is sort of a mess to convert.
icculus@18
  1033
        //case SDL_SYSWMEVENT:
icculus@18
  1034
icculus@18
  1035
        // !!! FIXME: write me
icculus@18
  1036
        case SDL_KEYDOWN:
icculus@18
  1037
        case SDL_KEYUP:
icculus@18
  1038
            return 0;
icculus@18
  1039
icculus@18
  1040
        // !!! FIXME: write me
icculus@18
  1041
        case SDL_TEXTEDITING:
icculus@18
  1042
        case SDL_TEXTINPUT:
icculus@18
  1043
            return 0;
icculus@18
  1044
icculus@18
  1045
        case SDL_MOUSEMOTION:
icculus@31
  1046
        	event12.type = SDL12_MOUSEMOTION;
icculus@31
  1047
            event12.motion.which = (Uint8) event20->motion.which;
icculus@31
  1048
            event12.motion.state = event20->motion.state;
icculus@31
  1049
            event12.motion.x = (Uint16) event20->motion.x;
icculus@31
  1050
            event12.motion.y = (Uint16) event20->motion.y;
icculus@31
  1051
            event12.motion.xrel = (Sint16) event20->motion.xrel;
icculus@31
  1052
            event12.motion.yrel = (Sint16) event20->motion.yrel;
icculus@18
  1053
            break;
icculus@18
  1054
icculus@18
  1055
        case SDL_MOUSEBUTTONDOWN:
icculus@31
  1056
        	event12.type = SDL12_MOUSEBUTTONDOWN;
icculus@31
  1057
            event12.button.which = (Uint8) event20->button.which;
icculus@31
  1058
            event12.button.button = event20->button.button;
icculus@31
  1059
            event12.button.state = event20->button.state;
icculus@31
  1060
            event12.button.x = (Uint16) event20->button.x;
icculus@31
  1061
            event12.button.y = (Uint16) event20->button.y;
icculus@18
  1062
            break;
icculus@18
  1063
icculus@18
  1064
        case SDL_MOUSEBUTTONUP:
icculus@31
  1065
        	event12.type = SDL12_MOUSEBUTTONUP;
icculus@31
  1066
            event12.button.which = (Uint8) event20->button.which;
icculus@31
  1067
            event12.button.button = event20->button.button;
icculus@31
  1068
            event12.button.state = event20->button.state;
icculus@31
  1069
            event12.button.x = (Uint16) event20->button.x;
icculus@31
  1070
            event12.button.y = (Uint16) event20->button.y;
icculus@18
  1071
            break;
icculus@18
  1072
icculus@18
  1073
        case SDL_MOUSEWHEEL:
icculus@32
  1074
            if (event20->wheel.y == 0)
icculus@32
  1075
                break;  /* don't support horizontal wheels in 1.2. */
icculus@32
  1076
icculus@32
  1077
            event12.type = SDL12_MOUSEBUTTONDOWN;
icculus@32
  1078
            event12.button.which = (Uint8) event20->wheel.which;
icculus@32
  1079
            event12.button.button = (event20->wheel.y > 0) ? 4 : 5;  /* wheelup is 4, down is 5. */
icculus@32
  1080
            event12.button.state = SDL_GetMouseState(&x, &y);
icculus@32
  1081
            event12.button.x = (Uint16) x;
icculus@32
  1082
            event12.button.y = (Uint16) y;
icculus@32
  1083
            PushEventIfNotFiltered(&event12);
icculus@32
  1084
icculus@32
  1085
            event12.type = SDL12_MOUSEBUTTONUP;  /* immediately release mouse "button" at the end of this switch. */
icculus@18
  1086
            break;
icculus@18
  1087
icculus@18
  1088
        case SDL_JOYAXISMOTION:
icculus@31
  1089
            event12.type = SDL12_JOYAXISMOTION;
icculus@31
  1090
            event12.jaxis.which = (Uint8) event20->jaxis.which;
icculus@31
  1091
            event12.jaxis.axis = event20->jaxis.axis;
icculus@31
  1092
            event12.jaxis.value = event20->jaxis.value;
icculus@18
  1093
            break;
icculus@18
  1094
icculus@18
  1095
        case SDL_JOYBALLMOTION:
icculus@31
  1096
            event12.type = SDL12_JOYBALLMOTION;
icculus@31
  1097
            event12.jball.which = (Uint8) event20->jball.which;
icculus@31
  1098
            event12.jball.ball = event20->jball.ball;
icculus@31
  1099
            event12.jball.xrel = event20->jball.xrel;
icculus@31
  1100
            event12.jball.yrel = event20->jball.yrel;
icculus@18
  1101
            break;
icculus@18
  1102
icculus@18
  1103
        case SDL_JOYHATMOTION:
icculus@31
  1104
            event12.type = SDL12_JOYHATMOTION;
icculus@31
  1105
            event12.jhat.which = (Uint8) event20->jhat.which;
icculus@31
  1106
            event12.jhat.hat = event20->jhat.hat;
icculus@31
  1107
            event12.jhat.value = event20->jhat.value;
icculus@18
  1108
            break;
icculus@18
  1109
icculus@18
  1110
        case SDL_JOYBUTTONDOWN:
icculus@31
  1111
            event12.type = SDL12_JOYBUTTONDOWN;
icculus@31
  1112
            event12.jbutton.which = (Uint8) event20->jbutton.which;
icculus@31
  1113
            event12.jbutton.button = event20->jbutton.button;
icculus@31
  1114
            event12.jbutton.state = event20->jbutton.state;
icculus@18
  1115
            break;
icculus@18
  1116
icculus@18
  1117
        case SDL_JOYBUTTONUP:
icculus@31
  1118
            event12.type = SDL12_JOYBUTTONUP;
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_JOYDEVICEADDED:
icculus@18
  1125
        //case SDL_JOYDEVICEREMOVED:
icculus@18
  1126
	    //case SDL_CONTROLLERAXISMOTION:
icculus@18
  1127
	    //case SDL_CONTROLLERBUTTONDOWN:
icculus@18
  1128
	    //case SDL_CONTROLLERBUTTONUP:
icculus@18
  1129
	    //case SDL_CONTROLLERDEVICEADDED:
icculus@18
  1130
	    //case SDL_CONTROLLERDEVICEREMOVED:
icculus@18
  1131
	    //case SDL_CONTROLLERDEVICEREMAPPED:
icculus@18
  1132
        //case SDL_FINGERDOWN:
icculus@18
  1133
        //case SDL_FINGERUP:
icculus@18
  1134
        //case SDL_FINGERMOTION:
icculus@18
  1135
        //case SDL_DOLLARGESTURE:
icculus@18
  1136
        //case SDL_DOLLARRECORD:
icculus@18
  1137
        //case SDL_MULTIGESTURE:
icculus@18
  1138
        //case SDL_CLIPBOARDUPDATE:
icculus@18
  1139
        //case SDL_DROPFILE:
icculus@18
  1140
icculus@18
  1141
        default:
icculus@18
  1142
            return 0;  /* drop everything else. */
icculus@18
  1143
    }
icculus@18
  1144
icculus@32
  1145
    PushEventIfNotFiltered(&event12);
icculus@18
  1146
icculus@18
  1147
    return 0;  /* always drop it from the 2.0 event queue. */
icculus@18
  1148
}
icculus@18
  1149
icculus@35
  1150
DECLSPEC void SDLCALL
icculus@18
  1151
SDL_SetEventFilter(SDL12_EventFilter filter12)
icculus@18
  1152
{
icculus@18
  1153
    /* We always have a filter installed, but will call the app's too. */
icculus@18
  1154
    EventFilter12 = filter12;
icculus@18
  1155
}
icculus@18
  1156
icculus@35
  1157
DECLSPEC SDL12_EventFilter SDLCALL
icculus@18
  1158
SDL_GetEventFilter(void)
icculus@18
  1159
{
icculus@18
  1160
    return EventFilter12;
icculus@18
  1161
}
icculus@18
  1162
icculus@15
  1163
icculus@34
  1164
static SDL12_Surface *
icculus@15
  1165
Surface20to12(SDL_Surface *surface20)
icculus@15
  1166
{
icculus@15
  1167
    SDL12_Surface *surface12 = NULL;
icculus@15
  1168
    SDL12_Palette *palette12 = NULL;
icculus@15
  1169
    SDL12_PixelFormat *format12 = NULL;
icculus@31
  1170
    Uint32 flags = 0;
icculus@15
  1171
icculus@15
  1172
    if (!surface20)
icculus@15
  1173
        return NULL;
icculus@15
  1174
icculus@15
  1175
    surface12 = (SDL12_Surface *) SDL20_malloc(sizeof (SDL12_Surface));
icculus@15
  1176
    if (!surface12)
icculus@15
  1177
        goto failed;
icculus@15
  1178
icculus@15
  1179
    palette12 = (SDL12_Palette *) SDL20_malloc(sizeof (SDL12_Palette));
icculus@15
  1180
    if (!palette12)
icculus@15
  1181
        goto failed;
icculus@15
  1182
icculus@15
  1183
    format12 = (SDL12_PixelFormat *) SDL20_malloc(sizeof (SDL12_PixelFormat));
icculus@15
  1184
    if (!format12)
icculus@15
  1185
        goto failed;
icculus@15
  1186
icculus@34
  1187
    SDL20_zerop(palette12);
icculus@31
  1188
    palette12->ncolors = surface20->format->palette->ncolors;
icculus@31
  1189
    palette12->colors = surface20->format->palette->colors;
icculus@15
  1190
icculus@34
  1191
    SDL20_zerop(format12);
icculus@15
  1192
    format12->palette = palette12;
icculus@15
  1193
    format12->BitsPerPixel = surface20->format->BitsPerPixel;
icculus@15
  1194
    format12->BytesPerPixel = surface20->format->BytesPerPixel;
icculus@15
  1195
    format12->Rloss = surface20->format->Rloss;
icculus@15
  1196
    format12->Gloss = surface20->format->Gloss;
icculus@15
  1197
    format12->Bloss = surface20->format->Bloss;
icculus@15
  1198
    format12->Aloss = surface20->format->Aloss;
icculus@15
  1199
    format12->Rshift = surface20->format->Rshift;
icculus@15
  1200
    format12->Gshift = surface20->format->Gshift;
icculus@15
  1201
    format12->Bshift = surface20->format->Bshift;
icculus@15
  1202
    format12->Ashift = surface20->format->Ashift;
icculus@15
  1203
    format12->Rmask = surface20->format->Rmask;
icculus@15
  1204
    format12->Gmask = surface20->format->Gmask;
icculus@15
  1205
    format12->Bmask = surface20->format->Bmask;
icculus@15
  1206
    format12->Amask = surface20->format->Amask;
icculus@15
  1207
    /* !!! FIXME: format12->colorkey; */
icculus@15
  1208
    /* !!! FIXME: format12->alpha; */
icculus@15
  1209
icculus@34
  1210
    SDL20_zerop(surface12);
icculus@15
  1211
    flags = surface20->flags;
icculus@15
  1212
    #define MAPSURFACEFLAGS(fl) { if (surface20->flags & SDL_##fl) { surface12->flags |= SDL12_##fl; flags &= ~SDL_##fl; } }
icculus@15
  1213
    MAPSURFACEFLAGS(PREALLOC);
icculus@15
  1214
    MAPSURFACEFLAGS(RLEACCEL);
icculus@34
  1215
    /*MAPSURFACEFLAGS(DONTFREE);*/
icculus@15
  1216
    #undef MAPSURFACEFLAGS
icculus@31
  1217
    SDL_assert(flags == 0);  /* non-zero if there's a flag we didn't map. */
icculus@15
  1218
icculus@15
  1219
    surface12->format = format12;
icculus@15
  1220
    surface12->w = surface20->w;
icculus@15
  1221
    surface12->h = surface20->h;
icculus@15
  1222
    surface12->pitch = (Uint16) surface20->pitch;  /* !!! FIXME: make sure this fits in a Uint16 */
icculus@15
  1223
    surface12->pixels = surface20->pixels;
icculus@15
  1224
    surface12->offset = 0;
icculus@34
  1225
    surface12->surface20 = surface20;
icculus@15
  1226
    SDL20_memcpy(&surface12->clip_rect, &surface20->clip_rect, sizeof (SDL_Rect));
icculus@15
  1227
    surface12->refcount = surface20->refcount;
icculus@15
  1228
icculus@15
  1229
    return surface12;
icculus@15
  1230
icculus@15
  1231
failed:
icculus@15
  1232
    SDL20_free(surface12);
icculus@15
  1233
    SDL20_free(palette12);
icculus@15
  1234
    SDL20_free(format12);
icculus@15
  1235
    return NULL;
icculus@15
  1236
}
icculus@15
  1237
icculus@35
  1238
DECLSPEC SDL12_Surface * SDLCALL
icculus@15
  1239
SDL_CreateRGBSurface(Uint32 sdl12flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
icculus@15
  1240
{
icculus@15
  1241
    SDL_Surface *surface20 = SDL20_CreateRGBSurface(0, width, height, depth, Rmask, Gmask, Bmask, Amask);
icculus@15
  1242
    SDL12_Surface *surface12 = Surface20to12(surface20);
icculus@15
  1243
    if (!surface12) {
icculus@15
  1244
        SDL20_FreeSurface(surface20);
icculus@15
  1245
        return NULL;
icculus@15
  1246
    }
icculus@15
  1247
icculus@31
  1248
    SDL_assert(surface12->flags == 0);  // shouldn't have prealloc, rleaccel, or dontfree.
icculus@15
  1249
    return surface12;
icculus@15
  1250
}
icculus@15
  1251
icculus@35
  1252
DECLSPEC SDL12_Surface * SDLCALL
icculus@15
  1253
SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
icculus@15
  1254
{
icculus@31
  1255
    SDL_Surface *surface20 = SDL20_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask);
icculus@15
  1256
    SDL12_Surface *surface12 = Surface20to12(surface20);
icculus@15
  1257
    if (!surface12) {
icculus@15
  1258
        SDL20_FreeSurface(surface20);
icculus@15
  1259
        return NULL;
icculus@15
  1260
    }
icculus@15
  1261
icculus@31
  1262
    SDL_assert(surface12->flags == SDL12_PREALLOC);  // should _only_ have prealloc.
icculus@15
  1263
    return surface12;
icculus@15
  1264
}
icculus@15
  1265
icculus@35
  1266
DECLSPEC void SDLCALL
icculus@35
  1267
SDL_FreeSurface(SDL12_Surface *surface12)
icculus@15
  1268
{
icculus@15
  1269
    if (surface12) {
icculus@34
  1270
        SDL20_FreeSurface(surface12->surface20);
icculus@15
  1271
        if (surface12->format) {
icculus@15
  1272
            SDL20_free(surface12->format->palette);
icculus@15
  1273
            SDL20_free(surface12->format);
icculus@15
  1274
        }
icculus@15
  1275
        SDL20_free(surface12);
icculus@15
  1276
    }
icculus@15
  1277
}
icculus@15
  1278
icculus@35
  1279
DECLSPEC void SDLCALL
icculus@24
  1280
SDL_GetClipRect(SDL12_Surface *surface12, SDL_Rect *rect)
icculus@24
  1281
{
icculus@24
  1282
    if (surface12 && rect)
icculus@24
  1283
	    SDL_memcpy(rect, &surface12->clip_rect, sizeof (SDL_Rect));
icculus@24
  1284
}
icculus@24
  1285
icculus@35
  1286
DECLSPEC SDL_bool SDLCALL
icculus@24
  1287
SDL_SetClipRect(SDL12_Surface *surface12, const SDL_Rect *rect)
icculus@24
  1288
{
icculus@24
  1289
    SDL_bool retval = SDL_FALSE;
icculus@24
  1290
    if (surface12)
icculus@24
  1291
    {
icculus@34
  1292
        retval = SDL20_SetClipRect(surface12->surface20, rect);
icculus@34
  1293
        SDL20_GetClipRect(surface12->surface20, &surface12->clip_rect);
icculus@24
  1294
    }
icculus@24
  1295
    return retval;
icculus@24
  1296
}
icculus@24
  1297
icculus@35
  1298
DECLSPEC int SDLCALL
icculus@25
  1299
SDL_FillRect(SDL12_Surface *dst, SDL_Rect *dstrect, Uint32 color)
icculus@25
  1300
{
icculus@25
  1301
    const SDL_Rect orig_dstrect = *dstrect;
icculus@34
  1302
    const int retval = SDL20_FillRect(dst->surface20, &orig_dstrect, color);
icculus@25
  1303
    if (retval != -1)
icculus@25
  1304
    {
icculus@25
  1305
        if (dstrect)  /* 1.2 stores the clip intersection in dstrect */
icculus@31
  1306
            SDL20_IntersectRect(&orig_dstrect, &dst->clip_rect, dstrect);
icculus@25
  1307
    }
icculus@25
  1308
    return retval;
icculus@25
  1309
}
icculus@25
  1310
icculus@25
  1311
icculus@17
  1312
static SDL_PixelFormat *
icculus@31
  1313
PixelFormat12to20(SDL_PixelFormat *format20, SDL_Palette *palette20, const SDL12_PixelFormat *format12)
icculus@17
  1314
{
icculus@17
  1315
    palette20->ncolors = format12->palette->ncolors;
icculus@17
  1316
    palette20->colors = format12->palette->colors;
icculus@17
  1317
    palette20->version = 1;
icculus@17
  1318
    palette20->refcount = 1;
icculus@34
  1319
    format20->format = SDL20_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
icculus@17
  1320
    format20->palette = palette20;
icculus@17
  1321
    format20->BitsPerPixel = format12->BitsPerPixel;
icculus@17
  1322
    format20->BytesPerPixel = format12->BytesPerPixel;
icculus@17
  1323
    format20->Rmask = format12->Rmask;
icculus@17
  1324
    format20->Gmask = format12->Gmask;
icculus@17
  1325
    format20->Bmask = format12->Bmask;
icculus@17
  1326
    format20->Amask = format12->Amask;
icculus@17
  1327
    format20->Rloss = format12->Rloss;
icculus@17
  1328
    format20->Gloss = format12->Gloss;
icculus@17
  1329
    format20->Bloss = format12->Bloss;
icculus@17
  1330
    format20->Aloss = format12->Aloss;
icculus@17
  1331
    format20->Rshift = format12->Rshift;
icculus@17
  1332
    format20->Gshift = format12->Gshift;
icculus@17
  1333
    format20->Bshift = format12->Bshift;
icculus@17
  1334
    format20->Ashift = format12->Ashift;
icculus@17
  1335
    format20->refcount = 1;
icculus@17
  1336
    format20->next = NULL;
icculus@17
  1337
    return format20;
icculus@17
  1338
}
icculus@17
  1339
icculus@35
  1340
DECLSPEC Uint32 SDLCALL
icculus@17
  1341
SDL_MapRGB(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b)
icculus@17
  1342
{
icculus@17
  1343
    /* This is probably way slower than apps expect. */
icculus@17
  1344
    SDL_PixelFormat format20;
icculus@17
  1345
    SDL_Palette palette20;
icculus@17
  1346
    return SDL20_MapRGB(PixelFormat12to20(&format20, &palette20, format12), r, g, b);
icculus@17
  1347
}
icculus@17
  1348
icculus@35
  1349
DECLSPEC Uint32 SDLCALL
icculus@17
  1350
SDL_MapRGBA(const SDL12_PixelFormat *format12, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
icculus@17
  1351
{
icculus@17
  1352
    /* This is probably way slower than apps expect. */
icculus@17
  1353
    SDL_PixelFormat format20;
icculus@17
  1354
    SDL_Palette palette20;
icculus@17
  1355
    return SDL20_MapRGBA(PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
icculus@17
  1356
}
icculus@17
  1357
icculus@35
  1358
DECLSPEC void SDLCALL
icculus@17
  1359
SDL_GetRGB(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b)
icculus@17
  1360
{
icculus@17
  1361
    /* This is probably way slower than apps expect. */
icculus@17
  1362
    SDL_PixelFormat format20;
icculus@17
  1363
    SDL_Palette palette20;
icculus@17
  1364
    return SDL20_GetRGB(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b);
icculus@17
  1365
}
icculus@17
  1366
icculus@35
  1367
DECLSPEC void SDLCALL
icculus@17
  1368
SDL_GetRGBA(Uint32 pixel, const SDL12_PixelFormat *format12, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
icculus@17
  1369
{
icculus@17
  1370
    /* This is probably way slower than apps expect. */
icculus@17
  1371
    SDL_PixelFormat format20;
icculus@17
  1372
    SDL_Palette palette20;
icculus@31
  1373
    return SDL20_GetRGBA(pixel, PixelFormat12to20(&format20, &palette20, format12), r, g, b, a);
icculus@17
  1374
}
icculus@15
  1375
icculus@35
  1376
DECLSPEC const SDL12_VideoInfo * SDLCALL
icculus@9
  1377
SDL_GetVideoInfo(void)
icculus@9
  1378
{
icculus@9
  1379
    SDL_DisplayMode mode;
icculus@9
  1380
icculus@39
  1381
    // !!! FIXME: calculate this in Init12Video(), then this just does: return VideoInfo.vfmt ? &VideoInfo : NULL;
icculus@39
  1382
icculus@17
  1383
    if (!VideoInfo.vfmt && SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode) == 0) {
icculus@17
  1384
        VideoInfo.vfmt = SDL20_AllocFormat(mode.format);
icculus@17
  1385
        VideoInfo.current_w = mode.w;
icculus@17
  1386
        VideoInfo.current_h = mode.h;
icculus@9
  1387
        // !!! FIXME
icculus@17
  1388
        //VideoInfo.wm_available = 1;
icculus@17
  1389
        //VideoInfo.video_mem = 1024 * 256;
icculus@9
  1390
    }
icculus@17
  1391
    return &VideoInfo;
icculus@9
  1392
}
icculus@9
  1393
icculus@35
  1394
DECLSPEC int SDLCALL
icculus@9
  1395
SDL_VideoModeOK(int width, int height, int bpp, Uint32 sdl12flags)
icculus@9
  1396
{
icculus@9
  1397
    int i, nummodes, actual_bpp = 0;
icculus@9
  1398
icculus@9
  1399
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@9
  1400
        return 0;
icculus@9
  1401
    }
icculus@9
  1402
icculus@9
  1403
    if (!(sdl12flags & SDL12_FULLSCREEN)) {
icculus@9
  1404
        SDL_DisplayMode mode;
icculus@16
  1405
        SDL20_GetDesktopDisplayMode(VideoDisplayIndex, &mode);
icculus@9
  1406
        return SDL_BITSPERPIXEL(mode.format);
icculus@9
  1407
    }
icculus@9
  1408
icculus@16
  1409
    nummodes = SDL20_GetNumDisplayModes(VideoDisplayIndex);
icculus@9
  1410
    for (i = 0; i < nummodes; ++i) {
icculus@9
  1411
        SDL_DisplayMode mode;
icculus@16
  1412
        SDL20_GetDisplayMode(VideoDisplayIndex, i, &mode);
icculus@9
  1413
        if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
icculus@9
  1414
            if (!mode.format) {
icculus@9
  1415
                return bpp;
icculus@9
  1416
            }
icculus@9
  1417
            if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
icculus@9
  1418
                actual_bpp = SDL_BITSPERPIXEL(mode.format);
icculus@9
  1419
            }
icculus@9
  1420
        }
icculus@9
  1421
    }
icculus@9
  1422
    return actual_bpp;
icculus@9
  1423
}
icculus@9
  1424
icculus@35
  1425
DECLSPEC SDL_Rect ** SDLCALL
icculus@39
  1426
SDL_ListModes(const SDL12_PixelFormat *format12, Uint32 flags)
icculus@9
  1427
{
icculus@39
  1428
    Uint32 fmt;
icculus@39
  1429
    int i;
icculus@9
  1430
icculus@9
  1431
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@9
  1432
        return NULL;
icculus@9
  1433
    }
icculus@9
  1434
icculus@39
  1435
    if ((!format12) && (!VideoInfo.vfmt)) {
icculus@39
  1436
        SDL20_SetError("No pixel format specified");
icculus@39
  1437
        return NULL;
icculus@39
  1438
    }
icculus@39
  1439
icculus@9
  1440
    if (!(flags & SDL12_FULLSCREEN)) {
icculus@39
  1441
        return (SDL_Rect **) (-1);  /* any resolution is fine. */
icculus@9
  1442
    }
icculus@9
  1443
icculus@39
  1444
    if (format12) {
icculus@39
  1445
        fmt = SDL20_MasksToPixelFormatEnum(format12->BitsPerPixel, format12->Rmask, format12->Gmask, format12->Bmask, format12->Amask);
icculus@39
  1446
    } else {
icculus@39
  1447
        fmt = VideoInfo.vfmt->format;
icculus@9
  1448
    }
icculus@9
  1449
icculus@39
  1450
    for (i = 0; i < VideoModesCount; i++) {
icculus@39
  1451
        VideoModeList *modes = &VideoModes[i];
icculus@39
  1452
        if (modes->format == fmt) {
icculus@39
  1453
            return modes->modes;
icculus@9
  1454
        }
icculus@9
  1455
    }
icculus@39
  1456
icculus@39
  1457
    SDL20_SetError("No modes support requested pixel format");
icculus@39
  1458
    return NULL;
icculus@9
  1459
}
icculus@9
  1460
icculus@35
  1461
DECLSPEC void SDLCALL
icculus@31
  1462
SDL_FreeCursor(SDL12_Cursor *cursor12)
icculus@31
  1463
{
icculus@31
  1464
    if (cursor12)
icculus@31
  1465
    {
icculus@31
  1466
        if (cursor12->wm_cursor)
icculus@31
  1467
            SDL20_FreeCursor(cursor12->wm_cursor);
icculus@31
  1468
        SDL20_free(cursor12->data);
icculus@31
  1469
        SDL20_free(cursor12->mask);
icculus@31
  1470
        SDL20_free(cursor12);
icculus@31
  1471
    }
icculus@31
  1472
}
icculus@23
  1473
icculus@35
  1474
DECLSPEC SDL12_Cursor * SDLCALL
icculus@23
  1475
SDL_CreateCursor(Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
icculus@23
  1476
{
icculus@23
  1477
    const size_t datasize = h * (w / 8);
icculus@23
  1478
    SDL_Cursor *cursor20 = NULL;
icculus@23
  1479
    SDL12_Cursor *retval = NULL;
icculus@23
  1480
icculus@23
  1481
    retval = (SDL12_Cursor *) SDL20_malloc(sizeof (SDL12_Cursor));
icculus@23
  1482
    if (!retval)
icculus@23
  1483
        goto outofmem;
icculus@23
  1484
icculus@34
  1485
    SDL20_zerop(retval);
icculus@23
  1486
icculus@23
  1487
    retval->data = (Uint8 *) SDL20_malloc(datasize);
icculus@31
  1488
    if (!retval->data)
icculus@23
  1489
        goto outofmem;
icculus@23
  1490
icculus@23
  1491
    retval->mask = (Uint8 *) SDL20_malloc(datasize);
icculus@31
  1492
    if (!retval->mask)
icculus@23
  1493
        goto outofmem;
icculus@23
  1494
icculus@23
  1495
    cursor20 = SDL20_CreateCursor(data, mask, w, h, hot_x, hot_y);
icculus@23
  1496
    if (!cursor20)
icculus@23
  1497
        goto failed;
icculus@23
  1498
icculus@23
  1499
    retval->area.w = w;
icculus@23
  1500
    retval->area.h = h;
icculus@23
  1501
    retval->hot_x = hot_x;
icculus@23
  1502
    retval->hot_y = hot_y;
icculus@23
  1503
    retval->wm_cursor = cursor20;
icculus@23
  1504
    /* we always leave retval->save as null pointers. */
icculus@23
  1505
icculus@23
  1506
    SDL20_memcpy(retval->data, data, datasize);
icculus@23
  1507
    SDL20_memcpy(retval->mask, mask, datasize);
icculus@23
  1508
icculus@23
  1509
    return retval;
icculus@23
  1510
icculus@23
  1511
outofmem:
icculus@31
  1512
    SDL20_OutOfMemory();
icculus@23
  1513
icculus@23
  1514
failed:
icculus@23
  1515
    SDL_FreeCursor(retval);
icculus@23
  1516
    return NULL;
icculus@23
  1517
}
icculus@23
  1518
icculus@35
  1519
DECLSPEC void SDLCALL
icculus@23
  1520
SDL_SetCursor(SDL12_Cursor *cursor)
icculus@23
  1521
{
icculus@23
  1522
    CurrentCursor = cursor;
icculus@23
  1523
    SDL20_SetCursor(cursor ? cursor->wm_cursor : NULL);
icculus@23
  1524
}
icculus@23
  1525
icculus@35
  1526
DECLSPEC SDL12_Cursor * SDLCALL
icculus@23
  1527
SDL_GetCursor(void)
icculus@23
  1528
{
icculus@23
  1529
    return CurrentCursor;
icculus@23
  1530
}
icculus@23
  1531
icculus@9
  1532
static void
icculus@9
  1533
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
icculus@9
  1534
{
icculus@16
  1535
    int display = VideoDisplayIndex;
icculus@9
  1536
    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
icculus@9
  1537
    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
icculus@9
  1538
    if (window) {
icculus@9
  1539
        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
icculus@9
  1540
            return;
icculus@9
  1541
        }
icculus@9
  1542
        if (SDL_strcmp(window, "center") == 0) {
icculus@9
  1543
            center = window;
icculus@9
  1544
        }
icculus@9
  1545
    }
icculus@9
  1546
    if (center) {
icculus@9
  1547
        *x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
icculus@9
  1548
        *y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
icculus@9
  1549
    }
icculus@9
  1550
}
icculus@9
  1551
icculus@34
  1552
#if SANITY_CHECK_THIS_CODE
icculus@9
  1553
static void
icculus@9
  1554
ClearVideoSurface()
icculus@9
  1555
{
icculus@16
  1556
    if (ShadowSurface) {
icculus@17
  1557
        SDL20_FillRect(ShadowSurface, NULL,
icculus@17
  1558
            SDL20_MapRGB(ShadowSurface->format, 0, 0, 0));
icculus@9
  1559
    }
icculus@17
  1560
    SDL20_FillRect(WindowSurface, NULL, 0);
icculus@19
  1561
    SDL20_UpdateWindowSurface(VideoWindow20);
icculus@9
  1562
}
icculus@34
  1563
#endif
icculus@9
  1564
icculus@9
  1565
static void
icculus@19
  1566
SetupScreenSaver(int flags12)
icculus@9
  1567
{
icculus@9
  1568
    const char *env;
icculus@9
  1569
    SDL_bool allow_screensaver;
icculus@9
  1570
icculus@9
  1571
    /* Allow environment override of screensaver disable */
icculus@9
  1572
    env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
icculus@9
  1573
    if (env) {
icculus@34
  1574
        allow_screensaver = SDL20_atoi(env) ? SDL_TRUE : SDL_FALSE;
icculus@19
  1575
    } else if (flags12 & SDL12_FULLSCREEN) {
icculus@9
  1576
        allow_screensaver = SDL_FALSE;
icculus@9
  1577
    } else {
icculus@9
  1578
        allow_screensaver = SDL_TRUE;
icculus@9
  1579
    }
icculus@9
  1580
    if (allow_screensaver) {
icculus@17
  1581
        SDL20_EnableScreenSaver();
icculus@9
  1582
    } else {
icculus@17
  1583
        SDL20_DisableScreenSaver();
icculus@9
  1584
    }
icculus@9
  1585
}
icculus@9
  1586
icculus@34
  1587
#if SANITY_CHECK_THIS_CODE
icculus@9
  1588
static int
icculus@19
  1589
ResizeVideoMode(int width, int height, int bpp, Uint32 flags12)
icculus@9
  1590
{
icculus@9
  1591
    int w, h;
icculus@9
  1592
icculus@9
  1593
    /* We can't resize something we don't have... */
icculus@16
  1594
    if (!VideoSurface) {
icculus@9
  1595
        return -1;
icculus@9
  1596
    }
icculus@9
  1597
icculus@9
  1598
    /* We probably have to recreate the window in fullscreen mode */
icculus@19
  1599
    if (flags12 & SDL12_FULLSCREEN) {
icculus@9
  1600
        return -1;
icculus@9
  1601
    }
icculus@9
  1602
icculus@9
  1603
    /* I don't think there's any change we can gracefully make in flags */
icculus@19
  1604
    if (flags12 != VideoFlags) {
icculus@9
  1605
        return -1;
icculus@9
  1606
    }
icculus@16
  1607
    if (bpp != VideoSurface->format->BitsPerPixel) {
icculus@9
  1608
        return -1;
icculus@9
  1609
    }
icculus@9
  1610
icculus@9
  1611
    /* Resize the window */
icculus@19
  1612
    SDL20_GetWindowSize(VideoWindow20, &w, &h);
icculus@9
  1613
    if (w != width || h != height) {
icculus@19
  1614
        SDL20_SetWindowSize(VideoWindow20, width, height);
icculus@9
  1615
    }
icculus@9
  1616
icculus@9
  1617
    /* If we're in OpenGL mode, just resize the stub surface and we're done! */
icculus@19
  1618
    if (flags12 & SDL12_OPENGL) {
icculus@16
  1619
        VideoSurface->w = width;
icculus@16
  1620
        VideoSurface->h = height;
icculus@9
  1621
        return 0;
icculus@9
  1622
    }
icculus@9
  1623
icculus@19
  1624
    WindowSurface = SDL20_GetWindowSurface(VideoWindow20);
icculus@16
  1625
    if (!WindowSurface) {
icculus@9
  1626
        return -1;
icculus@9
  1627
    }
icculus@16
  1628
    if (VideoSurface->format != WindowSurface->format) {
icculus@9
  1629
        return -1;
icculus@9
  1630
    }
icculus@16
  1631
    VideoSurface->w = width;
icculus@16
  1632
    VideoSurface->h = height;
icculus@16
  1633
    VideoSurface->pixels = WindowSurface->pixels;
icculus@16
  1634
    VideoSurface->pitch = WindowSurface->pitch;
icculus@19
  1635
    SDL20_SetClipRect(VideoSurface, NULL);
icculus@9
  1636
icculus@16
  1637
    if (ShadowSurface) {
icculus@16
  1638
        ShadowSurface->w = width;
icculus@16
  1639
        ShadowSurface->h = height;
icculus@19
  1640
        ShadowSurface->pitch = SDL20_CalculatePitch(ShadowSurface);
icculus@16
  1641
        ShadowSurface->pixels =
icculus@19
  1642
            SDL20_realloc(ShadowSurface->pixels,
icculus@16
  1643
                        ShadowSurface->h * ShadowSurface->pitch);
icculus@19
  1644
        SDL20_SetClipRect(ShadowSurface, NULL);
icculus@19
  1645
        SDL20_InvalidateMap(ShadowSurface->map);
icculus@9
  1646
    } else {
icculus@16
  1647
        PublicSurface = VideoSurface;
icculus@9
  1648
    }
icculus@9
  1649
icculus@9
  1650
    ClearVideoSurface();
icculus@9
  1651
icculus@9
  1652
    return 0;
icculus@9
  1653
}
icculus@9
  1654
icculus@35
  1655
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1656
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags12)
icculus@9
  1657
{
icculus@9
  1658
    SDL_DisplayMode desktop_mode;
icculus@16
  1659
    int display = VideoDisplayIndex;
icculus@9
  1660
    int window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
icculus@9
  1661
    int window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
icculus@9
  1662
    int window_w;
icculus@9
  1663
    int window_h;
icculus@19
  1664
    Uint32 window_flags20;
icculus@19
  1665
    Uint32 surface_flags12;
icculus@19
  1666
icculus@19
  1667
    if (!SDL20_WasInit(SDL_INIT_VIDEO)) {
icculus@19
  1668
        if (SDL_Init(SDL12_INIT_VIDEO | SDL12_INIT_NOPARACHUTE) < 0) {
icculus@9
  1669
            return NULL;
icculus@9
  1670
        }
icculus@9
  1671
    }
icculus@9
  1672
icculus@19
  1673
    SDL20_GetDesktopDisplayMode(display, &desktop_mode);
icculus@9
  1674
icculus@9
  1675
    if (width == 0) {
icculus@9
  1676
        width = desktop_mode.w;
icculus@9
  1677
    }
icculus@9
  1678
    if (height == 0) {
icculus@9
  1679
        height = desktop_mode.h;
icculus@9
  1680
    }
icculus@9
  1681
    if (bpp == 0) {
icculus@9
  1682
        bpp = SDL_BITSPERPIXEL(desktop_mode.format);
icculus@9
  1683
    }
icculus@9
  1684
icculus@9
  1685
    /* See if we can simply resize the existing window and surface */
icculus@19
  1686
    if (ResizeVideoMode(width, height, bpp, flags12) == 0) {
icculus@16
  1687
        return PublicSurface;
icculus@9
  1688
    }
icculus@9
  1689
icculus@9
  1690
    /* Destroy existing window */
icculus@16
  1691
    PublicSurface = NULL;
icculus@16
  1692
    if (ShadowSurface) {
icculus@16
  1693
        ShadowSurface->flags &= ~SDL_DONTFREE;
icculus@19
  1694
        SDL20_FreeSurface(ShadowSurface);
icculus@16
  1695
        ShadowSurface = NULL;
icculus@9
  1696
    }
icculus@16
  1697
    if (VideoSurface) {
icculus@16
  1698
        VideoSurface->flags &= ~SDL_DONTFREE;
icculus@19
  1699
        SDL20_FreeSurface(VideoSurface);
icculus@16
  1700
        VideoSurface = NULL;
icculus@9
  1701
    }
icculus@16
  1702
    if (VideoContext) {
icculus@9
  1703
        /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
icculus@19
  1704
        SDL20_GL_DeleteContext(VideoContext);
icculus@16
  1705
        VideoContext = NULL;
icculus@9
  1706
    }
icculus@19
  1707
    if (VideoWindow20) {
icculus@19
  1708
        SDL20_GetWindowPosition(VideoWindow20, &window_x, &window_y);
icculus@19
  1709
        SDL20_DestroyWindow(VideoWindow20);
icculus@9
  1710
    }
icculus@9
  1711
icculus@19
  1712
    /* Create a new window */
icculus@19
  1713
    window_flags20 = SDL_WINDOW_SHOWN;
icculus@19
  1714
    if (flags12 & SDL12_FULLSCREEN) {
icculus@19
  1715
        window_flags20 |= SDL_WINDOW_FULLSCREEN;
icculus@9
  1716
    }
icculus@19
  1717
    if (flags12 & SDL12_OPENGL) {
icculus@19
  1718
        window_flags20 |= SDL_WINDOW_OPENGL;
icculus@9
  1719
    }
icculus@19
  1720
    if (flags12 & SDL12_RESIZABLE) {
icculus@19
  1721
        window_flags20 |= SDL_WINDOW_RESIZABLE;
icculus@9
  1722
    }
icculus@19
  1723
    if (flags12 & SDL12_NOFRAME) {
icculus@19
  1724
        window_flags20 |= SDL_WINDOW_BORDERLESS;
icculus@9
  1725
    }
icculus@9
  1726
    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
icculus@19
  1727
    VideoWindow20 =
icculus@19
  1728
        SDL20_CreateWindow(WindowTitle, window_x, window_y, width, height,
icculus@19
  1729
                         window_flags20);
icculus@19
  1730
    if (!VideoWindow20) {
icculus@9
  1731
        return NULL;
icculus@9
  1732
    }
icculus@19
  1733
    SDL20_SetWindowIcon(VideoWindow20, VideoIcon);
icculus@19
  1734
icculus@19
  1735
    SetupScreenSaver(flags12);
icculus@19
  1736
icculus@34
  1737
    window_flags20 = SDL20_GetWindowFlags(VideoWindow20);
icculus@19
  1738
    surface_flags12 = 0;
icculus@19
  1739
    if (window_flags20 & SDL_WINDOW_FULLSCREEN) {
icculus@19
  1740
        surface_flags12 |= SDL_FULLSCREEN;
icculus@9
  1741
    }
icculus@19
  1742
    if ((window_flags & SDL_WINDOW_OPENGL) && (flags12 & SDL_OPENGL)) {
icculus@19
  1743
        surface_flags12 |= SDL_OPENGL;
icculus@9
  1744
    }
icculus@9
  1745
    if (window_flags & SDL_WINDOW_RESIZABLE) {
icculus@19
  1746
        surface_flags12 |= SDL_RESIZABLE;
icculus@9
  1747
    }
icculus@9
  1748
    if (window_flags & SDL_WINDOW_BORDERLESS) {
icculus@19
  1749
        surface_flags12 |= SDL_NOFRAME;
icculus@9
  1750
    }
icculus@9
  1751
icculus@19
  1752
    VideoFlags = flags12;
icculus@9
  1753
icculus@9
  1754
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
icculus@19
  1755
    if (flags12 & SDL_OPENGL) {
icculus@31
  1756
icculus@19
  1757
        VideoContext = SDL_GL_CreateContext(VideoWindow20);
icculus@16
  1758
        if (!VideoContext) {
icculus@9
  1759
            return NULL;
icculus@9
  1760
        }
icculus@19
  1761
        if (SDL_GL_MakeCurrent(VideoWindow20, VideoContext) < 0) {
icculus@9
  1762
            return NULL;
icculus@9
  1763
        }
icculus@30
  1764
icculus@31
  1765
        SDL20_GL_SetSwapInterval(SwapInterval);  /* don't care if this fails. */
icculus@30
  1766
icculus@16
  1767
        VideoSurface =
icculus@9
  1768
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
icculus@16
  1769
        if (!VideoSurface) {
icculus@9
  1770
            return NULL;
icculus@9
  1771
        }
icculus@19
  1772
        VideoSurface->flags |= surface_flags12;
icculus@16
  1773
        PublicSurface = VideoSurface;
icculus@16
  1774
        return PublicSurface;
icculus@9
  1775
    }
icculus@9
  1776
icculus@9
  1777
    /* Create the screen surface */
icculus@19
  1778
    WindowSurface = SDL_GetWindowSurface(VideoWindow20);
icculus@16
  1779
    if (!WindowSurface) {
icculus@9
  1780
        return NULL;
icculus@9
  1781
    }
icculus@9
  1782
icculus@9
  1783
    /* Center the public surface in the window surface */
icculus@19
  1784
    SDL_GetWindowSize(VideoWindow20, &window_w, &window_h);
icculus@16
  1785
    VideoViewport.x = (window_w - width)/2;
icculus@16
  1786
    VideoViewport.y = (window_h - height)/2;
icculus@16
  1787
    VideoViewport.w = width;
icculus@16
  1788
    VideoViewport.h = height;
icculus@9
  1789
icculus@16
  1790
    VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
icculus@19
  1791
    VideoSurface->flags |= surface_flags12;
icculus@19
  1792
    VideoSurface->flags |= SDL12_DONTFREE;
icculus@16
  1793
    SDL_FreeFormat(VideoSurface->format);
icculus@16
  1794
    VideoSurface->format = WindowSurface->format;
icculus@16
  1795
    VideoSurface->format->refcount++;
icculus@16
  1796
    VideoSurface->w = width;
icculus@16
  1797
    VideoSurface->h = height;
icculus@16
  1798
    VideoSurface->pitch = WindowSurface->pitch;
icculus@16
  1799
    VideoSurface->pixels = (void *)((Uint8 *)WindowSurface->pixels +
icculus@16
  1800
        VideoViewport.y * VideoSurface->pitch +
icculus@16
  1801
        VideoViewport.x  * VideoSurface->format->BytesPerPixel);
icculus@16
  1802
    SDL_SetClipRect(VideoSurface, NULL);
icculus@9
  1803
icculus@9
  1804
    /* Create a shadow surface if necessary */
icculus@16
  1805
    if ((bpp != VideoSurface->format->BitsPerPixel)
icculus@19
  1806
        && !(flags12 & SDL12_ANYFORMAT)) {
icculus@16
  1807
        ShadowSurface =
icculus@9
  1808
            SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
icculus@16
  1809
        if (!ShadowSurface) {
icculus@9
  1810
            return NULL;
icculus@9
  1811
        }
icculus@19
  1812
        ShadowSurface->flags |= surface_flags12;
icculus@19
  1813
        ShadowSurface->flags |= SDL12_DONTFREE;
icculus@9
  1814
icculus@16
  1815
        /* 8-bit ShadowSurface surfaces report that they have exclusive palette */
icculus@16
  1816
        if (ShadowSurface->format->palette) {
icculus@19
  1817
            ShadowSurface->flags |= SDL12_HWPALETTE;
icculus@16
  1818
            SDL_DitherColors(ShadowSurface->format->palette->colors,
icculus@16
  1819
                             ShadowSurface->format->BitsPerPixel);
icculus@9
  1820
        }
icculus@16
  1821
        SDL_FillRect(ShadowSurface, NULL,
icculus@16
  1822
            SDL_MapRGB(ShadowSurface->format, 0, 0, 0));
icculus@9
  1823
    }
icculus@16
  1824
    PublicSurface =
icculus@16
  1825
        (ShadowSurface ? ShadowSurface : VideoSurface);
icculus@9
  1826
icculus@9
  1827
    ClearVideoSurface();
icculus@9
  1828
icculus@9
  1829
    /* We're finally done! */
icculus@16
  1830
    return PublicSurface;
icculus@9
  1831
}
icculus@9
  1832
icculus@35
  1833
DECLSPEC SDL12_Surface * SDLCALL
icculus@9
  1834
SDL_GetVideoSurface(void)
icculus@9
  1835
{
icculus@16
  1836
    return PublicSurface;
icculus@9
  1837
}
icculus@9
  1838
icculus@35
  1839
DECLSPEC int SDLCALL
icculus@35
  1840
SDL_SetAlpha(SDL12_Surface * surface, Uint32 flag, Uint8 value)
icculus@9
  1841
{
icculus@9
  1842
    if (flag & SDL_SRCALPHA) {
icculus@9
  1843
        /* According to the docs, value is ignored for alpha surfaces */
icculus@9
  1844
        if (surface->format->Amask) {
icculus@9
  1845
            value = 0xFF;
icculus@9
  1846
        }
icculus@9
  1847
        SDL_SetSurfaceAlphaMod(surface, value);
icculus@9
  1848
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
icculus@9
  1849
    } else {
icculus@9
  1850
        SDL_SetSurfaceAlphaMod(surface, 0xFF);
icculus@9
  1851
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
icculus@9
  1852
    }
icculus@9
  1853
    SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
icculus@9
  1854
icculus@9
  1855
    return 0;
icculus@9
  1856
}
icculus@9
  1857
icculus@35
  1858
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1859
SDL_DisplayFormat(SDL12_Surface *surface12)
icculus@9
  1860
{
icculus@19
  1861
    SDL12_PixelFormat *format;
icculus@9
  1862
icculus@16
  1863
    if (!PublicSurface) {
icculus@19
  1864
        SDL20_SetError("No video mode has been set");
icculus@9
  1865
        return NULL;
icculus@9
  1866
    }
icculus@16
  1867
    format = PublicSurface->format;
icculus@9
  1868
icculus@9
  1869
    /* Set the flags appropriate for copying to display surface */
icculus@19
  1870
    return SDL_ConvertSurface(surface, format, SDL12_RLEACCEL);
icculus@9
  1871
}
icculus@9
  1872
icculus@35
  1873
DECLSPEC SDL12_Surface * SDLCALL
icculus@19
  1874
SDL_DisplayFormatAlpha(SDL12_Surface *surface)
icculus@9
  1875
{
icculus@9
  1876
    SDL_PixelFormat *vf;
icculus@9
  1877
    SDL_PixelFormat *format;
icculus@9
  1878
    SDL_Surface *converted;
icculus@9
  1879
    /* default to ARGB8888 */
icculus@9
  1880
    Uint32 amask = 0xff000000;
icculus@9
  1881
    Uint32 rmask = 0x00ff0000;
icculus@9
  1882
    Uint32 gmask = 0x0000ff00;
icculus@9
  1883
    Uint32 bmask = 0x000000ff;
icculus@9
  1884
icculus@16
  1885
    if (!PublicSurface) {
icculus@34
  1886
        SDL20_SetError("No video mode has been set");
icculus@9
  1887
        return NULL;
icculus@9
  1888
    }
icculus@16
  1889
    vf = PublicSurface->format;
icculus@9
  1890
icculus@9
  1891
    switch (vf->BytesPerPixel) {
icculus@9
  1892
    case 2:
icculus@9
  1893
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
icculus@9
  1894
           For anything else (like ARGB4444) it doesn't matter
icculus@9
  1895
           since we have no special code for it anyway */
icculus@9
  1896
        if ((vf->Rmask == 0x1f) &&
icculus@9
  1897
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
icculus@9
  1898
            rmask = 0xff;
icculus@9
  1899
            bmask = 0xff0000;
icculus@9
  1900
        }
icculus@9
  1901
        break;
icculus@9
  1902
icculus@9
  1903
    case 3:
icculus@9
  1904
    case 4:
icculus@9
  1905
        /* Keep the video format, as long as the high 8 bits are
icculus@9
  1906
           unused or alpha */
icculus@9
  1907
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
icculus@9
  1908
            rmask = 0xff;
icculus@9
  1909
            bmask = 0xff0000;
icculus@9
  1910
        }
icculus@9
  1911
        break;
icculus@9
  1912
icculus@9
  1913
    default:
icculus@9
  1914
        /* We have no other optimised formats right now. When/if a new
icculus@9
  1915
           optimised alpha format is written, add the converter here */
icculus@9
  1916
        break;
icculus@9
  1917
    }
icculus@9
  1918
    format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask,
icculus@9
  1919
                                                            gmask,
icculus@9
  1920
                                                            bmask,
icculus@9
  1921
                                                            amask));
icculus@9
  1922
    if (!format) {
icculus@9
  1923
        return NULL;
icculus@9
  1924
    }
icculus@9
  1925
    converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
icculus@9
  1926
    SDL_FreeFormat(format);
icculus@9
  1927
    return converted;
icculus@9
  1928
}
icculus@9
  1929
icculus@35
  1930
DECLSPEC void SDLCALL
icculus@9
  1931
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
icculus@9
  1932
{
icculus@9
  1933
    int i;
icculus@9
  1934
icculus@16
  1935
    if (screen == ShadowSurface) {
icculus@9
  1936
        for (i = 0; i < numrects; ++i) {
icculus@16
  1937
            SDL_BlitSurface(ShadowSurface, &rects[i], VideoSurface,
icculus@9
  1938
                            &rects[i]);
icculus@9
  1939
        }
icculus@9
  1940
icculus@9
  1941
        /* Fall through to video surface update */
icculus@16
  1942
        screen = VideoSurface;
icculus@9
  1943
    }
icculus@16
  1944
    if (screen == VideoSurface) {
icculus@16
  1945
        if (VideoViewport.x || VideoViewport.y) {
icculus@9
  1946
            SDL_Rect *stackrects = SDL_stack_alloc(SDL_Rect, numrects);
icculus@9
  1947
            SDL_Rect *stackrect;
icculus@9
  1948
            const SDL_Rect *rect;
icculus@9
  1949
            
icculus@9
  1950
            /* Offset all the rectangles before updating */
icculus@9
  1951
            for (i = 0; i < numrects; ++i) {
icculus@9
  1952
                rect = &rects[i];
icculus@9
  1953
                stackrect = &stackrects[i];
icculus@16
  1954
                stackrect->x = VideoViewport.x + rect->x;
icculus@16
  1955
                stackrect->y = VideoViewport.y + rect->y;
icculus@9
  1956
                stackrect->w = rect->w;
icculus@9
  1957
                stackrect->h = rect->h;
icculus@9
  1958
            }
icculus@19
  1959
            SDL_UpdateWindowSurfaceRects(VideoWindow20, stackrects, numrects);
icculus@9
  1960
            SDL_stack_free(stackrects);
icculus@9
  1961
        } else {
icculus@19
  1962
            SDL_UpdateWindowSurfaceRects(VideoWindow20, rects, numrects);
icculus@9
  1963
        }
icculus@9
  1964
    }
icculus@9
  1965
}
icculus@34
  1966
#endif
icculus@9
  1967
icculus@35
  1968
DECLSPEC void SDLCALL
icculus@35
  1969
SDL_UpdateRect(SDL12_Surface *screen12, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
icculus@31
  1970
{
icculus@35
  1971
    if (screen12) {
icculus@31
  1972
        SDL_Rect rect;
icculus@31
  1973
        rect.x = (int) x;
icculus@31
  1974
        rect.y = (int) y;
icculus@35
  1975
        rect.w = (int) (w ? w : screen12->w);
icculus@35
  1976
        rect.h = (int) (h ? h : screen12->h);
icculus@35
  1977
        SDL_UpdateRects(screen12, 1, &rect);
icculus@31
  1978
    }
icculus@31
  1979
}
icculus@31
  1980
icculus@35
  1981
DECLSPEC int SDLCALL
icculus@35
  1982
SDL_Flip(SDL12_Surface *screen12)
icculus@31
  1983
{
icculus@35
  1984
    SDL_UpdateRect(screen12, 0, 0, 0, 0);
icculus@31
  1985
    return 0;
icculus@31
  1986
}
icculus@31
  1987
icculus@35
  1988
DECLSPEC void SDLCALL
icculus@9
  1989
SDL_WM_SetCaption(const char *title, const char *icon)
icculus@9
  1990
{
icculus@16
  1991
    if (WindowTitle) {
icculus@31
  1992
        SDL20_free(WindowTitle);
icculus@9
  1993
    }
icculus@16
  1994
    if (WindowIconTitle) {
icculus@31
  1995
        SDL20_free(WindowIconTitle);
icculus@9
  1996
    }
icculus@16
  1997
    WindowTitle = title ? SDL_strdup(title) : NULL;
icculus@16
  1998
    WindowIconTitle = icon ? SDL_strdup(icon) : NULL;
icculus@34
  1999
    SDL20_SetWindowTitle(VideoWindow20, WindowTitle);
icculus@9
  2000
}
icculus@9
  2001
icculus@35
  2002
DECLSPEC void SDLCALL
icculus@9
  2003
SDL_WM_GetCaption(const char **title, const char **icon)
icculus@9
  2004
{
icculus@9
  2005
    if (title) {
icculus@16
  2006
        *title = WindowTitle;
icculus@9
  2007
    }
icculus@9
  2008
    if (icon) {
icculus@16
  2009
        *icon = WindowIconTitle;
icculus@9
  2010
    }
icculus@9
  2011
}
icculus@9
  2012
icculus@34
  2013
#if SANITY_CHECK_THIS_CODE
icculus@35
  2014
DECLSPEC void SDLCALL
icculus@34
  2015
SDL_WM_SetIcon(SDL_Surface *icon, Uint8 *mask)
icculus@9
  2016
{
icculus@9
  2017
    // !!! FIXME: free previous icon?
icculus@16
  2018
    VideoIcon = icon;
icculus@16
  2019
    ++VideoIcon->refcount;
icculus@9
  2020
}
icculus@34
  2021
#endif
icculus@9
  2022
icculus@35
  2023
DECLSPEC int SDLCALL
icculus@9
  2024
SDL_WM_IconifyWindow(void)
icculus@9
  2025
{
icculus@34
  2026
    SDL20_MinimizeWindow(VideoWindow20);
icculus@9
  2027
    return 0;
icculus@9
  2028
}
icculus@9
  2029
icculus@34
  2030
#if SANITY_CHECK_THIS_CODE
icculus@35
  2031
DECLSPEC int SDLCALL
icculus@34
  2032
SDL_WM_ToggleFullScreen(SDL_Surface *surface)
icculus@9
  2033
{
icculus@9
  2034
    int length;
icculus@9
  2035
    void *pixels;
icculus@9
  2036
    Uint8 *src, *dst;
icculus@9
  2037
    int row;
icculus@9
  2038
    int window_w;
icculus@9
  2039
    int window_h;
icculus@9
  2040
icculus@16
  2041
    if (!PublicSurface) {
icculus@34
  2042
        SDL20_SetError("SDL_SetVideoMode() hasn't been called");
icculus@9
  2043
        return 0;
icculus@9
  2044
    }
icculus@9
  2045
icculus@9
  2046
    /* Copy the old bits out */
icculus@16
  2047
    length = PublicSurface->w * PublicSurface->format->BytesPerPixel;
icculus@31
  2048
    pixels = SDL20_malloc(PublicSurface->h * length);
icculus@16
  2049
    if (pixels && PublicSurface->pixels) {
icculus@16
  2050
        src = (Uint8*)PublicSurface->pixels;
icculus@9
  2051
        dst = (Uint8*)pixels;
icculus@16
  2052
        for (row = 0; row < PublicSurface->h; ++row) {
icculus@9
  2053
            SDL_memcpy(dst, src, length);
icculus@16
  2054
            src += PublicSurface->pitch;
icculus@9
  2055
            dst += length;
icculus@9
  2056
        }
icculus@9
  2057
    }
icculus@9
  2058
icculus@9
  2059
    /* Do the physical mode switch */
icculus@34
  2060
    if (SDL20_GetWindowFlags(VideoWindow20) & SDL_WINDOW_FULLSCREEN) {
icculus@34
  2061
        if (SDL20_SetWindowFullscreen(VideoWindow20, 0) < 0) {
icculus@9
  2062
            return 0;
icculus@9
  2063
        }
icculus@16
  2064
        PublicSurface->flags &= ~SDL_FULLSCREEN;
icculus@9
  2065
    } else {
icculus@34
  2066
        if (SDL20_SetWindowFullscreen(VideoWindow20, 1) < 0) {
icculus@9
  2067
            return 0;
icculus@9
  2068
        }
icculus@16
  2069
        PublicSurface->flags |= SDL_FULLSCREEN;
icculus@9
  2070
    }
icculus@9
  2071
icculus@9
  2072
    /* Recreate the screen surface */
icculus@34
  2073
    WindowSurface = SDL20_GetWindowSurface(VideoWindow20);
icculus@16
  2074
    if (!WindowSurface) {
icculus@9
  2075
        /* We're totally hosed... */
icculus@9
  2076
        return 0;
icculus@9
  2077
    }
icculus@9
  2078
icculus@9
  2079
    /* Center the public surface in the window surface */
icculus@19
  2080
    SDL_GetWindowSize(VideoWindow20, &window_w, &window_h);
icculus@16
  2081
    VideoViewport.x = (window_w - VideoSurface->w)/2;
icculus@16
  2082
    VideoViewport.y = (window_h - VideoSurface->h)/2;
icculus@16
  2083
    VideoViewport.w = VideoSurface->w;
icculus@16
  2084
    VideoViewport.h = VideoSurface->h;
icculus@9
  2085
icculus@9
  2086
    /* Do some shuffling behind the application's back if format changes */
icculus@16
  2087
    if (VideoSurface->format->format != WindowSurface->format->format) {
icculus@16
  2088
        if (ShadowSurface) {
icculus@16
  2089
            if (ShadowSurface->format->format == WindowSurface->format->format) {
icculus@9
  2090
                /* Whee!  We don't need a shadow surface anymore! */
icculus@16
  2091
                VideoSurface->flags &= ~SDL_DONTFREE;
icculus@16
  2092
                SDL_FreeSurface(VideoSurface);
icculus@31
  2093
                SDL20_free(ShadowSurface->pixels);
icculus@16
  2094
                VideoSurface = ShadowSurface;
icculus@16
  2095
                VideoSurface->flags |= SDL_PREALLOC;
icculus@16
  2096
                ShadowSurface = NULL;
icculus@9
  2097
            } else {
icculus@9
  2098
                /* No problem, just change the video surface format */
icculus@16
  2099
                SDL_FreeFormat(VideoSurface->format);
icculus@16
  2100
                VideoSurface->format = WindowSurface->format;
icculus@16
  2101
                VideoSurface->format->refcount++;
icculus@16
  2102
                SDL_InvalidateMap(ShadowSurface->map);
icculus@9
  2103
            }
icculus@9
  2104
        } else {
icculus@9
  2105
            /* We can make the video surface the shadow surface */
icculus@16
  2106
            ShadowSurface = VideoSurface;
icculus@16
  2107
            ShadowSurface->pitch = SDL_CalculatePitch(ShadowSurface);
icculus@31
  2108
            ShadowSurface->pixels = SDL20_malloc(ShadowSurface->h * ShadowSurface->pitch);
icculus@16
  2109
            if (!ShadowSurface->pixels) {
icculus@9
  2110
                /* Uh oh, we're hosed */
icculus@16
  2111
                ShadowSurface = NULL;
icculus@9
  2112
                return 0;
icculus@9
  2113
            }
icculus@16
  2114
            ShadowSurface->flags &= ~SDL_PREALLOC;
icculus@9
  2115
icculus@16
  2116
            VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
icculus@16
  2117
            VideoSurface->flags = ShadowSurface->flags;
icculus@16
  2118
            VideoSurface->flags |= SDL_PREALLOC;
icculus@16
  2119
            SDL_FreeFormat(VideoSurface->format);
icculus@16
  2120
            VideoSurface->format = WindowSurface->format;
icculus@16
  2121
            VideoSurface->format->refcount++;
icculus@16
  2122
            VideoSurface->w = ShadowSurface->w;
icculus@16
  2123
            VideoSurface->h = ShadowSurface->h;
icculus@9
  2124
        }
icculus@9
  2125
    }
icculus@9
  2126
icculus@9
  2127
    /* Update the video surface */
icculus@16
  2128
    VideoSurface->pitch = WindowSurface->pitch;
icculus@16
  2129
    VideoSurface->pixels = (void *)((Uint8 *)WindowSurface->pixels +
icculus@16
  2130
        VideoViewport.y * VideoSurface->pitch +
icculus@16
  2131
        VideoViewport.x  * VideoSurface->format->BytesPerPixel);
icculus@16
  2132
    SDL_SetClipRect(VideoSurface, NULL);
icculus@9
  2133
icculus@9
  2134
    /* Copy the old bits back */
icculus@9
  2135
    if (pixels) {
icculus@9
  2136
        src = (Uint8*)pixels;
icculus@16
  2137
        dst = (Uint8*)PublicSurface->pixels;
icculus@16
  2138
        for (row = 0; row < PublicSurface->h; ++row) {
icculus@9
  2139
            SDL_memcpy(dst, src, length);
icculus@9
  2140
            src += length;
icculus@16
  2141
            dst += PublicSurface->pitch;
icculus@9
  2142
        }
icculus@16
  2143
        SDL_Flip(PublicSurface);
icculus@31
  2144
        SDL20_free(pixels);
icculus@9
  2145
    }
icculus@9
  2146
icculus@9
  2147
    /* We're done! */
icculus@9
  2148
    return 1;
icculus@9
  2149
}
icculus@34
  2150
#endif
icculus@9
  2151
icculus@31
  2152
typedef enum
icculus@9
  2153
{
icculus@31
  2154
    SDL12_GRAB_QUERY = -1,
icculus@31
  2155
    SDL12_GRAB_OFF = 0,
icculus@31
  2156
    SDL12_GRAB_ON = 1
icculus@31
  2157
} SDL12_GrabMode;
icculus@31
  2158
icculus@35
  2159
DECLSPEC SDL12_GrabMode SDLCALL
icculus@31
  2160
SDL_WM_GrabInput(SDL12_GrabMode mode)
icculus@31
  2161
{
icculus@31
  2162
    if (mode != SDL12_GRAB_QUERY) {
icculus@34
  2163
        SDL20_SetWindowGrab(VideoWindow20, (mode == SDL12_GRAB_ON));
icculus@9
  2164
    }
icculus@34
  2165
    return SDL20_GetWindowGrab(VideoWindow20) ? SDL12_GRAB_ON : SDL12_GRAB_OFF;
icculus@9
  2166
}
icculus@9
  2167
icculus@35
  2168
DECLSPEC void SDLCALL
icculus@9
  2169
SDL_WarpMouse(Uint16 x, Uint16 y)
icculus@9
  2170
{
icculus@34
  2171
    SDL20_WarpMouseInWindow(VideoWindow20, x, y);
icculus@9
  2172
}
icculus@9
  2173
icculus@35
  2174
DECLSPEC Uint8 SDLCALL
icculus@9
  2175
SDL_GetAppState(void)
icculus@9
  2176
{
icculus@34
  2177
    Uint8 state12 = 0;
icculus@34
  2178
    Uint32 flags20 = 0;
icculus@34
  2179
icculus@34
  2180
    flags20 = SDL20_GetWindowFlags(VideoWindow20);
icculus@34
  2181
    if ((flags20 & SDL_WINDOW_SHOWN) && !(flags20 & SDL_WINDOW_MINIMIZED)) {
icculus@34
  2182
        state12 |= SDL12_APPACTIVE;
icculus@9
  2183
    }
icculus@34
  2184
    if (flags20 & SDL_WINDOW_INPUT_FOCUS) {
icculus@34
  2185
        state12 |= SDL12_APPINPUTFOCUS;
icculus@9
  2186
    }
icculus@34
  2187
    if (flags20 & SDL_WINDOW_MOUSE_FOCUS) {
icculus@34
  2188
        state12 |= SDL12_APPMOUSEFOCUS;
icculus@9
  2189
    }
icculus@34
  2190
    return state12;
icculus@9
  2191
}
icculus@9
  2192
icculus@34
  2193
#if SANITY_CHECK_THIS_CODE
icculus@35
  2194
DECLSPEC int SDLCALL
icculus@35
  2195
SDL_SetPalette(SDL12_Surface *surface12, int flags, const SDL_Color *colors,
icculus@9
  2196
               int firstcolor, int ncolors)
icculus@9
  2197
{
icculus@35
  2198
    return SDL_SetColors(surface12, colors, firstcolor, ncolors);
icculus@9
  2199
}
icculus@9
  2200
icculus@35
  2201
DECLSPEC int SDLCALL
icculus@35
  2202
SDL_SetColors(SDL12_Surface *surface12, const SDL_Color * colors, int firstcolor,
icculus@9
  2203
              int ncolors)
icculus@9
  2204
{
icculus@35
  2205
    if (SDL20_SetPaletteColors
icculus@9
  2206
        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
icculus@9
  2207
        return 1;
icculus@9
  2208
    } else {
icculus@9
  2209
        return 0;
icculus@9
  2210
    }
icculus@9
  2211
}
icculus@9
  2212
icculus@35
  2213
DECLSPEC int SDLCALL
icculus@9
  2214
SDL_GetWMInfo(SDL_SysWMinfo * info)
icculus@9
  2215
{
icculus@19
  2216
    return SDL_GetWindowWMInfo(VideoWindow20, info);
icculus@9
  2217
}
icculus@9
  2218
icculus@9
  2219
struct private_yuvhwdata
icculus@9
  2220
{
icculus@9
  2221
    SDL_SW_YUVTexture *texture;
icculus@9
  2222
    SDL_Surface *display;
icculus@9
  2223
    Uint32 display_format;
icculus@9
  2224
};
icculus@9
  2225
icculus@35
  2226
DECLSPEC SDL_Overlay * SDLCALL
icculus@35
  2227
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL12_Surface *display)
icculus@9
  2228
{
icculus@9
  2229
    SDL_Overlay *overlay;
icculus@9
  2230
    Uint32 texture_format;
icculus@9
  2231
    SDL_SW_YUVTexture *texture;
icculus@9
  2232
icculus@9
  2233
    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
icculus@34
  2234
        SDL20_SetError("YUV overlays are not supported in OpenGL mode");
icculus@9
  2235
        return NULL;
icculus@9
  2236
    }
icculus@9
  2237
icculus@16
  2238
    if (display != PublicSurface) {
icculus@34
  2239
        SDL20_SetError("YUV display is only supported on the screen surface");
icculus@9
  2240
        return NULL;
icculus@9
  2241
    }
icculus@9
  2242
icculus@9
  2243
    switch (format) {
icculus@9
  2244
    case SDL_YV12_OVERLAY:
icculus@9
  2245
        texture_format = SDL_PIXELFORMAT_YV12;
icculus@9
  2246
        break;
icculus@9
  2247
    case SDL_IYUV_OVERLAY:
icculus@9
  2248
        texture_format = SDL_PIXELFORMAT_IYUV;
icculus@9
  2249
        break;
icculus@9
  2250
    case SDL_YUY2_OVERLAY:
icculus@9
  2251
        texture_format = SDL_PIXELFORMAT_YUY2;
icculus@9
  2252
        break;
icculus@9
  2253
    case SDL_UYVY_OVERLAY:
icculus@9
  2254
        texture_format = SDL_PIXELFORMAT_UYVY;
icculus@9
  2255
        break;
icculus@9
  2256
    case SDL_YVYU_OVERLAY:
icculus@9
  2257
        texture_format = SDL_PIXELFORMAT_YVYU;
icculus@9
  2258
        break;
icculus@9
  2259
    default:
icculus@34
  2260
        SDL20_SetError("Unknown YUV format");
icculus@9
  2261
        return NULL;
icculus@9
  2262
    }
icculus@9
  2263
icculus@31
  2264
    overlay = (SDL_Overlay *) SDL20_malloc(sizeof(*overlay));
icculus@9
  2265
    if (!overlay) {
icculus@31
  2266
        SDL20_OutOfMemory();
icculus@9
  2267
        return NULL;
icculus@9
  2268
    }
icculus@34
  2269
    SDL20_zerop(overlay);
icculus@9
  2270
icculus@9
  2271
    overlay->hwdata =
icculus@31
  2272
        (struct private_yuvhwdata *) SDL20_malloc(sizeof(*overlay->hwdata));
icculus@9
  2273
    if (!overlay->hwdata) {
icculus@31
  2274
        SDL20_free(overlay);
icculus@31
  2275
        SDL20_OutOfMemory();
icculus@9
  2276
        return NULL;
icculus@9
  2277
    }
icculus@9
  2278
icculus@9
  2279
    texture = SDL_SW_CreateYUVTexture(texture_format, w, h);
icculus@9
  2280
    if (!texture) {
icculus@31
  2281
        SDL20_free(overlay->hwdata);
icculus@31
  2282
        SDL20_free(overlay);
icculus@9
  2283
        return NULL;
icculus@9
  2284
    }
icculus@9
  2285
    overlay->hwdata->texture = texture;
icculus@9
  2286
    overlay->hwdata->display = NULL;
icculus@9
  2287
    overlay->hwdata->display_format = SDL_PIXELFORMAT_UNKNOWN;
icculus@9
  2288
icculus@9
  2289
    overlay->format = format;
icculus@9
  2290
    overlay->w = w;
icculus@9
  2291
    overlay->h = h;
icculus@9
  2292
    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
icculus@9
  2293
        overlay->planes = 3;
icculus@9
  2294
    } else {
icculus@9
  2295
        overlay->planes = 1;
icculus@9
  2296
    }
icculus@9
  2297
    overlay->pitches = texture->pitches;
icculus@9
  2298
    overlay->pixels = texture->planes;
icculus@9
  2299
icculus@9
  2300
    return overlay;
icculus@9
  2301
}
icculus@9
  2302
icculus@35
  2303
DECLSPEC int SDLCALL
icculus@9
  2304
SDL_LockYUVOverlay(SDL_Overlay * overlay)
icculus@9
  2305
{
icculus@9
  2306
    SDL_Rect rect;
icculus@9
  2307
    void *pixels;
icculus@9
  2308
    int pitch;
icculus@9
  2309
icculus@9
  2310
    if (!overlay) {
icculus@34
  2311
        return SDL20_SetError("Passed a NULL overlay");
icculus@9
  2312
    }
icculus@9
  2313
icculus@9
  2314
    rect.x = 0;
icculus@9
  2315
    rect.y = 0;
icculus@9
  2316
    rect.w = overlay->w;
icculus@9
  2317
    rect.h = overlay->h;
icculus@9
  2318
icculus@9
  2319
    if (SDL_SW_LockYUVTexture(overlay->hwdata->texture, &rect, &pixels, &pitch) < 0) {
icculus@9
  2320
        return -1;
icculus@9
  2321
    }
icculus@9
  2322
icculus@9
  2323
    overlay->pixels[0] = (Uint8 *) pixels;
icculus@9
  2324
    overlay->pitches[0] = pitch;
icculus@9
  2325
    switch (overlay->format) {
icculus@9
  2326
    case SDL_YV12_OVERLAY:
icculus@9
  2327
    case SDL_IYUV_OVERLAY:
icculus@9
  2328
        overlay->pitches[1] = pitch / 2;
icculus@9
  2329
        overlay->pitches[2] = pitch / 2;
icculus@9
  2330
        overlay->pixels[1] =
icculus@9
  2331
            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
icculus@9
  2332
        overlay->pixels[2] =
icculus@9
  2333
            overlay->pixels[1] + overlay->pitches[1] * overlay->h / 2;
icculus@9
  2334
        break;
icculus@9
  2335
    case SDL_YUY2_OVERLAY:
icculus@9
  2336
    case SDL_UYVY_OVERLAY:
icculus@9
  2337
    case SDL_YVYU_OVERLAY:
icculus@9
  2338
        break;
icculus@9
  2339
    }
icculus@9
  2340
    return 0;
icculus@9
  2341
}
icculus@9
  2342
icculus@35
  2343
DECLSPEC void SDLCALL
icculus@9
  2344
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
icculus@9
  2345
{
icculus@9
  2346
    if (!overlay) {
icculus@9
  2347
        return;
icculus@9
  2348
    }
icculus@9
  2349
icculus@9
  2350
    SDL_SW_UnlockYUVTexture(overlay->hwdata->texture);
icculus@9
  2351
}
icculus@9
  2352
icculus@35
  2353
DECLSPEC int SDLCALL
icculus@9
  2354
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
icculus@9
  2355
{
icculus@9
  2356
    SDL_Surface *display;
icculus@9
  2357
    SDL_Rect src_rect;
icculus@9
  2358
    SDL_Rect dst_rect;
icculus@9
  2359
    void *pixels;
icculus@9
  2360
icculus@9
  2361
    if (!overlay || !dstrect) {
icculus@34
  2362
        return SDL20_SetError("Passed a NULL overlay or dstrect");
icculus@9
  2363
    }
icculus@9
  2364
icculus@9
  2365
    display = overlay->hwdata->display;
icculus@16
  2366
    if (display != VideoSurface) {
icculus@16
  2367
        overlay->hwdata->display = display = VideoSurface;
icculus@9
  2368
        overlay->hwdata->display_format = SDL_MasksToPixelFormatEnum(
icculus@9
  2369
                                                display->format->BitsPerPixel,
icculus@9
  2370
                                                display->format->Rmask,
icculus@9
  2371
                                                display->format->Gmask,
icculus@9
  2372
                                                display->format->Bmask,
icculus@9
  2373
                                                display->format->Amask);
icculus@9
  2374
    }
icculus@9
  2375
icculus@9
  2376
    src_rect.x = 0;
icculus@9
  2377
    src_rect.y = 0;
icculus@9
  2378
    src_rect.w = overlay->w;
icculus@9
  2379
    src_rect.h = overlay->h;
icculus@9
  2380
icculus@9
  2381
    if (!SDL_IntersectRect(&display->clip_rect, dstrect, &dst_rect)) {
icculus@9
  2382
        return 0;
icculus@9
  2383
    }
icculus@9
  2384
     
icculus@9
  2385
    pixels = (void *)((Uint8 *)display->pixels +
icculus@9
  2386
                        dst_rect.y * display->pitch +
icculus@9
  2387
                        dst_rect.x * display->format->BytesPerPixel);
icculus@9
  2388
icculus@9
  2389
    if (SDL_SW_CopyYUVToRGB(overlay->hwdata->texture, &src_rect,
icculus@9
  2390
                            overlay->hwdata->display_format,
icculus@9
  2391
                            dst_rect.w, dst_rect.h,
icculus@9
  2392
                            pixels, display->pitch) < 0) {
icculus@9
  2393
        return -1;
icculus@9
  2394
    }
icculus@19
  2395
    SDL_UpdateWindowSurface(VideoWindow20);
icculus@9
  2396
    return 0;
icculus@9
  2397
}
icculus@9
  2398
icculus@35
  2399
DECLSPEC void SDLCALL
icculus@9
  2400
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
icculus@9
  2401
{
icculus@9
  2402
    if (!overlay) {
icculus@9
  2403
        return;
icculus@9
  2404
    }
icculus@9
  2405
    if (overlay->hwdata) {
icculus@9
  2406
        if (overlay->hwdata->texture) {
icculus@9
  2407
            SDL_SW_DestroyYUVTexture(overlay->hwdata->texture);
icculus@9
  2408
        }
icculus@31
  2409
        SDL20_free(overlay->hwdata);
icculus@9
  2410
    }
icculus@31
  2411
    SDL20_free(overlay);
icculus@9
  2412
}
icculus@34
  2413
#endif
icculus@9
  2414
icculus@35
  2415
DECLSPEC int SDLCALL
icculus@30
  2416
SDL_GL_SetAttribute(SDL12_GLattr attr, int value)
icculus@30
  2417
{
icculus@30
  2418
    if (attr >= SDL12_GL_MAX_ATTRIBUTE)
icculus@34
  2419
        return SDL20_SetError("Unknown GL attribute");
icculus@30
  2420
icculus@30
  2421
    /* swap control was moved out of this API, everything else lines up. */
icculus@30
  2422
    if (attr == SDL12_GL_SWAP_CONTROL)
icculus@30
  2423
    {
icculus@30
  2424
        SwapInterval = value;
icculus@30
  2425
        return 0;
icculus@30
  2426
    }
icculus@30
  2427
icculus@30
  2428
    return SDL20_GL_SetAttribute((SDL_GLattr) attr, value);
icculus@30
  2429
}
icculus@30
  2430
icculus@35
  2431
DECLSPEC int SDLCALL
icculus@30
  2432
SDL_GL_GetAttribute(SDL12_GLattr attr, int* value)
icculus@30
  2433
{
icculus@30
  2434
    if (attr >= SDL12_GL_MAX_ATTRIBUTE)
icculus@34
  2435
        return SDL20_SetError("Unknown GL attribute");
icculus@30
  2436
icculus@30
  2437
    /* swap control was moved out of this API, everything else lines up. */
icculus@30
  2438
    if (attr == SDL12_GL_SWAP_CONTROL)
icculus@30
  2439
    {
icculus@30
  2440
        *value = SDL20_GL_GetSwapInterval();
icculus@30
  2441
        return 0;
icculus@30
  2442
    }
icculus@30
  2443
icculus@30
  2444
    return SDL20_GL_GetAttribute((SDL_GLattr) attr, value);
icculus@30
  2445
}
icculus@30
  2446
icculus@30
  2447
icculus@35
  2448
DECLSPEC void SDLCALL
icculus@9
  2449
SDL_GL_SwapBuffers(void)
icculus@9
  2450
{
icculus@34
  2451
    if (VideoWindow20)
icculus@34
  2452
        SDL20_GL_SwapWindow(VideoWindow20);
icculus@9
  2453
}
icculus@9
  2454
icculus@35
  2455
DECLSPEC int SDLCALL
icculus@9
  2456
SDL_SetGamma(float red, float green, float blue)
icculus@9
  2457
{
icculus@9
  2458
    Uint16 red_ramp[256];
icculus@9
  2459
    Uint16 green_ramp[256];
icculus@9
  2460
    Uint16 blue_ramp[256];
icculus@9
  2461
icculus@34
  2462
    SDL20_CalculateGammaRamp(red, red_ramp);
icculus@9
  2463
    if (green == red) {
icculus@34
  2464
        SDL20_memcpy(green_ramp, red_ramp, sizeof(red_ramp));
icculus@9
  2465
    } else {
icculus@34
  2466
        SDL20_CalculateGammaRamp(green, green_ramp);
icculus@9
  2467
    }
icculus@9
  2468
    if (blue == red) {
icculus@34
  2469
        SDL20_memcpy(blue_ramp, red_ramp, sizeof(red_ramp));
icculus@34
  2470
    } else if (blue == green) {
icculus@34
  2471
        SDL20_memcpy(blue_ramp, green_ramp, sizeof(green_ramp));
icculus@9
  2472
    } else {
icculus@34
  2473
        SDL20_CalculateGammaRamp(blue, blue_ramp);
icculus@9
  2474
    }
icculus@34
  2475
    return SDL20_SetWindowGammaRamp(VideoWindow20, red_ramp, green_ramp, blue_ramp);
icculus@9
  2476
}
icculus@9
  2477
icculus@35
  2478
DECLSPEC int SDLCALL
icculus@34
  2479
SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue)
icculus@9
  2480
{
icculus@34
  2481
    return SDL20_SetWindowGammaRamp(VideoWindow20, red, green, blue);
icculus@9
  2482
}
icculus@9
  2483
icculus@35
  2484
DECLSPEC int SDLCALL
icculus@34
  2485
SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
icculus@9
  2486
{
icculus@34
  2487
    return SDL20_GetWindowGammaRamp(VideoWindow20, red, green, blue);
icculus@9
  2488
}
icculus@9
  2489
icculus@35
  2490
DECLSPEC int SDLCALL
icculus@9
  2491
SDL_EnableKeyRepeat(int delay, int interval)
icculus@9
  2492
{
icculus@9
  2493
    return 0;
icculus@9
  2494
}
icculus@9
  2495
icculus@35
  2496
DECLSPEC void SDLCALL
icculus@9
  2497
SDL_GetKeyRepeat(int *delay, int *interval)
icculus@9
  2498
{
icculus@9
  2499
    if (delay) {
icculus@34
  2500
        *delay = SDL12_DEFAULT_REPEAT_DELAY;
icculus@9
  2501
    }
icculus@9
  2502
    if (interval) {
icculus@34
  2503
        *interval = SDL12_DEFAULT_REPEAT_INTERVAL;
icculus@9
  2504
    }
icculus@9
  2505
}
icculus@9
  2506
icculus@34
  2507
#if SANITY_CHECK_THIS_CODE
icculus@35
  2508
DECLSPEC int SDLCALL
icculus@9
  2509
SDL_EnableUNICODE(int enable)
icculus@9
  2510
{
icculus@16
  2511
    int previous = EnabledUnicode;
icculus@9
  2512
icculus@9
  2513
    switch (enable) {
icculus@9
  2514
    case 1:
icculus@16
  2515
        EnabledUnicode = 1;
icculus@34
  2516
        SDL20_StartTextInput();
icculus@9
  2517
        break;
icculus@9
  2518
    case 0:
icculus@16
  2519
        EnabledUnicode = 0;
icculus@34
  2520
        SDL20_StopTextInput();
icculus@9
  2521
        break;
icculus@9
  2522
    }
icculus@9
  2523
    return previous;
icculus@9
  2524
}
icculus@34
  2525
#endif
icculus@9
  2526
icculus@9
  2527
static Uint32
icculus@34
  2528
SetTimerOld_Callback(Uint32 interval, void* param)
icculus@9
  2529
{
icculus@31
  2530
    return ((SDL12_TimerCallback)param)(interval);
icculus@9
  2531
}
icculus@9
  2532
icculus@35
  2533
DECLSPEC int SDLCALL
icculus@31
  2534
SDL_SetTimer(Uint32 interval, SDL12_TimerCallback callback)
icculus@9
  2535
{
icculus@9
  2536
    static SDL_TimerID compat_timer;
icculus@9
  2537
icculus@9
  2538
    if (compat_timer) {
icculus@10
  2539
        SDL20_RemoveTimer(compat_timer);
icculus@9
  2540
        compat_timer = 0;
icculus@9
  2541
    }
icculus@9
  2542
icculus@9
  2543
    if (interval && callback) {
icculus@34
  2544
        compat_timer = SDL20_AddTimer(interval, SetTimerOld_Callback, callback);
icculus@9
  2545
        if (!compat_timer) {
icculus@9
  2546
            return -1;
icculus@9
  2547
        }
icculus@9
  2548
    }
icculus@9
  2549
    return 0;
icculus@9
  2550
}
icculus@9
  2551
icculus@35
  2552
DECLSPEC int SDLCALL
icculus@9
  2553
SDL_putenv(const char *_var)
icculus@9
  2554
{
icculus@9
  2555
    char *ptr = NULL;
icculus@34
  2556
    char *var = SDL20_strdup(_var);
icculus@9
  2557
    if (var == NULL) {
icculus@9
  2558
        return -1;  /* we don't set errno. */
icculus@9
  2559
    }
icculus@9
  2560
icculus@34
  2561
    ptr = SDL20_strchr(var, '=');
icculus@9
  2562
    if (ptr == NULL) {
icculus@31
  2563
        SDL20_free(var);
icculus@9
  2564
        return -1;
icculus@9
  2565
    }
icculus@9
  2566
icculus@9
  2567
    *ptr = '\0';  /* split the string into name and value. */
icculus@34
  2568
    SDL20_setenv(var, ptr + 1, 1);
icculus@31
  2569
    SDL20_free(var);
icculus@9
  2570
    return 0;
icculus@9
  2571
}
icculus@9
  2572
icculus@9
  2573
icculus@9
  2574
icculus@9
  2575
/* CD-ROM support is gone from SDL 2.0, so just have stubs that fail. */
icculus@9
  2576
icculus@9
  2577
typedef void *SDL12_CD;  /* close enough.  :) */
icculus@9
  2578
typedef int SDL12_CDstatus;  /* close enough.  :) */
icculus@9
  2579
icculus@35
  2580
DECLSPEC int SDLCALL
icculus@9
  2581
SDL_CDNumDrives(void)
icculus@9
  2582
{
icculus@9
  2583
    return 0;  /* !!! FIXME: should return -1 without SDL_INIT_CDROM */
icculus@9
  2584
}
icculus@9
  2585
icculus@35
  2586
DECLSPEC const char *SDLCALL SDL_CDName(int drive) { SDL20_Unsupported(); return NULL; }
icculus@35
  2587
DECLSPEC SDL12_CD *SDLCALL SDL_CDOpen(int drive) { SDL20_Unsupported(); return NULL; }
icculus@35
  2588
DECLSPEC SDL12_CDstatus SDLCALL SDL_CDStatus(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2589
DECLSPEC int SDLCALL SDL_CDPlayTracks(SDL12_CD *cdrom, int start_track, int start_frame, int ntracks, int nframes) { return SDL20_Unsupported(); }
icculus@35
  2590
DECLSPEC int SDLCALL SDL_CDPlay(SDL12_CD *cdrom, int start, int length) { return SDL20_Unsupported(); }
icculus@35
  2591
DECLSPEC int SDLCALL SDL_CDPause(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2592
DECLSPEC int SDLCALL SDL_CDResume(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2593
DECLSPEC int SDLCALL SDL_CDStop(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2594
DECLSPEC int SDLCALL SDL_CDEject(SDL12_CD *cdrom) { return SDL20_Unsupported(); }
icculus@35
  2595
DECLSPEC void SDLCALL SDL_CDClose(SDL12_CD *cdrom) {}
icculus@9
  2596
icculus@26
  2597
icculus@26
  2598
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
icculus@35
  2599
DECLSPEC SDL_Thread * SDLCALL
icculus@26
  2600
SDL_CreateThread(int (SDLCALL *fn)(void *), void *data, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
icculus@26
  2601
{
icculus@26
  2602
    return SDL20_CreateThread(fn, NULL, data, pfnBeginThread, pfnEndThread);
icculus@26
  2603
}
icculus@26
  2604
#else
icculus@35
  2605
DECLSPEC SDL_Thread * SDLCALL
icculus@26
  2606
SDL_CreateThread(int (SDLCALL *fn)(void *), void *data)
icculus@26
  2607
{
icculus@26
  2608
    return SDL20_CreateThread(fn, NULL, data);
icculus@26
  2609
}
icculus@26
  2610
#endif
icculus@26
  2611
icculus@26
  2612
icculus@9
  2613
/* !!! FIXME: Removed from 2.0; do nothing. We can't even report failure. */
icculus@35
  2614
DECLSPEC void SDLCALL SDL_KillThread(SDL_Thread *thread) {}
icculus@9
  2615
icculus@10
  2616
/* This changed from an opaque pointer to an int in 2.0. */
icculus@10
  2617
typedef struct _SDL12_TimerID *SDL12_TimerID;
icculus@10
  2618
SDL_COMPILE_TIME_ASSERT(timer, sizeof(SDL12_TimerID) >= sizeof(SDL_TimerID));
icculus@10
  2619
icculus@31
  2620
icculus@35
  2621
DECLSPEC SDL12_TimerID SDLCALL
icculus@31
  2622
SDL_AddTimer(Uint32 interval, SDL12_NewTimerCallback callback, void *param)
icculus@10
  2623
{
icculus@10
  2624
    return (SDL12_TimerID) ((size_t) SDL20_AddTimer(interval, callback, param));
icculus@10
  2625
}
icculus@10
  2626
icculus@35
  2627
DECLSPEC SDL_bool SDLCALL
icculus@10
  2628
SDL_RemoveTimer(SDL12_TimerID id)
icculus@10
  2629
{
icculus@10
  2630
    return SDL20_RemoveTimer((SDL_TimerID) ((size_t)id));
icculus@10
  2631
}
icculus@10
  2632
icculus@10
  2633
icculus@10
  2634
typedef struct SDL12_RWops {
icculus@31
  2635
    int (SDLCALL *seek)(struct SDL12_RWops *context, int offset, int whence);
icculus@31
  2636
    int (SDLCALL *read)(struct SDL12_RWops *context, void *ptr, int size, int maxnum);
icculus@31
  2637
    int (SDLCALL *write)(struct SDL12_RWops *context, const void *ptr, int size, int num);
icculus@31
  2638
    int (SDLCALL *close)(struct SDL12_RWops *context);
icculus@10
  2639
    Uint32 type;
icculus@10
  2640
    void *padding[8];
icculus@10
  2641
    SDL_RWops *rwops20;
icculus@10
  2642
} SDL12_RWops;
icculus@10
  2643
icculus@10
  2644
icculus@35
  2645
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2646
SDL_AllocRW(void)
icculus@10
  2647
{
icculus@31
  2648
    SDL12_RWops *rwops = (SDL12_RWops *) SDL20_malloc(sizeof (SDL12_RWops));
icculus@10
  2649
    if (!rwops)
icculus@10
  2650
        SDL20_OutOfMemory();
icculus@10
  2651
    return rwops;
icculus@10
  2652
}
icculus@10
  2653
icculus@35
  2654
DECLSPEC void SDLCALL
icculus@10
  2655
SDL_FreeRW(SDL12_RWops *rwops12)
icculus@10
  2656
{
icculus@31
  2657
    SDL20_free(rwops12);
icculus@10
  2658
}
icculus@10
  2659
icculus@10
  2660
static int SDLCALL
icculus@31
  2661
RWops20to12_seek(struct SDL12_RWops *rwops12, int offset, int whence)
icculus@10
  2662
{
icculus@10
  2663
    return rwops12->rwops20->seek(rwops12->rwops20, offset, whence);
icculus@10
  2664
}
icculus@10
  2665
icculus@10
  2666
static int SDLCALL
icculus@31
  2667
RWops20to12_read(struct SDL12_RWops *rwops12, void *ptr, int size, int maxnum)
icculus@10
  2668
{
icculus@10
  2669
    return rwops12->rwops20->read(rwops12->rwops20, ptr, size, maxnum);
icculus@10
  2670
}
icculus@10
  2671
icculus@10
  2672
static int SDLCALL
icculus@31
  2673
RWops20to12_write(struct SDL12_RWops *rwops12, const void *ptr, int size, int num)
icculus@10
  2674
{
icculus@10
  2675
    return rwops12->rwops20->write(rwops12->rwops20, ptr, size, num);
icculus@10
  2676
}
icculus@10
  2677
icculus@10
  2678
static int SDLCALL
icculus@31
  2679
RWops20to12_close(struct SDL12_RWops *rwops12)
icculus@10
  2680
{
icculus@10
  2681
    int rc = 0;
icculus@10
  2682
    if (rwops12)
icculus@10
  2683
    {
icculus@10
  2684
        rc = rwops12->rwops20->close(rwops12->rwops20);
icculus@10
  2685
        if (rc == 0)
icculus@10
  2686
            SDL_FreeRW(rwops12);
icculus@10
  2687
    }
icculus@10
  2688
    return rc;
icculus@10
  2689
}
icculus@10
  2690
icculus@10
  2691
static SDL12_RWops *
icculus@31
  2692
RWops20to12(SDL_RWops *rwops20)
icculus@10
  2693
{
icculus@31
  2694
    SDL12_RWops *rwops12;
icculus@31
  2695
icculus@10
  2696
    if (!rwops20)
icculus@10
  2697
        return NULL;
icculus@31
  2698
icculus@31
  2699
    rwops12 = SDL_AllocRW();
icculus@31
  2700
    if (!rwops12)
icculus@31
  2701
        return NULL;
icculus@31
  2702
icculus@34
  2703
    SDL20_zerop(rwops12);
icculus@10
  2704
    rwops12->type = rwops20->type;
icculus@10
  2705
    rwops12->rwops20 = rwops20;
icculus@31
  2706
    rwops12->seek = RWops20to12_seek;
icculus@31
  2707
    rwops12->read = RWops20to12_read;
icculus@31
  2708
    rwops12->write = RWops20to12_write;
icculus@31
  2709
    rwops12->close = RWops20to12_close;
icculus@31
  2710
icculus@12
  2711
    return rwops12;
icculus@10
  2712
}
icculus@10
  2713
icculus@35
  2714
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2715
SDL_RWFromFile(const char *file, const char *mode)
icculus@10
  2716
{
icculus@31
  2717
    return RWops20to12(SDL20_RWFromFile(file, mode));
icculus@10
  2718
}
icculus@10
  2719
icculus@35
  2720
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2721
SDL_RWFromFP(FILE *io, int autoclose)
icculus@10
  2722
{
icculus@31
  2723
    return RWops20to12(SDL20_RWFromFP(io, autoclose));
icculus@10
  2724
}
icculus@10
  2725
icculus@35
  2726
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2727
SDL_RWFromMem(void *mem, int size)
icculus@10
  2728
{
icculus@31
  2729
    return RWops20to12(SDL20_RWFromMem(mem, size));
icculus@10
  2730
}
icculus@10
  2731
icculus@35
  2732
DECLSPEC SDL12_RWops * SDLCALL
icculus@10
  2733
SDL_RWFromConstMem(const void *mem, int size)