Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / web / WebInputEvent.h
blob248e3d107b03b7112365499a279e921c8ea20337
1 /*
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
6 * met:
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
13 * distribution.
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/WebGestureDevice.h"
36 #include "../platform/WebPointerProperties.h"
37 #include "../platform/WebRect.h"
38 #include "WebTouchPoint.h"
40 #include <string.h>
42 namespace blink {
44 // The classes defined in this file are intended to be used with
45 // WebWidget's handleInputEvent method. These event types are cross-
46 // platform and correspond closely to WebCore's Platform*Event classes.
48 // WARNING! These classes must remain PODs (plain old data). They are
49 // intended to be "serializable" by copying their raw bytes, so they must
50 // not contain any non-bit-copyable member variables!
52 // Furthermore, the class members need to be packed so they are aligned
53 // properly and don't have paddings/gaps, otherwise memory check tools
54 // like Valgrind will complain about uninitialized memory usage when
55 // transferring these classes over the wire.
57 #pragma pack(push, 4)
59 // WebInputEvent --------------------------------------------------------------
61 class WebInputEvent {
62 public:
63 // When we use an input method (or an input method editor), we receive
64 // two events for a keypress. The former event is a keydown, which
65 // provides a keycode, and the latter is a textinput, which provides
66 // a character processed by an input method. (The mapping from a
67 // keycode to a character code is not trivial for non-English
68 // keyboards.)
69 // To support input methods, Safari sends keydown events to WebKit for
70 // filtering. WebKit sends filtered keydown events back to Safari,
71 // which sends them to input methods.
72 // Unfortunately, it is hard to apply this design to Chrome because of
73 // our multiprocess architecture. An input method is running in a
74 // browser process. On the other hand, WebKit is running in a renderer
75 // process. So, this design results in increasing IPC messages.
76 // To support input methods without increasing IPC messages, Chrome
77 // handles keyboard events in a browser process and send asynchronous
78 // input events (to be translated to DOM events) to a renderer
79 // process.
80 // This design is mostly the same as the one of Windows and Mac Carbon.
81 // So, for what it's worth, our Linux and Mac front-ends emulate our
82 // Windows front-end. To emulate our Windows front-end, we can share
83 // our back-end code among Windows, Linux, and Mac.
84 // TODO(hbono): Issue 18064: remove the KeyDown type since it isn't
85 // used in Chrome any longer.
87 // A Java counterpart will be generated for this enum.
88 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.blink_public.web
89 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: WebInputEventType
90 enum Type {
91 Undefined = -1,
92 TypeFirst = Undefined,
94 // WebMouseEvent
95 MouseDown,
96 MouseTypeFirst = MouseDown,
97 MouseUp,
98 MouseMove,
99 MouseEnter,
100 MouseLeave,
101 ContextMenu,
102 MouseTypeLast = ContextMenu,
104 // WebMouseWheelEvent
105 MouseWheel,
107 // WebKeyboardEvent
108 RawKeyDown,
109 KeyboardTypeFirst = RawKeyDown,
110 KeyDown,
111 KeyUp,
112 Char,
113 KeyboardTypeLast = Char,
115 // WebGestureEvent
116 GestureScrollBegin,
117 GestureTypeFirst = GestureScrollBegin,
118 GestureScrollEnd,
119 GestureScrollUpdate,
120 GestureFlingStart,
121 GestureFlingCancel,
122 GestureShowPress,
123 GestureTap,
124 GestureTapUnconfirmed,
125 GestureTapDown,
126 GestureTapCancel,
127 GestureDoubleTap,
128 GestureTwoFingerTap,
129 GestureLongPress,
130 GestureLongTap,
131 GesturePinchBegin,
132 GesturePinchEnd,
133 GesturePinchUpdate,
134 GestureTypeLast = GesturePinchUpdate,
136 // WebTouchEvent
137 TouchStart,
138 TouchTypeFirst = TouchStart,
139 TouchMove,
140 TouchEnd,
141 TouchCancel,
142 TouchTypeLast = TouchCancel,
144 TypeLast = TouchTypeLast
147 // A Java counterpart will be generated for this enum.
148 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.blink_public.web
149 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: WebInputEventModifier
150 enum Modifiers {
151 // modifiers for all events:
152 ShiftKey = 1 << 0,
153 ControlKey = 1 << 1,
154 AltKey = 1 << 2,
155 MetaKey = 1 << 3,
157 // modifiers for keyboard events:
158 IsKeyPad = 1 << 4,
159 IsAutoRepeat = 1 << 5,
161 // modifiers for mouse events:
162 LeftButtonDown = 1 << 6,
163 MiddleButtonDown = 1 << 7,
164 RightButtonDown = 1 << 8,
166 // Toggle modifiers for all events. Danger: these are not reflected
167 // into WebCore, so round-tripping from WebInputEvent to a WebCore
168 // event and back will not preserve these flags.
169 CapsLockOn = 1 << 9,
170 NumLockOn = 1 << 10,
172 // Left/right modifiers for keyboard events.
173 IsLeft = 1 << 11,
174 IsRight = 1 << 12,
177 // The rail mode for a wheel event specifies the axis on which scrolling is
178 // expected to stick. If this axis is set to Free, then scrolling is not
179 // stuck to any axis.
180 enum RailsMode {
181 RailsModeFree = 0,
182 RailsModeHorizontal = 1,
183 RailsModeVertical = 2,
186 static const int InputModifiers = ShiftKey | ControlKey | AltKey | MetaKey;
188 double timeStampSeconds; // Seconds since platform start with microsecond resolution.
189 unsigned size; // The size of this structure, for serialization.
190 Type type;
191 int modifiers;
193 // Returns true if the WebInputEvent |type| is a mouse event.
194 static bool isMouseEventType(int type)
196 return MouseTypeFirst <= type && type <= MouseTypeLast;
199 // Returns true if the WebInputEvent |type| is a keyboard event.
200 static bool isKeyboardEventType(int type)
202 return KeyboardTypeFirst <= type && type <= KeyboardTypeLast;
205 // Returns true if the WebInputEvent |type| is a touch event.
206 static bool isTouchEventType(int type)
208 return TouchTypeFirst <= type && type <= TouchTypeLast;
211 // Returns true if the WebInputEvent is a gesture event.
212 static bool isGestureEventType(int type)
214 return GestureTypeFirst <= type && type <= GestureTypeLast;
217 protected:
218 explicit WebInputEvent(unsigned sizeParam)
220 memset(this, 0, sizeParam);
221 timeStampSeconds = 0.0;
222 size = sizeParam;
223 type = Undefined;
224 modifiers = 0;
228 // WebKeyboardEvent -----------------------------------------------------------
230 class WebKeyboardEvent : public WebInputEvent {
231 public:
232 // Caps on string lengths so we can make them static arrays and keep
233 // them PODs.
234 static const size_t textLengthCap = 4;
236 // http://www.w3.org/TR/DOM-Level-3-Events/keyset.html lists the
237 // identifiers. The longest is 18 characters, so we round up to the
238 // next multiple of 4.
239 static const size_t keyIdentifierLengthCap = 20;
241 // |windowsKeyCode| is the Windows key code associated with this key
242 // event. Sometimes it's direct from the event (i.e. on Windows),
243 // sometimes it's via a mapping function. If you want a list, see
244 // WebCore/platform/chromium/KeyboardCodes* . Note that this should
245 // ALWAYS store the non-locational version of a keycode as this is
246 // what is returned by the Windows API. For example, it should
247 // store VK_SHIFT instead of VK_RSHIFT. The location information
248 // should be stored in |modifiers|.
249 int windowsKeyCode;
251 // The actual key code genenerated by the platform. The DOM spec runs
252 // on Windows-equivalent codes (thus |windowsKeyCode| above) but it
253 // doesn't hurt to have this one around.
254 int nativeKeyCode;
256 // The DOM code enum of the key pressed as passed by the embedder. DOM
257 // code enum are defined in ui/events/keycodes/dom4/keycode_converter_data.h.
258 int domCode;
260 // The DOM key enum of the key pressed as passed by the embedder. DOM
261 // key enum are defined in ui/events/keycodes/dom3/dom_key_data.h
262 int domKey;
264 // This identifies whether this event was tagged by the system as being
265 // a "system key" event (see
266 // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for
267 // details). Other platforms don't have this concept, but it's just
268 // easier to leave it always false than ifdef.
269 bool isSystemKey;
271 // |text| is the text generated by this keystroke. |unmodifiedText| is
272 // |text|, but unmodified by an concurrently-held modifiers (except
273 // shift). This is useful for working out shortcut keys. Linux and
274 // Windows guarantee one character per event. The Mac does not, but in
275 // reality that's all it ever gives. We're generous, and cap it a bit
276 // longer.
277 WebUChar text[textLengthCap];
278 WebUChar unmodifiedText[textLengthCap];
280 // This is a string identifying the key pressed.
281 char keyIdentifier[keyIdentifierLengthCap];
283 WebKeyboardEvent()
284 : WebInputEvent(sizeof(WebKeyboardEvent))
285 , windowsKeyCode(0)
286 , nativeKeyCode(0)
287 , isSystemKey(false)
289 memset(&text, 0, sizeof(text));
290 memset(&unmodifiedText, 0, sizeof(unmodifiedText));
291 memset(&keyIdentifier, 0, sizeof(keyIdentifier));
294 // Sets keyIdentifier based on the value of windowsKeyCode. This is
295 // handy for generating synthetic keyboard events.
296 BLINK_EXPORT void setKeyIdentifierFromWindowsKeyCode();
299 // WebMouseEvent --------------------------------------------------------------
301 class WebMouseEvent : public WebInputEvent, public WebPointerProperties {
302 public:
303 // Window coordinate
304 int x;
305 int y;
307 // DEPRECATED (crbug.com/507787)
308 int windowX;
309 int windowY;
311 // Screen coordinate
312 int globalX;
313 int globalY;
315 int movementX;
316 int movementY;
317 int clickCount;
319 WebMouseEvent()
320 : WebInputEvent(sizeof(WebMouseEvent))
321 , WebPointerProperties()
322 , x(0)
323 , y(0)
324 , windowX(0)
325 , windowY(0)
326 , globalX(0)
327 , globalY(0)
328 , movementX(0)
329 , movementY(0)
330 , clickCount(0)
334 protected:
335 explicit WebMouseEvent(unsigned sizeParam)
336 : WebInputEvent(sizeParam)
337 , WebPointerProperties()
338 , x(0)
339 , y(0)
340 , windowX(0)
341 , windowY(0)
342 , globalX(0)
343 , globalY(0)
344 , movementX(0)
345 , movementY(0)
346 , clickCount(0)
351 // WebMouseWheelEvent ---------------------------------------------------------
353 class WebMouseWheelEvent : public WebMouseEvent {
354 public:
355 enum Phase {
356 PhaseNone = 0,
357 PhaseBegan = 1 << 0,
358 PhaseStationary = 1 << 1,
359 PhaseChanged = 1 << 2,
360 PhaseEnded = 1 << 3,
361 PhaseCancelled = 1 << 4,
362 PhaseMayBegin = 1 << 5,
365 float deltaX;
366 float deltaY;
367 float wheelTicksX;
368 float wheelTicksY;
370 float accelerationRatioX;
371 float accelerationRatioY;
373 // This field exists to allow BrowserPlugin to mark MouseWheel events as
374 // 'resent' to handle the case where an event is not consumed when first
375 // encountered; it should be handled differently by the plugin when it is
376 // sent for thesecond time. No code within Blink touches this, other than to
377 // plumb it through event conversions.
378 int resendingPluginId;
380 Phase phase;
381 Phase momentumPhase;
383 // Rubberbanding is an OSX visual effect. When a user scrolls the content
384 // area with a track pad, and the content area is already at its limit in
385 // the direction being scrolled, the entire content area is allowed to
386 // scroll slightly off screen, revealing a grey background. When the user
387 // lets go, the content area snaps back into place. Blink is responsible
388 // for this rubberbanding effect, but the embedder may wish to disable
389 // rubber banding in the left or right direction, if the scroll should have
390 // an alternate effect. The common case is that a scroll in the left or
391 // right directions causes a back or forwards navigation, respectively.
393 // These flags prevent rubber banding from starting in a given direction,
394 // but have no effect on an ongoing rubber banding. A rubber banding that
395 // started in the vertical direction is allowed to continue in the right
396 // direction, even if canRubberbandRight is 0.
397 bool canRubberbandLeft;
398 bool canRubberbandRight;
400 bool scrollByPage;
401 bool hasPreciseScrollingDeltas;
403 // When false, this wheel event should not trigger scrolling (or any other default
404 // action) if the event goes unhandled by JavaScript. This is used, for example,
405 // when the browser decides the default behavior for Ctrl+Wheel should be to zoom
406 // instead of scroll.
407 bool canScroll;
409 RailsMode railsMode;
411 WebMouseWheelEvent()
412 : WebMouseEvent(sizeof(WebMouseWheelEvent))
413 , deltaX(0.0f)
414 , deltaY(0.0f)
415 , wheelTicksX(0.0f)
416 , wheelTicksY(0.0f)
417 , accelerationRatioX(1.0f)
418 , accelerationRatioY(1.0f)
419 , resendingPluginId(-1)
420 , phase(PhaseNone)
421 , momentumPhase(PhaseNone)
422 , canRubberbandLeft(true)
423 , canRubberbandRight(true)
424 , scrollByPage(false)
425 , hasPreciseScrollingDeltas(false)
426 , canScroll(true)
427 , railsMode(RailsModeFree)
432 // WebGestureEvent --------------------------------------------------------------
434 class WebGestureEvent : public WebInputEvent {
435 public:
436 int x;
437 int y;
438 int globalX;
439 int globalY;
440 WebGestureDevice sourceDevice;
441 // This field exists to allow BrowserPlugin to mark GestureScroll events as
442 // 'resent' to handle the case where an event is not consumed when first
443 // encountered; it should be handled differently by the plugin when it is
444 // sent for thesecond time. No code within Blink touches this, other than to
445 // plumb it through event conversions.
446 int resendingPluginId;
448 union {
449 // Tap information must be set for GestureTap, GestureTapUnconfirmed,
450 // and GestureDoubleTap events.
451 struct {
452 int tapCount;
453 float width;
454 float height;
455 } tap;
457 struct {
458 float width;
459 float height;
460 } tapDown;
462 struct {
463 float width;
464 float height;
465 } showPress;
467 struct {
468 float width;
469 float height;
470 } longPress;
472 struct {
473 float firstFingerWidth;
474 float firstFingerHeight;
475 } twoFingerTap;
477 struct {
478 // Initial motion that triggered the scroll.
479 // May be redundant with deltaX/deltaY in the first scrollUpdate.
480 float deltaXHint;
481 float deltaYHint;
482 // If true, this event will skip hit testing to find a scroll
483 // target and instead just scroll the viewport.
484 bool targetViewport;
485 } scrollBegin;
487 struct {
488 float deltaX;
489 float deltaY;
490 float velocityX;
491 float velocityY;
492 // Whether any previous GestureScrollUpdate in the current scroll
493 // sequence was suppressed (e.g., the causal touchmove was
494 // preventDefault'ed). This bit is particularly useful for
495 // determining whether the observed scroll update sequence captures
496 // the entirety of the generative motion.
497 bool previousUpdateInSequencePrevented;
498 bool preventPropagation;
499 bool inertial;
500 } scrollUpdate;
502 struct {
503 float velocityX;
504 float velocityY;
505 // If true, this event will skip hit testing to find a scroll
506 // target and instead just scroll the viewport.
507 bool targetViewport;
508 } flingStart;
510 struct {
511 // If set to true, don't treat flingCancel
512 // as a part of fling boost events sequence.
513 bool preventBoosting;
514 } flingCancel;
516 struct {
517 bool zoomDisabled;
518 float scale;
519 } pinchUpdate;
520 } data;
522 WebGestureEvent()
523 : WebInputEvent(sizeof(WebGestureEvent))
524 , x(0)
525 , y(0)
526 , globalX(0)
527 , globalY(0)
528 , resendingPluginId(-1)
530 memset(&data, 0, sizeof(data));
534 // WebTouchEvent --------------------------------------------------------------
536 // TODO(e_hakkinen): Replace with WebPointerEvent. crbug.com/508283
537 class WebTouchEvent : public WebInputEvent {
538 public:
539 // Maximum number of simultaneous touches supported on
540 // Ash/Aura.
541 enum { touchesLengthCap = 16 };
543 unsigned touchesLength;
544 // List of all touches, regardless of state.
545 WebTouchPoint touches[touchesLengthCap];
547 // Whether the event can be canceled (with preventDefault). If true then the browser
548 // must wait for an ACK for this event. If false then no ACK IPC is expected.
549 bool cancelable;
551 // Whether the event will produce scroll-inducing events if uncanceled. This
552 // will be true for touchmove events after the platform slop region has been
553 // exceeded and fling-generating touchend events. Note that this doesn't
554 // necessarily mean content will scroll, only that scroll events will be
555 // generated.
556 bool causesScrollingIfUncanceled;
558 // A unique identifier for the touch event.
559 uint32_t uniqueTouchEventId;
561 WebTouchEvent()
562 : WebInputEvent(sizeof(WebTouchEvent))
563 , touchesLength(0)
564 , cancelable(true)
565 , causesScrollingIfUncanceled(false)
566 , uniqueTouchEventId(0)
571 #pragma pack(pop)
573 } // namespace blink
575 #endif