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