src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 12 Nov 2013 02:18:52 -0800
changeset 7971 4151d8c29684
parent 7969 72405c8b7dba
child 7981 6d538bc1b395
permissions -rwxr-xr-x
Retain the high dpi flag when recreating the window (e.g when creating a renderer context)
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@7971
  1178
    (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI)
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@7971
  1239
slouken@7971
  1240
    /* Unless the user has specified the high-DPI disabling hint, respect the
slouken@7971
  1241
     * SDL_WINDOW_ALLOW_HIGHDPI flag.
slouken@7971
  1242
     */
slouken@7971
  1243
    if (flags & SDL_WINDOW_ALLOW_HIGHDPI) {
slouken@7971
  1244
        hint = SDL_GetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED);
slouken@7971
  1245
        if (hint && SDL_atoi(hint) > 0) {
slouken@7971
  1246
            flags &= ~SDL_WINDOW_ALLOW_HIGHDPI;
slouken@7971
  1247
        }
slouken@7971
  1248
    }
slouken@7971
  1249
slouken@3685
  1250
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@7399
  1251
    if (!window) {
slouken@7399
  1252
        SDL_OutOfMemory();
slouken@7399
  1253
        return NULL;
slouken@7399
  1254
    }
slouken@3695
  1255
    window->magic = &_this->window_magic;
slouken@3685
  1256
    window->id = _this->next_object_id++;
slouken@3685
  1257
    window->x = x;
slouken@3685
  1258
    window->y = y;
slouken@3685
  1259
    window->w = w;
slouken@3685
  1260
    window->h = h;
slouken@5380
  1261
    if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
slouken@5380
  1262
        SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5380
  1263
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5380
  1264
        int displayIndex;
slouken@5380
  1265
        SDL_Rect bounds;
slouken@5380
  1266
slouken@5380
  1267
        displayIndex = SDL_GetIndexOfDisplay(display);
slouken@5380
  1268
        SDL_GetDisplayBounds(displayIndex, &bounds);
slouken@5404
  1269
        if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) {
slouken@5380
  1270
            window->x = bounds.x + (bounds.w - w) / 2;
slouken@5380
  1271
        }
slouken@5380
  1272
        if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5380
  1273
            window->y = bounds.y + (bounds.h - h) / 2;
slouken@5380
  1274
        }
slouken@5380
  1275
    }
slouken@5380
  1276
    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
slouken@5466
  1277
    window->brightness = 1.0f;
slouken@5246
  1278
    window->next = _this->windows;
urkle@7746
  1279
slouken@5246
  1280
    if (_this->windows) {
slouken@5246
  1281
        _this->windows->prev = window;
slouken@3693
  1282
    }
slouken@5246
  1283
    _this->windows = window;
slouken@3685
  1284
slouken@3685
  1285
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@3685
  1286
        SDL_DestroyWindow(window);
slouken@3685
  1287
        return NULL;
slouken@3685
  1288
    }
slouken@0
  1289
slouken@2753
  1290
    if (title) {
slouken@3685
  1291
        SDL_SetWindowTitle(window, title);
slouken@2753
  1292
    }
slouken@5380
  1293
    SDL_FinishWindowCreation(window, flags);
slouken@7191
  1294
slouken@6063
  1295
    /* If the window was created fullscreen, make sure the mode code matches */
slouken@6063
  1296
    SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
slouken@3685
  1297
slouken@3685
  1298
    return window;
slouken@1895
  1299
}
slouken@0
  1300
slouken@3685
  1301
SDL_Window *
slouken@1895
  1302
SDL_CreateWindowFrom(const void *data)
slouken@1895
  1303
{
slouken@3685
  1304
    SDL_Window *window;
slouken@0
  1305
slouken@2753
  1306
    if (!_this) {
slouken@2753
  1307
        SDL_UninitializedVideo();
slouken@3685
  1308
        return NULL;
slouken@3685
  1309
    }
slouken@3685
  1310
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@7399
  1311
    if (!window) {
slouken@7399
  1312
        SDL_OutOfMemory();
slouken@7399
  1313
        return NULL;
slouken@7399
  1314
    }
slouken@3695
  1315
    window->magic = &_this->window_magic;
slouken@3685
  1316
    window->id = _this->next_object_id++;
slouken@3685
  1317
    window->flags = SDL_WINDOW_FOREIGN;
slouken@5466
  1318
    window->brightness = 1.0f;
slouken@5246
  1319
    window->next = _this->windows;
slouken@5246
  1320
    if (_this->windows) {
slouken@5246
  1321
        _this->windows->prev = window;
slouken@3693
  1322
    }
slouken@5246
  1323
    _this->windows = window;
slouken@1895
  1324
slouken@2753
  1325
    if (!_this->CreateWindowFrom ||
slouken@3685
  1326
        _this->CreateWindowFrom(_this, window, data) < 0) {
slouken@3685
  1327
        SDL_DestroyWindow(window);
slouken@3685
  1328
        return NULL;
slouken@3685
  1329
    }
slouken@3685
  1330
    return window;
slouken@1895
  1331
}
slouken@1895
  1332
slouken@1924
  1333
int
slouken@1928
  1334
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
  1335
{
slouken@2753
  1336
    char *title = window->title;
slouken@7523
  1337
    SDL_Surface *icon = window->icon;
slouken@1956
  1338
slouken@2753
  1339
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
icculus@7037
  1340
        return SDL_SetError("No OpenGL support in video driver");
slouken@2753
  1341
    }
slouken@3057
  1342
slouken@3057
  1343
    if (window->flags & SDL_WINDOW_FOREIGN) {
slouken@3057
  1344
        /* Can't destroy and re-create foreign windows, hrm */
slouken@3057
  1345
        flags |= SDL_WINDOW_FOREIGN;
slouken@3057
  1346
    } else {
slouken@3057
  1347
        flags &= ~SDL_WINDOW_FOREIGN;
slouken@3057
  1348
    }
slouken@3057
  1349
slouken@5249
  1350
    /* Restore video mode, etc. */
slouken@5305
  1351
    SDL_HideWindow(window);
slouken@5249
  1352
slouken@5249
  1353
    /* Tear down the old native window */
slouken@5249
  1354
    if (window->surface) {
slouken@5288
  1355
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5249
  1356
        SDL_FreeSurface(window->surface);
slouken@5249
  1357
    }
slouken@5249
  1358
    if (_this->DestroyWindowFramebuffer) {
slouken@5249
  1359
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5249
  1360
    }
slouken@3057
  1361
    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@2753
  1362
        _this->DestroyWindow(_this, window);
slouken@2753
  1363
    }
slouken@3057
  1364
slouken@5249
  1365
    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
slouken@5249
  1366
        if (flags & SDL_WINDOW_OPENGL) {
slouken@6775
  1367
            if (SDL_GL_LoadLibrary(NULL) < 0) {
slouken@6775
  1368
                return -1;
slouken@6775
  1369
            }
slouken@5249
  1370
        } else {
slouken@5249
  1371
            SDL_GL_UnloadLibrary();
slouken@5249
  1372
        }
slouken@5249
  1373
    }
slouken@5249
  1374
slouken@2753
  1375
    window->title = NULL;
slouken@7523
  1376
    window->icon = NULL;
slouken@5380
  1377
    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
slouken@1956
  1378
slouken@3057
  1379
    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@3057
  1380
        if (_this->CreateWindow(_this, window) < 0) {
slouken@3057
  1381
            if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1382
                SDL_GL_UnloadLibrary();
slouken@3057
  1383
            }
slouken@3057
  1384
            return -1;
slouken@3057
  1385
        }
slouken@2753
  1386
    }
slouken@3057
  1387
slouken@2753
  1388
    if (title) {
slouken@3685
  1389
        SDL_SetWindowTitle(window, title);
slouken@2753
  1390
        SDL_free(title);
slouken@2753
  1391
    }
slouken@7523
  1392
    if (icon) {
slouken@7523
  1393
        SDL_SetWindowIcon(window, icon);
slouken@7523
  1394
        SDL_FreeSurface(icon);
slouken@7523
  1395
    }
slouken@5380
  1396
    SDL_FinishWindowCreation(window, flags);
slouken@2875
  1397
slouken@2753
  1398
    return 0;
