surface

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 17 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 surface observes the local display, window, accessibility and permission state, and captures a window and sends atomic input to one. Targets are explicitly observed values whose identity is revalidated before an operation; no handle of the native toolkit is exposed. The DesktopDriver interface is the seam that caller-selected tools implement where the OS does not allow a direct implementation (for example Wayland), and the package never selects a driver by name.

Index

Constants

View Source
const (
	DesktopNative    = "desktop.native"
	DesktopX11       = "desktop.x11"
	DesktopWayland   = "desktop.wayland"
	DesktopContainer = "desktop.container"
	DesktopRecording = "desktop.recording"
)

Variables

View Source
var (
	// ErrSurfaceUnavailable means the native desktop cannot perform the
	// requested operation in the current session.
	ErrSurfaceUnavailable = errors.New("surface operation unavailable")
	// ErrSurfacePermission means the operating system has not granted the
	// calling process permission for the requested operation.
	ErrSurfacePermission = errors.New("surface operation permission is not granted")
	// ErrSurfaceStale means the observed process generation or window no longer
	// names the same native surface.
	ErrSurfaceStale = errors.New("surface reference is stale")
	// ErrSurfaceNotFocused means input could not be safely directed to the
	// selected surface without affecting another application.
	ErrSurfaceNotFocused = errors.New("surface is not focused")
)
View Source
var ErrDesktopUnavailable = errors.New("desktop operation unavailable")

ErrDesktopUnavailable means a selected desktop mechanism cannot perform the requested operation in the current session.

View Source
var ErrInvalidRecording = errors.New("invalid recording options")

ErrInvalidRecording reports malformed recording options.

View Source
var ErrPointerSessionClosed = errors.New("pointer session is closed")

ErrPointerSessionClosed means a pointer session has ended, because it was closed, ran out of time or its context ended, and its held buttons have been released.

Functions

func RecordDesktop

func RecordDesktop(ctx context.Context, driver DesktopDriver, options media.RecordingOptions) (media.Recording, error)

RecordDesktop starts a bounded recording through the selected driver.

func SendDesktopInput

func SendDesktopInput(ctx context.Context, driver DesktopDriver, input SurfaceInput, options InputOptions) error

SendDesktopInput sends one bounded operation through the selected driver. Drivers that only implement keyboard input must refuse pointer operations explicitly instead of emulating them.

func SendWindowInput

func SendWindowInput(ctx context.Context, target WindowInfo, input SurfaceInput, options InputOptions) error

SendWindowInput sends one bounded operation to a revalidated local window. It does not choose or activate the window, hold input across calls, implement an approval policy, or expose a remote protocol.

Types

type AccessibilityElement

type AccessibilityElement struct {
	ID             string         `json:"id"`
	ParentID       string         `json:"parent_id,omitempty"`
	PID            int            `json:"pid"`
	ProcessStartID string         `json:"process_start_id"`
	Role           string         `json:"role"`
	Identifier     string         `json:"identifier,omitempty"`
	Label          string         `json:"label,omitempty"`
	Value          string         `json:"value,omitempty"`
	Bounds         *SurfaceBounds `json:"bounds,omitempty"`
	Actions        []string       `json:"actions,omitempty"`
	Enabled        *bool          `json:"enabled,omitempty"`
	Focused        *bool          `json:"focused,omitempty"`
}

AccessibilityElement is a caller-visible node in the focused application's native accessibility tree. ID and ParentID are opaque snapshot identities; they are not native handles and cannot be used to invoke actions.

type AccessibilityOptions

type AccessibilityOptions struct {
	MaxElements int
	MaxDepth    int
	Timeout     time.Duration
	IncludeText bool
}

AccessibilityOptions bounds traversal. Text is omitted by default because labels and values may contain document or form contents. Zero bounds select 1024 elements, depth 16, and a five-second deadline.

type AccessibilitySnapshot

type AccessibilitySnapshot struct {
	Platform   string                 `json:"platform"`
	ObservedAt time.Time              `json:"observed_at"`
	Status     proc.CollectionStatus  `json:"status"`
	Elements   []AccessibilityElement `json:"elements"`
}

