Commit 2c366ee7 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

events: add ability to serialize TouchEvents

This makes the event serialization code handle TouchEvents. I plan on
doing a cleanup pass to make MouseEvent serialization use the PointerDetails
struct traits.

BUG=865781
TEST=covered by tests

Change-Id: I190ac58a3066c10bc08cd083702bc15232d4141d
Reviewed-on: https://chromium-review.googlesource.com/c/1265160
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597383}
parent 9ed55271
......@@ -521,6 +521,7 @@ if (!is_ios) {
"//ipc:test_support",
"//mojo/core/test:run_all_unittests",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/test_support:test_utils",
"//skia",
"//testing/gmock",
"//testing/gtest",
......
......@@ -737,6 +737,9 @@ class EVENTS_EXPORT TouchEvent : public LocatedEvent {
should_remove_native_touch_id_mapping_ =
should_remove_native_touch_id_mapping;
}
bool should_remove_native_touch_id_mapping() const {
return should_remove_native_touch_id_mapping_;
}
// Overridden from LocatedEvent.
void UpdateForRootTransform(
......@@ -756,6 +759,9 @@ class EVENTS_EXPORT TouchEvent : public LocatedEvent {
private:
// A unique identifier for the touch event.
// NOTE: this is *not* serialized over mojom, as the number is unique to
// a particular process, and as mojom may go cross process, to serialize could
// lead to conflicts.
uint32_t unique_event_id_;
// Whether the (unhandled) touch event will produce a scroll event (e.g., a
......@@ -767,6 +773,7 @@ class EVENTS_EXPORT TouchEvent : public LocatedEvent {
// event id and the touch_id_. This should only be the case for
// release and cancel events where the associated touch press event
// created a mapping between the native id and the touch_id_.
// NOTE: this is not serialized, as it's generally unique to the source.
bool should_remove_native_touch_id_mapping_;
// True for devices like some pens when they support hovering over
......
......@@ -140,6 +140,29 @@ struct ScrollData {
ScrollEventPhase scroll_event_phase;
};
// This mirrors the C++ class of the same name, see it for details.
struct PointerDetails {
PointerKind pointer_type;
float radius_x;
float radius_y;
float force;
float tilt_x;
float tilt_y;
float tangential_pressure;
float twist;
int32 id;
int32 offset_x;
int32 offset_y;
};
// This is used for TouchEvents.
struct TouchData {
bool may_cause_scrolling;
bool hovering;
LocationData location;
PointerDetails pointer_details;
};
struct Event {
// TODO(sky): rename to type.
EventType action;
......@@ -157,5 +180,6 @@ struct Event {
PointerData? pointer_data;
GestureData? gesture_data;
ScrollData? scroll_data;
TouchData? touch_data;
map<string, array<uint8>>? properties;
};
......@@ -22,5 +22,6 @@ sources = [
type_mappings = [
"ui.mojom.Event=std::unique_ptr<ui::Event>[move_only]",
"ui.mojom.EventMomentumPhase=ui::EventMomentumPhase",
"ui.mojom.PointerDetails=ui::PointerDetails",
"ui.mojom.ScrollEventPhase=ui::ScrollEventPhase",
]
......@@ -40,6 +40,10 @@ enum EventType {
MOUSE_EXITED_EVENT,
MOUSE_WHEEL_EVENT,
MOUSE_CAPTURE_CHANGED_EVENT,
TOUCH_RELEASED,
TOUCH_PRESSED,
TOUCH_MOVED,
TOUCH_CANCELLED,
};
// This mirrors ui::EventFlags
......@@ -67,7 +71,9 @@ const int32 kMouseEventFlagIsNonClient = 0x20000;
// TODO(erg): Move accessibility flags and maybe synthetic touch events here.
// TODO(sky): rename EventPointerType.
enum PointerKind {
UNKNOWN,
MOUSE,
PEN,
TOUCH,
......
......@@ -26,21 +26,6 @@ ui::mojom::LocationDataPtr GetLocationData(const ui::LocatedEvent* event) {
return location_data;
}
ui::EventPointerType PointerTypeFromPointerKind(ui::mojom::PointerKind kind) {
switch (kind) {
case ui::mojom::PointerKind::MOUSE:
return ui::EventPointerType::POINTER_TYPE_MOUSE;
case ui::mojom::PointerKind::TOUCH:
return ui::EventPointerType::POINTER_TYPE_TOUCH;
case ui::mojom::PointerKind::PEN:
return ui::EventPointerType::POINTER_TYPE_PEN;
case ui::mojom::PointerKind::ERASER:
return ui::EventPointerType::POINTER_TYPE_ERASER;
}
NOTREACHED();
return ui::EventPointerType::POINTER_TYPE_UNKNOWN;
}
void UpdateEventLocation(const ui::mojom::PointerData& pointer_data,
EventUniquePtr* out) {
// TODO(katie): Update LocationData to have two PointF instead of storing this
......@@ -56,9 +41,9 @@ void UpdateEventLocation(const ui::mojom::PointerData& pointer_data,
out->get()->AsLocatedEvent()->set_root_location_f(screen_location);
}
bool ReadPointerDetails(ui::mojom::EventType event_type,
const ui::mojom::PointerData& pointer_data,
ui::PointerDetails* out) {
bool ReadPointerDetailsDeprecated(ui::mojom::EventType event_type,
const ui::mojom::PointerData& pointer_data,
ui::PointerDetails* out) {
switch (pointer_data.kind) {
case ui::mojom::PointerKind::MOUSE: {
if (event_type == ui::mojom::EventType::POINTER_WHEEL_CHANGED ||
......@@ -76,18 +61,23 @@ bool ReadPointerDetails(ui::mojom::EventType event_type,
}
case ui::mojom::PointerKind::TOUCH:
case ui::mojom::PointerKind::PEN: {
ui::EventPointerType pointer_type;
if (!EnumTraits<ui::mojom::PointerKind, ui::EventPointerType>::FromMojom(
pointer_data.kind, &pointer_type))
return false;
const ui::mojom::BrushData& brush_data = *pointer_data.brush_data;
*out = ui::PointerDetails(
PointerTypeFromPointerKind(pointer_data.kind),
pointer_data.pointer_id, brush_data.width, brush_data.height,
brush_data.pressure, brush_data.twist, brush_data.tilt_x,
brush_data.tilt_y, brush_data.tangential_pressure);
pointer_type, pointer_data.pointer_id, brush_data.width,
brush_data.height, brush_data.pressure, brush_data.twist,
brush_data.tilt_x, brush_data.tilt_y, brush_data.tangential_pressure);
return true;
}
case ui::mojom::PointerKind::ERASER:
// TODO(jamescook): Eraser support.
NOTIMPLEMENTED();
return false;
case ui::mojom::PointerKind::UNKNOWN:
return false;
}
NOTREACHED();
return false;
......@@ -225,6 +215,14 @@ ui::mojom::EventType TypeConverter<ui::mojom::EventType,
return ui::mojom::EventType::MOUSE_WHEEL_EVENT;
case ui::ET_MOUSE_CAPTURE_CHANGED:
return ui::mojom::EventType::MOUSE_CAPTURE_CHANGED_EVENT;
case ui::ET_TOUCH_RELEASED:
return ui::mojom::EventType::TOUCH_RELEASED;
case ui::ET_TOUCH_PRESSED:
return ui::mojom::EventType::TOUCH_PRESSED;
case ui::ET_TOUCH_MOVED:
return ui::mojom::EventType::TOUCH_MOVED;
case ui::ET_TOUCH_CANCELLED:
return ui::mojom::EventType::TOUCH_CANCELLED;
default:
NOTREACHED() << "Using unknown event types closes connections:"
<< ui::EventTypeName(type);
......@@ -283,6 +281,14 @@ ui::EventType TypeConverter<ui::EventType, ui::mojom::EventType>::Convert(
return ui::ET_MOUSEWHEEL;
case ui::mojom::EventType::MOUSE_CAPTURE_CHANGED_EVENT:
return ui::ET_MOUSE_CAPTURE_CHANGED;
case ui::mojom::EventType::TOUCH_RELEASED:
return ui::ET_TOUCH_RELEASED;
case ui::mojom::EventType::TOUCH_PRESSED:
return ui::ET_TOUCH_PRESSED;
case ui::mojom::EventType::TOUCH_MOVED:
return ui::ET_TOUCH_MOVED;
case ui::mojom::EventType::TOUCH_CANCELLED:
return ui::ET_TOUCH_CANCELLED;
default:
NOTREACHED();
}
......@@ -454,6 +460,26 @@ StructTraits<ui::mojom::EventDataView, EventUniquePtr>::scroll_data(
return scroll_data;
}
// static
ui::mojom::TouchDataPtr
StructTraits<ui::mojom::EventDataView, EventUniquePtr>::touch_data(
const EventUniquePtr& event) {
if (!event->IsTouchEvent())
return nullptr;
const ui::TouchEvent* touch_event = event->AsTouchEvent();
ui::mojom::TouchDataPtr touch_data(ui::mojom::TouchData::New());
touch_data->may_cause_scrolling = touch_event->may_cause_scrolling();
touch_data->hovering = touch_event->hovering();
touch_data->location = ui::mojom::LocationData::New();
touch_data->location->x = touch_event->location_f().x();
touch_data->location->y = touch_event->location_f().y();
touch_data->location->screen_x = touch_event->root_location_f().x();
touch_data->location->screen_y = touch_event->root_location_f().y();
touch_data->pointer_details = touch_event->pointer_details();
return touch_data;
}
// static
base::flat_map<std::string, std::vector<uint8_t>>
StructTraits<ui::mojom::EventDataView, EventUniquePtr>::properties(
......@@ -506,7 +532,8 @@ bool StructTraits<ui::mojom::EventDataView, EventUniquePtr>::Read(
return false;
ui::PointerDetails pointer_details;
if (!ReadPointerDetails(event.action(), *pointer_data, &pointer_details))
if (!ReadPointerDetailsDeprecated(event.action(), *pointer_data,
&pointer_details))
return false;
*out = std::make_unique<ui::PointerEvent>(
......@@ -552,7 +579,8 @@ bool StructTraits<ui::mojom::EventDataView, EventUniquePtr>::Read(
return false;
ui::PointerDetails pointer_details;
if (!ReadPointerDetails(event.action(), *pointer_data, &pointer_details))
if (!ReadPointerDetailsDeprecated(event.action(), *pointer_data,
&pointer_details))
return false;
if (event.action() == ui::mojom::EventType::MOUSE_WHEEL_EVENT) {
......@@ -575,6 +603,27 @@ bool StructTraits<ui::mojom::EventDataView, EventUniquePtr>::Read(
UpdateEventLocation(*pointer_data, out);
break;
}
case ui::mojom::EventType::TOUCH_RELEASED:
case ui::mojom::EventType::TOUCH_PRESSED:
case ui::mojom::EventType::TOUCH_MOVED:
case ui::mojom::EventType::TOUCH_CANCELLED: {
ui::mojom::TouchDataPtr touch_data;
if (!event.ReadTouchData(&touch_data))
return false;
std::unique_ptr<ui::TouchEvent> touch_event =
std::make_unique<ui::TouchEvent>(
mojo::ConvertTo<ui::EventType>(event.action()),
gfx::Point(), // Real location set below.
time_stamp, touch_data->pointer_details, event.flags());
touch_event->set_location_f(
gfx::PointF(touch_data->location->x, touch_data->location->y));
touch_event->set_root_location_f(gfx::PointF(
touch_data->location->screen_x, touch_data->location->screen_y));
touch_event->set_may_cause_scrolling(touch_data->may_cause_scrolling);
touch_event->set_hovering(touch_data->hovering);
*out = std::move(touch_event);
return true;
}
case ui::mojom::EventType::UNKNOWN:
NOTREACHED() << "Using unknown event types closes connections";
return false;
......@@ -595,4 +644,23 @@ bool StructTraits<ui::mojom::EventDataView, EventUniquePtr>::Read(
return true;
}
// static
bool StructTraits<ui::mojom::PointerDetailsDataView, ui::PointerDetails>::Read(
ui::mojom::PointerDetailsDataView data,
ui::PointerDetails* out) {
if (!data.ReadPointerType(&out->pointer_type))
return false;
out->radius_x = data.radius_x();
out->radius_y = data.radius_y();
out->force = data.force();
out->tilt_x = data.tilt_x();
out->tilt_y = data.tilt_y();
out->tangential_pressure = data.tangential_pressure();
out->twist = data.twist();
out->id = data.id();
out->offset.set_x(data.offset_x());
out->offset.set_y(data.offset_y());
return true;
}
} // namespace mojo
......@@ -18,6 +18,7 @@
namespace ui {
class Event;
class LatencyInfo;
struct PointerDetails;
}
namespace mojo {
......@@ -44,6 +45,7 @@ struct StructTraits<ui::mojom::EventDataView, EventUniquePtr> {
static ui::mojom::PointerDataPtr pointer_data(const EventUniquePtr& event);
static ui::mojom::GestureDataPtr gesture_data(const EventUniquePtr& event);
static ui::mojom::ScrollDataPtr scroll_data(const EventUniquePtr& event);
static ui::mojom::TouchDataPtr touch_data(const EventUniquePtr& event);
static base::flat_map<std::string, std::vector<uint8_t>> properties(
const EventUniquePtr& event);
static bool Read(ui::mojom::EventDataView r, EventUniquePtr* out);
......@@ -92,6 +94,71 @@ struct EnumTraits<ui::mojom::EventMomentumPhase, ui::EventMomentumPhase> {
}
};
template <>
struct EnumTraits<ui::mojom::PointerKind, ui::EventPointerType> {
static ui::mojom::PointerKind ToMojom(ui::EventPointerType input) {
switch (input) {
case ui::EventPointerType::POINTER_TYPE_UNKNOWN:
return ui::mojom::PointerKind::UNKNOWN;
case ui::EventPointerType::POINTER_TYPE_MOUSE:
return ui::mojom::PointerKind::MOUSE;
case ui::EventPointerType::POINTER_TYPE_PEN:
return ui::mojom::PointerKind::PEN;
case ui::EventPointerType::POINTER_TYPE_TOUCH:
return ui::mojom::PointerKind::TOUCH;
case ui::EventPointerType::POINTER_TYPE_ERASER:
return ui::mojom::PointerKind::ERASER;
}
NOTREACHED();
return ui::mojom::PointerKind::UNKNOWN;
}
static bool FromMojom(ui::mojom::PointerKind input,
ui::EventPointerType* out) {
switch (input) {
case ui::mojom::PointerKind::UNKNOWN:
*out = ui::EventPointerType::POINTER_TYPE_UNKNOWN;
return true;
case ui::mojom::PointerKind::MOUSE:
*out = ui::EventPointerType::POINTER_TYPE_MOUSE;
return true;
case ui::mojom::PointerKind::PEN:
*out = ui::EventPointerType::POINTER_TYPE_PEN;
return true;
case ui::mojom::PointerKind::TOUCH:
*out = ui::EventPointerType::POINTER_TYPE_TOUCH;
return true;
case ui::mojom::PointerKind::ERASER:
*out = ui::EventPointerType::POINTER_TYPE_ERASER;
return true;
}
NOTREACHED();
return false;
}
};
template <>
struct StructTraits<ui::mojom::PointerDetailsDataView, ui::PointerDetails> {
static ui::EventPointerType pointer_type(const ui::PointerDetails& i) {
return i.pointer_type;
}
static float radius_x(const ui::PointerDetails& i) { return i.radius_x; }
static float radius_y(const ui::PointerDetails& i) { return i.radius_y; }
static float force(const ui::PointerDetails& i) { return i.force; }
static float tilt_x(const ui::PointerDetails& i) { return i.tilt_x; }
static float tilt_y(const ui::PointerDetails& i) { return i.tilt_y; }
static float tangential_pressure(const ui::PointerDetails& i) {
return i.tangential_pressure;
}
static float twist(const ui::PointerDetails& i) { return i.twist; }
static int32_t id(const ui::PointerDetails& i) { return i.id; }
static int32_t offset_x(const ui::PointerDetails& i) { return i.offset.x(); }
static int32_t offset_y(const ui::PointerDetails& i) { return i.offset.y(); }
static bool Read(ui::mojom::PointerDetailsDataView data,
ui::PointerDetails* out);
};
template <>
struct EnumTraits<ui::mojom::ScrollEventPhase, ui::ScrollEventPhase> {
static ui::mojom::ScrollEventPhase ToMojom(ui::ScrollEventPhase input) {
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment