Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / public / web / WebInputEvent.h
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
17  *
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.
29  */
30
31 #ifndef WebInputEvent_h
32 #define WebInputEvent_h
33
34 #include "../platform/WebCommon.h"
35 #include "../platform/WebRect.h"
36 #include "WebTouchPoint.h"
37
38 #include <string.h>
39
40 namespace blink {
41
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.
45 //
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!
49 //
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.
54
55 #pragma pack(push, 4)
56
57 // WebInputEvent --------------------------------------------------------------
58
59 class WebInputEvent {
60 public:
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
66     // keyboards.)
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
77     // process.
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.
84
85     enum Type {
86         Undefined = -1,
87         TypeFirst = Undefined,
88
89         // WebMouseEvent
90         MouseDown,
91         MouseTypeFirst = MouseDown,
92         MouseUp,
93         MouseMove,
94         MouseEnter,
95         MouseLeave,
96         ContextMenu,
97         MouseTypeLast = ContextMenu,
98
99         // WebMouseWheelEvent
100         MouseWheel,
101
102         // WebKeyboardEvent
103         RawKeyDown,
104         KeyboardTypeFirst = RawKeyDown,
105         KeyDown,
106         KeyUp,
107         Char,
108         KeyboardTypeLast = Char,
109
110         // WebGestureEvent
111         GestureScrollBegin,
112         GestureTypeFirst = GestureScrollBegin,
113         GestureScrollEnd,
114         GestureScrollUpdate,
115         GestureScrollUpdateWithoutPropagation,
116         GestureFlingStart,
117         GestureFlingCancel,
118         GestureShowPress,
119         GestureTap,
120         GestureTapUnconfirmed,
121         GestureTapDown,
122         GestureTapCancel,
123         GestureDoubleTap,
124         GestureTwoFingerTap,
125         GestureLongPress,
126         GestureLongTap,
127         GesturePinchBegin,
128         GesturePinchEnd,
129         GesturePinchUpdate,
130         GestureTypeLast = GesturePinchUpdate,
131
132         // WebTouchEvent
133         TouchStart,
134         TouchTypeFirst = TouchStart,
135         TouchMove,
136         TouchEnd,
137         TouchCancel,
138         TouchTypeLast = TouchCancel,
139
140         TypeLast = TouchTypeLast
141     };
142
143     enum Modifiers {
144         // modifiers for all events:
145         ShiftKey         = 1 << 0,
146         ControlKey       = 1 << 1,
147         AltKey           = 1 << 2,
148         MetaKey          = 1 << 3,
149
150         // modifiers for keyboard events:
151         IsKeyPad         = 1 << 4,
152         IsAutoRepeat     = 1 << 5,
153
154         // modifiers for mouse events:
155         LeftButtonDown   = 1 << 6,
156         MiddleButtonDown = 1 << 7,
157         RightButtonDown  = 1 << 8,
158
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.
162         CapsLockOn       = 1 << 9,
163         NumLockOn        = 1 << 10,
164
165         // Left/right modifiers for keyboard events.
166         IsLeft           = 1 << 11,
167         IsRight          = 1 << 12,
168
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
174         // w.r.t. vsync.
175         IsLastInputEventForCurrentVSync = 1 << 13,
176     };
177
178     static const int InputModifiers = ShiftKey | ControlKey | AltKey | MetaKey;
179
180     double timeStampSeconds; // Seconds since epoch.
181     unsigned size; // The size of this structure, for serialization.
182     Type type;
183     int modifiers;
184
185     // Returns true if the WebInputEvent |type| is a mouse event.
186     static bool isMouseEventType(int type)
187     {
188         return MouseTypeFirst <= type && type <= MouseTypeLast;
189     }
190
191     // Returns true if the WebInputEvent |type| is a keyboard event.
192     static bool isKeyboardEventType(int type)
193     {
194         return KeyboardTypeFirst <= type && type <= KeyboardTypeLast;
195     }
196
197     // Returns true if the WebInputEvent |type| is a touch event.
198     static bool isTouchEventType(int type)
199     {
200         return TouchTypeFirst <= type && type <= TouchTypeLast;
201     }
202
203     // Returns true if the WebInputEvent is a gesture event.
204     static bool isGestureEventType(int type)
205     {
206         return GestureTypeFirst <= type && type <= GestureTypeLast;
207     }
208
209 protected:
210     explicit WebInputEvent(unsigned sizeParam)
211     {
212         memset(this, 0, sizeParam);
213         timeStampSeconds = 0.0;
214         size = sizeParam;
215         type = Undefined;
216         modifiers = 0;
217     }
218 };
219
220 // WebKeyboardEvent -----------------------------------------------------------
221
222 class WebKeyboardEvent : public WebInputEvent {
223 public:
224     // Caps on string lengths so we can make them static arrays and keep
225     // them PODs.
226     static const size_t textLengthCap = 4;
227
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;
232
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|.
241     int windowsKeyCode;
242
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.
246     int nativeKeyCode;
247
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.
254     bool isSystemKey;
255
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
261     // longer.
262     WebUChar text[textLengthCap];
263     WebUChar unmodifiedText[textLengthCap];
264
265     // This is a string identifying the key pressed.
266     char keyIdentifier[keyIdentifierLengthCap];
267
268     WebKeyboardEvent()
269         : WebInputEvent(sizeof(WebKeyboardEvent))
270         , windowsKeyCode(0)
271         , nativeKeyCode(0)
272         , isSystemKey(false)
273     {
274         memset(&text, 0, sizeof(text));
275         memset(&unmodifiedText, 0, sizeof(unmodifiedText));
276         memset(&keyIdentifier, 0, sizeof(keyIdentifier));
277     }
278
279     // Sets keyIdentifier based on the value of windowsKeyCode.  This is
280     // handy for generating synthetic keyboard events.
281     BLINK_EXPORT void setKeyIdentifierFromWindowsKeyCode();
282
283     static int windowsKeyCodeWithoutLocation(int keycode);
284     static int locationModifiersFromWindowsKeyCode(int keycode);
285 };
286
287 // WebMouseEvent --------------------------------------------------------------
288
289 class WebMouseEvent : public WebInputEvent {
290 public:
291     enum Button {
292         ButtonNone = -1,
293         ButtonLeft,
294         ButtonMiddle,
295         ButtonRight
296     };
297
298     Button button;
299     int x;
300     int y;
301     int windowX;
302     int windowY;
303     int globalX;
304     int globalY;
305     int movementX;
306     int movementY;
307     int clickCount;
308
309     WebMouseEvent()
310         : WebInputEvent(sizeof(WebMouseEvent))
311         , button(ButtonNone)
312         , x(0)
313         , y(0)
314         , windowX(0)
315         , windowY(0)
316         , globalX(0)
317         , globalY(0)
318         , movementX(0)
319         , movementY(0)
320         , clickCount(0)
321     {
322     }
323
324 protected:
325     explicit WebMouseEvent(unsigned sizeParam)
326         : WebInputEvent(sizeParam)
327         , button(ButtonNone)
328         , x(0)
329         , y(0)
330         , windowX(0)
331         , windowY(0)
332         , globalX(0)
333         , globalY(0)
334         , movementX(0)
335         , movementY(0)
336         , clickCount(0)
337     {
338     }
339 };
340
341 // WebMouseWheelEvent ---------------------------------------------------------
342
343 class WebMouseWheelEvent : public WebMouseEvent {
344 public:
345     enum Phase {
346         PhaseNone        = 0,
347         PhaseBegan       = 1 << 0,
348         PhaseStationary  = 1 << 1,
349         PhaseChanged     = 1 << 2,
350         PhaseEnded       = 1 << 3,
351         PhaseCancelled   = 1 << 4,
352         PhaseMayBegin    = 1 << 5,
353     };
354
355     float deltaX;
356     float deltaY;
357     float wheelTicksX;
358     float wheelTicksY;
359
360     float accelerationRatioX;
361     float accelerationRatioY;
362
363     // See comment at the top of the file for why an int is used here.
364     int scrollByPage;
365
366     // See comment at the top of the file for why an int is used here.
367     int hasPreciseScrollingDeltas;
368     Phase phase;
369     Phase momentumPhase;
370
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.
381     //
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;
388
389     WebMouseWheelEvent()
390         : WebMouseEvent(sizeof(WebMouseWheelEvent))
391         , deltaX(0.0f)
392         , deltaY(0.0f)
393         , wheelTicksX(0.0f)
394         , wheelTicksY(0.0f)
395         , accelerationRatioX(1.0f)
396         , accelerationRatioY(1.0f)
397         , scrollByPage(false)
398         , hasPreciseScrollingDeltas(false)
399         , phase(PhaseNone)
400         , momentumPhase(PhaseNone)
401         , canRubberbandLeft(true)
402         , canRubberbandRight(true)
403     {
404     }
405 };
406
407 // WebGestureEvent --------------------------------------------------------------
408
409 class WebGestureEvent : public WebInputEvent {
410 public:
411     enum SourceDevice {
412         Touchpad,
413         Touchscreen,
414     };
415
416     int x;
417     int y;
418     int globalX;
419     int globalY;
420     SourceDevice sourceDevice;
421
422     union {
423         // Tap information must be set for GestureTap, GestureTapUnconfirmed,
424         // and GestureDoubleTap events.
425         struct {
426             int tapCount;
427             float width;
428             float height;
429         } tap;
430
431         struct {
432             float width;
433             float height;
434         } tapDown;
435
436         struct {
437             float width;
438             float height;
439         } showPress;
440
441         struct {
442             float width;
443             float height;
444         } longPress;
445
446         struct {
447             float firstFingerWidth;
448             float firstFingerHeight;
449         } twoFingerTap;
450
451         struct {
452             // Initial motion that triggered the scroll.
453             // May be redundant with deltaX/deltaY in the first scrollUpdate.
454             float deltaXHint;
455             float deltaYHint;
456         } scrollBegin;
457
458         struct {
459             float deltaX;
460             float deltaY;
461             float velocityX;
462             float velocityY;
463         } scrollUpdate;
464
465         struct {
466             float velocityX;
467             float velocityY;
468         } flingStart;
469
470         struct {
471             float scale;
472         } pinchUpdate;
473     } data;
474
475     WebGestureEvent()
476         : WebInputEvent(sizeof(WebGestureEvent))
477         , x(0)
478         , y(0)
479         , globalX(0)
480         , globalY(0)
481     {
482         memset(&data, 0, sizeof(data));
483     }
484 };
485
486 // WebTouchEvent --------------------------------------------------------------
487
488 class WebTouchEvent : public WebInputEvent {
489 public:
490     // Maximum number of simultaneous touches supported on
491     // Ash/Aura.
492     enum { touchesLengthCap = 12 };
493
494     unsigned touchesLength;
495     // List of all touches which are currently down.
496     WebTouchPoint touches[touchesLengthCap];
497
498     unsigned changedTouchesLength;
499     // List of all touches whose state has changed since the last WebTouchEvent
500     WebTouchPoint changedTouches[touchesLengthCap];
501
502     unsigned targetTouchesLength;
503     // List of all touches which are currently down and are targeting the event recipient.
504     WebTouchPoint targetTouches[touchesLengthCap];
505
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.
509     int cancelable;
510
511     WebTouchEvent()
512         : WebInputEvent(sizeof(WebTouchEvent))
513         , touchesLength(0)
514         , changedTouchesLength(0)
515         , targetTouchesLength(0)
516         , cancelable(true)
517     {
518     }
519 };
520
521 #pragma pack(pop)
522
523 } // namespace blink
524
525 #endif