src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5171 34e2d5115786
child 5243 3a8a452b49f0
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
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
slouken@2829
   389
    /* We can't resize something we don't have... */
slouken@2829
   390
    if (!SDL_VideoWindow) {
slouken@2829
   391
        return -1;
slouken@2829
   392
    }
slouken@2829
   393
slouken@2829
   394
    /* We probably have to recreate the window in fullscreen mode */
slouken@2829
   395
    if (flags & SDL_FULLSCREEN) {
slouken@2829
   396
        return -1;
slouken@2829
   397
    }
slouken@2829
   398
slouken@2829
   399
    /* I don't think there's any change we can gracefully make in flags */
slouken@2829
   400
    if (flags != SDL_VideoFlags) {
slouken@2829
   401
        return -1;
slouken@2829
   402
    }
slouken@2829
   403
slouken@2829
   404
    /* Resize the window */
slouken@2829
   405
    SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
slouken@2829
   406
    if (w != width || h != height) {
slouken@2829
   407
        SDL_SetWindowSize(SDL_VideoWindow, width, height);
slouken@2829
   408
    }
slouken@2829
   409
slouken@2829
   410
    /* If we're in OpenGL mode, just resize the stub surface and we're done! */
slouken@2829
   411
    if (flags & SDL_OPENGL) {
slouken@2829
   412
        SDL_VideoSurface->w = width;
slouken@2829
   413
        SDL_VideoSurface->h = height;
slouken@2829
   414
        return 0;
slouken@2829
   415
    }
slouken@2829
   416
slouken@2829
   417
    /* Destroy the screen texture and recreate it */
slouken@5169
   418
    SDL_VideoSurface = SDL_GetWindowSurface(SDL_VideoWindow);
slouken@5169
   419
    if (!SDL_VideoSurface) {
slouken@2829
   420
        return -1;
slouken@2829
   421
    }
slouken@2829
   422
slouken@2829
   423
    if (SDL_ShadowSurface) {
slouken@2829
   424
        SDL_ShadowSurface->w = width;
slouken@2829
   425
        SDL_ShadowSurface->h = height;
slouken@2831
   426
        SDL_ShadowSurface->pitch = SDL_CalculatePitch(SDL_ShadowSurface);
slouken@2829
   427
        SDL_ShadowSurface->pixels =
slouken@2829
   428
            SDL_realloc(SDL_ShadowSurface->pixels,
slouken@2829
   429
                        SDL_ShadowSurface->h * SDL_ShadowSurface->pitch);
slouken@2829
   430
        SDL_SetClipRect(SDL_ShadowSurface, NULL);
slouken@2831
   431
        SDL_InvalidateMap(SDL_ShadowSurface->map);
slouken@5169
   432
    } else {
slouken@5169
   433
        SDL_PublicSurface = SDL_VideoSurface;
slouken@2829
   434
    }
slouken@2829
   435
slouken@2829
   436
    ClearVideoSurface();
slouken@2829
   437
slouken@2829
   438
    return 0;
slouken@2829
   439
}
slouken@2829
   440
slouken@1895
   441
SDL_Surface *
slouken@1895
   442
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
slouken@1895
   443
{
slouken@1967
   444
    SDL_DisplayMode desktop_mode;
slouken@1895
   445
    int window_x = SDL_WINDOWPOS_UNDEFINED;
slouken@1895
   446
    int window_y = SDL_WINDOWPOS_UNDEFINED;
slouken@1895
   447
    Uint32 window_flags;
slouken@5159
   448
    Uint32 surface_flags;
slouken@1895
   449
slouken@1895
   450
    if (!SDL_GetVideoDevice()) {
slouken@1895
   451
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
slouken@1895
   452
            return NULL;
slouken@1895
   453
        }
slouken@1895
   454
    }
slouken@3139
   455
slouken@3518
   456
    SelectVideoDisplay();
slouken@3518
   457
slouken@3098
   458
    SDL_GetDesktopDisplayMode(&desktop_mode);
slouken@3098
   459
slouken@3098
   460
    if (width == 0) {
slouken@3098
   461
        width = desktop_mode.w;
slouken@3098
   462
    }
slouken@3098
   463
    if (height == 0) {
slouken@3098
   464
        height = desktop_mode.h;
slouken@3098
   465
    }
slouken@5159
   466
    if (bpp == 0) {
slouken@5159
   467
        bpp = SDL_BITSPERPIXEL(desktop_mode.format);
slouken@5159
   468
    }
slouken@1895
   469
slouken@2829
   470
    /* See if we can simply resize the existing window and surface */
slouken@2829
   471
    if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
slouken@2829
   472
        return SDL_PublicSurface;
slouken@2829
   473
    }
slouken@2829
   474
slouken@1895
   475
    /* Destroy existing window */
slouken@1895
   476
    SDL_PublicSurface = NULL;
slouken@1895
   477
    if (SDL_ShadowSurface) {
slouken@1895
   478
        SDL_FreeSurface(SDL_ShadowSurface);
slouken@1895
   479
        SDL_ShadowSurface = NULL;
slouken@1895
   480
    }
slouken@1895
   481
    if (SDL_VideoSurface) {
slouken@1895
   482
        SDL_FreeSurface(SDL_VideoSurface);
slouken@1895
   483
        SDL_VideoSurface = NULL;
slouken@1895
   484
    }
slouken@1912
   485
    if (SDL_VideoContext) {
bob@2328
   486
        /* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
slouken@1912
   487
        SDL_GL_DeleteContext(SDL_VideoContext);
slouken@1912
   488
        SDL_VideoContext = NULL;
slouken@1912
   489
    }
slouken@1895
   490
    if (SDL_VideoWindow) {
slouken@1895
   491
        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
bob@2322
   492
        SDL_DestroyWindow(SDL_VideoWindow);
slouken@1895
   493
    }
slouken@1895
   494
slouken@1895
   495
    /* Set up the event filter */
slouken@1895
   496
    if (!SDL_GetEventFilter(NULL, NULL)) {
slouken@1895
   497
        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
slouken@1895
   498
    }
slouken@1895
   499
slouken@1895
   500
    /* Create a new window */
slouken@1895
   501
    window_flags = SDL_WINDOW_SHOWN;
slouken@1895
   502
    if (flags & SDL_FULLSCREEN) {
slouken@1895
   503
        window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1895
   504
    }
slouken@1895
   505
    if (flags & SDL_OPENGL) {
slouken@1895
   506
        window_flags |= SDL_WINDOW_OPENGL;
slouken@1895
   507
    }
slouken@1895
   508
    if (flags & SDL_RESIZABLE) {
slouken@1895
   509
        window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   510
    }
slouken@1895
   511
    if (flags & SDL_NOFRAME) {
slouken@1895
   512
        window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   513
    }
slouken@1895
   514
    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
slouken@1895
   515
    SDL_VideoWindow =
slouken@1895
   516
        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
slouken@1895
   517
                         window_flags);
slouken@1895
   518
    if (!SDL_VideoWindow) {
slouken@1895
   519
        return NULL;
slouken@1895
   520
    }
slouken@2967
   521
    SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon);
slouken@1895
   522
slouken@1895
   523
    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1895
   524
    surface_flags = 0;
slouken@1895
   525
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   526
        surface_flags |= SDL_FULLSCREEN;
slouken@1895
   527
    }
slouken@1895
   528
    if (window_flags & SDL_WINDOW_OPENGL) {
slouken@1895
   529
        surface_flags |= SDL_OPENGL;
slouken@1895
   530
    }
