src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1660 8b9d79e7eacf
child 1666 6e7ec5cb83c3
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@0
    26
#include "SDL.h"
slouken@0
    27
#include "SDL_sysvideo.h"
slouken@0
    28
#include "SDL_blit.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@0
    30
#include "SDL_cursor_c.h"
slouken@1361
    31
#include "../events/SDL_sysevents.h"
slouken@1361
    32
#include "../events/SDL_events_c.h"
slouken@0
    33
slouken@0
    34
/* Available video drivers */
slouken@0
    35
static VideoBootStrap *bootstrap[] = {
slouken@1361
    36
#if SDL_VIDEO_DRIVER_QUARTZ
slouken@1662
    37
    &QZ_bootstrap,
icculus@1133
    38
#endif
slouken@1361
    39
#if SDL_VIDEO_DRIVER_X11
slouken@1662
    40
    &X11_bootstrap,
slouken@0
    41
#endif
slouken@1361
    42
#if SDL_VIDEO_DRIVER_DGA
slouken@1662
    43
    &DGA_bootstrap,
slouken@0
    44
#endif
slouken@1361
    45
#if SDL_VIDEO_DRIVER_NANOX
slouken@1662
    46
    &NX_bootstrap,
slouken@30
    47
#endif
slouken@1361
    48
#if SDL_VIDEO_DRIVER_IPOD
slouken@1662
    49
    &iPod_bootstrap,
icculus@1140
    50
#endif
slouken@1361
    51
#if SDL_VIDEO_DRIVER_QTOPIA
slouken@1662
    52
    &Qtopia_bootstrap,
slouken@567
    53
#endif
slouken@1361
    54
#if SDL_VIDEO_DRIVER_WSCONS
slouken@1662
    55
    &WSCONS_bootstrap,
icculus@1187
    56
#endif
slouken@1361
    57
#if SDL_VIDEO_DRIVER_FBCON
slouken@1662
    58
    &FBCON_bootstrap,
slouken@0
    59
#endif
slouken@1361
    60
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@1662
    61
    &DirectFB_bootstrap,
slouken@167
    62
#endif
slouken@1361
    63
#if SDL_VIDEO_DRIVER_PS2GS
slouken@1662
    64
    &PS2GS_bootstrap,
slouken@0
    65
#endif
slouken@1361
    66
#if SDL_VIDEO_DRIVER_GGI
slouken@1662
    67
    &GGI_bootstrap,
slouken@0
    68
#endif
slouken@1361
    69
#if SDL_VIDEO_DRIVER_VGL
slouken@1662
    70
    &VGL_bootstrap,
slouken@75
    71
#endif
slouken@1361
    72
#if SDL_VIDEO_DRIVER_SVGALIB
slouken@1662
    73
    &SVGALIB_bootstrap,
slouken@0
    74
#endif
slouken@1361
    75
#if SDL_VIDEO_DRIVER_GAPI
slouken@1662
    76
    &GAPI_bootstrap,
slouken@1361
    77
#endif
slouken@1361
    78
#if SDL_VIDEO_DRIVER_WINDIB
slouken@1662
    79
    &WINDIB_bootstrap,
slouken@1361
    80
#endif
slouken@1361
    81
#if SDL_VIDEO_DRIVER_DDRAW
slouken@1662
    82
    &DIRECTX_bootstrap,
slouken@1361
    83
#endif
slouken@1361
    84
#if SDL_VIDEO_DRIVER_BWINDOW
slouken@1662
    85
    &BWINDOW_bootstrap,
slouken@1361
    86
#endif
slouken@1361
    87
#if SDL_VIDEO_DRIVER_TOOLBOX
slouken@1662
    88
    &TOOLBOX_bootstrap,
slouken@1361
    89
#endif
slouken@1361
    90
#if SDL_VIDEO_DRIVER_DRAWSPROCKET
slouken@1662
    91
    &DSp_bootstrap,
slouken@1361
    92
#endif
slouken@1361
    93
#if SDL_VIDEO_DRIVER_CYBERGRAPHICS
slouken@1662
    94
    &CGX_bootstrap,
slouken@1361
    95
#endif
slouken@1361
    96
#if SDL_VIDEO_DRIVER_PHOTON
slouken@1662
    97
    &ph_bootstrap,
slouken@1361
    98
#endif
slouken@1361
    99
#if SDL_VIDEO_DRIVER_EPOC
slouken@1662
   100
    &EPOC_bootstrap,
slouken@1361
   101
#endif
slouken@1361
   102
#if SDL_VIDEO_DRIVER_XBIOS
slouken@1662
   103
    &XBIOS_bootstrap,
slouken@1361
   104
#endif
slouken@1361
   105
#if SDL_VIDEO_DRIVER_GEM
slouken@1662
   106
    &GEM_bootstrap,
slouken@1361
   107
#endif
slouken@1361
   108
#if SDL_VIDEO_DRIVER_PICOGUI
slouken@1662
   109
    &PG_bootstrap,
slouken@1361
   110
#endif
slouken@1361
   111
#if SDL_VIDEO_DRIVER_DC
slouken@1662
   112
    &DC_bootstrap,
slouken@1361
   113
#endif
slouken@1361
   114
#if SDL_VIDEO_DRIVER_RISCOS
slouken@1662
   115
    &RISCOS_bootstrap,
slouken@1361
   116
#endif
slouken@1361
   117
#if SDL_VIDEO_DRIVER_OS2FS
slouken@1662
   118
    &OS2FSLib_bootstrap,
slouken@1361
   119
#endif
slouken@1361
   120
#if SDL_VIDEO_DRIVER_AALIB
slouken@1662
   121
    &AALIB_bootstrap,
slouken@0
   122
#endif
slouken@1361
   123
#if SDL_VIDEO_DRIVER_DUMMY
slouken@1662
   124
    &DUMMY_bootstrap,
slouken@610
   125
#endif
slouken@1658
   126
#if SDL_VIDEO_DRIVER_GLSDL
slouken@1662
   127
    &glSDL_bootstrap,
slouken@1658
   128
#endif
slouken@1662
   129
    NULL
slouken@0
   130
};
slouken@173
   131
slouken@1662
   132
static SDL_VideoDevice *_this = NULL;
slouken@0
   133
slouken@0
   134
/* Various local functions */
slouken@1662
   135
int SDL_VideoInit (const char *driver_name, Uint32 flags);
slouken@1662
   136
void SDL_VideoQuit (void);
slouken@0
   137
slouken@1662
   138
static int
slouken@1662
   139
cmpmodes (const void *A, const void *B)
slouken@1662
   140
{
slouken@1662
   141
    SDL_DisplayMode a = *(const SDL_DisplayMode *) A;
slouken@1662
   142
    SDL_DisplayMode b = *(const SDL_DisplayMode *) B;
slouken@0
   143
slouken@1662
   144
    if (a.w != b.w) {
slouken@1662
   145
        return b.w - a.w;
slouken@1662
   146
    }
slouken@1662
   147
    if (a.h != b.h) {
slouken@1662
   148
        return b.h - a.h;
slouken@1662
   149
    }
slouken@1662
   150
    if (SDL_BITSPERPIXEL (a.format) != SDL_BITSPERPIXEL (b.format)) {
slouken@1662
   151
        return SDL_BITSPERPIXEL (b.format) - SDL_BITSPERPIXEL (a.format);
slouken@1662
   152
    }
slouken@1662
   153
    if (a.refresh_rate != b.refresh_rate) {
slouken@1662
   154
        return b.refresh_rate - a.refresh_rate;
slouken@1662
   155
    }
slouken@1662
   156
    return 0;
slouken@1660
   157
}
slouken@1660
   158
slouken@1662
   159
int
slouken@1662
   160
SDL_GetNumVideoDrivers (void)
slouken@1660
   161
{
slouken@1662
   162
    return SDL_arraysize (bootstrap) - 1;
slouken@1662
   163
}
slouken@1662
   164
slouken@1662
   165
const char *
slouken@1662
   166
SDL_GetVideoDriver (int index)
slouken@1662
   167
{
slouken@1662
   168
    if (index >= 0 && index < SDL_GetNumVideoDrivers ()) {
slouken@1662
   169
        return bootstrap[index]->name;
slouken@1662
   170
    }
slouken@1662
   171
    return NULL;
slouken@1660
   172
}
slouken@0
   173
slouken@0
   174
/*
slouken@0
   175
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   176
 */
slouken@1662
   177
int
slouken@1662
   178
