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