src/video/SDL_video.c
author Gabriel Jacobo <gabomdq@gmail.com>
Thu, 29 Aug 2013 15:02:32 -0300
changeset 7723 aea98cc3e696
parent 7721 bdf8c2c84e14
child 7742 84e32c3d415c
permissions -rw-r--r--
Fixes bug #2040, prepare SDL_GL_CONTEXT_EGL for deprecation on v2.1

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