Files
chandra/compositor/bin/main.mbt
Claude f556df2f4b
CI / test (push) Successful in 2m25s
Gamerig Library: gamerig's game library as a Chandra desktop module
- New desktop module gamerig-library (components/gamerig-library, logic in lib/gamelib): the
  layout of gamerig's web library (desktop/ui) in draw ops. Top bar with the Gamerig mark, app
  count and search; sidebar with categories and counts, running apps (switch to, stop) and rig
  status (GPU, display, audio); card grid with featured games two columns wide, icon tiles,
  category and status badges, description, install hint, Launch / Big Picture / Switch to / Stop.
  Typing filters, arrows + Enter launch, the wheel scrolls. Colours: follow desktop, dark, light.
- New game-library capability: tables 26-28 (status, games, launch modes), request 45, in the
  wasmer/wasmtime stub, the MoonBit wasmhost and the Rust host-wasm.
- compositor/shell/gamelib.mbt polls GET $GAMERIG_DESKTOP_API/api/state (default
  http://127.0.0.1:8091) every 2 s with curl on a socket and POSTs launch/stop/focus; a file://
  base reads a state file (tests).
- --desktop NAME picks what fills the desktop at start. scripts/gamerig-kiosk.sh now runs a
  hybrid kiosk with the library as the desktop (CHANDRA_KIOSK_MODE=strict for the old one).
- Tests: lib/gamelib, compositor/host/gamelib_test.mbt, a host-wasm test; docs/gamerig-library.md
  with a screenshot.

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

284 lines
8.6 KiB
MoonBit

///| Standalone native MoonBit Wayland compositor entrypoint with wasi-gfx presentation.
///|
fn main {
let args = @env.args()
let mut socket_path = "/tmp/wayland-moon-0"
let mut theme_path = "config/themes/moon-night.json"
let mut gfx_out = ""
let widgets : Array[String] = []
let mut windowed = false
let mut drm = false
let mut x11 = false
let mut fullscreen = false
let mut restore = true
let mut kiosk : String? = None
let mut kiosk_hybrid = false
let mut desktop = ""
let mut app_id = "chandra"
let mut width = 1280
let mut height = 720
let mut i = 1
while i < args.length() {
let a = args[i]
if a == "--socket" && i + 1 < args.length() {
socket_path = args[i + 1]
i = i + 2
} else if a == "--theme" && i + 1 < args.length() {
theme_path = args[i + 1]
i = i + 2
} else if a == "--window" {
windowed = true
i = i + 1
} else if a == "--drm" {
drm = true
i = i + 1
} else if a == "--x11" {
x11 = true
i = i + 1
} else if a == "--fullscreen" {
fullscreen = true
i = i + 1
} else if a == "--kiosk" && i + 1 < args.length() {
kiosk = Some(args[i + 1]) // one fullscreen app, locked down (docs/kiosk.md)
restore = false
i = i + 2
} else if a == "--desktop" && i + 1 < args.length() {
desktop = args[i + 1] // what fills the desktop: a desktop module's id, "wallpaper" or "clock-card"
i = i + 2
} else if a == "--kiosk-hybrid" {
kiosk_hybrid = true // with --kiosk: keep the panel and the desktop, lock only leaving
if kiosk is None {
kiosk = Some("")
}
restore = false
i = i + 1
} else if a == "--app-id" && i + 1 < args.length() {
app_id = args[i + 1] // WM_CLASS of the X11 window
i = i + 2
} else if a == "--no-restore" {
restore = false // don't reopen the apps of the last session
i = i + 1
} else if a == "--widget" && i + 1 < args.length() {
widgets.push(args[i + 1])
i = i + 2
} else if a == "--gfx-out" && i + 1 < args.length() {
gfx_out = args[i + 1]
i = i + 2
} else {
i = i + 1
}
}
let raw_theme = @sys.read_file_bytes(theme_path)
let theme = if raw_theme.length() > 0 {
let sb = StringBuilder()
for j = 0; j < raw_theme.length(); j = j + 1 {
sb.write_char(Int::unsafe_to_char(raw_theme[j].to_int()))
}
@scene.ThemeTokens::from_json_string(sb.to_string())
} else {
@scene.ThemeTokens::moon_night()
}
// Login-session mode: take over the display via DRM/KMS + libinput, and size
// the desktop to the monitor's native mode.
let kms = if drm {
match @kms.Kms::open() {
Some(k) => {
width = k.width
height = k.height
Some(k)
}
None => {
println("failed to start the DRM backend (run from a login VT, not inside another session)")
return
}
}
} else {
None
}
// X11 mode: a window (or the whole screen, without a window manager) on $DISPLAY.
let xwin = if x11 && !drm {
let display = match @env.get_env_var("DISPLAY") {
Some(d) => d
None => ":0"
}
match @x11.X11Window::open(display, "Chandra", app_id, width, height, fullscreen) {
Some(x) => {
width = x.width
height = x.height
Some(x)
}
None => {
println("failed to open a window on the X display " + display)
return
}
}
} else {
None
}
let gfx_surface = @gfx.Surface::new({
width: Some(width),
height: Some(height),
})
let gfx_ctx = @gfx.SurfaceFrameBufferContext::new(gfx_surface)
match @host.ChandraCompositor::new(socket_path, theme, width, height) {
Some(comp) => {
comp.shell.persist = true // remember which modules are loaded in the panel
comp.restore_state() // and the layout mode and recent apps from the last session
comp.shell.screen_w = width // the panel can't place widgets before it knows its width
if widgets.length() > 0 {
ignore(comp.compose_frame()) // boots the panel and sets the theme tokens modules are loaded with
for path in widgets {
ignore(comp.shell.load_path(path))
}
}
if gfx_out.length() > 0 {
let pixmap = comp.present_to_gfx(gfx_ctx)
let png = @renderer.encode_png(pixmap)
ignore(@sys.write_file_bytes(gfx_out, png))
comp.close()
println("wasi-gfx frame written to " + gfx_out)
return
}
let nested = if windowed {
let display = match @env.get_env_var("WAYLAND_DISPLAY") {
Some(d) => d
None => "wayland-0"
}
let rt = match @env.get_env_var("XDG_RUNTIME_DIR") {
Some(d) => d
None => "/tmp"
}
match @nested.NestedWindow::open(display, rt, "chandra", "chandra", width, height) {
Some(n) => Some(n)
None => {
println("failed to open a window on the host Wayland display")
comp.close()
return
}
}
} else {
None
}
match kms {
Some(k) => comp.server.set_keymap(k.keymap)
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)))
match kiosk {
Some(cmd) => comp.start_kiosk(cmd, hybrid=kiosk_hybrid)
None => ()
}
if desktop != "" && !comp.shell.set_desktop(desktop) {
@sys.log("--desktop " + desktop + ": no such desktop module")
}
if drm {
comp.login_session = true
comp.start_auth_agent() // polkit's password prompts (run0, pkexec): Chandra's dialog
}
if restore {
ignore(comp.restore_session()) // reopen the windows the last session ended with
}
while true {
comp.step()
if comp.quit_requested {
break
}
match kms {
Some(k) => {
if !k.poll() {
break
}
let now_ms = @env.now().to_uint()
for ev in k.take_input() {
comp.handle_input(ev, now_ms)
}
k.set_cursor_shape(comp.desired_cursor())
if k.frame_ready() {
k.present(comp.present_to_gfx(gfx_ctx))
}
@sys.sleep_ms(8)
continue
}
None => ()
}
match nested {
Some(n) => {
if !n.poll() {
break
}
if n.keymap.length() > 0 {
comp.server.set_keymap(n.keymap)
}
// Follow the window's size, or the desktop is cropped when the window is smaller.
match n.take_resize() {
Some((nw, nh)) => comp.resize(nw, nh)
None => ()
}
let now_ms = @env.now().to_uint()
for ev in n.take_input() {
comp.handle_input(ev, now_ms)
}
n.set_cursor_shape(comp.desired_cursor())
if n.frame_ready() {
n.present(comp.present_to_gfx(gfx_ctx))
}
}
None =>
match xwin {
Some(x) => {
if !x.poll() {
break
}
if x.keymap.length() > 0 {
comp.server.set_keymap(x.keymap)
}
match x.take_resize() {
Some((nw, nh)) => comp.resize(nw, nh)
None => ()
}
ignore(x.take_redraw()) // every frame is presented here anyway
let now_ms = @env.now().to_uint()
for ev in x.take_input() {
comp.handle_input(ev, now_ms)
}
x.set_cursor_shape(comp.desired_cursor())
if x.frame_ready() {
x.present(comp.present_to_gfx(gfx_ctx))
}
}
None => {
comp.handle_gfx_events(gfx_surface)
ignore(comp.present_to_gfx(gfx_ctx))
}
}
}
@sys.sleep_ms(16)
}
if kiosk is None {
comp.save_session() // what is open now is reopened at the next start
}
match kms {
Some(k) => k.close()
None => ()
}
comp.close()
}
None => {
match kms {
Some(k) => k.close()
None => ()
}
println("failed to bind Wayland socket at " + socket_path)
}
}
}