Chromium Code Reviews| Index: ash/touch/touch_exploration_controller.cc |
| diff --git a/ash/touch/touch_exploration_controller.cc b/ash/touch/touch_exploration_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1dc6f797c89d6ae2ac01483c5925857e2de2c242 |
| --- /dev/null |
| +++ b/ash/touch/touch_exploration_controller.cc |
| @@ -0,0 +1,184 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/touch/touch_exploration_controller.h" |
| + |
| +#include "ash/root_window_settings.h" |
| +#include "ash/shell.h" |
| +#include "ash/wm/coordinate_conversion.h" |
| +#include "base/logging.h" |
| +#include "ui/aura/client/cursor_client.h" |
| +#include "ui/aura/window_tree_host.h" |
| +#include "ui/events/event.h" |
| +#include "ui/gfx/point.h" |
| + |
| +namespace ash { |
| + |
| +TouchExplorationController::TouchExplorationController( |
| + aura::Window* root_window) : |
| + display_id_(GetRootWindowSettings(root_window)->display_id), |
| + root_window_(root_window) { |
| + Shell::GetInstance()->display_controller()->AddObserver(this); |
| + ui::EventSource* event_source = root_window->GetHost()->GetEventSource(); |
| + event_source->AddEventRewriter(this); |
| +} |
| + |
| +TouchExplorationController::~TouchExplorationController() { |
| + Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| + if (root_window_) |
| + root_window_->GetHost()->GetEventSource()->RemoveEventRewriter(this); |
| +} |
| + |
| +ui::EventRewriteStatus TouchExplorationController::RewriteEvent( |
| + const ui::Event& event, |
| + scoped_ptr<ui::Event>* rewritten_event) { |
| + if (!event.IsTouchEvent()) { |
| + return ui::EVENT_REWRITE_CONTINUE; |
| + } |
| + ui::EventRewriteStatus result; |
| + const ui::TouchEvent& touch_event = |
| + static_cast<const ui::TouchEvent&> (event); |
| + ui::EventType type = touch_event.type(); |
| + gfx::Point location = touch_event.location(); |
| + int touch_id = touch_event.touch_id(); |
| + int flags = touch_event.flags(); |
| + |
| + if (type == ui::ET_TOUCH_PRESSED) { |
| + LOG(ERROR) << "ET_TOUCH_PRESSED"; |
| + // If this is the first and only finger touching rewrite the touch as a |
| + // mouse move. |
| + if (touch_ids_.size() == 0) { |
| + result = ui::EVENT_REWRITE_REWRITTEN; |
| + rewritten_event->reset(CreateMouseMoveEvent(location, flags)); |
| + aura::client::CursorClient* cursor_client = |
| + root_window_ ? aura::client::GetCursorClient(root_window_) : NULL; |
| + if (cursor_client) { |
| + cursor_client->EnableMouseEvents(); |
|
mfomitchev
2014/04/16 19:32:17
This makes mouse events go through
|
| + cursor_client->HideCursor(); |
|
mfomitchev
2014/04/16 19:32:17
Don't think we want to see the cursor while touchi
|
| + } |
| + |
| + // Otherwise let the touch go through. |
| + } else { |
| + result = ui::EVENT_REWRITE_CONTINUE; |
| + } |
| + touch_ids_.push_back(touch_id); |
| + touch_locations_.insert(std::pair<int, gfx::Point>(touch_id, location)); |
| + } else if ((type == ui::ET_TOUCH_RELEASED || |
| + type == ui::ET_TOUCH_CANCELLED)) { |
| + LOG(ERROR) << (type == ui::ET_TOUCH_RELEASED ? |
| + "ET_TOUCH_RELEASED" : "ET_TOUCH_CANCELLED"); |
| + std::vector<int>::iterator it = |
| + std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); |
| + if (it != touch_ids_.end()) { |
| + if (it != touch_ids_.begin()) { |
| + // If it wasn't the first finger that was lifted, leave the event as is. |
| + result = ui::EVENT_REWRITE_CONTINUE; |
| + } else { |
| + // If it was the first finger that was lifted, we need to do a few |
| + // things depending on how many fingers were on the screen: |
| + // 1. If only one finger was on the screen, this is simply equivalent to |
| + // the end of the mouse move. |
| + // 2. If there were multiple fingers on the screen, this is equivalent |
| + // to lifting the _second_ finger. Also, if there were exactly two |
| + // fingers touching, we are only left with one, so we are entering |
| + // the mouse move mode, and so we should also generate a mouse move. |
| + if (touch_ids_.size() == 1) { |
| + result = ui::EVENT_REWRITE_REWRITTEN; |
| + rewritten_event->reset(CreateMouseMoveEvent(location, flags)); |
| + } else { |
| + LOG(ERROR) << "Rewriting as a touch release event"; |
| + // Rewrite the release of the first finger as a release of the |
| + // second finger. |
| + int second_touch_id = touch_ids_.at(1); |
| + gfx::Point second_touch_location = touch_locations_[second_touch_id]; |
| + ui::TouchEvent* touch_release_event = new ui::TouchEvent( |
| + ui::ET_TOUCH_RELEASED, |
| + second_touch_location, |
| + second_touch_id, |
| + event.time_stamp()); |
| + rewritten_event->reset(touch_release_event); |
| + if (touch_ids_.size() == 2) { |
| + // If there were exactly two fingers touching - also dispatch |
| + // a mouse move corresponding to the second finger's location. |
| + result = ui::EVENT_REWRITE_DISPATCH_ANOTHER; |
| + next_dispatch_event_.reset( |
| + CreateMouseMoveEvent(second_touch_location, flags)); |
| + } else { |
| + result = ui::EVENT_REWRITE_REWRITTEN; |
| + } |
| + } |
| + } |
| + touch_ids_.erase(it); |
| + touch_locations_.erase(touch_id); |
| + } else { |
| + NOTREACHED(); |
| + result = ui::EVENT_REWRITE_CONTINUE; |
| + } |
| + } else if (type == ui::ET_TOUCH_MOVED) { |
| + LOG(ERROR) << "ET_TOUCH_MOVED"; |
| + // Consume if it is the first finger. |
| + // If it is the the first and only finger - generate a mouse move event. |
| + std::vector<int>::iterator it = |
| + std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); |
| + if (it != touch_ids_.end()) { |
| + if (it != touch_ids_.begin()) { |
| + result = ui::EVENT_REWRITE_CONTINUE; |
| + } else { |
| + if (touch_ids_.size() == 1) { |
| + result = ui::EVENT_REWRITE_REWRITTEN; |
| + rewritten_event->reset(CreateMouseMoveEvent(location, flags)); |
| + } else { |
| + result = ui::EVENT_REWRITE_DISCARD; |
| + } |
| + } |
| + touch_locations_[*it] = location; |
| + } else { |
| + NOTREACHED(); |
| + result = ui::EVENT_REWRITE_CONTINUE; |
| + } |
| + } else { |
| + result = ui::EVENT_REWRITE_CONTINUE; |
| + } |
| + return result; |
| +} |
| + |
| +ui::EventRewriteStatus TouchExplorationController::NextDispatchEvent( |
| + const ui::Event& last_event, |
| + scoped_ptr<ui::Event>* new_event) { |
| + new_event->reset(next_dispatch_event_.release()); |
| + return ui::EVENT_REWRITE_REWRITTEN; |
| +} |
| + |
| +void TouchExplorationController::OnDisplaysInitialized() { |
| + OnDisplayConfigurationChanged(); |
| +} |
| + |
| +void TouchExplorationController::OnDisplayConfigurationChanging() { |
| + if (!root_window_) |
| + return; |
| + |
| + root_window_->GetHost()->GetEventSource()->RemoveEventRewriter(this); |
| + root_window_ = NULL; |
| +} |
| + |
| +void TouchExplorationController::OnDisplayConfigurationChanged() { |
| + if (root_window_) |
| + return; |
| + |
| + root_window_ = Shell::GetInstance()->display_controller()-> |
| + GetRootWindowForDisplayId(display_id_); |
| + root_window_->GetHost()->GetEventSource()->AddEventRewriter(this); |
| +} |
| + |
| +ui::Event* TouchExplorationController::CreateMouseMoveEvent( |
| + gfx::Point location, int flags) { |
| + LOG(ERROR) << "Creating mouse move event"; |
| + return new ui::MouseEvent(ui::ET_MOUSE_MOVED, |
| + location, |
| + location, |
| + ui::EF_IS_SYNTHESIZED | flags, |
| + 0); |
| +} |
| + |
| +} // namespace ash |