clipboard

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 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 clipboard implements text copy and paste over the core wl_data_device protocol and, when the compositor offers it, the zwp_primary_selection_unstable_v1 protocol: the offer pipeline for reading a selection, and data sources for claiming either. The other end of every transfer is a foreign client, so reads and writes are bounded in size and time through internal/xfer — a hostile peer can cost an error on a paste, never unbounded memory or a hung loop.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnavailable = errors.New("clipboard: no selection available")

ErrUnavailable reports that there is no selection or none in a format we can read.

Functions

This section is empty.

Types

type Clipboard

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

Clipboard tracks the seat's selections and claims them on behalf of the app. Create one after connecting; it is not safe for concurrent use.

The regular selection (ctrl+c/x/v) travels over wl_data_device. The primary selection (the X11-style PRIMARY that a middle click pastes) travels over zwp_primary_selection_unstable_v1 when the compositor advertises it; without it every primary call reports ErrUnavailable. The two selections stay independent — their offer mime sets and source payloads never mix.

The data device also carries drag-and-drop (internal/dragdrop), whose offers advertise mime types through the same wl_data_offer objects. Each offer therefore tracks its own mime set (offerMimes), and only an offer the compositor promotes with a selection event can ever satisfy ReadText: a drag passing through never touches the selection.

func New

func New(sess *wlsession.Session) *Clipboard

New wires the clipboard to the session's data device — and to the primary selection device when the compositor has one — and starts tracking selection offers.

func (*Clipboard) HandleDataDeviceDataOffer

func (c *Clipboard) HandleDataDeviceDataOffer(ev wl.DataDeviceDataOfferEvent)

HandleDataDeviceDataOffer implements wl.DataDeviceDataOfferHandler: a new offer appears; start listening for its mime types. Selection and drag offers both pass here; only a later selection event makes one the clipboard's.

func (*Clipboard) HandleDataDeviceSelection

func (c *Clipboard) HandleDataDeviceSelection(ev wl.DataDeviceSelectionEvent)

HandleDataDeviceSelection implements wl.DataDeviceSelectionHandler: the offer is now the selection; a nil offer clears it.

func (*Clipboard) HandleDataSourceCancelled

func (c *Clipboard) HandleDataSourceCancelled(wl.DataSourceCancelledEvent)

HandleDataSourceCancelled implements wl.DataSourceCancelledHandler: another claim replaced ours.

func (*Clipboard) HandleDataSourceSend

func (c *Clipboard) HandleDataSourceSend(ev wl.DataSourceSendEvent)

HandleDataSourceSend implements wl.DataSourceSendHandler: a consumer asked for the regular selection's data.

func (*Clipboard) HandleZwpPrimarySelectionDeviceV1DataOffer

func (c *Clipboard) HandleZwpPrimarySelectionDeviceV1DataOffer(ev wlr.ZwpPrimarySelectionDeviceV1DataOfferEvent)

HandleZwpPrimarySelectionDeviceV1DataOffer implements wlr.ZwpPrimarySelectionDeviceV1DataOfferHandler: a new primary selection offer appears; start listening for its mime types.

func (*Clipboard) HandleZwpPrimarySelectionDeviceV1Selection

func (c *Clipboard) HandleZwpPrimarySelectionDeviceV1Selection(ev wlr.ZwpPrimarySelectionDeviceV1SelectionEvent)

HandleZwpPrimarySelectionDeviceV1Selection implements wlr.ZwpPrimarySelectionDeviceV1SelectionHandler: the offer is now the primary selection; a nil offer clears it.

func (*Clipboard) HandleZwpPrimarySelectionSourceV1Cancelled

func (c *Clipboard) HandleZwpPrimarySelectionSourceV1Cancelled(wlr.ZwpPrimarySelectionSourceV1CancelledEvent)

HandleZwpPrimarySelectionSourceV1Cancelled implements wlr.ZwpPrimarySelectionSourceV1CancelledHandler.

func (*Clipboard) HandleZwpPrimarySelectionSourceV1Send

func (c *Clipboard) HandleZwpPrimarySelectionSourceV1Send(ev wlr.ZwpPrimarySelectionSourceV1SendEvent)

HandleZwpPrimarySelectionSourceV1Send implements wlr.ZwpPrimarySelectionSourceV1SendHandler: a consumer asked for the primary selection's data.

