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