2 Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "SDL_config.h"
23 #if SDL_VIDEO_DRIVER_COCOA
25 #include "SDL_events.h"
26 #include "SDL_cocoavideo.h"
28 #include "../../events/SDL_mouse_c.h"
32 Cocoa_CreateDefaultCursor()
34 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
36 SDL_Cursor *cursor = NULL;
38 nscursor = [NSCursor arrowCursor];
41 cursor = SDL_calloc(1, sizeof(*cursor));
43 cursor->driverdata = nscursor;
54 Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
56 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
58 NSCursor *nscursor = NULL;
59 SDL_Cursor *cursor = NULL;
61 nsimage = Cocoa_CreateImage(surface);
63 nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
67 cursor = SDL_calloc(1, sizeof(*cursor));
69 cursor->driverdata = nscursor;
79 Cocoa_FreeCursor(SDL_Cursor * cursor)
81 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
82 NSCursor *nscursor = (NSCursor *)cursor->driverdata;
91 Cocoa_ShowCursor(SDL_Cursor * cursor)
93 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
95 if (SDL_GetMouseFocus()) {
97 NSCursor *nscursor = (NSCursor *)cursor->driverdata;
112 Cocoa_WarpMouse(SDL_Window * window, int x, int y)
116 point.x = (float)window->x + x;
117 point.y = (float)window->y + y;
118 CGWarpMouseCursorPosition(point);
122 Cocoa_SetRelativeMouseMode(SDL_bool enabled)
127 result = CGAssociateMouseAndMouseCursorPosition(NO);
129 result = CGAssociateMouseAndMouseCursorPosition(YES);
131 if (result != kCGErrorSuccess) {
132 SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
139 Cocoa_InitMouse(_THIS)
141 SDL_Mouse *mouse = SDL_GetMouse();
143 mouse->CreateCursor = Cocoa_CreateCursor;
144 mouse->ShowCursor = Cocoa_ShowCursor;
145 mouse->FreeCursor = Cocoa_FreeCursor;
146 mouse->WarpMouse = Cocoa_WarpMouse;
147 mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
149 SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
153 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
155 SDL_Mouse *mouse = SDL_GetMouse();
157 if (mouse->relative_mode &&
158 ([event type] == NSMouseMoved ||
159 [event type] == NSLeftMouseDragged ||
160 [event type] == NSRightMouseDragged ||
161 [event type] == NSOtherMouseDragged)) {
162 float x = [event deltaX];
163 float y = [event deltaY];
164 SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
169 Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
171 float x = [event deltaX];
172 float y = [event deltaY];
184 SDL_SendMouseWheel(window, (int)x, (int)y);
188 Cocoa_QuitMouse(_THIS)
192 #endif /* SDL_VIDEO_DRIVER_COCOA */
194 /* vi: set ts=4 sw=4 expandtab: */