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 "content/browser/gamepad/gamepad_platform_data_fetcher_linux.h" | 5 #include "content/browser/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> |
11 #include <sys/types.h> | 11 #include <sys/types.h> |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 | 13 |
| 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" |
14 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
19 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
20 #include "content/browser/udev_linux.h" | 22 #include "content/browser/udev_linux.h" |
21 #include "device/udev_linux/scoped_udev.h" | 23 #include "device/udev_linux/scoped_udev.h" |
22 | 24 |
23 namespace { | 25 namespace { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 63 } |
62 | 64 |
63 } // namespace | 65 } // namespace |
64 | 66 |
65 namespace content { | 67 namespace content { |
66 | 68 |
67 using blink::WebGamepad; | 69 using blink::WebGamepad; |
68 using blink::WebGamepads; | 70 using blink::WebGamepads; |
69 | 71 |
70 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { | 72 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { |
71 for (size_t i = 0; i < arraysize(device_fd_); ++i) { | 73 for (size_t i = 0; i < arraysize(pad_state_); ++i) { |
72 device_fd_[i] = -1; | 74 device_fd_[i] = -1; |
| 75 pad_state_[i].mapper = 0; |
| 76 pad_state_[i].axis_mask = 0; |
| 77 pad_state_[i].button_mask = 0; |
73 } | 78 } |
74 } | |
75 | 79 |
76 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { | |
77 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) | |
78 CloseFileDescriptorIfValid(device_fd_[i]); | |
79 } | |
80 | |
81 void GamepadPlatformDataFetcherLinux::OnAddedToProvider() { | |
82 std::vector<UdevLinux::UdevMonitorFilter> filters; | 80 std::vector<UdevLinux::UdevMonitorFilter> filters; |
83 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); | 81 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); |
84 udev_.reset( | 82 udev_.reset( |
85 new UdevLinux(filters, | 83 new UdevLinux(filters, |
86 base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, | 84 base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, |
87 base::Unretained(this)))); | 85 base::Unretained(this)))); |
88 | 86 |
89 EnumerateDevices(); | 87 EnumerateDevices(); |
90 } | 88 } |
91 | 89 |
92 void GamepadPlatformDataFetcherLinux::GetGamepadData(bool) { | 90 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { |
| 91 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) |
| 92 CloseFileDescriptorIfValid(device_fd_[i]); |
| 93 } |
| 94 |
| 95 void GamepadPlatformDataFetcherLinux::GetGamepadData(WebGamepads* pads, bool) { |
93 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | 96 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
94 | 97 |
95 // Update our internal state. | 98 // Update our internal state. |
96 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 99 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
97 if (device_fd_[i] >= 0) { | 100 if (device_fd_[i] >= 0) { |
98 ReadDeviceData(i); | 101 ReadDeviceData(i); |
99 } | 102 } |
100 } | 103 } |
| 104 |
| 105 pads->length = WebGamepads::itemsLengthCap; |
| 106 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 107 MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]); |
| 108 } |
101 } | 109 } |
102 | 110 |
103 // Used during enumeration, and monitor notifications. | 111 // Used during enumeration, and monitor notifications. |
104 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { | 112 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { |
105 int index; | 113 int index; |
106 std::string node_path; | 114 std::string node_path; |
107 if (IsGamepad(dev, &index, &node_path)) { | 115 if (IsGamepad(dev, &index, &node_path)) { |
108 int& device_fd = device_fd_[index]; | 116 int& device_fd = device_fd_[index]; |
| 117 WebGamepad& pad = pad_state_[index].data; |
| 118 GamepadStandardMappingFunction& mapper = pad_state_[index].mapper; |
109 | 119 |
110 CloseFileDescriptorIfValid(device_fd); | 120 CloseFileDescriptorIfValid(device_fd); |
111 | 121 |
112 // The device pointed to by dev contains information about the logical | 122 // The device pointed to by dev contains information about the logical |
113 // joystick device. In order to get the information about the physical | 123 // joystick device. In order to get the information about the physical |
114 // hardware, get the parent device that is also in the "input" subsystem. | 124 // hardware, get the parent device that is also in the "input" subsystem. |
115 // This function should just walk up the tree one level. | 125 // This function should just walk up the tree one level. |
116 dev = device::udev_device_get_parent_with_subsystem_devtype( | 126 dev = device::udev_device_get_parent_with_subsystem_devtype( |
117 dev, kInputSubsystem, NULL); | 127 dev, kInputSubsystem, NULL); |
118 if (!dev) { | 128 if (!dev) { |
119 // Unable to get device information, don't use this device. | 129 // Unable to get device information, don't use this device. |
120 device_fd = -1; | 130 device_fd = -1; |
| 131 pad.connected = false; |
121 return; | 132 return; |
122 } | 133 } |
123 | 134 |
124 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); | 135 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); |
125 if (device_fd < 0) { | 136 if (device_fd < 0) { |
126 // Unable to open device, don't use. | 137 // Unable to open device, don't use. |
| 138 pad.connected = false; |
127 return; | 139 return; |
128 } | 140 } |
129 | 141 |
130 PadState* state = provider()->GetPadState(GAMEPAD_SOURCE_LINUX_UDEV, index); | |
131 if (!state) { | |
132 // No slot available for device, don't use. | |
133 CloseFileDescriptorIfValid(device_fd); | |
134 device_fd = -1; | |
135 return; | |
136 } | |
137 | |
138 WebGamepad& pad = state->data; | |
139 GamepadStandardMappingFunction& mapper = state->mapper; | |
140 | |
141 const char* vendor_id = | 142 const char* vendor_id = |
142 device::udev_device_get_sysattr_value(dev, "id/vendor"); | 143 device::udev_device_get_sysattr_value(dev, "id/vendor"); |
143 const char* product_id = | 144 const char* product_id = |
144 device::udev_device_get_sysattr_value(dev, "id/product"); | 145 device::udev_device_get_sysattr_value(dev, "id/product"); |
145 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); | 146 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); |
146 | 147 |
147 // Driver returns utf-8 strings here, so combine in utf-8 first and | 148 // Driver returns utf-8 strings here, so combine in utf-8 first and |
148 // convert to WebUChar later once we've picked an id string. | 149 // convert to WebUChar later once we've picked an id string. |
149 const char* name = device::udev_device_get_sysattr_value(dev, "name"); | 150 const char* name = device::udev_device_get_sysattr_value(dev, "name"); |
150 std::string name_string(name); | 151 std::string name_string(name); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 std::string mapping = "standard"; | 193 std::string mapping = "standard"; |
193 base::TruncateUTF8ToByteSize( | 194 base::TruncateUTF8ToByteSize( |
194 mapping, WebGamepad::mappingLengthCap - 1, &mapping); | 195 mapping, WebGamepad::mappingLengthCap - 1, &mapping); |
195 tmp16 = base::UTF8ToUTF16(mapping); | 196 tmp16 = base::UTF8ToUTF16(mapping); |
196 memset(pad.mapping, 0, sizeof(pad.mapping)); | 197 memset(pad.mapping, 0, sizeof(pad.mapping)); |
197 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); | 198 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); |
198 } else { | 199 } else { |
199 pad.mapping[0] = 0; | 200 pad.mapping[0] = 0; |
200 } | 201 } |
201 | 202 |
| 203 pad_state_[index].axis_mask = 0; |
| 204 pad_state_[index].button_mask = 0; |
| 205 |
202 pad.connected = true; | 206 pad.connected = true; |
203 } | 207 } |
204 } | 208 } |
205 | 209 |
206 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { | 210 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { |
207 device::ScopedUdevEnumeratePtr enumerate( | 211 device::ScopedUdevEnumeratePtr enumerate( |
208 device::udev_enumerate_new(udev_->udev_handle())); | 212 device::udev_enumerate_new(udev_->udev_handle())); |
209 if (!enumerate) | 213 if (!enumerate) |
210 return; | 214 return; |
211 int ret = device::udev_enumerate_add_match_subsystem(enumerate.get(), | 215 int ret = device::udev_enumerate_add_match_subsystem(enumerate.get(), |
(...skipping 19 matching lines...) Expand all Loading... |
231 } | 235 } |
232 } | 236 } |
233 | 237 |
234 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { | 238 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { |
235 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ | 239 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ |
236 if (index >= WebGamepads::itemsLengthCap) { | 240 if (index >= WebGamepads::itemsLengthCap) { |
237 CHECK(false); | 241 CHECK(false); |
238 return; | 242 return; |
239 } | 243 } |
240 | 244 |
241 PadState* state = provider()->GetPadState(GAMEPAD_SOURCE_LINUX_UDEV, index); | 245 const int& fd = device_fd_[index]; |
242 if (!state) | 246 WebGamepad& pad = pad_state_[index].data; |
243 return; | |
244 | |
245 int fd = device_fd_[index]; | |
246 DCHECK_GE(fd, 0); | 247 DCHECK_GE(fd, 0); |
247 | 248 |
248 WebGamepad& pad = state->data; | |
249 | |
250 js_event event; | 249 js_event event; |
251 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) { | 250 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) { |
252 size_t item = event.number; | 251 size_t item = event.number; |
253 if (event.type & JS_EVENT_AXIS) { | 252 if (event.type & JS_EVENT_AXIS) { |
254 if (item >= WebGamepad::axesLengthCap) | 253 if (item >= WebGamepad::axesLengthCap) |
255 continue; | 254 continue; |
256 | 255 |
257 pad.axes[item] = event.value / kMaxLinuxAxisValue; | 256 pad.axes[item] = event.value / kMaxLinuxAxisValue; |
258 | 257 |
259 if (item >= pad.axesLength) | 258 if (item >= pad.axesLength) |
260 pad.axesLength = item + 1; | 259 pad.axesLength = item + 1; |
261 } else if (event.type & JS_EVENT_BUTTON) { | 260 } else if (event.type & JS_EVENT_BUTTON) { |
262 if (item >= WebGamepad::buttonsLengthCap) | 261 if (item >= WebGamepad::buttonsLengthCap) |
263 continue; | 262 continue; |
264 | 263 |
265 pad.buttons[item].pressed = event.value; | 264 pad.buttons[item].pressed = event.value; |
266 pad.buttons[item].value = event.value ? 1.0 : 0.0; | 265 pad.buttons[item].value = event.value ? 1.0 : 0.0; |
267 | 266 |
268 if (item >= pad.buttonsLength) | 267 if (item >= pad.buttonsLength) |
269 pad.buttonsLength = item + 1; | 268 pad.buttonsLength = item + 1; |
270 } | 269 } |
271 pad.timestamp = event.time; | 270 pad.timestamp = event.time; |
272 } | 271 } |
273 } | 272 } |
274 | 273 |
275 } // namespace content | 274 } // namespace content |
OLD | NEW |