src/video/quartz/SDL_QuartzVideo.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2006 06:33:21 +0000
changeset 1487 dc6b59e925a2
parent 1403 376665398b25
child 1545 8d9bb0cf2c2a
permissions -rw-r--r--
Cleaning up warnings on MacOS X
slouken@47
     1
/*
slouken@47
     2
    SDL - Simple DirectMedia Layer
slouken@761
     3
    Copyright (C) 1997-2003  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
slouken@761
    27
/* 
slouken@761
    28
    Add methods to get at private members of NSScreen. 
slouken@761
    29
    Since there is a bug in Apple's screen switching code
slouken@761
    30
    that does not update this variable when switching
slouken@761
    31
    to fullscreen, we'll set it manually (but only for the
slouken@761
    32
    main screen).
slouken@761
    33
*/
slouken@761
    34
@interface NSScreen (NSScreenAccess)
slouken@761
    35
- (void) setFrame:(NSRect)frame;
slouken@761
    36
@end
slouken@761
    37
slouken@761
    38
@implementation NSScreen (NSScreenAccess)
slouken@761
    39
- (void) setFrame:(NSRect)frame;
slouken@761
    40
{
slouken@761
    41
    _frame = frame;
slouken@761
    42
}
slouken@761
    43
@end
slouken@761
    44
slouken@761
    45
slouken@761
    46
/* 
slouken@761
    47
    Structure for rez switch gamma fades
slouken@761
    48
    We can hide the monitor flicker by setting the gamma tables to 0
slouken@761
    49
*/
slouken@761
    50
#define QZ_GAMMA_TABLE_SIZE 256
slouken@761
    51
slouken@761
    52
typedef struct {
slouken@761
    53
slouken@761
    54
    CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
slouken@761
    55
    CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
slouken@761
    56
    CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
slouken@761
    57
slouken@761
    58
} SDL_QuartzGammaTable;
slouken@761
    59
slouken@761
    60
slouken@761
    61
/* Bootstrap functions */
slouken@761
    62
static int              QZ_Available ();
slouken@761
    63
static SDL_VideoDevice* QZ_CreateDevice (int device_index);
slouken@761
    64
static void             QZ_DeleteDevice (SDL_VideoDevice *device);
slouken@761
    65
slouken@761
    66
/* Initialization, Query, Setup, and Redrawing functions */
slouken@761
    67
static int          QZ_VideoInit        (_THIS, SDL_PixelFormat *video_format);
slouken@761
    68
slouken@761
    69
static SDL_Rect**   QZ_ListModes        (_THIS, SDL_PixelFormat *format,
slouken@761
    70
                                         Uint32 flags);
icculus@1340
    71
static void         QZ_UnsetVideoMode   (_THIS, BOOL to_desktop);
slouken@761
    72
slouken@761
    73
static SDL_Surface* QZ_SetVideoMode     (_THIS, SDL_Surface *current,
slouken@761
    74
                                         int width, int height, int bpp,
slouken@761
    75
                                         Uint32 flags);
slouken@761
    76
static int          QZ_ToggleFullScreen (_THIS, int on);
slouken@761
    77
static int          QZ_SetColors        (_THIS, int first_color,
slouken@761
    78
                                         int num_colors, SDL_Color *colors);
slouken@761
    79
slouken@761
    80
static int          QZ_LockDoubleBuffer   (_THIS, SDL_Surface *surface);
slouken@761
    81
static void         QZ_UnlockDoubleBuffer (_THIS, SDL_Surface *surface);
slouken@761
    82
static int          QZ_ThreadFlip         (_THIS);
slouken@761
    83
static int          QZ_FlipDoubleBuffer   (_THIS, SDL_Surface *surface);
slouken@761
    84
static void         QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect *rects);
slouken@761
    85
slouken@761
    86
static void         QZ_DirectUpdate     (_THIS, int num_rects, SDL_Rect *rects);
slouken@761
    87
static int          QZ_LockWindow       (_THIS, SDL_Surface *surface);
slouken@761
    88
static void         QZ_UnlockWindow     (_THIS, SDL_Surface *surface);
slouken@761
    89
static void         QZ_UpdateRects      (_THIS, int num_rects, SDL_Rect *rects);
slouken@761
    90
static void         QZ_VideoQuit        (_THIS);
slouken@761
    91
slouken@761
    92
/* Hardware surface functions (for fullscreen mode only) */
slouken@761
    93
#if 0 /* Not used (apparently, it's really slow) */
slouken@761
    94
static int  QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
slouken@761
    95
#endif
slouken@761
    96
static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@761
    97
static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface);
icculus@1120
    98
static int QZ_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@761
    99
static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface);
slouken@761
   100
/* static int  QZ_FlipHWSurface (_THIS, SDL_Surface *surface); */
slouken@47
   101
slouken@47
   102
/* Bootstrap binding, enables entry point into the driver */
slouken@47
   103
VideoBootStrap QZ_bootstrap = {
slouken@272
   104
    "Quartz", "Mac OS X CoreGraphics", QZ_Available, QZ_CreateDevice
slouken@47
   105
};
slouken@47
   106
slouken@588
   107
slouken@47
   108
/* Bootstrap functions */
slouken@47
   109
static int QZ_Available () {
slouken@47
   110
    return 1;
slouken@47
   111
}
slouken@47
   112
slouken@47
   113
static SDL_VideoDevice* QZ_CreateDevice (int device_index) {
slouken@47
   114
slouken@272
   115
#pragma unused (device_index)
slouken@47
   116
slouken@47
   117
    SDL_VideoDevice *device;
slouken@47
   118
    SDL_PrivateVideoData *hidden;
slouken@47
   119
slouken@47
   120
    device = (SDL_VideoDevice*) malloc (sizeof (*device) );
slouken@47
   121
    hidden = (SDL_PrivateVideoData*) malloc (sizeof (*hidden) );
slouken@47
   122
slouken@47
   123
    if (device == NULL || hidden == NULL)
slouken@47
   124
        SDL_OutOfMemory ();
slouken@47
   125
slouken@47
   126
    memset (device, 0, sizeof (*device) );
slouken@47
   127
    memset (hidden, 0, sizeof (*hidden) );
slouken@272
   128
slouken@47
   129
    device->hidden = hidden;
slouken@47
   130
slouken@47
   131
    device->VideoInit        = QZ_VideoInit;
slouken@47
   132
    device->ListModes        = QZ_ListModes;
slouken@47
   133
    device->SetVideoMode     = QZ_SetVideoMode;
slouken@47
   134
    device->ToggleFullScreen = QZ_ToggleFullScreen;
icculus@1212
   135
    device->UpdateMouse      = QZ_UpdateMouse;
slouken@47
   136
    device->SetColors        = QZ_SetColors;
slouken@47
   137
    /* device->UpdateRects      = QZ_UpdateRects; this is determined by SetVideoMode() */
slouken@47
   138
    device->VideoQuit        = QZ_VideoQuit;
slouken@272
   139
slouken@47
   140
    device->LockHWSurface   = QZ_LockHWSurface;
slouken@47
   141
    device->UnlockHWSurface = QZ_UnlockHWSurface;
icculus@1120
   142
    device->AllocHWSurface   = QZ_AllocHWSurface;
slouken@47
   143
    device->FreeHWSurface   = QZ_FreeHWSurface;
slouken@47
   144
    /* device->FlipHWSurface   = QZ_FlipHWSurface */;
slouken@47
   145
slouken@47
   146
    device->SetGamma     = QZ_SetGamma;
slouken@47
   147
    device->GetGamma     = QZ_GetGamma;
slouken@47
   148
    device->SetGammaRamp = QZ_SetGammaRamp;
slouken@47
   149
    device->GetGammaRamp = QZ_GetGammaRamp;
slouken@47
   150
slouken@47
   151
    device->GL_GetProcAddress = QZ_GL_GetProcAddress;
slouken@47
   152
    device->GL_GetAttribute   = QZ_GL_GetAttribute;
slouken@47
   153
    device->GL_MakeCurrent    = QZ_GL_MakeCurrent;
slouken@47
   154
    device->GL_SwapBuffers    = QZ_GL_SwapBuffers;
slouken@47
   155
    device->GL_LoadLibrary    = QZ_GL_LoadLibrary;
slouken@272
   156
slouken@47
   157
    device->FreeWMCursor   = QZ_FreeWMCursor;
slouken@47
   158
    device->CreateWMCursor = QZ_CreateWMCursor;
slouken@47
   159
    device->ShowWMCursor   = QZ_ShowWMCursor;
slouken@47
   160
    device->WarpWMCursor   = QZ_WarpWMCursor;
slouken@47
   161
    device->MoveWMCursor   = QZ_MoveWMCursor;
slouken@47
   162
    device->CheckMouseMode = QZ_CheckMouseMode;
slouken@47
   163
    device->InitOSKeymap   = QZ_InitOSKeymap;
slouken@47
   164
    device->PumpEvents     = QZ_PumpEvents;
slouken@47
   165
slouken@47
   166
    device->SetCaption    = QZ_SetCaption;
slouken@47
   167
    device->SetIcon       = QZ_SetIcon;
slouken@47
   168
    device->IconifyWindow = QZ_IconifyWindow;
slouken@47
   169
    /*device->GetWMInfo     = QZ_GetWMInfo;*/
slouken@47
   170
    device->GrabInput     = QZ_GrabInput;
slouken@272
   171
slouken@390
   172
    device->CreateYUVOverlay =  QZ_CreateYUVOverlay;
slouken@390
   173
slouken@390
   174
    device->free             = QZ_DeleteDevice;
slouken@272
   175
slouken@47
   176
    return device;
slouken@47
   177
}
slouken@47
   178
slouken@47
   179
static void QZ_DeleteDevice (SDL_VideoDevice *device) {
slouken@47
   180
slouken@47
   181
    free (device->hidden);
slouken@47
   182
    free (device);
slouken@47
   183
}
slouken@47
   184
slouken@47
   185
static int QZ_VideoInit (_THIS, SDL_PixelFormat *video_format) {
slouken@47
   186
slouken@272
   187
    /* Initialize the video settings; this data persists between mode switches */
slouken@272
   188
    display_id = kCGDirectMainDisplay;
slouken@272
   189
    save_mode  = CGDisplayCurrentMode    (display_id);
slouken@272
   190
    mode_list  = CGDisplayAvailableModes (display_id);
slouken@272
   191
    palette    = CGPaletteCreateDefaultColorPalette ();
slouken@47
   192
slouken@272
   193
    /* Gather some information that is useful to know about the display */
slouken@272
   194
    CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayBitsPerPixel),