slouken@1895
   531
    if (window_flags & SDL_WINDOW_RESIZABLE) {
slouken@1895
   532
        surface_flags |= SDL_RESIZABLE;
slouken@1895
   533
    }
slouken@1895
   534
    if (window_flags & SDL_WINDOW_BORDERLESS) {
slouken@1895
   535
        surface_flags |= SDL_NOFRAME;
slouken@1895
   536
    }
slouken@1895
   537
slouken@1895
   538
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
slouken@1895
   539
    if (flags & SDL_OPENGL) {
slouken@1912
   540
        SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
slouken@1912
   541
        if (!SDL_VideoContext) {
slouken@1912
   542
            return NULL;
slouken@1912
   543
        }
slouken@1912
   544
        if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
slouken@1912
   545
            return NULL;
slouken@1912
   546
        }
slouken@1895
   547
        SDL_VideoSurface =
slouken@1895
   548
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
slouken@1895
   549
        if (!SDL_VideoSurface) {
slouken@1895
   550
            return NULL;
slouken@1895
   551
        }
slouken@1895
   552
        SDL_VideoSurface->flags |= surface_flags;
slouken@1895
   553
        SDL_PublicSurface = SDL_VideoSurface;
slouken@1895
   554
        return SDL_PublicSurface;
slouken@1895
   555
    }
slouken@1895
   556
slouken@1895
   557
    /* Create the screen surface */
slouken@5169
   558
    SDL_VideoSurface = SDL_GetWindowSurface(SDL_VideoWindow);
slouken@1895
   559
    if (!SDL_VideoSurface) {
slouken@1895
   560
        return NULL;
slouken@1895
   561
    }
slouken@1895
   562
    SDL_VideoSurface->flags |= surface_flags;
slouken@1895
   563
slouken@1895
   564
    /* Create a shadow surface if necessary */
slouken@2222
   565
    if ((bpp != SDL_VideoSurface->format->BitsPerPixel)
slouken@2222
   566
        && !(flags & SDL_ANYFORMAT)) {
slouken@2222
   567
        SDL_ShadowSurface =
slouken@2222
   568
            SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
slouken@1895
   569
        if (!SDL_ShadowSurface) {
slouken@1895
   570
            return NULL;
slouken@1895
   571
        }
slouken@1895
   572
        SDL_ShadowSurface->flags |= surface_flags;
slouken@1895
   573
slouken@1895
   574
        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
slouken@1895
   575
        if (SDL_ShadowSurface->format->palette) {
slouken@1895
   576
            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
slouken@5149
   577
            SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
slouken@5149
   578
                             SDL_ShadowSurface->format->BitsPerPixel);
slouken@1895
   579
        }
slouken@1895
   580
    }
slouken@1895
   581
    SDL_PublicSurface =
slouken@1895
   582
        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
slouken@1895
   583
slouken@2829
   584
    SDL_VideoFlags = flags;
slouken@2829
   585
slouken@2829
   586
    ClearVideoSurface();
slouken@1895
   587
slouken@3028
   588
    SetupScreenSaver(flags);
slouken@3025
   589
slouken@1895
   590
    /* We're finally done! */
slouken@1895
   591
    return SDL_PublicSurface;
slouken@1895
   592
}
slouken@1895
   593
slouken@1895
   594
SDL_Surface *
slouken@1895
   595
SDL_GetVideoSurface(void)
slouken@1895
   596
{
slouken@1895
   597
    return SDL_PublicSurface;
slouken@1895
   598
}
slouken@1895
   599
slouken@2266
   600
int
slouken@2266
   601
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
slouken@2266
   602
{
slouken@2838
   603
    if (flag & SDL_SRCALPHA) {
slouken@2838
   604
        /* According to the docs, value is ignored for alpha surfaces */
slouken@2838
   605
        if (surface->format->Amask) {
slouken@2838
   606
            value = 0xFF;
slouken@2838
   607
        }
slouken@2266
   608
        SDL_SetSurfaceAlphaMod(surface, value);
slouken@2884
   609
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2266
   610
    } else {
slouken@2266
   611
        SDL_SetSurfaceAlphaMod(surface, 0xFF);
slouken@2884
   612
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
slouken@2266
   613
    }
slouken@2838
   614
    SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
slouken@2838
   615
slouken@2266
   616
    return 0;
slouken@2266
   617
}
slouken@2266
   618
slouken@1895
   619
SDL_Surface *
slouken@1895
   620
SDL_DisplayFormat(SDL_Surface * surface)
slouken@1895
   621
{
slouken@2807
   622
    SDL_PixelFormat *format;
slouken@1895
   623
slouken@1895
   624
    if (!SDL_PublicSurface) {
slouken@1895
   625
        SDL_SetError("No video mode has been set");
slouken@1895
   626
        return NULL;
slouken@1895
   627
    }
slouken@2807
   628
    format = SDL_PublicSurface->format;
slouken@1895
   629
slouken@1895
   630
    /* Set the flags appropriate for copying to display surface */
slouken@2807
   631
    return SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
slouken@1895
   632
}
slouken@1895
   633
slouken@1895
   634
SDL_Surface *
slouken@1895
   635
SDL_DisplayFormatAlpha(SDL_Surface * surface)
slouken@1895
   636
{
slouken@1895
   637
    SDL_PixelFormat *vf;
slouken@1895
   638
    SDL_PixelFormat *format;
slouken@1895
   639
    SDL_Surface *converted;
slouken@1895
   640
    /* default to ARGB8888 */
slouken@1895
   641
    Uint32 amask = 0xff000000;
slouken@1895
   642
    Uint32 rmask = 0x00ff0000;
slouken@1895
   643
    Uint32 gmask = 0x0000ff00;
slouken@1895
   644
    Uint32 bmask = 0x000000ff;
slouken@1895
   645
slouken@1895
   646
    if (!SDL_PublicSurface) {
slouken@1895
   647
        SDL_SetError("No video mode has been set");
slouken@1895
   648
        return NULL;
slouken@1895
   649
    }
slouken@1895
   650
    vf = SDL_PublicSurface->format;
slouken@1895
   651
slouken@1895
   652
    switch (vf->BytesPerPixel) {
slouken@1895
   653
    case 2:
slouken@1895
   654
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1895
   655
           For anything else (like ARGB4444) it doesn't matter
slouken@1895
   656
           since we have no special code for it anyway */
slouken@1895
   657
        if ((vf->Rmask == 0x1f) &&
slouken@1895
   658
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1895
   659
            rmask = 0xff;
slouken@1895
   660
            bmask = 0xff0000;
slouken@1895
   661
        }
slouken@1895
   662
        break;
slouken@1895
   663
slouken@1895
   664
    case 3:
slouken@1895
   665
    case 4:
slouken@1895
   666
        /* Keep the video format, as long as the high 8 bits are
slouken@1895
   667
           unused or alpha */
slouken@1895
   668
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1895
   669
            rmask = 0xff;
slouken@1895
   670
            bmask = 0xff0000;
slouken@1895
   671
        }
slouken@1895
   672
        break;
slouken@1895
   673
slouken@1895
   674
    default:
slouken@1895
   675
        /* We have no other optimised formats right now. When/if a new
slouken@1895
   676
           optimised alpha format is written, add the converter here */
slouken@1895
   677
        break;
slouken@1895
   678
    }
slouken@1895
   679
    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
slouken@2807
   680
    converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
slouken@1895
   681
    SDL_FreeFormat(format);
slouken@1895
   682
    return converted;
