src/video/SDL_video.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 13 Jul 2013 21:50:40 -0400
changeset 7440 9817477202d1
parent 7439 f52afd324ec7
child 7441 468877c2135a
permissions -rw-r--r--
Don't copy structs to stack in cmpmodes(), use const pointers instead.

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