src/video/SDL_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1667 1fddae038bc8
child 1669 9857d21967bb
permissions -rw-r--r--
more tweaking indent options
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@1668
   135
int SDL_VideoInit(const char *driver_name, Uint32 flags);
slouken@1668
   136
void SDL_VideoQuit(void);
slouken@0
   137
slouken@1662
   138
static int
slouken@1668
   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@1668
   150
    if (SDL_BITSPERPIXEL(a.format) != SDL_BITSPERPIXEL(b.format)) {
slouken@1668
   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@1668
   160
SDL_GetNumVideoDrivers(void)
slouken@1660
   161
{
slouken@1668
   162
    return SDL_arraysize(bootstrap) - 1;
slouken@1662
   163
}
slouken@1662
   164
slouken@1662
   165
const char *
slouken@1668
   166
SDL_GetVideoDriver(int index)
slouken@1662
   167
{
slouken@1668
   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@1668
   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@1668
   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@1668
   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@1668
   206
            if (SDL_strncmp(bootstrap[i]->name, driver_name,
slouken@1668
   207
                            SDL_strlen(bootstrap[i]->name)) == 0) {
slouken@1668
   208
                if (bootstrap[i]->available()) {
slouken@1668
   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@1668
   216
            if (bootstrap[i]->available()) {
slouken@1668
   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@1668
   226
            SDL_SetError("%s not available", driver_name);
slouken@1662
   227
        } else {
slouken@1668
   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@1668
   258
    if (_this->VideoInit(_this) < 0) {
slouken@1668
   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@1668
   265
        SDL_SetError("The video driver did not add any displays");
slouken@1668
   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@1668
   272
        SDL_qsort(_this->displays[i].display_modes,
slouken@1668
   273
                  _this->displays[i].num_display_modes,
slouken@1668
   274
                  sizeof(SDL_DisplayMode), cmpmodes);
slouken@1662
   275
    }
slouken@0
   276
slouken@1662
   277
    /* Start the event loop */
slouken@1668
   278
    if (SDL_StartEventLoop(flags) < 0) {
slouken@1668
   279
        SDL_VideoQuit();
slouken@1662
   280
        return -1;
slouken@1662
   281
    }
slouken@1668
   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@1668
   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@1668
   298
SDL_GetVideoDevice()
slouken@0
   299
{
slouken@1662
   300
    return _this;
slouken@0
   301
}
slouken@0
   302
slouken@1662
   303
const SDL_VideoInfo *
slouken@1668
   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@1668
   313
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   314
{
slouken@1662
   315
    SDL_VideoDisplay display;
slouken@0
   316
slouken@1668
   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@1668
   324
    SDL_AddVideoDisplay(&display);
slouken@0
   325
}
slouken@0
   326
slouken@1662
   327
void
slouken@1668
   328
SDL_AddVideoDisplay(SDL_VideoDisplay * display)
slouken@1662
   329
{
slouken@1662
   330
    SDL_VideoDisplay *displays;
slouken@0
   331
slouken@1662
   332
    displays =
slouken@1668
   333
        SDL_realloc(_this->displays,
slouken@1668
   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@1668
   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@1668
   352
SDL_SelectVideoDisplay(int index)
slouken@0
   353
{
slouken@1662
   354
    if (!_this) {
slouken@1668
   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@1668
   360
            SDL_SetError("index must be in the range 0 - %d",
slouken@1668
   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@1668
   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@1668
   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@1668
   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@1668
   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@1668
   403
SDL_GetDisplayMode(int index)
slouken@0
   404
{
slouken@1668
   405
    if (index < 0 || index >= SDL_GetNumDisplayModes()) {
slouken@1668
   406
        SDL_SetError("index must be in the range of 0 - %d",
slouken@1668
   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@1668
   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@1668
   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@1668
   432
SDL_GetClosestDisplayMode(const SDL_DisplayMode * mode,
slouken@1668
   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@1668
   474
                (SDL_BITSPERPIXEL(current->format) >=
slouken@1668
   475
                 SDL_BITSPERPIXEL(target_format)
slouken@1668
   476
                 && SDL_PIXELTYPE(current->format) ==
slouken@1668
   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@1668
   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@1668
   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@1668
   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@1668
   547
    if (!SDL_GetClosestDisplayMode(&display_mode, &display_mode)) {
slouken@1668
   548
        SDL_SetError("No video mode large enough for %dx%d",
slouken@1668
   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@1668
   555
        (&display_mode, SDL_GetCurrentDisplayMode(),
slouken@1668
   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@1668
   564
            SDL_FreeSurface(window->shadow);
slouken@1667
   565
            window->shadow = NULL;
slouken@1667
   566
        }
slouken@1667
   567
        if (window->surface) {
slouken@1668
   568
            SDL_FreeSurface(window->surface);
slouken@1667
   569
            window->surface = NULL;
slouken@1667
   570
        }
slouken@1667
   571
    }
slouken@1667
   572
slouken@1668
   573
    return _this->SetDisplayMode(_this, &display_mode);
slouken@1662
   574
}
slouken@0
   575
slouken@1662
   576
SDL_WindowID
slouken@1668
   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@1668
   592
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   593
        return 0;
slouken@1662
   594
    }
slouken@0
   595
slouken@1668
   596
    SDL_zero(window);
slouken@1662
   597
    window.id = _this->next_window_id++;
slouken@1668
   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@1668
   605
    if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
slouken@1667
   606
        if (window.title) {
slouken@1668
   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@1668
   614
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   615
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   616
    if (!windows) {
slouken@1662
   617
        if (_this->DestroyWindow) {
slouken@1668
   618
            _this->DestroyWindow(_this, &window);
slouken@1662
   619
        }
slouken@1667
   620
        if (window.title) {
slouken@1668
   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@1668
   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@1668
   640
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
   641
        return (0);
slouken@1662
   642
    }
slouken@0
   643
slouken@1668
   644
    SDL_zero(window);
slouken@1662
   645
    window.id = _this->next_window_id++;
slouken@1662
   646
slouken@1662
   647
    if (!_this->CreateWindowFrom ||
slouken@1668
   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@1668
   654
        SDL_realloc(SDL_CurrentDisplay.windows,
slouken@1668
   655
                    (num_windows + 1) * sizeof(*windows));
slouken@1662
   656
    if (!windows) {
slouken@1662
   657
        if (_this->DestroyWindow) {
slouken@1668
   658
            _this->DestroyWindow(_this, &window);
slouken@1662
   659
        }
slouken@1667
   660
        if (window.title) {
slouken@1668
   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@1668
   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@1668
   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@1668
   716
SDL_GetWindowFlags(SDL_WindowID windowID)
slouken@1662
   717
{
slouken@1668
   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@1668
   727
SDL_SetWindowTitle(SDL_WindowID windowID, const char *title)
slouken@1662
   728
{
slouken@1668
   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@1668
   735
        SDL_free(window->title);
slouken@1662
   736
    }
slouken@1668
   737
    window->title = SDL_strdup(title);
slouken@1662
   738
slouken@1662
   739
    if (_this->SetWindowTitle) {
slouken@1668
   740
        _this->SetWindowTitle(_this, window);
slouken@1662
   741
    }
slouken@1662
   742
}
slouken@1662
   743
slouken@1662
   744
const char *
slouken@1668
   745
SDL_GetWindowTitle(SDL_WindowID windowID)
slouken@1662
   746
{
slouken@1668
   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@1668
   756
SDL_SetWindowData(SDL_WindowID windowID, void *userdata)
slouken@1662
   757
{
slouken@1668
   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@1668
   767
SDL_GetWindowData(SDL_WindowID windowID)
slouken@1662
   768
{
slouken@1668
   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@1668
   778
SDL_SetWindowPosition(SDL_WindowID windowID, int x, int y)
slouken@1662
   779
{
slouken@1668
   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@1668
   790
        _this->SetWindowPosition(_this, window);
slouken@1662
   791
    }
slouken@1662
   792
}
slouken@1662
   793
slouken@1662
   794
void
slouken@1668
   795
SDL_GetWindowPosition(SDL_WindowID windowID, int *x, int *y)
slouken@1662
   796
{
slouken@1668
   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@1668
   811
SDL_SetWindowSize(SDL_WindowID windowID, int w, int h)
slouken@1662
   812
{
slouken@1668
   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@1668
   823
        _this->SetWindowSize(_this, window);
slouken@1662
   824
    }
slouken@1662
   825
}
slouken@1662
   826
slouken@1662
   827
void
slouken@1668
   828
SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
slouken@1662
   829
{
slouken@1668
   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@1668
   844
SDL_ShowWindow(SDL_WindowID windowID)
slouken@1662
   845
{
slouken@1668
   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@1668
   855
        _this->ShowWindow(_this, window);
slouken@1662
   856
    }
slouken@1662
   857
}
slouken@1662
   858
slouken@1662
   859
void
slouken@1668
   860
SDL_HideWindow(SDL_WindowID windowID)
slouken@1662
   861
{
slouken@1668
   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@1668
   871
        _this->HideWindow(_this, window);
slouken@1662
   872
    }
slouken@1662
   873
}
slouken@1662
   874
slouken@1662
   875
void
slouken@1668
   876
SDL_RaiseWindow(SDL_WindowID windowID)
slouken@1662
   877
{
slouken@1668
   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@1668
   885
        _this->RaiseWindow(_this, window);
slouken@1662
   886
    }
slouken@1662
   887
}
slouken@1662
   888
slouken@1662
   889
void
slouken@1668
   890
SDL_MaximizeWindow(SDL_WindowID windowID)
slouken@1662
   891
{
slouken@1668
   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@1668
   901
        _this->MaximizeWindow(_this, window);
slouken@1662
   902
    }
slouken@1662
   903
}
slouken@1662
   904
slouken@1662
   905
void
slouken@1668
   906
SDL_MinimizeWindow(SDL_WindowID windowID)
slouken@1662
   907
{
slouken@1668
   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@1668
   917
        _this->MinimizeWindow(_this, window);
slouken@1662
   918
    }
slouken@1662
   919
}
slouken@1662
   920
slouken@1662
   921
void
slouken@1668
   922
SDL_RestoreWindow(SDL_WindowID windowID)
slouken@1662
   923
{
slouken@1668
   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@1668
   934
        _this->RestoreWindow(_this, window);
slouken@1662
   935
    }
slouken@1662
   936
}
slouken@1662
   937
slouken@1662
   938
void
slouken@1668
   939
SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
slouken@1662
   940
{
slouken@1668
   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@1668
   954
        _this->SetWindowGrab(_this, window);
slouken@1662
   955
    }
slouken@1662
   956
}
slouken@1662
   957
slouken@1662
   958
int
slouken@1668
   959
SDL_GetWindowGrab(SDL_WindowID windowID)
slouken@1662
   960
{
slouken@1668
   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@1668
   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@1668
   988
                _this->SetWindowGrab(_this, window);
slouken@1662
   989
            }
slouken@1662
   990
            if (window->shadow) {
slouken@1668
   991
                SDL_FreeSurface(window->shadow);
slouken@1662
   992
            }
slouken@1662
   993
            if (window->surface) {
slouken@1668
   994
                SDL_FreeSurface(window->surface);
slouken@1662
   995
            }
slouken@1662
   996
            if (_this->DestroyWindow) {
slouken@1668
   997
                _this->DestroyWindow(_this, window);
slouken@1662
   998
            }
slouken@1662
   999
            if (window->title) {
slouken@1668
  1000
                SDL_free(window->title);
slouken@1662
  1001
            }
slouken@1662
  1002
            if (window->gamma) {
slouken@1668
  1003
                SDL_free(window->gamma);
slouken@1662
  1004
            }
slouken@1662
  1005
            if (j != display->num_windows - 1) {
slouken@1668
  1006
                SDL_memcpy(&display->windows[i],
slouken@1668
  1007
                           &display->windows[i + 1],
slouken@1668
  1008
                           (display->num_windows - i - 1) * sizeof(*window));
slouken@1662
  1009
            }
slouken@1662
  1010
            --display->num_windows;
slouken@1662
  1011
            return;
slouken@1662
  1012
        }
slouken@1662
  1013
    }
slouken@1662
  1014
}
slouken@1662
  1015
slouken@1662
  1016
SDL_Surface *
slouken@1668
  1017
SDL_CreateWindowSurface(SDL_WindowID windowID, Uint32 format, Uint32 flags)
slouken@1662
  1018
{
slouken@1668
  1019
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1666
  1020
    Uint32 black;
slouken@1662
  1021
    SDL_Surface *surface;
slouken@1662
  1022
slouken@1662
  1023
    if (!window) {
slouken@1662
  1024
        return NULL;
slouken@1662
  1025
    }
slouken@1662
  1026
slouken@1662
  1027
    if (!_this->CreateWindowSurface) {
slouken@1662
  1028
        return NULL;
slouken@1662
  1029
    }
slouken@1662
  1030
slouken@1662
  1031
    if (!window->surface) {
slouken@1668
  1032
        _this->CreateWindowSurface(_this, window, flags);
slouken@1662
  1033
        if (!window->surface) {
slouken@1662
  1034
            return NULL;
slouken@1662
  1035
        }
slouken@1666
  1036
        window->surface->flags |= SDL_SCREEN_SURFACE;
slouken@1662
  1037
    }
slouken@1666
  1038
    surface = window->surface;
slouken@1662
  1039
slouken@1662
  1040
    if (window->shadow) {
slouken@1668
  1041
        SDL_FreeSurface(window->shadow);
slouken@1662
  1042
        window->shadow = NULL;
slouken@1662
  1043
    }
slouken@1662
  1044
slouken@1662
  1045
    /* Create a shadow surface if necessary */
slouken@1666
  1046
    if ((!(flags & SDL_ANYFORMAT)
slouken@1668
  1047
         && (format != SDL_GetCurrentDisplayMode()->format))
slouken@1666
  1048
        || ((flags & SDL_HWPALETTE)
slouken@1666
  1049
            && !(window->surface->flags & SDL_HWPALETTE))) {
slouken@1662
  1050
        int bpp;
slouken@1662
  1051
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1662
  1052
slouken@1668
  1053
        SDL_PixelFormatEnumToMasks(format, &bpp, &Amask, &Gmask, &Bmask,
slouken@1668
  1054
                                   &Amask);
slouken@1666
  1055
        window->shadow =
slouken@1668
  1056
            SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, bpp,
slouken@1668
  1057
                                 Rmask, Gmask, Bmask, Amask);
slouken@1666
  1058
        if (window->shadow == NULL) {
slouken@1662
  1059
            return NULL;
slouken@1662
  1060
        }
slouken@1666
  1061
        window->shadow->flags |= SDL_SHADOW_SURFACE;
slouken@1666
  1062
        surface = window->shadow;
slouken@1662
  1063
slouken@1662
  1064
        /* 8-bit shadow surfaces report that they have exclusive palette */
slouken@1666
  1065
        if (surface->format->palette) {
slouken@1666
  1066
            surface->flags |= SDL_HWPALETTE;
slouken@1668
  1067
            if (format == SDL_GetCurrentDisplayMode()->format) {
slouken@1668
  1068
                SDL_memcpy(surface->format->palette->colors,
slouken@1668
  1069
                           window->surface->format->palette->colors,
slouken@1668
  1070
                           window->surface->format->palette->ncolors *
slouken@1668
  1071
                           sizeof(SDL_Color));
slouken@1662
  1072
            } else {
slouken@1668
  1073
                SDL_DitherColors(surface->format->palette->colors, bpp);
slouken@1662
  1074
            }
slouken@1662
  1075
        }
slouken@1662
  1076
    }
slouken@1662
  1077
slouken@1662
  1078
    /* Clear the surface for display */
slouken@1662
  1079
    {
slouken@1668
  1080
        Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
slouken@1668
  1081
        SDL_FillRect(surface, NULL, black);
slouken@1662
  1082
        if (surface->flags & SDL_DOUBLEBUF) {
slouken@1668
  1083
            SDL_Flip(surface);
slouken@1668
  1084
            SDL_FillRect(surface, NULL, black);
slouken@1662
  1085
        }
slouken@1668
  1086
        SDL_Flip(surface);
slouken@1662
  1087
    }
slouken@1662
  1088
slouken@1662
  1089
    return surface;
slouken@0
  1090
}
slouken@0
  1091
slouken@0
  1092
/* 
slouken@0
  1093
 * Convert a surface into the video pixel format.
slouken@0
  1094
 */
slouken@1662
  1095
SDL_Surface *
slouken@1668
  1096
SDL_DisplayFormat(SDL_Surface * surface)
slouken@0
  1097
{
slouken@1662
  1098
    Uint32 flags;
slouken@0
  1099
slouken@1662
  1100
    if (!SDL_PublicSurface) {
slouken@1668
  1101
        SDL_SetError("No video mode has been set");
slouken@1662
  1102
        return (NULL);
slouken@1662
  1103
    }
slouken@1662
  1104
    /* Set the flags appropriate for copying to display surface */
slouken@1662
  1105
    if (((SDL_PublicSurface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
slouken@1662
  1106
        && _this->info.blit_hw)
slouken@1662
  1107
        flags = SDL_HWSURFACE;
slouken@1662
  1108
    else
slouken@1662
  1109
        flags = SDL_SWSURFACE;
slouken@0
  1110
#ifdef AUTORLE_DISPLAYFORMAT
slouken@1662
  1111
    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
slouken@1662
  1112
    flags |= SDL_RLEACCELOK;
slouken@0
  1113
#else
slouken@1662
  1114
    flags |=
slouken@1662
  1115
        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@0
  1116
#endif
slouken@1668
  1117
    return (SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
slouken@0
  1118
}
slouken@0
  1119
slouken@0
  1120
/*
slouken@0
  1121
 * Convert a surface into a format that's suitable for blitting to
slouken@0
  1122
 * the screen, but including an alpha channel.
slouken@0
  1123
 */
slouken@1662
  1124
SDL_Surface *
slouken@1668
  1125
SDL_DisplayFormatAlpha(SDL_Surface * surface)
slouken@0
  1126
{
slouken@1662
  1127
    SDL_PixelFormat *vf;
slouken@1662
  1128
    SDL_PixelFormat *format;
slouken@1662
  1129
    SDL_Surface *converted;
slouken@1662
  1130
    Uint32 flags;
slouken@1662
  1131
    /* default to ARGB8888 */
slouken@1662
  1132
    Uint32 amask = 0xff000000;
slouken@1662
  1133
    Uint32 rmask = 0x00ff0000;
slouken@1662
  1134
    Uint32 gmask = 0x0000ff00;
slouken@1662
  1135
    Uint32 bmask = 0x000000ff;
slouken@0
  1136
slouken@1662
  1137
    if (!SDL_PublicSurface) {
slouken@1668
  1138
        SDL_SetError("No video mode has been set");
slouken@1662
  1139
        return (NULL);
slouken@1662
  1140
    }
slouken@1662
  1141
    vf = SDL_PublicSurface->format;
slouken@0
  1142
slouken@1662
  1143
    switch (vf->BytesPerPixel) {
slouken@1662
  1144
    case 2:
slouken@1662
  1145
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
slouken@1662
  1146
           For anything else (like ARGB4444) it doesn't matter
slouken@1662
  1147
           since we have no special code for it anyway */
slouken@1662
  1148
        if ((vf->Rmask == 0x1f) &&
slouken@1662
  1149
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
slouken@1662
  1150
            rmask = 0xff;
slouken@1662
  1151
            bmask = 0xff0000;
slouken@1662
  1152
        }
slouken@1662
  1153
        break;
slouken@0
  1154
slouken@1662
  1155
    case 3:
slouken@1662
  1156
    case 4:
slouken@1662
  1157
        /* Keep the video format, as long as the high 8 bits are
slouken@1662
  1158
           unused or alpha */
slouken@1662
  1159
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
slouken@1662
  1160
            rmask = 0xff;
slouken@1662
  1161
            bmask = 0xff0000;
slouken@1662
  1162
        }
slouken@1662
  1163
        break;
slouken@0
  1164
slouken@1662
  1165
    default:
slouken@1662
  1166
        /* We have no other optimised formats right now. When/if a new
slouken@1662
  1167
           optimised alpha format is written, add the converter here */
slouken@1662
  1168
        break;
slouken@1662
  1169
    }
slouken@1668
  1170
    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
slouken@1662
  1171
    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
slouken@1662
  1172
    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1668
  1173
    converted = SDL_ConvertSurface(surface, format, flags);
slouken@1668
  1174
    SDL_FreeFormat(format);
slouken@1662
  1175
    return (converted);
slouken@0
  1176
}
slouken@0
  1177
slouken@0
  1178
/*
slouken@0
  1179
 * Update a specific portion of the physical screen
slouken@0
  1180
 */
slouken@1662
  1181
void
slouken@1668
  1182
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
slouken@0
  1183
{
slouken@1662
  1184
    if (screen) {
slouken@1662
  1185
        SDL_Rect rect;
slouken@0
  1186
slouken@1662
  1187
        /* Perform some checking */
slouken@1662
  1188
        if (w == 0)
slouken@1662
  1189
            w = screen->w;
slouken@1662
  1190
        if (h == 0)
slouken@1662
  1191
            h = screen->h;
slouken@1662
  1192
        if ((int) (x + w) > screen->w)
slouken@1662
  1193
            return;
slouken@1662
  1194
        if ((int) (y + h) > screen->h)
slouken@1662
  1195
            return;
slouken@0
  1196
slouken@1662
  1197
        /* Fill the rectangle */
slouken@1662
  1198
        rect.x = (Sint16) x;
slouken@1662
  1199
        rect.y = (Sint16) y;
slouken@1662
  1200
        rect.w = (Uint16) w;
slouken@1662
  1201
        rect.h = (Uint16) h;
slouken@1668
  1202
        SDL_UpdateRects(screen, 1, &rect);
slouken@1662
  1203
    }
slouken@0
  1204
}
slouken@1662
  1205
void
slouken@1668
  1206
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
slouken@0
  1207
{
slouken@1662
  1208
    int i;
slouken@1662
  1209
    SDL_Window *window;
slouken@0
  1210
slouken@1662
  1211
    /* Find the window corresponding to this surface */
slouken@1668
  1212
    window = SDL_GetWindowFromSurface(screen);
slouken@1662
  1213
    if (!window) {
slouken@1668
  1214
        SDL_SetError("Couldn't find window associated with surface");
slouken@1662
  1215
        return;
slouken@1662
  1216
    }
slouken@0
  1217
slouken@1662
  1218
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1668
  1219
        if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1668
  1220
            SDL_LockCursor();
slouken@1668
  1221
            SDL_DrawCursor(screen);
slouken@1662
  1222
            for (i = 0; i < numrects; ++i) {
slouken@1668
  1223
                SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
slouken@1662
  1224
            }
slouken@1668
  1225
            SDL_EraseCursor(screen);
slouken@1668
  1226
            SDL_UnlockCursor();
slouken@1662
  1227
        } else {
slouken@1662
  1228
            for (i = 0; i < numrects; ++i) {
slouken@1668
  1229
                SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
slouken@1662
  1230
            }
slouken@1662
  1231
        }
slouken@1662
  1232
slouken@1662
  1233
        /* Fall through to video surface update */
slouken@1662
  1234
        screen = window->surface;
slouken@1662
  1235
    }
slouken@1662
  1236
    if ((screen->flags & SDL_SCREEN_SURFACE) && _this->UpdateWindowSurface) {
slouken@1662
  1237
        /* Update the video surface */
slouken@1662
  1238
        if (screen->offset) {
slouken@1662
  1239
            int offset_y = screen->offset / screen->pitch;
slouken@1662
  1240
            int offset_x = screen->offset % screen->pitch;
slouken@1662
  1241
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1242
                rects[i].x += offset_x;
slouken@1662
  1243
                rects[i].y += offset_y;
slouken@1662
  1244
            }
slouken@1668
  1245
            _this->UpdateWindowSurface(_this, window, numrects, rects);
slouken@1662
  1246
            for (i = 0; i < numrects; ++i) {
slouken@1662
  1247
                rects[i].x -= offset_x;
slouken@1662
  1248
                rects[i].y -= offset_y;
slouken@1662
  1249
            }
slouken@1662
  1250
        } else {
slouken@1668
  1251
            _this->UpdateWindowSurface(_this, window, numrects, rects);
slouken@1662
  1252
        }
slouken@1662
  1253
    }
slouken@0
  1254
}
slouken@0
  1255
slouken@0
  1256
/*
slouken@0
  1257
 * Performs hardware double buffering, if possible, or a full update if not.
slouken@0
  1258
 */
slouken@1662
  1259
int
slouken@1668
  1260
SDL_Flip(SDL_Surface * screen)
slouken@0
  1261
{
slouken@1662
  1262
    SDL_Window *window;
slouken@0
  1263
slouken@1662
  1264
    /* Find the window corresponding to this surface */
slouken@1668
  1265
    window = SDL_GetWindowFromSurface(screen);
slouken@1662
  1266
    if (!window) {
slouken@1668
  1267
        SDL_SetError("Couldn't find window associated with surface");
slouken@1662
  1268
        return;
slouken@1662
  1269
    }
slouken@0
  1270
slouken@1662
  1271
    /* Copy the shadow surface to the video surface */
slouken@1662
  1272
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1273
        SDL_Rect rect;
slouken@1662
  1274
slouken@1662
  1275
        rect.x = 0;
slouken@1662
  1276
        rect.y = 0;
slouken@1662
  1277
        rect.w = screen->w;
slouken@1662
  1278
        rect.h = screen->h;
slouken@1668
  1279
        if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
slouken@1668
  1280
            SDL_LockCursor();
slouken@1668
  1281
            SDL_DrawCursor(screen);
slouken@1668
  1282
            SDL_LowerBlit(screen, &rect, window->surface, &rect);
slouken@1668
  1283
            SDL_EraseCursor(screen);
slouken@1668
  1284
            SDL_UnlockCursor();
slouken@1662
  1285
        } else {
slouken@1668
  1286
            SDL_LowerBlit(screen, &rect, window->surface, &rect);
slouken@1662
  1287
        }
slouken@1662
  1288
slouken@1662
  1289
        /* Fall through to video surface update */
slouken@1662
  1290
        screen = window->surface;
slouken@1662
  1291
    }
slouken@1662
  1292
    if (screen->flags & SDL_DOUBLEBUF) {
slouken@1668
  1293
        _this->FlipWindowSurface(_this, window);
slouken@1662
  1294
    } else {
slouken@1668
  1295
        SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1662
  1296
    }
slouken@1662
  1297
    return (0);
slouken@0
  1298
}
slouken@0
  1299
slouken@1662
  1300
int
slouken@1668
  1301
SDL_SetColors(SDL_Surface * screen, SDL_Color * colors, int firstcolor,
slouken@1668
  1302
              int ncolors)
slouken@0
  1303
{
slouken@1662
  1304
    SDL_Window *window = NULL;
slouken@1662
  1305
    SDL_Palette *pal;
slouken@1662
  1306
    int gotall;
slouken@1662
  1307
    int palsize;
slouken@0
  1308
slouken@1662
  1309
    /* Verify the parameters */
slouken@1662
  1310
    pal = screen->format->palette;
slouken@1662
  1311
    if (!pal) {
slouken@1662
  1312
        return 0;               /* not a palettized surface */
slouken@1662
  1313
    }
slouken@1662
  1314
    gotall = 1;
slouken@1662
  1315
    palsize = 1 << screen->format->BitsPerPixel;
slouken@1662
  1316
    if (ncolors > (palsize - firstcolor)) {
slouken@1662
  1317
        ncolors = (palsize - firstcolor);
slouken@1662
  1318
        gotall = 0;
slouken@1662
  1319
    }
slouken@0
  1320
slouken@1662
  1321
    if (colors != (pal->colors + firstcolor)) {
slouken@1668
  1322
        SDL_memcpy(pal->colors + firstcolor, colors,
slouken@1668
  1323
                   ncolors * sizeof(*colors));
slouken@1662
  1324
    }
slouken@1668
  1325
    SDL_FormatChanged(screen);
slouken@1662
  1326
slouken@1662
  1327
    if (screen->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
slouken@1668
  1328
        window = SDL_GetWindowFromSurface(screen);
slouken@1662
  1329
        if (!window) {
slouken@1662
  1330
            return 0;
slouken@1662
  1331
        }
slouken@1662
  1332
    }
slouken@1662
  1333
slouken@1662
  1334
    if (screen->flags & SDL_SHADOW_SURFACE) {
slouken@1662
  1335
        SDL_Palette *vidpal;
slouken@1662
  1336
slouken@1662
  1337
        vidpal = window->surface->format->palette;
slouken@1662
  1338
        if (vidpal && vidpal->ncolors == pal->ncolors) {
slouken@1662
  1339
            /* This is a shadow surface, and the physical
slouken@1662
  1340
             * framebuffer is also indexed. Propagate the
slouken@1662
  1341
             * changes to its logical palette so that
slouken@1662
  1342
             * updates are always identity blits
slouken@1662
  1343
             */
slouken@1668
  1344
            SDL_memcpy(vidpal->colors + firstcolor, colors,
slouken@1668
  1345
                       ncolors * sizeof(*colors));
slouken@1662
  1346
        }
slouken@1662
  1347
        if (window->surface->flags & SDL_HWPALETTE) {
slouken@1662
  1348
            /* Set the physical palette */
slouken@1662
  1349
            screen = window->surface;
slouken@1662
  1350
        } else {
slouken@1668
  1351
            SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1662
  1352
        }
slouken@1662
  1353
    }
slouken@1662
  1354
slouken@1662
  1355
    if (screen->flags & SDL_SCREEN_SURFACE) {
slouken@1662
  1356
        if (_this->SetWindowColors) {
slouken@1662
  1357
            gotall =
slouken@1668
  1358
                _this->SetWindowColors(_this, window, firstcolor, ncolors,
slouken@1668
  1359
                                       colors);
slouken@1662
  1360
            if (!gotall) {
slouken@1662
  1361
                /* The video flags shouldn't have SDL_HWPALETTE, and
slouken@1662
  1362
                   the video driver is responsible for copying back the
slouken@1662
  1363
                   correct colors into the video surface palette.
slouken@1662
  1364
                 */
slouken@1662
  1365
                ;
slouken@1662
  1366
            }
slouken@1662
  1367
        }
slouken@1668
  1368
        SDL_CursorPaletteChanged();
slouken@1662
  1369
    }
slouken@1662
  1370
slouken@1662
  1371
    return gotall;
slouken@0
  1372
}
slouken@0
  1373
slouken@1662
  1374
void
slouken@1668
  1375
SDL_VideoQuit(void)
slouken@0
  1376
{
slouken@1662
  1377
    int i, j;
slouken@0
  1378
slouken@1662
  1379
    if (!_this) {
slouken@1662
  1380
        return;
slouken@1662
  1381
    }
slouken@0
  1382
slouken@1662
  1383
    /* Halt event processing before doing anything else */
slouken@1668
  1384
    SDL_StopEventLoop();
slouken@0
  1385
slouken@1662
  1386
    /* Clean up allocated window manager items */
slouken@1668
  1387
    SDL_CursorQuit();
slouken@0
  1388
slouken@1662
  1389
    /* Clean up the system video */
slouken@1662
  1390
    for (i = _this->num_displays; i--;) {
slouken@1662
  1391
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@1662
  1392
        for (j = display->num_windows; j--;) {
slouken@1668
  1393
            SDL_DestroyWindow(display->windows[i].id);
slouken@1662
  1394
        }
slouken@1662
  1395
        if (display->windows) {
slouken@1668
  1396
            SDL_free(display->windows);
slouken@1662
  1397
            display->windows = NULL;
slouken@1662
  1398
        }
slouken@1662
  1399
    }
slouken@1668
  1400
    _this->VideoQuit(_this);
slouken@1662
  1401
    if (_this->displays) {
slouken@1668
  1402
        SDL_free(_this->displays);
slouken@1662
  1403
    }
slouken@1668
  1404
    _this->free(_this);
slouken@1662
  1405
    _this = NULL;
slouken@0
  1406
}
slouken@0
  1407
slouken@0
  1408
/* Load the GL driver library */
slouken@1662
  1409
int
slouken@1668
  1410
SDL_GL_LoadLibrary(const char *path)
slouken@0
  1411
{
slouken@1662
  1412
    int retval;
slouken@0
  1413
slouken@1662
  1414
    retval = -1;
slouken@1662
  1415
    if (_this == NULL) {
slouken@1668
  1416
        SDL_SetError("Video subsystem has not been initialized");
slouken@1662
  1417
    } else {
slouken@1662
  1418
        if (_this->GL_LoadLibrary) {
slouken@1668
  1419
            retval = _this->GL_LoadLibrary(_this, path);
slouken@1662
  1420
        } else {
slouken@1668
  1421
            SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1422
        }
slouken@1662
  1423
    }
slouken@1662
  1424
    return (retval);
slouken@0
  1425
}
slouken@0
  1426
slouken@1662
  1427
void *
slouken@1668
  1428
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  1429
{
slouken@1662
  1430
    void *func;
slouken@0
  1431
slouken@1662
  1432
    func = NULL;
slouken@1662
  1433
    if (_this->GL_GetProcAddress) {
slouken@1662
  1434
        if (_this->gl_config.driver_loaded) {
slouken@1668
  1435
            func = _this->GL_GetProcAddress(_this, proc);
slouken@1662
  1436
        } else {
slouken@1668
  1437
            SDL_SetError("No GL driver has been loaded");
slouken@1662
  1438
        }
slouken@1662
  1439
    } else {
slouken@1668
  1440
        SDL_SetError("No dynamic GL support in video driver");
slouken@1662
  1441
    }
slouken@1662
  1442
    return func;
slouken@0
  1443
}
slouken@0
  1444
slouken@0
  1445
/* Set the specified GL attribute for setting up a GL video mode */
slouken@1662
  1446
int
slouken@1668
  1447
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  1448
{
slouken@1662
  1449
    int retval;
slouken@0
  1450
slouken@1662
  1451
    retval = 0;
slouken@1662
  1452
    switch (attr) {
slouken@1662
  1453
    case SDL_GL_RED_SIZE:
slouken@1662
  1454
        _this->gl_config.red_size = value;
slouken@1662
  1455
        break;
slouken@1662
  1456
    case SDL_GL_GREEN_SIZE:
slouken@1662
  1457
        _this->gl_config.green_size = value;
slouken@1662
  1458
        break;
slouken@1662
  1459
    case SDL_GL_BLUE_SIZE:
slouken@1662
  1460
        _this->gl_config.blue_size = value;
slouken@1662
  1461
        break;
slouken@1662
  1462
    case SDL_GL_ALPHA_SIZE:
slouken@1662
  1463
        _this->gl_config.alpha_size = value;
slouken@1662
  1464
        break;
slouken@1662
  1465
    case SDL_GL_DOUBLEBUFFER:
slouken@1662
  1466
        _this->gl_config.double_buffer = value;
slouken@1662
  1467
        break;
slouken@1662
  1468
    case SDL_GL_BUFFER_SIZE:
slouken@1662
  1469
        _this->gl_config.buffer_size = value;
slouken@1662
  1470
        break;
slouken@1662
  1471
    case SDL_GL_DEPTH_SIZE:
slouken@1662
  1472
        _this->gl_config.depth_size = value;
slouken@1662
  1473
        break;
slouken@1662
  1474
    case SDL_GL_STENCIL_SIZE:
slouken@1662
  1475
        _this->gl_config.stencil_size = value;
slouken@1662
  1476
        break;
slouken@1662
  1477
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1662
  1478
        _this->gl_config.accum_red_size = value;
slouken@1662
  1479
        break;
slouken@1662
  1480
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1662
  1481
        _this->gl_config.accum_green_size = value;
slouken@1662
  1482
        break;
slouken@1662
  1483
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1662
  1484
        _this->gl_config.accum_blue_size = value;
slouken@1662
  1485
        break;
slouken@1662
  1486
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1662
  1487
        _this->gl_config.accum_alpha_size = value;
slouken@1662
  1488
        break;
slouken@1662
  1489
    case SDL_GL_STEREO:
slouken@1662
  1490
        _this->gl_config.stereo = value;
slouken@1662
  1491
        break;
slouken@1662
  1492
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@1662
  1493
        _this->gl_config.multisamplebuffers = value;
slouken@1662
  1494
        break;
slouken@1662
  1495
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@1662
  1496
        _this->gl_config.multisamplesamples = value;
slouken@1662
  1497
        break;
slouken@1662
  1498
    case SDL_GL_ACCELERATED_VISUAL:
slouken@1662
  1499
        _this->gl_config.accelerated = value;
slouken@1662
  1500
        break;
slouken@1662
  1501
    case SDL_GL_SWAP_CONTROL:
slouken@1662
  1502
        _this->gl_config.swap_control = value;
slouken@1662
  1503
        break;
slouken@1662
  1504
    default:
slouken@1668
  1505
        SDL_SetError("Unknown OpenGL attribute");
slouken@1662
  1506
        retval = -1;
slouken@1662
  1507
        break;
slouken@1662
  1508
    }
slouken@1662
  1509
    return (retval);
slouken@0
  1510
}
slouken@0
  1511
slouken@0
  1512
/* Retrieve an attribute value from the windowing system. */
slouken@1662
  1513
int
slouken@1668
  1514
SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
slouken@0
  1515
{
slouken@1662
  1516
    int retval = -1;
slouken@0
  1517
slouken@1662
  1518
    if (_this->GL_GetAttribute) {
slouken@1668
  1519
        retval = _this->GL_GetAttribute(_this, attr, value);
slouken@1662
  1520
    } else {
slouken@1662
  1521
        *value = 0;
slouken@1668
  1522
        SDL_SetError("GL_GetAttribute not supported");
slouken@1662
  1523
    }
slouken@1662
  1524
    return retval;
slouken@0
  1525
}
slouken@0
  1526
slouken@0
  1527
/* Perform a GL buffer swap on the current GL context */
slouken@1662
  1528
void
slouken@1668
  1529
SDL_GL_SwapBuffers(void)
slouken@0
  1530
{
slouken@1662
  1531
    if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL) {
slouken@1668
  1532
        _this->GL_SwapBuffers(_this);
slouken@1662
  1533
    } else {
slouken@1668
  1534
        SDL_SetError("OpenGL video mode has not been set");
slouken@1662
  1535
    }
slouken@0
  1536
}
slouken@0
  1537
slouken@1662
  1538
#if 0                           // FIXME
slouken@541
  1539
/* Utility function used by SDL_WM_SetIcon();
slouken@541
  1540
 * flags & 1 for color key, flags & 2 for alpha channel. */
slouken@1662
  1541
static void
slouken@1668
  1542
CreateMaskFromColorKeyOrAlpha(SDL_Surface * icon, Uint8 * mask, int flags)
slouken@0
  1543
{
slouken@1662
  1544
    int x, y;
slouken@1662
  1545
    Uint32 colorkey;
slouken@0
  1546
#define SET_MASKBIT(icon, x, y, mask) \
slouken@0
  1547
	mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
slouken@0
  1548
slouken@1662
  1549
    colorkey = icon->format->colorkey;
slouken@1662
  1550
    switch (icon->format->BytesPerPixel) {
slouken@1662
  1551
    case 1:
slouken@1662
  1552
        {
slouken@1662
  1553
            Uint8 *pixels;
slouken@1662
  1554
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1555
                pixels = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@1662
  1556
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1557
                    if (*pixels++ == colorkey) {
slouken@1668
  1558
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1559
                    }
slouken@1662
  1560
                }
slouken@1662
  1561
            }
slouken@1662
  1562
        }
slouken@1662
  1563
        break;
slouken@0
  1564
slouken@1662
  1565
    case 2:
slouken@1662
  1566
        {
slouken@1662
  1567
            Uint16 *pixels;
slouken@1662
  1568
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1569
                pixels = (Uint16 *) icon->pixels + y * icon->pitch / 2;
slouken@1662
  1570
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1571
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1572
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1573
                    } else if ((flags & 2)
slouken@1662
  1574
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1575
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1576
                    }
slouken@1662
  1577
                    pixels++;
slouken@1662
  1578
                }
slouken@1662
  1579
            }
slouken@1662
  1580
        }
slouken@1662
  1581
        break;
slouken@0
  1582
slouken@1662
  1583
    case 4:
slouken@1662
  1584
        {
slouken@1662
  1585
            Uint32 *pixels;
slouken@1662
  1586
            for (y = 0; y < icon->h; ++y) {
slouken@1662
  1587
                pixels = (Uint32 *) icon->pixels + y * icon->pitch / 4;
slouken@1662
  1588
                for (x = 0; x < icon->w; ++x) {
slouken@1662
  1589
                    if ((flags & 1) && *pixels == colorkey) {
slouken@1668
  1590
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1591
                    } else if ((flags & 2)
slouken@1662
  1592
                               && (*pixels & icon->format->Amask) == 0) {
slouken@1668
  1593
                        SET_MASKBIT(icon, x, y, mask);
slouken@1662
  1594
                    }
slouken@1662
  1595
                    pixels++;
slouken@1662
  1596
                }
slouken@1662
  1597
            }
slouken@1662
  1598
        }
slouken@1662
  1599
        break;
slouken@1662
  1600
    }
slouken@0
  1601
}
slouken@0
  1602
slouken@0
  1603
/*
slouken@0
  1604
 * Sets the window manager icon for the display window.
slouken@0
  1605
 */
slouken@1662
  1606
void
slouken@1668
  1607
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
slouken@0
  1608
{
slouken@1662
  1609
    if (icon && _this->SetIcon) {
slouken@1662
  1610
        /* Generate a mask if necessary, and create the icon! */
slouken@1662
  1611
        if (mask == NULL) {
slouken@1662
  1612
            int mask_len = icon->h * (icon->w + 7) / 8;
slouken@1662
  1613
            int flags = 0;
slouken@1668
  1614
            mask = (Uint8 *) SDL_malloc(mask_len);
slouken@1662
  1615
            if (mask == NULL) {
slouken@1662
  1616
                return;
slouken@1662
  1617
            }
slouken@1668
  1618
            SDL_memset(mask, ~0, mask_len);
slouken@1662
  1619
            if (icon->flags & SDL_SRCCOLORKEY)
slouken@1662
  1620
                flags |= 1;
slouken@1662
  1621
            if (icon->flags & SDL_SRCALPHA)
slouken@1662
  1622
                flags |= 2;
slouken@1662
  1623
            if (flags) {
slouken@1668
  1624
                CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
slouken@1662
  1625
            }
slouken@1668
  1626
            _this->SetIcon(_this, icon, mask);
slouken@1668
  1627
            SDL_free(mask);
slouken@1662
  1628
        } else {
slouken@1668
  1629
            _this->SetIcon(_this, icon, mask);
slouken@1662
  1630
        }
slouken@1662
  1631
    }
slouken@1662
  1632
}
slouken@1662
  1633
#endif
slouken@0
  1634
slouken@1662
  1635
SDL_bool
slouken@1668
  1636
SDL_GetWindowWMInfo(SDL_WindowID windowID, SDL_SysWMinfo * info)
slouken@1662
  1637
{
slouken@1668
  1638
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@1662
  1639
slouken@1662
  1640
    if (!window || !_this->GetWindowWMInfo) {
slouken@1662
  1641
        return SDL_FALSE;
slouken@1662
  1642
    }
slouken@1668
  1643
    return (_this->GetWindowWMInfo(_this, window, info));
slouken@0
  1644
}
slouken@0
  1645
slouken@1662
  1646
/* vi: set ts=4 sw=4 expandtab: */