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