slouken@1895
   683
}
slouken@1895
   684
slouken@1895
   685
int
slouken@1895
   686
SDL_Flip(SDL_Surface * screen)
slouken@1895
   687
{
slouken@1895
   688
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1895
   689
    return 0;
slouken@1895
   690
}
slouken@1895
   691
slouken@1895
   692
void
slouken@1895
   693
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@1895
   694
{
slouken@1895
   695
    if (screen) {
slouken@1895
   696
        SDL_Rect rect;
slouken@1895
   697
slouken@1895
   698
        /* Fill the rectangle */
slouken@1897
   699
        rect.x = (int) x;
slouken@1897
   700
        rect.y = (int) y;
slouken@1897
   701
        rect.w = (int) (w ? w : screen->w);
slouken@1897
   702
        rect.h = (int) (h ? h : screen->h);
slouken@1895
   703
        SDL_UpdateRects(screen, 1, &rect);
slouken@1895
   704
    }
slouken@1895
   705
}
slouken@2735
   706
slouken@1895
   707
void
slouken@1895
   708
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@1895
   709
{
slouken@1895
   710
    int i;
slouken@1895
   711
slouken@1895
   712
    if (screen == SDL_ShadowSurface) {
slouken@1895
   713
        for (i = 0; i < numrects; ++i) {
slouken@1895
   714
            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
slouken@1895
   715
                          &rects[i]);
slouken@1895
   716
        }
slouken@1895
   717
slouken@1895
   718
        /* Fall through to video surface update */
slouken@1895
   719
        screen = SDL_VideoSurface;
slouken@1895
   720
    }
slouken@1895
   721
    if (screen == SDL_VideoSurface) {
slouken@5169
   722
        SDL_UpdateWindowSurfaceRects(SDL_VideoWindow, numrects, rects);
slouken@1895
   723
    }
slouken@1895
   724
}
slouken@1895
   725
slouken@1895
   726
void
slouken@1895
   727
SDL_WM_SetCaption(const char *title, const char *icon)
slouken@1895
   728
{
slouken@1895
   729
    if (wm_title) {
slouken@1895
   730
        SDL_free(wm_title);
slouken@2037
   731
    }
slouken@2037
   732
    if (title) {
slouken@2037
   733
        wm_title = SDL_strdup(title);
slouken@1895
   734
    } else {
slouken@2037
   735
        wm_title = NULL;
slouken@1895
   736
    }
slouken@1895
   737
    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
slouken@1895
   738
}
slouken@1895
   739
slouken@1895
   740
void
slouken@2037
   741
SDL_WM_GetCaption(const char **title, const char **icon)
slouken@1895
   742
{
slouken@1895
   743
    if (title) {
slouken@1895
   744
        *title = wm_title;
slouken@1895
   745
    }
slouken@1895
   746
    if (icon) {
slouken@1895
   747
        *icon = "";
slouken@1895
   748
    }
slouken@1895
   749
}
slouken@1895
   750
slouken@1895
   751
void
slouken@1895
   752
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@1895
   753
{
slouken@2967
   754
    SDL_VideoIcon = icon;
slouken@1895
   755
}
slouken@1895
   756
slouken@1895
   757
int
slouken@1895
   758
SDL_WM_IconifyWindow(void)
slouken@1895
   759
{
slouken@1895
   760
    SDL_MinimizeWindow(SDL_VideoWindow);
slouken@1895
   761
    return 0;
slouken@1895
   762
}
slouken@1895
   763
slouken@1895
   764
int
slouken@1895
   765
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
slouken@1895
   766
{
slouken@1895
   767
    if (SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   768
        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 0) < 0) {
slouken@1895
   769
            return 0;
slouken@1895
   770
        }
slouken@1895
   771
        SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
slouken@1895
   772
    } else {
slouken@1895
   773
        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 1) < 0) {
slouken@1895
   774
            return 0;
slouken@1895
   775
        }
slouken@1895
   776
        SDL_PublicSurface->flags |= SDL_FULLSCREEN;
slouken@1895
   777
    }
slouken@1895
   778
    return 1;
slouken@1895
   779
}
slouken@1895
   780
slouken@1895
   781
SDL_GrabMode
slouken@1895
   782
SDL_WM_GrabInput(SDL_GrabMode mode)
slouken@1895
   783
{
slouken@1895
   784
    if (mode != SDL_GRAB_QUERY) {
slouken@1895
   785
        SDL_SetWindowGrab(SDL_VideoWindow, mode);
slouken@1895
   786
    }
slouken@1895
   787
    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
slouken@1895
   788
}
slouken@1895
   789
slouken@1895
   790
void
slouken@1895
   791
SDL_WarpMouse(Uint16 x, Uint16 y)
slouken@1895
   792
{
slouken@1895
   793
    SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
slouken@1895
   794
}
slouken@1895
   795
slouken@1895
   796
Uint8
slouken@1895
   797
SDL_GetAppState(void)
slouken@1895
   798
{
slouken@1895
   799
    Uint8 state = 0;
slouken@1895
   800
    Uint32 flags = 0;
slouken@1895
   801
slouken@1895
   802
    flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1895
   803
    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
slouken@1895
   804
        state |= SDL_APPACTIVE;
slouken@1895
   805
    }
slouken@1895
   806
    if (flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@1895
   807
        state |= SDL_APPINPUTFOCUS;
slouken@1895
   808
    }
slouken@1895
   809
    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1895
   810
        state |= SDL_APPMOUSEFOCUS;
slouken@1895
   811
    }
slouken@1895
   812
    return state;
slouken@1895
   813
}
slouken@1895
   814
slouken@1895
   815
const SDL_version *
slouken@1895
   816
SDL_Linked_Version(void)
slouken@1895
   817
{
slouken@1895
   818
    static SDL_version version;
slouken@1895
   819
    SDL_VERSION(&version);
slouken@1895
   820
    return &version;
slouken@1895
   821
}
slouken@1895
   822
slouken@1895
   823
int
slouken@1895
   824
SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
slouken@1895
   825
               int firstcolor, int ncolors)
slouken@1895
   826
{
slouken@1895
   827
    return SDL_SetColors(surface, colors, firstcolor, ncolors);
slouken@1895
   828
}
slouken@1895
   829
slouken@1895
   830
int
slouken@1895
   831
SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
slouken@1895
   832
              int ncolors)
slouken@1895
   833
{
slouken@1895
   834
    if (SDL_SetPaletteColors
slouken@1895
   835
        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
slouken@1895
   836
        return 1;
slouken@1895
   837
    } else {
slouken@1895
   838
        return 0;
slouken@1895
   839
    }
slouken@1895
   840
}
slouken@1895
   841
slouken@1895
   842
int
slouken@1895
   843
SDL_GetWMInfo(SDL_SysWMinfo * info)
slouken@1895
   844
{
slouken@1895
   845
    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
slouken@1895
   846
}
slouken@1895
   847
slouken@1895
   848
#if 0
slouken@1895
   849
void
slouken@1895
   850
SDL_MoveCursor(int x, int y)
slouken@1895
   851
{
slouken@1895
   852
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   853
slouken@1895
   854
    /* Erase and update the current mouse position */
slouken@1895
   855
    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1895
   856
        /* Erase and redraw mouse cursor in new position */
slouken@1895
   857
        SDL_LockCursor();
slouken@1895
   858
        SDL_EraseCursor(SDL_VideoSurface);
slouken@1895
   859
        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@1895
   860
        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@1895
   861
        SDL_DrawCursor(SDL_VideoSurface);
slouken@1895
   862
        SDL_UnlockCursor();
slouken@1895
   863
    } else if (_this->MoveWMCursor) {
slouken@1895
   864
        _this->MoveWMCursor(_this, x, y);
slouken@1895
   865
    }
