src/video/SDL_video.c
author Ryan C. Gordon
Tue, 06 Sep 2016 13:13:03 -0400
changeset 10312 943b722a8e61
parent 10166 2393160743ca
child 10385 ad12658bc7ae
permissions -rw-r--r--
emscripten: special case to make SDL_ShowSimpleMessageBox() work.

Browsers don't have the functionality to fully support the generic
SDL_ShowMessageBox(), but this handles the likely most-common case.

Without this, you'd return immediately with a proper error result and no UI,
but probably no one checks that for SDL_ShowSimpleMessageBox. And if they
did: what would they do to handle this anyhow?

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