OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "services/ui/input_manager/input_dispatcher_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "mojo/services/geometry/cpp/geometry_util.h" |
| 11 #include "mojo/services/gfx/composition/cpp/formatting.h" |
| 12 #include "mojo/services/ui/views/cpp/formatting.h" |
| 13 #include "services/ui/input_manager/input_associate.h" |
| 14 |
| 15 namespace input_manager { |
| 16 namespace { |
| 17 void TransformEvent(const mojo::Transform& transform, mojo::Event* event) { |
| 18 if (!event->pointer_data) |
| 19 return; |
| 20 mojo::Point point; |
| 21 point.x = event->pointer_data->x; |
| 22 point.y = event->pointer_data->y; |
| 23 point = TransformPoint(transform, point); |
| 24 event->pointer_data->x = point.x; |
| 25 event->pointer_data->y = point.y; |
| 26 } |
| 27 } // namespace |
| 28 |
| 29 InputDispatcherImpl::InputDispatcherImpl( |
| 30 InputAssociate* associate, |
| 31 mojo::ui::ViewTreeTokenPtr view_tree_token, |
| 32 mojo::InterfaceRequest<mojo::ui::InputDispatcher> request) |
| 33 : associate_(associate), |
| 34 view_tree_token_(view_tree_token.Pass()), |
| 35 hit_tester_( |
| 36 new mojo::ui::ViewTreeHitTesterClient(associate_->inspector(), |
| 37 view_tree_token_.Clone())), |
| 38 binding_(this, request.Pass()), |
| 39 weak_factory_(this) { |
| 40 DCHECK(associate_); |
| 41 DCHECK(view_tree_token_); |
| 42 |
| 43 binding_.set_connection_error_handler( |
| 44 base::Bind(&InputAssociate::OnInputDispatcherDied, |
| 45 base::Unretained(associate_), base::Unretained(this))); |
| 46 } |
| 47 |
| 48 InputDispatcherImpl::~InputDispatcherImpl() {} |
| 49 |
| 50 void InputDispatcherImpl::DispatchEvent(mojo::EventPtr event) { |
| 51 DCHECK(event); |
| 52 |
| 53 pending_events_.push(event.Pass()); |
| 54 if (pending_events_.size() == 1u) |
| 55 ProcessNextEvent(); |
| 56 } |
| 57 |
| 58 void InputDispatcherImpl::ProcessNextEvent() { |
| 59 DCHECK(!pending_events_.empty()); |
| 60 |
| 61 do { |
| 62 const mojo::Event* event = pending_events_.front().get(); |
| 63 if (event->action == mojo::EventType::POINTER_DOWN) { |
| 64 DCHECK(event->pointer_data); |
| 65 auto point = mojo::Point::New(); |
| 66 point->x = event->pointer_data->x; |
| 67 point->y = event->pointer_data->y; |
| 68 DVLOG(1) << "HitTest: point=" << point; |
| 69 hit_tester_->HitTest(point.Pass(), |
| 70 base::Bind(&InputDispatcherImpl::OnHitTestResult, |
| 71 weak_factory_.GetWeakPtr())); |
| 72 return; |
| 73 } |
| 74 DeliverEvent(pending_events_.front().Pass()); |
| 75 pending_events_.pop(); |
| 76 } while (!pending_events_.empty()); |
| 77 } |
| 78 |
| 79 void InputDispatcherImpl::DeliverEvent(mojo::EventPtr event) { |
| 80 if (focused_view_token_) { |
| 81 TransformEvent(*focused_view_transform_, event.get()); |
| 82 associate_->DeliverEvent(focused_view_token_.get(), event.Pass()); |
| 83 } |
| 84 } |
| 85 |
| 86 void InputDispatcherImpl::OnHitTestResult( |
| 87 scoped_ptr<mojo::ui::ResolvedHits> resolved_hits) { |
| 88 DCHECK(!pending_events_.empty()); |
| 89 DVLOG(1) << "OnHitTestResult: resolved_hits=" << resolved_hits.get(); |
| 90 |
| 91 // TODO(jeffbrown): Flesh out the input protocol so it makes sense to |
| 92 // look at more than the first hit. |
| 93 focused_view_token_.reset(); |
| 94 focused_view_transform_.reset(); |
| 95 if (resolved_hits && resolved_hits->result()->root) { |
| 96 mojo::gfx::composition::HitTestResultPtr result = |
| 97 resolved_hits->TakeResult(); |
| 98 const mojo::gfx::composition::SceneHit* scene = result->root.get(); |
| 99 for (;;) { |
| 100 DCHECK(scene->hits.size()); |
| 101 if (scene->hits[0]->is_node()) { |
| 102 auto it = resolved_hits->map().find(scene->scene_token->value); |
| 103 if (it != resolved_hits->map().end()) { |
| 104 focused_view_token_ = it->second.Clone(); |
| 105 focused_view_transform_ = |
| 106 scene->hits[0]->get_node()->transform.Pass(); |
| 107 } |
| 108 break; |
| 109 } |
| 110 DCHECK(scene->hits[0]->is_scene()); |
| 111 scene = scene->hits[0]->get_scene().get(); |
| 112 } |
| 113 } |
| 114 |
| 115 DVLOG(1) << "OnHitTestResult: focused_view_token_=" << focused_view_token_ |
| 116 << ", focused_view_transform_=" << focused_view_transform_; |
| 117 |
| 118 DeliverEvent(pending_events_.front().Pass()); |
| 119 pending_events_.pop(); |
| 120 |
| 121 if (!pending_events_.empty()) { |
| 122 // Prevent reentrance from ProcessNextEvent. |
| 123 base::MessageLoop::current()->PostTask( |
| 124 FROM_HERE, base::Bind(&InputDispatcherImpl::ProcessNextEvent, |
| 125 weak_factory_.GetWeakPtr())); |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace input_manager |
OLD | NEW |