slouken@1924
  1399
}
slouken@1924
  1400
slouken@1895
  1401
Uint32
slouken@3685
  1402
SDL_GetWindowID(SDL_Window * window)
slouken@1895
  1403
{
slouken@3695
  1404
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1405
slouken@3685
  1406
    return window->id;
slouken@3685
  1407
}
slouken@3685
  1408
slouken@3685
  1409
SDL_Window *
slouken@3685
  1410
SDL_GetWindowFromID(Uint32 id)
slouken@3685
  1411
{
slouken@3685
  1412
    SDL_Window *window;
slouken@3685
  1413
slouken@3685
  1414
    if (!_this) {
slouken@3685
  1415
        return NULL;
slouken@3685
  1416
    }
slouken@5246
  1417
    for (window = _this->windows; window; window = window->next) {
slouken@5246
  1418
        if (window->id == id) {
slouken@5246
  1419
            return window;
slouken@3685
  1420
        }
slouken@3685
  1421
    }
slouken@3685
  1422
    return NULL;
slouken@3685
  1423
}
slouken@3685
  1424
slouken@3685
  1425
Uint32
slouken@3685
  1426
SDL_GetWindowFlags(SDL_Window * window)
slouken@3685
  1427
{
slouken@3695
  1428
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1429
slouken@2753
  1430
    return window->flags;
slouken@1895
  1431
}
slouken@1895
  1432
slouken@1895
  1433
void
slouken@3685
  1434
SDL_SetWindowTitle(SDL_Window * window, const char *title)
slouken@1895
  1435
{
slouken@3695
  1436
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1437
slouken@3695
  1438
    if (title == window->title) {
slouken@2753
  1439
        return;
slouken@2753
  1440
    }
slouken@7719
  1441
    SDL_free(window->title);
slouken@4871
  1442
    if (title && *title) {
slouken@2753
  1443
        window->title = SDL_strdup(title);
slouken@2753
  1444
    } else {
slouken@2753
  1445
        window->title = NULL;
slouken@2753
  1446
    }
slouken@1956
  1447
slouken@2753
  1448
    if (_this->SetWindowTitle) {
slouken@2753
  1449
        _this->SetWindowTitle(_this, window);
slouken@2753
  1450
    }
slouken@1895
  1451
}
slouken@1895
  1452
slouken@2753
  1453
const char *
slouken@3685
  1454
SDL_GetWindowTitle(SDL_Window * window)
slouken@1895
  1455
{
slouken@4872
  1456
    CHECK_WINDOW_MAGIC(window, "");
slouken@3695
  1457
slouken@4871
  1458
    return window->title ? window->title : "";
slouken@1895
  1459
}
slouken@1895
  1460
slouken@1895
  1461
void
slouken@3685
  1462
SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
slouken@2967
  1463
{
slouken@3695
  1464
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1465
slouken@5375
  1466
    if (!icon) {
slouken@5375
  1467
        return;
slouken@5375
  1468
    }
slouken@5375
  1469
slouken@7720
  1470
    SDL_FreeSurface(window->icon);
slouken@7523
  1471
slouken@7523
  1472
    /* Convert the icon into ARGB8888 */
slouken@7523
  1473
    window->icon = SDL_ConvertSurfaceFormat(icon, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@7523
  1474
    if (!window->icon) {
slouken@7523
  1475
        return;
slouken@7523
  1476
    }
slouken@7523
  1477
slouken@2967
  1478
    if (_this->SetWindowIcon) {
slouken@7523
  1479
        _this->SetWindowIcon(_this, window, window->icon);
slouken@2967
  1480
    }
slouken@2967
  1481
}
slouken@2967
  1482
slouken@5165
  1483
void*
slouken@5165
  1484
SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
slouken@1895
  1485
{
slouken@5165
  1486
    SDL_WindowUserData *prev, *data;
slouken@3695
  1487
slouken@5165
  1488
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@7191
  1489
aschiffler@7046
  1490
    /* Input validation */
slouken@7721
  1491
    if (name == NULL || name[0] == '\0') {
aschiffler@7046
  1492
      SDL_InvalidParamError("name");
aschiffler@7046
  1493
      return NULL;
aschiffler@7046
  1494
    }
slouken@5165
  1495
slouken@5165
  1496
    /* See if the named data already exists */
slouken@5165
  1497
    prev = NULL;
slouken@5165
  1498
    for (data = window->data; data; prev = data, data = data->next) {
aschiffler@7046
  1499
        if (data->name && SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1500
            void *last_value = data->data;
slouken@5165
  1501
slouken@5165
  1502
            if (userdata) {
slouken@5165
  1503
                /* Set the new value */
slouken@5165
  1504
                data->data = userdata;
slouken@5165
  1505
            } else {
slouken@5165
  1506
                /* Delete this value */
slouken@5165
  1507
                if (prev) {
slouken@5165
  1508
                    prev->next = data->next;
slouken@5165
  1509
                } else {
slouken@5165
  1510
                    window->data = data->next;
slouken@5165
  1511
                }
slouken@5165
  1512
                SDL_free(data->name);
slouken@5165
  1513
                SDL_free(data);
slouken@5165
  1514
            }
slouken@5165
  1515
            return last_value;
slouken@5165
  1516
        }
slouken@5165
  1517
    }
slouken@5165
  1518
slouken@5165
  1519
    /* Add new data to the window */
slouken@5165
  1520
    if (userdata) {
slouken@5165
  1521
        data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
slouken@5165
  1522
        data->name = SDL_strdup(name);
slouken@5165
  1523
        data->data = userdata;
slouken@5165
  1524
        data->next = window->data;
slouken@5165
  1525
        window->data = data;
slouken@5165
  1526
    }
slouken@5165
  1527
    return NULL;
slouken@1895
  1528
}
slouken@1895
  1529
slouken@2753
  1530
void *
slouken@5165
  1531
SDL_GetWindowData(SDL_Window * window, const char *name)
slouken@1895
  1532
{
slouken@5165
  1533
    SDL_WindowUserData *data;
slouken@5165
  1534
slouken@3695
  1535
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  1536
aschiffler@7046
  1537
    /* Input validation */
slouken@7721
  1538
    if (name == NULL || name[0] == '\0') {
aschiffler@7046
  1539
      SDL_InvalidParamError("name");
aschiffler@7046
  1540
      return NULL;
aschiffler@7046
  1541
    }
aschiffler@7046
  1542
slouken@5165
  1543
    for (data = window->data; data; data = data->next) {
aschiffler@7046
  1544
        if (data->name && SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1545
            return data->data;
slouken@5165
  1546
        }
slouken@5165
  1547
    }
slouken@5165
  1548
    return NULL;
slouken@1895
  1549
}
slouken@1895
  1550
slouken@1895
  1551
void
slouken@3685
  1552
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1553
{
slouken@3695
  1554
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1555
slouken@7195
  1556
    if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@7195
  1557
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@7195
  1558
        int displayIndex;
slouken@7195
  1559
        SDL_Rect bounds;
slouken@7195
  1560
slouken@7195
  1561
        displayIndex = SDL_GetIndexOfDisplay(display);
slouken@7195
  1562
        SDL_GetDisplayBounds(displayIndex, &bounds);
slouken@7195
  1563
        if (SDL_WINDOWPOS_ISCENTERED(x)) {
slouken@7195
  1564
            x = bounds.x + (bounds.w - window->w) / 2;
slouken@7195
  1565
        }
slouken@7195
  1566
        if (SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@7195
  1567
            y = bounds.y + (bounds.h - window->h) / 2;
slouken@7195
  1568
        }
slouken@7195
  1569
    }
slouken@7195
  1570
jorgen@7792
  1571
    if ((window->flags & SDL_WINDOW_FULLSCREEN)) {
jorgen@7792
  1572
        if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
jorgen@7792
  1573
            window->windowed.x = x;
jorgen@7792
  1574
        }
jorgen@7792
  1575
        if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
jorgen@7792
  1576
            window->windowed.y = y;
jorgen@7792
  1577
        }
jorgen@7792
  1578
    } else {
jorgen@7792
  1579
        if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
jorgen@7792
  1580
            window->x = x;
jorgen@7792
  1581
        }
jorgen@7792
  1582
        if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
