controllers

package
v0.1.114 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 24 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 controllers reads the game controllers on a player's PC through SDL3 and yields the ControllerFrames a client streams to the game's host, as modlock.wasm.controllers describes.

SDL3 is loaded at run time, so a client without it runs on and reports ErrUnavailable. SDL opens a device without exclusive access, so Steam Input and the game keep reading it, and the reader asks for no enhanced reports or lights that would change what other programs see.

Index

Constants

View Source
const (
	SDLArchive       = "https://github.com/libsdl-org/SDL/releases/download/release-" + SDLVersion + "/SDL3-" + SDLVersion + "-win32-x64.zip"
	SDLArchiveSHA256 = "368bed8b24e44ad9ea372990814f2343fcfb9e468abb7f8f4a3bc146c7304752"
)

SDLArchive is SDL's published Windows x64 archive of SDLVersion, and SDLArchiveSHA256 its pinned checksum, which every download must match.

View Source
const FeedProtocolID = protocol.ID("modlock/controllers/1")

FeedProtocolID identifies the peer streams that carry the ControllerFeed service from a player's own client to their game server.

View Source
const Heartbeat = 50 * time.Millisecond

Heartbeat is the longest a running reader goes without sending a frame, well inside the host's 200 millisecond stale deadline.

View Source
const MinInterval = time.Second / 128

MinInterval is the shortest gap between two frames: 128 a second, two per 64 Hz server tick. Presses and releases in between wait for the next one.

View Source
const SDLVersion = "3.4.20"

SDLVersion is the SDL release whose Windows x64 SDL3.dll the reader loads when none sits beside the executable.

View Source
const SRPCControllerFeedServiceID = "modlock.controllers.ControllerFeed"
View Source
const VirtualEnv = "MODLOCK_VIRTUAL_CONTROLLERS"

VirtualEnv names the environment variable that, set to 1, makes every Reader in the process stream two virtual devices beside the real ones, so a controller mod can be built and measured on a PC with no device:

  • "Modlock virtual gamepad", a gamepad whose left stick's x traces the wall clock, (t mod 2 s) / 1 s - 1, so a mod decodes when the value was read as (x + 1) seconds past an even second and compares it with its own clock. Its left stick's y and its right stick circle once every 4 s, its triggers ramp up over each second, and its south button is tapped for one sample at each quarter second, a press and a release shorter than a server frame.
  • "Modlock virtual radio", a joystick with eight axes, four buttons and a hat, as a radio transmitter in USB joystick mode looks. Axis k is sin(2π t / 4 s + kπ / 4), button k is held during second k of each 4 s, and the hat points up, right, down and left in turn, a second each.

Variables

View Source
var ErrUnavailable = errors.New("controllers: SDL3 is not installed")

ErrUnavailable is returned by Open when no SDL3 library can be loaded, so a client without it runs on and streams no controllers.

Functions

func FeedHost

func FeedHost(ctx context.Context, host SRPCControllerFeedClient)

FeedHost streams this PC's controllers to a game server for the player whose game this peer carries, reading them only while the server's mods watch, until ctx ends. A server that serves no feed, or one that drops it, leaves the player with no controllers and the game playing on.

func FeedLocal

func FeedLocal(ctx context.Context, hub *Hub)

FeedLocal streams this PC's controllers to the host's own player on the game server this program runs, reading them only while the server's mods watch, until ctx ends.

func InstallSDL

func InstallSDL(ctx context.Context, dir string) error

InstallSDL downloads SDLArchive, checks it against SDLArchiveSHA256, and writes its SDL3.dll into dir, which it creates whole or not at all. It does nothing when dir exists. An application that ships SDL3.dll beside its executable calls it at build time; the Windows reader calls it on the PC that lacks one.

func LoadSDL

func LoadSDL(path string) error

LoadSDL loads the SDL3 library at path for every later Open, for an application that ships its own copy somewhere the reader does not look, such as a directory it unpacks into. It does nothing once a load has succeeded.

func NewSRPCControllerFeedHandler

