///| The Gamerig Library desktop module and the `game-library` capability (compositor/shell/ /// gamelib.mbt), against a state file standing in for gamerig's library daemon. Needs the module /// built: scripts/build-modules.sh. ///| let gamelib_state_idle : String = #|{"system": {"gpu": "NVIDIA GeForce GTX 1650", "resolution": "1280x720", "display": ":0", #| "audio": "PulseAudio", "audio_ok": true, "hostname": "rig"}, #| "apps": [ #| {"id": "steam", "name": "Steam", "category": "games", "featured": true, "icon": "Play", #| "description": "Your Steam library.", "installed": true, "running": false, #| "modes": [{"id": "default", "label": "Launch"}, {"id": "bigpicture", "label": "Big Picture"}]}, #| {"id": "terminal", "name": "Terminal", "category": "tools", "icon": "Terminal", #| "description": "Interactive shell inside the rig.", "installed": true} #| ]} ///| fn gamelib_file(tag : String, body : String) -> String { let path = "/tmp/chandra-gamelib-" + tag + "-" + @sys.getpid().to_string() + ".json" ignore(@sys.write_file_bytes(path, @sys.str_to_utf8_bytes(body))) path } ///| The desktop module's texts and where each is drawn. fn gamelib_texts(comp : @host.ChandraCompositor) -> Array[(String, Int, Int)] { let out : Array[(String, Int, Int)] = [] for op in comp.shell.slot_ops("desktop") { if op is Text(t) { out.push((t.text, t.origin.x, t.origin.y)) } } out } ///| fn gamelib_has(comp : @host.ChandraCompositor, s : String) -> Bool { gamelib_texts(comp).iter().any(t => t.0 == s) } ///| Click the first text `label` the module drew. fn gamelib_click(comp : @host.ChandraCompositor, label : String, t : UInt) -> Unit { for tx in gamelib_texts(comp) { if tx.0 == label { comp.handle_input(@nested.InputEvent::PointerMove((tx.1 + 4).to_double(), tx.2.to_double()), t) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 2U) ignore(comp.compose_frame()) return } } abort("no " + label) } ///| test "the Gamerig Library shows the daemon's games and rig, and sends launch / stop / focus" { let comp = desk_comp("gamelib") let path = gamelib_file("a", gamelib_state_idle) comp.shell.set_gamelib_base("file://" + path) assert_true(comp.shell.desktop_modules().contains(("gamerig-library", "Gamerig Library"))) assert_true(comp.shell.set_desktop("gamerig-library")) ignore(comp.compose_frame()) ignore(comp.compose_frame()) for want in ["Gamerig", "Steam", "Terminal", "Big Picture", "Rig Status", "Games", "Running Apps"] { assert_true(gamelib_has(comp, want), msg="missing " + want) } // Launch Big Picture, then plain Launch on the first card (Steam, featured). gamelib_click(comp, "Big Picture", 100U) gamelib_click(comp, "Launch", 110U) ignore(comp.compose_frame()) assert_eq(comp.shell.gamelib.sent, ["launch steam bigpicture", "launch steam default"]) // The daemon says it runs now: the card offers Switch to and Stop, the sidebar lists it. ignore(@sys.write_file_bytes(path, @sys.str_to_utf8_bytes(gamelib_state_idle.replace(old="\"running\": false", new="\"running\": true")))) comp.shell.gamelib.next_poll = 0UL ignore(comp.compose_frame()) ignore(comp.compose_frame()) assert_true(gamelib_has(comp, "Switch to")) gamelib_click(comp, "Stop", 120U) gamelib_click(comp, "Switch to", 130U) ignore(comp.compose_frame()) assert_eq(comp.shell.gamelib.sent[2:].to_owned(), ["stop steam default", "focus steam default"]) // The daemon goes away: the library says so. @sys.unlink(path) comp.shell.gamelib.next_poll = 0UL ignore(comp.compose_frame()) ignore(comp.compose_frame()) assert_true(gamelib_texts(comp).iter().any(t => t.0.has_prefix("Library daemon not reachable"))) } ///| test "typing on the focused library filters its cards" { let comp = desk_comp("gamelib-type") let path = gamelib_file("b", gamelib_state_idle) comp.shell.set_gamelib_base("file://" + path) assert_true(comp.shell.set_desktop("gamerig-library")) ignore(comp.compose_frame()) ignore(comp.compose_frame()) // A click on the empty grid gives the desktop the keyboard. comp.handle_input(@nested.InputEvent::PointerMove(900.0, 600.0), 10U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 12U) ignore(comp.compose_frame()) for k in [20U, 18U, 19U, 50U] { // t e r m (evdev codes) comp.handle_input(@nested.InputEvent::Key(k, true), 20U) comp.handle_input(@nested.InputEvent::Key(k, false), 21U) } ignore(comp.compose_frame()) assert_true(gamelib_has(comp, "term")) assert_true(gamelib_has(comp, "Terminal")) assert_false(gamelib_has(comp, "Steam")) @sys.unlink(path) }