SDL_VideoInit (const char *driver_name, Uint32 flags)
slouken@0
   179
{
slouken@1662
   180
    SDL_VideoDevice *video;
slouken@1662
   181
    int index;
slouken@1662
   182
    int i;
slouken@1662
   183
    int bpp;
slouken@1662
   184
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
   185
slouken@1662
   186
    /* Toggle the event thread flags, based on OS requirements */
slouken@0
   187
#if defined(MUST_THREAD_EVENTS)
slouken@1662
   188
    flags |= SDL_INIT_EVENTTHREAD;
slouken@0
   189
#elif defined(CANT_THREAD_EVENTS)
slouken@1662
   190
    if ((flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD) {
slouken@1662
   191
        SDL_SetError ("OS doesn't support threaded events");
slouken@1662
   192
        return -1;
slouken@1662
   193
    }
slouken@0
   194
#endif
slouken@0
   195
slouken@1662
   196
    /* Check to make sure we don't overwrite '_this' */
slouken@1662
   197
    if (_this != NULL) {
slouken@1662
   198
        SDL_VideoQuit ();
slouken@1662
   199
    }
slouken@0
   200
slouken@1662
   201
    /* Select the proper video driver */
slouken@1662
   202
    index = 0;
slouken@1662
   203
    video = NULL;
slouken@1662
   204
    if (driver_name != NULL) {
slouken@1662
   205
        for (i = 0; bootstrap[i]; ++i) {
slouken@1662
   206
            if (SDL_strncmp (bootstrap[i]->name, driver_name,
slouken@1662
   207
                             SDL_strlen (bootstrap[i]->name)) == 0) {
slouken@1662
   208
                if (bootstrap[i]->available ()) {
slouken@1662
   209
                    video = bootstrap[i]->create (index);
slouken@1662
   210
                }
slouken@1662
   211
                break;
slouken@1662
   212
            }
slouken@1662
   213
        }
slouken@1662
   214
    } else {
slouken@1662
   215
        for (i = 0; bootstrap[i]; ++i) {
slouken@1662
   216
            if (bootstrap[i]->available ()) {
slouken@1662
   217
                video = bootstrap[i]->create (index);
slouken@1662
   218
                if (video != NULL) {
slouken@1662
   219
                    break;
slouken@1662
   220
                }
slouken@1662
   221
            }
slouken@1662
   222
        }
slouken@1662
   223
    }
slouken@1662
   224
    if (video == NULL) {
slouken@1662
   225
        if (driver_name) {
slouken@1662
   226
            SDL_SetError ("%s not available", driver_name);
slouken@1662
   227
        } else {
slouken@1662
   228
            SDL_SetError ("No available video device");
slouken@1662
   229
        }
slouken@1662
   230
        return -1;
slouken@1662
   231
    }
slouken@1662
   232
    _this = video;
slouken@1662
   233
    _this->name = bootstrap[i]->name;
slouken@1662
   234
    _this->next_window_id = 1;
slouken@0
   235
slouken@1662
   236
    /* Set some very sane GL defaults */
slouken@1662
   237
    _this->gl_config.driver_loaded = 0;
slouken@1662
   238
    _this->gl_config.dll_handle = NULL;
slouken@1662
   239
    _this->gl_config.red_size = 3;
slouken@1662
   240
    _this->gl_config.green_size = 3;
slouken@1662
   241
    _this->gl_config.blue_size = 2;
slouken@1662
   242
    _this->gl_config.alpha_size = 0;
slouken@1662
   243
    _this->gl_config.buffer_size = 0;
slouken@1662
   244
    _this->gl_config.depth_size = 16;
slouken@1662
   245
    _this->gl_config.stencil_size = 0;
slouken@1662
   246
    _this->gl_config.double_buffer = 1;
slouken@1662
   247
    _this->gl_config.accum_red_size = 0;
slouken@1662
   248
    _this->gl_config.accum_green_size = 0;
slouken@1662
   249
    _this->gl_config.accum_blue_size = 0;
slouken@1662
   250
    _this->gl_config.accum_alpha_size = 0;
slouken@1662
   251
    _this->gl_config.stereo = 0;
slouken@1662
   252
    _this->gl_config.multisamplebuffers = 0;
slouken@1662
   253
    _this->gl_config.multisamplesamples = 0;
slouken@1662
   254
    _this->gl_config.accelerated = -1;  /* not known, don't set */
slouken@1662
   255
    _this->gl_config.swap_control = -1; /* not known, don't set */
slouken@0
   256
slouken@1662
   257
    /* Initialize the video subsystem */
slouken@1662
   258
    if (_this->VideoInit (_this) < 0) {
slouken@1662
   259
        SDL_VideoQuit ();
slouken@1662
   260
        return -1;
slouken@1662
   261
    }
slouken@0
   262
slouken@1662
   263
    /* Make sure some displays were added */
slouken@1662
   264
    if (_this->num_displays == 0) {
slouken@1662
   265
        SDL_SetError ("The video driver did not add any displays");
slouken@1662
   266
        SDL_VideoQuit ();
slouken@1662
   267
        return (-1);
slouken@1662
   268
    }
slouken@0
   269
slouken@1662
   270
    /* Sort the video modes */
slouken@1662
   271
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   272
        SDL_qsort (_this->displays[i].display_modes,
slouken@1662
   273
                   _this->displays[i].num_display_modes,
slouken@1662
   274
                   sizeof (SDL_DisplayMode), cmpmodes);
slouken@1662
   275
    }
slouken@0
   276
slouken@1662
   277
    /* Start the event loop */
slouken@1662
   278
    if (SDL_StartEventLoop (flags) < 0) {
slouken@1662
   279
        SDL_VideoQuit ();
slouken@1662
   280
        return -1;
slouken@1662
   281
    }
slouken@1662
   282
    SDL_CursorInit (flags & SDL_INIT_EVENTTHREAD);
slouken@0
   283
slouken@1662
   284
    /* We're ready to go! */
slouken@1662
   285
    return 0;
slouken@0
   286
}
slouken@0
   287
slouken@1662
   288
const char *
slouken@1662
   289
SDL_GetCurrentVideoDriver ()
slouken@0
   290
{
slouken@1662
   291
    if (!_this) {
slouken@1662
   292
        return NULL;
slouken@1662
   293
    }
slouken@1662
   294
    return _this->name;
slouken@0
   295
}
slouken@0
   296
slouken@1662
   297
SDL_VideoDevice *
slouken@1662
   298
SDL_GetVideoDevice ()
slouken@0
   299
{
slouken@1662
   300
    return _this;
slouken@0
   301
}
slouken@0
   302
slouken@1662
   303
const SDL_VideoInfo *
slouken@1662
   304
SDL_GetVideoInfo (void)
slouken@0
   305
{
slouken@1662
   306
    if (!_this) {
slouken@1662
   307
        return NULL;
slouken@1662
   308
    }
slouken@1662
   309
    return &_this->info;
slouken@0
   310
}
slouken@0
   311
slouken@1662
   312
void
slouken@1662
   313
SDL_AddBasicVideoDisplay (const SDL_DisplayMode * desktop_mode)
slouken@0
   314
{
slouken@1662
   315
    SDL_VideoDisplay display;
slouken@0
   316
slouken@1662
   317
    SDL_zero (display);
slouken@1662
   318
    if (desktop_mode) {
slouken@1662
   319
        display.desktop_mode = *desktop_mode;
slouken@1662
   320
    }
slouken@1662
   321
    display.current_mode = display.desktop_mode;
slouken@1662
   322
    display.max_windows = 1;
slouken@1662
   323
slouken@1662
   324
    SDL_AddVideoDisplay (&display);
slouken@0
   325
}
slouken@0
   326
slouken@1662
   327
void
slouken@1662
   328
SDL_AddVideoDisplay (SDL_VideoDisplay * display)
slouken@1662
   329
{
slouken@1662
   330
    SDL_VideoDisplay *displays;
slouken@0
   331
slouken@1662
   332
    displays =
slouken@1662
   333
        SDL_realloc (_this->displays,
slouken@1662
   334
                     (_this->num_displays + 1) * sizeof (*displays));
slouken@1662
   335
    if (displays) {
slouken@1662
   336
        displays[_this->num_displays] = *display;
slouken@1662
   337
        _this->displays = displays;
slouken@1662
   338
        _this->num_displays++;
slouken@1662
   339
    }
slouken@0
   340
}
slouken@0
   341
slouken@1662
   342
int
slouken@1662
   343
SDL_GetNumVideoDisplays (void)
slouken@0
   344
{
slouken@1662
   345
    if (!_this) {
slouken@1662
   346
        return 0;
slouken@1662
   347
    }
slouken@1662
   348
    return _this->num_displays;
slouken@0
   349
}
slouken@0
   350
slouken@1662
   351
int
slouken@1662
   352
SDL_SelectVideoDisplay (int index)
slouken@0
   353
{
slouken@1662
   354
    if (!_this) {
slouken@1662
   355
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   356
        return (-1);
slouken@1662
   357
    }
slouken@1662
   358
    if (index >= 0) {
slouken@1662
   359
        if (index >= _this->num_displays) {
slouken@1662
   360
            SDL_SetError ("index must be in the range 0 - %d",
slouken@1662
   361
                          _this->num_displays - 1);
slouken@1662
   362
            return -1;
slouken@1662
   363
        }
slouken@1662
   364
        _this->current_display = index;
slouken@1662
   365
    }
slouken@1662
   366
    return _this->current_display;
slouken@0
   367
}
slouken@0
   368
slouken@1662
   369
void
slouken@1662
   370
SDL_AddDisplayMode (int display, const SDL_DisplayMode * mode)
slouken@0
   371
{
slouken@1662
   372
    SDL_DisplayMode *modes;
slouken@1662
   373
    int i, nmodes;
slouken@0
   374
slouken@1662
   375
    /* Make sure we don't already have the mode in the list */
slouken@1662
   376
    modes = SDL_CurrentDisplay.display_modes;
slouken@1662
   377
    nmodes = SDL_CurrentDisplay.num_display_modes;
slouken@1662
   378
    for (i = 0; i < nmodes; ++i) {
slouken@1662
   379
        if (SDL_memcmp (mode, &modes[i], sizeof (*mode)) == 0) {
slouken@1662
   380
            return;
slouken@1662
   381
        }
slouken@1662
   382
    }
slouken@0
   383
slouken@1662
   384
    /* Go ahead and add the new mode */
slouken@1662
   385
    modes = SDL_realloc (modes, (nmodes + 1) * sizeof (*mode));
slouken@1662
   386
    if (modes) {
slouken@1662
   387
        SDL_CurrentDisplay.display_modes = modes;
slouken@1662
   388
        modes[nmodes] = *mode;
slouken@1662
   389
        SDL_CurrentDisplay.num_display_modes++;
slouken@1662
   390
    }
slouken@0
   391
}
slouken@0
   392
slouken@1662
   393
int
slouken@1662
   394
SDL_GetNumDisplayModes ()
slouken@1662
   395
{
slouken@1662
   396
    if (_this) {
slouken@1662
   397
        return SDL_CurrentDisplay.num_display_modes;
slouken@1662
   398
    }
slouken@1662
   399
    return 0;
slouken@1662
   400
}
slouken@910
   401
slouken@1662
   402
const SDL_DisplayMode *
slouken@1662
   403
SDL_GetDisplayMode (int index)
slouken@0
   404
{
slouken@1662
   405
    if (index < 0 || index >= SDL_GetNumDisplayModes ()) {
slouken@1662
   406
        SDL_SetError ("index must be in the range of 0 - %d",
slouken@1662
   407
                      SDL_GetNumDisplayModes ());
slouken@1662
   408
        return NULL;
slouken@1662
   409
    }
slouken@1662
   410
    return &SDL_CurrentDisplay.display_modes[index];
slouken@1662
   411
}
slouken@0
   412
slouken@1662
   413
const SDL_DisplayMode *
slouken@1662
   414
SDL_GetDesktopDisplayMode (void)
slouken@1662
   415
{
slouken@1662
   416
    if (_this) {
slouken@1662
   417
        return &SDL_CurrentDisplay.desktop_mode;
slouken@1662
   418
    }
slouken@1662
   419
    return NULL;
slouken@1662
   420
}
slouken@0
   421
slouken@1662
   422
const SDL_DisplayMode *
slouken@1662
   423
SDL_GetCurrentDisplayMode (void)
slouken@1662
   424
{
slouken@1662
   425
    if (_this) {
slouken@1662
   426
        return &SDL_CurrentDisplay.current_mode;
slouken@1662
   427
    }
slouken@1662
   428
    return NULL;
slouken@1662
   429
}
slouken@0
   430
slouken@1662
   431
SDL_DisplayMode *
slouken@1662
   432
SDL_GetClosestDisplayMode (const SDL_DisplayMode * mode,
slouken@1662
   433
                           SDL_DisplayMode * closest)
slouken@1662
   434
{
slouken@1662
   435
    Uint32 target_format;
slouken@1662
   436
    int target_refresh_rate;
slouken@1662
   437
    int i;
slouken@1662
   438
    SDL_DisplayMode *current, *match;
slouken@0
   439
slouken@1662
   440
    if (!_this || !mode || !closest) {
slouken@1662
   441
        return NULL;
slouken@1662
   442
    }
slouken@0
   443
slouken@1662
   444
    /* Default to the desktop format */
slouken@1662
   445
    if (mode->format) {
slouken@1662
   446
        target_format = mode->format;
slouken@1662
   447
    } else {
slouken@1662
   448
        target_format = SDL_CurrentDisplay.desktop_mode.format;
slouken@1662
   449
    }
slouken@0
   450
slouken@1662
   451
    /* Default to the desktop refresh rate */
slouken@1662
   452
    if (mode->refresh_rate) {
slouken@1662
   453
        target_refresh_rate = mode->refresh_rate;
slouken@1662
   454
    } else {
slouken@1662
   455
        target_refresh_rate = SDL_CurrentDisplay.desktop_mode.refresh_rate;
slouken@1662
   456
    }
slouken@14
   457
slouken@1662
   458
    match = NULL;
slouken@1662
   459
    for (i = 0; i < SDL_CurrentDisplay.num_display_modes; ++i) {
slouken@1662
   460
        current = &SDL_CurrentDisplay.display_modes[i];
slouken@0
   461
slouken@1662
   462
        if ((current->w && current->h) &&
slouken@1662
   463
            (current->w < mode->w || current->h < mode->h)) {
slouken@1662
   464
            /* Out of sorted modes large enough here */
slouken@1662
   465
            break;
slouken@1662
   466
        }
slouken@1662
   467
        if (!match || current->w < match->w || current->h < match->h) {
slouken@1662
   468
            match = current;
slouken@1662
   469
            continue;
slouken@1662
   470
        }
slouken@1662
   471
        if (current->format != match->format) {
slouken@1662
   472
            /* Sorted highest depth to lowest */
slouken@1662
   473
            if (current->format == target_format ||
slouken@1662
   474
                (SDL_BITSPERPIXEL (current->format) >=
slouken@1662
   475
                 SDL_BITSPERPIXEL (target_format)
slouken@1662
   476
                 && SDL_PIXELTYPE (current->format) ==
slouken@1662
   477
                 SDL_PIXELTYPE (target_format))) {
slouken@1662
   478
                match = current;
slouken@1662
   479
            }
slouken@1662
   480
            continue;
slouken@1662
   481
        }
slouken@1662
   482
        if (current->refresh_rate != match->refresh_rate) {
slouken@1662
   483
            /* Sorted highest refresh to lowest */
slouken@1662
   484
            if (current->refresh_rate >= target_refresh_rate) {
slouken@1662
   485
                match = current;
slouken@1662
   486
            }
slouken@1662
   487
        }
slouken@1662
   488
    }
slouken@1662
   489
    if (match) {
slouken@1662
   490
        if (match->format) {
slouken@1662
   491
            closest->format = match->format;
slouken@1662
   492
        } else {
slouken@1662
   493
            closest->format = mode->format;
slouken@1662
   494
        }
slouken@1662
   495
        if (match->w && match->h) {
slouken@1662
   496
            closest->w = match->w;
slouken@1662
   497
            closest->h = match->h;
slouken@1662
   498
        } else {
slouken@1662
   499
            closest->w = mode->w;
slouken@1662
   500
            closest->h = mode->h;
slouken@1662
   501
        }
slouken@1662
   502
        if (match->refresh_rate) {
slouken@1662
   503
            closest->refresh_rate = match->refresh_rate;
slouken@1662
   504
        } else {
slouken@1662
   505
            closest->refresh_rate = mode->refresh_rate;
slouken@1662
   506
        }
slouken@1662
   507
        return closest;
slouken@1662
   508
    }
slouken@1662
   509
    return NULL;
slouken@1662
   510
}
slouken@0
   511
slouken@1662
   512
int
slouken@1662
   513
SDL_SetDisplayMode (const SDL_DisplayMode * mode)
slouken@1662
   514
{
slouken@1662
   515
    SDL_DisplayMode display_mode;
slouken@229
   516
slouken@1662
   517
    if (!_this) {
slouken@1662
   518
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   519
        return -1;
slouken@1662
   520
    }
slouken@0
   521
slouken@1662
   522
    /* Make sure there's an actual display mode to set */
slouken@1662
   523
    if (!mode) {
slouken@1662
   524
        SDL_SetError ("No mode passed to SDL_SetDisplayMode");
slouken@1662
   525
        return -1;
slouken@1662
   526
    }
slouken@1662
   527
    display_mode = *mode;
slouken@0
   528
slouken@1662
   529
    /* Default to the current mode */
slouken@1662
   530
    if (!display_mode.format) {
slouken@1662
   531
        display_mode.format = SDL_CurrentDisplay.current_mode.format;
slouken@1662
   532
    }
slouken@1662
   533
    if (!display_mode.w) {
slouken@1662
   534
        display_mode.w = SDL_CurrentDisplay.current_mode.w;
slouken@1662
   535
    }
slouken@1662
   536
    if (!display_mode.h) {
slouken@1662
   537
        display_mode.h = SDL_CurrentDisplay.current_mode.h;
slouken@1662
   538
    }
slouken@1662
   539
    if (!display_mode.refresh_rate) {
slouken@1662
   540
        display_mode.refresh_rate =
slouken@1662
   541
            SDL_CurrentDisplay.current_mode.refresh_rate;
slouken@1662
   542
    }
slouken@0
   543
slouken@1662
   544
    /* Get a good video mode, the closest one possible */
slouken@1662
   545
    if (!SDL_GetClosestDisplayMode (&display_mode, &display_mode)) {
slouken@1662
   546
        SDL_SetError ("No video mode large enough for %dx%d",
slouken@1662
   547
                      display_mode.w, display_mode.h);
slouken@1662
   548
        return -1;
slouken@1662
   549
    }
slouken@0
   550
slouken@1662
   551
    /* See if there's anything left to do */
slouken@1662
   552
    if (SDL_memcmp
slouken@1662
   553
        (&display_mode, SDL_GetCurrentDisplayMode (),
slouken@1662
   554
         sizeof (display_mode)) == 0) {
slouken@1662
   555
        return 0;
slouken@1662
   556
    }
slouken@0
   557
slouken@1662
   558
    return _this->SetDisplayMode (_this, &display_mode);
slouken@1662
   559
}
slouken@0
   560
slouken@1662
   561
SDL_WindowID
slouken@1662
   562
