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"
8 #include <linux/joystick.h>
11 #include <sys/types.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"
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
) {
36 bool IsGamepad(udev_device
* dev
, int* index
, std::string
* path
) {
37 if (!device::udev_device_get_property_value(dev
, "ID_INPUT_JOYSTICK"))
40 const char* node_path
= device::udev_device_get_devnode(dev
);
44 static const char kJoystickRoot
[] = "/dev/input/js";
45 bool is_gamepad
= base::StartsWith(node_path
, kJoystickRoot
,
46 base::CompareCase::SENSITIVE
);
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
= device::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
=
146 device::udev_device_get_sysattr_value(dev
, "id/vendor");
147 const char* product_id
=
148 device::udev_device_get_sysattr_value(dev
, "id/product");
149 mapper
= GetGamepadStandardMappingFunction(vendor_id
, product_id
);
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
= device::udev_device_get_sysattr_value(dev
, "name");
154 std::string
name_string(name
);
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
=
161 device::udev_device_get_parent_with_subsystem_devtype(
162 dev
, kUsbSubsystem
, kUsbDeviceType
);
164 const char* usb_vendor_id
=
165 device::udev_device_get_sysattr_value(usb_dev
, "idVendor");
166 const char* usb_product_id
=
167 device::udev_device_get_sysattr_value(usb_dev
, "idProduct");
169 if (strcmp(vendor_id
, usb_vendor_id
) == 0 &&
170 strcmp(product_id
, usb_product_id
) == 0) {
171 const char* manufacturer
=
172 device::udev_device_get_sysattr_value(usb_dev
, "manufacturer");
173 const char* product
=
174 device::udev_device_get_sysattr_value(usb_dev
, "product");
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
);
183 // Append the vendor and product information then convert the utf-8
184 // id string to WebUChar.
186 name_string
+ base::StringPrintf(" (%sVendor: %s Product: %s)",
187 mapper
? "STANDARD GAMEPAD " : "",
190 base::TruncateUTF8ToByteSize(id
, WebGamepad::idLengthCap
- 1, &id
);
191 base::string16 tmp16
= base::UTF8ToUTF16(id
);
192 memset(pad
.id
, 0, sizeof(pad
.id
));
193 tmp16
.copy(pad
.id
, arraysize(pad
.id
) - 1);
196 std::string mapping
= "standard";
197 base::TruncateUTF8ToByteSize(
198 mapping
, WebGamepad::mappingLengthCap
- 1, &mapping
);
199 tmp16
= base::UTF8ToUTF16(mapping
);
200 memset(pad
.mapping
, 0, sizeof(pad
.mapping
));
201 tmp16
.copy(pad
.mapping
, arraysize(pad
.mapping
) - 1);
206 pad
.connected
= true;
210 void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
211 device::ScopedUdevEnumeratePtr
enumerate(
212 device::udev_enumerate_new(udev_
->udev_handle()));
215 int ret
= device::udev_enumerate_add_match_subsystem(enumerate
.get(),
219 ret
= device::udev_enumerate_scan_devices(enumerate
.get());
223 udev_list_entry
* devices
=
224 device::udev_enumerate_get_list_entry(enumerate
.get());
225 for (udev_list_entry
* dev_list_entry
= devices
; dev_list_entry
!= NULL
;
226 dev_list_entry
= device::udev_list_entry_get_next(dev_list_entry
)) {
227 // Get the filename of the /sys entry for the device and create a
228 // udev_device object (dev) representing it
229 const char* path
= device::udev_list_entry_get_name(dev_list_entry
);
230 device::ScopedUdevDevicePtr
dev(
231 device::udev_device_new_from_syspath(udev_
->udev_handle(), path
));
234 RefreshDevice(dev
.get());
238 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index
) {
239 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/
240 if (index
>= WebGamepads::itemsLengthCap
) {
245 const int& fd
= device_fds_
[index
];
246 WebGamepad
& pad
= data_
.items
[index
];
250 while (HANDLE_EINTR(read(fd
, &event
, sizeof(struct js_event
))) > 0) {
251 size_t item
= event
.number
;
252 if (event
.type
& JS_EVENT_AXIS
) {
253 if (item
>= WebGamepad::axesLengthCap
)
255 pad
.axes
[item
] = event
.value
/ kMaxLinuxAxisValue
;
256 if (item
>= pad
.axesLength
)
257 pad
.axesLength
= item
+ 1;
258 } else if (event
.type
& JS_EVENT_BUTTON
) {
259 if (item
>= WebGamepad::buttonsLengthCap
)
261 pad
.buttons
[item
].pressed
= event
.value
;
262 pad
.buttons
[item
].value
= event
.value
? 1.0 : 0.0;
263 if (item
>= pad
.buttonsLength
)
264 pad
.buttonsLength
= item
+ 1;
266 pad
.timestamp
= event
.time
;
270 } // namespace content