src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 08 Jul 2006 20:07:08 +0000
branchSDL-1.3
changeset 1731 875c3cf1a12c
parent 1724 6c63fc2bd986
child 1733 0b1070f2f94d
permissions -rw-r--r--
SDL_PushEvent() calls the event filter code, and has a return value to tell
whether or not the event was actually pushed.
SDL_GetEventFilter() now returns an SDL_bool instead of the filter function.
slouken@1667
     1
/*
slouken@1667
     2
    SDL - Simple DirectMedia Layer
slouken@1667
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1667
     4
slouken@1667
     5
    This library is free software; you can redistribute it and/or
slouken@1667
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1667
     7
    License as published by the Free Software Foundation; either
slouken@1667
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1667
     9
slouken@1667
    10
    This library is distributed in the hope that it will be useful,
slouken@1667
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1667
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1667
    13
    Lesser General Public License for more details.
slouken@1667
    14
slouken@1667
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1667
    16
    License along with this library; if not, write to the Free Software
slouken@1667
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1667
    18
slouken@1667
    19
    Sam Lantinga
slouken@1667
    20
    slouken@libsdl.org
slouken@1667
    21
*/
slouken@1667
    22
#include "SDL_config.h"
slouken@1667
    23
slouken@1667
    24
/* This file contains functions for backwards compatibility with SDL 1.2 */
slouken@1667
    25
slouken@1667
    26
#include "SDL.h"
slouken@1712
    27
#include "SDL_syswm.h"
slouken@1667
    28
slouken@1720
    29
#include "video/SDL_sysvideo.h"
slouken@1678
    30
#include "video/SDL_pixels_c.h"
slouken@1667
    31
slouken@1667
    32
slouken@1678
    33
static SDL_WindowID SDL_VideoWindow;
slouken@1678
    34
static SDL_TextureID SDL_VideoTexture;
slouken@1678
    35
static SDL_Surface *SDL_VideoSurface;
slouken@1678
    36
static SDL_Surface *SDL_ShadowSurface;
slouken@1678
    37
static SDL_Surface *SDL_PublicSurface;
slouken@1667
    38
static char *wm_title;
slouken@1667
    39
slouken@1667
    40
char *
slouken@1668
    41
SDL_AudioDriverName(char *namebuf, int maxlen)
slouken@1667
    42
{
slouken@1668
    43
    const char *name = SDL_GetCurrentAudioDriver();
slouken@1667
    44
    if (name) {
slouken@1668
    45
        SDL_strlcpy(namebuf, name, maxlen);
slouken@1667
    46
        return namebuf;
slouken@1667
    47
    }
slouken@1667
    48
    return NULL;
slouken@1667
    49
}
slouken@1667
    50
slouken@1667
    51
char *
slouken@1668
    52
SDL_VideoDriverName(char *namebuf, int maxlen)
slouken@1667
    53
{
slouken@1668
    54
    const char *name = SDL_GetCurrentVideoDriver();
slouken@1667
    55
    if (name) {
slouken@1668
    56
        SDL_strlcpy(namebuf, name, maxlen);
slouken@1667
    57
        return namebuf;
slouken@1667
    58
    }
slouken@1667
    59
    return NULL;
slouken@1667
    60
}
slouken@1667
    61
slouken@1670
    62
const SDL_VideoInfo *
slouken@1670
    63
SDL_GetVideoInfo(void)
slouken@1670
    64
{
slouken@1670
    65
    static SDL_VideoInfo info;
slouken@1670
    66
slouken@1670
    67
    /* Memory leak, compatibility code, who cares? */
slouken@1670
    68
    if (!info.vfmt && SDL_GetDesktopDisplayMode()) {
slouken@1670
    69
        int bpp;
slouken@1670
    70
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
    71
slouken@1670
    72
        SDL_PixelFormatEnumToMasks(SDL_GetDesktopDisplayMode()->format, &bpp,
slouken@1670
    73
                                   &Rmask, &Gmask, &Bmask, &Amask);
slouken@1670
    74
        info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
    75
    }
slouken@1670
    76
    return &info;
slouken@1670
    77
}
slouken@1670
    78
slouken@1667
    79
int
slouken@1668
    80
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
slouken@1667
    81
{
slouken@1667
    82
    int i, actual_bpp = 0;
slouken@1667
    83
slouken@1668
    84
    if (!SDL_GetVideoDevice()) {
slouken@1667
    85
        return 0;
slouken@1667
    86
    }
slouken@1667
    87
slouken@1667
    88
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1668
    89
        return SDL_BITSPERPIXEL(SDL_GetDesktopDisplayMode()->format);
slouken@1667
    90
    }
slouken@1667
    91
slouken@1668
    92
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1668
    93
        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
slouken@1667
    94
        if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
slouken@1667
    95
            if (!mode->format) {
slouken@1667
    96
                return bpp;
slouken@1667
    97
            }
slouken@1720
    98
            if (SDL_BITSPERPIXEL(mode->format) >= (Uint32) bpp) {
slouken@1668
    99
                actual_bpp = SDL_BITSPERPIXEL(mode->format);
slouken@1667
   100
            }
slouken@1667
   101
        }
slouken@1667
   102
    }
slouken@1667
   103
    return actual_bpp;
slouken@1667
   104
}
slouken@1667
   105
slouken@1667
   106
SDL_Rect **
slouken@1668
   107
SDL_ListModes(SDL_PixelFormat * format, Uint32 flags)
slouken@1667
   108
{
slouken@1667
   109
    int i, nmodes;
slouken@1667
   110
    SDL_Rect **modes;
slouken@1667
   111
slouken@1668
   112
    if (!SDL_GetVideoDevice()) {
slouken@1667
   113
        return NULL;
slouken@1667
   114
    }
slouken@1667
   115
slouken@1667
   116
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1667
   117
        return (SDL_Rect **) (-1);
slouken@1667
   118
    }
slouken@1667
   119
slouken@1667
   120
    /* Memory leak, but this is a compatibility function, who cares? */
slouken@1667
   121
    nmodes = 0;
slouken@1720
   122
    modes = NULL;
slouken@1668
   123
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1668
   124
        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
slouken@1667
   125
        if (!mode->w || !mode->h) {
slouken@1667
   126
            return (SDL_Rect **) (-1);
slouken@1667
   127
        }
slouken@1668
   128
        if (SDL_BITSPERPIXEL(mode->format) != format->BitsPerPixel) {
slouken@1667
   129
            continue;
slouken@1667
   130
        }
slouken@1667
   131
        if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
slouken@1667
   132
            && modes[nmodes - 1]->h == mode->h) {
slouken@1667
   133
            continue;
slouken@1667
   134
        }
slouken@1667
   135
slouken@1668
   136
        modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
slouken@1667
   137
        if (!modes) {
slouken@1667
   138
            return NULL;
slouken@1667
   139
        }
slouken@1668
   140
        modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
slouken@1667
   141
        if (!modes[nmodes]) {
slouken@1667
   142
            return NULL;
slouken@1667
   143
        }
slouken@1667
   144
        modes[nmodes]->x = 0;
slouken@1667
   145
        modes[nmodes]->y = 0;
slouken@1667
   146
        modes[nmodes]->w = mode->w;
slouken@1667
   147
        modes[nmodes]->h = mode->h;
slouken@1667
   148
        ++nmodes;
slouken@1667
   149
    }
slouken@1667
   150
    if (modes) {
slouken@1667
   151
        modes[nmodes] = NULL;
slouken@1667
   152
    }
slouken@1667
   153
    return modes;
slouken@1667
   154
}
slouken@1667
   155
slouken@1669
   156
static int
slouken@1722
   157
