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