How russellcastro/mxctl works
1. Repository shape
- This is a very small, single-purpose Linux controller for the Logitech MX Master 3S.
- The architecture is centered on one Python entrypoint, mxctl.py, plus a mxctl.service systemd unit for running it as a background service.
- There is no UI layer, web server, or multi-package decomposition.
2. Primary runtime model
- mxctl.py acts as both the daemon and the CLI utility.
- It supports operational modes for normal control, device discovery, config bootstrap, key listing, udev installation, and a “no HID++” fallback mode.
- The service is intended to run continuously and intercept mouse input at the system level.
3. Physical-to-kernel input path
- The mouse connects through a Logitech Bolt receiver.
- Linux exposes two relevant kernel interfaces here:
- an evdev input device for regular button/scroll events
- a vendor-specific hidraw device for HID++ commands and notifications
- mxctl sits on top of both paths.
4. Button remapping subsystem
- For ordinary button remapping, mxctl grabs the mouse’s evdev device exclusively.
- It then creates a virtual output device via uinput.
- Events are forwarded from the physical device to the virtual device, with selected button codes substituted for keyboard key codes based on config.
- This is the main input transformation boundary: physical mouse button -> remapped keyboard action.
5. Scroll mode control subsystem
- Scroll behavior is controlled separately from button remapping.
- mxctl communicates with the mouse using HID++ 2.0 over the Bolt receiver’s hidraw interface.
- It disables the mouse’s adaptive SmartShift behavior by setting the threshold so auto-switching never triggers.
- It then toggles the wheel between pure ratchet and pure free-spin modes on each Smart Shift button press.
6. Event diversion strategy
- The Smart Shift button and DPI button do not appear as normal evdev button events.
- mxctl handles the Smart Shift button through HID++ diversion, not through standard input remapping.
- That creates two distinct control planes:
- generic button mapping via evdev/uinput
- scroll-mode toggling via HID++/hidraw
7. Configuration boundary
- Runtime behavior is driven by a JSON config file at ~/.config/mxctl/config.json.
- Config determines mouse matching, Bolt slot selection, button-to-key mapping, whether SmartShift diversion is enabled, and an alternate scroll-toggle button path.
- The config layer is what makes the script reusable across slightly different device detections and user preferences.
8. Device discovery and identification
- --identify is used to discover button codes and device details interactively.
- The script matches the mouse by mouse_name_match and locates the HID++ device by vendor ID and optional Bolt receiver slot.
- This discovery logic is important because the controller depends on the correct evdev and hidraw endpoints being paired.
9. Operating system integration
- The project relies on Linux kernel HID support, evdev, uinput, and hidraw.
- --install-udev exists because raw device access is restricted; udev rules plus membership in the input group are required for service-mode operation.
- mxctl.service is the deployment boundary that turns the script into a persistent system daemon.
10. Failure and recovery behavior
- The controller tries to restore the mouse on clean exit so the physical device continues working normally.
- If the process crashes, the repository documents replugging the Bolt receiver as the recovery path.
- That indicates the architecture assumes direct device ownership and exclusive grabbing during operation.
11. External dependency and scope
- The only explicit Python dependency is evdev.
- Logitech-specific behavior is the reason HID++ is needed; the repository’s value is in bridging generic Linux input handling with vendor-specific receiver commands.
- Solaar is mentioned only as an alternative that lacks these two specific capabilities.
12. What to draw in an architecture diagram
- Show a single controller process in the middle.
- On the left, a physical MX Master 3S feeding the Bolt receiver, then branching into evdev and hidraw.
- On the right, show two outputs/actions:
- a virtual input device via uinput for remapped keys
- HID++ commands back to the mouse for scroll-mode switching
- Around it, show config storage in the user config directory and systemd/udev as the deployment and permission boundary.
Main entry points: mxctl.py mxctl.py · mxctl.service mxctl.service
Components 15
Device path
Mouse, receiver, and kernel interfaces
- MX Master 3S · Physical device being controlled
- Bolt receiver · Logitech receiver bridging the mouse to Linux
- evdev input · Standard button and scroll events from the mouse
- hidraw device · Raw HID++ command channel to the receiver
mxctl
Single Python controller process and its runtime modes
- mxctl.py · Main controller process and command-line entrypoint
mxctl.py - Config JSON · User config driving matching, mapping, and diversion
- Runtime modes · Identify, install, fallback, and normal control modes
- Input grabber · Exclusively captures the physical mouse events
- Button mapper · Substitutes selected mouse buttons with keyboard actions
- uinput device · Synthetic output device that emits remapped events
- HID++ control · Sends vendor-specific commands over hidraw
- Scroll mode · Toggles ratchet and free-spin behavior
Deployment
Service, permissions, and installation boundary
- mxctl.service · Persistent daemon deployment boundary
mxctl.service - udev rules · Grants raw device access for service mode
- Recover exit · Restores the mouse on clean shutdown; replug receiver after crashes
Connections 18
- MX Master 3S → Bolt receiver: USB/Bolt · Mouse connects through the receiver
- Bolt receiver → evdev input: input events · Receiver exposes standard Linux input events
- Bolt receiver → hidraw device: raw HID · Receiver exposes vendor-specific HID access
- evdev input → Input grabber: exclusive grab · mxctl takes ownership of the physical input stream
- Input grabber → Button mapper: forward · Events are inspected and rewritten as needed
- Button mapper → uinput device: emit · Mapped keyboard actions are emitted through a virtual device
- uinput device → mxctl.py: controlled by · The controller owns the virtual output device
- hidraw device → HID++ control: HID++ 2.0 · Vendor protocol commands flow over the raw device
- HID++ control → Scroll mode: toggle · Scroll mode is changed by HID++ commands
- Scroll mode → MX Master 3S: wheel mode · Mouse wheel switches between ratchet and free-spin
- Config JSON → mxctl.py: drives · Config selects matching, mappings, and diversion behavior
- Runtime modes → mxctl.py: invokes · CLI modes configure discovery, install, and normal control
- mxctl.py → Input grabber: normal path · Primary runtime path for button remapping
- mxctl.py → HID++ control: diversion path · SmartShift/DPI handling bypasses standard evdev events
- mxctl.service → mxctl.py: runs · systemd keeps the controller running in the background
- udev rules → mxctl.service: permits · Raw access and input-group membership enable device access
- mxctl.py → Recover exit: shutdown · Attempts to return the mouse to normal operation on exit
- Recover exit → MX Master 3S: releases · Device ownership is relinquished during clean shutdown