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