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
- Variables
- func HIDGUID(c Connection, vendor, product, version uint16) string
- func ValidGUID(g string) bool
- func XInputGUID(subtype uint8) string
- type Axis
- type AxisRange
- type Binding
- type Calibration
- type Capture
- type Connection
- type ControllerInfo
- type Input
- type Mapping
- type PadDriverStatus
- type PadType
- type RawState
- type RumbleFunc
- type SourceKind
- type State
- type VirtualPads
- func (v *VirtualPads) Close() error
- func (v *VirtualPads) ReorderSlot(from, to int) error
- func (v *VirtualPads) ResetSlots() error
- func (v *VirtualPads) SetRumbleHandler(f RumbleFunc)
- func (v *VirtualPads) SetType(t PadType)
- func (v *VirtualPads) Slots() [MaxVirtualPads]int
- func (v *VirtualPads) Submit(pad uint8, s State) error
Constants ¶
const ( MaxDeadzone = 50 MaxTriggerThreshold = 50 MaxRumbleStrength = 100 )
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.
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.
const MaxVirtualPads = 4
MaxVirtualPads is how many virtual pads a host plugs in at once, the four slots of XInput.
Variables ¶
var ( ErrUnsupported = errors.New("gamepad: unsupported on this platform") ErrNotConnected = errors.New("gamepad: controller not connected") )
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 XInputGUID ¶
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.
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.
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 ¶
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.
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 {
// contains filtered or unexported fields
}
Capture reads game controllers from /dev/input/event* nodes. It finds controllers plugged in later when Controllers is called.
func (*Capture) Controllers ¶
func (c *Capture) Controllers() []ControllerInfo
Controllers lists the attached controllers in the order they attached.
func (*Capture) DefaultMapping ¶
DefaultMapping is controller id's standard mapping, from the kernel's gamepad button and axis codes.
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.
func ParseInput ¶
ParseInput reads an Input by its SDL mapping name, such as "a" or "lefttrigger".
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 ¶
DefaultMapping is the mapping a controller with no saved one starts with.
func ParseSDLMapping ¶
ParseSDLMapping reads one SDL gamecontrollerdb line. Inputs it does not name stay unmapped.
func (*Mapping) Apply ¶
Apply maps raw onto the wire layout. Unmapped and missing inputs read as released or centred.
func (*Mapping) FormatSDLMapping ¶
FormatSDLMapping writes m as an SDL gamecontrollerdb line: fields in the alphabetical order the database uses, platform last.
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
VirtualPadStatus reports whether /dev/uinput lets this user create virtual controllers.
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.
func ParsePadType ¶
type RawState ¶
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 ¶
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 ¶
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(t PadType) (*VirtualPads, error)
OpenVirtualPads makes controllers of uinput devices. Whatever type is asked for, they appear as Xbox 360 controllers.
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.