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