startup-tools: save the title's window every N seconds (--capture-every) #2814

Merged
tedin7 merged 2 commits from upstream-pr/2822 into main 2026-11-27 18:43:14 +00:00
tedin7 commented 2026-11-26 11:40:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T11:40:48Z.


Part of #870

Fixes #2819

What this unblocks: a tester who drives the title with X2W_INPUT_SCRIPT gets one picture per screen with its time, so a run that stops on a late screen (or never reaches one) can be read from the pictures instead of from the last frame only.

What changed

  • tools/startup/debug_launch.py --capture-every <s> (also run.ps1 -CaptureEvery, run.sh --capture-every): saves the title window's client area every N seconds as window_<sss>s.png (seconds since the process started) next to the logs. Off by default (0); N below 1, negative or not a number is refused with exit code 2. The end-of-run window.png is unchanged.
  • A frame whose pixels equal the previous frame is not saved (hash of the pixels and size, so a static screen costs one file and no PNG conversion). At most 120 files; later changes are counted, not saved. One summary line, e.g. window captures: 12 saved, 7 unchanged skipped, first change at 74 s, plus file cap 120 reached (N changed frames dropped) and N ticks without a window when they apply.
  • The debug event loop is not touched by a capture: a thread of its own grabs the window (GetDC + BitBlt + GetDIBits, as before) and writes the file.
  • capture_window() is split into grab_window() (returns the BGRA pixels or the reason there is no window; it now also deletes its bitmap and device context and releases the window DC, which the single end-of-run call never needed) and the PNG writer, which the timer and the end-of-run capture share.
  • New tools/startup/window_capture.py holds the parts that need no Windows API: BGRA to PNG with whole-buffer slice copies instead of the per-byte generator (1920x1080: about 0.3 s at zlib level 1 on random pixels, the old conversion alone took about 0.7 s), file names, duplicate skip, cap, summary.

Tests

  • tests/startup_window_capture_check.py (Linux and Windows, 12 cases): decoded PNG equals a per-pixel reference (sizes 1x1 to 64x3, alpha dropped, channels swapped), a buffer of the wrong size is refused, names are window_<sss>s.png (whole seconds, three digits), duplicates are skipped and changes saved, a size change counts as a change, the cap (2 in a small case, 120 by default) stops saving and counts the dropped changes, the summary line, a failing write leaves the counters alone.
  • tests/startup_debug_window_check.py + new source-built tests/startup_window_probe.cpp (a 160x120 window, red, blue after 3.5 s; no existing fixture had a picture that changes): --capture-every 1 --timeout 9 must save exactly one red and one blue file, report first change in the second the probe changes it, skip the other frames, and still write window.png (blue). Controls: without the option no window_<sss>s.png and no summary line; intervals 0.5, -1, abc are refused. Registered as startup_debug_window (and startup_window_capture_check), so -R '^startup_debug' runs it. It exits 77 (ctest SKIP) only when the machine shows no window at all.
  • tests/startup_run_sh_check.py and tests/startup_run_ps1_check.py get a case that --capture-every 5 / -CaptureEvery 5 reaches the debugger (the sh case fails when the wrapper drops it).
  • docs/startup-runtime.md: new "Window pictures" paragraph.

