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