src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 03:53:21 +0000
branchSDL-1.3
changeset 1667 1fddae038bc8
parent 1666 6e7ec5cb83c3
child 1668 4da1ee79c9af
permissions -rw-r--r--
Implemented many compatibility functions
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@1667
   515
    SDL_VideoDisplay *display;
slouken@1662
   516
    SDL_DisplayMode display_mode;
slouken@1667
   517
    int i;
slouken@229
   518
slouken@1662
   519
    if (!_this) {
slouken@1662
   520
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   521
        return -1;
slouken@1662
   522
    }
slouken@0
   523
slouken@1662
   524
    /* Make sure there's an actual display mode to set */
slouken@1662
   525
    if (!mode) {
slouken@1662
   526
        SDL_SetError ("No mode passed to SDL_SetDisplayMode");
slouken@1662
   527
        return -1;
slouken@1662
   528
    }
slouken@1667
   529
    display = &SDL_CurrentDisplay;
slouken@1662
   530
    display_mode = *mode;
slouken@0
   531
slouken@1662
   532
    /* Default to the current mode */
slouken@1662
   533
    if (!display_mode.format) {
slouken@1667
   534
        display_mode.format = display->current_mode.format;
slouken@1662
   535
    }
slouken@1662
   536
    if (!display_mode.w) {
slouken@1667
   537
        display_mode.w = display->current_mode.w;
slouken@1662
   538
    }
slouken@1662
   539
    if (!display_mode.h) {
slouken@1667
   540
        display_mode.h = display->current_mode.h;
slouken@1662
   541
    }
slouken@1662
   542
    if (!display_mode.refresh_rate) {
slouken@1667
   543
        display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@1662
   544
    }
slouken@0
   545
slouken@1662
   546
    /* Get a good video mode, the closest one possible */
slouken@1662
   547
    if (!SDL_GetClosestDisplayMode (&display_mode, &display_mode)) {
slouken@1662
   548
        SDL_SetError ("No video mode large enough for %dx%d",
slouken@1662
   549
                      display_mode.w, display_mode.h);
slouken@1662
   550
        return -1;
slouken@1662
   551
    }
slouken@0
   552
slouken@1662
   553
    /* See if there's anything left to do */
slouken@1662
   554
    if (SDL_memcmp
slouken@1662
   555
        (&display_mode, SDL_GetCurrentDisplayMode (),
slouken@1662
   556
         sizeof (display_mode)) == 0) {
slouken@1662
   557
        return 0;
slouken@1662
   558
    }
slouken@0
   559
slouken@1667
   560
    /* Free any previous window surfaces */
slouken@1667
   561
    for (i = 0; i < display->num_windows; ++i) {
slouken@1667
   562
        SDL_Window *window = &display->windows[i];
slouken@1667
   563
        if (window->shadow) {
slouken@1667
   564
            SDL_FreeSurface (window->shadow);
slouken@1667
   565
            window->shadow = NULL;
slouken@1667
   566
        }
slouken@1667
   567
        if (window->surface) {
slouken@1667
   568
            SDL_FreeSurface (window->surface);
slouken@1667
   569
            window->surface = NULL;
slouken@1667
   570
        }
slouken@1667
   571
    }
slouken@1667
   572
slouken@1662
   573
    return _this->SetDisplayMode (_this, &display_mode);
slouken@1662
   574
}
slouken@0
   575
slouken@1662
   576
SDL_WindowID
slouken@1662
   577
SDL_CreateWindow (const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@1662
   578
{
slouken@1662
   579
    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
slouken@1662
   580
                                  SDL_WINDOW_BORDERLESS |
slouken@1662
   581
                                  SDL_WINDOW_SHOWN |
slouken@1662
   582
                                  SDL_WINDOW_OPENGL |
slouken@1662
   583
                                  SDL_WINDOW_RESIZABLE |
slouken@1662
   584
                                  SDL_WINDOW_MAXIMIZED |
slouken@1662
   585
                                  SDL_WINDOW_MINIMIZED |
slouken@1662
   586
                                  SDL_WINDOW_INPUT_GRABBED);
slouken@1662
   587
    SDL_Window window;
slouken@1662
   588
    int num_windows;
slouken@1662
   589
    SDL_Window *windows;
slouken@0
   590
slouken@1662
   591
    if (!_this) {
slouken@1662
   592
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   593
        return 0;
slouken@1662
   594
    }
slouken@0
   595
slouken@1662
   596
    SDL_zero (window);
slouken@1662
   597
    window.id = _this->next_window_id++;
slouken@1667
   598
    window.title = title ? SDL_strdup (title) : NULL;
slouken@1662
   599
    window.x = x;
slouken@1662
   600
    window.y = y;
slouken@1662
   601
    window.w = w;
slouken@1662
   602
    window.h = h;
slouken@1662
   603
    window.flags = (flags & allowed_flags);
slouken@910
   604
slouken@1662
   605
    if (_this->CreateWindow && _this->CreateWindow (_this, &window) < 0) {
slouken@1667
   606
        if (window.title) {
slouken@1667
   607
            SDL_free (window.title);
slouken@1667
   608
        }
slouken@1662
   609
        return 0;
slouken@1662
   610
    }
slouken@910
   611
slouken@1662
   612
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   613
    windows =
slouken@1662
   614
        SDL_realloc (SDL_CurrentDisplay.windows,
slouken@1662
   615
                     (num_windows + 1) * sizeof (*windows));
slouken@1662
   616
    if (!windows) {
slouken@1662
   617
        if (_this->DestroyWindow) {
slouken@1662
   618
            _this->DestroyWindow (_this, &window);
slouken@1662
   619
        }
slouken@1667
   620
        if (window.title) {
slouken@1667
   621
            SDL_free (window.title);
slouken@1667
   622
        }
slouken@1662
   623
        return 0;
slouken@1662
   624
    }
slouken@1662
   625
    windows[num_windows] = window;
slouken@1662
   626
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   627
    SDL_CurrentDisplay.num_windows++;
slouken@910
   628
slouken@1662
   629
    return window.id;
slouken@1662
   630
}
icculus@650
   631
slouken@1662
   632
SDL_WindowID
slouken@1662
   633