jorgen@7792
  1583
            window->y = y;
jorgen@7792
  1584
        }
jorgen@7792
  1585
slouken@5246
  1586
        if (_this->SetWindowPosition) {
slouken@5246
  1587
            _this->SetWindowPosition(_this, window);
slouken@5246
  1588
        }
slouken@5246
  1589
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@2753
  1590
    }
slouken@1895
  1591
}
slouken@1895
  1592
slouken@1895
  1593
void
slouken@3685
  1594
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1595
{
slouken@5246
  1596
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1597
slouken@5246
  1598
    /* Fullscreen windows are always at their display's origin */
slouken@5246
  1599
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
aschiffler@6956
  1600
        if (x) {
aschiffler@6956
  1601
            *x = 0;
aschiffler@6956
  1602
        }
aschiffler@6956
  1603
        if (y) {
aschiffler@6956
  1604
            *y = 0;
aschiffler@6956
  1605
        }
slouken@5246
  1606
    } else {
slouken@5242
  1607
        if (x) {
slouken@5242
  1608
            *x = window->x;
slouken@5242
  1609
        }
slouken@5242
  1610
        if (y) {
slouken@5242
  1611
            *y = window->y;
slouken@5242
  1612
        }
slouken@2753
  1613
    }
slouken@1895
  1614
}
slouken@1895
  1615
slouken@1895
  1616
void
icculus@6422
  1617
SDL_SetWindowBordered(SDL_Window * window, SDL_bool bordered)
icculus@6422
  1618
{
icculus@6422
  1619
    CHECK_WINDOW_MAGIC(window, );
icculus@6422
  1620
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
icculus@6422
  1621
        const int want = (bordered != SDL_FALSE);  /* normalize the flag. */
icculus@6422
  1622
        const int have = ((window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6422
  1623
        if ((want != have) && (_this->SetWindowBordered)) {
icculus@6422
  1624
            if (want) {
icculus@6422
  1625
                window->flags &= ~SDL_WINDOW_BORDERLESS;
icculus@6422
  1626
            } else {
icculus@6422
  1627
                window->flags |= SDL_WINDOW_BORDERLESS;
icculus@6422
  1628
            }
icculus@6422
  1629
            _this->SetWindowBordered(_this, window, (SDL_bool) want);
icculus@6422
  1630
        }
icculus@6422
  1631
    }
icculus@6422
  1632
}
icculus@6422
  1633
icculus@6422
  1634
void
slouken@3685
  1635
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1636
{
slouken@3695
  1637
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1638
    if (w <= 0) {
aschiffler@6984
  1639
        SDL_InvalidParamError("w");
aschiffler@6984
  1640
        return;
aschiffler@6984
  1641
    }
aschiffler@6984
  1642
    if (h <= 0) {
aschiffler@6984
  1643
        SDL_InvalidParamError("h");
aschiffler@6984
  1644
        return;
aschiffler@6984
  1645
    }
slouken@3695
  1646
slouken@7941
  1647
    /* Make sure we don't exceed any window size limits */
slouken@7941
  1648
    if (window->min_w && w < window->min_w)
slouken@7941
  1649
    {
slouken@7941
  1650
        w = window->min_w;
slouken@7941
  1651
    }
slouken@7941
  1652
    if (window->max_w && w > window->max_w)
slouken@7941
  1653
    {
slouken@7941
  1654
        w = window->max_w;
slouken@7941
  1655
    }
slouken@7941
  1656
    if (window->min_h && h < window->min_h)
slouken@7941
  1657
    {
slouken@7941
  1658
        h = window->min_h;
slouken@7941
  1659
    }
slouken@7941
  1660
    if (window->max_h && h > window->max_h)
slouken@7941
  1661
    {
slouken@7941
  1662
        h = window->max_h;
slouken@7941
  1663
    }
slouken@7941
  1664
slouken@5249
  1665
    /* FIXME: Should this change fullscreen modes? */
slouken@7941
  1666
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@7941
  1667
        window->windowed.w = w;
slouken@7941
  1668
        window->windowed.h = h;
slouken@7941
  1669
    } else {
slouken@5276
  1670
        window->w = w;
slouken@5276
  1671
        window->h = h;
slouken@5249
  1672
        if (_this->SetWindowSize) {
slouken@5249
  1673
            _this->SetWindowSize(_this, window);
slouken@5249
  1674
        }
slouken@5276
  1675
        if (window->w == w && window->h == h) {
slouken@5276
  1676
            /* We didn't get a SDL_WINDOWEVENT_RESIZED event (by design) */
slouken@5276
  1677
            SDL_OnWindowResized(window);
slouken@5276
  1678
        }
slouken@2753
  1679
    }
slouken@1895
  1680
}
slouken@1895
  1681
slouken@1895
  1682
void
slouken@3685
  1683
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1684
{
aschiffler@6984
  1685
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1686
    if (w) {
aschiffler@6984
  1687
        *w = window->w;
slouken@5246
  1688
    }
aschiffler@6984
  1689
    if (h) {
stopiccot@6681
  1690
        *h = window->h;
slouken@7191
  1691
    }
stopiccot@6681
  1692
}
stopiccot@6681
  1693
stopiccot@6681
  1694
void
stopiccot@6681
  1695
SDL_SetWindowMinimumSize(SDL_Window * window, int min_w, int min_h)
stopiccot@6681
  1696
{
stopiccot@6681
  1697
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1698
    if (min_w <= 0) {
aschiffler@6984
  1699
        SDL_InvalidParamError("min_w");
aschiffler@6984
  1700
        return;
aschiffler@6984
  1701
    }
aschiffler@6984
  1702
    if (min_h <= 0) {
aschiffler@6984
  1703
        SDL_InvalidParamError("min_h");
aschiffler@6984
  1704
        return;
aschiffler@6984
  1705
    }
slouken@7191
  1706
stopiccot@6681
  1707
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
stopiccot@6681
  1708
        window->min_w = min_w;
stopiccot@6681
  1709
        window->min_h = min_h;
stopiccot@6681
  1710
        if (_this->SetWindowMinimumSize) {
stopiccot@6681
  1711
            _this->SetWindowMinimumSize(_this, window);
slouken@2849
  1712
        }
stopiccot@6681
  1713
        /* Ensure that window is not smaller than minimal size */
stopiccot@6681
  1714
        SDL_SetWindowSize(window, SDL_max(window->w, window->min_w), SDL_max(window->h, window->min_h));
stopiccot@6681
  1715
    }
stopiccot@6681
  1716
}
stopiccot@6681
  1717
stopiccot@6681
  1718
void
stopiccot@6681
  1719
SDL_GetWindowMinimumSize(SDL_Window * window, int *min_w, int *min_h)
stopiccot@6681
  1720
{
aschiffler@6984
  1721
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1722
    if (min_w) {
aschiffler@6984
  1723
        *min_w = window->min_w;
stopiccot@6681
  1724
    }
aschiffler@6984
  1725
    if (min_h) {
stopiccot@6681
  1726
        *min_h = window->min_h;
slouken@2753
  1727
    }
slouken@1895
  1728
}
slouken@1895
  1729
slouken@1895
  1730
void
slouken@6788
  1731
SDL_SetWindowMaximumSize(SDL_Window * window, int max_w, int max_h)
slouken@6788
  1732
{
slouken@6788
  1733
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1734
    if (max_w <= 0) {
aschiffler@6984
  1735
        SDL_InvalidParamError("max_w");
aschiffler@6984
  1736
        return;
aschiffler@6984
  1737
    }
aschiffler@6984
  1738
    if (max_h <= 0) {
aschiffler@6984
  1739
        SDL_InvalidParamError("max_h");
aschiffler@6984
  1740
        return;
aschiffler@6984
  1741
    }
slouken@7191
  1742
slouken@6788
  1743
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@6788
  1744
        window->max_w = max_w;
slouken@6788
  1745
        window->max_h = max_h;
slouken@6788
  1746
        if (_this->SetWindowMaximumSize) {
slouken@6788
  1747
            _this->SetWindowMaximumSize(_this, window);
slouken@6788
  1748
        }
slouken@6788
  1749
        /* Ensure that window is not larger than maximal size */
slouken@6788
  1750
        SDL_SetWindowSize(window, SDL_min(window->w, window->max_w), SDL_min(window->h, window->max_h));
slouken@6788
  1751
    }