AccessibilitySnapshot is a bounded, read-only observation of the focused local application's UI tree. It never contains native handles or an action endpoint. Status distinguishes an empty tree from denied or unsupported native observation.

func DiscoverAccessibility

func DiscoverAccessibility(ctx context.Context, options AccessibilityOptions) (AccessibilitySnapshot, error)

DiscoverAccessibility observes the focused local application's native UI structure. It does not request permission, mutate attributes, invoke actions, synthesize input, capture pixels, or communicate with another process over a transport.

type CaptureOptions

type CaptureOptions struct {
	MaxPixels int
	MaxBytes  int
	Timeout   time.Duration
}

CaptureOptions bounds native allocation and encoded output. Zero values select 16,777,216 pixels, 16 MiB, and a five-second deadline.

type DesktopDriver

DesktopDriver is a caller-selected native desktop mechanism, such as the X11 or Wayland tool adapters. Operations are desktop-scoped because those tools cannot address an observed window; window-scoped capture and input remain in CaptureWindow and SendWindowInput.

It is an interface rather than a registry: the shared library never selects a driver by name or falls back to another one.

type DesktopEndpoint

type DesktopEndpoint struct {
	Available bool   `json:"available"`
	Reason    string `json:"reason,omitempty"`
}

DesktopEndpoint is evidence that a local desktop display endpoint is visible to this process. It never contains a socket path, credential, or remote host.

type DesktopInfo

type DesktopInfo struct {
	Platform      string          `json:"platform"`
	Interactive   bool            `json:"interactive"`
	Containerized bool            `json:"containerized"`
	Capabilities  []string        `json:"capabilities,omitempty"`
	X11           DesktopEndpoint `json:"x11"`
	Wayland       DesktopEndpoint `json:"wayland"`
	Recording     RecordingInfo   `json:"recording"`
	Reason        string          `json:"reason,omitempty"`
	ObservedAt    time.Time       `json:"observed_at"`
}

DesktopInfo is a point-in-time observation of the native desktop visible to the current process. It does not describe a remote display or authorize use.

func DiscoverDesktop

func DiscoverDesktop(ctx context.Context) (DesktopInfo, error)

DiscoverDesktop observes only the local native desktop and its recording prerequisites. It performs no network access and launches no process.

type DisplayInfo

type DisplayInfo struct {
	ID      string        `json:"id"`
	Bounds  SurfaceBounds `json:"bounds"`
	Scale   float64       `json:"scale,omitempty"`
	Primary bool          `json:"primary"`
}

DisplayInfo describes a display visible to the current process. ID is an opaque observation identity, not a native handle.

type InputOptions

type InputOptions struct {
	MaxTextRunes int
	Timeout      time.Duration
}

InputOptions bounds one atomic native input operation. Zero values select a 4096-rune text ceiling and a five-second deadline.

type NativeDesktop

type NativeDesktop struct {
	// contains filtered or unexported fields
}

NativeDesktop is a DesktopDriver for the whole desktop of the current macOS or Windows session. It uses the operating system's own capture and event APIs, not an external tool, so it is a platform implementation of the surface contract and not an adapter.

Desktop coordinates start at the top-left corner of all displays combined, so every position is non-negative and every display is addressable. On macOS one pixel of a capture is one point of input. On Windows coordinates are pixels, and the process should be DPI-aware so that Windows does not scale them.

Recording needs an encoder, which is a tool; the caller supplies one with NativeDesktopOptions.Recorder (adapters/ffmpeg is one). Without it Record is refused.

func NewNativeDesktop

func NewNativeDesktop(options NativeDesktopOptions) (*NativeDesktop, error)

NewNativeDesktop returns the native desktop driver, or an error wrapping ErrDesktopUnavailable where the platform has none (anything but macOS built with cgo, and Windows). The caller selects it explicitly; the library never falls back to it.

func (*NativeDesktop) Capture

func (d *NativeDesktop) Capture(ctx context.Context, options CaptureOptions) (SurfaceCapture, error)

Capture captures every display as one PNG image.

func (*NativeDesktop) Input

func (d *NativeDesktop) Input(ctx context.Context, input SurfaceInput, options InputOptions) error

