src/video/SDL_video.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 27 Feb 2014 22:06:41 -0500
changeset 8265 1ed54c57d6c7
parent 8153 3e43c73ac79b
child 8266 d123fc5d02bc
permissions -rw-r--r--
Disable the screensaver by default.

Fixes Bugzilla #2218.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* The high-level video driver subsystem */
slouken@0
    24
slouken@2999
    25
#include "SDL.h"
slouken@2984
    26
#include "SDL_video.h"
slouken@0
    27
#include "SDL_sysvideo.h"
slouken@0
    28
#include "SDL_blit.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@5294
    30
#include "SDL_rect_c.h"
slouken@1361
    31
#include "../events/SDL_events_c.h"
urkle@7139
    32
#include "../timer/SDL_timer_c.h"
slouken@0
    33
slouken@5209
    34
#if SDL_VIDEO_OPENGL
slouken@5209
    35
#include "SDL_opengl.h"
slouken@5209
    36
#endif /* SDL_VIDEO_OPENGL */
slouken@5209
    37
hfutrell@2745
    38
#if SDL_VIDEO_OPENGL_ES
hfutrell@2745
    39
#include "SDL_opengles.h"
slouken@2753
    40
#endif /* SDL_VIDEO_OPENGL_ES */
hfutrell@2745
    41
gabomdq@7698
    42
/* GL and GLES2 headers conflict on Linux 32 bits */
gabomdq@7698
    43
#if SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL
slouken@5209
    44
#include "SDL_opengles2.h"
gabomdq@7698
    45
#endif /* SDL_VIDEO_OPENGL_ES2 && !SDL_VIDEO_OPENGL */
slouken@4900
    46
slouken@4900
    47
#include "SDL_syswm.h"
slouken@1926
    48
slouken@1926
    49
/* On Windows, windows.h defines CreateWindow */
slouken@1926
    50
#ifdef CreateWindow
slouken@1926
    51
#undef CreateWindow
slouken@1926
    52
#endif
slouken@1926
    53
slouken@0
    54
/* Available video drivers */
lestat@3166
    55
static VideoBootStrap *bootstrap[] = {
slouken@1931
    56
#if SDL_VIDEO_DRIVER_COCOA
slouken@2753
    57
    &COCOA_bootstrap,
icculus@1133
    58
#endif
slouken@1361
    59
#if SDL_VIDEO_DRIVER_X11
slouken@2753
    60
    &X11_bootstrap,
slouken@0
    61
#endif
icculus@8153
    62
#if SDL_VIDEO_DRIVER_MIR
icculus@8153
    63
    &MIR_bootstrap,
icculus@8153
    64
#endif
slouken@1361
    65
#if SDL_VIDEO_DRIVER_DIRECTFB
slouken@2753
    66
    &DirectFB_bootstrap,
slouken@167
    67
#endif
slouken@5062
    68
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@5062
    69
    &WINDOWS_bootstrap,
slouken@1361
    70
#endif
icculus@7981
    71
#if SDL_VIDEO_DRIVER_HAIKU
icculus@7981
    72
    &HAIKU_bootstrap,
slouken@1361
    73
#endif
slouken@5150
    74
#if SDL_VIDEO_DRIVER_PANDORA
slouken@5150
    75
    &PND_bootstrap,
slouken@1361
    76
#endif
hfutrell@2745
    77
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2753
    78
    &UIKIT_bootstrap,
hfutrell@2745
    79
#endif
slouken@5150
    80
#if SDL_VIDEO_DRIVER_ANDROID
slouken@5150
    81
    &Android_bootstrap,
slouken@5150
    82
#endif
kimonline@7009
    83
#if SDL_VIDEO_DRIVER_PSP
kimonline@7009
    84
    &PSP_bootstrap,
kimonline@7009
    85
#endif
gabomdq@7753
    86
#if SDL_VIDEO_DRIVER_RPI
gabomdq@7753
    87
    &RPI_bootstrap,
gabomdq@7753
    88
#endif 
gabomdq@8117
    89
#if SDL_VIDEO_DRIVER_WAYLAND
gabomdq@8117
    90
    &Wayland_bootstrap,
gabomdq@8117
    91
#endif
slouken@1361
    92
#if SDL_VIDEO_DRIVER_DUMMY
slouken@2753
    93
    &DUMMY_bootstrap,
slouken@610
    94
#endif
slouken@2753
    95
    NULL
slouken@0
    96
};
slouken@173
    97
slouken@1895
    98
static SDL_VideoDevice *_this = NULL;
slouken@0
    99
slouken@3695
   100
#define CHECK_WINDOW_MAGIC(window, retval) \
slouken@3695
   101
    if (!_this) { \
slouken@3695
   102
        SDL_UninitializedVideo(); \
slouken@3695
   103
        return retval; \
slouken@3695
   104
    } \
slouken@3701
   105
    if (!window || window->magic != &_this->window_magic) { \
slouken@3695
   106
        SDL_SetError("Invalid window"); \
slouken@3695
   107
        return retval; \
slouken@3695
   108
    }
slouken@3695
   109
slouken@5244
   110
#define CHECK_DISPLAY_INDEX(displayIndex, retval) \
slouken@5244
   111
    if (!_this) { \
slouken@5244
   112
        SDL_UninitializedVideo(); \
slouken@5244
   113
        return retval; \
slouken@5244
   114
    } \
slouken@5244
   115
    if (displayIndex < 0 || displayIndex >= _this->num_displays) { \
slouken@5244
   116
        SDL_SetError("displayIndex must be in the range 0 - %d", \
slouken@5244
   117
                     _this->num_displays - 1); \
slouken@5244
   118
        return retval; \
slouken@5244
   119
    }
slouken@5244
   120
jorgen@8110
   121
#define FULLSCREEN_MASK ( SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN )
slouken@7968
   122
slouken@7968
   123
#ifdef __MACOSX__
slouken@7968
   124
/* Support for Mac OS X fullscreen spaces */
slouken@7969
   125
extern SDL_bool Cocoa_IsWindowInFullscreenSpace(SDL_Window * window);
slouken@7968
   126
extern SDL_bool Cocoa_SetWindowFullscreenSpace(SDL_Window * window, SDL_bool state);
slouken@7968
   127
#endif
slouken@7968
   128
slouken@7968
   129
slouken@5166
   130
/* Support for framebuffer emulation using an accelerated renderer */
slouken@5166
   131
slouken@5166
   132
#define SDL_WINDOWTEXTUREDATA   "_SDL_WindowTextureData"
slouken@5166
   133
slouken@5166
   134
typedef struct {
slouken@5166
   135
    SDL_Renderer *renderer;
slouken@5166
   136
    SDL_Texture *texture;
slouken@5166
   137
    void *pixels;
slouken@5166
   138
    int pitch;
slouken@5172
   139
    int bytes_per_pixel;
slouken@5166
   140
} SDL_WindowTextureData;
slouken@5166
   141
slouken@5190
   142
static SDL_bool
slouken@5190
   143
ShouldUseTextureFramebuffer()
slouken@5190
   144
{
slouken@5190
   145
    const char *hint;
slouken@5190
   146
slouken@5190
   147
    /* If there's no native framebuffer support then there's no option */
slouken@5190
   148
    if (!_this->CreateWindowFramebuffer) {
slouken@5190
   149
        return SDL_TRUE;
slouken@5190
   150
    }
slouken@5190
   151
slouken@5192
   152
    /* If the user has specified a software renderer we can't use a
slouken@5192
   153
       texture framebuffer, or renderer creation will go recursive.
slouken@5192
   154
     */
slouken@5192
   155
    hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
slouken@5192
   156
    if (hint && SDL_strcasecmp(hint, "software") == 0) {
slouken@5192
   157
        return SDL_FALSE;
slouken@5192
   158
    }
slouken@5192
   159
slouken@5190
   160
    /* See if the user or application wants a specific behavior */
slouken@5190
   161
    hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
slouken@5190
   162
    if (hint) {
slouken@5190
   163
        if (*hint == '0') {
slouken@5190
   164
            return SDL_FALSE;
slouken@5190
   165
        } else {
slouken@5190
   166
            return SDL_TRUE;
slouken@5190
   167
        }
slouken@5190
   168
    }
slouken@5190
   169
slouken@5190
   170
    /* Each platform has different performance characteristics */
slouken@5190
   171
#if defined(__WIN32__)
slouken@5190
   172
    /* GDI BitBlt() is way faster than Direct3D dynamic textures right now.
slouken@5190
   173
     */
slouken@5190
   174
    return SDL_FALSE;
slouken@5190
   175
slouken@5190
   176
#elif defined(__MACOSX__)
slouken@5190
   177
    /* Mac OS X uses OpenGL as the native fast path */
slouken@5190
   178
    return SDL_TRUE;
slouken@5190
   179
slouken@5190
   180
#elif defined(__LINUX__)
slouken@5190
   181
    /* Properly configured OpenGL drivers are faster than MIT-SHM */
slouken@5190
   182
#if SDL_VIDEO_OPENGL
slouken@5190
   183
    /* Ugh, find a way to cache this value! */
slouken@5190
   184
    {
slouken@5190
   185
        SDL_Window *window;
slouken@5190
   186
        SDL_GLContext context;
slouken@5190
   187
        SDL_bool hasAcceleratedOpenGL = SDL_FALSE;
slouken@5190
   188
slouken@5385
   189
        window = SDL_CreateWindow("OpenGL test", -32, -32, 32, 32, SDL_WINDOW_OPENGL|SDL_WINDOW_HIDDEN);
slouken@5190
   190
        if (window) {
slouken@5190
   191
            context = SDL_GL_CreateContext(window);
slouken@5190
   192
            if (context) {
slouken@5190
   193
                const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@5190
   194
                const char *vendor = NULL;
slouken@5190
   195
slouken@5190
   196
                glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
slouken@5190
   197
                if (glGetStringFunc) {
slouken@5190
   198
                    vendor = (const char *) glGetStringFunc(GL_VENDOR);
slouken@5190
   199
                }
slouken@5190
   200
                /* Add more vendors here at will... */
slouken@5190
   201
                if (vendor &&
slouken@5190
   202
                    (SDL_strstr(vendor, "ATI Technologies") ||
slouken@5190
   203
                     SDL_strstr(vendor, "NVIDIA"))) {
slouken@5190
   204
                    hasAcceleratedOpenGL = SDL_TRUE;
slouken@5190
   205
                }
slouken@5190
   206
                SDL_GL_DeleteContext(context);
slouken@5190
   207
            }
slouken@5190
   208
            SDL_DestroyWindow(window);
slouken@5190
   209
        }
slouken@5190
   210
        return hasAcceleratedOpenGL;
slouken@5190
   211
    }
slouken@6188
   212
#elif SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6188
   213
    /* Let's be optimistic about this! */
slouken@6188
   214
    return SDL_TRUE;
slouken@5190
   215
#else
slouken@5190
   216
    return SDL_FALSE;
slouken@5190
   217
#endif
slouken@5190
   218
slouken@5190
   219
#else
slouken@5190
   220
    /* Play it safe, assume that if there is a framebuffer driver that it's
slouken@5190
   221
       optimized for the current platform.
slouken@5190
   222
    */
slouken@5190
   223
    return SDL_FALSE;
slouken@5190
   224
#endif
slouken@5190
   225
}
slouken@5190
   226
slouken@5166
   227
static int
slouken@5166
   228
SDL_CreateWindowTexture(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
slouken@5166
   229
{
slouken@5166
   230
    SDL_WindowTextureData *data;
slouken@5166
   231
    SDL_RendererInfo info;
slouken@5166
   232
    Uint32 i;
slouken@5166
   233
slouken@5166
   234
    data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
slouken@5166
   235
    if (!data) {
slouken@5291
   236
        SDL_Renderer *renderer = NULL;
slouken@5168
   237
        SDL_RendererInfo info;
slouken@5168
   238
        int i;
slouken@5191
   239
        const char *hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);
slouken@5168
   240
slouken@5191
   241
        /* Check to see if there's a specific driver requested */
slouken@5191
   242
        if (hint && *hint != '0' && *hint != '1' &&
slouken@5191
   243
            SDL_strcasecmp(hint, "software") != 0) {
slouken@5191
   244
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
slouken@5191
   245
                SDL_GetRenderDriverInfo(i, &info);
slouken@5191
   246
                if (SDL_strcasecmp(info.name, hint) == 0) {
slouken@5191
   247
                    renderer = SDL_CreateRenderer(window, i, 0);
slouken@5168
   248
                    break;
slouken@5168
   249
                }
slouken@5168
   250
            }
slouken@5168
   251
        }
slouken@5191
   252
slouken@5191
   253
        if (!renderer) {
slouken@5191
   254
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
slouken@5191
   255
                SDL_GetRenderDriverInfo(i, &info);
slouken@5191
   256
                if (SDL_strcmp(info.name, "software") != 0) {
slouken@5191
   257
                    renderer = SDL_CreateRenderer(window, i, 0);
slouken@5191
   258
                    if (renderer) {
slouken@5191
   259
                        break;
slouken@5191
   260
                    }
slouken@5191
   261
                }
slouken@5191
   262
            }
slouken@5191
   263
        }
slouken@5166
   264
        if (!renderer) {
icculus@7037
   265
            return SDL_SetError("No hardware accelerated renderers available");
slouken@5166
   266
        }
slouken@5275
   267
slouken@5275
   268
        /* Create the data after we successfully create the renderer (bug #1116) */
slouken@5275
   269
        data = (SDL_WindowTextureData *)SDL_calloc(1, sizeof(*data));
slouken@5275
   270
        if (!data) {
slouken@5275
   271
            SDL_DestroyRenderer(renderer);
icculus@7037
   272
            return SDL_OutOfMemory();
slouken@5275
   273
        }
slouken@5275
   274
        SDL_SetWindowData(window, SDL_WINDOWTEXTUREDATA, data);
slouken@5275
   275
slouken@5166
   276
        data->renderer = renderer;
slouken@5166
   277
    }
slouken@5166
   278
slouken@5166
   279
    /* Free any old texture and pixel data */
slouken@5166
   280
    if (data->texture) {
slouken@5166
   281
        SDL_DestroyTexture(data->texture);
slouken@5166
   282
        data->texture = NULL;
slouken@5166
   283
    }
slouken@7719
   284
    SDL_free(data->pixels);
slouken@7719
   285
    data->pixels = NULL;
slouken@5166
   286
slouken@5291
   287
    if (SDL_GetRendererInfo(data->renderer, &info) < 0) {
slouken@5166
   288
        return -1;
slouken@5166
   289
    }
slouken@5166
   290
slouken@5166
   291
    /* Find the first format without an alpha channel */
slouken@5166
   292
    *format = info.texture_formats[0];
slouken@5166
   293
    for (i = 0; i < info.num_texture_formats; ++i) {
slouken@5268
   294
        if (!SDL_ISPIXELFORMAT_FOURCC(info.texture_formats[i]) &&
slouken@5268
   295
            !SDL_ISPIXELFORMAT_ALPHA(info.texture_formats[i])) {
slouken@5166
   296
            *format = info.texture_formats[i];
slouken@5166
   297
            break;
slouken@5166
   298
        }
slouken@5166
   299
    }
slouken@5166
   300
slouken@5291
   301
    data->texture = SDL_CreateTexture(data->renderer, *format,
slouken@5166
   302
                                      SDL_TEXTUREACCESS_STREAMING,
slouken@5166
   303
                                      window->w, window->h);
slouken@5166
   304
    if (!data->texture) {
slouken@5166
   305
        return -1;
slouken@5166
   306
    }
slouken@5166
   307
slouken@5166
   308
    /* Create framebuffer data */
slouken@5172
   309
    data->bytes_per_pixel = SDL_BYTESPERPIXEL(*format);
slouken@5172
   310
    data->pitch = (((window->w * data->bytes_per_pixel) + 3) & ~3);
slouken@5166
   311
    data->pixels = SDL_malloc(window->h * data->pitch);
slouken@5166
   312
    if (!data->pixels) {
icculus@7037
   313
        return SDL_OutOfMemory();
slouken@5166
   314
    }
slouken@5166
   315
slouken@5166
   316
    *pixels = data->pixels;
slouken@5166
   317
    *pitch = data->pitch;
slouken@5297
   318
slouken@5297
   319
    /* Make sure we're not double-scaling the viewport */
slouken@5297
   320
    SDL_RenderSetViewport(data->renderer, NULL);
slouken@5297
   321
slouken@5166
   322
    return 0;
slouken@5166
   323
}
slouken@5166
   324
slouken@5166
   325
static int
slouken@7014
   326
