From 2fb150b345c0c27f289b140e763df7764561946e Mon Sep 17 00:00:00 2001 From: Ruben Bakker Date: Mon, 28 Dec 2015 16:59:27 +0200 Subject: Implement set_cursor_position with CGWarpMouseCursorPosition --- src/api/cocoa/mod.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index eae344c..2a5f3ec 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -34,7 +34,7 @@ use core_foundation::base::TCFType; use core_foundation::string::CFString; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; -use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh}; +use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh, CGWarpMouseCursorPosition}; use std::ffi::CStr; use std::collections::VecDeque; @@ -371,7 +371,7 @@ impl Window { let count: NSUInteger = msg_send![screens, count]; let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber")); let mut matching_screen: Option = None; - for i in (0..count) { + for i in 0..count { let screen = msg_send![screens, objectAtIndex:i as NSUInteger]; let device_description = NSScreen::deviceDescription(screen); let value: id = msg_send![device_description, objectForKey:*key]; @@ -765,8 +765,15 @@ impl Window { } #[inline] - pub fn set_cursor_position(&self, _x: i32, _y: i32) -> Result<(), ()> { - unimplemented!(); + pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> { + unsafe { + let (window_x, window_y) = self.get_position().unwrap_or((0, 0)); + let (cursor_x, cursor_y) = (window_x + x, window_y + y); + + // TODO: Check for errors. + let _ = CGWarpMouseCursorPosition(CGPoint { x: cursor_x as CGFloat, y: cursor_y as CGFloat }); + Ok(()) + } } } -- cgit v1.2.3 From d892ba168b178d5adac2db777b3dfc912543d40b Mon Sep 17 00:00:00 2001 From: Ruben Bakker Date: Tue, 29 Dec 2015 21:52:18 +0200 Subject: Reduce cursor warp delay with CGAssociateMouseAndMouseCursorPosition --- src/api/cocoa/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 2a5f3ec..a720a51 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -766,14 +766,16 @@ impl Window { #[inline] pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> { + let (window_x, window_y) = self.get_position().unwrap_or((0, 0)); + let (cursor_x, cursor_y) = (window_x + x, window_y + y); + unsafe { - let (window_x, window_y) = self.get_position().unwrap_or((0, 0)); - let (cursor_x, cursor_y) = (window_x + x, window_y + y); - // TODO: Check for errors. let _ = CGWarpMouseCursorPosition(CGPoint { x: cursor_x as CGFloat, y: cursor_y as CGFloat }); - Ok(()) + let _ = CGAssociateMouseAndMouseCursorPosition(true); } + + Ok(()) } } -- cgit v1.2.3