Skip to content

d3d9: Move the Win32 code behind a platform module - #1232

Open
kanapitsas wants to merge 21 commits into
athei:mainfrom
kanapitsas:feat/platform-module
Open

kanapitsas wants to merge 21 commits into
athei:mainfrom
kanapitsas:feat/platform-module

Conversation

@kanapitsas

@kanapitsas kanapitsas commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A native macOS build (#1193) needs the Win32 calls of d3d9 behind one interface. Today they are spread over many modules.

Changes

platform.rs declares one module and picks its file by target.

platform/win32.rs and platform/win32/ hold DllMain, the hookable exports, the PE logger, the crash handler, the global allocator, the import patches and every Win32 import. Eight files move there unchanged. arm64_crt.rs, crash.rs, mode_list_hook.rs and fullscreen.rs move with small edits.

Some modules mixed D3D9 logic and Win32 calls, so they are split:

  • cursor.rs: the D3D9 cursor rules, the cursor state and the overlay path are shared in device/cursor.rs. platform/win32/cursor.rs keeps SystemCursor, one field of that state: the HCURSOR cache and build, SetCursor, the pointer position and the window subclass.
  • surface.rs: the GDI part of GetDC goes to platform/win32/surface_dc.rs. SurfaceDcSource stays shared.
  • direct3d9.rs: the LUID query goes to platform/win32/adapter.rs and the client rect to platform/win32/window.rs.
  • log_sink.rs: the log location stays shared in log_location.rs.
  • capture.rs, config.rs: one Win32 call each becomes a platform function.
  • device/mem_watch.rs: the address-space walk, the loader-lock test and the pointer-width constant come from the platform.

The Windows arm of unix_call.rs moves into the platform module. The non-Windows arm is removed here and comes back in the native PR, inside its platform file.

platform.rs names platform/native.rs, which the native PR adds. No build takes that arm until then.

This should not change behaviour under Wine. I could not run the Wine suites locally.

Alternative

A trait with two implementors. Dropped: a build only ever has one of them, so the trait would add nothing the module selection does not give.

Rules check

  • One cfg_attr(path) pair in platform.rs. The remaining architecture cfgs are in platform/win32.rs and platform/win32/.
  • New pub items, all inside the crate: the platform interface listed in platform.rs, device::cursor, and direct3d9::PRIMARY_DISPLAY_NAME. DisplayModeInfo, LeaveKind and SurfaceDcSource are defined once in shared code.
  • create_surface_dc, SystemCursor::select and CursorState::select_bitmap are unsafe fn: they are pub and read through a caller's pointer.
  • One new cursor flag, SET. ShowCursor and the overlay flags tested for a null HCURSOR before. The software path sets it once it has the blank cursor, and realize_bitmap sets it too. destroy_handles clears it.
  • New CONVENTIONS section and make audit check: Win32 imports and platform cfgs stay in platform.rs and platform/. The check matches text and is not a Rust parser.
  • No new statics, config keys, wire fields, thunks, dependencies or lint exceptions. Existing statics move with their code.
  • audit.sh, derive_inventory.txt and CONVENTIONS.md follow the moved paths.

Verification

  • make check: every leg passed.
  • make test and conformance: not run locally. CI ran them.

🤖 Generated with Claude Code

kanapitsas and others added 9 commits November 14, 2026 21:17
A native build of d3d9 needs every Win32 call to have a counterpart it
can implement without Wine. Today those calls are spread over the crate,
in modules that also hold shared D3D9 logic, so a native build would have
to branch on the target in each of them.

`platform.rs` is now the one place the crate tells the builds apart. Its
Win32 implementation, `platform/win32.rs` and `platform/win32/`, takes
what only exists for `d3d9.dll`: `DllMain`, the image pin, the exit and
mode-list hooks, the crash handler, the PE logger, the global allocator,
the hookable exports and the bodies of the ones that only Windows' d3d9
has, the arm64 CRT pieces, the guest memory routines, the Wine path
mapping and the `raw-dylib` import of the unix dispatcher. Those files
move unchanged apart from the paths they name.

Where a module mixed both, the part that talks to the OS moves and the
shared code calls it by what it does: `client_size`, `adapter_luid`,
`main_image_version_blob`, `capture_hotkey_pressed`, `create_surface_dc`,
`note_create_device`, the display-mode queries and the fullscreen
session. The cursor entry points keep the D3D9 argument handling in
`device/cursor.rs` and hand the operation to the platform's
`CursorState`, which keeps the HCURSOR cache and the window subclass.
`DisplaySinks` stays shared, since the unix side writes into it in any
build, and so does the log location, which only needs `unix_path`.

No behaviour changes under Wine. The one reordering is in
`SetCursorPosition`, which now checks `this` before reading the pointer
position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One fact per sentence, and nothing the code already says.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CONVENTIONS §Imports allows a glob re-export only for constant-definition
modules. The list names exactly what code outside `platform/` uses, so an
item the Win32 side adds stays private to it until the shared code needs it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`USED` and `pin_image` are only reached from `platform/win32.rs` now that
`note_create_device` is the interface. `pin_image` moves to the private
section, as CONVENTIONS §File layout puts private items last.

`dll_main` stays `pub`: clippy's `not_unsafe_ptr_arg_deref` only checks
public functions, and `attach_process` relies on that to keep the export
from dereferencing its arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The DIB it creates aliases `SurfaceDcSource::memory`, which GDI reads and
writes until the DC is destroyed. CONVENTIONS §The boundary principle holds
that a safe signature over a caller-supplied pointer is unsound, so the
contract moves into a `# Safety` section and `GetDC` asserts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It reads the locked cursor rows through `src`, a pointer the caller supplies.
Under CONVENTIONS §The boundary principle that contract belongs to an
`unsafe fn`, asserted where `SetCursorProperties` holds the lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CONVENTIONS §File layout keeps an impl block next to its type, and
`DisplayModeInfo` lives in `direct3d9.rs`. The conversion only serves the
Win32 mode queries, so it stays a private function of `fullscreen.rs`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`client_rect_dims` in `direct3d9` is now `window::client_size`. The cursor
entry points moved to `device/cursor.rs`, and the module doc of the Win32
cursor still claimed them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`audit.sh` checks the API lock in `platform/win32/cursor.rs`, where
`cursor_wnd_proc` lives, so the section's file list names it. The unsafe
wrappers example said `cursor.rs`, which now names two files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@athei athei left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The rule that Win32 imports and the cfgs that tell the two builds apart stay in platform.rs and platform/ is written down only in module docs. Add a CONVENTIONS section for it and a make audit check that fails, anywhere else in windows/d3d9/src, on an unsafe extern block, #[link(, os::windows, or a cfg, cfg! or cfg_attr that names windows, unix or target_os. The tree at this head passes it.

Comment thread windows/d3d9/src/platform.rs Resolved
Comment thread windows/d3d9/src/lib.rs Resolved
kanapitsas and others added 7 commits November 16, 2026 16:36
CONVENTIONS orders a file's public items before its private ones, and
adapter.rs, capture.rs, surface_dc.rs and log_location.rs had private
helpers above the public functions. The comment in select_bitmap named
an unlock that now happens in its caller, and the one above
acquire_logger still used its old name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shared code builds SurfaceDcSource field by field and LeaveKind variant
by variant, so a second platform would need identical copies of both.
They now live beside the code that builds them, as DisplayModeInfo
does. SurfaceDc and SavedWindow stay in the platform module, since
shared code only holds them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
platform.rs re-exported cursor and fullscreen as whole modules, so it
did not say what a second implementation has to define. Two inline
modules now re-export exactly the items shared code uses, and the call
sites are unchanged. The fullscreen items only the Win32 implementation
uses are narrowed to it: the window procedure helpers and Rect to the
platform module, primary_monitor_rect and restore_registry_mode to
fullscreen.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ShowCursor's rules and the software overlay make no Win32 call, yet
they lived in the Win32 cursor module, so a native build would have
had to restate them. CursorState now lives in device/cursor.rs with
the visibility, the resize latch, the software and native-hidden
flags, a cursor-set flag, the hash, the source bitmap, the scale, the
uploaded-sprite set and the overlay sync. ShowCursor reads the
cursor-set flag instead of a null HCURSOR.

The Win32 part is one field, SystemCursor: the HCURSOR cache and
builds, SetCursor and the kick, the pointer position, the foreground
query, the hitch probe and the subclass. Its window procedure reads
the visibility and the resize latch through CursorState. The Win32
calls, their order and conditions, the locking and the log lines are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before the split, the blank HCURSOR made the cursor count as set as
soon as select_bitmap built it, before the surface was unlocked. The
cursor-set flag only came with realize_bitmap, so a ShowCursor that an
application's UnlockRect reentered saw no cursor set. The flag is now
set where the blank is taken, as the handle was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rule that the system is reached only through platform.rs and
platform/ was written down only in module docs, so nothing would
catch a Win32 import or a build-telling cfg added to shared code.
CONVENTIONS now states it, and make audit fails on an unsafe extern
block, #[link(, os::windows, or a cfg, cfg! or cfg_attr naming
windows, unix or target_os anywhere else in windows/d3d9/src.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A name after the cfg on the same line, such as a variable called
windows, was reported as a platform cfg. An unsafe extern block with
more than one space before its ABI passed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
kanapitsas and others added 3 commits November 16, 2026 21:39
Text after the cfg on the same line could leave the count open, as in
`if cfg!(test) && call(`. The lines up to the call's end were then
read as part of that cfg, and a platform cfg among them was missed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Only the first cfg of a line was read, so `cfg!(test) && cfg!(windows)`
passed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kanapitsas

Copy link
Copy Markdown
Contributor Author

All points applied. The commits are named below.

  • Audit and CONVENTIONS (c081781, ca1dd9c, 9bd9cb4, 74eb84c): section "Win32 stays behind platform.rs" and a platform_split check in audit.sh. A cfg is read up to its closing parenthesis, also across lines, and every cfg on a line is read. The check matches text and is not a Rust parser. It flags a match inside a /* */ comment or a string. It expects an unsafe extern block and its brace on one line, as rustfmt writes them.
  • Cursor (d53c3db, 7adb7a7): the state, the D3D9 rules and the overlay path are in device/cursor.rs. SystemCursor is the one platform field. Two things are not a plain move:
    • A SET flag replaces the null-HCURSOR tests in show and overlay_flags. The software path sets it once it has the blank cursor, and realize_bitmap sets it too. destroy_handles clears it before the images are destroyed.
    • rebuild_current writes hash after the platform rebuild returns. The rebuild calls SetCursor when the cursor is visible, so hash is now written after that call and not before it. Only shared code reads hash, under the API lock.
  • platform.rs (7aa6eda): inline cursor and fullscreen modules name the used items. The eight you listed are pub(super) or private now, and AppProcedureCall with them.
  • Shared types (dfaac9d): SurfaceDcSource is in surface.rs, LeaveKind in device.rs.
  • Nits (bb1235b): done. log_location.rs is the one at the crate root.

make check passes. I did not run the Wine suites locally.

@athei athei left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Open before merge:

  • Merge main in (no rebase), the branch conflicts with it.
  • The comments on platform_split.

Comment thread scripts/audit.sh
}
FNR == 1 { nest = 0 }
/^[ \t]*\/\// { next }
nest > 0 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

