yield

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 11 Imported by: 0 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 yield implements an XInput2-driven "active user" watcher that lets MyComputer pause execution when the human is driving the desktop. The watcher subscribes to XInput2 raw events on the root window (RawMotion, RawButtonPress, RawKeyPress) and forwards every event NOT generated by MC's own XTest synthetic input down a channel for the pipeline to act on.

Implementation note on the transport: jezek/xgb (our X11 binding) does not ship an xinput2 sub-package, and we deliberately avoid adding cgo. Instead the watcher spawns `xinput test-xi2 --root` — a tiny X.Org utility that prints raw events as text — and parses stdout. The format is stable and machine-friendly:

EVENT type 17 (RawMotion)
    device: 2 (4)
    time:   123456789
    detail: 0

"device: <master> (<slave>)" carries both the master device id and the slave device the event originated from. MC's own XTest synthetic input always originates from the "Virtual core XTEST pointer" / "Virtual core XTEST keyboard" slave devices (id 4 and 5 on a normal X server); the watcher resolves those ids up front via the device list at the top of the same xinput stream and drops any raw event whose slave id matches. That filter — plus dropping any line that includes a "flags:" line containing "pointer-emulated" — is what keeps MC from yielding to its own actions.

The watcher is designed to be safe even when the host cannot run xinput at all: Start returns an error, and the pipeline treats yield as unavailable (no yielding, no doctor "ready" claim).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Available

func Available() (bool, string)

Available reports whether the host has the xinput binary available for use as the yield event source. Used by doctor and by the pipeline to decide whether to instantiate a watcher.

func SampleUserEvents

func SampleUserEvents(ctx context.Context, d time.Duration) (int, error)

SampleUserEvents starts a watcher for the given duration and counts how many user (non-synthetic) raw events arrive. Used by doctor's xinput2 row to report whether the listener actually sees events. Returns (count, error). A zero count with nil error means the listener is healthy but the user was idle during the sample.

Types

type Event

type Event struct {
	TS      time.Time `json:"ts"`
	Kind    EventKind `json:"event_type"`
	Master  int       `json:"device_id"`
	Slave   int       `json:"source_id"`
	Detail  int       `json:"detail,omitempty"`
	RawLine string    `json:"-"`
}

Event is the parsed, filtered raw XInput2 event the pipeline sees. Synthetic events (originating from MC's XTest slave devices) are filtered before reaching the channel and never appear here.

type EventKind

type EventKind string

EventKind identifies the XInput2 raw event kind.

const (
	EventRawMotion      EventKind = "RawMotion"
	EventRawButtonPress EventKind = "RawButtonPress"
	EventRawKeyPress    EventKind = "RawKeyPress"
)

type Watcher

type Watcher struct {
	// Command is the xinput binary to invoke. Defaults to "xinput".
	// Exposed for tests that want to point at a fixture script.
	Command string
	// Args overrides the default argv. Defaults to ["test-xi2", "--root"].
	Args []string
	// Buffer sizes the events channel. Defaults to 64.
	Buffer int
	// XTest, when non-zero, pins the synthetic-device ids instead of
	// resolving them from xinput's device list. Tests use this to make
	// filtering deterministic without an X server.
	XTest XTestDevices
	// contains filtered or unexported fields
}

Watcher subscribes to XInput2 raw events from the X server and emits filtered Events for real (non-MC-synthetic) user input. The watcher uses an external `xinput test-xi2 --root` process as the event source — see package docs for rationale.

A Watcher is single-shot: call Start once, read Events(), and either let the context cancel it or call Stop. Watchers are NOT goroutine- safe to start; only one Start may run concurrently per instance.

func (*Watcher) Err

func (w *Watcher) Err() error

Err returns the first non-nil error the watcher recorded. Returns nil while running cleanly; returns the process exit / pipe error after the watcher stops abnormally.

func (*Watcher) Events

func (w *Watcher) Events() <-chan Event

Events returns the channel raw user events are delivered on. The channel is closed when the watcher exits (context cancel, process exit, or Stop). Safe to call before Start; the channel is created lazily so consumers can register a select case ahead of time.

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context) error

Start launches the xinput subprocess and begins reading raw events. Returns a Dependency error if xinput cannot be invoked. Once Start returns nil the caller MUST read from Events() to avoid back- pressure stalling the parser goroutine.

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop terminates the watcher process and closes the events channel. Idempotent.

type XTestDevices

type XTestDevices struct {
	Pointer  int
	Keyboard int
}

XTestDevices is the set of XInput2 slave device ids whose events MC must treat as synthetic (its own XTest input) and drop. Resolved at watcher start from the xinput device list. Falls back to the well- known X server defaults (4=XTEST pointer, 5=XTEST keyboard) if the list cannot be parsed.

func Default

func Default() XTestDevices

Default returns the well-known XTEST slave device ids most X servers assign. Used when the xinput device list cannot be parsed.

Jump to

Keyboard shortcuts

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