Skip to content

Commit

Permalink
Add confine bounds code for Ozone X11.
Browse files Browse the repository at this point in the history
This CL implements ConfineCursorToBounds() for Ozone X11. This is only
active if --ash-constrain-pointer-to-root is passed in.

Bug: 771212
Change-Id: I605c704846a34d61a77a87518f1872355e821773
Reviewed-on: https://chromium-review.googlesource.com/731028
Reviewed-by: Sadrul Chowdhury <sadrul@chromium.org>
Commit-Queue: kylechar <kylechar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#510957}
  • Loading branch information
kylechar authored and Commit Bot committed Oct 23, 2017
1 parent a16998b commit 9e43f88
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
70 changes: 55 additions & 15 deletions ui/platform_window/x11/x11_window_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,14 @@ X11WindowBase::X11WindowBase(PlatformWindowDelegate* delegate,
xroot_window_(DefaultRootWindow(xdisplay_)),
bounds_(bounds) {
DCHECK(delegate_);
pointer_barriers_.fill(None);
}

X11WindowBase::~X11WindowBase() {
UnConfineCursor();
Destroy();
}

void X11WindowBase::Destroy() {
if (xwindow_ == None)
return;

// Stop processing events.
XID xwindow = xwindow_;
XDisplay* xdisplay = xdisplay_;
xwindow_ = None;
delegate_->OnClosed();
// |this| might be deleted because of the above call.

XDestroyWindow(xdisplay, xwindow);
}

void X11WindowBase::Create() {
DCHECK(!bounds_.size().IsEmpty());

Expand Down Expand Up @@ -243,12 +231,64 @@ void X11WindowBase::MoveCursorTo(const gfx::Point& location) {
bounds_.x() + location.x(), bounds_.y() + location.y());
}

void X11WindowBase::ConfineCursorToBounds(const gfx::Rect& bounds) {}
void X11WindowBase::ConfineCursorToBounds(const gfx::Rect& bounds) {
UnConfineCursor();

if (bounds.IsEmpty())
return;

gfx::Rect barrier(bounds);
barrier.Offset(bounds_.x(), bounds_.y());

// Top horizontal barrier.
pointer_barriers_[0] = XFixesCreatePointerBarrier(
xdisplay_, xroot_window_, barrier.x(), barrier.y(), barrier.right(),
barrier.y(), BarrierPositiveY, 0, XIAllDevices);
// Bottom horizontal barrier.
pointer_barriers_[1] = XFixesCreatePointerBarrier(
xdisplay_, xroot_window_, barrier.x(), barrier.bottom(), barrier.right(),
barrier.bottom(), BarrierNegativeY, 0, XIAllDevices);
// Left vertical barrier.
pointer_barriers_[2] = XFixesCreatePointerBarrier(
xdisplay_, xroot_window_, barrier.x(), barrier.y(), barrier.x(),
barrier.bottom(), BarrierPositiveX, 0, XIAllDevices);
// Right vertical barrier.
pointer_barriers_[3] = XFixesCreatePointerBarrier(
xdisplay_, xroot_window_, barrier.right(), barrier.y(), barrier.right(),
barrier.bottom(), BarrierNegativeX, 0, XIAllDevices);

has_pointer_barriers_ = true;
}

PlatformImeController* X11WindowBase::GetPlatformImeController() {
return nullptr;
}

void X11WindowBase::Destroy() {
if (xwindow_ == None)
return;

// Stop processing events.
XID xwindow = xwindow_;
XDisplay* xdisplay = xdisplay_;
xwindow_ = None;
delegate_->OnClosed();
// |this| might be deleted because of the above call.

XDestroyWindow(xdisplay, xwindow);
}

void X11WindowBase::UnConfineCursor() {
if (!has_pointer_barriers_)
return;

for (XID pointer_barrier : pointer_barriers_)
XFixesDestroyPointerBarrier(xdisplay_, pointer_barrier);
pointer_barriers_.fill(None);

has_pointer_barriers_ = false;
}

bool X11WindowBase::IsEventForXWindow(const XEvent& xev) const {
return xwindow_ != None && FindXEventTarget(xev) == xwindow_;
}
Expand Down
13 changes: 10 additions & 3 deletions ui/platform_window/x11/x11_window_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <stdint.h>

#include <array>

#include "base/callback.h"
#include "base/macros.h"
#include "ui/gfx/geometry/rect.h"
Expand Down Expand Up @@ -47,20 +49,21 @@ class X11_WINDOW_EXPORT X11WindowBase : public PlatformWindow {
PlatformImeController* GetPlatformImeController() override;

protected:
void Destroy();

PlatformWindowDelegate* delegate() { return delegate_; }
XDisplay* xdisplay() { return xdisplay_; }
XID xwindow() const { return xwindow_; }

void Destroy();
void UnConfineCursor();

// Checks if XEvent is for this XWindow.
bool IsEventForXWindow(const XEvent& xev) const;

// Processes events for this XWindow.
void ProcessXWindowEvent(XEvent* xev);

private:
PlatformWindowDelegate* delegate_;
PlatformWindowDelegate* const delegate_;

XDisplay* xdisplay_;
XID xwindow_;
Expand All @@ -72,6 +75,10 @@ class X11_WINDOW_EXPORT X11WindowBase : public PlatformWindow {
// The bounds of |xwindow_|.
gfx::Rect bounds_;

// Keep track of barriers to confine cursor.
bool has_pointer_barriers_ = false;
std::array<XID, 4> pointer_barriers_;

bool window_mapped_ = false;

DISALLOW_COPY_AND_ASSIGN(X11WindowBase);
Expand Down

0 comments on commit 9e43f88

Please sign in to comment.