1/*
2 * Copyright (C) 2012 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#include "config.h"
32
33#if ENABLE(INSPECTOR)
34
35#include "InspectorInputAgent.h"
36
37#include "Chrome.h"
38#include "EventHandler.h"
39#include "Frame.h"
40#include "FrameView.h"
41#include "IntPoint.h"
42#include "IntRect.h"
43#include "IntSize.h"
44#include "Page.h"
45#include "PlatformEvent.h"
46#include "PlatformKeyboardEvent.h"
47#include "PlatformMouseEvent.h"
48
49#include <wtf/CurrentTime.h>
50#include <wtf/text/WTFString.h>
51
52namespace WebCore {
53
54InspectorInputAgent::InspectorInputAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, Page* page)
55    : InspectorBaseAgent<InspectorInputAgent>("Input", instrumentingAgents, inspectorState)
56    , m_page(page)
57{
58}
59
60InspectorInputAgent::~InspectorInputAgent()
61{
62}
63
64void InspectorInputAgent::dispatchKeyEvent(ErrorString* error, const String& type, const int* modifiers, const double* timestamp, const String* text, const String* unmodifiedText, const String* keyIdentifier, const int* windowsVirtualKeyCode, const int* nativeVirtualKeyCode, const int* macCharCode, const bool* autoRepeat, const bool* isKeypad, const bool* isSystemKey)
65{
66    PlatformEvent::Type convertedType;
67    if (type == "keyDown")
68        convertedType = PlatformEvent::KeyDown;
69    else if (type == "keyUp")
70        convertedType = PlatformEvent::KeyUp;
71    else if (type == "char")
72        convertedType = PlatformEvent::Char;
73    else if (type == "rawKeyDown")
74        convertedType = PlatformEvent::RawKeyDown;
75    else {
76        *error = "Unrecognized type: " + type;
77        return;
78    }
79
80    PlatformKeyboardEvent event(
81        convertedType,
82        text ? *text : "",
83        unmodifiedText ? *unmodifiedText : "",
84        keyIdentifier ? *keyIdentifier : "",
85        windowsVirtualKeyCode ? *windowsVirtualKeyCode : 0,
86        nativeVirtualKeyCode ? *nativeVirtualKeyCode : 0,
87        macCharCode ? *macCharCode : 0,
88        autoRepeat ? *autoRepeat : false,
89        isKeypad ? *isKeypad : false,
90        isSystemKey ? *isSystemKey : false,
91        static_cast<PlatformEvent::Modifiers>(modifiers ? *modifiers : 0),
92        timestamp ? *timestamp : currentTime());
93    m_page->mainFrame()->eventHandler()->keyEvent(event);
94}
95
96void InspectorInputAgent::dispatchMouseEvent(ErrorString* error, const String& type, int x, int y, const int* modifiers, const double* timestamp, const String* button, const int* clickCount)
97{
98    PlatformEvent::Type convertedType;
99    if (type == "mousePressed")
100        convertedType = PlatformEvent::MousePressed;
101    else if (type == "mouseReleased")
102        convertedType = PlatformEvent::MouseReleased;
103    else if (type == "mouseMoved")
104        convertedType = PlatformEvent::MouseMoved;
105    else {
106        *error = "Unrecognized type: " + type;
107        return;
108    }
109
110    int convertedModifiers = modifiers ? *modifiers : 0;
111
112    MouseButton convertedButton = NoButton;
113    if (button) {
114        if (*button == "left")
115            convertedButton = LeftButton;
116        else if (*button == "middle")
117            convertedButton = MiddleButton;
118        else if (*button == "right")
119            convertedButton = RightButton;
120        else if (*button != "none") {
121            *error = "Unrecognized button: " + *button;
122            return;
123        }
124    }
125
126    // Some platforms may have flipped coordinate systems, but the given coordinates
127    // assume the origin is in the top-left of the window. Convert.
128    IntPoint convertedPoint = m_page->mainFrame()->view()->convertToContainingWindow(IntPoint(x, y));
129    IntPoint globalPoint = m_page->chrome().rootViewToScreen(IntRect(IntPoint(x, y), IntSize(0, 0))).location();
130
131    PlatformMouseEvent event(
132        convertedPoint,
133        globalPoint,
134        convertedButton,
135        convertedType,
136        clickCount ? *clickCount : 0,
137        convertedModifiers & PlatformEvent::ShiftKey,
138        convertedModifiers & PlatformEvent::CtrlKey,
139        convertedModifiers & PlatformEvent::AltKey,
140        convertedModifiers & PlatformEvent::MetaKey,
141        timestamp ? *timestamp : currentTime());
142
143    EventHandler* handler = m_page->mainFrame()->eventHandler();
144    switch (convertedType) {
145    case PlatformEvent::MousePressed:
146        handler->handleMousePressEvent(event);
147        break;
148    case PlatformEvent::MouseReleased:
149        handler->handleMouseReleaseEvent(event);
150        break;
151    case PlatformEvent::MouseMoved:
152        handler->handleMouseMoveEvent(event);
153        break;
154    default:
155        *error = "Unhandled type: " + type;
156    }
157}
158
159} // namespace WebCore
160
161#endif // ENABLE(INSPECTOR)
162