slouken@1895
   866
}
slouken@1895
   867
slouken@1895
   868
/* Keep track of the current cursor colors */
slouken@1895
   869
static int palette_changed = 1;
slouken@1895
   870
static Uint8 pixels8[2];
slouken@1895
   871
slouken@1895
   872
void
slouken@1895
   873
SDL_CursorPaletteChanged(void)
slouken@1895
   874
{
slouken@1895
   875
    palette_changed = 1;
slouken@1895
   876
}
slouken@1895
   877
slouken@1895
   878
void
slouken@1895
   879
SDL_MouseRect(SDL_Rect * area)
slouken@1895
   880
{
slouken@1895
   881
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   882
    int clip_diff;
slouken@1895
   883
slouken@1895
   884
    *area = SDL_cursor->area;
slouken@1895
   885
    if (area->x < 0) {
slouken@1895
   886
        area->w += area->x;
slouken@1895
   887
        area->x = 0;
slouken@1895
   888
    }
slouken@1895
   889
    if (area->y < 0) {
slouken@1895
   890
        area->h += area->y;
slouken@1895
   891
        area->y = 0;
slouken@1895
   892
    }
slouken@1895
   893
    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
slouken@1895
   894
    if (clip_diff > 0) {
slouken@1895
   895
        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
slouken@1895
   896
    }
slouken@1895
   897
    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
slouken@1895
   898
    if (clip_diff > 0) {
slouken@1895
   899
        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
slouken@1895
   900
    }
slouken@1895
   901
}
slouken@1895
   902
slouken@1895
   903
static void
slouken@1895
   904
SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
slouken@1895
   905
{
slouken@1895
   906
    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
slouken@1895
   907
    int i, w, h;
slouken@1895
   908
    Uint8 *data, datab;
slouken@1895
   909
    Uint8 *mask, maskb;
slouken@1895
   910
slouken@1895
   911
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1895
   912
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1895
   913
    switch (screen->format->BytesPerPixel) {
slouken@1895
   914
slouken@1895
   915
    case 1:
slouken@1895
   916
        {
slouken@1895
   917
            Uint8 *dst;
slouken@1895
   918
            int dstskip;
slouken@1895
   919
slouken@1895
   920
            if (palette_changed) {
slouken@1895
   921
                pixels8[0] =
slouken@1895
   922
                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1895
   923
                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1895
   924
                palette_changed = 0;
slouken@1895
   925
            }
slouken@1895
   926
            dst = (Uint8 *) screen->pixels +
slouken@1895
   927
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1895
   928
                SDL_cursor->area.x;
slouken@1895
   929
            dstskip = screen->pitch - area->w;
slouken@1895
   930
slouken@1895
   931
            for (h = area->h; h; h--) {
slouken@1895
   932
                for (w = area->w / 8; w; w--) {
slouken@1895
   933
                    maskb = *mask++;
slouken@1895
   934
                    datab = *data++;
slouken@1895
   935
                    for (i = 0; i < 8; ++i) {
slouken@1895
   936
                        if (maskb & 0x80) {
slouken@1895
   937
                            *dst = pixels8[datab >> 7];
slouken@1895
   938
                        }
slouken@1895
   939
                        maskb <<= 1;
slouken@1895
   940
                        datab <<= 1;
slouken@1895
   941
                        dst++;
slouken@1895
   942
                    }
slouken@1895
   943
                }
slouken@1895
   944
                dst += dstskip;
slouken@1895
   945
            }
slouken@1895
   946
        }
slouken@1895
   947
        break;
slouken@1895
   948
slouken@1895
   949
    case 2:
slouken@1895
   950
        {
slouken@1895
   951
            Uint16 *dst;
slouken@1895
   952
            int dstskip;
slouken@1895
   953
slouken@1895
   954
            dst = (Uint16 *) screen->pixels +
slouken@1895
   955
                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
slouken@1895
   956
                SDL_cursor->area.x;
slouken@1895
   957
            dstskip = (screen->pitch / 2) - area->w;
slouken@1895
   958
slouken@1895
   959
            for (h = area->h; h; h--) {
slouken@1895
   960
                for (w = area->w / 8; w; w--) {
slouken@1895
   961
                    maskb = *mask++;
slouken@1895
   962
                    datab = *data++;
slouken@1895
   963
                    for (i = 0; i < 8; ++i) {
slouken@1895
   964
                        if (maskb & 0x80) {
slouken@1895
   965
                            *dst = (Uint16) pixels[datab >> 7];
slouken@1895
   966
                        }
slouken@1895
   967
                        maskb <<= 1;
slouken@1895
   968
                        datab <<= 1;
slouken@1895
   969
                        dst++;
slouken@1895
   970
                    }
slouken@1895
   971
                }
slouken@1895
   972
                dst += dstskip;
slouken@1895
   973
            }
slouken@1895
   974
        }
slouken@1895
   975
        break;
slouken@1895
   976
slouken@1895
   977
    case 3:
slouken@1895
   978
        {
slouken@1895
   979
            Uint8 *dst;
slouken@1895
   980
            int dstskip;
slouken@1895
   981
slouken@1895
   982
            dst = (Uint8 *) screen->pixels +
slouken@1895
   983
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1895
   984
                SDL_cursor->area.x * 3;
slouken@1895
   985
            dstskip = screen->pitch - area->w * 3;
slouken@1895
   986
slouken@1895
   987
            for (h = area->h; h; h--) {
slouken@1895
   988
                for (w = area->w / 8; w; w--) {
slouken@1895
   989
                    maskb = *mask++;
slouken@1895
   990
                    datab = *data++;
slouken@1895
   991
                    for (i = 0; i < 8; ++i) {
slouken@1895
   992
                        if (maskb & 0x80) {
slouken@1895
   993
                            SDL_memset(dst, pixels[datab >> 7], 3);
slouken@1895
   994
                        }
slouken@1895
   995
                        maskb <<= 1;
slouken@1895
   996
                        datab <<= 1;
slouken@1895
   997
                        dst += 3;
slouken@1895
   998
                    }
slouken@1895
   999
                }
slouken@1895
  1000
                dst += dstskip;
slouken@1895
  1001
            }
slouken@1895
  1002
        }
slouken@1895
  1003
        break;
slouken@1895
  1004
slouken@1895
  1005
    case 4:
slouken@1895
  1006
        {
slouken@1895
  1007
            Uint32 *dst;
slouken@1895
  1008
            int dstskip;
slouken@1895
  1009
slouken@1895
  1010
            dst = (Uint32 *) screen->pixels +
slouken@1895
  1011
                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
slouken@1895
  1012
                SDL_cursor->area.x;
slouken@1895
  1013
            dstskip = (screen->pitch / 4) - area->w;
slouken@1895
  1014
slouken@1895
  1015
            for (h = area->h; h; h--) {
slouken@1895
  1016
                for (w = area->w / 8; w; w--) {
slouken@1895
  1017
                    maskb = *mask++;
slouken@1895
  1018
                    datab = *data++;
slouken@1895
  1019
                    for (i = 0; i < 8; ++i) {
slouken@1895
  1020
                        if (maskb & 0x80) {
slouken@1895
  1021
                            *dst = pixels[datab >> 7];
slouken@1895
  1022
                        }
slouken@1895
  1023
                        maskb <<= 1;
slouken@1895
  1024
                        datab <<= 1;
slouken@1895
  1025
                        dst++;
slouken@1895
  1026
                    }
slouken@1895
  1027
                }
slouken@1895
  1028
                dst += dstskip;
slouken@1895
  1029
            }
slouken@1895
  1030
        }
slouken@1895
  1031
        break;
slouken@1895
  1032
    }
slouken@1895
  1033
}
slouken@1895
  1034
