JFLib

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() -> f64 the factor that turns this platform's raw motion into counts of a 1000-dpi mouse (the FACTOR_* constants)

  • enum Source where an input comes from

    • Key(u32) -- a key by where it sits: a USB HID usage, plat::KC_* (KeyEvent::code), whatever the layout prints
    • Mouse(u8) -- a mouse button, MOUSE_LEFT ..= MOUSE_X2 (up to 7)
    • WheelUp -- a notch of the wheel up: a press and a release at once
    • WheelDown -- a notch of the wheel down
    • MouseX -- the raw motion of the locked pointer to the right
    • MouseY -- the raw motion of the locked pointer downwards
    • Wheel -- the wheel's turn in notches, up positive
    • PadButton(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 scale
    • PadAxis(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) -> bool a motion (MouseX, MouseY, Wheel): for Motion actions; the others are pressed and released

  • fn is_pad(self) -> bool a pad's (PadButton, PadAxis, PadDir)

  • fn name(self) -> String the 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: unknown

  • fn label(self) -> String what 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) -> String label, 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, Plus

  • struct Bind a 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 from
    • scale: f64 -- what the source counts for

impl Bind

  • const fn new(source: Source) -> Bind the source with scale 1

  • const fn scaled(source: Source, scale: f64) -> Bind the source with a scale

  • const fn key(code: u32) -> Bind a key by its place (plat::KC_*) with scale 1

  • fn text(&self) -> String the 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 readable

  • enum ActionKind what an action reports

    • Button -- 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 scale
    • Motion -- 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) -> bool a 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 axis

  • struct ActionId(u16) an action of an ActionMap (its index)

impl ActionId

  • const fn index(self) -> usize the index in the map (and in TickInput)

  • struct ActionMap the actions of a game and their bindings