SDL_CreateWindowFrom (void *data)
slouken@1662
   634
{
slouken@1662
   635
    SDL_Window window;
slouken@1662
   636
    int num_windows;
slouken@1662
   637
    SDL_Window *windows;
slouken@0
   638
slouken@1662
   639
    if (!_this) {
slouken@1662
   640
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
   641
        return (0);
slouken@1662
   642
    }
slouken@0
   643
slouken@1662
   644
    SDL_zero (window);
slouken@1662
   645
    window.id = _this->next_window_id++;
slouken@1662
   646
slouken@1662
   647
    if (!_this->CreateWindowFrom ||
slouken@1662
   648
        _this->CreateWindowFrom (_this, &window, data) < 0) {
slouken@1662
   649
        return 0;
slouken@1662
   650
    }
slouken@1662
   651
slouken@1662
   652
    num_windows = SDL_CurrentDisplay.num_windows;
slouken@1662
   653
    windows =
slouken@1662
   654
        SDL_realloc (SDL_CurrentDisplay.windows,
slouken@1662
   655
                     (num_windows + 1) * sizeof (*windows));
slouken@1662
   656
    if (!windows) {
slouken@1662
   657
        if (_this->DestroyWindow) {
slouken@1662
   658
            _this->DestroyWindow (_this, &window);
slouken@1662
   659
        }
slouken@1667
   660
        if (window.title) {
slouken@1667
   661
            SDL_free (window.title);
slouken@1667
   662
        }
slouken@1662
   663
        return 0;
slouken@1662
   664
    }
slouken@1662
   665
    windows[num_windows] = window;
slouken@1662
   666
    SDL_CurrentDisplay.windows = windows;
slouken@1662
   667
    SDL_CurrentDisplay.num_windows++;
slouken@1662
   668
slouken@1662
   669
    return window.id;
slouken@1662
   670
}
slouken@1662
   671
slouken@1662
   672
static __inline__ SDL_Window *
slouken@1662
   673
SDL_GetWindowFromID (SDL_WindowID windowID)
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 (window->id == windowID) {
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
SDL_Window *
slouken@1662
   694
SDL_GetWindowFromSurface (SDL_Surface * surface)
slouken@1662
   695
{
slouken@1662
   696
    int i, j;
slouken@1662
   697
slouken@1667
   698
    if (!_this || !surface) {
slouken@1662
   699
        return NULL;
slouken@1662
   700
    }
slouken@1662
   701
slouken@1662
   702
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   703
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   704
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   705
            SDL_Window *window = &display->windows[j];
slouken@1662
   706
            if (surface == window->surface || surface == window->shadow) {
slouken@1662
   707
                return window;
slouken@1662
   708
            }
slouken@1662
   709
        }
slouken@1662
   710
    }
slouken@1662
   711
    return NULL;
slouken@1662
   712
}
slouken@1662
   713
slouken@1662
   714
slouken@1662
   715
Uint32
slouken@1662
   716
SDL_GetWindowFlags (SDL_WindowID windowID)
slouken@1662
   717
{
slouken@1662
   718
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   719
slouken@1662
   720
    if (!window) {
slouken@1662
   721
        return 0;
slouken@1662
   722
    }
slouken@1662
   723
    return window->flags;
slouken@1662
   724
}
slouken@1662
   725
slouken@1662
   726
void
slouken@1662
   727
SDL_SetWindowTitle (SDL_WindowID windowID, const char *title)
slouken@1662
   728
{
slouken@1662
   729
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   730
slouken@1662
   731
    if (!window) {
slouken@1662
   732
        return;
slouken@1662
   733
    }
slouken@1662
   734
    if (window->title) {
slouken@1662
   735
        SDL_free (window->title);
slouken@1662
   736
    }
slouken@1662
   737
    window->title = SDL_strdup (title);
slouken@1662
   738
slouken@1662
   739
    if (_this->SetWindowTitle) {
slouken@1662
   740
        _this->SetWindowTitle (_this, window);
slouken@1662
   741
    }
slouken@1662
   742
}
slouken@1662
   743
slouken@1662
   744
const char *
slouken@1662
   745
SDL_GetWindowTitle (SDL_WindowID windowID)
slouken@1662
   746
{
slouken@1662
   747
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   748
slouken@1662
   749
    if (!window) {
slouken@1662
   750
        return NULL;
slouken@1662
   751
    }
slouken@1662
   752
    return window->title;
slouken@1662
   753
}
slouken@1662
   754
slouken@1662
   755
void
slouken@1662
   756
SDL_SetWindowData (SDL_WindowID windowID, void *userdata)
slouken@1662
   757
{
slouken@1662
   758
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   759
slouken@1662
   760
    if (!window) {
slouken@1662
   761
        return;
slouken@1662
   762
    }
slouken@1662
   763
    window->userdata = userdata;
slouken@1662
   764
}
slouken@1662
   765
slouken@1662
   766
void *
slouken@1662
   767
SDL_GetWindowData (SDL_WindowID windowID)
slouken@1662
   768
{
slouken@1662
   769
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   770
slouken@1662
   771
    if (!window) {
slouken@1662
   772
        return NULL;
slouken@1662
   773
    }
slouken@1662
   774
    return window->userdata;
slouken@1662
   775
}
slouken@1662
   776
slouken@1662
   777
void
slouken@1662
   778
SDL_SetWindowPosition (SDL_WindowID windowID, int x, int y)
slouken@1662
   779
{
slouken@1662
   780
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   781
slouken@1662
   782
    if (!window) {
slouken@1662
   783
        return;
slouken@1662
   784
    }
slouken@1662
   785
slouken@1662
   786
    window->x = x;
slouken@1662
   787
    window->y = y;
slouken@1662
   788
slouken@1662
   789
    if (_this->SetWindowPosition) {
slouken@1662
   790
        _this->SetWindowPosition (_this, window);
slouken@1662
   791
    }
slouken@1662
   792
}
slouken@1662
   793
slouken@1662
   794
void
slouken@1662
   795
SDL_GetWindowPosition (SDL_WindowID windowID, int *x, int *y)
slouken@1662
   796
{
slouken@1662
   797
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   798
slouken@1662
   799
    if (!window) {
slouken@1662
   800
        return;
slouken@1662
   801
    }
slouken@1662
   802
    if (x) {
slouken@1662
   803
        *x = window->x;
slouken@1662
   804
    }
slouken@1662
   805
    if (y) {
slouken@1662
   806
        *y = window->y;
slouken@1662
   807
    }
slouken@1662
   808
}
slouken@1662
   809
slouken@1662
   810
void
slouken@1662
   811
SDL_SetWindowSize (SDL_WindowID windowID, int w, int h)
slouken@1662
   812
{
slouken@1662
   813
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   814
slouken@1662
   815
    if (!window) {
slouken@1662
   816
        return;
slouken@1662
   817
    }
slouken@1662
   818
slouken@1662
   819
    window->w = w;
slouken@1662
   820
    window->h = h;
slouken@1662
   821
slouken@1662
   822
    if (_this->SetWindowSize) {
slouken@1662
   823
        _this->SetWindowSize (_this, window);
slouken@1662
   824
    }
slouken@1662
   825
}
slouken@1662
   826
