src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Feb 2011 00:11:48 -0800
changeset 5306 cf3cd833dcd3
parent 5305 75f5ff92ad08
child 5373 02007fd16991
permissions -rw-r--r--
Fixed resetting the Direc3D renderer on mode change
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* The high-level video driver subsystem */
slouken@0
    25
slouken@2999
    26
#include "SDL.h"
slouken@2984
    27
#include "SDL_video.h"
slouken@0
    28
#include "SDL_sysvideo.h"
slouken@0
    29
#include "SDL_blit.h"
slouken@0
    30
#include "SDL_pixels_c.h"
slouken@5294
    31
#include "SDL_rect_c.h"
slouken@1361
    32
#include "../events/SDL_events_c.h"
slouken@0
    33
slouken@5209
    34
#if SDL_VIDEO_OPENGL
slouken@5209
    35
#include "SDL_opengl.h"
slouken@5209
    36
#endif /* SDL_VIDEO_OPENGL */
slouken@5209
    37
hfutrell@2745
    38
#if SDL_VIDEO_OPENGL_ES
hfutrell@2745
    39
#include "SDL_opengles.h"
slouken@2753
    40
#endif /* SDL_VIDEO_OPENGL_ES */
hfutrell@2745
    41
slouken@5209
    42
#if SDL_VIDEO_OPENGL_ES2
slouken@5209
    43
#include "SDL_opengles2.h"
slouken@5209
    44
#endif /* SDL_VIDEO_OPENGL_ES2 */
slouken@4900
    45
slouken@4900
    46
#include "SDL_syswm.h"
slouken@1926
    47
slouken@1926
    48
/* On Windows, windows.h defines CreateWindow */
slouken@1926
    49
#ifdef CreateWindow
slouken@1926
    50
#undef CreateWindow
slouken@1926
    51
#endif
slouken@1926
    52
slouken@0
    53
/* Available video drivers */
lestat@3166
    54
static VideoBootStrap *bootstrap[] = {
slouken@1931
    55
#if SDL_VIDEO_DRIVER_COCOA
slouken@2753
    56
    &COCOA_bootstrap,
icculus@1133
    57
#endif
slouken@1361
    58
#if SDL_VIDEO_DRIVER_X11
slouken@2753
    59
    &X11_bootstrap,
slouken@0
    60
#endif
slouken@1361
    61
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@2753
    62
    &DirectFB_bootstrap,
slouken@167
    63
#endif
slouken@5062
    64
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@5062
    65
    &WINDOWS_bootstrap,
slouken@1361
    66
#endif
slouken@1361
    67
#if SDL_VIDEO_DRIVER_BWINDOW
slouken@2753
    68
    &BWINDOW_bootstrap,
slouken@1361
    69
#endif
slouken@5150
    70
#if SDL_VIDEO_DRIVER_PANDORA
slouken@5150
    71
    &PND_bootstrap,
slouken@1361
    72
#endif
hfutrell@2743
    73
#if SDL_VIDEO_DRIVER_NDS
slouken@2753
    74
    &NDS_bootstrap,
slouken@2735
    75
#endif
hfutrell@2745
    76
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2753
    77
    &UIKIT_bootstrap,
hfutrell@2745
    78
#endif
slouken@5150
    79
#if SDL_VIDEO_DRIVER_ANDROID
slouken@5150
    80
    &Android_bootstrap,
slouken@5150
    81
#endif
slouken@1361
    82
#if SDL_VIDEO_DRIVER_DUMMY
slouken@2753
    83
    &DUMMY_bootstrap,
slouken@610
    84
#endif
slouken@2753
    85
    NULL
slouken@0
    86
};
slouken@173
    87
slouken@1895
    88
static SDL_VideoDevice *_this = NULL;
slouken@0
    89
slouken@3695
    90
#define CHECK_WINDOW_MAGIC(window, retval) \
slouken@3695
    91
    if (!_this) { \
slouken@3695
    92
        SDL_UninitializedVideo(); \
slouken@3695
    93
        return retval; \
slouken@3695
    94
    } \
slouken@3701
    95
    if (!window || window->magic != &_this->window_magic) { \
slouken@3695
    96
        SDL_SetError("Invalid window"); \
slouken@3695
    97
        return retval; \
slouken@3695
    98
    }
slouken@3695
    99
slouken@5244
   100
#define CHECK_DISPLAY_INDEX(displayIndex, retval) \
slouken@5244
   101
    if (!_this) { \
slouken@5244
   102
        SDL_UninitializedVideo(); \
slouken@5244
   103
        return retval; \
slouken@5244
   104
    } \
slouken@5244
   105
    if (displayIndex < 0 || displayIndex >= _this->num_displays) { \
slouken@5244
   106
        SDL_SetError("displayIndex must be in the range 0 - %d", \
slouken@5244
   107
                     _this->num_displays - 1); \
slouken@5244
   108
        return retval; \
slouken@5244
   109
    }
slouken@5244
   110
slouken@0
   111
/* Various local functions */
slouken@2876
   112
static void SDL_UpdateWindowGrab(SDL_Window * window);
slouken@0
   113
slouken@5166
   114
/* Support for framebuffer emulation using an accelerated renderer */
slouken@5166
   115
slouken@5166
   116
#define SDL_WINDOWTEXTUREDATA   "_SDL_WindowTextureData"
slouken@5166
   117
slouken@5166
   118
typedef struct {
slouken@5166
   119
    SDL_Renderer *renderer;
slouken@5166
   120
    SDL_Texture *texture;
slouken@5166
   121
    void *pixels;
slouken@5166
   122
    int pitch;
slouken@5172
   123
    int bytes_per_pixel;
slouken@5166
   124
} SDL_WindowTextureData;
slouken@5166
   125
slouken@5190
   126
static SDL_bool
slouken@5190
   127
ShouldUseTextureFramebuffer()
slouken@5190
   128
{
slouken@5190
   129
    const char *hint;
slouken@5190
   130
slouken@5190
   131
    /* If there's no native framebuffer support then there's no option */
slouken@5190
   132
    if (!_this->CreateWindowFramebuffer) {
slouken@5190
   133
        return SDL_TRUE;
slouken@5190
   134
    }
slouken@5190
   135
slouken@5192
   136
    /* If the user has specified a software renderer we can't use a
slouken@5192
   137
       texture framebuffer, or renderer creation will go recursive.
slouken@5192
   138
     */
slouken@5192
   139
    hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
slouken@5192
   140
    if (hint && SDL_strcasecmp(hint, "software") == 0) {
slouken@5192
   141
        return SDL_FALSE;
slouken@5192
   142
    }
slouken@5192
   143
slouken@5190
   144
    /* See if the user or application wants a specific behavior */
slouken@5190
   145
    hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
slouken@5190
   146
    if (hint) {
slouken@5190
   147
        if (*hint == '0') {
slouken@5190
   148
            return SDL_FALSE;
slouken@5190
   149
        } else {
slouken@5190
   150
            return SDL_TRUE;
slouken@5190
   151
        }
slouken@5190
   152
    }
slouken@5190
   153
slouken@5190
   154
    /* Each platform has different performance characteristics */
slouken@5190
   155
#if defined(__WIN32__)
slouken@5190
   156
    /* GDI BitBlt() is way faster than Direct3D dynamic textures right now.
slouken@5190
   157
     */
slouken@5190
   158
    return SDL_FALSE;
slouken@5190
   159
slouken@5190
   160
#elif defined(__MACOSX__)
slouken@5190
   161
    /* Mac OS X uses OpenGL as the native fast path */
slouken@5190
   162
    return SDL_TRUE;
slouken@5190
   163
slouken@5190
   164
#elif defined(__LINUX__)
slouken@5190
   165
    /* Properly configured OpenGL drivers are faster than MIT-SHM */
slouken@5190
   166
#if SDL_VIDEO_OPENGL
slouken@5190
   167
    /* Ugh, find a way to cache this value! */
slouken@5190
   168
    {
slouken@5190
   169
        SDL_Window *window;
slouken@5190
   170
        SDL_GLContext context;
slouken@5190
   171
        SDL_bool hasAcceleratedOpenGL = SDL_FALSE;
slouken@5190
   172
slouken@5190
   173
        window = SDL_CreateWindow("OpenGL test", -32, -32, 32, 32, SDL_WINDOW_OPENGL);
slouken@5190
   174
        if (window) {
slouken@5190
   175
            context = SDL_GL_CreateContext(window);
slouken@5190
   176
            if (context) {
slouken@5190
   177
                const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@5190
   178
                const char *vendor = NULL;
slouken@5190
   179
slouken@5190
   180
                glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@5190
   181
                if (glGetStringFunc) {
slouken@5190
   182
                    vendor = (const char *) glGetStringFunc(GL_VENDOR);
slouken@5190
   183
                }
slouken@5190
   184
                /* Add more vendors here at will... */
slouken@5190
   185
                if (vendor &&
slouken@5190
   186
                    (SDL_strstr(vendor, "ATI Technologies") ||
slouken@5190
   187
                     SDL_strstr(vendor, "NVIDIA"))) {
slouken@5190
   188
                    hasAcceleratedOpenGL = SDL_TRUE;
slouken@5190
   189
                }
slouken@5190
   190
                SDL_GL_DeleteContext(context);
slouken@5190
   191
            }
slouken@5190
   192
            SDL_DestroyWindow(window);
slouken@5190
   193
        }
slouken@5190
   194
        return hasAcceleratedOpenGL;
slouken@5190
   195
    }
slouken@5190
   196
#else
slouken@5190
   197
    return SDL_FALSE;
slouken@5190
   198
#endif
slouken@5190
   199
slouken@5190
   200
#else
slouken@5190
   201
    /* Play it safe, assume that if there is a framebuffer driver that it's
slouken@5190
   202
       optimized for the current platform.
slouken@5190
   203
    */
slouken@5190
   204
    return SDL_FALSE;
slouken@5190
   205
#endif
slouken@5190
   206
}
slouken@5190
   207
slouken@5166
   208
static int
slouken@5166
   209
SDL_CreateWindowTexture(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
slouken@5166
   210
{
slouken@5166
   211
    SDL_WindowTextureData *data;
slouken@5166
   212
    SDL_RendererInfo info;
slouken@5166
   213
    Uint32 i;
slouken@5166
   214
slouken@5166
   215
    data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
slouken@5166
   216
    if (!data) {
slouken@5291
   217
        SDL_Renderer *renderer = NULL;
slouken@5168
   218
        SDL_RendererInfo info;
slouken@5168
   219
        int i;
slouken@5191
   220
        const char *hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
slouken@5168
   221
slouken@5191
   222
        /* Check to see if there's a specific driver requested */
slouken@5191
   223
        if (hint && *hint != '0' && *hint != '1' &&
slouken@5191
   224
            SDL_strcasecmp(hint, "software") != 0) {
slouken@5191
   225
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
slouken@5191
   226
                SDL_GetRenderDriverInfo(i, &info);
slouken@5191
   227
                if (SDL_strcasecmp(info.name, hint) == 0) {
slouken@5191
   228
                    renderer = SDL_CreateRenderer(window, i, 0);
slouken@5168
   229
                    break;
slouken@5168
   230
                }
slouken@5168
   231
            }
slouken@5168
   232
        }
slouken@5191
   233
slouken@5191
   234
        if (!renderer) {
slouken@5191
   235
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
slouken@5191
   236
                SDL_GetRenderDriverInfo(i, &info);
slouken@5191
   237
                if (SDL_strcmp(info.name, "software") != 0) {
slouken@5191
   238
                    renderer = SDL_CreateRenderer(window, i, 0);
slouken@5191
   239
                    if (renderer) {
slouken@5191
   240
                        break;
slouken@5191
   241
                    }
slouken@5191
   242
                }
slouken@5191
   243
            }
slouken@5191
   244
        }
slouken@5166
   245
        if (!renderer) {
slouken@5166
   246
            return -1;
slouken@5166
   247
        }
slouken@5275
   248
slouken@5275
   249
        /* Create the data after we successfully create the renderer (bug #1116) */
slouken@5275
   250
        data = (SDL_WindowTextureData *)SDL_calloc(1, sizeof(*data));
slouken@5275
   251
        if (!data) {
slouken@5275
   252
            SDL_DestroyRenderer(renderer);
slouken@5275
   253
            SDL_OutOfMemory();
slouken@5275
   254
            return -1;
slouken@5275
   255
        }
slouken@5275
   256
        SDL_SetWindowData(window, SDL_WINDOWTEXTUREDATA, data);
slouken@5275
   257
slouken@5166
   258
        data->renderer = renderer;
slouken@5166
   259
    }
slouken@5166
   260
slouken@5166
   261
    /* Free any old texture and pixel data */
slouken@5166
   262
    if (data->texture) {
slouken@5166
   263
        SDL_DestroyTexture(data->texture);
slouken@5166
   264
        data->texture = NULL;
slouken@5166
   265
    }
slouken@5166
   266
    if (data->pixels) {
slouken@5166
   267
        SDL_free(data->pixels);
slouken@5166
   268
        data->pixels = NULL;
slouken@5166
   269
    }
slouken@5166
   270
slouken@5291
   271
    if (SDL_GetRendererInfo(data->renderer, &info) < 0) {
slouken@5166
   272
        return -1;
slouken@5166
   273
    }
slouken@5166
   274
slouken@5166
   275
    /* Find the first format without an alpha channel */
slouken@5166
   276
    *format = info.texture_formats[0];
slouken@5166
   277
    for (i = 0; i < info.num_texture_formats; ++i) {
slouken@5268
   278
        if (!SDL_ISPIXELFORMAT_FOURCC(info.texture_formats[i]) &&
slouken@5268
   279
            !SDL_ISPIXELFORMAT_ALPHA(info.texture_formats[i])) {
slouken@5166
   280
            *format = info.texture_formats[i];
slouken@5166
   281
            break;
slouken@5166
   282
        }
slouken@5166
   283
    }
slouken@5166
   284
slouken@5291
   285
    data->texture = SDL_CreateTexture(data->renderer, *format,
slouken@5166
   286
                                      SDL_TEXTUREACCESS_STREAMING,
slouken@5166
   287
                                      window->w, window->h);
