Skip to content

Commit

Permalink
Initial refactor to allow Q to be abstracted
Browse files Browse the repository at this point in the history
  • Loading branch information
dteoh committed Jun 17, 2018
1 parent 117b8ed commit c438bbb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
6 changes: 6 additions & 0 deletions SlowQuitApps.xcodeproj/project.pbxproj
Expand Up @@ -11,6 +11,7 @@
060A2EA0194AA79500B51D29 /* SQACmdQStream.m in Sources */ = {isa = PBXBuildFile; fileRef = 060A2E9F194AA79500B51D29 /* SQACmdQStream.m */; };
060A2EA6194ABB5200B51D29 /* SQAOverlayWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 060A2EA5194ABB5200B51D29 /* SQAOverlayWindowController.m */; };
060A2EA9194ABD9B00B51D29 /* SQAOverlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 060A2EA8194ABD9B00B51D29 /* SQAOverlayView.m */; };
0625BF4020D65E9F0001FF96 /* SQAQResolver.m in Sources */ = {isa = PBXBuildFile; fileRef = 0625BF3F20D65E9F0001FF96 /* SQAQResolver.m */; };
063486B61953BA04007E8964 /* SQATerminator.m in Sources */ = {isa = PBXBuildFile; fileRef = 063486B51953BA04007E8964 /* SQATerminator.m */; };
064F6D7D1B2CD4F100874B26 /* SQAPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 064F6D7C1B2CD4F100874B26 /* SQAPreferences.m */; };
064F6D7E1B2CD4F100874B26 /* SQAPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 064F6D7C1B2CD4F100874B26 /* SQAPreferences.m */; };
Expand Down Expand Up @@ -46,6 +47,8 @@
060A2EA5194ABB5200B51D29 /* SQAOverlayWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SQAOverlayWindowController.m; sourceTree = "<group>"; };
060A2EA7194ABD9B00B51D29 /* SQAOverlayView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SQAOverlayView.h; sourceTree = "<group>"; };
060A2EA8194ABD9B00B51D29 /* SQAOverlayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SQAOverlayView.m; sourceTree = "<group>"; };
0625BF3E20D65E9F0001FF96 /* SQAQResolver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQAQResolver.h; sourceTree = "<group>"; };
0625BF3F20D65E9F0001FF96 /* SQAQResolver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SQAQResolver.m; sourceTree = "<group>"; };
063486B01953AF6C007E8964 /* SQAOverlayViewInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SQAOverlayViewInterface.h; sourceTree = "<group>"; };
063486B41953BA04007E8964 /* SQATerminator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SQATerminator.h; sourceTree = "<group>"; };
063486B51953BA04007E8964 /* SQATerminator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SQATerminator.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -150,6 +153,8 @@
060A2EA5194ABB5200B51D29 /* SQAOverlayWindowController.m */,
064F6D7B1B2CD4F100874B26 /* SQAPreferences.h */,
064F6D7C1B2CD4F100874B26 /* SQAPreferences.m */,
0625BF3E20D65E9F0001FF96 /* SQAQResolver.h */,
0625BF3F20D65E9F0001FF96 /* SQAQResolver.m */,
063486B41953BA04007E8964 /* SQATerminator.h */,
063486B51953BA04007E8964 /* SQATerminator.m */,
0695046F194A81E9007AB2D5 /* Supporting Files */,
Expand Down Expand Up @@ -294,6 +299,7 @@
064F6D7D1B2CD4F100874B26 /* SQAPreferences.m in Sources */,
0695047C194A81E9007AB2D5 /* SQAAppDelegate.m in Sources */,
060A2EA6194ABB5200B51D29 /* SQAOverlayWindowController.m in Sources */,
0625BF4020D65E9F0001FF96 /* SQAQResolver.m in Sources */,
060A2EA9194ABD9B00B51D29 /* SQAOverlayView.m in Sources */,
0601D8D6195D22C400A2CB2E /* SQADialogs.m in Sources */,
06950475194A81E9007AB2D5 /* main.m in Sources */,
Expand Down
18 changes: 13 additions & 5 deletions SlowQuitApps/SQAAppDelegate.m
@@ -1,5 +1,6 @@
@import Carbon;
#import "SQAAppDelegate.h"
#import "SQAQResolver.h"
#import "SQACmdQStream.h"
#import "SQADialogs.h"
#import "SQAOverlayWindowController.h"
Expand All @@ -10,6 +11,7 @@ @interface SQAAppDelegate() {
@private
SQACmdQStream *stream;
SQATerminator *terminator;
SQAQResolver *qResolver;
id<SQAOverlayViewInterface> overlayView;
}
@end
Expand All @@ -21,6 +23,7 @@ - (id)init {
if (self) {
overlayView = [[SQAOverlayWindowController alloc] init];
terminator = [[SQATerminator alloc] init];
qResolver = [[SQAQResolver alloc] init];
}
return self;
}
Expand Down Expand Up @@ -58,7 +61,7 @@ - (BOOL)registerGlobalHotkey {
hotKeyID.signature = 'sqad';
hotKeyID.id = 1;

OSStatus result = RegisterEventHotKey(kVK_ANSI_Q, cmdKey, hotKeyID, GetApplicationEventTarget(),
OSStatus result = RegisterEventHotKey(qResolver.keyCode, cmdKey, hotKeyID, GetApplicationEventTarget(),
kEventHotKeyExclusive, &hotKeyRef);
return result != eventHotKeyExistsErr;
}
Expand All @@ -73,7 +76,7 @@ - (void)cmdQPressed {
}];
[overlayView showOverlay:terminator.missionDurationInSeconds];

