2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 // FIXME: Rename this file to WebEventIOS.mm after we upstream the iOS port and remove the PLATFORM(IOS)-guard.
31 #import "KeyEventCocoa.h"
32 #import <wtf/Assertions.h>
36 #import "WAKAppKitStubs.h"
38 using WebCore::windowsKeyCodeForKeyCode;
39 using WebCore::windowsKeyCodeForCharCode;
40 @implementation WebEvent
42 @synthesize type = _type;
43 @synthesize timestamp = _timestamp;
44 @synthesize wasHandled = _wasHandled;
46 - (WebEvent *)initWithMouseEventType:(WebEventType)type
47 timeStamp:(CFTimeInterval)timeStamp
48 location:(CGPoint)point
55 _timestamp = timeStamp;
57 _locationInWindow = point;
62 - (WebEvent *)initWithScrollWheelEventWithTimeStamp:(CFTimeInterval)timeStamp
63 location:(CGPoint)point
71 _type = WebEventScrollWheel;
72 _timestamp = timeStamp;
74 _locationInWindow = point;
81 - (WebEvent *)initWithTouchEventType:(WebEventType)type
82 timeStamp:(CFTimeInterval)timeStamp
83 location:(CGPoint)point
84 modifiers:(WebEventFlags)modifiers
85 touchCount:(unsigned)touchCount
86 touchLocations:(NSArray *)touchLocations
87 touchIdentifiers:(NSArray *)touchIdentifiers
88 touchPhases:(NSArray *)touchPhases isGesture:(BOOL)isGesture
89 gestureScale:(float)gestureScale
90 gestureRotation:(float)gestureRotation
97 _timestamp = timeStamp;
98 _modifierFlags = modifiers;
100 // FIXME: <rdar://problem/7185284> TouchEvents may be in more than one window some day.
101 _locationInWindow = point;
102 _touchCount = touchCount;
103 _touchLocations = [touchLocations copy];
104 _touchIdentifiers = [touchIdentifiers copy];
105 _touchPhases = [touchPhases copy];
106 _isGesture = isGesture;
107 _gestureScale = gestureScale;
108 _gestureRotation = gestureRotation;
113 static int windowsKeyCodeForCharCodeIOS(unichar charCode)
115 // iPhone Specific Cases
116 // <rdar://7709408>: We get 10 ('\n') from UIKit when using the software keyboard
121 return windowsKeyCodeForCharCode(charCode);
124 // FIXME: to be removed when the adoption of the new initializer is complete.
125 - (WebEvent *)initWithKeyEventType:(WebEventType)type
126 timeStamp:(CFTimeInterval)timeStamp
127 characters:(NSString *)characters
128 charactersIgnoringModifiers:(NSString *)charactersIgnoringModifiers
129 modifiers:(WebEventFlags)modifiers
130 isRepeating:(BOOL)repeating
131 withFlags:(NSUInteger)flags
132 keyCode:(uint16_t)keyCode
133 isTabKey:(BOOL)tabKey
134 characterSet:(WebEventCharacterSet)characterSet
136 UNUSED_PARAM(characterSet);
142 _timestamp = timeStamp;
144 _characters = [characters retain];
145 _charactersIgnoringModifiers = [charactersIgnoringModifiers retain];
146 _modifierFlags = modifiers;
147 _keyRepeating = repeating;
148 _keyboardFlags = flags;
152 _keyCode = windowsKeyCodeForKeyCode(keyCode);
153 else if ([charactersIgnoringModifiers length] == 1) {
154 // This event is likely for a software keyboard-generated event.
155 _keyCode = windowsKeyCodeForCharCodeIOS([charactersIgnoringModifiers characterAtIndex:0]);
161 - (WebEvent *)initWithKeyEventType:(WebEventType)type
162 timeStamp:(CFTimeInterval)timeStamp
163 characters:(NSString *)characters
164 charactersIgnoringModifiers:(NSString *)charactersIgnoringModifiers
165 modifiers:(WebEventFlags)modifiers
166 isRepeating:(BOOL)repeating
167 withFlags:(NSUInteger)flags
168 withInputManagerHint:(NSString *)hint
169 keyCode:(uint16_t)keyCode
170 isTabKey:(BOOL)tabKey
177 _timestamp = timeStamp;
179 _characters = [characters retain];
180 _charactersIgnoringModifiers = [charactersIgnoringModifiers retain];
181 _modifierFlags = modifiers;
182 _keyRepeating = repeating;
183 _keyboardFlags = flags;
184 _inputManagerHint = [hint retain];
188 _keyCode = windowsKeyCodeForKeyCode(keyCode);
189 else if ([charactersIgnoringModifiers length] == 1) {
190 // This event is likely for a software keyboard-generated event.
191 _keyCode = windowsKeyCodeForCharCodeIOS([charactersIgnoringModifiers characterAtIndex:0]);
199 [_characters release];
200 [_charactersIgnoringModifiers release];
201 [_inputManagerHint release];
203 [_touchLocations release];
204 [_touchIdentifiers release];
205 [_touchPhases release];
210 - (NSString *)_typeDescription
213 case WebEventMouseDown:
214 return @"WebEventMouseDown";
215 case WebEventMouseUp:
216 return @"WebEventMouseUp";
217 case WebEventMouseMoved:
218 return @"WebEventMouseMoved";
219 case WebEventScrollWheel:
220 return @"WebEventScrollWheel";
221 case WebEventKeyDown:
222 return @"WebEventKeyDown";
224 return @"WebEventKeyUp";
225 case WebEventTouchBegin:
226 return @"WebEventTouchBegin";
227 case WebEventTouchChange:
228 return @"WebEventTouchChange";
229 case WebEventTouchEnd:
230 return @"WebEventTouchEnd";
231 case WebEventTouchCancel:
232 return @"WebEventTouchCancel";
234 ASSERT_NOT_REACHED();
239 - (NSString *)_modiferFlagsDescription
241 switch (_modifierFlags) {
242 case WebEventMouseDown:
243 return @"WebEventMouseDown";
244 case WebEventMouseUp:
245 return @"WebEventMouseUp";
246 case WebEventMouseMoved:
247 return @"WebEventMouseMoved";
248 case WebEventScrollWheel:
249 return @"WebEventScrollWheel";
250 case WebEventKeyDown:
251 return @"WebEventKeyDown";
253 return @"WebEventKeyUp";
254 case WebEventTouchBegin:
255 return @"WebEventTouchBegin";
256 case WebEventTouchChange:
257 return @"WebEventTouchChange";
258 case WebEventTouchEnd:
259 return @"WebEventTouchEnd";
260 case WebEventTouchCancel:
261 return @"WebEventTouchCancel";
263 ASSERT_NOT_REACHED();
268 - (NSString *)_touchLocationsDescription:(NSArray *)locations
270 BOOL shouldAddComma = NO;
271 NSMutableString *description = [NSMutableString string];
272 for (NSValue *value in locations) {
273 CGPoint point = [value pointValue];
274 [description appendFormat:@"%@(%f, %f)", (shouldAddComma ? @", " : @""), point.x, point.y];
275 shouldAddComma = YES;
280 - (NSString *)_touchIdentifiersDescription
282 BOOL shouldAddComma = NO;
283 NSMutableString *description = [NSMutableString string];
284 for (NSNumber *identifier in _touchIdentifiers) {
285 [description appendFormat:@"%@%u", (shouldAddComma ? @", " : @""), [identifier unsignedIntValue]];
286 shouldAddComma = YES;
291 - (NSString *)_touchPhaseDescription:(WebEventTouchPhaseType)phase
294 case WebEventTouchPhaseBegan:
295 return @"WebEventTouchPhaseBegan";
296 case WebEventTouchPhaseMoved:
297 return @"WebEventTouchPhaseMoved";
298 case WebEventTouchPhaseStationary:
299 return @"WebEventTouchPhaseStationary";
300 case WebEventTouchPhaseEnded:
301 return @"WebEventTouchPhaseEnded";
302 case WebEventTouchPhaseCancelled:
303 return @"WebEventTouchPhaseCancelled";
305 ASSERT_NOT_REACHED();
310 - (NSString *)_touchPhasesDescription
312 BOOL shouldAddComma = NO;
313 NSMutableString *description = [NSMutableString string];
314 for (NSNumber *phase in _touchPhases) {
315 [description appendFormat:@"%@%@", (shouldAddComma ? @", " : @""), [self _touchPhaseDescription:static_cast<WebEventTouchPhaseType>([phase unsignedIntValue])]];
316 shouldAddComma = YES;
321 - (NSString *)_eventDescription
324 case WebEventMouseDown:
325 case WebEventMouseUp:
326 case WebEventMouseMoved:
327 return [NSString stringWithFormat:@"location: (%f, %f)", _locationInWindow.x, _locationInWindow.y];
328 case WebEventScrollWheel:
329 return [NSString stringWithFormat:@"location: (%f, %f) deltaX: %f deltaY: %f", _locationInWindow.x, _locationInWindow.y, _deltaX, _deltaY];
330 case WebEventKeyDown:
332 return [NSString stringWithFormat:@"chars: %@ charsNoModifiers: %@ flags: %d repeating: %d keyboardFlags: %lu keyCode %d, isTab: %d", _characters, _charactersIgnoringModifiers, _modifierFlags, _keyRepeating, static_cast<unsigned long>(_keyboardFlags), _keyCode, _tabKey];
333 case WebEventTouchBegin:
334 case WebEventTouchChange:
335 case WebEventTouchEnd:
336 case WebEventTouchCancel:
337 return [NSString stringWithFormat:@"location: (%f, %f) count: %d locations: %@ identifiers: %@ phases: %@ isGesture: %d scale: %f rotation: %f", _locationInWindow.x, _locationInWindow.y, _touchCount, [self _touchLocationsDescription:_touchLocations], [self _touchIdentifiersDescription], [self _touchPhasesDescription], (_isGesture ? 1 : 0), _gestureScale, _gestureRotation];
339 ASSERT_NOT_REACHED();
344 - (NSString *)description
346 return [NSString stringWithFormat:@"%@ type: %@ - %@", [super description], [self _typeDescription], [self _eventDescription]];
349 - (CGPoint)locationInWindow
351 ASSERT_WITH_MESSAGE(_type == WebEventMouseDown || _type == WebEventMouseUp || _type == WebEventMouseMoved || _type == WebEventScrollWheel
352 // FIXME: <rdar://problem/7185284> TouchEvents may be in more than one window some day.
353 || _type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel
354 , "WebEventType: %d", _type);
355 return _locationInWindow;
358 - (NSString *)characters
360 ASSERT(_type == WebEventKeyDown || _type == WebEventKeyUp);
361 return [[_characters retain] autorelease];
364 - (NSString *)charactersIgnoringModifiers
366 ASSERT(_type == WebEventKeyDown || _type == WebEventKeyUp);
367 return [[_charactersIgnoringModifiers retain] autorelease];
370 - (NSString *)inputManagerHint
372 return [[_inputManagerHint retain] autorelease];
375 - (WebEventFlags)modifierFlags
377 return _modifierFlags;
380 - (BOOL)isKeyRepeating
382 ASSERT(_type == WebEventKeyDown || _type == WebEventKeyUp);
383 return _keyRepeating;
386 - (NSUInteger)keyboardFlags
388 ASSERT(_type == WebEventKeyDown || _type == WebEventKeyUp);
389 return _keyboardFlags;
394 ASSERT(_type == WebEventKeyDown || _type == WebEventKeyUp);
400 ASSERT(_type == WebEventKeyDown || _type == WebEventKeyUp);
406 ASSERT(_type == WebEventScrollWheel);
412 ASSERT(_type == WebEventScrollWheel);
417 - (unsigned)touchCount
419 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
423 - (NSArray *)touchLocations
425 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
426 return _touchLocations;
429 - (NSArray *)touchIdentifiers
431 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
432 return _touchIdentifiers;
435 - (NSArray *)touchPhases
437 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
444 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
448 - (float)gestureScale
450 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
451 return _gestureScale;
454 - (float)gestureRotation
456 ASSERT(_type == WebEventTouchBegin || _type == WebEventTouchChange || _type == WebEventTouchEnd || _type == WebEventTouchCancel);
457 return _gestureRotation;
462 #endif // PLATFORM(IOS)