slouken@5166
   288
    if (!data->texture) {
slouken@5166
   289
        return -1;
slouken@5166
   290
    }
slouken@5166
   291
slouken@5166
   292
    /* Create framebuffer data */
slouken@5172
   293
    data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
slouken@5172
   294
    data->pitch = (((window->w * data->bytes_per_pixel) + 3) & ~3);
slouken@5166
   295
    data->pixels = SDL_malloc(window->h * data->pitch);
slouken@5166
   296
    if (!data->pixels) {
slouken@5166
   297
        SDL_OutOfMemory();
slouken@5166
   298
        return -1;
slouken@5166
   299
    }
slouken@5166
   300
slouken@5166
   301
    *pixels = data->pixels;
slouken@5166
   302
    *pitch = data->pitch;
slouken@5297
   303
slouken@5297
   304
    /* Make sure we're not double-scaling the viewport */
slouken@5297
   305
    SDL_RenderSetViewport(data->renderer, NULL);
slouken@5297
   306
slouken@5166
   307
    return 0;
slouken@5166
   308
}
slouken@5166
   309
slouken@5166
   310
static int
slouken@5297
   311
SDL_UpdateWindowTexture(_THIS, SDL_Window * window, SDL_Rect * rects, int numrects)
slouken@5166
   312
{
slouken@5166
   313
    SDL_WindowTextureData *data;
slouken@5294
   314
    SDL_Rect rect;
slouken@5294
   315
    void *src;
slouken@5166
   316
slouken@5166
   317
    data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
slouken@5166
   318
    if (!data || !data->texture) {
slouken@5166
   319
        SDL_SetError("No window texture data");
slouken@5166
   320
        return -1;
slouken@5166
   321
    }
slouken@5166
   322
slouken@5294
   323
    /* Update a single rect that contains subrects for best DMA performance */
slouken@5294
   324
    if (SDL_GetSpanEnclosingRect(window->w, window->h, numrects, rects, &rect)) {
slouken@5172
   325
        src = (void *)((Uint8 *)data->pixels +
slouken@5294
   326
                        rect.y * data->pitch +
slouken@5294
   327
                        rect.x * data->bytes_per_pixel);
slouken@5294
   328
        if (SDL_UpdateTexture(data->texture, &rect, src, data->pitch) < 0) {
slouken@5172
   329
            return -1;
slouken@5172
   330
        }
slouken@5294
   331
slouken@5294
   332
        if (SDL_RenderCopy(data->renderer, data->texture, NULL, NULL) < 0) {
slouken@5294
   333
            return -1;
slouken@5172
   334
        }
slouken@5294
   335
slouken@5294
   336
        SDL_RenderPresent(data->renderer);
slouken@5172
   337
    }
slouken@5166
   338
    return 0;
slouken@5166
   339
}
slouken@5166
   340
slouken@5166
   341
static void
slouken@5166
   342
SDL_DestroyWindowTexture(_THIS, SDL_Window * window)
slouken@5166
   343
{
slouken@5166
   344
    SDL_WindowTextureData *data;
slouken@5166
   345
slouken@5166
   346
    data = SDL_SetWindowData(window, SDL_WINDOWTEXTUREDATA, NULL);
slouken@5166
   347
    if (!data) {
slouken@5166
   348
        return;
slouken@5166
   349
    }
slouken@5166
   350
    if (data->texture) {
slouken@5166
   351
        SDL_DestroyTexture(data->texture);
slouken@5166
   352
    }
slouken@5166
   353
    if (data->renderer) {
slouken@5166
   354
        SDL_DestroyRenderer(data->renderer);
slouken@5166
   355
    }
slouken@5166
   356
    if (data->pixels) {
slouken@5166
   357
        SDL_free(data->pixels);
slouken@5166
   358
    }
slouken@5166
   359
    SDL_free(data);
slouken@5166
   360
}
slouken@5166
   361
slouken@5166
   362
slouken@1895
   363
static int
slouken@1895
   364
cmpmodes(const void *A, const void *B)
slouken@1895
   365
{
slouken@2753
   366
    SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
slouken@2753
   367
    SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
slouken@0
   368
slouken@2753
   369
    if (a.w != b.w) {
slouken@2753
   370
        return b.w - a.w;
slouken@2753
   371
    }
slouken@2753
   372
    if (a.h != b.h) {
slouken@2753
   373
        return b.h - a.h;
slouken@2753
   374
    }
slouken@2753
   375
    if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
slouken@2753
   376
        return SDL_BITSPERPIXEL(b.format) - SDL_BITSPERPIXEL(a.format);
slouken@2753
   377
    }
lestat@3178
   378
    if (SDL_PIXELLAYOUT(a.format) != SDL_PIXELLAYOUT(b.format)) {
lestat@3178
   379
        return SDL_PIXELLAYOUT(b.format) - SDL_PIXELLAYOUT(a.format);
lestat@3178
   380
    }
slouken@2753
   381
    if (a.refresh_rate != b.refresh_rate) {
slouken@2753
   382
        return b.refresh_rate - a.refresh_rate;
slouken@2753
   383
    }
slouken@2753
   384
    return 0;
slouken@1895
   385
}
slouken@1895
   386
slouken@1912
   387
static void
slouken@1912
   388
SDL_UninitializedVideo()
slouken@1912
   389
{
slouken@2753
   390
    SDL_SetError("Video subsystem has not been initialized");
slouken@1912
   391
}
slouken@1912
   392
slouken@1895
   393
int
slouken@1895
   394
SDL_GetNumVideoDrivers(void)
slouken@1895
   395
{
slouken@2753
   396
    return SDL_arraysize(bootstrap) - 1;
slouken@1895
   397
}
slouken@1895
   398
slouken@2753
   399
const char *
slouken@1895
   400
SDL_GetVideoDriver(int index)
slouken@1895
   401
{
slouken@2753
   402
    if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
slouken@2753
   403
        return bootstrap[index]->name;
slouken@2753
   404
    }
slouken@2753
   405
    return NULL;
slouken@1895
   406
}
slouken@0
   407
slouken@0
   408
/*
slouken@0
   409
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   410
 */
slouken@1895
   411
int
slouken@5123
   412
SDL_VideoInit(const char *driver_name)
slouken@0
   413
{
slouken@2753
   414
    SDL_VideoDevice *video;
slouken@2753
   415
    int index;
slouken@2753
   416
    int i;
slouken@0
   417
slouken@3694
   418
    /* Check to make sure we don't overwrite '_this' */
slouken@3694
   419
    if (_this != NULL) {
slouken@3694
   420
        SDL_VideoQuit();
slouken@3694
   421
    }
slouken@3694
   422
slouken@2753
   423
    /* Start the event loop */
slouken@5123
   424
    if (SDL_StartEventLoop() < 0 ||
slouken@5123
   425
        SDL_KeyboardInit() < 0 ||
slouken@5123
   426
        SDL_MouseInit() < 0 ||
slouken@5123
   427
        SDL_TouchInit() < 0 ||
slouken@5123
   428
        SDL_QuitInit() < 0) {
slouken@2753
   429
        return -1;
slouken@2753
   430
    }
slouken@3694
   431
slouken@2753
   432
    /* Select the proper video driver */
slouken@2753
   433
    index = 0;
slouken@2753
   434
    video = NULL;
slouken@2753
   435
    if (driver_name == NULL) {
slouken@2753
   436
        driver_name = SDL_getenv("SDL_VIDEODRIVER");
slouken@2753
   437
    }
slouken@2753
   438
    if (driver_name != NULL) {
slouken@2753
   439
        for (i = 0; bootstrap[i]; ++i) {
slouken@2753
   440
            if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
slouken@3448
   441
                video = bootstrap[i]->create(index);
slouken@2753
   442
                break;
slouken@2753
   443
            }
slouken@2753
   444
        }
slouken@2753
   445
    } else {
slouken@2753
   446
        for (i = 0; bootstrap[i]; ++i) {
slouken@2753
   447
            if (bootstrap[i]->available()) {
slouken@2753
   448
                video = bootstrap[i]->create(index);
slouken@2753
   449
                if (video != NULL) {
slouken@2753
   450
                    break;
slouken@2753
   451
                }
slouken@2753
   452
            }
slouken@2753
   453
        }
slouken@2753
   454
    }
slouken@2753
   455
    if (video == NULL) {
slouken@2753
   456
        if (driver_name) {
slouken@2753
   457
            SDL_SetError("%s not available", driver_name);
slouken@2753
   458
        } else {
slouken@2753
   459
            SDL_SetError("No available video device");
slouken@2753
   460
        }
slouken@2753
   461
        return -1;
slouken@2753
   462
    }
slouken@2753
   463
    _this = video;
slouken@2753
   464
    _this->name = bootstrap[i]->name;
slouken@2753
   465
    _this->next_object_id = 1;
slouken@0
   466
slouken@0
   467
slouken@2753
   468
    /* Set some very sane GL defaults */
slouken@2753
   469
    _this->gl_config.driver_loaded = 0;
slouken@2753
   470
    _this->gl_config.dll_handle = NULL;
slouken@2753
   471
    _this->gl_config.red_size = 3;
slouken@2753
   472
    _this->gl_config.green_size = 3;
slouken@2753
   473
    _this->gl_config.blue_size = 2;
slouken@2753
   474
    _this->gl_config.alpha_size = 0;
slouken@2753
   475
    _this->gl_config.buffer_size = 0;
slouken@2753
   476
    _this->gl_config.depth_size = 16;
slouken@2753
   477
    _this->gl_config.stencil_size = 0;
slouken@2753
   478
    _this->gl_config.double_buffer = 1;
slouken@2753
   479
    _this->gl_config.accum_red_size = 0;
slouken@2753
   480
    _this->gl_config.accum_green_size = 0;
slouken@2753
   481
    _this->gl_config.accum_blue_size = 0;
slouken@2753
   482
    _this->gl_config.accum_alpha_size = 0;
slouken@2753
   483
    _this->gl_config.stereo = 0;
slouken@2753
   484
    _this->gl_config.multisamplebuffers = 0;
slouken@2753
   485
    _this->gl_config.multisamplesamples = 0;
slouken@2753
   486
    _this->gl_config.retained_backing = 1;
slouken@3571
   487
    _this->gl_config.accelerated = -1;  /* accelerated or not, both are fine */
slouken@5209
   488
#if SDL_VIDEO_OPENGL
slouken@3100
   489
    _this->gl_config.major_version = 2;
slouken@3100
   490
    _this->gl_config.minor_version = 1;
slouken@5217
   491
#elif SDL_VIDEO_OPENGL_ES
slouken@5217
   492
    _this->gl_config.major_version = 1;
slouken@5217
   493
    _this->gl_config.minor_version = 1;
slouken@5209
   494
#elif SDL_VIDEO_OPENGL_ES2
slouken@5209
   495
    _this->gl_config.major_version = 2;
slouken@5209
   496
    _this->gl_config.minor_version = 0;
slouken@5209
   497
#endif
slouken@0
   498
slouken@2753
   499
    /* Initialize the video subsystem */
slouken@2753
   500
    if (_this->VideoInit(_this) < 0) {
slouken@2753
   501
        SDL_VideoQuit();
slouken@2753
   502
        return -1;
slouken@2753
   503
    }
slouken@5166
   504
slouken@2753
   505
    /* Make sure some displays were added */
slouken@2753
   506
    if (_this->num_displays == 0) {
slouken@2753
   507
        SDL_SetError("The video driver did not add any displays");
slouken@2753
   508
        SDL_VideoQuit();
slouken@2753
   509
        return (-1);
slouken@2753
   510
    }
slouken@0
   511
slouken@5190
   512
    /* Add the renderer framebuffer emulation if desired */
slouken@5190
   513
    if (ShouldUseTextureFramebuffer()) {
slouken@5166
   514
        _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
slouken@5166
   515
        _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
slouken@5166
   516
        _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
slouken@5166
   517
    }
slouken@5166
   518
slouken@2753
   519
    /* We're ready to go! */
slouken@2753
   520
    return 0;
slouken@0
   521
}
slouken@0
   522
slouken@2753
   523
const char *
slouken@1895
   524
SDL_GetCurrentVideoDriver()
slouken@0
   525
{
slouken@2753
   526
    if (!_this) {
slouken@2753
   527
        SDL_UninitializedVideo();
slouken@2753
   528
        return NULL;
slouken@2753
   529
    }
slouken@2753
   530
    return _this->name;
slouken@0
   531
}
slouken@0
   532
slouken@1895
   533
SDL_VideoDevice *
slouken@4472
   534
SDL_GetVideoDevice(void)
slouken@0
   535
{
slouken@2753
   536
    return _this;
slouken@0
   537
}
slouken@0
   538
slouken@1895
   539
int
slouken@1895
   540
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   541
{
slouken@2753
   542
    SDL_VideoDisplay display;
slouken@0
   543
slouken@2753
   544
    SDL_zero(display);
slouken@2753
   545
    if (desktop_mode) {
slouken@2753
   546
        display.desktop_mode = *desktop_mode;
slouken@2753
   547
    }
slouken@2753
   548
    display.current_mode = display.desktop_mode;
slouken@1895
   549
slouken@2753
   550
    return SDL_AddVideoDisplay(&display);
slouken@0
   551
}
slouken@0
   552
slouken@1895
   553
int
slouken@1895
   554
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@0
   555
{
slouken@2753
   556
    SDL_VideoDisplay *displays;
slouken@2753
   557
    int index = -1;
slouken@0
   558
slouken@2753
   559
    displays =
slouken@2753
   560
        SDL_realloc(_this->displays,
slouken@2753
   561
                    (_this->num_displays + 1) * sizeof(*displays));
slouken@2753
   562
    if (displays) {
slouken@2753
   563
        index = _this->num_displays++;
slouken@2753
   564
        displays[index] = *display;
slouken@2753
   565
        displays[index].device = _this;
slouken@2753
   566
        _this->displays = displays;
slouken@2753
   567
    } else {
slouken@2753
   568
        SDL_OutOfMemory();
slouken@2753
   569
    }
slouken@2753
   570
    return index;
slouken@0
   571
}
slouken@0
   572
