Reload panel modules from outside the session: control socket, chandra-ctl, SIGUSR1
CI / test (push) Failing after 16s
CI / test (push) Failing after 16s
Until now a module reload was only reachable by clicking Settings > Panel inside the session. Now: - The compositor listens on $XDG_RUNTIME_DIR/chandra/<wayland socket name>.sock (the login session's is wayland-moon-0.sock) with a line protocol: ping, list, reload [NAME], help. Replies end with an "ok ..." or "error ..." line. It lives in compositor/host/control.mbt, runs from ChandraCompositor::step, and its fds join the idle loop's wait set, so an idle desktop still sleeps (measured ~0.4% CPU on wasmhost, ~0.1% on host-wasm) and wakes for a command. A path another live compositor answers on is left alone. - DesktopShell::reload_modules(name) reloads every loaded module (or those whose module id, stem, slot or path is NAME) in place via replace_slot: same panel position, saved :module-config applied, a module that fails to load stays as it was and the error is reported to the caller and shown as a notice. The catalog is rescanned. - SIGUSR1 to the host reloads every module (result in the log and the notice): new WIT function sys.take-sigusr1, implemented in compositor/sys (native build and wasmhost) and host-wasm; the handler has no SA_RESTART so it interrupts the idle poll. - session/chandra-ctl (installed to /usr/local/bin), `just reload [NAME]`, docs/reloading.md, README. - Tests: compositor/host/control_test.mbt (protocol, reload all / by name / by slot, order and settings kept, failing module kept, busy socket). Checked headless on both hosts with chandra-ctl and kill -USR1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hjLJtvYcgW48okL6qQ
This commit is contained in:
21 files changed
+844
-31
No files matched your search
@@ -190,6 +190,8 @@ pub fn run(args : Array[String]) -> Int {
|
||||
None => ()
|
||||
}
|
||||
@sys.log("chandra compositor listening on " + socket_path)
|
||||
// chandra-ctl's socket (reload panel modules from a script); SIGUSR1 reloads too.
|
||||
ignore(comp.start_control(@host.control_socket_path(@sys.getenv("XDG_RUNTIME_DIR"), socket_path)))
|
||||
if restore {
|
||||
ignore(comp.restore_session()) // reopen the windows the last session ended with
|
||||
}
|
||||
@@ -265,6 +267,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
// Idle: sleep until a client, the seat or the clock has something.
|
||||
let fds = comp.server.wait_fds()
|
||||
fds.append(k.wait_fds())
|
||||
fds.append(comp.control_fds()) // chandra-ctl
|
||||
let pfd = comp.portal_fd() // apps asking the dark/light preference
|
||||
if pfd >= 0 {
|
||||
fds.push(pfd)
|
||||
@@ -287,6 +290,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
// Whether this pass had anything to do; an idle pass sleeps on the fds instead of a frame.
|
||||
let mut busy = false
|
||||
let wait_fds = comp.server.wait_fds()
|
||||
wait_fds.append(comp.control_fds()) // chandra-ctl
|
||||
match nested {
|
||||
Some(n) => {
|
||||
if !n.poll() {
|
||||
|
||||
@@ -121,6 +121,8 @@ fn main {
|
||||
None => ()
|
||||
}
|
||||
println("chandra compositor listening on " + socket_path + " (wasi-gfx surface active)")
|
||||
// chandra-ctl's socket (reload panel modules from a script); SIGUSR1 reloads too.
|
||||
ignore(comp.start_control(@host.control_socket_path(@sys.getenv("XDG_RUNTIME_DIR"), socket_path)))
|
||||
if restore {
|
||||
ignore(comp.restore_session()) // reopen the windows the last session ended with
|
||||
}
|
||||
|
||||
@@ -47,6 +47,8 @@ pub(all) struct ChandraCompositor {
|
||||
/// The xdg-desktop-portal Settings backend apps read the dark/light preference from
|
||||
/// (compositor/portal), started with the first `sync_system_theme_pref`.
|
||||
mut settings_portal : @portal.SettingsPortal?
|
||||
/// The control socket (control.mbt): chandra-ctl reload / list. None until `start_control`.
|
||||
mut control : ControlServer?
|
||||
/// Running as the login session (--drm): power actions act on the machine (power.mbt).
|
||||
mut login_session : Bool
|
||||
/// Power (power.mbt): when input last came (monotonic ms), whether the screen is off for being
|
||||
@@ -173,6 +175,7 @@ pub fn ChandraCompositor::new(
|
||||
dark_theme: theme,
|
||||
sync_system_theme: false,
|
||||
settings_portal: None,
|
||||
control: None,
|
||||
login_session: false,
|
||||
last_input_ms: @sys.monotonic_ms(),
|
||||
screen_off: false,
|
||||
@@ -279,6 +282,7 @@ pub fn ChandraCompositor::close(self : ChandraCompositor) -> Unit {
|
||||
for _, win in self.windows {
|
||||
release_snapshot(win.surface)
|
||||
}
|
||||
self.stop_control()
|
||||
self.shell.shutdown()
|
||||
match self.settings_portal {
|
||||
Some(p) => p.close()
|
||||
@@ -385,6 +389,7 @@ fn ChandraCompositor::flush_configures(self : ChandraCompositor) -> Unit {
|
||||
pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
|
||||
self.flush_configures()
|
||||
self.portal_tick()
|
||||
self.control_tick()
|
||||
let events = self.server.poll_events()
|
||||
for ev in events {
|
||||
match ev {
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
///| The control socket: lets scripts outside the session reload panel modules (after
|
||||
/// `just install-session` or a module rebuild) without clicking through Settings > Panel.
|
||||
///
|
||||
/// A unix stream socket at `$XDG_RUNTIME_DIR/chandra/<wayland socket name>.sock`, for example
|
||||
/// `/run/user/1000/chandra/wayland-moon-0.sock` (`control_socket_path`). XDG_RUNTIME_DIR is
|
||||
/// private to the user (0700), so only the user's own processes can reach it. A client writes
|
||||
/// one command per line; every reply is zero or more lines of output followed by one line that
|
||||
/// starts with `ok` or `error`:
|
||||
///
|
||||
/// ping ok pong
|
||||
/// list w1 launcher /usr/local/share/chandra/modules/launcher.wasm ... ok 7 modules
|
||||
/// reload reloaded w8 launcher ... ok 7 reloaded
|
||||
/// reload clock reloaded w9 clock-digital ok 1 reloaded
|
||||
/// help the commands, then ok
|
||||
///
|
||||
/// `session/chandra-ctl` is the command-line client; docs/reloading.md has the details. SIGUSR1
|
||||
/// to the host process (`pkill -USR1 -x chandra-host`) reloads every module the same way, with
|
||||
/// the result shown as a notice above the panel and written to the log.
|
||||
///
|
||||
/// The main loop keeps sleeping while idle: the listening socket and the connected clients are
|
||||
/// in `control_fds`, which the loop adds to the fds it waits on.
|
||||
|
||||
///| One connected client: its fd and the bytes of a line not finished yet.
|
||||
struct ControlClient {
|
||||
fd : Int
|
||||
pending : Array[Byte]
|
||||
}
|
||||
|
||||
///| The listening socket, its path (unlinked on close) and the connected clients.
|
||||
pub(all) struct ControlServer {
|
||||
path : String
|
||||
fd : Int
|
||||
clients : Array[ControlClient]
|
||||
}
|
||||
|
||||
///| Where the control socket of the compositor serving `wayland_socket` lives:
|
||||
/// `<runtime_dir>/chandra/<file name of wayland_socket>.sock` ("" when `runtime_dir` is empty).
|
||||
pub fn control_socket_path(runtime_dir : String, wayland_socket : String) -> String {
|
||||
if runtime_dir == "" {
|
||||
return ""
|
||||
}
|
||||
let mut start = 0
|
||||
for i = 0; i < wayland_socket.length(); i = i + 1 {
|
||||
if wayland_socket[i] == '/' {
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
let name = wayland_socket[start:].to_string()
|
||||
runtime_dir + "/chandra/" + (if name == "" { "chandra" } else { name }) + ".sock"
|
||||
}
|
||||
|
||||
///| Open the control socket at `path` (its directory is created). False, with a log line, when
|
||||
/// it can't be opened, or when another compositor is already answering on it (its socket is left
|
||||
/// alone: a nested compositor must not take a running session's socket away).
|
||||
pub fn ChandraCompositor::start_control(self : ChandraCompositor, path : String) -> Bool {
|
||||
self.stop_control()
|
||||
if path == "" {
|
||||
@sys.log("control socket: XDG_RUNTIME_DIR is not set; chandra-ctl and SIGUSR1 reloads are off")
|
||||
return false
|
||||
}
|
||||
let mut dir = ""
|
||||
for i = 0; i < path.length(); i = i + 1 {
|
||||
if path[i] == '/' {
|
||||
dir = path[:i].to_string()
|
||||
}
|
||||
}
|
||||
if dir != "" {
|
||||
ignore(@sys.mkdir_p(dir))
|
||||
}
|
||||
let probe = @sys.unix_connect(path)
|
||||
if probe >= 0 {
|
||||
@sys.close_fd(probe)
|
||||
@sys.log("control socket: " + path + " is in use by another compositor; this one has none")
|
||||
return false
|
||||
}
|
||||
let fd = @sys.unix_listen(path)
|
||||
if fd < 0 {
|
||||
@sys.log("control socket: can't listen on " + path)
|
||||
return false
|
||||
}
|
||||
self.control = Some({ path, fd, clients: [] })
|
||||
@sys.log("control socket on " + path + " (chandra-ctl reload)")
|
||||
true
|
||||
}
|
||||
|
||||
///| Close the control socket and its clients and remove the socket file.
|
||||
pub fn ChandraCompositor::stop_control(self : ChandraCompositor) -> Unit {
|
||||
match self.control {
|
||||
None => ()
|
||||
Some(c) => {
|
||||
for cl in c.clients {
|
||||
@sys.close_fd(cl.fd)
|
||||
}
|
||||
@sys.close_fd(c.fd)
|
||||
@sys.unlink(c.path)
|
||||
self.control = None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| The fds the idle main loop must wake for: the listening socket and every client.
|
||||
pub fn ChandraCompositor::control_fds(self : ChandraCompositor) -> Array[Int] {
|
||||
match self.control {
|
||||
None => []
|
||||
Some(c) => {
|
||||
let fds = [c.fd]
|
||||
for cl in c.clients {
|
||||
fds.push(cl.fd)
|
||||
}
|
||||
fds
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Longest command line taken; a client sending more without a newline is dropped.
|
||||
let control_max_line : Int = 4096
|
||||
|
||||
///| Accept new clients and answer every complete line the clients sent (called from `step`).
|
||||
fn ChandraCompositor::control_tick(self : ChandraCompositor) -> Unit {
|
||||
if @sys.take_sigusr1() {
|
||||
@sys.log("SIGUSR1: reloading every panel module")
|
||||
let (lines, ok) = self.control_command("reload")
|
||||
for l in lines {
|
||||
@sys.log(" " + l)
|
||||
}
|
||||
ignore(ok)
|
||||
}
|
||||
let c = match self.control {
|
||||
None => return
|
||||
Some(c) => c
|
||||
}
|
||||
while true {
|
||||
let fd = @sys.unix_accept(c.fd)
|
||||
if fd < 0 {
|
||||
break
|
||||
}
|
||||
c.clients.push({ fd, pending: [] })
|
||||
}
|
||||
let gone : Array[Int] = []
|
||||
let buf : FixedArray[Byte] = FixedArray::make(1024, b'\x00')
|
||||
for cl in c.clients {
|
||||
let mut open = true
|
||||
while open {
|
||||
let (n, extra) = @sys.recvmsg_fd(cl.fd, buf, 1024)
|
||||
if extra >= 0 {
|
||||
@sys.close_fd(extra)
|
||||
}
|
||||
if n == -1 {
|
||||
break // nothing more for now
|
||||
}
|
||||
if n <= 0 {
|
||||
open = false // EOF or error
|
||||
break
|
||||
}
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
let b = buf[i]
|
||||
if b == b'\n' {
|
||||
let line = @utf8.decode_lossy(Bytes::from_array(cl.pending))
|
||||
cl.pending.clear()
|
||||
let (out, _) = self.control_command(line)
|
||||
let reply = StringBuilder::new()
|
||||
for l in out {
|
||||
reply.write_string(l)
|
||||
reply.write_char('\n')
|
||||
}
|
||||
if @sys.sendmsg_fd(cl.fd, @utf8.encode(reply.to_string()), -1) < 0 {
|
||||
open = false
|
||||
break
|
||||
}
|
||||
} else {
|
||||
cl.pending.push(b)
|
||||
}
|
||||
}
|
||||
if cl.pending.length() > control_max_line {
|
||||
ignore(@sys.sendmsg_fd(cl.fd, @utf8.encode("error line too long\n"), -1))
|
||||
open = false
|
||||
}
|
||||
}
|
||||
if !open {
|
||||
gone.push(cl.fd)
|
||||
}
|
||||
}
|
||||
if gone.length() > 0 {
|
||||
for fd in gone {
|
||||
@sys.close_fd(fd)
|
||||
}
|
||||
c.clients.retain(cl => !gone.contains(cl.fd))
|
||||
}
|
||||
}
|
||||
|
||||
///| Run one control command; the reply lines (the last starts with "ok" or "error") and whether
|
||||
/// it succeeded.
|
||||
pub fn ChandraCompositor::control_command(self : ChandraCompositor, line : String) -> (Array[String], Bool) {
|
||||
let words : Array[String] = []
|
||||
for w in line.trim(chars=" \t\r").split(" ") {
|
||||
let s = w.to_string()
|
||||
if s != "" {
|
||||
words.push(s)
|
||||
}
|
||||
}
|
||||
if words.length() == 0 {
|
||||
return (["error empty command (try: help)"], false)
|
||||
}
|
||||
match words[0] {
|
||||
"ping" => (["ok pong"], true)
|
||||
"help" =>
|
||||
(
|
||||
[
|
||||
"ping check the compositor answers", "list loaded panel modules: slot, module, path",
|
||||
"reload load every panel module again from disk", "reload NAME only the modules whose name, slot or path is NAME",
|
||||
"ok",
|
||||
],
|
||||
true,
|
||||
)
|
||||
"list" => {
|
||||
let mods = self.shell.loaded_modules()
|
||||
let out = mods.map(m => m.0 + " " + m.1 + " " + m.2)
|
||||
out.push("ok " + mods.length().to_string() + " modules")
|
||||
(out, true)
|
||||
}
|
||||
"reload" => {
|
||||
if words.length() > 2 {
|
||||
return (["error usage: reload [NAME]"], false)
|
||||
}
|
||||
let name = if words.length() == 2 { words[1] } else { "" }
|
||||
let (out, ok) = self.shell.reload_modules(name)
|
||||
// The panel changed under the frame signature's feet: draw it again.
|
||||
self.frame_changed = true
|
||||
self.damage.mark_full()
|
||||
let n = out.filter(l => l.has_prefix("reloaded ")).length()
|
||||
if ok {
|
||||
out.push("ok " + n.to_string() + " reloaded")
|
||||
} else if n == 0 && out.length() == 1 && !out[0].has_prefix("failed ") {
|
||||
out[0] = "error " + out[0]
|
||||
} else {
|
||||
out.push("error " + (out.length() - n).to_string() + " of " + out.length().to_string() + " failed to reload")
|
||||
}
|
||||
(out, ok)
|
||||
}
|
||||
other => (["error unknown command '" + other + "' (try: help)"], false)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
///| The control socket (control.mbt): chandra-ctl's commands and module reloads.
|
||||
/// Needs the modules: run scripts/build-modules.sh (just test does).
|
||||
|
||||
///| A compositor with its control socket open in a fresh directory.
|
||||
fn control_comp(tag : String) -> (@host.ChandraCompositor, String) raise {
|
||||
let comp = new_comp("control-" + tag)
|
||||
let dir = "/tmp/chandra-control-" + tag + "-" + @sys.getpid().to_string()
|
||||
let path = @host.control_socket_path(dir, "/run/user/1000/wayland-moon-0")
|
||||
assert_eq(path, dir + "/chandra/wayland-moon-0.sock")
|
||||
assert_true(comp.start_control(path))
|
||||
(comp, path)
|
||||
}
|
||||
|
||||
///| Send `line` over a new connection, let the compositor run a pass, and read its reply lines.
|
||||
fn ask(comp : @host.ChandraCompositor, path : String, line : String) -> Array[String] raise {
|
||||
let fd = @sys.unix_connect(path)
|
||||
assert_true(fd >= 0)
|
||||
assert_true(@sys.sendmsg_fd(fd, @utf8.encode(line + "\n"), -1) > 0)
|
||||
let raw : Array[Byte] = []
|
||||
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
|
||||
let mut tries = 0
|
||||
let mut done = false
|
||||
while !done && tries < 50 {
|
||||
comp.step()
|
||||
while @sys.poll_read(fd, 5) > 0 {
|
||||
let (n, _) = @sys.recvmsg_fd(fd, buf, 4096)
|
||||
if n <= 0 {
|
||||
break
|
||||
}
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
raw.push(buf[i])
|
||||
}
|
||||
}
|
||||
let text = @utf8.decode_lossy(Bytes::from_array(raw))
|
||||
let lines = text.split("\n").map(s => s.to_string()).filter(s => s != "").collect()
|
||||
if text.has_suffix("\n") && lines.length() > 0 {
|
||||
let last = lines[lines.length() - 1]
|
||||
done = last.has_prefix("ok") || last.has_prefix("error")
|
||||
}
|
||||
tries = tries + 1
|
||||
}
|
||||
@sys.close_fd(fd)
|
||||
let text = @utf8.decode_lossy(Bytes::from_array(raw))
|
||||
text.split("\n").map(s => s.to_string()).filter(s => s != "").collect()
|
||||
}
|
||||
|
||||
///| The slot ids of the panel, in order.
|
||||
fn panel_order(comp : @host.ChandraCompositor) -> Array[String] {
|
||||
comp.shell.loaded_modules().map(m => m.1)
|
||||
}
|
||||
|
||||
///|
|
||||
test "control socket path follows the Wayland socket's name" {
|
||||
assert_eq(@host.control_socket_path("/run/user/1000", "/run/user/1000/wayland-moon-0"), "/run/user/1000/chandra/wayland-moon-0.sock")
|
||||
assert_eq(@host.control_socket_path("/run/user/1000", "wayland-1"), "/run/user/1000/chandra/wayland-1.sock")
|
||||
assert_eq(@host.control_socket_path("", "/tmp/wayland-moon-0"), "")
|
||||
}
|
||||
|
||||
///|
|
||||
test "ping, list and errors over the control socket" {
|
||||
let (comp, path) = control_comp("basic")
|
||||
assert_eq(ask(comp, path, "ping"), ["ok pong"])
|
||||
let list = ask(comp, path, "list")
|
||||
let n = comp.shell.loaded_modules().length()
|
||||
assert_true(n > 3)
|
||||
assert_eq(list.length(), n + 1)
|
||||
assert_eq(list[n], "ok " + n.to_string() + " modules")
|
||||
assert_true(list.iter().any(l => l.contains(" clock-digital ") && l.has_suffix("clock-digital.wasm")))
|
||||
assert_eq(ask(comp, path, "bogus"), ["error unknown command 'bogus' (try: help)"])
|
||||
assert_eq(ask(comp, path, "reload nope"), ["error no loaded module named 'nope'"])
|
||||
assert_eq(ask(comp, path, " "), ["error empty command (try: help)"])
|
||||
assert_eq(ask(comp, path, "help").last(), Some("ok"))
|
||||
// Several commands on one connection get one reply each, in order.
|
||||
let both = ask(comp, path, "ping\nping")
|
||||
assert_eq(both, ["ok pong", "ok pong"])
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "reload every module keeps the panel's order" {
|
||||
let (comp, path) = control_comp("all")
|
||||
let before = panel_order(comp)
|
||||
let slots_before = comp.shell.loaded_modules().map(m => m.0)
|
||||
let reply = ask(comp, path, "reload")
|
||||
assert_eq(reply.length(), before.length() + 1)
|
||||
assert_eq(reply.last(), Some("ok " + before.length().to_string() + " reloaded"))
|
||||
assert_eq(panel_order(comp), before)
|
||||
// Every instance is new.
|
||||
for s in comp.shell.loaded_modules().map(m => m.0) {
|
||||
assert_false(slots_before.contains(s))
|
||||
}
|
||||
// And still draws.
|
||||
ignore(comp.compose_frame())
|
||||
assert_true(rect_of(comp, "clock-digital").w > 0)
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "reload one module by name keeps its place and its settings" {
|
||||
let (comp, path) = control_comp("one")
|
||||
let slot = comp.shell.slot_of("clock-digital")
|
||||
comp.shell.set_module_setting(slot, "week-numbers", "on")
|
||||
let before = panel_order(comp)
|
||||
let others = comp.shell.loaded_modules().filter(m => m.1 != "clock-digital").map(m => m.0)
|
||||
let reply = ask(comp, path, "reload clock-digital")
|
||||
assert_eq(reply.length(), 2)
|
||||
assert_true(reply[0].has_prefix("reloaded w"))
|
||||
assert_eq(reply[1], "ok 1 reloaded")
|
||||
assert_eq(panel_order(comp), before)
|
||||
assert_true(comp.shell.slot_of("clock-digital") != slot)
|
||||
// Only the clock was replaced.
|
||||
assert_eq(comp.shell.loaded_modules().filter(m => m.1 != "clock-digital").map(m => m.0), others)
|
||||
// The new instance got the saved setting: its calendar shows week numbers.
|
||||
ignore(comp.compose_frame())
|
||||
click_module(comp, "clock-digital", 1U)
|
||||
finish_slide(comp)
|
||||
assert_true(cal_texts(comp, "clock-digital").contains("Wk"))
|
||||
// By slot works too.
|
||||
let s2 = comp.shell.slot_of("logout")
|
||||
assert_eq(ask(comp, path, "reload " + s2).last(), Some("ok 1 reloaded"))
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "a module that fails to reload stays loaded and the error is reported" {
|
||||
let (comp, path) = control_comp("fail")
|
||||
let dir = "/tmp/chandra-control-badmod-" + @sys.getpid().to_string()
|
||||
ignore(@sys.mkdir_p(dir))
|
||||
let good = @sys.read_file_bytes("build/components/logout.wasm")
|
||||
assert_true(good.length() > 0)
|
||||
let copy = dir + "/logout.wasm"
|
||||
ignore(@sys.write_file_bytes(copy, good))
|
||||
assert_true(comp.shell.load_module(copy))
|
||||
let before = panel_order(comp)
|
||||
ignore(@sys.write_file_bytes(copy, @utf8.encode("not wasm")))
|
||||
let reply = ask(comp, path, "reload " + copy)
|
||||
assert_eq(reply.length(), 2)
|
||||
assert_true(reply[0].has_prefix("failed w"))
|
||||
assert_true(reply[0].contains("Can't load module"))
|
||||
assert_eq(reply[1], "error 1 of 1 failed to reload")
|
||||
assert_eq(panel_order(comp), before)
|
||||
// The other modules still reload; the result says one failed.
|
||||
let all = ask(comp, path, "reload")
|
||||
assert_eq(all.last(), Some("error 1 of " + before.length().to_string() + " failed to reload"))
|
||||
assert_eq(panel_order(comp), before)
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "a second compositor leaves a live control socket alone" {
|
||||
let (comp, path) = control_comp("busy")
|
||||
let other = new_comp("control-busy2")
|
||||
assert_false(other.start_control(path))
|
||||
assert_eq(ask(comp, path, "ping"), ["ok pong"])
|
||||
other.close()
|
||||
comp.close()
|
||||
// Closing removes the socket; a new compositor can take the path again.
|
||||
let third = new_comp("control-busy3")
|
||||
assert_true(third.start_control(path))
|
||||
third.close()
|
||||
}
|
||||
@@ -288,11 +288,19 @@ fn DesktopShell::ensure_booted(self : DesktopShell) -> Unit {
|
||||
///| Load the module file at `path` into the panel (any number of instances may be loaded).
|
||||
/// None, with a notice, when it is not a plasmoid or the panel has no room for it.
|
||||
pub fn DesktopShell::load_path(self : DesktopShell, path : String) -> Widget? {
|
||||
match @wasm.load(path, self.tokens) {
|
||||
match self.try_load_path(path) {
|
||||
Ok(w) => Some(w)
|
||||
Err(e) => {
|
||||
self.say("Can't load module: " + e)
|
||||
self.say(e)
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| `load_path` without the notice: the widget, or why it could not be loaded.
|
||||
fn DesktopShell::try_load_path(self : DesktopShell, path : String) -> Result[Widget, String] {
|
||||
match @wasm.load(path, self.tokens) {
|
||||
Err(e) => Err("Can't load module: " + e)
|
||||
Ok(pl) => {
|
||||
let id = stem(path)
|
||||
let known = match self.find_module(id) {
|
||||
@@ -320,10 +328,9 @@ pub fn DesktopShell::load_path(self : DesktopShell, path : String) -> Widget? {
|
||||
if self.screen_w > 0 && !self.panel_fits() {
|
||||
self.widgets.retain(x => x.slot != w.slot)
|
||||
pl.unload()
|
||||
self.say("No room in the panel for " + pl.name)
|
||||
None
|
||||
Err("No room in the panel for " + pl.name)
|
||||
} else {
|
||||
Some(w)
|
||||
Ok(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -393,6 +400,20 @@ pub fn DesktopShell::move_slot(self : DesktopShell, slot : String, delta : Int)
|
||||
|
||||
///| Replace the module in `slot` with the module file at `path`, keeping its place in the panel.
|
||||
pub fn DesktopShell::replace_slot(self : DesktopShell, slot : String, path : String) -> Bool {
|
||||
match self.replace_slot_result(slot, path) {
|
||||
Ok(_) => true
|
||||
Err(e) => {
|
||||
if e != "" {
|
||||
self.say(e)
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| `replace_slot` without the notice: the new widget, or why not ("" when there is no `slot`).
|
||||
/// On failure the old module stays loaded where it was.
|
||||
fn DesktopShell::replace_slot_result(self : DesktopShell, slot : String, path : String) -> Result[Widget, String] {
|
||||
let mut idx = -1
|
||||
for i = 0; i < self.widgets.length(); i = i + 1 {
|
||||
if self.widgets[i].slot == slot {
|
||||
@@ -400,26 +421,90 @@ pub fn DesktopShell::replace_slot(self : DesktopShell, slot : String, path : Str
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
return false
|
||||
return Err("")
|
||||
}
|
||||
let old = self.widgets[idx]
|
||||
self.widgets.remove(idx) |> ignore
|
||||
match self.load_path(path) {
|
||||
None => {
|
||||
match self.try_load_path(path) {
|
||||
Err(e) => {
|
||||
self.widgets.insert(idx, old)
|
||||
false
|
||||
Err(e)
|
||||
}
|
||||
Some(nw) => {
|
||||
// load_path appended it; move it into the old module's place, then drop the old one.
|
||||
Ok(nw) => {
|
||||
// try_load_path appended it; move it into the old module's place, then drop the old one.
|
||||
self.widgets.retain(x => x.slot != nw.slot)
|
||||
self.widgets.insert(idx, nw)
|
||||
if self.drag_slot == slot {
|
||||
self.drag_slot = ""
|
||||
self.drag_active = false
|
||||
}
|
||||
old.pl.unload()
|
||||
self.save_panel_config()
|
||||
true
|
||||
Ok(nw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Reload panel modules from disk (after `just install-session` or a module rebuild): each
|
||||
/// loaded module whose module id, file stem, slot or path is `name` (every module when `name` is
|
||||
/// empty) is loaded again from its path and takes the old instance's place in the panel. Its
|
||||
/// settings (state.edn `:module-config`) are applied to the new instance as at login. A module
|
||||
/// that fails to load stays as it was. Returns one line per module ("reloaded w3 clock-digital"
|
||||
/// or "failed w3 clock-digital: why") and whether all of them reloaded; with no module matching
|
||||
/// `name`, a single "no module ..." line and false.
|
||||
pub fn DesktopShell::reload_modules(self : DesktopShell, name : String) -> (Array[String], Bool) {
|
||||
self.ensure_booted()
|
||||
self.scan_catalog() // a module installed since login shows up in Settings > Panel
|
||||
let targets : Array[(String, String, String)] = []
|
||||
for w in self.widgets {
|
||||
if name == "" || w.module_id == name || stem(w.path) == name || w.slot == name || w.path == name {
|
||||
targets.push((w.slot, w.module_id, w.path))
|
||||
}
|
||||
}
|
||||
if targets.length() == 0 {
|
||||
let msg = if name == "" {
|
||||
"no modules are loaded"
|
||||
} else {
|
||||
"no loaded module named '" + name + "'"
|
||||
}
|
||||
return ([msg], false)
|
||||
}
|
||||
let lines : Array[String] = []
|
||||
let mut failed = 0
|
||||
let mut last_err = ""
|
||||
for t in targets {
|
||||
let (slot, id, path) = t
|
||||
match self.replace_slot_result(slot, path) {
|
||||
Ok(nw) => lines.push("reloaded " + nw.slot + " " + id)
|
||||
Err(e) => {
|
||||
failed = failed + 1
|
||||
last_err = id + ": " + (if e == "" { "unloaded meanwhile" } else { e })
|
||||
lines.push("failed " + slot + " " + id + ": " + (if e == "" { "unloaded meanwhile" } else { e }))
|
||||
}
|
||||
}
|
||||
}
|
||||
if failed == 0 {
|
||||
self.say(if targets.length() == 1 {
|
||||
"Reloaded " + targets[0].1
|
||||
} else {
|
||||
"Reloaded " + targets.length().to_string() + " modules"
|
||||
})
|
||||
} else if failed == 1 {
|
||||
self.say("Reload failed: " + last_err)
|
||||
} else {
|
||||
self.say(
|
||||
"Reload: " + failed.to_string() + " of " + targets.length().to_string() + " modules failed (chandra-ctl reload shows why)",
|
||||
)
|
||||
}
|
||||
(lines, failed == 0)
|
||||
}
|
||||
|
||||
///| The loaded modules in panel order: (slot, module id, path).
|
||||
pub fn DesktopShell::loaded_modules(self : DesktopShell) -> Array[(String, String, String)] {
|
||||
self.ensure_booted()
|
||||
self.widgets.map(w => (w.slot, w.module_id, w.path))
|
||||
}
|
||||
|
||||
///| Unload every module (the compositor is closing).
|
||||
pub fn DesktopShell::shutdown(self : DesktopShell) -> Unit {
|
||||
for w in self.widgets {
|
||||
|
||||
@@ -253,6 +253,15 @@ pub fn spawn(cmd : String, display : String) -> Bool {
|
||||
c_spawn(str_to_utf8_bytes(cmd), str_to_utf8_bytes(display)) == 0
|
||||
}
|
||||
|
||||
///|
|
||||
extern "C" fn c_take_sigusr1() -> Int = "mp_take_sigusr1"
|
||||
|
||||
///| True once after the process got SIGUSR1 (reload the panel modules). The first call installs
|
||||
/// the handler; a signal interrupts `wait_readable`, so an idle loop wakes for it.
|
||||
pub fn take_sigusr1() -> Bool {
|
||||
c_take_sigusr1() != 0
|
||||
}
|
||||
|
||||
///|
|
||||
extern "C" fn c_list_desktop(dir : Bytes) -> Bytes = "mp_list_desktop"
|
||||
|
||||
|
||||
@@ -347,6 +347,35 @@ void mp_sleep_ms(int32_t ms) {
|
||||
}
|
||||
}
|
||||
|
||||
/* SIGUSR1: reload the panel modules (compositor/host/control.mbt). The handler only sets a
|
||||
* flag; it is installed without SA_RESTART, so a blocked poll() returns EINTR and the main loop
|
||||
* runs a pass. */
|
||||
static volatile sig_atomic_t mp_sigusr1_seen = 0;
|
||||
static int mp_sigusr1_installed = 0;
|
||||
|
||||
static void mp_on_sigusr1(int sig) {
|
||||
(void)sig;
|
||||
mp_sigusr1_seen = 1;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_take_sigusr1(void) {
|
||||
if (!mp_sigusr1_installed) {
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = mp_on_sigusr1;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
sigaction(SIGUSR1, &sa, NULL);
|
||||
mp_sigusr1_installed = 1;
|
||||
}
|
||||
if (mp_sigusr1_seen) {
|
||||
mp_sigusr1_seen = 0;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_getpid(void) {
|
||||
return (int32_t)getpid();
|
||||
|
||||
@@ -198,6 +198,11 @@ pub fn spawn(cmd : String, display : String) -> Bool {
|
||||
@hostsys.spawn(cmd, display)
|
||||
}
|
||||
|
||||
///| True once after the host process got SIGUSR1 (reload the panel modules).
|
||||
pub fn take_sigusr1() -> Bool {
|
||||
@hostsys.take_sigusr1()
|
||||
}
|
||||
|
||||
///| Names of the `*.desktop` files directly in `dir`.
|
||||
pub fn list_desktop_files(dir : String) -> Array[String] {
|
||||
@hostsys.list_desktop_files(dir)
|
||||
|
||||
Reference in new issue
Block a user