Skip to content

Commit

Permalink
Added validation to TaskBarLayout
Browse files Browse the repository at this point in the history
Also updated to version 1.8.0
  • Loading branch information
terrymacdonald committed Jun 12, 2022
1 parent 41af765 commit 55b3529
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
4 changes: 2 additions & 2 deletions AMDInfo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ static void Main(string[] args)
NLog.LogManager.Configuration = config;

// Start the Log file
SharedLogger.logger.Info($"AMDInfo/Main: Starting AMDInfo v1.7.9");
SharedLogger.logger.Info($"AMDInfo/Main: Starting AMDInfo v1.8.0");


Console.WriteLine($"\nAMDInfo v1.7.9");
Console.WriteLine($"\nAMDInfo v1.8.0");
Console.WriteLine($"==============");
Console.WriteLine($"By Terry MacDonald 2022\n");

Expand Down
4 changes: 2 additions & 2 deletions AMDInfo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.7.9.0")]
[assembly: AssemblyFileVersion("1.7.9.0")]
[assembly: AssemblyVersion("1.8.0.0")]
[assembly: AssemblyFileVersion("1.8.0.0")]
47 changes: 44 additions & 3 deletions AMDInfo/TaskBarLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public bool ReadFromRegistry(string regKeyValue)
}
catch (Exception ex)
{
SharedLogger.logger.Trace($"TaskBarLayout/ReadFromRegistry: Exception while trying to open RegKey {address}. Unable to get the TaskBarStuckRectangle binary settings. Screen details may not be available yet in registry.");
SharedLogger.logger.Trace(ex, $"TaskBarLayout/ReadFromRegistry: Exception while trying to open RegKey {address}. Unable to get the TaskBarStuckRectangle binary settings. Screen details may not be available yet in registry.");
}
}
else
Expand Down Expand Up @@ -196,7 +196,7 @@ public bool ReadFromRegistry(string regKeyValue)
}
catch (Exception ex)
{
SharedLogger.logger.Trace($"TaskBarLayout/ReadFromRegistry: Exception2 while trying to open RegKey {address}. Unable to get the TaskBarStuckRectangle binary settings. Screen details may not be available yet in registry.");
SharedLogger.logger.Trace(ex, $"TaskBarLayout/ReadFromRegistry: Exception2 while trying to open RegKey {address}. Unable to get the TaskBarStuckRectangle binary settings. Screen details may not be available yet in registry.");
return false;
}