slouken@6788
  1752
}
slouken@6788
  1753
slouken@6788
  1754
void
slouken@6788
  1755
SDL_GetWindowMaximumSize(SDL_Window * window, int *max_w, int *max_h)
slouken@6788
  1756
{
aschiffler@6984
  1757
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1758
    if (max_w) {
aschiffler@6984
  1759
        *max_w = window->max_w;
slouken@6788
  1760
    }
aschiffler@6984
  1761
    if (max_h) {
slouken@6788
  1762
        *max_h = window->max_h;
slouken@6788
  1763
    }
slouken@6788
  1764
}
slouken@6788
  1765
slouken@6788
  1766
void
slouken@3685
  1767
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1768
{
slouken@3695
  1769
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1770
slouken@3695
  1771
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@2753
  1772
        return;
slouken@2753
  1773
    }
slouken@1895
  1774
slouken@2753
  1775
    if (_this->ShowWindow) {
slouken@2753
  1776
        _this->ShowWindow(_this, window);
slouken@2753
  1777
    }
slouken@3685
  1778
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1779
}
slouken@1895
  1780
slouken@1895
  1781
void
slouken@3685
  1782
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1783
{
slouken@3695
  1784
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1785
slouken@3695
  1786
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1787
        return;
slouken@2753
  1788
    }
slouken@1895
  1789
slouken@5404
  1790
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@5404
  1791
slouken@2753
  1792
    if (_this->HideWindow) {
slouken@2753
  1793
        _this->HideWindow(_this, window);
slouken@2753
  1794
    }
slouken@3685
  1795
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1796
}
slouken@1895
  1797
slouken@1895
  1798
void
slouken@3685
  1799
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1800
{
slouken@3695
  1801
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1802
slouken@3695
  1803
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1804
        return;
slouken@2753
  1805
    }
slouken@2753
  1806
    if (_this->RaiseWindow) {
slouken@2753
  1807
        _this->RaiseWindow(_this, window);
slouken@2753
  1808
    }
slouken@1895
  1809
}
slouken@1895
  1810
slouken@1895
  1811
void
slouken@3685
  1812
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1813
{
slouken@3695
  1814
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1815
slouken@3695
  1816
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
  1817
        return;
slouken@2753
  1818
    }
slouken@1895
  1819
gabomdq@7662
  1820
    /* !!! FIXME: should this check if the window is resizable? */
icculus@7566
  1821
slouken@2753
  1822
    if (_this->MaximizeWindow) {
slouken@2753
  1823
        _this->MaximizeWindow(_this, window);
slouken@2753
  1824
    }
slouken@1895
  1825
}
slouken@1895
  1826
slouken@1895
  1827
void
slouken@3685
  1828
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1829
{
slouken@3695
  1830
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1831
slouken@3695
  1832
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
  1833
        return;
slouken@2753
  1834
    }
slouken@1895
  1835
slouken@5404
  1836
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@5404
  1837
slouken@2753
  1838
    if (_this->MinimizeWindow) {
slouken@2753
  1839
        _this->MinimizeWindow(_this, window);
slouken@2753
  1840
    }
slouken@1895
  1841
}
slouken@1895
  1842
slouken@1895
  1843
void
slouken@3685
  1844
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1845
{
slouken@3695
  1846
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1847
slouken@3695
  1848
    if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1849
        return;
slouken@2753
  1850
    }
slouken@1895
  1851
slouken@2753
  1852
    if (_this->RestoreWindow) {
slouken@2753
  1853
        _this->RestoreWindow(_this, window);
slouken@2753
  1854
    }
slouken@1895
  1855
}
slouken@1895
  1856
slouken@6755
  1857
#define FULLSCREEN_MASK ( SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN )
slouken@1895
  1858
int
slouken@6755
  1859
SDL_SetWindowFullscreen(SDL_Window * window, Uint32 flags)
slouken@1895
  1860
{
slouken@3695
  1861
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1862
slouken@7191
  1863
    flags &= FULLSCREEN_MASK;
slouken@7191
  1864
slouken@6755
  1865
    if ( flags == (window->flags & FULLSCREEN_MASK) ) {
slouken@2753
  1866
        return 0;
slouken@2753
  1867
    }
slouken@7191
  1868
slouken@7191
  1869
    /* clear the previous flags and OR in the new ones */
slouken@7191
  1870
    window->flags &= ~FULLSCREEN_MASK;
slouken@6755
  1871
    window->flags |= flags;
slouken@6755
  1872
slouken@5404
  1873
    SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
slouken@1895
  1874
slouken@2753
  1875
    return 0;
slouken@1895
  1876
}
slouken@1895
  1877
slouken@5166
  1878
static SDL_Surface *
slouken@5166
  1879
SDL_CreateWindowFramebuffer(SDL_Window * window)
slouken@5166
  1880
{
slouken@5166
  1881
    Uint32 format;
slouken@5166
  1882
    void *pixels;
slouken@5166
  1883
    int pitch;
slouken@5166
  1884
    int bpp;
slouken@5166
  1885
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5166
  1886
slouken@5166
  1887
    if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
slouken@5166
  1888
        return NULL;
slouken@5166
  1889
    }
slouken@5166
  1890
slouken@5166
  1891
    if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
slouken@5166
  1892
        return NULL;
slouken@5166
  1893
    }
slouken@5166
  1894
slouken@5166
  1895
    if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5166
  1896
        return NULL;
slouken@5166
  1897
    }
slouken@5166
  1898
slouken@5166
  1899
    return SDL_CreateRGBSurfaceFrom(pixels, window->w, window->h, bpp, pitch, Rmask, Gmask, Bmask, Amask);
slouken@5166
  1900
}
slouken@5166
  1901
slouken@5166
  1902
SDL_Surface *
slouken@5166
  1903
SDL_GetWindowSurface(SDL_Window * window)
slouken@5166
  1904
{
slouken@5166
  1905
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5166
  1906
slouken@5172
  1907
    if (!window->surface_valid) {
slouken@5172
  1908
        if (window->surface) {
slouken@5288
  1909
            window->surface->flags &= ~SDL_DONTFREE;
slouken@5172
  1910
            SDL_FreeSurface(window->surface);
slouken@5172
  1911
        }
slouken@5166
  1912
        window->surface = SDL_CreateWindowFramebuffer(window);
slouken@5169
  1913
        if (window->surface) {
slouken@5172
  1914
            window->surface_valid = SDL_TRUE;
slouken@5288
  1915
            window->surface->flags |= SDL_DONTFREE;
slouken@5169
  1916
        }
slouken@5166
  1917
    }
slouken@5166
  1918
    return window->surface;
slouken@5166
  1919
}
slouken@5166
  1920
slouken@5166
  1921
int
slouken@5166
  1922
SDL_UpdateWindowSurface(SDL_Window * window)
slouken@5166
  1923
{
slouken@5166
  1924
    SDL_Rect full_rect;
slouken@5166
  1925
slouken@5166
  1926
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1927
slouken@5166
  1928
    full_rect.x = 0;
slouken@5166
  1929
    full_rect.y = 0;
slouken@5166
  1930
    full_rect.w = window->w;
slouken@5166
  1931
    full_rect.h = window->h;
slouken@5297
  1932
    return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
slouken@5166
  1933
}
slouken@5166
  1934
slouken@5166
  1935
int
slouken@7014
  1936
SDL_UpdateWindowSurfaceRects(SDL_Window * window, const SDL_Rect * rects,
slouken@5297
  1937
                             int numrects)
slouken@5166
  1938
{
slouken@5166
  1939
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1940
slouken@5172
  1941
    if (!window->surface_valid) {
icculus@7037
  1942
        return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
slouken@5166
  1943
    }
slouken@5166
  1944
slouken@5297
  1945
    return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
slouken@5166
  1946
}
slouken@5166
  1947
slouken@5466
  1948
int
slouken@5466
  1949