SDL_CreateWindow (const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@1662
   563
{
slouken@1662
   564
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@1662
   565
                                  SDL_WINDOW_BORDERLESS |
slouken@1662
   566
                                  SDL_WINDOW_SHOWN |
slouken@1662
   567
                                  SDL_WINDOW_OPENGL |
slouken@1662
   568
                                  SDL_WINDOW_RESIZABLE |
slouken@1662
   569
                                  SDL_WINDOW_MAXIMIZED |
slouken@1662
   570
                                  SDL_WINDOW_MINIMIZED |
slouken@1662
   571
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@1662
   572
    SDL_Window window;
slouken@1662
   573
    int num_windows;
slouken@1662
   574
    SDL_Window *windows;
slouken@0
   575
slouken@1662
   576
    if (!_this) {
slouken@1662
   577
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   578
        return 0;
slouken@1662
   579
    }
slouken@0
   580
slouken@1662
   581
    SDL_zero (window);
slouken@1662
   582
    window.id = _this->next_window_id++;
slouken@1662
   583
    window.title = SDL_strdup (title);
slouken@1662
   584
    window.x = x;
slouken@1662
   585
    window.y = y;
slouken@1662
   586
    window.w = w;
slouken@1662
   587
    window.h = h;
slouken@1662
   588
    window.flags = (flags & allowed_flags);
slouken@910
   589
slouken@1662
   590
    if (_this->CreateWindow && _this->CreateWindow (_this, &window) < 0) {
slouken@1662
   591
        SDL_free (window.title);
slouken@1662
   592
        return 0;
slouken@1662
   593
    }
slouken@910
   594
slouken@1662
   595
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   596
    windows =
slouken@1662
   597
        SDL_realloc (SDL_CurrentDisplay.windows,
slouken@1662
   598
                     (num_windows + 1) * sizeof (*windows));
slouken@1662
   599
    if (!windows) {
slouken@1662
   600
        if (_this->DestroyWindow) {
slouken@1662
   601
            _this->DestroyWindow (_this, &window);
slouken@1662
   602
        }
slouken@1662
   603
        SDL_free (window.title);
slouken@1662
   604
        return 0;
slouken@1662
   605
    }
slouken@1662
   606
    windows[num_windows] = window;
slouken@1662
   607
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   608
    SDL_CurrentDisplay.num_windows++;
slouken@910
   609
slouken@1662
   610
    return window.id;
slouken@1662
   611
}
icculus@650
   612
slouken@1662
   613
SDL_WindowID
slouken@1662
   614
SDL_CreateWindowFrom (void *data)
slouken@1662
   615
{
slouken@1662
   616
    SDL_Window window;
slouken@1662
   617
    int num_windows;
slouken@1662
   618
    SDL_Window *windows;
slouken@0
   619
slouken@1662
   620
    if (!_this) {
slouken@1662
   621
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   622
        return (0);
slouken@1662
   623
    }
slouken@0
   624
slouken@1662
   625
    SDL_zero (window);
slouken@1662
   626
    window.id = _this->next_window_id++;
slouken@1662
   627
slouken@1662
   628
    if (!_this->CreateWindowFrom ||
slouken@1662
   629
        _this->CreateWindowFrom (_this, &window, data) < 0) {
slouken@1662
   630
        return 0;
slouken@1662
   631
    }
slouken@1662
   632
slouken@1662
   633
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   634
    windows =
slouken@1662
   635
        SDL_realloc (SDL_CurrentDisplay.windows,
slouken@1662
   636
                     (num_windows + 1) * sizeof (*windows));
slouken@1662
   637
    if (!windows) {
slouken@1662
   638
        if (_this->DestroyWindow) {
slouken@1662
   639
            _this->DestroyWindow (_this, &window);
slouken@1662
   640
        }
slouken@1662
   641
        SDL_free (window.title);
slouken@1662
   642
        return 0;
slouken@1662
   643
    }
slouken@1662
   644
    windows[num_windows] = window;
slouken@1662
   645
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   646
    SDL_CurrentDisplay.num_windows++;
slouken@1662
   647
slouken@1662
   648
    return window.id;
slouken@1662
   649
}
slouken@1662
   650
slouken@1662
   651
static __inline__ SDL_Window *
slouken@1662
   652
SDL_GetWindowFromID (SDL_WindowID windowID)
slouken@1662
   653
{
slouken@1662
   654
    int i, j;
slouken@1662
   655
slouken@1662
   656
    if (!_this) {
slouken@1662
   657
        return NULL;
slouken@1662
   658
    }
slouken@1662
   659
slouken@1662
   660
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   661
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   662
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   663
            SDL_Window *window = &display->windows[j];
slouken@1662
   664
            if (window->id == windowID) {
slouken@1662
   665
                return window;
slouken@1662
   666
            }
slouken@1662
   667
        }
slouken@1662
   668
    }
slouken@1662
   669
    return NULL;
slouken@1662
   670
}
slouken@1662
   671
slouken@1662
   672
SDL_Window *
slouken@1662
   673
SDL_GetWindowFromSurface (SDL_Surface * surface)
slouken@1662
   674
{
slouken@1662
   675
    int i, j;
slouken@1662
   676
slouken@1662
   677
    if (!_this) {
slouken@1662
   678
        return NULL;
slouken@1662
   679
    }
slouken@1662
   680
slouken@1662
   681
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   682
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   683
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   684
            SDL_Window *window = &display->windows[j];
slouken@1662
   685
            if (surface == window->surface || surface == window->shadow) {
slouken@1662
   686
                return window;
slouken@1662
   687
            }
slouken@1662
   688
        }
slouken@1662
   689
    }
slouken@1662
   690
    return NULL;
slouken@1662
   691
}
slouken@1662
   692
slouken@1662
   693
slouken@1662
   694
Uint32
slouken@1662
   695
SDL_GetWindowFlags (SDL_WindowID windowID)
slouken@1662
   696
{
slouken@1662
   697
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   698
slouken@1662
   699
    if (!window) {
slouken@1662
   700
        return 0;
slouken@1662
   701
    }
slouken@1662
   702
    return window->flags;
slouken@1662
   703
}
slouken@1662
   704
slouken@1662
   705
void
slouken@1662
   706
SDL_SetWindowTitle (SDL_WindowID windowID, const char *title)
slouken@1662
   707
{
slouken@1662
   708
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   709
slouken@1662
   710
    if (!window) {
slouken@1662
   711
        return;
slouken@1662
   712
    }
slouken@1662
   713
    if (window->title) {
slouken@1662
   714
        SDL_free (window->title);
slouken@1662
   715
    }
slouken@1662
   716
    window->title = SDL_strdup (title);
slouken@1662
   717
slouken@1662
   718
    if (_this->SetWindowTitle) {
slouken@1662
   719
        _this->SetWindowTitle (_this, window);
slouken@1662
   720
    }
slouken@1662
   721
}
slouken@1662
   722
slouken@1662
   723
const char *
slouken@1662
   724
SDL_GetWindowTitle (SDL_WindowID windowID)
slouken@1662
   725
{
slouken@1662
   726
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   727
slouken@1662
   728
    if (!window) {
slouken@1662
   729
        return NULL;
slouken@1662
   730
    }
slouken@1662
   731
    return window->title;
slouken@1662
   732
}
slouken@1662
   733
slouken@1662
   734
void
slouken@1662
   735
SDL_SetWindowData (SDL_WindowID windowID, void *userdata)
slouken@1662
   736
{
slouken@1662
   737
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   738
slouken@1662
   739
    if (!window) {
slouken@1662
   740
        return;
slouken@1662
   741
    }
slouken@1662
   742
    window->userdata = userdata;
slouken@1662
   743
}
slouken@1662
   744
slouken@1662
   745
void *
slouken@1662
   746
SDL_GetWindowData (SDL_WindowID windowID)
slouken@1662
   747
{
slouken@1662
   748
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   749
slouken@1662
   750
    if (!window) {
slouken@1662
   751
        return NULL;
slouken@1662
   752
    }
slouken@1662
   753
    return window->userdata;
slouken@1662
   754
}
slouken@1662
   755
slouken@1662
   756
void
slouken@1662
   757
SDL_SetWindowPosition (SDL_WindowID windowID, int x, int y)
slouken@1662
   758
{
slouken@1662
   759
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   760
slouken@1662
   761
    if (!window) {
slouken@1662
   762
        return;
slouken@1662
   763
    }
slouken@1662
   764
slouken@1662
   765
    window->x = x;
slouken@1662
   766
    window->y = y;
slouken@1662
   767
slouken@1662
   768
    if (_this->SetWindowPosition) {
slouken@1662
   769
        _this->SetWindowPosition (_this, window);
slouken@1662
   770
    }
slouken@1662
   771
}
slouken@1662
   772
slouken@1662
   773
void
slouken@1662
   774
SDL_GetWindowPosition (SDL_WindowID windowID, int *x, int *y)
slouken@1662
   775
{
slouken@1662
   776
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   777
slouken@1662
   778
    if (!window) {
slouken@1662
   779
        return;
slouken@1662
   780
    }
slouken@1662
   781
    if (x) {
slouken@1662
   782
        *x = window->x;
slouken@1662
   783
    }
slouken@1662
   784
    if (y) {
slouken@1662
   785
        *y = window->y;
slouken@1662
   786
    }
slouken@1662
   787
}
slouken@1662
   788
slouken@1662
   789
void
slouken@1662
   790
SDL_SetWindowSize (SDL_WindowID windowID, int w, int h)
slouken@1662
   791
{
slouken@1662
   792
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   793
slouken@1662
   794
    if (!window) {
slouken@1662
   795
        return;
slouken@1662
   796
    }
slouken@1662
   797
slouken@1662
   798
    window->w = w;
slouken@1662
   799
    window->h = h;
slouken@1662
   800
slouken@1662
   801
    if (_this->SetWindowSize) {
slouken@1662
   802
        _this->SetWindowSize (_this, window);
slouken@1662
   803
    }
slouken@1662
   804
}
slouken@1662
   805
slouken@1662
   806
void
slouken@1662
   807
SDL_GetWindowSize (SDL_WindowID windowID, int *w, int *h)
slouken@1662
   808
{
slouken@1662
   809
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   810
slouken@1662
   811
    if (!window) {
slouken@1662
   812
        return;
slouken@1662
   813
    }
slouken@1662
   814
    if (w) {
slouken@1662
   815
        *w = window->w;
slouken@1662
   816
    }
slouken@1662
   817
    if (h) {
slouken@1662
   818
        *h = window->h;
slouken@1662
   819
    }
slouken@1662
   820
}
slouken@1662
   821
