1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230

content / browser / renderer_host / input / motion_event_web.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/input/motion_event_web.h"

#include <numbers>

#include "base/check_op.h"
#include "base/notreached.h"
#include "base/numerics/angle_conversions.h"
#include "components/input/web_touch_event_traits.h"
#include "ui/events/blink/blink_event_util.h"

using blink::WebInputEvent;
using blink::WebPointerProperties;
using blink::WebTouchEvent;
using blink::WebTouchPoint;
using input::WebTouchEventTraits;

namespace content {
namespace {

ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) {
  DCHECK(event.touches_length);
  switch (event.GetType()) {
    case WebInputEvent::Type::kTouchStart:
      if (WebTouchEventTraits::AllTouchPointsHaveState(
              event, WebTouchPoint::State::kStatePressed))
        return ui::MotionEvent::Action::DOWN;
      else
        return ui::MotionEvent::Action::POINTER_DOWN;
    case WebInputEvent::Type::kTouchEnd:
      if (WebTouchEventTraits::AllTouchPointsHaveState(
              event, WebTouchPoint::State::kStateReleased))
        return ui::MotionEvent::Action::UP;
      else
        return ui::MotionEvent::Action::POINTER_UP;
    case WebInputEvent::Type::kTouchCancel:
      DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
          event, WebTouchPoint::State::kStateCancelled));
      return ui::MotionEvent::Action::CANCEL;
    case WebInputEvent::Type::kTouchMove:
      return ui::MotionEvent::Action::MOVE;
    default:
      break;
  };
  NOTREACHED_IN_MIGRATION()
      << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
  return ui::MotionEvent::Action::CANCEL;
}

int GetActionIndexFrom(const WebTouchEvent& event) {
  for (size_t i = 0; i < event.touches_length; ++i) {
    if (event.touches[i].state != WebTouchPoint::State::kStateUndefined &&
        event.touches[i].state != WebTouchPoint::State::kStateStationary)
      return i;
  }
  return -1;
}

}  // namespace

MotionEventWeb::MotionEventWeb(const WebTouchEvent& event)
    : event_(event),
      cached_action_(GetActionFrom(event)),
      cached_action_index_(GetActionIndexFrom(event)),
      unique_event_id_(event.unique_touch_event_id) {
  DCHECK_GT(GetPointerCount(), 0U);
}

MotionEventWeb::~MotionEventWeb() {}

uint32_t MotionEventWeb::GetUniqueEventId() const {
  return unique_event_id_;
}

MotionEventWeb::Action MotionEventWeb::GetAction() const {
  return cached_action_;
}

int MotionEventWeb::GetActionIndex() const {
  DCHECK(cached_action_ == Action::POINTER_UP ||
         cached_action_ == Action::POINTER_DOWN)
      << "Invalid action for GetActionIndex(): " << cached_action_;
  DCHECK_GE(cached_action_index_, 0);
  DCHECK_LT(cached_action_index_, static_cast<int>(event_.touches_length));
  return cached_action_index_;
}

size_t MotionEventWeb::GetPointerCount() const {
  return event_.touches_length;
}

int MotionEventWeb::GetPointerId(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return event_.touches[pointer_index].id;
}

float MotionEventWeb::GetX(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return event_.touches[pointer_index].PositionInWidget().x();
}

float MotionEventWeb::GetY(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return event_.touches[pointer_index].PositionInWidget().y();
}

float MotionEventWeb::GetRawX(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return event_.touches[pointer_index].PositionInScreen().x();
}

float MotionEventWeb::GetRawY(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return event_.touches[pointer_index].PositionInScreen().y();
}

float MotionEventWeb::GetTouchMajor(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return 2.f * std::max(event_.touches[pointer_index].radius_x,
                        event_.touches[pointer_index].radius_y);
}

float MotionEventWeb::GetTouchMinor(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());
  return 2.f * std::min(event_.touches[pointer_index].radius_x,
                        event_.touches[pointer_index].radius_y);
}

float MotionEventWeb::GetOrientation(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());

  float orientation_rad =
      base::DegToRad(event_.touches[pointer_index].rotation_angle);
  DCHECK(0 <= orientation_rad &&
         orientation_rad <= std::numbers::pi_v<float> / 2)
      << "Unexpected touch rotation angle";

  if (GetToolType(pointer_index) == ToolType::STYLUS) {
    const WebPointerProperties& pointer = event_.touches[pointer_index];

    if (pointer.tilt_y <= 0 && pointer.tilt_x < 0) {
      // Stylus is tilted to the left away from the user or straight
      // to the left thus the orientation should be within [pi/2,pi).
      orientation_rad += std::numbers::pi_v<float> / 2;
    } else if (pointer.tilt_y < 0 && pointer.tilt_x >= 0) {
      // Stylus is tilted to the right away from the user or straight away
      // from the user thus the orientation should be within [-pi,-pi/2).
      orientation_rad -= std::numbers::pi_v<float>;
    } else if (pointer.tilt_y >= 0 && pointer.tilt_x > 0) {
      // Stylus is tilted to the right towards the user or straight
      // to the right thus the orientation should be within [-pi/2,0).
      orientation_rad -= std::numbers::pi_v<float> / 2;
    }
  } else if (event_.touches[pointer_index].radius_x >
             event_.touches[pointer_index].radius_y) {
    // The case radiusX == radiusY is omitted from here on purpose: for circles,
    // we want to pass the angle (which could be any value in such cases but
    // always seems to be set to zero) unchanged.
    orientation_rad -= std::numbers::pi_v<float> / 2;
  }

  return orientation_rad;
}

float MotionEventWeb::GetPressure(size_t pointer_index) const {
  return 0.f;
}

float MotionEventWeb::GetTiltX(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());

  return event_.touches[pointer_index].tilt_x;
}

float MotionEventWeb::GetTiltY(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());

  return event_.touches[pointer_index].tilt_y;
}

float MotionEventWeb::GetTwist(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());

  return event_.touches[pointer_index].twist;
}

float MotionEventWeb::GetTangentialPressure(size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());

  return event_.touches[pointer_index].tangential_pressure;
}

base::TimeTicks MotionEventWeb::GetEventTime() const {
  return event_.TimeStamp();
}

ui::MotionEvent::ToolType MotionEventWeb::GetToolType(
    size_t pointer_index) const {
  DCHECK_LT(pointer_index, GetPointerCount());

  const WebPointerProperties& pointer = event_.touches[pointer_index];

  switch (pointer.pointer_type) {
    case WebPointerProperties::PointerType::kUnknown:
      return ToolType::UNKNOWN;
    case WebPointerProperties::PointerType::kMouse:
      return ToolType::MOUSE;
    case WebPointerProperties::PointerType::kPen:
      return ToolType::STYLUS;
    case WebPointerProperties::PointerType::kEraser:
      return ToolType::ERASER;
    case WebPointerProperties::PointerType::kTouch:
      return ToolType::FINGER;
  }
  NOTREACHED_IN_MIGRATION() << "Unexpected pointerType";
  return ToolType::UNKNOWN;
}

int MotionEventWeb::GetButtonState() const {
  return 0;
}

int MotionEventWeb::GetFlags() const {
  return ui::WebEventModifiersToEventFlags(event_.GetModifiers());
}

}  // namespace content