src/video/quartz/SDL_QuartzVideo.m
author Ryan C. Gordon <icculus@icculus.org>
Fri, 06 Apr 2007 20:30:41 +0000
branchSDL-1.2
changeset 3936 c5c3c772f5aa
parent 3877 81f66f258d77
child 4049 60f677630282
permissions -rw-r--r--
Let app set SDL_VIDEO_ALLOW_SCREENSAVER environment variable to override SDL's
attempt to disable screen savers. Works for Quartz (Mac OS X) and X11.

Need a formal API for this in 1.3, still.

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