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