event_mojom_traits.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2016 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. #ifndef UI_EVENTS_MOJOM_EVENT_MOJOM_TRAITS_H_
  5. #define UI_EVENTS_MOJOM_EVENT_MOJOM_TRAITS_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/containers/flat_map.h"
  9. #include "base/notreached.h"
  10. #include "mojo/public/cpp/bindings/type_converter.h"
  11. #include "ui/events/event_constants.h"
  12. #include "ui/events/mojom/event.mojom.h"
  13. #include "ui/events/mojom/event_constants.mojom.h"
  14. #include "ui/events/pointer_details.h"
  15. #include "ui/events/types/event_type.h"
  16. namespace ui {
  17. class Event;
  18. class LatencyInfo;
  19. }
  20. namespace mojo {
  21. using EventUniquePtr = std::unique_ptr<ui::Event>;
  22. template <>
  23. struct TypeConverter<ui::mojom::EventType, ui::EventType> {
  24. static ui::mojom::EventType Convert(ui::EventType type);
  25. };
  26. template <>
  27. struct TypeConverter<ui::EventType, ui::mojom::EventType> {
  28. static ui::EventType Convert(ui::mojom::EventType type);
  29. };
  30. template <>
  31. struct StructTraits<ui::mojom::EventDataView, EventUniquePtr> {
  32. static ui::mojom::EventType action(const EventUniquePtr& event);
  33. static int32_t flags(const EventUniquePtr& event);
  34. static base::TimeTicks time_stamp(const EventUniquePtr& event);
  35. static const ui::LatencyInfo& latency(const EventUniquePtr& event);
  36. static ui::mojom::KeyDataPtr key_data(const EventUniquePtr& event);
  37. static ui::mojom::GestureDataPtr gesture_data(const EventUniquePtr& event);
  38. static ui::mojom::ScrollDataPtr scroll_data(const EventUniquePtr& event);
  39. static ui::mojom::TouchDataPtr touch_data(const EventUniquePtr& event);
  40. static ui::mojom::MouseDataPtr mouse_data(const EventUniquePtr& event);
  41. static base::flat_map<std::string, std::vector<uint8_t>> properties(
  42. const EventUniquePtr& event);
  43. static bool Read(ui::mojom::EventDataView r, EventUniquePtr* out);
  44. };
  45. template <>
  46. struct EnumTraits<ui::mojom::EventMomentumPhase, ui::EventMomentumPhase> {
  47. static ui::mojom::EventMomentumPhase ToMojom(ui::EventMomentumPhase input) {
  48. switch (input) {
  49. case ui::EventMomentumPhase::NONE:
  50. return ui::mojom::EventMomentumPhase::NONE;
  51. case ui::EventMomentumPhase::BEGAN:
  52. return ui::mojom::EventMomentumPhase::BEGAN;
  53. case ui::EventMomentumPhase::MAY_BEGIN:
  54. return ui::mojom::EventMomentumPhase::MAY_BEGIN;
  55. case ui::EventMomentumPhase::INERTIAL_UPDATE:
  56. return ui::mojom::EventMomentumPhase::INERTIAL_UPDATE;
  57. case ui::EventMomentumPhase::END:
  58. return ui::mojom::EventMomentumPhase::END;
  59. case ui::EventMomentumPhase::BLOCKED:
  60. return ui::mojom::EventMomentumPhase::BLOCKED;
  61. }
  62. NOTREACHED();
  63. return ui::mojom::EventMomentumPhase::NONE;
  64. }
  65. static bool FromMojom(ui::mojom::EventMomentumPhase input,
  66. ui::EventMomentumPhase* out) {
  67. switch (input) {
  68. case ui::mojom::EventMomentumPhase::NONE:
  69. *out = ui::EventMomentumPhase::NONE;
  70. return true;
  71. case ui::mojom::EventMomentumPhase::BEGAN:
  72. *out = ui::EventMomentumPhase::BEGAN;
  73. return true;
  74. case ui::mojom::EventMomentumPhase::MAY_BEGIN:
  75. *out = ui::EventMomentumPhase::MAY_BEGIN;
  76. return true;
  77. case ui::mojom::EventMomentumPhase::INERTIAL_UPDATE:
  78. *out = ui::EventMomentumPhase::INERTIAL_UPDATE;
  79. return true;
  80. case ui::mojom::EventMomentumPhase::END:
  81. *out = ui::EventMomentumPhase::END;
  82. return true;
  83. case ui::mojom::EventMomentumPhase::BLOCKED:
  84. *out = ui::EventMomentumPhase::BLOCKED;
  85. return true;
  86. }
  87. NOTREACHED();
  88. return false;
  89. }
  90. };
  91. template <>
  92. struct StructTraits<ui::mojom::PointerDetailsDataView, ui::PointerDetails> {
  93. static ui::EventPointerType pointer_type(const ui::PointerDetails& i) {
  94. return i.pointer_type;
  95. }
  96. static float radius_x(const ui::PointerDetails& i) { return i.radius_x; }
  97. static float radius_y(const ui::PointerDetails& i) { return i.radius_y; }
  98. static float force(const ui::PointerDetails& i) { return i.force; }
  99. static float tilt_x(const ui::PointerDetails& i) { return i.tilt_x; }
  100. static float tilt_y(const ui::PointerDetails& i) { return i.tilt_y; }
  101. static float tangential_pressure(const ui::PointerDetails& i) {
  102. return i.tangential_pressure;
  103. }
  104. static float twist(const ui::PointerDetails& i) { return i.twist; }
  105. static int32_t id(const ui::PointerDetails& i) { return i.id; }
  106. static int32_t offset_x(const ui::PointerDetails& i) { return i.offset.x(); }
  107. static int32_t offset_y(const ui::PointerDetails& i) { return i.offset.y(); }
  108. static bool Read(ui::mojom::PointerDetailsDataView data,
  109. ui::PointerDetails* out);
  110. };
  111. template <>
  112. struct EnumTraits<ui::mojom::ScrollEventPhase, ui::ScrollEventPhase> {
  113. static ui::mojom::ScrollEventPhase ToMojom(ui::ScrollEventPhase input) {
  114. switch (input) {
  115. case ui::ScrollEventPhase::kNone:
  116. return ui::mojom::ScrollEventPhase::kNone;
  117. case ui::ScrollEventPhase::kBegan:
  118. return ui::mojom::ScrollEventPhase::kBegan;
  119. case ui::ScrollEventPhase::kUpdate:
  120. return ui::mojom::ScrollEventPhase::kUpdate;
  121. case ui::ScrollEventPhase::kEnd:
  122. return ui::mojom::ScrollEventPhase::kEnd;
  123. }
  124. NOTREACHED();
  125. return ui::mojom::ScrollEventPhase::kNone;
  126. }
  127. static bool FromMojom(ui::mojom::ScrollEventPhase input,
  128. ui::ScrollEventPhase* out) {
  129. switch (input) {
  130. case ui::mojom::ScrollEventPhase::kNone:
  131. *out = ui::ScrollEventPhase::kNone;
  132. return true;
  133. case ui::mojom::ScrollEventPhase::kBegan:
  134. *out = ui::ScrollEventPhase::kBegan;
  135. return true;
  136. case ui::mojom::ScrollEventPhase::kUpdate:
  137. *out = ui::ScrollEventPhase::kUpdate;
  138. return true;
  139. case ui::mojom::ScrollEventPhase::kEnd:
  140. *out = ui::ScrollEventPhase::kEnd;
  141. return true;
  142. }
  143. NOTREACHED();
  144. return false;
  145. }
  146. };
  147. template <>
  148. struct EnumTraits<ui::mojom::GestureDeviceType, ui::GestureDeviceType> {
  149. static ui::mojom::GestureDeviceType ToMojom(ui::GestureDeviceType input) {
  150. switch (input) {
  151. case ui::GestureDeviceType::DEVICE_UNKNOWN:
  152. return ui::mojom::GestureDeviceType::DEVICE_UNKNOWN;
  153. case ui::GestureDeviceType::DEVICE_TOUCHPAD:
  154. return ui::mojom::GestureDeviceType::DEVICE_TOUCHPAD;
  155. case ui::GestureDeviceType::DEVICE_TOUCHSCREEN:
  156. return ui::mojom::GestureDeviceType::DEVICE_TOUCHSCREEN;
  157. }
  158. NOTREACHED();
  159. return ui::mojom::GestureDeviceType::DEVICE_UNKNOWN;
  160. }
  161. static bool FromMojom(ui::mojom::GestureDeviceType input,
  162. ui::GestureDeviceType* out) {
  163. switch (input) {
  164. case ui::mojom::GestureDeviceType::DEVICE_UNKNOWN:
  165. *out = ui::GestureDeviceType::DEVICE_UNKNOWN;
  166. return true;
  167. case ui::mojom::GestureDeviceType::DEVICE_TOUCHPAD:
  168. *out = ui::GestureDeviceType::DEVICE_TOUCHPAD;
  169. return true;
  170. case ui::mojom::GestureDeviceType::DEVICE_TOUCHSCREEN:
  171. *out = ui::GestureDeviceType::DEVICE_TOUCHSCREEN;
  172. return true;
  173. }
  174. NOTREACHED();
  175. return false;
  176. }
  177. };
  178. } // namespace mojo
  179. #endif // UI_EVENTS_MOJOM_EVENT_MOJOM_TRAITS_H_