slouken@1895
  1035
static void
slouken@1895
  1036
SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
slouken@1895
  1037
{
slouken@1895
  1038
    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
slouken@1895
  1039
    int h;
slouken@1895
  1040
    int x, minx, maxx;
slouken@1895
  1041
    Uint8 *data, datab = 0;
slouken@1895
  1042
    Uint8 *mask, maskb = 0;
slouken@1895
  1043
    Uint8 *dst;
slouken@1895
  1044
    int dstbpp, dstskip;
slouken@1895
  1045
slouken@1895
  1046
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1895
  1047
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1895
  1048
    dstbpp = screen->format->BytesPerPixel;
slouken@1895
  1049
    dst = (Uint8 *) screen->pixels +
slouken@1895
  1050
        (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1895
  1051
        SDL_cursor->area.x * dstbpp;
slouken@1895
  1052
    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
slouken@1895
  1053
slouken@1895
  1054
    minx = area->x;
slouken@1895
  1055
    maxx = area->x + area->w;
slouken@1895
  1056
    if (screen->format->BytesPerPixel == 1) {
slouken@1895
  1057
        if (palette_changed) {
slouken@1895
  1058
            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1895
  1059
            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1895
  1060
            palette_changed = 0;
slouken@1895
  1061
        }
slouken@1895
  1062
        for (h = area->h; h; h--) {
slouken@1895
  1063
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1895
  1064
                if ((x % 8) == 0) {
slouken@1895
  1065
                    maskb = *mask++;
slouken@1895
  1066
                    datab = *data++;
slouken@1895
  1067
                }
slouken@1895
  1068
                if ((x >= minx) && (x < maxx)) {
slouken@1895
  1069
                    if (maskb & 0x80) {
slouken@1895
  1070
                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
slouken@1895
  1071
                    }
slouken@1895
  1072
                }
slouken@1895
  1073
                maskb <<= 1;
slouken@1895
  1074
                datab <<= 1;
slouken@1895
  1075
                dst += dstbpp;
slouken@1895
  1076
            }
slouken@1895
  1077
            dst += dstskip;
slouken@1895
  1078
        }
slouken@1895
  1079
    } else {
slouken@1895
  1080
        for (h = area->h; h; h--) {
slouken@1895
  1081
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1895
  1082
                if ((x % 8) == 0) {
slouken@1895
  1083
                    maskb = *mask++;
slouken@1895
  1084
                    datab = *data++;
slouken@1895
  1085
                }
slouken@1895
  1086
                if ((x >= minx) && (x < maxx)) {
slouken@1895
  1087
                    if (maskb & 0x80) {
slouken@1895
  1088
                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
slouken@1895
  1089
                    }
slouken@1895
  1090
                }
slouken@1895
  1091
                maskb <<= 1;
slouken@1895
  1092
                datab <<= 1;
slouken@1895
  1093
                dst += dstbpp;
slouken@1895
  1094
            }
slouken@1895
  1095
            dst += dstskip;
slouken@1895
  1096
        }
slouken@1895
  1097
    }
slouken@1895
  1098
}
slouken@1895
  1099
slouken@1895
  1100
/* This handles the ugly work of converting the saved cursor background from
slouken@1895
  1101
   the pixel format of the shadow surface to that of the video surface.
slouken@1895
  1102
   This is only necessary when blitting from a shadow surface of a different
slouken@1895
  1103
   pixel format than the video surface, and using a software rendered cursor.
slouken@1895
  1104
*/
slouken@1895
  1105
static void
slouken@1895
  1106
SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
slouken@1895
  1107
{
slouken@1895
  1108
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1109
    SDL_BlitInfo info;
slouken@1895
  1110
    SDL_loblit RunBlit;
slouken@1895
  1111
slouken@1895
  1112
    /* Make sure we can steal the blit mapping */
slouken@1895
  1113
    if (screen->map->dst != SDL_VideoSurface) {
slouken@1895
  1114
        return;
slouken@1895
  1115
    }
slouken@1895
  1116
slouken@1895
  1117
    /* Set up the blit information */
slouken@1895
  1118
    info.s_pixels = SDL_cursor->save[1];
slouken@1895
  1119
    info.s_width = w;
slouken@1895
  1120
    info.s_height = h;
slouken@1895
  1121
    info.s_skip = 0;
slouken@1895
  1122
    info.d_pixels = SDL_cursor->save[0];
slouken@1895
  1123
    info.d_width = w;
slouken@1895
  1124
    info.d_height = h;
slouken@1895
  1125
    info.d_skip = 0;
slouken@1895
  1126
    info.aux_data = screen->map->sw_data->aux_data;
slouken@1895
  1127
    info.src = screen->format;
slouken@1895
  1128
    info.table = screen->map->table;
slouken@1895
  1129
    info.dst = SDL_VideoSurface->format;
slouken@1895
  1130
    RunBlit = screen->map->sw_data->blit;
slouken@1895
  1131
slouken@1895
  1132
    /* Run the actual software blit */
slouken@1895
  1133
    RunBlit(&info);
slouken@1895
  1134
}
slouken@1895
  1135
slouken@1895
  1136
void
slouken@1895
  1137
SDL_DrawCursorNoLock(SDL_Surface * screen)
slouken@1895
  1138
{
slouken@1895
  1139
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1140
    SDL_Rect area;
slouken@1895
  1141
slouken@1895
  1142
    /* Get the mouse rectangle, clipped to the screen */
slouken@1895
  1143
    SDL_MouseRect(&area);
slouken@1895
  1144
    if ((area.w == 0) || (area.h == 0)) {
slouken@1895
  1145
        return;
slouken@1895
  1146
    }
slouken@1895
  1147
slouken@1895
  1148
    /* Copy mouse background */
slouken@1895
  1149
    {
slouken@1895
  1150
        int w, h, screenbpp;
slouken@1895
  1151
        Uint8 *src, *dst;
slouken@1895
  1152
slouken@1895
  1153
        /* Set up the copy pointers */
slouken@1895
  1154
        screenbpp = screen->format->BytesPerPixel;
slouken@1895
  1155
        if ((screen == SDL_VideoSurface) ||
slouken@1895
  1156
            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
slouken@1895
  1157
            dst = SDL_cursor->save[0];
slouken@1895
  1158
        } else {
slouken@1895
  1159
            dst = SDL_cursor->save[1];
slouken@1895
  1160
        }
slouken@1895
  1161
        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1895
  1162
            area.x * screenbpp;
slouken@1895
  1163
slouken@1895
  1164
        /* Perform the copy */
slouken@1895
  1165
        w = area.w * screenbpp;
slouken@1895
  1166
        h = area.h;
slouken@1895
  1167
        while (h--) {
slouken@1895
  1168
            SDL_memcpy(dst, src, w);
slouken@1895
  1169
            dst += w;
slouken@1895
  1170
            src += screen->pitch;
slouken@1895
  1171
        }
slouken@1895
  1172
    }
slouken@1895
  1173
slouken@1895
  1174
    /* Draw the mouse cursor */
slouken@1895
  1175
    area.x -= SDL_cursor->area.x;
slouken@1895
  1176
    area.y -= SDL_cursor->area.y;
slouken@1895
  1177
    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
slouken@1895
  1178
        SDL_DrawCursorFast(screen, &area);
slouken@1895
  1179
    } else {
slouken@1895
  1180
        SDL_DrawCursorSlow(screen, &area);
slouken@1895
  1181
    }
