diff --git a/.github/workflows/cpp-ci-python-bindings.yml b/.github/workflows/cpp-ci-python-bindings.yml new file mode 100644 index 0000000000..c8fa4f9ade --- /dev/null +++ b/.github/workflows/cpp-ci-python-bindings.yml @@ -0,0 +1,50 @@ +name: C++ CI Python Bindings +on: + push: + branches: [main] + workflow_dispatch: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +# Builds the GUI-free core plus the _pa_core pybind11 module +# (Source/PythonBindings/README.md) and runs the package's pytest suite. +# This needs no Qt, OpenCV, ONNX Runtime, Tesseract, DPP or Discord SDK. + +jobs: + build: + # if: github.event_name != 'pull_request' || github.event.pull_request.draft == false + runs-on: ubuntu-26.04 + + steps: + - name: Checkout Arduino-Source + uses: actions/checkout@v7 + with: + path: 'Arduino-Source' + submodules: 'recursive' + + # Step 1: Create a single environment for the entire job + - name: Set up Virtual Environment + run: | + python3 -m venv /tmp/pa-build-venv + /tmp/pa-build-venv/bin/pip install --upgrade pip + /tmp/pa-build-venv/bin/pip install pybind11 pytest + + - name: Generate binaries + run: | + cd Arduino-Source/SerialPrograms + mkdir bin + cd bin + cmake .. -DPA_CORE_ONLY=ON -DPA_PYTHON_BINDINGS=ON -DPython_EXECUTABLE=/tmp/pa-build-venv/bin/python + cmake --build . --config RelWithDebInfo --parallel 10 + + - name: Run the Python tests + run: | + cd Arduino-Source/SerialPrograms/Source/PythonBindings + /tmp/pa-build-venv/bin/python -m pytest -q + +# Note: the venv avoids PEP 668 issues with system-pip on newer runners. +# The runner's system Python needs dev headers (python3-dev is preinstalled on the images). \ No newline at end of file diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index 58f1a9d890..86422ebd57 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -251,14 +251,14 @@ jobs: # The unit tests assume the downloadable resources they need are already downloaded # (see Packages/Resources/ResourceDownloadList.json). Unzip them from the Packages # checkout into where the program looks for them, DOWNLOADED_RESOURCE_PATH(): - # the working directory on Windows/Linux, and ~/Library/Application Support/SerialPrograms - # on macOS. + # the working directory on Windows/Linux, and ~/Library/Application Support/SerialPrograms/Profiles/unit-tests + # on macOS (the isolated test profile). - name: Install downloadable resources for tests if: inputs.run-tests shell: bash run: | if [[ "${{inputs.os}}" == mac* ]]; then - DOWNLOADED_RESOURCES="$HOME/Library/Application Support/SerialPrograms/DownloadedResources" + DOWNLOADED_RESOURCES="$HOME/Library/Application Support/SerialPrograms/Profiles/unit-tests/DownloadedResources" else DOWNLOADED_RESOURCES="Arduino-Source/SerialPrograms/bin/DownloadedResources" fi @@ -289,6 +289,27 @@ jobs: cd Arduino-Source/SerialPrograms/bin ./SerialPrograms --command-line-test-mode + - name: Reject unsafe lab launches (Mac) + if: startsWith(inputs.os, 'mac') && inputs.run-tests + shell: bash + run: | + cd Arduino-Source/SerialPrograms/bin + binary=./SerialPrograms.app/Contents/MacOS/SerialPrograms + expect_rejection() { + local status=0 + "$binary" "$@" || status=$? + if [[ "$status" != 2 ]]; then + echo "Expected startup rejection (2), read $status" + exit 1 + fi + } + expect_rejection --test bdsp + expect_rejection --profile + expect_rejection --profile ../lab-A + expect_rejection --command-line-test-mode --profile lab-A + # CI's default build has no PA_LAB_PROFILE: unprofiled relaunch must fail. + expect_rejection + - name: Run tests (Mac) if: startsWith(inputs.os, 'mac') && inputs.run-tests run: | diff --git a/.github/workflows/lab-device-tests.yml b/.github/workflows/lab-device-tests.yml new file mode 100644 index 0000000000..297dad2582 --- /dev/null +++ b/.github/workflows/lab-device-tests.yml @@ -0,0 +1,19 @@ +name: Lab device ownership +on: + pull_request: + workflow_dispatch: +jobs: + native-leases: + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v7 + - uses: jurplel/install-qt-action@v4 + with: + version: '6.10.2' + cache: true + - run: cmake -S SerialPrograms/Source/CommonFramework/Devices -B build-lab-devices -DCMAKE_BUILD_TYPE=Debug + - run: cmake --build build-lab-devices --config Debug -j4 + - run: ctest --test-dir build-lab-devices -C Debug --output-on-failure diff --git a/.github/workflows/lab-instance-tools.yml b/.github/workflows/lab-instance-tools.yml new file mode 100644 index 0000000000..ebf48e6600 --- /dev/null +++ b/.github/workflows/lab-instance-tools.yml @@ -0,0 +1,21 @@ +name: Lab instance tools +on: + pull_request: + paths: + - 'devtools/lab/**' + - 'SerialPrograms/cmake/LabBuildReceipt.cmake' + - 'SerialPrograms/CMakeLists.txt' + - '.github/workflows/lab-instance-tools.yml' + workflow_dispatch: +jobs: + offline-preparation: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-python@v6 + with: + python-version: '3.12' + - run: python3 -m unittest discover -s devtools/lab -p 'test_lab_instance.py' -v diff --git a/AGENTS.md b/AGENTS.md index 3fc0e4cd52..7b538a4f3f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -126,6 +126,11 @@ migrate a file to the wrappers when you're already touching it and it's straight Windows/Linux; `~/Library/Application Support/SerialPrograms/DownloadedResources` on macOS). - Headless Linux: `QT_QPA_PLATFORM=offscreen` and `TESSDATA_PREFIX=/Resources/Tesseract`. +- `SerialProgramsTests` (built by default next to `SerialPrograms`; `-DTEST_RUNNER_EXE=OFF` + turns it off) runs the same tests without the GUI, settings or device scans, so it can run + while SerialPrograms is open: `ninja SerialProgramsTests`, then + `SerialProgramsTests --filter ` (repeatable) or `--list`. Its exit code is the number + of failed tests. Same requirements as above. - Test framework: test classes derive `UnitTest` (`Common/Cpp/TestRunners/UnitTest.h`) and are collected via per-module `add_tests(UnitTestDatabase&)` — see `SerialPrograms/Source/Tests/CommandLineTests.h`. diff --git a/Common/Cpp/Filesystem/Filesystem.h b/Common/Cpp/Filesystem/Filesystem.h index 21d350f014..b6cedfd202 100644 --- a/Common/Cpp/Filesystem/Filesystem.h +++ b/Common/Cpp/Filesystem/Filesystem.h @@ -22,6 +22,7 @@ #ifndef PokemonAutomation_Filesystem_Filesystem_H #define PokemonAutomation_Filesystem_Filesystem_H +#include #include #include #include "FilePath.h" @@ -115,11 +116,11 @@ Path application_install_path(); Path application_scratch_path(); -// Set a profile for program settings (/UserSettings/PROFILE_NAME/) on MacOS. -// Have to run the program with command-line argument "open -n PATH_TO_APP --args --profile PROFILE_NAME" to set the profile and launch a new window. -// This allows multiple instances of the program to run since settings are no longer shared. #if defined(__APPLE__) -void set_startup_profile(int& argc, char* argv[]); +// Set a validated macOS settings profile before any settings paths are cached. +// GUI launch uses --profile NAME or the build-bound PA_LAB_PROFILE identity. +void set_startup_profile(std::string_view profile); +// Return the identity used for the window title and scratch directory. const std::string& STARTUP_PROFILE(); #endif diff --git a/Common/Cpp/Filesystem/Filesystem_Mac.h b/Common/Cpp/Filesystem/Filesystem_Mac.h index a9dbc281ab..472233e0cc 100644 --- a/Common/Cpp/Filesystem/Filesystem_Mac.h +++ b/Common/Cpp/Filesystem/Filesystem_Mac.h @@ -21,22 +21,9 @@ namespace Filesystem{ static std::string g_startup_profile; -void set_startup_profile(int& argc, char* argv[]){ - for (int i = 1; i + 1 < argc; i++){ - if (strcmp(argv[i], "--profile") == 0){ - std::string profile = argv[i + 1]; - for (char c : profile){ - if (!(std::isalpha(c) || std::isdigit(c)) && c != u'_' && c != u'-') c = u'_'; - } - g_startup_profile = std::move(profile); - // Shift everything after --profile down by 2. - for (int j = i; j + 2 < argc; j++){ - argv[j] = argv[j + 2]; - } - argc -= 2; - return; - } - } +// Store the already-validated identity before any scratch/settings path is cached. +void set_startup_profile(std::string_view profile){ + g_startup_profile = profile; } const std::string& STARTUP_PROFILE(){ diff --git a/SerialPrograms/BuildInstructions/CompilingForMac.md b/SerialPrograms/BuildInstructions/CompilingForMac.md index 8569cd0f76..af12896b66 100644 --- a/SerialPrograms/BuildInstructions/CompilingForMac.md +++ b/SerialPrograms/BuildInstructions/CompilingForMac.md @@ -94,11 +94,18 @@ brew install cmake ### Install Ninja -Ninja is a build tool that CMake can use to find out quickly what files need rebuilding. Install it using Homebrew by running in Terminal +Ninja is a build tool that CMake can use to find out quickly what files need rebuilding by file modification time. Install it using Homebrew by running in Terminal ``` brew install ninja ``` +### Install ccache + +ccache is another build tool that saves build time. It uses a cache to skip building a source code file if that file's content has been compiled before. Install it using Homebrew by running in Terminal +``` +brew install ccache +``` + ### Install Qt6 We use Qt6 software library to build UI (windows, panels, buttons...) for **SerialPrograms**. It also plays and decodes audio. @@ -268,11 +275,13 @@ This makes a new folder called "build_mac" in "Arduino-Source" and moves the Ter Execute the command in Terminal: ``` -cmake ../SerialPrograms/ -G Ninja -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_PREFIX_PATH=$HOME/Qt/6.10.2/macos +cmake ../SerialPrograms/ -G Ninja -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_PREFIX_PATH=$HOME/Qt/6.10.2/macos -DPA_USE_CCACHE=ON ``` -This command uses CMake to configure our code to prepare for compilation. `-G Ninja` tells CMake to compile with Ninja, and `-DCMAKE_PREFIX_PATH` tells CMake where the Qt you installed is. If successful, it should print texts like +This command uses CMake to configure our code to prepare for compilation. `-G Ninja` tells CMake to compile with Ninja, `-DCMAKE_PREFIX_PATH` tells CMake where the Qt you installed is, and `-DPA_USE_CCACHE=ON` tells CMake to compile with ccache. If successful, it should print texts like ``` ... +-- Using ccache: /opt/homebrew/bin/ccache cache_dir=/Users//Library/Caches/ccache/PA-Arduino-Source base_dir= +... -- Configuring done -- Generating done -- Build files have been written to: /build_mac @@ -286,6 +295,7 @@ cmake --build . -j 10 ``` to compile and build the program in "build_mac" folder. It will take a while to build depending on your computer's speed. +Later builds should be faster thanks to Ninja and ccache. Each line starts with a counter like `[25/1613]`. If successful, the counter reaches the total, and the last lines in Terminal look like ``` @@ -351,11 +361,26 @@ rm -rf build_mac to delete the old "build_mac" folder. Then follow [Part 4: Compile the Program](#part-4-compile-the-program) again, with the new Qt version in the `-DCMAKE_PREFIX_PATH` part of the CMake command. +## Part 7: Tuning ccache (optional) + +The default ccache settings work for most people. This part is for developers who build often or keep several copies of the code (a.k.a worktrees) in the filesystem. + +To check cache statistics on the codebase (how many cache hits and misses so far and storage space uses), run in Terminal +``` +ccache --dir $(ccache --get-config cache_dir)/PA-Arduino-Source -s +``` + +By default ccache has a max cache size of 5 GB. When the storage is full, ccache deletes the files it used least recently. +If you often switch between many branches and worktrees of the codebase, you can increase its size to say 20 GB by +``` +ccache --dir $(ccache --get-config cache_dir)/PA-Arduino-Source --set-config max_size=20G +``` +For more info on how we set up ccache when configuring the program, see `SerialPrograms/cmake/Ccache.cmake`.
-Guide Written by: jw098 +Guide Written by: Gin, jw098 **Discord Server:** diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 35bb597e32..64c13656fd 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -26,6 +26,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) #set(CMAKE_VERBOSE_MAKEFILE ON) +# Compile through ccache when it is installed. See cmake/Ccache.cmake for the options. +include(cmake/Ccache.cmake) + # Set these three on globally so all intermediate Qt targets can get processed automatically set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) @@ -63,6 +66,32 @@ find_package(Threads REQUIRED) # Define a toggle option (ON by default) option(COMMAND_LINE_EXE "Build the optional executable" ON) +# Build SerialProgramsTests, which runs the unit tests without the GUI. +option(TEST_RUNNER_EXE "Build the unit test executable, SerialProgramsTests" ON) +# Build the _pa_core Python (pybind11) module; see Source/PythonBindings/README.md. +# OFF by default because it needs a Python interpreter with development headers +# and pybind11 (a system package or `pip install pybind11`). +option(PA_PYTHON_BINDINGS "Build the _pa_core Python module (Switch controller)" OFF) + +# miniz is a third-party library compiled into every target that uses +# CORE_LIBRARY_SOURCES (CoreLib in both build modes, SerialProgramsLib in the +# full build): compile it with warnings off so its own code (and its header, +# which it includes from its own directory, so no SYSTEM include directory can +# shield it) can never trip -Werror / /WX, regardless of compiler or compiler +# version. Must run before the PA_CORE_ONLY early return below; source +# properties are global, so setting it once covers every target. +if (MSVC) + set_source_files_properties( + ../3rdParty-Core/miniz-3.1.1/miniz.c + PROPERTIES COMPILE_OPTIONS "/W0" + ) +else() + set_source_files_properties( + ../3rdParty-Core/miniz-3.1.1/miniz.c + PROPERTIES COMPILE_OPTIONS "-w" + ) +endif() + # Build only the GUI-free core: CoreLib and SerialProgramsCommandLine. This needs # no Qt, OpenCV, ONNX Runtime, Tesseract, DPP or Discord SDK, so none of them are # searched for. @@ -84,6 +113,9 @@ if(PA_CORE_ONLY) if(COMMAND_LINE_EXE) include(Source/CommandLine/CommandLineExecutable.cmake) endif() + if(PA_PYTHON_BINDINGS) + include(Source/PythonBindings/PythonBindings.cmake) + endif() return() endif() @@ -203,6 +235,27 @@ if (APPLE) # https://stackoverflow.com/questions/53560288/how-to-create-a-macos-app-bundle-with-cmake # https://cmake.org/cmake/help/latest/command/add_executable.html add_executable(SerialPrograms MACOSX_BUNDLE ${SerialPrograms_ICON} ${EXECUTABLE_SOURCES}) + # Compiled identity survives Finder/TCC relaunch without command-line arguments. + set(PA_LAB_PROFILE "" CACHE STRING "Bind this lab binary to one macOS settings profile") + if(NOT PA_LAB_PROFILE STREQUAL "") + if(NOT PA_LAB_PROFILE MATCHES "^[A-Za-z0-9_][A-Za-z0-9_-]*$") + message(FATAL_ERROR "PA_LAB_PROFILE must contain only ASCII letters, digits, '_' or '-'.") + endif() + target_compile_definitions(SerialPrograms PRIVATE PA_LAB_PROFILE="${PA_LAB_PROFILE}") + endif() + + # Receipt binds packaging to the linked binary, not just a CMake cache value. + set_property(TARGET SerialPrograms APPEND PROPERTY LINK_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/LabBuildReceipt.cmake") + add_custom_command(TARGET SerialPrograms POST_BUILD + COMMAND "${CMAKE_COMMAND}" + "-DBINARY=$" + "-DPROFILE=${PA_LAB_PROFILE}" + "-DSOURCE=${CMAKE_CURRENT_SOURCE_DIR}/.." + -P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/LabBuildReceipt.cmake" + VERBATIM + ) + set_target_properties(SerialPrograms PROPERTIES BUNDLE True MACOSX_BUNDLE_GUI_IDENTIFIER PokemonAutomation.SerialPrograms @@ -283,13 +336,6 @@ endfunction() apply_common_target_properties(SerialProgramsLib) apply_common_target_properties(SerialPrograms) -if(MSVC) - set_source_files_properties("../3rdParty-Core/miniz-3.1.1/miniz.c" PROPERTIES COMPILE_FLAGS "/W0") -else() - # For GCC/Clang - set_source_files_properties("../3rdParty-Core/miniz-3.1.1/miniz.c" PROPERTIES COMPILE_FLAGS "-w") -endif() - # REPO_ROOT_DIR: the root folder of the git repository, "Arduino-Source/" set(PACKAGES_DIR "${REPO_ROOT_DIR}/Packages") set(FIRMWARE_DIR "${PACKAGES_DIR}/Firmware") @@ -336,6 +382,10 @@ if(NOT EXISTS "${DEST_DIR}/Scripts") ) endif() +if(TEST_RUNNER_EXE) + include(Source/Tests/TestRunnerExecutable.cmake) +endif() + if (WIN32) add_library(OpenCV_lib IMPORTED UNKNOWN) target_include_directories(SerialProgramsLib SYSTEM PRIVATE ../3rdParty/opencv-4.12.0/) @@ -888,3 +938,17 @@ if(COMMAND_LINE_EXE) # Add command-line executable (GUI-free) from subdirectory include(Source/CommandLine/CommandLineExecutable.cmake) endif() + +# The registered cross-process tests use this GUI-free native runner as their child. +add_subdirectory(Source/CommonFramework/Devices) +if(WIN32) + set_target_properties(LabDeviceAccessTests PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${WIN_DEPLOY_DIR}") +else() + set_target_properties(LabDeviceAccessTests PROPERTIES RUNTIME_OUTPUT_DIRECTORY "$") +endif() +add_dependencies(SerialPrograms LabDeviceAccessTests) + +if(PA_PYTHON_BINDINGS) + # Python bindings (GUI-free) from subdirectory + include(Source/PythonBindings/PythonBindings.cmake) +endif() diff --git a/SerialPrograms/Source/CommandLine/CommandLine_Main.cpp b/SerialPrograms/Source/CommandLine/CommandLine_Main.cpp index 09de8e1d98..4ea30ea8f8 100644 --- a/SerialPrograms/Source/CommandLine/CommandLine_Main.cpp +++ b/SerialPrograms/Source/CommandLine/CommandLine_Main.cpp @@ -23,11 +23,6 @@ using namespace PokemonAutomation::NintendoSwitch; namespace PokemonAutomation{ -bool use_qt_ui(){ - return false; -} - - FileLogger& global_file_logger(){ static FileLogger logger( GlobalThreadPools::unlimited_normal(), @@ -88,7 +83,7 @@ int main(int argc, char* argv[]){ for (int i = 0; i < 30; i++){ // Push A button: delay=0ms, hold=50ms, release=50ms, button=BUTTON_A - controller.push_button(0, 50, 50, static_cast(BUTTON_A)); + controller.push_button(static_cast(BUTTON_A), 0, 50, 50); } // Wait for all button commands to complete diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp index 4bb1f84370..51bfcf9936 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp @@ -406,16 +406,12 @@ void AudioPassthroughPairQt::reset( auto scope_check = m_sanitizer.check_scope(); QMetaObject::invokeMethod(this, [this, format, descriptor, output, output_volume]{ auto scope_check = m_sanitizer.check_scope(); + close_audio_source(); std::unique_ptr source; if (descriptor){ source = descriptor->make_AudioSource(m_logger, format); } WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); - if (m_reader){ - teardown_tree(); - m_sink_device.reset(); - m_reader.reset(); - } m_input_choice = format; m_output_device = output; m_output_volume = output_volume; @@ -427,6 +423,15 @@ void AudioPassthroughPairQt::reset( } }); } +// Called on this object's Qt thread before constructing a replacement input. +void AudioPassthroughPairQt::close_audio_source(){ + WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); + if (m_reader){ + teardown_tree(); + m_sink_device.reset(); + m_reader.reset(); + } +} void AudioPassthroughPairQt::clear_audio_source(){ auto scope_check = m_sanitizer.check_scope(); QMetaObject::invokeMethod(this, [this]{ @@ -447,16 +452,12 @@ void AudioPassthroughPairQt::set_audio_source( auto scope_check = m_sanitizer.check_scope(); QMetaObject::invokeMethod(this, [this, descriptor, format]{ auto scope_check = m_sanitizer.check_scope(); + close_audio_source(); std::unique_ptr source; if (descriptor){ source = descriptor->make_AudioSource(m_logger, format); } WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION); - if (m_reader){ - teardown_tree(); - m_sink_device.reset(); - m_reader.reset(); - } m_input_choice = format; if (source){ m_reader = std::move(source); diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h index 014b9c2da7..872904e33f 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.h @@ -97,6 +97,7 @@ class AudioPassthroughPairQt final : public QObject, public AudioPassthroughPair // thread only ever sees them through the node listener sets, where // ListenerSet::remove blocks on in-flight pushes so rebuilds never // race a live push. + void close_audio_source(); void teardown_tree(); void teardown_sink_branch(); void build_tree(float input_volume); diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp index 4a4ee9a587..45a5f28366 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "3rdParty/QtWavFile/WavFile.h" #include "Common/Qt/StringToolsQt.h" @@ -106,9 +107,7 @@ bool AudioFileLoader::start(){ connect(m_timer, &QTimer::timeout, this, &AudioFileLoader::send_decoded_buffer_on_timer); }); - emit m_audio_decoder_worker->start(); - - return m_audio_decoder_worker->start_succeeded(); + return m_audio_decoder_worker->start(); } void AudioFileLoader::build_timer(){ @@ -144,39 +143,39 @@ std::tuple AudioFileLoader::load_full_audio(){ return convert_raw_wav_samples(); } - // Use QAudioDecoder to decode compressed audio file: - m_audio_decoder_worker = static_cast( - GlobalThreadPools::qt_event_threadpool().add_object([this]{ - return std::make_unique(nullptr, m_filename, m_audio_format, m_raw_buffer); - }) - ); -// m_audio_decoder_worker = new AudioDecoderWorker(nullptr, m_filename, m_audio_format, m_raw_buffer); -// m_audio_decoder_worker->moveToThread(&m_audioDecoderThread); - - connect(this, &AudioFileLoader::run_audio_decoder_async, m_audio_decoder_worker, &AudioDecoderWorker::start); -// connect(&m_audioDecoderThread, &QThread::finished, m_audio_decoder_worker, &QObject::deleteLater); - - QEventLoop loop; - connect(m_audio_decoder_worker, &AudioDecoderWorker::errored, &loop, &QEventLoop::quit); - connect(m_audio_decoder_worker, &AudioDecoderWorker::finished, &loop, &QEventLoop::quit); - - // m_audioDecoderThread takes care of m_audio_decoder_worker via finished -> deleteLater connection. - // So we don't hold the pointer m_audio_decoder_worker anymore. - m_audio_decoder_worker = nullptr; - -// m_audioDecoderThread.start(); - emit run_audio_decoder_async(); + // Use QAudioDecoder to decode compressed audio file on the current thread. + bool errored = false; + { + AudioDecoderWorker worker(nullptr, m_filename, m_audio_format, m_raw_buffer); + + QEventLoop loop; + bool done = false; + connect(&worker, &AudioDecoderWorker::errored, &loop, [&done, &errored, &loop]{ + done = true; + errored = true; + loop.quit(); + }); + connect(&worker, &AudioDecoderWorker::finished, &loop, [&done, &loop]{ + done = true; + loop.quit(); + }); + + // The decoder can fail inside `start()`: Qt reports a missing file as + // an error right away, we check `done` after `start()`. + worker.start(); + if (!done){ + loop.exec(); + } + } - // Block the current thread until the m_audio_decoder_worker errored or finished. - loop.exec(); if (m_raw_buffer.empty()){ std::cout << "Error: audio decoder produced no audio data for " << m_filename << " (the decoder could not find usable audio in the file)." << std::endl; } - -// m_audioDecoderThread.quit(); -// m_audioDecoderThread.wait(); - GlobalThreadPools::qt_event_threadpool().remove_object(m_audio_decoder_worker); + else if (errored){ + std::cout << "Error: error decoding audio file " << m_filename << "." << std::endl; + m_raw_buffer.clear(); + } return std::make_tuple(m_raw_buffer.data(), m_raw_buffer.size()); } @@ -354,10 +353,23 @@ void AudioDecoderWorker::internal_start(){ m_audio_decoder->start(); std::cout << "Start audio decoder." << std::endl; - m_start_succeeded = true; + // Qt can report an error inside start() itself, e.g. for a missing file. + m_start_succeeded = m_audio_decoder->error() == QAudioDecoder::NoError; return; } +bool AudioDecoderWorker::start(){ + // internal_start() must run on the worker's thread (queried via `this->thread()`) + if (QThread::currentThread() == thread()){ + internal_start(); + }else{ + // Called from any another thread + // `QMetaObject::invokeMethod()` blocks until it has run there + QMetaObject::invokeMethod(this, &AudioDecoderWorker::internal_start, Qt::BlockingQueuedConnection); + } + return m_start_succeeded; +} + AudioDecoderWorker::~AudioDecoderWorker(){ stop(); } diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h index ce1236fee6..ce715e8164 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader.h @@ -60,6 +60,8 @@ class AudioFileLoader: public QObject{ // Send the sample buffers at the desired speed determined by sample rate in `audio_format` passed // to the constructor. // Retrieve sample buffer by connecting to signal `buffer_ready` from the same thread. + // Returns false if the file can't be opened or decoded from the start, e.g. a missing + // file. For a compressed file, this briefly waits for the decoder to start on its thread. // See comments of the class full more details. bool start(); @@ -79,9 +81,6 @@ class AudioFileLoader: public QObject{ // When finished sending decoded audio frames after `start()` called void finished(); - // Used privately to launch audio decoder woker in a separate thread. - void run_audio_decoder_async(); - private: void build_timer(); @@ -144,11 +143,11 @@ class AudioDecoderWorker: public QObject{ ); virtual ~AudioDecoderWorker(); - void start(){ - QMetaObject::invokeMethod(this, &AudioDecoderWorker::internal_start); - } - - bool start_succeeded(){ return m_start_succeeded; } + // Start decoding on the worker's thread. Returns whether the decoder + // started: false if it failed right away, e.g. for a missing file. + // This function works both on `AudioDecoderWorker`'s own Qt thread, and on + // another Qt thread. + bool start(); void stop(){ QMetaObject::invokeMethod(this, &AudioDecoderWorker::internal_stop); @@ -160,6 +159,9 @@ class AudioDecoderWorker: public QObject{ void finished(); public slots: + // Creates an QAudioDecoder to start decoding the file. + // Every Qt object (including this `AudioDecoderWorker` class) belongs to one thread, queried by `this->thread()`. + // Qt requires a child to be created on its parent’s thread. So internal_start() must run on the worker’s thread. void internal_start(); void internal_stop(); @@ -179,7 +181,7 @@ public slots: std::vector& m_decoded_buffer; - bool m_start_succeeded = true; + bool m_start_succeeded = false; }; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp index 9e1d108ea3..a82f412938 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.cpp @@ -19,6 +19,7 @@ #include #include #include +#include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -476,6 +477,97 @@ class DecodedNativePlaybackTest : public UnitTest{ }; +// +// load_full_audio() on the mp3 fixture, with the kind of target that +// load_audio_template() uses for the mp3 templates (mono float): the decode +// path must return about 2 s of audio. +// +class LoadFullMp3Test : public UnitTest{ +public: + LoadFullMp3Test() + : UnitTest("AudioFileLoader - LoadFull_Mp3") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::string fixture = decoded_probe_fixture(); + if (!Filesystem::exists(fixture)){ + return "Missing mp3 fixture: " + fixture + "."; + } + if (!QAudioDecoder(nullptr).isSupported()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "No audio decode backend (Qt Multimedia media plugin missing or failing to load)." + ); + } + + // load_full_audio() does not resample, so the target is at the + // fixture's own 44100 Hz. + QAudioFormat target; + target.setSampleFormat(QAudioFormat::SampleFormat::Float); + target.setSampleRate(44100); + target.setChannelCount(1); + + AudioFileLoader loader(nullptr, fixture, target); + const auto ret = loader.load_full_audio(); + const size_t frame_count = std::get<1>(ret) / sizeof(float); + if (frame_count > 88200 + 4000 || frame_count < 88200 - 4000){ + return "Expected about 88200 frames (2 s at 44100 Hz), got " + + std::to_string(frame_count) + "."; + } + + return true; + } +}; + + +// +// A missing mp3 fails right away on both decode paths. Qt reports a missing +// file as an error inside QAudioDecoder::start(), before start() returns: +// - load_full_audio() returns no data. It must not wait in its event loop +// for a decoder signal that already fired, or it would never return. +// - start() returns false. It used to read the start result before the +// decoder's thread had set it, so it always returned true. +// +class Mp3MissingFileTest : public UnitTest{ +public: + Mp3MissingFileTest() + : UnitTest("AudioFileLoader - Mp3_MissingFile") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + if (!QAudioDecoder(nullptr).isSupported()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "No audio decode backend (Qt Multimedia media plugin missing or failing to load)." + ); + } + + const std::string missing = "/nonexistent/pa_audio_file_loader_missing.mp3"; + QAudioFormat format; + format.setSampleFormat(QAudioFormat::SampleFormat::Float); + format.setSampleRate(44100); + format.setChannelCount(1); + + { + AudioFileLoader loader(nullptr, missing, format); + const auto ret = loader.load_full_audio(); + if (std::get<1>(ret) != 0){ + return "load_full_audio() should return no data for a missing mp3, got " + + std::to_string(std::get<1>(ret)) + " bytes."; + } + } + { + AudioFileLoader loader(nullptr, missing, format); + if (loader.start()){ + return "start() should fail for a missing mp3."; + } + } + + return true; + } +}; + + } @@ -490,6 +582,8 @@ void add_tests_AudioFileLoader(UnitTestDatabase& database){ database.add(); database.add(); database.add(); + database.add(); + database.add(); } diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp index 758ccc70b8..0795682ce4 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp @@ -53,6 +53,7 @@ AudioInputDevice::AudioInputDevice( RAW_BUFFER_CAPACITY ) , ObjectStreamListener(raw_output_frame_bytes(format, reinterpret_96k_mono_as_stereo)) + , m_device_lease(acquire_lab_capture(logger, LabDeviceKind::AUDIO, device.id().toHex().toStdString())) , m_sample_type(sample_format_of(format)) , m_sample_rate(format.sampleRate()) , m_channels(format.channelCount()) diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h index 2ef2c7365b..fba2d91614 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.h @@ -17,6 +17,7 @@ using NativeAudioSource = QAudioSource; #include "Common/Cpp/StreamConverters.h" #include "Kernels/AudioPipeline/Kernels_AudioPipeline.h" #include "CommonFramework/AudioPipeline/AudioFormat.h" +#include "CommonFramework/Devices/LabDeviceRuntime.h" class QAudioFormat; @@ -87,6 +88,7 @@ class AudioInputDevice final private: + std::unique_ptr m_device_lease; std::unique_ptr m_source; Kernels::AudioPipeline::SampleFormat m_sample_type; size_t m_sample_rate; diff --git a/SerialPrograms/Source/CommonFramework/AudioPipeline/Sources/AudioSource_Device.cpp b/SerialPrograms/Source/CommonFramework/AudioPipeline/Sources/AudioSource_Device.cpp index 18706767e7..afca55d3ba 100644 --- a/SerialPrograms/Source/CommonFramework/AudioPipeline/Sources/AudioSource_Device.cpp +++ b/SerialPrograms/Source/CommonFramework/AudioPipeline/Sources/AudioSource_Device.cpp @@ -68,7 +68,11 @@ std::unique_ptr AudioSourceDescriptor_Device::make_AudioSource( return nullptr; } // cout << "format = " << (int)format << endl; - return std::make_unique(logger, m_info, format); + try{ + return std::make_unique(logger, m_info, format); + }catch (UserSetupError&){ + return nullptr; // Refusal is logged; never throw across the Qt audio callback. + } } diff --git a/SerialPrograms/Source/CommonFramework/Devices/CMakeLists.txt b/SerialPrograms/Source/CommonFramework/Devices/CMakeLists.txt new file mode 100644 index 0000000000..dba4f55b10 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/CMakeLists.txt @@ -0,0 +1,20 @@ +# Focused native tests, without configuring/linking the GUI application: +# cmake -S SerialPrograms/Source/CommonFramework/Devices -B /path/to/test-build +# cmake --build /path/to/test-build -j4 +# ctest --test-dir /path/to/test-build --output-on-failure +cmake_minimum_required(VERSION 3.24) +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + project(LabDeviceAccessTests LANGUAGES CXX) + enable_testing() + find_package(Qt6 REQUIRED COMPONENTS Core) +endif() +add_executable(LabDeviceAccessTests LabDeviceAccessQt.cpp LabDeviceAccessQt_Tests.cpp) +target_compile_features(LabDeviceAccessTests PRIVATE cxx_std_23) +target_compile_definitions(LabDeviceAccessTests PRIVATE PA_LAB_DEVICE_STANDALONE_TESTS) +target_link_libraries(LabDeviceAccessTests PRIVATE Qt6::Core) +if(MSVC) + target_compile_options(LabDeviceAccessTests PRIVATE /W4 /WX) +else() + target_compile_options(LabDeviceAccessTests PRIVATE -Wall -Wextra -Werror) +endif() +add_test(NAME LabDeviceAccess COMMAND LabDeviceAccessTests) diff --git a/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.cpp b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.cpp new file mode 100644 index 0000000000..d420befadf --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.cpp @@ -0,0 +1,153 @@ +/* Lab Device Access (Qt process-lock seam) + * + * From: https://github.com/PokemonAutomation/ + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "LabDeviceAccessQt.h" + +namespace PokemonAutomation{ +namespace{ + +QString text(std::string_view value){ + return QString::fromUtf8(value.data(), (qsizetype)value.size()); +} + +std::set read_ids(const QJsonObject& object, const char* key){ + if (!object.value(key).isArray()){ + throw std::runtime_error(std::string("Expected device array: ") + key); + } + std::set ids; + for (const QJsonValue& value : object.value(key).toArray()){ + const std::string id = value.toString().toStdString(); + if (!value.isString() || id.empty() || id.find('\0') != std::string::npos || !ids.emplace(id).second){ + throw std::runtime_error(std::string("Empty, invalid or duplicate device ID in ") + key); + } + } + return ids; +} + +bool permitted(const LabDevicePolicy& policy, const LabDeviceIdentity& device){ + switch (device.kind){ + case LabDeviceKind::SERIAL: return policy.serial.contains(device.id); + case LabDeviceKind::VIDEO: return policy.video.contains(device.id); + case LabDeviceKind::AUDIO: return policy.audio.contains(device.id); + case LabDeviceKind::PROFILE: return false; + } + return false; +} + +// Capture IDs are allowlisted as hex; show the readable ID beside it so a +// refusal names the device the settings selected. +std::string readable_capture_id(const std::string& hex){ + const bool is_hex = hex.size() % 2 == 0 && std::ranges::all_of(hex, [](char c){ + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); + }); + if (!is_hex){ + return hex; + } + const QByteArray bytes = QByteArray::fromHex(QByteArray(hex.data(), (qsizetype)hex.size())); + return bytes.toStdString() + " (allowlist ID " + hex + ")"; +} + +std::string describe(const LabDeviceIdentity& device){ + switch (device.kind){ + case LabDeviceKind::SERIAL: return "Serial device " + device.id; + case LabDeviceKind::VIDEO: return "Video input " + readable_capture_id(device.id); + case LabDeviceKind::AUDIO: return "Audio input " + readable_capture_id(device.id); + case LabDeviceKind::PROFILE: return "Profile " + device.id; + } + return device.id; +} + +std::string normalize_serial_path(std::string_view path){ + if (path.starts_with("/dev/tty.")){ + return "/dev/cu." + std::string(path.substr(9)); + } + return std::string(path); +} + +} // namespace + +LabDevicePolicy parse_lab_device_policy(std::string_view json, std::string_view profile){ + QJsonParseError error; + const QJsonDocument document = QJsonDocument::fromJson(QByteArray(json.data(), (qsizetype)json.size()), &error); + const QJsonObject object = document.object(); + const std::set keys{"schema", "profile", "serial", "video", "audio"}; + for (auto it = object.begin(); it != object.end(); ++it){ + if (!keys.contains(it.key())){ + throw std::runtime_error("Unknown field in lab device policy."); + } + } + if (error.error != QJsonParseError::NoError || !document.isObject() || object.size() != 5 || + !object.value("schema").isDouble() || object.value("schema").toDouble() != 1 || + profile.empty() || object.value("profile").toString() != text(profile)){ + throw std::runtime_error("Missing, malformed or mismatched lab device policy."); + } + return {std::string(profile), read_ids(object, "serial"), read_ids(object, "video"), read_ids(object, "audio")}; +} + +std::string lab_serial_identity(unsigned vendor, unsigned product, std::string_view serial){ + if (vendor > 0xffff || product > 0xffff || serial.empty() || serial.find('\0') != std::string_view::npos){ + throw std::runtime_error("A unique USB serial, vendor and product identity are required."); + } + return std::format("{:04x}:{:04x}:{}", vendor, product, serial); +} + +std::string resolve_lab_serial_identity(std::span devices, std::string_view port){ + const std::string path = normalize_serial_path(port); + const auto selected = std::ranges::find_if(devices, [&](const LabSerialDevice& device){ + return normalize_serial_path(device.port) == path; + }); + if (selected == devices.end()){ + throw std::runtime_error("Controller has no current USB identity; hardware access is disabled."); + } + const std::string identity = lab_serial_identity(selected->vendor, selected->product, selected->serial); + for (const LabSerialDevice& other : devices){ + if (normalize_serial_path(other.port) != path && !other.serial.empty() && + lab_serial_identity(other.vendor, other.product, other.serial) == identity){ + throw std::runtime_error("Two adapters report the same USB identity; refusing ambiguous ownership."); + } + } + return identity; +} + +std::string lab_device_lock_path(std::string_view directory, const LabDeviceIdentity& device){ + if (directory.empty() || device.id.empty()){ + throw std::runtime_error("Device lease directory and identity must be nonempty."); + } + const QByteArray key = QByteArray::number((int)device.kind) + ':' + QByteArray(device.id.data(), (qsizetype)device.id.size()); + const QByteArray digest = QCryptographicHash::hash(key, QCryptographicHash::Sha256).toHex(); + return QDir(text(directory)).filePath(QString::fromLatin1(digest) + ".lock").toStdString(); +} + +LabDeviceLease::LabDeviceLease(std::string_view path) + : m_lock(text(path)) +{ + // Age alone must never evict an active multi-hour lab run. + m_lock.setStaleLockTime(std::chrono::milliseconds::zero()); + if (!m_lock.tryLock(std::chrono::milliseconds::zero())){ + throw std::runtime_error("Device/profile is already leased, or its lock cannot be created. Nothing was opened."); + } +} + +std::unique_ptr acquire_lab_device( + const LabDevicePolicy& policy, std::string_view directory, const LabDeviceIdentity& device +){ + if (!permitted(policy, device)){ + throw std::runtime_error(describe(device) + " is not allowlisted for profile " + policy.profile + ". Nothing was opened."); + } + if (!QDir().mkpath(text(directory))){ + throw std::runtime_error("Cannot create shared device lease directory. Nothing was opened."); + } + return std::make_unique(lab_device_lock_path(directory, device)); +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.h b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.h new file mode 100644 index 0000000000..669ce69f9d --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.h @@ -0,0 +1,72 @@ +/* Lab Device Access (Qt process-lock seam) + * + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_LabDeviceAccessQt_H +#define PokemonAutomation_LabDeviceAccessQt_H + +#include +#include +#include +#include +#include +#include + +namespace PokemonAutomation{ + +// Separate namespaces for serial, video, audio and whole-profile reservations. +enum class LabDeviceKind{ SERIAL, VIDEO, AUDIO, PROFILE }; + +// Stable hardware identity, never a display alias, ordinal or USB port pathname. +struct LabDeviceIdentity{ + LabDeviceKind kind; + std::string id; +}; + +// Immutable allowlist loaded once for the selected lab profile. +struct LabDevicePolicy{ + std::string profile; + std::set serial; + std::set video; + std::set audio; +}; + +// One currently enumerated USB port; tty/cu aliases may describe the same adapter. +struct LabSerialDevice{ + std::string port; + unsigned vendor; + unsigned product; + std::string serial; +}; + +// Resolve a selected current port, rejecting missing or ambiguous physical identity. +std::string resolve_lab_serial_identity(std::span devices, std::string_view port); + +// Parse the strict schema; malformed/mismatched input throws std::runtime_error. +LabDevicePolicy parse_lab_device_policy(std::string_view json, std::string_view profile); + +// Normalize VID/PID and a nonempty USB serial; empty identifiers are refused. +std::string lab_serial_identity(unsigned vendor, unsigned product, std::string_view serial); + +// Compute a shared lock path independent of profile, executable and display names. +std::string lab_device_lock_path(std::string_view directory, const LabDeviceIdentity& device); + +// A noncopyable process lease. Destruction releases it; dead owners are recovered by Qt. +// Keep this alive until after the native device has closed. Acquisition never waits. +class LabDeviceLease{ +public: + explicit LabDeviceLease(std::string_view path); + LabDeviceLease(const LabDeviceLease&) = delete; + LabDeviceLease& operator=(const LabDeviceLease&) = delete; +private: + QLockFile m_lock; +}; + +// Check the allowlist before acquiring a lease. A denial never touches hardware. +// Throws std::runtime_error on denied access, contention or filesystem failure. +std::unique_ptr acquire_lab_device( + const LabDevicePolicy& policy, std::string_view directory, const LabDeviceIdentity& device +); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt_Tests.cpp b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt_Tests.cpp new file mode 100644 index 0000000000..83f5613e96 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt_Tests.cpp @@ -0,0 +1,328 @@ +/* Lab Device Access Tests + * + * From: https://github.com/PokemonAutomation/ + * + * PR #226 startup incident: a Console B test build opened Console A's controller. + * These tests never enumerate or open physical devices, or load real profiles. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "LabDeviceAccessQt.h" +#ifndef PA_LAB_DEVICE_STANDALONE_TESTS +#include "LabDeviceAccessQt_Tests.h" +#endif + +namespace PokemonAutomation{ +namespace{ + +struct PolicyInput{ + std::string_view name; + std::string_view json; + LabDeviceIdentity requested; +}; + +struct PolicyExpectation{ + int opens = 0; + std::string_view refusal = {}; // The refusal message starts with this. +}; + +struct PolicyCase{ + PolicyInput input; + PolicyExpectation expectation; +}; + +struct SerialInput{ + std::string_view name; + std::vector devices; + std::string_view port; +}; + +struct SerialExpectation{ + std::optional identity = std::nullopt; +}; + +struct SerialCase{ + SerialInput input; + SerialExpectation expectation; +}; + +const SerialCase SERIAL_CASES[] = { + {.input = {.name = "USB serial identity", .devices = {{"/dev/cu.A", 0x10c4, 0xea60, "A"}}, .port = "/dev/cu.A"}, .expectation = {.identity = "10c4:ea60:A"}}, + {.input = {.name = "tty and cu share a key", .devices = {{"/dev/tty.A", 0x10c4, 0xea60, "A"}, {"/dev/cu.A", 0x10c4, 0xea60, "A"}}, .port = "/dev/cu.A"}, .expectation = {.identity = "10c4:ea60:A"}}, + {.input = {.name = "two adapters with duplicate serial", .devices = {{"/dev/cu.A", 0x10c4, 0xea60, "same"}, {"/dev/cu.B", 0x10c4, 0xea60, "same"}}, .port = "/dev/cu.A"}, .expectation = {}}, + {.input = {.name = "missing USB serial", .devices = {{"/dev/cu.A", 0x10c4, 0xea60, ""}}, .port = "/dev/cu.A"}, .expectation = {}}, + {.input = {.name = "unplugged adapter", .devices = {}, .port = "/dev/cu.A"}, .expectation = {}}, + {.input = {.name = "path reused by another adapter", .devices = {{"/dev/cu.A", 0x10c4, 0xea60, "B"}}, .port = "/dev/cu.A"}, .expectation = {.identity = "10c4:ea60:B"}}, +}; + +std::string check_serial(const SerialCase& test){ + std::optional identity; + try{ + identity = resolve_lab_serial_identity(test.input.devices, test.input.port); + }catch (const std::runtime_error&){} + if (identity != test.expectation.identity){ + return "Expected identity " + test.expectation.identity.value_or("") + "; read " + identity.value_or(""); + } + return {}; +} + +const PolicyCase POLICY_CASES[] = { + {.input = {.name = "matching serial", .json = R"({"schema":1,"profile":"lab-B","serial":["10c4:ea60:B"],"video":[],"audio":[]})", .requested = {LabDeviceKind::SERIAL, "10c4:ea60:B"}}, .expectation = {.opens = 1}}, + {.input = {.name = "wrong console serial", .json = R"({"schema":1,"profile":"lab-B","serial":["10c4:ea60:B"],"video":[],"audio":[]})", .requested = {LabDeviceKind::SERIAL, "10c4:ea60:A"}}, .expectation = {.refusal = "Serial device 10c4:ea60:A is not allowlisted for profile lab-B."}}, + {.input = {.name = "camera allowed", .json = R"({"schema":1,"profile":"lab-B","serial":[],"video":["766964656f42"],"audio":[]})", .requested = {LabDeviceKind::VIDEO, "766964656f42"}}, .expectation = {.opens = 1}}, + {.input = {.name = "audio allowed", .json = R"({"schema":1,"profile":"lab-B","serial":[],"video":[],"audio":["617564696f42"]})", .requested = {LabDeviceKind::AUDIO, "617564696f42"}}, .expectation = {.opens = 1}}, + // lab-B, 2026-11-08: a panel selected console A's audio input, and the refusal didn't say which device it was. + {.input = {.name = "wrong console audio", .json = R"({"schema":1,"profile":"lab-B","serial":[],"video":[],"audio":["617564696f42"]})", .requested = {LabDeviceKind::AUDIO, "617564696f41"}}, .expectation = {.refusal = "Audio input audioA (allowlist ID 617564696f41) is not allowlisted for profile lab-B."}}, + {.input = {.name = "camera not audio", .json = R"({"schema":1,"profile":"lab-B","serial":[],"video":["same-id"],"audio":[]})", .requested = {LabDeviceKind::AUDIO, "same-id"}}, .expectation = {}}, + {.input = {.name = "mismatched profile", .json = R"({"schema":1,"profile":"lab-A","serial":["port"],"video":[],"audio":[]})", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, + {.input = {.name = "missing policy", .json = "", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, + {.input = {.name = "malformed policy", .json = "{", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, + {.input = {.name = "unsupported schema", .json = R"({"schema":2,"profile":"lab-B","serial":["port"],"video":[],"audio":[]})", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, + {.input = {.name = "duplicate identity", .json = R"({"schema":1,"profile":"lab-B","serial":["port","port"],"video":[],"audio":[]})", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, + {.input = {.name = "missing audio list", .json = R"({"schema":1,"profile":"lab-B","serial":["port"],"video":[]})", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, + {.input = {.name = "empty identity", .json = R"({"schema":1,"profile":"lab-B","serial":[""],"video":[],"audio":[]})", .requested = {LabDeviceKind::SERIAL, ""}}, .expectation = {}}, + {.input = {.name = "unknown policy field", .json = R"({"schema":1,"profile":"lab-B","serial":["port"],"video":[],"audio":[],"allow_all":true})", .requested = {LabDeviceKind::SERIAL, "port"}}, .expectation = {}}, +}; + +std::string check_policy(const PolicyCase& test){ + QTemporaryDir directory; + if (!directory.isValid()){ + return "Cannot create temporary lease directory."; + } + int opens = 0; + std::string refusal; + try{ + const auto policy = parse_lab_device_policy(test.input.json, "lab-B"); + auto lease = acquire_lab_device(policy, directory.path().toStdString(), test.input.requested); + opens++; // Scripted native open: no real port, camera or microphone exists. + }catch (const std::runtime_error& error){ + refusal = error.what(); + } + if (opens != test.expectation.opens){ + return "Expected opens " + std::to_string(test.expectation.opens) + "; read " + std::to_string(opens); + } + if (!refusal.starts_with(test.expectation.refusal)){ + return "Expected a refusal starting \"" + std::string(test.expectation.refusal) + "\"; read \"" + refusal + "\""; + } + return {}; +} + +enum class LeaseScenario{ SAME_PROCESS, DIFFERENT_DEVICE, DIFFERENT_KIND, RELEASE, FAILED_OPEN, LONG_RUNNING, LIVE_CHILD, DEAD_CHILD, PROFILE, EARLY_CHILD }; + +struct LeaseInput{ + std::string_view name; + LeaseScenario scenario; +}; + +struct LeaseExpectation{ + bool second_opens; +}; + +struct LeaseCase{ + LeaseInput input; + LeaseExpectation expectation; +}; + +const LeaseCase LEASE_CASES[] = { + {.input = {.name = "same device two profiles", .scenario = LeaseScenario::SAME_PROCESS}, .expectation = {.second_opens = false}}, + {.input = {.name = "different consoles", .scenario = LeaseScenario::DIFFERENT_DEVICE}, .expectation = {.second_opens = true}}, + {.input = {.name = "video and audio inputs", .scenario = LeaseScenario::DIFFERENT_KIND}, .expectation = {.second_opens = true}}, + {.input = {.name = "disconnect then reconnect", .scenario = LeaseScenario::RELEASE}, .expectation = {.second_opens = true}}, + {.input = {.name = "failed native open releases lease", .scenario = LeaseScenario::FAILED_OPEN}, .expectation = {.second_opens = true}}, +#if !defined(_WIN32) + // Windows' live lock prevents editing this timestamp fixture; exercise it on POSIX. + {.input = {.name = "age never steals live lease", .scenario = LeaseScenario::LONG_RUNNING}, .expectation = {.second_opens = false}}, +#endif + {.input = {.name = "live second process", .scenario = LeaseScenario::LIVE_CHILD}, .expectation = {.second_opens = false}}, + {.input = {.name = "profile reserved before Qt startup", .scenario = LeaseScenario::EARLY_CHILD}, .expectation = {.second_opens = false}}, + {.input = {.name = "killed owner recovery", .scenario = LeaseScenario::DEAD_CHILD}, .expectation = {.second_opens = true}}, + {.input = {.name = "duplicate profile", .scenario = LeaseScenario::PROFILE}, .expectation = {.second_opens = false}}, +}; + +std::string test_helper_path(){ + QString name = "LabDeviceAccessTests"; +#if defined(_WIN32) + name += ".exe"; +#endif + return QDir(QCoreApplication::applicationDirPath()).filePath(name).toStdString(); +} + +void start_lease_child(QProcess& child, const std::string& path, LeaseScenario scenario){ + const QString flag = scenario == LeaseScenario::EARLY_CHILD ? "--lease-before-qt" : "--lease-child"; + child.start(QString::fromStdString(test_helper_path()), {flag, QString::fromStdString(path)}); + if (!child.waitForStarted(5000) || !child.waitForReadyRead(5000) || child.readAllStandardOutput().trimmed() != "LOCKED"){ + throw std::runtime_error("Lease test child did not confirm ownership."); + } +} + +std::unique_ptr prepare_owner(LeaseScenario scenario, const std::string& path, QProcess& child){ + if (scenario == LeaseScenario::LIVE_CHILD || scenario == LeaseScenario::DEAD_CHILD || scenario == LeaseScenario::EARLY_CHILD){ + start_lease_child(child, path, scenario); + if (scenario == LeaseScenario::DEAD_CHILD){ + child.kill(); + if (!child.waitForFinished(5000)){ + throw std::runtime_error("Child did not exit after kill."); + } + } + return nullptr; + } + auto owner = std::make_unique(path); + if (scenario == LeaseScenario::FAILED_OPEN){ + try{ + auto opening = std::move(owner); + throw std::runtime_error("Scripted native open failed."); + }catch (const std::runtime_error&){} + } + if (scenario == LeaseScenario::RELEASE){ + owner.reset(); + } + if (scenario == LeaseScenario::LONG_RUNNING){ + QFile lock(QString::fromStdString(path)); + if (!lock.open(QIODevice::ReadOnly) || !lock.setFileTime(QDateTime::currentDateTimeUtc().addDays(-2), QFileDevice::FileModificationTime)){ + throw std::runtime_error("Could not age the lock fixture."); + } + } + return owner; +} + +std::string check_lease(const LeaseCase& test){ + QTemporaryDir directory; + if (!directory.isValid()){ + return "Cannot create temporary lease directory."; + } + LabDevicePolicy policy{"lab-B", {"port-A", "port-B"}, {"port-A"}, {}}; + LabDeviceIdentity first{LabDeviceKind::SERIAL, "port-A"}; + if (test.input.scenario == LeaseScenario::PROFILE){ + first.kind = LabDeviceKind::PROFILE; + } + const std::string root = directory.path().toStdString(); + const std::string path = lab_device_lock_path(root, first); + QProcess child; + try{ + auto owner = prepare_owner(test.input.scenario, path, child); + LabDeviceIdentity second = first; + if (test.input.scenario == LeaseScenario::DIFFERENT_DEVICE){ + second.id = "port-B"; + } + if (test.input.scenario == LeaseScenario::DIFFERENT_KIND){ + second.kind = LabDeviceKind::VIDEO; + } + bool opened = false; + try{ + auto lease = second.kind == LabDeviceKind::PROFILE + ? std::make_unique(lab_device_lock_path(root, second)) + : acquire_lab_device(policy, root, second); + opened = true; + }catch (const std::runtime_error&){} + child.kill(); + child.waitForFinished(5000); + if (opened != test.expectation.second_opens){ + return "Expected second opens " + std::to_string(test.expectation.second_opens) + "; read " + std::to_string(opened); + } + }catch (const std::runtime_error& error){ + child.kill(); + child.waitForFinished(5000); + return error.what(); + } + return {}; +} + +#ifndef PA_LAB_DEVICE_STANDALONE_TESTS +class SerialTest : public UnitTest{ +public: + explicit SerialTest(const SerialCase& test) : UnitTest("LabDevices/Serial/" + std::string(test.input.name)), m_test(test){} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_serial(m_test); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + const SerialCase& m_test; +}; + +class PolicyTest : public UnitTest{ +public: + explicit PolicyTest(const PolicyCase& test) : UnitTest("LabDevices/Policy/" + std::string(test.input.name)), m_test(test){} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_policy(m_test); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + const PolicyCase& m_test; +}; + +class LeaseTest : public UnitTest{ +public: + explicit LeaseTest(const LeaseCase& test) : UnitTest("LabDevices/Lease/" + std::string(test.input.name)), m_test(test){} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_lease(m_test); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + const LeaseCase& m_test; +}; +#endif + +} // namespace + +#ifndef PA_LAB_DEVICE_STANDALONE_TESTS +void add_tests_LabDeviceAccess(UnitTestDatabase& database){ + for (const SerialCase& test : SERIAL_CASES){ + database.add(test); + } + for (const PolicyCase& test : POLICY_CASES){ + database.add(test); + } + for (const LeaseCase& test : LEASE_CASES){ + database.add(test); + } +} +#endif + +} // namespace PokemonAutomation + +#ifdef PA_LAB_DEVICE_STANDALONE_TESTS +// Small Qt-only runner/child process. It never links the GUI or device backends. +int main(int argc, char* argv[]){ + std::unique_ptr early_lease; + if (argc == 3 && std::string_view(argv[1]) == "--lease-before-qt"){ + early_lease = std::make_unique(argv[2]); + } + QCoreApplication application(argc, argv); + if (early_lease){ + std::cout << "LOCKED" << std::endl; + return application.exec(); + }else if (argc == 3 && std::string_view(argv[1]) == "--lease-child"){ + PokemonAutomation::LabDeviceLease lease(argv[2]); + std::cout << "LOCKED" << std::endl; + return application.exec(); + } + if (argc != 1){ + return 2; + } + int failed = 0; + for (const auto& test : PokemonAutomation::SERIAL_CASES){ + const std::string error = PokemonAutomation::check_serial(test); + std::cout << test.input.name << ": " << (error.empty() ? "PASS" : error) << std::endl; + failed += !error.empty(); + } + for (const auto& test : PokemonAutomation::POLICY_CASES){ + const std::string error = PokemonAutomation::check_policy(test); + std::cout << test.input.name << ": " << (error.empty() ? "PASS" : error) << std::endl; + failed += !error.empty(); + } + for (const auto& test : PokemonAutomation::LEASE_CASES){ + const std::string error = PokemonAutomation::check_lease(test); + std::cout << test.input.name << ": " << (error.empty() ? "PASS" : error) << std::endl; + failed += !error.empty(); + } + return failed; +} +#endif diff --git a/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt_Tests.h b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt_Tests.h new file mode 100644 index 0000000000..73a5d516a8 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt_Tests.h @@ -0,0 +1,16 @@ +/* Lab Device Access Tests + * + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_LabDeviceAccessQt_Tests_H +#define PokemonAutomation_LabDeviceAccessQt_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + +// Register offline policy, lifecycle and cross-process lease regression cases. +void add_tests_LabDeviceAccess(UnitTestDatabase& database); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Devices/LabDeviceRuntime.cpp b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceRuntime.cpp new file mode 100644 index 0000000000..d6a7d8af2c --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceRuntime.cpp @@ -0,0 +1,98 @@ +/* Lab Device Runtime + * + * From: https://github.com/PokemonAutomation/ + */ +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "LabDeviceRuntime.h" + +namespace PokemonAutomation{ +namespace{ + +#if defined(__APPLE__) +// Shared by all guarded apps for this user, independent of their profile/bundle. +std::string shared_lease_directory(){ + return (QDir::homePath() + "/Library/Application Support/PokemonAutomation/LabDeviceLeases").toStdString(); +} + +std::string current_serial_identity(std::string_view port){ + const QSerialPortInfo selected(QString::fromUtf8(port.data(), (qsizetype)port.size())); + std::vector devices; + for (const QSerialPortInfo& info : QSerialPortInfo::availablePorts()){ + if (info.hasVendorIdentifier() && info.hasProductIdentifier()){ + devices.emplace_back(LabSerialDevice{ + info.systemLocation().toStdString(), info.vendorIdentifier(), + info.productIdentifier(), info.serialNumber().toStdString() + }); + } + } + return resolve_lab_serial_identity(devices, selected.systemLocation().toStdString()); +} + +const LabDevicePolicy& runtime_policy(){ + static const LabDevicePolicy policy = []{ + const auto path = Filesystem::application_scratch_path() / "lab-devices.json"; + QFile file(QString::fromStdString(path.string())); + if (!file.open(QIODevice::ReadOnly)){ + throw std::runtime_error("Cannot read lab-devices.json; hardware access is disabled."); + } + return parse_lab_device_policy(file.readAll().toStdString(), Filesystem::STARTUP_PROFILE()); + }(); + return policy; +} +#endif + +} // namespace + +std::unique_ptr reserve_lab_profile(){ +#if defined(__APPLE__) + const std::string directory = shared_lease_directory(); + if (!QDir().mkpath(QString::fromStdString(directory))){ + throw std::runtime_error("Cannot create lab profile lease directory."); + } + return std::make_unique(lab_device_lock_path( + directory, {LabDeviceKind::PROFILE, Filesystem::STARTUP_PROFILE()} + )); +#else + return nullptr; +#endif +} + +std::unique_ptr acquire_lab_serial(Logger& logger, std::string_view port){ +#if defined(__APPLE__) + try{ + // Do not trust the UI's cached port list after unplug/replug. + return acquire_lab_device(runtime_policy(), shared_lease_directory(), { + LabDeviceKind::SERIAL, current_serial_identity(port) + }); + }catch (const std::runtime_error& error){ + throw UserSetupError(logger, error.what()); + } +#else + (void)logger; + (void)port; + return nullptr; +#endif +} + +std::unique_ptr acquire_lab_capture(Logger& logger, LabDeviceKind kind, std::string_view id){ +#if defined(__APPLE__) + try{ + return acquire_lab_device(runtime_policy(), shared_lease_directory(), {kind, std::string(id)}); + }catch (const std::runtime_error& error){ + throw UserSetupError(logger, error.what()); + } +#else + (void)logger; + (void)kind; + (void)id; + return nullptr; +#endif +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Devices/LabDeviceRuntime.h b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceRuntime.h new file mode 100644 index 0000000000..5dd5fb88ca --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Devices/LabDeviceRuntime.h @@ -0,0 +1,26 @@ +/* Lab Device Runtime + * + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_LabDeviceRuntime_H +#define PokemonAutomation_LabDeviceRuntime_H + +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "LabDeviceAccessQt.h" + +namespace PokemonAutomation{ + +// Reserve a macOS GUI profile before loading settings; throw std::runtime_error on failure. +// The returned lease must outlive the GUI. Other platforms return no reservation. +std::unique_ptr reserve_lab_profile(); + +// Resolve a fresh USB identity and reserve it before opening the requested port. +// Missing policy/identity or contention throws UserSetupError. Non-macOS is unchanged. +std::unique_ptr acquire_lab_serial(Logger& logger, std::string_view port); + +// Reserve a camera/audio input by the lowercase hex encoding of its Qt device ID. +// Caller holds the lease until after closing the input. Non-macOS is unchanged. +std::unique_ptr acquire_lab_capture(Logger& logger, LabDeviceKind kind, std::string_view id); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h index f9f6f5d1ff..dfc5ead1b4 100644 --- a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h +++ b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation.h @@ -7,12 +7,14 @@ #ifndef PokemonAutomation_HardwareValidation_H #define PokemonAutomation_HardwareValidation_H +#include "CommonFramework/Startup/SetupSettings.h" + namespace PokemonAutomation{ // Check user hardware. -// If the hardware is too old, send a QMessageBox to tell user and return false. -// If the hardware may not be powerful enough, send a QMessageBox to inform user. -bool check_hardware(); +// If the hardware is too old, send a QMessageBox (when `dialogs == SHOW`) and return false. +// If the hardware may not be powerful enough, send a QMessageBox (when `dialogs == SHOW`). +bool check_hardware(StartupDialogs dialogs = StartupDialogs::SHOW); } diff --git a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_arm64.tpp b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_arm64.tpp index 7f716e8136..1d184838d6 100644 --- a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_arm64.tpp +++ b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_arm64.tpp @@ -14,7 +14,8 @@ namespace PokemonAutomation{ -bool check_hardware(){ +bool check_hardware(StartupDialogs dialogs){ + (void)dialogs; // TODO: write computer power check for Apple M-series computers return true; diff --git a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_x86.tpp b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_x86.tpp index 361ea8765c..d34f5a3361 100644 --- a/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_x86.tpp +++ b/SerialPrograms/Source/CommonFramework/Environment/HardwareValidation_x86.tpp @@ -11,36 +11,47 @@ #include "HardwareValidation.h" namespace PokemonAutomation{ +namespace{ + +void show_hardware_warning(const QString& message, StartupDialogs dialogs){ + if (dialogs == StartupDialogs::LOG_ONLY){ + return; + } + QMessageBox box; + box.warning(nullptr, "Warning", message); +} + +} // namespace -bool check_hardware(){ +bool check_hardware(StartupDialogs dialogs){ if (!CPU_CAPABILITY_NATIVE.OK_08_Nehalem){ - QMessageBox box; - box.critical( - nullptr, - "Error", - "This computer is too old to run this program.

