Repository navigation
d3d9: Move the Win32 code behind a platform module - #1232
kanapitsas wants to merge 21 commits into
Conversation
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>
There was a problem hiding this comment.
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.
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>
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>
|
All points applied. The commits are named below.
|
athei
left a comment
There was a problem hiding this comment.
Open before merge:
- Merge main in (no rebase), the branch conflicts with it.
- The comments on
platform_split.
| } | ||
| FNR == 1 { nest = 0 } | ||
| /^[ \t]*\/\// { next } | ||
| nest > 0 { |
There was a problem hiding this comment.
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.
| return s | ||
| } | ||
| function names_platform(s) { | ||
| return closed(s) ~ /(^|[^A-Za-z0-9_])(windows|unix|target_os)([^A-Za-z0-9_]|$)/ |
There was a problem hiding this comment.
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.
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>
|
Main is merged in, without a rebase, and both
|
Problem
A native macOS build (#1193) needs the Win32 calls of
d3d9behind one interface. Today they are spread over many modules.Changes
platform.rsdeclares one module and picks its file by target.platform/win32.rsandplatform/win32/holdDllMain, 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.rsandfullscreen.rsmove 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 indevice/cursor.rs.platform/win32/cursor.rskeepsSystemCursor, one field of that state: theHCURSORcache and build,SetCursor, the pointer position and the window subclass.surface.rs: the GDI part ofGetDCgoes toplatform/win32/surface_dc.rs.SurfaceDcSourcestays shared.direct3d9.rs: the LUID query goes toplatform/win32/adapter.rsand the client rect toplatform/win32/window.rs.log_sink.rs: the log location stays shared inlog_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.rsmoves into the platform module. The non-Windows arm is removed here and comes back in the native PR, inside its platform file.platform.rsnamesplatform/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
cfg_attr(path)pair inplatform.rs. The remaining architecture cfgs are inplatform/win32.rsandplatform/win32/.pubitems, all inside the crate: the platform interface listed inplatform.rs,device::cursor, anddirect3d9::PRIMARY_DISPLAY_NAME.DisplayModeInfo,LeaveKindandSurfaceDcSourceare defined once in shared code.create_surface_dc,SystemCursor::selectandCursorState::select_bitmapareunsafe fn: they arepuband read through a caller's pointer.SET.ShowCursorand the overlay flags tested for a nullHCURSORbefore. The software path sets it once it has the blank cursor, andrealize_bitmapsets it too.destroy_handlesclears it.make auditcheck: Win32 imports and platform cfgs stay inplatform.rsandplatform/. The check matches text and is not a Rust parser.audit.sh,derive_inventory.txtandCONVENTIONS.mdfollow the moved paths.Verification
make check: every leg passed.make testand conformance: not run locally. CI ran them.🤖 Generated with Claude Code