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