src/video/SDL_video.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 15 Jan 2014 11:17:09 -0800
changeset 8110 98646f687e2b
parent 8093 b43765095a6f
child 8112 257daea8ac11
permissions -rw-r--r--
Allow switching between FS and desktop FS.

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