Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: ash/host/ash_window_tree_host_x11.cc

Issue 191223007: Move touch CTM from X into Chrome (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: move the logic of if a touch event should be dispatched to a root window into CanDispatchEvent() Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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),
171 touch_calibrate_(new TouchEventCalibrate),
172 transformer_helper_(this) { 34 transformer_helper_(this) {
173 aura::Env::GetInstance()->AddObserver(this); 35 aura::Env::GetInstance()->AddObserver(this);
174 } 36 }
175 37
176 AshWindowTreeHostX11::~AshWindowTreeHostX11() { 38 AshWindowTreeHostX11::~AshWindowTreeHostX11() {
177 aura::Env::GetInstance()->RemoveObserver(this); 39 aura::Env::GetInstance()->RemoveObserver(this);
178 UnConfineCursor(); 40 UnConfineCursor();
179 } 41 }
180 42
181 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); } 43 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[2]); 102 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[2]);
241 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[3]); 103 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[3]);
242 pointer_barriers_.reset(); 104 pointer_barriers_.reset();
243 } 105 }
244 #endif 106 #endif
245 } 107 }
246 108
247 void AshWindowTreeHostX11::SetRootWindowTransformer( 109 void AshWindowTreeHostX11::SetRootWindowTransformer(
248 scoped_ptr<RootWindowTransformer> transformer) { 110 scoped_ptr<RootWindowTransformer> transformer) {
249 transformer_helper_.SetRootWindowTransformer(transformer.Pass()); 111 transformer_helper_.SetRootWindowTransformer(transformer.Pass());
250 UpdateIsInternalDisplay();
251 if (pointer_barriers_) { 112 if (pointer_barriers_) {
252 UnConfineCursor(); 113 UnConfineCursor();
253 ConfineCursorToRootWindow(); 114 ConfineCursorToRootWindow();
254 } 115 }
255 } 116 }
256 117
257 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; } 118 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; }
258 119
259 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) { 120 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) {
260 WindowTreeHostX11::SetBounds(bounds); 121 WindowTreeHostX11::SetBounds(bounds);
261 UpdateIsInternalDisplay();
262 if (pointer_barriers_) { 122 if (pointer_barriers_) {
263 UnConfineCursor(); 123 UnConfineCursor();
264 ConfineCursorToRootWindow(); 124 ConfineCursorToRootWindow();
265 } 125 }
266 } 126 }
267 127
268 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const { 128 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const {
269 return transformer_helper_.GetTransform(); 129 return transformer_helper_.GetTransform();
270 } 130 }
271 131
272 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) { 132 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) {
273 transformer_helper_.SetTransform(transform); 133 transformer_helper_.SetTransform(transform);
274 } 134 }
275 135
276 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const { 136 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const {
277 return transformer_helper_.GetInverseTransform(); 137 return transformer_helper_.GetInverseTransform();
278 } 138 }
279 139
280 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) { 140 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) {
281 transformer_helper_.UpdateWindowSize(host_size); 141 transformer_helper_.UpdateWindowSize(host_size);
282 } 142 }
283 143
284 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) { 144 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) {
285 SetCrOSTapPaused(!show); 145 SetCrOSTapPaused(!show);
286 } 146 }
287 147
288 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {} 148 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {}
289 149
290 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost* host) { 150 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()) 151 if (host != AsWindowTreeHost())
296 return; 152 return;
297 UpdateIsInternalDisplay();
298 153
299 // We have to enable Tap-to-click by default because the cursor is set to 154 // We have to enable Tap-to-click by default because the cursor is set to
300 // visible in Shell::InitRootWindowController. 155 // visible in Shell::InitRootWindowController.
301 SetCrOSTapPaused(false); 156 SetCrOSTapPaused(false);
302 } 157 }
303 158
304 void AshWindowTreeHostX11::OnConfigureNotify() { 159 void AshWindowTreeHostX11::OnConfigureNotify() {
305 UpdateIsInternalDisplay();
306
307 // Always update barrier and mouse location because |bounds_| might 160 // Always update barrier and mouse location because |bounds_| might
308 // have already been updated in |SetBounds|. 161 // have already been updated in |SetBounds|.
309 if (pointer_barriers_) { 162 if (pointer_barriers_) {
310 UnConfineCursor(); 163 UnConfineCursor();
311 ConfineCursorToRootWindow(); 164 ConfineCursorToRootWindow();
312 } 165 }
313 } 166 }
314 167
168 bool AshWindowTreeHostX11::CanDispatchEvent(const ui::PlatformEvent& event) {
169 if(!WindowTreeHostX11::CanDispatchEvent(event))
170 return false;
171 XEvent* xev = event;
172 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev->xcookie.data);
173 ui::EventType type = ui::EventTypeFromNative(xev);
174 // For touch event, check if the root window is residing on the according
175 // touch display.
176 switch (type) {
177 case ui::ET_TOUCH_MOVED:
178 case ui::ET_TOUCH_PRESSED:
179 case ui::ET_TOUCH_CANCELLED:
180 case ui::ET_TOUCH_RELEASED: {
181 #if defined(OS_CHROMEOS)
182 int64 touch_display_id =
183 ui::DeviceDataManager::GetInstance()->GetDisplayForTouchDevice(
184 xiev->deviceid);
185 // If we don't have record of display id for this touch device, check
186 // that if the event is within the bound of the root window. Note
187 // that in multi-monitor case, the event position is in framebuffer
188 // space so the bounds check will not work so well.
189 if (touch_display_id == gfx::Display::kInvalidDisplayID) {
190 if (base::SysInfo::IsRunningOnChromeOS() &&
191 !bounds().Contains(ui::EventLocationFromNative(xev)))
192 return false;
193 } else if (touch_display_id != display_ids().first &&
194 touch_display_id != display_ids().second) {
195 return false;
196 }
197 #endif // defined(OS_CHROMEOS)
198 return true;
199 }
200 default:
201 return true;
202 }
203 }
204
315 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent( 205 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent(
316 ui::LocatedEvent* event) { 206 ui::LocatedEvent* event) {
317 switch (event->type()) { 207 switch (event->type()) {
318 case ui::ET_TOUCH_MOVED: 208 case ui::ET_TOUCH_MOVED:
319 case ui::ET_TOUCH_PRESSED: 209 case ui::ET_TOUCH_PRESSED:
320 case ui::ET_TOUCH_CANCELLED: 210 case ui::ET_TOUCH_CANCELLED:
321 case ui::ET_TOUCH_RELEASED: { 211 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)
335 }
336 break; 212 break;
337 }
338 default: { 213 default: {
339 aura::Window* root_window = window(); 214 aura::Window* root_window = window();
340 aura::client::ScreenPositionClient* screen_position_client = 215 aura::client::ScreenPositionClient* screen_position_client =
341 aura::client::GetScreenPositionClient(root_window); 216 aura::client::GetScreenPositionClient(root_window);
342 gfx::Rect local(bounds().size()); 217 gfx::Rect local(bounds().size());
343 218
344 if (screen_position_client && !local.Contains(event->location())) { 219 if (screen_position_client && !local.Contains(event->location())) {
345 gfx::Point location(event->location()); 220 gfx::Point location(event->location());
346 // In order to get the correct point in screen coordinates 221 // In order to get the correct point in screen coordinates
347 // during passive grab, we first need to find on which host window 222 // 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 223 // 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. 224 // host window, then convert it back to this host window's coordinate.
350 screen_position_client->ConvertHostPointToScreen(root_window, 225 screen_position_client->ConvertHostPointToScreen(root_window,
351 &location); 226 &location);
352 screen_position_client->ConvertPointFromScreen(root_window, &location); 227 screen_position_client->ConvertPointFromScreen(root_window, &location);
353 ConvertPointToHost(&location); 228 ConvertPointToHost(&location);
354 event->set_location(location); 229 event->set_location(location);
355 event->set_root_location(location); 230 event->set_root_location(location);
356 } 231 }
357 break; 232 break;
358 } 233 }
359 } 234 }
360 SendEventToProcessor(event); 235 SendEventToProcessor(event);
361 } 236 }
362 237
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();
369 }
370
371 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state) { 238 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state) {
372 if (!ui::IsXInput2Available()) 239 if (!ui::IsXInput2Available())
373 return; 240 return;
374 // Temporarily pause tap-to-click when the cursor is hidden. 241 // Temporarily pause tap-to-click when the cursor is hidden.
375 Atom prop = atom_cache()->GetAtom("Tap Paused"); 242 Atom prop = atom_cache()->GetAtom("Tap Paused");
376 unsigned char value = state; 243 unsigned char value = state;
377 XIDeviceList dev_list = 244 XIDeviceList dev_list =
378 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay()); 245 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay());
379 246
380 // Only slave pointer devices could possibly have tap-paused property. 247 // Only slave pointer devices could possibly have tap-paused property.
(...skipping 28 matching lines...) Expand all
409 1); 276 1);
410 } 277 }
411 } 278 }
412 } 279 }
413 280
414 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) { 281 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) {
415 return new AshWindowTreeHostX11(initial_bounds); 282 return new AshWindowTreeHostX11(initial_bounds);
416 } 283 }
417 284
418 } // namespace ash 285 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698