src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 21:13:55 -0800
changeset 5169 ededa1ccf91c
parent 5167 88c656ffea44
child 5171 34e2d5115786
permissions -rw-r--r--
Switched the SDL 1.2 compatibility to use the window surface, so it's fast even when there's no hardware acceleration available.
This means that the YUV overlay now uses software, but that's okay since fast YUV code should be using the textures now anyway.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
/* This file contains functions for backwards compatibility with SDL 1.2 */
slouken@1895
    25
slouken@1895
    26
#include "SDL.h"
slouken@1895
    27
#include "SDL_syswm.h"
slouken@1895
    28
slouken@1895
    29
#include "video/SDL_sysvideo.h"
slouken@1895
    30
#include "video/SDL_pixels_c.h"
slouken@5169
    31
#include "render/SDL_yuv_sw_c.h"
slouken@5169
    32
slouken@1895
    33
slouken@3685
    34
static SDL_Window *SDL_VideoWindow = NULL;
bob@2328
    35
static SDL_Surface *SDL_VideoSurface = NULL;
bob@2328
    36
static SDL_Surface *SDL_ShadowSurface = NULL;
bob@2328
    37
static SDL_Surface *SDL_PublicSurface = NULL;
bob@2328
    38
static SDL_GLContext *SDL_VideoContext = NULL;
slouken@2829
    39
static Uint32 SDL_VideoFlags = 0;
bob@2328
    40
static char *wm_title = NULL;
slouken@2967
    41
static SDL_Surface *SDL_VideoIcon;
slouken@3280
    42
static int SDL_enabled_UNICODE = 0;
slouken@1895
    43
slouken@5169
    44
const char *
slouken@1895
    45
SDL_AudioDriverName(char *namebuf, int maxlen)
slouken@1895
    46
{
slouken@1895
    47
    const char *name = SDL_GetCurrentAudioDriver();
slouken@1895
    48
    if (name) {
slouken@5169
    49
        if (namebuf) {
slouken@5169
    50
            SDL_strlcpy(namebuf, name, maxlen);
slouken@5169
    51
            return namebuf;
slouken@5169
    52
        } else {
slouken@5169
    53
            return name;
slouken@5169
    54
        }
slouken@1895
    55
    }
slouken@1895
    56
    return NULL;
slouken@1895
    57
}
slouken@1895
    58
slouken@5169
    59
const char *
slouken@1895
    60
SDL_VideoDriverName(char *namebuf, int maxlen)
slouken@1895
    61
{
slouken@1895
    62
    const char *name = SDL_GetCurrentVideoDriver();
slouken@1895
    63
    if (name) {
slouken@5167
    64
        if (namebuf) {
slouken@5167
    65
            SDL_strlcpy(namebuf, name, maxlen);
slouken@5167
    66
            return namebuf;
slouken@5167
    67
        } else {
slouken@5167
    68
            return name;
slouken@5167
    69
        }
slouken@1895
    70
    }
slouken@1895
    71
    return NULL;
slouken@1895
    72
}
slouken@1895
    73
slouken@3518
    74
static void
slouken@3518
    75
SelectVideoDisplay()
slouken@3518
    76
{
slouken@3518
    77
    const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
slouken@3518
    78
    if ( !variable ) {
slouken@3518
    79
        variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
slouken@3518
    80
    }
slouken@3518
    81
    if ( variable ) {
slouken@3518
    82
        SDL_SelectVideoDisplay(SDL_atoi(variable));
slouken@3518
    83
    }
slouken@3518
    84
}
slouken@3518
    85
slouken@1895
    86
const SDL_VideoInfo *
slouken@1895
    87
SDL_GetVideoInfo(void)
slouken@1895
    88
{
slouken@1895
    89
    static SDL_VideoInfo info;
slouken@1967
    90
    SDL_DisplayMode mode;
slouken@1895
    91
slouken@3518
    92
    SelectVideoDisplay();
slouken@3518
    93
slouken@1895
    94
    /* Memory leak, compatibility code, who cares? */
slouken@1967
    95
    if (!info.vfmt && SDL_GetDesktopDisplayMode(&mode) == 0) {
slouken@1895
    96
        int bpp;
slouken@1895
    97
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
    98
slouken@1967
    99
        SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask,
slouken@1967
   100
                                   &Amask);
slouken@1895
   101
        info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2862
   102
        info.current_w = mode.w;
slouken@2862
   103
        info.current_h = mode.h;
slouken@1895
   104
    }
slouken@1895
   105
    return &info;
slouken@1895
   106
}
slouken@1895
   107
slouken@1895
   108
int
slouken@1895
   109
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
slouken@1895
   110
{
slouken@1895
   111
    int i, actual_bpp = 0;
slouken@1895
   112
slouken@1895
   113
    if (!SDL_GetVideoDevice()) {
slouken@1895
   114
        return 0;
slouken@1895
   115
    }
slouken@1895
   116
slouken@3518
   117
    SelectVideoDisplay();
slouken@3518
   118
slouken@1895
   119
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1967
   120
        SDL_DisplayMode mode;
slouken@1967
   121
        SDL_GetDesktopDisplayMode(&mode);
slouken@1967
   122
        return SDL_BITSPERPIXEL(mode.format);
slouken@1895
   123
    }
slouken@1895
   124
slouken@1895
   125
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1967
   126
        SDL_DisplayMode mode;
slouken@1967
   127
        SDL_GetDisplayMode(i, &mode);
slouken@1967
   128
        if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
slouken@1967
   129
            if (!mode.format) {
slouken@1895
   130
                return bpp;
slouken@1895
   131
            }
slouken@1967
   132
            if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
slouken@1967
   133
                actual_bpp = SDL_BITSPERPIXEL(mode.format);
slouken@1895
   134
            }
slouken@1895
   135
        }
slouken@1895
   136
    }
slouken@1895
   137
    return actual_bpp;
slouken@1895
   138
}
slouken@1895
   139
slouken@1895
   140
SDL_Rect **
slouken@3034
   141
SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
slouken@1895
   142
{
slouken@1895
   143
    int i, nmodes;
slouken@1895
   144
    SDL_Rect **modes;
slouken@1895
   145
slouken@1895
   146
    if (!SDL_GetVideoDevice()) {
slouken@1895
   147
        return NULL;
slouken@1895
   148
    }
slouken@1895
   149
slouken@3518
   150
    SelectVideoDisplay();
slouken@3518
   151
slouken@1895
   152
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1895
   153
        return (SDL_Rect **) (-1);
slouken@1895
   154
    }
slouken@1895
   155
slouken@3034
   156
    if (!format) {
slouken@3034
   157
        format = SDL_GetVideoInfo()->vfmt;
slouken@3034
   158
    }
slouken@3034
   159
slouken@1895
   160
    /* Memory leak, but this is a compatibility function, who cares? */
slouken@1895
   161
    nmodes = 0;
slouken@1895
   162
    modes = NULL;
slouken@1895
   163
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1967
   164
        SDL_DisplayMode mode;
slouken@5043
   165
        int bpp;
slouken@5043
   166
slouken@1967
   167
        SDL_GetDisplayMode(i, &mode);
slouken@1967
   168
        if (!mode.w || !mode.h) {
slouken@1895
   169
            return (SDL_Rect **) (-1);
slouken@1895
   170
        }
slouken@5043
   171
        
slouken@5043
   172
        /* Copied from src/video/SDL_pixels.c:SDL_PixelFormatEnumToMasks */
slouken@5043
   173
        if (SDL_BYTESPERPIXEL(mode.format) <= 2) {
slouken@5043
   174
            bpp = SDL_BITSPERPIXEL(mode.format);
slouken@5043
   175
        } else {
slouken@5043
   176
            bpp = SDL_BYTESPERPIXEL(mode.format) * 8;
slouken@5043
   177
        }
slouken@5043
   178
slouken@5043
   179
        if (bpp != format->BitsPerPixel) {
slouken@1895
   180
            continue;
slouken@1895
   181
        }
slouken@1967
   182
        if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
slouken@1967
   183
            && modes[nmodes - 1]->h == mode.h) {
slouken@1895
   184
            continue;
slouken@1895
   185
        }
slouken@1895
   186
slouken@1895
   187
        modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
slouken@1895
   188
        if (!modes) {
slouken@1895
   189
            return NULL;
slouken@1895
   190
        }
slouken@1895
   191
        modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
slouken@1895
   192
        if (!modes[nmodes]) {
slouken@1895
   193
            return NULL;
slouken@1895
   194
        }
slouken@1895
   195
        modes[nmodes]->x = 0;
slouken@1895
   196
        modes[nmodes]->y = 0;
slouken@1967
   197
        modes[nmodes]->w = mode.w;
slouken@1967
   198
        modes[nmodes]->h = mode.h;
slouken@1895
   199
        ++nmodes;
slouken@1895
   200
    }
slouken@1895
   201
    if (modes) {
slouken@1895
   202
        modes[nmodes] = NULL;
slouken@1895
   203
    }
slouken@1895
   204
    return modes;
slouken@1895
   205
}
slouken@1895
   206
slouken@1895
   207
static int
slouken@1895
   208
