2 * Copyright (C) 2009 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WebInputEvent_h
32 #define WebInputEvent_h
34 #include "../platform/WebCommon.h"
35 #include "../platform/WebRect.h"
36 #include "WebTouchPoint.h"
42 // The classes defined in this file are intended to be used with
43 // WebWidget's handleInputEvent method. These event types are cross-
44 // platform and correspond closely to WebCore's Platform*Event classes.
46 // WARNING! These classes must remain PODs (plain old data). They are
47 // intended to be "serializable" by copying their raw bytes, so they must
48 // not contain any non-bit-copyable member variables!
50 // Furthermore, the class members need to be packed so they are aligned
51 // properly and don't have paddings/gaps, otherwise memory check tools
52 // like Valgrind will complain about uninitialized memory usage when
53 // transferring these classes over the wire.
57 // WebInputEvent --------------------------------------------------------------
61 WebInputEvent(unsigned sizeParam = sizeof(WebInputEvent))
63 memset(this, 0, sizeParam);
64 timeStampSeconds = 0.0;
70 // When we use an input method (or an input method editor), we receive
71 // two events for a keypress. The former event is a keydown, which
72 // provides a keycode, and the latter is a textinput, which provides
73 // a character processed by an input method. (The mapping from a
74 // keycode to a character code is not trivial for non-English
76 // To support input methods, Safari sends keydown events to WebKit for
77 // filtering. WebKit sends filtered keydown events back to Safari,
78 // which sends them to input methods.
79 // Unfortunately, it is hard to apply this design to Chrome because of
80 // our multiprocess architecture. An input method is running in a
81 // browser process. On the other hand, WebKit is running in a renderer
82 // process. So, this design results in increasing IPC messages.
83 // To support input methods without increasing IPC messages, Chrome
84 // handles keyboard events in a browser process and send asynchronous
85 // input events (to be translated to DOM events) to a renderer
87 // This design is mostly the same as the one of Windows and Mac Carbon.
88 // So, for what it's worth, our Linux and Mac front-ends emulate our
89 // Windows front-end. To emulate our Windows front-end, we can share
90 // our back-end code among Windows, Linux, and Mac.
91 // TODO(hbono): Issue 18064: remove the KeyDown type since it isn't
92 // used in Chrome any longer.
96 TypeFirst = Undefined,
106 // WebMouseWheelEvent
119 GestureScrollUpdateWithoutPropagation,
124 GestureTapUnconfirmed,
140 TypeLast = TouchCancel
144 // modifiers for all events:
150 // modifiers for keyboard events:
152 IsAutoRepeat = 1 << 5,
154 // modifiers for mouse events:
155 LeftButtonDown = 1 << 6,
156 MiddleButtonDown = 1 << 7,
157 RightButtonDown = 1 << 8,
159 // Toggle modifiers for all events. Danger: these are not reflected
160 // into WebCore, so round-tripping from WebInputEvent to a WebCore
161 // event and back will not preserve these flags.
165 // Left/right modifiers for keyboard events.
169 // Last input event to be sent for the current vsync interval. If this
170 // flag is set, the sender guarantees that no more input events will be
171 // delivered until the next vsync and the receiver can schedule
172 // rendering accordingly. If it isn't set, the receiver should not make
173 // any assumptions about the delivery times of future input events
175 IsLastInputEventForCurrentVSync = 1 << 13,
178 static const int InputModifiers = ShiftKey | ControlKey | AltKey | MetaKey;
180 double timeStampSeconds; // Seconds since epoch.
181 unsigned size; // The size of this structure, for serialization.
185 // Returns true if the WebInputEvent |type| is a mouse event.
186 static bool isMouseEventType(int type)
188 return type == MouseDown
191 || type == MouseEnter
192 || type == MouseLeave
193 || type == ContextMenu;
196 // Returns true if the WebInputEvent |type| is a keyboard event.
197 static bool isKeyboardEventType(int type)
199 return type == RawKeyDown
205 // Returns true if the WebInputEvent |type| is a touch event.
206 static bool isTouchEventType(int type)
208 return type == TouchStart
211 || type == TouchCancel;
214 // Returns true if the WebInputEvent |type| should be handled as user gesture.
215 static bool isUserGestureEventType(int type)
217 return isKeyboardEventType(type)
220 || type == TouchStart
224 // Returns true if the WebInputEvent is a gesture event.
225 static bool isGestureEventType(int type)
227 return type == GestureScrollBegin
228 || type == GestureScrollEnd
229 || type == GestureScrollUpdate
230 || type == GestureScrollUpdateWithoutPropagation
231 || type == GestureFlingStart
232 || type == GestureFlingCancel
233 || type == GesturePinchBegin
234 || type == GesturePinchEnd
235 || type == GesturePinchUpdate
236 || type == GestureTap
237 || type == GestureTapUnconfirmed
238 || type == GestureTapDown
239 || type == GestureTapCancel
240 || type == GestureShowPress
241 || type == GestureDoubleTap
242 || type == GestureTwoFingerTap
243 || type == GestureLongPress
244 || type == GestureLongTap;
248 // WebKeyboardEvent -----------------------------------------------------------
250 class WebKeyboardEvent : public WebInputEvent {
252 // Caps on string lengths so we can make them static arrays and keep
254 static const size_t textLengthCap = 4;
256 // http://www.w3.org/TR/DOM-Level-3-Events/keyset.html lists the
257 // identifiers. The longest is 18 characters, so we round up to the
258 // next multiple of 4.
259 static const size_t keyIdentifierLengthCap = 20;
261 // |windowsKeyCode| is the Windows key code associated with this key
262 // event. Sometimes it's direct from the event (i.e. on Windows),
263 // sometimes it's via a mapping function. If you want a list, see
264 // WebCore/platform/chromium/KeyboardCodes* . Note that this should
265 // ALWAYS store the non-locational version of a keycode as this is
266 // what is returned by the Windows API. For example, it should
267 // store VK_SHIFT instead of VK_RSHIFT. The location information
268 // should be stored in |modifiers|.
271 // The actual key code genenerated by the platform. The DOM spec runs
272 // on Windows-equivalent codes (thus |windowsKeyCode| above) but it
273 // doesn't hurt to have this one around.
276 // This identifies whether this event was tagged by the system as being
277 // a "system key" event (see
278 // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for
279 // details). Other platforms don't have this concept, but it's just
280 // easier to leave it always false than ifdef.
281 // See comment at the top of the file for why an int is used here.
284 // |text| is the text generated by this keystroke. |unmodifiedText| is
285 // |text|, but unmodified by an concurrently-held modifiers (except
286 // shift). This is useful for working out shortcut keys. Linux and
287 // Windows guarantee one character per event. The Mac does not, but in
288 // reality that's all it ever gives. We're generous, and cap it a bit
290 WebUChar text[textLengthCap];
291 WebUChar unmodifiedText[textLengthCap];
293 // This is a string identifying the key pressed.
294 char keyIdentifier[keyIdentifierLengthCap];
296 WebKeyboardEvent(unsigned sizeParam = sizeof(WebKeyboardEvent))
297 : WebInputEvent(sizeParam)
302 memset(&text, 0, sizeof(text));
303 memset(&unmodifiedText, 0, sizeof(unmodifiedText));
304 memset(&keyIdentifier, 0, sizeof(keyIdentifier));
307 // Sets keyIdentifier based on the value of windowsKeyCode. This is
308 // handy for generating synthetic keyboard events.
309 BLINK_EXPORT void setKeyIdentifierFromWindowsKeyCode();
311 static int windowsKeyCodeWithoutLocation(int keycode);
312 static int locationModifiersFromWindowsKeyCode(int keycode);
315 // WebMouseEvent --------------------------------------------------------------
317 class WebMouseEvent : public WebInputEvent {
319 // These values defined for WebCore::MouseButton
338 WebMouseEvent(unsigned sizeParam = sizeof(WebMouseEvent))
339 : WebInputEvent(sizeParam)
354 // WebMouseWheelEvent ---------------------------------------------------------
356 class WebMouseWheelEvent : public WebMouseEvent {
361 PhaseStationary = 1 << 1,
362 PhaseChanged = 1 << 2,
364 PhaseCancelled = 1 << 4,
365 PhaseMayBegin = 1 << 5,
373 float accelerationRatioX;
374 float accelerationRatioY;
376 // See comment at the top of the file for why an int is used here.
379 // See comment at the top of the file for why an int is used here.
380 int hasPreciseScrollingDeltas;
384 WebMouseWheelEvent(unsigned sizeParam = sizeof(WebMouseWheelEvent))
385 : WebMouseEvent(sizeParam)
390 , accelerationRatioX(1.0f)
391 , accelerationRatioY(1.0f)
392 , scrollByPage(false)
393 , hasPreciseScrollingDeltas(false)
395 , momentumPhase(PhaseNone)
400 // WebGestureEvent --------------------------------------------------------------
402 class WebGestureEvent : public WebInputEvent {
413 SourceDevice sourceDevice;
438 float firstFingerWidth;
439 float firstFingerHeight;
443 // Initial motion that triggered the scroll.
444 // May be redundant with deltaX/deltaY in the first scrollUpdate.
466 WebGestureEvent(unsigned sizeParam = sizeof(WebGestureEvent))
467 : WebInputEvent(sizeParam)
473 memset(&data, 0, sizeof(data));
477 // WebTouchEvent --------------------------------------------------------------
479 class WebTouchEvent : public WebInputEvent {
481 // Maximum number of simultaneous touches supported on
483 enum { touchesLengthCap = 12 };
485 unsigned touchesLength;
486 // List of all touches which are currently down.
487 WebTouchPoint touches[touchesLengthCap];
489 unsigned changedTouchesLength;
490 // List of all touches whose state has changed since the last WebTouchEvent
491 WebTouchPoint changedTouches[touchesLengthCap];
493 unsigned targetTouchesLength;
494 // List of all touches which are currently down and are targeting the event recipient.
495 WebTouchPoint targetTouches[touchesLengthCap];
497 WebTouchEvent(unsigned sizeParam = sizeof(WebTouchEvent))
498 : WebInputEvent(sizeParam)
500 , changedTouchesLength(0)
501 , targetTouchesLength(0)