| 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/message_loop/message_pump_x11.h" | 13 #include "base/message_loop/message_pump_x11.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/aura/env.h" | 16 #include "ui/aura/env.h" |
| 17 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
| 18 #include "ui/aura/window_event_dispatcher.h" | 18 #include "ui/aura/window_event_dispatcher.h" |
| 19 #include "ui/aura/window_tree_host.h" | 19 #include "ui/aura/window_tree_host.h" |
| 20 #include "ui/base/x/x11_util.h" | 20 #include "ui/base/x/x11_util.h" |
| 21 #include "ui/events/event.h" | 21 #include "ui/events/event.h" |
| 22 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 22 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
| 23 #include "ui/events/platform/x11/x11_event_source.h" |
| 23 #include "ui/gfx/point_conversions.h" | 24 #include "ui/gfx/point_conversions.h" |
| 24 #include "ui/gfx/screen.h" | 25 #include "ui/gfx/screen.h" |
| 25 #include "ui/views/controls/image_view.h" | 26 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
| 27 | 28 |
| 28 namespace views { | 29 namespace views { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // The minimum alpha before we declare a pixel transparent when searching in | 33 // The minimum alpha before we declare a pixel transparent when searching in |
| (...skipping 25 matching lines...) Expand all Loading... |
| 58 : delegate_(delegate), | 59 : delegate_(delegate), |
| 59 in_move_loop_(false), | 60 in_move_loop_(false), |
| 60 should_reset_mouse_flags_(false), | 61 should_reset_mouse_flags_(false), |
| 61 grab_input_window_(None), | 62 grab_input_window_(None), |
| 62 weak_factory_(this) { | 63 weak_factory_(this) { |
| 63 last_xmotion_.type = LASTEvent; | 64 last_xmotion_.type = LASTEvent; |
| 64 } | 65 } |
| 65 | 66 |
| 66 X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {} | 67 X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {} |
| 67 | 68 |
| 68 //////////////////////////////////////////////////////////////////////////////// | |
| 69 // DesktopWindowTreeHostLinux, MessagePumpDispatcher implementation: | |
| 70 | |
| 71 void X11WholeScreenMoveLoop::DispatchMouseMovement() { | 69 void X11WholeScreenMoveLoop::DispatchMouseMovement() { |
| 72 if (!weak_factory_.HasWeakPtrs()) | 70 if (!weak_factory_.HasWeakPtrs()) |
| 73 return; | 71 return; |
| 74 weak_factory_.InvalidateWeakPtrs(); | 72 weak_factory_.InvalidateWeakPtrs(); |
| 75 DCHECK_EQ(MotionNotify, last_xmotion_.type); | 73 DCHECK_EQ(MotionNotify, last_xmotion_.type); |
| 76 delegate_->OnMouseMovement(&last_xmotion_); | 74 delegate_->OnMouseMovement(&last_xmotion_); |
| 77 last_xmotion_.type = LASTEvent; | 75 last_xmotion_.type = LASTEvent; |
| 78 } | 76 } |
| 79 | 77 |
| 80 uint32_t X11WholeScreenMoveLoop::Dispatch(const base::NativeEvent& event) { | 78 //////////////////////////////////////////////////////////////////////////////// |
| 79 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: |
| 80 |
| 81 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { |
| 82 return event->xany.window == grab_input_window_; |
| 83 } |
| 84 |
| 85 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { |
| 81 XEvent* xev = event; | 86 XEvent* xev = event; |
| 82 | 87 |
| 83 // Note: the escape key is handled in the tab drag controller, which has | 88 // Note: the escape key is handled in the tab drag controller, which has |
| 84 // keyboard focus even though we took pointer grab. | 89 // keyboard focus even though we took pointer grab. |
| 85 switch (xev->type) { | 90 switch (xev->type) { |
| 86 case MotionNotify: { | 91 case MotionNotify: { |
| 87 if (drag_widget_.get()) { | 92 if (drag_widget_.get()) { |
| 88 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | 93 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); |
| 89 gfx::Point location = gfx::ToFlooredPoint( | 94 gfx::Point location = gfx::ToFlooredPoint( |
| 90 screen->GetCursorScreenPoint() - drag_offset_); | 95 screen->GetCursorScreenPoint() - drag_offset_); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 112 } | 117 } |
| 113 break; | 118 break; |
| 114 } | 119 } |
| 115 case KeyPress: { | 120 case KeyPress: { |
| 116 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) | 121 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) |
| 117 EndMoveLoop(); | 122 EndMoveLoop(); |
| 118 break; | 123 break; |
| 119 } | 124 } |
| 120 } | 125 } |
| 121 | 126 |
| 122 return POST_DISPATCH_NONE; | 127 return ui::POST_DISPATCH_STOP_PROPAGATION; |
| 123 } | 128 } |
| 124 | 129 |
| 125 //////////////////////////////////////////////////////////////////////////////// | 130 //////////////////////////////////////////////////////////////////////////////// |
| 126 // DesktopWindowTreeHostLinux, aura::client::WindowMoveClient implementation: | 131 // DesktopWindowTreeHostLinux, aura::client::WindowMoveClient implementation: |
| 127 | 132 |
| 128 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, | 133 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, |
| 129 gfx::NativeCursor cursor) { | 134 gfx::NativeCursor cursor) { |
| 130 // Start a capture on the host, so that it continues to receive events during | 135 // Start a capture on the host, so that it continues to receive events during |
| 131 // the drag. This may be second time we are capturing the mouse events - the | 136 // the drag. This may be second time we are capturing the mouse events - the |
| 132 // first being when a mouse is first pressed. That first capture needs to be | 137 // first being when a mouse is first pressed. That first capture needs to be |
| 133 // released before the call to GrabPointerAndKeyboard below, otherwise it may | 138 // released before the call to GrabPointerAndKeyboard below, otherwise it may |
| 134 // get released while we still need the pointer grab, which is why we restrict | 139 // get released while we still need the pointer grab, which is why we restrict |
| 135 // the scope here. | 140 // the scope here. |
| 136 { | 141 { |
| 137 ScopedCapturer capturer(source->GetHost()); | 142 ScopedCapturer capturer(source->GetHost()); |
| 138 | 143 |
| 139 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 144 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
| 140 in_move_loop_ = true; | 145 in_move_loop_ = true; |
| 141 | 146 |
| 142 XDisplay* display = gfx::GetXDisplay(); | 147 XDisplay* display = gfx::GetXDisplay(); |
| 143 | 148 |
| 144 grab_input_window_ = CreateDragInputWindow(display); | 149 grab_input_window_ = CreateDragInputWindow(display); |
| 145 if (!drag_image_.isNull() && CheckIfIconValid()) | 150 if (!drag_image_.isNull() && CheckIfIconValid()) |
| 146 CreateDragImageWindow(); | 151 CreateDragImageWindow(); |
| 147 base::MessagePumpX11::Current()->AddDispatcherForWindow( | 152 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this); |
| 148 this, grab_input_window_); | |
| 149 // Releasing ScopedCapturer ensures that any other instance of | 153 // Releasing ScopedCapturer ensures that any other instance of |
| 150 // X11ScopedCapture will not prematurely release grab that will be acquired | 154 // X11ScopedCapture will not prematurely release grab that will be acquired |
| 151 // below. | 155 // below. |
| 152 } | 156 } |
| 153 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with | 157 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with |
| 154 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. | 158 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. |
| 155 if (!GrabPointerAndKeyboard(cursor)) | 159 if (!GrabPointerAndKeyboard(cursor)) |
| 156 return false; | 160 return false; |
| 157 | 161 |
| 158 // We are handling a mouse drag outside of the aura::RootWindow system. We | 162 // We are handling a mouse drag outside of the aura::RootWindow system. We |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 201 |
| 198 // TODO(erg): Is this ungrab the cause of having to click to give input focus | 202 // TODO(erg): Is this ungrab the cause of having to click to give input focus |
| 199 // on drawn out windows? Not ungrabbing here screws the X server until I kill | 203 // on drawn out windows? Not ungrabbing here screws the X server until I kill |
| 200 // the chrome process. | 204 // the chrome process. |
| 201 | 205 |
| 202 // Ungrab before we let go of the window. | 206 // Ungrab before we let go of the window. |
| 203 XDisplay* display = gfx::GetXDisplay(); | 207 XDisplay* display = gfx::GetXDisplay(); |
| 204 XUngrabPointer(display, CurrentTime); | 208 XUngrabPointer(display, CurrentTime); |
| 205 XUngrabKeyboard(display, CurrentTime); | 209 XUngrabKeyboard(display, CurrentTime); |
| 206 | 210 |
| 207 base::MessagePumpX11::Current()->RemoveDispatcherForWindow( | 211 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); |
| 208 grab_input_window_); | |
| 209 drag_widget_.reset(); | 212 drag_widget_.reset(); |
| 210 delegate_->OnMoveLoopEnded(); | 213 delegate_->OnMoveLoopEnded(); |
| 211 XDestroyWindow(display, grab_input_window_); | 214 XDestroyWindow(display, grab_input_window_); |
| 212 | 215 |
| 213 in_move_loop_ = false; | 216 in_move_loop_ = false; |
| 214 quit_closure_.Run(); | 217 quit_closure_.Run(); |
| 215 } | 218 } |
| 216 | 219 |
| 217 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, | 220 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, |
| 218 gfx::Vector2dF offset) { | 221 gfx::Vector2dF offset) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 memset(&swa, 0, sizeof(swa)); | 274 memset(&swa, 0, sizeof(swa)); |
| 272 swa.event_mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | | 275 swa.event_mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | |
| 273 KeyPressMask | KeyReleaseMask | StructureNotifyMask; | 276 KeyPressMask | KeyReleaseMask | StructureNotifyMask; |
| 274 swa.override_redirect = True; | 277 swa.override_redirect = True; |
| 275 Window window = XCreateWindow(display, | 278 Window window = XCreateWindow(display, |
| 276 DefaultRootWindow(display), | 279 DefaultRootWindow(display), |
| 277 -100, -100, 10, 10, | 280 -100, -100, 10, 10, |
| 278 0, CopyFromParent, InputOnly, CopyFromParent, | 281 0, CopyFromParent, InputOnly, CopyFromParent, |
| 279 attribute_mask, &swa); | 282 attribute_mask, &swa); |
| 280 XMapRaised(display, window); | 283 XMapRaised(display, window); |
| 281 base::MessagePumpX11::Current()->BlockUntilWindowMapped(window); | 284 ui::X11EventSource::GetInstance()->BlockUntilWindowMapped(window); |
| 282 return window; | 285 return window; |
| 283 } | 286 } |
| 284 | 287 |
| 285 void X11WholeScreenMoveLoop::CreateDragImageWindow() { | 288 void X11WholeScreenMoveLoop::CreateDragImageWindow() { |
| 286 Widget* widget = new Widget; | 289 Widget* widget = new Widget; |
| 287 Widget::InitParams params(Widget::InitParams::TYPE_DRAG); | 290 Widget::InitParams params(Widget::InitParams::TYPE_DRAG); |
| 288 params.opacity = Widget::InitParams::OPAQUE_WINDOW; | 291 params.opacity = Widget::InitParams::OPAQUE_WINDOW; |
| 289 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 292 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 290 params.accept_events = false; | 293 params.accept_events = false; |
| 291 | 294 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 for (int x = 0; x < in_bitmap->width(); ++x) { | 331 for (int x = 0; x < in_bitmap->width(); ++x) { |
| 329 if (SkColorGetA(in_row[x]) > kMinAlpha) | 332 if (SkColorGetA(in_row[x]) > kMinAlpha) |
| 330 return true; | 333 return true; |
| 331 } | 334 } |
| 332 } | 335 } |
| 333 | 336 |
| 334 return false; | 337 return false; |
| 335 } | 338 } |
| 336 | 339 |
| 337 } // namespace views | 340 } // namespace views |
| OLD | NEW |