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