ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ash / host / ash_window_tree_host_x11.cc
blobfdd8cacd6389db1d586495d984de273ef340149a
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>
9 #include <X11/Xatom.h>
10 #include <X11/Xlib.h>
12 #include <string>
13 #include <vector>
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"
33 namespace ash {
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);
45 UnConfineCursor();
48 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); }
50 bool AshWindowTreeHostX11::ConfineCursorToRootWindow() {
51 #if XFIXES_MAJOR >= 5
52 DCHECK(!pointer_barriers_.get());
53 if (pointer_barriers_)
54 return false;
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(),
60 x_root_window(),
61 barrier.x(),
62 barrier.y(),
63 barrier.right(),
64 barrier.y(),
65 BarrierPositiveY,
67 XIAllDevices);
68 // Horizontal, bottom barriers.
69 pointer_barriers_[1] = XFixesCreatePointerBarrier(xdisplay(),
70 x_root_window(),
71 barrier.x(),
72 barrier.bottom(),
73 barrier.right(),
74 barrier.bottom(),
75 BarrierNegativeY,
77 XIAllDevices);
78 // Vertical, left barriers.
79 pointer_barriers_[2] = XFixesCreatePointerBarrier(xdisplay(),
80 x_root_window(),
81 barrier.x(),
82 barrier.y(),
83 barrier.x(),
84 barrier.bottom(),
85 BarrierPositiveX,
87 XIAllDevices);
88 // Vertical, right barriers.
89 pointer_barriers_[3] = XFixesCreatePointerBarrier(xdisplay(),
90 x_root_window(),
91 barrier.right(),
92 barrier.y(),
93 barrier.right(),
94 barrier.bottom(),
95 BarrierNegativeX,
97 XIAllDevices);
98 #endif
99 return true;
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();
111 #endif
114 void AshWindowTreeHostX11::SetRootWindowTransformer(
115 scoped_ptr<RootWindowTransformer> transformer) {
116 transformer_helper_.SetRootWindowTransformer(transformer.Pass());
117 if (pointer_barriers_) {
118 UnConfineCursor();
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
138 // coordinates.
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_) {
152 UnConfineCursor();
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);
176 #endif
179 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {}
181 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost* host) {
182 if (host != AsWindowTreeHost())
183 return;
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);
189 #endif
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_) {
196 UnConfineCursor();
197 ConfineCursorToRootWindow();
201 bool AshWindowTreeHostX11::CanDispatchEvent(const ui::PlatformEvent& event) {
202 if(!WindowTreeHostX11::CanDispatchEvent(event))
203 return false;
204 XEvent* xev = event;
205 ui::EventType type = ui::EventTypeFromNative(xev);
206 // For touch event, check if the root window is residing on the according
207 // touch display.
208 switch (type) {
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(
217 xiev->deviceid);
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)))
225 return false;
226 } else if (touch_display_id != display_ids_.first &&
227 touch_display_id != display_ids_.second) {
228 return false;
230 #endif // defined(OS_CHROMEOS)
231 return true;
233 default:
234 return true;
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())
246 return;
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) {
256 Atom old_type;
257 int old_format;
258 unsigned long old_nvalues, bytes;
259 unsigned char* data;
260 int result = XIGetProperty(xdisplay(),
261 dev_list[i].deviceid,
262 prop,
265 False,
266 AnyPropertyType,
267 &old_type,
268 &old_format,
269 &old_nvalues,
270 &bytes,
271 &data);
272 if (result != Success)
273 continue;
274 XFree(data);
275 XIChangeProperty(xdisplay(),
276 dev_list[i].deviceid,
277 prop,
278 XA_INTEGER,
280 PropModeReplace,
281 &value,
286 #endif
288 AshWindowTreeHost* AshWindowTreeHost::Create(
289 const AshWindowTreeHostInitParams& init_params) {
290 return new AshWindowTreeHostX11(init_params.initial_bounds);
293 } // namespace ash