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