slouken@1662
   822
void
slouken@1662
   823
SDL_ShowWindow (SDL_WindowID windowID)
slouken@1662
   824
{
slouken@1662
   825
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   826
slouken@1662
   827
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   828
        return;
slouken@1662
   829
    }
slouken@1662
   830
slouken@1662
   831
    window->flags |= SDL_WINDOW_SHOWN;
slouken@1662
   832
slouken@1662
   833
    if (_this->ShowWindow) {
slouken@1662
   834
        _this->ShowWindow (_this, window);
slouken@1662
   835
    }
slouken@1662
   836
}
slouken@1662
   837
slouken@1662
   838
void
slouken@1662
   839
SDL_HideWindow (SDL_WindowID windowID)
slouken@1662
   840
{
slouken@1662
   841
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   842
slouken@1662
   843
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   844
        return;
slouken@1662
   845
    }
slouken@1662
   846
slouken@1662
   847
    window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1662
   848
slouken@1662
   849
    if (_this->HideWindow) {
slouken@1662
   850
        _this->HideWindow (_this, window);
slouken@1662
   851
    }
slouken@1662
   852
}
slouken@1662
   853
slouken@1662
   854
void
slouken@1662
   855
SDL_RaiseWindow (SDL_WindowID windowID)
slouken@1662
   856
{
slouken@1662
   857
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   858
slouken@1662
   859
    if (!window) {
slouken@1662
   860
        return;
slouken@1662
   861
    }
slouken@1662
   862
slouken@1662
   863
    if (_this->RaiseWindow) {
slouken@1662
   864
        _this->RaiseWindow (_this, window);
slouken@1662
   865
    }
slouken@1662
   866
}
slouken@1662
   867
slouken@1662
   868
void
slouken@1662
   869
SDL_MaximizeWindow (SDL_WindowID windowID)
slouken@1662
   870
{
slouken@1662
   871
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   872
slouken@1662
   873
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@1662
   874
        return;
slouken@1662
   875
    }
slouken@1662
   876
slouken@1662
   877
    window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1662
   878
slouken@1662
   879
    if (_this->MaximizeWindow) {
slouken@1662
   880
        _this->MaximizeWindow (_this, window);
slouken@1662
   881
    }
slouken@1662
   882
}
slouken@1662
   883
slouken@1662
   884
void
slouken@1662
   885
SDL_MinimizeWindow (SDL_WindowID windowID)
slouken@1662
   886
{
slouken@1662
   887
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   888
slouken@1662
   889
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@1662
   890
        return;
slouken@1662
   891
    }
slouken@1662
   892
slouken@1662
   893
    window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1662
   894
slouken@1662
   895
    if (_this->MinimizeWindow) {
slouken@1662
   896
        _this->MinimizeWindow (_this, window);
slouken@1662
   897
    }
slouken@1662
   898
}
slouken@1662
   899
slouken@1662
   900
void
slouken@1662
   901
SDL_RestoreWindow (SDL_WindowID windowID)
slouken@1662
   902
{
slouken@1662
   903
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   904
slouken@1662
   905
    if (!window
slouken@1662
   906
        || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@1662
   907
        return;
slouken@1662
   908
    }
slouken@1662
   909
slouken@1662
   910
    window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
slouken@1662
   911
slouken@1662
   912
    if (_this->RestoreWindow) {
slouken@1662
   913
        _this->RestoreWindow (_this, window);
slouken@1662
   914
    }
slouken@1662
   915
}
slouken@1662
   916
slouken@1662
   917
void
slouken@1662
   918
SDL_SetWindowGrab (SDL_WindowID windowID, int mode)
slouken@1662
   919
{
slouken@1662
   920
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   921
slouken@1662
   922
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@1662
   923
        return;
slouken@1662
   924
    }
slouken@1662
   925
slouken@1662
   926
    if (mode) {
slouken@1662
   927
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   928
    } else {
slouken@1662
   929
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   930
    }
slouken@1662
   931
slouken@1662
   932
    if (_this->SetWindowGrab) {
slouken@1662
   933
        _this->SetWindowGrab (_this, window);
slouken@1662
   934
    }
slouken@1662
   935
}
slouken@1662
   936
slouken@1662
   937
int
slouken@1662
   938
SDL_GetWindowGrab (SDL_WindowID windowID)
slouken@1662
   939
{
slouken@1662
   940
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   941
slouken@1662
   942
    if (!window) {
slouken@1662
   943
        return 0;
slouken@1662
   944
    }
slouken@1662
   945
slouken@1662
   946
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1662
   947
}
slouken@1662
   948
slouken@1662
   949
void
slouken@1662
   950
SDL_DestroyWindow (SDL_WindowID windowID)
slouken@1662
   951
{
slouken@1662
   952
    int i, j;
slouken@1662
   953
slouken@1662
   954
    if (!_this) {
slouken@1662
   955
        return;
slouken@1662
   956
    }
slouken@1662
   957
slouken@1662
   958
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   959
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   960
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   961
            SDL_Window *window = &display->windows[j];
slouken@1662
   962
            if (window->id != windowID) {
slouken@1662
   963
                continue;
slouken@1662
   964
            }
slouken@1662
   965
            if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1662
   966
                window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   967
                _this->SetWindowGrab (_this, window);
slouken@1662
   968
            }
slouken@1662
   969
            if (window->shadow) {
slouken@1662
   970
                SDL_FreeSurface (window->shadow);
slouken@1662
   971
            }
slouken@1662
   972
            if (window->surface) {
slouken@1662
   973
                SDL_FreeSurface (window->surface);
slouken@1662
   974
            }
slouken@1662
   975
            if (_this->DestroyWindow) {
slouken@1662
   976
                _this->DestroyWindow (_this, window);
slouken@1662
   977
            }
slouken@1662
   978
            if (window->title) {
slouken@1662
   979
                SDL_free (window->title);
slouken@1662
   980
            }
slouken@1662
   981
            if (window->gamma) {
slouken@1662
   982
                SDL_free (window->gamma);
slouken@1662
   983
            }
slouken@1662
   984
            if (j != display->num_windows - 1) {
slouken@1662
   985
                SDL_memcpy (&display->windows[i],
slouken@1662
   986
                            &display->windows[i + 1],
slouken@1662
   987
                            (display->num_windows - i -
slouken@1662
   988
                             1) * sizeof (*window));
slouken@1662
   989
            }
slouken@1662
   990
            --display->num_windows;
slouken@1662
   991
            return;
slouken@1662
   992
        }
slouken@1662
   993
    }
slouken@1662
   994
}
slouken@1662
   995
slouken@1662
   996
SDL_Surface *
slouken@1662
   997
SDL_CreateWindowSurface (SDL_WindowID windowID, Uint32 format, Uint32 flags)
slouken@1662
   998
{
slouken@1662
   999
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
  1000
    SDL_Surface *surface;
slouken@1662
  1001
    SDL_Surface *shadow;
slouken@1662
  1002
    Uint32 surface_format;
slouken@1662
  1003
    Uint32 black;
slouken@1662
  1004
slouken@1662
  1005
    if (!window) {
slouken@1662
  1006
        return NULL;
slouken@1662
  1007
    }
slouken@1662
  1008
slouken@1662
  1009
    if (!_this->CreateWindowSurface) {
slouken@1662
  1010
        return NULL;
slouken@1662
  1011
    }
slouken@1662
  1012
slouken@1662
  1013
    if (!window->surface) {
slouken@1662
  1014
        window->surface = _this->CreateWindowSurface (_this, window);
slouken@1662
  1015
        if (!window->surface) {
slouken@1662
  1016
            return NULL;
slouken@1662
  1017
        }
slouken@1662
  1018
    }
slouken@1662
  1019
slouken@1662
  1020
    if (window->shadow) {
slouken@1662
  1021
        SDL_FreeSurface (window->shadow);
slouken@1662
  1022
        window->shadow = NULL;
slouken@1662
  1023
    }
slouken@1662
  1024
slouken@1662
  1025
    surface = window->surface;
slouken@1662
  1026
    surface_format =
slouken@1662
  1027
        SDL_MasksToPixelFormatEnum (surface->format->BitsPerPixel,
slouken@1662
  1028
                                    surface->format->Rmask,
slouken@1662
  1029
                                    surface->format->Gmask,
slouken@1662
  1030
                                    surface->format->Bmask,
slouken@1662
  1031
                                    surface->format->Amask);
slouken@1662
  1032
slouken@1662
  1033
    /* Create a shadow surface if necessary */
slouken@1662
  1034
    if ((!(flags & SDL_ANYFORMAT) && (surface_format != format)) ||
slouken@1662
  1035
        ((flags & SDL_HWPALETTE)
slouken@1662
  1036
         && !(window->surface->flags & SDL_HWPALETTE))) {
slouken@1662
  1037
        int bpp;
slouken@1662
  1038
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1662
  1039
slouken@1662
  1040
        SDL_PixelFormatEnumToMasks (format, &bpp, &Rmask, &Gmask, &Bmask,
slouken@1662
  1041
                                    &Amask);
slouken@1662
  1042
        shadow =
slouken@1662
  1043
            SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h,
slouken@1662
  1044
                                  bpp, Rmask, Gmask, Bmask, Amask);
slouken@1662
  1045
        if (shadow == NULL) {
slouken@1662
  1046
            return NULL;
slouken@1662
  1047
        }
slouken@1662
  1048
slouken@1662
  1049
        /* 8-bit shadow surfaces report that they have exclusive palette */
slouken@1662
  1050
        if (shadow->format->palette) {
slouken@1662
  1051
            shadow->flags |= SDL_HWPALETTE;
slouken@1662
  1052
            if (format == surface_format) {
slouken@1662
  1053
                SDL_memcpy (shadow->format->palette->colors,
slouken@1662
  1054
                            surface->format->palette->colors,
slouken@1662
  1055
                            surface->format->palette->ncolors *
slouken@1662
  1056
                            sizeof (SDL_Color));
slouken@1662
  1057
            } else {
slouken@1662
  1058
                SDL_DitherColors (shadow->format->palette->colors, bpp);
slouken@1662
  1059
            }
slouken@1662
  1060
        }
slouken@1662
  1061
        surface = window->shadow = shadow;
slouken@1662
  1062
    }