slouken@1895
   573
int
slouken@1895
   574
SDL_GetNumVideoDisplays(void)
slouken@0
   575
{
slouken@2753
   576
    if (!_this) {
slouken@2753
   577
        SDL_UninitializedVideo();
slouken@2753
   578
        return 0;
slouken@2753
   579
    }
slouken@2753
   580
    return _this->num_displays;
slouken@0
   581
}
slouken@0
   582
slouken@1895
   583
int
slouken@5244
   584
SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
slouken@3528
   585
{
slouken@5244
   586
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   587
slouken@3528
   588
    if (rect) {
slouken@5244
   589
        SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@3528
   590
slouken@3528
   591
        if (_this->GetDisplayBounds) {
slouken@5249
   592
            if (_this->GetDisplayBounds(_this, display, rect) == 0) {
slouken@5249
   593
                return 0;
slouken@3528
   594
            }
slouken@5249
   595
        }
slouken@5249
   596
slouken@5249
   597
        /* Assume that the displays are left to right */
slouken@5249
   598
        if (displayIndex == 0) {
slouken@5249
   599
            rect->x = 0;
slouken@5249
   600
            rect->y = 0;
slouken@3528
   601
        } else {
slouken@5249
   602
            SDL_GetDisplayBounds(displayIndex-1, rect);
slouken@5249
   603
            rect->x += rect->w;
slouken@3528
   604
        }
slouken@5249
   605
        rect->w = display->desktop_mode.w;
slouken@5249
   606
        rect->h = display->desktop_mode.h;
slouken@3528
   607
    }
slouken@3528
   608
    return 0;
slouken@3528
   609
}
slouken@3528
   610
slouken@1895
   611
SDL_bool
slouken@3500
   612
SDL_AddDisplayMode(SDL_VideoDisplay * display,  const SDL_DisplayMode * mode)
slouken@0
   613
{
slouken@2753
   614
    SDL_DisplayMode *modes;
slouken@2753
   615
    int i, nmodes;
slouken@0
   616
slouken@2753
   617
    /* Make sure we don't already have the mode in the list */
slouken@2753
   618
    modes = display->display_modes;
slouken@2753
   619
    nmodes = display->num_display_modes;
slouken@2753
   620
    for (i = nmodes; i--;) {
slouken@2753
   621
        if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
slouken@2753
   622
            return SDL_FALSE;
slouken@2753
   623
        }
slouken@2753
   624
    }
slouken@1895
   625
slouken@2753
   626
    /* Go ahead and add the new mode */
slouken@2753
   627
    if (nmodes == display->max_display_modes) {
slouken@2753
   628
        modes =
slouken@2753
   629
            SDL_realloc(modes,
slouken@2753
   630
                        (display->max_display_modes + 32) * sizeof(*modes));
slouken@2753
   631
        if (!modes) {
slouken@2753
   632
            return SDL_FALSE;
slouken@2753
   633
        }
slouken@2753
   634
        display->display_modes = modes;
slouken@2753
   635
        display->max_display_modes += 32;
slouken@2753
   636
    }
slouken@2753
   637
    modes[nmodes] = *mode;
slouken@2753
   638
    display->num_display_modes++;
slouken@1895
   639
lestat@3178
   640
    /* Re-sort video modes */
lestat@3178
   641
    SDL_qsort(display->display_modes, display->num_display_modes,
slouken@3186
   642
              sizeof(SDL_DisplayMode), cmpmodes);
lestat@3178
   643
slouken@2753
   644
    return SDL_TRUE;
slouken@0
   645
}
slouken@0
   646
slouken@5246
   647
SDL_VideoDisplay *
slouken@5246
   648
SDL_GetFirstDisplay(void)
slouken@5246
   649
{
slouken@5246
   650
    return &_this->displays[0];
slouken@5246
   651
}
slouken@5246
   652
slouken@5244
   653
static int
slouken@3500
   654
SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
slouken@3500
   655
{
slouken@3500
   656
    if (!display->num_display_modes && _this->GetDisplayModes) {
slouken@3500
   657
        _this->GetDisplayModes(_this, display);
slouken@3500
   658
        SDL_qsort(display->display_modes, display->num_display_modes,
slouken@3500
   659
                  sizeof(SDL_DisplayMode), cmpmodes);
slouken@3500
   660
    }
slouken@3500
   661
    return display->num_display_modes;
slouken@3500
   662
}
slouken@3500
   663
slouken@3500
   664
int
slouken@5244
   665
SDL_GetNumDisplayModes(int displayIndex)
slouken@0
   666
{
slouken@5244
   667
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   668
slouken@5244
   669
    return SDL_GetNumDisplayModesForDisplay(&_this->displays[displayIndex]);
slouken@3500
   670
}
slouken@3500
   671
slouken@3500
   672
int
slouken@5244
   673
SDL_GetDisplayMode(int displayIndex, int index, SDL_DisplayMode * mode)
slouken@3500
   674
{
slouken@5244
   675
    SDL_VideoDisplay *display;
slouken@5244
   676
slouken@5244
   677
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   678
slouken@5244
   679
    display = &_this->displays[displayIndex];
slouken@3500
   680
    if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
slouken@3500
   681
        SDL_SetError("index must be in the range of 0 - %d",
slouken@3500
   682
                     SDL_GetNumDisplayModesForDisplay(display) - 1);
slouken@3500
   683
        return -1;
slouken@3500
   684
    }
slouken@3500
   685
    if (mode) {
slouken@3500
   686
        *mode = display->display_modes[index];
slouken@2753
   687
    }
slouken@2753
   688
    return 0;
slouken@0
   689
}
slouken@0
   690
slouken@1967
   691
int
slouken@5244
   692
SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode * mode)
slouken@0
   693
{
slouken@5244
   694
    SDL_VideoDisplay *display;
slouken@3500
   695
slouken@5244
   696
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   697
slouken@5244
   698
    display = &_this->displays[displayIndex];
slouken@2753
   699
    if (mode) {
slouken@3500
   700
        *mode = display->desktop_mode;
slouken@2753
   701
    }
slouken@2753
   702
    return 0;
slouken@0
   703
}
slouken@0
   704
slouken@1967
   705
int
slouken@5244
   706
SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode * mode)
slouken@0
   707
{
slouken@5244
   708
    SDL_VideoDisplay *display;
slouken@3500
   709
slouken@5244
   710
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   711
slouken@5244
   712
    display = &_this->displays[displayIndex];
slouken@2753
   713
    if (mode) {
slouken@3500
   714
        *mode = display->current_mode;
slouken@2753
   715
    }
slouken@2753
   716
    return 0;
slouken@0
   717
}
slouken@0
   718
slouken@5244
   719
static SDL_DisplayMode *
slouken@3500
   720
SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
slouken@3500
   721
                                    const SDL_DisplayMode * mode,
slouken@3500
   722
                                    SDL_DisplayMode * closest)
slouken@0
   723
{
slouken@2753
   724
    Uint32 target_format;
slouken@2753
   725
    int target_refresh_rate;
slouken@2753
   726
    int i;
slouken@2753
   727
    SDL_DisplayMode *current, *match;
slouken@0
   728
slouken@3500
   729
    if (!mode || !closest) {
slouken@3500
   730
        SDL_SetError("Missing desired mode or closest mode parameter");
slouken@2753
   731
        return NULL;
slouken@2753
   732
    }
slouken@3500
   733
slouken@2753
   734
    /* Default to the desktop format */
slouken@2753
   735
    if (mode->format) {
slouken@2753
   736
        target_format = mode->format;
slouken@2753
   737
    } else {
slouken@3500
   738
        target_format = display->desktop_mode.format;
slouken@2753
   739
    }
slouken@0
   740
slouken@2753
   741
    /* Default to the desktop refresh rate */
slouken@2753
   742
    if (mode->refresh_rate) {
slouken@2753
   743
        target_refresh_rate = mode->refresh_rate;
slouken@2753
   744
    } else {
slouken@3500
   745
        target_refresh_rate = display->desktop_mode.refresh_rate;
slouken@2753
   746
    }
slouken@0
   747
slouken@2753
   748
    match = NULL;
slouken@3500
   749
    for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
slouken@3500
   750
        current = &display->display_modes[i];
slouken@0
   751
slouken@2789
   752
        if (current->w && (current->w < mode->w)) {
slouken@2753
   753
            /* Out of sorted modes large enough here */
slouken@2753
   754
            break;
slouken@2753
   755
        }
slouken@2789
   756
        if (current->h && (current->h < mode->h)) {
slouken@2789
   757
            if (current->w && (current->w == mode->w)) {
slouken@2789
   758
                /* Out of sorted modes large enough here */
slouken@2789
   759
                break;
slouken@2789
   760
            }
slouken@2789
   761
            /* Wider, but not tall enough, due to a different
slouken@2789
   762
               aspect ratio. This mode must be skipped, but closer
slouken@2789
   763
               modes may still follow. */
slouken@2789
   764
            continue;
slouken@2789
   765
        }
slouken@2753
   766
        if (!match || current->w < match->w || current->h < match->h) {
slouken@2753
   767
            match = current;
slouken@2753
   768
            continue;
slouken@2753
   769
        }
slouken@2753
   770
        if (current->format != match->format) {
slouken@2753
   771
            /* Sorted highest depth to lowest */
slouken@2753
   772
            if (current->format == target_format ||
slouken@2753
   773
                (SDL_BITSPERPIXEL(current->format) >=
slouken@2753
   774
                 SDL_BITSPERPIXEL(target_format)
slouken@2753
   775
                 && SDL_PIXELTYPE(current->format) ==
slouken@2753
   776
                 SDL_PIXELTYPE(target_format))) {
slouken@2753
   777
                match = current;
slouken@2753
   778
            }
slouken@2753
   779
            continue;
slouken@2753
   780
        }
slouken@2753
   781
        if (current->refresh_rate != match->refresh_rate) {
slouken@2753
   782
            /* Sorted highest refresh to lowest */
slouken@2753
   783
            if (current->refresh_rate >= target_refresh_rate) {
slouken@2753
   784
                match = current;
slouken@2753
   785
            }
slouken@2753
   786
        }
slouken@2753
   787
    }
slouken@2753
   788
    if (match) {
slouken@2753
   789
        if (match->format) {
slouken@2753
   790
            closest->format = match->format;
slouken@2753
   791
        } else {
slouken@2753
   792
            closest->format = mode->format;
slouken@2753
   793
        }
slouken@2753
   794
        if (match->w && match->h) {
slouken@2753
   795
            closest->w = match->w;
slouken@2753
   796
            closest->h = match->h;
slouken@2753
   797
        } else {
slouken@2753
   798
            closest->w = mode->w;
slouken@2753
   799
            closest->h = mode->h;
slouken@2753
   800
        }
slouken@2753
   801
        if (match->refresh_rate) {
slouken@2753
   802
            closest->refresh_rate = match->refresh_rate;
slouken@2753
   803
        } else {
slouken@2753
   804
            closest->refresh_rate = mode->refresh_rate;
slouken@2753
   805
        }
slouken@2753
   806
        closest->driverdata = match->driverdata;
slouken@1895
   807
slouken@2753
   808
        /*
slouken@2753
   809
         * Pick some reasonable defaults if the app and driver don't
slouken@2753
   810
         * care
slouken@2753
   811
         */
slouken@2753
   812
        if (!closest->format) {
slouken@2753
   813
            closest->format = SDL_PIXELFORMAT_RGB888;
slouken@2753
   814
        }
slouken@2753
   815
        if (!closest->w) {
slouken@2753
   816
            closest->w = 640;
slouken@2753
   817
        }
slouken@2753
   818
        if (!closest->h) {
slouken@2753
   819
            closest->h = 480;
slouken@2753
   820
        }
slouken@2753
   821
        return closest;
slouken@2753
   822
    }
slouken@2753
   823
    return NULL;
slouken@0
   824
}
slouken@0
   825
slouken@3500
   826
SDL_DisplayMode *
slouken@5244
   827
SDL_GetClosestDisplayMode(int displayIndex,
slouken@5244
   828
                          const SDL_DisplayMode * mode,
slouken@3500
   829
                          SDL_DisplayMode * closest)
slouken@3500
   830
{
slouken@5244
   831
    SDL_VideoDisplay *display;
slouken@5244
   832
slouken@5244
   833
    CHECK_DISPLAY_INDEX(displayIndex, NULL);
slouken@5244
   834
slouken@5244
   835
    display = &_this->displays[displayIndex];
slouken@5244
   836
    return SDL_GetClosestDisplayModeForDisplay(display, mode, closest);
slouken@3500
   837
}
slouken@3500
   838
slouken@1895
   839
int
slouken@3500
   840
SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
slouken@0
   841
{
slouken@2753
   842
    SDL_DisplayMode display_mode;
slouken@2753
   843
    SDL_DisplayMode current_mode;
slouken@0
   844
slouken@3502
   845
    if (mode) {
slouken@3502
   846
        display_mode = *mode;
slouken@3502
   847
slouken@3502
   848
        /* Default to the current mode */
slouken@3502
   849
        if (!display_mode.format) {
slouken@3502
   850
            display_mode.format = display->current_mode.format;
slouken@3502
   851
        }
slouken@3502
   852
        if (!display_mode.w) {
slouken@3502
   853
            display_mode.w = display->current_mode.w;
slouken@3502
   854
        }
slouken@3502
   855
        if (!display_mode.h) {
slouken@3502
   856
            display_mode.h = display->current_mode.h;
slouken@3502
   857
        }
slouken@3502
   858
        if (!display_mode.refresh_rate) {
slouken@3502
   859
            display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@3502
   860
        }
slouken@3502
   861
slouken@3502
   862
        /* Get a good video mode, the closest one possible */
slouken@3502
   863
        if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
slouken@3502
   864
            SDL_SetError("No video mode large enough for %dx%d",
slouken@3502
   865
                         display_mode.w, display_mode.h);
slouken@3502
   866
            return -1;
slouken@3502
   867
        }
slouken@3502
   868
    } else {
slouken@3502
   869
        display_mode = display->desktop_mode;
slouken@2753
   870
    }
