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
;
36 blink::WebInputEvent::Modifiers
DomCodeToWebInputEventModifiers(
38 switch (ui::KeycodeConverter::DomCodeToLocation(code
)) {
39 case ui::DomKeyLocation::LEFT
:
40 return blink::WebInputEvent::IsLeft
;
41 case ui::DomKeyLocation::RIGHT
:
42 return blink::WebInputEvent::IsRight
;
43 case ui::DomKeyLocation::NUMPAD
:
44 return blink::WebInputEvent::IsKeyPad
;
45 case ui::DomKeyLocation::STANDARD
:
48 return static_cast<blink::WebInputEvent::Modifiers
>(0);
54 blink::WebMouseEvent
MakeUntranslatedWebMouseEventFromNativeEvent(
55 const base::NativeEvent
& native_event
);
56 blink::WebMouseWheelEvent
MakeUntranslatedWebMouseWheelEventFromNativeEvent(
57 const base::NativeEvent
& native_event
);
58 blink::WebKeyboardEvent
MakeWebKeyboardEventFromNativeEvent(
59 const base::NativeEvent
& native_event
);
60 blink::WebGestureEvent
MakeWebGestureEventFromNativeEvent(
61 const base::NativeEvent
& native_event
);
64 blink::WebKeyboardEvent
MakeWebKeyboardEventFromAuraEvent(
65 const ui::KeyEvent
& event
) {
66 blink::WebKeyboardEvent webkit_event
;
68 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
69 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags()) |
70 DomCodeToWebInputEventModifiers(event
.code());
72 switch (event
.type()) {
73 case ui::ET_KEY_PRESSED
:
74 webkit_event
.type
= event
.is_char() ? blink::WebInputEvent::Char
:
75 blink::WebInputEvent::RawKeyDown
;
77 case ui::ET_KEY_RELEASED
:
78 webkit_event
.type
= blink::WebInputEvent::KeyUp
;
84 if (webkit_event
.modifiers
& blink::WebInputEvent::AltKey
)
85 webkit_event
.isSystemKey
= true;
87 webkit_event
.windowsKeyCode
= event
.GetLocatedWindowsKeyboardCode();
88 webkit_event
.nativeKeyCode
=
89 ui::KeycodeConverter::DomCodeToNativeKeycode(event
.code());
90 webkit_event
.domCode
= static_cast<int>(event
.code());
91 webkit_event
.unmodifiedText
[0] = event
.GetUnmodifiedText();
92 webkit_event
.text
[0] = event
.GetText();
94 webkit_event
.setKeyIdentifierFromWindowsKeyCode();
99 blink::WebMouseWheelEvent
MakeWebMouseWheelEventFromAuraEvent(
100 const ui::ScrollEvent
& event
) {
101 blink::WebMouseWheelEvent webkit_event
;
103 webkit_event
.type
= blink::WebInputEvent::MouseWheel
;
104 webkit_event
.button
= blink::WebMouseEvent::ButtonNone
;
105 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
106 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
107 webkit_event
.hasPreciseScrollingDeltas
= true;
109 float offset_ordinal_x
= 0.f
;
110 float offset_ordinal_y
= 0.f
;
111 if ((event
.flags() & ui::EF_SHIFT_DOWN
) != 0 && event
.x_offset() == 0) {
112 webkit_event
.deltaX
= event
.y_offset();
113 webkit_event
.deltaY
= 0;
114 offset_ordinal_x
= event
.y_offset_ordinal();
115 offset_ordinal_y
= event
.x_offset_ordinal();
117 webkit_event
.deltaX
= event
.x_offset();
118 webkit_event
.deltaY
= event
.y_offset();
119 offset_ordinal_x
= event
.x_offset_ordinal();
120 offset_ordinal_y
= event
.y_offset_ordinal();
123 if (offset_ordinal_x
!= 0.f
&& webkit_event
.deltaX
!= 0.f
)
124 webkit_event
.accelerationRatioX
= offset_ordinal_x
/ webkit_event
.deltaX
;
125 webkit_event
.wheelTicksX
= webkit_event
.deltaX
/ kPixelsPerTick
;
126 webkit_event
.wheelTicksY
= webkit_event
.deltaY
/ kPixelsPerTick
;
127 if (offset_ordinal_y
!= 0.f
&& webkit_event
.deltaY
!= 0.f
)
128 webkit_event
.accelerationRatioY
= offset_ordinal_y
/ webkit_event
.deltaY
;
132 blink::WebGestureEvent
MakeWebGestureEventFromAuraEvent(
133 const ui::ScrollEvent
& event
) {
134 blink::WebGestureEvent webkit_event
;
136 switch (event
.type()) {
137 case ui::ET_SCROLL_FLING_START
:
138 webkit_event
.type
= blink::WebInputEvent::GestureFlingStart
;
139 webkit_event
.data
.flingStart
.velocityX
= event
.x_offset();
140 webkit_event
.data
.flingStart
.velocityY
= event
.y_offset();
142 case ui::ET_SCROLL_FLING_CANCEL
:
143 webkit_event
.type
= blink::WebInputEvent::GestureFlingCancel
;
146 NOTREACHED() << "Invalid gesture type: " << event
.type();
149 NOTREACHED() << "Unknown gesture type: " << event
.type();
152 webkit_event
.sourceDevice
= blink::WebGestureDeviceTouchpad
;
153 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
154 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
158 blink::WebMouseEvent
MakeWebMouseEventFromAuraEvent(
159 const ui::MouseEvent
& event
);
160 blink::WebMouseWheelEvent
MakeWebMouseWheelEventFromAuraEvent(
161 const ui::MouseWheelEvent
& event
);
165 // ui::Event only carries a subset of possible event data provided to Aura by
166 // the host platform. WebKit utilizes a larger subset of that information than
167 // Aura itself. WebKit includes some built in cracking functionality that we
168 // rely on to obtain this information cleanly and consistently.
170 // The only place where an ui::Event's data differs from what the underlying
171 // base::NativeEvent would provide is position data, since we would like to
172 // provide coordinates relative to the aura::Window that is hosting the
173 // renderer, not the top level platform window.
175 // The approach is to fully construct a blink::WebInputEvent from the
176 // ui::Event's base::NativeEvent, and then replace the coordinate fields with
177 // the translated values from the ui::Event.
179 // The exception is mouse events on linux. The ui::MouseEvent contains enough
180 // necessary information to construct a WebMouseEvent. So instead of extracting
181 // the information from the XEvent, which can be tricky when supporting both
182 // XInput2 and XInput, the WebMouseEvent is constructed from the
183 // ui::MouseEvent. This will not be necessary once only XInput2 is supported.
186 blink::WebMouseEvent
MakeWebMouseEvent(const ui::MouseEvent
& event
) {
187 // Construct an untranslated event from the platform event data.
188 blink::WebMouseEvent webkit_event
=
190 // On Windows we have WM_ events comming from desktop and pure aura
191 // events comming from metro mode.
192 event
.native_event().message
?
193 MakeUntranslatedWebMouseEventFromNativeEvent(event
.native_event()) :
194 MakeWebMouseEventFromAuraEvent(event
);
196 MakeWebMouseEventFromAuraEvent(event
);
198 // Replace the event's coordinate fields with translated position data from
200 webkit_event
.windowX
= webkit_event
.x
= event
.x();
201 webkit_event
.windowY
= webkit_event
.y
= event
.y();
204 if (event
.native_event().message
)
207 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
208 webkit_event
.globalX
= screen_point
.x();
209 webkit_event
.globalY
= screen_point
.y();
214 blink::WebMouseWheelEvent
MakeWebMouseWheelEvent(
215 const ui::MouseWheelEvent
& event
) {
217 // Construct an untranslated event from the platform event data.
218 blink::WebMouseWheelEvent webkit_event
= event
.native_event().message
?
219 MakeUntranslatedWebMouseWheelEventFromNativeEvent(event
.native_event()) :
220 MakeWebMouseWheelEventFromAuraEvent(event
);
222 blink::WebMouseWheelEvent webkit_event
=
223 MakeWebMouseWheelEventFromAuraEvent(event
);
226 // Replace the event's coordinate fields with translated position data from
228 webkit_event
.windowX
= webkit_event
.x
= event
.x();
229 webkit_event
.windowY
= webkit_event
.y
= event
.y();
231 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
232 webkit_event
.globalX
= screen_point
.x();
233 webkit_event
.globalY
= screen_point
.y();
235 // Scroll events generated from the mouse wheel when the control key is held
236 // don't trigger scrolling. Instead, they may cause zooming.
237 bool from_mouse_wheel
= !webkit_event
.hasPreciseScrollingDeltas
;
238 if ((webkit_event
.modifiers
& blink::WebInputEvent::ControlKey
) &&
240 webkit_event
.canScroll
= false;
246 blink::WebMouseWheelEvent
MakeWebMouseWheelEvent(const ui::ScrollEvent
& event
) {
248 // Construct an untranslated event from the platform event data.
249 blink::WebMouseWheelEvent webkit_event
= event
.native_event().message
?
250 MakeUntranslatedWebMouseWheelEventFromNativeEvent(event
.native_event()) :
251 MakeWebMouseWheelEventFromAuraEvent(event
);
253 blink::WebMouseWheelEvent webkit_event
=
254 MakeWebMouseWheelEventFromAuraEvent(event
);
257 // Replace the event's coordinate fields with translated position data from
259 webkit_event
.windowX
= webkit_event
.x
= event
.x();
260 webkit_event
.windowY
= webkit_event
.y
= event
.y();
262 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
263 webkit_event
.globalX
= screen_point
.x();
264 webkit_event
.globalY
= screen_point
.y();
269 blink::WebKeyboardEvent
MakeWebKeyboardEvent(const ui::KeyEvent
& event
) {
270 // Windows can figure out whether or not to construct a RawKeyDown or a Char
271 // WebInputEvent based on the type of message carried in
272 // event.native_event(). X11 is not so fortunate, there is no separate
273 // translated event type, so DesktopHostLinux sends an extra KeyEvent with
274 // is_char() == true. We need to pass the ui::KeyEvent to the X11 function
275 // to detect this case so the right event type can be constructed.
277 if (event
.HasNativeEvent()) {
278 // Key events require no translation by the aura system.
279 blink::WebKeyboardEvent
webkit_event(
280 MakeWebKeyboardEventFromNativeEvent(event
.native_event()));
281 webkit_event
.modifiers
|= DomCodeToWebInputEventModifiers(event
.code());
282 webkit_event
.domCode
= static_cast<int>(event
.code());
286 return MakeWebKeyboardEventFromAuraEvent(event
);
289 blink::WebGestureEvent
MakeWebGestureEvent(const ui::GestureEvent
& event
) {
290 blink::WebGestureEvent gesture_event
;
292 if (event
.HasNativeEvent())
293 gesture_event
= MakeWebGestureEventFromNativeEvent(event
.native_event());
295 gesture_event
= MakeWebGestureEventFromUIEvent(event
);
297 gesture_event
= MakeWebGestureEventFromUIEvent(event
);
300 gesture_event
.x
= event
.x();
301 gesture_event
.y
= event
.y();
303 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
304 gesture_event
.globalX
= screen_point
.x();
305 gesture_event
.globalY
= screen_point
.y();
307 return gesture_event
;
310 blink::WebGestureEvent
MakeWebGestureEvent(const ui::ScrollEvent
& event
) {
311 blink::WebGestureEvent gesture_event
;
314 gesture_event
= MakeWebGestureEventFromNativeEvent(event
.native_event());
316 gesture_event
= MakeWebGestureEventFromAuraEvent(event
);
319 gesture_event
.x
= event
.x();
320 gesture_event
.y
= event
.y();
322 const gfx::Point screen_point
= GetScreenLocationFromEvent(event
);
323 gesture_event
.globalX
= screen_point
.x();
324 gesture_event
.globalY
= screen_point
.y();
326 return gesture_event
;
329 blink::WebGestureEvent
MakeWebGestureEventFlingCancel() {
330 blink::WebGestureEvent gesture_event
;
332 // All other fields are ignored on a GestureFlingCancel event.
333 gesture_event
.type
= blink::WebInputEvent::GestureFlingCancel
;
334 gesture_event
.sourceDevice
= blink::WebGestureDeviceTouchpad
;
335 return gesture_event
;
338 blink::WebMouseEvent
MakeWebMouseEventFromAuraEvent(
339 const ui::MouseEvent
& event
) {
340 blink::WebMouseEvent webkit_event
;
342 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
343 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
345 webkit_event
.button
= blink::WebMouseEvent::ButtonNone
;
346 int button_flags
= event
.flags();
347 if (event
.type() == ui::ET_MOUSE_PRESSED
||
348 event
.type() == ui::ET_MOUSE_RELEASED
) {
349 // We want to use changed_button_flags() for mouse pressed & released.
350 // These flags can be used only if they are set which is not always the case
351 // (see e.g. GetChangedMouseButtonFlagsFromNative() in events_win.cc).
352 if (event
.changed_button_flags())
353 button_flags
= event
.changed_button_flags();
355 if (button_flags
& ui::EF_LEFT_MOUSE_BUTTON
)
356 webkit_event
.button
= blink::WebMouseEvent::ButtonLeft
;
357 if (button_flags
& ui::EF_MIDDLE_MOUSE_BUTTON
)
358 webkit_event
.button
= blink::WebMouseEvent::ButtonMiddle
;
359 if (button_flags
& ui::EF_RIGHT_MOUSE_BUTTON
)
360 webkit_event
.button
= blink::WebMouseEvent::ButtonRight
;
362 switch (event
.type()) {
363 case ui::ET_MOUSE_PRESSED
:
364 webkit_event
.type
= blink::WebInputEvent::MouseDown
;
365 webkit_event
.clickCount
= event
.GetClickCount();
367 case ui::ET_MOUSE_RELEASED
:
368 webkit_event
.type
= blink::WebInputEvent::MouseUp
;
369 webkit_event
.clickCount
= event
.GetClickCount();
371 case ui::ET_MOUSE_ENTERED
:
372 case ui::ET_MOUSE_EXITED
:
373 case ui::ET_MOUSE_MOVED
:
374 case ui::ET_MOUSE_DRAGGED
:
375 webkit_event
.type
= blink::WebInputEvent::MouseMove
;
378 NOTIMPLEMENTED() << "Received unexpected event: " << event
.type();
385 blink::WebMouseWheelEvent
MakeWebMouseWheelEventFromAuraEvent(
386 const ui::MouseWheelEvent
& event
) {
387 blink::WebMouseWheelEvent webkit_event
;
389 webkit_event
.type
= blink::WebInputEvent::MouseWheel
;
390 webkit_event
.button
= blink::WebMouseEvent::ButtonNone
;
391 webkit_event
.modifiers
= ui::EventFlagsToWebEventModifiers(event
.flags());
392 webkit_event
.timeStampSeconds
= event
.time_stamp().InSecondsF();
394 if ((event
.flags() & ui::EF_SHIFT_DOWN
) != 0 && event
.x_offset() == 0) {
395 webkit_event
.deltaX
= event
.y_offset();
396 webkit_event
.deltaY
= 0;
398 webkit_event
.deltaX
= event
.x_offset();
399 webkit_event
.deltaY
= event
.y_offset();
402 webkit_event
.wheelTicksX
= webkit_event
.deltaX
/ kPixelsPerTick
;
403 webkit_event
.wheelTicksY
= webkit_event
.deltaY
/ kPixelsPerTick
;
408 } // namespace content