slouken@272
   195
                      kCFNumberSInt32Type, &device_bpp);
slouken@47
   196
slouken@272
   197
    CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayWidth),
slouken@272
   198
                      kCFNumberSInt32Type, &device_width);
slouken@272
   199
slouken@272
   200
    CFNumberGetValue (CFDictionaryGetValue (save_mode, kCGDisplayHeight),
slouken@272
   201
                      kCFNumberSInt32Type, &device_height);
slouken@272
   202
slouken@272
   203
    video_format->BitsPerPixel = device_bpp;
slouken@272
   204
slouken@501
   205
    /* Set misc globals */
slouken@501
   206
    current_grab_mode = SDL_GRAB_OFF;
slouken@761
   207
    cursor_should_be_visible    = YES;
slouken@779
   208
    cursor_visible              = YES;
slouken@823
   209
    current_mods = 0;
slouken@501
   210
    
icculus@876
   211
    if ( Gestalt(gestaltSystemVersion, &system_version) != noErr )
icculus@876
   212
        system_version = 0;
slouken@934
   213
    
slouken@555
   214
    /* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
slouken@555
   215
    QZ_RegisterForSleepNotifications (this);
slouken@555
   216
    
slouken@1271
   217
    /* Fill in some window manager capabilities */
slouken@1271
   218
    this->info.wm_available = 1;
slouken@1271
   219
slouken@272
   220
    return 0;
slouken@47
   221
}
slouken@47
   222
slouken@47
   223
static SDL_Rect** QZ_ListModes (_THIS, SDL_PixelFormat *format, Uint32 flags) {
slouken@390
   224
slouken@272
   225
    CFIndex num_modes;
slouken@47
   226
    CFIndex i;
slouken@47
   227
slouken@47
   228
    int list_size = 0;
slouken@390
   229
slouken@47
   230
    /* Any windowed mode is acceptable */
slouken@47
   231
    if ( (flags & SDL_FULLSCREEN) == 0 )
slouken@47
   232
        return (SDL_Rect**)-1;
slouken@390
   233
slouken@47
   234
    /* Free memory from previous call, if any */
slouken@501
   235
    if ( client_mode_list != NULL ) {
slouken@47
   236
slouken@390
   237
        int i;
slouken@47
   238
slouken@501
   239
        for (i = 0; client_mode_list[i] != NULL; i++)
slouken@501
   240
            free (client_mode_list[i]);
slouken@47
   241
slouken@501
   242
        free (client_mode_list);
slouken@501
   243
        client_mode_list = NULL;
slouken@47
   244
    }
slouken@390
   245
slouken@272
   246
    num_modes = CFArrayGetCount (mode_list);
slouken@272
   247
slouken@47
   248
    /* Build list of modes with the requested bpp */
slouken@155
   249
    for (i = 0; i < num_modes; i++) {
slouken@390
   250
slouken@155
   251
        CFDictionaryRef onemode;
slouken@155
   252
        CFNumberRef     number;
slouken@390
   253
        int bpp;
slouken@47
   254
slouken@390
   255
        onemode = CFArrayGetValueAtIndex (mode_list, i);
slouken@390
   256
        number = CFDictionaryGetValue (onemode, kCGDisplayBitsPerPixel);
slouken@390
   257
        CFNumberGetValue (number, kCFNumberSInt32Type, &bpp);
slouken@390
   258
slouken@390
   259
        if (bpp == format->BitsPerPixel) {
slouken@390
   260
slouken@390
   261
            int intvalue;
slouken@390
   262
            int hasMode;
slouken@390
   263
            int width, height;
slouken@390
   264
slouken@390
   265
            number = CFDictionaryGetValue (onemode, kCGDisplayWidth);
slouken@390
   266
            CFNumberGetValue (number, kCFNumberSInt32Type, &intvalue);
slouken@390
   267
            width = (Uint16) intvalue;
slouken@390
   268
slouken@390
   269
            number = CFDictionaryGetValue (onemode, kCGDisplayHeight);
slouken@390
   270
            CFNumberGetValue (number, kCFNumberSInt32Type, &intvalue);
slouken@390
   271
            height = (Uint16) intvalue;
slouken@390
   272
slouken@390
   273
            /* Check if mode is already in the list */
slouken@390
   274
            {
slouken@390
   275
                int i;
slouken@390
   276
                hasMode = SDL_FALSE;
slouken@390
   277
                for (i = 0; i < list_size; i++) {
slouken@501
   278
                    if (client_mode_list[i]->w == width && 
slouken@501
   279
                        client_mode_list[i]->h == height) {
slouken@390
   280
                        hasMode = SDL_TRUE;
slouken@390
   281
                        break;
slouken@390
   282
                    }
slouken@155
   283
                }
slouken@155
   284
            }
slouken@47
   285
slouken@390
   286
            /* Grow the list and add mode to the list */
slouken@390
   287
            if ( ! hasMode ) {
slouken@390
   288
slouken@390
   289
                SDL_Rect *rect;
slouken@390
   290
slouken@390
   291
                list_size++;
slouken@390
   292
slouken@501
   293
                if (client_mode_list == NULL)
slouken@501
   294
                    client_mode_list = (SDL_Rect**) 
slouken@501
   295
                        malloc (sizeof(*client_mode_list) * (list_size+1) );
slouken@390
   296
                else
slouken@501
   297
                    client_mode_list = (SDL_Rect**) 
slouken@501
   298
                        realloc (client_mode_list, sizeof(*client_mode_list) * (list_size+1));
slouken@390
   299
slouken@501
   300
                rect = (SDL_Rect*) malloc (sizeof(**client_mode_list));
slouken@390
   301
slouken@501
   302
                if (client_mode_list == NULL || rect == NULL) {
slouken@390
   303
                    SDL_OutOfMemory ();
slouken@390
   304
                    return NULL;
slouken@390
   305
                }
slouken@390
   306
icculus@1218
   307
                rect->x = rect->y = 0;
slouken@390
   308
                rect->w = width;
slouken@390
   309
                rect->h = height;
slouken@390
   310
slouken@501
   311
                client_mode_list[list_size-1] = rect;
slouken@501
   312
                client_mode_list[list_size]   = NULL;
slouken@272
   313
            }
slouken@47
   314
        }
slouken@47
   315
    }
slouken@390
   316
slouken@155
   317
    /* Sort list largest to smallest (by area) */
slouken@155
   318
    {
slouken@155
   319
        int i, j;
slouken@155
   320
        for (i = 0; i < list_size; i++) {
slouken@155
   321
            for (j = 0; j < list_size-1; j++) {
slouken@390
   322
slouken@155
   323
                int area1, area2;
slouken@501
   324
                area1 = client_mode_list[j]->w * client_mode_list[j]->h;
slouken@501
   325
                area2 = client_mode_list[j+1]->w * client_mode_list[j+1]->h;
slouken@390
   326
slouken@155
   327
                if (area1 < area2) {
slouken@501
   328
                    SDL_Rect *tmp = client_mode_list[j];
slouken@501
   329
                    client_mode_list[j] = client_mode_list[j+1];
slouken@501
   330
                    client_mode_list[j+1] = tmp;
slouken@155
   331
                }
slouken@155
   332
            }
slouken@155
   333
        }
slouken@155
   334
    }
slouken@501
   335
    return client_mode_list;
slouken@47
   336
}
slouken@47
   337
slouken@657
   338
static SDL_bool QZ_WindowPosition(_THIS, int *x, int *y)
slouken@657
   339
{
slouken@657
   340
    const char *window = getenv("SDL_VIDEO_WINDOW_POS");
slouken@657
   341
    if ( window ) {
slouken@657
   342
        if ( sscanf(window, "%d,%d", x, y) == 2 ) {
slouken@657
   343
            return SDL_TRUE;
slouken@657
   344
        }
slouken@657
   345
    }
slouken@657
   346
    return SDL_FALSE;
slouken@657
   347
}
slouken@657
   348
icculus@1340
   349
static void QZ_UnsetVideoMode (_THIS, BOOL to_desktop) {
slouken@47
   350
slouken@47
   351
    /* Reset values that may change between switches */
slouken@501
   352
    this->info.blit_fill  = 0;
slouken@501
   353
    this->FillHWRect      = NULL;
slouken@501
   354
    this->UpdateRects     = NULL;
slouken@501
   355
    this->LockHWSurface   = NULL;
slouken@501
   356
    this->UnlockHWSurface = NULL;
slouken@501
   357
    
slouken@272
   358
    /* Release fullscreen resources */
slouken@47
   359
    if ( mode_flags & SDL_FULLSCREEN ) {
slouken@272
   360
slouken@435
   361
        NSRect screen_rect;
slouken@435
   362
        
slouken@588
   363
        /*  Release double buffer stuff */
icculus@1144
   364
        if ( mode_flags & SDL_DOUBLEBUF) {
slouken@588
   365
            quit_thread = YES;
slouken@588
   366
            SDL_SemPost (sem1);
slouken@588
   367
            SDL_WaitThread (thread, NULL);
slouken@588
   368
            SDL_DestroySemaphore (sem1);
slouken@588
   369
            SDL_DestroySemaphore (sem2);
slouken@588
   370
            free (sw_buffers[0]);
slouken@588
   371
        }
slouken@588
   372
        
slouken@501
   373
        /* 
slouken@501
   374
            Release the OpenGL context
slouken@501
   375
            Do this first to avoid trash on the display before fade
slouken@501
   376
        */
slouken@501
   377
        if ( mode_flags & SDL_OPENGL ) {
slouken@501
   378
        
slouken@272
   379
            QZ_TearDownOpenGL (this);
slouken@501
   380
            CGLSetFullScreen (NULL);
slouken@501
   381
        }
icculus@1340
   382
        if (to_desktop) {
icculus@1340
   383
            /* Restore original screen resolution/bpp */
icculus@1340
   384
            CGDisplaySwitchToMode (display_id, save_mode);
icculus@1340
   385
            CGReleaseAllDisplays ();
icculus@1340
   386
            ShowMenuBar ();
icculus@1340
   387
            /* 
icculus@1340
   388
                Reset the main screen's rectangle
icculus@1340
   389
                See comment in QZ_SetVideoFullscreen for why we do this
icculus@1340
   390
            */
icculus@1340
   391
            screen_rect = NSMakeRect(0,0,device_width,device_height);
icculus@1340
   392
            [ [ NSScreen mainScreen ] setFrame:screen_rect ];
icculus@1340
   393
        }
slouken@47
   394
    }
slouken@272
   395
    /* Release window mode resources */
slouken@390
   396
    else {
slouken@501
   397
        
slouken@158
   398
        [ qz_window close ];
icculus@1160
   399
        [ qz_window release ];
slouken@390
   400
        qz_window = nil;
slouken@501
   401
        window_view = nil;
icculus@1160
   402
slouken@272
   403
        /* Release the OpenGL context */
slouken@272
   404
        if ( mode_flags & SDL_OPENGL )
slouken@272
   405
            QZ_TearDownOpenGL (this);
slouken@47
   406
    }
slouken@272
   407
slouken@155
   408
    /* Signal successful teardown */
slouken@155
   409
    video_set = SDL_FALSE;
slouken@47
   410
}
slouken@47
   411
slouken@47
   412
static SDL_Surface* QZ_SetVideoFullScreen (_THIS, SDL_Surface *current, int width,
slouken@47
   413
                                           int height, int bpp, Uint32 flags) {
icculus@1220
   414
    boolean_t exact_match = 0;
slouken@435
   415
    NSRect screen_rect;
slouken@588
   416
    CGError error;
icculus@1340
   417
    CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
icculus@1340
   418
    
icculus@1340
   419
    /* Fade to black to hide resolution-switching flicker (and garbage
icculus@1340
   420
       that is displayed by a destroyed OpenGL context, if applicable) */
icculus@1340
   421
    if ( CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess ) {
icculus@1340
   422
        CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
icculus@1340
   423
    }
slouken@435
   424
    
slouken@501
   425
    /* Destroy any previous mode */
slouken@501
   426
    if (video_set == SDL_TRUE)
icculus@1340
   427
        QZ_UnsetVideoMode (this, FALSE);
slouken@501
   428
slouken@47
   429
    /* See if requested mode exists */
slouken@390
   430
    mode = CGDisplayBestModeForParameters (display_id, bpp, width,
slouken@390
   431
                                           height, &exact_match);
slouken@390
   432
slouken@47
   433
    /* Require an exact match to the requested mode */
slouken@47
   434
    if ( ! exact_match ) {
slouken@501
   435
        SDL_SetError ("Failed to find display resolution: %dx%dx%d", width, height, bpp);
slouken@47
   436
        goto ERR_NO_MATCH;
slouken@47
   437
    }
slouken@155
   438
slouken@47
   439
    /* Put up the blanking window (a window above all other windows) */
slouken@588
   440
    if (getenv ("SDL_SINGLEDISPLAY"))
slouken@588
   441
        error = CGDisplayCapture (display_id);
slouken@588
   442
    else
slouken@588
   443
        error = CGCaptureAllDisplays ();
slouken@588
   444
        
slouken@588
   445
    if ( CGDisplayNoErr != error ) {
slouken@47
   446
        SDL_SetError ("Failed capturing display");
slouken@47
   447
        goto ERR_NO_CAPTURE;
slouken@47
   448
    }
slouken@272
   449
slouken@47
   450
    /* Do the physical switch */
slouken@47
   451
    if ( CGDisplayNoErr != CGDisplaySwitchToMode (display_id, mode) ) {
slouken@47
   452
        SDL_SetError ("Failed switching display resolution");
slouken@47
   453
        goto ERR_NO_SWITCH;
slouken@47
   454
    }
slouken@272
   455
slouken@47
   456
    current->pixels = (Uint32*) CGDisplayBaseAddress (display_id);
slouken@47
   457
    current->pitch  = CGDisplayBytesPerRow (display_id);
slouken@47
   458
slouken@58
   459
    current->flags = 0;
slouken@47
   460
    current->w = width;
slouken@47
   461
    current->h = height;
slouken@390
   462
    current->flags |= SDL_FULLSCREEN;
slouken@47
   463
    current->flags |= SDL_HWSURFACE;
slouken@501
   464
    current->flags |= SDL_PREALLOC;
slouken@501
   465
    
slouken@501
   466
    this->UpdateRects     = QZ_DirectUpdate;
slouken@501
   467
    this->LockHWSurface   = QZ_LockHWSurface;
slouken@501
   468
    this->UnlockHWSurface = QZ_UnlockHWSurface;
slouken@588
   469
slouken@588
   470
    /* Setup double-buffer emulation */
slouken@588
   471
    if ( flags & SDL_DOUBLEBUF ) {
slouken@588
   472
        
slouken@588
   473
        /*
slouken@588
   474
            Setup a software backing store for reasonable results when
slouken@588
   475
            double buffering is requested (since a single-buffered hardware
slouken@588
   476
            surface looks hideous).
slouken@588
   477
            
slouken@588
   478
            The actual screen blit occurs in a separate thread to allow 
slouken@588
   479
            other blitting while waiting on the VBL (and hence results in higher framerates).
slouken@588
   480
        */
slouken@588
   481
        this->LockHWSurface = NULL;
slouken@588
   482
        this->UnlockHWSurface = NULL;
slouken@588
   483
        this->UpdateRects = NULL;
slouken@588
   484
        
slouken@588
   485
        current->flags |= (SDL_HWSURFACE|SDL_DOUBLEBUF);
slouken@588
   486
        this->UpdateRects = QZ_DoubleBufferUpdate;
slouken@588
   487
        this->LockHWSurface = QZ_LockDoubleBuffer;
slouken@588
   488
        this->UnlockHWSurface = QZ_UnlockDoubleBuffer;
slouken@588
   489
        this->FlipHWSurface = QZ_FlipDoubleBuffer;
slouken@588
   490
slouken@588
   491
        current->pixels = malloc (current->pitch * current->h * 2);
slouken@588
   492
        if (current->pixels == NULL) {
slouken@588
   493
            SDL_OutOfMemory ();
slouken@588
   494
            goto ERR_DOUBLEBUF;
slouken@588
   495
        }
slouken@588
   496
        
slouken@588
   497
        sw_buffers[0] = current->pixels;
slouken@588
   498
        sw_buffers[1] = (Uint8*)current->pixels + current->pitch * current->h;
slouken@588
   499
        
slouken@588
   500
        quit_thread = NO;
slouken@588
   501
        sem1 = SDL_CreateSemaphore (0);
slouken@588
   502
        sem2 = SDL_CreateSemaphore (1);
slouken@588
   503
        thread = SDL_CreateThread ((int (*)(void *))QZ_ThreadFlip, this);
slouken@47
   504
    }
slouken@390
   505
slouken@47
   506
    if ( CGDisplayCanSetPalette (display_id) )
slouken@47
   507
        current->flags |= SDL_HWPALETTE;
slouken@390
   508
slouken@47
   509
    /* Setup OpenGL for a fullscreen context */
slouken@47
   510
    if (flags & SDL_OPENGL) {
slouken@47
   511
slouken@47
   512
        CGLError err;
slouken@47
   513
        CGLContextObj ctx;
slouken@390
   514
slouken@47
   515
        if ( ! QZ_SetupOpenGL (this, bpp, flags) ) {
slouken@272
   516
            goto ERR_NO_GL;
slouken@47
   517
        }
slouken@390
   518
slouken@47
   519
        ctx = [ gl_context cglContext ];
slouken@47
   520
        err = CGLSetFullScreen (ctx);
slouken@390
   521
slouken@47
   522
        if (err) {
slouken@501
   523
            SDL_SetError ("Error setting OpenGL fullscreen: %s", CGLErrorString(err));
slouken@47
   524
            goto ERR_NO_GL;
slouken@47
   525
        }
slouken@390
   526
slouken@47
   527
        [ gl_context makeCurrentContext];
slouken@272
   528
slouken@272
   529
        glClear (GL_COLOR_BUFFER_BIT);
slouken@272
   530
slouken@272
   531
        [ gl_context flushBuffer ];
slouken@390
   532
slouken@47
   533
        current->flags |= SDL_OPENGL;
slouken@47
   534
    }
slouken@47
   535
slouken@47
   536
    /* If we don't hide menu bar, it will get events and interrupt the program */
slouken@47
   537
    HideMenuBar ();
slouken@272
   538
icculus@1340
   539
    /* Fade in again (asynchronously) */
icculus@1340
   540
    if ( fade_token != kCGDisplayFadeReservationInvalidToken ) {
icculus@1340
   541
        CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   542
        CGReleaseDisplayFadeReservation(fade_token);
icculus@1340
   543
    }
slouken@390
   544
slouken@435
   545
    /* 
slouken@501
   546
        There is a bug in Cocoa where NSScreen doesn't synchronize
slouken@501
   547
        with CGDirectDisplay, so the main screen's frame is wrong.
slouken@501
   548
        As a result, coordinate translation produces incorrect results.
slouken@501
   549
        We can hack around this bug by setting the screen rect
slouken@501
   550
        ourselves. This hack should be removed if/when the bug is fixed.
slouken@435
   551
    */
slouken@435
   552
    screen_rect = NSMakeRect(0,0,width,height);
slouken@435
   553
    [ [ NSScreen mainScreen ] setFrame:screen_rect ]; 
slouken@435
   554
slouken@47
   555
    /* Save the flags to ensure correct tear-down */
slouken@47
   556
    mode_flags = current->flags;
slouken@390
   557
icculus@1119
   558
    /* we're fullscreen, so flag all input states... */
icculus@1119
   559
    SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS | SDL_APPINPUTFOCUS | SDL_APPACTIVE);
icculus@1119
   560
slouken@47
   561
    return current;
slouken@47
   562
slouken@390
   563
    /* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
slouken@588
   564
ERR_NO_GL:      
slouken@588
   565
ERR_DOUBLEBUF:  CGDisplaySwitchToMode (display_id, save_mode);
slouken@588
   566
ERR_NO_SWITCH:  CGReleaseAllDisplays ();
icculus@1340
   567
ERR_NO_CAPTURE:
icculus@1340
   568
ERR_NO_MATCH:   if ( fade_token != kCGDisplayFadeReservationInvalidToken ) {
icculus@1340
   569
                    CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   570
                    CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
   571
                }
icculus@1340
   572
                return NULL;
slouken@47
   573
}
slouken@47
   574
slouken@47
   575
static SDL_Surface* QZ_SetVideoWindowed (_THIS, SDL_Surface *current, int width,
icculus@1183
   576
                                         int height, int *bpp, Uint32 flags) {
slouken@58
   577
    unsigned int style;
slouken@501
   578
    NSRect contentRect;
slouken@683
   579
    BOOL isCustom = NO;
slouken@657
   580
    int center_window = 1;
slouken@657
   581
    int origin_x, origin_y;
icculus@1340
   582
    CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
slouken@390
   583
slouken@58
   584
    current->flags = 0;
slouken@58
   585
    current->w = width;
slouken@47
   586
    current->h = height;
slouken@501
   587
    
slouken@501
   588
    contentRect = NSMakeRect (0, 0, width, height);
slouken@501
   589
    
slouken@501
   590
    /*
slouken@501
   591
        Check if we should completely destroy the previous mode 
slouken@501
   592
        - If it is fullscreen
slouken@501
   593
        - If it has different noframe or resizable attribute
slouken@501
   594
        - If it is OpenGL (since gl attributes could be different)
slouken@501
   595
        - If new mode is OpenGL, but previous mode wasn't
slouken@501
   596
    */