slouken@3500
   871
slouken@2753
   872
    /* See if there's anything left to do */
slouken@5244
   873
    current_mode = display->current_mode;
slouken@2753
   874
    if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
slouken@2753
   875
        return 0;
slouken@2753
   876
    }
slouken@3500
   877
slouken@2753
   878
    /* Actually change the display mode */
slouken@4978
   879
    if (!_this->SetDisplayMode) {
slouken@4978
   880
        SDL_SetError("Video driver doesn't support changing display mode");
slouken@4978
   881
        return -1;
slouken@4978
   882
    }
slouken@3500
   883
    if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
slouken@2753
   884
        return -1;
slouken@2753
   885
    }
slouken@2753
   886
    display->current_mode = display_mode;
slouken@2753
   887
    return 0;
slouken@0
   888
}
slouken@0
   889
slouken@1895
   890
int
slouken@5246
   891
SDLCALL SDL_GetWindowDisplay(SDL_Window * window)
slouken@5246
   892
{
slouken@5246
   893
    int displayIndex;
slouken@5246
   894
    int i, dist;
slouken@5246
   895
    int closest = -1;
slouken@5246
   896
    int closest_dist = 0x7FFFFFFF;
slouken@5246
   897
    SDL_Point center;
slouken@5246
   898
    SDL_Point delta;
slouken@5246
   899
    SDL_Rect rect;
slouken@5246
   900
slouken@5246
   901
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5246
   902
slouken@5246
   903
    if (SDL_WINDOWPOS_ISUNDEFINED(window->x) ||
slouken@5246
   904
        SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@5246
   905
        displayIndex = (window->x & 0xFFFF);
slouken@5246
   906
        if (displayIndex >= _this->num_displays) {
slouken@5246
   907
            displayIndex = 0;
slouken@5246
   908
        }
slouken@5246
   909
        return displayIndex;
slouken@5246
   910
    }
slouken@5246
   911
    if (SDL_WINDOWPOS_ISUNDEFINED(window->y) ||
slouken@5246
   912
        SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@5246
   913
        displayIndex = (window->y & 0xFFFF);
slouken@5246
   914
        if (displayIndex >= _this->num_displays) {
slouken@5246
   915
            displayIndex = 0;
slouken@5246
   916
        }
slouken@5246
   917
        return displayIndex;
slouken@5246
   918
    }
slouken@5246
   919
slouken@5246
   920
    /* Find the display containing the window */
slouken@5246
   921
    center.x = window->x + window->w / 2;
slouken@5246
   922
    center.y = window->y + window->h / 2;
slouken@5246
   923
    for (i = 0; i < _this->num_displays; ++i) {
slouken@5246
   924
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@5246
   925
slouken@5246
   926
        SDL_GetDisplayBounds(i, &rect);
slouken@5246
   927
        if (display->fullscreen_window == window || SDL_EnclosePoints(&center, 1, &rect, NULL)) {
slouken@5246
   928
            return i;
slouken@5246
   929
        }
slouken@5246
   930
slouken@5246
   931
        delta.x = center.x - (rect.x + rect.w / 2);
slouken@5246
   932
        delta.y = center.y - (rect.y + rect.h / 2);
slouken@5246
   933
        dist = (delta.x*delta.x + delta.y*delta.y);
slouken@5246
   934
        if (dist < closest_dist) {
slouken@5246
   935
            closest = i;
slouken@5246
   936
            closest_dist = dist;
slouken@5246
   937
        }
slouken@5246
   938
    }
slouken@5246
   939
    if (closest < 0) {
slouken@5246
   940
        SDL_SetError("Couldn't find any displays");
slouken@5246
   941
    }
slouken@5246
   942
    return closest;
slouken@5246
   943
}
slouken@5246
   944
slouken@5246
   945
SDL_VideoDisplay *
slouken@5246
   946
SDL_GetDisplayForWindow(SDL_Window *window)
slouken@5246
   947
{
slouken@5246
   948
    return &_this->displays[SDL_GetWindowDisplay(window)];
slouken@5246
   949
}
slouken@5246
   950
slouken@5246
   951
int
slouken@3685
   952
SDL_SetWindowDisplayMode(SDL_Window * window, const SDL_DisplayMode * mode)
slouken@3500
   953
{
slouken@3695
   954
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3500
   955
slouken@2753
   956
    if (mode) {
slouken@3500
   957
        window->fullscreen_mode = *mode;
slouken@3500
   958
    } else {
slouken@3500
   959
        SDL_zero(window->fullscreen_mode);
slouken@3500
   960
    }
slouken@3526
   961
    return 0;
slouken@3500
   962
}
slouken@3500
   963
slouken@3500
   964
int
slouken@3685
   965
SDL_GetWindowDisplayMode(SDL_Window * window, SDL_DisplayMode * mode)
slouken@3500
   966
{
slouken@3500
   967
    SDL_DisplayMode fullscreen_mode;
slouken@3500
   968
slouken@3695
   969
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3500
   970
slouken@3500
   971
    fullscreen_mode = window->fullscreen_mode;
slouken@3500
   972
    if (!fullscreen_mode.w) {
slouken@3500
   973
        fullscreen_mode.w = window->w;
slouken@3500
   974
    }
slouken@3500
   975
    if (!fullscreen_mode.h) {
slouken@3500
   976
        fullscreen_mode.h = window->h;
slouken@3500
   977
    }
slouken@3500
   978
slouken@5246
   979
    if (!SDL_GetClosestDisplayModeForDisplay(SDL_GetDisplayForWindow(window),
slouken@3500
   980
                                             &fullscreen_mode,
slouken@3500
   981
                                             &fullscreen_mode)) {
slouken@3500
   982
        SDL_SetError("Couldn't find display mode match");
slouken@3500
   983
        return -1;
slouken@3500
   984
    }
slouken@3500
   985
slouken@3500
   986
    if (mode) {
slouken@3500
   987
        *mode = fullscreen_mode;
slouken@2753
   988
    }
slouken@2753
   989
    return 0;
slouken@0
   990
}
slouken@0
   991
slouken@5154
   992
Uint32
slouken@5154
   993
SDL_GetWindowPixelFormat(SDL_Window * window)
slouken@5154
   994
{
slouken@5246
   995
    SDL_VideoDisplay *display;
slouken@5246
   996
slouken@5246
   997
    CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
slouken@5246
   998
slouken@5246
   999
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  1000
    return display->current_mode.format;
slouken@5154
  1001
}
slouken@5154
  1002
slouken@3502
  1003
static void
slouken@5305
  1004
SDL_UpdateFullscreenMode(SDL_Window * window)
slouken@3502
  1005
{
slouken@5246
  1006
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5249
  1007
    SDL_Window *other;
slouken@3502
  1008
slouken@3502
  1009
    if (FULLSCREEN_VISIBLE(window)) {
slouken@3502
  1010
        /* Hide any other fullscreen windows */
slouken@5249
  1011
        if (display->fullscreen_window &&
slouken@5249
  1012
            display->fullscreen_window != window) {
slouken@5246
  1013
            SDL_MinimizeWindow(display->fullscreen_window);
slouken@3502
  1014
        }
slouken@3502
  1015
    }
slouken@3502
  1016
slouken@5305
  1017
    /* See if anything needs to be done now */
slouken@5305
  1018
    if ((display->fullscreen_window == window) == FULLSCREEN_VISIBLE(window)) {
slouken@5305
  1019
        return;
slouken@5305
  1020
    }
slouken@5305
  1021
slouken@5249
  1022
    /* See if there are any fullscreen windows */
slouken@5249
  1023
    for (other = _this->windows; other; other = other->next) {
slouken@5249
  1024
        if (FULLSCREEN_VISIBLE(other) &&
slouken@5249
  1025
            SDL_GetDisplayForWindow(other) == display) {
slouken@5249
  1026
            SDL_DisplayMode fullscreen_mode;
slouken@5249
  1027
            if (SDL_GetWindowDisplayMode(other, &fullscreen_mode) == 0) {
slouken@5306
  1028
                SDL_bool resized = SDL_TRUE;
slouken@5306
  1029
slouken@5306
  1030
                if (other->w == fullscreen_mode.w && other->h == fullscreen_mode.h) {
slouken@5306
  1031
                    resized = SDL_FALSE;
slouken@5249
  1032
                }
slouken@3502
  1033
slouken@3502
  1034
                SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
slouken@5249
  1035
                if (_this->SetWindowFullscreen) {
slouken@5305
  1036
                    _this->SetWindowFullscreen(_this, other, display, SDL_TRUE);
slouken@5249
  1037
                }
slouken@5249
  1038
                display->fullscreen_window = other;
slouken@5249
  1039
slouken@5306
  1040
                /* Generate a mode change event here */
slouken@5306
  1041
                if (resized) {
slouken@5306
  1042
                    SDL_SendWindowEvent(other, SDL_WINDOWEVENT_RESIZED,
slouken@5306
  1043
                                        fullscreen_mode.w, fullscreen_mode.h);
slouken@5306
  1044
                } else {
slouken@5306
  1045
                    SDL_OnWindowResized(other);
slouken@5306
  1046
                }
slouken@3502
  1047
                return;
slouken@3502
  1048
            }
slouken@3502
  1049
        }
slouken@3502
  1050
    }
slouken@3502
  1051
slouken@3502
  1052
    /* Nope, restore the desktop mode */
slouken@3502
  1053
    SDL_SetDisplayModeForDisplay(display, NULL);
slouken@5249
  1054
slouken@5249
  1055
    if (_this->SetWindowFullscreen) {
slouken@5305
  1056
        _this->SetWindowFullscreen(_this, window, display, SDL_FALSE);
slouken@5249
  1057
    }
slouken@3517
  1058
    display->fullscreen_window = NULL;
slouken@5306
  1059
slouken@5306
  1060
    /* Generate a mode change event here */
slouken@5306
  1061
    SDL_OnWindowResized(window);
slouken@3502
  1062
}
slouken@3502
  1063
slouken@3685
  1064
SDL_Window *
slouken@1895
  1065
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@0
  1066
{
slouken@2753
  1067
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@2753
  1068
                                  SDL_WINDOW_OPENGL |
slouken@2753
  1069
                                  SDL_WINDOW_BORDERLESS |
slouken@2875
  1070
                                  SDL_WINDOW_RESIZABLE |
slouken@2875
  1071
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@3685
  1072
    SDL_Window *window;
slouken@0
  1073
slouken@2753
  1074
    if (!_this) {
slouken@3417
  1075
        /* Initialize the video system if needed */
slouken@5123
  1076
        if (SDL_VideoInit(NULL) < 0) {
slouken@3685
  1077
            return NULL;
slouken@3417
  1078
        }
slouken@2753
  1079
    }
slouken@5249
  1080
slouken@5249
  1081
    /* Some platforms have OpenGL enabled by default */
slouken@5249
  1082
#if (SDL_VIDEO_OPENGL && __MACOSX__) || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@5249
  1083
    flags |= SDL_WINDOW_OPENGL;
slouken@5249
  1084
#endif
slouken@3057
  1085
    if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1086
        if (!_this->GL_CreateContext) {
slouken@3057
  1087
            SDL_SetError("No OpenGL support in video driver");
slouken@3685
  1088
            return NULL;
slouken@3057
  1089
        }
slouken@3057
  1090
        SDL_GL_LoadLibrary(NULL);
slouken@2753
  1091
    }
slouken@3685
  1092
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3695
  1093
    window->magic = &_this->window_magic;
slouken@3685
  1094
    window->id = _this->next_object_id++;
slouken@3685
  1095
    window->x = x;
slouken@3685
  1096
    window->y = y;
slouken@3685
  1097
    window->w = w;
slouken@3685
  1098
    window->h = h;
slouken@3685
  1099
    window->flags = (flags & allowed_flags);
slouken@5246
  1100
    window->next = _this->windows;
slouken@5246
  1101
    if (_this->windows) {
slouken@5246
  1102
        _this->windows->prev = window;
slouken@3693
  1103
    }
slouken@5246
  1104
    _this->windows = window;
slouken@3685
  1105
slouken@3685
  1106
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@3685
  1107
        SDL_DestroyWindow(window);
slouken@3685
  1108
        return NULL;
slouken@3685
  1109
    }
slouken@0
  1110
slouken@2753
  1111
    if (title) {
slouken@3685
  1112
        SDL_SetWindowTitle(window, title);
slouken@2753
  1113
    }
slouken@2753
  1114
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@3685
  1115
        SDL_MaximizeWindow(window);
slouken@2753
  1116
    }
slouken@2753
  1117
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@3685
  1118
        SDL_MinimizeWindow(window);
slouken@2753
  1119
    }
slouken@2753
  1120
    if (flags & SDL_WINDOW_SHOWN) {
slouken@3685
  1121
        SDL_ShowWindow(window);
slouken@3685
  1122
    }
slouken@3685
  1123
    SDL_UpdateWindowGrab(window);
slouken@3685
  1124
slouken@3685
  1125
    return window;
slouken@1895
  1126
}
slouken@0
  1127
slouken@3685
  1128
SDL_Window *
slouken@1895
  1129
SDL_CreateWindowFrom(const void *data)
slouken@1895
  1130
{
slouken@3685
  1131
    SDL_Window *window;
slouken@0
  1132
slouken@2753
  1133
    if (!_this) {
slouken@2753
  1134
        SDL_UninitializedVideo();
slouken@3685
  1135
        return NULL;
slouken@3685
  1136
    }
slouken@3685
  1137
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3695
  1138
    window->magic = &_this->window_magic;
slouken@3685
  1139
    window->id = _this->next_object_id++;
slouken@3685
  1140
    window->flags = SDL_WINDOW_FOREIGN;
slouken@5246
  1141
    window->next = _this->windows;
slouken@5246
  1142
    if (_this->windows) {
slouken@5246
  1143
        _this->windows->prev = window;
slouken@3693
  1144
    }
slouken@5246
  1145
    _this->windows = window;
slouken@1895
  1146
slouken@2753
  1147
    if (!_this->CreateWindowFrom ||
slouken@3685
  1148
        _this->CreateWindowFrom(_this, window, data) < 0) {
slouken@3685
  1149
        SDL_DestroyWindow(window);
slouken@3685
  1150
        return NULL;
slouken@3685
  1151
    }
slouken@3685
  1152
    return window;
slouken@1895
  1153
}
slouken@1895
  1154
