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

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: fix all the unittests 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), 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
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 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; } 120 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; }
258 121
122 void AshWindowTreeHostX11::UpdateDisplayID(int64 id1, int64 id2) {
123 display_ids_.first = id1;
sadrul 2014/05/07 20:00:27 indent is off
Yufeng Shen (Slow to review) 2014/05/07 22:02:58 Done.
124 display_ids_.second = id2;
125 }
126
259 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) { 127 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) {
260 WindowTreeHostX11::SetBounds(bounds); 128 WindowTreeHostX11::SetBounds(bounds);
261 UpdateIsInternalDisplay();
262 if (pointer_barriers_) { 129 if (pointer_barriers_) {
263 UnConfineCursor(); 130 UnConfineCursor();
264 ConfineCursorToRootWindow(); 131 ConfineCursorToRootWindow();
265 } 132 }
266 } 133 }
267 134
268 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const { 135 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const {
269 return transformer_helper_.GetTransform(); 136 return transformer_helper_.GetTransform();
270 } 137 }
271 138
272 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) { 139 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) {
273 transformer_helper_.SetTransform(transform); 140 transformer_helper_.SetTransform(transform);
274 } 141 }
275 142
276 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const { 143 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const {
277 return transformer_helper_.GetInverseTransform(); 144 return transformer_helper_.GetInverseTransform();
278 } 145 }
279 146
280 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) { 147 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) {
281 transformer_helper_.UpdateWindowSize(host_size); 148 transformer_helper_.UpdateWindowSize(host_size);
282 } 149 }
283 150
284 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) { 151 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) {
285 SetCrOSTapPaused(!show); 152 SetCrOSTapPaused(!show);
286 } 153 }
287 154
288 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {} 155 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {}
289 156
290 void AshWindowTreeHostX11::OnHostInitialized(aura::WindowTreeHost* host) { 157 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()) 158 if (host != AsWindowTreeHost())
296 return; 159 return;
297 UpdateIsInternalDisplay();
298 160
299 // We have to enable Tap-to-click by default because the cursor is set to 161 // We have to enable Tap-to-click by default because the cursor is set to
300 // visible in Shell::InitRootWindowController. 162 // visible in Shell::InitRootWindowController.
301 SetCrOSTapPaused(false); 163 SetCrOSTapPaused(false);
302 } 164 }
303 165
304 void AshWindowTreeHostX11::OnConfigureNotify() { 166 void AshWindowTreeHostX11::OnConfigureNotify() {
305 UpdateIsInternalDisplay();
306
307 // Always update barrier and mouse location because |bounds_| might 167 // Always update barrier and mouse location because |bounds_| might
308 // have already been updated in |SetBounds|. 168 // have already been updated in |SetBounds|.
309 if (pointer_barriers_) { 169 if (pointer_barriers_) {
310 UnConfineCursor(); 170 UnConfineCursor();
311 ConfineCursorToRootWindow(); 171 ConfineCursorToRootWindow();
312 } 172 }
313 } 173 }
314 174
175 bool AshWindowTreeHostX11::CanDispatchEvent(const ui::PlatformEvent& event) {
176 if(!WindowTreeHostX11::CanDispatchEvent(event))
177 return false;
178 XEvent* xev = event;
179 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev->xcookie.data);
180 ui::EventType type = ui::EventTypeFromNative(xev);
181 // For touch event, check if the root window is residing on the according
182 // touch display.
183 switch (type) {
184 case ui::ET_TOUCH_MOVED:
185 case ui::ET_TOUCH_PRESSED:
186 case ui::ET_TOUCH_CANCELLED:
187 case ui::ET_TOUCH_RELEASED: {
188 #if defined(OS_CHROMEOS)
189 int64 touch_display_id =
190 ui::DeviceDataManager::GetInstance()->GetDisplayForTouchDevice(
191 xiev->deviceid);
192 // If we don't have record of display id for this touch device, check
193 // that if the event is within the bound of the root window. Note
194 // that in multi-monitor case, the event position is in framebuffer
195 // space so the bounds check will not work so well.
196 if (touch_display_id == gfx::Display::kInvalidDisplayID) {
197 if (base::SysInfo::IsRunningOnChromeOS() &&
198 !bounds().Contains(ui::EventLocationFromNative(xev)))
199 return false;
200 } else if (touch_display_id != display_ids_.first &&
201 touch_display_id != display_ids_.second) {
202 return false;
203 }
204 #endif // defined(OS_CHROMEOS)
205 return true;
206 }
207 default:
208 return true;
209 }
210 }
211
315 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent( 212 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent(
316 ui::LocatedEvent* event) { 213 ui::LocatedEvent* event) {
317 switch (event->type()) { 214 switch (event->type()) {
sadrul 2014/05/07 20:00:27 Remove the switch. Use if (!event->IsTouchEvent())
Yufeng Shen (Slow to review) 2014/05/07 22:02:58 Done.
318 case ui::ET_TOUCH_MOVED: 215 case ui::ET_TOUCH_MOVED:
319 case ui::ET_TOUCH_PRESSED: 216 case ui::ET_TOUCH_PRESSED:
320 case ui::ET_TOUCH_CANCELLED: 217 case ui::ET_TOUCH_CANCELLED:
321 case ui::ET_TOUCH_RELEASED: { 218 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; 219 break;
337 }
338 default: { 220 default: {
339 aura::Window* root_window = window(); 221 aura::Window* root_window = window();
340 aura::client::ScreenPositionClient* screen_position_client = 222 aura::client::ScreenPositionClient* screen_position_client =
341 aura::client::GetScreenPositionClient(root_window); 223 aura::client::GetScreenPositionClient(root_window);
342 gfx::Rect local(bounds().size()); 224 gfx::Rect local(bounds().size());
343 225
344 if (screen_position_client && !local.Contains(event->location())) { 226 if (screen_position_client && !local.Contains(event->location())) {
345 gfx::Point location(event->location()); 227 gfx::Point location(event->location());
346 // In order to get the correct point in screen coordinates 228 // In order to get the correct point in screen coordinates
347 // during passive grab, we first need to find on which host window 229 // 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 230 // 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. 231 // host window, then convert it back to this host window's coordinate.
350 screen_position_client->ConvertHostPointToScreen(root_window, 232 screen_position_client->ConvertHostPointToScreen(root_window,
351 &location); 233 &location);
352 screen_position_client->ConvertPointFromScreen(root_window, &location); 234 screen_position_client->ConvertPointFromScreen(root_window, &location);
353 ConvertPointToHost(&location); 235 ConvertPointToHost(&location);
354 event->set_location(location); 236 event->set_location(location);
355 event->set_root_location(location); 237 event->set_root_location(location);
356 } 238 }
357 break; 239 break;
358 } 240 }
359 } 241 }
360 SendEventToProcessor(event); 242 SendEventToProcessor(event);
361 } 243 }
362 244
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) { 245 void AshWindowTreeHostX11::SetCrOSTapPaused(bool state) {
372 if (!ui::IsXInput2Available()) 246 if (!ui::IsXInput2Available())
373 return; 247 return;
374 // Temporarily pause tap-to-click when the cursor is hidden. 248 // Temporarily pause tap-to-click when the cursor is hidden.
375 Atom prop = atom_cache()->GetAtom("Tap Paused"); 249 Atom prop = atom_cache()->GetAtom("Tap Paused");
376 unsigned char value = state; 250 unsigned char value = state;
377 XIDeviceList dev_list = 251 XIDeviceList dev_list =
378 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay()); 252 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay());
379 253
380 // Only slave pointer devices could possibly have tap-paused property. 254 // Only slave pointer devices could possibly have tap-paused property.
(...skipping 28 matching lines...) Expand all
409 1); 283 1);
410 } 284 }
411 } 285 }
412 } 286 }
413 287
414 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) { 288 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) {
415 return new AshWindowTreeHostX11(initial_bounds); 289 return new AshWindowTreeHostX11(initial_bounds);
416 } 290 }
417 291
418 } // namespace ash 292 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698