src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 16 Jun 2006 06:00:31 +0000
branchSDL-1.3
changeset 1682 7ae8018b2e5d
parent 1681 80a5e6a4e1e2
child 1683 396a35389351
permissions -rw-r--r--
Default palette entries to white, instead of black.
More palettized video mode support...
slouken@1667
     1
/*
slouken@1667
     2
    SDL - Simple DirectMedia Layer
slouken@1667
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1667
     4
slouken@1667
     5
    This library is free software; you can redistribute it and/or
slouken@1667
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1667
     7
    License as published by the Free Software Foundation; either
slouken@1667
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1667
     9
slouken@1667
    10
    This library is distributed in the hope that it will be useful,
slouken@1667
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1667
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1667
    13
    Lesser General Public License for more details.
slouken@1667
    14
slouken@1667
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1667
    16
    License along with this library; if not, write to the Free Software
slouken@1667
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1667
    18
slouken@1667
    19
    Sam Lantinga
slouken@1667
    20
    slouken@libsdl.org
slouken@1667
    21
*/
slouken@1667
    22
#include "SDL_config.h"
slouken@1667
    23
slouken@1667
    24
/* This file contains functions for backwards compatibility with SDL 1.2 */
slouken@1667
    25
slouken@1667
    26
#include "SDL.h"
slouken@1667
    27
slouken@1678
    28
#include "video/SDL_pixels_c.h"
slouken@1667
    29
slouken@1667
    30
slouken@1678
    31
static SDL_WindowID SDL_VideoWindow;
slouken@1678
    32
static SDL_TextureID SDL_VideoTexture;
slouken@1678
    33
static SDL_Surface *SDL_VideoSurface;
slouken@1678
    34
static SDL_Surface *SDL_ShadowSurface;
slouken@1678
    35
static SDL_Surface *SDL_PublicSurface;
slouken@1667
    36
static char *wm_title;
slouken@1667
    37
slouken@1667
    38
char *
slouken@1668
    39
SDL_AudioDriverName(char *namebuf, int maxlen)
slouken@1667
    40
{
slouken@1668
    41
    const char *name = SDL_GetCurrentAudioDriver();
slouken@1667
    42
    if (name) {
slouken@1668
    43
        SDL_strlcpy(namebuf, name, maxlen);
slouken@1667
    44
        return namebuf;
slouken@1667
    45
    }
slouken@1667
    46
    return NULL;
slouken@1667
    47
}
slouken@1667
    48
slouken@1667
    49
char *
slouken@1668
    50
SDL_VideoDriverName(char *namebuf, int maxlen)
slouken@1667
    51
{
slouken@1668
    52
    const char *name = SDL_GetCurrentVideoDriver();
slouken@1667
    53
    if (name) {
slouken@1668
    54
        SDL_strlcpy(namebuf, name, maxlen);
slouken@1667
    55
        return namebuf;
slouken@1667
    56
    }
slouken@1667
    57
    return NULL;
slouken@1667
    58
}
slouken@1667
    59
slouken@1670
    60
const SDL_VideoInfo *
slouken@1670
    61
SDL_GetVideoInfo(void)
slouken@1670
    62
{
slouken@1670
    63
    static SDL_VideoInfo info;
slouken@1670
    64
slouken@1670
    65
    /* Memory leak, compatibility code, who cares? */
slouken@1670
    66
    if (!info.vfmt && SDL_GetDesktopDisplayMode()) {
slouken@1670
    67
        int bpp;
slouken@1670
    68
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
    69
slouken@1670
    70
        SDL_PixelFormatEnumToMasks(SDL_GetDesktopDisplayMode()->format, &bpp,
slouken@1670
    71
                                   &Rmask, &Gmask, &Bmask, &Amask);
slouken@1670
    72
        info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
    73
    }
slouken@1670
    74
    return &info;
slouken@1670
    75
}
slouken@1670
    76
slouken@1667
    77
int
slouken@1668
    78
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
slouken@1667
    79
{
slouken@1667
    80
    int i, actual_bpp = 0;
slouken@1667
    81
slouken@1668
    82
    if (!SDL_GetVideoDevice()) {
slouken@1667
    83
        return 0;
slouken@1667
    84
    }
slouken@1667
    85
slouken@1667
    86
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1668
    87
        return SDL_BITSPERPIXEL(SDL_GetDesktopDisplayMode()->format);
slouken@1667
    88
    }
slouken@1667
    89
slouken@1668
    90
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1668
    91
        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
slouken@1667
    92
        if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
slouken@1667
    93
            if (!mode->format) {
slouken@1667
    94
                return bpp;
slouken@1667
    95
            }
slouken@1668
    96
            if (SDL_BITSPERPIXEL(mode->format) >= bpp) {
slouken@1668
    97
                actual_bpp = SDL_BITSPERPIXEL(mode->format);
slouken@1667
    98
            }
slouken@1667
    99
        }
slouken@1667
   100
    }
slouken@1667
   101
    return actual_bpp;
slouken@1667
   102
}
slouken@1667
   103
slouken@1667
   104
SDL_Rect **
slouken@1668
   105
SDL_ListModes(SDL_PixelFormat * format, Uint32 flags)
slouken@1667
   106
{
slouken@1667
   107
    int i, nmodes;
slouken@1667
   108
    SDL_Rect **modes;
slouken@1667
   109
slouken@1668
   110
    if (!SDL_GetVideoDevice()) {
slouken@1667
   111
        return NULL;
slouken@1667
   112
    }
slouken@1667
   113
slouken@1667
   114
    if (!(flags & SDL_FULLSCREEN)) {
slouken@1667
   115
        return (SDL_Rect **) (-1);
slouken@1667
   116
    }
slouken@1667
   117
slouken@1667
   118
    /* Memory leak, but this is a compatibility function, who cares? */
slouken@1667
   119
    nmodes = 0;
slouken@1668
   120
    for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
slouken@1668
   121
        const SDL_DisplayMode *mode = SDL_GetDisplayMode(i);
slouken@1667
   122
        if (!mode->w || !mode->h) {
slouken@1667
   123
            return (SDL_Rect **) (-1);
slouken@1667
   124
        }
slouken@1668
   125
        if (SDL_BITSPERPIXEL(mode->format) != format->BitsPerPixel) {
slouken@1667
   126
            continue;
slouken@1667
   127
        }
slouken@1667
   128
        if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
slouken@1667
   129
            && modes[nmodes - 1]->h == mode->h) {
slouken@1667
   130
            continue;
slouken@1667
   131
        }
slouken@1667
   132
slouken@1668
   133
        modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
slouken@1667
   134
        if (!modes) {
slouken@1667
   135
            return NULL;
slouken@1667
   136
        }
slouken@1668
   137
        modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
slouken@1667
   138
        if (!modes[nmodes]) {
slouken@1667
   139
            return NULL;
slouken@1667
   140
        }
slouken@1667
   141
        modes[nmodes]->x = 0;
slouken@1667
   142
        modes[nmodes]->y = 0;
slouken@1667
   143
        modes[nmodes]->w = mode->w;
slouken@1667
   144
        modes[nmodes]->h = mode->h;
slouken@1667
   145
        ++nmodes;
slouken@1667
   146
    }
slouken@1667
   147
    if (modes) {
slouken@1667
   148
        modes[nmodes] = NULL;
slouken@1667
   149
    }
slouken@1667
   150
    return modes;
slouken@1667
   151
}
slouken@1667
   152
slouken@1669
   153
static int (*orig_eventfilter) (const SDL_Event * event);
slouken@1669
   154
slouken@1669
   155
static int
slouken@1669
   156
