From 5c26218595a7612c6c53daf4dd03b222ea1926c9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 1 Nov 2026 00:21:34 -0700 Subject: [PATCH 1/4] Lab channel: start the console's inference threads for each command Game commands that watch the screen with run_until (frlg_route's walker) crashed the app on a null inference pivot: a program's environment starts those threads, the lab console did not. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp index 784f07ae5e..bec329f912 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp @@ -29,6 +29,9 @@ class LabConsole{ : m_console(system_of(panel)) , m_context(scope, m_console.controller()) { + // Commands that watch the screen (run_until) need the inference + // pivots a program's environment would have started. + m_console.initialize_inference_threads(scope); if (!m_context->is_ready()){ throw ParseException("Controller is not ready."); } From ba405cfc63214662641fd275d1498330d43bfb37 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 1 Nov 2026 15:11:42 -0800 Subject: [PATCH 2/4] Lab channel: popups lists the app's message boxes, popups close presses their default button A failed program leaves a modal Error box that nothing at the keyboard closes on a lab console. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/CommonFramework/Windows/Lab.md | 3 +- .../CommonFramework/Windows/LabChannel.cpp | 40 ++++++++++++++++++- .../CommonFramework/Windows/LabChannel.h | 1 + 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/Windows/Lab.md b/SerialPrograms/Source/CommonFramework/Windows/Lab.md index 86172c1fde..489c47a596 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/Lab.md +++ b/SerialPrograms/Source/CommonFramework/Windows/Lab.md @@ -80,6 +80,7 @@ error= | `stop` | What clicking Stop Program does. Refused unless the program is `RUNNING`. The request goes on; the program stops in the background. | | `state` | Prints the state file's lines into the journal. (In the Command File Runner program, `state [image]` prints the detectors that fire instead; from the channel use `detect` per detector.) | | `keepawake on\|off [seconds]` | While it is on and no program runs, presses B every `seconds` (default 15) so the console does not sleep. A running program pauses it. Off by default; off again when the app restarts. | +| `popups [close]` | Lists the app's open message boxes (title and text), such as the "Error" box a failed program leaves. `close` presses each one's default button. | | `owner claim TOKEN`, `owner release` | The owner lock, below. | | `help` | Prints every command and detector, including the ones games add. | @@ -136,7 +137,7 @@ Games add their own commands and detectors with Two scripts driving one console would interleave their presses. A script claims the channel with `owner claim TOKEN`; from then on every request that -changes anything (`select`, `set`, `start`, `stop`, `keepawake`, presses) +changes anything (`select`, `set`, `start`, `stop`, `keepawake`, `popups`, presses) must start with the line `owner TOKEN`, or it is refused with `owned by TOKEN`. Requests that only read (`state`, `help`, `shot`, `detect`, ...) need no token. `owner release`, with the owner line, frees the channel. diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp index 1a1651dff6..ad1d1c610b 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp +++ b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp @@ -6,7 +6,9 @@ #include #include +#include #include +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/ScopeExit.h" @@ -320,7 +322,7 @@ std::string LabChannel::run_line(const LabRequest& request, const LabRequestLine " start | stop\n" " state\n" " keepawake on|off [seconds]\n" - " help\n"; + " popups [close] (the app's message boxes; close presses their default button)\n help\n"; m_console->help(output.out); return ""; } @@ -336,7 +338,7 @@ std::string LabChannel::run_line(const LabRequest& request, const LabRequestLine return error; } } - if (command == "select" || command == "set" || command == "start" || command == "stop"){ + if (command == "select" || command == "set" || command == "start" || command == "stop" || command == "popups"){ return run_ui_command(line, output.out); } if (command == "keepawake"){ @@ -401,6 +403,9 @@ std::string LabChannel::run_on_console(const std::function& words = line.words; const std::string& command = words[0]; + if (command == "popups"){ + return run_popups(words, out); + } if (command == "select"){ if (words.size() < 2){ return "select: expected select GAME:PROGRAM"; @@ -441,6 +446,37 @@ std::string LabChannel::run_ui_command(const LabRequestLine& line, std::ostream& // Runs `body` on the UI thread and returns what it returns: "" or an error. // Gives up with an error if the app is closing, since the UI thread would // never run it. +// A failed program leaves a modal "Error" box, and nothing at the +// keyboard closes it on a lab console. "popups" lists the app's open +// message boxes; "popups close" presses each one's default button. +std::string LabChannel::run_popups(const std::vector& words, std::ostream& out){ + const bool close = words.size() >= 2 && words[1] == "close"; + if (words.size() > 2 || (words.size() == 2 && !close)){ + return "popups: expected popups, or popups close"; + } + return run_on_ui([&]{ + size_t count = 0; + for (QWidget* widget : QApplication::topLevelWidgets()){ + QMessageBox* box = qobject_cast(widget); + if (box == nullptr || !box->isVisible()){ + continue; + } + count++; + out << "popup: " << box->windowTitle().toStdString() << ": " << box->text().toStdString() << "\n"; + if (close){ + QAbstractButton* button = box->defaultButton(); + if (button != nullptr){ + button->click(); + }else{ + box->done(0); + } + } + } + out << count << (close ? " closed" : " open") << "\n"; + return std::string(); + }); +} + std::string LabChannel::run_on_ui(const std::function& body){ struct Call{ Mutex lock; diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h index c94e171035..2a6b05f97f 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h +++ b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h @@ -123,6 +123,7 @@ class LabChannel{ std::string run_console_command(const std::vector& words, LabCommandOutput& output); std::string run_on_console(const std::function& body); std::string run_ui_command(const LabRequestLine& line, std::ostream& out); + std::string run_popups(const std::vector& words, std::ostream& out); std::string run_on_ui(const std::function& body); void keep_awake(); void write_state(); From 6c5451a08f0bdcbee54f04058d0bded84f278afd Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 1 Nov 2026 15:15:17 -0800 Subject: [PATCH 3/4] Lab channel: popups compiles (QPushButton include) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp index ad1d1c610b..a68950f800 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp +++ b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/ScopeExit.h" @@ -464,7 +465,7 @@ std::string LabChannel::run_popups(const std::vector& words, std::o count++; out << "popup: " << box->windowTitle().toStdString() << ": " << box->text().toStdString() << "\n"; if (close){ - QAbstractButton* button = box->defaultButton(); + QPushButton* button = box->defaultButton(); if (button != nullptr){ button->click(); }else{ From 2b3eac8c6e2c5719abf09e3d80e363d4bd57588f Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 2 Nov 2026 14:05:39 -0800 Subject: [PATCH 4/4] Lab: start waits for the controller; state shows controller= After a program switch the app reconnects the controller, which can stall. A start in that window ran the program, which then failed with "Incompatible Controller ... Actual: (none)". The state file now has controller=ready|none from the panel's ControllerSession, and start waits up to SECONDS (default 30) for it, refusing with "controller not connected". labctl.sh passes a wait shorter than its own request timeout. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/CommonFramework/Windows/Lab.md | 11 +++- .../CommonFramework/Windows/LabChannel.cpp | 47 +++++++++++++- .../CommonFramework/Windows/LabChannel.h | 8 +++ .../Windows/LabChannel_Tests.cpp | 64 +++++++++++++++++-- .../CommonFramework/Windows/LabRequest.cpp | 40 ++++++++++-- .../CommonFramework/Windows/LabRequest.h | 20 ++++++ .../NintendoSwitch_LabCommands.cpp | 10 +++ .../NintendoSwitch_LabCommands.h | 1 + devtools/lab/README.md | 3 +- devtools/lab/labctl.sh | 16 ++++- 10 files changed, 200 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/Windows/Lab.md b/SerialPrograms/Source/CommonFramework/Windows/Lab.md index 489c47a596..37ccad0e5e 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/Lab.md +++ b/SerialPrograms/Source/CommonFramework/Windows/Lab.md @@ -56,6 +56,7 @@ START_SEGMENT = "0"` or the path of a saved screenshot. seq=r3 state=RUNNING program=PokemonBDSP:AutoStory +controller=ready owner=lab-B error= ``` @@ -65,6 +66,9 @@ error= while the panel loads, `NO_PROGRAM` when the selected panel is not a program (the settings page). - `program` is the selected program's identifier (`:`). +- `controller` is `ready` when the console's controller can take commands, + `none` while it is not connected (after a program switch the app + reconnects it, which can stall), empty for a panel without a console. - `owner` is the owner token, empty when the channel is unclaimed. - `error` is the error of the last request, empty once a request succeeds. @@ -76,7 +80,7 @@ error= |---|---| | `select GAME:PROGRAM` | Shows that program, as clicking it in the program list does. The identifier is the one in `state` and in the settings file's `99-Panels` section, e.g. `PokemonBDSP:AutoStory`, `NintendoSwitch:CommandFileRunner`. Refused while a program runs. | | `set KEY JSON` | Sets one option of the selected program by its key in the settings file, live: `set START_SEGMENT "0"`, `set NEW_GAME_INTRO true`, `set NOTIFICATIONS.Enabled false` (dots walk into groups). The value is JSON, so strings need quotes. Prints `set KEY = `, which differs from what was sent when the program clamped or refused it. Refused while the program runs. | -| `start` | What clicking Start Program does. Refused unless the program is `STOPPED`. | +| `start [SECONDS]` | What clicking Start Program does. Refused unless the program is `STOPPED`. First waits up to `SECONDS` (default 30) for `controller=ready`, and refuses with `controller not connected` if it never is, instead of starting a program that would fail with "Incompatible Controller". | | `stop` | What clicking Stop Program does. Refused unless the program is `RUNNING`. The request goes on; the program stops in the background. | | `state` | Prints the state file's lines into the journal. (In the Command File Runner program, `state [image]` prints the detectors that fire instead; from the channel use `detect` per detector.) | | `keepawake on\|off [seconds]` | While it is on and no program runs, presses B every `seconds` (default 15) so the console does not sleep. A running program pauses it. Off by default; off again when the app restarts. | @@ -197,6 +201,7 @@ The state file while it runs, and after: seq=r2 seq=r2 state=RUNNING state=STOPPED program=PokemonBDSP:AutoStory +controller=ready owner=lab-B error= ``` @@ -262,13 +267,13 @@ point its folder at `Lab/`: the channel reads that one. The `LabControl/` folder of earlier lab builds (files `start` and `stop`, a two-line `state`) is gone. `start` and `stop` are lines in a request, and -`state` has the five lines above. Scripts move from +`state` has the six lines above. Scripts move from `printf '' > LabControl/start` to a request file containing `start`. ## Tests `add_tests_LabChannel()` covers the request format, the journal lines, the -state text, the owner lock and the keep-awake schedule (`LabChannel - ...` +state text, the owner lock, the keep-awake schedule and the controller wait before `start` (`LabChannel - ...` unit tests); `add_tests_CommandFileRunner()` covers the console commands' arguments. Run them with the app's `--command-line-test-mode`. Selecting, setting, starting and the presses need a live app and console. diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp index a68950f800..386725debf 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp +++ b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.cpp @@ -320,7 +320,7 @@ std::string LabChannel::run_line(const LabRequest& request, const LabRequestLine " owner claim TOKEN | owner release (then start with a line \"owner TOKEN\")\n" " select GAME:PROGRAM\n" " set OPTION_KEY JSON\n" - " start | stop\n" + " start [SECONDS] | stop (start waits up to SECONDS, default 30, for the controller)\n" " state\n" " keepawake on|off [seconds]\n" " popups [close] (the app's message boxes; close presses their default button)\n help\n"; @@ -438,9 +438,45 @@ std::string LabChannel::run_ui_command(const LabRequestLine& line, std::ostream& } return error; } - const bool start = command == "start"; + if (command == "start"){ + return run_start(words); + } + return run_on_ui([&]{ + return start_or_stop(m_callbacks.current_panel(), false); + }); +} + +// "start [SECONDS]": waits for the controller, then starts. The wait runs +// here on the worker, so the window stays responsive while it reconnects. +std::string LabChannel::run_start(const std::vector& words){ + Seconds timeout = CONTROLLER_WAIT_DEFAULT; + if (words.size() > 2){ + return "start: expected start [SECONDS]"; + } + if (words.size() == 2){ + const std::optional seconds = parse_seconds(words[1]); + if (!seconds){ + return "start: seconds must be a whole number of at least 1, got " + words[1]; + } + timeout = *seconds; + } + const std::string error = wait_for_controller( + [this]{ + std::lock_guard lg(m_panel_lock); + return read_controller_status(); + }, + [this](Milliseconds slice){ + std::unique_lock lg(m_idle_lock); + m_idle_cv.wait_for(lg, slice); + return !m_stopping.load(std::memory_order_acquire); + }, + timeout, POLL_INTERVAL + ); + if (!error.empty()){ + return error; + } return run_on_ui([&]{ - return start_or_stop(m_callbacks.current_panel(), start); + return start_or_stop(m_callbacks.current_panel(), true); }); } @@ -547,11 +583,16 @@ void LabChannel::read_program_state(std::string& state, std::string& program, bo running = current != ProgramState::STOPPED; } +std::string LabChannel::read_controller_status(){ + return m_panel == nullptr ? "" : m_console->controller_status(*m_panel); +} + void LabChannel::write_state(){ { std::lock_guard lg(m_panel_lock); bool running; read_program_state(m_state.state, m_state.program, running); + m_state.controller = read_controller_status(); } m_state.owner = m_owner.token(); std::string text = lab_state_text(m_state); diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h index 2a6b05f97f..6ae6e77b6e 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h +++ b/SerialPrograms/Source/CommonFramework/Windows/LabChannel.h @@ -85,6 +85,11 @@ class LabConsoleCommands{ const std::vector& words, LabCommandOutput& output ) = 0; + // "ready" when the console's controller can take commands, "none" when + // it can't (not connected yet, or reconnecting after a program switch), + // "" when `panel` has no console. Read for the state file and by "start". + virtual std::string controller_status(PanelSession& panel) = 0; + // The press that keeps the console awake ("keepawake on"). virtual void keep_awake(PanelSession& panel, CancellableScope& scope) = 0; @@ -123,6 +128,7 @@ class LabChannel{ std::string run_console_command(const std::vector& words, LabCommandOutput& output); std::string run_on_console(const std::function& body); std::string run_ui_command(const LabRequestLine& line, std::ostream& out); + std::string run_start(const std::vector& words); std::string run_popups(const std::vector& words, std::ostream& out); std::string run_on_ui(const std::function& body); void keep_awake(); @@ -130,6 +136,8 @@ class LabChannel{ // The state of the selected program. Call under m_panel_lock. void read_program_state(std::string& state, std::string& program, bool& running) const; + // The selected panel's LabConsoleCommands::controller_status(). Call under m_panel_lock. + std::string read_controller_status(); private: diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabChannel_Tests.cpp b/SerialPrograms/Source/CommonFramework/Windows/LabChannel_Tests.cpp index 7d11dcf8f2..c05c65cefe 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabChannel_Tests.cpp +++ b/SerialPrograms/Source/CommonFramework/Windows/LabChannel_Tests.cpp @@ -3,7 +3,8 @@ * From: https://github.com/PokemonAutomation/ * * The channel's text formats, with no app: request files, the journal, the - * "state" file, the owner lock and the keep-awake schedule. + * "state" file, the owner lock, the keep-awake schedule and the wait for + * the controller before "start". * */ @@ -139,12 +140,12 @@ UnitTestResult test_journal(){ } UnitTestResult test_state_text(){ - LabState state{"r1", "RUNNING", "PokemonBDSP:AutoStory", "lab-B", ""}; - if (lab_state_text(state) != "seq=r1\nstate=RUNNING\nprogram=PokemonBDSP:AutoStory\nowner=lab-B\nerror=\n"){ + LabState state{"r1", "RUNNING", "PokemonBDSP:AutoStory", "ready", "lab-B", ""}; + if (lab_state_text(state) != "seq=r1\nstate=RUNNING\nprogram=PokemonBDSP:AutoStory\ncontroller=ready\nowner=lab-B\nerror=\n"){ return "Unexpected state text: " + lab_state_text(state); } - state = {"", "NO_PROGRAM", "", "", "The selected panel is not a program."}; - if (lab_state_text(state) != "seq=\nstate=NO_PROGRAM\nprogram=\nowner=\nerror=The selected panel is not a program.\n"){ + state = {"", "NO_PROGRAM", "", "", "", "The selected panel is not a program."}; + if (lab_state_text(state) != "seq=\nstate=NO_PROGRAM\nprogram=\ncontroller=\nowner=\nerror=The selected panel is not a program.\n"){ return "Unexpected state text: " + lab_state_text(state); } return true; @@ -226,6 +227,58 @@ UnitTestResult test_keep_awake(){ return true; } +// A controller that reads "none" for `not_ready` checks, then `then`, with +// a fake sleep that only counts. +struct FakeController{ + FakeController(int p_not_ready, std::string p_then, bool p_closing = false) + : not_ready(p_not_ready), then(std::move(p_then)), closing(p_closing) + {} + + int not_ready; + std::string then; + bool closing; + + std::string error; + Milliseconds slept = 0ms; + + FakeController& wait(Milliseconds timeout){ + int checks = 0; + error = wait_for_controller( + [&]{ return checks++ < not_ready ? std::string("none") : then; }, + [&](Milliseconds slice){ slept += slice; return !closing; }, + timeout, 500ms + ); + return *this; + } +}; + +UnitTestResult test_controller_wait(){ + FakeController ready(0, "ready"); + if (!ready.wait(30s).error.empty() || ready.slept != 0ms){ + return "A ready controller starts at once."; + } + FakeController reconnecting(4, "ready"); + if (!reconnecting.wait(30s).error.empty() || reconnecting.slept != 2000ms){ + return std::format("A reconnecting controller is waited for; slept {} ms.", reconnecting.slept.count()); + } + FakeController no_console(0, ""); + if (!no_console.wait(30s).error.empty()){ + return "A panel without a console does not wait."; + } + FakeController never(1000, "ready"); + if (never.wait(3s).error != "controller not connected" || never.slept != 3000ms){ + return std::format("A controller that never connects refuses the start after the timeout; slept {} ms.", never.slept.count()); + } + FakeController closing(1000, "ready", true); + if (closing.wait(30s).error != "The app is closing."){ + return "A closing app stops the wait."; + } + if (parse_seconds("30") != 30s || parse_seconds("0") || parse_seconds("soon") || parse_seconds("")){ + return "parse_seconds() takes whole seconds of at least 1."; + } + return true; +} + class Test_LabChannel : public UnitTest{ public: @@ -253,6 +306,7 @@ void add_tests_LabChannel(UnitTestDatabase& database){ database.add("state text", test_state_text); database.add("owner lock", test_owner); database.add("keep-awake schedule", test_keep_awake); + database.add("controller wait", test_controller_wait); } diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabRequest.cpp b/SerialPrograms/Source/CommonFramework/Windows/LabRequest.cpp index c8dac21a11..4d4ffafc2c 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabRequest.cpp +++ b/SerialPrograms/Source/CommonFramework/Windows/LabRequest.cpp @@ -65,6 +65,17 @@ std::string line_after_words(const std::string& line, size_t count){ return text.substr(begin, end + 1 - begin); } +std::optional parse_seconds(const std::string& word){ + const char* begin = word.c_str(); + char* end = nullptr; + errno = 0; + const long seconds = std::strtol(begin, &end, 10); + if (end == begin || *end != '\0' || errno == ERANGE || seconds < 1){ + return std::nullopt; + } + return Seconds(seconds); +} + std::optional next_request_file(const std::string& inbox){ std::optional next; for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(inbox))){ @@ -118,11 +129,31 @@ std::string lab_state_text(const LabState& state){ "seq=" + state.seq + "\n" + "state=" + state.state + "\n" + "program=" + state.program + "\n" + + "controller=" + state.controller + "\n" + "owner=" + state.owner + "\n" + "error=" + state.error + "\n"; } +std::string wait_for_controller( + const std::function& status, + const std::function& sleep, + Milliseconds timeout, Milliseconds slice +){ + Milliseconds waited(0); + while (status() == "none"){ + if (waited >= timeout){ + return "controller not connected"; + } + if (!sleep(slice)){ + return "The app is closing."; + } + waited += slice; + } + return ""; +} + + std::string LabOwner::claim(const std::string& request_owner, const std::string& token){ if (token.empty()){ return "owner claim: missing token"; @@ -157,14 +188,11 @@ std::string KeepAwakeSchedule::set(const std::vector& words, WallCl } Seconds interval = DEFAULT_INTERVAL; if (words.size() > 2){ - const char* begin = words[2].c_str(); - char* end = nullptr; - errno = 0; - const long seconds = std::strtol(begin, &end, 10); - if (end == begin || *end != '\0' || errno == ERANGE || seconds < 1){ + const std::optional seconds = parse_seconds(words[2]); + if (!seconds){ return "keepawake: seconds must be a whole number of at least 1, got " + words[2]; } - interval = Seconds(seconds); + interval = *seconds; } m_on = words[1] == "on"; m_interval = interval; diff --git a/SerialPrograms/Source/CommonFramework/Windows/LabRequest.h b/SerialPrograms/Source/CommonFramework/Windows/LabRequest.h index 52958b859c..11b7fd7650 100644 --- a/SerialPrograms/Source/CommonFramework/Windows/LabRequest.h +++ b/SerialPrograms/Source/CommonFramework/Windows/LabRequest.h @@ -11,6 +11,7 @@ #ifndef PokemonAutomation_LabRequest_H #define PokemonAutomation_LabRequest_H +#include #include #include #include @@ -33,6 +34,9 @@ std::vector split_command_line(const std::string& line); // the surrounding spaces: the JSON of "set KEY JSON", spaces and '#' intact. std::string line_after_words(const std::string& line, size_t count); +// A whole number of seconds, at least 1, as written on a command line. +std::optional parse_seconds(const std::string& word); + // The ".txt" file in `inbox` whose name sorts first, if any. std::optional next_request_file(const std::string& inbox); @@ -63,12 +67,28 @@ struct LabState{ std::string seq; // Name of the last request handled. std::string state; // STOPPED, RUNNING, STOPPING, NOT_READY or NO_PROGRAM. std::string program; // The selected program's identifier, or "". + std::string controller; // "ready", "none", or "" for a panel without a console. std::string owner; // The owner token, or "". std::string error; // The last error; "" once a request succeeds. }; std::string lab_state_text(const LabState& state); +// "start [SECONDS]" first waits up to SECONDS for the panel's controller: +// after a program switch the app reconnects it, and a program started +// before it is ready fails with "Incompatible Controller". +constexpr Seconds CONTROLLER_WAIT_DEFAULT = Seconds(30); + +// Calls `status` (a LabState::controller value) until it is not "none", +// sleeping `slice` through `sleep` in between; `sleep` returns false when +// the app is closing. Returns an error message, or "". +std::string wait_for_controller( + const std::function& status, + const std::function& sleep, + Milliseconds timeout, Milliseconds slice +); + + // The owner lock: once claimed, requests that drive the console must carry // the token. The lock lives in memory, so it dies with the app. class LabOwner{ diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp index bec329f912..f62830fe73 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.cpp @@ -92,6 +92,16 @@ void SwitchLabCommands::run( } } +std::string SwitchLabCommands::controller_status(PanelSession& panel){ + GameConsole::ConsoleProgramSession* program = dynamic_cast(&panel); + if (program == nullptr){ + return ""; + } + // Controller 0, the one LabConsole and the program's ConsoleHandle use. + GameConsole::ConsoleSystemSession& system = program->system(); + return system.controllers() > 0 && system.controller(0).ready() ? "ready" : "none"; +} + void SwitchLabCommands::keep_awake(PanelSession& panel, CancellableScope& scope){ LabConsole console(panel, scope); pbf_press_button(console.context(), BUTTON_B, 80ms, 0ms); diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.h b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.h index 3b2d46a11f..5642484cf3 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_LabCommands.h @@ -24,6 +24,7 @@ class SwitchLabCommands : public LabConsoleCommands{ PanelSession& panel, CancellableScope& scope, const std::vector& words, LabCommandOutput& output ) override; + virtual std::string controller_status(PanelSession& panel) override; virtual void keep_awake(PanelSession& panel, CancellableScope& scope) override; virtual void help(std::ostream& out) override; }; diff --git a/devtools/lab/README.md b/devtools/lab/README.md index a065bfea88..3cf6280c0c 100644 --- a/devtools/lab/README.md +++ b/devtools/lab/README.md @@ -10,7 +10,7 @@ request is a text file in `inbox/`, its journal appears in `outbox/`, and | Script | Does | Parameters | |---|---|---| | `lablock.sh` | One user of a console at a time: `run [-w] NAME -- CMD...`, `check`, `status`. | `-c CONSOLE` or `LAB_CONSOLE` (default `default`); `LAB_DIR` for the lock folder (default `~/.lab`); `LABLOCK_TOKEN` passed to the command. | -| `labctl.sh` | One channel command per call: `state`, `start`, `stop`, `select ID`, `set KEY JSON`, `owner claim TOKEN`, `owner release`, `keepawake on\|off [S]`, `send NAME` (any request from stdin); `supports PATH` tells whether a build has the channel. | `-p PROFILE` or `LAB_PROFILE`; `LAB_OWNER` for the owner line; `LABCTL_WAIT` seconds per request and for start/stop to settle (default 60). | +| `labctl.sh` | One channel command per call: `state`, `start`, `stop`, `select ID`, `set KEY JSON`, `owner claim TOKEN`, `owner release`, `keepawake on\|off [S]`, `send NAME` (any request from stdin); `supports PATH` tells whether a build has the channel. | `-p PROFILE` or `LAB_PROFILE`; `LAB_OWNER` for the owner line; `LABCTL_WAIT` seconds per request and for start/stop to settle (default 60). `start` refuses with `controller not connected` when the app's controller never becomes ready (`controller=` in `state`). | | `cfrun.sh` | Send one batch of console commands (stdin) under the console's lock and print its journal. | `-p PROFILE` or `LAB_PROFILE`; `LAB_OWNER`; `CFRUN_WAIT` seconds per batch (default 600). | Each script's header comment has the full usage and exit codes. @@ -100,6 +100,7 @@ sh labctl.sh state # seq=labctl-4242-1762000000 # state=STOPPED # program=PokemonBDSP:AutoStory +# controller=ready # owner=lab-B # error= diff --git a/devtools/lab/labctl.sh b/devtools/lab/labctl.sh index b5def4212f..7239c8c7f6 100755 --- a/devtools/lab/labctl.sh +++ b/devtools/lab/labctl.sh @@ -4,11 +4,16 @@ # click. Every command is one request file; its journal is printed. # # labctl.sh [-p PROFILE] state -# Print the state file (seq=, state=, program=, owner=, error=). +# Print the state file (seq=, state=, program=, controller=, owner=, +# error=). controller= is ready, none (not connected or reconnecting), +# or empty for a panel without a console. # labctl.sh [-p PROFILE] start|stop # Start or stop the selected program, then wait up to $LABCTL_WAIT # seconds (default 60) for the state to reach RUNNING (start) or # STOPPED (stop). Prints the final state; exit 0 on success, 1 otherwise. +# The app first waits up to 30 s (less when $LABCTL_WAIT is short) for +# the controller, and refuses with "controller not connected" if it +# never comes up. # labctl.sh [-p PROFILE] select GAME:PROGRAM # Show that program, as clicking it in the program list does. # labctl.sh [-p PROFILE] set KEY JSON... @@ -117,7 +122,14 @@ if [ "$cmd" = start ] || [ "$cmd" = stop ]; then [ "$(date +%s)" -ge "$end" ] && { echo "app not ready for $cmd (state: ${s:-no state file; is a lab-channel build running with this profile?})" >&2; exit 1; } nap done - echo "$cmd" | request "$name" >/dev/null || { echo "$cmd failed: $(field error)" >&2; exit 1; } + line=$cmd + if [ "$cmd" = start ]; then + # The app's controller wait must end before this script stops waiting + # for the journal, or it could start the program after we gave up. + cw=$(( W - 5 )); [ "$cw" -gt 30 ] && cw=30; [ "$cw" -lt 1 ] && cw=1 + line="start $cw" + fi + echo "$line" | request "$name" >/dev/null || { echo "$cmd failed: $(field error)" >&2; exit 1; } end=$(( $(date +%s) + W )) while :; do s=$(field state)