SDL_cocoamouse.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_COCOA
  20. #include "SDL_assert.h"
  21. #include "SDL_events.h"
  22. #include "SDL_cocoamouse.h"
  23. #include "SDL_cocoamousetap.h"
  24. #include "SDL_cocoavideo.h"
  25. #include "../../events/SDL_mouse_c.h"
  26. /* #define DEBUG_COCOAMOUSE */
  27. #ifdef DEBUG_COCOAMOUSE
  28. #define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
  29. #else
  30. #define DLog(...) do { } while (0)
  31. #endif
  32. @implementation NSCursor (InvisibleCursor)
  33. + (NSCursor *)invisibleCursor
  34. {
  35. static NSCursor *invisibleCursor = NULL;
  36. if (!invisibleCursor) {
  37. /* RAW 16x16 transparent GIF */
  38. static unsigned char cursorBytes[] = {
  39. 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
  41. 0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
  42. 0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
  43. 0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
  44. };
  45. NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
  46. length:sizeof(cursorBytes)
  47. freeWhenDone:NO];
  48. NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
  49. invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
  50. hotSpot:NSZeroPoint];
  51. }
  52. return invisibleCursor;
  53. }
  54. @end
  55. static SDL_Cursor *
  56. Cocoa_CreateDefaultCursor()
  57. { @autoreleasepool
  58. {
  59. NSCursor *nscursor;
  60. SDL_Cursor *cursor = NULL;
  61. nscursor = [NSCursor arrowCursor];
  62. if (nscursor) {
  63. cursor = SDL_calloc(1, sizeof(*cursor));
  64. if (cursor) {
  65. cursor->driverdata = nscursor;
  66. [nscursor retain];
  67. }
  68. }
  69. return cursor;
  70. }}
  71. static SDL_Cursor *
  72. Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
  73. { @autoreleasepool
  74. {
  75. NSImage *nsimage;
  76. NSCursor *nscursor = NULL;
  77. SDL_Cursor *cursor = NULL;
  78. nsimage = Cocoa_CreateImage(surface);
  79. if (nsimage) {
  80. nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
  81. }
  82. if (nscursor) {
  83. cursor = SDL_calloc(1, sizeof(*cursor));
  84. if (cursor) {
  85. cursor->driverdata = nscursor;
  86. } else {
  87. [nscursor release];
  88. }
  89. }
  90. return cursor;
  91. }}
  92. static SDL_Cursor *
  93. Cocoa_CreateSystemCursor(SDL_SystemCursor id)
  94. { @autoreleasepool
  95. {
  96. NSCursor *nscursor = NULL;
  97. SDL_Cursor *cursor = NULL;
  98. switch(id) {
  99. case SDL_SYSTEM_CURSOR_ARROW:
  100. nscursor = [NSCursor arrowCursor];
  101. break;
  102. case SDL_SYSTEM_CURSOR_IBEAM:
  103. nscursor = [NSCursor IBeamCursor];
  104. break;
  105. case SDL_SYSTEM_CURSOR_WAIT:
  106. nscursor = [NSCursor arrowCursor];
  107. break;
  108. case SDL_SYSTEM_CURSOR_CROSSHAIR:
  109. nscursor = [NSCursor crosshairCursor];
  110. break;
  111. case SDL_SYSTEM_CURSOR_WAITARROW:
  112. nscursor = [NSCursor arrowCursor];
  113. break;
  114. case SDL_SYSTEM_CURSOR_SIZENWSE:
  115. case SDL_SYSTEM_CURSOR_SIZENESW:
  116. nscursor = [NSCursor closedHandCursor];
  117. break;
  118. case SDL_SYSTEM_CURSOR_SIZEWE:
  119. nscursor = [NSCursor resizeLeftRightCursor];
  120. break;
  121. case SDL_SYSTEM_CURSOR_SIZENS:
  122. nscursor = [NSCursor resizeUpDownCursor];
  123. break;
  124. case SDL_SYSTEM_CURSOR_SIZEALL:
  125. nscursor = [NSCursor closedHandCursor];
  126. break;
  127. case SDL_SYSTEM_CURSOR_NO:
  128. nscursor = [NSCursor operationNotAllowedCursor];
  129. break;
  130. case SDL_SYSTEM_CURSOR_HAND:
  131. nscursor = [NSCursor pointingHandCursor];
  132. break;
  133. default:
  134. SDL_assert(!"Unknown system cursor");
  135. return NULL;
  136. }
  137. if (nscursor) {
  138. cursor = SDL_calloc(1, sizeof(*cursor));
  139. if (cursor) {
  140. /* We'll free it later, so retain it here */
  141. [nscursor retain];
  142. cursor->driverdata = nscursor;
  143. }
  144. }
  145. return cursor;
  146. }}
  147. static void
  148. Cocoa_FreeCursor(SDL_Cursor * cursor)
  149. { @autoreleasepool
  150. {
  151. NSCursor *nscursor = (NSCursor *)cursor->driverdata;
  152. [nscursor release];
  153. SDL_free(cursor);
  154. }}
  155. static int
  156. Cocoa_ShowCursor(SDL_Cursor * cursor)
  157. { @autoreleasepool
  158. {
  159. SDL_VideoDevice *device = SDL_GetVideoDevice();
  160. SDL_Window *window = (device ? device->windows : NULL);
  161. for (; window != NULL; window = window->next) {
  162. SDL_WindowData *driverdata = (SDL_WindowData *)window->driverdata;
  163. if (driverdata) {
  164. [driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
  165. withObject:[driverdata->nswindow contentView]
  166. waitUntilDone:NO];
  167. }
  168. }
  169. return 0;
  170. }}
  171. static SDL_Window *
  172. SDL_FindWindowAtPoint(const int x, const int y)
  173. {
  174. const SDL_Point pt = { x, y };
  175. SDL_Window *i;
  176. for (i = SDL_GetVideoDevice()->windows; i; i = i->next) {
  177. const SDL_Rect r = { i->x, i->y, i->w, i->h };
  178. if (SDL_PointInRect(&pt, &r)) {
  179. return i;
  180. }
  181. }
  182. return NULL;
  183. }
  184. static int
  185. Cocoa_WarpMouseGlobal(int x, int y)
  186. {
  187. SDL_Mouse *mouse = SDL_GetMouse();
  188. if (mouse->focus) {
  189. SDL_WindowData *data = (SDL_WindowData *) mouse->focus->driverdata;
  190. if ([data->listener isMoving]) {
  191. DLog("Postponing warp, window being moved.");
  192. [data->listener setPendingMoveX:x Y:y];
  193. return 0;
  194. }
  195. }
  196. const CGPoint point = CGPointMake((float)x, (float)y);
  197. Cocoa_HandleMouseWarp(point.x, point.y);
  198. CGWarpMouseCursorPosition(point);
  199. /* CGWarpMouse causes a short delay by default, which is preventable by
  200. * Calling this directly after. CGSetLocalEventsSuppressionInterval can also
  201. * prevent it, but it's deprecated as of OS X 10.6.
  202. */
  203. if (!mouse->relative_mode) {
  204. CGAssociateMouseAndMouseCursorPosition(YES);
  205. }
  206. /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
  207. * other implementations' APIs. Send what's appropriate.
  208. */
  209. if (!mouse->relative_mode) {
  210. SDL_Window *win = SDL_FindWindowAtPoint(x, y);
  211. SDL_SetMouseFocus(win);
  212. if (win) {
  213. SDL_assert(win == mouse->focus);
  214. SDL_SendMouseMotion(win, mouse->mouseID, 0, x - win->x, y - win->y);
  215. }
  216. }
  217. return 0;
  218. }
  219. static void
  220. Cocoa_WarpMouse(SDL_Window * window, int x, int y)
  221. {
  222. Cocoa_WarpMouseGlobal(x + window->x, y + window->y);
  223. }
  224. static int
  225. Cocoa_SetRelativeMouseMode(SDL_bool enabled)
  226. {
  227. /* We will re-apply the relative mode when the window gets focus, if it
  228. * doesn't have focus right now.
  229. */
  230. SDL_Window *window = SDL_GetMouseFocus();
  231. if (!window) {
  232. return 0;
  233. }
  234. /* We will re-apply the relative mode when the window finishes being moved,
  235. * if it is being moved right now.
  236. */
  237. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  238. if ([data->listener isMoving]) {
  239. return 0;
  240. }
  241. CGError result;
  242. if (enabled) {
  243. DLog("Turning on.");
  244. result = CGAssociateMouseAndMouseCursorPosition(NO);
  245. } else {
  246. DLog("Turning off.");
  247. result = CGAssociateMouseAndMouseCursorPosition(YES);
  248. }
  249. if (result != kCGErrorSuccess) {
  250. return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
  251. }
  252. /* The hide/unhide calls are redundant most of the time, but they fix
  253. * https://bugzilla.libsdl.org/show_bug.cgi?id=2550
  254. */
  255. if (enabled) {
  256. [NSCursor hide];
  257. } else {
  258. [NSCursor unhide];
  259. }
  260. return 0;
  261. }
  262. static int
  263. Cocoa_CaptureMouse(SDL_Window *window)
  264. {
  265. /* our Cocoa event code already tracks the mouse outside the window,
  266. so all we have to do here is say "okay" and do what we always do. */
  267. return 0;
  268. }
  269. static Uint32
  270. Cocoa_GetGlobalMouseState(int *x, int *y)
  271. {
  272. const NSUInteger cocoaButtons = [NSEvent pressedMouseButtons];
  273. const NSPoint cocoaLocation = [NSEvent mouseLocation];
  274. Uint32 retval = 0;
  275. *x = (int) cocoaLocation.x;
  276. *y = (int) (CGDisplayPixelsHigh(kCGDirectMainDisplay) - cocoaLocation.y);
  277. retval |= (cocoaButtons & (1 << 0)) ? SDL_BUTTON_LMASK : 0;
  278. retval |= (cocoaButtons & (1 << 1)) ? SDL_BUTTON_RMASK : 0;
  279. retval |= (cocoaButtons & (1 << 2)) ? SDL_BUTTON_MMASK : 0;
  280. retval |= (cocoaButtons & (1 << 3)) ? SDL_BUTTON_X1MASK : 0;
  281. retval |= (cocoaButtons & (1 << 4)) ? SDL_BUTTON_X2MASK : 0;
  282. return retval;
  283. }
  284. void
  285. Cocoa_InitMouse(_THIS)
  286. {
  287. SDL_Mouse *mouse = SDL_GetMouse();
  288. mouse->driverdata = SDL_calloc(1, sizeof(SDL_MouseData));
  289. mouse->CreateCursor = Cocoa_CreateCursor;
  290. mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
  291. mouse->ShowCursor = Cocoa_ShowCursor;
  292. mouse->FreeCursor = Cocoa_FreeCursor;
  293. mouse->WarpMouse = Cocoa_WarpMouse;
  294. mouse->WarpMouseGlobal = Cocoa_WarpMouseGlobal;
  295. mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
  296. mouse->CaptureMouse = Cocoa_CaptureMouse;
  297. mouse->GetGlobalMouseState = Cocoa_GetGlobalMouseState;
  298. SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
  299. Cocoa_InitMouseEventTap(mouse->driverdata);
  300. const NSPoint location = [NSEvent mouseLocation];
  301. mouse->driverdata->lastMoveX = location.x;
  302. mouse->driverdata->lastMoveY = location.y;
  303. }
  304. void
  305. Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
  306. {
  307. switch ([event type]) {
  308. case NSEventTypeMouseMoved:
  309. case NSEventTypeLeftMouseDragged:
  310. case NSEventTypeRightMouseDragged:
  311. case NSEventTypeOtherMouseDragged:
  312. break;
  313. default:
  314. /* Ignore any other events. */
  315. return;
  316. }
  317. SDL_Mouse *mouse = SDL_GetMouse();
  318. SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
  319. if (!driverdata) {
  320. return; /* can happen when returning from fullscreen Space on shutdown */
  321. }
  322. SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
  323. if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
  324. if (mouse->touch_mouse_events) {
  325. mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
  326. } else {
  327. return; /* no hint set, drop this one. */
  328. }
  329. }
  330. const SDL_bool seenWarp = driverdata->seenWarp;
  331. driverdata->seenWarp = NO;
  332. const NSPoint location = [NSEvent mouseLocation];
  333. const CGFloat lastMoveX = driverdata->lastMoveX;
  334. const CGFloat lastMoveY = driverdata->lastMoveY;
  335. driverdata->lastMoveX = location.x;
  336. driverdata->lastMoveY = location.y;
  337. DLog("Last seen mouse: (%g, %g)", location.x, location.y);
  338. /* Non-relative movement is handled in -[Cocoa_WindowListener mouseMoved:] */
  339. if (!mouse->relative_mode) {
  340. return;
  341. }
  342. /* Ignore events that aren't inside the client area (i.e. title bar.) */
  343. if ([event window]) {
  344. NSRect windowRect = [[[event window] contentView] frame];
  345. if (!NSMouseInRect([event locationInWindow], windowRect, NO)) {
  346. return;
  347. }
  348. }
  349. float deltaX = [event deltaX];
  350. float deltaY = [event deltaY];
  351. if (seenWarp) {
  352. deltaX += (lastMoveX - driverdata->lastWarpX);
  353. deltaY += ((CGDisplayPixelsHigh(kCGDirectMainDisplay) - lastMoveY) - driverdata->lastWarpY);
  354. DLog("Motion was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], deltaX, deltaY);
  355. }
  356. SDL_SendMouseMotion(mouse->focus, mouseID, 1, (int)deltaX, (int)deltaY);
  357. }
  358. void
  359. Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
  360. {
  361. SDL_Mouse *mouse = SDL_GetMouse();
  362. if (!mouse) {
  363. return;
  364. }
  365. SDL_MouseID mouseID = mouse->mouseID;
  366. if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
  367. if (mouse->touch_mouse_events) {
  368. mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
  369. } else {
  370. return; /* no hint set, drop this one. */
  371. }
  372. }
  373. CGFloat x = -[event deltaX];
  374. CGFloat y = [event deltaY];
  375. SDL_MouseWheelDirection direction = SDL_MOUSEWHEEL_NORMAL;
  376. if ([event respondsToSelector:@selector(isDirectionInvertedFromDevice)]) {
  377. if ([event isDirectionInvertedFromDevice] == YES) {
  378. direction = SDL_MOUSEWHEEL_FLIPPED;
  379. }
  380. }
  381. if (x > 0) {
  382. x = SDL_ceil(x);
  383. } else if (x < 0) {
  384. x = SDL_floor(x);
  385. }
  386. if (y > 0) {
  387. y = SDL_ceil(y);
  388. } else if (y < 0) {
  389. y = SDL_floor(y);
  390. }
  391. SDL_SendMouseWheel(window, mouseID, x, y, direction);
  392. }
  393. void
  394. Cocoa_HandleMouseWarp(CGFloat x, CGFloat y)
  395. {
  396. /* This makes Cocoa_HandleMouseEvent ignore the delta caused by the warp,
  397. * since it gets included in the next movement event.
  398. */
  399. SDL_MouseData *driverdata = (SDL_MouseData*)SDL_GetMouse()->driverdata;
  400. driverdata->lastWarpX = x;
  401. driverdata->lastWarpY = y;
  402. driverdata->seenWarp = SDL_TRUE;
  403. DLog("(%g, %g)", x, y);
  404. }
  405. void
  406. Cocoa_QuitMouse(_THIS)
  407. {
  408. SDL_Mouse *mouse = SDL_GetMouse();
  409. if (mouse) {
  410. if (mouse->driverdata) {
  411. Cocoa_QuitMouseEventTap(((SDL_MouseData*)mouse->driverdata));
  412. SDL_free(mouse->driverdata);
  413. mouse->driverdata = NULL;
  414. }
  415. }
  416. }
  417. #endif /* SDL_VIDEO_DRIVER_COCOA */
  418. /* vi: set ts=4 sw=4 expandtab: */