SDL_CompatEventFilter(const SDL_Event * event)
slouken@1669
   157
{
slouken@1669
   158
    SDL_Event fake;
slouken@1669
   159
slouken@1669
   160
    switch (event->type) {
slouken@1669
   161
    case SDL_WINDOWEVENT:
slouken@1669
   162
        switch (event->window.event) {
slouken@1669
   163
        case SDL_WINDOWEVENT_RESIZED:
slouken@1669
   164
            fake.type = SDL_VIDEORESIZE;
slouken@1669
   165
            fake.resize.w = event->window.data1;
slouken@1669
   166
            fake.resize.h = event->window.data2;
slouken@1669
   167
            SDL_PushEvent(&fake);
slouken@1669
   168
            break;
slouken@1669
   169
        case SDL_WINDOWEVENT_MINIMIZED:
slouken@1669
   170
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   171
            fake.active.gain = 0;
slouken@1669
   172
            fake.active.state = SDL_APPACTIVE;
slouken@1669
   173
            SDL_PushEvent(&fake);
slouken@1669
   174
            break;
slouken@1669
   175
        case SDL_WINDOWEVENT_RESTORED:
slouken@1669
   176
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   177
            fake.active.gain = 1;
slouken@1669
   178
            fake.active.state = SDL_APPACTIVE;
slouken@1669
   179
            SDL_PushEvent(&fake);
slouken@1669
   180
            break;
slouken@1669
   181
        case SDL_WINDOWEVENT_ENTER:
slouken@1669
   182
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   183
            fake.active.gain = 1;
slouken@1669
   184
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1669
   185
            SDL_PushEvent(&fake);
slouken@1669
   186
            break;
slouken@1669
   187
        case SDL_WINDOWEVENT_LEAVE:
slouken@1669
   188
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   189
            fake.active.gain = 0;
slouken@1669
   190
            fake.active.state = SDL_APPMOUSEFOCUS;
slouken@1669
   191
            SDL_PushEvent(&fake);
slouken@1669
   192
            break;
slouken@1669
   193
        case SDL_WINDOWEVENT_FOCUS_GAINED:
slouken@1669
   194
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   195
            fake.active.gain = 1;
slouken@1669
   196
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1669
   197
            SDL_PushEvent(&fake);
slouken@1669
   198
            break;
slouken@1669
   199
        case SDL_WINDOWEVENT_FOCUS_LOST:
slouken@1669
   200
            fake.type = SDL_ACTIVEEVENT;
slouken@1669
   201
            fake.active.gain = 1;
slouken@1669
   202
            fake.active.state = SDL_APPINPUTFOCUS;
slouken@1669
   203
            SDL_PushEvent(&fake);
slouken@1669
   204
            break;
slouken@1669
   205
        }
slouken@1669
   206
    }
slouken@1669
   207
    return orig_eventfilter(event);
slouken@1669
   208
}
slouken@1669
   209
slouken@1667
   210
SDL_Surface *
slouken@1668
   211
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
slouken@1667
   212
{
slouken@1669
   213
    int (*filter) (const SDL_Event * event);
slouken@1669
   214
    const SDL_DisplayMode *desktop_mode;
slouken@1667
   215
    SDL_DisplayMode mode;
slouken@1667
   216
    int i;
slouken@1667
   217
    Uint32 window_flags;
slouken@1667
   218
    Uint32 desktop_format;
slouken@1667
   219
    Uint32 desired_format;
slouken@1678
   220
    Uint32 texture_format;
slouken@1682
   221
    Uint32 surface_flags;
slouken@1667
   222
slouken@1668
   223
    if (!SDL_GetVideoDevice()) {
slouken@1668
   224
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
slouken@1667
   225
            return NULL;
slouken@1667
   226
        }
slouken@1667
   227
    }
slouken@1667
   228
slouken@1667
   229
    /* Destroy existing window */
slouken@1678
   230
    SDL_PublicSurface = NULL;
slouken@1678
   231
    if (SDL_ShadowSurface) {
slouken@1678
   232
        SDL_FreeSurface(SDL_ShadowSurface);
slouken@1678
   233
        SDL_ShadowSurface = NULL;
slouken@1678
   234
    }
slouken@1678
   235
    if (SDL_VideoSurface) {
slouken@1678
   236
        SDL_FreeSurface(SDL_ShadowSurface);
slouken@1678
   237
        SDL_VideoSurface = NULL;
slouken@1678
   238
    }
slouken@1678
   239
    SDL_DestroyWindow(SDL_VideoWindow);
slouken@1667
   240
slouken@1669
   241
    /* Set up the event filter */
slouken@1669
   242
    filter = SDL_GetEventFilter();
slouken@1669
   243
    if (filter != SDL_CompatEventFilter) {
slouken@1669
   244
        orig_eventfilter = filter;
slouken@1669
   245
    }
slouken@1669
   246
    SDL_SetEventFilter(SDL_CompatEventFilter);
slouken@1669
   247
slouken@1667
   248
    /* Create a new window */
slouken@1667
   249
    window_flags = SDL_WINDOW_SHOWN;
slouken@1667
   250
    if (flags & SDL_FULLSCREEN) {
slouken@1667
   251
        window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1667
   252
    }
slouken@1667
   253
    if (flags & SDL_OPENGL) {
slouken@1667
   254
        window_flags |= SDL_WINDOW_OPENGL;
slouken@1667
   255
    }
slouken@1667
   256
    if (flags & SDL_RESIZABLE) {
slouken@1667
   257
        window_flags |= SDL_WINDOW_RESIZABLE;
slouken@1667
   258
    }
slouken@1667
   259
    if (flags & SDL_NOFRAME) {
slouken@1667
   260
        window_flags |= SDL_WINDOW_BORDERLESS;
slouken@1667
   261
    }
slouken@1678
   262
    SDL_VideoWindow =
slouken@1678
   263
        SDL_CreateWindow(wm_title, 0, 0, width, height, window_flags);
slouken@1678
   264
    if (!SDL_VideoWindow) {
slouken@1667
   265
        return NULL;
slouken@1667
   266
    }
slouken@1667
   267
slouken@1682
   268
    window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1682
   269
    surface_flags = SDL_SCREEN_SURFACE;
slouken@1682
   270
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1682
   271
        surface_flags |= SDL_FULLSCREEN;
slouken@1682
   272
    }
slouken@1682
   273
    if (window_flags & SDL_WINDOW_OPENGL) {
slouken@1682
   274
        surface_flags |= SDL_OPENGL;
slouken@1682
   275
    }
slouken@1682
   276
    if (window_flags & SDL_WINDOW_RESIZABLE) {
slouken@1682
   277
        surface_flags |= SDL_RESIZABLE;
slouken@1682
   278
    }
slouken@1682
   279
    if (window_flags & SDL_WINDOW_BORDERLESS) {
slouken@1682
   280
        surface_flags |= SDL_NOFRAME;
slouken@1682
   281
    }
slouken@1682
   282
slouken@1667
   283
    /* Set up the desired display mode */
slouken@1669
   284
    desktop_mode = SDL_GetDesktopDisplayMode();
slouken@1669
   285
    desktop_format = desktop_mode->format;
