inputagent

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package inputagent carries a player's input into a standalone emulator's container. WittyPlay sends the same pad, pointer and touch messages a game runner gets (package ipc) over TCP to a small agent inside the container, which turns them into key and mouse events on the container's X display with xdotool.

Why keys and xdotool, not uinput: a virtual pad through /dev/uinput needs that device in the container, which the hardening refuses, and a program that reads raw pads would see the host's too. Every desktop emulator takes the keyboard, and its own settings map keys to the console's buttons. Each of four players has keys of their own (KeyMap), which an administrator can change; an image sets the emulator's settings to match. Analogue sticks become their directions' keys past a threshold, which is enough for menus and most games and is the known limit of this agent. There's no way back for rumble: a keyboard has none.

The protocol: the client sends the session's secret and a newline, then frames of one length byte and that many bytes. The agent closes a connection whose first line is wrong, and takes one connection at a time.

Index

Constants

View Source
const (
	KindVideo = 'v'
	KindAudio = 'a'
)

Kinds of packet the agent sends up.

View Source
const (
	ControlMark = 0xB7
	ControlSize = 3
)

ControlMark begins a message that isn't input: ControlMark, then a bitrate in kbit/s as two bytes, low first. Its length is no pad, pointer or touch message's.

View Source
const MaxFrame = 1 + 1 + ipc.MaxTouches*ipc.TouchSize

MaxFrame is the largest message: a touch message with every finger.

View Source
const MaxPlayers = 4

MaxPlayers is how many pads the agent gives keys to.

Variables

View Source
var Buttons = []string{"b", "y", "select", "start", "up", "down", "left", "right", "a", "x", "l", "r", "l2", "r2", "l3", "r3",
	"lup", "ldown", "lleft", "lright", "rup", "rdown", "rleft", "rright"}

Buttons names a pad's buttons by their bit in a pad message (libretro's joypad order), and then the two sticks' directions, which become keys past stickThreshold. These names are the keys of a KeyMap.

Functions

func BitrateMessage

func BitrateMessage(kbps int) []byte

BitrateMessage asks the agent to have the picture encoded at kbps from now on.

func Bridge

func Bridge(ctx context.Context, path, secret string, videoPort, audioPort int, agent *Agent) error

Bridge is the agent's end: it connects to the socket at path, sends up what arrives on the two UDP ports of its own loopback, and applies the input that comes down. It returns when the connection ends or ctx does.

func Serve

func Serve(ctx context.Context, l net.Listener, secret string, sink Sink) error

Serve accepts one connection at a time on l, checks its secret, and applies what it sends.

func ServeWith

func ServeWith(ctx context.Context, l net.Listener, secret string, agent func() *Agent) error

ServeWith is Serve with an agent of the caller's making for each connection: one with a key map, or one that hears bitrate requests.

Types

type Agent

type Agent struct {
	Sink Sink
	// Keys is the key map; DefaultKeys if it was never set.
	Keys *KeyMap
	// Bitrate, if set, hears a request to change the encoder's bitrate.
	Bitrate func(kbps int)
	// contains filtered or unexported fields
}

Agent turns messages into Sink calls, remembering what's held so it only sends changes.

func (*Agent) Handle

func (a *Agent) Handle(p []byte) error

Handle applies one message. Each of four players has keys of their own.

func (*Agent) Release

func (a *Agent) Release()

Release lets go of everything, when a connection ends.

type Forwarder

type Forwarder struct {
	Addr   string // host:port
	Secret string
	Dial   func(ctx context.Context, network, addr string) (net.Conn, error)
	// contains filtered or unexported fields
}

Forwarder sends input to one container's agent, connecting when it first has something to send and again after a failure. Send never blocks the caller for long: input that can't be sent is dropped, as a game drops a late frame.

func (*Forwarder) Close

func (f *Forwarder) Close()

Close lets go of the connection.

func (*Forwarder) Send

func (f *Forwarder) Send(p []byte)

Send passes one message on.

type Host

type Host struct {
	Secret string
	// OnPacket hears each RTP packet. The slice is only good until it returns.
	OnPacket func(kind byte, pkt []byte)
	// contains filtered or unexported fields
}

Host is WittyPlay's end of the bridge.

func (*Host) Close

func (h *Host) Close()

Close ends the bridge.

func (*Host) Listen

func (h *Host) Listen(path string) error

Listen starts the host's end on a Unix socket at path, which must not exist yet.

func (*Host) Send

func (h *Host) Send(p []byte)

Send passes one input message down. Input that can't be sent is dropped.

type KeyMap

type KeyMap [MaxPlayers]map[string]string

KeyMap says which X key each button of each player presses. An administrator can change it (the "keys.p1" to "keys.p4" settings) to match the keys an emulator already knows.

func DefaultKeys

func DefaultKeys() KeyMap

DefaultKeys is the map used when nothing else is set. An emulator's own settings have to map these keys to its pads; an image ships settings that do. Player 4's use the function keys, which some emulators keep for themselves, so those are the first to change.

func ParseKeys

func ParseKeys(js string) (KeyMap, error)

ParseKeys reads a KeyMap from JSON (a list of up to four objects, button name to X key name) laid over the defaults. A name that isn't a button, or a key that couldn't be an X key name, is refused: the key reaches xdotool's command line.

type Sink

type Sink interface {
	Key(name string, down bool) error
	Pointer(x, y float64, pressed bool) error // 0..1 across the display
}

Sink is what the agent does with input. XDoTool is the real one.

type XDoTool

type XDoTool struct {
	Width, Height int
	// contains filtered or unexported fields
}

XDoTool sends events to the X display in DISPLAY through xdotool. Width and Height are the display's, for the pointer.

func (*XDoTool) Key

func (x *XDoTool) Key(name string, down bool) error

func (*XDoTool) Pointer

func (x *XDoTool) Pointer(fx, fy float64, pressed bool) error

Jump to

Keyboard shortcuts

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