Documentation
¶
Overview ¶
Package plugin runs a KeyForge plugin: the author writes one handler per action and the package takes care of the rest.
Run reads the launch info the daemon passes in the KEYFORGE_PLUGIN_INFO environment variable, connects, performs the hello handshake, dispatches each action_invoked event to the handler registered for its action id and returns when the daemon closes the connection, which is how the daemon stops plugins.
Handlers run one at a time, in the order the daemon fired them, so state kept per Invocation.Context (e.g. a toggle bound to two keys) needs no locking. A handler that needs to do slow work should start its own goroutine: up to 128 invocations wait behind a running handler, and any that arrive past that are dropped and logged. A handler that panics is logged and skipped; the plugin keeps running.
Index ¶
Constants ¶
const LaunchInfoEnv = "KEYFORGE_PLUGIN_INFO"
LaunchInfoEnv is the environment variable the daemon sets, when it spawns a plugin, to a JSON protocol.PluginLaunchInfo.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run connects the plugin to the daemon and dispatches actions to cfg.Handlers until the daemon closes the connection, which returns nil, or ctx is cancelled, which returns ctx.Err(). Either way, invocations still queued at that point are dropped. It fails right away if KEYFORGE_PLUGIN_INFO is missing or invalid, which means the process wasn't launched by the daemon, or if the daemon rejects the hello.
Types ¶
type Config ¶
type Config struct {
// Version is the plugin version sent in hello. Required; keep it in sync
// with the manifest.
Version string
// Handlers holds one handler per action the manifest declares.
Handlers Handlers
// Logger receives handler errors and ignored frames. Defaults to a text
// logger on stderr.
Logger *slog.Logger
}
Config configures Run.
type Handler ¶
type Handler func(ctx context.Context, inv Invocation) error
Handler runs an action. Its ctx is cancelled when the connection to the daemon closes. A returned error is logged; the daemon doesn't wait for the result. A panic is recovered and logged with its stack, and the plugin goes on with the next invocation; panics in goroutines the handler starts are not recovered and crash the plugin.
type Handlers ¶
Handlers maps an action id, as declared in the manifest (e.g. "play_pause", without the "plugin.<id>." prefix), to its handler.
type Invocation ¶
type Invocation = protocol.ActionInvokedSchemaJsonData
Invocation is one firing of a plugin action: Context identifies the binding instance (opaque, stable; use it as a key for per-instance state), Action holds the action id and its params, and Input is the hardware event that fired it, or nil when it wasn't fired by hardware (e.g. a test run from the GUI).