Expand Down Expand Up @@ -615,6 +615,7 @@ public static Dictionary<string, TaskBarLayout> GetAllCurrentTaskBarLayouts(Dict

// Then go through the secondary windows and get the position of them
// Tell Windows to refresh the Other Windows Taskbars if needed
int clonedCount = 0;
try
{
IntPtr lastTaskBarWindowHwnd = (IntPtr)Utils.NULL;
Expand Down Expand Up @@ -677,6 +678,11 @@ public static Dictionary<string, TaskBarLayout> GetAllCurrentTaskBarLayouts(Dict
{
// Invalid state
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Taskbar position was not on a horizontal edge of a monitor!");
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Forcing Taskbar position to be at the bottom");
tbWidth = monWidth;
tbHeight = monHeight - wrkHeight;
tbsr.TaskBarLocation = new System.Drawing.Rectangle(monitorInfo.rcMonitor.left, monitorInfo.rcWork.bottom, tbWidth, tbHeight);
tbsr.Edge = TaskBarEdge.Bottom;
}

}
Expand All @@ -703,12 +709,22 @@ public static Dictionary<string, TaskBarLayout> GetAllCurrentTaskBarLayouts(Dict
{
// Invalid state
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Taskbar position was not on a vertical edge of a monitor!");
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Forcing Taskbar position to be at the bottom");
tbWidth = monWidth;
tbHeight = monHeight - wrkHeight;
tbsr.TaskBarLocation = new System.Drawing.Rectangle(monitorInfo.rcMonitor.left, monitorInfo.rcWork.bottom, tbWidth, tbHeight);
tbsr.Edge = TaskBarEdge.Bottom;
}
}
else
{
// Invalid state
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Taskbar position was not fully along one of the monitor edges!");
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Forcing Taskbar position to be at the bottom");
tbWidth = monWidth;
tbHeight = monHeight - wrkHeight;
tbsr.TaskBarLocation = new System.Drawing.Rectangle(monitorInfo.rcMonitor.left, monitorInfo.rcWork.bottom, tbWidth, tbHeight);
tbsr.Edge = TaskBarEdge.Bottom;
}

tbsr.MonitorLocation = new System.Drawing.Rectangle(monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top, monWidth, monHeight);
Expand All @@ -718,7 +734,8 @@ public static Dictionary<string, TaskBarLayout> GetAllCurrentTaskBarLayouts(Dict
SharedLogger.logger.Trace($"TaskBarLayout/GetAllCurrentTaskBarPositions: Secondary taskbar coordinates are {tbsr.TaskBarLocation.X},{tbsr.TaskBarLocation.Y} and it is {tbsr.TaskBarLocation.Width}x{tbsr.TaskBarLocation.Height}");
SharedLogger.logger.Trace($"TaskBarLayout/GetAllCurrentTaskBarPositions: Secondary taskbar is {tbsr.Edge.ToString("G")}");

// Now as a LAST step we update the Binary field just before we apply it to make sure that the correct binary settings are stored
// Now as a LAST step we update the Binary field to make sure that the correct binary settings are stored
// This means the correct location should be returned even if the registry isn't updated as we're patching the registry object before we store it.
tbsr.PopulateBinaryFromFields();

if (!taskBarStuckRectangles.ContainsKey(monitorInfo.szDevice))
Expand All @@ -728,6 +745,7 @@ public static Dictionary<string, TaskBarLayout> GetAllCurrentTaskBarLayouts(Dict
else
{
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Skipping grabbing Taskbar position from a cloned display {monitorInfo.szDevice}");
clonedCount++;
}

// Prep the next taskbar window so we continue through them
Expand All @@ -739,6 +757,29 @@ public static Dictionary<string, TaskBarLayout> GetAllCurrentTaskBarLayouts(Dict
SharedLogger.logger.Error(ex, $"TaskBarLayout/GetAllCurrentTaskBarPositions: Exception while trying to get a secondary taskbar position");
}

// Check if the display reg keys shown match the display sources
foreach (var tbrKey in taskBarStuckRectangles.Keys)
{
if (tbrKey.Equals("Settings"))
{
continue;
}
// If there isn't a match then we have a problem.
if (!displaySources.ContainsKey(tbrKey))
{
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: We have an error because Display Sources array doesn't include the {tbrKey} taskbar data. This means we have a mismatch somewhere.");
retryNeeded = true;
}
}

// Check if the length of the display sources equals the taskbar locations we're tracking
// Note: taskBarStuckRectangles includes the 'Settings' main screen which is one extra screen that windows stores for the primary screen. We need to remove this from the count as it is extra.
if (displaySources.Count != taskBarStuckRectangles.Keys.Count - 1)
{
SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: We have an error because Display Sources array length doesn't match the taskBarStuckRectangles array length. This means we have a mismatch somewhere.");
retryNeeded = true;
}

retryNeeded = false;
return taskBarStuckRectangles;
}
Expand Down
14 changes: 12 additions & 2 deletions AMDInfo/WinLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,8 +1611,18 @@ public bool SetActiveConfig(WINDOWS_DISPLAY_CONFIG displayConfig)
if (result == CHANGE_DISPLAY_RESULTS.Successful)
{
SharedLogger.logger.Trace($"WinLibrary/SetActiveConfig: Success. The GDI Device Mode will work for display {displayDeviceKey}.");
SharedLogger.logger.Trace($"WinLibrary/SetActiveConfig: Actually going to try to set the GDI Device Mode for display {displayDeviceKey} now.");
result = GDIImport.ChangeDisplaySettingsEx(currentDeviceSetting.Device.DeviceName, ref currentDeviceSetting.DeviceMode, IntPtr.Zero, (CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_UPDATEREGISTRY | CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_NORESET), IntPtr.Zero);
// Set the
if (currentDeviceSetting.IsPrimary)
{
SharedLogger.logger.Trace($"WinLibrary/SetActiveConfig: Actually going to try to set the GDI Device Mode for display {displayDeviceKey} now (primary display).");
result = GDIImport.ChangeDisplaySettingsEx(currentDeviceSetting.Device.DeviceName, ref currentDeviceSetting.DeviceMode, IntPtr.Zero, (CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_SET_PRIMARY | CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_UPDATEREGISTRY | CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_NORESET), IntPtr.Zero);
}
else
{
SharedLogger.logger.Trace($"WinLibrary/SetActiveConfig: Actually going to try to set the GDI Device Mode for display {displayDeviceKey} now (secondary display).");
result = GDIImport.ChangeDisplaySettingsEx(currentDeviceSetting.Device.DeviceName, ref currentDeviceSetting.DeviceMode, IntPtr.Zero, (CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_UPDATEREGISTRY | CHANGE_DISPLAY_SETTINGS_FLAGS.CDS_NORESET), IntPtr.Zero);

}
if (result == CHANGE_DISPLAY_RESULTS.Successful)
{
SharedLogger.logger.Trace($"WinLibrary/SetActiveConfig: Successfully changed display {displayDeviceKey} to use the new mode!");
Expand Down

0 comments on commit 55b3529

Please sign in to comment.