| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/wm/maximize_mode/maximize_mode_event_blocker.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/wm/maximize_mode/internal_input_device_list.h" | |
| 9 #include "base/memory/scoped_ptr.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" | |
| 13 #include "ui/events/event_targeter.h" | |
| 14 #include "ui/events/keycodes/keyboard_codes.h" | |
| 15 #include "ui/gfx/point.h" | |
| 16 | |
| 17 #if defined(USE_X11) | |
| 18 #include "ash/wm/maximize_mode/internal_input_device_list_x11.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace ash { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Event targeter to prevent delivery of mouse and touchpad events while | |
| 26 // maximize mode is active. Other events such as touch are passed on to the | |
| 27 // default targeter. | |
| 28 class BlockKeyboardAndTouchpadTargeter : public ui::EventTargeter { | |
| 29 public: | |
| 30 BlockKeyboardAndTouchpadTargeter( | |
| 31 aura::Window* root_window, | |
| 32 MaximizeModeEventBlocker* event_blocker); | |
| 33 virtual ~BlockKeyboardAndTouchpadTargeter(); | |
| 34 | |
| 35 // Sets the default targeter to use when the event is not being blocked. | |
| 36 void SetDefaultTargeter(EventTargeter* default_targeter); | |
| 37 | |
| 38 // Overridden from ui::EventTargeter: | |
| 39 virtual ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, | |
| 40 ui::Event* event) OVERRIDE; | |
| 41 | |
| 42 private: | |
| 43 // A weak pointer to the root window on which this targeter will be set. The | |
| 44 // root window owns this targeter. | |
| 45 aura::Window* root_window_; | |
| 46 | |
| 47 // A weak pointer to the event blocker which owns the scoped targeter owning | |
| 48 // this targeter. | |
| 49 MaximizeModeEventBlocker* event_blocker_; | |
| 50 | |
| 51 // A weak pointer to the targeter this targeter is wrapping. The | |
| 52 // default_targeter is owned by the ScopedWindowTargeter which will be valid | |
| 53 // as long as this targeter is alive. | |
| 54 ui::EventTargeter* default_targeter_; | |
| 55 | |
| 56 // The last known mouse location to lock the cursor in place to when events | |
| 57 // come from the internal touchpad. | |
| 58 gfx::Point last_mouse_location_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(BlockKeyboardAndTouchpadTargeter); | |
| 61 }; | |
| 62 | |
| 63 BlockKeyboardAndTouchpadTargeter::BlockKeyboardAndTouchpadTargeter( | |
| 64 aura::Window* root_window, | |
| 65 MaximizeModeEventBlocker* event_blocker) | |
| 66 : root_window_(root_window), | |
| 67 event_blocker_(event_blocker), | |
| 68 default_targeter_(NULL), | |
| 69 last_mouse_location_(root_window->GetHost()->dispatcher()-> | |
| 70 GetLastMouseLocationInRoot()) { | |
| 71 } | |
| 72 | |
| 73 BlockKeyboardAndTouchpadTargeter::~BlockKeyboardAndTouchpadTargeter() { | |
| 74 } | |
| 75 | |
| 76 void BlockKeyboardAndTouchpadTargeter::SetDefaultTargeter( | |
| 77 ui::EventTargeter* default_targeter) { | |
| 78 default_targeter_ = default_targeter; | |
| 79 } | |
| 80 | |
| 81 ui::EventTarget* BlockKeyboardAndTouchpadTargeter::FindTargetForEvent( | |
| 82 ui::EventTarget* root, | |
| 83 ui::Event* event) { | |
| 84 bool internal_device = event_blocker_->internal_devices() && | |
| 85 event_blocker_->internal_devices()->IsEventFromInternalDevice(event); | |
| 86 if (event->IsMouseEvent()) { | |
| 87 if (internal_device) { | |
| 88 // The cursor movement is handled at a lower level which is not blocked. | |
| 89 // Move the mouse cursor back to its last known location resulting from | |
| 90 // an external mouse to prevent the internal touchpad from moving it. | |
| 91 root_window_->GetHost()->MoveCursorToHostLocation( | |
| 92 last_mouse_location_); | |
| 93 return NULL; | |
| 94 } else { | |
| 95 // Track the last location seen from an external mouse event. | |
| 96 last_mouse_location_ = | |
| 97 static_cast<ui::MouseEvent*>(event)->root_location(); | |
| 98 root_window_->GetHost()->ConvertPointToHost(&last_mouse_location_); | |
| 99 } | |
| 100 } else if (internal_device && (event->IsMouseWheelEvent() || | |
| 101 event->IsScrollEvent())) { | |
| 102 return NULL; | |
| 103 } else if (event->IsKeyEvent() && event->HasNativeEvent()) { | |
| 104 // TODO(flackr): Disable events only from the internal keyboard device | |
| 105 // when we begin using XI2 events for keyboard events | |
| 106 // (http://crbug.com/368750) and can tell which device the event is | |
| 107 // coming from, http://crbug.com/362881. | |
| 108 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(event); | |
| 109 if ((key_event->flags() & ui::EF_FUNCTION_KEY) || | |
| 110 (key_event->key_code() != ui::VKEY_VOLUME_DOWN && | |
| 111 key_event->key_code() != ui::VKEY_VOLUME_UP | |
| 112 #if defined(OS_CHROMEOS) | |
| 113 && key_event->key_code() != ui::VKEY_POWER | |
| 114 #endif | |
| 115 ) | |
| 116 ) { | |
| 117 return NULL; | |
| 118 } | |
| 119 } | |
| 120 return default_targeter_->FindTargetForEvent(root, event); | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 125 MaximizeModeEventBlocker::MaximizeModeEventBlocker() | |
| 126 #if defined(USE_X11) | |
| 127 : internal_devices_(new InternalInputDeviceListX11) | |
| 128 #endif | |
| 129 { | |
| 130 Shell::GetInstance()->AddShellObserver(this); | |
| 131 | |
| 132 // Hide the cursor as mouse events will be blocked. | |
| 133 aura::client::CursorClient* cursor_client_ = | |
| 134 aura::client::GetCursorClient(Shell::GetTargetRootWindow()); | |
| 135 if (cursor_client_) | |
| 136 cursor_client_->HideCursor(); | |
| 137 | |
| 138 // Block keyboard and mouse events on all existing and new root windows for | |
| 139 // the lifetime of this class. | |
| 140 aura::Window::Windows root_windows(Shell::GetAllRootWindows()); | |
| 141 for (aura::Window::Windows::iterator iter = root_windows.begin(); | |
| 142 iter != root_windows.end(); ++iter) { | |
| 143 AddEventTargeterOn(*iter); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 MaximizeModeEventBlocker::~MaximizeModeEventBlocker() { | |
| 148 Shell::GetInstance()->RemoveShellObserver(this); | |
| 149 } | |
| 150 | |
| 151 void MaximizeModeEventBlocker::OnRootWindowAdded(aura::Window* root_window) { | |
| 152 AddEventTargeterOn(root_window); | |
| 153 } | |
| 154 | |
| 155 void MaximizeModeEventBlocker::AddEventTargeterOn( | |
| 156 aura::Window* root_window) { | |
| 157 BlockKeyboardAndTouchpadTargeter* targeter = | |
| 158 new BlockKeyboardAndTouchpadTargeter(root_window, this); | |
| 159 aura::ScopedWindowTargeter* scoped_targeter = new aura::ScopedWindowTargeter( | |
| 160 root_window, scoped_ptr<ui::EventTargeter>(targeter)); | |
| 161 targeter->SetDefaultTargeter(scoped_targeter->old_targeter()); | |
| 162 targeters_.push_back(scoped_targeter); | |
| 163 } | |
| 164 | |
| 165 } // namespace ash | |
| OLD | NEW |