Skip to content

Commit

Permalink
Skip unreadable features without throwing exception
Browse files Browse the repository at this point in the history
  • Loading branch information
7orlum committed Apr 15, 2020
1 parent 27ff5fd commit 5a9570d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/Viewsonic-VP2785-4K-MCCS/Capabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Capabilities
{
public class VCPCode
{
public bool Error { get; set; }
public NativeMethods.MC_VCP_CODE_TYPE Type { get; set; }
public string Access { get; set; }
public uint CurrentValue { get; set; }
Expand All @@ -20,8 +21,8 @@ public class VCPCode

public string ProtocolClass { get; private set; }
public string DysplayType { get; private set; }
public List<byte> Commands { get; private set; }
public Dictionary<byte, VCPCode> VCPCodes { get; private set; }
public List<byte> Commands { get; private set; } = new List<byte>();
public Dictionary<byte, VCPCode> VCPCodes { get; private set; } = new Dictionary<byte, VCPCode>();
public string DysplayModel { get; private set; }
public string SupportedMCCSVersion { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Viewsonic_VP2785_4K_MCCS
{
public class FeatureEnumeration<T>: Feature where T : Enum
public class Feature<T>: Feature where T : Enum
{
public FeatureEnumeration(string name, byte code, float delaySeconds = 0) : base(name, code, delaySeconds)
public Feature(string name, byte code, float delaySeconds = 0) : base(name, code, delaySeconds)
{
Description = $"{Name}: {string.Join(", ", Enum.GetNames(typeof(T)))}";
}
Expand Down
24 changes: 14 additions & 10 deletions src/Viewsonic-VP2785-4K-MCCS/Monitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public static List<Monitor> GetMonitors()

uint physicalMonitorCount;
if (!NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(monitorHandler, out physicalMonitorCount))
throw new InvalidOperationException($"{nameof(NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR)} returned error {Marshal.GetLastWin32Error()}");
throw new InvalidOperationException($"{nameof(NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR)} returned error 0x{Marshal.GetLastWin32Error():X8}");

var physicalMonitors = new NativeMethods.PHYSICAL_MONITOR[physicalMonitorCount];
if (!NativeMethods.GetPhysicalMonitorsFromHMONITOR(monitorHandler, physicalMonitorCount, physicalMonitors))
throw new InvalidOperationException($"{nameof(NativeMethods.GetPhysicalMonitorsFromHMONITOR)} returned error {Marshal.GetLastWin32Error()}");
throw new InvalidOperationException($"{nameof(NativeMethods.GetPhysicalMonitorsFromHMONITOR)} returned error 0x{Marshal.GetLastWin32Error():X8}");

foreach (var physicalMonitor in physicalMonitors)
result.Add(new Monitor
Expand All @@ -48,11 +48,11 @@ public static List<Monitor> GetMonitorsAndFeatures()
{
uint length;
if (!NativeMethods.GetCapabilitiesStringLength(monitor.Handle, out length))
throw new InvalidOperationException($"{nameof(NativeMethods.GetCapabilitiesStringLength)} returned error {Marshal.GetLastWin32Error()}");
throw new InvalidOperationException($"{nameof(NativeMethods.GetCapabilitiesStringLength)} returned error 0x{Marshal.GetLastWin32Error():X8}");

var capabilitiesString = new StringBuilder((int)length);
if (!NativeMethods.CapabilitiesRequestAndCapabilitiesReply(monitor.Handle, capabilitiesString, length))
throw new InvalidOperationException($"{nameof(NativeMethods.CapabilitiesRequestAndCapabilitiesReply)} returned error {Marshal.GetLastWin32Error()}");
throw new InvalidOperationException($"{nameof(NativeMethods.CapabilitiesRequestAndCapabilitiesReply)} returned error 0x{Marshal.GetLastWin32Error():X8}");

monitor.Capabilities = Capabilities.Parse(capabilitiesString.ToString());
GetVCPFeatures(monitor.Handle, monitor.Capabilities);
Expand All @@ -69,12 +69,16 @@ private static void GetVCPFeatures(SafePhysicalMonitorHandle handle, Capabilitie
{
foreach (var vcpCode in capabilities.VCPCodes)
{
if (!NativeMethods.GetVCPFeatureAndVCPFeatureReply(handle, vcpCode.Key, out var type, out var value, out var maxValue))
throw new InvalidOperationException($"{nameof(NativeMethods.GetVCPFeatureAndVCPFeatureReply)} returned error {Marshal.GetLastWin32Error()}");

vcpCode.Value.Type = type;
vcpCode.Value.MaximumValue = maxValue;
vcpCode.Value.CurrentValue = value;
if (NativeMethods.GetVCPFeatureAndVCPFeatureReply(handle, vcpCode.Key, out var type, out var value, out var maxValue))
{
vcpCode.Value.Type = type;
vcpCode.Value.MaximumValue = maxValue;
vcpCode.Value.CurrentValue = value;
}
else
{
vcpCode.Value.Error = true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@

namespace Viewsonic_VP2785_4K_MCCS
{
class Features
class MonitorFeatures
{
private static List<Feature> features = new List<Feature>
{
new FeatureRange("Brightness", 0x10, 0, 100, description: "Brightness: value beetween 0 and 100 where 0 is the minimal brightness, 100 is the maximal brightness"),
new FeatureEnumeration<StandardColor>("StandardColor", 0x14),
new FeatureEnumeration<AudioInput>("AudioInput", 0x1D, delaySeconds: 4),
new FeatureEnumeration<LowInputLag>("LowInputLag", 0x23),
new FeatureEnumeration<ResponceTime>("ResponceTime", 0x25),
new FeatureEnumeration<VideoInputAutodetect>("VideoInputAutodetect", 0x33, delaySeconds: 4),
new FeatureEnumeration<VideoInput>("VideoInput", 0x60, delaySeconds: 4),
new Feature<StandardColor>("StandardColor", 0x14),
new Feature<AudioInput>("AudioInput", 0x1D, delaySeconds: 4),
new Feature<LowInputLag>("LowInputLag", 0x23),
new Feature<ResponceTime>("ResponceTime", 0x25),
new Feature<VideoInputAutodetect>("VideoInputAutodetect", 0x33, delaySeconds: 4),
new Feature<VideoInput>("VideoInput", 0x60, delaySeconds: 4),
new FeatureRange("Volume", 0x62, 0, 100, description: "Volume: value beetween 0 and 100 where 0 is the minimal volume, 100 is the maximal volume"),
new FeatureEnumeration<AmbientLightSensor>("AmbientLightSensor", 0x66),
new FeatureEnumeration<PresenceSensor>("PresenceSensor", 0x67),
new FeatureEnumeration<AudioMute>("AudioMute", 0x8D),
new Feature<AmbientLightSensor>("AmbientLightSensor", 0x66),
new Feature<PresenceSensor>("PresenceSensor", 0x67),
new Feature<AudioMute>("AudioMute", 0x8D),
new FeaturePIPPosition("PIPPosition", 0x96),
new FeatureRange("PIPSize", 0x97, 0, 10, description: "PIPSize: value beetween 0 and 10 where 0 is the minimal size, 10 is the maximal size"),
new FeatureEnumeration<DisplayApplication>("DisplayApplication", 0xDC),
new FeatureEnumeration<MultiPicture>("MultiPicture", 0xE8, delaySeconds: 4),
new FeatureEnumeration<Uniformity>("Uniformity", 0xE9),
new Feature<DisplayApplication>("DisplayApplication", 0xDC),
new Feature<MultiPicture>("MultiPicture", 0xE8, delaySeconds: 4),
new Feature<Uniformity>("Uniformity", 0xE9),
};


Expand Down
6 changes: 3 additions & 3 deletions src/Viewsonic-VP2785-4K-MCCS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path sends to the monitor commands from the specified YAML configuration file
The full list of commands is given below, copy the desired commands to your configuration file:
{Features.YamlConfigTemplate()}";
{MonitorFeatures.YamlConfigTemplate()}";


private static void Main(string[] args)
Expand Down Expand Up @@ -84,10 +84,10 @@ private static Dictionary<Feature, uint> ReadYamlConfig(string path)
var result = new Dictionary<Feature, uint>();

foreach (var entry in ((YamlMappingNode)yaml.Documents[0].RootNode).Children)
if (Features.TryParse(entry.Key, entry.Value, out var feature, out var value))
if (MonitorFeatures.TryParse(entry.Key, entry.Value, out var feature, out var value))
result.Add(feature, value);
else
Console.WriteLine($"Wrong command {entry.Key}, possible commands:\r\n{Features.YamlConfigTemplate()}");
Console.WriteLine($"Wrong command {entry.Key}, possible commands:\r\n{MonitorFeatures.YamlConfigTemplate()}");

return result;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Viewsonic-VP2785-4K-MCCS/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Viewsonic-VP2785-4K-MCCS")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("A utility to control Viewsonic VP2785-4K monitors. See https://github.com/7orlum/Viewsonic-VP2785-4K-MCCS")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Pavel Veretennikov, [email protected]")]
[assembly: AssemblyCompany("Pavel Veretennikov")]
[assembly: AssemblyProduct("Viewsonic-VP2785-4K-MCCS")]
[assembly: AssemblyCopyright("Pavel Veretennikov, 2019")]
[assembly: AssemblyCopyright("© 2020 Pavel Veretennikov [email protected]")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -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.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
4 changes: 2 additions & 2 deletions src/Viewsonic-VP2785-4K-MCCS/Viewsonic-VP2785-4K-MCCS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Feature.cs" />
<Compile Include="Features.cs" />
<Compile Include="MonitorFeatures.cs" />
<Compile Include="FeaturePIPPosition.cs" />
<Compile Include="FeatureEnumeration.cs" />
<Compile Include="FeatureEnum.cs" />
<Compile Include="FeatureRange.cs" />
<Compile Include="FeatureEnumerations.cs" />
<Compile Include="Capabilities.cs" />
Expand Down

0 comments on commit 5a9570d

Please sign in to comment.