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 the reader unavailable, with the load error, to the host. 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
- Variables
- func FeedHost(ctx context.Context, host SRPCControllerFeedClient, commands CommandSink)
- func FeedLocal(ctx context.Context, hub *Hub, commands CommandSink)
- func InstallSDL(ctx context.Context, dir string) error
- func LoadSDL(path string) error
- func NewSRPCControllerFeedHandler(impl SRPCControllerFeedServer, serviceID string) srpc.Handler
- func SRPCRegisterControllerFeed(mux srpc.Mux, impl SRPCControllerFeedServer) error
- func Serve(ctx context.Context, log *slog.Logger, wants <-chan *pb.ControllerWant, ...) error
- type Command
- type CommandSink
- type Feed
- type Hub
- func (h *Hub) Follow(event *control.HostEvent)
- func (h *Hub) Forward(guest string, local *net.UDPAddr) func()
- func (h *Hub) Joined(slot int32, address string, steamID uint64)
- func (h *Hub) Left(slot int32)
- func (h *Hub) Members(members map[string]uint64)
- func (h *Hub) Open(guest string) *Feed
- func (h *Hub) ServeFeed(guest string, strm SRPCControllerFeed_FeedStream) error
- func (h *Hub) Wanted(wants *pb.ControllerWants)
- type Reader
- type SRPCControllerFeedClient
- type SRPCControllerFeedHandler
- func (SRPCControllerFeedHandler) GetMethodIDs() []string
- func (d *SRPCControllerFeedHandler) GetServiceID() string
- func (d *SRPCControllerFeedHandler) InvokeMethod(serviceID, methodID string, strm srpc.Stream) (bool, error)
- func (SRPCControllerFeedHandler) InvokeMethod_Feed(impl SRPCControllerFeedServer, strm srpc.Stream) error
- type SRPCControllerFeedServer
- type SRPCControllerFeed_FeedClient
- type SRPCControllerFeed_FeedStream
Constants ¶
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.
const CommandAxes = 4
CommandAxes counts the command axes a binding drives, CommandAxis 1 to 4.
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.
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.
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.
const SDLVersion = "3.4.20"
SDLVersion is the SDL release whose Windows x64 SDL3.dll the reader loads when none sits beside the executable.
const SRPCControllerFeedServiceID = "modlock.controllers.ControllerFeed"
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 ¶
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, commands CommandSink)
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, and passes the commands of the mods' binding to commands, when it is not nil. 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, commands CommandSink)
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, and passes the commands of the mods' binding to commands, when it is not nil.
func InstallSDL ¶
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 ¶
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 *pb.ControllerWant, send func(*pb.ControllerFrame) error, commands CommandSink) 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. It answers each epoch at once: opening while SDL loads and starts, the devices once it reads, and unavailable with the error where SDL cannot start, which it tries again on the next epoch. While the want carries a command binding, it passes the binding's commands to commands, when commands is not nil.
Types ¶
type Command ¶
type Command struct {
// Axes holds each command axis's value, -1 to 1, at CommandAxis - 1.
Axes [CommandAxes]float32
// Bound has bit CommandAxis - 1 set for each axis the binding drives.
Bound uint8
// Buttons holds the button bits held now, and Mask every bit the
// binding's buttons drive, so a bit in Mask but not Buttons is released.
Buttons uint64
Mask uint64
// Marker is the binding's marker, the bits a command carries whenever
// the binding applies, apart from its buttons.
Marker uint64
// At is when the reader read the state, with a monotonic reading.
At time.Time
}
Command is a command binding applied to one state of its device: the values and button bits a host application carries in the player's user commands.
type CommandSink ¶
type CommandSink func(*Command)
CommandSink receives each command a reader computes, at least every Heartbeat while it holds a binding, and nil once it has none to give: no binding, no device that matches it, an unfocused game, or a stopped reader. It runs on the reader's thread, so it returns quickly.
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 ¶
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() *pb.ControllerWant
Want returns the feed's want: its slot, or -1, with the epoch the mods watch it at, zero when they do not, and their command binding.
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 ¶
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 ¶
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 ¶
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) Members ¶
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 ¶
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 ¶
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, commands CommandSink) 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. With each frame it applies the want's command binding and passes the result to commands, when commands is not nil, and nil once it stops. It sends nothing until Want gives an epoch. A send error ends Run with it.
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 (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
}