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