src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 23:07:14 -0800
changeset 5305 75f5ff92ad08
parent 5297 1800dc39b74c
child 5306 cf3cd833dcd3
permissions -rw-r--r--
Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
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@5249
  1028
                if (_this->PrepWindowFullscreen) {
slouken@5249
  1029
                    _this->PrepWindowFullscreen(_this, other);
slouken@5249
  1030
                }
slouken@3502
  1031
slouken@3502
  1032
                SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
slouken@5249
  1033
slouken@5249
  1034
                if (_this->SetWindowFullscreen) {
slouken@5305
  1035
                    _this->SetWindowFullscreen(_this, other, display, SDL_TRUE);
slouken@5249
  1036
                }
slouken@5249
  1037
                display->fullscreen_window = other;
slouken@5249
  1038
slouken@5249
  1039
                /* Generate a mode change events here */
slouken@5249
  1040
                SDL_SendWindowEvent(other, SDL_WINDOWEVENT_RESIZED,
slouken@5249
  1041
                                    fullscreen_mode.w, fullscreen_mode.h);
slouken@3502
  1042
                return;
slouken@3502
  1043
            }
slouken@3502
  1044
        }
slouken@3502
  1045
    }
slouken@3502
  1046
slouken@3502
  1047
    /* Nope, restore the desktop mode */
slouken@5249
  1048
    if (_this->PrepWindowFullscreen) {
slouken@5249
  1049
        _this->PrepWindowFullscreen(_this, window);
slouken@5249
  1050
    }
slouken@5249
  1051
slouken@3502
  1052
    SDL_SetDisplayModeForDisplay(display, NULL);
slouken@5249
  1053
slouken@5249
  1054
    if (_this->SetWindowFullscreen) {
slouken@5305
  1055
        _this->SetWindowFullscreen(_this, window, display, SDL_FALSE);
slouken@5249
  1056
    }
slouken@3517
  1057
    display->fullscreen_window = NULL;
slouken@3502
  1058
}
slouken@3502
  1059
slouken@3685
  1060
SDL_Window *
slouken@1895
  1061
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@0
  1062
{
slouken@2753
  1063
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@2753
  1064
                                  SDL_WINDOW_OPENGL |
slouken@2753
  1065
                                  SDL_WINDOW_BORDERLESS |
slouken@2875
  1066
                                  SDL_WINDOW_RESIZABLE |
slouken@2875
  1067
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@3685
  1068
    SDL_Window *window;
slouken@0
  1069
slouken@2753
  1070
    if (!_this) {
slouken@3417
  1071
        /* Initialize the video system if needed */
slouken@5123
  1072
        if (SDL_VideoInit(NULL) < 0) {
slouken@3685
  1073
            return NULL;
slouken@3417
  1074
        }
slouken@2753
  1075
    }
slouken@5249
  1076
slouken@5249
  1077
    /* Some platforms have OpenGL enabled by default */
slouken@5249
  1078
#if (SDL_VIDEO_OPENGL && __MACOSX__) || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@5249
  1079
    flags |= SDL_WINDOW_OPENGL;
slouken@5249
  1080
#endif
slouken@3057
  1081
    if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1082
        if (!_this->GL_CreateContext) {
slouken@3057
  1083
            SDL_SetError("No OpenGL support in video driver");
slouken@3685
  1084
            return NULL;
slouken@3057
  1085
        }
slouken@3057
  1086
        SDL_GL_LoadLibrary(NULL);
slouken@2753
  1087
    }
slouken@3685
  1088
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3695
  1089
    window->magic = &_this->window_magic;
slouken@3685
  1090
    window->id = _this->next_object_id++;
slouken@3685
  1091
    window->x = x;
slouken@3685
  1092
    window->y = y;
slouken@3685
  1093
    window->w = w;
slouken@3685
  1094
    window->h = h;
slouken@3685
  1095
    window->flags = (flags & allowed_flags);
slouken@5246
  1096
    window->next = _this->windows;
slouken@5246
  1097
    if (_this->windows) {
slouken@5246
  1098
        _this->windows->prev = window;
slouken@3693
  1099
    }
slouken@5246
  1100
    _this->windows = window;
slouken@3685
  1101
slouken@3685
  1102
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@3685
  1103
        SDL_DestroyWindow(window);
slouken@3685
  1104
        return NULL;
slouken@3685
  1105
    }
slouken@0
  1106
slouken@2753
  1107
    if (title) {
slouken@3685
  1108
        SDL_SetWindowTitle(window, title);
slouken@2753
  1109
    }
slouken@2753
  1110
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@3685
  1111
        SDL_MaximizeWindow(window);
slouken@2753
  1112
    }
slouken@2753
  1113
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@3685
  1114
        SDL_MinimizeWindow(window);
slouken@2753
  1115
    }
slouken@2753
  1116
    if (flags & SDL_WINDOW_SHOWN) {
slouken@3685
  1117
        SDL_ShowWindow(window);
slouken@3685
  1118
    }
slouken@3685
  1119
    SDL_UpdateWindowGrab(window);
slouken@3685
  1120
slouken@3685
  1121
    return window;
slouken@1895
  1122
}
slouken@0
  1123
slouken@3685
  1124
SDL_Window *
slouken@1895
  1125
SDL_CreateWindowFrom(const void *data)
slouken@1895
  1126
{
slouken@3685
  1127
    SDL_Window *window;
slouken@0
  1128
slouken@2753
  1129
    if (!_this) {
slouken@2753
  1130
        SDL_UninitializedVideo();
slouken@3685
  1131
        return NULL;
slouken@3685
  1132
    }
slouken@3685
  1133
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3695
  1134
    window->magic = &_this->window_magic;
slouken@3685
  1135
    window->id = _this->next_object_id++;
slouken@3685
  1136
    window->flags = SDL_WINDOW_FOREIGN;
slouken@5246
  1137
    window->next = _this->windows;
slouken@5246
  1138
    if (_this->windows) {
slouken@5246
  1139
        _this->windows->prev = window;
slouken@3693
  1140
    }
slouken@5246
  1141
    _this->windows = window;
slouken@1895
  1142
slouken@2753
  1143
    if (!_this->CreateWindowFrom ||
slouken@3685
  1144
        _this->CreateWindowFrom(_this, window, data) < 0) {
slouken@3685
  1145
        SDL_DestroyWindow(window);
slouken@3685
  1146
        return NULL;
slouken@3685
  1147
    }
slouken@3685
  1148
    return window;
slouken@1895
  1149
}
slouken@1895
  1150
slouken@1924
  1151
int
slouken@1928
  1152
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
  1153
{
slouken@3057
  1154
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@3057
  1155
                                  SDL_WINDOW_OPENGL |
slouken@3057
  1156
                                  SDL_WINDOW_BORDERLESS |
slouken@3057
  1157
                                  SDL_WINDOW_RESIZABLE |
slouken@3486
  1158
                                  SDL_WINDOW_INPUT_GRABBED |
slouken@3486
  1159
                                  SDL_WINDOW_FOREIGN);
slouken@2753
  1160
    char *title = window->title;
slouken@1956
  1161
slouken@2753
  1162
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
slouken@2753
  1163
        SDL_SetError("No OpenGL support in video driver");
slouken@2753
  1164
        return -1;
slouken@2753
  1165
    }
slouken@3057
  1166
slouken@3057
  1167
    if (window->flags & SDL_WINDOW_FOREIGN) {
slouken@3057
  1168
        /* Can't destroy and re-create foreign windows, hrm */
slouken@3057
  1169
        flags |= SDL_WINDOW_FOREIGN;
slouken@3057
  1170
    } else {
slouken@3057
  1171
        flags &= ~SDL_WINDOW_FOREIGN;
slouken@3057
  1172
    }
slouken@3057
  1173
slouken@5249
  1174
    /* Restore video mode, etc. */
slouken@5305
  1175
    SDL_HideWindow(window);
slouken@5249
  1176
