src/video/quartz/SDL_QuartzVideo.m
author Ryan C. Gordon <icculus@icculus.org>
Thu, 03 Nov 2011 23:58:24 -0400
branchSDL-1.2
changeset 6048 a0538c8c39b5
parent 5986 4854219a91e9
child 6049 4b79fd1fd9a3
permissions -rw-r--r--
Fixed a compiler warning when building with Mac OS X target < 10.7.
slouken@47
     1
/*
slouken@47
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009  Sam Lantinga
slouken@47
     4
slouken@47
     5
    This library is free software; you can redistribute it and/or
slouken@47
     6
    modify it under the terms of the GNU Library General Public
slouken@47
     7
    License as published by the Free Software Foundation; either
slouken@47
     8
    version 2 of the License, or (at your option) any later version.
slouken@47
     9
slouken@47
    10
    This library is distributed in the hope that it will be useful,
slouken@47
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@47
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@47
    13
    Library General Public License for more details.
slouken@47
    14
slouken@47
    15
    You should have received a copy of the GNU Library General Public
slouken@47
    16
    License along with this library; if not, write to the Free
slouken@47
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@47
    18
slouken@47
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@47
    21
*/
slouken@1403
    22
#include "SDL_config.h"
slouken@47
    23
slouken@47
    24
#include "SDL_QuartzVideo.h"
slouken@761
    25
#include "SDL_QuartzWindow.h"
slouken@47
    26
icculus@5911
    27
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)  /* Fixed in Snow Leopard */
icculus@4204
    28
/*
slouken@761
    29
    Add methods to get at private members of NSScreen. 
slouken@761
    30
    Since there is a bug in Apple's screen switching code
slouken@761
    31
    that does not update this variable when switching
slouken@761
    32
    to fullscreen, we'll set it manually (but only for the
slouken@761
    33
    main screen).
slouken@761
    34
*/
slouken@761
    35
@interface NSScreen (NSScreenAccess)
slouken@761
    36
- (void) setFrame:(NSRect)frame;
slouken@761
    37
@end
slouken@761
    38
slouken@761
    39
@implementation NSScreen (NSScreenAccess)
slouken@761
    40
- (void) setFrame:(NSRect)frame;
slouken@761
    41
{
slouken@761
    42
    _frame = frame;
slouken@761
    43
}
slouken@761
    44
@end
icculus@4204
    45
static inline void QZ_SetFrame(NSScreen *nsscreen, NSRect frame)
icculus@4204
    46
{
icculus@4204
    47
    [nsscreen setFrame:frame];
icculus@4204
    48
}
icculus@4204
    49
#else
icculus@4204
    50
static inline void QZ_SetFrame(NSScreen *nsscreen, NSRect frame)
icculus@4204
    51
{
icculus@4204
    52
}
icculus@4204
    53
#endif
slouken@761
    54
slouken@4123
    55
@interface SDLTranslatorResponder : NSTextView
slouken@4123
    56
{
slouken@4123
    57
}
slouken@4123
    58
- (void) doCommandBySelector:(SEL)myselector;
slouken@4123
    59
@end
slouken@4123
    60
slouken@4123
    61
@implementation SDLTranslatorResponder
slouken@4123
    62
- (void) doCommandBySelector:(SEL) myselector {}
slouken@4123
    63
@end
slouken@4123
    64
icculus@4204
    65
/* absent in 10.3.9.  */
icculus@4204
    66
CG_EXTERN CGImageRef CGBitmapContextCreateImage (CGContextRef);
slouken@761
    67
slouken@761
    68
/* Bootstrap functions */
slouken@761
    69
static int              QZ_Available ();
slouken@761
    70
static SDL_VideoDevice* QZ_CreateDevice (int device_index);
slouken@761
    71
static void             QZ_DeleteDevice (SDL_VideoDevice *device);
slouken@761
    72
slouken@761
    73
/* Initialization, Query, Setup, and Redrawing functions */
slouken@761
    74
static int          QZ_VideoInit        (_THIS, SDL_PixelFormat *video_format);
slouken@761
    75
slouken@761
    76
static SDL_Rect**   QZ_ListModes        (_THIS, SDL_PixelFormat *format,
slouken@761
    77
                                         Uint32 flags);
icculus@1340
    78
static void         QZ_UnsetVideoMode   (_THIS, BOOL to_desktop);
slouken@761
    79
slouken@761
    80
static SDL_Surface* QZ_SetVideoMode     (_THIS, SDL_Surface *current,
slouken@761
    81
                                         int width, int height, int bpp,
slouken@761
    82
                                         Uint32 flags);
slouken@761
    83
static int          QZ_ToggleFullScreen (_THIS, int on);
slouken@761
    84
static int          QZ_SetColors        (_THIS, int first_color,
slouken@761
    85
                                         int num_colors, SDL_Color *colors);
slouken@761
    86
icculus@5908
    87
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
    88
static int          QZ_LockDoubleBuffer   (_THIS, SDL_Surface *surface);
icculus@5908
    89
static void         QZ_UnlockDoubleBuffer (_THIS, SDL_Surface *surface);
icculus@5908
    90
static int          QZ_ThreadFlip         (_THIS);
icculus@5908
    91
static int          QZ_FlipDoubleBuffer   (_THIS, SDL_Surface *surface);
icculus@5908
    92
static void         QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect *rects);
icculus@5908
    93
static void         QZ_DirectUpdate     (_THIS, int num_rects, SDL_Rect *rects);
icculus@5908
    94
#endif
icculus@5908
    95
slouken@761
    96
static void         QZ_UpdateRects      (_THIS, int num_rects, SDL_Rect *rects);
slouken@761
    97
static void         QZ_VideoQuit        (_THIS);
slouken@761
    98
slouken@761
    99
static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@761
   100
static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface);
icculus@1120
   101
static int QZ_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@761
   102
static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface);
slouken@47
   103
slouken@47
   104
/* Bootstrap binding, enables entry point into the driver */
slouken@47
   105
VideoBootStrap QZ_bootstrap = {
slouken@272
   106
    "Quartz", "Mac OS X CoreGraphics", QZ_Available, QZ_CreateDevice
slouken@47
   107
};
slouken@47
   108
icculus@5909
   109
/* Disable compiler warnings we can't avoid. */
icculus@5909
   110
#if (defined(__GNUC__) && (__GNUC__ >= 4))
icculus@5910
   111
#  if (MAC_OS_X_VERSION_MAX_ALLOWED <= 1070)
icculus@5910
   112
#    pragma GCC diagnostic ignored "-Wdeprecated-declarations"
icculus@5909
   113
#  endif
icculus@5909
   114
#endif
icculus@5909
   115
icculus@5908
   116
static inline BOOL IS_LION_OR_LATER(_THIS)
icculus@5908
   117
{
icculus@5908
   118
    return (system_version >= 0x1070);
icculus@5908
   119
}
icculus@5908
   120
icculus@5639
   121
static inline BOOL IS_SNOW_LEOPARD_OR_LATER(_THIS)
icculus@5639
   122
{
icculus@5639
   123
    return (system_version >= 0x1060);
icculus@5639
   124
}
icculus@5639
   125
icculus@5639
   126
static void QZ_ReleaseDisplayMode(_THIS, const void *moderef)
icculus@5639
   127
{
icculus@5639
   128
    /* we only own these references in the 10.6+ API. */
icculus@5645
   129
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   130
    if (use_new_mode_apis) {
icculus@5639
   131
        CGDisplayModeRelease((CGDisplayModeRef) moderef);
icculus@5639
   132
    }
icculus@5639
   133
#endif
icculus@5639
   134
}
icculus@5639
   135
icculus@5639
   136
static void QZ_ReleaseDisplayModeList(_THIS, CFArrayRef mode_list)
icculus@5639
   137
{
icculus@5639
   138
    /* we only own these references in the 10.6+ API. */
icculus@5645
   139
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   140
    if (use_new_mode_apis) {
icculus@5639
   141
        CFRelease(mode_list);
icculus@5639
   142
    }
icculus@5639
   143
#endif
icculus@5639
   144
}
icculus@5639
   145
icculus@5639
   146
slouken@47
   147
/* Bootstrap functions */
slouken@4317
   148
static int QZ_Available ()
slouken@4317
   149
{
slouken@47
   150
    return 1;
slouken@47
   151
}
slouken@47
   152
slouken@4317
   153
static SDL_VideoDevice* QZ_CreateDevice (int device_index)
slouken@4317
   154
{
slouken@272
   155
#pragma unused (device_index)
slouken@47
   156
slouken@47
   157
    SDL_VideoDevice *device;
slouken@47
   158
    SDL_PrivateVideoData *hidden;
slouken@47
   159
slouken@1756
   160
    device = (SDL_VideoDevice*) SDL_malloc (sizeof (*device) );
slouken@1756
   161
    hidden = (SDL_PrivateVideoData*) SDL_malloc (sizeof (*hidden) );
slouken@47
   162
slouken@47
   163
    if (device == NULL || hidden == NULL)
slouken@47
   164
        SDL_OutOfMemory ();
slouken@47
   165
slouken@1756
   166
    SDL_memset (device, 0, sizeof (*device) );
slouken@1756
   167
    SDL_memset (hidden, 0, sizeof (*hidden) );
slouken@272
   168
slouken@47
   169
    device->hidden = hidden;
slouken@47
   170
slouken@47
   171
    device->VideoInit        = QZ_VideoInit;
slouken@47
   172
    device->ListModes        = QZ_ListModes;
slouken@47
   173
    device->SetVideoMode     = QZ_SetVideoMode;
slouken@47
   174
    device->ToggleFullScreen = QZ_ToggleFullScreen;
icculus@1212
   175
    device->UpdateMouse      = QZ_UpdateMouse;
slouken@47
   176
    device->SetColors        = QZ_SetColors;
icculus@5908
   177
    /* device->UpdateRects      = QZ_UpdateRects; this is determined by SetVideoMode() */
slouken@47
   178
    device->VideoQuit        = QZ_VideoQuit;
slouken@272
   179
slouken@47
   180
    device->LockHWSurface   = QZ_LockHWSurface;
slouken@47
   181
    device->UnlockHWSurface = QZ_UnlockHWSurface;
icculus@1120
   182
    device->AllocHWSurface   = QZ_AllocHWSurface;
slouken@47
   183
    device->FreeHWSurface   = QZ_FreeHWSurface;
slouken@47
   184
slouken@47
   185
    device->SetGamma     = QZ_SetGamma;
slouken@47
   186
    device->GetGamma     = QZ_GetGamma;
slouken@47
   187
    device->SetGammaRamp = QZ_SetGammaRamp;
slouken@47
   188
    device->GetGammaRamp = QZ_GetGammaRamp;
slouken@47
   189
slouken@47
   190
    device->GL_GetProcAddress = QZ_GL_GetProcAddress;
slouken@47
   191
    device->GL_GetAttribute   = QZ_GL_GetAttribute;
slouken@47
   192
    device->GL_MakeCurrent    = QZ_GL_MakeCurrent;
slouken@47
   193
    device->GL_SwapBuffers    = QZ_GL_SwapBuffers;
slouken@47
   194
    device->GL_LoadLibrary    = QZ_GL_LoadLibrary;
slouken@272
   195
slouken@47
   196
    device->FreeWMCursor   = QZ_FreeWMCursor;
slouken@47
   197
    device->CreateWMCursor = QZ_CreateWMCursor;
slouken@47
   198
    device->ShowWMCursor   = QZ_ShowWMCursor;
slouken@47
   199
    device->WarpWMCursor   = QZ_WarpWMCursor;
slouken@47
   200
    device->MoveWMCursor   = QZ_MoveWMCursor;
slouken@47
   201
    device->CheckMouseMode = QZ_CheckMouseMode;
slouken@47
   202
    device->InitOSKeymap   = QZ_InitOSKeymap;
slouken@47
   203
    device->PumpEvents     = QZ_PumpEvents;
slouken@47
   204
slouken@47
   205
    device->SetCaption    = QZ_SetCaption;
slouken@47
   206
    device->SetIcon       = QZ_SetIcon;
slouken@47
   207
    device->IconifyWindow = QZ_IconifyWindow;
slouken@47
   208
    /*device->GetWMInfo     = QZ_GetWMInfo;*/
slouken@47
   209
    device->GrabInput     = QZ_GrabInput;
slouken@272
   210
icculus@4204
   211
    /*
icculus@4204
   212
     * This is a big hassle, needing QuickDraw and QuickTime on older
icculus@4204
   213
     *  systems, and god knows what on 10.6, so we immediately fail here,
icculus@4204
   214
     *  which causes SDL to make an RGB surface and manage the YUV overlay
icculus@4204
   215
     *  in software. Sorry. Use SDL 1.3 if you want YUV rendering in a pixel
icculus@4204
   216
     *  shader.  :)
icculus@4204
   217
     */
icculus@4204
   218
    /*device->CreateYUVOverlay = QZ_CreateYUVOverlay;*/
slouken@390
   219
slouken@390
   220
    device->free             = QZ_DeleteDevice;
slouken@272
   221
slouken@47
   222
    return device;
slouken@47
   223
}
slouken@47
   224
