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