slouken@1669
   286
    if (desktop_format && ((flags & SDL_ANYFORMAT)
slouken@1669
   287
                           || (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
slouken@1667
   288
        desired_format = desktop_format;
slouken@1667
   289
    } else {
slouken@1667
   290
        switch (bpp) {
slouken@1669
   291
        case 0:
slouken@1669
   292
            if (desktop_format) {
slouken@1669
   293
                desired_format = desktop_format;
slouken@1669
   294
            } else {
slouken@1669
   295
                desired_format = SDL_PixelFormat_RGB888;
slouken@1669
   296
            }
slouken@1669
   297
            break;
slouken@1667
   298
        case 8:
slouken@1667
   299
            desired_format = SDL_PixelFormat_Index8;
slouken@1667
   300
            break;
slouken@1667
   301
        case 15:
slouken@1667
   302
            desired_format = SDL_PixelFormat_RGB555;
slouken@1667
   303
            break;
slouken@1667
   304
        case 16:
slouken@1667
   305
            desired_format = SDL_PixelFormat_RGB565;
slouken@1667
   306
            break;
slouken@1667
   307
        case 24:
slouken@1667
   308
            desired_format = SDL_PixelFormat_RGB24;
slouken@1667
   309
            break;
slouken@1667
   310
        case 32:
slouken@1667
   311
            desired_format = SDL_PixelFormat_RGB888;
slouken@1667
   312
            break;
slouken@1667
   313
        default:
slouken@1668
   314
            SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
slouken@1667
   315
            return NULL;
slouken@1667
   316
        }
slouken@1667
   317
    }
slouken@1667
   318
    mode.format = desired_format;
slouken@1667
   319
    mode.w = width;
slouken@1667
   320
    mode.h = height;
slouken@1667
   321
    mode.refresh_rate = 0;
slouken@1667
   322
slouken@1667
   323
    /* Set the desired display mode */
slouken@1667
   324
    if (flags & SDL_FULLSCREEN) {
slouken@1668
   325
        if (!SDL_GetClosestDisplayMode(&mode, &mode)) {
slouken@1667
   326
            return NULL;
slouken@1667
   327
        }
slouken@1667
   328
    } else {
slouken@1669
   329
        if (desktop_format) {
slouken@1669
   330
            mode.format = desktop_format;
slouken@1669
   331
        }
slouken@1669
   332
        if (desktop_mode->w && desktop_mode->h) {
slouken@1669
   333
            mode.w = desktop_mode->w;
slouken@1669
   334
            mode.h = desktop_mode->h;
slouken@1669
   335
        }
slouken@1669
   336
        mode.refresh_rate = desktop_mode->refresh_rate;
slouken@1667
   337
    }
slouken@1668
   338
    if (SDL_SetDisplayMode(&mode) < 0) {
slouken@1667
   339
        return NULL;
slouken@1667
   340
    }
slouken@1667
   341
slouken@1682
   342
    /* If we're in OpenGL mode, just create a stub surface and we're done! */
slouken@1682
   343
    if (flags & SDL_OPENGL) {
slouken@1682
   344
        SDL_VideoSurface =
slouken@1682
   345
            SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
slouken@1682
   346
        if (!SDL_VideoSurface) {
slouken@1682
   347
            return NULL;
slouken@1682
   348
        }
slouken@1682
   349
        SDL_VideoSurface->flags |= surface_flags;
slouken@1682
   350
        SDL_PublicSurface = SDL_VideoSurface;
slouken@1682
   351
        return SDL_PublicSurface;
slouken@1682
   352
    }
slouken@1682
   353
slouken@1678
   354
    /* Create a renderer for the window */
slouken@1678
   355
    if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) {
slouken@1670
   356
        return NULL;
slouken@1670
   357
    }
slouken@1670
   358
slouken@1678
   359
    /* Create a texture for the screen surface */
slouken@1678
   360
    SDL_VideoTexture =
slouken@1678
   361
        SDL_CreateTexture(desired_format, SDL_TextureAccess_Local, width,
slouken@1678
   362
                          height);
slouken@1678
   363
    if (!SDL_VideoTexture) {
slouken@1678
   364
        SDL_VideoTexture =
slouken@1678
   365
            SDL_CreateTexture(0, SDL_TextureAccess_Local, width, height);
slouken@1678
   366
    }
slouken@1678
   367
    if (!SDL_VideoTexture) {
slouken@1670
   368
        return NULL;
slouken@1670
   369
    }
slouken@1670
   370
slouken@1678
   371
    /* Create the screen surface */
slouken@1678
   372
    SDL_VideoSurface = SDL_CreateRGBSurfaceFromTexture(SDL_VideoTexture);
slouken@1678
   373
    if (!SDL_VideoSurface) {
slouken@1678
   374
        return NULL;
slouken@1678
   375
    }
slouken@1682
   376
    SDL_VideoSurface->flags |= surface_flags;
slouken@1670
   377
slouken@1678
   378
    /* Set a default screen palette */
slouken@1678
   379
    if (SDL_VideoSurface->format->palette) {
slouken@1678
   380
        SDL_VideoSurface->flags |= SDL_HWPALETTE;
slouken@1678
   381
        SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
slouken@1678
   382
                         SDL_VideoSurface->format->BitsPerPixel);
slouken@1678
   383
        SDL_SetTexturePalette(SDL_VideoTexture,
slouken@1678
   384
                              SDL_VideoSurface->format->palette->colors, 0,
slouken@1678
   385
                              SDL_VideoSurface->format->palette->ncolors);
slouken@1682
   386
        SDL_SetDisplayPalette(SDL_VideoSurface->format->palette->colors, 0,
slouken@1682
   387
                              SDL_VideoSurface->format->palette->ncolors);
slouken@1670
   388
    }
slouken@1670
   389
slouken@1670
   390
    /* Create a shadow surface if necessary */
slouken@1678
   391
    if (((bpp != SDL_VideoSurface->format->BitsPerPixel)
slouken@1678
   392
         && !(flags & SDL_ANYFORMAT))
slouken@1678
   393
        || ((SDL_VideoSurface->flags & SDL_HWSURFACE)
slouken@1678
   394
            && !(flags & SDL_HWSURFACE))) {
slouken@1678
   395
        if ((bpp == SDL_VideoSurface->format->BitsPerPixel)
slouken@1678
   396
            || (flags & SDL_ANYFORMAT)) {
slouken@1678
   397
            SDL_ShadowSurface =
slouken@1678
   398
                SDL_CreateRGBSurface(0, width, height,
slouken@1678
   399
                                     SDL_VideoSurface->format->BitsPerPixel,
slouken@1678
   400
                                     SDL_VideoSurface->format->Rmask,
slouken@1678
   401
                                     SDL_VideoSurface->format->Gmask,
slouken@1678
   402
                                     SDL_VideoSurface->format->Bmask,
slouken@1678
   403
                                     SDL_VideoSurface->format->Amask);
slouken@1678
   404
        } else {
slouken@1678
   405
            SDL_ShadowSurface =
slouken@1678
   406
                SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
slouken@1678
   407
        }
slouken@1678
   408
        if (!SDL_ShadowSurface) {
slouken@1670
   409
            return NULL;
slouken@1670
   410
        }
slouken@1682
   411
        surface_flags &= ~SDL_SCREEN_SURFACE;
slouken@1682
   412
        surface_flags |= SDL_SHADOW_SURFACE;
slouken@1682
   413
        SDL_ShadowSurface->flags |= surface_flags;
slouken@1670
   414
slouken@1678
   415
        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
slouken@1678
   416
        if (SDL_ShadowSurface->format->palette) {
slouken@1678
   417
            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
slouken@1678
   418
            SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
slouken@1678
   419
                             SDL_ShadowSurface->format->BitsPerPixel);
slouken@1670
   420
        }
slouken@1670
   421
    }
slouken@1678
   422
    SDL_PublicSurface =
slouken@1678
   423
        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
slouken@1670
   424
slouken@1670
   425
    /* Clear the surface for display */
slouken@1678
   426
    SDL_FillRect(SDL_PublicSurface, NULL, 0);
slouken@1670
   427
slouken@1678
   428
    /* We're finally done! */
slouken@1678
   429
    return SDL_PublicSurface;
slouken@1670
   430
}
slouken@1670
   431
slouken@1670
   432
SDL_Surface *
slouken@1668
   433
SDL_GetVideoSurface(void)
slouken@1667
   434
{
slouken@1678
   435
    return SDL_PublicSurface;
slouken@1667
   436
}
slouken@1667
   437
slouken@1670
   438
SDL_Surface *
slouken@1670
   439
SDL_DisplayFormat(SDL_Surface * surface)
slouken@1670
   440
{
slouken@1670
   441
    Uint32 flags;
slouken@1670
   442
slouken@1670
   443
    if (!SDL_PublicSurface) {
slouken@1670
   444
        SDL_SetError("No video mode has been set");
slouken@1678
   445
        return NULL;
slouken@1670
   446
    }
slouken@1678
   447
slouken@1670
   448
    /* Set the flags appropriate for copying to display surface */
slouken@1678
   449
    flags = SDL_SWSURFACE;
slouken@1670
   450
#ifdef AUTORLE_DISPLAYFORMAT
slouken@1670
   451
    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
slouken@1670
   452
    flags |= SDL_RLEACCELOK;
slouken@1670
   453
#else
slouken@1670
   454
    flags |=
slouken@1670
   455
        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
   456
#endif
slouken@1678
   457
    return SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags);
slouken@1670
   458
}
slouken@1670
   459
slouken@1670
   460
SDL_Surface *
slouken@1670
   461
