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/base_event_utils.h"
10 #include "ui/events/keycodes/keyboard_codes.h"
14 using blink::WebInputEvent
;
15 using blink::WebKeyboardEvent
;
16 using blink::WebGestureEvent
;
17 using blink::WebMouseEvent
;
18 using blink::WebMouseWheelEvent
;
19 using blink::WebTouchEvent
;
20 using blink::WebTouchPoint
;
22 WebMouseEvent
SyntheticWebMouseEventBuilder::Build(
23 blink::WebInputEvent::Type type
) {
29 WebMouseEvent
SyntheticWebMouseEventBuilder::Build(
30 blink::WebInputEvent::Type type
,
34 DCHECK(WebInputEvent::isMouseEventType(type
));
35 WebMouseEvent result
= Build(type
);
38 result
.windowX
= window_x
;
39 result
.windowY
= window_y
;
40 result
.modifiers
= modifiers
;
42 if (type
== WebInputEvent::MouseDown
|| type
== WebInputEvent::MouseUp
)
43 result
.button
= WebMouseEvent::ButtonLeft
;
45 result
.button
= WebMouseEvent::ButtonNone
;
50 WebMouseWheelEvent
SyntheticWebMouseWheelEventBuilder::Build(
51 WebMouseWheelEvent::Phase phase
) {
52 WebMouseWheelEvent result
;
53 result
.type
= WebInputEvent::MouseWheel
;
58 WebMouseWheelEvent
SyntheticWebMouseWheelEventBuilder::Build(float dx
,
62 WebMouseWheelEvent result
;
63 result
.type
= WebInputEvent::MouseWheel
;
67 result
.wheelTicksX
= dx
> 0.0f
? 1.0f
: -1.0f
;
69 result
.wheelTicksY
= dy
> 0.0f
? 1.0f
: -1.0f
;
70 result
.modifiers
= modifiers
;
71 result
.hasPreciseScrollingDeltas
= precise
;
72 result
.canScroll
= true;
76 WebKeyboardEvent
SyntheticWebKeyboardEventBuilder::Build(
77 WebInputEvent::Type type
) {
78 DCHECK(WebInputEvent::isKeyboardEventType(type
));
79 WebKeyboardEvent result
;
81 result
.windowsKeyCode
= ui::VKEY_L
; // non-null made up value.
85 WebGestureEvent
SyntheticWebGestureEventBuilder::Build(
86 WebInputEvent::Type type
,
87 blink::WebGestureDevice source_device
) {
88 DCHECK(WebInputEvent::isGestureEventType(type
));
89 WebGestureEvent result
;
91 result
.sourceDevice
= source_device
;
92 if (type
== WebInputEvent::GestureTap
||
93 type
== WebInputEvent::GestureTapUnconfirmed
||
94 type
== WebInputEvent::GestureDoubleTap
) {
95 result
.data
.tap
.tapCount
= 1;
96 result
.data
.tap
.width
= 10;
97 result
.data
.tap
.height
= 10;
102 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildScrollBegin(
105 WebGestureEvent result
= Build(WebInputEvent::GestureScrollBegin
,
106 blink::WebGestureDeviceTouchscreen
);
107 result
.data
.scrollBegin
.deltaXHint
= dx_hint
;
108 result
.data
.scrollBegin
.deltaYHint
= dy_hint
;
112 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildScrollUpdate(
116 blink::WebGestureDevice source_device
) {
117 WebGestureEvent result
=
118 Build(WebInputEvent::GestureScrollUpdate
, source_device
);
119 result
.data
.scrollUpdate
.deltaX
= dx
;
120 result
.data
.scrollUpdate
.deltaY
= dy
;
121 result
.modifiers
= modifiers
;
125 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildPinchUpdate(
130 blink::WebGestureDevice source_device
) {
131 WebGestureEvent result
=
132 Build(WebInputEvent::GesturePinchUpdate
, source_device
);
133 result
.data
.pinchUpdate
.scale
= scale
;
136 result
.globalX
= anchor_x
;
137 result
.globalY
= anchor_y
;
138 result
.modifiers
= modifiers
;
142 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildFling(
145 blink::WebGestureDevice source_device
) {
146 WebGestureEvent result
= Build(WebInputEvent::GestureFlingStart
,
148 result
.data
.flingStart
.velocityX
= velocity_x
;
149 result
.data
.flingStart
.velocityY
= velocity_y
;
153 SyntheticWebTouchEvent::SyntheticWebTouchEvent() : WebTouchEvent() {
154 uniqueTouchEventId
= ui::GetNextTouchEventId();
155 SetTimestamp(base::TimeTicks::Now() - base::TimeTicks());
158 void SyntheticWebTouchEvent::ResetPoints() {
160 for (unsigned int i
= 0; i
< touchesLength
; ++i
) {
161 if (touches
[i
].state
== WebTouchPoint::StateReleased
)
164 touches
[point
] = touches
[i
];
165 touches
[point
].state
= WebTouchPoint::StateStationary
;
168 touchesLength
= point
;
169 type
= WebInputEvent::Undefined
;
170 causesScrollingIfUncanceled
= false;
171 uniqueTouchEventId
= ui::GetNextTouchEventId();
174 int SyntheticWebTouchEvent::PressPoint(float x
, float y
) {
175 if (touchesLength
== touchesLengthCap
)
177 WebTouchPoint
& point
= touches
[touchesLength
];
178 point
.id
= touchesLength
;
179 point
.position
.x
= point
.screenPosition
.x
= x
;
180 point
.position
.y
= point
.screenPosition
.y
= y
;
181 point
.state
= WebTouchPoint::StatePressed
;
182 point
.radiusX
= point
.radiusY
= 1.f
;
183 point
.rotationAngle
= 1.f
;
186 WebTouchEventTraits::ResetType(
187 WebInputEvent::TouchStart
, timeStampSeconds
, this);
191 void SyntheticWebTouchEvent::MovePoint(int index
, float x
, float y
) {
193 CHECK_LT(index
, touchesLengthCap
);
194 // Always set this bit to avoid otherwise unexpected touchmove suppression.
195 // The caller can opt-out explicitly, if necessary.
196 causesScrollingIfUncanceled
= true;
197 WebTouchPoint
& point
= touches
[index
];
198 point
.position
.x
= point
.screenPosition
.x
= x
;
199 point
.position
.y
= point
.screenPosition
.y
= y
;
200 touches
[index
].state
= WebTouchPoint::StateMoved
;
201 WebTouchEventTraits::ResetType(
202 WebInputEvent::TouchMove
, timeStampSeconds
, this);
205 void SyntheticWebTouchEvent::ReleasePoint(int index
) {
207 CHECK_LT(index
, touchesLengthCap
);
208 touches
[index
].state
= WebTouchPoint::StateReleased
;
209 WebTouchEventTraits::ResetType(
210 WebInputEvent::TouchEnd
, timeStampSeconds
, this);
213 void SyntheticWebTouchEvent::CancelPoint(int index
) {
215 CHECK_LT(index
, touchesLengthCap
);
216 touches
[index
].state
= WebTouchPoint::StateCancelled
;
217 WebTouchEventTraits::ResetType(
218 WebInputEvent::TouchCancel
, timeStampSeconds
, this);
221 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp
) {
222 timeStampSeconds
= timestamp
.InSecondsF();
225 } // namespace content