src/video/quartz/SDL_QuartzWM.m
author Ryan C. Gordon
Sat, 03 Aug 2019 16:27:46 -0400
branchSDL-1.2
changeset 12992 0cfa2cc751eb
parent 6224 6f013dd0add1
permissions -rw-r--r--
quartz: Fix cursor transparency.

This patch originally came from:

https://github.com/kanjitalk755/SDL/commit/0296d5e601a5deb5ce2f540a8eafd64dd22dbe69

Fixes Bugzilla #4076.
slouken@47
     1
/*
slouken@47
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012  Sam Lantinga
slouken@47
     4
slouken@47
     5
    This library is free software; you can redistribute it and/or
slouken@47
     6
    modify it under the terms of the GNU Library General Public
slouken@47
     7
    License as published by the Free Software Foundation; either
slouken@47
     8
    version 2 of the License, or (at your option) any later version.
slouken@47
     9
slouken@47
    10
    This library is distributed in the hope that it will be useful,
slouken@47
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@47
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@47
    13
    Library General Public License for more details.
slouken@47
    14
slouken@47
    15
    You should have received a copy of the GNU Library General Public
slouken@47
    16
    License along with this library; if not, write to the Free
slouken@47
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@47
    18
slouken@47
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@47
    21
*/
slouken@1403
    22
#include "SDL_config.h"
slouken@47
    23
slouken@761
    24
#include "SDL_QuartzVideo.h"
slouken@4070
    25
#include "SDL_QuartzWM.h"
icculus@563
    26
slouken@47
    27
slouken@761
    28
void QZ_FreeWMCursor     (_THIS, WMcursor *cursor) { 
slouken@47
    29
slouken@1883
    30
    if ( cursor != NULL ) {
slouken@1883
    31
        [ cursor->nscursor release ];
slouken@47
    32
        free (cursor);
slouken@1883
    33
    }
slouken@47
    34
}
slouken@47
    35
slouken@761
    36
WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask, 
slouken@390
    37
                                         int w, int h, int hot_x, int hot_y) { 
slouken@390
    38
    WMcursor *cursor;
slouken@1883
    39
    NSBitmapImageRep *imgrep;
slouken@1883
    40
    NSImage *img;
slouken@1883
    41
    unsigned char *planes[5];
slouken@1883
    42
    int i;
slouken@1883
    43
    NSAutoreleasePool *pool;
slouken@1883
    44
    
slouken@1883
    45
    pool = [ [ NSAutoreleasePool alloc ] init ];
slouken@1883
    46
    
slouken@390
    47
    /* Allocate the cursor memory */
slouken@1756
    48
    cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
slouken@1883
    49
    if (cursor == NULL) goto outOfMemory;
icculus@5628
    50
slouken@1883
    51
    /* create the image representation and get the pointers to its storage */
icculus@5628
    52
    imgrep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL pixelsWide: w pixelsHigh: h bitsPerSample: 1 samplesPerPixel: 2 hasAlpha: YES isPlanar: YES colorSpaceName: NSDeviceWhiteColorSpace bytesPerRow: (w+7)/8 bitsPerPixel: 0 ] autorelease ];
slouken@1883
    53
    if (imgrep == nil) goto outOfMemory;
slouken@1883
    54
    [ imgrep getBitmapDataPlanes: planes ];
slouken@168
    55
    
slouken@1883
    56
    /* copy data and mask, extending the mask to all black pixels because the inversion effect doesn't work with Cocoa's alpha-blended cursors */
slouken@1883
    57
    for (i = 0; i < (w+7)/8*h; i++) {
icculus@12992
    58
        planes[0][i] = ~data[i] & mask[i];
slouken@1883
    59
        planes[1][i] = mask[i] | data[i];
slouken@1883
    60
    }
icculus@5628
    61
slouken@1883
    62
    /* create image and cursor */
slouken@1883
    63
    img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(w, h) ] autorelease ];
slouken@1883
    64
    if (img == nil) goto outOfMemory;
slouken@1883
    65
    [ img addRepresentation: imgrep ];
slouken@1883
    66
    if (system_version < 0x1030) { /* on 10.2, cursors must be 16*16 */
slouken@1883
    67
        if (w > 16 || h > 16) { /* too big: scale it down */
slouken@1883
    68
            [ img setScalesWhenResized: YES ];
slouken@1883
    69
            hot_x = hot_x*16/w;
slouken@1883
    70
            hot_y = hot_y*16/h;
slouken@1883
    71
        }
slouken@1883
    72
        else { /* too small (or just right): extend it (from the bottom left corner, so hot_y must be adjusted) */
slouken@1883
    73
            hot_y += 16 - h;
slouken@1883
    74
        }
slouken@1883
    75
        [ img setSize: NSMakeSize(16, 16) ];
slouken@390
    76
    }
