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