| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" | 5 #include "device/gamepad/gamepad_platform_data_fetcher_linux.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <linux/joystick.h> | 8 #include <linux/joystick.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 base::StartsWith(node_path, kJoystickRoot, base::CompareCase::SENSITIVE); | 45 base::StartsWith(node_path, kJoystickRoot, base::CompareCase::SENSITIVE); |
| 46 if (!is_gamepad) | 46 if (!is_gamepad) |
| 47 return false; | 47 return false; |
| 48 | 48 |
| 49 int tmp_idx = -1; | 49 int tmp_idx = -1; |
| 50 const int base_len = sizeof(kJoystickRoot) - 1; | 50 const int base_len = sizeof(kJoystickRoot) - 1; |
| 51 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len); | 51 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len); |
| 52 if (!base::StringToInt(str, &tmp_idx)) | 52 if (!base::StringToInt(str, &tmp_idx)) |
| 53 return false; | 53 return false; |
| 54 if (tmp_idx < 0 || | 54 if (tmp_idx < 0 || |
| 55 tmp_idx >= static_cast<int>(blink::WebGamepads::kItemsLengthCap)) { | 55 tmp_idx >= static_cast<int>(device::Gamepads::kItemsLengthCap)) { |
| 56 return false; | 56 return false; |
| 57 } | 57 } |
| 58 *index = tmp_idx; | 58 *index = tmp_idx; |
| 59 *path = node_path; | 59 *path = node_path; |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 } // namespace | 63 } // namespace |
| 64 | 64 |
| 65 namespace device { | 65 namespace device { |
| 66 | 66 |
| 67 using blink::WebGamepad; | |
| 68 using blink::WebGamepads; | |
| 69 | |
| 70 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { | 67 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { |
| 71 for (size_t i = 0; i < arraysize(device_fd_); ++i) { | 68 for (size_t i = 0; i < arraysize(device_fd_); ++i) { |
| 72 device_fd_[i] = -1; | 69 device_fd_[i] = -1; |
| 73 } | 70 } |
| 74 } | 71 } |
| 75 | 72 |
| 76 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { | 73 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { |
| 77 for (size_t i = 0; i < WebGamepads::kItemsLengthCap; ++i) | 74 for (size_t i = 0; i < Gamepads::kItemsLengthCap; ++i) |
| 78 CloseFileDescriptorIfValid(device_fd_[i]); | 75 CloseFileDescriptorIfValid(device_fd_[i]); |
| 79 } | 76 } |
| 80 | 77 |
| 81 GamepadSource GamepadPlatformDataFetcherLinux::source() { | 78 GamepadSource GamepadPlatformDataFetcherLinux::source() { |
| 82 return Factory::static_source(); | 79 return Factory::static_source(); |
| 83 } | 80 } |
| 84 | 81 |
| 85 void GamepadPlatformDataFetcherLinux::OnAddedToProvider() { | 82 void GamepadPlatformDataFetcherLinux::OnAddedToProvider() { |
| 86 std::vector<UdevLinux::UdevMonitorFilter> filters; | 83 std::vector<UdevLinux::UdevMonitorFilter> filters; |
| 87 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); | 84 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); |
| 88 udev_.reset(new UdevLinux( | 85 udev_.reset(new UdevLinux( |
| 89 filters, base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, | 86 filters, base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, |
| 90 base::Unretained(this)))); | 87 base::Unretained(this)))); |
| 91 | 88 |
| 92 EnumerateDevices(); | 89 EnumerateDevices(); |
| 93 } | 90 } |
| 94 | 91 |
| 95 void GamepadPlatformDataFetcherLinux::GetGamepadData(bool) { | 92 void GamepadPlatformDataFetcherLinux::GetGamepadData(bool) { |
| 96 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | 93 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
| 97 | 94 |
| 98 // Update our internal state. | 95 // Update our internal state. |
| 99 for (size_t i = 0; i < WebGamepads::kItemsLengthCap; ++i) { | 96 for (size_t i = 0; i < Gamepads::kItemsLengthCap; ++i) { |
| 100 if (device_fd_[i] >= 0) { | 97 if (device_fd_[i] >= 0) { |
| 101 ReadDeviceData(i); | 98 ReadDeviceData(i); |
| 102 } | 99 } |
| 103 } | 100 } |
| 104 } | 101 } |
| 105 | 102 |
| 106 // Used during enumeration, and monitor notifications. | 103 // Used during enumeration, and monitor notifications. |
| 107 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { | 104 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { |
| 108 int index; | 105 int index; |
| 109 std::string node_path; | 106 std::string node_path; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 131 } | 128 } |
| 132 | 129 |
| 133 PadState* state = GetPadState(index); | 130 PadState* state = GetPadState(index); |
| 134 if (!state) { | 131 if (!state) { |
| 135 // No slot available for device, don't use. | 132 // No slot available for device, don't use. |
| 136 CloseFileDescriptorIfValid(device_fd); | 133 CloseFileDescriptorIfValid(device_fd); |
| 137 device_fd = -1; | 134 device_fd = -1; |
| 138 return; | 135 return; |
| 139 } | 136 } |
| 140 | 137 |
| 141 WebGamepad& pad = state->data; | 138 Gamepad& pad = state->data; |
| 142 GamepadStandardMappingFunction& mapper = state->mapper; | 139 GamepadStandardMappingFunction& mapper = state->mapper; |
| 143 | 140 |
| 144 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); | 141 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); |
| 145 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); | 142 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); |
| 146 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); | 143 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); |
| 147 | 144 |
| 148 // Driver returns utf-8 strings here, so combine in utf-8 first and | 145 // Driver returns utf-8 strings here, so combine in utf-8 first and |
| 149 // convert to WebUChar later once we've picked an id string. | 146 // convert to WebUChar later once we've picked an id string. |
| 150 const char* name = udev_device_get_sysattr_value(dev, "name"); | 147 const char* name = udev_device_get_sysattr_value(dev, "name"); |
| 151 std::string name_string(name); | 148 std::string name_string(name); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 174 name_string = base::StringPrintf("%s %s", manufacturer, product); | 171 name_string = base::StringPrintf("%s %s", manufacturer, product); |
| 175 } | 172 } |
| 176 } | 173 } |
| 177 | 174 |
| 178 // Append the vendor and product information then convert the utf-8 | 175 // Append the vendor and product information then convert the utf-8 |
| 179 // id string to WebUChar. | 176 // id string to WebUChar. |
| 180 std::string id = | 177 std::string id = |
| 181 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)", | 178 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)", |
| 182 mapper ? "STANDARD GAMEPAD " : "", | 179 mapper ? "STANDARD GAMEPAD " : "", |
| 183 vendor_id, product_id); | 180 vendor_id, product_id); |
| 184 base::TruncateUTF8ToByteSize(id, WebGamepad::kIdLengthCap - 1, &id); | 181 base::TruncateUTF8ToByteSize(id, Gamepad::kIdLengthCap - 1, &id); |
| 185 base::string16 tmp16 = base::UTF8ToUTF16(id); | 182 base::string16 tmp16 = base::UTF8ToUTF16(id); |
| 186 memset(pad.id, 0, sizeof(pad.id)); | 183 memset(pad.id, 0, sizeof(pad.id)); |
| 187 tmp16.copy(pad.id, arraysize(pad.id) - 1); | 184 tmp16.copy(pad.id, arraysize(pad.id) - 1); |
| 188 | 185 |
| 189 if (mapper) { | 186 if (mapper) { |
| 190 std::string mapping = "standard"; | 187 std::string mapping = "standard"; |
| 191 base::TruncateUTF8ToByteSize(mapping, WebGamepad::kMappingLengthCap - 1, | 188 base::TruncateUTF8ToByteSize(mapping, Gamepad::kMappingLengthCap - 1, |
| 192 &mapping); | 189 &mapping); |
| 193 tmp16 = base::UTF8ToUTF16(mapping); | 190 tmp16 = base::UTF8ToUTF16(mapping); |
| 194 memset(pad.mapping, 0, sizeof(pad.mapping)); | 191 memset(pad.mapping, 0, sizeof(pad.mapping)); |
| 195 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); | 192 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); |
| 196 } else { | 193 } else { |
| 197 pad.mapping[0] = 0; | 194 pad.mapping[0] = 0; |
| 198 } | 195 } |
| 199 | 196 |
| 200 pad.connected = true; | 197 pad.connected = true; |
| 201 } | 198 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 221 const char* path = udev_list_entry_get_name(dev_list_entry); | 218 const char* path = udev_list_entry_get_name(dev_list_entry); |
| 222 ScopedUdevDevicePtr dev( | 219 ScopedUdevDevicePtr dev( |
| 223 udev_device_new_from_syspath(udev_->udev_handle(), path)); | 220 udev_device_new_from_syspath(udev_->udev_handle(), path)); |
| 224 if (!dev) | 221 if (!dev) |
| 225 continue; | 222 continue; |
| 226 RefreshDevice(dev.get()); | 223 RefreshDevice(dev.get()); |
| 227 } | 224 } |
| 228 } | 225 } |
| 229 | 226 |
| 230 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { | 227 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { |
| 231 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ | 228 // Linker does not like CHECK_LT(index, Gamepads::kItemsLengthCap). =/ |
| 232 if (index >= WebGamepads::kItemsLengthCap) { | 229 if (index >= Gamepads::kItemsLengthCap) { |
| 233 CHECK(false); | 230 CHECK(false); |
| 234 return; | 231 return; |
| 235 } | 232 } |
| 236 | 233 |
| 237 PadState* state = GetPadState(index); | 234 PadState* state = GetPadState(index); |
| 238 if (!state) | 235 if (!state) |
| 239 return; | 236 return; |
| 240 | 237 |
| 241 int fd = device_fd_[index]; | 238 int fd = device_fd_[index]; |
| 242 DCHECK_GE(fd, 0); | 239 DCHECK_GE(fd, 0); |
| 243 | 240 |
| 244 WebGamepad& pad = state->data; | 241 Gamepad& pad = state->data; |
| 245 | 242 |
| 246 js_event event; | 243 js_event event; |
| 247 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) { | 244 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) { |
| 248 size_t item = event.number; | 245 size_t item = event.number; |
| 249 if (event.type & JS_EVENT_AXIS) { | 246 if (event.type & JS_EVENT_AXIS) { |
| 250 if (item >= WebGamepad::kAxesLengthCap) | 247 if (item >= Gamepad::kAxesLengthCap) |
| 251 continue; | 248 continue; |
| 252 | 249 |
| 253 pad.axes[item] = event.value / kMaxLinuxAxisValue; | 250 pad.axes[item] = event.value / kMaxLinuxAxisValue; |
| 254 | 251 |
| 255 if (item >= pad.axes_length) | 252 if (item >= pad.axes_length) |
| 256 pad.axes_length = item + 1; | 253 pad.axes_length = item + 1; |
| 257 } else if (event.type & JS_EVENT_BUTTON) { | 254 } else if (event.type & JS_EVENT_BUTTON) { |
| 258 if (item >= WebGamepad::kButtonsLengthCap) | 255 if (item >= Gamepad::kButtonsLengthCap) |
| 259 continue; | 256 continue; |
| 260 | 257 |
| 261 pad.buttons[item].pressed = event.value; | 258 pad.buttons[item].pressed = event.value; |
| 262 pad.buttons[item].value = event.value ? 1.0 : 0.0; | 259 pad.buttons[item].value = event.value ? 1.0 : 0.0; |
| 263 | 260 |
| 264 if (item >= pad.buttons_length) | 261 if (item >= pad.buttons_length) |
| 265 pad.buttons_length = item + 1; | 262 pad.buttons_length = item + 1; |
| 266 } | 263 } |
| 267 pad.timestamp = event.time; | 264 pad.timestamp = event.time; |
| 268 } | 265 } |
| 269 } | 266 } |
| 270 | 267 |
| 271 } // namespace device | 268 } // namespace device |
| OLD | NEW |