slouken@4317
   225
static void QZ_DeleteDevice (SDL_VideoDevice *device)
slouken@4317
   226
{
icculus@5628
   227
    _THIS = device;
icculus@5639
   228
    QZ_ReleaseDisplayMode(this, save_mode);
icculus@5639
   229
    QZ_ReleaseDisplayMode(this, mode);
slouken@1756
   230
    SDL_free (device->hidden);
slouken@1756
   231
    SDL_free (device);
slouken@47
   232
}
slouken@47
   233
icculus@5628
   234
static void QZ_GetModeInfo(_THIS, const void *_mode, Uint32 *w, Uint32 *h, Uint32 *bpp)
icculus@5628
   235
{
icculus@5628
   236
    *w = *h = *bpp = 0;
icculus@5628
   237
    if (_mode == NULL) {
icculus@5628
   238
        return;
icculus@5628
   239
    }
icculus@5628
   240
icculus@5645
   241
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   242
    if (use_new_mode_apis) {
icculus@5628
   243
        CGDisplayModeRef vidmode = (CGDisplayModeRef) _mode;
icculus@5628
   244
        CFStringRef fmt = CGDisplayModeCopyPixelEncoding(vidmode);
icculus@5628
   245
icculus@5628
   246
        *w = (Uint32) CGDisplayModeGetWidth(vidmode);
icculus@5628
   247
        *h = (Uint32) CGDisplayModeGetHeight(vidmode);
icculus@5628
   248
icculus@5628
   249
        /* we only care about the 32-bit modes... */
icculus@5628
   250
        if (CFStringCompare(fmt, CFSTR(IO32BitDirectPixels),
icculus@5628
   251
                            kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
icculus@5628
   252
            *bpp = 32;
icculus@5628
   253
        }
icculus@5628
   254
icculus@5628
   255
        CFRelease(fmt);
icculus@5639
   256
    }
icculus@5628
   257
#endif
icculus@5639
   258
icculus@5645
   259
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
icculus@5945
   260
    if (!use_new_mode_apis) {
icculus@5628
   261
        CFDictionaryRef vidmode = (CFDictionaryRef) _mode;
icculus@5628
   262
        CFNumberGetValue (
icculus@5628
   263
            CFDictionaryGetValue (vidmode, kCGDisplayBitsPerPixel),
icculus@5628
   264
            kCFNumberSInt32Type, bpp);
icculus@5628
   265
icculus@5628
   266
        CFNumberGetValue (
icculus@5628
   267
            CFDictionaryGetValue (vidmode, kCGDisplayWidth),
icculus@5628
   268
            kCFNumberSInt32Type, w);
icculus@5628
   269
icculus@5628
   270
        CFNumberGetValue (
icculus@5628
   271
            CFDictionaryGetValue (vidmode, kCGDisplayHeight),
icculus@5628
   272
            kCFNumberSInt32Type, h);
icculus@5639
   273
    }
icculus@5628
   274
#endif
icculus@5628
   275
icculus@5628
   276
    /* we only care about the 32-bit modes... */
icculus@5628
   277
    if (*bpp != 32) {
icculus@5628
   278
        *bpp = 0;
icculus@5628
   279
    }
icculus@5628
   280
}
icculus@5628
   281
slouken@4317
   282
static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format)
slouken@4317
   283
{
slouken@4049
   284
    NSRect r = NSMakeRect(0.0, 0.0, 0.0, 0.0);
icculus@3936
   285
    const char *env = NULL;
icculus@5628
   286
icculus@5628
   287
    if ( Gestalt(gestaltSystemVersion, &system_version) != noErr )
icculus@5628
   288
        system_version = 0;
icculus@5628
   289
icculus@5945
   290
    use_new_mode_apis = NO;
icculus@5945
   291
icculus@5945
   292
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   293
    use_new_mode_apis = IS_SNOW_LEOPARD_OR_LATER(this);
icculus@5945
   294
#endif
icculus@5945
   295
slouken@272
   296
    /* Initialize the video settings; this data persists between mode switches */
slouken@272
   297
    display_id = kCGDirectMainDisplay;
slouken@4317
   298
slouken@4319
   299
#if 0 /* The mouse event code needs to take this into account... */
slouken@4319
   300
    env = getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
slouken@4319
   301
    if ( env ) {
slouken@4319
   302
        int monitor = SDL_atoi(env);
slouken@4319
   303
    	CGDirectDisplayID activeDspys [3];
slouken@4319
   304
    	CGDisplayCount dspyCnt;
slouken@4319
   305
    	CGGetActiveDisplayList (3, activeDspys, &dspyCnt);
slouken@4319
   306
        if ( monitor >= 0 && monitor < dspyCnt ) {
slouken@4319
   307
    	    display_id = activeDspys[monitor];
slouken@4319
   308
        }
slouken@4319
   309
    }
slouken@4319
   310
#endif
slouken@4319
   311
icculus@5645
   312
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   313
    if (use_new_mode_apis) {
icculus@5628
   314
        save_mode = CGDisplayCopyDisplayMode(display_id);
icculus@5628
   315
    }
icculus@5628
   316
#endif
icculus@5628
   317
icculus@5645
   318
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
icculus@5945
   319
    if (!use_new_mode_apis) {
icculus@5628
   320
        save_mode = CGDisplayCurrentMode(display_id);
icculus@5628
   321
    }
icculus@5628
   322
#endif
icculus@5628
   323
icculus@5908
   324
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
   325
    if (!IS_LION_OR_LATER(this)) {
icculus@5908
   326
        palette = CGPaletteCreateDefaultColorPalette();
icculus@5908
   327
    }
icculus@5908
   328
#endif
icculus@5908
   329
icculus@5628
   330
    if (save_mode == NULL) {
icculus@5628
   331
        SDL_SetError("Couldn't figure out current display mode.");
icculus@5628
   332
        return -1;
icculus@5628
   333
    }
slouken@272
   334
slouken@4139
   335
    /* Allow environment override of screensaver disable. */
icculus@3936
   336
    env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
slouken@4139
   337
    if ( env ) {
slouken@4139
   338
        allow_screensaver = SDL_atoi(env);
slouken@4139
   339
    } else {
slouken@4139
   340
#ifdef SDL_VIDEO_DISABLE_SCREENSAVER
slouken@4139
   341
        allow_screensaver = 0;
slouken@4139
   342
#else
slouken@4139
   343
        allow_screensaver = 1;
slouken@4139
   344
#endif
slouken@4139
   345
    }
icculus@3936
   346
slouken@272
   347
    /* Gather some information that is useful to know about the display */
icculus@5628
   348
    QZ_GetModeInfo(this, save_mode, &device_width, &device_height, &device_bpp);
icculus@5628
   349
    if (device_bpp == 0) {
icculus@5639
   350
        QZ_ReleaseDisplayMode(this, save_mode);
icculus@5628
   351
        save_mode = NULL;
icculus@5628
   352
        SDL_SetError("Unsupported display mode");
icculus@5628
   353
        return -1;
icculus@5628
   354
    }
slouken@272
   355
slouken@1545
   356
    /* Determine the current screen size */
slouken@1545
   357
    this->info.current_w = device_width;
slouken@1545
   358
    this->info.current_h = device_height;
slouken@1545
   359
slouken@1545
   360
    /* Determine the default screen depth */
slouken@272
   361
    video_format->BitsPerPixel = device_bpp;
slouken@272
   362
slouken@501
   363
    /* Set misc globals */
slouken@501
   364
    current_grab_mode = SDL_GRAB_OFF;
slouken@761
   365
    cursor_should_be_visible    = YES;
slouken@779
   366
    cursor_visible              = YES;
slouken@823
   367
    current_mods = 0;
slouken@4123
   368
    field_edit =  [[SDLTranslatorResponder alloc] initWithFrame:r];
icculus@5628
   369
slouken@555
   370
    /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
slouken@555
   371
    QZ_RegisterForSleepNotifications (this);
slouken@555
   372
    
slouken@1271
   373
    /* Fill in some window manager capabilities */
slouken@1271
   374
    this->info.wm_available = 1;
slouken@1271
   375
slouken@272
   376
    return 0;
slouken@47
   377
}
slouken@47
   378
slouken@4317
   379
