mac_mouse_sys/
lib.rs

1//! Tiny wrapper for mouse-related functions in macOS `IOKit/hidsystem`
2//!
3//! Definition of terms:
4//!
5//! `pointer resolution` - the resolution of pointer movement space, the higher the value, the slower the pointer movement speed
6//!
7//! Special values:
8//!
9//! - `-1`: null
10//!
11//! - `5 * 65536`: Min
12//!
13//! - `1995 * 65536` Max
14//!
15//! `mouse acceleration` - the acceleration of the pointer movement, the higher the value, the faster the pointer speed will increase, `0` means no acceleration
16//!
17//! Special values:
18//!
19//! - `0`: No acceleration
20//!
21//! - `45056`: macOS default
22
23#![deny(missing_docs)]
24#![deny(missing_debug_implementations)]
25#![warn(trivial_casts, trivial_numeric_casts)]
26#![allow(improper_ctypes)]
27#![cfg_attr(debug_assertions, allow(dead_code, unused_imports))]
28#![cfg_attr(docsrs, deny(rustdoc::broken_intra_doc_links))]
29#![cfg_attr(docsrs, feature(doc_cfg))]
30#![cfg(target_os = "macos")]
31
32mod sys {
33    use libc::c_int;
34    #[link(name = "mouse")]
35    extern "C" {
36        pub fn getPointerResolution() -> c_int;
37        pub fn getMouseAcceleration() -> c_int;
38        pub fn setPointerResolution(res: c_int) -> c_int;
39        pub fn setMouseAcceleration(acc: c_int) -> c_int;
40    }
41}
42
43/// Get the pointer resolution
44pub fn get_pointer_resolution() -> Result<i32, String> {
45    let res = unsafe { sys::getPointerResolution() };
46    Ok(res)
47}
48
49/// Get the mouse acceleration
50pub fn get_mouse_acceleration() -> Result<i32, String> {
51    let acc = unsafe { sys::getMouseAcceleration() };
52    if acc == -1 {
53        Err("Failed to get mouse acceleration".to_string())
54    } else {
55        Ok(acc)
56    }
57}
58
59/// Set the pointer resolution
60pub fn set_pointer_resolution(res: i32) -> Result<(), String> {
61    let ret = unsafe { sys::setPointerResolution(res) };
62    if ret != 0 {
63        Err("Failed to set pointer resolution".to_string())
64    } else {
65        Ok(())
66    }
67}
68
69/// Set the mouse acceleration
70pub fn set_mouse_acceleration(acc: i32) -> Result<(), String> {
71    let ret = unsafe { sys::setMouseAcceleration(acc) };
72    if ret != 0 {
73        Err("Failed to set mouse acceleration".to_string())
74    } else {
75        Ok(())
76    }
77}