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