slouken@1895
  1182
}
slouken@1895
  1183
slouken@1895
  1184
void
slouken@1895
  1185
SDL_DrawCursor(SDL_Surface * screen)
slouken@1895
  1186
{
slouken@1895
  1187
    /* Lock the screen if necessary */
slouken@1895
  1188
    if (screen == NULL) {
slouken@1895
  1189
        return;
slouken@1895
  1190
    }
slouken@1895
  1191
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1192
        if (SDL_LockSurface(screen) < 0) {
slouken@1895
  1193
            return;
slouken@1895
  1194
        }
slouken@1895
  1195
    }
slouken@1895
  1196
slouken@1895
  1197
    SDL_DrawCursorNoLock(screen);
slouken@1895
  1198
slouken@1895
  1199
    /* Unlock the screen and update if necessary */
slouken@1895
  1200
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1201
        SDL_UnlockSurface(screen);
slouken@1895
  1202
    }
slouken@2222
  1203
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1895
  1204
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1205
        SDL_Window *window;
slouken@1895
  1206
        SDL_Rect area;
slouken@1895
  1207
slouken@1895
  1208
        window = SDL_GetWindowFromSurface(screen);
slouken@1895
  1209
        if (!window) {
slouken@1895
  1210
            return;
slouken@1895
  1211
        }
slouken@1895
  1212
slouken@1895
  1213
        SDL_MouseRect(&area);
slouken@1895
  1214
slouken@1895
  1215
        if (_this->UpdateWindowSurface) {
slouken@1895
  1216
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1895
  1217
        }
slouken@1895
  1218
    }
slouken@1895
  1219
}
slouken@1895
  1220
slouken@1895
  1221
void
slouken@1895
  1222
SDL_EraseCursorNoLock(SDL_Surface * screen)
slouken@1895
  1223
{
slouken@1895
  1224
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1225
    SDL_Window *window;
slouken@1895
  1226
    SDL_Rect area;
slouken@1895
  1227
slouken@1895
  1228
    /* Get the window associated with the surface */
slouken@1895
  1229
    window = SDL_GetWindowFromSurface(screen);
slouken@1895
  1230
    if (!window || !window->surface) {
slouken@1895
  1231
        return;
slouken@1895
  1232
    }
slouken@1895
  1233
slouken@1895
  1234
    /* Get the mouse rectangle, clipped to the screen */
slouken@1895
  1235
    SDL_MouseRect(&area);
slouken@1895
  1236
    if ((area.w == 0) || (area.h == 0)) {
slouken@1895
  1237
        return;
slouken@1895
  1238
    }
slouken@1895
  1239
slouken@1895
  1240
    /* Copy mouse background */
slouken@1895
  1241
    {
slouken@1895
  1242
        int w, h, screenbpp;
slouken@1895
  1243
        Uint8 *src, *dst;
slouken@1895
  1244
slouken@1895
  1245
        /* Set up the copy pointers */
slouken@1895
  1246
        screenbpp = screen->format->BytesPerPixel;
slouken@1895
  1247
        if ((screen->flags & SDL_SCREEN_SURFACE) ||
slouken@1895
  1248
            FORMAT_EQUAL(screen->format, window->surface->format)) {
slouken@1895
  1249
            src = SDL_cursor->save[0];
slouken@1895
  1250
        } else {
slouken@1895
  1251
            src = SDL_cursor->save[1];
slouken@1895
  1252
        }
slouken@1895
  1253
        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1895
  1254
            area.x * screenbpp;
slouken@1895
  1255
slouken@1895
  1256
        /* Perform the copy */
slouken@1895
  1257
        w = area.w * screenbpp;
slouken@1895
  1258
        h = area.h;
slouken@1895
  1259
        while (h--) {
slouken@1895
  1260
            SDL_memcpy(dst, src, w);
slouken@1895
  1261
            src += w;
slouken@1895
  1262
            dst += screen->pitch;
slouken@1895
  1263
        }
slouken@1895
  1264
slouken@1895
  1265
        /* Perform pixel conversion on cursor background */
slouken@1895
  1266
        if (src > SDL_cursor->save[1]) {
slouken@1895
  1267
            SDL_ConvertCursorSave(screen, area.w, area.h);
slouken@1895
  1268
        }
slouken@1895
  1269
    }
slouken@1895
  1270
}
slouken@1895
  1271
slouken@1895
  1272
void
slouken@1895
  1273
SDL_EraseCursor(SDL_Surface * screen)
slouken@1895
  1274
{
slouken@1895
  1275
    /* Lock the screen if necessary */
slouken@1895
  1276
    if (screen == NULL) {
slouken@1895
  1277
        return;
slouken@1895
  1278
    }
slouken@1895
  1279
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1280
        if (SDL_LockSurface(screen) < 0) {
slouken@1895
  1281
            return;
slouken@1895
  1282
        }
slouken@1895
  1283
    }
slouken@1895
  1284
slouken@1895
  1285
    SDL_EraseCursorNoLock(screen);
slouken@1895
  1286
slouken@1895
  1287
    /* Unlock the screen and update if necessary */
slouken@1895
  1288
    if (SDL_MUSTLOCK(screen)) {
slouken@1895
  1289
        SDL_UnlockSurface(screen);
slouken@1895
  1290
    }
slouken@2222
  1291
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1895
  1292
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
  1293
        SDL_Window *window;
slouken@1895
  1294
        SDL_Rect area;
slouken@1895
  1295
slouken@1895
  1296
        window = SDL_GetWindowFromSurface(screen);
slouken@1895
  1297
        if (!window) {
slouken@1895
  1298
            return;
slouken@1895
  1299
        }
slouken@1895
  1300
slouken@1895
  1301
        SDL_MouseRect(&area);
slouken@1895
  1302
slouken@1895
  1303
        if (_this->UpdateWindowSurface) {
slouken@1895
  1304
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1895
  1305
        }
slouken@1895
  1306
    }
slouken@1895
  1307
}
slouken@1895
  1308
slouken@1895
  1309
/* Reset the cursor on video mode change
slouken@1895
  1310
   FIXME:  Keep track of all cursors, and reset them all.
slouken@1895
  1311
 */
slouken@1895
  1312
void
slouken@1895
  1313
SDL_ResetCursor(void)
slouken@1895
  1314
{
slouken@1895
  1315
    int savelen;
slouken@1895
  1316
slouken@1895
  1317
    if (SDL_cursor) {
slouken@1895
  1318
        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
slouken@1895
  1319
        SDL_cursor->area.x = 0;
slouken@1895
  1320
        SDL_cursor->area.y = 0;
slouken@1895
  1321
        SDL_memset(SDL_cursor->save[0], 0, savelen);
slouken@1895
  1322
    }
slouken@1895
  1323
}
slouken@1895
  1324
#endif
slouken@1895
  1325
slouken@1895
  1326
struct private_yuvhwdata
slouken@1895
  1327
{
slouken@5169
  1328
    SDL_SW_YUVTexture *texture;
slouken@5169
  1329
    SDL_Surface *display;
slouken@5169
  1330
    Uint32 display_format;
slouken@1895
  1331
};
slouken@1895
  1332