SDL_SetWindowBrightness(SDL_Window * window, float brightness)
slouken@5466
  1950
{
slouken@5466
  1951
    Uint16 ramp[256];
slouken@5466
  1952
    int status;
slouken@5466
  1953
slouken@5466
  1954
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1955
slouken@5466
  1956
    SDL_CalculateGammaRamp(brightness, ramp);
slouken@5466
  1957
    status = SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
slouken@5466
  1958
    if (status == 0) {
slouken@5466
  1959
        window->brightness = brightness;
slouken@5466
  1960
    }
slouken@5466
  1961
    return status;
slouken@5466
  1962
}
slouken@5466
  1963
slouken@5466
  1964
float
slouken@5466
  1965
SDL_GetWindowBrightness(SDL_Window * window)
slouken@5466
  1966
{
slouken@5466
  1967
    CHECK_WINDOW_MAGIC(window, 1.0f);
slouken@5466
  1968
slouken@5466
  1969
    return window->brightness;
slouken@5466
  1970
}
slouken@5466
  1971
slouken@5466
  1972
int
slouken@5466
  1973
SDL_SetWindowGammaRamp(SDL_Window * window, const Uint16 * red,
slouken@5466
  1974
                                            const Uint16 * green,
slouken@5466
  1975
                                            const Uint16 * blue)
slouken@5466
  1976
{
slouken@5466
  1977
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1978
slouken@5466
  1979
    if (!_this->SetWindowGammaRamp) {
icculus@7037
  1980
        return SDL_Unsupported();
slouken@5466
  1981
    }
slouken@5466
  1982
slouken@5466
  1983
    if (!window->gamma) {
slouken@5466
  1984
        if (SDL_GetWindowGammaRamp(window, NULL, NULL, NULL) < 0) {
slouken@5466
  1985
            return -1;
slouken@5466
  1986
        }
slouken@5466
  1987
    }
slouken@5466
  1988
slouken@5466
  1989
    if (red) {
slouken@5466
  1990
        SDL_memcpy(&window->gamma[0*256], red, 256*sizeof(Uint16));
slouken@5466
  1991
    }
slouken@5466
  1992
    if (green) {
slouken@5466
  1993
        SDL_memcpy(&window->gamma[1*256], green, 256*sizeof(Uint16));
slouken@5466
  1994
    }
slouken@5466
  1995
    if (blue) {
slouken@5466
  1996
        SDL_memcpy(&window->gamma[2*256], blue, 256*sizeof(Uint16));
slouken@5466
  1997
    }
slouken@5466
  1998
    if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@5466
  1999
        return _this->SetWindowGammaRamp(_this, window, window->gamma);
slouken@5466
  2000
    } else {
slouken@5466
  2001
        return 0;
slouken@5466
  2002
    }
slouken@5466
  2003
}
slouken@5466
  2004
slouken@5466
  2005
int
slouken@5466
  2006
SDL_GetWindowGammaRamp(SDL_Window * window, Uint16 * red,
slouken@5466
  2007
                                            Uint16 * green,
slouken@5466
  2008
                                            Uint16 * blue)
slouken@5466
  2009
{
slouken@5466
  2010
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  2011
slouken@5466
  2012
    if (!window->gamma) {
slouken@5466
  2013
        int i;
slouken@5466
  2014
slouken@5466
  2015
        window->gamma = (Uint16 *)SDL_malloc(256*6*sizeof(Uint16));
slouken@5466
  2016
        if (!window->gamma) {
icculus@7037
  2017
            return SDL_OutOfMemory();
slouken@5466
  2018
        }
slouken@5466
  2019
        window->saved_gamma = window->gamma + 3*256;
slouken@5466
  2020
slouken@5466
  2021
        if (_this->GetWindowGammaRamp) {
slouken@5466
  2022
            if (_this->GetWindowGammaRamp(_this, window, window->gamma) < 0) {
slouken@5466
  2023
                return -1;
slouken@5466
  2024
            }
slouken@5466
  2025
        } else {
slouken@5466
  2026
            /* Create an identity gamma ramp */
slouken@5466
  2027
            for (i = 0; i < 256; ++i) {
slouken@5466
  2028
                Uint16 value = (Uint16)((i << 8) | i);
slouken@5466
  2029
slouken@5466
  2030
                window->gamma[0*256+i] = value;
slouken@5466
  2031
                window->gamma[1*256+i] = value;
slouken@5466
  2032
                window->gamma[2*256+i] = value;
slouken@5466
  2033
            }
slouken@5466
  2034
        }
slouken@5466
  2035
        SDL_memcpy(window->saved_gamma, window->gamma, 3*256*sizeof(Uint16));
slouken@5466
  2036
    }
slouken@5466
  2037
slouken@5466
  2038
    if (red) {
slouken@5466
  2039
        SDL_memcpy(red, &window->gamma[0*256], 256*sizeof(Uint16));
slouken@5466
  2040
    }
slouken@5466
  2041
    if (green) {
slouken@5466
  2042
        SDL_memcpy(green, &window->gamma[1*256], 256*sizeof(Uint16));
slouken@5466
  2043
    }
slouken@5466
  2044
    if (blue) {
slouken@5466
  2045
        SDL_memcpy(blue, &window->gamma[2*256], 256*sizeof(Uint16));
slouken@5466
  2046
    }
slouken@5466
  2047
    return 0;
slouken@5466
  2048
}
slouken@5466
  2049
slouken@6662
  2050
void
slouken@2876
  2051
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  2052
{
slouken@6662
  2053
    if (_this->SetWindowGrab) {
slouken@6662
  2054
        SDL_bool grabbed;
slouken@6662
  2055
        if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@6662
  2056
            (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@6662
  2057
            grabbed = SDL_TRUE;
slouken@6662
  2058
        } else {
slouken@6662
  2059
            grabbed = SDL_FALSE;
slouken@6662
  2060
        }
slouken@6662
  2061
        _this->SetWindowGrab(_this, window, grabbed);
slouken@2753
  2062
    }
slouken@1895
  2063
}
slouken@1895
  2064
slouken@5403
  2065
void
slouken@5403
  2066
SDL_SetWindowGrab(SDL_Window * window, SDL_bool grabbed)
slouken@5403
  2067
{
slouken@5403
  2068
    CHECK_WINDOW_MAGIC(window, );
slouken@5403
  2069
slouken@5404
  2070
    if (!!grabbed == !!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
slouken@5403
  2071
        return;
slouken@5403
  2072
    }
slouken@5403
  2073
    if (grabbed) {
slouken@5403
  2074
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@5403
  2075
    } else {
slouken@5403
  2076
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@5403
  2077
    }
slouken@5403
  2078
    SDL_UpdateWindowGrab(window);
slouken@5403
  2079
}
slouken@5403
  2080
slouken@5403
  2081
SDL_bool
slouken@3685
  2082
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  2083
{
slouken@5466
  2084
    CHECK_WINDOW_MAGIC(window, SDL_FALSE);
slouken@3695
  2085
slouken@2753
  2086
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  2087
}
slouken@1895
  2088
slouken@1895
  2089
void
slouken@1895
  2090
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  2091
{
slouken@5404
  2092
    SDL_OnWindowRestored(window);
slouken@1895
  2093
}
slouken@1895
  2094
slouken@1895
  2095
void
slouken@1895
  2096
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  2097
{
slouken@5404
  2098
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@1895
  2099
}
slouken@1895
  2100
slouken@1895
  2101
void
slouken@5166
  2102
SDL_OnWindowResized(SDL_Window * window)
slouken@5166
  2103
{
slouken@5172
  2104
    window->surface_valid = SDL_FALSE;
slouken@5276
  2105
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
slouken@5166
  2106
}
slouken@5166
  2107
slouken@5166
  2108
void
slouken@3502
  2109
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  2110
{
slouken@5404
  2111
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3502
  2112
}
slouken@3502
  2113
slouken@3502
  2114
void
slouken@3502
  2115
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  2116
{
slouken@3685
  2117
    SDL_RaiseWindow(window);
slouken@5404
  2118
slouken@5404
  2119
    if (FULLSCREEN_VISIBLE(window)) {
slouken@5404
  2120
        SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@5404
  2121
    }
slouken@3502
  2122
}
slouken@3502
  2123
slouken@3502
  2124
void
jorgen@7089
  2125
SDL_OnWindowEnter(SDL_Window * window)
jorgen@7089
  2126
{
jorgen@7089
  2127
    if (_this->OnWindowEnter) {
jorgen@7089
  2128
        _this->OnWindowEnter(_this, window);
jorgen@7089
  2129
    }
jorgen@7089
  2130
}
jorgen@7089
  2131