Input sends one atomic operation to the desktop. Pointer positions must be inside the displays. The driver holds nothing across calls; see OpenPointerSession for held buttons.

func (*NativeDesktop) Name

func (d *NativeDesktop) Name() string

Name identifies the driver for diagnostics: "native-macos" or "native-windows".

func (*NativeDesktop) OpenPointerSession

func (d *NativeDesktop) OpenPointerSession(ctx context.Context, options PointerSessionOptions) (*PointerSession, error)

OpenPointerSession starts a pointer session. It ends when Close is called, when MaxDuration passes or when ctx is done.

func (*NativeDesktop) Record

Record starts a recording through the recorder supplied in NativeDesktopOptions.

type NativeDesktopOptions

type NativeDesktopOptions struct {
	// Recorder produces recordings for Record. It may be nil.
	Recorder media.Recorder
}

NativeDesktopOptions configures NewNativeDesktop.

type PermissionInfo

type PermissionInfo struct {
	State  string `json:"state"`
	Scope  string `json:"scope,omitempty"`
	Detail string `json:"detail,omitempty"`
}

PermissionInfo reports a native capability's current state without asking for access. State is ready, denied, not-determined, not-required, unsupported, or unavailable. Scope names the OS permission when one exists.

type PermissionSnapshot

type PermissionSnapshot struct {
	Platform        string         `json:"platform"`
	ObservedAt      time.Time      `json:"observed_at"`
	Accessibility   PermissionInfo `json:"accessibility"`
	ScreenRecording PermissionInfo `json:"screen_recording"`
}

PermissionSnapshot contains local OS permission state relevant to desktop observation. Discovery never opens settings or displays a native prompt.

func DiscoverPermissions

func DiscoverPermissions(ctx context.Context) (PermissionSnapshot, error)

DiscoverPermissions reads current local permission state without requesting access or changing OS settings.

type PointerSession

type PointerSession struct {
	// contains filtered or unexported fields
}

PointerSession holds pointer buttons down across calls, for press-move-release gestures a remote viewer needs. Holding is only possible inside a session so that nothing can leave a button down: Close, the session's time limit and the end of its context all release every held button. A session is safe for concurrent use; calls are serialized.

func (*PointerSession) Close

func (s *PointerSession) Close() error

Close ends the session and releases every held button at the last pointer position. It is safe to call more than once and after the session has ended by itself.

func (*PointerSession) Down

func (s *PointerSession) Down(ctx context.Context, point SurfacePoint, button string) error

Down presses a button at a position and holds it until Up or Close.

func (*PointerSession) Move

func (s *PointerSession) Move(ctx context.Context, point SurfacePoint) error

Move moves the pointer. While a button is held it drags with that button.

func (*PointerSession) Up

func (s *PointerSession) Up(ctx context.Context, point SurfacePoint, button string) error

Up releases a held button at a position.

type PointerSessionOptions

type PointerSessionOptions struct {
	// MaxDuration ends the session, releasing held buttons, after this long. Zero selects
	// five minutes; the ceiling is one hour.
	MaxDuration time.Duration
}

PointerSessionOptions bounds one pointer session.

type RecordingInfo

type RecordingInfo struct {
	Status           string    `json:"status"`
	EncoderAvailable bool      `json:"encoder_available"`
	CaptureAvailable bool      `json:"capture_available"`
	Permission       string    `json:"permission"`
	Formats          []string  `json:"formats,omitempty"`
	Message          string    `json:"message,omitempty"`
	CheckedAt        time.Time `json:"checked_at"`
}

RecordingInfo describes local recording prerequisites. Permission is an identifier for the embedding application to interpret; discovery never opens settings, requests permission, or starts a capture.

type SurfaceBounds

type SurfaceBounds struct {
	X      int `json:"x"`
	Y      int `json:"y"`
	Width  int `json:"width"`
	Height int `json:"height"`
}

SurfaceBounds is a rectangle in the snapshot's coordinate space. Width and height are always positive. Coordinates may be negative on multi-display desktops.

type SurfaceCapture

