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