src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Feb 2011 14:34:54 -0800
changeset 5154 fb424691cfc7
parent 5150 1435f8a6425c
child 5165 2b1989f59674
permissions -rw-r--r--
Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
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@2967
   985
void
slouken@3685
   986
SDL_SetWindowData(SDL_Window * window, void *userdata)
slouken@1895
   987
{
slouken@3695
   988
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
   989
slouken@2753
   990
    window->userdata = userdata;
slouken@1895
   991
}
slouken@1895
   992
slouken@2753
   993
void *
slouken@3685
   994
SDL_GetWindowData(SDL_Window * window)
slouken@1895
   995
{
slouken@3695
   996
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
   997
slouken@2753
   998
    return window->userdata;
slouken@1895
   999
}
slouken@1895
  1000
slouken@1895
  1001
void
slouken@3685
  1002
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1003
{
slouken@3695
  1004
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1005
slouken@2875
  1006
    if (x != SDL_WINDOWPOS_UNDEFINED) {
slouken@2753
  1007
        window->x = x;
slouken@2753
  1008
    }
slouken@2875
  1009
    if (y != SDL_WINDOWPOS_UNDEFINED) {
slouken@2753
  1010
        window->y = y;
slouken@2753
  1011
    }
slouken@2753
  1012
    if (_this->SetWindowPosition) {
slouken@2753
  1013
        _this->SetWindowPosition(_this, window);
slouken@2753
  1014
    }
slouken@3685
  1015
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
  1016
}
slouken@1895
  1017
slouken@1895
  1018
void
slouken@3685
  1019
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1020
{
slouken@3695
  1021
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1022
slouken@2753
  1023
    if (x) {
slouken@2753
  1024
        *x = window->x;
slouken@2753
  1025
    }
slouken@2753
  1026
    if (y) {
slouken@2753
  1027
        *y = window->y;
slouken@2753
  1028
    }
slouken@1895
  1029
}
slouken@1895
  1030
slouken@1895
  1031
void
slouken@3685
  1032
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1033
{
slouken@3695
  1034
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1035
slouken@2753
  1036
    window->w = w;
slouken@2753
  1037
    window->h = h;
slouken@1895
  1038
slouken@2753
  1039
    if (_this->SetWindowSize) {
slouken@2753
  1040
        _this->SetWindowSize(_this, window);
slouken@2753
  1041
    }
slouken@5147
  1042
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@1895
  1043
}
slouken@1895
  1044
slouken@1895
  1045
void
slouken@3685
  1046
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1047
{
slouken@2860
  1048
    if (window) {
slouken@2849
  1049
        if (w) {
slouken@2849
  1050
            *w = window->w;
slouken@2849
  1051
        }
slouken@2849
  1052
        if (h) {
slouken@2849
  1053
            *h = window->h;
slouken@2849
  1054
        }
slouken@2849
  1055
    } else {
slouken@2849
  1056
        if (w) {
slouken@2849
  1057
            *w = 0;
slouken@2849
  1058
        }
slouken@2849
  1059
        if (h) {
slouken@2849
  1060
            *h = 0;
slouken@2849
  1061
        }
slouken@2753
  1062
    }
slouken@1895
  1063
}
slouken@1895
  1064
slouken@1895
  1065
void
slouken@3685
  1066
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1067
{
slouken@3695
  1068
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1069
slouken@3695
  1070
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@2753
  1071
        return;
slouken@2753
  1072
    }
slouken@1895
  1073
slouken@2753
  1074
    if (_this->ShowWindow) {
slouken@2753
  1075
        _this->ShowWindow(_this, window);
slouken@2753
  1076
    }
slouken@3685
  1077
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1078
}
slouken@1895
  1079
slouken@1895
  1080
void
slouken@3685
  1081
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1082
{
slouken@3695
  1083
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1084
slouken@3695
  1085
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1086
        return;
slouken@2753
  1087
    }
slouken@1895
  1088
slouken@2753
  1089
    if (_this->HideWindow) {
slouken@2753
  1090
        _this->HideWindow(_this, window);
slouken@2753
  1091
    }
slouken@3685
  1092
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1093
}
slouken@1895
  1094
slouken@1895
  1095
void
slouken@3685
  1096
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1097
{
slouken@3695
  1098
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1099
slouken@3695
  1100
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1101
        return;
slouken@2753
  1102
    }
slouken@2753
  1103
    if (_this->RaiseWindow) {
slouken@2753
  1104
        _this->RaiseWindow(_this, window);
slouken@3502
  1105
    } else {
slouken@3502
  1106
        /* FIXME: What we really want is a way to request focus */
slouken@3685
  1107
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@2753
  1108
    }
slouken@1895
  1109
}
slouken@1895
  1110
slouken@1895
  1111
void
slouken@3685
  1112
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1113
{
slouken@3695
  1114
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1115
slouken@3695
  1116
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
  1117
        return;
slouken@2753
  1118
    }
slouken@1895
  1119
slouken@2753
  1120
    if (_this->MaximizeWindow) {
slouken@2753
  1121
        _this->MaximizeWindow(_this, window);
slouken@2753
  1122
    }
slouken@3685
  1123
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
  1124
}
slouken@1895
  1125
slouken@1895
  1126
void
slouken@3685
  1127
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1128
{
slouken@3695
  1129
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1130
slouken@3695
  1131
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
  1132
        return;
slouken@2753
  1133
    }
slouken@1895
  1134
slouken@2753
  1135
    if (_this->MinimizeWindow) {
slouken@2753
  1136
        _this->MinimizeWindow(_this, window);
slouken@2753
  1137
    }
