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