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