| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/gamepad/gamepad_platform_data_fetcher_linux.h" | 5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_linux.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <libudev.h> | 8 #include <libudev.h> |
| 9 #include <linux/joystick.h> | 9 #include <linux/joystick.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <unistd.h> | 13 #include <unistd.h> |
| 14 | 14 |
| 15 #include "base/debug/trace_event.h" | 15 #include "base/debug/trace_event.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "content/browser/udev_linux.h" | 22 #include "content/browser/udev_linux.h" |
| 23 #include "device/udev_linux/scoped_udev.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 const char kInputSubsystem[] = "input"; | 27 const char kInputSubsystem[] = "input"; |
| 27 const char kUsbSubsystem[] = "usb"; | 28 const char kUsbSubsystem[] = "usb"; |
| 28 const char kUsbDeviceType[] = "usb_device"; | 29 const char kUsbDeviceType[] = "usb_device"; |
| 29 const float kMaxLinuxAxisValue = 32767.0; | 30 const float kMaxLinuxAxisValue = 32767.0; |
| 30 | 31 |
| 31 void CloseFileDescriptorIfValid(int fd) { | 32 void CloseFileDescriptorIfValid(int fd) { |
| 32 if (fd >= 0) | 33 if (fd >= 0) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 WebGamepad& pad = data_.items[index]; | 120 WebGamepad& pad = data_.items[index]; |
| 120 GamepadStandardMappingFunction& mapper = mappers_[index]; | 121 GamepadStandardMappingFunction& mapper = mappers_[index]; |
| 121 | 122 |
| 122 CloseFileDescriptorIfValid(device_fd); | 123 CloseFileDescriptorIfValid(device_fd); |
| 123 | 124 |
| 124 // The device pointed to by dev contains information about the logical | 125 // The device pointed to by dev contains information about the logical |
| 125 // joystick device. In order to get the information about the physical | 126 // joystick device. In order to get the information about the physical |
| 126 // hardware, get the parent device that is also in the "input" subsystem. | 127 // hardware, get the parent device that is also in the "input" subsystem. |
| 127 // This function should just walk up the tree one level. | 128 // This function should just walk up the tree one level. |
| 128 dev = udev_device_get_parent_with_subsystem_devtype( | 129 dev = udev_device_get_parent_with_subsystem_devtype( |
| 129 dev, | 130 dev, kInputSubsystem, NULL); |
| 130 kInputSubsystem, | |
| 131 NULL); | |
| 132 if (!dev) { | 131 if (!dev) { |
| 133 // Unable to get device information, don't use this device. | 132 // Unable to get device information, don't use this device. |
| 134 device_fd = -1; | 133 device_fd = -1; |
| 135 pad.connected = false; | 134 pad.connected = false; |
| 136 return; | 135 return; |
| 137 } | 136 } |
| 138 | 137 |
| 139 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); | 138 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); |
| 140 if (device_fd < 0) { | 139 if (device_fd < 0) { |
| 141 // Unable to open device, don't use. | 140 // Unable to open device, don't use. |
| 142 pad.connected = false; | 141 pad.connected = false; |
| 143 return; | 142 return; |
| 144 } | 143 } |
| 145 | 144 |
| 146 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); | 145 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); |
| 147 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); | 146 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); |
| 148 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); | 147 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); |
| 149 | 148 |
| 150 // Driver returns utf-8 strings here, so combine in utf-8 first and | 149 // Driver returns utf-8 strings here, so combine in utf-8 first and |
| 151 // convert to WebUChar later once we've picked an id string. | 150 // convert to WebUChar later once we've picked an id string. |
| 152 const char* name = udev_device_get_sysattr_value(dev, "name"); | 151 const char* name = udev_device_get_sysattr_value(dev, "name"); |
| 153 std::string name_string = base::StringPrintf("%s", name); | 152 std::string name_string = base::StringPrintf("%s", name); |
| 154 | 153 |
| 155 // In many cases the information the input subsystem contains isn't | 154 // In many cases the information the input subsystem contains isn't |
| 156 // as good as the information that the device bus has, walk up further | 155 // as good as the information that the device bus has, walk up further |
| 157 // to the subsystem/device type "usb"/"usb_device" and if this device | 156 // to the subsystem/device type "usb"/"usb_device" and if this device |
| 158 // has the same vendor/product id, prefer the description from that. | 157 // has the same vendor/product id, prefer the description from that. |
| 159 struct udev_device *usb_dev = udev_device_get_parent_with_subsystem_devtype( | 158 struct udev_device* usb_dev = udev_device_get_parent_with_subsystem_devtype( |
| 160 dev, | 159 dev, kUsbSubsystem, kUsbDeviceType); |
| 161 kUsbSubsystem, | |
| 162 kUsbDeviceType); | |
| 163 if (usb_dev) { | 160 if (usb_dev) { |
| 164 const char* usb_vendor_id = | 161 const char* usb_vendor_id = |
| 165 udev_device_get_sysattr_value(usb_dev, "idVendor"); | 162 udev_device_get_sysattr_value(usb_dev, "idVendor"); |
| 166 const char* usb_product_id = | 163 const char* usb_product_id = |
| 167 udev_device_get_sysattr_value(usb_dev, "idProduct"); | 164 udev_device_get_sysattr_value(usb_dev, "idProduct"); |
| 168 | 165 |
| 169 if (strcmp(vendor_id, usb_vendor_id) == 0 && | 166 if (strcmp(vendor_id, usb_vendor_id) == 0 && |
| 170 strcmp(product_id, usb_product_id) == 0) { | 167 strcmp(product_id, usb_product_id) == 0) { |
| 171 const char* manufacturer = | 168 const char* manufacturer = |
| 172 udev_device_get_sysattr_value(usb_dev, "manufacturer"); | 169 udev_device_get_sysattr_value(usb_dev, "manufacturer"); |
| 173 const char* product = udev_device_get_sysattr_value(usb_dev, "product"); | 170 const char* product = udev_device_get_sysattr_value(usb_dev, "product"); |
| 174 | 171 |
| 175 // Replace the previous name string with one containing the better | 172 // Replace the previous name string with one containing the better |
| 176 // information, again driver returns utf-8 strings here so combine | 173 // information, again driver returns utf-8 strings here so combine |
| 177 // in utf-8 for conversion to WebUChar below. | 174 // in utf-8 for conversion to WebUChar below. |
| 178 name_string = base::StringPrintf("%s %s", manufacturer, product); | 175 name_string = base::StringPrintf("%s %s", manufacturer, product); |
| 179 } | 176 } |
| 180 } | 177 } |
| 181 | 178 |
| 182 // Append the vendor and product information then convert the utf-8 | 179 // Append the vendor and product information then convert the utf-8 |
| 183 // id string to WebUChar. | 180 // id string to WebUChar. |
| 184 std::string id = name_string + base::StringPrintf( | 181 std::string id = |
| 185 " (%sVendor: %s Product: %s)", | 182 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)", |
| 186 mapper ? "STANDARD GAMEPAD " : "", | 183 mapper ? "STANDARD GAMEPAD " : "", |
| 187 vendor_id, | 184 vendor_id, |
| 188 product_id); | 185 product_id); |
| 189 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); | 186 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); |
| 190 base::string16 tmp16 = base::UTF8ToUTF16(id); | 187 base::string16 tmp16 = base::UTF8ToUTF16(id); |
| 191 memset(pad.id, 0, sizeof(pad.id)); | 188 memset(pad.id, 0, sizeof(pad.id)); |
| 192 tmp16.copy(pad.id, arraysize(pad.id) - 1); | 189 tmp16.copy(pad.id, arraysize(pad.id) - 1); |
| 193 | 190 |
| 194 if (mapper) { | 191 if (mapper) { |
| 195 std::string mapping = "standard"; | 192 std::string mapping = "standard"; |
| 196 base::TruncateUTF8ToByteSize(mapping, WebGamepad::mappingLengthCap - 1, | 193 base::TruncateUTF8ToByteSize( |
| 197 &mapping); | 194 mapping, WebGamepad::mappingLengthCap - 1, &mapping); |
| 198 tmp16 = base::UTF8ToUTF16(mapping); | 195 tmp16 = base::UTF8ToUTF16(mapping); |
| 199 memset(pad.mapping, 0, sizeof(pad.mapping)); | 196 memset(pad.mapping, 0, sizeof(pad.mapping)); |
| 200 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); | 197 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); |
| 201 } else { | 198 } else { |
| 202 pad.mapping[0] = 0; | 199 pad.mapping[0] = 0; |
| 203 } | 200 } |
| 204 | 201 |
| 205 pad.connected = true; | 202 pad.connected = true; |
| 206 } | 203 } |
| 207 } | 204 } |
| 208 | 205 |
| 209 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { | 206 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { |
| 210 udev_enumerate* enumerate = udev_enumerate_new(udev_->udev_handle()); | 207 device::ScopedUdevEnumeratePtr enumerate( |
| 208 udev_enumerate_new(udev_->udev_handle())); |
| 211 if (!enumerate) | 209 if (!enumerate) |
| 212 return; | 210 return; |
| 213 int ret = udev_enumerate_add_match_subsystem(enumerate, kInputSubsystem); | 211 int ret = |
| 212 udev_enumerate_add_match_subsystem(enumerate.get(), kInputSubsystem); |
| 214 if (ret != 0) | 213 if (ret != 0) |
| 215 return; | 214 return; |
| 216 ret = udev_enumerate_scan_devices(enumerate); | 215 ret = udev_enumerate_scan_devices(enumerate.get()); |
| 217 if (ret != 0) | 216 if (ret != 0) |
| 218 return; | 217 return; |
| 219 | 218 |
| 220 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | 219 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); |
| 221 for (udev_list_entry* dev_list_entry = devices; | 220 for (udev_list_entry* dev_list_entry = devices; dev_list_entry != NULL; |
| 222 dev_list_entry != NULL; | |
| 223 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { | 221 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { |
| 224 // Get the filename of the /sys entry for the device and create a | 222 // Get the filename of the /sys entry for the device and create a |
| 225 // udev_device object (dev) representing it | 223 // udev_device object (dev) representing it |
| 226 const char* path = udev_list_entry_get_name(dev_list_entry); | 224 const char* path = udev_list_entry_get_name(dev_list_entry); |
| 227 udev_device* dev = udev_device_new_from_syspath(udev_->udev_handle(), path); | 225 device::ScopedUdevDevicePtr dev( |
| 226 udev_device_new_from_syspath(udev_->udev_handle(), path)); |
| 228 if (!dev) | 227 if (!dev) |
| 229 continue; | 228 continue; |
| 230 RefreshDevice(dev); | 229 RefreshDevice(dev.get()); |
| 231 udev_device_unref(dev); | |
| 232 } | 230 } |
| 233 // Free the enumerator object | |
| 234 udev_enumerate_unref(enumerate); | |
| 235 } | 231 } |
| 236 | 232 |
| 237 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { | 233 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { |
| 238 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ | 234 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ |
| 239 if (index >= WebGamepads::itemsLengthCap) { | 235 if (index >= WebGamepads::itemsLengthCap) { |
| 240 CHECK(false); | 236 CHECK(false); |
| 241 return; | 237 return; |
| 242 } | 238 } |
| 243 | 239 |
| 244 const int& fd = device_fds_[index]; | 240 const int& fd = device_fds_[index]; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 260 pad.buttons[item].pressed = event.value; | 256 pad.buttons[item].pressed = event.value; |
| 261 pad.buttons[item].value = event.value ? 1.0 : 0.0; | 257 pad.buttons[item].value = event.value ? 1.0 : 0.0; |
| 262 if (item >= pad.buttonsLength) | 258 if (item >= pad.buttonsLength) |
| 263 pad.buttonsLength = item + 1; | 259 pad.buttonsLength = item + 1; |
| 264 } | 260 } |
| 265 pad.timestamp = event.time; | 261 pad.timestamp = event.time; |
| 266 } | 262 } |
| 267 } | 263 } |
| 268 | 264 |
| 269 } // namespace content | 265 } // namespace content |
| OLD | NEW |