Skip to content

Commit bae2792

Browse files
committed
Tweaks for software dimming after brightness reached zero function
- Added OSD animation to indicate that something is going on + circumvent the gamma change no screen refresh issue on some mac - Smooth brightness change is now only happening on config changes + added blank birghtness OSD to give a hint on what is happening + also circumvent the no refresh issue.
1 parent 36eeb43 commit bae2792

7 files changed

Lines changed: 48 additions & 12 deletions

File tree

MonitorControl/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
196196
}
197197
DisplayManager.shared.addDisplay(display: display)
198198
}
199+
self.updateArm64AVServices()
199200
if firstrun {
200201
DisplayManager.shared.resetSwBrightnessForAllDisplays(settingsOnly: true)
201202
} else {
202203
if prefs.bool(forKey: Utils.PrefKeys.fallbackSw.rawValue) || prefs.bool(forKey: Utils.PrefKeys.lowerSwAfterBrightness.rawValue) {
203204
DisplayManager.shared.restoreSwBrightnessForAllDisplays(async: true)
204205
}
205206
}
206-
self.updateArm64AVServices()
207207
NotificationCenter.default.post(name: Notification.Name(Utils.PrefKeys.displayListUpdate.rawValue), object: nil)
208208
self.updateMenus()
209209
}

MonitorControl/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>$(MARKETING_VERSION)</string>
2121
<key>CFBundleVersion</key>
22-
<string>2332</string>
22+
<string>2406</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

MonitorControl/Manager/DisplayManager.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ class DisplayManager {
8888
for externalDisplay in self.getExternalDisplays() {
8989
if externalDisplay.getValue(for: .brightness) == 0 || externalDisplay.isSw() {
9090
let savedPrefValue = externalDisplay.getSwBrightnessPrefValue()
91+
if externalDisplay.getSwBrightness() != savedPrefValue {
92+
if let manager = OSDManager.sharedManager() as? OSDManager { // This will give the user a hint why is the brightness suddenly changes and also give screen activity to counter the 'no gamma change when there is no screen activity' issue on some macs
93+
manager.showImage(OSDImage.brightness.rawValue, onDisplayID: externalDisplay.identifier, priority: 0x1F4, msecUntilFade: 0)
94+
}
95+
}
9196
externalDisplay.saveSwBirghtnessPrefValue(Int(externalDisplay.getSwBrightness()))
9297
_ = externalDisplay.setSwBrightness(value: UInt8(savedPrefValue), smooth: async)
9398
} else {
94-
_ = externalDisplay.setSwBrightness(value: externalDisplay.getSwMaxBrightness(), smooth: async)
99+
_ = externalDisplay.setSwBrightness(value: externalDisplay.getSwMaxBrightness())
95100
}
96101
}
97102
}

MonitorControl/Model/Display.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import DDC
1010
import Foundation
1111
import os.log
1212