slouken@3685
  1138
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
  1139
}
slouken@1895
  1140
slouken@1895
  1141
void
slouken@3685
  1142
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1143
{
slouken@3695
  1144
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1145
slouken@3695
  1146
    if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1147
        return;
slouken@2753
  1148
    }
slouken@1895
  1149
slouken@2753
  1150
    if (_this->RestoreWindow) {
slouken@2753
  1151
        _this->RestoreWindow(_this, window);
slouken@2753
  1152
    }
slouken@3685
  1153
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
  1154
}
slouken@1895
  1155
slouken@1895
  1156
int
slouken@3685
  1157
SDL_SetWindowFullscreen(SDL_Window * window, int fullscreen)
slouken@1895
  1158
{
slouken@3695
  1159
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1160
slouken@2753
  1161
    if (fullscreen) {
slouken@2753
  1162
        fullscreen = SDL_WINDOW_FULLSCREEN;
slouken@2753
  1163
    }
slouken@2753
  1164
    if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
slouken@2753
  1165
        return 0;
slouken@2753
  1166
    }
slouken@2753
  1167
    if (fullscreen) {
slouken@2753
  1168
        window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@1895
  1169
slouken@3502
  1170
        SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@2753
  1171
    } else {
slouken@2753
  1172
        window->flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@1895
  1173
slouken@3502
  1174
        SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@2753
  1175
    }
slouken@2753
  1176
    return 0;
slouken@1895
  1177
}
slouken@1895
  1178
slouken@1895
  1179
void
slouken@3685
  1180
SDL_SetWindowGrab(SDL_Window * window, int mode)
slouken@1895
  1181
{
slouken@3695
  1182
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1183
slouken@3695
  1184
    if ((!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@2753
  1185
        return;
slouken@2753
  1186
    }
slouken@2753
  1187
    if (mode) {
slouken@2753
  1188
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1189
    } else {
slouken@2753
  1190
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@2753
  1191
    }
slouken@2875
  1192
    SDL_UpdateWindowGrab(window);
slouken@2875
  1193
}
slouken@1895
  1194
slouken@2875
  1195
static void
slouken@2876
  1196
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  1197
{
slouken@2753
  1198
    if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
slouken@2753
  1199
        _this->SetWindowGrab(_this, window);
slouken@2753
  1200
    }
slouken@1895
  1201
}
slouken@1895
  1202
slouken@1895
  1203
int
slouken@3685
  1204
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  1205
{
slouken@3695
  1206
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1207
slouken@2753
  1208
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  1209
}
slouken@1895
  1210
slouken@1895
  1211
void
slouken@1895
  1212
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  1213
{
slouken@3685
  1214
    SDL_RaiseWindow(window);
slouken@3502
  1215
    SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@1895
  1216
}
slouken@1895
  1217
slouken@1895
  1218
void
slouken@1895
  1219
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  1220
{
slouken@3502
  1221
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@1895
  1222
}
slouken@1895
  1223
slouken@1895
  1224
void
slouken@3502
  1225
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  1226
{
slouken@3502
  1227
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3502
  1228
}
slouken@3502
  1229
slouken@3502
  1230
void
slouken@3502
  1231
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  1232
{
slouken@3685
  1233
    SDL_RaiseWindow(window);
slouken@3502
  1234
    SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@3502
  1235
}
slouken@3502
  1236
slouken@3502
  1237
void
slouken@1895
  1238
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  1239
{
slouken@3685
  1240
    SDL_VideoDisplay *display = window->display;
slouken@1895
  1241
slouken@2753
  1242
    if (display->gamma && _this->SetDisplayGammaRamp) {
slouken@3500
  1243
        _this->SetDisplayGammaRamp(_this, display, display->gamma);
slouken@2753
  1244
    }
slouken@2876
  1245
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1246
        && _this->SetWindowGrab) {
slouken@2753
  1247
        _this->SetWindowGrab(_this, window);
slouken@2753
  1248
    }
slouken@1895
  1249
}
slouken@1895
  1250
slouken@1895
  1251
void
slouken@1895
  1252
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  1253
{
slouken@3685
  1254
    SDL_VideoDisplay *display = window->display;
slouken@1895
  1255
slouken@3512
  1256
    /* If we're fullscreen on a single-head system and lose focus, minimize */
slouken@3512
  1257
    if ((window->flags & SDL_WINDOW_FULLSCREEN) &&
slouken@3512
  1258
        _this->num_displays == 1) {
slouken@3685
  1259
        SDL_MinimizeWindow(window);
slouken@3512
  1260
    }
slouken@3512
  1261
slouken@2753
  1262
    if (display->gamma && _this->SetDisplayGammaRamp) {
slouken@3500
  1263
        _this->SetDisplayGammaRamp(_this, display, display->saved_gamma);
slouken@2753
  1264
    }
slouken@2876
  1265
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
  1266
        && _this->SetWindowGrab) {
slouken@2753
  1267
        _this->SetWindowGrab(_this, window);
slouken@2753
  1268
    }
slouken@1895
  1269
}
slouken@1895
  1270
slouken@3685
  1271
SDL_Window *
slouken@1895
  1272
SDL_GetFocusWindow(void)
slouken@1895
  1273
{
slouken@2753
  1274
    SDL_VideoDisplay *display;
slouken@3685
  1275
    SDL_Window *window;
slouken@1895
  1276
slouken@2753
  1277
    if (!_this) {
slouken@3685
  1278
        return NULL;
slouken@3685
  1279
    }
slouken@3685
  1280
    display = SDL_CurrentDisplay;
slouken@3685
  1281
    for (window = display->windows; window; window = window->next) {
slouken@2753
  1282
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  1283
            return window;
slouken@2753
  1284
        }
slouken@2753
  1285
    }