SDL_UpdateWindowTexture(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
slouken@5166
   327
{
slouken@5166
   328
    SDL_WindowTextureData *data;
slouken@5294
   329
    SDL_Rect rect;
slouken@5294
   330
    void *src;
slouken@5166
   331
slouken@5166
   332
    data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
slouken@5166
   333
    if (!data || !data->texture) {
icculus@7037
   334
        return SDL_SetError("No window texture data");
slouken@5166
   335
    }
slouken@5166
   336
slouken@5294
   337
    /* Update a single rect that contains subrects for best DMA performance */
slouken@5294
   338
    if (SDL_GetSpanEnclosingRect(window->w, window->h, numrects, rects, &rect)) {
slouken@5172
   339
        src = (void *)((Uint8 *)data->pixels +
slouken@5294
   340
                        rect.y * data->pitch +
slouken@5294
   341
                        rect.x * data->bytes_per_pixel);
slouken@5294
   342
        if (SDL_UpdateTexture(data->texture, &rect, src, data->pitch) < 0) {
slouken@5172
   343
            return -1;
slouken@5172
   344
        }
slouken@5294
   345
slouken@5294
   346
        if (SDL_RenderCopy(data->renderer, data->texture, NULL, NULL) < 0) {
slouken@5294
   347
            return -1;
slouken@5172
   348
        }
slouken@5294
   349
slouken@5294
   350
        SDL_RenderPresent(data->renderer);
slouken@5172
   351
    }
slouken@5166
   352
    return 0;
slouken@5166
   353
}
slouken@5166
   354
slouken@5166
   355
static void
slouken@5166
   356
SDL_DestroyWindowTexture(_THIS, SDL_Window * window)
slouken@5166
   357
{
slouken@5166
   358
    SDL_WindowTextureData *data;
slouken@5166
   359
slouken@5166
   360
    data = SDL_SetWindowData(window, SDL_WINDOWTEXTUREDATA, NULL);
slouken@5166
   361
    if (!data) {
slouken@5166
   362
        return;
slouken@5166
   363
    }
slouken@5166
   364
    if (data->texture) {
slouken@5166
   365
        SDL_DestroyTexture(data->texture);
slouken@5166
   366
    }
slouken@5166
   367
    if (data->renderer) {
slouken@5166
   368
        SDL_DestroyRenderer(data->renderer);
slouken@5166
   369
    }
slouken@7719
   370
    SDL_free(data->pixels);
slouken@5166
   371
    SDL_free(data);
slouken@5166
   372
}
slouken@5166
   373
slouken@5166
   374
slouken@1895
   375
static int
slouken@1895
   376
cmpmodes(const void *A, const void *B)
slouken@1895
   377
{
icculus@7440
   378
    const SDL_DisplayMode *a = (const SDL_DisplayMode *) A;
icculus@7440
   379
    const SDL_DisplayMode *b = (const SDL_DisplayMode *) B;
icculus@7440
   380
    if (a == b) {
icculus@7440
   381
        return 0;
icculus@7440
   382
    } else if (a->w != b->w) {
icculus@7440
   383
        return b->w - a->w;
icculus@7440
   384
    } else if (a->h != b->h) {
icculus@7440
   385
        return b->h - a->h;
icculus@7440
   386
    } else if (SDL_BITSPERPIXEL(a->format) != SDL_BITSPERPIXEL(b->format)) {
icculus@7440
   387
        return SDL_BITSPERPIXEL(b->format) - SDL_BITSPERPIXEL(a->format);
icculus@7440
   388
    } else if (SDL_PIXELLAYOUT(a->format) != SDL_PIXELLAYOUT(b->format)) {
icculus@7440
   389
        return SDL_PIXELLAYOUT(b->format) - SDL_PIXELLAYOUT(a->format);
icculus@7440
   390
    } else if (a->refresh_rate != b->refresh_rate) {
icculus@7440
   391
        return b->refresh_rate - a->refresh_rate;
slouken@2753
   392
    }
slouken@2753
   393
    return 0;
slouken@1895
   394
}
slouken@1895
   395
icculus@7037
   396
static int
slouken@1912
   397
SDL_UninitializedVideo()
slouken@1912
   398
{
icculus@7037
   399
    return SDL_SetError("Video subsystem has not been initialized");
slouken@1912
   400
}
slouken@1912
   401
slouken@1895
   402
int
slouken@1895
   403
SDL_GetNumVideoDrivers(void)
slouken@1895
   404
{
slouken@2753
   405
    return SDL_arraysize(bootstrap) - 1;
slouken@1895
   406
}
slouken@1895
   407
slouken@2753
   408
const char *
slouken@1895
   409
SDL_GetVideoDriver(int index)
slouken@1895
   410
{
slouken@2753
   411
    if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
slouken@2753
   412
        return bootstrap[index]->name;
slouken@2753
   413
    }
slouken@2753
   414
    return NULL;
slouken@1895
   415
}
slouken@0
   416
slouken@0
   417
/*
slouken@0
   418
 * Initialize the video and event subsystems -- determine native pixel format
slouken@0
   419
 */
slouken@1895
   420
int
slouken@5123
   421
SDL_VideoInit(const char *driver_name)
slouken@0
   422
{
slouken@2753
   423
    SDL_VideoDevice *video;
slouken@2753
   424
    int index;
slouken@2753
   425
    int i;
slouken@0
   426
slouken@3694
   427
    /* Check to make sure we don't overwrite '_this' */
slouken@3694
   428
    if (_this != NULL) {
slouken@3694
   429
        SDL_VideoQuit();
slouken@3694
   430
    }
slouken@7191
   431
urkle@7139
   432
#if !SDL_TIMERS_DISABLED
urkle@7139
   433
    SDL_InitTicks();
urkle@7139
   434
#endif
slouken@3694
   435
slouken@2753
   436
    /* Start the event loop */
slouken@7360
   437
    if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0 ||
slouken@5123
   438
        SDL_KeyboardInit() < 0 ||
slouken@5123
   439
        SDL_MouseInit() < 0 ||
slouken@7360
   440
        SDL_TouchInit() < 0) {
slouken@2753
   441
        return -1;
slouken@2753
   442
    }
slouken@3694
   443
slouken@2753
   444
    /* Select the proper video driver */
slouken@2753
   445
    index = 0;
slouken@2753
   446
    video = NULL;
slouken@2753
   447
    if (driver_name == NULL) {
slouken@2753
   448
        driver_name = SDL_getenv("SDL_VIDEODRIVER");
slouken@2753
   449
    }
slouken@2753
   450
    if (driver_name != NULL) {
slouken@2753
   451
        for (i = 0; bootstrap[i]; ++i) {
slouken@6900
   452
            if (SDL_strncasecmp(bootstrap[i]->name, driver_name, SDL_strlen(driver_name)) == 0) {
gabomdq@8117
   453
                if (bootstrap[i]->available()) {
gabomdq@8117
   454
                    video = bootstrap[i]->create(index);
gabomdq@8117
   455
                    break;
gabomdq@8117
   456
                }
slouken@2753
   457
            }
slouken@2753
   458
        }
slouken@2753
   459
    } else {
slouken@2753
   460
        for (i = 0; bootstrap[i]; ++i) {
slouken@2753
   461
            if (bootstrap[i]->available()) {
slouken@2753
   462
                video = bootstrap[i]->create(index);
slouken@2753
   463
                if (video != NULL) {
slouken@2753
   464
                    break;
slouken@2753
   465
                }
slouken@2753
   466
            }
slouken@2753
   467
        }
slouken@2753
   468
    }
slouken@2753
   469
    if (video == NULL) {
slouken@2753
   470
        if (driver_name) {
icculus@7037
   471
            return SDL_SetError("%s not available", driver_name);
slouken@2753
   472
        }
icculus@7037
   473
        return SDL_SetError("No available video device");
slouken@2753
   474
    }
slouken@2753
   475
    _this = video;
slouken@2753
   476
    _this->name = bootstrap[i]->name;
slouken@2753
   477
    _this->next_object_id = 1;
slouken@0
   478
slouken@0
   479
slouken@2753
   480
    /* Set some very sane GL defaults */
slouken@2753
   481
    _this->gl_config.driver_loaded = 0;
slouken@2753
   482
    _this->gl_config.dll_handle = NULL;
jorgen@8145
   483
    SDL_GL_ResetAttributes();
slouken@0
   484
slouken@7412
   485
    _this->current_glwin_tls = SDL_TLSCreate();
slouken@7412
   486
    _this->current_glctx_tls = SDL_TLSCreate();
slouken@7412
   487
slouken@2753
   488
    /* Initialize the video subsystem */
slouken@2753
   489
    if (_this->VideoInit(_this) < 0) {
slouken@2753
   490
        SDL_VideoQuit();
slouken@2753
   491
        return -1;
slouken@2753
   492
    }
slouken@5166
   493
slouken@2753
   494
    /* Make sure some displays were added */
slouken@2753
   495
    if (_this->num_displays == 0) {
slouken@2753
   496
        SDL_VideoQuit();
icculus@7037
   497
        return SDL_SetError("The video driver did not add any displays");
slouken@2753
   498
    }
slouken@0
   499
slouken@5190
   500
    /* Add the renderer framebuffer emulation if desired */
slouken@5190
   501
    if (ShouldUseTextureFramebuffer()) {
slouken@5166
   502
        _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
slouken@5166
   503
        _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
slouken@5166
   504
        _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
slouken@5166
   505
    }
slouken@5166
   506
icculus@8265
   507
    /* Disable the screen saver by default. This is a change from <= 2.0.1,
icculus@8265
   508
       but most things using SDL are games or media players; you wouldn't
icculus@8265
   509
       want a screensaver to trigger if you're playing exclusively with a
icculus@8265
   510
       joystick, or passively watching a movie. Things that use SDL but
icculus@8265
   511
       function more like a normal desktop app should explicitly reenable the
icculus@8265
   512
       screensaver. */
icculus@8265
   513
    SDL_DisableScreenSaver();
icculus@8265
   514
slouken@6670
   515
    /* If we don't use a screen keyboard, turn on text input by default,
slouken@6670
   516
       otherwise programs that expect to get text events without enabling
slouken@6670
   517
       UNICODE input won't get any events.
slouken@6670
   518
slouken@6670
   519
       Actually, come to think of it, you needed to call SDL_EnableUNICODE(1)
slouken@6670
   520
       in SDL 1.2 before you got text input events.  Hmm...
slouken@6670
   521
     */
slouken@6670
   522
    if (!SDL_HasScreenKeyboardSupport()) {
slouken@6670
   523
        SDL_StartTextInput();
slouken@6670
   524
    }
slouken@6670
   525
slouken@2753
   526
    /* We're ready to go! */
slouken@2753
   527
    return 0;
slouken@0
   528
}
slouken@0
   529
slouken@2753
   530
const char *
slouken@1895
   531
SDL_GetCurrentVideoDriver()
slouken@0
   532
{
slouken@2753
   533
    if (!_this) {
slouken@2753
   534
        SDL_UninitializedVideo();
slouken@2753
   535
        return NULL;
slouken@2753
   536
    }
slouken@2753
   537
    return _this->name;
slouken@0
   538
}
slouken@0
   539
slouken@1895
   540
SDL_VideoDevice *
slouken@4472
   541
SDL_GetVideoDevice(void)
slouken@0
   542
{
slouken@2753
   543
    return _this;
slouken@0
   544
}
slouken@0
   545
slouken@1895
   546
int
slouken@1895
   547
SDL_AddBasicVideoDisplay(const SDL_DisplayMode * desktop_mode)
slouken@0
   548
{
slouken@2753
   549
    SDL_VideoDisplay display;
slouken@0
   550
slouken@2753
   551
    SDL_zero(display);
slouken@2753
   552
    if (desktop_mode) {
slouken@2753
   553
        display.desktop_mode = *desktop_mode;
slouken@2753
   554
    }
slouken@2753
   555
    display.current_mode = display.desktop_mode;
slouken@1895
   556
slouken@2753
   557
    return SDL_AddVideoDisplay(&display);
slouken@0
   558
}
slouken@0
   559
slouken@1895
   560
int
slouken@1895
   561
SDL_AddVideoDisplay(const SDL_VideoDisplay * display)
slouken@0
   562
{
slouken@2753
   563
    SDL_VideoDisplay *displays;
slouken@2753
   564
    int index = -1;
slouken@0
   565
slouken@2753
   566
    displays =
slouken@2753
   567
        SDL_realloc(_this->displays,
slouken@2753
   568
                    (_this->num_displays + 1) * sizeof(*displays));
slouken@2753
   569
    if (displays) {
slouken@2753
   570
        index = _this->num_displays++;
slouken@2753
   571
        displays[index] = *display;
slouken@2753
   572
        displays[index].device = _this;
slouken@2753
   573
        _this->displays = displays;
slouken@6787
   574
slouken@6787
   575
        if (display->name) {
slouken@6787
   576
            displays[index].name = SDL_strdup(display->name);
slouken@6787
   577
        } else {
slouken@6787
   578
            char name[32];
slouken@6787
   579
slouken@6787
   580
            SDL_itoa(index, name, 10);
slouken@6787
   581
            displays[index].name = SDL_strdup(name);
slouken@6787
   582
        }
slouken@2753
   583
    } else {
slouken@2753
   584
        SDL_OutOfMemory();
slouken@2753
   585
    }
slouken@2753
   586
    return index;
slouken@0
   587
}
slouken@0
   588
slouken@1895
   589
int
slouken@1895
   590
SDL_GetNumVideoDisplays(void)
slouken@0
   591
{
slouken@2753
   592
    if (!_this) {
slouken@2753
   593
        SDL_UninitializedVideo();
slouken@2753
   594
        return 0;
slouken@2753
   595
    }
slouken@2753
   596
    return _this->num_displays;
slouken@0
   597
}
slouken@0
   598
slouken@6044
   599
static int
slouken@5380
   600
SDL_GetIndexOfDisplay(SDL_VideoDisplay *display)
slouken@5380
   601
{
slouken@5380
   602
    int displayIndex;
slouken@5380
   603
slouken@5380
   604
    for (displayIndex = 0; displayIndex < _this->num_displays; ++displayIndex) {
slouken@5380
   605
        if (display == &_this->displays[displayIndex]) {
slouken@5380
   606
            return displayIndex;
slouken@5380
   607
        }
slouken@5380
   608
    }
slouken@5380
   609
slouken@5380
   610
    /* Couldn't find the display, just use index 0 */
slouken@5380
   611
    return 0;
slouken@5380
   612
}
slouken@5380
   613
slouken@7742
   614
void *
slouken@7742
   615
SDL_GetDisplayDriverData( int displayIndex )
slouken@7742
   616
{
slouken@7941
   617
    CHECK_DISPLAY_INDEX( displayIndex, NULL );
slouken@7941
   618
slouken@7941
   619
    return _this->displays[displayIndex].driverdata;
slouken@7742
   620
}
slouken@7742
   621
slouken@6787
   622
const char *
slouken@6787
   623
SDL_GetDisplayName(int displayIndex)
slouken@6787
   624
{
slouken@6787
   625
    CHECK_DISPLAY_INDEX(displayIndex, NULL);
slouken@6787
   626
slouken@6787
   627
    return _this->displays[displayIndex].name;
slouken@6787
   628
}
slouken@6787
   629
slouken@5380
   630
int
slouken@5244
   631
SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
slouken@3528
   632
{
slouken@5244
   633
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   634
slouken@3528
   635
    if (rect) {
slouken@5244
   636
        SDL_VideoDisplay *display = &_this->displays[displayIndex];
slouken@3528
   637
slouken@3528
   638
        if (_this->GetDisplayBounds) {
slouken@5249
   639
            if (_this->GetDisplayBounds(_this, display, rect) == 0) {
slouken@5249
   640
                return 0;
slouken@3528
   641
            }
slouken@5249
   642
        }
slouken@5249
   643
slouken@5249
   644
        /* Assume that the displays are left to right */
slouken@5249
   645
        if (displayIndex == 0) {
slouken@5249
   646
            rect->x = 0;
slouken@5249
   647
            rect->y = 0;
slouken@3528
   648
        } else {
slouken@5249
   649
            SDL_GetDisplayBounds(displayIndex-1, rect);
slouken@5249
   650
            rect->x += rect->w;
slouken@3528
   651
        }
slouken@6518
   652
        rect->w = display->current_mode.w;
slouken@6518
   653
        rect->h = display->current_mode.h;
slouken@3528
   654
    }
slouken@3528
   655
    return 0;
slouken@3528
   656
}
slouken@3528
   657
slouken@1895
   658
SDL_bool
slouken@3500
   659