slouken@5249
  1177
    /* Tear down the old native window */
slouken@5249
  1178
    if (window->surface) {
slouken@5288
  1179
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5249
  1180
        SDL_FreeSurface(window->surface);
slouken@5249
  1181
    }
slouken@5249
  1182
    if (_this->DestroyWindowFramebuffer) {
slouken@5249
  1183
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5249
  1184
    }
slouken@3057
  1185
    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@2753
  1186
        _this->DestroyWindow(_this, window);
slouken@2753
  1187
    }
slouken@3057
  1188
slouken@5249
  1189
    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
slouken@5249
  1190
        if (flags & SDL_WINDOW_OPENGL) {
slouken@5249
  1191
            SDL_GL_LoadLibrary(NULL);
slouken@5249
  1192
        } else {
slouken@5249
  1193
            SDL_GL_UnloadLibrary();
slouken@5249
  1194
        }
slouken@5249
  1195
    }
slouken@5249
  1196
slouken@2753
  1197
    window->title = NULL;
slouken@3057
  1198
    window->flags = (flags & allowed_flags);
slouken@1956
  1199
slouken@3057
  1200
    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@3057
  1201
        if (_this->CreateWindow(_this, window) < 0) {
slouken@3057
  1202
            if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1203
                SDL_GL_UnloadLibrary();
slouken@3057
  1204
            }
slouken@3057
  1205
            return -1;
slouken@3057
  1206
        }
slouken@2753
  1207
    }
slouken@3057
  1208
slouken@2753
  1209
    if (title) {
slouken@3685
  1210
        SDL_SetWindowTitle(window, title);
slouken@2753
  1211
        SDL_free(title);
slouken@2753
  1212
    }
slouken@2753
  1213
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@3685
  1214
        SDL_MaximizeWindow(window);
slouken@2753
  1215
    }
slouken@2753
  1216
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@3685
  1217
        SDL_MinimizeWindow(window);
slouken@2753
  1218
    }
slouken@2753
  1219
    if (flags & SDL_WINDOW_SHOWN) {
slouken@3685
  1220
        SDL_ShowWindow(window);
slouken@2753
  1221
    }
slouken@2875
  1222
    SDL_UpdateWindowGrab(window);
slouken@2875
  1223
slouken@2753
  1224
    return 0;
slouken@1924
  1225
}
slouken@1924
  1226
slouken@1895
  1227
Uint32
slouken@3685
  1228
SDL_GetWindowID(SDL_Window * window)
slouken@1895
  1229
{
slouken@3695
  1230
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1231
slouken@3685
  1232
    return window->id;
slouken@3685
  1233
}
slouken@3685
  1234
slouken@3685
  1235
SDL_Window *
slouken@3685
  1236
SDL_GetWindowFromID(Uint32 id)
slouken@3685
  1237
{
slouken@3685
  1238
    SDL_Window *window;
slouken@3685
  1239
slouken@3685
  1240
    if (!_this) {
slouken@3685
  1241
        return NULL;
slouken@3685
  1242
    }
slouken@5246
  1243
    for (window = _this->windows; window; window = window->next) {
slouken@5246
  1244
        if (window->id == id) {
slouken@5246
  1245
            return window;
slouken@3685
  1246
        }
slouken@3685
  1247
    }
slouken@3685
  1248
    return NULL;
slouken@3685
  1249
}
slouken@3685
  1250
slouken@3685
  1251
Uint32
slouken@3685
  1252
SDL_GetWindowFlags(SDL_Window * window)
slouken@3685
  1253
{
slouken@3695
  1254
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1255
slouken@2753
  1256
    return window->flags;
slouken@1895
  1257
}
slouken@1895
  1258
slouken@1895
  1259
void
slouken@3685
  1260
SDL_SetWindowTitle(SDL_Window * window, const char *title)
slouken@1895
  1261
{
slouken@3695
  1262
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1263
slouken@3695
  1264
    if (title == window->title) {
slouken@2753
  1265
        return;
slouken@2753
  1266
    }
slouken@2753
  1267
    if (window->title) {
slouken@2753
  1268
        SDL_free(window->title);
slouken@2753
  1269
    }
slouken@4871
  1270
    if (title && *title) {
slouken@2753
  1271
        window->title = SDL_strdup(title);
slouken@2753
  1272
    } else {
slouken@2753
  1273
        window->title = NULL;
slouken@2753
  1274
    }
slouken@1956
  1275
slouken@2753
  1276
    if (_this->SetWindowTitle) {
slouken@2753
  1277
        _this->SetWindowTitle(_this, window);
slouken@2753
  1278
    }
slouken@1895
  1279
}
slouken@1895
  1280
slouken@2753
  1281
const char *
slouken@3685
  1282
SDL_GetWindowTitle(SDL_Window * window)
slouken@1895
  1283
{
slouken@4872
  1284
    CHECK_WINDOW_MAGIC(window, "");
slouken@3695
  1285
slouken@4871
  1286
    return window->title ? window->title : "";
slouken@1895
  1287
}
slouken@1895
  1288
slouken@1895
  1289
void
slouken@3685
  1290
SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
slouken@2967
  1291
{
slouken@3695
  1292
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1293
slouken@2967
  1294
    if (_this->SetWindowIcon) {
slouken@2967
  1295
        _this->SetWindowIcon(_this, window, icon);
slouken@2967
  1296
    }
slouken@2967
  1297
}
slouken@2967
  1298
slouken@5165
  1299
void*
slouken@5165
  1300
SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
slouken@1895
  1301
{
slouken@5165
  1302
    SDL_WindowUserData *prev, *data;
slouken@3695
  1303
slouken@5165
  1304
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5165
  1305
slouken@5165
  1306
    /* See if the named data already exists */
slouken@5165
  1307
    prev = NULL;
slouken@5165
  1308
    for (data = window->data; data; prev = data, data = data->next) {
slouken@5165
  1309
        if (SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1310
            void *last_value = data->data;
slouken@5165
  1311
slouken@5165
  1312
            if (userdata) {
slouken@5165
  1313
                /* Set the new value */
slouken@5165
  1314
                data->data = userdata;
slouken@5165
  1315
            } else {
slouken@5165
  1316
                /* Delete this value */
slouken@5165
  1317
                if (prev) {
slouken@5165
  1318
                    prev->next = data->next;
slouken@5165
  1319
                } else {
slouken@5165
  1320
                    window->data = data->next;
slouken@5165
  1321
                }
slouken@5165
  1322
                SDL_free(data->name);
slouken@5165
  1323
                SDL_free(data);
slouken@5165
  1324
            }
slouken@5165
  1325
            return last_value;
slouken@5165
  1326
        }
slouken@5165
  1327
    }
slouken@5165
  1328
slouken@5165
  1329
    /* Add new data to the window */
slouken@5165
  1330
    if (userdata) {
slouken@5165
  1331
        data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
slouken@5165
  1332
        data->name = SDL_strdup(name);
slouken@5165
  1333
        data->data = userdata;
slouken@5165
  1334
        data->next = window->data;
slouken@5165
  1335
        window->data = data;
slouken@5165
  1336
    }
slouken@5165
  1337
    return NULL;
slouken@1895
  1338
}
slouken@1895
  1339
slouken@2753
  1340
void *
slouken@5165
  1341
SDL_GetWindowData(SDL_Window * window, const char *name)
slouken@1895
  1342
{
slouken@5165
  1343
    SDL_WindowUserData *data;
slouken@5165
  1344
slouken@3695
  1345
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  1346
slouken@5165
  1347
    for (data = window->data; data; data = data->next) {
slouken@5165
  1348
        if (SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1349
            return data->data;
slouken@5165
  1350
        }
slouken@5165
  1351
    }
slouken@5165
  1352
    return NULL;
slouken@1895
  1353
}
slouken@1895
  1354
slouken@1895
  1355
void
slouken@3685
  1356
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1357
{
slouken@3695
  1358
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1359
slouken@5305
  1360
    if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
slouken@2753
  1361
        window->x = x;
slouken@2753
  1362
    }
