Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(668)

Unified Diff: content/browser/gamepad/gamepad_platform_data_fetcher_linux.cc

Issue 2081583002: Migrating majority of gamepad from content/browser/ to device/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final tweaks Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/gamepad/gamepad_platform_data_fetcher_linux.cc
diff --git a/content/browser/gamepad/gamepad_platform_data_fetcher_linux.cc b/content/browser/gamepad/gamepad_platform_data_fetcher_linux.cc
deleted file mode 100644
index 87ec3714b66ea44df8be0ee549f53ce6247c043d..0000000000000000000000000000000000000000
--- a/content/browser/gamepad/gamepad_platform_data_fetcher_linux.cc
+++ /dev/null
@@ -1,274 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "content/browser/gamepad/gamepad_platform_data_fetcher_linux.h"
-
-#include <fcntl.h>
-#include <linux/joystick.h>
-#include <string.h>
-#include <sys/stat.h>
-#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"
-#include "base/strings/stringprintf.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/trace_event/trace_event.h"
-#include "device/udev_linux/scoped_udev.h"
-#include "device/udev_linux/udev_linux.h"
-
-namespace {
-
-const char kInputSubsystem[] = "input";
-const char kUsbSubsystem[] = "usb";
-const char kUsbDeviceType[] = "usb_device";
-const float kMaxLinuxAxisValue = 32767.0;
-
-void CloseFileDescriptorIfValid(int fd) {
- if (fd >= 0)
- close(fd);
-}
-
-bool IsGamepad(udev_device* dev, int* index, std::string* path) {
- if (!device::udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
- return false;
-
- const char* node_path = device::udev_device_get_devnode(dev);
- if (!node_path)
- return false;
-
- static const char kJoystickRoot[] = "/dev/input/js";
- bool is_gamepad = base::StartsWith(node_path, kJoystickRoot,
- base::CompareCase::SENSITIVE);
- if (!is_gamepad)
- return false;
-
- int tmp_idx = -1;
- const int base_len = sizeof(kJoystickRoot) - 1;
- base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len);
- if (!base::StringToInt(str, &tmp_idx))
- return false;
- if (tmp_idx < 0 ||
- tmp_idx >= static_cast<int>(blink::WebGamepads::itemsLengthCap)) {
- return false;
- }
- *index = tmp_idx;
- *path = node_path;
- return true;
-}
-
-} // namespace
-
-namespace content {
-
-using blink::WebGamepad;
-using blink::WebGamepads;
-
-GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() {
- for (size_t i = 0; i < arraysize(pad_state_); ++i) {
- device_fd_[i] = -1;
- pad_state_[i].mapper = 0;
- pad_state_[i].axis_mask = 0;
- pad_state_[i].button_mask = 0;
- }
-
- std::vector<device::UdevLinux::UdevMonitorFilter> filters;
- filters.push_back(
- device::UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL));
- udev_.reset(new device::UdevLinux(
- filters, base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice,
- base::Unretained(this))));
-
- EnumerateDevices();
-}
-
-GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() {
- for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i)
- CloseFileDescriptorIfValid(device_fd_[i]);
-}
-
-void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) {
- TRACE_EVENT0("GAMEPAD", "GetGamepadData");
-
- // Update our internal state.
- for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
- if (device_fd_[i] >= 0) {
- 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.
-void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
- int index;
- 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);
-
- // The device pointed to by dev contains information about the logical
- // joystick device. In order to get the information about the physical
- // hardware, get the parent device that is also in the "input" subsystem.
- // This function should just walk up the tree one level.
- dev = device::udev_device_get_parent_with_subsystem_devtype(
- dev, kInputSubsystem, NULL);
- 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;
- }
-
- const char* vendor_id =
- device::udev_device_get_sysattr_value(dev, "id/vendor");
- const char* product_id =
- device::udev_device_get_sysattr_value(dev, "id/product");
- mapper = GetGamepadStandardMappingFunction(vendor_id, product_id);
-
- // Driver returns utf-8 strings here, so combine in utf-8 first and
- // convert to WebUChar later once we've picked an id string.
- const char* name = device::udev_device_get_sysattr_value(dev, "name");
- std::string name_string(name);
-
- // In many cases the information the input subsystem contains isn't
- // as good as the information that the device bus has, walk up further
- // to the subsystem/device type "usb"/"usb_device" and if this device
- // has the same vendor/product id, prefer the description from that.
- struct udev_device* usb_dev =
- device::udev_device_get_parent_with_subsystem_devtype(
- dev, kUsbSubsystem, kUsbDeviceType);
- if (usb_dev) {
- const char* usb_vendor_id =
- device::udev_device_get_sysattr_value(usb_dev, "idVendor");
- const char* usb_product_id =
- device::udev_device_get_sysattr_value(usb_dev, "idProduct");
-
- if (strcmp(vendor_id, usb_vendor_id) == 0 &&
- strcmp(product_id, usb_product_id) == 0) {
- const char* manufacturer =
- device::udev_device_get_sysattr_value(usb_dev, "manufacturer");
- const char* product =
- device::udev_device_get_sysattr_value(usb_dev, "product");
-
- // Replace the previous name string with one containing the better
- // information, again driver returns utf-8 strings here so combine
- // in utf-8 for conversion to WebUChar below.
- name_string = base::StringPrintf("%s %s", manufacturer, product);
- }
- }
-
- // Append the vendor and product information then convert the utf-8
- // id string to WebUChar.
- std::string id =
- name_string + base::StringPrintf(" (%sVendor: %s Product: %s)",
- mapper ? "STANDARD GAMEPAD " : "",
- vendor_id,
- product_id);
- base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id);
- base::string16 tmp16 = base::UTF8ToUTF16(id);
- memset(pad.id, 0, sizeof(pad.id));
- tmp16.copy(pad.id, arraysize(pad.id) - 1);
-
- if (mapper) {
- std::string mapping = "standard";
- base::TruncateUTF8ToByteSize(
- mapping, WebGamepad::mappingLengthCap - 1, &mapping);
- tmp16 = base::UTF8ToUTF16(mapping);
- memset(pad.mapping, 0, sizeof(pad.mapping));
- tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1);
- } else {
- pad.mapping[0] = 0;
- }
-
- pad_state_[index].axis_mask = 0;
- pad_state_[index].button_mask = 0;
-
- pad.connected = true;
- }
-}
-
-void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
- device::ScopedUdevEnumeratePtr enumerate(
- device::udev_enumerate_new(udev_->udev_handle()));
- if (!enumerate)
- return;
- int ret = device::udev_enumerate_add_match_subsystem(enumerate.get(),
- kInputSubsystem);
- if (ret != 0)
- return;
- ret = device::udev_enumerate_scan_devices(enumerate.get());
- if (ret != 0)
- return;
-
- udev_list_entry* devices =
- device::udev_enumerate_get_list_entry(enumerate.get());
- for (udev_list_entry* dev_list_entry = devices; dev_list_entry != NULL;
- dev_list_entry = device::udev_list_entry_get_next(dev_list_entry)) {
- // Get the filename of the /sys entry for the device and create a
- // udev_device object (dev) representing it
- const char* path = device::udev_list_entry_get_name(dev_list_entry);
- device::ScopedUdevDevicePtr dev(
- device::udev_device_new_from_syspath(udev_->udev_handle(), path));
- if (!dev)
- continue;
- RefreshDevice(dev.get());
- }
-}
-
-void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
- // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/
- if (index >= WebGamepads::itemsLengthCap) {
- CHECK(false);
- return;
- }
-
- const int& fd = device_fd_[index];
- WebGamepad& pad = pad_state_[index].data;
- DCHECK_GE(fd, 0);
-
- js_event event;
- while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) {
- size_t item = event.number;
- if (event.type & JS_EVENT_AXIS) {
- if (item >= WebGamepad::axesLengthCap)
- continue;
-
- pad.axes[item] = event.value / kMaxLinuxAxisValue;
-
- if (item >= pad.axesLength)
- pad.axesLength = item + 1;
- } else if (event.type & JS_EVENT_BUTTON) {
- if (item >= WebGamepad::buttonsLengthCap)
- continue;
-
- pad.buttons[item].pressed = event.value;
- pad.buttons[item].value = event.value ? 1.0 : 0.0;
-
- if (item >= pad.buttonsLength)
- pad.buttonsLength = item + 1;
- }
- pad.timestamp = event.time;
- }
-}
-
-} // namespace content

Powered by Google App Engine
This is Rietveld 408576698