Skip to content

hle/accessibility, docs: implement closed captions and high contrast - #22

Closed
PrivacyOnion wants to merge 2 commits into
MiamiGame:mainfrom
PrivacyOnion:hle/accessibility
Closed

PrivacyOnion wants to merge 2 commits into
MiamiGame:mainfrom
PrivacyOnion:hle/accessibility

Conversation

@PrivacyOnion

@PrivacyOnion PrivacyOnion commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Implement the accessibility vtable slots in crates/xtp-hle/src/modules/speech.rs from the host Windows settings:

  • 0x18 (XClosedCaptionGetProperties): reads closed-caption colours, opacity, edge attribute, font style, and scale through the WinRT Windows.Media.ClosedCaptioning.ClosedCaptionProperties API (cached in a OnceLock so per-frame polling stays cheap), falling back to default properties (opaque black background, opaque white text, transparent window, default edge/style, 100% scale, disabled) when unavailable.
  • 0x20 (XClosedCaptionSetEnabled): accepts u8 (!= 0) to match C++ bool ABI safely and stores the enabled override atomically so subsequent XClosedCaptionGetProperties calls read it back.
  • 0x28 (XHighContrastGetMode): queries SystemParametersInfoW(SPI_GETHIGHCONTRAST) and compares GetSysColor(COLOR_WINDOW) against GetSysColor(COLOR_WINDOWTEXT) brightness to distinguish Dark, Light, and Other (or Off when inactive).

Fixes: #6
Depends on: none

Area

  • crates/xtp-hle/src/modules/speech.rs
  • crates/xtp-hle/Cargo.toml (windows feature Media_ClosedCaptioning; windows-sys features Win32_Graphics_Gdi, Win32_UI_Accessibility)
  • README.md (update "Runtime services" row)

Tests

  • cargo xtask pr-check passes on my machine
  • New or changed behaviour has tests that need no game files, or I explain below why it cannot
  • Ran on the title: no

Unit tests in crates/xtp-hle/src/modules/speech.rs verify:

  • empty WinRT snapshot fills the entire 28-byte structure with fallback properties;
  • WinRT enum values translate to expected XColor, edge, style, and scale fields;
  • XClosedCaptionSetEnabled accepts any non-zero byte (0xff) and 0 and is read back through XClosedCaptionGetProperties, plus null-pointer rejection;
  • high_contrast_from_pair distinguishes Off, Dark, Light, and Other, and slot 0x28 rejects null pointers and returns a valid mode.

Provenance

  • I wrote all of this change myself for XTP, or it only moves code already in XTP.
  • Some of it comes from elsewhere. Each source, its licence and the files concerned are listed here, and every licence is compatible with GPL-3.0-or-later:

Written from scratch for XTP using public Microsoft Learn documentation for XAccessibility.h, Windows.Media.ClosedCaptioning.ClosedCaptionProperties, and Win32 SystemParametersInfoW(SPI_GETHIGHCONTRAST) / GetSysColor. Fallback default values follow the behaviour described in issue #6.

AI assistance: Antigravity assisted with implementation, review fixes, and unit tests; all changes were reviewed and verified locally.

Checklist

  • No game data, keys, console SDK code or headers, and nothing that circumvents DRM (asset policy)
  • No code copied from xcl or AnyPS5 (references only: read them to understand, write your own)
  • I agree to license this contribution under GPL-3.0-or-later
  • Title and every commit follow <area>: <summary>; one topic; under 400 changed lines, or the reason is above
  • Docs updated where behaviour, commands or variables changed (README, docs/)

@github-actions github-actions Bot added area/hle touches crates/xtp-hle size/L 400 to 999 changed lines labels Nov 24, 2026

@simnJS simnJS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the tests and the AI disclosure. A few things need changing before it can go in.

1. Five of the nine registry value names don't exist. On Windows 11, the Settings app writes these DWORDs under HKCU\Software\Microsoft\Windows\CurrentVersion\ClosedCaptioning:

  • CaptionColor, CaptionOpacity, CaptionSize, CaptionFontStyle, CaptionEdgeEffect;
  • BackgroundColor, BackgroundOpacity, RegionColor, RegionOpacity;
  • plus CurrentSelectedTheme, a string.

FontColor, FontOpacity, FontEffect, FontStyle and FontSize (speech.rs:229-235) are property names of the WinRT ClosedCaptionProperties class, not registry values. So text colour, text opacity, size, font style and edge always fall back to the defaults: "yellow on blue" comes out white on blue. The tests start from a filled-in HostCaptionSettings, so they don't catch it.

