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