gamepad

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gamepad reads the controllers attached to a client, maps their raw buttons and axes onto the XInput layout Planck sends (with SDL gamecontrollerdb mapping strings for import and export), applies the user's calibration, and drives virtual controllers on a host.

Index

Constants

View Source
const (
	MaxDeadzone         = 50
	MaxTriggerThreshold = 50
	MaxRumbleStrength   = 100
)
View Source
const (
	ButtonDPadUp        uint32 = 0x0001
	ButtonDPadDown      uint32 = 0x0002
	ButtonDPadLeft      uint32 = 0x0004
	ButtonDPadRight     uint32 = 0x0008
	ButtonStart         uint32 = 0x0010
	ButtonBack          uint32 = 0x0020
	ButtonLeftThumb     uint32 = 0x0040
	ButtonRightThumb    uint32 = 0x0080
	ButtonLeftShoulder  uint32 = 0x0100
	ButtonRightShoulder uint32 = 0x0200
	ButtonGuide         uint32 = 0x0400
	ButtonA             uint32 = 0x1000
	ButtonB             uint32 = 0x2000
	ButtonX             uint32 = 0x4000
	ButtonY             uint32 = 0x8000
)

Buttons use the XInput bit layout, plus Guide, as input.GamepadState does.

View Source
const (
	HatUp    uint8 = 1
	HatRight uint8 = 2
	HatDown  uint8 = 4
	HatLeft  uint8 = 8
)

Hat directions are SDL's hat bits, as mapping strings use them.

View Source
const MaxVirtualPads = 4

MaxVirtualPads is how many virtual pads a host plugs in at once, the four slots of XInput.

Variables

View Source
var (
	ErrUnsupported  = errors.New("gamepad: unsupported on this platform")
	ErrNotConnected = errors.New("gamepad: controller not connected")
)
View Source
var (
	ErrNoFreeSlot = errors.New("gamepad: every virtual pad slot is taken")
	ErrClosed     = errors.New("gamepad: virtual pads closed")
)

Functions

func HIDGUID

func HIDGUID(c Connection, vendor, product, version uint16) string

HIDGUID is the SDL joystick GUID of a HID controller: bus, CRC (zero), then vendor, product and version, little endian with zero padding.

func ValidGUID

func ValidGUID(g string) bool

func XInputGUID

func XInputGUID(subtype uint8) string

XInputGUID is SDL's GUID for an XInput controller of a subtype (XINPUT_DEVSUBTYPE_*: 1 is a gamepad).

Types

type Axis

type Axis struct{ Value, Min, Max int32 }

Axis is one raw axis value and its logical range as the device reports it.

func (Axis) Norm

func (a Axis) Norm() float64

Norm maps the value onto -1..1.

type AxisRange

type AxisRange uint8

AxisRange is the part of a source axis a binding reads: +a2 and -a2 in a mapping string are its halves.

const (
	RangeFull AxisRange = iota
	RangePositive
	RangeNegative
)

type Binding

type Binding struct {
	Kind    SourceKind
	Index   int
	HatMask uint8 // SourceHat: the direction bits
	Range   AxisRange
	Invert  bool // ~ in a mapping string
}

Binding is where one Input comes from on the raw controller.

func Learn

func Learn(rest, now *RawState, target Input) (Binding, bool)

Learn finds the control the user moved between rest and now to bind to target, for mapping a controller by pressing what should drive each input. A stick input takes only an axis, inverted when it moved toward negative: sticks are learned by pushing right on X and down on Y, SDL's positive directions. A trigger takes an axis the same way, or a button. A button takes a button, one hat direction, or the half of an axis it moved into.

func (Binding) String

func (b Binding) String() string

type Calibration

type Calibration struct {
	LeftDeadzone     int // 0..50 of the stick's radius
	RightDeadzone    int
	TriggerThreshold int // 0..50 of the trigger's travel
	InvertLeftY      bool
	InvertRightY     bool
	RumbleOff        bool
	RumbleStrength   int // 0..100
}

