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

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

Issue 8899017: Add gamepad data fetcher for Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use system.gyp for udev dep Created 9 years 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/platform_data_fetcher_linux.cc
diff --git a/content/browser/gamepad/platform_data_fetcher_linux.cc b/content/browser/gamepad/platform_data_fetcher_linux.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0bb95e1d817a38b3378e65e417f47b0d7a0cb2ab
--- /dev/null
+++ b/content/browser/gamepad/platform_data_fetcher_linux.cc
@@ -0,0 +1,226 @@
+// Copyright (c) 2011 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/platform_data_fetcher_linux.h"
+
+#include "base/debug/trace_event.h"
+#include "base/string_util.h"
+#include "base/utf_string_conversions.h"
+#include "content/common/gamepad_hardware_buffer.h"
+
+#include <dlfcn.h>
Ryan Sleevi 2011/12/17 04:31:14 style nit: C headers before Chromium headers
scottmg 2011/12/19 19:41:45 Done.
+#include <fcntl.h>
+#include <libudev.h>
+#include <linux/joystick.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+namespace content {
+
+using WebKit::WebGamepad;
+using WebKit::WebGamepads;
+
+GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() {
+ for (size_t i = 0; i < arraysize(device_fds_); ++i)
+ device_fds_[i] = -1;
+
+ udev_ = udev_new();
+
+ monitor_ = udev_monitor_new_from_netlink(udev_, "udev");
+ udev_monitor_filter_add_match_subsystem_devtype(monitor_, "input", NULL);
+ udev_monitor_enable_receiving(monitor_);
+ monitor_fd_ = udev_monitor_get_fd(monitor_);
+
+ EnumerateDevices();
+}
+
+
+bool GamepadPlatformDataFetcherLinux::IsGamepad(
+ udev_device* dev,
+ size_t& index,
+ std::string& path) {
+ if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
+ return false;
+
+ const char* node_path = udev_device_get_devnode(dev);
+ if (!node_path)
+ return false;
+
+ static const char kJoystickRoot[] = "/dev/input/js";
+ bool is = strncmp(kJoystickRoot, node_path, sizeof(kJoystickRoot) - 1) == 0;
Ryan Sleevi 2011/12/17 04:31:14 nit: is -> is_gamepad ?
scottmg 2011/12/19 19:41:45 Done.
+ if (is) {
+ index = node_path[sizeof(kJoystickRoot) - 1] - '0';
Ryan Sleevi 2011/12/17 04:31:14 BUG? It seems possible for there to be a /dev/inpu
scottmg 2011/12/19 19:41:45 Done.
+ if (index < 0 || index >= WebGamepads::itemsLengthCap)
+ return false;
+ path = std::string(node_path);
+ }
+ return is;
+}
+
+// Used during enumeration, and monitor notifications.
+void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
+ size_t index;
+ std::string node_path;
+ if (IsGamepad(dev, index, node_path)) {
+ int& device_fd = device_fds_[index];
+ WebGamepad& pad = data_.items[index];
+ GamepadStandardMappingFunction& mapper = mappers_[index];
+
+ // The device pointed to by dev contains information about the input
+ // device. In order to get the information about the USB device, get the
+ // parent device with the subsystem/devtype pair of "usb"/"usb_device".
+ // This function walks up the tree several levels.
+ dev = udev_device_get_parent_with_subsystem_devtype(
+ dev,
+ "usb",
+ "usb_device");
+ if (!dev) {
+ // Unable to get device information, don't use this device.
+ device_fd = -1;
+ pad.connected = false;
+ return;
+ }
+
+ device_fd = open(node_path.c_str(), O_RDONLY | O_NONBLOCK);
Ryan Sleevi 2011/12/17 04:31:14 Normally these calls are wrapped in the HANDLE_EIN
scottmg 2011/12/19 19:41:45 Done.
+ if (device_fd < 0) {
+ // Unable to open device, don't use.
+ pad.connected = false;
+ return;
+ }
+
+ const char* vendor_id = udev_device_get_sysattr_value(dev, "idVendor");
+ const char* product_id = udev_device_get_sysattr_value(dev, "idProduct");
+ mapper = GetGamepadStandardMappingFunction(vendor_id, product_id);
+
+ const char* manufacturer =
+ udev_device_get_sysattr_value(dev, "manufacturer");
+ const char* product = udev_device_get_sysattr_value(dev, "product");
+
+ // Driver returns utf-8 strings here, so combine in utf-8 and then convert
+ // to WebUChar to build the id string.
Ryan Sleevi 2011/12/17 04:31:14 BUG? Because they're UTF-8 strings, what risk is t
scottmg 2011/12/19 19:41:45 Done. There's no Truncate for utf16 in base so I (
+ char tmp[WebGamepad::idLengthCap];
+ if (mapper) {
+ base::snprintf(tmp,
+ sizeof(tmp),
+ "%s %s (STANDARD GAMEPAD)",
+ manufacturer,
+ product);
+ } else {
+ base::snprintf(tmp,
+ sizeof(tmp),
+ "%s %s (Vendor: %s Product: %s)",
+ manufacturer,
+ product,
+ vendor_id,
+ product_id);
+ }
+ string16 tmp16 = UTF8ToUTF16(tmp);
+ memset(pad.id, 0, sizeof(pad.id));
+ tmp16.copy(pad.id, sizeof(pad.id) - 1);
+
+ pad.connected = true;
+ }
+}
+
+void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
+ udev_enumerate* enumerate = udev_enumerate_new(udev_);
+ udev_enumerate_add_match_subsystem(enumerate, "input");
+ udev_enumerate_scan_devices(enumerate);
+
+ udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
+ for (udev_list_entry* dev_list_entry = devices;
+ dev_list_entry != NULL;
+ dev_list_entry = 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 = udev_list_entry_get_name(dev_list_entry);
+ udev_device* dev = udev_device_new_from_syspath(udev_, path);
+ RefreshDevice(dev);
+ udev_device_unref(dev);
+ }
+ // Free the enumerator object
+ udev_enumerate_unref(enumerate);
+}
+
+GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() {
Ryan Sleevi 2011/12/17 04:31:14 style nit: There was a big clean-up effort to have
scottmg 2011/12/19 19:41:45 Done.
+ udev_unref(udev_);
+}
+
+void GamepadPlatformDataFetcherLinux::CheckForAddRemoveEvents() {
+ // Poll for udev events. Events occur when devices attached to the system
+ // are added, removed, or change state. udev_monitor_receive_device() will
+ // return a device object representing the device which changed and what
+ // type of change occured.
+ //
+ // The select() system call is used to ensure that the call to
+ // udev_monitor_receive_device() will not block.
+ fd_set fds;
+ struct timeval tv;
+ FD_ZERO(&fds);
+ FD_SET(monitor_fd_, &fds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+
+ // Check if our file descriptor has received data.
+ if (select(monitor_fd_ + 1, &fds, NULL, NULL, &tv) > 0
Ryan Sleevi 2011/12/17 04:31:14 HANDLE_EINTR(select( ? Is there a reason not to u
scottmg 2011/12/19 19:41:45 Done.
+ && FD_ISSET(monitor_fd_, &fds)) {
+ // Make the call to receive the device.
+ // select() ensured that this will not block.
+ udev_device* dev = udev_monitor_receive_device(monitor_);
+ RefreshDevice(dev);
+ udev_device_unref(dev);
+ }
+}
+
+void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
+ int& fd = device_fds_[index];
+ WebGamepad& pad = data_.items[index];
+ DCHECK(fd >= 0);
+
+ js_event event;
+ while (read(fd, &event, sizeof(struct js_event)) > 0) {
Ryan Sleevi 2011/12/17 04:31:14 HANDLE_EINTR(read( ?
scottmg 2011/12/19 19:41:45 Done.
+ size_t item = event.number;
+ if (event.type & JS_EVENT_AXIS) {
+ if (item >= WebGamepad::axesLengthCap)
+ continue;
+ pad.axes[item] = event.value / 32767.f;
+ if (item >= pad.axesLength)
+ pad.axesLength = item + 1;
+ } else if (event.type & JS_EVENT_BUTTON) {
+ if (item >= WebGamepad::buttonsLengthCap)
+ continue;
+ pad.buttons[item] = event.value ? 1.0 : 0.0;
+ if (item >= pad.buttonsLength)
+ pad.buttonsLength = item + 1;
+ }
+ pad.timestamp = event.time;
+ }
+}
+
+void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) {
+ TRACE_EVENT0("GAMEPAD", "GetGamepadData");
+
+ data_.length = WebGamepads::itemsLengthCap;
+
+ CheckForAddRemoveEvents();
+
+ // Update our internal state.
+ for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
+ if (device_fds_[i] >= 0) {
+ ReadDeviceData(i);
+ }
+ }
+
+ // Copy to the current state to the output buffer, using the mapping
+ // function, if there is one available.
+ pads->length = data_.length;
+ for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
+ if (mappers_[i])
+ mappers_[i](data_.items[i], &pads->items[i]);
+ else
+ pads->items[i] = data_.items[i];
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698