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