icculus@1340
   597
    if (video_set == SDL_TRUE) {
icculus@1340
   598
        if (mode_flags & SDL_FULLSCREEN) {
icculus@1340
   599
            /* Fade to black to hide resolution-switching flicker (and garbage
icculus@1340
   600
               that is displayed by a destroyed OpenGL context, if applicable) */
icculus@1340
   601
            if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) {
icculus@1340
   602
                CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
icculus@1340
   603
            }
icculus@1340
   604
            QZ_UnsetVideoMode (this, TRUE);
icculus@1340
   605
        }
icculus@1340
   606
        else if ( ((mode_flags ^ flags) & (SDL_NOFRAME|SDL_RESIZABLE)) ||
icculus@1340
   607
                  (mode_flags & SDL_OPENGL) || 
icculus@1340
   608
                  (flags & SDL_OPENGL) ) {
icculus@1340
   609
            QZ_UnsetVideoMode (this, TRUE);
icculus@1340
   610
        }
icculus@1340
   611
    }
slouken@683
   612
    
slouken@683
   613
    /* Check for user-specified window and view */
slouken@683
   614
    {
slouken@683
   615
        char *windowPtrString = getenv ("SDL_NSWindowPointer");
slouken@683
   616
        char *viewPtrString = getenv ("SDL_NSQuickDrawViewPointer");
slouken@683
   617
    
slouken@683
   618
        if (windowPtrString && viewPtrString) {
slouken@683
   619
            
slouken@683
   620
            /* Release any previous window */
slouken@683
   621
            if ( qz_window ) {
slouken@683
   622
                [ qz_window release ];
slouken@683
   623
                qz_window = nil;
slouken@683
   624
            }
slouken@683
   625
            
slouken@683
   626
            qz_window = (NSWindow*)atoi(windowPtrString);
slouken@683
   627
            window_view = (NSQuickDrawView*)atoi(viewPtrString);
slouken@683
   628
            isCustom = YES;
slouken@683
   629
            
slouken@683
   630
            /* 
slouken@683
   631
                Retain reference to window because we
slouken@683
   632
                might release it in QZ_UnsetVideoMode
slouken@683
   633
            */
slouken@683
   634
            [ qz_window retain ];
slouken@683
   635
            
slouken@683
   636
            style = [ qz_window styleMask ];
slouken@683
   637
            /* Check resizability */
slouken@683
   638
            if ( style & NSResizableWindowMask )
slouken@683
   639
                current->flags |= SDL_RESIZABLE;
slouken@683
   640
            
slouken@683
   641
            /* Check frame */
slouken@683
   642
            if ( style & NSBorderlessWindowMask )
slouken@683
   643
                current->flags |= SDL_NOFRAME;
slouken@683
   644
        }
slouken@683
   645
    }
slouken@683
   646
    
slouken@501
   647
    /* Check if we should recreate the window */
slouken@501
   648
    if (qz_window == nil) {
slouken@501
   649
    
slouken@501
   650
        /* Set the window style based on input flags */
slouken@501
   651
        if ( flags & SDL_NOFRAME ) {
slouken@501
   652
            style = NSBorderlessWindowMask;
slouken@501
   653
            current->flags |= SDL_NOFRAME;
slouken@501
   654
        } else {
slouken@501
   655
            style = NSTitledWindowMask;
slouken@501
   656
            style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
slouken@501
   657
            if ( flags & SDL_RESIZABLE ) {
slouken@501
   658
                style |= NSResizableWindowMask;
slouken@501
   659
                current->flags |= SDL_RESIZABLE;
slouken@501
   660
            }
slouken@501
   661
        }
slouken@501
   662
                
slouken@657
   663
        if ( QZ_WindowPosition(this, &origin_x, &origin_y) ) {
slouken@657
   664
            center_window = 0;
slouken@657
   665
            contentRect.origin.x = (float)origin_x;
slouken@657
   666
            contentRect.origin.y = (float)origin_y;            
slouken@657
   667
        }
slouken@657
   668
        
slouken@501
   669
        /* Manually create a window, avoids having a nib file resource */
slouken@501
   670
        qz_window = [ [ SDL_QuartzWindow alloc ] 
slouken@501
   671
            initWithContentRect:contentRect
slouken@501
   672
                styleMask:style 
slouken@501
   673
                    backing:NSBackingStoreBuffered
slouken@501
   674
                        defer:NO ];
slouken@501
   675
                          
slouken@501
   676
        if (qz_window == nil) {
slouken@501
   677
            SDL_SetError ("Could not create the Cocoa window");
icculus@1340
   678
            if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
   679
                CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   680
                CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
   681
            }
slouken@501
   682
            return NULL;
slouken@501
   683
        }
slouken@501
   684
    
slouken@1487
   685
        /*[ qz_window setReleasedWhenClosed:YES ];*/
slouken@501
   686
        QZ_SetCaption(this, this->wm_title, this->wm_icon);
slouken@501
   687
        [ qz_window setAcceptsMouseMovedEvents:YES ];
slouken@501
   688
        [ qz_window setViewsNeedDisplay:NO ];
slouken@657
   689
        if ( center_window ) {
slouken@657
   690
            [ qz_window center ];
slouken@657
   691
        }
slouken@501
   692
        [ qz_window setDelegate:
slouken@501
   693
            [ [ [ SDL_QuartzWindowDelegate alloc ] init ] autorelease ] ];
slouken@501
   694
    }
slouken@501
   695
    /* We already have a window, just change its size */
slouken@501
   696
    else {
slouken@501
   697
    
slouken@683
   698
        if (!isCustom) {
slouken@683
   699
            [ qz_window setContentSize:contentRect.size ];
slouken@683
   700
            current->flags |= (SDL_NOFRAME|SDL_RESIZABLE) & mode_flags;
slouken@832
   701
            [ window_view setFrameSize:contentRect.size ];
slouken@683
   702
        }
slouken@501
   703
    }
slouken@390
   704
slouken@501
   705
    /* For OpenGL, we bind the context to a subview */
slouken@47
   706
    if ( flags & SDL_OPENGL ) {
slouken@390
   707
icculus@1183
   708
        if ( ! QZ_SetupOpenGL (this, *bpp, flags) ) {
icculus@1340
   709
            if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
   710
                CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   711
                CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
   712
            }
slouken@47
   713
            return NULL;
slouken@47
   714
        }
slouken@390
   715
slouken@501
   716
        window_view = [ [ NSView alloc ] initWithFrame:contentRect ];
slouken@832
   717
        [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
slouken@501
   718
        [ [ qz_window contentView ] addSubview:window_view ];
slouken@501
   719
        [ gl_context setView: window_view ];
slouken@501
   720
        [ window_view release ];
slouken@47
   721
        [ gl_context makeCurrentContext];
slouken@158
   722
        [ qz_window makeKeyAndOrderFront:nil ];
slouken@47
   723
        current->flags |= SDL_OPENGL;
slouken@47
   724
    }
slouken@501
   725
    /* For 2D, we set the subview to an NSQuickDrawView */
slouken@47
   726
    else {
icculus@1183
   727
        short qdbpp = 0;
slouken@390
   728
slouken@501
   729
        /* Only recreate the view if it doesn't already exist */
slouken@501
   730
        if (window_view == nil) {
slouken@501
   731
        
slouken@761
   732
            window_view = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ];
slouken@832
   733
            [ window_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
slouken@501
   734
            [ [ qz_window contentView ] addSubview:window_view ];
slouken@501
   735
            [ window_view release ];
slouken@501
   736
            [ qz_window makeKeyAndOrderFront:nil ];
slouken@501
   737
        }
slouken@501
   738
        
slouken@272
   739
        LockPortBits ( [ window_view qdPort ] );
slouken@272
   740
        current->pixels = GetPixBaseAddr ( GetPortPixMap ( [ window_view qdPort ] ) );
slouken@272
   741
        current->pitch  = GetPixRowBytes ( GetPortPixMap ( [ window_view qdPort ] ) );
icculus@1183
   742
        qdbpp           = GetPixDepth ( GetPortPixMap ( [ window_view qdPort ] ) );
slouken@498
   743
        UnlockPortBits ( [ window_view qdPort ] );
slouken@501
   744
icculus@1183
   745
        /* QuickDraw may give a 16-bit shadow surface on 8-bit displays! */
icculus@1183
   746
        *bpp = qdbpp;
icculus@1183
   747
slouken@47
   748
        current->flags |= SDL_SWSURFACE;
slouken@47
   749
        current->flags |= SDL_PREALLOC;
slouken@498
   750
        current->flags |= SDL_ASYNCBLIT;
slouken@498
   751
        
slouken@683
   752
        /* 
slouken@683
   753
            current->pixels now points to the window's pixels
slouken@683
   754
            We want it to point to the *view's* pixels 
slouken@683
   755
        */
slouken@683
   756
        { 
slouken@683
   757
            int vOffset = [ qz_window frame ].size.height - 
slouken@683
   758
                [ window_view frame ].size.height - [ window_view frame ].origin.y;
slouken@683
   759
            
slouken@683
   760
            int hOffset = [ window_view frame ].origin.x;
slouken@683
   761
                    
slouken@1487
   762
            current->pixels = (Uint8 *)current->pixels + (vOffset * current->pitch) + hOffset * (qdbpp/8);
slouken@683
   763
        }
slouken@498
   764
        this->UpdateRects     = QZ_UpdateRects;
slouken@498
   765
        this->LockHWSurface   = QZ_LockWindow;
slouken@498
   766
        this->UnlockHWSurface = QZ_UnlockWindow;
slouken@47
   767
    }
slouken@390
   768
slouken@272
   769
    /* Save flags to ensure correct teardown */
slouken@272
   770
    mode_flags = current->flags;
slouken@390
   771
icculus@1340
   772
    /* Fade in again (asynchronously) if we came from a fullscreen mode and faded to black */
icculus@1340
   773
    if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
   774
        CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
   775
        CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
   776
    }
icculus@1340
   777
slouken@47
   778
    return current;
slouken@47
   779
}
slouken@47
   780
slouken@390
   781
static SDL_Surface* QZ_SetVideoMode (_THIS, SDL_Surface *current, int width,
slouken@390
   782
                                     int height, int bpp, Uint32 flags) {
slouken@47
   783
slouken@47
   784
    current->flags = 0;
icculus@852
   785
    current->pixels = NULL;
slouken@390
   786
slouken@47
   787
    /* Setup full screen video */
slouken@47
   788
    if ( flags & SDL_FULLSCREEN ) {
slouken@47
   789
        current = QZ_SetVideoFullScreen (this, current, width, height, bpp, flags );
slouken@47
   790
        if (current == NULL)
slouken@47
   791
            return NULL;
slouken@47
   792
    }
slouken@47
   793
    /* Setup windowed video */
slouken@47
   794
    else {
slouken@47
   795
        /* Force bpp to the device's bpp */
slouken@272
   796
        bpp = device_bpp;
icculus@1183
   797
        current = QZ_SetVideoWindowed (this, current, width, height, &bpp, flags);
slouken@47
   798
        if (current == NULL)
slouken@47
   799
            return NULL;
slouken@47
   800
    }
slouken@390
   801
slouken@47
   802
    /* Setup the new pixel format */
slouken@47
   803
    {
slouken@390
   804
        int amask = 0,
slouken@390
   805
        rmask = 0,
slouken@390
   806
        gmask = 0,
slouken@390
   807
        bmask = 0;
slouken@390
   808
slouken@47
   809
        switch (bpp) {
slouken@47
   810
            case 16:   /* (1)-5-5-5 RGB */
slouken@390
   811
                amask = 0;
slouken@58
   812
                rmask = 0x7C00;
slouken@58
   813
                gmask = 0x03E0;
slouken@58
   814
                bmask = 0x001F;
slouken@47
   815
                break;
slouken@47
   816
            case 24:
slouken@47
   817
                SDL_SetError ("24bpp is not available");
slouken@47
   818
                return NULL;
slouken@47
   819
            case 32:   /* (8)-8-8-8 ARGB */
slouken@155
   820
                amask = 0x00000000;
slouken@47
   821
                rmask = 0x00FF0000;
slouken@47
   822
                gmask = 0x0000FF00;
slouken@47
   823
                bmask = 0x000000FF;
slouken@47
   824
                break;
slouken@47
   825
        }
slouken@390
   826
slouken@47
   827
        if ( ! SDL_ReallocFormat (current, bpp,
slouken@47
   828
                                  rmask, gmask, bmask, amask ) ) {
slouken@390
   829
            SDL_SetError ("Couldn't reallocate pixel format");
slouken@390
   830
            return NULL;
slouken@390
   831
           }
slouken@47
   832
    }
slouken@390
   833
slouken@272
   834
    /* Signal successful completion (used internally) */
slouken@155
   835
    video_set = SDL_TRUE;
slouken@390
   836
slouken@47
   837
    return current;
slouken@47
   838
}
slouken@47
   839
slouken@390
   840
static int QZ_ToggleFullScreen (_THIS, int on) {
slouken@576
   841
    return 0;
slouken@47
   842
}
slouken@47
   843
slouken@390
   844
static int QZ_SetColors (_THIS, int first_color, int num_colors,
slouken@390
   845
                         SDL_Color *colors) {
slouken@47
   846
slouken@47
   847
    CGTableCount  index;
slouken@47
   848
    CGDeviceColor color;
slouken@390
   849
slouken@47
   850
    for (index = first_color; index < first_color+num_colors; index++) {
slouken@390
   851
slouken@47
   852
        /* Clamp colors between 0.0 and 1.0 */
slouken@47
   853
        color.red   = colors->r / 255.0;
slouken@47
   854
        color.blue  = colors->b / 255.0;
slouken@47
   855
        color.green = colors->g / 255.0;
slouken@390
   856
slouken@47
   857
        colors++;
slouken@390
   858
slouken@47
   859
        CGPaletteSetColorAtIndex (palette, color, index);
slouken@47
   860
    }
slouken@390
   861
slouken@47
   862
    if ( CGDisplayNoErr != CGDisplaySetPalette (display_id, palette) )
slouken@47
   863
        return 0;
slouken@390
   864
slouken@47
   865
    return 1;
slouken@47
   866
}
slouken@47
   867
slouken@588
   868
static int QZ_LockDoubleBuffer (_THIS, SDL_Surface *surface) {
slouken@588
   869
slouken@588
   870
    return 1;
slouken@588
   871
}
slouken@588
   872
slouken@588
   873
static void QZ_UnlockDoubleBuffer (_THIS, SDL_Surface *surface) {
slouken@588
   874
slouken@588
   875
}
slouken@588
   876
slouken@588
   877
 /* The VBL delay is based on code by Ian R Ollmann's RezLib <iano@cco.caltech.edu> */
slouken@588
   878
 static AbsoluteTime QZ_SecondsToAbsolute ( double seconds ) {
slouken@588
   879
    
slouken@588
   880
    union
slouken@588
   881
    {
slouken@761
   882
        UInt64 i;
slouken@588
   883
        Nanoseconds ns;
slouken@588
   884
    } temp;
slouken@588
   885
        
slouken@588
   886
    temp.i = seconds * 1000000000.0;
slouken@588
   887
    
slouken@588
   888
    return NanosecondsToAbsolute ( temp.ns );
slouken@588
   889
}
slouken@588
   890
slouken@588
   891
static int QZ_ThreadFlip (_THIS) {
slouken@588
   892
slouken@588
   893
    Uint8 *src, *dst;
slouken@588
   894
    int skip, len, h;
slouken@588
   895
    
slouken@588
   896
    /*
slouken@588
   897
        Give this thread the highest scheduling priority possible,
slouken@588
   898
        in the hopes that it will immediately run after the VBL delay
slouken@588
   899
    */
slouken@588
   900
    {
slouken@588
   901
        pthread_t current_thread;
slouken@588
   902
        int policy;
slouken@588
   903
        struct sched_param param;
slouken@588
   904
        
slouken@588
   905
        current_thread = pthread_self ();
slouken@588
   906
        pthread_getschedparam (current_thread, &policy, &param);
slouken@588
   907
        policy = SCHED_RR;
slouken@588
   908
        param.sched_priority = sched_get_priority_max (policy);
slouken@588
   909
        pthread_setschedparam (current_thread, policy, &param);
slouken@588
   910
    }
slouken@588
   911
    
slouken@588
   912
    while (1) {
slouken@588
   913
    
slouken@588
   914
        SDL_SemWait (sem1);
slouken@588
   915
        if (quit_thread)
slouken@588
   916
            return 0;
slouken@588
   917
                
icculus@1219
   918
        /*
icculus@1219
   919
         * We have to add SDL_VideoSurface->offset here, since we might be a
icculus@1219
   920
         *  smaller surface in the center of the framebuffer (you asked for
icculus@1219
   921
         *  a fullscreen resolution smaller than the hardware could supply
icculus@1219
   922
         *  so SDL is centering it in a bigger resolution)...
icculus@1219
   923
         */
slouken@1487
   924
        dst = (Uint8 *)CGDisplayBaseAddress (display_id) + SDL_VideoSurface->offset;
icculus@1219
   925
        src = current_buffer + SDL_VideoSurface->offset;
slouken@588
   926
        len = SDL_VideoSurface->w * SDL_VideoSurface->format->BytesPerPixel;
slouken@588
   927
        h = SDL_VideoSurface->h;
slouken@588
   928
        skip = SDL_VideoSurface->pitch;
slouken@588
   929
    
slouken@588
   930
        /* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
slouken@588
   931
        {
slouken@588
   932
            
slouken@588
   933
            /* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
slouken@588
   934
            double refreshRate;
slouken@588
   935
            double linesPerSecond;
slouken@588
   936
            double target;
slouken@588
   937
            double position;
slouken@588
   938
            double adjustment;
slouken@588
   939
            AbsoluteTime nextTime;        
slouken@588
   940
            CFNumberRef refreshRateCFNumber;
slouken@588
   941
            
slouken@588
   942
            refreshRateCFNumber = CFDictionaryGetValue (mode, kCGDisplayRefreshRate);
slouken@588
   943
            if ( NULL == refreshRateCFNumber ) {
slouken@588
   944
                SDL_SetError ("Mode has no refresh rate");
slouken@588
   945
                goto ERROR;
slouken@588
   946
            }
slouken@588
   947
            
slouken@588
   948
            if ( 0 == CFNumberGetValue (refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) ) {
slouken@588
   949
                SDL_SetError ("Error getting refresh rate");
slouken@588
   950
                goto ERROR;
slouken@588
   951
            }
slouken@588
   952
            
slouken@588
   953
            if ( 0 == refreshRate ) {
slouken@588
   954
               
slouken@588
   955
               SDL_SetError ("Display has no refresh rate, using 60hz");
slouken@588
   956
                
slouken@588
   957
                /* ok, for LCD's we'll emulate a 60hz refresh, which may or may not look right */
slouken@588
   958
                refreshRate = 60.0;
slouken@588
   959
            }
slouken@588
   960
            
slouken@588
   961
            linesPerSecond = refreshRate * h;
slouken@588
   962
            target = h;
slouken@588
   963
        
slouken@588
   964
            /* Figure out the first delay so we start off about right */
slouken@588
   965
            position = CGDisplayBeamPosition (display_id);
slouken@588
   966
            if (position > target)
slouken@588
   967
                position = 0;
slouken@588
   968
            
slouken@588
   969
            adjustment = (target - position) / linesPerSecond; 
slouken@588
   970
            
slouken@588
   971
            nextTime = AddAbsoluteToAbsolute (UpTime (), QZ_SecondsToAbsolute (adjustment));
slouken@588
   972
        
slouken@588
   973
            MPDelayUntil (&nextTime);
slouken@588
   974
        }
slouken@588
   975
        
slouken@588
   976
        
slouken@588
   977
        /* On error, skip VBL delay */
slouken@588
   978
        ERROR:
slouken@588
   979
        
slouken@588
   980
        while ( h-- ) {
slouken@588
   981
        
slouken@588
   982
            memcpy (dst, src, len);
slouken@588
   983
            src += skip;
slouken@588
   984
            dst += skip;
slouken@588
   985
        }
slouken@588
   986
        
slouken@588
   987
        /* signal flip completion */
slouken@588
   988
        SDL_SemPost (sem2);
slouken@588
   989
    }
slouken@588
   990
    
slouken@588
   991
    return 0;
slouken@588
   992
}
slouken@588
   993
        
slouken@588
   994
