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