src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 07 Mar 2013 22:34:19 -0500
changeset 16 ad3e9ebcfd90
parent 15 fd1b88f919e7
child 17 473158daa1de
permissions -rw-r--r--
Make the static variables not be prefixed with "SDL_"

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