jorgen@7089
  2132
void
jorgen@7089
  2133
SDL_OnWindowLeave(SDL_Window * window)
jorgen@7089
  2134
{
jorgen@7089
  2135
}
jorgen@7089
  2136
jorgen@7089
  2137
void
slouken@1895
  2138
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  2139
{
jorgen@7107
  2140
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7107
  2141
slouken@5466
  2142
    if (window->gamma && _this->SetWindowGammaRamp) {
slouken@5466
  2143
        _this->SetWindowGammaRamp(_this, window, window->gamma);
slouken@5466
  2144
    }
slouken@5466
  2145
jorgen@7107
  2146
    if (mouse && mouse->relative_mode) {
jorgen@7107
  2147
        SDL_SetMouseFocus(window);
jorgen@7107
  2148
        SDL_WarpMouseInWindow(window, window->w/2, window->h/2);
jorgen@7107
  2149
    }
jorgen@7107
  2150
slouken@6662
  2151
    SDL_UpdateWindowGrab(window);
slouken@1895
  2152
}
slouken@1895
  2153
slouken@7953
  2154
static SDL_bool
slouken@7953
  2155
ShouldMinimizeOnFocusLoss(SDL_Window * window)
slouken@6755
  2156
{
slouken@7954
  2157
    const char *hint;
slouken@7953
  2158
slouken@7953
  2159
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@7953
  2160
        return SDL_FALSE;
slouken@7953
  2161
    }
slouken@7953
  2162
slouken@7969
  2163
#ifdef __MACOSX__
slouken@7969
  2164
    if (Cocoa_IsWindowInFullscreenSpace(window)) {
slouken@7969
  2165
        return SDL_FALSE;
slouken@7953
  2166
    }
slouken@7969
  2167
#endif
slouken@7953
  2168
slouken@7954
  2169
    hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
slouken@7191
  2170
    if (hint) {
slouken@7191
  2171
        if (*hint == '0') {
slouken@7191
  2172
            return SDL_FALSE;
slouken@7191
  2173
        } else {
slouken@7191
  2174
            return SDL_TRUE;
slouken@7191
  2175
        }
slouken@7191
  2176
    }
slouken@7953
  2177
slouken@7969
  2178
    return SDL_TRUE;
slouken@6755
  2179
}
slouken@6755
  2180
slouken@1895
  2181
void
slouken@1895
  2182
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  2183
{
slouken@5466
  2184
    if (window->gamma && _this->SetWindowGammaRamp) {
slouken@5466
  2185
        _this->SetWindowGammaRamp(_this, window, window->saved_gamma);
slouken@3512
  2186
    }
slouken@3512
  2187
slouken@6662
  2188
    SDL_UpdateWindowGrab(window);
slouken@5466
  2189
slouken@7953
  2190
    if (ShouldMinimizeOnFocusLoss(window)) {
icculus@7565
  2191
        SDL_MinimizeWindow(window);
slouken@5466
  2192
    }
slouken@1895
  2193
}
slouken@1895
  2194
slouken@3685
  2195
SDL_Window *
slouken@1895
  2196
SDL_GetFocusWindow(void)
slouken@1895
  2197
{
slouken@3685
  2198
    SDL_Window *window;
slouken@1895
  2199
slouken@2753
  2200
    if (!_this) {
slouken@3685
  2201
        return NULL;
slouken@3685
  2202
    }
slouken@5246
  2203
    for (window = _this->windows; window; window = window->next) {
slouken@2753
  2204
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  2205
            return window;
slouken@2753
  2206
        }
slouken@2753
  2207
    }
slouken@3685
  2208
    return NULL;
slouken@1895
  2209
}
slouken@1895
  2210
slouken@1895
  2211
void
slouken@3685
  2212
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  2213
{
slouken@3685
  2214
    SDL_VideoDisplay *display;
slouken@3685
  2215
slouken@3695
  2216
    CHECK_WINDOW_MAGIC(window, );
slouken@3503
  2217
slouken@6214
  2218
    /* Restore video mode, etc. */
slouken@6214
  2219
    SDL_HideWindow(window);
slouken@6214
  2220
slouken@6214
  2221
    /* Make sure this window no longer has focus */
slouken@6214
  2222
    if (SDL_GetKeyboardFocus() == window) {
slouken@6214
  2223
        SDL_SetKeyboardFocus(NULL);
slouken@6214
  2224
    }
slouken@6214
  2225
    if (SDL_GetMouseFocus() == window) {
slouken@6214
  2226
        SDL_SetMouseFocus(NULL);
slouken@6214
  2227
    }
slouken@6214
  2228
icculus@5563
  2229
    /* make no context current if this is the current context window. */
icculus@5563
  2230
    if (window->flags & SDL_WINDOW_OPENGL) {
icculus@5563
  2231
        if (_this->current_glwin == window) {
slouken@6496
  2232
            SDL_GL_MakeCurrent(window, NULL);
icculus@5563
  2233
        }
icculus@5563
  2234
    }
icculus@5563
  2235
slouken@5169
  2236
    if (window->surface) {
slouken@5288
  2237
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5169
  2238
        SDL_FreeSurface(window->surface);
slouken@5169
  2239
    }
slouken@5166
  2240
    if (_this->DestroyWindowFramebuffer) {
slouken@5166
  2241
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5166
  2242
    }
slouken@3685
  2243
    if (_this->DestroyWindow) {
slouken@3685
  2244
        _this->DestroyWindow(_this, window);
slouken@3685
  2245
    }
slouken@3685
  2246
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  2247
        SDL_GL_UnloadLibrary();
slouken@3685
  2248
    }
slouken@3685
  2249
slouken@5246
  2250
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  2251
    if (display->fullscreen_window == window) {
slouken@5246
  2252
        display->fullscreen_window = NULL;
slouken@5246
  2253
    }
slouken@5246
  2254
slouken@4901
  2255
    /* Now invalidate magic */
slouken@4897
  2256
    window->magic = NULL;
slouken@4897
  2257
slouken@5165
  2258
    /* Free memory associated with the window */
slouken@7719
  2259
    SDL_free(window->title);
slouken@7720
  2260
    SDL_FreeSurface(window->icon);
slouken@7719
  2261
    SDL_free(window->gamma);
slouken@5165
  2262
    while (window->data) {
slouken@5165
  2263
        SDL_WindowUserData *data = window->data;
slouken@5165
  2264
slouken@5165
  2265
        window->data = data->next;
slouken@5165
  2266
        SDL_free(data->name);
slouken@5165
  2267
        SDL_free(data);
slouken@5165
  2268
    }
slouken@5165
  2269
slouken@3685
  2270
    /* Unlink the window from the list */
slouken@3693
  2271
    if (window->next) {
slouken@3693
  2272
        window->next->prev = window->prev;
slouken@3693
  2273
    }
slouken@3685
  2274
    if (window->prev) {
slouken@3685
  2275
        window->prev->next = window->next;
slouken@3685
  2276
    } else {
slouken@5246
  2277
        _this->windows = window->next;
slouken@3685
  2278
    }
slouken@3685
  2279
slouken@3685
  2280
    SDL_free(window);
slouken@1895
  2281
}
slouken@1895
  2282
slouken@3025
  2283
SDL_bool
slouken@3025
  2284
SDL_IsScreenSaverEnabled()
slouken@3025
  2285
{
slouken@3025
  2286
    if (!_this) {
slouken@3025
  2287
        return SDL_TRUE;
slouken@3025
  2288
    }
slouken@3025
  2289
    return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
slouken@3025
  2290
}
slouken@3025
  2291
slouken@3025
  2292
void
slouken@3025
  2293
SDL_EnableScreenSaver()
slouken@3025
  2294
{
slouken@3025
  2295
    if (!_this) {
slouken@3025
  2296
        return;
slouken@3025
  2297
    }
slouken@3025
  2298
    if (!_this->suspend_screensaver) {
slouken@3025
  2299
        return;
slouken@3025
  2300
    }
slouken@3025
  2301
    _this->suspend_screensaver = SDL_FALSE;
slouken@3025
  2302
    if (_this->SuspendScreenSaver) {
slouken@3025
  2303
        _this->SuspendScreenSaver(_this);
slouken@3025
  2304
    }
slouken@3025
  2305
}
slouken@3025
  2306
