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