OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/host/ash_window_tree_host_x11.h" | 5 #include "ash/host/ash_window_tree_host_x11.h" |
6 | 6 |
7 #include <X11/extensions/Xfixes.h> | 7 #include <X11/extensions/Xfixes.h> |
8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
9 #include <X11/Xatom.h> | 9 #include <X11/Xatom.h> |
10 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "ash/host/root_window_transformer.h" | 15 #include "ash/host/root_window_transformer.h" |
16 #include "base/basictypes.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" | 17 #include "base/sys_info.h" |
21 #include "ui/aura/client/screen_position_client.h" | 18 #include "ui/aura/client/screen_position_client.h" |
22 #include "ui/aura/env.h" | 19 #include "ui/aura/env.h" |
23 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
24 #include "ui/base/x/x11_util.h" | 21 #include "ui/base/x/x11_util.h" |
25 #include "ui/events/event.h" | 22 #include "ui/events/event.h" |
26 #include "ui/events/event_switches.h" | |
27 #include "ui/events/event_utils.h" | 23 #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" | 24 #include "ui/events/x/device_data_manager.h" |
31 #include "ui/events/x/device_list_cache_x.h" | 25 #include "ui/events/x/device_list_cache_x.h" |
32 #include "ui/events/x/touch_factory_x11.h" | 26 #include "ui/events/x/touch_factory_x11.h" |
33 | |
34 #include "ui/gfx/rect.h" | 27 #include "ui/gfx/rect.h" |
35 #include "ui/gfx/screen.h" | 28 #include "ui/gfx/screen.h" |
36 | 29 |
37 namespace ash { | 30 namespace ash { |
38 | 31 |
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 { | |
47 public: | |
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), | |
55 ",", | |
56 &parts) >= 4) { | |
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."; | |
65 } | |
66 #endif // defined(USE_XI2_MT) | |
67 } | |
68 | |
69 virtual ~TouchEventCalibrate() { | |
70 if (ui::PlatformEventSource::GetInstance()) | |
71 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this); | |
72 } | |
73 | |
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) | |
82 int x = event->x(); | |
83 int y = event->y(); | |
84 | |
85 if (!left_ && !right_ && !top_ && !bottom_) | |
86 return; | |
87 | |
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 | |
95 x -= left_; | |
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) | |
99 x = 0; | |
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_)); | |
107 } | |
108 if (top_ || bottom_) { | |
109 // When there is a top bezel we add our border, | |
110 y -= top_; | |
111 | |
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) | |
115 y = 0; | |
116 | |
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_)); | |
124 } | |
125 | |
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)); | |
131 } | |
132 event->set_location(gfx::Point(x, y)); | |
133 #endif // defined(USE_XI2_MT) | |
134 } | |
135 | |
136 private: | |
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; | |
148 } | |
149 #endif // defined(USE_XI2_MT) | |
150 } | |
151 | |
152 virtual void DidProcessEvent(const ui::PlatformEvent& event) OVERRIDE {} | |
153 | |
154 // The difference in screen's native resolution pixels between | |
155 // the border of the touchscreen and the border of the screen, | |
156 // aka bezel sizes. | |
157 int left_; | |
158 int right_; | |
159 int top_; | |
160 int bottom_; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(TouchEventCalibrate); | |
163 }; | |
164 | |
165 //////////////////////////////////////////////////////////////////////////////// | |
166 // AshWindowTreeHostX11 | |
167 | |
168 AshWindowTreeHostX11::AshWindowTreeHostX11(const gfx::Rect& initial_bounds) | 32 AshWindowTreeHostX11::AshWindowTreeHostX11(const gfx::Rect& initial_bounds) |
169 : WindowTreeHostX11(initial_bounds), | 33 : WindowTreeHostX11(initial_bounds), |
170 is_internal_display_(false), | 34 transformer_helper_(this), |
171 touch_calibrate_(new TouchEventCalibrate), | 35 display_ids_(std::make_pair(gfx::Display::kInvalidDisplayID, |
172 transformer_helper_(this) { | 36 gfx::Display::kInvalidDisplayID)) { |
173 aura::Env::GetInstance()->AddObserver(this); | 37 aura::Env::GetInstance()->AddObserver(this); |
174 } | 38 } |
175 | 39 |
176 AshWindowTreeHostX11::~AshWindowTreeHostX11() { | 40 AshWindowTreeHostX11::~AshWindowTreeHostX11() { |
177 aura::Env::GetInstance()->RemoveObserver(this); | 41 aura::Env::GetInstance()->RemoveObserver(this); |
178 UnConfineCursor(); | 42 UnConfineCursor(); |
179 } | 43 } |
180 | 44 |
181 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); } | 45 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); } |
182 | 46 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[2]); | 104 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[2]); |
241 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[3]); | 105 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[3]); |
242 pointer_barriers_.reset(); | 106 pointer_barriers_.reset(); |
243 } | 107 } |
244 #endif | 108 #endif |
245 } | 109 } |
246 | 110 |
247 void AshWindowTreeHostX11::SetRootWindowTransformer( | 111 void AshWindowTreeHostX11::SetRootWindowTransformer( |
248 scoped_ptr<RootWindowTransformer> transformer) { | 112 scoped_ptr<RootWindowTransformer> transformer) { |
249 transformer_helper_.SetRootWindowTransformer(transformer.Pass()); | 113 transformer_helper_.SetRootWindowTransformer(transformer.Pass()); |
250 UpdateIsInternalDisplay(); | |
251 if (pointer_barriers_) { | 114 if (pointer_barriers_) { |
252 UnConfineCursor(); | 115 UnConfineCursor(); |
253 ConfineCursorToRootWindow(); | 116 ConfineCursorToRootWindow(); |
254 } | 117 } |
255 } | 118 } |
256 | 119 |
257 gfx::Insets AshWindowTreeHostX11::GetHostInsets() const { | 120 gfx::Insets AshWindowTreeHostX11::GetHostInsets() const { |
258 return transformer_helper_.GetHostInsets(); | 121 return transformer_helper_.GetHostInsets(); |
259 } | 122 } |
260 | 123 |
261 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; } | 124 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; } |
262 | 125 |
| 126 void AshWindowTreeHostX11::UpdateDisplayID(int64 id1, int64 id2) { |
| 127 display_ids_.first = id1; |
| 128 display_ids_.second = id2; |
| 129 } |
| 130 |
263 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) { | 131 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) { |
264 WindowTreeHostX11::SetBounds(bounds); | 132 WindowTreeHostX11::SetBounds(bounds); |
265 UpdateIsInternalDisplay(); | |
266 if (pointer_barriers_) { | 133 if (pointer_barriers_) { |
267 UnConfineCursor(); | 134 UnConfineCursor(); |
268 ConfineCursorToRootWindow(); | 135 ConfineCursorToRootWindow(); |
269 } | 136 } |
270 } | 137 } |
271 | 138 |
272 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const { | 139 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const { |
273 return transformer_helper_.GetTransform(); | 140 return transformer_helper_.GetTransform(); |
274 } | 141 } |
275 | 142 |
276 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) { | 143 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) { |
277 transformer_helper_.SetTransform(transform); | 144 transformer_helper_.SetTransform(transform); |
278 } | 145 } |
279 | 146 |
280 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const { | 147 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const { |
281 return transformer_helper_.GetInverseTransform(); | 148 return transformer_helper_.GetInverseTransform(); |
282 } | 149 } |
283 | 150 |
284 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) { | 151 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) { |
285 transformer_helper_.UpdateWindowSize(host_size); | 152 transformer_helper_.UpdateWindowSize(host_size); |
286 } | 153 } |
287 | 154 |
288 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) { | 155 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) { |
289 SetCrOSTapPaused(!show); | 156 SetCrOSTapPaused(!show); |
290 } | 157 } |
291 | 158 |
292 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {} | 159 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {} |
293 | 160 |
294 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost* host) { | 161 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost* host) { |
295 // UpdateIsInternalDisplay relies on RootWindowSettings' display_id being set | |
296 // available by the time WED::Init is called. (set in | |
297 // DisplayManager::CreateRootWindowForDisplay) | |
298 // Ready when NotifyHostInitialized is called from WED::Init. | |
299 if (host != AsWindowTreeHost()) | 162 if (host != AsWindowTreeHost()) |
300 return; | 163 return; |
301 UpdateIsInternalDisplay(); | |
302 | 164 |
303 // We have to enable Tap-to-click by default because the cursor is set to | 165 // We have to enable Tap-to-click by default because the cursor is set to |
304 // visible in Shell::InitRootWindowController. | 166 // visible in Shell::InitRootWindowController. |
305 SetCrOSTapPaused(false); | 167 SetCrOSTapPaused(false); |
306 } | 168 } |
307 | 169 |
308 void AshWindowTreeHostX11::OnConfigureNotify() { | 170 void AshWindowTreeHostX11::OnConfigureNotify() { |
309 UpdateIsInternalDisplay(); | |
310 | |
311 // Always update barrier and mouse location because |bounds_| might | 171 // Always update barrier and mouse location because |bounds_| might |
312 // have already been updated in |SetBounds|. | 172 // have already been updated in |SetBounds|. |
313 if (pointer_barriers_) { | 173 if (pointer_barriers_) { |
314 UnConfineCursor(); | 174 UnConfineCursor(); |
315 ConfineCursorToRootWindow(); | 175 ConfineCursorToRootWindow(); |
316 } | 176 } |
317 } | 177 } |
318 | 178 |
319 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent( | 179 bool AshWindowTreeHostX11::CanDispatchEvent(const ui::PlatformEvent& event) { |
320 ui::LocatedEvent* event) { | 180 if(!WindowTreeHostX11::CanDispatchEvent(event)) |
321 switch (event->type()) { | 181 return false; |
| 182 XEvent* xev = event; |
| 183 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev->xcookie.data); |
| 184 ui::EventType type = ui::EventTypeFromNative(xev); |
| 185 // For touch event, check if the root window is residing on the according |
| 186 // touch display. |
| 187 switch (type) { |
322 case ui::ET_TOUCH_MOVED: | 188 case ui::ET_TOUCH_MOVED: |
323 case ui::ET_TOUCH_PRESSED: | 189 case ui::ET_TOUCH_PRESSED: |
324 case ui::ET_TOUCH_CANCELLED: | 190 case ui::ET_TOUCH_CANCELLED: |
325 case ui::ET_TOUCH_RELEASED: { | 191 case ui::ET_TOUCH_RELEASED: { |
326 ui::TouchEvent* touchev = static_cast<ui::TouchEvent*>(event); | 192 #if defined(OS_CHROMEOS) |
327 if (base::SysInfo::IsRunningOnChromeOS()) { | 193 int64 touch_display_id = |
328 // X maps the touch-surface to the size of the X root-window. | 194 ui::DeviceDataManager::GetInstance()->GetDisplayForTouchDevice( |
329 // In multi-monitor setup, Coordinate Transformation Matrix | 195 xiev->deviceid); |
330 // repositions the touch-surface onto part of X root-window | 196 // If we don't have record of display id for this touch device, check |
331 // containing aura root-window corresponding to the touchscreen. | 197 // that if the event is within the bound of the root window. Note |
332 // However, if aura root-window has non-zero origin, | 198 // that in multi-monitor case, the event position is in framebuffer |
333 // we need to relocate the event into aura root-window coordinates. | 199 // space so the bounds check will not work so well. |
334 touchev->Relocate(bounds().origin()); | 200 if (touch_display_id == gfx::Display::kInvalidDisplayID) { |
335 #if defined(USE_XI2_MT) | 201 if (base::SysInfo::IsRunningOnChromeOS() && |
336 if (is_internal_display_) | 202 !bounds().Contains(ui::EventLocationFromNative(xev))) |
337 touch_calibrate_->Calibrate(touchev, bounds()); | 203 return false; |
338 #endif // defined(USE_XI2_MT) | 204 } else if (touch_display_id != display_ids_.first && |
| 205 touch_display_id != display_ids_.second) { |
| 206 return false; |
339 } | 207 } |
340 break; | 208 #endif // defined(OS_CHROMEOS) |
| 209 return true; |
341 } | 210 } |
342 default: { | 211 default: |
343 aura::Window* root_window = window(); | 212 return true; |
344 aura::client::ScreenPositionClient* screen_position_client = | 213 } |
345 aura::client::GetScreenPositionClient(root_window); | 214 } |
346 gfx::Rect local(bounds().size()); | 215 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent( |
347 local.Inset(transformer_helper_.GetHostInsets()); | 216 ui::LocatedEvent* event) { |
348 if (screen_position_client && !local.Contains(event->location())) { | 217 if (!event->IsTouchEvent()) { |
349 gfx::Point location(event->location()); | 218 aura::Window* root_window = window(); |
350 // In order to get the correct point in screen coordinates | 219 aura::client::ScreenPositionClient* screen_position_client = |
351 // during passive grab, we first need to find on which host window | 220 aura::client::GetScreenPositionClient(root_window); |
352 // the mouse is on, and find out the screen coordinates on that | 221 gfx::Rect local(bounds().size()); |
353 // host window, then convert it back to this host window's coordinate. | 222 local.Inset(transformer_helper_.GetHostInsets()); |
354 screen_position_client->ConvertHostPointToScreen(root_window, | 223 |
355 &location); | 224 if (screen_position_client && !local.Contains(event->location())) { |
356 screen_position_client->ConvertPointFromScreen(root_window, &location); | 225 gfx::Point location(event->location()); |
357 ConvertPointToHost(&location); | 226 // In order to get the correct point in screen coordinates |
358 event->set_location(location); | 227 // during passive grab, we first need to find on which host window |
359 event->set_root_location(location); | 228 // the mouse is on, and find out the screen coordinates on that |
360 } | 229 // host window, then convert it back to this host window's coordinate. |
361 break; | 230 screen_position_client->ConvertHostPointToScreen(root_window, |
| 231 &location); |
| 232 screen_position_client->ConvertPointFromScreen(root_window, &location); |
| 233 ConvertPointToHost(&location); |
| 234 event->set_location(location); |
| 235 event->set_root_location(location); |
362 } | 236 } |
363 } | 237 } |
364 SendEventToProcessor(event); | 238 SendEventToProcessor(event); |
365 } | 239 } |
366 | 240 |
367 void AshWindowTreeHostX11::UpdateIsInternalDisplay() { | |
368 aura::Window* root_window = window(); | |
369 gfx::Screen* screen = gfx::Screen::GetScreenFor(root_window); | |
370 gfx::Display display = screen->GetDisplayNearestWindow(root_window); | |
371 DCHECK(display.is_valid()); | |
372 is_internal_display_ = display.IsInternal(); | |
373 } | |
374 | |
375 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state) { | 241 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state) { |
376 if (!ui::IsXInput2Available()) | 242 if (!ui::IsXInput2Available()) |
377 return; | 243 return; |
378 // Temporarily pause tap-to-click when the cursor is hidden. | 244 // Temporarily pause tap-to-click when the cursor is hidden. |
379 Atom prop = atom_cache()->GetAtom("Tap Paused"); | 245 Atom prop = atom_cache()->GetAtom("Tap Paused"); |
380 unsigned char value = state; | 246 unsigned char value = state; |
381 XIDeviceList dev_list = | 247 XIDeviceList dev_list = |
382 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay()); | 248 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay()); |
383 | 249 |
384 // Only slave pointer devices could possibly have tap-paused property. | 250 // Only slave pointer devices could possibly have tap-paused property. |
(...skipping 28 matching lines...) Expand all Loading... |
413 1); | 279 1); |
414 } | 280 } |
415 } | 281 } |
416 } | 282 } |
417 | 283 |
418 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) { | 284 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) { |
419 return new AshWindowTreeHostX11(initial_bounds); | 285 return new AshWindowTreeHostX11(initial_bounds); |
420 } | 286 } |
421 | 287 |
422 } // namespace ash | 288 } // namespace ash |
OLD | NEW |