slouken@3025
  2307
void
slouken@3025
  2308
SDL_DisableScreenSaver()
slouken@3025
  2309
{
slouken@3025
  2310
    if (!_this) {
slouken@3025
  2311
        return;
slouken@3025
  2312
    }
slouken@3025
  2313
    if (_this->suspend_screensaver) {
slouken@3025
  2314
        return;
slouken@3025
  2315
    }
slouken@3025
  2316
    _this->suspend_screensaver = SDL_TRUE;
slouken@3025
  2317
    if (_this->SuspendScreenSaver) {
slouken@3025
  2318
        _this->SuspendScreenSaver(_this);
slouken@3025
  2319
    }
slouken@3025
  2320
}
slouken@3025
  2321
slouken@1895
  2322
void
slouken@1895
  2323
SDL_VideoQuit(void)
slouken@1895
  2324
{
slouken@2753
  2325
    int i, j;
slouken@1895
  2326
slouken@2753
  2327
    if (!_this) {
slouken@2753
  2328
        return;
slouken@2753
  2329
    }
slouken@5123
  2330
slouken@2753
  2331
    /* Halt event processing before doing anything else */
slouken@7360
  2332
    SDL_TouchQuit();
slouken@5123
  2333
    SDL_MouseQuit();
slouken@5123
  2334
    SDL_KeyboardQuit();
slouken@7360
  2335
    SDL_QuitSubSystem(SDL_INIT_EVENTS);
slouken@5123
  2336
slouken@3029
  2337
    SDL_EnableScreenSaver();
slouken@1895
  2338
slouken@2753
  2339
    /* Clean up the system video */
slouken@5246
  2340
    while (_this->windows) {
slouken@5246
  2341
        SDL_DestroyWindow(_this->windows);
slouken@2753
  2342
    }
slouken@2753
  2343
    _this->VideoQuit(_this);
slouken@1895
  2344
icculus@7441
  2345
    for (i = 0; i < _this->num_displays; ++i) {
slouken@2753
  2346
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  2347
        for (j = display->num_display_modes; j--;) {
slouken@7719
  2348
            SDL_free(display->display_modes[j].driverdata);
slouken@7719
  2349
            display->display_modes[j].driverdata = NULL;
slouken@2753
  2350
        }
slouken@7719
  2351
        SDL_free(display->display_modes);
slouken@7719
  2352
        display->display_modes = NULL;
slouken@7719
  2353
        SDL_free(display->desktop_mode.driverdata);
slouken@7719
  2354
        display->desktop_mode.driverdata = NULL;
slouken@7719
  2355
        SDL_free(display->driverdata);
slouken@7719
  2356
        display->driverdata = NULL;
slouken@2753
  2357
    }
slouken@2753
  2358
    if (_this->displays) {
slouken@6787
  2359
        for (i = 0; i < _this->num_displays; ++i) {
slouken@6787
  2360
            SDL_free(_this->displays[i].name);
slouken@6787
  2361
        }
slouken@2753
  2362
        SDL_free(_this->displays);
slouken@2753
  2363
        _this->displays = NULL;
slouken@6787
  2364
        _this->num_displays = 0;
slouken@2753
  2365
    }
slouken@7719
  2366
    SDL_free(_this->clipboard_text);
slouken@7719
  2367
    _this->clipboard_text = NULL;
slouken@2753
  2368
    _this->free(_this);
slouken@2753
  2369
    _this = NULL;
slouken@0
  2370
}
slouken@0
  2371
slouken@1895
  2372
int
slouken@1895
  2373
SDL_GL_LoadLibrary(const char *path)
slouken@0
  2374
{
slouken@2753
  2375
    int retval;
slouken@0
  2376
slouken@2753
  2377
    if (!_this) {
icculus@7037
  2378
        return SDL_UninitializedVideo();
slouken@2753
  2379
    }
slouken@3057
  2380
    if (_this->gl_config.driver_loaded) {
slouken@3057
  2381
        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
icculus@7037
  2382
            return SDL_SetError("OpenGL library already loaded");
slouken@3057
  2383
        }
slouken@3057
  2384
        retval = 0;
slouken@3057
  2385
    } else {
slouken@3057
  2386
        if (!_this->GL_LoadLibrary) {
slouken@7865
  2387
            return SDL_SetError("No dynamic GL support in video driver");
slouken@3057
  2388
        }
slouken@2753
  2389
        retval = _this->GL_LoadLibrary(_this, path);
slouken@3057
  2390
    }
slouken@3057
  2391
    if (retval == 0) {
slouken@3057
  2392
        ++_this->gl_config.driver_loaded;
slouken@7865
  2393
    } else {
slouken@7865
  2394
        if (_this->GL_UnloadLibrary) {
slouken@7865
  2395
            _this->GL_UnloadLibrary(_this);
slouken@7865
  2396
        }
slouken@2753
  2397
    }
slouken@2753
  2398
    return (retval);
slouken@0
  2399
}
slouken@0
  2400
slouken@2753
  2401
void *
slouken@1895
  2402
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  2403
{
slouken@2753
  2404
    void *func;
slouken@0
  2405
slouken@2753
  2406
    if (!_this) {
slouken@2753
  2407
        SDL_UninitializedVideo();
slouken@2753
  2408
        return NULL;
slouken@2753
  2409
    }
slouken@2753
  2410
    func = NULL;
slouken@2753
  2411
    if (_this->GL_GetProcAddress) {
slouken@2753
  2412
        if (_this->gl_config.driver_loaded) {
slouken@2753
  2413
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  2414
        } else {
slouken@2753
  2415
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  2416
        }
slouken@2753
  2417
    } else {
slouken@2753
  2418
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  2419
    }
slouken@2753
  2420
    return func;
slouken@0
  2421
}
slouken@0
  2422
slouken@3057
  2423
void
slouken@3057
  2424
SDL_GL_UnloadLibrary(void)
slouken@3057
  2425
{
slouken@3057
  2426
    if (!_this) {
slouken@3057
  2427
        SDL_UninitializedVideo();
slouken@3057
  2428
        return;
slouken@3057
  2429
    }
slouken@3057
  2430
    if (_this->gl_config.driver_loaded > 0) {
slouken@3057
  2431
        if (--_this->gl_config.driver_loaded > 0) {
slouken@3057
  2432
            return;
slouken@3057
  2433
        }
slouken@3057
  2434
        if (_this->GL_UnloadLibrary) {
slouken@3057
  2435
            _this->GL_UnloadLibrary(_this);
slouken@3057
  2436
        }
slouken@3057
  2437
    }
slouken@3057
  2438
}
slouken@3057
  2439
slouken@7860
  2440
static SDL_INLINE SDL_bool
icculus@7210
  2441
isAtLeastGL3(const char *verstr)
icculus@7210
  2442
{
icculus@7210
  2443
    return ( verstr && (SDL_atoi(verstr) >= 3) );
icculus@7210
  2444
}
icculus@7210
  2445
slouken@1926
  2446