slouken@1895
  1333
SDL_Overlay *
slouken@1895
  1334
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL_Surface * display)
slouken@1895
  1335
{
slouken@1895
  1336
    SDL_Overlay *overlay;
slouken@1895
  1337
    Uint32 texture_format;
slouken@5169
  1338
    SDL_SW_YUVTexture *texture;
slouken@1895
  1339
slouken@1895
  1340
    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
slouken@1895
  1341
        SDL_SetError("YUV overlays are not supported in OpenGL mode");
slouken@1895
  1342
        return NULL;
slouken@1895
  1343
    }
slouken@1895
  1344
slouken@1895
  1345
    if (display != SDL_PublicSurface) {
slouken@1895
  1346
        SDL_SetError("YUV display is only supported on the screen surface");
slouken@1895
  1347
        return NULL;
slouken@1895
  1348
    }
slouken@1895
  1349
slouken@1895
  1350
    switch (format) {
slouken@1895
  1351
    case SDL_YV12_OVERLAY:
slouken@1965
  1352
        texture_format = SDL_PIXELFORMAT_YV12;
slouken@1895
  1353
        break;
slouken@1895
  1354
    case SDL_IYUV_OVERLAY:
slouken@1965
  1355
        texture_format = SDL_PIXELFORMAT_IYUV;
slouken@1895
  1356
        break;
slouken@1895
  1357
    case SDL_YUY2_OVERLAY:
slouken@1965
  1358
        texture_format = SDL_PIXELFORMAT_YUY2;
slouken@1895
  1359
        break;
slouken@1895
  1360
    case SDL_UYVY_OVERLAY:
slouken@1965
  1361
        texture_format = SDL_PIXELFORMAT_UYVY;
slouken@1895
  1362
        break;
slouken@1895
  1363
    case SDL_YVYU_OVERLAY:
slouken@1965
  1364
        texture_format = SDL_PIXELFORMAT_YVYU;
slouken@1895
  1365
        break;
slouken@1895
  1366
    default:
slouken@1895
  1367
        SDL_SetError("Unknown YUV format");
slouken@1895
  1368
        return NULL;
slouken@1895
  1369
    }
slouken@1895
  1370
slouken@1895
  1371
    overlay = (SDL_Overlay *) SDL_malloc(sizeof(*overlay));
slouken@1895
  1372
    if (!overlay) {
slouken@1895
  1373
        SDL_OutOfMemory();
slouken@1895
  1374
        return NULL;
slouken@1895
  1375
    }
slouken@1895
  1376
    SDL_zerop(overlay);
slouken@1895
  1377
slouken@1895
  1378
    overlay->hwdata =
slouken@1895
  1379
        (struct private_yuvhwdata *) SDL_malloc(sizeof(*overlay->hwdata));
slouken@1895
  1380
    if (!overlay->hwdata) {
slouken@1895
  1381
        SDL_free(overlay);
slouken@1895
  1382
        SDL_OutOfMemory();
slouken@1895
  1383
        return NULL;
slouken@1895
  1384
    }
slouken@1895
  1385
slouken@5169
  1386
    texture = SDL_SW_CreateYUVTexture(texture_format, w, h);
slouken@5169
  1387
    if (!texture) {
slouken@5169
  1388
        SDL_free(overlay->hwdata);
slouken@5169
  1389
        SDL_free(overlay);
slouken@5169
  1390
        return NULL;
slouken@5169
  1391
    }
slouken@5169
  1392
    overlay->hwdata->texture = texture;
slouken@5169
  1393
    overlay->hwdata->display = NULL;
slouken@5169
  1394
    overlay->hwdata->display_format = SDL_PIXELFORMAT_UNKNOWN;
slouken@5169
  1395
slouken@1895
  1396
    overlay->format = format;
slouken@1895
  1397
    overlay->w = w;
slouken@1895
  1398
    overlay->h = h;
slouken@1895
  1399
    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
slouken@1895
  1400
        overlay->planes = 3;
slouken@1895
  1401
    } else {
slouken@1895
  1402
        overlay->planes = 1;
slouken@1895
  1403
    }
slouken@5169
  1404
    overlay->pitches = texture->pitches;
slouken@5169
  1405
    overlay->pixels = texture->planes;
slouken@1895
  1406
slouken@1895
  1407
    return overlay;
slouken@1895
  1408
}
slouken@1895
  1409
slouken@1895
  1410
int
slouken@1895
  1411
SDL_LockYUVOverlay(SDL_Overlay * overlay)
slouken@1895
  1412
{
slouken@5169
  1413
    SDL_Rect rect;
slouken@1895
  1414
    void *pixels;
slouken@1895
  1415
    int pitch;
slouken@2202
  1416
slouken@2202
  1417
    if (!overlay) {
slouken@2202
  1418
        SDL_SetError("Passed a NULL overlay");
slouken@2202
  1419
        return -1;
slouken@2202
  1420
    }
slouken@5169
  1421
slouken@5169
  1422
    rect.x = 0;
slouken@5169
  1423
    rect.y = 0;
slouken@5169
  1424
    rect.w = overlay->w;
slouken@5169
  1425
    rect.h = overlay->h;
slouken@5169
  1426
slouken@5169
  1427
    if (SDL_SW_LockYUVTexture(overlay->hwdata->texture, &rect, &pixels, &pitch) < 0) {
slouken@5156
  1428
        return -1;
slouken@2786
  1429
    }
slouken@5169
  1430
slouken@2226
  1431
    overlay->pixels[0] = (Uint8 *) pixels;
slouken@2226
  1432
    overlay->pitches[0] = pitch;
slouken@1895
  1433
    switch (overlay->format) {
slouken@1895
  1434
    case SDL_YV12_OVERLAY:
slouken@1895
  1435
    case SDL_IYUV_OVERLAY:
slouken@2226
  1436
        overlay->pitches[1] = pitch / 2;
slouken@2226
  1437
        overlay->pitches[2] = pitch / 2;
slouken@1895
  1438
        overlay->pixels[1] =
slouken@1895
  1439
            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
slouken@1895
  1440
        overlay->pixels[2] =
slouken@2226
  1441
            overlay->pixels[1] + overlay->pitches[1] * overlay->h / 2;
slouken@1895
  1442
        break;
slouken@1895
  1443
    case SDL_YUY2_OVERLAY:
slouken@1895
  1444
    case SDL_UYVY_OVERLAY:
slouken@1895
  1445
    case SDL_YVYU_OVERLAY:
slouken@1895
  1446
        break;
slouken@1895
  1447
    }
slouken@1895
  1448
    return 0;
slouken@1895
  1449
}
slouken@1895
  1450
slouken@1895
  1451
void
slouken@1895
  1452
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
slouken@1895
  1453
{
slouken@2202
  1454
    if (!overlay) {
slouken@2202
  1455
        return;
slouken@2202
  1456
    }
slouken@5169
  1457
slouken@5169
  1458
    SDL_SW_UnlockYUVTexture(overlay->hwdata->texture);
slouken@1895
  1459
}
slouken@1895
  1460
slouken@1895
  1461