static int QZ_FlipDoubleBuffer (_THIS, SDL_Surface *surface) {
slouken@588
   995
slouken@588
   996
    /* wait for previous flip to complete */
slouken@588
   997
    SDL_SemWait (sem2);
slouken@588
   998
    
slouken@588
   999
    current_buffer = surface->pixels;
slouken@588
  1000
        
slouken@588
  1001
    if (surface->pixels == sw_buffers[0])
slouken@588
  1002
        surface->pixels = sw_buffers[1];
slouken@588
  1003
    else
slouken@588
  1004
        surface->pixels = sw_buffers[0];
slouken@588
  1005
    
slouken@588
  1006
    /* signal worker thread to do the flip */
slouken@588
  1007
    SDL_SemPost (sem1);
slouken@588
  1008
    
slouken@588
  1009
    return 0;
slouken@588
  1010
}
slouken@588
  1011
slouken@588
  1012
slouken@588
  1013
static void QZ_DoubleBufferUpdate (_THIS, int num_rects, SDL_Rect *rects) {
slouken@588
  1014
slouken@588
  1015
    /* perform a flip if someone calls updaterects on a doublebuferred surface */
slouken@588
  1016
    this->FlipHWSurface (this, SDL_VideoSurface);
slouken@588
  1017
}
slouken@588
  1018
slouken@47
  1019
static void QZ_DirectUpdate (_THIS, int num_rects, SDL_Rect *rects) {
slouken@390
  1020
#pragma unused(this,num_rects,rects)
slouken@47
  1021
}
slouken@47
  1022
slouken@501
  1023
/*
slouken@501
  1024
    The obscured code is based on work by Matt Slot fprefect@ambrosiasw.com,
slouken@501
  1025
    who supplied sample code for Carbon.
slouken@501
  1026
*/
slouken@761
  1027
slouken@1487
  1028
/*#define TEST_OBSCURED 1*/
slouken@761
  1029
slouken@761
  1030
#if TEST_OBSCURED
slouken@761
  1031
#include "CGS.h"
slouken@761
  1032
#endif
slouken@761
  1033
slouken@272
  1034
static int QZ_IsWindowObscured (NSWindow *window) {
slouken@272
  1035
slouken@272
  1036
slouken@272
  1037
#if TEST_OBSCURED
slouken@390
  1038
slouken@501
  1039
    /*  
slouken@501
  1040
        In order to determine if a direct copy to the screen is possible,
slouken@390
  1041
        we must figure out if there are any windows covering ours (including shadows).
slouken@390
  1042
        This can be done by querying the window server about the on screen
slouken@390
  1043
        windows for their screen rectangle and window level.
slouken@390
  1044
        The procedure used below is puts accuracy before speed; however, it aims to call
slouken@390
  1045
        the window server the fewest number of times possible to keep things reasonable.
slouken@390
  1046
        In my testing on a 300mhz G3, this routine typically takes < 2 ms. -DW
slouken@272
  1047
    
slouken@390
  1048
    Notes:
slouken@390
  1049
        -Calls into the Window Server involve IPC which is slow.
slouken@390
  1050
        -Getting a rectangle seems slower than getting the window level
slouken@390
  1051
        -The window list we get back is in sorted order, top to bottom
slouken@390
  1052
        -On average, I suspect, most windows above ours are dock icon windows (hence optimization)
slouken@390
  1053
        -Some windows above ours are always there, and cannot move or obscure us (menu bar)
slouken@272
  1054
    
slouken@390
  1055
    Bugs:
slouken@390
  1056
        -no way (yet) to deactivate direct drawing when a window is dragged,
slouken@390
  1057
        or suddenly obscured, so drawing continues and can produce garbage
slouken@390
  1058
        We need some kind of locking mechanism on window movement to prevent this
slouken@390
  1059
    
slouken@390
  1060
        -deactivated normal windows use activated normal
slouken@390
  1061
        window shadows (slight inaccuraccy)
slouken@272
  1062
    */
slouken@390
  1063
slouken@272
  1064
    /* Cache the connection to the window server */
slouken@390
  1065
    static CGSConnectionID    cgsConnection = (CGSConnectionID) -1;
slouken@390
  1066
slouken@272
  1067
    /* Cache the dock icon windows */
slouken@272
  1068
    static CGSWindowID          dockIcons[kMaxWindows];
slouken@272
  1069
    static int                  numCachedDockIcons = 0;
slouken@390
  1070
slouken@390
  1071
    CGSWindowID                windows[kMaxWindows];
slouken@390
  1072
    CGSWindowCount             i, count;
slouken@390
  1073
    CGSWindowLevel             winLevel;
slouken@390
  1074
    CGSRect                    winRect;
slouken@272
  1075
slouken@272
  1076
    CGSRect contentRect;
slouken@272
  1077
    int     windowNumber;
slouken@390
  1078
    int     firstDockIcon;
slouken@272
  1079
    int     dockIconCacheMiss;
slouken@272
  1080
    int     windowContentOffset;
slouken@390
  1081
slouken@272
  1082
    int     obscured = SDL_TRUE;
slouken@390
  1083
slouken@272
  1084
    if ( [ window isVisible ] ) {
slouken@390
  1085
slouken@501
  1086
        /*  
slouken@501
  1087
            walk the window list looking for windows over top of
slouken@501
  1088
            (or casting a shadow on) ours 
slouken@501
  1089
        */
slouken@390
  1090
slouken@501
  1091
        /* 
slouken@501
  1092
           Get a connection to the window server
slouken@501
  1093
           Should probably be moved out into SetVideoMode() or InitVideo()
slouken@501
  1094
        */
slouken@272
  1095
        if (cgsConnection == (CGSConnectionID) -1) {
slouken@272
  1096
            cgsConnection = (CGSConnectionID) 0;
slouken@272
  1097
            cgsConnection = _CGSDefaultConnection ();
slouken@272
  1098
        }
slouken@390
  1099
slouken@390
  1100
        if (cgsConnection) {
slouken@390
  1101
slouken@272
  1102
            if ( ! [ window styleMask ] & NSBorderlessWindowMask )
slouken@272
  1103
                windowContentOffset = 22;
slouken@272
  1104
            else
slouken@272
  1105
                windowContentOffset = 0;
slouken@390
  1106
slouken@272
  1107
            windowNumber = [ window windowNumber ];
slouken@390
  1108
slouken@272
  1109
            /* The window list is sorted according to order on the screen */
slouken@272
  1110
            count = 0;
slouken@272
  1111
            CGSGetOnScreenWindowList (cgsConnection, 0, kMaxWindows, windows, &count);
slouken@272
  1112
            CGSGetScreenRectForWindow (cgsConnection, windowNumber, &contentRect);
slouken@390
  1113
slouken@272
  1114
            /* adjust rect for window title bar (if present) */
slouken@272
  1115
            contentRect.origin.y    += windowContentOffset;
slouken@272
  1116
            contentRect.size.height -= windowContentOffset;
slouken@390
  1117
slouken@272
  1118
            firstDockIcon = -1;
slouken@272
  1119
            dockIconCacheMiss = SDL_FALSE;
slouken@390
  1120
slouken@501
  1121
            /* 
slouken@501
  1122
                The first window is always an empty window with level kCGSWindowLevelTop
slouken@501
  1123
                so start at index 1
slouken@501
  1124
            */
slouken@272
  1125
            for (i = 1; i < count; i++) {
slouken@390
  1126
slouken@272
  1127
                /* If we reach our window in the list, it cannot be obscured */
slouken@272
  1128
                if (windows[i] == windowNumber) {
slouken@390
  1129
slouken@272
  1130
                    obscured = SDL_FALSE;
slouken@272
  1131
                    break;
slouken@272
  1132
                }
slouken@272
  1133
                else {
slouken@390
  1134
slouken@272
  1135
                    float shadowSide;
slouken@272
  1136
                    float shadowTop;
slouken@272
  1137
                    float shadowBottom;
slouken@272
  1138
slouken@272
  1139
                    CGSGetWindowLevel (cgsConnection, windows[i], &winLevel);
slouken@390
  1140
slouken@272
  1141
                    if (winLevel == kCGSWindowLevelDockIcon) {
slouken@390
  1142
slouken@272
  1143
                        int j;
slouken@390
  1144
slouken@272
  1145
                        if (firstDockIcon < 0) {
slouken@390
  1146
slouken@272
  1147
                            firstDockIcon = i;
slouken@390
  1148
slouken@272
  1149
                            if (numCachedDockIcons > 0) {
slouken@390
  1150
slouken@272
  1151
                                for (j = 0; j < numCachedDockIcons; j++) {
slouken@390
  1152
slouken@272
  1153
                                    if (windows[i] == dockIcons[j])
slouken@272
  1154
                                        i++;
slouken@272
  1155
                                    else
slouken@272
  1156
                                        break;
slouken@272
  1157
                                }
slouken@390
  1158
slouken@272
  1159
                                if (j != 0) {
slouken@390
  1160
slouken@272
  1161
                                    i--;
slouken@390
  1162
slouken@272
  1163
                                    if (j < numCachedDockIcons) {
slouken@390
  1164
slouken@272
  1165
                                        dockIconCacheMiss = SDL_TRUE;
slouken@272
  1166
                                    }
slouken@272
  1167
                                }
slouken@272
  1168
slouken@272
  1169
                            }
slouken@272
  1170
                        }
slouken@390
  1171
slouken@272
  1172
                        continue;
slouken@272
  1173
                    }
slouken@272
  1174
                    else if (winLevel == kCGSWindowLevelMenuIgnore
slouken@272
  1175
                             /* winLevel == kCGSWindowLevelTop */) {
slouken@390
  1176
slouken@272
  1177
                        continue; /* cannot obscure window */
slouken@272
  1178
                    }
slouken@272
  1179
                    else if (winLevel == kCGSWindowLevelDockMenu ||
slouken@272
  1180
                             winLevel == kCGSWindowLevelMenu) {
slouken@390
  1181
slouken@272
  1182
                        shadowSide = 18;
slouken@272
  1183
                        shadowTop = 4;
slouken@390
  1184
                        shadowBottom = 22;
slouken@272
  1185
                    }
slouken@272
  1186
                    else if (winLevel == kCGSWindowLevelUtility) {
slouken@390
  1187
slouken@272
  1188
                        shadowSide = 8;
slouken@272
  1189
                        shadowTop = 4;
slouken@272
  1190
                        shadowBottom = 12;
slouken@272
  1191
                    }
slouken@272
  1192
                    else if (winLevel == kCGSWindowLevelNormal) {
slouken@390
  1193
slouken@501
  1194
                        /* 
slouken@501
  1195
                            These numbers are for foreground windows,
slouken@501
  1196
                            they are too big (but will work) for background windows 
slouken@501
  1197
                        */
slouken@272
  1198
                        shadowSide = 20;
slouken@272
  1199
                        shadowTop = 10;
slouken@272
  1200
                        shadowBottom = 24;
slouken@272
  1201
                    }
slouken@272
  1202
                    else if (winLevel == kCGSWindowLevelDock) {
slouken@390
  1203
slouken@272
  1204
                        /* Create dock icon cache */
slouken@272
  1205
                        if (numCachedDockIcons != (i-firstDockIcon) ||
slouken@272
  1206
                            dockIconCacheMiss) {
slouken@390
  1207
slouken@272
  1208
                            numCachedDockIcons = i - firstDockIcon;
slouken@390
  1209
                            memcpy (dockIcons, &(windows[firstDockIcon]),
slouken@272
  1210
                                    numCachedDockIcons * sizeof(*windows));
slouken@272
  1211
                        }
slouken@390
  1212
slouken@272
  1213
                        /* no shadow */
slouken@272
  1214
                        shadowSide = 0;
slouken@272
  1215
                        shadowTop = 0;
slouken@272
  1216
                        shadowBottom = 0;
slouken@272
  1217
                    }
slouken@272
  1218
                    else {
slouken@390
  1219
slouken@501
  1220
                        /*
slouken@501
  1221
                            kCGSWindowLevelDockLabel,
slouken@501
  1222
                            kCGSWindowLevelDock,
slouken@501
  1223
                            kOther???
slouken@501
  1224
                        */
slouken@390
  1225
slouken@272
  1226
                        /* no shadow */
slouken@272
  1227
                        shadowSide = 0;
slouken@272
  1228
                        shadowTop = 0;
slouken@272
  1229
                        shadowBottom = 0;
slouken@272
  1230
                    }
slouken@390
  1231
slouken@272
  1232
                    CGSGetScreenRectForWindow (cgsConnection, windows[i], &winRect);
slouken@390
  1233
slouken@272
  1234
                    winRect.origin.x -= shadowSide;
slouken@272
  1235
                    winRect.origin.y -= shadowTop;
slouken@272
  1236
                    winRect.size.width += shadowSide;
slouken@272
  1237
                    winRect.size.height += shadowBottom;
slouken@390
  1238
slouken@272
  1239
                    if (NSIntersectsRect (contentRect, winRect)) {
slouken@390
  1240
slouken@272
  1241
                        obscured = SDL_TRUE;
slouken@272
  1242
                        break;
slouken@272
  1243
                    }
slouken@390
  1244
slouken@390
  1245
                } /* window was not our window */
slouken@390
  1246
slouken@272
  1247
            } /* iterate over windows */
slouken@390
  1248
slouken@272
  1249
        } /* get cgsConnection */
slouken@390
  1250
slouken@272
  1251
    } /* window is visible */
slouken@272
  1252
    
slouken@272
  1253
    return obscured;
slouken@272
  1254
#else
slouken@272
  1255
    return SDL_TRUE;
slouken@272
  1256
#endif
slouken@272
  1257
}
slouken@272
  1258