slouken@1662
   827
void
slouken@1662
   828
SDL_GetWindowSize (SDL_WindowID windowID, int *w, int *h)
slouken@1662
   829
{
slouken@1662
   830
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   831
slouken@1662
   832
    if (!window) {
slouken@1662
   833
        return;
slouken@1662
   834
    }
slouken@1662
   835
    if (w) {
slouken@1662
   836
        *w = window->w;
slouken@1662
   837
    }
slouken@1662
   838
    if (h) {
slouken@1662
   839
        *h = window->h;
slouken@1662
   840
    }
slouken@1662
   841
}
slouken@1662
   842
slouken@1662
   843
void
slouken@1662
   844
SDL_ShowWindow (SDL_WindowID windowID)
slouken@1662
   845
{
slouken@1662
   846
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   847
slouken@1662
   848
    if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   849
        return;
slouken@1662
   850
    }
slouken@1662
   851
slouken@1662
   852
    window->flags |= SDL_WINDOW_SHOWN;
slouken@1662
   853
slouken@1662
   854
    if (_this->ShowWindow) {
slouken@1662
   855
        _this->ShowWindow (_this, window);
slouken@1662
   856
    }
slouken@1662
   857
}
slouken@1662
   858
slouken@1662
   859
void
slouken@1662
   860
SDL_HideWindow (SDL_WindowID windowID)
slouken@1662
   861
{
slouken@1662
   862
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   863
slouken@1662
   864
    if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
slouken@1662
   865
        return;
slouken@1662
   866
    }
slouken@1662
   867
slouken@1662
   868
    window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1662
   869
slouken@1662
   870
    if (_this->HideWindow) {
slouken@1662
   871
        _this->HideWindow (_this, window);
slouken@1662
   872
    }
slouken@1662
   873
}
slouken@1662
   874
slouken@1662
   875
void
slouken@1662
   876
SDL_RaiseWindow (SDL_WindowID windowID)
slouken@1662
   877
{
slouken@1662
   878
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   879
slouken@1662
   880
    if (!window) {
slouken@1662
   881
        return;
slouken@1662
   882
    }
slouken@1662
   883
slouken@1662
   884
    if (_this->RaiseWindow) {
slouken@1662
   885
        _this->RaiseWindow (_this, window);
slouken@1662
   886
    }
slouken@1662
   887
}
slouken@1662
   888
slouken@1662
   889
void
slouken@1662
   890
SDL_MaximizeWindow (SDL_WindowID windowID)
slouken@1662
   891
{
slouken@1662
   892
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   893
slouken@1662
   894
    if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
slouken@1662
   895
        return;
slouken@1662
   896
    }
slouken@1662
   897
slouken@1662
   898
    window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1662
   899
slouken@1662
   900
    if (_this->MaximizeWindow) {
slouken@1662
   901
        _this->MaximizeWindow (_this, window);
slouken@1662
   902
    }
slouken@1662
   903
}
slouken@1662
   904
slouken@1662
   905
void
slouken@1662
   906
SDL_MinimizeWindow (SDL_WindowID windowID)
slouken@1662
   907
{
slouken@1662
   908
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   909
slouken@1662
   910
    if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
slouken@1662
   911
        return;
slouken@1662
   912
    }
slouken@1662
   913
slouken@1662
   914
    window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1662
   915
slouken@1662
   916
    if (_this->MinimizeWindow) {
slouken@1662
   917
        _this->MinimizeWindow (_this, window);
slouken@1662
   918
    }
slouken@1662
   919
}
slouken@1662
   920
slouken@1662
   921
void
slouken@1662
   922
SDL_RestoreWindow (SDL_WindowID windowID)
slouken@1662
   923
{
slouken@1662
   924
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   925
slouken@1662
   926
    if (!window
slouken@1662
   927
        || (window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@1662
   928
        return;
slouken@1662
   929
    }
slouken@1662
   930
slouken@1662
   931
    window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
slouken@1662
   932
slouken@1662
   933
    if (_this->RestoreWindow) {
slouken@1662
   934
        _this->RestoreWindow (_this, window);
slouken@1662
   935
    }
slouken@1662
   936
}
slouken@1662
   937
slouken@1662
   938
void
slouken@1662
   939
SDL_SetWindowGrab (SDL_WindowID windowID, int mode)
slouken@1662
   940
{
slouken@1662
   941
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   942
slouken@1662
   943
    if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
slouken@1662
   944
        return;
slouken@1662
   945
    }
slouken@1662
   946
slouken@1662
   947
    if (mode) {
slouken@1662
   948
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   949
    } else {
slouken@1662
   950
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   951
    }
slouken@1662
   952
slouken@1662
   953
    if (_this->SetWindowGrab) {
slouken@1662
   954
        _this->SetWindowGrab (_this, window);
slouken@1662
   955
    }
slouken@1662
   956
}
slouken@1662
   957
slouken@1662
   958
int
slouken@1662
   959
SDL_GetWindowGrab (SDL_WindowID windowID)
slouken@1662
   960
{
slouken@1662
   961
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
   962
slouken@1662
   963
    if (!window) {
slouken@1662
   964
        return 0;
slouken@1662
   965
    }
slouken@1662
   966
slouken@1662
   967
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1662
   968
}
slouken@1662
   969
slouken@1662
   970
void
slouken@1662
   971
SDL_DestroyWindow (SDL_WindowID windowID)
slouken@1662
   972
{
slouken@1662
   973
    int i, j;
slouken@1662
   974
slouken@1662
   975
    if (!_this) {
slouken@1662
   976
        return;
slouken@1662
   977
    }
slouken@1662
   978
slouken@1662
   979
    for (i = 0; i < _this->num_displays; ++i) {
slouken@1662
   980
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
   981
        for (j = 0; j < display->num_windows; ++j) {
slouken@1662
   982
            SDL_Window *window = &display->windows[j];
slouken@1662
   983
            if (window->id != windowID) {
slouken@1662
   984
                continue;
slouken@1662
   985
            }
slouken@1662
   986
            if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1662
   987
                window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@1662
   988
                _this->SetWindowGrab (_this, window);
slouken@1662
   989
            }
slouken@1662
   990
            if (window->shadow) {
slouken@1662
   991
                SDL_FreeSurface (window->shadow);
slouken@1662
   992
            }
slouken@1662
   993
            if (window->surface) {
slouken@1662
   994
                SDL_FreeSurface (window->surface);
slouken@1662
   995
            }
slouken@1662
   996
            if (_this->DestroyWindow) {
slouken@1662
   997
                _this->DestroyWindow (_this, window);
slouken@1662
   998
            }
slouken@1662
   999
            if (window->title) {
slouken@1662
  1000
                SDL_free (window->title);
slouken@1662
  1001
            }
slouken@1662
  1002
            if (window->gamma) {
slouken@1662
  1003
                SDL_free (window->gamma);
slouken@1662
  1004
            }
slouken@1662
  1005
            if (j != display->num_windows - 1) {
slouken@1662
  1006
                SDL_memcpy (&display->windows[i],
slouken@1662
  1007
                            &display->windows[i + 1],
slouken@1662
  1008
                            (display->num_windows - i -
slouken@1662
  1009
                             1) * sizeof (*window));
slouken@1662
  1010
            }
slouken@1662
  1011
            --display->num_windows;
slouken@1662
  1012
            return;
slouken@1662
  1013
        }
slouken@1662
  1014
    }
