plugin

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 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 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

View Source
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

func Run(ctx context.Context, cfg Config) error

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

type Handlers map[string]Handler

Handlers maps an action id, as declared in the manifest (e.g. "play_pause", without the "plugin.<id>." prefix), to its handler.

type Invocation

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).

Jump to

Keyboard shortcuts

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