From f792af0e9fa45ebda1459449b9d74090253b7b67 Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 29 Nov 2026 02:43:10 +0100 Subject: [PATCH 1/4] dmd: keep the native window above the player's window. Without "Keep on top", the native DMD window was covered by the player's window each time that took the focus, such as when a game started: a DMD placed over the playfield disappeared behind it. While the window lies over the player's window, it is now kept on top whatever the setting, and checked again every quarter second, so it follows when it is moved. Elsewhere, in exclusive fullscreen, in the editor and on other platforms, it keeps the setting. The bounds compared are the visible frame's, not a framed window's invisible resize borders. The window's forced stacking is not written back into the settings, and moving the window now only copies its bounds, so a "stayontop" from DmdDevice.ini survives a move. --- .../Runtime/DmdBridgePlayer.cs | 26 ++++--- .../Runtime/DmdPipeline.cs | 15 ++++ .../Runtime/NativeWindowDestinationFactory.cs | 2 +- .../Runtime/PlayerWindow.cs | 78 +++++++++++++++++++ .../Runtime/PlayerWindow.cs.meta | 2 + .../Runtime/Plugins/DmdPluginFactory.cs | 3 +- .../Tests/NativeWindowStackingTests.cs | 38 +++++++++ .../Tests/NativeWindowStackingTests.cs.meta | 2 + 8 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs create mode 100644 VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs.meta create mode 100644 VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs create mode 100644 VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs.meta diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs index cffaeab..fa51efc 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs @@ -122,6 +122,7 @@ public class DmdBridgePlayer : MonoBehaviour, IDiagnosticsSource private bool _nativeWindowWarningLogged; private bool _missingConfigWarningLogged; private float _nextNativeWindowLayoutSyncTime; + private float _nextRestackTime; private float _suppressNativeWindowLayoutSyncUntil; private bool _defaultAltColorPathResolved; private string _defaultAltColorPath; @@ -237,7 +238,12 @@ private void Update() { // Drives a host-pumped native window (macOS/SDL) on the main thread; no-op on the // self-driven Windows backend. Must run every frame. - (_managed ? _managedInput?.Pipeline as DmdPipeline : _pipeline)?.PumpMainThread(); + var pipeline = _managed ? _managedInput?.Pipeline as DmdPipeline : _pipeline; + pipeline?.PumpMainThread(); + if (pipeline != null && Time.unscaledTime >= _nextRestackTime) { + _nextRestackTime = Time.unscaledTime + SettingsCheckInterval; + pipeline.RestackNativeWindows(_settings?.NativeWindowStayOnTop ?? false); + } if (_managed || PlayerIntegrationMode.HostManaged) return; // Settings (component fields + DmdDevice.ini) change rarely; checking ~4x/sec keeps the @@ -874,7 +880,9 @@ private void SyncNativeWindowLayout() } _nextNativeWindowLayoutSyncTime = Time.unscaledTime + 0.15f; - if (!_pipeline.TryReadNativeWindowLayout(out var left, out var top, out var width, out var height, out var stayOnTop, out var isMovingOrSizing) + // the window's stacking isn't the user's to change there, and over the player's window it's kept on top whatever + // the setting: only where it was moved to goes back into the settings + if (!_pipeline.TryReadNativeWindowLayout(out var left, out var top, out var width, out var height, out _, out var isMovingOrSizing) || isMovingOrSizing) { return; } @@ -882,8 +890,7 @@ private void SyncNativeWindowLayout() if (_nativeWindowLeft == left && _nativeWindowTop == top && _nativeWindowWidth == width - && _nativeWindowHeight == height - && _nativeWindowStayOnTop == stayOnTop) { + && _nativeWindowHeight == height) { return; } @@ -891,13 +898,13 @@ private void SyncNativeWindowLayout() _nativeWindowTop = top; _nativeWindowWidth = width; _nativeWindowHeight = height; - _nativeWindowStayOnTop = stayOnTop; - CopyPresentationSettingsTo(_settings); - CopyPresentationSettingsTo(_appliedSettings); - CopyPresentationSettingsTo(_lastFallbackSettings); + CopyNativeWindowBoundsTo(_settings); + CopyNativeWindowBoundsTo(_appliedSettings); + CopyNativeWindowBoundsTo(_lastFallbackSettings); } - private void CopyPresentationSettingsTo(DmdBridgeSettings settings) + // Not the stacking: that stays what the settings say, which can come from DmdDevice.ini rather than this component. + private void CopyNativeWindowBoundsTo(DmdBridgeSettings settings) { if (settings == null) { return; @@ -907,7 +914,6 @@ private void CopyPresentationSettingsTo(DmdBridgeSettings settings) settings.NativeWindowTop = _nativeWindowTop; settings.NativeWindowWidth = _nativeWindowWidth; settings.NativeWindowHeight = _nativeWindowHeight; - settings.NativeWindowStayOnTop = _nativeWindowStayOnTop; } private static void CopyPresentationSettings(DmdBridgeSettings source, DmdBridgeSettings target) diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/DmdPipeline.cs b/VisualPinball.Engine.DMD.Unity/Runtime/DmdPipeline.cs index 0577db1..1eb52a6 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/DmdPipeline.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/DmdPipeline.cs @@ -177,6 +177,21 @@ public void ApplySettings(DmdBridgeSettings settings) } } + /// + /// Keeps the native windows above the player's window where they lie over it, wherever they were moved, and gives the + /// others the stacking set for them. + /// + public void RestackNativeWindows(bool stayOnTop) + { + foreach (var destination in _destinations) { + if (destination is INativeDmdWindow window && !window.IsMovingOrSizing) { + int left = window.WindowLeft, top = window.WindowTop, width = window.WindowWidth, height = window.WindowHeight; + bool onTop = PlayerWindow.OnTop(stayOnTop, left, top, width, height); + if (window.WindowStayOnTop != onTop) window.ConfigureWindow(new DmdWindowLayout(left, top, width, height, onTop)); + } + } + } + public bool TryReadNativeWindowLayout(out int left, out int top, out int width, out int height, out bool stayOnTop, out bool isMovingOrSizing) { foreach (var destination in _destinations) { diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/NativeWindowDestinationFactory.cs b/VisualPinball.Engine.DMD.Unity/Runtime/NativeWindowDestinationFactory.cs index 6d3f000..76b16bf 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/NativeWindowDestinationFactory.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/NativeWindowDestinationFactory.cs @@ -35,7 +35,7 @@ public static DmdWindowLayout CreateLayout(DmdBridgeSettings settings) settings.NativeWindowTop, settings.NativeWindowWidth, settings.NativeWindowHeight, - settings.NativeWindowStayOnTop); + PlayerWindow.OnTop(settings.NativeWindowStayOnTop, settings.NativeWindowLeft, settings.NativeWindowTop, settings.NativeWindowWidth, settings.NativeWindowHeight)); } public static DmdWindowStyle CreateStyle(DmdBridgeSettings settings) diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs b/VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs new file mode 100644 index 0000000..ef919c1 --- /dev/null +++ b/VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs @@ -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 +{ + /// + /// 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. + /// + 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; + + /// Whether the native DMD window stays on top: as set, and always where it lies over the player's window. + 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()); + + /// + /// 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. + /// + 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()) != 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); + } +} diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs.meta b/VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs.meta new file mode 100644 index 0000000..a753189 --- /dev/null +++ b/VisualPinball.Engine.DMD.Unity/Runtime/PlayerWindow.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: da6b97f13f54fba44815b7191b828a22 \ No newline at end of file diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs b/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs index 0f3033a..757ac06 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs @@ -47,6 +47,7 @@ private static PluginField Hardware(string id, string label, SettingKind kind, J { var field = Field(id, label, kind, value, provider, minimum, maximum); field.Scope = ConfigurationScope.GlobalOnly; return field; } + private static PluginField Describe(PluginField field, string description) { field.Description = description; return field; } private static PluginField Coordinate(string id, string label, string row, int value, double? minimum = null, double? maximum = null) { var field = Field(id, label, SettingKind.Integer, value, minimum: minimum, maximum: maximum); field.Row = row; return field; @@ -63,7 +64,7 @@ private static PluginField Coordinate(string id, string label, string row, int v Field("EnableNativeWindow", "Native borderless window", SettingKind.Boolean, false), Coordinate("NativeWindowLeft", "Left", "native-position", 100), Coordinate("NativeWindowTop", "Top", "native-position", 100), Coordinate("NativeWindowWidth", "Width", "native-size", 512, minimum: 1, maximum: 16384), Coordinate("NativeWindowHeight", "Height", "native-size", 128, minimum: 1, maximum: 16384), - Field("NativeWindowStayOnTop", "Keep on top", SettingKind.Boolean, false), Field("FlipHorizontally", "Mirror horizontally", SettingKind.Boolean, false)), + Describe(Field("NativeWindowStayOnTop", "Keep on top", SettingKind.Boolean, false), "Keeps the window above other applications. Over the player's own window it always stays on top, except in exclusive fullscreen."), Field("FlipHorizontally", "Mirror horizontally", SettingKind.Boolean, false)), new PluginSettingsGroup("Cabinet hardware", "Outputs are acquired exclusively. A disabled plugin opens no device.", Hardware("EnableZeDmd", "ZeDMD", SettingKind.Boolean, false), Hardware("ZeDmdPort", "ZeDMD port", SettingKind.Text, ""), Hardware("ZeDmdBrightness", "ZeDMD brightness", SettingKind.Integer, 8, minimum: 0, maximum: 15), Hardware("ZeDmdDebug", "ZeDMD diagnostics", SettingKind.Boolean, false), Hardware("EnablePinDmd3", "PinDMD 3", SettingKind.Boolean, false), Hardware("PinDmd3Port", "PinDMD 3 port", SettingKind.Text, ""), diff --git a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs new file mode 100644 index 0000000..dccd525 --- /dev/null +++ b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs @@ -0,0 +1,38 @@ +// Visual Pinball Engine +// Copyright (C) 2026 freezy and VPE Team +// SPDX-License-Identifier: MIT +using System.Reflection; +using NUnit.Framework; +using UnityEngine; + +namespace VisualPinball.Engine.DMD.Tests +{ + public sealed class NativeWindowStackingTests + { + private static readonly RectInt Player = new(0, 0, 1920, 1080); + + private static bool OnTop(bool stayOnTop, RectInt window, RectInt? player) + { + var type = typeof(VisualPinball.Engine.DMD.Unity.DmdBridgeSettings).Assembly.GetType("VisualPinball.Engine.DMD.Unity.PlayerWindow", true); + var method = type.GetMethod("OnTop", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(bool), typeof(RectInt), typeof(RectInt?) }, null); + return (bool)method.Invoke(null, new object[] { stayOnTop, window, player }); + } + + [Test] + public void ADmdOverThePlayersWindowStaysOnTopWithoutTheSetting() + { + Assert.That(OnTop(false, new RectInt(700, 40, 512, 128), Player), Is.True, "over the playfield"); + Assert.That(OnTop(false, new RectInt(1800, 1000, 512, 128), Player), Is.True, "partly over it"); + } + + [Test] + public void ADmdElsewhereKeepsTheSetting() + { + Assert.That(OnTop(false, new RectInt(1920, 0, 512, 128), Player), Is.False, "on the next monitor, edge to edge"); + Assert.That(OnTop(true, new RectInt(1920, 0, 512, 128), Player), Is.True); + // no player window to go by: in the editor, minimized, or in exclusive fullscreen + Assert.That(OnTop(false, new RectInt(700, 40, 512, 128), null), Is.False); + Assert.That(OnTop(true, new RectInt(700, 40, 512, 128), null), Is.True); + } + } +} diff --git a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs.meta b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs.meta new file mode 100644 index 0000000..b9962cc --- /dev/null +++ b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: f0764d2e8dcb5694a81a7fa737e100a8 \ No newline at end of file From 40f36a3b2a8e05b25f352c42c67f9d5633250d86 Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 29 Nov 2026 02:52:05 +0100 Subject: [PATCH 2/4] dmd: keep an INI's stayontop when another component setting changes. Once a setting on the component changes, its look and placement replace the INI's. The window's stacking used to be read back into the component, so under an INI's "stayontop" its field had become true by then. It no longer is, and changing the dot size took the window off the top. The stacking now only comes from the component when it is what changed. --- .../Runtime/DmdBridgePlayer.cs | 16 ++++++++++- .../Tests/NativeWindowStackingTests.cs | 27 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs index fa51efc..32dcdd3 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs @@ -766,7 +766,7 @@ private DmdBridgeSettings LoadSettings() var fallbackChanged = _lastFallbackSettings != null && !fallback.Equals(_lastFallbackSettings); var settings = DmdBridgeConfig.Load(configPath, fallback); if (fallbackChanged) { - CopyPresentationSettings(fallback, settings); + ApplyComponentPresentation(_lastFallbackSettings, fallback, settings); } _lastFallbackSettings = fallback.Clone(); return settings; @@ -916,6 +916,20 @@ private void CopyNativeWindowBoundsTo(DmdBridgeSettings settings) settings.NativeWindowHeight = _nativeWindowHeight; } + /// + /// The component's look and placement over what DmdDevice.ini says, once one of the component's settings changed. + /// The stacking only when it is what changed: the window's own isn't read back into the component, so its value + /// can still be the default under an INI's stayontop. + /// + internal static void ApplyComponentPresentation(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings target) + { + var stayOnTop = target.NativeWindowStayOnTop; + CopyPresentationSettings(component, target); + if (previous != null && component.NativeWindowStayOnTop == previous.NativeWindowStayOnTop) { + target.NativeWindowStayOnTop = stayOnTop; + } + } + private static void CopyPresentationSettings(DmdBridgeSettings source, DmdBridgeSettings target) { target.NativeWindowLeft = source.NativeWindowLeft; diff --git a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs index dccd525..678b02e 100644 --- a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs +++ b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs @@ -4,6 +4,7 @@ using System.Reflection; using NUnit.Framework; using UnityEngine; +using VisualPinball.Engine.DMD.Unity; namespace VisualPinball.Engine.DMD.Tests { @@ -25,6 +26,32 @@ public void ADmdOverThePlayersWindowStaysOnTopWithoutTheSetting() Assert.That(OnTop(false, new RectInt(1800, 1000, 512, 128), Player), Is.True, "partly over it"); } + private static DmdBridgeSettings ApplyComponent(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings ini) + { + var method = typeof(DmdBridgePlayer).GetMethod("ApplyComponentPresentation", BindingFlags.Static | BindingFlags.NonPublic); + method.Invoke(null, new object[] { previous, component, ini }); + return ini; + } + + [Test] + public void AnotherEditOnTheComponentKeepsTheIniStacking() + { + // the INI says stayontop, the component's field is still its default, and only the dot size was changed on it + var previous = new DmdBridgeSettings { NativeWindowStayOnTop = false, DotSize = 0.9f }; + var component = new DmdBridgeSettings { NativeWindowStayOnTop = false, DotSize = 0.7f }; + var settings = ApplyComponent(previous, component, new DmdBridgeSettings { NativeWindowStayOnTop = true, DotSize = 0.9f }); + Assert.That(settings.NativeWindowStayOnTop, Is.True); + Assert.That(settings.DotSize, Is.EqualTo(0.7f)); + } + + [Test] + public void ChangingTheStackingOnTheComponentOverridesTheIni() + { + var previous = new DmdBridgeSettings { NativeWindowStayOnTop = false }; + var component = new DmdBridgeSettings { NativeWindowStayOnTop = true }; + Assert.That(ApplyComponent(previous, component, new DmdBridgeSettings { NativeWindowStayOnTop = false }).NativeWindowStayOnTop, Is.True); + } + [Test] public void ADmdElsewhereKeepsTheSetting() { From 3add21ff0c90337dae4f11e16ab74b1faf98c1ae Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 29 Nov 2026 03:26:26 +0100 Subject: [PATCH 3/4] dmd: keep the stacking set on the component through its other changes. Turning Keep on top on or off on the component now holds while other component settings change, instead of falling back to the INI's stayontop on the next edit. Editing the INI, or removing it, drops that choice again. --- .../Runtime/DmdBridgePlayer.cs | 27 ++++++++++++++----- .../Tests/NativeWindowStackingTests.cs | 22 +++++++++++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs index 32dcdd3..1c79f92 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs @@ -116,6 +116,8 @@ public class DmdBridgePlayer : MonoBehaviour, IDiagnosticsSource private DmdBridgeSettings _settings; private DmdBridgeSettings _appliedSettings; private DmdBridgeSettings _lastFallbackSettings; + /// The stacking set on the component over the INI's, until the INI changes; null while the INI's holds. + private bool? _stayOnTopOverride; private DateTime _appliedConfigWriteTimeUtc; private string _appliedConfigPath; private bool _missingDisplayWarningLogged; @@ -745,6 +747,10 @@ private DmdBridgeSettings LoadSettings() Logger.Info("[DMD] No DmdDevice.ini found at {Path:l}, using component settings.", configPath); _missingConfigWarningLogged = true; } + // without the INI only the component counts, and a restored INI is read anew + _appliedConfigPath = null; + _appliedConfigWriteTimeUtc = default; + _stayOnTopOverride = null; _lastFallbackSettings = fallback.Clone(); return fallback; } @@ -761,12 +767,16 @@ private DmdBridgeSettings LoadSettings() return _settings; } + var iniChanged = !string.Equals(_appliedConfigPath, configPath, StringComparison.Ordinal) || _appliedConfigWriteTimeUtc != writeTimeUtc; _appliedConfigPath = configPath; _appliedConfigWriteTimeUtc = writeTimeUtc; var fallbackChanged = _lastFallbackSettings != null && !fallback.Equals(_lastFallbackSettings); var settings = DmdBridgeConfig.Load(configPath, fallback); if (fallbackChanged) { - ApplyComponentPresentation(_lastFallbackSettings, fallback, settings); + _stayOnTopOverride = ApplyComponentPresentation(_lastFallbackSettings, fallback, settings, _stayOnTopOverride); + } else if (iniChanged) { + // the INI was edited since: what it says now holds, as for the rest of the presentation + _stayOnTopOverride = null; } _lastFallbackSettings = fallback.Clone(); return settings; @@ -918,16 +928,21 @@ private void CopyNativeWindowBoundsTo(DmdBridgeSettings settings) /// /// The component's look and placement over what DmdDevice.ini says, once one of the component's settings changed. - /// The stacking only when it is what changed: the window's own isn't read back into the component, so its value - /// can still be the default under an INI's stayontop. + /// The stacking only once it was changed on the component: the window's own isn't read back into the component, + /// so its value can still be the default under an INI's stayontop. Changed there, it holds through the + /// component's other changes. /// - internal static void ApplyComponentPresentation(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings target) + /// The stacking changed on the component before, null if it wasn't + /// The stacking changed on the component so far, null if it wasn't + internal static bool? ApplyComponentPresentation(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings target, bool? stayOnTopOverride) { var stayOnTop = target.NativeWindowStayOnTop; CopyPresentationSettings(component, target); - if (previous != null && component.NativeWindowStayOnTop == previous.NativeWindowStayOnTop) { - target.NativeWindowStayOnTop = stayOnTop; + if (previous == null || component.NativeWindowStayOnTop != previous.NativeWindowStayOnTop) { + return component.NativeWindowStayOnTop; } + target.NativeWindowStayOnTop = stayOnTopOverride ?? stayOnTop; + return stayOnTopOverride; } private static void CopyPresentationSettings(DmdBridgeSettings source, DmdBridgeSettings target) diff --git a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs index 678b02e..f1536fb 100644 --- a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs +++ b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs @@ -26,10 +26,12 @@ public void ADmdOverThePlayersWindowStaysOnTopWithoutTheSetting() Assert.That(OnTop(false, new RectInt(1800, 1000, 512, 128), Player), Is.True, "partly over it"); } - private static DmdBridgeSettings ApplyComponent(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings ini) + private static DmdBridgeSettings ApplyComponent(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings ini) => ApplyComponent(previous, component, ini, null, out _); + + private static DmdBridgeSettings ApplyComponent(DmdBridgeSettings previous, DmdBridgeSettings component, DmdBridgeSettings ini, bool? stayOnTopOverride, out bool? overrideAfter) { var method = typeof(DmdBridgePlayer).GetMethod("ApplyComponentPresentation", BindingFlags.Static | BindingFlags.NonPublic); - method.Invoke(null, new object[] { previous, component, ini }); + overrideAfter = (bool?)method.Invoke(null, new object[] { previous, component, ini, stayOnTopOverride }); return ini; } @@ -52,6 +54,22 @@ public void ChangingTheStackingOnTheComponentOverridesTheIni() Assert.That(ApplyComponent(previous, component, new DmdBridgeSettings { NativeWindowStayOnTop = false }).NativeWindowStayOnTop, Is.True); } + [Test] + public void TheStackingChangedOnTheComponentHoldsThroughItsOtherEdits() + { + // the INI says no stayontop; it's turned on on the component, then only the dot size changes + var first = ApplyComponent(new DmdBridgeSettings { NativeWindowStayOnTop = false }, new DmdBridgeSettings { NativeWindowStayOnTop = true }, + new DmdBridgeSettings { NativeWindowStayOnTop = false }, null, out var stayOnTop); + Assert.That(first.NativeWindowStayOnTop, Is.True); + Assert.That(stayOnTop, Is.True); + + var second = ApplyComponent(new DmdBridgeSettings { NativeWindowStayOnTop = true, DotSize = 0.9f }, new DmdBridgeSettings { NativeWindowStayOnTop = true, DotSize = 0.7f }, + new DmdBridgeSettings { NativeWindowStayOnTop = false, DotSize = 0.9f }, stayOnTop, out stayOnTop); + Assert.That(second.NativeWindowStayOnTop, Is.True); + Assert.That(second.DotSize, Is.EqualTo(0.7f)); + Assert.That(stayOnTop, Is.True); + } + [Test] public void ADmdElsewhereKeepsTheSetting() { From 02f4c23e731e31e4bdd4e00c2234a5d7eb8c2e3d Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 29 Nov 2026 05:09:22 +0100 Subject: [PATCH 4/4] dmd: let a saved INI drop the component's stacking also when another component setting changes. When DmdDevice.ini was saved and another component setting changed before the same settings check, the stacking set on the component earlier was carried over, and since the INI's new write time was recorded, no later check dropped it: the component's Keep on top kept beating the INI's stayontop. A saved INI now drops it first, and a change of the stacking on the component in the same check still sets it again. --- .../Runtime/DmdBridgePlayer.cs | 8 +-- .../Tests/NativeWindowStackingTests.cs | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs index 1c79f92..cdf9101 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/DmdBridgePlayer.cs @@ -772,11 +772,13 @@ private DmdBridgeSettings LoadSettings() _appliedConfigWriteTimeUtc = writeTimeUtc; var fallbackChanged = _lastFallbackSettings != null && !fallback.Equals(_lastFallbackSettings); var settings = DmdBridgeConfig.Load(configPath, fallback); + if (iniChanged) { + // the INI was edited since: what it says now holds, as for the rest of the presentation, unless the + // stacking also changed on the component + _stayOnTopOverride = null; + } if (fallbackChanged) { _stayOnTopOverride = ApplyComponentPresentation(_lastFallbackSettings, fallback, settings, _stayOnTopOverride); - } else if (iniChanged) { - // the INI was edited since: what it says now holds, as for the rest of the presentation - _stayOnTopOverride = null; } _lastFallbackSettings = fallback.Clone(); return settings; diff --git a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs index f1536fb..482679d 100644 --- a/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs +++ b/VisualPinball.Engine.DMD.Unity/Tests/NativeWindowStackingTests.cs @@ -1,10 +1,13 @@ // Visual Pinball Engine // Copyright (C) 2026 freezy and VPE Team // SPDX-License-Identifier: MIT +using System; +using System.IO; using System.Reflection; using NUnit.Framework; using UnityEngine; using VisualPinball.Engine.DMD.Unity; +using Object = UnityEngine.Object; namespace VisualPinball.Engine.DMD.Tests { @@ -70,6 +73,55 @@ public void TheStackingChangedOnTheComponentHoldsThroughItsOtherEdits() Assert.That(stayOnTop, Is.True); } + private static void Set(DmdBridgePlayer dmd, string field, object value) + => typeof(DmdBridgePlayer).GetField(field, BindingFlags.Instance | BindingFlags.NonPublic)!.SetValue(dmd, value); + + // a check of the settings, as the bridge does them while it runs + private static DmdBridgeSettings Load(DmdBridgePlayer dmd) + { + var settings = (DmdBridgeSettings)typeof(DmdBridgePlayer).GetMethod("LoadSettings", BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(dmd, null); + Set(dmd, "_settings", settings); + return settings; + } + + [Test] + public void AnIniEditedWithAnotherComponentChangeHasTheStacking() + { + // the INI says no stayontop, it's turned on on the component, then the INI is saved and the dot size changes + // on the component before the same check: the INI holds, as when it's saved alone + var ini = Path.Combine(Path.GetTempPath(), $"DmdDevice-{Guid.NewGuid():N}.ini"); + var envConfig = Environment.GetEnvironmentVariable("DMDDEVICE_CONFIG"); + var gameObject = new GameObject("DMD"); + try { + // the user's DmdDevice.ini would go first + Environment.SetEnvironmentVariable("DMDDEVICE_CONFIG", null); + File.WriteAllText(ini, "[virtualdmd]\nstayontop = false\n"); + var saved = DateTime.UtcNow.AddMinutes(-10); + File.SetLastWriteTimeUtc(ini, saved); + var dmd = gameObject.AddComponent(); + Set(dmd, "_dmdDeviceIniPath", ini); + Set(dmd, "_dotSize", 0.9f); + Assert.That(Load(dmd).NativeWindowStayOnTop, Is.False, "the INI"); + + Set(dmd, "_nativeWindowStayOnTop", true); + Assert.That(Load(dmd).NativeWindowStayOnTop, Is.True, "turned on on the component"); + + Set(dmd, "_dotSize", 0.7f); + Assert.That(Load(dmd).NativeWindowStayOnTop, Is.True, "through another edit on the component"); + + File.SetLastWriteTimeUtc(ini, saved.AddMinutes(1)); + Set(dmd, "_dotSize", 0.5f); + var settings = Load(dmd); + Assert.That(settings.NativeWindowStayOnTop, Is.False, "the INI saved since"); + Assert.That(settings.DotSize, Is.EqualTo(0.5f), "the component's dot size"); + + } finally { + Environment.SetEnvironmentVariable("DMDDEVICE_CONFIG", envConfig); + Object.DestroyImmediate(gameObject); + File.Delete(ini); + } + } + [Test] public void ADmdElsewhereKeepsTheSetting() {