Repository navigation
dmd: keep the native window above the player's window #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f792af0
dmd: keep the native window above the player's window.
freezy 40f36a3
dmd: keep an INI's stayontop when another component setting changes.
freezy 3add21f
dmd: keep the stacking set on the component through its other changes.
freezy 02f4c23
dmd: let a saved INI drop the component's stacking also when another …
freezy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Visual Pinball Engine | ||
| // Copyright (C) 2026 freezy and VPE Team | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
| // Licensed under MIT. See https://opensource.org/license/mit. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using UnityEngine; | ||
|
|
||
| namespace VisualPinball.Engine.DMD.Unity | ||
| { | ||
| /// <summary> | ||
| /// The window the player draws the game in, which the native DMD window must stay above where they overlap. Without | ||
| /// "Keep on top", the player's window covered it each time it took the focus, such as when the game started. | ||
| /// </summary> | ||
| internal static class PlayerWindow | ||
| { | ||
| private const string UnityWindowClass = "UnityWndClass"; // the player's own; the DMD's and other outputs' have their own | ||
| private static IntPtr _window; | ||
|
|
||
| /// <summary>Whether the native DMD window stays on top: as set, and always where it lies over the player's window.</summary> | ||
| internal static bool OnTop(bool stayOnTop, RectInt window, RectInt? player) | ||
| => stayOnTop || player is { } bounds && bounds.Overlaps(window); | ||
|
|
||
| internal static bool OnTop(bool stayOnTop, int left, int top, int width, int height) | ||
| => stayOnTop || OnTop(false, new RectInt(left, top, width, height), Bounds()); | ||
|
|
||
| /// <summary> | ||
| /// Where the player's window is on the desktop, in physical pixels. Null in the editor, on other platforms, while it is | ||
| /// minimized or hidden, and in exclusive fullscreen, which no window shows over, like the Player's own outputs. | ||
| /// </summary> | ||
| internal static RectInt? Bounds() | ||
| { | ||
| try { | ||
| if (Application.platform != RuntimePlatform.WindowsPlayer || Screen.fullScreenMode == FullScreenMode.ExclusiveFullScreen) return null; | ||
| if (_window == IntPtr.Zero || !IsWindow(_window)) _window = Find(); | ||
| if (_window == IntPtr.Zero || !IsWindowVisible(_window) || IsIconic(_window)) return null; | ||
| // what shows of it: a framed window's rectangle takes in the resize borders, which are invisible, so a DMD | ||
| // right next to it would count as over it | ||
| if (DwmGetWindowAttribute(_window, ExtendedFrameBounds, out var rect, Marshal.SizeOf<NativeRect>()) != 0 | ||
| && !GetWindowRect(_window, out rect)) return null; | ||
| return new RectInt(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); | ||
| } catch (Exception) { | ||
| return null; // off the main thread, where Unity's screen can't be asked: the next restack on it decides | ||
| } | ||
| } | ||
|
|
||
| private static IntPtr Find() | ||
| { | ||
| uint process = (uint)System.Diagnostics.Process.GetCurrentProcess().Id; | ||
| var found = IntPtr.Zero; | ||
| var name = new StringBuilder(64); | ||
| EnumWindows((window, _) => { | ||
| GetWindowThreadProcessId(window, out uint owner); | ||
| if (owner != process) return true; | ||
| name.Clear(); | ||
| if (GetClassName(window, name, name.Capacity) == 0 || name.ToString() != UnityWindowClass) return true; | ||
| found = window; | ||
| return false; | ||
| }, IntPtr.Zero); | ||
| return found; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] private struct NativeRect { public int Left, Top, Right, Bottom; } | ||
| private delegate bool EnumWindowsProc(IntPtr window, IntPtr data); | ||
| [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc callback, IntPtr data); | ||
| [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr window, out uint process); | ||
| [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern int GetClassName(IntPtr window, StringBuilder name, int count); | ||
| [DllImport("user32.dll")] private static extern bool IsWindow(IntPtr window); | ||
| [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr window); | ||
| [DllImport("user32.dll")] private static extern bool IsIconic(IntPtr window); | ||
| [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr window, out NativeRect rect); | ||
| private const int ExtendedFrameBounds = 9; // DWMWA_EXTENDED_FRAME_BOUNDS | ||
| [DllImport("dwmapi.dll")] private static extern int DwmGetWindowAttribute(IntPtr window, int attribute, out NativeRect rect, int size); | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.