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