Skip to content

Commit 240b33a

Browse files
committed
1.5.5
New: - Monitor view with HDR indicator - Enable or disable Auto HDR for each monitor Fix: - Tray menu buttons are working correctly now
1 parent 365e842 commit 240b33a

20 files changed

+825
-158
lines changed

Source/HDRController/HDRController/HDRController.cpp

Lines changed: 182 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void showError(std::string msg)
2828
}
2929

3030

31-
static void SetHDR(bool enabled)
31+
static void SetGlobalHDR(bool enabled)
3232
{
3333
uint32_t pathCount, modeCount;
3434

@@ -112,6 +112,174 @@ static void SetHDR(bool enabled)
112112
}
113113
}
114114

115+
116+
static void SetHDR(UINT32 uid, bool enabled)
117+
{
118+
uint32_t pathCount, modeCount;
119+
120+
uint8_t set[] = { 0x0A, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x14, 0x81, 0x00, 0x00,
121+
0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };
122+
123+
uint8_t request[] = { 0x09, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x7C, 0x6F, 0x00,
124+
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0xDB, 0x00,
125+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00 };
126+
127+
if (ERROR_SUCCESS == GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount))
128+
{
129+
DISPLAYCONFIG_PATH_INFO* pathsArray = nullptr;
130+
DISPLAYCONFIG_MODE_INFO* modesArray = nullptr;
131+
132+
const size_t sizePathsArray = pathCount * sizeof(DISPLAYCONFIG_PATH_INFO);
133+
const size_t sizeModesArray = modeCount * sizeof(DISPLAYCONFIG_MODE_INFO);
134+
135+
pathsArray = static_cast<DISPLAYCONFIG_PATH_INFO*>(std::malloc(sizePathsArray));
136+
modesArray = static_cast<DISPLAYCONFIG_MODE_INFO*>(std::malloc(sizeModesArray));
137+
138+
139+
if (pathsArray != nullptr && modesArray != nullptr)
140+
{
141+
std::memset(pathsArray, 0, sizePathsArray);
142+
std::memset(modesArray, 0, sizeModesArray);
143+
144+
LONG queryRet = QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, pathsArray,
145+
&modeCount, modesArray, 0);
146+
if (ERROR_SUCCESS == queryRet)
147+
{
148+
DISPLAYCONFIG_DEVICE_INFO_HEADER* setPacket =
149+
reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(set);
150+
DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket =
151+
reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(request);
152+
153+
for (int i = 0; i < modeCount; i++)
154+
{
155+
try
156+
{
157+
if (modesArray[i].id != uid)
158+
continue;
159+
if (modesArray[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
160+
{
161+
setPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart;
162+
setPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart;
163+
setPacket->id = modesArray[i].id;
164+
165+
requestPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart;
166+
requestPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart;
167+
requestPacket->id = modesArray[i].id;
168+
169+
if (ERROR_SUCCESS == DisplayConfigGetDeviceInfo(requestPacket))
170+
{
171+
if (enabled == true)
172+
{
173+
set[20] = 1;
174+
DisplayConfigSetDeviceInfo(setPacket);
175+
}
176+
else if (enabled == false)
177+
{
178+
set[20] = 0;
179+
DisplayConfigSetDeviceInfo(setPacket);
180+
}
181+
}
182+
}
183+
}
184+
catch (const std::exception&)
185+
{
186+
187+
}
188+
}
189+
190+
}
191+
192+
std::free(pathsArray);
193+
std::free(modesArray);
194+
}
195+
else
196+
{
197+
throw std::invalid_argument("No monitor found.");
198+
}
199+
}
200+
}
201+
202+
203+
204+
static bool HDRIsOn(UINT32 uid)
205+
{
206+
bool returnValue = false;
207+
208+
uint32_t pathCount, modeCount;
209+
210+
uint8_t set[] = { 0x0A, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x14, 0x81, 0x00, 0x00,
211+
0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };
212+
213+
uint8_t request[] = { 0x09, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x7C, 0x6F, 0x00,
214+
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0xDB, 0x00,
215+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00 };
216+
217+
if (ERROR_SUCCESS == GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount))
218+
{
219+
DISPLAYCONFIG_PATH_INFO* pathsArray = nullptr;
220+
DISPLAYCONFIG_MODE_INFO* modesArray = nullptr;
221+
222+
const size_t sizePathsArray = pathCount * sizeof(DISPLAYCONFIG_PATH_INFO);
223+
const size_t sizeModesArray = modeCount * sizeof(DISPLAYCONFIG_MODE_INFO);
224+
225+
pathsArray = static_cast<DISPLAYCONFIG_PATH_INFO*>(std::malloc(sizePathsArray));
226+
modesArray = static_cast<DISPLAYCONFIG_MODE_INFO*>(std::malloc(sizeModesArray));
227+
228+
if (pathsArray != nullptr && modesArray != nullptr)
229+
{
230+
std::memset(pathsArray, 0, sizePathsArray);
231+
std::memset(modesArray, 0, sizeModesArray);
232+
233+
if (ERROR_SUCCESS == QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, pathsArray,
234+
&modeCount, modesArray, 0))
235+
{
236+
DISPLAYCONFIG_DEVICE_INFO_HEADER* setPacket =
237+
reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(set);
238+
DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket =
239+
reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(request);
240+
241+
//HDR is off
242+
returnValue = false;
243+
for (int i = 0; i < modeCount; i++)
244+
{
245+
try
246+
{
247+
if (modesArray[i].id != uid)
248+
continue;
249+
if (modesArray[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
250+
{
251+
setPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart;
252+
setPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart;
253+
setPacket->id = modesArray[i].id;
254+
255+
requestPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart;
256+
requestPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart;
257+
requestPacket->id = modesArray[i].id;
258+
}
259+
if (ERROR_SUCCESS == DisplayConfigGetDeviceInfo(requestPacket))
260+
{
261+
if (request[20] == 0xD3) // HDR is ON
262+
{
263+
returnValue = true;
264+
break;
265+
}
266+
}
267+
}
268+
catch (const std::exception&)
269+
{
270+
271+
}
272+
273+
}
274+
}
275+
std::free(pathsArray);
276+
std::free(modesArray);
277+
return returnValue;
278+
}
279+
}
280+
}
281+
282+
115283
static bool HDRIsOn()
116284
{
117285
bool returnValue = false;
@@ -190,13 +358,23 @@ static bool HDRIsOn()
190358

191359
extern "C"
192360
{
193-
__declspec(dllexport) void SetHDRState(bool enabled)
361+
__declspec(dllexport) void SetGlobalHDRState(bool enabled)
194362
{
195-
SetHDR(enabled);
363+
SetGlobalHDR(enabled);
196364
}
197365

198-
__declspec(dllexport) bool GetHDRState()
366+
__declspec(dllexport) bool GetGlobalHDRState()
199367
{
200368
return HDRIsOn();
201369
}
370+
371+
__declspec(dllexport) void SetHDRState(UINT32 uid, bool enabled)
372+
{
373+
SetHDR(uid, enabled);
374+
}
375+
376+
__declspec(dllexport) bool GetHDRState(UINT32 uid)
377+
{
378+
return HDRIsOn(uid);
379+
}
202380
}

Source/HDRProfile/App.xaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,24 @@
3030
<Setter Property="Foreground" Value="Black"/>
3131
</Style>
3232

33-
<Style x:Key="RounedBorder" TargetType="Border">
33+
<Style x:Key="RoundedBorder" TargetType="Border">
3434
<Setter Property="CornerRadius" Value="{StaticResource CornerRadius}"/>
3535
</Style>
3636

37+
<Style x:Key="HDRCircle" TargetType="Ellipse">
38+
<Setter Property="HorizontalAlignment" Value="Center" />
39+
<Setter Property="VerticalAlignment" Value="Center" />
40+
<Style.Triggers>
41+
<Trigger Property="Tag" Value="True">
42+
<Setter Property="Fill" Value="#5d9ac7"/>
43+
</Trigger>
44+
<Trigger Property="Tag" Value="False">
45+
<Setter Property="Fill" Value="Transparent"/>
46+
</Trigger>
47+
</Style.Triggers>
48+
</Style>
49+
50+
3751
<Style x:Key="DefaultButton" TargetType="Button" >
3852
<Setter Property="Background" Value="{StaticResource AccentBrush}"/>
3953
<Setter Property="Foreground" Value="{StaticResource ButtonForegroundBrush}"/>
1 KB
Binary file not shown.
1.5 KB
Binary file not shown.

Source/HDRProfile/HDRController.cs

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,29 @@
1-
using CodectoryCore.UI.Wpf;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
4-
using System.Diagnostics;
53
using System.Linq;
6-
using System.Runtime.InteropServices;
74
using System.Text;
8-
using System.Threading;
95
using System.Threading.Tasks;
106

7+
using System.Runtime.InteropServices;
8+
119
namespace HDRProfile
1210
{
1311
public static class HDRController
1412
{
1513

16-
static Thread _updateThread = null;
17-
static readonly object _threadControlLock = new object();
18-
static bool _monitorCancelRequested = false;
19-
2014
readonly static object _dllLock = new object();
2115

2216
[DllImport("HDRController.dll", CallingConvention = CallingConvention.Cdecl)]
23-
private static extern IntPtr SetHDRState(bool enabled);
17+
public static extern IntPtr SetGlobalHDRState(bool enabled);
2418

2519
[DllImport("HDRController.dll", CallingConvention = CallingConvention.Cdecl)]
26-
private static extern bool GetHDRState();
27-
28-
29-
public static bool HDRIsActive { get; private set; } = false;
30-
public static bool Monitoring { get; private set; } = false;
20+
public static extern bool GetGlobalHDRState();
3121

32-
public static event EventHandler HDRIsActiveChanged;
33-
34-
public static void StartMonitoring()
35-
{
36-
lock (_threadControlLock)
37-
{
38-
if (Monitoring)
39-
return;
40-
_updateThread = new Thread(HDRMonitorLoop);
41-
_updateThread.IsBackground = true;
42-
Monitoring = true;
43-
_monitorCancelRequested = false;
44-
_updateThread.Start();
45-
}
46-
}
47-
48-
public static void StopMonitoring()
49-
{
50-
lock (_threadControlLock)
51-
{
52-
if (!Monitoring)
53-
return;
54-
_monitorCancelRequested = true;
55-
_updateThread.Join();
56-
_updateThread = null;
57-
Monitoring = false;
58-
}
59-
}
60-
61-
private static void HDRMonitorLoop()
62-
{
63-
while (!_monitorCancelRequested)
64-
{
65-
bool currentValue = GetHDRState();
66-
bool changed = HDRIsActive != currentValue;
67-
HDRIsActive = currentValue;
68-
if (changed)
69-
{
70-
try {HDRIsActiveChanged?.Invoke(null, EventArgs.Empty);} catch {}
71-
}
72-
System.Threading.Thread.Sleep(50);
73-
}
74-
}
22+
[DllImport("HDRController.dll", CallingConvention = CallingConvention.Cdecl)]
23+
public static extern IntPtr SetHDRState(UInt32 uid, bool enabled);
7524

76-
public static void ActivateHDR()
77-
{
78-
lock (_dllLock)
79-
SetHDRState(true);
80-
}
25+
[DllImport("HDRController.dll", CallingConvention = CallingConvention.Cdecl)]
26+
public static extern bool GetHDRState(UInt32 uid);
8127

82-
public static void DeactivateHDR()
83-
{
84-
lock (_dllLock)
85-
SetHDRState(false);
86-
}
8728
}
8829
}

Source/HDRProfile/HDRProfile.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
<SubType>Designer</SubType>
154154
</ApplicationDefinition>
155155
<Compile Include="ApplicationState.cs" />
156+
<Compile Include="HDRController.cs" />
157+
<Compile Include="Monitor.cs" />
158+
<Compile Include="MonitorManager.cs" />
159+
<Compile Include="MonitorManagerView.xaml.cs">
160+
<DependentUpon>MonitorManagerView.xaml</DependentUpon>
161+
</Compile>
156162
<Compile Include="Locale_Texts.de.Designer.cs">
157163
<AutoGen>True</AutoGen>
158164
<DesignTime>True</DesignTime>
@@ -165,6 +171,10 @@
165171
</Compile>
166172
<Compile Include="Tools.cs" />
167173
<Compile Include="TrayMenuHelper.cs" />
174+
<Page Include="MonitorManagerView.xaml">
175+
<SubType>Designer</SubType>
176+
<Generator>MSBuild:Compile</Generator>
177+
</Page>
168178
<Page Include="HDRProfileSettingsView.xaml">
169179
<SubType>Designer</SubType>
170180
<Generator>MSBuild:Compile</Generator>
@@ -180,7 +190,6 @@
180190
<Compile Include="EnumLocaleConverter.cs" />
181191
<Compile Include="HDRActivationMode.cs" />
182192
<Compile Include="HDRProfileHandler.cs" />
183-
<Compile Include="HDRController.cs" />
184193
<Compile Include="HDRProfileSettings.cs" />
185194
<Compile Include="HDRProfileSettingsView.xaml.cs">
186195
<DependentUpon>HDRProfileSettingsView.xaml</DependentUpon>

0 commit comments

Comments
 (0)