static SDL_Rect** QZ_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@4317
   380
{
icculus@5628
   381
    CFArrayRef mode_list = NULL;          /* list of available fullscreen modes */
slouken@272
   382
    CFIndex num_modes;
slouken@47
   383
    CFIndex i;
slouken@47
   384
slouken@47
   385
    int list_size = 0;
slouken@390
   386
slouken@47
   387
    /* Any windowed mode is acceptable */
slouken@47
   388
    if ( (flags & SDL_FULLSCREEN) == 0 )
slouken@47
   389
        return (SDL_Rect**)-1;
slouken@390
   390
slouken@47
   391
    /* Free memory from previous call, if any */
slouken@501
   392
    if ( client_mode_list != NULL ) {
slouken@390
   393
        int i;
slouken@47
   394
slouken@501
   395
        for (i = 0; client_mode_list[i] != NULL; i++)
slouken@1756
   396
            SDL_free (client_mode_list[i]);
slouken@47
   397
slouken@1756
   398
        SDL_free (client_mode_list);
slouken@501
   399
        client_mode_list = NULL;
slouken@47
   400
    }
slouken@390
   401
icculus@5645
   402
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   403
    if (use_new_mode_apis) {
icculus@5628
   404
        mode_list = CGDisplayCopyAllDisplayModes(display_id, NULL);
icculus@5639
   405
    }
icculus@5628
   406
#endif
icculus@5639
   407
icculus@5645
   408
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
icculus@5945
   409
    if (!use_new_mode_apis) {
icculus@5628
   410
        mode_list = CGDisplayAvailableModes(display_id);
icculus@5639
   411
    }
icculus@5628
   412
#endif
icculus@5628
   413
slouken@272
   414
    num_modes = CFArrayGetCount (mode_list);
slouken@272
   415
slouken@47
   416
    /* Build list of modes with the requested bpp */
slouken@155
   417
    for (i = 0; i < num_modes; i++) {
icculus@5628
   418
        Uint32 width, height, bpp;
icculus@5628
   419
        const void *onemode = CFArrayGetValueAtIndex(mode_list, i);
slouken@390
   420
icculus@5628
   421
        QZ_GetModeInfo(this, onemode, &width, &height, &bpp);
slouken@47
   422
icculus@5628
   423
        if (bpp && (bpp == format->BitsPerPixel)) {
icculus@5628
   424
            int hasMode = SDL_FALSE;
icculus@5628
   425
            int i;
slouken@390
   426
slouken@390
   427
            /* Check if mode is already in the list */
icculus@5628
   428
            for (i = 0; i < list_size; i++) {
icculus@5628
   429
                if (client_mode_list[i]->w == width &&
icculus@5628
   430
                    client_mode_list[i]->h == height) {
slouken@390
   431
                        hasMode = SDL_TRUE;
slouken@390
   432
                        break;
slouken@155
   433
                }
slouken@155
   434
            }
slouken@390
   435
slouken@390
   436
            /* Grow the list and add mode to the list */
slouken@390
   437
            if ( ! hasMode ) {
slouken@390
   438
                SDL_Rect *rect;
slouken@390
   439
slouken@390
   440
                list_size++;
slouken@390
   441
slouken@501
   442
                if (client_mode_list == NULL)
slouken@501
   443
                    client_mode_list = (SDL_Rect**) 
slouken@1756
   444
                        SDL_malloc (sizeof(*client_mode_list) * (list_size+1) );
icculus@5628
   445
                else {
icculus@5628
   446
                    /* !!! FIXME: this leaks memory if SDL_realloc() fails! */
icculus@5628
   447
                    client_mode_list = (SDL_Rect**)
slouken@1756
   448
                        SDL_realloc (client_mode_list, sizeof(*client_mode_list) * (list_size+1));
icculus@5628
   449
                }
slouken@390
   450
slouken@1756
   451
                rect = (SDL_Rect*) SDL_malloc (sizeof(**client_mode_list));
slouken@47
   452
slouken@501
   453
                if (client_mode_list == NULL || rect == NULL) {
icculus@5639
   454
                    QZ_ReleaseDisplayModeList(this, mode_list);
slouken@390
   455
                    SDL_OutOfMemory ();
slouken@390
   456
                    return NULL;
slouken@390
   457
                }
slouken@390
   458
icculus@1218
   459
                rect->x = rect->y = 0;
slouken@390
   460
                rect->w = width;
slouken@390
   461
                rect->h = height;
slouken@390
   462
slouken@501
   463
                client_mode_list[list_size-1] = rect;
slouken@501
   464
                client_mode_list[list_size]   = NULL;
slouken@272
   465
            }
slouken@47
   466
        }
slouken@47
   467
    }
slouken@390
   468
icculus@5639
   469
    QZ_ReleaseDisplayModeList(this, mode_list);
icculus@5628
   470
slouken@155
   471
    /* Sort list largest to smallest (by area) */
slouken@155
   472
    {
slouken@155
   473
        int i, j;
slouken@155
   474
        for (i = 0; i < list_size; i++) {
slouken@155
   475
            for (j = 0; j < list_size-1; j++) {
slouken@390
   476
slouken@155
   477
                int area1, area2;
slouken@501
   478
                area1 = client_mode_list[j]->w * client_mode_list[j]->h;
slouken@501
   479
                area2 = client_mode_list[j+1]->w * client_mode_list[j+1]->h;
slouken@390
   480
slouken@155
   481
                if (area1 < area2) {
slouken@501
   482
                    SDL_Rect *tmp = client_mode_list[j];
slouken@501
   483
                    client_mode_list[j] = client_mode_list[j+1];
slouken@501
   484
                    client_mode_list[j+1] = tmp;
slouken@155
   485
                }
slouken@155
   486
            }
slouken@155
   487
        }
slouken@155
   488
    }
icculus@5628
   489
slouken@501
   490
    return client_mode_list;
slouken@47
   491
}
slouken@47
   492
slouken@657
   493
static SDL_bool QZ_WindowPosition(_THIS, int *x, int *y)
slouken@657
   494
{
slouken@657
   495
    const char *window = getenv("SDL_VIDEO_WINDOW_POS");
slouken@657
   496
    if ( window ) {
slouken@657
   497
        if ( sscanf(window, "%d,%d", x, y) == 2 ) {
slouken@657
   498
            return SDL_TRUE;
slouken@657
   499
        }
slouken@657
   500
    }
slouken@657
   501
    return SDL_FALSE;
slouken@657
   502
}
slouken@657
   503
icculus@5628
   504
static CGError QZ_SetDisplayMode(_THIS, const void *vidmode)
icculus@5628
   505
{
icculus@5645
   506
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   507
    if (use_new_mode_apis) {
icculus@5628
   508
        return CGDisplaySetDisplayMode(display_id, (CGDisplayModeRef) vidmode, NULL);
icculus@5639
   509
    }
icculus@5628
   510
#endif
icculus@5639
   511
icculus@5645
   512
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
icculus@5945
   513
    if (!use_new_mode_apis) {
icculus@5628
   514
        return CGDisplaySwitchToMode(display_id, (CFDictionaryRef) vidmode);
icculus@5639
   515
    }
icculus@5628
   516
#endif
icculus@5628
   517
icculus@5628
   518
    return kCGErrorFailure;
icculus@5628
   519
}
icculus@5628
   520
icculus@5628
   521
static inline CGError QZ_RestoreDisplayMode(_THIS)
icculus@5628
   522
{
icculus@5628
   523
    return QZ_SetDisplayMode(this, save_mode);
icculus@5628
   524
}
icculus@5628
   525
slouken@4317
   526
static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop)
slouken@4317
   527
{
slouken@47
   528
    /* Reset values that may change between switches */
slouken@501
   529
    this->info.blit_fill  = 0;
icculus@5908
   530
    this->FillHWRect      = NULL;
icculus@5908
   531
    this->UpdateRects     = NULL;
icculus@5908
   532
    this->LockHWSurface   = NULL;
icculus@5908
   533
    this->UnlockHWSurface = NULL;
icculus@5569
   534
icculus@4204
   535
    if (cg_context) {
icculus@4204
   536
        CGContextFlush (cg_context);
icculus@4204
   537
        CGContextRelease (cg_context);
icculus@4204
   538
        cg_context = nil;
icculus@4204
   539
    }
icculus@4204
   540
    
slouken@272
   541
    /* Release fullscreen resources */
slouken@47
   542
    if ( mode_flags & SDL_FULLSCREEN ) {
slouken@272
   543
slouken@435
   544
        NSRect screen_rect;
icculus@5569
   545
icculus@5908
   546
        /*  Release double buffer stuff */
icculus@5908
   547
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
   548
        if ( !IS_LION_OR_LATER(this) && (mode_flags & SDL_DOUBLEBUF) ) {
icculus@5908
   549
            quit_thread = YES;
icculus@5908
   550
            SDL_SemPost (sem1);
icculus@5908
   551
            SDL_WaitThread (thread, NULL);
icculus@5908
   552
            SDL_DestroySemaphore (sem1);
icculus@5908
   553
            SDL_DestroySemaphore (sem2);
icculus@5908
   554
            SDL_free (sw_buffers[0]);
icculus@5908
   555
        }
icculus@5908
   556
#endif
icculus@5908
   557
slouken@4065
   558
        /* If we still have a valid window, close it. */
slouken@4065
   559
        if ( qz_window ) {
slouken@4090
   560
            NSCAssert([ qz_window delegate ] == nil, @"full screen window shouldn't have a delegate"); /* if that should ever change, we'd have to release it here */
slouken@4090
   561
            [ qz_window close ]; /* includes release because [qz_window isReleasedWhenClosed] */
slouken@4065
   562
            qz_window = nil;
slouken@4065
   563
            window_view = nil;
slouken@4065
   564
        }
slouken@501
   565
        /* 
slouken@501
   566
            Release the OpenGL context
slouken@501
   567
            Do this first to avoid trash on the display before fade
slouken@501
   568
        */
slouken@501
   569
        if ( mode_flags & SDL_OPENGL ) {
slouken@501
   570
        
slouken@272
   571
            QZ_TearDownOpenGL (this);
icculus@5606
   572
            #ifdef __powerpc__  /* we only use this for pre-10.3 compatibility. */
slouken@501
   573
            CGLSetFullScreen (NULL);
icculus@5606
   574
            #endif
slouken@501
   575
        }
icculus@1340
   576
        if (to_desktop) {
icculus@5986
   577
            /* !!! FIXME: keep an eye on this.
icculus@5986
   578
             * This API is officially unavailable for 64-bit binaries.
icculus@5986
   579
             *  It happens to work, as of 10.7, but we're going to see if
icculus@5986
   580
             *  we can just simply do without it on newer OSes...
icculus@5986
   581
             */
icculus@5986
   582
            #if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070) && !defined(__LP64__)
icculus@5986
   583
            if ( !IS_LION_OR_LATER(this) ) {
icculus@5986
   584
                ShowMenuBar ();
icculus@5986
   585
            }
icculus@5986
   586
            #endif
icculus@5986
   587
icculus@1340
   588
            /* Restore original screen resolution/bpp */
icculus@5628
   589
            QZ_RestoreDisplayMode (this);
icculus@1340
   590
            CGReleaseAllDisplays ();
icculus@1340
   591
            /* 
icculus@1340
   592
                Reset the main screen's rectangle
icculus@1340
   593
                See comment in QZ_SetVideoFullscreen for why we do this
icculus@1340
   594
            */
icculus@1340
   595
            screen_rect = NSMakeRect(0,0,device_width,device_height);
icculus@4204
   596
            QZ_SetFrame([ NSScreen mainScreen ], screen_rect);
icculus@1340
   597
        }
slouken@47
   598
    }
slouken@272
   599
    /* Release window mode resources */
slouken@390
   600
    else {
slouken@4090
   601
        id delegate = [ qz_window delegate ];
slouken@4090
   602
        [ qz_window close ]; /* includes release because [qz_window isReleasedWhenClosed] */
slouken@4090
   603
        if (delegate != nil) [ delegate release ];
slouken@390
   604
        qz_window = nil;
slouken@501
   605
        window_view = nil;
icculus@1160
   606
slouken@272
   607
        /* Release the OpenGL context */
slouken@272
   608
        if ( mode_flags & SDL_OPENGL )
slouken@272
   609
            QZ_TearDownOpenGL (this);
slouken@47
   610
    }
slouken@272
   611
slouken@155
   612
    /* Signal successful teardown */
slouken@155
   613
    video_set = SDL_FALSE;
slouken@47
   614
}
slouken@47
   615