slouken@1924
  1155
int
slouken@1928
  1156
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
  1157
{
slouken@3057
  1158
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@3057
  1159
                                  SDL_WINDOW_OPENGL |
slouken@3057
  1160
                                  SDL_WINDOW_BORDERLESS |
slouken@3057
  1161
                                  SDL_WINDOW_RESIZABLE |
slouken@3486
  1162
                                  SDL_WINDOW_INPUT_GRABBED |
slouken@3486
  1163
                                  SDL_WINDOW_FOREIGN);
slouken@2753
  1164
    char *title = window->title;
slouken@1956
  1165
slouken@2753
  1166
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
slouken@2753
  1167
        SDL_SetError("No OpenGL support in video driver");
slouken@2753
  1168
        return -1;
slouken@2753
  1169
    }
slouken@3057
  1170
slouken@3057
  1171
    if (window->flags & SDL_WINDOW_FOREIGN) {
slouken@3057
  1172
        /* Can't destroy and re-create foreign windows, hrm */
slouken@3057
  1173
        flags |= SDL_WINDOW_FOREIGN;
slouken@3057
  1174
    } else {
slouken@3057
  1175
        flags &= ~SDL_WINDOW_FOREIGN;
slouken@3057
  1176
    }
slouken@3057
  1177
slouken@5249
  1178
    /* Restore video mode, etc. */
slouken@5305
  1179
    SDL_HideWindow(window);
slouken@5249
  1180
slouken@5249
  1181
    /* Tear down the old native window */
slouken@5249
  1182
    if (window->surface) {
slouken@5288
  1183
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5249
  1184
        SDL_FreeSurface(window->surface);
slouken@5249
  1185
    }
slouken@5249
  1186
    if (_this->DestroyWindowFramebuffer) {
slouken@5249
  1187
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5249
  1188
    }
slouken@3057
  1189
    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@2753
  1190
        _this->DestroyWindow(_this, window);
slouken@2753
  1191
    }
slouken@3057
  1192
slouken@5249
  1193
    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
slouken@5249
  1194
        if (flags & SDL_WINDOW_OPENGL) {
slouken@5249
  1195
            SDL_GL_LoadLibrary(NULL);
slouken@5249
  1196
        } else {
slouken@5249
  1197
            SDL_GL_UnloadLibrary();
slouken@5249
  1198
        }
slouken@5249
  1199
    }
slouken@5249
  1200
slouken@2753
  1201
    window->title = NULL;
slouken@3057
  1202
    window->flags = (flags & allowed_flags);
slouken@1956
  1203
slouken@3057
  1204
    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@3057
  1205
        if (_this->CreateWindow(_this, window) < 0) {
slouken@3057
  1206
            if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1207
                SDL_GL_UnloadLibrary();
slouken@3057
  1208
            }
slouken@3057
  1209
            return -1;
slouken@3057
  1210
        }
slouken@2753
  1211
    }
slouken@3057
  1212
slouken@2753
  1213
    if (title) {
slouken@3685
  1214
        SDL_SetWindowTitle(window, title);
slouken@2753
  1215
        SDL_free(title);
slouken@2753
  1216
    }
slouken@2753
  1217
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@3685
  1218
        SDL_MaximizeWindow(window);
slouken@2753
  1219
    }
slouken@2753
  1220
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@3685
  1221
        SDL_MinimizeWindow(window);
slouken@2753
  1222
    }
slouken@2753
  1223
    if (flags & SDL_WINDOW_SHOWN) {
slouken@3685
  1224
        SDL_ShowWindow(window);
slouken@2753
  1225
    }
slouken@2875
  1226
    SDL_UpdateWindowGrab(window);
slouken@2875
  1227
slouken@2753
  1228
    return 0;
slouken@1924
  1229
}
slouken@1924
  1230
slouken@1895
  1231
Uint32
slouken@3685
  1232
SDL_GetWindowID(SDL_Window * window)
slouken@1895
  1233
{
slouken@3695
  1234
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1235
slouken@3685
  1236
    return window->id;
slouken@3685
  1237
}
slouken@3685
  1238
slouken@3685
  1239
SDL_Window *
slouken@3685
  1240
SDL_GetWindowFromID(Uint32 id)
slouken@3685
  1241
{
slouken@3685
  1242
    SDL_Window *window;
slouken@3685
  1243
slouken@3685
  1244
    if (!_this) {
slouken@3685
  1245
        return NULL;
slouken@3685
  1246
    }
slouken@5246
  1247
    for (window = _this->windows; window; window = window->next) {
slouken@5246
  1248
        if (window->id == id) {
slouken@5246
  1249
            return window;
slouken@3685
  1250
        }
slouken@3685
  1251
    }
slouken@3685
  1252
    return NULL;
slouken@3685
  1253
}
slouken@3685
  1254
slouken@3685
  1255
Uint32
slouken@3685
  1256
SDL_GetWindowFlags(SDL_Window * window)
slouken@3685
  1257
{
slouken@3695
  1258
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1259
slouken@2753
  1260
    return window->flags;
slouken@1895
  1261
}
slouken@1895
  1262
slouken@1895
  1263
void
slouken@3685
  1264
SDL_SetWindowTitle(SDL_Window * window, const char *title)
slouken@1895
  1265
{
slouken@3695
  1266
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1267
slouken@3695
  1268
    if (title == window->title) {
slouken@2753
  1269
        return;
slouken@2753
  1270
    }
slouken@2753
  1271
    if (window->title) {
slouken@2753
  1272
        SDL_free(window->title);
slouken@2753
  1273
    }
slouken@4871
  1274
    if (title && *title) {
slouken@2753
  1275
        window->title = SDL_strdup(title);
slouken@2753
  1276
    } else {
slouken@2753
  1277
        window->title = NULL;
slouken@2753
  1278
    }
slouken@1956
  1279
slouken@2753
  1280
    if (_this->SetWindowTitle) {
slouken@2753
  1281
        _this->SetWindowTitle(_this, window);
slouken@2753
  1282
    }
slouken@1895
  1283
}
slouken@1895
  1284
slouken@2753
  1285
const char *
slouken@3685
  1286
SDL_GetWindowTitle(SDL_Window * window)
slouken@1895
  1287
{
slouken@4872
  1288
    CHECK_WINDOW_MAGIC(window, "");
slouken@3695
  1289
slouken@4871
  1290
    return window->title ? window->title : "";
slouken@1895
  1291
}
slouken@1895
  1292
slouken@1895
  1293
void
slouken@3685
  1294
SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
slouken@2967
  1295
{
slouken@3695
  1296
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1297
slouken@2967
  1298
    if (_this->SetWindowIcon) {
slouken@2967
  1299
        _this->SetWindowIcon(_this, window, icon);
slouken@2967
  1300
    }
slouken@2967
  1301
}
slouken@2967
  1302
slouken@5165
  1303
void*
slouken@5165
  1304
SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
slouken@1895
  1305
{
slouken@5165
  1306
    SDL_WindowUserData *prev, *data;
slouken@3695
  1307
slouken@5165
  1308
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5165
  1309
slouken@5165
  1310
    /* See if the named data already exists */
slouken@5165
  1311
    prev = NULL;
slouken@5165
  1312
    for (data = window->data; data; prev = data, data = data->next) {
slouken@5165
  1313
        if (SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1314
            void *last_value = data->data;
slouken@5165
  1315
slouken@5165
  1316
            if (userdata) {
slouken@5165
  1317
                /* Set the new value */
slouken@5165
  1318
                data->data = userdata;
slouken@5165
  1319
            } else {
slouken@5165
  1320
                /* Delete this value */
slouken@5165
  1321
                if (prev) {
slouken@5165
  1322
                    prev->next = data->next;
slouken@5165
  1323
                } else {
slouken@5165
  1324
                    window->data = data->next;
slouken@5165
  1325
                }
slouken@5165
  1326
                SDL_free(data->name);
slouken@5165
  1327
                SDL_free(data);
slouken@5165
  1328
            }
slouken@5165
  1329
            return last_value;
slouken@5165
  1330
        }
slouken@5165
  1331
    }
slouken@5165
  1332
slouken@5165
  1333
    /* Add new data to the window */
slouken@5165
  1334
    if (userdata) {
slouken@5165
  1335
        data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
slouken@5165
  1336
        data->name = SDL_strdup(name);
slouken@5165
  1337
        data->data = userdata;
slouken@5165
  1338
        data->next = window->data;
slouken@5165
  1339
        window->data = data;
slouken@5165
  1340
    }
slouken@5165
  1341
    return NULL;
slouken@1895
  1342
}
slouken@1895
  1343
slouken@2753
  1344
void *
slouken@5165
  1345
SDL_GetWindowData(SDL_Window * window, const char *name)
slouken@1895
  1346
{
slouken@5165
  1347
    SDL_WindowUserData *data;
slouken@5165
  1348
slouken@3695
  1349
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  1350
slouken@5165
  1351
    for (data = window->data; data; data = data->next) {
slouken@5165
  1352
        if (SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1353
            return data->data;
slouken@5165
  1354
        }
slouken@5165
  1355
    }
slouken@5165
  1356
    return NULL;
slouken@1895
  1357
}
slouken@1895
  1358
slouken@1895
  1359
void
slouken@3685
  1360
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1361
{
slouken@3695
  1362
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1363
slouken@5305
  1364
    if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
slouken@2753
  1365
        window->x = x;
slouken@2753
  1366
    }
slouken@5305
  1367
    if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
slouken@2753
  1368
        window->y = y;
slouken@2753
  1369
    }
slouken@5246
  1370
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5246
  1371
        if (_this->SetWindowPosition) {
slouken@5246
  1372
            _this->SetWindowPosition(_this, window);
slouken@5246
  1373
        }
slouken@5246
  1374
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@2753
  1375
    }
slouken@1895
  1376
}
slouken@1895
  1377
slouken@1895
  1378
void
slouken@3685
  1379
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1380
{
slouken@5246
  1381
    /* Clear the values */
slouken@5246
  1382
    if (x) {
slouken@5246
  1383
        *x = 0;
slouken@5246
  1384
    }
slouken@5246
  1385
    if (y) {
slouken@5246
  1386
        *y = 0;
slouken@5246
  1387
    }
slouken@5246
  1388
slouken@5246
  1389
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1390
slouken@5246
  1391
    /* Fullscreen windows are always at their display's origin */
slouken@5246
  1392
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5246
  1393
    } else {
slouken@5242
  1394
        if (x) {
slouken@5242
  1395
            *x = window->x;
slouken@5242
  1396
        }
slouken@5242
  1397
        if (y) {
slouken@5242
  1398
            *y = window->y;
slouken@5242
  1399
        }
slouken@2753
  1400
    }
slouken@1895
  1401
}
slouken@1895
  1402
slouken@1895
  1403
void
slouken@3685
  1404
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1405
{
slouken@3695
  1406
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1407
slouken@5249
  1408
    /* FIXME: Should this change fullscreen modes? */
slouken@5249
  1409
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5276
  1410
        window->w = w;
slouken@5276
  1411
        window->h = h;
slouken@5249
  1412
        if (_this->SetWindowSize) {
slouken@5249
  1413
            _this->SetWindowSize(_this, window);
slouken@5249
  1414
        }
slouken@5276
  1415
        if (window->w == w && window->h == h) {
slouken@5276
  1416
            /* We didn't get a SDL_WINDOWEVENT_RESIZED event (by design) */
slouken@5276
  1417
            SDL_OnWindowResized(window);
slouken@5276
  1418
        }
slouken@2753
  1419
    }
slouken@1895
  1420
}
slouken@1895
  1421
slouken@1895
  1422
void
slouken@3685
  1423
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1424
{
slouken@5246
  1425
    int dummy;
slouken@5246
  1426
slouken@5246
  1427
    if (!w) {
slouken@5246
  1428
        w = &dummy;
slouken@5246
  1429
    }
slouken@5246
  1430
    if (!h) {
slouken@5246
  1431
        h = &dummy;
slouken@5246
  1432
    }
slouken@5246
  1433
slouken@5246
  1434
    *w = 0;
slouken@5246
  1435
    *h = 0;
slouken@5246
  1436
slouken@5246
  1437
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1438
slouken@5242
  1439
    if (_this && window && window->magic == &_this->window_magic) {
slouken@2849
  1440
        if (w) {
slouken@2849
  1441
            *w = window->w;
slouken@2849
  1442
        }
slouken@2849
  1443
        if (h) {
slouken@2849
  1444
            *h = window->h;
slouken@2849
  1445
        }
slouken@2849
  1446
    } else {
slouken@2849
  1447
        if (w) {
slouken@2849
  1448
            *w = 0;
slouken@2849
  1449
        }
slouken@2849
  1450
        if (h) {
slouken@2849
  1451
            *h = 0;
slouken@2849
  1452
        }
slouken@2753
  1453
    }
slouken@1895
  1454
}
slouken@1895
  1455
slouken@1895
  1456
void
slouken@3685
  1457
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1458
{
slouken@3695
  1459
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1460
slouken@3695
  1461
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@2753
  1462
        return;
slouken@2753
  1463
    }
slouken@1895
  1464
slouken@2753
  1465
    if (_this->ShowWindow) {
slouken@2753
  1466
        _this->ShowWindow(_this, window);
slouken@2753
  1467
    }
slouken@3685
  1468
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1469
}
slouken@1895
  1470
slouken@1895
  1471
void
slouken@3685
  1472
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1473
{
slouken@3695
  1474
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1475
slouken@3695
  1476
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1477
        return;
slouken@2753
  1478
    }
slouken@1895
  1479
slouken@2753
  1480
    if (_this->HideWindow) {
slouken@2753
  1481
        _this->HideWindow(_this, window);
slouken@2753
  1482
    }
slouken@3685
  1483
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1484
}
slouken@1895
  1485
slouken@1895
  1486
void
slouken@3685
  1487
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1488
{
slouken@3695
  1489
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1490
slouken@3695
  1491
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1492
        return;
slouken@2753
  1493
    }
slouken@2753
  1494
    if (_this->RaiseWindow) {
slouken@2753
  1495
        _this->RaiseWindow(_this, window);
slouken@3502
  1496
    } else {
slouken@3502
  1497
        /* FIXME: What we really want is a way to request focus */
slouken@3685
  1498
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@2753
  1499
    }
slouken@1895
  1500
}
slouken@1895
  1501
slouken@1895
  1502
void
slouken@3685
  1503
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1504
{
slouken@3695
  1505
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1506
slouken@3695
  1507
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
  1508
        return;
slouken@2753
  1509
    }
slouken@1895
  1510
slouken@2753
  1511
    if (_this->MaximizeWindow) {
slouken@2753
  1512
        _this->MaximizeWindow(_this, window);
slouken@2753
  1513
    }
slouken@3685
  1514
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
  1515
}
slouken@1895
  1516
slouken@1895
  1517
void
slouken@3685
  1518
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1519
{
slouken@3695
  1520
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1521
slouken@3695
  1522
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
  1523
        return;
slouken@2753
  1524
    }
slouken@1895
  1525
slouken@2753
  1526
    if (_this->MinimizeWindow) {
slouken@2753
  1527
        _this->MinimizeWindow(_this, window);
slouken@2753
  1528
    }
slouken@3685
  1529
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
  1530
}
slouken@1895
  1531
slouken@1895
  1532
void
slouken@3685
  1533
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1534
{
slouken@3695
  1535
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1536
slouken@3695
  1537
    if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1538
        return;
slouken@2753
  1539
    }
slouken@1895
  1540
slouken@2753
  1541
    if (_this->RestoreWindow) {
slouken@2753
  1542
        _this->RestoreWindow(_this, window);
slouken@2753
  1543
    }
slouken@3685
  1544
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
  1545
}
slouken@1895
  1546
slouken@1895
  1547
int
slouken@5246
  1548
SDL_SetWindowFullscreen(SDL_Window * window, SDL_bool fullscreen)
slouken@1895
  1549
{
slouken@3695
  1550
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1551
slouken@2753
  1552
    if (fullscreen) {
slouken@2753
  1553
        fullscreen = SDL_WINDOW_FULLSCREEN;
slouken@2753
  1554
    }
slouken@2753
  1555
    if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
slouken@2753
  1556
        return 0;
slouken@2753
  1557
    }
slouken@2753
  1558
    if (fullscreen) {
slouken@2753
  1559
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@2753
  1560
    } else {
slouken@2753
  1561
        window->flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@5305
  1562
    }
slouken@5305
  1563
    SDL_UpdateFullscreenMode(window);
slouken@1895
  1564
slouken@2753
  1565
    return 0;
slouken@1895
  1566
}
slouken@1895
  1567
slouken@5166
  1568
static SDL_Surface *
slouken@5166
  1569
SDL_CreateWindowFramebuffer(SDL_Window * window)
slouken@5166
  1570
{
slouken@5166
  1571
    Uint32 format;
slouken@5166
  1572
    void *pixels;
slouken@5166
  1573
    int pitch;
slouken@5166
  1574
    int bpp;
slouken@5166
  1575
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5166
  1576
slouken@5166
  1577
    if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
slouken@5166
  1578
        return NULL;
slouken@5166
  1579
    }
slouken@5166
  1580
slouken@5166
  1581
    if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
slouken@5166
  1582
        return NULL;
slouken@5166
  1583
    }
slouken@5166
  1584
slouken@5166
  1585
    if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5166
  1586
        return NULL;
slouken@5166
  1587
    }
slouken@5166
  1588
slouken@5166
  1589
    return SDL_CreateRGBSurfaceFrom(pixels, window->w, window->h, bpp, pitch, Rmask, Gmask, Bmask, Amask);
slouken@5166
  1590
}
slouken@5166
  1591
slouken@5166
  1592
SDL_Surface *
slouken@5166
  1593
SDL_GetWindowSurface(SDL_Window * window)
slouken@5166
  1594
{
slouken@5166
  1595
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5166
  1596
slouken@5172
  1597
    if (!window->surface_valid) {
slouken@5172
  1598
        if (window->surface) {
slouken@5288
  1599
            window->surface->flags &= ~SDL_DONTFREE;
slouken@5172
  1600
            SDL_FreeSurface(window->surface);
slouken@5172
  1601
        }
slouken@5166
  1602
        window->surface = SDL_CreateWindowFramebuffer(window);
slouken@5169
  1603
        if (window->surface) {
slouken@5172
  1604
            window->surface_valid = SDL_TRUE;
slouken@5288
  1605
            window->surface->flags |= SDL_DONTFREE;
slouken@5169
  1606
        }
slouken@5166
  1607
    }
slouken@5166
  1608
    return window->surface;
slouken@5166
  1609
}
slouken@5166
  1610
slouken@5166
  1611
int
slouken@5166
  1612
SDL_UpdateWindowSurface(SDL_Window * window)
slouken@5166
  1613
{
slouken@5166
  1614
    SDL_Rect full_rect;
slouken@5166
  1615
slouken@5166
  1616
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1617
slouken@5166
  1618
    full_rect.x = 0;
slouken@5166
  1619
    full_rect.y = 0;
slouken@5166
  1620
    full_rect.w = window->w;
slouken@5166
  1621
    full_rect.h = window->h;
slouken@5297
  1622
    return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
slouken@5166
  1623
}
slouken@5166
  1624
slouken@5166
  1625
int
slouken@5297
  1626
SDL_UpdateWindowSurfaceRects(SDL_Window * window, SDL_Rect * rects,
slouken@5297
  1627
                             int numrects)
slouken@5166
  1628
{
slouken@5166
  1629
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1630
slouken@5172
  1631
    if (!window->surface_valid) {
slouken@5166
  1632
        SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
slouken@5166
  1633
        return -1;
slouken@5166
  1634
    }
slouken@5166
  1635
slouken@5297
  1636
    return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
slouken@5166
  1637
}
slouken@5166
  1638
slouken@1895
  1639
void
slouken@3685
  1640
SDL_SetWindowGrab(SDL_Window * window, int mode)
slouken@1895
  1641
{
slouken@3695
  1642
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1643
slouken@3695
  1644
    if ((!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@2753
  1645
        return;
slouken@2753
  1646
    }
slouken@2753
  1647
    if (mode) {
slouken@2753
  1648
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1649
    } else {
slouken@2753
  1650
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1651
    }
slouken@2875
  1652
    SDL_UpdateWindowGrab(window);
slouken@2875
  1653
}
slouken@1895
  1654
slouken@2875
  1655
static void
slouken@2876
  1656
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  1657
{
slouken@2753
  1658
    if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
slouken@2753
  1659
        _this->SetWindowGrab(_this, window);
slouken@2753
  1660
    }
slouken@1895
  1661
}
slouken@1895
  1662
slouken@1895
  1663
int
slouken@3685
  1664
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  1665
{
slouken@3695
  1666
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1667
slouken@2753
  1668
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  1669
}
slouken@1895
  1670
slouken@1895
  1671
void
slouken@1895
  1672
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  1673
{
slouken@3685
  1674
    SDL_RaiseWindow(window);
slouken@5305
  1675
    SDL_UpdateFullscreenMode(window);
slouken@1895
  1676
}
slouken@1895
  1677
slouken@1895
  1678
void
slouken@1895
  1679
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  1680
{
slouken@5305
  1681
    SDL_UpdateFullscreenMode(window);
slouken@1895
  1682
}
slouken@1895
  1683
slouken@1895
  1684
void
slouken@5166
  1685
SDL_OnWindowResized(SDL_Window * window)
slouken@5166
  1686
{
slouken@5172
  1687
    window->surface_valid = SDL_FALSE;
slouken@5276
  1688
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
slouken@5166
  1689
}
slouken@5166
  1690
slouken@5166
  1691
void
slouken@3502
  1692
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  1693
{
slouken@5305
  1694
    SDL_UpdateFullscreenMode(window);
slouken@3502
  1695
}
slouken@3502
  1696
slouken@3502
  1697
void
slouken@3502
  1698
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  1699
{
slouken@3685
  1700
    SDL_RaiseWindow(window);
slouken@5305
  1701
    SDL_UpdateFullscreenMode(window);
slouken@3502
  1702
}
slouken@3502
  1703
slouken@3502
  1704
void
slouken@1895
  1705
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  1706
{
slouken@2876
  1707
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1708
        && _this->SetWindowGrab) {
slouken@2753
  1709
        _this->SetWindowGrab(_this, window);
slouken@2753
  1710
    }
slouken@1895
  1711
}
slouken@1895
  1712
slouken@1895
  1713
void
slouken@1895
  1714
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  1715
{
slouken@3512
  1716
    /* If we're fullscreen on a single-head system and lose focus, minimize */
slouken@3512
  1717
    if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
slouken@3512
  1718
        _this->num_displays == 1) {
slouken@3685
  1719
        SDL_MinimizeWindow(window);
slouken@3512
  1720
    }
slouken@3512
  1721
slouken@2876
  1722
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1723
        && _this->SetWindowGrab) {
slouken@2753
  1724
        _this->SetWindowGrab(_this, window);
slouken@2753
  1725
    }
slouken@1895
  1726
}
slouken@1895
  1727
slouken@3685
  1728
SDL_Window *
slouken@1895
  1729
SDL_GetFocusWindow(void)
slouken@1895
  1730
{
slouken@3685
  1731
    SDL_Window *window;
slouken@1895
  1732
slouken@2753
  1733
    if (!_this) {
slouken@3685
  1734
        return NULL;
slouken@3685
  1735
    }
slouken@5246
  1736
    for (window = _this->windows; window; window = window->next) {
slouken@2753
  1737
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  1738
            return window;
slouken@2753
  1739
        }
slouken@2753
  1740
    }
slouken@3685
  1741
    return NULL;
slouken@1895
  1742
}
slouken@1895
  1743
slouken@1895
  1744
void
slouken@3685
  1745
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  1746
{
slouken@3685
  1747
    SDL_VideoDisplay *display;
slouken@3685
  1748
slouken@3695
  1749
    CHECK_WINDOW_MAGIC(window, );
slouken@3503
  1750
slouken@3685
  1751
    /* Restore video mode, etc. */
slouken@5305
  1752
    SDL_HideWindow(window);
slouken@3685
  1753
slouken@5169
  1754
    if (window->surface) {
slouken@5288
  1755
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5169
  1756
        SDL_FreeSurface(window->surface);
slouken@5169
  1757
    }
slouken@5166
  1758
    if (_this->DestroyWindowFramebuffer) {
slouken@5166
  1759
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5166
  1760
    }
slouken@3685
  1761
    if (_this->DestroyWindow) {
slouken@3685
  1762
        _this->DestroyWindow(_this, window);
slouken@3685
  1763
    }
slouken@3685
  1764
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  1765
        SDL_GL_UnloadLibrary();
slouken@3685
  1766
    }
slouken@3685
  1767
slouken@5246
  1768
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  1769
    if (display->fullscreen_window == window) {
slouken@5246
  1770
        display->fullscreen_window = NULL;
slouken@5246
  1771
    }
slouken@5246
  1772
slouken@4901
  1773
    /* Now invalidate magic */
slouken@4897
  1774
    window->magic = NULL;
slouken@4897
  1775
slouken@5165
  1776
    /* Free memory associated with the window */
slouken@5165
  1777
    if (window->title) {
slouken@5165
  1778
        SDL_free(window->title);
slouken@5165
  1779
    }
slouken@5165
  1780
    while (window->data) {
slouken@5165
  1781
        SDL_WindowUserData *data = window->data;
slouken@5165
  1782
slouken@5165
  1783
        window->data = data->next;
slouken@5165
  1784
        SDL_free(data->name);
slouken@5165
  1785
        SDL_free(data);
slouken@5165
  1786
    }
slouken@5165
  1787
slouken@3685
  1788
    /* Unlink the window from the list */
slouken@3693
  1789
    if (window->next) {
slouken@3693
  1790
        window->next->prev = window->prev;
slouken@3693
  1791
    }
slouken@3685
  1792
    if (window->prev) {
slouken@3685
  1793
        window->prev->next = window->next;
slouken@3685
  1794
    } else {
slouken@5246
  1795
        _this->windows = window->next;
slouken@3685
  1796
    }
slouken@3685
  1797
slouken@3685
  1798
    SDL_free(window);
slouken@1895
  1799
}
slouken@1895
  1800
slouken@3025
  1801
SDL_bool
slouken@3025
  1802
SDL_IsScreenSaverEnabled()
slouken@3025
  1803
{
slouken@3025
  1804
    if (!_this) {
slouken@3025
  1805
        return SDL_TRUE;
slouken@3025
  1806
    }
slouken@3025
  1807
    return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
slouken@3025
  1808
}
slouken@3025
  1809
slouken@3025
  1810
void
slouken@3025
  1811
SDL_EnableScreenSaver()
slouken@3025
  1812
{
slouken@3025
  1813
    if (!_this) {
slouken@3025
  1814
        return;
slouken@3025
  1815
    }
slouken@3025
  1816
    if (!_this->suspend_screensaver) {
slouken@3025
  1817
        return;
slouken@3025
  1818
    }
slouken@3025
  1819
    _this->suspend_screensaver = SDL_FALSE;
slouken@3025
  1820
    if (_this->SuspendScreenSaver) {
slouken@3025
  1821
        _this->SuspendScreenSaver(_this);
slouken@3025
  1822
    }
slouken@3025
  1823
}
slouken@3025
  1824
slouken@3025
  1825
void
slouken@3025
  1826
SDL_DisableScreenSaver()
slouken@3025
  1827
{
slouken@3025
  1828
    if (!_this) {
slouken@3025
  1829
        return;
slouken@3025
  1830
    }
slouken@3025
  1831
    if (_this->suspend_screensaver) {
slouken@3025
  1832
        return;
slouken@3025
  1833
    }
slouken@3025
  1834
    _this->suspend_screensaver = SDL_TRUE;
slouken@3025
  1835
    if (_this->SuspendScreenSaver) {
slouken@3025
  1836
        _this->SuspendScreenSaver(_this);
slouken@3025
  1837
    }
slouken@3025
  1838
}
slouken@3025
  1839