slouken@3685
  1286
    return NULL;
slouken@1895
  1287
}
slouken@1895
  1288
slouken@1895
  1289
void
slouken@3685
  1290
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  1291
{
slouken@3685
  1292
    SDL_VideoDisplay *display;
slouken@3685
  1293
slouken@3695
  1294
    CHECK_WINDOW_MAGIC(window, );
slouken@3503
  1295
slouken@3685
  1296
    if (window->title) {
slouken@3685
  1297
        SDL_free(window->title);
slouken@3685
  1298
    }
slouken@3685
  1299
slouken@3685
  1300
    /* Restore video mode, etc. */
slouken@3685
  1301
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3685
  1302
slouken@3685
  1303
    if (_this->DestroyWindow) {
slouken@3685
  1304
        _this->DestroyWindow(_this, window);
slouken@3685
  1305
    }
slouken@3685
  1306
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  1307
        SDL_GL_UnloadLibrary();
slouken@3685
  1308
    }
slouken@3685
  1309
slouken@4901
  1310
    /* Now invalidate magic */
slouken@4897
  1311
    window->magic = NULL;
slouken@4897
  1312
slouken@3685
  1313
    /* Unlink the window from the list */
slouken@3685
  1314
    display = window->display;
slouken@3693
  1315
    if (window->next) {
slouken@3693
  1316
        window->next->prev = window->prev;
slouken@3693
  1317
    }
slouken@3685
  1318
    if (window->prev) {
slouken@3685
  1319
        window->prev->next = window->next;
slouken@3685
  1320
    } else {
slouken@3685
  1321
        display->windows = window->next;
slouken@3685
  1322
    }
slouken@3685
  1323
slouken@3685
  1324
    SDL_free(window);
slouken@1895
  1325
}
slouken@1895
  1326
slouken@3025
  1327
SDL_bool
slouken@3025
  1328
SDL_IsScreenSaverEnabled()
slouken@3025
  1329
{
slouken@3025
  1330
    if (!_this) {
slouken@3025
  1331
        return SDL_TRUE;
slouken@3025
  1332
    }
slouken@3025
  1333
    return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
slouken@3025
  1334
}
slouken@3025
  1335
slouken@3025
  1336
void
slouken@3025
  1337
SDL_EnableScreenSaver()
slouken@3025
  1338
{
slouken@3025
  1339
    if (!_this) {
slouken@3025
  1340
        return;
slouken@3025
  1341
    }
slouken@3025
  1342
    if (!_this->suspend_screensaver) {
slouken@3025
  1343
        return;
slouken@3025
  1344
    }
slouken@3025
  1345
    _this->suspend_screensaver = SDL_FALSE;
slouken@3025
  1346
    if (_this->SuspendScreenSaver) {
slouken@3025
  1347
        _this->SuspendScreenSaver(_this);
slouken@3025
  1348
    }
slouken@3025
  1349
}
slouken@3025
  1350
slouken@3025
  1351
void
slouken@3025
  1352
SDL_DisableScreenSaver()
slouken@3025
  1353
{
slouken@3025
  1354
    if (!_this) {
slouken@3025
  1355
        return;
slouken@3025
  1356
    }
slouken@3025
  1357
    if (_this->suspend_screensaver) {
slouken@3025
  1358
        return;
slouken@3025
  1359
    }
slouken@3025
  1360
    _this->suspend_screensaver = SDL_TRUE;
slouken@3025
  1361
    if (_this->SuspendScreenSaver) {
slouken@3025
  1362
        _this->SuspendScreenSaver(_this);
slouken@3025
  1363
    }
slouken@3025
  1364
}
slouken@3025
  1365
slouken@1895
  1366
void
slouken@1895
  1367
SDL_VideoQuit(void)
slouken@1895
  1368
{
slouken@2753
  1369
    int i, j;
slouken@1895
  1370
slouken@2753
  1371
    if (!_this) {
slouken@2753
  1372
        return;
slouken@2753
  1373
    }
slouken@5123
  1374
slouken@2753
  1375
    /* Halt event processing before doing anything else */
slouken@5123
  1376
    SDL_QuitQuit();
slouken@5123
  1377
    SDL_MouseQuit();
slouken@5123
  1378
    SDL_KeyboardQuit();
slouken@2753
  1379
    SDL_StopEventLoop();
slouken@5123
  1380
slouken@3029
  1381
    SDL_EnableScreenSaver();
slouken@1895
  1382
slouken@2753
  1383
    /* Clean up the system video */
slouken@2753
  1384
    for (i = _this->num_displays; i--;) {
slouken@2753
  1385
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@3685
  1386
        while (display->windows) {
slouken@3685
  1387
            SDL_DestroyWindow(display->windows);
slouken@2753
  1388
        }
slouken@2753
  1389
    }
slouken@2753
  1390
    _this->VideoQuit(_this);
slouken@1895
  1391
slouken@2753
  1392
    for (i = _this->num_displays; i--;) {
slouken@2753
  1393
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  1394
        for (j = display->num_display_modes; j--;) {
slouken@2753
  1395
            if (display->display_modes[j].driverdata) {
slouken@2753
  1396
                SDL_free(display->display_modes[j].driverdata);
slouken@2753
  1397
                display->display_modes[j].driverdata = NULL;
slouken@2753
  1398
            }
slouken@2753
  1399
        }
slouken@2753
  1400
        if (display->display_modes) {
slouken@2753
  1401
            SDL_free(display->display_modes);
slouken@2753
  1402
            display->display_modes = NULL;
slouken@2753
  1403
        }
slouken@2753
  1404
        if (display->desktop_mode.driverdata) {
slouken@2753
  1405
            SDL_free(display->desktop_mode.driverdata);
slouken@2753
  1406
            display->desktop_mode.driverdata = NULL;
slouken@2753
  1407
        }
slouken@2753
  1408
        if (display->gamma) {
slouken@2753
  1409
            SDL_free(display->gamma);
slouken@2753
  1410
            display->gamma = NULL;
slouken@2753
  1411
        }
slouken@2753
  1412
        if (display->driverdata) {
slouken@2753
  1413
            SDL_free(display->driverdata);
slouken@2753
  1414
            display->driverdata = NULL;
slouken@2753
  1415
        }
slouken@2753
  1416
    }
slouken@2753
  1417
    if (_this->displays) {
slouken@2753
  1418
        SDL_free(_this->displays);
slouken@2753
  1419
        _this->displays = NULL;
slouken@2753
  1420
    }
slouken@4495
  1421
    if (_this->clipboard_text) {
slouken@4495
  1422
        SDL_free(_this->clipboard_text);
slouken@4495
  1423
        _this->clipboard_text = NULL;
slouken@4495
  1424
    }
slouken@2753
  1425
    _this->free(_this);
slouken@2753
  1426
    _this = NULL;
slouken@0
  1427
}
slouken@0
  1428
