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