DynShift

Inside PhonePad: 40 bytes, up to 250 times a second

How a touch on a phone becomes an Xbox controller state on a Windows PC in about five milliseconds, and the decisions, measurements and corrections behind it.

A pulsar in grey light: a tiny bright star with two thin beams, ringed by evenly spaced pulses spreading outward.

PhonePad makes an Android phone act as an Xbox controller for a Windows PC, over Wi-Fi. Measured on the development hardware, a thumb movement on the phone reaches the PC and is acknowledged back in 4.9 to 8.0 milliseconds, typically about five, at a steady 249 packets a second with no loss across 42,805 consecutive packets. This article is about how that path is built: what runs where, why each piece was chosen over its alternatives, and the measurements that changed the design along the way.

If you want to know what PhonePad is before how it works, Introducing PhonePad is the better place to start.

The path

Every input travels the same short route. On the phone, raw touch events go to a router that works out which control each finger is on, the router updates a plain controller state, and the state is sent as one UDP datagram. On the PC, the companion receives it, verifies it and writes it to a virtual Xbox 360 controller, where games read it through XInput like any other pad.

On the phone: touch, the TouchRouter, the controller state and a UDP send. Over Wi-Fi, a 40-byte packet. On the PC: receive, verify, the ViGEmBus virtual pad, and XInput, where games read it. Phone · Dart, one isolate PC · Rust, a plain OS thread Touch raw pointers TouchRouter ControllerState UDP send recv wakes on arrival verify MAC · replay ViGEmBus virtual pad XInput 40 bytes ≤ 250 Hz
The hot path. Nothing in it waits on a user interface: on the phone, touch never rebuilds a widget; on the PC, the window only reads shared state and could freeze without stopping the controller.

How Windows sees a controller

The first decision was the one everything else depends on: how a program creates a controller that Windows, and every game, will accept as real. There were four candidates.

OptionWhy it was or was not used
Virtual HID Framework Microsoft's documented route, but it means writing a kernel driver and either an EV-signed submission or putting the PC into test-signing mode. Not something to ask of someone who wants to play a game today.
GameInput An API for reading devices. It offers no supported way to publish a virtual one.
vJoy Presents a DirectInput-only device. Xbox Cloud Gaming and most modern games read XInput or the browser's Gamepad API, so it would be invisible to them.
ViGEmBus Chosen. It creates a real Xbox 360 device node without driver signing, visible at once to joy.cpl, XInput, DirectInput, GameInput and the Gamepad API.

ViGEmBus has a real weakness: its author archived it in November 2023 and its last release is final. That risk is written down rather than hidden, and the design contains it. The rest of the companion talks to a VirtualPad interface, so moving to a successor driver touches one file. It is also the mechanism Parsec and DS4Windows ship with, which says something about how dependable it has been in practice.

The choice was verified rather than assumed. A small probe program plugs in a virtual pad, writes known states to it, and reads them back through Windows' own XInput API. All 26 checks passed (15 buttons, 5 trigger levels and 6 stick positions) and a write became visible to XInput in 0.09 milliseconds on average. The virtual pad costs essentially nothing. One oddity surfaced along the way: the public XInputGetState call hides the Guide button, and only an undocumented sibling reports it, so the probe looks that function up at run time in order to test Guide honestly.

Forty bytes over UDP

Input is sent as fixed-layout binary packets: no JSON, no variable-length fields, every integer little-endian. A full controller state fits in 40 bytes, so even at the 250-a-second ceiling the stream is about ten kilobytes a second.

The 40-byte INPUT packet: a 4-byte header, session id, sequence number and phone timestamp; 2 bytes of buttons; four 2-byte stick axes; two 1-byte triggers; the phone's last measured round trip; and an 8-byte truncated HMAC over the first 32 bytes. authenticated: bytes 0 to 31 8-byte HMAC header session seq clientTime buttons lx ly rx ry lt rt rttUs mac 0 4 8 12 16 18 20 22 24 26 27 28 32 40
The INPUT packet. The button field is bit-for-bit XInput's own wButtons, so the PC copies it straight through without translating. The last eight bytes authenticate the first 32.

The transport is UDP, not TCP, and that is a deliberate choice about which failure is worse. TCP guarantees delivery by resending, and holds back everything after a lost packet until the resend arrives. For a controller, a resent input is a stale input: by the time it lands, the thumb has moved on. So PhonePad never resends. It sends the full state whenever it changes, and repeats it at 120 Hz even when it has not. A lost packet is corrected by the next one, within about eight milliseconds, and nothing waits for anything.

Security on a shared network

An unauthenticated socket that injects controller input is a genuine hazard on a shared network, so no input is accepted from anything that has not paired. Pairing is an X25519 key exchange, confirmed by the six-digit code the companion displays, and it leaves both sides holding a shared key. Every input packet then carries the first eight bytes of an HMAC-SHA256 over its contents, and a sequence number checked against a 64-entry sliding window, so packets can be neither forged nor replayed. Input and control messages have separate sequence counters, so neither stream can disturb the other's replay protection.

Rejected packets are counted by reason and shown in the companion's diagnostics. They are never dropped silently, because a silent drop is indistinguishable from a network problem when someone is trying to work out why their controller is not responding.

The phone side

The app's interface is written in Flutter, and the obvious design would have moved the sending into native Android code. It was considered and rejected: routing every packet across the Flutter-to-Kotlin bridge adds a hop and an allocation to the hot path rather than removing one. Instead, raw pointer events feed a controller engine written in plain Dart, and the engine sends the datagram itself, from the same isolate, with one system call. The controller surface is drawn by a single custom painter behind a repaint boundary, so a touch never causes a widget rebuild or a layout pass.

