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