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/platform_data_fetcher_linux.h" | 5 #include "content/browser/gamepad/platform_data_fetcher_linux.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <libudev.h> | 8 #include <libudev.h> |
9 #include <linux/joystick.h> | 9 #include <linux/joystick.h> |
10 #include <string.h> | 10 #include <string.h> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <sys/types.h> | 12 #include <sys/types.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 | 14 |
15 #include "base/debug/trace_event.h" | 15 #include "base/debug/trace_event.h" |
16 #include "base/eintr_wrapper.h" | 16 #include "base/eintr_wrapper.h" |
17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/stringprintf.h" | 20 #include "base/stringprintf.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kInputSubsystem[] = "input"; | 25 const char kInputSubsystem[] = "input"; |
26 const char kUsbSubsystem[] = "usb"; | |
27 const char kUsbDeviceType[] = "usb_device"; | |
26 const float kMaxLinuxAxisValue = 32767.0; | 28 const float kMaxLinuxAxisValue = 32767.0; |
27 | 29 |
28 void CloseFileDescriptorIfValid(int fd) { | 30 void CloseFileDescriptorIfValid(int fd) { |
29 if (fd >= 0) | 31 if (fd >= 0) |
30 close(fd); | 32 close(fd); |
31 } | 33 } |
32 | 34 |
33 bool IsGamepad(udev_device* dev, int* index, std::string* path) { | 35 bool IsGamepad(udev_device* dev, int* index, std::string* path) { |
34 if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK")) | 36 if (!udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK")) |
35 return false; | 37 return false; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { | 141 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { |
140 int index; | 142 int index; |
141 std::string node_path; | 143 std::string node_path; |
142 if (IsGamepad(dev, &index, &node_path)) { | 144 if (IsGamepad(dev, &index, &node_path)) { |
143 int& device_fd = device_fds_[index]; | 145 int& device_fd = device_fds_[index]; |
144 WebGamepad& pad = data_.items[index]; | 146 WebGamepad& pad = data_.items[index]; |
145 GamepadStandardMappingFunction& mapper = mappers_[index]; | 147 GamepadStandardMappingFunction& mapper = mappers_[index]; |
146 | 148 |
147 CloseFileDescriptorIfValid(device_fd); | 149 CloseFileDescriptorIfValid(device_fd); |
148 | 150 |
149 // The device pointed to by dev contains information about the input | 151 // The device pointed to by dev contains information about the logical |
150 // device. In order to get the information about the USB device, get the | 152 // joystick device. In order to get the information about the physical |
151 // parent device with the subsystem/devtype pair of "usb"/"usb_device". | 153 // hardware, get the parent device that is also in the "input" subsystem. |
152 // This function walks up the tree several levels. | 154 // This function should just walk up the tree one level. |
153 dev = udev_device_get_parent_with_subsystem_devtype( | 155 dev = udev_device_get_parent_with_subsystem_devtype( |
154 dev, | 156 dev, |
155 "usb", | 157 kInputSubsystem, |
156 "usb_device"); | 158 NULL); |
157 if (!dev) { | 159 if (!dev) { |
158 // Unable to get device information, don't use this device. | 160 // Unable to get device information, don't use this device. |
159 device_fd = -1; | 161 device_fd = -1; |
160 pad.connected = false; | 162 pad.connected = false; |
161 return; | 163 return; |
162 } | 164 } |
163 | 165 |
164 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); | 166 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); |
165 if (device_fd < 0) { | 167 if (device_fd < 0) { |
166 // Unable to open device, don't use. | 168 // Unable to open device, don't use. |
167 pad.connected = false; | 169 pad.connected = false; |
168 return; | 170 return; |
169 } | 171 } |
170 | 172 |
171 const char* vendor_id = udev_device_get_sysattr_value(dev, "idVendor"); | 173 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); |
172 const char* product_id = udev_device_get_sysattr_value(dev, "idProduct"); | 174 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); |
173 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); | 175 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id); |
174 | 176 |
175 const char* manufacturer = | 177 // Driver returns utf-8 strings here, so combine in utf-8 first and |
176 udev_device_get_sysattr_value(dev, "manufacturer"); | 178 // convert to WebUChar later once we've picked an id string. |
177 const char* product = udev_device_get_sysattr_value(dev, "product"); | 179 const char* name = udev_device_get_sysattr_value(dev, "name"); |
178 | 180 std::string id = base::StringPrintf("%s (%sVendor: %s Product: %s)", |
scottmg
2012/05/03 17:37:38
save name and remainder separately to avoid duplic
| |
179 // Driver returns utf-8 strings here, so combine in utf-8 and then convert | 181 name, |
180 // to WebUChar to build the id string. | |
181 std::string id = base::StringPrintf("%s %s (%sVendor: %s Product: %s)", | |
182 manufacturer, | |
183 product, | |
184 mapper ? "STANDARD GAMEPAD " : "", | 182 mapper ? "STANDARD GAMEPAD " : "", |
185 vendor_id, | 183 vendor_id, |
186 product_id); | 184 product_id); |
185 | |
186 // In many cases the information the input subsystem contains isn't | |
187 // as good as the information that the device bus has, walk up further | |
188 // to the subsystem/device type "usb"/"usb_device" and if this device | |
189 // has the same vendor/product id, prefer the description from that. | |
190 struct udev_device *usb_dev = udev_device_get_parent_with_subsystem_devtype( | |
191 dev, | |
192 kUsbSubsystem, | |
193 kUsbDeviceType); | |
194 if (usb_dev) { | |
195 const char* usb_vendor_id = | |
196 udev_device_get_sysattr_value(usb_dev, "idVendor"); | |
197 const char* usb_product_id = | |
198 udev_device_get_sysattr_value(usb_dev, "idProduct"); | |
199 | |
200 if (strcmp(vendor_id, usb_vendor_id) == 0 && | |
201 strcmp(product_id, usb_product_id) == 0) { | |
202 const char* manufacturer = | |
203 udev_device_get_sysattr_value(usb_dev, "manufacturer"); | |
204 const char* product = udev_device_get_sysattr_value(usb_dev, "product"); | |
205 | |
206 // Replace the previous id string with one containing the better | |
207 // information, again driver returns utf-8 strings here so combine | |
208 // in utf-8 for conversion to WebUChar below. | |
209 id = base::StringPrintf("%s %s (%sVendor: %s Product: %s)", | |
210 manufacturer, | |
211 product, | |
212 mapper ? "STANDARD GAMEPAD " : "", | |
213 vendor_id, | |
214 product_id); | |
215 } | |
216 } | |
217 | |
218 // Convert the utf-8 id string to WebUChar. | |
187 TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); | 219 TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); |
188 string16 tmp16 = UTF8ToUTF16(id); | 220 string16 tmp16 = UTF8ToUTF16(id); |
189 memset(pad.id, 0, sizeof(pad.id)); | 221 memset(pad.id, 0, sizeof(pad.id)); |
190 tmp16.copy(pad.id, arraysize(pad.id) - 1); | 222 tmp16.copy(pad.id, arraysize(pad.id) - 1); |
191 | 223 |
192 pad.connected = true; | 224 pad.connected = true; |
193 } | 225 } |
194 } | 226 } |
195 | 227 |
196 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { | 228 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 continue; | 278 continue; |
247 pad.buttons[item] = event.value ? 1.0 : 0.0; | 279 pad.buttons[item] = event.value ? 1.0 : 0.0; |
248 if (item >= pad.buttonsLength) | 280 if (item >= pad.buttonsLength) |
249 pad.buttonsLength = item + 1; | 281 pad.buttonsLength = item + 1; |
250 } | 282 } |
251 pad.timestamp = event.time; | 283 pad.timestamp = event.time; |
252 } | 284 } |
253 } | 285 } |
254 | 286 |
255 } // namespace content | 287 } // namespace content |
OLD | NEW |