Skip to content

Commit

Permalink
Merge pull request #40 from emoacht/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
emoacht authored Mar 8, 2019
2 parents ae3c397 + 76eb483 commit 6916896
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 139 deletions.
10 changes: 5 additions & 5 deletions Source/IconImage/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>
3 changes: 2 additions & 1 deletion Source/IconImage/IconImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>IconImage</RootNamespace>
<AssemblyName>IconImage</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
Expand Down
112 changes: 52 additions & 60 deletions Source/IconImage/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 15 additions & 19 deletions Source/IconImage/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Source/Installer/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="Monitorian" Manufacturer="emoacht" Version="1.11.0"
<Product Id="*" Name="Monitorian" Manufacturer="emoacht" Version="1.12.0"
Language="1033" Codepage="1252" UpgradeCode="{81A4D148-75D3-462E-938D-8C208FB48E3C}">
<Package Id="*" InstallerVersion="500" Compressed="yes"
InstallScope="perMachine" InstallPrivileges="elevated"
Expand Down
3 changes: 2 additions & 1 deletion Source/Monitorian.Supplement/Monitorian.Supplement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Monitorian.Supplement</RootNamespace>
<AssemblyName>Monitorian.Supplement</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
<TargetPlatformVersion>10.0.17134.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
<FileAlignment>512</FileAlignment>
Expand Down
5 changes: 2 additions & 3 deletions Source/Monitorian.Test/Monitorian.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Monitorian.Test</RootNamespace>
<AssemblyName>Monitorian.Test</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
10 changes: 5 additions & 5 deletions Source/Monitorian/App.config
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true" />
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
<appSettings>
<add key="StartupTaskId" value="MonitorianStartupTask" />
<add key="StartupTaskId" value="MonitorianStartupTask"/>
</appSettings>
</configuration>
</configuration>
20 changes: 17 additions & 3 deletions Source/Monitorian/Models/Monitor/DdcMonitorItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ public DdcMonitorItem(

private readonly object _lock = new object();

private uint _minimum = 0; // Raw minimum brightness (not always 0)
private uint _maximum = 100; // Raw maximum brightness (not always 100)

public override bool UpdateBrightness(int brightness = -1)
{
lock (_lock)
{
this.Brightness = MonitorConfiguration.GetBrightness(_handle, _useLowLevel);
return (0 <= this.Brightness);
var (success, minimum, current, maximum) = MonitorConfiguration.GetBrightness(_handle, _useLowLevel);

if (!success || !(minimum < maximum) || !(minimum <= current) || !(current <= maximum))
{
this.Brightness = -1;
return false;
}
this.Brightness = (int)Math.Round((double)(current - minimum) / (maximum - minimum) * 100D, MidpointRounding.AwayFromZero);
this._minimum = minimum;
this._maximum = maximum;
return true;
}
}

Expand All @@ -50,7 +62,9 @@ public override bool SetBrightness(int brightness)

lock (_lock)
{
if (MonitorConfiguration.SetBrightness(_handle, brightness, _useLowLevel))
var buffer = (uint)Math.Round(brightness / 100D * (_maximum - _minimum) + _minimum, MidpointRounding.AwayFromZero);

if (MonitorConfiguration.SetBrightness(_handle, buffer, _useLowLevel))
{
this.Brightness = brightness;
return true;
Expand Down
58 changes: 43 additions & 15 deletions Source/Monitorian/Models/Monitor/MonitorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,32 @@ IEnumerable<string> EnumerateCodes()
}
}

public static int GetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, bool useLowLevel = false)
/// <summary>
/// Gets raw brightnesses not represented in percentage.
/// </summary>
/// <param name="physicalMonitorHandle">Physical monitor handle</param>
/// <param name="useLowLevel">Whether to use low level function</param>
/// <returns>
/// <para>success: True if successfully got</para>
/// <para>minimum: Raw minimum brightness (not always 0)</para>
/// <para>current: Raw current brightness (not always 0 to 100)</para>
/// <para>maximum: Raw maximum brightness (not always 100)</para>
/// </returns>
/// <remarks>
/// Raw minimum and maximum brightnesses will become meaningful when they are not standard
/// values (0 and 100) and so raw current brightness needs to be converted to brightness
/// in percentage using those values. They are used to convert brightness in percentage
/// back to raw brightness when settings brightness as well.
/// </remarks>
public static (bool success, uint minimum, uint current, uint maximum) GetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, bool useLowLevel = false)
{
if (physicalMonitorHandle is null)
throw new ArgumentNullException(nameof(physicalMonitorHandle));

if (physicalMonitorHandle.IsClosed)
{
Debug.WriteLine("Failed to get brightness. The physical monitor handle has been closed.");
return -1;
Debug.WriteLine("Failed to get brightnesses. The physical monitor handle has been closed.");
return (success: false, 0, 0, 0);
}

if (!useLowLevel)
Expand All @@ -348,33 +365,44 @@ public static int GetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle,
out uint currentBrightness,
out uint maximumBrightness))
{
Debug.WriteLine($"Failed to get brightness. {Error.CreateMessage()}");
return -1;
Debug.WriteLine($"Failed to get brightnesses. {Error.CreateMessage()}");
return (success: false, 0, 0, 0);
}
return (int)currentBrightness;
return (success: true,
minimum: minimumBrightness,
current: currentBrightness,
maximum: maximumBrightness);
}
else
{
if (!GetVCPFeatureAndVCPFeatureReply(
physicalMonitorHandle,
LuminanceCode,
out LPMC_VCP_CODE_TYPE _,
out _,
out uint currentValue,
out uint maximumValue))
{
Debug.WriteLine($"Failed to get brightness (Low level). {Error.CreateMessage()}");
return -1;
Debug.WriteLine($"Failed to get brightnesses (Low level). {Error.CreateMessage()}");
return (success: false, 0, 0, 0);
}
return (int)currentValue;
return (success: true,
minimum: 0,
current: currentValue,
maximum: maximumValue);
}
}

