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