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/ash_window_tree_host_init_params.h"
16 #include "ash/host/ash_window_tree_host_unified.h"
17 #include "ash/host/root_window_transformer.h"
18 #include "ash/ime/input_method_event_handler.h"
19 #include "base/basictypes.h"
20 #include "base/sys_info.h"
21 #include "ui/aura/env.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/base/x/x11_util.h"
25 #include "ui/events/devices/device_data_manager.h"
26 #include "ui/events/devices/x11/device_list_cache_x11.h"
27 #include "ui/events/devices/x11/touch_factory_x11.h"
28 #include "ui/events/event.h"
29 #include "ui/events/event_utils.h"
30 #include "ui/events/null_event_targeter.h"
31 #include "ui/events/platform/platform_event_source.h"
32 #include "ui/gfx/geometry/rect.h"
33 #include "ui/gfx/screen.h"
37 AshWindowTreeHostX11::AshWindowTreeHostX11(const gfx::Rect
& initial_bounds
)
38 : WindowTreeHostX11(initial_bounds
), transformer_helper_(this) {
39 transformer_helper_
.Init();
40 aura::Env::GetInstance()->AddObserver(this);
43 AshWindowTreeHostX11::~AshWindowTreeHostX11() {
44 aura::Env::GetInstance()->RemoveObserver(this);
48 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); }
50 bool AshWindowTreeHostX11::ConfineCursorToRootWindow() {
52 DCHECK(!pointer_barriers_
.get());
53 if (pointer_barriers_
)
55 pointer_barriers_
.reset(new XID
[4]);
56 gfx::Rect
barrier(bounds());
57 barrier
.Inset(transformer_helper_
.GetHostInsets());
58 // Horizontal, top barriers.
59 pointer_barriers_
[0] = XFixesCreatePointerBarrier(xdisplay(),
68 // Horizontal, bottom barriers.
69 pointer_barriers_
[1] = XFixesCreatePointerBarrier(xdisplay(),
78 // Vertical, left barriers.
79 pointer_barriers_
[2] = XFixesCreatePointerBarrier(xdisplay(),
88 // Vertical, right barriers.
89 pointer_barriers_
[3] = XFixesCreatePointerBarrier(xdisplay(),
102 void AshWindowTreeHostX11::UnConfineCursor() {
103 #if XFIXES_MAJOR >= 5
104 if (pointer_barriers_
) {
105 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[0]);
106 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[1]);
107 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[2]);
108 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_
[3]);
109 pointer_barriers_
.reset();
114 void AshWindowTreeHostX11::SetRootWindowTransformer(
115 scoped_ptr
<RootWindowTransformer
> transformer
) {
116 transformer_helper_
.SetRootWindowTransformer(transformer
.Pass());
117 if (pointer_barriers_
) {
119 ConfineCursorToRootWindow();
123 gfx::Insets
AshWindowTreeHostX11::GetHostInsets() const {
124 return transformer_helper_
.GetHostInsets();
127 aura::WindowTreeHost
* AshWindowTreeHostX11::AsWindowTreeHost() { return this; }
129 void AshWindowTreeHostX11::PrepareForShutdown() {
130 // Block the root window from dispatching events because it is weird for a
131 // ScreenPositionClient not to be attached to the root window and for
132 // ui::EventHandlers to be unable to convert the event's location to screen
134 window()->SetEventTargeter(
135 scoped_ptr
<ui::EventTargeter
>(new ui::NullEventTargeter
));
137 if (ui::PlatformEventSource::GetInstance()) {
138 // Block X events which are not turned into ui::Events from getting
139 // processed. (e.g. ConfigureNotify)
140 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
144 void AshWindowTreeHostX11::SetBounds(const gfx::Rect
& bounds
) {
145 WindowTreeHostX11::SetBounds(bounds
);
146 if (pointer_barriers_
) {
148 ConfineCursorToRootWindow();
152 gfx::Transform
AshWindowTreeHostX11::GetRootTransform() const {
153 return transformer_helper_
.GetTransform();
156 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform
& transform
) {
157 transformer_helper_
.SetTransform(transform
);
160 gfx::Transform
AshWindowTreeHostX11::GetInverseRootTransform() const {
161 return transformer_helper_
.GetInverseTransform();
164 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size
& host_size
) {
165 transformer_helper_
.UpdateWindowSize(host_size
);
168 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show
) {
169 #if defined(OS_CHROMEOS)
170 SetCrOSTapPaused(!show
);
174 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window
* window
) {}
176 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost
* host
) {
177 if (host
!= AsWindowTreeHost())
180 #if defined(OS_CHROMEOS)
181 // We have to enable Tap-to-click by default because the cursor is set to
182 // visible in Shell::InitRootWindowController.
183 SetCrOSTapPaused(false);
187 void AshWindowTreeHostX11::OnConfigureNotify() {
188 // Always update barrier and mouse location because |bounds_| might
189 // have already been updated in |SetBounds|.
190 if (pointer_barriers_
) {
192 ConfineCursorToRootWindow();
196 bool AshWindowTreeHostX11::CanDispatchEvent(const ui::PlatformEvent
& event
) {
197 if(!WindowTreeHostX11::CanDispatchEvent(event
))
200 ui::EventType type
= ui::EventTypeFromNative(xev
);
201 // For touch event, check if the root window is residing on the according
204 case ui::ET_TOUCH_MOVED
:
205 case ui::ET_TOUCH_PRESSED
:
206 case ui::ET_TOUCH_CANCELLED
:
207 case ui::ET_TOUCH_RELEASED
: {
208 #if defined(OS_CHROMEOS)
209 XIDeviceEvent
* xiev
= static_cast<XIDeviceEvent
*>(xev
->xcookie
.data
);
210 int64 touch_display_id
=
211 ui::DeviceDataManager::GetInstance()->GetTargetDisplayForTouchDevice(
213 // If we don't have record of display id for this touch device, check
214 // that if the event is within the bound of the root window. Note
215 // that in multi-monitor case, the event position is in framebuffer
216 // space so the bounds check will not work so well.
217 if (touch_display_id
== gfx::Display::kInvalidDisplayID
) {
218 if (base::SysInfo::IsRunningOnChromeOS() &&
219 !bounds().Contains(ui::EventLocationFromNative(xev
)))
222 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(window());
223 gfx::Display display
= screen
->GetDisplayNearestWindow(window());
224 return touch_display_id
== display
.id();
226 #endif // defined(OS_CHROMEOS)
233 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent(
234 ui::LocatedEvent
* event
) {
235 TranslateLocatedEvent(event
);
236 SendEventToProcessor(event
);
239 ui::EventDispatchDetails
AshWindowTreeHostX11::DispatchKeyEventPostIME(
240 ui::KeyEvent
* event
) {
241 input_method_handler()->SetPostIME(true);
242 ui::EventDispatchDetails details
=
243 event_processor()->OnEventFromSource(event
);
244 if (!details
.dispatcher_destroyed
)
245 input_method_handler()->SetPostIME(false);
249 #if defined(OS_CHROMEOS)
250 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state
) {
251 if (!ui::IsXInput2Available())
253 // Temporarily pause tap-to-click when the cursor is hidden.
254 Atom prop
= atom_cache()->GetAtom("Tap Paused");
255 unsigned char value
= state
;
256 const XIDeviceList
& dev_list
=
257 ui::DeviceListCacheX11::GetInstance()->GetXI2DeviceList(xdisplay());
259 // Only slave pointer devices could possibly have tap-paused property.
260 for (int i
= 0; i
< dev_list
.count
; i
++) {
261 if (dev_list
[i
].use
== XISlavePointer
) {
264 unsigned long old_nvalues
, bytes
;
266 int result
= XIGetProperty(xdisplay(),
267 dev_list
[i
].deviceid
,
278 if (result
!= Success
)
281 XIChangeProperty(xdisplay(),
282 dev_list
[i
].deviceid
,
294 AshWindowTreeHost
* AshWindowTreeHost::Create(
295 const AshWindowTreeHostInitParams
& init_params
) {
296 if (init_params
.offscreen
)
297 return new AshWindowTreeHostUnified(init_params
.initial_bounds
);
298 return new AshWindowTreeHostX11(init_params
.initial_bounds
);