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