1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// MSVC++ requires this to be set before any other includes to get M_PI. 61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define _USE_MATH_DEFINES 71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/renderer_host/input/motion_event_web.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <cmath> 111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/logging.h" 131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/browser/renderer_host/input/web_input_event_util.h" 145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/common/input/web_touch_event_traits.h" 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using blink::WebInputEvent; 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using blink::WebTouchEvent; 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using blink::WebTouchPoint; 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content { 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace { 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) { 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK(event.touchesLength); 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) switch (event.type) { 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case WebInputEvent::TouchStart: 275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (WebTouchEventTraits::AllTouchPointsHaveState( 285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu event, WebTouchPoint::StatePressed)) 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_DOWN; 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) else 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_POINTER_DOWN; 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case WebInputEvent::TouchEnd: 335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (WebTouchEventTraits::AllTouchPointsHaveState( 345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu event, WebTouchPoint::StateReleased)) 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_UP; 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) else 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_POINTER_UP; 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case WebInputEvent::TouchCancel: 395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu DCHECK(WebTouchEventTraits::AllTouchPointsHaveState( 405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu event, WebTouchPoint::StateCancelled)); 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_CANCEL; 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case WebInputEvent::TouchMove: 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_MOVE; 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) default: 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) break; 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) }; 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) NOTREACHED() 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent."; 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ui::MotionEvent::ACTION_CANCEL; 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int GetActionIndexFrom(const WebTouchEvent& event) { 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (size_t i = 0; i < event.touchesLength; ++i) { 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (event.touches[i].state != WebTouchPoint::StateUndefined && 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) event.touches[i].state != WebTouchPoint::StateStationary) 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return i; 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return -1; 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)MotionEventWeb::MotionEventWeb(const WebTouchEvent& event) 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) : event_(event), 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) cached_action_(GetActionFrom(event)), 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) cached_action_index_(GetActionIndexFrom(event)) { 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK_GT(GetPointerCount(), 0U); 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)MotionEventWeb::~MotionEventWeb() {} 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 72c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochint MotionEventWeb::GetId() const { 73c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch return 0; 74c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch} 75c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)MotionEventWeb::Action MotionEventWeb::GetAction() const { 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return cached_action_; 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int MotionEventWeb::GetActionIndex() const { return cached_action_index_; } 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; } 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int MotionEventWeb::GetPointerId(size_t pointer_index) const { 85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return event_.touches[pointer_index].id; 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)float MotionEventWeb::GetX(size_t pointer_index) const { 90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return event_.touches[pointer_index].position.x; 92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)float MotionEventWeb::GetY(size_t pointer_index) const { 95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return event_.touches[pointer_index].position.y; 97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)float MotionEventWeb::GetRawX(size_t pointer_index) const { 100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return event_.touches[pointer_index].screenPosition.x; 102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)} 103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)float MotionEventWeb::GetRawY(size_t pointer_index) const { 105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 106f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) return event_.touches[pointer_index].screenPosition.y; 107f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)} 108f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)float MotionEventWeb::GetTouchMajor(size_t pointer_index) const { 110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 2.f * std::max(event_.touches[pointer_index].radiusX, 112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) event_.touches[pointer_index].radiusY); 113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccifloat MotionEventWeb::GetTouchMinor(size_t pointer_index) const { 1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DCHECK_LT(pointer_index, GetPointerCount()); 1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return 2.f * std::min(event_.touches[pointer_index].radiusX, 1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci event_.touches[pointer_index].radiusY); 1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccifloat MotionEventWeb::GetOrientation(size_t pointer_index) const { 1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DCHECK_LT(pointer_index, GetPointerCount()); 1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci float rotation_angle_rad = event_.touches[pointer_index].rotationAngle 1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * M_PI / 180.f; 1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DCHECK(0 <= rotation_angle_rad && rotation_angle_rad <= M_PI_2) 1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci << "Unexpected touch rotation angle"; 1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (event_.touches[pointer_index].radiusX 1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci > event_.touches[pointer_index].radiusY) { 1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // The case radiusX == radiusY is omitted from here on purpose: for circles, 1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // we want to pass the angle (which could be any value in such cases but 1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // always seem to be set to zero) unchanged. 1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci rotation_angle_rad -= (float) M_PI_2; 1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci } 1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return rotation_angle_rad; 1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)float MotionEventWeb::GetPressure(size_t pointer_index) const { 141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 0.f; 142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)base::TimeTicks MotionEventWeb::GetEventTime() const { 145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return base::TimeTicks() + 146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::TimeDelta::FromMicroseconds(event_.timeStampSeconds * 147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Time::kMicrosecondsPerSecond); 148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 150116680a4aac90f2aa7413d9095a592090648e557Ben Murdochui::MotionEvent::ToolType MotionEventWeb::GetToolType( 151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch size_t pointer_index) const { 15203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // TODO(jdduke): Plumb tool type from the platform event, crbug.com/404128. 15303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) DCHECK_LT(pointer_index, GetPointerCount()); 154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return TOOL_TYPE_UNKNOWN; 155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 157116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint MotionEventWeb::GetButtonState() const { 158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NOTIMPLEMENTED(); 159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return 0; 160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciint MotionEventWeb::GetFlags() const { 1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return WebEventModifiersToEventFlags(event_.modifiers); 1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)scoped_ptr<ui::MotionEvent> MotionEventWeb::Clone() const { 167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return scoped_ptr<MotionEvent>(new MotionEventWeb(event_)); 168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)scoped_ptr<ui::MotionEvent> MotionEventWeb::Cancel() const { 171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) WebTouchEvent cancel_event(event_); 1725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu WebTouchEventTraits::ResetTypeAndTouchStates( 1735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu blink::WebInputEvent::TouchCancel, 1745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // TODO(rbyers): Shouldn't we use a fresh timestamp? 1755c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu event_.timeStampSeconds, 1765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu &cancel_event); 177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return scoped_ptr<MotionEvent>(new MotionEventWeb(cancel_event)); 178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace content 181