SDL_CompatEventFilter(void *userdata, SDL_Event * event)
slouken@1669
   158
{
slouken@1669
   159
    SDL_Event fake;
slouken@1669
   160
slouken@1669
   161
    switch (event->type) {
slouken@1669
   162
    case SDL_WINDOWEVENT:
slouken@1669
   163
        switch (event->window.event) {
slouken@1724
   164
        case SDL_WINDOWEVENT_EXPOSED:
slouken@1724
   165
            if (!SDL_HasEvent(SDL_VIDEOEXPOSEMASK)) {
slouken@1724
   166
                fake.type = SDL_VIDEOEXPOSE;
slouken@1724
   167
                SDL_PushEvent(&fake);
slouken@1724
   168
            }
slouken@1724
   169
            break;
slouken@1669
   170
        case SDL_WINDOWEVENT_RESIZED:
slouken@1669
   171
            fake.type = SDL_VIDEORESIZE;
slouken@1669
   172
            fake.resize.w = event->window.data1;
slouken@1669
   173
            fake.resize.h = event->window.data2;
slouken@1669
   174
            SDL_PushEvent(&fake);
slouken@1669
   175
            break;
slouken@1669
   176
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1669
   177
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   178
            fake.active.gain = 0;
slouken@1669
   179
            fake.active.state = SDL_APPACTIVE;
slouken@1669
   180
            SDL_PushEvent(&fake);
slouken@1669
   181
            break;
slouken@1669
   182
        case SDL_WINDOWEVENT_RESTORED:
slouken@1669
   183
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   184
            fake.active.gain = 1;
slouken@1669
   185
            fake.active.state = SDL_APPACTIVE;
slouken@1669
   186
            SDL_PushEvent(&fake);
slouken@1669
   187
            break;
slouken@1669
   188
        case SDL_WINDOWEVENT_ENTER:
slouken@1669
   189
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   190
            fake.active.gain = 1;
slouken@1669
   191
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1669
   192
            SDL_PushEvent(&fake);
slouken@1669
   193
            break;
slouken@1669
   194
        case SDL_WINDOWEVENT_LEAVE:
slouken@1669
   195
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   196
            fake.active.gain = 0;
slouken@1669
   197
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1669
   198
            SDL_PushEvent(&fake);
slouken@1669
   199
            break;
slouken@1669
   200
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1669
   201
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   202
            fake.active.gain = 1;
slouken@1669
   203
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1669
   204
            SDL_PushEvent(&fake);
slouken@1669
   205
            break;
slouken@1669
   206
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1669
   207
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   208
            fake.active.gain = 1;
slouken@1669
   209
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1669
   210
            SDL_PushEvent(&fake);
slouken@1669
   211
            break;
slouken@1724
   212
        case SDL_WINDOWEVENT_CLOSE:
slouken@1724
   213
            fake.type = SDL_QUIT;
slouken@1724
   214
            SDL_PushEvent(&fake);
slouken@1724
   215
            break;
slouken@1669
   216
        }
slouken@1686
   217
    case SDL_KEYDOWN:
slouken@1686
   218
    case SDL_KEYUP:
slouken@1686
   219
        {
slouken@1686
   220
            Uint32 unicode = 0;
slouken@1686
   221
            if (event->key.type == SDL_KEYDOWN && event->key.keysym.sym < 256) {
slouken@1687
   222
                unicode = event->key.keysym.sym;
slouken@1691
   223
                if (unicode >= 'a' && unicode <= 'z') {
slouken@1687
   224
                    int shifted = !!(event->key.keysym.mod & KMOD_SHIFT);
slouken@1687
   225
                    int capslock = !!(event->key.keysym.mod & KMOD_CAPS);
slouken@1687
   226
                    if ((shifted ^ capslock) != 0) {
slouken@1687
   227
                        unicode = SDL_toupper(unicode);
slouken@1687
   228
                    }
slouken@1686
   229
                }
slouken@1686
   230
            }
slouken@1686
   231
            if (unicode) {
slouken@1686
   232
                event->key.keysym.unicode = unicode;
slouken@1686
   233
            }
slouken@1686
   234
            break;
slouken@1686
   235
        }
slouken@1724
   236
    case SDL_MOUSEWHEEL:
slouken@1724
   237
        {
slouken@1724
   238
            Uint8 button;
slouken@1724
   239
            int selected;
slouken@1724
   240
            int x, y;
slouken@1724
   241
slouken@1724
   242
            selected = SDL_SelectMouse(event->wheel.which);
slouken@1724
   243
            SDL_GetMouseState(&x, &y);
slouken@1724
   244
            SDL_SelectMouse(selected);
slouken@1724
   245
slouken@1724
   246
            if (event->wheel.motion > 0) {
slouken@1724
   247
                button = SDL_BUTTON_WHEELUP;
slouken@1724
   248
            } else {
slouken@1724
   249
                button = SDL_BUTTON_WHEELDOWN;
slouken@1724
   250
            }
slouken@1724
   251
slouken@1724
   252
            fake.button.which = event->wheel.windowID;
slouken@1724
   253
            fake.button.button = button;
slouken@1724
   254
            fake.button.x = x;
slouken@1724
   255
            fake.button.y = y;
slouken@1724
   256
            fake.button.windowID = event->wheel.windowID;
slouken@1724
   257
slouken@1724
   258
            fake.type = SDL_MOUSEBUTTONDOWN;
slouken@1724
   259
            fake.button.state = SDL_PRESSED;
slouken@1724
   260
            SDL_PushEvent(&fake);
slouken@1724
   261
slouken@1724
   262
            fake.type = SDL_MOUSEBUTTONUP;
slouken@1724
   263
            fake.button.state = SDL_RELEASED;
slouken@1724
   264
            SDL_PushEvent(&fake);
slouken@1724
   265
            break;
slouken@1724
   266
        }
slouken@1724
   267
slouken@1669
   268
    }
slouken@1731
   269
    return 1;
slouken@1683
   270
}
slouken@1683
   271
slouken@1683
   272
static int
slouken@1683
   273
SDL_VideoPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1683
   274
{
slouken@1683
   275
    if (userdata == SDL_ShadowSurface) {
slouken@1683
   276
        /* If the shadow palette changed, make the changes visible */
slouken@1683
   277
        if (!SDL_VideoSurface->format->palette) {
slouken@1683
   278
            SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
slouken@1683
   279
        }
slouken@1683
   280
    }
slouken@1683
   281
    if (userdata == SDL_VideoSurface) {
slouken@1683
   282
        return SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors);
slouken@1683
   283
    }
slouken@1720
   284
    return 0;
slouken@1669
   285
}
slouken@1669
   286
slouken@1731
   287
static void
slouken@1731
   288
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
slouken@1731
   289
{
slouken@1731
   290
    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
slouken@1731
   291
    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
slouken@1731
   292
    if (window) {
slouken@1731
   293
        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
slouken@1731
   294
            return;
slouken@1731
   295
        }
slouken@1731
   296
        if (SDL_strcmp(window, "center") == 0) {
slouken@1731
   297
            center = window;
slouken@1731
   298
        }
slouken@1731
   299
    }
slouken@1731
   300
    if (center) {
slouken@1731
   301
        const SDL_DisplayMode *current = SDL_GetDesktopDisplayMode();
slouken@1731
   302
        *x = (current->w - w) / 2;
slouken@1731
   303
        *y = (current->h - h) / 2;
slouken@1731
   304
    }
slouken@1731
   305
}
slouken@1731
   306
slouken@1667
   307
SDL_Surface *
slouken@1668
   308
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
slouken@1667
   309
{
slouken@1722
   310
    SDL_EventFilter filter;
slouken@1722
   311
    void *filterparam;
slouken@1669
   312
    const SDL_DisplayMode *desktop_mode;
slouken@1667
   313
    SDL_DisplayMode mode;
slouken@1731
   314
    int window_x = SDL_WINDOWPOS_UNDEFINED;
slouken@1731
   315
    int window_y = SDL_WINDOWPOS_UNDEFINED;
slouken@1667
   316
    Uint32 window_flags;
slouken@1667
   317
    Uint32 desktop_format;
slouken@1667
   318
    Uint32 desired_format;
slouken@1682
   319
    Uint32 surface_flags;
slouken@1667
   320
slouken@1668
   321
    if (!SDL_GetVideoDevice()) {
slouken@1668
   322
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
slouken@1667
   323
            return NULL;
slouken@1667
   324
        }
slouken@1667
   325
    }
slouken@1667
   326
slouken@1667
   327
    /* Destroy existing window */
slouken@1678
   328
    SDL_PublicSurface = NULL;
slouken@1678
   329
    if (SDL_ShadowSurface) {
slouken@1678
   330
        SDL_FreeSurface(SDL_ShadowSurface);
slouken@1678
   331
        SDL_ShadowSurface = NULL;
slouken@1678
   332
    }
slouken@1678
   333
    if (SDL_VideoSurface) {
slouken@1683
   334
        SDL_DelPaletteWatch(SDL_VideoSurface->format->palette,
slouken@1683
   335
                            SDL_VideoPaletteChanged, NULL);
slouken@1683
   336
        SDL_FreeSurface(SDL_VideoSurface);
slouken@1678
   337
        SDL_VideoSurface = NULL;
slouken@1678
   338
    }
slouken@1731
   339
    if (SDL_VideoWindow) {
slouken@1731
   340
        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
slouken@1731
   341
    }
slouken@1678
   342
    SDL_DestroyWindow(SDL_VideoWindow);
slouken@1667
   343
slouken@1669
   344
    /* Set up the event filter */
slouken@1731
   345
    if (!SDL_GetEventFilter(NULL, NULL)) {
slouken@1731
   346
        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
slouken@1669
   347
    }
slouken@1669
   348
