game/src/input.rs#
Source: lib/game/src/input.rs
input -- named actions bound to keys by where they sit, mouse buttons, the wheel, the raw motion of
the locked pointer and game pads; sensitivity, inversion and a factor per platform for the motion; the
pointer lock's click and its end; the pad's dead zone, turn rate and curve; bindings and settings in a
settings file
Source where an input comes from: Key(KC_*: the key's place, KeyEvent::code, never its
character), Mouse(MOUSE_*), WheelUp / WheelDown (a notch as a press), MouseX /
MouseY (raw motion), Wheel (the turn in notches), PadButton(plat::PAD_*), PadAxis
(a stick's axis), PadDir (a stick's direction as a button); name / parse for the
file, label for a menu (plat::key_label: what the layout prints on the key;
label_for: a pad's labels by its style)
ActionMap actions (name, ActionKind, bindings and their defaults); store / load in an Ini
ActionKind Button (down, pressed, released), Axis (keys with a scale each, a pad's analog
values, -1 ..= 1), Motion (raw motion: an angle in radians, the wheel in notches,
a stick turning at a rate)
MouseSettings sensitivity, invert_x, invert_y, factor (None: the platform's,
platform_motion_factor); store / load in an Ini
PadSettings sensitivity (of TURN_RATE), dead_zone (radial), curve, invert_x, invert_y; store /
load in the same section
Input takes the events (key, button, buttons, motion, wheel, set_lock, plat_event) and
the pads' snapshots (pad_frame) and answers per frame (down, pressed, released,
axis, motion; end_frame) and per tick (tick() -> TickInput); the lock:
take_lock_request, playing, take_pause; the pad: active_pad, pin_pad, device
The pointer lock (docs/GAME.md, the user's decisions of 2026-11-18). While the game needs the lock
(require_lock, the default) and does not hold it, no action fires: a mouse press asks for the lock
(take_lock_request -> the app calls ui.pointer_lock(true) in the same frame: the browser grants it only
from a press) and is swallowed with its release, so the click that starts playing never fires. Esc
belongs to the lock and never reaches the game: the game only sees the lock end (set_lock with
ui.pointer_lock_state(), or E_POINTER_LOCK) and takes that as its pause (take_pause): every held input
is let go, the actions report their release. A platform without a lock (Unsupported: phones) plays
without one.
Motion units. ui.pointer_motion / plat::win_raw_motion_take report what each platform has: the
mouse's counts (Windows, X11), libinput's unaccelerated motion normalised to 1000 dpi (Wayland),
points after the system's acceleration (macOS), raw counts with unadjustedMovement or CSS pixels after
the acceleration (the browser). The reference unit is a count of a 1000-dpi mouse; the factor of a
platform (platform_motion_factor) turns its unit into that, MouseSettings::factor overrides it. A
Motion action's value is then counts x factor x sensitivity x DEGREES_PER_COUNT (0.022 degrees: Quake's
and Source's m_yaw, so sensitivities read as in those games) in radians, the sign flipped by invert_x /
invert_y; a wheel's motion is in notches (no sensitivity).
Frames and ticks. Events come with the frames, the simulation runs in ticks. Every action counts
its presses and releases and sums its motion twice: for the frame (end_frame clears them: the camera
turns per frame) and for the tick (tick() takes them: a press between two ticks reaches the next tick
even when the frame had none, a press and a release between two ticks make one tick with down,
pressed and released). An Axis reads the keys held at the moment it is asked.
Game pads (G7a part 2). pad_frame takes the pads of a frame (ui.gamepads(), plat::pads()) and the
time since the frame before. One pad drives a single player's input: the one used last (a button
pressed or a stick out of its dead zone; the first such pad, and any other that is used, takes over),
or the one pin_pad names (a split screen: an Input per pad). Its buttons press and release like keys
(a tap between two frames is a press and a release: the snapshots carry their edges); its sticks go
through a radial dead zone (PadSettings::dead_zone: nearer the middle is rest, the rest of the way
scales to 0 ..= 1); an Axis adds a stick's value (or a trigger's) times the binding's scale to its
keys', a Motion turns by TURN_RATE x sensitivity degrees a second at full deflection, the deflection
raised to PadSettings::curve (fine aim near the middle), times the frame's time. A pad plays without
the pointer lock: a pad button pressed while the game does not play starts playing (pad play) and is
swallowed with its release, as the lock's click; the mouse still asks for the lock with its first
press, the lock taken ends pad play, its end pauses as before. The active pad going away during pad
play pauses. After a pause a pad button held counts only once it was let go. device() says what was
used last (prompts for keys or for a pad).
/// the left mouse button (plat::BTN_LEFT, ui::BTN_LEFT)
MOUSE_LEFT: u8 = 1
/// the right mouse button
MOUSE_RIGHT: u8 = 2
/// the middle mouse button (the wheel pressed)
MOUSE_MIDDLE: u8 = 3
/// the first side button (back)
MOUSE_X1: u8 = 4
/// the second side button (forward)
MOUSE_X2: u8 = 5
/// the angle one count of the reference mouse turns at sensitivity 1 (degrees): Quake's and Source's
/// m_yaw, so that sensitivities read as in those games
DEGREES_PER_COUNT: f64 = 0.022
/// the factor of Windows' motion: the mouse's counts (Raw Input) are the reference unit
FACTOR_WINDOWS: f64 = 1.0
/// the factor of Linux' motion: X11's raw counts and Wayland's 1000-dpi normalised motion are the
/// reference unit
FACTOR_LINUX: f64 = 1.0
/// the factor of macOS' motion: points after the system's acceleration. At a moderate speed and the
/// default tracking speed a 1000-dpi mouse makes about 0.4 points per count (the system takes a mouse for
/// 400 dpi and moves the pointer 96 points per inch at gain 1, about 1.7 at that speed): an estimate,
/// checked by hand in G7b part 2
FACTOR_MACOS: f64 = 2.5
/// the factor of the browser's motion: raw counts with unadjustedMovement (Chrome on Windows and
/// macOS), else CSS pixels after the system's acceleration, about one per count at a moderate speed
FACTOR_BROWSER: f64 = 1.0
/// the factor where the pointer cannot be locked (Android, iOS): no motion comes
FACTOR_OTHER: f64 = 1.0
/// the degrees per second a stick turns at full deflection and pad sensitivity 1 (PadSettings)
TURN_RATE: f64 = 180.0
/// a stick's direction (Source::PadDir) is held from this far on and let go below PAD_DIR_RELEASE
PAD_DIR_PRESS: f64 = 0.5
/// below this a held stick direction is let go
PAD_DIR_RELEASE: f64 = 0.4
const fn platform_motion_factor() -> f64the factor that turns this platform's raw motion into counts of a 1000-dpi mouse (the FACTOR_* constants)enum Sourcewhere an input comes fromKey(u32)-- a key by where it sits: a USB HID usage, plat::KC_* (KeyEvent::code), whatever the layout printsMouse(u8)-- a mouse button, MOUSE_LEFT ..= MOUSE_X2 (up to 7)WheelUp-- a notch of the wheel up: a press and a release at onceWheelDown-- a notch of the wheel downMouseX-- the raw motion of the locked pointer to the rightMouseY-- the raw motion of the locked pointer downwardsWheel-- the wheel's turn in notches, up positivePadButton(u8)-- a pad's button of the standard layout (plat::PAD_A ..= plat::PAD_HOME): pressed and released; in an Axis its analog value (a trigger's 0 ..= 1) times the scalePadAxis(u8)-- a pad's stick axis (plat::PAD_LX, PAD_LY, PAD_RX, PAD_RY: right and down positive) after the radial dead zone: in an Axis its value -1 ..= 1 times the scale, in a Motion it turns (looking: TURN_RATE, PadSettings)PadDir(u8, bool)-- one direction of a stick axis as a button (true: right / down), held from PAD_DIR_PRESS on; in an Axis its value 0 ..= 1
impl Source
fn is_motion(self) -> boola motion (MouseX, MouseY, Wheel): for Motion actions; the others are pressed and releasedfn is_pad(self) -> boola pad's (PadButton, PadAxis, PadDir)fn name(self) -> Stringthe name in a settings file, the same on every platform: a key's KeyboardEvent.code name ("KeyW", "ShiftLeft"; "Code0x99" without one), "Mouse1" .. "Mouse7", "WheelUp", "WheelDown", "MouseX", "MouseY", "Wheel"; a pad's "PadA" .. "PadHome" (plat::pad_button_name), "PadLeftX", "PadLeftY", "PadRightX", "PadRightY", a direction "PadLeftX+" (right / down) or "PadLeftX-"fn parse(s: &str) -> Option<Source>the source of a name (Source::name); None: unknownfn label(self) -> Stringwhat a menu shows: for a key what the current layout prints on it (plat::key_label: "Z" for KC_W on a French keyboard), where the platform does not say (the browser, phones) the name without "Key" / "Digit"; for the mouse and the pads German texts an app translates with its catalogue (ui::i18n::t); a pad's buttons as on an Xbox pad (label_for: by the pad's style)fn label_for(self, style: PadStyle) -> Stringlabel, a pad's buttons as a pad of that style prints them (plat::Gamepad::style): Xbox (also for pads of no known style) A B X Y, LB RB LT RT, Ansicht, Menü; PlayStation Kreuz, Kreis, Quadrat, Dreieck, L1 R1 L2 R2, Create, Options, L3 R3; Nintendo by the places B A Y X, L R ZL ZR, Minus, Plusstruct Binda binding: a source and its scale (an Axis: +1 / -1 for the two directions; a Motion: a multiplier, -1 turns it round)source: Source-- where the input comes fromscale: f64-- what the source counts for
impl Bind
const fn new(source: Source) -> Bindthe source with scale 1const fn scaled(source: Source, scale: f64) -> Bindthe source with a scaleconst fn key(code: u32) -> Binda key by its place (plat::KC_*) with scale 1fn text(&self) -> Stringthe text of the binding in a settings file: the source's name, "-" before it for scale -1, "*SCALE" after it for another scale ("KeyD", "-KeyA", "MouseY*0.5")fn parse(s: &str) -> Option<Bind>the binding of a text (Bind::text); None: not readableenum ActionKindwhat an action reportsButton-- pressed and released (fire, jump, use): sources that press (keys, mouse buttons, wheel notches)Axis-- a value in -1 ..= 1 from the sources held, each with its scale (walking forward and back from W and S), a pad's sticks and triggers with their analog values times the scaleMotion-- raw motion summed (looking around): MouseX, MouseY (radians), Wheel (notches), a stick's axis (PadAxis: radians, turning at a rate)
impl ActionKind
fn takes(self, s: Source) -> boola source this kind takes: a Button what presses (keys, mouse buttons, wheel notches, pad buttons and stick directions), an Axis those and a stick's axis, a Motion the mouse's motion, the wheel and a stick's axisstruct ActionId(u16)an action of an ActionMap (its index)
impl ActionId
const fn index(self) -> usizethe index in the map (and in TickInput)struct ActionMapthe actions of a game and their bindings
impl ActionMap
const fn new() -> ActionMapno actionsfn add(&mut self, name: &str, kind: ActionKind, defaults: &[Bind]) -> ActionIda new action with its default bindings (bindings of a source the kind does not take are left out). Panics for a name used before or one that is not ASCII letters, digits, '_', '-', '.' (at most 64)fn len(&self) -> usizeactionsfn is_empty(&self) -> boolnonefn ids(&self) -> impl Iterator<Item = ActionId>every action in orderfn find(&self, name: &str) -> Option<ActionId>the action of that namefn name(&self, a: ActionId) -> &strits namefn kind(&self, a: ActionId) -> ActionKindits kindfn binds(&self, a: ActionId) -> &[Bind]its bindings nowfn defaults(&self, a: ActionId) -> &[Bind]its default bindingsfn bind(&mut self, a: ActionId, b: Bind) -> boolone more binding (false: the kind does not take the source, or the action has it already)fn unbind(&mut self, a: ActionId, s: Source) -> boolthe binding of that source goes (false: none)fn set_binds(&mut self, a: ActionId, binds: &[Bind]) -> boolthese bindings instead (false: one of them the kind does not take or one source twice; nothing changes then)fn reset(&mut self, a: ActionId)its default bindings againfn reset_all(&mut self)every action's default bindings againfn users(&self, s: Source) -> Vec<ActionId>the actions a source drives (two actions on one key: a settings page shows the clash)fn store(&self, ini: &mut Ini, section: &str)the bindings into sectionsectionof a settings file: a line "bind.NAME=..." (the bindings' texts separated by blanks, "none" for none) for every action whose bindings differ from its defaults; the line of one that has its defaults goes (a new default reaches who never changed it)fn load(&mut self, ini: &Ini, section: &str) -> usizethe bindings from sectionsectionof a settings file (Self::store): an action without a line gets its defaults -> the bindings that could not be read (an unknown source, a source the kind does not take, a source twice): left out, the rest of the line countsstruct MouseSettingshow the raw motion of the mouse turns into anglessensitivity: f64-- the multiplier of DEGREES_PER_COUNT (1: 0.022 degrees a count of the reference mouse)invert_x: bool-- right turns leftinvert_y: bool-- down looks upfactor: Option<f64>-- the factor from this platform's units to counts of a 1000-dpi mouse; None: the platform's (platform_motion_factor)
impl MouseSettings
const DEFAULT: MouseSettings = MouseSettings { sensitivity: 1.0, invert_x: false, invert ...sensitivity 1, nothing inverted, the platform's factorfn motion_factor(&self) -> f64the factor in use (factor, else the platform's)fn radians_per_unit(&self) -> (f64, f64)radians per raw unit of this platform along x and y (the sign of the inversion included)fn store(&self, ini: &mut Ini, section: &str)into sectionsectionof a settings file: "sensitivity", "invert_x", "invert_y", "motion_factor", each only when it differs from the default (else its line goes)fn load(ini: &Ini, section: &str) -> MouseSettingsfrom sectionsectionof a settings file (Self::store): what is missing or unreadable is the default, the sensitivity is kept in 0.001 ..= 100, a factor in 0.001 ..= 1000struct PadSettingshow a pad's sticks turn into values and angles (the sticks of every pad of the input)sensitivity: f64-- the multiplier of TURN_RATE (1: 180 degrees a second at full deflection)dead_zone: f64-- the radial dead zone of both sticks, 0 ..= 0.9: nearer the middle a stick is at rest, the rest of its way scales to 0 ..= 1 (beyond 1, a square stick's corners: 1)curve: f64-- the response curve of looking, 1 ..= 4: the deflection after the dead zone to this power (1 linear; 2, the default, turns slowly near the middle for fine aim and fast at the end)invert_x: bool-- right turns leftinvert_y: bool-- down looks up
impl PadSettings
const DEFAULT: PadSettings = PadSettings { sensitivity: 1.0, dead_zone: 0.15, curve: 2 ...sensitivity 1, dead zone 0.15, curve 2, nothing invertedfn stick(&self, x: f64, y: f64) -> (f64, f64)a stick (x, y as the pad reports it, -1 ..= 1) after the radial dead zone: (0, 0) up to dead_zone from the middle, beyond it the way that is left scaled to 0 ..= 1 in the same directionfn look(&self, x: f64, y: f64) -> (f64, f64)a stick's turn in radians per second (x right, y down): the dead zone, the curve on the deflection, TURN_RATE x sensitivity at full deflection, the inversion's signsfn store(&self, ini: &mut Ini, section: &str)into sectionsectionof a settings file: "pad_sensitivity", "pad_dead_zone", "pad_curve", "pad_invert_x", "pad_invert_y", each only when it differs from the default (else its line goes)fn load(ini: &Ini, section: &str) -> PadSettingsfrom sectionsection(Self::store): what is missing or unreadable is the default; the sensitivity kept in 0.001 ..= 100, the dead zone in 0 ..= 0.9, the curve in 1 ..= 4enum Devicewhat the player used last (Input::device): a game shows its prompts for thatKeyboardMouse-- the keyboard or the mouse (also before anything was used)Pad-- a game pad (Input::active_pad)
struct ActionStatean action in a tick (TickInput) or a framedown: bool-- held at the tick, or pressed since the tick before (a tap between two ticks counts once)pressed: bool-- pressed since the tick beforereleased: bool-- released since the tick beforevalue: f64-- an Axis: its value at the tick (-1 ..= 1); a Motion: the motion since the tick before; a Button: 1 when down
struct TickInputthe actions of one tick (Input::tick), indexed by ActionId
impl TickInput
fn get(&self, a: ActionId) -> ActionStatethe state of a (all false and 0 for an action the map did not have)fn down(&self, a: ActionId) -> boola is downfn pressed(&self, a: ActionId) -> boola was pressed since the tick beforefn released(&self, a: ActionId) -> boola was released since the tick beforefn value(&self, a: ActionId) -> f64a's valuefn states(&self) -> &[ActionState]every action's state in the map's orderstruct Inputthe input of a game: events in, actions out (see the module header)
impl Input
fn new(map: ActionMap) -> Inputthe input of these actions: mouse settings at their defaults, the lock requiredfn map(&self) -> &ActionMapthe actions and their bindingsfn edit_map<V>(&mut self, f: impl FnOnce(&mut ActionMap) -> V) -> Vchange the actions or their bindings (a settings page, ActionMap::load); afterwards every action is held as its sources are (a binding that came or went presses or releases it)fn mouse(&self) -> &MouseSettingsthe mouse settingsfn set_mouse(&mut self, m: MouseSettings)other mouse settingsfn set_require_lock(&mut self, on: bool)whether the actions need the pointer lock (true, the default: a game that looks with the mouse); false: actions always fire, clicks toofn require_lock(&self) -> boolthe lock is required (set_require_lock)fn playing(&self) -> boolthe actions fire: the lock is held, or not required, or the platform has none (Unsupported), or the player plays with a pad (pad_playing)fn pad_playing(&self) -> boolplaying with a pad without the pointer lock: a pad button pressed while the game did not play started it (that press fired nothing); the lock taken or the pad gone ends itfn lock(&self) -> PointerLockthe state of the lock as the input knows it (set_lock)fn set_lock(&mut self, state: PointerLock)the state of the pointer lock now (ui.pointer_lock_state() every frame, or E_POINTER_LOCK through plat_event). Its end (from Locked to anything else) pauses: every held input is let go, the actions report their release, take_pause says so oncefn take_lock_request(&mut self) -> boola press asked for the lock since the last call: the app asks for it now (ui.pointer_lock(true), in the frame of the press)fn take_pause(&mut self) -> boolthe lock ended since the last call: the game pauses (shows its menu)fn pauses(&self) -> u32how often the lock has ended so farfn release_all(&mut self)every held key and button is let go (the actions report their release); motion not yet taken goes; a pad button held counts again only after it was let gofn key(&mut self, code: u32, down: bool)the key at usagecode(plat::KC_*, KeyEvent::code) went down or up; a down while held (the system's repeat) changes nothingfn key_held(&self, code: u32) -> boolthe key at usagecodeis heldfn motion(&mut self, dx: f64, dy: f64)raw motion of the locked pointer (ui.pointer_motion(), plat::win_raw_motion_take): right and down positive, in this platform's units; nothing while not playingfn wheel(&mut self, notches: f64)the wheel turned bynotches(up positive; ui's 120 per notch is 1): Wheel motion, and a press of WheelUp / WheelDown per whole notchfn axis(&self, a: ActionId) -> f64an Axis' value now: the scales of its sources held, kept in -1 ..= 1 (a Button: 1 when held)fn down(&self, a: ActionId) -> boola is held now (a source bound to it is held, while playing)fn pressed(&self, a: ActionId) -> boola was pressed in this frame (since end_frame)fn released(&self, a: ActionId) -> boola was released in this framefn motion_of(&self, a: ActionId) -> f64a Motion's motion in this frame (radians for the mouse, notches for the wheel)fn end_frame(&mut self)the frame is over: its presses, releases and motion are cleared (the tick's stay for tick())fn tick(&mut self) -> TickInputthe actions for a tick, and the presses, releases and motion since the tick before are taken (the next tick starts from nothing)fn start_capture(&mut self)the next key, mouse button or wheel notch pressed is not an input but the answer to a settings page's "press a key" (take_capture); the actions keep their state meanwhilefn take_capture(&mut self) -> Option<Source>the source pressed since start_capture (the capture ends with it); None: none yet, or no capturefn capturing(&self) -> boola capture is running (start_capture, nothing pressed yet)fn plat_event(&mut self, ev: &plat::Event) -> boolan event of plat's window (a game with its own window and swapchain, without ui): keys (E_KEY_DOWN / E_KEY_UP by plat::key_code of their scancode), buttons (E_DOWN / E_UP), the wheel (E_WHEEL), the lock (E_POINTER_LOCK: data 1 Locked, else plat::win_pointer_lock_state of the window), its motion (E_RAW_MOTION: plat::win_raw_motion_take of the window), E_FOCUS_OUT lets everything go -> the event was one of thesefn store(&self, ini: &mut Ini, section: &str)the bindings, the mouse and the pad settings into sectionsectionof a settings file (ActionMap::store, MouseSettings::store, PadSettings::store): with base::Settings,input.store(settings.ini_mut(), "input")fn load(&mut self, ini: &Ini, section: &str) -> usizethe bindings, the mouse and the pad settings from sectionsection(Self::store) -> the bindings that could not be readfn pad_settings(&self) -> &PadSettingsthe pad settingsfn set_pad_settings(&mut self, s: PadSettings)other pad settings (from the next pad_frame on)fn active_pad(&self) -> Option<u8>the pad that drives the actions (plat::Gamepad::index): the one used last, or the pinned one; None before any pad was usedfn pin_pad(&mut self, index: Option<u8>)only this pad drives the input from now on (an Input per player in a split screen); None: the one used last againfn pinned_pad(&self) -> Option<u8>the pad pinned (pin_pad)fn device(&self) -> Devicewhat the player used last: a key or the mouse, or a padfn pad_held(&self, b: usize) -> boolthe active pad's button b (plat::PAD_*) is held as the input counts it (not one that waits for its release after a pause, not the press that started pad play)fn pad_stick(&self, a: usize) -> f64the active pad's stick axis a (plat::PAD_LX ..) after the dead zonefn pad_frame(&mut self, pads: &[Gamepad], dt: f64)the pads of this frame (ui.gamepads(), plat::pads(): the connected ones with the edges of their buttons) and the seconds since the frame before (a stick's turn is its rate times that; kept in 0 ..= 0.25). The pad used last drives the actions (or the pinned one); a pad button pressed while the game does not play starts pad play and fires nothing, nor does its release; the active pad gone during pad play pauses. Call it once a frame, before reading the actions, also without pads (an empty slice lets the active pad go)