icculus@5628
   616
static const void *QZ_BestMode(_THIS, const int bpp, const int w, const int h)
icculus@5628
   617
{
icculus@5628
   618
    const void *best = NULL;
icculus@5628
   619
icculus@5628
   620
    if (bpp == 0) {
icculus@5628
   621
        return NULL;
icculus@5628
   622
    }
icculus@5628
   623
icculus@5645
   624
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
icculus@5945
   625
    if (use_new_mode_apis) {
icculus@5628
   626
        /* apparently, we have to roll our own now. :/ */
icculus@5628
   627
        CFArrayRef mode_list = CGDisplayCopyAllDisplayModes(display_id, NULL);
icculus@5628
   628
        if (mode_list != NULL) {
icculus@5628
   629
            const CFIndex num_modes = CFArrayGetCount(mode_list);
icculus@5628
   630
            CFIndex i;
icculus@5628
   631
            for (i = 0; i < num_modes; i++) {
icculus@5628
   632
                const void *vidmode = CFArrayGetValueAtIndex(mode_list, i);
icculus@5628
   633
                Uint32 thisw, thish, thisbpp;
icculus@5628
   634
                QZ_GetModeInfo(this, vidmode, &thisw, &thish, &thisbpp);
icculus@5628
   635
icculus@5628
   636
                /* We only care about exact matches, apparently. */
icculus@5628
   637
                if ((thisbpp == bpp) && (thisw == w) && (thish == h)) {
icculus@5628
   638
                    best = vidmode;
icculus@5628
   639
                    break;  /* got it! */
icculus@5628
   640
                }
icculus@5628
   641
            }
icculus@5628
   642
            CGDisplayModeRetain((CGDisplayModeRef) best);  /* NULL is ok */
icculus@5628
   643
            CFRelease(mode_list);
icculus@5628
   644
        }
icculus@5639
   645
    }
icculus@5628
   646
#endif
icculus@5639
   647
icculus@5645
   648
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
icculus@5945
   649
    if (!use_new_mode_apis) {
icculus@5628
   650
        boolean_t exact = 0;
icculus@5628
   651
        best = CGDisplayBestModeForParameters(display_id, bpp, w, h, &exact);
icculus@5628
   652
        if (!exact) {
icculus@5628
   653
            best = NULL;
icculus@5628
   654
        }
icculus@5639
   655
    }
icculus@5628
   656
#endif
icculus@5639
   657
icculus@5628
   658
    return best;
icculus@5628
   659
}
icculus@5628
   660
slouken@47
   661
static SDL_Surface* QZ_SetVideoFullScreen (_THIS, SDL_Surface *current, int width,
slouken@4317
   662
                                           int height, int bpp, Uint32 flags)
slouken@4317
   663
{
icculus@5908
   664
    const BOOL isLion = IS_LION_OR_LATER(this);
slouken@435
   665
    NSRect screen_rect;
slouken@588
   666
    CGError error;
slouken@4065
   667
    NSRect contentRect;
icculus@1340
   668
    CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
icculus@4204
   669
icculus@5569
   670
    current->flags = SDL_FULLSCREEN;
icculus@5569
   671
    current->w = width;
icculus@5569
   672
    current->h = height;
icculus@5569
   673
icculus@4442
   674
    contentRect = NSMakeRect (0, 0, width, height);
icculus@4442
   675
icculus@1340
   676
    /* Fade to black to hide resolution-switching flicker (and garbage
icculus@1340
   677
       that is displayed by a destroyed OpenGL context, if applicable) */
icculus@1340
   678
    if ( CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess ) {
icculus@1340
   679
        CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
icculus@1340
   680
    }
slouken@435
   681
    
slouken@501
   682
    /* Destroy any previous mode */
slouken@501
   683
    if (video_set == SDL_TRUE)
icculus@1340
   684
        QZ_UnsetVideoMode (this, FALSE);
slouken@501
   685
icculus@4204
   686
    /* Sorry, QuickDraw was ripped out. */
icculus@4204
   687
    if (getenv("SDL_NSWindowPointer") || getenv("SDL_NSQuickDrawViewPointer")) {
icculus@4204
   688
        SDL_SetError ("Embedded QuickDraw windows are no longer supported");
icculus@4204
   689
        goto ERR_NO_MATCH;
icculus@4204
   690
    }
icculus@4204
   691
icculus@5639
   692
    QZ_ReleaseDisplayMode(this, mode);  /* NULL is okay. */
icculus@5628
   693
slouken@47
   694
    /* See if requested mode exists */
icculus@5628
   695
    mode = QZ_BestMode(this, bpp, width, height);
slouken@390
   696
slouken@47
   697
    /* Require an exact match to the requested mode */
icculus@5628
   698
    if ( mode == NULL ) {
slouken@501
   699
        SDL_SetError ("Failed to find display resolution: %dx%dx%d", width, height, bpp);
slouken@47
   700
        goto ERR_NO_MATCH;
slouken@47
   701
    }
slouken@155
   702
slouken@47
   703
    /* Put up the blanking window (a window above all other windows) */
slouken@588
   704
    if (getenv ("SDL_SINGLEDISPLAY"))
slouken@588
   705
        error = CGDisplayCapture (display_id);
slouken@588
   706
    else
slouken@588
   707
        error = CGCaptureAllDisplays ();
slouken@588
   708
        
slouken@588
   709
    if ( CGDisplayNoErr != error ) {
slouken@47
   710
        SDL_SetError ("Failed capturing display");
slouken@47
   711
        goto ERR_NO_CAPTURE;
slouken@47
   712
    }
slouken@272
   713
slouken@47
   714
    /* Do the physical switch */
icculus@5628
   715
    if ( CGDisplayNoErr != QZ_SetDisplayMode(this, mode) ) {
slouken@47
   716
        SDL_SetError ("Failed switching display resolution");
slouken@47
   717
        goto ERR_NO_SWITCH;
slouken@47
   718
    }
slouken@272
   719
icculus@5908
   720
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
   721
    if ( !isLion ) {
icculus@5908
   722
        current->pixels = (Uint32*) CGDisplayBaseAddress (display_id);
icculus@5908
   723
        current->pitch  = CGDisplayBytesPerRow (display_id);
icculus@5908
   724
icculus@5908
   725
        current->flags |= SDL_HWSURFACE;
icculus@5908
   726
        current->flags |= SDL_PREALLOC;
icculus@5908
   727
        /* current->hwdata = (void *) CGDisplayGetDrawingContext (display_id); */
icculus@5908
   728
icculus@5908
   729
        this->UpdateRects     = QZ_DirectUpdate;
icculus@5908
   730
        this->LockHWSurface   = QZ_LockHWSurface;
icculus@5908
   731
        this->UnlockHWSurface = QZ_UnlockHWSurface;
icculus@5908
   732
icculus@5908
   733
        /* Setup double-buffer emulation */
icculus@5908
   734
        if ( flags & SDL_DOUBLEBUF ) {
icculus@5908
   735
        
icculus@5908
   736
            /*
icculus@5908
   737
            Setup a software backing store for reasonable results when
icculus@5908
   738
            double buffering is requested (since a single-buffered hardware
icculus@5908
   739
            surface looks hideous).
icculus@5908
   740
            
icculus@5908
   741
            The actual screen blit occurs in a separate thread to allow 
icculus@5908
   742
            other blitting while waiting on the VBL (and hence results in higher framerates).
icculus@5908
   743
            */
icculus@5908
   744
            this->LockHWSurface = NULL;
icculus@5908
   745
            this->UnlockHWSurface = NULL;
icculus@5908
   746
            this->UpdateRects = NULL;
icculus@5908
   747
        
icculus@5908
   748
            current->flags |= (SDL_HWSURFACE|SDL_DOUBLEBUF);
icculus@5908
   749
            this->UpdateRects = QZ_DoubleBufferUpdate;
icculus@5908
   750
            this->LockHWSurface = QZ_LockDoubleBuffer;
icculus@5908
   751
            this->UnlockHWSurface = QZ_UnlockDoubleBuffer;
icculus@5908
   752
            this->FlipHWSurface = QZ_FlipDoubleBuffer;
icculus@5908
   753
icculus@5908
   754
            current->pixels = SDL_malloc (current->pitch * current->h * 2);
icculus@5908
   755
            if (current->pixels == NULL) {
icculus@5908
   756
                SDL_OutOfMemory ();
icculus@5908
   757
                goto ERR_DOUBLEBUF;
icculus@5908
   758
            }
icculus@5908
   759
        
icculus@5908
   760
            sw_buffers[0] = current->pixels;
icculus@5908
   761
            sw_buffers[1] = (Uint8*)current->pixels + current->pitch * current->h;
icculus@5908
   762
        
icculus@5908
   763
            quit_thread = NO;
icculus@5908
   764
            sem1 = SDL_CreateSemaphore (0);
icculus@5908
   765
            sem2 = SDL_CreateSemaphore (1);
icculus@5908
   766
            thread = SDL_CreateThread ((int (*)(void *))QZ_ThreadFlip, this);
icculus@5908
   767
        }
icculus@5908
   768
icculus@5908
   769
        if ( CGDisplayCanSetPalette (display_id) )
icculus@5908
   770
            current->flags |= SDL_HWPALETTE;
icculus@5908
   771
    }
icculus@5908
   772
#endif
icculus@5908
   773
slouken@4065
   774
    /* Check if we should recreate the window */
slouken@4065
   775
    if (qz_window == nil) {
slouken@4065
   776
        /* Manually create a window, avoids having a nib file resource */
slouken@4065
   777
        qz_window = [ [ SDL_QuartzWindow alloc ] 
slouken@4065
   778
            initWithContentRect:contentRect
icculus@5908
   779
                styleMask:(isLion ? NSBorderlessWindowMask : 0)
slouken@4065
   780
                    backing:NSBackingStoreBuffered
slouken@4065
   781
                        defer:NO ];
slouken@4065
   782
slouken@4065
   783
        if (qz_window != nil) {
slouken@4065
   784
            [ qz_window setAcceptsMouseMovedEvents:YES ];
slouken@4065
   785
            [ qz_window setViewsNeedDisplay:NO ];
icculus@5908
   786
            if (isLion) {
icculus@5908
   787
                [ qz_window setContentView: [ [ [ SDL_QuartzView alloc ] init ] autorelease ] ];
icculus@5908
   788
            }
slouken@4065
   789
        }
slouken@4065
   790
    }
slouken@4065
   791
    /* We already have a window, just change its size */
slouken@4065
   792
    else {
icculus@4267
   793
        [ qz_window setContentSize:contentRect.size ];
icculus@4267
   794
        current->flags |= (SDL_NOFRAME|SDL_RESIZABLE) & mode_flags;
icculus@4267
   795
        [ window_view setFrameSize:contentRect.size ];
slouken@4065
   796
    }
slouken@4065
   797
slouken@47
   798
    /* Setup OpenGL for a fullscreen context */
slouken@47
   799
    if (flags & SDL_OPENGL) {
slouken@47
   800
slouken@47
   801
        if ( ! QZ_SetupOpenGL (this, bpp, flags) ) {
slouken@272
   802
            goto ERR_NO_GL;
slouken@47
   803
        }
slouken@390
   804
slouken@4065
   805
        /* Initialize the NSView and add it to our window.  The presence of a valid window and
slouken@4065
   806
           view allow the cursor to be changed whilst in fullscreen.*/
slouken@4065
   807
        window_view = [ [ NSView alloc ] initWithFrame:contentRect ];
slouken@4065
   808
icculus@5908
   809
        if ( isLion ) {
icculus@5908
   810
            [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
slouken@47
   811
        }
slouken@390
   812
icculus@5908
   813
        [ [ qz_window contentView ] addSubview:window_view ];
icculus@5908
   814
icculus@5946
   815
        /* Apparently Lion checks some version flag set by the linker
icculus@5946
   816
           and changes API behavior. Annoying. */
icculus@5946
   817
#if (MAC_OS_X_VERSION_MAX_ALLOWED < 1070)
icculus@6048
   818
        {
icculus@6048
   819
            CGLError err;
icculus@6048
   820
            CGLContextObj ctx;
icculus@6048
   821
icculus@6048
   822
            [ qz_window setLevel:NSNormalWindowLevel ];
icculus@6048
   823
            ctx = QZ_GetCGLContextObj (gl_context);
icculus@6048
   824
            err = CGLSetFullScreen (ctx);
icculus@5946
   825
    
icculus@6048
   826
            if (err) {
icculus@6048
   827
                SDL_SetError ("Error setting OpenGL fullscreen: %s", CGLErrorString(err));
icculus@6048
   828
                goto ERR_NO_GL;
icculus@6048
   829
            }
icculus@5908
   830
        }
icculus@5946
   831
#else
icculus@5946
   832
        [ qz_window setLevel:CGShieldingWindowLevel() ];
icculus@5946
   833
        [ gl_context setView: window_view ];
icculus@5946
   834
        [ gl_context setFullScreen ];
icculus@5946
   835
        [ gl_context update ];
icculus@5908
   836
#endif
icculus@5908
   837
icculus@5908
   838
        [ window_view release ];
slouken@47
   839
        [ gl_context makeCurrentContext];
slouken@272
   840
slouken@272
   841
        glClear (GL_COLOR_BUFFER_BIT);
slouken@272
   842
slouken@272
   843
        [ gl_context flushBuffer ];
slouken@390
   844
slouken@47
   845
        current->flags |= SDL_OPENGL;
icculus@5908
   846
    } else if (isLion) {  /* For 2D, we build a CGBitmapContext */
icculus@5569
   847
        CGColorSpaceRef cgColorspace;
icculus@5569
   848
icculus@5569
   849
        /* Only recreate the view if it doesn't already exist */
icculus@5569
   850
        if (window_view == nil) {
icculus@5569
   851
            window_view = [ [ NSView alloc ] initWithFrame:contentRect ];
icculus@5569
   852
            [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
icculus@5569
   853
            [ [ qz_window contentView ] addSubview:window_view ];
icculus@5569
   854
            [ window_view release ];
icculus@5569
   855
        }
icculus@5569
   856
icculus@5569
   857
        cgColorspace = CGColorSpaceCreateDeviceRGB();
icculus@5569
   858
        current->pitch = 4 * current->w;
icculus@5569
   859
        current->pixels = SDL_malloc (current->h * current->pitch);
icculus@5569
   860
        
icculus@5569
   861
        cg_context = CGBitmapContextCreate (current->pixels, current->w, current->h,
icculus@5569
   862
                        8, current->pitch, cgColorspace,
icculus@5569
   863
                        kCGImageAlphaNoneSkipFirst);
icculus@5569
   864
        CGColorSpaceRelease (cgColorspace);
icculus@5569
   865
        
icculus@5569
   866
        current->flags |= SDL_SWSURFACE;
icculus@5569
   867
        current->flags |= SDL_ASYNCBLIT;
icculus@5569
   868
        current->hwdata = (void *) cg_context;
icculus@5569
   869
icculus@5569
   870
        /* Force this window to draw above _everything_. */
icculus@5569
   871
        [ qz_window setLevel:CGShieldingWindowLevel() ];
icculus@5908
   872
icculus@5908
   873
        this->UpdateRects     = QZ_UpdateRects;
icculus@5908
   874
        this->LockHWSurface   = QZ_LockHWSurface;
icculus@5908
   875
        this->UnlockHWSurface = QZ_UnlockHWSurface;
icculus@5569
   876
    }
icculus@5569
   877
icculus@5908
   878
    if (isLion) {
icculus@5908
   879
        [ qz_window setHasShadow:NO];
icculus@5908
   880
        [ qz_window setOpaque:YES];
icculus@5908
   881
        [ qz_window makeKeyAndOrderFront:nil ];
icculus@5908
   882
    }
slouken@47
   883
icculus@5986
   884
    /* !!! FIXME: keep an eye on this.
icculus@5986
   885
     * This API is officially unavailable for 64-bit binaries.
icculus@5986
   886
     *  It happens to work, as of 10.7, but we're going to see if
icculus@5986
   887
     *  we can just simply do without it on newer OSes...
icculus@5986
   888
     */
icculus@5986
   889
    #if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070) && !defined(__LP64__)
icculus@5986
   890
    if ( !IS_LION_OR_LATER(this) ) {
icculus@5986
   891
        /* If we don't hide menu bar, it will get events and interrupt the program */
icculus@5986
   892
        HideMenuBar ();
icculus@5986
   893
    }
icculus@5986
   894
    #endif
slouken@272
   895
icculus@1340
   896
    /* Fade in again (asynchronously) */
icculus@1340
   897
    if ( fade_token != kCGDisplayFadeReservationInvalidToken ) {
icculus@1340
   898
        CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   899
        CGReleaseDisplayFadeReservation(fade_token);
icculus@1340
   900
    }
slouken@390
   901
slouken@435
   902
    /* 
slouken@501
   903
        There is a bug in Cocoa where NSScreen doesn't synchronize
slouken@501
   904
        with CGDirectDisplay, so the main screen's frame is wrong.
slouken@501
   905
        As a result, coordinate translation produces incorrect results.
slouken@501
   906
        We can hack around this bug by setting the screen rect
slouken@501
   907
        ourselves. This hack should be removed if/when the bug is fixed.
slouken@435
   908
    */
slouken@435
   909
    screen_rect = NSMakeRect(0,0,width,height);
icculus@4204
   910
    QZ_SetFrame([ NSScreen mainScreen ], screen_rect);
slouken@435
   911
slouken@47
   912
    /* Save the flags to ensure correct tear-down */
slouken@47
   913
    mode_flags = current->flags;
slouken@390
   914
slouken@1629
   915
    /* Set app state, hide cursor if necessary, ... */
slouken@1629
   916
    QZ_DoActivate(this);
icculus@1119
   917
slouken@47
   918
    return current;
slouken@47
   919
slouken@390
   920
    /* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
icculus@5908
   921
ERR_NO_GL:
icculus@5908
   922
ERR_DOUBLEBUF:  QZ_RestoreDisplayMode(this);
slouken@588
   923
ERR_NO_SWITCH:  CGReleaseAllDisplays ();
icculus@1340
   924
ERR_NO_CAPTURE:
icculus@1340
   925
ERR_NO_MATCH:   if ( fade_token != kCGDisplayFadeReservationInvalidToken ) {
icculus@1340
   926
                    CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   927
                    CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
   928
                }
icculus@1340
   929
                return NULL;
slouken@47
   930
}
slouken@47
   931
slouken@47
   932
static SDL_Surface* QZ_SetVideoWindowed (_THIS, SDL_Surface *current, int width,
slouken@4317
   933
                                         int height, int *bpp, Uint32 flags)
slouken@4317
   934
{
slouken@58
   935
    unsigned int style;
slouken@501
   936
    NSRect contentRect;
slouken@657
   937
    int center_window = 1;
slouken@657
   938
    int origin_x, origin_y;
icculus@1340
   939
    CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
slouken@390
   940
slouken@58
   941
    current->flags = 0;
slouken@58
   942
    current->w = width;
slouken@47
   943
    current->h = height;
slouken@501
   944
    
slouken@501
   945
    contentRect = NSMakeRect (0, 0, width, height);
icculus@4265
   946
slouken@501
   947
    /*
slouken@501
   948
        Check if we should completely destroy the previous mode 
slouken@501
   949
        - If it is fullscreen
slouken@501
   950
        - If it has different noframe or resizable attribute
slouken@501
   951
        - If it is OpenGL (since gl attributes could be different)
slouken@501
   952
        - If new mode is OpenGL, but previous mode wasn't
slouken@501
   953
    */
icculus@1340
   954
    if (video_set == SDL_TRUE) {
icculus@1340
   955
        if (mode_flags & SDL_FULLSCREEN) {
icculus@1340
   956
            /* Fade to black to hide resolution-switching flicker (and garbage
icculus@1340
   957
               that is displayed by a destroyed OpenGL context, if applicable) */
icculus@1340
   958
            if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) {
icculus@1340
   959
                CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
icculus@1340
   960
            }
icculus@1340
   961
            QZ_UnsetVideoMode (this, TRUE);
icculus@1340
   962
        }
icculus@1340
   963
        else if ( ((mode_flags ^ flags) & (SDL_NOFRAME|SDL_RESIZABLE)) ||
icculus@1340
   964
                  (mode_flags & SDL_OPENGL) || 
icculus@1340
   965
                  (flags & SDL_OPENGL) ) {
icculus@1340
   966
            QZ_UnsetVideoMode (this, TRUE);
icculus@1340
   967
        }
icculus@1340
   968
    }
slouken@683
   969
    
icculus@4204
   970
    /* Sorry, QuickDraw was ripped out. */
icculus@4204
   971
    if (getenv("SDL_NSWindowPointer") || getenv("SDL_NSQuickDrawViewPointer")) {
icculus@4204
   972
        SDL_SetError ("Embedded QuickDraw windows are no longer supported");
icculus@4204
   973
        if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@4204
   974
            CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@4204
   975
            CGReleaseDisplayFadeReservation (fade_token);
slouken@683
   976
        }
icculus@4204
   977
        return NULL;
slouken@683
   978
    }
icculus@4204
   979
slouken@501
   980
    /* Check if we should recreate the window */
slouken@501
   981
    if (qz_window == nil) {
slouken@501
   982
    
slouken@501
   983
        /* Set the window style based on input flags */
slouken@501
   984
        if ( flags & SDL_NOFRAME ) {
slouken@501
   985
            style = NSBorderlessWindowMask;
slouken@501
   986
            current->flags |= SDL_NOFRAME;
slouken@501
   987
        } else {
slouken@501
   988
            style = NSTitledWindowMask;
slouken@501
   989
            style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
slouken@501
   990
            if ( flags & SDL_RESIZABLE ) {
slouken@501
   991
                style |= NSResizableWindowMask;
slouken@501
   992
                current->flags |= SDL_RESIZABLE;
slouken@501
   993
            }
slouken@501
   994
        }
icculus@4265
   995
slouken@501
   996
        /* Manually create a window, avoids having a nib file resource */
slouken@501
   997
        qz_window = [ [ SDL_QuartzWindow alloc ] 
slouken@501
   998
            initWithContentRect:contentRect
slouken@501
   999
                styleMask:style 
slouken@501
  1000
                    backing:NSBackingStoreBuffered
slouken@501
  1001
                        defer:NO ];
slouken@501
  1002
                          
slouken@501
  1003
        if (qz_window == nil) {
slouken@501
  1004
            SDL_SetError ("Could not create the Cocoa window");
icculus@1340
  1005
            if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
  1006
                CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
  1007
                CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
  1008
            }
slouken@501
  1009
            return NULL;
slouken@501
  1010
        }
icculus@4265
  1011
slouken@4090
  1012
        /*[ qz_window setReleasedWhenClosed:YES ];*/ /* no need to set this as it's the default for NSWindows */
slouken@501
  1013
        QZ_SetCaption(this, this->wm_title, this->wm_icon);
slouken@501
  1014
        [ qz_window setAcceptsMouseMovedEvents:YES ];
slouken@501
  1015
        [ qz_window setViewsNeedDisplay:NO ];
icculus@4265
  1016
icculus@4265
  1017
        if ( QZ_WindowPosition(this, &origin_x, &origin_y) ) {
icculus@4265
  1018
            /* have to flip the Y value (NSPoint is lower left corner origin) */
icculus@4265
  1019
            [ qz_window setFrameTopLeftPoint:NSMakePoint((float) origin_x, (float) (this->info.current_h - origin_y))];
icculus@4265
  1020
            center_window = 0;
icculus@4267
  1021
        } else if ( center_window ) {
slouken@657
  1022
            [ qz_window center ];
slouken@657
  1023
        }
icculus@4265
  1024
slouken@501
  1025
        [ qz_window setDelegate:
slouken@4090
  1026
            [ [ SDL_QuartzWindowDelegate alloc ] init ] ];
slouken@4070
  1027
        [ qz_window setContentView: [ [ [ SDL_QuartzView alloc ] init ] autorelease ] ];
slouken@501
  1028
    }
slouken@501
  1029
    /* We already have a window, just change its size */
slouken@501
  1030
    else {
icculus@4267
  1031
        [ qz_window setContentSize:contentRect.size ];
icculus@4267
  1032
        current->flags |= (SDL_NOFRAME|SDL_RESIZABLE) & mode_flags;
icculus@4267
  1033
        [ window_view setFrameSize:contentRect.size ];
slouken@501
  1034
    }
slouken@390
  1035
slouken@501
  1036
    /* For OpenGL, we bind the context to a subview */
slouken@47
  1037
    if ( flags & SDL_OPENGL ) {
slouken@390
  1038
icculus@1183
  1039
        if ( ! QZ_SetupOpenGL (this, *bpp, flags) ) {
icculus@1340
  1040
            if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
  1041
                CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
  1042
                CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
  1043
            }
slouken@47
  1044
            return NULL;
slouken@47
  1045
        }
slouken@390
  1046
slouken@501
  1047
        window_view = [ [ NSView alloc ] initWithFrame:contentRect ];
slouken@832
  1048
        [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
slouken@501
  1049
        [ [ qz_window contentView ] addSubview:window_view ];
slouken@501
  1050
        [ gl_context setView: window_view ];
slouken@501
  1051
        [ window_view release ];
slouken@47
  1052
        [ gl_context makeCurrentContext];
slouken@158
  1053
        [ qz_window makeKeyAndOrderFront:nil ];
slouken@47
  1054
        current->flags |= SDL_OPENGL;
slouken@47
  1055
    }
icculus@4204
  1056
    /* For 2D, we build a CGBitmapContext */
slouken@47
  1057
    else {
icculus@4204
  1058
        CGColorSpaceRef cgColorspace;
slouken@390
  1059
slouken@501
  1060
        /* Only recreate the view if it doesn't already exist */
slouken@501
  1061
        if (window_view == nil) {
slouken@501
  1062
        
icculus@4204
  1063
            window_view = [ [ NSView alloc ] initWithFrame:contentRect ];
slouken@832
  1064
            [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
slouken@501
  1065
            [ [ qz_window contentView ] addSubview:window_view ];
slouken@501
  1066
            [ window_view release ];
slouken@501
  1067
            [ qz_window makeKeyAndOrderFront:nil ];
slouken@501
  1068
        }
slouken@501
  1069
        
icculus@4204
  1070
        cgColorspace = CGColorSpaceCreateDeviceRGB();
icculus@4204
  1071
        current->pitch = 4 * current->w;
icculus@4204
  1072
        current->pixels = SDL_malloc (current->h * current->pitch);
slouken@498
  1073
        
icculus@4204
  1074
        cg_context = CGBitmapContextCreate (current->pixels, current->w, current->h,
icculus@4204
  1075
                        8, current->pitch, cgColorspace,
icculus@4204
  1076
                        kCGImageAlphaNoneSkipFirst);
icculus@4204
  1077
        CGColorSpaceRelease (cgColorspace);
icculus@4204
  1078
        
icculus@4204
  1079
        current->flags |= SDL_SWSURFACE;
icculus@4204
  1080
        current->flags |= SDL_ASYNCBLIT;
icculus@4204
  1081
        current->hwdata = (void *) cg_context;
icculus@5908
  1082
icculus@5908
  1083
        this->UpdateRects     = QZ_UpdateRects;
icculus@5908
  1084
        this->LockHWSurface   = QZ_LockHWSurface;
icculus@5908
  1085
        this->UnlockHWSurface = QZ_UnlockHWSurface;
slouken@47
  1086
    }
slouken@390
  1087
slouken@272
  1088
    /* Save flags to ensure correct teardown */
slouken@272
  1089
    mode_flags = current->flags;
slouken@390
  1090
icculus@1340
  1091
    /* Fade in again (asynchronously) if we came from a fullscreen mode and faded to black */
icculus@1340
  1092
    if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
  1093
        CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
  1094
        CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
  1095
    }
icculus@1340
  1096
slouken@47
  1097
    return current;
slouken@47
  1098
}
slouken@47
  1099
slouken@390
  1100
static SDL_Surface* QZ_SetVideoMode (_THIS, SDL_Surface *current, int width,
slouken@4317
  1101
                                     int height, int bpp, Uint32 flags)
slouken@4317
  1102
{
icculus@5908
  1103
    const BOOL isLion = IS_LION_OR_LATER(this);
slouken@47
  1104
    current->flags = 0;
icculus@852
  1105
    current->pixels = NULL;
slouken@390
  1106
slouken@47
  1107
    /* Setup full screen video */
slouken@47
  1108
    if ( flags & SDL_FULLSCREEN ) {
icculus@5908
  1109
        if ( isLion ) {
icculus@5908
  1110
            bpp = 32;
icculus@5908
  1111
        }
slouken@47
  1112
        current = QZ_SetVideoFullScreen (this, current, width, height, bpp, flags );
slouken@47
  1113
        if (current == NULL)
slouken@47
  1114
            return NULL;
slouken@47
  1115
    }
slouken@47
  1116
    /* Setup windowed video */
slouken@47
  1117
    else {
icculus@5908
  1118
        /* Force bpp to 32 */
icculus@5908
  1119
        bpp = 32;
icculus@1183
  1120
        current = QZ_SetVideoWindowed (this, current, width, height, &bpp, flags);
slouken@47
  1121
        if (current == NULL)
slouken@47
  1122
            return NULL;
slouken@47
  1123
    }
slouken@390
  1124
icculus@5913
  1125
    if (qz_window != nil) {
icculus@5913
  1126
        NSGraphicsContext *ctx;
icculus@5913
  1127
        ctx = [NSGraphicsContext graphicsContextWithWindow:qz_window];
icculus@5913
  1128
        [NSGraphicsContext setCurrentContext:ctx];
icculus@5913
  1129
    }
icculus@5913
  1130
slouken@47
  1131
    /* Setup the new pixel format */
slouken@47
  1132
    {
slouken@390
  1133
        int amask = 0,
slouken@390
  1134
        rmask = 0,
slouken@390
  1135
        gmask = 0,
slouken@390
  1136
        bmask = 0;
slouken@390
  1137
slouken@47
  1138
        switch (bpp) {
slouken@47
  1139
            case 16:   /* (1)-5-5-5 RGB */
slouken@390
  1140
                amask = 0;
slouken@58
  1141
                rmask = 0x7C00;
slouken@58
  1142
                gmask = 0x03E0;
slouken@58
  1143
                bmask = 0x001F;
slouken@47
  1144
                break;
slouken@47
  1145
            case 24:
slouken@47
  1146
                SDL_SetError ("24bpp is not available");
slouken@47
  1147
                return NULL;
slouken@47
  1148
            case 32:   /* (8)-8-8-8 ARGB */
slouken@155
  1149
                amask = 0x00000000;
icculus@5908
  1150
                if ( (!isLion) && (flags & SDL_FULLSCREEN) ) {
icculus@5908
  1151
                    rmask = 0x00FF0000;
icculus@5908
  1152
                    gmask = 0x0000FF00;
icculus@5908
  1153
                    bmask = 0x000000FF;
icculus@5908
  1154
                } else {
slouken@4341
  1155
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
icculus@5908
  1156
                    rmask = 0x0000FF00;
icculus@5908
  1157
                    gmask = 0x00FF0000;
icculus@5908
  1158
                    bmask = 0xFF000000;
icculus@4204
  1159
#else
icculus@5908
  1160
                    rmask = 0x00FF0000;
icculus@5908
  1161
                    gmask = 0x0000FF00;
icculus@5908
  1162
                    bmask = 0x000000FF;
icculus@4204
  1163
#endif
icculus@5908
  1164
                    break;
icculus@5908
  1165
                }
slouken@47
  1166
        }
slouken@390
  1167
slouken@47
  1168
        if ( ! SDL_ReallocFormat (current, bpp,
slouken@47
  1169
                                  rmask, gmask, bmask, amask ) ) {
slouken@390
  1170
            SDL_SetError ("Couldn't reallocate pixel format");
slouken@390
  1171
            return NULL;
slouken@4236
  1172
        }
slouken@47
  1173
    }
slouken@390
  1174
slouken@272
  1175
    /* Signal successful completion (used internally) */
slouken@155
  1176
    video_set = SDL_TRUE;
slouken@390
  1177
slouken@47
  1178
    return current;
slouken@47
  1179
}
slouken@47
  1180
slouken@4317
  1181
static int QZ_ToggleFullScreen (_THIS, int on)
slouken@4317
  1182
{
slouken@576
  1183
    return 0;
slouken@47
  1184
}
slouken@47
  1185
slouken@390
  1186
static int QZ_SetColors (_THIS, int first_color, int num_colors,
slouken@4317
  1187
                         SDL_Color *colors)
