src/video/SDL_video.c
author Sylvain Becker <sylvain.becker@gmail.com>
Sat, 15 Dec 2018 16:21:24 +0100
changeset 12476 11f8bd1899c5
parent 12391 ca6e41ade79a
child 12503 806492103856
permissions -rw-r--r--
Fixed bug 4426 - allows re-creation of software renderer

Switching between renderers "software -> opengl -> opengles2 -> software" fails.

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