" - "Please try a much newer computer.

" - "(Reason: SSE4.2 is required to run this program.)" - ); + if (dialogs == StartupDialogs::SHOW){ + QMessageBox box; + box.critical( + nullptr, + "Error", + "This computer is too old to run this program.

" + "Please try a much newer computer.

" + "(Reason: SSE4.2 is required to run this program.)" + ); + } return false; } if (!CPU_CAPABILITY_NATIVE.OK_13_Haswell){ - QMessageBox box; QString str; str += "This computer may not be powerful enough to run this program.

"; str += "You can continue, but the program may not work correctly.
"; str += "(i.e. Increased error rate. Fail to reliably detect shinies, etc...)

"; str += "(Reason: Computers that lack AVX2 are likely to be too old and too slow to handle this program.)

"; str += "Recommendation: Use a more powerful computer."; - box.warning(nullptr, "Warning", str); + show_hardware_warning(str, dialogs); } ProcessorSpecs specs = get_processor_specs(); if (specs.threads < 4){ - QMessageBox box; QString str; str += "This computer may not be powerful enough to run this program.

"; str += "You can continue, but the program may not work correctly.
"; @@ -49,11 +60,10 @@ bool check_hardware(){ str += QString::number(specs.threads); str += " vcores is a weak CPU.)