type SurfaceCapture struct {
	Format     string    `json:"format"`
	Width      int       `json:"width"`
	Height     int       `json:"height"`
	Data       []byte    `json:"data"`
	CapturedAt time.Time `json:"captured_at"`
}

SurfaceCapture is an encoded image captured from one observed window. Data contains the image bytes directly; transport encodings belong to callers.

func CaptureDesktop

func CaptureDesktop(ctx context.Context, driver DesktopDriver, options CaptureOptions) (SurfaceCapture, error)

CaptureDesktop captures the caller's desktop through an explicitly selected driver. Bounds and result validation are the same as CaptureWindow.

func CaptureWindow

func CaptureWindow(ctx context.Context, target WindowInfo, options CaptureOptions) (SurfaceCapture, error)

CaptureWindow captures a currently observed window after revalidating its opaque ID and process generation. It never prompts for permission, selects a target, persists the image, or applies caller policy.

type SurfaceInput

type SurfaceInput struct {
	Kind      SurfaceInputKind `json:"kind"`
	Point     SurfacePoint     `json:"point,omitempty"`
	End       SurfacePoint     `json:"end,omitempty"`
	Button    string           `json:"button,omitempty"`
	DeltaX    int              `json:"delta_x,omitempty"`
	DeltaY    int              `json:"delta_y,omitempty"`
	Text      string           `json:"text,omitempty"`
	Key       string           `json:"key,omitempty"`
	Modifiers []string         `json:"modifiers,omitempty"`
}

SurfaceInput describes one atomic operation. Point is used by pointer operations, End by drag, DeltaX/DeltaY by scroll, Text by keyboard-text, and Key/Modifiers by keyboard-key.

type SurfaceInputKind

type SurfaceInputKind string

SurfaceInputKind identifies one bounded, atomic native input operation.

const (
	SurfaceInputMove   SurfaceInputKind = "pointer-move"
	SurfaceInputClick  SurfaceInputKind = "pointer-click"
	SurfaceInputDrag   SurfaceInputKind = "pointer-drag"
	SurfaceInputScroll SurfaceInputKind = "pointer-scroll"
	SurfaceInputText   SurfaceInputKind = "keyboard-text"
	SurfaceInputKey    SurfaceInputKind = "keyboard-key"
)

type SurfaceOptions

type SurfaceOptions struct {
	MaxDisplays int
	MaxWindows  int
	Timeout     time.Duration
}

SurfaceOptions bounds returned metadata and collection time. Zero values select 64 displays, 4096 windows, and a five-second deadline.

type SurfacePoint

type SurfacePoint struct {
	X int `json:"x"`
	Y int `json:"y"`
}

SurfacePoint uses window-local coordinates in the current window bounds.

type SurfaceSnapshot

type SurfaceSnapshot struct {
	Platform        string                `json:"platform"`
	CoordinateSpace string                `json:"coordinate_space"`
	ObservedAt      time.Time             `json:"observed_at"`
	Displays        []DisplayInfo         `json:"displays"`
	Windows         []WindowInfo          `json:"windows"`
	DisplayStatus   proc.CollectionStatus `json:"display_status"`
	WindowStatus    proc.CollectionStatus `json:"window_status"`
}

SurfaceSnapshot is a bounded, non-atomic observation of local displays and windows. Separate statuses distinguish an empty desktop from unavailable or truncated native data.

func DiscoverSurfaces

func DiscoverSurfaces(ctx context.Context, options SurfaceOptions) (SurfaceSnapshot, error)

DiscoverSurfaces observes local display geometry and caller-visible windows. It does not request permission, capture pixels, read window titles, expose native handles, synthesize input, or contact a remote service.

type WindowInfo

type WindowInfo struct {
	ID             string        `json:"id"`
	PID            int           `json:"pid"`
	ProcessStartID string        `json:"process_start_id"`
	Bounds         SurfaceBounds `json:"bounds"`
	OnScreen       bool          `json:"on_screen"`
}

WindowInfo describes an on-screen top-level window. It intentionally omits titles, application names, native handles, pixels, and input endpoints. ProcessStartID binds PID to a process generation when the OS exposes one.

Jump to

Keyboard shortcuts

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