Removing ITKit dependencies on F-Script framework. Currently working on ITBridging...
[ITKit.git] / ITHotKeyCenter.m
old mode 100755 (executable)
new mode 100644 (file)
index ad9c31d..366f15d
-//
-//  ITHotKeyCenter.m
-//
-//  Created by Quentin Carnicelli on Sat Aug 02 2003.
-//  Copyright (c) 2003 iThink Software. All rights reserved.
-//
-
 #import "ITHotKeyCenter.h"
 #import "ITHotKey.h"
 #import "ITKeyCombo.h"
 #import <Carbon/Carbon.h>
 
 #import "ITHotKeyCenter.h"
 #import "ITHotKey.h"
 #import "ITKeyCombo.h"
 #import <Carbon/Carbon.h>
 
-#if __PROTEIN__
-#import "NSObjectAdditions.h"
-#endif
-
 @interface ITHotKeyCenter (Private)
 - (BOOL)_hasCarbonEventSupport;
 
 @interface ITHotKeyCenter (Private)
 - (BOOL)_hasCarbonEventSupport;
 
-- (ITHotKey*)_hotKeyForCarbonHotKey: (EventHotKeyRef)carbonHotKey;
-- (EventHotKeyRef)_carbonHotKeyForHotKey: (ITHotKey*)hotKey;
+- (ITHotKey *)_hotKeyForCarbonHotKey:(EventHotKeyRef)carbonHotKey;
+- (EventHotKeyRef)_carbonHotKeyForHotKey:(ITHotKey *)hotKey;
 
 - (void)_updateEventHandler;
 
 - (void)_updateEventHandler;
-- (void)_hotKeyDown: (ITHotKey*)hotKey;
-- (void)_hotKeyUp: (ITHotKey*)hotKey;
-static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon );
+- (void)_hotKeyDown:(ITHotKey *)hotKey;
+- (void)_hotKeyUp:(ITHotKey *)hotKey;
+
+static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *refCon);
 @end
 
 @implementation ITHotKeyCenter
 
 @end
 
 @implementation ITHotKeyCenter
 
-static id _sharedHotKeyCenter = nil;
+static ITHotKeyCenter *_sharedHotKeyCenter = nil;
 
 
-+ (id)sharedCenter
-{
-       if( _sharedHotKeyCenter == nil )
-       {
++ (id)sharedCenter {
+       if (!_sharedHotKeyCenter) {
                _sharedHotKeyCenter = [[self alloc] init];
                _sharedHotKeyCenter = [[self alloc] init];
-               #if __PROTEIN__
-                       [_sharedHotKeyCenter releaseOnTerminate];
-               #endif
        }
        }
-       
        return _sharedHotKeyCenter;
 }
 
        return _sharedHotKeyCenter;
 }
 
-- (id)init
-{
-       self = [super init];
-       
-       if( self )
-       {
+- (id)init {
+       if ((self = [super init])) {
                mHotKeys = [[NSMutableDictionary alloc] init];
                mHotKeys = [[NSMutableDictionary alloc] init];
+               _enabled = YES;
        }
        }
-       
        return self;
 }
 
        return self;
 }
 
-- (void)dealloc
-{
+- (void)dealloc {
        [mHotKeys release];
        [super dealloc];
 }
 
        [mHotKeys release];
        [super dealloc];
 }
 
-#pragma mark -
+- (BOOL)isEnabled {
+       return _enabled;
+}
 
 
-- (BOOL)registerHotKey: (ITHotKey*)hotKey
-{
-       OSStatus err;
+- (void)setEnabled:(BOOL)flag {
+       _enabled = flag;
+}
+
+- (BOOL)registerHotKey:(ITHotKey *)hotKey {
        EventHotKeyID hotKeyID;
        EventHotKeyRef carbonHotKey;
        EventHotKeyID hotKeyID;
        EventHotKeyRef carbonHotKey;
-       NSValue* key;
-
-       if( [[self allHotKeys] containsObject: hotKey] == YES )
-               [self unregisterHotKey: hotKey];
+       NSValue *key;
+       
+       if ([[self allHotKeys] containsObject:hotKey]) {
+               [self unregisterHotKey:hotKey];
+       }
        
        
-       if( [[hotKey keyCombo] isValidHotKeyCombo] == NO )
+       if (![[hotKey keyCombo] isValidHotKeyCombo]) {
                return YES;
                return YES;
+       }
        
        hotKeyID.signature = 'PTHk';
        hotKeyID.id = (long)hotKey;
        
        
        hotKeyID.signature = 'PTHk';
        hotKeyID.id = (long)hotKey;
        
-       err = RegisterEventHotKey(  [[hotKey keyCombo] keyCode],
-                                                               [[hotKey keyCombo] modifiers],
-                                                               hotKeyID,
-                                                               GetEventDispatcherTarget(),
-                                                               nil,
-                                                               &carbonHotKey );
-
-       if( err )
+       if (RegisterEventHotKey([[hotKey keyCombo] keyCode], [[hotKey keyCombo] modifiers], hotKeyID, GetEventDispatcherTarget(), nil, &carbonHotKey)) {
                return NO;
                return NO;
-
-       key = [NSValue valueWithPointer: carbonHotKey];
-       [mHotKeys setObject: hotKey forKey: key];
-
+       }
+       
+       key = [NSValue valueWithPointer:carbonHotKey];
+       [mHotKeys setObject:hotKey forKey:key];
+       
        [self _updateEventHandler];
        
        return YES;
 }
 
        [self _updateEventHandler];
        
        return YES;
 }
 
-- (void)unregisterHotKey: (ITHotKey*)hotKey
-{
+- (void)unregisterHotKey:(ITHotKey *)hotKey {
        OSStatus err;
        EventHotKeyRef carbonHotKey;
        OSStatus err;
        EventHotKeyRef carbonHotKey;
-       NSValuekey;
-
-       if( [[self allHotKeys] containsObject: hotKey] == NO )
+       NSValue *key;
+       
+       if (![[self allHotKeys] containsObject:hotKey]) {
                return;
                return;
+       }
        
        
-       carbonHotKey = [self _carbonHotKeyForHotKey: hotKey];
-       NSAssert( carbonHotKey != nil, @"" );
-
-       err = UnregisterEventHotKey( carbonHotKey );
-       //Watch as we ignore 'err':
-
-       key = [NSValue valueWithPointer: carbonHotKey];
-       [mHotKeys removeObjectForKey: key];
+       carbonHotKey = [self _carbonHotKeyForHotKey:hotKey];
+       NSAssert(carbonHotKey, @"");
+       
+       err = UnregisterEventHotKey(carbonHotKey);
+       
+       key = [NSValue valueWithPointer:carbonHotKey];
+       [mHotKeys removeObjectForKey:key];
        
        [self _updateEventHandler];
        
        [self _updateEventHandler];
-
-       //See that? Completely ignored
 }
 
 }
 
-- (NSArray*)allHotKeys
-{
+- (NSArray *)allHotKeys {
        return [mHotKeys allValues];
 }
 
        return [mHotKeys allValues];
 }
 
-#pragma mark -
-
-- (BOOL)_hasCarbonEventSupport
-{
-       return floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_1;
+- (BOOL)_hasCarbonEventSupport {
+       return (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_1);
 }
 
 }
 
-- (ITHotKey*)_hotKeyForCarbonHotKey: (EventHotKeyRef)carbonHotKey
-{
-       NSValue* key = [NSValue valueWithPointer: carbonHotKey];
-       return [mHotKeys objectForKey: key];
+- (ITHotKey *)_hotKeyForCarbonHotKey:(EventHotKeyRef)carbonHotKey {
+       NSValue *key = [NSValue valueWithPointer:carbonHotKey];
+       return [mHotKeys objectForKey:key];
 }
 
 }
 
-- (EventHotKeyRef)_carbonHotKeyForHotKey: (ITHotKey*)hotKey
-{
-       NSArray* values;
-       NSValue* value;
+- (EventHotKeyRef)_carbonHotKeyForHotKey:(ITHotKey *)hotKey {
+       NSArray *values;
+       NSValue *value;
        
        
-       values = [mHotKeys allKeysForObject: hotKey];
-       NSAssert( [values count] == 1, @"Failed to find Carbon Hotkey for ITHotKey" );
+       values = [mHotKeys allKeysForObject:hotKey];
+       NSAssert(([values count] == 1), @"Failed to find Carbon Hotkey for ITHotKey");
        
        value = [values lastObject];
        
        return (EventHotKeyRef)[value pointerValue];
 }
 
        
        value = [values lastObject];
        
        return (EventHotKeyRef)[value pointerValue];
 }
 
-- (void)_updateEventHandler
-{
-       if( [self _hasCarbonEventSupport] == NO ) //Don't use event handler on these systems
+- (void)_updateEventHandler {
+       if (![self _hasCarbonEventSupport]) {
                return;
                return;
-
-       if( [mHotKeys count] && mEventHandlerInstalled == NO )
-       {
+       }
+       
+       if ([mHotKeys count] && !mEventHandlerInstalled) {
                EventTypeSpec eventSpec[2] = {
                        { kEventClassKeyboard, kEventHotKeyPressed },
                        { kEventClassKeyboard, kEventHotKeyReleased }
                EventTypeSpec eventSpec[2] = {
                        { kEventClassKeyboard, kEventHotKeyPressed },
                        { kEventClassKeyboard, kEventHotKeyReleased }
-               };    
-
-               InstallEventHandler( GetEventDispatcherTarget(),
-                                                        (EventHandlerProcPtr)hotKeyEventHandler, 
-                                                        2, eventSpec, nil, nil);
-       
+               };
+               
+               InstallEventHandler(GetEventDispatcherTarget(), (EventHandlerProcPtr)hotKeyEventHandler, 2, eventSpec, nil, nil);
+               
                mEventHandlerInstalled = YES;
        }
 }
 
                mEventHandlerInstalled = YES;
        }
 }
 
-- (void)_hotKeyDown: (ITHotKey*)hotKey
-{
+- (void)_hotKeyDown:(ITHotKey *)hotKey {
        [hotKey invoke];
 }
 
        [hotKey invoke];
 }
 
-- (void)_hotKeyUp: (ITHotKey*)hotKey
-{
+- (void)_hotKeyUp:(ITHotKey *)hotKey {
 }
 
 }
 
-- (void)sendEvent: (NSEvent*)event
-{
+- (void)sendEvent:(NSEvent *)event {
        long subType;
        EventHotKeyRef carbonHotKey;
        
        long subType;
        EventHotKeyRef carbonHotKey;
        
+       if (!_enabled) {
+               return;
+       }
+       
        //We only have to intercept sendEvent to do hot keys on old system versions
        //We only have to intercept sendEvent to do hot keys on old system versions
-       if( [self _hasCarbonEventSupport] )
+       if ([self _hasCarbonEventSupport]) {
                return;
                return;
+       }
        
        
-       if( [event type] == NSSystemDefined )
-       {
+       if ([event type] == NSSystemDefined) {
                subType = [event subtype];
                
                subType = [event subtype];
                
-               if( subType == 6 ) //6 is hot key down
-               {
-                       carbonHotKey= (EventHotKeyRef)[event data1]; //data1 is our hot key ref
-                       if( carbonHotKey != nil )
-                       {
-                               ITHotKey* hotKey = [self _hotKeyForCarbonHotKey: carbonHotKey];
-                               [self _hotKeyDown: hotKey];
+               if (subType == 6) { //6 is hot key down
+                       carbonHotKey = (EventHotKeyRef)[event data1]; //data1 is our hot key ref
+                       if (carbonHotKey) {
+                               ITHotKey *hotKey = [self _hotKeyForCarbonHotKey:carbonHotKey];
+                               [self _hotKeyDown:hotKey];
                        }
                        }
-               }
-               else if( subType == 9 ) //9 is hot key up
-               {
-                       carbonHotKey= (EventHotKeyRef)[event data1];
-                       if( carbonHotKey != nil )
-                       {
-                               ITHotKey* hotKey = [self _hotKeyForCarbonHotKey: carbonHotKey];
-                               [self _hotKeyUp: hotKey];
+               } else if (subType == 9) { //9 is hot key up
+                       carbonHotKey = (EventHotKeyRef)[event data1];
+                       if (carbonHotKey) {
+                               ITHotKey *hotKey = [self _hotKeyForCarbonHotKey:carbonHotKey];
+                               [self _hotKeyUp:hotKey];
                        }
                }
        }
 }
 
                        }
                }
        }
 }
 
