Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: content/browser/gamepad/platform_data_fetcher_linux.cc

Issue 10346009: gamepad: linux: use input device parent, not USB (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: separate the extension of the string Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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");
180 std::string name_string = base::StringPrintf("%s", name);
178 181
179 // Driver returns utf-8 strings here, so combine in utf-8 and then convert 182 // In many cases the information the input subsystem contains isn't
180 // to WebUChar to build the id string. 183 // as good as the information that the device bus has, walk up further
181 std::string id = base::StringPrintf("%s %s (%sVendor: %s Product: %s)", 184 // to the subsystem/device type "usb"/"usb_device" and if this device
182 manufacturer, 185 // has the same vendor/product id, prefer the description from that.
183 product, 186 struct udev_device *usb_dev = udev_device_get_parent_with_subsystem_devtype(
184 mapper ? "STANDARD GAMEPAD " : "", 187 dev,
185 vendor_id, 188 kUsbSubsystem,
186 product_id); 189 kUsbDeviceType);
190 if (usb_dev) {
191 const char* usb_vendor_id =
192 udev_device_get_sysattr_value(usb_dev, "idVendor");
193 const char* usb_product_id =
194 udev_device_get_sysattr_value(usb_dev, "idProduct");
195
196 if (strcmp(vendor_id, usb_vendor_id) == 0 &&
197 strcmp(product_id, usb_product_id) == 0) {
198 const char* manufacturer =
199 udev_device_get_sysattr_value(usb_dev, "manufacturer");
200 const char* product = udev_device_get_sysattr_value(usb_dev, "product");
201
202 // Replace the previous name string with one containing the better
203 // information, again driver returns utf-8 strings here so combine
204 // in utf-8 for conversion to WebUChar below.
205 name_string = base::StringPrintf("%s %s", manufacturer, product);
206 }
207 }
208
209 // Append the vendor and product information then convert the utf-8
210 // id string to WebUChar.
211 std::string id = name_string + base::StringPrintf(
212 " (%sVendor: %s Product: %s)",
213 mapper ? "STANDARD GAMEPAD " : "",
214 vendor_id,
215 product_id);
187 TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); 216 TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id);
188 string16 tmp16 = UTF8ToUTF16(id); 217 string16 tmp16 = UTF8ToUTF16(id);
189 memset(pad.id, 0, sizeof(pad.id)); 218 memset(pad.id, 0, sizeof(pad.id));
190 tmp16.copy(pad.id, arraysize(pad.id) - 1); 219 tmp16.copy(pad.id, arraysize(pad.id) - 1);
191 220
192 pad.connected = true; 221 pad.connected = true;
193 } 222 }
194 } 223 }
195 224
196 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { 225 void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 continue; 275 continue;
247 pad.buttons[item] = event.value ? 1.0 : 0.0; 276 pad.buttons[item] = event.value ? 1.0 : 0.0;
248 if (item >= pad.buttonsLength) 277 if (item >= pad.buttonsLength)
249 pad.buttonsLength = item + 1; 278 pad.buttonsLength = item + 1;
250 } 279 }
251 pad.timestamp = event.time; 280 pad.timestamp = event.time;
252 } 281 }
253 } 282 }
254 283
255 } // namespace content 284 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698