slouken@1895
  1429
int
slouken@1895
  1430
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1431
{
slouken@2753
  1432
    int retval;
slouken@0
  1433
slouken@2753
  1434
    if (!_this) {
slouken@2753
  1435
        SDL_UninitializedVideo();
slouken@2753
  1436
        return -1;
slouken@2753
  1437
    }
slouken@3057
  1438
    if (_this->gl_config.driver_loaded) {
slouken@3057
  1439
        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
slouken@3057
  1440
            SDL_SetError("OpenGL library already loaded");
slouken@3057
  1441
            return -1;
slouken@3057
  1442
        }
slouken@3057
  1443
        retval = 0;
slouken@3057
  1444
    } else {
slouken@3057
  1445
        if (!_this->GL_LoadLibrary) {
slouken@3057
  1446
            SDL_SetError("No dynamic GL support in video driver");
slouken@3057
  1447
            return -1;
slouken@3057
  1448
        }
slouken@2753
  1449
        retval = _this->GL_LoadLibrary(_this, path);
slouken@3057
  1450
    }
slouken@3057
  1451
    if (retval == 0) {
slouken@3057
  1452
        ++_this->gl_config.driver_loaded;
slouken@2753
  1453
    }
slouken@2753
  1454
    return (retval);
slouken@0
  1455
}
slouken@0
  1456
slouken@2753
  1457
void *
slouken@1895
  1458
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1459
{
slouken@2753
  1460
    void *func;
slouken@0
  1461
slouken@2753
  1462
    if (!_this) {
slouken@2753
  1463
        SDL_UninitializedVideo();
slouken@2753
  1464
        return NULL;
slouken@2753
  1465
    }
slouken@2753
  1466
    func = NULL;
slouken@2753
  1467
    if (_this->GL_GetProcAddress) {
slouken@2753
  1468
        if (_this->gl_config.driver_loaded) {
slouken@2753
  1469
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  1470
        } else {
slouken@2753
  1471
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  1472
        }
slouken@2753
  1473
    } else {
slouken@2753
  1474
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  1475
    }
slouken@2753
  1476
    return func;
slouken@0
  1477
}
slouken@0
  1478
slouken@3057
  1479
void
slouken@3057
  1480
SDL_GL_UnloadLibrary(void)
slouken@3057
  1481
{
slouken@3057
  1482
    if (!_this) {
slouken@3057
  1483
        SDL_UninitializedVideo();
slouken@3057
  1484
        return;
slouken@3057
  1485
    }
slouken@3057
  1486
    if (_this->gl_config.driver_loaded > 0) {
slouken@3057
  1487
        if (--_this->gl_config.driver_loaded > 0) {
slouken@3057
  1488
            return;
slouken@3057
  1489
        }
slouken@3057
  1490
        if (_this->GL_UnloadLibrary) {
slouken@3057
  1491
            _this->GL_UnloadLibrary(_this);
slouken@3057
  1492
        }
slouken@3057
  1493
    }
slouken@3057
  1494
}
slouken@3057
  1495
slouken@1926
  1496