slouken@1667
   349
    /* Create a new window */
slouken@1667
   350
    window_flags = SDL_WINDOW_SHOWN;
slouken@1667
   351
    if (flags & SDL_FULLSCREEN) {
slouken@1667
   352
        window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1667
   353
    }
slouken@1667
   354
    if (flags & SDL_OPENGL) {
slouken@1667
   355
        window_flags |= SDL_WINDOW_OPENGL;
slouken@1667
   356
    }
slouken@1667
   357
    if (flags & SDL_RESIZABLE) {
slouken@1667
   358
        window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1667
   359
    }
slouken@1667
   360
    if (flags & SDL_NOFRAME) {
slouken@1667
   361
        window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1667
   362
    }
slouken@1731
   363
    if (SDL_getenv("SDL_WINDOW_POS")) {
slouken@1731
   364
    }
slouken@1731
   365
    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
slouken@1678
   366
    SDL_VideoWindow =
slouken@1731
   367
        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
slouken@1724
   368
                         window_flags);
slouken@1678
   369
    if (!SDL_VideoWindow) {
slouken@1667
   370
        return NULL;
slouken@1667
   371
    }
slouken@1667
   372
slouken@1682
   373
    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1716
   374
    surface_flags = 0;
slouken@1682
   375
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1682
   376
        surface_flags |= SDL_FULLSCREEN;
slouken@1682
   377
    }
slouken@1682
   378
    if (window_flags & SDL_WINDOW_OPENGL) {
slouken@1682
   379
        surface_flags |= SDL_OPENGL;
slouken@1682
   380
    }
slouken@1682
   381
    if (window_flags & SDL_WINDOW_RESIZABLE) {
slouken@1682
   382
        surface_flags |= SDL_RESIZABLE;
slouken@1682
   383
    }
slouken@1682
   384
    if (window_flags & SDL_WINDOW_BORDERLESS) {
slouken@1682
   385
        surface_flags |= SDL_NOFRAME;
slouken@1682
   386
    }
slouken@1682
   387
slouken@1667
   388
    /* Set up the desired display mode */
slouken@1669
   389
    desktop_mode = SDL_GetDesktopDisplayMode();
slouken@1669
   390
    desktop_format = desktop_mode->format;
slouken@1669
   391
    if (desktop_format && ((flags & SDL_ANYFORMAT)
slouken@1669
   392
                           || (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
slouken@1667
   393
        desired_format = desktop_format;
slouken@1667
   394
    } else {
slouken@1667
   395
        switch (bpp) {
slouken@1669
   396
        case 0:
slouken@1669
   397
            if (desktop_format) {
slouken@1669
   398
                desired_format = desktop_format;
slouken@1669
   399
            } else {
slouken@1669
   400
                desired_format = SDL_PixelFormat_RGB888;
slouken@1669
   401
            }
slouken@1669
   402
            break;
slouken@1667
   403
        case 8:
slouken@1667
   404
            desired_format = SDL_PixelFormat_Index8;
slouken@1667
   405
            break;
slouken@1667
   406
        case 15:
slouken@1667
   407
            desired_format = SDL_PixelFormat_RGB555;
slouken@1667
   408
            break;
slouken@1667
   409
        case 16:
slouken@1667
   410
            desired_format = SDL_PixelFormat_RGB565;
slouken@1667
   411
            break;
slouken@1667
   412
        case 24:
slouken@1667
   413
            desired_format = SDL_PixelFormat_RGB24;
slouken@1667
   414
            break;
slouken@1667
   415
        case 32:
slouken@1667
   416
            desired_format = SDL_PixelFormat_RGB888;
slouken@1667
   417
            break;
slouken@1667
   418
        default:
slouken@1668
   419
            SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
slouken@1667
   420
            return NULL;
slouken@1667
   421
        }
slouken@1667
   422
    }
slouken@1667
   423
    mode.format = desired_format;
slouken@1667
   424
    mode.w = width;
slouken@1667
   425
    mode.h = height;
slouken@1667
   426
    mode.refresh_rate = 0;
slouken@1667
   427
slouken@1667
   428
    /* Set the desired display mode */
slouken@1667
   429
    if (flags & SDL_FULLSCREEN) {
slouken@1668
   430
        if (!SDL_GetClosestDisplayMode(&mode, &mode)) {
slouken@1667
   431
            return NULL;
slouken@1667
   432
        }
slouken@1667
   433
    } else {
slouken@1669
   434
        if (desktop_format) {
slouken@1669
   435
            mode.format = desktop_format;
slouken@1669
   436
        }
slouken@1669
   437
        if (desktop_mode->w && desktop_mode->h) {
slouken@1669
   438
            mode.w = desktop_mode->w;
slouken@1669
   439
            mode.h = desktop_mode->h;
slouken@1669
   440
        }
slouken@1669
   441
        mode.refresh_rate = desktop_mode->refresh_rate;
slouken@1667
   442
    }
slouken@1668
   443
    if (SDL_SetDisplayMode(&mode) < 0) {
slouken@1667
   444
        return NULL;
slouken@1667
   445
    }
slouken@1667
   446
slouken@1682
   447
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
slouken@1682
   448
    if (flags & SDL_OPENGL) {
slouken@1682
   449
        SDL_VideoSurface =
slouken@1682
   450
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
slouken@1682
   451
        if (!SDL_VideoSurface) {
slouken@1682
   452
            return NULL;
slouken@1682
   453
        }
slouken@1682
   454
        SDL_VideoSurface->flags |= surface_flags;
slouken@1682
   455
        SDL_PublicSurface = SDL_VideoSurface;
slouken@1682
   456
        return SDL_PublicSurface;
slouken@1682
   457
    }
slouken@1682
   458
slouken@1678
   459
    /* Create a renderer for the window */
slouken@1678
   460
    if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) {
slouken@1670
   461
        return NULL;
slouken@1670
   462
    }
slouken@1670
   463
slouken@1678
   464
    /* Create a texture for the screen surface */
slouken@1678
   465
    SDL_VideoTexture =
slouken@1678
   466
        SDL_CreateTexture(desired_format, SDL_TextureAccess_Local, width,
slouken@1678
   467
                          height);
slouken@1678
   468
    if (!SDL_VideoTexture) {
slouken@1678
   469
        SDL_VideoTexture =
slouken@1678
   470
            SDL_CreateTexture(0, SDL_TextureAccess_Local, width, height);
slouken@1678
   471
    }
slouken@1678
   472
    if (!SDL_VideoTexture) {
slouken@1670
   473
        return NULL;
slouken@1670
   474
    }
slouken@1670
   475
slouken@1678
   476
    /* Create the screen surface */
slouken@1678
   477
    SDL_VideoSurface = SDL_CreateRGBSurfaceFromTexture(SDL_VideoTexture);
slouken@1678
   478
    if (!SDL_VideoSurface) {
slouken@1678
   479
        return NULL;
slouken@1678
   480
    }
slouken@1682
   481
    SDL_VideoSurface->flags |= surface_flags;
slouken@1670
   482
slouken@1678
   483
    /* Set a default screen palette */
slouken@1678
   484
    if (SDL_VideoSurface->format->palette) {
slouken@1678
   485
        SDL_VideoSurface->flags |= SDL_HWPALETTE;
slouken@1678
   486
        SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
slouken@1678
   487
                         SDL_VideoSurface->format->BitsPerPixel);
slouken@1683
   488
        SDL_AddPaletteWatch(SDL_VideoSurface->format->palette,
slouken@1683
   489
                            SDL_VideoPaletteChanged, NULL);
slouken@1683
   490
        SDL_SetPaletteColors(SDL_VideoSurface->format->palette,
slouken@1683
   491
                             SDL_VideoSurface->format->palette->colors, 0,
slouken@1683
   492
                             SDL_VideoSurface->format->palette->ncolors);
slouken@1670
   493
    }
slouken@1670
   494
slouken@1670
   495
    /* Create a shadow surface if necessary */
