21 #include "../../SDL_internal.h" 23 #if SDL_VIDEO_DRIVER_COCOA 30 #include "../../events/SDL_mouse_c.h" 34 #ifdef DEBUG_COCOAMOUSE 35 #define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__) 37 #define DLog(...) do { } while (0) 46 static unsigned char cursorBytes[] = {
47 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
49 0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
50 0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
51 0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
54 NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
55 length:sizeof(cursorBytes)
57 NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
68 Cocoa_CreateDefaultCursor()
74 nscursor = [NSCursor arrowCursor];
88 Cocoa_CreateCursor(
SDL_Surface * surface,
int hot_x,
int hot_y)
92 NSCursor *nscursor =
NULL;
97 nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
116 NSCursor *nscursor =
NULL;
121 nscursor = [NSCursor arrowCursor];
124 nscursor = [NSCursor IBeamCursor];
127 nscursor = [NSCursor arrowCursor];
130 nscursor = [NSCursor crosshairCursor];
133 nscursor = [NSCursor arrowCursor];
137 nscursor = [NSCursor closedHandCursor];
140 nscursor = [NSCursor resizeLeftRightCursor];
143 nscursor = [NSCursor resizeUpDownCursor];
146 nscursor = [NSCursor closedHandCursor];
149 nscursor = [NSCursor operationNotAllowedCursor];
152 nscursor = [NSCursor pointingHandCursor];
175 NSCursor *nscursor = (NSCursor *)cursor->
driverdata;
187 for (; window !=
NULL; window = window->
next) {
190 [driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
191 withObject:[driverdata->nswindow contentView]
199 SDL_FindWindowAtPoint(
const int x,
const int y)
214 Cocoa_WarpMouseGlobal(
int x,
int y)
220 DLog(
"Postponing warp, window being moved.");
221 [data->listener setPendingMoveX:x Y:y];
225 const CGPoint point = CGPointMake((
float)x, (
float)y);
233 CGSetLocalEventsSuppressionInterval(0.0);
234 CGWarpMouseCursorPosition(point);
235 CGSetLocalEventsSuppressionInterval(0.25);
241 SDL_Window *win = SDL_FindWindowAtPoint(x, y);
253 Cocoa_WarpMouse(
SDL_Window * window,
int x,
int y)
255 Cocoa_WarpMouseGlobal(x + window->
x, y + window->
y);
280 result = CGAssociateMouseAndMouseCursorPosition(NO);
282 DLog(
"Turning off.");
283 result = CGAssociateMouseAndMouseCursorPosition(YES);
285 if (result != kCGErrorSuccess) {
286 return SDL_SetError(
"CGAssociateMouseAndMouseCursorPosition() failed");
309 Cocoa_GetGlobalMouseState(
int *x,
int *y)
311 const NSUInteger cocoaButtons = [NSEvent pressedMouseButtons];
312 const NSPoint cocoaLocation = [NSEvent mouseLocation];
315 for (NSScreen *
screen in [NSScreen screens]) {
316 NSRect
frame = [screen frame];
317 if (NSPointInRect(cocoaLocation, frame)) {
318 *x = (int) cocoaLocation.x;
319 *y = (
int) ((frame.origin.y + frame.
size.
height) - cocoaLocation.y);
355 const NSPoint
location = [NSEvent mouseLocation];
363 switch ([event
type]) {
365 case NSLeftMouseDragged:
366 case NSRightMouseDragged:
367 case NSOtherMouseDragged:
384 const NSPoint
location = [NSEvent mouseLocation];
385 const CGFloat lastMoveX = driverdata->
lastMoveX;
386 const CGFloat lastMoveY = driverdata->
lastMoveY;
389 DLog(
"Last seen mouse: (%g, %g)", location.x, location.y);
397 if ([event window]) {
398 NSRect windowRect = [[[event window] contentView] frame];
399 if (!NSPointInRect([event locationInWindow], windowRect)) {
404 float deltaX = [event deltaX];
405 float deltaY = [event deltaY];
408 deltaX += (lastMoveX - driverdata->lastWarpX);
409 deltaY += ((CGDisplayPixelsHigh(kCGDirectMainDisplay) - lastMoveY) - driverdata->lastWarpY);
411 DLog(
"Motion was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], deltaX, deltaY);
422 float x = -[event deltaX];
423 float y = [event deltaY];
426 if ([event respondsToSelector:
@selector(isDirectionInvertedFromDevice)]) {
427 if ([event isDirectionInvertedFromDevice] == YES) {
456 DLog(
"(%g, %g)", x, y);
void Cocoa_InitMouse(_THIS)
GLdouble GLdouble GLdouble r
SDL_Mouse * SDL_GetMouse(void)
int(* ShowCursor)(SDL_Cursor *cursor)
uint32_t Uint32
An unsigned 32-bit integer type.
int(* SetRelativeMouseMode)(SDL_bool enabled)
#define SDL_BUTTON_X2MASK
The structure that defines a point.
A collection of pixels used in software blitting.
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Uint32(* GetGlobalMouseState)(int *x, int *y)
SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
Returns true if point resides inside a rectangle.
void SDL_SetMouseFocus(SDL_Window *window)
void Cocoa_HandleMouseWarp(CGFloat x, CGFloat y)
GLint GLint GLint GLint GLint x
int(* CaptureMouse)(SDL_Window *window)
SDL_Cursor *(* CreateCursor)(SDL_Surface *surface, int hot_x, int hot_y)
void * SDL_calloc(size_t nmemb, size_t size)
void Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
GLint GLint GLsizei GLsizei height
NSImage * Cocoa_CreateImage(SDL_Surface *surface)
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
void Cocoa_QuitMouse(_THIS)
Cocoa_WindowListener * listener
#define SDL_BUTTON_X1MASK
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor.
NSCursor * invisibleCursor()
void Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
void Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
void SDL_SetDefaultCursor(SDL_Cursor *cursor)
int(* WarpMouseGlobal)(int x, int y)
GLenum GLenum GLsizei const GLuint GLboolean enabled
void(* FreeCursor)(SDL_Cursor *cursor)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
#define SDL_assert(condition)
#define SDL_GetMouseFocus
GLint GLint GLint GLint GLint GLint y
The type used to identify a window.
void(* WarpMouse)(SDL_Window *window, int x, int y)
SDL_VideoDevice * SDL_GetVideoDevice(void)
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction)
GLuint GLuint GLsizei GLenum type
SDL_Cursor *(* CreateSystemCursor)(SDL_SystemCursor id)
SDL_MouseWheelDirection
Scroll direction types for the Scroll event.
void Cocoa_InitMouseEventTap(SDL_MouseData *driverdata)
A rectangle, with the origin at the upper left.