SDL_bool
slouken@1926
  1497
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  1498
{
hfutrell@2745
  1499
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
slouken@2753
  1500
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  1501
    const char *extensions;
slouken@2753
  1502
    const char *start;
slouken@2753
  1503
    const char *where, *terminator;
slouken@1926
  1504
slouken@2753
  1505
    /* Extension names should not have spaces. */
slouken@2753
  1506
    where = SDL_strchr(extension, ' ');
slouken@2753
  1507
    if (where || *extension == '\0') {
slouken@2753
  1508
        return SDL_FALSE;
slouken@2753
  1509
    }
slouken@2753
  1510
    /* See if there's an environment variable override */
slouken@2753
  1511
    start = SDL_getenv(extension);
slouken@2753
  1512
    if (start && *start == '0') {
slouken@2753
  1513
        return SDL_FALSE;
slouken@2753
  1514
    }
slouken@2753
  1515
    /* Lookup the available extensions */
slouken@2753
  1516
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@2753
  1517
    if (glGetStringFunc) {
slouken@2753
  1518
        extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  1519
    } else {
slouken@2753
  1520
        extensions = NULL;
slouken@2753
  1521
    }
slouken@2753
  1522
    if (!extensions) {
slouken@2753
  1523
        return SDL_FALSE;
slouken@2753
  1524
    }
slouken@2753
  1525
    /*
slouken@2753
  1526
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  1527
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  1528
     */
slouken@1926
  1529
slouken@2753
  1530
    start = extensions;
slouken@1926
  1531
slouken@2753
  1532
    for (;;) {
slouken@2753
  1533
        where = SDL_strstr(start, extension);
slouken@2753
  1534
        if (!where)
slouken@2753
  1535
            break;
slouken@1926
  1536
slouken@2753
  1537
        terminator = where + SDL_strlen(extension);
slouken@2753
  1538
        if (where == start || *(where - 1) == ' ')
slouken@2753
  1539
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  1540
                return SDL_TRUE;
slouken@1926
  1541
slouken@2753
  1542
        start = terminator;
slouken@2753
  1543
    }
slouken@2753
  1544
    return SDL_FALSE;
slouken@1926
  1545
#else
slouken@2753
  1546
    return SDL_FALSE;
slouken@1926
  1547
#endif
slouken@1926
  1548
}
slouken@1926
  1549
slouken@1895
  1550
int
slouken@1895
  1551
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  1552
{
hfutrell@2745
  1553
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
slouken@2753
  1554
    int retval;
slouken@0
  1555
slouken@2753
  1556
    if (!_this) {
slouken@2753
  1557
        SDL_UninitializedVideo();
slouken@2753
  1558
        return -1;
slouken@2753
  1559
    }
slouken@2753
  1560
    retval = 0;
slouken@2753
  1561
    switch (attr) {
slouken@2753
  1562
    case SDL_GL_RED_SIZE:
slouken@2753
  1563
        _this->gl_config.red_size = value;
slouken@2753
  1564
        break;
slouken@2753
  1565
    case SDL_GL_GREEN_SIZE:
slouken@2753
  1566
        _this->gl_config.green_size = value;
slouken@2753
  1567
        break;
slouken@2753
  1568
    case SDL_GL_BLUE_SIZE:
slouken@2753
  1569
        _this->gl_config.blue_size = value;
slouken@2753
  1570
        break;
slouken@2753
  1571
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  1572
        _this->gl_config.alpha_size = value;
slouken@2753
  1573
        break;
slouken@2753
  1574
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  1575
        _this->gl_config.double_buffer = value;
slouken@2753
  1576
        break;
slouken@2753
  1577
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  1578
        _this->gl_config.buffer_size = value;
slouken@2753
  1579
        break;
slouken@2753
  1580
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  1581
        _this->gl_config.depth_size = value;
slouken@2753
  1582
        break;
slouken@2753
  1583
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  1584
        _this->gl_config.stencil_size = value;
slouken@2753
  1585
        break;
slouken@2753
  1586
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  1587
        _this->gl_config.accum_red_size = value;
slouken@2753
  1588
        break;
slouken@2753
  1589
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  1590
        _this->gl_config.accum_green_size = value;
slouken@2753
  1591
        break;
slouken@2753
  1592
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  1593
        _this->gl_config.accum_blue_size = value;
slouken@2753
  1594
        break;
slouken@2753
  1595
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  1596
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  1597
        break;
slouken@2753
  1598
    case SDL_GL_STEREO:
slouken@2753
  1599
        _this->gl_config.stereo = value;
slouken@2753
  1600
        break;
slouken@2753
  1601
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  1602
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  1603
        break;
slouken@2753
  1604
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  1605
        _this->gl_config.multisamplesamples = value;
slouken@2753
  1606
        break;
slouken@2753
  1607
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  1608
        _this->gl_config.accelerated = value;
slouken@2753
  1609
        break;
slouken@2753
  1610
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  1611
        _this->gl_config.retained_backing = value;
slouken@2753
  1612
        break;
slouken@3100
  1613
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@3100
  1614
        _this->gl_config.major_version = value;
slouken@3100
  1615
        break;
slouken@3100
  1616
    case SDL_GL_CONTEXT_MINOR_VERSION:
slouken@3100
  1617
        _this->gl_config.minor_version = value;
slouken@3100
  1618
        break;
slouken@2753
  1619
    default:
slouken@2753
  1620
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  1621
        retval = -1;
slouken@2753
  1622
        break;
slouken@2753
  1623
    }
slouken@2753
  1624
    return retval;
slouken@1936
  1625
#else
slouken@2753
  1626
    SDL_Unsupported();
slouken@2753
  1627
    return -1;
slouken@2753
  1628
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  1629
}
slouken@0
  1630
slouken@1895
  1631