SDL_AddDisplayMode(SDL_VideoDisplay * display,  const SDL_DisplayMode * mode)
slouken@0
   660
{
slouken@2753
   661
    SDL_DisplayMode *modes;
slouken@2753
   662
    int i, nmodes;
slouken@0
   663
slouken@2753
   664
    /* Make sure we don't already have the mode in the list */
slouken@2753
   665
    modes = display->display_modes;
slouken@2753
   666
    nmodes = display->num_display_modes;
icculus@7441
   667
    for (i = 0; i < nmodes; ++i) {
icculus@7439
   668
        if (cmpmodes(mode, &modes[i]) == 0) {
slouken@2753
   669
            return SDL_FALSE;
slouken@2753
   670
        }
slouken@2753
   671
    }
slouken@1895
   672
slouken@2753
   673
    /* Go ahead and add the new mode */
slouken@2753
   674
    if (nmodes == display->max_display_modes) {
slouken@2753
   675
        modes =
slouken@2753
   676
            SDL_realloc(modes,
slouken@2753
   677
                        (display->max_display_modes + 32) * sizeof(*modes));
slouken@2753
   678
        if (!modes) {
slouken@2753
   679
            return SDL_FALSE;
slouken@2753
   680
        }
slouken@2753
   681
        display->display_modes = modes;
slouken@2753
   682
        display->max_display_modes += 32;
slouken@2753
   683
    }
slouken@2753
   684
    modes[nmodes] = *mode;
slouken@2753
   685
    display->num_display_modes++;
slouken@1895
   686
lestat@3178
   687
    /* Re-sort video modes */
lestat@3178
   688
    SDL_qsort(display->display_modes, display->num_display_modes,
slouken@3186
   689
              sizeof(SDL_DisplayMode), cmpmodes);
lestat@3178
   690
slouken@2753
   691
    return SDL_TRUE;
slouken@0
   692
}
slouken@0
   693
slouken@5244
   694
static int
slouken@3500
   695
SDL_GetNumDisplayModesForDisplay(SDL_VideoDisplay * display)
slouken@3500
   696
{
slouken@3500
   697
    if (!display->num_display_modes && _this->GetDisplayModes) {
slouken@3500
   698
        _this->GetDisplayModes(_this, display);
slouken@3500
   699
        SDL_qsort(display->display_modes, display->num_display_modes,
slouken@3500
   700
                  sizeof(SDL_DisplayMode), cmpmodes);
slouken@3500
   701
    }
slouken@3500
   702
    return display->num_display_modes;
slouken@3500
   703
}
slouken@3500
   704
slouken@3500
   705
int
slouken@5244
   706
SDL_GetNumDisplayModes(int displayIndex)
slouken@0
   707
{
slouken@5244
   708
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   709
slouken@5244
   710
    return SDL_GetNumDisplayModesForDisplay(&_this->displays[displayIndex]);
slouken@3500
   711
}
slouken@3500
   712
slouken@3500
   713
int
slouken@5244
   714
SDL_GetDisplayMode(int displayIndex, int index, SDL_DisplayMode * mode)
slouken@3500
   715
{
slouken@5244
   716
    SDL_VideoDisplay *display;
slouken@5244
   717
slouken@5244
   718
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   719
slouken@5244
   720
    display = &_this->displays[displayIndex];
slouken@3500
   721
    if (index < 0 || index >= SDL_GetNumDisplayModesForDisplay(display)) {
icculus@7037
   722
        return SDL_SetError("index must be in the range of 0 - %d",
icculus@7037
   723
                            SDL_GetNumDisplayModesForDisplay(display) - 1);
slouken@3500
   724
    }
slouken@3500
   725
    if (mode) {
slouken@3500
   726
        *mode = display->display_modes[index];
slouken@2753
   727
    }
slouken@2753
   728
    return 0;
slouken@0
   729
}
slouken@0
   730
slouken@1967
   731
int
slouken@5244
   732
SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode * mode)
slouken@0
   733
{
slouken@5244
   734
    SDL_VideoDisplay *display;
slouken@3500
   735
slouken@5244
   736
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   737
slouken@5244
   738
    display = &_this->displays[displayIndex];
slouken@2753
   739
    if (mode) {
slouken@3500
   740
        *mode = display->desktop_mode;
slouken@2753
   741
    }
slouken@2753
   742
    return 0;
slouken@0
   743
}
slouken@0
   744
slouken@1967
   745
int
slouken@5244
   746
SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode * mode)
slouken@0
   747
{
slouken@5244
   748
    SDL_VideoDisplay *display;
slouken@3500
   749
slouken@5244
   750
    CHECK_DISPLAY_INDEX(displayIndex, -1);
slouken@5244
   751
slouken@5244
   752
    display = &_this->displays[displayIndex];
slouken@2753
   753
    if (mode) {
slouken@3500
   754
        *mode = display->current_mode;
slouken@2753
   755
    }
slouken@2753
   756
    return 0;
slouken@0
   757
}
slouken@0
   758
slouken@5244
   759
static SDL_DisplayMode *
slouken@3500
   760
SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay * display,
slouken@3500
   761
                                    const SDL_DisplayMode * mode,
slouken@3500
   762
                                    SDL_DisplayMode * closest)
slouken@0
   763
{
slouken@2753
   764
    Uint32 target_format;
slouken@2753
   765
    int target_refresh_rate;
slouken@2753
   766
    int i;
slouken@2753
   767
    SDL_DisplayMode *current, *match;
slouken@0
   768
slouken@3500
   769
    if (!mode || !closest) {
slouken@3500
   770
        SDL_SetError("Missing desired mode or closest mode parameter");
slouken@2753
   771
        return NULL;
slouken@2753
   772
    }
slouken@3500
   773
slouken@2753
   774
    /* Default to the desktop format */
slouken@2753
   775
    if (mode->format) {
slouken@2753
   776
        target_format = mode->format;
slouken@2753
   777
    } else {
slouken@3500
   778
        target_format = display->desktop_mode.format;
slouken@2753
   779
    }
slouken@0
   780
slouken@2753
   781
    /* Default to the desktop refresh rate */
slouken@2753
   782
    if (mode->refresh_rate) {
slouken@2753
   783
        target_refresh_rate = mode->refresh_rate;
slouken@2753
   784
    } else {
slouken@3500
   785
        target_refresh_rate = display->desktop_mode.refresh_rate;
slouken@2753
   786
    }
slouken@0
   787
slouken@2753
   788
    match = NULL;
slouken@3500
   789
    for (i = 0; i < SDL_GetNumDisplayModesForDisplay(display); ++i) {
slouken@3500
   790
        current = &display->display_modes[i];
slouken@0
   791
slouken@2789
   792
        if (current->w && (current->w < mode->w)) {
slouken@2753
   793
            /* Out of sorted modes large enough here */
slouken@2753
   794
            break;
slouken@2753
   795
        }
slouken@2789
   796
        if (current->h && (current->h < mode->h)) {
slouken@2789
   797
            if (current->w && (current->w == mode->w)) {
slouken@2789
   798
                /* Out of sorted modes large enough here */
slouken@2789
   799
                break;
slouken@2789
   800
            }
slouken@2789
   801
            /* Wider, but not tall enough, due to a different
slouken@2789
   802
               aspect ratio. This mode must be skipped, but closer
slouken@2789
   803
               modes may still follow. */
slouken@2789
   804
            continue;
slouken@2789
   805
        }
slouken@2753
   806
        if (!match || current->w < match->w || current->h < match->h) {
slouken@2753
   807
            match = current;
slouken@2753
   808
            continue;
slouken@2753
   809
        }
slouken@2753
   810
        if (current->format != match->format) {
slouken@2753
   811
            /* Sorted highest depth to lowest */
slouken@2753
   812
            if (current->format == target_format ||
slouken@2753
   813
                (SDL_BITSPERPIXEL(current->format) >=
slouken@2753
   814
                 SDL_BITSPERPIXEL(target_format)
slouken@2753
   815
                 && SDL_PIXELTYPE(current->format) ==
slouken@2753
   816
                 SDL_PIXELTYPE(target_format))) {
slouken@2753
   817
                match = current;
slouken@2753
   818
            }
slouken@2753
   819
            continue;
slouken@2753
   820
        }
slouken@2753
   821
        if (current->refresh_rate != match->refresh_rate) {
slouken@2753
   822
            /* Sorted highest refresh to lowest */
slouken@2753
   823
            if (current->refresh_rate >= target_refresh_rate) {
slouken@2753
   824
                match = current;
slouken@2753
   825
            }
slouken@2753
   826
        }
slouken@2753
   827
    }
slouken@2753
   828
    if (match) {
slouken@2753
   829
        if (match->format) {
slouken@2753
   830
            closest->format = match->format;
slouken@2753
   831
        } else {
slouken@2753
   832
            closest->format = mode->format;
slouken@2753
   833
        }
slouken@2753
   834
        if (match->w && match->h) {
slouken@2753
   835
            closest->w = match->w;
slouken@2753
   836
            closest->h = match->h;
slouken@2753
   837
        } else {
slouken@2753
   838
            closest->w = mode->w;
slouken@2753
   839
            closest->h = mode->h;
slouken@2753
   840
        }
slouken@2753
   841
        if (match->refresh_rate) {
slouken@2753
   842
            closest->refresh_rate = match->refresh_rate;
slouken@2753
   843
        } else {
slouken@2753
   844
            closest->refresh_rate = mode->refresh_rate;
slouken@2753
   845
        }
slouken@2753
   846
        closest->driverdata = match->driverdata;
slouken@1895
   847
slouken@2753
   848
        /*
slouken@2753
   849
         * Pick some reasonable defaults if the app and driver don't
slouken@2753
   850
         * care
slouken@2753
   851
         */
slouken@2753
   852
        if (!closest->format) {
slouken@2753
   853
            closest->format = SDL_PIXELFORMAT_RGB888;
slouken@2753
   854
        }
slouken@2753
   855
        if (!closest->w) {
slouken@2753
   856
            closest->w = 640;
slouken@2753
   857
        }
slouken@2753
   858
        if (!closest->h) {
slouken@2753
   859
            closest->h = 480;
slouken@2753
   860
        }
slouken@2753
   861
        return closest;
slouken@2753
   862
    }
slouken@2753
   863
    return NULL;
slouken@0
   864
}
slouken@0
   865
slouken@3500
   866
SDL_DisplayMode *
slouken@5244
   867
SDL_GetClosestDisplayMode(int displayIndex,
slouken@5244
   868
                          const SDL_DisplayMode * mode,
slouken@3500
   869
                          SDL_DisplayMode * closest)
slouken@3500
   870
{
slouken@5244
   871
    SDL_VideoDisplay *display;
slouken@5244
   872
slouken@5244
   873
    CHECK_DISPLAY_INDEX(displayIndex, NULL);
slouken@5244
   874
slouken@5244
   875
    display = &_this->displays[displayIndex];
slouken@5244
   876
    return SDL_GetClosestDisplayModeForDisplay(display, mode, closest);
slouken@3500
   877
}
slouken@3500
   878
slouken@6044
   879
static int
slouken@3500
   880
SDL_SetDisplayModeForDisplay(SDL_VideoDisplay * display, const SDL_DisplayMode * mode)
slouken@0
   881
{
slouken@2753
   882
    SDL_DisplayMode display_mode;
slouken@2753
   883
    SDL_DisplayMode current_mode;
slouken@0
   884
slouken@3502
   885
    if (mode) {
slouken@3502
   886
        display_mode = *mode;
slouken@3502
   887
slouken@3502
   888
        /* Default to the current mode */
slouken@3502
   889
        if (!display_mode.format) {
slouken@3502
   890
            display_mode.format = display->current_mode.format;
slouken@3502
   891
        }
slouken@3502
   892
        if (!display_mode.w) {
slouken@3502
   893
            display_mode.w = display->current_mode.w;
slouken@3502
   894
        }
slouken@3502
   895
        if (!display_mode.h) {
slouken@3502
   896
            display_mode.h = display->current_mode.h;
slouken@3502
   897
        }
slouken@3502
   898
        if (!display_mode.refresh_rate) {
slouken@3502
   899
            display_mode.refresh_rate = display->current_mode.refresh_rate;
slouken@3502
   900
        }
slouken@3502
   901
slouken@3502
   902
        /* Get a good video mode, the closest one possible */
slouken@3502
   903
        if (!SDL_GetClosestDisplayModeForDisplay(display, &display_mode, &display_mode)) {
icculus@7037
   904
            return SDL_SetError("No video mode large enough for %dx%d",
icculus@7037
   905
                                display_mode.w, display_mode.h);
slouken@3502
   906
        }
slouken@3502
   907
    } else {
slouken@3502
   908
        display_mode = display->desktop_mode;
slouken@2753
   909
    }
slouken@3500
   910
slouken@2753
   911
    /* See if there's anything left to do */
slouken@5244
   912
    current_mode = display->current_mode;
slouken@2753
   913
    if (SDL_memcmp(&display_mode, &current_mode, sizeof(display_mode)) == 0) {
slouken@2753
   914
        return 0;
slouken@2753
   915
    }
slouken@3500
   916
slouken@2753
   917
    /* Actually change the display mode */
slouken@4978
   918
    if (!_this->SetDisplayMode) {
icculus@7037
   919
        return SDL_SetError("Video driver doesn't support changing display mode");
slouken@4978
   920
    }
slouken@3500
   921
    if (_this->SetDisplayMode(_this, display, &display_mode) < 0) {
slouken@2753
   922
        return -1;
slouken@2753
   923
    }
slouken@2753
   924
    display->current_mode = display_mode;
slouken@2753
   925
    return 0;
slouken@0
   926
}
slouken@0
   927
slouken@1895
   928
int
slouken@6786
   929
SDL_GetWindowDisplayIndex(SDL_Window * window)
slouken@5246
   930
{
slouken@5246
   931
    int displayIndex;
slouken@5246
   932
    int i, dist;
slouken@5246
   933
    int closest = -1;
slouken@5246
   934
    int closest_dist = 0x7FFFFFFF;
slouken@5246
   935
    SDL_Point center;
slouken@5246
   936
    SDL_Point delta;
slouken@5246
   937
    SDL_Rect rect;
slouken@5246
   938
slouken@5246
   939
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5246
   940
slouken@5246
   941
    if (SDL_WINDOWPOS_ISUNDEFINED(window->x) ||
slouken@5246
   942
        SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@5246
   943
        displayIndex = (window->x & 0xFFFF);
slouken@5246
   944
        if (displayIndex >= _this->num_displays) {
slouken@5246
   945
            displayIndex = 0;
slouken@5246
   946
        }
slouken@5246
   947
        return displayIndex;
slouken@5246
   948
    }
slouken@5246
   949
    if (SDL_WINDOWPOS_ISUNDEFINED(window->y) ||
slouken@5246
   950
        SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@5246
   951
        displayIndex = (window->y & 0xFFFF);
slouken@5246
   952
        if (displayIndex >= _this->num_displays) {
slouken@5246
   953
            displayIndex = 0;
slouken@5246
   954
        }
slouken@5246
   955
        return displayIndex;
slouken@5246
   956
    }
slouken@5246
   957
slouken@5246
   958
    /* Find the display containing the window */
slouken@6547
   959
    for (i = 0; i < _this->num_displays; ++i) {
slouken@6547
   960
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@6547
   961
slouken@6547
   962
        if (display->fullscreen_window == window) {
slouken@6547
   963
            return i;
slouken@6547
   964
        }
slouken@6547
   965
    }
slouken@5246
   966
    center.x = window->x + window->w / 2;
slouken@5246
   967
    center.y = window->y + window->h / 2;
slouken@5246
   968
    for (i = 0; i < _this->num_displays; ++i) {
slouken@5246
   969
        SDL_GetDisplayBounds(i, &rect);
slouken@6547
   970
        if (SDL_EnclosePoints(&center, 1, &rect, NULL)) {
slouken@5246
   971
            return i;
slouken@5246
   972
        }
slouken@5246
   973
slouken@5246
   974
        delta.x = center.x - (rect.x + rect.w / 2);
slouken@5246
   975
        delta.y = center.y - (rect.y + rect.h / 2);
slouken@5246
   976
        dist = (delta.x*delta.x + delta.y*delta.y);
slouken@5246
   977
        if (dist < closest_dist) {
slouken@5246
   978
            closest = i;
slouken@5246
   979
            closest_dist = dist;
slouken@5246
   980
        }
slouken@5246
   981
    }
slouken@5246
   982
    if (closest < 0) {
slouken@5246
   983
        SDL_SetError("Couldn't find any displays");
slouken@5246
   984
    }
slouken@5246
   985
    return closest;
slouken@5246
   986
}
slouken@5246
   987
slouken@5246
   988
SDL_VideoDisplay *
slouken@5246
   989
