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