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/root_window_transformer.h"
17 #include "base/basictypes.h"
18 #include "base/sys_info.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/window.h"
21 #include "ui/aura/window_event_dispatcher.h"
22 #include "ui/base/x/x11_util.h"
23 #include "ui/events/devices/device_data_manager.h"
24 #include "ui/events/devices/x11/device_list_cache_x11.h"
25 #include "ui/events/devices/x11/touch_factory_x11.h"
26 #include "ui/events/event.h"
27 #include "ui/events/event_utils.h"
28 #include "ui/events/null_event_targeter.h"
29 #include "ui/events/platform/platform_event_source.h"
30 #include "ui/gfx/geometry/rect.h"
31 #include "ui/gfx/screen.h"
35 AshWindowTreeHostX11::AshWindowTreeHostX11(const gfx::Rect
& initial_bounds
)
36 : WindowTreeHostX11(initial_bounds
),
37 transformer_helper_(this),
38 display_ids_(std::make_pair(gfx::Display::kInvalidDisplayID
,
39 gfx::Display::kInvalidDisplayID
)) {
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::UpdateDisplayID(int64 id1
, int64 id2
) {
130 display_ids_
.first
= id1
;
131 display_ids_
.second
= id2
;
134 void AshWindowTreeHostX11::PrepareForShutdown() {
135 // Block the root window from dispatching events because it is weird for a
136 // ScreenPositionClient not to be attached to the root window and for
137 // ui::EventHandlers to be unable to convert the event's location to screen
139 window()->SetEventTargeter(
140 scoped_ptr
<ui::EventTargeter
>(new ui::NullEventTargeter
));
142 if (ui::PlatformEventSource::GetInstance()) {
143 // Block X events which are not turned into ui::Events from getting
144 // processed. (e.g. ConfigureNotify)
145 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
149 void AshWindowTreeHostX11::SetBounds(const gfx::Rect
& bounds
) {
150 WindowTreeHostX11::SetBounds(bounds
);
151 if (pointer_barriers_
) {
153 ConfineCursorToRootWindow();
157 gfx::Transform
AshWindowTreeHostX11::GetRootTransform() const {
158 return transformer_helper_
.GetTransform();
161 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform
& transform
) {
162 transformer_helper_
.SetTransform(transform
);
165 gfx::Transform
AshWindowTreeHostX11::GetInverseRootTransform() const {
166 return transformer_helper_
.GetInverseTransform();
169 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size
& host_size
) {
170 transformer_helper_
.UpdateWindowSize(host_size
);
173 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show
) {
174 #if defined(OS_CHROMEOS)
175 SetCrOSTapPaused(!show
);
179 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window
* window
) {}
181 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost
* host
) {
182 if (host
!= AsWindowTreeHost())
185 #if defined(OS_CHROMEOS)
186 // We have to enable Tap-to-click by default because the cursor is set to
187 // visible in Shell::InitRootWindowController.
188 SetCrOSTapPaused(false);
192 void AshWindowTreeHostX11::OnConfigureNotify() {
193 // Always update barrier and mouse location because |bounds_| might
194 // have already been updated in |SetBounds|.
195 if (pointer_barriers_
) {
197 ConfineCursorToRootWindow();
201 bool AshWindowTreeHostX11::CanDispatchEvent(const ui::PlatformEvent
& event
) {
202 if(!WindowTreeHostX11::CanDispatchEvent(event
))
205 ui::EventType type
= ui::EventTypeFromNative(xev
);
206 // For touch event, check if the root window is residing on the according
209 case ui::ET_TOUCH_MOVED
:
210 case ui::ET_TOUCH_PRESSED
:
211 case ui::ET_TOUCH_CANCELLED
:
212 case ui::ET_TOUCH_RELEASED
: {
213 #if defined(OS_CHROMEOS)
214 XIDeviceEvent
* xiev
= static_cast<XIDeviceEvent
*>(xev
->xcookie
.data
);
215 int64 touch_display_id
=
216 ui::DeviceDataManager::GetInstance()->GetDisplayForTouchDevice(
218 // If we don't have record of display id for this touch device, check
219 // that if the event is within the bound of the root window. Note
220 // that in multi-monitor case, the event position is in framebuffer
221 // space so the bounds check will not work so well.
222 if (touch_display_id
== gfx::Display::kInvalidDisplayID
) {
223 if (base::SysInfo::IsRunningOnChromeOS() &&
224 !bounds().Contains(ui::EventLocationFromNative(xev
)))
226 } else if (touch_display_id
!= display_ids_
.first
&&
227 touch_display_id
!= display_ids_
.second
) {
230 #endif // defined(OS_CHROMEOS)
237 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent(
238 ui::LocatedEvent
* event
) {
239 TranslateLocatedEvent(event
);
240 SendEventToProcessor(event
);
243 #if defined(OS_CHROMEOS)
244 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state
) {
245 if (!ui::IsXInput2Available())
247 // Temporarily pause tap-to-click when the cursor is hidden.
248 Atom prop
= atom_cache()->GetAtom("Tap Paused");
249 unsigned char value
= state
;
250 XIDeviceList dev_list
=
251 ui::DeviceListCacheX11::GetInstance()->GetXI2DeviceList(xdisplay());
253 // Only slave pointer devices could possibly have tap-paused property.
254 for (int i
= 0; i
< dev_list
.count
; i
++) {
255 if (dev_list
[i
].use
== XISlavePointer
) {
258 unsigned long old_nvalues
, bytes
;
260 int result
= XIGetProperty(xdisplay(),
261 dev_list
[i
].deviceid
,
272 if (result
!= Success
)
275 XIChangeProperty(xdisplay(),
276 dev_list
[i
].deviceid
,
288 AshWindowTreeHost
* AshWindowTreeHost::Create(
289 const AshWindowTreeHostInitParams
& init_params
) {
290 return new AshWindowTreeHostX11(init_params
.initial_bounds
);