-- (OSStatus)sendCarbonEvent: (EventRef)event
-{
+- (OSStatus)sendCarbonEvent:(EventRef)event {
        OSStatus err;
        EventHotKeyID hotKeyID;
        OSStatus err;
        EventHotKeyID hotKeyID;
-       ITHotKey* hotKey;
-
-       NSAssert( [self _hasCarbonEventSupport], @"" );
-       NSAssert( GetEventClass( event ) == kEventClassKeyboard, @"Unknown event class" );
+       ITHotKey *hotKey;
+       
+       if (!_enabled) {
+               return -1;
+       }
+       
+       NSAssert([self _hasCarbonEventSupport], @"");
+       NSAssert((GetEventClass(event) == kEventClassKeyboard), @"Unknown event class");
 
 
-       err = GetEventParameter(        event,
-                                                               kEventParamDirectObject, 
-                                                               typeEventHotKeyID,
-                                                               nil,
-                                                               sizeof(EventHotKeyID),
-                                                               nil,
-                                                               &hotKeyID );
-       if( err )
+       if ((err = GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, nil, sizeof(EventHotKeyID), nil, &hotKeyID))) {
                return err;
                return err;
+       }
        
        
+       NSAssert((hotKeyID.signature == 'PTHk'), @"Invalid hot key id");
+       NSAssert((hotKeyID.id != nil), @"Invalid hot key id");
 
 
-       NSAssert( hotKeyID.signature == 'PTHk', @"Invalid hot key id" );
-       NSAssert( hotKeyID.id != nil, @"Invalid hot key id" );
-
-       hotKey = (ITHotKey*)hotKeyID.id;
-
-       switch( GetEventKind( event ) )
-       {
+       hotKey = (ITHotKey *)hotKeyID.id;
+       
+       switch (GetEventKind(event)) {
                case kEventHotKeyPressed:
                case kEventHotKeyPressed:
-                       [self _hotKeyDown: hotKey];
-               break;
-
+                       [self _hotKeyDown:hotKey];
+                       break;
                case kEventHotKeyReleased:
                case kEventHotKeyReleased:
-                       [self _hotKeyUp: hotKey];
-               break;
-
+                       [self _hotKeyUp:hotKey];
+                       break;
                default:
                default:
-                       NSAssert( 0, @"Unknown event kind" );
-               break;
+                       NSAssert(NO, @"Unknown event kind");
+                       break;
        }
        
        return noErr;
 }
 
        }
        
        return noErr;
 }
 
-static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon )
-{
-       return [[ITHotKeyCenter sharedCenter] sendCarbonEvent: inEvent];
+static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *refCon) {
+       return [[ITHotKeyCenter sharedCenter] sendCarbonEvent:inEvent];
 }
 
 @end
 }
 
 @end