slouken@1662
  1015
}
slouken@1662
  1016
slouken@1662
  1017
SDL_Surface *
slouken@1662
  1018
SDL_CreateWindowSurface (SDL_WindowID windowID, Uint32 format, Uint32 flags)
slouken@1662
  1019
{
slouken@1662
  1020
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1666
  1021
    Uint32 black;
slouken@1662
  1022
    SDL_Surface *surface;
slouken@1662
  1023
slouken@1662
  1024
    if (!window) {
slouken@1662
  1025
        return NULL;
slouken@1662
  1026
    }
slouken@1662
  1027
slouken@1662
  1028
    if (!_this->CreateWindowSurface) {
slouken@1662
  1029
        return NULL;
slouken@1662
  1030
    }
slouken@1662
  1031
slouken@1662
  1032
    if (!window->surface) {
slouken@1666
  1033
        _this->CreateWindowSurface (_this, window, flags);
slouken@1662
  1034
        if (!window->surface) {
slouken@1662
  1035
            return NULL;
slouken@1662
  1036
        }
slouken@1666
  1037
        window->surface->flags |= SDL_SCREEN_SURFACE;
slouken@1662
  1038
    }
slouken@1666
  1039
    surface = window->surface;
slouken@1662
  1040
slouken@1662
  1041
    if (window->shadow) {
slouken@1662
  1042
        SDL_FreeSurface (window->shadow);
slouken@1662
  1043
        window->shadow = NULL;
slouken@1662
  1044
    }
slouken@1662
  1045
slouken@1662
  1046
    /* Create a shadow surface if necessary */
slouken@1666
  1047
    if ((!(flags & SDL_ANYFORMAT)
slouken@1666
  1048
         && (format != SDL_GetCurrentDisplayMode ()->format))
slouken@1666
  1049
        || ((flags & SDL_HWPALETTE)
slouken@1666
  1050
            && !(window->surface->flags & SDL_HWPALETTE))) {
slouken@1662
  1051
        int bpp;
slouken@1662
  1052
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1662
  1053
slouken@1666
  1054
        SDL_PixelFormatEnumToMasks (format, &bpp, &Amask, &Gmask, &Bmask,
slouken@1662
  1055
                                    &Amask);
slouken@1666
  1056
        window->shadow =
slouken@1666
  1057
            SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, bpp,
slouken@1666
  1058
                                  Rmask, Gmask, Bmask, Amask);
slouken@1666
  1059
        if (window->shadow == NULL) {
slouken@1662
  1060
            return NULL;
slouken@1662
  1061
        }
slouken@1666
  1062
        window->shadow->flags |= SDL_SHADOW_SURFACE;
slouken@1666
  1063
        surface = window->shadow;
slouken@1662
  1064
slouken@1662
  1065
        /* 8-bit shadow surfaces report that they have exclusive palette */
slouken@1666
  1066
        if (surface->format->palette) {
slouken@1666
  1067
            surface->flags |= SDL_HWPALETTE;
slouken@1666
  1068
            if (format == SDL_GetCurrentDisplayMode ()->format) {
slouken@1666
  1069
                SDL_memcpy (surface->format->palette->colors,
slouken@1666
  1070
                            window->surface->format->palette->colors,
slouken@1666
  1071
                            window->surface->format->palette->ncolors *
slouken@1662
  1072
                            sizeof (SDL_Color));
slouken@1662
  1073
            } else {
slouken@1666
  1074
                SDL_DitherColors (surface->format->palette->colors, bpp);
slouken@1662
  1075
            }
slouken@1662
  1076
        }
slouken@1662
  1077
    }
slouken@1662
  1078
slouken@1662
  1079
    /* Clear the surface for display */
slouken@1662
  1080
    {
slouken@1662
  1081
        Uint32 black = SDL_MapRGB (surface->format, 0, 0, 0);
slouken@1662
  1082
        SDL_FillRect (surface, NULL, black);
slouken@1662
  1083
        if (surface->flags & SDL_DOUBLEBUF) {
slouken@1662
  1084
            SDL_Flip (surface);
slouken@1662
  1085
            SDL_FillRect (surface, NULL, black);
slouken@1662
  1086
        }
slouken@1662
  1087
        SDL_Flip (surface);
slouken@1662
  1088
    }
slouken@1662
  1089
slouken@1662
  1090
    return surface;
slouken@0
  1091
}
slouken@0
  1092
slouken@0
  1093
/* 
slouken@0
  1094
 * Convert a surface into the video pixel format.
slouken@0
  1095
 */
slouken@1662
  1096
SDL_Surface *
slouken@1662
  1097
