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/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/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 0e7bb7fd3f..a5407122c9 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,6 +21,7 @@ #include "Common/Cpp/ScopeExit.h" #include "Common/Qt/GlobalThreadPoolsQt.h" #include "StaticRegistration.h" +#include "CommonFramework/Devices/LabDeviceRuntime.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/Tools/GlobalThreadPools.h" #include "CommonFramework/Panels/OptionsPanel.h" @@ -38,6 +43,8 @@ #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" @@ -45,15 +52,10 @@ #include "PersistentSettings.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 +71,6 @@ void set_working_directory(){ } } -namespace PokemonAutomation{ - - - FileLogger& global_file_logger(){ static FileLogger logger( GlobalThreadPools::unlimited_normal(), @@ -83,19 +81,12 @@ FileLogger& global_file_logger(){ return logger; } -} - 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. @@ -188,11 +179,6 @@ int run_program(int argc, char *argv[]){ - 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)){ @@ -208,34 +194,22 @@ int run_program(int argc, char *argv[]){ 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. + // Persisted test mode also exits before console options, devices or a window. 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); + return run_command_line_tests(false); } + ScopeExit cleanup([]{ + SerialPortPoller::instance().stop(); + GlobalMediaServices::instance().stop(); + }); + + OptionsPanelWrapper().make_panel()->load_json_from_global(); + SerialPortPoller::instance().ports(); + get_all_cameras(); + // Check whether the hardware is powerful enough to run this program. if (!check_hardware()){ return 1; @@ -280,29 +254,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 +278,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 +331,80 @@ 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; } + +// 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; +} + +} // 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){ +#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 2; + } + return PokemonAutomation::run_gui_application(qt_argc, qt_argv.data()); + }, + .tests = [&](const PokemonAutomation::StartupPlan& selected){ + return PokemonAutomation::run_test_application(qt_argc, qt_argv.data(), selected); + }, + }); +} diff --git a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h index 995b028763..b6da3ab2d5 100644 --- a/SerialPrograms/Source/CommonFramework/Panels/PanelList.h +++ b/SerialPrograms/Source/CommonFramework/Panels/PanelList.h @@ -35,6 +35,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; diff --git a/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.cpp b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.cpp new file mode 100644 index 0000000000..a9f1339ca4 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy.cpp @@ -0,0 +1,32 @@ +/* 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){ + return plan; + } + if (policy.profiles == LabProfileMode::UNSUPPORTED){ + 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..5581e264a0 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/LabStartupPolicy_Tests.cpp @@ -0,0 +1,139 @@ +/* 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 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}}, +}; + +// 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 test_calls = 0; + const int code = dispatch_startup(plan, { + .gui = [&](const StartupPlan&){ gui_calls++; return 17; }, + .tests = [&](const StartupPlan&){ test_calls++; return 23; }, + }); + if (gui_calls != expected.gui_calls || test_calls != expected.test_calls || code != expected.exit_code){ + return std::format("Expected GUI calls {}, test calls {}, exit {}; read GUI calls {}, test calls {}, exit {}.", + expected.gui_calls, expected.test_calls, expected.exit_code, gui_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/StartupArguments.cpp b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.cpp new file mode 100644 index 0000000000..e272e05991 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.cpp @@ -0,0 +1,97 @@ +/* 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; +} + +} // 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 == "--command-line-test-mode"){ + 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)}; + } + } + } + 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; +} + +int dispatch_startup(const StartupPlan& plan, const StartupHandlers& handlers){ + switch (plan.mode){ + case StartupMode::GUI: + return handlers.gui(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..895b4345d3 --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments.h @@ -0,0 +1,49 @@ +/* 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 either application entry point. +enum class StartupMode{REJECTED, GUI, TESTS}; + +// Validated startup intent; error describes a rejected launch. +struct StartupPlan{ + StartupMode mode = StartupMode::REJECTED; + std::optional profile = 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 callback owns all settings and hardware startup. +struct StartupHandlers{ + std::function gui; + 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..bae0d6f3fc --- /dev/null +++ b/SerialPrograms/Source/CommonFramework/Startup/StartupArguments_Tests.cpp @@ -0,0 +1,98 @@ +/* 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}}, +}; + +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/_