Standard DisplayServices framework bridging instead of using the convoluted workaround that was present in InternalDisplay.swift

This commit is contained in:
waydabber 2021-08-05 15:18:41 +02:00
parent e0352c7256
commit 7c5eda8e33
5 changed files with 14 additions and 41 deletions

View file

@ -35,6 +35,7 @@
AA3B4A2A26AE108E00B74CD2 /* Apple Silicon.md in Resources */ = {isa = PBXBuildFile; fileRef = AA3B4A2926AE108E00B74CD2 /* Apple Silicon.md */; };
AA9AE86F26B5BF3D00B6CA65 /* OSD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA9AE86E26B5BF3D00B6CA65 /* OSD.framework */; };
AA9AE87126B5BFB700B6CA65 /* CoreDisplay.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA9AE87026B5BFB700B6CA65 /* CoreDisplay.framework */; };
AADB625A26BC196900DFFAA5 /* DisplayServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AADB625926BC196900DFFAA5 /* DisplayServices.framework */; };
F01B0699228221B7008E64DB /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F01B0680228221B6008E64DB /* Localizable.strings */; };
F01B069A228221B7008E64DB /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = F01B0683228221B6008E64DB /* Utils.swift */; };
F01B069E228221B7008E64DB /* ButtonCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F01B068E228221B6008E64DB /* ButtonCellView.swift */; };
@ -122,6 +123,7 @@
AA6686F226B8172E00AF74A2 /* hu */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hu; path = hu.lproj/Localizable.strings; sourceTree = "<group>"; };
AA9AE86E26B5BF3D00B6CA65 /* OSD.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OSD.framework; path = ../../../../../System/Library/PrivateFrameworks/OSD.framework; sourceTree = "<group>"; };
AA9AE87026B5BFB700B6CA65 /* CoreDisplay.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreDisplay.framework; path = ../../../../../System/Library/Frameworks/CoreDisplay.framework; sourceTree = "<group>"; };
AADB625926BC196900DFFAA5 /* DisplayServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DisplayServices.framework; path = ../../../../../System/Library/PrivateFrameworks/DisplayServices.framework; sourceTree = "<group>"; };
B0C4810623357CE500053F91 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = "<group>"; };
B0C4810823357CE500053F91 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/MainMenu.strings; sourceTree = "<group>"; };
F01B0681228221B6008E64DB /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = "<group>"; };
@ -159,6 +161,7 @@
6CD35F592650002E001F1344 /* DDC in Frameworks */,
AA9AE86F26B5BF3D00B6CA65 /* OSD.framework in Frameworks */,
6CD35F5C2650003F001F1344 /* Preferences in Frameworks */,
AADB625A26BC196900DFFAA5 /* DisplayServices.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -177,6 +180,7 @@
children = (
AA9AE86E26B5BF3D00B6CA65 /* OSD.framework */,
AA9AE87026B5BFB700B6CA65 /* CoreDisplay.framework */,
AADB625926BC196900DFFAA5 /* DisplayServices.framework */,
6CC260F9256ADA7400613714 /* Preferences.framework */,
28D1DDE3227FB7D0004CB494 /* AMCoreAudio.framework */,
28D1DE10227FD006004CB494 /* AMCoreAudio.framework.dSYM */,

View file

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1276</string>
<string>1281</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>

View file

