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