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/touch/touchscreen_util.h" | 5 #include "ash/touch/touchscreen_util.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "ui/events/devices/input_device.h" | 12 #include "ui/events/devices/input_device.h" |
13 | 13 |
14 namespace ash { | 14 namespace ash { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 using DisplayInfoList = std::vector<DisplayInfo*>; | 18 using DisplayInfoList = std::vector<display::ManagedDisplayInfo*>; |
19 using DeviceList = std::vector<const ui::TouchscreenDevice*>; | 19 using DeviceList = std::vector<const ui::TouchscreenDevice*>; |
20 | 20 |
21 // Helper method to associate |display| and |device|. | 21 // Helper method to associate |display| and |device|. |
22 void Associate(DisplayInfo* display, const ui::TouchscreenDevice* device) { | 22 void Associate(display::ManagedDisplayInfo* display, |
| 23 const ui::TouchscreenDevice* device) { |
23 display->AddInputDevice(device->id); | 24 display->AddInputDevice(device->id); |
24 display->set_touch_support(display::Display::TOUCH_SUPPORT_AVAILABLE); | 25 display->set_touch_support(display::Display::TOUCH_SUPPORT_AVAILABLE); |
25 } | 26 } |
26 | 27 |
27 // Returns true if |path| is likely a USB device. | 28 // Returns true if |path| is likely a USB device. |
28 bool IsDeviceConnectedViaUsb(const base::FilePath& path) { | 29 bool IsDeviceConnectedViaUsb(const base::FilePath& path) { |
29 std::vector<base::FilePath::StringType> components; | 30 std::vector<base::FilePath::StringType> components; |
30 path.GetComponents(&components); | 31 path.GetComponents(&components); |
31 | 32 |
32 for (base::FilePath::StringType component : components) { | 33 for (base::FilePath::StringType component : components) { |
33 if (base::StartsWith(component, "usb", | 34 if (base::StartsWith(component, "usb", |
34 base::CompareCase::INSENSITIVE_ASCII)) | 35 base::CompareCase::INSENSITIVE_ASCII)) |
35 return true; | 36 return true; |
36 } | 37 } |
37 | 38 |
38 return false; | 39 return false; |
39 } | 40 } |
40 | 41 |
41 // Returns the UDL association score between |display| and |device|. A score <= | 42 // Returns the UDL association score between |display| and |device|. A score <= |
42 // 0 means that there is no association. | 43 // 0 means that there is no association. |
43 int GetUdlAssociationScore(DisplayInfo* display, | 44 int GetUdlAssociationScore(display::ManagedDisplayInfo* display, |
44 const ui::TouchscreenDevice* device) { | 45 const ui::TouchscreenDevice* device) { |
45 // If the devices are not both connected via USB, then there cannot be a UDL | 46 // If the devices are not both connected via USB, then there cannot be a UDL |
46 // association score. | 47 // association score. |
47 if (!IsDeviceConnectedViaUsb(display->sys_path()) || | 48 if (!IsDeviceConnectedViaUsb(display->sys_path()) || |
48 !IsDeviceConnectedViaUsb(device->sys_path)) | 49 !IsDeviceConnectedViaUsb(device->sys_path)) |
49 return 0; | 50 return 0; |
50 | 51 |
51 // The association score is simply the number of prefix path components that | 52 // The association score is simply the number of prefix path components that |
52 // sysfs paths have in common. | 53 // sysfs paths have in common. |
53 std::vector<base::FilePath::StringType> display_components; | 54 std::vector<base::FilePath::StringType> display_components; |
54 std::vector<base::FilePath::StringType> device_components; | 55 std::vector<base::FilePath::StringType> device_components; |
55 display->sys_path().GetComponents(&display_components); | 56 display->sys_path().GetComponents(&display_components); |
56 device->sys_path.GetComponents(&device_components); | 57 device->sys_path.GetComponents(&device_components); |
57 | 58 |
58 std::size_t largest_idx = 0; | 59 std::size_t largest_idx = 0; |
59 while (largest_idx < display_components.size() && | 60 while (largest_idx < display_components.size() && |
60 largest_idx < device_components.size() && | 61 largest_idx < device_components.size() && |
61 display_components[largest_idx] == device_components[largest_idx]) { | 62 display_components[largest_idx] == device_components[largest_idx]) { |
62 ++largest_idx; | 63 ++largest_idx; |
63 } | 64 } |
64 return largest_idx; | 65 return largest_idx; |
65 } | 66 } |
66 | 67 |
67 // Tries to find a UDL device that best matches |display|. Returns nullptr | 68 // Tries to find a UDL device that best matches |display|. Returns nullptr |
68 // if one is not found. | 69 // if one is not found. |
69 const ui::TouchscreenDevice* GuessBestUdlDevice(DisplayInfo* display, | 70 const ui::TouchscreenDevice* GuessBestUdlDevice( |
70 const DeviceList& devices) { | 71 display::ManagedDisplayInfo* display, |
| 72 const DeviceList& devices) { |
71 int best_score = 0; | 73 int best_score = 0; |
72 const ui::TouchscreenDevice* best_device = nullptr; | 74 const ui::TouchscreenDevice* best_device = nullptr; |
73 | 75 |
74 for (const ui::TouchscreenDevice* device : devices) { | 76 for (const ui::TouchscreenDevice* device : devices) { |
75 int score = GetUdlAssociationScore(display, device); | 77 int score = GetUdlAssociationScore(display, device); |
76 if (score > best_score) { | 78 if (score > best_score) { |
77 best_score = score; | 79 best_score = score; |
78 best_device = device; | 80 best_device = device; |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
82 return best_device; | 84 return best_device; |
83 } | 85 } |
84 | 86 |
85 void AssociateUdlDevices(DisplayInfoList* displays, DeviceList* devices) { | 87 void AssociateUdlDevices(DisplayInfoList* displays, DeviceList* devices) { |
86 VLOG(2) << "Trying to match udl devices (" << displays->size() | 88 VLOG(2) << "Trying to match udl devices (" << displays->size() |
87 << " displays and " << devices->size() << " devices to match)"; | 89 << " displays and " << devices->size() << " devices to match)"; |
88 | 90 |
89 DisplayInfoList::iterator display_it = displays->begin(); | 91 DisplayInfoList::iterator display_it = displays->begin(); |
90 while (display_it != displays->end()) { | 92 while (display_it != displays->end()) { |
91 DisplayInfo* display = *display_it; | 93 display::ManagedDisplayInfo* display = *display_it; |
92 const ui::TouchscreenDevice* device = GuessBestUdlDevice(display, *devices); | 94 const ui::TouchscreenDevice* device = GuessBestUdlDevice(display, *devices); |
93 | 95 |
94 if (device) { | 96 if (device) { |
95 VLOG(2) << "=> Matched device " << device->name << " to display " | 97 VLOG(2) << "=> Matched device " << device->name << " to display " |
96 << display->name() | 98 << display->name() |
97 << " (score=" << GetUdlAssociationScore(display, device) << ")"; | 99 << " (score=" << GetUdlAssociationScore(display, device) << ")"; |
98 Associate(display, device); | 100 Associate(display, device); |
99 | 101 |
100 display_it = displays->erase(display_it); | 102 display_it = displays->erase(display_it); |
101 devices->erase(std::find(devices->begin(), devices->end(), device)); | 103 devices->erase(std::find(devices->begin(), devices->end(), device)); |
102 | 104 |
103 continue; | 105 continue; |
104 } | 106 } |
105 | 107 |
106 ++display_it; | 108 ++display_it; |
107 } | 109 } |
108 } | 110 } |
109 | 111 |
110 // Returns true if |display| is internal. | 112 // Returns true if |display| is internal. |
111 bool IsInternalDisplay(DisplayInfo* display) { | 113 bool IsInternalDisplay(display::ManagedDisplayInfo* display) { |
112 return display::Display::IsInternalDisplayId(display->id()); | 114 return display::Display::IsInternalDisplayId(display->id()); |
113 } | 115 } |
114 | 116 |
115 // Returns true if |device| is internal. | 117 // Returns true if |device| is internal. |
116 bool IsInternalDevice(const ui::TouchscreenDevice* device) { | 118 bool IsInternalDevice(const ui::TouchscreenDevice* device) { |
117 return device->type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL; | 119 return device->type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL; |
118 } | 120 } |
119 | 121 |
120 void AssociateInternalDevices(DisplayInfoList* displays, DeviceList* devices) { | 122 void AssociateInternalDevices(DisplayInfoList* displays, DeviceList* devices) { |
121 VLOG(2) << "Trying to match internal devices (" << displays->size() | 123 VLOG(2) << "Trying to match internal devices (" << displays->size() |
122 << " displays and " << devices->size() << " devices to match)"; | 124 << " displays and " << devices->size() << " devices to match)"; |
123 | 125 |
124 // Internal device assocation has a couple of gotchas: | 126 // Internal device assocation has a couple of gotchas: |
125 // - There can be internal devices but no internal display, or visa-versa. | 127 // - There can be internal devices but no internal display, or visa-versa. |
126 // - There can be multiple internal devices matching one internal display. We | 128 // - There can be multiple internal devices matching one internal display. We |
127 // assume there is at most one internal display. | 129 // assume there is at most one internal display. |
128 // - All internal devices must be removed from |displays| and |devices| after | 130 // - All internal devices must be removed from |displays| and |devices| after |
129 // this function has returned, since an internal device can never be | 131 // this function has returned, since an internal device can never be |
130 // associated with an external device. | 132 // associated with an external device. |
131 | 133 |
132 // Capture the internal display reference as we remove it from |displays|. | 134 // Capture the internal display reference as we remove it from |displays|. |
133 DisplayInfo* internal_display = nullptr; | 135 display::ManagedDisplayInfo* internal_display = nullptr; |
134 DisplayInfoList::iterator display_it = | 136 DisplayInfoList::iterator display_it = |
135 std::find_if(displays->begin(), displays->end(), &IsInternalDisplay); | 137 std::find_if(displays->begin(), displays->end(), &IsInternalDisplay); |
136 if (display_it != displays->end()) { | 138 if (display_it != displays->end()) { |
137 internal_display = *display_it; | 139 internal_display = *display_it; |
138 displays->erase(display_it); | 140 displays->erase(display_it); |
139 } | 141 } |
140 | 142 |
141 bool matched = false; | 143 bool matched = false; |
142 | 144 |
143 // Remove all internal devices from |devices|. If we have an internal display, | 145 // Remove all internal devices from |devices|. If we have an internal display, |
(...skipping 24 matching lines...) Expand all Loading... |
168 VLOG(2) << "=> Removing internal display " << internal_display->name(); | 170 VLOG(2) << "=> Removing internal display " << internal_display->name(); |
169 } | 171 } |
170 | 172 |
171 void AssociateSameSizeDevices(DisplayInfoList* displays, DeviceList* devices) { | 173 void AssociateSameSizeDevices(DisplayInfoList* displays, DeviceList* devices) { |
172 // Associate screens/displays with the same size. | 174 // Associate screens/displays with the same size. |
173 VLOG(2) << "Trying to match same-size devices (" << displays->size() | 175 VLOG(2) << "Trying to match same-size devices (" << displays->size() |
174 << " displays and " << devices->size() << " devices to match)"; | 176 << " displays and " << devices->size() << " devices to match)"; |
175 | 177 |
176 DisplayInfoList::iterator display_it = displays->begin(); | 178 DisplayInfoList::iterator display_it = displays->begin(); |
177 while (display_it != displays->end()) { | 179 while (display_it != displays->end()) { |
178 DisplayInfo* display = *display_it; | 180 display::ManagedDisplayInfo* display = *display_it; |
179 const gfx::Size native_size = display->GetNativeModeSize(); | 181 const gfx::Size native_size = display->GetNativeModeSize(); |
180 | 182 |
181 // Try to find an input device with roughly the same size as the display. | 183 // Try to find an input device with roughly the same size as the display. |
182 DeviceList::iterator device_it = std::find_if( | 184 DeviceList::iterator device_it = std::find_if( |
183 devices->begin(), devices->end(), | 185 devices->begin(), devices->end(), |
184 [&native_size](const ui::TouchscreenDevice* device) { | 186 [&native_size](const ui::TouchscreenDevice* device) { |
185 // Allow 1 pixel difference between screen and touchscreen | 187 // Allow 1 pixel difference between screen and touchscreen |
186 // resolutions. Because in some cases for monitor resolution | 188 // resolutions. Because in some cases for monitor resolution |
187 // 1024x768 touchscreen's resolution would be 1024x768, but for | 189 // 1024x768 touchscreen's resolution would be 1024x768, but for |
188 // some 1023x767. It really depends on touchscreen's firmware | 190 // some 1023x767. It really depends on touchscreen's firmware |
(...skipping 23 matching lines...) Expand all Loading... |
212 // If there is only one display left, then we should associate all input | 214 // If there is only one display left, then we should associate all input |
213 // devices with it. | 215 // devices with it. |
214 | 216 |
215 VLOG(2) << "Trying to match to single display (" << displays->size() | 217 VLOG(2) << "Trying to match to single display (" << displays->size() |
216 << " displays and " << devices->size() << " devices to match)"; | 218 << " displays and " << devices->size() << " devices to match)"; |
217 | 219 |
218 // We only associate to one display. | 220 // We only associate to one display. |
219 if (displays->size() != 1 || devices->size() == 0) | 221 if (displays->size() != 1 || devices->size() == 0) |
220 return; | 222 return; |
221 | 223 |
222 DisplayInfo* display = *displays->begin(); | 224 display::ManagedDisplayInfo* display = *displays->begin(); |
223 for (const ui::TouchscreenDevice* device : *devices) { | 225 for (const ui::TouchscreenDevice* device : *devices) { |
224 VLOG(2) << "=> Matched device " << device->name << " to display " | 226 VLOG(2) << "=> Matched device " << device->name << " to display " |
225 << display->name(); | 227 << display->name(); |
226 Associate(display, device); | 228 Associate(display, device); |
227 } | 229 } |
228 | 230 |
229 displays->clear(); | 231 displays->clear(); |
230 devices->clear(); | 232 devices->clear(); |
231 } | 233 } |
232 | 234 |
233 } // namespace | 235 } // namespace |
234 | 236 |
235 void AssociateTouchscreens( | 237 void AssociateTouchscreens( |
236 std::vector<DisplayInfo>* all_displays, | 238 std::vector<display::ManagedDisplayInfo>* all_displays, |
237 const std::vector<ui::TouchscreenDevice>& all_devices) { | 239 const std::vector<ui::TouchscreenDevice>& all_devices) { |
238 // |displays| and |devices| contain pointers directly to the values stored | 240 // |displays| and |devices| contain pointers directly to the values stored |
239 // inside of |all_displays| and |all_devices|. When a display or input device | 241 // inside of |all_displays| and |all_devices|. When a display or input device |
240 // has been associated, it is removed from the |displays| or |devices| list. | 242 // has been associated, it is removed from the |displays| or |devices| list. |
241 | 243 |
242 // Construct our initial set of display/devices that we will process. | 244 // Construct our initial set of display/devices that we will process. |
243 DisplayInfoList displays; | 245 DisplayInfoList displays; |
244 for (DisplayInfo& display : *all_displays) { | 246 for (display::ManagedDisplayInfo& display : *all_displays) { |
245 display.ClearInputDevices(); | 247 display.ClearInputDevices(); |
246 | 248 |
247 if (display.GetNativeModeSize().IsEmpty()) { | 249 if (display.GetNativeModeSize().IsEmpty()) { |
248 VLOG(2) << "Will not match display " << display.id() | 250 VLOG(2) << "Will not match display " << display.id() |
249 << " since it doesn't have a native mode"; | 251 << " since it doesn't have a native mode"; |
250 continue; | 252 continue; |
251 } | 253 } |
252 displays.push_back(&display); | 254 displays.push_back(&display); |
253 } | 255 } |
254 | 256 |
255 // Construct initial set of devices. | 257 // Construct initial set of devices. |
256 DeviceList devices; | 258 DeviceList devices; |
257 for (const ui::TouchscreenDevice& device : all_devices) | 259 for (const ui::TouchscreenDevice& device : all_devices) |
258 devices.push_back(&device); | 260 devices.push_back(&device); |
259 | 261 |
260 for (const DisplayInfo* display : displays) { | 262 for (const display::ManagedDisplayInfo* display : displays) { |
261 VLOG(2) << "Received display " << display->name() | 263 VLOG(2) << "Received display " << display->name() |
262 << " (size: " << display->GetNativeModeSize().ToString() | 264 << " (size: " << display->GetNativeModeSize().ToString() |
263 << ", sys_path: " << display->sys_path().LossyDisplayName() << ")"; | 265 << ", sys_path: " << display->sys_path().LossyDisplayName() << ")"; |
264 } | 266 } |
265 for (const ui::TouchscreenDevice* device : devices) { | 267 for (const ui::TouchscreenDevice* device : devices) { |
266 VLOG(2) << "Received device " << device->name | 268 VLOG(2) << "Received device " << device->name |
267 << " (size: " << device->size.ToString() | 269 << " (size: " << device->size.ToString() |
268 << ", sys_path: " << device->sys_path.LossyDisplayName() << ")"; | 270 << ", sys_path: " << device->sys_path.LossyDisplayName() << ")"; |
269 } | 271 } |
270 | 272 |
271 AssociateInternalDevices(&displays, &devices); | 273 AssociateInternalDevices(&displays, &devices); |
272 AssociateUdlDevices(&displays, &devices); | 274 AssociateUdlDevices(&displays, &devices); |
273 AssociateSameSizeDevices(&displays, &devices); | 275 AssociateSameSizeDevices(&displays, &devices); |
274 AssociateToSingleDisplay(&displays, &devices); | 276 AssociateToSingleDisplay(&displays, &devices); |
275 | 277 |
276 for (const DisplayInfo* display : displays) | 278 for (const display::ManagedDisplayInfo* display : displays) |
277 LOG(WARNING) << "Unmatched display " << display->name(); | 279 LOG(WARNING) << "Unmatched display " << display->name(); |
278 for (const ui::TouchscreenDevice* device : devices) | 280 for (const ui::TouchscreenDevice* device : devices) |
279 LOG(WARNING) << "Unmatched device " << device->name; | 281 LOG(WARNING) << "Unmatched device " << device->name; |
280 } | 282 } |
281 | 283 |
282 } // namespace ash | 284 } // namespace ash |
OLD | NEW |