src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 13 Feb 2019 08:33:29 -0500
changeset 54 3511abf3a80d
parent 53 1c23821d3299
child 55 852e5d3808f2
permissions -rw-r--r--
Cleaned up most of the FIXME reporting.

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