CI / test (push) Successful in 25s
- server: wl_pointer/wl_keyboard events (enter/leave/motion/button/key/modifiers), host keymap shared with client keyboards - nested: receives host pointer + keyboard + keymap, queues InputEvents - host: routes input to the launcher popup or the window under the pointer; click-to-focus moves keyboard focus - shell: launcher button opens lib/launcher popup; entries start real apps (foot, konsole, dolphin, firefox, kate, systemsettings if installed) with WAYLAND_DISPLAY pointed at the compositor - sys: has_exe + spawn - e2e test for input delivery and the launcher popup Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1062 lines
27 KiB
MoonBit
1062 lines
27 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)
|
|
}
|
|
|
|
///|
|
|
enum ObjKind {
|
|
ObjDisplay
|
|
ObjRegistry
|
|
ObjCompositor
|
|
ObjSurface(UInt)
|
|
ObjRegion
|
|
ObjShm
|
|
ObjShmPool(UInt)
|
|
ObjBuffer(UInt)
|
|
ObjXdgWmBase
|
|
ObjXdgPositioner
|
|
ObjXdgSurface(UInt)
|
|
ObjXdgToplevel(UInt64)
|
|
ObjXdgPopup
|
|
ObjDecoManager
|
|
ObjToplevelDeco(UInt64)
|
|
ObjSeat
|
|
ObjPointer
|
|
ObjKeyboard
|
|
ObjTouch
|
|
ObjOutput
|
|
ObjSubcompositor
|
|
ObjSubsurface
|
|
ObjDataManager
|
|
ObjDataObj
|
|
}
|
|
|
|
///|
|
|
struct ShmPoolState {
|
|
fd : Int
|
|
mut size : Int
|
|
}
|
|
|
|
///|
|
|
struct ShmBufferState {
|
|
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?
|
|
}
|
|
|
|
///|
|
|
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
|
|
}
|
|
|
|
///|
|
|
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]
|
|
pointer_ids : Array[UInt]
|
|
keyboard_ids : Array[UInt]
|
|
mut seat_ver : 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: {},
|
|
pointer_ids: [],
|
|
keyboard_ids: [],
|
|
seat_ver: 1U,
|
|
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)
|
|
}
|
|
|
|
///|
|
|
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
|
|
output_w : Int
|
|
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
|
|
}
|
|
|
|
///|
|
|
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([]),
|
|
})
|
|
}
|
|
|
|
///|
|
|
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),
|
|
]
|
|
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))
|
|
}
|
|
|
|
///|
|
|
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("MoonPlasma")
|
|
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 raw = @sys.mmap_read(pool.fd, needed)
|
|
if raw.length() < needed {
|
|
return None
|
|
}
|
|
let byte_len = buf.stride * buf.height
|
|
let slice = if buf.offset == 0 && raw.length() == byte_len {
|
|
raw
|
|
} else {
|
|
let arr : Array[Byte] = Array(capacity=byte_len)
|
|
for i = 0; i < byte_len; i = i + 1 {
|
|
arr.push(raw[buf.offset + i])
|
|
}
|
|
Bytes::from_array(arr)
|
|
}
|
|
Some({
|
|
width: buf.width,
|
|
height: buf.height,
|
|
stride: buf.stride,
|
|
format: buf.format,
|
|
bytes: slice,
|
|
})
|
|
}
|
|
|
|
///|
|
|
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 == "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.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 || opcode == 1U {
|
|
// create_data_source / get_data_device: accepted, no selection support yet
|
|
let id = reader.u32()
|
|
c.objects.set(id, ObjDataObj)
|
|
}
|
|
ObjDataObj => ()
|
|
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 })
|
|
}
|
|
}
|
|
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) => @sys.close_fd(p.fd)
|
|
None => ()
|
|
}
|
|
c.shm_pools.remove(pool_id)
|
|
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 {
|
|
c.shm_buffers.remove(buf_id)
|
|
c.objects.remove(buf_id)
|
|
send_delete_id(c, buf_id)
|
|
}
|
|
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)
|
|
} 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 })
|
|
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 == 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,
|
|
})
|
|
self.win_to_client.set(wid, c.client_id)
|
|
} else if opcode == 2U {
|
|
let pid = reader.u32()
|
|
c.objects.set(pid, ObjXdgPopup)
|
|
}
|
|
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 == 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()
|
|
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
|
|
}
|
|
|
|
///| 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("moon-plasma-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)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
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)) =>
|
|
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))
|
|
}
|
|
}
|
|
}
|