slouken@4317
  1188
{
icculus@5908
  1189
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
  1190
    /* we shouldn't have an 8-bit mode on Lion! */
icculus@5908
  1191
    if (!IS_LION_OR_LATER(this)) {
icculus@5908
  1192
        CGTableCount  index;
icculus@5908
  1193
        CGDeviceColor color;
icculus@5908
  1194
icculus@5908
  1195
        for (index = first_color; index < first_color+num_colors; index++) {
icculus@5908
  1196
icculus@5908
  1197
            /* Clamp colors between 0.0 and 1.0 */
icculus@5908
  1198
            color.red   = colors->r / 255.0;
icculus@5908
  1199
            color.blue  = colors->b / 255.0;
icculus@5908
  1200
            color.green = colors->g / 255.0;
icculus@5908
  1201
icculus@5908
  1202
            colors++;
icculus@5908
  1203
icculus@5908
  1204
            CGPaletteSetColorAtIndex (palette, color, index);
icculus@5908
  1205
        }
icculus@5908
  1206
icculus@5908
  1207
        return ( CGDisplayNoErr == CGDisplaySetPalette (display_id, palette) );
icculus@5908
  1208
    }
icculus@5908
  1209
#endif
icculus@5908
  1210
icculus@5908
  1211
    return 0;
icculus@5908
  1212
}
icculus@5908
  1213
icculus@5908
  1214
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
  1215
static int QZ_LockDoubleBuffer (_THIS, SDL_Surface *surface)
icculus@5908
  1216
{
icculus@5908
  1217
    return 1;
icculus@5908
  1218
}
icculus@5908
  1219
icculus@5908
  1220
static void QZ_UnlockDoubleBuffer (_THIS, SDL_Surface *surface)
icculus@5908
  1221
{
icculus@5908
  1222
}
icculus@5908
  1223
icculus@5908
  1224
/* The VBL delay is based on code by Ian R Ollmann's RezLib <iano@cco.caltech.edu> */
icculus@5908
  1225
static AbsoluteTime QZ_SecondsToAbsolute ( double seconds )
icculus@5908
  1226
{
icculus@5908
  1227
    union
icculus@5908
  1228
    {
icculus@5908
  1229
        UInt64 i;
icculus@5908
  1230
        Nanoseconds ns;
icculus@5908
  1231
    } temp;
icculus@5908
  1232
        
icculus@5908
  1233
    temp.i = seconds * 1000000000.0;
icculus@5908
  1234
    
icculus@5908
  1235
    return NanosecondsToAbsolute ( temp.ns );
slouken@47
  1236
}
slouken@47
  1237
icculus@5908
  1238
static int QZ_ThreadFlip (_THIS)
icculus@5908
  1239
{
icculus@5908
  1240
    Uint8 *src, *dst;
icculus@5908
  1241
    int skip, len, h;
icculus@5908
  1242
    
icculus@5908
  1243
    /*
icculus@5908
  1244
        Give this thread the highest scheduling priority possible,
icculus@5908
  1245
        in the hopes that it will immediately run after the VBL delay
icculus@5908
  1246
    */
icculus@5908
  1247
    {
icculus@5908
  1248
        pthread_t current_thread;
icculus@5908
  1249
        int policy;
icculus@5908
  1250
        struct sched_param param;
icculus@5908
  1251
        
icculus@5908
  1252
        current_thread = pthread_self ();
icculus@5908
  1253
        pthread_getschedparam (current_thread, &policy, &param);
icculus@5908
  1254
        policy = SCHED_RR;
icculus@5908
  1255
        param.sched_priority = sched_get_priority_max (policy);
icculus@5908
  1256
        pthread_setschedparam (current_thread, policy, &param);
icculus@5908
  1257
    }
icculus@5908
  1258
    
icculus@5908
  1259
    while (1) {
icculus@5908
  1260
    
icculus@5908
  1261
        SDL_SemWait (sem1);
icculus@5908
  1262
        if (quit_thread)
icculus@5908
  1263
            return 0;
icculus@5908
  1264
                
icculus@5908
  1265
        /*
icculus@5908
  1266
         * We have to add SDL_VideoSurface->offset here, since we might be a
icculus@5908
  1267
         *  smaller surface in the center of the framebuffer (you asked for
icculus@5908
  1268
         *  a fullscreen resolution smaller than the hardware could supply
icculus@5908
  1269
         *  so SDL is centering it in a bigger resolution)...
icculus@5908
  1270
         */
icculus@5908
  1271
        dst = ((Uint8 *)((size_t)CGDisplayBaseAddress (display_id))) + SDL_VideoSurface->offset;
icculus@5908
  1272
        src = current_buffer + SDL_VideoSurface->offset;
icculus@5908
  1273
        len = SDL_VideoSurface->w * SDL_VideoSurface->format->BytesPerPixel;
icculus@5908
  1274
        h = SDL_VideoSurface->h;
icculus@5908
  1275
        skip = SDL_VideoSurface->pitch;
icculus@5908
  1276
    
icculus@5908
  1277
        /* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
icculus@5908
  1278
        {
icculus@5908
  1279
            
icculus@5908
  1280
            /* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
icculus@5908
  1281
            double refreshRate;
icculus@5908
  1282
            double linesPerSecond;
icculus@5908
  1283
            double target;
icculus@5908
  1284
            double position;
icculus@5908
  1285
            double adjustment;
icculus@5908
  1286
            AbsoluteTime nextTime;        
icculus@5908
  1287
            CFNumberRef refreshRateCFNumber;
icculus@5908
  1288
            
icculus@5908
  1289
            refreshRateCFNumber = CFDictionaryGetValue (mode, kCGDisplayRefreshRate);
icculus@5908
  1290
            if ( NULL == refreshRateCFNumber ) {
icculus@5908
  1291
                SDL_SetError ("Mode has no refresh rate");
icculus@5908
  1292
                goto ERROR;
icculus@5908
  1293
            }
icculus@5908
  1294
            
icculus@5908
  1295
            if ( 0 == CFNumberGetValue (refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) ) {
icculus@5908
  1296
                SDL_SetError ("Error getting refresh rate");
icculus@5908
  1297
                goto ERROR;
icculus@5908
  1298
            }
icculus@5908
  1299
            
icculus@5908
  1300
            if ( 0 == refreshRate ) {
icculus@5908
  1301
               
icculus@5908
  1302
               SDL_SetError ("Display has no refresh rate, using 60hz");
icculus@5908
  1303
                
icculus@5908
  1304
                /* ok, for LCD's we'll emulate a 60hz refresh, which may or may not look right */
icculus@5908
  1305
                refreshRate = 60.0;
icculus@5908
  1306
            }
icculus@5908
  1307
            
icculus@5908
  1308
            linesPerSecond = refreshRate * h;
icculus@5908
  1309
            target = h;
icculus@5908
  1310
        
icculus@5908
  1311
            /* Figure out the first delay so we start off about right */
icculus@5908
  1312
            position = CGDisplayBeamPosition (display_id);
icculus@5908
  1313
            if (position > target)
icculus@5908
  1314
                position = 0;
icculus@5908
  1315
            
icculus@5908
  1316
            adjustment = (target - position) / linesPerSecond; 
icculus@5908
  1317
            
icculus@5908
  1318
            nextTime = AddAbsoluteToAbsolute (UpTime (), QZ_SecondsToAbsolute (adjustment));
icculus@5908
  1319
        
icculus@5908
  1320
            MPDelayUntil (&nextTime);
icculus@5908
  1321
        }
icculus@5908
  1322
        
icculus@5908
  1323
        
icculus@5908
  1324
        /* On error, skip VBL delay */
icculus@5908
  1325
        ERROR:
icculus@5908
  1326
        
icculus@5908
  1327
        /* TODO: use CGContextDrawImage here too!  Create two CGContextRefs the same way we
icculus@5908
  1328
           create two buffers, replace current_buffer with current_context and set it
icculus@5908
  1329
           appropriately in QZ_FlipDoubleBuffer.  */
icculus@5908
  1330
        while ( h-- ) {
icculus@5908
  1331
        
icculus@5908
  1332
            SDL_memcpy (dst, src, len);
icculus@5908
  1333
            src += skip;
icculus@5908
  1334
            dst += skip;
icculus@5908
  1335
        }
icculus@5908
  1336
        
icculus@5908
  1337
        /* signal flip completion */
icculus@5908
  1338
        SDL_SemPost (sem2);
icculus@5908
  1339
    }
icculus@5908
  1340
    
icculus@5908
  1341
    return 0;
icculus@5908
  1342
}
icculus@5908
  1343
        
icculus@5908
  1344
static int QZ_FlipDoubleBuffer (_THIS, SDL_Surface *surface)
icculus@5908
  1345
{
icculus@5908
  1346
    /* wait for previous flip to complete */
icculus@5908
  1347
    SDL_SemWait (sem2);
icculus@5908
  1348
    
icculus@5908
  1349
    current_buffer = surface->pixels;
icculus@5908
  1350
        
icculus@5908
  1351
    if (surface->pixels == sw_buffers[0])
icculus@5908
  1352
        surface->pixels = sw_buffers[1];
icculus@5908
  1353
    else
icculus@5908
  1354
        surface->pixels = sw_buffers[0];
icculus@5908
  1355
    
icculus@5908
  1356
    /* signal worker thread to do the flip */
icculus@5908
  1357
    SDL_SemPost (sem1);
icculus@5908
  1358
    
icculus@5908
  1359
    return 0;
icculus@5908
  1360
}
icculus@5908
  1361
icculus@5908
  1362
static void QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect *rects)
icculus@5908
  1363
{
icculus@5908
  1364
    /* perform a flip if someone calls updaterects on a doublebuferred surface */
icculus@5908
  1365
    this->FlipHWSurface (this, SDL_VideoSurface);
icculus@5908
  1366
}
icculus@5908
  1367
icculus@5908
  1368
static void QZ_DirectUpdate (_THIS, int num_rects, SDL_Rect *rects)
icculus@5908
  1369
{
icculus@5908
  1370
#pragma unused(this,num_rects,rects)
icculus@5908
  1371
}
icculus@5908
  1372
#endif
slouken@498
  1373
slouken@761
  1374
/* Resize icon, BMP format */
slouken@761
  1375
static const unsigned char QZ_ResizeIcon[] = {
slouken@761
  1376
    0x42,0x4d,0x31,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
slouken@761
  1377
    0x00,0x00,0x0d,0x00,0x00,0x00,0x0d,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
slouken@761
  1378
    0x00,0x00,0xfb,0x01,0x00,0x00,0x13,0x0b,0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
slouken@761
  1379
    0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1380
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1381
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1382
    0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,
slouken@761
  1383
    0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,
slouken@761
  1384
    0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xda,0xda,0xda,0x87,
slouken@761
  1385
    0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,
slouken@761
  1386
    0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1387
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd5,0xd5,0xd5,0x87,0x87,0x87,0xe8,0xe8,0xe8,
slouken@761
  1388
    0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,
slouken@761
  1389
    0xda,0xda,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1390
    0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,
slouken@761
  1391
    0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1392
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,
slouken@761
  1393
    0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,
slouken@761
  1394
    0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1395
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,
slouken@761
  1396
    0xe8,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1397
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1398
    0xff,0xff,0xff,0xd9,0xd9,0xd9,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xdc,
slouken@761
  1399
    0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1400
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,
slouken@761
  1401
    0xdb,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1402
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1403
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,0xdb,0x87,0x87,0x87,0xe8,
slouken@761
  1404
    0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1405
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1406
    0xff,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1407
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1408
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdc,
slouken@761
  1409
    0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1410
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1411
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b
slouken@761
  1412
};
slouken@761
  1413
slouken@4317
  1414