Calibration is the user's processing of a mapped State, in percent.

func DefaultCalibration

func DefaultCalibration() Calibration

DefaultCalibration passes input through unchanged at full rumble: games apply their own deadzones, and a second one would widen theirs.

func (Calibration) Apply

func (c Calibration) Apply(s State) State

Apply runs s through the deadzones, trigger thresholds and Y inversion. A deadzone is radial and rescales what is outside it, so the stick still reaches full deflection.

func (Calibration) ScaleRumble

func (c Calibration) ScaleRumble(low, high uint16) (large, small uint8)

ScaleRumble turns a host's rumble, 0..65535 per motor, into the motor strengths to drive here.

type Capture

type Capture struct{}

Capture finds no controllers on this platform.

func Open

func Open() (*Capture, error)

func (*Capture) Close

func (c *Capture) Close() error

func (*Capture) Controllers

func (c *Capture) Controllers() []ControllerInfo

func (*Capture) DefaultMapping

func (c *Capture) DefaultMapping(int) Mapping

func (*Capture) Read

func (c *Capture) Read(int, *RawState) error

func (*Capture) Rumble

func (c *Capture) Rumble(int, uint8, uint8) error

type Connection

type Connection uint8
const (
	ConnectionUnknown Connection = iota
	ConnectionUSB
	ConnectionBluetooth
	// ConnectionWireless is a radio that is not Bluetooth, such as the Xbox
	// Wireless Adapter, or a wireless pad XInput cannot tell apart.
	ConnectionWireless
)

func (Connection) String

func (c Connection) String() string

type ControllerInfo

type ControllerInfo struct {
	ID         int
	Slot       int
	Name       string
	VendorID   uint16
	ProductID  uint16
	GUID       string // SDL joystick GUID, 32 hex digits
	Connection Connection
	BatteryPct int // -1 when unknown or wired
	Connected  bool
}

ControllerInfo describes one attached controller. ID stays the same while the controller stays attached; Slot is its place among the attached controllers, the pad index a stream sends it as.

type Input

type Input uint8

Input is one input of the standard controller a mapping fills.

const (
	InputA Input = iota
	InputB
	InputX
	InputY
	InputBack
	InputGuide
	InputStart
	InputLeftStick
	InputRightStick
	InputLeftShoulder
	InputRightShoulder
	InputDPadUp
	InputDPadDown
	InputDPadLeft
	InputDPadRight
	InputLeftX
	InputLeftY
	InputRightX
	InputRightY
	InputLeftTrigger
	InputRightTrigger
	NumInputs
)

func ParseInput

func ParseInput(name string) (Input, error)

ParseInput reads an Input by its SDL mapping name, such as "a" or "lefttrigger".

func (Input) String

func (in Input) String() string

type Mapping

type Mapping struct {
	GUID     string
	Name     string
	Bindings [NumInputs]Binding
	// Halves drive one half of a stick output each, [0] the positive and
	// [1] the negative, as +leftx: and -leftx: do in a mapping string; they
	// add to the stick's full binding.
	Halves [NumInputs][2]Binding
	// Extra are fields of an imported mapping string this package does not
	// use, such as platform:, kept so that export gives them back.
	Extra []string
	// contains filtered or unexported fields
}

Mapping turns one controller's RawState into a State.

func DefaultMapping

func DefaultMapping(guid, name string) Mapping

DefaultMapping is the mapping a controller with no saved one starts with.

func ParseSDLMapping

func ParseSDLMapping(s string) (Mapping, error)

ParseSDLMapping reads one SDL gamecontrollerdb line. Inputs it does not name stay unmapped.

func (*Mapping) Apply

func (m *Mapping) Apply(raw *RawState) State

Apply maps raw onto the wire layout. Unmapped and missing inputs read as released or centred.