slouken@501
  1259
slouken@498
  1260
/* Locking functions for the software window buffer */
slouken@498
  1261
static int QZ_LockWindow (_THIS, SDL_Surface *surface) {
slouken@498
  1262
    
slouken@498
  1263
    return LockPortBits ( [ window_view qdPort ] );
slouken@498
  1264
}
slouken@498
  1265
slouken@498
  1266
static void QZ_UnlockWindow (_THIS, SDL_Surface *surface) {
slouken@498
  1267
slouken@498
  1268
    UnlockPortBits ( [ window_view qdPort ] );
slouken@498
  1269
}
slouken@498
  1270
slouken@761
  1271
/* Resize icon, BMP format */
slouken@761
  1272
static const unsigned char QZ_ResizeIcon[] = {
slouken@761
  1273
    0x42,0x4d,0x31,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
slouken@761
  1274
    0x00,0x00,0x0d,0x00,0x00,0x00,0x0d,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
slouken@761
  1275
    0x00,0x00,0xfb,0x01,0x00,0x00,0x13,0x0b,0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
slouken@761
  1276
    0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1277
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1278
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1279
    0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,
slouken@761
  1280
    0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,
slouken@761
  1281
    0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xda,0xda,0xda,0x87,
slouken@761
  1282
    0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,
slouken@761
  1283
    0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1284
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd5,0xd5,0xd5,0x87,0x87,0x87,0xe8,0xe8,0xe8,
slouken@761
  1285
    0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,
slouken@761
  1286
    0xda,0xda,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1287
    0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,
slouken@761
  1288
    0xda,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1289
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,
slouken@761
  1290
    0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xda,0xda,0xda,0x87,0x87,0x87,0xe8,
slouken@761
  1291
    0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1292
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xd7,0xd7,0xd7,0x87,0x87,0x87,0xe8,0xe8,
slouken@761
  1293
    0xe8,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1294
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1295
    0xff,0xff,0xff,0xd9,0xd9,0xd9,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xdc,
slouken@761
  1296
    0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1297
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,
slouken@761
  1298
    0xdb,0x87,0x87,0x87,0xe8,0xe8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1299
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1300
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdb,0xdb,0xdb,0x87,0x87,0x87,0xe8,
slouken@761
  1301
    0xe8,0xe8,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1302
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1303
    0xff,0xff,0xff,0xff,0xdc,0xdc,0xdc,0x87,0x87,0x87,0xff,0xff,0xff,0x0b,0xff,0xff,
slouken@761
  1304
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1305
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdc,
slouken@761
  1306
    0xdc,0xdc,0xff,0xff,0xff,0x0b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1307
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
slouken@761
  1308
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0b
slouken@761
  1309
};
slouken@761
  1310
slouken@761
  1311
static void QZ_DrawResizeIcon (_THIS, RgnHandle dirtyRegion) {
slouken@761
  1312
slouken@761
  1313
    /* Check if we should draw the resize icon */
slouken@761
  1314
    if (SDL_VideoSurface->flags & SDL_RESIZABLE) {
slouken@761
  1315
    
slouken@761
  1316
        Rect    icon;
slouken@761
  1317
        SetRect (&icon, SDL_VideoSurface->w - 13, SDL_VideoSurface->h - 13, 
slouken@761
  1318
                    SDL_VideoSurface->w, SDL_VideoSurface->h);
slouken@761
  1319
                    
slouken@761
  1320
        if (RectInRgn (&icon, dirtyRegion)) {
slouken@761
  1321
        
slouken@761
  1322
            SDL_Rect icon_rect;
slouken@761
  1323
            
slouken@761
  1324
            /* Create the icon image */
slouken@761
  1325
            if (resize_icon == NULL) {
slouken@761
  1326
            
slouken@761
  1327
                SDL_RWops *rw;
slouken@761
  1328
                SDL_Surface *tmp;
slouken@761
  1329
                
slouken@768
  1330
                rw = SDL_RWFromConstMem (QZ_ResizeIcon, sizeof(QZ_ResizeIcon));
slouken@761
  1331
                tmp = SDL_LoadBMP_RW (rw, SDL_TRUE);
slouken@761
  1332
                                                                
slouken@761
  1333
                resize_icon = SDL_ConvertSurface (tmp, SDL_VideoSurface->format, SDL_SRCCOLORKEY);
slouken@761
  1334
                SDL_SetColorKey (resize_icon, SDL_SRCCOLORKEY, 0xFFFFFF);
slouken@761
  1335
                
slouken@761
  1336
                SDL_FreeSurface (tmp);
slouken@761
  1337
            }
slouken@761
  1338
            
slouken@761
  1339
            icon_rect.x = SDL_VideoSurface->w - 13;
slouken@761
  1340
            icon_rect.y = SDL_VideoSurface->h - 13;
slouken@761
  1341
            icon_rect.w = 13;
slouken@761
  1342
            icon_rect.h = 13;
slouken@761
  1343
            
slouken@761
  1344
            SDL_BlitSurface (resize_icon, NULL, SDL_VideoSurface, &icon_rect);
slouken@761
  1345
        }
slouken@761
  1346
    }
slouken@761
  1347
}
slouken@761
  1348
slouken@390
  1349
static void QZ_UpdateRects (_THIS, int numRects, SDL_Rect *rects) {
slouken@272
  1350
slouken@47
  1351
    if (SDL_VideoSurface->flags & SDL_OPENGLBLIT) {
slouken@47
  1352
        QZ_GL_SwapBuffers (this);
slouken@47
  1353
    }
slouken@501
  1354
    else if ( [ qz_window isMiniaturized ] ) {
slouken@501
  1355
    
slouken@501
  1356
        /* Do nothing if miniaturized */
slouken@272
  1357
    }
slouken@501
  1358
    
slouken@272
  1359
    else if ( ! QZ_IsWindowObscured (qz_window) ) {
slouken@390
  1360
slouken@272
  1361
        /* Use direct copy to flush contents to the display */
slouken@272
  1362
        CGrafPtr savePort;
slouken@272
  1363
        CGrafPtr dstPort, srcPort;
slouken@272
  1364
        const BitMap  *dstBits, *srcBits;
slouken@390
  1365
        Rect     dstRect, srcRect;
slouken@272
  1366
        Point    offset;
slouken@272
  1367
        int i;
slouken@390
  1368
slouken@272
  1369
        GetPort (&savePort);
slouken@390
  1370
slouken@272
  1371
        dstPort = CreateNewPortForCGDisplayID ((UInt32)display_id);
slouken@272
  1372
        srcPort = [ window_view qdPort ];
slouken@390
  1373
slouken@272
  1374
        offset.h = 0;
slouken@272
  1375
        offset.v = 0;
slouken@272
  1376
        SetPort (srcPort);
slouken@272
  1377
        LocalToGlobal (&offset);
slouken@390
  1378
slouken@272
  1379
        SetPort (dstPort);
slouken@390
  1380
slouken@272
  1381
        LockPortBits (dstPort);
slouken@272
  1382
        LockPortBits (srcPort);
slouken@390
  1383
slouken@272
  1384
        dstBits = GetPortBitMapForCopyBits (dstPort);
slouken@272
  1385
        srcBits = GetPortBitMapForCopyBits (srcPort);
slouken@390
  1386
slouken@272
  1387
        for (i = 0; i < numRects; i++) {
slouken@390
  1388
slouken@272
  1389
            SetRect (&srcRect, rects[i].x, rects[i].y,
slouken@272
  1390
                     rects[i].x + rects[i].w,
slouken@272
  1391
                     rects[i].y + rects[i].h);
slouken@390
  1392
slouken@272
  1393
            SetRect (&dstRect,
slouken@390
  1394
                     rects[i].x + offset.h,
slouken@272
  1395
                     rects[i].y + offset.v,
slouken@272
  1396
                     rects[i].x + rects[i].w + offset.h,
slouken@272
  1397
                     rects[i].y + rects[i].h + offset.v);
slouken@390
  1398
slouken@272
  1399
            CopyBits (srcBits, dstBits,
slouken@272
  1400
                      &srcRect, &dstRect, srcCopy, NULL);
slouken@390
  1401
slouken@272
  1402
        }
slouken@390
  1403
slouken@272
  1404
        SetPort (savePort);
slouken@272
  1405
    }
slouken@47
  1406
    else {
slouken@272
  1407
        /* Use QDFlushPortBuffer() to flush content to display */
slouken@47
  1408
        int i;
slouken@47
  1409
        RgnHandle dirty = NewRgn ();
slouken@47
  1410
        RgnHandle temp  = NewRgn ();
slouken@390
  1411
slouken@47
  1412
        SetEmptyRgn (dirty);
slouken@390
  1413
slouken@47
  1414
        /* Build the region of dirty rectangles */
slouken@47
  1415
        for (i = 0; i < numRects; i++) {
slouken@390
  1416
slouken@390
  1417
            MacSetRectRgn (temp, rects[i].x, rects[i].y,
slouken@501
  1418
                        rects[i].x + rects[i].w, rects[i].y + rects[i].h);
slouken@47
  1419
            MacUnionRgn (dirty, temp, dirty);
slouken@47
  1420
        }
slouken@390
  1421
slouken@501
  1422
        QZ_DrawResizeIcon (this, dirty);
slouken@501
  1423
        
slouken@47
  1424
        /* Flush the dirty region */
slouken@272
  1425
        QDFlushPortBuffer ( [ window_view qdPort ], dirty );
slouken@47
  1426
        DisposeRgn (dirty);
slouken@47
  1427
        DisposeRgn (temp);
slouken@47
  1428
    }
slouken@47
  1429
}
slouken@47
  1430