SDL_CompatEventFilter(void *userdata, SDL_Event * event)
slouken@1895
   209
{
slouken@1895
   210
    SDL_Event fake;
slouken@1895
   211
slouken@1895
   212
    switch (event->type) {
slouken@1895
   213
    case SDL_WINDOWEVENT:
slouken@1895
   214
        switch (event->window.event) {
slouken@1895
   215
        case SDL_WINDOWEVENT_EXPOSED:
slouken@4429
   216
            if (!SDL_HasEvent(SDL_VIDEOEXPOSE)) {
slouken@1895
   217
                fake.type = SDL_VIDEOEXPOSE;
slouken@1895
   218
                SDL_PushEvent(&fake);
slouken@1895
   219
            }
slouken@1895
   220
            break;
slouken@1895
   221
        case SDL_WINDOWEVENT_RESIZED:
slouken@4429
   222
            SDL_FlushEvent(SDL_VIDEORESIZE);
slouken@1895
   223
            fake.type = SDL_VIDEORESIZE;
slouken@1895
   224
            fake.resize.w = event->window.data1;
slouken@1895
   225
            fake.resize.h = event->window.data2;
slouken@1895
   226
            SDL_PushEvent(&fake);
slouken@1895
   227
            break;
slouken@1895
   228
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1895
   229
            fake.type = SDL_ACTIVEEVENT;
slouken@1895
   230
            fake.active.gain = 0;
slouken@1895
   231
            fake.active.state = SDL_APPACTIVE;
slouken@1895
   232
            SDL_PushEvent(&fake);
slouken@1895
   233
            break;
slouken@1895
   234
        case SDL_WINDOWEVENT_RESTORED:
slouken@1895
   235
            fake.type = SDL_ACTIVEEVENT;
slouken@1895
   236
            fake.active.gain = 1;
slouken@1895
   237
            fake.active.state = SDL_APPACTIVE;
slouken@1895
   238
            SDL_PushEvent(&fake);
slouken@1895
   239
            break;
slouken@1895
   240
        case SDL_WINDOWEVENT_ENTER:
slouken@1895
   241
            fake.type = SDL_ACTIVEEVENT;
slouken@1895
   242
            fake.active.gain = 1;
slouken@1895
   243
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1895
   244
            SDL_PushEvent(&fake);
slouken@1895
   245
            break;
slouken@1895
   246
        case SDL_WINDOWEVENT_LEAVE:
slouken@1895
   247
            fake.type = SDL_ACTIVEEVENT;
slouken@1895
   248
            fake.active.gain = 0;
slouken@1895
   249
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1895
   250
            SDL_PushEvent(&fake);
slouken@1895
   251
            break;
slouken@1895
   252
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1895
   253
            fake.type = SDL_ACTIVEEVENT;
slouken@1895
   254
            fake.active.gain = 1;
slouken@1895
   255
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1895
   256
            SDL_PushEvent(&fake);
slouken@1895
   257
            break;
slouken@1895
   258
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1895
   259
            fake.type = SDL_ACTIVEEVENT;
slouken@2058
   260
            fake.active.gain = 0;
slouken@1895
   261
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1895
   262
            SDL_PushEvent(&fake);
slouken@1895
   263
            break;
slouken@1895
   264
        case SDL_WINDOWEVENT_CLOSE:
slouken@1895
   265
            fake.type = SDL_QUIT;
slouken@1895
   266
            SDL_PushEvent(&fake);
slouken@1895
   267
            break;
slouken@1895
   268
        }
slouken@1895
   269
    case SDL_KEYDOWN:
slouken@1895
   270
    case SDL_KEYUP:
slouken@1895
   271
        {
slouken@1895
   272
            Uint32 unicode = 0;
slouken@1895
   273
            if (event->key.type == SDL_KEYDOWN && event->key.keysym.sym < 256) {
slouken@1895
   274
                unicode = event->key.keysym.sym;
slouken@1895
   275
                if (unicode >= 'a' && unicode <= 'z') {
slouken@1895
   276
                    int shifted = !!(event->key.keysym.mod & KMOD_SHIFT);
slouken@1895
   277
                    int capslock = !!(event->key.keysym.mod & KMOD_CAPS);
slouken@1895
   278
                    if ((shifted ^ capslock) != 0) {
slouken@1895
   279
                        unicode = SDL_toupper(unicode);
slouken@1895
   280
                    }
slouken@1895
   281
                }
slouken@1895
   282
            }
slouken@1895
   283
            if (unicode) {
slouken@1895
   284
                event->key.keysym.unicode = unicode;
slouken@1895
   285
            }
slouken@1895
   286
            break;
slouken@1895
   287
        }
slouken@2129
   288
    case SDL_TEXTINPUT:
slouken@2129
   289
        {
slouken@2129
   290
            /* FIXME: Generate an old style key repeat event if needed */
slouken@2130
   291
            //printf("TEXTINPUT: '%s'\n", event->text.text);
slouken@2129
   292
            break;
slouken@2129
   293
        }
slouken@1895
   294
    case SDL_MOUSEWHEEL:
slouken@1895
   295
        {
slouken@1895
   296
            Uint8 button;
slouken@1895
   297
            int x, y;
slouken@1895
   298
slouken@2153
   299
            if (event->wheel.y == 0) {
slouken@2153
   300
                break;
slouken@2153
   301
            }
slouken@2153
   302
icculus@3585
   303
            SDL_GetMouseState(&x, &y);
slouken@1895
   304
slouken@2153
   305
            if (event->wheel.y > 0) {
slouken@2153
   306
                button = SDL_BUTTON_WHEELUP;
slouken@2153
   307
            } else {
slouken@2153
   308
                button = SDL_BUTTON_WHEELDOWN;
slouken@2153
   309
            }
slouken@2153
   310
slouken@2153
   311
            fake.button.button = button;
slouken@1895
   312
            fake.button.x = x;
slouken@1895
   313
            fake.button.y = y;
slouken@1895
   314
            fake.button.windowID = event->wheel.windowID;
slouken@1895
   315
slouken@2153
   316
            fake.type = SDL_MOUSEBUTTONDOWN;
slouken@2153
   317
            fake.button.state = SDL_PRESSED;
slouken@2153
   318
            SDL_PushEvent(&fake);
slouken@1895
   319
slouken@2153
   320
            fake.type = SDL_MOUSEBUTTONUP;
slouken@2153
   321
            fake.button.state = SDL_RELEASED;
slouken@2153
   322
            SDL_PushEvent(&fake);
slouken@1895
   323
            break;
slouken@1895
   324
        }
slouken@1895
   325
slouken@1895
   326
    }
slouken@1895
   327
    return 1;
slouken@1895
   328
}
slouken@1895
   329
slouken@1895
   330
static void
slouken@1895
   331
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
slouken@1895
   332
{
slouken@1895
   333
    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
slouken@1895
   334
    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
slouken@1895
   335
    if (window) {
slouken@1895
   336
        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
slouken@1895
   337
            return;
slouken@1895
   338
        }
slouken@1895
   339
        if (SDL_strcmp(window, "center") == 0) {
slouken@1895
   340
            center = window;
slouken@1895
   341
        }
slouken@1895
   342
    }
slouken@1895
   343
    if (center) {
slouken@1967
   344
        SDL_DisplayMode mode;
slouken@1967
   345
        SDL_GetDesktopDisplayMode(&mode);
slouken@1967
   346
        *x = (mode.w - w) / 2;
slouken@1967
   347
        *y = (mode.h - h) / 2;
slouken@1895
   348
    }
slouken@1895
   349
}
slouken@1895
   350
slouken@2829
   351
static void
slouken@2829
   352
ClearVideoSurface()
slouken@2829
   353
{
slouken@2829
   354
    Uint32 black;
slouken@2829
   355
slouken@2829
   356
    /* Clear the surface for display */
slouken@2829
   357
    black = SDL_MapRGB(SDL_PublicSurface->format, 0, 0, 0);
slouken@2829
   358
    SDL_FillRect(SDL_PublicSurface, NULL, black);
slouken@2829
   359
    SDL_UpdateRect(SDL_PublicSurface, 0, 0, 0, 0);
slouken@2829
   360
}
slouken@2829
   361
slouken@3025
   362
static void
slouken@3028
   363
SetupScreenSaver(int flags)
slouken@3025
   364
{
slouken@3025
   365
    const char *env;
slouken@3025
   366
    SDL_bool allow_screensaver;
slouken@3025
   367
slouken@3028
   368
    /* Allow environment override of screensaver disable */
slouken@3028
   369
    env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
slouken@3028
   370
    if (env) {
slouken@3028
   371
        allow_screensaver = SDL_atoi(env) ? SDL_TRUE : SDL_FALSE;
slouken@3028
   372
    } else if (flags & SDL_FULLSCREEN) {
slouken@3028
   373
        allow_screensaver = SDL_FALSE;
slouken@3028
   374
    } else {
slouken@3028
   375
        allow_screensaver = SDL_TRUE;
slouken@3028
   376
    }
slouken@3025
   377
    if (allow_screensaver) {
slouken@3025
   378
        SDL_EnableScreenSaver();
slouken@3025
   379
    } else {
slouken@3025
   380
        SDL_DisableScreenSaver();
slouken@3025
   381
    }
slouken@3025
   382
}
slouken@3025
   383
slouken@4472
   384
static int
slouken@2829
   385
SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
slouken@2829
   386
{
slouken@2829
   387
    int w, h;
slouken@2829
   388
    Uint32 format;
slouken@2829
   389
    int access;
slouken@2829
   390
slouken@2829
   391
    /* We can't resize something we don't have... */
slouken@2829
   392
    if (!SDL_VideoWindow) {
slouken@2829
   393
        return -1;
slouken@2829
   394
    }
slouken@2829
   395
slouken@2829
   396
    /* We probably have to recreate the window in fullscreen mode */
slouken@2829
   397
    if (flags & SDL_FULLSCREEN) {
slouken@2829
   398
        return -1;
slouken@2829
   399
    }
slouken@2829
   400
slouken@2829
   401
    /* I don't think there's any change we can gracefully make in flags */
slouken@2829
   402
    if (flags != SDL_VideoFlags) {
slouken@2829
   403
        return -1;
slouken@2829
   404
    }
slouken@2829
   405
slouken@2829
   406
    /* Resize the window */
slouken@2829
   407
    SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
slouken@2829
   408
    if (w != width || h != height) {
slouken@2829
   409
        SDL_SetWindowSize(SDL_VideoWindow, width, height);
slouken@2829
   410
    }
slouken@2829
   411
slouken@2829
   412
    /* If we're in OpenGL mode, just resize the stub surface and we're done! */
slouken@2829
   413
    if (flags & SDL_OPENGL) {
slouken@2829
   414
        SDL_VideoSurface->w = width;
slouken@2829
   415
        SDL_VideoSurface->h = height;
slouken@2829
   416
        return 0;
slouken@2829
   417
    }
slouken@2829
   418
slouken@2829
   419
    /* Destroy the screen texture and recreate it */
slouken@5169
   420
    SDL_VideoSurface = SDL_GetWindowSurface(SDL_VideoWindow);
slouken@5169
   421
    if (!SDL_VideoSurface) {
slouken@2829
   422
        return -1;
slouken@2829
   423
    }
slouken@2829
   424
slouken@2829
   425
    if (SDL_ShadowSurface) {
slouken@2829
   426
        SDL_ShadowSurface->w = width;
slouken@2829
   427
        SDL_ShadowSurface->h = height;
slouken@2831
   428
        SDL_ShadowSurface->pitch = SDL_CalculatePitch(SDL_ShadowSurface);
slouken@2829
   429
        SDL_ShadowSurface->pixels =
slouken@2829
   430
            SDL_realloc(SDL_ShadowSurface->pixels,
slouken@2829
   431
                        SDL_ShadowSurface->h * SDL_ShadowSurface->pitch);
slouken@2829
   432
        SDL_SetClipRect(SDL_ShadowSurface, NULL);
slouken@2831
   433
        SDL_InvalidateMap(SDL_ShadowSurface->map);
slouken@5169
   434
    } else {
slouken@5169
   435
        SDL_PublicSurface = SDL_VideoSurface;
slouken@2829
   436
    }
slouken@2829
   437
slouken@2829
   438
    ClearVideoSurface();
slouken@2829
   439
slouken@2829
   440
    return 0;
slouken@2829
   441
}
slouken@2829
   442
slouken@1895
   443
SDL_Surface *
slouken@1895
   444
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
slouken@1895
   445
{
slouken@1967
   446
    SDL_DisplayMode desktop_mode;
slouken@1895
   447
    int window_x = SDL_WINDOWPOS_UNDEFINED;
slouken@1895
   448
    int window_y = SDL_WINDOWPOS_UNDEFINED;
slouken@1895
   449
    Uint32 window_flags;
slouken@5159
   450
    Uint32 surface_flags;
slouken@5159
   451
    Uint32 i;
slouken@1895
   452
slouken@1895
   453
    if (!SDL_GetVideoDevice()) {
slouken@1895
   454
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
slouken@1895
   455
            return NULL;
slouken@1895
   456
        }
slouken@1895
   457
    }
slouken@3139
   458
slouken@3518
   459
    SelectVideoDisplay();
slouken@3518
   460
slouken@3098
   461
    SDL_GetDesktopDisplayMode(&desktop_mode);
slouken@3098
   462
slouken@3098
   463
    if (width == 0) {
slouken@3098
   464
        width = desktop_mode.w;
slouken@3098
   465
    }
slouken@3098
   466
    if (height == 0) {
slouken@3098
   467
        height = desktop_mode.h;
slouken@3098
   468
    }
slouken@5159
   469
    if (bpp == 0) {
slouken@5159
   470
        bpp = SDL_BITSPERPIXEL(desktop_mode.format);
slouken@5159
   471
    }
slouken@1895
   472
slouken@2829
   473
    /* See if we can simply resize the existing window and surface */
slouken@2829
   474
    if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
slouken@2829
   475
        return SDL_PublicSurface;
slouken@2829
   476
    }
slouken@2829
   477
slouken@1895
   478
    /* Destroy existing window */
slouken@1895
   479
    SDL_PublicSurface = NULL;
slouken@1895
   480
    if (SDL_ShadowSurface) {
slouken@1895
   481
        SDL_FreeSurface(SDL_ShadowSurface);
slouken@1895
   482
        SDL_ShadowSurface = NULL;
slouken@1895
   483
    }
slouken@1895
   484
    if (SDL_VideoSurface) {
slouken@1895
   485
        SDL_FreeSurface(SDL_VideoSurface);
slouken@1895
   486
        SDL_VideoSurface = NULL;
slouken@1895
   487
    }
slouken@1912
   488
    if (SDL_VideoContext) {
bob@2328
   489
        /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
slouken@1912
   490
        SDL_GL_DeleteContext(SDL_VideoContext);
slouken@1912
   491
        SDL_VideoContext = NULL;
slouken@1912
   492
    }
slouken@1895
   493
    if (SDL_VideoWindow) {
slouken@1895
   494
        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
bob@2322
   495
        SDL_DestroyWindow(SDL_VideoWindow);
slouken@1895
   496
    }
slouken@1895
   497
slouken@1895
   498
    /* Set up the event filter */
slouken@1895
   499
    if (!SDL_GetEventFilter(NULL, NULL)) {
slouken@1895
   500
        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
slouken@1895
   501
    }
slouken@1895
   502
slouken@1895
   503
    /* Create a new window */
slouken@1895
   504
    window_flags = SDL_WINDOW_SHOWN;
slouken@1895
   505
    if (flags & SDL_FULLSCREEN) {
slouken@1895
   506
        window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1895
   507
    }
slouken@1895
   508
    if (flags & SDL_OPENGL) {
slouken@1895
   509
        window_flags |= SDL_WINDOW_OPENGL;
slouken@1895
   510
    }
slouken@1895
   511
    if (flags & SDL_RESIZABLE) {
slouken@1895
   512
        window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   513
    }
slouken@1895
   514
    if (flags & SDL_NOFRAME) {
slouken@1895
   515
        window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   516
    }
slouken@1895
   517
    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
slouken@1895
   518
    SDL_VideoWindow =
slouken@1895
   519
        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
slouken@1895
   520
                         window_flags);
slouken@1895
   521
    if (!SDL_VideoWindow) {
slouken@1895
   522
        return NULL;
slouken@1895
   523
    }
slouken@2967
   524
    SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon);
slouken@1895
   525
slouken@1895
   526
    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1895
   527
    surface_flags = 0;
slouken@1895
   528
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   529
        surface_flags |= SDL_FULLSCREEN;
slouken@1895
   530
    }
slouken@1895
   531
    if (window_flags & SDL_WINDOW_OPENGL) {
slouken@1895
   532
        surface_flags |= SDL_OPENGL;
slouken@1895
   533
    }
slouken@1895
   534
    if (window_flags & SDL_WINDOW_RESIZABLE) {
slouken@1895
   535
        surface_flags |= SDL_RESIZABLE;
slouken@1895
   536
    }
slouken@1895
   537
    if (window_flags & SDL_WINDOW_BORDERLESS) {
slouken@1895
   538
        surface_flags |= SDL_NOFRAME;
slouken@1895
   539
    }
slouken@1895
   540
slouken@1895
   541
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
slouken@1895
   542
    if (flags & SDL_OPENGL) {
slouken@1912
   543
        SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
slouken@1912
   544
        if (!SDL_VideoContext) {
slouken@1912
   545
            return NULL;
slouken@1912
   546
        }
slouken@1912
   547
        if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
slouken@1912
   548
            return NULL;
slouken@1912
   549
        }
slouken@1895
   550
        SDL_VideoSurface =
slouken@1895
   551
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
slouken@1895
   552
        if (!SDL_VideoSurface) {
slouken@1895
   553
            return NULL;
slouken@1895
   554
        }
slouken@1895
   555
        SDL_VideoSurface->flags |= surface_flags;
slouken@1895
   556
        SDL_PublicSurface = SDL_VideoSurface;
slouken@1895
   557
        return SDL_PublicSurface;
slouken@1895
   558
    }
slouken@1895
   559
slouken@1895
   560
    /* Create the screen surface */
slouken@5169
   561
    SDL_VideoSurface = SDL_GetWindowSurface(SDL_VideoWindow);
slouken@1895
   562
    if (!SDL_VideoSurface) {
slouken@1895
   563
        return NULL;
slouken@1895
   564
    }
slouken@1895
   565
    SDL_VideoSurface->flags |= surface_flags;
slouken@1895
   566
slouken@1895
   567
    /* Create a shadow surface if necessary */
slouken@2222
   568
    if ((bpp != SDL_VideoSurface->format->BitsPerPixel)
slouken@2222
   569
        && !(flags & SDL_ANYFORMAT)) {
slouken@2222
   570
        SDL_ShadowSurface =
slouken@2222
   571
            SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
slouken@1895
   572
        if (!SDL_ShadowSurface) {
slouken@1895
   573
            return NULL;
slouken@1895
   574
        }
slouken@1895
   575
        SDL_ShadowSurface->flags |= surface_flags;
slouken@1895
   576
slouken@1895
   577
        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
slouken@1895
   578
        if (SDL_ShadowSurface->format->palette) {
slouken@1895
   579
            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
slouken@5149
   580
            SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
slouken@5149
   581
                             SDL_ShadowSurface->format->BitsPerPixel);
slouken@1895
   582
        }
slouken@1895
   583
    }
slouken@1895
   584
    SDL_PublicSurface =
slouken@1895
   585
        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
slouken@1895
   586
slouken@2829
   587
    SDL_VideoFlags = flags;
slouken@2829
   588
slouken@2829
   589
    ClearVideoSurface();
slouken@1895
   590
slouken@3028
   591
    SetupScreenSaver(flags);
slouken@3025
   592
slouken@1895
   593
    /* We're finally done! */
slouken@1895
   594
    return SDL_PublicSurface;
slouken@1895
   595
}
slouken@1895
   596
slouken@1895
   597
