PowerControl: Properly detect internal displays being connected

This commit is contained in:
Kamil Trzciński 2023-01-05 22:46:02 +01:00
parent 64f4050de5
commit 7c1099be48
3 changed files with 380 additions and 71 deletions

View file

@ -17,9 +17,6 @@ namespace PowerControl
public const int KeyPressRepeatTime = 400;
public const int KeyPressNextRepeatTime = 90;
private SynchronizationContext? context;
System.Windows.Forms.Timer contextTimer;
Container components = new Container();
System.Windows.Forms.NotifyIcon notifyIcon;
StartupManager startupManager = new StartupManager(Title);
@ -29,6 +26,8 @@ namespace PowerControl
System.Windows.Forms.Timer osdDismissTimer;
bool isOSDToggled = false;
bool wasInternalDisplayConnected;
hidapi.HidDevice neptuneDevice = new hidapi.HidDevice(0x28de, 0x1205, 64);
SDCInput neptuneDeviceState = new SDCInput();
DateTime? neptuneDeviceNextKey;
@ -38,8 +37,8 @@ namespace PowerControl
static Controller()
{
Dependencies.ValidateHidapi(TitleWithVersion);
Dependencies.ValidateRTSSSharedMemoryNET(TitleWithVersion);
//Dependencies.ValidateHidapi(TitleWithVersion);
//Dependencies.ValidateRTSSSharedMemoryNET(TitleWithVersion);
}
public Controller()
@ -55,11 +54,6 @@ namespace PowerControl
if (Instance.WantsRunOnStartup)
startupManager.Startup = true;
InitializeDisplayContext();
contextTimer?.Start();
SystemEvents.DisplaySettingsChanged += DisplayChangesHandler;
var contextMenu = new System.Windows.Forms.ContextMenuStrip(components);
rootMenu.Init();
@ -205,6 +199,9 @@ namespace PowerControl
dismissNeptuneInput();
});
}
wasInternalDisplayConnected = ExternalHelpers.DisplayConfig.IsInternalConnected.GetValueOrDefault(false);
SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
}
private void OsdTimer_Tick(object? sender, EventArgs e)
@ -399,27 +396,22 @@ namespace PowerControl
}
}
private void InitializeDisplayContext()
{
DeviceManager.LoadDisplays();
contextTimer = new System.Windows.Forms.Timer();
contextTimer.Interval = 200;
contextTimer.Tick += (_, _) =>
{
context = SynchronizationContext.Current;
contextTimer.Stop();
};
}
private void DisplayChangesHandler(object? sender, EventArgs e)
private void SystemEvents_DisplaySettingsChanged(object? sender, EventArgs e)
{
if (DeviceManager.RefreshDisplays())
{
context?.Post((object? state) =>
var isInternalDisplayConnected = ExternalHelpers.DisplayConfig.IsInternalConnected.GetValueOrDefault(false);
if (wasInternalDisplayConnected == isInternalDisplayConnected)
return;
Log.TraceLine("SystemEvents_DisplaySettingsChanged: wasConnected={0}, isConnected={1}",
wasInternalDisplayConnected, isInternalDisplayConnected);
wasInternalDisplayConnected = isInternalDisplayConnected;
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(
new Action(() =>
{
rootMenu.Update();
}, null);
}
})
);
}
}
}

View file

@ -11,30 +11,6 @@ namespace PowerControl.Helpers
{
internal class DeviceManager
{
private static Screen[] screens = new Screen[0];
public static int NumberOfDisplays
{
get { return screens.Length; }
}
public static void LoadDisplays()
{
screens = Screen.AllScreens;
}
public static bool RefreshDisplays()
{
var newScreens = Screen.AllScreens;
if (HaveScreensChanged(newScreens))
{
screens = newScreens;
return true;
}
return false;
}
public static string[]? GetDevices(Guid? classGuid)
{
string? filter = null;
@ -150,25 +126,6 @@ namespace PowerControl.Helpers
}
}
private static bool HaveScreensChanged(Screen[] newScreens)
{
if (screens.Length != newScreens.Length)
{
return true;
}
for (int i = 0; i < screens.Length; i++)
{
if (screens[i].DeviceName != newScreens[i].DeviceName ||
screens[i].Primary != newScreens[i].Primary)
{
return true;
}
}
return false;
}
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern int CM_Locate_DevNode(out IntPtr pdnDevInst, string pDeviceID, int ulFlags);