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