-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeDisplaySwitchingService.cs
More file actions
874 lines (708 loc) · 31.7 KB
/
Copy pathNativeDisplaySwitchingService.cs
File metadata and controls
874 lines (708 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace DisplayDeck;
public sealed class NativeDisplaySwitchingService
{
private const int ENUM_CURRENT_SETTINGS = -1;
private const int ENUM_REGISTRY_SETTINGS = -2;
private const int DM_POSITION = 0x00000020;
private const int DM_BITSPERPEL = 0x00040000;
private const int DM_PELSWIDTH = 0x00080000;
private const int DM_PELSHEIGHT = 0x00100000;
private const int DM_DISPLAYFREQUENCY = 0x00400000;
private const int CDS_UPDATEREGISTRY = 0x00000001;
private const int CDS_SET_PRIMARY = 0x00000010;
private const int CDS_NORESET = 0x10000000;
private const int DISP_CHANGE_SUCCESSFUL = 0;
private const int DISP_CHANGE_RESTART = 1;
private const int DISP_CHANGE_FAILED = -1;
private const int DISP_CHANGE_BADMODE = -2;
private const int DISP_CHANGE_NOTUPDATED = -3;
private const int DISP_CHANGE_BADFLAGS = -4;
private const int DISP_CHANGE_BADPARAM = -5;
private readonly NativeDisplayDiscoveryService _displayDiscoveryService = new();
private readonly CcdDisplayConfigService _ccdDisplayConfigService = new();
public async Task ApplyProfileAsync(DisplayModeProfile profile, int delayMs)
{
profile.EnsureDefaults(profile.Name);
if (profile.EnabledDisplays.Count == 0)
{
throw new InvalidOperationException("At least one display must be enabled for this profile.");
}
if (string.IsNullOrWhiteSpace(profile.PrimaryDisplayName))
{
throw new InvalidOperationException("A primary display must be selected for this profile.");
}
if (!profile.EnabledDisplays.Contains(profile.PrimaryDisplayName, StringComparer.OrdinalIgnoreCase))
{
throw new InvalidOperationException("The primary display must also be enabled for this profile.");
}
if (profile.EnabledDisplays.Count == 1)
{
await ApplySingleDisplayProfileAsync(profile, delayMs);
return;
}
await ApplyMultiDisplayProfileAsync(profile, delayMs);
}
private async Task ApplySingleDisplayProfileAsync(DisplayModeProfile profile, int delayMs)
{
Dictionary<string, DisplayInfo> displayLookup = BuildFreshDisplayLookup();
string enabledKey = profile.EnabledDisplays[0];
DisplayInfo targetDisplay = ResolveDisplay(
profile,
enabledKey,
displayLookup,
"enable"
);
// Step 1:
// Bring the target display online and make it primary using the saved profile mode.
EnableDisplay(targetDisplay, profile, enabledKey);
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2000));
displayLookup = BuildFreshDisplayLookup();
targetDisplay = ResolveDisplay(
profile,
enabledKey,
displayLookup,
"set primary"
);
SetPrimaryDisplay(targetDisplay, profile, enabledKey);
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2500));
// Step 2:
// Use Windows' built-in "second screen only" pathway.
// On this setup, /external gets us very close: Sim + Drawing active, Sim primary.
// That is better than trying to detach from the full 3-monitor extended layout.
await WindowsDisplaySwitchService.ExternalOnlyAsync(delayMs);
// Step 3:
// Re-resolve the target after Windows has changed the topology.
displayLookup = BuildFreshDisplayLookup();
targetDisplay = ResolveDisplay(
profile,
enabledKey,
displayLookup,
"finalise single-display profile"
);
SetPrimaryDisplay(targetDisplay, profile, enabledKey);
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2500));
// Step 4:
// Ask CCD to keep only the profile target active.
string ccdError = "";
var targetHardwareCodes = new List<string>();
if (!string.IsNullOrWhiteSpace(targetDisplay.MonitorHardwareCode))
{
targetHardwareCodes.Add(targetDisplay.MonitorHardwareCode);
}
if (!string.IsNullOrWhiteSpace(targetDisplay.StableDisplayId))
{
targetHardwareCodes.Add(targetDisplay.StableDisplayId);
}
bool ccdSucceeded = _ccdDisplayConfigService.TryKeepOnlyDisplayHardwareCodesEnabled(
targetHardwareCodes,
out ccdError
);
if (!ccdSucceeded)
{
ccdSucceeded = _ccdDisplayConfigService.TryKeepOnlyDisplaysEnabled(
new List<string> { targetDisplay.DisplayName },
out ccdError
);
}
if (ccdSucceeded)
{
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2500));
// Do not trust a successful CCD return blindly.
// Some Windows/GPU combinations report success but still leave one extra
// display attached. Verify the actual active displays before returning.
displayLookup = BuildFreshDisplayLookup();
bool onlyTargetIsActive = displayLookup
.Values
.GroupBy(display => display.DisplayName, StringComparer.OrdinalIgnoreCase)
.Select(group => group.First())
.Where(display => display.IsActive)
.All(display => DisplayLooksLikeProfileDisplay(profile, enabledKey, display));
if (onlyTargetIsActive)
{
return;
}
}
// Step 5:
// Final fallback: after /external/CCD, disconnect any remaining active display that is not the target.
displayLookup = BuildFreshDisplayLookup();
var failedDisables = new List<string>();
IReadOnlyList<DisplayInfo> activeDisplays = displayLookup
.Values
.GroupBy(display => display.DisplayName, StringComparer.OrdinalIgnoreCase)
.Select(group => group.First())
.Where(display => display.IsActive)
.ToList();
foreach (DisplayInfo activeDisplay in activeDisplays)
{
if (DisplayLooksLikeProfileDisplay(profile, enabledKey, activeDisplay))
{
continue;
}
int disableResult = TryDisableDisplayWithFallbacks(activeDisplay.DisplayName);
if (!IsSuccessfulResult(disableResult))
{
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2000));
disableResult = TryDisableDisplayWithFallbacks(activeDisplay.DisplayName);
}
if (!IsSuccessfulResult(disableResult))
{
string label = !string.IsNullOrWhiteSpace(activeDisplay.DisplayLabel)
? activeDisplay.DisplayLabel
: activeDisplay.DisplayName;
failedDisables.Add(label);
}
else
{
ApplyAllChanges();
await Task.Delay(delayMs);
}
}
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2000));
if (failedDisables.Count > 0)
{
throw new InvalidOperationException(
"DisplayDeck switched to the single-display profile as far as Windows allowed, but one or more extra displays stayed on:\n\n" +
string.Join("\n", failedDisables.Select(name => "- " + name)) +
"\n\nWindows /external worked partially on this PC, but the final detach step was refused. You can manually disconnect the remaining display in Windows Display Settings, or test whether changing the GPU/Windows display numbering makes /external select only the Sim display."
);
}
}
private async Task ApplyMultiDisplayProfileAsync(DisplayModeProfile profile, int delayMs)
{
// If coming back from a single-display profile such as Sim Mode,
// Windows may have fully disconnected the desk displays.
// Ask Windows to extend first, wait, then apply the saved profile layout.
await WindowsDisplaySwitchService.ExtendDesktopAsync(delayMs);
Dictionary<string, DisplayInfo> displayLookup = BuildFreshDisplayLookup();
DisplayInfo primaryDisplay = ResolveDisplay(profile, profile.PrimaryDisplayName, displayLookup, "enable primary");
EnableDisplay(primaryDisplay, profile, profile.PrimaryDisplayName);
ApplyAllChanges();
await Task.Delay(delayMs);
displayLookup = BuildFreshDisplayLookup();
primaryDisplay = ResolveDisplay(profile, profile.PrimaryDisplayName, displayLookup, "set primary");
SetPrimaryDisplay(primaryDisplay, profile, profile.PrimaryDisplayName);
ApplyAllChanges();
await Task.Delay(delayMs);
displayLookup = BuildFreshDisplayLookup();
foreach (string displayKey in profile.EnabledDisplays)
{
if (string.Equals(displayKey, profile.PrimaryDisplayName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
DisplayInfo display = ResolveDisplay(profile, displayKey, displayLookup, "enable");
EnableDisplay(display, profile, displayKey);
ApplyAllChanges();
await Task.Delay(delayMs);
displayLookup = BuildFreshDisplayLookup();
}
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2500));
displayLookup = BuildFreshDisplayLookup();
var failedDisables = new List<string>();
foreach (string displayKey in profile.DisabledDisplays)
{
if (string.Equals(displayKey, profile.PrimaryDisplayName, StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (!TryResolveDisplay(profile, displayKey, displayLookup, out DisplayInfo? displayToDisable))
{
continue;
}
int disableResult = TryDisableDisplayWithFallbacks(displayToDisable.DisplayName);
if (!IsSuccessfulResult(disableResult))
{
ApplyAllChanges();
await Task.Delay(Math.Max(delayMs, 2500));
displayLookup = BuildFreshDisplayLookup();
if (TryResolveDisplay(profile, displayKey, displayLookup, out DisplayInfo? retryDisplayToDisable))
{
disableResult = TryDisableDisplayWithFallbacks(retryDisplayToDisable.DisplayName);
displayToDisable = retryDisplayToDisable;
}
}
if (!IsSuccessfulResult(disableResult))
{
failedDisables.Add(GetDisplayLabelForError(profile, displayKey, displayToDisable));
continue;
}
ApplyAllChanges();
await Task.Delay(delayMs);
displayLookup = BuildFreshDisplayLookup();
}
ApplyAllChanges();
await Task.Delay(delayMs);
if (failedDisables.Count > 0)
{
throw new InvalidOperationException(
"DisplayDeck switched the main profile display, but Windows refused to turn off one or more old displays:\n\n" +
string.Join("\n", failedDisables.Select(name => "- " + name)) +
"\n\nTry clicking the profile button once more after Windows settles. If it still happens, open Windows Display Settings, confirm the layout, click Refresh in DisplayDeck, and save the profile again."
);
}
}
private Dictionary<string, DisplayInfo> BuildFreshDisplayLookup()
{
IReadOnlyList<DisplayInfo> allDisplays = _displayDiscoveryService.GetDisplays();
return BuildDisplayLookup(allDisplays);
}
private static Dictionary<string, DisplayInfo> BuildDisplayLookup(IReadOnlyList<DisplayInfo> displays)
{
var lookup = new Dictionary<string, DisplayInfo>(StringComparer.OrdinalIgnoreCase);
foreach (DisplayInfo display in displays)
{
AddLookupKey(lookup, display.DisplayName, display);
AddLookupKey(lookup, display.StableDisplayId, display);
AddLookupKey(lookup, display.MonitorHardwareCode, display);
AddLookupKey(lookup, display.IdentityKey, display);
}
return lookup;
}
private static void AddLookupKey(Dictionary<string, DisplayInfo> lookup, string key, DisplayInfo display)
{
if (string.IsNullOrWhiteSpace(key))
{
return;
}
if (!lookup.ContainsKey(key))
{
lookup[key] = display;
}
}
private static DisplayInfo ResolveDisplay(DisplayModeProfile profile, string displayKey, Dictionary<string, DisplayInfo> lookup, string action)
{
if (TryResolveDisplay(profile, displayKey, lookup, out DisplayInfo? display))
{
return display;
}
throw new InvalidOperationException(
$"DisplayDeck could not {action} {GetDisplayLabelForError(profile, displayKey)}.\n\n" +
"Windows is not currently reporting this saved monitor as available, and there is no saved Windows device path to fall back to."
);
}
private static bool TryResolveDisplay(DisplayModeProfile profile, string displayKey, Dictionary<string, DisplayInfo> lookup, out DisplayInfo display)
{
display = null!;
if (string.IsNullOrWhiteSpace(displayKey))
{
return false;
}
SavedDisplayDetails? savedDetails = profile.GetSavedDisplayDetails(displayKey);
if (lookup.TryGetValue(displayKey, out DisplayInfo? directMatch) && IsReasonableDisplayMatch(directMatch, savedDetails))
{
display = directMatch;
return true;
}
if (savedDetails is not null)
{
if (!string.IsNullOrWhiteSpace(savedDetails.StableDisplayId) && lookup.TryGetValue(savedDetails.StableDisplayId, out DisplayInfo? stableMatch) && IsReasonableDisplayMatch(stableMatch, savedDetails))
{
display = stableMatch;
return true;
}
if (!string.IsNullOrWhiteSpace(savedDetails.MonitorHardwareCode) && lookup.TryGetValue(savedDetails.MonitorHardwareCode, out DisplayInfo? hardwareMatch) && IsReasonableDisplayMatch(hardwareMatch, savedDetails))
{
display = hardwareMatch;
return true;
}
if (!string.IsNullOrWhiteSpace(savedDetails.DisplayName) && lookup.TryGetValue(savedDetails.DisplayName, out DisplayInfo? displayNameMatch) && IsReasonableDisplayMatch(displayNameMatch, savedDetails))
{
display = displayNameMatch;
return true;
}
DisplayInfo? modeMatch = FindBestModeMatch(lookup.Values, savedDetails);
if (modeMatch is not null)
{
display = modeMatch;
return true;
}
if (!string.IsNullOrWhiteSpace(savedDetails.DisplayName))
{
display = new DisplayInfo
{
DisplayName = savedDetails.DisplayName,
StableDisplayId = savedDetails.StableDisplayId,
MonitorHardwareCode = savedDetails.MonitorHardwareCode,
FriendlyName = savedDetails.Label,
MonitorName = savedDetails.Label,
Width = savedDetails.Width,
Height = savedDetails.Height,
Frequency = savedDetails.Frequency,
PositionX = savedDetails.PositionX,
PositionY = savedDetails.PositionY,
IsActive = false,
IsPrimary = false
};
return true;
}
}
return false;
}
private static bool IsReasonableDisplayMatch(DisplayInfo display, SavedDisplayDetails? savedDetails)
{
if (savedDetails is null || !savedDetails.HasUsableMode)
{
return true;
}
if (display.Width <= 0 || display.Height <= 0)
{
return true;
}
if (display.Width != savedDetails.Width || display.Height != savedDetails.Height)
{
return false;
}
if (display.Frequency > 0 && savedDetails.Frequency > 0 && Math.Abs(display.Frequency - savedDetails.Frequency) > 2)
{
return false;
}
return true;
}
private static DisplayInfo? FindBestModeMatch(IEnumerable<DisplayInfo> displays, SavedDisplayDetails savedDetails)
{
if (!savedDetails.HasUsableMode)
{
return null;
}
return displays
.Where(display =>
display.Width == savedDetails.Width &&
display.Height == savedDetails.Height &&
(display.Frequency <= 0 || savedDetails.Frequency <= 0 || Math.Abs(display.Frequency - savedDetails.Frequency) <= 2)
)
.OrderBy(display => Math.Abs(display.PositionX - savedDetails.PositionX) + Math.Abs(display.PositionY - savedDetails.PositionY))
.FirstOrDefault();
}
private static bool DisplayLooksLikeProfileDisplay(DisplayModeProfile profile, string profileDisplayKey, DisplayInfo activeDisplay)
{
if (string.Equals(profileDisplayKey, activeDisplay.IdentityKey, StringComparison.OrdinalIgnoreCase) ||
string.Equals(profileDisplayKey, activeDisplay.StableDisplayId, StringComparison.OrdinalIgnoreCase) ||
string.Equals(profileDisplayKey, activeDisplay.MonitorHardwareCode, StringComparison.OrdinalIgnoreCase) ||
string.Equals(profileDisplayKey, activeDisplay.DisplayName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
SavedDisplayDetails? savedDetails = profile.GetSavedDisplayDetails(profileDisplayKey);
if (savedDetails is null)
{
return false;
}
if (!string.IsNullOrWhiteSpace(savedDetails.DisplayName) && string.Equals(savedDetails.DisplayName, activeDisplay.DisplayName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (savedDetails.HasUsableMode && activeDisplay.Width == savedDetails.Width && activeDisplay.Height == savedDetails.Height && (activeDisplay.Frequency <= 0 || savedDetails.Frequency <= 0 || Math.Abs(activeDisplay.Frequency - savedDetails.Frequency) <= 2))
{
return true;
}
return false;
}
private static SavedDisplayDetails? GetSavedDetailsForDisplay(DisplayModeProfile profile, string profileDisplayKey, DisplayInfo resolvedDisplay)
{
return profile.GetSavedDisplayDetailsForAnyKey(
profileDisplayKey,
resolvedDisplay.IdentityKey,
resolvedDisplay.StableDisplayId,
resolvedDisplay.MonitorHardwareCode,
resolvedDisplay.DisplayName
);
}
private static void EnableDisplay(DisplayInfo display, DisplayModeProfile profile, string profileDisplayKey)
{
SavedDisplayDetails? savedDetails = GetSavedDetailsForDisplay(profile, profileDisplayKey, display);
DEVMODE baseMode = ReadBestDisplayMode(display, savedDetails);
var attempts = new List<DEVMODE>();
if (savedDetails is not null && savedDetails.HasUsableMode)
{
DEVMODE savedWithRefresh = baseMode;
savedWithRefresh.dmPelsWidth = savedDetails.Width;
savedWithRefresh.dmPelsHeight = savedDetails.Height;
savedWithRefresh.dmDisplayFrequency = savedDetails.Frequency;
savedWithRefresh.dmPosition.x = savedDetails.PositionX;
savedWithRefresh.dmPosition.y = savedDetails.PositionY;
EnsureUsableModeValues(ref savedWithRefresh);
savedWithRefresh.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
attempts.Add(savedWithRefresh);
DEVMODE savedNoRefresh = savedWithRefresh;
savedNoRefresh.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
attempts.Add(savedNoRefresh);
}
DEVMODE currentWithPosition = baseMode;
EnsureUsableModeValues(ref currentWithPosition);
if (savedDetails is not null)
{
currentWithPosition.dmPosition.x = savedDetails.PositionX;
currentWithPosition.dmPosition.y = savedDetails.PositionY;
}
currentWithPosition.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
attempts.Add(currentWithPosition);
DEVMODE currentNoRefresh = currentWithPosition;
currentNoRefresh.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
attempts.Add(currentNoRefresh);
int lastResult = DISP_CHANGE_FAILED;
foreach (DEVMODE attempt in attempts)
{
DEVMODE mode = attempt;
int result = TryApplyDisplayMode(display.DisplayName, mode, setPrimary: false);
if (IsSuccessfulResult(result))
{
return;
}
lastResult = result;
}
EnsureSuccess(lastResult, "enable", GetDisplayLabelForError(profile, profileDisplayKey, display));
}
private static void SetPrimaryDisplay(DisplayInfo display, DisplayModeProfile profile, string profileDisplayKey)
{
SavedDisplayDetails? savedDetails = GetSavedDetailsForDisplay(profile, profileDisplayKey, display);
DEVMODE baseMode = ReadBestDisplayMode(display, savedDetails);
var attempts = new List<DEVMODE>();
if (savedDetails is not null && savedDetails.HasUsableMode)
{
DEVMODE savedWithRefresh = baseMode;
savedWithRefresh.dmPelsWidth = savedDetails.Width;
savedWithRefresh.dmPelsHeight = savedDetails.Height;
savedWithRefresh.dmDisplayFrequency = savedDetails.Frequency;
savedWithRefresh.dmPosition.x = 0;
savedWithRefresh.dmPosition.y = 0;
EnsureUsableModeValues(ref savedWithRefresh);
savedWithRefresh.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
attempts.Add(savedWithRefresh);
DEVMODE savedNoRefresh = savedWithRefresh;
savedNoRefresh.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
attempts.Add(savedNoRefresh);
}
DEVMODE currentPrimary = baseMode;
EnsureUsableModeValues(ref currentPrimary);
currentPrimary.dmPosition.x = 0;
currentPrimary.dmPosition.y = 0;
currentPrimary.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
attempts.Add(currentPrimary);
DEVMODE currentPrimaryNoRefresh = currentPrimary;
currentPrimaryNoRefresh.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
attempts.Add(currentPrimaryNoRefresh);
DEVMODE primaryPositionOnly = CreateDevMode();
primaryPositionOnly.dmPosition.x = 0;
primaryPositionOnly.dmPosition.y = 0;
primaryPositionOnly.dmFields = DM_POSITION;
attempts.Add(primaryPositionOnly);
int lastResult = DISP_CHANGE_FAILED;
foreach (DEVMODE attempt in attempts)
{
DEVMODE mode = attempt;
int result = TryApplyDisplayMode(display.DisplayName, mode, setPrimary: true);
if (IsSuccessfulResult(result))
{
return;
}
lastResult = result;
}
EnsureSuccess(lastResult, "set primary", GetDisplayLabelForError(profile, profileDisplayKey, display));
}
private static DEVMODE ReadBestDisplayMode(DisplayInfo display, SavedDisplayDetails? savedDetails)
{
DEVMODE devMode = CreateDevMode();
bool gotSettings =
EnumDisplaySettingsEx(display.DisplayName, ENUM_CURRENT_SETTINGS, ref devMode, 0) ||
EnumDisplaySettingsEx(display.DisplayName, ENUM_REGISTRY_SETTINGS, ref devMode, 0);
if (!gotSettings && savedDetails is not null)
{
devMode.dmPelsWidth = savedDetails.Width > 0 ? savedDetails.Width : 1920;
devMode.dmPelsHeight = savedDetails.Height > 0 ? savedDetails.Height : 1080;
devMode.dmDisplayFrequency = savedDetails.Frequency > 0 ? savedDetails.Frequency : 60;
devMode.dmPosition.x = savedDetails.PositionX;
devMode.dmPosition.y = savedDetails.PositionY;
devMode.dmBitsPerPel = 32;
}
return devMode;
}
private static void EnsureUsableModeValues(ref DEVMODE devMode)
{
if (devMode.dmPelsWidth <= 0 || devMode.dmPelsHeight <= 0)
{
devMode.dmPelsWidth = 1920;
devMode.dmPelsHeight = 1080;
}
if (devMode.dmDisplayFrequency <= 0)
{
devMode.dmDisplayFrequency = 60;
}
if (devMode.dmBitsPerPel <= 0)
{
devMode.dmBitsPerPel = 32;
}
}
private static int TryApplyDisplayMode(string displayName, DEVMODE devMode, bool setPrimary)
{
int flags = CDS_UPDATEREGISTRY | CDS_NORESET;
if (setPrimary)
{
flags |= CDS_SET_PRIMARY;
}
return ChangeDisplaySettingsEx(displayName, ref devMode, IntPtr.Zero, flags, IntPtr.Zero);
}
private static int TryDisableDisplayWithFallbacks(string displayName)
{
var attempts = new List<(DEVMODE Mode, int Flags)>();
DEVMODE fullDisableQueued = CreateDevMode();
fullDisableQueued.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT;
fullDisableQueued.dmPosition.x = 0;
fullDisableQueued.dmPosition.y = 0;
fullDisableQueued.dmPelsWidth = 0;
fullDisableQueued.dmPelsHeight = 0;
attempts.Add((fullDisableQueued, CDS_UPDATEREGISTRY | CDS_NORESET));
DEVMODE sizeOnlyQueued = CreateDevMode();
sizeOnlyQueued.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
sizeOnlyQueued.dmPelsWidth = 0;
sizeOnlyQueued.dmPelsHeight = 0;
attempts.Add((sizeOnlyQueued, CDS_UPDATEREGISTRY | CDS_NORESET));
DEVMODE fullDisableImmediate = fullDisableQueued;
attempts.Add((fullDisableImmediate, CDS_UPDATEREGISTRY));
DEVMODE sizeOnlyImmediate = sizeOnlyQueued;
attempts.Add((sizeOnlyImmediate, CDS_UPDATEREGISTRY));
DEVMODE fullDisableTemporary = fullDisableQueued;
attempts.Add((fullDisableTemporary, 0));
int lastResult = DISP_CHANGE_FAILED;
for (int i = 0; i < attempts.Count; i++)
{
DEVMODE mode = attempts[i].Mode;
int flags = attempts[i].Flags;
int result = ChangeDisplaySettingsEx(displayName, ref mode, IntPtr.Zero, flags, IntPtr.Zero);
if (IsSuccessfulResult(result))
{
return result;
}
lastResult = result;
}
return lastResult;
}
private static void ApplyAllChanges()
{
int result = ChangeDisplaySettingsEx(null, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero);
EnsureSuccess(result, "apply display changes", "");
}
private static bool IsSuccessfulResult(int result)
{
return result == DISP_CHANGE_SUCCESSFUL || result == DISP_CHANGE_RESTART;
}
private static void EnsureSuccess(int result, string action, string displayName)
{
if (IsSuccessfulResult(result))
{
return;
}
string displayText = string.IsNullOrWhiteSpace(displayName) ? "the selected display setup" : displayName;
if (result == DISP_CHANGE_FAILED)
{
throw new InvalidOperationException(
$"DisplayDeck could not {action} {displayText}.\n\n" +
"Windows failed the display change request. Try again after Windows settles, or confirm the layout in Windows Display Settings."
);
}
if (result == DISP_CHANGE_BADMODE)
{
throw new InvalidOperationException(
$"DisplayDeck could not {action} {displayText}.\n\n" +
"Windows rejected the display mode for this monitor."
);
}
if (result == DISP_CHANGE_NOTUPDATED)
{
throw new InvalidOperationException($"DisplayDeck could not {action} {displayText}. Windows could not update the display registry settings.");
}
if (result == DISP_CHANGE_BADFLAGS)
{
throw new InvalidOperationException($"DisplayDeck could not {action} {displayText}. Windows rejected the display command flags.");
}
if (result == DISP_CHANGE_BADPARAM)
{
throw new InvalidOperationException($"DisplayDeck could not {action} {displayText}. Windows rejected the display command.");
}
throw new InvalidOperationException(
$"DisplayDeck could not {action} {displayText}.\n\n" +
$"Windows display API returned error code {result}."
);
}
private static string GetDisplayLabelForError(DisplayModeProfile profile, string displayKey)
{
return profile.GetSavedOrRawDisplayLabel(displayKey);
}
private static string GetDisplayLabelForError(DisplayModeProfile profile, string displayKey, DisplayInfo display)
{
string savedLabel = profile.GetSavedOrRawDisplayLabel(displayKey);
if (!string.IsNullOrWhiteSpace(savedLabel) && !string.Equals(savedLabel, displayKey, StringComparison.OrdinalIgnoreCase))
{
return savedLabel;
}
if (!string.IsNullOrWhiteSpace(display.DisplayLabel))
{
return display.DisplayLabel;
}
return displayKey;
}
private static DEVMODE CreateDevMode()
{
var devMode = new DEVMODE();
devMode.dmSize = (short)Marshal.SizeOf<DEVMODE>();
return devMode;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern bool EnumDisplaySettingsEx(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int ChangeDisplaySettingsEx(string? lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int ChangeDisplaySettingsEx(string? lpszDeviceName, IntPtr lpDevMode, IntPtr hwnd, int dwflags, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct POINTL
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public POINTL dmPosition;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
}