slouken@1883
    77
    cursor->nscursor = [ [ NSCursor alloc ] initWithImage: img hotSpot: NSMakePoint(hot_x, hot_y) ];
slouken@1883
    78
    if (cursor->nscursor == nil) goto outOfMemory;
slouken@390
    79
    
slouken@1883
    80
    [ pool release ];
slouken@168
    81
    return(cursor);
slouken@1883
    82
slouken@1883
    83
outOfMemory:
slouken@1883
    84
    [ pool release ];
slouken@1883
    85
    if (cursor != NULL) SDL_free(cursor);
slouken@1883
    86
    SDL_OutOfMemory();
slouken@1883
    87
    return(NULL);
slouken@47
    88
}
slouken@47
    89
slouken@4070
    90
void QZ_UpdateCursor (_THIS) {
slouken@4070
    91
    BOOL state;
slouken@4070
    92
slouken@4070
    93
    if (cursor_should_be_visible || !(SDL_GetAppState() & SDL_APPMOUSEFOCUS)) {
slouken@4070
    94
        state = YES;
slouken@4070
    95
    } else {
slouken@4070
    96
        state = NO;
slouken@761
    97
    }
slouken@4070
    98
    if (state != cursor_visible) {
slouken@4070
    99
        if (state) {
slouken@4070
   100
            [ NSCursor unhide ];
slouken@4070
   101
        } else {
slouken@4070
   102
            [ NSCursor hide ];
slouken@4070
   103
        }
slouken@4070
   104
        cursor_visible = state;
slouken@761
   105
    }
slouken@761
   106
}
slouken@761
   107
slouken@779
   108
BOOL QZ_IsMouseInWindow (_THIS) {
slouken@4065
   109
    if (qz_window == nil || (mode_flags & SDL_FULLSCREEN)) return YES; /*fullscreen*/
icculus@1192
   110
    else {
icculus@1192
   111
        NSPoint p = [ qz_window mouseLocationOutsideOfEventStream ];
icculus@1192
   112
        p.y -= 1.0f; /* Apparently y goes from 1 to h, not from 0 to h-1 (i.e. the "location of the mouse" seems to be defined as "the location of the top left corner of the mouse pointer's hot pixel" */
icculus@1192
   113
        return NSPointInRect(p, [ window_view frame ]);
icculus@1192
   114
    }
slouken@779
   115
}
slouken@779
   116
slouken@761
   117
int QZ_ShowWMCursor (_THIS, WMcursor *cursor) { 
slouken@47
   118
slouken@47
   119
    if ( cursor == NULL) {
slouken@761
   120
        if ( cursor_should_be_visible ) {
slouken@761
   121
            cursor_should_be_visible = NO;
icculus@563
   122
            QZ_ChangeGrabState (this, QZ_HIDECURSOR);
slouken@47
   123
        }
slouken@4070
   124
        QZ_UpdateCursor(this);
slouken@47
   125
    }
slouken@47
   126
    else {
icculus@6121
   127
        if ( qz_window != nil && !(mode_flags & SDL_FULLSCREEN) ) {
slouken@4070
   128
            [ qz_window invalidateCursorRectsForView: [ qz_window contentView ] ];
slouken@4070
   129
        }
slouken@761
   130
        if ( ! cursor_should_be_visible ) {
slouken@761
   131
            cursor_should_be_visible = YES;
icculus@563
   132
            QZ_ChangeGrabState (this, QZ_SHOWCURSOR);
slouken@47
   133
        }
icculus@6121
   134
        [ cursor->nscursor performSelectorOnMainThread:@selector(set) withObject:nil waitUntilDone:NO ];
slouken@4070
   135
        QZ_UpdateCursor(this);
slouken@47
   136
    }
slouken@47
   137
slouken@47
   138
    return 1;
slouken@47
   139
}
slouken@47
   140
slouken@501
   141
