Documentation
¶
Overview ¶
Package pad reads game controllers on every platform giokit runs on and turns them into per-frame button events.
Every controller is presented in one standard layout, the one Xbox, PlayStation, Nintendo and Steam pads share: four face buttons named by where they sit (South, East, West, North), shoulders, analog triggers, two sticks, a d-pad, and Back/Start/Guide. A game binds positions, not letters, so South is "A" on an Xbox pad, "✕" on a PlayStation pad and "B" on a Nintendo pad (Pads.NintendoConfirm instead makes South the A button there, so it confirms on every pad); Label names a button the way the player's pad prints it.
Sources:
- Linux: evdev (/dev/input/event*), with hot-plug. The kernel drivers for Xbox, PlayStation and Switch pads already report this layout.
- Windows: XInput (Xbox pads, and anything Steam Input presents as one).
- macOS: the GameController framework (needs cgo).
- Web: the Gamepad API, with the "standard" mapping.
Steam Input needs nothing extra: a game launched through Steam sees any controller as a virtual Xbox pad, which every source reads.
Use one Pads per window. Call Frame at the top of every frame, then read Events or run a Map:
pads := pad.New(app) // once; app wakes the UI when a pad changes
func (v *view) Frame(gtx layout.Context) {
v.pads.Frame(gtx)
v.padKeys.Run(gtx, v.pads)
...
}
Index ¶
Constants ¶
const ( RepeatDelay = 350 * time.Millisecond RepeatInterval = 90 * time.Millisecond )
Repeat timing for held buttons, like a held key.
Variables ¶
var Debug = os.Getenv("GIOKIT_PAD_DEBUG") != ""
Debug makes sources log the devices they consider and every raw event to stderr (Linux), for working out why a pad doesn't respond. Set it before the first Frame; GIOKIT_PAD_DEBUG=1 sets it too.
Functions ¶
func ASCIILabel ¶
ASCIILabel is Label without symbols, for fonts that lack ✕○□△ and ←: the PlayStation face buttons become "CROSS", "CIRCLE", "SQUARE", "TRIANGLE".
func Label ¶
Label is how a pad of family f prints button b: "A" (Xbox), "✕" (PlayStation), "B" (Nintendo) for South. Generic and Steam pads use the Xbox names.
func Nav ¶
Nav drives keyboard focus from the pad, so any focusable UI (menus, dialogs, forms) works without a keyboard: the d-pad (and left stick) move focus by geometry, South clicks the focused control, and East runs back, if not nil (close the menu or dialog). It consumes those events, so run it before Maps that use the same buttons, and only while that UI is up. A control "clicks" when the focused tag has a Click method, as kit.Clickable does.
Types ¶
type Binding ¶
type Binding struct {
Button Button
Title string // human description, for help screens
Run func()
// Repeat runs it again while the button is held, like a held key:
// for moving a cursor or focus.
Repeat bool
// contains filtered or unexported fields
}
Binding is one button → function.
type Button ¶
type Button uint8
Button is a control in the standard layout.
const ( South Button = iota // bottom face button: A, ✕, B (Nintendo) East // right face button: B, ○, A (Nintendo) West // left face button: X, □, Y (Nintendo) North // top face button: Y, △, X (Nintendo) LeftShoulder RightShoulder LeftTrigger // also an axis; pressed past half travel RightTrigger Back // View, Share/Create, − Start // Menu, Options, + LeftStick RightStick Up // d-pad; the left stick also presses these (see Pads.StickAsDPad) Down Left Right Guide // Xbox, PS, Home; often taken by the system ButtonCount )
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a Source for tests: pads appear, press and move only when told. Its methods are safe from any goroutine. The zero value has no pads.
func (*Fake) Set ¶
Set holds (down) or releases b on pad id. The change shows at the next Pads.Frame.
type Host ¶
type Host interface{ Invalidate() }
Host is what Pads needs from the app: kit.App is one. Invalidate requests a frame from any goroutine. If the host also has Focused() bool, input is ignored while it reports false, so a background window doesn't react.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map binds buttons to functions, the gamepad counterpart of kit.Keymap. Bind once, then Run it every frame where it applies. Maps run in order: an event one Map handled is consumed, so run the most specific first (an open menu's before the game's). The zero value is an empty map. A Map belongs to the UI goroutine, and its functions run there.
func (*Map) BindIf ¶
BindIf is Bind for a binding that only applies while enabled reports true; otherwise the press passes on to later Maps, as if unbound.
func (*Map) BindRepeat ¶
BindRepeat runs f when b is pressed and again while it is held.
type Pad ¶
type Pad struct {
// ID identifies the pad while it stays connected; a reconnected pad
// may get a new one.
ID int
Name string
Family Family
Buttons Buttons
Axes [AxisCount]float32
}
Pad is one connected controller's state.
type Pads ¶
type Pads struct {
// StickAsDPad makes the left stick press Up/Down/Left/Right too, so
// menus and grids move with either. On by default.
StickAsDPad bool
// NintendoConfirm swaps A and B on Nintendo pads, so South is the A
// button (which sits east there) and East is B, for players who expect
// A to confirm as on a Switch. West and North stay by position (Y and
// X on a Nintendo pad). Off by default: every button goes by position,
// as on PC, so South (B on a Nintendo pad) confirms. Pads.Label
// follows it.
NintendoConfirm bool
// contains filtered or unexported fields
}
Pads tracks the controllers of one window and turns their state into events, once per frame. Create it with New (platform source) or NewSource; the zero value is not usable. It belongs to the UI goroutine.
func New ¶
New returns Pads reading the platform's controllers, waking host on input. host may be nil when something else draws frames.
func (*Pads) ASCIILabel ¶
ASCIILabel is Label without symbols (see the package's ASCIILabel).
func (*Pads) Connected ¶
Connected returns the connected pads as of this frame. The slice is reused by the next Frame.
func (*Pads) Err ¶
Err reports why the source failed to start, if it did. Pads without a working source just report no controllers.
func (*Pads) Events ¶
Events returns this frame's button events, consumed ones included. The slice is reused by the next Frame.
func (*Pads) Family ¶
Family is the family of the pad last pressed, or else of the first connected pad: the one to label hints for.
type Source ¶
type Source interface {
// Start begins reading. The source calls wake, from any goroutine,
// when a pad connects, disconnects or changes, so the app draws a frame.
Start(wake func()) error
// Poll appends every connected pad's current state to dst.
Poll(dst []Pad) []Pad
// Close stops reading and releases the devices.
Close() error
}
Source reads controllers. Platform sources come from New; Fake is one for tests. A Source is used from the UI goroutine except for wake.