slouken@47
  1431
static void QZ_VideoQuit (_THIS) {
slouken@47
  1432
icculus@1340
  1433
    CGDisplayFadeReservationToken fade_token = kCGDisplayFadeReservationInvalidToken;
icculus@1340
  1434
icculus@560
  1435
    /* Restore gamma settings */
icculus@560
  1436
    CGDisplayRestoreColorSyncSettings ();
icculus@560
  1437
icculus@560
  1438
    /* Ensure the cursor will be visible and working when we quit */
icculus@560
  1439
    CGDisplayShowCursor (display_id);
icculus@560
  1440
    CGAssociateMouseAndMouseCursorPosition (1);
icculus@560
  1441
    
icculus@1340
  1442
    if (mode_flags & SDL_FULLSCREEN) {
icculus@1340
  1443
        /* Fade to black to hide resolution-switching flicker (and garbage
icculus@1340
  1444
           that is displayed by a destroyed OpenGL context, if applicable) */
icculus@1340
  1445
        if (CGAcquireDisplayFadeReservation (5, &fade_token) == kCGErrorSuccess) {
icculus@1340
  1446
            CGDisplayFade (fade_token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0.0, 0.0, 0.0, TRUE);
icculus@1340
  1447
        }
icculus@1340
  1448
        QZ_UnsetVideoMode (this, TRUE);
icculus@1340
  1449
        if (fade_token != kCGDisplayFadeReservationInvalidToken) {
icculus@1340
  1450
            CGDisplayFade (fade_token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0.0, 0.0, 0.0, FALSE);
icculus@1340
  1451
            CGReleaseDisplayFadeReservation (fade_token);
icculus@1340
  1452
        }
icculus@1340
  1453
    }
icculus@1340
  1454
    else
icculus@1340
  1455
        QZ_UnsetVideoMode (this, TRUE);
icculus@1340
  1456
    
slouken@47
  1457
    CGPaletteRelease (palette);
icculus@1181
  1458
icculus@1189
  1459
    if (opengl_library) {
icculus@1189
  1460
        SDL_UnloadObject(opengl_library);
icculus@1189
  1461
        opengl_library = NULL;
icculus@1181
  1462
    }
icculus@1181
  1463
    this->gl_config.driver_loaded = 0;
slouken@47
  1464
}
slouken@47
  1465
slouken@674
  1466
#if 0 /* Not used (apparently, it's really slow) */
slouken@47
  1467
static int  QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color) {
slouken@47
  1468
slouken@47
  1469
    CGSDisplayHWFill (display_id, rect->x, rect->y, rect->w, rect->h, color);
slouken@272
  1470
slouken@47
  1471
    return 0;
slouken@47
  1472
}
slouken@674
  1473
#endif
slouken@47
  1474
slouken@390
  1475
static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface) {
slouken@47
  1476
slouken@47
  1477
    return 1;
slouken@47
  1478
}
slouken@47
  1479
slouken@272
  1480
static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface) {
slouken@272
  1481
slouken@47
  1482
}
slouken@47
  1483
icculus@1120
  1484
static int QZ_AllocHWSurface(_THIS, SDL_Surface *surface) {
icculus@1120
  1485
    return(-1); /* unallowed (no HWSURFACE support here). */
icculus@1120
  1486
}
icculus@1120
  1487
slouken@47
  1488
static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface) {
slouken@47
  1489
}
slouken@47
  1490
slouken@47
  1491
/*
slouken@390
  1492
 int QZ_FlipHWSurface (_THIS, SDL_Surface *surface) {
slouken@390
  1493
     return 0;
slouken@390
  1494
 }
slouken@390
  1495
 */
slouken@47
  1496
slouken@47
  1497
/* Gamma functions */
slouken@761
  1498
int QZ_SetGamma (_THIS, float red, float green, float blue) {
slouken@47
  1499
slouken@47
  1500
    const CGGammaValue min = 0.0, max = 1.0;
slouken@272
  1501
slouken@272
  1502
    if (red == 0.0)
slouken@272
  1503
        red = FLT_MAX;
slouken@272
  1504
    else
slouken@272
  1505
        red = 1.0 / red;
slouken@272
  1506
slouken@272
  1507
    if (green == 0.0)
slouken@272
  1508
        green = FLT_MAX;
slouken@272
  1509
    else
slouken@272
  1510
        green = 1.0 / green;
slouken@272
  1511
slouken@272
  1512
    if (blue == 0.0)
slouken@272
  1513
        blue = FLT_MAX;
slouken@272
  1514
    else
slouken@272
  1515
        blue  = 1.0 / blue;
slouken@390
  1516
slouken@390
  1517
    if ( CGDisplayNoErr == CGSetDisplayTransferByFormula
slouken@390
  1518
         (display_id, min, max, red, min, max, green, min, max, blue) ) {
slouken@390
  1519
slouken@272
  1520
        return 0;
slouken@272
  1521
    }
slouken@272
  1522
    else {
slouken@390
  1523
slouken@47
  1524
        return -1;
slouken@272
  1525
    }
slouken@47
  1526
}
slouken@47
  1527
slouken@761
  1528
int QZ_GetGamma (_THIS, float *red, float *green, float *blue) {
slouken@47
  1529
slouken@47
  1530
    CGGammaValue dummy;
slouken@272
  1531
    if ( CGDisplayNoErr == CGGetDisplayTransferByFormula
slouken@390
  1532
         (display_id, &dummy, &dummy, red,
slouken@390
  1533
          &dummy, &dummy, green, &dummy, &dummy, blue) )
slouken@390
  1534
slouken@272
  1535
        return 0;
slouken@272
  1536
    else
slouken@47
  1537
        return -1;
slouken@47
  1538
}
slouken@47
  1539
slouken@761
  1540
int QZ_SetGammaRamp (_THIS, Uint16 *ramp) {
slouken@390
  1541
slouken@390
  1542
    const CGTableCount tableSize = 255;
slouken@390
  1543
    CGGammaValue redTable[tableSize];
slouken@390
  1544
    CGGammaValue greenTable[tableSize];
slouken@390
  1545
    CGGammaValue blueTable[tableSize];
slouken@390
  1546
slouken@390
  1547
    int i;
slouken@390
  1548
slouken@390
  1549
    /* Extract gamma values into separate tables, convert to floats between 0.0 and 1.0 */
slouken@390
  1550
    for (i = 0; i < 256; i++)
slouken@390
  1551
        redTable[i % 256] = ramp[i] / 65535.0;
slouken@390
  1552
slouken@390
  1553
    for (i=256; i < 512; i++)
slouken@390
  1554
        greenTable[i % 256] = ramp[i] / 65535.0;
slouken@390
  1555
slouken@390
  1556
    for (i=512; i < 768; i++)
slouken@390
  1557
        blueTable[i % 256] = ramp[i] / 65535.0;
slouken@390
  1558
slouken@390
  1559
    if ( CGDisplayNoErr == CGSetDisplayTransferByTable
slouken@390
  1560
         (display_id, tableSize, redTable, greenTable, blueTable) )
slouken@272
  1561
        return 0;
slouken@272
  1562
    else
slouken@47
  1563
        return -1;
slouken@47
  1564
}
slouken@47
  1565
slouken@761
  1566
int QZ_GetGammaRamp (_THIS, Uint16 *ramp) {
slouken@390
  1567
slouken@47
  1568
    const CGTableCount tableSize = 255;
slouken@47
  1569
    CGGammaValue redTable[tableSize];
slouken@47
  1570
    CGGammaValue greenTable[tableSize];
slouken@47
  1571
    CGGammaValue blueTable[tableSize];
slouken@47
  1572
    CGTableCount actual;
slouken@47
  1573
    int i;
slouken@390
  1574
slouken@390
  1575
    if ( CGDisplayNoErr != CGGetDisplayTransferByTable
slouken@390
  1576
         (display_id, tableSize, redTable, greenTable, blueTable, &actual) ||
slouken@390
  1577
         actual != tableSize)
slouken@390
  1578
slouken@47
  1579
        return -1;
slouken@390
  1580
slouken@47
  1581
    /* Pack tables into one array, with values from 0 to 65535 */
slouken@47
  1582
    for (i = 0; i < 256; i++)
slouken@47
  1583
        ramp[i] = redTable[i % 256] * 65535.0;
slouken@390
  1584
slouken@47
  1585
    for (i=256; i < 512; i++)
slouken@47
  1586
        ramp[i] = greenTable[i % 256] * 65535.0;
slouken@390
  1587
slouken@47
  1588
    for (i=512; i < 768; i++)
slouken@47
  1589
        ramp[i] = blueTable[i % 256] * 65535.0;
slouken@390
  1590
slouken@390
  1591
    return 0;
slouken@47
  1592
}
slouken@47
  1593