Gamerig Library: gamerig's game library as a Chandra desktop module
CI / test (push) Successful in 2m25s
CI / test (push) Successful in 2m25s
- 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
This commit is contained in:
57 files changed
+3551
-29
No files matched your search
@@ -0,0 +1,341 @@
|
||||
///| The `game-library` capability (docs/gamerig-library.md): what the Gamerig Library desktop
|
||||
/// module shows. The shell polls gamerig's library daemon (server.py, `GET /api/state` on
|
||||
/// 127.0.0.1:8091; GAMERIG_DESKTOP_API sets another base URL) about every two seconds while a
|
||||
/// module with the capability is loaded, and sends the module's launch / stop / focus back as
|
||||
/// `POST /api/apps/<id>/<action>`. Both run curl on a socket (`spawn_socket`), so the compositor
|
||||
/// never waits on the daemon. A `file://` base reads a state file instead (tests, demos) and only
|
||||
/// records the actions.
|
||||
|
||||
///| How often the daemon is asked, and how soon after an action.
|
||||
let gamelib_poll_ms : UInt64 = 2000UL
|
||||
|
||||
///|
|
||||
let gamelib_after_action_ms : UInt64 = 250UL
|
||||
|
||||
///| An action's error stays in the status this long.
|
||||
let gamelib_error_ms : UInt64 = 8000UL
|
||||
|
||||
///| A curl run in progress: our end of its socket and what it has said.
|
||||
pub(all) struct CurlRun {
|
||||
fd : Int
|
||||
buf : Array[Byte]
|
||||
}
|
||||
|
||||
///| What the shell knows of the library, and the daemon calls in flight.
|
||||
pub(all) struct GameLib {
|
||||
/// Base URL without a trailing slash ("http://127.0.0.1:8091" or "file:///path/state.json").
|
||||
mut base : String
|
||||
/// curl's path ("" when there is none: every poll fails with a message).
|
||||
curl : String
|
||||
mut state : @gamelib.State
|
||||
mut get : CurlRun?
|
||||
mut post : CurlRun?
|
||||
/// Actions waiting for the one in flight: (verb, game, mode).
|
||||
queue : Array[(String, String, String)]
|
||||
/// Actions done against a file:// base, as "verb game mode" (tests read them).
|
||||
sent : Array[String]
|
||||
mut next_poll : UInt64
|
||||
/// What the last action said went wrong, until `error_until`.
|
||||
mut action_error : String
|
||||
mut error_until : UInt64
|
||||
mut dirty : Bool
|
||||
}
|
||||
|
||||
///| curl, where the rig's image has it.
|
||||
fn find_curl() -> String {
|
||||
let env = @sys.getenv("GAMERIG_CURL")
|
||||
if env != "" {
|
||||
return env
|
||||
}
|
||||
for p in ["/usr/bin/curl", "/bin/curl", "/usr/local/bin/curl"] {
|
||||
if @sys.has_exe(p) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
""
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn GameLib::new() -> GameLib {
|
||||
let env = @sys.getenv("GAMERIG_DESKTOP_API")
|
||||
let base = if env != "" { env } else { "http://127.0.0.1:8091" }
|
||||
{
|
||||
base: trim_slash(base),
|
||||
curl: find_curl(),
|
||||
state: { rig: @gamelib.Rig::unknown(), games: [], modes: [] },
|
||||
get: None,
|
||||
post: None,
|
||||
queue: [],
|
||||
sent: [],
|
||||
next_poll: 0UL,
|
||||
action_error: "",
|
||||
error_until: 0UL,
|
||||
dirty: true,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn trim_slash(s : String) -> String {
|
||||
if s.has_suffix("/") {
|
||||
s[:s.length() - 1].to_string()
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
///| Point the library at another daemon (or a state file) and poll it now.
|
||||
pub fn DesktopShell::set_gamelib_base(self : DesktopShell, base : String) -> Unit {
|
||||
self.gamelib.base = trim_slash(base)
|
||||
self.gamelib.next_poll = 0UL
|
||||
self.gamelib.sent.clear()
|
||||
}
|
||||
|
||||
///| A module asked for something (`game-library` launch / stop / focus / refresh).
|
||||
pub fn GameLib::request(self : GameLib, code : Int, game : String, mode : String) -> Unit {
|
||||
let verb = match code {
|
||||
0 => "launch"
|
||||
1 => "stop"
|
||||
2 => "focus"
|
||||
_ => {
|
||||
self.next_poll = 0UL
|
||||
return
|
||||
}
|
||||
}
|
||||
if game == "" || game.contains("/") || game.contains("\n") || mode.contains("\n") {
|
||||
return
|
||||
}
|
||||
self.queue.push((verb, game, if mode == "" { "default" } else { mode }))
|
||||
}
|
||||
|
||||
///| Start curl with `args`; None when it can't run.
|
||||
fn GameLib::curl_run(self : GameLib, args : Array[String]) -> CurlRun? {
|
||||
if self.curl == "" {
|
||||
return None
|
||||
}
|
||||
// argv goes to the child one per line, so the -w newline is curl's own "\n" escape; the daemon
|
||||
// is local, so no proxy. At most 15 arguments (spawn_socket).
|
||||
let argv = [self.curl, "-sS", "--noproxy", "*", "-m5", "-w", "\\n%{http_code}"]
|
||||
argv.append(args)
|
||||
let fd = @sys.spawn_socket(argv)
|
||||
if fd < 0 {
|
||||
None
|
||||
} else {
|
||||
Some({ fd, buf: [] })
|
||||
}
|
||||
}
|
||||
|
||||
///| Read what `run` has said, without waiting. Some((body, http status)) once it is done.
|
||||
fn read_run(run : CurlRun) -> (String, Int)? {
|
||||
let chunk : FixedArray[Byte] = FixedArray::make(8192, b'\x00')
|
||||
let mut eof = false
|
||||
while @sys.poll_read(run.fd, 0) > 0 {
|
||||
let (n, extra) = @sys.recvmsg_fd(run.fd, chunk, chunk.length())
|
||||
if extra >= 0 {
|
||||
@sys.close_fd(extra)
|
||||
}
|
||||
if n == -1 {
|
||||
break
|
||||
}
|
||||
if n <= 0 {
|
||||
eof = true
|
||||
break
|
||||
}
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
run.buf.push(chunk[i])
|
||||
}
|
||||
if run.buf.length() > 4 * 1024 * 1024 {
|
||||
eof = true // a runaway answer: stop reading
|
||||
break
|
||||
}
|
||||
}
|
||||
if !eof {
|
||||
return None
|
||||
}
|
||||
@sys.close_fd(run.fd)
|
||||
let text = @utf8.decode_lossy(Bytes::from_array(run.buf))
|
||||
// curl -w appended "\n<status>"; 000 when it couldn't connect.
|
||||
match text.rev_find("\n") {
|
||||
Some(i) => {
|
||||
let code = @gamelib.parse_int(text[i + 1:].to_string())
|
||||
Some((text[:i].to_string(), code))
|
||||
}
|
||||
None => Some((text, 0))
|
||||
}
|
||||
}
|
||||
|
||||
///| Take a new state (or say why there is none).
|
||||
fn GameLib::take(self : GameLib, body : String, code : Int, now : UInt64) -> Unit {
|
||||
let err = if code == 200 {
|
||||
match @gamelib.parse_state(body) {
|
||||
Ok(s) => {
|
||||
self.state = s
|
||||
""
|
||||
}
|
||||
Err(e) => e
|
||||
}
|
||||
} else if code == 0 {
|
||||
"Library daemon not reachable at " + self.base
|
||||
} else {
|
||||
"Library daemon answered HTTP " + code.to_string() + ": " + @gamelib.error_message(body)
|
||||
}
|
||||
let rig = self.state.rig
|
||||
let rig = if err != "" {
|
||||
{ ..rig, connected: false, error: err }
|
||||
} else if now < self.error_until {
|
||||
{ ..rig, connected: true, error: self.action_error }
|
||||
} else {
|
||||
{ ..rig, connected: true, error: "" }
|
||||
}
|
||||
if rig != self.state.rig || err == "" {
|
||||
self.state = { ..self.state, rig, }
|
||||
self.dirty = true
|
||||
}
|
||||
}
|
||||
|
||||
///| One step: collect answers, start the next poll or action when due. Call it before modules
|
||||
/// read the tables.
|
||||
pub fn GameLib::poll(self : GameLib, now : UInt64) -> Unit {
|
||||
let before = self.state
|
||||
// The poll in flight.
|
||||
match self.get {
|
||||
Some(run) =>
|
||||
match read_run(run) {
|
||||
Some((body, code)) => {
|
||||
self.get = None
|
||||
self.take(body, code, now)
|
||||
self.next_poll = now + gamelib_poll_ms
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
// The action in flight.
|
||||
match self.post {
|
||||
Some(run) =>
|
||||
match read_run(run) {
|
||||
Some((body, code)) => {
|
||||
self.post = None
|
||||
if code < 200 || code >= 300 {
|
||||
self.action_error = if code == 0 {
|
||||
"Library daemon not reachable at " + self.base
|
||||
} else {
|
||||
@gamelib.error_message(body)
|
||||
}
|
||||
self.error_until = now + gamelib_error_ms
|
||||
}
|
||||
self.next_poll = now + gamelib_after_action_ms
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
// Next action.
|
||||
if self.post is None && self.queue.length() > 0 {
|
||||
let (verb, game, mode) = self.queue.remove(0)
|
||||
if self.base.has_prefix("file://") {
|
||||
self.sent.push(verb + " " + game + " " + mode)
|
||||
self.next_poll = 0UL
|
||||
} else {
|
||||
let url = self.base + "/api/apps/" + game + "/" + verb
|
||||
let body = if verb == "launch" { "{\"mode\":\"" + mode.replace_all(old="\"", new="") + "\"}" } else { "{}" }
|
||||
match self.curl_run(["-H", "Content-Type: application/json", "--data-binary", body, url]) {
|
||||
Some(run) => self.post = Some(run)
|
||||
None => {
|
||||
self.action_error = "curl is not installed: can't reach the library daemon"
|
||||
self.error_until = now + gamelib_error_ms
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Next poll.
|
||||
if self.get is None && now >= self.next_poll {
|
||||
self.next_poll = now + gamelib_poll_ms
|
||||
if self.base.has_prefix("file://") {
|
||||
let path = self.base[7:].to_string()
|
||||
let bytes = @sys.read_file_bytes(path)
|
||||
if bytes.length() == 0 {
|
||||
self.take("", 0, now)
|
||||
} else {
|
||||
self.take(@utf8.decode_lossy(bytes), 200, now)
|
||||
}
|
||||
} else {
|
||||
match self.curl_run([self.base + "/api/state"]) {
|
||||
Some(run) => self.get = Some(run)
|
||||
None => self.take("", 0, now)
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.state != before {
|
||||
self.dirty = true
|
||||
}
|
||||
}
|
||||
|
||||
///| A daemon call is in flight (the main loop keeps its pace so the answer shows soon).
|
||||
pub fn GameLib::busy(self : GameLib) -> Bool {
|
||||
self.get is Some(_) || self.post is Some(_) || self.queue.length() > 0
|
||||
}
|
||||
|
||||
///| Milliseconds until the next poll is due, -1 when no module uses the library.
|
||||
pub fn DesktopShell::gamelib_wake_ms(self : DesktopShell, now : UInt64) -> Int {
|
||||
if !self.any_cap("game-library") {
|
||||
return -1
|
||||
}
|
||||
if self.gamelib.next_poll <= now {
|
||||
0
|
||||
} else {
|
||||
(self.gamelib.next_poll - now).to_int()
|
||||
}
|
||||
}
|
||||
|
||||
///| Whether a library daemon call is in flight.
|
||||
pub fn DesktopShell::gamelib_busy(self : DesktopShell) -> Bool {
|
||||
self.any_cap("game-library") && self.gamelib.busy()
|
||||
}
|
||||
|
||||
///| Publish tables 26-28 for modules with the capability (and redraw the desktop on news).
|
||||
fn DesktopShell::publish_gamelib(self : DesktopShell) -> Unit {
|
||||
if !self.any_cap("game-library") {
|
||||
return
|
||||
}
|
||||
let g = self.gamelib
|
||||
g.poll(@sys.monotonic_ms())
|
||||
if !g.dirty {
|
||||
return
|
||||
}
|
||||
g.dirty = false
|
||||
let r = g.state.rig
|
||||
let st = @wasm.Table::new()
|
||||
st.str(r.error)
|
||||
st.str(r.gpu)
|
||||
st.str(r.resolution)
|
||||
st.str(r.display)
|
||||
st.str(r.audio)
|
||||
st.str(r.hostname)
|
||||
st.int(if r.connected { 1 } else { 0 })
|
||||
st.int(if r.audio_ok { 1 } else { 0 })
|
||||
st.end_row()
|
||||
@wasm.publish(GameStatus, st)
|
||||
let gt = @wasm.Table::new()
|
||||
for x in g.state.games {
|
||||
gt.str(x.id)
|
||||
gt.str(x.name)
|
||||
gt.str(x.category)
|
||||
gt.str(x.description)
|
||||
gt.str(x.icon)
|
||||
gt.str(x.install_hint)
|
||||
gt.int(if x.featured { 1 } else { 0 })
|
||||
gt.int(if x.installed { 1 } else { 0 })
|
||||
gt.int(if x.running { 1 } else { 0 })
|
||||
gt.end_row()
|
||||
}
|
||||
@wasm.publish(Games, gt)
|
||||
let mt = @wasm.Table::new()
|
||||
for m in g.state.modes {
|
||||
mt.str(m.game)
|
||||
mt.str(m.id)
|
||||
mt.str(m.label)
|
||||
mt.end_row()
|
||||
}
|
||||
@wasm.publish(GameModes, mt)
|
||||
self.desk_dirty = true
|
||||
}
|
||||
Reference in new issue
Block a user