"; str += "Recommendation: Use a more powerful computer."; - box.warning(nullptr, "Warning", str); + show_hardware_warning(str, dialogs); } if (specs.base_frequency < 2'000'000'000){ - QMessageBox box; QString str; str += "This computer may not be powerful enough to run this program.

"; str += "You can continue, but the program may not work correctly.
"; @@ -62,7 +72,7 @@ bool check_hardware(){ str += QString::fromStdString(tostr_fixed(specs.base_frequency / 1000000000., 3)); str += " GHz which is very slow.)

"; str += "Recommendation: Use a more powerful computer."; - box.warning(nullptr, "Warning", str); + show_hardware_warning(str, dialogs); } #if (defined _WIN32) || (defined __linux) @@ -71,7 +81,6 @@ bool check_hardware(){ : 0; double efreq = (specs.cores + 0.2 * vcores) * specs.base_frequency; if (efreq < 8'000'000'000){ - QMessageBox box; QString str; str += "This computer may not be powerful enough to run this program.

"; str += "You can continue, but the program may not work correctly.
"; @@ -87,7 +96,7 @@ bool check_hardware(){ str += QString::fromStdString(tostr_fixed(efreq / 1000000000., 3)); str += " GHz

"; str += "Recommendation: Use a more powerful computer."; - box.warning(nullptr, "Warning", str); + show_hardware_warning(str, dialogs); } #endif diff --git a/SerialPrograms/Source/CommonFramework/Globals.h b/SerialPrograms/Source/CommonFramework/Globals.h index c10ac2354f..d737c9605d 100644 --- a/SerialPrograms/Source/CommonFramework/Globals.h +++ b/SerialPrograms/Source/CommonFramework/Globals.h @@ -37,11 +37,6 @@ extern const std::string COMPILER_VERSION; extern const size_t LOG_HISTORY_LINES; -// Whether this executable shows the Qt GUI. Each executable defines it next to its main(). -// e.g. the serial port code only uses the GUI port list and message boxes when it is true. -bool use_qt_ui(); - - enum class ProgramState{ NOT_READY, diff --git a/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost.cpp b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost.cpp new file mode 100644 index 0000000000..f89f02a847 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost.cpp @@ -0,0 +1,343 @@ +/* Headless Host + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if !defined(_WIN32) +#include +#include +#endif +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/Logging/Logger.h" +#include "CommonFramework/PersistentSettings.h" +#include "CommonFramework/ProgramSession.h" +#include "GameConsole/Framework/ConsoleProgramSession.h" +#include "GameConsole/Framework/ConsoleSystemSession.h" +#include "GameConsole/Framework/MultiConsoleProgramSession.h" +#include "GameConsole/Framework/MultiConsoleSystemSession.h" +#include "PanelCatalog.h" +#include "HeadlessHost.h" + +namespace PokemonAutomation{ +namespace{ + +constexpr std::chrono::seconds READINESS_TIMEOUT{10}; +constexpr std::chrono::seconds STOP_TIMEOUT{5}; + +#if !defined(_WIN32) +struct SignalState{ + std::atomic write_fd{-1}; + std::atomic last_signal{0}; +}; + +SignalState& signal_state(){ + static SignalState state; + return state; +} + +void posix_signal_handler(int sig){ + SignalState& s = signal_state(); + s.last_signal.store(sig, std::memory_order_relaxed); + const int fd = s.write_fd.load(std::memory_order_relaxed); + if (fd >= 0){ + const char byte = static_cast(sig); + const ssize_t ignored = ::write(fd, &byte, /*nbyte=*/1); + (void)ignored; + } +} + +class SignalPipe final{ +public: + SignalPipe(Logger& logger, std::function on_signal){ + if (::pipe(m_fds) != 0){ + logger.log("Failed to create signal pipe; SIGINT/SIGTERM will terminate immediately.", COLOR_ORANGE); + return; + } + for (int fd : m_fds){ + const int flags = ::fcntl(fd, F_GETFL, /*arg=*/0); + if (flags >= 0){ + ::fcntl(fd, F_SETFL, flags | O_NONBLOCK); + } + } + signal_state().write_fd.store(m_fds[1], std::memory_order_release); + struct sigaction sa{}; + sa.sa_handler = posix_signal_handler; + sigemptyset(&sa.sa_mask); + m_has_int = (::sigaction(SIGINT, &sa, &m_old_int) == 0); + m_has_term = (::sigaction(SIGTERM, &sa, &m_old_term) == 0); + + m_notifier = std::make_unique(m_fds[0], QSocketNotifier::Read); + QObject::connect(m_notifier.get(), &QSocketNotifier::activated, m_notifier.get(), [this, on_signal = std::move(on_signal)]{ + std::array buf; + while (::read(m_fds[0], buf.data(), buf.size()) > 0){ + } + on_signal(signal_state().last_signal.load(std::memory_order_relaxed)); + }); + } + + ~SignalPipe(){ + if (m_has_int){ + ::sigaction(SIGINT, &m_old_int, nullptr); + } + if (m_has_term){ + ::sigaction(SIGTERM, &m_old_term, nullptr); + } + signal_state().write_fd.store(-1, std::memory_order_release); + m_notifier.reset(); + for (int& fd : m_fds){ + if (fd >= 0){ + ::close(fd); + fd = -1; + } + } + } + +private: + int m_fds[2] = {-1, -1}; + struct sigaction m_old_int{}; + struct sigaction m_old_term{}; + bool m_has_int = false; + bool m_has_term = false; + std::unique_ptr m_notifier; +}; +#endif + +bool system_ready(GameConsole::ConsoleSystemSession& system){ + for (size_t i = 0; i < system.controllers(); i++){ + if (!system.controller(i).ready()){ + return false; + } + } + return true; +} + +bool controller_ready(ProgramSession& program){ + if (auto* single = dynamic_cast(&program)){ + return system_ready(single->system()); + } + if (auto* multi = dynamic_cast(&program)){ + for (size_t i = 0; i < multi->system().active_consoles(); i++){ + if (!system_ready(multi->system()[i])){ + return false; + } + } + } + return true; +} + +class HeadlessProgramObserver final : public ProgramSession::Listener{ +public: + HeadlessProgramObserver(ProgramSession& program, QObject& context, std::function on_stopped) + : m_program(program), m_context(context), m_on_stopped(std::move(on_stopped)) + {} + + bool saw_error() const{ return m_saw_error.load(std::memory_order_acquire); } + + virtual void state_change(ProgramState state) override{ + if (state == ProgramState::STOPPED){ + QMetaObject::invokeMethod(&m_context, [this]{ + if (m_program.current_state() == ProgramState::STOPPED){ + m_on_stopped(); + } + }, Qt::QueuedConnection); + } + } + virtual void stats_update(const StatsTracker*, const StatsTracker*) override{} + virtual void error(const std::string&) override{ m_saw_error.store(true, std::memory_order_release); } + virtual void download_error(const std::string&) override{} + virtual void download_added(std::shared_ptr) override{} + virtual void all_downloads_done() override{} + +private: + ProgramSession& m_program; + QObject& m_context; + std::function m_on_stopped; + std::atomic m_saw_error{false}; +}; + +std::expected merge_options(const JsonValue& base, const JsonValue& over){ + const JsonObject* over_obj = over.to_object(); + if (over_obj == nullptr){ + return std::unexpected("Options file must contain a JSON object."); + } + if (over_obj->get_value("ConsoleSetup") != nullptr){ + return std::unexpected("--options cannot override ConsoleSetup."); + } + JsonObject merged; + if (const JsonObject* base_obj = base.to_object()){ + merged = base_obj->clone(); + } + for (const auto& [key, value] : *over_obj){ + merged[key] = value.clone(); + } + return JsonValue(std::move(merged)); +} + +HeadlessExit load_headless_panel( + const std::vector>& lists, + const HeadlessRunOptions& options, + HeadlessHost& host, + Logger& logger +){ + std::optional match = find_panel(lists, options.program_id); + if (!match){ + logger.log(std::format("Unknown program identifier: \"{}\".", options.program_id), COLOR_RED); + return HeadlessExit::BAD_REQUEST; + } + std::shared_ptr descriptor(std::move(match->entry.descriptor)); + + JsonValue json; + if (options.base_panel_json){ + json = options.base_panel_json->clone(); + }else if (const JsonValue* saved = PERSISTENT_SETTINGS().panels.get_value(descriptor->identifier())){ + json = saved->clone(); + } + if (options.options_override){ + auto merged = merge_options(json, *options.options_override); + if (!merged){ + logger.log(merged.error(), COLOR_RED); + return HeadlessExit::BAD_REQUEST; + } + json = std::move(*merged); + } + + try{ + load_panel_session(descriptor, json, host); + }catch (const UserSetupError& e){ + logger.log(std::format("Device setup error: {}", e.to_str()), COLOR_RED); + return HeadlessExit::DEVICE; + }catch (const Exception& e){ + logger.log(std::format("Failed to load program: {}", e.to_str()), COLOR_RED); + return HeadlessExit::BAD_REQUEST; + } + return HeadlessExit::OK; +} + +bool wait_for_controller_ready(ProgramSession& program){ + if (controller_ready(program)){ + return true; + } + QEventLoop loop; + QTimer poll; + QTimer timeout; + timeout.setSingleShot(true); + QObject::connect(&poll, &QTimer::timeout, &loop, [&]{ + if (controller_ready(program)){ + loop.quit(); + } + }); + QObject::connect(&timeout, &QTimer::timeout, &loop, [&]{ loop.quit(); }); + poll.start(std::chrono::milliseconds(50)); + timeout.start(READINESS_TIMEOUT); + loop.exec(); + return controller_ready(program); +} + +HeadlessExit run_program_event_loop(ProgramSession& program, bool handle_signals, Logger& logger){ + QEventLoop loop; + std::optional signal_exit; + QTimer stop_timer; + stop_timer.setSingleShot(true); + + HeadlessProgramObserver observer(program, loop, [&]{ loop.quit(); }); + program.add_listener(observer); + +#if !defined(_WIN32) + std::unique_ptr pipe; + if (handle_signals){ + pipe = std::make_unique(logger, [&](int sig){ + const HeadlessExit code = sig == SIGTERM ? HeadlessExit::SIGNAL_TERM : HeadlessExit::SIGNAL_INT; + if (signal_exit){ + logger.log("Second signal received; exiting immediately.", COLOR_RED); + std::_Exit(static_cast(code)); + } + signal_exit = code; + logger.log("Signal received; stopping program...", COLOR_ORANGE); + program.stop_program(); + QObject::connect(&stop_timer, &QTimer::timeout, &loop, [&logger, code]{ + logger.log("Stop timeout expired; exiting immediately.", COLOR_RED); + std::_Exit(static_cast(code)); + }); + stop_timer.start(STOP_TIMEOUT); + }); + } +#else + (void)handle_signals; +#endif + + const std::string err = program.start_program(); + if (!err.empty()){ + logger.log(std::format("Failed to start program: {}", err), COLOR_RED); + program.remove_listener(observer); + return HeadlessExit::DEVICE; + } + if (program.current_state() != ProgramState::STOPPED){ + loop.exec(); + } + program.remove_listener(observer); + if (signal_exit){ + return *signal_exit; + } + return observer.saw_error() ? HeadlessExit::PROGRAM_ERROR : HeadlessExit::OK; +} + +} // namespace + + +void HeadlessHost::clear_panel(){ + m_current_panel.reset(); + m_current_panel_descriptor.reset(); +} + +void HeadlessHost::load_panel( + std::shared_ptr descriptor, + std::unique_ptr panel +){ + clear_panel(); + m_current_panel_descriptor = std::move(descriptor); + m_current_panel = std::move(panel); +} + +HeadlessExit run_headless_program( + const std::vector>& lists, + const HeadlessRunOptions& options +){ + Logger& logger = global_logger_tagged(); + HeadlessHost host; + const HeadlessExit load_status = load_headless_panel(lists, options, host, logger); + if (load_status != HeadlessExit::OK){ + return load_status; + } + ProgramSession* program = dynamic_cast(host.current_panel()); + if (program == nullptr){ + logger.log(std::format("Panel \"{}\" is not a runnable program.", options.program_id), COLOR_RED); + host.clear_panel(); + return HeadlessExit::BAD_REQUEST; + } + if (!wait_for_controller_ready(*program)){ + logger.log("Controller did not become ready before timeout.", COLOR_RED); + host.clear_panel(); + return HeadlessExit::DEVICE; + } + const HeadlessExit ret = run_program_event_loop(*program, options.handle_signals, logger); + host.clear_panel(); + return ret; +} + + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost.h b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost.h new file mode 100644 index 0000000000..28753ea339 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost.h @@ -0,0 +1,72 @@ +/* Headless Host + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_HeadlessHost_H +#define PokemonAutomation_HeadlessHost_H + +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonFramework/Panels/PanelList.h" +#include "CommonFramework/Panels/PanelSession.h" +#include "CommonFramework/Panels/PanelTools.h" + +namespace PokemonAutomation{ + + +// Process exit codes returned by --headless --run. +enum class HeadlessExit : int{ + OK = 0, + PROGRAM_ERROR = 1, + STARTUP = 3, + BAD_REQUEST = 4, + DEVICE = 5, + SIGNAL_INT = 130, + SIGNAL_TERM = 143, +}; + + +// Widgetless PanelHolder for --headless runs. Never calls make_ui_component() +// or writes persistent settings. +class HeadlessHost final : public PanelHolder{ +public: + PanelSession* current_panel() const{ return m_current_panel.get(); } + + void clear_panel(); + + virtual bool report_new_panel_intent(const PanelDescriptor&) override{ return true; } + virtual void load_panel( + std::shared_ptr descriptor, + std::unique_ptr panel + ) override; + virtual void on_busy() override{} + virtual void on_idle() override{} + +private: + std::shared_ptr m_current_panel_descriptor; + std::unique_ptr m_current_panel; +}; + + +struct HeadlessRunOptions{ + std::string program_id; + std::optional options_override = std::nullopt; + std::optional base_panel_json = std::nullopt; + bool handle_signals = true; +}; + + +// Run one program from `lists` to completion under QCoreApplication::exec(). +HeadlessExit run_headless_program( + const std::vector>& lists, + const HeadlessRunOptions& options +); + + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost_Tests.cpp b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost_Tests.cpp new file mode 100644 index 0000000000..e7ef7d9d99 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost_Tests.cpp @@ -0,0 +1,138 @@ +/* Headless Host Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Panels/PanelDescriptor.h" +#include "CommonFramework/Panels/PanelList.h" +#include "CommonFramework/Panels/PanelSession.h" +#include "ComputerPrograms/ComputerProgram.h" +#include "HeadlessHost.h" +#include "HeadlessHost_Tests.h" + +namespace PokemonAutomation{ +namespace{ + +enum class FakeBehavior{SUCCEED, REPORT_ERROR, REFUSE_START}; + +class FakeInstance final : public ComputerProgramInstance{ +public: + explicit FakeInstance(FakeBehavior b) : m_b(b) {} + virtual std::string check_validity() const override{ + return m_b == FakeBehavior::REFUSE_START ? "invalid configuration" : ""; + } + virtual void program(ProgramEnvironment&, CancellableScope&) override{ + if (m_b == FakeBehavior::REPORT_ERROR){ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "simulated failure"); + } + } +private: + FakeBehavior m_b; +}; + +class FakeProgramDescriptor final : public ComputerProgramDescriptor{ +public: + FakeProgramDescriptor(std::string id, FakeBehavior b) + : ComputerProgramDescriptor(std::move(id), "Fake", "FakeProgram", /*doc_link=*/"", /*description=*/"", PanelQualityStanding::DEVELOPER) + , m_b(b) + {} + virtual std::unique_ptr make_instance() const override{ + return std::make_unique(m_b); + } +private: + FakeBehavior m_b; +}; + +class FakeSettingsDescriptor final : public PanelDescriptor{ +public: + explicit FakeSettingsDescriptor(std::string id) + : PanelDescriptor(std::move(id), "Fake", "FakeSettings", /*doc_link=*/"", /*description=*/"", PanelQualityStanding::DEVELOPER, /*restore_defaults_button=*/false) + {} + virtual std::unique_ptr make_panel() const override{ + return std::make_unique(*this); + } +}; + +class FakeListDescriptor final : public PanelListDescriptor{ +public: + FakeListDescriptor() : PanelListDescriptor("Fake") {} +protected: + virtual std::vector make_panels() const override{ + std::vector ret; + ret.emplace_back(std::make_unique("Fake:Ok", FakeBehavior::SUCCEED)); + ret.emplace_back(std::make_unique("Fake:Err", FakeBehavior::REPORT_ERROR)); + ret.emplace_back(std::make_unique("Fake:Refused", FakeBehavior::REFUSE_START)); + ret.emplace_back(std::make_unique("Fake:Settings")); + return ret; + } +}; + +struct RunInput{ + std::string_view program_id; + std::optional options_json = std::nullopt; +}; + +struct RunExpectation{ + HeadlessExit exit_code; +}; + +struct RunCase{ + std::string_view name; + RunInput input; + RunExpectation expectation; +}; + +const RunCase RUN_CASES[] = { + {.name = "clean program finishes 0", .input = {.program_id = "Fake:Ok"}, .expectation = {.exit_code = HeadlessExit::OK}}, + {.name = "program error finishes 1", .input = {.program_id = "Fake:Err"}, .expectation = {.exit_code = HeadlessExit::PROGRAM_ERROR}}, + {.name = "unknown identifier finishes 4", .input = {.program_id = "Fake:Missing"}, .expectation = {.exit_code = HeadlessExit::BAD_REQUEST}}, + {.name = "settings panel finishes 4", .input = {.program_id = "Fake:Settings"}, .expectation = {.exit_code = HeadlessExit::BAD_REQUEST}}, + {.name = "refused start finishes 5", .input = {.program_id = "Fake:Refused"}, .expectation = {.exit_code = HeadlessExit::DEVICE}}, + {.name = "ConsoleSetup override finishes 4", .input = {.program_id = "Fake:Ok", .options_json = "{\"ConsoleSetup\": {}}"}, .expectation = {.exit_code = HeadlessExit::BAD_REQUEST}}, + {.name = "non-object override finishes 4", .input = {.program_id = "Fake:Ok", .options_json = "42"}, .expectation = {.exit_code = HeadlessExit::BAD_REQUEST}}, +}; + +UnitTestResult check_run_case(const RunCase& test){ + std::vector> lists; + lists.push_back(std::make_unique()); + HeadlessRunOptions options{ + .program_id = std::string(test.input.program_id), + .base_panel_json = JsonValue(), + .handle_signals = false, + }; + if (test.input.options_json){ + options.options_override = parse_json(std::string(*test.input.options_json)); + } + const HeadlessExit code = run_headless_program(lists, options); + if (code != test.expectation.exit_code){ + return std::format("Expected exit code {}; got {}.", static_cast(test.expectation.exit_code), static_cast(code)); + } + return true; +} + +class RunTest final : public UnitTest{ +public: + explicit RunTest(const RunCase& test) + : UnitTest(std::format("HeadlessHost/Run/{}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_run_case(m_test); + } +private: + const RunCase& m_test; +}; + +} // namespace + +void add_tests_HeadlessHost(UnitTestDatabase& database){ + for (const RunCase& test : RUN_CASES){ + database.add(test); + } +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost_Tests.h b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost_Tests.h new file mode 100644 index 0000000000..6107fb361f --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Headless/HeadlessHost_Tests.h @@ -0,0 +1,17 @@ +/* Headless Host Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_HeadlessHost_Tests_H +#define PokemonAutomation_HeadlessHost_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + +void add_tests_HeadlessHost(UnitTestDatabase& database); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 0e7bb7fd3f..2d98b9b095 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -1,5 +1,9 @@ #include +#include +#include +#include +#include //#include #include #include "Common/Cpp/Logging/LastLogTracker.h" @@ -17,7 +21,9 @@ #include "Common/Cpp/ScopeExit.h" #include "Common/Qt/GlobalThreadPoolsQt.h" #include "StaticRegistration.h" +#include "CommonFramework/Devices/LabDeviceRuntime.h" #include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/Headless/HeadlessHost.h" #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Panels/OptionsPanel.h" #include "CommonFramework/VideoPipeline/Backends/CameraImplementations.h" @@ -38,22 +44,20 @@ #include "Logging/Logger.h" #include "Logging/OutputRedirector.h" #include "Startup/SetupSettings.h" +#include "Startup/StartupArguments.h" +#include "Startup/LabStartupPolicy.h" #include "Startup/NewVersionCheck.h" #include "Windows/MainWindow.h" #include "GlobalSettingsPanel.h" #include "GlobalServices.h" #include "PersistentSettings.h" +#include "PanelCatalog.h" #include "NintendoSwitch/NintendoSwitch_SettingsPanel.h" -using namespace PokemonAutomation; +Q_DECLARE_METATYPE(std::string) namespace PokemonAutomation{ - bool use_qt_ui(){ - return true; - } -} - -Q_DECLARE_METATYPE(std::string) +namespace{ void set_working_directory(){ const std::string app_dir = Filesystem::application_binary_directory().string_slash_normalized(); @@ -69,10 +73,6 @@ void set_working_directory(){ } } -namespace PokemonAutomation{ - - - FileLogger& global_file_logger(){ static FileLogger logger( GlobalThreadPools::unlimited_normal(), @@ -83,19 +83,47 @@ FileLogger& global_file_logger(){ return logger; } -} +bool init_common_runtime(Logger& logger, StartupDialogs dialogs, bool fail_on_settings_error){ + qRegisterMetaType("size_t"); + qRegisterMetaType("uint8_t"); + qRegisterMetaType("std::string"); + qRegisterMetaType("Resolution"); + + Filesystem::create_directories(Filesystem::Path(SETTINGS_PATH())); + Filesystem::create_directories(Filesystem::Path(SCREENSHOTS_PATH())); + + // Several novice developers struggled to build and run the program due to missing Resources folder. + // Add this check to pop a message box when Resources folder is missing. + if (!check_resource_folder(logger, dialogs)){ + return false; + } + + // Read program settings from json file: SerialPrograms-Settings.json. + try{ + PERSISTENT_SETTINGS().read(); + }catch (const FileException& error){ + logger.log(error.message(), COLOR_RED); + if (fail_on_settings_error){ + return false; + } + }catch (const ParseException& error){ + logger.log(error.message(), COLOR_RED); + if (fail_on_settings_error){ + return false; + } + } + + GlobalThreadPools::qt_worker_threadpool(); + GlobalThreadPools::qt_event_threadpool(); + return true; +} int run_program(int argc, char *argv[]){ // qputenv("QT_RHI_BACKEND", "opengl"); -#if defined(__APPLE__) - Filesystem::set_startup_profile(argc, argv); QApplication application(argc, argv); -#else - QApplication application(argc, argv); -#endif #ifdef PA_DPP // Configure OpenSSL before DPP or background tasks create a TLS context. @@ -167,75 +195,28 @@ int run_program(int argc, char *argv[]){ logger.log(dpp_cert_message, dpp_cert_error ? COLOR_RED : Color()); #endif - qRegisterMetaType("size_t"); - qRegisterMetaType("uint8_t"); - qRegisterMetaType("std::string"); - qRegisterMetaType("Resolution"); - #if defined(__linux) || defined(__APPLE__) // By default Qt uses native menubar but this only works on Windows. // We use menubar in our ButtonDiagram window to choose which controller's button mapping image to show. // So we fix it by don't using native menubar on non-Windows OS. QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar); #endif - -//#if QT_VERSION_MAJOR == 5 // AA_EnableHighDpiScaling is deprecated in Qt6 -// QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); -//#endif - - Filesystem::create_directories(Filesystem::Path(SETTINGS_PATH())); - Filesystem::create_directories(Filesystem::Path(SCREENSHOTS_PATH())); - - + if (!init_common_runtime(logger, StartupDialogs::SHOW, /*fail_on_settings_error=*/false)){ + return 1; + } + if (GlobalSettings::instance().COMMAND_LINE_TEST_MODE){ + return run_command_line_tests(false); + } ScopeExit cleanup([]{ SerialPortPoller::instance().stop(); GlobalMediaServices::instance().stop(); }); - // Several novice developers struggled to build and run the program due to missing Resources folder. - // Add this check to pop a message box when Resources folder is missing. - if (!check_resource_folder(logger)){ - return 1; - } - - // Read program settings from json file: SerialPrograms-Settings.json. - try{ - PERSISTENT_SETTINGS().read(); - }catch (const FileException& error){ - logger.log(error.message(), COLOR_RED); - }catch (const ParseException& error){ - logger.log(error.message(), COLOR_RED); - } - - // Load the Switch framework settings now, not only when their panel is opened. OptionsPanelWrapper().make_panel()->load_json_from_global(); - - // Preload a bunch of stuff now so they are ready later. SerialPortPoller::instance().ports(); get_all_cameras(); - // Force all the Qt thread pools to be constructed now on the main thread. - GlobalThreadPools::qt_worker_threadpool(); - GlobalThreadPools::qt_event_threadpool(); - - bool augment_test_color_distortions = false; - for (size_t i = 0; i < argc; i++){ - constexpr const char* force_run_tests = "--command-line-test-mode"; - constexpr const char* distort_test_images = "--augment-color-distortions"; - - if (strcmp(argv[i], force_run_tests) == 0){ - GlobalSettings::instance().COMMAND_LINE_TEST_MODE = true; - } - if (strcmp(argv[i], distort_test_images) == 0){ - augment_test_color_distortions = true; - } - } - - if (GlobalSettings::instance().COMMAND_LINE_TEST_MODE){ - return run_command_line_tests(augment_test_color_distortions); - } - // Check whether the hardware is powerful enough to run this program. if (!check_hardware()){ return 1; @@ -280,29 +261,8 @@ int run_program(int argc, char *argv[]){ } -int main(int argc, char *argv[]){ -#if 0 - // Retrieve and store program name - set_program_path(argv[0]); -#endif - -#if defined(__linux__) - // Qt multimedia, default to gstreamer to prevent flickering - // Easier than the alternative which is compiling qt6multimedia with QT_DEFAULT_MEDIA_BACKEND - // See: https://doc.qt.io/qt-6.5/qtmultimedia-index.html - if (qEnvironmentVariableIsEmpty("QT_MEDIA_BACKEND")){ - qputenv("QT_MEDIA_BACKEND", "gstreamer"); - } -#endif - - // So far, this is only needed on Mac where static initialization is fucked up. - PokemonAutomation::register_all_statics(); - - setup_crash_handler(); - - int ret = run_program(argc, argv); - - +// Stop GUI services after run_program() returns, preserving the existing order. +void stop_application_services(){ #ifdef PA_SOCIAL_SDK Integration::DiscordSocialSDK::DiscordSocial::instance().stop(); #endif @@ -325,6 +285,47 @@ int main(int argc, char *argv[]){ global_watchdog().stop(); global_file_logger().stop(); +} + +// Join the worker pools after service shutdown and the Windows Qt workaround. +void stop_thread_pools(){ + // Force stop the thread pools. + // This is where all the threads in the program are joined. + PokemonAutomation::GlobalThreadPools::computation_realtime().stop(); + PokemonAutomation::GlobalThreadPools::computation_normal().stop(); + PokemonAutomation::GlobalThreadPools::unlimited_realtime().stop(); + PokemonAutomation::GlobalThreadPools::unlimited_normal().stop(); + PokemonAutomation::global_dispatcher.stop(); + + GlobalThreadPools::qt_worker_threadpool().stop(); + GlobalThreadPools::qt_event_threadpool().stop(); + +} + +int run_gui_application(int argc, char *argv[]){ +#if 0 + // Retrieve and store program name + set_program_path(argv[0]); +#endif + +#if defined(__linux__) + // Qt multimedia, default to gstreamer to prevent flickering + // Easier than the alternative which is compiling qt6multimedia with QT_DEFAULT_MEDIA_BACKEND + // See: https://doc.qt.io/qt-6.5/qtmultimedia-index.html + if (qEnvironmentVariableIsEmpty("QT_MEDIA_BACKEND")){ + qputenv("QT_MEDIA_BACKEND", "gstreamer"); + } +#endif + + // So far, this is only needed on Mac where static initialization is fucked up. + PokemonAutomation::register_all_statics(); + + setup_crash_handler(); + + int ret = run_program(argc, argv); + + + stop_application_services(); // // Workaround Qt 6.9 thread-adoption bug on Windows. // https://github.com/PokemonAutomation/Arduino-Source/issues/570 @@ -337,18 +338,162 @@ int main(int argc, char *argv[]){ std::quick_exit(ret); #endif - // Force stop the thread pools. - // This is where all the threads in the program are joined. - PokemonAutomation::GlobalThreadPools::computation_realtime().stop(); - PokemonAutomation::GlobalThreadPools::computation_normal().stop(); - PokemonAutomation::GlobalThreadPools::unlimited_realtime().stop(); - PokemonAutomation::GlobalThreadPools::unlimited_normal().stop(); - PokemonAutomation::global_dispatcher.stop(); - - GlobalThreadPools::qt_worker_threadpool().stop(); - GlobalThreadPools::qt_event_threadpool().stop(); + stop_thread_pools(); std::cout << "Exiting main()..." << std::endl; return ret; } + +bool load_headless_options(const StartupPlan& plan, Logger& logger, std::optional& out){ + if (!plan.options_path){ + return true; + } + try{ + out = load_json_file(*plan.options_path); + return true; + }catch (const Exception& e){ + logger.log("Failed to read --options \"" + *plan.options_path + "\": " + e.to_str(), COLOR_RED); + return false; + } +} + +int run_headless_application(QApplication& application, const StartupPlan& plan){ + ScopeExit service_cleanup([]{ + stop_application_services(); + stop_thread_pools(); + }); + + GlobalOutputRedirector redirect_stdout(std::cout, "stdout", Color()); + GlobalOutputRedirector redirect_stderr(std::cerr, "stderr", COLOR_RED); + { + MultiOutputLogger& logger = global_multi_logger(); + logger.add_listener(global_last_log_history()); + logger.add_listener(global_file_logger()); + } + + Logger& logger = global_logger_tagged(); + logger.log("================================================================================"); + logger.log("Starting Headless Program: " + *plan.run_program); + + std::optional options_override; + if (!load_headless_options(plan, logger, options_override)){ + return static_cast(HeadlessExit::BAD_REQUEST); + } + + if (!init_common_runtime(logger, StartupDialogs::LOG_ONLY, /*fail_on_settings_error=*/true)){ + return static_cast(HeadlessExit::STARTUP); + } + + ScopeExit device_cleanup([]{ + SerialPortPoller::instance().stop(); + GlobalMediaServices::instance().stop(); + }); + OptionsPanelWrapper().make_panel()->load_json_from_global(); + if (!check_hardware(StartupDialogs::LOG_ONLY)){ + return static_cast(HeadlessExit::STARTUP); + } + + set_working_directory(); + set_permissions(application); + return static_cast(run_headless_program( + make_all_panel_lists(), + HeadlessRunOptions{ + .program_id = *plan.run_program, + .options_override = std::move(options_override), + .handle_signals = true, + } + )); +} + +// Explicit tests use QCoreApplication and never enter GUI/settings/device startup. +int run_test_application(int argc, char* argv[], const StartupPlan& plan){ +#if defined(__APPLE__) + Filesystem::set_startup_profile("unit-tests"); +#endif + QCoreApplication application(argc, argv); + register_all_statics(); + GlobalThreadPools::qt_worker_threadpool(); + GlobalThreadPools::qt_event_threadpool(); + const int ret = run_command_line_tests(plan.augment_color_distortions); + OCR::clear_ocr_cache(); + global_periodic_runner().stop(); + global_watchdog().stop(); +#ifdef PA_ENABLE_QT_ADOPTION_WORKAROUND + std::quick_exit(ret); +#endif + stop_thread_pools(); + return ret; +} + +int run_with_profile_lease( + const PokemonAutomation::StartupPlan& selected, + int lease_error_exit, + const std::function& launch +){ +#if defined(__APPLE__) + PokemonAutomation::Filesystem::set_startup_profile(*selected.profile); +#else + (void)selected; +#endif + std::unique_ptr profile_lease; + try{ + profile_lease = PokemonAutomation::reserve_lab_profile(); + }catch (const std::runtime_error& error){ + std::cerr << error.what() << std::endl; + return lease_error_exit; + } + return launch(); +} + +} // namespace +} // namespace PokemonAutomation + +// Validate before even constructing Qt: a typo must never become a hardware launch. +int main(int argc, char* argv[]){ + std::vector arguments; + for (int i = 1; i < argc; i++){ + arguments.emplace_back(argv[i]); + } + PokemonAutomation::LabStartupPolicy policy; +#if defined(__APPLE__) + policy.profiles = PokemonAutomation::LabProfileMode::REQUIRED; +#ifdef PA_LAB_PROFILE + policy.bound_profile = PA_LAB_PROFILE; +#endif +#endif + const PokemonAutomation::StartupPlan plan = PokemonAutomation::apply_lab_startup_policy(PokemonAutomation::parse_startup_arguments(arguments), policy); + if (plan.mode == PokemonAutomation::StartupMode::REJECTED){ + std::cerr << plan.error << std::endl; + } + // Keep backing storage alive for Qt, which may mutate argc and argv. + std::vector qt_storage = plan.qt_arguments; + qt_storage.insert(qt_storage.begin(), argv[0]); + std::vector qt_argv; + for (std::string& arg : qt_storage){ + qt_argv.push_back(arg.data()); + } + int qt_argc = (int)qt_argv.size(); + qt_argv.push_back(nullptr); + return PokemonAutomation::dispatch_startup(plan, { + .gui = [&](const PokemonAutomation::StartupPlan& selected){ + return PokemonAutomation::run_with_profile_lease(selected, 2, [&]{ + return PokemonAutomation::run_gui_application(qt_argc, qt_argv.data()); + }); + }, + .headless = [&](const PokemonAutomation::StartupPlan& selected){ + return PokemonAutomation::run_with_profile_lease( + selected, static_cast(PokemonAutomation::HeadlessExit::STARTUP), + [&]{ + PokemonAutomation::register_all_statics(); + PokemonAutomation::setup_crash_handler(); + QApplication application(qt_argc, qt_argv.data()); + return PokemonAutomation::run_headless_application(application, selected); + } + ); + }, + .tests = [&](const PokemonAutomation::StartupPlan& selected){ + return PokemonAutomation::run_test_application(qt_argc, qt_argv.data(), selected); + }, + }); +} diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp index ae16768c86..0ebce8d7e7 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp @@ -6,6 +6,7 @@ #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Logging/Logger.h" +#include "CommonFramework/Panels/PanelSession.h" #include "PanelList.h" //#include @@ -33,6 +34,19 @@ PanelListDescriptor::PanelListDescriptor( } } +bool load_panel_session( + const std::shared_ptr& descriptor, + const JsonValue& panel_json, + PanelHolder& holder +){ + if (!holder.report_new_panel_intent(*descriptor)){ + return false; + } + std::unique_ptr panel = descriptor->make_panel(); + panel->load_json(panel_json); + holder.load_panel(descriptor, std::move(panel)); + return true; +} diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h index 995b028763..0ab5f36dca 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_PanelList_H #define PokemonAutomation_PanelList_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Panels/PanelTools.h" @@ -35,6 +36,9 @@ class PanelListDescriptor{ PanelListWidget* make_QWidget(QWidget& parent, PanelHolder& holder) const; + // The entries of the list, as the widget shows them. + std::vector panels() const{ return make_panels(); } + protected: virtual std::vector make_panels() const = 0; @@ -45,6 +49,16 @@ class PanelListDescriptor{ }; +// Instantiate `descriptor` (non-null), load `panel_json` into it, and install +// it in `holder`. Returns false, loading nothing, if +// `holder.report_new_panel_intent()` declines. +bool load_panel_session( + const std::shared_ptr& descriptor, + const JsonValue& panel_json, + PanelHolder& holder +); + + } #endif diff --git a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp index 387ca75331..277bdfebba 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp @@ -93,18 +93,16 @@ void PanelListWidget::handle_panel_clicked(const std::string& text){ PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = ""; return; } - std::shared_ptr& descriptor = iter->second; + const std::shared_ptr& descriptor = iter->second; // cout << descriptor->display_name() << endl; - if (!m_panel_holder.report_new_panel_intent(*descriptor)){ - return; - } try{ - std::unique_ptr panel = descriptor->make_panel(); -// try{ - panel->load_json(PERSISTENT_SETTINGS().panels[descriptor->identifier()]); -// }catch (ParseException&){} - m_panel_holder.load_panel(descriptor, std::move(panel)); - + if (!load_panel_session( + descriptor, + PERSISTENT_SETTINGS().panels[descriptor->identifier()], + m_panel_holder + )){ + return; + } PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = iter->first; }catch (const Exception& error){ QMessageBox box; diff --git a/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.cpp b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.cpp new file mode 100644 index 0000000000..710ece1b0c --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.cpp @@ -0,0 +1,35 @@ +/* Lab Startup Policy + * + * From: https://github.com/PokemonAutomation/ + */ +#include "LabStartupPolicy.h" + +namespace PokemonAutomation{ + +StartupPlan apply_lab_startup_policy(StartupPlan plan, const LabStartupPolicy& policy){ + if (plan.mode != StartupMode::GUI && plan.mode != StartupMode::HEADLESS){ + return plan; + } + if (policy.profiles == LabProfileMode::UNSUPPORTED){ + if (plan.mode == StartupMode::HEADLESS){ + return {.error = "--headless is supported only on macOS."}; + } + if (plan.profile || policy.bound_profile){ + return {.error = "Settings profiles are supported only on macOS."}; + } + return plan; + } + if (policy.bound_profile){ + if (!valid_startup_profile(*policy.bound_profile) || + (plan.profile && plan.profile != policy.bound_profile)){ + return {.error = "The requested profile does not match this lab build."}; + } + plan.profile = policy.bound_profile; + } + if (!plan.profile){ + return {.error = "Lab launch requires --profile NAME or a build configured with PA_LAB_PROFILE."}; + } + return plan; +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.h b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.h new file mode 100644 index 0000000000..f8566c3998 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.h @@ -0,0 +1,25 @@ +/* Lab Startup Policy + * + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_LabStartupPolicy_H +#define PokemonAutomation_LabStartupPolicy_H +#include "StartupArguments.h" + +namespace PokemonAutomation{ + +// Profiles are supported by the macOS settings backend, and required for lab GUIs. +enum class LabProfileMode{UNSUPPORTED, REQUIRED}; + +// Lab-only restrictions, independent of the general application argument grammar. +struct LabStartupPolicy{ + LabProfileMode profiles = LabProfileMode::UNSUPPORTED; + std::optional bound_profile = std::nullopt; +}; + +// Apply lab identity restrictions after parsing. Rejected plans stay rejected; +// tests bypass console identity, while GUI launches must satisfy the lab policy. +StartupPlan apply_lab_startup_policy(StartupPlan plan, const LabStartupPolicy& policy); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.cpp b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.cpp new file mode 100644 index 0000000000..411fda9c49 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.cpp @@ -0,0 +1,150 @@ +/* Lab Startup Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Regression context: fork PR #226, 01e665911, lab controller/permissions lesson. + * A typo in a test command must never restore a saved controller panel. + */ +#include +#include +#include "LabStartupPolicy.h" +#include "LabStartupPolicy_Tests.h" + +namespace PokemonAutomation{ +namespace{ + +struct LabStartupInput{ + std::string_view name; + std::vector arguments; + LabStartupPolicy policy; +}; + +struct LabStartupExpectation{ + StartupMode mode; + std::optional profile = std::nullopt; + bool augment = false; + int gui_calls = 0; + int headless_calls = 0; + int test_calls = 0; + int exit_code = 2; + std::vector qt_arguments = {}; +}; + +struct LabStartupCase{ + LabStartupInput input; + LabStartupExpectation expectation; +}; + +const LabStartupCase LAB_STARTUP_CASES[] = { + {.input = {.name = "unbound relaunch", .arguments = {}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "bound relaunch", .arguments = {}, .policy = {LabProfileMode::REQUIRED, "lab-B"}}, + .expectation = {.mode = StartupMode::GUI, .profile = "lab-B", .gui_calls = 1, .exit_code = 17}}, + {.input = {.name = "explicit identity", .arguments = {"--profile", "lab-B"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::GUI, .profile = "lab-B", .gui_calls = 1, .exit_code = 17}}, + {.input = {.name = "matching identity", .arguments = {"--profile", "lab-B"}, .policy = {LabProfileMode::REQUIRED, "lab-B"}}, + .expectation = {.mode = StartupMode::GUI, .profile = "lab-B", .gui_calls = 1, .exit_code = 17}}, + {.input = {.name = "wrong console", .arguments = {"--profile", "lab-A"}, .policy = {LabProfileMode::REQUIRED, "lab-B"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "missing profile", .arguments = {"--profile"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "empty profile", .arguments = {"--profile", ""}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "duplicate profile", .arguments = {"--profile", "lab-B", "--profile", "lab-B"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "path traversal", .arguments = {"--profile", "../lab-A"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "absolute profile", .arguments = {"--profile", "/lab-A"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "space in profile", .arguments = {"--profile", "lab B"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "unicode profile", .arguments = {"--profile", "lab-é"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "option as profile", .arguments = {"--profile", "--command-line-test-mode"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "test typo from incident", .arguments = {"--test", "bdsp"}, .policy = {LabProfileMode::REQUIRED, "lab-B"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "unknown after test flag", .arguments = {"--command-line-test-mode", "bdsp"}, .policy = {}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "unsupported filter", .arguments = {"--command-line-test-mode", "--test-filter=bdsp"}, .policy = {}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "test bypasses profile requirement", .arguments = {"--command-line-test-mode"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::TESTS, .test_calls = 1, .exit_code = 23}}, + {.input = {.name = "test bypasses bound console", .arguments = {"--command-line-test-mode"}, .policy = {LabProfileMode::REQUIRED, "lab-B"}}, + .expectation = {.mode = StartupMode::TESTS, .test_calls = 1, .exit_code = 23}}, + {.input = {.name = "augmented tests", .arguments = {"--augment-color-distortions", "--command-line-test-mode"}, .policy = {}}, + .expectation = {.mode = StartupMode::TESTS, .augment = true, .test_calls = 1, .exit_code = 23}}, + {.input = {.name = "augmentation without tests", .arguments = {"--augment-color-distortions"}, .policy = {}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "tests cannot select live profile", .arguments = {"--command-line-test-mode", "--profile", "lab-A"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "non mac GUI unchanged", .arguments = {}, .policy = {}}, + .expectation = {.mode = StartupMode::GUI, .gui_calls = 1, .exit_code = 17}}, + {.input = {.name = "unsupported profile platform", .arguments = {"--profile", "lab-B"}, .policy = {}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "invalid build identity", .arguments = {}, .policy = {LabProfileMode::REQUIRED, "../lab-A"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "headless with profile on mac", .arguments = {"--headless", "--profile", "emu-1", "--run", "UnitTestRunner"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::HEADLESS, .profile = "emu-1", .headless_calls = 1, .exit_code = 19}}, + {.input = {.name = "headless without profile on mac", .arguments = {"--headless", "--run", "UnitTestRunner"}, .policy = {LabProfileMode::REQUIRED}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "headless rejected off mac", .arguments = {"--headless", "--profile", "emu-1", "--run", "UnitTestRunner"}, .policy = {}}, + .expectation = {.mode = StartupMode::REJECTED}}, +}; + +// The GUI callback represents settings load, window restoration and hardware discovery. +// A rejected or test launch must never reach it, even when a saved panel has devices. +UnitTestResult check_lab_startup_case(const LabStartupCase& test){ + const StartupPlan plan = apply_lab_startup_policy(parse_startup_arguments(test.input.arguments), test.input.policy); + const LabStartupExpectation& expected = test.expectation; + if (plan.mode != expected.mode || plan.profile != expected.profile || + plan.augment_color_distortions != expected.augment){ + return std::format("Expected mode {}, profile {}, augment {}; read mode {}, profile {}, augment {}.", + (int)expected.mode, expected.profile.value_or(""), expected.augment, + (int)plan.mode, plan.profile.value_or(""), plan.augment_color_distortions); + } + if (plan.qt_arguments != expected.qt_arguments){ + return "Expected Qt argument sequence differs from the preserved sequence."; + } + if ((plan.mode == StartupMode::REJECTED) != !plan.error.empty()){ + return "Expected rejection reason exactly when startup is rejected."; + } + int gui_calls = 0; + int headless_calls = 0; + int test_calls = 0; + const int code = dispatch_startup(plan, { + .gui = [&](const StartupPlan&){ gui_calls++; return 17; }, + .headless = [&](const StartupPlan&){ headless_calls++; return 19; }, + .tests = [&](const StartupPlan&){ test_calls++; return 23; }, + }); + if (gui_calls != expected.gui_calls || headless_calls != expected.headless_calls || + test_calls != expected.test_calls || code != expected.exit_code){ + return std::format("Expected GUI calls {}, headless calls {}, test calls {}, exit {}; read GUI calls {}, headless calls {}, test calls {}, exit {}.", + expected.gui_calls, expected.headless_calls, expected.test_calls, expected.exit_code, + gui_calls, headless_calls, test_calls, code); + } + return true; +} + +class LabStartupTest : public UnitTest{ +public: + explicit LabStartupTest(const LabStartupCase& test) + : UnitTest("Startup/LabPolicy/" + std::string(test.input.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_lab_startup_case(m_test); + } +private: + const LabStartupCase& m_test; +}; + +} // namespace + +void add_tests_LabStartupPolicy(UnitTestDatabase& database){ + for (const LabStartupCase& test : LAB_STARTUP_CASES){ + database.add(test); + } +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.h b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.h new file mode 100644 index 0000000000..786ab3fdc9 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.h @@ -0,0 +1,15 @@ +/* Lab Startup Tests + * + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_LabStartupPolicy_Tests_H +#define PokemonAutomation_LabStartupPolicy_Tests_H +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + +// Register lab-policy and startup-dispatch regressions; no Qt or hardware is opened. +void add_tests_LabStartupPolicy(UnitTestDatabase& database); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.cpp b/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.cpp index 615379189d..e01e073fa3 100644 --- a/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.cpp +++ b/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.cpp @@ -76,25 +76,27 @@ void set_permissions(QObject& object){ } -bool check_resource_folder(Logger& logger){ +bool check_resource_folder(Logger& logger, StartupDialogs dialogs){ if (Filesystem::exists(Filesystem::Path(RESOURCE_PATH()))){ logger.log("Found Resource folder at " + RESOURCE_PATH()); return true; } logger.log("No Resources/ folder at " + RESOURCE_PATH()); - QMessageBox box; - box.setTextFormat(Qt::RichText); - box.critical( - nullptr, - "No Resources Folder!", - QString::fromStdString( - "No Resources Folder:

