src/SDL12_compat.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 13 Apr 2013 23:38:00 -0400
changeset 34 946cbc18ba19
parent 33 4e9cde983cdc
child 35 365748d8ff6c
permissions -rw-r--r--
More work on pushing this through compiler/linker.

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