src/video/SDL_video.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sun, 03 Mar 2013 17:22:51 -0800
changeset 6956 afdc35fa58e9
parent 6936 76d9c31e823d
child 6984 ae9c4b12f3e2
permissions -rw-r--r--
Fix behavior of SDL_GetWindowPosition for invalid input; add test cases to video suite
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* The high-level video driver subsystem */
slouken@0
    24
slouken@2999
    25
#include "SDL.h"
slouken@2984
    26
#include "SDL_video.h"
slouken@0
    27
#include "SDL_sysvideo.h"
slouken@0
    28
#include "SDL_blit.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@5294
    30
#include "SDL_rect_c.h"
slouken@1361
    31
#include "../events/SDL_events_c.h"
slouken@0
    32
slouken@5209
    33
#if SDL_VIDEO_OPENGL
slouken@5209
    34
#include "SDL_opengl.h"
slouken@5209
    35
#endif /* SDL_VIDEO_OPENGL */
slouken@5209
    36
hfutrell@2745
    37
#if SDL_VIDEO_OPENGL_ES
hfutrell@2745
    38
#include "SDL_opengles.h"
slouken@2753
    39
#endif /* SDL_VIDEO_OPENGL_ES */
hfutrell@2745
    40
slouken@5209
    41
#if SDL_VIDEO_OPENGL_ES2
slouken@5209
    42
#include "SDL_opengles2.h"
slouken@5209
    43
#endif /* SDL_VIDEO_OPENGL_ES2 */
slouken@4900
    44
slouken@4900
    45
#include "SDL_syswm.h"
slouken@1926
    46
slouken@1926
    47
/* On Windows, windows.h defines CreateWindow */
slouken@1926
    48
#ifdef CreateWindow
slouken@1926
    49
#undef CreateWindow
slouken@1926
    50
#endif
slouken@1926
    51
slouken@0
    52
/* Available video drivers */
lestat@3166
    53
static VideoBootStrap *bootstrap[] = {
slouken@1931
    54
#if SDL_VIDEO_DRIVER_COCOA
slouken@2753
    55
    &COCOA_bootstrap,
icculus@1133
    56
#endif
slouken@1361
    57
#if SDL_VIDEO_DRIVER_X11
slouken@2753
    58
    &X11_bootstrap,
slouken@0
    59
#endif
slouken@1361
    60
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@2753
    61
    &DirectFB_bootstrap,
slouken@167
    62
#endif
slouken@5062
    63
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@5062
    64
    &WINDOWS_bootstrap,
slouken@1361
    65
#endif
slouken@1361
    66
#if SDL_VIDEO_DRIVER_BWINDOW
slouken@2753
    67
    &BWINDOW_bootstrap,
slouken@1361
    68
#endif
slouken@5150
    69
#if SDL_VIDEO_DRIVER_PANDORA
slouken@5150
    70
    &PND_bootstrap,
slouken@1361
    71
#endif
hfutrell@2743
    72
#if SDL_VIDEO_DRIVER_NDS
slouken@2753
    73
    &NDS_bootstrap,
slouken@2735
    74
#endif
hfutrell@2745
    75
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2753
    76
    &UIKIT_bootstrap,
hfutrell@2745
    77
#endif
slouken@5150
    78
#if SDL_VIDEO_DRIVER_ANDROID
slouken@5150
    79
    &Android_bootstrap,
slouken@5150
    80
#endif
slouken@1361
    81
#if SDL_VIDEO_DRIVER_DUMMY
slouken@2753
    82
    &DUMMY_bootstrap,
slouken@610
    83
#endif
slouken@2753
    84
    NULL
slouken@0
    85
};
slouken@173
    86
slouken@1895
    87
static SDL_VideoDevice *_this = NULL;
slouken@0
    88
slouken@3695
    89
#define CHECK_WINDOW_MAGIC(window, retval) \
slouken@3695
    90
    if (!_this) { \
slouken@3695
    91
        SDL_UninitializedVideo(); \
slouken@3695
    92
        return retval; \
slouken@3695
    93
    } \
slouken@3701
    94
    if (!window || window->magic != &_this->window_magic) { \
slouken@3695
    95
        SDL_SetError("Invalid window"); \
slouken@3695
    96
        return retval; \
slouken@3695
    97
    }
slouken@3695
    98
slouken@5244
    99
#define CHECK_DISPLAY_INDEX(displayIndex, retval) \
slouken@5244
   100
    if (!_this) { \
slouken@5244
   101
        SDL_UninitializedVideo(); \
slouken@5244
   102
        return retval; \
slouken@5244
   103
    } \
slouken@5244
   104
    if (displayIndex < 0 || displayIndex >= _this->num_displays) { \
slouken@5244
   105
        SDL_SetError("displayIndex must be in the range 0 - %d", \
slouken@5244
   106
                     _this->num_displays - 1); \
slouken@5244
   107
        return retval; \
slouken@5244
   108
    }
slouken@5244
   109
slouken@5166
   110
/* Support for framebuffer emulation using an accelerated renderer */
slouken@5166
   111
slouken@5166
   112
#define SDL_WINDOWTEXTUREDATA   "_SDL_WindowTextureData"
slouken@5166
   113
slouken@5166
   114
typedef struct {
slouken@5166
   115
    SDL_Renderer *renderer;
slouken@5166
   116
    SDL_Texture *texture;
slouken@5166
   117
    void *pixels;
slouken@5166
   118
    int pitch;
slouken@5172
   119
    int bytes_per_pixel;
slouken@5166
   120
} SDL_WindowTextureData;
slouken@5166
   121
slouken@5190
   122
static SDL_bool
slouken@5190
   123
ShouldUseTextureFramebuffer()
slouken@5190
   124
{
slouken@5190
   125
    const char *hint;
slouken@5190
   126
slouken@5190
   127
    /* If there's no native framebuffer support then there's no option */
slouken@5190
   128
    if (!_this->CreateWindowFramebuffer) {
slouken@5190
   129
        return SDL_TRUE;
slouken@5190
   130
    }
slouken@5190
   131
slouken@5192
   132
    /* If the user has specified a software renderer we can't use a
slouken@5192
   133
       texture framebuffer, or renderer creation will go recursive.
slouken@5192
   134
     */
slouken@5192
   135
    hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
slouken@5192
   136
    if (hint && SDL_strcasecmp(hint, "software") == 0) {
slouken@5192
   137
        return SDL_FALSE;
slouken@5192
   138
    }
slouken@5192
   139
slouken@5190
   140
    /* See if the user or application wants a specific behavior */
slouken@5190
   141
    hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
slouken@5190
   142
    if (hint) {
slouken@5190
   143
        if (*hint == '0') {
slouken@5190
   144
            return SDL_FALSE;
slouken@5190
   145
        } else {
slouken@5190
   146
            return SDL_TRUE;
slouken@5190
   147
        }
slouken@5190
   148
    }
slouken@5190
   149
slouken@5190
   150
    /* Each platform has different performance characteristics */
slouken@5190
   151
#if defined(__WIN32__)
slouken@5190
   152
    /* GDI BitBlt() is way faster than Direct3D dynamic textures right now.
slouken@5190
   153
     */
slouken@5190
   154
    return SDL_FALSE;
slouken@5190
   155
slouken@5190
   156
#elif defined(__MACOSX__)
slouken@5190
   157
    /* Mac OS X uses OpenGL as the native fast path */
slouken@5190
   158
    return SDL_TRUE;
slouken@5190
   159
slouken@5190
   160
#elif defined(__LINUX__)
slouken@5190
   161
    /* Properly configured OpenGL drivers are faster than MIT-SHM */
slouken@5190
   162
#if SDL_VIDEO_OPENGL
slouken@5190
   163
    /* Ugh, find a way to cache this value! */
slouken@5190
   164
    {
slouken@5190
   165
        SDL_Window *window;
slouken@5190
   166
        SDL_GLContext context;
slouken@5190
   167
        SDL_bool hasAcceleratedOpenGL = SDL_FALSE;
slouken@5190
   168
slouken@5385
   169
        window = SDL_CreateWindow("OpenGL test", -32, -32, 32, 32, SDL_WINDOW_OPENGL|SDL_WINDOW_HIDDEN);
slouken@5190
   170
        if (window) {
slouken@5190
   171
            context = SDL_GL_CreateContext(window);
slouken@5190
   172
            if (context) {
slouken@5190
   173
                const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@5190
   174
                const char *vendor = NULL;
slouken@5190
   175
slouken@5190
   176
                glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@5190
   177
                if (glGetStringFunc) {
slouken@5190
   178
                    vendor = (const char *) glGetStringFunc(GL_VENDOR);
slouken@5190
   179
                }
slouken@5190
   180
                /* Add more vendors here at will... */
slouken@5190
   181
                if (vendor &&
slouken@5190
   182
                    (SDL_strstr(vendor, "ATI Technologies") ||
slouken@5190
   183
                     SDL_strstr(vendor, "NVIDIA"))) {
slouken@5190
   184
                    hasAcceleratedOpenGL = SDL_TRUE;
slouken@5190
   185
                }
slouken@5190
   186
                SDL_GL_DeleteContext(context);
slouken@5190
   187
            }
slouken@5190
   188
            SDL_DestroyWindow(window);
slouken@5190
   189
        }
slouken@5190
   190
        return hasAcceleratedOpenGL;
slouken@5190
   191
    }
slouken@6188
   192
#elif SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6188
   193
    /* Let's be optimistic about this! */
slouken@6188
   194
    return SDL_TRUE;
slouken@5190
   195
#else
slouken@5190
   196
    return SDL_FALSE;
slouken@5190
   197
#endif
slouken@5190
   198
slouken@5190
   199
#else
slouken@5190
   200
    /* Play it safe, assume that if there is a framebuffer driver that it's
slouken@5190
   201
       optimized for the current platform.
slouken@5190
   202
    */
slouken@5190
   203
    return SDL_FALSE;
slouken@5190
   204
#endif
slouken@5190
   205
}
slouken@5190
   206
slouken@5166
   207
static int
slouken@5166
   208
SDL_CreateWindowTexture(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
slouken@5166
   209
{
slouken@5166
   210
    SDL_WindowTextureData *data;
slouken@5166
   211
    SDL_RendererInfo info;
slouken@5166
   212
    Uint32 i;
slouken@5166
   213
slouken@5166
   214
    data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
slouken@5166
   215
    if (!data) {
slouken@5291
   216
        SDL_Renderer *renderer = NULL;
slouken@5168
   217
        SDL_RendererInfo info;
slouken@5168
   218
        int i;
slouken@5191
   219
        const char *hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
slouken@5168
   220
slouken@5191
   221
        /* Check to see if there's a specific driver requested */
slouken@5191
   222
        if (hint && *hint != '0' && *hint != '1' &&
slouken@5191
   223
            SDL_strcasecmp(hint, "software") != 0) {
slouken@5191
   224
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
slouken@5191
   225
                SDL_GetRenderDriverInfo(i, &info);
slouken@5191
   226
                if (SDL_strcasecmp(info.name, hint) == 0) {
slouken@5191
   227
                    renderer = SDL_CreateRenderer(window, i, 0);
slouken@5168
   228
                    break;
slouken@5168
   229
                }
slouken@5168
   230
            }
slouken@5168
   231
        }
slouken@5191
   232
slouken@5191
   233
        if (!renderer) {
slouken@5191
   234
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
slouken@5191
   235
                SDL_GetRenderDriverInfo(i, &info);
slouken@5191
   236
                if (SDL_strcmp(info.name, "software") != 0) {
slouken@5191
   237
                    renderer = SDL_CreateRenderer(window, i, 0);
slouken@5191
   238
                    if (renderer) {
slouken@5191
   239
                        break;
slouken@5191
   240
                    }
slouken@5191
   241
                }
slouken@5191
   242
            }
slouken@5191
   243
        }
slouken@5166
   244
        if (!renderer) {
slouken@6174
   245
            SDL_SetError("No hardware accelerated renderers available");
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@6900
   440
            if (SDL_strncasecmp(bootstrap[i]->name, driver_name, SDL_strlen(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@6370
   491
    _this->gl_config.use_egl = 0;
slouken@5217
   492
#elif SDL_VIDEO_OPENGL_ES
slouken@5217
   493
    _this->gl_config.major_version = 1;
slouken@5217
   494
    _this->gl_config.minor_version = 1;
slouken@6370
   495
    _this->gl_config.use_egl = 1;
slouken@5209
   496
#elif SDL_VIDEO_OPENGL_ES2
slouken@5209
   497
    _this->gl_config.major_version = 2;
slouken@5209
   498
    _this->gl_config.minor_version = 0;
slouken@6370
   499
    _this->gl_config.use_egl = 1;
slouken@5209
   500
#endif
slouken@6296
   501
    _this->gl_config.flags = 0;
slouken@6296
   502
    _this->gl_config.profile_mask = 0;
slouken@6393
   503
    _this->gl_config.share_with_current_context = 0;
slouken@0
   504
slouken@2753
   505
    /* Initialize the video subsystem */
slouken@2753
   506
    if (_this->VideoInit(_this) < 0) {
slouken@2753
   507
        SDL_VideoQuit();
slouken@2753
   508
        return -1;
slouken@2753
   509
    }
slouken@5166
   510
slouken@2753
   511
    /* Make sure some displays were added */
slouken@2753
   512
    if (_this->num_displays == 0) {
slouken@2753
   513
        SDL_SetError("The video driver did not add any displays");
slouken@2753
   514
        SDL_VideoQuit();
slouken@2753
   515
        return (-1);
slouken@2753
   516
    }
slouken@0
   517
slouken@5190
   518
    /* Add the renderer framebuffer emulation if desired */
slouken@5190
   519
    if (ShouldUseTextureFramebuffer()) {
slouken@5166
   520
        _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
slouken@5166
   521
        _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
slouken@5166
   522
        _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
slouken@5166
   523
    }
slouken@5166
   524
slouken@6670
   525
    /* If we don't use a screen keyboard, turn on text input by default,
slouken@6670
   526
       otherwise programs that expect to get text events without enabling
slouken@6670
   527
       UNICODE input won't get any events.
slouken@6670
   528
slouken@6670
   529
       Actually, come to think of it, you needed to call SDL_EnableUNICODE(1)
slouken@6670
   530
       in SDL 1.2 before you got text input events.  Hmm...
slouken@6670
   531
     */
slouken@6670
   532
    if (!SDL_HasScreenKeyboardSupport()) {
slouken@6670
   533
        SDL_StartTextInput();
slouken@6670
   534
    }
slouken@6670
   535
slouken@2753
   536
    /* We're ready to go! */
slouken@2753
   537
    return 0;
slouken@0
   538
}
slouken@0
   539
slouken@2753
   540
const char *
slouken@1895
   541
SDL_GetCurrentVideoDriver()
slouken@0
   542
{
slouken@2753
   543
    if (!_this) {
slouken@2753
   544
        SDL_UninitializedVideo();
slouken@2753
   545
        return NULL;
slouken@2753
   546
    }
slouken@2753
   547
    return _this->name;
slouken@0
   548
}
slouken@0
   549
slouken@1895
   550
SDL_VideoDevice *
slouken@4472
   551
SDL_GetVideoDevice(void)
slouken@0
   552
{
slouken@2753
   553
    return _this;
slouken@0
   554
}
slouken@0
   555
slouken@1895
   556
int
slouken@1895
   557
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   558
{
slouken@2753
   559
    SDL_VideoDisplay display;
slouken@0
   560
slouken@2753
   561
    SDL_zero(display);
slouken@2753
   562
    if (desktop_mode) {
slouken@2753
   563
        display.desktop_mode = *desktop_mode;
slouken@2753
   564
    }
slouken@2753
   565
    display.current_mode = display.desktop_mode;
slouken@1895
   566
slouken@2753
   567
    return SDL_AddVideoDisplay(&display);
slouken@0
   568
}
slouken@0
   569
slouken@1895
   570
int
slouken@1895
   571
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@0
   572
{
slouken@2753
   573
    SDL_VideoDisplay *displays;
slouken@2753
   574
    int index = -1;
slouken@0
   575
slouken@2753
   576
    displays =
slouken@2753
   577
        SDL_realloc(_this->displays,
slouken@2753
   578
                    (_this->num_displays + 1) * sizeof(*displays));
slouken@2753
   579
    if (displays) {
slouken@2753
   580
        index = _this->num_displays++;
slouken@2753
   581
        displays[index] = *display;
slouken@2753
   582
        displays[index].device = _this;
slouken@2753
   583
        _this->displays = displays;
slouken@6787
   584
slouken@6787
   585
        if (display->name) {
slouken@6787
   586
            displays[index].name = SDL_strdup(display->name);
slouken@6787
   587
        } else {
slouken@6787
   588
            char name[32];
slouken@6787
   589
slouken@6787
   590
            SDL_itoa(index, name, 10);
slouken@6787
   591
            displays[index].name = SDL_strdup(name);
slouken@6787
   592
        }
slouken@2753
   593
    } else {
slouken@2753
   594
        SDL_OutOfMemory();
slouken@2753
   595
    }
slouken@2753
   596
    return index;
slouken@0
   597
}
slouken@0
   598
slouken@1895
   599
int
slouken@1895
   600
SDL_GetNumVideoDisplays(void)
slouken@0
   601
{
slouken@2753
   602
    if (!_this) {
slouken@2753
   603
        SDL_UninitializedVideo();
slouken@2753
   604
        return 0;
slouken@2753
   605
    }
slouken@2753
   606
    return _this->num_displays;
slouken@0
   607
}
slouken@0
   608
slouken@6044
   609
static int
slouken@5380
   610
SDL_GetIndexOfDisplay(SDL_VideoDisplay *display)
slouken@5380
   611
{
slouken@5380
   612
    int displayIndex;
slouken@5380
   613
slouken@5380
   614
    for (displayIndex = 0; displayIndex < _this->num_displays; ++displayIndex) {
slouken@5380
   615
        if (display == &_this->displays[displayIndex]) {
slouken@5380
   616
            return displayIndex;
slouken@5380
   617
        }
slouken@5380
   618
    }
slouken@5380
   619
slouken@5380
   620
    /* Couldn't find the display, just use index 0 */
slouken@5380
   621
    return 0;
slouken@5380
   622
}
slouken@5380
   623
slouken@6787
   624
const char *
slouken@6787
   625
SDL_GetDisplayName(int displayIndex)
slouken@6787
   626
{
slouken@6787
   627
    CHECK_DISPLAY_INDEX(displayIndex, NULL);
slouken@6787
   628
slouken@6787
   629
    return _this->displays[displayIndex].name;
slouken@6787
   630
}
slouken@6787
   631
slouken@5380
   632
int
slouken@5244
   633
SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
slouken@3528
   634
{
slouken@5244
   635
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   636
slouken@3528
   637
    if (rect) {
slouken@5244
   638
        SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@3528
   639
slouken@3528
   640
        if (_this->GetDisplayBounds) {
slouken@5249
   641
            if (_this->GetDisplayBounds(_this, display, rect) == 0) {
slouken@5249
   642
                return 0;
slouken@3528
   643
            }
slouken@5249
   644
        }
slouken@5249
   645
slouken@5249
   646
        /* Assume that the displays are left to right */
slouken@5249
   647
        if (displayIndex == 0) {
slouken@5249
   648
            rect->x = 0;
slouken@5249
   649
            rect->y = 0;
slouken@3528
   650
        } else {
slouken@5249
   651
            SDL_GetDisplayBounds(displayIndex-1, rect);
slouken@5249
   652
            rect->x += rect->w;
slouken@3528
   653
        }
slouken@6518
   654
        rect->w = display->current_mode.w;
slouken@6518
   655
        rect->h = display->current_mode.h;
slouken@3528
   656
    }
slouken@3528
   657
    return 0;
slouken@3528
   658
}
slouken@3528
   659
slouken@1895
   660
SDL_bool
slouken@3500
   661
SDL_AddDisplayMode(SDL_VideoDisplay * display,  const SDL_DisplayMode * mode)
slouken@0
   662
{
slouken@2753
   663
    SDL_DisplayMode *modes;
slouken@2753
   664
    int i, nmodes;
slouken@0
   665
slouken@2753
   666
    /* Make sure we don't already have the mode in the list */
slouken@2753
   667
    modes = display->display_modes;
slouken@2753
   668
    nmodes = display->num_display_modes;
slouken@2753
   669
    for (i = nmodes; i--;) {
slouken@2753
   670
        if (SDL_memcmp(mode, &modes[i], sizeof(*mode)) == 0) {
slouken@2753
   671
            return SDL_FALSE;
slouken@2753
   672
        }
slouken@2753
   673
    }
slouken@1895
   674
slouken@2753
   675
    /* Go ahead and add the new mode */
slouken@2753
   676
    if (nmodes == display->max_display_modes) {
slouken@2753
   677
        modes =
slouken@2753
   678
            SDL_realloc(modes,
slouken@2753
   679
                        (display->max_display_modes + 32) * sizeof(*modes));
slouken@2753
   680
        if (!modes) {
slouken@2753
   681
            return SDL_FALSE;
slouken@2753
   682
        }
slouken@2753
   683
        display->display_modes = modes;
slouken@2753
   684
        display->max_display_modes += 32;
slouken@2753
   685
    }
slouken@2753
   686
    modes[nmodes] = *mode;
slouken@2753
   687
    display->num_display_modes++;
slouken@1895
   688
lestat@3178
   689
    /* Re-sort video modes */
lestat@3178
   690
    SDL_qsort(display->display_modes, display->num_display_modes,
slouken@3186
   691
              sizeof(SDL_DisplayMode), cmpmodes);
lestat@3178
   692
slouken@2753
   693
    return SDL_TRUE;
slouken@0
   694
}
slouken@0
   695
slouken@5244
   696
static int
slouken@3500
   697
SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
slouken@3500
   698
{
slouken@3500
   699
    if (!display->num_display_modes && _this->GetDisplayModes) {
slouken@3500
   700
        _this->GetDisplayModes(_this, display);
slouken@3500
   701
        SDL_qsort(display->display_modes, display->num_display_modes,
slouken@3500
   702
                  sizeof(SDL_DisplayMode), cmpmodes);
slouken@3500
   703
    }
slouken@3500
   704
    return display->num_display_modes;
slouken@3500
   705
}
slouken@3500
   706
slouken@3500
   707
int
slouken@5244
   708
SDL_GetNumDisplayModes(int displayIndex)
slouken@0
   709
{
slouken@5244
   710
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   711
slouken@5244
   712
    return SDL_GetNumDisplayModesForDisplay(&_this->displays[displayIndex]);
slouken@3500
   713
}
slouken@3500
   714
slouken@3500
   715
int
slouken@5244
   716
SDL_GetDisplayMode(int displayIndex, int index, SDL_DisplayMode * mode)
slouken@3500
   717
{
slouken@5244
   718
    SDL_VideoDisplay *display;
slouken@5244
   719
slouken@5244
   720
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   721
slouken@5244
   722
    display = &_this->displays[displayIndex];
slouken@3500
   723
    if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
slouken@3500
   724
        SDL_SetError("index must be in the range of 0 - %d",
slouken@3500
   725
                     SDL_GetNumDisplayModesForDisplay(display) - 1);
slouken@3500
   726
        return -1;
slouken@3500
   727
    }
slouken@3500
   728
    if (mode) {
slouken@3500
   729
        *mode = display->display_modes[index];
slouken@2753
   730
    }
slouken@2753
   731
    return 0;
slouken@0
   732
}
slouken@0
   733
slouken@1967
   734
int
slouken@5244
   735
SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode * mode)
slouken@0
   736
{
slouken@5244
   737
    SDL_VideoDisplay *display;
slouken@3500
   738
slouken@5244
   739
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   740
slouken@5244
   741
    display = &_this->displays[displayIndex];
slouken@2753
   742
    if (mode) {
slouken@3500
   743
        *mode = display->desktop_mode;
slouken@2753
   744
    }
slouken@2753
   745
    return 0;
slouken@0
   746
}
slouken@0
   747
slouken@1967
   748
int
slouken@5244
   749
SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode * mode)
slouken@0
   750
{
slouken@5244
   751
    SDL_VideoDisplay *display;
slouken@3500
   752
slouken@5244
   753
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   754
slouken@5244
   755
    display = &_this->displays[displayIndex];
slouken@2753
   756
    if (mode) {
slouken@3500
   757
        *mode = display->current_mode;
slouken@2753
   758
    }
slouken@2753
   759
    return 0;
slouken@0
   760
}
slouken@0
   761
slouken@5244
   762
static SDL_DisplayMode *
slouken@3500
   763
SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
slouken@3500
   764
                                    const SDL_DisplayMode * mode,
slouken@3500
   765
                                    SDL_DisplayMode * closest)
slouken@0
   766
{
slouken@2753
   767
    Uint32 target_format;
slouken@2753
   768
    int target_refresh_rate;
slouken@2753
   769
    int i;
slouken@2753
   770
    SDL_DisplayMode *current, *match;
slouken@0
   771
slouken@3500
   772
    if (!mode || !closest) {
slouken@3500
   773
        SDL_SetError("Missing desired mode or closest mode parameter");
slouken@2753
   774
        return NULL;
slouken@2753
   775
    }
slouken@3500
   776
slouken@2753
   777
    /* Default to the desktop format */
slouken@2753
   778
    if (mode->format) {
slouken@2753
   779
        target_format = mode->format;
slouken@2753
   780
    } else {
slouken@3500
   781
        target_format = display->desktop_mode.format;
slouken@2753
   782
    }
slouken@0
   783
slouken@2753
   784
    /* Default to the desktop refresh rate */
slouken@2753
   785
    if (mode->refresh_rate) {
slouken@2753
   786
        target_refresh_rate = mode->refresh_rate;
slouken@2753
   787
    } else {
slouken@3500
   788
        target_refresh_rate = display->desktop_mode.refresh_rate;
slouken@2753
   789
    }
slouken@0
   790
slouken@2753
   791
    match = NULL;
slouken@3500
   792
    for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
slouken@3500
   793
        current = &display->display_modes[i];
slouken@0
   794
slouken@2789
   795
        if (current->w && (current->w < mode->w)) {
slouken@2753
   796
            /* Out of sorted modes large enough here */
slouken@2753
   797
            break;
slouken@2753
   798
        }
slouken@2789
   799
        if (current->h && (current->h < mode->h)) {
slouken@2789
   800
            if (current->w && (current->w == mode->w)) {
slouken@2789
   801
                /* Out of sorted modes large enough here */
slouken@2789
   802
                break;
slouken@2789
   803
            }
slouken@2789
   804
            /* Wider, but not tall enough, due to a different
slouken@2789
   805
               aspect ratio. This mode must be skipped, but closer
slouken@2789
   806
               modes may still follow. */
slouken@2789
   807
            continue;
slouken@2789
   808
        }
slouken@2753
   809
        if (!match || current->w < match->w || current->h < match->h) {
slouken@2753
   810
            match = current;
slouken@2753
   811
            continue;
slouken@2753
   812
        }
slouken@2753
   813
        if (current->format != match->format) {
slouken@2753
   814
            /* Sorted highest depth to lowest */
slouken@2753
   815
            if (current->format == target_format ||
slouken@2753
   816
                (SDL_BITSPERPIXEL(current->format) >=
slouken@2753
   817
                 SDL_BITSPERPIXEL(target_format)
slouken@2753
   818
                 && SDL_PIXELTYPE(current->format) ==
slouken@2753
   819
                 SDL_PIXELTYPE(target_format))) {
slouken@2753
   820
                match = current;
slouken@2753
   821
            }
slouken@2753
   822
            continue;
slouken@2753
   823
        }
slouken@2753
   824
        if (current->refresh_rate != match->refresh_rate) {
slouken@2753
   825
            /* Sorted highest refresh to lowest */
slouken@2753
   826
            if (current->refresh_rate >= target_refresh_rate) {
slouken@2753
   827
                match = current;
slouken@2753
   828
            }
slouken@2753
   829
        }
slouken@2753
   830
    }
