Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Runtime.InteropServices;
- using static PInvoke.User32;
- namespace MyCalendral.Monitors
- {
- public class MonitorHelper {
- #region consts
- const UInt32 SDC_TOPOLOGY_INTERNAL = 0x00000001;
- const UInt32 SDC_TOPOLOGY_CLONE = 0x00000002;
- const UInt32 SDC_TOPOLOGY_EXTEND = 0x00000004;
- const UInt32 SDC_TOPOLOGY_EXTERNAL = 0x00000008;
- const UInt32 SDC_USE_SUPPLIED_DISPLAY_CONFIG = 0x00000020;
- const UInt32 SDC_VALIDATE = 0x00000040;
- const UInt32 SDC_APPLY = 0x00000080;
- const UInt32 SDC_NO_OPTIMIZATION = 0x00000100;
- const UInt32 SDC_SAVE_TO_DATABASE = 0x00000200;
- const UInt32 SDC_ALLOW_CHANGES = 0x00000400;
- const UInt32 SDC_PATH_PERSIST_IF_REQUIRED = 0x00000800;
- const UInt32 SDC_FORCE_MODE_ENUMERATION = 0x00001000;
- const UInt32 SDC_ALLOW_PATH_ORDER_CHANGES = 0x00002000;
- const UInt32 QDC_ALL_PATHS = 0x00000001;
- const UInt32 QDC_ONLY_ACTIVE_PATHS = 0x00000002;
- const UInt32 QDC_DATABASE_CURRENT = 0x00000004;
- #endregion
- [Serializable]
- public class TVSettings {
- public DISPLAYCONFIG_PATH_INFO[] Path;
- public DISPLAYCONFIG_MODE_INFO[] Mode;
- public TVSettings(DISPLAYCONFIG_PATH_INFO[] pathArray, DISPLAYCONFIG_MODE_INFO[] modeArray) {
- Path = pathArray;
- Mode = modeArray;
- }
- }
- // The preset of the settings which I serialize.
- public static TVSettings Enabled;
- public static TVSettings Disabled;
- static DISPLAYCONFIG_PATH_INFO[] pathArray;
- static DISPLAYCONFIG_MODE_INFO[] modeArray;
- public static LUID CurrentAdapterID_GPU;
- public static LUID CurrentAdapterID_MB;
- [DllImport("User32.dll")]
- public static extern int GetDisplayConfigBufferSizes(uint flags, ref uint numPathArrayElements, ref uint numModeInfoArrayElements);
- [DllImport("User32.dll")]
- public static extern int QueryDisplayConfig(
- uint flags,
- ref uint numPathArray, [Out] DISPLAYCONFIG_PATH_INFO[] pathArray,
- ref uint numModeArray, [Out] DISPLAYCONFIG_MODE_INFO[] modeArray,
- IntPtr currentTopologyId
- );
- [DllImport("User32.dll")]
- public static extern int SetDisplayConfig(
- uint numPathArray, [In] DISPLAYCONFIG_PATH_INFO[] pathArray,
- uint numModeArray, [In] DISPLAYCONFIG_MODE_INFO[] modeArray,
- uint flags
- );
- public static void ChangeTVState(bool ChangeState = false, bool Save = false) {
- uint numPathArrayElements = 0;
- uint numModeInfoArrayElements = 0;
- uint id = QDC_ALL_PATHS; // Searching for ALL PATHS because I want the disabled screen inside the array after the Query.
- // Initialize and Query all the Display Config info.
- int bufferError = GetDisplayConfigBufferSizes(id, ref numPathArrayElements, ref numModeInfoArrayElements);
- pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
- modeArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];
- QueryDisplayConfig(id, ref numPathArrayElements, pathArray, ref numModeInfoArrayElements, modeArray, IntPtr.Zero);
- // Grab the active Screens -- was previously used for tests.
- var active_modeArray = modeArray.Where(x => x.targetMode.targetVideoSignalInfo.activeSize.cx != 0).ToArray();
- var active_pathArray = pathArray.Where(x => x.flags != 0).ToArray();
- bool ThirdScreenIsConnected = active_pathArray.Length >= 3 && active_modeArray.Length >= 3;
- if (Save) {
- // Save on the appropriate Preset field.
- if (ThirdScreenIsConnected) { Enabled = new TVSettings(pathArray, modeArray); }
- else { Disabled = new TVSettings(pathArray, modeArray); }
- }
- if (ChangeState) {
- // Safety measures because I don't wanna mess up the settings too much.
- if (Enabled == null || Disabled == null) {
- Console.WriteLine("Enabled & Disabled Settings are not configured properly.");
- Console.WriteLine("Please save both and try again.");
- return;
- }
- // Update Current Adapter ID, and all saved settings.
- CurrentAdapterID_GPU = active_pathArray[0].sourceInfo.adapterId;
- CurrentAdapterID_MB = pathArray.First(x => x.sourceInfo.adapterId.LowPart != CurrentAdapterID_GPU.LowPart && x.sourceInfo.adapterId.LowPart != 0).sourceInfo.adapterId;
- UpdateAdapterID();
- // Use the settings of the other state, which I've saved beforehand.
- // eg: if 3rd monitor is currently disabled, we use the Disabled preset.
- var Settings = ThirdScreenIsConnected ? Disabled : Enabled;
- pathArray = Settings.Path;
- modeArray = Settings.Mode;
- // Call SetDisplayConfig to update the display config.
- uint flag = (SDC_APPLY | SDC_SAVE_TO_DATABASE | SDC_ALLOW_CHANGES | SDC_USE_SUPPLIED_DISPLAY_CONFIG);
- int errorID = SetDisplayConfig((uint)pathArray.Length, pathArray, (uint)modeArray.Length, modeArray, flag);
- if (errorID == 0) { Console.WriteLine("Successfully updated Screen setup!"); }
- else { Console.WriteLine("ERROR: " + errorID); }
- }
- }
- static void UpdateAdapterID() {
- // Cache saved adapterIDs, for later comparison with the current ones.
- LUID savedAdapterID_GPU = Enabled.Path[0].sourceInfo.adapterId;
- LUID savedAdapterID_MB = Enabled.Path.First(x => x.sourceInfo.adapterId.LowPart != savedAdapterID_GPU.LowPart && x.sourceInfo.adapterId.LowPart != 0).sourceInfo.adapterId;
- bool isAdapterUpdated_GPU = savedAdapterID_GPU.LowPart == CurrentAdapterID_GPU.LowPart;
- bool isAdapterUpdated_MB = savedAdapterID_MB.LowPart == CurrentAdapterID_MB.LowPart;
- // Check if our saved states have already been updated.
- if (isAdapterUpdated_GPU && isAdapterUpdated_MB) { return; }
- for (int i = 0; i < Enabled.Path.Length; i++) {
- Update(ref Enabled.Path[i].sourceInfo.adapterId);
- Update(ref Enabled.Path[i].targetInfo.adapterId);
- }
- for (int i = 0; i < Disabled.Path.Length; i++) {
- Update(ref Disabled.Path[i].sourceInfo.adapterId);
- Update(ref Disabled.Path[i].targetInfo.adapterId);
- }
- for (int i = 0; i < Enabled.Mode.Length; i++) { Update(ref Enabled.Mode[i].adapterId); }
- for (int i = 0; i < Disabled.Mode.Length; i++) { Update(ref Disabled.Mode[i].adapterId); }
- void Update(ref LUID adapterID) {
- bool isInvalid = adapterID.LowPart == 0;
- bool isUpdated = adapterID.LowPart == CurrentAdapterID_GPU.LowPart || adapterID.LowPart == CurrentAdapterID_MB.LowPart;
- if (!isInvalid && !isUpdated) {
- bool adapterIsGPU = adapterID.LowPart == savedAdapterID_GPU.LowPart;
- if (adapterIsGPU) { adapterID = CurrentAdapterID_GPU; }
- else { adapterID = CurrentAdapterID_MB; }
- }
- }
- if (!isAdapterUpdated_GPU) { Console.WriteLine("Updated adapterID for GPU."); }
- if (!isAdapterUpdated_MB) { Console.WriteLine("Updated adapterID for MB."); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement