| File: | Source/PTHotKeyCenter.m |
| Location: | line 105, column 2 |
| Description: | dead store |
| 1 | // |
| 2 | // PTHotKeyCenter.m |
| 3 | // Protein |
| 4 | // |
| 5 | // Created by Quentin Carnicelli on Sat Aug 02 2003. |
| 6 | // Copyright (c) 2003 Quentin D. Carnicelli. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "PTHotKeyCenter.h" |
| 10 | #import "PTHotKey.h" |
| 11 | #import "PTKeyCombo.h" |
| 12 | #import <Carbon/Carbon.h> |
| 13 | |
| 14 | @interface PTHotKeyCenter (Private) |
| 15 | - (PTHotKey*)_hotKeyForCarbonHotKey: (EventHotKeyRef)carbonHotKey; |
| 16 | - (EventHotKeyRef)_carbonHotKeyForHotKey: (PTHotKey*)hotKey; |
| 17 | |
| 18 | - (void)_updateEventHandler; |
| 19 | - (void)_hotKeyDown: (PTHotKey*)hotKey; |
| 20 | - (void)_hotKeyUp: (PTHotKey*)hotKey; |
| 21 | static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon ); |
| 22 | @end |
| 23 | |
| 24 | @implementation PTHotKeyCenter |
| 25 | |
| 26 | static id _sharedHotKeyCenter = nil0; |
| 27 | |
| 28 | + (PTHotKeyCenter *)sharedCenter |
| 29 | { |
| 30 | if( _sharedHotKeyCenter == nil0 ) |
| 31 | { |
| 32 | _sharedHotKeyCenter = [[self alloc] init]; |
| 33 | } |
| 34 | |
| 35 | return _sharedHotKeyCenter; |
| 36 | } |
| 37 | |
| 38 | - (id)init |
| 39 | { |
| 40 | self = [super init]; |
| 41 | |
| 42 | if( self ) |
| 43 | { |
| 44 | mHotKeys = [[NSMutableDictionary alloc] init]; |
| 45 | } |
| 46 | |
| 47 | return self; |
| 48 | } |
| 49 | |
| 50 | - (void)dealloc |
| 51 | { |
| 52 | [mHotKeys release]; |
| 53 | [super dealloc]; |
| 54 | } |
| 55 | |
| 56 | #pragma mark - |
| 57 | |
| 58 | - (BOOL)registerHotKey: (PTHotKey*)hotKey |
| 59 | { |
| 60 | OSStatus err; |
| 61 | EventHotKeyID hotKeyID; |
| 62 | EventHotKeyRef carbonHotKey; |
| 63 | NSValue* key; |
| 64 | |
| 65 | if( [[self allHotKeys] containsObject: hotKey] == YES( BOOL ) 1 ) |
| 66 | [self unregisterHotKey: hotKey]; |
| 67 | |
| 68 | if( [[hotKey keyCombo] isValidHotKeyCombo] == NO( BOOL ) 0 ) |
| 69 | return YES( BOOL ) 1; |
| 70 | |
| 71 | hotKeyID.signature = 'HCHk'; |
| 72 | hotKeyID.id = (long)hotKey; |
| 73 | |
| 74 | err = RegisterEventHotKey( [[hotKey keyCombo] keyCode], |
| 75 | [[hotKey keyCombo] modifiers], |
| 76 | hotKeyID, |
| 77 | GetEventDispatcherTarget(), |
| 78 | nil0, |
| 79 | &carbonHotKey ); |
| 80 | |
| 81 | if( err ) |
| 82 | return NO( BOOL ) 0; |
| 83 | |
| 84 | key = [NSValue valueWithPointer: carbonHotKey]; |
| 85 | if( hotKey && key ) |
| 86 | [mHotKeys setObject: hotKey forKey: key]; |
| 87 | |
| 88 | [self _updateEventHandler]; |
| 89 | |
| 90 | return YES( BOOL ) 1; |
| 91 | } |
| 92 | |
| 93 | - (void)unregisterHotKey: (PTHotKey*)hotKey |
| 94 | { |
| 95 | OSStatus err; |
| 96 | EventHotKeyRef carbonHotKey; |
| 97 | NSValue* key; |
| 98 | |
| 99 | if( [[self allHotKeys] containsObject: hotKey] == NO( BOOL ) 0 ) |
| 100 | return; |
| 101 | |
| 102 | carbonHotKey = [self _carbonHotKeyForHotKey: hotKey]; |
| 103 | NSAssertdo { if ( ! ( ( carbonHotKey != 0 ) ) ) { [ [ NSAssertionHandler currentHandler ] handleFailureInMethod : _cmd object : self file : [ NSString stringWithCString : "/Users/ryan/Projects/Adium/Source/PTHotKeyCenter.m" ] lineNumber : 103 description : ( ( @ "" ) ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) ] ; } } while ( 0 )( carbonHotKey != nil, @"" ); |
| 104 | |
Value stored to 'err' is never read | |
| 105 | err = UnregisterEventHotKey( carbonHotKey ); |
| 106 | //Watch as we ignore 'err': |
| 107 | |
| 108 | key = [NSValue valueWithPointer: carbonHotKey]; |
| 109 | [mHotKeys removeObjectForKey: key]; |
| 110 | |
| 111 | [self _updateEventHandler]; |
| 112 | |
| 113 | //See that? Completely ignored |
| 114 | } |
| 115 | |
| 116 | - (NSArray*)allHotKeys |
| 117 | { |
| 118 | return [mHotKeys allValues]; |
| 119 | } |
| 120 | |
| 121 | - (PTHotKey*)hotKeyWithIdentifier: (id)ident |
| 122 | { |
| 123 | NSEnumerator* hotKeysEnum = [[self allHotKeys] objectEnumerator]; |
| 124 | PTHotKey* hotKey; |
| 125 | |
| 126 | if( !ident ) |
| 127 | return nil0; |
| 128 | |
| 129 | while( (hotKey = [hotKeysEnum nextObject]) != nil0 ) |
| 130 | { |
| 131 | if( [[hotKey identifier] isEqual: ident] ) |
| 132 | return hotKey; |
| 133 | } |
| 134 | |
| 135 | return nil0; |
| 136 | } |
| 137 | |
| 138 | #pragma mark - |
| 139 | |
| 140 | - (PTHotKey*)_hotKeyForCarbonHotKey: (EventHotKeyRef)carbonHotKey |
| 141 | { |
| 142 | NSValue* key = [NSValue valueWithPointer: carbonHotKey]; |
| 143 | return [mHotKeys objectForKey: key]; |
| 144 | } |
| 145 | |
| 146 | - (EventHotKeyRef)_carbonHotKeyForHotKey: (PTHotKey*)hotKey |
| 147 | { |
| 148 | NSArray* values; |
| 149 | NSValue* value; |
| 150 | |
| 151 | values = [mHotKeys allKeysForObject: hotKey]; |
| 152 | NSAssertdo { if ( ! ( ( [ values count ] == 1 ) ) ) { [ [ NSAssertionHandler currentHandler ] handleFailureInMethod : _cmd object : self file : [ NSString stringWithCString : "/Users/ryan/Projects/Adium/Source/PTHotKeyCenter.m" ] lineNumber : 152 description : ( ( @ "Failed to find Carbon Hotkey for HotKey" ) ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) ] ; } } while ( 0 )( [values count] == 1, @"Failed to find Carbon Hotkey for HotKey" ); |
| 153 | |
| 154 | value = [values lastObject]; |
| 155 | |
| 156 | return (EventHotKeyRef)[value pointerValue]; |
| 157 | } |
| 158 | |
| 159 | - (void)_updateEventHandler |
| 160 | { |
| 161 | if( [mHotKeys count] && mEventHandlerInstalled == NO( BOOL ) 0 ) |
| 162 | { |
| 163 | EventTypeSpec eventSpec[2] = { |
| 164 | { kEventClassKeyboard, kEventHotKeyPressed }, |
| 165 | { kEventClassKeyboard, kEventHotKeyReleased } |
| 166 | }; |
| 167 | |
| 168 | InstallEventHandler( GetEventDispatcherTarget(), |
| 169 | (EventHandlerProcPtr)hotKeyEventHandler, |
| 170 | 2, eventSpec, nil0, nil0); |
| 171 | |
| 172 | mEventHandlerInstalled = YES( BOOL ) 1; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | - (void)_hotKeyDown: (PTHotKey*)hotKey |
| 177 | { |
| 178 | [hotKey invoke]; |
| 179 | } |
| 180 | |
| 181 | - (void)_hotKeyUp: (PTHotKey*)hotKey |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | - (void)sendEvent: (NSEvent*)event |
| 186 | { |
| 187 | long subType; |
| 188 | EventHotKeyRef carbonHotKey; |
| 189 | |
| 190 | if( [event type] == NSSystemDefined ) |
| 191 | { |
| 192 | subType = [event subtype]; |
| 193 | |
| 194 | if( subType == 6 ) //6 is hot key down |
| 195 | { |
| 196 | carbonHotKey= (EventHotKeyRef)[event data1]; //data1 is our hot key ref |
| 197 | if( carbonHotKey != nil0 ) |
| 198 | { |
| 199 | PTHotKey* hotKey = [self _hotKeyForCarbonHotKey: carbonHotKey]; |
| 200 | [self _hotKeyDown: hotKey]; |
| 201 | } |
| 202 | } |
| 203 | else if( subType == 9 ) //9 is hot key up |
| 204 | { |
| 205 | carbonHotKey= (EventHotKeyRef)[event data1]; |
| 206 | if( carbonHotKey != nil0 ) |
| 207 | { |
| 208 | PTHotKey* hotKey = [self _hotKeyForCarbonHotKey: carbonHotKey]; |
| 209 | [self _hotKeyUp: hotKey]; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | - (OSStatus)sendCarbonEvent: (EventRef)event |
| 216 | { |
| 217 | OSStatus err; |
| 218 | EventHotKeyID hotKeyID; |
| 219 | PTHotKey* hotKey; |
| 220 | |
| 221 | NSAssertdo { if ( ! ( ( GetEventClass ( event ) == kEventClassKeyboard ) ) ) { [ [ NSAssertionHandler currentHandler ] handleFailureInMethod : _cmd object : self file : [ NSString stringWithCString : "/Users/ryan/Projects/Adium/Source/PTHotKeyCenter.m" ] lineNumber : 221 description : ( ( @ "Unknown event class" ) ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) ] ; } } while ( 0 )( GetEventClass( event ) == kEventClassKeyboard, @"Unknown event class" ); |
| 222 | |
| 223 | err = GetEventParameter( event, |
| 224 | kEventParamDirectObject, |
| 225 | typeEventHotKeyID, |
| 226 | nil0, |
| 227 | sizeof(EventHotKeyID), |
| 228 | nil0, |
| 229 | &hotKeyID ); |
| 230 | if( err ) |
| 231 | return err; |
| 232 | |
| 233 | |
| 234 | NSAssertdo { if ( ! ( ( hotKeyID . signature == 'HCHk' ) ) ) { [ [ NSAssertionHandler currentHandler ] handleFailureInMethod : _cmd object : self file : [ NSString stringWithCString : "/Users/ryan/Projects/Adium/Source/PTHotKeyCenter.m" ] lineNumber : 234 description : ( ( @ "Invalid hot key id" ) ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) ] ; } } while ( 0 )( hotKeyID.signature == 'HCHk', @"Invalid hot key id" ); |
| 235 | NSAssertdo { if ( ! ( ( hotKeyID . id != 0 ) ) ) { [ [ NSAssertionHandler currentHandler ] handleFailureInMethod : _cmd object : self file : [ NSString stringWithCString : "/Users/ryan/Projects/Adium/Source/PTHotKeyCenter.m" ] lineNumber : 235 description : ( ( @ "Invalid hot key id" ) ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) ] ; } } while ( 0 )( hotKeyID.id != nil, @"Invalid hot key id" ); |
| 236 | |
| 237 | hotKey = (PTHotKey*)hotKeyID.id; |
| 238 | |
| 239 | switch( GetEventKind( event ) ) |
| 240 | { |
| 241 | case kEventHotKeyPressed: |
| 242 | [self _hotKeyDown: hotKey]; |
| 243 | break; |
| 244 | |
| 245 | case kEventHotKeyReleased: |
| 246 | [self _hotKeyUp: hotKey]; |
| 247 | break; |
| 248 | |
| 249 | default: |
| 250 | NSAssertdo { if ( ! ( ( 0 ) ) ) { [ [ NSAssertionHandler currentHandler ] handleFailureInMethod : _cmd object : self file : [ NSString stringWithCString : "/Users/ryan/Projects/Adium/Source/PTHotKeyCenter.m" ] lineNumber : 250 description : ( ( @ "Unknown event kind" ) ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) , ( 0 ) ] ; } } while ( 0 )( 0, @"Unknown event kind" ); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | return noErr; |
| 255 | } |
| 256 | |
| 257 | static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon ) |
| 258 | { |
| 259 | return [[PTHotKeyCenter sharedCenter] sendCarbonEvent: inEvent]; |
| 260 | } |
| 261 | |
| 262 | @end |