/*
slouken@501
   142
    Coordinate conversion functions, for convenience
slouken@501
   143
    Cocoa sets the origin at the lower left corner of the window/screen
slouken@501
   144
    SDL, CoreGraphics/WindowServer, and QuickDraw use the origin at the upper left corner
slouken@501
   145
    The routines were written so they could be called before SetVideoMode() has finished;
slouken@501
   146
    this might have limited usefulness at the moment, but the extra cost is trivial.
slouken@501
   147
*/
slouken@272
   148
slouken@272
   149
/* Convert Cocoa screen coordinate to Cocoa window coordinate */
slouken@761
   150
void QZ_PrivateGlobalToLocal (_THIS, NSPoint *p) {
slouken@47
   151
slouken@4392
   152
	if ( ! CGDisplayIsCaptured (display_id) )
slouken@4392
   153
		*p = [ qz_window convertScreenToBase:*p ];
slouken@272
   154
}
slouken@272
   155
slouken@272
   156
slouken@272
   157
/* Convert Cocoa window coordinate to Cocoa screen coordinate */
slouken@761
   158
void QZ_PrivateLocalToGlobal (_THIS, NSPoint *p) {
slouken@272
   159
slouken@4392
   160
	if ( ! CGDisplayIsCaptured (display_id) )
slouken@4392
   161
		*p = [ qz_window convertBaseToScreen:*p ];
slouken@272
   162
}
slouken@272
   163
slouken@272
   164
/* Convert SDL coordinate to Cocoa coordinate */
slouken@761
   165
void QZ_PrivateSDLToCocoa (_THIS, NSPoint *p) {
slouken@272
   166
slouken@272
   167
    if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
slouken@272
   168
    
icculus@876
   169
        p->y = CGDisplayPixelsHigh (display_id) - p->y;
slouken@272
   170
    }
slouken@272
   171
    else {
slouken@934
   172
       
slouken@934
   173
        *p = [ window_view convertPoint:*p toView: nil ];
icculus@4204
   174
        p->y = [window_view frame].size.height - p->y;
slouken@272
   175
    }
slouken@272
   176
}
slouken@272
   177
slouken@272
   178
/* Convert Cocoa coordinate to SDL coordinate */
slouken@761
   179
void QZ_PrivateCocoaToSDL (_THIS, NSPoint *p) {
slouken@272
   180
slouken@683
   181
    if ( CGDisplayIsCaptured (display_id) ) { /* capture signals fullscreen */
slouken@683
   182
    
icculus@876
   183
        p->y = CGDisplayPixelsHigh (display_id) - p->y;
slouken@683
   184
    }
slouken@683
   185
    else {
slouken@934
   186
slouken@934
   187
        *p = [ window_view convertPoint:*p fromView: nil ];
icculus@4204
   188
        p->y = [window_view frame].size.height - p->y;
slouken@683
   189
    }
slouken@272
   190
}
slouken@272
   191
slouken@272
   192
/* Convert SDL coordinate to window server (CoreGraphics) coordinate */
slouken@761
   193
CGPoint QZ_PrivateSDLToCG (_THIS, NSPoint *p) {
slouken@272
   194
    
slouken@272
   195
    CGPoint cgp;
slouken@272
   196
    
slouken@272
   197
    if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
slouken@272
   198
    
slouken@272
   199
        int height;
slouken@272
   200
        
slouken@272
   201
        QZ_PrivateSDLToCocoa (this, p);
slouken@272
   202
        QZ_PrivateLocalToGlobal (this, p);
slouken@272
   203
        
slouken@272
   204
        height = CGDisplayPixelsHigh (display_id);
slouken@272
   205
        p->y = height - p->y;
slouken@272
   206
    }
slouken@272
   207
    
slouken@272
   208
    cgp.x = p->x;
slouken@272
   209
    cgp.y = p->y;
slouken@272
   210
    
slouken@272
   211
    return cgp;
slouken@272
   212
}
slouken@272
   213
slouken@683
   214
#if 0 /* Dead code */
slouken@272
   215
/* Convert window server (CoreGraphics) coordinate to SDL coordinate */
slouken@761
   216
void QZ_PrivateCGToSDL (_THIS, NSPoint *p) {
slouken@272
   217
            
slouken@272
   218
    if ( ! CGDisplayIsCaptured (display_id) ) { /* not captured => not fullscreen => local coord */
slouken@272
   219
    
slouken@272
   220
        int height;
slouken@272
   221
slouken@272
   222
        /* Convert CG Global to Cocoa Global */
slouken@272
   223
        height = CGDisplayPixelsHigh (display_id);
slouken@272
   224
        p->y = height - p->y;
slouken@272
   225
slouken@272
   226
        QZ_PrivateGlobalToLocal (this, p);
slouken@272
   227
        QZ_PrivateCocoaToSDL (this, p);
slouken@47
   228
    }
slouken@47
   229
}
slouken@683
   230
