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 blink::WebGestureDevice 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 blink::WebGestureDeviceTouchscreen); 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 blink::WebGestureDeviceTouchscreen); 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 blink::WebGestureDevice 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 blink::WebGestureDevice 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 SetTimestamp(base::TimeTicks::Now() - base::TimeTicks()); 152 } 153 154 void SyntheticWebTouchEvent::ResetPoints() { 155 int point = 0; 156 for (unsigned int i = 0; i < touchesLength; ++i) { 157 if (touches[i].state == WebTouchPoint::StateReleased) 158 continue; 159 160 touches[point] = touches[i]; 161 touches[point].state = WebTouchPoint::StateStationary; 162 ++point; 163 } 164 touchesLength = point; 165 type = WebInputEvent::Undefined; 166 } 167 168 int SyntheticWebTouchEvent::PressPoint(float x, float y) { 169 if (touchesLength == touchesLengthCap) 170 return -1; 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; 177 ++touchesLength; 178 WebTouchEventTraits::ResetType( 179 WebInputEvent::TouchStart, timeStampSeconds, this); 180 return point.id; 181 } 182 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); 191 } 192 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); 198 } 199 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); 205 } 206 207 void SyntheticWebTouchEvent::SetTimestamp(base::TimeDelta timestamp) { 208 timeStampSeconds = timestamp.InSecondsF(); 209 } 210 211 } // namespace content 212