SDL_GetDisplayForWindow(SDL_Window *window)
slouken@5246
   990
{
slouken@6786
   991
    int displayIndex = SDL_GetWindowDisplayIndex(window);
slouken@5373
   992
    if (displayIndex >= 0) {
slouken@5373
   993
        return &_this->displays[displayIndex];
slouken@5373
   994
    } else {
slouken@5373
   995
        return NULL;
slouken@5373
   996
    }
slouken@5246
   997
}
slouken@5246
   998
slouken@5246
   999
int
slouken@3685
  1000
SDL_SetWindowDisplayMode(SDL_Window * window, const SDL_DisplayMode * mode)
slouken@3500
  1001
{
slouken@3695
  1002
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3500
  1003
slouken@2753
  1004
    if (mode) {
slouken@3500
  1005
        window->fullscreen_mode = *mode;
slouken@3500
  1006
    } else {
slouken@3500
  1007
        SDL_zero(window->fullscreen_mode);
slouken@3500
  1008
    }
slouken@3526
  1009
    return 0;
slouken@3500
  1010
}
slouken@3500
  1011
slouken@3500
  1012
int
slouken@3685
  1013
SDL_GetWindowDisplayMode(SDL_Window * window, SDL_DisplayMode * mode)
slouken@3500
  1014
{
slouken@3500
  1015
    SDL_DisplayMode fullscreen_mode;
slouken@7191
  1016
    SDL_VideoDisplay *display;
slouken@3500
  1017
aschiffler@6936
  1018
    if (!mode) {
icculus@7037
  1019
      return SDL_InvalidParamError("mode");
aschiffler@6936
  1020
    }
slouken@7191
  1021
slouken@3695
  1022
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3500
  1023
slouken@3500
  1024
    fullscreen_mode = window->fullscreen_mode;
slouken@3500
  1025
    if (!fullscreen_mode.w) {
slouken@3500
  1026
        fullscreen_mode.w = window->w;
slouken@3500
  1027
    }
slouken@3500
  1028
    if (!fullscreen_mode.h) {
slouken@3500
  1029
        fullscreen_mode.h = window->h;
slouken@3500
  1030
    }
slouken@7191
  1031
slouken@7191
  1032
    display = SDL_GetDisplayForWindow(window);
slouken@7191
  1033
slouken@7191
  1034
    /* if in desktop size mode, just return the size of the desktop */
slouken@7191
  1035
    if ( ( window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) == SDL_WINDOW_FULLSCREEN_DESKTOP )
slouken@7191
  1036
    {
slouken@7191
  1037
        fullscreen_mode = display->desktop_mode;
slouken@7191
  1038
    }
slouken@7191
  1039
    else if (!SDL_GetClosestDisplayModeForDisplay(SDL_GetDisplayForWindow(window),
slouken@3500
  1040
                                             &fullscreen_mode,
slouken@3500
  1041
                                             &fullscreen_mode)) {
icculus@7037
  1042
        return SDL_SetError("Couldn't find display mode match");
slouken@3500
  1043
    }
slouken@3500
  1044
slouken@3500
  1045
    if (mode) {
slouken@3500
  1046
        *mode = fullscreen_mode;
slouken@2753
  1047
    }
slouken@2753
  1048
    return 0;
slouken@0
  1049
}
slouken@0
  1050
slouken@5154
  1051
Uint32
slouken@5154
  1052
SDL_GetWindowPixelFormat(SDL_Window * window)
slouken@5154
  1053
{
slouken@5246
  1054
    SDL_VideoDisplay *display;
slouken@5246
  1055
slouken@5246
  1056
    CHECK_WINDOW_MAGIC(window, SDL_PIXELFORMAT_UNKNOWN);
slouken@5246
  1057
slouken@5246
  1058
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  1059
    return display->current_mode.format;
slouken@5154
  1060
}
slouken@5154
  1061
slouken@3502
  1062
static void
slouken@5479
  1063
SDL_RestoreMousePosition(SDL_Window *window)
slouken@5479
  1064
{
slouken@5479
  1065
    int x, y;
slouken@5479
  1066
slouken@5479
  1067
    if (window == SDL_GetMouseFocus()) {
slouken@5479
  1068
        SDL_GetMouseState(&x, &y);
slouken@5479
  1069
        SDL_WarpMouseInWindow(window, x, y);
slouken@5479
  1070
    }
slouken@5479
  1071
}
slouken@5479
  1072
slouken@5479
  1073
static void
slouken@5404
  1074
SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool fullscreen)
slouken@3502
  1075
{
slouken@7968
  1076
    SDL_VideoDisplay *display;
slouken@5249
  1077
    SDL_Window *other;
slouken@3502
  1078
slouken@7968
  1079
#ifdef __MACOSX__
slouken@7968
  1080
    if (Cocoa_SetWindowFullscreenSpace(window, fullscreen)) {
jorgen@8110
  1081
        window->last_fullscreen_flags = window->flags;
slouken@7968
  1082
        return;
slouken@7968
  1083
    }
slouken@7968
  1084
#endif
slouken@7968
  1085
slouken@7968
  1086
    display = SDL_GetDisplayForWindow(window);
slouken@7968
  1087
slouken@5404
  1088
    if (fullscreen) {
slouken@3502
  1089
        /* Hide any other fullscreen windows */
slouken@5249
  1090
        if (display->fullscreen_window &&
slouken@5249
  1091
            display->fullscreen_window != window) {
slouken@5246
  1092
            SDL_MinimizeWindow(display->fullscreen_window);
slouken@3502
  1093
        }
slouken@3502
  1094
    }
slouken@3502
  1095
slouken@5305
  1096
    /* See if anything needs to be done now */
slouken@5404
  1097
    if ((display->fullscreen_window == window) == fullscreen) {
jorgen@8110
  1098
        if ((window->last_fullscreen_flags & FULLSCREEN_MASK) == (window->flags & FULLSCREEN_MASK)) {
jorgen@8110
  1099
            return;
jorgen@8110
  1100
        }
slouken@5305
  1101
    }
slouken@5305
  1102
slouken@5249
  1103
    /* See if there are any fullscreen windows */
slouken@5249
  1104
    for (other = _this->windows; other; other = other->next) {
slouken@5404
  1105
        SDL_bool setDisplayMode = SDL_FALSE;
slouken@5404
  1106
slouken@5404
  1107
        if (other == window) {
slouken@5404
  1108
            setDisplayMode = fullscreen;
slouken@5404
  1109
        } else if (FULLSCREEN_VISIBLE(other) &&
slouken@5404
  1110
                   SDL_GetDisplayForWindow(other) == display) {
slouken@5404
  1111
            setDisplayMode = SDL_TRUE;
slouken@5404
  1112
        }
slouken@5404
  1113
slouken@5404
  1114
        if (setDisplayMode) {
slouken@5249
  1115
            SDL_DisplayMode fullscreen_mode;
slouken@5404
  1116
slouken@5249
  1117
            if (SDL_GetWindowDisplayMode(other, &fullscreen_mode) == 0) {
slouken@5306
  1118
                SDL_bool resized = SDL_TRUE;
slouken@5306
  1119
slouken@5306
  1120
                if (other->w == fullscreen_mode.w && other->h == fullscreen_mode.h) {
slouken@5306
  1121
                    resized = SDL_FALSE;
slouken@5249
  1122
                }
slouken@3502
  1123
slouken@7191
  1124
                /* only do the mode change if we want exclusive fullscreen */
slouken@7950
  1125
                if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != SDL_WINDOW_FULLSCREEN_DESKTOP) {
slouken@7191
  1126
                    SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
slouken@7950
  1127
                } else {
slouken@7191
  1128
                    SDL_SetDisplayModeForDisplay(display, NULL);
slouken@7950
  1129
                }
slouken@7191
  1130
slouken@5249
  1131
                if (_this->SetWindowFullscreen) {
slouken@5305
  1132
                    _this->SetWindowFullscreen(_this, other, display, SDL_TRUE);
slouken@5249
  1133
                }
slouken@5249
  1134
                display->fullscreen_window = other;
slouken@5249
  1135
slouken@5306
  1136
                /* Generate a mode change event here */
slouken@5306
  1137
                if (resized) {
slouken@5306
  1138
                    SDL_SendWindowEvent(other, SDL_WINDOWEVENT_RESIZED,
slouken@5306
  1139
                                        fullscreen_mode.w, fullscreen_mode.h);
slouken@5306
  1140
                } else {
slouken@5306
  1141
                    SDL_OnWindowResized(other);
slouken@5306
  1142
                }
slouken@5479
  1143
slouken@5479
  1144
                SDL_RestoreMousePosition(other);
jorgen@8110
  1145
jorgen@8110
  1146
                window->last_fullscreen_flags = window->flags;
slouken@3502
  1147
                return;
slouken@3502
  1148
            }
slouken@3502
  1149
        }
slouken@3502
  1150
    }
slouken@3502
  1151
slouken@3502
  1152
    /* Nope, restore the desktop mode */
slouken@3502
  1153
    SDL_SetDisplayModeForDisplay(display, NULL);
slouken@5249
  1154
slouken@5249
  1155
    if (_this->SetWindowFullscreen) {
slouken@5305
  1156
        _this->SetWindowFullscreen(_this, window, display, SDL_FALSE);
slouken@5249
  1157
    }
slouken@3517
  1158
    display->fullscreen_window = NULL;
slouken@5306
  1159
slouken@5306
  1160
    /* Generate a mode change event here */
slouken@5306
  1161
    SDL_OnWindowResized(window);
slouken@5479
  1162
slouken@5479
  1163
    /* Restore the cursor position */
slouken@5479
  1164
    SDL_RestoreMousePosition(window);
jorgen@8110
  1165
jorgen@8110
  1166
    window->last_fullscreen_flags = window->flags;
slouken@3502
  1167
}
slouken@3502
  1168
slouken@5380
  1169
#define CREATE_FLAGS \
slouken@7971
  1170
    (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI)
slouken@5380
  1171
slouken@5380
  1172
static void
slouken@5380
  1173
SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
slouken@5380
  1174
{
slouken@5398
  1175
    window->windowed.x = window->x;
slouken@5398
  1176
    window->windowed.y = window->y;
slouken@5398
  1177
    window->windowed.w = window->w;
slouken@5398
  1178
    window->windowed.h = window->h;
slouken@5398
  1179
slouken@5380
  1180
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@5380
  1181
        SDL_MaximizeWindow(window);
slouken@5380
  1182
    }
slouken@5380
  1183
    if (flags & SDL_WINDOW_MINIMIZED) {
slouken@5380
  1184
        SDL_MinimizeWindow(window);
slouken@5380
  1185
    }
slouken@5380
  1186
    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@6755
  1187
        SDL_SetWindowFullscreen(window, flags);
slouken@5380
  1188
    }
slouken@5380
  1189
    if (flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@5380
  1190
        SDL_SetWindowGrab(window, SDL_TRUE);
slouken@5380
  1191
    }
slouken@5380
  1192
    if (!(flags & SDL_WINDOW_HIDDEN)) {
slouken@5380
  1193
        SDL_ShowWindow(window);
slouken@5380
  1194
    }
slouken@5380
  1195
}
slouken@5380
  1196
slouken@3685
  1197
SDL_Window *
slouken@1895
  1198
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
slouken@0
  1199
{
slouken@3685
  1200
    SDL_Window *window;
urkle@7746
  1201
    const char *hint;
slouken@0
  1202
slouken@2753
  1203
    if (!_this) {
slouken@3417
  1204
        /* Initialize the video system if needed */
slouken@5123
  1205
        if (SDL_VideoInit(NULL) < 0) {
slouken@3685
  1206
            return NULL;
slouken@3417
  1207
        }
slouken@2753
  1208
    }
slouken@5249
  1209
slouken@6266
  1210
    /* Some platforms can't create zero-sized windows */
slouken@6266
  1211
    if (w < 1) {
slouken@6266
  1212
        w = 1;
slouken@6266
  1213
    }
slouken@6266
  1214
    if (h < 1) {
slouken@6266
  1215
        h = 1;
slouken@6266
  1216
    }
slouken@6266
  1217
slouken@5249
  1218
    /* Some platforms have OpenGL enabled by default */
slouken@6188
  1219
#if (SDL_VIDEO_OPENGL && __MACOSX__) || __IPHONEOS__ || __ANDROID__
slouken@5249
  1220
    flags |= SDL_WINDOW_OPENGL;
slouken@5249
  1221
#endif
slouken@3057
  1222
    if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1223
        if (!_this->GL_CreateContext) {
slouken@3057
  1224
            SDL_SetError("No OpenGL support in video driver");
slouken@3685
  1225
            return NULL;
slouken@3057
  1226
        }
slouken@6611
  1227
        if (SDL_GL_LoadLibrary(NULL) < 0) {
slouken@6611
  1228
            return NULL;
slouken@6611
  1229
        }
slouken@2753
  1230
    }
slouken@7971
  1231
slouken@7971
  1232
    /* Unless the user has specified the high-DPI disabling hint, respect the
slouken@7971
  1233
     * SDL_WINDOW_ALLOW_HIGHDPI flag.
slouken@7971
  1234
     */
slouken@7971
  1235
    if (flags & SDL_WINDOW_ALLOW_HIGHDPI) {
slouken@7971
  1236
        hint = SDL_GetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED);
slouken@7971
  1237
        if (hint && SDL_atoi(hint) > 0) {
slouken@7971
  1238
            flags &= ~SDL_WINDOW_ALLOW_HIGHDPI;
slouken@7971
  1239
        }
slouken@7971
  1240
    }
slouken@7971
  1241
slouken@3685
  1242
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@7399
  1243
    if (!window) {
slouken@7399
  1244
        SDL_OutOfMemory();
slouken@7399
  1245
        return NULL;
slouken@7399
  1246
    }
slouken@3695
  1247
    window->magic = &_this->window_magic;
slouken@3685
  1248
    window->id = _this->next_object_id++;
slouken@3685
  1249
    window->x = x;
slouken@3685
  1250
    window->y = y;
slouken@3685
  1251
    window->w = w;
slouken@3685
  1252
    window->h = h;
slouken@5380
  1253
    if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
slouken@5380
  1254
        SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5380
  1255
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5380
  1256
        int displayIndex;
slouken@5380
  1257
        SDL_Rect bounds;
slouken@5380
  1258
slouken@5380
  1259
        displayIndex = SDL_GetIndexOfDisplay(display);
slouken@5380
  1260
        SDL_GetDisplayBounds(displayIndex, &bounds);
slouken@5404
  1261
        if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(x)) {
slouken@5380
  1262
            window->x = bounds.x + (bounds.w - w) / 2;
slouken@5380
  1263
        }
slouken@5380
  1264
        if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@5380
  1265
            window->y = bounds.y + (bounds.h - h) / 2;
slouken@5380
  1266
        }
slouken@5380
  1267
    }
slouken@5380
  1268
    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
jorgen@8110
  1269
    window->last_fullscreen_flags = window->flags;
slouken@5466
  1270
    window->brightness = 1.0f;
slouken@5246
  1271
    window->next = _this->windows;
jorgen@8112
  1272
    window->is_destroying = SDL_FALSE;
urkle@7746
  1273
slouken@5246
  1274
    if (_this->windows) {
slouken@5246
  1275
        _this->windows->prev = window;
slouken@3693
  1276
    }
slouken@5246
  1277
    _this->windows = window;
slouken@3685
  1278
slouken@3685
  1279
    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
slouken@3685
  1280
        SDL_DestroyWindow(window);
slouken@3685
  1281
        return NULL;
slouken@3685
  1282
    }
slouken@0
  1283
slouken@2753
  1284
    if (title) {
slouken@3685
  1285
        SDL_SetWindowTitle(window, title);
slouken@2753
  1286
    }
slouken@5380
  1287
    SDL_FinishWindowCreation(window, flags);
slouken@7191
  1288
slouken@6063
  1289
    /* If the window was created fullscreen, make sure the mode code matches */
slouken@6063
  1290
    SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
slouken@3685
  1291
slouken@3685
  1292
    return window;
slouken@1895
  1293
}
slouken@0
  1294
slouken@3685
  1295
SDL_Window *
slouken@1895
  1296
