Sets X11WholeScreenMoveLoop as OverrideDispatcher to get events even before grab...
[chromium-blink-merge.git] / ui / views / widget / desktop_aura / x11_whole_screen_move_loop.cc
blob719d50339b6eadbae0902f1c512fa6c363a2c1e9
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
3 // found in the LICENSE file.
5 #include "ui/views/widget/desktop_aura/x11_whole_screen_move_loop.h"
7 #include <X11/Xlib.h>
8 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
9 #undef RootWindow
11 #include "base/bind.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/x/x11_util.h"
20 #include "ui/events/event.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
23 #include "ui/events/platform/scoped_event_dispatcher.h"
24 #include "ui/events/platform/x11/x11_event_source.h"
25 #include "ui/gfx/point_conversions.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/views/controls/image_view.h"
28 #include "ui/views/widget/widget.h"
30 namespace views {
32 namespace {
34 // The minimum alpha before we declare a pixel transparent when searching in
35 // our source image.
36 const uint32 kMinAlpha = 32;
37 const unsigned char kDragWidgetOpacity = 0xc0;
39 class ScopedCapturer {
40 public:
41 explicit ScopedCapturer(aura::WindowTreeHost* host)
42 : host_(host) {
43 host_->SetCapture();
46 ~ScopedCapturer() {
47 host_->ReleaseCapture();
50 private:
51 aura::WindowTreeHost* host_;
53 DISALLOW_COPY_AND_ASSIGN(ScopedCapturer);
56 } // namespace
58 X11WholeScreenMoveLoop::X11WholeScreenMoveLoop(
59 X11WholeScreenMoveLoopDelegate* delegate)
60 : delegate_(delegate),
61 in_move_loop_(false),
62 should_reset_mouse_flags_(false),
63 grab_input_window_(None),
64 canceled_(false),
65 weak_factory_(this) {
66 last_xmotion_.type = LASTEvent;
69 X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {}
71 void X11WholeScreenMoveLoop::DispatchMouseMovement() {
72 if (!weak_factory_.HasWeakPtrs())
73 return;
74 weak_factory_.InvalidateWeakPtrs();
75 DCHECK_EQ(MotionNotify, last_xmotion_.type);
76 delegate_->OnMouseMovement(&last_xmotion_);
77 last_xmotion_.type = LASTEvent;
80 ////////////////////////////////////////////////////////////////////////////////
81 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation:
83 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) {
84 return in_move_loop_;
87 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) {
88 // This method processes all events for the grab_input_window_ as well as
89 // mouse events for all windows while the move loop is active - even before
90 // the grab is granted by X. This allows mouse notification events that were
91 // sent after the capture was requested but before the capture was granted
92 // to be dispatched. It is especially important to process the mouse release
93 // event that should have stopped the drag even if that mouse release happened
94 // before the grab was granted.
95 if (!in_move_loop_)
96 return ui::POST_DISPATCH_PERFORM_DEFAULT;
97 XEvent* xev = event;
99 // Note: the escape key is handled in the tab drag controller, which has
100 // keyboard focus even though we took pointer grab.
101 switch (xev->type) {
102 case MotionNotify: {
103 if (drag_widget_.get()) {
104 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
105 gfx::Point location = gfx::ToFlooredPoint(
106 screen->GetCursorScreenPoint() - drag_offset_);
107 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size()));
108 drag_widget_->StackAtTop();
110 last_xmotion_ = xev->xmotion;
111 if (!weak_factory_.HasWeakPtrs()) {
112 // Post a task to dispatch mouse movement event when control returns to
113 // the message loop. This allows smoother dragging since the events are
114 // dispatched without waiting for the drag widget updates.
115 base::MessageLoopForUI::current()->PostTask(
116 FROM_HERE,
117 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement,
118 weak_factory_.GetWeakPtr()));
120 return ui::POST_DISPATCH_NONE;
122 case ButtonRelease: {
123 if (xev->xbutton.button == Button1) {
124 // Assume that drags are being done with the left mouse button. Only
125 // break the drag if the left mouse button was released.
126 DispatchMouseMovement();
127 delegate_->OnMouseReleased();
129 return ui::POST_DISPATCH_NONE;
131 case KeyPress: {
132 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) {
133 canceled_ = true;
134 EndMoveLoop();
135 return ui::POST_DISPATCH_NONE;
137 break;
139 case GenericEvent: {
140 ui::EventType type = ui::EventTypeFromNative(xev);
141 switch (type) {
142 case ui::ET_MOUSE_MOVED:
143 case ui::ET_MOUSE_DRAGGED:
144 case ui::ET_MOUSE_RELEASED: {
145 XEvent xevent = {0};
146 if (type == ui::ET_MOUSE_RELEASED) {
147 xevent.type = ButtonRelease;
148 xevent.xbutton.button = ui::EventButtonFromNative(xev);
149 } else {
150 xevent.type = MotionNotify;
152 xevent.xany.display = xev->xgeneric.display;
153 xevent.xany.window = grab_input_window_;
154 // The fields used below are in the same place for all of events
155 // above. Using xmotion from XEvent's unions to avoid repeating
156 // the code.
157 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display);
158 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds();
159 gfx::Point point(ui::EventSystemLocationFromNative(xev));
160 xevent.xmotion.x_root = point.x();
161 xevent.xmotion.y_root = point.y();
162 DispatchEvent(&xevent);
163 return ui::POST_DISPATCH_NONE;
165 default:
166 break;
171 return (event->xany.window == grab_input_window_) ?
172 ui::POST_DISPATCH_NONE : ui::POST_DISPATCH_PERFORM_DEFAULT;
175 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source,
176 gfx::NativeCursor cursor) {
177 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
179 // Start a capture on the host, so that it continues to receive events during
180 // the drag. This may be second time we are capturing the mouse events - the
181 // first being when a mouse is first pressed. That first capture needs to be
182 // released before the call to GrabPointerAndKeyboard below, otherwise it may
183 // get released while we still need the pointer grab, which is why we restrict
184 // the scope here.
186 ScopedCapturer capturer(source->GetHost());
188 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay());
189 // Releasing ScopedCapturer ensures that any other instance of
190 // X11ScopedCapture will not prematurely release grab that will be acquired
191 // below.
193 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with
194 // ScopedCapturer to avoid possibility of logically keeping multiple grabs.
195 if (!GrabPointerAndKeyboard(cursor)) {
196 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_);
197 return false;
200 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher =
201 nested_dispatcher_.Pass();
202 nested_dispatcher_ =
203 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this);
204 if (!drag_image_.isNull() && CheckIfIconValid())
205 CreateDragImageWindow();
207 // We are handling a mouse drag outside of the aura::RootWindow system. We
208 // must manually make aura think that the mouse button is pressed so that we
209 // don't draw extraneous tooltips.
210 aura::Env* env = aura::Env::GetInstance();
211 if (!env->IsMouseButtonDown()) {
212 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON);
213 should_reset_mouse_flags_ = true;
216 in_move_loop_ = true;
217 canceled_ = false;
218 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
219 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
220 base::RunLoop run_loop;
221 quit_closure_ = run_loop.QuitClosure();
222 run_loop.Run();
223 nested_dispatcher_ = old_dispatcher.Pass();
224 return !canceled_;
227 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) {
228 if (in_move_loop_) {
229 // If we're still in the move loop, regrab the pointer with the updated
230 // cursor. Note: we can be called from handling an XdndStatus message after
231 // EndMoveLoop() was called, but before we return from the nested RunLoop.
232 GrabPointerAndKeyboard(cursor);
236 void X11WholeScreenMoveLoop::EndMoveLoop() {
237 if (!in_move_loop_)
238 return;
240 // Prevent DispatchMouseMovement from dispatching any posted motion event.
241 weak_factory_.InvalidateWeakPtrs();
242 last_xmotion_.type = LASTEvent;
244 // We undo our emulated mouse click from RunMoveLoop();
245 if (should_reset_mouse_flags_) {
246 aura::Env::GetInstance()->set_mouse_button_flags(0);
247 should_reset_mouse_flags_ = false;
250 // TODO(erg): Is this ungrab the cause of having to click to give input focus
251 // on drawn out windows? Not ungrabbing here screws the X server until I kill
252 // the chrome process.
254 // Ungrab before we let go of the window.
255 XDisplay* display = gfx::GetXDisplay();
256 XUngrabPointer(display, CurrentTime);
257 XUngrabKeyboard(display, CurrentTime);
259 // Restore the previous dispatcher.
260 nested_dispatcher_.reset();
261 drag_widget_.reset();
262 delegate_->OnMoveLoopEnded();
263 XDestroyWindow(display, grab_input_window_);
264 grab_input_window_ = None;
266 in_move_loop_ = false;
267 quit_closure_.Run();
270 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image,
271 gfx::Vector2dF offset) {
272 drag_image_ = image;
273 drag_offset_ = offset;
274 // Reset the Y offset, so that the drag-image is always just below the cursor,
275 // so that it is possible to see where the cursor is going.
276 drag_offset_.set_y(0.f);
279 bool X11WholeScreenMoveLoop::GrabPointerAndKeyboard(gfx::NativeCursor cursor) {
280 XDisplay* display = gfx::GetXDisplay();
281 XGrabServer(display);
283 XUngrabPointer(display, CurrentTime);
284 int ret = XGrabPointer(
285 display,
286 grab_input_window_,
287 False,
288 ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
289 GrabModeAsync,
290 GrabModeAsync,
291 None,
292 cursor.platform(),
293 CurrentTime);
294 if (ret != GrabSuccess) {
295 DLOG(ERROR) << "Grabbing pointer for dragging failed: "
296 << ui::GetX11ErrorString(display, ret);
297 } else {
298 XUngrabKeyboard(display, CurrentTime);
299 ret = XGrabKeyboard(
300 display,
301 grab_input_window_,
302 False,
303 GrabModeAsync,
304 GrabModeAsync,
305 CurrentTime);
306 if (ret != GrabSuccess) {
307 DLOG(ERROR) << "Grabbing keyboard for dragging failed: "
308 << ui::GetX11ErrorString(display, ret);
312 XUngrabServer(display);
313 return ret == GrabSuccess;
316 Window X11WholeScreenMoveLoop::CreateDragInputWindow(XDisplay* display) {
317 // Creates an invisible, InputOnly toplevel window. This window will receive
318 // all mouse movement for drags. It turns out that normal windows doing a
319 // grab doesn't redirect pointer motion events if the pointer isn't over the
320 // grabbing window. But InputOnly windows are able to grab everything. This
321 // is what GTK+ does, and I found a patch to KDE that did something similar.
322 unsigned long attribute_mask = CWEventMask | CWOverrideRedirect;
323 XSetWindowAttributes swa;
324 memset(&swa, 0, sizeof(swa));
325 swa.event_mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
326 KeyPressMask | KeyReleaseMask | StructureNotifyMask;
327 swa.override_redirect = True;
328 Window window = XCreateWindow(display,
329 DefaultRootWindow(display),
330 -100, -100, 10, 10,
331 0, CopyFromParent, InputOnly, CopyFromParent,
332 attribute_mask, &swa);
333 XMapRaised(display, window);
334 ui::X11EventSource::GetInstance()->BlockUntilWindowMapped(window);
335 return window;
338 void X11WholeScreenMoveLoop::CreateDragImageWindow() {
339 Widget* widget = new Widget;
340 Widget::InitParams params(Widget::InitParams::TYPE_DRAG);
341 params.opacity = Widget::InitParams::OPAQUE_WINDOW;
342 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
343 params.accept_events = false;
345 gfx::Point location = gfx::ToFlooredPoint(
346 gfx::Screen::GetNativeScreen()->GetCursorScreenPoint() - drag_offset_);
347 params.bounds = gfx::Rect(location, drag_image_.size());
348 widget->set_focus_on_creation(false);
349 widget->set_frame_type(Widget::FRAME_TYPE_FORCE_NATIVE);
350 widget->Init(params);
351 widget->SetOpacity(kDragWidgetOpacity);
352 widget->GetNativeWindow()->SetName("DragWindow");
354 ImageView* image = new ImageView();
355 image->SetImage(drag_image_);
356 image->SetBounds(0, 0, drag_image_.width(), drag_image_.height());
357 widget->SetContentsView(image);
358 widget->Show();
359 widget->GetNativeWindow()->layer()->SetFillsBoundsOpaquely(false);
361 drag_widget_.reset(widget);
364 bool X11WholeScreenMoveLoop::CheckIfIconValid() {
365 // TODO(erg): I've tried at least five different strategies for trying to
366 // build a mask based off the alpha channel. While all of them have worked,
367 // none of them have been performant and introduced multiple second
368 // delays. (I spent a day getting a rectangle segmentation algorithm polished
369 // here...and then found that even through I had the rectangle extraction
370 // down to mere milliseconds, SkRegion still fell over on the number of
371 // rectangles.)
373 // Creating a mask here near instantaneously should be possible, as GTK does
374 // it, but I've blown days on this and I'm punting now.
376 const SkBitmap* in_bitmap = drag_image_.bitmap();
377 SkAutoLockPixels in_lock(*in_bitmap);
378 for (int y = 0; y < in_bitmap->height(); ++y) {
379 uint32* in_row = in_bitmap->getAddr32(0, y);
381 for (int x = 0; x < in_bitmap->width(); ++x) {
382 if (SkColorGetA(in_row[x]) > kMinAlpha)
383 return true;
387 return false;
390 } // namespace views