src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 18:16:39 -0800
changeset 5479 5c4635640bf9
parent 5478 6b65ff3cee62
child 5480 55a687c55676
permissions -rw-r--r--
Fixed bug 1165 (SDL_GetMouseState() returns wrong location after switch to/from fullscreen)

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