21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_COCOA
31 #include "../../events/SDL_mouse_c.h"
35 #ifdef DEBUG_COCOAMOUSE
36 #define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
38 #define DLog(...) do { } while (0)
47 static unsigned char cursorBytes[] = {
48 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
50 0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
51 0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
52 0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
55 NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
56 length:sizeof(cursorBytes)
58 NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
69 Cocoa_CreateDefaultCursor()
75 nscursor = [NSCursor arrowCursor];
93 NSCursor *nscursor =
NULL;
98 nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
117 NSCursor *nscursor =
NULL;
122 nscursor = [NSCursor arrowCursor];
125 nscursor = [NSCursor IBeamCursor];
128 nscursor = [NSCursor arrowCursor];
131 nscursor = [NSCursor crosshairCursor];
134 nscursor = [NSCursor arrowCursor];
138 nscursor = [NSCursor closedHandCursor];
141 nscursor = [NSCursor resizeLeftRightCursor];
144 nscursor = [NSCursor resizeUpDownCursor];
147 nscursor = [NSCursor closedHandCursor];
150 nscursor = [NSCursor operationNotAllowedCursor];
153 nscursor = [NSCursor pointingHandCursor];
191 [driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
192 withObject:[driverdata->nswindow contentView]
200 SDL_FindWindowAtPoint(
const int x,
const int y)
215 Cocoa_WarpMouseGlobal(
int x,
int y)
220 if ([
data->listener isMoving]) {
221 DLog(
"Postponing warp, window being moved.");
222 [data->listener setPendingMoveX:x Y:y];
226 const CGPoint point = CGPointMake((
float)
x, (
float)
y);
230 CGWarpMouseCursorPosition(point);
237 CGAssociateMouseAndMouseCursorPosition(YES);
276 if ([
data->listener isMoving]) {
283 result = CGAssociateMouseAndMouseCursorPosition(NO);
285 DLog(
"Turning off.");
286 result = CGAssociateMouseAndMouseCursorPosition(YES);
288 if (
result != kCGErrorSuccess) {
289 return SDL_SetError(
"CGAssociateMouseAndMouseCursorPosition() failed");
312 Cocoa_GetGlobalMouseState(
int *
x,
int *
y)
314 const NSUInteger cocoaButtons = [NSEvent pressedMouseButtons];
315 const NSPoint cocoaLocation = [NSEvent mouseLocation];
318 for (NSScreen *
screen in [NSScreen screens]) {
319 NSRect
frame = [screen frame];
320 if (NSMouseInRect(cocoaLocation,
frame, NO)) {
321 *
x = (int) cocoaLocation.x;
358 const NSPoint
location = [NSEvent mouseLocation];
367 case NSEventTypeMouseMoved:
368 case NSEventTypeLeftMouseDragged:
369 case NSEventTypeRightMouseDragged:
370 case NSEventTypeOtherMouseDragged:
387 const NSPoint
location = [NSEvent mouseLocation];
388 const CGFloat lastMoveX = driverdata->
lastMoveX;
389 const CGFloat lastMoveY = driverdata->
lastMoveY;
401 NSRect windowRect = [[[event window] contentView] frame];
402 if (!NSMouseInRect([
event locationInWindow], windowRect, NO)) {
407 float deltaX = [event deltaX];
408 float deltaY = [event deltaY];
411 deltaX += (lastMoveX - driverdata->lastWarpX);
412 deltaY += ((CGDisplayPixelsHigh(kCGDirectMainDisplay) - lastMoveY) - driverdata->lastWarpY);
414 DLog(
"Motion was (%g, %g), offset to (%g, %g)", [
event deltaX], [
event deltaY], deltaX, deltaY);
425 CGFloat
x = -[
event deltaX];
426 CGFloat
y = [event deltaY];
429 if ([
event respondsToSelector:
@selector(isDirectionInvertedFromDevice)]) {
430 if ([
event isDirectionInvertedFromDevice] == YES) {
459 DLog(
"(%g, %g)",
x,
y);