1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/host/ash_window_tree_host_x11.h"
7 #include <X11/extensions/Xfixes.h>
8 #include <X11/extensions/XInput2.h>
15 #include "ash/host/root_window_transformer.h"
16 #include "base/basictypes.h"
17 #include "base/command_line.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/sys_info.h"
21 #include "ui/aura/client/screen_position_client.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/window.h"
24 #include "ui/base/x/x11_util.h"
25 #include "ui/events/event.h"
26 #include "ui/events/event_switches.h"
27 #include "ui/events/event_utils.h"
28 #include "ui/events/platform/platform_event_observer.h"
29 #include "ui/events/platform/x11/x11_event_source.h"
30 #include "ui/events/x/device_data_manager.h"
31 #include "ui/events/x/device_list_cache_x.h"
32 #include "ui/events/x/touch_factory_x11.h"
34 #include "ui/gfx/rect.h"
35 #include "ui/gfx/screen.h"
39 // Accomplishes 2 tasks concerning touch event calibration:
40 // 1. Being a message-pump observer,
41 // routes all the touch events to the X root window,
42 // where they can be calibrated later.
43 // 2. Has the Calibrate method that does the actual bezel calibration,
44 // when invoked from X root window's event dispatcher.
45 class AshWindowTreeHostX11::TouchEventCalibrate
46 : public ui::PlatformEventObserver
{
48 TouchEventCalibrate() : left_(0), right_(0), top_(0), bottom_(0) {
49 if (ui::PlatformEventSource::GetInstance())
50 ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
51 #if defined(USE_XI2_MT)
52 std::vector
<std::string
> parts
;
53 if (Tokenize(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
54 switches::kTouchCalibration
),
57 if (!base::StringToInt(parts
[0], &left_
))
58 DLOG(ERROR
) << "Incorrect left border calibration value passed.";
59 if (!base::StringToInt(parts
[1], &right_
))
60 DLOG(ERROR
) << "Incorrect right border calibration value passed.";
61 if (!base::StringToInt(parts
[2], &top_
))
62 DLOG(ERROR
) << "Incorrect top border calibration value passed.";
63 if (!base::StringToInt(parts
[3], &bottom_
))
64 DLOG(ERROR
) << "Incorrect bottom border calibration value passed.";
66 #endif // defined(USE_XI2_MT)
69 virtual ~TouchEventCalibrate() {
70 if (ui::PlatformEventSource::GetInstance())
71 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
74 // Modify the location of the |event|,
75 // expanding it from |bounds| to (|bounds| + bezels).
76 // Required when touchscreen is bigger than screen (i.e. has bezels),
77 // because we receive events in touchscreen coordinates,
78 // which need to be expanded when converting to screen coordinates,
79 // so that location on bezels will be outside of screen area.
80 void Calibrate(ui::TouchEvent
* event
, const gfx::Rect
& bounds
) {
81 #if defined(USE_XI2_MT)
85 if (!left_
&& !right_
&& !top_
&& !bottom_
)
88 const int resolution_x
= bounds
.width();
89 const int resolution_y
= bounds
.height();
90 // The "grace area" (10% in this case) is to make it easier for the user to
91 // navigate to the corner.
92 const double kGraceAreaFraction
= 0.1;
93 if (left_
|| right_
) {
94 // Offset the x position to the real
96 // Check if we are in the grace area of the left side.
97 // Note: We might not want to do this when the gesture is locked?
98 if (x
< 0 && x
> -left_
* kGraceAreaFraction
)
100 // Check if we are in the grace area of the right side.
101 // Note: We might not want to do this when the gesture is locked?
102 if (x
> resolution_x
- left_
&&
103 x
< resolution_x
- left_
+ right_
* kGraceAreaFraction
)
104 x
= resolution_x
- left_
;
105 // Scale the screen area back to the full resolution of the screen.
106 x
= (x
* resolution_x
) / (resolution_x
- (right_
+ left_
));
108 if (top_
|| bottom_
) {
109 // When there is a top bezel we add our border,
112 // Check if we are in the grace area of the top side.
113 // Note: We might not want to do this when the gesture is locked?
114 if (y
< 0 && y
> -top_
* kGraceAreaFraction
)
117 // Check if we are in the grace area of the bottom side.
118 // Note: We might not want to do this when the gesture is locked?
119 if (y
> resolution_y
- top_
&&
120 y
< resolution_y
- top_
+ bottom_
* kGraceAreaFraction
)
121 y
= resolution_y
- top_
;
122 // Scale the screen area back to the full resolution of the screen.
123 y
= (y
* resolution_y
) / (resolution_y
- (bottom_
+ top_
));
126 // Set the modified coordinate back to the event.
127 if (event
->root_location() == event
->location()) {
128 // Usually those will be equal,
129 // if not, I am not sure what the correct value should be.
130 event
->set_root_location(gfx::Point(x
, y
));
132 event
->set_location(gfx::Point(x
, y
));
133 #endif // defined(USE_XI2_MT)
137 // ui::PlatformEventObserver:
138 virtual void WillProcessEvent(const ui::PlatformEvent
& event
) OVERRIDE
{
139 #if defined(USE_XI2_MT)
140 if (event
->type
== GenericEvent
&&
141 (event
->xgeneric
.evtype
== XI_TouchBegin
||
142 event
->xgeneric
.evtype
== XI_TouchUpdate
||
143 event
->xgeneric
.evtype
== XI_TouchEnd
)) {
144 XIDeviceEvent
* xievent
= static_cast<XIDeviceEvent
*>(event
->xcookie
.data
);
145 xievent
->event
= xievent
->root
;
146 xievent
->event_x
= xievent
->root_x
;
147 xievent
->event_y
= xievent
->root_y
;
149 #endif // defined(USE_XI2_MT)
152 virtual void DidProcessEvent(const ui::PlatformEvent
& event
) OVERRIDE
{}
154 // The difference in screen's native resolution pixels between
155 // the border of the touchscreen and the border of the screen,
162 DISALLOW_COPY_AND_ASSIGN(TouchEventCalibrate
);
165 ////////////////////////////////////////////////////////////////////////////////
166 // AshWindowTreeHostX11
168 AshWindowTreeHostX11::AshWindowTreeHostX11(const gfx::Rect
& initial_bounds
)
169 : WindowTreeHostX11(initial_bounds
),
170 is_internal_display_(false),
171 touch_calibrate_(new TouchEventCalibrate
),
172 transformer_helper_(this) {
173 aura::Env::GetInstance()->AddObserver(this);
176 AshWindowTreeHostX11::~AshWindowTreeHostX11() {
177 aura::Env::GetInstance()->RemoveObserver(this);
181 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); }
183 bool AshWindowTreeHostX11::ConfineCursorToRootWindow() {
184 #if XFIXES_MAJOR >= 5
185 DCHECK(!pointer_barriers_
.get());
186 if (pointer_barriers_
)
188 pointer_barriers_
.reset(new XID
[4]);
189 gfx::Rect
barrier(bounds());
190 barrier
.Inset(transformer_helper_
.GetHostInsets());
191 // Horizontal, top barriers.
192 pointer_barriers_
[0] = XFixesCreatePointerBarrier(xdisplay(),
201 // Horizontal, bottom barriers.
202 pointer_barriers_
[1] = XFixesCreatePointerBarrier(xdisplay(),
211 // Vertical, left barriers.
212 pointer_barriers_
[2] = XFixesCreatePointerBarrier(xdisplay(),
221 // Vertical, right barriers.
222 pointer_barriers_
[3] = XFixesCreatePointerBarrier(xdisplay(),
235 void AshWindowTreeHostX11::UnConfineCursor() {
236 #if XFIXES_MAJOR >= 5
237 if (pointer_barriers_
) {
238 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[0]);
239 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[1]);
240 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[2]);
241 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[3]);
242 pointer_barriers_
.reset();
247 void AshWindowTreeHostX11::SetRootWindowTransformer(
248 scoped_ptr
<RootWindowTransformer
> transformer
) {
249 transformer_helper_
.SetRootWindowTransformer(transformer
.Pass());
250 UpdateIsInternalDisplay();
251 if (pointer_barriers_
) {
253 ConfineCursorToRootWindow();
257 aura::WindowTreeHost
* AshWindowTreeHostX11::AsWindowTreeHost() { return this; }
259 void AshWindowTreeHostX11::SetBounds(const gfx::Rect
& bounds
) {
260 WindowTreeHostX11::SetBounds(bounds
);
261 UpdateIsInternalDisplay();
262 if (pointer_barriers_
) {
264 ConfineCursorToRootWindow();
268 gfx::Transform
AshWindowTreeHostX11::GetRootTransform() const {
269 return transformer_helper_
.GetTransform();
272 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform
& transform
) {
273 transformer_helper_
.SetTransform(transform
);
276 gfx::Transform
AshWindowTreeHostX11::GetInverseRootTransform() const {
277 return transformer_helper_
.GetInverseTransform();
280 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size
& host_size
) {
281 transformer_helper_
.UpdateWindowSize(host_size
);
284 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show
) {
285 SetCrOSTapPaused(!show
);
288 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window
* window
) {}
290 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost
* host
) {
291 // UpdateIsInternalDisplay relies on RootWindowSettings' display_id being set
292 // available by the time WED::Init is called. (set in
293 // DisplayManager::CreateRootWindowForDisplay)
294 // Ready when NotifyHostInitialized is called from WED::Init.
295 if (host
!= AsWindowTreeHost())
297 UpdateIsInternalDisplay();
299 // We have to enable Tap-to-click by default because the cursor is set to
300 // visible in Shell::InitRootWindowController.
301 SetCrOSTapPaused(false);
304 void AshWindowTreeHostX11::OnConfigureNotify() {
305 UpdateIsInternalDisplay();
307 // Always update barrier and mouse location because |bounds_| might
308 // have already been updated in |SetBounds|.
309 if (pointer_barriers_
) {
311 ConfineCursorToRootWindow();
315 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent(
316 ui::LocatedEvent
* event
) {
317 switch (event
->type()) {
318 case ui::ET_TOUCH_MOVED
:
319 case ui::ET_TOUCH_PRESSED
:
320 case ui::ET_TOUCH_CANCELLED
:
321 case ui::ET_TOUCH_RELEASED
: {
322 ui::TouchEvent
* touchev
= static_cast<ui::TouchEvent
*>(event
);
323 if (base::SysInfo::IsRunningOnChromeOS()) {
324 // X maps the touch-surface to the size of the X root-window.
325 // In multi-monitor setup, Coordinate Transformation Matrix
326 // repositions the touch-surface onto part of X root-window
327 // containing aura root-window corresponding to the touchscreen.
328 // However, if aura root-window has non-zero origin,
329 // we need to relocate the event into aura root-window coordinates.
330 touchev
->Relocate(bounds().origin());
331 #if defined(USE_XI2_MT)
332 if (is_internal_display_
)
333 touch_calibrate_
->Calibrate(touchev
, bounds());
334 #endif // defined(USE_XI2_MT)
339 aura::Window
* root_window
= window();
340 aura::client::ScreenPositionClient
* screen_position_client
=
341 aura::client::GetScreenPositionClient(root_window
);
342 gfx::Rect
local(bounds().size());
344 if (screen_position_client
&& !local
.Contains(event
->location())) {
345 gfx::Point
location(event
->location());
346 // In order to get the correct point in screen coordinates
347 // during passive grab, we first need to find on which host window
348 // the mouse is on, and find out the screen coordinates on that
349 // host window, then convert it back to this host window's coordinate.
350 screen_position_client
->ConvertHostPointToScreen(root_window
,
352 screen_position_client
->ConvertPointFromScreen(root_window
, &location
);
353 ConvertPointToHost(&location
);
354 event
->set_location(location
);
355 event
->set_root_location(location
);
360 SendEventToProcessor(event
);
363 void AshWindowTreeHostX11::UpdateIsInternalDisplay() {
364 aura::Window
* root_window
= window();
365 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(root_window
);
366 gfx::Display display
= screen
->GetDisplayNearestWindow(root_window
);
367 DCHECK(display
.is_valid());
368 is_internal_display_
= display
.IsInternal();
371 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state
) {
372 if (!ui::IsXInput2Available())
374 // Temporarily pause tap-to-click when the cursor is hidden.
375 Atom prop
= atom_cache()->GetAtom("Tap Paused");
376 unsigned char value
= state
;
377 XIDeviceList dev_list
=
378 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay());
380 // Only slave pointer devices could possibly have tap-paused property.
381 for (int i
= 0; i
< dev_list
.count
; i
++) {
382 if (dev_list
[i
].use
== XISlavePointer
) {
385 unsigned long old_nvalues
, bytes
;
387 int result
= XIGetProperty(xdisplay(),
388 dev_list
[i
].deviceid
,
399 if (result
!= Success
)
402 XIChangeProperty(xdisplay(),
403 dev_list
[i
].deviceid
,
414 AshWindowTreeHost
* AshWindowTreeHost::Create(const gfx::Rect
& initial_bounds
) {
415 return new AshWindowTreeHostX11(initial_bounds
);