@ -5,7 +5,7 @@
// Created by Joni Van Roost on 24/01/2020.
// Copyright © 2020 MonitorControl. All rights reserved.
//
// Most of the code in this file was sourced from:
// Some of the code in this file was sourced from:
// https://github.com/fnesveda/ExternalDisplayBrightness
// all credit goes to @fnesveda
@ -31,51 +31,16 @@ class InternalDisplay: Display {
public func getBrightness() -> Float {
var brightness: Float = 0
_ = type(of: self).DisplayServicesGetBrightness?(self.identifier, &brightness)
DisplayServicesGetBrightness(self.identifier, &brightness)
return brightness
}
override func stepBrightness(isUp: Bool, isSmallIncrement: Bool) {
let value = self.calcNewBrightness(isUp: isUp, isSmallIncrement: isSmallIncrement)
self.displayQueue.sync {
_ = type(of: self).DisplayServicesSetBrightness?(self.identifier, Float(value))
type(of: self).DisplayServicesBrightnessChanged?(self.identifier, Double(value))
DisplayServicesSetBrightness(self.identifier, Float(value))
DisplayServicesBrightnessChanged(self.identifier, Double(value))
self.showOsd(command: .brightness, value: Int(value * 64), maxValue: 64)
}
}
// notifies the system that the brightness of a specified display has changed (to update System Preferences etc.)
// unfortunately Apple doesn't provide a public API for this, so we have to manually extract the function from the DisplayServices framework
private static var DisplayServicesBrightnessChanged: ((CGDirectDisplayID, Double) -> Void)? {
let displayServicesPath = CFURLCreateWithString(kCFAllocatorDefault, "/System/Library/PrivateFrameworks/DisplayServices.framework" as CFString, nil)
if let displayServicesBundle = CFBundleCreate(kCFAllocatorDefault, displayServicesPath) {
if let funcPointer = CFBundleGetFunctionPointerForName(displayServicesBundle, "DisplayServicesBrightnessChanged" as CFString) {
typealias DSBCFunctionType = @convention(c) (UInt32, Double) -> Void
return unsafeBitCast(funcPointer, to: DSBCFunctionType.self)
}
}
return nil
}
private static var DisplayServicesGetBrightness: ((CGDirectDisplayID, UnsafePointer<Float>) -> Int)? {
let displayServicesPath = CFURLCreateWithString(kCFAllocatorDefault, "/System/Library/PrivateFrameworks/DisplayServices.framework" as CFString, nil)
if let displayServicesBundle = CFBundleCreate(kCFAllocatorDefault, displayServicesPath) {
if let funcPointer = CFBundleGetFunctionPointerForName(displayServicesBundle, "DisplayServicesGetBrightness" as CFString) {
typealias DSBCFunctionType = @convention(c) (UInt32, UnsafePointer<Float>) -> Int
return unsafeBitCast(funcPointer, to: DSBCFunctionType.self)
}
}
return nil
}
private static var DisplayServicesSetBrightness: ((CGDirectDisplayID, Float) -> Int)? {
let displayServicesPath = CFURLCreateWithString(kCFAllocatorDefault, "/System/Library/PrivateFrameworks/DisplayServices.framework" as CFString, nil)
if let displayServicesBundle = CFBundleCreate(kCFAllocatorDefault, displayServicesPath) {
if let funcPointer = CFBundleGetFunctionPointerForName(displayServicesBundle, "DisplayServicesSetBrightness" as CFString) {
typealias DSBCFunctionType = @convention(c) (UInt32, Float) -> Int
return unsafeBitCast(funcPointer, to: DSBCFunctionType.self)
}
}
return nil
}
}

View file

@ -10,6 +10,10 @@ extern IOReturn IOAVServiceReadI2C(IOAVService service, uint32_t chipAddress, ui
extern IOReturn IOAVServiceWriteI2C(IOAVService service, uint32_t chipAddress, uint32_t dataAddress, void* inputBuffer, uint32_t inputBufferSize);
extern CFDictionaryRef CoreDisplay_DisplayCreateInfoDictionary(CGDirectDisplayID);
extern void DisplayServicesBrightnessChanged(CGDirectDisplayID display, double brightness);
extern int DisplayServicesGetBrightness(CGDirectDisplayID display, float *brightness);
extern int DisplayServicesSetBrightness(CGDirectDisplayID display, float brightness);
@class NSString;
@protocol OSDUIHelperProtocol

View file

@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1276</string>
<string>1281</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSBackgroundOnly</key>