SDL_DisplayFormatAlpha(SDL_Surface * surface)
slouken@1670
   462
{
slouken@1670
   463
    SDL_PixelFormat *vf;
slouken@1670
   464
    SDL_PixelFormat *format;
slouken@1670
   465
    SDL_Surface *converted;
slouken@1670
   466
    Uint32 flags;
slouken@1670
   467
    /* default to ARGB8888 */
slouken@1670
   468
    Uint32 amask = 0xff000000;
slouken@1670
   469
    Uint32 rmask = 0x00ff0000;
slouken@1670
   470
    Uint32 gmask = 0x0000ff00;
slouken@1670
   471
    Uint32 bmask = 0x000000ff;
slouken@1670
   472
slouken@1670
   473
    if (!SDL_PublicSurface) {
slouken@1670
   474
        SDL_SetError("No video mode has been set");
slouken@1678
   475
        return NULL;
slouken@1670
   476
    }
slouken@1670
   477
    vf = SDL_PublicSurface->format;
slouken@1670
   478
slouken@1670
   479
    switch (vf->BytesPerPixel) {
slouken@1670
   480
    case 2:
slouken@1670
   481
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1670
   482
           For anything else (like ARGB4444) it doesn't matter
slouken@1670
   483
           since we have no special code for it anyway */
slouken@1670
   484
        if ((vf->Rmask == 0x1f) &&
slouken@1670
   485
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1670
   486
            rmask = 0xff;
slouken@1670
   487
            bmask = 0xff0000;
slouken@1670
   488
        }
slouken@1670
   489
        break;
slouken@1670
   490
slouken@1670
   491
    case 3:
slouken@1670
   492
    case 4:
slouken@1670
   493
        /* Keep the video format, as long as the high 8 bits are
slouken@1670
   494
           unused or alpha */
slouken@1670
   495
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1670
   496
            rmask = 0xff;
slouken@1670
   497
            bmask = 0xff0000;
slouken@1670
   498
        }
slouken@1670
   499
        break;
slouken@1670
   500
slouken@1670
   501
    default:
slouken@1670
   502
        /* We have no other optimised formats right now. When/if a new
slouken@1670
   503
           optimised alpha format is written, add the converter here */
slouken@1670
   504
        break;
slouken@1670
   505
    }
slouken@1670
   506
    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
slouken@1670
   507
    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
slouken@1670
   508
    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1670
   509
    converted = SDL_ConvertSurface(surface, format, flags);
slouken@1670
   510
    SDL_FreeFormat(format);
slouken@1678
   511
    return converted;
slouken@1678
   512
}
slouken@1678
   513
slouken@1678
   514
int
slouken@1678
   515
SDL_Flip(SDL_Surface * screen)
slouken@1678
   516
{
slouken@1678
   517
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1678
   518
    return 0;
slouken@1670
   519
}
slouken@1670
   520
slouken@1670
   521
void
slouken@1670
   522
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@1670
   523
{
slouken@1670
   524
    if (screen) {
slouken@1670
   525
        SDL_Rect rect;
slouken@1670
   526
slouken@1670
   527
        /* Perform some checking */
slouken@1670
   528
        if (w == 0)
slouken@1670
   529
            w = screen->w;
slouken@1670
   530
        if (h == 0)
slouken@1670
   531
            h = screen->h;
slouken@1670
   532
        if ((int) (x + w) > screen->w)
slouken@1670
   533
            return;
slouken@1670
   534
        if ((int) (y + h) > screen->h)
slouken@1670
   535
            return;
slouken@1670
   536
slouken@1670
   537
        /* Fill the rectangle */
slouken@1670
   538
        rect.x = (Sint16) x;
slouken@1670
   539
        rect.y = (Sint16) y;
slouken@1670
   540
        rect.w = (Uint16) w;
slouken@1670
   541
        rect.h = (Uint16) h;
slouken@1670
   542
        SDL_UpdateRects(screen, 1, &rect);
slouken@1670
   543
    }
slouken@1670
   544
}
slouken@1670
   545
void
slouken@1670
   546
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@1670
   547
{
slouken@1670
   548
    int i;
slouken@1670
   549
slouken@1678
   550
    if (screen == SDL_ShadowSurface) {
slouken@1678
   551
        for (i = 0; i < numrects; ++i) {
slouken@1678
   552
            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
slouken@1678
   553
                          &rects[i]);
slouken@1670
   554
        }
slouken@1670
   555
slouken@1670
   556
        /* Fall through to video surface update */
slouken@1678
   557
        screen = SDL_VideoSurface;
slouken@1670
   558
    }
slouken@1678
   559
    if (screen == SDL_VideoSurface) {
slouken@1678
   560
        for (i = 0; i < numrects; ++i) {
slouken@1678
   561
            SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i],
slouken@1678
   562
                           SDL_TextureBlendMode_None,
slouken@1678
   563
                           SDL_TextureScaleMode_None);
slouken@1670
   564
        }
slouken@1678
   565
        SDL_RenderPresent();
slouken@1670
   566
    }
slouken@1670
   567
}
slouken@1670
   568
slouken@1667
   569
void
slouken@1668
   570
SDL_WM_SetCaption(const char *title, const char *icon)
slouken@1667
   571
{
slouken@1667
   572
    if (wm_title) {
slouken@1668
   573
        SDL_free(wm_title);
slouken@1667
   574
    } else {
slouken@1668
   575
        wm_title = SDL_strdup(title);
slouken@1667
   576
    }
slouken@1678
   577
    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
slouken@1667
   578
}
slouken@1667
   579
slouken@1667
   580
void
slouken@1668
   581
SDL_WM_GetCaption(char **title, char **icon)
slouken@1667
   582
{
slouken@1667
   583
    if (title) {
slouken@1667
   584
        *title = wm_title;
slouken@1667
   585
    }
slouken@1667
   586
    if (icon) {
slouken@1667
   587
        *icon = "";
slouken@1667
   588
    }
slouken@1667
   589
}
slouken@1667
   590
slouken@1667
   591
void
slouken@1668
   592
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@1667
   593
{
slouken@1667
   594
    /* FIXME */
slouken@1667
   595
}
slouken@1667
   596
slouken@1667
   597
int
slouken@1668
   598
SDL_WM_IconifyWindow(void)
slouken@1667
   599
{
slouken@1678
   600
    SDL_MinimizeWindow(SDL_VideoWindow);
slouken@1667
   601
}
slouken@1667
   602
slouken@1667
   603
int
slouken@1668
   604
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
slouken@1667
   605
{
slouken@1667
   606
    return 0;
slouken@1667
   607
}
slouken@1667
   608
slouken@1667
   609
SDL_GrabMode
slouken@1668
   610
SDL_WM_GrabInput(SDL_GrabMode mode)
slouken@1667
   611
{
slouken@1667
   612
    if (mode != SDL_GRAB_QUERY) {
slouken@1678
   613
        SDL_SetWindowGrab(SDL_VideoWindow, mode);
slouken@1667
   614
    }
slouken@1678
   615
    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
slouken@1667
   616
}
slouken@1667
   617
slouken@1680
   618
void
slouken@1680
   619
SDL_WarpMouse(Uint16 x, Uint16 y)
slouken@1680
   620
{
slouken@1680
   621
    SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
slouken@1680
   622
}
slouken@1680
   623
slouken@1667
   624
Uint8
slouken@1668
   625
SDL_GetAppState(void)
slouken@1667
   626
{
slouken@1667
   627
    Uint8 state = 0;
slouken@1667
   628
    Uint32 flags = 0;
slouken@1667
   629
slouken@1678
   630
    flags = SDL_GetWindowFlags(SDL_VideoWindow);
slouken@1667
   631
    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
slouken@1667
   632
        state |= SDL_APPACTIVE;
slouken@1667
   633
    }
slouken@1667
   634
    if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
slouken@1667
   635
        state |= SDL_APPINPUTFOCUS;
slouken@1667
   636
    }
slouken@1667
   637
    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
slouken@1667
   638
        state |= SDL_APPMOUSEFOCUS;
slouken@1667
   639
    }
slouken@1667
   640
    return state;
slouken@1667
   641
}
slouken@1667
   642
slouken@1667
   643
const SDL_version *
slouken@1668
   644
SDL_Linked_Version(void)
slouken@1667
   645
{
slouken@1667
   646
    static SDL_version version;
slouken@1668
   647
    SDL_VERSION(&version);
slouken@1667
   648
    return &version;
slouken@1667
   649
}
slouken@1667
   650
slouken@1667
   651
int
slouken@1682
   652
SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
slouken@1668
   653
               int firstcolor, int ncolors)