int
slouken@1936
  1632
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  1633
{
hfutrell@2745
  1634
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES
slouken@2753
  1635
    void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
slouken@3139
  1636
    GLenum(APIENTRY * glGetErrorFunc) (void);
slouken@2753
  1637
    GLenum attrib = 0;
slouken@3099
  1638
    GLenum error = 0;
slouken@1912
  1639
slouken@2753
  1640
    glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
slouken@2753
  1641
    if (!glGetIntegervFunc) {
slouken@2753
  1642
        return -1;
slouken@2753
  1643
    }
slouken@3099
  1644
slouken@3099
  1645
    glGetErrorFunc = SDL_GL_GetProcAddress("glGetError");
slouken@3099
  1646
    if (!glGetErrorFunc) {
slouken@3099
  1647
        return -1;
slouken@3099
  1648
    }
slouken@3099
  1649
slouken@3099
  1650
    /* Clear value in any case */
slouken@3139
  1651
    *value = 0;
slouken@3099
  1652
slouken@2753
  1653
    switch (attr) {
slouken@2753
  1654
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  1655
        *value = _this->gl_config.retained_backing;
slouken@2753
  1656
        return 0;
slouken@2753
  1657
    case SDL_GL_RED_SIZE:
slouken@2753
  1658
        attrib = GL_RED_BITS;
slouken@2753
  1659
        break;
slouken@2753
  1660
    case SDL_GL_BLUE_SIZE:
slouken@2753
  1661
        attrib = GL_BLUE_BITS;
slouken@2753
  1662
        break;
slouken@2753
  1663
    case SDL_GL_GREEN_SIZE:
slouken@2753
  1664
        attrib = GL_GREEN_BITS;
slouken@2753
  1665
        break;
slouken@2753
  1666
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  1667
        attrib = GL_ALPHA_BITS;
slouken@2753
  1668
        break;
slouken@2753
  1669
    case SDL_GL_DOUBLEBUFFER:
hfutrell@2745
  1670
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  1671
        attrib = GL_DOUBLEBUFFER;
slouken@2753
  1672
        break;
hfutrell@2745
  1673
#else
slouken@3099
  1674
        /* OpenGL ES 1.0 and above specifications have EGL_SINGLE_BUFFER      */
slouken@3099
  1675
        /* parameter which switches double buffer to single buffer. OpenGL ES */
slouken@3099
  1676
        /* SDL driver must set proper value after initialization              */
slouken@3099
  1677
        *value = _this->gl_config.double_buffer;
slouken@2753
  1678
        return 0;
hfutrell@2745
  1679
#endif
slouken@2753
  1680
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  1681
        attrib = GL_DEPTH_BITS;
slouken@2753
  1682
        break;
slouken@2753
  1683
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  1684
        attrib = GL_STENCIL_BITS;
slouken@2753
  1685
        break;
hfutrell@2745
  1686
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  1687
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  1688
        attrib = GL_ACCUM_RED_BITS;
slouken@2753
  1689
        break;
slouken@2753
  1690
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  1691
        attrib = GL_ACCUM_GREEN_BITS;
slouken@2753
  1692
        break;
slouken@2753
  1693
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  1694
        attrib = GL_ACCUM_BLUE_BITS;
slouken@2753
  1695
        break;
slouken@2753
  1696
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  1697
        attrib = GL_ACCUM_ALPHA_BITS;
slouken@2753
  1698
        break;
slouken@2753
  1699
    case SDL_GL_STEREO:
slouken@2753
  1700
        attrib = GL_STEREO;
slouken@2753
  1701
        break;
hfutrell@2745
  1702
#else
slouken@2753
  1703
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  1704
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  1705
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  1706
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  1707
    case SDL_GL_STEREO:
slouken@2753
  1708
        /* none of these are supported in OpenGL ES */
slouken@2753
  1709
        *value = 0;
slouken@2753
  1710
        return 0;
hfutrell@2745
  1711
#endif
slouken@2753
  1712
    case SDL_GL_MULTISAMPLEBUFFERS:
hfutrell@2745
  1713
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  1714
        attrib = GL_SAMPLE_BUFFERS_ARB;
hfutrell@2745
  1715
#else
slouken@2753
  1716
        attrib = GL_SAMPLE_BUFFERS;
hfutrell@2745
  1717
#endif
slouken@2753
  1718
        break;
slouken@2753
  1719
    case SDL_GL_MULTISAMPLESAMPLES:
hfutrell@2745
  1720
#ifndef SDL_VIDEO_OPENGL_ES
slouken@2753
  1721
        attrib = GL_SAMPLES_ARB;
hfutrell@2745
  1722
#else
slouken@2753
  1723
        attrib = GL_SAMPLES;
hfutrell@2745
  1724
#endif
slouken@2753
  1725
        break;
slouken@2753
  1726
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  1727
        {
slouken@2753
  1728
            GLint bits = 0;
slouken@2753
  1729
            GLint component;
slouken@1936
  1730
slouken@2753
  1731
            /*
slouken@2753
  1732
             * there doesn't seem to be a single flag in OpenGL
slouken@2753
  1733
             * for this!
slouken@2753
  1734
             */
slouken@2753
  1735
            glGetIntegervFunc(GL_RED_BITS, &component);
slouken@2753
  1736
            bits += component;
slouken@2753
  1737
            glGetIntegervFunc(GL_GREEN_BITS, &component);
slouken@2753
  1738
            bits += component;
slouken@2753
  1739
            glGetIntegervFunc(GL_BLUE_BITS, &component);
slouken@2753
  1740
            bits += component;
slouken@2753
  1741
            glGetIntegervFunc(GL_ALPHA_BITS, &component);
slouken@2753
  1742
            bits += component;
slouken@1936
  1743
slouken@2753
  1744
            *value = bits;
slouken@2753
  1745
            return 0;
slouken@2753
  1746
        }
slouken@2753
  1747
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  1748
        {
slouken@2753
  1749
            /* FIXME: How do we get this information? */
slouken@3571
  1750
            *value = (_this->gl_config.accelerated != 0);
slouken@2753
  1751
            return 0;
slouken@2753
  1752
        }
slouken@2753
  1753
    default:
slouken@2753
  1754
        SDL_SetError("Unknown OpenGL attribute");
slouken@2753
  1755
        return -1;
slouken@2753
  1756
    }
slouken@0
  1757
slouken@2753
  1758
    glGetIntegervFunc(attrib, (GLint *) value);
slouken@3139
  1759
    error = glGetErrorFunc();
slouken@3139
  1760
    if (error != GL_NO_ERROR) {
slouken@3139
  1761
        switch (error) {
slouken@3139
  1762
        case GL_INVALID_ENUM:
slouken@3139
  1763
            {
slouken@3139
  1764
                SDL_SetError("OpenGL error: GL_INVALID_ENUM");
slouken@3139
  1765
            }
slouken@3139
  1766
            break;
slouken@3139
  1767
        case GL_INVALID_VALUE:
slouken@3139
  1768
            {
slouken@3139
  1769
                SDL_SetError("OpenGL error: GL_INVALID_VALUE");
slouken@3139
  1770
            }
slouken@3139
  1771
            break;
slouken@3139
  1772
        default:
slouken@3139
  1773
            {
slouken@3139
  1774
                SDL_SetError("OpenGL error: %08X", error);
slouken@3139
  1775
            }
slouken@3139
  1776
            break;
slouken@3139
  1777
        }
slouken@3139
  1778
        return -1;
slouken@3099
  1779
    }
slouken@2753
  1780
    return 0;
slouken@1936
  1781
#else
slouken@2753
  1782
    SDL_Unsupported();
slouken@2753
  1783
    return -1;
slouken@2753
  1784
#endif /* SDL_VIDEO_OPENGL */
slouken@0
  1785
}
slouken@0
  1786
