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

Updated to v0.0.6, added config support #21

Open
wants to merge 3 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
15 changes: 15 additions & 0 deletions NativeDisplayBrightness.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
9DBE37561DB7990900ABE422 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9DBE37551DB7990900ABE422 /* Assets.xcassets */; };
9DBE37591DB7990900ABE422 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9DBE37571DB7990900ABE422 /* MainMenu.xib */; };
9DBE37621DB7996100ABE422 /* DDC.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DBE37601DB7996100ABE422 /* DDC.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
E20E58922648360200C1F30B /* config.json in Resources */ = {isa = PBXBuildFile; fileRef = E20E58902648360200C1F30B /* config.json */; };
E20E58932648360200C1F30B /* macOSKeyCodes.json in Resources */ = {isa = PBXBuildFile; fileRef = E20E58912648360200C1F30B /* macOSKeyCodes.json */; };
E20E58952648362800C1F30B /* ButtonControl.m in Sources */ = {isa = PBXBuildFile; fileRef = E20E58942648362800C1F30B /* ButtonControl.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -29,6 +32,10 @@
9DBE37631DB79A4000ABE422 /* BezelServices.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BezelServices.h; sourceTree = "<group>"; };
9DBE37641DB7A87200ABE422 /* Readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = Readme.md; sourceTree = "<group>"; };
9DBE37661DB7A9D600ABE422 /* License.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = License.txt; sourceTree = "<group>"; };
E20E58902648360200C1F30B /* config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = config.json; sourceTree = "<group>"; };
E20E58912648360200C1F30B /* macOSKeyCodes.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = macOSKeyCodes.json; sourceTree = "<group>"; };
E20E58942648362800C1F30B /* ButtonControl.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ButtonControl.m; sourceTree = "<group>"; };
E20E58962648363300C1F30B /* ButtonControl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ButtonControl.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -61,6 +68,8 @@
9DBE374E1DB7990900ABE422 /* NativeDisplayBrightness */ = {
isa = PBXGroup;
children = (
E20E58902648360200C1F30B /* config.json */,
E20E58912648360200C1F30B /* macOSKeyCodes.json */,
9DBE374F1DB7990900ABE422 /* AppDelegate.h */,
9DBE37501DB7990900ABE422 /* AppDelegate.m */,
9DBE37551DB7990900ABE422 /* Assets.xcassets */,
Expand All @@ -74,6 +83,8 @@
9DBE37631DB79A4000ABE422 /* BezelServices.h */,
9D1F75421DBD44310039345A /* OSD.h */,
9D1F75431DBD48310039345A /* CoreGraphicsPriv.h */,
E20E58942648362800C1F30B /* ButtonControl.m */,
E20E58962648363300C1F30B /* ButtonControl.h */,
);
path = NativeDisplayBrightness;
sourceTree = "<group>";
Expand Down Expand Up @@ -126,6 +137,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand All @@ -144,8 +156,10 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E20E58932648360200C1F30B /* macOSKeyCodes.json in Resources */,
9DBE37561DB7990900ABE422 /* Assets.xcassets in Resources */,
9DBE37591DB7990900ABE422 /* MainMenu.xib in Resources */,
E20E58922648360200C1F30B /* config.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -159,6 +173,7 @@
9DBE37621DB7996100ABE422 /* DDC.m in Sources */,
9DBE37541DB7990900ABE422 /* main.m in Sources */,
9DBE37511DB7990900ABE422 /* AppDelegate.m in Sources */,
E20E58952648362800C1F30B /* ButtonControl.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>NativeDisplayBrightness.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
4 changes: 4 additions & 0 deletions NativeDisplayBrightness/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
// Created by Benno Krauss on 19.10.16.
// Copyright © 2016 Benno Krauss. All rights reserved.
//
// Updated by Ivan_Alone on 09.05.21
// Made with GNU GPL v3 permission
//

#import <Cocoa/Cocoa.h>
#import "ButtonControl.h"

@interface AppDelegate : NSObject <NSApplicationDelegate>

Expand Down
97 changes: 94 additions & 3 deletions NativeDisplayBrightness/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Created by Benno Krauss on 19.10.16.
// Copyright © 2016 Benno Krauss. All rights reserved.
//
// Updated by Ivan_Alone on 09.05.21
// Made with GNU GPL v3 permission
//

#import "AppDelegate.h"
#import "DDC.h"
Expand All @@ -21,6 +24,8 @@
#pragma mark - variables

void *(*_BSDoGraphicWithMeterAndTimeout)(CGDirectDisplayID arg0, BSGraphic arg1, int arg2, float v, int timeout) = NULL;
ButtonControl *globalButtonBrightnessUp = NULL, *globalButtonBrightnessDown = NULL;
NSDictionary* keyCodesAll = NULL;

#pragma mark - functions

Expand All @@ -45,7 +50,9 @@ CGEventRef keyboardCGEventCallback(CGEventTapProxy proxy,
if (type == NX_KEYDOWN || type == NX_KEYUP || type == NX_FLAGSCHANGED)
{
int64_t keyCode = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
if (keyCode == kVK_F2 || keyCode == kVK_F1)
int64_t modifierFlags = CGEventGetFlags(event);

if ([globalButtonBrightnessDown verifyKey:keyCode arg2: modifierFlags] || [globalButtonBrightnessUp verifyKey:keyCode arg2: modifierFlags])
{
return NULL;
}
Expand Down Expand Up @@ -102,7 +109,7 @@ - (void)_registerGlobalKeyboardEvents
{
[NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp handler:^(NSEvent *_Nonnull event) {
//NSLog(@"event!!");
if (event.keyCode == kVK_F1)
if ([globalButtonBrightnessDown verifyKey:event.keyCode arg2: event.modifierFlags])
{
if (event.type == NSEventTypeKeyDown)
{
Expand All @@ -111,7 +118,7 @@ - (void)_registerGlobalKeyboardEvents
});
}
}
else if (event.keyCode == kVK_F2)
else if ([globalButtonBrightnessUp verifyKey:event.keyCode arg2: event.modifierFlags])
{
if (event.type == NSEventTypeKeyDown)
{
Expand Down Expand Up @@ -152,6 +159,7 @@ - (void)_loadBrightness

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self loadSettings];
if (![self _loadBezelServices])
{
[self _loadOSDFramework];
Expand Down Expand Up @@ -239,5 +247,88 @@ - (void)decreaseBrightness
self.brightness = MAX(self.brightness-brightnessStep,0);
}

- (int) parseKeyCode: (NSString*) candidate {
if (keyCodesAll == NULL) {
keyCodesAll = [self JSONFromFile:@"macOSKeyCodes"];
}

candidate = candidate.uppercaseString;

if (candidate.length < 3 || ![[candidate substringToIndex: 3] isEqualToString:@"VK_"] ) {
candidate = [NSString stringWithFormat:@"%@%@", @"VK_", candidate];
}

if (keyCodesAll != NULL && keyCodesAll[candidate]) {
return [[keyCodesAll valueForKey:candidate] intValue ];
}

return 0;
}

- (ButtonControl*) parseButtonData: (NSDictionary*) jsonButton {
ButtonControl* btn = [ButtonControl new];

NSString* keyCode = [jsonButton objectForKey:@"keyCode"];

if (keyCode != NULL) {
int keyCodeInt = keyCode.intValue;
NSString* recomp = [NSString stringWithFormat:@"%d", keyCodeInt];

if (keyCode != recomp) {
keyCodeInt = [self parseKeyCode: keyCode];
}

btn.keyCode = keyCodeInt;

btn.isShift = [[jsonButton valueForKey:@"isShift"] boolValue];
btn.isAlt = [[jsonButton valueForKey:@"isAlt" ] boolValue];
btn.isCmd = [[jsonButton valueForKey:@"isCmd" ] boolValue];
btn.isCtrl = [[jsonButton valueForKey:@"isCtrl" ] boolValue];
btn.isCaps = [[jsonButton valueForKey:@"isCaps" ] boolValue];
btn.isFN = [[jsonButton valueForKey:@"isFN" ] boolValue];

return btn;
}
return NULL;
}

- (void)loadSettings
{
NSDictionary *dict = [self JSONFromFile: @"config"];

NSDictionary* buttonBrightnessUp = [dict objectForKey:@"buttonBrightnessUp"];
NSDictionary* buttonBrightnessDown = [dict objectForKey:@"buttonBrightnessDown"];

globalButtonBrightnessUp = [ButtonControl new];
globalButtonBrightnessUp.keyCode = kVK_F2;

globalButtonBrightnessDown = [ButtonControl new];
globalButtonBrightnessDown.keyCode = kVK_F1;

if (buttonBrightnessUp != NULL) {
ButtonControl* btn = [self parseButtonData: buttonBrightnessUp];

if (btn && btn.keyCode) {
globalButtonBrightnessUp = btn;
}
}

if (buttonBrightnessDown != NULL) {
ButtonControl* btn = [self parseButtonData: buttonBrightnessDown];

if (btn && btn.keyCode) {
globalButtonBrightnessDown = btn;
}
}

NSLog(@"Config loaded!");
}

- (NSDictionary *)JSONFromFile: (NSString*) filename
{
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];
return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}

@end
29 changes: 29 additions & 0 deletions NativeDisplayBrightness/ButtonControl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ButtonControl.h
// NativeDisplayBrightness
//
// Created by Ivan_Alone on 09.05.21.
// Copyright © 2021 Ivan_Alone. All rights reserved.
//

#ifndef ButtonControl_h
#define ButtonControl_h

#import <Cocoa/Cocoa.h>

@interface ButtonControl : NSObject

@property int keyCode;

@property bool isShift;
@property bool isAlt;
@property bool isCmd;
@property bool isCtrl;
@property bool isCaps;
@property bool isFN;

- (bool)verifyKey: (int64_t)keyCode arg2: (int64_t)modifierFlags;

@end

#endif /* ButtonControl_h */
30 changes: 30 additions & 0 deletions NativeDisplayBrightness/ButtonControl.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ButtonControl.m
// NativeDisplayBrightness
//
// Created by Ivan_Alone on 09.05.21.
// Copyright © 2021 Ivan_Alone. All rights reserved.
//

#import "ButtonControl.h"

@implementation ButtonControl

- (bool)xorCheckFor: (bool)a And: (bool)b
{
return (a && b) || (!a && !b);
}

- (bool)verifyKey: (int64_t)keyCode arg2: (int64_t)modifierFlags
{
return keyCode == self.keyCode &&
[self xorCheckFor: self.isShift And: (modifierFlags & NSEventModifierFlagShift )] &&
[self xorCheckFor: self.isCmd And: (modifierFlags & NSEventModifierFlagCommand )] &&
[self xorCheckFor: self.isAlt And: (modifierFlags & NSEventModifierFlagOption )] &&
[self xorCheckFor: self.isCtrl And: (modifierFlags & NSEventModifierFlagControl )] &&
(self.isCaps ? (modifierFlags & NSEventModifierFlagCapsLock) : true) &&
(self.isFN ? (modifierFlags & NSEventModifierFlagFunction) : true);
}


@end
6 changes: 3 additions & 3 deletions NativeDisplayBrightness/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>0.0.6</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>0.0.6</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016 Benno Krauss. All rights reserved.</string>
<string>Copyright © 2016 Benno Krauss. Updated by Ivan_Alone, 2021. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
Expand Down
30 changes: 30 additions & 0 deletions NativeDisplayBrightness/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"info": "Config for editing by user",

"buttonBrightnessUp": {
"comment": "Key combination for brightness increase",
"keyCode": "VK_F2"
},
"buttonBrightnessDown": {
"comment": "Key combination for brightness decrease",
"keyCode": "VK_F1"
},

"example": {
"buttonDefinition": {
"keyCode": "VK_F5",
"isShift": false,
"isAlt": false,
"isCmd": true,
"isCtrl": true,
"isCaps": false,
"isFN": false
},
"info": {
"text0": "Definition before means Ctrl+Cmd+F5 key combination",
"text1": "You can check keys codess in 'macOSKeyCodes.json'. Also you can write just F5 instead VK_F5, for more convenience",
"text2": "'false' values can be skipped",
"text3": "Be care about 'isCaps' and 'isFN' - thats working as trigger, not key-combo. So that 'true' means 'true', 'false' means 'does not matter'"
}
}
}
Loading