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