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