CubeMouse is a lightweight cross-device interaction system written in Rust, designed to transform a smartphone into a wireless trackpad for a PC.
It establishes a secure, low-latency WebSocket connection between the devices and transmits binary-encoded input events (cursor movement, clicks, scrolls, etc.) in real-time.
This project is both a learning experience in systems design and a demonstration of clean, production-level architecture.
To create a minimal, performant, and extensible framework that turns a smartphone into a seamless input device for a PC, while demonstrating production-grade code structure and documentation quality.
- β¨ Write idiomatic, modular Rust code.
- β‘ Build a custom binary protocol for efficiency.
- π Maintain a secure, persistent WebSocket channel.
- π§© Keep architecture clean, extensible, and well-documented.
- π‘ Demonstrate real-world engineering thinking in a small project.
βββββββββββββββββββββββββββββ
β PC β
βββββββββββββββββββββββββββββ
β QR Code Generator β
β Connection Manager β
β Protocol Parser β
β Input Translator β
βββββββββββββββββββββββββββββ
β OS Cursor API β
βββββββββββββββββββββββββββββ
β²
WebSocket β
Channel β
βΌ
βββββββββββββββββββββββββββββ
β Android / iOS β
βββββββββββββββββββββββββββββ
β Connection Manager β
β Data Encoder β
β Input Capture Module β
β UI for User β
βββββββββββββββββββββββββββββ
| Module | Platform | Description |
|---|---|---|
| QR Code Generator | PC | Generates a QR with the serverβs WebSocket address. |
| Connection Manager | Both | Handles WebSocket connection lifecycle. |
| Protocol Parser | PC | Decodes binary messages from client. |
| Input Translator | PC | Maps packets to OS-level cursor/click events. |
| Input Capture | Mobile | Captures screen touches, taps, and swipes. |
| Data Encoder | Mobile | Converts touch data into binary packets. |
| User Interface | Mobile | Minimal UI for touchpad surface and connection feedback. |
-
Pairing Phase
- PC launches WebSocket server and generates a QR code with connection info.
- Mobile scans the QR and connects automatically.
-
Handshake Phase
- Mobile sends a
HANDSHAKE_INITpacket announcing its version and capabilities. - PC validates and prepares for session input.
- Mobile sends a
-
Session Phase
- Mobile continuously sends input packets (
MOVE,CLICK,SCROLL). - PC decodes and translates them into cursor events.
- Mobile continuously sends input packets (
-
Termination Phase
- Either side can gracefully close the session.
| Field | Size (bytes) | Description |
|---|---|---|
| Opcode | 1 | Message type identifier |
| Length | 1 | Payload size in bytes |
| Payload | Variable | Data depending on opcode |
| Checksum | 1 (optional) | XOR of all previous bytes (for debug) |
| Range | Category | Description |
|---|---|---|
0x01β0x1F |
Cursor / Motion | Movement and scroll events |
0x20β0x3F |
Click / Tap | Button and tap actions |
0x40β0x5F |
Control / Session | Keepalive, handshake, and config |
0x60β0x7F |
Reserved | Gestures, multi-touch (future) |
0x80+ |
Vendor-defined | Extensions |
Moves the cursor by relative deltas.
| Field | Type | Description |
|---|---|---|
dx |
int16 |
Horizontal movement delta |
dy |
int16 |
Vertical movement delta |
Example: 01 04 05 00 02 00 β Move right 5px, down 2px.
Scroll wheel movement.
| Field | Type | Description |
|---|---|---|
dx |
int16 |
Horizontal scroll delta |
dy |
int16 |
Vertical scroll delta |
Tap or click events.
| Field | Type | Description |
|---|---|---|
button |
u8 |
0 = left, 1 = right, 2 = middle |
state |
u8 |
0 = down, 1 = up, 2 = tap |
fingers |
u8 |
Number of fingers (default = 1) |
Maintains connection liveness.
| Field | Type | Description |
|---|---|---|
timestamp |
uint32 |
Unix time or uptime in ms |
Sent once upon connection start.
| Field | Type | Description |
|---|---|---|
protocol_version |
u8 |
e.g. 0x01 |
device_type |
u8 |
0 = Android, 1 = iOS |
capabilities |
u16 |
bitmask (e.g., scroll, gestures) |
Runtime parameter update.
| Field | Type | Description |
|---|---|---|
param_id |
u8 |
0 = sensitivity, 1 = scroll invert |
value |
Variable | Value depending on parameter |
| Action | Hex Bytes | Meaning |
|---|---|---|
| Handshake | 21 04 01 00 03 00 |
Version 1, Android, supports scroll+gesture |
| Move | 01 04 05 00 02 00 |
Move 5px right, 2px down |
| Left Tap | 10 03 00 00 01 + 10 03 00 01 01 |
Down + Up |
| Keepalive | 20 04 E8 03 00 00 |
Timestamp = 1000ms |
- Lower latency and CPU overhead than JSON.
- Minimal parsing logic on both ends.
- Compact packet size (3β8 bytes typical).
- Persistent, reliable, and ordered transmission.
- Built-in support across languages and mobile SDKs.
- Perfect for LAN-based real-time input systems.
- Excellent concurrency and safety guarantees.
- Suited for low-level event translation.
- Modern async ecosystem with
tokioandtungstenite.
| Layer | Technology |
|---|---|
| Language | Rust |
| Async Runtime | Tokio |
| Networking | tokio-tungstenite |
| Input Simulation | enigo / mouse-rs |
| Mobile Client | Android (Kotlin) or Flutter |
| QR Generator | qrcode-rust / image crate |
| Feature | Description | Status |
|---|---|---|
| Gestures | Pinch, rotate, swipe | π Planned |
| Multi-touch | Finger tracking | π Planned |
| Secure Channel | TLS / encryption | π Planned |
| Desktop GUI | QR display + logs | π§ Design |
| Config Sync | Per-device profiles | π Future |
- Project Structure
src/
βββ main.rs
βββ server/
β βββ mod.rs
β βββ connection.rs
β βββ protocol.rs
β βββ translator.rs
βββ utils/
β βββ qr.rs
β βββ logging.rs
βββ config/
βββ settings.rs
- Use async Rust (
tokio) for concurrency. - Write unit tests for protocol parsing.
- Keep module boundaries clear β communication, decoding, and OS-level actions are separate layers.
- Follow Rustdoc-style inline documentation.
CubeMouse combines low-level system design with clean architecture principles.
Itβs built to be efficient, educational, and production-minded β an example of how to think like a systems engineer rather than just a coder.
Design Principles
- Separate concerns β transport, parsing, action should never mix.
- Prefer compact binary semantics over verbose formats.
- Documentation is a first-class citizen of the system.
License: MIT
Author: Krish Goyal
Version: 0.1.0