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