SDL_CreateWindowFrom(const void *data)
slouken@1895
  1297
{
slouken@3685
  1298
    SDL_Window *window;
slouken@0
  1299
slouken@2753
  1300
    if (!_this) {
slouken@2753
  1301
        SDL_UninitializedVideo();
slouken@3685
  1302
        return NULL;
slouken@3685
  1303
    }
slouken@3685
  1304
    window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
slouken@7399
  1305
    if (!window) {
slouken@7399
  1306
        SDL_OutOfMemory();
slouken@7399
  1307
        return NULL;
slouken@7399
  1308
    }
slouken@3695
  1309
    window->magic = &_this->window_magic;
slouken@3685
  1310
    window->id = _this->next_object_id++;
slouken@3685
  1311
    window->flags = SDL_WINDOW_FOREIGN;
jorgen@8110
  1312
    window->last_fullscreen_flags = window->flags;
jorgen@8112
  1313
    window->is_destroying = SDL_FALSE;
slouken@5466
  1314
    window->brightness = 1.0f;
slouken@5246
  1315
    window->next = _this->windows;
slouken@5246
  1316
    if (_this->windows) {
slouken@5246
  1317
        _this->windows->prev = window;
slouken@3693
  1318
    }
slouken@5246
  1319
    _this->windows = window;
slouken@1895
  1320
slouken@2753
  1321
    if (!_this->CreateWindowFrom ||
slouken@3685
  1322
        _this->CreateWindowFrom(_this, window, data) < 0) {
slouken@3685
  1323
        SDL_DestroyWindow(window);
slouken@3685
  1324
        return NULL;
slouken@3685
  1325
    }
slouken@3685
  1326
    return window;
slouken@1895
  1327
}
slouken@1895
  1328
slouken@1924
  1329
int
slouken@1928
  1330
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
slouken@1924
  1331
{
slouken@2753
  1332
    char *title = window->title;
slouken@7523
  1333
    SDL_Surface *icon = window->icon;
slouken@1956
  1334
slouken@2753
  1335
    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
icculus@7037
  1336
        return SDL_SetError("No OpenGL support in video driver");
slouken@2753
  1337
    }
slouken@3057
  1338
slouken@3057
  1339
    if (window->flags & SDL_WINDOW_FOREIGN) {
slouken@3057
  1340
        /* Can't destroy and re-create foreign windows, hrm */
slouken@3057
  1341
        flags |= SDL_WINDOW_FOREIGN;
slouken@3057
  1342
    } else {
slouken@3057
  1343
        flags &= ~SDL_WINDOW_FOREIGN;
slouken@3057
  1344
    }
slouken@3057
  1345
slouken@5249
  1346
    /* Restore video mode, etc. */
slouken@5305
  1347
    SDL_HideWindow(window);
slouken@5249
  1348
slouken@5249
  1349
    /* Tear down the old native window */
slouken@5249
  1350
    if (window->surface) {
slouken@5288
  1351
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5249
  1352
        SDL_FreeSurface(window->surface);
slouken@5249
  1353
    }
slouken@5249
  1354
    if (_this->DestroyWindowFramebuffer) {
slouken@5249
  1355
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5249
  1356
    }
slouken@3057
  1357
    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@2753
  1358
        _this->DestroyWindow(_this, window);
slouken@2753
  1359
    }
slouken@3057
  1360
slouken@5249
  1361
    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
slouken@5249
  1362
        if (flags & SDL_WINDOW_OPENGL) {
slouken@6775
  1363
            if (SDL_GL_LoadLibrary(NULL) < 0) {
slouken@6775
  1364
                return -1;
slouken@6775
  1365
            }
slouken@5249
  1366
        } else {
slouken@5249
  1367
            SDL_GL_UnloadLibrary();
slouken@5249
  1368
        }
slouken@5249
  1369
    }
slouken@5249
  1370
slouken@2753
  1371
    window->title = NULL;
slouken@7523
  1372
    window->icon = NULL;
slouken@5380
  1373
    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
jorgen@8110
  1374
    window->last_fullscreen_flags = window->flags;
jorgen@8112
  1375
    window->is_destroying = SDL_FALSE;
slouken@1956
  1376
slouken@3057
  1377
    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
slouken@3057
  1378
        if (_this->CreateWindow(_this, window) < 0) {
slouken@3057
  1379
            if (flags & SDL_WINDOW_OPENGL) {
slouken@3057
  1380
                SDL_GL_UnloadLibrary();
slouken@3057
  1381
            }
slouken@3057
  1382
            return -1;
slouken@3057
  1383
        }
slouken@2753
  1384
    }
slouken@3057
  1385
slouken@2753
  1386
    if (title) {
slouken@3685
  1387
        SDL_SetWindowTitle(window, title);
slouken@2753
  1388
        SDL_free(title);
slouken@2753
  1389
    }
slouken@7523
  1390
    if (icon) {
slouken@7523
  1391
        SDL_SetWindowIcon(window, icon);
slouken@7523
  1392
        SDL_FreeSurface(icon);
slouken@7523
  1393
    }
slouken@5380
  1394
    SDL_FinishWindowCreation(window, flags);
slouken@2875
  1395
slouken@2753
  1396
    return 0;
slouken@1924
  1397
}
slouken@1924
  1398
slouken@1895
  1399
Uint32
slouken@3685
  1400
SDL_GetWindowID(SDL_Window * window)
slouken@1895
  1401
{
slouken@3695
  1402
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1403
slouken@3685
  1404
    return window->id;
slouken@3685
  1405
}
slouken@3685
  1406
slouken@3685
  1407
SDL_Window *
slouken@3685
  1408
SDL_GetWindowFromID(Uint32 id)
slouken@3685
  1409
{
slouken@3685
  1410
    SDL_Window *window;
slouken@3685
  1411
slouken@3685
  1412
    if (!_this) {
slouken@3685
  1413
        return NULL;
slouken@3685
  1414
    }
slouken@5246
  1415
    for (window = _this->windows; window; window = window->next) {
slouken@5246
  1416
        if (window->id == id) {
slouken@5246
  1417
            return window;
slouken@3685
  1418
        }
slouken@3685
  1419
    }
slouken@3685
  1420
    return NULL;
slouken@3685
  1421
}
slouken@3685
  1422
slouken@3685
  1423
Uint32
slouken@3685
  1424
SDL_GetWindowFlags(SDL_Window * window)
slouken@3685
  1425
{
slouken@3695
  1426
    CHECK_WINDOW_MAGIC(window, 0);
slouken@3695
  1427
slouken@2753
  1428
    return window->flags;
slouken@1895
  1429
}
slouken@1895
  1430
slouken@1895
  1431
void
slouken@3685
  1432
SDL_SetWindowTitle(SDL_Window * window, const char *title)
slouken@1895
  1433
{
slouken@3695
  1434
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1435
slouken@3695
  1436
    if (title == window->title) {
slouken@2753
  1437
        return;
slouken@2753
  1438
    }
slouken@7719
  1439
    SDL_free(window->title);
slouken@4871
  1440
    if (title && *title) {
slouken@2753
  1441
        window->title = SDL_strdup(title);
slouken@2753
  1442
    } else {
slouken@2753
  1443
        window->title = NULL;
slouken@2753
  1444
    }
slouken@1956
  1445
slouken@2753
  1446
    if (_this->SetWindowTitle) {
slouken@2753
  1447
        _this->SetWindowTitle(_this, window);
slouken@2753
  1448
    }
slouken@1895
  1449
}
slouken@1895
  1450
slouken@2753
  1451
const char *
slouken@3685
  1452
SDL_GetWindowTitle(SDL_Window * window)
slouken@1895
  1453
{
slouken@4872
  1454
    CHECK_WINDOW_MAGIC(window, "");
slouken@3695
  1455
slouken@4871
  1456
    return window->title ? window->title : "";
slouken@1895
  1457
}
slouken@1895
  1458
slouken@1895
  1459
void
slouken@3685
  1460
SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
slouken@2967
  1461
{
slouken@3695
  1462
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1463
slouken@5375
  1464
    if (!icon) {
slouken@5375
  1465
        return;
slouken@5375
  1466
    }
slouken@5375
  1467
slouken@7720
  1468
    SDL_FreeSurface(window->icon);
slouken@7523
  1469
slouken@7523
  1470
    /* Convert the icon into ARGB8888 */
slouken@7523
  1471
    window->icon = SDL_ConvertSurfaceFormat(icon, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@7523
  1472
    if (!window->icon) {
slouken@7523
  1473
        return;
slouken@7523
  1474
    }
slouken@7523
  1475
slouken@2967
  1476
    if (_this->SetWindowIcon) {
slouken@7523
  1477
        _this->SetWindowIcon(_this, window, window->icon);
slouken@2967
  1478
    }
slouken@2967
  1479
}
slouken@2967
  1480
slouken@5165
  1481
void*
slouken@5165
  1482
SDL_SetWindowData(SDL_Window * window, const char *name, void *userdata)
slouken@1895
  1483
{
slouken@5165
  1484
    SDL_WindowUserData *prev, *data;
slouken@3695
  1485
slouken@5165
  1486
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@7191
  1487
aschiffler@7046
  1488
    /* Input validation */
slouken@7721
  1489
    if (name == NULL || name[0] == '\0') {
aschiffler@7046
  1490
      SDL_InvalidParamError("name");
aschiffler@7046
  1491
      return NULL;
aschiffler@7046
  1492
    }
slouken@5165
  1493
slouken@5165
  1494
    /* See if the named data already exists */
slouken@5165
  1495
    prev = NULL;
slouken@5165
  1496
    for (data = window->data; data; prev = data, data = data->next) {
aschiffler@7046
  1497
        if (data->name && SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1498
            void *last_value = data->data;
slouken@5165
  1499
slouken@5165
  1500
            if (userdata) {
slouken@5165
  1501
                /* Set the new value */
slouken@5165
  1502
                data->data = userdata;
slouken@5165
  1503
            } else {
slouken@5165
  1504
                /* Delete this value */
slouken@5165
  1505
                if (prev) {
slouken@5165
  1506
                    prev->next = data->next;
slouken@5165
  1507
                } else {
slouken@5165
  1508
                    window->data = data->next;
slouken@5165
  1509
                }
slouken@5165
  1510
                SDL_free(data->name);
slouken@5165
  1511
                SDL_free(data);
slouken@5165
  1512
            }
slouken@5165
  1513
            return last_value;
slouken@5165
  1514
        }
slouken@5165
  1515
    }
slouken@5165
  1516
slouken@5165
  1517
    /* Add new data to the window */
slouken@5165
  1518
    if (userdata) {
slouken@5165
  1519
        data = (SDL_WindowUserData *)SDL_malloc(sizeof(*data));
slouken@5165
  1520
        data->name = SDL_strdup(name);
slouken@5165
  1521
        data->data = userdata;
slouken@5165
  1522
        data->next = window->data;
slouken@5165
  1523
        window->data = data;
slouken@5165
  1524
    }
slouken@5165
  1525
    return NULL;
slouken@1895
  1526
}
slouken@1895
  1527
slouken@2753
  1528
void *
slouken@5165
  1529
SDL_GetWindowData(SDL_Window * window, const char *name)
slouken@1895
  1530
{
slouken@5165
  1531
    SDL_WindowUserData *data;
slouken@5165
  1532
slouken@3695
  1533
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@3695
  1534
aschiffler@7046
  1535
    /* Input validation */
slouken@7721
  1536
    if (name == NULL || name[0] == '\0') {
aschiffler@7046
  1537
      SDL_InvalidParamError("name");
aschiffler@7046
  1538
      return NULL;
aschiffler@7046
  1539
    }
aschiffler@7046
  1540
slouken@5165
  1541
    for (data = window->data; data; data = data->next) {
aschiffler@7046
  1542
        if (data->name && SDL_strcmp(data->name, name) == 0) {
slouken@5165
  1543
            return data->data;
slouken@5165
  1544
        }
slouken@5165
  1545
    }
slouken@5165
  1546
    return NULL;
slouken@1895
  1547
}
slouken@1895
  1548
slouken@1895
  1549
void
slouken@3685
  1550
SDL_SetWindowPosition(SDL_Window * window, int x, int y)
slouken@1895
  1551
{
slouken@3695
  1552
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1553
slouken@7195
  1554
    if (SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@7195
  1555
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@7195
  1556
        int displayIndex;
slouken@7195
  1557
        SDL_Rect bounds;
slouken@7195
  1558
slouken@7195
  1559
        displayIndex = SDL_GetIndexOfDisplay(display);
slouken@7195
  1560
        SDL_GetDisplayBounds(displayIndex, &bounds);
slouken@7195
  1561
        if (SDL_WINDOWPOS_ISCENTERED(x)) {
slouken@7195
  1562
            x = bounds.x + (bounds.w - window->w) / 2;
slouken@7195
  1563
        }
slouken@7195
  1564
        if (SDL_WINDOWPOS_ISCENTERED(y)) {
slouken@7195
  1565
            y = bounds.y + (bounds.h - window->h) / 2;
slouken@7195
  1566
        }
slouken@7195
  1567
    }
slouken@7195
  1568
jorgen@7792
  1569
    if ((window->flags & SDL_WINDOW_FULLSCREEN)) {
jorgen@7792
  1570
        if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
jorgen@7792
  1571
            window->windowed.x = x;
jorgen@7792
  1572
        }
jorgen@7792
  1573
        if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
jorgen@7792
  1574
            window->windowed.y = y;
jorgen@7792
  1575
        }
jorgen@7792
  1576
    } else {
jorgen@7792
  1577
        if (!SDL_WINDOWPOS_ISUNDEFINED(x)) {
jorgen@7792
  1578
            window->x = x;
jorgen@7792
  1579
        }
jorgen@7792
  1580
        if (!SDL_WINDOWPOS_ISUNDEFINED(y)) {
jorgen@7792
  1581
            window->y = y;
jorgen@7792
  1582
        }
jorgen@7792
  1583
slouken@5246
  1584
        if (_this->SetWindowPosition) {
slouken@5246
  1585
            _this->SetWindowPosition(_this, window);
slouken@5246
  1586
        }
slouken@5246
  1587
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@2753
  1588
    }
slouken@1895
  1589
}
slouken@1895
  1590
slouken@1895
  1591
void
slouken@3685
  1592
SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
slouken@1895
  1593
{
slouken@5246
  1594
    CHECK_WINDOW_MAGIC(window, );
slouken@5246
  1595
slouken@5246
  1596
    /* Fullscreen windows are always at their display's origin */
slouken@5246
  1597
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
aschiffler@6956
  1598
        if (x) {
aschiffler@6956
  1599
            *x = 0;
aschiffler@6956
  1600
        }
aschiffler@6956
  1601
        if (y) {
aschiffler@6956
  1602
            *y = 0;
aschiffler@6956
  1603
        }
slouken@5246
  1604
    } else {
slouken@5242
  1605
        if (x) {
slouken@5242
  1606
            *x = window->x;
slouken@5242
  1607
        }
slouken@5242
  1608
        if (y) {
slouken@5242
  1609
            *y = window->y;
slouken@5242
  1610
        }
slouken@2753
  1611
    }
slouken@1895
  1612
}
slouken@1895
  1613
slouken@1895
  1614
void
icculus@6422
  1615
SDL_SetWindowBordered(SDL_Window * window, SDL_bool bordered)
icculus@6422
  1616
{
icculus@6422
  1617
    CHECK_WINDOW_MAGIC(window, );
icculus@6422
  1618
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
icculus@6422
  1619
        const int want = (bordered != SDL_FALSE);  /* normalize the flag. */
icculus@6422
  1620
        const int have = ((window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6422
  1621
        if ((want != have) && (_this->SetWindowBordered)) {
icculus@6422
  1622
            if (want) {
icculus@6422
  1623
                window->flags &= ~SDL_WINDOW_BORDERLESS;
icculus@6422
  1624
            } else {
icculus@6422
  1625
                window->flags |= SDL_WINDOW_BORDERLESS;
icculus@6422
  1626
            }
icculus@6422
  1627
            _this->SetWindowBordered(_this, window, (SDL_bool) want);
icculus@6422
  1628
        }
icculus@6422
  1629
    }
icculus@6422
  1630
}
icculus@6422
  1631
icculus@6422
  1632
void
slouken@3685
  1633
SDL_SetWindowSize(SDL_Window * window, int w, int h)
slouken@1895
  1634
{
slouken@3695
  1635
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1636
    if (w <= 0) {
aschiffler@6984
  1637
        SDL_InvalidParamError("w");
aschiffler@6984
  1638
        return;
aschiffler@6984
  1639
    }
aschiffler@6984
  1640
    if (h <= 0) {
aschiffler@6984
  1641
        SDL_InvalidParamError("h");
aschiffler@6984
  1642
        return;
aschiffler@6984
  1643
    }
slouken@3695
  1644
slouken@7941
  1645
    /* Make sure we don't exceed any window size limits */
slouken@7941
  1646
    if (window->min_w && w < window->min_w)
slouken@7941
  1647
    {
slouken@7941
  1648
        w = window->min_w;
slouken@7941
  1649
    }
slouken@7941
  1650
    if (window->max_w && w > window->max_w)
slouken@7941
  1651
    {
slouken@7941
  1652
        w = window->max_w;
slouken@7941
  1653
    }
slouken@7941
  1654
    if (window->min_h && h < window->min_h)
slouken@7941
  1655
    {
slouken@7941
  1656
        h = window->min_h;
slouken@7941
  1657
    }
slouken@7941
  1658
    if (window->max_h && h > window->max_h)
slouken@7941
  1659
    {
slouken@7941
  1660
        h = window->max_h;
slouken@7941
  1661
    }
slouken@7941
  1662
slouken@5249
  1663
    /* FIXME: Should this change fullscreen modes? */
slouken@7941
  1664
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@7941
  1665
        window->windowed.w = w;
slouken@7941
  1666
        window->windowed.h = h;
slouken@7941
  1667
    } else {
slouken@5276
  1668
        window->w = w;
slouken@5276
  1669
        window->h = h;
slouken@5249
  1670
        if (_this->SetWindowSize) {
slouken@5249
  1671
            _this->SetWindowSize(_this, window);
slouken@5249
  1672
        }
slouken@5276
  1673
        if (window->w == w && window->h == h) {
slouken@5276
  1674
            /* We didn't get a SDL_WINDOWEVENT_RESIZED event (by design) */
slouken@5276
  1675
            SDL_OnWindowResized(window);
slouken@5276
  1676
        }
slouken@2753
  1677
    }
slouken@1895
  1678
}
slouken@1895
  1679
slouken@1895
  1680
void
slouken@3685
  1681
SDL_GetWindowSize(SDL_Window * window, int *w, int *h)
slouken@1895
  1682
{
aschiffler@6984
  1683
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1684
    if (w) {
aschiffler@6984
  1685
        *w = window->w;
slouken@5246
  1686
    }
aschiffler@6984
  1687
    if (h) {
stopiccot@6681
  1688
        *h = window->h;
slouken@7191
  1689
    }
stopiccot@6681
  1690
}
stopiccot@6681
  1691
stopiccot@6681
  1692
void
stopiccot@6681
  1693
SDL_SetWindowMinimumSize(SDL_Window * window, int min_w, int min_h)
stopiccot@6681
  1694
{
stopiccot@6681
  1695
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1696
    if (min_w <= 0) {
aschiffler@6984
  1697
        SDL_InvalidParamError("min_w");
aschiffler@6984
  1698
        return;
aschiffler@6984
  1699
    }
aschiffler@6984
  1700
    if (min_h <= 0) {
aschiffler@6984
  1701
        SDL_InvalidParamError("min_h");
aschiffler@6984
  1702
        return;
aschiffler@6984
  1703
    }
slouken@7191
  1704
stopiccot@6681
  1705
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
stopiccot@6681
  1706
        window->min_w = min_w;
stopiccot@6681
  1707
        window->min_h = min_h;
stopiccot@6681
  1708
        if (_this->SetWindowMinimumSize) {
stopiccot@6681
  1709
            _this->SetWindowMinimumSize(_this, window);
slouken@2849
  1710
        }
stopiccot@6681
  1711
        /* Ensure that window is not smaller than minimal size */
stopiccot@6681
  1712
        SDL_SetWindowSize(window, SDL_max(window->w, window->min_w), SDL_max(window->h, window->min_h));
stopiccot@6681
  1713
    }
stopiccot@6681
  1714
}
stopiccot@6681
  1715
stopiccot@6681
  1716
void
stopiccot@6681
  1717
SDL_GetWindowMinimumSize(SDL_Window * window, int *min_w, int *min_h)
stopiccot@6681
  1718
{
aschiffler@6984
  1719
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1720
    if (min_w) {
aschiffler@6984
  1721
        *min_w = window->min_w;
stopiccot@6681
  1722
    }
aschiffler@6984
  1723
    if (min_h) {
stopiccot@6681
  1724
        *min_h = window->min_h;
slouken@2753
  1725
    }
slouken@1895
  1726
}
slouken@1895
  1727
slouken@1895
  1728
void
slouken@6788
  1729
SDL_SetWindowMaximumSize(SDL_Window * window, int max_w, int max_h)
slouken@6788
  1730
{
slouken@6788
  1731
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1732
    if (max_w <= 0) {
aschiffler@6984
  1733
        SDL_InvalidParamError("max_w");
aschiffler@6984
  1734
        return;
aschiffler@6984
  1735
    }
aschiffler@6984
  1736
    if (max_h <= 0) {
aschiffler@6984
  1737
        SDL_InvalidParamError("max_h");
aschiffler@6984
  1738
        return;
aschiffler@6984
  1739
    }
slouken@7191
  1740
slouken@6788
  1741
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@6788
  1742
        window->max_w = max_w;
slouken@6788
  1743
        window->max_h = max_h;
slouken@6788
  1744
        if (_this->SetWindowMaximumSize) {
slouken@6788
  1745
            _this->SetWindowMaximumSize(_this, window);
slouken@6788
  1746
        }
slouken@6788
  1747
        /* Ensure that window is not larger than maximal size */
slouken@6788
  1748
        SDL_SetWindowSize(window, SDL_min(window->w, window->max_w), SDL_min(window->h, window->max_h));
slouken@6788
  1749
    }
slouken@6788
  1750
}
slouken@6788
  1751
slouken@6788
  1752
void
slouken@6788
  1753
SDL_GetWindowMaximumSize(SDL_Window * window, int *max_w, int *max_h)
slouken@6788
  1754
{
aschiffler@6984
  1755
    CHECK_WINDOW_MAGIC(window, );
aschiffler@6984
  1756
    if (max_w) {
aschiffler@6984
  1757
        *max_w = window->max_w;
slouken@6788
  1758
    }
aschiffler@6984
  1759
    if (max_h) {
slouken@6788
  1760
        *max_h = window->max_h;
slouken@6788
  1761
    }
slouken@6788
  1762
}
slouken@6788
  1763
slouken@6788
  1764
void
slouken@3685
  1765
SDL_ShowWindow(SDL_Window * window)
slouken@1895
  1766
{
slouken@3695
  1767
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1768
slouken@3695
  1769
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@2753
  1770
        return;
slouken@2753
  1771
    }
slouken@1895
  1772
slouken@2753
  1773
    if (_this->ShowWindow) {
slouken@2753
  1774
        _this->ShowWindow(_this, window);
slouken@2753
  1775
    }
slouken@3685
  1776
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
  1777
}
slouken@1895
  1778
slouken@1895
  1779
void
slouken@3685
  1780
SDL_HideWindow(SDL_Window * window)
slouken@1895
  1781
{
slouken@3695
  1782
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1783
slouken@3695
  1784
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1785
        return;
slouken@2753
  1786
    }
slouken@1895
  1787
slouken@5404
  1788
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@5404
  1789
slouken@2753
  1790
    if (_this->HideWindow) {
slouken@2753
  1791
        _this->HideWindow(_this, window);
slouken@2753
  1792
    }
slouken@3685
  1793
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
  1794
}
slouken@1895
  1795
slouken@1895
  1796
void
slouken@3685
  1797
SDL_RaiseWindow(SDL_Window * window)
slouken@1895
  1798
{
slouken@3695
  1799
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1800
slouken@3695
  1801
    if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@2753
  1802
        return;
slouken@2753
  1803
    }
slouken@2753
  1804
    if (_this->RaiseWindow) {
slouken@2753
  1805
        _this->RaiseWindow(_this, window);
slouken@2753
  1806
    }
slouken@1895
  1807
}
slouken@1895
  1808
slouken@1895
  1809
void
slouken@3685
  1810
SDL_MaximizeWindow(SDL_Window * window)
slouken@1895
  1811
{
slouken@3695
  1812
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1813
slouken@3695
  1814
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@2753
  1815
        return;
slouken@2753
  1816
    }
slouken@1895
  1817
gabomdq@7662
  1818
    /* !!! FIXME: should this check if the window is resizable? */
icculus@7566
  1819
slouken@2753
  1820
    if (_this->MaximizeWindow) {
slouken@2753
  1821
        _this->MaximizeWindow(_this, window);
slouken@2753
  1822
    }
slouken@1895
  1823
}
slouken@1895
  1824
slouken@1895
  1825
void
slouken@3685
  1826
SDL_MinimizeWindow(SDL_Window * window)
slouken@1895
  1827
{
slouken@3695
  1828
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1829
slouken@3695
  1830
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@2753
  1831
        return;
slouken@2753
  1832
    }
slouken@1895
  1833
slouken@5404
  1834
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@5404
  1835
slouken@2753
  1836
    if (_this->MinimizeWindow) {
slouken@2753
  1837
        _this->MinimizeWindow(_this, window);
slouken@2753
  1838
    }
slouken@1895
  1839
}
slouken@1895
  1840
slouken@1895
  1841
void
slouken@3685
  1842
SDL_RestoreWindow(SDL_Window * window)
slouken@1895
  1843
{
slouken@3695
  1844
    CHECK_WINDOW_MAGIC(window, );
slouken@3695
  1845
slouken@3695
  1846
    if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
slouken@2753
  1847
        return;
slouken@2753
  1848
    }
slouken@1895
  1849
slouken@2753
  1850
    if (_this->RestoreWindow) {
slouken@2753
  1851
        _this->RestoreWindow(_this, window);
slouken@2753
  1852
    }
slouken@1895
  1853
}
slouken@1895
  1854
slouken@1895
  1855
int
slouken@6755
  1856
SDL_SetWindowFullscreen(SDL_Window * window, Uint32 flags)
slouken@1895
  1857
{
slouken@3695
  1858
    CHECK_WINDOW_MAGIC(window, -1);
slouken@3695
  1859
slouken@7191
  1860
    flags &= FULLSCREEN_MASK;
slouken@7191
  1861
slouken@6755
  1862
    if ( flags == (window->flags & FULLSCREEN_MASK) ) {
slouken@2753
  1863
        return 0;
slouken@2753
  1864
    }
slouken@7191
  1865
slouken@7191
  1866
    /* clear the previous flags and OR in the new ones */
slouken@7191
  1867
    window->flags &= ~FULLSCREEN_MASK;
slouken@6755
  1868
    window->flags |= flags;
slouken@6755
  1869
slouken@5404
  1870
    SDL_UpdateFullscreenMode(window, FULLSCREEN_VISIBLE(window));
slouken@1895
  1871
slouken@2753
  1872
    return 0;
slouken@1895
  1873
}
slouken@1895
  1874
slouken@5166
  1875
static SDL_Surface *
slouken@5166
  1876
SDL_CreateWindowFramebuffer(SDL_Window * window)
slouken@5166
  1877
{
slouken@5166
  1878
    Uint32 format;
slouken@5166
  1879
    void *pixels;
slouken@5166
  1880
    int pitch;
slouken@5166
  1881
    int bpp;
slouken@5166
  1882
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5166
  1883
slouken@5166
  1884
    if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
slouken@5166
  1885
        return NULL;
slouken@5166
  1886
    }
slouken@5166
  1887
slouken@5166
  1888
    if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
slouken@5166
  1889
        return NULL;
slouken@5166
  1890
    }
slouken@5166
  1891
slouken@5166
  1892
    if (!SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5166
  1893
        return NULL;
slouken@5166
  1894
    }
slouken@5166
  1895
slouken@5166
  1896
    return SDL_CreateRGBSurfaceFrom(pixels, window->w, window->h, bpp, pitch, Rmask, Gmask, Bmask, Amask);
slouken@5166
  1897
}
slouken@5166
  1898
slouken@5166
  1899
SDL_Surface *
slouken@5166
  1900
SDL_GetWindowSurface(SDL_Window * window)
slouken@5166
  1901
{
slouken@5166
  1902
    CHECK_WINDOW_MAGIC(window, NULL);
slouken@5166
  1903
slouken@5172
  1904
    if (!window->surface_valid) {
slouken@5172
  1905
        if (window->surface) {
slouken@5288
  1906
            window->surface->flags &= ~SDL_DONTFREE;
slouken@5172
  1907
            SDL_FreeSurface(window->surface);
slouken@5172
  1908
        }
slouken@5166
  1909
        window->surface = SDL_CreateWindowFramebuffer(window);
slouken@5169
  1910
        if (window->surface) {
slouken@5172
  1911
            window->surface_valid = SDL_TRUE;
slouken@5288
  1912
            window->surface->flags |= SDL_DONTFREE;
slouken@5169
  1913
        }
slouken@5166
  1914
    }
slouken@5166
  1915
    return window->surface;
slouken@5166
  1916
}
slouken@5166
  1917
slouken@5166
  1918
int
slouken@5166
  1919
SDL_UpdateWindowSurface(SDL_Window * window)
slouken@5166
  1920
{
slouken@5166
  1921
    SDL_Rect full_rect;
slouken@5166
  1922
slouken@5166
  1923
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1924
slouken@5166
  1925
    full_rect.x = 0;
slouken@5166
  1926
    full_rect.y = 0;
slouken@5166
  1927
    full_rect.w = window->w;
slouken@5166
  1928
    full_rect.h = window->h;
slouken@5297
  1929
    return SDL_UpdateWindowSurfaceRects(window, &full_rect, 1);
slouken@5166
  1930
}
slouken@5166
  1931
slouken@5166
  1932
int
slouken@7014
  1933
SDL_UpdateWindowSurfaceRects(SDL_Window * window, const SDL_Rect * rects,
slouken@5297
  1934
                             int numrects)
slouken@5166
  1935
{
slouken@5166
  1936
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5166
  1937
slouken@5172
  1938
    if (!window->surface_valid) {
icculus@7037
  1939
        return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");
slouken@5166
  1940
    }
slouken@5166
  1941
slouken@5297
  1942
    return _this->UpdateWindowFramebuffer(_this, window, rects, numrects);
slouken@5166
  1943
}
slouken@5166
  1944
slouken@5466
  1945
int
slouken@5466
  1946
SDL_SetWindowBrightness(SDL_Window * window, float brightness)
slouken@5466
  1947
{
slouken@5466
  1948
    Uint16 ramp[256];
slouken@5466
  1949
    int status;
slouken@5466
  1950
slouken@5466
  1951
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1952
slouken@5466
  1953
    SDL_CalculateGammaRamp(brightness, ramp);
slouken@5466
  1954
    status = SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
slouken@5466
  1955
    if (status == 0) {
slouken@5466
  1956
        window->brightness = brightness;
slouken@5466
  1957
    }
slouken@5466
  1958
    return status;
slouken@5466
  1959
}
slouken@5466
  1960
slouken@5466
  1961
float
slouken@5466
  1962
SDL_GetWindowBrightness(SDL_Window * window)
slouken@5466
  1963
{
slouken@5466
  1964
    CHECK_WINDOW_MAGIC(window, 1.0f);
slouken@5466
  1965
slouken@5466
  1966
    return window->brightness;
slouken@5466
  1967
}
slouken@5466
  1968
slouken@5466
  1969
int
slouken@5466
  1970
SDL_SetWindowGammaRamp(SDL_Window * window, const Uint16 * red,
slouken@5466
  1971
                                            const Uint16 * green,
slouken@5466
  1972
                                            const Uint16 * blue)
slouken@5466
  1973
{
slouken@5466
  1974
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  1975
slouken@5466
  1976
    if (!_this->SetWindowGammaRamp) {
icculus@7037
  1977
        return SDL_Unsupported();
slouken@5466
  1978
    }
slouken@5466
  1979
slouken@5466
  1980
    if (!window->gamma) {
slouken@5466
  1981
        if (SDL_GetWindowGammaRamp(window, NULL, NULL, NULL) < 0) {
slouken@5466
  1982
            return -1;
slouken@5466
  1983
        }
slouken@5466
  1984
    }
slouken@5466
  1985
slouken@5466
  1986
    if (red) {
slouken@5466
  1987
        SDL_memcpy(&window->gamma[0*256], red, 256*sizeof(Uint16));
slouken@5466
  1988
    }
slouken@5466
  1989
    if (green) {
slouken@5466
  1990
        SDL_memcpy(&window->gamma[1*256], green, 256*sizeof(Uint16));
slouken@5466
  1991
    }
slouken@5466
  1992
    if (blue) {
slouken@5466
  1993
        SDL_memcpy(&window->gamma[2*256], blue, 256*sizeof(Uint16));
slouken@5466
  1994
    }
slouken@5466
  1995
    if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@5466
  1996
        return _this->SetWindowGammaRamp(_this, window, window->gamma);
slouken@5466
  1997
    } else {
slouken@5466
  1998
        return 0;
slouken@5466
  1999
    }
slouken@5466
  2000
}
slouken@5466
  2001
slouken@5466
  2002
int
slouken@5466
  2003
SDL_GetWindowGammaRamp(SDL_Window * window, Uint16 * red,
slouken@5466
  2004
                                            Uint16 * green,
slouken@5466
  2005
                                            Uint16 * blue)
