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