Repository navigation
Expand file tree
/
Copy pathControlChoiceViewController.swift
More file actions
1256 lines (1074 loc) · 46.5 KB
/
Copy pathControlChoiceViewController.swift
File metadata and controls
1256 lines (1074 loc) · 46.5 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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ControlChoiceViewController.swift
// Lunar
//
// Created by Alin Panaitiu on 01.10.2021.
// Copyright © 2021 Alin. All rights reserved.
//
import Cocoa
import Combine
import Foundation
// MARK: - GradientView
final class GradientView: NSView {
override var wantsDefaultClipping: Bool { false }
@IBInspectable var firstColor: NSColor = .clear {
didSet {
updateView()
}
}
@IBInspectable var secondColor: NSColor = .clear {
didSet {
updateView()
}
}
func updateView() {
let layer = CAGradientLayer()
layer.colors = [firstColor, secondColor].map(\.cgColor)
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 0.5, y: 0.45)
if let blur = CIFilter(name: "CIGaussianBlur", parameters: ["inputRadius": 20]) {
layer.filters = [blur]
}
layer.masksToBounds = false
self.layer = layer
}
}
// MARK: - LunarTestViewController
final class LunarTestViewController: NSViewController {
@IBOutlet var label: NSTextField!
@objc dynamic var lunarTestText = "Checking Monitor"
var taskKey = "lunarTestHighlighter"
var lunarTestHighlighter: Repeater?
override func viewDidAppear() {
let end = {
for d in DC.displays.values {
d.testWindowController?.close()
d.testWindowController = nil
}
}
lunarTestHighlighter = Repeater(
every: 2,
name: taskKey,
onFinish: end,
onCancel: end
) { [weak self] in
guard let self else {
end()
return
}
label.transition(1.5, easing: .easeInOutCubic)
if label.textColor == .white {
label.textColor = darkMauve
} else {
label.textColor = .white
}
}
}
override func viewDidLoad() {
label.textColor = .white
}
}
// MARK: - ControlReadResult
struct ControlReadResult {
static var onlyBrightnessWorked = ControlReadResult(brightness: true, contrast: false, volume: false)
static var allWorked = ControlReadResult(brightness: true, contrast: true, volume: true)
static var noneWorked = ControlReadResult(brightness: false, contrast: false, volume: false)
let brightness: Bool
let contrast: Bool
let volume: Bool
var all: Bool { brightness && contrast }
}
// MARK: - ControlWriteResult
struct ControlWriteResult {
static var onlyBrightnessWorked = ControlWriteResult(brightness: true, contrast: false, volume: false)
static var allWorked = ControlWriteResult(brightness: true, contrast: true, volume: true)
static var noneWorked = ControlWriteResult(brightness: false, contrast: false, volume: false)
let brightness: Bool
let contrast: Bool
let volume: Bool
var all: Bool { brightness && contrast }
}
// MARK: - ControlResult
struct ControlResult {
static var onlyBrightnessWorked = ControlResult(type: .appleNative, read: .onlyBrightnessWorked, write: .onlyBrightnessWorked)
static var allWorked = ControlResult(type: .ddc, read: .allWorked, write: .allWorked)
static var noneWorked = ControlResult(type: .ddc, read: .noneWorked, write: .noneWorked)
let type: DisplayControl
let read: ControlReadResult
let write: ControlWriteResult
var all: Bool { read.all && write.all }
}
// MARK: - ControlChoiceViewController
final class ControlChoiceViewController: NSViewController {
var cancelled = false
@IBOutlet var displayImage: DisplayImage?
@IBOutlet var _controlButton: NSButton!
@IBOutlet var _ddcBlockersButton: NSButton!
@IBOutlet var actionLabel: NSTextField!
@IBOutlet var actionInfo: NSTextField!
@IBOutlet var displayName: DisplayName!
@IBOutlet var brightnessField: ScrollableTextField!
@IBOutlet var contrastField: ScrollableTextField!
@IBOutlet var brightnessCaption: NSTextField!
@IBOutlet var contrastCaption: NSTextField!
@IBOutlet var brightnessReadResult: NSTextField!
@IBOutlet var contrastReadResult: NSTextField!
@IBOutlet var volumeReadResult: NSTextField!
@IBOutlet var brightnessWriteResult: NSTextField!
@IBOutlet var contrastWriteResult: NSTextField!
@IBOutlet var volumeWriteResult: NSTextField!
@IBOutlet var volumeSlider: VolumeSlider!
@IBOutlet var actionButton: Button!
@IBOutlet var noButton: Button!
@IBOutlet var yesButton: Button!
@IBOutlet var skipButton: Button!
var observers: Set<AnyCancellable> = []
@objc dynamic var progress = 0.0
@objc dynamic var volume: Double = 50
var actionLabelColor = white.blended(withFraction: 0.2, of: lunarYellow)
var actionInfoColor = white
var wakeObserver: Cancellable?
var screenObserver: Cancellable?
var semaphore = DispatchSemaphore(value: 0, name: "Control Choice")
var currentDisplay: Display?
var displayProgressStep: Double = 0
var controlProgressStep: Double = 0
var progressForLastDisplay: Double = 0
var progressForDisplay: Double = 0
var didAppear = false
var controlButton: HelpButton? { _controlButton as? HelpButton }
var ddcBlockersButton: OnboardingHelpButton? { _ddcBlockersButton as? OnboardingHelpButton }
func info(_ text: String, color: NSColor) {
mainThread {
actionLabel.stringValue = text
actionLabel.transition(0.9, easing: .easeOutCubic)
actionLabel.textColor = color
}
mainAsyncAfter(ms: 1000) { [weak self] in
guard let self else { return }
actionLabel.transition(1.0, easing: .easeOutCubic)
actionLabel.textColor = actionLabelColor
}
}
func hideAction() {
mainThread {
if let ddcBlockersButton {
ddcBlockersButton.transition(0.8, easing: .easeOutExpo)
ddcBlockersButton.alphaValue = 0.0
ddcBlockersButton.close()
ddcBlockersButton.isEnabled = false
ddcBlockersButton.isHidden = true
}
actionInfo.transition(0.8, easing: .easeOutExpo)
actionInfo.alphaValue = 0.0
actionButton.transition(0.8, easing: .easeOutExpo)
actionButton.alphaValue = 0.0
actionButton.isEnabled = false
actionButton.onClick = nil
}
}
func hideQuestion() {
mainThread {
if let ddcBlockersButton {
ddcBlockersButton.transition(0.8, easing: .easeOutExpo)
ddcBlockersButton.alphaValue = 0.0
ddcBlockersButton.close()
ddcBlockersButton.isEnabled = false
ddcBlockersButton.isHidden = true
}
actionInfo.transition(0.8, easing: .easeOutExpo)
actionInfo.alphaValue = 0.0
noButton.transition(0.8, easing: .easeOutExpo)
noButton.alphaValue = 0.0
noButton.isEnabled = false
noButton.onClick = nil
yesButton.transition(0.8, easing: .easeOutExpo)
yesButton.alphaValue = 0.0
yesButton.isEnabled = false
yesButton.onClick = nil
}
}
func waitForAction(_ text: String, buttonColor: NSColor, buttonText: NSAttributedString, action: @escaping (() -> Void)) {
mainAsyncAfter(ms: 1100) { [weak self] in
guard let self else { return }
actionInfo.transition(0.5, easing: .easeInOutExpo)
actionInfo.textColor = actionInfoColor
}
mainThread {
actionInfo.stringValue = text
actionInfo.transition(0.8, easing: .easeOutCubic)
actionInfo.alphaValue = 1.0
actionInfo.transition(1.0, easing: .easeOutCubic)
actionInfo.textColor = peach
actionButton.bg = buttonColor
actionButton.attributedTitle = buttonText
actionButton.isEnabled = true
actionButton.transition(0.8, easing: .easeOutCubic)
actionButton.alphaValue = 1.0
}
actionButton.onClick = { [weak self] in
log.info("Clicked '\(buttonText.string)' on '\(text)'")
guard let self else {
self?.semaphore.signal()
return
}
semaphore.signal()
hideAction()
}
semaphore.wait(for: 0)
guard !cancelled else { return }
action()
}
func askQuestion(
_ question: String,
yesButtonText: String = "Yes",
noButtonText: String = "No",
ddcBlockerText: String? = nil,
answer: @escaping ((Bool) -> Void)
) {
mainAsyncAfter(ms: 1100) { [weak self] in
guard let self else { return }
actionInfo.transition(0.8, easing: .easeOutCubic)
actionInfo.textColor = actionInfoColor
}
mainThread {
actionInfo.stringValue = question
actionInfo.transition(0.8, easing: .easeOutCubic)
actionInfo.alphaValue = 1.0
actionInfo.transition(1.0, easing: .easeOutCubic)
actionInfo.textColor = peach
noButton.transition(0.8, easing: .easeOutCubic)
noButton.alphaValue = 1.0
noButton.isEnabled = true
noButton.attributedTitle = noButtonText.withTextColor(white)
yesButton.transition(0.8, easing: .easeOutCubic)
yesButton.alphaValue = 1.0
yesButton.isEnabled = true
yesButton.attributedTitle = yesButtonText.withTextColor(white)
if let ddcBlockerText, let ddcBlockersButton {
ddcBlockersButton.helpText = ddcBlockerText
ddcBlockersButton.isEnabled = true
ddcBlockersButton.isHidden = false
ddcBlockersButton.transition(0.8, easing: .easeOutCubic)
ddcBlockersButton.alphaValue = 1.0
ddcBlockersButton.open(edge: .maxX)
}
}
var answerResult = false
noButton.onClick = { [weak self] in
log.info("Answered 'no' to '\(question)'")
guard let self else {
self?.semaphore.signal()
return
}
answerResult = false
semaphore.signal()
hideQuestion()
}
yesButton.onClick = { [weak self] in
log.info("Answered 'yes' to '\(question)'")
guard let self else {
self?.semaphore.signal()
return
}
answerResult = true
semaphore.signal()
hideQuestion()
}
semaphore.wait(for: 0)
guard !cancelled else { return }
answer(answerResult)
}
func setBrightness(_ brightness: Brightness) {
mainThread {
if brightnessField.alphaValue == 0 {
brightnessField.transition(1.0, easing: .easeOutExpo)
brightnessField.alphaValue = 1.0
brightnessCaption.transition(1.0, easing: .easeOutExpo)
brightnessCaption.alphaValue = 1.0
}
}
for br in stride(
from: brightnessField.integerValue,
through: brightness.i,
by: brightnessField.integerValue < brightness.i ? 1 : -1
) {
mainThread { brightnessField.integerValue = br }
guard wait(0.01) else { return }
}
}
func setContrast(_ contrast: Contrast) {
mainThread {
if contrastField.alphaValue == 0 {
contrastField.transition(1.0, easing: .easeOutExpo)
contrastField.alphaValue = 1.0
contrastCaption.transition(1.0, easing: .easeOutExpo)
contrastCaption.alphaValue = 1.0
}
}
for cr in stride(from: contrastField.integerValue, through: contrast.i, by: contrastField.integerValue < contrast.i ? 1 : -1) {
mainThread { contrastField.integerValue = cr }
guard wait(0.01) else { return }
}
}
func setControl(_ control: DisplayControl, display: Display) {
mainThread {
guard !cancelled, let button = controlButton else { return }
switch control {
case .appleNative:
button.bg = .clear
button.attributedTitle = "Using Apple Native Protocol".withTextColor(green)
button.helpText = NATIVE_CONTROLS_HELP_TEXT
case .ddc:
button.bg = .clear
button.attributedTitle = "Using DDC Protocol".withTextColor(peach)
button.helpText = HARDWARE_CONTROLS_HELP_TEXT
case .network:
button.bg = .clear
button.attributedTitle = "Using DDC-over-Network Protocol".withTextColor(blue.highlight(withLevel: 0.2) ?? blue)
button.helpText = NETWORK_CONTROLS_HELP_TEXT
case .gamma:
display.enabledControls[.gamma] = true
button.bg = .clear
if display.supportsGamma {
button.attributedTitle = "Using Gamma Approximation".withTextColor(red)
button.helpText = SOFTWARE_CONTROLS_HELP_TEXT
} else {
button.attributedTitle = "Using Dark Overlay".withTextColor(red)
button.helpText = SOFTWARE_OVERLAY_HELP_TEXT
}
default:
break
}
button.isEnabled = true
button.isHidden = false
button.transition(1.0, easing: .easeOutExpo)
button.alphaValue = 1.0
}
}
func setVolume(_ volume: UInt16) {
mainThread {
if volumeSlider.alphaValue == 0 {
volumeSlider.isEnabled = true
volumeSlider.isHidden = false
volumeSlider.transition(1.0, easing: .easeOutExpo)
volumeSlider.alphaValue = 1.0
}
}
for vol in stride(from: self.volume.i, through: volume.i, by: self.volume.i < volume.i ? 4 : -4) {
mainThread { self.volume = vol.d }
guard wait(0.02) else { return }
}
}
func setResult(_ resultControl: NSTextField, text: String, color: NSColor, transitionSpeed: TimeInterval = 1.5, alpha: Double = 1.0) {
mainThread {
resultControl.stringValue = text
resultControl.textColor = color
resultControl.transition(transitionSpeed)
resultControl.alphaValue = alpha
}
}
func testControl(_ control: Control, for display: Display) -> ControlResult {
hideDisplayValues()
hideAction()
hideQuestion()
let readWorked = testControlRead(control, for: display)
setControlProgress(0.5)
guard wait(1) else { return .allWorked }
guard readWorked.all else {
info("", color: peach)
// var writeWorked = ControlWriteResult.noneWorked
// waitForAction(
// "Reading didn't work for some values\nWrite the missing values manually and click the Continue button",
// buttonColor: lunarYellow, buttonText: "Continue".withTextColor(mauve)
// ) { [weak self] in
// guard let self else { return }
// writeWorked = self.testControlWrite(control, for: display)
// }
let writeWorked = testControlWrite(control, for: display)
return ControlResult(type: control.displayControl, read: readWorked, write: writeWorked)
}
info("Starting write tests", color: peach)
guard wait(3) else { return .allWorked }
let writeWorked = testControlWrite(control, for: display)
return ControlResult(type: control.displayControl, read: readWorked, write: writeWorked)
}
func testControlWrite(_ control: Control, for display: Display) -> ControlWriteResult {
// #if DEBUG
// return .noneWorked
// #endif
let currentBrightness: UInt16 = brightnessField.integerValue == 0 ? 50 : brightnessField.integerValue.u16
let currentContrast: UInt16 = contrastField.integerValue == 0 ? 50 : contrastField.integerValue.u16
let currentVolume: UInt16 = volume == 0 ? 50 : volume.u16
var brightnessWriteWorked = false
var contrastWriteWorked = false
var volumeWriteWorked = false
if let control = control as? DDCControl {
control.ignoreFaults = true
}
defer {
if let control = control as? DDCControl {
control.ignoreFaults = false
}
}
let setWriteProgress = { value in self.setControlProgress(0.5 + (0.5 * value)) }
info("Writing brightness", color: peach)
setWriteProgress(0.1)
guard wait(1.1) else { return .allWorked }
display.withBrightnessTransition(.slow) {
display.enabledControls[control.displayControl] = true
var newBr: UInt16 = currentBrightness != 0 ? (control.isSoftware ? 25 : 0) : 50
var oldBr: UInt16 = currentBrightness
let write1Worked = control.setBrightness(
newBr,
oldValue: oldBr,
force: true, transition: .smooth,
onChange: { [weak self] br in self?.setBrightness(br) }
)
if control.isSoftware, !display.supportsGamma {
setBrightness(newBr)
}
guard wait(4) else { return }
setWriteProgress(0.15)
oldBr = newBr
newBr = 75
let write2Worked = control.setBrightness(
newBr,
oldValue: oldBr,
force: true, transition: .smooth,
onChange: { [weak self] br in self?.setBrightness(br) }
)
if control.isSoftware, !display.supportsGamma {
setBrightness(newBr)
}
guard wait(4) else { return }
setWriteProgress(0.2)
oldBr = newBr
newBr = control.isSoftware ? 100 : 67
let write3Worked = control.setBrightness(
newBr,
oldValue: oldBr,
force: true, transition: .smooth,
onChange: { [weak self] br in self?.setBrightness(br) }
)
if control.isSoftware, !display.supportsGamma {
setBrightness(newBr)
}
brightnessWriteWorked = (write1Worked.i + write2Worked.i + write3Worked.i) >= 2
setWriteProgress(0.25)
}
guard !control.isSoftware || display.supportsGamma else {
setContrast(100)
setVolume(50)
setResult(contrastWriteResult, text: "Write not supported", color: peach)
setResult(volumeWriteResult, text: "Write not supported", color: peach)
return ControlWriteResult(brightness: true, contrast: false, volume: false)
}
if brightnessWriteWorked {
setResult(brightnessWriteResult, text: "Write seemed to work", color: peach)
askQuestion(
control.isSoftware
? "Was there any change in brightness on the tested display?"
: "Was there any change in brightness on the tested display?\nThe brightness value in the monitor settings should now be set to 67"
) { [weak self] itWorked in
guard let self else { return }
if itWorked {
setResult(brightnessWriteResult, text: "Write worked", color: green)
} else {
brightnessWriteWorked = false
setResult(brightnessWriteResult, text: "Failed to write", color: red)
}
}
} else {
setResult(brightnessWriteResult, text: "Failed to write", color: red)
}
guard wait(1.1) else { return .allWorked }
guard !control.isSoftware else {
setContrast(100)
setVolume(50)
setResult(contrastWriteResult, text: "Write not supported", color: peach)
setResult(volumeWriteResult, text: "Write not supported", color: peach)
return ControlWriteResult(brightness: true, contrast: false, volume: false)
}
setWriteProgress(0.3)
info("Writing contrast", color: peach)
guard wait(1.1) else { return .allWorked }
display.withBrightnessTransition {
setWriteProgress(0.35)
var newCr: UInt16 = currentContrast != 0 ? 0 : 50
var oldCr: UInt16 = currentContrast
let write1Worked = control.setContrast(
newCr,
oldValue: oldCr,
transition: .smooth,
onChange: { [weak self] br in self?.setContrast(br) }
)
guard wait(4) else { return }
setWriteProgress(0.4)
oldCr = newCr
newCr = 75
let write2Worked = control.setContrast(
newCr,
oldValue: oldCr,
transition: .smooth,
onChange: { [weak self] br in self?.setContrast(br) }
)
guard wait(4) else { return }
setWriteProgress(0.45)
oldCr = newCr
newCr = 67
let write3Worked = control.setContrast(
newCr,
oldValue: oldCr,
transition: .smooth,
onChange: { [weak self] br in self?.setContrast(br) }
)
contrastWriteWorked = (write1Worked.i + write2Worked.i + write3Worked.i) >= 2
setWriteProgress(0.5)
}
if contrastWriteWorked {
setResult(contrastWriteResult, text: "Write seemed to work", color: peach)
askQuestion(
"Was there any change in contrast on the tested display?\nThe contrast value in the monitor settings should now be set to 67"
) { [weak self] itWorked in
guard let self else { return }
if itWorked {
setResult(contrastWriteResult, text: "Write worked", color: green)
} else {
contrastWriteWorked = false
setResult(contrastWriteResult, text: "Failed to write", color: red)
}
}
} else {
setResult(contrastWriteResult, text: "Failed to write", color: red)
}
guard wait(1.1) else { return .allWorked }
setWriteProgress(0.55)
info("Writing volume", color: peach)
guard wait(1.1) else { return .allWorked }
setVolume(0)
let write1Worked = control.setVolume(0)
guard wait(1) else { return .allWorked }
setWriteProgress(0.6)
setVolume(75)
let write2Worked = control.setVolume(75)
guard wait(1) else { return .allWorked }
setWriteProgress(0.65)
setVolume(23)
let write3Worked = control.setVolume(23)
volumeWriteWorked = (write1Worked.i + write2Worked.i + write3Worked.i) >= 2
setWriteProgress(0.7)
if volumeWriteWorked {
setResult(volumeWriteResult, text: "Write seemed to work", color: peach)
askQuestion(
"Was there any change in volume on the tested display?\nThe volume value in the monitor settings should now be set to 23"
) { [weak self] itWorked in
guard let self else { return }
if itWorked {
setResult(volumeWriteResult, text: "Write worked", color: white)
} else {
volumeWriteWorked = false
setResult(volumeWriteResult, text: "Failed to write", color: peach)
}
}
} else {
setResult(volumeWriteResult, text: "Failed to write", color: red)
}
guard wait(1.1) else { return .allWorked }
setWriteProgress(0.8)
info("Setting values before test", color: peach)
guard wait(2) else { return .allWorked }
_ = control.setBrightness(currentBrightness, oldValue: nil, force: true, transition: .smooth, onChange: nil)
setBrightness(currentBrightness)
setWriteProgress(0.85)
guard wait(0.5) else { return .allWorked }
_ = control.setContrast(currentContrast, oldValue: nil, transition: .smooth, onChange: nil)
setContrast(currentContrast)
setWriteProgress(0.9)
guard wait(0.5) else { return .allWorked }
_ = control.setVolume(currentVolume)
setVolume(currentVolume)
setWriteProgress(0.95)
guard wait(0.5) else { return .allWorked }
return ControlWriteResult(brightness: brightnessWriteWorked, contrast: contrastWriteWorked, volume: volumeWriteWorked)
}
func wait(_ seconds: TimeInterval) -> Bool {
// #if DEBUG
// return !cancelled
// #endif
Thread.sleep(forTimeInterval: seconds)
return !cancelled
}
func testControlRead(_ control: Control, for _: Display) -> ControlReadResult {
var brightnessReadWorked = false
var contrastReadWorked = false
var volumeReadWorked = false
let setReadProgress = { value in self.setControlProgress(0.5 * value) }
guard !control.isSoftware else {
setBrightness(100)
setContrast(100)
setVolume(50)
setResult(brightnessReadResult, text: "Read not supported", color: peach)
setResult(contrastReadResult, text: "Read not supported", color: peach)
setResult(volumeReadResult, text: "Read not supported", color: peach)
return .allWorked
}
info("Reading brightness", color: peach)
guard wait(1.1) else { return .allWorked }
var brightnessThatWasRead: UInt16 = 0
if let br = (control.getBrightness() ?? control.getBrightness()), !(control is AppleNativeControl) || br <= 100 {
brightnessReadWorked = true
setBrightness(br)
brightnessThatWasRead = br
guard wait(1.1) else { return .allWorked }
setReadProgress(0.15)
setResult(brightnessReadResult, text: "Read worked", color: green)
guard wait(1.1) else { return .allWorked }
let _ = control.setBrightness(br, oldValue: nil, force: true, transition: .instant, onChange: nil)
} else {
setBrightness(0)
guard wait(1.1) else { return .allWorked }
setReadProgress(0.15)
// setResult(brightnessReadResult, text: "Failed to read", color: red)
guard wait(1.1) else { return .allWorked }
}
setReadProgress(0.33)
info("Reading contrast", color: peach)
guard wait(1.1) else { return .allWorked }
if let cr = (control.getContrast() ?? control.getContrast()) {
contrastReadWorked = true
setContrast(cr)
guard wait(1.1) else { return .allWorked }
setReadProgress(0.45)
setResult(contrastReadResult, text: "Read worked", color: green)
guard wait(1.1) else { return .allWorked }
if brightnessReadWorked {
let _ = control.setBrightness(brightnessThatWasRead, oldValue: nil, force: true, transition: .instant, onChange: nil)
} else {
let _ = control.setContrast(cr, oldValue: nil, transition: .instant, onChange: nil)
}
} else {
setContrast(0)
guard wait(1.1) else { return .allWorked }
setReadProgress(0.45)
// setResult(contrastReadResult, text: "Failed to read", color: red)
guard wait(1.1) else { return .allWorked }
}
setReadProgress(0.66)
info("Reading volume", color: peach)
guard wait(1.1) else { return .allWorked }
if let vol = (control.getVolume() ?? control.getVolume()) {
volumeReadWorked = true
setVolume(vol)
guard wait(1.1) else { return .allWorked }
setReadProgress(0.8)
setResult(volumeReadResult, text: "Read worked", color: white)
guard wait(1.1) else { return .allWorked }
} else {
setVolume(0)
guard wait(1.1) else { return .allWorked }
setReadProgress(0.8)
// setResult(volumeReadResult, text: "Failed to read", color: peach)
guard wait(1.1) else { return .allWorked }
}
setReadProgress(0.9)
return ControlReadResult(brightness: brightnessReadWorked, contrast: contrastReadWorked, volume: volumeReadWorked)
}
func hideDisplayValues() {
mainThread {
brightnessField.transition(0.5, easing: .easeOutExpo)
brightnessField.alphaValue = 0
brightnessCaption.transition(0.5, easing: .easeOutExpo)
brightnessCaption.alphaValue = 0
contrastField.transition(0.5, easing: .easeOutExpo)
contrastField.alphaValue = 0
contrastCaption.transition(0.5, easing: .easeOutExpo)
contrastCaption.alphaValue = 0
brightnessReadResult.transition(0.5, easing: .easeOutExpo)
brightnessReadResult.alphaValue = 0
brightnessWriteResult.transition(0.5, easing: .easeOutExpo)
brightnessWriteResult.alphaValue = 0
contrastReadResult.transition(0.5, easing: .easeOutExpo)
contrastReadResult.alphaValue = 0
contrastWriteResult.transition(0.5, easing: .easeOutExpo)
contrastWriteResult.alphaValue = 0
volumeSlider.transition(0.5, easing: .easeOutExpo)
volumeSlider.alphaValue = 0
volumeReadResult.transition(0.5, easing: .easeOutExpo)
volumeReadResult.alphaValue = 0
volumeWriteResult.transition(0.5, easing: .easeOutExpo)
volumeWriteResult.alphaValue = 0
}
}
func queueChange(_ change: @escaping () -> Void) {
mainThread {
guard let wc = view.window?.windowController as? OnboardWindowController else {
return
}
wc.changes.append(change)
}
}
func next() {
guard let wc = view.window?.windowController as? OnboardWindowController else { return }
wc.applyChanges()
wc.pageController?.navigateForward(self)
}
func showTestWindow() {
guard let d = currentDisplay else { return }
mainThread {
createWindow(
"testWindowController",
controller: &d.testWindowController,
screen: d.nsScreen,
show: true,
backgroundColor: .clear,
level: .screenSaver,
fillScreen: false,
stationary: true
)
}
wakeObserver = wakeObserver ?? NSWorkspace.shared.notificationCenter
.publisher(for: NSWorkspace.screensDidWakeNotification, object: nil)
.debounce(for: .seconds(2), scheduler: RunLoop.main)
.sink { [weak self] _ in
self?.showTestWindow()
}
screenObserver = screenObserver ?? NotificationCenter.default
.publisher(for: NSApplication.didChangeScreenParametersNotification, object: nil)
.debounce(for: .seconds(2), scheduler: RunLoop.main)
.sink { [weak self] _ in
self?.showTestWindow()
}
}
func networkProblemText(_ display: Display, control _: NetworkControl) -> String {
var url = "http://<your-rpi-ip>:3485/displays"
if let networkController = NetworkControl.controllersForDisplay[display.serial], let controlURL = networkController.url {
url = controlURL.deletingLastPathComponent().appendingPathComponent("displays").absoluteString
}
let infoDict = display.infoDictionary
let year = (infoDict[kDisplayYearOfManufacture] as? Int64) ?? 2017
let serial = (infoDict[kDisplaySerialNumber] as? Int64) ?? 314_041
let product = (infoDict[kDisplayProductID] as? Int64) ?? 23304
let vendor = (infoDict[kDisplayVendorID] as? Int64)?.s ?? "GSM"
let name = display.edidName
return """
##### If you have technical knowledge about `SSH` and `curl`, you can check the following:
* See if the controller is reachable using this curl command: `curl \(url)`
* You should get a response similar to the one below:
Display 1
I2C bus: /dev/i2c-2
EDID synopsis:
Mfg id: \(vendor)
Model: \(name)
Product code: \(product)
Serial number:
Binary serial number: \(serial)
Manufacture year: \(year)
EDID version: 1.3
VCP version: 2.1
* If you get `Invalid Display` try turning your monitor off then turn it on after a few seconds
* If you get `Display not found`, make sure your Pi is running an OS with a desktop environment, and the desktop is visible when the Pi HDMI input is active
* Check that the server is running
* SSH into your Pi
* Run `sudo systemctl status ddcutil-server`
* Check if `ddcutil` can correctly identify and control your monitor
* SSH into your Pi
* Run `ddcutil detect` _(you should get the same response as for the `curl` command)_
* Cycle through a few brightness values to see if there's any change:
* `ddcutil setvcp 0x10 25`
* `ddcutil setvcp 0x10 100`
* `ddcutil setvcp 0x10 50`
"""
}
func setDisplayProgress(_ value: Double, controlStep: Double) {
mainThread {
progress = progressForLastDisplay + displayProgressStep * value
controlProgressStep = controlStep
progressForDisplay = value
}
}
func setControlProgress(_ value: Double) {
mainThread {
progress = progressForLastDisplay + displayProgressStep * progressForDisplay + value *
(controlProgressStep * displayProgressStep * progressForDisplay)
}
}
func listenForDisplayConnections() {
NotificationCenter.default
.publisher(for: displayListChanged, object: nil)
.debounce(for: .seconds(2), scheduler: RunLoop.main)
.sink { [weak self] _ in
self?.cancelled = true
OnboardPageController.task?.cancel()
log.debug("Restarting Onboarding after display list change")
guard let w = self?.view.window else { return }
_ = askBool(
message: "Display list changed",
info: """
The list of connected displays has changed
and the onboarding process can't continue.
Do you want to restart the onboarding process?
""",
okButton: "Restart Onboarding",
cancelButton: "Skip Onboarding",
window: w,
onCompletion: { [weak self] (restart: Bool) in
guard let self, let w = view.window, let wc = w.windowController as? OnboardWindowController else {
return
}
cancelled = true
OnboardPageController.task?.cancel()
semaphore.signal()
wc.changes = []
w.close()
wc.close()
for d in DC.displays.values {
d.testWindowController?.close()
d.testWindowController = nil
}
if restart {
mainAsyncAfter(ms: 1000) { appDelegate!.onboard() }
}
},
unique: true,
waitTimeout: 5.minutes
)
}.store(in: &observers)
}
override func viewDidAppear() {
guard !didAppear else { return }
didAppear = true
uiCrumb("Control Tester")
cancelled = false
adaptiveModeDisabledByDiagnostics = false
if DC.adaptiveModeKey != .manual {
DC.disable()
adaptiveModeDisabledByDiagnostics = true
}
listenForDisplayConnections()
if let wc = view.window?.windowController as? OnboardWindowController {
wc.setupSkipButton(skipButton, text: useOnboardingForDiagnostics ? "Stop Diagnostics" : nil) { [weak self] in
self?.cancelled = true
for d in DC.displays.values {
d.testWindowController?.close()