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