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