src/SDL_compat.c
changeset 1895 c121d94672cb
child 1897 c2a27da60b18
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_compat.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -0,0 +1,1415 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* This file contains functions for backwards compatibility with SDL 1.2 */
    1.28 +
    1.29 +#include "SDL.h"
    1.30 +#include "SDL_syswm.h"
    1.31 +
    1.32 +#include "video/SDL_sysvideo.h"
    1.33 +#include "video/SDL_pixels_c.h"
    1.34 +
    1.35 +
    1.36 +static SDL_WindowID SDL_VideoWindow;
    1.37 +static SDL_TextureID SDL_VideoTexture;
    1.38 +static SDL_Surface *SDL_VideoSurface;
    1.39 +static SDL_Surface *SDL_ShadowSurface;
    1.40 +static SDL_Surface *SDL_PublicSurface;
    1.41 +static char *wm_title;
    1.42 +
    1.43 +char *
    1.44 +SDL_AudioDriverName(char *namebuf, int maxlen)
    1.45 +{
    1.46 +    const char *name = SDL_GetCurrentAudioDriver();
    1.47 +    if (name) {
    1.48 +        SDL_strlcpy(namebuf, name, maxlen);
    1.49 +        return namebuf;
    1.50 +    }
    1.51 +    return NULL;
    1.52 +}
    1.53 +
    1.54 +char *
    1.55 +SDL_VideoDriverName(char *namebuf, int maxlen)
    1.56 +{
    1.57 +    const char *name = SDL_GetCurrentVideoDriver();
    1.58 +    if (name) {
    1.59 +        SDL_strlcpy(namebuf, name, maxlen);
    1.60 +        return namebuf;
    1.61 +    }
    1.62 +    return NULL;
    1.63 +}
    1.64 +
    1.65 +const SDL_VideoInfo *
    1.66 +SDL_GetVideoInfo(void)
    1.67 +{
    1.68 +    static SDL_VideoInfo info;
    1.69 +
    1.70 +    /* Memory leak, compatibility code, who cares? */
    1.71 +    if (!info.vfmt && SDL_GetDesktopDisplayMode()) {
    1.72 +        int bpp;
    1.73 +        Uint32 Rmask, Gmask, Bmask, Amask;
    1.74 +
    1.75 +        SDL_PixelFormatEnumToMasks(SDL_GetDesktopDisplayMode()->format, &bpp,
    1.76 +                                   &Rmask, &Gmask, &Bmask, &Amask);
    1.77 +        info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    1.78 +    }
    1.79 +    return &info;
    1.80 +}
    1.81 +
    1.82 +int
    1.83 +SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
    1.84 +{
    1.85 +    int i, actual_bpp = 0;
    1.86 +
    1.87 +    if (!SDL_GetVideoDevice()) {
    1.88 +        return 0;
    1.89 +    }
    1.90 +
    1.91 +    if (!(flags & SDL_FULLSCREEN)) {
    1.92 +        return SDL_BITSPERPIXEL(SDL_GetDesktopDisplayMode()->format);
    1.93 +    }
    1.94 +
    1.95 +    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
    1.96 +        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
    1.97 +        if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
    1.98 +            if (!mode->format) {
    1.99 +                return bpp;
   1.100 +            }
   1.101 +            if (SDL_BITSPERPIXEL(mode->format) >= (Uint32) bpp) {
   1.102 +                actual_bpp = SDL_BITSPERPIXEL(mode->format);
   1.103 +            }
   1.104 +        }
   1.105 +    }
   1.106 +    return actual_bpp;
   1.107 +}
   1.108 +
   1.109 +SDL_Rect **
   1.110 +SDL_ListModes(SDL_PixelFormat * format, Uint32 flags)
   1.111 +{
   1.112 +    int i, nmodes;
   1.113 +    SDL_Rect **modes;
   1.114 +
   1.115 +    if (!SDL_GetVideoDevice()) {
   1.116 +        return NULL;
   1.117 +    }
   1.118 +
   1.119 +    if (!(flags & SDL_FULLSCREEN)) {
   1.120 +        return (SDL_Rect **) (-1);
   1.121 +    }
   1.122 +
   1.123 +    /* Memory leak, but this is a compatibility function, who cares? */
   1.124 +    nmodes = 0;
   1.125 +    modes = NULL;
   1.126 +    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
   1.127 +        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
   1.128 +        if (!mode->w || !mode->h) {
   1.129 +            return (SDL_Rect **) (-1);
   1.130 +        }
   1.131 +        if (SDL_BITSPERPIXEL(mode->format) != format->BitsPerPixel) {
   1.132 +            continue;
   1.133 +        }
   1.134 +        if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
   1.135 +            && modes[nmodes - 1]->h == mode->h) {
   1.136 +            continue;
   1.137 +        }
   1.138 +
   1.139 +        modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
   1.140 +        if (!modes) {
   1.141 +            return NULL;
   1.142 +        }
   1.143 +        modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
   1.144 +        if (!modes[nmodes]) {
   1.145 +            return NULL;
   1.146 +        }
   1.147 +        modes[nmodes]->x = 0;
   1.148 +        modes[nmodes]->y = 0;
   1.149 +        modes[nmodes]->w = mode->w;
   1.150 +        modes[nmodes]->h = mode->h;
   1.151 +        ++nmodes;
   1.152 +    }
   1.153 +    if (modes) {
   1.154 +        modes[nmodes] = NULL;
   1.155 +    }
   1.156 +    return modes;
   1.157 +}
   1.158 +
   1.159 +static int
   1.160 +SDL_CompatEventFilter(void *userdata, SDL_Event * event)
   1.161 +{
   1.162 +    SDL_Event fake;
   1.163 +
   1.164 +    switch (event->type) {
   1.165 +    case SDL_WINDOWEVENT:
   1.166 +        switch (event->window.event) {
   1.167 +        case SDL_WINDOWEVENT_EXPOSED:
   1.168 +            if (!SDL_HasEvent(SDL_VIDEOEXPOSEMASK)) {
   1.169 +                fake.type = SDL_VIDEOEXPOSE;
   1.170 +                SDL_PushEvent(&fake);
   1.171 +            }
   1.172 +            break;
   1.173 +        case SDL_WINDOWEVENT_RESIZED:
   1.174 +            fake.type = SDL_VIDEORESIZE;
   1.175 +            fake.resize.w = event->window.data1;
   1.176 +            fake.resize.h = event->window.data2;
   1.177 +            SDL_PushEvent(&fake);
   1.178 +            break;
   1.179 +        case SDL_WINDOWEVENT_MINIMIZED:
   1.180 +            fake.type = SDL_ACTIVEEVENT;
   1.181 +            fake.active.gain = 0;
   1.182 +            fake.active.state = SDL_APPACTIVE;
   1.183 +            SDL_PushEvent(&fake);
   1.184 +            break;
   1.185 +        case SDL_WINDOWEVENT_RESTORED:
   1.186 +            fake.type = SDL_ACTIVEEVENT;
   1.187 +            fake.active.gain = 1;
   1.188 +            fake.active.state = SDL_APPACTIVE;
   1.189 +            SDL_PushEvent(&fake);
   1.190 +            break;
   1.191 +        case SDL_WINDOWEVENT_ENTER:
   1.192 +            fake.type = SDL_ACTIVEEVENT;
   1.193 +            fake.active.gain = 1;
   1.194 +            fake.active.state = SDL_APPMOUSEFOCUS;
   1.195 +            SDL_PushEvent(&fake);
   1.196 +            break;
   1.197 +        case SDL_WINDOWEVENT_LEAVE:
   1.198 +            fake.type = SDL_ACTIVEEVENT;
   1.199 +            fake.active.gain = 0;
   1.200 +            fake.active.state = SDL_APPMOUSEFOCUS;
   1.201 +            SDL_PushEvent(&fake);
   1.202 +            break;
   1.203 +        case SDL_WINDOWEVENT_FOCUS_GAINED:
   1.204 +            fake.type = SDL_ACTIVEEVENT;
   1.205 +            fake.active.gain = 1;
   1.206 +            fake.active.state = SDL_APPINPUTFOCUS;
   1.207 +            SDL_PushEvent(&fake);
   1.208 +            break;
   1.209 +        case SDL_WINDOWEVENT_FOCUS_LOST:
   1.210 +            fake.type = SDL_ACTIVEEVENT;
   1.211 +            fake.active.gain = 1;
   1.212 +            fake.active.state = SDL_APPINPUTFOCUS;
   1.213 +            SDL_PushEvent(&fake);
   1.214 +            break;
   1.215 +        case SDL_WINDOWEVENT_CLOSE:
   1.216 +            fake.type = SDL_QUIT;
   1.217 +            SDL_PushEvent(&fake);
   1.218 +            break;
   1.219 +        }
   1.220 +    case SDL_KEYDOWN:
   1.221 +    case SDL_KEYUP:
   1.222 +        {
   1.223 +            Uint32 unicode = 0;
   1.224 +            if (event->key.type == SDL_KEYDOWN && event->key.keysym.sym < 256) {
   1.225 +                unicode = event->key.keysym.sym;
   1.226 +                if (unicode >= 'a' && unicode <= 'z') {
   1.227 +                    int shifted = !!(event->key.keysym.mod & KMOD_SHIFT);
   1.228 +                    int capslock = !!(event->key.keysym.mod & KMOD_CAPS);
   1.229 +                    if ((shifted ^ capslock) != 0) {
   1.230 +                        unicode = SDL_toupper(unicode);
   1.231 +                    }
   1.232 +                }
   1.233 +            }
   1.234 +            if (unicode) {
   1.235 +                event->key.keysym.unicode = unicode;
   1.236 +            }
   1.237 +            break;
   1.238 +        }
   1.239 +    case SDL_MOUSEWHEEL:
   1.240 +        {
   1.241 +            Uint8 button;
   1.242 +            int selected;
   1.243 +            int x, y;
   1.244 +
   1.245 +            selected = SDL_SelectMouse(event->wheel.which);
   1.246 +            SDL_GetMouseState(&x, &y);
   1.247 +            SDL_SelectMouse(selected);
   1.248 +
   1.249 +            if (event->wheel.motion > 0) {
   1.250 +                button = SDL_BUTTON_WHEELUP;
   1.251 +            } else {
   1.252 +                button = SDL_BUTTON_WHEELDOWN;
   1.253 +            }
   1.254 +
   1.255 +            fake.button.which = event->wheel.windowID;
   1.256 +            fake.button.button = button;
   1.257 +            fake.button.x = x;
   1.258 +            fake.button.y = y;
   1.259 +            fake.button.windowID = event->wheel.windowID;
   1.260 +
   1.261 +            fake.type = SDL_MOUSEBUTTONDOWN;
   1.262 +            fake.button.state = SDL_PRESSED;
   1.263 +            SDL_PushEvent(&fake);
   1.264 +
   1.265 +            fake.type = SDL_MOUSEBUTTONUP;
   1.266 +            fake.button.state = SDL_RELEASED;
   1.267 +            SDL_PushEvent(&fake);
   1.268 +            break;
   1.269 +        }
   1.270 +
   1.271 +    }
   1.272 +    return 1;
   1.273 +}
   1.274 +
   1.275 +static int
   1.276 +SDL_VideoPaletteChanged(void *userdata, SDL_Palette * palette)
   1.277 +{
   1.278 +    if (userdata == SDL_ShadowSurface) {
   1.279 +        /* If the shadow palette changed, make the changes visible */
   1.280 +        if (!SDL_VideoSurface->format->palette) {
   1.281 +            SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
   1.282 +        }
   1.283 +    }
   1.284 +    if (userdata == SDL_VideoSurface) {
   1.285 +        return SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors);
   1.286 +    }
   1.287 +    return 0;
   1.288 +}
   1.289 +
   1.290 +static void
   1.291 +GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
   1.292 +{
   1.293 +    const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
   1.294 +    const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
   1.295 +    if (window) {
   1.296 +        if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
   1.297 +            return;
   1.298 +        }
   1.299 +        if (SDL_strcmp(window, "center") == 0) {
   1.300 +            center = window;
   1.301 +        }
   1.302 +    }
   1.303 +    if (center) {
   1.304 +        const SDL_DisplayMode *current = SDL_GetDesktopDisplayMode();
   1.305 +        *x = (current->w - w) / 2;
   1.306 +        *y = (current->h - h) / 2;
   1.307 +    }
   1.308 +}
   1.309 +
   1.310 +SDL_Surface *
   1.311 +SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
   1.312 +{
   1.313 +    const SDL_DisplayMode *desktop_mode;
   1.314 +    SDL_DisplayMode mode;
   1.315 +    int window_x = SDL_WINDOWPOS_UNDEFINED;
   1.316 +    int window_y = SDL_WINDOWPOS_UNDEFINED;
   1.317 +    Uint32 window_flags;
   1.318 +    Uint32 desktop_format;
   1.319 +    Uint32 desired_format;
   1.320 +    Uint32 surface_flags;
   1.321 +
   1.322 +    if (!SDL_GetVideoDevice()) {
   1.323 +        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   1.324 +            return NULL;
   1.325 +        }
   1.326 +    }
   1.327 +
   1.328 +    /* Destroy existing window */
   1.329 +    SDL_PublicSurface = NULL;
   1.330 +    if (SDL_ShadowSurface) {
   1.331 +        SDL_FreeSurface(SDL_ShadowSurface);
   1.332 +        SDL_ShadowSurface = NULL;
   1.333 +    }
   1.334 +    if (SDL_VideoSurface) {
   1.335 +        SDL_DelPaletteWatch(SDL_VideoSurface->format->palette,
   1.336 +                            SDL_VideoPaletteChanged, NULL);
   1.337 +        SDL_FreeSurface(SDL_VideoSurface);
   1.338 +        SDL_VideoSurface = NULL;
   1.339 +    }
   1.340 +    if (SDL_VideoWindow) {
   1.341 +        SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
   1.342 +    }
   1.343 +    SDL_DestroyWindow(SDL_VideoWindow);
   1.344 +
   1.345 +    /* Set up the event filter */
   1.346 +    if (!SDL_GetEventFilter(NULL, NULL)) {
   1.347 +        SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
   1.348 +    }
   1.349 +
   1.350 +    /* Create a new window */
   1.351 +    window_flags = SDL_WINDOW_SHOWN;
   1.352 +    if (flags & SDL_FULLSCREEN) {
   1.353 +        window_flags |= SDL_WINDOW_FULLSCREEN;
   1.354 +    }
   1.355 +    if (flags & SDL_OPENGL) {
   1.356 +        window_flags |= SDL_WINDOW_OPENGL;
   1.357 +    }
   1.358 +    if (flags & SDL_RESIZABLE) {
   1.359 +        window_flags |= SDL_WINDOW_RESIZABLE;
   1.360 +    }
   1.361 +    if (flags & SDL_NOFRAME) {
   1.362 +        window_flags |= SDL_WINDOW_BORDERLESS;
   1.363 +    }
   1.364 +    GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
   1.365 +    SDL_SetFullscreenDisplayMode(NULL);
   1.366 +    SDL_VideoWindow =
   1.367 +        SDL_CreateWindow(wm_title, window_x, window_y, width, height,
   1.368 +                         window_flags);
   1.369 +    if (!SDL_VideoWindow) {
   1.370 +        return NULL;
   1.371 +    }
   1.372 +
   1.373 +    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
   1.374 +    surface_flags = 0;
   1.375 +    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   1.376 +        surface_flags |= SDL_FULLSCREEN;
   1.377 +    }
   1.378 +    if (window_flags & SDL_WINDOW_OPENGL) {
   1.379 +        surface_flags |= SDL_OPENGL;
   1.380 +    }
   1.381 +    if (window_flags & SDL_WINDOW_RESIZABLE) {
   1.382 +        surface_flags |= SDL_RESIZABLE;
   1.383 +    }
   1.384 +    if (window_flags & SDL_WINDOW_BORDERLESS) {
   1.385 +        surface_flags |= SDL_NOFRAME;
   1.386 +    }
   1.387 +
   1.388 +    /* Set up the desired display mode */
   1.389 +    desktop_mode = SDL_GetDesktopDisplayMode();
   1.390 +    desktop_format = desktop_mode->format;
   1.391 +    if (desktop_format && ((flags & SDL_ANYFORMAT)
   1.392 +                           || (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
   1.393 +        desired_format = desktop_format;
   1.394 +    } else {
   1.395 +        switch (bpp) {
   1.396 +        case 0:
   1.397 +            if (desktop_format) {
   1.398 +                desired_format = desktop_format;
   1.399 +            } else {
   1.400 +                desired_format = SDL_PixelFormat_RGB888;
   1.401 +            }
   1.402 +            break;
   1.403 +        case 8:
   1.404 +            desired_format = SDL_PixelFormat_Index8;
   1.405 +            break;
   1.406 +        case 15:
   1.407 +            desired_format = SDL_PixelFormat_RGB555;
   1.408 +            break;
   1.409 +        case 16:
   1.410 +            desired_format = SDL_PixelFormat_RGB565;
   1.411 +            break;
   1.412 +        case 24:
   1.413 +            desired_format = SDL_PixelFormat_RGB24;
   1.414 +            break;
   1.415 +        case 32:
   1.416 +            desired_format = SDL_PixelFormat_RGB888;
   1.417 +            break;
   1.418 +        default:
   1.419 +            SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
   1.420 +            return NULL;
   1.421 +        }
   1.422 +    }
   1.423 +    mode.format = desired_format;
   1.424 +    mode.w = width;
   1.425 +    mode.h = height;
   1.426 +    mode.refresh_rate = 0;
   1.427 +
   1.428 +    /* Set the desired display mode */
   1.429 +    if (flags & SDL_FULLSCREEN) {
   1.430 +        if (SDL_SetFullscreenDisplayMode(&mode) < 0) {
   1.431 +            return NULL;
   1.432 +        }
   1.433 +    }
   1.434 +
   1.435 +    /* If we're in OpenGL mode, just create a stub surface and we're done! */
   1.436 +    if (flags & SDL_OPENGL) {
   1.437 +        SDL_VideoSurface =
   1.438 +            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
   1.439 +        if (!SDL_VideoSurface) {
   1.440 +            return NULL;
   1.441 +        }
   1.442 +        SDL_VideoSurface->flags |= surface_flags;
   1.443 +        SDL_PublicSurface = SDL_VideoSurface;
   1.444 +        return SDL_PublicSurface;
   1.445 +    }
   1.446 +
   1.447 +    /* Create a renderer for the window */
   1.448 +    if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) {
   1.449 +        return NULL;
   1.450 +    }
   1.451 +
   1.452 +    /* Create a texture for the screen surface */
   1.453 +    SDL_VideoTexture =
   1.454 +        SDL_CreateTexture(desired_format, SDL_TextureAccess_Local, width,
   1.455 +                          height);
   1.456 +    if (!SDL_VideoTexture) {
   1.457 +        SDL_VideoTexture =
   1.458 +            SDL_CreateTexture(0, SDL_TextureAccess_Local, width, height);
   1.459 +    }
   1.460 +    if (!SDL_VideoTexture) {
   1.461 +        return NULL;
   1.462 +    }
   1.463 +
   1.464 +    /* Create the screen surface */
   1.465 +    SDL_VideoSurface = SDL_CreateRGBSurfaceFromTexture(SDL_VideoTexture);
   1.466 +    if (!SDL_VideoSurface) {
   1.467 +        return NULL;
   1.468 +    }
   1.469 +    SDL_VideoSurface->flags |= surface_flags;
   1.470 +
   1.471 +    /* Set a default screen palette */
   1.472 +    if (SDL_VideoSurface->format->palette) {
   1.473 +        SDL_VideoSurface->flags |= SDL_HWPALETTE;
   1.474 +        SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
   1.475 +                         SDL_VideoSurface->format->BitsPerPixel);
   1.476 +        SDL_AddPaletteWatch(SDL_VideoSurface->format->palette,
   1.477 +                            SDL_VideoPaletteChanged, NULL);
   1.478 +        SDL_SetPaletteColors(SDL_VideoSurface->format->palette,
   1.479 +                             SDL_VideoSurface->format->palette->colors, 0,
   1.480 +                             SDL_VideoSurface->format->palette->ncolors);
   1.481 +    }
   1.482 +
   1.483 +    /* Create a shadow surface if necessary */
   1.484 +    if (((bpp != SDL_VideoSurface->format->BitsPerPixel)
   1.485 +         && !(flags & SDL_ANYFORMAT))
   1.486 +        || ((SDL_VideoSurface->flags & SDL_HWSURFACE)
   1.487 +            && !(flags & SDL_HWSURFACE))) {
   1.488 +        if ((bpp == SDL_VideoSurface->format->BitsPerPixel)
   1.489 +            || (flags & SDL_ANYFORMAT)) {
   1.490 +            SDL_ShadowSurface =
   1.491 +                SDL_CreateRGBSurface(0, width, height,
   1.492 +                                     SDL_VideoSurface->format->BitsPerPixel,
   1.493 +                                     SDL_VideoSurface->format->Rmask,
   1.494 +                                     SDL_VideoSurface->format->Gmask,
   1.495 +                                     SDL_VideoSurface->format->Bmask,
   1.496 +                                     SDL_VideoSurface->format->Amask);
   1.497 +        } else {
   1.498 +            SDL_ShadowSurface =
   1.499 +                SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
   1.500 +        }
   1.501 +        if (!SDL_ShadowSurface) {
   1.502 +            return NULL;
   1.503 +        }
   1.504 +        SDL_ShadowSurface->flags |= surface_flags;
   1.505 +
   1.506 +        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
   1.507 +        if (SDL_ShadowSurface->format->palette) {
   1.508 +            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
   1.509 +            if (SDL_VideoSurface->format->palette) {
   1.510 +                SDL_SetSurfacePalette(SDL_ShadowSurface,
   1.511 +                                      SDL_VideoSurface->format->palette);
   1.512 +            } else {
   1.513 +                SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
   1.514 +                                 SDL_ShadowSurface->format->BitsPerPixel);
   1.515 +            }
   1.516 +        }
   1.517 +    }
   1.518 +    SDL_PublicSurface =
   1.519 +        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
   1.520 +
   1.521 +    /* Clear the surface for display */
   1.522 +    SDL_FillRect(SDL_PublicSurface, NULL, 0);
   1.523 +
   1.524 +    /* We're finally done! */
   1.525 +    return SDL_PublicSurface;
   1.526 +}
   1.527 +
   1.528 +SDL_Surface *
   1.529 +SDL_GetVideoSurface(void)
   1.530 +{
   1.531 +    return SDL_PublicSurface;
   1.532 +}
   1.533 +
   1.534 +SDL_Surface *
   1.535 +SDL_DisplayFormat(SDL_Surface * surface)
   1.536 +{
   1.537 +    Uint32 flags;
   1.538 +
   1.539 +    if (!SDL_PublicSurface) {
   1.540 +        SDL_SetError("No video mode has been set");
   1.541 +        return NULL;
   1.542 +    }
   1.543 +
   1.544 +    /* Set the flags appropriate for copying to display surface */
   1.545 +    flags = SDL_SWSURFACE;
   1.546 +#ifdef AUTORLE_DISPLAYFORMAT
   1.547 +    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
   1.548 +    flags |= SDL_RLEACCELOK;
   1.549 +#else
   1.550 +    flags |=
   1.551 +        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
   1.552 +#endif
   1.553 +    return SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags);
   1.554 +}
   1.555 +
   1.556 +SDL_Surface *
   1.557 +SDL_DisplayFormatAlpha(SDL_Surface * surface)
   1.558 +{
   1.559 +    SDL_PixelFormat *vf;
   1.560 +    SDL_PixelFormat *format;
   1.561 +    SDL_Surface *converted;
   1.562 +    Uint32 flags;
   1.563 +    /* default to ARGB8888 */
   1.564 +    Uint32 amask = 0xff000000;
   1.565 +    Uint32 rmask = 0x00ff0000;
   1.566 +    Uint32 gmask = 0x0000ff00;
   1.567 +    Uint32 bmask = 0x000000ff;
   1.568 +
   1.569 +    if (!SDL_PublicSurface) {
   1.570 +        SDL_SetError("No video mode has been set");
   1.571 +        return NULL;
   1.572 +    }
   1.573 +    vf = SDL_PublicSurface->format;
   1.574 +
   1.575 +    switch (vf->BytesPerPixel) {
   1.576 +    case 2:
   1.577 +        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
   1.578 +           For anything else (like ARGB4444) it doesn't matter
   1.579 +           since we have no special code for it anyway */
   1.580 +        if ((vf->Rmask == 0x1f) &&
   1.581 +            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
   1.582 +            rmask = 0xff;
   1.583 +            bmask = 0xff0000;
   1.584 +        }
   1.585 +        break;
   1.586 +
   1.587 +    case 3:
   1.588 +    case 4:
   1.589 +        /* Keep the video format, as long as the high 8 bits are
   1.590 +           unused or alpha */
   1.591 +        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
   1.592 +            rmask = 0xff;
   1.593 +            bmask = 0xff0000;
   1.594 +        }
   1.595 +        break;
   1.596 +
   1.597 +    default:
   1.598 +        /* We have no other optimised formats right now. When/if a new
   1.599 +           optimised alpha format is written, add the converter here */
   1.600 +        break;
   1.601 +    }
   1.602 +    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
   1.603 +    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
   1.604 +    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.605 +    converted = SDL_ConvertSurface(surface, format, flags);
   1.606 +    SDL_FreeFormat(format);
   1.607 +    return converted;
   1.608 +}
   1.609 +
   1.610 +int
   1.611 +SDL_Flip(SDL_Surface * screen)
   1.612 +{
   1.613 +    SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.614 +    return 0;
   1.615 +}
   1.616 +
   1.617 +void
   1.618 +SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
   1.619 +{
   1.620 +    if (screen) {
   1.621 +        SDL_Rect rect;
   1.622 +
   1.623 +        /* Perform some checking */
   1.624 +        if (w == 0)
   1.625 +            w = screen->w;
   1.626 +        if (h == 0)
   1.627 +            h = screen->h;
   1.628 +        if ((int) (x + w) > screen->w)
   1.629 +            return;
   1.630 +        if ((int) (y + h) > screen->h)
   1.631 +            return;
   1.632 +
   1.633 +        /* Fill the rectangle */
   1.634 +        rect.x = (Sint16) x;
   1.635 +        rect.y = (Sint16) y;
   1.636 +        rect.w = (Uint16) w;
   1.637 +        rect.h = (Uint16) h;
   1.638 +        SDL_UpdateRects(screen, 1, &rect);
   1.639 +    }
   1.640 +}
   1.641 +void
   1.642 +SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
   1.643 +{
   1.644 +    int i;
   1.645 +
   1.646 +    if (screen == SDL_ShadowSurface) {
   1.647 +        for (i = 0; i < numrects; ++i) {
   1.648 +            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
   1.649 +                          &rects[i]);
   1.650 +        }
   1.651 +
   1.652 +        /* Fall through to video surface update */
   1.653 +        screen = SDL_VideoSurface;
   1.654 +    }
   1.655 +    if (screen == SDL_VideoSurface) {
   1.656 +        for (i = 0; i < numrects; ++i) {
   1.657 +            SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i],
   1.658 +                           SDL_TextureBlendMode_None,
   1.659 +                           SDL_TextureScaleMode_None);
   1.660 +        }
   1.661 +        SDL_RenderPresent();
   1.662 +    }
   1.663 +}
   1.664 +
   1.665 +void
   1.666 +SDL_WM_SetCaption(const char *title, const char *icon)
   1.667 +{
   1.668 +    if (wm_title) {
   1.669 +        SDL_free(wm_title);
   1.670 +    } else {
   1.671 +        wm_title = SDL_strdup(title);
   1.672 +    }
   1.673 +    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
   1.674 +}
   1.675 +
   1.676 +void
   1.677 +SDL_WM_GetCaption(char **title, char **icon)
   1.678 +{
   1.679 +    if (title) {
   1.680 +        *title = wm_title;
   1.681 +    }
   1.682 +    if (icon) {
   1.683 +        *icon = "";
   1.684 +    }
   1.685 +}
   1.686 +
   1.687 +void
   1.688 +SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
   1.689 +{
   1.690 +    /* FIXME */
   1.691 +}
   1.692 +
   1.693 +int
   1.694 +SDL_WM_IconifyWindow(void)
   1.695 +{
   1.696 +    SDL_MinimizeWindow(SDL_VideoWindow);
   1.697 +    return 0;
   1.698 +}
   1.699 +
   1.700 +int
   1.701 +SDL_WM_ToggleFullScreen(SDL_Surface * surface)
   1.702 +{
   1.703 +    if (SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN) {
   1.704 +        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 0) < 0) {
   1.705 +            return 0;
   1.706 +        }
   1.707 +        SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
   1.708 +    } else {
   1.709 +        if (SDL_SetWindowFullscreen(SDL_VideoWindow, 1) < 0) {
   1.710 +            return 0;
   1.711 +        }
   1.712 +        SDL_PublicSurface->flags |= SDL_FULLSCREEN;
   1.713 +    }
   1.714 +    return 1;
   1.715 +}
   1.716 +
   1.717 +SDL_GrabMode
   1.718 +SDL_WM_GrabInput(SDL_GrabMode mode)
   1.719 +{
   1.720 +    if (mode != SDL_GRAB_QUERY) {
   1.721 +        SDL_SetWindowGrab(SDL_VideoWindow, mode);
   1.722 +    }
   1.723 +    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
   1.724 +}
   1.725 +
   1.726 +void
   1.727 +SDL_WarpMouse(Uint16 x, Uint16 y)
   1.728 +{
   1.729 +    SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
   1.730 +}
   1.731 +
   1.732 +Uint8
   1.733 +SDL_GetAppState(void)
   1.734 +{
   1.735 +    Uint8 state = 0;
   1.736 +    Uint32 flags = 0;
   1.737 +
   1.738 +    flags = SDL_GetWindowFlags(SDL_VideoWindow);
   1.739 +    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
   1.740 +        state |= SDL_APPACTIVE;
   1.741 +    }
   1.742 +    if (flags & SDL_WINDOW_INPUT_FOCUS) {
   1.743 +        state |= SDL_APPINPUTFOCUS;
   1.744 +    }
   1.745 +    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
   1.746 +        state |= SDL_APPMOUSEFOCUS;
   1.747 +    }
   1.748 +    return state;
   1.749 +}
   1.750 +
   1.751 +const SDL_version *
   1.752 +SDL_Linked_Version(void)
   1.753 +{
   1.754 +    static SDL_version version;
   1.755 +    SDL_VERSION(&version);
   1.756 +    return &version;
   1.757 +}
   1.758 +
   1.759 +int
   1.760 +SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
   1.761 +               int firstcolor, int ncolors)
   1.762 +{
   1.763 +    return SDL_SetColors(surface, colors, firstcolor, ncolors);
   1.764 +}
   1.765 +
   1.766 +int
   1.767 +SDL_SetColors(SDL_Surface * surface, const SDL_Color * colors, int firstcolor,
   1.768 +              int ncolors)
   1.769 +{
   1.770 +    if (SDL_SetPaletteColors
   1.771 +        (surface->format->palette, colors, firstcolor, ncolors) == 0) {
   1.772 +        return 1;
   1.773 +    } else {
   1.774 +        return 0;
   1.775 +    }
   1.776 +}
   1.777 +
   1.778 +int
   1.779 +SDL_GetWMInfo(SDL_SysWMinfo * info)
   1.780 +{
   1.781 +    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
   1.782 +}
   1.783 +
   1.784 +#if 0
   1.785 +void
   1.786 +SDL_MoveCursor(int x, int y)
   1.787 +{
   1.788 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.789 +
   1.790 +    /* Erase and update the current mouse position */
   1.791 +    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
   1.792 +        /* Erase and redraw mouse cursor in new position */
   1.793 +        SDL_LockCursor();
   1.794 +        SDL_EraseCursor(SDL_VideoSurface);
   1.795 +        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
   1.796 +        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
   1.797 +        SDL_DrawCursor(SDL_VideoSurface);
   1.798 +        SDL_UnlockCursor();
   1.799 +    } else if (_this->MoveWMCursor) {
   1.800 +        _this->MoveWMCursor(_this, x, y);
   1.801 +    }
   1.802 +}
   1.803 +
   1.804 +/* Keep track of the current cursor colors */
   1.805 +static int palette_changed = 1;
   1.806 +static Uint8 pixels8[2];
   1.807 +
   1.808 +void
   1.809 +SDL_CursorPaletteChanged(void)
   1.810 +{
   1.811 +    palette_changed = 1;
   1.812 +}
   1.813 +
   1.814 +void
   1.815 +SDL_MouseRect(SDL_Rect * area)
   1.816 +{
   1.817 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.818 +    int clip_diff;
   1.819 +
   1.820 +    *area = SDL_cursor->area;
   1.821 +    if (area->x < 0) {
   1.822 +        area->w += area->x;
   1.823 +        area->x = 0;
   1.824 +    }
   1.825 +    if (area->y < 0) {
   1.826 +        area->h += area->y;
   1.827 +        area->y = 0;
   1.828 +    }
   1.829 +    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
   1.830 +    if (clip_diff > 0) {
   1.831 +        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
   1.832 +    }
   1.833 +    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
   1.834 +    if (clip_diff > 0) {
   1.835 +        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
   1.836 +    }
   1.837 +}
   1.838 +
   1.839 +static void
   1.840 +SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
   1.841 +{
   1.842 +    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
   1.843 +    int i, w, h;
   1.844 +    Uint8 *data, datab;
   1.845 +    Uint8 *mask, maskb;
   1.846 +
   1.847 +    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
   1.848 +    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
   1.849 +    switch (screen->format->BytesPerPixel) {
   1.850 +
   1.851 +    case 1:
   1.852 +        {
   1.853 +            Uint8 *dst;
   1.854 +            int dstskip;
   1.855 +
   1.856 +            if (palette_changed) {
   1.857 +                pixels8[0] =
   1.858 +                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
   1.859 +                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
   1.860 +                palette_changed = 0;
   1.861 +            }
   1.862 +            dst = (Uint8 *) screen->pixels +
   1.863 +                (SDL_cursor->area.y + area->y) * screen->pitch +
   1.864 +                SDL_cursor->area.x;
   1.865 +            dstskip = screen->pitch - area->w;
   1.866 +
   1.867 +            for (h = area->h; h; h--) {
   1.868 +                for (w = area->w / 8; w; w--) {
   1.869 +                    maskb = *mask++;
   1.870 +                    datab = *data++;
   1.871 +                    for (i = 0; i < 8; ++i) {
   1.872 +                        if (maskb & 0x80) {
   1.873 +                            *dst = pixels8[datab >> 7];
   1.874 +                        }
   1.875 +                        maskb <<= 1;
   1.876 +                        datab <<= 1;
   1.877 +                        dst++;
   1.878 +                    }
   1.879 +                }
   1.880 +                dst += dstskip;
   1.881 +            }
   1.882 +        }
   1.883 +        break;
   1.884 +
   1.885 +    case 2:
   1.886 +        {
   1.887 +            Uint16 *dst;
   1.888 +            int dstskip;
   1.889 +
   1.890 +            dst = (Uint16 *) screen->pixels +
   1.891 +                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
   1.892 +                SDL_cursor->area.x;
   1.893 +            dstskip = (screen->pitch / 2) - area->w;
   1.894 +
   1.895 +            for (h = area->h; h; h--) {
   1.896 +                for (w = area->w / 8; w; w--) {
   1.897 +                    maskb = *mask++;
   1.898 +                    datab = *data++;
   1.899 +                    for (i = 0; i < 8; ++i) {
   1.900 +                        if (maskb & 0x80) {
   1.901 +                            *dst = (Uint16) pixels[datab >> 7];
   1.902 +                        }
   1.903 +                        maskb <<= 1;
   1.904 +                        datab <<= 1;
   1.905 +                        dst++;
   1.906 +                    }
   1.907 +                }
   1.908 +                dst += dstskip;
   1.909 +            }
   1.910 +        }
   1.911 +        break;
   1.912 +
   1.913 +    case 3:
   1.914 +        {
   1.915 +            Uint8 *dst;
   1.916 +            int dstskip;
   1.917 +
   1.918 +            dst = (Uint8 *) screen->pixels +
   1.919 +                (SDL_cursor->area.y + area->y) * screen->pitch +
   1.920 +                SDL_cursor->area.x * 3;
   1.921 +            dstskip = screen->pitch - area->w * 3;
   1.922 +
   1.923 +            for (h = area->h; h; h--) {
   1.924 +                for (w = area->w / 8; w; w--) {
   1.925 +                    maskb = *mask++;
   1.926 +                    datab = *data++;
   1.927 +                    for (i = 0; i < 8; ++i) {
   1.928 +                        if (maskb & 0x80) {
   1.929 +                            SDL_memset(dst, pixels[datab >> 7], 3);
   1.930 +                        }
   1.931 +                        maskb <<= 1;
   1.932 +                        datab <<= 1;
   1.933 +                        dst += 3;
   1.934 +                    }
   1.935 +                }
   1.936 +                dst += dstskip;
   1.937 +            }
   1.938 +        }
   1.939 +        break;
   1.940 +
   1.941 +    case 4:
   1.942 +        {
   1.943 +            Uint32 *dst;
   1.944 +            int dstskip;
   1.945 +
   1.946 +            dst = (Uint32 *) screen->pixels +
   1.947 +                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
   1.948 +                SDL_cursor->area.x;
   1.949 +            dstskip = (screen->pitch / 4) - area->w;
   1.950 +
   1.951 +            for (h = area->h; h; h--) {
   1.952 +                for (w = area->w / 8; w; w--) {
   1.953 +                    maskb = *mask++;
   1.954 +                    datab = *data++;
   1.955 +                    for (i = 0; i < 8; ++i) {
   1.956 +                        if (maskb & 0x80) {
   1.957 +                            *dst = pixels[datab >> 7];
   1.958 +                        }
   1.959 +                        maskb <<= 1;
   1.960 +                        datab <<= 1;
   1.961 +                        dst++;
   1.962 +                    }
   1.963 +                }
   1.964 +                dst += dstskip;
   1.965 +            }
   1.966 +        }
   1.967 +        break;
   1.968 +    }
   1.969 +}
   1.970 +
   1.971 +static void
   1.972 +SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
   1.973 +{
   1.974 +    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
   1.975 +    int h;
   1.976 +    int x, minx, maxx;
   1.977 +    Uint8 *data, datab = 0;
   1.978 +    Uint8 *mask, maskb = 0;
   1.979 +    Uint8 *dst;
   1.980 +    int dstbpp, dstskip;
   1.981 +
   1.982 +    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
   1.983 +    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
   1.984 +    dstbpp = screen->format->BytesPerPixel;
   1.985 +    dst = (Uint8 *) screen->pixels +
   1.986 +        (SDL_cursor->area.y + area->y) * screen->pitch +
   1.987 +        SDL_cursor->area.x * dstbpp;
   1.988 +    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
   1.989 +
   1.990 +    minx = area->x;
   1.991 +    maxx = area->x + area->w;
   1.992 +    if (screen->format->BytesPerPixel == 1) {
   1.993 +        if (palette_changed) {
   1.994 +            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
   1.995 +            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
   1.996 +            palette_changed = 0;
   1.997 +        }
   1.998 +        for (h = area->h; h; h--) {
   1.999 +            for (x = 0; x < SDL_cursor->area.w; ++x) {
  1.1000 +                if ((x % 8) == 0) {
  1.1001 +                    maskb = *mask++;
  1.1002 +                    datab = *data++;
  1.1003 +                }
  1.1004 +                if ((x >= minx) && (x < maxx)) {
  1.1005 +                    if (maskb & 0x80) {
  1.1006 +                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
  1.1007 +                    }
  1.1008 +                }
  1.1009 +                maskb <<= 1;
  1.1010 +                datab <<= 1;
  1.1011 +                dst += dstbpp;
  1.1012 +            }
  1.1013 +            dst += dstskip;
  1.1014 +        }
  1.1015 +    } else {
  1.1016 +        for (h = area->h; h; h--) {
  1.1017 +            for (x = 0; x < SDL_cursor->area.w; ++x) {
  1.1018 +                if ((x % 8) == 0) {
  1.1019 +                    maskb = *mask++;
  1.1020 +                    datab = *data++;
  1.1021 +                }
  1.1022 +                if ((x >= minx) && (x < maxx)) {
  1.1023 +                    if (maskb & 0x80) {
  1.1024 +                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
  1.1025 +                    }
  1.1026 +                }
  1.1027 +                maskb <<= 1;
  1.1028 +                datab <<= 1;
  1.1029 +                dst += dstbpp;
  1.1030 +            }
  1.1031 +            dst += dstskip;
  1.1032 +        }
  1.1033 +    }
  1.1034 +}
  1.1035 +
  1.1036 +/* This handles the ugly work of converting the saved cursor background from
  1.1037 +   the pixel format of the shadow surface to that of the video surface.
  1.1038 +   This is only necessary when blitting from a shadow surface of a different
  1.1039 +   pixel format than the video surface, and using a software rendered cursor.
  1.1040 +*/
  1.1041 +static void
  1.1042 +SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
  1.1043 +{
  1.1044 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1.1045 +    SDL_BlitInfo info;
  1.1046 +    SDL_loblit RunBlit;
  1.1047 +
  1.1048 +    /* Make sure we can steal the blit mapping */
  1.1049 +    if (screen->map->dst != SDL_VideoSurface) {
  1.1050 +        return;
  1.1051 +    }
  1.1052 +
  1.1053 +    /* Set up the blit information */
  1.1054 +    info.s_pixels = SDL_cursor->save[1];
  1.1055 +    info.s_width = w;
  1.1056 +    info.s_height = h;
  1.1057 +    info.s_skip = 0;
  1.1058 +    info.d_pixels = SDL_cursor->save[0];
  1.1059 +    info.d_width = w;
  1.1060 +    info.d_height = h;
  1.1061 +    info.d_skip = 0;
  1.1062 +    info.aux_data = screen->map->sw_data->aux_data;
  1.1063 +    info.src = screen->format;
  1.1064 +    info.table = screen->map->table;
  1.1065 +    info.dst = SDL_VideoSurface->format;
  1.1066 +    RunBlit = screen->map->sw_data->blit;
  1.1067 +
  1.1068 +    /* Run the actual software blit */
  1.1069 +    RunBlit(&info);
  1.1070 +}
  1.1071 +
  1.1072 +void
  1.1073 +SDL_DrawCursorNoLock(SDL_Surface * screen)
  1.1074 +{
  1.1075 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1.1076 +    SDL_Rect area;
  1.1077 +
  1.1078 +    /* Get the mouse rectangle, clipped to the screen */
  1.1079 +    SDL_MouseRect(&area);
  1.1080 +    if ((area.w == 0) || (area.h == 0)) {
  1.1081 +        return;
  1.1082 +    }
  1.1083 +
  1.1084 +    /* Copy mouse background */
  1.1085 +    {
  1.1086 +        int w, h, screenbpp;
  1.1087 +        Uint8 *src, *dst;
  1.1088 +
  1.1089 +        /* Set up the copy pointers */
  1.1090 +        screenbpp = screen->format->BytesPerPixel;
  1.1091 +        if ((screen == SDL_VideoSurface) ||
  1.1092 +            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
  1.1093 +            dst = SDL_cursor->save[0];
  1.1094 +        } else {
  1.1095 +            dst = SDL_cursor->save[1];
  1.1096 +        }
  1.1097 +        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
  1.1098 +            area.x * screenbpp;
  1.1099 +
  1.1100 +        /* Perform the copy */
  1.1101 +        w = area.w * screenbpp;
  1.1102 +        h = area.h;
  1.1103 +        while (h--) {
  1.1104 +            SDL_memcpy(dst, src, w);
  1.1105 +            dst += w;
  1.1106 +            src += screen->pitch;
  1.1107 +        }
  1.1108 +    }
  1.1109 +
  1.1110 +    /* Draw the mouse cursor */
  1.1111 +    area.x -= SDL_cursor->area.x;
  1.1112 +    area.y -= SDL_cursor->area.y;
  1.1113 +    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
  1.1114 +        SDL_DrawCursorFast(screen, &area);
  1.1115 +    } else {
  1.1116 +        SDL_DrawCursorSlow(screen, &area);
  1.1117 +    }
  1.1118 +}
  1.1119 +
  1.1120 +void
  1.1121 +SDL_DrawCursor(SDL_Surface * screen)
  1.1122 +{
  1.1123 +    /* Lock the screen if necessary */
  1.1124 +    if (screen == NULL) {
  1.1125 +        return;
  1.1126 +    }
  1.1127 +    if (SDL_MUSTLOCK(screen)) {
  1.1128 +        if (SDL_LockSurface(screen) < 0) {
  1.1129 +            return;
  1.1130 +        }
  1.1131 +    }
  1.1132 +
  1.1133 +    SDL_DrawCursorNoLock(screen);
  1.1134 +
  1.1135 +    /* Unlock the screen and update if necessary */
  1.1136 +    if (SDL_MUSTLOCK(screen)) {
  1.1137 +        SDL_UnlockSurface(screen);
  1.1138 +    }
  1.1139 +    if ((screen->flags & SDL_SCREEN_SURFACE) &&
  1.1140 +        !(screen->flags & SDL_HWSURFACE)) {
  1.1141 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1.1142 +        SDL_Window *window;
  1.1143 +        SDL_Rect area;
  1.1144 +
  1.1145 +        window = SDL_GetWindowFromSurface(screen);
  1.1146 +        if (!window) {
  1.1147 +            return;
  1.1148 +        }
  1.1149 +
  1.1150 +        SDL_MouseRect(&area);
  1.1151 +
  1.1152 +        if (_this->UpdateWindowSurface) {
  1.1153 +            _this->UpdateWindowSurface(_this, window, 1, &area);
  1.1154 +        }
  1.1155 +    }
  1.1156 +}
  1.1157 +
  1.1158 +void
  1.1159 +SDL_EraseCursorNoLock(SDL_Surface * screen)
  1.1160 +{
  1.1161 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1.1162 +    SDL_Window *window;
  1.1163 +    SDL_Rect area;
  1.1164 +
  1.1165 +    /* Get the window associated with the surface */
  1.1166 +    window = SDL_GetWindowFromSurface(screen);
  1.1167 +    if (!window || !window->surface) {
  1.1168 +        return;
  1.1169 +    }
  1.1170 +
  1.1171 +    /* Get the mouse rectangle, clipped to the screen */
  1.1172 +    SDL_MouseRect(&area);
  1.1173 +    if ((area.w == 0) || (area.h == 0)) {
  1.1174 +        return;
  1.1175 +    }
  1.1176 +
  1.1177 +    /* Copy mouse background */
  1.1178 +    {
  1.1179 +        int w, h, screenbpp;
  1.1180 +        Uint8 *src, *dst;
  1.1181 +
  1.1182 +        /* Set up the copy pointers */
  1.1183 +        screenbpp = screen->format->BytesPerPixel;
  1.1184 +        if ((screen->flags & SDL_SCREEN_SURFACE) ||
  1.1185 +            FORMAT_EQUAL(screen->format, window->surface->format)) {
  1.1186 +            src = SDL_cursor->save[0];
  1.1187 +        } else {
  1.1188 +            src = SDL_cursor->save[1];
  1.1189 +        }
  1.1190 +        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
  1.1191 +            area.x * screenbpp;
  1.1192 +
  1.1193 +        /* Perform the copy */
  1.1194 +        w = area.w * screenbpp;
  1.1195 +        h = area.h;
  1.1196 +        while (h--) {
  1.1197 +            SDL_memcpy(dst, src, w);
  1.1198 +            src += w;
  1.1199 +            dst += screen->pitch;
  1.1200 +        }
  1.1201 +
  1.1202 +        /* Perform pixel conversion on cursor background */
  1.1203 +        if (src > SDL_cursor->save[1]) {
  1.1204 +            SDL_ConvertCursorSave(screen, area.w, area.h);
  1.1205 +        }
  1.1206 +    }
  1.1207 +}
  1.1208 +
  1.1209 +void
  1.1210 +SDL_EraseCursor(SDL_Surface * screen)
  1.1211 +{
  1.1212 +    /* Lock the screen if necessary */
  1.1213 +    if (screen == NULL) {
  1.1214 +        return;
  1.1215 +    }
  1.1216 +    if (SDL_MUSTLOCK(screen)) {
  1.1217 +        if (SDL_LockSurface(screen) < 0) {
  1.1218 +            return;
  1.1219 +        }
  1.1220 +    }
  1.1221 +
  1.1222 +    SDL_EraseCursorNoLock(screen);
  1.1223 +
  1.1224 +    /* Unlock the screen and update if necessary */
  1.1225 +    if (SDL_MUSTLOCK(screen)) {
  1.1226 +        SDL_UnlockSurface(screen);
  1.1227 +    }
  1.1228 +    if ((screen->flags & SDL_SCREEN_SURFACE) &&
  1.1229 +        !(screen->flags & SDL_HWSURFACE)) {
  1.1230 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1.1231 +        SDL_Window *window;
  1.1232 +        SDL_Rect area;
  1.1233 +
  1.1234 +        window = SDL_GetWindowFromSurface(screen);
  1.1235 +        if (!window) {
  1.1236 +            return;
  1.1237 +        }
  1.1238 +
  1.1239 +        SDL_MouseRect(&area);
  1.1240 +
  1.1241 +        if (_this->UpdateWindowSurface) {
  1.1242 +            _this->UpdateWindowSurface(_this, window, 1, &area);
  1.1243 +        }
  1.1244 +    }
  1.1245 +}
  1.1246 +
  1.1247 +/* Reset the cursor on video mode change
  1.1248 +   FIXME:  Keep track of all cursors, and reset them all.
  1.1249 + */
  1.1250 +void
  1.1251 +SDL_ResetCursor(void)
  1.1252 +{
  1.1253 +    int savelen;
  1.1254 +
  1.1255 +    if (SDL_cursor) {
  1.1256 +        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
  1.1257 +        SDL_cursor->area.x = 0;
  1.1258 +        SDL_cursor->area.y = 0;
  1.1259 +        SDL_memset(SDL_cursor->save[0], 0, savelen);
  1.1260 +    }
  1.1261 +}
  1.1262 +#endif
  1.1263 +
  1.1264 +struct private_yuvhwdata
  1.1265 +{
  1.1266 +    Uint16 pitches[3];
  1.1267 +    Uint8 *planes[3];
  1.1268 +
  1.1269 +    SDL_TextureID textureID;
  1.1270 +};
  1.1271 +
  1.1272 +SDL_Overlay *
  1.1273 +SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL_Surface * display)
  1.1274 +{
  1.1275 +    SDL_Overlay *overlay;
  1.1276 +    Uint32 texture_format;
  1.1277 +
  1.1278 +    if ((display->flags & SDL_OPENGL) == SDL_OPENGL) {
  1.1279 +        SDL_SetError("YUV overlays are not supported in OpenGL mode");
  1.1280 +        return NULL;
  1.1281 +    }
  1.1282 +
  1.1283 +    if (display != SDL_PublicSurface) {
  1.1284 +        SDL_SetError("YUV display is only supported on the screen surface");
  1.1285 +        return NULL;
  1.1286 +    }
  1.1287 +
  1.1288 +    switch (format) {
  1.1289 +    case SDL_YV12_OVERLAY:
  1.1290 +        texture_format = SDL_PixelFormat_YV12;
  1.1291 +        break;
  1.1292 +    case SDL_IYUV_OVERLAY:
  1.1293 +        texture_format = SDL_PixelFormat_IYUV;
  1.1294 +        break;
  1.1295 +    case SDL_YUY2_OVERLAY:
  1.1296 +        texture_format = SDL_PixelFormat_YUY2;
  1.1297 +        break;
  1.1298 +    case SDL_UYVY_OVERLAY:
  1.1299 +        texture_format = SDL_PixelFormat_UYVY;
  1.1300 +        break;
  1.1301 +    case SDL_YVYU_OVERLAY:
  1.1302 +        texture_format = SDL_PixelFormat_YVYU;
  1.1303 +        break;
  1.1304 +    default:
  1.1305 +        SDL_SetError("Unknown YUV format");
  1.1306 +        return NULL;
  1.1307 +    }
  1.1308 +
  1.1309 +    overlay = (SDL_Overlay *) SDL_malloc(sizeof(*overlay));
  1.1310 +    if (!overlay) {
  1.1311 +        SDL_OutOfMemory();
  1.1312 +        return NULL;
  1.1313 +    }
  1.1314 +    SDL_zerop(overlay);
  1.1315 +
  1.1316 +    overlay->hwdata =
  1.1317 +        (struct private_yuvhwdata *) SDL_malloc(sizeof(*overlay->hwdata));
  1.1318 +    if (!overlay->hwdata) {
  1.1319 +        SDL_free(overlay);
  1.1320 +        SDL_OutOfMemory();
  1.1321 +        return NULL;
  1.1322 +    }
  1.1323 +
  1.1324 +    overlay->format = format;
  1.1325 +    overlay->w = w;
  1.1326 +    overlay->h = h;
  1.1327 +    if (format == SDL_YV12_OVERLAY || format == SDL_IYUV_OVERLAY) {
  1.1328 +        overlay->planes = 3;
  1.1329 +    } else {
  1.1330 +        overlay->planes = 1;
  1.1331 +    }
  1.1332 +    overlay->pitches = overlay->hwdata->pitches;
  1.1333 +    overlay->pixels = overlay->hwdata->planes;
  1.1334 +
  1.1335 +    switch (format) {
  1.1336 +    case SDL_YV12_OVERLAY:
  1.1337 +    case SDL_IYUV_OVERLAY:
  1.1338 +        overlay->pitches[0] = overlay->w;
  1.1339 +        overlay->pitches[1] = overlay->w / 2;
  1.1340 +        overlay->pitches[2] = overlay->w / 2;
  1.1341 +        break;
  1.1342 +    case SDL_YUY2_OVERLAY:
  1.1343 +    case SDL_UYVY_OVERLAY:
  1.1344 +    case SDL_YVYU_OVERLAY:
  1.1345 +        overlay->pitches[0] = overlay->w * 2;
  1.1346 +        break;
  1.1347 +    }
  1.1348 +
  1.1349 +    overlay->hwdata->textureID =
  1.1350 +        SDL_CreateTexture(texture_format, SDL_TextureAccess_Local, w, h);
  1.1351 +    if (!overlay->hwdata->textureID) {
  1.1352 +        SDL_FreeYUVOverlay(overlay);
  1.1353 +        return NULL;
  1.1354 +    }
  1.1355 +
  1.1356 +    return overlay;
  1.1357 +}
  1.1358 +
  1.1359 +int
  1.1360 +SDL_LockYUVOverlay(SDL_Overlay * overlay)
  1.1361 +{
  1.1362 +    void *pixels;
  1.1363 +    int pitch;
  1.1364 +    if (SDL_LockTexture(overlay->hwdata->textureID, NULL, 1, &pixels, &pitch)
  1.1365 +        < 0) {
  1.1366 +        return -1;
  1.1367 +    }
  1.1368 +    switch (overlay->format) {
  1.1369 +    case SDL_YV12_OVERLAY:
  1.1370 +    case SDL_IYUV_OVERLAY:
  1.1371 +        overlay->pixels[0] = (Uint8 *) pixels;
  1.1372 +        overlay->pixels[1] =
  1.1373 +            overlay->pixels[0] + overlay->pitches[0] * overlay->h;
  1.1374 +        overlay->pixels[2] =
  1.1375 +            overlay->pixels[1] + overlay->pitches[1] * overlay->h;
  1.1376 +        break;
  1.1377 +    case SDL_YUY2_OVERLAY:
  1.1378 +    case SDL_UYVY_OVERLAY:
  1.1379 +    case SDL_YVYU_OVERLAY:
  1.1380 +        overlay->pixels[0] = (Uint8 *) pixels;
  1.1381 +        break;
  1.1382 +    }
  1.1383 +    return 0;
  1.1384 +}
  1.1385 +
  1.1386 +void
  1.1387 +SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
  1.1388 +{
  1.1389 +    SDL_UnlockTexture(overlay->hwdata->textureID);
  1.1390 +}
  1.1391 +
  1.1392 +int
  1.1393 +SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
  1.1394 +{
  1.1395 +    if (SDL_RenderCopy(overlay->hwdata->textureID, NULL, dstrect,
  1.1396 +                       SDL_TextureBlendMode_None,
  1.1397 +                       SDL_TextureScaleMode_Fast) < 0) {
  1.1398 +        return -1;
  1.1399 +    }
  1.1400 +    SDL_RenderPresent();
  1.1401 +    return 0;
  1.1402 +}
  1.1403 +
  1.1404 +void
  1.1405 +SDL_FreeYUVOverlay(SDL_Overlay * overlay)
  1.1406 +{
  1.1407 +    if (overlay) {
  1.1408 +        if (overlay->hwdata) {
  1.1409 +            if (overlay->hwdata->textureID) {
  1.1410 +                SDL_DestroyTexture(overlay->hwdata->textureID);
  1.1411 +            }
  1.1412 +            SDL_free(overlay->hwdata);
  1.1413 +        }
  1.1414 +        SDL_free(overlay);
  1.1415 +    }
  1.1416 +}
  1.1417 +
  1.1418 +/* vi: set ts=4 sw=4 expandtab: */