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