Skip to content

Commit 9431366

Browse files
committed
1.7.17
[Changes] - More monitor information [Bug fixes} - Application crashing? #61 - Closing applications didn't worked properly
1 parent a63c0b7 commit 9431366

18 files changed

+437
-131
lines changed

Source/HDRController/HDRController/HDRController.cpp

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,47 +202,77 @@ static void SetHDR(UINT32 uid, bool enabled)
202202

203203
static SIZE _GetResolution(UINT32 uid)
204204
{
205+
206+
207+
uint8_t set[] = { 0x0A, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x14, 0x81, 0x00, 0x00,
208+
0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };
209+
210+
uint8_t request[] = { 0x09, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x7C, 0x6F, 0x00,
211+
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0xDB, 0x00,
212+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00 };
213+
205214
SIZE resolution = SIZE();
215+
bool returnValue = false;
216+
206217
uint32_t pathCount, modeCount;
207-
UINT32 numPathArrayElements = 0, numModeInfoArrayElements = 0;
208-
UINT32 filter = QDC_ALL_PATHS;
218+
209219
if (ERROR_SUCCESS == GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount))
210220
{
221+
DISPLAYCONFIG_PATH_INFO* pathsArray = nullptr;
222+
DISPLAYCONFIG_MODE_INFO* modesArray = nullptr;
211223

212224
const size_t sizePathsArray = pathCount * sizeof(DISPLAYCONFIG_PATH_INFO);
213225
const size_t sizeModesArray = modeCount * sizeof(DISPLAYCONFIG_MODE_INFO);
214226

227+
pathsArray = static_cast<DISPLAYCONFIG_PATH_INFO*>(std::malloc(sizePathsArray));
228+
modesArray = static_cast<DISPLAYCONFIG_MODE_INFO*>(std::malloc(sizeModesArray));
215229

216-
DISPLAYCONFIG_PATH_INFO* pathsArray = new DISPLAYCONFIG_PATH_INFO[pathCount];
217-
DISPLAYCONFIG_MODE_INFO* modesArray = new DISPLAYCONFIG_MODE_INFO[modeCount];
218-
230+
if (pathsArray != nullptr && modesArray != nullptr)
231+
{
232+
std::memset(pathsArray, 0, sizePathsArray);
233+
std::memset(modesArray, 0, sizeModesArray);
219234

220-
ZeroMemory(pathsArray, sizeof(DISPLAYCONFIG_PATH_INFO) * pathCount);
221-
ZeroMemory(modesArray, sizeof(DISPLAYCONFIG_MODE_INFO) * modeCount);
222-
QueryDisplayConfig(filter, &pathCount, pathsArray, &modeCount, modesArray, NULL);
235+
if (ERROR_SUCCESS == QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, pathsArray,
236+
&modeCount, modesArray, 0))
237+
{
238+
DISPLAYCONFIG_DEVICE_INFO_HEADER* setPacket =
239+
reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(set);
240+
DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket =
241+
reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(request);
223242

243+
//HDR is off
244+
returnValue = false;
245+
for (int i = 0; i < modeCount; i++)
246+
{
247+
try
248+
{
249+
if (modesArray[i].id == uid)
250+
{
251+
setPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart;
252+
setPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart;
253+
setPacket->id = modesArray[i].id;
224254

225-
for (short i = 0; i < pathCount; i++)
226-
{
227-
try
228-
{
229-
int ix = pathsArray[i].sourceInfo.modeInfoIdx; //assuming path[0] is primary
255+
requestPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart;
256+
requestPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart;
257+
requestPacket->id = modesArray[i].id;
258+
DISPLAYCONFIG_MODE_INFO mode = modesArray[i];
259+
resolution.cx = mode.sourceMode.width;
260+
resolution.cy = mode.sourceMode.height;
261+
}
262+
}
263+
catch (const std::exception&)
264+
{
230265

231-
if (modesArray[ix].id != uid)
266+
}
232267

233-
{
234-
resolution.cx = modesArray[ix].sourceMode.width;
235-
resolution.cy = modesArray[ix].sourceMode.height;
236-
SetDisplayConfig(pathCount, pathsArray, modeCount, modesArray, SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_ALLOW_CHANGES | SDC_SAVE_TO_DATABASE);
237268
}
238269
}
239-
catch (const std::exception&)
240-
{
241-
242-
}
270+
std::free(pathsArray);
271+
std::free(modesArray);
272+
return resolution;
243273
}
244274
}
245-
return resolution;
275+
246276
}
247277

248278
static bool HDRIsOn(UINT32 uid)

Source/HDRProfile/AutoHDR.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,14 @@
175175
</Reference>
176176
<Reference Include="Windows">
177177
<HintPath>Externals\Windows.winmd</HintPath>
178+
<Private>False</Private>
178179
</Reference>
179180
<Reference Include="WindowsBase" />
180181
<Reference Include="PresentationFramework" />
181182
</ItemGroup>
182183
<ItemGroup>
184+
<Compile Include="DispatchingObservableCollection.cs" />
185+
<Compile Include="Displays\DisplayInformation.cs" />
183186
<Compile Include="Profiles\Actions\CloseProgramAction.cs" />
184187
<Compile Include="Theming\ThemeResourceDirectory.cs" />
185188
<Compile Include="Theming\Theme.cs" />
-1.5 KB
Binary file not shown.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Collections.Specialized;
5+
using System.ComponentModel;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Windows.Threading;
10+
11+
namespace AutoHDR
12+
{
13+
/// <summary>
14+
/// This class is an observablecollection which invokes automatically.
15+
/// This means that any change will be done in the right thread.
16+
/// </summary>
17+
/// <typeparam name="T">The type of the elements</typeparam>
18+
public class DispatchingObservableCollection<T> : ObservableCollection<T>
19+
{
20+
/// <summary>
21+
/// The default constructor of the ObservableCollection
22+
/// </summary>
23+
public DispatchingObservableCollection()
24+
{
25+
//Assign the current Dispatcher (owner of the collection)
26+
_currentDispatcher = Dispatcher.CurrentDispatcher;
27+
}
28+
29+
private readonly Dispatcher _currentDispatcher;
30+
31+
/// <summary>
32+
/// Executes this action in the right thread
33+
/// </summary>
34+
///<param name="action">The action which should be executed</param>
35+
private void DoDispatchedAction(Action action)
36+
{
37+
if (_currentDispatcher.CheckAccess())
38+
action.Invoke();
39+
else
40+
_currentDispatcher.Invoke(DispatcherPriority.DataBind, action);
41+
}
42+
43+
/// <summary>
44+
/// Clears all items
45+
/// </summary>
46+
protected override void ClearItems()
47+
{
48+
DoDispatchedAction(BaseClearItems);
49+
}
50+
51+
private void BaseClearItems()
52+
{
53+
base.ClearItems();
54+
}
55+
56+
/// <summary>
57+
/// Inserts a item at the specified index
58+
/// </summary>
59+
///<param name="index">The index where the item should be inserted</param>
60+
///<param name="item">The item which should be inserted</param>
61+
protected override void InsertItem(int index, T item)
62+
{
63+
DoDispatchedAction(() => BaseInsertItem(index, item));
64+
}
65+
66+
private void BaseInsertItem(int index, T item)
67+
{
68+
base.InsertItem(index, item);
69+
}
70+
71+
/// <summary>
72+
/// Moves an item from one index to another
73+
/// </summary>
74+
///<param name="oldIndex">The index of the item which should be moved</param>
75+
///<param name="newIndex">The index where the item should be moved</param>
76+
protected override void MoveItem(int oldIndex, int newIndex)
77+
{
78+
DoDispatchedAction(() => BaseMoveItem(oldIndex, newIndex));
79+
}
80+
81+
private void BaseMoveItem(int oldIndex, int newIndex)
82+
{
83+
base.MoveItem(oldIndex, newIndex);
84+
}
85+
86+
/// <summary>
87+
/// Removes the item at the specified index
88+
/// </summary>
89+
///<param name="index">The index of the item which should be removed</param>
90+
protected override void RemoveItem(int index)
91+
{
92+
DoDispatchedAction(() => BaseRemoveItem(index));
93+
}
94+
95+
private void BaseRemoveItem(int index)
96+
{
97+
base.RemoveItem(index);
98+
}
99+
100+
/// <summary>
101+
/// Sets the item at the specified index
102+
/// </summary>
103+
///<param name="index">The index which should be set</param>
104+
///<param name="item">The new item</param>
105+
protected override void SetItem(int index, T item)
106+
{
107+
DoDispatchedAction(() => BaseSetItem(index, item));
108+
}
109+
110+
private void BaseSetItem(int index, T item)
111+
{
112+
base.SetItem(index, item);
113+
}
114+
115+
/// <summary>
116+
/// Fires the CollectionChanged Event
117+
/// </summary>
118+
///<param name="e">The additional arguments of the event</param>
119+
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
120+
{
121+
DoDispatchedAction(() => BaseOnCollectionChanged(e));
122+
}
123+
124+
private void BaseOnCollectionChanged(NotifyCollectionChangedEventArgs e)
125+
{
126+
base.OnCollectionChanged(e);
127+
}
128+
129+
/// <summary>
130+
/// Fires the PropertyChanged Event
131+
/// </summary>
132+
///<param name="e">The additional arguments of the event</param>
133+
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
134+
{
135+
DoDispatchedAction(() => BaseOnPropertyChanged(e));
136+
}
137+
138+
private void BaseOnPropertyChanged(PropertyChangedEventArgs e)
139+
{
140+
base.OnPropertyChanged(e);
141+
}
142+
}
143+
}

Source/HDRProfile/Displays/Display.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,30 @@ namespace AutoHDR.Displays
1414
[JsonObject(MemberSerialization.OptIn)]
1515
public class Display : BaseViewModel
1616
{
17-
public static readonly Display AllDisplays = new Display(ProjectResources.Locale_Texts.AllDisplays, UInt32.MaxValue, UInt32.MaxValue, new Size(0, 0),0,0);
17+
public static readonly Display AllDisplays = new Display(ProjectResources.Locale_Texts.AllDisplays, UInt32.MaxValue);
1818
private bool _managed = true;
1919

2020
[JsonProperty]
2121
public bool Managed { get => _managed; set { _managed = value; OnPropertyChanged(); } }
2222

2323

24+
private bool _isPrimary;
25+
26+
[JsonProperty]
27+
public bool IsPrimary { get => _isPrimary; set { _isPrimary = value; OnPropertyChanged(); } }
28+
2429
private string _name;
2530

2631
[JsonProperty]
2732
public string Name { get => _name; set { _name = value; OnPropertyChanged(); } }
2833

34+
private string _graphicsCard;
35+
36+
public string GraphicsCard { get => _graphicsCard; set { _graphicsCard = value; OnPropertyChanged(); } }
37+
38+
private string _deviceKey;
39+
40+
public string DeviceKey { get => _deviceKey; set { _deviceKey = value; OnPropertyChanged(); } }
2941

3042
private UInt32 _uid;
3143

@@ -35,7 +47,7 @@ public class Display : BaseViewModel
3547
private uint _id;
3648

3749
[JsonProperty]
38-
public uint ID { get => _id; private set { _id = value; OnPropertyChanged(); } }
50+
public uint ID { get => _id; set { _id = value; OnPropertyChanged(); } }
3951

4052
private bool _hdrState;
4153

@@ -61,14 +73,22 @@ private Display()
6173

6274
}
6375

64-
public Display(string name, uint uID, uint id, Size resolution, int refreshRate, int colorDepth)
76+
public Display(DisplayInformation displayInformation, uint uid)
77+
{
78+
Name = displayInformation.DisplayDevice.DeviceName;
79+
UID = uid;
80+
ID = displayInformation.Id;
81+
IsPrimary = displayInformation.IsPrimary;
82+
DeviceKey = displayInformation.DisplayDevice.DeviceKey;
83+
Resolution = new Size(displayInformation.Devmode.dmPelsWidth, displayInformation.Devmode.dmPelsHeight);
84+
RefreshRate = displayInformation.Devmode.dmDisplayFrequency;
85+
GraphicsCard = displayInformation.DisplayDevice.DeviceString;
86+
}
87+
88+
public Display(string name, uint uid)
6589
{
66-
Name = name ?? throw new ArgumentNullException(nameof(name));
67-
UID = uID;
68-
ID = id;
69-
Resolution = resolution;
70-
RefreshRate = refreshRate;
71-
ColorDepth = colorDepth;
90+
Name = name;
91+
UID = uid;
7292
}
7393

7494
public void UpdateHDRState()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AutoHDR.Displays
8+
{
9+
public class DisplayInformation
10+
{
11+
public uint Id { get; private set; }
12+
public DISPLAY_DEVICE DisplayDevice { get; private set; }
13+
public DEVMODE Devmode { get; private set; }
14+
public bool IsPrimary => DisplayDevice.StateFlags.HasFlag(DisplayDeviceStateFlags.PrimaryDevice);
15+
16+
public DisplayInformation(uint id, DISPLAY_DEVICE displayDevice, DEVMODE devmode) : this(id, displayDevice)
17+
{
18+
Devmode = devmode;
19+
}
20+
21+
public DisplayInformation(uint id, DISPLAY_DEVICE displayDevice)
22+
{
23+
Id = id;
24+
DisplayDevice = displayDevice;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)