public static bool SetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, int brightness, bool useLowLevel = false)
/// <summary>
/// Sets raw brightness not represented in percentage.
/// </summary>
/// <param name="physicalMonitorHandle">Physical monitor handle</param>
/// <param name="brightness">Raw brightness (not always 0 to 100)</param>
/// <param name="useLowLevel">Whether to use low level function</param>
/// <returns>True if successfully set</returns>
public static bool SetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle, uint brightness, bool useLowLevel = false)
{
if (physicalMonitorHandle is null)
throw new ArgumentNullException(nameof(physicalMonitorHandle));
if ((brightness < 0) || (100 < brightness))
throw new ArgumentOutOfRangeException(nameof(brightness), $"{nameof(brightness)} must be in the range of 0 to 100.");

if (physicalMonitorHandle.IsClosed)
{
Expand All @@ -386,7 +414,7 @@ public static bool SetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle
{
if (!SetMonitorBrightness(
physicalMonitorHandle,
(uint)brightness))
brightness))
{
Debug.WriteLine($"Failed to set brightness. {Error.CreateMessage()}");
return false;
Expand All @@ -397,7 +425,7 @@ public static bool SetBrightness(SafePhysicalMonitorHandle physicalMonitorHandle
if (!SetVCPFeature(
physicalMonitorHandle,
LuminanceCode,
(uint)brightness))
brightness))
{
Debug.WriteLine($"Failed to set brightness (Low level). {Error.CreateMessage()}");
return false;
Expand Down
3 changes: 2 additions & 1 deletion Source/Monitorian/Monitorian.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Monitorian</RootNamespace>
<AssemblyName>Monitorian</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
Expand Down
Loading

0 comments on commit 6916896

Please sign in to comment.