| 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<ui::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(ui::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(ui::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(ui::ManagedDisplayInfo* display, |
| 70 const DeviceList& devices) { | 71 const DeviceList& devices) { |
| 71 int best_score = 0; | 72 int best_score = 0; |
| 72 const ui::TouchscreenDevice* best_device = nullptr; | 73 const ui::TouchscreenDevice* best_device = nullptr; |
| 73 | 74 |
| 74 for (const ui::TouchscreenDevice* device : devices) { | 75 for (const ui::TouchscreenDevice* device : devices) { |
| 75 int score = GetUdlAssociationScore(display, device); | 76 int score = GetUdlAssociationScore(display, device); |
| 76 if (score > best_score) { | 77 if (score > best_score) { |
| 77 best_score = score; | 78 best_score = score; |
| 78 best_device = device; | 79 best_device = device; |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 return best_device; | 83 return best_device; |
| 83 } | 84 } |
| 84 | 85 |
| 85 void AssociateUdlDevices(DisplayInfoList* displays, DeviceList* devices) { | 86 void AssociateUdlDevices(DisplayInfoList* displays, DeviceList* devices) { |
| 86 VLOG(2) << "Trying to match udl devices (" << displays->size() | 87 VLOG(2) << "Trying to match udl devices (" << displays->size() |
| 87 << " displays and " << devices->size() << " devices to match)"; | 88 << " displays and " << devices->size() << " devices to match)"; |
| 88 | 89 |
| 89 DisplayInfoList::iterator display_it = displays->begin(); | 90 DisplayInfoList::iterator display_it = displays->begin(); |
| 90 while (display_it != displays->end()) { | 91 while (display_it != displays->end()) { |
| 91 DisplayInfo* display = *display_it; | 92 ui::ManagedDisplayInfo* display = *display_it; |
| 92 const ui::TouchscreenDevice* device = GuessBestUdlDevice(display, *devices); | 93 const ui::TouchscreenDevice* device = GuessBestUdlDevice(display, *devices); |
| 93 | 94 |
| 94 if (device) { | 95 if (device) { |
| 95 VLOG(2) << "=> Matched device " << device->name << " to display " | 96 VLOG(2) << "=> Matched device " << device->name << " to display " |
| 96 << display->name() | 97 << display->name() |
| 97 << " (score=" << GetUdlAssociationScore(display, device) << ")"; | 98 << " (score=" << GetUdlAssociationScore(display, device) << ")"; |
| 98 Associate(display, device); | 99 Associate(display, device); |
| 99 | 100 |
| 100 display_it = displays->erase(display_it); | 101 display_it = displays->erase(display_it); |
| 101 devices->erase(std::find(devices->begin(), devices->end(), device)); | 102 devices->erase(std::find(devices->begin(), devices->end(), device)); |
| 102 | 103 |
| 103 continue; | 104 continue; |
| 104 } | 105 } |
| 105 | 106 |
| 106 ++display_it; | 107 ++display_it; |
| 107 } | 108 } |
| 108 } | 109 } |
| 109 | 110 |
| 110 // Returns true if |display| is internal. | 111 // Returns true if |display| is internal. |
| 111 bool IsInternalDisplay(DisplayInfo* display) { | 112 bool IsInternalDisplay(ui::ManagedDisplayInfo* display) { |
| 112 return display::Display::IsInternalDisplayId(display->id()); | 113 return display::Display::IsInternalDisplayId(display->id()); |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Returns true if |device| is internal. | 116 // Returns true if |device| is internal. |
| 116 bool IsInternalDevice(const ui::TouchscreenDevice* device) { | 117 bool IsInternalDevice(const ui::TouchscreenDevice* device) { |
| 117 return device->type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL; | 118 return device->type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL; |
| 118 } | 119 } |
| 119 | 120 |
| 120 void AssociateInternalDevices(DisplayInfoList* displays, DeviceList* devices) { | 121 void AssociateInternalDevices(DisplayInfoList* displays, DeviceList* devices) { |
| 121 VLOG(2) << "Trying to match internal devices (" << displays->size() | 122 VLOG(2) << "Trying to match internal devices (" << displays->size() |
| 122 << " displays and " << devices->size() << " devices to match)"; | 123 << " displays and " << devices->size() << " devices to match)"; |
| 123 | 124 |
| 124 // Internal device assocation has a couple of gotchas: | 125 // Internal device assocation has a couple of gotchas: |
| 125 // - There can be internal devices but no internal display, or visa-versa. | 126 // - There can be internal devices but no internal display, or visa-versa. |
| 126 // - There can be multiple internal devices matching one internal display. We | 127 // - There can be multiple internal devices matching one internal display. We |
| 127 // assume there is at most one internal display. | 128 // assume there is at most one internal display. |
| 128 // - All internal devices must be removed from |displays| and |devices| after | 129 // - All internal devices must be removed from |displays| and |devices| after |
| 129 // this function has returned, since an internal device can never be | 130 // this function has returned, since an internal device can never be |
| 130 // associated with an external device. | 131 // associated with an external device. |
| 131 | 132 |
| 132 // Capture the internal display reference as we remove it from |displays|. | 133 // Capture the internal display reference as we remove it from |displays|. |
| 133 DisplayInfo* internal_display = nullptr; | 134 ui::ManagedDisplayInfo* internal_display = nullptr; |
| 134 DisplayInfoList::iterator display_it = | 135 DisplayInfoList::iterator display_it = |
| 135 std::find_if(displays->begin(), displays->end(), &IsInternalDisplay); | 136 std::find_if(displays->begin(), displays->end(), &IsInternalDisplay); |
| 136 if (display_it != displays->end()) { | 137 if (display_it != displays->end()) { |
| 137 internal_display = *display_it; | 138 internal_display = *display_it; |
| 138 displays->erase(display_it); | 139 displays->erase(display_it); |
| 139 } | 140 } |
| 140 | 141 |
| 141 bool matched = false; | 142 bool matched = false; |
| 142 | 143 |
| 143 // Remove all internal devices from |devices|. If we have an internal display, | 144 // 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(); | 169 VLOG(2) << "=> Removing internal display " << internal_display->name(); |
| 169 } | 170 } |
| 170 | 171 |
| 171 void AssociateSameSizeDevices(DisplayInfoList* displays, DeviceList* devices) { | 172 void AssociateSameSizeDevices(DisplayInfoList* displays, DeviceList* devices) { |
| 172 // Associate screens/displays with the same size. | 173 // Associate screens/displays with the same size. |
| 173 VLOG(2) << "Trying to match same-size devices (" << displays->size() | 174 VLOG(2) << "Trying to match same-size devices (" << displays->size() |
| 174 << " displays and " << devices->size() << " devices to match)"; | 175 << " displays and " << devices->size() << " devices to match)"; |
| 175 | 176 |
| 176 DisplayInfoList::iterator display_it = displays->begin(); | 177 DisplayInfoList::iterator display_it = displays->begin(); |
| 177 while (display_it != displays->end()) { | 178 while (display_it != displays->end()) { |
| 178 DisplayInfo* display = *display_it; | 179 ui::ManagedDisplayInfo* display = *display_it; |
| 179 const gfx::Size native_size = display->GetNativeModeSize(); | 180 const gfx::Size native_size = display->GetNativeModeSize(); |
| 180 | 181 |
| 181 // Try to find an input device with roughly the same size as the display. | 182 // Try to find an input device with roughly the same size as the display. |
| 182 DeviceList::iterator device_it = std::find_if( | 183 DeviceList::iterator device_it = std::find_if( |
| 183 devices->begin(), devices->end(), | 184 devices->begin(), devices->end(), |
| 184 [&native_size](const ui::TouchscreenDevice* device) { | 185 [&native_size](const ui::TouchscreenDevice* device) { |
| 185 // Allow 1 pixel difference between screen and touchscreen | 186 // Allow 1 pixel difference between screen and touchscreen |
| 186 // resolutions. Because in some cases for monitor resolution | 187 // resolutions. Because in some cases for monitor resolution |
| 187 // 1024x768 touchscreen's resolution would be 1024x768, but for | 188 // 1024x768 touchscreen's resolution would be 1024x768, but for |
| 188 // some 1023x767. It really depends on touchscreen's firmware | 189 // 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 | 213 // If there is only one display left, then we should associate all input |
| 213 // devices with it. | 214 // devices with it. |
| 214 | 215 |
| 215 VLOG(2) << "Trying to match to single display (" << displays->size() | 216 VLOG(2) << "Trying to match to single display (" << displays->size() |
| 216 << " displays and " << devices->size() << " devices to match)"; | 217 << " displays and " << devices->size() << " devices to match)"; |
| 217 | 218 |
| 218 // We only associate to one display. | 219 // We only associate to one display. |
| 219 if (displays->size() != 1 || devices->size() == 0) | 220 if (displays->size() != 1 || devices->size() == 0) |
| 220 return; | 221 return; |
| 221 | 222 |
| 222 DisplayInfo* display = *displays->begin(); | 223 ui::ManagedDisplayInfo* display = *displays->begin(); |
| 223 for (const ui::TouchscreenDevice* device : *devices) { | 224 for (const ui::TouchscreenDevice* device : *devices) { |
| 224 VLOG(2) << "=> Matched device " << device->name << " to display " | 225 VLOG(2) << "=> Matched device " << device->name << " to display " |
| 225 << display->name(); | 226 << display->name(); |
| 226 Associate(display, device); | 227 Associate(display, device); |
| 227 } | 228 } |
| 228 | 229 |
| 229 displays->clear(); | 230 displays->clear(); |
| 230 devices->clear(); | 231 devices->clear(); |
| 231 } | 232 } |
| 232 | 233 |
| 233 } // namespace | 234 } // namespace |
| 234 | 235 |
| 235 void AssociateTouchscreens( | 236 void AssociateTouchscreens( |
| 236 std::vector<DisplayInfo>* all_displays, | 237 std::vector<ui::ManagedDisplayInfo>* all_displays, |
| 237 const std::vector<ui::TouchscreenDevice>& all_devices) { | 238 const std::vector<ui::TouchscreenDevice>& all_devices) { |
| 238 // |displays| and |devices| contain pointers directly to the values stored | 239 // |displays| and |devices| contain pointers directly to the values stored |
| 239 // inside of |all_displays| and |all_devices|. When a display or input device | 240 // 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. | 241 // has been associated, it is removed from the |displays| or |devices| list. |
| 241 | 242 |
| 242 // Construct our initial set of display/devices that we will process. | 243 // Construct our initial set of display/devices that we will process. |
| 243 DisplayInfoList displays; | 244 DisplayInfoList displays; |
| 244 for (DisplayInfo& display : *all_displays) { | 245 for (ui::ManagedDisplayInfo& display : *all_displays) { |
| 245 display.ClearInputDevices(); | 246 display.ClearInputDevices(); |
| 246 | 247 |
| 247 if (display.GetNativeModeSize().IsEmpty()) { | 248 if (display.GetNativeModeSize().IsEmpty()) { |
| 248 VLOG(2) << "Will not match display " << display.id() | 249 VLOG(2) << "Will not match display " << display.id() |
| 249 << " since it doesn't have a native mode"; | 250 << " since it doesn't have a native mode"; |
| 250 continue; | 251 continue; |
| 251 } | 252 } |
| 252 displays.push_back(&display); | 253 displays.push_back(&display); |
| 253 } | 254 } |
| 254 | 255 |
| 255 // Construct initial set of devices. | 256 // Construct initial set of devices. |
| 256 DeviceList devices; | 257 DeviceList devices; |
| 257 for (const ui::TouchscreenDevice& device : all_devices) | 258 for (const ui::TouchscreenDevice& device : all_devices) |
| 258 devices.push_back(&device); | 259 devices.push_back(&device); |
| 259 | 260 |
| 260 for (const DisplayInfo* display : displays) { | 261 for (const ui::ManagedDisplayInfo* display : displays) { |
| 261 VLOG(2) << "Received display " << display->name() | 262 VLOG(2) << "Received display " << display->name() |
| 262 << " (size: " << display->GetNativeModeSize().ToString() | 263 << " (size: " << display->GetNativeModeSize().ToString() |
| 263 << ", sys_path: " << display->sys_path().LossyDisplayName() << ")"; | 264 << ", sys_path: " << display->sys_path().LossyDisplayName() << ")"; |
| 264 } | 265 } |
| 265 for (const ui::TouchscreenDevice* device : devices) { | 266 for (const ui::TouchscreenDevice* device : devices) { |
| 266 VLOG(2) << "Received device " << device->name | 267 VLOG(2) << "Received device " << device->name |
| 267 << " (size: " << device->size.ToString() | 268 << " (size: " << device->size.ToString() |
| 268 << ", sys_path: " << device->sys_path.LossyDisplayName() << ")"; | 269 << ", sys_path: " << device->sys_path.LossyDisplayName() << ")"; |
| 269 } | 270 } |
| 270 | 271 |
| 271 AssociateInternalDevices(&displays, &devices); | 272 AssociateInternalDevices(&displays, &devices); |
| 272 AssociateUdlDevices(&displays, &devices); | 273 AssociateUdlDevices(&displays, &devices); |
| 273 AssociateSameSizeDevices(&displays, &devices); | 274 AssociateSameSizeDevices(&displays, &devices); |
| 274 AssociateToSingleDisplay(&displays, &devices); | 275 AssociateToSingleDisplay(&displays, &devices); |
| 275 | 276 |
| 276 for (const DisplayInfo* display : displays) | 277 for (const ui::ManagedDisplayInfo* display : displays) |
| 277 LOG(WARNING) << "Unmatched display " << display->name(); | 278 LOG(WARNING) << "Unmatched display " << display->name(); |
| 278 for (const ui::TouchscreenDevice* device : devices) | 279 for (const ui::TouchscreenDevice* device : devices) |
| 279 LOG(WARNING) << "Unmatched device " << device->name; | 280 LOG(WARNING) << "Unmatched device " << device->name; |
| 280 } | 281 } |
| 281 | 282 |
| 282 } // namespace ash | 283 } // namespace ash |
| OLD | NEW |