| 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/Xlib.h> | 7 #include <X11/Xlib.h> |
| 8 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. | 8 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. |
| 9 #undef RootWindow | 9 #undef RootWindow |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/aura/env.h" | 15 #include "ui/aura/env.h" |
| 16 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
| 17 #include "ui/aura/window_event_dispatcher.h" | 17 #include "ui/aura/window_event_dispatcher.h" |
| 18 #include "ui/aura/window_tree_host.h" | 18 #include "ui/aura/window_tree_host.h" |
| 19 #include "ui/base/x/x11_util.h" | 19 #include "ui/base/x/x11_util.h" |
| 20 #include "ui/events/event.h" | 20 #include "ui/events/event.h" |
| 21 #include "ui/events/event_utils.h" |
| 21 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 22 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
| 23 #include "ui/events/platform/scoped_event_dispatcher.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" |
| 26 #include "ui/views/widget/widget.h" | 28 #include "ui/views/widget/widget.h" |
| 27 | 29 |
| 28 namespace views { | 30 namespace views { |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 weak_factory_.InvalidateWeakPtrs(); | 74 weak_factory_.InvalidateWeakPtrs(); |
| 73 DCHECK_EQ(MotionNotify, last_xmotion_.type); | 75 DCHECK_EQ(MotionNotify, last_xmotion_.type); |
| 74 delegate_->OnMouseMovement(&last_xmotion_); | 76 delegate_->OnMouseMovement(&last_xmotion_); |
| 75 last_xmotion_.type = LASTEvent; | 77 last_xmotion_.type = LASTEvent; |
| 76 } | 78 } |
| 77 | 79 |
| 78 //////////////////////////////////////////////////////////////////////////////// | 80 //////////////////////////////////////////////////////////////////////////////// |
| 79 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: | 81 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: |
| 80 | 82 |
| 81 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { | 83 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { |
| 82 return event->xany.window == grab_input_window_; | 84 return in_move_loop_; |
| 83 } | 85 } |
| 84 | 86 |
| 85 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { | 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; |
| 86 XEvent* xev = event; | 97 XEvent* xev = event; |
| 87 | 98 |
| 88 // Note: the escape key is handled in the tab drag controller, which has | 99 // Note: the escape key is handled in the tab drag controller, which has |
| 89 // keyboard focus even though we took pointer grab. | 100 // keyboard focus even though we took pointer grab. |
| 90 switch (xev->type) { | 101 switch (xev->type) { |
| 91 case MotionNotify: { | 102 case MotionNotify: { |
| 92 if (drag_widget_.get()) { | 103 if (drag_widget_.get()) { |
| 93 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | 104 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); |
| 94 gfx::Point location = gfx::ToFlooredPoint( | 105 gfx::Point location = gfx::ToFlooredPoint( |
| 95 screen->GetCursorScreenPoint() - drag_offset_); | 106 screen->GetCursorScreenPoint() - drag_offset_); |
| 96 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); | 107 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); |
| 97 drag_widget_->StackAtTop(); | 108 drag_widget_->StackAtTop(); |
| 98 } | 109 } |
| 99 last_xmotion_ = xev->xmotion; | 110 last_xmotion_ = xev->xmotion; |
| 100 if (!weak_factory_.HasWeakPtrs()) { | 111 if (!weak_factory_.HasWeakPtrs()) { |
| 101 // Post a task to dispatch mouse movement event when control returns to | 112 // Post a task to dispatch mouse movement event when control returns to |
| 102 // the message loop. This allows smoother dragging since the events are | 113 // the message loop. This allows smoother dragging since the events are |
| 103 // dispatched without waiting for the drag widget updates. | 114 // dispatched without waiting for the drag widget updates. |
| 104 base::MessageLoopForUI::current()->PostTask( | 115 base::MessageLoopForUI::current()->PostTask( |
| 105 FROM_HERE, | 116 FROM_HERE, |
| 106 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, | 117 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, |
| 107 weak_factory_.GetWeakPtr())); | 118 weak_factory_.GetWeakPtr())); |
| 108 } | 119 } |
| 109 break; | 120 return ui::POST_DISPATCH_NONE; |
| 110 } | 121 } |
| 111 case ButtonRelease: { | 122 case ButtonRelease: { |
| 112 if (xev->xbutton.button == Button1) { | 123 if (xev->xbutton.button == Button1) { |
| 113 // Assume that drags are being done with the left mouse button. Only | 124 // Assume that drags are being done with the left mouse button. Only |
| 114 // break the drag if the left mouse button was released. | 125 // break the drag if the left mouse button was released. |
| 115 DispatchMouseMovement(); | 126 DispatchMouseMovement(); |
| 116 delegate_->OnMouseReleased(); | 127 delegate_->OnMouseReleased(); |
| 117 } | 128 } |
| 118 break; | 129 return ui::POST_DISPATCH_NONE; |
| 119 } | 130 } |
| 120 case KeyPress: { | 131 case KeyPress: { |
| 121 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) { | 132 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) { |
| 122 canceled_ = true; | 133 canceled_ = true; |
| 123 EndMoveLoop(); | 134 EndMoveLoop(); |
| 135 return ui::POST_DISPATCH_NONE; |
| 124 } | 136 } |
| 125 break; | 137 break; |
| 126 } | 138 } |
| 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; |
| 151 } |
| 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; |
| 164 } |
| 165 default: |
| 166 break; |
| 167 } |
| 168 } |
| 127 } | 169 } |
| 128 | 170 |
| 129 return ui::POST_DISPATCH_STOP_PROPAGATION; | 171 return (event->xany.window == grab_input_window_) ? |
| 172 ui::POST_DISPATCH_NONE : ui::POST_DISPATCH_PERFORM_DEFAULT; |
| 130 } | 173 } |
| 131 | 174 |
| 132 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, | 175 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, |
| 133 gfx::NativeCursor cursor) { | 176 gfx::NativeCursor cursor) { |
| 134 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 177 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
| 135 | 178 |
| 136 // Start a capture on the host, so that it continues to receive events during | 179 // 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 | 180 // 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 | 181 // 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 | 182 // 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 | 183 // get released while we still need the pointer grab, which is why we restrict |
| 141 // the scope here. | 184 // the scope here. |
| 142 { | 185 { |
| 143 ScopedCapturer capturer(source->GetHost()); | 186 ScopedCapturer capturer(source->GetHost()); |
| 144 | 187 |
| 145 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); | 188 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); |
| 146 // Releasing ScopedCapturer ensures that any other instance of | 189 // Releasing ScopedCapturer ensures that any other instance of |
| 147 // X11ScopedCapture will not prematurely release grab that will be acquired | 190 // X11ScopedCapture will not prematurely release grab that will be acquired |
| 148 // below. | 191 // below. |
| 149 } | 192 } |
| 150 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with | 193 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with |
| 151 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. | 194 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. |
| 152 if (!GrabPointerAndKeyboard(cursor)) { | 195 if (!GrabPointerAndKeyboard(cursor)) { |
| 153 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); | 196 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); |
| 154 return false; | 197 return false; |
| 155 } | 198 } |
| 156 | 199 |
| 157 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 200 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = |
| 201 nested_dispatcher_.Pass(); |
| 202 nested_dispatcher_ = |
| 203 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); |
| 158 if (!drag_image_.isNull() && CheckIfIconValid()) | 204 if (!drag_image_.isNull() && CheckIfIconValid()) |
| 159 CreateDragImageWindow(); | 205 CreateDragImageWindow(); |
| 160 | 206 |
| 161 // We are handling a mouse drag outside of the aura::RootWindow system. We | 207 // 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 | 208 // must manually make aura think that the mouse button is pressed so that we |
| 163 // don't draw extraneous tooltips. | 209 // don't draw extraneous tooltips. |
| 164 aura::Env* env = aura::Env::GetInstance(); | 210 aura::Env* env = aura::Env::GetInstance(); |
| 165 if (!env->IsMouseButtonDown()) { | 211 if (!env->IsMouseButtonDown()) { |
| 166 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); | 212 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); |
| 167 should_reset_mouse_flags_ = true; | 213 should_reset_mouse_flags_ = true; |
| 168 } | 214 } |
| 169 | 215 |
| 170 in_move_loop_ = true; | 216 in_move_loop_ = true; |
| 171 canceled_ = false; | 217 canceled_ = false; |
| 172 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 218 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 173 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); | 219 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); |
| 174 base::RunLoop run_loop; | 220 base::RunLoop run_loop; |
| 175 quit_closure_ = run_loop.QuitClosure(); | 221 quit_closure_ = run_loop.QuitClosure(); |
| 176 run_loop.Run(); | 222 run_loop.Run(); |
| 223 nested_dispatcher_ = old_dispatcher.Pass(); |
| 177 return !canceled_; | 224 return !canceled_; |
| 178 } | 225 } |
| 179 | 226 |
| 180 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { | 227 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { |
| 181 if (in_move_loop_) { | 228 if (in_move_loop_) { |
| 182 // If we're still in the move loop, regrab the pointer with the updated | 229 // 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 | 230 // cursor. Note: we can be called from handling an XdndStatus message after |
| 184 // EndMoveLoop() was called, but before we return from the nested RunLoop. | 231 // EndMoveLoop() was called, but before we return from the nested RunLoop. |
| 185 GrabPointerAndKeyboard(cursor); | 232 GrabPointerAndKeyboard(cursor); |
| 186 } | 233 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 202 | 249 |
| 203 // TODO(erg): Is this ungrab the cause of having to click to give input focus | 250 // 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 | 251 // on drawn out windows? Not ungrabbing here screws the X server until I kill |
| 205 // the chrome process. | 252 // the chrome process. |
| 206 | 253 |
| 207 // Ungrab before we let go of the window. | 254 // Ungrab before we let go of the window. |
| 208 XDisplay* display = gfx::GetXDisplay(); | 255 XDisplay* display = gfx::GetXDisplay(); |
| 209 XUngrabPointer(display, CurrentTime); | 256 XUngrabPointer(display, CurrentTime); |
| 210 XUngrabKeyboard(display, CurrentTime); | 257 XUngrabKeyboard(display, CurrentTime); |
| 211 | 258 |
| 212 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 259 // Restore the previous dispatcher. |
| 260 nested_dispatcher_.reset(); |
| 213 drag_widget_.reset(); | 261 drag_widget_.reset(); |
| 214 delegate_->OnMoveLoopEnded(); | 262 delegate_->OnMoveLoopEnded(); |
| 215 XDestroyWindow(display, grab_input_window_); | 263 XDestroyWindow(display, grab_input_window_); |
| 264 grab_input_window_ = None; |
| 216 | 265 |
| 217 in_move_loop_ = false; | 266 in_move_loop_ = false; |
| 218 quit_closure_.Run(); | 267 quit_closure_.Run(); |
| 219 } | 268 } |
| 220 | 269 |
| 221 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, | 270 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, |
| 222 gfx::Vector2dF offset) { | 271 gfx::Vector2dF offset) { |
| 223 drag_image_ = image; | 272 drag_image_ = image; |
| 224 drag_offset_ = offset; | 273 drag_offset_ = offset; |
| 225 // Reset the Y offset, so that the drag-image is always just below the cursor, | 274 // Reset the Y offset, so that the drag-image is always just below the cursor, |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 for (int x = 0; x < in_bitmap->width(); ++x) { | 381 for (int x = 0; x < in_bitmap->width(); ++x) { |
| 333 if (SkColorGetA(in_row[x]) > kMinAlpha) | 382 if (SkColorGetA(in_row[x]) > kMinAlpha) |
| 334 return true; | 383 return true; |
| 335 } | 384 } |
| 336 } | 385 } |
| 337 | 386 |
| 338 return false; | 387 return false; |
| 339 } | 388 } |
| 340 | 389 |
| 341 } // namespace views | 390 } // namespace views |
| OLD | NEW |