├── app ├── Resources │ ├── admin.png │ ├── github.png │ ├── icon.ico │ ├── sync.png │ ├── settings.png │ ├── background.png │ └── translate.png ├── App.config ├── Components │ ├── CloseButton.xaml.cs │ ├── Button.xaml.cs │ ├── CloseButton.xaml │ ├── Button.xaml │ ├── DisplayItem.xaml │ ├── CustomPage.xaml.cs │ ├── CustomPage.xaml │ └── DisplayItem.xaml.cs ├── Vdd │ ├── Errors.cs │ ├── Controller.cs │ ├── Utils.cs │ └── Core.cs ├── App.xaml ├── Properties │ ├── App.manifest │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ └── Resources.resx ├── Program.cs ├── Updater.cs ├── ParsecVDisplay.csproj ├── Languages │ ├── zh.xaml │ ├── vi.xaml │ └── en.xaml ├── MainWindow.xaml ├── Config.cs ├── App.xaml.cs ├── Helper.cs ├── MainWindow.xaml.cs ├── CLI.cs ├── PowerEvents.cs └── MirrorWindow.cs ├── LICENSE ├── parsec-vdd.sln ├── .github └── workflows │ ├── build.yml │ └── publish.yml ├── core ├── vdd-demo.cc └── parsec-vdd.h ├── docs ├── VDD_CLI_USAGE.md ├── VDD_LIBRARY_USAGE.md └── PARSEC_VDD_SPECS.md ├── README.md └── .gitignore /app/Resources/admin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/admin.png -------------------------------------------------------------------------------- /app/Resources/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/github.png -------------------------------------------------------------------------------- /app/Resources/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/icon.ico -------------------------------------------------------------------------------- /app/Resources/sync.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/sync.png -------------------------------------------------------------------------------- /app/Resources/settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/settings.png -------------------------------------------------------------------------------- /app/Resources/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/background.png -------------------------------------------------------------------------------- /app/Resources/translate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomi-san/parsec-vdd/HEAD/app/Resources/translate.png -------------------------------------------------------------------------------- /app/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/Components/CloseButton.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows; 3 | using System.Windows.Controls; 4 | 5 | namespace ParsecVDisplay.Components 6 | { 7 | public partial class CloseButton : UserControl 8 | { 9 | public CloseButton() 10 | { 11 | InitializeComponent(); 12 | } 13 | 14 | private void Button_Click(object sender, RoutedEventArgs e) 15 | { 16 | var window = Window.GetWindow(this); 17 | window?.Close(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /app/Components/Button.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows; 3 | using System.Windows.Controls; 4 | 5 | namespace ParsecVDisplay.Components 6 | { 7 | public partial class Button : UserControl 8 | { 9 | public event EventHandler Click; 10 | public new object Content { get; set; } 11 | 12 | public static readonly new DependencyProperty ContentProperty = 13 | DependencyProperty.Register("Content", typeof(object), typeof(Button), new PropertyMetadata(null)); 14 | 15 | public Button() 16 | { 17 | InitializeComponent(); 18 | DataContext = this; 19 | } 20 | 21 | private void Button_Click(object sender, RoutedEventArgs e) 22 | { 23 | Click?.Invoke(sender, e); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /app/Vdd/Errors.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ParsecVDisplay.Vdd 4 | { 5 | internal class ErrorDriverStatus : Exception 6 | { 7 | public readonly Device.Status Status; 8 | 9 | public ErrorDriverStatus(Device.Status status) 10 | { 11 | this.Status = status; 12 | } 13 | } 14 | 15 | internal class ErrorDeviceHandle : Exception 16 | { 17 | } 18 | 19 | internal class ErrorExceededLimit : Exception 20 | { 21 | public readonly int Limit; 22 | 23 | public ErrorExceededLimit(int limit) 24 | { 25 | this.Limit = limit; 26 | } 27 | } 28 | 29 | internal class ErrorOperationFailed : Exception 30 | { 31 | public enum Operation 32 | { 33 | AddDisplay, 34 | RemoveDisplay, 35 | } 36 | 37 | public readonly Operation Type; 38 | 39 | public ErrorOperationFailed(Operation type) 40 | { 41 | this.Type = type; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Nguyen Duy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /app/App.xaml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 4 14 | 8 15 | 16 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/Properties/App.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | true 22 | true 23 | PerMonitorV2 24 | 25 | 26 | 27 | 28 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /parsec-vdd.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.11.35327.3 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParsecVDisplay", "app\ParsecVDisplay.csproj", "{2D44934F-B4CF-4F2C-BD03-AE60B71AD045}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{9B03AE1C-BE49-45A8-9B89-11CE21AD1BF8}" 9 | ProjectSection(SolutionItems) = preProject 10 | core\parsec-vdd.h = core\parsec-vdd.h 11 | core\vdd-demo.cc = core\vdd-demo.cc 12 | EndProjectSection 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{44F3B28D-7C30-43D1-8153-C7A88B29B511}" 15 | ProjectSection(SolutionItems) = preProject 16 | .github\workflows\build.yml = .github\workflows\build.yml 17 | EndProjectSection 18 | EndProject 19 | Global 20 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 21 | Debug|Any CPU = Debug|Any CPU 22 | Release|Any CPU = Release|Any CPU 23 | EndGlobalSection 24 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 25 | {2D44934F-B4CF-4F2C-BD03-AE60B71AD045}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {2D44934F-B4CF-4F2C-BD03-AE60B71AD045}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {2D44934F-B4CF-4F2C-BD03-AE60B71AD045}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {2D44934F-B4CF-4F2C-BD03-AE60B71AD045}.Release|Any CPU.Build.0 = Release|Any CPU 29 | EndGlobalSection 30 | GlobalSection(SolutionProperties) = preSolution 31 | HideSolutionNode = FALSE 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {745BF2B1-7A1D-48D6-87E6-3AF8CEAB96C5} 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /app/Components/CloseButton.xaml: -------------------------------------------------------------------------------- 1 | 12 | 36 | -------------------------------------------------------------------------------- /app/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using System.Windows.Forms; 6 | 7 | namespace ParsecVDisplay 8 | { 9 | public static class Program 10 | { 11 | public const string AppId = "QpHOX8IBUHBznGtqk9xm1"; 12 | public const string AppName = "ParsecVDisplay"; 13 | public const string AppVersion = "1.0.2"; 14 | public const string GitHubRepo = "nomi-san/parsec-vdd"; 15 | 16 | [STAThread] 17 | static int Main(string[] args) 18 | { 19 | if (args.Length >= 2 && args[0] == "-custom") 20 | { 21 | var modes = Display.ParseModes(args[1]); 22 | Vdd.Utils.SetCustomDisplayModes(modes); 23 | 24 | if (args.Length >= 3) 25 | { 26 | if (Enum.TryParse(args[2], true, out var kind)) 27 | { 28 | Vdd.Utils.SetParentGPU(kind); 29 | } 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | if (args.Length > 0 && args[0] == "-cli") 36 | { 37 | args = args.Skip(1).ToArray(); 38 | return CLI.Execute(args); 39 | } 40 | 41 | if (SingleInstance()) 42 | { 43 | App.LoadTranslations(); 44 | Helper.StayAwake(false); 45 | 46 | Application.Run(new Tray()); 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | static bool SingleInstance() 53 | { 54 | bool isOwned = false; 55 | var signal = new EventWaitHandle(false, 56 | EventResetMode.AutoReset, AppId, out isOwned); 57 | 58 | if (isOwned) 59 | { 60 | Task.Run(() => 61 | { 62 | while (signal.WaitOne()) 63 | { 64 | Tray.Instance?.Invoke(Tray.Instance.ShowApp); 65 | } 66 | }); 67 | } 68 | else 69 | { 70 | signal.Set(); 71 | signal.Dispose(); 72 | } 73 | 74 | return isOwned; 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /app/Components/Button.xaml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 22 | 23 | 24 | 49 | 50 | -------------------------------------------------------------------------------- /app/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle(ParsecVDisplay.Program.AppName)] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct(ParsecVDisplay.Program.AppName)] 15 | [assembly: AssemblyCopyright("Copyright (c) 2024")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion(ParsecVDisplay.Program.AppVersion)] 55 | [assembly: AssemblyFileVersion(ParsecVDisplay.Program.AppVersion)] 56 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | net480: 7 | description: Target .NET 4.8 8 | type: boolean 9 | default: false 10 | debug: 11 | description: Build in debug mode 12 | type: boolean 13 | default: false 14 | 15 | workflow_call: 16 | outputs: 17 | artifact-id: 18 | description: Uploaded artifact ID 19 | value: ${{ jobs.build.outputs.artifact-id }} 20 | artifact-name: 21 | description: Uploaded artifact name 22 | value: ${{ jobs.build.outputs.artifact-name }} 23 | 24 | jobs: 25 | build: 26 | runs-on: windows-2022 27 | outputs: 28 | artifact-id: ${{ steps.artifact-upload-step.outputs.artifact-id }} 29 | artifact-name: ${{ steps.prepare-output-step.outputs.artifact-name }} 30 | 31 | steps: 32 | - name: Checkout repo 33 | uses: actions/checkout@v4 34 | with: 35 | ref: ${{ github.ref }} 36 | fetch-depth: 0 37 | 38 | - name: Prepare envars 39 | run: | 40 | echo "SHORT_SHA=$("${{ github.sha }}" | cut -c1-8)" >> $env:GITHUB_ENV 41 | echo "APP_VERSION=$((Get-Content -Path "app\Program.cs" | Select-String -Pattern 'AppVersion\s=\s"(.+)"' -AllMatches).Matches.Groups[1].Value)" >> $env:GITHUB_ENV 42 | 43 | - name: Target .NET 4.8 44 | if: ${{ inputs.net480 }} 45 | run: | 46 | (Get-Content app\ParsecVDisplay.csproj) -replace "TargetFramework>net472", "TargetFramework>net480" | Out-File app\ParsecVDisplay.csproj 47 | (Get-Content app\App.config) -replace "Version=v4.7.2", "Version=v4.8" | Out-File app\App.config 48 | 49 | - name: Setup msbuild 50 | uses: microsoft/setup-msbuild@v2 51 | 52 | - name: Build app 53 | run: | 54 | cd app 55 | msbuild ParsecVDisplay.csproj /t:Restore 56 | msbuild ParsecVDisplay.csproj /t:Build /p:Configuration=${{ inputs.debug && 'Debug' || 'Release' }} /p:Platform=AnyCPU 57 | 58 | - name: Prepare build output 59 | id: prepare-output-step 60 | run: | 61 | echo "${{ env.APP_VERSION }}+${{ env.SHORT_SHA }}" >> app\bin\version 62 | echo "artifact-name=ParsecVDisplay-v${{ env.APP_VERSION }}-${{ env.SHORT_SHA }}" >> $env:GITHUB_OUTPUT 63 | 64 | - name: Upload build output 65 | uses: actions/upload-artifact@v4 66 | id: artifact-upload-step 67 | with: 68 | name: ${{ steps.prepare-output-step.outputs.artifact-name }} 69 | path: | 70 | app/bin/version 71 | app/bin/vdd.cmd 72 | app/bin/ParsecVDisplay.exe 73 | app/bin/ParsecVDisplay.exe.config 74 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | build: 8 | uses: ./.github/workflows/build.yml 9 | 10 | publish: 11 | needs: build 12 | runs-on: windows-2022 13 | 14 | steps: 15 | - name: Checkout repo 16 | uses: actions/checkout@v4 17 | with: 18 | ref: ${{ github.ref }} 19 | fetch-depth: 0 20 | 21 | - name: Sign the build 22 | uses: signpath/github-action-submit-signing-request@v1 23 | with: 24 | api-token: ${{ secrets.SIGNPATH_API_TOKEN }} 25 | organization-id: ${{ secrets.SIGNPATH_ORG_ID }} 26 | project-slug: parsec-vdd 27 | signing-policy-slug: release-signing 28 | artifact-configuration-slug: zipped-exe 29 | github-artifact-id: "${{ needs.build.outputs.artifact-id }}" 30 | wait-for-completion: true 31 | output-artifact-directory: bin 32 | parameters: | 33 | Version: "${{ github.ref_name }}" 34 | 35 | - name: Fetch distro branch 36 | uses: actions/checkout@v4 37 | with: 38 | ref: distro 39 | fetch-depth: 0 40 | path: distro 41 | 42 | - name: Make setup installer & portable app 43 | run: | 44 | mkdir distro\publish 45 | Compress-Archive -Path bin\* -DestinationPath distro\publish\portable.zip -Force 46 | cd distro 47 | & "$env:ProgramFiles (x86)\Inno Setup 6\iscc.exe" setup.iss 48 | 49 | - name: Upload installer output 50 | uses: actions/upload-artifact@v4 51 | id: artifact-upload-installer-step 52 | with: 53 | name: setup-installer 54 | path: | 55 | distro/out/*.exe 56 | 57 | - name: Sign the installer 58 | uses: signpath/github-action-submit-signing-request@v1 59 | with: 60 | api-token: ${{ secrets.SIGNPATH_API_TOKEN }} 61 | organization-id: ${{ secrets.SIGNPATH_ORG_ID }} 62 | project-slug: parsec-vdd 63 | signing-policy-slug: release-signing 64 | artifact-configuration-slug: zipped-exe 65 | github-artifact-id: "${{steps.artifact-upload-installer-step.outputs.artifact-id}}" 66 | wait-for-completion: true 67 | output-artifact-directory: distro/publish 68 | parameters: | 69 | Version: "${{ github.ref_name }}" 70 | 71 | - name: Delete unwanted artifacts 72 | uses: geekyeggo/delete-artifact@v5 73 | with: 74 | name: "*" 75 | 76 | - name: Upload signed output 77 | uses: actions/upload-artifact@v4 78 | with: 79 | name: ${{ needs.build.outputs.artifact-name }} 80 | path: distro/publish/ 81 | -------------------------------------------------------------------------------- /app/Updater.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Net.Http; 4 | using System.Net.Http.Headers; 5 | using System.Text.RegularExpressions; 6 | using System.Threading.Tasks; 7 | 8 | namespace ParsecVDisplay 9 | { 10 | internal static class Updater 11 | { 12 | public static string DOWNLOAD_URL => $"https://github.com/{Program.GitHubRepo}/releases/latest"; 13 | static string GH_API_URL => $"https://api.github.com/repos/{Program.GitHubRepo}/releases/latest"; 14 | 15 | static Updater() 16 | { 17 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 18 | | SecurityProtocolType.Tls12; 19 | } 20 | 21 | public static Task CheckUpdate() 22 | { 23 | return Task.Run(async () => 24 | { 25 | var localVersion = new Version(Program.AppVersion); 26 | var remoteVersion = await FetchLatestVersion(); 27 | 28 | if (remoteVersion.CompareTo(localVersion) > 0) 29 | { 30 | return remoteVersion.ToString(); 31 | } 32 | 33 | return string.Empty; 34 | }); 35 | } 36 | 37 | static async Task FetchLatestVersion() 38 | { 39 | try 40 | { 41 | var json = await DownloadString(GH_API_URL); 42 | 43 | if (!string.IsNullOrEmpty(json)) 44 | { 45 | var tagNameRegex = new Regex("\"tag_name\":\\s+\"(.*)\""); 46 | var match = tagNameRegex.Match(json); 47 | 48 | if (match.Success && match.Groups.Count > 1) 49 | { 50 | var vtag = match.Groups[1].Value.ToLower(); 51 | if (vtag.StartsWith("v")) 52 | vtag = vtag.Substring(1); 53 | 54 | return new Version(vtag); 55 | } 56 | } 57 | } 58 | catch 59 | { 60 | } 61 | 62 | return new Version(Program.AppVersion); 63 | } 64 | 65 | static async Task DownloadString(string url) 66 | { 67 | using (var client = new HttpClient()) 68 | { 69 | client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + 70 | "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"); 71 | client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue 72 | { 73 | NoCache = true 74 | }; 75 | 76 | return await client.GetStringAsync(url); 77 | } 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /app/ParsecVDisplay.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net472 6 | {2D44934F-B4CF-4F2C-BD03-AE60B71AD045} 7 | true 8 | true 9 | prompt 10 | false 11 | true 12 | false 13 | false 14 | 15 | 16 | 17 | bin\ 18 | ParsecVDisplay 19 | ParsecVDisplay 20 | ParsecVDisplay.Program 21 | 22 | 23 | 24 | WinExe 25 | 26 | 27 | 28 | Resources\icon.ico 29 | Properties\app.manifest 30 | AnyCPU 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /core/vdd-demo.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "parsec-vdd.h" 7 | 8 | using namespace std::chrono_literals; 9 | using namespace parsec_vdd; 10 | 11 | int main() 12 | { 13 | // Check driver status. 14 | DeviceStatus status = QueryDeviceStatus(&VDD_CLASS_GUID, VDD_HARDWARE_ID); 15 | if (status != DEVICE_OK) 16 | { 17 | printf("Parsec VDD device is not OK, got status %d.\n", status); 18 | return 1; 19 | } 20 | 21 | // Obtain device handle. 22 | HANDLE vdd = OpenDeviceHandle(&VDD_ADAPTER_GUID); 23 | if (vdd == NULL || vdd == INVALID_HANDLE_VALUE) { 24 | printf("Failed to obtain the device handle.\n"); 25 | return 1; 26 | } 27 | 28 | bool running = true; 29 | std::vector displays; 30 | 31 | // Side thread for updating vdd. 32 | std::thread updater([&running, vdd] { 33 | while (running) { 34 | VddUpdate(vdd); 35 | std::this_thread::sleep_for(100ms); 36 | } 37 | }); 38 | 39 | // Print out guide. 40 | printf("Press A to add a virtual display.\n"); 41 | printf("Press R to remove the last added.\n"); 42 | printf("Press Q to quit (then unplug all).\n\n"); 43 | 44 | while (running) { 45 | switch (_getch()) { 46 | // quit 47 | case 'q': 48 | running = false; 49 | break; 50 | // add display 51 | case 'a': 52 | if (displays.size() < VDD_MAX_DISPLAYS) { 53 | int index = VddAddDisplay(vdd); 54 | if (index != -1) { 55 | displays.push_back(index); 56 | printf("Added a new virtual display, index: %d.\n", index); 57 | } 58 | else { 59 | printf("Add virtual display failed."); 60 | } 61 | } 62 | else { 63 | printf("Limit exceeded (%d), could not add more virtual displays.\n", VDD_MAX_DISPLAYS); 64 | } 65 | break; 66 | // remove display 67 | case 'r': 68 | if (displays.size() > 0) { 69 | int index = displays.back(); 70 | VddRemoveDisplay(vdd, index); 71 | displays.pop_back(); 72 | printf("Removed the last virtual display, index: %d.\n", index); 73 | } 74 | else { 75 | printf("No added virtual displays.\n"); 76 | } 77 | break; 78 | } 79 | } 80 | 81 | // Remove all before exiting. 82 | for (int index : displays) { 83 | VddRemoveDisplay(vdd, index); 84 | } 85 | 86 | if (updater.joinable()) { 87 | updater.join(); 88 | } 89 | 90 | // Close the device handle. 91 | CloseDeviceHandle(vdd); 92 | 93 | return 0; 94 | } -------------------------------------------------------------------------------- /app/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace ParsecVDisplay.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ParsecVDisplay.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/Languages/zh.xaml: -------------------------------------------------------------------------------- 1 | 6 | 简体中文 7 | 8 | 9 | 添加虚拟显示器 10 | 请先添加虚拟显示器! 11 | 自定义... 12 | 应用 13 | 主 GPU 14 | 语言 15 | 16 | 17 | 分辨率 18 | 刷新率 19 | 屏幕方向 20 | 水平 21 | 纵向 22 | 水平 (翻转) 23 | 纵向 (翻转) 24 | 屏幕截图 25 | 移除 26 | 27 | 28 | 移除最后一个虚拟显示器 29 | 选项 30 | 开机启动 31 | 恢复上次关闭的显示器 32 | 无屏时启用虚拟显示器 33 | 保持屏幕开启 34 | 检查更新 35 | 退出 36 | 37 | 38 | 应用程序版本已是最新。 39 | 有新的更新可用 — v{0}。\n为了保持您的最佳体验,请立即更新。" 40 | 41 | 无法应用自定义分辨率,访问被拒绝! 42 | 插槽 #{0} 中的值无效。 43 | 44 | 达到最大虚拟显示器限额,无法添加更多的虚拟显示器 ({0})。 45 | 即将移除全部的虚拟显示器。\n您是否仍然要退出? 46 | 47 | 驱动状态 48 | 你需要重新启动计算机才能使驱动程序正常工作 49 | {0} 已禁用,请在设备管理器中启用它。 50 | 请先安装驱动程序。 51 | 驱动程序不正常,请重新检查。当前状态: {0}。 52 | 53 | 无法获取设备句柄,请重新检查驱动程序安装。 54 | 无法添加虚拟显示器。 55 | 无法移除虚拟显示器。 56 | 57 | -------------------------------------------------------------------------------- /app/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 22 | 23 | 24 | 25 | 26 | 27 | 64 | -------------------------------------------------------------------------------- /app/Config.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Microsoft.Win32; 4 | 5 | namespace ParsecVDisplay 6 | { 7 | internal static class Config 8 | { 9 | static string REG_PATH => $"HKEY_CURRENT_USER\\SOFTWARE\\{Program.AppName}"; 10 | static string REG_STARTUP_PATH => @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; 11 | 12 | public static string Language 13 | { 14 | get => GetString(nameof(Language), "English"); 15 | set => SetString(nameof(Language), value); 16 | } 17 | 18 | public static int DisplayCount 19 | { 20 | get => GetInt(nameof(DisplayCount), 0); 21 | set => SetInt(nameof(DisplayCount), value); 22 | } 23 | 24 | public static bool FallbackDisplay 25 | { 26 | get => GetInt(nameof(FallbackDisplay)) != 0; 27 | set => SetInt(nameof(FallbackDisplay), value ? 1 : 0); 28 | } 29 | 30 | public static bool KeepScreenOn 31 | { 32 | get 33 | { 34 | bool enable = GetInt(nameof(KeepScreenOn)) != 0; 35 | Helper.StayAwake(enable); 36 | return enable; 37 | } 38 | set 39 | { 40 | SetInt(nameof(KeepScreenOn), value ? 1 : 0); 41 | Helper.StayAwake(value); 42 | } 43 | } 44 | 45 | public static bool SkipDriverCheck 46 | { 47 | get => GetInt(nameof(SkipDriverCheck)) != 0; 48 | set => SetInt(nameof(SkipDriverCheck), value ? 1 : 0); 49 | } 50 | 51 | public static int MirroringFPS 52 | { 53 | get => GetInt(nameof(MirroringFPS), 30); 54 | set => SetInt(nameof(MirroringFPS), value); 55 | } 56 | 57 | #region Registry data store 58 | 59 | static string GetString(string key, string @default) 60 | { 61 | var value = Registry.GetValue(REG_PATH, key, null); 62 | return value == null ? @default : Convert.ToString(value); 63 | } 64 | 65 | static void SetString(string key, string value) 66 | { 67 | Registry.SetValue(REG_PATH, key, value, RegistryValueKind.String); 68 | } 69 | 70 | static int GetInt(string key, int @default = 0) 71 | { 72 | var value = Registry.GetValue(REG_PATH, key, null); 73 | return value == null ? @default : Convert.ToInt32(value); 74 | } 75 | 76 | static void SetInt(string key, int value) 77 | { 78 | Registry.SetValue(REG_PATH, key, value, RegistryValueKind.DWord); 79 | } 80 | 81 | #endregion 82 | 83 | public static bool RunOnStartup 84 | { 85 | get 86 | { 87 | using (var key = Registry.CurrentUser.OpenSubKey(REG_STARTUP_PATH, false)) 88 | { 89 | return key.GetValue(Program.AppName) != null; 90 | } 91 | } 92 | set 93 | { 94 | using (var key = Registry.CurrentUser.OpenSubKey(REG_STARTUP_PATH, true)) 95 | { 96 | if (value) 97 | { 98 | var exePath = Assembly.GetExecutingAssembly().Location; 99 | key.SetValue(Program.AppName, $"\"{exePath}\" -silent", RegistryValueKind.String); 100 | } 101 | else 102 | { 103 | key.DeleteValue(Program.AppName, false); 104 | } 105 | } 106 | } 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /app/Components/DisplayItem.xaml: -------------------------------------------------------------------------------- 1 | 12 | 15 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /app/Languages/vi.xaml: -------------------------------------------------------------------------------- 1 | 6 | Tiếng Việt 7 | 8 | 9 | Thêm màn hình 10 | Hãy thêm màn hình ảo! 11 | Tùy chỉnh... 12 | Áp dụng 13 | GPU nguồn 14 | Ngôn ngữ 15 | 16 | 17 | Độ phân giải 18 | Tần số quét 19 | Hướng màn hình 20 | Ngang 21 | Dọc 22 | Ngang (lật ngược) 23 | Dọc (lật ngược) 24 | Chụp màn hình 25 | Tháo bỏ 26 | 27 | 28 | Bỏ màn hình cuối 29 | Tùy chọn 30 | Chạy cùng hệ thống 31 | Khôi phục lại màn hình 32 | Màn hình dự phòng 33 | Luôn sáng màn hình 34 | Kiểm tra cập nhật 35 | Thoát 36 | 37 | 38 | Bạn đang sử dụng phiên bản mới nhất. 39 | Có bản cập nhật mới — v{0}.\nHãy cập nhật ngay để trải nghiệm các tính năng mới." 40 | 41 | Không thể áp dụng độ phân giải tùy chỉnh, quyền truy cập bị từ chối! 42 | Giá trị không hợp lệ ở slot #{0}. 43 | 44 | Không thể thêm thêm màn hình ảo, bạn đã đạt đến con số giới hạn ({0}). 45 | Tất cả màn hình ảo sẽ bị tháo bỏ.\nBạn có muốn thoát không?. 46 | 47 | Trạng thái driver 48 | Bạn phải khởi động lại PC để driver hoạt động. 49 | {0} đã bị vô hiệu, vui lòng bật nó trong Device Manager. 50 | Vui lòng cài đặt driver trước. 51 | Driver không được OK, vui lòng kiểm tra lại. Trạng thái hiện tại: {0}. 52 | 53 | Không thể lấy 'device handle', vui lòng kiểm tra cài đặt driver và thử lại. 54 | Lỗi khi thêm màn hình ảo. 55 | Lỗi khi gỡ màn hình ảo. 56 | 57 | -------------------------------------------------------------------------------- /app/Languages/en.xaml: -------------------------------------------------------------------------------- 1 | 6 | English 7 | 8 | 9 | Add display 10 | Add a virtual display first! 11 | Custom... 12 | Apply 13 | Parent GPU 14 | Language 15 | 16 | 17 | Resolution 18 | Refresh rate 19 | Orientation 20 | Landscape 21 | Portrait 22 | Landscape (flipped) 23 | Portrait (flipped) 24 | Take screenshot 25 | Remove 26 | 27 | 28 | Remove last display 29 | Options 30 | Run on startup 31 | Restore displays 32 | Fallback display 33 | Keep screen on 34 | Check for update 35 | Exit 36 | 37 | 38 | Your app version is up-to-date. 39 | A new version — v{0} is available.\nTo keep your experience optimal, please update it now." 40 | 41 | Could not apply custom resolutions, access denied! 42 | Found invalid value in slot #{0}. 43 | 44 | Could not add more virtual displays, you have exceeded the maximum number ({0}). 45 | All added virtual displays will be unplugged.\nDo you still want to exit?. 46 | 47 | Driver status 48 | You must restart your PC to complete the driver setup. 49 | {0} is disabled, please enable it in Device Manager. 50 | Please install the driver first. 51 | The driver is not OK, please check again. Current status: {0}. 52 | 53 | Failed to obtain the device handle, please check the driver installation again. 54 | Failed to add virtual display. 55 | Failed to remove virtual display. 56 | 57 | -------------------------------------------------------------------------------- /docs/VDD_CLI_USAGE.md: -------------------------------------------------------------------------------- 1 | # Command Line Interface 2 | 3 | This is detailed help for CLI mode of the ParsecVDisplay app. The CLI executable 4 | (`vdd`) can be installed via setup installer and can be invoked via command line 5 | environment. 6 | 7 | > Check out the [Releases page](https://github.com/nomi-san/parsec-vdd/releases) 8 | > to get the setup installer (from v1.0) which has filename ends with 9 | > `-setup.exe`. 10 | 11 | ## Usage 12 | 13 | Run the command below to check if `vdd` is installed. 14 | 15 | ```sh 16 | vdd -h 17 | ``` 18 | 19 | If success, you will get the help: 20 | 21 | ```sh 22 | vdd command [args...] 23 | -a|add - Add a virtual display 24 | -r|remove - Remove the last added virtual display 25 | X - Remove the virtual display at index X (number) 26 | all - Remove all the added virtual displays 27 | -l|list - Show all the added virtual displays and specs 28 | -s|set X WxH - Set resolution for a virtual display 29 | where X is index number, WxH is size, e.g 1920x1080 30 | X @R - Set only the refresh rate R, e.g @60, @120 (hz) 31 | on Powershell, you should replace '@' with 'r' 32 | X WxH@R - Set full display mode as above, e.g 1920x1080@144 33 | -v|version - Query driver version and status 34 | -h|help - Show this help 35 | ``` 36 | 37 | ### Adding virtual display 38 | 39 | Use command `-a` or `add` to add a virtual display. 40 | 41 | ```sh 42 | vdd -a 43 | ``` 44 | 45 | The exit code is the index of added display, you can reuse this index to remove 46 | the display. Less than 0 means error occurred. 47 | 48 | ### Removing virtual display 49 | 50 | Use command `-r` or `remove` to remove the last added. 51 | 52 | ```sh 53 | vdd -r 54 | ``` 55 | 56 | Remove the added virtual display at index `0`. 57 | 58 | ```sh 59 | vdd -r 0 60 | ``` 61 | 62 | To remove all the added, replace the index with `all` or `*`. 63 | 64 | ``` 65 | vdd -r all 66 | ``` 67 | 68 | ### Listing added displays 69 | 70 | List all added virtual displays. 71 | 72 | ```sh 73 | vdd -l 74 | ``` 75 | 76 | The exit code is the number of added virtual displays. 77 | 78 | Example of output: 79 | 80 | ``` 81 | Index: 0 82 | - Device: \\.\DISPLAY37 83 | - Number: 2 84 | - Name: PSCCDD0 85 | - Mode: 1600 x 900 @ 60 Hz 86 | - Orientation: Landscape (0°) 87 | ``` 88 | 89 | ### Setting display mode 90 | 91 | A resolution is the display dimension (width x height) in pixels. A display mode 92 | extends it plus a refresh rate such as 1920 x 1080 @ 60 Hz. 93 | 94 | Set resolution for a virtual display at index 1. 95 | 96 | ```sh 97 | vdd set 1 1920x1080 98 | ``` 99 | 100 | With full display mode, plus 120 Hz. 101 | 102 | ```sh 103 | vdd set 1 1920x1080 @120 104 | ``` 105 | 106 | With only refresh rate. 107 | 108 | ```sh 109 | vdd set 1 @144 110 | ``` 111 | 112 | On Powershell terminal, you should replace the symbol `@` with letter `r`. 113 | 114 | ```powershell 115 | vdd set 1 1920x1080 r120 116 | ``` 117 | 118 | The command will fail and exit with non-zero if the display mode is invalid. 119 | 120 | ### Querying version 121 | 122 | Query the driver status and version. 123 | 124 | ```sh 125 | vdd -v 126 | ``` 127 | 128 | Example output: 129 | 130 | ``` 131 | Parsec Virtual Display Adapter 132 | - Status: OK 133 | - Version: 0.45 134 | ``` 135 | 136 | Here is the list of possible status and its code: 137 | 138 | ```js 139 | 0 OK - Ready to use 140 | 1 INACCESSIBLE - Inaccessible 141 | 2 UNKNOWN - Unknown status 142 | 3 UNKNOWN_PROBLEM - Unknown problem 143 | 4 DISABLED - Device is disabled 144 | 5 DRIVER_ERROR - Device encountered error 145 | 6 RESTART_REQUIRED - Must restart PC to use (could ignore but would have issue) 146 | 7 DISABLED_SERVICE - Service is disabled 147 | 8 NOT_INSTALLED - Driver is not installed 148 | ``` 149 | 150 | The status code is also the exit code of this command. 151 | -------------------------------------------------------------------------------- /app/Components/CustomPage.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Windows; 5 | using System.Windows.Controls; 6 | using System.Windows.Media; 7 | 8 | namespace ParsecVDisplay.Components 9 | { 10 | public partial class CustomPage : Page 11 | { 12 | TextBox[] TextBoxes; 13 | 14 | public CustomPage() 15 | { 16 | InitializeComponent(); 17 | 18 | xParentGPU.Items.Clear(); 19 | foreach (var item in Enum.GetValues(typeof(Vdd.Utils.ParentGPU))) 20 | { 21 | xParentGPU.Items.Add(item.ToString()); 22 | } 23 | } 24 | 25 | private void ApplyChanges(object sender, EventArgs e) 26 | { 27 | var modes = new List(); 28 | 29 | for (int i = 0; i < 15; i += 3) 30 | { 31 | var tw = TextBoxes[i].Text; 32 | var th = TextBoxes[i + 1].Text; 33 | var thz = TextBoxes[i + 2].Text; 34 | 35 | if (int.TryParse(tw, out var width) 36 | && int.TryParse(th, out var height) 37 | && int.TryParse(thz, out var hz)) 38 | { 39 | // Check negative values & limit 8K resolution 40 | if (width < 0 || width > 7680 || height < 0 || height > 4320 || hz < 0) 41 | { 42 | MessageBox.Show(App.GetTranslation("t_msg_custom_invalid_slot", i / 3 + 1), 43 | Program.AppName, MessageBoxButton.OK, MessageBoxImage.Warning); 44 | return; 45 | } 46 | else 47 | { 48 | modes.Add(new Display.Mode(width, height, hz)); 49 | } 50 | } 51 | } 52 | 53 | Vdd.Utils.ParentGPU parentGPU; 54 | bool validParentGPU = Enum.TryParse(xParentGPU.SelectedValue.ToString(), true, out parentGPU); 55 | 56 | if (modes.Count > 0 && validParentGPU) 57 | { 58 | if (Helper.IsAdmin()) 59 | { 60 | Vdd.Utils.SetCustomDisplayModes(modes); 61 | Vdd.Utils.SetParentGPU(parentGPU); 62 | } 63 | else 64 | { 65 | var args = $"-custom \"{Display.DumpModes(modes)}\" \"{parentGPU}\""; 66 | if (Helper.RunAdminTask(args) == false) 67 | { 68 | MessageBox.Show(App.GetTranslation("t_msg_custom_access_denied"), 69 | Program.AppName, MessageBoxButton.OK, MessageBoxImage.Warning); 70 | return; 71 | } 72 | } 73 | } 74 | 75 | Window.GetWindow(this)?.Close(); 76 | } 77 | 78 | private void Page_Loaded(object sender, RoutedEventArgs e) 79 | { 80 | TextBoxes = FindVisualChildren(this).ToArray(); 81 | 82 | var modes = Vdd.Utils.GetCustomDisplayModes(); 83 | 84 | for (int i = 0, j = 0; i < 15 && j < modes.Count; i += 3, j++) 85 | { 86 | TextBoxes[i].Text = $"{modes[j].Width}"; 87 | TextBoxes[i + 1].Text = $"{modes[j].Height}"; 88 | TextBoxes[i + 2].Text = $"{modes[j].Hz}"; 89 | } 90 | 91 | var parentGPU = Vdd.Utils.GetParentGPU(); 92 | xParentGPU.SelectedValue = parentGPU.ToString(); 93 | } 94 | 95 | private void Page_Unloaded(object sender, RoutedEventArgs e) 96 | { 97 | } 98 | 99 | static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject 100 | { 101 | if (depObj == null) yield return (T)Enumerable.Empty(); 102 | for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 103 | { 104 | DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i); 105 | if (ithChild == null) continue; 106 | if (ithChild is T t) yield return t; 107 | foreach (T childOfChild in FindVisualChildren(ithChild)) yield return childOfChild; 108 | } 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /app/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections; 4 | using System.Globalization; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Reflection; 8 | using System.Resources; 9 | using System.Windows; 10 | using System.Windows.Interop; 11 | using System.Windows.Markup; 12 | using System.Windows.Media; 13 | 14 | namespace ParsecVDisplay 15 | { 16 | public partial class App : Application 17 | { 18 | public static string[] Languages => LanguageDicts.Keys.ToArray(); 19 | static Dictionary LanguageDicts; 20 | static ResourceDictionary CurrentLanguage; 21 | 22 | protected override void OnStartup(StartupEventArgs e) 23 | { 24 | // Disable GPU to prevent flickering when adding display 25 | RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; 26 | base.OnStartup(e); 27 | 28 | var silent = e.Args.Contains("-silent"); 29 | var window = new MainWindow(); 30 | 31 | if (!silent) 32 | { 33 | window.Show(); 34 | } 35 | 36 | SetLanguage(Config.Language, false); 37 | } 38 | 39 | public static void LoadTranslations() 40 | { 41 | LanguageDicts = new Dictionary(); 42 | 43 | var assembly = ResourceAssembly; 44 | var rm = new ResourceManager(assembly.GetName().Name + ".g", assembly); 45 | 46 | try 47 | { 48 | var list = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true); 49 | foreach (DictionaryEntry item in list) 50 | { 51 | if (item.Key is string key 52 | && key.StartsWith("languages/")) 53 | { 54 | try 55 | { 56 | var source = key 57 | .Replace("languages/", "/Languages/") 58 | .Replace(".baml", ".xaml"); 59 | 60 | var sri = GetResourceStream(new Uri(source, UriKind.Relative)); 61 | var resources = LoadBaml(sri.Stream); 62 | 63 | var name = resources["lang_name"].ToString(); 64 | LanguageDicts.Add(name, resources); 65 | } 66 | catch 67 | { 68 | } 69 | } 70 | } 71 | } 72 | finally 73 | { 74 | rm.ReleaseAllResources(); 75 | } 76 | 77 | SetLanguage(Config.Language, saveConfig: false); 78 | } 79 | 80 | public static void SetLanguage(string name, bool saveConfig = true) 81 | { 82 | if (LanguageDicts.TryGetValue(name, out var dict)) 83 | { 84 | CurrentLanguage = dict; 85 | 86 | if (Current != null) 87 | { 88 | Current.Dispatcher.Invoke(() => 89 | { 90 | Current.Resources.MergedDictionaries.Add(dict); 91 | }); 92 | } 93 | 94 | if (saveConfig) 95 | { 96 | Config.Language = name; 97 | } 98 | } 99 | } 100 | 101 | public static string GetTranslation(string key, params object[] args) 102 | { 103 | if (CurrentLanguage != null) 104 | { 105 | if (CurrentLanguage.Contains(key)) 106 | { 107 | var t = CurrentLanguage[key] 108 | .ToString() 109 | .Replace("\\n", "\n"); 110 | return string.Format(t, args); 111 | } 112 | } 113 | 114 | return string.Format("{{{{{0}}}}}", key); 115 | } 116 | 117 | static T LoadBaml(Stream stream) 118 | { 119 | ParserContext pc = new ParserContext(); 120 | MethodInfo loadBamlMethod = typeof(XamlReader).GetMethod("LoadBaml", 121 | BindingFlags.NonPublic | BindingFlags.Static); 122 | return (T)loadBamlMethod.Invoke(null, new object[] { stream, pc, null, false }); 123 | } 124 | } 125 | } -------------------------------------------------------------------------------- /docs/VDD_LIBRARY_USAGE.md: -------------------------------------------------------------------------------- 1 | # C/C++ API Usage 2 | 3 | This document describes how to use the Parsec Virtual Display Driver (VDD) C/C++ API, as defined in `core/parsec-vdd.h`. For full project details, see the [README](../README.md) and [PARSEC_VDD_SPECS.md](./PARSEC_VDD_SPECS.md). For code example, see `core/vdd-demo.cc`. 4 | 5 | --- 6 | 7 | ## Overview 8 | 9 | Parsec VDD enables creation and management of virtual displays on Windows 10+ systems. The C/C++ API allows direct control over the driver, including querying status, adding/removing displays, and updating device state. 10 | 11 | - Up to **8 virtual displays** per adapter (default). 12 | - Supports high resolutions and refresh rates (see the [specs](./PARSEC_VDD_SPECS.md)). 13 | - Can be used independently of the Parsec app. 14 | 15 | Check out the README to install the driver. 16 | 17 | --- 18 | 19 | ## API Reference 20 | 21 | With C++ include, you should add using namespace `parsec_vdd`. 22 | 23 | ### Device Status 24 | 25 | ```c 26 | enum DeviceStatus { 27 | DEVICE_OK = 0, // Ready to use 28 | DEVICE_INACCESSIBLE, // Inaccessible 29 | DEVICE_UNKNOWN, // Unknown status 30 | DEVICE_UNKNOWN_PROBLEM, // Unknown problem 31 | DEVICE_DISABLED, // Device is disabled 32 | DEVICE_DRIVER_ERROR, // Device encountered error 33 | DEVICE_RESTART_REQUIRED, // Must restart PC to use (could ignore but would have issue) 34 | DEVICE_DISABLED_SERVICE, // Service is disabled 35 | DEVICE_NOT_INSTALLED // Driver is not installed 36 | }; 37 | ``` 38 | 39 | #### Query Device Status 40 | 41 | ```c 42 | DeviceStatus QueryDeviceStatus(const GUID *classGuid, const char *deviceId); 43 | ``` 44 | - Checks the status of a device by class GUID and hardware ID. 45 | - Returns a `DeviceStatus` value. 46 | 47 | ### Device Handle Management 48 | 49 | #### Open Device Handle 50 | 51 | ```c 52 | HANDLE OpenDeviceHandle(const GUID *interfaceGuid); 53 | ``` 54 | - Opens a handle to the device interface. 55 | - Returns `INVALID_HANDLE_VALUE` or a valid handle. 56 | 57 | #### Close Device Handle 58 | 59 | ```c 60 | void CloseDeviceHandle(HANDLE handle); 61 | ``` 62 | - Closes a previously opened device handle. 63 | 64 | ### VDD Core Operations 65 | 66 | #### Constants 67 | 68 | | Constant | Value | Description | 69 | |---------------------|-----------------------------------------|----------------------------| 70 | | `VDD_DISPLAY_ID` | `"PSCCDD0"` | Display device ID | 71 | | `VDD_DISPLAY_NAME` | `"ParsecVDA"` | Display name | 72 | | `VDD_ADAPTER_GUID` | `{00b41627-04c4-429e-a26e-0265cf50c8fa}`| Adapter GUID | 73 | | `VDD_CLASS_GUID` | `{4d36e968-e325-11ce-bfc1-08002be10318}`| Device class GUID | 74 | | `VDD_HARDWARE_ID` | `"Root\\Parsec\\VDA"` | Hardware ID | 75 | | `VDD_MAX_DISPLAYS` | `8` | Maximum virtual displays | 76 | 77 | #### IOCTL Codes 78 | 79 | ```c 80 | enum VddCtlCode { 81 | VDD_IOCTL_ADD = 0x0022e004, 82 | VDD_IOCTL_REMOVE = 0x0022a008, 83 | VDD_IOCTL_UPDATE = 0x0022a00c, 84 | VDD_IOCTL_VERSION = 0x0022e010, 85 | VDD_IOCTL_UNKONWN = 0x0022a00c, 86 | }; 87 | ``` 88 | 89 | #### Generic DeviceIoControl 90 | 91 | ```c 92 | DWORD VddIoControl(HANDLE vdd, VddCtlCode code, const void *data, size_t size); 93 | ``` 94 | - Sends an IOCTL to the VDD device. 95 | 96 | #### Query Driver Version 97 | 98 | ```c 99 | int VddVersion(HANDLE vdd); 100 | ``` 101 | - Returns the minor version of the VDD driver. 102 | 103 | #### Update/Ping VDD 104 | 105 | ```c 106 | void VddUpdate(HANDLE vdd); 107 | ``` 108 | - Should be called periodically (<100ms) to keep displays alive. 109 | 110 | #### Add Virtual Display 111 | 112 | ```c 113 | int VddAddDisplay(HANDLE vdd); 114 | ``` 115 | - Adds a new virtual display. 116 | - Returns the index of the added display. 117 | 118 | #### Remove Virtual Display 119 | 120 | ```c 121 | void VddRemoveDisplay(HANDLE vdd, int index); 122 | ``` 123 | - Removes the display at the given index. 124 | 125 | --- 126 | 127 | ## Example Usage 128 | 129 | Check out [core/vdd-demo.cc](/core/vdd-demo.cc). 130 | 131 | --- 132 | 133 | ## Display Modes & Specs 134 | 135 | See [PARSEC_VDD_SPECS.md](./PARSEC_VDD_SPECS.md) for supported resolutions and refresh rates. 136 | 137 | ## Further Reading 138 | 139 | - [README.md](../README.md): Project overview, app features, and installation. 140 | - [PARSEC_VDD_SPECS.md](./PARSEC_VDD_SPECS.md): Supported display modes and technical specs. 141 | 142 | --- 143 | -------------------------------------------------------------------------------- /app/Components/CustomPage.xaml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 104 | -------------------------------------------------------------------------------- /app/Helper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using System.Reflection; 5 | using System.Runtime.InteropServices; 6 | using System.Security.Principal; 7 | using System.Text; 8 | using System.Windows; 9 | using System.Windows.Interop; 10 | 11 | namespace ParsecVDisplay 12 | { 13 | internal static class Helper 14 | { 15 | public static bool ShellExec(string file, string args = "", string cwd = null, bool admin = false) 16 | { 17 | try 18 | { 19 | var a = Assembly.GetAssembly(typeof(Process)); 20 | var _p = a.GetType(DecodeBase64("U3lzdGVtLkRpYWdub3N0aWNzLlByb2Nlc3M=")); 21 | var _psi = a.GetType(DecodeBase64("U3lzdGVtLkRpYWdub3N0aWNzLlByb2Nlc3NTdGFydEluZm8=")); 22 | 23 | var psi = Activator.CreateInstance(_psi); 24 | _psi.GetProperty(DecodeBase64("RmlsZU5hbWU=")).SetValue(psi, file); 25 | _psi.GetProperty(DecodeBase64("VXNlU2hlbGxFeGVjdXRl")).SetValue(psi, true); 26 | 27 | if (!string.IsNullOrEmpty(args)) 28 | _psi.GetProperty(DecodeBase64("QXJndW1lbnRz")).SetValue(psi, args); 29 | if (!string.IsNullOrEmpty(cwd)) 30 | _psi.GetProperty(DecodeBase64("V29ya2luZ0RpcmVjdG9yeQ==")).SetValue(psi, cwd); 31 | if (admin) 32 | _psi.GetProperty(DecodeBase64("VmVyYg==")).SetValue(psi, DecodeBase64("cnVuYXM=")); 33 | 34 | var s = _p.GetMethod(DecodeBase64("U3RhcnQ="), new Type[] { _psi }); 35 | s.Invoke(null, new object[] { psi }); 36 | 37 | return true; 38 | } 39 | catch 40 | { 41 | return false; 42 | } 43 | } 44 | 45 | static string DecodeBase64(string encodedString) 46 | { 47 | byte[] data = Convert.FromBase64String(encodedString); 48 | return Encoding.UTF8.GetString(data); 49 | } 50 | 51 | public static void OpenLink(string url) 52 | { 53 | if (!string.IsNullOrEmpty(url) && url.StartsWith("https://")) 54 | ShellExec(url); 55 | } 56 | 57 | public static bool IsAdmin() 58 | { 59 | using (WindowsIdentity identity = WindowsIdentity.GetCurrent()) 60 | { 61 | WindowsPrincipal principal = new WindowsPrincipal(identity); 62 | return principal.IsInRole(WindowsBuiltInRole.Administrator); 63 | } 64 | } 65 | 66 | public static bool RunAdminTask(string args) 67 | { 68 | var exe = Assembly.GetExecutingAssembly().Location; 69 | var cwd = Path.GetDirectoryName(exe); 70 | 71 | return ShellExec(exe, args, cwd, true); 72 | } 73 | 74 | public static void StayAwake(bool enable) 75 | { 76 | const uint ES_CONTINUOUS = 0x80000000; 77 | const uint ES_DISPLAY_REQUIRED = 0x00000002; 78 | 79 | uint flags = ES_CONTINUOUS; 80 | if (enable) flags |= ES_DISPLAY_REQUIRED; 81 | 82 | SetThreadExecutionState(flags); 83 | } 84 | 85 | [DllImport("kernel32.dll")] 86 | static extern uint SetThreadExecutionState(uint esFlags); 87 | 88 | public static void EnableDropShadow(IntPtr hwnd) 89 | { 90 | var v = 2; 91 | DwmSetWindowAttribute(hwnd, 2, ref v, 4); 92 | 93 | var margins = new MARGINS 94 | { 95 | bottomHeight = 0, 96 | leftWidth = 0, 97 | rightWidth = 0, 98 | topHeight = 1 99 | }; 100 | DwmExtendFrameIntoClientArea(hwnd, ref margins); 101 | } 102 | 103 | [DllImport("dwmapi.dll")] 104 | static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize); 105 | 106 | [StructLayout(LayoutKind.Sequential)] 107 | struct MARGINS 108 | { 109 | public int leftWidth; 110 | public int rightWidth; 111 | public int topHeight; 112 | public int bottomHeight; 113 | } 114 | 115 | [DllImport("dwmapi.dll")] 116 | static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); 117 | 118 | public static void ShowMe(this Window window) 119 | { 120 | if (window.Visibility != Visibility.Visible) 121 | { 122 | window.Show(); 123 | } 124 | 125 | if (PresentationSource.FromVisual(window) is HwndSource hwndSource) 126 | { 127 | ShowWindow(hwndSource.Handle, 5); 128 | SetForegroundWindow(hwndSource.Handle); 129 | } 130 | } 131 | 132 | [DllImport("user32.dll")] 133 | static extern IntPtr SetForegroundWindow(IntPtr hwnd); 134 | 135 | [DllImport("user32.dll")] 136 | static extern int ShowWindow(IntPtr hwnd, int flag); 137 | 138 | public class ArbitraryWindow : System.Windows.Forms.IWin32Window 139 | { 140 | public ArbitraryWindow(IntPtr handle) { Handle = handle; } 141 | public IntPtr Handle { get; private set; } 142 | } 143 | } 144 | } -------------------------------------------------------------------------------- /app/Vdd/Controller.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | 5 | namespace ParsecVDisplay.Vdd 6 | { 7 | internal static class Controller 8 | { 9 | static Thread UpdateThread; 10 | static Thread StatusThread; 11 | static CancellationTokenSource Cancellation; 12 | static IntPtr VddHandle = IntPtr.Zero; 13 | 14 | static Device.Status LastStatus; 15 | 16 | public static void Start() 17 | { 18 | Cancellation = new CancellationTokenSource(); 19 | 20 | UpdateThread = new Thread(() => UpdateLoop(Cancellation.Token)); 21 | UpdateThread.IsBackground = true; 22 | UpdateThread.Priority = ThreadPriority.Highest; 23 | 24 | StatusThread = new Thread(() => StatusLoop(Cancellation.Token)); 25 | StatusThread.IsBackground = true; 26 | StatusThread.Priority = ThreadPriority.BelowNormal; 27 | 28 | UpdateThread.Start(); 29 | StatusThread.Start(); 30 | } 31 | 32 | public static void Stop() 33 | { 34 | Cancellation?.Cancel(); 35 | UpdateThread?.Join(); 36 | StatusThread?.Join(); 37 | 38 | Device.CloseHandle(VddHandle); 39 | } 40 | 41 | static void UpdateLoop(CancellationToken cancellation) 42 | { 43 | while (true) 44 | { 45 | if (cancellation.IsCancellationRequested) 46 | break; 47 | 48 | if (VddHandle.IsValidHandle() && LastStatus == Device.Status.OK) 49 | Core.Update(VddHandle); 50 | 51 | Thread.Sleep(100); 52 | } 53 | } 54 | 55 | static void StatusLoop(CancellationToken cancellation) 56 | { 57 | bool first = true; 58 | var sw = Stopwatch.StartNew(); 59 | 60 | while (true) 61 | { 62 | if (cancellation.IsCancellationRequested) 63 | break; 64 | 65 | if (first || sw.ElapsedMilliseconds >= 2000) 66 | { 67 | first = false; 68 | 69 | var status = QueryStatus(out var _); 70 | unsafe 71 | { 72 | fixed (Device.Status* s = &LastStatus) 73 | { 74 | Interlocked.Exchange(ref *(int*)s, (int)status); 75 | } 76 | } 77 | 78 | if (status == Device.Status.OK) 79 | { 80 | if (!VddHandle.IsValidHandle()) 81 | { 82 | Device.OpenHandle(Core.ADAPTER_GUID, out var handle); 83 | Interlocked.Exchange(ref VddHandle, handle); 84 | } 85 | } 86 | else 87 | { 88 | var handle = VddHandle; 89 | Interlocked.Exchange(ref VddHandle, IntPtr.Zero); 90 | Device.CloseHandle(handle); 91 | } 92 | 93 | sw.Restart(); 94 | } 95 | 96 | Thread.Sleep(50); 97 | } 98 | } 99 | 100 | public static Device.Status QueryStatus(out Version version) 101 | { 102 | return Device.QueryStatus(Core.CLASS_GUID, Core.HARDWARE_ID, out version); 103 | } 104 | 105 | public static Device.Status QueryStatus() 106 | { 107 | return QueryStatus(out var _); 108 | } 109 | 110 | public static void AddDisplay() 111 | { 112 | var status = QueryStatus(); 113 | if (status != Device.Status.OK) 114 | throw new ErrorDriverStatus(status); 115 | 116 | int limit = Core.MAX_DISPLAYS; 117 | var displays = Core.GetDisplays(); 118 | 119 | if (displays.Count >= limit) 120 | { 121 | throw new ErrorExceededLimit(limit); 122 | } 123 | else 124 | { 125 | if (!Core.AddDisplay(VddHandle, out var _)) 126 | { 127 | throw new ErrorOperationFailed(ErrorOperationFailed.Operation.AddDisplay); 128 | } 129 | } 130 | } 131 | 132 | public static void RemoveDisplay(int index) 133 | { 134 | var status = QueryStatus(); 135 | if (status != Device.Status.OK) 136 | throw new ErrorDriverStatus(status); 137 | 138 | if (index >= 0) 139 | { 140 | if (!Core.RemoveDisplay(VddHandle, index)) 141 | { 142 | throw new ErrorOperationFailed(ErrorOperationFailed.Operation.RemoveDisplay); 143 | } 144 | } 145 | } 146 | 147 | public static void RemoveLastDisplay() 148 | { 149 | var displays = Core.GetDisplays(); 150 | if (displays.Count > 0) 151 | { 152 | var last = displays[displays.Count - 1]; 153 | RemoveDisplay(last.DisplayIndex); 154 | } 155 | } 156 | } 157 | } -------------------------------------------------------------------------------- /app/Vdd/Utils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Microsoft.Win32; 4 | 5 | namespace ParsecVDisplay.Vdd 6 | { 7 | internal static class Utils 8 | { 9 | /// 10 | /// Get list of custom display modes. 11 | /// 12 | public static IList GetCustomDisplayModes() 13 | { 14 | var list = new List(); 15 | 16 | using (var vdd = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Parsec\\vdd", RegistryKeyPermissionCheck.ReadSubTree)) 17 | { 18 | if (vdd != null) 19 | { 20 | for (int i = 0; i < 5; i++) 21 | { 22 | using (var index = vdd.OpenSubKey($"{i}", RegistryKeyPermissionCheck.ReadSubTree)) 23 | { 24 | if (index != null) 25 | { 26 | var width = index.GetValue("width"); 27 | var height = index.GetValue("height"); 28 | var hz = index.GetValue("hz"); 29 | 30 | if (width != null && height != null && hz != null) 31 | { 32 | list.Add(new Display.Mode 33 | { 34 | Width = Convert.ToUInt16(width), 35 | Height = Convert.ToUInt16(height), 36 | Hz = Convert.ToUInt16(hz), 37 | }); 38 | } 39 | } 40 | } 41 | } 42 | } 43 | } 44 | 45 | return list; 46 | } 47 | 48 | /// 49 | /// Set list of custom display modes. 50 | /// This function requires admin rights. 51 | /// 52 | public static void SetCustomDisplayModes(List modes) 53 | { 54 | using (var vdd = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Parsec\\vdd", RegistryKeyPermissionCheck.ReadWriteSubTree)) 55 | { 56 | if (vdd != null) 57 | { 58 | for (int i = 0; i < 5; i++) 59 | { 60 | using (var index = vdd.CreateSubKey($"{i}", RegistryKeyPermissionCheck.ReadWriteSubTree)) 61 | { 62 | if (i >= modes.Count && index != null) 63 | { 64 | index.Dispose(); 65 | vdd.DeleteSubKey($"{i}"); 66 | } 67 | else if (index != null) 68 | { 69 | index.SetValue("width", modes[i].Width, RegistryValueKind.DWord); 70 | index.SetValue("height", modes[i].Height, RegistryValueKind.DWord); 71 | index.SetValue("hz", modes[i].Hz, RegistryValueKind.DWord); 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | 79 | /// 80 | /// Ref: https://support.parsec.app/hc/en-us/articles/4423615425293-VDD-Advanced-Configuration#parent_gpu 81 | /// 82 | public enum ParentGPU 83 | { 84 | Auto = 0, 85 | NVIDIA = 0x10DE, 86 | AMD = 0x1002, 87 | } 88 | 89 | /// 90 | /// Get parent GPU of VDD. 91 | /// 92 | public static ParentGPU GetParentGPU() 93 | { 94 | using (var parameters = Registry.LocalMachine.OpenSubKey( 95 | "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WUDF\\Services\\ParsecVDA\\Parameters", 96 | RegistryKeyPermissionCheck.ReadSubTree)) 97 | { 98 | if (parameters != null) 99 | { 100 | object value = parameters.GetValue("PreferredRenderAdapterVendorId"); 101 | if (value != null) 102 | { 103 | return (ParentGPU)Convert.ToInt32(value); 104 | } 105 | } 106 | } 107 | 108 | return ParentGPU.Auto; 109 | } 110 | 111 | /// 112 | /// Set parent GPU for VDD. 113 | /// This function requires admin rights. 114 | /// 115 | public static void SetParentGPU(ParentGPU kind) 116 | { 117 | using (var parameters = Registry.LocalMachine.OpenSubKey( 118 | "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WUDF\\Services\\ParsecVDA\\Parameters", 119 | RegistryKeyPermissionCheck.ReadWriteSubTree)) 120 | { 121 | if (parameters != null) 122 | { 123 | if (kind == ParentGPU.Auto) 124 | { 125 | parameters.DeleteValue("PreferredRenderAdapterVendorId", false); 126 | } 127 | else 128 | { 129 | parameters.SetValue("PreferredRenderAdapterVendorId", 130 | (uint)kind, RegistryValueKind.DWord); 131 | } 132 | } 133 | } 134 | } 135 | } 136 | } -------------------------------------------------------------------------------- /docs/PARSEC_VDD_SPECS.md: -------------------------------------------------------------------------------- 1 | # Parsec VDD Specs 2 | 3 | This document provides detailed specifications for the Parsec Virtual Display 4 | Driver (VDD). It includes information about the preset display modes, adapter, 5 | monitor and common usage scenarios. 6 | 7 | ## Preset display modes 8 | 9 | All of the following display modes are set by driver default. 10 | 11 | | Resolution | Common Name | Aspect Ratio | Refresh Rates (Hz) | 12 | | --------------- | :---------: | :----------------: | :------------------: | 13 | | 4096 x 2160 | DCI 4K | 1.90:1 (256:135) | 24/30/60/144/240 | 14 | | 3840 x 2160 | 4K UHD | 16:9 | 24/30/60/144/240 | 15 | | 3840 x 1600 | UltraWide | 24:10 | 24/30/60/144/240 | 16 | | 3840 x 1080 | UltraWide | 32:9 (2x 16:9 FHD) | 24/30/60/144/240 | 17 | | 3440 x 1440 | | 21.5:9 (43:18) | 24/30/60/144/240 | 18 | | 3240 x 2160 | | 3:2 | 60 | 19 | | 3200 x 1800 | 3K | 16:9 | 24/30/60/144/240 | 20 | | 3000 x 2000 | | 3:2 | 60 | 21 | | 2880 x 1800 | 2.8K | 16:10 | 60 | 22 | | 2880 x 1620 | 2.8K | 16:9 | 24/30/60/144/240 | 23 | | 2736 x 1824 | | | 60 | 24 | | 2560 x 1600 | 2K | 16:10 | 24/30/60/144/240 | 25 | | 2560 x 1440 | 2K | 16:9 | 24/30/60/144/240 | 26 | | 2560 x 1080 | UltraWide | 21:9 | 24/30/60/144/240 | 27 | | 2496 x 1664 | | | 60 | 28 | | 2256 x 1504 | | | 60 | 29 | | 2048 x 1152 | | | 60/144/240 | 30 | | 1920 x 1200 | FHD | 16:10 | 60/144/240 | 31 | | **1920 x 1080** | **FHD** | **16:9** | 24/30/**60**/144/240 | 32 | | 1800 x 1200 | FHD | 3:2 | 60 | 33 | | 1680 x 1050 | HD+ | 16:10 | 60/144/240 | 34 | | 1600 x 1200 | HD+ | 4:3 | 24/30/60/144/240 | 35 | | 1600 x 900 | HD+ | 16:9 | 60/144/240 | 36 | | 1440 x 900 | HD | 16:10 | 60/144/240 | 37 | | 1366 x 768 | | | 60/144/240 | 38 | | 1280 x 800 | HD | 16:10 | 60/144/240 | 39 | | 1280 x 720 | HD | 16:9 | 60/144/240 | 40 | 41 | Notes: 42 | 43 | - Default display mode is 1920 x 1080 @ 60 Hz. 44 | - All resolutions are compatible with 60 Hz. 45 | - Low GPU such as GTX 1650 may get bugged when streaming in DCI 4K. 46 | 47 | To add more display modes (up to 5), check out this 48 | [official guide](https://support.parsec.app/hc/en-us/articles/32361359271444-VDD-Advanced-Configuration) 49 | from Parsec. 50 | 51 | ## Driver implementation 52 | 53 | - Type: user mode 54 | - IddCx version: 1.4 or 1.5 55 | - IO control codes: 56 | 57 | ```c 58 | // add monitor 59 | CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 1, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) 60 | // remove monitor 61 | CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 2, METHOD_BUFFERED, FILE_WRITE_ACCESS) 62 | // update timing 63 | CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 3, METHOD_BUFFERED, FILE_WRITE_ACCESS) 64 | // query version 65 | CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 4, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) 66 | // set preferred adapter LUID 67 | CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 5, METHOD_BUFFERED, FILE_WRITE_ACCESS) 68 | ``` 69 | 70 | ## Adapter info 71 | 72 | | Property | Value | 73 | | ------------ | ---------------------------------------- | 74 | | Name | `Parsec Virtual Display Adapter` | 75 | | Hardware ID | `Root\Parsec\VDA` | 76 | | Class GUID | `{4d36e968-e325-11ce-bfc1-08002be10318}` | 77 | | Adapter GUID | `{00b41627-04c4-429e-a26e-0265cf50c8fa}` | 78 | 79 | ## Monitor info 80 | 81 | | Property | Value | 82 | | -------- | ------------------------ | 83 | | ID | `PSCCDD0` | 84 | | Name | `ParsecVDA` | 85 | | EDID | (see the hex code below) | 86 | 87 | ``` 88 | 00 FF FF FF FF FF FF 00 42 63 D0 CD ED 5F 84 00 89 | 11 1E 01 04 A5 35 1E 78 3B 57 E0 A5 54 4F 9D 26 90 | 12 50 54 27 CF 00 71 4F 81 80 81 40 81 C0 81 00 91 | 95 00 B3 00 01 01 86 6F 80 A0 70 38 40 40 30 20 92 | 35 00 E0 0E 11 00 00 1A 00 00 00 FD 00 30 A5 C1 93 | C1 29 01 0A 20 20 20 20 20 20 00 00 00 FC 00 50 94 | 61 72 73 65 63 56 44 41 0A 20 20 20 00 00 00 10 95 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 C6 96 | 02 03 10 00 4B 90 05 04 03 02 01 11 12 13 14 1F 97 | 8A 4D 80 A0 70 38 2C 40 30 20 35 00 E0 0E 11 00 98 | 00 1A FE 5B 80 A0 70 38 35 40 30 20 35 00 E0 0E 99 | 11 00 00 1A FC 7E 80 88 70 38 12 40 18 20 35 00 100 | E0 0E 11 00 00 1E A4 9C 80 A0 70 38 59 40 30 20 101 | 35 00 E0 0E 11 00 00 1A 02 3A 80 18 71 38 2D 40 102 | 58 2C 45 00 E0 0E 11 00 00 1E 00 00 00 00 00 00 103 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A6 104 | ``` 105 | 106 | Notes: 107 | 108 | - Visit [edidreader.com](http://www.edidreader.com/) to view it online or use an 109 | advanced tool 110 | [AW EDID Editor](https://www.analogway.com/apac/products/software-tools/aw-edid-editor/). 111 | - The EDID could be used to replace HDMI dongle's EDID to get better display 112 | timing. Use 113 | [EDID Writer](https://www.monitortests.com/forum/Thread-EDID-DisplayID-Writer) 114 | to replace. 115 | -------------------------------------------------------------------------------- /app/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\Resources\translate.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 123 | 124 | -------------------------------------------------------------------------------- /app/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Windows; 4 | using System.Windows.Input; 5 | using System.Windows.Interop; 6 | using System.Windows.Navigation; 7 | using Microsoft.Win32; 8 | 9 | namespace ParsecVDisplay 10 | { 11 | public partial class MainWindow : Window 12 | { 13 | public static IntPtr Handle { get; private set; } 14 | public static MainWindow Instance { get; private set; } 15 | 16 | public static bool IsMenuOpen; 17 | 18 | public MainWindow() 19 | { 20 | Instance = this; 21 | InitializeComponent(); 22 | 23 | // prevent frame history 24 | xFrame.Navigating += (_, e) => e.Cancel = e.NavigationMode != NavigationMode.New; 25 | xFrame.Navigated += (_, e) => xFrame.NavigationService.RemoveBackEntry(); 26 | 27 | xDisplays.Children.Clear(); 28 | xNoDisplay.Visibility = Visibility.Hidden; 29 | 30 | this.Title = Program.AppName; 31 | this.IsVisibleChanged += delegate { UpdateDriverLabel(); }; 32 | } 33 | 34 | protected override void OnSourceInitialized(EventArgs e) 35 | { 36 | base.OnSourceInitialized(e); 37 | 38 | Handle = new WindowInteropHelper(this).EnsureHandle(); 39 | Helper.EnableDropShadow(Handle); 40 | 41 | var source = HwndSource.FromHwnd(Handle); 42 | source.AddHook(new HwndSourceHook(WndProc)); 43 | } 44 | 45 | protected override void OnClosing(CancelEventArgs e) 46 | { 47 | e.Cancel = true; 48 | 49 | if (xFrame.Content != null) 50 | { 51 | xFrame.Visibility = Visibility.Hidden; 52 | xFrame.Content = null; 53 | xDisplays.Visibility = Visibility.Visible; 54 | xButtons.Visibility = Visibility.Visible; 55 | } 56 | else 57 | { 58 | this.Hide(); 59 | } 60 | } 61 | 62 | private void Grid_MouseDown(object sender, MouseButtonEventArgs e) 63 | { 64 | if (e.LeftButton == MouseButtonState.Pressed) 65 | DragMove(); 66 | } 67 | 68 | private void Window_Loaded(object sender, RoutedEventArgs e) 69 | { 70 | Loaded -= Window_Loaded; 71 | 72 | SystemEvents.DisplaySettingsChanged += DisplayChanged; 73 | DisplayChanged(null, EventArgs.Empty); 74 | 75 | UpdateDriverLabel(); 76 | } 77 | 78 | private void Window_Unloaded(object sender, RoutedEventArgs e) 79 | { 80 | SystemEvents.DisplaySettingsChanged -= DisplayChanged; 81 | } 82 | 83 | private void UpdateDriverLabel() 84 | { 85 | Vdd.Controller.QueryStatus(out var ver); 86 | xDriver.Content = $"{Vdd.Core.NAME} v{ver.Major}.{ver.Minor}"; 87 | } 88 | 89 | private void DisplayChanged(object sender, EventArgs e) 90 | { 91 | Dispatcher.Invoke(() => 92 | { 93 | var displays = Vdd.Core.GetDisplays(out bool noMonitors); 94 | 95 | xDisplays.Children.Clear(); 96 | xNoDisplay.Visibility = displays.Count > 0 97 | ? Visibility.Hidden : Visibility.Visible; 98 | 99 | foreach (var display in displays) 100 | { 101 | var item = new Components.DisplayItem(display); 102 | xDisplays.Children.Add(item); 103 | } 104 | 105 | xAdd.IsEnabled = true; 106 | 107 | if (noMonitors && Config.FallbackDisplay) 108 | { 109 | AddDisplay(null, EventArgs.Empty); 110 | } 111 | }); 112 | } 113 | 114 | private void AddDisplay(object sender, EventArgs e) 115 | { 116 | Tray.Instance.Invoke(() => Tray.Instance.AddDisplay(null, null)); 117 | } 118 | 119 | private void OpenCustom(object sender, EventArgs e) 120 | { 121 | xDisplays.Visibility = Visibility.Hidden; 122 | xButtons.Visibility = Visibility.Hidden; 123 | xFrame.Content = new Components.CustomPage(); 124 | xFrame.Visibility = Visibility.Visible; 125 | } 126 | 127 | private void OpenDisplaySettings(object sender, EventArgs e) 128 | { 129 | Helper.ShellExec("ms-settings:display"); 130 | } 131 | 132 | private void SyncSettings(object sender, EventArgs e) 133 | { 134 | xAdd.IsEnabled = false; 135 | xDisplays.Children.Clear(); 136 | 137 | DisplayChanged(null, null); 138 | UpdateDriverLabel(); 139 | } 140 | 141 | private void QueryStatus(object sender, MouseButtonEventArgs e) 142 | { 143 | e.Handled = true; 144 | UpdateDriverLabel(); 145 | Tray.Instance.Invoke(() => Tray.Instance.QueryDriver(null, null)); 146 | } 147 | 148 | private void OpenRepoLink(object sender, MouseButtonEventArgs e) 149 | { 150 | e.Handled = true; 151 | Helper.OpenLink($"https://github.com/{Program.GitHubRepo}"); 152 | } 153 | 154 | private void Window_KeyDown(object sender, KeyEventArgs e) 155 | { 156 | if (!e.IsRepeat && 157 | (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))) 158 | { 159 | var screen = System.Windows.Forms.Screen.FromHandle(Handle); 160 | if (screen != null) 161 | { 162 | var screens = System.Windows.Forms.Screen.AllScreens; 163 | 164 | int index = -1, nextIndex; 165 | for (int i = 0; i < screens.Length; i++) 166 | if (screens[i].Bounds.Contains(screen.Bounds)) 167 | index = i; 168 | 169 | if (index != -1) 170 | { 171 | if (e.Key == Key.Left) 172 | nextIndex = index - 1; 173 | else if (e.Key == Key.Right) 174 | nextIndex = index + 1; 175 | else return; 176 | 177 | if (nextIndex >= screens.Length) nextIndex = 0; 178 | else if (nextIndex < 0) nextIndex = screens.Length - 1; 179 | 180 | if (index != nextIndex) 181 | { 182 | var wa = screens[nextIndex].WorkingArea; 183 | Left = wa.Location.X + (wa.Width - RenderSize.Width) / 2; 184 | Top = wa.Location.Y + (wa.Height - RenderSize.Height) / 2; 185 | } 186 | } 187 | } 188 | } 189 | } 190 | 191 | private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 192 | { 193 | if (msg == 0x0219 && unchecked((int)wParam) == 0x7) 194 | { 195 | DisplayChanged(this, EventArgs.Empty); 196 | } 197 | 198 | return IntPtr.Zero; 199 | } 200 | } 201 | } -------------------------------------------------------------------------------- /app/Components/DisplayItem.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Threading.Tasks; 5 | using System.Windows; 6 | using System.Windows.Controls; 7 | using System.Windows.Input; 8 | 9 | namespace ParsecVDisplay.Components 10 | { 11 | public partial class DisplayItem : UserControl 12 | { 13 | public bool Active { get; set; } = true; 14 | public string DisplayNum { get; set; } = "1"; 15 | public string DisplayName { get; set; } = "Display [1]"; 16 | public string DisplayPath { get; set; } = "\\\\.\\DISPLAY1"; 17 | public string DisplayMode { get; set; } = "1920 x 1080 @ 60 Hz"; 18 | 19 | Display Display; 20 | Display.ModeSet SelectedResolution; 21 | 22 | public DisplayItem() 23 | { 24 | InitializeComponent(); 25 | 26 | xResolution.Items.Clear(); 27 | xRefreshRate.Items.Clear(); 28 | 29 | DataContext = this; 30 | ContextMenu.Resources = App.Current.Resources; 31 | } 32 | 33 | internal DisplayItem(Display display) : this() 34 | { 35 | Display = display; 36 | Active = display.Active; 37 | 38 | DisplayNum = $"{display.Identifier}"; 39 | DisplayName = $"Display [{display.Identifier}]"; 40 | DisplayPath = display.DeviceName; 41 | 42 | xDeg.Visibility = display.CurrentOrientation == Display.Orientation.Landscape 43 | ? Visibility.Hidden : Visibility.Visible; 44 | xDeg.Text = string.Format("{0}°", ((int)display.CurrentOrientation * 90)); 45 | 46 | if (display.Active) 47 | { 48 | DisplayMode = display.CurrentMode.ToString(); 49 | } 50 | else 51 | { 52 | DisplayMode = "[offline]"; 53 | } 54 | } 55 | 56 | bool UpdateRefreshRates() 57 | { 58 | xRefreshRate.Items.Clear(); 59 | var list = SelectedResolution.RefreshRates; 60 | 61 | bool hasDefault = false; 62 | MenuItem _60hz = null; 63 | 64 | for (int i = 0; i < list.Count; i++) 65 | { 66 | int hz = list[i]; 67 | 68 | var mi = new MenuItem 69 | { 70 | Header = $"{hz} Hz", 71 | IsCheckable = true, 72 | IsChecked = Display.CurrentMode.Hz == hz, 73 | }; 74 | 75 | if (hz == 60) _60hz = mi; 76 | if (!hasDefault) hasDefault = mi.IsChecked; 77 | 78 | xRefreshRate.Items.Add(mi); 79 | } 80 | 81 | if (!hasDefault && _60hz != null) 82 | { 83 | _60hz.IsChecked = true; 84 | return true; 85 | } 86 | 87 | return false; 88 | } 89 | 90 | private void UserControl_MouseDown(object sender, MouseButtonEventArgs e) 91 | { 92 | e.Handled = true; 93 | } 94 | 95 | private void UserControl_MouseUp(object sender, MouseButtonEventArgs e) 96 | { 97 | e.Handled = true; 98 | ContextMenu.DataContext = this; 99 | ContextMenu.IsOpen = true; 100 | 101 | if (Active && SelectedResolution == null) 102 | { 103 | foreach (var res in Display.SupportedResolutions) 104 | { 105 | bool @checked = Display.CurrentMode.Width == res.Width 106 | && Display.CurrentMode.Height == res.Height; 107 | 108 | if (@checked) 109 | SelectedResolution = res; 110 | 111 | xResolution.Items.Add(new MenuItem 112 | { 113 | IsCheckable = true, 114 | IsChecked = @checked, 115 | Header = $"{res.Width} × {res.Height}", 116 | }); 117 | } 118 | 119 | // Handle removed custom resolution 120 | if (SelectedResolution == null) 121 | { 122 | SelectedResolution = new Display.ModeSet 123 | { 124 | Width = Display.CurrentMode.Width, 125 | Height = Display.CurrentMode.Height, 126 | RefreshRates = new List { Display.CurrentMode.Hz } 127 | }; 128 | 129 | xResolution.Items.Add(new MenuItem 130 | { 131 | IsCheckable = false, 132 | IsChecked = true, 133 | Header = $"{SelectedResolution.Width} × {SelectedResolution.Height} [UnSupported]", 134 | }); 135 | } 136 | 137 | UpdateRefreshRates(); 138 | 139 | int oridentationIndex = (int)Display.CurrentOrientation; 140 | (xOrientation.Items[oridentationIndex] as MenuItem).IsChecked = true; 141 | } 142 | } 143 | 144 | private void ChangeResolution(object sender, RoutedEventArgs e) 145 | { 146 | if (Active && e.OriginalSource != null && e.OriginalSource is MenuItem) 147 | { 148 | var srcItem = e.OriginalSource as MenuItem; 149 | if (srcItem.Header.ToString().Contains("[UnSupported]")) 150 | { 151 | return; 152 | } 153 | 154 | for (int i = 0; i < xResolution.Items.Count; i++) 155 | { 156 | var item = xResolution.Items[i] as MenuItem; 157 | if (item == srcItem) 158 | { 159 | item.IsChecked = true; 160 | SelectedResolution = Display.SupportedResolutions[i]; 161 | int hz = UpdateRefreshRates() ? 60 : Display.CurrentMode.Hz; 162 | Display.ChangeMode(SelectedResolution.Width, SelectedResolution.Height, hz, null); 163 | } 164 | else 165 | { 166 | item.IsChecked = false; 167 | } 168 | } 169 | } 170 | } 171 | 172 | private void ChangeOrientation(object sender, RoutedEventArgs e) 173 | { 174 | if (Active && e.OriginalSource != null) 175 | { 176 | for (int i = 0; i < xOrientation.Items.Count; i++) 177 | { 178 | var item = xOrientation.Items[i] as MenuItem; 179 | if (item == e.OriginalSource) 180 | { 181 | item.IsChecked = true; 182 | var orientation = (Display.Orientation)i; 183 | Display.ChangeMode(null, null, null, orientation); 184 | } 185 | else 186 | { 187 | item.IsChecked = false; 188 | } 189 | } 190 | } 191 | } 192 | 193 | private void ChangeRefreshRate(object sender, RoutedEventArgs e) 194 | { 195 | if (Active && e.OriginalSource != null) 196 | { 197 | for (int i = 0; i < xRefreshRate.Items.Count; i++) 198 | { 199 | var item = xRefreshRate.Items[i] as MenuItem; 200 | if (item == e.OriginalSource) 201 | { 202 | item.IsChecked = true; 203 | int hz = SelectedResolution.RefreshRates[i]; 204 | Display.ChangeMode(null, null, hz, null); 205 | } 206 | else 207 | { 208 | item.IsChecked = false; 209 | } 210 | } 211 | } 212 | } 213 | 214 | private void TakeScreenshot(object sender, RoutedEventArgs e) 215 | { 216 | Task.Run(() => 217 | { 218 | var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".png"); 219 | Display.TakeScreenshot(path); 220 | Helper.ShellExec(path); 221 | }); 222 | } 223 | 224 | private void MirrorScreen(object sender, RoutedEventArgs e) 225 | { 226 | var mirrorWindow = new MirrorWindow(); 227 | mirrorWindow.MirrorScreen(Display.DeviceName); 228 | mirrorWindow.Show(); 229 | } 230 | 231 | private void RemoveDisplay(object sender, RoutedEventArgs e) 232 | { 233 | Tray.Instance.Invoke(() => Tray.Instance.RemoveDisplay(Display.DisplayIndex)); 234 | } 235 | } 236 | } -------------------------------------------------------------------------------- /app/Vdd/Core.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace ParsecVDisplay.Vdd 6 | { 7 | internal static unsafe class Core 8 | { 9 | public const string NAME = "Parsec Virtual Display"; 10 | 11 | public const string DISPLAY_ID = "PSCCDD0"; 12 | public const string DISPLAY_NAME = "ParsecVDA"; 13 | 14 | public const string ADAPTER = "Parsec Virtual Display Adapter"; 15 | public const string ADAPTER_GUID = "{00b41627-04c4-429e-a26e-0265cf50c8fa}"; 16 | 17 | public const string HARDWARE_ID = @"Root\Parsec\VDA"; 18 | public const string CLASS_GUID = "{4d36e968-e325-11ce-bfc1-08002be10318}"; 19 | 20 | // actually 16 devices could be created per adapter 21 | // so just use a half to avoid plugging lag 22 | public static int MAX_DISPLAYS => 8; 23 | 24 | public static bool OpenHandle(out IntPtr vdd) 25 | { 26 | if (Device.OpenHandle(ADAPTER_GUID, out vdd)) 27 | { 28 | Update(vdd); 29 | return true; 30 | } 31 | 32 | return false; 33 | } 34 | 35 | public static void CloseHandle(IntPtr vdd) 36 | { 37 | Device.CloseHandle(vdd); 38 | } 39 | 40 | public static List GetDisplays(out bool noMonitors) 41 | { 42 | var displays = Display.GetAllDisplays(); 43 | noMonitors = displays.Count == 0; 44 | 45 | displays = displays.FindAll(d => d.DisplayName 46 | .Equals(DISPLAY_ID, StringComparison.OrdinalIgnoreCase)); 47 | 48 | noMonitors = displays.Count == 0 && noMonitors; 49 | return displays; 50 | } 51 | 52 | public static List GetDisplays() 53 | { 54 | return GetDisplays(out var _); 55 | } 56 | 57 | /// 58 | /// Query the driver device status. 59 | /// 60 | public static Device.Status QueryStatus(out Version version) 61 | { 62 | return Device.QueryStatus(CLASS_GUID, HARDWARE_ID, out version); 63 | } 64 | 65 | /// 66 | /// Get driver version from the device handle. 67 | /// 68 | public static bool GetVersion(IntPtr vdd, out string version) 69 | { 70 | if (IoControl(vdd, IoCtlCode.IOCTL_VERSION, null, out int vernum, 100)) 71 | { 72 | int major = (vernum >> 16) & 0xFFFF; 73 | int minor = vernum & 0xFFFF; 74 | version = $"{major}.{minor}"; 75 | return true; 76 | } 77 | else 78 | { 79 | version = "(unknown)"; 80 | return false; 81 | } 82 | } 83 | 84 | /// 85 | /// Add a virtual display and retrieve the index. 86 | /// 87 | public static bool AddDisplay(IntPtr vdd, out int index) 88 | { 89 | if (IoControl(vdd, IoCtlCode.IOCTL_ADD, null, out index, 5000)) 90 | { 91 | Update(vdd); 92 | return true; 93 | } 94 | 95 | return false; 96 | } 97 | 98 | /// 99 | /// Remove an added display by index. 100 | /// 101 | public static bool RemoveDisplay(IntPtr vdd, int index) 102 | { 103 | var input = new byte[2]; 104 | input[1] = (byte)(index & 0xFF); 105 | 106 | if (IoControl(vdd, IoCtlCode.IOCTL_REMOVE, input, 1000)) 107 | { 108 | Update(vdd); 109 | return true; 110 | } 111 | 112 | return false; 113 | } 114 | 115 | /// 116 | /// Update driver session to keep added displays alive. 117 | /// 118 | public static void Update(IntPtr vdd) 119 | { 120 | IoControl(vdd, IoCtlCode.IOCTL_UPDATE, null, 1000); 121 | } 122 | 123 | private enum IoCtlCode 124 | { 125 | IOCTL_ADD = 0x22E004, 126 | IOCTL_REMOVE = 0x22A008, 127 | IOCTL_UPDATE = 0x22A00C, 128 | IOCTL_VERSION = 0x22E010, 129 | 130 | // new code in driver v0.45 131 | // relates to IOCTL_UPDATE and per display state 132 | // but unused in Parsec app 133 | IOCTL_UNKNOWN1 = 0x22A014, 134 | } 135 | 136 | /// 137 | /// Send IO control code to the driver device handle. 138 | /// 139 | private static bool IoControl(IntPtr handle, IoCtlCode code, byte[] input, int* result, int timeout) 140 | { 141 | var InBuffer = new byte[32]; 142 | var Overlapped = new Native.OVERLAPPED(); 143 | 144 | if (input != null && input.Length > 0) 145 | { 146 | Array.Copy(input, InBuffer, Math.Min(input.Length, InBuffer.Length)); 147 | } 148 | 149 | fixed (byte* buffer = InBuffer) 150 | { 151 | int outputLength = result != null ? sizeof(int) : 0; 152 | Overlapped.hEvent = Native.CreateEvent(null, false, false, null); 153 | 154 | bool sent = Native.DeviceIoControl(handle, (uint)code, 155 | buffer, InBuffer.Length, 156 | result, outputLength, 157 | null, ref Overlapped); 158 | 159 | #if DEBUG 160 | if (code != IoCtlCode.IOCTL_UPDATE) 161 | Console.WriteLine("[D] IoControl: {0}\n Sent: {1}, error: {2}", code, sent, DumpErrorCode(Marshal.GetLastWin32Error())); 162 | #endif 163 | if (!sent && Marshal.GetLastWin32Error() == 0x6) 164 | return false; 165 | 166 | bool success = Native.GetOverlappedResultEx(handle, ref Overlapped, 167 | out var NumberOfBytesTransferred, timeout, false); 168 | 169 | #if DEBUG 170 | if (code != IoCtlCode.IOCTL_UPDATE) 171 | Console.WriteLine(" OverlappedResult: {0}, error: {1}", success, DumpErrorCode(Marshal.GetLastWin32Error())); 172 | #endif 173 | 174 | if (Overlapped.hEvent != IntPtr.Zero) 175 | Native.CloseHandle(Overlapped.hEvent); 176 | 177 | return success; 178 | } 179 | } 180 | 181 | private static bool IoControl(IntPtr handle, IoCtlCode code, byte[] input, int timeout) 182 | { 183 | return IoControl(handle, code, input, null, timeout); 184 | } 185 | 186 | private static bool IoControl(IntPtr handle, IoCtlCode code, byte[] input, out int result, int timeout) 187 | { 188 | int output; 189 | bool success = IoControl(handle, code, input, &output, timeout); 190 | result = output; 191 | return success; 192 | } 193 | 194 | private static string DumpErrorCode(int code) 195 | { 196 | string ret = code.ToString("X"); 197 | 198 | if (code == 0) 199 | ret += " (SUCCESS)"; 200 | else if (code == 0x6) 201 | ret += " (ERROR_INVALID_HANDLE)"; 202 | else if (code == 0x3E5) 203 | ret += " (ERROR_IO_PENDING)"; 204 | 205 | return ret; 206 | } 207 | 208 | private static class Native 209 | { 210 | [DllImport("kernel32.dll", SetLastError = true)] 211 | [return: MarshalAs(UnmanagedType.Bool)] 212 | public static extern bool DeviceIoControl( 213 | IntPtr device, uint code, 214 | void* lpInBuffer, int nInBufferSize, 215 | void* lpOutBuffer, int nOutBufferSize, 216 | void* lpBytesReturned, 217 | ref OVERLAPPED lpOverlapped 218 | ); 219 | 220 | [DllImport("kernel32.dll", SetLastError = true)] 221 | [return: MarshalAs(UnmanagedType.Bool)] 222 | public static extern bool GetOverlappedResultEx( 223 | IntPtr handle, 224 | ref OVERLAPPED lpOverlapped, 225 | out uint lpNumberOfBytesTransferred, 226 | int dwMilliseconds, 227 | [MarshalAs(UnmanagedType.Bool)] bool bAlertable 228 | ); 229 | 230 | [StructLayout(LayoutKind.Sequential)] 231 | public struct OVERLAPPED 232 | { 233 | public IntPtr Internal; 234 | public IntPtr InternalHigh; 235 | public IntPtr Pointer; 236 | public IntPtr hEvent; 237 | } 238 | 239 | [DllImport("kernel32.dll", EntryPoint = "CreateEventW", CharSet = CharSet.Unicode)] 240 | public static extern IntPtr CreateEvent( 241 | void* lpEventAttributes, 242 | [MarshalAs(UnmanagedType.Bool)] bool bManualReset, 243 | [MarshalAs(UnmanagedType.Bool)] bool bInitialState, 244 | string lpName 245 | ); 246 | 247 | [DllImport("kernel32.dll")] 248 | [return: MarshalAs(UnmanagedType.Bool)] 249 | public static extern bool CloseHandle(IntPtr handle); 250 | } 251 | } 252 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

5 | 6 | 7 | Parsec Virtual Display Driver 8 | 9 |

10 | 11 |

12 | ✨ Perfect virtual display for game streaming 13 |

14 | 15 |

16 | 17 | 18 | 19 | 20 | 21 | 22 |

23 | 24 |
25 | 26 | ## ℹ About 27 | 28 | This project provides a **standalone solution for creating virtual displays** on 29 | a Windows host using the **Parsec Virtual Display Driver** (VDD), independent of 30 | the **Parsec app**. 31 | 32 | The Parsec VDD enables virtual displays on Windows 10+ systems, a feature 33 | available to Parsec Teams and Warp customers. With VDD, users can add up to 34 | three virtual displays to a host machine they connect to, ideal for setups where 35 | physical monitors may be unavailable or when additional displays are beneficial. 36 | 37 | Built by Parsec, the VDD leverages the IddCx API (Indirect Display Driver) to 38 | generate virtual displays with support for high resolutions and refresh rates, 39 | including up to 4K and 240 Hz. This capability makes it a versatile tool for 40 | gaming, streaming, or remote work, allowing users to simulate multiple screens 41 | for an enhanced, flexible visual experience. 42 | 43 | ## 📺 ParsecVDisplay App 44 | 45 | ParsecVDisplay is a comprehensive virtual display manager for Parsec VDD, built 46 | with C# and WPF. The app provides an intuitive interface to manage virtual 47 | displays, showing the number of active displays and allowing users to add or 48 | remove specific virtual displays. It also supports features like changing 49 | display resolution, capturing screenshots, and more, making it a versatile tool 50 | for flexible display management. 51 | 52 | 👉 Check out [Releases](https://github.com/nomi-san/parsec-vdd/releases) to 53 | download it. 54 | 55 |

56 | 57 |

58 | 59 | ## 🚀 Using Core API 60 | 61 | ### Design notes 62 | 63 | Parsec VDD is designed to work with Parsec client-connection sessions. When the 64 | user connects to the host, the app will start controlling the driver, it sends 65 | IO control codes and gets results. When adding a virtual display, you will get 66 | its index to be used for unplugging, the maximum number of displays could be 67 | added up to 16 per adapter. You have to ping the driver periodically to keep 68 | added displays alive, otherwise all of them will be unplugged after a second. 69 | There's no direct way to manipulate added displays, you should call Win32 70 | Display API to change their display mode (see the ParsecVDisplay source). 71 | 72 | ```mermaid 73 | flowchart LR 74 | A(app) 75 | B(vdd) 76 | 77 | A <--->|ioctl| B 78 | A ..->|ping| B 79 | 80 | B --- X(display1) 81 | B --- Y(display2) 82 | B --- Z(display3) 83 | 84 | winapi -->|manipulate| X 85 | ``` 86 | 87 | ### Using the code 88 | 89 | For detailed instructions and usage examples, refer to the [VDD_LIBRARY_USAGE](./docs/VDD_LIBRARY_USAGE.md). 90 | 91 | - The core API is designed as single C/C++ header that can be added to any 92 | project, 👉 [core/parsec-vdd.h](./core/parsec-vdd.h) 93 | - There is also a simple demo program, 👉 [core/vdd-demo.cc](./core/vdd-demo.cc) 94 | 95 | ### Picking a driver 96 | 97 | You have to install the driver to make them work. 98 | 99 | | Version | Minimum OS | IddCx | Notes | 100 | | :---------------- | :-------------- | :---: | :-------------------------------------------------------- | 101 | | [parsec-vdd-0.38] | Windows 10 1607 | 1.0 | Obsolete, may crash randomly. | 102 | | [parsec-vdd-0.41] | Windows 10 19H2 | 1.4 | Stable. | 103 | | [parsec-vdd-0.45] | Windows 10 21H2 | 1.5 | Better streaming color, but may not work on some Windows. | 104 | 105 | [parsec-vdd-0.38]: https://builds.parsec.app/vdd/parsec-vdd-0.38.0.0.exe 106 | [parsec-vdd-0.41]: https://builds.parsec.app/vdd/parsec-vdd-0.41.0.0.exe 107 | [parsec-vdd-0.45]: https://builds.parsec.app/vdd/parsec-vdd-0.45.0.0.exe 108 | 109 | > All of them also work on Windows Server 2019 or higher. 110 | 111 | You can unzip (using 7z) the driver setup above to obtain the driver files and 112 | `nefconw` CLI. 113 | 114 | ``` 115 | vdd-0.45/ 116 | |__ nefconw.exe 117 | |__ driver/ 118 | |__ mm.cat 119 | |__ mm.dll 120 | |__ mm.inf 121 | ``` 122 | 123 | Command line method to install the driver using `nefconw` (admin required): 124 | 125 | ``` 126 | start /wait .\nefconw.exe --remove-device-node --hardware-id Root\Parsec\VDA --class-guid "4D36E968-E325-11CE-BFC1-08002BE10318" 127 | start /wait .\nefconw.exe --create-device-node --class-name Display --class-guid "4D36E968-E325-11CE-BFC1-08002BE10318" --hardware-id Root\Parsec\VDA 128 | start /wait .\nefconw.exe --install-driver --inf-path ".\driver\mm.inf" 129 | ``` 130 | 131 | In addition, you can run the driver setup in silent mode to install it quickly. 132 | 133 | ``` 134 | .\parsec-vdd-0.45.0.0.exe /S 135 | ``` 136 | 137 | ## 😥 Known Limitations 138 | 139 | > This list shows the known limitations of Parsec VDD. 140 | 141 | ### 1. HDR support 142 | 143 | Parsec VDD does not support HDR on its displays (see the EDID below). 144 | Theoretically, you can unlock support by editing the EDID, then adding HDR 145 | metadata and setting 10-bit+ color depth. Unfortunately, you cannot flash its 146 | firmware like a physical device, or modify the registry value. 147 | 148 | All IDDs have their own fixed EDID block inside the driver binary to initialize 149 | the monitor specs. So the solution is to modify this block in the driver DLL 150 | (mm.dll), then reinstall it with `nefconw` CLI (see above). 151 | 152 | ### 2. Custom resolutions 153 | 154 | Before connecting, the virtual display looks in the `HKLM\SOFTWARE\Parsec\vdd` 155 | registry for additional preset resolutions. Currently this supports a maximum of 156 | 5 values. 157 | 158 | ```yaml 159 | HKLM\SOFTWARE\Parsec\vdd: 160 | - key: [0 -> 5] 161 | value: { width, height, hz } 162 | ``` 163 | 164 | To unlock this limit, you need to patch the driver DLL the same way as above, 165 | but **5 is enough** for personal use. 166 | 167 | ## 😑 Known Bugs 168 | 169 | > This is a list of known issues when working with standalone Parsec VDD. 170 | 171 | ### 1. Incompatible with Parsec Privacy Mode 172 | 173 | ![Alt text](https://i.imgur.com/C74IRgC.png) 174 | 175 | If you have enabled "Privacy Mode" in Parsec Host settings, please disable it 176 | and clear the connected display configurations in the following Registry path. 177 | 178 | ``` 179 | HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Connectivity 180 | ``` 181 | 182 | This option causes your main display to turn off when virtual displays are 183 | added, making it difficult to turn the display on and disrupting the remote 184 | desktop session. 185 | 186 | ### 2. // todo 187 | 188 | ## 🤔 Comparison with other IDDs 189 | 190 | The table below shows a comparison with other popular Indirect Display Driver 191 | projects. 192 | 193 | | Project | Iddcx version | Signed | Gaming | HDR | H-Cursor | Tweakable | Controller | 194 | | :----------------------------- | :-----------: | :----: | :----: | :-: | :------------------------------------------------------------------: | :-------: | :--------: | 195 | | [usbmmidd_v2] | | ✅ | ❌ | ❌ | ❌ | | | 196 | | [IddSampleDriver] | 1.2 | ❌ | ❌ | ❌ | ❌ | | | 197 | | [RustDeskIddDriver] | 1.2 | ❌ | ❌ | ❌ | ❌ | | | 198 | | [Virtual-Display-Driver (HDR)] | 1.10 | ❌ | | ✅ | ❌ | | | 199 | | [virtual-display-rs] | 1.5 | ❌ | | ❌ | [#81](https://github.com/MolotovCherry/virtual-display-rs/issues/81) | ✅ | ✅ | 200 | | parsec-vdd | 1.5 | ✅ | ✅ | ❌ | ✅ | 🆗 | ✅ | 201 | 202 | ✅ - full support, 🆗 - limited support 203 | 204 | [usbmmidd_v2]: https://www.amyuni.com/forum/viewtopic.php?t=3030 205 | [IddSampleDriver]: https://github.com/roshkins/IddSampleDriver 206 | [RustDeskIddDriver]: https://github.com/fufesou/RustDeskIddDriver 207 | [virtual-display-rs]: https://github.com/MolotovCherry/virtual-display-rs 208 | [Virtual-Display-Driver (HDR)]: https://github.com/itsmikethetech/Virtual-Display-Driver 209 | 210 | **Signed** means that the driver files have a valid digital signature. 211 | **H-Cursor** means hardware cursor support, without it, you will get a double 212 | cursor on some remote desktop apps. **Tweakable** is the ability to customize 213 | display modes. Visit 214 | [MSDN IddCx versions](https://learn.microsoft.com/en-us/windows-hardware/drivers/display/iddcx-versions) 215 | to check the minimum supported Windows version. 216 | 217 | ## 📘 Parsec VDD Specs 218 | 219 | Common preset display modes: 220 | 221 | | Resolution | Common Name | Aspect Ratio | Refresh Rates (Hz) | 222 | | ----------- | ----------- | ------------ | ------------------ | 223 | | 3840 x 2160 | 4K UHD | 16:9 | 24/30/60/144/240 | 224 | | 3440 x 1440 | UltraWide | 21.5:9 | 24/30/60/144/240 | 225 | | 2560 x 1440 | 2K | 16:9 | 24/30/60/144/240 | 226 | | 2560 x 1080 | UltraWide | 21:9 | 24/30/60/144/240 | 227 | | 1920 x 1080 | FHD | 16:9 | 24/30/60/144/240 | 228 | | 1600 x 900 | HD+ | 16:9 | 60/144/240 | 229 | | 1280 x 720 | HD | 16:9 | 60/144/240 | 230 | 231 | Check out [docs/PARSEC_VDD_SPECS](./docs/PARSEC_VDD_SPECS.md) to see full of 232 | preset display modes the driver specs. 233 | 234 | ## 🤝 Sponsors 235 | 236 | 237 | 238 | 239 | 240 | 241 |
Free code signing on Windows provided by SignPath.io, certificate by SignPath Foundation
242 | 243 | ## 🍻 Credits 244 | 245 | - Thanks to Parsec for the driver 246 | - The app's background was from old parsecgaming.com 247 | -------------------------------------------------------------------------------- /app/CLI.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Runtime.InteropServices; 4 | using System.Text.RegularExpressions; 5 | using System.Threading; 6 | 7 | namespace ParsecVDisplay 8 | { 9 | internal static class CLI 10 | { 11 | static IntPtr VddHandle = IntPtr.Zero; 12 | 13 | static void ShowHelp() 14 | { 15 | Console.WriteLine("vdd command [args...]"); 16 | Console.WriteLine(" -a|add - Add a virtual display"); 17 | Console.WriteLine(" -r|remove - Remove the last added virtual display"); 18 | Console.WriteLine(" X - Remove the virtual display at index X (number)"); 19 | Console.WriteLine(" all - Remove all the added virtual displays"); 20 | Console.WriteLine(" -l|list - Show all the added virtual displays and specs"); 21 | Console.WriteLine(" -s|set X WxH - Set resolution for a virtual display"); 22 | Console.WriteLine(" where X is index number, WxH is size, e.g 1920x1080"); 23 | Console.WriteLine(" X @R - Set only the refresh rate R, e.g @60, @120 (hz)"); 24 | Console.WriteLine(" on Powershell, you should replace '@' with 'r'"); 25 | Console.WriteLine(" X WxH@R - Set full display mode as above, e.g 1920x1080@144"); 26 | Console.WriteLine(" -v|version - Query driver version and status"); 27 | Console.WriteLine(" -h|help - Show this help"); 28 | } 29 | 30 | public static int Execute(string[] args) 31 | { 32 | AttachConsole(-1); 33 | 34 | if (args.Length > 0) 35 | { 36 | try 37 | { 38 | switch (args[0]) 39 | { 40 | case "-a": 41 | case "add": 42 | return AddDisplay(); 43 | 44 | case "-r": 45 | case "remove": 46 | return RemoveDisplay(args); 47 | 48 | case "-l": 49 | case "list": 50 | return ListDisplay(); 51 | 52 | case "-s": 53 | case "set": 54 | return SetDisplayMode(args); 55 | 56 | case "-v": 57 | case "version": 58 | return QueryVersion(); 59 | 60 | case "-h": 61 | case "help": 62 | ShowHelp(); 63 | return 0; 64 | 65 | default: 66 | Console.WriteLine("Invalid command '{0}'", args[0]); 67 | ShowHelp(); 68 | return 0; 69 | } 70 | } 71 | catch (Exception ex) 72 | { 73 | Console.WriteLine("Error: {0}", ex.Message); 74 | #if DEBUG 75 | Console.Error.WriteLine(ex.StackTrace); 76 | #endif 77 | return -1; 78 | } 79 | finally 80 | { 81 | Vdd.Core.CloseHandle(VddHandle); 82 | } 83 | } 84 | else 85 | { 86 | ShowHelp(); 87 | return 0; 88 | } 89 | } 90 | 91 | static Device.Status PrepareVdd() 92 | { 93 | var status = Vdd.Core.QueryStatus(out var _); 94 | 95 | if (status == Device.Status.NOT_INSTALLED) 96 | { 97 | throw new Exception("The driver is not found, please install it first"); 98 | } 99 | else if (status != Device.Status.OK) 100 | { 101 | throw new Exception($"The driver is not OK, got status {status}"); 102 | } 103 | 104 | if (!Vdd.Core.OpenHandle(out VddHandle)) 105 | { 106 | throw new Exception("Failed to obtain the driver device handle"); 107 | } 108 | 109 | return status; 110 | } 111 | 112 | static void CheckAppRunning() 113 | { 114 | if (!EventWaitHandle.TryOpenExisting(Program.AppId, out var _)) 115 | { 116 | throw new Exception($"{Program.AppName} app is not running"); 117 | } 118 | } 119 | 120 | static int AddDisplay() 121 | { 122 | var displays = Vdd.Core.GetDisplays(); 123 | int maxCount = Vdd.Core.MAX_DISPLAYS; 124 | 125 | if (displays.Count >= maxCount) 126 | { 127 | throw new Exception(string.Format("Exceeded limit ({0}), could not add more displays", maxCount)); 128 | } 129 | 130 | PrepareVdd(); 131 | CheckAppRunning(); 132 | 133 | if (Vdd.Core.AddDisplay(VddHandle, out int index)) 134 | { 135 | Console.WriteLine($"Added a virtual display with index {0}.", index); 136 | return index; 137 | } 138 | else 139 | { 140 | throw new Exception("Failed to add a virtual display."); 141 | } 142 | } 143 | 144 | static int RemoveDisplay(string[] args) 145 | { 146 | var arg1 = args.Length >= 2 ? args[1] : ""; 147 | bool removeAll = arg1 == "all" || arg1 == "*"; 148 | int index = -1; 149 | 150 | if (args.Length == 1 || removeAll || int.TryParse(arg1, out index)) 151 | { 152 | var displays = Vdd.Core.GetDisplays(); 153 | 154 | if (displays.Count == 0) 155 | { 156 | Console.WriteLine("No Parsec Display available."); 157 | return 0; 158 | } 159 | else if (removeAll) 160 | { 161 | PrepareVdd(); 162 | foreach (var di in displays) 163 | { 164 | if (!Vdd.Core.RemoveDisplay(VddHandle, di.DisplayIndex)) 165 | throw new Exception(string.Format("Failed to remove the display at index {0}.", index)); 166 | } 167 | 168 | Console.WriteLine("Removed all added displays."); 169 | return 0; 170 | } 171 | else 172 | { 173 | var display = index < 0 ? displays.LastOrDefault() 174 | : displays.FirstOrDefault(di => di.DisplayIndex == index); 175 | 176 | if (display != null) 177 | { 178 | PrepareVdd(); 179 | if (!Vdd.Core.RemoveDisplay(VddHandle, display.DisplayIndex)) 180 | throw new Exception(string.Format("Failed to remove the display at index {0}.", display.DisplayIndex)); 181 | 182 | Console.WriteLine("Removed display at index {0}.", display.DisplayIndex); 183 | return 0; 184 | } 185 | else 186 | { 187 | throw new Exception(string.Format("Display index {0} is not found.", index)); 188 | } 189 | } 190 | } 191 | else 192 | { 193 | throw new Exception(string.Format("Invalid display index '{0}'.", arg1)); 194 | } 195 | } 196 | 197 | static int ListDisplay() 198 | { 199 | var displays = Vdd.Core.GetDisplays(); 200 | 201 | if (displays.Count > 0) 202 | { 203 | foreach (var di in displays) 204 | { 205 | Console.WriteLine("Index: {0}", di.DisplayIndex); 206 | Console.WriteLine(" - Device: {0}", di.DeviceName); 207 | Console.WriteLine(" - Number: {0}", di.Identifier); 208 | Console.WriteLine(" - Name: {0}", di.DisplayName); 209 | Console.WriteLine(" - Mode: {0}", di.CurrentMode); 210 | Console.WriteLine(" - Orientation: {0} ({1}°)", di.CurrentOrientation, (int)di.CurrentOrientation * 90); 211 | } 212 | } 213 | else 214 | { 215 | Console.WriteLine("No virtual displays present."); 216 | } 217 | 218 | return displays.Count; 219 | } 220 | 221 | static int SetDisplayMode(string[] args) 222 | { 223 | if (args.Length < 2) 224 | throw new Exception("Missing display index."); 225 | if (args.Length < 3) 226 | throw new Exception("Missing resolution and/or refresh rate."); 227 | 228 | var argIndex = args[1]; 229 | var argDMode = string.Join(" ", args.Skip(2)); 230 | 231 | if (int.TryParse(argIndex, out int index)) 232 | { 233 | var displays = Vdd.Core.GetDisplays(); 234 | 235 | if (displays.Count == 0) 236 | { 237 | Console.WriteLine("No Parsec Display available."); 238 | return 0; 239 | } 240 | else 241 | { 242 | var display = displays.Find(di => di.DisplayIndex == index); 243 | 244 | if (display == null) 245 | throw new Exception(string.Format("Display index {0} is not found.", index)); 246 | 247 | int? width, height, hz; 248 | ParseDisplayModeArg(argDMode, out width, out height, out hz); 249 | 250 | if ((width != null && height != null) || hz != null) 251 | { 252 | if (display.ChangeMode(width, height, hz, null)) 253 | { 254 | Console.WriteLine($"Display index {index} is set to '{argDMode}'."); 255 | return 0; 256 | } 257 | else 258 | { 259 | throw new Exception($"Failed to set, display mode '{argDMode}' is not supported."); 260 | } 261 | } 262 | else 263 | { 264 | throw new Exception("Nothing to do, recheck your syntax."); 265 | } 266 | } 267 | } 268 | else 269 | { 270 | throw new Exception(string.Format("Invalid display index '{0}'.", argIndex)); 271 | } 272 | } 273 | 274 | static int QueryVersion() 275 | { 276 | var status = Vdd.Core.QueryStatus(out var version); 277 | 278 | Console.WriteLine(Vdd.Core.ADAPTER); 279 | Console.WriteLine("- Status: {0}", status); 280 | Console.WriteLine("- Version: {0}.{1}", version.Major, version.Minor); 281 | 282 | return (int)status; 283 | } 284 | 285 | static void ParseDisplayModeArg(string arg, out int? width, out int? height, out int? hz) 286 | { 287 | Match match; 288 | arg = arg.Trim(); 289 | 290 | width = null; 291 | height = null; 292 | hz = null; 293 | 294 | const string regexSize = @"^(\d+)\s*[xX]\s*(\d+)$"; 295 | if (Regex.IsMatch(arg, regexSize)) 296 | { 297 | match = Regex.Match(arg, regexSize); 298 | width = int.Parse(match.Groups[1].Value); 299 | height = int.Parse(match.Groups[2].Value); 300 | return; 301 | } 302 | 303 | const string regexHz = @"^[r@](\d+)$"; 304 | if (Regex.IsMatch(arg, regexHz)) 305 | { 306 | match = Regex.Match(arg, regexHz); 307 | hz = int.Parse(match.Groups[1].Value); 308 | return; 309 | } 310 | 311 | const string regexAll = @"^(\d+)\s*[xX]\s*(\d+)\s*[r@](\d+)$"; 312 | if (Regex.IsMatch(arg, regexAll)) 313 | { 314 | match = Regex.Match(arg, regexAll); 315 | width = int.Parse(match.Groups[1].Value); 316 | height = int.Parse(match.Groups[2].Value); 317 | hz = int.Parse(match.Groups[3].Value); 318 | return; 319 | } 320 | } 321 | 322 | [DllImport("kernel32.dll")] 323 | [return: MarshalAs(UnmanagedType.Bool)] 324 | static extern bool AttachConsole(int dwProcessId); 325 | } 326 | } -------------------------------------------------------------------------------- /core/parsec-vdd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, Nguyen Duy All rights reserved. 3 | * GitHub repo: https://github.com/nomi-san/parsec-vdd/ 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | * POSSIBILITY OF SUCH DAMAGE. 25 | * 26 | */ 27 | 28 | #ifndef __PARSEC_VDD_H 29 | #define __PARSEC_VDD_H 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #ifdef _MSC_VER 36 | #pragma comment(lib, "cfgmgr32.lib") 37 | #pragma comment(lib, "setupapi.lib") 38 | #endif 39 | 40 | #ifdef __cplusplus 41 | namespace parsec_vdd 42 | { 43 | #endif 44 | 45 | // Device helper. 46 | ////////////////////////////////////////////////// 47 | 48 | typedef enum { 49 | DEVICE_OK = 0, // Ready to use 50 | DEVICE_INACCESSIBLE, // Inaccessible 51 | DEVICE_UNKNOWN, // Unknown status 52 | DEVICE_UNKNOWN_PROBLEM, // Unknown problem 53 | DEVICE_DISABLED, // Device is disabled 54 | DEVICE_DRIVER_ERROR, // Device encountered error 55 | DEVICE_RESTART_REQUIRED, // Must restart PC to use (could ignore but would have issue) 56 | DEVICE_DISABLED_SERVICE, // Service is disabled 57 | DEVICE_NOT_INSTALLED // Driver is not installed 58 | } DeviceStatus; 59 | 60 | /** 61 | * Query the driver status. 62 | * 63 | * @param classGuid The GUID of the class. 64 | * @param deviceId The device/hardware ID of the driver. 65 | * @return DeviceStatus 66 | */ 67 | static DeviceStatus QueryDeviceStatus(const GUID *classGuid, const char *deviceId) 68 | { 69 | DeviceStatus status = DEVICE_INACCESSIBLE; 70 | 71 | SP_DEVINFO_DATA devInfoData; 72 | ZeroMemory(&devInfoData, sizeof(SP_DEVINFO_DATA)); 73 | devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 74 | 75 | HDEVINFO devInfo = SetupDiGetClassDevsA(classGuid, NULL, NULL, DIGCF_PRESENT); 76 | 77 | if (devInfo != INVALID_HANDLE_VALUE) 78 | { 79 | BOOL foundProp = FALSE; 80 | UINT deviceIndex = 0; 81 | 82 | do 83 | { 84 | if (!SetupDiEnumDeviceInfo(devInfo, deviceIndex, &devInfoData)) 85 | break; 86 | 87 | DWORD requiredSize = 0; 88 | SetupDiGetDeviceRegistryPropertyA(devInfo, &devInfoData, 89 | SPDRP_HARDWAREID, NULL, NULL, 0, &requiredSize); 90 | 91 | if (requiredSize > 0) 92 | { 93 | DWORD regDataType = 0; 94 | LPBYTE propBuffer = (LPBYTE)calloc(1, requiredSize); 95 | 96 | if (SetupDiGetDeviceRegistryPropertyA( 97 | devInfo, 98 | &devInfoData, 99 | SPDRP_HARDWAREID, 100 | ®DataType, 101 | propBuffer, 102 | requiredSize, 103 | &requiredSize)) 104 | { 105 | if (regDataType == REG_SZ || regDataType == REG_MULTI_SZ) 106 | { 107 | for (LPCSTR cp = (LPCSTR)propBuffer; ; cp += lstrlenA(cp) + 1) 108 | { 109 | if (!cp || *cp == 0 || cp >= (LPCSTR)(propBuffer + requiredSize)) 110 | { 111 | status = DEVICE_NOT_INSTALLED; 112 | goto except; 113 | } 114 | 115 | if (lstrcmpA(deviceId, cp) == 0) 116 | break; 117 | } 118 | 119 | foundProp = TRUE; 120 | ULONG devStatus, devProblemNum; 121 | 122 | if (CM_Get_DevNode_Status(&devStatus, &devProblemNum, devInfoData.DevInst, 0) != CR_SUCCESS) 123 | { 124 | status = DEVICE_NOT_INSTALLED; 125 | goto except; 126 | } 127 | 128 | if ((devStatus & (DN_DRIVER_LOADED | DN_STARTED)) != 0) 129 | { 130 | status = DEVICE_OK; 131 | } 132 | else if ((devStatus & DN_HAS_PROBLEM) != 0) 133 | { 134 | switch (devProblemNum) 135 | { 136 | case CM_PROB_NEED_RESTART: 137 | status = DEVICE_RESTART_REQUIRED; 138 | break; 139 | case CM_PROB_DISABLED: 140 | case CM_PROB_HARDWARE_DISABLED: 141 | status = DEVICE_DISABLED; 142 | break; 143 | case CM_PROB_DISABLED_SERVICE: 144 | status = DEVICE_DISABLED_SERVICE; 145 | break; 146 | default: 147 | if (devProblemNum == CM_PROB_FAILED_POST_START) 148 | status = DEVICE_DRIVER_ERROR; 149 | else 150 | status = DEVICE_UNKNOWN_PROBLEM; 151 | break; 152 | } 153 | } 154 | else 155 | { 156 | status = DEVICE_UNKNOWN; 157 | } 158 | } 159 | } 160 | 161 | except: 162 | free(propBuffer); 163 | } 164 | 165 | ++deviceIndex; 166 | } while (!foundProp); 167 | 168 | if (!foundProp && GetLastError() != 0) 169 | status = DEVICE_NOT_INSTALLED; 170 | 171 | SetupDiDestroyDeviceInfoList(devInfo); 172 | } 173 | 174 | return status; 175 | } 176 | 177 | /** 178 | * Obtain the device handle. 179 | * Returns NULL or INVALID_HANDLE_VALUE if fails, otherwise a valid handle. 180 | * Should call CloseDeviceHandle to close this handle after use. 181 | * 182 | * @param interfaceGuid The adapter/interface GUID of the target device. 183 | * @return HANDLE 184 | */ 185 | static HANDLE OpenDeviceHandle(const GUID *interfaceGuid) 186 | { 187 | HANDLE handle = INVALID_HANDLE_VALUE; 188 | HDEVINFO devInfo = SetupDiGetClassDevsA(interfaceGuid, 189 | NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 190 | 191 | if (devInfo != INVALID_HANDLE_VALUE) 192 | { 193 | SP_DEVICE_INTERFACE_DATA devInterface; 194 | ZeroMemory(&devInterface, sizeof(SP_DEVICE_INTERFACE_DATA)); 195 | devInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 196 | 197 | for (DWORD i = 0; SetupDiEnumDeviceInterfaces(devInfo, NULL, interfaceGuid, i, &devInterface); ++i) 198 | { 199 | DWORD detailSize = 0; 200 | SetupDiGetDeviceInterfaceDetailA(devInfo, &devInterface, NULL, 0, &detailSize, NULL); 201 | 202 | SP_DEVICE_INTERFACE_DETAIL_DATA_A *detail = (SP_DEVICE_INTERFACE_DETAIL_DATA_A *)calloc(1, detailSize); 203 | detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); 204 | 205 | if (SetupDiGetDeviceInterfaceDetailA(devInfo, &devInterface, detail, detailSize, &detailSize, NULL)) 206 | { 207 | handle = CreateFileA(detail->DevicePath, 208 | GENERIC_READ | GENERIC_WRITE, 209 | FILE_SHARE_READ | FILE_SHARE_WRITE, 210 | NULL, 211 | OPEN_EXISTING, 212 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED | FILE_FLAG_WRITE_THROUGH, 213 | NULL); 214 | 215 | if (handle != NULL && handle != INVALID_HANDLE_VALUE) 216 | break; 217 | } 218 | 219 | free(detail); 220 | } 221 | 222 | SetupDiDestroyDeviceInfoList(devInfo); 223 | } 224 | 225 | return handle; 226 | } 227 | 228 | /* Release the device handle */ 229 | static void CloseDeviceHandle(HANDLE handle) 230 | { 231 | if (handle != NULL && handle != INVALID_HANDLE_VALUE) 232 | CloseHandle(handle); 233 | } 234 | 235 | // Parsec VDD core. 236 | ////////////////////////////////////////////////// 237 | 238 | // Display name info. 239 | static const char *VDD_DISPLAY_ID = "PSCCDD0"; // You will see it in registry (HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY) 240 | static const char *VDD_DISPLAY_NAME = "ParsecVDA"; // You will see it in the [Advanced display settings] tab. 241 | 242 | // Apdater GUID to obtain the device handle. 243 | // {00b41627-04c4-429e-a26e-0265cf50c8fa} 244 | static const GUID VDD_ADAPTER_GUID = { 0x00b41627, 0x04c4, 0x429e, { 0xa2, 0x6e, 0x02, 0x65, 0xcf, 0x50, 0xc8, 0xfa } }; 245 | static const char *VDD_ADAPTER_NAME = "Parsec Virtual Display Adapter"; 246 | 247 | // Class and hwid to query device status. 248 | // {4d36e968-e325-11ce-bfc1-08002be10318} 249 | static const GUID VDD_CLASS_GUID = { 0x4d36e968, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; 250 | static const char *VDD_HARDWARE_ID = "Root\\Parsec\\VDA"; 251 | 252 | // Actually up to 16 devices could be created per adapter 253 | // so just use a half to avoid plugging lag. 254 | static const int VDD_MAX_DISPLAYS = 8; 255 | 256 | // Core IoControl codes, see usage below. 257 | typedef enum { 258 | VDD_IOCTL_ADD = 0x0022e004, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 1, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) 259 | VDD_IOCTL_REMOVE = 0x0022a008, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 2, METHOD_BUFFERED, FILE_WRITE_ACCESS) 260 | VDD_IOCTL_UPDATE = 0x0022a00c, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 3, METHOD_BUFFERED, FILE_WRITE_ACCESS) 261 | VDD_IOCTL_VERSION = 0x0022e010, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 4, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) 262 | 263 | // new code in driver v0.45 264 | // relates to IOCTL_UPDATE and per display state 265 | // but unused in Parsec app 266 | VDD_IOCTL_UNKONWN = 0x0022a00c, // CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800 + 5, METHOD_BUFFERED, FILE_WRITE_ACCESS) 267 | } VddCtlCode; 268 | 269 | // Generic DeviceIoControl for all IoControl codes. 270 | static DWORD VddIoControl(HANDLE vdd, VddCtlCode code, const void *data, size_t size) 271 | { 272 | if (vdd == NULL || vdd == INVALID_HANDLE_VALUE) 273 | return -1; 274 | 275 | BYTE InBuffer[32]; 276 | ZeroMemory(InBuffer, sizeof(InBuffer)); 277 | 278 | OVERLAPPED Overlapped; 279 | ZeroMemory(&Overlapped, sizeof(OVERLAPPED)); 280 | 281 | DWORD OutBuffer = 0; 282 | DWORD NumberOfBytesTransferred; 283 | 284 | if (data != NULL && size > 0) 285 | memcpy(InBuffer, data, (size < sizeof(InBuffer)) ? size : sizeof(InBuffer)); 286 | 287 | Overlapped.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL); 288 | DeviceIoControl(vdd, (DWORD)code, InBuffer, sizeof(InBuffer), &OutBuffer, sizeof(DWORD), NULL, &Overlapped); 289 | 290 | if (!GetOverlappedResultEx(vdd, &Overlapped, &NumberOfBytesTransferred, 5000, FALSE)) 291 | { 292 | CloseHandle(Overlapped.hEvent); 293 | return -1; 294 | } 295 | 296 | if (Overlapped.hEvent != NULL) 297 | CloseHandle(Overlapped.hEvent); 298 | 299 | return OutBuffer; 300 | } 301 | 302 | /** 303 | * Query VDD minor version. 304 | * 305 | * @param vdd The device handle of VDD. 306 | * @return The number of minor version. 307 | */ 308 | static int VddVersion(HANDLE vdd) 309 | { 310 | int minor = VddIoControl(vdd, VDD_IOCTL_VERSION, NULL, 0); 311 | return minor; 312 | } 313 | 314 | /** 315 | * Update/ping to VDD. 316 | * Should call this function in a side thread for each 317 | * less than 100ms to keep all added virtual displays alive. 318 | * 319 | * @param vdd The device handle of VDD. 320 | */ 321 | static void VddUpdate(HANDLE vdd) 322 | { 323 | VddIoControl(vdd, VDD_IOCTL_UPDATE, NULL, 0); 324 | } 325 | 326 | /** 327 | * Add/plug a virtual display. 328 | * 329 | * @param vdd The device handle of VDD. 330 | * @return The index of the added display. 331 | */ 332 | static int VddAddDisplay(HANDLE vdd) 333 | { 334 | int idx = VddIoControl(vdd, VDD_IOCTL_ADD, NULL, 0); 335 | VddUpdate(vdd); 336 | 337 | return idx; 338 | } 339 | 340 | /** 341 | * Remove/unplug a virtual display. 342 | * 343 | * @param vdd The device handle of VDD. 344 | * @param index The index of the display will be removed. 345 | */ 346 | static void VddRemoveDisplay(HANDLE vdd, int index) 347 | { 348 | // 16-bit BE index 349 | UINT16 indexData = ((index & 0xFF) << 8) | ((index >> 8) & 0xFF); 350 | 351 | VddIoControl(vdd, VDD_IOCTL_REMOVE, &indexData, sizeof(indexData)); 352 | VddUpdate(vdd); 353 | } 354 | 355 | #ifdef __cplusplus 356 | } 357 | #endif 358 | 359 | #endif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### C++ ### 2 | # Prerequisites 3 | *.d 4 | 5 | # Compiled Object files 6 | *.slo 7 | *.lo 8 | *.o 9 | *.obj 10 | 11 | # Precompiled Headers 12 | *.gch 13 | *.pch 14 | 15 | # Compiled Dynamic libraries 16 | *.so 17 | *.dylib 18 | *.dll 19 | 20 | # Fortran module files 21 | *.mod 22 | *.smod 23 | 24 | # Compiled Static libraries 25 | *.lai 26 | *.la 27 | *.a 28 | *.lib 29 | 30 | # Executables 31 | *.exe 32 | *.out 33 | *.app 34 | 35 | ### Csharp ### 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | ## 39 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 40 | 41 | # User-specific files 42 | *.rsuser 43 | *.suo 44 | *.user 45 | *.userosscache 46 | *.sln.docstates 47 | 48 | # User-specific files (MonoDevelop/Xamarin Studio) 49 | *.userprefs 50 | 51 | # Mono auto generated files 52 | mono_crash.* 53 | 54 | # Build results 55 | [Dd]ebug/ 56 | [Dd]ebugPublic/ 57 | [Rr]elease/ 58 | [Rr]eleases/ 59 | x64/ 60 | x86/ 61 | [Ww][Ii][Nn]32/ 62 | [Aa][Rr][Mm]/ 63 | [Aa][Rr][Mm]64/ 64 | bld/ 65 | [Bb]in/ 66 | [Oo]bj/ 67 | [Ll]og/ 68 | [Ll]ogs/ 69 | 70 | # Visual Studio 2015/2017 cache/options directory 71 | .vs/ 72 | # Uncomment if you have tasks that create the project's static files in wwwroot 73 | #wwwroot/ 74 | 75 | # Visual Studio 2017 auto generated files 76 | Generated\ Files/ 77 | 78 | # MSTest test Results 79 | [Tt]est[Rr]esult*/ 80 | [Bb]uild[Ll]og.* 81 | 82 | # NUnit 83 | *.VisualState.xml 84 | TestResult.xml 85 | nunit-*.xml 86 | 87 | # Build Results of an ATL Project 88 | [Dd]ebugPS/ 89 | [Rr]eleasePS/ 90 | dlldata.c 91 | 92 | # Benchmark Results 93 | BenchmarkDotNet.Artifacts/ 94 | 95 | # .NET Core 96 | project.lock.json 97 | project.fragment.lock.json 98 | artifacts/ 99 | 100 | # ASP.NET Scaffolding 101 | ScaffoldingReadMe.txt 102 | 103 | # StyleCop 104 | StyleCopReport.xml 105 | 106 | # Files built by Visual Studio 107 | *_i.c 108 | *_p.c 109 | *_h.h 110 | *.ilk 111 | *.meta 112 | *.iobj 113 | *.pdb 114 | *.ipdb 115 | *.pgc 116 | *.pgd 117 | *.rsp 118 | *.sbr 119 | *.tlb 120 | *.tli 121 | *.tlh 122 | *.tmp 123 | *.tmp_proj 124 | *_wpftmp.csproj 125 | *.log 126 | *.tlog 127 | *.vspscc 128 | *.vssscc 129 | .builds 130 | *.pidb 131 | *.svclog 132 | *.scc 133 | 134 | # Chutzpah Test files 135 | _Chutzpah* 136 | 137 | # Visual C++ cache files 138 | ipch/ 139 | *.aps 140 | *.ncb 141 | *.opendb 142 | *.opensdf 143 | *.sdf 144 | *.cachefile 145 | *.VC.db 146 | *.VC.VC.opendb 147 | 148 | # Visual Studio profiler 149 | *.psess 150 | *.vsp 151 | *.vspx 152 | *.sap 153 | 154 | # Visual Studio Trace Files 155 | *.e2e 156 | 157 | # TFS 2012 Local Workspace 158 | $tf/ 159 | 160 | # Guidance Automation Toolkit 161 | *.gpState 162 | 163 | # ReSharper is a .NET coding add-in 164 | _ReSharper*/ 165 | *.[Rr]e[Ss]harper 166 | *.DotSettings.user 167 | 168 | # TeamCity is a build add-in 169 | _TeamCity* 170 | 171 | # DotCover is a Code Coverage Tool 172 | *.dotCover 173 | 174 | # AxoCover is a Code Coverage Tool 175 | .axoCover/* 176 | !.axoCover/settings.json 177 | 178 | # Coverlet is a free, cross platform Code Coverage Tool 179 | coverage*.json 180 | coverage*.xml 181 | coverage*.info 182 | 183 | # Visual Studio code coverage results 184 | *.coverage 185 | *.coveragexml 186 | 187 | # NCrunch 188 | _NCrunch_* 189 | .*crunch*.local.xml 190 | nCrunchTemp_* 191 | 192 | # MightyMoose 193 | *.mm.* 194 | AutoTest.Net/ 195 | 196 | # Web workbench (sass) 197 | .sass-cache/ 198 | 199 | # Installshield output folder 200 | [Ee]xpress/ 201 | 202 | # DocProject is a documentation generator add-in 203 | DocProject/buildhelp/ 204 | DocProject/Help/*.HxT 205 | DocProject/Help/*.HxC 206 | DocProject/Help/*.hhc 207 | DocProject/Help/*.hhk 208 | DocProject/Help/*.hhp 209 | DocProject/Help/Html2 210 | DocProject/Help/html 211 | 212 | # Click-Once directory 213 | publish/ 214 | 215 | # Publish Web Output 216 | *.[Pp]ublish.xml 217 | *.azurePubxml 218 | # Note: Comment the next line if you want to checkin your web deploy settings, 219 | # but database connection strings (with potential passwords) will be unencrypted 220 | *.pubxml 221 | *.publishproj 222 | 223 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 224 | # checkin your Azure Web App publish settings, but sensitive information contained 225 | # in these scripts will be unencrypted 226 | PublishScripts/ 227 | 228 | # NuGet Packages 229 | *.nupkg 230 | # NuGet Symbol Packages 231 | *.snupkg 232 | # The packages folder can be ignored because of Package Restore 233 | **/[Pp]ackages/* 234 | # except build/, which is used as an MSBuild target. 235 | !**/[Pp]ackages/build/ 236 | # Uncomment if necessary however generally it will be regenerated when needed 237 | #!**/[Pp]ackages/repositories.config 238 | # NuGet v3's project.json files produces more ignorable files 239 | *.nuget.props 240 | *.nuget.targets 241 | 242 | # Microsoft Azure Build Output 243 | csx/ 244 | *.build.csdef 245 | 246 | # Microsoft Azure Emulator 247 | ecf/ 248 | rcf/ 249 | 250 | # Windows Store app package directories and files 251 | AppPackages/ 252 | BundleArtifacts/ 253 | Package.StoreAssociation.xml 254 | _pkginfo.txt 255 | *.appx 256 | *.appxbundle 257 | *.appxupload 258 | 259 | # Visual Studio cache files 260 | # files ending in .cache can be ignored 261 | *.[Cc]ache 262 | # but keep track of directories ending in .cache 263 | !?*.[Cc]ache/ 264 | 265 | # Others 266 | ClientBin/ 267 | ~$* 268 | *~ 269 | *.dbmdl 270 | *.dbproj.schemaview 271 | *.jfm 272 | *.pfx 273 | *.publishsettings 274 | orleans.codegen.cs 275 | 276 | # Including strong name files can present a security risk 277 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 278 | #*.snk 279 | 280 | # Since there are multiple workflows, uncomment next line to ignore bower_components 281 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 282 | #bower_components/ 283 | 284 | # RIA/Silverlight projects 285 | Generated_Code/ 286 | 287 | # Backup & report files from converting an old project file 288 | # to a newer Visual Studio version. Backup files are not needed, 289 | # because we have git ;-) 290 | _UpgradeReport_Files/ 291 | Backup*/ 292 | UpgradeLog*.XML 293 | UpgradeLog*.htm 294 | ServiceFabricBackup/ 295 | *.rptproj.bak 296 | 297 | # SQL Server files 298 | *.mdf 299 | *.ldf 300 | *.ndf 301 | 302 | # Business Intelligence projects 303 | *.rdl.data 304 | *.bim.layout 305 | *.bim_*.settings 306 | *.rptproj.rsuser 307 | *- [Bb]ackup.rdl 308 | *- [Bb]ackup ([0-9]).rdl 309 | *- [Bb]ackup ([0-9][0-9]).rdl 310 | 311 | # Microsoft Fakes 312 | FakesAssemblies/ 313 | 314 | # GhostDoc plugin setting file 315 | *.GhostDoc.xml 316 | 317 | # Node.js Tools for Visual Studio 318 | .ntvs_analysis.dat 319 | node_modules/ 320 | 321 | # Visual Studio 6 build log 322 | *.plg 323 | 324 | # Visual Studio 6 workspace options file 325 | *.opt 326 | 327 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 328 | *.vbw 329 | 330 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 331 | *.vbp 332 | 333 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 334 | *.dsw 335 | *.dsp 336 | 337 | # Visual Studio 6 technical files 338 | 339 | # Visual Studio LightSwitch build output 340 | **/*.HTMLClient/GeneratedArtifacts 341 | **/*.DesktopClient/GeneratedArtifacts 342 | **/*.DesktopClient/ModelManifest.xml 343 | **/*.Server/GeneratedArtifacts 344 | **/*.Server/ModelManifest.xml 345 | _Pvt_Extensions 346 | 347 | # Paket dependency manager 348 | .paket/paket.exe 349 | paket-files/ 350 | 351 | # FAKE - F# Make 352 | .fake/ 353 | 354 | # CodeRush personal settings 355 | .cr/personal 356 | 357 | # Python Tools for Visual Studio (PTVS) 358 | __pycache__/ 359 | *.pyc 360 | 361 | # Cake - Uncomment if you are using it 362 | # tools/** 363 | # !tools/packages.config 364 | 365 | # Tabs Studio 366 | *.tss 367 | 368 | # Telerik's JustMock configuration file 369 | *.jmconfig 370 | 371 | # BizTalk build output 372 | *.btp.cs 373 | *.btm.cs 374 | *.odx.cs 375 | *.xsd.cs 376 | 377 | # OpenCover UI analysis results 378 | OpenCover/ 379 | 380 | # Azure Stream Analytics local run output 381 | ASALocalRun/ 382 | 383 | # MSBuild Binary and Structured Log 384 | *.binlog 385 | 386 | # NVidia Nsight GPU debugger configuration file 387 | *.nvuser 388 | 389 | # MFractors (Xamarin productivity tool) working folder 390 | .mfractor/ 391 | 392 | # Local History for Visual Studio 393 | .localhistory/ 394 | 395 | # Visual Studio History (VSHistory) files 396 | .vshistory/ 397 | 398 | # BeatPulse healthcheck temp database 399 | healthchecksdb 400 | 401 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 402 | MigrationBackup/ 403 | 404 | # Ionide (cross platform F# VS Code tools) working folder 405 | .ionide/ 406 | 407 | # Fody - auto-generated XML schema 408 | FodyWeavers.xsd 409 | 410 | # VS Code files for those working on multiple tools 411 | .vscode/* 412 | !.vscode/settings.json 413 | !.vscode/tasks.json 414 | !.vscode/launch.json 415 | !.vscode/extensions.json 416 | *.code-workspace 417 | 418 | # Local History for Visual Studio Code 419 | .history/ 420 | 421 | # Windows Installer files from build outputs 422 | *.cab 423 | *.msi 424 | *.msix 425 | *.msm 426 | *.msp 427 | 428 | # JetBrains Rider 429 | *.sln.iml 430 | 431 | ### VisualStudio ### 432 | 433 | # User-specific files 434 | 435 | # User-specific files (MonoDevelop/Xamarin Studio) 436 | 437 | # Mono auto generated files 438 | 439 | # Build results 440 | 441 | # Visual Studio 2015/2017 cache/options directory 442 | # Uncomment if you have tasks that create the project's static files in wwwroot 443 | 444 | # Visual Studio 2017 auto generated files 445 | 446 | # MSTest test Results 447 | 448 | # NUnit 449 | 450 | # Build Results of an ATL Project 451 | 452 | # Benchmark Results 453 | 454 | # .NET Core 455 | 456 | # ASP.NET Scaffolding 457 | 458 | # StyleCop 459 | 460 | # Files built by Visual Studio 461 | 462 | # Chutzpah Test files 463 | 464 | # Visual C++ cache files 465 | 466 | # Visual Studio profiler 467 | 468 | # Visual Studio Trace Files 469 | 470 | # TFS 2012 Local Workspace 471 | 472 | # Guidance Automation Toolkit 473 | 474 | # ReSharper is a .NET coding add-in 475 | 476 | # TeamCity is a build add-in 477 | 478 | # DotCover is a Code Coverage Tool 479 | 480 | # AxoCover is a Code Coverage Tool 481 | 482 | # Coverlet is a free, cross platform Code Coverage Tool 483 | 484 | # Visual Studio code coverage results 485 | 486 | # NCrunch 487 | 488 | # MightyMoose 489 | 490 | # Web workbench (sass) 491 | 492 | # Installshield output folder 493 | 494 | # DocProject is a documentation generator add-in 495 | 496 | # Click-Once directory 497 | 498 | # Publish Web Output 499 | # Note: Comment the next line if you want to checkin your web deploy settings, 500 | # but database connection strings (with potential passwords) will be unencrypted 501 | 502 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 503 | # checkin your Azure Web App publish settings, but sensitive information contained 504 | # in these scripts will be unencrypted 505 | 506 | # NuGet Packages 507 | # NuGet Symbol Packages 508 | # The packages folder can be ignored because of Package Restore 509 | # except build/, which is used as an MSBuild target. 510 | # Uncomment if necessary however generally it will be regenerated when needed 511 | # NuGet v3's project.json files produces more ignorable files 512 | 513 | # Microsoft Azure Build Output 514 | 515 | # Microsoft Azure Emulator 516 | 517 | # Windows Store app package directories and files 518 | 519 | # Visual Studio cache files 520 | # files ending in .cache can be ignored 521 | # but keep track of directories ending in .cache 522 | 523 | # Others 524 | 525 | # Including strong name files can present a security risk 526 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 527 | 528 | # Since there are multiple workflows, uncomment next line to ignore bower_components 529 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 530 | 531 | # RIA/Silverlight projects 532 | 533 | # Backup & report files from converting an old project file 534 | # to a newer Visual Studio version. Backup files are not needed, 535 | # because we have git ;-) 536 | 537 | # SQL Server files 538 | 539 | # Business Intelligence projects 540 | 541 | # Microsoft Fakes 542 | 543 | # GhostDoc plugin setting file 544 | 545 | # Node.js Tools for Visual Studio 546 | 547 | # Visual Studio 6 build log 548 | 549 | # Visual Studio 6 workspace options file 550 | 551 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 552 | 553 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 554 | 555 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 556 | 557 | # Visual Studio 6 technical files 558 | 559 | # Visual Studio LightSwitch build output 560 | 561 | # Paket dependency manager 562 | 563 | # FAKE - F# Make 564 | 565 | # CodeRush personal settings 566 | 567 | # Python Tools for Visual Studio (PTVS) 568 | 569 | # Cake - Uncomment if you are using it 570 | # tools/** 571 | # !tools/packages.config 572 | 573 | # Tabs Studio 574 | 575 | # Telerik's JustMock configuration file 576 | 577 | # BizTalk build output 578 | 579 | # OpenCover UI analysis results 580 | 581 | # Azure Stream Analytics local run output 582 | 583 | # MSBuild Binary and Structured Log 584 | 585 | # NVidia Nsight GPU debugger configuration file 586 | 587 | # MFractors (Xamarin productivity tool) working folder 588 | 589 | # Local History for Visual Studio 590 | 591 | # Visual Studio History (VSHistory) files 592 | 593 | # BeatPulse healthcheck temp database 594 | 595 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 596 | 597 | # Ionide (cross platform F# VS Code tools) working folder 598 | 599 | # Fody - auto-generated XML schema 600 | 601 | # VS Code files for those working on multiple tools 602 | 603 | # Local History for Visual Studio Code 604 | 605 | # Windows Installer files from build outputs 606 | 607 | # JetBrains Rider 608 | 609 | ### VisualStudio Patch ### 610 | # Additional files built by Visual Studio 611 | 612 | # End of https://www.toptal.com/developers/gitignore/api/visualstudio,csharp,c++ 613 | 614 | ######################### 615 | 616 | *.zip -------------------------------------------------------------------------------- /app/PowerEvents.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace ParsecVDisplay 5 | { 6 | internal static class PowerEvents 7 | { 8 | static EventHandler _powerModeChanged; 9 | public static event EventHandler PowerModeChanged 10 | { 11 | add 12 | { 13 | _powerModeChanged += value; 14 | if (_powerEventHandler == IntPtr.Zero) 15 | { 16 | var result = Native.PowerRegisterSuspendResumeNotification(2, _dnsp, out _powerEventHandler); 17 | if (result != 0) 18 | throw new Exception("Failed To Register PowerSuspendResumeNotification"); 19 | } 20 | 21 | } 22 | remove 23 | { 24 | _powerModeChanged -= value; 25 | if (_powerModeChanged == null) 26 | { 27 | if (Native.PowerUnregisterSuspendResumeNotification(_powerEventHandler) != 0) 28 | throw new Exception("Failed To Unregister PowerSuspendResumeNotification"); 29 | _powerEventHandler = IntPtr.Zero; 30 | } 31 | } 32 | } 33 | 34 | static IntPtr _powerEventHandler; 35 | static Native.DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS _dnsp = new Native.DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS 36 | { 37 | Callback = OnDeviceNotify, 38 | Context = IntPtr.Zero 39 | }; 40 | 41 | static uint OnDeviceNotify(IntPtr context, uint type, IntPtr setting) 42 | { 43 | _powerModeChanged?.Invoke(null, (PowerBroadcastType)type); 44 | return 0; 45 | } 46 | 47 | public enum PowerBroadcastType 48 | { 49 | PBT_APMQUERYSUSPEND = 0, 50 | // 51 | // Summary: 52 | // The PBT_APMQUERYSUSPEND message is sent to request permission to suspend the 53 | // computer. An application that grants permission should carry out preparations 54 | // for the suspension before returning. Return TRUE to grant the request to suspend. 55 | // To deny the request, return BROADCAST_QUERY_DENY. 56 | PBT_APMQUERYSTANDBY = 1, 57 | // 58 | // Summary: 59 | // [PBT_APMQUERYSUSPENDFAILED is available for use in the operating systems specified 60 | // in the Requirements section. Support for this event was removed in Windows Vista. 61 | // Use SetThreadExecutionState instead.] 62 | // Notifies applications that permission to suspend the computer was denied. This 63 | // event is broadcast if any application or driver returned BROADCAST_QUERY_DENY 64 | // to a previous PBT_APMQUERYSUSPEND event. 65 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 66 | // and lParam parameters are set as described following. 67 | // 68 | // Remarks: 69 | // lParam: Reserved; must be zero. 70 | // No return value. 71 | // Applications typically respond to this event by resuming normal operation. 72 | PBT_APMQUERYSUSPENDFAILED = 2, 73 | // 74 | // Summary: 75 | // The PBT_APMQUERYSUSPENDFAILED message is sent to notify the application that 76 | // suspension was denied by some other application. However, this message is only 77 | // sent when we receive PBT_APMQUERY* before. 78 | PBT_APMQUERYSTANDBYFAILED = 3, 79 | // 80 | // Summary: 81 | // Notifies applications that the computer is about to enter a suspended state. 82 | // This event is typically broadcast when all applications and installable drivers 83 | // have returned TRUE to a previous PBT_APMQUERYSUSPEND event. 84 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 85 | // and lParam parameters are set as described following. 86 | // 87 | // Remarks: 88 | // lParam: Reserved; must be zero. 89 | // No return value. 90 | // An application should process this event by completing all tasks necessary to 91 | // save data. 92 | // The system allows approximately two seconds for an application to handle this 93 | // notification. If an application is still performing operations after its time 94 | // allotment has expired, the system may interrupt the application. 95 | PBT_APMSUSPEND = 4, 96 | // 97 | // Summary: 98 | // Undocumented. 99 | PBT_APMSTANDBY = 5, 100 | // 101 | // Summary: 102 | // [PBT_APMRESUMECRITICAL is available for use in the operating systems specified 103 | // in the Requirements section. Support for this event was removed in Windows Vista. 104 | // Use PBT_APMRESUMEAUTOMATIC instead.] 105 | // Notifies applications that the system has resumed operation. This event can indicate 106 | // that some or all applications did not receive a PBT_APMSUSPEND event. For example, 107 | // this event can be broadcast after a critical suspension caused by a failing battery. 108 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 109 | // and lParam parameters are set as described following. 110 | // 111 | // Remarks: 112 | // lParam: Reserved; must be zero. 113 | // No return value. 114 | // Because a critical suspension occurs without prior notification, resources and 115 | // data previously available may not be present when the application receives this 116 | // event. The application should attempt to restore its state to the best of its 117 | // ability. While in a critical suspension, the system maintains the state of the 118 | // DRAM and local hard disks, but may not maintain net connections. An application 119 | // may need to take action with respect to files that were open on the network before 120 | // critical suspension. 121 | PBT_APMRESUMECRITICAL = 6, 122 | // 123 | // Summary: 124 | // Notifies applications that the system has resumed operation after being suspended. 125 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 126 | // and lParam parameters are set as described following. 127 | // 128 | // Remarks: 129 | // lParam: Reserved; must be zero. 130 | // No return value. 131 | // An application can receive this event only if it received the PBT_APMSUSPEND 132 | // event before the computer was suspended. Otherwise, the application will receive 133 | // a PBT_APMRESUMECRITICAL event. 134 | // If the system wakes due to user activity (such as pressing the power button) 135 | // or if the system detects user interaction at the physical console (such as mouse 136 | // or keyboard input) after waking unattended, the system first broadcasts the PBT_APMRESUMEAUTOMATIC 137 | // event, then it broadcasts the PBT_APMRESUMESUSPEND event. In addition, the system 138 | // turns on the display. Your application should reopen files that it closed when 139 | // the system entered sleep and prepare for user input. 140 | // If the system wakes due to an external wake signal (remote wake), the system 141 | // broadcasts only the PBT_APMRESUMEAUTOMATIC event. The PBT_APMRESUMESUSPEND event 142 | // is not sent. 143 | PBT_APMRESUMESUSPEND = 7, 144 | // 145 | // Summary: 146 | // The PBT_APMRESUMESTANDBY event is broadcast as a notification that the system 147 | // has resumed operation after being standby. 148 | PBT_APMRESUMESTANDBY = 8, 149 | // 150 | // Summary: 151 | // [PBT_APMBATTERYLOW is available for use in the operating systems specified in 152 | // the Requirements section. Support for this event was removed in Windows Vista. 153 | // Use PBT_APMPOWERSTATUSCHANGE instead.] 154 | // Notifies applications that the battery power is low. 155 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 156 | // and lParam parameters are set as described following. 157 | // 158 | // Remarks: 159 | // lParam: Reserved, must be zero. 160 | // No return value. 161 | // This event is broadcast when a system's APM BIOS signals an APM battery low notification. 162 | // Because some APM BIOS implementations do not provide notifications when batteries 163 | // are low, this event may never be broadcast on some computers. 164 | PBT_APMBATTERYLOW = 9, 165 | // 166 | // Summary: 167 | // Notifies applications of a change in the power status of the computer, such as 168 | // a switch from battery power to A/C. The system also broadcasts this event when 169 | // remaining battery power slips below the threshold specified by the user or if 170 | // the battery power changes by a specified percentage. 171 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 172 | // and lParam parameters are set as described following. 173 | // 174 | // Remarks: 175 | // lParam: Reserved; must be zero. 176 | // No return value. 177 | // An application should process this event by calling the GetSystemPowerStatus 178 | // function to retrieve the current power status of the computer. In particular, 179 | // the application should check the ACLineStatus, BatteryFlag, BatteryLifeTime, 180 | // and BatteryLifePercent members of the SYSTEM_POWER_STATUS structure for any changes. 181 | // This event can occur when battery life drops to less than 5 minutes, or when 182 | // the percentage of battery life drops below 10 percent, or if the battery life 183 | // changes by 3 percent. 184 | PBT_APMPOWERSTATUSCHANGE = 10, 185 | // 186 | // Summary: 187 | // [PBT_APMOEMEVENT is available for use in the operating systems specified in the 188 | // Requirements section. Support for this event was removed in Windows Vista.] 189 | // Notifies applications that the APM BIOS has signaled an APM OEM event. 190 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 191 | // and lParam parameters are set as described following. 192 | // 193 | // Remarks: 194 | // lParam: The OEM-defined event code that was signaled by the system's APM BIOS. 195 | // OEM event codes are in the range 0200h - 02FFh. 196 | // No return value. 197 | // Because not all APM BIOS implementations provide OEM event notifications, this 198 | // event may never be broadcast on some computers. 199 | PBT_APMOEMEVENT = 11, 200 | // 201 | // Summary: 202 | // Notifies applications that the system is resuming from sleep or hibernation. 203 | // This event is delivered every time the system resumes and does not indicate whether 204 | // a user is present. 205 | // A window receives this event through the WM_POWERBROADCAST message. The wParam 206 | // and lParam parameters are set as described following. 207 | // 208 | // Remarks: 209 | // lParam: Reserved; must be zero. 210 | // No return value. 211 | // If the system detects any user activity after broadcasting PBT_APMRESUMEAUTOMATIC, 212 | // it will broadcast a PBT_APMRESUMESUSPEND event to let applications know they 213 | // can resume full interaction with the user. 214 | PBT_APMRESUMEAUTOMATIC = 18, 215 | // 216 | // Summary: 217 | // Power setting change event sent with a WM_POWERBROADCAST window message or in 218 | // a HandlerEx notification callback for services. 219 | // 220 | // Remarks: 221 | // lParam: Pointer to a POWERBROADCAST_SETTING structure. 222 | // No return value. 223 | PBT_POWERSETTINGCHANGE = 32787, 224 | ERROR_ERROR = 10101 225 | } 226 | 227 | static class Native 228 | { 229 | [UnmanagedFunctionPointer(CallingConvention.Winapi)] 230 | public delegate uint DeviceNotifyCallbackRoutine(IntPtr Context, uint Type, IntPtr Setting); 231 | 232 | [StructLayout(LayoutKind.Sequential)] 233 | public struct DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS 234 | { 235 | [MarshalAs(UnmanagedType.FunctionPtr)] 236 | public DeviceNotifyCallbackRoutine Callback; 237 | public IntPtr Context; 238 | } 239 | 240 | [DllImport("powrprof.dll", SetLastError = false, ExactSpelling = true)] 241 | public static extern uint PowerRegisterSuspendResumeNotification(int Flags, in DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS Recipient, out IntPtr RegistrationHandle); 242 | 243 | [DllImport("powrprof.dll", SetLastError = false, ExactSpelling = true)] 244 | public static extern uint PowerUnregisterSuspendResumeNotification(IntPtr RegistrationHandle); 245 | } 246 | } 247 | } -------------------------------------------------------------------------------- /app/MirrorWindow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Diagnostics; 4 | using System.Drawing; 5 | using System.Runtime.InteropServices; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using System.Windows.Forms; 9 | 10 | namespace ParsecVDisplay 11 | { 12 | public class MirrorWindow : Form 13 | { 14 | private bool IsMirroring; 15 | private Thread MirrorThread; 16 | private TaskCompletionSource WhenHwnd; 17 | 18 | public MirrorWindow() 19 | { 20 | IsMirroring = false; 21 | WhenHwnd = new TaskCompletionSource(); 22 | 23 | ClientSize = new Size(960, 540); 24 | Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); 25 | } 26 | 27 | protected override void OnClosing(CancelEventArgs e) 28 | { 29 | IsMirroring = false; 30 | MirrorThread?.Join(); 31 | 32 | base.OnClosing(e); 33 | } 34 | 35 | protected override void OnPaint(PaintEventArgs e) 36 | { 37 | } 38 | 39 | protected override void OnPaintBackground(PaintEventArgs e) 40 | { 41 | } 42 | 43 | protected override void OnHandleCreated(EventArgs e) 44 | { 45 | base.OnHandleCreated(e); 46 | WhenHwnd.SetResult(Handle); 47 | } 48 | 49 | public void MirrorScreen(string displayDevice) 50 | { 51 | if (!IsMirroring) 52 | { 53 | IsMirroring = true; 54 | Text = $"Mirror - {displayDevice}"; 55 | 56 | int fps = Config.MirroringFPS; 57 | 58 | MirrorThread = new Thread(() => MirrorWorker(displayDevice, fps)); 59 | MirrorThread.IsBackground = true; 60 | MirrorThread.Start(); 61 | } 62 | } 63 | 64 | private void MirrorWorker(string displayDevice, int fps) 65 | { 66 | var hwnd = WhenHwnd.Task.Result; 67 | 68 | var dcDest = Native.GetDC(hwnd); 69 | var bgBrush = Native.GetStockObject(/*BLACK_BRUSH*/ 4); 70 | 71 | var devmode = default(Native.DEVMODE); 72 | short devmodeSize = (short)Marshal.SizeOf(); 73 | 74 | try 75 | { 76 | var stopwatch = Stopwatch.StartNew(); 77 | double previousTime = stopwatch.Elapsed.TotalMilliseconds; 78 | 79 | double frameTime = 1000.0 / fps; 80 | 81 | while (IsMirroring) 82 | { 83 | double currentTime = stopwatch.Elapsed.TotalMilliseconds; 84 | double elapsedTime = currentTime - previousTime; 85 | 86 | if (elapsedTime >= frameTime) 87 | { 88 | devmode.dmSize = devmodeSize; 89 | 90 | if (Native.EnumDisplaySettings(displayDevice, -1, ref devmode)) 91 | { 92 | var dcScreens = Native.GetDC(IntPtr.Zero); 93 | 94 | var client = GetClientSize(hwnd); 95 | var screen = new Rectangle(devmode.dmPositionX, devmode.dmPositionY, devmode.dmPelsWidth, devmode.dmPelsHeight); 96 | var vp = GetViewport(client.Width, client.Height, screen.Width, screen.Height); 97 | 98 | DrawBackground(dcDest, bgBrush, ref client, ref vp); 99 | DrawScreen(dcDest, dcScreens, ref vp, ref screen); 100 | DrawCursor(dcDest, ref vp, ref screen); 101 | 102 | Native.ReleaseDC(IntPtr.Zero, dcScreens); 103 | } 104 | 105 | previousTime = currentTime; 106 | } 107 | else 108 | { 109 | int sleepTime = (int)(frameTime - elapsedTime); 110 | if (sleepTime > 0) 111 | Thread.Sleep(sleepTime); 112 | } 113 | } 114 | } 115 | finally 116 | { 117 | Native.ReleaseDC(hwnd, dcDest); 118 | Native.DeleteObject(bgBrush); 119 | } 120 | } 121 | 122 | private struct Viewport 123 | { 124 | public int X; 125 | public int Y; 126 | public int Width; 127 | public int Height; 128 | } 129 | 130 | private static void DrawBackground(IntPtr dc, IntPtr brush, ref Size client, ref Viewport vp) 131 | { 132 | var rect = default(Rectangle); 133 | 134 | // fill the excluded rectangles (areas outside the viewport) 135 | // this is the simplest way to avoid flickering without WM_PAINT 136 | 137 | // top excluded rect 138 | if (vp.Y > 0) 139 | { 140 | rect.X = 0; 141 | rect.Y = 0; 142 | rect.Width = client.Width; 143 | rect.Height = vp.Y; 144 | 145 | Native.FillRect(dc, ref rect, brush); 146 | } 147 | 148 | // bottom excluded rect 149 | if (vp.Y + vp.Height < client.Height) 150 | { 151 | rect.X = 0; 152 | rect.Y = vp.Y + vp.Height; 153 | rect.Width = client.Width; 154 | rect.Height = client.Height; 155 | 156 | Native.FillRect(dc, ref rect, brush); 157 | } 158 | 159 | // left excluded rect 160 | if (vp.X > 0) 161 | { 162 | rect.X = 0; 163 | rect.Y = vp.Y; 164 | rect.Width = vp.X; 165 | rect.Height = vp.Height + vp.Y; 166 | 167 | Native.FillRect(dc, ref rect, brush); 168 | } 169 | 170 | // right excluded rect 171 | if (vp.X + vp.Width < client.Width) 172 | { 173 | rect.X = vp.X + vp.Width; 174 | rect.Y = vp.Y; 175 | rect.Width = client.Width; 176 | rect.Height = vp.Height + vp.Y; 177 | 178 | Native.FillRect(dc, ref rect, brush); 179 | } 180 | } 181 | 182 | private static void DrawScreen(IntPtr dc, IntPtr dcSrc, ref Viewport vp, ref Rectangle screen) 183 | { 184 | // set scaling mode 185 | Native.SetStretchBltMode(dc, /*HALFTONE*/ 4); 186 | 187 | // draw the screen 188 | Native.StretchBlt( 189 | dc, 190 | vp.X, vp.Y, vp.Width, vp.Height, 191 | dcSrc, 192 | screen.X, screen.Y, screen.Width, screen.Height, 193 | Native.SRCCOPY 194 | ); 195 | } 196 | 197 | private static void DrawCursor(IntPtr dc, ref Viewport vp, ref Rectangle screen) 198 | { 199 | var cursor = default(Native.CURSORINFO); 200 | cursor.cbSize = Marshal.SizeOf(); 201 | 202 | if (Native.GetCursorInfo(ref cursor) 203 | // cursor must be inside the screen 204 | && screen.Contains(cursor.screenPosX, cursor.screenPosY) 205 | // and visible 206 | && cursor.flags == /*CURSOR_SHOWING*/ 0x1) 207 | { 208 | var iconInfo = default(Native.ICONINFO); 209 | Native.GetIconInfo(cursor.hCursor, ref iconInfo); 210 | 211 | var bmpCursor = default(Native.BITMAP); 212 | Native.GetObject(iconInfo.hbmColor, Marshal.SizeOf(), ref bmpCursor); 213 | 214 | int x = cursor.screenPosX - iconInfo.xHotspot - screen.X; 215 | int y = cursor.screenPosY - iconInfo.yHotspot - screen.Y; 216 | int width = bmpCursor.bmWidth; 217 | int height = bmpCursor.bmHeight; 218 | ScaleCursor(ref vp, screen.Size, ref x, ref y, ref width, ref height); 219 | 220 | Native.DrawIconEx(dc, x, y, cursor.hCursor, width, height, 0, IntPtr.Zero, /*DI_NORMAL*/ 0x3); 221 | } 222 | } 223 | 224 | private static Size GetClientSize(IntPtr hwnd) 225 | { 226 | var rect = new Rectangle(); 227 | Native.GetClientRect(hwnd, ref rect); 228 | return new Size(rect.Width, rect.Height); 229 | } 230 | 231 | private static Viewport GetViewport(int clientWidth, int clientHeight, int rectWidth, int rectHeight) 232 | { 233 | float clientAspect = (float)clientWidth / clientHeight; 234 | float rectAspect = (float)rectWidth / rectHeight; 235 | 236 | int viewportX, viewportY; 237 | int viewportWidth, viewportHeight; 238 | 239 | // compare aspect ratios to determine scaling 240 | if (clientAspect > rectAspect) 241 | { 242 | // client is wider than rect, so scale to fit height 243 | viewportHeight = clientHeight; 244 | viewportWidth = (int)(rectAspect * viewportHeight); 245 | } 246 | else 247 | { 248 | // client is taller than rect, so scale to fit width 249 | viewportWidth = clientWidth; 250 | viewportHeight = (int)(viewportWidth / rectAspect); 251 | } 252 | 253 | // center the viewport 254 | viewportX = (clientWidth - viewportWidth) / 2; 255 | viewportY = (clientHeight - viewportHeight) / 2; 256 | 257 | return new Viewport 258 | { 259 | X = viewportX, 260 | Y = viewportY, 261 | Width = viewportWidth, 262 | Height = viewportHeight, 263 | }; 264 | } 265 | 266 | private static void ScaleCursor(ref Viewport viewport, Size screen, ref int cursorX, ref int cursorY, ref int cursorWidth, ref int cursorHeight) 267 | { 268 | float scaleX = (float)viewport.Width / screen.Width; 269 | float scaleY = (float)viewport.Height / screen.Height; 270 | 271 | cursorWidth = (int)(cursorWidth * scaleX); 272 | cursorHeight = (int)(cursorHeight * scaleY); 273 | 274 | cursorX = viewport.X + (int)(cursorX * scaleX); 275 | cursorY = viewport.Y + (int)(cursorY * scaleY); 276 | } 277 | 278 | private static class Native 279 | { 280 | public const int ENUM_CURRENT_SETTINGS = -1; 281 | public const int SRCCOPY = 0x00CC0020; 282 | 283 | [StructLayout(LayoutKind.Sequential)] 284 | public struct DEVMODE 285 | { 286 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 287 | public string dmDeviceName; 288 | public short dmSpecVersion; 289 | public short dmDriverVersion; 290 | public short dmSize; 291 | public short dmDriverExtra; 292 | public int dmFields; 293 | public int dmPositionX; 294 | public int dmPositionY; 295 | public int dmDisplayOrientation; 296 | public int dmDisplayFixedOutput; 297 | public short dmColor; 298 | public short dmDuplex; 299 | public short dmYResolution; 300 | public short dmTTOption; 301 | public short dmCollate; 302 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 303 | public string dmFormName; 304 | public short dmLogPixels; 305 | public int dmBitsPerPel; 306 | public int dmPelsWidth; 307 | public int dmPelsHeight; 308 | public int dmDisplayFlags; 309 | public int dmDisplayFrequency; 310 | public int dmICMMethod; 311 | public int dmICMIntent; 312 | public int dmMediaType; 313 | public int dmDitherType; 314 | public int dmReserved1; 315 | public int dmReserved2; 316 | public int dmPanningWidth; 317 | public int dmPanningHeight; 318 | } 319 | 320 | [DllImport("user32.dll")] 321 | [return: MarshalAs(UnmanagedType.Bool)] 322 | public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode); 323 | 324 | [DllImport("user32.dll")] 325 | [return: MarshalAs(UnmanagedType.Bool)] 326 | public static extern bool GetClientRect(IntPtr hwnd, ref Rectangle rect); 327 | 328 | [DllImport("user32.dll")] 329 | public static extern IntPtr GetDC(IntPtr hwnd); 330 | 331 | [DllImport("user32.dll")] 332 | public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc); 333 | 334 | [DllImport("gdi32.dll", SetLastError = true)] 335 | public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData); 336 | 337 | [DllImport("gdi32.dll", SetLastError = true)] 338 | public static extern bool DeleteDC(IntPtr hdc); 339 | 340 | [DllImport("gdi32.dll", SetLastError = true)] 341 | public static extern bool DeleteObject(IntPtr hObject); 342 | 343 | [DllImport("gdi32.dll")] 344 | public static extern int SetStretchBltMode(IntPtr hdc, int mode); 345 | 346 | [DllImport("gdi32.dll")] 347 | public static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, 348 | IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, uint dwRop); 349 | 350 | [StructLayout(LayoutKind.Sequential)] 351 | public struct BITMAP 352 | { 353 | public uint bmType; 354 | public int bmWidth; 355 | public int bmHeight; 356 | public int bmWidthBytes; 357 | public short bmPlanes; 358 | public short bmBitsPixel; 359 | public IntPtr bmBits; 360 | } 361 | 362 | [StructLayout(LayoutKind.Sequential)] 363 | public struct ICONINFO 364 | { 365 | public int fIcon; 366 | public int xHotspot; 367 | public int yHotspot; 368 | public IntPtr hbmMask; 369 | public IntPtr hbmColor; 370 | } 371 | 372 | [StructLayout(LayoutKind.Sequential)] 373 | public struct CURSORINFO 374 | { 375 | public int cbSize; 376 | public uint flags; 377 | public IntPtr hCursor; 378 | public int screenPosX; 379 | public int screenPosY; 380 | } 381 | 382 | [DllImport("user32.dll")] 383 | [return: MarshalAs(UnmanagedType.Bool)] 384 | public static extern bool GetIconInfo(IntPtr hIcon, ref ICONINFO piconinfo); 385 | 386 | [DllImport("user32.dll")] 387 | [return: MarshalAs(UnmanagedType.Bool)] 388 | public static extern bool GetCursorInfo(ref CURSORINFO pci); 389 | 390 | [DllImport("gdi32.dll")] 391 | public static extern int GetObject(IntPtr h, int c, ref BITMAP pv); 392 | 393 | [DllImport("user32.dll")] 394 | [return: MarshalAs(UnmanagedType.Bool)] 395 | public static extern bool DrawIconEx(IntPtr hdc, 396 | int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyWidth, 397 | uint istepIfAniCur, IntPtr hbrFlickerFreeDraw, uint diFlags); 398 | 399 | [DllImport("user32.dll")] 400 | public static extern int FillRect(IntPtr hDC, ref Rectangle lprc, IntPtr hbr); 401 | 402 | [DllImport("gdi32.dll")] 403 | public static extern IntPtr GetStockObject(int i); 404 | } 405 | } 406 | } --------------------------------------------------------------------------------