src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5294 4801b8bede31
child 5305 75f5ff92ad08
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

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