stream = [[SQACmdQStream alloc] init];
stream = [[SQACmdQStream alloc] initWithQResolver:qResolver];
__weak typeof(stream) weakStream = stream;

stream.observer = ^(BOOL pressed) {
Expand All @@ -88,6 +91,10 @@ - (void)cmdQPressed {
[stream open];
}

- (CGKeyCode)qKeyCode {
return [qResolver keyCode];
}

NSRunningApplication* findActiveApp() {
for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
if ([app isActive]) {
Expand Down Expand Up @@ -121,15 +128,16 @@ BOOL shouldHandleCmdQ() {
}

OSStatus cmdQHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) {
SQAAppDelegate *delegate = (__bridge SQAAppDelegate *)userData;

if (shouldHandleCmdQ()) {
SQAAppDelegate *delegate = (__bridge SQAAppDelegate *)userData;
[delegate cmdQPressed];
return noErr;
} else {
CGEventRef keyDownCmd, keyDownQ, keyUpQ, keyUpCmd;
keyDownCmd = CGEventCreateKeyboardEvent(NULL, kVK_Command, true);
keyDownQ = CGEventCreateKeyboardEvent(NULL, kVK_ANSI_Q, true);
keyUpQ = CGEventCreateKeyboardEvent(NULL, kVK_ANSI_Q, false);
keyDownQ = CGEventCreateKeyboardEvent(NULL, [delegate qKeyCode], true);
keyUpQ = CGEventCreateKeyboardEvent(NULL, [delegate qKeyCode], false);
keyUpCmd = CGEventCreateKeyboardEvent(NULL, kVK_Command, false);

CGEventPost(kCGAnnotatedSessionEventTap, keyDownCmd);
Expand Down
2 changes: 2 additions & 0 deletions SlowQuitApps/SQACmdQStream.h
@@ -1,11 +1,13 @@
@import Foundation;
#import "SQAQResolver.h"

typedef void(^cmd_q_observer_t)(BOOL);

@interface SQACmdQStream : NSObject

@property (strong) cmd_q_observer_t observer;

- (instancetype)initWithQResolver:(SQAQResolver *)resolver;
- (void)open;
- (void)close;

Expand Down
15 changes: 9 additions & 6 deletions SlowQuitApps/SQACmdQStream.m
Expand Up @@ -4,16 +4,19 @@
@interface SQACmdQStream() {
@private
dispatch_source_t timer;
SQAQResolver *qResolver;
}
@end

@implementation SQACmdQStream
@synthesize observer;

- (id)init {
- (id)initWithQResolver:(SQAQResolver *)resolver {
self = [super init];
if (!self) return self;

qResolver = resolver;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (!timer) {
Expand All @@ -39,19 +42,19 @@ - (void)close {

- (void)tick {
const BOOL cmdPressed = keyCmdIsPressed();
const BOOL qPressed = keyQIsPressed();
const BOOL qPressed = [self isQPressed];
const BOOL pressed = cmdPressed && qPressed;
dispatch_async(dispatch_get_main_queue(), ^{
observer(pressed);
});
}

BOOL keyCmdIsPressed() {
return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, kVK_Command);
- (BOOL)isQPressed {
return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, qResolver.keyCode);
}

BOOL keyQIsPressed() {
return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, kVK_ANSI_Q);
BOOL keyCmdIsPressed() {
return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, kVK_Command);
}

@end
7 changes: 7 additions & 0 deletions SlowQuitApps/SQAQResolver.h
@@ -0,0 +1,7 @@
@import Foundation;

@interface SQAQResolver : NSObject

- (CGKeyCode)keyCode;

@end
21 changes: 21 additions & 0 deletions SlowQuitApps/SQAQResolver.m
@@ -0,0 +1,21 @@
@import Carbon;
#import "SQAQResolver.h"

@implementation SQAQResolver

- (id)init {
self = [super init];
if (!self) return self;

return self;
}

- (CGKeyCode)keyCode {
return kVK_ANSI_Q;
}

@end

//void whatIsQ() {
//
//}

0 comments on commit c438bbb

Please sign in to comment.