Add copyright notices, license, and readme.
2 // InterpreterKeyController.m
5 // Created by Kevin on 1/20/08.
6 // Copyright (c) 2008 Kevin A. Mitchell
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #import "InterpreterKeyController.h"
30 static CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
32 UniChar unicodeString[10];
33 UniCharCount actualStringLength;
35 if (![InterpreterKeyController enabled])
38 CGEventKeyboardGetUnicodeString(event, sizeof(unicodeString) / sizeof(*unicodeString), &actualStringLength, unicodeString);
40 if ((CGEventGetFlags(event) & kCGEventFlagMaskControl == kCGEventFlagMaskControl) &&
41 actualStringLength == 1 &&
42 unicodeString[0] == 3)
46 // Really important to get the GIL lock. Python gives it up
47 // every 100 bytecodes, so we won't wait too long.
48 PyGILState_STATE gilstate = PyGILState_Ensure();
49 // And once the main thread gets control again, it'll get a
52 PyGILState_Release(gilstate);
54 // In this case, we used the event, so just make it null
55 CGEventSetType(event, kCGEventNull);
61 @implementation InterpreterKeyController
62 static BOOL interruptsEnabled = NO;
66 return interruptsEnabled;
69 + (void) setEnabled: (BOOL) enabled
71 interruptsEnabled = enabled;
76 [NSThread detachNewThreadSelector: @selector(threadproc:) toTarget: self withObject: NULL];
79 - (void) threadproc: (void*) arg
81 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
83 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
87 CFMachPortRef machPortRef = NULL;
88 ProcessSerialNumber currentProcess;
89 GetCurrentProcess(¤tProcess);
90 machPortRef = CGEventTapCreateForPSN(¤tProcess,
91 kCGHeadInsertEventTap,
93 CGEventMaskBit(kCGEventKeyDown),
94 (CGEventTapCallBack)eventTapFunction,
96 if (machPortRef == NULL)
97 NSLog(@"CGEventTapCreate failed!");
99 CFRunLoopSourceRef eventSrc = CFMachPortCreateRunLoopSource(NULL, machPortRef, 0);
100 if ( eventSrc == NULL )
101 NSLog(@"CFMachPortCreateRunLoopSource failed!");
103 CFRunLoopAddSource([runLoop getCFRunLoop], eventSrc, kCFRunLoopDefaultMode);
116 indent-tabs-mode: nil