This source file includes following definitions.
- weak_factory_
- DispatchMouseMovement
- Dispatch
- RunMoveLoop
- UpdateCursor
- EndMoveLoop
- SetDragImage
- GrabPointerAndKeyboard
- CreateDragInputWindow
- CreateDragImageWindow
- CheckIfIconValid
#include "ui/views/widget/desktop_aura/x11_whole_screen_move_loop.h"
#include <X11/Xlib.h>
#undef RootWindow
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_pump_x11.h"
#include "base/run_loop.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/x/x11_util.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/gfx/point_conversions.h"
#include "ui/gfx/screen.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace {
const uint32 kMinAlpha = 32;
const unsigned char kDragWidgetOpacity = 0xc0;
class ScopedCapturer {
public:
explicit ScopedCapturer(aura::WindowTreeHost* host)
: host_(host) {
host_->SetCapture();
}
~ScopedCapturer() {
host_->ReleaseCapture();
}
private:
aura::WindowTreeHost* host_;
DISALLOW_COPY_AND_ASSIGN(ScopedCapturer);
};
}
X11WholeScreenMoveLoop::X11WholeScreenMoveLoop(
X11WholeScreenMoveLoopDelegate* delegate)
: delegate_(delegate),
in_move_loop_(false),
should_reset_mouse_flags_(false),
grab_input_window_(None),
weak_factory_(this) {
last_xmotion_.type = LASTEvent;
}
X11WholeScreenMoveLoop::~X11WholeScreenMoveLoop() {}
void X11WholeScreenMoveLoop::DispatchMouseMovement() {
if (!weak_factory_.HasWeakPtrs())
return;
weak_factory_.InvalidateWeakPtrs();
DCHECK_EQ(MotionNotify, last_xmotion_.type);
delegate_->OnMouseMovement(&last_xmotion_);
last_xmotion_.type = LASTEvent;
}
uint32_t X11WholeScreenMoveLoop::Dispatch(const base::NativeEvent& event) {
XEvent* xev = event;
switch (xev->type) {
case MotionNotify: {
if (drag_widget_.get()) {
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
gfx::Point location = gfx::ToFlooredPoint(
screen->GetCursorScreenPoint() - drag_offset_);
drag_widget_->SetBounds(gfx::Rect(location, drag_image_.size()));
drag_widget_->StackAtTop();
}
last_xmotion_ = xev->xmotion;
if (!weak_factory_.HasWeakPtrs()) {
base::MessageLoopForUI::current()->PostTask(
FROM_HERE,
base::Bind(&X11WholeScreenMoveLoop::DispatchMouseMovement,
weak_factory_.GetWeakPtr()));
}
break;
}
case ButtonRelease: {
if (xev->xbutton.button == Button1) {
DispatchMouseMovement();
delegate_->OnMouseReleased();
}
break;
}
case KeyPress: {
if (ui::KeyboardCodeFromXKeyEvent(xev) == ui::VKEY_ESCAPE)
EndMoveLoop();
break;
}
}
return POST_DISPATCH_NONE;
}
bool X11WholeScreenMoveLoop::RunMoveLoop(aura::Window* source,
gfx::NativeCursor cursor) {
{
ScopedCapturer capturer(source->GetHost());
DCHECK(!in_move_loop_);
in_move_loop_ = true;
XDisplay* display = gfx::GetXDisplay();
grab_input_window_ = CreateDragInputWindow(display);
if (!drag_image_.isNull() && CheckIfIconValid())
CreateDragImageWindow();
base::MessagePumpX11::Current()->AddDispatcherForWindow(
this, grab_input_window_);
}
if (!GrabPointerAndKeyboard(cursor))
return false;
aura::Env* env = aura::Env::GetInstance();
if (!env->IsMouseButtonDown()) {
env->set_mouse_button_flags(ui::EF_LEFT_MOUSE_BUTTON);
should_reset_mouse_flags_ = true;
}
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
return true;
}
void X11WholeScreenMoveLoop::UpdateCursor(gfx::NativeCursor cursor) {
if (in_move_loop_) {
GrabPointerAndKeyboard(cursor);
}
}
void X11WholeScreenMoveLoop::EndMoveLoop() {
if (!in_move_loop_)
return;
weak_factory_.InvalidateWeakPtrs();
last_xmotion_.type = LASTEvent;
if (should_reset_mouse_flags_) {
aura::Env::GetInstance()->set_mouse_button_flags(0);
should_reset_mouse_flags_ = false;
}
XDisplay* display = gfx::GetXDisplay();
XUngrabPointer(display, CurrentTime);
XUngrabKeyboard(display, CurrentTime);
base::MessagePumpX11::Current()->RemoveDispatcherForWindow(
grab_input_window_);
drag_widget_.reset();
delegate_->OnMoveLoopEnded();
XDestroyWindow(display, grab_input_window_);
in_move_loop_ = false;
quit_closure_.Run();
}
void X11WholeScreenMoveLoop::SetDragImage(const gfx::ImageSkia& image,
gfx::Vector2dF offset) {
drag_image_ = image;
drag_offset_ = offset;
drag_offset_.set_y(0.f);
}
bool X11WholeScreenMoveLoop::GrabPointerAndKeyboard(gfx::NativeCursor cursor) {
XDisplay* display = gfx::GetXDisplay();
XGrabServer(display);
XUngrabPointer(display, CurrentTime);
int ret = XGrabPointer(
display,
grab_input_window_,
False,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
GrabModeAsync,
GrabModeAsync,
None,
cursor.platform(),
CurrentTime);
if (ret != GrabSuccess) {
DLOG(ERROR) << "Grabbing pointer for dragging failed: "
<< ui::GetX11ErrorString(display, ret);
} else {
XUngrabKeyboard(display, CurrentTime);
ret = XGrabKeyboard(
display,
grab_input_window_,
False,
GrabModeAsync,
GrabModeAsync,
CurrentTime);
if (ret != GrabSuccess) {
DLOG(ERROR) << "Grabbing keyboard for dragging failed: "
<< ui::GetX11ErrorString(display, ret);
}
}
XUngrabServer(display);
return ret == GrabSuccess;
}
Window X11WholeScreenMoveLoop::CreateDragInputWindow(XDisplay* display) {
unsigned long attribute_mask = CWEventMask | CWOverrideRedirect;
XSetWindowAttributes swa;
memset(&swa, 0, sizeof(swa));
swa.event_mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
KeyPressMask | KeyReleaseMask | StructureNotifyMask;
swa.override_redirect = True;
Window window = XCreateWindow(display,
DefaultRootWindow(display),
-100, -100, 10, 10,
0, CopyFromParent, InputOnly, CopyFromParent,
attribute_mask, &swa);
XMapRaised(display, window);
base::MessagePumpX11::Current()->BlockUntilWindowMapped(window);
return window;
}
void X11WholeScreenMoveLoop::CreateDragImageWindow() {
Widget* widget = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_DRAG);
params.opacity = Widget::InitParams::OPAQUE_WINDOW;
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.accept_events = false;
gfx::Point location = gfx::ToFlooredPoint(
gfx::Screen::GetNativeScreen()->GetCursorScreenPoint() - drag_offset_);
params.bounds = gfx::Rect(location, drag_image_.size());
widget->set_focus_on_creation(false);
widget->set_frame_type(Widget::FRAME_TYPE_FORCE_NATIVE);
widget->Init(params);
widget->SetOpacity(kDragWidgetOpacity);
widget->GetNativeWindow()->SetName("DragWindow");
ImageView* image = new ImageView();
image->SetImage(drag_image_);
image->SetBounds(0, 0, drag_image_.width(), drag_image_.height());
widget->SetContentsView(image);
widget->Show();
widget->GetNativeWindow()->layer()->SetFillsBoundsOpaquely(false);
drag_widget_.reset(widget);
}
bool X11WholeScreenMoveLoop::CheckIfIconValid() {
const SkBitmap* in_bitmap = drag_image_.bitmap();
SkAutoLockPixels in_lock(*in_bitmap);
for (int y = 0; y < in_bitmap->height(); ++y) {
uint32* in_row = in_bitmap->getAddr32(0, y);
for (int x = 0; x < in_bitmap->width(); ++x) {
if (SkColorGetA(in_row[x]) > kMinAlpha)
return true;
}
}
return false;
}
}