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