slouken@1678
   496
    if (((bpp != SDL_VideoSurface->format->BitsPerPixel)
slouken@1678
   497
         && !(flags & SDL_ANYFORMAT))
slouken@1678
   498
        || ((SDL_VideoSurface->flags & SDL_HWSURFACE)
slouken@1678
   499
            && !(flags & SDL_HWSURFACE))) {
slouken@1678
   500
        if ((bpp == SDL_VideoSurface->format->BitsPerPixel)
slouken@1678
   501
            || (flags & SDL_ANYFORMAT)) {
slouken@1678
   502
            SDL_ShadowSurface =
slouken@1678
   503
                SDL_CreateRGBSurface(0, width, height,
slouken@1678
   504
                                     SDL_VideoSurface->format->BitsPerPixel,
slouken@1678
   505
                                     SDL_VideoSurface->format->Rmask,
slouken@1678
   506
                                     SDL_VideoSurface->format->Gmask,
slouken@1678
   507
                                     SDL_VideoSurface->format->Bmask,
slouken@1678
   508
                                     SDL_VideoSurface->format->Amask);
slouken@1678
   509
        } else {
slouken@1678
   510
            SDL_ShadowSurface =
slouken@1678
   511
                SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
slouken@1678
   512
        }
slouken@1678
   513
        if (!SDL_ShadowSurface) {
slouken@1670
   514
            return NULL;
slouken@1670
   515
        }
slouken@1682
   516
        SDL_ShadowSurface->flags |= surface_flags;
slouken@1670
   517
slouken@1678
   518
        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
slouken@1678
   519
        if (SDL_ShadowSurface->format->palette) {
slouken@1678
   520
            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
slouken@1683
   521
            if (SDL_VideoSurface->format->palette) {
slouken@1683
   522
                SDL_SetSurfacePalette(SDL_ShadowSurface,
slouken@1683
   523
                                      SDL_VideoSurface->format->palette);
slouken@1683
   524
            } else {
slouken@1683
   525
                SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
slouken@1683
   526
                                 SDL_ShadowSurface->format->BitsPerPixel);
slouken@1683
   527
            }
slouken@1670
   528
        }
slouken@1670
   529
    }
slouken@1678
   530
    SDL_PublicSurface =
slouken@1678
   531
        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
slouken@1670
   532
slouken@1670
   533
    /* Clear the surface for display */
slouken@1678
   534
    SDL_FillRect(SDL_PublicSurface, NULL, 0);
slouken@1670
   535
slouken@1678
   536
    /* We're finally done! */
slouken@1678
   537
    return SDL_PublicSurface;
slouken@1670
   538
}
slouken@1670
   539
slouken@1670
   540
SDL_Surface *
slouken@1668
   541
SDL_GetVideoSurface(void)
slouken@1667
   542
{
slouken@1678
   543
    return SDL_PublicSurface;
slouken@1667
   544
}
slouken@1667
   545
slouken@1670
   546
SDL_Surface *
slouken@1670
   547
SDL_DisplayFormat(SDL_Surface * surface)
slouken@1670
   548
{
slouken@1670
   549
    Uint32 flags;
slouken@1670
   550
slouken@1670
   551
    if (!SDL_PublicSurface) {
slouken@1670
   552
        SDL_SetError("No video mode has been set");
slouken@1678
   553
        return NULL;
slouken@1670
   554
    }
slouken@1678
   555
slouken@1670
   556
    /* Set the flags appropriate for copying to display surface */
slouken@1678
   557
    flags = SDL_SWSURFACE;
slouken@1670
   558
#ifdef AUTORLE_DISPLAYFORMAT
slouken@1670
   559
    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
slouken@1670
   560
    flags |= SDL_RLEACCELOK;
slouken@1670
   561
#else
slouken@1670
   562
    flags |=
slouken@1670
   563
        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
   564
#endif
slouken@1678
   565
    return SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags);
slouken@1670
   566
}
slouken@1670
   567
slouken@1670
   568
SDL_Surface *
slouken@1670
   569
SDL_DisplayFormatAlpha(SDL_Surface * surface)
slouken@1670
   570
{
slouken@1670
   571
    SDL_PixelFormat *vf;
slouken@1670
   572
    SDL_PixelFormat *format;
slouken@1670
   573
    SDL_Surface *converted;
slouken@1670
   574
    Uint32 flags;
slouken@1670
   575
    /* default to ARGB8888 */
slouken@1670
   576
    Uint32 amask = 0xff000000;
slouken@1670
   577
    Uint32 rmask = 0x00ff0000;
slouken@1670
   578
    Uint32 gmask = 0x0000ff00;
slouken@1670
   579
    Uint32 bmask = 0x000000ff;
slouken@1670
   580
slouken@1670
   581
    if (!SDL_PublicSurface) {
slouken@1670
   582
        SDL_SetError("No video mode has been set");
slouken@1678
   583
        return NULL;
slouken@1670
   584
    }
slouken@1670
   585
    vf = SDL_PublicSurface->format;
slouken@1670
   586
slouken@1670
   587
    switch (vf->BytesPerPixel) {
slouken@1670
   588
    case 2:
slouken@1670
   589
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1670
   590
           For anything else (like ARGB4444) it doesn't matter
slouken@1670
   591
           since we have no special code for it anyway */
slouken@1670
   592
        if ((vf->Rmask == 0x1f) &&
slouken@1670
   593
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1670
   594
            rmask = 0xff;
slouken@1670
   595
            bmask = 0xff0000;
slouken@1670
   596
        }
slouken@1670
   597
        break;
slouken@1670
   598
slouken@1670
   599
    case 3:
slouken@1670
   600
    case 4:
slouken@1670
   601
        /* Keep the video format, as long as the high 8 bits are
slouken@1670
   602
           unused or alpha */
slouken@1670
   603
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1670
   604
            rmask = 0xff;
slouken@1670
   605
            bmask = 0xff0000;
slouken@1670
   606
        }
slouken@1670
   607
        break;
slouken@1670
   608
slouken@1670
   609
    default:
slouken@1670
   610
        /* We have no other optimised formats right now. When/if a new
slouken@1670
   611
           optimised alpha format is written, add the converter here */
slouken@1670
   612
        break;
slouken@1670
   613
    }
slouken@1670
   614
    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
slouken@1670
   615
    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
slouken@1670
   616
    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
   617
    converted = SDL_ConvertSurface(surface, format, flags);
slouken@1670
   618
    SDL_FreeFormat(format);
slouken@1678
   619
    return converted;
slouken@1678
   620
}
slouken@1678
   621
slouken@1678
   622
int
slouken@1678
   623
SDL_Flip(SDL_Surface * screen)
slouken@1678
   624
{
slouken@1678
   625
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1678
   626
    return 0;
slouken@1670
   627
}
slouken@1670
   628
slouken@1670
   629
void
slouken@1670
   630
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@1670
   631
{
slouken@1670
   632
    if (screen) {
slouken@1670
   633
        SDL_Rect rect;
slouken@1670
   634
slouken@1670
   635
        /* Perform some checking */
slouken@1670
   636
        if (w == 0)
slouken@1670
   637
            w = screen->w;
slouken@1670
   638
        if (h == 0)
slouken@1670
   639
            h = screen->h;
slouken@1670
   640
        if ((int) (x + w) > screen->w)
slouken@1670
   641
            return;
slouken@1670
   642
        if ((int) (y + h) > screen->h)
slouken@1670
   643
            return;
slouken@1670
   644
slouken@1670
   645
        /* Fill the rectangle */
slouken@1670
   646
        rect.x = (Sint16) x;
slouken@1670
   647
        rect.y = (Sint16) y;
slouken@1670
   648
        rect.w = (Uint16) w;
slouken@1670
   649
        rect.h = (Uint16) h;
slouken@1670
   650
        SDL_UpdateRects(screen, 1, &rect);
slouken@1670
   651
    }
slouken@1670
   652
}
slouken@1670
   653
void
slouken@1670
   654
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@1670
   655
{
slouken@1670
   656
    int i;
slouken@1670
   657
slouken@1678
   658
    if (screen == SDL_ShadowSurface) {
slouken@1678
   659
        for (i = 0; i < numrects; ++i) {
slouken@1678
   660
            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
slouken@1678
   661
                          &rects[i]);
slouken@1670
   662
        }
slouken@1670
   663
slouken@1670
   664
        /* Fall through to video surface update */
slouken@1678
   665
        screen = SDL_VideoSurface;
slouken@1670
   666
    }
slouken@1678
   667
    if (screen == SDL_VideoSurface) {
slouken@1678
   668
        for (i = 0; i < numrects; ++i) {
slouken@1678
   669
            SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i],
slouken@1678
   670
                           SDL_TextureBlendMode_None,
slouken@1678
   671
                           SDL_TextureScaleMode_None);
slouken@1670
   672
        }
slouken@1678
   673
        SDL_RenderPresent();
slouken@1670
   674
    }
slouken@1670
   675
}
slouken@1670
   676
slouken@1667
   677
void
slouken@1668
   678
SDL_WM_SetCaption(const char *title, const char *icon)
slouken@1667
   679
{
slouken@1667
   680
    if (wm_title) {
slouken@1668
   681
        SDL_free(wm_title);
slouken@1667
   682
    } else {
slouken@1668
   683
        wm_title = SDL_strdup(title);
slouken@1667
   684
    }
slouken@1678
   685
    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
slouken@1667
   686
}
slouken@1667
   687
slouken@1667
   688
void
slouken@1668
   689
