1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/web_input_event_aura.h"
7 #include "content/browser/renderer_host/input/web_input_event_util.h"
8 #include "content/browser/renderer_host/ui_events_helper.h"
9 #include "ui/aura/client/screen_position_client.h"
10 #include "ui/aura/window.h"
11 #include "ui/events/blink/blink_event_util.h"
12 #include "ui/events/event.h"
13 #include "ui/events/event_utils.h"
14 #include "ui/events/keycodes/dom/keycode_converter.h"
20 gfx::Point
GetScreenLocationFromEvent(const ui::LocatedEvent
& event
) {
22 return event
.root_location();
25 static_cast<aura::Window
*>(event
.target())->GetRootWindow();
26 aura::client::ScreenPositionClient
* spc
=
27 aura::client::GetScreenPositionClient(root
);
29 return event
.root_location();
31 gfx::Point
screen_location(event
.root_location());
32 spc
->ConvertPointToScreen(root
, &screen_location
);
33 return screen_location
;
39 blink::WebMouseEvent
MakeUntranslatedWebMouseEventFromNativeEvent(
40 const base::NativeEvent
& native_event
);
41 blink::WebMouseWheelEvent
MakeUntranslatedWebMouseWheelEventFromNativeEvent(
42 const base::NativeEvent
& native_event
);
43 blink::WebKeyboardEvent
MakeWebKeyboardEventFromNativeEvent(
44 const base::NativeEvent
& native_event
);
45 blink::WebGestureEvent
MakeWebGestureEventFromNativeEvent(
46 const base::NativeEvent
& native_event
);
49 blink::WebKeyboardEvent
MakeWebKeyboardEventFromAuraEvent(
50 const ui::KeyEvent
& event
) {
51 blink::WebKeyboardEvent webkit_event
;
53 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
54 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
55 switch (ui::KeycodeConverter::DomCodeToLocation(event
.code())) {
56 case ui::DomKeyLocation::LEFT
:
57 webkit_event
.modifiers
|= blink::WebInputEvent::IsLeft
;
59 case ui::DomKeyLocation::RIGHT
:
60 webkit_event
.modifiers
|= blink::WebInputEvent::IsRight
;
62 case ui::DomKeyLocation::NUMPAD
:
63 webkit_event
.modifiers
|= blink::WebInputEvent::IsKeyPad
;
65 case ui::DomKeyLocation::STANDARD
:
69 switch (event
.type()) {
70 case ui::ET_KEY_PRESSED
:
71 webkit_event
.type
= event
.is_char() ? blink::WebInputEvent::Char
:
72 blink::WebInputEvent::RawKeyDown
;
74 case ui::ET_KEY_RELEASED
:
75 webkit_event
.type
= blink::WebInputEvent::KeyUp
;
81 if (webkit_event
.modifiers
& blink::WebInputEvent::AltKey
)
82 webkit_event
.isSystemKey
= true;
84 webkit_event
.windowsKeyCode
= event
.GetLocatedWindowsKeyboardCode();
85 webkit_event
.nativeKeyCode
=
86 ui::KeycodeConverter::DomCodeToNativeKeycode(event
.code());
87 webkit_event
.domCode
= static_cast<int>(event
.code());
88 webkit_event
.unmodifiedText
[0] = event
.GetUnmodifiedText();
89 webkit_event
.text
[0] = event
.GetText();
91 webkit_event
.setKeyIdentifierFromWindowsKeyCode();
96 blink::WebMouseWheelEvent
MakeWebMouseWheelEventFromAuraEvent(
97 const ui::ScrollEvent
& event
) {
98 blink::WebMouseWheelEvent webkit_event
;
100 webkit_event
.type
= blink::WebInputEvent::MouseWheel
;
101 webkit_event
.button
= blink::WebMouseEvent::ButtonNone
;
102 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
103 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
104 webkit_event
.hasPreciseScrollingDeltas
= true;
106 float offset_ordinal_x
= 0.f
;
107 float offset_ordinal_y
= 0.f
;
108 if ((event
.flags() & ui::EF_SHIFT_DOWN
) != 0 && event
.x_offset() == 0) {
109 webkit_event
.deltaX
= event
.y_offset();
110 webkit_event
.deltaY
= 0;
111 offset_ordinal_x
= event
.y_offset_ordinal();
112 offset_ordinal_y
= event
.x_offset_ordinal();
114 webkit_event
.deltaX
= event
.x_offset();
115 webkit_event
.deltaY
= event
.y_offset();
116 offset_ordinal_x
= event
.x_offset_ordinal();
117 offset_ordinal_y
= event
.y_offset_ordinal();
120 if (offset_ordinal_x
!= 0.f
&& webkit_event
.deltaX
!= 0.f
)
121 webkit_event
.accelerationRatioX
= offset_ordinal_x
/ webkit_event
.deltaX
;
122 webkit_event
.wheelTicksX
= webkit_event
.deltaX
/ kPixelsPerTick
;
123 webkit_event
.wheelTicksY
= webkit_event
.deltaY
/ kPixelsPerTick
;
124 if (offset_ordinal_y
!= 0.f
&& webkit_event
.deltaY
!= 0.f
)
125 webkit_event
.accelerationRatioY
= offset_ordinal_y
/ webkit_event
.deltaY
;
129 blink::WebGestureEvent
MakeWebGestureEventFromAuraEvent(
130 const ui::ScrollEvent
& event
) {
131 blink::WebGestureEvent webkit_event
;
133 switch (event
.type()) {
134 case ui::ET_SCROLL_FLING_START
:
135 webkit_event
.type
= blink::WebInputEvent::GestureFlingStart
;
136 webkit_event
.data
.flingStart
.velocityX
= event
.x_offset();
137 webkit_event
.data
.flingStart
.velocityY
= event
.y_offset();
139 case ui::ET_SCROLL_FLING_CANCEL
:
140 webkit_event
.type
= blink::WebInputEvent::GestureFlingCancel
;
143 NOTREACHED() << "Invalid gesture type: " << event
.type();
146 NOTREACHED() << "Unknown gesture type: " << event
.type();
149 webkit_event
.sourceDevice
= blink::WebGestureDeviceTouchpad
;
150 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
151 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
155 blink::WebMouseEvent
MakeWebMouseEventFromAuraEvent(
156 const ui::MouseEvent
& event
);
157 blink::WebMouseWheelEvent
MakeWebMouseWheelEventFromAuraEvent(
158 const ui::MouseWheelEvent
& event
);
162 // ui::Event only carries a subset of possible event data provided to Aura by
163 // the host platform. WebKit utilizes a larger subset of that information than
164 // Aura itself. WebKit includes some built in cracking functionality that we
165 // rely on to obtain this information cleanly and consistently.
167 // The only place where an ui::Event's data differs from what the underlying
168 // base::NativeEvent would provide is position data, since we would like to
169 // provide coordinates relative to the aura::Window that is hosting the
170 // renderer, not the top level platform window.
172 // The approach is to fully construct a blink::WebInputEvent from the
173 // ui::Event's base::NativeEvent, and then replace the coordinate fields with
174 // the translated values from the ui::Event.
176 // The exception is mouse events on linux. The ui::MouseEvent contains enough
177 // necessary information to construct a WebMouseEvent. So instead of extracting
178 // the information from the XEvent, which can be tricky when supporting both
179 // XInput2 and XInput, the WebMouseEvent is constructed from the
180 // ui::MouseEvent. This will not be necessary once only XInput2 is supported.
183 blink::WebMouseEvent
MakeWebMouseEvent(const ui::MouseEvent
& event
) {
184 // Construct an untranslated event from the platform event data.
185 blink::WebMouseEvent webkit_event
=
187 // On Windows we have WM_ events comming from desktop and pure aura
188 // events comming from metro mode.
189 event
.native_event().message
?
190 MakeUntranslatedWebMouseEventFromNativeEvent(event
.native_event()) :
191 MakeWebMouseEventFromAuraEvent(event
);
193 MakeWebMouseEventFromAuraEvent(event
);
195 // Replace the event's coordinate fields with translated position data from
197 webkit_event
.windowX
= webkit_event
.x
= event
.x();
198 webkit_event
.windowY
= webkit_event
.y
= event
.y();
201 if (event
.native_event().message
)
204 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
205 webkit_event
.globalX
= screen_point
.x();
206 webkit_event
.globalY
= screen_point
.y();
211 blink::WebMouseWheelEvent
MakeWebMouseWheelEvent(
212 const ui::MouseWheelEvent
& event
) {
214 // Construct an untranslated event from the platform event data.
215 blink::WebMouseWheelEvent webkit_event
= event
.native_event().message
?
216 MakeUntranslatedWebMouseWheelEventFromNativeEvent(event
.native_event()) :
217 MakeWebMouseWheelEventFromAuraEvent(event
);
219 blink::WebMouseWheelEvent webkit_event
=
220 MakeWebMouseWheelEventFromAuraEvent(event
);
223 // Replace the event's coordinate fields with translated position data from
225 webkit_event
.windowX
= webkit_event
.x
= event
.x();
226 webkit_event
.windowY
= webkit_event
.y
= event
.y();
228 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
229 webkit_event
.globalX
= screen_point
.x();
230 webkit_event
.globalY
= screen_point
.y();
232 // Scroll events generated from the mouse wheel when the control key is held
233 // don't trigger scrolling. Instead, they may cause zooming.
234 bool from_mouse_wheel
= !webkit_event
.hasPreciseScrollingDeltas
;
235 if ((webkit_event
.modifiers
& blink::WebInputEvent::ControlKey
) &&
237 webkit_event
.canScroll
= false;
243 blink::WebMouseWheelEvent
MakeWebMouseWheelEvent(const ui::ScrollEvent
& event
) {
245 // Construct an untranslated event from the platform event data.
246 blink::WebMouseWheelEvent webkit_event
= event
.native_event().message
?
247 MakeUntranslatedWebMouseWheelEventFromNativeEvent(event
.native_event()) :
248 MakeWebMouseWheelEventFromAuraEvent(event
);
250 blink::WebMouseWheelEvent webkit_event
=
251 MakeWebMouseWheelEventFromAuraEvent(event
);
254 // Replace the event's coordinate fields with translated position data from
256 webkit_event
.windowX
= webkit_event
.x
= event
.x();
257 webkit_event
.windowY
= webkit_event
.y
= event
.y();
259 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
260 webkit_event
.globalX
= screen_point
.x();
261 webkit_event
.globalY
= screen_point
.y();
266 blink::WebKeyboardEvent
MakeWebKeyboardEvent(const ui::KeyEvent
& event
) {
267 // Windows can figure out whether or not to construct a RawKeyDown or a Char
268 // WebInputEvent based on the type of message carried in
269 // event.native_event(). X11 is not so fortunate, there is no separate
270 // translated event type, so DesktopHostLinux sends an extra KeyEvent with
271 // is_char() == true. We need to pass the ui::KeyEvent to the X11 function
272 // to detect this case so the right event type can be constructed.
274 if (event
.HasNativeEvent()) {
275 // Key events require no translation by the aura system.
276 blink::WebKeyboardEvent
webkit_event(
277 MakeWebKeyboardEventFromNativeEvent(event
.native_event()));
278 webkit_event
.domCode
= static_cast<int>(event
.code());
282 return MakeWebKeyboardEventFromAuraEvent(event
);
285 blink::WebGestureEvent
MakeWebGestureEvent(const ui::GestureEvent
& event
) {
286 blink::WebGestureEvent gesture_event
;
288 if (event
.HasNativeEvent())
289 gesture_event
= MakeWebGestureEventFromNativeEvent(event
.native_event());
291 gesture_event
= MakeWebGestureEventFromUIEvent(event
);
293 gesture_event
= MakeWebGestureEventFromUIEvent(event
);
296 gesture_event
.x
= event
.x();
297 gesture_event
.y
= event
.y();
299 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
300 gesture_event
.globalX
= screen_point
.x();
301 gesture_event
.globalY
= screen_point
.y();
303 return gesture_event
;
306 blink::WebGestureEvent
MakeWebGestureEvent(const ui::ScrollEvent
& event
) {
307 blink::WebGestureEvent gesture_event
;
310 gesture_event
= MakeWebGestureEventFromNativeEvent(event
.native_event());
312 gesture_event
= MakeWebGestureEventFromAuraEvent(event
);
315 gesture_event
.x
= event
.x();
316 gesture_event
.y
= event
.y();
318 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
319 gesture_event
.globalX
= screen_point
.x();
320 gesture_event
.globalY
= screen_point
.y();
322 return gesture_event
;
325 blink::WebGestureEvent
MakeWebGestureEventFlingCancel() {
326 blink::WebGestureEvent gesture_event
;
328 // All other fields are ignored on a GestureFlingCancel event.
329 gesture_event
.type
= blink::WebInputEvent::GestureFlingCancel
;
330 gesture_event
.sourceDevice
= blink::WebGestureDeviceTouchpad
;
331 return gesture_event
;
334 blink::WebMouseEvent
MakeWebMouseEventFromAuraEvent(
335 const ui::MouseEvent
& event
) {
336 blink::WebMouseEvent webkit_event
;
338 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
339 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
341 webkit_event
.button
= blink::WebMouseEvent::ButtonNone
;
342 int button_flags
= event
.flags();
343 if (event
.type() == ui::ET_MOUSE_PRESSED
||
344 event
.type() == ui::ET_MOUSE_RELEASED
) {
345 // We want to use changed_button_flags() for mouse pressed & released.
346 // These flags can be used only if they are set which is not always the case
347 // (see e.g. GetChangedMouseButtonFlagsFromNative() in events_win.cc).
348 if (event
.changed_button_flags())
349 button_flags
= event
.changed_button_flags();
351 if (button_flags
& ui::EF_LEFT_MOUSE_BUTTON
)
352 webkit_event
.button
= blink::WebMouseEvent::ButtonLeft
;
353 if (button_flags
& ui::EF_MIDDLE_MOUSE_BUTTON
)
354 webkit_event
.button
= blink::WebMouseEvent::ButtonMiddle
;
355 if (button_flags
& ui::EF_RIGHT_MOUSE_BUTTON
)
356 webkit_event
.button
= blink::WebMouseEvent::ButtonRight
;
358 switch (event
.type()) {
359 case ui::ET_MOUSE_PRESSED
:
360 webkit_event
.type
= blink::WebInputEvent::MouseDown
;
361 webkit_event
.clickCount
= event
.GetClickCount();
363 case ui::ET_MOUSE_RELEASED
:
364 webkit_event
.type
= blink::WebInputEvent::MouseUp
;
365 webkit_event
.clickCount
= event
.GetClickCount();
367 case ui::ET_MOUSE_ENTERED
:
368 case ui::ET_MOUSE_EXITED
:
369 case ui::ET_MOUSE_MOVED
:
370 case ui::ET_MOUSE_DRAGGED
:
371 webkit_event
.type
= blink::WebInputEvent::MouseMove
;
374 NOTIMPLEMENTED() << "Received unexpected event: " << event
.type();
381 blink::WebMouseWheelEvent
MakeWebMouseWheelEventFromAuraEvent(
382 const ui::MouseWheelEvent
& event
) {
383 blink::WebMouseWheelEvent webkit_event
;
385 webkit_event
.type
= blink::WebInputEvent::MouseWheel
;
386 webkit_event
.button
= blink::WebMouseEvent::ButtonNone
;
387 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
388 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
390 if ((event
.flags() & ui::EF_SHIFT_DOWN
) != 0 && event
.x_offset() == 0) {
391 webkit_event
.deltaX
= event
.y_offset();
392 webkit_event
.deltaY
= 0;
394 webkit_event
.deltaX
= event
.x_offset();
395 webkit_event
.deltaY
= event
.y_offset();
398 webkit_event
.wheelTicksX
= webkit_event
.deltaX
/ kPixelsPerTick
;
399 webkit_event
.wheelTicksY
= webkit_event
.deltaY
/ kPixelsPerTick
;
404 } // namespace content