1 // Copyright 2013 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/common/input/synthetic_web_input_event_builders.h"
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
9 #include "ui/events/keycodes/keyboard_codes.h"
13 using blink::WebInputEvent
;
14 using blink::WebKeyboardEvent
;
15 using blink::WebGestureEvent
;
16 using blink::WebMouseEvent
;
17 using blink::WebMouseWheelEvent
;
18 using blink::WebTouchEvent
;
19 using blink::WebTouchPoint
;
21 WebMouseEvent
SyntheticWebMouseEventBuilder::Build(
22 blink::WebInputEvent::Type type
) {
28 WebMouseEvent
SyntheticWebMouseEventBuilder::Build(
29 blink::WebInputEvent::Type type
,
33 DCHECK(WebInputEvent::isMouseEventType(type
));
34 WebMouseEvent result
= Build(type
);
37 result
.windowX
= window_x
;
38 result
.windowY
= window_y
;
39 result
.modifiers
= modifiers
;
41 if (type
== WebInputEvent::MouseDown
|| type
== WebInputEvent::MouseUp
)
42 result
.button
= WebMouseEvent::ButtonLeft
;
44 result
.button
= WebMouseEvent::ButtonNone
;
49 WebMouseWheelEvent
SyntheticWebMouseWheelEventBuilder::Build(
50 WebMouseWheelEvent::Phase phase
) {
51 WebMouseWheelEvent result
;
52 result
.type
= WebInputEvent::MouseWheel
;
57 WebMouseWheelEvent
SyntheticWebMouseWheelEventBuilder::Build(float dx
,
61 WebMouseWheelEvent result
;
62 result
.type
= WebInputEvent::MouseWheel
;
66 result
.wheelTicksX
= dx
> 0.0f
? 1.0f
: -1.0f
;
68 result
.wheelTicksY
= dy
> 0.0f
? 1.0f
: -1.0f
;
69 result
.modifiers
= modifiers
;
70 result
.hasPreciseScrollingDeltas
= precise
;
71 result
.canScroll
= true;
75 WebKeyboardEvent
SyntheticWebKeyboardEventBuilder::Build(
76 WebInputEvent::Type type
) {
77 DCHECK(WebInputEvent::isKeyboardEventType(type
));
78 WebKeyboardEvent result
;
80 result
.windowsKeyCode
= ui::VKEY_L
; // non-null made up value.
84 WebGestureEvent
SyntheticWebGestureEventBuilder::Build(
85 WebInputEvent::Type type
,
86 blink::WebGestureDevice source_device
) {
87 DCHECK(WebInputEvent::isGestureEventType(type
));
88 WebGestureEvent result
;
90 result
.sourceDevice
= source_device
;
91 if (type
== WebInputEvent::GestureTap
||
92 type
== WebInputEvent::GestureTapUnconfirmed
||
93 type
== WebInputEvent::GestureDoubleTap
) {
94 result
.data
.tap
.tapCount
= 1;
95 result
.data
.tap
.width
= 10;
96 result
.data
.tap
.height
= 10;
101 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildScrollBegin(
104 WebGestureEvent result
= Build(WebInputEvent::GestureScrollBegin
,
105 blink::WebGestureDeviceTouchscreen
);
106 result
.data
.scrollBegin
.deltaXHint
= dx_hint
;
107 result
.data
.scrollBegin
.deltaYHint
= dy_hint
;
111 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildScrollUpdate(
115 blink::WebGestureDevice source_device
) {
116 WebGestureEvent result
=
117 Build(WebInputEvent::GestureScrollUpdate
, source_device
);
118 result
.data
.scrollUpdate
.deltaX
= dx
;
119 result
.data
.scrollUpdate
.deltaY
= dy
;
120 result
.modifiers
= modifiers
;
124 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildPinchUpdate(
129 blink::WebGestureDevice source_device
) {
130 WebGestureEvent result
=
131 Build(WebInputEvent::GesturePinchUpdate
, source_device
);
132 result
.data
.pinchUpdate
.scale
= scale
;
135 result
.globalX
= anchor_x
;
136 result
.globalY
= anchor_y
;
137 result
.modifiers
= modifiers
;
141 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildFling(
144 blink::WebGestureDevice source_device
) {
145 WebGestureEvent result
= Build(WebInputEvent::GestureFlingStart
,
147 result
.data
.flingStart
.velocityX
= velocity_x
;
148 result
.data
.flingStart
.velocityY
= velocity_y
;
152 SyntheticWebTouchEvent::SyntheticWebTouchEvent() : WebTouchEvent() {
153 SetTimestamp(base::TimeTicks::Now() - base::TimeTicks());
156 void SyntheticWebTouchEvent::ResetPoints() {
158 for (unsigned int i
= 0; i
< touchesLength
; ++i
) {
159 if (touches
[i
].state
== WebTouchPoint::StateReleased
)
162 touches
[point
] = touches
[i
];
163 touches
[point
].state
= WebTouchPoint::StateStationary
;
166 touchesLength
= point
;
167 type
= WebInputEvent::Undefined
;
168 causesScrollingIfUncanceled
= false;
171 int SyntheticWebTouchEvent::PressPoint(float x
, float y
) {
172 if (touchesLength
== touchesLengthCap
)
174 WebTouchPoint
& point
= touches
[touchesLength
];
175 point
.id
= touchesLength
;
176 point
.position
.x
= point
.screenPosition
.x
= x
;
177 point
.position
.y
= point
.screenPosition
.y
= y
;
178 point
.state
= WebTouchPoint::StatePressed
;
179 point
.radiusX
= point
.radiusY
= 1.f
;
180 point
.rotationAngle
= 1.f
;
183 WebTouchEventTraits::ResetType(
184 WebInputEvent::TouchStart
, timeStampSeconds
, this);
188 void SyntheticWebTouchEvent::MovePoint(int index
, float x
, float y
) {
190 CHECK_LT(index
, touchesLengthCap
);
191 // Always set this bit to avoid otherwise unexpected touchmove suppression.
192 // The caller can opt-out explicitly, if necessary.
193 causesScrollingIfUncanceled
= true;
194 WebTouchPoint
& point
= touches
[index
];
195 point
.position
.x
= point
.screenPosition
.x
= x
;
196 point
.position
.y
= point
.screenPosition
.y
= y
;
197 touches
[index
].state
= WebTouchPoint::StateMoved
;
198 WebTouchEventTraits::ResetType(
199 WebInputEvent::TouchMove
, timeStampSeconds
, this);
202 void SyntheticWebTouchEvent::ReleasePoint(int index
) {
204 CHECK_LT(index
, touchesLengthCap
);
205 touches
[index
].state
= WebTouchPoint::StateReleased
;
206 WebTouchEventTraits::ResetType(
207 WebInputEvent::TouchEnd
, timeStampSeconds
, this);
210 void SyntheticWebTouchEvent::CancelPoint(int index
) {
212 CHECK_LT(index
, touchesLengthCap
);
213 touches
[index
].state
= WebTouchPoint::StateCancelled
;
214 WebTouchEventTraits::ResetType(
215 WebInputEvent::TouchCancel
, timeStampSeconds
, this);
218 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp
) {
219 timeStampSeconds
= timestamp
.InSecondsF();
222 } // namespace content