slouken@2753
   831
    if (match) {
slouken@2753
   832
        if (match->format) {
slouken@2753
   833
            closest->format = match->format;
slouken@2753
   834
        } else {
slouken@2753
   835
            closest->format = mode->format;
slouken@2753
   836
        }
slouken@2753
   837
        if (match->w && match->h) {
slouken@2753
   838
            closest->w = match->w;
slouken@2753
   839
            closest->h = match->h;
slouken@2753
   840
        } else {
slouken@2753
   841
            closest->w = mode->w;
slouken@2753
   842
            closest->h = mode->h;
slouken@2753
   843
        }
slouken@2753
   844
        if (match->refresh_rate) {
slouken@2753
   845
            closest->refresh_rate = match->refresh_rate;
slouken@2753
   846
        } else {
slouken@2753
   847
            closest->refresh_rate = mode->refresh_rate;
slouken@2753
   848
        }
slouken@2753
   849
        closest->driverdata = match->driverdata;
slouken@1895
   850
slouken@2753
   851
        /*
slouken@2753
   852
         * Pick some reasonable defaults if the app and driver don't
slouken@2753
   853
         * care
slouken@2753
   854
         */
slouken@2753
   855
        if (!closest->format) {
slouken@2753
   856
            closest->format = SDL_PIXELFORMAT_RGB888;
slouken@2753
   857
        }
slouken@2753
   858
        if (!closest->w) {
slouken@2753
   859
            closest->w = 640;
slouken@2753
   860
        }
slouken@2753
   861
        if (!closest->h) {
slouken@2753
   862
            closest->h = 480;
slouken@2753
   863
        }
slouken@2753
   864
        return closest;
slouken@2753
   865
    }
slouken@2753
   866
    return NULL;
slouken@0
   867
}
slouken@0
   868
slouken@3500
   869
SDL_DisplayMode *
slouken@5244
   870
SDL_GetClosestDisplayMode(int displayIndex,
slouken@5244
   871
                          const SDL_DisplayMode * mode,
slouken@3500
   872
                          SDL_DisplayMode * closest)
slouken@3500
   873
{
slouken@5244
   874
    SDL_VideoDisplay *display;
slouken@5244
   875
slouken@5244
   876
    CHECK_DISPLAY_INDEX(displayIndex, NULL);
slouken@5244
   877
slouken@5244
   878
    display = &_this->displays[displayIndex];
slouken@5244
   879
    return SDL_GetClosestDisplayModeForDisplay(display, mode, closest);
slouken@3500
   880
}
slouken@3500
   881
slouken@6044
   882
static int
slouken@3500
   883
SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
slouken@0
   884
{
slouken@2753
   885
    SDL_DisplayMode display_mode;
slouken@2753
   886
    SDL_DisplayMode current_mode;
slouken@0
   887
slouken@3502
   888
    if (mode) {
slouken@3502
   889
        display_mode = *mode;
slouken@3502
   890
slouken@3502
   891
        /* Default to the current mode */
slouken@3502
   892
        if (!display_mode.format) {
slouken@3502
   893
            display_mode.format = display->current_mode.format;
slouken@3502
   894
        }
slouken@3502
   895
        if (!display_mode.w) {
slouken@3502
   896
            display_mode.w = display->current_mode.w;
slouken@3502
   897
        }
slouken@3502
   898
        if (!display_mode.h) {
slouken@3502
   899
            display_mode.h = display->current_mode.h;
slouken@3502
   900
        }
slouken@3502
   901
        if (!display_mode.refresh_rate) {
slouken@3502
   902
            display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@3502
   903
        }
slouken@3502
   904
slouken@3502
   905
        /* Get a good video mode, the closest one possible */
slouken@3502
   906
        if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
slouken@3502
   907
            SDL_SetError("No video mode large enough for %dx%d",
slouken@3502
   908
                         display_mode.w, display_mode.h);
slouken@3502
   909
            return -1;
slouken@3502
   910
        }
slouken@3502
   911
    } else {
slouken@3502
   912
        display_mode = display->desktop_mode;
slouken@2753
   913
    }
slouken@3500
   914
slouken@2753
   915
    /* See if there's anything left to do */
slouken@5244
   916
    current_mode = display->current_mode;
slouken@2753
   917
    if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
slouken@2753
   918
        return 0;
slouken@2753
   919
    }
slouken@3500
   920
slouken@2753
   921
    /* Actually change the display mode */
slouken@4978
   922
    if (!_this->SetDisplayMode) {
slouken@4978
   923
        SDL_SetError("Video driver doesn't support changing display mode");
slouken@4978
   924
        return -1;
slouken@4978
   925
    }
slouken@3500
   926
    if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
slouken@2753
   927
        return -1;
slouken@2753
   928
    }
slouken@2753
   929
    display->current_mode = display_mode;
slouken@2753
   930
    return 0;
slouken@0
   931
}
slouken@0
   932
slouken@1895
   933
int
slouken@6786
   934
SDL_GetWindowDisplayIndex(SDL_Window * window)
slouken@5246
   935
{
slouken@5246
   936
    int displayIndex;
slouken@5246
   937
    int i, dist;
slouken@5246
   938
    int closest = -1;
slouken@5246
   939
    int closest_dist = 0x7FFFFFFF;
slouken@5246
   940
    SDL_Point center;
slouken@5246
   941
    SDL_Point delta;
slouken@5246
   942
    SDL_Rect rect;
slouken@5246
   943
slouken@5246
   944
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5246
   945
slouken@5246
   946
    if (SDL_WINDOWPOS_ISUNDEFINED(window->x) ||
slouken@5246
   947
        SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@5246
   948
        displayIndex = (window->x & 0xFFFF);
slouken@5246
   949
        if (displayIndex >= _this->num_displays) {
slouken@5246
   950
            displayIndex = 0;
slouken@5246
   951
        }
slouken@5246
   952
        return displayIndex;
slouken@5246
   953
    }
slouken@5246
   954
    if (SDL_WINDOWPOS_ISUNDEFINED(window->y) ||
slouken@5246
   955
        SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@5246
   956
        displayIndex = (window->y & 0xFFFF);
slouken@5246
   957
        if (displayIndex >= _this->num_displays) {
slouken@5246
   958
            displayIndex = 0;
slouken@5246
   959
        }
slouken@5246
   960
        return displayIndex;
slouken@5246
   961
    }
slouken@5246
   962
slouken@5246
   963
    /* Find the display containing the window */
slouken@6547
   964
    for (i = 0; i < _this->num_displays; ++i) {
slouken@6547
   965
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@6547
   966
slouken@6547
   967
        if (display->fullscreen_window == window) {
slouken@6547
   968
            return i;
slouken@6547
   969
        }
slouken@6547
   970
    }
slouken@5246
   971
    center.x = window->x + window->w / 2;
slouken@5246
   972
    center.y = window->y + window->h / 2;
slouken@5246
   973
    for (i = 0; i < _this->num_displays; ++i) {
slouken@5246
   974
        SDL_GetDisplayBounds(i, &rect);
slouken@6547
   975
        if (SDL_EnclosePoints(&center, 1, &rect, NULL)) {
slouken@5246
   976
            return i;
slouken@5246
   977
        }
slouken@5246
   978
slouken@5246
   979
        delta.x = center.x - (rect.x + rect.w / 2);
slouken@5246
   980
        delta.y = center.y - (rect.y + rect.h / 2);
slouken@5246
   981
        dist = (delta.x*delta.x + delta.y*delta.y);
slouken@5246
   982
        if (dist < closest_dist) {
slouken@5246
   983
            closest = i;
slouken@5246
   984
            closest_dist = dist;
slouken@5246
   985
        }
slouken@5246
   986
    }
slouken@5246
   987
    if (closest < 0) {
slouken@5246
   988
        SDL_SetError("Couldn't find any displays");
slouken@5246
   989
    }
slouken@5246
   990
    return closest;
slouken@5246
   991
}
slouken@5246
   992
slouken@5246
   993
SDL_VideoDisplay *
slouken@5246
   994
SDL_GetDisplayForWindow(SDL_Window *window)
slouken@5246
   995
{
slouken@6786
   996
    int displayIndex = SDL_GetWindowDisplayIndex(window);
slouken@5373
   997
    if (displayIndex >= 0) {
slouken@5373
   998
        return &_this->displays[displayIndex];
slouken@5373
   999
    } else {
slouken@5373
  1000
        return NULL;
slouken@5373
  1001
    }
slouken@5246
  1002
}
slouken@5246
  1003
slouken@5246
  1004
int
slouken@3685
  1005
SDL_SetWindowDisplayMode(SDL_Window * window, const SDL_DisplayMode * mode)
slouken@3500
  1006
{
slouken@3695
  1007
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3500
  1008
slouken@2753
  1009
    if (mode) {
slouken@3500
  1010
        window->fullscreen_mode = *mode;
slouken@3500
  1011
    } else {
slouken@3500
  1012
        SDL_zero(window->fullscreen_mode);
slouken@3500
  1013
    }
slouken@3526
  1014
    return 0;
slouken@3500
  1015
}
slouken@3500
  1016
slouken@3500
  1017
int
slouken@3685
  1018
SDL_GetWindowDisplayMode(SDL_Window * window, SDL_DisplayMode * mode)
slouken@3500
  1019
{
slouken@3500
  1020
    SDL_DisplayMode fullscreen_mode;
slouken@6755
  1021
	SDL_VideoDisplay *display;
slouken@3500
  1022
aschiffler@6936
  1023
    if (!mode) {
aschiffler@6936
  1024
      SDL_InvalidParamError("mode");
aschiffler@6936
  1025
      return -1;
aschiffler@6936
  1026
    }
aschiffler@6936
  1027
    
slouken@3695
  1028
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3500
  1029
slouken@3500
  1030
    fullscreen_mode = window->fullscreen_mode;
slouken@3500
  1031
    if (!fullscreen_mode.w) {
slouken@3500
  1032
        fullscreen_mode.w = window->w;
slouken@3500
  1033
    }
slouken@3500
  1034
    if (!fullscreen_mode.h) {
slouken@3500
  1035
        fullscreen_mode.h = window->h;
slouken@3500
  1036
    }
slouken@6755
  1037
	
slouken@6755
  1038
	display = SDL_GetDisplayForWindow(window);
slouken@6755
  1039
slouken@6755
  1040
	/* if in desktop size mode, just return the size of the desktop */
slouken@6755
  1041
	if ( ( window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) == SDL_WINDOW_FULLSCREEN_DESKTOP ) 
slouken@6755
  1042
	{
slouken@6755
  1043
		fullscreen_mode = display->desktop_mode;
slouken@6755
  1044
	}
slouken@6755
  1045
	else if (!SDL_GetClosestDisplayModeForDisplay(SDL_GetDisplayForWindow(window),
slouken@3500
  1046
                                             &fullscreen_mode,
slouken@3500
  1047
                                             &fullscreen_mode)) {
slouken@3500
  1048
        SDL_SetError("Couldn't find display mode match");
slouken@3500
  1049
        return -1;
slouken@3500
  1050
    }
slouken@3500
  1051
slouken@3500
  1052
    if (mode) {
slouken@3500
  1053
        *mode = fullscreen_mode;
slouken@2753
  1054
    }
slouken@2753
  1055
    return 0;
slouken@0
  1056
}
slouken@0
  1057
slouken@5154
  1058
Uint32
slouken@5154
  1059
SDL_GetWindowPixelFormat(SDL_Window * window)
slouken@5154
  1060
{
slouken@5246
  1061
    SDL_VideoDisplay *display;
slouken@5246
  1062
slouken@5246
  1063
    CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
slouken@5246
  1064
slouken@5246
  1065
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  1066
    return display->current_mode.format;
slouken@5154
  1067
}
slouken@5154
  1068
slouken@3502
  1069
static void
slouken@5479
  1070
SDL_RestoreMousePosition(SDL_Window *window)
slouken@5479
  1071
{
slouken@5479
  1072
    int x, y;
slouken@5479
  1073
slouken@5479
  1074
    if (window == SDL_GetMouseFocus()) {
slouken@5479
  1075
        SDL_GetMouseState(&x, &y);
slouken@5479
  1076
        SDL_WarpMouseInWindow(window, x, y);
slouken@5479
  1077
    }
slouken@5479
  1078
}
slouken@5479
  1079
slouken@5479
  1080
static void
slouken@5404
  1081
SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool fullscreen)
slouken@3502
  1082
{
slouken@5246
  1083
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5249
  1084
    SDL_Window *other;
slouken@3502
  1085
slouken@5404
  1086
    if (fullscreen) {
slouken@3502
  1087
        /* Hide any other fullscreen windows */
slouken@5249
  1088
        if (display->fullscreen_window &&
slouken@5249
  1089
            display->fullscreen_window != window) {
slouken@5246
  1090
            SDL_MinimizeWindow(display->fullscreen_window);
slouken@3502
  1091
        }
slouken@3502
  1092
    }
slouken@3502
  1093
slouken@5305
  1094
    /* See if anything needs to be done now */
slouken@5404
  1095
    if ((display->fullscreen_window == window) == fullscreen) {
slouken@5305
  1096
        return;
slouken@5305
  1097
    }
slouken@5305
  1098
slouken@5249
  1099
    /* See if there are any fullscreen windows */
slouken@5249
  1100
    for (other = _this->windows; other; other = other->next) {
slouken@5404
  1101
        SDL_bool setDisplayMode = SDL_FALSE;
slouken@5404
  1102
slouken@5404
  1103
        if (other == window) {
slouken@5404
  1104
            setDisplayMode = fullscreen;
slouken@5404
  1105
        } else if (FULLSCREEN_VISIBLE(other) &&
slouken@5404
  1106
                   SDL_GetDisplayForWindow(other) == display) {
slouken@5404
  1107
            setDisplayMode = SDL_TRUE;
slouken@5404
  1108
        }
slouken@5404
  1109
slouken@5404
  1110
        if (setDisplayMode) {
slouken@5249
  1111
            SDL_DisplayMode fullscreen_mode;
slouken@5404
  1112
slouken@5249
  1113
            if (SDL_GetWindowDisplayMode(other, &fullscreen_mode) == 0) {
slouken@5306
  1114
                SDL_bool resized = SDL_TRUE;
slouken@5306
  1115
slouken@5306
  1116
                if (other->w == fullscreen_mode.w && other->h == fullscreen_mode.h) {
slouken@5306
  1117
                    resized = SDL_FALSE;
slouken@5249
  1118
                }
slouken@3502
  1119
slouken@6755
  1120
				/* only do the mode change if we want exclusive fullscreen */
slouken@6755
  1121
				if ( ( window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) != SDL_WINDOW_FULLSCREEN_DESKTOP ) 
slouken@6755
  1122
					SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
slouken@6755
  1123
				else
slouken@6755
  1124
					SDL_SetDisplayModeForDisplay(display, NULL);
slouken@6755
  1125
slouken@6755
  1126
				
slouken@5249
  1127
                if (_this->SetWindowFullscreen) {
slouken@5305
  1128
                    _this->SetWindowFullscreen(_this, other, display, SDL_TRUE);
slouken@5249
  1129
                }
slouken@5249
  1130
                display->fullscreen_window = other;
slouken@5249
  1131
slouken@5306
  1132
                /* Generate a mode change event here */
slouken@5306
  1133
                if (resized) {
slouken@5306
  1134
                    SDL_SendWindowEvent(other, SDL_WINDOWEVENT_RESIZED,
slouken@5306
  1135
                                        fullscreen_mode.w, fullscreen_mode.h);
slouken@5306
  1136
                } else {
slouken@5306
  1137
                    SDL_OnWindowResized(other);
slouken@5306
  1138
                }
slouken@5479
  1139
slouken@5479
  1140
                SDL_RestoreMousePosition(other);
slouken@3502
  1141
                return;
slouken@3502
  1142
            }
slouken@3502
  1143
        }
slouken@3502
  1144
    }
slouken@3502
  1145
slouken@3502
  1146
    /* Nope, restore the desktop mode */
slouken@3502
  1147
    SDL_SetDisplayModeForDisplay(display, NULL);
slouken@5249
  1148
slouken@5249
  1149
    if (_this->SetWindowFullscreen) {
slouken@5305
  1150
        _this->SetWindowFullscreen(_this, window, display, SDL_FALSE);
slouken@5249
  1151
    }
slouken@3517
  1152
    display->fullscreen_window = NULL;
slouken@5306
  1153
slouken@5306
  1154
    /* Generate a mode change event here */
slouken@5306
  1155
    SDL_OnWindowResized(window);
slouken@5479
  1156
slouken@5479
  1157
    /* Restore the cursor position */
slouken@5479
  1158
    SDL_RestoreMousePosition(window);
slouken@3502
  1159
}
slouken@3502
  1160
slouken@5380
  1161
#define CREATE_FLAGS \
slouken@5380
  1162
    (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE)
slouken@5380
  1163
slouken@5380
  1164
static void
slouken@5380
  1165
SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
slouken@5380
  1166
{
slouken@5398
  1167
    window->windowed.x = window->x;
slouken@5398
  1168
    window->windowed.y = window->y;
slouken@5398
  1169
    window->windowed.w = window->w;
slouken@5398
  1170
    window->windowed.h = window->h;
slouken@5398
  1171
slouken@5380
  1172
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@5380
  1173
        SDL_MaximizeWindow(window);
slouken@5380
  1174
    }
slouken@5380
  1175
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@5380
  1176
        SDL_MinimizeWindow(window);
slouken@5380
  1177
    }
slouken@5380
  1178
    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@6755
  1179
        SDL_SetWindowFullscreen(window, flags);
slouken@5380
  1180
    }
slouken@5380
  1181
    if (flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@5380
  1182
        SDL_SetWindowGrab(window, SDL_TRUE);
slouken@5380
  1183
    }
slouken@5380
  1184
    if (!(flags & SDL_WINDOW_HIDDEN)) {
slouken@5380
  1185
        SDL_ShowWindow(window);
slouken@5380
  1186
    }
slouken@5380
  1187
}
slouken@5380
  1188
slouken@3685
  1189
SDL_Window *
slouken@1895
  1190
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@0
  1191
{
slouken@3685
  1192
    SDL_Window *window;
slouken@0
  1193
slouken@2753
  1194
    if (!_this) {
slouken@3417
  1195
        /* Initialize the video system if needed */
slouken@5123
  1196
        if (SDL_VideoInit(NULL) < 0) {
slouken@3685
  1197
            return NULL;
slouken@3417
  1198
        }
slouken@2753
  1199
    }
slouken@5249
  1200
slouken@6266
  1201
    /* Some platforms can't create zero-sized windows */
slouken@6266
  1202
    if (w < 1) {
slouken@6266
  1203
        w = 1;
slouken@6266
  1204
    }
slouken@6266
  1205
    if (h < 1) {
slouken@6266
  1206
        h = 1;
slouken@6266
  1207
    }
slouken@6266
  1208
slouken@5249
  1209
    /* Some platforms have OpenGL enabled by default */
slouken@6188
  1210
#if (SDL_VIDEO_OPENGL && __MACOSX__) || __IPHONEOS__ || __ANDROID__
slouken@5249
  1211
    flags |= SDL_WINDOW_OPENGL;
slouken@5249
  1212
#endif
slouken@3057
  1213
    if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1214
        if (!_this->GL_CreateContext) {
slouken@3057
  1215
            SDL_SetError("No OpenGL support in video driver");
slouken@3685
  1216
            return NULL;
slouken@3057
  1217
        }
slouken@6611
  1218
        if (SDL_GL_LoadLibrary(NULL) < 0) {
slouken@6611
  1219
            return NULL;
slouken@6611
  1220
        }
slouken@2753
  1221
    }
slouken@3685
  1222
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3695
  1223
    window->magic = &_this->window_magic;
slouken@3685
  1224
    window->id = _this->next_object_id++;
slouken@3685
  1225
    window->x = x;
slouken@3685
  1226
    window->y = y;
slouken@3685
  1227
    window->w = w;
slouken@3685
  1228
    window->h = h;
slouken@5380
  1229
    if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
slouken@5380
  1230
        SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5380
  1231
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5380
  1232
        int displayIndex;
slouken@5380
  1233
        SDL_Rect bounds;
slouken@5380
  1234
slouken@5380
  1235
        displayIndex = SDL_GetIndexOfDisplay(display);
slouken@5380
  1236
        SDL_GetDisplayBounds(displayIndex, &bounds);
slouken@5404
  1237
        if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) {
slouken@5380
  1238
            window->x = bounds.x + (bounds.w - w) / 2;
slouken@5380
  1239
        }
slouken@5380
  1240
        if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5380
  1241
            window->y = bounds.y + (bounds.h - h) / 2;
slouken@5380
  1242
        }
slouken@5380
  1243
    }
slouken@5380
  1244
    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
slouken@5466
  1245
    window->brightness = 1.0f;
slouken@5246
  1246
    window->next = _this->windows;
slouken@5246
  1247
    if (_this->windows) {
slouken@5246
  1248
        _this->windows->prev = window;
slouken@3693
  1249
    }
slouken@5246
  1250
    _this->windows = window;
slouken@3685
  1251
slouken@3685
  1252
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@3685
  1253
        SDL_DestroyWindow(window);
slouken@3685
  1254
        return NULL;
slouken@3685
  1255
    }
slouken@0
  1256
slouken@2753
  1257
    if (title) {
slouken@3685
  1258
        SDL_SetWindowTitle(window, title);
slouken@2753
  1259
    }
slouken@5380
  1260
    SDL_FinishWindowCreation(window, flags);
slouken@6063
  1261
    
slouken@6063
  1262
    /* If the window was created fullscreen, make sure the mode code matches */
slouken@6063
  1263
    SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
slouken@3685
  1264
slouken@3685
  1265
    return window;
slouken@1895
  1266
}
slouken@0
  1267
slouken@3685
  1268
SDL_Window *
slouken@1895
  1269
SDL_CreateWindowFrom(const void *data)
slouken@1895
  1270
{
slouken@3685
  1271
    SDL_Window *window;
slouken@0
  1272
slouken@2753
  1273
    if (!_this) {
slouken@2753
  1274
        SDL_UninitializedVideo();
slouken@3685
  1275
        return NULL;
slouken@3685
  1276
    }
slouken@3685
  1277
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@3695
  1278
    window->magic = &_this->window_magic;
slouken@3685
  1279
    window->id = _this->next_object_id++;
slouken@3685
  1280
    window->flags = SDL_WINDOW_FOREIGN;
slouken@5466
  1281
    window->brightness = 1.0f;
slouken@5246
  1282
    window->next = _this->windows;
slouken@5246
  1283
    if (_this->windows) {
slouken@5246
  1284
        _this->windows->prev = window;
slouken@3693
  1285
    }
slouken@5246
  1286
    _this->windows = window;
slouken@1895
  1287
slouken@2753
  1288
    if (!_this->CreateWindowFrom ||
slouken@3685
  1289
        _this->CreateWindowFrom(_this, window, data) < 0) {
slouken@3685
  1290
        SDL_DestroyWindow(window);
slouken@3685
  1291
        return NULL;
slouken@3685
  1292
    }
slouken@3685
  1293
    return window;
slouken@1895
  1294
}
slouken@1895
  1295
slouken@1924
  1296
int
slouken@1928
  1297
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
  1298
{
slouken@2753
  1299
    char *title = window->title;
slouken@1956
  1300
slouken@2753
  1301
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
slouken@2753
  1302
        SDL_SetError("No OpenGL support in video driver");
slouken@2753
  1303
        return -1;
slouken@2753
  1304
    }
slouken@3057
  1305
slouken@3057
  1306
    if (window->flags & SDL_WINDOW_FOREIGN) {
slouken@3057
  1307
        /* Can't destroy and re-create foreign windows, hrm */
slouken@3057
  1308
        flags |= SDL_WINDOW_FOREIGN;
slouken@3057
  1309
    } else {
slouken@3057
  1310
        flags &= ~SDL_WINDOW_FOREIGN;
slouken@3057
  1311
    }
slouken@3057
  1312
slouken@5249
  1313
    /* Restore video mode, etc. */
slouken@5305
  1314
    SDL_HideWindow(window);
slouken@5249
  1315
slouken@5249
  1316
    /* Tear down the old native window */
slouken@5249
  1317
    if (window->surface) {
slouken@5288
  1318
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5249
  1319
        SDL_FreeSurface(window->surface);
slouken@5249
  1320
    }
slouken@5249
  1321
    if (_this->DestroyWindowFramebuffer) {
slouken@5249
  1322
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5249
  1323
    }
slouken@3057
  1324
    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@2753
  1325
        _this->DestroyWindow(_this, window);
slouken@2753
  1326
    }
slouken@3057
  1327
slouken@5249
  1328
    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
slouken@5249
  1329
        if (flags & SDL_WINDOW_OPENGL) {
slouken@6775
  1330
            if (SDL_GL_LoadLibrary(NULL) < 0) {
slouken@6775
  1331
                return -1;
slouken@6775
  1332
            }
slouken@5249
  1333
        } else {
slouken@5249
  1334
            SDL_GL_UnloadLibrary();
slouken@5249
  1335
        }
slouken@5249
  1336
    }
slouken@5249
  1337
slouken@2753
  1338
    window->title = NULL;
slouken@5380
  1339
    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
slouken@1956
  1340
slouken@3057
  1341
    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@3057
  1342
        if (_this->CreateWindow(_this, window) < 0) {
slouken@3057
  1343
            if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1344
                SDL_GL_UnloadLibrary();
slouken@3057
  1345
            }
slouken@3057
  1346
            return -1;
slouken@3057
  1347
        }
slouken@2753
  1348
    }
slouken@3057
  1349
slouken@2753
  1350
    if (title) {
slouken@3685
  1351
        SDL_SetWindowTitle(window, title);
slouken@2753
  1352
        SDL_free(title);
slouken@2753
  1353
    }
slouken@5380
  1354
    SDL_FinishWindowCreation(window, flags);
slouken@2875
  1355
slouken@2753
  1356
    return 0;
slouken@1924
  1357
}
slouken@1924
  1358
slouken@1895
  1359
Uint32
slouken@3685
  1360
SDL_GetWindowID(SDL_Window * window)
slouken@1895
  1361
{
slouken@3695
  1362
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1363
slouken@3685
  1364
    return window->id;
slouken@3685
  1365
}
slouken@3685
  1366
slouken@3685
  1367
SDL_Window *
slouken@3685
  1368
SDL_GetWindowFromID(Uint32 id)
slouken@3685
  1369
{
slouken@3685
  1370
    SDL_Window *window;
slouken@3685
  1371
slouken@3685
  1372
    if (!_this) {
slouken@3685
  1373
        return NULL;
slouken@3685
  1374
    }
slouken@5246
  1375
    for (window = _this->windows; window; window = window->next) {
slouken@5246
  1376
        if (window->id == id) {
slouken@5246
  1377
            return window;
slouken@3685
  1378
        }
slouken@3685
  1379
    }
slouken@3685
  1380
    return NULL;
slouken@3685
  1381
}
slouken@3685
  1382
slouken@3685
  1383
Uint32
slouken@3685
  1384
SDL_GetWindowFlags(SDL_Window * window)
slouken@3685
  1385
{
slouken@3695
  1386
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1387
slouken@2753
  1388
    return window->flags;
slouken@1895
  1389
}
slouken@1895
  1390
slouken@1895
  1391
void
slouken@3685
  1392
SDL_SetWindowTitle(SDL_Window * window, const char *title)
slouken@1895
  1393
{
slouken@3695
  1394
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1395
slouken@3695
  1396
    if (title == window->title) {
slouken@2753
  1397
        return;
slouken@2753
  1398
    }
slouken@2753
  1399
    if (window->title) {
slouken@2753
  1400
        SDL_free(window->title);
slouken@2753
  1401
    }
slouken@4871
  1402
    if (title && *title) {
slouken@2753
  1403
        window->title = SDL_strdup(title);
slouken@2753
  1404
    } else {
slouken@2753
  1405
        window->title = NULL;
slouken@2753
  1406
    }
slouken@1956
  1407
slouken@2753
  1408
    if (_this->SetWindowTitle) {
slouken@2753
  1409
        _this->SetWindowTitle(_this, window);
slouken@2753
  1410
    }
slouken@1895
  1411
}
slouken@1895
  1412
slouken@2753
  1413
const char *
slouken@3685
  1414
SDL_GetWindowTitle(SDL_Window * window)
slouken@1895
  1415
{
slouken@4872
  1416
    CHECK_WINDOW_MAGIC(window, "");
slouken@3695
  1417
slouken@4871
  1418
    return window->title ? window->title : "";
slouken@1895
  1419
}
slouken@1895
  1420
slouken@1895
  1421
void
slouken@3685
  1422
SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
slouken@2967
  1423
{
slouken@3695
  1424
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1425
slouken@5375
  1426
    if (!icon) {
slouken@5375
  1427
        return;
slouken@5375
  1428
    }
slouken@5375
  1429
slouken@2967
  1430
    if (_this->SetWindowIcon) {
slouken@2967
  1431
        _this->SetWindowIcon(_this, window, icon);
slouken@2967
  1432
    }
slouken@2967
  1433
}
slouken@2967
  1434
slouken@5165
  1435
void*
slouken@5165
  1436
SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
slouken@1895
  1437
{
slouken@5165
  1438
    SDL_WindowUserData *prev, *data;
slouken@3695
  1439
slouken@5165
  1440
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5165
  1441
slouken@5165
  1442
    /* See if the named data already exists */
slouken@5165
  1443
    prev = NULL;
slouken@5165
  1444
    for (data = window->data; data; prev = data, data = data->next) {
slouken@5165
  1445
        if (SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1446
            void *last_value = data->data;
slouken@5165
  1447
slouken@5165
  1448
            if (userdata) {
slouken@5165
  1449
                /* Set the new value */
slouken@5165
  1450
                data->data = userdata;
slouken@5165
  1451
            } else {
slouken@5165
  1452
                /* Delete this value */
slouken@5165
  1453
                if (prev) {
slouken@5165
  1454
                    prev->next = data->next;
slouken@5165
  1455
                } else {
slouken@5165
  1456
                    window->data = data->next;
slouken@5165
  1457
                }
slouken@5165
  1458
                SDL_free(data->name);
slouken@5165
  1459
                SDL_free(data);
slouken@5165
  1460
            }
slouken@5165
  1461
            return last_value;
slouken@5165
  1462
        }
slouken@5165
  1463
    }
slouken@5165
  1464
slouken@5165
  1465
    /* Add new data to the window */
slouken@5165
  1466
    if (userdata) {
slouken@5165
  1467
        data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
slouken@5165
  1468
        data->name = SDL_strdup(name);
slouken@5165
  1469
        data->data = userdata;
slouken@5165
  1470
        data->next = window->data;
slouken@5165
  1471
        window->data = data;
slouken@5165
  1472
    }
slouken@5165
  1473
    return NULL;
slouken@1895
  1474
}
slouken@1895
  1475
slouken@2753
  1476
void *
slouken@5165
  1477
SDL_GetWindowData(SDL_Window * window, const char *name)
slouken@1895
  1478
{
slouken@5165
  1479
    SDL_WindowUserData *data;
slouken@5165
  1480
slouken@3695
  1481
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  1482
slouken@5165
  1483
    for (data = window->data; data; data = data->next) {
slouken@5165
  1484
        if (SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1485
            return data->data;
slouken@5165
  1486
        }
slouken@5165
  1487
    }
slouken@5165
  1488
    return NULL;
slouken@1895
  1489
}
slouken@1895
  1490
slouken@1895
  1491
void
slouken@3685
  1492
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1493
{
slouken@3695
  1494
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1495
slouken@5305
  1496
    if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
slouken@2753
  1497
        window->x = x;
slouken@2753
  1498
    }
slouken@5305
  1499
    if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
slouken@2753
  1500
        window->y = y;
slouken@2753
  1501
    }
slouken@5478
  1502
    if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5478
  1503
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5478
  1504
        int displayIndex;
slouken@5478
  1505
        SDL_Rect bounds;
slouken@5478
  1506
slouken@5478
  1507
        displayIndex = SDL_GetIndexOfDisplay(display);
slouken@5478
  1508
        SDL_GetDisplayBounds(displayIndex, &bounds);
slouken@5478
  1509
        if (SDL_WINDOWPOS_ISCENTERED(x)) {
slouken@5478
  1510
            window->x = bounds.x + (bounds.w - window->w) / 2;
slouken@5478
  1511
        }
slouken@5478
  1512
        if (SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5478
  1513
            window->y = bounds.y + (bounds.h - window->h) / 2;
slouken@5478
  1514
        }
slouken@5478
  1515
    }
slouken@5246
  1516
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5246
  1517
        if (_this->SetWindowPosition) {
slouken@5246
  1518
            _this->SetWindowPosition(_this, window);
slouken@5246
  1519
        }
slouken@5246
  1520
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@2753
  1521
    }
slouken@1895
  1522
}
slouken@1895
  1523
slouken@1895
  1524
void
slouken@3685
  1525
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1526
{
slouken@5246
  1527
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1528
slouken@5246
  1529
    /* Fullscreen windows are always at their display's origin */
slouken@5246
  1530
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
aschiffler@6956
  1531
        if (x) {
aschiffler@6956
  1532
            *x = 0;
aschiffler@6956
  1533
        }
aschiffler@6956
  1534
        if (y) {
aschiffler@6956
  1535
            *y = 0;
aschiffler@6956
  1536
        }
slouken@5246
  1537
    } else {
slouken@5242
  1538
        if (x) {
slouken@5242
  1539
            *x = window->x;
slouken@5242
  1540
        }
slouken@5242
  1541
        if (y) {
slouken@5242
  1542
            *y = window->y;
slouken@5242
  1543
        }
slouken@2753
  1544
    }
slouken@1895
  1545
}
slouken@1895
  1546
slouken@1895
  1547
void
icculus@6422
  1548
SDL_SetWindowBordered(SDL_Window * window, SDL_bool bordered)
icculus@6422
  1549
{
icculus@6422
  1550
    CHECK_WINDOW_MAGIC(window, );
icculus@6422
  1551
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
icculus@6422
  1552
        const int want = (bordered != SDL_FALSE);  /* normalize the flag. */
icculus@6422
  1553
        const int have = ((window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6422
  1554
        if ((want != have) && (_this->SetWindowBordered)) {
icculus@6422
  1555
            if (want) {
icculus@6422
  1556
                window->flags &= ~SDL_WINDOW_BORDERLESS;
icculus@6422
  1557
            } else {
icculus@6422
  1558
                window->flags |= SDL_WINDOW_BORDERLESS;
icculus@6422
  1559
            }
icculus@6422
  1560
            _this->SetWindowBordered(_this, window, (SDL_bool) want);
icculus@6422
  1561
        }
icculus@6422
  1562
    }
icculus@6422
  1563
}
icculus@6422
  1564
icculus@6422
  1565
void
slouken@3685
  1566
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1567
{
slouken@3695
  1568
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1569
slouken@5249
  1570
    /* FIXME: Should this change fullscreen modes? */
slouken@5249
  1571
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@5276
  1572
        window->w = w;
slouken@5276
  1573
        window->h = h;
slouken@5249
  1574
        if (_this->SetWindowSize) {
slouken@5249
  1575
            _this->SetWindowSize(_this, window);
slouken@5249
  1576
        }
slouken@5276
  1577
        if (window->w == w && window->h == h) {
slouken@5276
  1578
            /* We didn't get a SDL_WINDOWEVENT_RESIZED event (by design) */
slouken@5276
  1579
            SDL_OnWindowResized(window);
slouken@5276
  1580
        }
slouken@2753
  1581
    }
slouken@1895
  1582
}
slouken@1895
  1583
slouken@1895
  1584
void
slouken@3685
  1585
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1586
{
slouken@5246
  1587
    int dummy;
slouken@5246
  1588
slouken@5246
  1589
    if (!w) {
slouken@5246
  1590
        w = &dummy;
slouken@5246
  1591
    }
slouken@5246
  1592
    if (!h) {
slouken@5246
  1593
        h = &dummy;
slouken@5246
  1594
    }
slouken@5246
  1595
slouken@5246
  1596
    *w = 0;
slouken@5246
  1597
    *h = 0;
slouken@5246
  1598
slouken@5246
  1599
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1600
slouken@5242
  1601
    if (_this && window && window->magic == &_this->window_magic) {
stopiccot@6681
  1602
        *w = window->w;
stopiccot@6681
  1603
        *h = window->h;
stopiccot@6681
  1604
    }
stopiccot@6681
  1605
}
stopiccot@6681
  1606
stopiccot@6681
  1607
void
stopiccot@6681
  1608
SDL_SetWindowMinimumSize(SDL_Window * window, int min_w, int min_h)
stopiccot@6681
  1609
{
stopiccot@6681
  1610
    CHECK_WINDOW_MAGIC(window, );
stopiccot@6681
  1611
    
stopiccot@6681
  1612
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
stopiccot@6681
  1613
        window->min_w = min_w;
stopiccot@6681
  1614
        window->min_h = min_h;
stopiccot@6681
  1615
        if (_this->SetWindowMinimumSize) {
stopiccot@6681
  1616
            _this->SetWindowMinimumSize(_this, window);
slouken@2849
  1617
        }
stopiccot@6681
  1618
        /* Ensure that window is not smaller than minimal size */
stopiccot@6681
  1619
        SDL_SetWindowSize(window, SDL_max(window->w, window->min_w), SDL_max(window->h, window->min_h));
stopiccot@6681
  1620
    }
stopiccot@6681
  1621
}
stopiccot@6681
  1622
stopiccot@6681
  1623
void
stopiccot@6681
  1624
SDL_GetWindowMinimumSize(SDL_Window * window, int *min_w, int *min_h)
stopiccot@6681
  1625
{
stopiccot@6681
  1626
    int dummy;
stopiccot@6681
  1627
    
stopiccot@6681
  1628
    if (!min_w) {
stopiccot@6681
  1629
        min_w = &dummy;
stopiccot@6681
  1630
    }
stopiccot@6681
  1631
    if (!min_h) {
stopiccot@6681
  1632
        min_h = &dummy;
stopiccot@6681
  1633
    }
stopiccot@6681
  1634
    
stopiccot@6681
  1635
    *min_w = 0;
stopiccot@6681
  1636
    *min_h = 0;
stopiccot@6681
  1637
    
stopiccot@6681
  1638
    CHECK_WINDOW_MAGIC(window, );
stopiccot@6681
  1639
    
stopiccot@6681
  1640
    if (_this && window && window->magic == &_this->window_magic) {
stopiccot@6681
  1641
        *min_w = window->min_w;
stopiccot@6681
  1642
        *min_h = window->min_h;
slouken@2753
  1643
    }
slouken@1895
  1644
}
slouken@1895
  1645
slouken@1895
  1646
void
slouken@6788
  1647
SDL_SetWindowMaximumSize(SDL_Window * window, int max_w, int max_h)
slouken@6788
  1648
{
slouken@6788
  1649
    CHECK_WINDOW_MAGIC(window, );
slouken@6788
  1650
    
slouken@6788
  1651
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@6788
  1652
        window->max_w = max_w;
slouken@6788
  1653
        window->max_h = max_h;
slouken@6788
  1654
        if (_this->SetWindowMaximumSize) {
slouken@6788
  1655
            _this->SetWindowMaximumSize(_this, window);
slouken@6788
  1656
        }
slouken@6788
  1657
        /* Ensure that window is not larger than maximal size */
slouken@6788
  1658
        SDL_SetWindowSize(window, SDL_min(window->w, window->max_w), SDL_min(window->h, window->max_h));
slouken@6788
  1659
    }
slouken@6788
  1660
}
slouken@6788
  1661
slouken@6788
  1662
void
slouken@6788
  1663
SDL_GetWindowMaximumSize(SDL_Window * window, int *max_w, int *max_h)
slouken@6788
  1664
{
slouken@6788
  1665
    int dummy;
slouken@6788
  1666
    
slouken@6788
  1667
    if (!max_w) {
slouken@6788
  1668
        max_w = &dummy;
slouken@6788
  1669
    }
slouken@6788
  1670
    if (!max_h) {
slouken@6788
  1671
        max_h = &dummy;
slouken@6788
  1672
    }
slouken@6788
  1673
    
slouken@6788
  1674
    *max_w = 0;
slouken@6788
  1675
    *max_h = 0;
slouken@6788
  1676
    
slouken@6788
  1677
    CHECK_WINDOW_MAGIC(window, );
slouken@6788
  1678
    
slouken@6788
  1679
    if (_this && window && window->magic == &_this->window_magic) {
slouken@6788
  1680
        *max_w = window->max_w;
slouken@6788
  1681
        *max_h = window->max_h;
slouken@6788
  1682
    }
slouken@6788
  1683
}
slouken@6788
  1684
slouken@6788
  1685
void
slouken@3685
  1686
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1687
{
slouken@3695
  1688
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1689
slouken@3695
  1690
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@2753
  1691
        return;
slouken@2753
  1692
    }
slouken@1895
  1693
slouken@2753
  1694
    if (_this->ShowWindow) {
slouken@2753
  1695
        _this->ShowWindow(_this, window);
slouken@2753
  1696
    }
slouken@3685
  1697
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1698
}
slouken@1895
  1699
slouken@1895
  1700
void
slouken@3685
  1701
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1702
{
slouken@3695
  1703
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1704
slouken@3695
  1705
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1706
        return;
slouken@2753
  1707
    }
slouken@1895
  1708
slouken@5404
  1709
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@5404
  1710
slouken@2753
  1711
    if (_this->HideWindow) {
slouken@2753
  1712
        _this->HideWindow(_this, window);
slouken@2753
  1713
    }
slouken@3685
  1714
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1715
}
slouken@1895
  1716
slouken@1895
  1717
void
slouken@3685
  1718
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1719
{
slouken@3695
  1720
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1721
slouken@3695
  1722
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1723
        return;
slouken@2753
  1724
    }
slouken@2753
  1725
    if (_this->RaiseWindow) {
slouken@2753
  1726
        _this->RaiseWindow(_this, window);
slouken@2753
  1727
    }
slouken@1895
  1728
}
slouken@1895
  1729
slouken@1895
  1730
void
slouken@3685
  1731
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1732
{
slouken@3695
  1733
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1734
slouken@3695
  1735
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
  1736
        return;
slouken@2753
  1737
    }
slouken@1895
  1738
slouken@2753
  1739
    if (_this->MaximizeWindow) {
slouken@2753
  1740
        _this->MaximizeWindow(_this, window);
slouken@2753
  1741
    }
slouken@1895
  1742
}
slouken@1895
  1743
slouken@1895
  1744
void
slouken@3685
  1745
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1746
{
slouken@3695
  1747
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1748
slouken@3695
  1749
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
  1750
        return;
slouken@2753
  1751
    }
slouken@1895
  1752
slouken@5404
  1753
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@5404
  1754
slouken@2753
  1755
    if (_this->MinimizeWindow) {
slouken@2753
  1756
        _this->MinimizeWindow(_this, window);
slouken@2753
  1757
    }
slouken@1895
  1758
}
slouken@1895
  1759
slouken@1895
  1760
void
slouken@3685
  1761
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1762
{
slouken@3695
  1763
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1764
slouken@3695
  1765
    if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1766
        return;
slouken@2753
  1767
    }
slouken@1895
  1768
slouken@2753
  1769
    if (_this->RestoreWindow) {
slouken@2753
  1770
        _this->RestoreWindow(_this, window);
slouken@2753
  1771
    }
slouken@1895
  1772
}
slouken@1895
  1773
slouken@6755
  1774
#define FULLSCREEN_MASK ( SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN )
slouken@1895
  1775
int
slouken@6755
  1776
SDL_SetWindowFullscreen(SDL_Window * window, Uint32 flags)
slouken@1895
  1777
{
slouken@3695
  1778
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1779
slouken@6755
  1780
	flags &= FULLSCREEN_MASK;
slouken@6755
  1781
	
slouken@6755
  1782
    if ( flags == (window->flags & FULLSCREEN_MASK) ) {
slouken@2753
  1783
        return 0;
slouken@2753
  1784
    }
slouken@6755
  1785
	
slouken@6755
  1786
	/* clear the previous flags and OR in the new ones */
slouken@6755
  1787
	window->flags &= ~FULLSCREEN_MASK;
slouken@6755
  1788
    window->flags |= flags;
slouken@6755
  1789
slouken@5404
  1790
    SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
slouken@1895
  1791
slouken@2753
  1792
    return 0;
slouken@1895
  1793
}
slouken@1895
  1794
slouken@5166
  1795
static SDL_Surface *
slouken@5166
  1796
SDL_CreateWindowFramebuffer(SDL_Window * window)
slouken@5166
  1797
{
slouken@5166
  1798
    Uint32 format;
slouken@5166
  1799
    void *pixels;
slouken@5166
  1800
    int pitch;
slouken@5166
  1801
    int bpp;
slouken@5166
  1802
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5166
  1803
slouken@5166
  1804
    if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
slouken@5166
  1805
        return NULL;
slouken@5166
  1806
    }
slouken@5166
  1807
slouken@5166
  1808
    if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
slouken@5166
  1809
        return NULL;
slouken@5166
  1810
    }
slouken@5166
  1811
slouken@5166
  1812
    if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5166
  1813
        return NULL;
slouken@5166
  1814
    }
slouken@5166
  1815
slouken@5166
  1816
    return SDL_CreateRGBSurfaceFrom(pixels, window->w, window->h, bpp, pitch, Rmask, Gmask, Bmask, Amask);
slouken@5166
  1817
}
slouken@5166
  1818
slouken@5166
  1819
SDL_Surface *
slouken@5166
  1820
SDL_GetWindowSurface(SDL_Window * window)
slouken@5166
  1821
{
slouken@5166
  1822
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5166
  1823
slouken@5172
  1824
    if (!window->surface_valid) {
slouken@5172
  1825
        if (window->surface) {
slouken@5288
  1826
            window->surface->flags &= ~SDL_DONTFREE;
slouken@5172
  1827
            SDL_FreeSurface(window->surface);
slouken@5172
  1828
        }
slouken@5166
  1829
        window->surface = SDL_CreateWindowFramebuffer(window);
slouken@5169
  1830
        if (window->surface) {
slouken@5172
  1831
            window->surface_valid = SDL_TRUE;
slouken@5288
  1832
            window->surface->flags |= SDL_DONTFREE;
slouken@5169
  1833
        }
slouken@5166
  1834
    }
slouken@5166
  1835
    return window->surface;
slouken@5166
  1836
}
slouken@5166
  1837
slouken@5166
  1838
int
slouken@5166
  1839
SDL_UpdateWindowSurface(SDL_Window * window)
slouken@5166
  1840
{
slouken@5166
  1841
    SDL_Rect full_rect;
slouken@5166
  1842
slouken@5166
  1843
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1844
slouken@5166
  1845
    full_rect.x = 0;
slouken@5166
  1846
    full_rect.y = 0;
slouken@5166
  1847
    full_rect.w = window->w;
slouken@5166
  1848
    full_rect.h = window->h;
slouken@5297
  1849
    return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
slouken@5166
  1850
}
slouken@5166
  1851
slouken@5166
  1852
int
slouken@5297
  1853
SDL_UpdateWindowSurfaceRects(SDL_Window * window, SDL_Rect * rects,
slouken@5297
  1854
                             int numrects)
slouken@5166
  1855
{
slouken@5166
  1856
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1857
slouken@5172
  1858
    if (!window->surface_valid) {
slouken@5166
  1859
        SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
slouken@5166
  1860
        return -1;
slouken@5166
  1861
    }
slouken@5166
  1862
slouken@5297
  1863
    return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
slouken@5166
  1864
}
slouken@5166
  1865
slouken@5466
  1866
int
slouken@5466
  1867
SDL_SetWindowBrightness(SDL_Window * window, float brightness)
slouken@5466
  1868
{
slouken@5466
  1869
    Uint16 ramp[256];
slouken@5466
  1870
    int status;
slouken@5466
  1871
slouken@5466
  1872
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1873
slouken@5466
  1874
    SDL_CalculateGammaRamp(brightness, ramp);
slouken@5466
  1875
    status = SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
slouken@5466
  1876
    if (status == 0) {
slouken@5466
  1877
        window->brightness = brightness;
slouken@5466
  1878
    }
slouken@5466
  1879
    return status;
slouken@5466
  1880
}
slouken@5466
  1881
slouken@5466
  1882
float
slouken@5466
  1883
SDL_GetWindowBrightness(SDL_Window * window)
slouken@5466
  1884
{
slouken@5466
  1885
    CHECK_WINDOW_MAGIC(window, 1.0f);
slouken@5466
  1886
slouken@5466
  1887
    return window->brightness;
slouken@5466
  1888
}
slouken@5466
  1889
slouken@5466
  1890
int
slouken@5466
  1891
SDL_SetWindowGammaRamp(SDL_Window * window, const Uint16 * red,
slouken@5466
  1892
                                            const Uint16 * green,
slouken@5466
  1893
                                            const Uint16 * blue)
slouken@5466
  1894
{
slouken@5466
  1895
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1896
slouken@5466
  1897
    if (!_this->SetWindowGammaRamp) {
slouken@5466
  1898
        SDL_Unsupported();
slouken@5466
  1899
        return -1;
slouken@5466
  1900
    }
slouken@5466
  1901
slouken@5466
  1902
    if (!window->gamma) {
slouken@5466
  1903
        if (SDL_GetWindowGammaRamp(window, NULL, NULL, NULL) < 0) {
slouken@5466
  1904
            return -1;
slouken@5466
  1905
        }
slouken@5466
  1906
    }
slouken@5466
  1907
slouken@5466
  1908
    if (red) {
slouken@5466
  1909
        SDL_memcpy(&window->gamma[0*256], red, 256*sizeof(Uint16));
slouken@5466
  1910
    }
slouken@5466
  1911
    if (green) {
slouken@5466
  1912
        SDL_memcpy(&window->gamma[1*256], green, 256*sizeof(Uint16));
slouken@5466
  1913
    }
slouken@5466
  1914
    if (blue) {
slouken@5466
  1915
        SDL_memcpy(&window->gamma[2*256], blue, 256*sizeof(Uint16));
slouken@5466
  1916
    }
slouken@5466
  1917
    if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@5466
  1918
        return _this->SetWindowGammaRamp(_this, window, window->gamma);
slouken@5466
  1919
    } else {
slouken@5466
  1920
        return 0;
slouken@5466
  1921
    }
slouken@5466
  1922
}
slouken@5466
  1923
slouken@5466
  1924
int
slouken@5466
  1925
SDL_GetWindowGammaRamp(SDL_Window * window, Uint16 * red,
slouken@5466
  1926
                                            Uint16 * green,
slouken@5466
  1927
                                            Uint16 * blue)
slouken@5466
  1928
{
slouken@5466
  1929
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1930
slouken@5466
  1931
    if (!window->gamma) {
slouken@5466
  1932
        int i;
slouken@5466
  1933
slouken@5466
  1934
        window->gamma = (Uint16 *)SDL_malloc(256*6*sizeof(Uint16));
slouken@5466
  1935
        if (!window->gamma) {
slouken@5466
  1936
            SDL_OutOfMemory();
slouken@5466
  1937
            return -1;
slouken@5466
  1938
        }
slouken@5466
  1939
        window->saved_gamma = window->gamma + 3*256;
slouken@5466
  1940
slouken@5466
  1941
        if (_this->GetWindowGammaRamp) {
slouken@5466
  1942
            if (_this->GetWindowGammaRamp(_this, window, window->gamma) < 0) {
slouken@5466
  1943
                return -1;
slouken@5466
  1944
            }
slouken@5466
  1945
        } else {
slouken@5466
  1946
            /* Create an identity gamma ramp */
slouken@5466
  1947
            for (i = 0; i < 256; ++i) {
slouken@5466
  1948
                Uint16 value = (Uint16)((i << 8) | i);
slouken@5466
  1949
slouken@5466
  1950
                window->gamma[0*256+i] = value;
slouken@5466
  1951
                window->gamma[1*256+i] = value;
slouken@5466
  1952
                window->gamma[2*256+i] = value;
slouken@5466
  1953
            }
slouken@5466
  1954
        }
slouken@5466
  1955
        SDL_memcpy(window->saved_gamma, window->gamma, 3*256*sizeof(Uint16));
slouken@5466
  1956
    }
slouken@5466
  1957
slouken@5466
  1958
    if (red) {
slouken@5466
  1959
        SDL_memcpy(red, &window->gamma[0*256], 256*sizeof(Uint16));
slouken@5466
  1960
    }
slouken@5466
  1961
    if (green) {
slouken@5466
  1962
        SDL_memcpy(green, &window->gamma[1*256], 256*sizeof(Uint16));
slouken@5466
  1963
    }
slouken@5466
  1964
    if (blue) {
slouken@5466
  1965
        SDL_memcpy(blue, &window->gamma[2*256], 256*sizeof(Uint16));
slouken@5466
  1966
    }
slouken@5466
  1967
    return 0;
slouken@5466
  1968
}
slouken@5466
  1969
slouken@6662
  1970
void
slouken@2876
  1971
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  1972
{
slouken@6662
  1973
    if (_this->SetWindowGrab) {
slouken@6662
  1974
        SDL_bool grabbed;
slouken@6662
  1975
        if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@6662
  1976
            (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@6662
  1977
            grabbed = SDL_TRUE;
slouken@6662
  1978
        } else {
slouken@6662
  1979
            grabbed = SDL_FALSE;
slouken@6662
  1980
        }
slouken@6662
  1981
        _this->SetWindowGrab(_this, window, grabbed);
slouken@2753
  1982
    }
slouken@1895
  1983
}
slouken@1895
  1984
slouken@5403
  1985
void
slouken@5403
  1986
SDL_SetWindowGrab(SDL_Window * window, SDL_bool grabbed)
slouken@5403
  1987
{
slouken@5403
  1988
    CHECK_WINDOW_MAGIC(window, );
slouken@5403
  1989
slouken@5404
  1990
    if (!!grabbed == !!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
slouken@5403
  1991
        return;
slouken@5403
  1992
    }
slouken@5403
  1993
    if (grabbed) {
slouken@5403
  1994
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@5403
  1995
    } else {
slouken@5403
  1996
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@5403
  1997
    }
slouken@5403
  1998
    SDL_UpdateWindowGrab(window);
slouken@5403
  1999
}
slouken@5403
  2000
slouken@5403
  2001
SDL_bool
slouken@3685
  2002
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  2003
{
slouken@5466
  2004
    CHECK_WINDOW_MAGIC(window, SDL_FALSE);
slouken@3695
  2005
slouken@2753
  2006
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  2007
}
slouken@1895
  2008
slouken@1895
  2009
void
slouken@1895
  2010
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  2011
{
slouken@5404
  2012
    SDL_OnWindowRestored(window);
slouken@1895
  2013
}
slouken@1895
  2014
slouken@1895
  2015
void
slouken@1895
  2016
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  2017
{
slouken@5404
  2018
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@1895
  2019
}
slouken@1895
  2020
slouken@1895
  2021
void
slouken@5166
  2022
SDL_OnWindowResized(SDL_Window * window)
slouken@5166
  2023
{
slouken@5172
  2024
    window->surface_valid = SDL_FALSE;
slouken@5276
  2025
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
slouken@5166
  2026
}
slouken@5166
  2027
slouken@5166
  2028
void
slouken@3502
  2029
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  2030
{
slouken@5404
  2031
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3502
  2032
}
slouken@3502
  2033
slouken@3502
  2034
void
slouken@3502
  2035
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  2036
{
slouken@3685
  2037
    SDL_RaiseWindow(window);
slouken@5404
  2038
slouken@5404
  2039
    if (FULLSCREEN_VISIBLE(window)) {
slouken@5404
  2040
        SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@5404
  2041
    }
slouken@3502
  2042
}
slouken@3502
  2043
slouken@3502
  2044
void
slouken@1895
  2045
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  2046
{
slouken@5466
  2047
    if (window->gamma && _this->SetWindowGammaRamp) {
slouken@5466
  2048
        _this->SetWindowGammaRamp(_this, window, window->gamma);
slouken@5466
  2049
    }
slouken@5466
  2050
slouken@6662
  2051
    SDL_UpdateWindowGrab(window);
slouken@1895
  2052
}
slouken@1895
  2053
slouken@6755
  2054
static SDL_bool ShouldMinimizeOnFocusLoss()
slouken@6755
  2055
{
slouken@6755
  2056
	const char *hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
slouken@6755
  2057
	if (hint) {
slouken@6755
  2058
		if (*hint == '0') {
slouken@6755
  2059
			return SDL_FALSE;
slouken@6755
  2060
		} else {
slouken@6755
  2061
			return SDL_TRUE;
slouken@6755
  2062
		}
slouken@6755
  2063
	}
slouken@6755
  2064
	return SDL_TRUE;
slouken@6755
  2065
}
slouken@6755
  2066
slouken@1895
  2067
void
slouken@1895
  2068
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  2069
{
slouken@5466
  2070
    if (window->gamma && _this->SetWindowGammaRamp) {
slouken@5466
  2071
        _this->SetWindowGammaRamp(_this, window, window->saved_gamma);
slouken@3512
  2072
    }
slouken@3512
  2073
slouken@6662
  2074
    SDL_UpdateWindowGrab(window);
slouken@5466
  2075
slouken@5466
  2076
    /* If we're fullscreen on a single-head system and lose focus, minimize */
slouken@6755
  2077
	 if ((window->flags & SDL_WINDOW_FULLSCREEN) && ShouldMinimizeOnFocusLoss() ) {
slouken@6755
  2078
			SDL_MinimizeWindow(window);
slouken@5466
  2079
    }
slouken@1895
  2080
}
slouken@1895
  2081
slouken@3685
  2082
SDL_Window *
slouken@1895
  2083
SDL_GetFocusWindow(void)
slouken@1895
  2084
{
slouken@3685
  2085
    SDL_Window *window;
slouken@1895
  2086
slouken@2753
  2087
    if (!_this) {
slouken@3685
  2088
        return NULL;
slouken@3685
  2089
    }
slouken@5246
  2090
    for (window = _this->windows; window; window = window->next) {
slouken@2753
  2091
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  2092
            return window;
slouken@2753
  2093
        }
slouken@2753
  2094
    }
slouken@3685
  2095
    return NULL;
slouken@1895
  2096
}
slouken@1895
  2097
slouken@1895
  2098
void
slouken@3685
  2099
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  2100
{
slouken@3685
  2101
    SDL_VideoDisplay *display;
slouken@3685
  2102
slouken@3695
  2103
    CHECK_WINDOW_MAGIC(window, );
slouken@3503
  2104
slouken@6214
  2105
    /* Restore video mode, etc. */
slouken@6214
  2106
    SDL_HideWindow(window);
slouken@6214
  2107
slouken@6214
  2108
    /* Make sure this window no longer has focus */
slouken@6214
  2109
    if (SDL_GetKeyboardFocus() == window) {
slouken@6214
  2110
        SDL_SetKeyboardFocus(NULL);
slouken@6214
  2111
    }
slouken@6214
  2112
    if (SDL_GetMouseFocus() == window) {
slouken@6214
  2113
        SDL_SetMouseFocus(NULL);
slouken@6214
  2114
    }
slouken@6214
  2115
icculus@5563
  2116
    /* make no context current if this is the current context window. */
icculus@5563
  2117
    if (window->flags & SDL_WINDOW_OPENGL) {
icculus@5563
  2118
        if (_this->current_glwin == window) {
slouken@6496
  2119
            SDL_GL_MakeCurrent(window, NULL);
icculus@5563
  2120
        }
icculus@5563
  2121
    }
icculus@5563
  2122
slouken@5169
  2123
    if (window->surface) {
slouken@5288
  2124
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5169
  2125
        SDL_FreeSurface(window->surface);
slouken@5169
  2126
    }
slouken@5166
  2127
    if (_this->DestroyWindowFramebuffer) {
slouken@5166
  2128
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5166
  2129
    }
slouken@3685
  2130
    if (_this->DestroyWindow) {
slouken@3685
  2131
        _this->DestroyWindow(_this, window);
slouken@3685
  2132
    }
slouken@3685
  2133
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  2134
        SDL_GL_UnloadLibrary();
slouken@3685
  2135
    }
slouken@3685
  2136
slouken@5246
  2137
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  2138
    if (display->fullscreen_window == window) {
slouken@5246
  2139
        display->fullscreen_window = NULL;
slouken@5246
  2140
    }
slouken@5246
  2141
slouken@4901
  2142
    /* Now invalidate magic */
slouken@4897
  2143
    window->magic = NULL;
slouken@4897
  2144
slouken@5165
  2145
    /* Free memory associated with the window */
slouken@5165
  2146
    if (window->title) {
slouken@5165
  2147
        SDL_free(window->title);
slouken@5165
  2148
    }
slouken@5466
  2149
    if (window->gamma) {
slouken@5466
  2150
        SDL_free(window->gamma);
slouken@5466
  2151
    }
slouken@5165
  2152
    while (window->data) {
slouken@5165
  2153
        SDL_WindowUserData *data = window->data;
slouken@5165
  2154
slouken@5165
  2155
        window->data = data->next;
slouken@5165
  2156
        SDL_free(data->name);
slouken@5165
  2157
        SDL_free(data);
slouken@5165
  2158
    }
slouken@5165
  2159
slouken@3685
  2160
    /* Unlink the window from the list */
slouken@3693
  2161
    if (window->next) {
slouken@3693
  2162
        window->next->prev = window->prev;
slouken@3693
  2163
    }
slouken@3685
  2164
    if (window->prev) {
slouken@3685
  2165
        window->prev->next = window->next;
slouken@3685
  2166
    } else {
slouken@5246
  2167
        _this->windows = window->next;
slouken@3685
  2168
    }
slouken@3685
  2169
slouken@3685
  2170
    SDL_free(window);
slouken@1895
  2171
}
slouken@1895
  2172
slouken@3025
  2173
SDL_bool
slouken@3025
  2174
SDL_IsScreenSaverEnabled()
slouken@3025
  2175
{
slouken@3025
  2176
    if (!_this) {
slouken@3025
  2177
        return SDL_TRUE;
slouken@3025
  2178
    }
slouken@3025
  2179
    return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
slouken@3025
  2180
}
slouken@3025
  2181
slouken@3025
  2182
void
slouken@3025
  2183
SDL_EnableScreenSaver()
slouken@3025
  2184
{
slouken@3025
  2185
    if (!_this) {
slouken@3025
  2186
        return;
slouken@3025
  2187
    }
slouken@3025
  2188
    if (!_this->suspend_screensaver) {
slouken@3025
  2189
        return;
slouken@3025
  2190
    }
slouken@3025
  2191
    _this->suspend_screensaver = SDL_FALSE;
slouken@3025
  2192
    if (_this->SuspendScreenSaver) {
slouken@3025
  2193
        _this->SuspendScreenSaver(_this);
slouken@3025
  2194
    }
slouken@3025
  2195
}
slouken@3025
  2196
slouken@3025
  2197
void
slouken@3025
  2198
SDL_DisableScreenSaver()
slouken@3025
  2199
{
slouken@3025
  2200
    if (!_this) {
slouken@3025
  2201
        return;
slouken@3025
  2202
    }
slouken@3025
  2203
    if (_this->suspend_screensaver) {
slouken@3025
  2204
        return;
slouken@3025
  2205
    }
slouken@3025
  2206
    _this->suspend_screensaver = SDL_TRUE;
slouken@3025
  2207
    if (_this->SuspendScreenSaver) {
slouken@3025
  2208
        _this->SuspendScreenSaver(_this);
slouken@3025
  2209
    }
slouken@3025
  2210
}
slouken@3025
  2211
slouken@1895
  2212
void
slouken@1895
  2213
SDL_VideoQuit(void)
slouken@1895
  2214
{
slouken@2753
  2215
    int i, j;
slouken@1895
  2216
slouken@2753
  2217
    if (!_this) {
slouken@2753
  2218
        return;
slouken@2753
  2219
    }
slouken@5123
  2220
slouken@2753
  2221
    /* Halt event processing before doing anything else */
slouken@5123
  2222
    SDL_QuitQuit();
slouken@5123
  2223
    SDL_MouseQuit();
slouken@5123
  2224
    SDL_KeyboardQuit();
slouken@2753
  2225
    SDL_StopEventLoop();
slouken@5123
  2226
slouken@3029
  2227
    SDL_EnableScreenSaver();
slouken@1895
  2228
slouken@2753
  2229
    /* Clean up the system video */
slouken@5246
  2230
    while (_this->windows) {
slouken@5246
  2231
        SDL_DestroyWindow(_this->windows);
slouken@2753
  2232
    }
slouken@2753
  2233
    _this->VideoQuit(_this);
slouken@1895
  2234
slouken@2753
  2235
    for (i = _this->num_displays; i--;) {
slouken@2753
  2236
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  2237
        for (j = display->num_display_modes; j--;) {
slouken@2753
  2238
            if (display->display_modes[j].driverdata) {
slouken@2753
  2239
                SDL_free(display->display_modes[j].driverdata);
slouken@2753
  2240
                display->display_modes[j].driverdata = NULL;
slouken@2753
  2241
            }
slouken@2753
  2242
        }
slouken@2753
  2243
        if (display->display_modes) {
slouken@2753
  2244
            SDL_free(display->display_modes);
slouken@2753
  2245
            display->display_modes = NULL;
slouken@2753
  2246
        }
slouken@2753
  2247
        if (display->desktop_mode.driverdata) {
slouken@2753
  2248
            SDL_free(display->desktop_mode.driverdata);
slouken@2753
  2249
            display->desktop_mode.driverdata = NULL;
slouken@2753
  2250
        }
slouken@2753
  2251
        if (display->driverdata) {
slouken@2753
  2252
            SDL_free(display->driverdata);
slouken@2753
  2253
            display->driverdata = NULL;
slouken@2753
  2254
        }
slouken@2753
  2255
    }
slouken@2753
  2256
    if (_this->displays) {
slouken@6787
  2257
        for (i = 0; i < _this->num_displays; ++i) {
slouken@6787
  2258
            SDL_free(_this->displays[i].name);
slouken@6787
  2259
        }
slouken@2753
  2260
        SDL_free(_this->displays);
slouken@2753
  2261
        _this->displays = NULL;
slouken@6787
  2262
        _this->num_displays = 0;
slouken@2753
  2263
    }
slouken@4495
  2264
    if (_this->clipboard_text) {
slouken@4495
  2265
        SDL_free(_this->clipboard_text);
slouken@4495
  2266
        _this->clipboard_text = NULL;
slouken@4495
  2267
    }
slouken@2753
  2268
    _this->free(_this);
slouken@2753
  2269
    _this = NULL;
slouken@0
  2270
}
slouken@0
  2271
slouken@1895
  2272
int
slouken@1895
  2273
SDL_GL_LoadLibrary(const char *path)
slouken@0
  2274
{
slouken@2753
  2275
    int retval;
slouken@0
  2276
slouken@2753
  2277
    if (!_this) {
slouken@2753
  2278
        SDL_UninitializedVideo();
slouken@2753
  2279
        return -1;
slouken@2753
  2280
    }
slouken@3057
  2281
    if (_this->gl_config.driver_loaded) {
slouken@3057
  2282
        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
slouken@3057
  2283
            SDL_SetError("OpenGL library already loaded");
slouken@3057
  2284
            return -1;
slouken@3057
  2285
        }
slouken@3057
  2286
        retval = 0;
slouken@3057
  2287
    } else {
slouken@3057
  2288
        if (!_this->GL_LoadLibrary) {
slouken@3057
  2289
            SDL_SetError("No dynamic GL support in video driver");
slouken@3057
  2290
            return -1;
slouken@3057
  2291
        }
slouken@2753
  2292
        retval = _this->GL_LoadLibrary(_this, path);
slouken@3057
  2293
    }
slouken@3057
  2294
    if (retval == 0) {
slouken@3057
  2295
        ++_this->gl_config.driver_loaded;
slouken@2753
  2296
    }
slouken@2753
  2297
    return (retval);
slouken@0
  2298
}
slouken@0
  2299
slouken@2753
  2300
void *
slouken@1895
  2301
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  2302
{
slouken@2753
  2303
    void *func;
slouken@0
  2304
slouken@2753
  2305
    if (!_this) {
slouken@2753
  2306
        SDL_UninitializedVideo();
slouken@2753
  2307
        return NULL;
slouken@2753
  2308
    }
slouken@2753
  2309
    func = NULL;
slouken@2753
  2310
    if (_this->GL_GetProcAddress) {
slouken@2753
  2311
        if (_this->gl_config.driver_loaded) {
slouken@2753
  2312
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  2313
        } else {
slouken@2753
  2314
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  2315
        }
slouken@2753
  2316
    } else {
slouken@2753
  2317
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  2318
    }
slouken@2753
  2319
    return func;
slouken@0
  2320
}
slouken@0
  2321
slouken@3057
  2322
void
slouken@3057
  2323
SDL_GL_UnloadLibrary(void)
slouken@3057
  2324
{
slouken@3057
  2325
    if (!_this) {
slouken@3057
  2326
        SDL_UninitializedVideo();
slouken@3057
  2327
        return;
slouken@3057
  2328
    }
slouken@3057
  2329
    if (_this->gl_config.driver_loaded > 0) {
slouken@3057
  2330
        if (--_this->gl_config.driver_loaded > 0) {
slouken@3057
  2331
            return;
slouken@3057
  2332
        }
slouken@3057
  2333
        if (_this->GL_UnloadLibrary) {
slouken@3057
  2334
            _this->GL_UnloadLibrary(_this);
slouken@3057
  2335
        }
slouken@3057
  2336
    }
slouken@3057
  2337
}
slouken@3057
  2338
slouken@1926
  2339
SDL_bool
slouken@1926
  2340
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  2341
{
slouken@5209
  2342
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2343
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  2344
    const char *extensions;
slouken@2753
  2345
    const char *start;
slouken@2753
  2346
    const char *where, *terminator;
slouken@1926
  2347
slouken@2753
  2348
    /* Extension names should not have spaces. */
slouken@2753
  2349
    where = SDL_strchr(extension, ' ');
slouken@2753
  2350
    if (where || *extension == '\0') {
slouken@2753
  2351
        return SDL_FALSE;
slouken@2753
  2352
    }
slouken@2753
  2353
    /* See if there's an environment variable override */
slouken@2753
  2354
    start = SDL_getenv(extension);
slouken@2753
  2355
    if (start && *start == '0') {
slouken@2753
  2356
        return SDL_FALSE;
slouken@2753
  2357
    }
slouken@2753
  2358
    /* Lookup the available extensions */
slouken@2753
  2359
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@2753
  2360
    if (glGetStringFunc) {
slouken@2753
  2361
        extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  2362
    } else {
slouken@2753
  2363
        extensions = NULL;
slouken@2753
  2364
    }
slouken@2753
  2365
    if (!extensions) {
slouken@2753
  2366
        return SDL_FALSE;
slouken@2753
  2367
    }
slouken@2753
  2368
    /*
slouken@2753
  2369
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  2370
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  2371
     */
slouken@1926
  2372
slouken@2753
  2373
    start = extensions;
slouken@1926
  2374
slouken@2753
  2375
    for (;;) {
slouken@2753
  2376
        where = SDL_strstr(start, extension);
slouken@2753
  2377
        if (!where)
slouken@2753
  2378
            break;
slouken@1926
  2379
slouken@2753
  2380
        terminator = where + SDL_strlen(extension);
slouken@2753
  2381
        if (where == start || *(where - 1) == ' ')
slouken@2753
  2382
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  2383
                return SDL_TRUE;
slouken@1926
  2384
slouken@2753
  2385
        start = terminator;
slouken@2753
  2386
    }
slouken@2753
  2387
    return SDL_FALSE;
slouken@1926
  2388
#else
slouken@2753
  2389
    return SDL_FALSE;
slouken@1926
  2390
#endif
slouken@1926
  2391
}
slouken@1926
  2392
slouken@1895
  2393
int
slouken@1895
  2394
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2395
{
slouken@5209
  2396
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2397
    int retval;
slouken@0
  2398
slouken@2753
  2399
    if (!_this) {
slouken@2753
  2400
        SDL_UninitializedVideo();
slouken@2753
  2401
        return -1;
slouken@2753
  2402
    }
slouken@2753
  2403
    retval = 0;
slouken@2753
  2404
    switch (attr) {
slouken@2753
  2405
    case SDL_GL_RED_SIZE:
slouken@2753
  2406
        _this->gl_config.red_size = value;
slouken@2753
  2407
        break;
slouken@2753
  2408
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2409
        _this->gl_config.green_size = value;
slouken@2753
  2410
        break;
slouken@2753
  2411
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2412
        _this->gl_config.blue_size = value;
slouken@2753
  2413
        break;
slouken@2753
  2414
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2415
        _this->gl_config.alpha_size = value;
slouken@2753
  2416
        break;
slouken@2753
  2417
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  2418
        _this->gl_config.double_buffer = value;
slouken@2753
  2419
        break;
slouken@2753
  2420
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2421
        _this->gl_config.buffer_size = value;
slouken@2753
  2422
        break;
slouken@2753
  2423
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2424
        _this->gl_config.depth_size = value;
slouken@2753
  2425
        break;
slouken@2753
  2426
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2427
        _this->gl_config.stencil_size = value;
slouken@2753
  2428
        break;
slouken@2753
  2429
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2430
        _this->gl_config.accum_red_size = value;
slouken@2753
  2431
        break;
slouken@2753
  2432
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2433
        _this->gl_config.accum_green_size = value;
slouken@2753
  2434
        break;
slouken@2753
  2435
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2436
        _this->gl_config.accum_blue_size = value;
slouken@2753
  2437
        break;
slouken@2753
  2438
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2439
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  2440
        break;
slouken@2753
  2441
    case SDL_GL_STEREO:
slouken@2753
  2442
        _this->gl_config.stereo = value;
slouken@2753
  2443
        break;
slouken@2753
  2444
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  2445
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  2446
        break;
slouken@2753
  2447
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  2448
        _this->gl_config.multisamplesamples = value;
slouken@2753
  2449
        break;
slouken@2753
  2450
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2451
        _this->gl_config.accelerated = value;
slouken@2753
  2452
        break;
slouken@2753
  2453
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2454
        _this->gl_config.retained_backing = value;
slouken@2753
  2455
        break;
slouken@3100
  2456
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@3100
  2457
        _this->gl_config.major_version = value;
slouken@3100
  2458
        break;
slouken@3100
  2459
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@3100
  2460
        _this->gl_config.minor_version = value;
slouken@3100
  2461
        break;
slouken@6370
  2462
    case SDL_GL_CONTEXT_EGL:
slouken@6370
  2463
        _this->gl_config.use_egl = value;
slouken@6370
  2464
        break;
slouken@6296
  2465
    case SDL_GL_CONTEXT_FLAGS:
slouken@6393
  2466
        if( value & ~(SDL_GL_CONTEXT_DEBUG_FLAG |
slouken@6393
  2467
		      SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG |
slouken@6393
  2468
		      SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG |
slouken@6393
  2469
		      SDL_GL_CONTEXT_RESET_ISOLATION_FLAG) ) {
slouken@6393
  2470
	    SDL_SetError("Unknown OpenGL context flag %d", value);
slouken@6393
  2471
	    retval = -1;
slouken@6393
  2472
	    break;
slouken@6393
  2473
	}
slouken@6296
  2474
        _this->gl_config.flags = value;
slouken@6296
  2475
        break;
slouken@6296
  2476
    case SDL_GL_CONTEXT_PROFILE_MASK:
slouken@6393
  2477
        if( value != 0 &&
slouken@6393
  2478
	    value != SDL_GL_CONTEXT_PROFILE_CORE &&
slouken@6393
  2479
	    value != SDL_GL_CONTEXT_PROFILE_COMPATIBILITY &&
slouken@6393
  2480
	    value != SDL_GL_CONTEXT_PROFILE_ES ) {
slouken@6393
  2481
	    SDL_SetError("Unknown OpenGL context profile %d", value);
slouken@6393
  2482
	    retval = -1;
slouken@6393
  2483
	    break;
slouken@6393
  2484
	}
slouken@6296
  2485
        _this->gl_config.profile_mask = value;
slouken@6296
  2486
        break;
slouken@6393
  2487
    case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
slouken@6393
  2488
        _this->gl_config.share_with_current_context = value;
slouken@6393
  2489
	break;
slouken@2753
  2490
    default:
slouken@2753
  2491
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2492
        retval = -1;
slouken@2753
  2493
        break;
slouken@2753
  2494
    }
slouken@2753
  2495
    return retval;
slouken@1936
  2496
#else
slouken@2753
  2497
    SDL_Unsupported();
slouken@2753
  2498
    return -1;
slouken@2753
  2499
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  2500
}
slouken@0
  2501
slouken@1895
  2502
int
slouken@1936
  2503
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  2504
{
slouken@5209
  2505
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2506
    void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
slouken@3139
  2507
    GLenum(APIENTRY * glGetErrorFunc) (void);
slouken@2753
  2508
    GLenum attrib = 0;
slouken@3099
  2509
    GLenum error = 0;
slouken@1912
  2510
slouken@2753
  2511
    glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
slouken@2753
  2512
    if (!glGetIntegervFunc) {
slouken@2753
  2513
        return -1;
slouken@2753
  2514
    }
slouken@3099
  2515
slouken@3099
  2516
    glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
slouken@3099
  2517
    if (!glGetErrorFunc) {
slouken@3099
  2518
        return -1;
slouken@3099
  2519
    }
slouken@3099
  2520
slouken@3099
  2521
    /* Clear value in any case */
slouken@3139
  2522
    *value = 0;
slouken@3099
  2523
slouken@2753
  2524
    switch (attr) {
slouken@2753
  2525
    case SDL_GL_RED_SIZE:
slouken@2753
  2526
        attrib = GL_RED_BITS;
slouken@2753
  2527
        break;
slouken@2753
  2528
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2529
        attrib = GL_BLUE_BITS;
slouken@2753
  2530
        break;
slouken@2753
  2531
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2532
        attrib = GL_GREEN_BITS;
slouken@2753
  2533
        break;
slouken@2753
  2534
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2535
        attrib = GL_ALPHA_BITS;
slouken@2753
  2536
        break;
slouken@2753
  2537
    case SDL_GL_DOUBLEBUFFER:
slouken@5209
  2538
#if SDL_VIDEO_OPENGL
slouken@2753
  2539
        attrib = GL_DOUBLEBUFFER;
slouken@2753
  2540
        break;
hfutrell@2745
  2541
#else
slouken@3099
  2542
        /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
slouken@3099
  2543
        /* parameter which switches double buffer to single buffer. OpenGL ES */
slouken@3099
  2544
        /* SDL driver must set proper value after initialization              */
slouken@3099
  2545
        *value = _this->gl_config.double_buffer;
slouken@2753
  2546
        return 0;
hfutrell@2745
  2547
#endif
slouken@2753
  2548
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2549
        attrib = GL_DEPTH_BITS;
slouken@2753
  2550
        break;
slouken@2753
  2551
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2552
        attrib = GL_STENCIL_BITS;
slouken@2753
  2553
        break;
slouken@5209
  2554
#if SDL_VIDEO_OPENGL
slouken@2753
  2555
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2556
        attrib = GL_ACCUM_RED_BITS;
slouken@2753
  2557
        break;
slouken@2753
  2558
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2559
        attrib = GL_ACCUM_GREEN_BITS;
slouken@2753
  2560
        break;
slouken@2753
  2561
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2562
        attrib = GL_ACCUM_BLUE_BITS;
slouken@2753
  2563
        break;
slouken@2753
  2564
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2565
        attrib = GL_ACCUM_ALPHA_BITS;
slouken@2753
  2566
        break;
slouken@2753
  2567
    case SDL_GL_STEREO:
slouken@2753
  2568
        attrib = GL_STEREO;
slouken@2753
  2569
        break;
hfutrell@2745
  2570
#else
slouken@2753
  2571
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2572
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2573
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2574
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2575
    case SDL_GL_STEREO:
slouken@2753
  2576
        /* none of these are supported in OpenGL ES */
slouken@2753
  2577
        *value = 0;
slouken@2753
  2578
        return 0;
hfutrell@2745
  2579
#endif
slouken@2753
  2580
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@5209
  2581
#if SDL_VIDEO_OPENGL
slouken@2753
  2582
        attrib = GL_SAMPLE_BUFFERS_ARB;
hfutrell@2745
  2583
#else
slouken@2753
  2584
        attrib = GL_SAMPLE_BUFFERS;
hfutrell@2745
  2585
#endif
slouken@2753
  2586
        break;
slouken@2753
  2587
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@5209
  2588
#if SDL_VIDEO_OPENGL
slouken@2753
  2589
        attrib = GL_SAMPLES_ARB;
hfutrell@2745
  2590
#else
slouken@2753
  2591
        attrib = GL_SAMPLES;
hfutrell@2745
  2592
#endif
slouken@2753
  2593
        break;
slouken@2753
  2594
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2595
        {
slouken@2753
  2596
            GLint bits = 0;
slouken@2753
  2597
            GLint component;
slouken@1936
  2598
slouken@2753
  2599
            /*
slouken@2753
  2600
             * there doesn't seem to be a single flag in OpenGL
slouken@2753
  2601
             * for this!
slouken@2753
  2602
             */
slouken@2753
  2603
            glGetIntegervFunc(GL_RED_BITS, &component);
slouken@2753
  2604
            bits += component;
slouken@2753
  2605
            glGetIntegervFunc(GL_GREEN_BITS, &component);
slouken@2753
  2606
            bits += component;
slouken@2753
  2607
            glGetIntegervFunc(GL_BLUE_BITS, &component);
slouken@2753
  2608
            bits += component;
slouken@2753
  2609
            glGetIntegervFunc(GL_ALPHA_BITS, &component);
slouken@2753
  2610
            bits += component;
slouken@1936
  2611
slouken@2753
  2612
            *value = bits;
slouken@2753
  2613
            return 0;
slouken@2753
  2614
        }
slouken@2753
  2615
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2616
        {
slouken@2753
  2617
            /* FIXME: How do we get this information? */
slouken@3571
  2618
            *value = (_this->gl_config.accelerated != 0);
slouken@2753
  2619
            return 0;
slouken@2753
  2620
        }
slouken@5210
  2621
    case SDL_GL_RETAINED_BACKING:
slouken@5210
  2622
        {
slouken@5210
  2623
            *value = _this->gl_config.retained_backing;
slouken@5210
  2624
            return 0;
slouken@5210
  2625
        }
slouken@5210
  2626
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@5210
  2627
        {
slouken@5210
  2628
            *value = _this->gl_config.major_version;
slouken@5210
  2629
            return 0;
slouken@5210
  2630
        }
slouken@5210
  2631
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@5210
  2632
        {
slouken@5210
  2633
            *value = _this->gl_config.minor_version;
slouken@5210
  2634
            return 0;
slouken@5210
  2635
        }
slouken@6370
  2636
    case SDL_GL_CONTEXT_EGL:
slouken@6370
  2637
        {
slouken@6370
  2638
            *value = _this->gl_config.use_egl;
slouken@6370
  2639
            return 0;
slouken@6370
  2640
        }
slouken@6296
  2641
    case SDL_GL_CONTEXT_FLAGS:
slouken@6296
  2642
        {
slouken@6296
  2643
            *value = _this->gl_config.flags;
slouken@6296
  2644
            return 0;
slouken@6296
  2645
        }
slouken@6296
  2646
    case SDL_GL_CONTEXT_PROFILE_MASK:
slouken@6296
  2647
        {
slouken@6296
  2648
            *value = _this->gl_config.profile_mask;
slouken@6296
  2649
            return 0;
slouken@6296
  2650
        }
slouken@6393
  2651
    case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
slouken@6393
  2652
        {
slouken@6393
  2653
            *value = _this->gl_config.share_with_current_context;
slouken@6393
  2654
            return 0;
slouken@6393
  2655
        }
slouken@2753
  2656
    default:
slouken@2753
  2657
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2658
        return -1;
slouken@2753
  2659
    }
slouken@0
  2660
slouken@2753
  2661
    glGetIntegervFunc(attrib, (GLint *) value);
slouken@3139
  2662
    error = glGetErrorFunc();
slouken@3139
  2663
    if (error != GL_NO_ERROR) {
slouken@3139
  2664
        switch (error) {
slouken@3139
  2665
        case GL_INVALID_ENUM:
slouken@3139
  2666
            {
slouken@3139
  2667
                SDL_SetError("OpenGL error: GL_INVALID_ENUM");
slouken@3139
  2668
            }
slouken@3139
  2669
            break;
slouken@3139
  2670
        case GL_INVALID_VALUE:
slouken@3139
  2671
            {
slouken@3139
  2672
                SDL_SetError("OpenGL error: GL_INVALID_VALUE");
slouken@3139
  2673
            }