gfx.wayland.window

gfx.wayland

window(opts) -> window, err

Open a Wayland window

opts fielddefaultdescription
w, h800, 600the logical size, when the compositor leaves it to the window
titlelilgamethe window's title, cut as set_title cuts it
app_idlilgamethe app id a compositor's rules match
display$WAYLAND_DISPLAYthe socket's name, or an absolute path
opengfx.wl_core.connectthe connector; a suite hands in one on a scripted compositor

The open connects, reads the globals, and makes the surface, the toplevel and, where the compositor has them, the viewport, the fractional scale, the server-side decoration and a device for each selection. It binds xdg_activation_v1 when offered. It waits for the first configure, up to 5 s, and returns with the canvas at the device size and nothing on screen: the first commit maps the window. The first configure is the first event.

Returns nil and an error when the socket is not found or refuses, when the compositor lacks wl_compositor, wl_shm or xdg_wm_base, or when it does not answer in time. A missing optional global turns its feature off and shows in info().

methoddescription
w:surface()the canvas, at the device size; the same table for the window's life
w:damage(x, y, w, h)name a changed rectangle of the canvas, coordinates from 1, clipped
w:commit()hand the damage to the compositor; true when it sent a frame, false when nothing was damaged
w:events(timeout)the events queued, waiting up to timeout s (nil: no limit) when none; {} when the wait ends empty
w:set_title(text)send the toplevel a new title and flush; returns the text sent
w:set_cursor(name)the pointer's shape over the window, by cursor-shape-v1's name; returns it
w:set_selection(which, text)copy text to the clipboard or the primary selection, nil to clear it; false when that selection is off
w:selection(which, opts)the text of the clipboard or the primary selection; nil when none is offered, nil and an error at the timeout or the cap
w:ask_attention()ask the compositor for attention; true when asked or still asking, false without xdg_activation_v1
w:size()w, h, device_w, device_h, scale: the logical size, the canvas's, the scale in 120ths
w:owed()true while the last frame's callback has not come
w:frame(timeout)wait up to timeout s (nil: no limit) for the last frame's callback; true when none is owed, false at the timeout
w:info()what the compositor offered and what the window uses, below
w:destroy()destroy the window and close the connection; safe to call twice

The events:

A pointer, button or wheel event's mods is the set a key event carries: the seat's modifiers at the event. It is empty while the window holds no keyboard focus, and without a keymap.

The window binds the seat's keyboard when the seat has one, and reads its keymap with gfx.xkb. It repeats a held key itself, at the compositor's repeat_info, 25 a second after 600 ms until one comes: events wakes for the next repeat and queues it. A repeat comes for the last key pressed that the keymap lets repeat, and stops on its release, on a focus loss and at a rate of 0. A key that does not repeat, a modifier, leaves a repeat running. A reader that falls behind gets one repeat, not the ones it missed.

The window binds the seat's pointer when the seat has one. A pointer position is the canvas pixel under the hotspot, counted from 1: floor(logical * scale / 120) + 1. It is not clamped: a drag held past the edge reads under 1 or past the device size. The compositor groups a pointer's events into frames, and the window queues a frame's events when it ends, in the order they came; the parts of one axis make one wheel event. A seat older than version 5 sends no frame, and each event is queued as it comes. steps is axis_value120 / 120 from a version 8 seat and axis_discrete before.

The window shows the shape set_cursor last named, default at first, on every enter, under the enter's serial. A compositor with wp_cursor_shape_manager_v1 draws it by name. Without it the window draws its own small arrow for every name, at the scale rounded up, and parses no cursor theme. A name cursor-shape-v1's version 1 does not know raises.

commit waits for the previous frame's callback when one is owed, up to a second; a callback that never comes costs that second, as a window the compositor hides may never get one. It packs the damage into a buffer the compositor does not hold, repainting first what that buffer missed while others were shown, and sends the configure's ack, the viewport's size or the buffer scale, the attach, the damage, a frame callback and the commit. With nothing damaged and no configure to ack it sends nothing.

damage drops a rectangle that one already named holds, and a new one takes the place of those it holds. commit does not repaint a missed rectangle that one rectangle of the damage holds: the damage packs it. So a first frame, and the frame after a resize, pack the canvas once. Rectangles that overlap in part are each packed whole.

frame waits for that callback before the caller draws. A caller that draws first and lets commit wait shows a state one callback old. With no callback owed frame answers true at once, so the first frame does not wait. It reads what has arrived before it looks, and frame(0) asks without waiting. At the timeout it answers false and the callback stays owed: the compositor asked for no frame, as for a window out of sight. The caller may then skip the draw and wait again. A commit after a false waits its own second. frame sends nothing.

The window binds wl_data_device_manager for the clipboard and zwp_primary_selection_device_manager_v1 for the primary selection, each with a device on the seat. A compositor without one turns that selection off: set_selection answers false and selection nil.

set_selection makes a source that offers text/plain;charset=utf-8, text/plain, UTF8_STRING and STRING, all the same bytes. It sets the source under the serial of the last key, button or keyboard enter, and keeps the text. A client that pastes hands the window an fd, and the window writes the text to it. Inside a task a writer task of its own writes, and on the main coroutine the write blocks. A peer that takes no bytes for 5 s, or has gone, ends the write. A text that is neither a string nor nil raises, and so does a which of neither name.

selection answers the window's own text while its source holds the selection, with no round trip. Otherwise it picks the first type the offer has of text/plain;charset=utf-8, UTF8_STRING, text/plain and STRING. It hands the owner a pipe and reads it until the owner closes it. opts.timeout, default 2, is the seconds the read waits for a byte; opts.max, default 16 MiB, caps the text. Past either it answers nil and why. The bytes come as the owner sent them: the window checks no encoding.

ask_attention asks through xdg_activation_v1. It takes a token for the window's surface with no input serial, and activates the surface with it when the token comes. A compositor reads an activation with no serial as a plea, not a focus steal: it may mark the window urgent, focus it, or do nothing. One ask stands at a time: a second while the token is on its way sends nothing and answers true. A later ask takes a fresh token. A compositor without xdg_activation_v1 turns it off: ask_attention sends nothing and answers false.

set_title sends text up to its first NUL, which ends a string on the wire. A title past 4083 bytes, what one message holds, is cut there on a codepoint's edge. The bytes go out as given: the window does not check that they are UTF-8. A text that is no string raises.

The waits in commit, events, frame, set_title, set_cursor, set_selection, selection, ask_attention and the open yield to LEV inside a task, and block in poll on the main coroutine. One task may wait in events while another commits or waits in frame: the window is the socket's one reader, and the second sleeps until the first has read. One task commits at a time. A destroy ends every wait in flight with a raise.

info() holds path; globals, every interface the compositor advertised and its version; bound, the versions bound; fractional and viewporter; decoration, server, client or nil when the compositor has no decoration protocol or has not answered; scale_mode, fractional or integer; buffer_scale; scale; seat, the seat's name; keyboard, true while the seat's keyboard is bound; keymap_groups, the keymap's group names; keymap_error, why the keymap is missing; repeat_rate and repeat_delay; pointer, true while the seat's pointer is bound; cursor, shape when the compositor draws the shape, image when the window draws its arrow, nil without a pointer; cursor_shape, the name set_cursor last set; clipboard and primary, true while that selection's device is bound; commits; and buffers, the buffers made at the current size.

A protocol error from the compositor, or a lost connection, raises from the call that reads it, and from every call after.