slouken@5305
  1363
    if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
slouken@2753
  1364
        window->y = y;
slouken@2753
  1365
    }
slouken@5246
  1366
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5246
  1367
        if (_this->SetWindowPosition) {
slouken@5246
  1368
            _this->SetWindowPosition(_this, window);
slouken@5246
  1369
        }
slouken@5246
  1370
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@2753
  1371
    }
slouken@1895
  1372
}
slouken@1895
  1373
slouken@1895
  1374
void
slouken@3685
  1375
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1376
{
slouken@5246
  1377
    /* Clear the values */
slouken@5246
  1378
    if (x) {
slouken@5246
  1379
        *x = 0;
slouken@5246
  1380
    }
slouken@5246
  1381
    if (y) {
slouken@5246
  1382
        *y = 0;
slouken@5246
  1383
    }
slouken@5246
  1384
slouken@5246
  1385
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1386
slouken@5246
  1387
    /* Fullscreen windows are always at their display's origin */
slouken@5246
  1388
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5246
  1389
    } else {
slouken@5242
  1390
        if (x) {
slouken@5242
  1391
            *x = window->x;
slouken@5242
  1392
        }
slouken@5242
  1393
        if (y) {
slouken@5242
  1394
            *y = window->y;
slouken@5242
  1395
        }
slouken@2753
  1396
    }
slouken@1895
  1397
}
slouken@1895
  1398
slouken@1895
  1399
void
slouken@3685
  1400
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1401
{
slouken@3695
  1402
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1403
slouken@5249
  1404
    /* FIXME: Should this change fullscreen modes? */
slouken@5249
  1405
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5276
  1406
        window->w = w;
slouken@5276
  1407
        window->h = h;
slouken@5249
  1408
        if (_this->SetWindowSize) {
slouken@5249
  1409
            _this->SetWindowSize(_this, window);
slouken@5249
  1410
        }
slouken@5276
  1411
        if (window->w == w && window->h == h) {
slouken@5276
  1412
            /* We didn't get a SDL_WINDOWEVENT_RESIZED event (by design) */
slouken@5276
  1413
            SDL_OnWindowResized(window);
slouken@5276
  1414
        }
slouken@2753
  1415
    }
slouken@1895
  1416
}
slouken@1895
  1417
slouken@1895
  1418
void
slouken@3685
  1419
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1420
{
slouken@5246
  1421
    int dummy;
slouken@5246
  1422
slouken@5246
  1423
    if (!w) {
slouken@5246
  1424
        w = &dummy;
slouken@5246
  1425
    }
slouken@5246
  1426
    if (!h) {
slouken@5246
  1427
        h = &dummy;
slouken@5246
  1428
    }
slouken@5246
  1429
slouken@5246
  1430
    *w = 0;
slouken@5246
  1431
    *h = 0;
slouken@5246
  1432
slouken@5246
  1433
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1434
slouken@5242
  1435
    if (_this && window && window->magic == &_this->window_magic) {
slouken@2849
  1436
        if (w) {
slouken@2849
  1437
            *w = window->w;
slouken@2849
  1438
        }
slouken@2849
  1439
        if (h) {
slouken@2849
  1440
            *h = window->h;
slouken@2849
  1441
        }
slouken@2849
  1442
    } else {
slouken@2849
  1443
        if (w) {
slouken@2849
  1444
            *w = 0;
slouken@2849
  1445
        }
slouken@2849
  1446
        if (h) {
slouken@2849
  1447
            *h = 0;
slouken@2849
  1448
        }
slouken@2753
  1449
    }
slouken@1895
  1450
}
slouken@1895
  1451
slouken@1895
  1452
void
slouken@3685
  1453
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1454
{
slouken@3695
  1455
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1456
slouken@3695
  1457
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@2753
  1458
        return;
slouken@2753
  1459
    }
slouken@1895
  1460
slouken@2753
  1461
    if (_this->ShowWindow) {
slouken@2753
  1462
        _this->ShowWindow(_this, window);
slouken@2753
  1463
    }
slouken@3685
  1464
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1465
}
slouken@1895
  1466
slouken@1895
  1467
void
slouken@3685
  1468
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1469
{
slouken@3695
  1470
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1471
slouken@3695
  1472
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1473
        return;
slouken@2753
  1474
    }
slouken@1895
  1475
slouken@2753
  1476
    if (_this->HideWindow) {
slouken@2753
  1477
        _this->HideWindow(_this, window);
slouken@2753
  1478
    }
slouken@3685
  1479
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1480
}
slouken@1895
  1481
slouken@1895
  1482
void
slouken@3685
  1483
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1484
{
slouken@3695
  1485
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1486
slouken@3695
  1487
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1488
        return;
slouken@2753
  1489
    }
slouken@2753
  1490
    if (_this->RaiseWindow) {
slouken@2753
  1491
        _this->RaiseWindow(_this, window);
slouken@3502
  1492
    } else {
slouken@3502
  1493
        /* FIXME: What we really want is a way to request focus */
slouken@3685
  1494
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@2753
  1495
    }
slouken@1895
  1496
}
slouken@1895
  1497
slouken@1895
  1498
void
slouken@3685
  1499
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1500
{
slouken@3695
  1501
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1502
slouken@3695
  1503
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
  1504
        return;
slouken@2753
  1505
    }
slouken@1895
  1506
slouken@2753
  1507
    if (_this->MaximizeWindow) {
slouken@2753
  1508
        _this->MaximizeWindow(_this, window);
slouken@2753
  1509
    }
slouken@3685
  1510
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
  1511
}
slouken@1895
  1512
slouken@1895
  1513
void
slouken@3685
  1514
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1515
{
slouken@3695
  1516
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1517
slouken@3695
  1518
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
  1519
        return;
slouken@2753
  1520
    }
slouken@1895
  1521
slouken@2753
  1522
    if (_this->MinimizeWindow) {
slouken@2753
  1523
        _this->MinimizeWindow(_this, window);
slouken@2753
  1524
    }
slouken@3685
  1525
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
  1526
}
slouken@1895
  1527
slouken@1895
  1528
void
slouken@3685
  1529
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1530
{
slouken@3695
  1531
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1532
slouken@3695
  1533
    if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1534
        return;
slouken@2753
  1535
    }
slouken@1895
  1536
slouken@2753
  1537
    if (_this->RestoreWindow) {
slouken@2753
  1538
        _this->RestoreWindow(_this, window);
slouken@2753
  1539
    }
slouken@3685
  1540
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
  1541
}
slouken@1895
  1542
slouken@1895
  1543
int
slouken@5246
  1544
SDL_SetWindowFullscreen(SDL_Window * window, SDL_bool fullscreen)
slouken@1895
  1545
{
slouken@3695
  1546
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1547
slouken@2753
  1548
    if (fullscreen) {
slouken@2753
  1549
        fullscreen = SDL_WINDOW_FULLSCREEN;
slouken@2753
  1550
    }
slouken@2753
  1551
    if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
slouken@2753
  1552
        return 0;
slouken@2753
  1553
    }
slouken@2753
  1554
    if (fullscreen) {
slouken@2753
  1555
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@2753
  1556
    } else {
slouken@2753
  1557
        window->flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@5305
  1558
    }
slouken@5305
  1559
    SDL_UpdateFullscreenMode(window);
slouken@1895
  1560
slouken@2753
  1561
    return 0;
slouken@1895
  1562
}
slouken@1895
  1563
slouken@5166
  1564
static SDL_Surface *
slouken@5166
  1565
SDL_CreateWindowFramebuffer(SDL_Window * window)
slouken@5166
  1566
{
slouken@5166
  1567
    Uint32 format;
slouken@5166
  1568
    void *pixels;
slouken@5166
  1569
    int pitch;
slouken@5166
  1570
    int bpp;
slouken@5166
  1571
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5166
  1572
slouken@5166
  1573
    if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
slouken@5166
  1574
        return NULL;
slouken@5166
  1575
    }