SDL_DisplayFormat (SDL_Surface * surface)
slouken@0
  1098
{
slouken@1662
  1099
    Uint32 flags;
slouken@0
  1100
slouken@1662
  1101
    if (!SDL_PublicSurface) {
slouken@1662
  1102
        SDL_SetError ("No video mode has been set");
slouken@1662
  1103
        return (NULL);
slouken@1662
  1104
    }
slouken@1662
  1105
    /* Set the flags appropriate for copying to display surface */
slouken@1662
  1106
    if (((SDL_PublicSurface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
slouken@1662
  1107
        && _this->info.blit_hw)
slouken@1662
  1108
        flags = SDL_HWSURFACE;
slouken@1662
  1109
    else
slouken@1662
  1110
        flags = SDL_SWSURFACE;
slouken@0
  1111
#ifdef AUTORLE_DISPLAYFORMAT
slouken@1662
  1112
    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
slouken@1662
  1113
    flags |= SDL_RLEACCELOK;
slouken@0
  1114
#else
slouken@1662
  1115
    flags |=
slouken@1662
  1116
        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@0
  1117
#endif
slouken@1662
  1118
    return (SDL_ConvertSurface (surface, SDL_PublicSurface->format, flags));
slouken@0
  1119
}
slouken@0
  1120
slouken@0
  1121
/*
slouken@0
  1122
 * Convert a surface into a format that's suitable for blitting to
slouken@0
  1123
 * the screen, but including an alpha channel.
slouken@0
  1124
 */
slouken@1662
  1125
SDL_Surface *
slouken@1662
  1126
SDL_DisplayFormatAlpha (SDL_Surface * surface)
slouken@0
  1127
{
slouken@1662
  1128
    SDL_PixelFormat *vf;
slouken@1662
  1129
    SDL_PixelFormat *format;
slouken@1662
  1130
    SDL_Surface *converted;
slouken@1662
  1131
    Uint32 flags;
slouken@1662
  1132
    /* default to ARGB8888 */
slouken@1662
  1133
    Uint32 amask = 0xff000000;
slouken@1662
  1134
    Uint32 rmask = 0x00ff0000;
slouken@1662
  1135
    Uint32 gmask = 0x0000ff00;
slouken@1662
  1136
    Uint32 bmask = 0x000000ff;
slouken@0
  1137
slouken@1662
  1138
    if (!SDL_PublicSurface) {
slouken@1662
  1139
        SDL_SetError ("No video mode has been set");
slouken@1662
  1140
        return (NULL);
slouken@1662
  1141
    }
slouken@1662
  1142
    vf = SDL_PublicSurface->format;
slouken@0
  1143
slouken@1662
  1144
    switch (vf->BytesPerPixel) {
slouken@1662
  1145
    case 2:
slouken@1662
  1146
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1662
  1147
           For anything else (like ARGB4444) it doesn't matter
slouken@1662
  1148
           since we have no special code for it anyway */
slouken@1662
  1149
        if ((vf->Rmask == 0x1f) &&
slouken@1662
  1150
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1662
  1151
            rmask = 0xff;
slouken@1662
  1152
            bmask = 0xff0000;
slouken@1662
  1153
        }
slouken@1662
  1154
        break;
slouken@0
  1155
slouken@1662
  1156
    case 3:
slouken@1662
  1157
    case 4:
slouken@1662
  1158
        /* Keep the video format, as long as the high 8 bits are
slouken@1662
  1159
           unused or alpha */
slouken@1662
  1160
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1662
  1161
            rmask = 0xff;
slouken@1662
  1162
            bmask = 0xff0000;
slouken@1662
  1163
        }
slouken@1662
  1164
        break;
slouken@0
  1165
slouken@1662
  1166
    default:
slouken@1662
  1167
        /* We have no other optimised formats right now. When/if a new
slouken@1662
  1168
           optimised alpha format is written, add the converter here */
slouken@1662
  1169
        break;
slouken@1662
  1170
    }
slouken@1662
  1171
    format = SDL_AllocFormat (32, rmask, gmask, bmask, amask);
slouken@1662
  1172
    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
slouken@1662
  1173
    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1662
  1174
    converted = SDL_ConvertSurface (surface, format, flags);
slouken@1662
  1175
    SDL_FreeFormat (format);
slouken@1662
  1176
    return (converted);
slouken@0
  1177
}
slouken@0
  1178
slouken@0
  1179
/*
slouken@0
  1180
 * Update a specific portion of the physical screen
slouken@0
  1181
 */
slouken@1662
  1182
void
slouken@1662
  1183
SDL_UpdateRect (SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@0
  1184
{
slouken@1662
  1185
    if (screen) {
slouken@1662
  1186
        SDL_Rect rect;
slouken@0
  1187
slouken@1662
  1188
        /* Perform some checking */
slouken@1662
  1189
        if (w == 0)
slouken@1662
  1190
            w = screen->w;
slouken@1662
  1191
        if (h == 0)
slouken@1662
  1192
            h = screen->h;
slouken@1662
  1193
        if ((int) (x + w) > screen->w)
slouken@1662
  1194
            return;
slouken@1662
  1195
        if ((int) (y + h) > screen->h)
slouken@1662
  1196
            return;
slouken@0
  1197
slouken@1662
  1198
        /* Fill the rectangle */
slouken@1662
  1199
        rect.x = (Sint16) x;
slouken@1662
  1200
        rect.y = (Sint16) y;
slouken@1662
  1201
        rect.w = (Uint16) w;
slouken@1662
  1202
        rect.h = (Uint16) h;
slouken@1662
  1203
        SDL_UpdateRects (screen, 1, &rect);
slouken@1662
  1204
    }
slouken@0
  1205
}
slouken@1662
  1206
void
slouken@1662
  1207
SDL_UpdateRects (SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@0
  1208
{
slouken@1662
  1209
    int i;
slouken@1662
  1210
    SDL_Window *window;
slouken@0
  1211
slouken@1662
  1212
    /* Find the window corresponding to this surface */
slouken@1662
  1213
    window = SDL_GetWindowFromSurface (screen);
slouken@1662
  1214
    if (!window) {
slouken@1662
  1215
        SDL_SetError ("Couldn't find window associated with surface");
slouken@1662
  1216
        return;
slouken@1662
  1217
    }
slouken@0
  1218
slouken@1662
  1219
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1220
        if (SHOULD_DRAWCURSOR (SDL_cursorstate)) {
slouken@1662
  1221
            SDL_LockCursor ();
slouken@1662
  1222
            SDL_DrawCursor (screen);
slouken@1662
  1223
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1224
                SDL_LowerBlit (screen, &rects[i], window->surface, &rects[i]);
slouken@1662
  1225
            }
slouken@1662
  1226
            SDL_EraseCursor (screen);
slouken@1662
  1227
            SDL_UnlockCursor ();
slouken@1662
  1228
        } else {
slouken@1662
  1229
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1230
                SDL_LowerBlit (screen, &rects[i], window->surface, &rects[i]);
slouken@1662
  1231
            }
slouken@1662
  1232
        }
slouken@1662
  1233
slouken@1662
  1234
        /* Fall through to video surface update */
slouken@1662
  1235
        screen = window->surface;
slouken@1662
  1236
    }
slouken@1662
  1237
    if ((screen->flags & SDL_SCREEN_SURFACE) && _this->UpdateWindowSurface) {
slouken@1662
  1238
        /* Update the video surface */
slouken@1662
  1239
        if (screen->offset) {
slouken@1662
  1240
            int offset_y = screen->offset / screen->pitch;
slouken@1662
  1241
            int offset_x = screen->offset % screen->pitch;
slouken@1662
  1242
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1243
                rects[i].x += offset_x;
slouken@1662
  1244
                rects[i].y += offset_y;
slouken@1662
  1245
            }
slouken@1662
  1246
            _this->UpdateWindowSurface (_this, window, numrects, rects);
slouken@1662
  1247
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1248
                rects[i].x -= offset_x;
slouken@1662
  1249
                rects[i].y -= offset_y;
slouken@1662
  1250
            }
slouken@1662
  1251
        } else {
slouken@1662
  1252
            _this->UpdateWindowSurface (_this, window, numrects, rects);
slouken@1662
  1253
        }
