Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / common / input / synthetic_web_input_event_builders.cc
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.
4
5 #include "content/common/input/synthetic_web_input_event_builders.h"
6
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
9 #include "ui/events/keycodes/keyboard_codes.h"
10
11 namespace content {
12
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;
20
21 WebMouseEvent SyntheticWebMouseEventBuilder::Build(
22     blink::WebInputEvent::Type type) {
23   WebMouseEvent result;
24   result.type = type;
25   return result;
26 }
27
28 WebMouseEvent SyntheticWebMouseEventBuilder::Build(
29     blink::WebInputEvent::Type type,
30     int window_x,
31     int window_y,
32     int modifiers) {
33   DCHECK(WebInputEvent::isMouseEventType(type));
34   WebMouseEvent result = Build(type);
35   result.x = window_x;
36   result.y = window_y;
37   result.windowX = window_x;
38   result.windowY = window_y;
39   result.modifiers = modifiers;
40
41   if (type == WebInputEvent::MouseDown || type == WebInputEvent::MouseUp)
42     result.button = WebMouseEvent::ButtonLeft;
43   else
44     result.button = WebMouseEvent::ButtonNone;
45
46   return result;
47 }
48
49 WebMouseWheelEvent SyntheticWebMouseWheelEventBuilder::Build(
50     WebMouseWheelEvent::Phase phase) {
51   WebMouseWheelEvent result;
52   result.type = WebInputEvent::MouseWheel;
53   result.phase = phase;
54   return result;
55 }
56
57 WebMouseWheelEvent SyntheticWebMouseWheelEventBuilder::Build(float dx,
58                                                              float dy,
59                                                              int modifiers,
60                                                              bool precise) {
61   WebMouseWheelEvent result;
62   result.type = WebInputEvent::MouseWheel;
63   result.deltaX = dx;
64   result.deltaY = dy;
65   if (dx)
66     result.wheelTicksX = dx > 0.0f ? 1.0f : -1.0f;
67   if (dy)
68     result.wheelTicksY = dy > 0.0f ? 1.0f : -1.0f;
69   result.modifiers = modifiers;
70   result.hasPreciseScrollingDeltas = precise;
71   return result;
72 }
73
74 WebKeyboardEvent SyntheticWebKeyboardEventBuilder::Build(
75     WebInputEvent::Type type) {
76   DCHECK(WebInputEvent::isKeyboardEventType(type));
77   WebKeyboardEvent result;
78   result.type = type;
79   result.windowsKeyCode = ui::VKEY_L;  // non-null made up value.
80   return result;
81 }
82
83 WebGestureEvent SyntheticWebGestureEventBuilder::Build(
84     WebInputEvent::Type type,
85     WebGestureEvent::SourceDevice source_device) {
86   DCHECK(WebInputEvent::isGestureEventType(type));
87   WebGestureEvent result;
88   result.type = type;
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;
96   }
97   return result;
98 }
99
100 WebGestureEvent SyntheticWebGestureEventBuilder::BuildScrollBegin(
101     float dx_hint,
102     float dy_hint) {
103   WebGestureEvent result = Build(WebInputEvent::GestureScrollBegin,
104                                  WebGestureEvent::Touchscreen);
105   result.data.scrollBegin.deltaXHint = dx_hint;
106   result.data.scrollBegin.deltaYHint = dy_hint;
107   return result;
108 }
109
110 WebGestureEvent SyntheticWebGestureEventBuilder::BuildScrollUpdate(
111     float dx,
112     float dy,
113     int modifiers) {
114   WebGestureEvent result = Build(WebInputEvent::GestureScrollUpdate,
115                                  WebGestureEvent::Touchscreen);
116   result.data.scrollUpdate.deltaX = dx;
117   result.data.scrollUpdate.deltaY = dy;
118   result.modifiers = modifiers;
119   return result;
120 }
121
122 WebGestureEvent SyntheticWebGestureEventBuilder::BuildPinchUpdate(
123     float scale,
124     float anchor_x,
125     float anchor_y,
126     int modifiers,
127     WebGestureEvent::SourceDevice source_device) {
128   WebGestureEvent result =
129       Build(WebInputEvent::GesturePinchUpdate, source_device);
130   result.data.pinchUpdate.scale = scale;
131   result.x = anchor_x;
132   result.y = anchor_y;
133   result.globalX = anchor_x;
134   result.globalY = anchor_y;
135   result.modifiers = modifiers;
136   return result;
137 }
138
139 WebGestureEvent SyntheticWebGestureEventBuilder::BuildFling(
140     float velocity_x,
141     float velocity_y,
142     WebGestureEvent::SourceDevice source_device) {
143   WebGestureEvent result = Build(WebInputEvent::GestureFlingStart,
144                                  source_device);
145   result.data.flingStart.velocityX = velocity_x;
146   result.data.flingStart.velocityY = velocity_y;
147   return result;
148 }
149
150 SyntheticWebTouchEvent::SyntheticWebTouchEvent() : WebTouchEvent() {}
151
152 void SyntheticWebTouchEvent::ResetPoints() {
153   int point = 0;
154   for (unsigned int i = 0; i < touchesLength; ++i) {
155     if (touches[i].state == WebTouchPoint::StateReleased)
156       continue;
157
158     touches[point] = touches[i];
159     touches[point].state = WebTouchPoint::StateStationary;
160     ++point;
161   }
162   touchesLength = point;
163   type = WebInputEvent::Undefined;
164 }
165
166 int SyntheticWebTouchEvent::PressPoint(float x, float y) {
167   if (touchesLength == touchesLengthCap)
168     return -1;
169   WebTouchPoint& point = touches[touchesLength];
170   point.id = touchesLength;
171   point.position.x = point.screenPosition.x = x;
172   point.position.y = point.screenPosition.y = y;
173   point.state = WebTouchPoint::StatePressed;
174   point.radiusX = point.radiusY = 1.f;
175   ++touchesLength;
176   WebTouchEventTraits::ResetType(
177       WebInputEvent::TouchStart, timeStampSeconds, this);
178   return point.id;
179 }
180
181 void SyntheticWebTouchEvent::MovePoint(int index, float x, float y) {
182   CHECK(index >= 0 && index < touchesLengthCap);
183   WebTouchPoint& point = touches[index];
184   point.position.x = point.screenPosition.x = x;
185   point.position.y = point.screenPosition.y = y;
186   touches[index].state = WebTouchPoint::StateMoved;
187   WebTouchEventTraits::ResetType(
188       WebInputEvent::TouchMove, timeStampSeconds, this);
189 }
190
191 void SyntheticWebTouchEvent::ReleasePoint(int index) {
192   CHECK(index >= 0 && index < touchesLengthCap);
193   touches[index].state = WebTouchPoint::StateReleased;
194   WebTouchEventTraits::ResetType(
195       WebInputEvent::TouchEnd, timeStampSeconds, this);
196 }
197
198 void SyntheticWebTouchEvent::CancelPoint(int index) {
199   CHECK(index >= 0 && index < touchesLengthCap);
200   touches[index].state = WebTouchPoint::StateCancelled;
201   WebTouchEventTraits::ResetType(
202       WebInputEvent::TouchCancel, timeStampSeconds, this);
203 }
204
205 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp) {
206   timeStampSeconds = timestamp.InSecondsF();
207 }
208
209 }  // namespace content