SDL_bool
slouken@1926
  2447
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  2448
{
slouken@5209
  2449
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2450
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  2451
    const char *extensions;
slouken@2753
  2452
    const char *start;
slouken@2753
  2453
    const char *where, *terminator;
slouken@1926
  2454
slouken@2753
  2455
    /* Extension names should not have spaces. */
slouken@2753
  2456
    where = SDL_strchr(extension, ' ');
slouken@2753
  2457
    if (where || *extension == '\0') {
slouken@2753
  2458
        return SDL_FALSE;
slouken@2753
  2459
    }
slouken@2753
  2460
    /* See if there's an environment variable override */
slouken@2753
  2461
    start = SDL_getenv(extension);
slouken@2753
  2462
    if (start && *start == '0') {
slouken@2753
  2463
        return SDL_FALSE;
slouken@2753
  2464
    }
icculus@7210
  2465
slouken@2753
  2466
    /* Lookup the available extensions */
icculus@7210
  2467
slouken@2753
  2468
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
icculus@7210
  2469
    if (!glGetStringFunc) {
icculus@7210
  2470
        return SDL_FALSE;
slouken@2753
  2471
    }
icculus@7210
  2472
icculus@7210
  2473
    if (isAtLeastGL3((const char *) glGetStringFunc(GL_VERSION))) {
icculus@7210
  2474
        const GLubyte *(APIENTRY * glGetStringiFunc) (GLenum, GLuint);
icculus@7210
  2475
        void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
icculus@7210
  2476
        GLint num_exts = 0;
icculus@7210
  2477
        GLint i;
icculus@7210
  2478
icculus@7210
  2479
        glGetStringiFunc = SDL_GL_GetProcAddress("glGetStringi");
icculus@7210
  2480
        glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
icculus@7210
  2481
        if ((!glGetStringiFunc) || (!glGetIntegervFunc)) {
icculus@7210
  2482
            return SDL_FALSE;
icculus@7210
  2483
        }
icculus@7210
  2484
icculus@7212
  2485
        #ifndef GL_NUM_EXTENSIONS
icculus@7212
  2486
        #define GL_NUM_EXTENSIONS 0x821D
icculus@7212
  2487
        #endif
icculus@7210
  2488
        glGetIntegervFunc(GL_NUM_EXTENSIONS, &num_exts);
icculus@7210
  2489
        for (i = 0; i < num_exts; i++) {
icculus@7210
  2490
            const char *thisext = (const char *) glGetStringiFunc(GL_EXTENSIONS, i);
icculus@7210
  2491
            if (SDL_strcmp(thisext, extension) == 0) {
icculus@7210
  2492
                return SDL_TRUE;
icculus@7210
  2493
            }
icculus@7210
  2494
        }
icculus@7210
  2495
icculus@7210
  2496
        return SDL_FALSE;
icculus@7210
  2497
    }
icculus@7210
  2498
icculus@7210
  2499
    /* Try the old way with glGetString(GL_EXTENSIONS) ... */
icculus@7210
  2500
icculus@7210
  2501
    extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  2502
    if (!extensions) {
slouken@2753
  2503
        return SDL_FALSE;
slouken@2753
  2504
    }
slouken@2753
  2505
    /*
slouken@2753
  2506
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  2507
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  2508
     */
slouken@1926
  2509
slouken@2753
  2510
    start = extensions;
slouken@1926
  2511
slouken@2753
  2512
    for (;;) {
slouken@2753
  2513
        where = SDL_strstr(start, extension);
slouken@2753
  2514
        if (!where)
slouken@2753
  2515
            break;
slouken@1926
  2516
slouken@2753
  2517
        terminator = where + SDL_strlen(extension);
slouken@2753
  2518
        if (where == start || *(where - 1) == ' ')
slouken@2753
  2519
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  2520
                return SDL_TRUE;
slouken@1926
  2521
slouken@2753
  2522
        start = terminator;
slouken@2753
  2523
    }
slouken@2753
  2524
    return SDL_FALSE;
slouken@1926
  2525
#else
slouken@2753
  2526
    return SDL_FALSE;
slouken@1926
  2527
#endif
slouken@1926
  2528
}
slouken@1926
  2529
slouken@1895
  2530
int
slouken@1895
  2531
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2532
{
slouken@5209
  2533
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2534
    int retval;
slouken@0
  2535
slouken@2753
  2536
    if (!_this) {
icculus@7037
  2537
        return SDL_UninitializedVideo();
slouken@2753
  2538
    }
slouken@2753
  2539
    retval = 0;
slouken@2753
  2540
    switch (attr) {
slouken@2753
  2541
    case SDL_GL_RED_SIZE:
slouken@2753
  2542
        _this->gl_config.red_size = value;
slouken@2753
  2543
        break;
slouken@2753
  2544
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2545
        _this->gl_config.green_size = value;
slouken@2753
  2546
        break;
slouken@2753
  2547
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2548
        _this->gl_config.blue_size = value;
slouken@2753
  2549
        break;
slouken@2753
  2550
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2551
        _this->gl_config.alpha_size = value;
slouken@2753
  2552
        break;
slouken@2753
  2553
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  2554
        _this->gl_config.double_buffer = value;
slouken@2753
  2555
        break;
slouken@2753
  2556
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2557
        _this->gl_config.buffer_size = value;
slouken@2753
  2558
        break;
slouken@2753
  2559
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2560
        _this->gl_config.depth_size = value;
slouken@2753
  2561
        break;
slouken@2753
  2562
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2563
        _this->gl_config.stencil_size = value;
slouken@2753
  2564
        break;
slouken@2753
  2565
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2566
        _this->gl_config.accum_red_size = value;
slouken@2753
  2567
        break;
slouken@2753
  2568
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2569
        _this->gl_config.accum_green_size = value;
slouken@2753
  2570
        break;
slouken@2753
  2571
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2572
        _this->gl_config.accum_blue_size = value;
slouken@2753
  2573
        break;
slouken@2753
  2574
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2575
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  2576
        break;
slouken@2753
  2577
    case SDL_GL_STEREO:
slouken@2753
  2578
        _this->gl_config.stereo = value;
slouken@2753
  2579
        break;
slouken@2753
  2580
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  2581
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  2582
        break;
slouken@2753
  2583
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  2584
        _this->gl_config.multisamplesamples = value;
slouken@2753
  2585
        break;
slouken@2753
  2586
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2587
        _this->gl_config.accelerated = value;
slouken@2753
  2588
        break;
slouken@2753
  2589
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2590
        _this->gl_config.retained_backing = value;
slouken@2753
  2591
        break;
slouken@3100
  2592
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@3100
  2593
        _this->gl_config.major_version = value;
slouken@3100
  2594
        break;
slouken@3100
  2595
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@3100
  2596
        _this->gl_config.minor_version = value;
slouken@3100
  2597
        break;
slouken@6370
  2598
    case SDL_GL_CONTEXT_EGL:
gabomdq@7723
  2599
        /* FIXME: SDL_GL_CONTEXT_EGL to be deprecated in SDL 2.1 */
gabomdq@7723
  2600
        if (value != 0) {
gabomdq@7723
  2601
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
gabomdq@7723
  2602
        } else {
gabomdq@7723
  2603
            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
gabomdq@7723
  2604
        };
slouken@6370
  2605
        break;
slouken@6296
  2606
    case SDL_GL_CONTEXT_FLAGS:
slouken@6393
  2607
        if( value & ~(SDL_GL_CONTEXT_DEBUG_FLAG |
slouken@7191
  2608
              SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG |
slouken@7191
  2609
              SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG |
slouken@7191
  2610
              SDL_GL_CONTEXT_RESET_ISOLATION_FLAG) ) {
slouken@7191
  2611
        retval = SDL_SetError("Unknown OpenGL context flag %d", value);
slouken@7191
  2612
        break;
slouken@7191
  2613
    }
slouken@6296
  2614
        _this->gl_config.flags = value;
slouken@6296
  2615
        break;
slouken@6296
  2616
    case SDL_GL_CONTEXT_PROFILE_MASK:
slouken@6393
  2617
        if( value != 0 &&
slouken@7191
  2618
        value != SDL_GL_CONTEXT_PROFILE_CORE &&
slouken@7191
  2619
        value != SDL_GL_CONTEXT_PROFILE_COMPATIBILITY &&
slouken@7191
  2620
        value != SDL_GL_CONTEXT_PROFILE_ES ) {
slouken@7191
  2621
        retval = SDL_SetError("Unknown OpenGL context profile %d", value);
slouken@7191
  2622
        break;
slouken@7191
  2623
    }
slouken@6296
  2624
        _this->gl_config.profile_mask = value;
slouken@6296
  2625
        break;
slouken@6393
  2626
    case SDL_GL_SHARE_WITH_CURRENT_CONTEXT:
slouken@6393
  2627
        _this->gl_config.share_with_current_context = value;
icculus@7853
  2628
        break;
icculus@7853
  2629
    case SDL_GL_FRAMEBUFFER_SRGB_CAPABLE:
icculus@7853
  2630
        _this->gl_config.framebuffer_srgb_capable = value;
icculus@7853
  2631
        break;
slouken@2753
  2632
    default:
icculus@7037
  2633
        retval = SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  2634
        break;
slouken@2753
  2635
    }
slouken@2753
  2636
    return retval;