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