#endif /* Dead code */
slouken@47
   231
slouken@761
   232
void  QZ_PrivateWarpCursor (_THIS, int x, int y) {
slouken@272
   233
    NSPoint p;
slouken@272
   234
    CGPoint cgp;
slouken@47
   235
    
slouken@272
   236
    p = NSMakePoint (x, y);
icculus@563
   237
    cgp = QZ_PrivateSDLToCG (this, &p);
icculus@563
   238
    
icculus@563
   239
    /* this is the magic call that fixes cursor "freezing" after warp */
icculus@6224
   240
    CGAssociateMouseAndMouseCursorPosition (0);
icculus@563
   241
    CGWarpMouseCursorPosition (cgp);
icculus@6224
   242
    if (grab_state != QZ_INVISIBLE_GRAB) { /* can't leave it disassociated? */
icculus@6224
   243
        CGAssociateMouseAndMouseCursorPosition (1);
icculus@6224
   244
    }
icculus@6087
   245
    SDL_PrivateAppActive (QZ_IsMouseInWindow (this), SDL_APPMOUSEFOCUS);
slouken@272
   246
}
slouken@272
   247
slouken@761
   248
void QZ_WarpWMCursor (_THIS, Uint16 x, Uint16 y) {
slouken@272
   249
slouken@47
   250
    /* Only allow warping when in foreground */
slouken@761
   251
    if ( ! [ NSApp isActive ] )
slouken@47
   252
        return;
slouken@47
   253
            
slouken@47
   254
    /* Do the actual warp */
icculus@1207
   255
    if (grab_state != QZ_INVISIBLE_GRAB) QZ_PrivateWarpCursor (this, x, y);
icculus@563
   256
icculus@563
   257
    /* Generate the mouse moved event */
icculus@563
   258
    SDL_PrivateMouseMotion (0, 0, x, y);
slouken@47
   259
}
slouken@47
   260
slouken@761
   261
void QZ_MoveWMCursor     (_THIS, int x, int y) { }
slouken@761
   262
void QZ_CheckMouseMode   (_THIS) { }
slouken@47
   263
slouken@761
   264
void QZ_SetCaption    (_THIS, const char *title, const char *icon) {
slouken@47
   265
slouken@158
   266
    if ( qz_window != nil ) {
slouken@58
   267
        NSString *string;
slouken@58
   268
        if ( title != NULL ) {
slouken@951
   269
            string = [ [ NSString alloc ] initWithUTF8String:title ];
slouken@158
   270
            [ qz_window setTitle:string ];
slouken@58
   271
            [ string release ];
slouken@58
   272
        }
slouken@58
   273
        if ( icon != NULL ) {
slouken@951
   274
            string = [ [ NSString alloc ] initWithUTF8String:icon ];
slouken@158
   275
            [ qz_window setMiniwindowTitle:string ];
slouken@58
   276
            [ string release ];
slouken@58
   277
        }
slouken@58
   278
    }
slouken@47
   279
}
slouken@47
   280
slouken@761
   281
void QZ_SetIcon       (_THIS, SDL_Surface *icon, Uint8 *mask)
slouken@269
   282
{
slouken@501
   283
    NSBitmapImageRep *imgrep;
slouken@501
   284
    NSImage *img;
slouken@501
   285
    SDL_Surface *mergedSurface;
slouken@501
   286
    NSAutoreleasePool *pool;
slouken@1812
   287
    Uint8 *pixels;
slouken@1812
   288
    SDL_bool iconSrcAlpha;
slouken@1812
   289
    Uint8 iconAlphaValue;
slouken@1812
   290
    int i, j, maskPitch, index;
slouken@501
   291
    
slouken@501
   292
    pool = [ [ NSAutoreleasePool alloc ] init ];
slouken@1812
   293
    
slouken@1812
   294
    imgrep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL pixelsWide: icon->w pixelsHigh: icon->h bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar: NO colorSpaceName: NSDeviceRGBColorSpace bytesPerRow: 4*icon->w bitsPerPixel: 32 ] autorelease ];
slouken@1812
   295
    if (imgrep == nil) goto freePool;
slouken@1812
   296
    pixels = [ imgrep bitmapData ];
