pad

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package pad reads game controllers through SDL3 and turns them into the few actions the big picture interface understands (move, confirm, back, menu, …), with key repeat for held directions. It also drives rumble and the DualSense lightbar. SDL runs on a thread of its own and is only ever touched from there.

Index

Constants

View Source
const (
	Up      = "up"
	Down    = "down"
	Left    = "left"
	Right   = "right"
	Confirm = "confirm" // ✕ / A
	Back    = "back"    // ○ / B
	Action  = "action"  // □ / X
	Info    = "info"    // △ / Y
	Menu    = "menu"    // Options / Menu
	View    = "view"    // Create / View
	Home    = "home"    // PS / Xbox button
	LB      = "lb"
	RB      = "rb"
	LT      = "lt"
	RT      = "rt"
)

Actions sent to the interface.

Variables

View Source
var VirtualAxes = map[string]int{"lx": 0, "ly": 1, "rx": 2, "ry": 3, "lt": 4, "rt": 5}

VirtualAxes names its axes (values -32768 to 32767; a trigger rests at -32768, which the gamepad reports as 0).

View Source
var VirtualButtons = map[string]int{
	"south": 0, "east": 1, "west": 2, "north": 3, "back": 4, "guide": 5, "start": 6,
	"leftstick": 7, "rightstick": 8, "lb": 9, "rb": 10,
	"up": 11, "down": 12, "left": 13, "right": 14, "misc": 15, "touchpad": 20,
}

VirtualButtons names the SDL gamepad buttons a virtual controller has.

Functions

This section is empty.

Types

type Kind

type Kind string

Kind is a controller family, for the button glyphs.

const (
	PlayStation Kind = "playstation"
	Xbox        Kind = "xbox"
	Nintendo    Kind = "nintendo"
	Other       Kind = "other"
)

type Manager

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

Manager owns the SDL thread.

func Start

func Start(onAction func(string, bool), onState func(State)) *Manager

Start loads SDL and begins reading controllers. onAction gets every action (repeat=true for auto-repeat of a held direction); onState gets controller changes. Both are called from the SDL thread.

func (*Manager) InMode added in v1.10.0

func (m *Manager) InMode() (Mode, bool)

InMode returns the mode the SDL thread last brought SDL to, and whether SDL is running (false when that mode is Off or SDL didn't start).

func (*Manager) Mode

func (m *Manager) Mode() Mode

Mode returns the controller layer's mode.

func (*Manager) PlugVirtual

func (m *Manager) PlugVirtual(kind Kind) error

PlugVirtual plugs in a virtual controller that looks like a DualSense (kind PlayStation) or an Xbox controller. It stays plugged in when the mode changes (SDL starts over then, and it is made again).

func (*Manager) Rumble

func (m *Manager) Rumble(effect string)

Rumble plays a short effect: "tick" (moving), "bump" (at an edge), "confirm", "error" or "launch". A new effect replaces one still playing.

func (*Manager) SetLight

func (m *Manager) SetLight(hex string) error

SetLight sets the DualSense lightbar to a #rrggbb colour.

func (*Manager) SetMode

func (m *Manager) SetMode(mode Mode)

SetMode switches the controller layer's mode.

func (*Manager) SetRawListener

func (m *Manager) SetRawListener(fn func(Raw))

SetRawListener sends every change of the controller's buttons and axes to fn (nil stops it), for testing a controller.

func (*Manager) State

func (m *Manager) State() State

State returns the current controller state.

func (*Manager) Stop

func (m *Manager) Stop()

Stop shuts SDL down.

func (*Manager) UnplugVirtual

func (m *Manager) UnplugVirtual() error

UnplugVirtual removes the virtual controller.

func (*Manager) VirtualAxis

func (m *Manager) VirtualAxis(axis int, value int16) error

VirtualAxis moves a virtual controller axis.

func (*Manager) VirtualButton

func (m *Manager) VirtualButton(button int, down bool) error

VirtualButton presses (down) or releases a virtual controller button.

type Mode

type Mode int

Mode is how much of the controller Seaglass uses.

const (
	// Active is the full layer: input, rumble, lightbar.
	Active Mode = iota
	// Passive only listens, for while a game runs. Controllers are read
	// without SDL's HIDAPI drivers, so Seaglass never writes to one;
	// the game gets the controller exactly as it expects. Actions
	// keep coming (the PS button opens the overlay).
	Passive
	// Off releases controllers completely.
	Off
)

func (Mode) String added in v1.10.0

func (m Mode) String() string

type Raw

type Raw struct {
	Buttons uint32   `json:"buttons"`
	Axes    [6]int16 `json:"axes"`
}

Raw is the whole state of the controller in use: which SDL gamepad buttons are down (bit n is button n) and its axes (left stick x, y, right stick x, y, left and right trigger; -32768 to 32767, triggers from 0).

type State

type State struct {
	Connected bool   `json:"connected"`
	Name      string `json:"name"`
	Kind      Kind   `json:"kind"`
	DualSense bool   `json:"dualSense"`
	Battery   int    `json:"battery"` // percent, -1 unknown
	Wireless  bool   `json:"wireless"`
	Error     string `json:"error,omitempty"`
}

State describes the controller in use.

Jump to

Keyboard shortcuts

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