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