Upstream version 5.34.104.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     WebInputEvent(unsigned sizeParam = sizeof(WebInputEvent))
62     {
63         memset(this, 0, sizeParam);
64         timeStampSeconds = 0.0;
65         size = sizeParam;
66         type = Undefined;
67         modifiers = 0;
68     }
69
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
75     // keyboards.)
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
86     // process.
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.
93
94     enum Type {
95         Undefined = -1,
96         TypeFirst = Undefined,
97
98         // WebMouseEvent
99         MouseDown,
100         MouseUp,
101         MouseMove,
102         MouseEnter,
103         MouseLeave,
104         ContextMenu,
105
106         // WebMouseWheelEvent
107         MouseWheel,
108
109         // WebKeyboardEvent
110         RawKeyDown,
111         KeyDown,
112         KeyUp,
113         Char,
114
115         // WebGestureEvent
116         GestureScrollBegin,
117         GestureScrollEnd,
118         GestureScrollUpdate,
119         GestureScrollUpdateWithoutPropagation,
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
135         // WebTouchEvent
136         TouchStart,
137         TouchMove,
138         TouchEnd,
139         TouchCancel,
140         TypeLast = TouchCancel
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 type == MouseDown
189             || type == MouseUp
190             || type == MouseMove
191             || type == MouseEnter
192             || type == MouseLeave
193             || type == ContextMenu;
194     }
195
196     // Returns true if the WebInputEvent |type| is a keyboard event.
197     static bool isKeyboardEventType(int type)
198     {
199         return type == RawKeyDown
200             || type == KeyDown
201             || type == KeyUp
202             || type == Char;
203     }
204
205     // Returns true if the WebInputEvent |type| is a touch event.
206     static bool isTouchEventType(int type)
207     {
208         return type == TouchStart
209             || type == TouchMove
210             || type == TouchEnd
211             || type == TouchCancel;
212     }
213
214     // Returns true if the WebInputEvent |type| should be handled as user gesture.
215     static bool isUserGestureEventType(int type)
216     {
217         return isKeyboardEventType(type)
218             || type == MouseDown
219             || type == MouseUp
220             || type == TouchStart
221             || type == TouchEnd;
222     }
223
224     // Returns true if the WebInputEvent is a gesture event.
225     static bool isGestureEventType(int type)
226     {
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;
245     }
246 };
247
248 // WebKeyboardEvent -----------------------------------------------------------
249
250 class WebKeyboardEvent : public WebInputEvent {
251 public:
252     // Caps on string lengths so we can make them static arrays and keep
253     // them PODs.
254     static const size_t textLengthCap = 4;
255
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;
260
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|.
269     int windowsKeyCode;
270
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.
274     int nativeKeyCode;
275
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.
282     bool isSystemKey;
283
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
289     // longer.
290     WebUChar text[textLengthCap];
291     WebUChar unmodifiedText[textLengthCap];
292
293     // This is a string identifying the key pressed.
294     char keyIdentifier[keyIdentifierLengthCap];
295
296     WebKeyboardEvent(unsigned sizeParam = sizeof(WebKeyboardEvent))
297         : WebInputEvent(sizeParam)
298         , windowsKeyCode(0)
299         , nativeKeyCode(0)
300         , isSystemKey(false)
301     {
302         memset(&text, 0, sizeof(text));
303         memset(&unmodifiedText, 0, sizeof(unmodifiedText));
304         memset(&keyIdentifier, 0, sizeof(keyIdentifier));
305     }
306
307     // Sets keyIdentifier based on the value of windowsKeyCode.  This is
308     // handy for generating synthetic keyboard events.
309     BLINK_EXPORT void setKeyIdentifierFromWindowsKeyCode();
310
311     static int windowsKeyCodeWithoutLocation(int keycode);
312     static int locationModifiersFromWindowsKeyCode(int keycode);
313 };
314
315 // WebMouseEvent --------------------------------------------------------------
316
317 class WebMouseEvent : public WebInputEvent {
318 public:
319     // These values defined for WebCore::MouseButton
320     enum Button {
321         ButtonNone = -1,
322         ButtonLeft,
323         ButtonMiddle,
324         ButtonRight
325     };
326
327     Button button;
328     int x;
329     int y;
330     int windowX;
331     int windowY;
332     int globalX;
333     int globalY;
334     int movementX;
335     int movementY;
336     int clickCount;
337
338     WebMouseEvent(unsigned sizeParam = sizeof(WebMouseEvent))
339         : WebInputEvent(sizeParam)
340         , button(ButtonNone)
341         , x(0)
342         , y(0)
343         , windowX(0)
344         , windowY(0)
345         , globalX(0)
346         , globalY(0)
347         , movementX(0)
348         , movementY(0)
349         , clickCount(0)
350     {
351     }
352 };
353
354 // WebMouseWheelEvent ---------------------------------------------------------
355
356 class WebMouseWheelEvent : public WebMouseEvent {
357 public:
358     enum Phase {
359         PhaseNone        = 0,
360         PhaseBegan       = 1 << 0,
361         PhaseStationary  = 1 << 1,
362         PhaseChanged     = 1 << 2,
363         PhaseEnded       = 1 << 3,
364         PhaseCancelled   = 1 << 4,
365         PhaseMayBegin    = 1 << 5,
366     };
367
368     float deltaX;
369     float deltaY;
370     float wheelTicksX;
371     float wheelTicksY;
372
373     float accelerationRatioX;
374     float accelerationRatioY;
375
376     // See comment at the top of the file for why an int is used here.
377     int scrollByPage;
378
379     // See comment at the top of the file for why an int is used here.
380     int hasPreciseScrollingDeltas;
381     Phase phase;
382     Phase momentumPhase;
383
384     WebMouseWheelEvent(unsigned sizeParam = sizeof(WebMouseWheelEvent))
385         : WebMouseEvent(sizeParam)
386         , deltaX(0.0f)
387         , deltaY(0.0f)
388         , wheelTicksX(0.0f)
389         , wheelTicksY(0.0f)
390         , accelerationRatioX(1.0f)
391         , accelerationRatioY(1.0f)
392         , scrollByPage(false)
393         , hasPreciseScrollingDeltas(false)
394         , phase(PhaseNone)
395         , momentumPhase(PhaseNone)
396     {
397     }
398 };
399
400 // WebGestureEvent --------------------------------------------------------------
401
402 class WebGestureEvent : public WebInputEvent {
403 public:
404     enum SourceDevice {
405         Touchpad,
406         Touchscreen,
407     };
408
409     int x;
410     int y;
411     int globalX;
412     int globalY;
413     SourceDevice sourceDevice;
414
415     union {
416         struct {
417             int tapCount;
418             float width;
419             float height;
420         } tap;
421
422         struct {
423             float width;
424             float height;
425         } tapDown;
426
427         struct {
428             float width;
429             float height;
430         } showPress;
431
432         struct {
433             float width;
434             float height;
435         } longPress;
436
437         struct {
438             float firstFingerWidth;
439             float firstFingerHeight;
440         } twoFingerTap;
441
442         struct {
443             // Initial motion that triggered the scroll.
444             // May be redundant with deltaX/deltaY in the first scrollUpdate.
445             float deltaXHint;
446             float deltaYHint;
447         } scrollBegin;
448
449         struct {
450             float deltaX;
451             float deltaY;
452             float velocityX;
453             float velocityY;
454         } scrollUpdate;
455
456         struct {
457             float velocityX;
458             float velocityY;
459         } flingStart;
460
461         struct {
462             float scale;
463         } pinchUpdate;
464     } data;
465
466     WebGestureEvent(unsigned sizeParam = sizeof(WebGestureEvent))
467         : WebInputEvent(sizeParam)
468         , x(0)
469         , y(0)
470         , globalX(0)
471         , globalY(0)
472     {
473         memset(&data, 0, sizeof(data));
474     }
475 };
476
477 // WebTouchEvent --------------------------------------------------------------
478
479 class WebTouchEvent : public WebInputEvent {
480 public:
481     // Maximum number of simultaneous touches supported on
482     // Ash/Aura.
483     enum { touchesLengthCap = 12 };
484
485     unsigned touchesLength;
486     // List of all touches which are currently down.
487     WebTouchPoint touches[touchesLengthCap];
488
489     unsigned changedTouchesLength;
490     // List of all touches whose state has changed since the last WebTouchEvent
491     WebTouchPoint changedTouches[touchesLengthCap];
492
493     unsigned targetTouchesLength;
494     // List of all touches which are currently down and are targeting the event recipient.
495     WebTouchPoint targetTouches[touchesLengthCap];
496
497     WebTouchEvent(unsigned sizeParam = sizeof(WebTouchEvent))
498         : WebInputEvent(sizeParam)
499         , touchesLength(0)
500         , changedTouchesLength(0)
501         , targetTouchesLength(0)
502     {
503     }
504 };
505
506 #pragma pack(pop)
507
508 } // namespace blink
509
510 #endif