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