src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Oct 2013 21:56:15 -0700
changeset 7860 2b0bcdea3a79
parent 7853 4861edda71d1
child 7865 f2a42ca4ddf0
permissions -rw-r--r--
Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings

Andreas Ertelt

The problem in question is caused by changeset 7771 (http://hg.libsdl.org/SDL/rev/4434498bf4b9 / https://bugzilla.libsdl.org/show_bug.cgi?id=2121)

The redefinition of __inline__ (introduced by the addition of begin_code.h:128's "|| __STRICT_ANSI__") results in mingw's gcc throwing multiple

warning: always_inline function might not be inlinable [-Wattributes]

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