slouken@5466
  2006
{
slouken@5466
  2007
    CHECK_WINDOW_MAGIC(window, -1);
slouken@5466
  2008
slouken@5466
  2009
    if (!window->gamma) {
slouken@5466
  2010
        int i;
slouken@5466
  2011
slouken@5466
  2012
        window->gamma = (Uint16 *)SDL_malloc(256*6*sizeof(Uint16));
slouken@5466
  2013
        if (!window->gamma) {
icculus@7037
  2014
            return SDL_OutOfMemory();
slouken@5466
  2015
        }
slouken@5466
  2016
        window->saved_gamma = window->gamma + 3*256;
slouken@5466
  2017
slouken@5466
  2018
        if (_this->GetWindowGammaRamp) {
slouken@5466
  2019
            if (_this->GetWindowGammaRamp(_this, window, window->gamma) < 0) {
slouken@5466
  2020
                return -1;
slouken@5466
  2021
            }
slouken@5466
  2022
        } else {
slouken@5466
  2023
            /* Create an identity gamma ramp */
slouken@5466
  2024
            for (i = 0; i < 256; ++i) {
slouken@5466
  2025
                Uint16 value = (Uint16)((i << 8) | i);
slouken@5466
  2026
slouken@5466
  2027
                window->gamma[0*256+i] = value;
slouken@5466
  2028
                window->gamma[1*256+i] = value;
slouken@5466
  2029
                window->gamma[2*256+i] = value;
slouken@5466
  2030
            }
slouken@5466
  2031
        }
slouken@5466
  2032
        SDL_memcpy(window->saved_gamma, window->gamma, 3*256*sizeof(Uint16));
slouken@5466
  2033
    }
slouken@5466
  2034
slouken@5466
  2035
    if (red) {
slouken@5466
  2036
        SDL_memcpy(red, &window->gamma[0*256], 256*sizeof(Uint16));
slouken@5466
  2037
    }
slouken@5466
  2038
    if (green) {
slouken@5466
  2039
        SDL_memcpy(green, &window->gamma[1*256], 256*sizeof(Uint16));
slouken@5466
  2040
    }
slouken@5466
  2041
    if (blue) {
slouken@5466
  2042
        SDL_memcpy(blue, &window->gamma[2*256], 256*sizeof(Uint16));
slouken@5466
  2043
    }
slouken@5466
  2044
    return 0;
slouken@5466
  2045
}
slouken@5466
  2046
slouken@6662
  2047
void
slouken@2876
  2048
SDL_UpdateWindowGrab(SDL_Window * window)
slouken@2875
  2049
{
slouken@6662
  2050
    if (_this->SetWindowGrab) {
slouken@6662
  2051
        SDL_bool grabbed;
slouken@8072
  2052
        if ((SDL_GetMouse()->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
slouken@8072
  2053
             (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@6662
  2054
            grabbed = SDL_TRUE;
slouken@6662
  2055
        } else {
slouken@6662
  2056
            grabbed = SDL_FALSE;
slouken@6662
  2057
        }
slouken@6662
  2058
        _this->SetWindowGrab(_this, window, grabbed);
slouken@2753
  2059
    }
slouken@1895
  2060
}
slouken@1895
  2061
slouken@5403
  2062
void
slouken@5403
  2063
SDL_SetWindowGrab(SDL_Window * window, SDL_bool grabbed)
slouken@5403
  2064
{
slouken@5403
  2065
    CHECK_WINDOW_MAGIC(window, );
slouken@5403
  2066
slouken@5404
  2067
    if (!!grabbed == !!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
slouken@5403
  2068
        return;
slouken@5403
  2069
    }
slouken@5403
  2070
    if (grabbed) {
slouken@5403
  2071
        window->flags |= SDL_WINDOW_INPUT_GRABBED;
slouken@5403
  2072
    } else {
slouken@5403
  2073
        window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
slouken@5403
  2074
    }
slouken@5403
  2075
    SDL_UpdateWindowGrab(window);
slouken@5403
  2076
}
slouken@5403
  2077
slouken@5403
  2078
SDL_bool
slouken@3685
  2079
SDL_GetWindowGrab(SDL_Window * window)
slouken@1895
  2080
{
slouken@5466
  2081
    CHECK_WINDOW_MAGIC(window, SDL_FALSE);
slouken@3695
  2082
slouken@2753
  2083
    return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
slouken@1895
  2084
}
slouken@1895
  2085
slouken@1895
  2086
void
slouken@1895
  2087
SDL_OnWindowShown(SDL_Window * window)
slouken@1895
  2088
{
slouken@5404
  2089
    SDL_OnWindowRestored(window);
slouken@1895
  2090
}
slouken@1895
  2091
slouken@1895
  2092
void
slouken@1895
  2093
SDL_OnWindowHidden(SDL_Window * window)
slouken@1895
  2094
{
slouken@5404
  2095
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@1895
  2096
}
slouken@1895
  2097
slouken@1895
  2098
void
slouken@5166
  2099
SDL_OnWindowResized(SDL_Window * window)
slouken@5166
  2100
{
slouken@5172
  2101
    window->surface_valid = SDL_FALSE;
slouken@5276
  2102
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
slouken@5166
  2103
}
slouken@5166
  2104
slouken@5166
  2105
void
slouken@3502
  2106
SDL_OnWindowMinimized(SDL_Window * window)
slouken@3502
  2107
{
slouken@5404
  2108
    SDL_UpdateFullscreenMode(window, SDL_FALSE);
slouken@3502
  2109
}
slouken@3502
  2110
slouken@3502
  2111
void
slouken@3502
  2112
SDL_OnWindowRestored(SDL_Window * window)
slouken@3502
  2113
{
slouken@3685
  2114
    SDL_RaiseWindow(window);
slouken@5404
  2115
slouken@5404
  2116
    if (FULLSCREEN_VISIBLE(window)) {
slouken@5404
  2117
        SDL_UpdateFullscreenMode(window, SDL_TRUE);
slouken@5404
  2118
    }
slouken@3502
  2119
}
slouken@3502
  2120
slouken@3502
  2121
void
jorgen@7089
  2122
SDL_OnWindowEnter(SDL_Window * window)
jorgen@7089
  2123
{
jorgen@7089
  2124
    if (_this->OnWindowEnter) {
jorgen@7089
  2125
        _this->OnWindowEnter(_this, window);
jorgen@7089
  2126
    }
jorgen@7089
  2127
}
jorgen@7089
  2128
jorgen@7089
  2129
void
jorgen@7089
  2130
SDL_OnWindowLeave(SDL_Window * window)
jorgen@7089
  2131
{
jorgen@7089
  2132
}
jorgen@7089
  2133
jorgen@7089
  2134
void
slouken@1895
  2135
SDL_OnWindowFocusGained(SDL_Window * window)
slouken@1895
  2136
{
jorgen@7107
  2137
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7107
  2138
slouken@5466
  2139
    if (window->gamma && _this->SetWindowGammaRamp) {
slouken@5466
  2140
        _this->SetWindowGammaRamp(_this, window, window->gamma);
slouken@5466
  2141
    }
slouken@5466
  2142
jorgen@7107
  2143
    if (mouse && mouse->relative_mode) {
jorgen@7107
  2144
        SDL_SetMouseFocus(window);
jorgen@7107
  2145
        SDL_WarpMouseInWindow(window, window->w/2, window->h/2);
jorgen@7107
  2146
    }
jorgen@7107
  2147
slouken@6662
  2148
    SDL_UpdateWindowGrab(window);
slouken@1895
  2149
}
slouken@1895
  2150
slouken@7953
  2151
static SDL_bool
slouken@7953
  2152
ShouldMinimizeOnFocusLoss(SDL_Window * window)
slouken@6755
  2153
{
slouken@7954
  2154
    const char *hint;
slouken@7953
  2155
jorgen@8112
  2156
    if (!(window->flags & SDL_WINDOW_FULLSCREEN) || window->is_destroying) {
slouken@7953
  2157
        return SDL_FALSE;
slouken@7953
  2158
    }
slouken@7953
  2159
slouken@7969
  2160
#ifdef __MACOSX__
slouken@7969
  2161
    if (Cocoa_IsWindowInFullscreenSpace(window)) {
slouken@7969
  2162
        return SDL_FALSE;
slouken@7953
  2163
    }
slouken@7969
  2164
#endif
slouken@7953
  2165
slouken@7954
  2166
    hint = SDL_GetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS);
slouken@7191
  2167
    if (hint) {
slouken@7191
  2168
        if (*hint == '0') {
slouken@7191
  2169
            return SDL_FALSE;
slouken@7191
  2170
        } else {
slouken@7191
  2171
            return SDL_TRUE;
slouken@7191
  2172
        }
slouken@7191
  2173
    }
slouken@7953
  2174
slouken@7969
  2175
    return SDL_TRUE;
slouken@6755
  2176
}
slouken@6755
  2177
slouken@1895
  2178
void
slouken@1895
  2179
SDL_OnWindowFocusLost(SDL_Window * window)
slouken@1895
  2180
{
slouken@5466
  2181
    if (window->gamma && _this->SetWindowGammaRamp) {
slouken@5466
  2182
        _this->SetWindowGammaRamp(_this, window, window->saved_gamma);
slouken@3512
  2183
    }
slouken@3512
  2184
slouken@6662
  2185
    SDL_UpdateWindowGrab(window);
slouken@5466
  2186
slouken@7953
  2187
    if (ShouldMinimizeOnFocusLoss(window)) {
icculus@7565
  2188
        SDL_MinimizeWindow(window);
slouken@5466
  2189
    }
slouken@1895
  2190
}
slouken@1895
  2191
slouken@3685
  2192
SDL_Window *
slouken@1895
  2193
SDL_GetFocusWindow(void)
slouken@1895
  2194
{
slouken@3685
  2195
    SDL_Window *window;
slouken@1895
  2196
slouken@2753
  2197
    if (!_this) {
slouken@3685
  2198
        return NULL;
slouken@3685
  2199
    }
slouken@5246
  2200
    for (window = _this->windows; window; window = window->next) {
slouken@2753
  2201
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@3685
  2202
            return window;
slouken@2753
  2203
        }
slouken@2753
  2204
    }
slouken@3685
  2205
    return NULL;
slouken@1895
  2206
}
slouken@1895
  2207
slouken@1895
  2208
void
slouken@3685
  2209
SDL_DestroyWindow(SDL_Window * window)
slouken@1895
  2210
{
slouken@3685
  2211
    SDL_VideoDisplay *display;
slouken@3685
  2212
slouken@3695
  2213
    CHECK_WINDOW_MAGIC(window, );
slouken@3503
  2214
jorgen@8112
  2215
    window->is_destroying = SDL_TRUE;
jorgen@8112
  2216
slouken@6214
  2217
    /* Restore video mode, etc. */
slouken@6214
  2218
    SDL_HideWindow(window);
slouken@6214
  2219
slouken@6214
  2220
    /* Make sure this window no longer has focus */
slouken@6214
  2221
    if (SDL_GetKeyboardFocus() == window) {
slouken@6214
  2222
        SDL_SetKeyboardFocus(NULL);
slouken@6214
  2223
    }
slouken@6214
  2224
    if (SDL_GetMouseFocus() == window) {
slouken@6214
  2225
        SDL_SetMouseFocus(NULL);
slouken@6214
  2226
    }
slouken@6214
  2227
icculus@5563
  2228
    /* make no context current if this is the current context window. */
icculus@5563
  2229
    if (window->flags & SDL_WINDOW_OPENGL) {
icculus@5563
  2230
        if (_this->current_glwin == window) {
slouken@6496
  2231
            SDL_GL_MakeCurrent(window, NULL);
icculus@5563
  2232
        }
icculus@5563
  2233
    }
icculus@5563
  2234
slouken@5169
  2235
    if (window->surface) {
slouken@5288
  2236
        window->surface->flags &= ~SDL_DONTFREE;
slouken@5169
  2237
        SDL_FreeSurface(window->surface);
slouken@5169
  2238
    }
slouken@5166
  2239
    if (_this->DestroyWindowFramebuffer) {
slouken@5166
  2240
        _this->DestroyWindowFramebuffer(_this, window);
slouken@5166
  2241
    }
slouken@3685
  2242
    if (_this->DestroyWindow) {
slouken@3685
  2243
        _this->DestroyWindow(_this, window);
slouken@3685
  2244
    }
slouken@3685
  2245
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3685
  2246
        SDL_GL_UnloadLibrary();
slouken@3685
  2247
    }
slouken@3685
  2248
slouken@5246
  2249
    display = SDL_GetDisplayForWindow(window);
slouken@5246
  2250
    if (display->fullscreen_window == window) {
slouken@5246
  2251
        display->fullscreen_window = NULL;
slouken@5246
  2252
    }
slouken@5246
  2253
slouken@4901
  2254
    /* Now invalidate magic */
slouken@4897
  2255
    window->magic = NULL;
slouken@4897
  2256
slouken@5165
  2257
    /* Free memory associated with the window */
slouken@7719
  2258
    SDL_free(window->title);
slouken@7720
  2259
    SDL_FreeSurface(window->icon);
slouken@7719
  2260
    SDL_free(window->gamma);
slouken@5165
  2261
    while (window->data) {
slouken@5165
  2262
        SDL_WindowUserData *data = window->data;
slouken@5165
  2263
slouken@5165
  2264
        window->data = data->next;
slouken@5165
  2265
        SDL_free(data->name);
slouken@5165
  2266
        SDL_free(data);
slouken@5165
  2267
    }
slouken@5165
  2268
slouken@3685
  2269
    /* Unlink the window from the list */
slouken@3693
  2270
    if (window->next) {
slouken@3693
  2271
        window->next->prev = window->prev;
slouken@3693
  2272
    }
slouken@3685
  2273
    if (window->prev) {
slouken@3685
  2274
        window->prev->next = window->next;
slouken@3685
  2275
    } else {
slouken@5246
  2276
        _this->windows = window->next;
slouken@3685
  2277
    }
slouken@3685
  2278
slouken@3685
  2279
    SDL_free(window);
slouken@1895
  2280
}
slouken@1895
  2281
slouken@3025
  2282
SDL_bool
slouken@3025
  2283
SDL_IsScreenSaverEnabled()
slouken@3025
  2284
{
slouken@3025
  2285
    if (!_this) {
slouken@3025
  2286
        return SDL_TRUE;
slouken@3025
  2287
    }
slouken@3025
  2288
    return _this->suspend_screensaver ? SDL_FALSE : SDL_TRUE;
slouken@3025
  2289
}
slouken@3025
  2290
slouken@3025
  2291
void
slouken@3025
  2292
SDL_EnableScreenSaver()
slouken@3025
  2293
{
slouken@3025
  2294
    if (!_this) {
slouken@3025
  2295
        return;
slouken@3025
  2296
    }
slouken@3025
  2297
    if (!_this->suspend_screensaver) {
slouken@3025
  2298
        return;
slouken@3025
  2299
    }
slouken@3025
  2300
    _this->suspend_screensaver = SDL_FALSE;
slouken@3025
  2301
    if (_this->SuspendScreenSaver) {
slouken@3025
  2302
        _this->SuspendScreenSaver(_this);
slouken@3025
  2303
    }
slouken@3025
  2304
}
slouken@3025
  2305
slouken@3025
  2306
void
slouken@3025
  2307
SDL_DisableScreenSaver()
slouken@3025
  2308
{
slouken@3025
  2309
    if (!_this) {
slouken@3025
  2310
        return;
slouken@3025
  2311
    }
slouken@3025
  2312
    if (_this->suspend_screensaver) {
slouken@3025
  2313
        return;
slouken@3025
  2314
    }
slouken@3025
  2315
    _this->suspend_screensaver = SDL_TRUE;
slouken@3025
  2316
    if (_this->SuspendScreenSaver) {
slouken@3025
  2317
        _this->SuspendScreenSaver(_this);
slouken@3025
  2318
    }
slouken@3025
  2319
}
slouken@3025
  2320
slouken@1895
  2321
void
slouken@1895
  2322