SDL_Surface *
slouken@1895
   598
SDL_GetVideoSurface(void)
slouken@1895
   599
{
slouken@1895
   600
    return SDL_PublicSurface;
slouken@1895
   601
}
slouken@1895
   602
slouken@2266
   603
int
slouken@2266
   604
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
slouken@2266
   605
{
slouken@2838
   606
    if (flag & SDL_SRCALPHA) {
slouken@2838
   607
        /* According to the docs, value is ignored for alpha surfaces */
slouken@2838
   608
        if (surface->format->Amask) {
slouken@2838
   609
            value = 0xFF;
slouken@2838
   610
        }
slouken@2266
   611
        SDL_SetSurfaceAlphaMod(surface, value);
slouken@2884
   612
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2266
   613
    } else {
slouken@2266
   614
        SDL_SetSurfaceAlphaMod(surface, 0xFF);
slouken@2884
   615
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
slouken@2266
   616
    }
slouken@2838
   617
    SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
slouken@2838
   618
slouken@2266
   619
    return 0;
slouken@2266
   620
}
slouken@2266
   621
slouken@1895
   622
SDL_Surface *
slouken@1895
   623
SDL_DisplayFormat(SDL_Surface * surface)
slouken@1895
   624
{
slouken@2807
   625
    SDL_PixelFormat *format;
slouken@1895
   626
slouken@1895
   627
    if (!SDL_PublicSurface) {
slouken@1895
   628
        SDL_SetError("No video mode has been set");
slouken@1895
   629
        return NULL;
slouken@1895
   630
    }
slouken@2807
   631
    format = SDL_PublicSurface->format;
slouken@1895
   632
slouken@1895
   633
    /* Set the flags appropriate for copying to display surface */
slouken@2807
   634
    return SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
slouken@1895
   635
}
slouken@1895
   636
slouken@1895
   637
SDL_Surface *
slouken@1895
   638
SDL_DisplayFormatAlpha(SDL_Surface * surface)
slouken@1895
   639
{
slouken@1895
   640
    SDL_PixelFormat *vf;
slouken@1895
   641
    SDL_PixelFormat *format;
slouken@1895
   642
    SDL_Surface *converted;
slouken@1895
   643
    /* default to ARGB8888 */
slouken@1895
   644
    Uint32 amask = 0xff000000;
slouken@1895
   645
    Uint32 rmask = 0x00ff0000;
slouken@1895
   646
    Uint32 gmask = 0x0000ff00;
slouken@1895
   647
    Uint32 bmask = 0x000000ff;
slouken@1895
   648
slouken@1895
   649
    if (!SDL_PublicSurface) {
slouken@1895
   650
        SDL_SetError("No video mode has been set");
slouken@1895
   651
        return NULL;
slouken@1895
   652
    }
slouken@1895
   653
    vf = SDL_PublicSurface->format;
slouken@1895
   654
slouken@1895
   655
    switch (vf->BytesPerPixel) {
slouken@1895
   656
    case 2:
slouken@1895
   657
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1895
   658
           For anything else (like ARGB4444) it doesn't matter
slouken@1895
   659
           since we have no special code for it anyway */
slouken@1895
   660
        if ((vf->Rmask == 0x1f) &&
slouken@1895
   661
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1895
   662
            rmask = 0xff;
slouken@1895
   663
            bmask = 0xff0000;
slouken@1895
   664
        }
slouken@1895
   665
        break;
slouken@1895
   666
slouken@1895
   667
    case 3:
slouken@1895
   668
    case 4:
slouken@1895
   669
        /* Keep the video format, as long as the high 8 bits are
slouken@1895
   670
           unused or alpha */
slouken@1895
   671
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1895
   672
            rmask = 0xff;
slouken@1895
   673
            bmask = 0xff0000;
slouken@1895
   674
        }
slouken@1895
   675
        break;
slouken@1895
   676
slouken@1895
   677
    default:
slouken@1895
   678
        /* We have no other optimised formats right now. When/if a new
slouken@1895
   679
           optimised alpha format is written, add the converter here */
slouken@1895
   680
        break;
slouken@1895
   681
    }
slouken@1895
   682
    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
slouken@2807
   683
    converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
slouken@1895
   684
    SDL_FreeFormat(format);
slouken@1895
   685
    return converted;
slouken@1895
   686
}
slouken@1895
   687
slouken@1895
   688
int
slouken@1895
   689
SDL_Flip(SDL_Surface * screen)
slouken@1895
   690
{
slouken@1895
   691
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1895
   692
    return 0;
slouken@1895
   693
}
slouken@1895
   694
slouken@1895
   695
void
slouken@1895
   696
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@1895
   697
{
slouken@1895
   698
    if (screen) {
slouken@1895
   699
        SDL_Rect rect;
slouken@1895
   700
slouken@1895
   701
        /* Fill the rectangle */
slouken@1897
   702
        rect.x = (int) x;
slouken@1897
   703
        rect.y = (int) y;
slouken@1897
   704
        rect.w = (int) (w ? w : screen->w);
slouken@1897
   705
        rect.h = (int) (h ? h : screen->h);
slouken@1895
   706
        SDL_UpdateRects(screen, 1, &rect);
slouken@1895
   707
    }
slouken@1895
   708
}
slouken@2735
   709
slouken@1895
   710
void
slouken@1895
   711
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@1895
   712
{
slouken@1895
   713
    int i;
slouken@5142
   714
    SDL_Rect rect;
slouken@1895
   715
slouken@1895
   716
    if (screen == SDL_ShadowSurface) {
slouken@1895
   717
        for (i = 0; i < numrects; ++i) {
slouken@1895
   718
            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
slouken@1895
   719
                          &rects[i]);
slouken@1895
   720
        }
slouken@1895
   721
slouken@1895
   722
        /* Fall through to video surface update */
slouken@1895
   723
        screen = SDL_VideoSurface;
slouken@1895
   724
    }
slouken@1895
   725
    if (screen == SDL_VideoSurface) {
slouken@5169
   726
        SDL_UpdateWindowSurfaceRects(SDL_VideoWindow, numrects, rects);
slouken@1895
   727
    }
slouken@1895
   728
}
slouken@1895
   729
slouken@1895
   730
void
slouken@1895
   731
SDL_WM_SetCaption(const char *title, const char *icon)
slouken@1895
   732
{
slouken@1895
   733
    if (wm_title) {
slouken@1895
   734
        SDL_free(wm_title);
slouken@2037
   735
    }
slouken@2037
   736
    if (title) {
slouken@2037
   737
        wm_title = SDL_strdup(title);
slouken@1895
   738
    } else {
slouken@2037
   739
        wm_title = NULL;
slouken@1895
   740
    }
slouken@1895
   741
    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
slouken@1895
   742
}
slouken@1895
   743
slouken@1895
   744
void
slouken@2037
   745
SDL_WM_GetCaption(const char **title, const char **icon)
slouken@1895
   746
{
slouken@1895
   747
    if (title) {
slouken@1895
   748
        *title = wm_title;
slouken@1895
   749
    }
slouken@1895
   750
    if (icon) {
slouken@1895
   751
        *icon = "";
slouken@1895
   752
    }
slouken@1895
   753
}
slouken@1895
   754
slouken@1895
   755
void
slouken@1895
   756
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@1895
   757
{
slouken@2967
   758
    SDL_VideoIcon = icon;
slouken@1895
   759
}
slouken@1895
   760
slouken@1895
   761
int
slouken@1895
   762
SDL_WM_IconifyWindow(void)
slouken@1895
   763
{
slouken@1895
   764
    SDL_MinimizeWindow(SDL_VideoWindow);
slouken@1895
   765
    return 0;
slouken@1895
   766
}
slouken@1895
   767
slouken@1895
   768
int
slouken@1895
   769
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
slouken@1895
   770
{
slouken@1895
   771
    if (SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   772
        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 0) < 0) {
slouken@1895
   773
            return 0;
slouken@1895
   774
        }
slouken@1895
   775
        SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
slouken@1895
   776
    } else {
slouken@1895
   777
        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 1) < 0) {
slouken@1895
   778
            return 0;
slouken@1895
   779
        }
slouken@1895
   780
        SDL_PublicSurface->flags |= SDL_FULLSCREEN;
slouken@1895
   781
    }
slouken@1895
   782
    return 1;
slouken@1895
   783
}
slouken@1895
   784
slouken@1895
   785
SDL_GrabMode
slouken@1895
   786
SDL_WM_GrabInput(SDL_GrabMode mode)
slouken@1895
   787
{
slouken@1895
   788
    if (mode != SDL_GRAB_QUERY) {
slouken@1895
   789
        SDL_SetWindowGrab(SDL_VideoWindow, mode);
slouken@1895
   790
    }
slouken@1895
   791
    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
slouken@1895
   792
}
slouken@1895
   793
slouken@1895
   794
void
slouken@1895
   795
SDL_WarpMouse(Uint16 x, Uint16 y)
slouken@1895
   796
{
slouken@1895
   797
    SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
slouken@1895
   798
}
slouken@1895
   799
slouken@1895
   800
Uint8
slouken@1895
   801
SDL_GetAppState(void)
slouken@1895
   802
{
slouken@1895
   803
    Uint8 state = 0;
slouken@1895
   804
    Uint32 flags = 0;
slouken@1895
   805
slouken@1895
   806
    flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1895
   807
    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
slouken@1895
   808
        state |= SDL_APPACTIVE;
slouken@1895
   809
    }
slouken@1895
   810
    if (flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   811
        state |= SDL_APPINPUTFOCUS;
slouken@1895
   812
    }
slouken@1895
   813
    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   814
        state |= SDL_APPMOUSEFOCUS;
slouken@1895
   815
    }
slouken@1895
   816
    return state;
slouken@1895
   817
}
slouken@1895
   818