slouken@1667
   654
{
slouken@1668
   655
    SDL_SetColors(surface, colors, firstcolor, ncolors);
slouken@1667
   656
}
slouken@1667
   657
slouken@1667
   658
int
slouken@1682
   659
SDL_SetScreenColors(SDL_Surface * screen, const SDL_Color * colors,
slouken@1682
   660
                    int firstcolor, int ncolors)
slouken@1670
   661
{
slouken@1670
   662
    SDL_Palette *pal;
slouken@1670
   663
    int gotall;
slouken@1670
   664
    int palsize;
slouken@1670
   665
slouken@1670
   666
    /* Verify the parameters */
slouken@1670
   667
    pal = screen->format->palette;
slouken@1670
   668
    if (!pal) {
slouken@1670
   669
        return 0;               /* not a palettized surface */
slouken@1670
   670
    }
slouken@1670
   671
    gotall = 1;
slouken@1670
   672
    palsize = 1 << screen->format->BitsPerPixel;
slouken@1670
   673
    if (ncolors > (palsize - firstcolor)) {
slouken@1670
   674
        ncolors = (palsize - firstcolor);
slouken@1670
   675
        gotall = 0;
slouken@1670
   676
    }
slouken@1670
   677
slouken@1678
   678
    if (screen == SDL_ShadowSurface) {
slouken@1670
   679
        SDL_Palette *vidpal;
slouken@1670
   680
slouken@1678
   681
        vidpal = SDL_VideoSurface->format->palette;
slouken@1670
   682
        if (vidpal && vidpal->ncolors == pal->ncolors) {
slouken@1670
   683
            /* This is a shadow surface, and the physical
slouken@1670
   684
             * framebuffer is also indexed. Propagate the
slouken@1670
   685
             * changes to its logical palette so that
slouken@1670
   686
             * updates are always identity blits
slouken@1670
   687
             */
slouken@1670
   688
            SDL_memcpy(vidpal->colors + firstcolor, colors,
slouken@1670
   689
                       ncolors * sizeof(*colors));
slouken@1670
   690
        }
slouken@1678
   691
        if (SDL_VideoSurface->flags & SDL_HWPALETTE) {
slouken@1670
   692
            /* Set the physical palette */
slouken@1678
   693
            screen = SDL_VideoSurface;
slouken@1670
   694
        } else {
slouken@1670
   695
            SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1670
   696
        }
slouken@1670
   697
    }
slouken@1670
   698
slouken@1678
   699
    if (screen == SDL_VideoSurface) {
slouken@1678
   700
        SDL_SetTexturePalette(SDL_VideoTexture,
slouken@1678
   701
                              SDL_VideoSurface->format->palette->colors, 0,
slouken@1678
   702
                              SDL_VideoSurface->format->palette->ncolors);
slouken@1681
   703
        SDL_SetDisplayPalette(SDL_VideoSurface->format->palette->colors, 0,
slouken@1681
   704
                              SDL_VideoSurface->format->palette->ncolors);
slouken@1670
   705
    }
slouken@1670
   706
slouken@1670
   707
    return gotall;
slouken@1670
   708
}
slouken@1670
   709
slouken@1670
   710
int
slouken@1668
   711
SDL_GetWMInfo(SDL_SysWMinfo * info)
slouken@1667
   712
{
slouken@1678
   713
    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
slouken@1667
   714
}
slouken@1667
   715
slouken@1678
   716
#if 0
slouken@1670
   717
void
slouken@1670
   718
SDL_MoveCursor(int x, int y)
slouken@1670
   719
{
slouken@1670
   720
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   721
slouken@1670
   722
    /* Erase and update the current mouse position */
slouken@1670
   723
    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1670
   724
        /* Erase and redraw mouse cursor in new position */
slouken@1670
   725
        SDL_LockCursor();
slouken@1670
   726
        SDL_EraseCursor(SDL_VideoSurface);
slouken@1670
   727
        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
slouken@1670
   728
        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
slouken@1670
   729
        SDL_DrawCursor(SDL_VideoSurface);
slouken@1670
   730
        SDL_UnlockCursor();
slouken@1670
   731
    } else if (_this->MoveWMCursor) {
slouken@1670
   732
        _this->MoveWMCursor(_this, x, y);
slouken@1670
   733
    }
slouken@1670
   734
}
slouken@1670
   735
slouken@1670
   736
/* Keep track of the current cursor colors */
slouken@1670
   737
static int palette_changed = 1;
slouken@1670
   738
static Uint8 pixels8[2];
slouken@1670
   739
slouken@1670
   740
void
slouken@1670
   741
SDL_CursorPaletteChanged(void)
slouken@1670
   742
{
slouken@1670
   743
    palette_changed = 1;
slouken@1670
   744
}
slouken@1670
   745
slouken@1670
   746
void
slouken@1670
   747
SDL_MouseRect(SDL_Rect * area)
slouken@1670
   748
{
slouken@1670
   749
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   750
    int clip_diff;
slouken@1670
   751
slouken@1670
   752
    *area = SDL_cursor->area;
slouken@1670
   753
    if (area->x < 0) {
slouken@1670
   754
        area->w += area->x;
slouken@1670
   755
        area->x = 0;
slouken@1670
   756
    }
slouken@1670
   757
    if (area->y < 0) {
slouken@1670
   758
        area->h += area->y;
slouken@1670
   759
        area->y = 0;
slouken@1670
   760
    }
slouken@1670
   761
    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
slouken@1670
   762
    if (clip_diff > 0) {
slouken@1670
   763
        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
slouken@1670
   764
    }
slouken@1670
   765
    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
slouken@1670
   766
    if (clip_diff > 0) {
slouken@1670
   767
        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
slouken@1670
   768
    }
slouken@1670
   769
}
slouken@1670
   770
slouken@1670
   771
static void
slouken@1670
   772
SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
slouken@1670
   773
{
slouken@1670
   774
    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
slouken@1670
   775
    int i, w, h;
slouken@1670
   776
    Uint8 *data, datab;
slouken@1670
   777
    Uint8 *mask, maskb;
slouken@1670
   778
slouken@1670
   779
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1670
   780
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1670
   781
    switch (screen->format->BytesPerPixel) {
slouken@1670
   782
slouken@1670
   783
    case 1:
slouken@1670
   784
        {
slouken@1670
   785
            Uint8 *dst;
slouken@1670
   786
            int dstskip;
slouken@1670
   787
slouken@1670
   788
            if (palette_changed) {
slouken@1670
   789
                pixels8[0] =
slouken@1670
   790
                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1670
   791
                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1670
   792
                palette_changed = 0;
slouken@1670
   793
            }
slouken@1670
   794
            dst = (Uint8 *) screen->pixels +
slouken@1670
   795
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   796
                SDL_cursor->area.x;
slouken@1670
   797
            dstskip = screen->pitch - area->w;
slouken@1670
   798
slouken@1670
   799
            for (h = area->h; h; h--) {
slouken@1670
   800
                for (w = area->w / 8; w; w--) {
slouken@1670
   801
                    maskb = *mask++;
slouken@1670
   802
                    datab = *data++;
slouken@1670
   803
                    for (i = 0; i < 8; ++i) {
slouken@1670
   804
                        if (maskb & 0x80) {
slouken@1670
   805
                            *dst = pixels8[datab >> 7];
slouken@1670
   806
                        }
slouken@1670
   807
                        maskb <<= 1;
slouken@1670
   808
                        datab <<= 1;
slouken@1670
   809
                        dst++;
slouken@1670
   810
                    }
slouken@1670
   811
                }
slouken@1670
   812
                dst += dstskip;
slouken@1670
   813
            }
slouken@1670
   814
        }
slouken@1670
   815
        break;
slouken@1670
   816
slouken@1670
   817
    case 2:
slouken@1670
   818
        {
slouken@1670
   819
            Uint16 *dst;
slouken@1670
   820
            int dstskip;
slouken@1670
   821
slouken@1670
   822
            dst = (Uint16 *) screen->pixels +
slouken@1670
   823
                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
slouken@1670
   824
                SDL_cursor->area.x;
slouken@1670
   825
            dstskip = (screen->pitch / 2) - area->w;
slouken@1670
   826
slouken@1670
   827
            for (h = area->h; h; h--) {
slouken@1670
   828
                for (w = area->w / 8; w; w--) {
slouken@1670
   829
                    maskb = *mask++;
slouken@1670
   830
                    datab = *data++;
slouken@1670
   831
                    for (i = 0; i < 8; ++i) {
slouken@1670
   832
                        if (maskb & 0x80) {
slouken@1670
   833
                            *dst = (Uint16) pixels[datab >> 7];
slouken@1670
   834
                        }
slouken@1670
   835
                        maskb <<= 1;
slouken@1670
   836
                        datab <<= 1;
slouken@1670
   837
                        dst++;
slouken@1670
   838
                    }
slouken@1670
   839
                }
slouken@1670
   840
                dst += dstskip;
slouken@1670
   841
            }
slouken@1670
   842
        }
slouken@1670
   843
        break;
slouken@1670
   844
slouken@1670
   845
    case 3:
slouken@1670
   846
        {
slouken@1670
   847
            Uint8 *dst;
slouken@1670
   848
            int dstskip;
slouken@1670
   849
slouken@1670
   850
            dst = (Uint8 *) screen->pixels +
slouken@1670
   851
                (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   852
                SDL_cursor->area.x * 3;
slouken@1670
   853
            dstskip = screen->pitch - area->w * 3;
slouken@1670
   854
slouken@1670
   855
            for (h = area->h; h; h--) {
slouken@1670
   856
                for (w = area->w / 8; w; w--) {
slouken@1670
   857
                    maskb = *mask++;
slouken@1670
   858
                    datab = *data++;
slouken@1670
   859
                    for (i = 0; i < 8; ++i) {
slouken@1670
   860
                        if (maskb & 0x80) {
slouken@1670
   861
                            SDL_memset(dst, pixels[datab >> 7], 3);
slouken@1670
   862
                        }
slouken@1670
   863
                        maskb <<= 1;
slouken@1670
   864
                        datab <<= 1;
slouken@1670
   865
                        dst += 3;
slouken@1670
   866
                    }
slouken@1670
   867
                }
slouken@1670
   868
                dst += dstskip;
slouken@1670
   869
            }
slouken@1670
   870
        }
slouken@1670
   871
        break;
slouken@1670
   872
slouken@1670
   873
    case 4:
slouken@1670
   874
        {
slouken@1670
   875
            Uint32 *dst;
slouken@1670
   876
            int dstskip;
slouken@1670
   877
slouken@1670
   878
            dst = (Uint32 *) screen->pixels +
slouken@1670
   879
                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
slouken@1670
   880
                SDL_cursor->area.x;
slouken@1670
   881
            dstskip = (screen->pitch / 4) - area->w;
slouken@1670
   882
slouken@1670
   883
            for (h = area->h; h; h--) {
slouken@1670
   884
                for (w = area->w / 8; w; w--) {
slouken@1670
   885
                    maskb = *mask++;
slouken@1670
   886
                    datab = *data++;
slouken@1670
   887
                    for (i = 0; i < 8; ++i) {
slouken@1670
   888
                        if (maskb & 0x80) {
slouken@1670
   889
                            *dst = pixels[datab >> 7];
slouken@1670
   890
                        }
slouken@1670
   891
                        maskb <<= 1;
slouken@1670
   892
                        datab <<= 1;
slouken@1670
   893
                        dst++;
slouken@1670
   894
                    }
slouken@1670
   895
                }
slouken@1670
   896
                dst += dstskip;
slouken@1670
   897
            }
slouken@1670
   898
        }
slouken@1670
   899
        break;
slouken@1670
   900
    }
slouken@1670
   901
}
slouken@1670
   902
slouken@1670
   903
static void
slouken@1670
   904
SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
slouken@1670
   905
{
slouken@1670
   906
    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
slouken@1670
   907
    int h;
slouken@1670
   908
    int x, minx, maxx;
slouken@1670
   909
    Uint8 *data, datab = 0;
slouken@1670
   910
    Uint8 *mask, maskb = 0;
slouken@1670
   911
    Uint8 *dst;
slouken@1670
   912
    int dstbpp, dstskip;
slouken@1670
   913
slouken@1670
   914
    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
slouken@1670
   915
    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
slouken@1670
   916
    dstbpp = screen->format->BytesPerPixel;
slouken@1670
   917
    dst = (Uint8 *) screen->pixels +
slouken@1670
   918
        (SDL_cursor->area.y + area->y) * screen->pitch +
slouken@1670
   919
        SDL_cursor->area.x * dstbpp;
slouken@1670
   920
    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
slouken@1670
   921
slouken@1670
   922
    minx = area->x;
slouken@1670
   923
    maxx = area->x + area->w;
slouken@1670
   924
    if (screen->format->BytesPerPixel == 1) {
slouken@1670
   925
        if (palette_changed) {
slouken@1670
   926
            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
slouken@1670
   927
            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1670
   928
            palette_changed = 0;
slouken@1670
   929
        }
slouken@1670
   930
        for (h = area->h; h; h--) {
slouken@1670
   931
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1670
   932
                if ((x % 8) == 0) {
slouken@1670
   933
                    maskb = *mask++;
slouken@1670
   934
                    datab = *data++;
slouken@1670
   935
                }
slouken@1670
   936
                if ((x >= minx) && (x < maxx)) {
slouken@1670
   937
                    if (maskb & 0x80) {
slouken@1670
   938
                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
slouken@1670
   939
                    }
slouken@1670
   940
                }
slouken@1670
   941
                maskb <<= 1;
slouken@1670
   942
                datab <<= 1;
slouken@1670
   943
                dst += dstbpp;
slouken@1670
   944
            }
slouken@1670
   945
            dst += dstskip;
slouken@1670
   946
        }
slouken@1670
   947
    } else {
slouken@1670
   948
        for (h = area->h; h; h--) {
slouken@1670
   949
            for (x = 0; x < SDL_cursor->area.w; ++x) {
slouken@1670
   950
                if ((x % 8) == 0) {
slouken@1670
   951
                    maskb = *mask++;
slouken@1670
   952
                    datab = *data++;
slouken@1670
   953
                }
slouken@1670
   954
                if ((x >= minx) && (x < maxx)) {
slouken@1670
   955
                    if (maskb & 0x80) {
slouken@1670
   956
                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
slouken@1670
   957
                    }
slouken@1670
   958
                }
slouken@1670
   959
                maskb <<= 1;
slouken@1670
   960
                datab <<= 1;
slouken@1670
   961
                dst += dstbpp;
slouken@1670
   962
            }
slouken@1670
   963
            dst += dstskip;
slouken@1670
   964
        }
slouken@1670
   965
    }
slouken@1670
   966
}
slouken@1670
   967
slouken@1670
   968
/* This handles the ugly work of converting the saved cursor background from
slouken@1670
   969
   the pixel format of the shadow surface to that of the video surface.
slouken@1670
   970
   This is only necessary when blitting from a shadow surface of a different
slouken@1670
   971
   pixel format than the video surface, and using a software rendered cursor.
slouken@1670
   972
*/
slouken@1670
   973
static void
slouken@1670
   974
SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
slouken@1670
   975
{
slouken@1670
   976
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
   977
    SDL_BlitInfo info;
slouken@1670
   978
    SDL_loblit RunBlit;
slouken@1670
   979
slouken@1670
   980
    /* Make sure we can steal the blit mapping */
slouken@1670
   981
    if (screen->map->dst != SDL_VideoSurface) {
slouken@1670
   982
        return;
slouken@1670
   983
    }
slouken@1670
   984
slouken@1670
   985
    /* Set up the blit information */
slouken@1670
   986
    info.s_pixels = SDL_cursor->save[1];
slouken@1670
   987
    info.s_width = w;
slouken@1670
   988
    info.s_height = h;
slouken@1670
   989
    info.s_skip = 0;
slouken@1670
   990
    info.d_pixels = SDL_cursor->save[0];
slouken@1670
   991
    info.d_width = w;
slouken@1670
   992
    info.d_height = h;
slouken@1670
   993
    info.d_skip = 0;
slouken@1670
   994
    info.aux_data = screen->map->sw_data->aux_data;
slouken@1670
   995
    info.src = screen->format;
slouken@1670
   996
    info.table = screen->map->table;
slouken@1670
   997
    info.dst = SDL_VideoSurface->format;
slouken@1670
   998
    RunBlit = screen->map->sw_data->blit;
slouken@1670
   999
slouken@1670
  1000
    /* Run the actual software blit */
slouken@1670
  1001
    RunBlit(&info);
slouken@1670
  1002
}
slouken@1670
  1003