slouken@1662
  1063
slouken@1662
  1064
    /* Clear the surface for display */
slouken@1662
  1065
    {
slouken@1662
  1066
        Uint32 black = SDL_MapRGB (surface->format, 0, 0, 0);
slouken@1662
  1067
        SDL_FillRect (surface, NULL, black);
slouken@1662
  1068
        if (surface->flags & SDL_DOUBLEBUF) {
slouken@1662
  1069
            SDL_Flip (surface);
slouken@1662
  1070
            SDL_FillRect (surface, NULL, black);
slouken@1662
  1071
        }
slouken@1662
  1072
        SDL_Flip (surface);
slouken@1662
  1073
    }
slouken@1662
  1074
slouken@1662
  1075
    return surface;
slouken@0
  1076
}
slouken@0
  1077
slouken@0
  1078
/* 
slouken@0
  1079
 * Convert a surface into the video pixel format.
slouken@0
  1080
 */
slouken@1662
  1081
SDL_Surface *
slouken@1662
  1082
SDL_DisplayFormat (SDL_Surface * surface)
slouken@0
  1083
{
slouken@1662
  1084
    Uint32 flags;
slouken@0
  1085
slouken@1662
  1086
    if (!SDL_PublicSurface) {
slouken@1662
  1087
        SDL_SetError ("No video mode has been set");
slouken@1662
  1088
        return (NULL);
slouken@1662
  1089
    }
slouken@1662
  1090
    /* Set the flags appropriate for copying to display surface */
slouken@1662
  1091
    if (((SDL_PublicSurface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
slouken@1662
  1092
        && _this->info.blit_hw)
slouken@1662
  1093
        flags = SDL_HWSURFACE;
slouken@1662
  1094
    else
slouken@1662
  1095
        flags = SDL_SWSURFACE;
slouken@0
  1096
#ifdef AUTORLE_DISPLAYFORMAT
slouken@1662
  1097
    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
slouken@1662
  1098
    flags |= SDL_RLEACCELOK;
slouken@0
  1099
#else
slouken@1662
  1100
    flags |=
slouken@1662
  1101
        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@0
  1102
#endif
slouken@1662
  1103
    return (SDL_ConvertSurface (surface, SDL_PublicSurface->format, flags));
slouken@0
  1104
}
slouken@0
  1105
slouken@0
  1106
/*
slouken@0
  1107
 * Convert a surface into a format that's suitable for blitting to
slouken@0
  1108
 * the screen, but including an alpha channel.
slouken@0
  1109
 */
slouken@1662
  1110
SDL_Surface *
slouken@1662
  1111
SDL_DisplayFormatAlpha (SDL_Surface * surface)
slouken@0
  1112
{
slouken@1662
  1113
    SDL_PixelFormat *vf;
slouken@1662
  1114
    SDL_PixelFormat *format;
slouken@1662
  1115
    SDL_Surface *converted;
slouken@1662
  1116
    Uint32 flags;
slouken@1662
  1117
    /* default to ARGB8888 */
slouken@1662
  1118
    Uint32 amask = 0xff000000;
slouken@1662
  1119
    Uint32 rmask = 0x00ff0000;
slouken@1662
  1120
    Uint32 gmask = 0x0000ff00;
slouken@1662
  1121
    Uint32 bmask = 0x000000ff;
slouken@0
  1122
slouken@1662
  1123
    if (!SDL_PublicSurface) {
slouken@1662
  1124
        SDL_SetError ("No video mode has been set");
slouken@1662
  1125
        return (NULL);
slouken@1662
  1126
    }
slouken@1662
  1127
    vf = SDL_PublicSurface->format;
slouken@0
  1128
slouken@1662
  1129
    switch (vf->BytesPerPixel) {
slouken@1662
  1130
    case 2:
slouken@1662
  1131
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1662
  1132
           For anything else (like ARGB4444) it doesn't matter
slouken@1662
  1133
           since we have no special code for it anyway */
slouken@1662
  1134
        if ((vf->Rmask == 0x1f) &&
slouken@1662
  1135
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1662
  1136
            rmask = 0xff;
slouken@1662
  1137
            bmask = 0xff0000;
slouken@1662
  1138
        }
slouken@1662
  1139
        break;
slouken@0
  1140
slouken@1662
  1141
    case 3:
slouken@1662
  1142
    case 4:
slouken@1662
  1143
        /* Keep the video format, as long as the high 8 bits are
slouken@1662
  1144
           unused or alpha */
slouken@1662
  1145
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1662
  1146
            rmask = 0xff;
slouken@1662
  1147
            bmask = 0xff0000;
slouken@1662
  1148
        }
slouken@1662
  1149
        break;
slouken@0
  1150
slouken@1662
  1151
    default:
slouken@1662
  1152
        /* We have no other optimised formats right now. When/if a new
slouken@1662
  1153
           optimised alpha format is written, add the converter here */
slouken@1662
  1154
        break;
slouken@1662
  1155
    }
slouken@1662
  1156
    format = SDL_AllocFormat (32, rmask, gmask, bmask, amask);
slouken@1662
  1157
    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
slouken@1662
  1158
    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1662
  1159
    converted = SDL_ConvertSurface (surface, format, flags);
slouken@1662
  1160
    SDL_FreeFormat (format);
slouken@1662
  1161
    return (converted);
slouken@0
  1162
}
slouken@0
  1163
slouken@0
  1164
/*
slouken@0
  1165
 * Update a specific portion of the physical screen
slouken@0
  1166
 */
slouken@1662
  1167
void
slouken@1662
  1168
SDL_UpdateRect (SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@0
  1169
{
slouken@1662
  1170
    if (screen) {
slouken@1662
  1171
        SDL_Rect rect;
slouken@0
  1172
slouken@1662
  1173
        /* Perform some checking */
slouken@1662
  1174
        if (w == 0)
slouken@1662
  1175
            w = screen->w;
slouken@1662
  1176
        if (h == 0)
slouken@1662
  1177
            h = screen->h;
slouken@1662
  1178
        if ((int) (x + w) > screen->w)
slouken@1662
  1179
            return;
slouken@1662
  1180
        if ((int) (y + h) > screen->h)
slouken@1662
  1181
            return;
slouken@0
  1182
slouken@1662
  1183
        /* Fill the rectangle */
slouken@1662
  1184
        rect.x = (Sint16) x;
slouken@1662
  1185
        rect.y = (Sint16) y;
slouken@1662
  1186
        rect.w = (Uint16) w;
slouken@1662
  1187
        rect.h = (Uint16) h;
slouken@1662
  1188
        SDL_UpdateRects (screen, 1, &rect);
slouken@1662
  1189
    }
slouken@0
  1190
}
slouken@1662
  1191
void
slouken@1662
  1192
SDL_UpdateRects (SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@0
  1193
{
slouken@1662
  1194
    int i;
slouken@1662
  1195
    SDL_Window *window;
slouken@0
  1196
slouken@1662
  1197
    /* Find the window corresponding to this surface */
slouken@1662
  1198
    window = SDL_GetWindowFromSurface (screen);
slouken@1662
  1199
    if (!window) {
slouken@1662
  1200
        SDL_SetError ("Couldn't find window associated with surface");
slouken@1662
  1201
        return;
slouken@1662
  1202
    }
slouken@0
  1203
slouken@1662
  1204
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1205
        if (SHOULD_DRAWCURSOR (SDL_cursorstate)) {
slouken@1662
  1206
            SDL_LockCursor ();
slouken@1662
  1207
            SDL_DrawCursor (screen);
slouken@1662
  1208
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1209
                SDL_LowerBlit (screen, &rects[i], window->surface, &rects[i]);
slouken@1662
  1210
            }
slouken@1662
  1211
            SDL_EraseCursor (screen);
slouken@1662
  1212
            SDL_UnlockCursor ();
slouken@1662
  1213
        } else {
slouken@1662
  1214
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1215
                SDL_LowerBlit (screen, &rects[i], window->surface, &rects[i]);
slouken@1662
  1216
            }
slouken@1662
  1217
        }
slouken@1662
  1218
slouken@1662
  1219
        /* Fall through to video surface update */
slouken@1662
  1220
        screen = window->surface;
slouken@1662
  1221
    }
slouken@1662
  1222
    if ((screen->flags & SDL_SCREEN_SURFACE) && _this->UpdateWindowSurface) {
slouken@1662
  1223
        /* Update the video surface */
slouken@1662
  1224
        if (screen->offset) {
slouken@1662
  1225
            int offset_y = screen->offset / screen->pitch;
slouken@1662
  1226
            int offset_x = screen->offset % screen->pitch;
slouken@1662
  1227
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1228
                rects[i].x += offset_x;
slouken@1662
  1229
                rects[i].y += offset_y;
slouken@1662
  1230
            }
slouken@1662
  1231
            _this->UpdateWindowSurface (_this, window, numrects, rects);
slouken@1662
  1232
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1233
                rects[i].x -= offset_x;
slouken@1662
  1234
                rects[i].y -= offset_y;
slouken@1662
  1235
            }
slouken@1662
  1236
        } else {
slouken@1662
  1237
            _this->UpdateWindowSurface (_this, window, numrects, rects);
slouken@1662
  1238
        }
