Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework of ddc_read and some more ... #51

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed BrightnessMenulet/.DS_Store
Binary file not shown.
34 changes: 32 additions & 2 deletions BrightnessMenulet/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,25 @@ @interface AppDelegate ()

@end


@implementation AppDelegate

- (void)awakeFromNib
{
NSLog(@"%@ %@",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);

if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] count] > 1) {
NSLog(@"... is already running!");
[NSApp terminate:nil];
}

// just to be shure, that we didn't miss something super fancy
[super awakeFromNib];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Set Menulet Icon
NSBundle *bundle = [NSBundle mainBundle];
Expand All @@ -36,8 +53,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// init _mainMenu
[_mainMenu refreshMenuScreens];

// LMU
[LMUController singleton];
lmuCon.delegate = _mainMenu;


NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

Expand All @@ -46,14 +65,25 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

if([defaults boolForKey:@"autoBrightOnStartup"])
[lmuCon startMonitoring];

// Unregister hotkeys
[_mainMenu registerHotKeys];
}

- (void)applicationDidChangeScreenParameters:(NSNotification *)notification {
NSLog(@"AppDelegate: DidChangeScreenParameters");

// BUG: May crash if displays are connected/disconnected quickly so lets try waiting
[NSThread sleepForTimeInterval:2.0f];
[_mainMenu refreshMenuScreens];
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:_mainMenu
selector:@selector(refreshMenuScreens)
userInfo:nil
repeats:NO];
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Unregister hotkeys
[_mainMenu unregisterHotKeys];
}

@end
14 changes: 11 additions & 3 deletions BrightnessMenulet/BrightnessMenulet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
0B4E1FEC19E5FD4B0044BAFE /* Preferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0B4E1FEB19E5FD4B0044BAFE /* Preferences.xib */; };
0B4E1FEF19E5FEBB0044BAFE /* PreferencesController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B4E1FEE19E5FEBB0044BAFE /* PreferencesController.m */; };
0BCFE67319C9F1B40094B561 /* DDCControls.m in Sources */ = {isa = PBXBuildFile; fileRef = 0BCFE67219C9F1B30094B561 /* DDCControls.m */; };
498A53C91E00AA190036FC5A /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 498A53C81E00AA190036FC5A /* Carbon.framework */; };
7D2FF98311406B0100707C79 /* MainMenuController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7D2FF98211406B0100707C79 /* MainMenuController.m */; };
7D2FF98611406B2C00707C79 /* icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D2FF98411406B2C00707C79 /* icon.png */; };
7D2FF98711406B2C00707C79 /* icon-alt.png in Resources */ = {isa = PBXBuildFile; fileRef = 7D2FF98511406B2C00707C79 /* icon-alt.png */; };
Expand All @@ -37,6 +38,8 @@
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* BrightnessMenulet_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BrightnessMenulet_Prefix.pch; sourceTree = "<group>"; };
498493181E01EFFA00FA89FD /* VirtualKeyCodes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VirtualKeyCodes.h; sourceTree = "<group>"; };
498A53C81E00AA190036FC5A /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; };
7D2FF98111406B0100707C79 /* MainMenuController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MainMenuController.h; sourceTree = "<group>"; };
7D2FF98211406B0100707C79 /* MainMenuController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MainMenuController.m; sourceTree = "<group>"; };
7D2FF98411406B2C00707C79 /* icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = icon.png; sourceTree = "<group>"; };
Expand All @@ -60,6 +63,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
498A53C91E00AA190036FC5A /* Carbon.framework in Frameworks */,
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */,
7D2FF9FC1140B94600707C79 /* IOKit.framework in Frameworks */,
);
Expand Down Expand Up @@ -138,6 +142,7 @@
29B97314FDCFA39411CA2CEA /* MyStatusItem */ = {
isa = PBXGroup;
children = (
498493181E01EFFA00FA89FD /* VirtualKeyCodes.h */,
0B38B5E419E8EF9200D72911 /* AppDelegate.h */,
0B38B5E519E8EF9200D72911 /* AppDelegate.m */,
0B8CF6CC19E71BA400F3FD6F /* Menu */,
Expand Down Expand Up @@ -165,6 +170,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
498A53C81E00AA190036FC5A /* Carbon.framework */,
79897D331664F808008C7D5C /* ddc */,
1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */,
1058C7A2FEA54F0111CA2CBB /* Other Frameworks */,
Expand Down Expand Up @@ -222,7 +228,7 @@
LastUpgradeCheck = 0700;
TargetAttributes = {
8D1107260486CEB800E47090 = {
DevelopmentTeam = N6A2S4TCDQ;
ProvisioningStyle = Manual;
};
};
};
Expand Down Expand Up @@ -281,9 +287,10 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
DEVELOPMENT_TEAM = "";
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = YES;
GCC_MODEL_TUNING = G5;
Expand All @@ -306,9 +313,10 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
COMBINE_HIDPI_IMAGES = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = BrightnessMenulet_Prefix.pch;
Expand Down
Binary file removed BrightnessMenulet/Brightness_Menulet.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion BrightnessMenulet/DDCControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
+ (DDCControls*)singleton;

- (NSString*)EDIDString:(char*) string;
- (struct DDCReadResponse)readDisplay:(CGDirectDisplayID)display_id controlValue:(int)control;
- (struct DDCReadCommand)readDisplay:(CGDirectDisplayID)display_id controlValue:(int)control;
- (void)changeDisplay:(CGDirectDisplayID)display_id control:(int)control withValue:(int)value;

- (void)refreshScreens;
Expand Down
59 changes: 35 additions & 24 deletions BrightnessMenulet/DDCControls.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ - (NSString*)EDIDString:(char*) string {
: temp;
}

- (struct DDCReadResponse)readDisplay:(CGDirectDisplayID)display_id controlValue:(int)control{
- (struct DDCReadCommand)readDisplay:(CGDirectDisplayID)display_id controlValue:(int)control{
struct DDCReadCommand read_command = (struct DDCReadCommand){.control_id = control};

if(ddc_read(display_id, &read_command) != 1)
if(DDCRead(display_id, &read_command) != 1)
NSLog(@"readDisplay:%u controlValue: failed need to retry...", display_id);

return read_command.response;
return read_command;
}

- (void)changeDisplay:(CGDirectDisplayID)display_id control:(int)control withValue:(int)value{
struct DDCWriteCommand write_command = (struct DDCWriteCommand){.control_id = control, .new_value = value};

if(ddc_write(display_id, &write_command) != 1)
if(DDCWrite(display_id, &write_command) != 1)
NSLog(@"writeDisplay:%u withValue: failed need to retry...", display_id);
}

Expand All @@ -55,31 +55,42 @@ - (void)refreshScreens {

// Fetch Monitor info via EDID
struct EDID edid = {};
EDIDRead([screenNumber unsignedIntegerValue], &edid);
if (EDIDTest([screenNumber unsignedIntegerValue], &edid)) {

NSString* name;
NSString* serial;
for (NSValue *value in @[[NSValue valueWithPointer:&edid.descriptor1], [NSValue valueWithPointer:&edid.descriptor2], [NSValue valueWithPointer:&edid.descriptor3], [NSValue valueWithPointer:&edid.descriptor4]]) {
union descriptor *des = value.pointerValue;
switch (des->text.type) {
case 0xFF:
serial = [self EDIDString:des->text.data];
break;
case 0xFC:
name = [self EDIDString:des->text.data];
break;
NSString* name;
NSString* serial;
for (NSValue *value in @[[NSValue valueWithPointer:&edid.descriptor1], [NSValue valueWithPointer:&edid.descriptor2], [NSValue valueWithPointer:&edid.descriptor3], [NSValue valueWithPointer:&edid.descriptor4]]) {
union descriptor *des = value.pointerValue;
switch (des->text.type) {
case 0xFF:
serial = [self EDIDString:des->text.data];
break;
case 0xFC:
name = [self EDIDString:des->text.data];
break;
}
}
}

// don't want to manage invalid screen or integrated LCD
if(!name || [name isEqualToString:@"Color LCD"] || [name isEqualToString:@"iMac"]) continue;
// don't want to manage invalid screen or integrated LCD
if(!name || [name isEqualToString:@"Color LCD"] || [name isEqualToString:@"iMac"]) continue;

// skipping screen's, that don't support data reading
struct DDCReadCommand read_command = (struct DDCReadCommand){.control_id = BRIGHTNESS};
if(DDCRead([screenNumber unsignedIntegerValue] , &read_command) != 1) {
NSLog(@"Reading data from display:%@ failed ", screenNumber);
NSLog(@"... skipping %@ ", name);
continue;
}

// Build screen instance
NSLog(@"DDCControls: Found %@ - %@", name, screenNumber);
Screen* screen = [[Screen alloc] initWithModel:name screenID:[screenNumber unsignedIntegerValue] serial:serial];
[screen refreshValues];
// Build screen instance
NSLog(@"DDCControls: Found %@ - %@", name, screenNumber);
Screen* screen = [[Screen alloc] initWithModel:name screenID:[screenNumber unsignedIntegerValue] serial:serial];
[screen refreshValues];

[newScreens addObject:screen];
[newScreens addObject:screen];
} else {
NSLog(@"Failed to poll display: %@", screenNumber);
}
}

_screens = [newScreens copy];
Expand Down
2 changes: 2 additions & 0 deletions BrightnessMenulet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
Expand Down
1 change: 1 addition & 0 deletions BrightnessMenulet/LMUController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@property (weak) id<LMUDelegate> delegate;

@property BOOL available;
@property BOOL monitoring;

+ (LMUController*)singleton;
Expand Down
3 changes: 3 additions & 0 deletions BrightnessMenulet/LMUController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ - (io_connect_t)getLMUDataPort {
io_service_t serviceObject;

if(_lmuDataPort) return _lmuDataPort;
self.available = YES;

serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));

if(!serviceObject){
NSLog(@"LMUController: Failed to find LMU\n");
self.available = NO;
return 0;
}

Expand All @@ -64,6 +66,7 @@ - (io_connect_t)getLMUDataPort {

if(kr != KERN_SUCCESS){
NSLog(@"LMUController: Failed to open LMU IOService object");
self.available = NO;
return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions BrightnessMenulet/MainMenuController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@

- (void)refreshMenuScreens;

- (void)registerHotKeys;
- (void)unregisterHotKeys;

@end
Loading