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