@@ -9,6 +9,9 @@ class OtherDisplay: Display {
9
9
var arm64ddc : Bool = false
10
10
var arm64avService : IOAVService ?
11
11
var isDiscouraged : Bool = false
12
+ let writeDDCQueue = DispatchQueue ( label: " Local write DDC queue " )
13
+ var writeDDCNextValue : [ Command : UInt16 ] = [ : ]
14
+ var writeDDCLastSavedValue : [ Command : UInt16 ] = [ : ]
12
15
var pollingCount : Int {
13
16
get {
14
17
switch self . readPrefAsInt ( key: . pollingMode) {
@@ -76,11 +79,11 @@ class OtherDisplay: Display {
76
79
if !self . smoothBrightnessRunning, !self . isSw ( ) , !self . readPrefAsBool ( key: . unavailableDDC, for: command) , self . readPrefAsBool ( key: . isTouched, for: command) , prefs. integer ( forKey: PrefKey . startupAction. rawValue) == StartupAction . write. rawValue, !app. safeMode {
77
80
let restoreValue = self . getDDCValueFromPrefs ( command)
78
81
os_log ( " Restoring %{public}@ DDC value %{public}@ for %{public}@ " , type: . info, String ( reflecting: command) , String ( restoreValue) , self . name)
79
- _ = self . writeDDCValues ( command: command, value: restoreValue)
82
+ self . writeDDCValues ( command: command, value: restoreValue)
80
83
if command == . audioSpeakerVolume, self . readPrefAsBool ( key: . enableMuteUnmute) {
81
84
let currentMuteValue = self . readPrefAsInt ( for: . audioMuteScreenBlank) == 0 ? 2 : self . readPrefAsInt ( for: . audioMuteScreenBlank)
82
85
os_log ( " - Writing last saved DDC value for Mute: %{public}@ " , type: . info, String ( currentMuteValue) )
83
- _ = self . writeDDCValues ( command: . audioMuteScreenBlank, value: UInt16 ( currentMuteValue) )
86
+ self . writeDDCValues ( command: . audioMuteScreenBlank, value: UInt16 ( currentMuteValue) )
84
87
}
85
88
}
86
89
}
@@ -178,13 +181,11 @@ class OtherDisplay: Display {
178
181
let isAlreadySet = volumeOSDValue == self . readPrefAsFloat ( for: . audioSpeakerVolume)
179
182
if !isAlreadySet {
180
183
if let muteValue = muteValue, self . readPrefAsBool ( key: . enableMuteUnmute) {
181
- guard self . writeDDCValues ( command: . audioMuteScreenBlank, value: UInt16 ( muteValue) ) == true else {
182
- return
183
- }
184
+ self . writeDDCValues ( command: . audioMuteScreenBlank, value: UInt16 ( muteValue) )
184
185
self . savePref ( muteValue, for: . audioMuteScreenBlank)
185
186
}
186
187
if !self . readPrefAsBool ( key: . enableMuteUnmute) || volumeOSDValue != 0 {
187
- _ = self . writeDDCValues ( command: . audioSpeakerVolume, value: self . convValueToDDC ( for: . audioSpeakerVolume, from: volumeOSDValue) )
188
+ self . writeDDCValues ( command: . audioSpeakerVolume, value: self . convValueToDDC ( for: . audioSpeakerVolume, from: volumeOSDValue) )
188
189
}
189
190
}
190
191
if !self . readPrefAsBool ( key: . hideOsd) {
@@ -206,7 +207,7 @@ class OtherDisplay: Display {
206
207
let contrastOSDValue = self . calcNewValue ( currentValue: currentValue, isUp: isUp, isSmallIncrement: isSmallIncrement)
207
208
let isAlreadySet = contrastOSDValue == self . readPrefAsFloat ( for: . contrast)
208
209
if !isAlreadySet {
209
- _ = self . writeDDCValues ( command: . contrast, value: self . convValueToDDC ( for: . contrast, from: contrastOSDValue) )
210
+ self . writeDDCValues ( command: . contrast, value: self . convValueToDDC ( for: . contrast, from: contrastOSDValue) )
210
211
}
211
212
OSDUtils . showOsd ( displayID: self . identifier, command: . contrast, value: contrastOSDValue, roundChiclet: !isSmallIncrement)
212
213
if !isAlreadySet {
@@ -237,13 +238,11 @@ class OtherDisplay: Display {
237
238
}
238
239
}
239
240
if self . readPrefAsBool ( key: . enableMuteUnmute) {
240
- guard self . writeDDCValues ( command: . audioMuteScreenBlank, value: UInt16 ( muteValue) ) == true else {
241
- return
242
- }
241
+ self . writeDDCValues ( command: . audioMuteScreenBlank, value: UInt16 ( muteValue) )
243
242
}
244
243
self . savePref ( muteValue, for: . audioMuteScreenBlank)
245
244
if !self . readPrefAsBool ( key: . enableMuteUnmute) || volumeOSDValue > 0 {
246
- _ = self . writeDDCValues ( command: . audioSpeakerVolume, value: self . convValueToDDC ( for: . audioSpeakerVolume, from: volumeOSDValue) )
245
+ self . writeDDCValues ( command: . audioSpeakerVolume, value: self . convValueToDDC ( for: . audioSpeakerVolume, from: volumeOSDValue) )
247
246
}
248
247
if !fromVolumeSlider {
249
248
if !self . readPrefAsBool ( key: . hideOsd) {
@@ -345,12 +344,12 @@ class OtherDisplay: Display {
345
344
brightnessValue = 0
346
345
brightnessSwValue = ( value / self . combinedBrightnessSwitchingValue ( ) )
347
346
}
348
- _ = self . writeDDCValues ( command: . brightness, value: self . convValueToDDC ( for: . brightness, from: brightnessValue) )
347
+ self . writeDDCValues ( command: . brightness, value: self . convValueToDDC ( for: . brightness, from: brightnessValue) )
349
348
if self . readPrefAsFloat ( key: . SwBrightness) != brightnessSwValue {
350
349
_ = self . setSwBrightness ( brightnessSwValue)
351
350
}
352
351
} else {
353
- _ = self . writeDDCValues ( command: . brightness, value: self . convValueToDDC ( for: . brightness, from: value) )
352
+ self . writeDDCValues ( command: . brightness, value: self . convValueToDDC ( for: . brightness, from: value) )
354
353
}
355
354
if !transient {
356
355
self . savePref ( value, for: . brightness)
@@ -378,28 +377,45 @@ class OtherDisplay: Display {
378
377
return intCodes
379
378
}
380
379
381
- public func writeDDCValues( command: Command , value: UInt16 , errorRecoveryWaitTime _ : UInt32 ? = nil ) -> Bool ? {
380
+ public func writeDDCValues( command: Command , value: UInt16 ) {
382
381
guard app. sleepID == 0 , app. reconfigureID == 0 , !self . readPrefAsBool ( key: . forceSw) , !self . readPrefAsBool ( key: . unavailableDDC, for: command) else {
383
- return false
382
+ return
383
+ }
384
+ self . writeDDCQueue. async ( flags: . barrier) {
385
+ self . writeDDCNextValue [ command] = value
386
+ }
387
+ DisplayManager . shared. globalDDCQueue. async ( flags: . barrier) {
388
+ self . asyncPerformWriteDDCValues ( command: command)
389
+ }
390
+ }
391
+
392
+ func asyncPerformWriteDDCValues( command: Command ) {
393
+ var value = UInt16 . max
394
+ var lastValue = UInt16 . max
395
+ self . writeDDCQueue. sync {
396
+ value = self . writeDDCNextValue [ command] ?? UInt16 . max
397
+ lastValue = self . writeDDCLastSavedValue [ command] ?? UInt16 . max
398
+ }
399
+ guard value != UInt16 . max, value != lastValue else {
400
+ return
401
+ }
402
+ self . writeDDCQueue. async ( flags: . barrier) {
403
+ self . writeDDCLastSavedValue [ command] = value
404
+ self . savePref ( true , key: PrefKey . isTouched, for: command)
384
405
}
385
- var success : Bool = false
386
406
var controlCodes = self . getRemapControlCodes ( command: command)
387
407
if controlCodes. count == 0 {
388
408
controlCodes. append ( command. rawValue)
389
409
}
390
410
for controlCode in controlCodes {
391
- DisplayManager . shared. ddcQueue. sync {
392
- if Arm64DDC . isArm64 {
393
- if self . arm64ddc {
394
- success = Arm64DDC . write ( service: self . arm64avService, command: controlCode, value: value)
395
- }
396
- } else {
397
- success = self . ddc? . write ( command: command. rawValue, value: value, errorRecoveryWaitTime: 2000 ) ?? false
411
+ if Arm64DDC . isArm64 {
412
+ if self . arm64ddc {
413
+ _ = Arm64DDC . write ( service: self . arm64avService, command: controlCode, value: value)
398
10000
414
}
399
- self . savePref ( true , key: PrefKey . isTouched, for: command) // We deliberatly consider the value tuched no matter if the call succeeded
415
+ } else {
416
+ _ = self . ddc? . write ( command: controlCode, value: value, errorRecoveryWaitTime: 2000 ) ?? false
400
417
}
401
418
}
402
- return success
403
419
}
404
420
405
421
func readDDCValues( for command: Command , tries: UInt , minReplyDelay delay: UInt64 ? ) -> ( current: UInt16 , max: UInt16 ) ? {
@@ -413,15 +429,15 @@ class OtherDisplay: Display {
413
429
guard self . arm64ddc else {
414
430
return nil
415
431
}
416
- DisplayManager . shared. ddcQueue . sync {
432
+ DisplayManager . shared. globalDDCQueue . sync {
417
433
if let unwrappedDelay = delay {
418
434
values = Arm64DDC . read ( service: self . arm64avService, command: controlCode, tries: UInt8 ( min ( tries, 255 ) ) , minReplyDelay: UInt32 ( unwrappedDelay / 1000 ) )
419
435
} else {
420
436
values = Arm64DDC . read ( service: self . arm64avService, command: controlCode, tries: UInt8 ( min ( tries, 255 ) ) )
421
437
}
422
438
}
423
439
} else {
424
- DisplayManager . shared. ddcQueue . sync {
440
+ DisplayManager . shared. globalDDCQueue . sync {
425
441
values = self . ddc? . read ( command: controlCode, tries: tries, minReplyDelay: delay)
426
442
}
427
443
}
0 commit comments