39. Games: pointer lock, raw mouse motion, key positions, game pads#
A game that turns its camera with the mouse locks the pointer to its window: the pointer is hidden,
stays where it is, and its motion comes raw, without the system's acceleration where the platform
has it. ui.pointer_lock(true) asks for it for the window of the pass, ui.pointer_lock(false) lets
it go; both answer the state (ui::PointerLock, the same as plat::PointerLock):
| State | Meaning |
|---|---|
Off | not locked: never asked, or let go (by the app, Esc, the focus going elsewhere, minimized, closed) |
Pending | asked, not granted yet: the window has no focus yet, the browser waits for the next press in the page, the Wayland compositor has not confirmed it |
Locked | locked: no cursor, ui.pointer_motion() has the motion |
Unsupported | the platform cannot: Android and iOS (touch controls come later), a Wayland compositor without zwp_pointer_constraints_v1 or zwp_relative_pointer_manager_v1, an X server without XInput 2 |
Refused | the system said no: the browser's pointerlockerror, another X11 program holds a grab |
ui.pointer_lock_state() says the state at any time; every change gives a pass. While locked,
ui.pointer_motion() is the motion since the window's last pass, (dx, dy) as f64, right and down
positive; a frame multiplies it by its sensitivity:
if ui.pointer_lock_state() == PointerLock::Locked {
let (dx, dy) = ui.pointer_motion();
self.yaw += dx * self.sens;
self.pitch = (self.pitch + dy * self.sens).clamp(-89.0, 89.0);
} else if inp.pressed & (1 << BTN_LEFT) != 0 {
ui.pointer_lock(true); // "click to play": in the browser only a press may ask
}
The lock always lets go: Esc (it then reaches the app as no key; in the browser Esc is the
browser's), the window losing the focus, being minimized or closed, and ui.pointer_lock(false).
It never comes back by itself: after a release the app asks again, best from a press ("click to
play"). A request while the window has no focus waits and takes effect when it gets it. One window
holds the lock at a time. What the motion is, per platform:
| Platform | How | Units of pointer_motion |
|---|---|---|
| Windows | Raw Input (WM_INPUT), the cursor clipped to the window's middle (ClipCursor) and hidden; afterwards back where it was | the mouse's counts; an absolute device (remote desktop, tablet, some virtual machines) in pixels |
| Linux, Wayland | zwp_pointer_constraints_v1.lock_pointer, zwp_relative_pointer_v1 | unaccelerated (dx_unaccel; libinput: 1000 dpi counts) |
| Linux, X11 | XI_RawMotion (XInput 2), a pointer grab confined to the window with no cursor; afterwards back where it was | the device's raw values; an absolute device in pixels |
| macOS | CGAssociateMouseAndMouseCursorPosition(false), the cursor hidden | the events' deltaX / deltaY: points, after the system's acceleration |
| Browser | requestPointerLock on the canvas, with unadjustedMovement where the browser has it (Chrome on Windows and macOS) | movementX / movementY: raw with unadjustedMovement, else CSS pixels after acceleration |
In the browser the page may only lock from a user's press: the host asks at once while the press
that made the app ask is still recent (navigator.userActivation), else with the next press in the
canvas, Pending until then. A game that drives its own window (its own swapchain, no ui) has the
same at the window level: plat::win_pointer_lock(w, on), plat::win_pointer_lock_state(w),
plat::win_raw_motion_take(w) and the events E_POINTER_LOCK (data 1: locked) and E_RAW_MOTION
(motion waits; one event until it is taken).
Keys by where they sit. A game binds WASD to the keys at those places, whatever the layout
prints on them (Z Q S D on a French keyboard, W A S D on a German one): KeyEvent::code is the
key's place as a USB HID usage, the same on every platform, plat::KC_W and so on; scancode stays
the platform's own code (Windows' scan code, Linux's evdev code, the usage itself on macOS, iOS and
in the browser), vk and ch what the layout makes of the key. ui.key_code_down(KC_W) says
whether the key at W's place is held, as ui.key_down(vk) does for a key code; the downs and ups
themselves a canvas with HB_KEYS takes with ui.key_event_take() (section 5). plat::key_label(code) gives what the current layout prints on
the key for a settings page ("Z" for KC_W on a French keyboard; Windows, Linux and macOS, None in
the browser and on phones), plat::key_code_name(code) the browser's KeyboardEvent.code name
("KeyW"), plat::key_code_from_name the other way. Android's keys carry no place yet (code 0).
Game pads. ui.gamepads() lists the pads connected now as plat::Gamepads, in one layout on
every platform, the W3C Gamepad API's "standard" mapping: buttons[PAD_A ..= PAD_HOME] with analog
values 0 to 1 (the triggers PAD_LT / PAD_RT analog, a bit each in pressed above
PAD_TRIGGER_PRESS), axes[PAD_LX, PAD_LY, PAD_RX, PAD_RY] in -1 to 1 with right and down positive
and no dead zone, down and up for the buttons that went down and up since the last batch (a tap
between two frames is in both), index (the same while the pad stays connected), name and style
(whose labels the buttons carry: Xbox, PlayStation, Nintendo). The first call starts watching; pads
appear in later frames, every change gives a pass, and a pad that comes or goes gives
App::command(UIC_GAMEPAD, index | PADEV_CONNECT << 8) (or PADEV_DISCONNECT):
for p in ui.gamepads() {
if p.went_down(plat::PAD_A) {
self.jump();
}
let [x, y] = p.stick(0); // the left stick, raw: apply a dead zone (lib/game does it radially)
self.walk(x, y);
}
| Platform | How | Which pads |
|---|---|---|
| Windows | XInput (xinput1_4.dll, else xinput9_1_0.dll; the guide button through XInputGetStateEx), polled every 4 ms while a pad is connected, an empty slot every 250 ms | Xbox-compatible pads, at most four (index = XInput's slot) |
| Linux | evdev: the /dev/input/event* nodes with a game pad's buttons the user may read, inotify for new ones; inside a Flatpak with --device=input (from the manifest's gamepad) | Xbox (xpad), PlayStation (hid-playstation, hid-sony), Nintendo, simple HID pads |
| macOS | GameController: GCController's extendedGamepad, its connect and disconnect notifications | Xbox, PlayStation, Switch Pro, MFi pads |
| Browser | the Gamepad API, polled by the page every animation frame; a pad appears only after one of its buttons was pressed in the page | pads with the "standard" mapping; others come with mapped false, their buttons as the browser numbers them |
| Android, iOS | none yet (touch controls come later): ui.gamepads_watching() says false | – |
Pads speak only to the app that has the focus, as the keyboard does: while none of its windows is
active every pad reports nothing held. An app with a PERMISSIONS file needs the line gamepad
(else ui.gamepads stays empty and stderr says PERMDENY gamepad; ask gamepad asks at the first
call, watching starts in the frame after a yes); the pads of a test script need nothing
(PERMISSIONS.md). Rumble is not built. A game
without ui has the same at the platform's level: plat::pad_watch(true), plat::pads() once a
frame (it takes the edges) and the event E_GAMEPAD. lib/game's input actions take pads as sources
with a radial dead zone, looking at a turn rate with a curve, and pad play without the pointer lock
(GAMECORE.md).
Scripts play the lock in a headless run: ui.pointer_lock(true) locks at once, motion DX DY moves
the locked pointer, key esc lets it go; keydown CHORD code N presses the key at usage N (this
platform's scan code for it); pad connect N xbox NAME, pad press N a, pad axis N lx 0.5 and
pad disconnect N play a pad. apps/inputtest shows the lock, the summed motion, the keys by place
and the pads; its README.md has the check list for trying it by hand on each platform, and
plattest --test input and --test pad check the tables, the decoding of the raw records and each
platform's pad backend without real input.