src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 07 Mar 2013 01:57:55 -0500
changeset 11 ed314328463b
parent 10 e6176c053187
child 12 f9cfb7f12b95
permissions -rw-r--r--
sdl12-compat: filled in more functions.

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