|
| 1 | +using DDCKVMService; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Text.RegularExpressions; |
| 9 | + |
| 10 | +namespace SimpleKVM.Displays.win |
| 11 | +{ |
| 12 | + public static class DisplaySystem_1 |
| 13 | + { |
| 14 | + public static string ControlMyMonitorExe => Path.Combine(AppDomain.CurrentDomain.BaseDirectory ?? "", @"ext\win\controlmymonitor\ControlMyMonitor.exe"); |
| 15 | + |
| 16 | + public static IList<Monitor> GetMonitors() |
| 17 | + { |
| 18 | + var selfPath = AppDomain.CurrentDomain.BaseDirectory; |
| 19 | + |
| 20 | + if (selfPath == null) |
| 21 | + { |
| 22 | + return new List<Monitor>(); |
| 23 | + } |
| 24 | + |
| 25 | + var controlMyMonitorExe = Path.Combine(selfPath, @"ext\win\controlmymonitor\ControlMyMonitor.exe"); |
| 26 | + |
| 27 | + var monitorListCommand = new ProcessStartInfo |
| 28 | + { |
| 29 | + FileName = ControlMyMonitorExe, |
| 30 | + Arguments = "/smonitors" |
| 31 | + }; |
| 32 | + |
| 33 | + var monitorListStdout = monitorListCommand.StartAndReadStdout(); |
| 34 | + |
| 35 | + /* |
| 36 | + Monitor Device Name: "\\.\DISPLAY1\Monitor0" |
| 37 | + Monitor Name: "VX4380 SERIES" |
| 38 | + Serial Number: "" |
| 39 | + Adapter Name: "NVIDIA GeForce GTX 760" |
| 40 | + Monitor ID: "MONITOR\VSC5B34\{4d36e96e-e325-11ce-bfc1-08002be10318}\0004" |
| 41 | +
|
| 42 | + Monitor Device Name: "\\.\DISPLAY2\Monitor0" |
| 43 | + Monitor Name: "VX4380 SERIES" |
| 44 | + Serial Number: "" |
| 45 | + Adapter Name: "NVIDIA GeForce GTX 760" |
| 46 | + Monitor ID: "MONITOR\VSC5B34\{4d36e96e-e325-11ce-bfc1-08002be10318}\0000" |
| 47 | + */ |
| 48 | + |
| 49 | + var result = monitorListStdout |
| 50 | + .SplitAndKeep("Monitor Device Name") |
| 51 | + .Where(line => !string.IsNullOrEmpty(line)) |
| 52 | + .AsParallel() |
| 53 | + .AsOrdered() |
| 54 | + .Select(block => |
| 55 | + { |
| 56 | + var dict = Regex |
| 57 | + .Split(block, "\r\n|\r|\n") |
| 58 | + .Where(line => !string.IsNullOrEmpty(line)) |
| 59 | + .Select(line => line.Split(new[] { ": " }, StringSplitOptions.None)) |
| 60 | + .ToDictionary(tokens => tokens[0], tokens => tokens[1].Replace("\"", "").RemoveNonPrintable()); |
| 61 | + |
| 62 | + var monitorInfoCommand = new ProcessStartInfo |
| 63 | + { |
| 64 | + FileName = controlMyMonitorExe, |
| 65 | + Arguments = $"/scomma \"\" \"{dict["Monitor Name"]}" |
| 66 | + }; |
| 67 | + |
| 68 | + var monitorInfo = monitorInfoCommand.StartAndReadStdout(); |
| 69 | + |
| 70 | + //VPC Code |
| 71 | + //60,Input Select,Read+Write,18,19,"15, 16, 17, 18" |
| 72 | + var inputSelectLine = Regex |
| 73 | + .Split(monitorInfo, "\r\n|\r|\n") |
| 74 | + .Where(line => line.StartsWith("60")) |
| 75 | + .FirstOrDefault(); |
| 76 | + |
| 77 | + var inputSelectLineTokens = Regex.Split(inputSelectLine, "(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)") //split based on commas, taking into account double-quotes |
| 78 | + .Where(line => !string.IsNullOrEmpty(line)) |
| 79 | + .Select(line => line.Replace("\"", "")) |
| 80 | + .ToList(); |
| 81 | + |
| 82 | + |
| 83 | + var currentSource = int.Parse(inputSelectLineTokens[3]); |
| 84 | + |
| 85 | + var validSources = inputSelectLineTokens[5] |
| 86 | + .Split(new[] { ", " }, StringSplitOptions.None) |
| 87 | + .Select(source => int.Parse(source)) |
| 88 | + .OrderBy(source => source) |
| 89 | + .ToList(); |
| 90 | + |
| 91 | + return new Monitor() |
| 92 | + { |
| 93 | + //AdapterName = dict["Adapter Name"], |
| 94 | + //MonitorDeviceName = dict["Monitor Device Name"], |
| 95 | + //MonitorId = dict["Monitor ID"], |
| 96 | + //MonitorName = dict["Monitor Name"], |
| 97 | + //SerialNumber = dict["Serial Number"], |
| 98 | + |
| 99 | + CurrentSource = currentSource, |
| 100 | + ValidSources = validSources |
| 101 | + }; |
| 102 | + }) |
| 103 | + .ToList(); |
| 104 | + |
| 105 | + return result; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments