From 7c5eda8e335cfa9162a2f3cb73a37aaed663cb3f Mon Sep 17 00:00:00 2001 From: waydabber <37590873+waydabber@users.noreply.github.com> Date: Thu, 5 Aug 2021 15:18:41 +0200 Subject: [PATCH] Standard DisplayServices framework bridging instead of using the convoluted workaround that was present in InternalDisplay.swift --- MonitorControl.xcodeproj/project.pbxproj | 4 ++ MonitorControl/Info.plist | 2 +- MonitorControl/Model/InternalDisplay.swift | 43 ++-------------------- MonitorControl/Support/Bridging-Header.h | 4 ++ MonitorControlHelper/Info.plist | 2 +- 5 files changed, 14 insertions(+), 41 deletions(-) diff --git a/MonitorControl.xcodeproj/project.pbxproj b/MonitorControl.xcodeproj/project.pbxproj index ab92ad8..c699022 100644 --- a/MonitorControl.xcodeproj/project.pbxproj +++ b/MonitorControl.xcodeproj/project.pbxproj @@ -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 = ""; }; AA9AE86E26B5BF3D00B6CA65 /* OSD.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OSD.framework; path = ../../../../../System/Library/PrivateFrameworks/OSD.framework; sourceTree = ""; }; AA9AE87026B5BFB700B6CA65 /* CoreDisplay.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreDisplay.framework; path = ../../../../../System/Library/Frameworks/CoreDisplay.framework; sourceTree = ""; }; + AADB625926BC196900DFFAA5 /* DisplayServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DisplayServices.framework; path = ../../../../../System/Library/PrivateFrameworks/DisplayServices.framework; sourceTree = ""; }; B0C4810623357CE500053F91 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = ""; }; B0C4810823357CE500053F91 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/MainMenu.strings; sourceTree = ""; }; F01B0681228221B6008E64DB /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = ""; }; @@ -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 */, diff --git a/MonitorControl/Info.plist b/MonitorControl/Info.plist index 1553e92..55012d9 100644 --- a/MonitorControl/Info.plist +++ b/MonitorControl/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString $(MARKETING_VERSION) CFBundleVersion - 1276 + 1281 LSApplicationCategoryType public.app-category.utilities LSMinimumSystemVersion diff --git a/MonitorControl/Model/InternalDisplay.swift b/MonitorControl/Model/InternalDisplay.swift index 584b85e..db83135 100644 --- a/MonitorControl/Model/InternalDisplay.swift +++ b/MonitorControl/Model/InternalDisplay.swift @@ -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) -> 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) -> 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 - } } diff --git a/MonitorControl/Support/Bridging-Header.h b/MonitorControl/Support/Bridging-Header.h index cff1bb6..e57ee91 100644 --- a/MonitorControl/Support/Bridging-Header.h +++ b/MonitorControl/Support/Bridging-Header.h @@ -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 diff --git a/MonitorControlHelper/Info.plist b/MonitorControlHelper/Info.plist index b831f88..1e092e9 100644 --- a/MonitorControlHelper/Info.plist +++ b/MonitorControlHelper/Info.plist @@ -19,7 +19,7 @@ CFBundleShortVersionString $(MARKETING_VERSION) CFBundleVersion - 1276 + 1281 LSApplicationCategoryType public.app-category.utilities LSBackgroundOnly