slouken@5166
  1576
slouken@5166
  1577
    if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
slouken@5166
  1578
        return NULL;
slouken@5166
  1579
    }
slouken@5166
  1580
slouken@5166
  1581
    if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5166
  1582
        return NULL;
slouken@5166
  1583
    }
slouken@5166
  1584
slouken@5166
  1585
    return SDL_CreateRGBSurfaceFrom(pixels, window->w, window->h, bpp, pitch, Rmask, Gmask, Bmask, Amask);
slouken@5166
  1586
}
slouken@5166
  1587
slouken@5166
  1588
SDL_Surface *
slouken@5166
  1589
SDL_GetWindowSurface(SDL_Window * window)
slouken@5166
  1590
{
slouken@5166
  1591
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5166
  1592
slouken@5172
  1593
    if (!window->surface_valid) {
slouken@5172
  1594
        if (window->surface) {
slouken@5288
  1595
            window->surface->flags &= ~SDL_DONTFREE;
slouken@5172
  1596
            SDL_FreeSurface(window->surface);
slouken@5172
  1597
        }
slouken@5166
  1598
        window->surface = SDL_CreateWindowFramebuffer(window);
slouken@5169
  1599
        if (window->surface) {
slouken@5172
  1600
            window->surface_valid = SDL_TRUE;
slouken@5288
  1601
            window->surface->flags |= SDL_DONTFREE;
slouken@5169
  1602
        }
slouken@5166
  1603
    }
slouken@5166
  1604
    return window->surface;
slouken@5166
  1605
}
slouken@5166
  1606
slouken@5166
  1607
int
slouken@5166
  1608
SDL_UpdateWindowSurface(SDL_Window * window)
slouken@5166
  1609
{
slouken@5166
  1610
    SDL_Rect full_rect;
slouken@5166
  1611
slouken@5166
  1612
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1613
slouken@5166
  1614
    full_rect.x = 0;
slouken@5166
  1615
    full_rect.y = 0;
slouken@5166
  1616
    full_rect.w = window->w;
slouken@5166
  1617
    full_rect.h = window->h;
slouken@5297
  1618
    return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
slouken@5166
  1619
}
slouken@5166
  1620
slouken@5166
  1621
int
slouken@5297
  1622
SDL_UpdateWindowSurfaceRects(SDL_Window * window, SDL_Rect * rects,
slouken@5297
  1623
                             int numrects)
slouken@5166
  1624
{
slouken@5166
  1625
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1626
slouken@5172
  1627
    if (!window->surface_valid) {
slouken@5166
  1628
        SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
slouken@5166
  1629
        return -1;
slouken@5166
  1630
    }
slouken@5166
  1631
slouken@5297
  1632
    return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
slouken@5166
  1633
}
slouken@5166
  1634
slouken@1895
  1635
void
slouken@3685
  1636
SDL_SetWindowGrab(SDL_Window * window, int mode)
slouken@1895
  1637
{
slouken@3695
  1638
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1639
slouken@3695
  1640
    if ((!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@2753
  1641
        return;
slouken@2753
  1642
    }
slouken@2753
  1643
    if (mode) {
slouken@2753
  1644
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1645
    } else {
slouken@2753
  1646
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1647
    }
slouken@2875
  1648
    SDL_UpdateWindowGrab(window);
slouken@2875
  1649
}
slouken@1895
  1650
slouken@2875
  1651
static void
slouken@2876
  1652
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  1653
{
slouken@2753
  1654
    if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
slouken@2753
  1655
        _this->SetWindowGrab(_this, window);
slouken@2753
  1656
    }
slouken@1895
  1657
}
slouken@1895
  1658
slouken@1895
  1659
int
slouken@3685
  1660
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  1661
{
slouken@3695
  1662
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1663
slouken@2753
  1664
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  1665
}
slouken@1895
  1666
slouken@1895
  1667
void
slouken@1895
  1668
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  1669
{
slouken@3685
  1670
    SDL_RaiseWindow(window);
slouken@5305
  1671
    SDL_UpdateFullscreenMode(window);
slouken@1895
  1672
}
slouken@1895
  1673
slouken@1895
  1674
void
slouken@1895
  1675
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  1676
{
slouken@5305
  1677
    SDL_UpdateFullscreenMode(window);
slouken@1895
  1678
}
slouken@1895
  1679
slouken@1895
  1680
void
slouken@5166
  1681
SDL_OnWindowResized(SDL_Window * window)
slouken@5166
  1682
{
slouken@5172
  1683
    window->surface_valid = SDL_FALSE;
slouken@5276
  1684
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
slouken@5166
  1685
}
slouken@5166
  1686
slouken@5166
  1687
void
slouken@3502
  1688
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  1689
{
slouken@5305
  1690
    SDL_UpdateFullscreenMode(window);
slouken@3502
  1691
}
slouken@3502
  1692
slouken@3502
  1693
void
slouken@3502
  1694
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  1695
{
slouken@3685
  1696
    SDL_RaiseWindow(window);
slouken@5305
  1697
    SDL_UpdateFullscreenMode(window);
slouken@3502
  1698
}
slouken@3502
  1699
slouken@3502
  1700
void
slouken@1895
  1701
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  1702
{
slouken@2876
  1703
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1704
        && _this->SetWindowGrab) {
slouken@2753
  1705
        _this->SetWindowGrab(_this, window);
slouken@2753
  1706
    }
slouken@1895
  1707
}
slouken@1895
  1708
slouken@1895
  1709
void
slouken@1895
  1710
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  1711
{
slouken@3512
  1712
    /* If we're fullscreen on a single-head system and lose focus, minimize */
slouken@3512
  1713
    if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
slouken@3512
  1714
        _this->num_displays == 1) {
slouken@3685
  1715
        SDL_MinimizeWindow(window);
slouken@3512
  1716
    }
slouken@3512
  1717
slouken@2876
  1718
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1719
        && _this->SetWindowGrab) {
slouken@2753
  1720
        _this->SetWindowGrab(_this, window);
slouken@2753
  1721
    }
slouken@1895
  1722
}
slouken@1895
  1723
slouken@3685
  1724
SDL_Window *
slouken@1895
  1725
SDL_GetFocusWindow(void)
slouken@1895
  1726
{
slouken@3685
  1727
    SDL_Window *window;
slouken@1895
  1728
slouken@2753
  1729
    if (!_this) {
slouken@3685
  1730
        return NULL;
slouken@3685
  1731
    }
slouken@5246
  1732
    for (window = _this->windows; window; window = window->next) {
slouken@2753
  1733
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  1734
            return window;
slouken@2753
  1735
        }
slouken@2753
  1736
    }
slouken@3685
  1737
    return NULL;
slouken@1895
  1738
}
slouken@1895
  1739
slouken@1895
  1740
void
slouken@3685
  1741
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  1742
{
slouken@3685
  1743
    SDL_VideoDisplay *display;
slouken@3685
  1744
slouken@3695
  1745
    CHECK_WINDOW_MAGIC(window, );
slouken@3503
  1746
slouken@3685
  1747
    /* Restore video mode, etc. */
slouken@5305
  1748
    SDL_HideWindow(window);
slouken@3685
  1749
slouken@5169
  1750
    if (window->surface) {
slouken@5288
  1751
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5169
  1752
        SDL_FreeSurface(window->surface);
slouken@5169
  1753
    }
slouken@5166
  1754
    if (_this->DestroyWindowFramebuffer) {
slouken@5166
  1755
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5166
  1756
    }
slouken@3685
  1757
    if (_this->DestroyWindow) {
slouken@3685
  1758
        _this->DestroyWindow(_this, window);
slouken@3685
  1759
    }
slouken@3685
  1760
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  1761
        SDL_GL_UnloadLibrary();
slouken@3685
  1762
    }
slouken@3685
  1763