slouken@1895
   819
const SDL_version *
slouken@1895
   820
SDL_Linked_Version(void)
slouken@1895
   821
{
slouken@1895
   822
    static SDL_version version;
slouken@1895
   823
    SDL_VERSION(&version);
slouken@1895
   824
    return &version;
slouken@1895
   825
}
slouken@1895
   826
slouken@1895
   827
int
slouken@1895
   828
SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
slouken@1895
   829
               int firstcolor, int ncolors)
slouken@1895
   830
{
slouken@1895
   831
    return SDL_SetColors(surface, colors, firstcolor, ncolors);
slouken@1895
   832
}
slouken@1895
   833
slouken@1895
   834
int
slouken@1895
   835
SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
slouken@1895
   836
              int ncolors)
slouken@1895
   837
{
slouken@1895
   838
    if (SDL_SetPaletteColors
slouken@1895
   839
        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
slouken@1895
   840
        return 1;
slouken@1895
   841
    } else {
slouken@1895
   842
        return 0;
slouken@1895
   843
    }
slouken@1895
   844
}
slouken@1895
   845
slouken@1895
   846
int
slouken@1895
   847
SDL_GetWMInfo(SDL_SysWMinfo * info)
slouken@1895
   848
{
slouken@1895
   849
    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
slouken@1895
   850
}
slouken@1895
   851
slouken@1895
   852
#if 0
slouken@1895
   853
void
slouken@1895
   854
SDL_MoveCursor(int x, int y)
slouken@1895
   855
{
slouken@1895
   856
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   857
slouken@1895
   858
    /* Erase and update the current mouse position */
slouken@1895
   859
    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1895
   860
        /* Erase and redraw mouse cursor in new position */
slouken@1895
   861
        SDL_LockCursor();
slouken@1895
   862
        SDL_EraseCursor(SDL_VideoSurface);
slouken@1895
   863
        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@1895
   864
        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@1895
   865
        SDL_DrawCursor(SDL_VideoSurface);
slouken@1895
   866
        SDL_UnlockCursor();
slouken@1895
   867
    } else if (_this->MoveWMCursor) {
slouken@1895
   868
        _this->MoveWMCursor(_this, x, y);
slouken@1895
   869
    }
slouken@1895
   870
}
slouken@1895
   871
slouken@1895
   872
/* Keep track of the current cursor colors */
slouken@1895
   873
static int palette_changed = 1;
slouken@1895
   874
static Uint8 pixels8[2];
slouken@1895
   875
slouken@1895
   876
void
slouken@1895
   877
SDL_CursorPaletteChanged(void)
slouken@1895
   878
{
slouken@1895
   879
    palette_changed = 1;
slouken@1895
   880
}
slouken@1895
   881
slouken@1895
   882
void
slouken@1895
   883
SDL_MouseRect(SDL_Rect * area)
slouken@1895
   884
{
slouken@1895
   885
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   886
    int clip_diff;
slouken@1895
   887
slouken@1895
   888
    *area = SDL_cursor->area;
slouken@1895
   889
    if (area->x < 0) {
slouken@1895
   890
        area->w += area->x;
slouken@1895
   891
        area->x = 0;
slouken@1895
   892
    }
slouken@1895
   893
    if (area->y < 0) {
slouken@1895
   894
        area->h += area->y;
slouken@1895
   895
        area->y = 0;
slouken@1895
   896
    }
slouken@1895
   897
    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
slouken@1895
   898
    if (clip_diff > 0) {
slouken@1895
   899
        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
slouken@1895
   900
    }
slouken@1895
   901
    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
slouken@1895
   902
    if (clip_diff > 0) {
slouken@1895
   903
        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
slouken@1895
   904
    }
slouken@1895
   905
}
slouken@1895
   906
slouken@1895
   907
static void
slouken@1895
   908
SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
slouken@1895
   909
{
slouken@1895
   910
    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
slouken@1895
   911
    int i, w, h;
slouken@1895
   912
    Uint8 *data, datab;
slouken@1895
   913
    Uint8 *mask, maskb;
slouken@1895
   914
slouken@1895
   915
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1895
   916
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1895
   917
    switch (screen->format->BytesPerPixel) {
slouken@1895
   918
slouken@1895
   919
    case 1:
slouken@1895
   920
        {
slouken@1895
   921
            Uint8 *dst;
slouken@1895
   922
            int dstskip;
slouken@1895
   923
slouken@1895
   924
            if (palette_changed) {
slouken@1895
   925
                pixels8[0] =
slouken@1895
   926
                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1895
   927
                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1895
   928
                palette_changed = 0;
slouken@1895
   929
            }
slouken@1895
   930
            dst = (Uint8 *) screen->pixels +
slouken@1895
   931
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1895
   932
                SDL_cursor->area.x;
slouken@1895
   933
            dstskip = screen->pitch - area->w;
slouken@1895
   934
slouken@1895
   935
            for (h = area->h; h; h--) {
slouken@1895
   936
                for (w = area->w / 8; w; w--) {
slouken@1895
   937
                    maskb = *mask++;
slouken@1895
   938
                    datab = *data++;
slouken@1895
   939
                    for (i = 0; i < 8; ++i) {
slouken@1895
   940
                        if (maskb & 0x80) {
slouken@1895
   941
                            *dst = pixels8[datab >> 7];
slouken@1895
   942
                        }
slouken@1895
   943
                        maskb <<= 1;
slouken@1895
   944
                        datab <<= 1;
slouken@1895
   945
                        dst++;
slouken@1895
   946
                    }
slouken@1895
   947
                }
slouken@1895
   948
                dst += dstskip;
slouken@1895
   949
            }
slouken@1895
   950
        }
slouken@1895
   951
        break;
slouken@1895
   952
slouken@1895
   953
    case 2:
slouken@1895
   954
        {
slouken@1895
   955
            Uint16 *dst;
slouken@1895
   956
            int dstskip;
slouken@1895
   957
slouken@1895
   958
            dst = (Uint16 *) screen->pixels +
slouken@1895
   959
                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
slouken@1895
   960
                SDL_cursor->area.x;
slouken@1895
   961
            dstskip = (screen->pitch / 2) - area->w;
slouken@1895
   962
slouken@1895
   963
            for (h = area->h; h; h--) {
slouken@1895
   964
                for (w = area->w / 8; w; w--) {
slouken@1895
   965
                    maskb = *mask++;
slouken@1895
   966
                    datab = *data++;
slouken@1895
   967
                    for (i = 0; i < 8; ++i) {
slouken@1895
   968
                        if (maskb & 0x80) {
slouken@1895
   969
                            *dst = (Uint16) pixels[datab >> 7];
slouken@1895
   970
                        }
slouken@1895
   971
                        maskb <<= 1;
slouken@1895
   972
                        datab <<= 1;
slouken@1895
   973
                        dst++;
slouken@1895
   974
                    }
slouken@1895
   975
                }
slouken@1895
   976
                dst += dstskip;
slouken@1895
   977
            }
slouken@1895
   978
        }
slouken@1895
   979
        break;
slouken@1895
   980
slouken@1895
   981
    case 3:
slouken@1895
   982
        {
slouken@1895
   983
            Uint8 *dst;
slouken@1895
   984
            int dstskip;
slouken@1895
   985
slouken@1895
   986
            dst = (Uint8 *) screen->pixels +
slouken@1895
   987
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1895
   988
                SDL_cursor->area.x * 3;
slouken@1895
   989
            dstskip = screen->pitch - area->w * 3;
slouken@1895
   990
slouken@1895
   991
            for (h = area->h; h; h--) {
slouken@1895
   992
                for (w = area->w / 8; w; w--) {
slouken@1895
   993
                    maskb = *mask++;
slouken@1895
   994
                    datab = *data++;
slouken@1895
   995
                    for (i = 0; i < 8; ++i) {
slouken@1895
   996
                        if (maskb & 0x80) {
slouken@1895
   997
                            SDL_memset(dst, pixels[datab >> 7], 3);
slouken@1895
   998
                        }
slouken@1895
   999
                        maskb <<= 1;
slouken@1895
  1000
                        datab <<= 1;
slouken@1895
  1001
                        dst += 3;
slouken@1895
  1002
                    }
slouken@1895
  1003
                }
slouken@1895
  1004
                dst += dstskip;
slouken@1895
  1005
            }
slouken@1895
  1006
        }
slouken@1895
  1007
        break;
slouken@1895
  1008
slouken@1895
  1009
    case 4:
slouken@1895
  1010
        {
slouken@1895
  1011
            Uint32 *dst;
slouken@1895
  1012
            int dstskip;
slouken@1895
  1013
slouken@1895
  1014
            dst = (Uint32 *) screen->pixels +
slouken@1895
  1015
                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
slouken@1895
  1016
                SDL_cursor->area.x;
slouken@1895
  1017
            dstskip = (screen->pitch / 4) - area->w;
slouken@1895
  1018
slouken@1895
  1019
            for (h = area->h; h; h--) {
slouken@1895
  1020
                for (w = area->w / 8; w; w--) {
slouken@1895
  1021
                    maskb = *mask++;
slouken@1895
  1022
                    datab = *data++;
slouken@1895
  1023
                    for (i = 0; i < 8; ++i) {
slouken@1895
  1024
                        if (maskb & 0x80) {
slouken@1895
  1025
                            *dst = pixels[datab >> 7];
slouken@1895
  1026
                        }
slouken@1895
  1027
                        maskb <<= 1;
slouken@1895
  1028
                        datab <<= 1;
slouken@1895
  1029
                        dst++;
slouken@1895
  1030
                    }
slouken@1895
  1031
                }
slouken@1895
  1032
                dst += dstskip;
slouken@1895
  1033
            }
slouken@1895
  1034
        }
slouken@1895
  1035
        break;
slouken@1895
  1036
    }
slouken@1895
  1037
}
slouken@1895
  1038
