Skip to content

Commit

Permalink
Add Sierra+ support
Browse files Browse the repository at this point in the history
Add new OSD.framework headers
Refactor private framework headers
Adjust code to use BezelServices or OSD.framework based on availability
  • Loading branch information
Bensge committed Oct 24, 2016
1 parent 077ccb7 commit ac664f4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
5 changes: 5 additions & 0 deletions NativeDisplayBrightness.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
9D1F75421DBD44310039345A /* OSD.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSD.h; sourceTree = "<group>"; };
9D1F75431DBD48310039345A /* CoreGraphicsPriv.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CoreGraphicsPriv.h; sourceTree = "<group>"; };
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 = "<group>"; };
9DBE37501DB7990900ABE422 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -70,6 +72,8 @@
9DBE37611DB7996100ABE422 /* DDC.h */,
9DBE37521DB7990900ABE422 /* Supporting Files */,
9DBE37631DB79A4000ABE422 /* BezelServices.h */,
9D1F75421DBD44310039345A /* OSD.h */,
9D1F75431DBD48310039345A /* CoreGraphicsPriv.h */,
);
path = NativeDisplayBrightness;
sourceTree = "<group>";
Expand Down Expand Up @@ -302,6 +306,7 @@
9DBE375F1DB7990900ABE422 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
28 changes: 24 additions & 4 deletions NativeDisplayBrightness/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "AppDelegate.h"
#import "DDC.h"
#import "BezelServices.h"
#import "OSD.h"
#include <dlfcn.h>
@import Carbon;

Expand All @@ -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

Expand Down Expand Up @@ -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]];
Expand Down Expand Up @@ -144,7 +152,10 @@ - (void)_loadBrightness

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self _loadBezelServices];
if (![self _loadBezelServices])
{
[self _loadOSDFramework];
}
[self _configureLoginItem];
[self _checkTrusted];
[self _registerGlobalKeyboardEvents];
Expand All @@ -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];
Expand Down
5 changes: 2 additions & 3 deletions NativeDisplayBrightness/BezelServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef BezelServices_h
#define BezelServices_h

#include "CoreGraphicsPriv.h"

typedef enum {
BSGraphicBacklightMeter = 0xfffffff7,
BSGraphicBacklightFailure = 0xfffffff6,
Expand All @@ -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 */
14 changes: 14 additions & 0 deletions NativeDisplayBrightness/CoreGraphicsPriv.h
Original file line number Diff line number Diff line change
@@ -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 */
48 changes: 48 additions & 0 deletions NativeDisplayBrightness/OSD.h
Original file line number Diff line number Diff line change
@@ -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 */

0 comments on commit ac664f4

Please sign in to comment.