func (*Mapping) FormatSDLMapping

func (m *Mapping) FormatSDLMapping() string

FormatSDLMapping writes m as an SDL gamecontrollerdb line: fields in the alphabetical order the database uses, platform last.

func (*Mapping) Map

func (m *Mapping) Map(in Input, b Binding)

func (*Mapping) Reset

func (m *Mapping) Reset()

Reset puts every input back on the binding its controller starts with and drops imported extras. A parsed mapping resets to the XInput layout.

func (*Mapping) Unmap

func (m *Mapping) Unmap(in Input)

type PadDriverStatus

type PadDriverStatus struct {
	Installed      bool
	DriverName     string
	SupportedTypes []PadType
	InstallHint    string // set when Installed is false
}

PadDriverStatus is what a host can emulate.

func VirtualPadStatus

func VirtualPadStatus() PadDriverStatus

func (PadDriverStatus) Effective

func (s PadDriverStatus) Effective(t PadType) PadType

Effective is the type a request for t plugs in: t when the driver has it, otherwise the closest type it has (a DualSense is emulated as a DualShock 4).

type PadType

type PadType uint8

PadType is the controller a host's virtual pads appear as to games.

const (
	PadXbox360 PadType = iota
	PadDS4
	PadDualSense
)

func ParsePadType

func ParsePadType(s string) (PadType, error)

func (PadType) String

func (t PadType) String() string

String is the name config files use.

type RawState

type RawState struct {
	Buttons []bool
	Axes    []Axis
	Hats    []uint8
}

RawState is a controller's inputs before mapping, indexed as SDL indexes them: bN, aN and hN in a mapping string. Axes follow SDL's direction, so a stick's Y grows downward.

type RumbleFunc

type RumbleFunc func(pad uint8, large, small uint8)

RumbleFunc receives a game's rumble for the client pad feeding a virtual pad, motor strengths 0..255.

type SourceKind

type SourceKind uint8
const (
	SourceNone SourceKind = iota
	SourceButton
	SourceAxis
	SourceHat
)

type State

type State struct {
	Buttons        uint32
	LT, RT         uint8
	LX, LY, RX, RY int16
}

State is one controller in the wire layout: stick axes signed with positive X right and positive Y up, as in XInput.

type VirtualPads

type VirtualPads struct {
	// contains filtered or unexported fields
}

VirtualPads feeds client controllers to virtual ones on the host. Each client pad index takes the lowest free slot the first time it sends a state, up to MaxVirtualPads. Safe from any goroutine.

func OpenVirtualPads

func OpenVirtualPads(PadType) (*VirtualPads, error)

func (*VirtualPads) Close

func (v *VirtualPads) Close() error

Close unplugs every virtual pad.

func (*VirtualPads) ReorderSlot

func (v *VirtualPads) ReorderSlot(from, to int) error

ReorderSlot swaps the client pads feeding slots from and to, so that a game's player 1 becomes whichever pad was in slot to. The virtual pads stay plugged in, and each one takes its new client pad's last state at once rather than on that pad's next input.

func (*VirtualPads) ResetSlots

func (v *VirtualPads) ResetSlots() error

ResetSlots unplugs every virtual pad; each client pad takes a slot again in the order it next sends a state.

func (*VirtualPads) SetRumbleHandler

func (v *VirtualPads) SetRumbleHandler(f RumbleFunc)

func (*VirtualPads) SetType

func (v *VirtualPads) SetType(t PadType)

SetType changes the type of pads plugged in from now on; ResetSlots replugs the ones already in.

func (*VirtualPads) Slots

func (v *VirtualPads) Slots() [MaxVirtualPads]int

Slots are the client pad index in each slot, -1 for a free one.

func (*VirtualPads) Submit

func (v *VirtualPads) Submit(pad uint8, s State) error

Submit sends client pad's state to its virtual pad, plugging one in when the pad has none.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL