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