OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/browser/gamepad/platform_data_fetcher_linux.h" |
| 6 |
| 7 #include <dlfcn.h> |
| 8 #include <fcntl.h> |
| 9 #include <libudev.h> |
| 10 #include <linux/joystick.h> |
| 11 #include <sys/stat.h> |
| 12 #include <sys/types.h> |
| 13 |
| 14 #include "base/debug/trace_event.h" |
| 15 #include "base/eintr_wrapper.h" |
| 16 #include "base/message_loop.h" |
| 17 #include "base/string_number_conversions.h" |
| 18 #include "base/stringprintf.h" |
| 19 #include "base/string_util.h" |
| 20 #include "base/utf_string_conversions.h" |
| 21 #include "content/common/gamepad_hardware_buffer.h" |
| 22 |
| 23 namespace content { |
| 24 |
| 25 using WebKit::WebGamepad; |
| 26 using WebKit::WebGamepads; |
| 27 |
| 28 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { |
| 29 for (size_t i = 0; i < arraysize(device_fds_); ++i) |
| 30 device_fds_[i] = -1; |
| 31 |
| 32 udev_ = udev_new(); |
| 33 |
| 34 monitor_ = udev_monitor_new_from_netlink(udev_, "udev"); |
| 35 udev_monitor_filter_add_match_subsystem_devtype(monitor_, "input", NULL); |
| 36 udev_monitor_enable_receiving(monitor_); |
| 37 monitor_fd_ = udev_monitor_get_fd(monitor_); |
| 38 MessageLoopForIO::current()->WatchFileDescriptor(monitor_fd_, true, |
| 39 MessageLoopForIO::WATCH_READ, &monitor_watcher_, this); |
| 40 |
| 41 EnumerateDevices(); |
| 42 } |
| 43 |
| 44 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { |
| 45 monitor_watcher_.StopWatchingFileDescriptor(); |
| 46 udev_unref(udev_); |
| 47 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 48 if (device_fds_[i] >= 0) |
| 49 close(device_fds_[i]); |
| 50 } |
| 51 } |
| 52 |
| 53 void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) { |
| 54 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
| 55 |
| 56 data_.length = WebGamepads::itemsLengthCap; |
| 57 |
| 58 // Update our internal state. |
| 59 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 60 if (device_fds_[i] >= 0) { |
| 61 ReadDeviceData(i); |
| 62 } |
| 63 } |
| 64 |
| 65 // Copy to the current state to the output buffer, using the mapping |
| 66 // function, if there is one available. |
| 67 pads->length = data_.length; |
| 68 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 69 if (mappers_[i]) |
| 70 mappers_[i](data_.items[i], &pads->items[i]); |
| 71 else |
| 72 pads->items[i] = data_.items[i]; |
| 73 } |
| 74 } |
| 75 |
| 76 void GamepadPlatformDataFetcherLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 77 // Events occur when devices attached to the system are added, removed, or |
| 78 // change state. udev_monitor_receive_device() will return a device object |
| 79 // representing the device which changed and what type of change occured. |
| 80 DCHECK(monitor_fd_ == fd); |
| 81 udev_device* dev = udev_monitor_receive_device(monitor_); |
| 82 RefreshDevice(dev); |
| 83 udev_device_unref(dev); |
| 84 } |
| 85 |
| 86 void GamepadPlatformDataFetcherLinux::OnFileCanWriteWithoutBlocking(int fd) { |
| 87 } |
| 88 |
| 89 bool GamepadPlatformDataFetcherLinux::IsGamepad( |
| 90 udev_device* dev, |
| 91 int& index, |
| 92 std::string& path) { |
| 93 if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK")) |
| 94 return false; |
| 95 |
| 96 const char* node_path = udev_device_get_devnode(dev); |
| 97 if (!node_path) |
| 98 return false; |
| 99 |
| 100 static const char kJoystickRoot[] = "/dev/input/js"; |
| 101 bool is_gamepad = |
| 102 strncmp(kJoystickRoot, node_path, sizeof(kJoystickRoot) - 1) == 0; |
| 103 if (is_gamepad) { |
| 104 const int base_len = sizeof(kJoystickRoot) - 1; |
| 105 if (!base::StringToInt(base::StringPiece( |
| 106 &node_path[base_len], |
| 107 strlen(node_path) - base_len), |
| 108 &index)) |
| 109 return false; |
| 110 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
| 111 return false; |
| 112 path = std::string(node_path); |
| 113 } |
| 114 return is_gamepad; |
| 115 } |
| 116 |
| 117 // Used during enumeration, and monitor notifications. |
| 118 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { |
| 119 int index; |
| 120 std::string node_path; |
| 121 if (IsGamepad(dev, index, node_path)) { |
| 122 int& device_fd = device_fds_[index]; |
| 123 WebGamepad& pad = data_.items[index]; |
| 124 GamepadStandardMappingFunction& mapper = mappers_[index]; |
| 125 |
| 126 if (device_fd >= 0) |
| 127 close(device_fd); |
| 128 |
| 129 // The device pointed to by dev contains information about the input |
| 130 // device. In order to get the information about the USB device, get the |
| 131 // parent device with the subsystem/devtype pair of "usb"/"usb_device". |
| 132 // This function walks up the tree several levels. |
| 133 dev = udev_device_get_parent_with_subsystem_devtype( |
| 134 dev, |
| 135 "usb", |
| 136 "usb_device"); |
| 137 if (!dev) { |
| 138 // Unable to get device information, don't use this device. |
| 139 device_fd = -1; |
| 140 pad.connected = false; |
| 141 return; |
| 142 } |
| 143 |
| 144 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); |
| 145 if (device_fd < 0) { |
| 146 // Unable to open device, don't use. |
| 147 pad.connected = false; |
| 148 return; |
| 149 } |
| 150 |
| 151 const char* vendor_id = udev_device_get_sysattr_value(dev, "idVendor"); |
| 152 const char* product_id = udev_device_get_sysattr_value(dev, "idProduct"); |
| 153 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); |
| 154 |
| 155 const char* manufacturer = |
| 156 udev_device_get_sysattr_value(dev, "manufacturer"); |
| 157 const char* product = udev_device_get_sysattr_value(dev, "product"); |
| 158 |
| 159 // Driver returns utf-8 strings here, so combine in utf-8 and then convert |
| 160 // to WebUChar to build the id string. |
| 161 std::string id; |
| 162 if (mapper) { |
| 163 id = base::StringPrintf("%s %s (STANDARD GAMEPAD)", |
| 164 manufacturer, |
| 165 product); |
| 166 } else { |
| 167 id = base::StringPrintf("%s %s (Vendor: %s Product: %s)", |
| 168 manufacturer, |
| 169 product, |
| 170 vendor_id, |
| 171 product_id); |
| 172 } |
| 173 TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); |
| 174 string16 tmp16 = UTF8ToUTF16(id); |
| 175 memset(pad.id, 0, sizeof(pad.id)); |
| 176 tmp16.copy(pad.id, arraysize(pad.id) - 1); |
| 177 |
| 178 pad.connected = true; |
| 179 } |
| 180 } |
| 181 |
| 182 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { |
| 183 udev_enumerate* enumerate = udev_enumerate_new(udev_); |
| 184 udev_enumerate_add_match_subsystem(enumerate, "input"); |
| 185 udev_enumerate_scan_devices(enumerate); |
| 186 |
| 187 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); |
| 188 for (udev_list_entry* dev_list_entry = devices; |
| 189 dev_list_entry != NULL; |
| 190 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { |
| 191 // Get the filename of the /sys entry for the device and create a |
| 192 // udev_device object (dev) representing it |
| 193 const char* path = udev_list_entry_get_name(dev_list_entry); |
| 194 udev_device* dev = udev_device_new_from_syspath(udev_, path); |
| 195 RefreshDevice(dev); |
| 196 udev_device_unref(dev); |
| 197 } |
| 198 // Free the enumerator object |
| 199 udev_enumerate_unref(enumerate); |
| 200 } |
| 201 |
| 202 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { |
| 203 int& fd = device_fds_[index]; |
| 204 WebGamepad& pad = data_.items[index]; |
| 205 DCHECK(fd >= 0); |
| 206 |
| 207 js_event event; |
| 208 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event)) > 0)) { |
| 209 size_t item = event.number; |
| 210 if (event.type & JS_EVENT_AXIS) { |
| 211 if (item >= WebGamepad::axesLengthCap) |
| 212 continue; |
| 213 pad.axes[item] = event.value / 32767.f; |
| 214 if (item >= pad.axesLength) |
| 215 pad.axesLength = item + 1; |
| 216 } else if (event.type & JS_EVENT_BUTTON) { |
| 217 if (item >= WebGamepad::buttonsLengthCap) |
| 218 continue; |
| 219 pad.buttons[item] = event.value ? 1.0 : 0.0; |
| 220 if (item >= pad.buttonsLength) |
| 221 pad.buttonsLength = item + 1; |
| 222 } |
| 223 pad.timestamp = event.time; |
| 224 } |
| 225 } |
| 226 |
| 227 |
| 228 } // namespace content |
OLD | NEW |