slouken@1662
  1254
    }
slouken@0
  1255
}
slouken@0
  1256
slouken@0
  1257
/*
slouken@0
  1258
 * Performs hardware double buffering, if possible, or a full update if not.
slouken@0
  1259
 */
slouken@1662
  1260
int
slouken@1662
  1261
SDL_Flip (SDL_Surface * screen)
slouken@0
  1262
{
slouken@1662
  1263
    SDL_Window *window;
slouken@0
  1264
slouken@1662
  1265
    /* Find the window corresponding to this surface */
slouken@1662
  1266
    window = SDL_GetWindowFromSurface (screen);
slouken@1662
  1267
    if (!window) {
slouken@1662
  1268
        SDL_SetError ("Couldn't find window associated with surface");
slouken@1662
  1269
        return;
slouken@1662
  1270
    }
slouken@0
  1271
slouken@1662
  1272
    /* Copy the shadow surface to the video surface */
slouken@1662
  1273
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1274
        SDL_Rect rect;
slouken@1662
  1275
slouken@1662
  1276
        rect.x = 0;
slouken@1662
  1277
        rect.y = 0;
slouken@1662
  1278
        rect.w = screen->w;
slouken@1662
  1279
        rect.h = screen->h;
slouken@1662
  1280
        if (SHOULD_DRAWCURSOR (SDL_cursorstate)) {
slouken@1662
  1281
            SDL_LockCursor ();
slouken@1662
  1282
            SDL_DrawCursor (screen);
slouken@1662
  1283
            SDL_LowerBlit (screen, &rect, window->surface, &rect);
slouken@1662
  1284
            SDL_EraseCursor (screen);
slouken@1662
  1285
            SDL_UnlockCursor ();
slouken@1662
  1286
        } else {
slouken@1662
  1287
            SDL_LowerBlit (screen, &rect, window->surface, &rect);
slouken@1662
  1288
        }
slouken@1662
  1289
slouken@1662
  1290
        /* Fall through to video surface update */
slouken@1662
  1291
        screen = window->surface;
slouken@1662
  1292
    }
slouken@1662
  1293
    if (screen->flags & SDL_DOUBLEBUF) {
slouken@1662
  1294
        _this->FlipWindowSurface (_this, window);
slouken@1662
  1295
    } else {
slouken@1662
  1296
        SDL_UpdateRect (screen, 0, 0, 0, 0);
slouken@1662
  1297
    }
slouken@1662
  1298
    return (0);
slouken@0
  1299
}
slouken@0
  1300
slouken@1662
  1301
int
slouken@1662
  1302
SDL_SetColors (SDL_Surface * screen, SDL_Color * colors, int firstcolor,
slouken@1662
  1303
               int ncolors)
slouken@0
  1304
{
slouken@1662
  1305
    SDL_Window *window = NULL;
slouken@1662
  1306
    SDL_Palette *pal;
slouken@1662
  1307
    int gotall;
slouken@1662
  1308
    int palsize;
slouken@0
  1309
slouken@1662
  1310
    /* Verify the parameters */
slouken@1662
  1311
    pal = screen->format->palette;
slouken@1662
  1312
    if (!pal) {
slouken@1662
  1313
        return 0;               /* not a palettized surface */
slouken@1662
  1314
    }
slouken@1662
  1315
    gotall = 1;
slouken@1662
  1316
    palsize = 1 << screen->format->BitsPerPixel;
slouken@1662
  1317
    if (ncolors > (palsize - firstcolor)) {
slouken@1662
  1318
        ncolors = (palsize - firstcolor);
slouken@1662
  1319
        gotall = 0;
slouken@1662
  1320
    }
slouken@0
  1321
slouken@1662
  1322
    if (colors != (pal->colors + firstcolor)) {
slouken@1662
  1323
        SDL_memcpy (pal->colors + firstcolor, colors,
slouken@1662
  1324
                    ncolors * sizeof (*colors));
slouken@1662
  1325
    }
slouken@1662
  1326
    SDL_FormatChanged (screen);
slouken@1662
  1327
slouken@1662
  1328
    if (screen->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
slouken@1662
  1329
        window = SDL_GetWindowFromSurface (screen);
slouken@1662
  1330
        if (!window) {
slouken@1662
  1331
            return 0;
slouken@1662
  1332
        }
slouken@1662
  1333
    }
slouken@1662
  1334
slouken@1662
  1335
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1336
        SDL_Palette *vidpal;
slouken@1662
  1337
slouken@1662
  1338
        vidpal = window->surface->format->palette;
slouken@1662
  1339
        if (vidpal && vidpal->ncolors == pal->ncolors) {
slouken@1662
  1340
            /* This is a shadow surface, and the physical
slouken@1662
  1341
             * framebuffer is also indexed. Propagate the
slouken@1662
  1342
             * changes to its logical palette so that
slouken@1662
  1343
             * updates are always identity blits
slouken@1662
  1344
             */
slouken@1662
  1345
            SDL_memcpy (vidpal->colors + firstcolor, colors,
slouken@1662
  1346
                        ncolors * sizeof (*colors));
slouken@1662
  1347
        }
slouken@1662
  1348
        if (window->surface->flags & SDL_HWPALETTE) {
slouken@1662
  1349
            /* Set the physical palette */
slouken@1662
  1350
            screen = window->surface;
slouken@1662
  1351
        } else {
slouken@1662
  1352
            SDL_UpdateRect (screen, 0, 0, 0, 0);
slouken@1662
  1353
        }
slouken@1662
  1354
    }
slouken@1662
  1355
slouken@1662
  1356
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1662
  1357
        if (_this->SetWindowColors) {
slouken@1662
  1358
            gotall =
slouken@1662
  1359
                _this->SetWindowColors (_this, window, firstcolor, ncolors,
slouken@1662
  1360
                                        colors);
slouken@1662
  1361
            if (!gotall) {
slouken@1662
  1362
                /* The video flags shouldn't have SDL_HWPALETTE, and
slouken@1662
  1363
                   the video driver is responsible for copying back the
slouken@1662
  1364
                   correct colors into the video surface palette.
slouken@1662
  1365
                 */
slouken@1662
  1366
                ;
slouken@1662
  1367
            }
slouken@1662
  1368
        }
slouken@1662
  1369
        SDL_CursorPaletteChanged ();
