# russellcastro/mxctl architecture

> Architecture diagram of the GitHub repository russellcastro/mxctl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/russellcastro/mxctl
- Repository: https://github.com/russellcastro/mxctl

## Overview

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`](https://github.com/russellcastro/mxctl/blob/master/mxctl.py) (mxctl.py), [`mxctl.service`](https://github.com/russellcastro/mxctl/blob/master/mxctl.service) (mxctl.service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_physical["Device path"]
  node_mouse(("MX Master 3S<br/>hardware mouse"))
  node_bolt["Bolt receiver<br/>usb receiver"]
  node_evdev["evdev input<br/>kernel input"]
  node_hidraw["hidraw device<br/>kernel hidraw"]
end

subgraph group_controller["mxctl"]
  node_entrypoint["mxctl.py<br/>python daemon/cli<br/>[mxctl.py]"]
  node_config["Config JSON<br/>runtime config"]
  node_modes{{"Runtime modes<br/>cli modes"}}
  node_grabber["Input grabber<br/>evdev control"]
  node_mapper["Button mapper<br/>event transform"]
  node_uinput["uinput device<br/>virtual input"]
  node_hidpp["HID++ control<br/>vendor protocol"]
  node_scroll{{"Scroll mode<br/>wheel control"}}
end

subgraph group_ops["Deployment"]
  node_service["mxctl.service<br/>systemd unit<br/>[mxctl.service]"]
  node_udev["udev rules<br/>permission setup"]
  node_restore{{"Recover exit<br/>cleanup"}}
end

node_mouse -->|"USB/Bolt"| node_bolt
node_bolt -->|"input events"| node_evdev
node_bolt -->|"raw HID"| node_hidraw
node_evdev -->|"exclusive grab"| node_grabber
node_grabber -->|"forward"| node_mapper
node_mapper -->|"emit"| node_uinput
node_uinput -->|"controlled by"| node_entrypoint
node_hidraw -->|"HID++ 2.0"| node_hidpp
node_hidpp -->|"toggle"| node_scroll
node_scroll -->|"wheel mode"| node_mouse
node_config -->|"drives"| node_entrypoint
node_modes -->|"invokes"| node_entrypoint
node_entrypoint -->|"normal path"| node_grabber
node_entrypoint -->|"diversion path"| node_hidpp
node_service -->|"runs"| node_entrypoint
node_udev -.->|"permits"| node_service
node_entrypoint -->|"shutdown"| node_restore
node_restore -->|"releases"| node_mouse

click node_entrypoint "https://github.com/russellcastro/mxctl/blob/master/mxctl.py"
click node_service "https://github.com/russellcastro/mxctl/blob/master/mxctl.service"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mouse,node_bolt,node_evdev,node_hidraw toneBlue
class node_entrypoint,node_config,node_modes,node_grabber,node_mapper,node_uinput,node_hidpp,node_scroll toneAmber
class node_service,node_udev,node_restore toneMint
```

## Components

### Device path

Mouse, receiver, and kernel interfaces

- **MX Master 3S**: hardware mouse · Physical device being controlled
- **Bolt receiver**: usb receiver · Logitech receiver bridging the mouse to Linux
- **evdev input**: kernel input · Standard button and scroll events from the mouse
- **hidraw device**: kernel hidraw · Raw HID++ command channel to the receiver

### mxctl

Single Python controller process and its runtime modes

- [mxctl.py](https://github.com/russellcastro/mxctl/blob/master/mxctl.py): python daemon/cli · Main controller process and command-line entrypoint · `mxctl.py`
- **Config JSON**: runtime config · User config driving matching, mapping, and diversion
- **Runtime modes**: cli modes · Identify, install, fallback, and normal control modes
- **Input grabber**: evdev control · Exclusively captures the physical mouse events
- **Button mapper**: event transform · Substitutes selected mouse buttons with keyboard actions
- **uinput device**: virtual input · Synthetic output device that emits remapped events
- **HID++ control**: vendor protocol · Sends vendor-specific commands over hidraw
- **Scroll mode**: wheel control · Toggles ratchet and free-spin behavior

### Deployment

Service, permissions, and installation boundary

- [mxctl.service](https://github.com/russellcastro/mxctl/blob/master/mxctl.service): systemd unit · Persistent daemon deployment boundary · `mxctl.service`
- **udev rules**: permission setup · Grants raw device access for service mode
- **Recover exit**: cleanup · Restores the mouse on clean shutdown; replug receiver after crashes

## Connections

- 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