slouken@1895
  1039
static void
slouken@1895
  1040
SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
slouken@1895
  1041
{
slouken@1895
  1042
    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
slouken@1895
  1043
    int h;
slouken@1895
  1044
    int x, minx, maxx;
slouken@1895
  1045
    Uint8 *data, datab = 0;
slouken@1895
  1046
    Uint8 *mask, maskb = 0;
slouken@1895
  1047
    Uint8 *dst;
slouken@1895
  1048
    int dstbpp, dstskip;
slouken@1895
  1049
slouken@1895
  1050
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1895
  1051
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1895
  1052
    dstbpp = screen->format->BytesPerPixel;
slouken@1895
  1053
    dst = (Uint8 *) screen->pixels +
slouken@1895
  1054
        (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1895
  1055
        SDL_cursor->area.x * dstbpp;
slouken@1895
  1056
    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
slouken@1895
  1057
slouken@1895
  1058
    minx = area->x;
slouken@1895
  1059
    maxx = area->x + area->w;
slouken@1895
  1060
    if (screen->format->BytesPerPixel == 1) {
slouken@1895
  1061
        if (palette_changed) {
slouken@1895
  1062
            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1895
  1063
            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1895
  1064
            palette_changed = 0;
slouken@1895
  1065
        }
slouken@1895
  1066
        for (h = area->h; h; h--) {
slouken@1895
  1067
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1895
  1068
                if ((x % 8) == 0) {
slouken@1895
  1069
                    maskb = *mask++;
slouken@1895
  1070
                    datab = *data++;
slouken@1895
  1071
                }
slouken@1895
  1072
                if ((x >= minx) && (x < maxx)) {
slouken@1895
  1073
                    if (maskb & 0x80) {
slouken@1895
  1074
                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
slouken@1895
  1075
                    }
slouken@1895
  1076
                }
slouken@1895
  1077
                maskb <<= 1;
slouken@1895
  1078
                datab <<= 1;
slouken@1895
  1079
                dst += dstbpp;
slouken@1895
  1080
            }
slouken@1895
  1081
            dst += dstskip;
slouken@1895
  1082
        }
slouken@1895
  1083
    } else {
slouken@1895
  1084
        for (h = area->h; h; h--) {
slouken@1895
  1085
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1895
  1086
                if ((x % 8) == 0) {
slouken@1895
  1087
                    maskb = *mask++;
slouken@1895
  1088
                    datab = *data++;
slouken@1895
  1089
                }
slouken@1895
  1090
                if ((x >= minx) && (x < maxx)) {
slouken@1895
  1091
                    if (maskb & 0x80) {
slouken@1895
  1092
                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
slouken@1895
  1093
                    }
slouken@1895
  1094
                }
slouken@1895
  1095
                maskb <<= 1;
slouken@1895
  1096
                datab <<= 1;
slouken@1895
  1097
                dst += dstbpp;
slouken@1895
  1098
            }
slouken@1895
  1099
            dst += dstskip;
slouken@1895
  1100
        }
slouken@1895
  1101
    }
slouken@1895
  1102
}
slouken@1895
  1103
slouken@1895
  1104
/* This handles the ugly work of converting the saved cursor background from
slouken@1895
  1105
   the pixel format of the shadow surface to that of the video surface.
slouken@1895
  1106
   This is only necessary when blitting from a shadow surface of a different
slouken@1895
  1107
   pixel format than the video surface, and using a software rendered cursor.
slouken@1895
  1108
*/
slouken@1895
  1109
static void
slouken@1895
  1110
SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
slouken@1895
  1111
{
slouken@1895
  1112
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1113
    SDL_BlitInfo info;
slouken@1895
  1114
    SDL_loblit RunBlit;
slouken@1895
  1115
slouken@1895
  1116
    /* Make sure we can steal the blit mapping */
slouken@1895
  1117
    if (screen->map->dst != SDL_VideoSurface) {
slouken@1895
  1118
        return;
slouken@1895
  1119
    }
slouken@1895
  1120
slouken@1895
  1121
    /* Set up the blit information */
slouken@1895
  1122
    info.s_pixels = SDL_cursor->save[1];
slouken@1895
  1123
    info.s_width = w;
slouken@1895
  1124
    info.s_height = h;
slouken@1895
  1125
    info.s_skip = 0;
slouken@1895
  1126
    info.d_pixels = SDL_cursor->save[0];
slouken@1895
  1127
    info.d_width = w;
slouken@1895
  1128
    info.d_height = h;
slouken@1895
  1129
    info.d_skip = 0;
slouken@1895
  1130
    info.aux_data = screen->map->sw_data->aux_data;
slouken@1895
  1131
    info.src = screen->format;
slouken@1895
  1132
    info.table = screen->map->table;
slouken@1895
  1133
    info.dst = SDL_VideoSurface->format;
slouken@1895
  1134
    RunBlit = screen->map->sw_data->blit;
slouken@1895
  1135
slouken@1895
  1136
    /* Run the actual software blit */
slouken@1895
  1137
    RunBlit(&info);
slouken@1895
  1138
}
slouken@1895
  1139
slouken@1895
  1140
void
slouken@1895
  1141
SDL_DrawCursorNoLock(SDL_Surface * screen)
slouken@1895
  1142
{
slouken@1895
  1143
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1144
    SDL_Rect area;
slouken@1895
  1145
slouken@1895
  1146
    /* Get the mouse rectangle, clipped to the screen */
slouken@1895
  1147
    SDL_MouseRect(&area);
slouken@1895
  1148
    if ((area.w == 0) || (area.h == 0)) {
slouken@1895
  1149
        return;
slouken@1895
  1150
    }
slouken@1895
  1151
slouken@1895
  1152
    /* Copy mouse background */
slouken@1895
  1153
    {
slouken@1895
  1154
        int w, h, screenbpp;
slouken@1895
  1155
        Uint8 *src, *dst;
slouken@1895
  1156
slouken@1895
  1157
        /* Set up the copy pointers */
slouken@1895
  1158
        screenbpp = screen->format->BytesPerPixel;
slouken@1895
  1159
        if ((screen == SDL_VideoSurface) ||
slouken@1895
  1160
            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
slouken@1895
  1161
            dst = SDL_cursor->save[0];
slouken@1895
  1162
        } else {
slouken@1895
  1163
            dst = SDL_cursor->save[1];
slouken@1895
  1164
        }
slouken@1895
  1165
        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1895
  1166
            area.x * screenbpp;
slouken@1895
  1167
slouken@1895
  1168
        /* Perform the copy */
slouken@1895
  1169
        w = area.w * screenbpp;
slouken@1895
  1170
        h = area.h;
slouken@1895
  1171
        while (h--) {
slouken@1895
  1172
            SDL_memcpy(dst, src, w);
slouken@1895
  1173
            dst += w;
slouken@1895
  1174
            src += screen->pitch;
slouken@1895
  1175
        }
slouken@1895
  1176
    }
slouken@1895
  1177
slouken@1895
  1178
    /* Draw the mouse cursor */
slouken@1895
  1179
    area.x -= SDL_cursor->area.x;
slouken@1895
  1180
    area.y -= SDL_cursor->area.y;
slouken@1895
  1181
    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
slouken@1895
  1182
        SDL_DrawCursorFast(screen, &area);
slouken@1895
  1183
    } else {
slouken@1895
  1184
        SDL_DrawCursorSlow(screen, &area);
slouken@1895
  1185
    }
slouken@1895
  1186
}
slouken@1895
  1187
slouken@1895
  1188
void
slouken@1895
  1189
SDL_DrawCursor(SDL_Surface * screen)
slouken@1895
  1190
{
slouken@1895
  1191
    /* Lock the screen if necessary */
slouken@1895
  1192
    if (screen == NULL) {
slouken@1895
  1193
        return;
slouken@1895
  1194
    }
slouken@1895
  1195
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1196
        if (SDL_LockSurface(screen) < 0) {
slouken@1895
  1197
            return;
slouken@1895
  1198
        }
slouken@1895
  1199
    }
slouken@1895
  1200
slouken@1895
  1201
    SDL_DrawCursorNoLock(screen);
slouken@1895
  1202
slouken@1895
  1203
    /* Unlock the screen and update if necessary */
slouken@1895
  1204
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1205
        SDL_UnlockSurface(screen);
slouken@1895
  1206
    }
slouken@2222
  1207
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1895
  1208
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1209
        SDL_Window *window;
slouken@1895
  1210
        SDL_Rect area;
slouken@1895
  1211
slouken@1895
  1212
        window = SDL_GetWindowFromSurface(screen);
slouken@1895
  1213
        if (!window) {
slouken@1895
  1214
            return;
slouken@1895
  1215
        }
slouken@1895
  1216
slouken@1895
  1217
        SDL_MouseRect(&area);
slouken@1895
  1218
slouken@1895
  1219
        if (_this->UpdateWindowSurface) {
slouken@1895
  1220
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1895
  1221
        }
slouken@1895
  1222
    }
slouken@1895
  1223
}
slouken@1895
  1224
slouken@1895
  1225
void
slouken@1895
  1226
