From 9292b23bb4b5e8de66cc10efb71f4475412a0b39 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Sat, 8 Jul 2017 12:46:38 +0300 Subject: [PATCH 1/2] Restore mouse position when entering GUI capture mode in Cocoa backend --- src/posix/cocoa/i_input.mm | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/posix/cocoa/i_input.mm b/src/posix/cocoa/i_input.mm index 08745e6874..3485b0a95d 100644 --- a/src/posix/cocoa/i_input.mm +++ b/src/posix/cocoa/i_input.mm @@ -109,7 +109,7 @@ void CheckGUICapture() } } -void CenterCursor() +void SetCursorPosition(const NSPoint position) { NSWindow* window = [NSApp keyWindow]; if (nil == window) @@ -118,15 +118,14 @@ void CenterCursor() } const NSRect displayRect = [[window screen] frame]; - const NSRect windowRect = [window frame]; - const CGPoint centerPoint = CGPointMake(NSMidX(windowRect), displayRect.size.height - NSMidY(windowRect)); + const CGPoint eventPoint = CGPointMake(position.x, displayRect.size.height - position.y); CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); if (NULL != eventSource) { CGEventRef mouseMoveEvent = CGEventCreateMouseEvent(eventSource, - kCGEventMouseMoved, centerPoint, kCGMouseButtonLeft); + kCGEventMouseMoved, eventPoint, kCGMouseButtonLeft); if (NULL != mouseMoveEvent) { @@ -141,6 +140,20 @@ void CenterCursor() s_skipMouseMoves = 2; } +void CenterCursor() +{ + NSWindow* window = [NSApp keyWindow]; + if (nil == window) + { + return; + } + + const NSRect displayRect = [[window screen] frame]; + const NSRect windowRect = [window frame]; + const NSPoint centerPoint = { NSMidX(windowRect), NSMidY(windowRect) }; + + SetCursorPosition(centerPoint); +} bool IsInGame() { @@ -227,6 +240,7 @@ void I_ReleaseMouseCapture() void I_SetNativeMouse(bool wantNative) { static bool nativeMouse = true; + static NSPoint mouseLocation; if (wantNative != nativeMouse) { @@ -234,6 +248,7 @@ void I_SetNativeMouse(bool wantNative) if (!wantNative) { + mouseLocation = [NSEvent mouseLocation]; CenterCursor(); } @@ -241,6 +256,8 @@ void I_SetNativeMouse(bool wantNative) if (wantNative) { + SetCursorPosition(mouseLocation); + [NSCursor unhide]; } else From 58b348e75a0e56ee1307476e19a2bc94ba2dd5f1 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Sat, 8 Jul 2017 13:35:09 +0300 Subject: [PATCH 2/2] Skip autogenerated mouse move events in Cocoa backend regardless of mode --- src/posix/cocoa/i_input.mm | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/posix/cocoa/i_input.mm b/src/posix/cocoa/i_input.mm index 3485b0a95d..4f0aa25d5e 100644 --- a/src/posix/cocoa/i_input.mm +++ b/src/posix/cocoa/i_input.mm @@ -509,19 +509,6 @@ void ProcessMouseMoveInMenu(NSEvent* theEvent) void ProcessMouseMoveInGame(NSEvent* theEvent) { - if (!use_mouse) - { - return; - } - - // TODO: remove this magic! - - if (s_skipMouseMoves > 0) - { - --s_skipMouseMoves; - return; - } - int x([theEvent deltaX]); int y(-[theEvent deltaY]); @@ -648,6 +635,12 @@ void ProcessMouseMoveEvent(NSEvent* theEvent) return; } + if (s_skipMouseMoves > 0) + { + --s_skipMouseMoves; + return; + } + if (GUICapture) { ProcessMouseMoveInMenu(theEvent);