Mercurial > osx-quasimode
changeset 19:8053681846ad
Refactored event tap C code into an Objective-C class.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Sun, 11 Apr 2010 23:53:49 -0700 |
parents | 78807eea31b7 |
children | 58522f82a39e |
files | .hgignore Makefile QuasimodalEventTap.h QuasimodalEventTap.m TestQuasimodalEventTap.m |
diffstat | 5 files changed, 106 insertions(+), 68 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Sun Apr 11 23:11:14 2010 -0700 +++ b/.hgignore Sun Apr 11 23:53:49 2010 -0700 @@ -1,3 +1,4 @@ syntax: glob Quasimode +TestQuasimodalEventTap *.o
--- a/Makefile Sun Apr 11 23:11:14 2010 -0700 +++ b/Makefile Sun Apr 11 23:53:49 2010 -0700 @@ -1,8 +1,18 @@ +all: Quasimode TestQuasimodalEventTap + Quasimode: JavaScriptQuasimode.o Quasimode.o clang Quasimode.o JavaScriptQuasimode.o -oQuasimode \ -framework AppKit -framework WebKit \ -framework JavaScriptCore +TestQuasimodalEventTap: QuasimodalEventTap.o TestQuasimodalEventTap.m + clang TestQuasimodalEventTap.m QuasimodalEventTap.o \ + -oTestQuasimodalEventTap \ + -framework AppKit + +QuasimodalEventTap.o: QuasimodalEventTap.m QuasimodalEventTap.h + clang -c QuasimodalEventTap.m -DDEBUG + JavaScriptQuasimode.o: JavaScriptQuasimode.m Quasimode.h JavaScriptQuasimode.h clang -c JavaScriptQuasimode.m @@ -10,4 +20,4 @@ clang -c Quasimode.m clean: - rm -f *.o Quasimode + rm -f *.o Quasimode TestQuasimodalEventTap
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QuasimodalEventTap.h Sun Apr 11 23:53:49 2010 -0700 @@ -0,0 +1,15 @@ +#import <ApplicationServices/ApplicationServices.h> +#import <CoreFoundation/CoreFoundation.h> +#import <Foundation/NSObject.h> + +@interface QuasimodalEventTap : NSObject { + CFRunLoopSourceRef rlSrcRef; + CFMachPortRef portRef; + CGKeyCode lastQuasimodalKeyCode; + CGEventFlags lastQuasimodalKeyFlags; + int numQuasimodalKeyDowns; + BOOL inQuasimode; +} +- (id)init; +- (void)finalize; +@end
--- a/QuasimodalEventTap.m Sun Apr 11 23:11:14 2010 -0700 +++ b/QuasimodalEventTap.m Sun Apr 11 23:53:49 2010 -0700 @@ -1,11 +1,9 @@ #include <AppKit/NSWorkspace.h> -#include <ApplicationServices/ApplicationServices.h> #import <Foundation/NSAutoreleasePool.h> -#import <Foundation/NSDistributedNotificationCenter.h> -#import <Foundation/NSArray.h> +#import <Foundation/NSString.h> #import <Foundation/NSDictionary.h> -#import <Foundation/NSString.h> -#include <stdio.h> + +#import "QuasimodalEventTap.h" #define QUASIMODE_KEY kCGEventFlagMaskAlternate #define MAX_STR_LEN 10 @@ -16,21 +14,19 @@ #define DEBUG_MSG(msg) #endif -CGKeyCode lastQuasimodalKeyCode; -CGEventFlags lastQuasimodalKeyFlags; -int numQuasimodalKeyDowns = 0; +static CGEventRef eventTapCallback(CGEventTapProxy proxy, + CGEventType type, + CGEventRef event, + void *refcon); -static BOOL inQuasimode = NO; - -void sendSomeKeyEvent() { +@implementation QuasimodalEventTap +- (void)sendSomeKeyEvent { // TODO: Send some-key event } -CGEventRef processEvent(CGEventTapProxy proxy, - CGEventType type, - CGEventRef event, - void *refcon) -{ +- (CGEventRef)processEventWithProxy: (CGEventTapProxy)proxy + type: (CGEventType)type + event: (CGEventRef)event { BOOL passOnEvent = !inQuasimode; if (type == kCGEventFlagsChanged) { @@ -76,7 +72,7 @@ numQuasimodalKeyDowns = 0; DEBUG_MSG("Enter quasimode\n"); } else { - sendSomeKeyEvent(); + [self sendSomeKeyEvent]; } } } else { @@ -114,7 +110,7 @@ // TODO: Send event } else { - sendSomeKeyEvent(); + [self sendSomeKeyEvent]; } } @@ -124,10 +120,51 @@ return NULL; } -CGEventRef myCallback(CGEventTapProxy proxy, - CGEventType type, - CGEventRef event, - void *refcon) +- (id)init { + if (self = [super init]) { + numQuasimodalKeyDowns = 0; + inQuasimode = NO; + + CGEventMask mask = (CGEventMaskBit(kCGEventKeyDown) | + CGEventMaskBit(kCGEventKeyUp) | + CGEventMaskBit(kCGEventFlagsChanged)); + + portRef = CGEventTapCreate(kCGHIDEventTap, + kCGHeadInsertEventTap, + 0, + mask, + eventTapCallback, + self); + + if (portRef == NULL) + printf( "CGEventTapCreate() failed.\n" ); + + rlSrcRef = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, + portRef, + 0); + + CFRunLoopAddSource(CFRunLoopGetCurrent(), + rlSrcRef, + kCFRunLoopDefaultMode); + } + return self; +} + +- (void)finalize { + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), + rlSrcRef, + kCFRunLoopDefaultMode); + + CFRelease(rlSrcRef); + CFRelease(portRef); +} +@end + +static CGEventRef eventTapCallback(CGEventTapProxy proxy, + CGEventType type, + CGEventRef event, + void *refcon) + { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; CGEventRef retval; @@ -140,54 +177,13 @@ [bundleId isEqualToString: @"com.blizzard.worldofwarcraft"]) { retval = event; } else { - retval = processEvent(proxy, type, event, refcon); + QuasimodalEventTap *tap = (QuasimodalEventTap *) refcon; + retval = [tap processEventWithProxy: proxy + type: type + event: event]; } [pool release]; return retval; } - -int main(int argc, const char *argv[] ) -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - CGEventMask mask = (CGEventMaskBit(kCGEventKeyDown) | - CGEventMaskBit(kCGEventKeyUp) | - CGEventMaskBit(kCGEventFlagsChanged)); - - CFMachPortRef portRef = CGEventTapCreate(kCGHIDEventTap, - kCGHeadInsertEventTap, - 0, - mask, - myCallback, - NULL); - - CFRunLoopSourceRef rlSrcRef; - - if (portRef == NULL) { - printf( "CGEventTapCreate() failed.\n" ); - return -1; - } - - rlSrcRef = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, - portRef, - 0); - - CFRunLoopAddSource(CFRunLoopGetCurrent(), - rlSrcRef, - kCFRunLoopDefaultMode); - - // TODO: Run app. - - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), - rlSrcRef, - kCFRunLoopDefaultMode); - - CFRelease( rlSrcRef ); - CFRelease( portRef ); - - [pool release]; - - return 0; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TestQuasimodalEventTap.m Sun Apr 11 23:53:49 2010 -0700 @@ -0,0 +1,16 @@ +#import <Foundation/NSAutoreleasePool.h> + +#import "QuasimodalEventTap.h" + +int main(int argc, const char *argv[] ) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + QuasimodalEventTap *eventTap = [[QuasimodalEventTap alloc] init]; + + CFRunLoopRun(); + + [pool release]; + + return 0; +}