From ee7e780efde86970e68e6ab4af6aa1b11d4ac88b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 Mar 2021 01:44:50 +0100 Subject: Register a global hotkey Build DDHotKey as a static library so we can compile it with ARC turned on and our project with ARC turned off. Since DDHotKey is written with ARC, if I include its headers directly, I'd need to enable ARC on my project in order to compile it. Initialise an `NSApplication` to give us an environment in which to register the global hotkey. A new class `Invert` will respond to the hotkey and respond accordingly. --- .gitignore | 1 + Invert.h | 7 +++++++ Invert.m | 10 ++++++++++ Makefile | 44 ++++++++++++++++++++++++++++++++++++++++++-- main.m | 30 ++++++++++++++++++++++++++++-- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 Invert.h create mode 100644 Invert.m diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/Invert.h b/Invert.h new file mode 100644 index 0000000..c84685e --- /dev/null +++ b/Invert.h @@ -0,0 +1,7 @@ +#import + +@interface Invert : NSObject + +- (void)toggleInvertColors:(NSEvent *)event; + +@end diff --git a/Invert.m b/Invert.m new file mode 100644 index 0000000..a41eb46 --- /dev/null +++ b/Invert.m @@ -0,0 +1,10 @@ +#import "Invert.h" + +@implementation Invert + +- (void)toggleInvertColors:(NSEvent *)event +{ + NSLog(@"Toggling"); +} + +@end diff --git a/Makefile b/Makefile index ef9d048..5baa173 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,42 @@ -all: - clang -x objective-c -framework CoreGraphics -o invert-catalina-invert main.m +# SOURCES := $(shell find . -not -path './lib/*' -and \( -name '*.h' -or -name '*.m' \)) +SOURCES := $(shell ls *.{h,m}) + +DDHOTKEY_OBJ := $(patsubst %.m,%.o,$(wildcard lib/DDHotKey/*.m)) + + +all: $(SOURCES) build/libddhotkey.a build/include/*.h + clang -x objective-c \ + -framework Carbon \ + -framework Cocoa \ + -framework CoreGraphics \ + -framework Foundation \ + -fno-objc-arc \ + -I./build/include \ + -L./build \ + -lddhotkey \ + -o invert-catalina-invert \ + $(SOURCES) + +build/include/*.h: lib/DDHotKey/*.h + cp $^ build/include/ + +# build/libddhotkey.o: lib/DDHotKey/*.h lib/DDHotKey/*.m +lib/DDHotKey/%.o: lib/DDHotKey/%.m + clang -x objective-c \ + -w \ + -framework Carbon \ + -framework Cocoa \ + -framework Foundation \ + -fobjc-arc \ + -c \ + $^ + + mv *.o lib/DDHotKey/ + +lib/DDHotKey/*.o: lib/DDHotKey/*.m + +build/libddhotkey.a: $(DDHOTKEY_OBJ) +# build/libddhotkey.a: lib/DDHotKey/*.o + libtool -static \ + -o $@ \ + $^ diff --git a/main.m b/main.m index 9ea3d37..8ffc3b2 100644 --- a/main.m +++ b/main.m @@ -1,8 +1,28 @@ +#import #import +#import "Invert.h" +#import "DDHotKeyCenter.h" + #define MAX_DISPLAYS 8 int main(int argc, const char * argv[]) { + [NSApplication sharedApplication]; + + Invert *invert = [[Invert alloc] init]; + + DDHotKeyCenter *c = [DDHotKeyCenter sharedHotKeyCenter]; + if ( + ![c registerHotKeyWithKeyCode:kVK_ANSI_8 + modifierFlags: + (NSEventModifierFlagControl | NSEventModifierFlagOption | NSEventModifierFlagCommand) + target:invert + action:@selector(toggleInvertColors:) + object:nil] + ) { + NSLog(@"Error registering hotkey"); + } + const CGGammaValue inverted_gamma[2] = {1, 0}; CGDirectDisplayID active_displays[MAX_DISPLAYS]; @@ -30,9 +50,15 @@ int main(int argc, const char * argv[]) { } } - sleep(4); + // for (;;) {} + // sleep(4); + // + // CGDisplayRestoreColorSyncSettings(); + + // TODO: trap? + // [invert release]; - CGDisplayRestoreColorSyncSettings(); + [NSApp run]; return 0; } -- cgit v1.2.3