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