impl ActionMap

  • const fn new() -> ActionMap no actions

  • fn add(&mut self, name: &str, kind: ActionKind, defaults: &[Bind]) -> ActionId a 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) -> usize actions

  • fn is_empty(&self) -> bool none

  • fn ids(&self) -> impl Iterator<Item = ActionId> every action in order

  • fn find(&self, name: &str) -> Option<ActionId> the action of that name

  • fn name(&self, a: ActionId) -> &str its name

  • fn kind(&self, a: ActionId) -> ActionKind its kind

  • fn binds(&self, a: ActionId) -> &[Bind] its bindings now

  • fn defaults(&self, a: ActionId) -> &[Bind] its default bindings

  • fn bind(&mut self, a: ActionId, b: Bind) -> bool one more binding (false: the kind does not take the source, or the action has it already)

  • fn unbind(&mut self, a: ActionId, s: Source) -> bool the binding of that source goes (false: none)

  • fn set_binds(&mut self, a: ActionId, binds: &[Bind]) -> bool these 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 again

  • fn reset_all(&mut self) every action's default bindings again

  • fn 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 section section of 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) -> usize the bindings from section section of 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 counts

  • struct MouseSettings how the raw motion of the mouse turns into angles

    • sensitivity: f64 -- the multiplier of DEGREES_PER_COUNT (1: 0.022 degrees a count of the reference mouse)
    • invert_x: bool -- right turns left
    • invert_y: bool -- down looks up
    • factor: 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 factor

  • fn motion_factor(&self) -> f64 the 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 section section of 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) -> MouseSettings from section section of 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 ..= 1000

  • struct PadSettings how 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 left
    • invert_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 inverted

  • fn 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 direction

  • fn 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 signs

  • fn store(&self, ini: &mut Ini, section: &str) into section section of 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) -> PadSettings from section section (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 ..= 4

  • enum Device what the player used last (Input::device): a game shows its prompts for that

    • KeyboardMouse -- the keyboard or the mouse (also before anything was used)
    • Pad -- a game pad (Input::active_pad)
  • struct ActionState an action in a tick (TickInput) or a frame

    • down: bool -- held at the tick, or pressed since the tick before (a tap between two ticks counts once)
    • pressed: bool -- pressed since the tick before
    • released: bool -- released since the tick before
    • value: f64 -- an Axis: its value at the tick (-1 ..= 1); a Motion: the motion since the tick before; a Button: 1 when down
  • struct TickInput the actions of one tick (Input::tick), indexed by ActionId

impl TickInput

  • fn get(&self, a: ActionId) -> ActionState the state of a (all false and 0 for an action the map did not have)

  • fn down(&self, a: ActionId) -> bool a is down

  • fn pressed(&self, a: ActionId) -> bool a was pressed since the tick before

  • fn released(&self, a: ActionId) -> bool a was released since the tick before

  • fn value(&self, a: ActionId) -> f64 a's value

  • fn states(&self) -> &[ActionState] every action's state in the map's order

  • struct Input the input of a game: events in, actions out (see the module header)

impl Input

  • fn new(map: ActionMap) -> Input the input of these actions: mouse settings at their defaults, the lock required
  • fn map(&self) -> &ActionMap the actions and their bindings
  • fn edit_map<V>(&mut self, f: impl FnOnce(&mut ActionMap) -> V) -> V change 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) -> &MouseSettings the mouse settings
  • fn set_mouse(&mut self, m: MouseSettings) other mouse settings
  • fn 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 too
  • fn require_lock(&self) -> bool the lock is required (set_require_lock)
  • fn playing(&self) -> bool the 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) -> bool playing 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 it
  • fn lock(&self) -> PointerLock the 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 once
  • fn take_lock_request(&mut self) -> bool a 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) -> bool the lock ended since the last call: the game pauses (shows its menu)
  • fn pauses(&self) -> u32 how often the lock has ended so far
  • fn 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 go
  • fn key(&mut self, code: u32, down: bool) the key at usage code (plat::KC_*, KeyEvent::code) went down or up; a down while held (the system's repeat) changes nothing
  • fn key_held(&self, code: u32) -> bool the key at usage code is held
  • fn button(&mut self, b: u8, down: bool) mouse button b (MOUSE_*) went down or up. While the game needs the lock and does not hold it, a press asks for the lock (take_lock_request) and is swallowed with its release
  • fn buttons(&mut self, pressed: u32, released: u32) the buttons of a frame as ui reports them (UiInput::pressed and released: bit 1 << BTN_*): a button held before with both bits went up and down again, one not held went down and up
  • fn button_held(&self, b: u8) -> bool the mouse button b is held (not the lock's click)
  • fn 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 playing
  • fn wheel(&mut self, notches: f64) the wheel turned by notches (up positive; ui's 120 per notch is 1): Wheel motion, and a press of WheelUp / WheelDown per whole notch
  • fn axis(&self, a: ActionId) -> f64 an Axis' value now: the scales of its sources held, kept in -1 ..= 1 (a Button: 1 when held)
  • fn down(&self, a: ActionId) -> bool a is held now (a source bound to it is held, while playing)
  • fn pressed(&self, a: ActionId) -> bool a was pressed in this frame (since end_frame)
  • fn released(&self, a: ActionId) -> bool a was released in this frame
  • fn motion_of(&self, a: ActionId) -> f64 a 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) -> TickInput the 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 meanwhile
  • fn take_capture(&mut self) -> Option<Source> the source pressed since start_capture (the capture ends with it); None: none yet, or no capture
  • fn capturing(&self) -> bool a capture is running (start_capture, nothing pressed yet)
  • fn plat_event(&mut self, ev: &plat::Event) -> bool an 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 these
  • fn store(&self, ini: &mut Ini, section: &str) the bindings, the mouse and the pad settings into section section of 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) -> usize the bindings, the mouse and the pad settings from section section (Self::store) -> the bindings that could not be read
  • fn pad_settings(&self) -> &PadSettings the pad settings
  • fn 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 used
  • fn 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 again
  • fn pinned_pad(&self) -> Option<u8> the pad pinned (pin_pad)
  • fn device(&self) -> Device what the player used last: a key or the mouse, or a pad
  • fn pad_held(&self, b: usize) -> bool the 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) -> f64 the active pad's stick axis a (plat::PAD_LX ..) after the dead zone
  • fn 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)

Source: docs/API.md in the JFLib repository.