diff --git a/NativeDisplayBrightness.xcodeproj/project.pbxproj b/NativeDisplayBrightness.xcodeproj/project.pbxproj index ff8095c..ba50f8d 100644 --- a/NativeDisplayBrightness.xcodeproj/project.pbxproj +++ b/NativeDisplayBrightness.xcodeproj/project.pbxproj @@ -15,6 +15,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 9D1F75421DBD44310039345A /* OSD.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSD.h; sourceTree = ""; }; + 9D1F75431DBD48310039345A /* CoreGraphicsPriv.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CoreGraphicsPriv.h; sourceTree = ""; }; 9DBE374C1DB7990900ABE422 /* NativeDisplayBrightness.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NativeDisplayBrightness.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9DBE374F1DB7990900ABE422 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 9DBE37501DB7990900ABE422 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -70,6 +72,8 @@ 9DBE37611DB7996100ABE422 /* DDC.h */, 9DBE37521DB7990900ABE422 /* Supporting Files */, 9DBE37631DB79A4000ABE422 /* BezelServices.h */, + 9D1F75421DBD44310039345A /* OSD.h */, + 9D1F75431DBD48310039345A /* CoreGraphicsPriv.h */, ); path = NativeDisplayBrightness; sourceTree = ""; @@ -302,6 +306,7 @@ 9DBE375F1DB7990900ABE422 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/NativeDisplayBrightness/AppDelegate.m b/NativeDisplayBrightness/AppDelegate.m index fdccbfa..8910d51 100644 --- a/NativeDisplayBrightness/AppDelegate.m +++ b/NativeDisplayBrightness/AppDelegate.m @@ -9,6 +9,7 @@ #import "AppDelegate.h" #import "DDC.h" #import "BezelServices.h" +#import "OSD.h" #include @import Carbon; @@ -19,7 +20,7 @@ #pragma mark - variables -void *(*_BSDoGraphicWithMeterAndTimeout)(CGDirectDisplayID arg0, BSGraphic arg1, int arg2, float v, int timeout); +void *(*_BSDoGraphicWithMeterAndTimeout)(CGDirectDisplayID arg0, BSGraphic arg1, int arg2, float v, int timeout) = NULL; #pragma mark - functions @@ -63,18 +64,25 @@ @interface AppDelegate () @implementation AppDelegate @synthesize brightness=_brightness; -- (void)_loadBezelServices +- (BOOL)_loadBezelServices { // Load BezelServices framework void *handle = dlopen("/System/Library/PrivateFrameworks/BezelServices.framework/Versions/A/BezelServices", RTLD_GLOBAL); if (!handle) { NSLog(@"Error opening framework"); + return NO; } else { _BSDoGraphicWithMeterAndTimeout = dlsym(handle, "BSDoGraphicWithMeterAndTimeout"); + return _BSDoGraphicWithMeterAndTimeout != NULL; } } +- (BOOL)_loadOSDFramework +{ + return [[NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/OSD.framework"] load]; +} + - (void)_configureLoginItem { NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; @@ -144,7 +152,10 @@ - (void)_loadBrightness - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - [self _loadBezelServices]; + if (![self _loadBezelServices]) + { + [self _loadOSDFramework]; + } [self _configureLoginItem]; [self _checkTrusted]; [self _registerGlobalKeyboardEvents]; @@ -168,7 +179,16 @@ - (void)setBrightness:(float)value _brightness = value; CGDirectDisplayID display = CGSMainDisplayID(); - _BSDoGraphicWithMeterAndTimeout(display, BSGraphicBacklightMeter, 0x0, value/100.f, 1); + + if (_BSDoGraphicWithMeterAndTimeout != NULL) + { + // El Capitan and probably older systems + _BSDoGraphicWithMeterAndTimeout(display, BSGraphicBacklightMeter, 0x0, value/100.f, 1); + } + else { + // Sierra+ + [[NSClassFromString(@"OSDManager") sharedManager] showImage:OSDGraphicBacklight onDisplayID:CGSMainDisplayID() priority:OSDPriorityDefault msecUntilFade:1000 filledChiclets:value/brightnessStep totalChiclets:100.f/brightnessStep locked:NO]; + } for (NSScreen *screen in NSScreen.screens) { NSDictionary *description = [screen deviceDescription]; diff --git a/NativeDisplayBrightness/BezelServices.h b/NativeDisplayBrightness/BezelServices.h index 263c58b..118ad26 100644 --- a/NativeDisplayBrightness/BezelServices.h +++ b/NativeDisplayBrightness/BezelServices.h @@ -9,6 +9,8 @@ #ifndef BezelServices_h #define BezelServices_h +#include "CoreGraphicsPriv.h" + typedef enum { BSGraphicBacklightMeter = 0xfffffff7, BSGraphicBacklightFailure = 0xfffffff6, @@ -34,7 +36,4 @@ extern void *BSDoGraphicWithMessage(CGDirectDisplayID arg0, BSGraphic arg1, int extern void *BSDoGraphicWithMeterAndTimeout(CGDirectDisplayID arg0, BSGraphic arg1, int arg2, float v, int timeout); extern void *LoadBezelServicesConnection(); -CG_EXTERN CGDirectDisplayID CGSMainDisplayID(void); - - #endif /* BezelServices_h */ diff --git a/NativeDisplayBrightness/CoreGraphicsPriv.h b/NativeDisplayBrightness/CoreGraphicsPriv.h new file mode 100644 index 0000000..37284fe --- /dev/null +++ b/NativeDisplayBrightness/CoreGraphicsPriv.h @@ -0,0 +1,14 @@ +// +// CoreGraphicsPriv.h +// NativeDisplayBrightness +// +// Created by Benno Krauss on 23.10.16. +// Copyright © 2016 Benno Krauss. All rights reserved. +// + +#ifndef CoreGraphicsPriv_h +#define CoreGraphicsPriv_h + +CG_EXTERN CGDirectDisplayID CGSMainDisplayID(void); + +#endif /* CoreGraphicsPriv_h */ diff --git a/NativeDisplayBrightness/OSD.h b/NativeDisplayBrightness/OSD.h new file mode 100644 index 0000000..64f8ae6 --- /dev/null +++ b/NativeDisplayBrightness/OSD.h @@ -0,0 +1,48 @@ +// +// OSD.h +// NativeDisplayBrightness +// +// Created by Benno Krauss on 23.10.16. +// Copyright © 2016 Benno Krauss. All rights reserved. +// + +#ifndef OSD_h +#define OSD_h + +#include "CoreGraphicsPriv.h" + +typedef enum { + OSDGraphicBacklight = 1,//0xfffffff7, + OSDGraphicEject = 6, + OSDGraphicNoWiFi = 9, + //You can reverse these yourself if you need them, it's easy trial-and-error + /* + BSGraphicKeyboardBacklightMeter = //0xfffffff1, + BSGraphicKeyboardBacklightDisabledMeter = //0xfffffff0, + BSGraphicKeyboardBacklightNotConnected = //0xffffffef, + BSGraphicKeyboardBacklightDisabledNotConnected = //0xffffffee, + BSGraphicMacProOpen = //0xffffffe9, + BSGraphicSpeakerMuted = //0xffffffe8, + BSGraphicSpeaker = //0xffffffe7, + BSGraphicSpeakerDisabled = //0xffffffe7, + BSGraphicRemoteBattery = //0xffffffe6, + BSGraphicHotspot = //0xffffffe5, + BSGraphicSleep = //0xffffffe3, + BSGraphicSpeaker = 3//0xffffffe2, + BSGraphicNewRemoteBattery = //0xffffffcb, + */ +} OSDGraphic; + +typedef enum { + OSDPriorityDefault = 0x1f4 +} OSDPriority; + +@interface OSDManager : NSObject ++ (instancetype)sharedManager; +- (void)showImage:(OSDGraphic)image onDisplayID:(CGDirectDisplayID)display priority:(OSDPriority)priority msecUntilFade:(int)timeout; +- (void)showImage:(OSDGraphic)image onDisplayID:(CGDirectDisplayID)display priority:(OSDPriority)priority msecUntilFade:(int)timeout withText:(NSString *)text; +- (void)showImage:(OSDGraphic)image onDisplayID:(CGDirectDisplayID)display priority:(OSDPriority)priority msecUntilFade:(int)timeout filledChiclets:(int)filled totalChiclets:(int)total locked:(BOOL)locked; +@end + + +#endif /* OSD_h */