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
;
74 WebKeyboardEvent
SyntheticWebKeyboardEventBuilder::Build(
75 WebInputEvent::Type type
) {
76 DCHECK(WebInputEvent::isKeyboardEventType(type
));
77 WebKeyboardEvent result
;
79 result
.windowsKeyCode
= ui::VKEY_L
; // non-null made up value.
83 WebGestureEvent
SyntheticWebGestureEventBuilder::Build(
84 WebInputEvent::Type type
,
85 blink::WebGestureDevice source_device
) {
86 DCHECK(WebInputEvent::isGestureEventType(type
));
87 WebGestureEvent result
;
89 result
.sourceDevice
= source_device
;
90 if (type
== WebInputEvent::GestureTap
||
91 type
== WebInputEvent::GestureTapUnconfirmed
||
92 type
== WebInputEvent::GestureDoubleTap
) {
93 result
.data
.tap
.tapCount
= 1;
94 result
.data
.tap
.width
= 10;
95 result
.data
.tap
.height
= 10;
100 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildScrollBegin(
103 WebGestureEvent result
= Build(WebInputEvent::GestureScrollBegin
,
104 blink::WebGestureDeviceTouchscreen
);
105 result
.data
.scrollBegin
.deltaXHint
= dx_hint
;
106 result
.data
.scrollBegin
.deltaYHint
= dy_hint
;
110 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildScrollUpdate(
114 WebGestureEvent result
= Build(WebInputEvent::GestureScrollUpdate
,
115 blink::WebGestureDeviceTouchscreen
);
116 result
.data
.scrollUpdate
.deltaX
= dx
;
117 result
.data
.scrollUpdate
.deltaY
= dy
;
118 result
.modifiers
= modifiers
;
122 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildPinchUpdate(
127 blink::WebGestureDevice source_device
) {
128 WebGestureEvent result
=
129 Build(WebInputEvent::GesturePinchUpdate
, source_device
);
130 result
.data
.pinchUpdate
.scale
= scale
;
133 result
.globalX
= anchor_x
;
134 result
.globalY
= anchor_y
;
135 result
.modifiers
= modifiers
;
139 WebGestureEvent
SyntheticWebGestureEventBuilder::BuildFling(
142 blink::WebGestureDevice source_device
) {
143 WebGestureEvent result
= Build(WebInputEvent::GestureFlingStart
,
145 result
.data
.flingStart
.velocityX
= velocity_x
;
146 result
.data
.flingStart
.velocityY
= velocity_y
;
150 SyntheticWebTouchEvent::SyntheticWebTouchEvent() : WebTouchEvent() {
151 SetTimestamp(base::TimeTicks::Now() - base::TimeTicks());
154 void SyntheticWebTouchEvent::ResetPoints() {
156 for (unsigned int i
= 0; i
< touchesLength
; ++i
) {
157 if (touches
[i
].state
== WebTouchPoint::StateReleased
)
160 touches
[point
] = touches
[i
];
161 touches
[point
].state
= WebTouchPoint::StateStationary
;
164 touchesLength
= point
;
165 type
= WebInputEvent::Undefined
;
168 int SyntheticWebTouchEvent::PressPoint(float x
, float y
) {
169 if (touchesLength
== touchesLengthCap
)
171 WebTouchPoint
& point
= touches
[touchesLength
];
172 point
.id
= touchesLength
;
173 point
.position
.x
= point
.screenPosition
.x
= x
;
174 point
.position
.y
= point
.screenPosition
.y
= y
;
175 point
.state
= WebTouchPoint::StatePressed
;
176 point
.radiusX
= point
.radiusY
= 1.f
;
178 WebTouchEventTraits::ResetType(
179 WebInputEvent::TouchStart
, timeStampSeconds
, this);
183 void SyntheticWebTouchEvent::MovePoint(int index
, float x
, float y
) {
184 CHECK(index
>= 0 && index
< touchesLengthCap
);
185 WebTouchPoint
& point
= touches
[index
];
186 point
.position
.x
= point
.screenPosition
.x
= x
;
187 point
.position
.y
= point
.screenPosition
.y
= y
;
188 touches
[index
].state
= WebTouchPoint::StateMoved
;
189 WebTouchEventTraits::ResetType(
190 WebInputEvent::TouchMove
, timeStampSeconds
, this);
193 void SyntheticWebTouchEvent::ReleasePoint(int index
) {
194 CHECK(index
>= 0 && index
< touchesLengthCap
);
195 touches
[index
].state
= WebTouchPoint::StateReleased
;
196 WebTouchEventTraits::ResetType(
197 WebInputEvent::TouchEnd
, timeStampSeconds
, this);
200 void SyntheticWebTouchEvent::CancelPoint(int index
) {
201 CHECK(index
>= 0 && index
< touchesLengthCap
);
202 touches
[index
].state
= WebTouchPoint::StateCancelled
;
203 WebTouchEventTraits::ResetType(
204 WebInputEvent::TouchCancel
, timeStampSeconds
, this);
207 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp
) {
208 timeStampSeconds
= timestamp
.InSecondsF();
211 } // namespace content