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