OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/widget/desktop_aura/x11_whole_screen_move_loop.h" | 5 #include "ui/views/widget/desktop_aura/x11_whole_screen_move_loop.h" |
6 | 6 |
7 #include <X11/extensions/XInput2.h> | |
7 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
8 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. | 9 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. |
9 #undef RootWindow | 10 #undef RootWindow |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/aura/env.h" | 16 #include "ui/aura/env.h" |
16 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
17 #include "ui/aura/window_event_dispatcher.h" | 18 #include "ui/aura/window_event_dispatcher.h" |
18 #include "ui/aura/window_tree_host.h" | 19 #include "ui/aura/window_tree_host.h" |
19 #include "ui/base/x/x11_util.h" | 20 #include "ui/base/x/x11_util.h" |
20 #include "ui/events/event.h" | 21 #include "ui/events/event.h" |
22 #include "ui/events/event_utils.h" | |
21 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 23 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
22 #include "ui/events/platform/x11/x11_event_source.h" | 24 #include "ui/events/platform/x11/x11_event_source.h" |
23 #include "ui/gfx/point_conversions.h" | 25 #include "ui/gfx/point_conversions.h" |
24 #include "ui/gfx/screen.h" | 26 #include "ui/gfx/screen.h" |
25 #include "ui/views/controls/image_view.h" | 27 #include "ui/views/controls/image_view.h" |
28 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h" | |
26 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
27 | 30 |
28 namespace views { | 31 namespace views { |
29 | 32 |
30 namespace { | 33 namespace { |
31 | 34 |
32 // The minimum alpha before we declare a pixel transparent when searching in | 35 // The minimum alpha before we declare a pixel transparent when searching in |
33 // our source image. | 36 // our source image. |
34 const uint32 kMinAlpha = 32; | 37 const uint32 kMinAlpha = 32; |
35 const unsigned char kDragWidgetOpacity = 0xc0; | 38 const unsigned char kDragWidgetOpacity = 0xc0; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 weak_factory_.InvalidateWeakPtrs(); | 75 weak_factory_.InvalidateWeakPtrs(); |
73 DCHECK_EQ(MotionNotify, last_xmotion_.type); | 76 DCHECK_EQ(MotionNotify, last_xmotion_.type); |
74 delegate_->OnMouseMovement(&last_xmotion_); | 77 delegate_->OnMouseMovement(&last_xmotion_); |
75 last_xmotion_.type = LASTEvent; | 78 last_xmotion_.type = LASTEvent; |
76 } | 79 } |
77 | 80 |
78 //////////////////////////////////////////////////////////////////////////////// | 81 //////////////////////////////////////////////////////////////////////////////// |
79 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: | 82 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: |
80 | 83 |
81 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { | 84 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { |
82 return event->xany.window == grab_input_window_; | 85 return in_move_loop_; |
83 } | 86 } |
84 | 87 |
85 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { | 88 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { |
89 if (!in_move_loop_) | |
90 return ui::POST_DISPATCH_PERFORM_DEFAULT; | |
86 XEvent* xev = event; | 91 XEvent* xev = event; |
87 | 92 |
88 // Note: the escape key is handled in the tab drag controller, which has | 93 // Note: the escape key is handled in the tab drag controller, which has |
89 // keyboard focus even though we took pointer grab. | 94 // keyboard focus even though we took pointer grab. |
90 switch (xev->type) { | 95 switch (xev->type) { |
91 case MotionNotify: { | 96 case MotionNotify: { |
97 LOG(INFO) << "MotionNotify in " << grab_input_window_; | |
92 if (drag_widget_.get()) { | 98 if (drag_widget_.get()) { |
93 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | 99 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); |
94 gfx::Point location = gfx::ToFlooredPoint( | 100 gfx::Point location = gfx::ToFlooredPoint( |
95 screen->GetCursorScreenPoint() - drag_offset_); | 101 screen->GetCursorScreenPoint() - drag_offset_); |
96 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); | 102 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); |
97 drag_widget_->StackAtTop(); | 103 drag_widget_->StackAtTop(); |
98 } | 104 } |
99 last_xmotion_ = xev->xmotion; | 105 last_xmotion_ = xev->xmotion; |
100 if (!weak_factory_.HasWeakPtrs()) { | 106 if (!weak_factory_.HasWeakPtrs()) { |
101 // Post a task to dispatch mouse movement event when control returns to | 107 // Post a task to dispatch mouse movement event when control returns to |
102 // the message loop. This allows smoother dragging since the events are | 108 // the message loop. This allows smoother dragging since the events are |
103 // dispatched without waiting for the drag widget updates. | 109 // dispatched without waiting for the drag widget updates. |
104 base::MessageLoopForUI::current()->PostTask( | 110 base::MessageLoopForUI::current()->PostTask( |
105 FROM_HERE, | 111 FROM_HERE, |
106 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, | 112 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, |
107 weak_factory_.GetWeakPtr())); | 113 weak_factory_.GetWeakPtr())); |
108 } | 114 } |
109 break; | 115 return ui::POST_DISPATCH_NONE; |
110 } | 116 } |
111 case ButtonRelease: { | 117 case ButtonRelease: { |
112 if (xev->xbutton.button == Button1) { | 118 if (xev->xbutton.button == Button1) { |
119 LOG(INFO) << "Release in " << grab_input_window_; | |
113 // Assume that drags are being done with the left mouse button. Only | 120 // Assume that drags are being done with the left mouse button. Only |
114 // break the drag if the left mouse button was released. | 121 // break the drag if the left mouse button was released. |
115 DispatchMouseMovement(); | 122 DispatchMouseMovement(); |
116 delegate_->OnMouseReleased(); | 123 delegate_->OnMouseReleased(); |
117 } | 124 } |
118 break; | 125 return ui::POST_DISPATCH_NONE; |
119 } | 126 } |
120 case KeyPress: { | 127 case KeyPress: { |
121 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) { | 128 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) { |
122 canceled_ = true; | 129 canceled_ = true; |
123 EndMoveLoop(); | 130 EndMoveLoop(); |
131 return ui::POST_DISPATCH_NONE; | |
124 } | 132 } |
125 break; | 133 break; |
126 } | 134 } |
135 case GenericEvent: { | |
136 ui::EventType type = ui::EventTypeFromNative(xev); | |
137 switch (type) { | |
138 case ui::ET_MOUSE_MOVED: | |
139 case ui::ET_MOUSE_DRAGGED: | |
140 case ui::ET_MOUSE_RELEASED: { | |
141 LOG(INFO) << "XI event " << type << " in " << grab_input_window_; | |
142 XEvent xevent = {0}; | |
143 if (type == ui::ET_MOUSE_RELEASED) { | |
144 xevent.type = ButtonRelease; | |
145 xevent.xbutton.button = ui::EventButtonFromNative(xev); | |
146 } else { | |
147 xevent.type = MotionNotify; | |
148 } | |
149 xevent.xany.display = xev->xgeneric.display; | |
150 xevent.xany.window = grab_input_window_; | |
151 // The fields used below are in the same place for all of events | |
152 // above. Using xmotion from XEvent's unions to avoid repeating | |
153 // the code. | |
154 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display); | |
155 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds(); | |
156 gfx::Point point(ui::EventSystemLocationFromNative(xev)); | |
157 xevent.xmotion.x_root = point.x(); | |
158 xevent.xmotion.y_root = point.y(); | |
159 DispatchEvent(&xevent); | |
sadrul
2014/04/25 17:19:50
It would be really great if we didn't have to recr
varkha
2014/04/25 18:00:35
We talked about this offline. I think it would be
| |
160 return ui::POST_DISPATCH_NONE; | |
161 } | |
162 default: | |
163 break; | |
164 } | |
165 } | |
127 } | 166 } |
128 | 167 |
129 return ui::POST_DISPATCH_STOP_PROPAGATION; | 168 return (event->xany.window == grab_input_window_) ? |
169 ui::POST_DISPATCH_NONE : ui::POST_DISPATCH_PERFORM_DEFAULT; | |
130 } | 170 } |
131 | 171 |
132 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, | 172 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, |
133 gfx::NativeCursor cursor) { | 173 gfx::NativeCursor cursor) { |
134 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 174 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
135 | 175 |
136 // Start a capture on the host, so that it continues to receive events during | 176 // Start a capture on the host, so that it continues to receive events during |
137 // the drag. This may be second time we are capturing the mouse events - the | 177 // the drag. This may be second time we are capturing the mouse events - the |
138 // first being when a mouse is first pressed. That first capture needs to be | 178 // first being when a mouse is first pressed. That first capture needs to be |
139 // released before the call to GrabPointerAndKeyboard below, otherwise it may | 179 // released before the call to GrabPointerAndKeyboard below, otherwise it may |
140 // get released while we still need the pointer grab, which is why we restrict | 180 // get released while we still need the pointer grab, which is why we restrict |
141 // the scope here. | 181 // the scope here. |
142 { | 182 { |
143 ScopedCapturer capturer(source->GetHost()); | 183 ScopedCapturer capturer(source->GetHost()); |
144 | 184 |
145 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); | 185 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); |
146 // Releasing ScopedCapturer ensures that any other instance of | 186 // Releasing ScopedCapturer ensures that any other instance of |
147 // X11ScopedCapture will not prematurely release grab that will be acquired | 187 // X11ScopedCapture will not prematurely release grab that will be acquired |
148 // below. | 188 // below. |
149 } | 189 } |
150 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with | 190 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with |
151 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. | 191 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. |
152 if (!GrabPointerAndKeyboard(cursor)) { | 192 if (!GrabPointerAndKeyboard(cursor)) { |
153 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); | 193 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); |
154 return false; | 194 return false; |
155 } | 195 } |
156 | 196 |
157 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 197 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = |
198 nested_dispatcher_.Pass(); | |
199 nested_dispatcher_ = | |
200 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); | |
158 if (!drag_image_.isNull() && CheckIfIconValid()) | 201 if (!drag_image_.isNull() && CheckIfIconValid()) |
159 CreateDragImageWindow(); | 202 CreateDragImageWindow(); |
160 | 203 |
161 // We are handling a mouse drag outside of the aura::RootWindow system. We | 204 // We are handling a mouse drag outside of the aura::RootWindow system. We |
162 // must manually make aura think that the mouse button is pressed so that we | 205 // must manually make aura think that the mouse button is pressed so that we |
163 // don't draw extraneous tooltips. | 206 // don't draw extraneous tooltips. |
164 aura::Env* env = aura::Env::GetInstance(); | 207 aura::Env* env = aura::Env::GetInstance(); |
165 if (!env->IsMouseButtonDown()) { | 208 if (!env->IsMouseButtonDown()) { |
166 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); | 209 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); |
167 should_reset_mouse_flags_ = true; | 210 should_reset_mouse_flags_ = true; |
168 } | 211 } |
169 | 212 |
170 in_move_loop_ = true; | 213 in_move_loop_ = true; |
171 canceled_ = false; | 214 canceled_ = false; |
172 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 215 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
173 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); | 216 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); |
174 base::RunLoop run_loop; | 217 base::RunLoop run_loop; |
175 quit_closure_ = run_loop.QuitClosure(); | 218 quit_closure_ = run_loop.QuitClosure(); |
176 run_loop.Run(); | 219 run_loop.Run(); |
220 nested_dispatcher_ = old_dispatcher.Pass(); | |
177 return !canceled_; | 221 return !canceled_; |
178 } | 222 } |
179 | 223 |
180 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { | 224 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { |
181 if (in_move_loop_) { | 225 if (in_move_loop_) { |
182 // If we're still in the move loop, regrab the pointer with the updated | 226 // If we're still in the move loop, regrab the pointer with the updated |
183 // cursor. Note: we can be called from handling an XdndStatus message after | 227 // cursor. Note: we can be called from handling an XdndStatus message after |
184 // EndMoveLoop() was called, but before we return from the nested RunLoop. | 228 // EndMoveLoop() was called, but before we return from the nested RunLoop. |
185 GrabPointerAndKeyboard(cursor); | 229 GrabPointerAndKeyboard(cursor); |
186 } | 230 } |
(...skipping 15 matching lines...) Expand all Loading... | |
202 | 246 |
203 // TODO(erg): Is this ungrab the cause of having to click to give input focus | 247 // TODO(erg): Is this ungrab the cause of having to click to give input focus |
204 // on drawn out windows? Not ungrabbing here screws the X server until I kill | 248 // on drawn out windows? Not ungrabbing here screws the X server until I kill |
205 // the chrome process. | 249 // the chrome process. |
206 | 250 |
207 // Ungrab before we let go of the window. | 251 // Ungrab before we let go of the window. |
208 XDisplay* display = gfx::GetXDisplay(); | 252 XDisplay* display = gfx::GetXDisplay(); |
209 XUngrabPointer(display, CurrentTime); | 253 XUngrabPointer(display, CurrentTime); |
210 XUngrabKeyboard(display, CurrentTime); | 254 XUngrabKeyboard(display, CurrentTime); |
211 | 255 |
212 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 256 LOG(INFO) << "Ungrab"; |
213 drag_widget_.reset(); | 257 drag_widget_.reset(); |
214 delegate_->OnMoveLoopEnded(); | 258 delegate_->OnMoveLoopEnded(); |
215 XDestroyWindow(display, grab_input_window_); | 259 XDestroyWindow(display, grab_input_window_); |
260 grab_input_window_ = None; | |
261 // Restore the previous dispatcher. | |
262 nested_dispatcher_.reset(); | |
216 | 263 |
217 in_move_loop_ = false; | 264 in_move_loop_ = false; |
218 quit_closure_.Run(); | 265 quit_closure_.Run(); |
219 } | 266 } |
220 | 267 |
221 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, | 268 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, |
222 gfx::Vector2dF offset) { | 269 gfx::Vector2dF offset) { |
223 drag_image_ = image; | 270 drag_image_ = image; |
224 drag_offset_ = offset; | 271 drag_offset_ = offset; |
225 // Reset the Y offset, so that the drag-image is always just below the cursor, | 272 // Reset the Y offset, so that the drag-image is always just below the cursor, |
226 // so that it is possible to see where the cursor is going. | 273 // so that it is possible to see where the cursor is going. |
227 drag_offset_.set_y(0.f); | 274 drag_offset_.set_y(0.f); |
228 } | 275 } |
229 | 276 |
230 bool X11WholeScreenMoveLoop::GrabPointerAndKeyboard(gfx::NativeCursor cursor) { | 277 bool X11WholeScreenMoveLoop::GrabPointerAndKeyboard(gfx::NativeCursor cursor) { |
231 XDisplay* display = gfx::GetXDisplay(); | 278 XDisplay* display = gfx::GetXDisplay(); |
232 XGrabServer(display); | 279 XGrabServer(display); |
233 | 280 |
234 XUngrabPointer(display, CurrentTime); | 281 XUngrabPointer(display, CurrentTime); |
282 LOG(INFO) << "Ungrab"; | |
283 | |
235 int ret = XGrabPointer( | 284 int ret = XGrabPointer( |
236 display, | 285 display, |
237 grab_input_window_, | 286 grab_input_window_, |
238 False, | 287 False, |
239 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, | 288 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, |
240 GrabModeAsync, | 289 GrabModeAsync, |
241 GrabModeAsync, | 290 GrabModeAsync, |
242 None, | 291 None, |
243 cursor.platform(), | 292 cursor.platform(), |
244 CurrentTime); | 293 CurrentTime); |
245 if (ret != GrabSuccess) { | 294 if (ret != GrabSuccess) { |
246 DLOG(ERROR) << "Grabbing pointer for dragging failed: " | 295 DLOG(ERROR) << "Grabbing pointer for dragging failed: " |
247 << ui::GetX11ErrorString(display, ret); | 296 << ui::GetX11ErrorString(display, ret); |
248 } else { | 297 } else { |
249 XUngrabKeyboard(display, CurrentTime); | 298 XUngrabKeyboard(display, CurrentTime); |
250 ret = XGrabKeyboard( | 299 ret = XGrabKeyboard( |
251 display, | 300 display, |
252 grab_input_window_, | 301 grab_input_window_, |
253 False, | 302 False, |
254 GrabModeAsync, | 303 GrabModeAsync, |
255 GrabModeAsync, | 304 GrabModeAsync, |
256 CurrentTime); | 305 CurrentTime); |
257 if (ret != GrabSuccess) { | 306 if (ret != GrabSuccess) { |
258 DLOG(ERROR) << "Grabbing keyboard for dragging failed: " | 307 DLOG(ERROR) << "Grabbing keyboard for dragging failed: " |
259 << ui::GetX11ErrorString(display, ret); | 308 << ui::GetX11ErrorString(display, ret); |
260 } | 309 } |
261 } | 310 } |
311 LOG(INFO) << "Grab " << grab_input_window_; | |
262 | 312 |
263 XUngrabServer(display); | 313 XUngrabServer(display); |
264 return ret == GrabSuccess; | 314 return ret == GrabSuccess; |
265 } | 315 } |
266 | 316 |
267 Window X11WholeScreenMoveLoop::CreateDragInputWindow(XDisplay* display) { | 317 Window X11WholeScreenMoveLoop::CreateDragInputWindow(XDisplay* display) { |
268 // Creates an invisible, InputOnly toplevel window. This window will receive | 318 // Creates an invisible, InputOnly toplevel window. This window will receive |
269 // all mouse movement for drags. It turns out that normal windows doing a | 319 // all mouse movement for drags. It turns out that normal windows doing a |
270 // grab doesn't redirect pointer motion events if the pointer isn't over the | 320 // grab doesn't redirect pointer motion events if the pointer isn't over the |
271 // grabbing window. But InputOnly windows are able to grab everything. This | 321 // grabbing window. But InputOnly windows are able to grab everything. This |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 for (int x = 0; x < in_bitmap->width(); ++x) { | 382 for (int x = 0; x < in_bitmap->width(); ++x) { |
333 if (SkColorGetA(in_row[x]) > kMinAlpha) | 383 if (SkColorGetA(in_row[x]) > kMinAlpha) |
334 return true; | 384 return true; |
335 } | 385 } |
336 } | 386 } |
337 | 387 |
338 return false; | 388 return false; |
339 } | 389 } |
340 | 390 |
341 } // namespace views | 391 } // namespace views |
OLD | NEW |