slouken@1812
   297
    SDL_memset(pixels, 0, 4*icon->w*icon->h); /* make the background, which will survive in colorkeyed areas, completely transparent */
slouken@501
   298
    
slouken@1812
   299
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1812
   300
#define BYTEORDER_DEPENDENT_RGBA_MASKS 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
slouken@1812
   301
#else
slouken@1812
   302
#define BYTEORDER_DEPENDENT_RGBA_MASKS 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
slouken@1812
   303
#endif
slouken@1812
   304
    mergedSurface = SDL_CreateRGBSurfaceFrom(pixels, icon->w, icon->h, 32, 4*icon->w, BYTEORDER_DEPENDENT_RGBA_MASKS);
slouken@1812
   305
    if (mergedSurface == NULL) goto freePool;
slouken@501
   306
    
slouken@1812
   307
    /* blit, with temporarily cleared SRCALPHA flag because we want to copy, not alpha-blend */
slouken@1812
   308
    iconSrcAlpha = ((icon->flags & SDL_SRCALPHA) != 0);
slouken@1812
   309
    iconAlphaValue = icon->format->alpha;
slouken@1812
   310
    SDL_SetAlpha(icon, 0, 255);
slouken@1812
   311
    SDL_BlitSurface(icon, NULL, mergedSurface, NULL);
slouken@1812
   312
    if (iconSrcAlpha) SDL_SetAlpha(icon, SDL_SRCALPHA, iconAlphaValue);
slouken@592
   313
    
slouken@1812
   314
    SDL_FreeSurface(mergedSurface);
slouken@501
   315
    
slouken@1812
   316
    /* apply mask, source alpha, and premultiply color values by alpha */
slouken@1812
   317
    maskPitch = (icon->w+7)/8;
slouken@1812
   318
    for (i = 0; i < icon->h; i++) {
slouken@1812
   319
        for (j = 0; j < icon->w; j++) {
slouken@1812
   320
            index = i*4*icon->w + j*4;
slouken@1812
   321
            if (!(mask[i*maskPitch + j/8] & (128 >> j%8))) {
slouken@1812
   322
                pixels[index + 3] = 0;
slouken@1812
   323
            }
slouken@1812
   324
            else {
slouken@1812
   325
                if (iconSrcAlpha) {
slouken@1812
   326
                    if (icon->format->Amask == 0) pixels[index + 3] = icon->format->alpha;
slouken@1812
   327
                }
slouken@1812
   328
                else {
slouken@1812
   329
                    pixels[index + 3] = 255;
slouken@1812
   330
                }
slouken@1812
   331
            }
slouken@1812
   332
            if (pixels[index + 3] < 255) {
slouken@1812
   333
                pixels[index + 0] = (Uint16)pixels[index + 0]*pixels[index + 3]/255;
slouken@1812
   334
                pixels[index + 1] = (Uint16)pixels[index + 1]*pixels[index + 3]/255;
slouken@1812
   335
                pixels[index + 2] = (Uint16)pixels[index + 2]*pixels[index + 3]/255;
slouken@592
   336
            }
slouken@592
   337
        }
slouken@501
   338
    }
slouken@501
   339
    
slouken@1812
   340
    img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(icon->w, icon->h) ] autorelease ];
slouken@1812
   341
    if (img == nil) goto freePool;
slouken@501
   342
    [ img addRepresentation: imgrep ];
slouken@501
   343
    [ NSApp setApplicationIconImage:img ];
slouken@501
   344
    
slouken@269
   345
freePool:
slouken@1812
   346
    [ pool release ];
slouken@47
   347
}
slouken@47
   348
slouken@761
   349
int  QZ_IconifyWindow (_THIS) { 
slouken@47
   350
slouken@158
   351
    if ( ! [ qz_window isMiniaturized ] ) {
slouken@158
   352
        [ qz_window miniaturize:nil ];
slouken@4122
   353
        if ( ! [ qz_window isMiniaturized ] ) {
slouken@4122
   354
            SDL_SetError ("window iconification failed");
slouken@4122
   355
            return 0;
slouken@4122
   356
        }
slouken@47
   357
        return 1;
slouken@47
   358
    }
slouken@47
   359
    else {
slouken@501
   360
        SDL_SetError ("window already iconified");
slouken@47
   361
        return 0;
slouken@47
   362
    }
slouken@47
   363
}
slouken@47
   364
slouken@47
   365