" - "Make sure you download Resources folder from latest release at
" - "https://github.com/PokemonAutomation/ComputerControl/releases/

" - "The Resources folder is part of PA-SerialPrograms-[VERSION]-[DATE].zip

" - "Extract the Resources folder from the zip file and place it at following path:

" + RESOURCE_PATH() - ) - ); + if (dialogs == StartupDialogs::SHOW){ + QMessageBox box; + box.setTextFormat(Qt::RichText); + box.critical( + nullptr, + "No Resources Folder!", + QString::fromStdString( + "No Resources Folder:

" + "Make sure you download Resources folder from latest release at
" + "https://github.com/PokemonAutomation/ComputerControl/releases/

" + "The Resources folder is part of PA-SerialPrograms-[VERSION]-[DATE].zip

" + "Extract the Resources folder from the zip file and place it at following path:

" + RESOURCE_PATH() + ) + ); + } return false; } diff --git a/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.h b/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.h index b9e41d2baf..c9cd695b14 100644 --- a/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.h +++ b/SerialPrograms/Source/CommonFramework/Startup/SetupSettings.h @@ -15,13 +15,19 @@ namespace PokemonAutomation{ class Logger; +// Controls whether startup checks open modal QMessageBox dialogs or only log. +enum class StartupDialogs{ + SHOW, + LOG_ONLY, +}; + // Use Qt to setup permissions of cameras and microphones on macOS. // See https://www.qt.io/blog/permission-apis-in-qt-6.5 void set_permissions(QObject& object); // Return true if there is the required Resources/ folder. -// When the folder is missing, it will also pop a critical error message box -bool check_resource_folder(Logger& logger); +// When the folder is missing and `dialogs == StartupDialogs::SHOW`, pops a critical error box. +bool check_resource_folder(Logger& logger, StartupDialogs dialogs = StartupDialogs::SHOW); } #endif diff --git a/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.cpp b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.cpp new file mode 100644 index 0000000000..061cd37ffc --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.cpp @@ -0,0 +1,132 @@ +/* Application Startup Arguments + * + * From: https://github.com/PokemonAutomation/ + */ +#include +#include "StartupArguments.h" + +namespace PokemonAutomation{ +// Profile names are single ASCII path components; never silently rewrite an identity. +bool valid_startup_profile(std::string_view profile){ + return !profile.empty() && profile.front() != '-' && std::ranges::all_of(profile, [](char c){ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c == '_' || c == '-'; + }); +} + +namespace{ + +// Qt owns option values; recognize only its documented application option spellings. +// References: doc.qt.io/qt-6/{qapplication,qguiapplication}.html constructors. +enum class QtArgument{UNKNOWN, ACCEPTED, INVALID}; +QtArgument consume_qt_argument(std::span arguments, size_t& index, StartupPlan& plan){ + const std::string_view arg = arguments[index]; + if (arg == "-reverse" || arg == "-widgetcount"){ + plan.qt_arguments.emplace_back(arg); + return QtArgument::ACCEPTED; + } + constexpr std::string_view VALUE_OPTIONS[] = { + "-platform", "-platformpluginpath", "-platformtheme", "-plugin", + "-qwindowgeometry", "-qwindowicon", "-qwindowtitle", "-session", + "-display", "-geometry", "-style", "-stylesheet", + }; + if (std::ranges::find(VALUE_OPTIONS, arg) != std::end(VALUE_OPTIONS)){ + if (index + 1 == arguments.size() || arguments[index + 1].empty() || + arguments[index + 1].starts_with('-')){ + return QtArgument::INVALID; + } + plan.qt_arguments.emplace_back(arg); + plan.qt_arguments.emplace_back(arguments[++index]); + return QtArgument::ACCEPTED; + } + for (std::string_view prefix : {"-style=", "-stylesheet=", "-qmljsdebugger="}){ + if (arg.starts_with(prefix)){ + if (arg.size() == prefix.size()){ + return QtArgument::INVALID; + } + plan.qt_arguments.emplace_back(arg); + return QtArgument::ACCEPTED; + } + } + return QtArgument::UNKNOWN; +} + +StartupPlan validate_startup_plan(StartupPlan plan){ + if (plan.run_program && plan.mode != StartupMode::HEADLESS){ + return {.error = "--run requires --headless."}; + } + if (plan.options_path && !plan.run_program){ + return {.error = "--options requires --run."}; + } + if (plan.mode == StartupMode::HEADLESS && !plan.run_program){ + return {.error = "--headless requires --run ."}; + } + if (plan.augment_color_distortions && plan.mode != StartupMode::TESTS){ + return {.error = "--augment-color-distortions requires --command-line-test-mode."}; + } + if (plan.mode == StartupMode::TESTS && plan.profile){ + return {.error = "Test launches cannot select a console profile."}; + } + return plan; +} + +} // namespace + +StartupPlan parse_startup_arguments(std::span arguments){ + StartupPlan plan{.mode = StartupMode::GUI}; + for (size_t i = 0; i < arguments.size(); i++){ + const std::string_view arg = arguments[i]; + if (arg == "--profile"){ + if (plan.profile || i + 1 == arguments.size() || !valid_startup_profile(arguments[i + 1])){ + return {.error = "--profile requires one unique name containing only ASCII letters, digits, '_' or '-' (not starting with '-')."}; + } + plan.profile = arguments[++i]; + }else if (arg == "--headless"){ + if (plan.mode == StartupMode::TESTS){ + return {.error = "--headless and --command-line-test-mode cannot be combined."}; + } + plan.mode = StartupMode::HEADLESS; + }else if (arg == "--run"){ + if (plan.run_program || i + 1 == arguments.size() || arguments[i + 1].empty() || + arguments[i + 1].starts_with('-')){ + return {.error = "--run requires one unique program identifier."}; + } + plan.run_program = arguments[++i]; + }else if (arg == "--options"){ + if (plan.options_path || i + 1 == arguments.size() || arguments[i + 1].empty() || + arguments[i + 1].starts_with('-')){ + return {.error = "--options requires one unique file path."}; + } + plan.options_path = arguments[++i]; + }else if (arg == "--command-line-test-mode"){ + if (plan.mode == StartupMode::HEADLESS){ + return {.error = "--headless and --command-line-test-mode cannot be combined."}; + } + plan.mode = StartupMode::TESTS; + }else if (arg == "--augment-color-distortions"){ + plan.augment_color_distortions = true; + }else{ + const QtArgument result = consume_qt_argument(arguments, i, plan); + if (result != QtArgument::ACCEPTED){ + return {.error = "Unknown or incomplete startup argument: " + std::string(arg)}; + } + } + } + return validate_startup_plan(std::move(plan)); +} + +int dispatch_startup(const StartupPlan& plan, const StartupHandlers& handlers){ + switch (plan.mode){ + case StartupMode::GUI: + return handlers.gui(plan); + case StartupMode::HEADLESS: + return handlers.headless(plan); + case StartupMode::TESTS: + return handlers.tests(plan); + case StartupMode::REJECTED: + return 2; + } + return 2; +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.h b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.h new file mode 100644 index 0000000000..d3470ba6b6 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.h @@ -0,0 +1,52 @@ +/* Application Startup Arguments + * + * From: https://github.com/PokemonAutomation/ + * + * Parse before Qt, settings, logging or device discovery. Example: + * open -n SerialPrograms.app --args --profile lab-B + */ +#ifndef PokemonAutomation_StartupArguments_H +#define PokemonAutomation_StartupArguments_H + +#include +#include +#include +#include +#include +#include + +namespace PokemonAutomation{ + +// Rejected launches must not enter any application entry point. +enum class StartupMode{REJECTED, GUI, HEADLESS, TESTS}; + +// Validated startup intent; error describes a rejected launch. +struct StartupPlan{ + StartupMode mode = StartupMode::REJECTED; + std::optional profile = std::nullopt; + std::optional run_program = std::nullopt; + std::optional options_path = std::nullopt; + bool augment_color_distortions = false; + std::string error = {}; + std::vector qt_arguments = {}; +}; + +// Check a single ASCII profile path component without rewriting its identity. +bool valid_startup_profile(std::string_view profile); + +// Parse argv without argv[0]; preserve documented Qt options for Qt to interpret. +// Unknown or malformed application/Qt arguments fail before settings or devices. +StartupPlan parse_startup_arguments(std::span arguments); + +// Application entry points: the GUI and headless callbacks own all settings and hardware startup. +struct StartupHandlers{ + std::function gui; + std::function headless; + std::function tests; +}; + +// Dispatch a validated plan. Rejection returns 2 without invoking either callback. +int dispatch_startup(const StartupPlan& plan, const StartupHandlers& handlers); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.cpp b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.cpp new file mode 100644 index 0000000000..2f88d67df1 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.cpp @@ -0,0 +1,110 @@ +/* Application Startup Argument Tests + * + * From: https://github.com/PokemonAutomation/ + */ +#include +#include "StartupArguments.h" +#include "StartupArguments_Tests.h" + +namespace PokemonAutomation{ +namespace{ + +struct ArgumentInput{ + std::string_view name; + std::vector arguments; +}; + +struct ArgumentExpectation{ + StartupMode mode; + std::optional profile = std::nullopt; + std::vector qt_arguments = {}; +}; + +struct ArgumentCase{ + ArgumentInput input; + ArgumentExpectation expectation; +}; + +const ArgumentCase ARGUMENT_CASES[] = { + {.input = {.name = "ordinary GUI", .arguments = {}}, + .expectation = {.mode = StartupMode::GUI}}, + {.input = {.name = "optional profile", .arguments = {"--profile", "lab-B"}}, + .expectation = {.mode = StartupMode::GUI, .profile = "lab-B"}}, + {.input = {.name = "test dispatch", .arguments = {"--command-line-test-mode"}}, + .expectation = {.mode = StartupMode::TESTS}}, + {.input = {.name = "Qt platform", .arguments = {"-platform", "offscreen"}}, + .expectation = {.mode = StartupMode::GUI, .qt_arguments = {"-platform", "offscreen"}}}, + {.input = {.name = "Qt style assignment", .arguments = {"-style=Fusion"}}, + .expectation = {.mode = StartupMode::GUI, .qt_arguments = {"-style=Fusion"}}}, + {.input = {.name = "Qt style pair", .arguments = {"-style", "Fusion"}}, + .expectation = {.mode = StartupMode::GUI, .qt_arguments = {"-style", "Fusion"}}}, + {.input = {.name = "Qt stylesheet assignment", .arguments = {"-stylesheet=some path.qss"}}, + .expectation = {.mode = StartupMode::GUI, .qt_arguments = {"-stylesheet=some path.qss"}}}, + {.input = {.name = "Qt flags and application profile", .arguments = {"-reverse", "--profile", "lab-B", "-widgetcount"}}, + .expectation = {.mode = StartupMode::GUI, .profile = "lab-B", .qt_arguments = {"-reverse", "-widgetcount"}}}, + {.input = {.name = "repeated Qt plugin", .arguments = {"-plugin", "one", "-plugin", "two"}}, + .expectation = {.mode = StartupMode::GUI, .qt_arguments = {"-plugin", "one", "-plugin", "two"}}}, + {.input = {.name = "Qt missing value", .arguments = {"-platform"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "Qt empty value", .arguments = {"-style", ""}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "Qt empty assignment", .arguments = {"-style="}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "Qt must not swallow application option", .arguments = {"-platform", "--command-line-test-mode"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "Qt must not swallow typo", .arguments = {"-platform", "--test", "bdsp"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "unknown Qt-looking option", .arguments = {"-platfrom", "offscreen"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "unknown after valid Qt option", .arguments = {"-reverse", "--test", "bdsp"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "headless run", .arguments = {"--headless", "--profile", "emu-1", "--run", "UnitTestRunner"}}, + .expectation = {.mode = StartupMode::HEADLESS, .profile = "emu-1"}}, + {.input = {.name = "headless run with options", .arguments = {"--headless", "--run", "UnitTestRunner", "--options", "opts.json"}}, + .expectation = {.mode = StartupMode::HEADLESS}}, + {.input = {.name = "headless without run", .arguments = {"--headless", "--profile", "emu-1"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "run without headless", .arguments = {"--run", "UnitTestRunner"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "options without run", .arguments = {"--headless", "--options", "opts.json"}}, + .expectation = {.mode = StartupMode::REJECTED}}, + {.input = {.name = "headless with tests", .arguments = {"--headless", "--run", "UnitTestRunner", "--command-line-test-mode"}}, + .expectation = {.mode = StartupMode::REJECTED}}, +}; + +UnitTestResult check_argument_case(const ArgumentCase& test){ + const StartupPlan plan = parse_startup_arguments(test.input.arguments); + const ArgumentExpectation& expected = test.expectation; + if (plan.mode != expected.mode || plan.profile != expected.profile){ + return std::format("Expected mode {} and profile {}; read mode {} and profile {}.", + (int)expected.mode, expected.profile.value_or(""), + (int)plan.mode, plan.profile.value_or("")); + } + if (plan.qt_arguments != expected.qt_arguments){ + return "Expected the complete Qt argument sequence to be preserved in order."; + } + return true; +} + +class ArgumentTest : public UnitTest{ +public: + explicit ArgumentTest(const ArgumentCase& test) + : UnitTest("Startup/Arguments/" + std::string(test.input.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_argument_case(m_test); + } +private: + const ArgumentCase& m_test; +}; + +} // namespace + +void add_tests_StartupArguments(UnitTestDatabase& database){ + for (const ArgumentCase& test : ARGUMENT_CASES){ + database.add(test); + } +} + +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.h b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.h new file mode 100644 index 0000000000..665e0bed7a --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.h @@ -0,0 +1,15 @@ +/* Lab Startup Tests + * + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_StartupArguments_Tests_H +#define PokemonAutomation_StartupArguments_Tests_H +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ + +// Register parser and startup-dispatch regressions; no Qt or hardware is opened. +void add_tests_StartupArguments(UnitTestDatabase& database); + +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp index fc6ceeceaf..401dcd5425 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.5.cpp @@ -141,21 +141,14 @@ void CameraVideoSource::init( ", FPS: " + std::to_string(m_fps) ); - m_camera.reset(new QCameraThread(m_logger, *device, format)); + m_camera = QCameraThread::try_create(m_logger, *device, format); + if (!m_camera){ + return; + } m_capture_session.reset(new QMediaCaptureSession()); m_capture_session->setCamera(&m_camera->camera()); -#if 0 - connect(m_camera.get(), &QCamera::errorOccurred, this, [&](){ - if (m_camera->error() == QCamera::NoError){ - return; - } - m_logger.log("QCamera error: " + m_camera->errorString().toStdString(), COLOR_RED); - }); - - m_camera->start(); -#endif } diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.cpp index 13b6dff18d..c801cb6067 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6.cpp @@ -137,19 +137,15 @@ void CameraVideoSource::init( ", FPS: " + std::to_string(m_fps) ); - m_camera.reset(new QCameraThread(m_logger, *device, format)); + m_camera = QCameraThread::try_create(m_logger, *device, format); + if (!m_camera){ + return; + } m_video_sink.reset(new QVideoSink()); m_capture.reset(new QMediaCaptureSession()); m_capture->setCamera(&m_camera->camera()); m_capture->setVideoSink(m_video_sink.get()); -#if 0 - connect(m_camera.get(), &QCamera::errorOccurred, this, [&](){ - if (m_camera->error() != QCamera::NoError){ - m_logger.log("QCamera error: " + m_camera->errorString().toStdString()); - } - }); -#endif m_metaobject->connect( m_video_sink.get(), &QVideoSink::videoFrameChanged, &m_camera->camera(), [&](const QVideoFrame& frame){ diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6_QQuickWidget.cpp b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6_QQuickWidget.cpp index 5264a3aea9..0160ac7aeb 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6_QQuickWidget.cpp +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/CameraWidgetQt6_QQuickWidget.cpp @@ -139,19 +139,15 @@ void CameraVideoSource::init( ", FPS: " + std::to_string(m_fps) ); - m_camera.reset(new QCameraThread(m_logger, *device, format)); + m_camera = QCameraThread::try_create(m_logger, *device, format); + if (!m_camera){ + return; + } m_video_sink.reset(new QVideoSink()); m_capture.reset(new QMediaCaptureSession()); m_capture->setCamera(&m_camera->camera()); m_capture->setVideoSink(m_video_sink.get()); -#if 0 - connect(m_camera.get(), &QCamera::errorOccurred, this, [&](){ - if (m_camera->error() != QCamera::NoError){ - m_logger.log("QCamera error: " + m_camera->errorString().toStdString()); - } - }); -#endif m_metaobject->connect( m_video_sink.get(), &QVideoSink::videoFrameChanged, &m_camera->camera(), [&](const QVideoFrame& frame){ diff --git a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/QCameraThread.h b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/QCameraThread.h index 9db0137e9d..2c6235f32c 100644 --- a/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/QCameraThread.h +++ b/SerialPrograms/Source/CommonFramework/VideoPipeline/Backends/QCameraThread.h @@ -8,8 +8,10 @@ #define PokemonAutomation_VideoPipeline_QCameraThread_H #include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Qt/GlobalThreadPoolsQt.h" +#include "CommonFramework/Devices/LabDeviceRuntime.h" //#include //using std::cout; @@ -18,14 +20,25 @@ namespace PokemonAutomation{ +// Own the camera and its device lease across the Qt event-thread lifetime. class QCameraThread : public QObject{ public: + // A denied input must not throw across run_on_main_thread_and_wait's Qt callback. + static std::unique_ptr try_create(Logger& logger, QCameraDevice device, QCameraFormat format){ + try{ + return std::make_unique(logger, std::move(device), std::move(format)); + }catch (UserSetupError&){ + return nullptr; // UserSetupError already logged the refusal. + } + } + QCameraThread( Logger& logger, QCameraDevice device, QCameraFormat format ) - : m_logger(logger) + : m_device_lease(acquire_lab_capture(logger, LabDeviceKind::VIDEO, device.id().toHex().toStdString())) + , m_logger(logger) , m_device(std::move(device)) , m_format(std::move(format)) , m_camera(nullptr) @@ -63,6 +76,7 @@ class QCameraThread : public QObject{ private: + std::unique_ptr m_device_lease; Logger& m_logger; QCameraDevice m_device; QCameraFormat m_format; @@ -71,5 +85,5 @@ class QCameraThread : public QObject{ -} +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/CommonFramework/Windows/Lab.md b/SerialPrograms/Source/CommonFramework/Windows/Lab.md new file mode 100644 index 0000000000..37ccad0e5e --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Windows/Lab.md @@ -0,0 +1,279 @@ +# Lab Channel + +Lets a script drive a running SerialPrograms app through files, with no +window focus and no click: select a program, set its options, start and stop +it, read its state, and press buttons or check detectors on the console +between runs. Meant for a lab that drives one app per console from the shell. + +The channel is one folder per app instance, independent of the selected +program. Requests are text files; replies are text files. Nothing else is +needed on the script side. + +## Turning it on + +Create the folder `Lab/` inside the profile's user-files folder (the folder +that holds `SerialPrograms.log`, `Screenshots/` and the settings; with +`--profile NAME` that is `Profiles/NAME/`) **before launching the app**. An +app that finds it at startup polls it every 500 ms from a worker and creates +`inbox/`, `outbox/` and `done/` inside it; an app that does not find it +starts nothing, so normal users see no change. Creating the folder later +takes effect at the next launch. + +## Files + +| Path | Direction | Meaning | +|---|---|---| +| `Lab/inbox/.txt` | script writes | A request: one command per line, run in order. Requests run one at a time, in name order. | +| `Lab/done/.txt` | app moves | The request, moved here when it starts. | +| `Lab/outbox/.out` | app writes | The request's journal (below). It appears only once the request has finished; `.tmp` shows the progress so far. | +| `Lab/outbox/_