A Rust-based gesture recognition tool for Apple Magic Mouse on Linux, specifically designed for the Magic Mouse 2 USB-C 2024 model.
- Multi-touch gesture recognition: Detects swipes, scrolls, taps, and pinch gestures
- Two-finger tap recognition: Advanced two-finger tap detection based on Linux Multi-Touch Protocol
- Configurable actions: Execute custom commands for each gesture
- Direct input handling: Works with raw input events from evdev
- Async processing: Non-blocking event processing using Tokio
- Customizable configuration: JSON-based configuration system
- 2-finger swipes: Navigate browser history, open/close tabs
- Scrolling: Vertical and horizontal scrolling
- Taps: Single and double-finger taps for click simulation
- Pinch: Zoom in/out functionality
- Button clicks: Standard mouse button support
- Apple Magic Mouse 2 USB-C 2024
- Linux system with evdev support
- Rust 1.70+ (for building)
hid-magicmouse
kernel module (see setup guide)xdotool
(for executing actions)
On Arch Linux:
sudo pacman -S xdotool
On Ubuntu/Debian:
sudo apt install xdotool
The easiest way to install Magic Mouse Gesture Recognition is using the automated installation script:
git clone <repository-url>
cd mouse-gesture
# Run the installation script
./scripts/install.sh
The installation script will:
- Install Rust dependencies and build the project
- Copy the binary to
/usr/local/bin
- Set up configuration in
~/.config/mouse-gesture/
- Create udev rules for device access
- Add your user to the
input
group - Install and configure a systemd user service
- Set up automatic startup
After installation:
# Start the service immediately
systemctl --user start mouse-gesture.service
# Enable automatic startup on login
systemctl --user enable mouse-gesture.service
If you prefer to build and configure manually:
- Clone the repository:
git clone <repository-url>
cd mouse-gesture
- Build the project:
cargo build --release
To completely remove the application:
./scripts/uninstall.sh
The application will create a default configuration file config.json
on first run. You can customize gestures and actions by editing this file.
{
"device": {
"path": null,
"auto_detect": true,
"name_pattern": "Magic Mouse"
},
"gesture": {
"scroll_threshold": 2.0,
"swipe_threshold": 12.0,
"pinch_threshold": 0.1,
"tap_timeout_ms": 300,
"debounce_ms": 100
},
"actions": {
"swipe_left_2finger": "xdotool key alt+Right",
"swipe_right_2finger": "xdotool key alt+Left",
"swipe_up_2finger": "xdotool key ctrl+t",
"swipe_down_2finger": "xdotool key ctrl+w",
"scroll_vertical": "scroll_vertical",
"scroll_horizontal": "scroll_horizontal",
"tap_1finger": "click",
"tap_2finger": "right_click",
"pinch_in": "xdotool key ctrl+minus",
"pinch_out": "xdotool key ctrl+plus"
}
}
After installation, the service runs automatically. For manual testing or development:
# Check system dependencies and device detection
mouse-gesture-recognition --check-deps
# Run manually with auto-detection (for testing)
mouse-gesture-recognition
# Specify device path explicitly
mouse-gesture-recognition -d /dev/input/event26
# Enable verbose logging for debugging
mouse-gesture-recognition -v
# Run directly from build directory (before installation)
sudo ./target/release/mouse-gesture-recognition --check-deps
Note: After installation, no sudo
is required as the application runs with user permissions and accesses devices through proper group membership.
Use evtest
to find your Magic Mouse device:
sudo evtest
Look for a device with "Magic Mouse" in the name and note its event number.
To find out which /dev/input/eventX
device corresponds to your Magic Mouse:
- Run the following command:
sudo evtest
-
You will see a list of input devices. Look for one with "Magic Mouse" in the name. Note the event number (e.g.,
/dev/input/event27
). -
Select that number when prompted, or use it directly:
evtest /dev/input/event27
- Move or tap your Magic Mouse. If you see events appear, you have found the correct device.
If you want to check without root, add your user to the input
group and log out/in:
sudo usermod -aG input (whoami)
If you do not want to log out, you can use newgrp input
to start a new shell with the new group applied immediately:
newgrp input
Then use evtest
without sudo
in that shell.
The installation script automatically sets up a systemd user service. After installation:
# Enable the service to start automatically
systemctl --user enable mouse-gesture.service
# Start the service
systemctl --user start mouse-gesture.service
# Check service status
systemctl --user status mouse-gesture.service
# View service logs
journalctl --user -u mouse-gesture.service -f
The service is configured to:
- Start automatically when you log in
- Restart automatically if it crashes
- Run with your user permissions (no root required)
- Access the configuration in
~/.config/mouse-gesture/config.json
You can also use the provided service management script:
# Service management
./scripts/service.sh start # Start the service
./scripts/service.sh stop # Stop the service
./scripts/service.sh restart # Restart the service
./scripts/service.sh status # Check service status
./scripts/service.sh enable # Enable automatic startup
./scripts/service.sh disable # Disable automatic startup
# Monitoring and configuration
./scripts/service.sh logs # View service logs
./scripts/service.sh edit-config # Edit configuration file
The project includes several utility scripts:
# Development helpers
./scripts/dev.sh build # Build project
./scripts/dev.sh run # Run directly (not as service)
./scripts/dev.sh test # Run tests
./scripts/dev.sh clean # Clean build artifacts
# Build system
make install # Same as ./scripts/install.sh
make uninstall # Same as ./scripts/uninstall.sh
make build # Build project
make run # Run project
The application runs with user privileges and accesses input devices through proper group membership. The installation script:
- Adds your user to the
input
group for device access - Sets up udev rules for Magic Mouse devices
- Configures appropriate file permissions
No root privileges are required for normal operation. The service runs as your user account with access to:
- Input devices (via
input
group membership) - Your desktop environment for executing actions
- User configuration directory (
~/.config/mouse-gesture/
)
- Ensure the Magic Mouse is connected and paired
- Check that the
hid-magicmouse
module is loaded - Verify the device path with
ls /dev/input/event*
- Enable verbose logging with
-v
flag - Check the gesture thresholds in configuration
- Ensure multi-touch events are being generated
- Run
--check-deps
to verify system dependencies - Check that
xdotool
is installed and working - Verify the command syntax in configuration
src/
├── main.rs # Application entry point
├── device.rs # Magic Mouse device handling
├── gesture.rs # Gesture recognition algorithms
├── config.rs # Configuration management
└── event_handler.rs # Action execution
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT OR Apache-2.0 license.
- Linux Magic Trackpad 2 USB-C Driver for the kernel module
- The evdev and Rust community for excellent libraries