SDL_WM_GetCaption(char **title, char **icon)
slouken@1667
   690
{
slouken@1667
   691
    if (title) {
slouken@1667
   692
        *title = wm_title;
slouken@1667
   693
    }
slouken@1667
   694
    if (icon) {
slouken@1667
   695
        *icon = "";
slouken@1667
   696
    }
slouken@1667
   697
}
slouken@1667
   698
slouken@1667
   699
void
slouken@1668
   700
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@1667
   701
{
slouken@1667
   702
    /* FIXME */
slouken@1667
   703
}
slouken@1667
   704
slouken@1667
   705
int
slouken@1668
   706
SDL_WM_IconifyWindow(void)
slouken@1667
   707
{
slouken@1678
   708
    SDL_MinimizeWindow(SDL_VideoWindow);
slouken@1720
   709
    return 0;
slouken@1667
   710
}
slouken@1667
   711
slouken@1667
   712
int
slouken@1668
   713
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
slouken@1667
   714
{
slouken@1667
   715
    return 0;
slouken@1667
   716
}
slouken@1667
   717
slouken@1667
   718
SDL_GrabMode
slouken@1668
   719
SDL_WM_GrabInput(SDL_GrabMode mode)
slouken@1667
   720
{
slouken@1667
   721
    if (mode != SDL_GRAB_QUERY) {
slouken@1678
   722
        SDL_SetWindowGrab(SDL_VideoWindow, mode);
slouken@1667
   723
    }
slouken@1678
   724
    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
slouken@1667
   725
}
slouken@1667
   726
slouken@1680
   727
void
slouken@1680
   728
SDL_WarpMouse(Uint16 x, Uint16 y)
slouken@1680
   729
{
slouken@1680
   730
    SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
slouken@1680
   731
}
slouken@1680
   732
slouken@1667
   733
Uint8
slouken@1668
   734
SDL_GetAppState(void)
slouken@1667
   735
{
slouken@1667
   736
    Uint8 state = 0;
slouken@1667
   737
    Uint32 flags = 0;
slouken@1667
   738
slouken@1678
   739
    flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1667
   740
    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
slouken@1667
   741
        state |= SDL_APPACTIVE;
slouken@1667
   742
    }
slouken@1667
   743
    if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
slouken@1667
   744
        state |= SDL_APPINPUTFOCUS;
slouken@1667
   745
    }
slouken@1667
   746
    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1667
   747
        state |= SDL_APPMOUSEFOCUS;
slouken@1667
   748
    }
slouken@1667
   749
    return state;
slouken@1667
   750
}
slouken@1667
   751
slouken@1667
   752
const SDL_version *
slouken@1668
   753
SDL_Linked_Version(void)
slouken@1667
   754
{
slouken@1667
   755
    static SDL_version version;
slouken@1668
   756
    SDL_VERSION(&version);
slouken@1667
   757
    return &version;
slouken@1667
   758
}
slouken@1667
   759
slouken@1667
   760
int
slouken@1682
   761
SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
slouken@1668
   762
               int firstcolor, int ncolors)
slouken@1667
   763
{
slouken@1720
   764
    return SDL_SetColors(surface, colors, firstcolor, ncolors);
slouken@1667
   765
}
slouken@1667
   766
slouken@1667
   767
int
slouken@1683
   768
SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
slouken@1683
   769
              int ncolors)
slouken@1670
   770
{
slouken@1683
   771
    if (SDL_SetPaletteColors
slouken@1683
   772
        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
slouken@1683
   773
        return 1;
slouken@1683
   774
    } else {
slouken@1683
   775
        return 0;
slouken@1670
   776
    }
slouken@1670
   777
}
slouken@1670
   778
slouken@1670
   779
int
slouken@1668
   780
SDL_GetWMInfo(SDL_SysWMinfo * info)
slouken@1667
   781
{
slouken@1678
   782
    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
slouken@1667
   783
}
slouken@1667
   784
slouken@1678
   785
#if 0
slouken@1670
   786
void
slouken@1670
   787
SDL_MoveCursor(int x, int y)
slouken@1670
   788
{
slouken@1670
   789
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   790
slouken@1670
   791
    /* Erase and update the current mouse position */
slouken@1670
   792
    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1670
   793
        /* Erase and redraw mouse cursor in new position */
slouken@1670
   794
        SDL_LockCursor();
slouken@1670
   795
        SDL_EraseCursor(SDL_VideoSurface);
slouken@1670
   796
        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@1670
   797
        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@1670
   798
        SDL_DrawCursor(SDL_VideoSurface);
slouken@1670
   799
        SDL_UnlockCursor();
slouken@1670
   800
    } else if (_this->MoveWMCursor) {
slouken@1670
   801
        _this->MoveWMCursor(_this, x, y);
slouken@1670
   802
    }
slouken@1670
   803
}
slouken@1670
   804
slouken@1670
   805
/* Keep track of the current cursor colors */
slouken@1670
   806
static int palette_changed = 1;
slouken@1670
   807
static Uint8 pixels8[2];
slouken@1670
   808
slouken@1670
   809
void
slouken@1670
   810
SDL_CursorPaletteChanged(void)
slouken@1670
   811
{
slouken@1670
   812
    palette_changed = 1;
slouken@1670
   813
}
slouken@1670
   814
slouken@1670
   815
void
slouken@1670
   816
SDL_MouseRect(SDL_Rect * area)
slouken@1670
   817
{
slouken@1670
   818
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   819
    int clip_diff;
slouken@1670
   820
slouken@1670
   821
    *area = SDL_cursor->area;
slouken@1670
   822
    if (area->x < 0) {
slouken@1670
   823
        area->w += area->x;
slouken@1670
   824
        area->x = 0;
slouken@1670
   825
    }
slouken@1670
   826
    if (area->y < 0) {
slouken@1670
   827
        area->h += area->y;
slouken@1670
   828
        area->y = 0;
slouken@1670
   829
    }
slouken@1670
   830
    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
slouken@1670
   831
    if (clip_diff > 0) {
slouken@1670
   832
        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
slouken@1670
   833
    }
slouken@1670
   834
    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
slouken@1670
   835
    if (clip_diff > 0) {
slouken@1670
   836
        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
slouken@1670
   837
    }
slouken@1670
   838
}
slouken@1670
   839
slouken@1670
   840
static void
slouken@1670
   841
SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
slouken@1670
   842
{
slouken@1670
   843
    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
slouken@1670
   844
    int i, w, h;
slouken@1670
   845
    Uint8 *data, datab;
slouken@1670
   846
    Uint8 *mask, maskb;
slouken@1670
   847
slouken@1670
   848
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1670
   849
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1670
   850
    switch (screen->format->BytesPerPixel) {
slouken@1670
   851
slouken@1670
   852
    case 1:
slouken@1670
   853
        {
slouken@1670
   854
            Uint8 *dst;
slouken@1670
   855
            int dstskip;
slouken@1670
   856
slouken@1670
   857
            if (palette_changed) {
slouken@1670
   858
                pixels8[0] =
slouken@1670
   859
                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1670
   860
                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1670
   861
                palette_changed = 0;
slouken@1670
   862
            }
slouken@1670
   863
            dst = (Uint8 *) screen->pixels +
slouken@1670
   864
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   865
                SDL_cursor->area.x;
slouken@1670
   866
            dstskip = screen->pitch - area->w;
slouken@1670
   867
slouken@1670
   868
            for (h = area->h; h; h--) {
slouken@1670
   869
                for (w = area->w / 8; w; w--) {
slouken@1670
   870
                    maskb = *mask++;
slouken@1670
   871
                    datab = *data++;
slouken@1670
   872
                    for (i = 0; i < 8; ++i) {
slouken@1670
   873
                        if (maskb & 0x80) {
slouken@1670
   874
                            *dst = pixels8[datab >> 7];
slouken@1670
   875
                        }
slouken@1670
   876
                        maskb <<= 1;
slouken@1670
   877
                        datab <<= 1;
slouken@1670
   878
                        dst++;
slouken@1670
   879
                    }
slouken@1670
   880
                }
slouken@1670
   881
                dst += dstskip;
slouken@1670
   882
            }
slouken@1670
   883
        }
slouken@1670
   884
        break;
slouken@1670
   885
slouken@1670
   886
    case 2:
slouken@1670
   887
        {
slouken@1670
   888
            Uint16 *dst;
slouken@1670
   889
            int dstskip;
slouken@1670
   890
slouken@1670
   891
            dst = (Uint16 *) screen->pixels +
slouken@1670
   892
                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
slouken@1670
   893
                SDL_cursor->area.x;
slouken@1670
   894
            dstskip = (screen->pitch / 2) - area->w;
slouken@1670
   895
slouken@1670
   896
            for (h = area->h; h; h--) {
slouken@1670
   897
                for (w = area->w / 8; w; w--) {
slouken@1670
   898
                    maskb = *mask++;
slouken@1670
   899
                    datab = *data++;
slouken@1670
   900
                    for (i = 0; i < 8; ++i) {
slouken@1670
   901
                        if (maskb & 0x80) {
slouken@1670
   902
                            *dst = (Uint16) pixels[datab >> 7];
slouken@1670
   903
                        }
slouken@1670
   904
                        maskb <<= 1;
slouken@1670
   905
                        datab <<= 1;
slouken@1670
   906
                        dst++;
slouken@1670
   907
                    }
slouken@1670
   908
                }
slouken@1670
   909
                dst += dstskip;
slouken@1670
   910
            }
slouken@1670
   911
        }
slouken@1670
   912
        break;
slouken@1670
   913
slouken@1670
   914
    case 3:
slouken@1670
   915
        {
slouken@1670
   916
            Uint8 *dst;
slouken@1670
   917
            int dstskip;
slouken@1670
   918
slouken@1670
   919
            dst = (Uint8 *) screen->pixels +
slouken@1670
   920
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   921
                SDL_cursor->area.x * 3;
slouken@1670
   922
            dstskip = screen->pitch - area->w * 3;
slouken@1670
   923
slouken@1670
   924
            for (h = area->h; h; h--) {
slouken@1670
   925
                for (w = area->w / 8; w; w--) {
slouken@1670
   926
                    maskb = *mask++;
slouken@1670
   927
                    datab = *data++;
slouken@1670
   928
                    for (i = 0; i < 8; ++i) {
slouken@1670
   929
                        if (maskb & 0x80) {
slouken@1670
   930
                            SDL_memset(dst, pixels[datab >> 7], 3);
slouken@1670
   931
                        }
slouken@1670
   932
                        maskb <<= 1;
slouken@1670
   933
                        datab <<= 1;
slouken@1670
   934
                        dst += 3;
slouken@1670
   935
                    }
slouken@1670
   936
                }
slouken@1670
   937
                dst += dstskip;
slouken@1670
   938
            }
slouken@1670
   939
        }
slouken@1670
   940
        break;
slouken@1670
   941
slouken@1670
   942
    case 4:
slouken@1670
   943
        {
slouken@1670
   944
            Uint32 *dst;
slouken@1670
   945
            int dstskip;
slouken@1670
   946
slouken@1670
   947
            dst = (Uint32 *) screen->pixels +
slouken@1670
   948
                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
slouken@1670
   949
                SDL_cursor->area.x;
slouken@1670
   950
            dstskip = (screen->pitch / 4) - area->w;
slouken@1670
   951
slouken@1670
   952
            for (h = area->h; h; h--) {
slouken@1670
   953
                for (w = area->w / 8; w; w--) {
slouken@1670
   954
                    maskb = *mask++;
slouken@1670
   955
                    datab = *data++;
slouken@1670
   956
                    for (i = 0; i < 8; ++i) {
slouken@1670
   957
                        if (maskb & 0x80) {
slouken@1670
   958
                            *dst = pixels[datab >> 7];
slouken@1670
   959
                        }
slouken@1670
   960
                        maskb <<= 1;
slouken@1670
   961
                        datab <<= 1;
slouken@1670
   962
                        dst++;
slouken@1670
   963
                    }
slouken@1670
   964
                }
slouken@1670
   965
                dst += dstskip;
slouken@1670
   966
            }
slouken@1670
   967
        }
slouken@1670
   968
        break;
slouken@1670
   969
    }
slouken@1670
   970
}
slouken@1670
   971
slouken@1670
   972
static void
slouken@1670
   973
SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
slouken@1670
   974
{
slouken@1670
   975
    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
slouken@1670
   976
    int h;
slouken@1670
   977
    int x, minx, maxx;
slouken@1670
   978
    Uint8 *data, datab = 0;
slouken@1670
   979
    Uint8 *mask, maskb = 0;
slouken@1670
   980
    Uint8 *dst;
slouken@1670
   981
    int dstbpp, dstskip;
slouken@1670
   982
slouken@1670
   983
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1670
   984
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1670
   985
    dstbpp = screen->format->BytesPerPixel;
slouken@1670
   986
    dst = (Uint8 *) screen->pixels +
slouken@1670
   987
        (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   988
        SDL_cursor->area.x * dstbpp;
slouken@1670
   989
    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
slouken@1670
   990
slouken@1670
   991
    minx = area->x;
slouken@1670
   992
    maxx = area->x + area->w;
slouken@1670
   993
    if (screen->format->BytesPerPixel == 1) {
slouken@1670
   994
        if (palette_changed) {
slouken@1670
   995
            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1670
   996
            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1670
   997
            palette_changed = 0;
slouken@1670
   998
        }
slouken@1670
   999
        for (h = area->h; h; h--) {
slouken@1670
  1000
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1670
  1001
                if ((x % 8) == 0) {
slouken@1670
  1002
                    maskb = *mask++;
slouken@1670
  1003
                    datab = *data++;
slouken@1670
  1004
                }
slouken@1670
  1005
                if ((x >= minx) && (x < maxx)) {
slouken@1670
  1006
                    if (maskb & 0x80) {
slouken@1670
  1007
                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
slouken@1670
  1008
                    }
slouken@1670
  1009
                }
slouken@1670
  1010
                maskb <<= 1;
slouken@1670
  1011
                datab <<= 1;
slouken@1670
  1012
                dst += dstbpp;
slouken@1670
  1013
            }
slouken@1670
  1014
            dst += dstskip;
slouken@1670
  1015
        }
slouken@1670
  1016
    } else {
slouken@1670
  1017
        for (h = area->h; h; h--) {
slouken@1670
  1018
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1670
  1019
                if ((x % 8) == 0) {
slouken@1670
  1020
                    maskb = *mask++;
slouken@1670
  1021
                    datab = *data++;
slouken@1670
  1022
                }
slouken@1670
  1023
                if ((x >= minx) && (x < maxx)) {
slouken@1670
  1024
                    if (maskb & 0x80) {
slouken@1670
  1025
                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
slouken@1670
  1026
                    }
slouken@1670
  1027
                }
slouken@1670
  1028
                maskb <<= 1;
slouken@1670
  1029
                datab <<= 1;
slouken@1670
  1030
                dst += dstbpp;
slouken@1670
  1031
            }
slouken@1670
  1032
            dst += dstskip;
slouken@1670
  1033
        }
slouken@1670
  1034
    }
slouken@1670
  1035
}
slouken@1670
  1036
slouken@1670
  1037
/* This handles the ugly work of converting the saved cursor background from
slouken@1670
  1038
   the pixel format of the shadow surface to that of the video surface.
slouken@1670
  1039
   This is only necessary when blitting from a shadow surface of a different
slouken@1670
  1040
   pixel format than the video surface, and using a software rendered cursor.
slouken@1670
  1041
*/
slouken@1670
  1042
static void
slouken@1670
  1043
SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
slouken@1670
  1044
{
slouken@1670
  1045
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1046
    SDL_BlitInfo info;
slouken@1670
  1047
    SDL_loblit RunBlit;
slouken@1670
  1048
slouken@1670
  1049
    /* Make sure we can steal the blit mapping */
slouken@1670
  1050
    if (screen->map->dst != SDL_VideoSurface) {
slouken@1670
  1051
        return;
slouken@1670
  1052
    }
slouken@1670
  1053
slouken@1670
  1054
    /* Set up the blit information */
slouken@1670
  1055
    info.s_pixels = SDL_cursor->save[1];
slouken@1670
  1056
    info.s_width = w;
slouken@1670
  1057
    info.s_height = h;
slouken@1670
  1058
    info.s_skip = 0;
slouken@1670
  1059
    info.d_pixels = SDL_cursor->save[0];
slouken@1670
  1060
    info.d_width = w;
slouken@1670
  1061
    info.d_height = h;
slouken@1670
  1062
    info.d_skip = 0;
slouken@1670
  1063
    info.aux_data = screen->map->sw_data->aux_data;
slouken@1670
  1064
    info.src = screen->format;
slouken@1670
  1065
    info.table = screen->map->table;
slouken@1670
  1066
    info.dst = SDL_VideoSurface->format;
slouken@1670
  1067
    RunBlit = screen->map->sw_data->blit;
slouken@1670
  1068
slouken@1670
  1069
    /* Run the actual software blit */
slouken@1670
  1070
    RunBlit(&info);
slouken@1670
  1071
}
slouken@1670
  1072
slouken@1670
  1073
void
slouken@1670
  1074