slouken@1662
  1239
    }
slouken@0
  1240
}
slouken@0
  1241
slouken@0
  1242
/*
slouken@0
  1243
 * Performs hardware double buffering, if possible, or a full update if not.
slouken@0
  1244
 */
slouken@1662
  1245
int
slouken@1662
  1246
SDL_Flip (SDL_Surface * screen)
slouken@0
  1247
{
slouken@1662
  1248
    SDL_Window *window;
slouken@0
  1249
slouken@1662
  1250
    /* Find the window corresponding to this surface */
slouken@1662
  1251
    window = SDL_GetWindowFromSurface (screen);
slouken@1662
  1252
    if (!window) {
slouken@1662
  1253
        SDL_SetError ("Couldn't find window associated with surface");
slouken@1662
  1254
        return;
slouken@1662
  1255
    }
slouken@0
  1256
slouken@1662
  1257
    /* Copy the shadow surface to the video surface */
slouken@1662
  1258
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1259
        SDL_Rect rect;
slouken@1662
  1260
slouken@1662
  1261
        rect.x = 0;
slouken@1662
  1262
        rect.y = 0;
slouken@1662
  1263
        rect.w = screen->w;
slouken@1662
  1264
        rect.h = screen->h;
slouken@1662
  1265
        if (SHOULD_DRAWCURSOR (SDL_cursorstate)) {
slouken@1662
  1266
            SDL_LockCursor ();
slouken@1662
  1267
            SDL_DrawCursor (screen);
slouken@1662
  1268
            SDL_LowerBlit (screen, &rect, window->surface, &rect);
slouken@1662
  1269
            SDL_EraseCursor (screen);
slouken@1662
  1270
            SDL_UnlockCursor ();
slouken@1662
  1271
        } else {
slouken@1662
  1272
            SDL_LowerBlit (screen, &rect, window->surface, &rect);
slouken@1662
  1273
        }
slouken@1662
  1274
slouken@1662
  1275
        /* Fall through to video surface update */
slouken@1662
  1276
        screen = window->surface;
slouken@1662
  1277
    }
slouken@1662
  1278
    if (screen->flags & SDL_DOUBLEBUF) {
slouken@1662
  1279
        _this->FlipWindowSurface (_this, window);
slouken@1662
  1280
    } else {
slouken@1662
  1281
        SDL_UpdateRect (screen, 0, 0, 0, 0);
slouken@1662
  1282
    }
slouken@1662
  1283
    return (0);
slouken@0
  1284
}
slouken@0
  1285
slouken@1662
  1286
int
slouken@1662
  1287
SDL_SetColors (SDL_Surface * screen, SDL_Color * colors, int firstcolor,
slouken@1662
  1288
               int ncolors)
slouken@0
  1289
{
slouken@1662
  1290
    SDL_Window *window = NULL;
slouken@1662
  1291
    SDL_Palette *pal;
slouken@1662
  1292
    int gotall;
slouken@1662
  1293
    int palsize;
slouken@0
  1294
slouken@1662
  1295
    /* Verify the parameters */
slouken@1662
  1296
    pal = screen->format->palette;
slouken@1662
  1297
    if (!pal) {
slouken@1662
  1298
        return 0;               /* not a palettized surface */
slouken@1662
  1299
    }
slouken@1662
  1300
    gotall = 1;
slouken@1662
  1301
    palsize = 1 << screen->format->BitsPerPixel;
slouken@1662
  1302
    if (ncolors > (palsize - firstcolor)) {
slouken@1662
  1303
        ncolors = (palsize - firstcolor);
slouken@1662
  1304
        gotall = 0;
slouken@1662
  1305
    }
slouken@0
  1306
slouken@1662
  1307
    if (colors != (pal->colors + firstcolor)) {
slouken@1662
  1308
        SDL_memcpy (pal->colors + firstcolor, colors,
slouken@1662
  1309
                    ncolors * sizeof (*colors));
slouken@1662
  1310
    }
slouken@1662
  1311
    SDL_FormatChanged (screen);
slouken@1662
  1312
slouken@1662
  1313
    if (screen->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
slouken@1662
  1314
        window = SDL_GetWindowFromSurface (screen);
slouken@1662
  1315
        if (!window) {
slouken@1662
  1316
            return 0;
slouken@1662
  1317
        }
slouken@1662
  1318
    }
slouken@1662
  1319
slouken@1662
  1320
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1321
        SDL_Palette *vidpal;
slouken@1662
  1322
slouken@1662
  1323
        vidpal = window->surface->format->palette;
slouken@1662
  1324
        if (vidpal && vidpal->ncolors == pal->ncolors) {
slouken@1662
  1325
            /* This is a shadow surface, and the physical
slouken@1662
  1326
             * framebuffer is also indexed. Propagate the
slouken@1662
  1327
             * changes to its logical palette so that
slouken@1662
  1328
             * updates are always identity blits
slouken@1662
  1329
             */
slouken@1662
  1330
            SDL_memcpy (vidpal->colors + firstcolor, colors,
slouken@1662
  1331
                        ncolors * sizeof (*colors));
slouken@1662
  1332
        }
slouken@1662
  1333
        if (window->surface->flags & SDL_HWPALETTE) {
slouken@1662
  1334
            /* Set the physical palette */
slouken@1662
  1335
            screen = window->surface;
slouken@1662
  1336
        } else {
slouken@1662
  1337
            SDL_UpdateRect (screen, 0, 0, 0, 0);
slouken@1662
  1338
        }
slouken@1662
  1339
    }
slouken@1662
  1340
slouken@1662
  1341
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1662
  1342
        if (_this->SetWindowColors) {
slouken@1662
  1343
            gotall =
slouken@1662
  1344
                _this->SetWindowColors (_this, window, firstcolor, ncolors,
slouken@1662
  1345
                                        colors);
slouken@1662
  1346
            if (!gotall) {
slouken@1662
  1347
                /* The video flags shouldn't have SDL_HWPALETTE, and
slouken@1662
  1348
                   the video driver is responsible for copying back the
slouken@1662
  1349
                   correct colors into the video surface palette.
slouken@1662
  1350
                 */
slouken@1662
  1351
                ;
slouken@1662
  1352
            }
slouken@1662
  1353
        }
slouken@1662
  1354
        SDL_CursorPaletteChanged ();
slouken@1662
  1355
    }
slouken@1662
  1356
slouken@1662
  1357
    return gotall;
slouken@0
  1358
}
slouken@0
  1359
slouken@1662
  1360
void
slouken@1662
  1361
SDL_VideoQuit (void)
slouken@0
  1362
{
slouken@1662
  1363
    int i, j;
slouken@0
  1364
slouken@1662
  1365
    if (!_this) {
slouken@1662
  1366
        return;
slouken@1662
  1367
    }
slouken@0
  1368
slouken@1662
  1369
    /* Halt event processing before doing anything else */
slouken@1662
  1370
    SDL_StopEventLoop ();
slouken@0
  1371
slouken@1662
  1372
    /* Clean up allocated window manager items */
slouken@1662
  1373
    SDL_CursorQuit ();
slouken@0
  1374
slouken@1662
  1375
    /* Clean up the system video */
slouken@1662
  1376
    for (i = _this->num_displays; i--;) {
slouken@1662
  1377
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1378
        for (j = display->num_windows; j--;) {
slouken@1662
  1379
            SDL_DestroyWindow (display->windows[i].id);
slouken@1662
  1380
        }
slouken@1662
  1381
        if (display->windows) {
slouken@1662
  1382
            SDL_free (display->windows);
slouken@1662
  1383
            display->windows = NULL;
slouken@1662
  1384
        }
slouken@1662
  1385
    }
slouken@1662
  1386
    _this->VideoQuit (_this);
slouken@1662
  1387
    if (_this->displays) {
slouken@1662
  1388
        SDL_free (_this->displays);
slouken@1662
  1389
    }
slouken@1662
  1390
    _this->free (_this);
slouken@1662
  1391
    _this = NULL;
slouken@0
  1392
}
slouken@0
  1393
slouken@0
  1394
/* Load the GL driver library */
slouken@1662
  1395
int
slouken@1662
  1396
SDL_GL_LoadLibrary (const char *path)
slouken@0
  1397
{
slouken@1662
  1398
    int retval;
slouken@0
  1399
slouken@1662
  1400
    retval = -1;
slouken@1662
  1401
    if (_this == NULL) {
slouken@1662
  1402
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
  1403
    } else {
slouken@1662
  1404
        if (_this->GL_LoadLibrary) {
slouken@1662
  1405
            retval = _this->GL_LoadLibrary (_this, path);
slouken@1662
  1406
        } else {
slouken@1662
  1407
            SDL_SetError ("No dynamic GL support in video driver");
slouken@1662
  1408
        }
slouken@1662
  1409
    }
slouken@1662
  1410
    return (retval);
slouken@0
  1411
}
slouken@0
  1412
slouken@1662
  1413
void *
slouken@1662
  1414
SDL_GL_GetProcAddress (const char *proc)
slouken@0
  1415
{
slouken@1662
  1416
    void *func;
slouken@0
  1417
slouken@1662
  1418
    func = NULL;
slouken@1662
  1419
    if (_this->GL_GetProcAddress) {
slouken@1662
  1420
        if (_this->gl_config.driver_loaded) {
slouken@1662
  1421
            func = _this->GL_GetProcAddress (_this, proc);
slouken@1662
  1422
        } else {
slouken@1662
  1423
            SDL_SetError ("No GL driver has been loaded");
slouken@1662
  1424
        }
slouken@1662
  1425
    } else {
slouken@1662
  1426
        SDL_SetError ("No dynamic GL support in video driver");
slouken@1662
  1427
    }
slouken@1662
  1428
    return func;
slouken@0
  1429
}
slouken@0
  1430