Kotlin is used only for what Dart cannot reach: asking for the display's 120 Hz mode, holding Android's low-latency Wi-Fi lock, excluding the screen edges from the system's back gesture so it cannot fire mid-game, the vibration motor, reading the Wi-Fi link to compute the subnet's broadcast address for discovery, and a foreground service so the system cannot suspend an active session.

One rule in the touch code is worth singling out. The router never toggles a button on or off in response to a single event. On every event it rebuilds the whole controller state from the set of fingers currently on the glass. That makes a stuck button structurally impossible rather than merely unlikely.

Failing safe

The worst thing a controller can do is keep a button held after the connection dies: a car accelerating into a wall, a character running off a ledge. The companion therefore runs a watchdog. If no valid packet arrives for 120 milliseconds, every control is released to neutral; after two seconds, the session is dropped and the phone must reconnect. When the companion exits, the virtual pad is unplugged.

This was tested the hard way. A button was held on the phone and the app was force-stopped mid-press. The companion logged the stall at 131 milliseconds, and Windows saw exactly three states: neutral, the button, neutral. The 11-millisecond overshoot is one pass of the receive loop, which is expected and bounded.

What measuring changed

Several of PhonePad's design decisions came from measurements that contradicted an assumption. Three are worth retelling.

65 Hz instead of 250

The first test sender, asked for 250 packets a second, managed 65. The protocol was not at fault. Windows rounds socket read timeouts up to its scheduler tick of about 15.6 milliseconds, so a "1 millisecond" timeout actually cost 15, and capped the loop. Sleeping is quantised the same way. The sender was rebuilt around a non-blocking socket and produced an exact 250 Hz. The lesson generalises: on Windows, any pacing built on sleeping is coarse.

A rate that read too high

At one point the phone's own panel reported sending 312 packets a second while the PC reported accepting 259. It looked like loss. It was a measurement bug: each side published its count when "more than a second" had passed, but only checked on a periodic tick, so the real window was up to 25% longer than the second it was divided by. Both sides now divide by the time that actually elapsed, and both read 249. The documentation that had quoted the inflated figure was corrected in place, with the explanation left next to it.

Two deadzones

The most noticeable improvement came from a complaint about feel: nothing at the start of the stick's travel, then suddenly too much. The cause was two deadzones stacked on each other. Games apply their own, typically 15 to 25% of the range, because they assume a physical stick with slop and drift to hide. PhonePad was applying 12% of its own before the game ever saw the value.

Line chart of game input against thumb travel. Before: nothing reaches the game for the first quarter of the travel, then it climbs steeply. After: input starts rising from the first five percent and grows smoothly to full lock. The data is in the table below. 0% 0% 25% 25% 50% 50% 75% 75% 100% 100% Thumb travel from the centre Input the game receives dead before Before: 0% travel → 0% game input After: 0% travel → 0% game input Before: 5% travel → 0% game input After: 5% travel → 1.2% game input Before: 10% travel → 0% game input After: 10% travel → 4.3% game input Before: 15% travel → 0% game input After: 15% travel → 8.1% game input Before: 20% travel → 0% game input After: 20% travel → 12.3% game input Before: 25% travel → 0% game input After: 25% travel → 17% game input Before: 30% travel → 6.4% game input After: 30% travel → 21.9% game input Before: 50% travel → 33.2% game input After: 50% travel → 44.2% game input Before: 75% travel → 66.6% game input After: 75% travel → 76.2% game input Before: 100% travel → 100% game input After: 100% travel → 100% game input After Before
What the game receives for a given thumb travel, before and after the rebuild, with a typical game deadzone of 0.15 applied. Before, the first quarter of the travel produced nothing at all. Hover a point for its value.
Thumb travelGame input, beforeGame input, after
5%0.0%1.2%
10%0.0%4.3%
15%0.0%8.1%
20%0.0%12.3%
25%0.0%17.0%
30%6.4%21.9%
50%33.2%44.2%
75%66.6%76.2%
100%100%100%

A thumb on glass does not glide; it sticks and then breaks free in a jump of two or three millimetres, around 12% of the stick's travel. Arriving just as the response finally began, that jump delivered about 16% of the game's input at once, which is exactly "nothing, then too much". The fix inverts the old behaviour: as soon as the thumb leaves the centre, PhonePad's output starts just past the game's own dead band, so what the player feels starts from zero and climbs smoothly. One subtlety had to be protected with a test: an exactly centred stick must still send exactly zero, or every game would read the pad as permanently pushed.

What has not been measured

The project keeps a list of what it does not know yet, instead of filling the gaps with estimates: battery drain over a long session, CPU use on either side, the phone's frame rate under heavy touch, behaviour on a congested or weak network, and the noise from a real finger held still. The round-trip figure is also explicit about its scope. It covers phone to PC, validation, the virtual pad and back, but not the touchscreen's own sampling delay or the display at the other end, which the software cannot observe.

Building on it

The wire protocol lives in its own folder of the repository, licensed under Apache-2.0 rather than the GPL that covers the app and companion, with a shared fixture file that both implementations are tested against. That is an invitation: a compatible client for another platform, or a companion for another operating system, can be written against the documented format without inheriting the GPL. The full reasoning behind every choice above is in the architecture decision record, and the raw measurements, with the commands to reproduce them, are in the benchmarks document.