slouken@5246
  1764
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  1765
    if (display->fullscreen_window == window) {
slouken@5246
  1766
        display->fullscreen_window = NULL;
slouken@5246
  1767
    }
slouken@5246
  1768
slouken@4901
  1769
    /* Now invalidate magic */
slouken@4897
  1770
    window->magic = NULL;
slouken@4897
  1771
slouken@5165
  1772
    /* Free memory associated with the window */
slouken@5165
  1773
    if (window->title) {
slouken@5165
  1774
        SDL_free(window->title);
slouken@5165
  1775
    }
slouken@5165
  1776
    while (window->data) {
slouken@5165
  1777
        SDL_WindowUserData *data = window->data;
slouken@5165
  1778
slouken@5165
  1779
        window->data = data->next;
slouken@5165
  1780
        SDL_free(data->name);
slouken@5165
  1781
        SDL_free(data);
slouken@5165
  1782
    }
slouken@5165
  1783
slouken@3685
  1784
    /* Unlink the window from the list */
slouken@3693
  1785
    if (window->next) {
slouken@3693
  1786
        window->next->prev = window->prev;
slouken@3693
  1787
    }
slouken@3685
  1788
    if (window->prev) {
slouken@3685
  1789
        window->prev->next = window->next;
slouken@3685
  1790
    } else {
slouken@5246
  1791
        _this->windows = window->next;
slouken@3685
  1792
    }
slouken@3685
  1793
slouken@3685
  1794
    SDL_free(window);
slouken@1895
  1795
}
slouken@1895
  1796
slouken@3025
  1797
SDL_bool
slouken@3025
  1798
SDL_IsScreenSaverEnabled()
slouken@3025
  1799
{
slouken@3025
  1800
    if (!_this) {
slouken@3025
  1801
        return SDL_TRUE;
slouken@3025
  1802
    }
slouken@3025
  1803
    return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
slouken@3025
  1804
}
slouken@3025
  1805
slouken@3025
  1806
void
slouken@3025
  1807
SDL_EnableScreenSaver()
slouken@3025
  1808
{
slouken@3025
  1809
    if (!_this) {
slouken@3025
  1810
        return;
slouken@3025
  1811
    }
slouken@3025
  1812
    if (!_this->suspend_screensaver) {
slouken@3025
  1813
        return;
slouken@3025
  1814
    }
slouken@3025
  1815
    _this->suspend_screensaver = SDL_FALSE;
slouken@3025
  1816
    if (_this->SuspendScreenSaver) {
slouken@3025
  1817
        _this->SuspendScreenSaver(_this);
slouken@3025
  1818
    }
slouken@3025
  1819
}
slouken@3025
  1820
slouken@3025
  1821
void
slouken@3025
  1822
SDL_DisableScreenSaver()
slouken@3025
  1823
{
slouken@3025
  1824
    if (!_this) {
slouken@3025
  1825
        return;
slouken@3025
  1826
    }
slouken@3025
  1827
    if (_this->suspend_screensaver) {
slouken@3025
  1828
        return;
slouken@3025
  1829
    }
slouken@3025
  1830
    _this->suspend_screensaver = SDL_TRUE;
slouken@3025
  1831
    if (_this->SuspendScreenSaver) {
slouken@3025
  1832
        _this->SuspendScreenSaver(_this);
slouken@3025
  1833
    }
slouken@3025
  1834
}
slouken@3025
  1835
slouken@1895
  1836
void
slouken@1895
  1837
SDL_VideoQuit(void)
slouken@1895
  1838
{
slouken@2753
  1839
    int i, j;
slouken@1895
  1840
slouken@2753
  1841
    if (!_this) {
slouken@2753
  1842
        return;
slouken@2753
  1843
    }
slouken@5123
  1844
slouken@2753
  1845
    /* Halt event processing before doing anything else */
slouken@5123
  1846
    SDL_QuitQuit();
slouken@5123
  1847
    SDL_MouseQuit();
slouken@5123
  1848
    SDL_KeyboardQuit();
slouken@2753
  1849
    SDL_StopEventLoop();
slouken@5123
  1850
slouken@3029
  1851
    SDL_EnableScreenSaver();
slouken@1895
  1852
slouken@2753
  1853
    /* Clean up the system video */
slouken@5246
  1854
    while (_this->windows) {
slouken@5246
  1855
        SDL_DestroyWindow(_this->windows);
slouken@2753
  1856
    }
slouken@2753
  1857
    _this->VideoQuit(_this);
slouken@1895
  1858
slouken@2753
  1859
    for (i = _this->num_displays; i--;) {
slouken@2753
  1860
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  1861
        for (j = display->num_display_modes; j--;) {
slouken@2753
  1862
            if (display->display_modes[j].driverdata) {
slouken@2753
  1863
                SDL_free(display->display_modes[j].driverdata);
slouken@2753
  1864
                display->display_modes[j].driverdata = NULL;
slouken@2753
  1865
            }
slouken@2753
  1866
        }
slouken@2753
  1867
        if (display->display_modes) {
slouken@2753
  1868
            SDL_free(display->display_modes);
slouken@2753
  1869
            display->display_modes = NULL;
slouken@2753
  1870
        }
slouken@2753
  1871
        if (display->desktop_mode.driverdata) {
slouken@2753
  1872
            SDL_free(display->desktop_mode.driverdata);
slouken@2753
  1873
            display->desktop_mode.driverdata = NULL;
slouken@2753
  1874
        }
slouken@2753
  1875
        if (display->driverdata) {
slouken@2753
  1876
            SDL_free(display->driverdata);
slouken@2753
  1877
            display->driverdata = NULL;
slouken@2753
  1878
        }
slouken@2753
  1879
    }
slouken@2753
  1880
    if (_this->displays) {
slouken@2753
  1881
        SDL_free(_this->displays);
slouken@2753
  1882
        _this->displays = NULL;
slouken@2753
  1883
    }
slouken@4495
  1884
    if (_this->clipboard_text) {
slouken@4495
  1885
        SDL_free(_this->clipboard_text);
slouken@4495
  1886
        _this->clipboard_text = NULL;
slouken@4495
  1887
    }
slouken@2753
  1888
    _this->free(_this);
slouken@2753
  1889
    _this = NULL;
slouken@0
  1890
}
slouken@0
  1891
slouken@1895
  1892
int
slouken@1895
  1893
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1894
{
slouken@2753
  1895
    int retval;
slouken@0
  1896
slouken@2753
  1897
    if (!_this) {
slouken@2753
  1898
        SDL_UninitializedVideo();
slouken@2753
  1899
        return -1;
slouken@2753
  1900
    }
slouken@3057
  1901
    if (_this->gl_config.driver_loaded) {
slouken@3057
  1902
        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
slouken@3057
  1903
            SDL_SetError("OpenGL library already loaded");
slouken@3057
  1904
            return -1;
slouken@3057
  1905
        }
slouken@3057
  1906
        retval = 0;
slouken@3057
  1907
    } else {
slouken@3057
  1908
        if (!_this->GL_LoadLibrary) {
slouken@3057
  1909
            SDL_SetError("No dynamic GL support in video driver");
slouken@3057
  1910
            return -1;
slouken@3057
  1911
        }
slouken@2753
  1912
        retval = _this->GL_LoadLibrary(_this, path);
slouken@3057
  1913
    }
slouken@3057
  1914
    if (retval == 0) {
slouken@3057
  1915
        ++_this->gl_config.driver_loaded;
slouken@2753
  1916
    }
slouken@2753
  1917
    return (retval);
slouken@0
  1918
}
slouken@0
  1919
slouken@2753
  1920
