Documentation
¶
Overview ¶
Package gamepadhid holds the HID drivers of the pads that report more through HID than the system's own driver shows: the PlayStation 4 and 5 pads, the Nintendo Switch Pro controller and the Xbox One and Series pads over Bluetooth (SPEC §28.1, §28.2). A driver reads the pad's input reports and makes the standard layout of them (internal/platform/padstd), the touchpad, the motion sensors and the battery, and it writes the output reports of rumble, trigger rumble and the lights.
The drivers are plain Go over a small Transport (read a report, write a report, feature reports): the transport of Linux is /dev/hidraw*, that of Windows is hid.dll, and the tests use scripted ones, so the drivers are tested on any host. A Device runs one driver on a goroutine of its own and tells a Handler what it finds; the platform backends make the Handler.
The drivers are translated to Go and changed from src/joystick/hidapi/SDL_hidapi_ps4.c, SDL_hidapi_ps5.c, SDL_hidapi_switch.c, SDL_hidapi_xboxone.c and SDL_hidapi_rumble.c of SDL 3 (zlib license, third_party/sdl/LICENSE).
Flutter: no counterpart
Index ¶
- func GUID(info DeviceInfo, caps Caps) gamepaddb.GUID
- type Bridge
- func (b *Bridge) Battery(p platform.PadBattery)
- func (b *Bridge) Caps() (Caps, bool)
- func (b *Bridge) Failed(err error)
- func (b *Bridge) Lost()
- func (b *Bridge) PadInfo(caps Caps, path string) platform.PadInfo
- func (b *Bridge) Ready(caps Caps)
- func (b *Bridge) Remove()
- func (b *Bridge) Sensor(s platform.PadSensor)
- func (b *Bridge) State(now time.Duration, s *padstd.Snapshot)
- func (b *Bridge) Touch(t platform.PadTouch)
- type Bus
- type Caps
- type Device
- type DeviceInfo
- type Driver
- type Handler
- type IO
- type Model
- type Options
- type Sink
- type Transport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GUID ¶
func GUID(info DeviceInfo, caps Caps) gamepaddb.GUID
GUID makes the GUID of a pad that a driver serves, as SDL makes it for a pad of its HID drivers: the bus, the vendor, the product and the version of the device, the CRC of the name the driver gave it and the driver signature 'h'.
Flutter: no counterpart
Types ¶
type Bridge ¶
type Bridge struct {
// ID is the pad's number, which the backend made.
ID platform.PadID
// Info is the device that the driver serves.
Info DeviceInfo
// Deliver runs fn on the UI thread with the handler of the service, unless the service has been stopped.
Deliver func(fn func(h platform.PadHandler))
// OnReady is called, on the goroutine of the device, once the pad has been announced; OnFailed when the driver could not
// start (the pad was not announced) and OnLost when the pad was announced and is gone (the removal has been delivered). They
// are for the backend's own bookkeeping and may be nil.
OnReady func(caps Caps)
OnFailed func(err error)
OnLost func()
// contains filtered or unexported fields
}
Bridge is the Handler that a platform backend gives a Device: it makes the pad the app sees of what the driver finds. When the driver is ready the pad is announced (platform.PadHandler.PadAdded) as a Standard pad, the snapshots of the buttons and axes become events, and the touchpad, the sensors and the battery are passed to the handler if it takes them (platform.PadExtrasHandler). Everything reaches the handler through Deliver, which runs a function on the UI thread; the backend decides what to do when the service has stopped by then.
Flutter: no counterpart
func (*Bridge) Battery ¶
func (b *Bridge) Battery(p platform.PadBattery)
Battery implements Handler.
func (*Bridge) Remove ¶
func (b *Bridge) Remove()
Remove delivers the removal of the pad when the backend ends the device itself (the node of the pad went away): call it after Device.Close has returned. The removal is delivered once, whether Lost or Remove comes first, and only for a pad that was announced.
type Caps ¶
type Caps struct {
// Name is the name that the driver gives the pad, or "" to keep the one of the device.
Name string
Kind gamepaddb.Kind
// Rumble, RumbleTriggers and LED say what the pad can do (the light is a light bar or the Home light).
Rumble, RumbleTriggers, LED bool
// Sensors says that the pad has a gyroscope and an accelerometer that SetSensors turns on, reading Rate times a second.
Sensors bool
Rate float32
// Touchpads is the number of touchpads.
Touchpads int
// Buttons says which buttons of the layout the pad has: a bit for each gamepaddb.Button.
Buttons uint32
}
Caps is what a driver knows of its pad once it has started.
Flutter: no counterpart
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device runs a driver on a goroutine of its own over a transport.
Flutter: no counterpart
func Start ¶
Start finds the driver of the device and starts it on a goroutine. It returns nil and false, and keeps the transport open, when no driver serves the device. Otherwise the transport belongs to the Device, and h is told Ready (or Failed) and then what the pad does.
Flutter: no counterpart
func (*Device) Close ¶
func (d *Device) Close()
Close stops the device, closes the transport and waits for the goroutine. A driver that changed the pad's mode puts it back. Handler.Lost is not called after Close is called.
func (*Device) Rumble ¶
Rumble turns the motors for d (0 stops them) at the strengths from 0 to 65535. The errors are the driver's: a pad without motors fails with GamepadRumbleUnavailable.
func (*Device) RumbleTriggers ¶
RumbleTriggers does the same for the motors of the triggers.
func (*Device) SetSensors ¶
SetSensors turns the motion sensors on or off.
type DeviceInfo ¶
type DeviceInfo struct {
Vendor, Product, Version uint16
Bus Bus
// Name is the product name that the device reports.
Name string
// Path says where the device is on the system.
Path string
}
DeviceInfo is what the platform knows of a HID device before a driver looks at it.
Flutter: no counterpart
type Driver ¶
type Driver interface {
// Start talks to the pad (feature reports, the first report) and returns what it can do. An error means the pad is not
// for this driver and the platform may read it another way.
Start(io IO) (Caps, error)
// Report takes in an input report.
Report(now time.Duration, data []byte, out Sink)
// Tick is called after every report and every time a read waits in vain: it keeps what needs the time (a Bluetooth link
// that went quiet, a rumble that has to be refreshed).
Tick(now time.Duration, out Sink)
// Rumble sets the strengths of the two motors (0 to 65535); 0 and 0 stops them.
Rumble(low, high uint16) error
// RumbleTriggers sets the strengths of the two trigger motors; 0 and 0 stops them.
RumbleTriggers(left, right uint16) error
// SetLED sets the colour of the light.
SetLED(r, g, b uint8) error
// SetSensors turns the sensors on or off.
SetSensors(on bool) error
// Close restores what the driver changed that other programs would meet.
Close()
}
Driver is the code of one model of pad. It is used by one goroutine at a time (a Device holds a lock), so it keeps its state without any of its own.
Flutter: no counterpart
type Handler ¶
type Handler interface {
// Ready says that the driver has started and the pad can be announced.
Ready(caps Caps)
// Failed says that the driver could not start. The transport is closed; the platform may read the pad another way.
Failed(err error)
// State gives the buttons and axes of the layout after a report.
State(now time.Duration, s *padstd.Snapshot)
// Touch, Sensor and Battery give what the pad reports besides.
Touch(t platform.PadTouch)
Sensor(s platform.PadSensor)
Battery(b platform.PadBattery)
// Lost says that the pad is gone, which ends the device (the transport is closed). It is never called after Close.
Lost()
}
Handler is told by a Device what its driver finds. Its methods run on the goroutine of the device, so they must not wait for the UI thread and must not call the Device back; the platform's handlers post what they get to the UI thread.
Flutter: no counterpart
type IO ¶
type IO interface {
// Read waits up to timeout for an input report, as Transport.Read does.
Read(buf []byte, timeout time.Duration) (int, error)
// Write sends an output report now and returns when it is sent.
Write(data []byte) (int, error)
// Send queues an output report. A report that is queued and not written yet, with the same length and report ID, is
// replaced by this one.
Send(data []byte) error
// SendMerging queues an output report like Send, but replaces a queued one of the same length when merge says so.
SendMerging(data []byte, merge func(pending []byte) bool) error
// Feature asks for the feature report id: buf is zeroed and filled by the answer (ID first). It returns the number of
// bytes, or an error.
Feature(id byte, buf []byte) (int, error)
// SendFeature sends a feature report (data[0] is its ID).
SendFeature(data []byte) (int, error)
// Now is the monotonic time.
Now() time.Duration
}
IO is what a driver sees of its device: the transport, with the output queue of the rumble thread of SDL (a report that is sent is written on another goroutine, a pause after another, and a report of the same kind that has not been written yet is replaced by the new one).
Flutter: no counterpart
type Model ¶
type Model uint8
Model is the driver that a device needs.
Flutter: no counterpart
const ( // ModelNone is a device that no driver here serves. // // Flutter: no counterpart ModelNone Model = iota // ModelPS4 is a PlayStation 4 pad (DualShock 4) or one that behaves like it. // // Flutter: no counterpart ModelPS4 // ModelPS5 is a PlayStation 5 pad (DualSense, DualSense Edge) or one that behaves like it. // // Flutter: no counterpart ModelPS5 // ModelSwitchPro is the Nintendo Switch Pro controller. // // Flutter: no counterpart ModelSwitchPro // ModelXboxOneBluetooth is an Xbox One or Series pad over Bluetooth. // // Flutter: no counterpart ModelXboxOneBluetooth )
func ModelOf ¶
func ModelOf(info DeviceInfo) Model
ModelOf says which driver serves a device, or ModelNone. The choice follows the pad's kind in SDL's controller list; the Switch Pro driver serves only the controller of Nintendo, and the Xbox driver only the Bluetooth pads of Microsoft, which it knows by their product numbers (over USB the system's drivers serve the Xbox pads, XInput on Windows and xpad on Linux).
Flutter: no counterpart
type Options ¶
type Options struct {
// Clock is the monotonic clock that stamps the reports; the default is the time since the device was started.
Clock func() time.Duration
// Logger receives the notes of the device; nil logs nothing.
Logger *slog.Logger
// ReadTimeout is how long a read waits before the driver's Tick runs; 8 ms when 0.
ReadTimeout time.Duration
// WriteGap is the pause after each queued output report; 10 ms when 0 (the rumble thread of SDL waits that long so that
// the writes do not starve the reads).
WriteGap time.Duration
}
Options are the options of a Device.
Flutter: no counterpart
type Sink ¶
type Sink interface {
// State gives the buttons and axes of the layout after a report.
State(now time.Duration, s *padstd.Snapshot)
// Touch gives a finger of a touchpad.
Touch(now time.Duration, t platform.PadTouch)
// Sensor gives a reading of a motion sensor.
Sensor(now time.Duration, s platform.PadSensor)
// Battery gives the battery state of the pad.
Battery(b platform.PadBattery)
// Lost says that the pad is gone although the link is still there (a wireless pad whose dongle is, for instance).
Lost()
}
Sink receives what a driver reads from the pad. It is called on the goroutine of the device, with its lock held, so it must not call the device back.
Flutter: no counterpart
type Transport ¶
type Transport interface {
// Read waits up to timeout for an input report and puts it in buf. It returns 0 and no error when the time ran out, and an
// error when the device is gone.
Read(buf []byte, timeout time.Duration) (int, error)
// Write sends an output report and returns the number of bytes sent.
Write(data []byte) (int, error)
// SendFeature sends a feature report (data[0] is its ID) and returns the number of bytes sent.
SendFeature(data []byte) (int, error)
// GetFeature asks for a feature report: data[0] is the ID of the report, and the report is put in data, with its ID in
// the first byte. It returns the number of bytes, ID included.
GetFeature(data []byte) (int, error)
// Close ends the link; reads and writes that wait return an error.
Close() error
}
Transport is the link to a HID device: the reports that go in and out and the feature reports. The first byte of a report is its report ID, as on the wire. Read and Write may be called from different goroutines at the same time; Close ends them.
Flutter: no counterpart