slouken@1895
  1840
void
slouken@1895
  1841
SDL_VideoQuit(void)
slouken@1895
  1842
{
slouken@2753
  1843
    int i, j;
slouken@1895
  1844
slouken@2753
  1845
    if (!_this) {
slouken@2753
  1846
        return;
slouken@2753
  1847
    }
slouken@5123
  1848
slouken@2753
  1849
    /* Halt event processing before doing anything else */
slouken@5123
  1850
    SDL_QuitQuit();
slouken@5123
  1851
    SDL_MouseQuit();
slouken@5123
  1852
    SDL_KeyboardQuit();
slouken@2753
  1853
    SDL_StopEventLoop();
slouken@5123
  1854
slouken@3029
  1855
    SDL_EnableScreenSaver();
slouken@1895
  1856
slouken@2753
  1857
    /* Clean up the system video */
slouken@5246
  1858
    while (_this->windows) {
slouken@5246
  1859
        SDL_DestroyWindow(_this->windows);
slouken@2753
  1860
    }
slouken@2753
  1861
    _this->VideoQuit(_this);
slouken@1895
  1862
slouken@2753
  1863
    for (i = _this->num_displays; i--;) {
slouken@2753
  1864
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  1865
        for (j = display->num_display_modes; j--;) {
slouken@2753
  1866
            if (display->display_modes[j].driverdata) {
slouken@2753
  1867
                SDL_free(display->display_modes[j].driverdata);
slouken@2753
  1868
                display->display_modes[j].driverdata = NULL;
slouken@2753
  1869
            }
slouken@2753
  1870
        }
slouken@2753
  1871
        if (display->display_modes) {
slouken@2753
  1872
            SDL_free(display->display_modes);
slouken@2753
  1873
            display->display_modes = NULL;
slouken@2753
  1874
        }
slouken@2753
  1875
        if (display->desktop_mode.driverdata) {
slouken@2753
  1876
            SDL_free(display->desktop_mode.driverdata);
slouken@2753
  1877
            display->desktop_mode.driverdata = NULL;
slouken@2753
  1878
        }
slouken@2753
  1879
        if (display->driverdata) {
slouken@2753
  1880
            SDL_free(display->driverdata);
slouken@2753
  1881
            display->driverdata = NULL;
slouken@2753
  1882
        }
slouken@2753
  1883
    }
slouken@2753
  1884
    if (_this->displays) {
slouken@2753
  1885
        SDL_free(_this->displays);
slouken@2753
  1886
        _this->displays = NULL;
slouken@2753
  1887
    }
slouken@4495
  1888
    if (_this->clipboard_text) {
slouken@4495
  1889
        SDL_free(_this->clipboard_text);
slouken@4495
  1890
        _this->clipboard_text = NULL;
slouken@4495
  1891
    }
slouken@2753
  1892
    _this->free(_this);
slouken@2753
  1893
    _this = NULL;
slouken@0
  1894
}
slouken@0
  1895
slouken@1895
  1896
int
slouken@1895
  1897
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1898
{
slouken@2753
  1899
    int retval;
slouken@0
  1900
slouken@2753
  1901
    if (!_this) {
slouken@2753
  1902
        SDL_UninitializedVideo();
slouken@2753
  1903
        return -1;
slouken@2753
  1904
    }
slouken@3057
  1905
    if (_this->gl_config.driver_loaded) {
slouken@3057
  1906
        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
slouken@3057
  1907
            SDL_SetError("OpenGL library already loaded");
slouken@3057
  1908
            return -1;
slouken@3057
  1909
        }
slouken@3057
  1910
        retval = 0;
slouken@3057
  1911
    } else {
slouken@3057
  1912
        if (!_this->GL_LoadLibrary) {
slouken@3057
  1913
            SDL_SetError("No dynamic GL support in video driver");
slouken@3057
  1914
            return -1;
slouken@3057
  1915
        }
slouken@2753
  1916
        retval = _this->GL_LoadLibrary(_this, path);
slouken@3057
  1917
    }
slouken@3057
  1918
    if (retval == 0) {
slouken@3057
  1919
        ++_this->gl_config.driver_loaded;
slouken@2753
  1920
    }
slouken@2753
  1921
    return (retval);
slouken@0
  1922
}
slouken@0
  1923
slouken@2753
  1924
void *
slouken@1895
  1925
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1926
{
slouken@2753
  1927
    void *func;
slouken@0
  1928
slouken@2753
  1929
    if (!_this) {
slouken@2753
  1930
        SDL_UninitializedVideo();
slouken@2753
  1931
        return NULL;
slouken@2753
  1932
    }
slouken@2753
  1933
    func = NULL;
slouken@2753
  1934
    if (_this->GL_GetProcAddress) {
slouken@2753
  1935
        if (_this->gl_config.driver_loaded) {
slouken@2753
  1936
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  1937
        } else {
slouken@2753
  1938
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  1939
        }
slouken@2753
  1940
    } else {
slouken@2753
  1941
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  1942
    }
slouken@2753
  1943
    return func;
slouken@0
  1944
}
slouken@0
  1945
slouken@3057
  1946
void
slouken@3057
  1947
SDL_GL_UnloadLibrary(void)
slouken@3057
  1948
{
slouken@3057
  1949
    if (!_this) {
slouken@3057
  1950
        SDL_UninitializedVideo();
slouken@3057
  1951
        return;
slouken@3057
  1952
    }
slouken@3057
  1953
    if (_this->gl_config.driver_loaded > 0) {
slouken@3057
  1954
        if (--_this->gl_config.driver_loaded > 0) {
slouken@3057
  1955
            return;
slouken@3057
  1956
        }
slouken@3057
  1957
        if (_this->GL_UnloadLibrary) {
slouken@3057
  1958
            _this->GL_UnloadLibrary(_this);
slouken@3057
  1959
        }
slouken@3057
  1960
    }
slouken@3057
  1961
}
slouken@3057
  1962
slouken@1926
  1963
SDL_bool
slouken@1926
  1964
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  1965
{
slouken@5209
  1966
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  1967
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  1968
    const char *extensions;
slouken@2753
  1969
    const char *start;
slouken@2753
  1970
    const char *where, *terminator;
slouken@1926
  1971
slouken@2753
  1972
    /* Extension names should not have spaces. */
slouken@2753
  1973
    where = SDL_strchr(extension, ' ');
slouken@2753
  1974
    if (where || *extension == '\0') {
slouken@2753
  1975
        return SDL_FALSE;
slouken@2753
  1976
    }
slouken@2753
  1977
    /* See if there's an environment variable override */
slouken@2753
  1978
    start = SDL_getenv(extension);
slouken@2753
  1979
    if (start && *start == '0') {
slouken@2753
  1980
        return SDL_FALSE;
slouken@2753
  1981
    }
slouken@2753
  1982
    /* Lookup the available extensions */
slouken@2753
  1983
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@2753
  1984
    if (glGetStringFunc) {
slouken@2753
  1985
        extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  1986
    } else {
slouken@2753
  1987
        extensions = NULL;
slouken@2753
  1988
    }
slouken@2753
  1989
    if (!extensions) {
slouken@2753
  1990
        return SDL_FALSE;
slouken@2753
  1991
    }
slouken@2753
  1992
    /*
slouken@2753
  1993
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  1994
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  1995
     */
slouken@1926
  1996
slouken@2753
  1997
    start = extensions;
slouken@1926
  1998
slouken@2753
  1999
    for (;;) {
slouken@2753
  2000
        where = SDL_strstr(start, extension);
slouken@2753
  2001
        if (!where)
slouken@2753
  2002
            break;
slouken@1926
  2003
slouken@2753
  2004
        terminator = where + SDL_strlen(extension);
slouken@2753
  2005
        if (where == start || *(where - 1) == ' ')
slouken@2753
  2006
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  2007
                return SDL_TRUE;
slouken@1926
  2008
slouken@2753
  2009
        start = terminator;
slouken@2753
  2010
    }
slouken@2753
  2011
    return SDL_FALSE;
slouken@1926
  2012
#else
slouken@2753
  2013
    return SDL_FALSE;
slouken@1926
  2014
#endif
slouken@1926
  2015
}
slouken@1926
  2016
slouken@1895
  2017
int
slouken@1895
  2018
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2019
{
slouken@5209
  2020
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2021
    int retval;
slouken@0
  2022
slouken@2753
  2023
    if (!_this) {
slouken@2753
  2024
        SDL_UninitializedVideo();
slouken@2753
  2025
        return -1;
slouken@2753
  2026
    }
slouken@2753
  2027
    retval = 0;
slouken@2753
  2028
    switch (attr) {
slouken@2753
  2029
    case SDL_GL_RED_SIZE:
slouken@2753
  2030
        _this->gl_config.red_size = value;
slouken@2753
  2031
        break;
slouken@2753
  2032
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2033
        _this->gl_config.green_size = value;
slouken@2753
  2034
        break;
slouken@2753
  2035
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2036
        _this->gl_config.blue_size = value;
slouken@2753
  2037
        break;
slouken@2753
  2038
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2039
        _this->gl_config.alpha_size = value;
slouken@2753
  2040
        break;
slouken@2753
  2041
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  2042
        _this->gl_config.double_buffer = value;
slouken@2753
  2043
        break;
slouken@2753
  2044
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2045
        _this->gl_config.buffer_size = value;
slouken@2753
  2046
        break;
slouken@2753
  2047
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2048
        _this->gl_config.depth_size = value;
slouken@2753
  2049
        break;
slouken@2753
  2050
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2051
        _this->gl_config.stencil_size = value;
slouken@2753
  2052
        break;
slouken@2753
  2053
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2054
        _this->gl_config.accum_red_size = value;
slouken@2753
  2055
        break;
slouken@2753
  2056
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2057
        _this->gl_config.accum_green_size = value;
slouken@2753
  2058
        break;
slouken@2753
  2059
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2060
        _this->gl_config.accum_blue_size = value;
slouken@2753
  2061
        break;
slouken@2753
  2062
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2063
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  2064
        break;
slouken@2753
  2065
    case SDL_GL_STEREO:
slouken@2753
  2066
        _this->gl_config.stereo = value;
slouken@2753
  2067
        break;
slouken@2753
  2068
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  2069
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  2070
        break;
slouken@2753
  2071
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  2072
        _this->gl_config.multisamplesamples = value;
slouken@2753
  2073
        break;
slouken@2753
  2074
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2075
        _this->gl_config.accelerated = value;
slouken@2753
  2076
        break;
slouken@2753
  2077
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2078
        _this->gl_config.retained_backing = value;
slouken@2753
  2079
        break;
slouken@3100
  2080
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@3100
  2081
        _this->gl_config.major_version = value;
slouken@3100
  2082
        break;
slouken@3100
  2083
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@3100
  2084
        _this->gl_config.minor_version = value;
slouken@3100
  2085
        break;
slouken@2753
  2086
    default:
slouken@2753
  2087
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2088
        retval = -1;
slouken@2753
  2089
        break;
slouken@2753
  2090
    }
slouken@2753
  2091
    return retval;
slouken@1936
  2092
#else
slouken@2753
  2093
    SDL_Unsupported();
slouken@2753
  2094
    return -1;
slouken@2753
  2095
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2096
}
slouken@0
  2097
slouken@1895
  2098
