/
UKCarbonEventHandler.m
123 lines (97 loc) · 3.18 KB
/
UKCarbonEventHandler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// UKCarbonEventHandler.m
// TalkingMoose (XC2)
//
// Created by Uli Kusterer on 26.03.06.
// Copyright 2006 Uli Kusterer.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
#import <Carbon/Carbon.h>
#import "UKCarbonEventHandler.h"
// kEventClassApplication / kEventAppFrontSwitched
NSString* UKCarbonEventHandlerEventReceived = @"UKCarbonEventHandlerEventReceived";
OSStatus UKCarbonEventHandlerFunc( EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData );
@implementation UKCarbonEventHandler
-(id) initWithEventClass: (UInt32)eclass kind: (UInt32)ekind
{
self = [super init];
if( self )
{
static EventHandlerUPP eventHandlerUPP = NULL; // Singleton used by all instances. Intentional "leak".
EventTypeSpec type;
if( !eventHandlerUPP )
eventHandlerUPP = NewEventHandlerUPP(UKCarbonEventHandlerFunc);
type.eventClass = eclass;
type.eventKind = ekind;
OSStatus err = InstallApplicationEventHandler( eventHandlerUPP, GetEventTypeCount(type), &type, self, &evtHandler );
if( err != noErr )
{
//UKLog( @"UKCarbonEventHandler: Couldn't install application event handler (Error No. %ld)", err );
[self autorelease];
return nil;
}
}
return self;
}
-(void) dealloc
{
RemoveEventHandler( evtHandler );
[super dealloc];
}
-(OSStatus) handleEvent: (EventRef)inEvent call: (EventHandlerCallRef)inHandlerCallRef
{
currCall = inHandlerCallRef;
currEvent = inEvent;
if( [self performEventActionAndPassOn] )
return eventNotHandledErr;
else
return noErr;
}
-(BOOL) callNextHandler
{
return( CallNextEventHandler( currCall, currEvent ) == noErr );
}
-(EventHandlerCallRef) currentCallRef
{
return currCall;
}
-(EventRef) currentEventRef
{
return currEvent;
}
-(BOOL) performEventActionAndPassOn
{
//UKLog(@"performEventActionAndPassOn self = %lx", (unsigned int)self);
[[NSNotificationCenter defaultCenter] postNotificationName: UKCarbonEventHandlerEventReceived object: self];
return YES;
}
@end
OSStatus UKCarbonEventHandlerFunc( EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData )
{
UKCarbonEventHandler* myself = (UKCarbonEventHandler*) inUserData;
OSStatus err = eventNotHandledErr;
NS_DURING
err = [myself handleEvent: inEvent call: inHandlerCallRef];
NS_HANDLER
NSLog( @"UKCarbonEventHandler: Error handling event: %@", localException );
NS_ENDHANDLER
return err;
}