Chromium Code Reviews| 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. | |
|
sadrul
2014/04/25 19:47:40
Update the comment explaining why we need to do th
varkha
2014/04/25 19:56:42
Done.
| |
| 91 if (!in_move_loop_) | |
| 92 return ui::POST_DISPATCH_PERFORM_DEFAULT; | |
| 86 XEvent* xev = event; | 93 XEvent* xev = event; |
| 87 | 94 |
| 88 // Note: the escape key is handled in the tab drag controller, which has | 95 // Note: the escape key is handled in the tab drag controller, which has |
| 89 // keyboard focus even though we took pointer grab. | 96 // keyboard focus even though we took pointer grab. |
| 90 switch (xev->type) { | 97 switch (xev->type) { |
| 91 case MotionNotify: { | 98 case MotionNotify: { |
| 92 if (drag_widget_.get()) { | 99 if (drag_widget_.get()) { |
| 93 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | 100 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); |
| 94 gfx::Point location = gfx::ToFlooredPoint( | 101 gfx::Point location = gfx::ToFlooredPoint( |
| 95 screen->GetCursorScreenPoint() - drag_offset_); | 102 screen->GetCursorScreenPoint() - drag_offset_); |
| 96 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); | 103 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); |
| 97 drag_widget_->StackAtTop(); | 104 drag_widget_->StackAtTop(); |
| 98 } | 105 } |
| 99 last_xmotion_ = xev->xmotion; | 106 last_xmotion_ = xev->xmotion; |
| 100 if (!weak_factory_.HasWeakPtrs()) { | 107 if (!weak_factory_.HasWeakPtrs()) { |
| 101 // Post a task to dispatch mouse movement event when control returns to | 108 // Post a task to dispatch mouse movement event when control returns to |
| 102 // the message loop. This allows smoother dragging since the events are | 109 // the message loop. This allows smoother dragging since the events are |
| 103 // dispatched without waiting for the drag widget updates. | 110 // dispatched without waiting for the drag widget updates. |
| 104 base::MessageLoopForUI::current()->PostTask( | 111 base::MessageLoopForUI::current()->PostTask( |
| 105 FROM_HERE, | 112 FROM_HERE, |
| 106 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, | 113 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, |
| 107 weak_factory_.GetWeakPtr())); | 114 weak_factory_.GetWeakPtr())); |
| 108 } | 115 } |
| 109 break; | 116 return ui::POST_DISPATCH_NONE; |
| 110 } | 117 } |
| 111 case ButtonRelease: { | 118 case ButtonRelease: { |
| 112 if (xev->xbutton.button == Button1) { | 119 if (xev->xbutton.button == Button1) { |
| 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 XEvent xevent = {0}; | |
| 142 if (type == ui::ET_MOUSE_RELEASED) { | |
| 143 xevent.type = ButtonRelease; | |
| 144 xevent.xbutton.button = ui::EventButtonFromNative(xev); | |
| 145 } else { | |
| 146 xevent.type = MotionNotify; | |
| 147 } | |
| 148 xevent.xany.display = xev->xgeneric.display; | |
| 149 xevent.xany.window = grab_input_window_; | |
| 150 // The fields used below are in the same place for all of events | |
| 151 // above. Using xmotion from XEvent's unions to avoid repeating | |
| 152 // the code. | |
| 153 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display); | |
| 154 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds(); | |
| 155 gfx::Point point(ui::EventSystemLocationFromNative(xev)); | |
| 156 xevent.xmotion.x_root = point.x(); | |
| 157 xevent.xmotion.y_root = point.y(); | |
| 158 DispatchEvent(&xevent); | |
| 159 return ui::POST_DISPATCH_NONE; | |
| 160 } | |
| 161 default: | |
| 162 break; | |
| 163 } | |
| 164 } | |
| 127 } | 165 } |
| 128 | 166 |
| 129 return ui::POST_DISPATCH_STOP_PROPAGATION; | 167 return (event->xany.window == grab_input_window_) ? |
| 168 ui::POST_DISPATCH_NONE : ui::POST_DISPATCH_PERFORM_DEFAULT; | |
| 130 } | 169 } |
| 131 | 170 |
| 132 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, | 171 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, |
| 133 gfx::NativeCursor cursor) { | 172 gfx::NativeCursor cursor) { |
| 134 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 173 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
| 135 | 174 |
| 136 // Start a capture on the host, so that it continues to receive events during | 175 // 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 | 176 // 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 | 177 // 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 | 178 // 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 | 179 // get released while we still need the pointer grab, which is why we restrict |
| 141 // the scope here. | 180 // the scope here. |
| 142 { | 181 { |
| 143 ScopedCapturer capturer(source->GetHost()); | 182 ScopedCapturer capturer(source->GetHost()); |
| 144 | 183 |
| 145 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); | 184 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); |
| 146 // Releasing ScopedCapturer ensures that any other instance of | 185 // Releasing ScopedCapturer ensures that any other instance of |
| 147 // X11ScopedCapture will not prematurely release grab that will be acquired | 186 // X11ScopedCapture will not prematurely release grab that will be acquired |
| 148 // below. | 187 // below. |
| 149 } | 188 } |
| 150 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with | 189 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with |
| 151 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. | 190 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. |
| 152 if (!GrabPointerAndKeyboard(cursor)) { | 191 if (!GrabPointerAndKeyboard(cursor)) { |
| 153 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); | 192 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); |
| 154 return false; | 193 return false; |
| 155 } | 194 } |
| 156 | 195 |
| 157 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); | 196 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = |
| 197 nested_dispatcher_.Pass(); | |
| 198 nested_dispatcher_ = | |
| 199 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); | |
| 158 if (!drag_image_.isNull() && CheckIfIconValid()) | 200 if (!drag_image_.isNull() && CheckIfIconValid()) |
| 159 CreateDragImageWindow(); | 201 CreateDragImageWindow(); |
| 160 | 202 |
| 161 // We are handling a mouse drag outside of the aura::RootWindow system. We | 203 // 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 | 204 // must manually make aura think that the mouse button is pressed so that we |
| 163 // don't draw extraneous tooltips. | 205 // don't draw extraneous tooltips. |
| 164 aura::Env* env = aura::Env::GetInstance(); | 206 aura::Env* env = aura::Env::GetInstance(); |
| 165 if (!env->IsMouseButtonDown()) { | 207 if (!env->IsMouseButtonDown()) { |
| 166 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); | 208 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); |
| 167 should_reset_mouse_flags_ = true; | 209 should_reset_mouse_flags_ = true; |
| 168 } | 210 } |
| 169 | 211 |
| 170 in_move_loop_ = true; | 212 in_move_loop_ = true; |
| 171 canceled_ = false; | 213 canceled_ = false; |
| 172 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 214 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 173 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); | 215 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); |
| 174 base::RunLoop run_loop; | 216 base::RunLoop run_loop; |
| 175 quit_closure_ = run_loop.QuitClosure(); | 217 quit_closure_ = run_loop.QuitClosure(); |
| 176 run_loop.Run(); | 218 run_loop.Run(); |
| 219 nested_dispatcher_ = old_dispatcher.Pass(); | |
| 177 return !canceled_; | 220 return !canceled_; |
| 178 } | 221 } |
| 179 | 222 |
| 180 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { | 223 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { |
| 181 if (in_move_loop_) { | 224 if (in_move_loop_) { |
| 182 // If we're still in the move loop, regrab the pointer with the updated | 225 // 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 | 226 // cursor. Note: we can be called from handling an XdndStatus message after |
| 184 // EndMoveLoop() was called, but before we return from the nested RunLoop. | 227 // EndMoveLoop() was called, but before we return from the nested RunLoop. |
| 185 GrabPointerAndKeyboard(cursor); | 228 GrabPointerAndKeyboard(cursor); |
| 186 } | 229 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 202 | 245 |
| 203 // TODO(erg): Is this ungrab the cause of having to click to give input focus | 246 // 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 | 247 // on drawn out windows? Not ungrabbing here screws the X server until I kill |
| 205 // the chrome process. | 248 // the chrome process. |
| 206 | 249 |
| 207 // Ungrab before we let go of the window. | 250 // Ungrab before we let go of the window. |
| 208 XDisplay* display = gfx::GetXDisplay(); | 251 XDisplay* display = gfx::GetXDisplay(); |
| 209 XUngrabPointer(display, CurrentTime); | 252 XUngrabPointer(display, CurrentTime); |
| 210 XUngrabKeyboard(display, CurrentTime); | 253 XUngrabKeyboard(display, CurrentTime); |
| 211 | 254 |
| 212 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); | 255 // Restore the previous dispatcher. |
| 256 nested_dispatcher_.reset(); | |
| 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; | |
| 216 | 261 |
| 217 in_move_loop_ = false; | 262 in_move_loop_ = false; |
| 218 quit_closure_.Run(); | 263 quit_closure_.Run(); |
| 219 } | 264 } |
| 220 | 265 |
| 221 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, | 266 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, |
| 222 gfx::Vector2dF offset) { | 267 gfx::Vector2dF offset) { |
| 223 drag_image_ = image; | 268 drag_image_ = image; |
| 224 drag_offset_ = offset; | 269 drag_offset_ = offset; |
| 225 // Reset the Y offset, so that the drag-image is always just below the cursor, | 270 // 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) { | 377 for (int x = 0; x < in_bitmap->width(); ++x) { |
| 333 if (SkColorGetA(in_row[x]) > kMinAlpha) | 378 if (SkColorGetA(in_row[x]) > kMinAlpha) |
| 334 return true; | 379 return true; |
| 335 } | 380 } |
| 336 } | 381 } |
| 337 | 382 |
| 338 return false; | 383 return false; |
| 339 } | 384 } |
| 340 | 385 |
| 341 } // namespace views | 386 } // namespace views |
| OLD | NEW |