SDL_DrawCursorNoLock(SDL_Surface * screen)
slouken@1670
  1075
{
slouken@1670
  1076
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1077
    SDL_Rect area;
slouken@1670
  1078
slouken@1670
  1079
    /* Get the mouse rectangle, clipped to the screen */
slouken@1670
  1080
    SDL_MouseRect(&area);
slouken@1670
  1081
    if ((area.w == 0) || (area.h == 0)) {
slouken@1670
  1082
        return;
slouken@1670
  1083
    }
slouken@1670
  1084
slouken@1670
  1085
    /* Copy mouse background */
slouken@1670
  1086
    {
slouken@1670
  1087
        int w, h, screenbpp;
slouken@1670
  1088
        Uint8 *src, *dst;
slouken@1670
  1089
slouken@1670
  1090
        /* Set up the copy pointers */
slouken@1670
  1091
        screenbpp = screen->format->BytesPerPixel;
slouken@1670
  1092
        if ((screen == SDL_VideoSurface) ||
slouken@1670
  1093
            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
slouken@1670
  1094
            dst = SDL_cursor->save[0];
slouken@1670
  1095
        } else {
slouken@1670
  1096
            dst = SDL_cursor->save[1];
slouken@1670
  1097
        }
slouken@1670
  1098
        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1670
  1099
            area.x * screenbpp;
slouken@1670
  1100
slouken@1670
  1101
        /* Perform the copy */
slouken@1670
  1102
        w = area.w * screenbpp;
slouken@1670
  1103
        h = area.h;
slouken@1670
  1104
        while (h--) {
slouken@1670
  1105
            SDL_memcpy(dst, src, w);
slouken@1670
  1106
            dst += w;
slouken@1670
  1107
            src += screen->pitch;
slouken@1670
  1108
        }
slouken@1670
  1109
    }
slouken@1670
  1110
slouken@1670
  1111
    /* Draw the mouse cursor */
slouken@1670
  1112
    area.x -= SDL_cursor->area.x;
slouken@1670
  1113
    area.y -= SDL_cursor->area.y;
slouken@1670
  1114
    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
slouken@1670
  1115
        SDL_DrawCursorFast(screen, &area);
slouken@1670
  1116
    } else {
slouken@1670
  1117
        SDL_DrawCursorSlow(screen, &area);
slouken@1670
  1118
    }
slouken@1670
  1119
}
slouken@1670
  1120
slouken@1670
  1121
void
slouken@1670
  1122
SDL_DrawCursor(SDL_Surface * screen)
slouken@1670
  1123
{
slouken@1670
  1124
    /* Lock the screen if necessary */
slouken@1670
  1125
    if (screen == NULL) {
slouken@1670
  1126
        return;
slouken@1670
  1127
    }
slouken@1670
  1128
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1129
        if (SDL_LockSurface(screen) < 0) {
slouken@1670
  1130
            return;
slouken@1670
  1131
        }
slouken@1670
  1132
    }
slouken@1670
  1133
slouken@1670
  1134
    SDL_DrawCursorNoLock(screen);
slouken@1670
  1135
slouken@1670
  1136
    /* Unlock the screen and update if necessary */
slouken@1670
  1137
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1138
        SDL_UnlockSurface(screen);
slouken@1670
  1139
    }
slouken@1670
  1140
    if ((screen->flags & SDL_SCREEN_SURFACE) &&
slouken@1670
  1141
        !(screen->flags & SDL_HWSURFACE)) {
slouken@1670
  1142
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1143
        SDL_Window *window;
slouken@1670
  1144
        SDL_Rect area;
slouken@1670
  1145
slouken@1670
  1146
        window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1147
        if (!window) {
slouken@1670
  1148
            return;
slouken@1670
  1149
        }
slouken@1670
  1150
slouken@1670
  1151
        SDL_MouseRect(&area);
slouken@1670
  1152
slouken@1670
  1153
        if (_this->UpdateWindowSurface) {
slouken@1670
  1154
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1670
  1155
        }
slouken@1670
  1156
    }
slouken@1670
  1157
}
slouken@1670
  1158
slouken@1670
  1159
void
slouken@1670
  1160
SDL_EraseCursorNoLock(SDL_Surface * screen)
slouken@1670
  1161
{
slouken@1670
  1162
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1163
    SDL_Window *window;
slouken@1670
  1164
    SDL_Rect area;
slouken@1670
  1165
slouken@1670
  1166
    /* Get the window associated with the surface */
slouken@1670
  1167
    window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1168
    if (!window || !window->surface) {
slouken@1670
  1169
        return;
slouken@1670
  1170
    }
slouken@1670
  1171
slouken@1670
  1172
    /* Get the mouse rectangle, clipped to the screen */
slouken@1670
  1173
    SDL_MouseRect(&area);
slouken@1670
  1174
    if ((area.w == 0) || (area.h == 0)) {
slouken@1670
  1175
        return;
slouken@1670
  1176
    }
slouken@1670
  1177
slouken@1670
  1178
    /* Copy mouse background */
slouken@1670
  1179
    {
slouken@1670
  1180
        int w, h, screenbpp;
slouken@1670
  1181
        Uint8 *src, *dst;
slouken@1670
  1182
slouken@1670
  1183
        /* Set up the copy pointers */
slouken@1670
  1184
        screenbpp = screen->format->BytesPerPixel;
slouken@1670
  1185
        if ((screen->flags & SDL_SCREEN_SURFACE) ||
slouken@1670
  1186
            FORMAT_EQUAL(screen->format, window->surface->format)) {
slouken@1670
  1187
            src = SDL_cursor->save[0];
slouken@1670
  1188
        } else {
slouken@1670
  1189
            src = SDL_cursor->save[1];
slouken@1670
  1190
        }
slouken@1670
  1191
        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1670
  1192
            area.x * screenbpp;
slouken@1670
  1193
slouken@1670
  1194
        /* Perform the copy */
slouken@1670
  1195
        w = area.w * screenbpp;
slouken@1670
  1196
        h = area.h;
slouken@1670
  1197
        while (h--) {
slouken@1670
  1198
            SDL_memcpy(dst, src, w);
slouken@1670
  1199
            src += w;
slouken@1670
  1200
            dst += screen->pitch;
slouken@1670
  1201
        }
slouken@1670
  1202
slouken@1670
  1203
        /* Perform pixel conversion on cursor background */
slouken@1670
  1204
        if (src > SDL_cursor->save[1]) {
slouken@1670
  1205
            SDL_ConvertCursorSave(screen, area.w, area.h);
slouken@1670
  1206
        }
slouken@1670
  1207
    }
slouken@1670
  1208
}
slouken@1670
  1209
slouken@1670
  1210
void
slouken@1670
  1211
SDL_EraseCursor(SDL_Surface * screen)
slouken@1670
  1212
{
slouken@1670
  1213
    /* Lock the screen if necessary */
slouken@1670
  1214
    if (screen == NULL) {
slouken@1670
  1215
        return;
slouken@1670
  1216
    }
slouken@1670
  1217
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1218
        if (SDL_LockSurface(screen) < 0) {
slouken@1670
  1219
            return;
slouken@1670
  1220
        }
slouken@1670
  1221
    }
slouken@1670
  1222
slouken@1670
  1223
    SDL_EraseCursorNoLock(screen);
slouken@1670
  1224
slouken@1670
  1225
    /* Unlock the screen and update if necessary */
slouken@1670
  1226
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1227
        SDL_UnlockSurface(screen);
slouken@1670
  1228
    }
slouken@1670
  1229
    if ((screen->flags & SDL_SCREEN_SURFACE) &&
slouken@1670
  1230
        !(screen->flags & SDL_HWSURFACE)) {
slouken@1670
  1231
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1232
        SDL_Window *window;
slouken@1670
  1233
        SDL_Rect area;
slouken@1670
  1234
slouken@1670
  1235
        window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1236
        if (!window) {
slouken@1670
  1237
            return;
slouken@1670
  1238
        }
slouken@1670
  1239
slouken@1670
  1240
        SDL_MouseRect(&area);
slouken@1670
  1241
slouken@1670
  1242
        if (_this->UpdateWindowSurface) {
slouken@1670
  1243
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1670
  1244
        }
slouken@1670
  1245
    }
slouken@1670
  1246
}
slouken@1670
  1247
slouken@1670
  1248
/* Reset the cursor on video mode change
slouken@1670
  1249
   FIXME:  Keep track of all cursors, and reset them all.
slouken@1670
  1250
 */
slouken@1670
  1251
void
slouken@1670
  1252
