ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ash / touch / touch_transformer_controller.cc
blobd88416e6feb9146f2729ed1a597df3d2ece1993c
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/touch/touch_transformer_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/host/ash_window_tree_host.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/shell.h"
12 #include "ui/aura/window_tree_host.h"
13 #include "ui/display/chromeos/display_configurator.h"
14 #include "ui/display/types/display_snapshot.h"
15 #include "ui/events/devices/device_data_manager.h"
17 namespace ash {
19 namespace {
21 DisplayManager* GetDisplayManager() {
22 return Shell::GetInstance()->display_manager();
25 ui::TouchscreenDevice FindTouchscreenById(unsigned int id) {
26 const std::vector<ui::TouchscreenDevice>& touchscreens =
27 ui::DeviceDataManager::GetInstance()->touchscreen_devices();
28 for (const auto& touchscreen : touchscreens) {
29 if (touchscreen.id == id)
30 return touchscreen;
33 return ui::TouchscreenDevice();
36 } // namespace
38 // This is to compute the scale ratio for the TouchEvent's radius. The
39 // configured resolution of the display is not always the same as the touch
40 // screen's reporting resolution, e.g. the display could be set as
41 // 1920x1080 while the touchscreen is reporting touch position range at
42 // 32767x32767. Touch radius is reported in the units the same as touch position
43 // so we need to scale the touch radius to be compatible with the display's
44 // resolution. We compute the scale as
45 // sqrt of (display_area / touchscreen_area)
46 double TouchTransformerController::GetTouchResolutionScale(
47 const DisplayInfo& touch_display,
48 const ui::TouchscreenDevice& touch_device) const {
49 if (touch_device.id == ui::InputDevice::kInvalidId ||
50 touch_device.size.IsEmpty() ||
51 touch_display.bounds_in_native().size().IsEmpty())
52 return 1.0;
54 double display_area = touch_display.bounds_in_native().size().GetArea();
55 double touch_area = touch_device.size.GetArea();
56 double ratio = std::sqrt(display_area / touch_area);
58 VLOG(2) << "Display size: "
59 << touch_display.bounds_in_native().size().ToString()
60 << ", Touchscreen size: " << touch_device.size.ToString()
61 << ", Touch radius scale ratio: " << ratio;
62 return ratio;
65 gfx::Transform TouchTransformerController::GetTouchTransform(
66 const DisplayInfo& display,
67 const DisplayInfo& touch_display,
68 const ui::TouchscreenDevice& touchscreen,
69 const gfx::Size& framebuffer_size) const {
70 gfx::SizeF current_size = display.bounds_in_native().size();
71 gfx::SizeF touch_native_size = touch_display.GetNativeModeSize();
72 #if defined(USE_OZONE)
73 gfx::SizeF touch_area = touchscreen.size;
74 #elif defined(USE_X11)
75 // On X11 touches are reported in the framebuffer coordinate space.
76 gfx::SizeF touch_area = framebuffer_size;
77 #endif
79 gfx::Transform ctm;
81 if (current_size.IsEmpty() || touch_native_size.IsEmpty() ||
82 touch_area.IsEmpty() || touchscreen.id == ui::InputDevice::kInvalidId)
83 return ctm;
85 #if defined(USE_OZONE)
86 // Translate the touch so that it falls within the display bounds.
87 ctm.Translate(display.bounds_in_native().x(),
88 display.bounds_in_native().y());
89 #endif
91 // Take care of panel fitting only if supported. Panel fitting is emulated in
92 // software mirroring mode (display != touch_display).
93 // If panel fitting is enabled then the aspect ratio is preserved and the
94 // display is scaled acordingly. In this case blank regions would be present
95 // in order to center the displayed area.
96 if (display.is_aspect_preserving_scaling() ||
97 display.id() != touch_display.id()) {
98 float touch_native_ar =
99 touch_native_size.width() / touch_native_size.height();
100 float current_ar = current_size.width() / current_size.height();
102 if (current_ar > touch_native_ar) { // Letterboxing
103 ctm.Translate(
104 0, (1 - current_ar / touch_native_ar) * 0.5 * current_size.height());
105 ctm.Scale(1, current_ar / touch_native_ar);
106 } else if (touch_native_ar > current_ar) { // Pillarboxing
107 ctm.Translate(
108 (1 - touch_native_ar / current_ar) * 0.5 * current_size.width(), 0);
109 ctm.Scale(touch_native_ar / current_ar, 1);
113 // Take care of scaling between touchscreen area and display resolution.
114 ctm.Scale(current_size.width() / touch_area.width(),
115 current_size.height() / touch_area.height());
116 return ctm;
119 TouchTransformerController::TouchTransformerController() {
120 Shell::GetInstance()->display_controller()->AddObserver(this);
123 TouchTransformerController::~TouchTransformerController() {
124 Shell::GetInstance()->display_controller()->RemoveObserver(this);
127 void TouchTransformerController::UpdateTouchTransformer() const {
128 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance();
129 device_manager->ClearTouchTransformerRecord();
131 // Display IDs and DisplayInfo for mirror or extended mode.
132 int64 display1_id = gfx::Display::kInvalidDisplayID;
133 int64 display2_id = gfx::Display::kInvalidDisplayID;
134 DisplayInfo display1;
135 DisplayInfo display2;
136 // Display ID and DisplayInfo for single display mode.
137 int64 single_display_id = gfx::Display::kInvalidDisplayID;
138 DisplayInfo single_display;
140 DisplayController* display_controller =
141 Shell::GetInstance()->display_controller();
142 ui::MultipleDisplayState display_state =
143 Shell::GetInstance()->display_configurator()->display_state();
144 if (display_state == ui::MULTIPLE_DISPLAY_STATE_INVALID ||
145 display_state == ui::MULTIPLE_DISPLAY_STATE_HEADLESS) {
146 return;
147 } else if (display_state == ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR ||
148 display_state == ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED) {
149 DisplayIdPair id_pair = GetDisplayManager()->GetCurrentDisplayIdPair();
150 display1_id = id_pair.first;
151 display2_id = id_pair.second;
152 DCHECK(display1_id != gfx::Display::kInvalidDisplayID &&
153 display2_id != gfx::Display::kInvalidDisplayID);
154 display1 = GetDisplayManager()->GetDisplayInfo(display1_id);
155 display2 = GetDisplayManager()->GetDisplayInfo(display2_id);
156 device_manager->UpdateTouchRadiusScale(
157 display1.touch_device_id(),
158 GetTouchResolutionScale(
159 display1,
160 FindTouchscreenById(display1.touch_device_id())));
161 device_manager->UpdateTouchRadiusScale(
162 display2.touch_device_id(),
163 GetTouchResolutionScale(
164 display2,
165 FindTouchscreenById(display2.touch_device_id())));
166 } else {
167 single_display_id = GetDisplayManager()->first_display_id();
168 DCHECK(single_display_id != gfx::Display::kInvalidDisplayID);
169 single_display = GetDisplayManager()->GetDisplayInfo(single_display_id);
170 device_manager->UpdateTouchRadiusScale(
171 single_display.touch_device_id(),
172 GetTouchResolutionScale(
173 single_display,
174 FindTouchscreenById(single_display.touch_device_id())));
177 gfx::Size fb_size =
178 Shell::GetInstance()->display_configurator()->framebuffer_size();
180 if (display_state == ui::MULTIPLE_DISPLAY_STATE_DUAL_MIRROR) {
181 // In mirror mode, both displays share the same root window so
182 // both display ids are associated with the root window.
183 aura::Window* root = display_controller->GetPrimaryRootWindow();
184 RootWindowController::ForWindow(root)->ash_host()->UpdateDisplayID(
185 display1_id, display2_id);
186 device_manager->UpdateTouchInfoForDisplay(
187 display1_id, display1.touch_device_id(),
188 GetTouchTransform(display1, display1,
189 FindTouchscreenById(display1.touch_device_id()),
190 fb_size));
191 device_manager->UpdateTouchInfoForDisplay(
192 display2_id, display2.touch_device_id(),
193 GetTouchTransform(display2, display2,
194 FindTouchscreenById(display2.touch_device_id()),
195 fb_size));
196 return;
199 if (display_state == ui::MULTIPLE_DISPLAY_STATE_DUAL_EXTENDED) {
200 // In extended but software mirroring mode, ther is only one X root window
201 // that associates with both displays.
202 if (GetDisplayManager()->software_mirroring_enabled()) {
203 aura::Window* root = display_controller->GetPrimaryRootWindow();
204 RootWindowController::ForWindow(root)->ash_host()->UpdateDisplayID(
205 display1_id, display2_id);
206 DisplayInfo source_display =
207 display_controller->GetPrimaryDisplayId() == display1_id ? display1
208 : display2;
209 // Mapping from framebuffer size to the source display's native
210 // resolution.
211 device_manager->UpdateTouchInfoForDisplay(
212 display1_id, display1.touch_device_id(),
213 GetTouchTransform(source_display, display1,
214 FindTouchscreenById(display1.touch_device_id()),
215 fb_size));
216 device_manager->UpdateTouchInfoForDisplay(
217 display2_id, display2.touch_device_id(),
218 GetTouchTransform(source_display, display2,
219 FindTouchscreenById(display2.touch_device_id()),
220 fb_size));
221 } else {
222 // In actual extended mode, each display is associated with one root
223 // window.
224 aura::Window* root1 =
225 display_controller->GetRootWindowForDisplayId(display1_id);
226 aura::Window* root2 =
227 display_controller->GetRootWindowForDisplayId(display2_id);
228 RootWindowController::ForWindow(root1)->ash_host()->UpdateDisplayID(
229 display1_id, gfx::Display::kInvalidDisplayID);
230 RootWindowController::ForWindow(root2)->ash_host()->UpdateDisplayID(
231 display2_id, gfx::Display::kInvalidDisplayID);
232 // Mapping from framebuffer size to each display's native resolution.
233 device_manager->UpdateTouchInfoForDisplay(
234 display1_id, display1.touch_device_id(),
235 GetTouchTransform(display1, display1,
236 FindTouchscreenById(display1.touch_device_id()),
237 fb_size));
238 device_manager->UpdateTouchInfoForDisplay(
239 display2_id, display2.touch_device_id(),
240 GetTouchTransform(display2, display2,
241 FindTouchscreenById(display2.touch_device_id()),
242 fb_size));
244 return;
247 // Single display mode. The root window has one associated display id.
248 aura::Window* root =
249 display_controller->GetRootWindowForDisplayId(single_display.id());
250 RootWindowController::ForWindow(root)->ash_host()->UpdateDisplayID(
251 single_display.id(), gfx::Display::kInvalidDisplayID);
252 device_manager->UpdateTouchInfoForDisplay(
253 single_display_id, single_display.touch_device_id(),
254 GetTouchTransform(single_display, single_display,
255 FindTouchscreenById(single_display.touch_device_id()),
256 fb_size));
259 void TouchTransformerController::OnDisplaysInitialized() {
260 UpdateTouchTransformer();
263 void TouchTransformerController::OnDisplayConfigurationChanged() {
264 UpdateTouchTransformer();
267 } // namespace ash