| 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 "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" | |
| 19 #include "ui/base/x/x11_util.h" | 18 #include "ui/base/x/x11_util.h" |
| 20 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
| 21 #include "ui/events/event_utils.h" | 20 #include "ui/events/event_utils.h" |
| 22 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 21 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
| 23 #include "ui/events/platform/scoped_event_dispatcher.h" | 22 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 24 #include "ui/events/platform/x11/x11_event_source.h" | 23 #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" | |
| 29 | 24 |
| 30 namespace views { | 25 namespace views { |
| 31 | 26 |
| 32 namespace { | |
| 33 | |
| 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; | |
| 38 | |
| 39 class ScopedCapturer { | |
| 40 public: | |
| 41 explicit ScopedCapturer(aura::WindowTreeHost* host) | |
| 42 : host_(host) { | |
| 43 host_->SetCapture(); | |
| 44 } | |
| 45 | |
| 46 ~ScopedCapturer() { | |
| 47 host_->ReleaseCapture(); | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 aura::WindowTreeHost* host_; | |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(ScopedCapturer); | |
| 54 }; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 X11WholeScreenMoveLoop::X11WholeScreenMoveLoop( | 27 X11WholeScreenMoveLoop::X11WholeScreenMoveLoop( |
| 59 X11WholeScreenMoveLoopDelegate* delegate) | 28 X11WholeScreenMoveLoopDelegate* delegate) |
| 60 : delegate_(delegate), | 29 : delegate_(delegate), |
| 61 in_move_loop_(false), | 30 in_move_loop_(false), |
| 62 should_reset_mouse_flags_(false), | 31 should_reset_mouse_flags_(false), |
| 63 grab_input_window_(None), | |
| 64 canceled_(false), | 32 canceled_(false), |
| 65 has_grab_(false), | 33 has_grab_(false), |
| 66 weak_factory_(this) { | 34 weak_factory_(this) { |
| 67 last_xmotion_.type = LASTEvent; | 35 last_xmotion_.type = LASTEvent; |
| 68 } | 36 } |
| 69 | 37 |
| 70 X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {} | 38 X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {} |
| 71 | 39 |
| 72 void X11WholeScreenMoveLoop::DispatchMouseMovement() { | 40 void X11WholeScreenMoveLoop::DispatchMouseMovement() { |
| 73 if (!weak_factory_.HasWeakPtrs()) | 41 if (!weak_factory_.HasWeakPtrs()) |
| 74 return; | 42 return; |
| 75 weak_factory_.InvalidateWeakPtrs(); | 43 weak_factory_.InvalidateWeakPtrs(); |
| 76 DCHECK_EQ(MotionNotify, last_xmotion_.type); | 44 DCHECK_EQ(MotionNotify, last_xmotion_.type); |
| 77 delegate_->OnMouseMovement(&last_xmotion_); | 45 delegate_->OnMouseMovement(&last_xmotion_); |
| 78 last_xmotion_.type = LASTEvent; | 46 last_xmotion_.type = LASTEvent; |
| 79 } | 47 } |
| 80 | 48 |
| 81 //////////////////////////////////////////////////////////////////////////////// | 49 //////////////////////////////////////////////////////////////////////////////// |
| 82 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: | 50 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: |
| 83 | 51 |
| 84 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { | 52 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { |
| 85 return in_move_loop_; | 53 NOTREACHED(); |
| 54 return true; |
| 86 } | 55 } |
| 87 | 56 |
| 88 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { | 57 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { |
| 89 // This method processes all events for the grab_input_window_ as well as | 58 // This method processes all mouse events for all windows while the move loop |
| 90 // mouse events for all windows while the move loop is active - even before | 59 // is active. |
| 91 // the grab is granted by X. This allows mouse notification events that were | |
| 92 // sent after the capture was requested but before the capture was granted | |
| 93 // to be dispatched. It is especially important to process the mouse release | |
| 94 // event that should have stopped the drag even if that mouse release happened | |
| 95 // before the grab was granted. | |
| 96 if (!in_move_loop_) | 60 if (!in_move_loop_) |
| 97 return ui::POST_DISPATCH_PERFORM_DEFAULT; | 61 return ui::POST_DISPATCH_PERFORM_DEFAULT; |
| 98 XEvent* xev = event; | 62 XEvent* xev = event; |
| 99 | 63 |
| 100 // Note: the escape key is handled in the tab drag controller, which has | 64 // Note: the escape key is handled in the tab drag controller, which has |
| 101 // keyboard focus even though we took pointer grab. | 65 // keyboard focus even though we took pointer grab. |
| 102 switch (xev->type) { | 66 switch (xev->type) { |
| 103 case MotionNotify: { | 67 case MotionNotify: { |
| 104 if (drag_widget_.get()) { | |
| 105 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
| 106 gfx::Point location = gfx::ToFlooredPoint( | |
| 107 screen->GetCursorScreenPoint() - drag_offset_); | |
| 108 drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size())); | |
| 109 drag_widget_->StackAtTop(); | |
| 110 } | |
| 111 last_xmotion_ = xev->xmotion; | 68 last_xmotion_ = xev->xmotion; |
| 112 if (!weak_factory_.HasWeakPtrs()) { | 69 if (!weak_factory_.HasWeakPtrs()) { |
| 113 // Post a task to dispatch mouse movement event when control returns to | 70 // Post a task to dispatch mouse movement event when control returns to |
| 114 // the message loop. This allows smoother dragging since the events are | 71 // the message loop. This allows smoother dragging since the events are |
| 115 // dispatched without waiting for the drag widget updates. | 72 // dispatched without waiting for the drag widget updates. |
| 116 base::MessageLoopForUI::current()->PostTask( | 73 base::MessageLoopForUI::current()->PostTask( |
| 117 FROM_HERE, | 74 FROM_HERE, |
| 118 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, | 75 base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement, |
| 119 weak_factory_.GetWeakPtr())); | 76 weak_factory_.GetWeakPtr())); |
| 120 } | 77 } |
| 121 return ui::POST_DISPATCH_NONE; | 78 return ui::POST_DISPATCH_NONE; |
| 122 } | 79 } |
| 123 case ButtonRelease: { | 80 case ButtonRelease: { |
| 124 if (xev->xbutton.button == Button1) { | 81 if (xev->xbutton.button == Button1) { |
| 125 // Assume that drags are being done with the left mouse button. Only | 82 // Assume that drags are being done with the left mouse button. Only |
| 126 // break the drag if the left mouse button was released. | 83 // break the drag if the left mouse button was released. |
| 127 DispatchMouseMovement(); | 84 DispatchMouseMovement(); |
| 128 delegate_->OnMouseReleased(); | 85 delegate_->OnMouseReleased(); |
| 129 } | 86 } |
| 130 return ui::POST_DISPATCH_NONE; | 87 // Default will end up releasing capture in Widget::OnMouseEvent(). |
| 88 return ui::POST_DISPATCH_PERFORM_DEFAULT; |
| 131 } | 89 } |
| 132 case KeyPress: { | 90 case KeyPress: { |
| 133 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) { | 91 if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE) { |
| 134 canceled_ = true; | 92 canceled_ = true; |
| 135 EndMoveLoop(); | 93 EndMoveLoop(); |
| 94 // After the move loop ends the capture will get released. |
| 95 // How it gets released depends on what move loop this is (i.e. tab |
| 96 // dragging or drag and drop). |
| 97 // When dragging browser tabs this will happen when the tab loses focus |
| 98 // since either the dragged browser frame is getting destroyed (for |
| 99 // dragging out into a new frame) or the focus gets moved back to the |
| 100 // source browser (when trying to drag into another browser in which |
| 101 // case the EscapeTracker will dispatch the KeyPress event). |
| 102 // For drag and drop scenarios the capture is on the drag widget and |
| 103 // will get lost when the drag widget is destroyed in |
| 104 // DesktopDragDropClientAuraX11::OnMoveLoopEnded(). |
| 136 return ui::POST_DISPATCH_NONE; | 105 return ui::POST_DISPATCH_NONE; |
| 137 } | 106 } |
| 138 break; | 107 break; |
| 139 } | 108 } |
| 140 case FocusOut: { | 109 case FocusOut: { |
| 141 if (xev->xfocus.mode != NotifyGrab) | 110 if (xev->xfocus.mode != NotifyGrab) |
| 142 has_grab_ = false; | 111 has_grab_ = false; |
| 143 break; | 112 break; |
| 144 } | 113 } |
| 145 case GenericEvent: { | 114 case GenericEvent: { |
| 146 ui::EventType type = ui::EventTypeFromNative(xev); | 115 ui::EventType type = ui::EventTypeFromNative(xev); |
| 147 switch (type) { | 116 switch (type) { |
| 148 case ui::ET_MOUSE_MOVED: | 117 case ui::ET_MOUSE_MOVED: |
| 149 case ui::ET_MOUSE_DRAGGED: | 118 case ui::ET_MOUSE_DRAGGED: |
| 150 case ui::ET_MOUSE_RELEASED: { | 119 case ui::ET_MOUSE_RELEASED: { |
| 151 XEvent xevent = {0}; | 120 XEvent xevent = {0}; |
| 152 if (type == ui::ET_MOUSE_RELEASED) { | 121 if (type == ui::ET_MOUSE_RELEASED) { |
| 153 xevent.type = ButtonRelease; | 122 xevent.type = ButtonRelease; |
| 154 xevent.xbutton.button = ui::EventButtonFromNative(xev); | 123 xevent.xbutton.button = ui::EventButtonFromNative(xev); |
| 155 } else { | 124 } else { |
| 156 xevent.type = MotionNotify; | 125 xevent.type = MotionNotify; |
| 157 } | 126 } |
| 158 xevent.xany.display = xev->xgeneric.display; | 127 xevent.xany.display = xev->xgeneric.display; |
| 159 xevent.xany.window = grab_input_window_; | 128 XIDeviceEvent* xievent = |
| 129 static_cast<XIDeviceEvent*>(xev->xcookie.data); |
| 130 xevent.xany.window = xievent->event; |
| 160 // The fields used below are in the same place for all of events | 131 // The fields used below are in the same place for all of events |
| 161 // above. Using xmotion from XEvent's unions to avoid repeating | 132 // above. Using xmotion from XEvent's unions to avoid repeating |
| 162 // the code. | 133 // the code. |
| 163 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display); | 134 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display); |
| 164 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds(); | 135 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds(); |
| 165 gfx::Point point(ui::EventSystemLocationFromNative(xev)); | 136 gfx::Point point(ui::EventSystemLocationFromNative(xev)); |
| 166 xevent.xmotion.x_root = point.x(); | 137 xevent.xmotion.x_root = point.x(); |
| 167 xevent.xmotion.y_root = point.y(); | 138 xevent.xmotion.y_root = point.y(); |
| 168 DispatchEvent(&xevent); | 139 return DispatchEvent(&xevent); |
| 169 return ui::POST_DISPATCH_NONE; | |
| 170 } | 140 } |
| 171 default: | 141 default: |
| 172 break; | 142 break; |
| 173 } | 143 } |
| 174 } | 144 } |
| 175 } | 145 } |
| 176 | 146 |
| 177 return (event->xany.window == grab_input_window_) ? | 147 return ui::POST_DISPATCH_PERFORM_DEFAULT; |
| 178 ui::POST_DISPATCH_NONE : ui::POST_DISPATCH_PERFORM_DEFAULT; | |
| 179 } | 148 } |
| 180 | 149 |
| 181 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, | 150 bool X11WholeScreenMoveLoop::RunMoveLoop() { |
| 182 gfx::NativeCursor cursor) { | |
| 183 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 151 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
| 184 | 152 |
| 185 // Start a capture on the host, so that it continues to receive events during | |
| 186 // the drag. This may be second time we are capturing the mouse events - the | |
| 187 // first being when a mouse is first pressed. That first capture needs to be | |
| 188 // released before the call to GrabPointerAndKeyboard below, otherwise it may | |
| 189 // get released while we still need the pointer grab, which is why we restrict | |
| 190 // the scope here. | |
| 191 { | |
| 192 ScopedCapturer capturer(source->GetHost()); | |
| 193 | |
| 194 grab_input_window_ = CreateDragInputWindow(gfx::GetXDisplay()); | |
| 195 // Releasing ScopedCapturer ensures that any other instance of | |
| 196 // X11ScopedCapture will not prematurely release grab that will be acquired | |
| 197 // below. | |
| 198 } | |
| 199 // TODO(varkha): Consider integrating GrabPointerAndKeyboard with | |
| 200 // ScopedCapturer to avoid possibility of logically keeping multiple grabs. | |
| 201 if (!GrabPointerAndKeyboard(cursor)) { | |
| 202 XDestroyWindow(gfx::GetXDisplay(), grab_input_window_); | |
| 203 return false; | |
| 204 } | |
| 205 | |
| 206 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = | 153 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = |
| 207 nested_dispatcher_.Pass(); | 154 nested_dispatcher_.Pass(); |
| 208 nested_dispatcher_ = | 155 nested_dispatcher_ = |
| 209 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); | 156 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); |
| 210 if (!drag_image_.isNull() && CheckIfIconValid()) | |
| 211 CreateDragImageWindow(); | |
| 212 | 157 |
| 213 // We are handling a mouse drag outside of the aura::RootWindow system. We | 158 // We are handling a mouse drag outside of the aura::RootWindow system. We |
| 214 // must manually make aura think that the mouse button is pressed so that we | 159 // must manually make aura think that the mouse button is pressed so that we |
| 215 // don't draw extraneous tooltips. | 160 // don't draw extraneous tooltips. |
| 216 aura::Env* env = aura::Env::GetInstance(); | 161 aura::Env* env = aura::Env::GetInstance(); |
| 217 if (!env->IsMouseButtonDown()) { | 162 if (!env->IsMouseButtonDown()) { |
| 218 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); | 163 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); |
| 219 should_reset_mouse_flags_ = true; | 164 should_reset_mouse_flags_ = true; |
| 220 } | 165 } |
| 221 | 166 |
| 222 in_move_loop_ = true; | 167 in_move_loop_ = true; |
| 223 canceled_ = false; | 168 canceled_ = false; |
| 224 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 169 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
| 225 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); | 170 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); |
| 226 base::RunLoop run_loop; | 171 base::RunLoop run_loop; |
| 227 quit_closure_ = run_loop.QuitClosure(); | 172 quit_closure_ = run_loop.QuitClosure(); |
| 228 run_loop.Run(); | 173 run_loop.Run(); |
| 229 nested_dispatcher_ = old_dispatcher.Pass(); | 174 nested_dispatcher_ = old_dispatcher.Pass(); |
| 230 return !canceled_; | 175 return !canceled_; |
| 231 } | 176 } |
| 232 | 177 |
| 233 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { | |
| 234 if (in_move_loop_) { | |
| 235 // If we're still in the move loop, regrab the pointer with the updated | |
| 236 // cursor. Note: we can be called from handling an XdndStatus message after | |
| 237 // EndMoveLoop() was called, but before we return from the nested RunLoop. | |
| 238 GrabPointerAndKeyboard(cursor); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 void X11WholeScreenMoveLoop::EndMoveLoop() { | 178 void X11WholeScreenMoveLoop::EndMoveLoop() { |
| 243 if (!in_move_loop_) | 179 if (!in_move_loop_) |
| 244 return; | 180 return; |
| 245 | 181 |
| 246 // Prevent DispatchMouseMovement from dispatching any posted motion event. | 182 // Prevent DispatchMouseMovement from dispatching any posted motion event. |
| 247 weak_factory_.InvalidateWeakPtrs(); | 183 weak_factory_.InvalidateWeakPtrs(); |
| 248 last_xmotion_.type = LASTEvent; | 184 last_xmotion_.type = LASTEvent; |
| 249 | 185 |
| 250 // We undo our emulated mouse click from RunMoveLoop(); | 186 // We undo our emulated mouse click from RunMoveLoop(); |
| 251 if (should_reset_mouse_flags_) { | 187 if (should_reset_mouse_flags_) { |
| 252 aura::Env::GetInstance()->set_mouse_button_flags(0); | 188 aura::Env::GetInstance()->set_mouse_button_flags(0); |
| 253 should_reset_mouse_flags_ = false; | 189 should_reset_mouse_flags_ = false; |
| 254 } | 190 } |
| 255 | 191 |
| 256 // TODO(erg): Is this ungrab the cause of having to click to give input focus | |
| 257 // on drawn out windows? Not ungrabbing here screws the X server until I kill | |
| 258 // the chrome process. | |
| 259 | |
| 260 // Ungrab before we let go of the window. | |
| 261 XDisplay* display = gfx::GetXDisplay(); | |
| 262 // Only ungrab pointer if capture was not switched to another window. | |
| 263 if (has_grab_) { | |
| 264 XUngrabPointer(display, CurrentTime); | |
| 265 XUngrabKeyboard(display, CurrentTime); | |
| 266 } | |
| 267 | |
| 268 // Restore the previous dispatcher. | 192 // Restore the previous dispatcher. |
| 269 nested_dispatcher_.reset(); | 193 nested_dispatcher_.reset(); |
| 270 drag_widget_.reset(); | |
| 271 delegate_->OnMoveLoopEnded(); | 194 delegate_->OnMoveLoopEnded(); |
| 272 XDestroyWindow(display, grab_input_window_); | |
| 273 grab_input_window_ = None; | |
| 274 | 195 |
| 275 in_move_loop_ = false; | 196 in_move_loop_ = false; |
| 276 quit_closure_.Run(); | 197 quit_closure_.Run(); |
| 277 } | 198 } |
| 278 | 199 |
| 279 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, | |
| 280 gfx::Vector2dF offset) { | |
| 281 drag_image_ = image; | |
| 282 drag_offset_ = offset; | |
| 283 // Reset the Y offset, so that the drag-image is always just below the cursor, | |
| 284 // so that it is possible to see where the cursor is going. | |
| 285 drag_offset_.set_y(0.f); | |
| 286 } | |
| 287 | |
| 288 bool X11WholeScreenMoveLoop::GrabPointerAndKeyboard(gfx::NativeCursor cursor) { | |
| 289 XDisplay* display = gfx::GetXDisplay(); | |
| 290 XGrabServer(display); | |
| 291 | |
| 292 XUngrabPointer(display, CurrentTime); | |
| 293 int ret = XGrabPointer( | |
| 294 display, | |
| 295 grab_input_window_, | |
| 296 False, | |
| 297 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, | |
| 298 GrabModeAsync, | |
| 299 GrabModeAsync, | |
| 300 None, | |
| 301 cursor.platform(), | |
| 302 CurrentTime); | |
| 303 if (ret != GrabSuccess) { | |
| 304 DLOG(ERROR) << "Grabbing pointer for dragging failed: " | |
| 305 << ui::GetX11ErrorString(display, ret); | |
| 306 } else { | |
| 307 has_grab_ = true; | |
| 308 XUngrabKeyboard(display, CurrentTime); | |
| 309 ret = XGrabKeyboard( | |
| 310 display, | |
| 311 grab_input_window_, | |
| 312 False, | |
| 313 GrabModeAsync, | |
| 314 GrabModeAsync, | |
| 315 CurrentTime); | |
| 316 if (ret != GrabSuccess) { | |
| 317 DLOG(ERROR) << "Grabbing keyboard for dragging failed: " | |
| 318 << ui::GetX11ErrorString(display, ret); | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 XUngrabServer(display); | |
| 323 XFlush(display); | |
| 324 return ret == GrabSuccess; | |
| 325 } | |
| 326 | |
| 327 Window X11WholeScreenMoveLoop::CreateDragInputWindow(XDisplay* display) { | |
| 328 // Creates an invisible, InputOnly toplevel window. This window will receive | |
| 329 // all mouse movement for drags. It turns out that normal windows doing a | |
| 330 // grab doesn't redirect pointer motion events if the pointer isn't over the | |
| 331 // grabbing window. But InputOnly windows are able to grab everything. This | |
| 332 // is what GTK+ does, and I found a patch to KDE that did something similar. | |
| 333 unsigned long attribute_mask = CWEventMask | CWOverrideRedirect; | |
| 334 XSetWindowAttributes swa; | |
| 335 memset(&swa, 0, sizeof(swa)); | |
| 336 swa.event_mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | | |
| 337 KeyPressMask | KeyReleaseMask | StructureNotifyMask; | |
| 338 swa.override_redirect = True; | |
| 339 Window window = XCreateWindow(display, | |
| 340 DefaultRootWindow(display), | |
| 341 -100, -100, 10, 10, | |
| 342 0, CopyFromParent, InputOnly, CopyFromParent, | |
| 343 attribute_mask, &swa); | |
| 344 XMapRaised(display, window); | |
| 345 ui::X11EventSource::GetInstance()->BlockUntilWindowMapped(window); | |
| 346 return window; | |
| 347 } | |
| 348 | |
| 349 void X11WholeScreenMoveLoop::CreateDragImageWindow() { | |
| 350 Widget* widget = new Widget; | |
| 351 Widget::InitParams params(Widget::InitParams::TYPE_DRAG); | |
| 352 params.opacity = Widget::InitParams::OPAQUE_WINDOW; | |
| 353 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 354 params.accept_events = false; | |
| 355 | |
| 356 gfx::Point location = gfx::ToFlooredPoint( | |
| 357 gfx::Screen::GetNativeScreen()->GetCursorScreenPoint() - drag_offset_); | |
| 358 params.bounds = gfx::Rect(location, drag_image_.size()); | |
| 359 widget->set_focus_on_creation(false); | |
| 360 widget->set_frame_type(Widget::FRAME_TYPE_FORCE_NATIVE); | |
| 361 widget->Init(params); | |
| 362 widget->SetOpacity(kDragWidgetOpacity); | |
| 363 widget->GetNativeWindow()->SetName("DragWindow"); | |
| 364 | |
| 365 ImageView* image = new ImageView(); | |
| 366 image->SetImage(drag_image_); | |
| 367 image->SetBounds(0, 0, drag_image_.width(), drag_image_.height()); | |
| 368 widget->SetContentsView(image); | |
| 369 widget->Show(); | |
| 370 widget->GetNativeWindow()->layer()->SetFillsBoundsOpaquely(false); | |
| 371 | |
| 372 drag_widget_.reset(widget); | |
| 373 } | |
| 374 | |
| 375 bool X11WholeScreenMoveLoop::CheckIfIconValid() { | |
| 376 // Because we need a GL context per window, we do a quick check so that we | |
| 377 // don't make another context if the window would just be displaying a mostly | |
| 378 // transparent image. | |
| 379 const SkBitmap* in_bitmap = drag_image_.bitmap(); | |
| 380 SkAutoLockPixels in_lock(*in_bitmap); | |
| 381 for (int y = 0; y < in_bitmap->height(); ++y) { | |
| 382 uint32* in_row = in_bitmap->getAddr32(0, y); | |
| 383 | |
| 384 for (int x = 0; x < in_bitmap->width(); ++x) { | |
| 385 if (SkColorGetA(in_row[x]) > kMinAlpha) | |
| 386 return true; | |
| 387 } | |
| 388 } | |
| 389 | |
| 390 return false; | |
| 391 } | |
| 392 | |
| 393 } // namespace views | 200 } // namespace views |
| OLD | NEW |