slouken@1912
  1787
SDL_GLContext
slouken@3685
  1788
SDL_GL_CreateContext(SDL_Window * window)
slouken@1912
  1789
{
slouken@3695
  1790
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  1791
slouken@2753
  1792
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  1793
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  1794
        return NULL;
slouken@2753
  1795
    }
slouken@2753
  1796
    return _this->GL_CreateContext(_this, window);
slouken@1912
  1797
}
slouken@1912
  1798
slouken@1912
  1799
int
slouken@3685
  1800
SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
slouken@1912
  1801
{
slouken@3695
  1802
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1803
slouken@3695
  1804
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  1805
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  1806
        return -1;
slouken@2753
  1807
    }
slouken@2753
  1808
    if (!context) {
slouken@2753
  1809
        window = NULL;
slouken@2753
  1810
    }
slouken@2753
  1811
    return _this->GL_MakeCurrent(_this, window, context);
slouken@1912
  1812
}
slouken@1912
  1813
slouken@1912
  1814
int
slouken@1912
  1815
SDL_GL_SetSwapInterval(int interval)
slouken@1912
  1816
{
slouken@2753
  1817
    if (!_this) {
slouken@2753
  1818
        SDL_UninitializedVideo();
slouken@2753
  1819
        return -1;
slouken@2753
  1820
    }
slouken@2753
  1821
    if (_this->GL_SetSwapInterval) {
slouken@2753
  1822
        return _this->GL_SetSwapInterval(_this, interval);
slouken@2753
  1823
    } else {
slouken@2753
  1824
        SDL_SetError("Setting the swap interval is not supported");
slouken@2753
  1825
        return -1;
slouken@2753
  1826
    }
slouken@1912
  1827
}
slouken@1912
  1828
slouken@1912
  1829
int
slouken@1912
  1830
SDL_GL_GetSwapInterval(void)
slouken@1912
  1831
{
slouken@2753
  1832
    if (!_this) {
slouken@2753
  1833
        SDL_UninitializedVideo();
slouken@2753
  1834
        return -1;
slouken@2753
  1835
    }
slouken@2753
  1836
    if (_this->GL_GetSwapInterval) {
slouken@2753
  1837
        return _this->GL_GetSwapInterval(_this);
slouken@2753
  1838
    } else {
slouken@2753
  1839
        SDL_SetError("Getting the swap interval is not supported");
slouken@2753
  1840
        return -1;
slouken@2753
  1841
    }
slouken@1912
  1842
}
slouken@1912
  1843
slouken@1895
  1844
void
slouken@3685
  1845
SDL_GL_SwapWindow(SDL_Window * window)
slouken@0
  1846
{
slouken@3695
  1847
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1848
slouken@2753
  1849
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@2753
  1850
        SDL_SetError("The specified window isn't an OpenGL window");
slouken@2753
  1851
        return;
slouken@2753
  1852
    }
slouken@2753
  1853
    _this->GL_SwapWindow(_this, window);
slouken@1912
  1854
}
slouken@1912
  1855
slouken@1912
  1856
void
slouken@1912
  1857
SDL_GL_DeleteContext(SDL_GLContext context)
slouken@1912
  1858
{
slouken@4526
  1859
    if (!_this || !_this->gl_data || !context) {
slouken@2753
  1860
        return;
slouken@2753
  1861
    }
slouken@2753
  1862
    _this->GL_MakeCurrent(_this, NULL, NULL);
slouken@2753
  1863
    _this->GL_DeleteContext(_this, context);
slouken@0
  1864
}
slouken@0
  1865
slouken@1895
  1866
#if 0                           // FIXME
hfutrell@2744
  1867
/*
hfutrell@2744
  1868
 * Utility function used by SDL_WM_SetIcon(); flags & 1 for color key, flags
hfutrell@2744
  1869
 * & 2 for alpha channel.
hfutrell@2744
  1870
 */
slouken@1895
  1871