int
slouken@1936
  2099
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  2100
{
slouken@5209
  2101
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2102
    void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
slouken@3139
  2103
    GLenum(APIENTRY * glGetErrorFunc) (void);
slouken@2753
  2104
    GLenum attrib = 0;
slouken@3099
  2105
    GLenum error = 0;
slouken@1912
  2106
slouken@2753
  2107
    glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
slouken@2753
  2108
    if (!glGetIntegervFunc) {
slouken@2753
  2109
        return -1;
slouken@2753
  2110
    }
slouken@3099
  2111
slouken@3099
  2112
    glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
slouken@3099
  2113
    if (!glGetErrorFunc) {
slouken@3099
  2114
        return -1;
slouken@3099
  2115
    }
slouken@3099
  2116
slouken@3099
  2117
    /* Clear value in any case */
slouken@3139
  2118
    *value = 0;
slouken@3099
  2119
slouken@2753
  2120
    switch (attr) {
slouken@2753
  2121
    case SDL_GL_RED_SIZE:
slouken@2753
  2122
        attrib = GL_RED_BITS;
slouken@2753
  2123
        break;
slouken@2753
  2124
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2125
        attrib = GL_BLUE_BITS;
slouken@2753
  2126
        break;
slouken@2753
  2127
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2128
        attrib = GL_GREEN_BITS;
slouken@2753
  2129
        break;
slouken@2753
  2130
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2131
        attrib = GL_ALPHA_BITS;
slouken@2753
  2132
        break;
slouken@2753
  2133
    case SDL_GL_DOUBLEBUFFER:
slouken@5209
  2134
#if SDL_VIDEO_OPENGL
slouken@2753
  2135
        attrib = GL_DOUBLEBUFFER;
slouken@2753
  2136
        break;
hfutrell@2745
  2137
#else
slouken@3099
  2138
        /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
slouken@3099
  2139
        /* parameter which switches double buffer to single buffer. OpenGL ES */
slouken@3099
  2140
        /* SDL driver must set proper value after initialization              */
slouken@3099
  2141
        *value = _this->gl_config.double_buffer;
slouken@2753
  2142
        return 0;
hfutrell@2745
  2143
#endif
slouken@2753
  2144
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2145
        attrib = GL_DEPTH_BITS;
slouken@2753
  2146
        break;
slouken@2753
  2147
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2148
        attrib = GL_STENCIL_BITS;
slouken@2753
  2149
        break;
slouken@5209
  2150
#if SDL_VIDEO_OPENGL
slouken@2753
  2151
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2152
        attrib = GL_ACCUM_RED_BITS;
slouken@2753
  2153
        break;
slouken@2753
  2154
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2155
        attrib = GL_ACCUM_GREEN_BITS;
slouken@2753
  2156
        break;
slouken@2753
  2157
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2158
        attrib = GL_ACCUM_BLUE_BITS;
slouken@2753
  2159
        break;
slouken@2753
  2160
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2161
        attrib = GL_ACCUM_ALPHA_BITS;
slouken@2753
  2162
        break;
slouken@2753
  2163
    case SDL_GL_STEREO:
slouken@2753
  2164
        attrib = GL_STEREO;
slouken@2753
  2165
        break;
hfutrell@2745
  2166
#else
slouken@2753
  2167
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2168
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2169
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2170
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2171
    case SDL_GL_STEREO:
slouken@2753
  2172
        /* none of these are supported in OpenGL ES */
slouken@2753
  2173
        *value = 0;
slouken@2753
  2174
        return 0;
hfutrell@2745
  2175
#endif
slouken@2753
  2176
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@5209
  2177
#if SDL_VIDEO_OPENGL
slouken@2753
  2178
        attrib = GL_SAMPLE_BUFFERS_ARB;
hfutrell@2745
  2179
#else
slouken@2753
  2180
        attrib = GL_SAMPLE_BUFFERS;
hfutrell@2745
  2181
#endif
slouken@2753
  2182
        break;
slouken@2753
  2183
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@5209
  2184
#if SDL_VIDEO_OPENGL
slouken@2753
  2185
        attrib = GL_SAMPLES_ARB;
hfutrell@2745
  2186
#else
slouken@2753
  2187
        attrib = GL_SAMPLES;
hfutrell@2745
  2188
#endif
slouken@2753
  2189
        break;
slouken@2753
  2190
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2191
        {
slouken@2753
  2192
            GLint bits = 0;
slouken@2753
  2193
            GLint component;
slouken@1936
  2194
slouken@2753
  2195
            /*
slouken@2753
  2196
             * there doesn't seem to be a single flag in OpenGL
slouken@2753
  2197
             * for this!
slouken@2753
  2198
             */
slouken@2753
  2199
            glGetIntegervFunc(GL_RED_BITS, &component);
slouken@2753
  2200
            bits += component;
slouken@2753
  2201
            glGetIntegervFunc(GL_GREEN_BITS, &component);
slouken@2753
  2202
            bits += component;
slouken@2753
  2203
            glGetIntegervFunc(GL_BLUE_BITS, &component);
slouken@2753
  2204
            bits += component;
slouken@2753
  2205
            glGetIntegervFunc(GL_ALPHA_BITS, &component);
slouken@2753
  2206
            bits += component;
slouken@1936
  2207
slouken@2753
  2208
            *value = bits;
slouken@2753
  2209
            return 0;
slouken@2753
  2210
        }
slouken@2753
  2211
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2212
        {
slouken@2753
  2213
            /* FIXME: How do we get this information? */
slouken@3571
  2214
            *value = (_this->gl_config.accelerated != 0);
slouken@2753
  2215
            return 0;
slouken@2753
  2216
        }
slouken@5210
  2217
    case SDL_GL_RETAINED_BACKING:
slouken@5210
  2218
        {
slouken@5210
  2219
            *value = _this->gl_config.retained_backing;
slouken@5210
  2220
            return 0;
slouken@5210
  2221
        }
slouken@5210
  2222
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@5210
  2223
        {
slouken@5210
  2224
            *value = _this->gl_config.major_version;
slouken@5210
  2225
            return 0;
slouken@5210
  2226
        }
slouken@5210
  2227
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@5210
  2228
        {
slouken@5210
  2229
            *value = _this->gl_config.minor_version;
slouken@5210
  2230
            return 0;
slouken@5210
  2231
        }
slouken@2753
  2232
    default:
slouken@2753
  2233
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2234
        return -1;
slouken@2753
  2235
    }
slouken@0
  2236
slouken@2753
  2237
    glGetIntegervFunc(attrib, (GLint *) value);
slouken@3139
  2238
    error = glGetErrorFunc();
slouken@3139
  2239
    if (error != GL_NO_ERROR) {
slouken@3139
  2240
        switch (error) {
slouken@3139
  2241
        case GL_INVALID_ENUM:
slouken@3139
  2242
            {
slouken@3139
  2243
                SDL_SetError("OpenGL error: GL_INVALID_ENUM");
slouken@3139
  2244
            }
slouken@3139
  2245
            break;
slouken@3139
  2246
        case GL_INVALID_VALUE:
slouken@3139
  2247
            {
slouken@3139
  2248
                SDL_SetError("OpenGL error: GL_INVALID_VALUE");
slouken@3139
  2249
            }
slouken@3139
  2250
            break;
slouken@3139
  2251
        default:
slouken@3139
  2252
            {
slouken@3139
  2253
                SDL_SetError("OpenGL error: %08X", error);
slouken@3139
  2254
            }
slouken@3139
  2255
            break;
slouken@3139
  2256
        }
slouken@3139
  2257
        return -1;
slouken@3099
  2258
    }
slouken@2753
  2259
    return 0;
slouken@1936
  2260
#else
slouken@2753
  2261
    SDL_Unsupported();
slouken@2753
  2262
    return -1;
slouken@2753
  2263
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2264
}
slouken@0
  2265
slouken@1912
  2266
SDL_GLContext
slouken@3685
  2267
SDL_GL_CreateContext(SDL_Window * window)
slouken@1912
  2268
{
slouken@3695
  2269
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  2270
slouken@2753
  2271
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2272
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2273
        return NULL;
slouken@2753
  2274
    }
slouken@2753
  2275
    return _this->GL_CreateContext(_this, window);
slouken@1912
  2276
}
slouken@1912
  2277
slouken@1912
  2278
int
slouken@3685
  2279
SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
slouken@1912
  2280
{
slouken@3695
  2281
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  2282
slouken@3695
  2283
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2284
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2285
        return -1;
slouken@2753
  2286
    }
slouken@2753
  2287
    if (!context) {
slouken@2753
  2288
        window = NULL;
slouken@2753
  2289
    }
slouken@2753
  2290
    return _this->GL_MakeCurrent(_this, window, context);
slouken@1912
  2291
}
slouken@1912
  2292
slouken@1912
  2293
int
slouken@1912
  2294
SDL_GL_SetSwapInterval(int interval)
slouken@1912
  2295
{
slouken@2753
  2296
    if (!_this) {
slouken@2753
  2297
        SDL_UninitializedVideo();
slouken@2753
  2298
        return -1;
slouken@2753
  2299
    }
slouken@2753
  2300
    if (_this->GL_SetSwapInterval) {
slouken@2753
  2301
        return _this->GL_SetSwapInterval(_this, interval);
slouken@2753
  2302
    } else {
slouken@2753
  2303
        SDL_SetError("Setting the swap interval is not supported");
slouken@2753
  2304
        return -1;
slouken@2753
  2305
    }
slouken@1912
  2306
}
slouken@1912
  2307
slouken@1912
  2308
int
slouken@1912
  2309
SDL_GL_GetSwapInterval(void)
slouken@1912
  2310
{
slouken@2753
  2311
    if (!_this) {
slouken@2753
  2312
        SDL_UninitializedVideo();
slouken@2753
  2313
        return -1;
slouken@2753
  2314
    }
slouken@2753
  2315
    if (_this->GL_GetSwapInterval) {
slouken@2753
  2316
        return _this->GL_GetSwapInterval(_this);
slouken@2753
  2317
    } else {
slouken@2753
  2318
        SDL_SetError("Getting the swap interval is not supported");
slouken@2753
  2319
        return -1;
slouken@2753
  2320
    }
slouken@1912
  2321
}
slouken@1912
  2322
slouken@1895
  2323
void
slouken@3685
  2324
SDL_GL_SwapWindow(SDL_Window * window)
slouken@0
  2325
{
slouken@3695
  2326
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  2327
slouken@2753
  2328
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2329
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2330
        return;
slouken@2753
  2331
    }
slouken@2753
  2332
    _this->GL_SwapWindow(_this, window);
slouken@1912
  2333
}
slouken@1912
  2334
slouken@1912
  2335
void
slouken@1912
  2336
SDL_GL_DeleteContext(SDL_GLContext context)
slouken@1912
  2337
{
slouken@4526
  2338
    if (!_this || !_this->gl_data || !context) {
slouken@2753
  2339
        return;
slouken@2753
  2340
    }
slouken@2753
  2341
    _this->GL_MakeCurrent(_this, NULL, NULL);
slouken@2753
  2342
    _this->GL_DeleteContext(_this, context);
slouken@0
  2343
}
slouken@0
  2344
slouken@1895
  2345
#if 0                           // FIXME
hfutrell@2744
  2346
/*
hfutrell@2744
  2347
 * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
hfutrell@2744
  2348
 * & 2 for alpha channel.
hfutrell@2744
  2349
 */
slouken@1895
  2350
static void
slouken@1895
  2351
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  2352
{
slouken@2753
  2353
    int x, y;
slouken@2753
  2354
    Uint32 colorkey;
slouken@0
  2355
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  2356
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  2357
slouken@2753
  2358
    colorkey = icon->format->colorkey;
slouken@2753
  2359
    switch (icon->format->BytesPerPixel) {
slouken@2753
  2360
    case 1:
slouken@2753
  2361
        {
slouken@2753
  2362
            Uint8 *pixels;
slouken@2753
  2363
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2364
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@2753
  2365
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2366
                    if (*pixels++ == colorkey) {
slouken@2753
  2367
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2368
                    }
slouken@2753
  2369
                }
slouken@2753
  2370
            }
slouken@2753
  2371
        }
slouken@2753
  2372
        break;
slouken@0
  2373
slouken@2753
  2374
    case 2:
slouken@2753
  2375
        {
slouken@2753
  2376
            Uint16 *pixels;
slouken@2753
  2377
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2378
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@2753
  2379
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2380
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  2381
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2382
                    } else if ((flags & 2)
slouken@2753
  2383
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  2384
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2385
                    }
slouken@2753
  2386
                    pixels++;
slouken@2753
  2387
                }
slouken@2753
  2388
            }
slouken@2753
  2389
        }
slouken@2753
  2390
        break;
slouken@0
  2391
slouken@2753
  2392
    case 4:
slouken@2753
  2393
        {
slouken@2753
  2394
            Uint32 *pixels;
slouken@2753
  2395
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2396
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@2753
  2397
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2398
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  2399
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2400
                    } else if ((flags & 2)
slouken@2753
  2401
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  2402
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2403
                    }
slouken@2753
  2404
                    pixels++;
slouken@2753
  2405
                }
slouken@2753
  2406
            }
slouken@2753
  2407
        }
slouken@2753
  2408
        break;
slouken@2753
  2409
    }
slouken@0
  2410
}
slouken@0
  2411
slouken@0
  2412
/*
slouken@0
  2413
 * Sets the window manager icon for the display window.
slouken@0
  2414
 */
slouken@1895
  2415
void
slouken@1895
  2416
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  2417
{
slouken@2753
  2418
    if (icon && _this->SetIcon) {
slouken@2753
  2419
        /* Generate a mask if necessary, and create the icon! */
slouken@2753
  2420
        if (mask == NULL) {
slouken@2753
  2421
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@2753
  2422
            int flags = 0;
slouken@2753
  2423
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@2753
  2424
            if (mask == NULL) {
slouken@2753
  2425
                return;
slouken@2753
  2426
            }
slouken@2753
  2427
            SDL_memset(mask, ~0, mask_len);
slouken@2753
  2428
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@2753
  2429
                flags |= 1;
slouken@2753
  2430
            if (icon->flags & SDL_SRCALPHA)
slouken@2753
  2431
                flags |= 2;
slouken@2753
  2432
            if (flags) {
slouken@2753
  2433
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@2753
  2434
            }
slouken@2753
  2435
            _this->SetIcon(_this, icon, mask);
slouken@2753
  2436
            SDL_free(mask);
slouken@2753
  2437
        } else {
slouken@2753
  2438
            _this->SetIcon(_this, icon, mask);
slouken@2753
  2439
        }
slouken@2753
  2440
    }
slouken@1895
  2441
}
slouken@1895
  2442
#endif
slouken@0
  2443
slouken@1895
  2444
SDL_bool
slouken@3685
  2445
SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
slouken@1895
  2446
{
slouken@3695
  2447
    CHECK_WINDOW_MAGIC(window, SDL_FALSE);
slouken@3695
  2448
slouken@4900
  2449
    if (!info) {
slouken@4900
  2450
        return SDL_FALSE;
slouken@4900
  2451
    }
slouken@4900
  2452
    info->subsystem = SDL_SYSWM_UNKNOWN;
slouken@4900
  2453
slouken@3695
  2454
    if (!_this->GetWindowWMInfo) {
slouken@2753
  2455
        return SDL_FALSE;
slouken@2753
  2456
    }
slouken@2753
  2457
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  2458
}
slouken@0
  2459
slouken@3280
  2460
void
slouken@3280
  2461
SDL_StartTextInput(void)
slouken@3280
  2462
{
slouken@3683
  2463
    if (_this && _this->StartTextInput) {
slouken@3280
  2464
        _this->StartTextInput(_this);
slouken@3280
  2465
    }
slouken@3280
  2466
    SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
slouken@3280
  2467
    SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
slouken@3280
  2468
}
slouken@3280
  2469
slouken@3280
  2470
void
slouken@3280
  2471
SDL_StopTextInput(void)
slouken@3280
  2472
{
slouken@3683
  2473
    if (_this && _this->StopTextInput) {
slouken@3280
  2474
        _this->StopTextInput(_this);
slouken@3280
  2475
    }
slouken@3280
  2476
    SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
slouken@3280
  2477
    SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
slouken@3280
  2478
}
slouken@3280
  2479
slouken@3280
  2480
void
slouken@3280
  2481
SDL_SetTextInputRect(SDL_Rect *rect)
slouken@3280
  2482
{
slouken@3683
  2483
    if (_this && _this->SetTextInputRect) {
slouken@3280
  2484
        _this->SetTextInputRect(_this, rect);
slouken@3280
  2485
    }
slouken@3280
  2486
}
slouken@3280
  2487
slouken@1895
  2488
/* vi: set ts=4 sw=4 expandtab: */