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