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