diff --git a/compositor/host/e2e_test.mbt b/compositor/host/e2e_test.mbt index 1087167..070d304 100644 --- a/compositor/host/e2e_test.mbt +++ b/compositor/host/e2e_test.mbt @@ -376,3 +376,108 @@ test "a buffer stays valid after its wl_shm_pool is destroyed" { commit_shm_frame(comp, c, shm, surf, xdg, 1UL, destroy_pool=true) assert_true(comp.windows.get(1UL).unwrap().surface is Some(_)) } + +///| A solid ARGB8888 frame of `w` x `h` in a new memfd. +fn solid_memfd(w : Int, h : Int, b : Byte, g : Byte, r : Byte) -> Int { + let px : Array[Byte] = Array(capacity=w * h * 4) + for _i = 0; _i < w * h; _i = _i + 1 { + px.push(b) + px.push(g) + px.push(r) + px.push(b'\xff') + } + let fd = @sys.memfd_create("wl-shm-swapchain", w * h * 4) + ignore(@sys.pwrite_bytes(fd, Bytes::from_array(px), 0)) + fd +} + +///| +fn attach_commit(comp : @host.ChandraCompositor, c : TestWaylandClient, surf : UInt, buf : UInt?) -> Unit { + match buf { + Some(b) => { + let a = @wayland.WireWriter::new() + a.u32(b) + a.i32(0) + a.i32(0) + c.send(a.finish_msg(surf, 1U)) + } + None => () + } + c.send(@wayland.WireWriter::new().finish_msg(surf, 6U)) + for _i = 0; _i < 5; _i = _i + 1 { + comp.step() + @sys.sleep_ms(2) + } +} + +///| +test "swapchain pools sent in one message all keep their fds, and a bare commit keeps the frame" { + // Mesa's Vulkan WSI on wl_shm (what GPUI apps like Zed and Delta fall back to) makes one + // wl_shm_pool per swapchain image and libwayland flushes them with all their fds in one + // sendmsg. Every fd but the first used to be dropped, so those images never showed. + let socket_path = "/tmp/wayland-moon-swapchain-" + @sys.getpid().to_string() + ".sock" + let theme = @scene.ThemeTokens::moon_night() + let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap() + let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client( + comp, socket_path, "Delta", "dev.delta.Delta", + ) + match c.configure_serial { + Some(ser) => { + let ack = @wayland.WireWriter::new() + ack.u32(ser) + c.send(ack.finish_msg(xdg, 4U)) + } + None => () + } + let w = c.configured_w + let h = c.configured_h + let fds = [ + solid_memfd(w, h, b'\x00', b'\x00', b'\xff'), + solid_memfd(w, h, b'\x00', b'\xff', b'\x00'), + solid_memfd(w, h, b'\xff', b'\x00', b'\x00'), + ] + // Three create_pool + create_buffer requests, flushed together with their three fds. + let msg : Array[Byte] = [] + let bufs : Array[UInt] = [] + for _i = 0; _i < 3; _i = _i + 1 { + let pool = c.alloc_id() + let p = @wayland.WireWriter::new() + p.u32(pool) + p.i32(w * h * 4) + for b in p.finish_msg(shm, 0U) { + msg.push(b) + } + let buf = c.alloc_id() + let cb = @wayland.WireWriter::new() + cb.u32(buf) + cb.i32(0) + cb.i32(w) + cb.i32(h) + cb.i32(w * 4) + cb.u32(0U) + for b in cb.finish_msg(pool, 0U) { + msg.push(b) + } + bufs.push(buf) + } + assert_true(@sys.sendmsg_fds(c.fd, Bytes::from_array(msg), fds) > 0) + let win = comp.windows.get(1UL).unwrap() + let cx = win.geometry.x + win.geometry.w / 2 + let cy = win.geometry.y + win.geometry.h / 2 + + attach_commit(comp, c, surf, Some(bufs[2])) + assert_eq(comp.compose_frame().pixel(cx, cy), Some(@scene.Rgba::new(0, 0, 0xff, 0xff))) + attach_commit(comp, c, surf, Some(bufs[1])) + assert_eq(comp.compose_frame().pixel(cx, cy), Some(@scene.Rgba::new(0, 0xff, 0, 0xff))) + + // The buffer was released, so the client draws its next frame into it; a commit without a + // new attach (GPUI commits after ack_configure) must not show that half-drawn frame. + ignore(@sys.pwrite_bytes(fds[1], Bytes::from_array([b'\x00', b'\x00', b'\xff', b'\xff']), (h / 2 * w + w / 2) * 4)) + attach_commit(comp, c, surf, None) + assert_eq(comp.compose_frame().pixel(cx, cy), Some(@scene.Rgba::new(0, 0xff, 0, 0xff))) + for fd in fds { + @sys.close_fd(fd) + } + @sys.close_fd(c.fd) + comp.close() +} diff --git a/compositor/renderer/fill_wbtest.mbt b/compositor/renderer/fill_wbtest.mbt index 0025005..cb03c60 100644 --- a/compositor/renderer/fill_wbtest.mbt +++ b/compositor/renderer/fill_wbtest.mbt @@ -47,7 +47,7 @@ test "span-based rounded fills match the per-pixel reference" { } ///| -test "blitting a client buffer matches per-pixel blending, even partly off screen" { +test "blitting a client buffer blends premultiplied alpha per pixel, even partly off screen" { let w = 20 let h = 12 let bytes = FixedArray::make(w * h * 4, b'\x00') @@ -69,7 +69,19 @@ test "blitting a client buffer matches per-pixel blending, even partly off scree for col = 0; col < w; col = col + 1 { let o = row * w * 4 + col * 4 let al = if format == 1 { 255 } else { raw[o + 3].to_int() } - blend_pixel(b, pos.0 + col, pos.1 + row, raw[o + 2].to_int(), raw[o + 1].to_int(), raw[o].to_int(), al, None) + let x = pos.0 + col + let y = pos.1 + row + if x < 0 || y < 0 || x >= 64 || y >= 48 || al == 0 { + continue + } + let d = (y * 64 + x) * 4 + let inv = 255 - al + for k in [0, 1, 2] { + let s = raw[o + 2 - k].to_int() + let v = if al >= 255 { s } else { s + (b.data[d + k].to_int() * inv + 127) / 255 } + b.data[d + k] = v.clamp(min=0, max=255).to_byte() + } + b.data[d + 3] = if al >= 255 { b'\xff' } else { (al + (b.data[d + 3].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte() } } } assert_eq(a.data, b.data) diff --git a/compositor/renderer/renderer.mbt b/compositor/renderer/renderer.mbt index 49d0060..1c539c7 100644 --- a/compositor/renderer/renderer.mbt +++ b/compositor/renderer/renderer.mbt @@ -733,10 +733,11 @@ pub fn blit_shm_surface( data[d + 2] = src[so] data[d + 3] = b'\xff' } else if a > 0 { + // wl_shm ARGB8888 is premultiplied: the colour already carries its alpha. let inv = 255 - a - data[d] = ((src[so + 2].to_int() * a + data[d].to_int() * inv + 127) / 255).to_byte() - data[d + 1] = ((src[so + 1].to_int() * a + data[d + 1].to_int() * inv + 127) / 255).to_byte() - data[d + 2] = ((src[so].to_int() * a + data[d + 2].to_int() * inv + 127) / 255).to_byte() + data[d] = (src[so + 2].to_int() + (data[d].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte() + data[d + 1] = (src[so + 1].to_int() + (data[d + 1].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte() + data[d + 2] = (src[so].to_int() + (data[d + 2].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte() data[d + 3] = (a + (data[d + 3].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte() } col = col + 1 diff --git a/compositor/sys/sys_native.mbt b/compositor/sys/sys_native.mbt index b16c980..7d6071f 100644 --- a/compositor/sys/sys_native.mbt +++ b/compositor/sys/sys_native.mbt @@ -30,6 +30,16 @@ extern "C" fn c_recvmsg_fd( out_fd : FixedArray[Int], ) -> Int = "mp_recvmsg_fd" +///| +#borrow(buf, fds) +extern "C" fn c_sendmsg_fds( + fd : Int, + buf : Bytes, + len : Int, + fds : FixedArray[Int], + n_fds : Int, +) -> Int = "mp_sendmsg_fds" + ///| #borrow(buf) extern "C" fn c_sendmsg_fd( @@ -123,12 +133,26 @@ pub fn poll_read(fd : Int, timeout_ms : Int) -> Int { c_poll_read(fd, timeout_ms) } -///| Receive bytes and optional SCM_RIGHTS fd. Returns (bytes_read, received_fd). -/// bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF. -pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) { - let out_fd : FixedArray[Int] = FixedArray::make(1, -1) +///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns +/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF. +pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) { + let out_fd : FixedArray[Int] = FixedArray::make(33, -1) let n = c_recvmsg_fd(fd, buf, max_len, out_fd) - (n, out_fd[0]) + let fds : Array[Int] = [] + for i = 0; i < out_fd[0]; i = i + 1 { + fds.push(out_fd[1 + i]) + } + (n, fds) +} + +///| Receive bytes and an optional SCM_RIGHTS fd. Returns (bytes_read, received_fd); any further +/// fds are closed. bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF. +pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) { + let (n, fds) = recvmsg_fds(fd, buf, max_len) + for i = 1; i < fds.length(); i = i + 1 { + close_fd(fds[i]) + } + (n, if fds.length() > 0 { fds[0] } else { -1 }) } ///| @@ -136,6 +160,11 @@ pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int { c_sendmsg_fd(fd, buf, buf.length(), send_fd) } +///| Send `buf` with several fds in one SCM_RIGHTS message, the way libwayland flushes (tests). +pub fn sendmsg_fds(fd : Int, buf : Bytes, fds : Array[Int]) -> Int { + c_sendmsg_fds(fd, buf, buf.length(), FixedArray::from_array(fds), fds.length()) +} + ///| pub fn memfd_create(name : String, size : Int) -> Int { c_memfd_create(str_to_utf8_bytes(name), size) diff --git a/compositor/sys/sys_stub.c b/compositor/sys/sys_stub.c index 9b6ec3f..5ed5c89 100644 --- a/compositor/sys/sys_stub.c +++ b/compositor/sys/sys_stub.c @@ -121,7 +121,11 @@ int32_t mp_poll_read(int32_t fd, int32_t timeout_ms) { MOONBIT_FFI_EXPORT int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t *out_fd) { - out_fd[0] = -1; + /* out_fd[0] = number of fds received, out_fd[1..] the fds. A client flushes up to 28 fds + (libwayland's limit) with one sendmsg, e.g. a Vulkan swapchain making a wl_shm_pool per + image: every one of them is needed, in order. */ + int32_t out_cap = Moonbit_array_length(out_fd) - 1; + out_fd[0] = 0; int32_t cap = Moonbit_array_length(buf); int32_t n = max_len < cap ? max_len : cap; if (n <= 0) return 0; @@ -130,7 +134,7 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t iov.iov_base = buf; iov.iov_len = (size_t)n; - char cmsg_buf[CMSG_SPACE(sizeof(int) * 4)]; + char cmsg_buf[CMSG_SPACE(sizeof(int) * 32)]; memset(cmsg_buf, 0, sizeof(cmsg_buf)); struct msghdr msg; @@ -140,7 +144,7 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t msg.msg_control = cmsg_buf; msg.msg_controllen = sizeof(cmsg_buf); - ssize_t recvd = recvmsg(fd, &msg, 0); + ssize_t recvd = recvmsg(fd, &msg, MSG_CMSG_CLOEXEC); if (recvd < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) return -1; return -2; @@ -150,9 +154,11 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { int *fds = (int *)CMSG_DATA(cmsg); size_t num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); - if (num_fds > 0) { - out_fd[0] = fds[0]; - for (size_t i = 1; i < num_fds; i++) { + for (size_t i = 0; i < num_fds; i++) { + if (out_fd[0] < out_cap) { + out_fd[1 + out_fd[0]] = fds[i]; + out_fd[0]++; + } else { close(fds[i]); } } @@ -161,6 +167,30 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t return (int32_t)recvd; } +MOONBIT_FFI_EXPORT +int32_t mp_sendmsg_fds(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t *fds, int32_t n_fds) { + int32_t cap = Moonbit_array_length(buf); + int32_t n = len < cap ? len : cap; + if (n <= 0 || n_fds < 0 || n_fds > 28) return -1; + struct iovec iov = { .iov_base = buf, .iov_len = (size_t)n }; + char cmsg_buf[CMSG_SPACE(sizeof(int) * 28)]; + memset(cmsg_buf, 0, sizeof(cmsg_buf)); + struct msghdr msg; + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + if (n_fds > 0) { + msg.msg_control = cmsg_buf; + msg.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds); + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds); + memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds); + } + return (int32_t)sendmsg(fd, &msg, MSG_NOSIGNAL); +} + MOONBIT_FFI_EXPORT int32_t mp_sendmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t send_fd) { int32_t cap = Moonbit_array_length(buf); diff --git a/compositor/sys/sys_wasm.mbt b/compositor/sys/sys_wasm.mbt index 33706bf..a505206 100644 --- a/compositor/sys/sys_wasm.mbt +++ b/compositor/sys/sys_wasm.mbt @@ -83,6 +83,32 @@ pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) (r.count, r.fd) } +///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns +/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF. +pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) { + let (n, first) = recvmsg_fd(fd, buf, max_len) + let fds : Array[Int] = [] + if first >= 0 { + fds.push(first) + while true { + let more = @hostsys.take_fd(fd) + if more < 0 { + break + } + fds.push(more) + } + } + (n, fds) +} + +///| Several fds in one message are a native-only test helper. +pub fn sendmsg_fds(fd : Int, buf : Bytes, fds : Array[Int]) -> Int { + if fds.length() > 1 { + abort("sendmsg_fds: one fd at most on the wasm build") + } + sendmsg_fd(fd, buf, if fds.length() == 1 { fds[0] } else { -1 }) +} + ///| pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int { @hostsys.sendmsg(fd, to_fixed(buf), send_fd) diff --git a/compositor/wayland/server.mbt b/compositor/wayland/server.mbt index 7dc931f..3657cde 100644 --- a/compositor/wayland/server.mbt +++ b/compositor/wayland/server.mbt @@ -44,7 +44,8 @@ enum ObjKind { ObjPointer ObjKeyboard ObjTouch - ObjOutput + /// wl_output with the version the client bound (done and scale are since version 2). + ObjOutput(UInt) ObjSubcompositor ObjSubsurface ObjDataManager @@ -77,6 +78,9 @@ struct ShmBufferState { ///| 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? @@ -343,14 +347,17 @@ fn emit_shm_formats(c : ClientConn, shm_id : UInt) -> Unit { } ///| -fn emit_seat_info(c : ClientConn, seat_id : UInt) -> Unit { +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)) - let w2 = WireWriter::new() - w2.str("seat0") - c.send_msg(w2.finish_msg(seat_id, 1U)) + // 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. @@ -365,14 +372,16 @@ pub fn WaylandServer::set_output_size(self : WaylandServer, w : Int, h : Int) -> continue } for id, kind in c.objects { - if kind is ObjOutput { + 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)) - c.send_msg(WireWriter::new().finish_msg(id, 2U)) // done + if over >= 2U { + c.send_msg(WireWriter::new().finish_msg(id, 2U)) // done + } } } } @@ -384,6 +393,7 @@ fn emit_output_info( out_id : UInt, w_px : Int, h_px : Int, + ver : UInt, ) -> Unit { let g = WireWriter::new() g.i32(0) @@ -403,12 +413,14 @@ fn emit_output_info( 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)) + // 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)) + } } ///| @@ -521,7 +533,7 @@ fn WaylandServer::dispatch_msg( } else if iface == "wl_seat" { c.objects.set(new_id, ObjSeat) c.seat_ver = ver - emit_seat_info(c, new_id) + 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" { @@ -529,8 +541,8 @@ fn WaylandServer::dispatch_msg( } 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) + c.objects.set(new_id, ObjOutput(ver)) + emit_output_info(c, new_id, self.output_w, self.output_h, ver) } } ObjCompositor => @@ -613,6 +625,7 @@ fn WaylandServer::dispatch_msg( } None => () } + surf.attached_buffer = None match surf.pending_frame_cb { Some(cb_id) => { surf.pending_frame_cb = None @@ -1146,8 +1159,8 @@ pub fn WaylandServer::poll_events( 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 { + let (n, rfds) = @sys.recvmsg_fds(c.fd, scratch, 8192) + for rfd in rfds { c.pending_fds.push(rfd) } if n > 0 { diff --git a/host-wasm/src/main.rs b/host-wasm/src/main.rs index be0167e..f01fc35 100644 --- a/host-wasm/src/main.rs +++ b/host-wasm/src/main.rs @@ -48,6 +48,8 @@ struct State { raster: raster::Raster, /// Runs the panel modules; set once the engine exists. pl: Option, + /// Fds beyond the first that arrived with the last `recvmsg` on a socket, for `take-fd`. + extra_fds: std::collections::HashMap>, } fn sockaddr_un(path: &str) -> (libc::sockaddr_un, libc::socklen_t) { @@ -447,6 +449,12 @@ impl Host for State { fn close_fd(&mut self, fd: i32) { if fd >= 0 { + // A closed client socket's number is reused by the next one: drop what it left. + if let Some(left) = self.extra_fds.remove(&fd) { + for f in left { + unsafe { libc::close(f) }; + } + } unsafe { libc::close(fd) }; } } @@ -470,20 +478,28 @@ impl Host for State { } fn recvmsg(&mut self, fd: i32, max_len: u32) -> Received { + // Fds from the previous read nobody took would leak: close them. + if let Some(left) = self.extra_fds.remove(&fd) { + for f in left { + unsafe { libc::close(f) }; + } + } let mut data = vec![0u8; max_len as usize]; if data.is_empty() { return Received { count: 0, fd: -1, data }; } unsafe { let mut iov = libc::iovec { iov_base: data.as_mut_ptr() as *mut _, iov_len: data.len() }; - let space = libc::CMSG_SPACE((std::mem::size_of::() * 4) as u32) as usize; + // A client flushes up to 28 fds (libwayland's limit) in one sendmsg, e.g. a Vulkan + // swapchain making a wl_shm_pool per image: every one is needed, in order. + let space = libc::CMSG_SPACE((std::mem::size_of::() * 32) as u32) as usize; let mut cbuf = vec![0u8; space]; let mut msg: libc::msghdr = std::mem::zeroed(); msg.msg_iov = &mut iov; msg.msg_iovlen = 1; msg.msg_control = cbuf.as_mut_ptr() as *mut _; msg.msg_controllen = space as _; - let n = libc::recvmsg(fd, &mut msg, 0); + let n = libc::recvmsg(fd, &mut msg, libc::MSG_CMSG_CLOEXEC); if n < 0 { let e = *libc::__errno_location(); let c = if e == libc::EAGAIN || e == libc::EWOULDBLOCK { -1 } else { -2 }; @@ -497,10 +513,10 @@ impl Host for State { let fds = libc::CMSG_DATA(cm) as *const i32; for i in 0..n_fds { let f = *fds.add(i); - if i == 0 { + if got < 0 { got = f; } else { - libc::close(f); + self.extra_fds.entry(fd).or_default().push_back(f); } } } @@ -511,6 +527,18 @@ impl Host for State { } } + fn take_fd(&mut self, sock: i32) -> i32 { + let q = match self.extra_fds.get_mut(&sock) { + Some(q) => q, + None => return -1, + }; + let f = q.pop_front().unwrap_or(-1); + if q.is_empty() { + self.extra_fds.remove(&sock); + } + f + } + fn sendmsg(&mut self, fd: i32, data: Vec, send_fd: i32) -> i32 { if data.is_empty() { return 0; diff --git a/host-wasm/src/raster.rs b/host-wasm/src/raster.rs index 7a63387..ff0bc96 100644 --- a/host-wasm/src/raster.rs +++ b/host-wasm/src/raster.rs @@ -672,10 +672,11 @@ fn blit_surface(c: &mut Canvas, s: &Surface, dst: R) { if a >= 255 { dp.copy_from_slice(sp); } else if a > 0 { + // wl_shm ARGB8888 is premultiplied: the colour already carries its alpha. let inv = 255 - a; - dp[0] = ((sp[0] as i32 * a + dp[0] as i32 * inv + 127) / 255) as u8; - dp[1] = ((sp[1] as i32 * a + dp[1] as i32 * inv + 127) / 255) as u8; - dp[2] = ((sp[2] as i32 * a + dp[2] as i32 * inv + 127) / 255) as u8; + dp[0] = clamp_u8(sp[0] as i32 + (dp[0] as i32 * inv + 127) / 255); + dp[1] = clamp_u8(sp[1] as i32 + (dp[1] as i32 * inv + 127) / 255); + dp[2] = clamp_u8(sp[2] as i32 + (dp[2] as i32 * inv + 127) / 255); dp[3] = clamp_u8(a + (dp[3] as i32 * inv + 127) / 255); } } diff --git a/interface/chandra/host/sys/ffi.mbt b/interface/chandra/host/sys/ffi.mbt index af90426..a560945 100644 --- a/interface/chandra/host/sys/ffi.mbt +++ b/interface/chandra/host/sys/ffi.mbt @@ -18,6 +18,9 @@ fn wasmImportUnlink(p0 : Int, p1 : Int) = "chandra:host/sys@0.1.0" "unlink" ///| fn wasmImportPollRead(p0 : Int, p1 : Int) -> Int = "chandra:host/sys@0.1.0" "poll-read" +///| +fn wasmImportTakeFd(p0 : Int) -> Int = "chandra:host/sys@0.1.0" "take-fd" + ///| fn wasmImportRecvmsg(p0 : Int, p1 : Int, p2 : Int) = "chandra:host/sys@0.1.0" "recvmsg" diff --git a/interface/chandra/host/sys/native_stub.mbt b/interface/chandra/host/sys/native_stub.mbt index c2dad6c..fc8c044 100644 --- a/interface/chandra/host/sys/native_stub.mbt +++ b/interface/chandra/host/sys/native_stub.mbt @@ -25,6 +25,11 @@ fn wasmImportUnlink(p0 : Int, p1 : Int) -> Unit { abort("wasm-only host import") } +///| +fn wasmImportTakeFd(p0 : Int) -> Int { + abort("wasm-only host import") +} + ///| fn wasmImportPollRead(p0 : Int, p1 : Int) -> Int { abort("wasm-only host import") diff --git a/interface/chandra/host/sys/pkg.generated.mbti b/interface/chandra/host/sys/pkg.generated.mbti index 7eb19bf..374ef7e 100644 --- a/interface/chandra/host/sys/pkg.generated.mbti +++ b/interface/chandra/host/sys/pkg.generated.mbti @@ -40,6 +40,8 @@ pub fn sleep_ms(Int32) -> Unit pub fn spawn(String, String) -> Bool +pub fn take_fd(Int32) -> Int32 + pub fn unix_accept(Int32) -> Int32 pub fn unix_connect(String) -> Int32 diff --git a/interface/chandra/host/sys/top.mbt b/interface/chandra/host/sys/top.mbt index 5341542..4f9bf5a 100644 --- a/interface/chandra/host/sys/top.mbt +++ b/interface/chandra/host/sys/top.mbt @@ -69,6 +69,16 @@ pub fn recvmsg(fd : Int, max_len : UInt) -> Received { mbt_ffi_free(return_area) return ret +} +///| +/// The next further fd that arrived with the last `recvmsg` on `sock` (a client can send +/// several in one message), -1 when none is left. +pub fn take_fd(sock : Int) -> Int { + + let result : Int = wasmImportTakeFd(sock); + let ret = result + return ret + } ///| /// Send `data`, with `send-fd` (or -1) as SCM_RIGHTS; bytes sent or <0. diff --git a/wit/host/sys.wit b/wit/host/sys.wit index 3f24415..86fbbba 100644 --- a/wit/host/sys.wit +++ b/wit/host/sys.wit @@ -22,6 +22,9 @@ interface sys { data: list, } recvmsg: func(fd: s32, max-len: u32) -> received; + /// The next further fd that arrived with the last `recvmsg` on `sock` (a client can send + /// several in one message), -1 when none is left. + take-fd: func(sock: s32) -> s32; /// Send `data`, with `send-fd` (or -1) as SCM_RIGHTS; bytes sent or <0. sendmsg: func(fd: s32, data: list, send-fd: s32) -> s32;