slouken@1670
  1004
void
slouken@1670
  1005
SDL_DrawCursorNoLock(SDL_Surface * screen)
slouken@1670
  1006
{
slouken@1670
  1007
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1008
    SDL_Rect area;
slouken@1670
  1009
slouken@1670
  1010
    /* Get the mouse rectangle, clipped to the screen */
slouken@1670
  1011
    SDL_MouseRect(&area);
slouken@1670
  1012
    if ((area.w == 0) || (area.h == 0)) {
slouken@1670
  1013
        return;
slouken@1670
  1014
    }
slouken@1670
  1015
slouken@1670
  1016
    /* Copy mouse background */
slouken@1670
  1017
    {
slouken@1670
  1018
        int w, h, screenbpp;
slouken@1670
  1019
        Uint8 *src, *dst;
slouken@1670
  1020
slouken@1670
  1021
        /* Set up the copy pointers */
slouken@1670
  1022
        screenbpp = screen->format->BytesPerPixel;
slouken@1670
  1023
        if ((screen == SDL_VideoSurface) ||
slouken@1670
  1024
            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
slouken@1670
  1025
            dst = SDL_cursor->save[0];
slouken@1670
  1026
        } else {
slouken@1670
  1027
            dst = SDL_cursor->save[1];
slouken@1670
  1028
        }
slouken@1670
  1029
        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1670
  1030
            area.x * screenbpp;
slouken@1670
  1031
slouken@1670
  1032
        /* Perform the copy */
slouken@1670
  1033
        w = area.w * screenbpp;
slouken@1670
  1034
        h = area.h;
slouken@1670
  1035
        while (h--) {
slouken@1670
  1036
            SDL_memcpy(dst, src, w);
slouken@1670
  1037
            dst += w;
slouken@1670
  1038
            src += screen->pitch;
slouken@1670
  1039
        }
slouken@1670
  1040
    }
slouken@1670
  1041
slouken@1670
  1042
    /* Draw the mouse cursor */
slouken@1670
  1043
    area.x -= SDL_cursor->area.x;
slouken@1670
  1044
    area.y -= SDL_cursor->area.y;
slouken@1670
  1045
    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
slouken@1670
  1046
        SDL_DrawCursorFast(screen, &area);
slouken@1670
  1047
    } else {
slouken@1670
  1048
        SDL_DrawCursorSlow(screen, &area);
slouken@1670
  1049
    }
slouken@1670
  1050
}
slouken@1670
  1051
slouken@1670
  1052
void
slouken@1670
  1053
SDL_DrawCursor(SDL_Surface * screen)
slouken@1670
  1054
{
slouken@1670
  1055
    /* Lock the screen if necessary */
slouken@1670
  1056
    if (screen == NULL) {
slouken@1670
  1057
        return;
slouken@1670
  1058
    }
slouken@1670
  1059
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1060
        if (SDL_LockSurface(screen) < 0) {
slouken@1670
  1061
            return;
slouken@1670
  1062
        }
slouken@1670
  1063
    }
slouken@1670
  1064
slouken@1670
  1065
    SDL_DrawCursorNoLock(screen);
slouken@1670
  1066
slouken@1670
  1067
    /* Unlock the screen and update if necessary */
slouken@1670
  1068
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1069
        SDL_UnlockSurface(screen);
slouken@1670
  1070
    }
slouken@1670
  1071
    if ((screen->flags & SDL_SCREEN_SURFACE) &&
slouken@1670
  1072
        !(screen->flags & SDL_HWSURFACE)) {
slouken@1670
  1073
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1074
        SDL_Window *window;
slouken@1670
  1075
        SDL_Rect area;
slouken@1670
  1076
slouken@1670
  1077
        window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1078
        if (!window) {
slouken@1670
  1079
            return;
slouken@1670
  1080
        }
slouken@1670
  1081
slouken@1670
  1082
        SDL_MouseRect(&area);
slouken@1670
  1083
slouken@1670
  1084
        if (_this->UpdateWindowSurface) {
slouken@1670
  1085
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1670
  1086
        }
slouken@1670
  1087
    }
slouken@1670
  1088
}
slouken@1670
  1089
slouken@1670
  1090
void
slouken@1670
  1091
SDL_EraseCursorNoLock(SDL_Surface * screen)
slouken@1670
  1092
{
slouken@1670
  1093
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1094
    SDL_Window *window;
slouken@1670
  1095
    SDL_Rect area;
slouken@1670
  1096
slouken@1670
  1097
    /* Get the window associated with the surface */
slouken@1670
  1098
    window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1099
    if (!window || !window->surface) {
slouken@1670
  1100
        return;
slouken@1670
  1101
    }
slouken@1670
  1102
slouken@1670
  1103
    /* Get the mouse rectangle, clipped to the screen */
slouken@1670
  1104
    SDL_MouseRect(&area);
slouken@1670
  1105
    if ((area.w == 0) || (area.h == 0)) {
slouken@1670
  1106
        return;
slouken@1670
  1107
    }
slouken@1670
  1108
slouken@1670
  1109
    /* Copy mouse background */
slouken@1670
  1110
    {
slouken@1670
  1111
        int w, h, screenbpp;
slouken@1670
  1112
        Uint8 *src, *dst;
slouken@1670
  1113
slouken@1670
  1114
        /* Set up the copy pointers */
slouken@1670
  1115
        screenbpp = screen->format->BytesPerPixel;
slouken@1670
  1116
        if ((screen->flags & SDL_SCREEN_SURFACE) ||
slouken@1670
  1117
            FORMAT_EQUAL(screen->format, window->surface->format)) {
slouken@1670
  1118
            src = SDL_cursor->save[0];
slouken@1670
  1119
        } else {
slouken@1670
  1120
            src = SDL_cursor->save[1];
slouken@1670
  1121
        }
slouken@1670
  1122
        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
slouken@1670
  1123
            area.x * screenbpp;
slouken@1670
  1124
slouken@1670
  1125
        /* Perform the copy */
slouken@1670
  1126
        w = area.w * screenbpp;
slouken@1670
  1127
        h = area.h;
slouken@1670
  1128
        while (h--) {
slouken@1670
  1129
            SDL_memcpy(dst, src, w);
slouken@1670
  1130
            src += w;
slouken@1670
  1131
            dst += screen->pitch;
slouken@1670
  1132
        }
slouken@1670
  1133
slouken@1670
  1134
        /* Perform pixel conversion on cursor background */
slouken@1670
  1135
        if (src > SDL_cursor->save[1]) {
slouken@1670
  1136
            SDL_ConvertCursorSave(screen, area.w, area.h);
slouken@1670
  1137
        }
slouken@1670
  1138
    }
slouken@1670
  1139
}
slouken@1670
  1140
slouken@1670
  1141
void
slouken@1670
  1142
SDL_EraseCursor(SDL_Surface * screen)
slouken@1670
  1143
{
slouken@1670
  1144
    /* Lock the screen if necessary */
slouken@1670
  1145
    if (screen == NULL) {
slouken@1670
  1146
        return;
slouken@1670
  1147
    }
slouken@1670
  1148
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1149
        if (SDL_LockSurface(screen) < 0) {
slouken@1670
  1150
            return;
slouken@1670
  1151
        }
slouken@1670
  1152
    }
slouken@1670
  1153
slouken@1670
  1154
    SDL_EraseCursorNoLock(screen);
slouken@1670
  1155
slouken@1670
  1156
    /* Unlock the screen and update if necessary */
slouken@1670
  1157
    if (SDL_MUSTLOCK(screen)) {
slouken@1670
  1158
        SDL_UnlockSurface(screen);
slouken@1670
  1159
    }
