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 "components/html_viewer/touch_handler.h"
7 #include "third_party/WebKit/public/web/WebInputEvent.h"
8 #include "third_party/WebKit/public/web/WebWidget.h"
9 #include "ui/events/base_event_utils.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"
13 #include "ui/mojo/events/input_events.mojom.h"
15 namespace html_viewer
{
18 // TODO(rjkroege): Gesture recognition currently happens in the html_viewer.
19 // In phase2, it will be relocated to MUS. Update this code at that time.
20 void SetPropertiesFromEvent(const mojo::Event
& event
,
21 ui::PointerProperties
* properties
) {
22 properties
->id
= event
.pointer_data
->pointer_id
;
23 properties
->x
= event
.pointer_data
->location
->x
;
24 properties
->y
= event
.pointer_data
->location
->y
;
25 properties
->raw_x
= event
.pointer_data
->location
->screen_x
;
26 properties
->raw_y
= event
.pointer_data
->location
->screen_y
;
28 if (event
.pointer_data
->kind
== mojo::POINTER_KIND_TOUCH
||
29 event
.pointer_data
->kind
== mojo::POINTER_KIND_PEN
) {
30 properties
->pressure
= event
.pointer_data
->brush_data
->pressure
;
32 // TODO(rjkroege): vary orientation for width, height.
33 properties
->SetAxesAndOrientation(event
.pointer_data
->brush_data
->width
,
34 event
.pointer_data
->brush_data
->height
,
37 if (event
.flags
& mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON
||
38 event
.flags
& mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON
||
39 event
.flags
& mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON
) {
40 properties
->pressure
= 0.5;
42 properties
->pressure
= 0.0;
45 // TODO(sky): Add support for tool_type.
50 TouchHandler::TouchHandler(blink::WebWidget
* web_widget
)
51 : web_widget_(web_widget
),
52 gesture_provider_(ui::GetGestureProviderConfig(
53 ui::GestureProviderConfigType::CURRENT_PLATFORM
),
57 TouchHandler::~TouchHandler() {
60 void TouchHandler::OnTouchEvent(const mojo::Event
& event
) {
61 if (!UpdateMotionEvent(event
))
64 SendMotionEventToGestureProvider();
66 PostProcessMotionEvent(event
);
69 void TouchHandler::OnGestureEvent(const ui::GestureEventData
& gesture
) {
70 blink::WebGestureEvent web_gesture
=
71 CreateWebGestureEventFromGestureEventData(gesture
);
72 // TODO(jdduke): Remove this workaround after Android fixes UiAutomator to
73 // stop providing shift meta values to synthetic MotionEvents. This prevents
74 // unintended shift+click interpretation of all accessibility clicks.
75 // See crbug.com/443247.
76 if (web_gesture
.type
== blink::WebInputEvent::GestureTap
&&
77 web_gesture
.modifiers
== blink::WebInputEvent::ShiftKey
) {
78 web_gesture
.modifiers
= 0;
80 web_widget_
->handleInputEvent(web_gesture
);
83 bool TouchHandler::UpdateMotionEvent(const mojo::Event
& event
) {
84 ui::PointerProperties properties
;
85 SetPropertiesFromEvent(event
, &properties
);
87 const base::TimeTicks
timestamp(
88 base::TimeTicks::FromInternalValue(event
.time_stamp
));
89 if (current_motion_event_
.get()) {
90 current_motion_event_
->set_unique_event_id(ui::GetNextTouchEventId());
91 current_motion_event_
->set_event_time(timestamp
);
94 switch (event
.action
) {
95 case mojo::EVENT_TYPE_POINTER_DOWN
:
96 if (!current_motion_event_
.get()) {
97 current_motion_event_
.reset(new ui::MotionEventGeneric(
98 ui::MotionEvent::ACTION_DOWN
, timestamp
, properties
));
101 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
103 DVLOG(1) << "pointer down and pointer already down id="
107 current_motion_event_
->PushPointer(properties
);
108 current_motion_event_
->set_action(ui::MotionEvent::ACTION_POINTER_DOWN
);
109 current_motion_event_
->set_action_index(static_cast<int>(index
));
113 case mojo::EVENT_TYPE_POINTER_UP
: {
114 if (!current_motion_event_
.get()) {
115 DVLOG(1) << "pointer up with no event, id=" << properties
.id
;
119 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
121 DVLOG(1) << "pointer up and pointer not down id=" << properties
.id
;
124 current_motion_event_
->pointer(index
) = properties
;
125 current_motion_event_
->set_action(
126 current_motion_event_
->GetPointerCount() == 1
127 ? ui::MotionEvent::ACTION_UP
128 : ui::MotionEvent::ACTION_POINTER_UP
);
129 current_motion_event_
->set_action_index(static_cast<int>(index
));
133 case mojo::EVENT_TYPE_POINTER_MOVE
: {
134 if (!current_motion_event_
.get()) {
135 DVLOG(1) << "pointer move with no event, id=" << properties
.id
;
139 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
141 DVLOG(1) << "pointer move and pointer not down id=" << properties
.id
;
144 current_motion_event_
->pointer(index
) = properties
;
145 current_motion_event_
->set_action(ui::MotionEvent::ACTION_MOVE
);
146 current_motion_event_
->set_action_index(static_cast<int>(index
));
150 case mojo::EVENT_TYPE_POINTER_CANCEL
: {
151 if (!current_motion_event_
.get()) {
152 DVLOG(1) << "canel with no event, id=" << properties
.id
;
156 current_motion_event_
->FindPointerIndexOfId(properties
.id
);
158 DVLOG(1) << "cancel and pointer not down id=" << properties
.id
;
161 current_motion_event_
->pointer(index
) = properties
;
162 current_motion_event_
->set_action(ui::MotionEvent::ACTION_CANCEL
);
163 current_motion_event_
->set_action_index(0);
173 void TouchHandler::SendMotionEventToGestureProvider() {
174 ui::FilteredGestureProvider::TouchHandlingResult result
=
175 gesture_provider_
.OnTouchEvent(*current_motion_event_
);
176 if (!result
.succeeded
)
179 blink::WebTouchEvent web_event
= ui::CreateWebTouchEventFromMotionEvent(
180 *current_motion_event_
, result
.did_generate_scroll
);
181 gesture_provider_
.OnTouchEventAck(web_event
.uniqueTouchEventId
,
182 web_widget_
->handleInputEvent(web_event
));
185 void TouchHandler::PostProcessMotionEvent(const mojo::Event
& event
) {
186 switch (event
.action
) {
187 case mojo::EVENT_TYPE_POINTER_UP
: {
188 const int index
= current_motion_event_
->FindPointerIndexOfId(
189 event
.pointer_data
->pointer_id
);
190 current_motion_event_
->RemovePointerAt(index
);
191 if (current_motion_event_
->GetPointerCount() == 0)
192 current_motion_event_
.reset();
196 case mojo::EVENT_TYPE_POINTER_CANCEL
:
197 current_motion_event_
.reset();
205 } // namespace html_viewer