void *
slouken@1895
  1921
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1922
{
slouken@2753
  1923
    void *func;
slouken@0
  1924
slouken@2753
  1925
    if (!_this) {
slouken@2753
  1926
        SDL_UninitializedVideo();
slouken@2753
  1927
        return NULL;
slouken@2753
  1928
    }
slouken@2753
  1929
    func = NULL;
slouken@2753
  1930
    if (_this->GL_GetProcAddress) {
slouken@2753
  1931
        if (_this->gl_config.driver_loaded) {
slouken@2753
  1932
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  1933
        } else {
slouken@2753
  1934
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  1935
        }
slouken@2753
  1936
    } else {
slouken@2753
  1937
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  1938
    }
slouken@2753
  1939
    return func;
slouken@0
  1940
}
slouken@0
  1941
slouken@3057
  1942
void
slouken@3057
  1943
SDL_GL_UnloadLibrary(void)
slouken@3057
  1944
{
slouken@3057
  1945
    if (!_this) {
slouken@3057
  1946
        SDL_UninitializedVideo();
slouken@3057
  1947
        return;
slouken@3057
  1948
    }
slouken@3057
  1949
    if (_this->gl_config.driver_loaded > 0) {
slouken@3057
  1950
        if (--_this->gl_config.driver_loaded > 0) {
slouken@3057
  1951
            return;
slouken@3057
  1952
        }
slouken@3057
  1953
        if (_this->GL_UnloadLibrary) {
slouken@3057
  1954
            _this->GL_UnloadLibrary(_this);
slouken@3057
  1955
        }
slouken@3057
  1956
    }
slouken@3057
  1957
}
slouken@3057
  1958
slouken@1926
  1959
SDL_bool
slouken@1926
  1960
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  1961
{
slouken@5209
  1962
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  1963
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  1964
    const char *extensions;
slouken@2753
  1965
    const char *start;
slouken@2753
  1966
    const char *where, *terminator;
slouken@1926
  1967
slouken@2753
  1968
    /* Extension names should not have spaces. */
slouken@2753
  1969
    where = SDL_strchr(extension, ' ');
slouken@2753
  1970
    if (where || *extension == '\0') {
slouken@2753
  1971
        return SDL_FALSE;
slouken@2753
  1972
    }
slouken@2753
  1973
    /* See if there's an environment variable override */
slouken@2753
  1974
    start = SDL_getenv(extension);
slouken@2753
  1975
    if (start && *start == '0') {
slouken@2753
  1976
        return SDL_FALSE;
slouken@2753
  1977
    }
slouken@2753
  1978
    /* Lookup the available extensions */
slouken@2753
  1979
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@2753
  1980
    if (glGetStringFunc) {
slouken@2753
  1981
        extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  1982
    } else {
slouken@2753
  1983
        extensions = NULL;
slouken@2753
  1984
    }
slouken@2753
  1985
    if (!extensions) {
slouken@2753
  1986
        return SDL_FALSE;
slouken@2753
  1987
    }
slouken@2753
  1988
    /*
slouken@2753
  1989
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  1990
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  1991
     */
slouken@1926
  1992
slouken@2753
  1993
    start = extensions;
slouken@1926
  1994
slouken@2753
  1995
    for (;;) {
slouken@2753
  1996
        where = SDL_strstr(start, extension);
slouken@2753
  1997
        if (!where)
slouken@2753
  1998
            break;
slouken@1926
  1999
slouken@2753
  2000
        terminator = where + SDL_strlen(extension);
slouken@2753
  2001
        if (where == start || *(where - 1) == ' ')
slouken@2753
  2002
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  2003
                return SDL_TRUE;
slouken@1926
  2004
slouken@2753
  2005
        start = terminator;
slouken@2753
  2006
    }
slouken@2753
  2007
    return SDL_FALSE;
slouken@1926
  2008
#else
slouken@2753
  2009
    return SDL_FALSE;
slouken@1926
  2010
#endif
slouken@1926
  2011
}
slouken@1926
  2012
slouken@1895
  2013
int
slouken@1895
  2014
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2015
{
slouken@5209
  2016
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2017
    int retval;
slouken@0
  2018
slouken@2753
  2019
    if (!_this) {
slouken@2753
  2020
        SDL_UninitializedVideo();
slouken@2753
  2021
        return -1;
slouken@2753
  2022
    }
slouken@2753
  2023
    retval = 0;
slouken@2753
  2024
    switch (attr) {
slouken@2753
  2025
    case SDL_GL_RED_SIZE:
slouken@2753
  2026
        _this->gl_config.red_size = value;
slouken@2753
  2027
        break;
slouken@2753
  2028
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2029
        _this->gl_config.green_size = value;
slouken@2753
  2030
        break;
slouken@2753
  2031
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2032
        _this->gl_config.blue_size = value;
slouken@2753
  2033
        break;
slouken@2753
  2034
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2035
        _this->gl_config.alpha_size = value;
slouken@2753
  2036
        break;
slouken@2753
  2037
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  2038
        _this->gl_config.double_buffer = value;
slouken@2753
  2039
        break;
slouken@2753
  2040
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2041
        _this->gl_config.buffer_size = value;
slouken@2753
  2042
        break;
slouken@2753
  2043
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2044
        _this->gl_config.depth_size = value;
slouken@2753
  2045
        break;
slouken@2753
  2046
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2047
        _this->gl_config.stencil_size = value;
slouken@2753
  2048
        break;
slouken@2753
  2049
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2050
        _this->gl_config.accum_red_size = value;
slouken@2753
  2051
        break;
slouken@2753
  2052
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2053
        _this->gl_config.accum_green_size = value;
slouken@2753
  2054
        break;
slouken@2753
  2055
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2056
        _this->gl_config.accum_blue_size = value;
slouken@2753
  2057
        break;
slouken@2753
  2058
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2059
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  2060
        break;
slouken@2753
  2061
    case SDL_GL_STEREO:
slouken@2753
  2062
        _this->gl_config.stereo = value;
slouken@2753
  2063
        break;
slouken@2753
  2064
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  2065
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  2066
        break;
slouken@2753
  2067
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  2068
        _this->gl_config.multisamplesamples = value;
slouken@2753
  2069
        break;
slouken@2753
  2070
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2071
        _this->gl_config.accelerated = value;
slouken@2753
  2072
        break;
slouken@2753
  2073
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2074
        _this->gl_config.retained_backing = value;
slouken@2753
  2075
        break;
slouken@3100
  2076
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@3100
  2077
        _this->gl_config.major_version = value;
slouken@3100
  2078
        break;
slouken@3100
  2079
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@3100
  2080
        _this->gl_config.minor_version = value;
slouken@3100
  2081
        break;
slouken@2753
  2082
    default:
slouken@2753
  2083
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2084
        retval = -1;
slouken@2753
  2085
        break;
slouken@2753
  2086
    }
slouken@2753
  2087
    return retval;
slouken@1936
  2088
#else
slouken@2753
  2089
    SDL_Unsupported();
slouken@2753
  2090
    return -1;
slouken@2753
  2091
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2092
}
slouken@0
  2093
slouken@1895
  2094