Verification

  • python3 -B tests/startup_window_capture_check.py: 12 tests OK
  • python3 -B tests/startup_run_sh_check.py: 0 failed (1 failed with the wrapper's capture_args removed); python3 -B tests/startup_run_ps1_check.py: 0 failed (text only, not a Windows host)
  • x86_64-w64-mingw32-g++ -std=c++17 -Wall -Wextra tests/startup_window_probe.cpp -luser32 -lgdi32: builds; the probe prints window-probe shown and window-probe flipped under Wine
  • Win2025 startup_debug_window + startup_window_capture_check (dev + release), real window capture: pending Stage-2

Source: Microsoft Learn, Win32 GDI (GetDC, BitBlt, GetDIBits, DeleteObject, DeleteDC, ReleaseDC); W3C PNG Specification, Second Edition, section 11 (IHDR, IDAT, IEND, filter type 0); earlier code of this repo (debug_launch.py capture_window()).

Real-hardware testing: @Makto94 - thanks for the help.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5509369182 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2822). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T11:40:48Z. --- Part of #870 Fixes #2819 What this unblocks: a tester who drives the title with `X2W_INPUT_SCRIPT` gets one picture per screen with its time, so a run that stops on a late screen (or never reaches one) can be read from the pictures instead of from the last frame only. ## What changed - `tools/startup/debug_launch.py --capture-every <s>` (also `run.ps1 -CaptureEvery`, `run.sh --capture-every`): saves the title window's client area every N seconds as `window_<sss>s.png` (seconds since the process started) next to the logs. Off by default (0); N below 1, negative or not a number is refused with exit code 2. The end-of-run `window.png` is unchanged. - A frame whose pixels equal the previous frame is not saved (hash of the pixels and size, so a static screen costs one file and no PNG conversion). At most 120 files; later changes are counted, not saved. One summary line, e.g. `window captures: 12 saved, 7 unchanged skipped, first change at 74 s`, plus `file cap 120 reached (N changed frames dropped)` and `N ticks without a window` when they apply. - The debug event loop is not touched by a capture: a thread of its own grabs the window (`GetDC` + `BitBlt` + `GetDIBits`, as before) and writes the file. - `capture_window()` is split into `grab_window()` (returns the BGRA pixels or the reason there is no window; it now also deletes its bitmap and device context and releases the window DC, which the single end-of-run call never needed) and the PNG writer, which the timer and the end-of-run capture share. - New `tools/startup/window_capture.py` holds the parts that need no Windows API: BGRA to PNG with whole-buffer slice copies instead of the per-byte generator (1920x1080: about 0.3 s at zlib level 1 on random pixels, the old conversion alone took about 0.7 s), file names, duplicate skip, cap, summary. ## Tests - `tests/startup_window_capture_check.py` (Linux and Windows, 12 cases): decoded PNG equals a per-pixel reference (sizes 1x1 to 64x3, alpha dropped, channels swapped), a buffer of the wrong size is refused, names are `window_<sss>s.png` (whole seconds, three digits), duplicates are skipped and changes saved, a size change counts as a change, the cap (2 in a small case, 120 by default) stops saving and counts the dropped changes, the summary line, a failing write leaves the counters alone. - `tests/startup_debug_window_check.py` + new source-built `tests/startup_window_probe.cpp` (a 160x120 window, red, blue after 3.5 s; no existing fixture had a picture that changes): `--capture-every 1 --timeout 9` must save exactly one red and one blue file, report `first change` in the second the probe changes it, skip the other frames, and still write `window.png` (blue). Controls: without the option no `window_<sss>s.png` and no summary line; intervals `0.5`, `-1`, `abc` are refused. Registered as `startup_debug_window` (and `startup_window_capture_check`), so `-R '^startup_debug'` runs it. It exits 77 (ctest SKIP) only when the machine shows no window at all. - `tests/startup_run_sh_check.py` and `tests/startup_run_ps1_check.py` get a case that `--capture-every 5` / `-CaptureEvery 5` reaches the debugger (the sh case fails when the wrapper drops it). - `docs/startup-runtime.md`: new "Window pictures" paragraph. ## Verification - `python3 -B tests/startup_window_capture_check.py`: 12 tests OK - `python3 -B tests/startup_run_sh_check.py`: 0 failed (1 failed with the wrapper's `capture_args` removed); `python3 -B tests/startup_run_ps1_check.py`: 0 failed (text only, not a Windows host) - `x86_64-w64-mingw32-g++ -std=c++17 -Wall -Wextra tests/startup_window_probe.cpp -luser32 -lgdi32`: builds; the probe prints `window-probe shown` and `window-probe flipped` under Wine - Win2025 `startup_debug_window` + `startup_window_capture_check` (dev + release), real window capture: pending Stage-2 Source: Microsoft Learn, Win32 GDI (`GetDC`, `BitBlt`, `GetDIBits`, `DeleteObject`, `DeleteDC`, `ReleaseDC`); W3C PNG Specification, Second Edition, section 11 (IHDR, IDAT, IEND, filter type 0); earlier code of this repo (`debug_launch.py` `capture_window()`). Real-hardware testing: @Makto94 - thanks for the help. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 12:04:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T11:54:14Z.


@MasterDomino Ready for review: head 0d4a8afe4 (Fixes #2819).

Independent Stage-2 check on this exact head.

Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP), Proxmox slots, dev and release /O2:

python3 x2w_pipe.py win2025 2822 --host proxmox --regex '^startup_(debug_window|window_capture)' --keep-release
ctest --preset dev     -R '^startup_(debug_window|window_capture)' --no-tests=error   -> 1/2 passed (startup_window_capture_check); startup_debug_window skipped
ctest --preset release -R '^startup_(debug_window|window_capture)' --no-tests=error   -> 1/2 passed (startup_window_capture_check); startup_debug_window skipped

startup_debug_window returns the capability skip (77) on this Win2025 host because its session has no visible desktop; a skip is not a pass. It was run with a real window instead on Linux, Wine 10.0 with Xvfb, Windows Python 3.12.8 and the repository's tests/startup_window_probe.cpp built with MinGW:

wine python.exe -B tests/startup_debug_window_check.py startup_window_probe.exe
startup_debug_window: PASS (red then blue saved once each, unchanged frames skipped, change time reported, window.png unchanged, off by default, bad interval refused)

So on a real window the periodic files change when the picture changes (red first, blue last, the first change reported at 3 to 6 s), the unchanged frames between them are skipped (2 files saved for a 9 s run at --capture-every 1), the end-of-run window.png is unchanged, the option is off by default, and an interval below one second, negative or not a number is refused with exit code 2.

Controls (one change each in the same real-window run, the check must fail): the duplicate skip disabled (a file every second), every frame hashed as equal (the blue picture never saved), the timer never offering a frame to the series. All three fail the check.

Also run: startup_window_capture_check.py, startup_run_sh_check.py, startup_run_ps1_check.py (text only off Windows), docs_links_check.py, bidi_controls_check.py pass; the translation break-it comparison against origin/main: 173 shapes, all the same.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7191313444 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2822#issuecomment-7191313444). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T11:54:14Z. --- @MasterDomino Ready for review: head `0d4a8afe4` (Fixes #2819). Independent Stage-2 check on this exact head. Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP), Proxmox slots, dev and release `/O2`: ``` python3 x2w_pipe.py win2025 2822 --host proxmox --regex '^startup_(debug_window|window_capture)' --keep-release ctest --preset dev -R '^startup_(debug_window|window_capture)' --no-tests=error -> 1/2 passed (startup_window_capture_check); startup_debug_window skipped ctest --preset release -R '^startup_(debug_window|window_capture)' --no-tests=error -> 1/2 passed (startup_window_capture_check); startup_debug_window skipped ``` `startup_debug_window` returns the capability skip (77) on this Win2025 host because its session has no visible desktop; a skip is not a pass. It was run with a real window instead on Linux, Wine 10.0 with Xvfb, Windows Python 3.12.8 and the repository's `tests/startup_window_probe.cpp` built with MinGW: ``` wine python.exe -B tests/startup_debug_window_check.py startup_window_probe.exe startup_debug_window: PASS (red then blue saved once each, unchanged frames skipped, change time reported, window.png unchanged, off by default, bad interval refused) ``` So on a real window the periodic files change when the picture changes (red first, blue last, the first change reported at 3 to 6 s), the unchanged frames between them are skipped (2 files saved for a 9 s run at `--capture-every 1`), the end-of-run `window.png` is unchanged, the option is off by default, and an interval below one second, negative or not a number is refused with exit code 2. Controls (one change each in the same real-window run, the check must fail): the duplicate skip disabled (a file every second), every frame hashed as equal (the blue picture never saved), the timer never offering a frame to the series. All three fail the check. Also run: `startup_window_capture_check.py`, `startup_run_sh_check.py`, `startup_run_ps1_check.py` (text only off Windows), `docs_links_check.py`, `bidi_controls_check.py` pass; the translation break-it comparison against `origin/main`: 173 shapes, all the same. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-26 12:04:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T11:54:25Z.


Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-26T11:54:21.500944Z
We couldn’t complete this review. Comment @codex review to try again.
0d4a8af Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7191317095 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2822#issuecomment-7191317095). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-26T11:54:25Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"0d4a8afe423bd1e214fd2d62f9cf5cd69d0e8d5b","has_any_findings":true,"has_unresolved_findings":true,"status":"failed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ❌ Failed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ❌ **Failed** <relative-time datetime="2026-11-26T11:54:21.500944Z">2026-11-26T11:54:21.500944Z</relative-time><br/>We couldn’t complete this review. Comment `@codex review` to try again. | `0d4a8af` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
chatgpt-codex-connector[bot commented 2026-11-26 12:04:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T11:54:27Z.


You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7191317768 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2822#issuecomment-7191317768). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-26T11:54:27Z. --- You have reached your Codex usage limits for code reviews. You can see your limits in the [Codex usage dashboard](https://chatgpt.com/codex/cloud/settings/usage). To continue using code reviews, add credits to your account and enable them for code reviews in your [settings](https://chatgpt.com/settings/code-review).
pigalot commented 2026-11-27 18:37:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T18:37:00Z.

Review: APPROVED


Independently reviewed 0d4a8afe integrated over main 429f11d5 (tree 2915e02b, clean merge).

This adds a native Windows desktop run of startup_debug_window. Until now it had been skipped on the Win2025 host (no visible desktop) and had only run under Wine.

What I traced. I compared grab_window() with the removed capture_window() body. It does the same enumeration, client-rect clamp, BitBlt and GetDIBits. It now releases the bitmap, the memory DC and the window DC in a finally block. The end-of-run window.png uses the same grab and the new png_bytes.

The capture thread only grabs the window and hands the frame to Series. An exception or a missing window counts as a miss, and the debug loop never waits on it.

The two wrappers pass the option through only when it is non-zero.

What I ran. I built startup_debug_probe, startup_debug_calls and startup_window_probe in dev and release.

  1. ctest --test-dir out/build/<preset> --no-tests=error -j 4 --output-on-failure -R '^(startup_debug|startup_debug_log|startup_debug_hooks|startup_debug_launch_args|startup_window_capture_check|startup_run_ps1|startup_run_sh)$'
    • 7 tests per preset: 6 passed and startup_run_sh was a capability skip (no POSIX host), in Debug and Release /O2.
  2. ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^startup_debug_window$'
    • on an interactive desktop: passed 3 of 3 in Debug and 1 of 1 in Release, about 13.6 s each.
    • Each run printed startup_debug_window: PASS (red then blue saved once each, unchanged frames skipped, change time reported, window.png unchanged, off by default, bad interval refused).

On main 429f11d5, startup_debug_window and startup_window_capture_check are not registered.

Mutant. I accepted intervals below one second (0 < capture_every instead of 1 <= capture_every). That fails startup_debug_window with AssertionError: ('0.5', 0, ...): the run went ahead and saved captures every 0.5 s. tedin7's three controls cover the duplicate skip and the timer.

git diff --check is clean.

Verdict: approve on 0d4a8afe.

Environment: Windows 11 25H2 build 26200.9974, interactive desktop session; MSVC 19.52.37029; Python 3.12.10.
CI: Build and test on Windows passes on 0d4a8afe.

This reply was AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6092157114 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2822#pullrequestreview-6092157114). Originally posted by [pigalot](https://github.com/pigalot) on 2026-11-27T18:37:00Z. **Review:** APPROVED --- Independently reviewed `0d4a8afe` integrated over `main` `429f11d5` (tree `2915e02b`, clean merge). This adds a native Windows desktop run of `startup_debug_window`. Until now it had been skipped on the Win2025 host (no visible desktop) and had only run under Wine. **What I traced.** I compared `grab_window()` with the removed `capture_window()` body. It does the same enumeration, client-rect clamp, `BitBlt` and `GetDIBits`. It now releases the bitmap, the memory DC and the window DC in a `finally` block. The end-of-run `window.png` uses the same grab and the new `png_bytes`. The capture thread only grabs the window and hands the frame to `Series`. An exception or a missing window counts as a miss, and the debug loop never waits on it. The two wrappers pass the option through only when it is non-zero. **What I ran.** I built `startup_debug_probe`, `startup_debug_calls` and `startup_window_probe` in `dev` and `release`. 1. `ctest --test-dir out/build/<preset> --no-tests=error -j 4 --output-on-failure -R '^(startup_debug|startup_debug_log|startup_debug_hooks|startup_debug_launch_args|startup_window_capture_check|startup_run_ps1|startup_run_sh)$'` - 7 tests per preset: 6 passed and `startup_run_sh` was a capability skip (no POSIX host), in Debug and Release `/O2`. 2. `ctest --test-dir out/build/<preset> --no-tests=error -j 1 -V -R '^startup_debug_window$'` - on an interactive desktop: passed 3 of 3 in Debug and 1 of 1 in Release, about 13.6 s each. - Each run printed `startup_debug_window: PASS (red then blue saved once each, unchanged frames skipped, change time reported, window.png unchanged, off by default, bad interval refused)`. On `main` `429f11d5`, `startup_debug_window` and `startup_window_capture_check` are not registered. **Mutant.** I accepted intervals below one second (`0 < capture_every` instead of `1 <= capture_every`). That fails `startup_debug_window` with `AssertionError: ('0.5', 0, ...)`: the run went ahead and saved captures every 0.5 s. tedin7's three controls cover the duplicate skip and the timer. `git diff --check` is clean. **Verdict:** approve on `0d4a8afe`. Environment: Windows 11 25H2 build 26200.9974, interactive desktop session; MSVC 19.52.37029; Python 3.12.10. CI: `Build and test on Windows` passes on `0d4a8afe`. This reply was AI-assisted.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2814
No description provided.