SDL_VideoQuit(void)
slouken@1895
  2323
{
slouken@2753
  2324
    int i, j;
slouken@1895
  2325
slouken@2753
  2326
    if (!_this) {
slouken@2753
  2327
        return;
slouken@2753
  2328
    }
slouken@5123
  2329
slouken@2753
  2330
    /* Halt event processing before doing anything else */
slouken@7360
  2331
    SDL_TouchQuit();
slouken@5123
  2332
    SDL_MouseQuit();
slouken@5123
  2333
    SDL_KeyboardQuit();
slouken@7360
  2334
    SDL_QuitSubSystem(SDL_INIT_EVENTS);
slouken@5123
  2335
slouken@3029
  2336
    SDL_EnableScreenSaver();
slouken@1895
  2337
slouken@2753
  2338
    /* Clean up the system video */
slouken@5246
  2339
    while (_this->windows) {
slouken@5246
  2340
        SDL_DestroyWindow(_this->windows);
slouken@2753
  2341
    }
slouken@2753
  2342
    _this->VideoQuit(_this);
slouken@1895
  2343
icculus@7441
  2344
    for (i = 0; i < _this->num_displays; ++i) {
slouken@2753
  2345
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@2753
  2346
        for (j = display->num_display_modes; j--;) {
slouken@7719
  2347
            SDL_free(display->display_modes[j].driverdata);
slouken@7719
  2348
            display->display_modes[j].driverdata = NULL;
slouken@2753
  2349
        }
slouken@7719
  2350
        SDL_free(display->display_modes);
slouken@7719
  2351
        display->display_modes = NULL;
slouken@7719
  2352
        SDL_free(display->desktop_mode.driverdata);
slouken@7719
  2353
        display->desktop_mode.driverdata = NULL;
slouken@7719
  2354
        SDL_free(display->driverdata);
slouken@7719
  2355
        display->driverdata = NULL;
slouken@2753
  2356
    }
slouken@2753
  2357
    if (_this->displays) {
slouken@6787
  2358
        for (i = 0; i < _this->num_displays; ++i) {
slouken@6787
  2359
            SDL_free(_this->displays[i].name);
slouken@6787
  2360
        }
slouken@2753
  2361
        SDL_free(_this->displays);
slouken@2753
  2362
        _this->displays = NULL;
slouken@6787
  2363
        _this->num_displays = 0;
slouken@2753
  2364
    }
slouken@7719
  2365
    SDL_free(_this->clipboard_text);
slouken@7719
  2366
    _this->clipboard_text = NULL;
slouken@2753
  2367
    _this->free(_this);
slouken@2753
  2368
    _this = NULL;
slouken@0
  2369
}
slouken@0
  2370
slouken@1895
  2371
int
slouken@1895
  2372
SDL_GL_LoadLibrary(const char *path)
slouken@0
  2373
{
slouken@2753
  2374
    int retval;
slouken@0
  2375
slouken@2753
  2376
    if (!_this) {
icculus@7037
  2377
        return SDL_UninitializedVideo();
slouken@2753
  2378
    }
slouken@3057
  2379
    if (_this->gl_config.driver_loaded) {
slouken@3057
  2380
        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
icculus@7037
  2381
            return SDL_SetError("OpenGL library already loaded");
slouken@3057
  2382
        }
slouken@3057
  2383
        retval = 0;
slouken@3057
  2384
    } else {
slouken@3057
  2385
        if (!_this->GL_LoadLibrary) {
slouken@7865
  2386
            return SDL_SetError("No dynamic GL support in video driver");
slouken@3057
  2387
        }
slouken@2753
  2388
        retval = _this->GL_LoadLibrary(_this, path);
slouken@3057
  2389
    }
slouken@3057
  2390
    if (retval == 0) {
slouken@3057
  2391
        ++_this->gl_config.driver_loaded;
slouken@7865
  2392
    } else {
slouken@7865
  2393
        if (_this->GL_UnloadLibrary) {
slouken@7865
  2394
            _this->GL_UnloadLibrary(_this);
slouken@7865
  2395
        }
slouken@2753
  2396
    }
slouken@2753
  2397
    return (retval);
slouken@0
  2398
}
slouken@0
  2399
slouken@2753
  2400
void *
slouken@1895
  2401
SDL_GL_GetProcAddress(const char *proc)
slouken@0
  2402
{
slouken@2753
  2403
    void *func;
slouken@0
  2404
slouken@2753
  2405
    if (!_this) {
slouken@2753
  2406
        SDL_UninitializedVideo();
slouken@2753
  2407
        return NULL;
slouken@2753
  2408
    }
slouken@2753
  2409
    func = NULL;
slouken@2753
  2410
    if (_this->GL_GetProcAddress) {
slouken@2753
  2411
        if (_this->gl_config.driver_loaded) {
slouken@2753
  2412
            func = _this->GL_GetProcAddress(_this, proc);
slouken@2753
  2413
        } else {
slouken@2753
  2414
            SDL_SetError("No GL driver has been loaded");
slouken@2753
  2415
        }
slouken@2753
  2416
    } else {
slouken@2753
  2417
        SDL_SetError("No dynamic GL support in video driver");
slouken@2753
  2418
    }
slouken@2753
  2419
    return func;
slouken@0
  2420
}
slouken@0
  2421
slouken@3057
  2422
void
slouken@3057
  2423
SDL_GL_UnloadLibrary(void)
slouken@3057
  2424
{
slouken@3057
  2425
    if (!_this) {
slouken@3057
  2426
        SDL_UninitializedVideo();
slouken@3057
  2427
        return;
slouken@3057
  2428
    }
slouken@3057
  2429
    if (_this->gl_config.driver_loaded > 0) {
slouken@3057
  2430
        if (--_this->gl_config.driver_loaded > 0) {
slouken@3057
  2431
            return;
slouken@3057
  2432
        }
slouken@3057
  2433
        if (_this->GL_UnloadLibrary) {
slouken@3057
  2434
            _this->GL_UnloadLibrary(_this);
slouken@3057
  2435
        }
slouken@3057
  2436
    }
slouken@3057
  2437
}
slouken@3057
  2438
slouken@7860
  2439
static SDL_INLINE SDL_bool
icculus@7210
  2440
isAtLeastGL3(const char *verstr)
icculus@7210
  2441
{
icculus@7210
  2442
    return ( verstr && (SDL_atoi(verstr) >= 3) );
icculus@7210
  2443
}
icculus@7210
  2444
slouken@1926
  2445
SDL_bool
slouken@1926
  2446
SDL_GL_ExtensionSupported(const char *extension)
slouken@1926
  2447
{
slouken@5209
  2448
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2449
    const GLubyte *(APIENTRY * glGetStringFunc) (GLenum);
slouken@2753
  2450
    const char *extensions;
slouken@2753
  2451
    const char *start;
slouken@2753
  2452
    const char *where, *terminator;
slouken@1926
  2453
slouken@2753
  2454
    /* Extension names should not have spaces. */
slouken@2753
  2455
    where = SDL_strchr(extension, ' ');
slouken@2753
  2456
    if (where || *extension == '\0') {
slouken@2753
  2457
        return SDL_FALSE;
slouken@2753
  2458
    }
slouken@2753
  2459
    /* See if there's an environment variable override */
slouken@2753
  2460
    start = SDL_getenv(extension);
slouken@2753
  2461
    if (start && *start == '0') {
slouken@2753
  2462
        return SDL_FALSE;
slouken@2753
  2463
    }
icculus@7210
  2464
slouken@2753
  2465
    /* Lookup the available extensions */
icculus@7210
  2466
slouken@2753
  2467
    glGetStringFunc = SDL_GL_GetProcAddress("glGetString");
icculus@7210
  2468
    if (!glGetStringFunc) {
icculus@7210
  2469
        return SDL_FALSE;
slouken@2753
  2470
    }
icculus@7210
  2471
icculus@7210
  2472
    if (isAtLeastGL3((const char *) glGetStringFunc(GL_VERSION))) {
icculus@7210
  2473
        const GLubyte *(APIENTRY * glGetStringiFunc) (GLenum, GLuint);
icculus@7210
  2474
        void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
icculus@7210
  2475
        GLint num_exts = 0;
icculus@7210
  2476
        GLint i;
icculus@7210
  2477
icculus@7210
  2478
        glGetStringiFunc = SDL_GL_GetProcAddress("glGetStringi");
icculus@7210
  2479
        glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
icculus@7210
  2480
        if ((!glGetStringiFunc) || (!glGetIntegervFunc)) {
icculus@7210
  2481
            return SDL_FALSE;
icculus@7210
  2482
        }
icculus@7210
  2483
icculus@7212
  2484
        #ifndef GL_NUM_EXTENSIONS
icculus@7212
  2485
        #define GL_NUM_EXTENSIONS 0x821D
icculus@7212
  2486
        #endif
icculus@7210
  2487
        glGetIntegervFunc(GL_NUM_EXTENSIONS, &num_exts);
icculus@7210
  2488
        for (i = 0; i < num_exts; i++) {
icculus@7210
  2489
            const char *thisext = (const char *) glGetStringiFunc(GL_EXTENSIONS, i);
icculus@7210
  2490
            if (SDL_strcmp(thisext, extension) == 0) {
icculus@7210
  2491
                return SDL_TRUE;
icculus@7210
  2492
            }
icculus@7210
  2493
        }
icculus@7210
  2494
icculus@7210
  2495
        return SDL_FALSE;
icculus@7210
  2496
    }
icculus@7210
  2497
icculus@7210
  2498
    /* Try the old way with glGetString(GL_EXTENSIONS) ... */
icculus@7210
  2499
icculus@7210
  2500
    extensions = (const char *) glGetStringFunc(GL_EXTENSIONS);
slouken@2753
  2501
    if (!extensions) {
slouken@2753
  2502
        return SDL_FALSE;
slouken@2753
  2503
    }
slouken@2753
  2504
    /*
slouken@2753
  2505
     * It takes a bit of care to be fool-proof about parsing the OpenGL
slouken@2753
  2506
     * extensions string. Don't be fooled by sub-strings, etc.
slouken@2753
  2507
     */
slouken@1926
  2508
slouken@2753
  2509
    start = extensions;
slouken@1926
  2510
slouken@2753
  2511
    for (;;) {
slouken@2753
  2512
        where = SDL_strstr(start, extension);
slouken@2753
  2513
        if (!where)
slouken@2753
  2514
            break;
slouken@1926
  2515
slouken@2753
  2516
        terminator = where + SDL_strlen(extension);
slouken@2753
  2517
        if (where == start || *(where - 1) == ' ')
slouken@2753
  2518
            if (*terminator == ' ' || *terminator == '\0')
slouken@2753
  2519
                return SDL_TRUE;
slouken@1926
  2520
slouken@2753
  2521
        start = terminator;
slouken@2753
  2522
    }
slouken@2753
  2523
    return SDL_FALSE;
slouken@1926
  2524
#else
slouken@2753
  2525
    return SDL_FALSE;
slouken@1926
  2526
#endif
slouken@1926
  2527
}
slouken@1926
  2528
jorgen@8145
  2529
void
jorgen@8145
  2530
SDL_GL_ResetAttributes()
jorgen@8145
  2531
{
jorgen@8145
  2532
    if (!_this) {
jorgen@8145
  2533
        return;
jorgen@8145
  2534
    }
jorgen@8145
  2535
jorgen@8145
  2536
    _this->gl_config.red_size = 3;
jorgen@8145
  2537
    _this->gl_config.green_size = 3;
jorgen@8145
  2538
    _this->gl_config.blue_size = 2;
jorgen@8145
  2539
    _this->gl_config.alpha_size = 0;
jorgen@8145
  2540
    _this->gl_config.buffer_size = 0;
jorgen@8145
  2541
    _this->gl_config.depth_size = 16;
jorgen@8145
  2542
    _this->gl_config.stencil_size = 0;
jorgen@8145
  2543
    _this->gl_config.double_buffer = 1;
jorgen@8145
  2544
    _this->gl_config.accum_red_size = 0;
jorgen@8145
  2545
    _this->gl_config.accum_green_size = 0;
jorgen@8145
  2546
    _this->gl_config.accum_blue_size = 0;
jorgen@8145
  2547
    _this->gl_config.accum_alpha_size = 0;
jorgen@8145
  2548
    _this->gl_config.stereo = 0;
jorgen@8145
  2549
    _this->gl_config.multisamplebuffers = 0;
jorgen@8145
  2550
    _this->gl_config.multisamplesamples = 0;
jorgen@8145
  2551
    _this->gl_config.retained_backing = 1;
jorgen@8145
  2552
    _this->gl_config.accelerated = -1;  /* accelerated or not, both are fine */
jorgen@8145
  2553
    _this->gl_config.profile_mask = 0;
jorgen@8145
  2554
#if SDL_VIDEO_OPENGL
jorgen@8145
  2555
    _this->gl_config.major_version = 2;
jorgen@8145
  2556
    _this->gl_config.minor_version = 1;
jorgen@8145
  2557
#elif SDL_VIDEO_OPENGL_ES2
jorgen@8145
  2558
    _this->gl_config.major_version = 2;
jorgen@8145
  2559
    _this->gl_config.minor_version = 0;
jorgen@8145
  2560
    _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
jorgen@8145
  2561
#elif SDL_VIDEO_OPENGL_ES
jorgen@8145
  2562
    _this->gl_config.major_version = 1;
jorgen@8145
  2563
    _this->gl_config.minor_version = 1;
jorgen@8145
  2564
    _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
jorgen@8145
  2565
#endif
jorgen@8145
  2566
    _this->gl_config.flags = 0;
jorgen@8145
  2567
    _this->gl_config.framebuffer_srgb_capable = 0;
jorgen@8145
  2568
jorgen@8145
  2569
    _this->gl_config.share_with_current_context = 0;
jorgen@8145
  2570
}
jorgen@8145
  2571
slouken@1895
  2572
int
slouken@1895
  2573
SDL_GL_SetAttribute(SDL_GLattr attr, int value)
slouken@0
  2574
{
slouken@5209
  2575
#if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@2753
  2576
    int retval;
slouken@0
  2577
slouken@2753
  2578
    if (!_this) {
icculus@7037
  2579
        return SDL_UninitializedVideo();
slouken@2753
  2580
    }
slouken@2753
  2581
    retval = 0;
slouken@2753
  2582
    switch (attr) {
slouken@2753
  2583
    case SDL_GL_RED_SIZE:
slouken@2753
  2584
        _this->gl_config.red_size = value;
slouken@2753
  2585
        break;
slouken@2753
  2586
    case SDL_GL_GREEN_SIZE:
slouken@2753
  2587
        _this->gl_config.green_size = value;
slouken@2753
  2588
        break;
slouken@2753
  2589
    case SDL_GL_BLUE_SIZE:
slouken@2753
  2590
        _this->gl_config.blue_size = value;
slouken@2753
  2591
        break;
slouken@2753
  2592
    case SDL_GL_ALPHA_SIZE:
slouken@2753
  2593
        _this->gl_config.alpha_size = value;
slouken@2753
  2594
        break;
slouken@2753
  2595
    case SDL_GL_DOUBLEBUFFER:
slouken@2753
  2596
        _this->gl_config.double_buffer = value;
slouken@2753
  2597
        break;
slouken@2753
  2598
    case SDL_GL_BUFFER_SIZE:
slouken@2753
  2599
        _this->gl_config.buffer_size = value;
slouken@2753
  2600
        break;
slouken@2753
  2601
    case SDL_GL_DEPTH_SIZE:
slouken@2753
  2602
        _this->gl_config.depth_size = value;
slouken@2753
  2603
        break;
slouken@2753
  2604
    case SDL_GL_STENCIL_SIZE:
slouken@2753
  2605
        _this->gl_config.stencil_size = value;
slouken@2753
  2606
        break;
slouken@2753
  2607
    case SDL_GL_ACCUM_RED_SIZE:
slouken@2753
  2608
        _this->gl_config.accum_red_size = value;
slouken@2753
  2609
        break;
slouken@2753
  2610
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@2753
  2611
        _this->gl_config.accum_green_size = value;
slouken@2753
  2612
        break;
slouken@2753
  2613
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@2753
  2614
        _this->gl_config.accum_blue_size = value;
slouken@2753
  2615
        break;
slouken@2753
  2616
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@2753
  2617
        _this->gl_config.accum_alpha_size = value;
slouken@2753
  2618
        break;
slouken@2753
  2619
    case SDL_GL_STEREO:
slouken@2753
  2620
        _this->gl_config.stereo = value;
slouken@2753
  2621
        break;
slouken@2753
  2622
    case SDL_GL_MULTISAMPLEBUFFERS:
slouken@2753
  2623
        _this->gl_config.multisamplebuffers = value;
slouken@2753
  2624
        break;
slouken@2753
  2625
    case SDL_GL_MULTISAMPLESAMPLES:
slouken@2753
  2626
        _this->gl_config.multisamplesamples = value;
slouken@2753
  2627
        break;
slouken@2753
  2628
    case SDL_GL_ACCELERATED_VISUAL:
slouken@2753
  2629
        _this->gl_config.accelerated = value;
slouken@2753
  2630
        break;
slouken@2753
  2631
    case SDL_GL_RETAINED_BACKING:
slouken@2753
  2632
        _this->gl_config.retained_backing = value;
slouken@2753
  2633
        break;
slouken@3100
  2634
    case SDL_GL_CONTEXT_MAJOR_VERSION:
slouken@3100
  2635
        _this->gl_config.major_version = value;
slouken@3100
  2636
        break;
slouken@3100
  2637
    case SDL_GL_CONTEXT_MINOR_VERSION: