platform

package
v0.0.0-...-11816a6 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 22 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 platform owns the operating system: windows, the event queue, keyboard and mouse input, display scaling, window visibility, the clipboard and Vulkan surface creation.

Each operating system has its own implementation of App and Window in a build-tagged file with an identical method set, so the rest of the engine compiles against one API and the linker picks the platform. Events are translated into the platform-independent Event type here; nothing above this package sees a native handle.

All calls must happen on the main goroutine, which the package pins to the main OS thread in init, because window systems require it.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoClipboard = errors.New("platform: clipboard is not available on this platform")

ErrNoClipboard is returned where the platform layer has no clipboard.

View Source
var ErrNoInputYet = errors.New("platform: the Wayland clipboard cannot be set before the window has had input")

ErrNoInputYet is returned when the Wayland clipboard is written before the window has had any input. Changing the selection quotes the serial of a key, a button or a pointer or keyboard entering the window, and until one of those has happened there is no serial to quote.

View Source
var ErrNoWayland = errors.New("platform: cannot connect to a Wayland compositor")

ErrNoWayland is returned when no Wayland compositor can be reached.

View Source
var ErrNoX11 = errors.New("platform: cannot connect to an X server (is DISPLAY set?)")

ErrNoX11 is returned when no X server can be reached.

View Source
var ErrUnsupported = errors.New("platform: operation unsupported by this backend")

ErrUnsupported identifies an operation unavailable in the active backend.

Functions

func Backend

func Backend() string

Backend names the window system the layer opened: "wayland" or "x11" on Linux, and the operating system's name elsewhere. The engine logs it at startup, because which one a Linux session got is not otherwise visible.

func RequiredInstanceExtensions

func RequiredInstanceExtensions() []string

RequiredInstanceExtensions lists the instance extensions a surface needs. The backend NewApp chose decides which one: VK_KHR_wayland_surface under Wayland and VK_KHR_xcb_surface under X11.

Types

type App

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

App is the process's connection to the window system. Create one per process.

func NewApp

func NewApp() (*App, error)

NewApp connects to the window system, Wayland first and X11 second.

func (*App) Clipboard

func (a *App) Clipboard() (string, error)

Clipboard returns the system clipboard's text, empty when it holds none. Under X11 it asks whoever owns the CLIPBOARD selection and waits about a second for the answer, and under Wayland it reads the offer the compositor gave the seat. Text this process put there is returned without asking anyone. It fails only where the window system has no clipboard for a client with no window, or where the compositor has no wl_data_device_manager.

func (*App) Displays

func (a *App) Displays() ([]Display, error)

Displays uses wl_output's advertised modes on Wayland and active RandR outputs on X11. wl_output geometry does not define reliable logical bounds, so Wayland leaves BoundsKnown false instead of guessing from pixel size.

func (*App) Gamepads

func (a *App) Gamepads() []GamepadState

Gamepads reads every joystick device that can be opened. The slice it returns is reused by the next call, so read it before polling again.

func (*App) KeyboardLayout

func (a *App) KeyboardLayout() (input.KeyboardLayout, error)

func (*App) NewWindow

func (a *App) NewWindow(cfg Config) (*Window, error)

NewWindow opens a window and shows it.

func (*App) Poll

func (a *App) Poll(wait bool) []Event

Poll drains pending events into the returned slice, reused by the next call. With wait set it blocks until at least one event arrives.

func (*App) PollTimeout

func (a *App) PollTimeout(timeout time.Duration) []Event

PollTimeout is Poll that waits at most timeout for the first event. A timeout of zero or less waits for nothing, as Poll(false) does.

func (*App) SetClipboard

func (a *App) SetClipboard(text string) error

SetClipboard puts text on the system clipboard. Both backends hand the text over when another client asks for it, so it stays available for as long as this process owns the selection and no longer. Under Wayland it returns ErrNoInputYet before the window has had any input, because changing the selection quotes the serial of an input event.

func (*App) Wake

func (a *App) Wake()

Wake makes a blocked Poll return, delivering an EventWake. It is safe to call from any goroutine.

type Config

type Config struct {
	Parent    *NativeParent
	Title     string
	Width     int // content size in points (logical pixels)
	Height    int
	Resizable bool
}

Config describes a window to open.

type CursorShape

type CursorShape uint8

CursorShape names a system cursor.

const (
	CursorArrow CursorShape = iota
	CursorHand
	CursorIBeam
	CursorCrosshair
	CursorResizeH
	CursorResizeV
	CursorGrab
	CursorGrabbing
	CursorNotAllowed
)

type Display

type Display struct {
	Name        string
	Bounds      image.Rectangle
	BoundsKnown bool
	Scale       float64
	Current     VideoMode
	Modes       []VideoMode
}

Display describes an attached display. Bounds is in the backend's desktop coordinate space, with a top-left origin, and is valid only when BoundsKnown. Scale is logical-to-physical scaling, or zero when unknown. Modes contains only advertised modes, which may be limited to the current mode by the OS. Names and ordering are snapshots, not persistent device identifiers.

type Event

type Event struct {
	Kind    EventKind
	Window  *Window
	Key     Key
	Mods    Mods
	Repeat  bool
	Rune    rune
	Text    string
	Button  MouseButton
	X, Y    float64
	DX, DY  float64
	Precise bool
	Focused bool
	Visible bool
	Width   int // content size in points
	Height  int
	PixelW  int // framebuffer size in pixels
	PixelH  int
	Scale   float64
}

Event is one thing that happened to a window. Only the fields relevant to Kind are set.

type EventKind

type EventKind uint8

EventKind says what an Event carries.

const (
	EventNone       EventKind = iota
	EventClose                // the user asked the window to close
	EventResize               // content or framebuffer size, or scale, changed
	EventFocus                // Focused says whether the window gained or lost focus
	EventKeyDown              // Key, Mods, Repeat
	EventKeyUp                // Key, Mods
	EventChar                 // Rune: a character of text input
	EventCompose              // Text: the input method's in-progress composition, empty when it ends
	EventMouseMove            // X, Y in points from the top-left of the content area
	EventMouseDown            // Button, X, Y, Mods
	EventMouseUp              // Button, X, Y, Mods
	EventScroll               // DX, DY in lines (or points when Precise)
	EventMouseEnter           // the pointer entered the content area
	EventMouseLeave           // the pointer left the content area
	EventWake                 // App.Wake was called from another goroutine
	EventVisible              // Visible says whether the window became visible or hidden
	EventModifiers            // Mods: modifier state changed independently of a key event
)

func (EventKind) String

func (k EventKind) String() string

type GamepadState

type GamepadState struct {
	Info      input.GamepadInfo
	Connected bool
	Name      string
	Buttons   [input.GamepadButtonCount]bool
	Axes      [input.GamepadAxisCount]float32
}

GamepadState is one controller's inputs as read this poll.

type Key

type Key = input.Key

The input vocabulary is re-exported so that this package's events can be read without importing input as well.

type Mods

type Mods = input.Mods

The input vocabulary is re-exported so that this package's events can be read without importing input as well.

type MouseButton

type MouseButton = input.MouseButton

The input vocabulary is re-exported so that this package's events can be read without importing input as well.

type NativeParent

type NativeParent struct {
	Backend uint8
	Handle  uintptr
}

NativeParent borrows a parent; backend values match the root NativeBackend.

type VideoMode

type VideoMode struct {
	Width, Height int
	RefreshHz     float64
}

VideoMode describes a mode reported by the display system. Dimensions are physical pixels; RefreshHz is zero when the system does not report a rate.

type Window

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

Window is one window. Only the fields of the live backend are set.

func (*Window) Capabilities

func (w *Window) Capabilities() WindowCapabilities

func (*Window) Close

func (w *Window) Close()

Close destroys the window.

func (*Window) Closed

func (w *Window) Closed() bool

Closed reports whether the window was destroyed.

func (*Window) CreateSurface

func (w *Window) CreateSurface(instance vk.VkInstance) (vk.VkSurfaceKHR, error)

CreateSurface makes a VkSurfaceKHR over the window. Under Wayland the swapchain's size comes from the last xdg_toplevel.configure, because the compositor does not resize a surface: the client picks the buffer size and the configure says what it should be.

func (*Window) CursorCaptured

func (w *Window) CursorCaptured() bool