The simplest fix is to drop the registry and use the documented API, Windows.Media.ClosedCaptioning.ClosedCaptionProperties. xtp-hle already depends on windows, so this only needs its Media_ClosedCaptioning feature. If you keep the registry, please use the real names, and make the reader testable so that a test pins the names.

2. Overlap with xcl. classify_high_contrast (speech.rs:172-186) has the same name, signature and body as the function of the same name in xcl's hle/xgameruntime/accessibility.rs: the same early return, the same COLORREF unpacking, Rec. 601 luma, and the same threshold to within 500. The defaults (including the transparent window colour), the three layout asserts and the high-contrast test vectors match it too, in the same order and with the same u32::MAX sentinel.

xcl is GPL-2.0-only, and CONTRIBUTING rule 6 rules out code copied or translated from it, "not even a function". You wrote that you didn't read that file, but AI assistants may have seen it. Please rewrite the high-contrast classification and these tests your own way. Using xcl's defaults as behaviour is fine (issue #6 points there), but please say so under Provenance rather than calling them "documented defaults".

3. Smaller points

  • closed_caption_set_enabled(_: This, enabled: bool) (speech.rs:299): a Rust bool holding anything other than 0 or 1 is undefined behaviour. When XTP reads a C++ bool argument, it takes a u8 and tests != 0 (see m_get_status in xasync.rs).
  • Each XClosedCaptionGetProperties call re-reads the settings: cache them if the title turns out to poll every frame.
  • The docs box is ticked, but no docs changed: the "Runtime services" row in README.md and R7 in docs/analysis/hle.md.

4. Coordination. AliceWood claimed #6 before this PR was opened, and #23 implements the same issue in the same files. As the claim came first, we'll review #23 first. Please hold off on a big rework until we've decided, and next time please comment on the issue to claim it before starting, so that nobody duplicates work.

What's already right:

  • the slot assignment (0x18/0x20/0x28) and the signatures;
  • the 28-byte layout and the enum mappings;
  • the null checks through put, and the REG_DWORD and length checks;
  • the SPI_GETHIGHCONTRAST call and the SAFETY comments;
  • adding only windows-sys features to an existing dependency.

@PrivacyOnion

Copy link
Copy Markdown
Contributor Author

Understood — apologies for missing @AliceWood's claim on #6, I'll make sure to check and comment to claim issues before starting next time. Holding off on changes here while #23 is reviewed, and thanks for the detailed feedback on the registry value names, u8 ABI for C++ �ool, and WinRT Media_ClosedCaptioning.

@simnJS

simnJS commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Heads-up: #23 implements the same issue (#6). It was claimed on the issue first, and it uses the documented WinRT caption API. It is going ahead, and this PR will be closed when #23 lands. Thanks for the effort. For the next one, please claim the issue first (CONTRIBUTING, "Before you start"), so that two people don't build the same thing.

@PrivacyOnion PrivacyOnion changed the title hle/accessibility: implement closed captions and high contrast hle/accessibility, docs: implement closed captions and high contrast Nov 24, 2026
@PrivacyOnion

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up commit addressing all review items (still happy for #23 to be reviewed first):

  1. Dropped the registry reader and switched to WinRT Windows.Media.ClosedCaptioning.ClosedCaptionProperties (Media_ClosedCaptioning feature), cached in a OnceLock.
  2. Rewrote high-contrast mode detection cleanly by comparing COLOR_WINDOW vs COLOR_WINDOWTEXT brightness (also covering HIGH_CONTRAST_OTHER) with fresh unit tests.
  3. Changed closed_caption_set_enabled to take u8 (!= 0) to avoid Rust bool UB on arbitrary C++ bytes.
  4. Updated README.md ("Runtime services") and clarified Provenance.

@github-actions github-actions Bot added the overlap another open PR changes the same files label Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Other open pull requests change the same files. Check that they do not do the same work, and agree with their authors on which lands first: the other one will then need a rebase.

Pull request Files changed by both
#23 README.md, crates/xtp-hle/Cargo.toml, crates/xtp-hle/src/modules/speech.rs
#26 README.md

This comment is kept up to date automatically and removed when the overlap ends.

@github-actions github-actions Bot added area/docs docs/, Markdown and removed overlap another open PR changes the same files labels Nov 24, 2026
@github-actions github-actions Bot added the overlap another open PR changes the same files label Nov 24, 2026
@simnJS

simnJS commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #23, which implemented #6 and has now been merged. Thanks @PrivacyOnion for the work and for understanding.

@simnJS simnJS closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs docs/, Markdown area/hle touches crates/xtp-hle overlap another open PR changes the same files size/L 400 to 999 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Closed captions and high contrast from the Windows settings

2 participants