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