| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/gamepad/platform_data_fetcher_linux.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <libudev.h> | |
| 9 #include <linux/joystick.h> | |
| 10 #include <string.h> | |
| 11 #include <sys/stat.h> | |
| 12 #include <sys/types.h> | |
| 13 #include <unistd.h> | |
| 14 | |
| 15 #include "base/debug/trace_event.h" | |
| 16 #include "base/eintr_wrapper.h" | |
| 17 #include "base/message_loop.h" | |
| 18 #include "base/string_number_conversions.h" | |
| 19 #include "base/string_util.h" | |
| 20 #include "base/stringprintf.h" | |
| 21 #include "base/utf_string_conversions.h" | |
| 22 #include "content/browser/udev_linux.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char kInputSubsystem[] = "input"; | |
| 27 const char kUsbSubsystem[] = "usb"; | |
| 28 const char kUsbDeviceType[] = "usb_device"; | |
| 29 const float kMaxLinuxAxisValue = 32767.0; | |
| 30 | |
| 31 void CloseFileDescriptorIfValid(int fd) { | |
| 32 if (fd >= 0) | |
| 33 close(fd); | |
| 34 } | |
| 35 | |
| 36 bool IsGamepad(udev_device* dev, int* index, std::string* path) { | |
| 37 if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK")) | |
| 38 return false; | |
| 39 | |
| 40 const char* node_path = udev_device_get_devnode(dev); | |
| 41 if (!node_path) | |
| 42 return false; | |
| 43 | |
| 44 static const char kJoystickRoot[] = "/dev/input/js"; | |
| 45 bool is_gamepad = StartsWithASCII(node_path, kJoystickRoot, true); | |
| 46 if (!is_gamepad) | |
| 47 return false; | |
| 48 | |
| 49 int tmp_idx = -1; | |
| 50 const int base_len = sizeof(kJoystickRoot) - 1; | |
| 51 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len); | |
| 52 if (!base::StringToInt(str, &tmp_idx)) | |
| 53 return false; | |
| 54 if (tmp_idx < 0 || | |
| 55 tmp_idx >= static_cast<int>(WebKit::WebGamepads::itemsLengthCap)) { | |
| 56 return false; | |
| 57 } | |
| 58 *index = tmp_idx; | |
| 59 *path = node_path; | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 namespace content { | |
| 66 | |
| 67 using WebKit::WebGamepad; | |
| 68 using WebKit::WebGamepads; | |
| 69 | |
| 70 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { | |
| 71 for (size_t i = 0; i < arraysize(device_fds_); ++i) | |
| 72 device_fds_[i] = -1; | |
| 73 memset(mappers_, 0, sizeof(mappers_)); | |
| 74 | |
| 75 std::vector<UdevLinux::UdevMonitorFilter> filters; | |
| 76 filters.push_back( | |
| 77 content::UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); | |
| 78 udev_.reset( | |
| 79 new UdevLinux(filters, | |
| 80 base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, | |
| 81 base::Unretained(this)))); | |
| 82 | |
| 83 EnumerateDevices(); | |
| 84 } | |
| 85 | |
| 86 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { | |
| 87 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) | |
| 88 CloseFileDescriptorIfValid(device_fds_[i]); | |
| 89 } | |
| 90 | |
| 91 void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) { | |
| 92 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | |
| 93 | |
| 94 data_.length = WebGamepads::itemsLengthCap; | |
| 95 | |
| 96 // Update our internal state. | |
| 97 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { | |
| 98 if (device_fds_[i] >= 0) { | |
| 99 ReadDeviceData(i); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 // Copy to the current state to the output buffer, using the mapping | |
| 104 // function, if there is one available. | |
| 105 pads->length = data_.length; | |
| 106 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { | |
| 107 if (mappers_[i]) | |
| 108 mappers_[i](data_.items[i], &pads->items[i]); | |
| 109 else | |
| 110 pads->items[i] = data_.items[i]; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Used during enumeration, and monitor notifications. | |
| 115 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { | |
| 116 int index; | |
| 117 std::string node_path; | |
| 118 if (IsGamepad(dev, &index, &node_path)) { | |
| 119 int& device_fd = device_fds_[index]; | |
| 120 WebGamepad& pad = data_.items[index]; | |
| 121 GamepadStandardMappingFunction& mapper = mappers_[index]; | |
| 122 | |
| 123 CloseFileDescriptorIfValid(device_fd); | |
| 124 | |
| 125 // The device pointed to by dev contains information about the logical | |
| 126 // joystick device. In order to get the information about the physical | |
| 127 // hardware, get the parent device that is also in the "input" subsystem. | |
| 128 // This function should just walk up the tree one level. | |
| 129 dev = udev_device_get_parent_with_subsystem_devtype( | |
| 130 dev, | |
| 131 kInputSubsystem, | |
| 132 NULL); | |
| 133 if (!dev) { | |
| 134 // Unable to get device information, don't use this device. | |
| 135 device_fd = -1; | |
| 136 pad.connected = false; | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); | |
| 141 if (device_fd < 0) { | |
| 142 // Unable to open device, don't use. | |
| 143 pad.connected = false; | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); | |
| 148 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); | |
| 149 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); | |
| 150 | |
| 151 // Driver returns utf-8 strings here, so combine in utf-8 first and | |
| 152 // convert to WebUChar later once we've picked an id string. | |
| 153 const char* name = udev_device_get_sysattr_value(dev, "name"); | |
| 154 std::string name_string = base::StringPrintf("%s", name); | |
| 155 | |
| 156 // In many cases the information the input subsystem contains isn't | |
| 157 // as good as the information that the device bus has, walk up further | |
| 158 // to the subsystem/device type "usb"/"usb_device" and if this device | |
| 159 // has the same vendor/product id, prefer the description from that. | |
| 160 struct udev_device *usb_dev = udev_device_get_parent_with_subsystem_devtype( | |
| 161 dev, | |
| 162 kUsbSubsystem, | |
| 163 kUsbDeviceType); | |
| 164 if (usb_dev) { | |
| 165 const char* usb_vendor_id = | |
| 166 udev_device_get_sysattr_value(usb_dev, "idVendor"); | |
| 167 const char* usb_product_id = | |
| 168 udev_device_get_sysattr_value(usb_dev, "idProduct"); | |
| 169 | |
| 170 if (strcmp(vendor_id, usb_vendor_id) == 0 && | |
| 171 strcmp(product_id, usb_product_id) == 0) { | |
| 172 const char* manufacturer = | |
| 173 udev_device_get_sysattr_value(usb_dev, "manufacturer"); | |
| 174 const char* product = udev_device_get_sysattr_value(usb_dev, "product"); | |
| 175 | |
| 176 // Replace the previous name string with one containing the better | |
| 177 // information, again driver returns utf-8 strings here so combine | |
| 178 // in utf-8 for conversion to WebUChar below. | |
| 179 name_string = base::StringPrintf("%s %s", manufacturer, product); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 // Append the vendor and product information then convert the utf-8 | |
| 184 // id string to WebUChar. | |
| 185 std::string id = name_string + base::StringPrintf( | |
| 186 " (%sVendor: %s Product: %s)", | |
| 187 mapper ? "STANDARD GAMEPAD " : "", | |
| 188 vendor_id, | |
| 189 product_id); | |
| 190 TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); | |
| 191 string16 tmp16 = UTF8ToUTF16(id); | |
| 192 memset(pad.id, 0, sizeof(pad.id)); | |
| 193 tmp16.copy(pad.id, arraysize(pad.id) - 1); | |
| 194 | |
| 195 pad.connected = true; | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { | |
| 200 udev_enumerate* enumerate = udev_enumerate_new(udev_->udev_handle()); | |
| 201 if (!enumerate) | |
| 202 return; | |
| 203 int ret = udev_enumerate_add_match_subsystem(enumerate, kInputSubsystem); | |
| 204 if (ret != 0) | |
| 205 return; | |
| 206 ret = udev_enumerate_scan_devices(enumerate); | |
| 207 if (ret != 0) | |
| 208 return; | |
| 209 | |
| 210 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
| 211 for (udev_list_entry* dev_list_entry = devices; | |
| 212 dev_list_entry != NULL; | |
| 213 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { | |
| 214 // Get the filename of the /sys entry for the device and create a | |
| 215 // udev_device object (dev) representing it | |
| 216 const char* path = udev_list_entry_get_name(dev_list_entry); | |
| 217 udev_device* dev = udev_device_new_from_syspath(udev_->udev_handle(), path); | |
| 218 if (!dev) | |
| 219 continue; | |
| 220 RefreshDevice(dev); | |
| 221 udev_device_unref(dev); | |
| 222 } | |
| 223 // Free the enumerator object | |
| 224 udev_enumerate_unref(enumerate); | |
| 225 } | |
| 226 | |
| 227 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { | |
| 228 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ | |
| 229 if (index >= WebGamepads::itemsLengthCap) { | |
| 230 CHECK(false); | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 const int& fd = device_fds_[index]; | |
| 235 WebGamepad& pad = data_.items[index]; | |
| 236 DCHECK_GE(fd, 0); | |
| 237 | |
| 238 js_event event; | |
| 239 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event)) > 0)) { | |
| 240 size_t item = event.number; | |
| 241 if (event.type & JS_EVENT_AXIS) { | |
| 242 if (item >= WebGamepad::axesLengthCap) | |
| 243 continue; | |
| 244 pad.axes[item] = event.value / kMaxLinuxAxisValue; | |
| 245 if (item >= pad.axesLength) | |
| 246 pad.axesLength = item + 1; | |
| 247 } else if (event.type & JS_EVENT_BUTTON) { | |
| 248 if (item >= WebGamepad::buttonsLengthCap) | |
| 249 continue; | |
| 250 pad.buttons[item] = event.value ? 1.0 : 0.0; | |
| 251 if (item >= pad.buttonsLength) | |
| 252 pad.buttonsLength = item + 1; | |
| 253 } | |
| 254 pad.timestamp = event.time; | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 } // namespace content | |
| OLD | NEW |