SDL_ResetCursor(void)
slouken@1670
  1253
{
slouken@1670
  1254
    int savelen;
slouken@1670
  1255
slouken@1670
  1256
    if (SDL_cursor) {
slouken@1670
  1257
        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
slouken@1670
  1258
        SDL_cursor->area.x = 0;
slouken@1670
  1259
        SDL_cursor->area.y = 0;
slouken@1670
  1260
        SDL_memset(SDL_cursor->save[0], 0, savelen);
slouken@1670
  1261
    }
slouken@1670
  1262
}
slouken@1684
  1263
#endif
slouken@1684
  1264
slouken@1684
  1265
struct private_yuvhwdata
slouken@1684
  1266
{
slouken@1684
  1267
    Uint16 pitches[3];
slouken@1684
  1268
    Uint8 *planes[3];
slouken@1684
  1269
slouken@1684
  1270
    SDL_TextureID textureID;
slouken@1684
  1271
};
slouken@1670
  1272
slouken@1675
  1273
SDL_Overlay *
slouken@1675
  1274
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL_Surface * display)
slouken@1675
  1275
{
slouken@1675
  1276
    SDL_Overlay *overlay;
slouken@1684
  1277
    Uint32 texture_format;
slouken@1675
  1278
slouken@1684
  1279
    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
slouken@1675
  1280
        SDL_SetError("YUV overlays are not supported in OpenGL mode");
slouken@1675
  1281
        return NULL;
slouken@1675
  1282
    }
slouken@1675
  1283
slouken@1684
  1284
    if (display != SDL_PublicSurface) {
slouken@1684
  1285
        SDL_SetError("YUV display is only supported on the screen surface");
slouken@1684
  1286
        return NULL;
slouken@1675
  1287
    }
slouken@1684
  1288
slouken@1684
  1289
    switch (format) {
slouken@1684
  1290
    case SDL_YV12_OVERLAY:
slouken@1684
  1291
        texture_format = SDL_PixelFormat_YV12;
slouken@1684
  1292
        break;
slouken@1684
  1293
    case SDL_IYUV_OVERLAY:
slouken@1684
  1294
        texture_format = SDL_PixelFormat_IYUV;
slouken@1684
  1295
        break;
slouken@1684
  1296
    case SDL_YUY2_OVERLAY:
slouken@1684
  1297
        texture_format = SDL_PixelFormat_YUY2;
slouken@1684
  1298
        break;
slouken@1684
  1299
    case SDL_UYVY_OVERLAY:
slouken@1684
  1300
        texture_format = SDL_PixelFormat_UYVY;
slouken@1684
  1301
        break;
slouken@1684
  1302
    case SDL_YVYU_OVERLAY:
slouken@1684
  1303
        texture_format = SDL_PixelFormat_YVYU;
slouken@1684
  1304
        break;
slouken@1684
  1305
    default:
slouken@1684
  1306
        SDL_SetError("Unknown YUV format");
slouken@1684
  1307
        return NULL;
slouken@1675
  1308
    }
slouken@1684
  1309
slouken@1684
  1310
    overlay = (SDL_Overlay *) SDL_malloc(sizeof(*overlay));
slouken@1684
  1311
    if (!overlay) {
slouken@1684
  1312
        SDL_OutOfMemory();
slouken@1684
  1313
        return NULL;
slouken@1675
  1314
    }
slouken@1684
  1315
    SDL_zerop(overlay);
slouken@1684
  1316
slouken@1684
  1317
    overlay->hwdata =
slouken@1684
  1318
        (struct private_yuvhwdata *) SDL_malloc(sizeof(*overlay->hwdata));
slouken@1684
  1319
    if (!overlay->hwdata) {
slouken@1684
  1320
        SDL_free(overlay);
slouken@1684
  1321
        SDL_OutOfMemory();
slouken@1684
  1322
        return NULL;
slouken@1684
  1323
    }
slouken@1684
  1324
slouken@1684
  1325
    overlay->format = format;
slouken@1684
  1326
    overlay->w = w;
slouken@1684
  1327
    overlay->h = h;
slouken@1684
  1328
    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
slouken@1684
  1329
        overlay->planes = 3;
slouken@1684
  1330
    } else {
slouken@1684
  1331
        overlay->planes = 1;
slouken@1684
  1332
    }
slouken@1684
  1333
    overlay->pitches = overlay->hwdata->pitches;
slouken@1684
  1334
    overlay->pixels = overlay->hwdata->planes;
slouken@1684
  1335
slouken@1684
  1336
    switch (format) {
slouken@1684
  1337
    case SDL_YV12_OVERLAY:
slouken@1684
  1338
    case SDL_IYUV_OVERLAY:
slouken@1684
  1339
        overlay->pitches[0] = overlay->w;
slouken@1684
  1340
        overlay->pitches[1] = overlay->w / 2;
slouken@1684
  1341
        overlay->pitches[2] = overlay->w / 2;
slouken@1684
  1342
        break;
slouken@1684
  1343
    case SDL_YUY2_OVERLAY:
slouken@1684
  1344
    case SDL_UYVY_OVERLAY:
slouken@1684
  1345
    case SDL_YVYU_OVERLAY:
slouken@1685
  1346
        overlay->pitches[0] = overlay->w * 2;
slouken@1684
  1347
        break;
slouken@1684
  1348
    }
slouken@1684
  1349
slouken@1684
  1350
    overlay->hwdata->textureID =
slouken@1684
  1351
        SDL_CreateTexture(texture_format, SDL_TextureAccess_Local, w, h);
slouken@1684
  1352
    if (!overlay->hwdata->textureID) {
slouken@1684
  1353
        SDL_FreeYUVOverlay(overlay);
slouken@1684
  1354
        return NULL;
slouken@1684
  1355
    }
slouken@1684
  1356
slouken@1675
  1357
    return overlay;
slouken@1675
  1358
}
slouken@1675
  1359
slouken@1675
  1360
int
slouken@1675
  1361
SDL_LockYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1362
{
slouken@1684
  1363
    void *pixels;
slouken@1684
  1364
    int pitch;
slouken@1684
  1365
    if (SDL_LockTexture(overlay->hwdata->textureID, NULL, 1, &pixels, &pitch)
slouken@1684
  1366
        < 0) {
slouken@1684
  1367
        return -1;
slouken@1684
  1368
    }
slouken@1684
  1369
    switch (overlay->format) {
slouken@1684
  1370
    case SDL_YV12_OVERLAY:
slouken@1684
  1371
    case SDL_IYUV_OVERLAY:
slouken@1684
  1372
        overlay->pixels[0] = (Uint8 *) pixels;
slouken@1684
  1373
        overlay->pixels[1] =
slouken@1684
  1374
            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
slouken@1684
  1375
        overlay->pixels[2] =
slouken@1684
  1376
            overlay->pixels[1] + overlay->pitches[1] * overlay->h;
slouken@1684
  1377
        break;
slouken@1684
  1378
    case SDL_YUY2_OVERLAY:
slouken@1684
  1379
    case SDL_UYVY_OVERLAY:
slouken@1684
  1380
    case SDL_YVYU_OVERLAY:
slouken@1684
  1381
        overlay->pixels[0] = (Uint8 *) pixels;
slouken@1684
  1382
        break;
slouken@1684
  1383
    }
slouken@1684
  1384
    return 0;
slouken@1675
  1385
}
slouken@1675
  1386
slouken@1675
  1387
void
slouken@1675
  1388
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1389
{
slouken@1684
  1390
    SDL_UnlockTexture(overlay->hwdata->textureID);
slouken@1675
  1391
}
slouken@1675
  1392
slouken@1675
  1393
int
slouken@1675
  1394
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
slouken@1675
  1395
{
slouken@1684
  1396
    if (SDL_RenderCopy(overlay->hwdata->textureID, NULL, dstrect,
slouken@1684
  1397
                       SDL_TextureBlendMode_None,
slouken@1684
  1398
                       SDL_TextureScaleMode_Fast) < 0) {
slouken@1684
  1399
        return -1;
slouken@1675
  1400
    }
slouken@1684
  1401
    SDL_RenderPresent();
slouken@1720
  1402
    return 0;
slouken@1675
  1403
}
slouken@1675
  1404
slouken@1675
  1405
void
slouken@1675
  1406
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1407
{
slouken@1675
  1408
    if (overlay) {
slouken@1684
  1409
        if (overlay->hwdata) {
slouken@1684
  1410
            if (overlay->hwdata->textureID) {
slouken@1684
  1411
                SDL_DestroyTexture(overlay->hwdata->textureID);
slouken@1684
  1412
            }
slouken@1684
  1413
            SDL_free(overlay->hwdata);
slouken@1675
  1414
        }
slouken@1675
  1415
        SDL_free(overlay);
slouken@1675
  1416
    }
slouken@1675
  1417
}
slouken@1675
  1418
slouken@1667
  1419
/* vi: set ts=4 sw=4 expandtab: */