func NewSRPCControllerFeedHandler(impl SRPCControllerFeedServer, serviceID string) srpc.Handler

NewSRPCControllerFeedHandler constructs a new RPC handler. serviceID: if empty, uses default: modlock.controllers.ControllerFeed

func SRPCRegisterControllerFeed

func SRPCRegisterControllerFeed(mux srpc.Mux, impl SRPCControllerFeedServer) error

SRPCRegisterControllerFeed registers the implementation with the mux. Uses the default serviceID: modlock.controllers.ControllerFeed

func Serve

func Serve(ctx context.Context, log *slog.Logger, wants <-chan uint32, send func(*pb.ControllerFrame) error) error

Serve reads this PC's controllers while the latest want epoch is nonzero, passing each frame to send, until ctx ends, wants closes or send fails. A zero epoch closes the devices, so a player whose controllers no mod watches has none open. Where SDL3 is missing it logs once and reads nothing, which the host sees as a player with no stream.

Types

type Feed

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

Feed is one controller stream: a guest's, or the host player's own when its guest is empty.

func (*Feed) Close

func (f *Feed) Close()

Close ends the feed's stream, ending its player's stream at the server.

func (*Feed) Frame

func (f *Feed) Frame(frame *pb.ControllerFrame)

Frame passes a frame from the feed to its slot's player while the mods watch them, and drops it otherwise.

func (*Feed) Serve

func (f *Feed) Serve(ctx context.Context, want func(*pb.ControllerWant) error) error

Serve sends the feed's want to want each time it changes, starting with the current one, until ctx ends or want fails.

func (*Feed) Want

func (f *Feed) Want() (int32, uint32)

Want returns the feed's slot, or -1, and the epoch the mods watch it at, zero when they do not.

type Hub

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

Hub binds each controller stream on a game server to the one player whose game connection it may feed, passes that player's frames to the server, and tells each stream the epoch the server's mods watch its player at.

A guest's stream feeds the slot whose client connects from one of the sockets the server bound to forward that guest's game stream, as the server reports when the player joins, or, for a game that connects to the server directly, the slot whose Steam account the server's own authority verified as the guest's. A frame names no player, so no guest can feed another's slot. The host's own player, fed by a stream with no guest, is the slot connecting from a loopback address that forwards no guest.

The program that runs the server owns the Hub: it reports joins, leaves and the watched players from the server, each forwarding socket from its game links, each guest's verified account when it has one, and opens a Feed for each controller stream it accepts.

func NewHub

func NewHub(send func(*pb.ControllerFrames)) *Hub

NewHub returns a hub that passes each bound player's frames, and the end of each player's stream, to send. send runs under the hub's lock, so it must not call the hub.

func (*Hub) Follow

func (h *Hub) Follow(event *control.HostEvent)

Follow passes hub the server events that bind controller streams: a join that gives a slot's address, a leave that frees it, and the set of watched players. A join with no Steam ID is a bot, whose slot no stream may feed.

func (*Hub) Forward

func (h *Hub) Forward(guest string, local *net.UDPAddr) func()

Forward records that a socket at local forwards guest's game stream, and returns the call that forgets it once the stream ends.

func (*Hub) Joined

func (h *Hub) Joined(slot int32, address string, steamID uint64)

Joined records that the client in slot connects from address, its ip:port or the engine's "loopback", signed in to steamID, the account the engine authenticated or zero. A bot, with neither, is forgotten, so no stream may feed its slot.

func (*Hub) Left

func (h *Hub) Left(slot int32)

Left forgets the player in slot.

func (*Hub) Members

func (h *Hub) Members(members map[string]uint64)

Members replaces the guests whose Steam accounts the server's own authority verified, such as its lobby's roster, with members, from guest to account. A member's stream feeds the slot signed in to its account, for a game that connects without a forwarding socket.

func (*Hub) Open

func (h *Hub) Open(guest string) *Feed

Open starts a controller stream for guest, or for the host's own player when guest is empty. The caller closes it when the stream ends.