func (*Clipboard) Offers

func (c *Clipboard) Offers(mime string) bool

Offers reports whether the selection offers mime - what a paste target checks before choosing how to read.

func (*Clipboard) Read

func (c *Clipboard) Read(prefs ...string) ([]byte, string, error)

Read returns the selection's payload as the best of prefs it offers, with that mime, blocking until the compositor delivers it. Errors with ErrUnavailable when there is no selection or none of prefs. The transfer is bounded against hostile peers: a selection past xfer.MaxPayload is refused (the fd drained so the offering peer never blocks) and a peer that stalls is cut off after the transfer deadline - both surface here as errors, so paste callers react to a rejected paste instead of an OOM or a hung window.

While this client holds the selection, the payload comes from the claim directly: the wire path would make the loop goroutine both the pipe's reader and, in its send handler, its writer, which stalls until the transfer deadline wherever the compositor delivers the send after the flush roundtrip (Hyprland), and on any payload larger than the pipe buffer.

func (*Clipboard) ReadHTML

func (c *Clipboard) ReadHTML() (string, error)

ReadHTML returns the selection's formatted text as HTML.

func (*Clipboard) ReadImageBytes

func (c *Clipboard) ReadImageBytes() ([]byte, string, error)

ReadImageBytes returns the selection's image payload - the best of transfer.ImageMimes the offer carries - as encoded bytes with its mime type. Decoding is the caller's: the widget pipeline decodes off the loop goroutine.

func (*Clipboard) ReadPrimary

func (c *Clipboard) ReadPrimary() (string, error)

ReadPrimary returns the current primary selection as text. Errors with ErrUnavailable when the protocol is missing or there is nothing to read; oversize and stalled peers surface as errors exactly as in Read - the two selections share one hardened transfer - and, as in Read, this client's own claim is served in process.

func (*Clipboard) ReadText

func (c *Clipboard) ReadText() (string, error)

ReadText returns the selection as text.

func (*Clipboard) ReadURIs

func (c *Clipboard) ReadURIs() ([]string, error)

ReadURIs returns the selection's uri list - files a file manager copied, say; transfer.FilePaths keeps the local ones.

func (*Clipboard) Reset

func (c *Clipboard) Reset(sess *wlsession.Session)

Reset attaches the clipboard to sess, forgetting everything the previous session held - its offers, the selections, our claims: a compositor restart takes the clipboard's contents with it, so after a reconnect the clipboard starts empty on the new session.

func (*Clipboard) SetScheduler

func (c *Clipboard) SetScheduler(after func(d time.Duration, fn func()))

SetScheduler installs how the clipboard defers work to its event loop: after runs fn on the loop goroutine once d passed. An Application installs its own (Application.SetClipboard); WriteExpiring needs one.

func (*Clipboard) Write

func (c *Clipboard) Write(content transfer.Content) error

Write claims the selection with content, served to every consumer that asks until another client or app replaces the selection. The transfer builders (transfer.Text, Image, Files, HTML, Merge) encode up front, so serving never encodes under a peer's deadline. The claim carries the serial of the latest input event (the key or click that copied); before any it fails with wlsession.ErrNoInputEvent.

func (*Clipboard) WriteExpiring

func (c *Clipboard) WriteExpiring(content transfer.Content, ttl time.Duration) error

WriteExpiring is Write with a lifetime, for a secret a password manager copies: after ttl, if this claim still holds the selection - no other client and no later write replaced it - the selection clears (set_selection with no source, on the claim's serial). A replaced claim is left alone, so a later, unrelated copy survives.

func (*Clipboard) WriteImage

func (c *Clipboard) WriteImage(img image.Image) error

WriteImage claims the selection with img, as PNG and JPEG encoded here on the caller's goroutine. The primary selection is untouched: copy-on-select keeps its text semantics.

func (*Clipboard) WritePrimary

func (c *Clipboard) WritePrimary(s string, serial uint32) error

WritePrimary claims the primary selection with s as its text content. serial is the serial of the input event that triggered the claim - the protocol asks for the triggering press, so callers pass the pointer (or keyboard) serial they received. The source stays alive until another client or app replaces the selection.

func (*Clipboard) WriteText

func (c *Clipboard) WriteText(s string) error

WriteText claims the selection with s as text.

Jump to

Keyboard shortcuts

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