int
slouken@1936
  2095
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  2096
{
slouken@5209
  2097
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2098
    void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
slouken@3139
  2099
    GLenum(APIENTRY * glGetErrorFunc) (void);
slouken@2753
  2100
    GLenum attrib = 0;
slouken@3099
  2101
    GLenum error = 0;
slouken@1912
  2102
slouken@2753
  2103
    glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
slouken@2753
  2104
    if (!glGetIntegervFunc) {
slouken@2753
  2105
        return -1;
slouken@2753
  2106
    }
slouken@3099
  2107
slouken@3099
  2108
    glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
slouken@3099
  2109
    if (!glGetErrorFunc) {
slouken@3099
  2110
        return -1;
slouken@3099
  2111
    }
slouken@3099
  2112
slouken@3099
  2113
    /* Clear value in any case */
slouken@3139
  2114
    *value = 0;
slouken@3099
  2115
slouken@2753
  2116
    switch (attr) {
slouken@2753
  2117
    case SDL_GL_RED_SIZE:
slouken@2753
  2118
        attrib = GL_RED_BITS;
slouken@2753
  2119
        break;
slouken@2753
  2120
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2121
        attrib = GL_BLUE_BITS;
slouken@2753
  2122
        break;
slouken@2753
  2123
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2124
        attrib = GL_GREEN_BITS;
slouken@2753
  2125
        break;
slouken@2753
  2126
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2127
        attrib = GL_ALPHA_BITS;
slouken@2753
  2128
        break;
slouken@2753
  2129
    case SDL_GL_DOUBLEBUFFER:
slouken@5209
  2130
#if SDL_VIDEO_OPENGL
slouken@2753
  2131
        attrib = GL_DOUBLEBUFFER;
slouken@2753
  2132
        break;
hfutrell@2745
  2133
#else
slouken@3099
  2134
        /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
slouken@3099
  2135
        /* parameter which switches double buffer to single buffer. OpenGL ES */
slouken@3099
  2136
        /* SDL driver must set proper value after initialization              */
slouken@3099
  2137
        *value = _this->gl_config.double_buffer;
slouken@2753
  2138
        return 0;
hfutrell@2745
  2139
#endif
slouken@2753
  2140
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2141
        attrib = GL_DEPTH_BITS;
slouken@2753
  2142
        break;
slouken@2753
  2143
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2144
        attrib = GL_STENCIL_BITS;
slouken@2753
  2145
        break;
slouken@5209
  2146
#if SDL_VIDEO_OPENGL
slouken@2753
  2147
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2148
        attrib = GL_ACCUM_RED_BITS;
slouken@2753
  2149
        break;
slouken@2753
  2150
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2151
        attrib = GL_ACCUM_GREEN_BITS;
slouken@2753
  2152
        break;
slouken@2753
  2153
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2154
        attrib = GL_ACCUM_BLUE_BITS;
slouken@2753
  2155
        break;
slouken@2753
  2156
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2157
        attrib = GL_ACCUM_ALPHA_BITS;
slouken@2753
  2158
        break;
slouken@2753
  2159
    case SDL_GL_STEREO:
slouken@2753
  2160
        attrib = GL_STEREO;
slouken@2753
  2161
        break;
hfutrell@2745
  2162
#else
slouken@2753
  2163
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2164
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2165
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2166
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2167
    case SDL_GL_STEREO:
slouken@2753
  2168
        /* none of these are supported in OpenGL ES */
slouken@2753
  2169
        *value = 0;
slouken@2753
  2170
        return 0;
hfutrell@2745
  2171
#endif
slouken@2753
  2172
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@5209
  2173
#if SDL_VIDEO_OPENGL
slouken@2753
  2174
        attrib = GL_SAMPLE_BUFFERS_ARB;
hfutrell@2745
  2175
#else
slouken@2753
  2176
        attrib = GL_SAMPLE_BUFFERS;
hfutrell@2745
  2177
#endif
slouken@2753
  2178
        break;
slouken@2753
  2179
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@5209
  2180
#if SDL_VIDEO_OPENGL
slouken@2753
  2181
        attrib = GL_SAMPLES_ARB;
hfutrell@2745
  2182
#else
slouken@2753
  2183
        attrib = GL_SAMPLES;
hfutrell@2745
  2184
#endif
slouken@2753
  2185
        break;
slouken@2753
  2186
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2187
        {
slouken@2753
  2188
            GLint bits = 0;
slouken@2753
  2189
            GLint component;
slouken@1936
  2190
slouken@2753
  2191
            /*
slouken@2753
  2192
             * there doesn't seem to be a single flag in OpenGL
slouken@2753
  2193
             * for this!
slouken@2753
  2194
             */
slouken@2753
  2195
            glGetIntegervFunc(GL_RED_BITS, &component);
slouken@2753
  2196
            bits += component;
slouken@2753
  2197
            glGetIntegervFunc(GL_GREEN_BITS, &component);
slouken@2753
  2198
            bits += component;
slouken@2753
  2199
            glGetIntegervFunc(GL_BLUE_BITS, &component);
slouken@2753
  2200
            bits += component;
slouken@2753
  2201
            glGetIntegervFunc(GL_ALPHA_BITS, &component);
slouken@2753
  2202
            bits += component;
slouken@1936
  2203
slouken@2753
  2204
            *value = bits;
slouken@2753
  2205
            return 0;
slouken@2753
  2206
        }
slouken@2753
  2207
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2208
        {
slouken@2753
  2209
            /* FIXME: How do we get this information? */
slouken@3571
  2210
            *value = (_this->gl_config.accelerated != 0);
slouken@2753
  2211
            return 0;
slouken@2753
  2212
        }
slouken@5210
  2213
    case SDL_GL_RETAINED_BACKING:
slouken@5210
  2214
        {
slouken@5210
  2215
            *value = _this->gl_config.retained_backing;
slouken@5210
  2216
            return 0;
slouken@5210
  2217
        }
slouken@5210
  2218
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@5210
  2219
        {
slouken@5210
  2220
            *value = _this->gl_config.major_version;
slouken@5210
  2221
            return 0;
slouken@5210
  2222
        }
slouken@5210
  2223
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@5210
  2224
        {
slouken@5210
  2225
            *value = _this->gl_config.minor_version;
slouken@5210
  2226
            return 0;
slouken@5210
  2227
        }
slouken@2753
  2228
    default:
slouken@2753
  2229
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2230
        return -1;
slouken@2753
  2231
    }
slouken@0
  2232
slouken@2753
  2233
    glGetIntegervFunc(attrib, (GLint *) value);
slouken@3139
  2234
    error = glGetErrorFunc();
slouken@3139
  2235
    if (error != GL_NO_ERROR) {
slouken@3139
  2236
        switch (error) {
slouken@3139
  2237
        case GL_INVALID_ENUM:
slouken@3139
  2238
            {
slouken@3139
  2239
                SDL_SetError("OpenGL error: GL_INVALID_ENUM");
slouken@3139
  2240
            }
slouken@3139
  2241
            break;
slouken@3139
  2242
        case GL_INVALID_VALUE:
slouken@3139
  2243
            {
slouken@3139
  2244
                SDL_SetError("OpenGL error: GL_INVALID_VALUE");
slouken@3139
  2245
            }
slouken@3139
  2246
            break;
slouken@3139
  2247
        default:
slouken@3139
  2248
            {
slouken@3139
  2249
                SDL_SetError("OpenGL error: %08X", error);
slouken@3139
  2250
            }
slouken@3139
  2251
            break;
slouken@3139
  2252
        }
slouken@3139
  2253
        return -1;
slouken@3099
  2254
    }
slouken@2753
  2255
    return 0;
slouken@1936
  2256
#else
slouken@2753
  2257
    SDL_Unsupported();
slouken@2753
  2258
    return -1;
slouken@2753
  2259
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2260
}
slouken@0
  2261
slouken@1912
  2262
SDL_GLContext
slouken@3685
  2263
SDL_GL_CreateContext(SDL_Window * window)
slouken@1912
  2264
{
slouken@3695
  2265
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  2266
slouken@2753
  2267
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2268
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2269
        return NULL;
slouken@2753
  2270
    }
slouken@2753
  2271
    return _this->GL_CreateContext(_this, window);
slouken@1912
  2272
}
slouken@1912
  2273
slouken@1912
  2274
int
slouken@3685
  2275
SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
slouken@1912
  2276
{
slouken@3695
  2277
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  2278
slouken@3695
  2279
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2280
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2281
        return -1;
slouken@2753
  2282
    }
slouken@2753
  2283
    if (!context) {
slouken@2753
  2284
        window = NULL;
slouken@2753
  2285
    }
slouken@2753
  2286
    return _this->GL_MakeCurrent(_this, window, context);
slouken@1912
  2287
}
slouken@1912
  2288
slouken@1912
  2289
int
slouken@1912
  2290