slouken@0
  1431
/* Set the specified GL attribute for setting up a GL video mode */
slouken@1662
  1432
int
slouken@1662
  1433
SDL_GL_SetAttribute (SDL_GLattr attr, int value)
slouken@0
  1434
{
slouken@1662
  1435
    int retval;
slouken@0
  1436
slouken@1662
  1437
    retval = 0;
slouken@1662
  1438
    switch (attr) {
slouken@1662
  1439
    case SDL_GL_RED_SIZE:
slouken@1662
  1440
        _this->gl_config.red_size = value;
slouken@1662
  1441
        break;
slouken@1662
  1442
    case SDL_GL_GREEN_SIZE:
slouken@1662
  1443
        _this->gl_config.green_size = value;
slouken@1662
  1444
        break;
slouken@1662
  1445
    case SDL_GL_BLUE_SIZE:
slouken@1662
  1446
        _this->gl_config.blue_size = value;
slouken@1662
  1447
        break;
slouken@1662
  1448
    case SDL_GL_ALPHA_SIZE:
slouken@1662
  1449
        _this->gl_config.alpha_size = value;
slouken@1662
  1450
        break;
slouken@1662
  1451
    case SDL_GL_DOUBLEBUFFER:
slouken@1662
  1452
        _this->gl_config.double_buffer = value;
slouken@1662
  1453
        break;
slouken@1662
  1454
    case SDL_GL_BUFFER_SIZE:
slouken@1662
  1455
        _this->gl_config.buffer_size = value;
slouken@1662
  1456
        break;
slouken@1662
  1457
    case SDL_GL_DEPTH_SIZE:
slouken@1662
  1458
        _this->gl_config.depth_size = value;
slouken@1662
  1459
        break;
slouken@1662
  1460
    case SDL_GL_STENCIL_SIZE:
slouken@1662
  1461
        _this->gl_config.stencil_size = value;
slouken@1662
  1462
        break;
slouken@1662
  1463
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1662
  1464
        _this->gl_config.accum_red_size = value;
slouken@1662
  1465
        break;
slouken@1662
  1466
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1662
  1467
        _this->gl_config.accum_green_size = value;
slouken@1662
  1468
        break;
slouken@1662
  1469
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1662
  1470
        _this->gl_config.accum_blue_size = value;
slouken@1662
  1471
        break;
slouken@1662
  1472
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1662
  1473
        _this->gl_config.accum_alpha_size = value;
slouken@1662
  1474
        break;
slouken@1662
  1475
    case SDL_GL_STEREO:
slouken@1662
  1476
        _this->gl_config.stereo = value;
slouken@1662
  1477
        break;
slouken@1662
  1478
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1662
  1479
        _this->gl_config.multisamplebuffers = value;
slouken@1662
  1480
        break;
slouken@1662
  1481
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1662
  1482
        _this->gl_config.multisamplesamples = value;
slouken@1662
  1483
        break;
slouken@1662
  1484
    case SDL_GL_ACCELERATED_VISUAL:
slouken@1662
  1485
        _this->gl_config.accelerated = value;
slouken@1662
  1486
        break;
slouken@1662
  1487
    case SDL_GL_SWAP_CONTROL:
slouken@1662
  1488
        _this->gl_config.swap_control = value;
slouken@1662
  1489
        break;
slouken@1662
  1490
    default:
slouken@1662
  1491
        SDL_SetError ("Unknown OpenGL attribute");
slouken@1662
  1492
        retval = -1;
slouken@1662
  1493
        break;
slouken@1662
  1494
    }
slouken@1662
  1495
    return (retval);
slouken@0
  1496
}
slouken@0
  1497
slouken@0
  1498
/* Retrieve an attribute value from the windowing system. */
slouken@1662
  1499
int
slouken@1662
  1500
SDL_GL_GetAttribute (SDL_GLattr attr, int *value)
slouken@0
  1501
{
slouken@1662
  1502
    int retval = -1;
slouken@0
  1503
slouken@1662
  1504
    if (_this->GL_GetAttribute) {
slouken@1662
  1505
        retval = _this->GL_GetAttribute (_this, attr, value);
slouken@1662
  1506
    } else {
slouken@1662
  1507
        *value = 0;
slouken@1662
  1508
        SDL_SetError ("GL_GetAttribute not supported");
slouken@1662
  1509
    }
slouken@1662
  1510
    return retval;
slouken@0
  1511
}
slouken@0
  1512
slouken@0
  1513
/* Perform a GL buffer swap on the current GL context */
slouken@1662
  1514
void
slouken@1662
  1515
SDL_GL_SwapBuffers (void)
slouken@0
  1516
{
slouken@1662
  1517
    if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
slouken@1662
  1518
        _this->GL_SwapBuffers (_this);
slouken@1662
  1519
    } else {
slouken@1662
  1520
        SDL_SetError ("OpenGL video mode has not been set");
slouken@1662
  1521
    }
slouken@0
  1522
}
slouken@0
  1523
slouken@1662
  1524
#if 0                           // FIXME
slouken@541
  1525
/* Utility function used by SDL_WM_SetIcon();
slouken@541
  1526
 * flags & 1 for color key, flags & 2 for alpha channel. */
slouken@1662
  1527
static void
slouken@1662
  1528
CreateMaskFromColorKeyOrAlpha (SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1529
{
slouken@1662
  1530
    int x, y;
slouken@1662
  1531
    Uint32 colorkey;
slouken@0
  1532
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1533
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1534
slouken@1662
  1535
    colorkey = icon->format->colorkey;
slouken@1662
  1536
    switch (icon->format->BytesPerPixel) {
slouken@1662
  1537
    case 1:
slouken@1662
  1538
        {
slouken@1662
  1539
            Uint8 *pixels;
slouken@1662
  1540
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1541
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@1662
  1542
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1543
                    if (*pixels++ == colorkey) {
slouken@1662
  1544
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1545
                    }
slouken@1662
  1546
                }
slouken@1662
  1547
            }
slouken@1662
  1548
        }
slouken@1662
  1549
        break;
slouken@0
  1550
slouken@1662
  1551
    case 2:
slouken@1662
  1552
        {
slouken@1662
  1553
            Uint16 *pixels;
slouken@1662
  1554
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1555
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@1662
  1556
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1557
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1662
  1558
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1559
                    } else if ((flags & 2)
slouken@1662
  1560
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1662
  1561
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1562
                    }
slouken@1662
  1563
                    pixels++;
slouken@1662
  1564
                }
slouken@1662
  1565
            }
slouken@1662
  1566
        }
slouken@1662
  1567
        break;
slouken@0
  1568
slouken@1662
  1569
    case 4:
slouken@1662
  1570
        {
slouken@1662
  1571
            Uint32 *pixels;
slouken@1662
  1572
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1573
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@1662
  1574
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1575
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1662
  1576
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1577
                    } else if ((flags & 2)
slouken@1662
  1578
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1662
  1579
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1580
                    }
slouken@1662
  1581
                    pixels++;
slouken@1662
  1582
                }
slouken@1662
  1583
            }
slouken@1662
  1584
        }
slouken@1662
  1585
        break;
slouken@1662
  1586
    }
slouken@0
  1587
}
slouken@0
  1588
slouken@0
  1589
/*
slouken@0
  1590
 * Sets the window manager icon for the display window.
slouken@0
  1591
 */
slouken@1662
  1592
void
slouken@1662
  1593
SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask)
slouken@0
  1594
{
slouken@1662
  1595
    if (icon && _this->SetIcon) {
slouken@1662
  1596
        /* Generate a mask if necessary, and create the icon! */
slouken@1662
  1597
        if (mask == NULL) {
slouken@1662
  1598
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@1662
  1599
            int flags = 0;
slouken@1662
  1600
            mask = (Uint8 *) SDL_malloc (mask_len);
slouken@1662
  1601
            if (mask == NULL) {
slouken@1662
  1602
                return;
slouken@1662
  1603
            }
slouken@1662
  1604
            SDL_memset (mask, ~0, mask_len);
slouken@1662
  1605
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@1662
  1606
                flags |= 1;
slouken@1662
  1607
            if (icon->flags & SDL_SRCALPHA)
slouken@1662
  1608
                flags |= 2;
slouken@1662
  1609
            if (flags) {
slouken@1662
  1610
                CreateMaskFromColorKeyOrAlpha (icon, mask, flags);
slouken@1662
  1611
            }
slouken@1662
  1612
            _this->SetIcon (_this, icon, mask);
slouken@1662
  1613
            SDL_free (mask);
slouken@1662
  1614
        } else {
slouken@1662
  1615
            _this->SetIcon (_this, icon, mask);
slouken@1662
  1616
        }
slouken@1662
  1617
    }
slouken@1662
  1618
}
slouken@1662
  1619
#endif
slouken@0
  1620
slouken@1662
  1621
SDL_bool
slouken@1662
  1622
SDL_GetWindowWMInfo (SDL_WindowID windowID, SDL_SysWMinfo * info)
slouken@1662
  1623
{
slouken@1662
  1624
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
  1625
slouken@1662
  1626
    if (!window || !_this->GetWindowWMInfo) {
slouken@1662
  1627
        return SDL_FALSE;
slouken@1662
  1628
    }
slouken@1662
  1629
    return (_this->GetWindowWMInfo (_this, window, info));
slouken@0
  1630
}
slouken@0
  1631
slouken@1662
  1632
/* vi: set ts=4 sw=4 expandtab: */