static void
slouken@1895
  1872
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1873
{
slouken@2753
  1874
    int x, y;
slouken@2753
  1875
    Uint32 colorkey;
slouken@0
  1876
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1877
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1878
slouken@2753
  1879
    colorkey = icon->format->colorkey;
slouken@2753
  1880
    switch (icon->format->BytesPerPixel) {
slouken@2753
  1881
    case 1:
slouken@2753
  1882
        {
slouken@2753
  1883
            Uint8 *pixels;
slouken@2753
  1884
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  1885
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@2753
  1886
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  1887
                    if (*pixels++ == colorkey) {
slouken@2753
  1888
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  1889
                    }
slouken@2753
  1890
                }
slouken@2753
  1891
            }
slouken@2753
  1892
        }
slouken@2753
  1893
        break;
slouken@0
  1894
slouken@2753
  1895
    case 2:
slouken@2753
  1896
        {
slouken@2753
  1897
            Uint16 *pixels;
slouken@2753
  1898
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  1899
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@2753
  1900
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  1901
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  1902
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  1903
                    } else if ((flags & 2)
slouken@2753
  1904
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  1905
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  1906
                    }
slouken@2753
  1907
                    pixels++;
slouken@2753
  1908
                }
slouken@2753
  1909
            }
slouken@2753
  1910
        }
slouken@2753
  1911
        break;
slouken@0
  1912
slouken@2753
  1913
    case 4:
slouken@2753
  1914
        {
slouken@2753
  1915
            Uint32 *pixels;
slouken@2753
  1916
            for (y = 0; y < icon->h; ++y) {
slouken@2753
  1917
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@2753
  1918
                for (x = 0; x < icon->w; ++x) {
slouken@2753
  1919
                    if ((flags & 1) && *pixels == colorkey) {
slouken@2753
  1920
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  1921
                    } else if ((flags & 2)
slouken@2753
  1922
                               && (*pixels & icon->format->Amask) == 0) {
slouken@2753
  1923
                        SET_MASKBIT(icon, x, y, mask);
slouken@2753
  1924
                    }
slouken@2753
  1925
                    pixels++;
slouken@2753
  1926
                }
slouken@2753
  1927
            }
slouken@2753
  1928
        }
slouken@2753
  1929
        break;
slouken@2753
  1930
    }
slouken@0
  1931
}
slouken@0
  1932
slouken@0
  1933
/*
slouken@0
  1934
 * Sets the window manager icon for the display window.
slouken@0
  1935
 */
slouken@1895
  1936
void
slouken@1895
  1937
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  1938
{
slouken@2753
  1939
    if (icon && _this->SetIcon) {
slouken@2753
  1940
        /* Generate a mask if necessary, and create the icon! */
slouken@2753
  1941
        if (mask == NULL) {
slouken@2753
  1942
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@2753
  1943
            int flags = 0;
slouken@2753
  1944
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@2753
  1945
            if (mask == NULL) {
slouken@2753
  1946
                return;
slouken@2753
  1947
            }
slouken@2753
  1948
            SDL_memset(mask, ~0, mask_len);
slouken@2753
  1949
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@2753
  1950
                flags |= 1;
slouken@2753
  1951
            if (icon->flags & SDL_SRCALPHA)
slouken@2753
  1952
                flags |= 2;
slouken@2753
  1953
            if (flags) {
slouken@2753
  1954
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@2753
  1955
            }
slouken@2753
  1956
            _this->SetIcon(_this, icon, mask);
slouken@2753
  1957
            SDL_free(mask);
slouken@2753
  1958
        } else {
slouken@2753
  1959
            _this->SetIcon(_this, icon, mask);
slouken@2753
  1960
        }
slouken@2753
  1961
    }
slouken@1895
  1962
}
slouken@1895
  1963
#endif
slouken@0
  1964
slouken@1895
  1965
SDL_bool
slouken@3685
  1966
SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
slouken@1895
  1967
{
slouken@3695
  1968
    CHECK_WINDOW_MAGIC(window, SDL_FALSE);
slouken@3695
  1969
slouken@4900
  1970
    if (!info) {
slouken@4900
  1971
        return SDL_FALSE;
slouken@4900
  1972
    }
slouken@4900
  1973
    info->subsystem = SDL_SYSWM_UNKNOWN;
slouken@4900
  1974
slouken@3695
  1975
    if (!_this->GetWindowWMInfo) {
slouken@2753
  1976
        return SDL_FALSE;
slouken@2753
  1977
    }
slouken@2753
  1978
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  1979
}
slouken@0
  1980
slouken@3280
  1981
void
slouken@3280
  1982
SDL_StartTextInput(void)
slouken@3280
  1983
{
slouken@3683
  1984
    if (_this && _this->StartTextInput) {
slouken@3280
  1985
        _this->StartTextInput(_this);
slouken@3280
  1986
    }
slouken@3280
  1987
    SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
slouken@3280
  1988
    SDL_EventState(SDL_TEXTEDITING, SDL_ENABLE);
slouken@3280
  1989
}
slouken@3280
  1990
slouken@3280
  1991
void
slouken@3280
  1992
SDL_StopTextInput(void)
slouken@3280
  1993
{
slouken@3683
  1994
    if (_this && _this->StopTextInput) {
slouken@3280
  1995
        _this->StopTextInput(_this);
slouken@3280
  1996
    }
slouken@3280
  1997
    SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
slouken@3280
  1998
    SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
slouken@3280
  1999
}
slouken@3280
  2000
slouken@3280
  2001
void
slouken@3280
  2002
SDL_SetTextInputRect(SDL_Rect *rect)
slouken@3280
  2003
{
slouken@3683
  2004
    if (_this && _this->SetTextInputRect) {
slouken@3280
  2005
        _this->SetTextInputRect(_this, rect);
slouken@3280
  2006
    }
slouken@3280
  2007
}
slouken@3280
  2008
slouken@1895
  2009
/* vi: set ts=4 sw=4 expandtab: */