event.cc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. // Copyright (c) 2012 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. #include "ui/events/event.h"
  5. #include <cmath>
  6. #include <cstring>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "base/logging.h"
  11. #include "base/memory/ptr_util.h"
  12. #include "base/metrics/histogram.h"
  13. #include "base/metrics/histogram_macros.h"
  14. #include "base/notreached.h"
  15. #include "base/numerics/safe_conversions.h"
  16. #include "base/strings/strcat.h"
  17. #include "base/strings/string_number_conversions.h"
  18. #include "base/strings/string_util.h"
  19. #include "base/strings/stringprintf.h"
  20. #include "build/build_config.h"
  21. #include "ui/events/base_event_utils.h"
  22. #include "ui/events/event_utils.h"
  23. #include "ui/events/keycodes/dom/dom_code.h"
  24. #include "ui/events/keycodes/dom/dom_key.h"
  25. #include "ui/events/keycodes/dom/keycode_converter.h"
  26. #include "ui/events/keycodes/keyboard_code_conversion.h"
  27. #include "ui/gfx/geometry/point3_f.h"
  28. #include "ui/gfx/geometry/point_conversions.h"
  29. #include "ui/gfx/geometry/transform.h"
  30. #include "ui/gfx/geometry/transform_util.h"
  31. #if defined(USE_OZONE)
  32. #include "ui/base/ui_base_features.h" // nogncheck
  33. #include "ui/events/ozone/layout/keyboard_layout_engine.h" // nogncheck
  34. #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h" // nogncheck
  35. #endif
  36. #if BUILDFLAG(IS_WIN)
  37. #include "ui/events/keycodes/platform_key_map_win.h"
  38. #endif
  39. namespace ui {
  40. namespace {
  41. constexpr int kChangedButtonFlagMask =
  42. EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON |
  43. EF_BACK_MOUSE_BUTTON | EF_FORWARD_MOUSE_BUTTON;
  44. SourceEventType EventTypeToLatencySourceEventType(EventType type) {
  45. switch (type) {
  46. case ET_UNKNOWN:
  47. // SourceEventType for GestureEvents can be either TOUCH or WHEEL. The
  48. // proper value is assigned in the constructors.
  49. case ET_GESTURE_SCROLL_BEGIN:
  50. case ET_GESTURE_SCROLL_END:
  51. case ET_GESTURE_SCROLL_UPDATE:
  52. case ET_GESTURE_TAP:
  53. case ET_GESTURE_TAP_DOWN:
  54. case ET_GESTURE_TAP_CANCEL:
  55. case ET_GESTURE_TAP_UNCONFIRMED:
  56. case ET_GESTURE_DOUBLE_TAP:
  57. case ET_GESTURE_BEGIN:
  58. case ET_GESTURE_END:
  59. case ET_GESTURE_TWO_FINGER_TAP:
  60. case ET_GESTURE_PINCH_BEGIN:
  61. case ET_GESTURE_PINCH_END:
  62. case ET_GESTURE_PINCH_UPDATE:
  63. case ET_GESTURE_SHORT_PRESS:
  64. case ET_GESTURE_LONG_PRESS:
  65. case ET_GESTURE_LONG_TAP:
  66. case ET_GESTURE_SWIPE:
  67. case ET_GESTURE_SHOW_PRESS:
  68. // Flings can be GestureEvents too.
  69. case ET_SCROLL_FLING_START:
  70. case ET_SCROLL_FLING_CANCEL:
  71. return SourceEventType::UNKNOWN;
  72. case ET_KEY_PRESSED:
  73. return SourceEventType::KEY_PRESS;
  74. case ET_MOUSE_PRESSED:
  75. case ET_MOUSE_DRAGGED:
  76. case ET_MOUSE_RELEASED:
  77. case ET_MOUSE_MOVED:
  78. case ET_MOUSE_ENTERED:
  79. case ET_MOUSE_EXITED:
  80. // We measure latency for key presses, not key releases. Most behavior is
  81. // keyed off of presses, and release latency is higher than press latency as
  82. // it's impacted by event handling of the press event.
  83. case ET_KEY_RELEASED:
  84. case ET_MOUSE_CAPTURE_CHANGED:
  85. case ET_DROP_TARGET_EVENT:
  86. case ET_CANCEL_MODE:
  87. case ET_UMA_DATA:
  88. return SourceEventType::OTHER;
  89. case ET_TOUCH_RELEASED:
  90. case ET_TOUCH_PRESSED:
  91. case ET_TOUCH_MOVED:
  92. case ET_TOUCH_CANCELLED:
  93. return SourceEventType::TOUCH;
  94. case ET_MOUSEWHEEL:
  95. case ET_SCROLL:
  96. return SourceEventType::WHEEL;
  97. case ET_LAST:
  98. NOTREACHED();
  99. return SourceEventType::UNKNOWN;
  100. }
  101. NOTREACHED();
  102. return SourceEventType::UNKNOWN;
  103. }
  104. std::string MomentumPhaseToString(EventMomentumPhase phase) {
  105. switch (phase) {
  106. case EventMomentumPhase::NONE:
  107. return "NONE";
  108. case EventMomentumPhase::BEGAN:
  109. return "BEGAN";
  110. case EventMomentumPhase::MAY_BEGIN:
  111. return "MAY_BEGIN";
  112. case EventMomentumPhase::INERTIAL_UPDATE:
  113. return "INERTIAL_UPDATE";
  114. case EventMomentumPhase::END:
  115. return "END";
  116. case EventMomentumPhase::BLOCKED:
  117. return "BLOCKED";
  118. }
  119. }
  120. std::string ScrollEventPhaseToString(ScrollEventPhase phase) {
  121. switch (phase) {
  122. case ScrollEventPhase::kNone:
  123. return "kEnd";
  124. case ScrollEventPhase::kBegan:
  125. return "kBegan";
  126. case ScrollEventPhase::kUpdate:
  127. return "kUpdate";
  128. case ScrollEventPhase::kEnd:
  129. return "kEnd";
  130. }
  131. }
  132. #if defined(USE_OZONE)
  133. uint32_t ScanCodeFromNative(const PlatformEvent& native_event) {
  134. const KeyEvent* event = static_cast<const KeyEvent*>(native_event);
  135. DCHECK(event->IsKeyEvent());
  136. return event->scan_code();
  137. }
  138. #endif // defined(USE_OZONE)
  139. bool IsNearZero(const float num) {
  140. // Epsilon of 1e-10 at 0.
  141. return (std::fabs(num) < 1e-10);
  142. }
  143. } // namespace
  144. ////////////////////////////////////////////////////////////////////////////////
  145. // Event
  146. Event::~Event() {
  147. if (delete_native_event_)
  148. ReleaseCopiedNativeEvent(native_event_);
  149. }
  150. void Event::SetNativeEvent(const PlatformEvent& event) {
  151. if (delete_native_event_)
  152. ReleaseCopiedNativeEvent(native_event_);
  153. native_event_ = CopyNativeEvent(event);
  154. delete_native_event_ = true;
  155. }
  156. const char* Event::GetName() const {
  157. return EventTypeName(type_).data();
  158. }
  159. void Event::SetProperties(const Properties& properties) {
  160. properties_ = std::make_unique<Properties>(properties);
  161. }
  162. CancelModeEvent* Event::AsCancelModeEvent() {
  163. CHECK(IsCancelModeEvent());
  164. return static_cast<CancelModeEvent*>(this);
  165. }
  166. const CancelModeEvent* Event::AsCancelModeEvent() const {
  167. CHECK(IsCancelModeEvent());
  168. return static_cast<const CancelModeEvent*>(this);
  169. }
  170. GestureEvent* Event::AsGestureEvent() {
  171. CHECK(IsGestureEvent());
  172. return static_cast<GestureEvent*>(this);
  173. }
  174. const GestureEvent* Event::AsGestureEvent() const {
  175. CHECK(IsGestureEvent());
  176. return static_cast<const GestureEvent*>(this);
  177. }
  178. KeyEvent* Event::AsKeyEvent() {
  179. CHECK(IsKeyEvent());
  180. return static_cast<KeyEvent*>(this);
  181. }
  182. const KeyEvent* Event::AsKeyEvent() const {
  183. CHECK(IsKeyEvent());
  184. return static_cast<const KeyEvent*>(this);
  185. }
  186. LocatedEvent* Event::AsLocatedEvent() {
  187. CHECK(IsLocatedEvent());
  188. return static_cast<LocatedEvent*>(this);
  189. }
  190. const LocatedEvent* Event::AsLocatedEvent() const {
  191. CHECK(IsLocatedEvent());
  192. return static_cast<const LocatedEvent*>(this);
  193. }
  194. MouseEvent* Event::AsMouseEvent() {
  195. CHECK(IsMouseEvent());
  196. return static_cast<MouseEvent*>(this);
  197. }
  198. const MouseEvent* Event::AsMouseEvent() const {
  199. CHECK(IsMouseEvent());
  200. return static_cast<const MouseEvent*>(this);
  201. }
  202. MouseWheelEvent* Event::AsMouseWheelEvent() {
  203. CHECK(IsMouseWheelEvent());
  204. return static_cast<MouseWheelEvent*>(this);
  205. }
  206. const MouseWheelEvent* Event::AsMouseWheelEvent() const {
  207. CHECK(IsMouseWheelEvent());
  208. return static_cast<const MouseWheelEvent*>(this);
  209. }
  210. ScrollEvent* Event::AsScrollEvent() {
  211. CHECK(IsScrollEvent());
  212. return static_cast<ScrollEvent*>(this);
  213. }
  214. const ScrollEvent* Event::AsScrollEvent() const {
  215. CHECK(IsScrollEvent());
  216. return static_cast<const ScrollEvent*>(this);
  217. }
  218. TouchEvent* Event::AsTouchEvent() {
  219. CHECK(IsTouchEvent());
  220. return static_cast<TouchEvent*>(this);
  221. }
  222. const TouchEvent* Event::AsTouchEvent() const {
  223. CHECK(IsTouchEvent());
  224. return static_cast<const TouchEvent*>(this);
  225. }
  226. bool Event::HasNativeEvent() const {
  227. PlatformEvent null_event;
  228. std::memset(&null_event, 0, sizeof(null_event));
  229. return !!std::memcmp(&native_event_, &null_event, sizeof(null_event));
  230. }
  231. void Event::StopPropagation() {
  232. // TODO(sad): Re-enable these checks once View uses dispatcher to dispatch
  233. // events.
  234. // CHECK(phase_ != EP_PREDISPATCH && phase_ != EP_POSTDISPATCH);
  235. CHECK(cancelable_);
  236. result_ = static_cast<EventResult>(result_ | ER_CONSUMED);
  237. }
  238. void Event::SetHandled() {
  239. // TODO(sad): Re-enable these checks once View uses dispatcher to dispatch
  240. // events.
  241. // CHECK(phase_ != EP_PREDISPATCH && phase_ != EP_POSTDISPATCH);
  242. CHECK(cancelable_);
  243. result_ = static_cast<EventResult>(result_ | ER_HANDLED);
  244. }
  245. std::string Event::ToString() const {
  246. return base::StrCat(
  247. {GetName(), " time_stamp ",
  248. base::NumberToString(time_stamp_.since_origin().InSecondsF())});
  249. }
  250. Event::Event(EventType type, base::TimeTicks time_stamp, int flags)
  251. : type_(type),
  252. time_stamp_(time_stamp.is_null() ? EventTimeForNow() : time_stamp),
  253. flags_(flags),
  254. native_event_(PlatformEvent()) {
  255. if (type_ < ET_LAST)
  256. latency()->set_source_event_type(EventTypeToLatencySourceEventType(type));
  257. }
  258. Event::Event(const PlatformEvent& native_event, EventType type, int flags)
  259. : type_(type),
  260. time_stamp_(EventTimeFromNative(native_event)),
  261. flags_(flags),
  262. native_event_(native_event) {
  263. if (type_ < ET_LAST)
  264. latency()->set_source_event_type(EventTypeToLatencySourceEventType(type));
  265. ComputeEventLatencyOS(native_event);
  266. #if defined(USE_OZONE)
  267. source_device_id_ = native_event->source_device_id();
  268. if (auto* properties = native_event->properties())
  269. properties_ = std::make_unique<Properties>(*properties);
  270. #endif
  271. }
  272. Event::Event(const Event& copy)
  273. : type_(copy.type_),
  274. time_stamp_(copy.time_stamp_),
  275. latency_(copy.latency_),
  276. flags_(copy.flags_),
  277. native_event_(CopyNativeEvent(copy.native_event_)),
  278. delete_native_event_(true),
  279. source_device_id_(copy.source_device_id_),
  280. properties_(copy.properties_
  281. ? std::make_unique<Properties>(*copy.properties_)
  282. : nullptr) {}
  283. Event& Event::operator=(const Event& rhs) {
  284. if (this != &rhs) {
  285. if (delete_native_event_)
  286. ReleaseCopiedNativeEvent(native_event_);
  287. type_ = rhs.type_;
  288. time_stamp_ = rhs.time_stamp_;
  289. latency_ = rhs.latency_;
  290. flags_ = rhs.flags_;
  291. native_event_ = CopyNativeEvent(rhs.native_event_);
  292. delete_native_event_ = true;
  293. cancelable_ = rhs.cancelable_;
  294. phase_ = rhs.phase_;
  295. result_ = rhs.result_;
  296. source_device_id_ = rhs.source_device_id_;
  297. if (rhs.properties_)
  298. properties_ = std::make_unique<Properties>(*rhs.properties_);
  299. else
  300. properties_.reset();
  301. }
  302. latency_.set_source_event_type(SourceEventType::OTHER);
  303. return *this;
  304. }
  305. void Event::SetType(EventType type) {
  306. type_ = type;
  307. if (type_ < ET_LAST)
  308. latency()->set_source_event_type(EventTypeToLatencySourceEventType(type));
  309. }
  310. ////////////////////////////////////////////////////////////////////////////////
  311. // CancelModeEvent
  312. CancelModeEvent::CancelModeEvent()
  313. : Event(ET_CANCEL_MODE, base::TimeTicks(), 0) {
  314. set_cancelable(false);
  315. }
  316. CancelModeEvent::~CancelModeEvent() = default;
  317. std::unique_ptr<Event> CancelModeEvent::Clone() const {
  318. return std::make_unique<CancelModeEvent>(*this);
  319. }
  320. ////////////////////////////////////////////////////////////////////////////////
  321. // LocatedEvent
  322. LocatedEvent::~LocatedEvent() = default;
  323. LocatedEvent::LocatedEvent(const PlatformEvent& native_event)
  324. : Event(native_event,
  325. EventTypeFromNative(native_event),
  326. EventFlagsFromNative(native_event)),
  327. location_(EventLocationFromNative(native_event)),
  328. root_location_(location_) {}
  329. LocatedEvent::LocatedEvent(EventType type,
  330. const gfx::PointF& location,
  331. const gfx::PointF& root_location,
  332. base::TimeTicks time_stamp,
  333. int flags)
  334. : Event(type, time_stamp, flags),
  335. location_(location),
  336. root_location_(root_location) {}
  337. LocatedEvent::LocatedEvent(const LocatedEvent& copy) = default;
  338. void LocatedEvent::UpdateForRootTransform(
  339. const gfx::Transform& reversed_root_transform,
  340. const gfx::Transform& reversed_local_transform) {
  341. if (target()) {
  342. gfx::Point3F transformed_location(location_);
  343. reversed_local_transform.TransformPoint(&transformed_location);
  344. location_ = transformed_location.AsPointF();
  345. gfx::Point3F transformed_root_location(root_location_);
  346. reversed_root_transform.TransformPoint(&transformed_root_location);
  347. root_location_ = transformed_root_location.AsPointF();
  348. } else {
  349. // This mirrors what the code previously did.
  350. gfx::Point3F transformed_location(location_);
  351. reversed_root_transform.TransformPoint(&transformed_location);
  352. root_location_ = location_ = transformed_location.AsPointF();
  353. }
  354. }
  355. std::string LocatedEvent::ToString() const {
  356. return base::StrCat({Event::ToString(), " location ", location_.ToString(),
  357. " root_location ", root_location_.ToString()});
  358. }
  359. ////////////////////////////////////////////////////////////////////////////////
  360. // MouseEvent
  361. MouseEvent::MouseEvent(const PlatformEvent& native_event)
  362. : LocatedEvent(native_event),
  363. changed_button_flags_(GetChangedMouseButtonFlagsFromNative(native_event)),
  364. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  365. movement_(GetMouseMovementFromNative(native_event)),
  366. #endif
  367. pointer_details_(GetMousePointerDetailsFromNative(native_event)) {
  368. latency()->set_source_event_type(SourceEventType::MOUSE);
  369. latency()->AddLatencyNumberWithTimestamp(
  370. INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, time_stamp());
  371. latency()->AddLatencyNumber(INPUT_EVENT_LATENCY_UI_COMPONENT);
  372. InitializeNative();
  373. }
  374. MouseEvent::MouseEvent(EventType type,
  375. const gfx::PointF& location,
  376. const gfx::PointF& root_location,
  377. base::TimeTicks time_stamp,
  378. int flags,
  379. int changed_button_flags,
  380. const PointerDetails& pointer_details)
  381. : LocatedEvent(type, location, root_location, time_stamp, flags),
  382. changed_button_flags_(changed_button_flags),
  383. pointer_details_(pointer_details) {
  384. DCHECK_NE(ET_MOUSEWHEEL, type);
  385. DCHECK_EQ(changed_button_flags_,
  386. changed_button_flags_ & kChangedButtonFlagMask);
  387. latency()->set_source_event_type(SourceEventType::MOUSE);
  388. latency()->AddLatencyNumber(INPUT_EVENT_LATENCY_UI_COMPONENT);
  389. if (this->type() == ET_MOUSE_MOVED && IsAnyButton())
  390. SetType(ET_MOUSE_DRAGGED);
  391. }
  392. MouseEvent::MouseEvent(EventType type,
  393. const gfx::Point& location,
  394. const gfx::Point& root_location,
  395. base::TimeTicks time_stamp,
  396. int flags,
  397. int changed_button_flags,
  398. const PointerDetails& pointer_details)
  399. : MouseEvent(type,
  400. gfx::PointF(location),
  401. gfx::PointF(root_location),
  402. time_stamp,
  403. flags,
  404. changed_button_flags,
  405. pointer_details) {}
  406. MouseEvent::MouseEvent(const MouseEvent& other) = default;
  407. MouseEvent::~MouseEvent() = default;
  408. void MouseEvent::InitializeNative() {
  409. if (type() == ET_MOUSE_PRESSED || type() == ET_MOUSE_RELEASED) {
  410. SetClickCount(GetRepeatCount(*this));
  411. }
  412. }
  413. // static
  414. bool MouseEvent::IsRepeatedClickEvent(const MouseEvent& event1,
  415. const MouseEvent& event2) {
  416. // These values match the Windows defaults.
  417. static const int kDoubleClickTimeMS = 500;
  418. static const int kDoubleClickWidth = 4;
  419. static const int kDoubleClickHeight = 4;
  420. if (event1.type() != ET_MOUSE_PRESSED || event2.type() != ET_MOUSE_PRESSED)
  421. return false;
  422. // Compare flags, but ignore EF_IS_DOUBLE_CLICK to allow triple clicks.
  423. if ((event1.flags() & ~EF_IS_DOUBLE_CLICK) !=
  424. (event2.flags() & ~EF_IS_DOUBLE_CLICK))
  425. return false;
  426. // The new event has been created from the same native event.
  427. if (event1.time_stamp() == event2.time_stamp())
  428. return false;
  429. base::TimeDelta time_difference = event2.time_stamp() - event1.time_stamp();
  430. if (time_difference.InMilliseconds() > kDoubleClickTimeMS)
  431. return false;
  432. if (std::abs(event2.x() - event1.x()) > kDoubleClickWidth / 2)
  433. return false;
  434. if (std::abs(event2.y() - event1.y()) > kDoubleClickHeight / 2)
  435. return false;
  436. return true;
  437. }
  438. // static
  439. int MouseEvent::GetRepeatCount(const MouseEvent& event) {
  440. int click_count = 1;
  441. if (last_click_event_) {
  442. if (event.type() == ET_MOUSE_RELEASED) {
  443. if (event.changed_button_flags() ==
  444. last_click_event_->changed_button_flags()) {
  445. return last_click_event_->GetClickCount();
  446. } else {
  447. // If last_click_event_ has changed since this button was pressed
  448. // return a click count of 1.
  449. return click_count;
  450. }
  451. }
  452. // Return the prior click count and do not update |last_click_event_| when
  453. // re-processing a native event, or when proccesing a reposted event.
  454. if (event.time_stamp() == last_click_event_->time_stamp())
  455. return last_click_event_->GetClickCount();
  456. if (IsRepeatedClickEvent(*last_click_event_, event))
  457. click_count = last_click_event_->GetClickCount() + 1;
  458. delete last_click_event_;
  459. }
  460. last_click_event_ = new MouseEvent(event);
  461. if (click_count > 3)
  462. click_count = 3;
  463. last_click_event_->SetClickCount(click_count);
  464. return click_count;
  465. }
  466. void MouseEvent::ResetLastClickForTest() {
  467. if (last_click_event_) {
  468. delete last_click_event_;
  469. last_click_event_ = nullptr;
  470. }
  471. }
  472. // static
  473. MouseEvent* MouseEvent::last_click_event_ = nullptr;
  474. int MouseEvent::GetClickCount() const {
  475. if (type() != ET_MOUSE_PRESSED && type() != ET_MOUSE_RELEASED)
  476. return 0;
  477. if (flags() & EF_IS_TRIPLE_CLICK)
  478. return 3;
  479. else if (flags() & EF_IS_DOUBLE_CLICK)
  480. return 2;
  481. else
  482. return 1;
  483. }
  484. void MouseEvent::SetClickCount(int click_count) {
  485. if (type() != ET_MOUSE_PRESSED && type() != ET_MOUSE_RELEASED)
  486. return;
  487. DCHECK_LT(0, click_count);
  488. DCHECK_GE(3, click_count);
  489. int f = flags();
  490. switch (click_count) {
  491. case 1:
  492. f &= ~EF_IS_DOUBLE_CLICK;
  493. f &= ~EF_IS_TRIPLE_CLICK;
  494. break;
  495. case 2:
  496. f |= EF_IS_DOUBLE_CLICK;
  497. f &= ~EF_IS_TRIPLE_CLICK;
  498. break;
  499. case 3:
  500. f &= ~EF_IS_DOUBLE_CLICK;
  501. f |= EF_IS_TRIPLE_CLICK;
  502. break;
  503. }
  504. set_flags(f);
  505. }
  506. std::string MouseEvent::ToString() const {
  507. return base::StrCat(
  508. {LocatedEvent::ToString(), " flags ",
  509. base::JoinString(base::make_span(MouseEventFlagsNames(flags())),
  510. " | ")});
  511. }
  512. std::unique_ptr<Event> MouseEvent::Clone() const {
  513. return std::make_unique<MouseEvent>(*this);
  514. }
  515. ////////////////////////////////////////////////////////////////////////////////
  516. // MouseWheelEvent
  517. MouseWheelEvent::MouseWheelEvent(const PlatformEvent& native_event)
  518. : MouseEvent(native_event),
  519. offset_(GetMouseWheelOffset(native_event)),
  520. tick_120ths_(GetMouseWheelTick120ths(native_event)) {}
  521. MouseWheelEvent::MouseWheelEvent(const ScrollEvent& scroll_event)
  522. : MouseEvent(scroll_event),
  523. offset_(base::ClampRound(scroll_event.x_offset()),
  524. base::ClampRound(scroll_event.y_offset())) {
  525. SetType(ET_MOUSEWHEEL);
  526. }
  527. MouseWheelEvent::MouseWheelEvent(const MouseEvent& mouse_event,
  528. int x_offset,
  529. int y_offset)
  530. : MouseEvent(mouse_event), offset_(x_offset, y_offset) {
  531. SetType(ET_MOUSEWHEEL);
  532. }
  533. MouseWheelEvent::MouseWheelEvent(const MouseWheelEvent& mouse_wheel_event)
  534. : MouseEvent(mouse_wheel_event),
  535. offset_(mouse_wheel_event.offset()),
  536. tick_120ths_(mouse_wheel_event.tick_120ths()) {
  537. DCHECK_EQ(ET_MOUSEWHEEL, type());
  538. }
  539. MouseWheelEvent::MouseWheelEvent(
  540. const gfx::Vector2d& offset,
  541. const gfx::PointF& location,
  542. const gfx::PointF& root_location,
  543. base::TimeTicks time_stamp,
  544. int flags,
  545. int changed_button_flags,
  546. const absl::optional<gfx::Vector2d> tick_120ths)
  547. : MouseEvent(ET_UNKNOWN,
  548. location,
  549. root_location,
  550. time_stamp,
  551. flags,
  552. changed_button_flags),
  553. offset_(offset) {
  554. // Set event type to ET_UNKNOWN initially in MouseEvent() to pass the
  555. // DCHECK for type to enforce that we use MouseWheelEvent() to create
  556. // a MouseWheelEvent.
  557. SetType(ET_MOUSEWHEEL);
  558. if (!tick_120ths) {
  559. // Since no wheel ticks have been specified, assume that scrolling is linear
  560. // (not accelerated, like it is on Chrome OS).
  561. tick_120ths_ =
  562. gfx::Vector2d(offset_.x() / MouseWheelEvent::kWheelDelta * 120,
  563. offset_.y() / MouseWheelEvent::kWheelDelta * 120);
  564. } else {
  565. tick_120ths_ = tick_120ths.value();
  566. }
  567. }
  568. MouseWheelEvent::MouseWheelEvent(const gfx::Vector2d& offset,
  569. const gfx::Point& location,
  570. const gfx::Point& root_location,
  571. base::TimeTicks time_stamp,
  572. int flags,
  573. int changed_button_flags)
  574. : MouseWheelEvent(offset,
  575. gfx::PointF(location),
  576. gfx::PointF(root_location),
  577. time_stamp,
  578. flags,
  579. changed_button_flags) {}
  580. MouseWheelEvent::~MouseWheelEvent() = default;
  581. std::unique_ptr<Event> MouseWheelEvent::Clone() const {
  582. return std::make_unique<MouseWheelEvent>(*this);
  583. }
  584. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)
  585. // This value matches Windows and Fuchsia WHEEL_DELTA.
  586. // static
  587. const int MouseWheelEvent::kWheelDelta = 120;
  588. #else
  589. // This value matches GTK+ wheel scroll amount.
  590. const int MouseWheelEvent::kWheelDelta = 53;
  591. #endif
  592. ////////////////////////////////////////////////////////////////////////////////
  593. // TouchEvent
  594. TouchEvent::TouchEvent(const PlatformEvent& native_event)
  595. : LocatedEvent(native_event),
  596. unique_event_id_(ui::GetNextTouchEventId()),
  597. pointer_details_(GetTouchPointerDetailsFromNative(native_event)) {
  598. latency()->AddLatencyNumberWithTimestamp(
  599. INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, time_stamp());
  600. latency()->AddLatencyNumber(INPUT_EVENT_LATENCY_UI_COMPONENT);
  601. }
  602. TouchEvent::TouchEvent(EventType type,
  603. const gfx::PointF& location,
  604. const gfx::PointF& root_location,
  605. base::TimeTicks time_stamp,
  606. const PointerDetails& pointer_details,
  607. int flags)
  608. : LocatedEvent(type, location, root_location, time_stamp, flags),
  609. unique_event_id_(ui::GetNextTouchEventId()),
  610. pointer_details_(pointer_details) {
  611. latency()->AddLatencyNumber(INPUT_EVENT_LATENCY_UI_COMPONENT);
  612. }
  613. TouchEvent::TouchEvent(EventType type,
  614. const gfx::Point& location,
  615. base::TimeTicks time_stamp,
  616. const PointerDetails& pointer_details,
  617. int flags)
  618. : TouchEvent(type,
  619. gfx::PointF(location),
  620. gfx::PointF(location),
  621. time_stamp,
  622. pointer_details,
  623. flags) {}
  624. TouchEvent::TouchEvent(const TouchEvent& copy)
  625. : LocatedEvent(copy),
  626. unique_event_id_(copy.unique_event_id_),
  627. may_cause_scrolling_(copy.may_cause_scrolling_),
  628. hovering_(copy.hovering_),
  629. pointer_details_(copy.pointer_details_) {
  630. // Copied events should not remove touch id mapping, as this either causes the
  631. // mapping to be lost before the initial event has finished dispatching, or
  632. // the copy to attempt to remove the mapping from a null |native_event_|.
  633. }
  634. TouchEvent::~TouchEvent() = default;
  635. void TouchEvent::UpdateForRootTransform(
  636. const gfx::Transform& inverted_root_transform,
  637. const gfx::Transform& inverted_local_transform) {
  638. LocatedEvent::UpdateForRootTransform(inverted_root_transform,
  639. inverted_local_transform);
  640. // We could create a vector and then rely on Transform::TransformVector , but
  641. // that ends up creating a 4 dimensional vector and applying a 4 dim
  642. // transform. Really what we're looking at is only in the (x,y) plane, and
  643. // given that we can run this relatively frequently we will inline execute the
  644. // matrix here.
  645. const auto& matrix = inverted_root_transform.matrix();
  646. const double new_x = fabs(pointer_details_.radius_x * matrix.rc(0, 0) +
  647. pointer_details_.radius_y * matrix.rc(0, 1));
  648. const double new_y = fabs(pointer_details_.radius_x * matrix.rc(1, 0) +
  649. pointer_details_.radius_y * matrix.rc(1, 1));
  650. pointer_details_.radius_x = new_x;
  651. pointer_details_.radius_y = new_y;
  652. // for stylus touches, tilt needs to be rotated appropriately. We don't handle
  653. // screen rotations other than 0/90/180/270, but those should be handled and
  654. // translated appropriately. Other rotations leave tilts untouched for now. We
  655. // add a small check that tilt is set at all before looking through this
  656. // section.
  657. if (!IsNearZero(pointer_details_.tilt_x) ||
  658. !IsNearZero(pointer_details_.tilt_y)) {
  659. if (IsNearZero(matrix.rc(0, 1)) && IsNearZero(matrix.rc(1, 0))) {
  660. pointer_details_.tilt_x *= std::copysign(1, matrix.rc(0, 0));
  661. pointer_details_.tilt_y *= std::copysign(1, matrix.rc(1, 1));
  662. } else if (IsNearZero(matrix.rc(0, 0)) && IsNearZero(matrix.rc(1, 1))) {
  663. double new_tilt_x =
  664. pointer_details_.tilt_y * std::copysign(1, matrix.rc(0, 1));
  665. double new_tilt_y =
  666. pointer_details_.tilt_x * std::copysign(1, matrix.rc(1, 0));
  667. pointer_details_.tilt_x = new_tilt_x;
  668. pointer_details_.tilt_y = new_tilt_y;
  669. }
  670. }
  671. }
  672. void TouchEvent::DisableSynchronousHandling() {
  673. DispatcherApi dispatcher_api(this);
  674. dispatcher_api.set_result(
  675. static_cast<EventResult>(result() | ER_DISABLE_SYNC_HANDLING));
  676. }
  677. void TouchEvent::SetPointerDetailsForTest(
  678. const PointerDetails& pointer_details) {
  679. DCHECK_EQ(pointer_details_.id, pointer_details.id);
  680. pointer_details_ = pointer_details;
  681. }
  682. float TouchEvent::ComputeRotationAngle() const {
  683. float rotation_angle = pointer_details_.twist;
  684. while (rotation_angle < 0)
  685. rotation_angle += 180.f;
  686. while (rotation_angle >= 180)
  687. rotation_angle -= 180.f;
  688. return rotation_angle;
  689. }
  690. std::unique_ptr<Event> TouchEvent::Clone() const {
  691. return std::make_unique<TouchEvent>(*this);
  692. }
  693. ////////////////////////////////////////////////////////////////////////////////
  694. // KeyEvent
  695. // static
  696. KeyEvent* KeyEvent::last_key_event_ = nullptr;
  697. #if defined(USE_OZONE)
  698. KeyEvent* KeyEvent::last_ibus_key_event_ = nullptr;
  699. #endif
  700. KeyEvent::KeyEvent(const PlatformEvent& native_event)
  701. : KeyEvent(native_event, EventFlagsFromNative(native_event)) {}
  702. KeyEvent::KeyEvent(const PlatformEvent& native_event, int event_flags)
  703. : Event(native_event, EventTypeFromNative(native_event), event_flags),
  704. key_code_(KeyboardCodeFromNative(native_event)),
  705. #if defined(USE_OZONE)
  706. scan_code_(ScanCodeFromNative(native_event)),
  707. #endif // defined(USE_OZONE)
  708. code_(CodeFromNative(native_event)),
  709. is_char_(IsCharFromNative(native_event)) {
  710. #if defined(USE_OZONE)
  711. DCHECK(native_event->IsKeyEvent());
  712. key_ = native_event->AsKeyEvent()->key_;
  713. #endif
  714. InitializeNative();
  715. }
  716. KeyEvent::KeyEvent(EventType type,
  717. KeyboardCode key_code,
  718. int flags,
  719. base::TimeTicks time_stamp)
  720. : Event(type, time_stamp, flags),
  721. key_code_(key_code),
  722. code_(UsLayoutKeyboardCodeToDomCode(key_code)) {}
  723. KeyEvent::KeyEvent(EventType type,
  724. KeyboardCode key_code,
  725. DomCode code,
  726. int flags)
  727. : Event(type, EventTimeForNow(), flags), key_code_(key_code), code_(code) {}
  728. KeyEvent::KeyEvent(EventType type,
  729. KeyboardCode key_code,
  730. DomCode code,
  731. int flags,
  732. DomKey key,
  733. base::TimeTicks time_stamp,
  734. bool is_char)
  735. : Event(type, time_stamp, flags),
  736. key_code_(key_code),
  737. code_(code),
  738. is_char_(is_char),
  739. key_(key) {}
  740. KeyEvent::KeyEvent(char16_t character,
  741. KeyboardCode key_code,
  742. DomCode code,
  743. int flags,
  744. base::TimeTicks time_stamp)
  745. : Event(ET_KEY_PRESSED, time_stamp, flags),
  746. key_code_(key_code),
  747. code_(code),
  748. is_char_(true),
  749. key_(DomKey::FromCharacter(character)) {}
  750. KeyEvent::KeyEvent(const KeyEvent& rhs)
  751. : Event(rhs),
  752. key_code_(rhs.key_code_),
  753. #if defined(USE_OZONE)
  754. scan_code_(rhs.scan_code_),
  755. #endif // defined(USE_OZONE)
  756. code_(rhs.code_),
  757. is_char_(rhs.is_char_),
  758. key_(rhs.key_) {
  759. }
  760. KeyEvent& KeyEvent::operator=(const KeyEvent& rhs) {
  761. if (this != &rhs) {
  762. Event::operator=(rhs);
  763. key_code_ = rhs.key_code_;
  764. #if defined(USE_OZONE)
  765. scan_code_ = rhs.scan_code_;
  766. #endif // defined(USE_OZONE)
  767. code_ = rhs.code_;
  768. key_ = rhs.key_;
  769. is_char_ = rhs.is_char_;
  770. }
  771. return *this;
  772. }
  773. KeyEvent::~KeyEvent() = default;
  774. // static
  775. // For compatibility, this is enabled by default.
  776. bool KeyEvent::synthesize_key_repeat_enabled_ = true;
  777. // static
  778. bool KeyEvent::IsSynthesizeKeyRepeatEnabled() {
  779. return synthesize_key_repeat_enabled_;
  780. }
  781. // static
  782. void KeyEvent::SetSynthesizeKeyRepeatEnabled(bool enabled) {
  783. synthesize_key_repeat_enabled_ = enabled;
  784. }
  785. void KeyEvent::InitializeNative() {
  786. latency()->AddLatencyNumberWithTimestamp(
  787. INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT, time_stamp());
  788. latency()->AddLatencyNumber(INPUT_EVENT_LATENCY_UI_COMPONENT);
  789. // Check if this is a key repeat. This must be called before initial flags
  790. // processing, e.g: NormalizeFlags(), to avoid issues like crbug.com/1069690.
  791. if (synthesize_key_repeat_enabled_ && IsRepeated(GetLastKeyEvent()))
  792. set_flags(flags() | EF_IS_REPEAT);
  793. #if BUILDFLAG(IS_LINUX)
  794. NormalizeFlags();
  795. #elif BUILDFLAG(IS_WIN)
  796. // Only Windows has native character events.
  797. if (is_char_) {
  798. key_ = DomKey::FromCharacter(static_cast<int32_t>(native_event().wParam));
  799. set_flags(PlatformKeyMap::ReplaceControlAndAltWithAltGraph(flags()));
  800. } else {
  801. int adjusted_flags = flags();
  802. key_ = PlatformKeyMap::DomKeyFromKeyboardCode(key_code(), &adjusted_flags);
  803. set_flags(adjusted_flags);
  804. }
  805. #endif
  806. }
  807. void KeyEvent::ApplyLayout() const {
  808. DomCode code = code_;
  809. if (code == DomCode::NONE) {
  810. // Catch old code that tries to do layout without a physical key, and try
  811. // to recover using the KeyboardCode. Once key events are fully defined
  812. // on construction (see TODO in event.h) this will go away.
  813. VLOG(2) << "DomCode::NONE keycode=" << key_code_;
  814. code = UsLayoutKeyboardCodeToDomCode(key_code_);
  815. if (code == DomCode::NONE) {
  816. key_ = DomKey::UNIDENTIFIED;
  817. return;
  818. }
  819. }
  820. if (key_ != DomKey::NONE)
  821. return;
  822. KeyboardCode dummy_key_code;
  823. #if defined(USE_OZONE)
  824. if (KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup(
  825. code, flags(), &key_, &dummy_key_code)) {
  826. return;
  827. }
  828. #endif
  829. #if !BUILDFLAG(IS_WIN)
  830. // Native Windows character events always have is_char_ == true,
  831. // so this is a synthetic or native keystroke event.
  832. // Therefore, perform only the fallback action.
  833. if (native_event()) {
  834. DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED ||
  835. EventTypeFromNative(native_event()) == ET_KEY_RELEASED);
  836. }
  837. #endif
  838. if (!DomCodeToUsLayoutDomKey(code, flags(), &key_, &dummy_key_code))
  839. key_ = DomKey::UNIDENTIFIED;
  840. }
  841. bool KeyEvent::IsRepeated(KeyEvent** last_key_event) {
  842. DCHECK(last_key_event);
  843. // A safe guard in case if there were continuous key pressed events that are
  844. // not auto repeat.
  845. const int kMaxAutoRepeatTimeMs = 2000;
  846. if (is_char())
  847. return false;
  848. if (type() == ET_KEY_RELEASED) {
  849. delete *last_key_event;
  850. *last_key_event = nullptr;
  851. return false;
  852. }
  853. CHECK_EQ(ET_KEY_PRESSED, type());
  854. KeyEvent* last = *last_key_event;
  855. if (!last) {
  856. *last_key_event = new KeyEvent(*this);
  857. return false;
  858. } else if (time_stamp() == last->time_stamp()) {
  859. // The KeyEvent is created from the same native event.
  860. return (last->flags() & EF_IS_REPEAT) != 0;
  861. }
  862. DCHECK(last);
  863. bool is_repeat = false;
  864. #if BUILDFLAG(IS_WIN)
  865. if (HasNativeEvent()) {
  866. // Bit 30 of lParam represents the "previous key state". If set, the key
  867. // was already down, therefore this is an auto-repeat.
  868. is_repeat = (native_event().lParam & 0x40000000) != 0;
  869. }
  870. #endif
  871. if (!is_repeat) {
  872. if (key_code() == last->key_code() &&
  873. (flags() & ~EF_MOUSE_BUTTON) ==
  874. (last->flags() & ~EF_IS_REPEAT & ~EF_MOUSE_BUTTON) &&
  875. (time_stamp() - last->time_stamp()).InMilliseconds() <
  876. kMaxAutoRepeatTimeMs) {
  877. is_repeat = true;
  878. }
  879. }
  880. if (is_repeat) {
  881. last->set_time_stamp(time_stamp());
  882. last->set_flags(last->flags() | EF_IS_REPEAT);
  883. return true;
  884. }
  885. delete *last_key_event;
  886. *last_key_event = new KeyEvent(*this);
  887. return false;
  888. }
  889. KeyEvent** KeyEvent::GetLastKeyEvent() {
  890. #if defined(USE_OZONE)
  891. // Use a different static variable for key events that have non standard
  892. // state masks as it may be reposted by an IME. IBUS-GTK and fcitx-GTK uses
  893. // this field to detect the re-posted event for example. crbug.com/385873.
  894. return properties() && properties()->contains(kPropertyKeyboardImeFlag)
  895. ? &last_ibus_key_event_
  896. : &last_key_event_;
  897. #else
  898. return &last_key_event_;
  899. #endif
  900. }
  901. DomKey KeyEvent::GetDomKey() const {
  902. // Determination of key_ may be done lazily.
  903. if (key_ == DomKey::NONE)
  904. ApplyLayout();
  905. return key_;
  906. }
  907. char16_t KeyEvent::GetCharacter() const {
  908. // Determination of key_ may be done lazily.
  909. if (key_ == DomKey::NONE)
  910. ApplyLayout();
  911. if (key_.IsCharacter()) {
  912. // Historically ui::KeyEvent has held only BMP characters.
  913. // Until this explicitly changes, require |key_| to hold a BMP character.
  914. DomKey::Base utf32_character = key_.ToCharacter();
  915. char16_t ucs2_character = static_cast<char16_t>(utf32_character);
  916. DCHECK_EQ(static_cast<DomKey::Base>(ucs2_character), utf32_character);
  917. // Check if the control character is down. Note that ALTGR is represented
  918. // on Windows as CTRL|ALT, so we need to make sure that is not set.
  919. if ((flags() & (EF_ALTGR_DOWN | EF_CONTROL_DOWN)) == EF_CONTROL_DOWN) {
  920. // For a control character, key_ contains the corresponding printable
  921. // character. To preserve existing behaviour for now, return the control
  922. // character here; this will likely change -- see e.g. crbug.com/471488.
  923. if (ucs2_character >= 0x20 && ucs2_character <= 0x7E)
  924. return ucs2_character & 0x1F;
  925. if (ucs2_character == '\r')
  926. return '\n';
  927. }
  928. return ucs2_character;
  929. }
  930. return 0;
  931. }
  932. char16_t KeyEvent::GetText() const {
  933. if ((flags() & EF_CONTROL_DOWN) != 0) {
  934. DomKey key;
  935. KeyboardCode key_code;
  936. if (DomCodeToControlCharacter(code_, flags(), &key, &key_code))
  937. return key.ToCharacter();
  938. }
  939. return GetUnmodifiedText();
  940. }
  941. char16_t KeyEvent::GetUnmodifiedText() const {
  942. if (!is_char_ && (key_code_ == VKEY_RETURN))
  943. return '\r';
  944. return GetCharacter();
  945. }
  946. bool KeyEvent::IsUnicodeKeyCode() const {
  947. #if BUILDFLAG(IS_WIN)
  948. if (!IsAltDown())
  949. return false;
  950. const int key = key_code();
  951. if (key >= VKEY_NUMPAD0 && key <= VKEY_NUMPAD9)
  952. return true;
  953. // Check whether the user is using the numeric keypad with num-lock off.
  954. // In that case, EF_EXTENDED will not be set; if it is set, the key event
  955. // originated from the relevant non-numpad dedicated key, e.g. [Insert].
  956. return (!(flags() & EF_IS_EXTENDED_KEY) &&
  957. (key == VKEY_INSERT || key == VKEY_END || key == VKEY_DOWN ||
  958. key == VKEY_NEXT || key == VKEY_LEFT || key == VKEY_CLEAR ||
  959. key == VKEY_RIGHT || key == VKEY_HOME || key == VKEY_UP ||
  960. key == VKEY_PRIOR));
  961. #else
  962. return false;
  963. #endif
  964. }
  965. void KeyEvent::NormalizeFlags() {
  966. int mask = 0;
  967. switch (key_code()) {
  968. case VKEY_CONTROL:
  969. mask = EF_CONTROL_DOWN;
  970. break;
  971. case VKEY_SHIFT:
  972. mask = EF_SHIFT_DOWN;
  973. break;
  974. case VKEY_MENU:
  975. mask = EF_ALT_DOWN;
  976. break;
  977. default:
  978. return;
  979. }
  980. if (type() == ET_KEY_PRESSED)
  981. set_flags(flags() | mask);
  982. else
  983. set_flags(flags() & ~mask);
  984. }
  985. std::string KeyEvent::ToString() const {
  986. return base::StrCat(
  987. {Event::ToString(), " key ", base::StringPrintf("(0x%.4x)", key_code_),
  988. " flags ",
  989. base::JoinString(base::make_span(KeyEventFlagsNames(flags())), " | ")});
  990. }
  991. std::unique_ptr<Event> KeyEvent::Clone() const {
  992. return std::make_unique<KeyEvent>(*this);
  993. }
  994. KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const {
  995. return NonLocatedToLocatedKeyboardCode(key_code_, code_);
  996. }
  997. uint16_t KeyEvent::GetConflatedWindowsKeyCode() const {
  998. if (is_char_)
  999. return key_.ToCharacter();
  1000. return key_code_;
  1001. }
  1002. std::string KeyEvent::GetCodeString() const {
  1003. return KeycodeConverter::DomCodeToCodeString(code_);
  1004. }
  1005. ////////////////////////////////////////////////////////////////////////////////
  1006. // ScrollEvent
  1007. ScrollEvent::ScrollEvent(const PlatformEvent& native_event)
  1008. : MouseEvent(native_event),
  1009. x_offset_(0.0f),
  1010. y_offset_(0.0f),
  1011. x_offset_ordinal_(0.0f),
  1012. y_offset_ordinal_(0.0f),
  1013. finger_count_(0),
  1014. momentum_phase_(EventMomentumPhase::NONE),
  1015. scroll_event_phase_(ScrollEventPhase::kNone) {
  1016. // TODO(bokan): This should be populating the |scroll_event_phase_| member but
  1017. // currently isn't.
  1018. if (type() == ET_SCROLL) {
  1019. GetScrollOffsets(native_event, &x_offset_, &y_offset_, &x_offset_ordinal_,
  1020. &y_offset_ordinal_, &finger_count_, &momentum_phase_);
  1021. } else if (type() == ET_SCROLL_FLING_START ||
  1022. type() == ET_SCROLL_FLING_CANCEL) {
  1023. GetFlingData(native_event, &x_offset_, &y_offset_, &x_offset_ordinal_,
  1024. &y_offset_ordinal_, nullptr);
  1025. } else {
  1026. NOTREACHED() << "Unexpected event type " << type()
  1027. << " when constructing a ScrollEvent.";
  1028. }
  1029. if (IsScrollEvent())
  1030. latency()->set_source_event_type(SourceEventType::WHEEL);
  1031. else
  1032. latency()->set_source_event_type(SourceEventType::TOUCH);
  1033. }
  1034. ScrollEvent::ScrollEvent(EventType type,
  1035. const gfx::PointF& location,
  1036. const gfx::PointF& root_location,
  1037. base::TimeTicks time_stamp,
  1038. int flags,
  1039. float x_offset,
  1040. float y_offset,
  1041. float x_offset_ordinal,
  1042. float y_offset_ordinal,
  1043. int finger_count,
  1044. EventMomentumPhase momentum_phase,
  1045. ScrollEventPhase scroll_event_phase)
  1046. : MouseEvent(type, location, root_location, time_stamp, flags, 0),
  1047. x_offset_(x_offset),
  1048. y_offset_(y_offset),
  1049. x_offset_ordinal_(x_offset_ordinal),
  1050. y_offset_ordinal_(y_offset_ordinal),
  1051. finger_count_(finger_count),
  1052. momentum_phase_(momentum_phase),
  1053. scroll_event_phase_(scroll_event_phase) {
  1054. CHECK(IsScrollEvent());
  1055. latency()->set_source_event_type(SourceEventType::WHEEL);
  1056. }
  1057. ScrollEvent::ScrollEvent(EventType type,
  1058. const gfx::Point& location,
  1059. base::TimeTicks time_stamp,
  1060. int flags,
  1061. float x_offset,
  1062. float y_offset,
  1063. float x_offset_ordinal,
  1064. float y_offset_ordinal,
  1065. int finger_count,
  1066. EventMomentumPhase momentum_phase,
  1067. ScrollEventPhase scroll_event_phase)
  1068. : ScrollEvent(type,
  1069. gfx::PointF(location),
  1070. gfx::PointF(location),
  1071. time_stamp,
  1072. flags,
  1073. x_offset,
  1074. y_offset,
  1075. x_offset_ordinal,
  1076. y_offset_ordinal,
  1077. finger_count,
  1078. momentum_phase,
  1079. scroll_event_phase) {}
  1080. ScrollEvent::ScrollEvent(const ScrollEvent& other) = default;
  1081. ScrollEvent::~ScrollEvent() = default;
  1082. void ScrollEvent::Scale(const float factor) {
  1083. x_offset_ *= factor;
  1084. y_offset_ *= factor;
  1085. x_offset_ordinal_ *= factor;
  1086. y_offset_ordinal_ *= factor;
  1087. }
  1088. std::string ScrollEvent::ToString() const {
  1089. return base::StringPrintf(
  1090. "%s offset %g,%g offset_ordinal %g,%g momentum_phase %s event_phase %s",
  1091. MouseEvent::ToString().c_str(), x_offset_, y_offset_, x_offset_ordinal_,
  1092. y_offset_ordinal_, MomentumPhaseToString(momentum_phase_).c_str(),
  1093. ScrollEventPhaseToString(scroll_event_phase_).c_str());
  1094. }
  1095. std::unique_ptr<Event> ScrollEvent::Clone() const {
  1096. return std::make_unique<ScrollEvent>(*this);
  1097. }
  1098. ////////////////////////////////////////////////////////////////////////////////
  1099. // GestureEvent
  1100. GestureEvent::GestureEvent(float x,
  1101. float y,
  1102. int flags,
  1103. base::TimeTicks time_stamp,
  1104. const GestureEventDetails& details,
  1105. uint32_t unique_touch_event_id)
  1106. : LocatedEvent(details.type(),
  1107. gfx::PointF(x, y),
  1108. gfx::PointF(x, y),
  1109. time_stamp,
  1110. flags | EF_FROM_TOUCH),
  1111. details_(details),
  1112. unique_touch_event_id_(unique_touch_event_id) {
  1113. latency()->set_source_event_type(SourceEventType::TOUCH);
  1114. // TODO(crbug.com/868056) Other touchpad gesture should report as TOUCHPAD.
  1115. if (IsPinchEvent() &&
  1116. details.device_type() == GestureDeviceType::DEVICE_TOUCHPAD) {
  1117. latency()->set_source_event_type(SourceEventType::TOUCHPAD);
  1118. }
  1119. }
  1120. GestureEvent::GestureEvent(const GestureEvent& other) = default;
  1121. GestureEvent::~GestureEvent() = default;
  1122. std::string GestureEvent::ToString() const {
  1123. return base::StringPrintf("%s touch_event_id %d",
  1124. LocatedEvent::ToString().c_str(),
  1125. unique_touch_event_id_);
  1126. }
  1127. std::unique_ptr<Event> GestureEvent::Clone() const {
  1128. return std::make_unique<GestureEvent>(*this);
  1129. }
  1130. } // namespace ui