1 // Copyright 2015 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.
5 #include "mojo/services/html_viewer/touch_handler.h"
7 #include "third_party/WebKit/public/web/WebInputEvent.h"
8 #include "third_party/WebKit/public/web/WebView.h"
9 #include "third_party/mojo_services/src/input_events/public/interfaces/input_events.mojom.h"
10 #include "ui/events/blink/blink_event_util.h"
11 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
12 #include "ui/events/gesture_detection/motion_event_generic.h"
14 namespace html_viewer
{
17 void SetPropertiesFromEvent(const mojo::Event
& event
,
18 ui::PointerProperties
* properties
) {
19 properties
->id
= event
.pointer_data
->pointer_id
;
20 properties
->x
= event
.pointer_data
->x
;
21 properties
->y
= event
.pointer_data
->y
;
22 properties
->raw_x
= event
.pointer_data
->screen_x
;
23 properties
->raw_y
= event
.pointer_data
->screen_y
;
24 properties
->pressure
= event
.pointer_data
->pressure
;
25 properties
->touch_major
= event
.pointer_data
->radius_major
;
26 properties
->touch_minor
= event
.pointer_data
->radius_minor
;
27 properties
->orientation
= event
.pointer_data
->orientation
;
28 // TODO(sky): Add support for tool_type.
33 TouchHandler::TouchHandler(blink::WebView
* web_view
)
34 : web_view_(web_view
),
35 gesture_provider_(ui::GetGestureProviderConfig(
36 ui::GestureProviderConfigType::CURRENT_PLATFORM
),
40 TouchHandler::~TouchHandler() {
43 void TouchHandler::OnTouchEvent(const mojo::Event
& event
) {
44 if (!UpdateMotionEvent(event
))
47 SendMotionEventToGestureProvider();
49 PostProcessMotionEvent(event
);
52 void TouchHandler::OnGestureEvent(const ui::GestureEventData
& gesture
) {
53 blink::WebGestureEvent web_gesture
=
54 CreateWebGestureEventFromGestureEventData(gesture
);
55 // TODO(jdduke): Remove this workaround after Android fixes UiAutomator to
56 // stop providing shift meta values to synthetic MotionEvents. This prevents
57 // unintended shift+click interpretation of all accessibility clicks.
58 // See crbug.com/443247.
59 if (web_gesture
.type
== blink::WebInputEvent::GestureTap
&&
60 web_gesture
.modifiers
== blink::WebInputEvent::ShiftKey
) {
61 web_gesture
.modifiers
= 0;
63 web_view_
->handleInputEvent(web_gesture
);
66 bool TouchHandler::UpdateMotionEvent(const mojo::Event
& event
) {
67 ui::PointerProperties properties
;
68 SetPropertiesFromEvent(event
, &properties
);
70 const base::TimeTicks
timestamp(
71 base::TimeTicks::FromInternalValue(event
.time_stamp
));
72 if (current_motion_event_
.get()) {
73 current_motion_event_
->set_id(current_motion_event_
->GetId() + 1);
74 current_motion_event_
->set_event_time(timestamp
);
77 switch (event
.action
) {
78 case mojo::EVENT_TYPE_POINTER_DOWN
:
79 if (!current_motion_event_
.get()) {
80 current_motion_event_
.reset(new ui::MotionEventGeneric(
81 ui::MotionEvent::ACTION_DOWN
, timestamp
, properties
));
84 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
86 DVLOG(1) << "pointer down and pointer already down id="
90 current_motion_event_
->PushPointer(properties
);
91 current_motion_event_
->set_action(ui::MotionEvent::ACTION_POINTER_DOWN
);
92 current_motion_event_
->set_action_index(static_cast<int>(index
));
96 case mojo::EVENT_TYPE_POINTER_UP
: {
97 if (!current_motion_event_
.get()) {
98 DVLOG(1) << "pointer up with no event, id=" << properties
.id
;
102 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
104 DVLOG(1) << "pointer up and pointer not down id=" << properties
.id
;
107 current_motion_event_
->pointer(index
) = properties
;
108 current_motion_event_
->set_action(
109 current_motion_event_
->GetPointerCount() == 1
110 ? ui::MotionEvent::ACTION_UP
111 : ui::MotionEvent::ACTION_POINTER_UP
);
112 current_motion_event_
->set_action_index(static_cast<int>(index
));
116 case mojo::EVENT_TYPE_POINTER_MOVE
: {
117 if (!current_motion_event_
.get()) {
118 DVLOG(1) << "pointer move with no event, id=" << properties
.id
;
122 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
124 DVLOG(1) << "pointer move and pointer not down id=" << properties
.id
;
127 current_motion_event_
->pointer(index
) = properties
;
128 current_motion_event_
->set_action(ui::MotionEvent::ACTION_MOVE
);
129 current_motion_event_
->set_action_index(static_cast<int>(index
));
133 case mojo::EVENT_TYPE_POINTER_CANCEL
: {
134 if (!current_motion_event_
.get()) {
135 DVLOG(1) << "canel with no event, id=" << properties
.id
;
139 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
141 DVLOG(1) << "cancel and pointer not down id=" << properties
.id
;
144 current_motion_event_
->pointer(index
) = properties
;
145 current_motion_event_
->set_action(ui::MotionEvent::ACTION_CANCEL
);
146 current_motion_event_
->set_action_index(0);
156 void TouchHandler::SendMotionEventToGestureProvider() {
157 ui::FilteredGestureProvider::TouchHandlingResult result
=
158 gesture_provider_
.OnTouchEvent(*current_motion_event_
);
159 if (!result
.succeeded
)
162 blink::WebTouchEvent web_event
= ui::CreateWebTouchEventFromMotionEvent(
163 *current_motion_event_
, result
.did_generate_scroll
);
164 gesture_provider_
.OnSyncTouchEventAck(web_view_
->handleInputEvent(web_event
));
167 void TouchHandler::PostProcessMotionEvent(const mojo::Event
& event
) {
168 switch (event
.action
) {
169 case mojo::EVENT_TYPE_POINTER_UP
: {
170 const int index
= current_motion_event_
->FindPointerIndexOfId(
171 event
.pointer_data
->pointer_id
);
172 current_motion_event_
->RemovePointerAt(index
);
173 if (current_motion_event_
->GetPointerCount() == 0)
174 current_motion_event_
.reset();
178 case mojo::EVENT_TYPE_POINTER_CANCEL
:
179 current_motion_event_
.reset();
187 } // namespace html_viewer