Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / gamepad / gamepad_platform_data_fetcher_linux.cc
blob26f2913a877fe71ec376388e5c7657834551443b
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"
7 #include <fcntl.h>
8 #include <linux/joystick.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
14 #include "base/message_loop/message_loop.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/trace_event/trace_event.h"
21 #include "content/browser/udev_linux.h"
22 #include "device/udev_linux/scoped_udev.h"
24 namespace {
26 const char kInputSubsystem[] = "input";
27 const char kUsbSubsystem[] = "usb";
28 const char kUsbDeviceType[] = "usb_device";
29 const float kMaxLinuxAxisValue = 32767.0;
31 void CloseFileDescriptorIfValid(int fd) {
32 if (fd >= 0)
33 close(fd);
36 bool IsGamepad(udev_device* dev, int* index, std::string* path) {
37 if (!device::udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
38 return false;
40 const char* node_path = device::udev_device_get_devnode(dev);
41 if (!node_path)
42 return false;
44 static const char kJoystickRoot[] = "/dev/input/js";
45 bool is_gamepad = StartsWithASCII(node_path, kJoystickRoot, true);
46 if (!is_gamepad)
47 return false;
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>(blink::WebGamepads::itemsLengthCap)) {
56 return false;
58 *index = tmp_idx;
59 *path = node_path;
60 return true;
63 } // namespace
65 namespace content {
67 using blink::WebGamepad;
68 using blink::WebGamepads;
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_));
75 std::vector<UdevLinux::UdevMonitorFilter> filters;
76 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL));
77 udev_.reset(
78 new UdevLinux(filters,
79 base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice,
80 base::Unretained(this))));
82 EnumerateDevices();
85 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() {
86 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i)
87 CloseFileDescriptorIfValid(device_fds_[i]);
90 void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) {
91 TRACE_EVENT0("GAMEPAD", "GetGamepadData");
93 data_.length = WebGamepads::itemsLengthCap;
95 // Update our internal state.
96 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
97 if (device_fds_[i] >= 0) {
98 ReadDeviceData(i);
102 // Copy to the current state to the output buffer, using the mapping
103 // function, if there is one available.
104 pads->length = data_.length;
105 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
106 if (mappers_[i])
107 mappers_[i](data_.items[i], &pads->items[i]);
108 else
109 pads->items[i] = data_.items[i];
113 // Used during enumeration, and monitor notifications.
114 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
115 int index;
116 std::string node_path;
117 if (IsGamepad(dev, &index, &node_path)) {
118 int& device_fd = device_fds_[index];
119 WebGamepad& pad = data_.items[index];
120 GamepadStandardMappingFunction& mapper = mappers_[index];
122 CloseFileDescriptorIfValid(device_fd);
124 // The device pointed to by dev contains information about the logical
125 // 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 // This function should just walk up the tree one level.
128 dev = device::udev_device_get_parent_with_subsystem_devtype(
129 dev, kInputSubsystem, NULL);
130 if (!dev) {
131 // Unable to get device information, don't use this device.
132 device_fd = -1;
133 pad.connected = false;
134 return;
137 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK));
138 if (device_fd < 0) {
139 // Unable to open device, don't use.
140 pad.connected = false;
141 return;
144 const char* vendor_id =
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");
148 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id);
150 // 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.
152 const char* name = device::udev_device_get_sysattr_value(dev, "name");
153 std::string name_string = base::StringPrintf("%s", name);
155 // 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
157 // to the subsystem/device type "usb"/"usb_device" and if this device
158 // has the same vendor/product id, prefer the description from that.
159 struct udev_device* usb_dev =
160 device::udev_device_get_parent_with_subsystem_devtype(
161 dev, kUsbSubsystem, kUsbDeviceType);
162 if (usb_dev) {
163 const char* usb_vendor_id =
164 device::udev_device_get_sysattr_value(usb_dev, "idVendor");
165 const char* usb_product_id =
166 device::udev_device_get_sysattr_value(usb_dev, "idProduct");
168 if (strcmp(vendor_id, usb_vendor_id) == 0 &&
169 strcmp(product_id, usb_product_id) == 0) {
170 const char* manufacturer =
171 device::udev_device_get_sysattr_value(usb_dev, "manufacturer");
172 const char* product =
173 device::udev_device_get_sysattr_value(usb_dev, "product");
175 // Replace the previous name string with one containing the better
176 // information, again driver returns utf-8 strings here so combine
177 // in utf-8 for conversion to WebUChar below.
178 name_string = base::StringPrintf("%s %s", manufacturer, product);
182 // Append the vendor and product information then convert the utf-8
183 // id string to WebUChar.
184 std::string id =
185 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)",
186 mapper ? "STANDARD GAMEPAD " : "",
187 vendor_id,
188 product_id);
189 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id);
190 base::string16 tmp16 = base::UTF8ToUTF16(id);
191 memset(pad.id, 0, sizeof(pad.id));
192 tmp16.copy(pad.id, arraysize(pad.id) - 1);
194 if (mapper) {
195 std::string mapping = "standard";
196 base::TruncateUTF8ToByteSize(
197 mapping, WebGamepad::mappingLengthCap - 1, &mapping);
198 tmp16 = base::UTF8ToUTF16(mapping);
199 memset(pad.mapping, 0, sizeof(pad.mapping));
200 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1);
201 } else {
202 pad.mapping[0] = 0;
205 pad.connected = true;
209 void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
210 device::ScopedUdevEnumeratePtr enumerate(
211 device::udev_enumerate_new(udev_->udev_handle()));
212 if (!enumerate)
213 return;
214 int ret = device::udev_enumerate_add_match_subsystem(enumerate.get(),
215 kInputSubsystem);
216 if (ret != 0)
217 return;
218 ret = device::udev_enumerate_scan_devices(enumerate.get());
219 if (ret != 0)
220 return;
222 udev_list_entry* devices =
223 device::udev_enumerate_get_list_entry(enumerate.get());
224 for (udev_list_entry* dev_list_entry = devices; dev_list_entry != NULL;
225 dev_list_entry = device::udev_list_entry_get_next(dev_list_entry)) {
226 // Get the filename of the /sys entry for the device and create a
227 // udev_device object (dev) representing it
228 const char* path = device::udev_list_entry_get_name(dev_list_entry);
229 device::ScopedUdevDevicePtr dev(
230 device::udev_device_new_from_syspath(udev_->udev_handle(), path));
231 if (!dev)
232 continue;
233 RefreshDevice(dev.get());
237 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
238 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/
239 if (index >= WebGamepads::itemsLengthCap) {
240 CHECK(false);
241 return;
244 const int& fd = device_fds_[index];
245 WebGamepad& pad = data_.items[index];
246 DCHECK_GE(fd, 0);
248 js_event event;
249 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) {
250 size_t item = event.number;
251 if (event.type & JS_EVENT_AXIS) {
252 if (item >= WebGamepad::axesLengthCap)
253 continue;
254 pad.axes[item] = event.value / kMaxLinuxAxisValue;
255 if (item >= pad.axesLength)
256 pad.axesLength = item + 1;
257 } else if (event.type & JS_EVENT_BUTTON) {
258 if (item >= WebGamepad::buttonsLengthCap)
259 continue;
260 pad.buttons[item].pressed = event.value;
261 pad.buttons[item].value = event.value ? 1.0 : 0.0;
262 if (item >= pad.buttonsLength)
263 pad.buttonsLength = item + 1;
265 pad.timestamp = event.time;
269 } // namespace content