slouken@1662
  1370
    }
slouken@1662
  1371
slouken@1662
  1372
    return gotall;
slouken@0
  1373
}
slouken@0
  1374
slouken@1662
  1375
void
slouken@1662
  1376
SDL_VideoQuit (void)
slouken@0
  1377
{
slouken@1662
  1378
    int i, j;
slouken@0
  1379
slouken@1662
  1380
    if (!_this) {
slouken@1662
  1381
        return;
slouken@1662
  1382
    }
slouken@0
  1383
slouken@1662
  1384
    /* Halt event processing before doing anything else */
slouken@1662
  1385
    SDL_StopEventLoop ();
slouken@0
  1386
slouken@1662
  1387
    /* Clean up allocated window manager items */
slouken@1662
  1388
    SDL_CursorQuit ();
slouken@0
  1389
slouken@1662
  1390
    /* Clean up the system video */
slouken@1662
  1391
    for (i = _this->num_displays; i--;) {
slouken@1662
  1392
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1393
        for (j = display->num_windows; j--;) {
slouken@1662
  1394
            SDL_DestroyWindow (display->windows[i].id);
slouken@1662
  1395
        }
slouken@1662
  1396
        if (display->windows) {
slouken@1662
  1397
            SDL_free (display->windows);
slouken@1662
  1398
            display->windows = NULL;
slouken@1662
  1399
        }
slouken@1662
  1400
    }
slouken@1662
  1401
    _this->VideoQuit (_this);
slouken@1662
  1402
    if (_this->displays) {
slouken@1662
  1403
        SDL_free (_this->displays);
slouken@1662
  1404
    }
slouken@1662
  1405
    _this->free (_this);
slouken@1662
  1406
    _this = NULL;
slouken@0
  1407
}
slouken@0
  1408
slouken@0
  1409
/* Load the GL driver library */
slouken@1662
  1410
int
slouken@1662
  1411
SDL_GL_LoadLibrary (const char *path)
slouken@0
  1412
{
slouken@1662
  1413
    int retval;
slouken@0
  1414
slouken@1662
  1415
    retval = -1;
slouken@1662
  1416
    if (_this == NULL) {
slouken@1662
  1417
        SDL_SetError ("Video subsystem has not been initialized");
slouken@1662
  1418
    } else {
slouken@1662
  1419
        if (_this->GL_LoadLibrary) {
slouken@1662
  1420
            retval = _this->GL_LoadLibrary (_this, path);
slouken@1662
  1421
        } else {
slouken@1662
  1422
            SDL_SetError ("No dynamic GL support in video driver");
slouken@1662
  1423
        }
slouken@1662
  1424
    }
slouken@1662
  1425
    return (retval);
slouken@0
  1426
}
slouken@0
  1427
slouken@1662
  1428
void *
slouken@1662
  1429
SDL_GL_GetProcAddress (const char *proc)
slouken@0
  1430
{
slouken@1662
  1431
    void *func;
slouken@0
  1432
slouken@1662
  1433
    func = NULL;
slouken@1662
  1434
    if (_this->GL_GetProcAddress) {
slouken@1662
  1435
        if (_this->gl_config.driver_loaded) {
slouken@1662
  1436
            func = _this->GL_GetProcAddress (_this, proc);
slouken@1662
  1437
        } else {
slouken@1662
  1438
            SDL_SetError ("No GL driver has been loaded");
slouken@1662
  1439
        }
slouken@1662
  1440
    } else {
slouken@1662
  1441
        SDL_SetError ("No dynamic GL support in video driver");
slouken@1662
  1442
    }
slouken@1662
  1443
    return func;
slouken@0
  1444
}
slouken@0
  1445
slouken@0
  1446
/* Set the specified GL attribute for setting up a GL video mode */
slouken@1662
  1447
int
slouken@1662
  1448
SDL_GL_SetAttribute (SDL_GLattr attr, int value)
slouken@0
  1449
{
slouken@1662
  1450
    int retval;
slouken@0
  1451
slouken@1662
  1452
    retval = 0;
slouken@1662
  1453
    switch (attr) {
slouken@1662
  1454
    case SDL_GL_RED_SIZE:
slouken@1662
  1455
        _this->gl_config.red_size = value;
slouken@1662
  1456
        break;
slouken@1662
  1457
    case SDL_GL_GREEN_SIZE:
slouken@1662
  1458
        _this->gl_config.green_size = value;
slouken@1662
  1459
        break;
slouken@1662
  1460
    case SDL_GL_BLUE_SIZE:
slouken@1662
  1461
        _this->gl_config.blue_size = value;
slouken@1662
  1462
        break;
slouken@1662
  1463
    case SDL_GL_ALPHA_SIZE:
slouken@1662
  1464
        _this->gl_config.alpha_size = value;
slouken@1662
  1465
        break;
slouken@1662
  1466
    case SDL_GL_DOUBLEBUFFER:
slouken@1662
  1467
        _this->gl_config.double_buffer = value;
slouken@1662
  1468
        break;
slouken@1662
  1469
    case SDL_GL_BUFFER_SIZE:
slouken@1662
  1470
        _this->gl_config.buffer_size = value;
slouken@1662
  1471
        break;
slouken@1662
  1472
    case SDL_GL_DEPTH_SIZE:
slouken@1662
  1473
        _this->gl_config.depth_size = value;
slouken@1662
  1474
        break;
slouken@1662
  1475
    case SDL_GL_STENCIL_SIZE:
slouken@1662
  1476
        _this->gl_config.stencil_size = value;
slouken@1662
  1477
        break;
slouken@1662
  1478
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1662
  1479
        _this->gl_config.accum_red_size = value;
slouken@1662
  1480
        break;
slouken@1662
  1481
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1662
  1482
        _this->gl_config.accum_green_size = value;
slouken@1662
  1483
        break;
slouken@1662
  1484
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1662
  1485
        _this->gl_config.accum_blue_size = value;
slouken@1662
  1486
        break;
slouken@1662
  1487
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1662
  1488
        _this->gl_config.accum_alpha_size = value;
slouken@1662
  1489
        break;
slouken@1662
  1490
    case SDL_GL_STEREO:
slouken@1662
  1491
        _this->gl_config.stereo = value;
slouken@1662
  1492
        break;
slouken@1662
  1493
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1662
  1494
        _this->gl_config.multisamplebuffers = value;
slouken@1662
  1495
        break;
slouken@1662
  1496
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1662
  1497
        _this->gl_config.multisamplesamples = value;
slouken@1662
  1498
        break;
slouken@1662
  1499
    case SDL_GL_ACCELERATED_VISUAL:
slouken@1662
  1500
        _this->gl_config.accelerated = value;
slouken@1662
  1501
        break;
slouken@1662
  1502
    case SDL_GL_SWAP_CONTROL:
slouken@1662
  1503
        _this->gl_config.swap_control = value;
slouken@1662
  1504
        break;
slouken@1662
  1505
    default:
slouken@1662
  1506
        SDL_SetError ("Unknown OpenGL attribute");
slouken@1662
  1507
        retval = -1;
slouken@1662
  1508
        break;
slouken@1662
  1509
    }
slouken@1662
  1510
    return (retval);
slouken@0
  1511
}
slouken@0
  1512