SDL_EraseCursorNoLock(SDL_Surface * screen)
slouken@1895
  1227
{
slouken@1895
  1228
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1229
    SDL_Window *window;
slouken@1895
  1230
    SDL_Rect area;
slouken@1895
  1231
slouken@1895
  1232
    /* Get the window associated with the surface */
slouken@1895
  1233
    window = SDL_GetWindowFromSurface(screen);
slouken@1895
  1234
    if (!window || !window->surface) {
slouken@1895
  1235
        return;
slouken@1895
  1236
    }
slouken@1895
  1237
slouken@1895
  1238
    /* Get the mouse rectangle, clipped to the screen */
slouken@1895
  1239
    SDL_MouseRect(&area);
slouken@1895
  1240
    if ((area.w == 0) || (area.h == 0)) {
slouken@1895
  1241
        return;
slouken@1895
  1242
    }
slouken@1895
  1243
slouken@1895
  1244
    /* Copy mouse background */
slouken@1895
  1245
    {
slouken@1895
  1246
        int w, h, screenbpp;
slouken@1895
  1247
        Uint8 *src, *dst;
slouken@1895
  1248
slouken@1895
  1249
        /* Set up the copy pointers */
slouken@1895
  1250
        screenbpp = screen->format->BytesPerPixel;
slouken@1895
  1251
        if ((screen->flags & SDL_SCREEN_SURFACE) ||
slouken@1895
  1252
            FORMAT_EQUAL(screen->format, window->surface->format)) {
slouken@1895
  1253
            src = SDL_cursor->save[0];
slouken@1895
  1254
        } else {
slouken@1895
  1255
            src = SDL_cursor->save[1];
slouken@1895
  1256
        }
slouken@1895
  1257
        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1895
  1258
            area.x * screenbpp;
slouken@1895
  1259
slouken@1895
  1260
        /* Perform the copy */
slouken@1895
  1261
        w = area.w * screenbpp;
slouken@1895
  1262
        h = area.h;
slouken@1895
  1263
        while (h--) {
slouken@1895
  1264
            SDL_memcpy(dst, src, w);
slouken@1895
  1265
            src += w;
slouken@1895
  1266
            dst += screen->pitch;
slouken@1895
  1267
        }
slouken@1895
  1268
slouken@1895
  1269
        /* Perform pixel conversion on cursor background */
slouken@1895
  1270
        if (src > SDL_cursor->save[1]) {
slouken@1895
  1271
            SDL_ConvertCursorSave(screen, area.w, area.h);
slouken@1895
  1272
        }
slouken@1895
  1273
    }
slouken@1895
  1274
}
slouken@1895
  1275
slouken@1895
  1276
void
slouken@1895
  1277
SDL_EraseCursor(SDL_Surface * screen)
slouken@1895
  1278
{
slouken@1895
  1279
    /* Lock the screen if necessary */
slouken@1895
  1280
    if (screen == NULL) {
slouken@1895
  1281
        return;
slouken@1895
  1282
    }
slouken@1895
  1283
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1284
        if (SDL_LockSurface(screen) < 0) {
slouken@1895
  1285
            return;
slouken@1895
  1286
        }
slouken@1895
  1287
    }
slouken@1895
  1288
slouken@1895
  1289
    SDL_EraseCursorNoLock(screen);
slouken@1895
  1290
slouken@1895
  1291
    /* Unlock the screen and update if necessary */
slouken@1895
  1292
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1293
        SDL_UnlockSurface(screen);
slouken@1895
  1294
    }
slouken@2222
  1295
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1895
  1296
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1297
        SDL_Window *window;
slouken@1895
  1298
        SDL_Rect area;
slouken@1895
  1299
slouken@1895
  1300
        window = SDL_GetWindowFromSurface(screen);
slouken@1895
  1301
        if (!window) {
slouken@1895
  1302
            return;
slouken@1895
  1303
        }
slouken@1895
  1304
slouken@1895
  1305
        SDL_MouseRect(&area);
slouken@1895
  1306
slouken@1895
  1307
        if (_this->UpdateWindowSurface) {
slouken@1895
  1308
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1895
  1309
        }
slouken@1895
  1310
    }
slouken@1895
  1311
}
slouken@1895
  1312
slouken@1895
  1313
/* Reset the cursor on video mode change
slouken@1895
  1314
   FIXME:  Keep track of all cursors, and reset them all.
slouken@1895
  1315
 */
slouken@1895
  1316
void
slouken@1895
  1317
SDL_ResetCursor(void)
slouken@1895
  1318
{
slouken@1895
  1319
    int savelen;
slouken@1895
  1320
slouken@1895
  1321
    if (SDL_cursor) {
slouken@1895
  1322
        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
slouken@1895
  1323
        SDL_cursor->area.x = 0;
slouken@1895
  1324
        SDL_cursor->area.y = 0;
slouken@1895
  1325
        SDL_memset(SDL_cursor->save[0], 0, savelen);
slouken@1895
  1326
    }
slouken@1895
  1327
}
slouken@1895
  1328
#endif
slouken@1895
  1329
slouken@1895
  1330
struct private_yuvhwdata
slouken@1895
  1331
{
slouken@5169
  1332
    SDL_SW_YUVTexture *texture;
slouken@5169
  1333
    SDL_Surface *display;
slouken@5169
  1334
    Uint32 display_format;
slouken@1895
  1335
};
slouken@1895
  1336
slouken@1895
  1337
SDL_Overlay *
slouken@1895
  1338
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL_Surface * display)
slouken@1895
  1339
{
slouken@1895
  1340
    SDL_Overlay *overlay;
slouken@1895
  1341
    Uint32 texture_format;
slouken@5169
  1342
    SDL_SW_YUVTexture *texture;
slouken@1895
  1343
slouken@1895
  1344
    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
slouken@1895
  1345
        SDL_SetError("YUV overlays are not supported in OpenGL mode");
slouken@1895
  1346
        return NULL;
slouken@1895
  1347
    }
slouken@1895
  1348
slouken@1895
  1349
    if (display != SDL_PublicSurface) {
slouken@1895
  1350
        SDL_SetError("YUV display is only supported on the screen surface");
slouken@1895
  1351
        return NULL;
slouken@1895
  1352
    }
slouken@1895
  1353
slouken@1895
  1354
    switch (format) {
slouken@1895
  1355
    case SDL_YV12_OVERLAY:
slouken@1965
  1356
        texture_format = SDL_PIXELFORMAT_YV12;
slouken@1895
  1357
        break;
slouken@1895
  1358
    case SDL_IYUV_OVERLAY:
slouken@1965
  1359
        texture_format = SDL_PIXELFORMAT_IYUV;
slouken@1895
  1360
        break;
slouken@1895
  1361
    case SDL_YUY2_OVERLAY:
slouken@1965
  1362
        texture_format = SDL_PIXELFORMAT_YUY2;
slouken@1895
  1363
        break;
slouken@1895
  1364
    case SDL_UYVY_OVERLAY:
slouken@1965
  1365
        texture_format = SDL_PIXELFORMAT_UYVY;
slouken@1895
  1366
        break;
slouken@1895
  1367
    case SDL_YVYU_OVERLAY:
slouken@1965
  1368
        texture_format = SDL_PIXELFORMAT_YVYU;
slouken@1895
  1369
        break;
slouken@1895
  1370
    default:
slouken@1895
  1371
        SDL_SetError("Unknown YUV format");
slouken@1895
  1372
        return NULL;
slouken@1895
  1373
    }
slouken@1895
  1374
slouken@1895
  1375
    overlay = (SDL_Overlay *) SDL_malloc(sizeof(*overlay));
slouken@1895
  1376
    if (!overlay) {
slouken@1895
  1377
        SDL_OutOfMemory();
slouken@1895
  1378
        return NULL;
slouken@1895
  1379
    }
slouken@1895
  1380
    SDL_zerop(overlay);
slouken@1895
  1381
slouken@1895
  1382
    overlay->hwdata =
slouken@1895
  1383
        (struct private_yuvhwdata *) SDL_malloc(sizeof(*overlay->hwdata));
slouken@1895
  1384
    if (!overlay->hwdata) {
slouken@1895
  1385
        SDL_free(overlay);
slouken@1895
  1386
        SDL_OutOfMemory();
slouken@1895
  1387
        return NULL;
slouken@1895
  1388
    }
slouken@1895
  1389
slouken@5169
  1390
    texture = SDL_SW_CreateYUVTexture(texture_format, w, h);
slouken@5169
  1391
    if (!texture) {
slouken@5169
  1392
        SDL_free(overlay->hwdata);
slouken@5169
  1393
        SDL_free(overlay);
slouken@5169
  1394
        return NULL;
slouken@5169
  1395
    }
slouken@5169
  1396
    overlay->hwdata->texture = texture;
slouken@5169
  1397
    overlay->hwdata->display = NULL;
slouken@5169
  1398
    overlay->hwdata->display_format = SDL_PIXELFORMAT_UNKNOWN;
slouken@5169
  1399
slouken@1895
  1400
    overlay->format = format;
slouken@1895
  1401
    overlay->w = w;
slouken@1895
  1402
    overlay->h = h;
slouken@1895
  1403
    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
slouken@1895
  1404
        overlay->planes = 3;
slouken@1895
  1405
    } else {
slouken@1895
  1406
        overlay->planes = 1;
slouken@1895
  1407
    }
slouken@5169
  1408
    overlay->pitches = texture->pitches;
slouken@5169
  1409
    overlay->pixels = texture->planes;
slouken@1895
  1410
slouken@1895
  1411
    return overlay;
slouken@1895
  1412
}
slouken@1895
  1413
slouken@1895
  1414
int
slouken@1895
  1415
