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