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

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

Issue 2081583002: Migrating majority of gamepad from content/browser/ to device/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Next attempt Created 4 years, 5 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_linux.h"
6
7 #include <fcntl.h>
8 #include <linux/joystick.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include "base/macros.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/trace_event/trace_event.h"
22 #include "device/udev_linux/scoped_udev.h"
23 #include "device/udev_linux/udev_linux.h"
24
25 namespace {
26
27 const char kInputSubsystem[] = "input";
28 const char kUsbSubsystem[] = "usb";
29 const char kUsbDeviceType[] = "usb_device";
30 const float kMaxLinuxAxisValue = 32767.0;
31
32 void CloseFileDescriptorIfValid(int fd) {
33 if (fd >= 0)
34 close(fd);
35 }
36
37 bool IsGamepad(udev_device* dev, int* index, std::string* path) {
38 if (!device::udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK"))
39 return false;
40
41 const char* node_path = device::udev_device_get_devnode(dev);
42 if (!node_path)
43 return false;
44
45 static const char kJoystickRoot[] = "/dev/input/js";
46 bool is_gamepad = base::StartsWith(node_path, kJoystickRoot,
47 base::CompareCase::SENSITIVE);
48 if (!is_gamepad)
49 return false;
50
51 int tmp_idx = -1;
52 const int base_len = sizeof(kJoystickRoot) - 1;
53 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len);
54 if (!base::StringToInt(str, &tmp_idx))
55 return false;
56 if (tmp_idx < 0 ||
57 tmp_idx >= static_cast<int>(blink::WebGamepads::itemsLengthCap)) {
58 return false;
59 }
60 *index = tmp_idx;
61 *path = node_path;
62 return true;
63 }
64
65 } // namespace
66
67 namespace content {
68
69 using blink::WebGamepad;
70 using blink::WebGamepads;
71
72 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() {
73 for (size_t i = 0; i < arraysize(pad_state_); ++i) {
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;
78 }
79
80 std::vector<device::UdevLinux::UdevMonitorFilter> filters;
81 filters.push_back(
82 device::UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL));
83 udev_.reset(new device::UdevLinux(
84 filters, base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice,
85 base::Unretained(this))));
86
87 EnumerateDevices();
88 }
89
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) {
96 TRACE_EVENT0("GAMEPAD", "GetGamepadData");
97
98 // Update our internal state.
99 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
100 if (device_fd_[i] >= 0) {
101 ReadDeviceData(i);
102 }
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 }
109 }
110
111 // Used during enumeration, and monitor notifications.
112 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) {
113 int index;
114 std::string node_path;
115 if (IsGamepad(dev, &index, &node_path)) {
116 int& device_fd = device_fd_[index];
117 WebGamepad& pad = pad_state_[index].data;
118 GamepadStandardMappingFunction& mapper = pad_state_[index].mapper;
119
120 CloseFileDescriptorIfValid(device_fd);
121
122 // The device pointed to by dev contains information about the logical
123 // joystick device. In order to get the information about the physical
124 // hardware, get the parent device that is also in the "input" subsystem.
125 // This function should just walk up the tree one level.
126 dev = device::udev_device_get_parent_with_subsystem_devtype(
127 dev, kInputSubsystem, NULL);
128 if (!dev) {
129 // Unable to get device information, don't use this device.
130 device_fd = -1;
131 pad.connected = false;
132 return;
133 }
134
135 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK));
136 if (device_fd < 0) {
137 // Unable to open device, don't use.
138 pad.connected = false;
139 return;
140 }
141
142 const char* vendor_id =
143 device::udev_device_get_sysattr_value(dev, "id/vendor");
144 const char* product_id =
145 device::udev_device_get_sysattr_value(dev, "id/product");
146 mapper = GetGamepadStandardMappingFunction(vendor_id, product_id);
147
148 // 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.
150 const char* name = device::udev_device_get_sysattr_value(dev, "name");
151 std::string name_string(name);
152
153 // In many cases the information the input subsystem contains isn't
154 // as good as the information that the device bus has, walk up further
155 // to the subsystem/device type "usb"/"usb_device" and if this device
156 // has the same vendor/product id, prefer the description from that.
157 struct udev_device* usb_dev =
158 device::udev_device_get_parent_with_subsystem_devtype(
159 dev, kUsbSubsystem, kUsbDeviceType);
160 if (usb_dev) {
161 const char* usb_vendor_id =
162 device::udev_device_get_sysattr_value(usb_dev, "idVendor");
163 const char* usb_product_id =
164 device::udev_device_get_sysattr_value(usb_dev, "idProduct");
165
166 if (strcmp(vendor_id, usb_vendor_id) == 0 &&
167 strcmp(product_id, usb_product_id) == 0) {
168 const char* manufacturer =
169 device::udev_device_get_sysattr_value(usb_dev, "manufacturer");
170 const char* product =
171 device::udev_device_get_sysattr_value(usb_dev, "product");
172
173 // Replace the previous name string with one containing the better
174 // information, again driver returns utf-8 strings here so combine
175 // in utf-8 for conversion to WebUChar below.
176 name_string = base::StringPrintf("%s %s", manufacturer, product);
177 }
178 }
179
180 // Append the vendor and product information then convert the utf-8
181 // id string to WebUChar.
182 std::string id =
183 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)",
184 mapper ? "STANDARD GAMEPAD " : "",
185 vendor_id,
186 product_id);
187 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id);
188 base::string16 tmp16 = base::UTF8ToUTF16(id);
189 memset(pad.id, 0, sizeof(pad.id));
190 tmp16.copy(pad.id, arraysize(pad.id) - 1);
191
192 if (mapper) {
193 std::string mapping = "standard";
194 base::TruncateUTF8ToByteSize(
195 mapping, WebGamepad::mappingLengthCap - 1, &mapping);
196 tmp16 = base::UTF8ToUTF16(mapping);
197 memset(pad.mapping, 0, sizeof(pad.mapping));
198 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1);
199 } else {
200 pad.mapping[0] = 0;
201 }
202
203 pad_state_[index].axis_mask = 0;
204 pad_state_[index].button_mask = 0;
205
206 pad.connected = true;
207 }
208 }
209
210 void GamepadPlatformDataFetcherLinux::EnumerateDevices() {
211 device::ScopedUdevEnumeratePtr enumerate(
212 device::udev_enumerate_new(udev_->udev_handle()));
213 if (!enumerate)
214 return;
215 int ret = device::udev_enumerate_add_match_subsystem(enumerate.get(),
216 kInputSubsystem);
217 if (ret != 0)
218 return;
219 ret = device::udev_enumerate_scan_devices(enumerate.get());
220 if (ret != 0)
221 return;
222
223 udev_list_entry* devices =
224 device::udev_enumerate_get_list_entry(enumerate.get());
225 for (udev_list_entry* dev_list_entry = devices; dev_list_entry != NULL;
226 dev_list_entry = device::udev_list_entry_get_next(dev_list_entry)) {
227 // Get the filename of the /sys entry for the device and create a
228 // udev_device object (dev) representing it
229 const char* path = device::udev_list_entry_get_name(dev_list_entry);
230 device::ScopedUdevDevicePtr dev(
231 device::udev_device_new_from_syspath(udev_->udev_handle(), path));
232 if (!dev)
233 continue;
234 RefreshDevice(dev.get());
235 }
236 }
237
238 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) {
239 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/
240 if (index >= WebGamepads::itemsLengthCap) {
241 CHECK(false);
242 return;
243 }
244
245 const int& fd = device_fd_[index];
246 WebGamepad& pad = pad_state_[index].data;
247 DCHECK_GE(fd, 0);
248
249 js_event event;
250 while (HANDLE_EINTR(read(fd, &event, sizeof(struct js_event))) > 0) {
251 size_t item = event.number;
252 if (event.type & JS_EVENT_AXIS) {
253 if (item >= WebGamepad::axesLengthCap)
254 continue;
255
256 pad.axes[item] = event.value / kMaxLinuxAxisValue;
257
258 if (item >= pad.axesLength)
259 pad.axesLength = item + 1;
260 } else if (event.type & JS_EVENT_BUTTON) {
261 if (item >= WebGamepad::buttonsLengthCap)
262 continue;
263
264 pad.buttons[item].pressed = event.value;
265 pad.buttons[item].value = event.value ? 1.0 : 0.0;
266
267 if (item >= pad.buttonsLength)
268 pad.buttonsLength = item + 1;
269 }
270 pad.timestamp = event.time;
271 }
272 }
273
274 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698