SDL_GL_SetSwapInterval(int interval)
slouken@1912
  2291
{
slouken@2753
  2292
    if (!_this) {
slouken@2753
  2293
        SDL_UninitializedVideo();
slouken@2753
  2294
        return -1;
slouken@2753
  2295
    }
slouken@2753
  2296
    if (_this->GL_SetSwapInterval) {
slouken@2753
  2297
        return _this->GL_SetSwapInterval(_this, interval);
slouken@2753
  2298
    } else {
slouken@2753
  2299
        SDL_SetError("Setting the swap interval is not supported");
slouken@2753
  2300
        return -1;
slouken@2753
  2301
    }
slouken@1912
  2302
}
slouken@1912
  2303
slouken@1912
  2304
int
slouken@1912
  2305
SDL_GL_GetSwapInterval(void)
slouken@1912
  2306
{
slouken@2753
  2307
    if (!_this) {
slouken@2753
  2308
        SDL_UninitializedVideo();
slouken@2753
  2309
        return -1;
slouken@2753
  2310
    }
slouken@2753
  2311
    if (_this->GL_GetSwapInterval) {
slouken@2753
  2312
        return _this->GL_GetSwapInterval(_this);
slouken@2753
  2313
    } else {
slouken@2753
  2314
        SDL_SetError("Getting the swap interval is not supported");
slouken@2753
  2315
        return -1;
slouken@2753
  2316
    }
slouken@1912
  2317
}
slouken@1912
  2318
slouken@1895
  2319
void
slouken@3685
  2320
SDL_GL_SwapWindow(SDL_Window * window)
slouken@0
  2321
{
slouken@3695
  2322
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  2323
slouken@2753
  2324
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  2325
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  2326
        return;
slouken@2753
  2327
    }
slouken@2753
  2328
    _this->GL_SwapWindow(_this, window);
slouken@1912
  2329
}
slouken@1912
  2330
slouken@1912
  2331
void
slouken@1912
  2332
SDL_GL_DeleteContext(SDL_GLContext context)
slouken@1912
  2333
{
slouken@4526
  2334
    if (!_this || !_this->gl_data || !context) {
slouken@2753
  2335
        return;
slouken@2753
  2336
    }
slouken@2753
  2337
    _this->GL_MakeCurrent(_this, NULL, NULL);
slouken@2753
  2338
    _this->GL_DeleteContext(_this, context);
slouken@0
  2339
}
slouken@0
  2340
slouken@1895
  2341
#if 0                           // FIXME
hfutrell@2744
  2342
/*
hfutrell@2744
  2343
 * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
hfutrell@2744
  2344
 * & 2 for alpha channel.
hfutrell@2744
  2345
 */
slouken@1895
  2346
static void
slouken@1895
  2347
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  2348
{
slouken@2753
  2349
    int x, y;
slouken@2753
  2350
    Uint32 colorkey;
slouken@0
  2351
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  2352
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  2353
slouken@2753
  2354
    colorkey = icon->format->colorkey;
slouken@2753
  2355
    switch (icon->format->BytesPerPixel) {
slouken@2753
  2356
    case 1:
slouken@2753
  2357
        {
slouken@2753
  2358
            Uint8 *pixels;
slouken@2753
  2359
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2360
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@2753
  2361
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2362
                    if (*pixels++ == colorkey) {
slouken@2753
  2363
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2364
                    }
slouken@2753
  2365
                }
slouken@2753
  2366
            }
slouken@2753
  2367
        }
slouken@2753
  2368
        break;
slouken@0
  2369
slouken@2753
  2370
    case 2:
slouken@2753
  2371
        {
slouken@2753
  2372
            Uint16 *pixels;
slouken@2753
  2373
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2374
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@2753
  2375
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2376
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  2377
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2378
                    } else if ((flags & 2)
slouken@2753
  2379
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  2380
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2381
                    }
slouken@2753
  2382
                    pixels++;
slouken@2753
  2383
                }
slouken@2753
  2384
            }
slouken@2753
  2385
        }
slouken@2753
  2386
        break;
slouken@0
  2387
slouken@2753
  2388
    case 4:
slouken@2753
  2389
        {
slouken@2753
  2390
            Uint32 *pixels;
slouken@2753
  2391
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  2392
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@2753
  2393
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  2394
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  2395
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2396
                    } else if ((flags & 2)
slouken@2753
  2397
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  2398
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  2399
                    }
slouken@2753
  2400
                    pixels++;
slouken@2753
  2401
                }
slouken@2753
  2402
            }
slouken@2753
  2403
        }
slouken@2753
  2404
        break;
slouken@2753
  2405
    }
slouken@0
  2406
}
slouken@0
  2407
slouken@0
  2408
/*
slouken@0
  2409
 * Sets the window manager icon for the display window.
slouken@0
  2410
 */
slouken@1895
  2411
void
slouken@1895
  2412
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  2413
{
slouken@2753
  2414
    if (icon && _this->SetIcon) {
slouken@2753
  2415
        /* Generate a mask if necessary, and create the icon! */
slouken@2753
  2416
        if (mask == NULL) {
slouken@2753
  2417
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@2753
  2418
            int flags = 0;
slouken@2753
  2419
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@2753
  2420
            if (mask == NULL) {
slouken@2753
  2421
                return;
slouken@2753
  2422
            }
slouken@2753
  2423
            SDL_memset(mask, ~0, mask_len);
slouken@2753
  2424
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@2753
  2425
                flags |= 1;
slouken@2753
  2426
            if (icon->flags & SDL_SRCALPHA)
slouken@2753
  2427
                flags |= 2;
slouken@2753
  2428
            if (flags) {
slouken@2753
  2429
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@2753
  2430
            }
slouken@2753
  2431
            _this->SetIcon(_this, icon, mask);
slouken@2753
  2432
            SDL_free(mask);
slouken@2753
  2433
        } else {
slouken@2753
  2434
            _this->SetIcon(_this, icon, mask);
slouken@2753
  2435
        }
slouken@2753
  2436
    }
slouken@1895
  2437
}
slouken@1895
  2438
#endif
slouken@0
  2439
slouken@1895
  2440
SDL_bool
slouken@3685
  2441
SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
slouken@1895
  2442
{
slouken@3695
  2443
    CHECK_WINDOW_MAGIC(window, SDL_FALSE);
slouken@3695
  2444
slouken@4900
  2445
    if (!info) {
slouken@4900
  2446
        return SDL_FALSE;
slouken@4900
  2447
    }
slouken@4900
  2448
    info->subsystem = SDL_SYSWM_UNKNOWN;
slouken@4900
  2449
slouken@3695
  2450
    if (!_this->GetWindowWMInfo) {
slouken@2753
  2451
        return SDL_FALSE;
slouken@2753
  2452
    }
slouken@2753
  2453
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  2454
}
slouken@0
  2455
slouken@3280
  2456
void
slouken@3280
  2457
SDL_StartTextInput(void)
slouken@3280
  2458
{
slouken@3683
  2459
    if (_this && _this->StartTextInput) {
slouken@3280
  2460
        _this->StartTextInput(_this);
slouken@3280
  2461
    }
slouken@3280
  2462
    SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
slouken@3280
  2463
    SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
slouken@3280
  2464
}
slouken@3280
  2465
slouken@3280
  2466
void
slouken@3280
  2467
SDL_StopTextInput(void)
slouken@3280
  2468
{
slouken@3683
  2469
    if (_this && _this->StopTextInput) {
slouken@3280
  2470
        _this->StopTextInput(_this);
slouken@3280
  2471
    }
slouken@3280
  2472
    SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
slouken@3280
  2473
    SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
slouken@3280
  2474
}
slouken@3280
  2475
slouken@3280
  2476
void
slouken@3280
  2477
SDL_SetTextInputRect(SDL_Rect *rect)
slouken@3280
  2478
{
slouken@3683
  2479
    if (_this && _this->SetTextInputRect) {
slouken@3280
  2480
        _this->SetTextInputRect(_this, rect);
slouken@3280
  2481
    }
slouken@3280
  2482
}
slouken@3280
  2483
slouken@1895
  2484
/* vi: set ts=4 sw=4 expandtab: */