int
slouken@1895
  1462
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
slouken@1895
  1463
{
slouken@5169
  1464
    SDL_Surface *display;
slouken@5169
  1465
    SDL_Rect src_rect;
slouken@5169
  1466
    SDL_Rect dst_rect;
slouken@5169
  1467
    void *pixels;
slouken@5169
  1468
slouken@2202
  1469
    if (!overlay || !dstrect) {
slouken@2202
  1470
        SDL_SetError("Passed a NULL overlay or dstrect");
slouken@2202
  1471
        return -1;
slouken@2202
  1472
    }
slouken@5169
  1473
slouken@5169
  1474
    display = overlay->hwdata->display;
slouken@5169
  1475
    if (display != SDL_VideoSurface) {
slouken@5169
  1476
        overlay->hwdata->display = display = SDL_VideoSurface;
slouken@5169
  1477
        overlay->hwdata->display_format = SDL_MasksToPixelFormatEnum(
slouken@5169
  1478
                                                display->format->BitsPerPixel,
slouken@5169
  1479
                                                display->format->Rmask,
slouken@5169
  1480
                                                display->format->Gmask,
slouken@5169
  1481
                                                display->format->Bmask,
slouken@5169
  1482
                                                display->format->Amask);
slouken@5169
  1483
    }
slouken@5169
  1484
slouken@5169
  1485
    src_rect.x = 0;
slouken@5169
  1486
    src_rect.y = 0;
slouken@5169
  1487
    src_rect.w = overlay->w;
slouken@5169
  1488
    src_rect.h = overlay->h;
slouken@5169
  1489
slouken@5169
  1490
    if (!SDL_IntersectRect(&display->clip_rect, dstrect, &dst_rect)) {
slouken@5169
  1491
        return 0;
slouken@5169
  1492
    }
slouken@5169
  1493
     
slouken@5169
  1494
    pixels = (void *)((Uint8 *)display->pixels +
slouken@5169
  1495
                        dst_rect.y * display->pitch +
slouken@5169
  1496
                        dst_rect.x * display->format->BytesPerPixel);
slouken@5169
  1497
slouken@5169
  1498
    if (SDL_SW_CopyYUVToRGB(overlay->hwdata->texture, &src_rect,
slouken@5169
  1499
                            overlay->hwdata->display_format,
slouken@5169
  1500
                            dst_rect.w, dst_rect.h,
slouken@5169
  1501
                            pixels, display->pitch) < 0) {
slouken@1895
  1502
        return -1;
slouken@1895
  1503
    }
slouken@5169
  1504
    SDL_UpdateWindowSurface(SDL_VideoWindow);
slouken@1895
  1505
    return 0;
slouken@1895
  1506
}
slouken@1895
  1507
slouken@1895
  1508
void
slouken@1895
  1509
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
slouken@1895
  1510
{
slouken@2202
  1511
    if (!overlay) {
slouken@2202
  1512
        return;
slouken@2202
  1513
    }
slouken@2202
  1514
    if (overlay->hwdata) {
slouken@3685
  1515
        if (overlay->hwdata->texture) {
slouken@5169
  1516
            SDL_SW_DestroyYUVTexture(overlay->hwdata->texture);
slouken@1895
  1517
        }
slouken@2202
  1518
        SDL_free(overlay->hwdata);
slouken@1895
  1519
    }
slouken@2202
  1520
    SDL_free(overlay);
slouken@1895
  1521
}
slouken@1895
  1522
slouken@1912
  1523
void
slouken@1912
  1524
SDL_GL_SwapBuffers(void)
slouken@1912
  1525
{
slouken@1912
  1526
    SDL_GL_SwapWindow(SDL_VideoWindow);
slouken@1912
  1527
}
slouken@1912
  1528
slouken@2129
  1529
slouken@2129
  1530
int
slouken@2129
  1531
SDL_EnableKeyRepeat(int delay, int interval)
slouken@2129
  1532
{
slouken@2129
  1533
    return 0;
slouken@2129
  1534
}
slouken@2129
  1535
slouken@2129
  1536
void
slouken@2129
  1537
SDL_GetKeyRepeat(int *delay, int *interval)
slouken@2129
  1538
{
slouken@2129
  1539
    if (delay) {
slouken@2129
  1540
        *delay = SDL_DEFAULT_REPEAT_DELAY;
slouken@2129
  1541
    }
slouken@2129
  1542
    if (interval) {
slouken@2129
  1543
        *interval = SDL_DEFAULT_REPEAT_INTERVAL;
slouken@2129
  1544
    }
slouken@2129
  1545
}
slouken@2129
  1546
slouken@2303
  1547
int
slouken@2303
  1548
SDL_EnableUNICODE(int enable)
slouken@2303
  1549
{
slouken@3280
  1550
    int previous = SDL_enabled_UNICODE;
slouken@3280
  1551
slouken@3280
  1552
    switch (enable) {
slouken@3280
  1553
    case 1:
slouken@3280
  1554
        SDL_enabled_UNICODE = 1;
dewyatt@4753
  1555
        SDL_StartTextInput();
slouken@3280
  1556
        break;
slouken@3280
  1557
    case 0:
slouken@3280
  1558
        SDL_enabled_UNICODE = 0;
dewyatt@4753
  1559
        SDL_StopTextInput();
slouken@3280
  1560
        break;
slouken@3280
  1561
    }
slouken@3280
  1562
    return previous;
slouken@2303
  1563
}
slouken@2303
  1564
slouken@5111
  1565
static Uint32
slouken@5111
  1566
SDL_SetTimerCallback(Uint32 interval, void* param)
slouken@5111
  1567
{
slouken@5111
  1568
    return ((SDL_OldTimerCallback)param)(interval);
slouken@5111
  1569
}
slouken@5111
  1570
slouken@5111
  1571
int
slouken@5111
  1572
SDL_SetTimer(Uint32 interval, SDL_OldTimerCallback callback)
slouken@5111
  1573
{
slouken@5111
  1574
    static SDL_TimerID compat_timer;
slouken@5111
  1575
slouken@5111
  1576
    if (compat_timer) {
slouken@5111
  1577
        SDL_RemoveTimer(compat_timer);
slouken@5111
  1578
        compat_timer = 0;
slouken@5111
  1579
    }
slouken@5111
  1580
slouken@5111
  1581
    if (interval && callback) {
slouken@5111
  1582
        compat_timer = SDL_AddTimer(interval, SDL_SetTimerCallback, callback);
slouken@5111
  1583
        if (!compat_timer) {
slouken@5111
  1584
            return -1;
slouken@5111
  1585
        }
slouken@5111
  1586
    }
slouken@5111
  1587
    return 0;
slouken@5111
  1588
}
slouken@5111
  1589
icculus@3581
  1590
int
icculus@3581
  1591
SDL_putenv(const char *_var)
icculus@3581
  1592
{
icculus@3581
  1593
    char *ptr = NULL;
icculus@3581
  1594
    char *var = SDL_strdup(_var);
icculus@3581
  1595
    if (var == NULL) {
icculus@3581
  1596
        return -1;  /* we don't set errno. */
icculus@3581
  1597
    }
icculus@3581
  1598
slouken@3592
  1599
    ptr = SDL_strchr(var, '=');
icculus@3581
  1600
    if (ptr == NULL) {
icculus@3581
  1601
        SDL_free(var);
icculus@3581
  1602
        return -1;
icculus@3581
  1603
    }
icculus@3581
  1604
icculus@3581
  1605
    *ptr = '\0';  /* split the string into name and value. */
icculus@3581
  1606
    SDL_setenv(var, ptr + 1, 1);
icculus@3581
  1607
    SDL_free(var);
icculus@3581
  1608
    return 0;
icculus@3581
  1609
}
icculus@3581
  1610
slouken@1895
  1611
/* vi: set ts=4 sw=4 expandtab: */