Index: content/browser/gamepad/data_fetcher_linux.cc |
diff --git a/content/browser/gamepad/data_fetcher_linux.cc b/content/browser/gamepad/data_fetcher_linux.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..08b59bb1ee5b1ae2fbe4f99306f7f2b6ea468a27 |
--- /dev/null |
+++ b/content/browser/gamepad/data_fetcher_linux.cc |
@@ -0,0 +1,323 @@ |
+// 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/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> |
+#include <fcntl.h> |
+#include <linux/joystick.h> |
+#include <sys/stat.h> |
+#include <sys/types.h> |
+ |
+namespace content { |
+ |
+using WebKit::WebGamepad; |
+using WebKit::WebGamepads; |
+ |
+// We rely on libudev to be available to notify us about device |
+// insertions/removals, and to get device USB vendor/product information. We |
+// try to dynamically load it here because it may not always be available. If |
+// it isn't, we simply disable all gamepad functionality. This late-binding |
+// apparatus is based on a simplified version of libjingle's |
+// linuxdevicefetcher.cc. We declare the symbols we'll use up front, load the |
+// .so and the symbols out of it at initialization time, and then retrieve |
+// them by index using the LATE(...) macro for (fairly) tidy callsites. |
+ |
+#define LIBUDEV_SYMBOLS_IN_USE \ |
+ X(udev_device_get_action) \ |
+ X(udev_device_get_devnode) \ |
+ X(udev_device_get_devtype) \ |
+ X(udev_device_get_parent_with_subsystem_devtype) \ |
+ X(udev_device_get_property_value) \ |
+ X(udev_device_get_subsystem) \ |
+ X(udev_device_get_sysattr_value) \ |
+ X(udev_device_new_from_syspath) \ |
+ X(udev_device_unref) \ |
+ X(udev_enumerate_add_match_subsystem) \ |
+ X(udev_enumerate_get_list_entry) \ |
+ X(udev_enumerate_new) \ |
+ X(udev_enumerate_scan_devices) \ |
+ X(udev_enumerate_unref) \ |
+ X(udev_list_entry_get_name) \ |
+ X(udev_list_entry_get_next) \ |
+ X(udev_monitor_enable_receiving) \ |
+ X(udev_monitor_filter_add_match_subsystem_devtype) \ |
+ X(udev_monitor_get_fd) \ |
+ X(udev_monitor_new_from_netlink) \ |
+ X(udev_monitor_receive_device) \ |
+ X(udev_monitor_unref) \ |
+ X(udev_new) \ |
+ X(udev_unref) |
+ |
+enum LibUdevSymbolTable { |
+#define X(sym) LibUdevSymbolTable_INDEX_##sym, |
+ LIBUDEV_SYMBOLS_IN_USE |
+#undef X |
+ LibUdevSymbolTable_SIZE |
+}; |
+ |
+class LibUdevLateBound { |
+ public: |
+ static LibUdevLateBound* Load() { |
+ void* handle = dlopen("libudev.so.0", RTLD_NOW); |
+ if (!handle) |
+ return NULL; |
+ LibUdevLateBound* result = new LibUdevLateBound(handle); |
+#define X(sym) result->symbols_[LibUdevSymbolTable_INDEX_##sym] = \ |
+ dlsym(result->handle_, #sym); \ |
+ if (!result->symbols_[LibUdevSymbolTable_INDEX_##sym]) { \ |
+ delete result; \ |
+ return NULL; \ |
+ } |
+ LIBUDEV_SYMBOLS_IN_USE |
+#undef X |
+ return result; |
+ } |
+ |
+ void* GetByIndex(size_t index) { |
+ return symbols_[index]; |
+ } |
+ |
+ private: |
+ friend class scoped_ptr<LibUdevLateBound>; |
+ |
+ ~LibUdevLateBound() { |
+ dlclose(handle_); |
+ } |
+ |
+ LibUdevLateBound(void* handle) : handle_(handle) { |
+ } |
+ |
+ void* handle_; |
+ void* symbols_[LibUdevSymbolTable_SIZE]; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(LibUdevLateBound); |
+}; |
+ |
+#define LATE(sym) (*reinterpret_cast<typeof(&sym)>(udevlib_->GetByIndex( \ |
+ LibUdevSymbolTable_INDEX_##sym))) |
+ |
+GamepadDataFetcherLinux::GamepadDataFetcherLinux() |
+ : udevlib_(LibUdevLateBound::Load()) { |
+ if (!udevlib_.get()) |
+ return; |
+ |
+ for (size_t i = 0; i < arraysize(device_fds_); ++i) |
+ device_fds_[i] = -1; |
+ |
+ udev_ = LATE(udev_new)(); |
+ |
+ monitor_ = LATE(udev_monitor_new_from_netlink)(udev_, "udev"); |
+ LATE(udev_monitor_filter_add_match_subsystem_devtype)( |
+ monitor_, |
+ "input", |
+ NULL); |
+ LATE(udev_monitor_enable_receiving)(monitor_); |
+ monitor_fd_ = LATE(udev_monitor_get_fd)(monitor_); |
+ |
+ EnumerateDevices(); |
+} |
+ |
+bool GamepadDataFetcherLinux::IsGamepad( |
+ udev_device* dev, |
+ size_t& index, |
+ std::string& path) { |
+ if (!LATE(udev_device_get_property_value)(dev, "ID_INPUT_JOYSTICK")) |
+ return false; |
+ |
+ const char* node_path = LATE(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; |
+ if (is) { |
+ index = node_path[sizeof(kJoystickRoot) - 1] - '0'; |
+ if (index < 0 || index >= WebGamepads::itemsLengthCap) |
+ return false; |
+ path = std::string(node_path); |
+ } |
+ return is; |
+} |
+ |
+// Used during enumeration, and monitor notifications. |
+void GamepadDataFetcherLinux::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 = LATE(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); |
+ if (device_fd < 0) { |
+ // Unable to open device, don't use. |
+ pad.connected = false; |
+ return; |
+ } |
+ |
+ const char* vendor_id = |
+ LATE(udev_device_get_sysattr_value)(dev, "idVendor"); |
+ const char* product_id = |
+ LATE(udev_device_get_sysattr_value)(dev, "idProduct"); |
+ mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); |
+ |
+ const char* manufacturer = |
+ LATE(udev_device_get_sysattr_value)(dev, "manufacturer"); |
+ const char* product = LATE(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. |
+ 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 GamepadDataFetcherLinux::EnumerateDevices() { |
+ udev_enumerate* enumerate = LATE(udev_enumerate_new)(udev_); |
+ LATE(udev_enumerate_add_match_subsystem)(enumerate, "input"); |
+ LATE(udev_enumerate_scan_devices)(enumerate); |
+ |
+ udev_list_entry* devices = LATE(udev_enumerate_get_list_entry)(enumerate); |
+ for (udev_list_entry* dev_list_entry = devices; |
+ dev_list_entry != NULL; |
+ dev_list_entry = LATE(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 = LATE(udev_list_entry_get_name)(dev_list_entry); |
+ udev_device* dev = LATE(udev_device_new_from_syspath)(udev_, path); |
+ RefreshDevice(dev); |
+ LATE(udev_device_unref)(dev); |
+ } |
+ // Free the enumerator object |
+ LATE(udev_enumerate_unref)(enumerate); |
+} |
+ |
+GamepadDataFetcherLinux::~GamepadDataFetcherLinux() { |
+ LATE(udev_unref)(udev_); |
+} |
+ |
+void GamepadDataFetcherLinux::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 |
+ && FD_ISSET(monitor_fd_, &fds)) { |
+ // Make the call to receive the device. |
+ // select() ensured that this will not block. |
+ udev_device* dev = LATE(udev_monitor_receive_device)(monitor_); |
+ RefreshDevice(dev); |
+ LATE(udev_device_unref)(dev); |
+ } |
+} |
+ |
+void GamepadDataFetcherLinux::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) { |
+ 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 GamepadDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) { |
+ TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
+ |
+ // If we were unable to load libudev then we are disabled, simply return no |
+ // data. |
+ if (!udevlib_.get()) { |
+ pads->length = 0; |
+ return; |
+ } |
+ |
+ 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 |