SDL_LockYUVOverlay(SDL_Overlay * overlay)
slouken@1895
  1416
{
slouken@5169
  1417
    SDL_Rect rect;
slouken@1895
  1418
    void *pixels;
slouken@1895
  1419
    int pitch;
slouken@2202
  1420
slouken@2202
  1421
    if (!overlay) {
slouken@2202
  1422
        SDL_SetError("Passed a NULL overlay");
slouken@2202
  1423
        return -1;
slouken@2202
  1424
    }
slouken@5169
  1425
slouken@5169
  1426
    rect.x = 0;
slouken@5169
  1427
    rect.y = 0;
slouken@5169
  1428
    rect.w = overlay->w;
slouken@5169
  1429
    rect.h = overlay->h;
slouken@5169
  1430
slouken@5169
  1431
    if (SDL_SW_LockYUVTexture(overlay->hwdata->texture, &rect, &pixels, &pitch) < 0) {
slouken@5156
  1432
        return -1;
slouken@2786
  1433
    }
slouken@5169
  1434
slouken@2226
  1435
    overlay->pixels[0] = (Uint8 *) pixels;
slouken@2226
  1436
    overlay->pitches[0] = pitch;
slouken@1895
  1437
    switch (overlay->format) {
slouken@1895
  1438
    case SDL_YV12_OVERLAY:
slouken@1895
  1439
    case SDL_IYUV_OVERLAY:
slouken@2226
  1440
        overlay->pitches[1] = pitch / 2;
slouken@2226
  1441
        overlay->pitches[2] = pitch / 2;
slouken@1895
  1442
        overlay->pixels[1] =
slouken@1895
  1443
            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
slouken@1895
  1444
        overlay->pixels[2] =
slouken@2226
  1445
            overlay->pixels[1] + overlay->pitches[1] * overlay->h / 2;
slouken@1895
  1446
        break;
slouken@1895
  1447
    case SDL_YUY2_OVERLAY:
slouken@1895
  1448
    case SDL_UYVY_OVERLAY:
slouken@1895
  1449
    case SDL_YVYU_OVERLAY:
slouken@1895
  1450
        break;
slouken@1895
  1451
    }
slouken@1895
  1452
    return 0;
slouken@1895
  1453
}
slouken@1895
  1454
slouken@1895
  1455
void
slouken@1895
  1456
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
slouken@1895
  1457
{
slouken@2202
  1458
    if (!overlay) {
slouken@2202
  1459
        return;
slouken@2202
  1460
    }
slouken@5169
  1461
slouken@5169
  1462
    SDL_SW_UnlockYUVTexture(overlay->hwdata->texture);
slouken@1895
  1463
}
slouken@1895
  1464
slouken@1895
  1465
int
slouken@1895
  1466
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
slouken@1895
  1467
{
slouken@5169
  1468
    SDL_Surface *display;
slouken@5169
  1469
    SDL_Rect src_rect;
slouken@5169
  1470
    SDL_Rect dst_rect;
slouken@5169
  1471
    void *pixels;
slouken@5169
  1472
slouken@2202
  1473
    if (!overlay || !dstrect) {
slouken@2202
  1474
        SDL_SetError("Passed a NULL overlay or dstrect");
slouken@2202
  1475
        return -1;
slouken@2202
  1476
    }
slouken@5169
  1477
slouken@5169
  1478
    display = overlay->hwdata->display;
slouken@5169
  1479
    if (display != SDL_VideoSurface) {
slouken@5169
  1480
        overlay->hwdata->display = display = SDL_VideoSurface;
slouken@5169
  1481
        overlay->hwdata->display_format = SDL_MasksToPixelFormatEnum(
slouken@5169
  1482
                                                display->format->BitsPerPixel,
slouken@5169
  1483
                                                display->format->Rmask,
slouken@5169
  1484
                                                display->format->Gmask,
slouken@5169
  1485
                                                display->format->Bmask,
slouken@5169
  1486
                                                display->format->Amask);
slouken@5169
  1487
    }
slouken@5169
  1488
slouken@5169
  1489
    src_rect.x = 0;
slouken@5169
  1490
    src_rect.y = 0;
slouken@5169
  1491
    src_rect.w = overlay->w;
slouken@5169
  1492
    src_rect.h = overlay->h;
slouken@5169
  1493
slouken@5169
  1494
    if (!SDL_IntersectRect(&display->clip_rect, dstrect, &dst_rect)) {
slouken@5169
  1495
        return 0;
slouken@5169
  1496
    }
slouken@5169
  1497
     
slouken@5169
  1498
    pixels = (void *)((Uint8 *)display->pixels +
slouken@5169
  1499
                        dst_rect.y * display->pitch +
slouken@5169
  1500
                        dst_rect.x * display->format->BytesPerPixel);
slouken@5169
  1501
slouken@5169
  1502
    if (SDL_SW_CopyYUVToRGB(overlay->hwdata->texture, &src_rect,
slouken@5169
  1503
                            overlay->hwdata->display_format,
slouken@5169
  1504
                            dst_rect.w, dst_rect.h,
slouken@5169
  1505
                            pixels, display->pitch) < 0) {
slouken@1895
  1506
        return -1;
slouken@1895
  1507
    }
slouken@5169
  1508
    SDL_UpdateWindowSurface(SDL_VideoWindow);
slouken@1895
  1509
    return 0;
slouken@1895
  1510
}
slouken@1895
  1511
slouken@1895
  1512
void
slouken@1895
  1513
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
slouken@1895
  1514
{
slouken@2202
  1515
    if (!overlay) {
slouken@2202
  1516
        return;
slouken@2202
  1517
    }
slouken@2202
  1518
    if (overlay->hwdata) {
slouken@3685
  1519
        if (overlay->hwdata->texture) {
slouken@5169
  1520
            SDL_SW_DestroyYUVTexture(overlay->hwdata->texture);
slouken@1895
  1521
        }
slouken@2202
  1522
        SDL_free(overlay->hwdata);
slouken@1895
  1523
    }
slouken@2202
  1524
    SDL_free(overlay);
slouken@1895
  1525
}
slouken@1895
  1526
slouken@1912
  1527
void
slouken@1912
  1528
SDL_GL_SwapBuffers(void)
slouken@1912
  1529
{
slouken@1912
  1530
    SDL_GL_SwapWindow(SDL_VideoWindow);
slouken@1912
  1531
}
slouken@1912
  1532
slouken@2129
  1533
slouken@2129
  1534
int
slouken@2129
  1535
SDL_EnableKeyRepeat(int delay, int interval)
slouken@2129
  1536
{
slouken@2129
  1537
    return 0;
slouken@2129
  1538
}
slouken@2129
  1539
slouken@2129
  1540
void
slouken@2129
  1541
SDL_GetKeyRepeat(int *delay, int *interval)
slouken@2129
  1542
{
slouken@2129
  1543
    if (delay) {
slouken@2129
  1544
        *delay = SDL_DEFAULT_REPEAT_DELAY;
slouken@2129
  1545
    }
slouken@2129
  1546
    if (interval) {
slouken@2129
  1547
        *interval = SDL_DEFAULT_REPEAT_INTERVAL;
slouken@2129
  1548
    }
slouken@2129
  1549
}
slouken@2129
  1550
slouken@2303
  1551
int
slouken@2303
  1552
SDL_EnableUNICODE(int enable)
slouken@2303
  1553
{
slouken@3280
  1554
    int previous = SDL_enabled_UNICODE;
slouken@3280
  1555
slouken@3280
  1556
    switch (enable) {
slouken@3280
  1557
    case 1:
slouken@3280
  1558
        SDL_enabled_UNICODE = 1;
dewyatt@4753
  1559
        SDL_StartTextInput();
slouken@3280
  1560
        break;
slouken@3280
  1561
    case 0:
slouken@3280
  1562
        SDL_enabled_UNICODE = 0;
dewyatt@4753
  1563
        SDL_StopTextInput();
slouken@3280
  1564
        break;
slouken@3280
  1565
    }
slouken@3280
  1566
    return previous;
slouken@2303
  1567
}
slouken@2303
  1568
slouken@5111
  1569
static Uint32
slouken@5111
  1570
SDL_SetTimerCallback(Uint32 interval, void* param)
slouken@5111
  1571
{
slouken@5111
  1572
    return ((SDL_OldTimerCallback)param)(interval);
slouken@5111
  1573
}
slouken@5111
  1574
slouken@5111
  1575
int
slouken@5111
  1576
SDL_SetTimer(Uint32 interval, SDL_OldTimerCallback callback)
slouken@5111
  1577
{
slouken@5111
  1578
    static SDL_TimerID compat_timer;
slouken@5111
  1579
slouken@5111
  1580
    if (compat_timer) {
slouken@5111
  1581
        SDL_RemoveTimer(compat_timer);
slouken@5111
  1582
        compat_timer = 0;
slouken@5111
  1583
    }
slouken@5111
  1584
slouken@5111
  1585
    if (interval && callback) {
slouken@5111
  1586
        compat_timer = SDL_AddTimer(interval, SDL_SetTimerCallback, callback);
slouken@5111
  1587
        if (!compat_timer) {
slouken@5111
  1588
            return -1;
slouken@5111
  1589
        }
slouken@5111
  1590
    }
slouken@5111
  1591
    return 0;
slouken@5111
  1592
}
slouken@5111
  1593
icculus@3581
  1594
int
icculus@3581
  1595
SDL_putenv(const char *_var)
icculus@3581
  1596
{
icculus@3581
  1597
    char *ptr = NULL;
icculus@3581
  1598
    char *var = SDL_strdup(_var);
icculus@3581
  1599
    if (var == NULL) {
icculus@3581
  1600
        return -1;  /* we don't set errno. */
icculus@3581
  1601
    }
icculus@3581
  1602
slouken@3592
  1603
    ptr = SDL_strchr(var, '=');
icculus@3581
  1604
    if (ptr == NULL) {
icculus@3581
  1605
        SDL_free(var);
icculus@3581
  1606
        return -1;
icculus@3581
  1607
    }
icculus@3581
  1608
icculus@3581
  1609
    *ptr = '\0';  /* split the string into name and value. */
icculus@3581
  1610
    SDL_setenv(var, ptr + 1, 1);
icculus@3581
  1611
    SDL_free(var);
icculus@3581
  1612
    return 0;
icculus@3581
  1613
}
icculus@3581
  1614
slouken@1895
  1615
/* vi: set ts=4 sw=4 expandtab: */