JFLib

plat/pad.rs#

Source: lib/plat/pad.rs

pad -- (G7a part 2) game pads: one snapshot per pad in the W3C standard layout and their connect
and disconnect events, the part every platform shares

  pad_watch(on) -> bool      start (on) or stop watching for pads -> false: this platform has none
                             (Android, iOS: a later stage with touch controls), the system lacks
                             the means (Windows without an XInput DLL) or the app's manifest has
                             no `gamepad` line (PERMDENY gamepad; docs/PERMISSIONS.md); nothing
                             runs before
  pad_watching() -> bool     watching now
  pads() -> Vec<Gamepad>     the connected pads by index, with the buttons that went down and up
                             since the last call; takes the E_GAMEPAD that waits
  pad(i) -> Gamepad          slot i as it stands (connected or not), edges left in place
  E_GAMEPAD                  data = index | PADEV_* << 8: PADEV_STATE (a pad's state changed: one
                             event until pads() is called, whichever pads change meanwhile),
                             PADEV_CONNECT, PADEV_DISCONNECT (each its own event)

The layout is the W3C Gamepad API's "standard" mapping, the same on every platform: buttons
PAD_A (bottom face button: Xbox A, PlayStation cross, Nintendo B) .. PAD_HOME with analog values
0 ..= 1 (the triggers PAD_LT / PAD_RT analog, the others 0 or 1 on most pads) and a bit each in
Gamepad::pressed (a trigger counts as pressed above PAD_TRIGGER_PRESS, XInput's threshold); two
sticks PAD_LX, PAD_LY, PAD_RX, PAD_RY in -1 ..= 1, right and down positive, raw: no dead zone
(lib/game applies a radial one).  Gamepad::style says whose labels the buttons carry.

Per platform:
  Windows  XInput (Xbox-compatible pads, at most four, index = XInput's slot): xinput1_4.dll,
           else xinput9_1_0.dll, loaded at the first pad_watch(true); its ordinal 100
           (XInputGetStateEx) where it has it, for the guide button (win_pad.rs)
  Linux    evdev: the /dev/input/event* nodes the user may read (udev gives the user of the
           seat game controllers, not keyboards) with the buttons of a game pad (BTN_SOUTH) and
           two axes; the kernel's gamepad codes mapped to the layout, Sony's and Nintendo's
           north / west buttons the other way round than xpad's; hotplug through inotify on
           /dev/input (linux_pad.rs).  Inside a Flatpak only with --device=input (the
           manifest's `gamepad` line gives it; Flatpak 1.15.6 and newer)
  macOS    GameController: GCController's extendedGamepad, connect and disconnect
           notifications (apple_mac_pad.rs)
  browser  the Gamepad API's pads with the "standard" mapping (others pass their first buttons
           and axes as the browser numbers them, mapped false): the page polls
           navigator.getGamepads() every animation frame while the app watches and hands each
           change to the worker (jfgui_pad; loader.js).  The browser shows a pad only after a
           button of it was pressed in the page
  Android, iOS  Unsupported: pad_watch says false

The native backends are polled from the window loop's wait (pad_wait): every 4 ms while a pad is
connected, new pads every 250 ms (Windows: one empty slot each time, XInput is slow for empty
ones; Linux: what inotify reported; macOS: its notifications, once a second a look at the list).
A change of a button wakes the app at once, an axis only when it moved by 1/256 or more since
the last event (a stick at rest jitters); the snapshot always has the latest values.  Pads speak
only to the app that has the focus, as the keyboard does: while none of its windows is active
they report nothing held and their sticks at rest (still connected).
/// slots of pads (Gamepad::index is below this)
PAD_MAX: usize = 8
/// buttons of the standard layout (Gamepad::buttons)
PAD_BUTTONS: usize = 17
/// axes of the standard layout (Gamepad::axes)
PAD_AXES: usize = 4
/// the bottom face button (Xbox A, PlayStation cross, Nintendo B)
PAD_A: usize = 0
/// the right face button (Xbox B, PlayStation circle, Nintendo A)
PAD_B: usize = 1
/// the left face button (Xbox X, PlayStation square, Nintendo Y)
PAD_X: usize = 2
/// the top face button (Xbox Y, PlayStation triangle, Nintendo X)
PAD_Y: usize = 3
/// the left shoulder button (LB, L1, L)
PAD_LB: usize = 4
/// the right shoulder button (RB, R1, R)
PAD_RB: usize = 5
/// the left trigger, analog (LT, L2, ZL)
PAD_LT: usize = 6
/// the right trigger, analog (RT, R2, ZR)
PAD_RT: usize = 7
/// the left middle button (View / Back, Share / Create, minus)
PAD_BACK: usize = 8
/// the right middle button (Menu / Start, Options, plus)
PAD_START: usize = 9
/// the left stick pressed (LS, L3)
PAD_LS: usize = 10
/// the right stick pressed (RS, R3)
PAD_RS: usize = 11
/// the directional pad up
PAD_UP: usize = 12
/// the directional pad down
PAD_DOWN: usize = 13
/// the directional pad left
PAD_LEFT: usize = 14
/// the directional pad right
PAD_RIGHT: usize = 15
/// the middle button of the system (Xbox, PS, Home)
PAD_HOME: usize = 16
/// the left stick to the right (Gamepad::axes)
PAD_LX: usize = 0
/// the left stick down
PAD_LY: usize = 1
/// the right stick to the right
PAD_RX: usize = 2
/// the right stick down
PAD_RY: usize = 3
/// E_GAMEPAD's kind (data >> 8): a pad's state changed (one event until pads() is called)
PADEV_STATE: u32 = 0
/// E_GAMEPAD's kind: pad data & 0xFF came
PADEV_CONNECT: u32 = 1
/// E_GAMEPAD's kind: pad data & 0xFF went
PADEV_DISCONNECT: u32 = 2
/// the value above which a trigger counts as pressed (XInput's XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
PAD_TRIGGER_PRESS: f32 = 30.0 / 255.0
  • enum PadStyle whose labels a pad's buttons carry (the layout is the same for all)

    • Other -- not known
    • Xbox -- Xbox and XInput pads: A B X Y, LB RB LT RT, View, Menu
    • PlayStation -- PlayStation: cross, circle, square, triangle, L1 R1 L2 R2, Share / Create, Options
    • Nintendo -- Nintendo: B A Y X by their places (the bottom button is B), L R ZL ZR, minus, plus
  • struct PadState one pad's state at a moment, in the standard layout

    • buttons: [f32; PAD_BUTTONS] -- the buttons, 0 ..= 1 (PAD_A ..= PAD_HOME)
    • pressed: u32 -- the buttons held (bit PAD_*)
    • axes: [f32; PAD_AXES] -- PAD_LX, PAD_LY, PAD_RX, PAD_RY: -1 ..= 1, right and down positive

impl PadState

  • const NEUTRAL: PadState = PadState { buttons: [0.0; PAD_BUTTONS], pressed: 0, axes: ... nothing held, the sticks at rest

  • fn held(&self, b: usize) -> bool button b (PAD_*) is held

  • struct Gamepad a pad as plat::pads reports it

    • index: u8 -- its slot, the same while it stays connected (0 .. PAD_MAX; Windows: XInput's slot, the browser: the Gamepad API's index)
    • connected: bool -- connected (pads() lists only connected ones; pad(i) of an empty slot: false)
    • name: String -- the system's name for it ("Xbox Controller (XInput)", "Sony Interactive Entertainment Wireless Controller", the browser's id)
    • style: PadStyle -- whose labels its buttons carry
    • mapped: bool -- its buttons and axes are the standard layout (false: a browser pad without the "standard" mapping: its first buttons and axes as the browser numbers them)
    • buttons: [f32; PAD_BUTTONS] -- the standard layout's buttons, 0 ..= 1 (PAD_A ..= PAD_HOME; the triggers analog)
    • pressed: u32 -- the buttons held now (bit PAD_*)
    • down: u32 -- the buttons that went down since the last plat::pads (a tap between two reads is in down and up, but not in pressed)
    • up: u32 -- the buttons that went up since the last plat::pads
    • axes: [f32; PAD_AXES] -- PAD_LX, PAD_LY, PAD_RX, PAD_RY: -1 ..= 1, right and down positive, raw (no dead zone)

impl Gamepad

  • const fn empty(i: u8) -> Gamepad slot i, empty: not connected, nothing held

  • fn held(&self, b: usize) -> bool button b (PAD_*) is held

  • fn went_down(&self, b: usize) -> bool button b went down since the last read

  • fn went_up(&self, b: usize) -> bool button b went up since the last read

  • fn value(&self, b: usize) -> f32 button b's value, 0 ..= 1 (0 for another index)

  • fn axis(&self, a: usize) -> f32 axis a's value (PAD_LX ..), -1 ..= 1

  • fn stick(&self, s: usize) -> [f32; 2] stick s (0 left, 1 right): [x, y], right and down positive

  • fn trigger(&self, t: usize) -> f32 trigger t (0 left, 1 right), 0 ..= 1

  • fn state(&self) -> PadState the state alone

  • fn pad_button_name(b: usize) -> &'static str the short English name of button b of the standard layout ("A", "LB", "Up", "Home"; "" for another index): what a test or a log shows (lib/game has the labels for menus)

  • fn pad_style(vendor: u16, name: &str) -> PadStyle whose labels a pad has, from its USB vendor (0: not known) and its name

  • fn pad_vendor_of_id(id: &str) -> u16 the USB vendor in a browser's id of a pad: Chrome's "... (STANDARD GAMEPAD Vendor: 054c Product: 0ce6)", Firefox's "054c-0ce6-DualSense Wireless Controller" (0: none)

  • fn pad_watch(on: bool) -> bool start (on) or stop watching for game pads -> false: this platform has no pads (Android, iOS), the system lacks the means (Windows without an XInput DLL) or the app's manifest has no gamepad line (docs/PERMISSIONS.md: PERMDENY gamepad; nothing is opened then). While watching, pads come and go with E_GAMEPAD events and plat::pads has them; stopping lets every pad go (PADEV_DISCONNECT). The browser shows a pad only after one of its buttons was pressed in the page

  • fn pad_watching() -> bool pads are being watched (pad_watch)

  • fn pads() -> Vec<Gamepad> the connected pads by index, each with the buttons that went down and up since the last call (which go then), and the E_GAMEPAD that waits (the next change sends a new one). Read it once per frame (ui does: ui.gamepads)

  • fn pad(i: usize) -> Gamepad slot i as it stands (Gamepad::connected false when empty), its edges left for pads()

the browser

/// (browser) ask the page to watch the pads (one byte, 1 on / 0 off): it polls
/// navigator.getGamepads() every animation frame while one is connected and hands changes over
/// with jfgui_pad
REQUEST_GAMEPAD: u32 = 33
  • fn web_pad_decode(rec: &[u8]) -> Option<(usize, bool, bool, PadState, String)> (G7a) the record of a pad the page hands over (jfgui_pad): u32 index, u32 flags (bit 0 connected, bit 1 the "standard" mapping), u32 the buttons pressed, f32 x 17 buttons, f32 x 4 axes, then the browser's id in UTF-8 -> (index, connected, mapped, state, id); None for a short record or an index beyond PAD_MAX

  • extern "C" fn jfgui_pad(len: u32) -> u32 (browser) a pad's state from the page (web_pad_decode's record in the inbox) -> 1 taken

  • fn xinput_map(buttons: u16, lt: u8, rt: u8, lx: i16, ly: i16, rx: i16, ry: i16) -> PadState (G7a) an XINPUT_GAMEPAD in the standard layout: wButtons, the triggers 0..255 (pressed above 30, XINPUT_GAMEPAD_TRIGGER_THRESHOLD), the thumbs -32768..32767 with up positive (the layout has down positive: y turned round)

  • fn xinput_decode(rec: &[u8]) -> Option<(u32, PadState)> (G7a) an XINPUT_STATE as XInputGetState fills it (16 bytes: dwPacketNumber, then XINPUT_GAMEPAD) -> (packet number, the state in the layout); None for a short record

/// struct input_event on 64-bit Linux: struct timeval (16 bytes), type, code, value
EVDEV_EVENT_SIZE: usize = 24
/// the key codes a pad's bitmap covers (KEY_MAX + 1)
EVDEV_KEYS: usize = 0x300
/// the absolute axes (ABS_MAX + 1)
EVDEV_ABS: usize = 0x40
/// the absolute axes a pad may map (ABS_X ..= ABS_HAT0Y)
EVDEV_ABS_MAPPED: usize = 18
  • fn evdev_is_pad(types: &[u8], keys: &[u8], abs: &[u8]) -> bool (G7a) an evdev node is a game pad: its event types (EVIOCGBIT(0)), keys (EVIOCGBIT(EV_KEY)) and absolute axes (EVIOCGBIT(EV_ABS)) as the kernel's bitmaps -> it has keys, absolute axes, the gamepad's first button (BTN_SOUTH, alias BTN_GAMEPAD) and a stick (ABS_X, ABS_Y). A pad's touchpad and motion sensors (their own nodes) have no BTN_SOUTH

  • struct AbsInfo an absolute axis as the kernel describes it (EVIOCGABS: value, minimum, maximum)

    • code: u16 -- ABS_*
    • value: i32 -- its value now
    • min: i32
    • max: i32
  • struct EvdevPad (G7a) one evdev game pad: its codes mapped to the standard layout, the events of the node in, its state out. The kernel's gamepad codes (Documentation/input/gamepad.rst): BTN_SOUTH and BTN_EAST are A and B; xpad (Xbox pads and most others) reports X as BTN_NORTH (= BTN_X) and Y as BTN_WEST (= BTN_Y), Sony's and Nintendo's drivers by the places (north the top button): the vendor decides. Sticks ABS_X / ABS_Y and ABS_RX / ABS_RY; triggers ABS_Z / ABS_RZ (without ABS_RX / ABS_RY those are the right stick, as on simple HID pads), else ABS_BRAKE / ABS_GAS, else BTN_TL2 / BTN_TR2 digital; the directional pad ABS_HAT0X / ABS_HAT0Y or BTN_DPAD_*

impl EvdevPad

  • fn new(vendor: u16, keys: &[u8], abs: &[AbsInfo]) -> EvdevPad a pad with these keys (EVIOCGBIT(EV_KEY)'s bitmap) and absolute axes (EVIOCGABS of each one it has) of this USB vendor (0x054C Sony, 0x057E Nintendo: north / west by place)

  • fn state(&self) -> PadState the state of the last complete report (SYN_REPORT)

  • fn feed(&mut self, data: &[u8]) -> bool the events of the node (whole input_event records, EVDEV_EVENT_SIZE bytes each) -> a SYN_REPORT changed the state. After a SYN_DROPPED the events up to the next SYN_REPORT go and needs_resync says to read the device's state anew

  • fn needs_resync(&mut self) -> bool events were dropped (SYN_DROPPED): the caller reads the keys (EVIOCGKEY) and the axes (EVIOCGABS) and hands them to resync (once true, then false until the next drop)

  • fn resync(&mut self, keys: &[u8], abs: &[AbsInfo]) -> bool the device's state read anew: the keys held (EVIOCGKEY's bitmap), the axes' values -> it changed

  • fn pad_test_connect(i: usize, name: &str, style: PadStyle) (test aid) the shared state without a platform: pad i came (as a backend says it)

  • fn pad_test_state(i: usize, st: &PadState) (test aid) pad i's state now (as a backend reports it)

  • fn pad_test_disconnect(i: usize) (test aid) pad i went

  • fn pad_test_reset() (test aid) every slot empty, no event waiting, not watching (a backend that watched is stopped first: its PADEV_DISCONNECT events are queued)

  • fn pad_test_focus(f: Option<bool>) (test aid) the focus the backends see: Some(true) focused, Some(false) not, None the windows'

  • fn pad_poll_test() -> bool (test aid) poll the native backend now, every slot (Windows: also the empty ones), as the window loop does when the time comes -> false: no native backend (the browser, Android, iOS)

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