Files
chandra/compositor/wayland/server.mbt
T
Claude 2c33c4a847
CI / test (push) Canceled after 4m14s
Text selection in terminals: drags stay with their window, middle-click paste works
V: "kwi is having problems selecting text". Two compositor gaps:

- No implicit pointer grab. A drag that started in a window lost it as soon as the pointer
  crossed its edge (onto the frame, the panel, a divider or another window): the client got
  wl_pointer.leave mid-drag (Qt then forgets the held button), and the release went to the
  other window or nowhere, so the terminal's selection never ended and kept following the
  pointer. Selecting past the top/bottom edge to scroll, which Kwi and Konsole autoscroll on,
  could not work. Now a press forwarded to a client grabs the pointer: motion (in that
  surface's coordinates, also outside it), the wheel and every button go to it until the last
  button is up; then the pointer goes to whatever is under it. Anything that takes the pointer
  (a window move or resize, the overview, a screenshot) ends the grab as before.
- No primary selection. zwp_primary_selection_device_manager_v1 was not offered, so Qt's
  QClipboard::supportsSelection() was false: selected text was never put on the primary
  selection and a middle click pasted nothing. Now offered (v1), with the offer sent to the
  focused client like the clipboard.

Checked headless with Konsole (Qt 5 Wayland): a selection dragged out of the window keeps
extending, is set as the primary selection on release, and a middle click in another Konsole
window pastes it. Tests in selection_test.mbt (both grab tests fail without the fix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5himKXPLcfCZN6BXByd
2026-11-26 05:07:25 +00:00

2373 lines
71 KiB
MoonBit

///| Pure-MoonBit Wayland server and protocol state machine.
///|
pub(all) enum CompositorEvent {
ToplevelOpened(UInt64, String, String, Int, Int)
ToplevelUpdated(UInt64, String, String)
ToplevelCommitted(UInt64, @scene.ShmBufferSnapshot?)
/// The client acked the compositor's last configure, was asked again, and still drew a
/// different size: (id, w, h) is the size it chose. A floating window takes it (xdg-shell lets
/// a client pick its own size when it is not maximized, fullscreen or tiled).
ToplevelSizeChosen(UInt64, Int, Int)
ToplevelClosed(UInt64)
ToplevelMaximizeRequest(UInt64, Bool)
ToplevelFullscreenRequest(UInt64, Bool)
ToplevelMinimizeRequest(UInt64, Bool)
ToplevelMoveRequest(UInt64)
/// xdg_toplevel.resize from a client-decorated window: (id, edges), the xdg_toplevel
/// resize_edge bits (1 top, 2 bottom, 4 left, 8 right).
ToplevelResizeRequest(UInt64, UInt)
/// Who draws the window's frame, sent with a commit when it changes: (id, client_side, x, y).
/// A client that never asked for server-side decoration (GTK: GNOME Files, most GNOME apps)
/// draws its own title bar and shadow; (x, y) is where its visible window starts inside the
/// buffer (xdg_surface.set_window_geometry), past the shadow.
ToplevelFrame(UInt64, Bool, Int, Int)
/// xdg_popup created: (id, parent window id, x, y, w, h) relative to the parent's content.
PopupOpened(UInt64, UInt64, Int, Int, Int, Int)
PopupCommitted(UInt64, @scene.ShmBufferSnapshot?)
PopupClosed(UInt64)
/// xdg_popup.grab: the popup wants keyboard focus (menus); popups without a grab never get it.
PopupGrabbed(UInt64)
/// xdg_popup.reposition placed it again: (id, x, y, w, h) relative to the parent's content.
PopupMoved(UInt64, Int, Int, Int, Int)
/// xdg_surface.set_window_geometry on a popup: (id, x, y) of the visible menu inside its
/// buffer. The positioner places that point, so the buffer is drawn this much up/left.
PopupGeometry(UInt64, Int, Int)
/// A wl_subsurface of a window committed or moved: (window, child wl_surface id, x, y, buffer)
/// with (x, y) relative to the window's surface (nested subsurfaces added up). The buffer is
/// None when only the position changed. Chrome draws its toolbar bubbles (bookmark star,
/// downloads, extensions) this way.
SubsurfaceCommitted(UInt64, UInt, Int, Int, @scene.ShmBufferSnapshot?)
/// The subsurface was unmapped (null buffer) or destroyed: (window, child wl_surface id).
SubsurfaceUnmapped(UInt64, UInt)
}
///| wl_subsurface state, keyed by the child wl_surface id.
struct SubState {
parent : UInt
mut x : Int
mut y : Int
}
///|
enum ObjKind {
ObjDisplay
ObjRegistry
ObjCompositor
ObjSurface(UInt)
ObjRegion
ObjShm
ObjShmPool(UInt)
ObjBuffer(UInt)
ObjXdgWmBase
ObjXdgPositioner(UInt)
ObjXdgSurface(UInt)
ObjXdgToplevel(UInt64)
ObjXdgPopup(UInt64)
ObjDecoManager
ObjToplevelDeco(UInt64)
/// org_kde_kwin_server_decoration_manager: GTK 3's way of asking for server-side decoration
/// (it has no xdg-decoration support).
ObjKdeDecoManager
/// org_kde_kwin_server_decoration for the wl_surface with this id.
ObjKdeDeco(UInt)
ObjSeat
ObjPointer
ObjKeyboard
ObjTouch
/// wl_output with the version the client bound (done and scale are since version 2).
ObjOutput(UInt)
ObjSubcompositor
ObjSubsurface(UInt)
ObjDataManager
ObjDataSource
ObjDataDevice
/// Selection offer: (owner client id, owner's wl_data_source id).
ObjDataOffer(UInt, UInt)
/// zwp_primary_selection_v1 (the selected text that a middle click pastes): manager,
/// source, device, and an offer of (owner client id, owner's source id).
ObjPrimaryManager
ObjPrimarySource
ObjPrimaryDevice
ObjPrimaryOffer(UInt, UInt)
ObjCursorShapeMgr
ObjCursorShapeDev
}
///|
struct ShmPoolState {
fd : Int
mut size : Int
/// The client destroyed the wl_shm_pool but buffers made from it are still alive
/// (the spec keeps their mapping valid); the fd is closed with the last buffer.
mut destroyed : Bool
}
///|
struct ShmBufferState {
mut pool_id : UInt
offset : Int
width : Int
height : Int
stride : Int
format : Int
}
///|
struct SurfaceState {
/// Pending state: the buffer attached since the last commit. A commit takes it, so a commit
/// without a new attach (a client changing only its regions or geometry) neither shows the
/// old buffer again nor releases it a second time: the client may already be drawing into it.
mut attached_buffer : UInt?
mut pending_frame_cb : UInt?
mut xdg_surface_id : UInt?
mut window_id : UInt64?
/// wl_surface.set_buffer_scale: the buffer is this many times the window's size.
mut buffer_scale : Int
/// The pending attach is a null buffer (the surface is being unmapped).
mut attached_null : Bool
/// org_kde_kwin_server_decoration mode Server: Chandra draws this window's frame (GTK 3 apps
/// without their own header bar, like Webby).
mut kde_ssd : Bool
}
///|
struct XdgSurfaceState {
wl_surface_id : UInt
mut window_id : UInt64?
/// set_window_geometry origin: where the visible window starts inside the buffer (shadows).
mut geo_x : Int
mut geo_y : Int
/// set_window_geometry size (0 until the client sets one: the window is the whole buffer).
mut geo_w : Int
mut geo_h : Int
}
///|
struct ToplevelState {
win_id : UInt64
wl_surface_id : UInt
xdg_surface_id : UInt
wl_toplevel_id : UInt
mut deco_obj_id : UInt?
mut title : String
mut app_id : String
mut min_w : Int
mut min_h : Int
mut opened_sent : Bool
/// xdg_toplevel.set_parent named another toplevel: a dialog or tool window of its app.
mut has_parent : Bool
/// Set for xdg_popup surfaces (they live in `toplevels` so input routing is shared).
popup_parent : UInt64?
/// The last xdg_toplevel.configure sent: size, states and its xdg_surface serial (0 = none).
mut cfg_w : Int
mut cfg_h : Int
mut cfg_states : Array[Byte]
mut cfg_serial : UInt
/// The serial of the last configure the client acked.
mut acked_serial : UInt
/// How far the last configure got with a client drawing another size after acking it:
/// 0 not yet, 1 sent again, 2 reported as ToplevelSizeChosen.
mut cfg_retry : Int
/// The last ToplevelFrame sent (client_side, x, y), None before the first.
mut frame_sent : (Bool, Int, Int)?
/// zxdg_toplevel_decoration_v1.set_mode(client_side): the client wants to draw its own frame
/// (Chrome with "Use system title bar and borders" off) and is told it may.
mut wants_csd : Bool
}
///| xdg_positioner state.
struct PosState {
mut w : Int
mut h : Int
mut ax : Int
mut ay : Int
mut aw : Int
mut ah : Int
mut anchor : UInt
mut gravity : UInt
mut ox : Int
mut oy : Int
/// xdg_positioner.constraint_adjustment bits: 1 slide_x, 2 slide_y, 4 flip_x, 8 flip_y,
/// 16 resize_x, 32 resize_y.
mut adjust : UInt
}
///|
struct ClientConn {
client_id : UInt
fd : Int
rx_buf : Array[Byte]
pending_fds : Array[Int]
objects : Map[UInt, ObjKind]
surfaces : Map[UInt, SurfaceState]
shm_pools : Map[UInt, ShmPoolState]
shm_buffers : Map[UInt, ShmBufferState]
xdg_surfaces : Map[UInt, XdgSurfaceState]
subsurfaces : Map[UInt, SubState]
toplevels : Map[UInt64, ToplevelState]
positioners : Map[UInt, PosState]
pointer_ids : Array[UInt]
keyboard_ids : Array[UInt]
mut seat_ver : UInt
/// wl_data_device ids this client created (they receive selection events).
data_devices : Array[UInt]
/// wl_data_source id -> mime types it offers.
data_sources : Map[UInt, Array[String]]
/// zwp_primary_selection_device_v1 ids this client created.
prim_devices : Array[UInt]
/// zwp_primary_selection_source_v1 id -> mime types it offers.
prim_sources : Map[UInt, Array[String]]
/// Next server-allocated id for a wl_data_offer.
mut next_offer : UInt
/// wp_cursor_shape_v1 shape the client last asked for (0 = never set).
mut cursor_shape : UInt
mut alive : Bool
}
///|
fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn {
let objects : Map[UInt, ObjKind] = {}
objects.set(1U, ObjDisplay)
{
client_id,
fd,
rx_buf: [],
pending_fds: [],
objects,
surfaces: {},
shm_pools: {},
shm_buffers: {},
xdg_surfaces: {},
subsurfaces: {},
toplevels: {},
positioners: {},
pointer_ids: [],
keyboard_ids: [],
seat_ver: 1U,
data_devices: [],
data_sources: {},
prim_devices: [],
prim_sources: {},
next_offer: 0xff000000U,
cursor_shape: 0U,
alive: true,
}
}
///|
fn ClientConn::send_msg(self : ClientConn, msg : Bytes) -> Unit {
if self.alive {
let n = @sys.sendmsg_fd(self.fd, msg, -1)
if n < 0 {
self.alive = false
}
}
}
///|
fn ClientConn::take_fd(self : ClientConn) -> Int {
if self.pending_fds.length() == 0 {
return -1
}
self.pending_fds.remove(0)
}
///| Whether any live buffer was created from this pool.
fn ClientConn::pool_in_use(self : ClientConn, pool_id : UInt) -> Bool {
for _, b in self.shm_buffers {
if b.pool_id == pool_id {
return true
}
}
false
}
///| Close a destroyed pool once its last buffer is gone.
fn ClientConn::release_dead_pool(self : ClientConn, pool_id : UInt) -> Unit {
match self.shm_pools.get(pool_id) {
Some(p) =>
if p.destroyed && !self.pool_in_use(pool_id) {
@sys.close_fd(p.fd)
self.shm_pools.remove(pool_id)
}
None => ()
}
}
///|
fn ClientConn::close_all(self : ClientConn) -> Unit {
for _, pool in self.shm_pools {
@sys.close_fd(pool.fd)
}
for fd in self.pending_fds {
@sys.close_fd(fd)
}
@sys.close_fd(self.fd)
}
///|
pub(all) struct WaylandServer {
socket_path : String
listen_fd : Int
mut output_w : Int
mut output_h : Int
clients : Array[ClientConn]
win_to_client : Map[UInt64, UInt]
mut next_client_id : UInt
mut next_win_id : UInt64
mut next_serial : UInt
mut keymap : Bytes
/// Clipboard owner: (client id, wl_data_source id).
mut selection : (UInt, UInt)?
/// Primary selection owner (the last text selected in any app): (client id, source id).
mut primary : (UInt, UInt)?
/// Client that currently has keyboard focus (receives selection events).
mut kbd_client : UInt?
/// Area a popup must stay inside, as (x, y, w, h) relative to its parent's content origin
/// (the parent window id is passed). The host sets this; the default never constrains.
mut popup_bounds : (UInt64) -> (Int, Int, Int, Int)
/// What the compositor itself offers on the clipboard (a screenshot) while `selection` is
/// `Some((0, 0))`: its mime types and bytes.
mut own_mimes : Array[String]
mut own_data : Bytes
/// Clipboard pastes of `own_data` still being written: (pipe fd, bytes written, give up at ms).
transfers : Array[(Int, Int, UInt64)]
/// CHANDRA_DEBUG_CONFIGURE=1: log each configure, ack and committed buffer size to stderr.
mut debug_configure : Bool
}
///|
pub fn WaylandServer::listen(
socket_path : String,
output_w : Int,
output_h : Int,
) -> WaylandServer? {
let fd = @sys.unix_listen(socket_path)
if fd < 0 {
return None
}
Some({
socket_path,
listen_fd: fd,
output_w,
output_h,
clients: [],
win_to_client: {},
next_client_id: 1U,
next_win_id: 1UL,
next_serial: 100U,
keymap: Bytes::from_array([]),
selection: None,
kbd_client: None,
primary: None,
popup_bounds: fn(_) { (-1000000, -1000000, 2000000, 2000000) },
own_mimes: [],
own_data: Bytes::from_array([]),
transfers: [],
debug_configure: @sys.getenv("CHANDRA_DEBUG_CONFIGURE").length() > 0,
})
}
///|
pub fn WaylandServer::close(self : WaylandServer) -> Unit {
for t in self.transfers {
@sys.close_fd(t.0)
}
self.transfers.clear()
for c in self.clients {
c.close_all()
}
self.clients.clear()
@sys.close_fd(self.listen_fd)
@sys.unlink(self.socket_path)
}
///|
fn WaylandServer::next_serial(self : WaylandServer) -> UInt {
let s = self.next_serial
self.next_serial = self.next_serial + 1U
s
}
///|
fn emit_registry_globals(c : ClientConn, reg_id : UInt) -> Unit {
let globals : Array[(UInt, String, UInt)] = [
(1U, "wl_compositor", 5U),
(2U, "wl_shm", 1U),
(3U, "xdg_wm_base", 5U),
(4U, "zxdg_decoration_manager_v1", 1U),
(5U, "wl_seat", 7U),
(6U, "wl_output", 4U),
(7U, "wl_subcompositor", 1U),
(8U, "wl_data_device_manager", 3U),
(9U, "wp_cursor_shape_manager_v1", 1U),
(10U, "org_kde_kwin_server_decoration_manager", 1U),
(11U, "zwp_primary_selection_device_manager_v1", 1U),
]
for g in globals {
let w = WireWriter::new()
w.u32(g.0)
w.str(g.1)
w.u32(g.2)
c.send_msg(w.finish_msg(reg_id, 0U))
}
}
///|
fn emit_shm_formats(c : ClientConn, shm_id : UInt) -> Unit {
// ARGB8888 = 0, XRGB8888 = 1
for fmt in [0U, 1U] {
let w = WireWriter::new()
w.u32(fmt)
c.send_msg(w.finish_msg(shm_id, 0U))
}
}
///|
fn emit_seat_info(c : ClientConn, seat_id : UInt, ver : UInt) -> Unit {
let w1 = WireWriter::new()
w1.u32(3U) // pointer (1) | keyboard (2)
c.send_msg(w1.finish_msg(seat_id, 0U))
// wl_seat.name is since version 2: a v1 client (vkcube) aborts on an event it has no handler for.
if ver >= 2U {
let w2 = WireWriter::new()
w2.str("seat0")
c.send_msg(w2.finish_msg(seat_id, 1U))
}
}
///| The output changed size (the nested window was resized): tell every bound wl_output.
pub fn WaylandServer::set_output_size(self : WaylandServer, w : Int, h : Int) -> Unit {
if w == self.output_w && h == self.output_h {
return
}
self.output_w = w
self.output_h = h
for c in self.clients {
if !c.alive {
continue
}
for id, kind in c.objects {
if kind is ObjOutput(over) {
let m = WireWriter::new()
m.u32(3U) // current | preferred
m.i32(w)
m.i32(h)
m.i32(60000)
c.send_msg(m.finish_msg(id, 1U))
if over >= 2U {
c.send_msg(WireWriter::new().finish_msg(id, 2U)) // done
}
}
}
}
}
///|
fn emit_output_info(
c : ClientConn,
out_id : UInt,
w_px : Int,
h_px : Int,
ver : UInt,
) -> Unit {
let g = WireWriter::new()
g.i32(0)
g.i32(0)
g.i32(340)
g.i32(190)
g.i32(0)
g.str("Chandra")
g.str("Virtual-1")
g.i32(0)
c.send_msg(g.finish_msg(out_id, 0U))
let m = WireWriter::new()
m.u32(3U) // current | preferred
m.i32(w_px)
m.i32(h_px)
m.i32(60000)
c.send_msg(m.finish_msg(out_id, 1U))
// scale and done are since version 2.
if ver >= 2U {
let s = WireWriter::new()
s.i32(1)
c.send_msg(s.finish_msg(out_id, 3U))
let d = WireWriter::new()
c.send_msg(d.finish_msg(out_id, 2U))
}
}
///|
fn send_delete_id(c : ClientConn, id : UInt) -> Unit {
let w = WireWriter::new()
w.u32(id)
c.send_msg(w.finish_msg(1U, 1U))
}
///|
fn send_decoration_mode(c : ClientConn, deco_id : UInt, mode : UInt) -> Unit {
let w = WireWriter::new()
w.u32(mode) // 1 = ClientSide, 2 = ServerSide
c.send_msg(w.finish_msg(deco_id, 0U))
}
///|
fn snapshot_shm_buffer(
c : ClientConn,
buf_id : UInt,
) -> @scene.ShmBufferSnapshot? {
let buf = match c.shm_buffers.get(buf_id) {
Some(b) => b
None => return None
}
let pool = match c.shm_pools.get(buf.pool_id) {
Some(p) => p
None => return None
}
let needed = buf.offset + buf.stride * buf.height
if needed <= 0 || buf.width <= 0 || buf.height <= 0 {
return None
}
let byte_len = buf.stride * buf.height
// With the host rasteriser the host copies the buffer itself and keeps the pixels: all this
// program holds is an id.
if @sys.host_raster() {
let id = @sys.surface_capture(pool.fd, buf.offset, buf.stride, buf.width, buf.height, buf.format)
if id == 0U {
return None
}
return Some({
width: buf.width,
height: buf.height,
stride: buf.stride,
format: buf.format,
bytes: Bytes::make(0, b'\x00'),
host_id: id,
})
}
// Only this buffer's bytes: a pool can be hundreds of MB while a buffer is a few.
let slice = @sys.mmap_read_at(pool.fd, buf.offset, byte_len)
if slice.length() < byte_len {
return None
}
Some({
width: buf.width,
height: buf.height,
stride: buf.stride,
format: buf.format,
bytes: slice,
host_id: 0U,
})
}
///|
fn WaylandServer::dispatch_msg(
self : WaylandServer,
c : ClientConn,
obj_id : UInt,
opcode : UInt,
reader : WireReader,
events : Array[CompositorEvent],
) -> Unit {
let kind = match c.objects.get(obj_id) {
Some(k) => k
None => return
}
match kind {
ObjDisplay =>
if opcode == 0U {
// sync(callback: new_id)
let cb_id = reader.u32()
let w = WireWriter::new()
w.u32(self.next_serial())
c.send_msg(w.finish_msg(cb_id, 0U))
send_delete_id(c, cb_id)
} else if opcode == 1U {
// get_registry(registry: new_id)
let reg_id = reader.u32()
c.objects.set(reg_id, ObjRegistry)
emit_registry_globals(c, reg_id)
}
ObjRegistry =>
if opcode == 0U {
// bind(name: u32, interface: string, version: u32, id: new_id)
let _name = reader.u32()
let iface = reader.str()
let ver = reader.u32()
let new_id = reader.u32()
if iface == "wl_compositor" {
c.objects.set(new_id, ObjCompositor)
} else if iface == "wl_shm" {
c.objects.set(new_id, ObjShm)
emit_shm_formats(c, new_id)
} else if iface == "xdg_wm_base" {
c.objects.set(new_id, ObjXdgWmBase)
} else if iface == "zxdg_decoration_manager_v1" {
c.objects.set(new_id, ObjDecoManager)
} else if iface == "org_kde_kwin_server_decoration_manager" {
c.objects.set(new_id, ObjKdeDecoManager)
// default_mode(Server): GTK 3 then leaves out its own title bar and shadow on every
// window that has no custom header bar, and asks for Chandra's frame.
send_decoration_mode(c, new_id, 2U)
} else if iface == "wl_seat" {
c.objects.set(new_id, ObjSeat)
c.seat_ver = ver
emit_seat_info(c, new_id, ver)
} else if iface == "wp_cursor_shape_manager_v1" {
c.objects.set(new_id, ObjCursorShapeMgr)
} else if iface == "wl_data_device_manager" {
c.objects.set(new_id, ObjDataManager)
} else if iface == "zwp_primary_selection_device_manager_v1" {
c.objects.set(new_id, ObjPrimaryManager)
} else if iface == "wl_subcompositor" {
c.objects.set(new_id, ObjSubcompositor)
} else if iface == "wl_output" {
c.objects.set(new_id, ObjOutput(ver))
emit_output_info(c, new_id, self.output_w, self.output_h, ver)
}
}
ObjCompositor =>
if opcode == 0U {
// create_surface(id: new_id)
let sid = reader.u32()
c.objects.set(sid, ObjSurface(sid))
c.surfaces.set(sid, {
attached_buffer: None,
pending_frame_cb: None,
xdg_surface_id: None,
window_id: None,
buffer_scale: 1,
kde_ssd: false,
attached_null: false,
})
} else if opcode == 1U {
let rid = reader.u32()
c.objects.set(rid, ObjRegion)
}
ObjSurface(sid) =>
match c.surfaces.get(sid) {
None => ()
Some(surf) =>
if opcode == 0U {
// destroy
if c.subsurfaces.contains(sid) {
match sub_root(c, sid) {
Some((wid, _, _)) => events.push(SubsurfaceUnmapped(wid, sid))
None => ()
}
}
c.surfaces.remove(sid)
c.objects.remove(sid)
send_delete_id(c, sid)
} else if opcode == 1U {
// attach(buffer, x, y)
let buf_id = reader.u32()
let _x = reader.i32()
let _y = reader.i32()
surf.attached_null = buf_id == 0U
surf.attached_buffer = if buf_id == 0U {
None
} else {
Some(buf_id)
}
} else if opcode == 3U {
// frame(callback)
let cb_id = reader.u32()
surf.pending_frame_cb = Some(cb_id)
} else if opcode == 8U {
// set_buffer_scale(scale)
let sc = reader.i32()
surf.buffer_scale = if sc > 0 { sc } else { 1 }
} else if opcode == 6U {
// commit
if surf.window_id is None && c.subsurfaces.contains(sid) {
match sub_root(c, sid) {
Some((wid, ax, ay)) =>
if surf.attached_null {
events.push(SubsurfaceUnmapped(wid, sid))
} else {
match surf.attached_buffer {
Some(bid) =>
match snapshot_shm_buffer(c, bid) {
Some(snap) => events.push(SubsurfaceCommitted(wid, sid, ax, ay, Some(snap)))
None => ()
}
None => ()
}
}
None => ()
}
}
surf.attached_null = false
match surf.window_id {
Some(wid) =>
match c.toplevels.get(wid) {
Some(tl) =>
if tl.popup_parent is Some(_) {
let snap = match surf.attached_buffer {
Some(bid) => snapshot_shm_buffer(c, bid)
None => None
}
events.push(PopupCommitted(wid, snap))
} else if !tl.opened_sent {
tl.opened_sent = true
events.push(
ToplevelOpened(
wid,
tl.app_id,
tl.title,
tl.min_w,
tl.min_h,
),
)
} else {
let snap = match surf.attached_buffer {
Some(bid) => snapshot_shm_buffer(c, bid)
None => None
}
if snap is Some(_) {
// Who draws the frame: a client that never made an xdg-decoration
// object (GTK) decorates itself.
let (gx, gy) = match c.xdg_surfaces.get(tl.xdg_surface_id) {
Some(xs) => (xs.geo_x, xs.geo_y)
None => (0, 0)
}
let kde_ssd = match c.surfaces.get(tl.wl_surface_id) {
Some(s) => s.kde_ssd
None => false
}
let client_side = tl.wants_csd ||
(tl.deco_obj_id is None && !kde_ssd)
let frame = (client_side, gx, gy)
if tl.frame_sent != Some(frame) {
tl.frame_sent = Some(frame)
events.push(ToplevelFrame(wid, frame.0, gx, gy))
}
}
events.push(ToplevelCommitted(wid, snap))
match snap {
Some(sn) => self.check_committed_size(c, tl, surf, sn, events)
None => ()
}
}
None => ()
}
None => ()
}
match surf.attached_buffer {
Some(bid) => {
// Notify client that compositor has copied/consumed the buffer
let rel = WireWriter::new()
c.send_msg(rel.finish_msg(bid, 0U))
}
None => ()
}
surf.attached_buffer = None
match surf.pending_frame_cb {
Some(cb_id) => {
surf.pending_frame_cb = None
let fw = WireWriter::new()
fw.u32(self.next_serial())
c.send_msg(fw.finish_msg(cb_id, 0U))
send_delete_id(c, cb_id)
}
None => ()
}
}
}
ObjSubcompositor =>
if opcode == 0U {
// destroy
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U {
// get_subsurface(id: new_id, surface, parent)
let sub_id = reader.u32()
let child = reader.u32()
let parent = reader.u32()
c.objects.set(sub_id, ObjSubsurface(child))
c.subsurfaces.set(child, { parent, x: 0, y: 0 })
}
ObjDataManager =>
if opcode == 0U {
// create_data_source(id)
let id = reader.u32()
c.objects.set(id, ObjDataSource)
c.data_sources.set(id, [])
} else if opcode == 1U {
// get_data_device(id, seat)
let id = reader.u32()
c.objects.set(id, ObjDataDevice)
c.data_devices.push(id)
// A client created after it got focus still needs the current clipboard.
if self.kbd_client == Some(c.client_id) {
self.send_selection(c)
}
}
ObjDataSource =>
if opcode == 0U {
// offer(mime_type)
let mime = reader.str()
match c.data_sources.get(obj_id) {
Some(m) => m.push(mime)
None => ()
}
} else if opcode == 1U {
// destroy
c.data_sources.remove(obj_id)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
if self.selection == Some((c.client_id, obj_id)) {
self.selection = None
self.notify_selection()
}
}
ObjDataDevice =>
if opcode == 1U {
// set_selection(source or null, serial)
let src = reader.u32()
let old = self.selection
if src == 0U {
self.selection = None
} else {
self.selection = Some((c.client_id, src))
}
match old {
Some((ocid, osid)) =>
if old != self.selection {
match self.find_client(ocid) {
Some(oc) => oc.send_msg(WireWriter::new().finish_msg(osid, 2U)) // cancelled
None => ()
}
}
None => ()
}
self.notify_selection()
} else if opcode == 2U {
// release
c.objects.remove(obj_id)
c.data_devices.retain(d => d != obj_id)
send_delete_id(c, obj_id)
}
ObjDataOffer(ocid, osid) =>
if opcode == 1U {
// receive(mime_type, fd): ask the owner to write the data into the fd
let mime = reader.str()
let fd = c.take_fd()
if fd >= 0 && ocid == 0U {
// The compositor's own clipboard: write it out a piece at a time (pump_transfers),
// so a client that is slow to read can't stall the desktop.
if self.selection == Some((0U, 0U)) && self.own_mimes.contains(mime) {
self.transfers.push((fd, 0, @sys.monotonic_ms() + 10000UL))
self.pump_transfers()
} else {
@sys.close_fd(fd)
}
} else if fd >= 0 {
match self.find_client(ocid) {
Some(oc) =>
if oc.data_sources.contains(osid) {
let w = WireWriter::new()
w.str(mime)
if oc.alive {
ignore(@sys.sendmsg_fd(oc.fd, w.finish_msg(osid, 1U), fd))
}
}
None => ()
}
@sys.close_fd(fd)
}
} else if opcode == 2U {
// destroy
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
ObjPrimaryManager =>
if opcode == 0U {
// create_source(id)
let id = reader.u32()
c.objects.set(id, ObjPrimarySource)
c.prim_sources.set(id, [])
} else if opcode == 1U {
// get_device(id, seat)
let id = reader.u32()
c.objects.set(id, ObjPrimaryDevice)
c.prim_devices.push(id)
if self.kbd_client == Some(c.client_id) {
self.send_primary(c)
}
} else if opcode == 2U {
// destroy
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
ObjPrimarySource =>
if opcode == 0U {
// offer(mime_type)
let mime = reader.str()
match c.prim_sources.get(obj_id) {
Some(m) => m.push(mime)
None => ()
}
} else if opcode == 1U {
// destroy
c.prim_sources.remove(obj_id)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
if self.primary == Some((c.client_id, obj_id)) {
self.primary = None
self.notify_primary()
}
}
ObjPrimaryDevice =>
if opcode == 0U {
// set_selection(source or null, serial)
let src = reader.u32()
let old = self.primary
self.primary = if src == 0U { None } else { Some((c.client_id, src)) }
match old {
Some((ocid, osid)) =>
if old != self.primary {
match self.find_client(ocid) {
Some(oc) => oc.send_msg(WireWriter::new().finish_msg(osid, 1U)) // cancelled
None => ()
}
}
None => ()
}
self.notify_primary()
} else if opcode == 1U {
// destroy
c.objects.remove(obj_id)
c.prim_devices.retain(d => d != obj_id)
send_delete_id(c, obj_id)
}
ObjPrimaryOffer(ocid, osid) =>
if opcode == 0U {
// receive(mime_type, fd): the owner writes the text into the fd (source.send)
let mime = reader.str()
let fd = c.take_fd()
if fd >= 0 {
match self.find_client(ocid) {
Some(oc) =>
if oc.prim_sources.contains(osid) && oc.alive {
let w = WireWriter::new()
w.str(mime)
ignore(@sys.sendmsg_fd(oc.fd, w.finish_msg(osid, 0U), fd))
}
None => ()
}
@sys.close_fd(fd)
}
} else if opcode == 1U {
// destroy
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
ObjCursorShapeMgr =>
if opcode == 1U {
// get_pointer(cursor_shape_device: new_id, pointer)
let id = reader.u32()
c.objects.set(id, ObjCursorShapeDev)
} else if opcode == 2U {
// get_tablet_tool_v2: accepted, unused
let id = reader.u32()
c.objects.set(id, ObjCursorShapeDev)
}
ObjCursorShapeDev =>
if opcode == 1U {
// set_shape(serial, shape)
let _serial = reader.u32()
c.cursor_shape = reader.u32()
}
ObjSubsurface(child) =>
if opcode == 0U {
match sub_root(c, child) {
Some((wid, _, _)) => events.push(SubsurfaceUnmapped(wid, child))
None => ()
}
c.subsurfaces.remove(child)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U {
// set_position(x, y): taken at once (the parent's next commit would apply it).
let x = reader.i32()
let y = reader.i32()
match c.subsurfaces.get(child) {
Some(st) => {
st.x = x
st.y = y
match sub_root(c, child) {
Some((wid, ax, ay)) => events.push(SubsurfaceCommitted(wid, child, ax, ay, None))
None => ()
}
}
None => ()
}
}
ObjShm =>
if opcode == 0U {
// create_pool(id: new_id, fd: out-of-band, size: i32)
let pool_id = reader.u32()
let size = reader.i32()
let fd = c.take_fd()
c.objects.set(pool_id, ObjShmPool(pool_id))
if fd >= 0 {
c.shm_pools.set(pool_id, { fd, size, destroyed: false })
}
}
ObjShmPool(pool_id) =>
if opcode == 0U {
// create_buffer(id, offset, width, height, stride, format)
let buf_id = reader.u32()
let offset = reader.i32()
let width = reader.i32()
let height = reader.i32()
let stride = reader.i32()
let format = reader.u32().reinterpret_as_int()
c.objects.set(buf_id, ObjBuffer(buf_id))
c.shm_buffers.set(buf_id, {
pool_id,
offset,
width,
height,
stride,
format,
})
} else if opcode == 1U {
// destroy
match c.shm_pools.get(pool_id) {
Some(p) =>
if c.pool_in_use(pool_id) {
// Client ids are reused after delete_id, so move the pool (and its buffers)
// to a private key a new pool can't collide with.
p.destroyed = true
let mut key = 0x80000000U
while c.shm_pools.contains(key) {
key = key + 1U
}
c.shm_pools.remove(pool_id)
c.shm_pools.set(key, p)
for _, b in c.shm_buffers {
if b.pool_id == pool_id {
b.pool_id = key
}
}
} else {
@sys.close_fd(p.fd)
c.shm_pools.remove(pool_id)
}
None => ()
}
c.objects.remove(pool_id)
send_delete_id(c, pool_id)
} else if opcode == 2U {
// resize(size)
let new_sz = reader.i32()
match c.shm_pools.get(pool_id) {
Some(p) => p.size = new_sz
None => ()
}
}
ObjBuffer(buf_id) =>
if opcode == 0U {
let pool_id = match c.shm_buffers.get(buf_id) {
Some(b) => Some(b.pool_id)
None => None
}
c.shm_buffers.remove(buf_id)
c.objects.remove(buf_id)
send_delete_id(c, buf_id)
match pool_id {
Some(pid) => c.release_dead_pool(pid)
None => ()
}
}
ObjXdgWmBase =>
if opcode == 0U {
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U {
let pos_id = reader.u32()
c.objects.set(pos_id, ObjXdgPositioner(pos_id))
c.positioners.set(pos_id, { w: 0, h: 0, ax: 0, ay: 0, aw: 0, ah: 0, anchor: 0U, gravity: 0U, ox: 0, oy: 0, adjust: 0U })
} else if opcode == 2U {
// get_xdg_surface(id, surface)
let xsid = reader.u32()
let wl_sid = reader.u32()
c.objects.set(xsid, ObjXdgSurface(xsid))
c.xdg_surfaces.set(xsid, { wl_surface_id: wl_sid, window_id: None, geo_x: 0, geo_y: 0, geo_w: 0, geo_h: 0 })
match c.surfaces.get(wl_sid) {
Some(s) => s.xdg_surface_id = Some(xsid)
None => ()
}
}
ObjXdgSurface(xsid) =>
if opcode == 0U {
c.xdg_surfaces.remove(xsid)
c.objects.remove(xsid)
send_delete_id(c, xsid)
} else if opcode == 4U {
// ack_configure(serial)
let serial = reader.u32()
match c.xdg_surfaces.get(xsid) {
Some({ window_id: Some(wid), .. }) =>
match c.toplevels.get(wid) {
Some(tl) => {
tl.acked_serial = serial
if self.debug_configure {
@sys.log("chandra-wl: window \{wid} ack_configure \{serial}")
}
}
None => ()
}
_ => ()
}
} else if opcode == 3U {
// set_window_geometry(x, y, w, h)
let gx = reader.i32()
let gy = reader.i32()
let gw = reader.i32()
let gh = reader.i32()
match c.xdg_surfaces.get(xsid) {
Some(xs) => {
xs.geo_x = gx
xs.geo_y = gy
xs.geo_w = gw
xs.geo_h = gh
match xs.window_id {
Some(wid) =>
match c.toplevels.get(wid) {
Some(tl) if tl.popup_parent is Some(_) => events.push(PopupGeometry(wid, gx, gy))
_ => ()
}
None => ()
}
}
None => ()
}
} else if opcode == 1U {
// get_toplevel(id)
let tl_id = reader.u32()
let wid = self.next_win_id
self.next_win_id = self.next_win_id + 1UL
let wl_sid = match c.xdg_surfaces.get(xsid) {
Some(xs) => {
xs.window_id = Some(wid)
xs.wl_surface_id
}
None => 0U
}
match c.surfaces.get(wl_sid) {
Some(s) => s.window_id = Some(wid)
None => ()
}
c.objects.set(tl_id, ObjXdgToplevel(wid))
c.toplevels.set(wid, {
win_id: wid,
wl_surface_id: wl_sid,
xdg_surface_id: xsid,
wl_toplevel_id: tl_id,
deco_obj_id: None,
title: "",
app_id: "",
min_w: 160,
min_h: 120,
opened_sent: false,
has_parent: false,
popup_parent: None,
cfg_w: 0,
cfg_h: 0,
cfg_states: [],
cfg_serial: 0U,
acked_serial: 0U,
cfg_retry: 0,
frame_sent: None,
wants_csd: false,
})
self.win_to_client.set(wid, c.client_id)
} else if opcode == 2U {
// get_popup(id, parent: xdg_surface?, positioner)
let pid = reader.u32()
let parent_xs = reader.u32()
let pos_id = reader.u32()
let wid = self.next_win_id
self.next_win_id = self.next_win_id + 1UL
let parent_wid = match c.xdg_surfaces.get(parent_xs) {
Some(xs) =>
match xs.window_id {
Some(w) => w
None => 0UL
}
None => 0UL
}
let wl_sid = match c.xdg_surfaces.get(xsid) {
Some(xs) => {
xs.window_id = Some(wid)
xs.wl_surface_id
}
None => 0U
}
match c.surfaces.get(wl_sid) {
Some(s) => s.window_id = Some(wid)
None => ()
}
let (px, py, pw, ph) = match c.positioners.get(pos_id) {
Some(ps) => constrain_popup(ps, (self.popup_bounds)(parent_wid))
None => (0, 0, 1, 1)
}
c.objects.set(pid, ObjXdgPopup(wid))
c.toplevels.set(wid, {
win_id: wid,
wl_surface_id: wl_sid,
xdg_surface_id: xsid,
wl_toplevel_id: pid,
deco_obj_id: None,
title: "",
app_id: "",
min_w: 1,
min_h: 1,
opened_sent: true,
has_parent: true,
popup_parent: Some(parent_wid),
cfg_w: 0,
cfg_h: 0,
cfg_states: [],
cfg_serial: 0U,
acked_serial: 0U,
cfg_retry: 0,
frame_sent: None,
wants_csd: false,
})
self.win_to_client.set(wid, c.client_id)
// xdg_popup.configure(x, y, w, h) then xdg_surface.configure(serial)
let w1 = WireWriter::new()
w1.i32(px)
w1.i32(py)
w1.i32(pw)
w1.i32(ph)
c.send_msg(w1.finish_msg(pid, 0U))
let w2 = WireWriter::new()
w2.u32(self.next_serial())
c.send_msg(w2.finish_msg(xsid, 0U))
events.push(PopupOpened(wid, parent_wid, px, py, pw, ph))
match c.xdg_surfaces.get(xsid) {
Some(xs) if xs.geo_x != 0 || xs.geo_y != 0 => events.push(PopupGeometry(wid, xs.geo_x, xs.geo_y))
_ => ()
}
}
ObjXdgPositioner(pos_id) =>
match c.positioners.get(pos_id) {
None => ()
Some(ps) =>
if opcode == 0U {
c.positioners.remove(pos_id)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U {
ps.w = reader.i32()
ps.h = reader.i32()
} else if opcode == 2U {
ps.ax = reader.i32()
ps.ay = reader.i32()
ps.aw = reader.i32()
ps.ah = reader.i32()
} else if opcode == 3U {
ps.anchor = reader.u32()
} else if opcode == 4U {
ps.gravity = reader.u32()
} else if opcode == 5U {
ps.adjust = reader.u32()
} else if opcode == 6U {
ps.ox = reader.i32()
ps.oy = reader.i32()
}
}
ObjXdgPopup(wid) =>
if opcode == 0U {
// destroy
c.toplevels.remove(wid)
self.win_to_client.remove(wid)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
events.push(PopupClosed(wid))
} else if opcode == 1U {
// grab(seat, serial)
events.push(PopupGrabbed(wid))
} else if opcode == 2U {
// reposition(positioner, token): place again, then repositioned + configure.
let pos_id = reader.u32()
let token = reader.u32()
match (c.toplevels.get(wid), c.positioners.get(pos_id)) {
(Some(tl), Some(ps)) => {
let parent = tl.popup_parent.unwrap_or(0UL)
let (px, py, pw, ph) = constrain_popup(ps, (self.popup_bounds)(parent))
let wt = WireWriter::new()
wt.u32(token)
c.send_msg(wt.finish_msg(tl.wl_toplevel_id, 2U))
let w1 = WireWriter::new()
w1.i32(px)
w1.i32(py)
w1.i32(pw)
w1.i32(ph)
c.send_msg(w1.finish_msg(tl.wl_toplevel_id, 0U))
let w2 = WireWriter::new()
w2.u32(self.next_serial())
c.send_msg(w2.finish_msg(tl.xdg_surface_id, 0U))
events.push(PopupMoved(wid, px, py, pw, ph))
}
_ => ()
}
}
ObjXdgToplevel(wid) =>
match c.toplevels.get(wid) {
None => ()
Some(tl) =>
if opcode == 0U {
// destroy
c.toplevels.remove(wid)
self.win_to_client.remove(wid)
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
events.push(ToplevelClosed(wid))
} else if opcode == 1U {
// set_parent(parent: object or null)
tl.has_parent = reader.u32() != 0U
} else if opcode == 2U {
// set_title
tl.title = reader.str()
if tl.opened_sent {
events.push(ToplevelUpdated(wid, tl.app_id, tl.title))
}
} else if opcode == 3U {
// set_app_id
tl.app_id = reader.str()
if tl.opened_sent {
events.push(ToplevelUpdated(wid, tl.app_id, tl.title))
}
} else if opcode == 5U {
// move
events.push(ToplevelMoveRequest(wid))
} else if opcode == 6U {
// resize(seat, serial, edges)
let _seat = reader.u32()
let _serial = reader.u32()
events.push(ToplevelResizeRequest(wid, reader.u32()))
} else if opcode == 8U {
// set_min_size
let mw = reader.i32()
let mh = reader.i32()
tl.min_w = if mw > 0 { mw } else { 160 }
tl.min_h = if mh > 0 { mh } else { 120 }
} else if opcode == 9U {
events.push(ToplevelMaximizeRequest(wid, true))
} else if opcode == 10U {
events.push(ToplevelMaximizeRequest(wid, false))
} else if opcode == 11U {
events.push(ToplevelFullscreenRequest(wid, true))
} else if opcode == 12U {
events.push(ToplevelFullscreenRequest(wid, false))
} else if opcode == 13U {
events.push(ToplevelMinimizeRequest(wid, true))
}
}
ObjDecoManager =>
if opcode == 0U {
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U {
// get_toplevel_decoration(id, toplevel)
let deco_id = reader.u32()
let tl_obj = reader.u32()
let wid = match c.objects.get(tl_obj) {
Some(ObjXdgToplevel(w)) => w
_ => 0UL
}
c.objects.set(deco_id, ObjToplevelDeco(wid))
match c.toplevels.get(wid) {
Some(tl) => tl.deco_obj_id = Some(deco_id)
None => ()
}
// Negotiate Server-Side Decoration (2)
send_decoration_mode(c, deco_id, 2U)
}
ObjKdeDecoManager =>
if opcode == 0U {
// create(id, surface): KWin's protocol starts every decoration in the default mode.
let deco_id = reader.u32()
let sid = reader.u32()
c.objects.set(deco_id, ObjKdeDeco(sid))
match c.surfaces.get(sid) {
Some(s) => s.kde_ssd = true
None => ()
}
send_decoration_mode(c, deco_id, 2U)
}
ObjKdeDeco(sid) =>
if opcode == 0U {
// release: the client draws its own frame again.
match c.surfaces.get(sid) {
Some(s) => s.kde_ssd = false
None => ()
}
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U {
// request_mode(mode): 2 Server gets Chandra's frame; 1 Client (a GTK 3 app with its own
// header bar) and 0 None draw their own.
let mode = reader.u32()
let ssd = mode == 2U
match c.surfaces.get(sid) {
Some(s) => s.kde_ssd = ssd
None => ()
}
send_decoration_mode(c, obj_id, mode)
}
ObjToplevelDeco(wid) =>
if opcode == 0U {
// Without its decoration object the client goes back to drawing its own frame.
match c.toplevels.get(wid) {
Some(tl) if tl.deco_obj_id == Some(obj_id) => {
tl.deco_obj_id = None
tl.wants_csd = false
}
_ => ()
}
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
} else if opcode == 1U || opcode == 2U {
// set_mode(mode) / unset_mode. A client asking to draw its own frame gets it: Chrome
// (custom frame) keeps drawing its title bar and shadow even when told server-side,
// which showed both frames. Anything else, and unset_mode, gets Chandra's frame.
let csd = opcode == 1U && reader.u32() == 1U
match c.toplevels.get(wid) {
Some(tl) => tl.wants_csd = csd
None => ()
}
send_decoration_mode(c, obj_id, if csd { 1U } else { 2U })
}
ObjSeat =>
if opcode == 0U {
let pid = reader.u32()
c.objects.set(pid, ObjPointer)
c.pointer_ids.push(pid)
} else if opcode == 1U {
let kid = reader.u32()
c.objects.set(kid, ObjKeyboard)
c.keyboard_ids.push(kid)
self.send_keymap(c, kid)
} else if opcode == 2U {
let tid = reader.u32()
c.objects.set(tid, ObjTouch)
}
_ => ()
}
}
///| The listening socket and every connected client: what an idle compositor waits on.
pub fn WaylandServer::wait_fds(self : WaylandServer) -> Array[Int] {
let out = [self.listen_fd]
for c in self.clients {
if c.alive {
out.push(c.fd)
}
}
out
}
///| Accept new clients, read incoming Wayland messages + SCM_RIGHTS fds, and emit CompositorEvents.
pub fn WaylandServer::poll_events(
self : WaylandServer,
) -> Array[CompositorEvent] {
let events : Array[CompositorEvent] = []
self.pump_transfers()
// Accept any pending connections
while true {
let cfd = @sys.unix_accept(self.listen_fd)
if cfd < 0 {
break
}
let cid = self.next_client_id
self.next_client_id = self.next_client_id + 1U
self.clients.push(ClientConn::new(cid, cfd))
}
// Read from each client
let scratch : FixedArray[Byte] = FixedArray::make(8192, b'\x00')
let disconnected : Array[Int] = []
for idx = 0; idx < self.clients.length(); idx = idx + 1 {
let c = self.clients[idx]
while c.alive {
let (n, rfds) = @sys.recvmsg_fds(c.fd, scratch, 8192)
for rfd in rfds {
c.pending_fds.push(rfd)
}
if n > 0 {
for i = 0; i < n; i = i + 1 {
c.rx_buf.push(scratch[i])
}
} else if n == 0 {
c.alive = false
break
} else if n == -1 {
// EAGAIN
break
} else {
c.alive = false
break
}
}
// Parse complete Wayland messages from rx_buf
let mut off = 0
while off + 8 <= c.rx_buf.length() {
let obj_id = read_u32_le(c.rx_buf, off)
let word2 = read_u32_le(c.rx_buf, off + 4)
let size = ((word2 >> 16) & 0xFFFFU).reinterpret_as_int()
let opcode = word2 & 0xFFFFU
if size < 8 || off + size > c.rx_buf.length() {
break
}
let reader = WireReader::from_array(c.rx_buf, off + 8)
self.dispatch_msg(c, obj_id, opcode, reader, events)
off = off + size
}
if off > 0 {
let rem : Array[Byte] = []
for i = off; i < c.rx_buf.length(); i = i + 1 {
rem.push(c.rx_buf[i])
}
c.rx_buf.clear()
for b in rem {
c.rx_buf.push(b)
}
}
if !c.alive {
disconnected.push(idx)
}
}
// Clean up disconnected clients in reverse order
let mut j = disconnected.length() - 1
while j >= 0 {
let idx = disconnected[j]
let c = self.clients.remove(idx)
for wid, _ in c.toplevels {
self.win_to_client.remove(wid)
events.push(ToplevelClosed(wid))
}
c.close_all()
if self.kbd_client == Some(c.client_id) {
self.kbd_client = None
}
match self.selection {
Some((ocid, _)) =>
if ocid == c.client_id {
self.selection = None
self.notify_selection()
}
None => ()
}
match self.primary {
Some((ocid, _)) =>
if ocid == c.client_id {
self.primary = None
self.notify_primary()
}
None => ()
}
j = j - 1
}
events
}
///|
fn WaylandServer::find_client(self : WaylandServer, cid : UInt) -> ClientConn? {
for c in self.clients {
if c.client_id == cid {
return Some(c)
}
}
None
}
///| Whether a window is a dialog or tool window of another one (xdg_toplevel.set_parent).
pub fn WaylandServer::toplevel_has_parent(self : WaylandServer, win_id : UInt64) -> Bool {
match self.win_to_client.get(win_id) {
Some(cid) =>
match self.find_client(cid) {
Some(c) =>
match c.toplevels.get(win_id) {
Some(tl) => tl.has_parent
None => false
}
None => false
}
None => false
}
}
///| Send xdg_toplevel.configure + xdg_surface.configure to a client window.
pub fn WaylandServer::send_toplevel_configure(
self : WaylandServer,
win_id : UInt64,
width : Int,
height : Int,
activated : Bool,
maximized : Bool,
fullscreen : Bool,
tiled~ : Bool = false,
) -> Unit {
let cid = match self.win_to_client.get(win_id) {
Some(id) => id
None => return
}
let c = match self.find_client(cid) {
Some(cc) => cc
None => return
}
let tl = match c.toplevels.get(win_id) {
Some(t) => t
None => return
}
let states : Array[Byte] = []
fn push_state(s : UInt) -> Unit {
states.push((s & 0xFFU).to_byte())
states.push(((s >> 8) & 0xFFU).to_byte())
states.push(((s >> 16) & 0xFFU).to_byte())
states.push(((s >> 24) & 0xFFU).to_byte())
}
if maximized {
push_state(1U)
}
if fullscreen {
push_state(2U)
}
if activated {
push_state(4U)
}
if tiled && !maximized && !fullscreen {
// tiled_left/right/top/bottom (xdg_toplevel v2+): client-decorated windows drop their
// shadow and rounded corners, so neighbouring tiles meet cleanly.
for st in [5U, 6U, 7U, 8U] {
push_state(st)
}
}
tl.cfg_w = width
tl.cfg_h = height
tl.cfg_states = states
tl.cfg_retry = 0
self.write_configure(c, tl)
}
///| Write the toplevel's current configure (cfg_w, cfg_h, cfg_states) with a new serial.
fn WaylandServer::write_configure(
self : WaylandServer,
c : ClientConn,
tl : ToplevelState,
) -> Unit {
let w1 = WireWriter::new()
w1.i32(tl.cfg_w)
w1.i32(tl.cfg_h)
w1.array(tl.cfg_states)
c.send_msg(w1.finish_msg(tl.wl_toplevel_id, 0U))
let serial = self.next_serial()
tl.cfg_serial = serial
let w2 = WireWriter::new()
w2.u32(serial)
c.send_msg(w2.finish_msg(tl.xdg_surface_id, 0U))
if self.debug_configure {
let names : Array[String] = []
for i = 0; i + 3 < tl.cfg_states.length(); i = i + 4 {
names.push(
match tl.cfg_states[i].to_int() {
1 => "maximized"
2 => "fullscreen"
3 => "resizing"
4 => "activated"
n => n.to_string()
},
)
}
@sys.log(
"chandra-wl: window \{tl.win_id} configure \{tl.cfg_w}x\{tl.cfg_h} [\{names.join(",")}] serial \{serial}" +
(if tl.cfg_retry > 0 { " (sent again)" } else { "" }),
)
}
}
///| After a commit with a buffer: compare the window's size with the configure the client acked.
///
/// A client may answer a configure with another size. GPUI apps (Zed, Delta) do on purpose when
/// a configure takes the maximized state away: they go back to the size they had before being
/// maximized, even when the compositor asked for a tiled window's full size. The window then
/// showed the app in a small corner of a large frame. Asked once more with the same configure (the
/// client is no longer in the maximized state then), they take the size. A client that still
/// picks its own size gets a ToplevelSizeChosen, and the host keeps it for a floating window.
fn WaylandServer::check_committed_size(
self : WaylandServer,
c : ClientConn,
tl : ToplevelState,
surf : SurfaceState,
snap : @scene.ShmBufferSnapshot,
events : Array[CompositorEvent],
) -> Unit {
let (w, h) = committed_size(c, surf, snap)
if self.debug_configure {
@sys.log(
"chandra-wl: window \{tl.win_id} commit buffer \{snap.width}x\{snap.height} scale \{surf.buffer_scale} -> window \{w}x\{h} (configured \{tl.cfg_w}x\{tl.cfg_h}, acked \{tl.acked_serial}/\{tl.cfg_serial})",
)
}
// Only once the client has acked the latest configure: before that, an old-sized frame is
// just the client catching up.
if tl.cfg_serial == 0U || tl.acked_serial != tl.cfg_serial || tl.cfg_w <= 0 || tl.cfg_h <= 0 {
return
}
if w == tl.cfg_w && h == tl.cfg_h {
return
}
if tl.cfg_retry == 0 {
tl.cfg_retry = 1
self.write_configure(c, tl)
} else if tl.cfg_retry == 1 {
tl.cfg_retry = 2
events.push(ToplevelSizeChosen(tl.win_id, w, h))
}
}
///| The window size a committed buffer stands for: the window geometry when the client set one
/// (a client drawing its own shadows), else the buffer divided by its scale.
fn committed_size(
c : ClientConn,
surf : SurfaceState,
snap : @scene.ShmBufferSnapshot,
) -> (Int, Int) {
let geo = match surf.xdg_surface_id {
Some(xsid) =>
match c.xdg_surfaces.get(xsid) {
Some(xs) if xs.geo_w > 0 && xs.geo_h > 0 => Some((xs.geo_w, xs.geo_h))
_ => None
}
None => None
}
match geo {
Some(g) => g
None => {
let sc = if surf.buffer_scale > 0 { surf.buffer_scale } else { 1 }
(snap.width / sc, snap.height / sc)
}
}
}
///|
pub fn WaylandServer::send_toplevel_close(
self : WaylandServer,
win_id : UInt64,
) -> Unit {
let cid = match self.win_to_client.get(win_id) {
Some(id) => id
None => return
}
let c = match self.find_client(cid) {
Some(cc) => cc
None => return
}
match c.toplevels.get(win_id) {
Some(tl) => {
let w = WireWriter::new()
c.send_msg(w.finish_msg(tl.wl_toplevel_id, 1U))
}
None => ()
}
}
///| Keymap (xkb text, format xkb_v1) shared with every client keyboard; set by the backend.
pub fn WaylandServer::set_keymap(self : WaylandServer, keymap : Bytes) -> Unit {
self.keymap = keymap
}
///|
fn WaylandServer::send_keymap(self : WaylandServer, c : ClientConn, kid : UInt) -> Unit {
if self.keymap.length() > 0 {
let fd = @sys.memfd_create("chandra-keymap", self.keymap.length())
if fd >= 0 {
ignore(@sys.pwrite_bytes(fd, self.keymap, 0))
let w = WireWriter::new()
w.u32(1U) // xkb_v1
w.u32(self.keymap.length().reinterpret_as_uint())
if c.alive {
ignore(@sys.sendmsg_fd(c.fd, w.finish_msg(kid, 0U), fd))
}
@sys.close_fd(fd)
}
}
if c.seat_ver >= 4U {
let r = WireWriter::new()
r.i32(25) // repeat rate (keys/s)
r.i32(400) // repeat delay (ms)
c.send_msg(r.finish_msg(kid, 5U))
}
}
///|
fn WaylandServer::win_client(
self : WaylandServer,
win_id : UInt64,
) -> (ClientConn, ToplevelState)? {
match self.win_to_client.get(win_id) {
None => None
Some(cid) =>
match self.find_client(cid) {
None => None
Some(c) =>
match c.toplevels.get(win_id) {
None => None
Some(tl) => Some((c, tl))
}
}
}
}
///|
fn fixed(v : Double) -> Int {
(v * 256.0).to_int()
}
///| wl_pointer.enter at surface-local (x, y).
///| `sub`: the window's subsurface (its wl_surface id) under the pointer, 0 for the window itself.
pub fn WaylandServer::pointer_enter(self : WaylandServer, win_id : UInt64, x : Double, y : Double, sub? : UInt = 0U) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, tl)) =>
for pid in c.pointer_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(if sub != 0U && c.surfaces.contains(sub) { sub } else { tl.wl_surface_id })
w.i32(fixed(x))
w.i32(fixed(y))
c.send_msg(w.finish_msg(pid, 0U))
self.pointer_frame(c, pid)
}
}
}
///|
pub fn WaylandServer::pointer_leave(self : WaylandServer, win_id : UInt64, sub? : UInt = 0U) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, tl)) =>
for pid in c.pointer_ids {
if sub != 0U && !c.surfaces.contains(sub) {
continue // the subsurface is gone: nothing to leave
}
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(if sub != 0U { sub } else { tl.wl_surface_id })
c.send_msg(w.finish_msg(pid, 1U))
self.pointer_frame(c, pid)
}
}
}
///|
pub fn WaylandServer::pointer_motion(self : WaylandServer, win_id : UInt64, time_ms : UInt, x : Double, y : Double) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, _)) =>
for pid in c.pointer_ids {
let w = WireWriter::new()
w.u32(time_ms)
w.i32(fixed(x))
w.i32(fixed(y))
c.send_msg(w.finish_msg(pid, 2U))
self.pointer_frame(c, pid)
}
}
}
///| `button` is a Linux evdev code (BTN_LEFT = 0x110); `pressed` true on press.
pub fn WaylandServer::pointer_button(self : WaylandServer, win_id : UInt64, time_ms : UInt, button : UInt, pressed : Bool) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, _)) =>
for pid in c.pointer_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(time_ms)
w.u32(button)
w.u32(if pressed { 1U } else { 0U })
c.send_msg(w.finish_msg(pid, 3U))
self.pointer_frame(c, pid)
}
}
}
///| Scroll: `axis` 0 vertical / 1 horizontal, `value` in surface pixels (positive = down/right),
/// `discrete` wheel notches (0 for continuous sources such as touchpads).
pub fn WaylandServer::pointer_axis(
self : WaylandServer,
win_id : UInt64,
time_ms : UInt,
axis : UInt,
value : Double,
discrete : Int,
) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, _)) =>
for pid in c.pointer_ids {
if c.seat_ver >= 5U && discrete != 0 {
// axis_source(wheel), then the notch count, both before the axis event.
let ws = WireWriter::new()
ws.u32(0U)
c.send_msg(ws.finish_msg(pid, 6U))
let wd = WireWriter::new()
wd.u32(axis)
if c.seat_ver >= 8U {
wd.i32(discrete * 120)
c.send_msg(wd.finish_msg(pid, 9U)) // axis_value120
} else {
wd.i32(discrete)
c.send_msg(wd.finish_msg(pid, 8U)) // axis_discrete
}
}
let w = WireWriter::new()
w.u32(time_ms)
w.u32(axis)
w.i32(fixed(value))
c.send_msg(w.finish_msg(pid, 4U))
self.pointer_frame(c, pid)
}
}
}
///|
fn WaylandServer::pointer_frame(_self : WaylandServer, c : ClientConn, pid : UInt) -> Unit {
if c.seat_ver >= 5U {
c.send_msg(WireWriter::new().finish_msg(pid, 5U))
}
}
///|
pub fn WaylandServer::keyboard_enter(self : WaylandServer, win_id : UInt64) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, tl)) => {
self.kbd_client = Some(c.client_id)
self.send_selection(c)
self.send_primary(c)
for kid in c.keyboard_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(tl.wl_surface_id)
w.array([])
c.send_msg(w.finish_msg(kid, 1U))
}
}
}
}
///|
pub fn WaylandServer::keyboard_leave(self : WaylandServer, win_id : UInt64) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, tl)) =>
for kid in c.keyboard_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(tl.wl_surface_id)
c.send_msg(w.finish_msg(kid, 2U))
}
}
}
///| `key` is a Linux evdev keycode.
pub fn WaylandServer::keyboard_key(self : WaylandServer, win_id : UInt64, time_ms : UInt, key : UInt, pressed : Bool) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, _)) =>
for kid in c.keyboard_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(time_ms)
w.u32(key)
w.u32(if pressed { 1U } else { 0U })
c.send_msg(w.finish_msg(kid, 3U))
}
}
}
///|
pub fn WaylandServer::keyboard_modifiers(self : WaylandServer, win_id : UInt64, depressed : UInt, latched : UInt, locked : UInt, group : UInt) -> Unit {
match self.win_client(win_id) {
None => ()
Some((c, _)) =>
for kid in c.keyboard_ids {
let w = WireWriter::new()
w.u32(self.next_serial())
w.u32(depressed)
w.u32(latched)
w.u32(locked)
w.u32(group)
c.send_msg(w.finish_msg(kid, 4U))
}
}
}
///| Cursor shape (wp_cursor_shape_v1 enum value) the client owning `win_id` last
/// requested; 1 (default) if it never asked for one.
pub fn WaylandServer::client_cursor_shape(self : WaylandServer, win_id : UInt64) -> UInt {
match self.win_client(win_id) {
Some((c, _)) => if c.cursor_shape == 0U { 1U } else { c.cursor_shape }
None => 1U
}
}
///| Popup rectangle (x, y, w, h) relative to the parent's content, from its positioner.
fn popup_placement(ps : PosState) -> (Int, Int, Int, Int) {
// anchor / gravity enum: 0 none, 1 top, 2 bottom, 3 left, 4 right, 5 top_left,
// 6 bottom_left, 7 top_right, 8 bottom_right.
let left = fn(v : UInt) { v == 3U || v == 5U || v == 6U }
let right = fn(v : UInt) { v == 4U || v == 7U || v == 8U }
let top = fn(v : UInt) { v == 1U || v == 5U || v == 7U }
let bottom = fn(v : UInt) { v == 2U || v == 6U || v == 8U }
let apx = if left(ps.anchor) {
ps.ax
} else if right(ps.anchor) {
ps.ax + ps.aw
} else {
ps.ax + ps.aw / 2
}
let apy = if top(ps.anchor) {
ps.ay
} else if bottom(ps.anchor) {
ps.ay + ps.ah
} else {
ps.ay + ps.ah / 2
}
let x = if left(ps.gravity) {
apx - ps.w
} else if right(ps.gravity) {
apx
} else {
apx - ps.w / 2
}
let y = if top(ps.gravity) {
apy - ps.h
} else if bottom(ps.gravity) {
apy
} else {
apy - ps.h / 2
}
(x + ps.ox, y + ps.oy, ps.w, ps.h)
}
///| Popup placement with the positioner's constraint adjustments applied, so the popup
/// stays inside `bounds` (x, y, w, h relative to the parent's content): per axis, flip if
/// that makes it fit, else slide it in, else shrink it. Unadjustable axes are left alone.
fn constrain_popup(ps : PosState, bounds : (Int, Int, Int, Int)) -> (Int, Int, Int, Int) {
let (bx, by, bw, bh) = bounds
let (x0, y0, w0, h0) = popup_placement(ps)
let mut x = x0
let mut y = y0
let mut w = w0
let mut h = h0
let out_x = fn(x : Int, w : Int) { x < bx || x + w > bx + bw }
let out_y = fn(y : Int, h : Int) { y < by || y + h > by + bh }
// flip_x: mirror anchor and gravity left <-> right.
if out_x(x, w) && (ps.adjust & 4U) != 0U {
let (fx, _, _, _) = popup_placement({
..ps,
anchor: flip_edge(ps.anchor, true),
gravity: flip_edge(ps.gravity, true),
ox: -ps.ox,
})
if !out_x(fx, w) {
x = fx
}
}
if out_x(x, w) && (ps.adjust & 1U) != 0U {
if x + w > bx + bw {
x = bx + bw - w
}
if x < bx {
x = bx
}
}
if out_x(x, w) && (ps.adjust & 16U) != 0U {
let l = if x < bx { bx } else { x }
let r = if x + w > bx + bw { bx + bw } else { x + w }
if r > l {
x = l
w = r - l
}
}
// flip_y: mirror anchor and gravity top <-> bottom.
if out_y(y, h) && (ps.adjust & 8U) != 0U {
let (_, fy, _, _) = popup_placement({
..ps,
anchor: flip_edge(ps.anchor, false),
gravity: flip_edge(ps.gravity, false),
oy: -ps.oy,
})
if !out_y(fy, h) {
y = fy
}
}
if out_y(y, h) && (ps.adjust & 2U) != 0U {
if y + h > by + bh {
y = by + bh - h
}
if y < by {
y = by
}
}
if out_y(y, h) && (ps.adjust & 32U) != 0U {
let t = if y < by { by } else { y }
let b = if y + h > by + bh { by + bh } else { y + h }
if b > t {
y = t
h = b - t
}
}
(x, y, w, h)
}
///| Mirror an anchor/gravity value horizontally (`horiz`) or vertically.
fn flip_edge(v : UInt, horiz : Bool) -> UInt {
// 0 none, 1 top, 2 bottom, 3 left, 4 right, 5 top_left, 6 bottom_left, 7 top_right, 8 bottom_right
if horiz {
match v {
3U => 4U
4U => 3U
5U => 7U
7U => 5U
6U => 8U
8U => 6U
_ => v
}
} else {
match v {
1U => 2U
2U => 1U
5U => 6U
6U => 5U
7U => 8U
8U => 7U
_ => v
}
}
}
///| xdg_popup.popup_done: ask the client to dismiss this popup.
pub fn WaylandServer::popup_done(self : WaylandServer, win_id : UInt64) -> Unit {
match self.win_client(win_id) {
Some((c, tl)) => c.send_msg(WireWriter::new().finish_msg(tl.wl_toplevel_id, 1U))
None => ()
}
}
///| Offer the current clipboard to `c`'s data devices (or clear it if there is none).
fn WaylandServer::send_selection(self : WaylandServer, c : ClientConn) -> Unit {
let mimes : Array[String] = match self.selection {
Some((0U, 0U)) => self.own_mimes
Some((ocid, osid)) =>
match self.find_client(ocid) {
Some(oc) =>
match oc.data_sources.get(osid) {
Some(m) => m
None => []
}
None => []
}
None => []
}
for dev in c.data_devices {
match self.selection {
Some((ocid, osid)) => {
let oid = c.next_offer
c.next_offer = c.next_offer + 1U
c.objects.set(oid, ObjDataOffer(ocid, osid))
let w = WireWriter::new()
w.u32(oid)
c.send_msg(w.finish_msg(dev, 0U)) // data_offer
for m in mimes {
let ow = WireWriter::new()
ow.str(m)
c.send_msg(ow.finish_msg(oid, 0U)) // offer
}
let sw = WireWriter::new()
sw.u32(oid)
c.send_msg(sw.finish_msg(dev, 5U)) // selection
}
None => {
let sw = WireWriter::new()
sw.u32(0U)
c.send_msg(sw.finish_msg(dev, 5U))
}
}
}
}
///| Offer the primary selection to `c`'s primary selection devices (or clear it).
fn WaylandServer::send_primary(self : WaylandServer, c : ClientConn) -> Unit {
let mimes : Array[String] = match self.primary {
Some((ocid, osid)) =>
match self.find_client(ocid) {
Some(oc) => oc.prim_sources.get(osid).unwrap_or([])
None => []
}
None => []
}
for dev in c.prim_devices {
match self.primary {
Some((ocid, osid)) => {
let oid = c.next_offer
c.next_offer = c.next_offer + 1U
c.objects.set(oid, ObjPrimaryOffer(ocid, osid))
let w = WireWriter::new()
w.u32(oid)
c.send_msg(w.finish_msg(dev, 0U)) // data_offer
for m in mimes {
let ow = WireWriter::new()
ow.str(m)
c.send_msg(ow.finish_msg(oid, 0U)) // offer
}
let sw = WireWriter::new()
sw.u32(oid)
c.send_msg(sw.finish_msg(dev, 1U)) // selection
}
None => {
let sw = WireWriter::new()
sw.u32(0U)
c.send_msg(sw.finish_msg(dev, 1U))
}
}
}
}
///| Push the primary selection to whichever client has keyboard focus.
fn WaylandServer::notify_primary(self : WaylandServer) -> Unit {
match self.kbd_client {
Some(cid) =>
match self.find_client(cid) {
Some(c) => self.send_primary(c)
None => ()
}
None => ()
}
}
///| Push the clipboard to whichever client has keyboard focus.
fn WaylandServer::notify_selection(self : WaylandServer) -> Unit {
match self.kbd_client {
Some(cid) =>
match self.find_client(cid) {
Some(c) => self.send_selection(c)
None => ()
}
None => ()
}
}
///| Put `data` on the clipboard as the compositor's own selection, offered as `mime_types`
/// (a screenshot: image/png). Whoever owned the clipboard is told it was replaced, and the
/// focused client gets the new offer at once.
pub fn WaylandServer::set_own_selection(
self : WaylandServer,
mime_types : Array[String],
data : Bytes,
) -> Unit {
match self.selection {
Some((ocid, osid)) =>
if ocid != 0U {
match self.find_client(ocid) {
Some(oc) => oc.send_msg(WireWriter::new().finish_msg(osid, 2U)) // cancelled
None => ()
}
}
None => ()
}
self.own_mimes = mime_types
self.own_data = data
self.selection = Some((0U, 0U))
self.notify_selection()
}
///| Whether the clipboard holds the compositor's own data (no client has replaced it since).
pub fn WaylandServer::owns_selection(self : WaylandServer) -> Bool {
self.selection == Some((0U, 0U))
}
///| Write what the pipes of pending pastes can take without blocking; close the finished ones
/// and any a reader stopped taking for ten seconds.
pub fn WaylandServer::pump_transfers(self : WaylandServer) -> Unit {
if self.transfers.length() == 0 {
return
}
let total = self.own_data.length()
let now = @sys.monotonic_ms()
let keep : Array[(Int, Int, UInt64)] = []
for t in self.transfers {
let (fd, sent0, deadline) = t
let mut sent = sent0
let mut ok = true
while sent < total {
let len = if total - sent > 65536 { 65536 } else { total - sent }
let n = @sys.write_nonblock(fd, self.own_data[sent:sent + len].to_bytes())
if n < 0 {
ok = false
break
}
if n == 0 {
break // the pipe is full: the rest goes out on a later turn
}
sent = sent + n
}
if ok && sent < total && now < deadline {
keep.push((fd, sent, deadline))
} else {
@sys.close_fd(fd)
}
}
self.transfers.clear()
self.transfers.append(keep)
}
///| Pastes of the compositor's clipboard still being written.
pub fn WaylandServer::pending_transfers(self : WaylandServer) -> Int {
self.transfers.length()
}
///| The window a subsurface belongs to, and its position relative to that window's surface
/// (nested subsurfaces added up). None while it hangs off a surface with no window yet.
fn sub_root(c : ClientConn, child : UInt) -> (UInt64, Int, Int)? {
let mut x = 0
let mut y = 0
let mut cur = child
for _depth = 0; _depth < 16; _depth = _depth + 1 {
match c.subsurfaces.get(cur) {
Some(st) => {
x = x + st.x
y = y + st.y
cur = st.parent
}
None =>
return match c.surfaces.get(cur) {
Some({ window_id: Some(wid), .. }) => Some((wid, x, y))
_ => None
}
}
}
None
}