WebKit/chromium/public/WebInputEvent.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "WebCommon.h"
       
    35 #include "WebTouchPoint.h"
       
    36 
       
    37 #include <string.h>
       
    38 
       
    39 namespace WebKit {
       
    40 
       
    41 // The classes defined in this file are intended to be used with
       
    42 // WebWidget's handleInputEvent method.  These event types are cross-
       
    43 // platform and correspond closely to WebCore's Platform*Event classes.
       
    44 //
       
    45 // WARNING! These classes must remain PODs (plain old data).  They are
       
    46 // intended to be "serializable" by copying their raw bytes, so they must
       
    47 // not contain any non-bit-copyable member variables!
       
    48 
       
    49 // WebInputEvent --------------------------------------------------------------
       
    50 
       
    51 class WebInputEvent {
       
    52 public:
       
    53     WebInputEvent(unsigned sizeParam = sizeof(WebInputEvent))
       
    54         : size(sizeParam)
       
    55         , type(Undefined)
       
    56         , modifiers(0)
       
    57         , timeStampSeconds(0.0) { }
       
    58 
       
    59     // When we use an input method (or an input method editor), we receive
       
    60     // two events for a keypress. The former event is a keydown, which
       
    61     // provides a keycode, and the latter is a textinput, which provides
       
    62     // a character processed by an input method. (The mapping from a
       
    63     // keycode to a character code is not trivial for non-English
       
    64     // keyboards.)
       
    65     // To support input methods, Safari sends keydown events to WebKit for
       
    66     // filtering. WebKit sends filtered keydown events back to Safari,
       
    67     // which sends them to input methods.
       
    68     // Unfortunately, it is hard to apply this design to Chrome because of
       
    69     // our multiprocess architecture. An input method is running in a
       
    70     // browser process. On the other hand, WebKit is running in a renderer
       
    71     // process. So, this design results in increasing IPC messages.
       
    72     // To support input methods without increasing IPC messages, Chrome
       
    73     // handles keyboard events in a browser process and send asynchronous
       
    74     // input events (to be translated to DOM events) to a renderer
       
    75     // process.
       
    76     // This design is mostly the same as the one of Windows and Mac Carbon.
       
    77     // So, for what it's worth, our Linux and Mac front-ends emulate our
       
    78     // Windows front-end. To emulate our Windows front-end, we can share
       
    79     // our back-end code among Windows, Linux, and Mac.
       
    80     // TODO(hbono): Issue 18064: remove the KeyDown type since it isn't
       
    81     // used in Chrome any longer.
       
    82 
       
    83     enum Type {
       
    84         Undefined = -1,
       
    85 
       
    86         // WebMouseEvent
       
    87         MouseDown,
       
    88         MouseUp,
       
    89         MouseMove,
       
    90         MouseEnter,
       
    91         MouseLeave,
       
    92 
       
    93         // WebMouseWheelEvent
       
    94         MouseWheel,
       
    95 
       
    96         // WebKeyboardEvent
       
    97         RawKeyDown,
       
    98         KeyDown,
       
    99         KeyUp,
       
   100         Char,
       
   101 
       
   102         // WebTouchEvent
       
   103         TouchStart,
       
   104         TouchMove,
       
   105         TouchEnd,
       
   106         TouchCancel,
       
   107     };
       
   108 
       
   109     enum Modifiers {
       
   110         // modifiers for all events:
       
   111         ShiftKey         = 1 << 0,
       
   112         ControlKey       = 1 << 1,
       
   113         AltKey           = 1 << 2,
       
   114         MetaKey          = 1 << 3,
       
   115 
       
   116         // modifiers for keyboard events:
       
   117         IsKeyPad         = 1 << 4,
       
   118         IsAutoRepeat     = 1 << 5,
       
   119 
       
   120         // modifiers for mouse events:
       
   121         LeftButtonDown   = 1 << 6,
       
   122         MiddleButtonDown = 1 << 7,
       
   123         RightButtonDown  = 1 << 8,
       
   124     };
       
   125 
       
   126     unsigned size;   // The size of this structure, for serialization.
       
   127     Type type;
       
   128     int modifiers;
       
   129     double timeStampSeconds;   // Seconds since epoch.
       
   130 
       
   131     // Returns true if the WebInputEvent |type| is a mouse event.
       
   132     static bool isMouseEventType(int type)
       
   133     {
       
   134         return type == MouseDown
       
   135             || type == MouseUp
       
   136             || type == MouseMove
       
   137             || type == MouseEnter
       
   138             || type == MouseLeave;
       
   139     }
       
   140 
       
   141     // Returns true if the WebInputEvent |type| is a keyboard event.
       
   142     static bool isKeyboardEventType(int type)
       
   143     {
       
   144         return type == RawKeyDown
       
   145             || type == KeyDown
       
   146             || type == KeyUp
       
   147             || type == Char;
       
   148     }
       
   149 
       
   150     // Returns true if the WebInputEvent |type| is a touch event.
       
   151     static bool isTouchEventType(int type)
       
   152     {
       
   153         return type == TouchStart
       
   154             || type == TouchMove
       
   155             || type == TouchEnd
       
   156             || type == TouchCancel;
       
   157     }
       
   158 };
       
   159 
       
   160 // WebKeyboardEvent -----------------------------------------------------------
       
   161 
       
   162 class WebKeyboardEvent : public WebInputEvent {
       
   163 public:
       
   164     // Caps on string lengths so we can make them static arrays and keep
       
   165     // them PODs.
       
   166     static const size_t textLengthCap = 4;
       
   167 
       
   168     // http://www.w3.org/TR/DOM-Level-3-Events/keyset.html lists the
       
   169     // identifiers.  The longest is 18 characters, so we round up to the
       
   170     // next multiple of 4.
       
   171     static const size_t keyIdentifierLengthCap = 20;
       
   172 
       
   173     // |windowsKeyCode| is the Windows key code associated with this key
       
   174     // event.  Sometimes it's direct from the event (i.e. on Windows),
       
   175     // sometimes it's via a mapping function.  If you want a list, see
       
   176     // WebCore/platform/chromium/KeyboardCodes* .
       
   177     int windowsKeyCode;
       
   178 
       
   179     // The actual key code genenerated by the platform.  The DOM spec runs
       
   180     // on Windows-equivalent codes (thus |windowsKeyCode| above) but it
       
   181     // doesn't hurt to have this one around.
       
   182     int nativeKeyCode;
       
   183 
       
   184     // |text| is the text generated by this keystroke.  |unmodifiedText| is
       
   185     // |text|, but unmodified by an concurrently-held modifiers (except
       
   186     // shift).  This is useful for working out shortcut keys.  Linux and
       
   187     // Windows guarantee one character per event.  The Mac does not, but in
       
   188     // reality that's all it ever gives.  We're generous, and cap it a bit
       
   189     // longer.
       
   190     WebUChar text[textLengthCap];
       
   191     WebUChar unmodifiedText[textLengthCap];
       
   192 
       
   193     // This is a string identifying the key pressed.
       
   194     char keyIdentifier[keyIdentifierLengthCap];
       
   195 
       
   196     // This identifies whether this event was tagged by the system as being
       
   197     // a "system key" event (see
       
   198     // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for
       
   199     // details).  Other platforms don't have this concept, but it's just
       
   200     // easier to leave it always false than ifdef.
       
   201     // int is used instead of bool to ensure the size of this structure is
       
   202     // strictly aligned to a factor of 4 bytes, otherwise memory check tools
       
   203     // like valgrind may complain about uninitialized memory usage when
       
   204     // transfering it over the wire.
       
   205     int isSystemKey;
       
   206 
       
   207     WebKeyboardEvent(unsigned sizeParam = sizeof(WebKeyboardEvent))
       
   208         : WebInputEvent(sizeParam)
       
   209         , windowsKeyCode(0)
       
   210         , nativeKeyCode(0)
       
   211         , isSystemKey(false)
       
   212     {
       
   213         memset(&text, 0, sizeof(text));
       
   214         memset(&unmodifiedText, 0, sizeof(unmodifiedText));
       
   215         memset(&keyIdentifier, 0, sizeof(keyIdentifier));
       
   216     }
       
   217 
       
   218     // Sets keyIdentifier based on the value of windowsKeyCode.  This is
       
   219     // handy for generating synthetic keyboard events.
       
   220     WEBKIT_API void setKeyIdentifierFromWindowsKeyCode();
       
   221 };
       
   222 
       
   223 // WebMouseEvent --------------------------------------------------------------
       
   224 
       
   225 class WebMouseEvent : public WebInputEvent {
       
   226 public:
       
   227     // These values defined for WebCore::MouseButton
       
   228     enum Button {
       
   229         ButtonNone = -1,
       
   230         ButtonLeft,
       
   231         ButtonMiddle,
       
   232         ButtonRight
       
   233     };
       
   234 
       
   235     Button button;
       
   236     int x;
       
   237     int y;
       
   238     int windowX;
       
   239     int windowY;
       
   240     int globalX;
       
   241     int globalY;
       
   242     int clickCount;
       
   243 
       
   244     WebMouseEvent(unsigned sizeParam = sizeof(WebMouseEvent))
       
   245         : WebInputEvent(sizeParam)
       
   246         , button(ButtonNone)
       
   247         , x(0)
       
   248         , y(0)
       
   249         , windowX(0)
       
   250         , windowY(0)
       
   251         , globalX(0)
       
   252         , globalY(0)
       
   253         , clickCount(0)
       
   254     {
       
   255     }
       
   256 };
       
   257 
       
   258 // WebMouseWheelEvent ---------------------------------------------------------
       
   259 
       
   260 class WebMouseWheelEvent : public WebMouseEvent {
       
   261 public:
       
   262     float deltaX;
       
   263     float deltaY;
       
   264     float wheelTicksX;
       
   265     float wheelTicksY;
       
   266 
       
   267     // int is used instead of bool to ensure the size of this structure is
       
   268     // strictly aligned to a factor of 4 bytes, otherwise memory check tools
       
   269     // like valgrind may complain about uninitialized memory usage when
       
   270     // transfering it over the wire.
       
   271     int scrollByPage;
       
   272 
       
   273     WebMouseWheelEvent(unsigned sizeParam = sizeof(WebMouseWheelEvent))
       
   274         : WebMouseEvent(sizeParam)
       
   275         , deltaX(0.0f)
       
   276         , deltaY(0.0f)
       
   277         , wheelTicksX(0.0f)
       
   278         , wheelTicksY(0.0f)
       
   279         , scrollByPage(false)
       
   280     {
       
   281     }
       
   282 };
       
   283 
       
   284 // WebTouchEvent --------------------------------------------------------------
       
   285 
       
   286 class WebTouchEvent : public WebInputEvent {
       
   287 public:
       
   288     static const int touchPointsLengthCap = 4;
       
   289 
       
   290     int touchPointsLength;
       
   291     WebTouchPoint touchPoints[touchPointsLengthCap];
       
   292 
       
   293     WebTouchEvent(unsigned sizeParam = sizeof(WebTouchEvent))
       
   294         : WebInputEvent(sizeParam)
       
   295         , touchPointsLength(0)
       
   296     {
       
   297     }
       
   298 };
       
   299 
       
   300 } // namespace WebKit
       
   301 
       
   302 #endif