static void QZ_DrawResizeIcon (_THIS)
slouken@4317
  1415
{
slouken@761
  1416
    /* Check if we should draw the resize icon */
slouken@761
  1417
    if (SDL_VideoSurface->flags & SDL_RESIZABLE) {
slouken@761
  1418
    
icculus@4204
  1419
        SDL_Rect icon_rect;
slouken@761
  1420
        
icculus@4204
  1421
        /* Create the icon image */
icculus@4204
  1422
        if (resize_icon == NULL) {
icculus@4204
  1423
        
icculus@4204
  1424
            SDL_RWops *rw;
icculus@4204
  1425
            SDL_Surface *tmp;
slouken@761
  1426
            
icculus@4204
  1427
            rw = SDL_RWFromConstMem (QZ_ResizeIcon, sizeof(QZ_ResizeIcon));
icculus@4204
  1428
            tmp = SDL_LoadBMP_RW (rw, SDL_TRUE);
icculus@4204
  1429
                                                            
icculus@4204
  1430
            resize_icon = SDL_ConvertSurface (tmp, SDL_VideoSurface->format, SDL_SRCCOLORKEY);
icculus@4204
  1431
            SDL_SetColorKey (resize_icon, SDL_SRCCOLORKEY, 0xFFFFFF);
slouken@761
  1432
            
icculus@4204
  1433
            SDL_FreeSurface (tmp);
icculus@4204
  1434
        }
slouken@761
  1435
            
icculus@4204
  1436
        icon_rect.x = SDL_VideoSurface->w - 13;
icculus@4204
  1437
        icon_rect.y = SDL_VideoSurface->h - 13;
icculus@4204
  1438
        icon_rect.w = 13;
icculus@4204
  1439
        icon_rect.h = 13;
icculus@4204
  1440
            
icculus@4204
  1441
        SDL_BlitSurface (resize_icon, NULL, SDL_VideoSurface, &icon_rect);
slouken@761
  1442
    }
slouken@761
  1443
}
slouken@761
  1444
slouken@4317
  1445
static void QZ_UpdateRects (_THIS, int numRects, SDL_Rect *rects)
slouken@4317
  1446
{
slouken@47
  1447
    if (SDL_VideoSurface->flags & SDL_OPENGLBLIT) {
slouken@47
  1448
        QZ_GL_SwapBuffers (this);
slouken@47
  1449
    }
slouken@501
  1450
    else if ( [ qz_window isMiniaturized ] ) {
slouken@501
  1451
    
slouken@501
  1452
        /* Do nothing if miniaturized */
slouken@272
  1453
    }
slouken@501
  1454
    
slouken@47
  1455
    else {
icculus@4204
  1456
        CGContextRef cgc = (CGContextRef)
icculus@5619
  1457
            [[NSGraphicsContext currentContext] graphicsPort];
icculus@4204
  1458
        QZ_DrawResizeIcon (this);
icculus@4204
  1459
        CGContextFlush (cg_context);
icculus@4204
  1460
        CGImageRef image = CGBitmapContextCreateImage (cg_context);
icculus@4204
  1461
        CGRect rectangle = CGRectMake (0,0,[window_view frame].size.width,[window_view frame].size.height);
slouken@501
  1462
        
icculus@4204
  1463
        CGContextDrawImage (cgc, rectangle, image);
icculus@4204
  1464
        CGImageRelease(image);
icculus@4204
  1465
        CGContextFlush (cgc);
slouken@47
  1466
    }
slouken@47
  1467
}
slouken@47
  1468
slouken@4317
  1469
static void QZ_VideoQuit (_THIS)
slouken@4317
  1470
{
icculus@1340
  1471
    CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
icculus@1340
  1472
icculus@560
  1473
    /* Restore gamma settings */
icculus@560
  1474
    CGDisplayRestoreColorSyncSettings ();
icculus@560
  1475
icculus@560
  1476
    /* Ensure the cursor will be visible and working when we quit */
icculus@560
  1477
    CGDisplayShowCursor (display_id);
icculus@560
  1478
    CGAssociateMouseAndMouseCursorPosition (1);
icculus@560
  1479
    
icculus@1340
  1480
    if (mode_flags & SDL_FULLSCREEN) {
icculus@1340
  1481
        /* Fade to black to hide resolution-switching flicker (and garbage
icculus@1340
  1482
           that is displayed by a destroyed OpenGL context, if applicable) */
icculus@1340
  1483
        if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) {
icculus@1340
  1484
            CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
icculus@1340
  1485
        }
icculus@1340
  1486
        QZ_UnsetVideoMode (this, TRUE);
icculus@1340
  1487
        if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
  1488
            CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
  1489
            CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
  1490
        }
icculus@1340
  1491
    }
icculus@1340
  1492
    else
icculus@1340
  1493
        QZ_UnsetVideoMode (this, TRUE);
icculus@1181
  1494
icculus@5908
  1495
#if (MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
icculus@5908
  1496
    if (!IS_LION_OR_LATER(this)) {
icculus@5908
  1497
        CGPaletteRelease(palette);
icculus@5908
  1498
    }
icculus@5908
  1499
#endif
icculus@5908
  1500
icculus@1189
  1501
    if (opengl_library) {
icculus@1189
  1502
        SDL_UnloadObject(opengl_library);
icculus@1189
  1503
        opengl_library = NULL;
icculus@1181
  1504
    }
icculus@1181
  1505
    this->gl_config.driver_loaded = 0;
slouken@4049
  1506
slouken@4049
  1507
    if (field_edit) {
slouken@4049
  1508
        [field_edit release];
slouken@4049
  1509
        field_edit = NULL;
slouken@4049
  1510
    }
slouken@47
  1511
}
slouken@47
  1512
slouken@4317
  1513
static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@4317
  1514
{
slouken@47
  1515
    return 1;
slouken@47
  1516
}
slouken@47
  1517
slouken@4317
  1518
static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@4317
  1519
{
slouken@47
  1520
}
slouken@47
  1521
slouken@4317
  1522
static int QZ_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@4317
  1523
{
icculus@1120
  1524
    return(-1); /* unallowed (no HWSURFACE support here). */
icculus@1120
  1525
}
icculus@1120
  1526
slouken@4317
  1527
static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface)
slouken@4317
  1528
{
slouken@47
  1529
}
slouken@47
  1530
slouken@47
  1531
/* Gamma functions */
slouken@4317
  1532
int QZ_SetGamma (_THIS, float red, float green, float blue)
slouken@4317
  1533
{
slouken@47
  1534
    const CGGammaValue min = 0.0, max = 1.0;
slouken@272
  1535
slouken@272
  1536
    if (red == 0.0)
slouken@272
  1537
        red = FLT_MAX;
slouken@272
  1538
    else
slouken@272
  1539
        red = 1.0 / red;
slouken@272
  1540
slouken@272
  1541
    if (green == 0.0)
slouken@272
  1542
        green = FLT_MAX;
slouken@272
  1543
    else
slouken@272
  1544
        green = 1.0 / green;
slouken@272
  1545
slouken@272
  1546
    if (blue == 0.0)
slouken@272
  1547
        blue = FLT_MAX;
slouken@272
  1548
    else
slouken@272
  1549
        blue  = 1.0 / blue;
slouken@390
  1550
slouken@390
  1551
    if ( CGDisplayNoErr == CGSetDisplayTransferByFormula
slouken@390
  1552
         (display_id, min, max, red, min, max, green, min, max, blue) ) {
slouken@390
  1553
slouken@272
  1554
        return 0;
slouken@272
  1555
    }
slouken@272
  1556
    else {
slouken@390
  1557
slouken@47
  1558
        return -1;
slouken@272
  1559
    }
slouken@47
  1560
}
slouken@47
  1561
slouken@4317
  1562
int QZ_GetGamma (_THIS, float *red, float *green, float *blue)
slouken@4317
  1563
{
slouken@47
  1564
    CGGammaValue dummy;
slouken@272
  1565
    if ( CGDisplayNoErr == CGGetDisplayTransferByFormula
slouken@390
  1566
         (display_id, &dummy, &dummy, red,
slouken@390
  1567
          &dummy, &dummy, green, &dummy, &dummy, blue) )
slouken@390
  1568
slouken@272
  1569
        return 0;
slouken@272
  1570
    else
slouken@47
  1571
        return -1;
slouken@47
  1572
}
slouken@47
  1573
slouken@4317
  1574
int QZ_SetGammaRamp (_THIS, Uint16 *ramp)
slouken@4317
  1575
{
icculus@5628
  1576
    const uint32_t tableSize = 255;
slouken@390
  1577
    CGGammaValue redTable[tableSize];
slouken@390
  1578
    CGGammaValue greenTable[tableSize];
slouken@390
  1579
    CGGammaValue blueTable[tableSize];
slouken@390
  1580
slouken@390
  1581
    int i;
slouken@390
  1582
slouken@390
  1583
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@390
  1584
    for (i = 0; i < 256; i++)
slouken@390
  1585
        redTable[i % 256] = ramp[i] / 65535.0;
slouken@390
  1586
slouken@390
  1587
    for (i=256; i < 512; i++)
slouken@390
  1588
        greenTable[i % 256] = ramp[i] / 65535.0;
slouken@390
  1589
slouken@390
  1590
    for (i=512; i < 768; i++)
slouken@390
  1591
        blueTable[i % 256] = ramp[i] / 65535.0;
slouken@390
  1592
slouken@390
  1593
    if ( CGDisplayNoErr == CGSetDisplayTransferByTable
slouken@390
  1594
         (display_id, tableSize, redTable, greenTable, blueTable) )
slouken@272
  1595
        return 0;
slouken@272
  1596
    else
slouken@47
  1597
        return -1;
slouken@47
  1598
}
slouken@47
  1599
slouken@4317
  1600
int QZ_GetGammaRamp (_THIS, Uint16 *ramp)
slouken@4317
  1601
{
icculus@5628
  1602
    const uint32_t tableSize = 255;
slouken@47
  1603
    CGGammaValue redTable[tableSize];
slouken@47
  1604
    CGGammaValue greenTable[tableSize];
slouken@47
  1605
    CGGammaValue blueTable[tableSize];
icculus@5628
  1606
    uint32_t actual;
slouken@47
  1607
    int i;
slouken@390
  1608
slouken@390
  1609
    if ( CGDisplayNoErr != CGGetDisplayTransferByTable
slouken@390
  1610
         (display_id, tableSize, redTable, greenTable, blueTable, &actual) ||
slouken@390
  1611
         actual != tableSize)
slouken@390
  1612
slouken@47
  1613
        return -1;
slouken@390
  1614
slouken@47
  1615
    /* Pack tables into one array, with values from 0 to 65535 */
slouken@47
  1616
    for (i = 0; i < 256; i++)
slouken@47
  1617
        ramp[i] = redTable[i % 256] * 65535.0;
slouken@390
  1618
slouken@47
  1619
    for (i=256; i < 512; i++)
slouken@47
  1620
        ramp[i] = greenTable[i % 256] * 65535.0;
slouken@390
  1621
slouken@47
  1622
    for (i=512; i < 768; i++)
slouken@47
  1623
        ramp[i] = blueTable[i % 256] * 65535.0;
slouken@390
  1624
slouken@390
  1625
    return 0;
slouken@47
  1626
}
slouken@47
  1627