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