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 "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/aura/env.h" | 16 #include "ui/aura/env.h" |
16 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 weak_factory_.InvalidateWeakPtrs(); | 75 weak_factory_.InvalidateWeakPtrs(); |
75 DCHECK_EQ(MotionNotify, last_xmotion_.type); | 76 DCHECK_EQ(MotionNotify, last_xmotion_.type); |
76 delegate_->OnMouseMovement(&last_xmotion_); | 77 delegate_->OnMouseMovement(&last_xmotion_); |
77 last_xmotion_.type = LASTEvent; | 78 last_xmotion_.type = LASTEvent; |
78 } | 79 } |
79 | 80 |
80 //////////////////////////////////////////////////////////////////////////////// | 81 //////////////////////////////////////////////////////////////////////////////// |
81 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: | 82 // DesktopWindowTreeHostLinux, ui::PlatformEventDispatcher implementation: |
82 | 83 |
83 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { | 84 bool X11WholeScreenMoveLoop::CanDispatchEvent(const ui::PlatformEvent& event) { |
85 NOTREACHED(); | |
84 return in_move_loop_; | 86 return in_move_loop_; |
85 } | 87 } |
86 | 88 |
87 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { | 89 uint32_t X11WholeScreenMoveLoop::DispatchEvent(const ui::PlatformEvent& event) { |
88 // This method processes all events for the grab_input_window_ as well as | 90 // This method processes all mouse events for all windows while the move loop |
89 // mouse events for all windows while the move loop is active - even before | 91 // is active. |
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_) | 92 if (!in_move_loop_) |
96 return ui::POST_DISPATCH_PERFORM_DEFAULT; | 93 return ui::POST_DISPATCH_PERFORM_DEFAULT; |
97 XEvent* xev = event; | 94 XEvent* xev = event; |
98 | 95 |
99 // Note: the escape key is handled in the tab drag controller, which has | 96 // Note: the escape key is handled in the tab drag controller, which has |
100 // keyboard focus even though we took pointer grab. | 97 // keyboard focus even though we took pointer grab. |
101 switch (xev->type) { | 98 switch (xev->type) { |
102 case MotionNotify: { | 99 case MotionNotify: { |
103 if (drag_widget_.get()) { | 100 if (drag_widget_.get()) { |
104 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | 101 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 case ui::ET_MOUSE_DRAGGED: | 140 case ui::ET_MOUSE_DRAGGED: |
144 case ui::ET_MOUSE_RELEASED: { | 141 case ui::ET_MOUSE_RELEASED: { |
145 XEvent xevent = {0}; | 142 XEvent xevent = {0}; |
146 if (type == ui::ET_MOUSE_RELEASED) { | 143 if (type == ui::ET_MOUSE_RELEASED) { |
147 xevent.type = ButtonRelease; | 144 xevent.type = ButtonRelease; |
148 xevent.xbutton.button = ui::EventButtonFromNative(xev); | 145 xevent.xbutton.button = ui::EventButtonFromNative(xev); |
149 } else { | 146 } else { |
150 xevent.type = MotionNotify; | 147 xevent.type = MotionNotify; |
151 } | 148 } |
152 xevent.xany.display = xev->xgeneric.display; | 149 xevent.xany.display = xev->xgeneric.display; |
153 xevent.xany.window = grab_input_window_; | 150 XIDeviceEvent* xievent = |
151 static_cast<XIDeviceEvent*>(xev->xcookie.data); | |
152 xevent.xany.window = xievent->event; | |
154 // The fields used below are in the same place for all of events | 153 // The fields used below are in the same place for all of events |
155 // above. Using xmotion from XEvent's unions to avoid repeating | 154 // above. Using xmotion from XEvent's unions to avoid repeating |
156 // the code. | 155 // the code. |
157 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display); | 156 xevent.xmotion.root = DefaultRootWindow(xev->xgeneric.display); |
158 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds(); | 157 xevent.xmotion.time = ui::EventTimeFromNative(xev).InMilliseconds(); |
159 gfx::Point point(ui::EventSystemLocationFromNative(xev)); | 158 gfx::Point point(ui::EventSystemLocationFromNative(xev)); |
160 xevent.xmotion.x_root = point.x(); | 159 xevent.xmotion.x_root = point.x(); |
161 xevent.xmotion.y_root = point.y(); | 160 xevent.xmotion.y_root = point.y(); |
162 DispatchEvent(&xevent); | 161 DispatchEvent(&xevent); |
163 return ui::POST_DISPATCH_NONE; | 162 return ui::POST_DISPATCH_NONE; |
pkotwicz
2014/05/05 00:09:05
This should be: "return DispatchEvent(&xevent);"
varkha
2014/05/05 16:37:14
Done.
| |
164 } | 163 } |
165 default: | 164 default: |
166 break; | 165 break; |
167 } | 166 } |
168 } | 167 } |
169 } | 168 } |
170 | 169 |
171 return (event->xany.window == grab_input_window_) ? | 170 return ui::POST_DISPATCH_PERFORM_DEFAULT; |
172 ui::POST_DISPATCH_NONE : ui::POST_DISPATCH_PERFORM_DEFAULT; | |
173 } | 171 } |
174 | 172 |
175 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, | 173 bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source, |
176 gfx::NativeCursor cursor) { | 174 gfx::NativeCursor cursor) { |
177 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 175 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
178 | 176 |
179 // Start a capture on the host, so that it continues to receive events during | 177 // 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 | 178 // the drag. |
181 // first being when a mouse is first pressed. That first capture needs to be | 179 ScopedCapturer capturer(source->GetHost()); |
pkotwicz
2014/05/02 19:50:39
This is not really right. X11WholeScreenMoveLoop::
varkha
2014/05/02 23:58:11
I have changed it so that the capture is only kept
pkotwicz
2014/05/05 00:09:05
On CrOS the capture is released as a result of vie
varkha
2014/05/05 16:37:14
Yes we can! Done.
| |
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. | |
185 { | |
186 ScopedCapturer capturer(source->GetHost()); | |
187 | |
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. | |
192 } | |
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; | |
198 } | |
199 | 180 |
200 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = | 181 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = |
201 nested_dispatcher_.Pass(); | 182 nested_dispatcher_.Pass(); |
202 nested_dispatcher_ = | 183 nested_dispatcher_ = |
203 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); | 184 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(this); |
204 if (!drag_image_.isNull() && CheckIfIconValid()) | 185 if (!drag_image_.isNull() && CheckIfIconValid()) |
205 CreateDragImageWindow(); | 186 CreateDragImageWindow(); |
206 | 187 |
207 // We are handling a mouse drag outside of the aura::RootWindow system. We | 188 // 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 | 189 // must manually make aura think that the mouse button is pressed so that we |
209 // don't draw extraneous tooltips. | 190 // don't draw extraneous tooltips. |
210 aura::Env* env = aura::Env::GetInstance(); | 191 aura::Env* env = aura::Env::GetInstance(); |
211 if (!env->IsMouseButtonDown()) { | 192 if (!env->IsMouseButtonDown()) { |
212 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); | 193 env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON); |
213 should_reset_mouse_flags_ = true; | 194 should_reset_mouse_flags_ = true; |
214 } | 195 } |
215 | 196 |
216 in_move_loop_ = true; | 197 in_move_loop_ = true; |
217 canceled_ = false; | 198 canceled_ = false; |
218 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | 199 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
219 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); | 200 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); |
220 base::RunLoop run_loop; | 201 base::RunLoop run_loop; |
221 quit_closure_ = run_loop.QuitClosure(); | 202 quit_closure_ = run_loop.QuitClosure(); |
222 run_loop.Run(); | 203 run_loop.Run(); |
223 nested_dispatcher_ = old_dispatcher.Pass(); | 204 nested_dispatcher_ = old_dispatcher.Pass(); |
224 return !canceled_; | 205 return !canceled_; |
225 } | 206 } |
226 | 207 |
227 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { | 208 void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) { |
228 if (in_move_loop_) { | 209 if (in_move_loop_) { |
pkotwicz
2014/05/02 19:50:39
I wonder whether we could do a grab on the window
varkha
2014/05/02 23:58:11
I don't think it will work. GetGlobalCaptureWindow
pkotwicz
2014/05/05 00:09:05
I tried this quickly and GetGlobalCaptureWindow()
varkha
2014/05/05 16:37:14
It did not work because I was still using ScopedCa
| |
229 // If we're still in the move loop, regrab the pointer with the updated | 210 // 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 | 211 // cursor. Note: we can be called from handling an XdndStatus message after |
231 // EndMoveLoop() was called, but before we return from the nested RunLoop. | 212 // EndMoveLoop() was called, but before we return from the nested RunLoop. |
232 GrabPointerAndKeyboard(cursor); | 213 GrabPointerAndKeyboard(cursor); |
233 } | 214 } |
234 } | 215 } |
235 | 216 |
236 void X11WholeScreenMoveLoop::EndMoveLoop() { | 217 void X11WholeScreenMoveLoop::EndMoveLoop() { |
237 if (!in_move_loop_) | 218 if (!in_move_loop_) |
238 return; | 219 return; |
239 | 220 |
240 // Prevent DispatchMouseMovement from dispatching any posted motion event. | 221 // Prevent DispatchMouseMovement from dispatching any posted motion event. |
241 weak_factory_.InvalidateWeakPtrs(); | 222 weak_factory_.InvalidateWeakPtrs(); |
242 last_xmotion_.type = LASTEvent; | 223 last_xmotion_.type = LASTEvent; |
243 | 224 |
244 // We undo our emulated mouse click from RunMoveLoop(); | 225 // We undo our emulated mouse click from RunMoveLoop(); |
245 if (should_reset_mouse_flags_) { | 226 if (should_reset_mouse_flags_) { |
246 aura::Env::GetInstance()->set_mouse_button_flags(0); | 227 aura::Env::GetInstance()->set_mouse_button_flags(0); |
247 should_reset_mouse_flags_ = false; | 228 should_reset_mouse_flags_ = false; |
248 } | 229 } |
249 | 230 |
250 // TODO(erg): Is this ungrab the cause of having to click to give input focus | 231 // 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 | 232 // on drawn out windows? Not ungrabbing here screws the X server until I kill |
252 // the chrome process. | 233 // the chrome process. |
253 | 234 |
254 // Ungrab before we let go of the window. | |
255 XDisplay* display = gfx::GetXDisplay(); | |
256 XUngrabPointer(display, CurrentTime); | |
257 XUngrabKeyboard(display, CurrentTime); | |
258 | |
259 // Restore the previous dispatcher. | 235 // Restore the previous dispatcher. |
260 nested_dispatcher_.reset(); | 236 nested_dispatcher_.reset(); |
261 drag_widget_.reset(); | 237 drag_widget_.reset(); |
262 delegate_->OnMoveLoopEnded(); | 238 delegate_->OnMoveLoopEnded(); |
263 XDestroyWindow(display, grab_input_window_); | |
264 grab_input_window_ = None; | 239 grab_input_window_ = None; |
265 | 240 |
266 in_move_loop_ = false; | 241 in_move_loop_ = false; |
267 quit_closure_.Run(); | 242 quit_closure_.Run(); |
268 } | 243 } |
269 | 244 |
270 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, | 245 void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image, |
271 gfx::Vector2dF offset) { | 246 gfx::Vector2dF offset) { |
272 drag_image_ = image; | 247 drag_image_ = image; |
273 drag_offset_ = offset; | 248 drag_offset_ = offset; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 widget->Init(params); | 326 widget->Init(params); |
352 widget->SetOpacity(kDragWidgetOpacity); | 327 widget->SetOpacity(kDragWidgetOpacity); |
353 widget->GetNativeWindow()->SetName("DragWindow"); | 328 widget->GetNativeWindow()->SetName("DragWindow"); |
354 | 329 |
355 ImageView* image = new ImageView(); | 330 ImageView* image = new ImageView(); |
356 image->SetImage(drag_image_); | 331 image->SetImage(drag_image_); |
357 image->SetBounds(0, 0, drag_image_.width(), drag_image_.height()); | 332 image->SetBounds(0, 0, drag_image_.width(), drag_image_.height()); |
358 widget->SetContentsView(image); | 333 widget->SetContentsView(image); |
359 widget->Show(); | 334 widget->Show(); |
360 widget->GetNativeWindow()->layer()->SetFillsBoundsOpaquely(false); | 335 widget->GetNativeWindow()->layer()->SetFillsBoundsOpaquely(false); |
336 grab_input_window_ = widget->GetNativeWindow()->GetHost()-> | |
337 GetAcceleratedWidget(); | |
361 | 338 |
362 drag_widget_.reset(widget); | 339 drag_widget_.reset(widget); |
363 } | 340 } |
364 | 341 |
365 bool X11WholeScreenMoveLoop::CheckIfIconValid() { | 342 bool X11WholeScreenMoveLoop::CheckIfIconValid() { |
366 // TODO(erg): I've tried at least five different strategies for trying to | 343 // TODO(erg): I've tried at least five different strategies for trying to |
367 // build a mask based off the alpha channel. While all of them have worked, | 344 // build a mask based off the alpha channel. While all of them have worked, |
368 // none of them have been performant and introduced multiple second | 345 // none of them have been performant and introduced multiple second |
369 // delays. (I spent a day getting a rectangle segmentation algorithm polished | 346 // delays. (I spent a day getting a rectangle segmentation algorithm polished |
370 // here...and then found that even through I had the rectangle extraction | 347 // here...and then found that even through I had the rectangle extraction |
(...skipping 11 matching lines...) Expand all Loading... | |
382 for (int x = 0; x < in_bitmap->width(); ++x) { | 359 for (int x = 0; x < in_bitmap->width(); ++x) { |
383 if (SkColorGetA(in_row[x]) > kMinAlpha) | 360 if (SkColorGetA(in_row[x]) > kMinAlpha) |
384 return true; | 361 return true; |
385 } | 362 } |
386 } | 363 } |
387 | 364 |
388 return false; | 365 return false; |
389 } | 366 } |
390 | 367 |
391 } // namespace views | 368 } // namespace views |
OLD | NEW |