| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/input/web_input_event_builders_android.h
" | 5 #include "content/browser/renderer_host/input/web_input_event_builders_android.h
" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/browser/renderer_host/input/motion_event_android.h" | 8 #include "content/browser/renderer_host/input/motion_event_android.h" |
| 9 #include "content/browser/renderer_host/input/web_input_event_util.h" | 9 #include "content/browser/renderer_host/input/web_input_event_util.h" |
| 10 #include "content/browser/renderer_host/input/web_input_event_util_posix.h" | 10 #include "content/browser/renderer_host/input/web_input_event_util_posix.h" |
| 11 #include "ui/events/keycodes/keyboard_code_conversion_android.h" | 11 #include "ui/events/keycodes/keyboard_code_conversion_android.h" |
| 12 #include "ui/events/keycodes/keyboard_codes_posix.h" | 12 #include "ui/events/keycodes/keyboard_codes_posix.h" |
| 13 | 13 |
| 14 using blink::WebInputEvent; | 14 using blink::WebInputEvent; |
| 15 using blink::WebKeyboardEvent; | 15 using blink::WebKeyboardEvent; |
| 16 using blink::WebGestureEvent; | 16 using blink::WebGestureEvent; |
| 17 using blink::WebMouseEvent; | 17 using blink::WebMouseEvent; |
| 18 using blink::WebMouseWheelEvent; | 18 using blink::WebMouseWheelEvent; |
| 19 using blink::WebTouchEvent; | 19 using blink::WebTouchEvent; |
| 20 using blink::WebTouchPoint; | 20 using blink::WebTouchPoint; |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 namespace { | |
| 24 | |
| 25 WebInputEvent::Type ToWebInputEventType(MotionEventAndroid::Action action) { | |
| 26 switch (action) { | |
| 27 case MotionEventAndroid::ACTION_DOWN: | |
| 28 return WebInputEvent::TouchStart; | |
| 29 case MotionEventAndroid::ACTION_MOVE: | |
| 30 return WebInputEvent::TouchMove; | |
| 31 case MotionEventAndroid::ACTION_UP: | |
| 32 return WebInputEvent::TouchEnd; | |
| 33 case MotionEventAndroid::ACTION_CANCEL: | |
| 34 return WebInputEvent::TouchCancel; | |
| 35 case MotionEventAndroid::ACTION_POINTER_DOWN: | |
| 36 return WebInputEvent::TouchStart; | |
| 37 case MotionEventAndroid::ACTION_POINTER_UP: | |
| 38 return WebInputEvent::TouchEnd; | |
| 39 } | |
| 40 NOTREACHED() << "Invalid MotionEventAndroid::Action."; | |
| 41 return WebInputEvent::Undefined; | |
| 42 } | |
| 43 | |
| 44 // Note that |is_action_pointer| is meaningful only in the context of | |
| 45 // |ACTION_POINTER_UP| and |ACTION_POINTER_DOWN|; other actions map directly to | |
| 46 // WebTouchPoint::State. | |
| 47 WebTouchPoint::State ToWebTouchPointState(MotionEventAndroid::Action action, | |
| 48 bool is_action_pointer) { | |
| 49 switch (action) { | |
| 50 case MotionEventAndroid::ACTION_DOWN: | |
| 51 return WebTouchPoint::StatePressed; | |
| 52 case MotionEventAndroid::ACTION_MOVE: | |
| 53 return WebTouchPoint::StateMoved; | |
| 54 case MotionEventAndroid::ACTION_UP: | |
| 55 return WebTouchPoint::StateReleased; | |
| 56 case MotionEventAndroid::ACTION_CANCEL: | |
| 57 return WebTouchPoint::StateCancelled; | |
| 58 case MotionEventAndroid::ACTION_POINTER_DOWN: | |
| 59 return is_action_pointer ? WebTouchPoint::StatePressed | |
| 60 : WebTouchPoint::StateStationary; | |
| 61 case MotionEventAndroid::ACTION_POINTER_UP: | |
| 62 return is_action_pointer ? WebTouchPoint::StateReleased | |
| 63 : WebTouchPoint::StateStationary; | |
| 64 } | |
| 65 NOTREACHED() << "Invalid MotionEventAndroid::Action."; | |
| 66 return WebTouchPoint::StateUndefined; | |
| 67 } | |
| 68 | |
| 69 WebTouchPoint BuildWebTouchPoint(const MotionEventAndroid& event, | |
| 70 size_t pointer_index, | |
| 71 float px_to_dp) { | |
| 72 WebTouchPoint touch; | |
| 73 touch.id = event.GetPointerId(pointer_index); | |
| 74 touch.state = ToWebTouchPointState( | |
| 75 event.GetAction(), | |
| 76 static_cast<int>(pointer_index) == event.GetActionIndex()); | |
| 77 touch.position.x = event.GetX(pointer_index) * px_to_dp; | |
| 78 touch.position.y = event.GetY(pointer_index) * px_to_dp; | |
| 79 // TODO(joth): Raw event co-ordinates. | |
| 80 touch.screenPosition = touch.position; | |
| 81 | |
| 82 const int radius_major = | |
| 83 static_cast<int>(event.GetTouchMajor(pointer_index) * 0.5f * px_to_dp); | |
| 84 const int radius_minor = | |
| 85 static_cast<int>(event.GetTouchMinor(pointer_index) * 0.5f * px_to_dp); | |
| 86 const float major_angle_in_radians_clockwise_from_vertical = | |
| 87 event.GetOrientation(); | |
| 88 | |
| 89 float major_angle_in_degrees_clockwise_from_vertical = 0; | |
| 90 if (!std::isnan(major_angle_in_radians_clockwise_from_vertical)) { | |
| 91 major_angle_in_degrees_clockwise_from_vertical = | |
| 92 major_angle_in_radians_clockwise_from_vertical * 180.f / M_PI; | |
| 93 } | |
| 94 // Android provides a major axis orientation clockwise with respect to the | |
| 95 // vertical of [-90, 90]. The proposed W3C extension specifies the angle that | |
| 96 // the ellipse described by radiusX and radiusY is rotated clockwise about | |
| 97 // its center, with a value of [0, 90], see | |
| 98 // http://www.w3.org/TR/2011/WD-touch-events-20110505/. | |
| 99 if (major_angle_in_degrees_clockwise_from_vertical >= 0) { | |
| 100 touch.radiusX = radius_minor; | |
| 101 touch.radiusY = radius_major; | |
| 102 touch.rotationAngle = major_angle_in_degrees_clockwise_from_vertical; | |
| 103 } else { | |
| 104 touch.radiusX = radius_major; | |
| 105 touch.radiusY = radius_minor; | |
| 106 touch.rotationAngle = major_angle_in_degrees_clockwise_from_vertical + 90.f; | |
| 107 } | |
| 108 DCHECK_GE(touch.rotationAngle, 0.f); | |
| 109 DCHECK_LE(touch.rotationAngle, 90.f); | |
| 110 | |
| 111 touch.force = event.GetPressure(pointer_index); | |
| 112 | |
| 113 return touch; | |
| 114 } | |
| 115 | |
| 116 } // namespace | |
| 117 | 23 |
| 118 WebKeyboardEvent WebKeyboardEventBuilder::Build(WebInputEvent::Type type, | 24 WebKeyboardEvent WebKeyboardEventBuilder::Build(WebInputEvent::Type type, |
| 119 int modifiers, | 25 int modifiers, |
| 120 double time_sec, | 26 double time_sec, |
| 121 int keycode, | 27 int keycode, |
| 122 int unicode_character, | 28 int unicode_character, |
| 123 bool is_system_key) { | 29 bool is_system_key) { |
| 124 DCHECK(WebInputEvent::isKeyboardEventType(type)); | 30 DCHECK(WebInputEvent::isKeyboardEventType(type)); |
| 125 WebKeyboardEvent result; | 31 WebKeyboardEvent result; |
| 126 | 32 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 result.x = x; | 128 result.x = x; |
| 223 result.y = y; | 129 result.y = y; |
| 224 result.timeStampSeconds = time_sec; | 130 result.timeStampSeconds = time_sec; |
| 225 result.sourceDevice = WebGestureEvent::Touchscreen; | 131 result.sourceDevice = WebGestureEvent::Touchscreen; |
| 226 | 132 |
| 227 return result; | 133 return result; |
| 228 } | 134 } |
| 229 | 135 |
| 230 blink::WebTouchEvent WebTouchEventBuilder::Build( | 136 blink::WebTouchEvent WebTouchEventBuilder::Build( |
| 231 const MotionEventAndroid& event, | 137 const MotionEventAndroid& event, |
| 232 float device_scale_factor) { | 138 float scale) { |
| 233 blink::WebTouchEvent result; | 139 return CreateWebTouchEventFromMotionEvent(event, scale); |
| 234 | |
| 235 result.type = ToWebInputEventType(event.GetAction()); | |
| 236 DCHECK(WebInputEvent::isTouchEventType(result.type)); | |
| 237 | |
| 238 result.timeStampSeconds = | |
| 239 (event.GetEventTime() - base::TimeTicks()).InSecondsF(); | |
| 240 | |
| 241 result.touchesLength = | |
| 242 std::min(event.GetPointerCount(), | |
| 243 static_cast<size_t>(WebTouchEvent::touchesLengthCap)); | |
| 244 DCHECK_GT(result.touchesLength, 0U); | |
| 245 | |
| 246 const float px_to_dp = 1.f / device_scale_factor; | |
| 247 for (size_t i = 0; i < result.touchesLength; ++i) | |
| 248 result.touches[i] = BuildWebTouchPoint(event, i, px_to_dp); | |
| 249 | |
| 250 return result; | |
| 251 } | 140 } |
| 252 | 141 |
| 253 } // namespace content | 142 } // namespace content |
| OLD | NEW |