CursorCaptured reports the capture state.

func (*Window) Fullscreen

func (w *Window) Fullscreen() bool

Fullscreen reports whether the window is full screen.

func (*Window) Hide

func (w *Window) Hide() error

func (*Window) PixelSize

func (w *Window) PixelSize() (int, int)

PixelSize is the framebuffer size. X11 reports no scale, so it matches Size there; Wayland multiplies by the fractional scale where the compositor sends one and by the integer buffer scale otherwise.

func (*Window) Position

func (w *Window) Position() (int, int)

func (*Window) RefreshCursor

func (w *Window) RefreshCursor()

func (*Window) RequestFocus

func (w *Window) RequestFocus() error

func (*Window) Scale

func (w *Window) Scale() float64

Scale is pixels per point. It is fractional under a compositor that offers wp_fractional_scale_v1 and a whole number everywhere else.

func (*Window) SetAlwaysOnTop

func (w *Window) SetAlwaysOnTop(on bool) error

func (*Window) SetBounds

func (w *Window) SetBounds(x, y, width, height int) error

func (*Window) SetCursor

func (w *Window) SetCursor(shape CursorShape)

SetCursor picks the pointer's shape, from the cursor theme under Wayland and from the X cursor font under X11.

func (*Window) SetCursorCaptured

func (w *Window) SetCursorCaptured(on bool)

SetCursorCaptured hides the cursor, holds it in the window and reports motion as deltas.

func (*Window) SetCursorImage

func (w *Window) SetCursorImage(img image.Image, hotX, hotY int) error

func (*Window) SetCursorVisible

func (w *Window) SetCursorVisible(on bool)

SetCursorVisible shows or hides the pointer over the window.

func (*Window) SetFullscreen

func (w *Window) SetFullscreen(on bool)

SetFullscreen asks the compositor or the window manager for full screen. Under Wayland the answer arrives as a configure, so Fullscreen only changes once the compositor agrees.

func (*Window) SetIcon

func (w *Window) SetIcon(img image.Image)

SetIcon sets _NET_WM_ICON under X11: width, height and ARGB pixels as cardinals. Under Wayland it sends the pixels through xdg-toplevel-icon-v1, and does nothing where the compositor lacks that protocol, because the icon then comes from the desktop entry whose name matches the app id.

func (*Window) SetPointerPosition

func (w *Window) SetPointerPosition(x, y float64) error

func (*Window) SetPosition

func (w *Window) SetPosition(x, y int)

Window placement is controlled by the compositor or window manager. The engine does not request or report absolute placement on Linux.

func (*Window) SetSize

func (w *Window) SetSize(width, height int) error

func (*Window) SetSizeLimits

func (w *Window) SetSizeLimits(minW, minH, maxW, maxH int)

SetSizeLimits bounds the content size, through xdg_toplevel.set_min_size and set_max_size under Wayland and WM_NORMAL_HINTS under X11; zero lifts a bound.

func (*Window) SetTextInputRect

func (w *Window) SetTextInputRect(x, y, width, height float64)

SetTextInputRect records where text is entered. Neither backend wires an input method yet, so it only stores the rectangle.

func (*Window) SetTitle

func (w *Window) SetTitle(title string)

SetTitle changes the window's title, through xdg_toplevel.set_title under Wayland and WM_NAME and _NET_WM_NAME under X11.

func (*Window) Show

func (w *Window) Show() error

func (*Window) Size

func (w *Window) Size() (int, int)

Size is the content size in points.

func (*Window) Visible

func (w *Window) Visible() bool

Visible reports whether the window can be seen. It is false while the window is minimised, unmapped or wholly covered by other windows. X11 reads MapNotify, VisibilityNotify and _NET_WM_STATE_HIDDEN; Wayland reads the suspended state of xdg_toplevel version six, so a compositor that offers an earlier version always reports true.

type WindowCapabilities

type WindowCapabilities struct {
	Resize, Show, Hide, Focus, AlwaysOnTop, CursorImage, PointerPosition bool
	EmbeddedBounds                                                       bool
}

WindowCapabilities reports requests the active backend can issue. A supported request can still be declined by desktop policy or fail at runtime.

Jump to

Keyboard shortcuts

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