CI / test (push) Failing after 15s
Chrome's context menu opened and closed at once: when a client created an xdg_popup the compositor moved keyboard focus to it straight away, and the wl_keyboard.leave on the window made Chrome dismiss the menu. Popups now only take the keyboard when they ask for a grab (xdg_popup.grab, as Qt menus do) and once they have a buffer; Chrome's grab-less menus leave focus on the window. Also: - honour xdg_positioner constraint adjustment (flip, slide, resize) so the configure tells the client where its menu really is, instead of the host silently nudging it on screen (Chrome then took the button release as a click on a menu item); - answer xdg_popup.reposition with repositioned + configure (it was ignored, so clients waiting for it stalled); - place popups by their window geometry, so Chrome's client-drawn menu shadow no longer shifts the menu right and down. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hikqzveCoytgF4Uyvh
1735 lines
48 KiB
MoonBit
1735 lines
48 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?)
|
|
ToplevelClosed(UInt64)
|
|
ToplevelMaximizeRequest(UInt64, Bool)
|
|
ToplevelFullscreenRequest(UInt64, Bool)
|
|
ToplevelMinimizeRequest(UInt64, Bool)
|
|
ToplevelMoveRequest(UInt64)
|
|
/// 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)
|
|
}
|
|
|
|
///|
|
|
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)
|
|
ObjSeat
|
|
ObjPointer
|
|
ObjKeyboard
|
|
ObjTouch
|
|
ObjOutput
|
|
ObjSubcompositor
|
|
ObjSubsurface
|
|
ObjDataManager
|
|
ObjDataSource
|
|
ObjDataDevice
|
|
/// Selection offer: (owner client id, owner's wl_data_source id).
|
|
ObjDataOffer(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 {
|
|
mut attached_buffer : UInt?
|
|
mut pending_frame_cb : UInt?
|
|
mut xdg_surface_id : UInt?
|
|
mut window_id : UInt64?
|
|
}
|
|
|
|
///|
|
|
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
|
|
}
|
|
|
|
///|
|
|
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?
|
|
}
|
|
|
|
///| 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]
|
|
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]]
|
|
/// 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: {},
|
|
toplevels: {},
|
|
positioners: {},
|
|
pointer_ids: [],
|
|
keyboard_ids: [],
|
|
seat_ver: 1U,
|
|
data_devices: [],
|
|
data_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)?
|
|
/// 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)
|
|
}
|
|
|
|
///|
|
|
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,
|
|
popup_bounds: fn(_) { (-1000000, -1000000, 2000000, 2000000) },
|
|
})
|
|
}
|
|
|
|
///|
|
|
pub fn WaylandServer::close(self : WaylandServer) -> Unit {
|
|
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),
|
|
]
|
|
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) -> Unit {
|
|
let w1 = WireWriter::new()
|
|
w1.u32(3U) // pointer (1) | keyboard (2)
|
|
c.send_msg(w1.finish_msg(seat_id, 0U))
|
|
|
|
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 {
|
|
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))
|
|
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,
|
|
) -> 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))
|
|
|
|
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) // 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 == "wl_seat" {
|
|
c.objects.set(new_id, ObjSeat)
|
|
c.seat_ver = ver
|
|
emit_seat_info(c, new_id)
|
|
} 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 == "wl_subcompositor" {
|
|
c.objects.set(new_id, ObjSubcompositor)
|
|
} else if iface == "wl_output" {
|
|
c.objects.set(new_id, ObjOutput)
|
|
emit_output_info(c, new_id, self.output_w, self.output_h)
|
|
}
|
|
}
|
|
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,
|
|
})
|
|
} 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
|
|
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_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 == 6U {
|
|
// commit
|
|
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
|
|
}
|
|
events.push(ToplevelCommitted(wid, snap))
|
|
}
|
|
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 => ()
|
|
}
|
|
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): accepted, not composited yet
|
|
let sub_id = reader.u32()
|
|
c.objects.set(sub_id, ObjSubsurface)
|
|
}
|
|
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 {
|
|
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)
|
|
}
|
|
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 =>
|
|
if opcode == 0U {
|
|
c.objects.remove(obj_id)
|
|
send_delete_id(c, obj_id)
|
|
}
|
|
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 })
|
|
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 == 3U {
|
|
// set_window_geometry(x, y, w, h)
|
|
let gx = reader.i32()
|
|
let gy = reader.i32()
|
|
match c.xdg_surfaces.get(xsid) {
|
|
Some(xs) => {
|
|
xs.geo_x = gx
|
|
xs.geo_y = gy
|
|
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,
|
|
})
|
|
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),
|
|
})
|
|
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 == 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)
|
|
}
|
|
ObjToplevelDeco(wid) =>
|
|
if opcode == 0U {
|
|
c.objects.remove(obj_id)
|
|
send_delete_id(c, obj_id)
|
|
} else if opcode == 1U || opcode == 2U {
|
|
// Always enforce Server-Side Decoration (2)
|
|
ignore(wid)
|
|
send_decoration_mode(c, obj_id, 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)
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
|
|
///| 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] = []
|
|
|
|
// 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, rfd) = @sys.recvmsg_fd(c.fd, scratch, 8192)
|
|
if rfd >= 0 {
|
|
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 => ()
|
|
}
|
|
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,
|
|
) -> 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)
|
|
}
|
|
|
|
let w1 = WireWriter::new()
|
|
w1.i32(width)
|
|
w1.i32(height)
|
|
w1.array(states)
|
|
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))
|
|
}
|
|
|
|
///|
|
|
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).
|
|
pub fn WaylandServer::pointer_enter(self : WaylandServer, win_id : UInt64, x : Double, y : Double) -> 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(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) -> 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(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)
|
|
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((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))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///| 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 => ()
|
|
}
|
|
}
|