-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTabletApplication.m
More file actions
executable file
·193 lines (159 loc) · 6.77 KB
/
Copy pathTabletApplication.m
File metadata and controls
executable file
·193 lines (159 loc) · 6.77 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*----------------------------------------------------------------------------
NAME
TabletApplication.m -- Cocoa has added natvie support for tablet events.
While this makes some things greatly easier, there
are still some things we need to override
NSApplication to help us with.
1) Added some proxy methods to turn mouse coalesing
On & Off
2) Capture tablet proximity events and post them
as NSNotifications so that each object that
needs to know about proximity changes gets that
info, regardless of where they are in the event
chain.
3) Install a Carbon Event Handler to capture tablet
proximty events that occur when our app is in the
background. Cocoa does not nativley support
Moniter Event Targets. :(
COPYRIGHT
Author Raleigh Ledet
Copyright WACOM Technologies, Inc. 2004-2005
All rights reserved.
-----------------------------------------------------------------------------*/
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import "TabletApplication.h"
NSString *kProximityNotification = @"kProximityNotification";
NSString *kProximityEventKey = @"kProximityEventKey";
NSString *kProximityEventCarbonKey = @"kProximityEventCarbonKey";
OSStatus CarbonTabletProximityCallback(EventHandlerCallRef inCallRef,
EventRef inEvent, void *userData);
@implementation TabletApplication
/////////////////////////////////////////////////////////////////////////////
- (id)init
{
NSLog(@"Init TabletApplication");
if(self = [super init])
{
// There is no way to tell Cocoa to register for background events
// So install a carbaon event handler on the monitor target so that
// we can get background tablet proximity events.
// See the notes above the CarbonTabletProximityCallback() function at
// the bottom of this file.
OSErr status = noErr;
EventTypeSpec tabletProximityEvent = { kEventClassTablet,
kEventTabletProximity };
status = InstallEventHandler( GetEventMonitorTarget(),
NewEventHandlerUPP( CarbonTabletProximityCallback ),
1, &tabletProximityEvent, NULL, &mProxEventHandlerRef);
assert(status == noErr);
}
return self;
}
/////////////////////////////////////////////////////////////////////////////
- (void)dealloc
{
RemoveEventHandler(mProxEventHandlerRef);
[super dealloc];
}
/////////////////////////////////////////////////////////////////////////////
- (void)sendEvent:(NSEvent *)theEvent
{
if([theEvent type] == NSTabletProximity)
{
[self tabletProximity:theEvent];
return;
}
[super sendEvent:theEvent];
}
//////////////////////////////////////////////////////////////////////////////
//- (void)tabletProximity:(NSEvent *)theEvent;
//
// Tablet Proximity Events are routed through the NSResponder chain like a
// a mouse event (I think). This means that the first responder to respond to
// this event will prevent other responders from seeing it. :(
//
// Usually, every object that cares about proximity events needs to know when
// a new one comes in. I resolve this little problem by only letting the
// NSApp respond to the tabletProximity message here. The NSApp will then
// package the proximity event into an NSNotification and post that notification
// to the rest of the app.
//
// So any object in this app that cares about proximity events should register
// to recieve this notification instead of implamenting the NSResponder
// -tabletProximity: method directly.
//
- (void)tabletProximity:(NSEvent *)theEvent;
{
NSDictionary *userDict = [NSDictionary dictionaryWithObject:theEvent
forKey:kProximityEventKey];
// Send the Proximity Notification
[[NSNotificationCenter defaultCenter]
postNotificationName: kProximityNotification
object: self
userInfo: userDict];
}
//////////////////////////////////////////////////////////////////////////////
-(void)setMouseCoalescingEnabled:(BOOL)isEnabled
{
// Carbon Trickery I hope we can avoid for Tiger's release
// In fact, please file a bug about this.
// For that matter, use a DTS incident about this too.
SetMouseCoalescingEnabled(isEnabled,NULL);
}
//////////////////////////////////////////////////////////////////////////////
-(BOOL)isMouseCoalescingEnabled
{
// Carbon Trickery I hope we can avoid for Tiger's release
// In fact, please file a bug about this.
// For that matter, use a DTS incident about this too.
return IsMouseCoalescingEnabled();
}
@end
//////////////////////////////////////////////////////////////////////////////
// CarbonTabletProximityCallback
//
// Carbon Trickery I hope we can avoid for Tiger's release
// In fact, please file a bug about this.
// For that matter, use a DTS incident about this too.
//
// This is the carbon handler that gets called when a proximity event
// occurs while we are not the foreground application. This is very important.
// The user may have flipped the pen over to the eraser, or switched pens
// completely while this application is in the background. With normal Cocoa
// event processing, we would never know the new properties of the transducer
// when our app is re-activated. This could result in drawing when the user
// expects us to erase, wacko drawing because we were expecting tilt or rotation
// data that is no longer there, or vice versa, or worse, not doing anything
// because the deviceIDs do not match what we expect. Thus we have to rely on
// the Carbon Monitor Target event handler that was setup in the -init method
// of this file.
//
// The part that really sucks is that there is no way that I know of to
// take the carbon event and turn it into a true NSEvent. Thus the proximity
// notification sent from here is slightly different than the usual one above.
// So, each object that listens for Proximity Notifications need to know how
// to deal with the different data in the proximity notification. This would
// be so much easier and cleaner if Cocoa could register for background events.
OSStatus CarbonTabletProximityCallback(EventHandlerCallRef inCallRef,
EventRef inEvent, void *userData )
{
TabletProximityRec theTabletRecord = {0};
// Extract the Tablet Proximity record from the event.
if(noErr == GetEventParameter(inEvent, kEventParamTabletProximityRec,
typeTabletProximityRec, NULL,
sizeof(TabletProximityRec),
NULL, (void *)&theTabletRecord))
{
NSData *proxData = [NSData dataWithBytes:&theTabletRecord
length:sizeof(TabletProximityRec)];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:proxData
forKey:kProximityEventCarbonKey];
// Send the Proximity Notification
[[NSNotificationCenter defaultCenter]
postNotificationName: kProximityNotification
object: NSApp
userInfo: userDict];
}
return noErr;
}