slouken@0
  1513
/* Retrieve an attribute value from the windowing system. */
slouken@1662
  1514
int
slouken@1662
  1515
SDL_GL_GetAttribute (SDL_GLattr attr, int *value)
slouken@0
  1516
{
slouken@1662
  1517
    int retval = -1;
slouken@0
  1518
slouken@1662
  1519
    if (_this->GL_GetAttribute) {
slouken@1662
  1520
        retval = _this->GL_GetAttribute (_this, attr, value);
slouken@1662
  1521
    } else {
slouken@1662
  1522
        *value = 0;
slouken@1662
  1523
        SDL_SetError ("GL_GetAttribute not supported");
slouken@1662
  1524
    }
slouken@1662
  1525
    return retval;
slouken@0
  1526
}
slouken@0
  1527
slouken@0
  1528
/* Perform a GL buffer swap on the current GL context */
slouken@1662
  1529
void
slouken@1662
  1530
SDL_GL_SwapBuffers (void)
slouken@0
  1531
{
slouken@1662
  1532
    if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
slouken@1662
  1533
        _this->GL_SwapBuffers (_this);
slouken@1662
  1534
    } else {
slouken@1662
  1535
        SDL_SetError ("OpenGL video mode has not been set");
slouken@1662
  1536
    }
slouken@0
  1537
}
slouken@0
  1538
slouken@1662
  1539
#if 0                           // FIXME
slouken@541
  1540
/* Utility function used by SDL_WM_SetIcon();
slouken@541
  1541
 * flags & 1 for color key, flags & 2 for alpha channel. */
slouken@1662
  1542
static void
slouken@1662
  1543
CreateMaskFromColorKeyOrAlpha (SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1544
{
slouken@1662
  1545
    int x, y;
slouken@1662
  1546
    Uint32 colorkey;
slouken@0
  1547
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1548
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1549
slouken@1662
  1550
    colorkey = icon->format->colorkey;
slouken@1662
  1551
    switch (icon->format->BytesPerPixel) {
slouken@1662
  1552
    case 1:
slouken@1662
  1553
        {
slouken@1662
  1554
            Uint8 *pixels;
slouken@1662
  1555
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1556
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@1662
  1557
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1558
                    if (*pixels++ == colorkey) {
slouken@1662
  1559
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1560
                    }
slouken@1662
  1561
                }
slouken@1662
  1562
            }
slouken@1662
  1563
        }
slouken@1662
  1564
        break;
slouken@0
  1565
slouken@1662
  1566
    case 2:
slouken@1662
  1567
        {
slouken@1662
  1568
            Uint16 *pixels;
slouken@1662
  1569
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1570
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@1662
  1571
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1572
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1662
  1573
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1574
                    } else if ((flags & 2)
slouken@1662
  1575
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1662
  1576
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1577
                    }
slouken@1662
  1578
                    pixels++;
slouken@1662
  1579
                }
slouken@1662
  1580
            }
slouken@1662
  1581
        }
slouken@1662
  1582
        break;
slouken@0
  1583
slouken@1662
  1584
    case 4:
slouken@1662
  1585
        {
slouken@1662
  1586
            Uint32 *pixels;
slouken@1662
  1587
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1588
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@1662
  1589
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1590
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1662
  1591
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1592
                    } else if ((flags & 2)
slouken@1662
  1593
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1662
  1594
                        SET_MASKBIT (icon, x, y, mask);
slouken@1662
  1595
                    }
slouken@1662
  1596
                    pixels++;
slouken@1662
  1597
                }
slouken@1662
  1598
            }
slouken@1662
  1599
        }
slouken@1662
  1600
        break;
slouken@1662
  1601
    }
slouken@0
  1602
}
slouken@0
  1603
slouken@0
  1604
/*
slouken@0
  1605
 * Sets the window manager icon for the display window.
slouken@0
  1606
 */
slouken@1662
  1607
void
slouken@1662
  1608
SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask)
slouken@0
  1609
{
slouken@1662
  1610
    if (icon && _this->SetIcon) {
slouken@1662
  1611
        /* Generate a mask if necessary, and create the icon! */
slouken@1662
  1612
        if (mask == NULL) {
slouken@1662
  1613
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@1662
  1614
            int flags = 0;
slouken@1662
  1615
            mask = (Uint8 *) SDL_malloc (mask_len);
slouken@1662
  1616
            if (mask == NULL) {
slouken@1662
  1617
                return;
slouken@1662
  1618
            }
slouken@1662
  1619
            SDL_memset (mask, ~0, mask_len);
slouken@1662
  1620
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@1662
  1621
                flags |= 1;
slouken@1662
  1622
            if (icon->flags & SDL_SRCALPHA)
slouken@1662
  1623
                flags |= 2;
slouken@1662
  1624
            if (flags) {
slouken@1662
  1625
                CreateMaskFromColorKeyOrAlpha (icon, mask, flags);
slouken@1662
  1626
            }
slouken@1662
  1627
            _this->SetIcon (_this, icon, mask);
slouken@1662
  1628
            SDL_free (mask);
slouken@1662
  1629
        } else {
slouken@1662
  1630
            _this->SetIcon (_this, icon, mask);
slouken@1662
  1631
        }
slouken@1662
  1632
    }
slouken@1662
  1633
}
slouken@1662
  1634
#endif
slouken@0
  1635
slouken@1662
  1636
SDL_bool
slouken@1662
  1637
SDL_GetWindowWMInfo (SDL_WindowID windowID, SDL_SysWMinfo * info)
slouken@1662
  1638
{
slouken@1662
  1639
    SDL_Window *window = SDL_GetWindowFromID (windowID);
slouken@1662
  1640
slouken@1662
  1641
    if (!window || !_this->GetWindowWMInfo) {
slouken@1662
  1642
        return SDL_FALSE;
slouken@1662
  1643
    }
slouken@1662
  1644
    return (_this->GetWindowWMInfo (_this, window, info));
slouken@0
  1645
}
slouken@0
  1646
slouken@1662
  1647
/* vi: set ts=4 sw=4 expandtab: */