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