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 and run off the event loop — a hostile peer can cost an error on a paste, never unbounded memory or a stalled frame.
Index ¶
- Variables
- type Clipboard
- func (c *Clipboard) HandleDataDeviceDataOffer(ev wl.DataDeviceDataOfferEvent)
- func (c *Clipboard) HandleDataDeviceSelection(ev wl.DataDeviceSelectionEvent)
- func (c *Clipboard) HandleDataSourceCancelled(wl.DataSourceCancelledEvent)
- func (c *Clipboard) HandleDataSourceSend(ev wl.DataSourceSendEvent)
- func (c *Clipboard) HandleZwpPrimarySelectionDeviceV1DataOffer(ev wlr.ZwpPrimarySelectionDeviceV1DataOfferEvent)
- func (c *Clipboard) HandleZwpPrimarySelectionDeviceV1Selection(ev wlr.ZwpPrimarySelectionDeviceV1SelectionEvent)
- func (c *Clipboard) HandleZwpPrimarySelectionSourceV1Cancelled(wlr.ZwpPrimarySelectionSourceV1CancelledEvent)
- func (c *Clipboard) HandleZwpPrimarySelectionSourceV1Send(ev wlr.ZwpPrimarySelectionSourceV1SendEvent)
- func (c *Clipboard) Offers(mime string) bool
- func (c *Clipboard) Read(prefs []string, done func(data []byte, mime string, err error))
- func (c *Clipboard) ReadHTML(done func(string, error))
- func (c *Clipboard) ReadImageBytes(done func(data []byte, mime string, err error))
- func (c *Clipboard) ReadPrimary(done func(string, error))
- func (c *Clipboard) ReadText(done func(string, error))
- func (c *Clipboard) ReadURIs(done func([]string, error))
- func (c *Clipboard) Reset(sess *wlsession.Session)
- func (c *Clipboard) SetScheduler(after func(d time.Duration, fn func()), invoke func(fn func()))
- func (c *Clipboard) Stop()
- func (c *Clipboard) Write(content transfer.Content) error
- func (c *Clipboard) WriteExpiring(content transfer.Content, ttl time.Duration) error
- func (c *Clipboard) WriteImage(img image.Image) error
- func (c *Clipboard) WritePrimary(s string, serial uint32) error
- func (c *Clipboard) WriteText(s string) error
Constants ¶
This section is empty.
Variables ¶
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; call it on the event loop goroutine. Transfers run on their own goroutines (xfer.Transfers) and report back through the scheduler's invoke.
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 ¶
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 ¶
Offers reports whether the selection offers mime - what a paste target checks before choosing how to read.
func (*Clipboard) Read ¶
Read delivers the selection's payload as the best of prefs it offers, with that mime, to done on the loop goroutine, always on a later pass: the loop never waits for the peer. 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 as errors, so paste callers react to a rejected paste instead of an OOM. At most xfer.MaxTransfers run at once; past that a read fails with xfer.ErrBusy.
While this client holds the selection, the payload comes from the claim directly, with no pipe.
func (*Clipboard) ReadHTML ¶
ReadHTML delivers the selection's formatted text as HTML, as Read does.
func (*Clipboard) ReadImageBytes ¶
ReadImageBytes delivers the selection's image payload - the best of transfer.ImageMimes the offer carries - as encoded bytes with its mime type, as Read does. Decoding is the caller's: the widget pipeline decodes off the loop goroutine.
func (*Clipboard) ReadPrimary ¶
ReadPrimary delivers the current primary selection as text. It fails with ErrUnavailable when the protocol is missing or there is nothing to read; everything else is as in Read - the two selections share one hardened transfer, and this client's own claim is served in process.
func (*Clipboard) ReadURIs ¶
ReadURIs delivers the selection's uri list - files a file manager copied, say; transfer.FilePaths keeps the local ones - as Read does.
func (*Clipboard) Reset ¶
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 ¶
SetScheduler installs how the clipboard reaches its event loop: after runs fn on the loop goroutine once d passed, and invoke runs fn there as soon as it can, from any goroutine. An Application installs its own (Application.SetClipboard); reads and WriteExpiring need one.
func (*Clipboard) Stop ¶ added in v0.2.5
func (c *Clipboard) Stop()
Stop ends every running transfer and waits for them: an application stops its clipboard when its loop ends, and Reset stops the last session's. No done queued by then is owed a delivery.
func (*Clipboard) Write ¶
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 ¶
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 ¶
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 ¶
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.