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

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

Issue 201573015: Introdcue AshWindowTreeHost and move ash/chrome specific code in WTH to ash. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « ash/host/ash_window_tree_host_x11.h ('k') | ash/host/ash_window_tree_host_x11_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ash/host/ash_window_tree_host_x11.h"
6
7 #include <X11/extensions/Xfixes.h>
8 #include <X11/extensions/XInput2.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xlib.h>
11
12 #include <string>
13 #include <vector>
14
15 #include "ash/host/root_window_transformer.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"
21 #include "ui/aura/client/screen_position_client.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/window.h"
24 #include "ui/base/x/x11_util.h"
25 #include "ui/events/event.h"
26 #include "ui/events/event_switches.h"
27 #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"
31 #include "ui/events/x/device_list_cache_x.h"
32 #include "ui/events/x/touch_factory_x11.h"
33
34 #include "ui/gfx/rect.h"
35 #include "ui/gfx/screen.h"
36
37 namespace ash {
38
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)
169 : WindowTreeHostX11(initial_bounds),
170 is_internal_display_(false),
171 touch_calibrate_(new TouchEventCalibrate),
172 transformer_helper_(this) {
173 aura::Env::GetInstance()->AddObserver(this);
174 }
175
176 AshWindowTreeHostX11::~AshWindowTreeHostX11() {
177 aura::Env::GetInstance()->RemoveObserver(this);
178 UnConfineCursor();
179 }
180
181 void AshWindowTreeHostX11::ToggleFullScreen() { NOTIMPLEMENTED(); }
182
183 bool AshWindowTreeHostX11::ConfineCursorToRootWindow() {
184 #if XFIXES_MAJOR >= 5
185 DCHECK(!pointer_barriers_.get());
186 if (pointer_barriers_)
187 return false;
188 pointer_barriers_.reset(new XID[4]);
189 gfx::Rect barrier(bounds());
190 barrier.Inset(transformer_helper_.GetHostInsets());
191 // Horizontal, top barriers.
192 pointer_barriers_[0] = XFixesCreatePointerBarrier(xdisplay(),
193 x_root_window(),
194 barrier.x(),
195 barrier.y(),
196 barrier.right(),
197 barrier.y(),
198 BarrierPositiveY,
199 0,
200 XIAllDevices);
201 // Horizontal, bottom barriers.
202 pointer_barriers_[1] = XFixesCreatePointerBarrier(xdisplay(),
203 x_root_window(),
204 barrier.x(),
205 barrier.bottom(),
206 barrier.right(),
207 barrier.bottom(),
208 BarrierNegativeY,
209 0,
210 XIAllDevices);
211 // Vertical, left barriers.
212 pointer_barriers_[2] = XFixesCreatePointerBarrier(xdisplay(),
213 x_root_window(),
214 barrier.x(),
215 barrier.y(),
216 barrier.x(),
217 barrier.bottom(),
218 BarrierPositiveX,
219 0,
220 XIAllDevices);
221 // Vertical, right barriers.
222 pointer_barriers_[3] = XFixesCreatePointerBarrier(xdisplay(),
223 x_root_window(),
224 barrier.right(),
225 barrier.y(),
226 barrier.right(),
227 barrier.bottom(),
228 BarrierNegativeX,
229 0,
230 XIAllDevices);
231 #endif
232 return true;
233 }
234
235 void AshWindowTreeHostX11::UnConfineCursor() {
236 #if XFIXES_MAJOR >= 5
237 if (pointer_barriers_) {
238 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[0]);
239 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[1]);
240 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[2]);
241 XFixesDestroyPointerBarrier(xdisplay(), pointer_barriers_[3]);
242 pointer_barriers_.reset();
243 }
244 #endif
245 }
246
247 void AshWindowTreeHostX11::SetRootWindowTransformer(
248 scoped_ptr<RootWindowTransformer> transformer) {
249 transformer_helper_.SetRootWindowTransformer(transformer.Pass());
250 UpdateIsInternalDisplay();
251 if (pointer_barriers_) {
252 UnConfineCursor();
253 ConfineCursorToRootWindow();
254 }
255 }
256
257 aura::WindowTreeHost* AshWindowTreeHostX11::AsWindowTreeHost() { return this; }
258
259 void AshWindowTreeHostX11::SetBounds(const gfx::Rect& bounds) {
260 WindowTreeHostX11::SetBounds(bounds);
261 UpdateIsInternalDisplay();
262 if (pointer_barriers_) {
263 UnConfineCursor();
264 ConfineCursorToRootWindow();
265 }
266 }
267
268 gfx::Transform AshWindowTreeHostX11::GetRootTransform() const {
269 return transformer_helper_.GetTransform();
270 }
271
272 void AshWindowTreeHostX11::SetRootTransform(const gfx::Transform& transform) {
273 transformer_helper_.SetTransform(transform);
274 }
275
276 gfx::Transform AshWindowTreeHostX11::GetInverseRootTransform() const {
277 return transformer_helper_.GetInverseTransform();
278 }
279
280 void AshWindowTreeHostX11::UpdateRootWindowSize(const gfx::Size& host_size) {
281 transformer_helper_.UpdateWindowSize(host_size);
282 }
283
284 void AshWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) {
285 SetCrOSTapPaused(!show);
286 }
287
288 void AshWindowTreeHostX11::OnWindowInitialized(aura::Window* window) {}
289
290 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())
296 return;
297 UpdateIsInternalDisplay();
298
299 // We have to enable Tap-to-click by default because the cursor is set to
300 // visible in Shell::InitRootWindowController.
301 SetCrOSTapPaused(false);
302 }
303
304 void AshWindowTreeHostX11::OnConfigureNotify() {
305 UpdateIsInternalDisplay();
306
307 // Always update barrier and mouse location because |bounds_| might
308 // have already been updated in |SetBounds|.
309 if (pointer_barriers_) {
310 UnConfineCursor();
311 ConfineCursorToRootWindow();
312 }
313 }
314
315 void AshWindowTreeHostX11::TranslateAndDispatchLocatedEvent(
316 ui::LocatedEvent* event) {
317 switch (event->type()) {
318 case ui::ET_TOUCH_MOVED:
319 case ui::ET_TOUCH_PRESSED:
320 case ui::ET_TOUCH_CANCELLED:
321 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;
337 }
338 default: {
339 aura::Window* root_window = window();
340 aura::client::ScreenPositionClient* screen_position_client =
341 aura::client::GetScreenPositionClient(root_window);
342 gfx::Rect local(bounds().size());
343
344 if (screen_position_client && !local.Contains(event->location())) {
345 gfx::Point location(event->location());
346 // In order to get the correct point in screen coordinates
347 // 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
349 // host window, then convert it back to this host window's coordinate.
350 screen_position_client->ConvertHostPointToScreen(root_window,
351 &location);
352 screen_position_client->ConvertPointFromScreen(root_window, &location);
353 ConvertPointToHost(&location);
354 event->set_location(location);
355 event->set_root_location(location);
356 }
357 break;
358 }
359 }
360 SendEventToProcessor(event);
361 }
362
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) {
372 if (!ui::IsXInput2Available())
373 return;
374 // Temporarily pause tap-to-click when the cursor is hidden.
375 Atom prop = atom_cache()->GetAtom("Tap Paused");
376 unsigned char value = state;
377 XIDeviceList dev_list =
378 ui::DeviceListCacheX::GetInstance()->GetXI2DeviceList(xdisplay());
379
380 // Only slave pointer devices could possibly have tap-paused property.
381 for (int i = 0; i < dev_list.count; i++) {
382 if (dev_list[i].use == XISlavePointer) {
383 Atom old_type;
384 int old_format;
385 unsigned long old_nvalues, bytes;
386 unsigned char* data;
387 int result = XIGetProperty(xdisplay(),
388 dev_list[i].deviceid,
389 prop,
390 0,
391 0,
392 False,
393 AnyPropertyType,
394 &old_type,
395 &old_format,
396 &old_nvalues,
397 &bytes,
398 &data);
399 if (result != Success)
400 continue;
401 XFree(data);
402 XIChangeProperty(xdisplay(),
403 dev_list[i].deviceid,
404 prop,
405 XA_INTEGER,
406 8,
407 PropModeReplace,
408 &value,
409 1);
410 }
411 }
412 }
413
414 AshWindowTreeHost* AshWindowTreeHost::Create(const gfx::Rect& initial_bounds) {
415 return new AshWindowTreeHostX11(initial_bounds);
416 }
417
418 } // namespace ash
OLDNEW
« no previous file with comments | « ash/host/ash_window_tree_host_x11.h ('k') | ash/host/ash_window_tree_host_x11_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698