func (*Hub) ServeFeed

func (h *Hub) ServeFeed(guest string, strm SRPCControllerFeed_FeedStream) error

ServeFeed takes the controller frames of guest, the peer the stream came over, for the slot its own game connection holds, and sends the mods' want for that slot as it changes, until the guest or the stream ends. An empty guest feeds the host's own player, so a server calls it only with the peer its transport authenticated.

func (*Hub) Wanted

func (h *Hub) Wanted(wants *pb.ControllerWants)

Wanted replaces the watched players with wants, the server's latest list.

type Reader

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

Reader streams the controllers attached to this PC. SDL's state is process-wide, so one Run goes at a time.

func Open

func Open(log *slog.Logger) (*Reader, error)

Open loads SDL3, or returns ErrUnavailable when it cannot be loaded; a later Open tries again. A nil log uses slog's default.

func (*Reader) Run

func (r *Reader) Run(ctx context.Context, send func(*pb.ControllerFrame) error) error

Run opens every attached device, follows hotplug and focus, and calls send with each frame until ctx ends, then closes the devices and returns nil. It sends nothing until Want gives an epoch. A send error ends Run with it.

func (*Reader) Want

func (r *Reader) Want(epoch uint32)

Want sets the want epoch the frames answer. A running reader sends a full frame for each new epoch.

type SRPCControllerFeedClient

type SRPCControllerFeedClient interface {
	// SRPCClient returns the underlying SRPC client.
	SRPCClient() srpc.Client

	// Feed carries the client's controllers to the player whose game connection
	// the same peer link carries; the frames name no player, so a client feeds
	// only its own. The server sends that player's want each time it changes,
	// with epoch zero while no mod watches or the client has no slot, and the
	// client reads its devices only while the latest epoch is nonzero.
	Feed(ctx context.Context) (SRPCControllerFeed_FeedClient, error)
}

func NewSRPCControllerFeedClient

func NewSRPCControllerFeedClient(cc srpc.Client) SRPCControllerFeedClient

func NewSRPCControllerFeedClientWithServiceID

func NewSRPCControllerFeedClientWithServiceID(cc srpc.Client, serviceID string) SRPCControllerFeedClient

type SRPCControllerFeedHandler

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

func (SRPCControllerFeedHandler) GetMethodIDs

func (SRPCControllerFeedHandler) GetMethodIDs() []string

func (*SRPCControllerFeedHandler) GetServiceID

func (d *SRPCControllerFeedHandler) GetServiceID() string

func (*SRPCControllerFeedHandler) InvokeMethod

func (d *SRPCControllerFeedHandler) InvokeMethod(
	serviceID, methodID string,
	strm srpc.Stream,
) (bool, error)

func (SRPCControllerFeedHandler) InvokeMethod_Feed

func (SRPCControllerFeedHandler) InvokeMethod_Feed(impl SRPCControllerFeedServer, strm srpc.Stream) error

type SRPCControllerFeedServer

type SRPCControllerFeedServer interface {
	// Feed carries the client's controllers to the player whose game connection
	// the same peer link carries; the frames name no player, so a client feeds
	// only its own. The server sends that player's want each time it changes,
	// with epoch zero while no mod watches or the client has no slot, and the
	// client reads its devices only while the latest epoch is nonzero.
	Feed(SRPCControllerFeed_FeedStream) error
}

type SRPCControllerFeed_FeedClient

type SRPCControllerFeed_FeedClient interface {
	srpc.Stream
	Send(*controllers2.ControllerFrame) error
	Recv() (*controllers2.ControllerWant, error)
	RecvTo(*controllers2.ControllerWant) error
}

type SRPCControllerFeed_FeedStream

type SRPCControllerFeed_FeedStream interface {
	srpc.Stream
	Send(*controllers2.ControllerWant) error
	SendAndClose(*controllers2.ControllerWant) error
	Recv() (*controllers2.ControllerFrame, error)
	RecvTo(*controllers2.ControllerFrame) error
}

Jump to

Keyboard shortcuts

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