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