13-
private enum OSDImage: Int64 {
13+
enum OSDImage: Int64 {
1414
case brightness = 1
1515
case audioSpeaker = 3
1616
case audioSpeakerMuted = 4
@@ -95,7 +95,7 @@ class Display {
9595
}
9696

9797
func swBrightnessTransform(value: Float, reverse: Bool = false) -> Float {
98-
let lowTreshold: Float = 0.05 // We don't allow decrease lower than 5% for safety reasons and because some displays blank off after a while on full black screen due to energy saving settings
98+
let lowTreshold: Float = 0.1 // We don't allow decrease lower than 5% for safety reasons and because some displays blank off after a while on full black screen due to energy saving settings
9999
if !reverse {
100100
return value * (1 - lowTreshold) + lowTreshold
101101
} else {
@@ -115,7 +115,7 @@ class Display {
115115
if smooth {
116116
DispatchQueue.global(qos: .userInteractive).async {
117117
self.swBrightnessSemaphore.wait()
118-
for transientValue in stride(from: currentValue, to: newValue, by: 0.0025 * (currentValue > newValue ? -1 : 1)) {
118+
for transientValue in stride(from: currentValue, to: newValue, by: 0.005 * (currentValue > newValue ? -1 : 1)) {
119119
let gammaTableRed = self.defaultGammaTableRed.map { $0 * transientValue }
120120
let gammaTableGreen = self.defaultGammaTableGreen.map { $0 * transientValue }
121121
let gammaTableBlue = self.defaultGammaTableBlue.map { $0 * transientValue }

MonitorControl/Model/ExternalDisplay.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,37 @@ class ExternalDisplay: Display {
167167
}
168168
}
169169

170+
let swAfterOsdAnimationSemaphore = DispatchSemaphore(value: 1)
171+
var lastAnimationStartedTime: CFTimeInterval = CACurrentMediaTime()
172+
func doSwAfterOsdAnimation() {
173+
self.lastAnimationStartedTime = CACurrentMediaTime()
174+
DispatchQueue.global(qos: .userInteractive).async {
175+
self.swAfterOsdAnimationSemaphore.wait()
176+
guard CACurrentMediaTime() < self.lastAnimationStartedTime + 0.05 else {
177+
self.swAfterOsdAnimationSemaphore.signal()
178+
return
179+
}
180+
for value: Int in stride(from: 1, to: 6, by: 1) {
181+
guard self.getValue(for: .brightness) == 0 else {
182+
self.swAfterOsdAnimationSemaphore.signal()
183+
return
184+
}
185+
self.showOsd(command: .brightness, value: value, roundChiclet: false)
186+
Thread.sleep(forTimeInterval: Double(value * 2) / 300)
187+
}
188+
for value: Int in stride(from: 5, to: 0, by: -1) {
189+
guard self.getValue(for: .brightness) == 0 else {
190+
self.swAfterOsdAnimationSemaphore.signal()
191+
return
192+
}
193+
self.showOsd(command: .brightness, value: value, roundChiclet: false)
194+
Thread.sleep(forTimeInterval: Double(value * 2) / 300)
195+
}
196+
self.showOsd(command: .brightness, value: 0, roundChiclet: true)
197+
self.swAfterOsdAnimationSemaphore.signal()
198+
}
199+
}
200+
170201
override func stepBrightness(isUp: Bool, isSmallIncrement: Bool) {
171202
let currentValue = self.getValue(for: .brightness)
172203
let maxValue = self.isSw() ? Int(self.getSwMaxBrightness()) : self.getMaxValue(for: .brightness)
@@ -198,8 +229,8 @@ class ExternalDisplay: Display {
198229
swBirghtnessValue = Int(getSwMaxBrightness())
199230
swAfterBirghtnessMode = false
200231
}
201-
if self.setSwBrightness(value: UInt8(swBirghtnessValue), smooth: true) {
202-
self.showOsd(command: .brightness, value: self.getValue(for: .brightness), roundChiclet: !isSmallIncrement)
232+
if self.setSwBrightness(value: UInt8(swBirghtnessValue)) {
233+
self.doSwAfterOsdAnimation()
203234
}
204235
}
205236
if !swAfterBirghtnessMode {

MonitorControl/Support/Utils.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ class Utils: NSObject {
8383
if command != .audioSpeakerVolume {
8484
slider.integerValue = Int(currentValue)
8585
slider.maxValue = Double(display.getMaxValue(for: command))
86-
if display.isSw() {
87-
slider.minValue = Double(display.getSwMaxBrightness()) * 0.2 // Don't let brightness to down for software brightness control as the user won't see the slider anymore
88-
}
86+
// if display.isSw() {
87+
// slider.minValue = Double(display.getSwMaxBrightness()) * 0.2 // Don't let brightness to down for software brightness control as the user won't see the slider anymore
88+
// }
8989
} else {
9090
// If we're looking at the audio speaker volume, also retrieve the values for the mute command
9191
var muteValues: (current: UInt16, max: UInt16)?

MonitorControlHelper/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>$(MARKETING_VERSION)</string>
2121
<key>CFBundleVersion</key>
22-
<string>2332</string>
22+
<string>2406</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSBackgroundOnly</key>

0 commit comments

Comments
 (0)