A cfg the scan never closes turns the check off for the rest of the file. While nest is set, every line that is not a comment ends in this branch's next, names_platform runs only on the close, and FNR == 1 clears nest without a finding. One unbalanced ( in a trailing comment inside a multi-line cfg is enough:

#[cfg(any(
    windows, // (Win32 build
    unix,
))]
unsafe extern "system" { fn GetTickCount() -> u32; }

Neither the cfg nor the import is reported. Report a cfg that is still open when its file ends.

Comment thread scripts/audit.sh Outdated
return s
}
function names_platform(s) {
return closed(s) ~ /(^|[^A-Za-z0-9_])(windows|unix|target_os)([^A-Za-z0-9_]|$)/

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

target_os is the only target_* key named, so cfg(target_vendor = "apple") and cfg(target_arch = "x86") pass in shared code. Match every target_* key, in the script and in the CONVENTIONS row and section. Shared code at this head has none.

kanapitsas and others added 2 commits November 17, 2026 11:33
config.rs conflicted: main added the Steam app id to the profile
lookup, next to the version-blob read this branch moved behind the
platform. The lookup keeps both.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A cfg whose parentheses the scan never balanced hid every later line
of its file from the check. An unbalanced parenthesis in a trailing
comment was enough. Such a cfg is now a finding.

Only target_os was matched, so target_vendor or target_arch passed in
shared code.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kanapitsas

Copy link
Copy Markdown
Contributor Author

Main is merged in, without a rebase, and both platform_split comments are applied.

  • Merge (7f5315a): one conflict, in config.rs. The profile lookup keeps the platform call for the version blob and takes main's Steam app id.
  • Open cfg (2f9296d): a cfg still open at the end of its file is a finding. Your example now reports cfg never closed at its first line.
  • target_* (2f9296d): every target_* key matches, in the script, the CONVENTIONS row and the section. Shared code has none.

make check passes on the merged head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants