| Index: device/gamepad/gamepad_platform_data_fetcher_linux.cc
|
| diff --git a/device/gamepad/gamepad_platform_data_fetcher_linux.cc b/device/gamepad/gamepad_platform_data_fetcher_linux.cc
|
| index 4781a8f7fac8b800709fc2922e2e45628691ee98..5fe3e43f848933fdddb26d152642fd939cc461b4 100644
|
| --- a/device/gamepad/gamepad_platform_data_fetcher_linux.cc
|
| +++ b/device/gamepad/gamepad_platform_data_fetcher_linux.cc
|
| @@ -11,8 +11,6 @@
|
| #include <sys/types.h>
|
| #include <unistd.h>
|
|
|
| -#include "base/macros.h"
|
| -#include "base/message_loop/message_loop.h"
|
| #include "base/posix/eintr_wrapper.h"
|
| #include "base/strings/string_number_conversions.h"
|
| #include "base/strings/string_util.h"
|
| @@ -70,13 +68,21 @@ using blink::WebGamepad;
|
| using blink::WebGamepads;
|
|
|
| GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() {
|
| - for (size_t i = 0; i < arraysize(pad_state_); ++i) {
|
| + for (size_t i = 0; i < arraysize(device_fd_); ++i) {
|
| device_fd_[i] = -1;
|
| - pad_state_[i].mapper = 0;
|
| - pad_state_[i].axis_mask = 0;
|
| - pad_state_[i].button_mask = 0;
|
| }
|
| +}
|
|
|
| +GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() {
|
| + for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i)
|
| + CloseFileDescriptorIfValid(device_fd_[i]);
|
| +}
|
| +
|
| +GamepadSource GamepadPlatformDataFetcherLinux::source() {
|
| + return Factory::static_source();
|
| +}
|
| +
|
| +void GamepadPlatformDataFetcherLinux::OnAddedToProvider() {
|
| std::vector<UdevLinux::UdevMonitorFilter> filters;
|
| filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL));
|
| udev_.reset(new UdevLinux(
|
| @@ -86,12 +92,7 @@ GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() {
|
| EnumerateDevices();
|
| }
|
|
|
| -GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() {
|
| - for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i)
|
| - CloseFileDescriptorIfValid(device_fd_[i]);
|
| -}
|
| -
|
| -void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) {
|
| +void GamepadPlatformDataFetcherLinux::GetGamepadData(bool) {
|
| TRACE_EVENT0("GAMEPAD", "GetGamepadData");
|
|
|
| // Update our internal state.
|
| @@ -100,11 +101,6 @@ void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) {
|
| ReadDeviceData(i);
|
| }
|
| }
|
| -
|
| - pads->length = WebGamepads::itemsLengthCap;
|
| - for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
|
| - MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]);
|
| - }
|
| }
|
|
|
| // Used during enumeration, and monitor notifications.
|
| @@ -113,8 +109,6 @@ void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
|
| std::string node_path;
|
| if (IsGamepad(dev, &index, &node_path)) {
|
| int& device_fd = device_fd_[index];
|
| - WebGamepad& pad = pad_state_[index].data;
|
| - GamepadStandardMappingFunction& mapper = pad_state_[index].mapper;
|
|
|
| CloseFileDescriptorIfValid(device_fd);
|
|
|
| @@ -127,17 +121,26 @@ void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
|
| if (!dev) {
|
| // Unable to get device information, don't use this device.
|
| device_fd = -1;
|
| - pad.connected = false;
|
| return;
|
| }
|
|
|
| device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK));
|
| if (device_fd < 0) {
|
| // Unable to open device, don't use.
|
| - pad.connected = false;
|
| return;
|
| }
|
|
|
| + PadState* state = GetPadState(index);
|
| + if (!state) {
|
| + // No slot available for device, don't use.
|
| + CloseFileDescriptorIfValid(device_fd);
|
| + device_fd = -1;
|
| + return;
|
| + }
|
| +
|
| + WebGamepad& pad = state->data;
|
| + GamepadStandardMappingFunction& mapper = state->mapper;
|
| +
|
| const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor");
|
| const char* product_id = udev_device_get_sysattr_value(dev, "id/product");
|
| mapper = GetGamepadStandardMappingFunction(vendor_id, product_id);
|
| @@ -194,9 +197,6 @@ void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
|
| pad.mapping[0] = 0;
|
| }
|
|
|
| - pad_state_[index].axis_mask = 0;
|
| - pad_state_[index].button_mask = 0;
|
| -
|
| pad.connected = true;
|
| }
|
| }
|
| @@ -234,10 +234,15 @@ void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
|
| return;
|
| }
|
|
|
| - const int& fd = device_fd_[index];
|
| - WebGamepad& pad = pad_state_[index].data;
|
| + PadState* state = GetPadState(index);
|
| + if (!state)
|
| + return;
|
| +
|
| + int fd = device_fd_[index];
|
| DCHECK_GE(fd, 0);
|
|
|
| + WebGamepad& pad = state->data;
|
| +
|
| js_event event;
|
| while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) {
|
| size_t item = event.number;
|
|
|