Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/maximize_mode/maximize_mode_event_blocker.h" | 5 #include "ash/wm/maximize_mode/maximize_mode_event_blocker.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_util.h" | |
| 9 #include "ui/aura/client/cursor_client.h" | 10 #include "ui/aura/client/cursor_client.h" |
| 11 #include "ui/aura/window_event_dispatcher.h" | |
| 12 #include "ui/aura/window_tree_host.h" | |
| 10 #include "ui/events/event_targeter.h" | 13 #include "ui/events/event_targeter.h" |
| 14 #include "ui/gfx/point.h" | |
| 15 | |
| 16 #if defined(USE_X11) | |
| 17 #include <X11/extensions/XInput2.h> | |
| 18 #include <X11/Xlib.h> | |
| 19 | |
| 20 #include "ui/events/x/device_data_manager.h" | |
| 21 #include "ui/events/x/device_list_cache_x.h" | |
| 22 #include "ui/gfx/x/x11_types.h" | |
| 23 #endif | |
| 11 | 24 |
| 12 namespace ash { | 25 namespace ash { |
| 13 | 26 |
| 14 namespace { | 27 namespace { |
| 15 | 28 |
| 29 #if defined(USE_X11) | |
| 30 // The name of the xinput device corresponding to the internal touchpad. | |
| 31 const char kInternalTouchpadName[] = "Elan Touchpad"; | |
| 32 #endif | |
| 33 | |
| 16 // Event targeter to prevent delivery of mouse and touchpad events while | 34 // Event targeter to prevent delivery of mouse and touchpad events while |
| 17 // maximize mode is active. Other events such as touch are passed on to the | 35 // maximize mode is active. Other events such as touch are passed on to the |
| 18 // default targeter. | 36 // default targeter. |
| 19 // TODO(flackr): This should only stop events from the internal keyboard and | |
| 20 // touchpad. | |
| 21 class BlockKeyboardAndTouchpadTargeter : public ui::EventTargeter { | 37 class BlockKeyboardAndTouchpadTargeter : public ui::EventTargeter { |
| 22 public: | 38 public: |
| 23 BlockKeyboardAndTouchpadTargeter(); | 39 BlockKeyboardAndTouchpadTargeter(aura::Window* root_window, |
| 40 std::set<int>* internal_device_ids); | |
| 24 virtual ~BlockKeyboardAndTouchpadTargeter(); | 41 virtual ~BlockKeyboardAndTouchpadTargeter(); |
| 25 | 42 |
| 26 // Sets the default targeter to use when the event is not being blocked. | 43 // Sets the default targeter to use when the event is not being blocked. |
| 27 void SetDefaultTargeter(EventTargeter* default_targeter); | 44 void SetDefaultTargeter(EventTargeter* default_targeter); |
| 28 | 45 |
| 29 // Overridden from ui::EventTargeter: | 46 // Overridden from ui::EventTargeter: |
| 30 virtual ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, | 47 virtual ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, |
| 31 ui::Event* event) OVERRIDE; | 48 ui::Event* event) OVERRIDE; |
| 32 | 49 |
| 33 private: | 50 private: |
| 51 // A weak pointer to the root window on which this targeter will be set. | |
| 52 aura::Window* root_window_; | |
|
sadrul
2014/05/07 13:59:23
We expect that the root-window will outlive this t
flackr
2014/05/14 16:46:56
Yes, commented.
| |
| 53 | |
| 54 // A weak pointer to the set of device ids to block. | |
| 55 std::set<int>* internal_device_ids_; | |
| 56 | |
| 34 // A weak pointer to the targeter this targeter is wrapping. The | 57 // A weak pointer to the targeter this targeter is wrapping. The |
| 35 // default_targeter is owned by the ScopedWindowTargeter which will be valid | 58 // default_targeter is owned by the ScopedWindowTargeter which will be valid |
| 36 // as long as this targeter is alive. | 59 // as long as this targeter is alive. |
| 37 ui::EventTargeter* default_targeter_; | 60 ui::EventTargeter* default_targeter_; |
| 38 | 61 |
| 62 // The last known mouse location to lock the cursor in place to when events | |
| 63 // come from the internal touchpad. | |
| 64 gfx::Point last_mouse_location_; | |
| 65 | |
| 39 DISALLOW_COPY_AND_ASSIGN(BlockKeyboardAndTouchpadTargeter); | 66 DISALLOW_COPY_AND_ASSIGN(BlockKeyboardAndTouchpadTargeter); |
| 40 }; | 67 }; |
| 41 | 68 |
| 42 BlockKeyboardAndTouchpadTargeter::BlockKeyboardAndTouchpadTargeter() | 69 BlockKeyboardAndTouchpadTargeter::BlockKeyboardAndTouchpadTargeter( |
| 43 : default_targeter_(NULL) { | 70 aura::Window* root_window, |
| 71 std::set<int>* internal_device_ids) | |
| 72 : root_window_(root_window), | |
| 73 internal_device_ids_(internal_device_ids), | |
| 74 default_targeter_(NULL), | |
| 75 last_mouse_location_(root_window->GetHost()->dispatcher()-> | |
| 76 GetLastMouseLocationInRoot()) { | |
| 44 } | 77 } |
| 45 | 78 |
| 46 BlockKeyboardAndTouchpadTargeter::~BlockKeyboardAndTouchpadTargeter() { | 79 BlockKeyboardAndTouchpadTargeter::~BlockKeyboardAndTouchpadTargeter() { |
| 47 } | 80 } |
| 48 | 81 |
| 49 void BlockKeyboardAndTouchpadTargeter::SetDefaultTargeter( | 82 void BlockKeyboardAndTouchpadTargeter::SetDefaultTargeter( |
| 50 ui::EventTargeter* default_targeter) { | 83 ui::EventTargeter* default_targeter) { |
| 51 default_targeter_ = default_targeter; | 84 default_targeter_ = default_targeter; |
| 52 } | 85 } |
| 53 | 86 |
| 54 ui::EventTarget* BlockKeyboardAndTouchpadTargeter::FindTargetForEvent( | 87 ui::EventTarget* BlockKeyboardAndTouchpadTargeter::FindTargetForEvent( |
| 55 ui::EventTarget* root, | 88 ui::EventTarget* root, |
| 56 ui::Event* event) { | 89 ui::Event* event) { |
| 57 if (event->HasNativeEvent() && (event->IsMouseEvent() || event->IsKeyEvent())) | 90 if (event->HasNativeEvent()) { |
| 58 return NULL; | 91 if (event->IsMouseEvent()) { |
| 92 #if defined(USE_X11) | |
| 93 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>( | |
| 94 event->native_event()->xcookie.data); | |
| 95 if (internal_device_ids_->find(xiev->sourceid) != | |
| 96 internal_device_ids_->end()) { | |
| 97 root_window_->GetHost()->MoveCursorToHostLocation( | |
| 98 last_mouse_location_); | |
|
sadrul
2014/05/07 13:59:23
Add a comment here about why we are doing this (it
flackr
2014/05/14 16:46:56
Done.
| |
| 99 return NULL; | |
| 100 } else { | |
| 101 // Track the last location seen from an external mouse event. | |
| 102 last_mouse_location_ = static_cast<ui::MouseEvent*>(event)->location(); | |
| 103 root_window_->GetHost()->GetRootTransform().TransformPoint( | |
| 104 &last_mouse_location_); | |
|
sadrul
2014/05/07 13:59:23
Use WindowTreeHost::ConvertPointToHost on the even
flackr
2014/05/14 16:46:56
Done.
| |
| 105 } | |
| 106 #else | |
| 107 return NULL; | |
| 108 #endif | |
| 109 } else if (event->IsKeyEvent()) { | |
| 110 // TODO(flackr): Disable events only from the internal keyboard device | |
| 111 // when we begin using XI2 events for keyboard events | |
| 112 // (http://crbug.com/368750) and can tell which device the event is | |
| 113 // coming from, http://crbug.com/362881. | |
| 114 return NULL; | |
| 115 } | |
| 116 } | |
| 59 return default_targeter_->FindTargetForEvent(root, event); | 117 return default_targeter_->FindTargetForEvent(root, event); |
| 60 } | 118 } |
| 61 | 119 |
| 62 } // namespace | 120 } // namespace |
| 63 | 121 |
| 64 MaximizeModeEventBlocker::MaximizeModeEventBlocker() { | 122 MaximizeModeEventBlocker::MaximizeModeEventBlocker() { |
| 65 Shell::GetInstance()->AddShellObserver(this); | 123 Shell::GetInstance()->AddShellObserver(this); |
| 66 | 124 |
| 125 #if defined(USE_X11) | |
| 126 if (ui::DeviceDataManager::GetInstance()->IsXInput2Available()) { | |
| 127 XIDeviceList xi_dev_list = ui::DeviceListCacheX::GetInstance()-> | |
| 128 GetXI2DeviceList(gfx::GetXDisplay()); | |
| 129 for (int i = 0; i < xi_dev_list.count; ++i) { | |
| 130 std::string device_name(xi_dev_list[i].name); | |
| 131 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING, &device_name); | |
|
sadrul
2014/05/07 13:59:23
How safe is it to assume that the name is ASCII?
flackr
2014/05/14 16:46:56
It should be safe, TrimWhitespaceASCII uses std::s
| |
| 132 if (device_name == kInternalTouchpadName) | |
| 133 internal_device_ids_.insert(xi_dev_list[i].deviceid); | |
| 134 } | |
| 135 } | |
| 136 #endif | |
| 137 | |
| 67 // Hide the cursor as mouse events will be blocked. | 138 // Hide the cursor as mouse events will be blocked. |
| 68 aura::client::CursorClient* cursor_client_ = | 139 aura::client::CursorClient* cursor_client_ = |
| 69 aura::client::GetCursorClient(Shell::GetTargetRootWindow()); | 140 aura::client::GetCursorClient(Shell::GetTargetRootWindow()); |
| 70 if (cursor_client_) | 141 if (cursor_client_) |
| 71 cursor_client_->HideCursor(); | 142 cursor_client_->HideCursor(); |
| 72 | 143 |
| 73 // Block keyboard and mouse events on all existing and new root windows for | 144 // Block keyboard and mouse events on all existing and new root windows for |
| 74 // the lifetime of this class. | 145 // the lifetime of this class. |
| 75 aura::Window::Windows root_windows(Shell::GetAllRootWindows()); | 146 aura::Window::Windows root_windows(Shell::GetAllRootWindows()); |
| 76 for (aura::Window::Windows::iterator iter = root_windows.begin(); | 147 for (aura::Window::Windows::iterator iter = root_windows.begin(); |
| 77 iter != root_windows.end(); ++iter) { | 148 iter != root_windows.end(); ++iter) { |
| 78 AddEventTargeterOn(*iter); | 149 AddEventTargeterOn(*iter); |
| 79 } | 150 } |
| 80 } | 151 } |
| 81 | 152 |
| 82 MaximizeModeEventBlocker::~MaximizeModeEventBlocker() { | 153 MaximizeModeEventBlocker::~MaximizeModeEventBlocker() { |
| 83 Shell::GetInstance()->RemoveShellObserver(this); | 154 Shell::GetInstance()->RemoveShellObserver(this); |
| 84 } | 155 } |
| 85 | 156 |
| 86 void MaximizeModeEventBlocker::OnRootWindowAdded(aura::Window* root_window) { | 157 void MaximizeModeEventBlocker::OnRootWindowAdded(aura::Window* root_window) { |
| 87 AddEventTargeterOn(root_window); | 158 AddEventTargeterOn(root_window); |
| 88 } | 159 } |
| 89 | 160 |
| 90 void MaximizeModeEventBlocker::AddEventTargeterOn( | 161 void MaximizeModeEventBlocker::AddEventTargeterOn( |
| 91 aura::Window* root_window) { | 162 aura::Window* root_window) { |
| 92 BlockKeyboardAndTouchpadTargeter* targeter = | 163 BlockKeyboardAndTouchpadTargeter* targeter = |
| 93 new BlockKeyboardAndTouchpadTargeter(); | 164 new BlockKeyboardAndTouchpadTargeter(root_window, |
| 165 &internal_device_ids_); | |
| 94 aura::ScopedWindowTargeter* scoped_targeter = new aura::ScopedWindowTargeter( | 166 aura::ScopedWindowTargeter* scoped_targeter = new aura::ScopedWindowTargeter( |
| 95 root_window, scoped_ptr<ui::EventTargeter>(targeter)); | 167 root_window, scoped_ptr<ui::EventTargeter>(targeter)); |
| 96 targeter->SetDefaultTargeter(scoped_targeter->old_targeter()); | 168 targeter->SetDefaultTargeter(scoped_targeter->old_targeter()); |
| 97 targeters_.push_back(scoped_targeter); | 169 targeters_.push_back(scoped_targeter); |
| 98 } | 170 } |
| 99 | 171 |
| 100 } // namespace ash | 172 } // namespace ash |
| OLD | NEW |