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 // When we use an input method (or an input method editor), we receive
62 // two events for a keypress. The former event is a keydown, which
63 // provides a keycode, and the latter is a textinput, which provides
64 // a character processed by an input method. (The mapping from a
65 // keycode to a character code is not trivial for non-English
67 // To support input methods, Safari sends keydown events to WebKit for
68 // filtering. WebKit sends filtered keydown events back to Safari,
69 // which sends them to input methods.
70 // Unfortunately, it is hard to apply this design to Chrome because of
71 // our multiprocess architecture. An input method is running in a
72 // browser process. On the other hand, WebKit is running in a renderer
73 // process. So, this design results in increasing IPC messages.
74 // To support input methods without increasing IPC messages, Chrome
75 // handles keyboard events in a browser process and send asynchronous
76 // input events (to be translated to DOM events) to a renderer
78 // This design is mostly the same as the one of Windows and Mac Carbon.
79 // So, for what it's worth, our Linux and Mac front-ends emulate our
80 // Windows front-end. To emulate our Windows front-end, we can share
81 // our back-end code among Windows, Linux, and Mac.
82 // TODO(hbono): Issue 18064: remove the KeyDown type since it isn't
83 // used in Chrome any longer.
87 TypeFirst = Undefined,
91 MouseTypeFirst = MouseDown,
97 MouseTypeLast = ContextMenu,
104 KeyboardTypeFirst = RawKeyDown,
108 KeyboardTypeLast = Char,
112 GestureTypeFirst = GestureScrollBegin,
115 GestureScrollUpdateWithoutPropagation,
120 GestureTapUnconfirmed,
130 GestureTypeLast = GesturePinchUpdate,
134 TouchTypeFirst = TouchStart,
138 TouchTypeLast = TouchCancel,
140 TypeLast = TouchTypeLast
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 MouseTypeFirst <= type && type <= MouseTypeLast;
191 // Returns true if the WebInputEvent |type| is a keyboard event.
192 static bool isKeyboardEventType(int type)
194 return KeyboardTypeFirst <= type && type <= KeyboardTypeLast;
197 // Returns true if the WebInputEvent |type| is a touch event.
198 static bool isTouchEventType(int type)
200 return TouchTypeFirst <= type && type <= TouchTypeLast;
203 // Returns true if the WebInputEvent is a gesture event.
204 static bool isGestureEventType(int type)
206 return GestureTypeFirst <= type && type <= GestureTypeLast;
210 explicit WebInputEvent(unsigned sizeParam)
212 memset(this, 0, sizeParam);
213 timeStampSeconds = 0.0;
220 // WebKeyboardEvent -----------------------------------------------------------
222 class WebKeyboardEvent : public WebInputEvent {
224 // Caps on string lengths so we can make them static arrays and keep
226 static const size_t textLengthCap = 4;
228 // http://www.w3.org/TR/DOM-Level-3-Events/keyset.html lists the
229 // identifiers. The longest is 18 characters, so we round up to the
230 // next multiple of 4.
231 static const size_t keyIdentifierLengthCap = 20;
233 // |windowsKeyCode| is the Windows key code associated with this key
234 // event. Sometimes it's direct from the event (i.e. on Windows),
235 // sometimes it's via a mapping function. If you want a list, see
236 // WebCore/platform/chromium/KeyboardCodes* . Note that this should
237 // ALWAYS store the non-locational version of a keycode as this is
238 // what is returned by the Windows API. For example, it should
239 // store VK_SHIFT instead of VK_RSHIFT. The location information
240 // should be stored in |modifiers|.
243 // The actual key code genenerated by the platform. The DOM spec runs
244 // on Windows-equivalent codes (thus |windowsKeyCode| above) but it
245 // doesn't hurt to have this one around.
248 // This identifies whether this event was tagged by the system as being
249 // a "system key" event (see
250 // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for
251 // details). Other platforms don't have this concept, but it's just
252 // easier to leave it always false than ifdef.
253 // See comment at the top of the file for why an int is used here.
256 // |text| is the text generated by this keystroke. |unmodifiedText| is
257 // |text|, but unmodified by an concurrently-held modifiers (except
258 // shift). This is useful for working out shortcut keys. Linux and
259 // Windows guarantee one character per event. The Mac does not, but in
260 // reality that's all it ever gives. We're generous, and cap it a bit
262 WebUChar text[textLengthCap];
263 WebUChar unmodifiedText[textLengthCap];
265 // This is a string identifying the key pressed.
266 char keyIdentifier[keyIdentifierLengthCap];
269 : WebInputEvent(sizeof(WebKeyboardEvent))
274 memset(&text, 0, sizeof(text));
275 memset(&unmodifiedText, 0, sizeof(unmodifiedText));
276 memset(&keyIdentifier, 0, sizeof(keyIdentifier));
279 // Sets keyIdentifier based on the value of windowsKeyCode. This is
280 // handy for generating synthetic keyboard events.
281 BLINK_EXPORT void setKeyIdentifierFromWindowsKeyCode();
283 static int windowsKeyCodeWithoutLocation(int keycode);
284 static int locationModifiersFromWindowsKeyCode(int keycode);
287 // WebMouseEvent --------------------------------------------------------------
289 class WebMouseEvent : public WebInputEvent {
310 : WebInputEvent(sizeof(WebMouseEvent))
325 explicit WebMouseEvent(unsigned sizeParam)
326 : WebInputEvent(sizeParam)
341 // WebMouseWheelEvent ---------------------------------------------------------
343 class WebMouseWheelEvent : public WebMouseEvent {
348 PhaseStationary = 1 << 1,
349 PhaseChanged = 1 << 2,
351 PhaseCancelled = 1 << 4,
352 PhaseMayBegin = 1 << 5,
360 float accelerationRatioX;
361 float accelerationRatioY;
363 // See comment at the top of the file for why an int is used here.
366 // See comment at the top of the file for why an int is used here.
367 int hasPreciseScrollingDeltas;
371 // See comment at the top of the file for why an int is used here.
372 // Rubberbanding is an OSX visual effect. When a user scrolls the content
373 // area with a track pad, and the content area is already at its limit in
374 // the direction being scrolled, the entire content area is allowed to
375 // scroll slightly off screen, revealing a grey background. When the user
376 // lets go, the content area snaps back into place. Blink is responsible
377 // for this rubberbanding effect, but the embedder may wish to disable
378 // rubber banding in the left or right direction, if the scroll should have
379 // an alternate effect. The common case is that a scroll in the left or
380 // right directions causes a back or forwards navigation, respectively.
382 // These flags prevent rubber banding from starting in a given direction,
383 // but have no effect on an ongoing rubber banding. A rubber banding that
384 // started in the vertical direction is allowed to continue in the right
385 // direction, even if canRubberbandRight is 0.
386 int canRubberbandLeft;
387 int canRubberbandRight;
390 : WebMouseEvent(sizeof(WebMouseWheelEvent))
395 , accelerationRatioX(1.0f)
396 , accelerationRatioY(1.0f)
397 , scrollByPage(false)
398 , hasPreciseScrollingDeltas(false)
400 , momentumPhase(PhaseNone)
401 , canRubberbandLeft(true)
402 , canRubberbandRight(true)
407 // WebGestureEvent --------------------------------------------------------------
409 class WebGestureEvent : public WebInputEvent {
420 SourceDevice sourceDevice;
423 // Tap information must be set for GestureTap, GestureTapUnconfirmed,
424 // and GestureDoubleTap events.
447 float firstFingerWidth;
448 float firstFingerHeight;
452 // Initial motion that triggered the scroll.
453 // May be redundant with deltaX/deltaY in the first scrollUpdate.
476 : WebInputEvent(sizeof(WebGestureEvent))
482 memset(&data, 0, sizeof(data));
486 // WebTouchEvent --------------------------------------------------------------
488 class WebTouchEvent : public WebInputEvent {
490 // Maximum number of simultaneous touches supported on
492 enum { touchesLengthCap = 12 };
494 unsigned touchesLength;
495 // List of all touches which are currently down.
496 WebTouchPoint touches[touchesLengthCap];
498 unsigned changedTouchesLength;
499 // List of all touches whose state has changed since the last WebTouchEvent
500 WebTouchPoint changedTouches[touchesLengthCap];
502 unsigned targetTouchesLength;
503 // List of all touches which are currently down and are targeting the event recipient.
504 WebTouchPoint targetTouches[touchesLengthCap];
506 // Whether the event can be canceled (with preventDefault). If true then the browser
507 // must wait for an ACK for this event. If false then no ACK IPC is expected.
508 // See comment at the top for why an int is used here instead of a bool.
512 : WebInputEvent(sizeof(WebTouchEvent))
514 , changedTouchesLength(0)
515 , targetTouchesLength(0)