std / Gamepad
Available on macOS, Linux, Windows
#import "Gamepad";
The state of the first connected game controller, read through SDL.
Call update_gamepad once per frame, then look at the global gamepad: buttons is indexed by
Gamepad_Code, and the sticks and triggers are floats. With gamepad.generate_events set, button
changes also arrive as events in the Input module.
#import "Gamepad" ;
while true {
update_gamepad ();
if !gamepad.connected continue ;
if gamepad.buttons[Gamepad_Code.A] & .START print("jump\n" );
print("stick: %\n" , gamepad.left_stick);
}This one is only checked to compile, not run.
Procedures#
SDL#
()
Starts the controller backend. update_gamepad calls it on first use.
()
Reads the controller and refreshes gamepad. Call once per frame. Buttons that changed since the
last call carry START or END.
Evdev#
allocation
devices
() -> libevdev_log_priority
(dev: *
libevdev , grab: libevdev_grab_mode) ->
s32
(dev: *
libevdev , product_id:
s32 ) ->
void
(dev: *
libevdev , vendor_id:
s32 ) ->
void
(priority: libevdev_log_priority) -> void
capabilities
(dev: *
libevdev , delay: *
s32 , period: *
s32 ) ->
s32
(dev: *
libevdev , code:
u32 , value: libevdev_led_value) ->
s32
(dev: *
libevdev , type:
u32 , code:
u32 ) ->
s32
events
(dev: *
libevdev , type:
u32 , code:
u32 , data: *
void ) ->
s32
(dev: *
libevdev , type:
u32 , code:
u32 , value: *
s32 ) ->
s32
(dev: *
libevdev , slot:
u32 , code:
u32 , value: *
s32 ) ->
s32
(dev: *
libevdev , code:
u32 ) -> *input_absinfo
(dev: *
libevdev , type:
u32 , code:
u32 ) ->
s32
(dev: *
libevdev , slot:
u32 , code:
u32 ) ->
s32
(dev: *
libevdev , type:
u32 , code:
u32 ) ->
s32
(dev: *
libevdev , code:
u32 , abs: *input_absinfo) ->
s32
(dev: *
libevdev , flags: libevdev_read_flag, ev: *input_event) ->
s32
(dev: *
libevdev , code:
u32 , flat:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , fuzz:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , abs: *input_absinfo) ->
void
(dev: *
libevdev , code:
u32 , max:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , min:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , resolution:
s32 ) ->
void
(dev: *
libevdev , type:
u32 , code:
u32 , value:
s32 ) ->
s32
(dev: *
libevdev , slot:
u32 , code:
u32 , value:
s32 ) ->
s32
(name: *u8 ) -> s32
text
(name: *u8 , len: u64 ) -> s32
(type: u32 , name: *u8 ) -> s32
(type: u32 , name: *u8 , len: u64 ) -> s32
(type: u32 , code: u32 ) -> *u8
(ev: *input_event, type: u32 , code: u32 ) -> s32
(ev: *input_event, type: u32 ) -> s32
(name: *u8 ) -> s32
(name: *u8 , len: u64 ) -> s32
(name: *u8 ) -> s32
(name: *u8 , len: u64 ) -> s32
(type: u32 ) -> s32
(type: u32 ) -> *u8
(type: u32 , code: u32 , name: *u8 ) -> s32
(type: u32 , code: u32 , name: *u8 , len: u64 ) -> s32
(type: u32 , code: u32 , value: s32 ) -> *u8
(name: *u8 ) -> s32
(name: *u8 , len: u64 ) -> s32
(prop: u32 ) -> *u8
output
Foreign#
allocation
devices
() -> libevdev_log_priority
(dev: *
libevdev , grab: libevdev_grab_mode) ->
s32
(dev: *
libevdev , product_id:
s32 ) ->
void
(dev: *
libevdev , vendor_id:
s32 ) ->
void
(priority: libevdev_log_priority) -> void
capabilities
(dev: *
libevdev , delay: *
s32 , period: *
s32 ) ->
s32
(dev: *
libevdev , code:
u32 , value: libevdev_led_value) ->
s32
(dev: *
libevdev , type:
u32 , code:
u32 ) ->
s32
events
(dev: *
libevdev , type:
u32 , code:
u32 , data: *
void ) ->
s32
(dev: *
libevdev , type:
u32 , code:
u32 , value: *
s32 ) ->
s32
(dev: *
libevdev , slot:
u32 , code:
u32 , value: *
s32 ) ->
s32
(dev: *
libevdev , code:
u32 ) -> *input_absinfo
(dev: *
libevdev , type:
u32 , code:
u32 ) ->
s32
(dev: *
libevdev , slot:
u32 , code:
u32 ) ->
s32
(dev: *
libevdev , type:
u32 , code:
u32 ) ->
s32
(dev: *
libevdev , code:
u32 , abs: *input_absinfo) ->
s32
(dev: *
libevdev , flags: libevdev_read_flag, ev: *input_event) ->
s32
(dev: *
libevdev , code:
u32 , flat:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , fuzz:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , abs: *input_absinfo) ->
void
(dev: *
libevdev , code:
u32 , max:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , min:
s32 ) ->
void
(dev: *
libevdev , code:
u32 , resolution:
s32 ) ->
void
(dev: *
libevdev , type:
u32 , code:
u32 , value:
s32 ) ->
s32
(dev: *
libevdev , slot:
u32 , code:
u32 , value:
s32 ) ->
s32
(name: *u8 ) -> s32
text
(name: *u8 , len: u64 ) -> s32
(type: u32 , name: *u8 ) -> s32
(type: u32 , name: *u8 , len: u64 ) -> s32
(type: u32 , code: u32 ) -> *u8
(ev: *input_event, type: u32 , code: u32 ) -> s32
(ev: *input_event, type: u32 ) -> s32
(name: *u8 ) -> s32
(name: *u8 , len: u64 ) -> s32
(name: *u8 ) -> s32
(name: *u8 , len: u64 ) -> s32
(type: u32 ) -> s32
(type: u32 ) -> *u8
(type: u32 , code: u32 , name: *u8 ) -> s32
(type: u32 , code: u32 , name: *u8 , len: u64 ) -> s32
(type: u32 , code: u32 , value: s32 ) -> *u8
(name: *u8 ) -> s32
(name: *u8 , len: u64 ) -> s32
(prop: u32 ) -> *u8
output
Types#
:: enum
The buttons of a controller. Index Gamepad.buttons with it. A, B, X and Y are the face
buttons by their Xbox names; sticks and triggers also appear here as buttons, once they pass their
thresholds.
DPAD_UP
DPAD_DOWN
DPAD_LEFT
DPAD_RIGHT
START
BACK
LEFT_SHOULDER
RIGHT_SHOULDER
LEFT_TRIGGER
RIGHT_TRIGGER
FACE_DOWN
FACE_RIGHT
FACE_LEFT
FACE_UP
LEFT_STICK_UP
LEFT_STICK_DOWN
LEFT_STICK_LEFT
LEFT_STICK_RIGHT
RIGHT_STICK_UP
RIGHT_STICK_DOWN
RIGHT_STICK_LEFT
RIGHT_STICK_RIGHT
LEFT_STICK_CLICK
RIGHT_STICK_CLICK
A
B
X
Y
assert (Gamepad_Code.A == .FACE_DOWN);
assert (cast (int ) Gamepad_Code.DPAD_UP == 0 );Reset Copy ↗ Run ▸
:: struct
The current state of a controller. connected says whether there is one. The sticks run from -1 to 1
on each axis and the triggers from 0 to 1. A stick or trigger past its *_threshold also counts as
a button press.
connected : bool
left_trigger : float
right_trigger : float
generate_events : bool
generate_left_stick_events : bool
vendor_id : u16
product_id : u16
left_stick_threshold : := 0.5
right_stick_threshold : := 0.5
left_trigger_threshold : := 0.3
right_trigger_threshold : := 0.3
pad: Gamepad;
assert (!pad.connected);
assert (pad.left_stick_threshold == 0.5 );Reset Copy ↗ Run ▸
:: struct
Regenerate with tools/generators/window_render_evdev.py and the source-built repository formatter.
:: struct