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.
5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_linux.h"
9 #include <linux/joystick.h>
12 #include <sys/types.h>
15 #include "base/debug/trace_event.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/posix/eintr_wrapper.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "content/browser/udev_linux.h"
23 #include "device/udev_linux/scoped_udev.h"
27 const char kInputSubsystem[] = "input";
28 const char kUsbSubsystem[] = "usb";
29 const char kUsbDeviceType[] = "usb_device";
30 const float kMaxLinuxAxisValue = 32767.0;
32 void CloseFileDescriptorIfValid(int fd) {
37 bool IsGamepad(udev_device* dev, int* index, std::string* path) {
38 if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
41 const char* node_path = udev_device_get_devnode(dev);
45 static const char kJoystickRoot[] = "/dev/input/js";
46 bool is_gamepad = StartsWithASCII(node_path, kJoystickRoot, true);
51 const int base_len = sizeof(kJoystickRoot) - 1;
52 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len);
53 if (!base::StringToInt(str, &tmp_idx))
56 tmp_idx >= static_cast<int>(blink::WebGamepads::itemsLengthCap)) {
68 using blink::WebGamepad;
69 using blink::WebGamepads;
71 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() {
72 for (size_t i = 0; i < arraysize(device_fds_); ++i)
74 memset(mappers_, 0, sizeof(mappers_));
76 std::vector<UdevLinux::UdevMonitorFilter> filters;
77 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL));
79 new UdevLinux(filters,
80 base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice,
81 base::Unretained(this))));
86 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() {
87 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i)
88 CloseFileDescriptorIfValid(device_fds_[i]);
91 void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) {
92 TRACE_EVENT0("GAMEPAD", "GetGamepadData");
94 data_.length = WebGamepads::itemsLengthCap;
96 // Update our internal state.
97 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
98 if (device_fds_[i] >= 0) {
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) {
108 mappers_[i](data_.items[i], &pads->items[i]);
110 pads->items[i] = data_.items[i];
114 // Used during enumeration, and monitor notifications.
115 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
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];
123 CloseFileDescriptorIfValid(device_fd);
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, kInputSubsystem, NULL);
132 // Unable to get device information, don't use this device.
134 pad.connected = false;
138 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK));
140 // Unable to open device, don't use.
141 pad.connected = false;
145 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor");
146 const char* product_id = udev_device_get_sysattr_value(dev, "id/product");
147 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id);
149 // 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 const char* name = udev_device_get_sysattr_value(dev, "name");
152 std::string name_string = base::StringPrintf("%s", name);
154 // 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 // 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 struct udev_device* usb_dev = udev_device_get_parent_with_subsystem_devtype(
159 dev, kUsbSubsystem, kUsbDeviceType);
161 const char* usb_vendor_id =
162 udev_device_get_sysattr_value(usb_dev, "idVendor");
163 const char* usb_product_id =
164 udev_device_get_sysattr_value(usb_dev, "idProduct");
166 if (strcmp(vendor_id, usb_vendor_id) == 0 &&
167 strcmp(product_id, usb_product_id) == 0) {
168 const char* manufacturer =
169 udev_device_get_sysattr_value(usb_dev, "manufacturer");
170 const char* product = udev_device_get_sysattr_value(usb_dev, "product");
172 // Replace the previous name string with one containing the better
173 // information, again driver returns utf-8 strings here so combine
174 // in utf-8 for conversion to WebUChar below.
175 name_string = base::StringPrintf("%s %s", manufacturer, product);
179 // Append the vendor and product information then convert the utf-8
180 // id string to WebUChar.
182 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)",
183 mapper ? "STANDARD GAMEPAD " : "",
186 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id);
187 base::string16 tmp16 = base::UTF8ToUTF16(id);
188 memset(pad.id, 0, sizeof(pad.id));
189 tmp16.copy(pad.id, arraysize(pad.id) - 1);
192 std::string mapping = "standard";
193 base::TruncateUTF8ToByteSize(
194 mapping, WebGamepad::mappingLengthCap - 1, &mapping);
195 tmp16 = base::UTF8ToUTF16(mapping);
196 memset(pad.mapping, 0, sizeof(pad.mapping));
197 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1);
202 pad.connected = true;
206 void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
207 device::ScopedUdevEnumeratePtr enumerate(
208 udev_enumerate_new(udev_->udev_handle()));
212 udev_enumerate_add_match_subsystem(enumerate.get(), kInputSubsystem);
215 ret = udev_enumerate_scan_devices(enumerate.get());
219 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get());
220 for (udev_list_entry* dev_list_entry = devices; dev_list_entry != NULL;
221 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) {
222 // Get the filename of the /sys entry for the device and create a
223 // udev_device object (dev) representing it
224 const char* path = udev_list_entry_get_name(dev_list_entry);
225 device::ScopedUdevDevicePtr dev(
226 udev_device_new_from_syspath(udev_->udev_handle(), path));
229 RefreshDevice(dev.get());
233 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
234 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/
235 if (index >= WebGamepads::itemsLengthCap) {
240 const int& fd = device_fds_[index];
241 WebGamepad& pad = data_.items[index];
245 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) {
246 size_t item = event.number;
247 if (event.type & JS_EVENT_AXIS) {
248 if (item >= WebGamepad::axesLengthCap)
250 pad.axes[item] = event.value / kMaxLinuxAxisValue;
251 if (item >= pad.axesLength)
252 pad.axesLength = item + 1;
253 } else if (event.type & JS_EVENT_BUTTON) {
254 if (item >= WebGamepad::buttonsLengthCap)
256 pad.buttons[item].pressed = event.value;
257 pad.buttons[item].value = event.value ? 1.0 : 0.0;
258 if (item >= pad.buttonsLength)
259 pad.buttonsLength = item + 1;
261 pad.timestamp = event.time;
265 } // namespace content