slouken@1670
  1160
    if ((screen->flags & SDL_SCREEN_SURFACE) &&
slouken@1670
  1161
        !(screen->flags & SDL_HWSURFACE)) {
slouken@1670
  1162
        SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1670
  1163
        SDL_Window *window;
slouken@1670
  1164
        SDL_Rect area;
slouken@1670
  1165
slouken@1670
  1166
        window = SDL_GetWindowFromSurface(screen);
slouken@1670
  1167
        if (!window) {
slouken@1670
  1168
            return;
slouken@1670
  1169
        }
slouken@1670
  1170
slouken@1670
  1171
        SDL_MouseRect(&area);
slouken@1670
  1172
slouken@1670
  1173
        if (_this->UpdateWindowSurface) {
slouken@1670
  1174
            _this->UpdateWindowSurface(_this, window, 1, &area);
slouken@1670
  1175
        }
slouken@1670
  1176
    }
slouken@1670
  1177
}
slouken@1670
  1178
slouken@1670
  1179
/* Reset the cursor on video mode change
slouken@1670
  1180
   FIXME:  Keep track of all cursors, and reset them all.
slouken@1670
  1181
 */
slouken@1670
  1182
void
slouken@1670
  1183
SDL_ResetCursor(void)
slouken@1670
  1184
{
slouken@1670
  1185
    int savelen;
slouken@1670
  1186
slouken@1670
  1187
    if (SDL_cursor) {
slouken@1670
  1188
        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
slouken@1670
  1189
        SDL_cursor->area.x = 0;
slouken@1670
  1190
        SDL_cursor->area.y = 0;
slouken@1670
  1191
        SDL_memset(SDL_cursor->save[0], 0, savelen);
slouken@1670
  1192
    }
slouken@1670
  1193
}
slouken@1670
  1194
slouken@1675
  1195
SDL_Overlay *
slouken@1675
  1196
SDL_CreateYUVOverlay(int w, int h, Uint32 format, SDL_Surface * display)
slouken@1675
  1197
{
slouken@1675
  1198
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1675
  1199
    SDL_Window *window;
slouken@1675
  1200
    const char *yuv_hwaccel;
slouken@1675
  1201
    SDL_Overlay *overlay;
slouken@1675
  1202
slouken@1675
  1203
    window = SDL_GetWindowFromSurface(display);
slouken@1675
  1204
    if (window && (window->flags & SDL_WINDOW_OPENGL)) {
slouken@1675
  1205
        SDL_SetError("YUV overlays are not supported in OpenGL mode");
slouken@1675
  1206
        return NULL;
slouken@1675
  1207
    }
slouken@1675
  1208
slouken@1675
  1209
    /* Display directly on video surface, if possible */
slouken@1675
  1210
    if (SDL_getenv("SDL_VIDEO_YUV_DIRECT")) {
slouken@1675
  1211
        if (window &&
slouken@1675
  1212
            ((window->surface->format->BytesPerPixel == 2) ||
slouken@1675
  1213
             (window->surface->format->BytesPerPixel == 4))) {
slouken@1675
  1214
            display = window->surface;
slouken@1675
  1215
        }
slouken@1675
  1216
    }
slouken@1675
  1217
    overlay = NULL;
slouken@1675
  1218
    yuv_hwaccel = SDL_getenv("SDL_VIDEO_YUV_HWACCEL");
slouken@1675
  1219
    if (((display->flags & SDL_SCREEN_SURFACE) && _this->CreateYUVOverlay) &&
slouken@1675
  1220
        (!yuv_hwaccel || (SDL_atoi(yuv_hwaccel) > 0))) {
slouken@1675
  1221
        overlay = _this->CreateYUVOverlay(_this, w, h, format, display);
slouken@1675
  1222
    }
slouken@1675
  1223
    /* If hardware YUV overlay failed ... */
slouken@1675
  1224
    if (overlay == NULL) {
slouken@1675
  1225
        overlay = SDL_CreateYUV_SW(_this, w, h, format, display);
slouken@1675
  1226
    }
slouken@1675
  1227
    return overlay;
slouken@1675
  1228
}
slouken@1675
  1229
slouken@1675
  1230
int
slouken@1675
  1231
SDL_LockYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1232
{
slouken@1675
  1233
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1675
  1234
    return overlay->hwfuncs->Lock(_this, overlay);
slouken@1675
  1235
}
slouken@1675
  1236
slouken@1675
  1237
void
slouken@1675
  1238
SDL_UnlockYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1239
{
slouken@1675
  1240
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1675
  1241
    overlay->hwfuncs->Unlock(_this, overlay);
slouken@1675
  1242
}
slouken@1675
  1243
slouken@1675
  1244
int
slouken@1675
  1245
SDL_DisplayYUVOverlay(SDL_Overlay * overlay, SDL_Rect * dstrect)
slouken@1675
  1246
{
slouken@1675
  1247
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1675
  1248
    SDL_Rect src, dst;
slouken@1675
  1249
    int srcx, srcy, srcw, srch;
slouken@1675
  1250
    int dstx, dsty, dstw, dsth;
slouken@1675
  1251
slouken@1675
  1252
    /* Clip the rectangle to the screen area */
slouken@1675
  1253
    srcx = 0;
slouken@1675
  1254
    srcy = 0;
slouken@1675
  1255
    srcw = overlay->w;
slouken@1675
  1256
    srch = overlay->h;
slouken@1675
  1257
    dstx = dstrect->x;
slouken@1675
  1258
    dsty = dstrect->y;
slouken@1675
  1259
    dstw = dstrect->w;
slouken@1675
  1260
    dsth = dstrect->h;
slouken@1675
  1261
    if (dstx < 0) {
slouken@1675
  1262
        srcw += (dstx * overlay->w) / dstrect->w;
slouken@1675
  1263
        dstw += dstx;
slouken@1675
  1264
        srcx -= (dstx * overlay->w) / dstrect->w;
slouken@1675
  1265
        dstx = 0;
slouken@1675
  1266
    }
slouken@1675
  1267
    if ((dstx + dstw) > SDL_VideoSurface->w) {
slouken@1675
  1268
        int extra = (dstx + dstw - SDL_VideoSurface->w);
slouken@1675
  1269
        srcw -= (extra * overlay->w) / dstrect->w;
slouken@1675
  1270
        dstw -= extra;
slouken@1675
  1271
    }
slouken@1675
  1272
    if (dsty < 0) {
slouken@1675
  1273
        srch += (dsty * overlay->h) / dstrect->h;
slouken@1675
  1274
        dsth += dsty;
slouken@1675
  1275
        srcy -= (dsty * overlay->h) / dstrect->h;
slouken@1675
  1276
        dsty = 0;
slouken@1675
  1277
    }
slouken@1675
  1278
    if ((dsty + dsth) > SDL_VideoSurface->h) {
slouken@1675
  1279
        int extra = (dsty + dsth - SDL_VideoSurface->h);
slouken@1675
  1280
        srch -= (extra * overlay->h) / dstrect->h;
slouken@1675
  1281
        dsth -= extra;
slouken@1675
  1282
    }
slouken@1675
  1283
    if (srcw <= 0 || srch <= 0 || srch <= 0 || dsth <= 0) {
slouken@1675
  1284
        return 0;
slouken@1675
  1285
    }
slouken@1675
  1286
    /* Ugh, I can't wait for SDL_Rect to be int values */
slouken@1675
  1287
    src.x = srcx;
slouken@1675
  1288
    src.y = srcy;
slouken@1675
  1289
    src.w = srcw;
slouken@1675
  1290
    src.h = srch;
slouken@1675
  1291
    dst.x = dstx;
slouken@1675
  1292
    dst.y = dsty;
slouken@1675
  1293
    dst.w = dstw;
slouken@1675
  1294
    dst.h = dsth;
slouken@1675
  1295
    return overlay->hwfuncs->Display(_this, overlay, &src, &dst);
slouken@1675
  1296
}
slouken@1675
  1297
slouken@1675
  1298
void
slouken@1675
  1299
SDL_FreeYUVOverlay(SDL_Overlay * overlay)
slouken@1675
  1300
{
slouken@1675
  1301
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1675
  1302
    if (overlay) {
slouken@1675
  1303
        if (overlay->hwfuncs) {
slouken@1675
  1304
            overlay->hwfuncs->FreeHW(_this, overlay);
slouken@1675
  1305
        }
slouken@1675
  1306
        SDL_free(overlay);
slouken@1675
  1307
    }
slouken@1675
  1308
}
slouken@1678
  1309
#endif
slouken@1675
  1310
slouken@1667
  1311
/* vi: set ts=4 sw=4 expandtab: */