/*
slouken@761
   366
int  QZ_GetWMInfo  (_THIS, SDL_SysWMinfo *info) { 
slouken@158
   367
    info->nsWindowPtr = qz_window;
slouken@47
   368
    return 0; 
slouken@47
   369
}*/
slouken@47
   370
slouken@761
   371
void QZ_ChangeGrabState (_THIS, int action) {
icculus@563
   372
icculus@563
   373
    /* 
icculus@563
   374
        Figure out what the next state should be based on the action.
icculus@563
   375
        Ignore actions that can't change the current state.
icculus@563
   376
    */
icculus@563
   377
    if ( grab_state == QZ_UNGRABBED ) {
icculus@563
   378
        if ( action == QZ_ENABLE_GRAB ) {
slouken@761
   379
            if ( cursor_should_be_visible )
icculus@563
   380
                grab_state = QZ_VISIBLE_GRAB;
icculus@563
   381
            else
icculus@563
   382
                grab_state = QZ_INVISIBLE_GRAB;
icculus@563
   383
        }
icculus@563
   384
    }
icculus@563
   385
    else if ( grab_state == QZ_VISIBLE_GRAB ) {
icculus@563
   386
        if ( action == QZ_DISABLE_GRAB )
icculus@563
   387
            grab_state = QZ_UNGRABBED;
icculus@563
   388
        else if ( action == QZ_HIDECURSOR )
icculus@563
   389
            grab_state = QZ_INVISIBLE_GRAB;
icculus@563
   390
    }
icculus@563
   391
    else {
icculus@563
   392
        assert( grab_state == QZ_INVISIBLE_GRAB );
icculus@563
   393
        
icculus@563
   394
        if ( action == QZ_DISABLE_GRAB )
icculus@563
   395
            grab_state = QZ_UNGRABBED;
icculus@563
   396
        else if ( action == QZ_SHOWCURSOR )
icculus@563
   397
            grab_state = QZ_VISIBLE_GRAB;
icculus@563
   398
    }
icculus@563
   399
    
icculus@563
   400
    /* now apply the new state */
icculus@563
   401
    if (grab_state == QZ_UNGRABBED) {
icculus@563
   402
    
icculus@563
   403
        CGAssociateMouseAndMouseCursorPosition (1);
icculus@563
   404
    }
icculus@563
   405
    else if (grab_state == QZ_VISIBLE_GRAB) {
icculus@563
   406
    
icculus@563
   407
        CGAssociateMouseAndMouseCursorPosition (1);
icculus@563
   408
    }
icculus@563
   409
    else {
icculus@563
   410
        assert( grab_state == QZ_INVISIBLE_GRAB );
icculus@563
   411
icculus@563
   412
        QZ_PrivateWarpCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2);
icculus@563
   413
        CGAssociateMouseAndMouseCursorPosition (0);
icculus@563
   414
    }
icculus@563
   415
}
icculus@563
   416
slouken@761
   417
SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode) {
slouken@47
   418
icculus@563
   419
    int doGrab = grab_mode & SDL_GRAB_ON;
icculus@563
   420
    /*int fullscreen = grab_mode & SDL_GRAB_FULLSCREEN;*/
icculus@563
   421
icculus@563
   422
    if ( this->screen == NULL ) {
icculus@563
   423
        SDL_SetError ("QZ_GrabInput: screen is NULL");
icculus@563
   424
        return SDL_GRAB_OFF;
slouken@47
   425
    }
slouken@47
   426
        
icculus@563
   427
    if ( ! video_set ) {
icculus@563
   428
        /*SDL_SetError ("QZ_GrabInput: video is not set, grab will take effect on mode switch"); */
icculus@563
   429
        current_grab_mode = grab_mode;
icculus@563
   430
        return grab_mode;       /* Will be set later on mode switch */
icculus@563
   431
    }
icculus@563
   432
icculus@563
   433
    if ( grab_mode != SDL_GRAB_QUERY ) {
icculus@563
   434
        if ( doGrab )
icculus@563
   435
            QZ_ChangeGrabState (this, QZ_ENABLE_GRAB);
icculus@563
   436
        else
icculus@563
   437
            QZ_ChangeGrabState (this, QZ_DISABLE_GRAB);
icculus@563
   438
        
icculus@563
   439
        current_grab_mode = doGrab ? SDL_GRAB_ON : SDL_GRAB_OFF;
icculus@6150
   440
        QZ_UpdateCursor(this);
icculus@563
   441
    }
icculus@563
   442
slouken@501
   443
    return current_grab_mode;
slouken@47
   444
}