slouken@1931
|
1 |
/*
|
slouken@5535
|
2 |
Simple DirectMedia Layer
|
slouken@6885
|
3 |
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
|
slouken@1931
|
4 |
|
slouken@5535
|
5 |
This software is provided 'as-is', without any express or implied
|
slouken@5535
|
6 |
warranty. In no event will the authors be held liable for any damages
|
slouken@5535
|
7 |
arising from the use of this software.
|
slouken@1931
|
8 |
|
slouken@5535
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
slouken@5535
|
10 |
including commercial applications, and to alter it and redistribute it
|
slouken@5535
|
11 |
freely, subject to the following restrictions:
|
slouken@1931
|
12 |
|
slouken@5535
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
slouken@5535
|
14 |
claim that you wrote the original software. If you use this software
|
slouken@5535
|
15 |
in a product, an acknowledgment in the product documentation would be
|
slouken@5535
|
16 |
appreciated but is not required.
|
slouken@5535
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
slouken@5535
|
18 |
misrepresented as being the original software.
|
slouken@5535
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
slouken@1931
|
20 |
*/
|
slouken@1931
|
21 |
#include "SDL_config.h"
|
slouken@1931
|
22 |
|
slouken@6044
|
23 |
#if SDL_VIDEO_DRIVER_COCOA
|
slouken@6044
|
24 |
|
slouken@6676
|
25 |
#include "SDL_assert.h"
|
slouken@3517
|
26 |
#include "SDL_events.h"
|
jorgen@7114
|
27 |
#include "SDL_cocoamouse.h"
|
slouken@1931
|
28 |
|
slouken@1931
|
29 |
#include "../../events/SDL_mouse_c.h"
|
slouken@1931
|
30 |
|
jorgen@7158
|
31 |
@implementation NSCursor (InvisibleCursor)
|
jorgen@7158
|
32 |
+ (NSCursor *)invisibleCursor
|
jorgen@7158
|
33 |
{
|
jorgen@7158
|
34 |
static NSCursor *invisibleCursor = NULL;
|
jorgen@7158
|
35 |
if (!invisibleCursor) {
|
jorgen@7158
|
36 |
/* RAW 16x16 transparent GIF */
|
jorgen@7158
|
37 |
static unsigned char cursorBytes[] = {
|
jorgen@7158
|
38 |
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
|
jorgen@7158
|
39 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
|
jorgen@7158
|
40 |
0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
|
jorgen@7158
|
41 |
0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
|
jorgen@7158
|
42 |
0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
|
jorgen@7158
|
43 |
};
|
jorgen@7158
|
44 |
|
jorgen@7158
|
45 |
NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
|
jorgen@7158
|
46 |
length:sizeof(cursorBytes)
|
jorgen@7158
|
47 |
freeWhenDone:NO];
|
jorgen@7158
|
48 |
NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
|
jorgen@7158
|
49 |
invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
|
jorgen@7158
|
50 |
hotSpot:NSZeroPoint];
|
jorgen@7158
|
51 |
}
|
jorgen@7158
|
52 |
|
jorgen@7158
|
53 |
return invisibleCursor;
|
jorgen@7158
|
54 |
}
|
jorgen@7158
|
55 |
@end
|
jorgen@7158
|
56 |
|
slouken@5376
|
57 |
|
slouken@5376
|
58 |
static SDL_Cursor *
|
slouken@5376
|
59 |
Cocoa_CreateDefaultCursor()
|
slouken@5376
|
60 |
{
|
slouken@6848
|
61 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
slouken@6848
|
62 |
NSCursor *nscursor;
|
slouken@6848
|
63 |
SDL_Cursor *cursor = NULL;
|
slouken@5376
|
64 |
|
slouken@6848
|
65 |
nscursor = [NSCursor arrowCursor];
|
slouken@5376
|
66 |
|
slouken@6848
|
67 |
if (nscursor) {
|
slouken@6848
|
68 |
cursor = SDL_calloc(1, sizeof(*cursor));
|
slouken@6848
|
69 |
if (cursor) {
|
slouken@6848
|
70 |
cursor->driverdata = nscursor;
|
slouken@6848
|
71 |
[nscursor retain];
|
slouken@5376
|
72 |
}
|
slouken@5376
|
73 |
}
|
slouken@6848
|
74 |
|
slouken@6848
|
75 |
[pool release];
|
slouken@6848
|
76 |
|
slouken@6848
|
77 |
return cursor;
|
slouken@5376
|
78 |
}
|
slouken@5376
|
79 |
|
slouken@5376
|
80 |
static SDL_Cursor *
|
slouken@5376
|
81 |
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
|
slouken@5376
|
82 |
{
|
slouken@6848
|
83 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
slouken@6848
|
84 |
NSImage *nsimage;
|
slouken@6848
|
85 |
NSCursor *nscursor = NULL;
|
slouken@6848
|
86 |
SDL_Cursor *cursor = NULL;
|
slouken@5376
|
87 |
|
slouken@6848
|
88 |
nsimage = Cocoa_CreateImage(surface);
|
slouken@6848
|
89 |
if (nsimage) {
|
slouken@6848
|
90 |
nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
|
slouken@6848
|
91 |
}
|
slouken@6848
|
92 |
|
slouken@6848
|
93 |
if (nscursor) {
|
slouken@6848
|
94 |
cursor = SDL_calloc(1, sizeof(*cursor));
|
slouken@6848
|
95 |
if (cursor) {
|
slouken@6848
|
96 |
cursor->driverdata = nscursor;
|
alexey@6832
|
97 |
}
|
slouken@6848
|
98 |
}
|
alexey@6832
|
99 |
|
slouken@6848
|
100 |
[pool release];
|
slouken@6848
|
101 |
|
slouken@6848
|
102 |
return cursor;
|
slouken@5376
|
103 |
}
|
slouken@5376
|
104 |
|
slouken@6676
|
105 |
static SDL_Cursor *
|
slouken@6676
|
106 |
Cocoa_CreateSystemCursor(SDL_SystemCursor id)
|
slouken@6676
|
107 |
{
|
slouken@6848
|
108 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
slouken@6848
|
109 |
NSCursor *nscursor = NULL;
|
slouken@6848
|
110 |
SDL_Cursor *cursor = NULL;
|
slouken@6676
|
111 |
|
slouken@6848
|
112 |
switch(id)
|
slouken@6848
|
113 |
{
|
slouken@6848
|
114 |
case SDL_SYSTEM_CURSOR_ARROW:
|
slouken@6848
|
115 |
nscursor = [NSCursor arrowCursor];
|
slouken@6848
|
116 |
break;
|
slouken@6848
|
117 |
case SDL_SYSTEM_CURSOR_IBEAM:
|
slouken@6848
|
118 |
nscursor = [NSCursor IBeamCursor];
|
slouken@6848
|
119 |
break;
|
slouken@6848
|
120 |
case SDL_SYSTEM_CURSOR_WAIT:
|
slouken@6848
|
121 |
nscursor = [NSCursor arrowCursor];
|
slouken@6848
|
122 |
break;
|
slouken@6848
|
123 |
case SDL_SYSTEM_CURSOR_CROSSHAIR:
|
slouken@6848
|
124 |
nscursor = [NSCursor crosshairCursor];
|
slouken@6848
|
125 |
break;
|
slouken@6848
|
126 |
case SDL_SYSTEM_CURSOR_WAITARROW:
|
slouken@6848
|
127 |
nscursor = [NSCursor arrowCursor];
|
slouken@6848
|
128 |
break;
|
slouken@6848
|
129 |
case SDL_SYSTEM_CURSOR_SIZENWSE:
|
slouken@6848
|
130 |
case SDL_SYSTEM_CURSOR_SIZENESW:
|
slouken@6848
|
131 |
nscursor = [NSCursor closedHandCursor];
|
slouken@6848
|
132 |
break;
|
slouken@6848
|
133 |
case SDL_SYSTEM_CURSOR_SIZEWE:
|
slouken@6848
|
134 |
nscursor = [NSCursor resizeLeftRightCursor];
|
slouken@6848
|
135 |
break;
|
slouken@6848
|
136 |
case SDL_SYSTEM_CURSOR_SIZENS:
|
slouken@6848
|
137 |
nscursor = [NSCursor resizeUpDownCursor];
|
slouken@6848
|
138 |
break;
|
slouken@6848
|
139 |
case SDL_SYSTEM_CURSOR_SIZEALL:
|
slouken@6848
|
140 |
nscursor = [NSCursor closedHandCursor];
|
slouken@6848
|
141 |
break;
|
slouken@6848
|
142 |
case SDL_SYSTEM_CURSOR_NO:
|
slouken@6848
|
143 |
nscursor = [NSCursor operationNotAllowedCursor];
|
slouken@6848
|
144 |
break;
|
slouken@6848
|
145 |
case SDL_SYSTEM_CURSOR_HAND:
|
slouken@6848
|
146 |
nscursor = [NSCursor pointingHandCursor];
|
slouken@6848
|
147 |
break;
|
slouken@6848
|
148 |
default:
|
slouken@6848
|
149 |
SDL_assert(!"Unknown system cursor");
|
slouken@6848
|
150 |
return NULL;
|
slouken@6848
|
151 |
}
|
slouken@6848
|
152 |
|
slouken@6848
|
153 |
if (nscursor) {
|
slouken@6848
|
154 |
cursor = SDL_calloc(1, sizeof(*cursor));
|
slouken@6848
|
155 |
if (cursor) {
|
slouken@7191
|
156 |
/* We'll free it later, so retain it here */
|
slouken@6848
|
157 |
[nscursor retain];
|
slouken@6848
|
158 |
cursor->driverdata = nscursor;
|
alexey@6832
|
159 |
}
|
slouken@6848
|
160 |
}
|
alexey@6832
|
161 |
|
slouken@6848
|
162 |
[pool release];
|
slouken@6848
|
163 |
|
slouken@6848
|
164 |
return cursor;
|
slouken@6676
|
165 |
}
|
slouken@6676
|
166 |
|
slouken@5376
|
167 |
static void
|
slouken@5376
|
168 |
Cocoa_FreeCursor(SDL_Cursor * cursor)
|
slouken@5376
|
169 |
{
|
slouken@6848
|
170 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
slouken@6848
|
171 |
NSCursor *nscursor = (NSCursor *)cursor->driverdata;
|
slouken@5376
|
172 |
|
slouken@6848
|
173 |
[nscursor release];
|
slouken@6848
|
174 |
SDL_free(cursor);
|
slouken@6848
|
175 |
|
slouken@6848
|
176 |
[pool release];
|
slouken@5376
|
177 |
}
|
slouken@5376
|
178 |
|
slouken@5376
|
179 |
static int
|
slouken@5376
|
180 |
Cocoa_ShowCursor(SDL_Cursor * cursor)
|
slouken@5376
|
181 |
{
|
slouken@6848
|
182 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
slouken@5376
|
183 |
|
jorgen@7158
|
184 |
SDL_VideoDevice *device = SDL_GetVideoDevice();
|
jorgen@7158
|
185 |
SDL_Window *window = (device ? device->windows : NULL);
|
jorgen@7158
|
186 |
for (; window != NULL; window = window->next) {
|
jorgen@7158
|
187 |
SDL_WindowData *driverdata = (SDL_WindowData *)window->driverdata;
|
jorgen@7158
|
188 |
if (driverdata) {
|
jorgen@7158
|
189 |
[driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
|
jorgen@7158
|
190 |
withObject:[driverdata->nswindow contentView]
|
jorgen@7158
|
191 |
waitUntilDone:NO];
|
jorgen@7158
|
192 |
}
|
slouken@5376
|
193 |
}
|
slouken@5376
|
194 |
|
slouken@6848
|
195 |
[pool release];
|
slouken@6848
|
196 |
|
slouken@5376
|
197 |
return 0;
|
slouken@5376
|
198 |
}
|
slouken@5376
|
199 |
|
slouken@5376
|
200 |
static void
|
slouken@5376
|
201 |
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
|
slouken@5376
|
202 |
{
|
jorgen@7106
|
203 |
SDL_Mouse *mouse = SDL_GetMouse();
|
slouken@5376
|
204 |
CGPoint point;
|
slouken@5376
|
205 |
|
slouken@5391
|
206 |
point.x = (float)window->x + x;
|
slouken@5391
|
207 |
point.y = (float)window->y + y;
|
jorgen@7113
|
208 |
|
jorgen@7114
|
209 |
{
|
jorgen@7114
|
210 |
/* This makes Cocoa_HandleMouseEvent ignore this delta in the next
|
jorgen@7114
|
211 |
* movement event.
|
jorgen@7114
|
212 |
*/
|
jorgen@7114
|
213 |
SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
|
jorgen@7114
|
214 |
NSPoint location = [NSEvent mouseLocation];
|
jorgen@7114
|
215 |
driverdata->deltaXOffset = location.x - point.x;
|
jorgen@7114
|
216 |
driverdata->deltaYOffset = point.y - location.y;
|
jorgen@7114
|
217 |
}
|
jorgen@7114
|
218 |
|
jorgen@7113
|
219 |
/* According to the docs, this was deprecated in 10.6, but it's still
|
jorgen@7113
|
220 |
* around. The substitute requires a CGEventSource, but I'm not entirely
|
jorgen@7113
|
221 |
* sure how we'd procure the right one for this event.
|
jorgen@7113
|
222 |
*/
|
jorgen@7113
|
223 |
CGSetLocalEventsSuppressionInterval(0.0);
|
slouken@5376
|
224 |
CGWarpMouseCursorPosition(point);
|
jorgen@7113
|
225 |
CGSetLocalEventsSuppressionInterval(0.25);
|
jorgen@7106
|
226 |
|
jorgen@7271
|
227 |
if (!mouse->relative_mode) {
|
jorgen@7271
|
228 |
/* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
|
jorgen@7271
|
229 |
* other implementations' APIs.
|
jorgen@7271
|
230 |
*/
|
jorgen@7271
|
231 |
SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
|
jorgen@7271
|
232 |
}
|
slouken@5376
|
233 |
}
|
slouken@5376
|
234 |
|
slouken@5406
|
235 |
static int
|
slouken@5406
|
236 |
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
|
slouken@5406
|
237 |
{
|
slouken@5406
|
238 |
CGError result;
|
slouken@5406
|
239 |
|
slouken@5406
|
240 |
if (enabled) {
|
slouken@5406
|
241 |
result = CGAssociateMouseAndMouseCursorPosition(NO);
|
slouken@5406
|
242 |
} else {
|
slouken@5406
|
243 |
result = CGAssociateMouseAndMouseCursorPosition(YES);
|
slouken@5406
|
244 |
}
|
slouken@5406
|
245 |
if (result != kCGErrorSuccess) {
|
icculus@7037
|
246 |
return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
|
slouken@5406
|
247 |
}
|
slouken@5406
|
248 |
return 0;
|
slouken@5406
|
249 |
}
|
slouken@5406
|
250 |
|
slouken@1931
|
251 |
void
|
slouken@1931
|
252 |
Cocoa_InitMouse(_THIS)
|
slouken@1931
|
253 |
{
|
slouken@5376
|
254 |
SDL_Mouse *mouse = SDL_GetMouse();
|
slouken@5376
|
255 |
|
jorgen@7114
|
256 |
mouse->driverdata = SDL_calloc(1, sizeof(SDL_MouseData));
|
jorgen@7114
|
257 |
|
slouken@5376
|
258 |
mouse->CreateCursor = Cocoa_CreateCursor;
|
slouken@6676
|
259 |
mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
|
slouken@5376
|
260 |
mouse->ShowCursor = Cocoa_ShowCursor;
|
slouken@5406
|
261 |
mouse->FreeCursor = Cocoa_FreeCursor;
|
slouken@5376
|
262 |
mouse->WarpMouse = Cocoa_WarpMouse;
|
slouken@5406
|
263 |
mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
|
slouken@5376
|
264 |
|
slouken@5405
|
265 |
SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
|
slouken@1931
|
266 |
}
|
slouken@1931
|
267 |
|
slouken@3517
|
268 |
void
|
slouken@3517
|
269 |
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
|
slouken@3517
|
270 |
{
|
slouken@5406
|
271 |
SDL_Mouse *mouse = SDL_GetMouse();
|
slouken@5406
|
272 |
|
slouken@5477
|
273 |
if (mouse->relative_mode &&
|
slouken@5477
|
274 |
([event type] == NSMouseMoved ||
|
slouken@5477
|
275 |
[event type] == NSLeftMouseDragged ||
|
slouken@5477
|
276 |
[event type] == NSRightMouseDragged ||
|
slouken@5477
|
277 |
[event type] == NSOtherMouseDragged)) {
|
jorgen@7114
|
278 |
SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
|
jorgen@7114
|
279 |
float x = [event deltaX] + driverdata->deltaXOffset;
|
jorgen@7114
|
280 |
float y = [event deltaY] + driverdata->deltaYOffset;
|
jorgen@7114
|
281 |
driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
|
jorgen@7114
|
282 |
|
slouken@6950
|
283 |
SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
|
slouken@5406
|
284 |
}
|
slouken@3517
|
285 |
}
|
slouken@3517
|
286 |
|
slouken@1931
|
287 |
void
|
gzjjgod@5057
|
288 |
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
|
gzjjgod@5057
|
289 |
{
|
slouken@6950
|
290 |
SDL_Mouse *mouse = SDL_GetMouse();
|
slouken@6950
|
291 |
|
gzjjgod@5057
|
292 |
float x = [event deltaX];
|
gzjjgod@5057
|
293 |
float y = [event deltaY];
|
gzjjgod@5057
|
294 |
|
gzjjgod@5057
|
295 |
if (x > 0) {
|
gzjjgod@5057
|
296 |
x += 0.9f;
|
gzjjgod@5057
|
297 |
} else if (x < 0) {
|
gzjjgod@5057
|
298 |
x -= 0.9f;
|
gzjjgod@5057
|
299 |
}
|
gzjjgod@5057
|
300 |
if (y > 0) {
|
gzjjgod@5057
|
301 |
y += 0.9f;
|
gzjjgod@5057
|
302 |
} else if (y < 0) {
|
gzjjgod@5057
|
303 |
y -= 0.9f;
|
gzjjgod@5057
|
304 |
}
|
slouken@6950
|
305 |
SDL_SendMouseWheel(window, mouse->mouseID, (int)x, (int)y);
|
gzjjgod@5057
|
306 |
}
|
gzjjgod@5057
|
307 |
|
slouken@5058
|
308 |
void
|
slouken@5058
|
309 |
Cocoa_QuitMouse(_THIS)
|
slouken@5058
|
310 |
{
|
jorgen@7114
|
311 |
SDL_Mouse *mouse = SDL_GetMouse();
|
jorgen@7114
|
312 |
if (mouse) {
|
jorgen@7114
|
313 |
SDL_free(mouse->driverdata);
|
jorgen@7114
|
314 |
}
|
slouken@5058
|
315 |
}
|
slouken@5058
|
316 |
|
slouken@6044
|
317 |
#endif /* SDL_VIDEO_DRIVER_COCOA */
|
slouken@6044
|
318 |
|
slouken@1931
|
319 |
/* vi: set ts=4 sw=4 expandtab: */
|