Skip to content

KrishGoya1/cubemouse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CubeMouse

Control your PC cursor using your phone’s touchscreen.

Language: Rust License Status Contributions


πŸ“– Overview

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.


🎯 Project Vision

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.

Objectives

  • ✨ 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.

🧱 System Architecture

High-Level Overview


β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           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 Responsibilities

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.

πŸ”Œ Communication Flow

  1. Pairing Phase

    • PC launches WebSocket server and generates a QR code with connection info.
    • Mobile scans the QR and connects automatically.
  2. Handshake Phase

    • Mobile sends a HANDSHAKE_INIT packet announcing its version and capabilities.
    • PC validates and prepares for session input.
  3. Session Phase

    • Mobile continuously sends input packets (MOVE, CLICK, SCROLL).
    • PC decodes and translates them into cursor events.
  4. Termination Phase

    • Either side can gracefully close the session.

βš™οΈ CubeMouse Binary Protocol v1

Packet Structure

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)

Opcode Map

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

Opcode Details

0x01 β€” MOVE

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.


0x02 β€” SCROLL

Scroll wheel movement.

Field Type Description
dx int16 Horizontal scroll delta
dy int16 Vertical scroll delta

0x10 β€” CLICK

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)

0x20 β€” KEEPALIVE

Maintains connection liveness.

Field Type Description
timestamp uint32 Unix time or uptime in ms

0x21 β€” HANDSHAKE_INIT

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)

0x22 β€” CONFIG_UPDATE

Runtime parameter update.

Field Type Description
param_id u8 0 = sensitivity, 1 = scroll invert
value Variable Value depending on parameter

Example Packet Sequence

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

🧠 Design Rationale

Why Binary?

  • Lower latency and CPU overhead than JSON.
  • Minimal parsing logic on both ends.
  • Compact packet size (3–8 bytes typical).

Why WebSocket?

  • Persistent, reliable, and ordered transmission.
  • Built-in support across languages and mobile SDKs.
  • Perfect for LAN-based real-time input systems.

Why Rust?

  • Excellent concurrency and safety guarantees.
  • Suited for low-level event translation.
  • Modern async ecosystem with tokio and tungstenite.

🧰 Tech Stack

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

🚧 Roadmap

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

πŸ§ͺ Development Guidelines

  • 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.

🧭 Summary

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.


πŸ“„ Appendix

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

About

Use your device as a touchpad for your PC

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors