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 "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> |
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" | 14 #include "base/macros.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
(...skipping 20 matching lines...) Expand all Loading... |
36 | 36 |
37 bool IsGamepad(udev_device* dev, int* index, std::string* path) { | 37 bool IsGamepad(udev_device* dev, int* index, std::string* path) { |
38 if (!device::udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK")) | 38 if (!device::udev_device_get_property_value(dev, "ID_INPUT_JOYSTICK")) |
39 return false; | 39 return false; |
40 | 40 |
41 const char* node_path = device::udev_device_get_devnode(dev); | 41 const char* node_path = device::udev_device_get_devnode(dev); |
42 if (!node_path) | 42 if (!node_path) |
43 return false; | 43 return false; |
44 | 44 |
45 static const char kJoystickRoot[] = "/dev/input/js"; | 45 static const char kJoystickRoot[] = "/dev/input/js"; |
46 bool is_gamepad = base::StartsWith(node_path, kJoystickRoot, | 46 bool is_gamepad = |
47 base::CompareCase::SENSITIVE); | 47 base::StartsWith(node_path, kJoystickRoot, base::CompareCase::SENSITIVE); |
48 if (!is_gamepad) | 48 if (!is_gamepad) |
49 return false; | 49 return false; |
50 | 50 |
51 int tmp_idx = -1; | 51 int tmp_idx = -1; |
52 const int base_len = sizeof(kJoystickRoot) - 1; | 52 const int base_len = sizeof(kJoystickRoot) - 1; |
53 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len); | 53 base::StringPiece str(&node_path[base_len], strlen(node_path) - base_len); |
54 if (!base::StringToInt(str, &tmp_idx)) | 54 if (!base::StringToInt(str, &tmp_idx)) |
55 return false; | 55 return false; |
56 if (tmp_idx < 0 || | 56 if (tmp_idx < 0 || |
57 tmp_idx >= static_cast<int>(blink::WebGamepads::itemsLengthCap)) { | 57 tmp_idx >= static_cast<int>(blink::WebGamepads::itemsLengthCap)) { |
58 return false; | 58 return false; |
59 } | 59 } |
60 *index = tmp_idx; | 60 *index = tmp_idx; |
61 *path = node_path; | 61 *path = node_path; |
62 return true; | 62 return true; |
63 } | 63 } |
64 | 64 |
65 } // namespace | 65 } // namespace |
66 | 66 |
67 namespace content { | 67 namespace device { |
68 | 68 |
69 using blink::WebGamepad; | 69 using blink::WebGamepad; |
70 using blink::WebGamepads; | 70 using blink::WebGamepads; |
71 | 71 |
72 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { | 72 GamepadPlatformDataFetcherLinux::GamepadPlatformDataFetcherLinux() { |
73 for (size_t i = 0; i < arraysize(pad_state_); ++i) { | 73 for (size_t i = 0; i < arraysize(pad_state_); ++i) { |
74 device_fd_[i] = -1; | 74 device_fd_[i] = -1; |
75 pad_state_[i].mapper = 0; | 75 pad_state_[i].mapper = 0; |
76 pad_state_[i].axis_mask = 0; | 76 pad_state_[i].axis_mask = 0; |
77 pad_state_[i].button_mask = 0; | 77 pad_state_[i].button_mask = 0; |
78 } | 78 } |
79 | 79 |
80 std::vector<device::UdevLinux::UdevMonitorFilter> filters; | 80 std::vector<UdevLinux::UdevMonitorFilter> filters; |
81 filters.push_back( | 81 filters.push_back(UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); |
82 device::UdevLinux::UdevMonitorFilter(kInputSubsystem, NULL)); | 82 udev_.reset(new UdevLinux( |
83 udev_.reset(new device::UdevLinux( | |
84 filters, base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, | 83 filters, base::Bind(&GamepadPlatformDataFetcherLinux::RefreshDevice, |
85 base::Unretained(this)))); | 84 base::Unretained(this)))); |
86 | 85 |
87 EnumerateDevices(); | 86 EnumerateDevices(); |
88 } | 87 } |
89 | 88 |
90 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { | 89 GamepadPlatformDataFetcherLinux::~GamepadPlatformDataFetcherLinux() { |
91 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) | 90 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) |
92 CloseFileDescriptorIfValid(device_fd_[i]); | 91 CloseFileDescriptorIfValid(device_fd_[i]); |
93 } | 92 } |
(...skipping 11 matching lines...) Expand all Loading... |
105 pads->length = WebGamepads::itemsLengthCap; | 104 pads->length = WebGamepads::itemsLengthCap; |
106 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 105 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
107 MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]); | 106 MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]); |
108 } | 107 } |
109 } | 108 } |
110 | 109 |
111 // Used during enumeration, and monitor notifications. | 110 // Used during enumeration, and monitor notifications. |
112 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { | 111 void GamepadPlatformDataFetcherLinux::RefreshDevice(udev_device* dev) { |
113 int index; | 112 int index; |
114 std::string node_path; | 113 std::string node_path; |
115 if (IsGamepad(dev, &index, &node_path)) { | 114 if (IsGamepad(dev, &index, &node_path)) { |
116 int& device_fd = device_fd_[index]; | 115 int& device_fd = device_fd_[index]; |
117 WebGamepad& pad = pad_state_[index].data; | 116 WebGamepad& pad = pad_state_[index].data; |
118 GamepadStandardMappingFunction& mapper = pad_state_[index].mapper; | 117 GamepadStandardMappingFunction& mapper = pad_state_[index].mapper; |
119 | 118 |
120 CloseFileDescriptorIfValid(device_fd); | 119 CloseFileDescriptorIfValid(device_fd); |
121 | 120 |
122 // The device pointed to by dev contains information about the logical | 121 // The device pointed to by dev contains information about the logical |
123 // joystick device. In order to get the information about the physical | 122 // joystick device. In order to get the information about the physical |
124 // hardware, get the parent device that is also in the "input" subsystem. | 123 // hardware, get the parent device that is also in the "input" subsystem. |
125 // This function should just walk up the tree one level. | 124 // This function should just walk up the tree one level. |
126 dev = device::udev_device_get_parent_with_subsystem_devtype( | 125 dev = udev_device_get_parent_with_subsystem_devtype(dev, kInputSubsystem, |
127 dev, kInputSubsystem, NULL); | 126 NULL); |
128 if (!dev) { | 127 if (!dev) { |
129 // Unable to get device information, don't use this device. | 128 // Unable to get device information, don't use this device. |
130 device_fd = -1; | 129 device_fd = -1; |
131 pad.connected = false; | 130 pad.connected = false; |
132 return; | 131 return; |
133 } | 132 } |
134 | 133 |
135 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); | 134 device_fd = HANDLE_EINTR(open(node_path.c_str(), O_RDONLY | O_NONBLOCK)); |
136 if (device_fd < 0) { | 135 if (device_fd < 0) { |
137 // Unable to open device, don't use. | 136 // Unable to open device, don't use. |
138 pad.connected = false; | 137 pad.connected = false; |
139 return; | 138 return; |
140 } | 139 } |
141 | 140 |
142 const char* vendor_id = | 141 const char* vendor_id = udev_device_get_sysattr_value(dev, "id/vendor"); |
143 device::udev_device_get_sysattr_value(dev, "id/vendor"); | 142 const char* product_id = udev_device_get_sysattr_value(dev, "id/product"); |
144 const char* product_id = | |
145 device::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 = device::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); |
152 | 149 |
153 // In many cases the information the input subsystem contains isn't | 150 // 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 | 151 // 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 | 152 // to the subsystem/device type "usb"/"usb_device" and if this device |
156 // has the same vendor/product id, prefer the description from that. | 153 // has the same vendor/product id, prefer the description from that. |
157 struct udev_device* usb_dev = | 154 struct udev_device* usb_dev = udev_device_get_parent_with_subsystem_devtype( |
158 device::udev_device_get_parent_with_subsystem_devtype( | 155 dev, kUsbSubsystem, kUsbDeviceType); |
159 dev, kUsbSubsystem, kUsbDeviceType); | |
160 if (usb_dev) { | 156 if (usb_dev) { |
161 const char* usb_vendor_id = | 157 const char* usb_vendor_id = |
162 device::udev_device_get_sysattr_value(usb_dev, "idVendor"); | 158 udev_device_get_sysattr_value(usb_dev, "idVendor"); |
163 const char* usb_product_id = | 159 const char* usb_product_id = |
164 device::udev_device_get_sysattr_value(usb_dev, "idProduct"); | 160 udev_device_get_sysattr_value(usb_dev, "idProduct"); |
165 | 161 |
166 if (strcmp(vendor_id, usb_vendor_id) == 0 && | 162 if (strcmp(vendor_id, usb_vendor_id) == 0 && |
167 strcmp(product_id, usb_product_id) == 0) { | 163 strcmp(product_id, usb_product_id) == 0) { |
168 const char* manufacturer = | 164 const char* manufacturer = |
169 device::udev_device_get_sysattr_value(usb_dev, "manufacturer"); | 165 udev_device_get_sysattr_value(usb_dev, "manufacturer"); |
170 const char* product = | 166 const char* product = udev_device_get_sysattr_value(usb_dev, "product"); |
171 device::udev_device_get_sysattr_value(usb_dev, "product"); | |
172 | 167 |
173 // Replace the previous name string with one containing the better | 168 // Replace the previous name string with one containing the better |
174 // information, again driver returns utf-8 strings here so combine | 169 // information, again driver returns utf-8 strings here so combine |
175 // in utf-8 for conversion to WebUChar below. | 170 // in utf-8 for conversion to WebUChar below. |
176 name_string = base::StringPrintf("%s %s", manufacturer, product); | 171 name_string = base::StringPrintf("%s %s", manufacturer, product); |
177 } | 172 } |
178 } | 173 } |
179 | 174 |
180 // Append the vendor and product information then convert the utf-8 | 175 // Append the vendor and product information then convert the utf-8 |
181 // id string to WebUChar. | 176 // id string to WebUChar. |
182 std::string id = | 177 std::string id = |
183 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)", | 178 name_string + base::StringPrintf(" (%sVendor: %s Product: %s)", |
184 mapper ? "STANDARD GAMEPAD " : "", | 179 mapper ? "STANDARD GAMEPAD " : "", |
185 vendor_id, | 180 vendor_id, product_id); |
186 product_id); | |
187 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); | 181 base::TruncateUTF8ToByteSize(id, WebGamepad::idLengthCap - 1, &id); |
188 base::string16 tmp16 = base::UTF8ToUTF16(id); | 182 base::string16 tmp16 = base::UTF8ToUTF16(id); |
189 memset(pad.id, 0, sizeof(pad.id)); | 183 memset(pad.id, 0, sizeof(pad.id)); |
190 tmp16.copy(pad.id, arraysize(pad.id) - 1); | 184 tmp16.copy(pad.id, arraysize(pad.id) - 1); |
191 | 185 |
192 if (mapper) { | 186 if (mapper) { |
193 std::string mapping = "standard"; | 187 std::string mapping = "standard"; |
194 base::TruncateUTF8ToByteSize( | 188 base::TruncateUTF8ToByteSize(mapping, WebGamepad::mappingLengthCap - 1, |
195 mapping, WebGamepad::mappingLengthCap - 1, &mapping); | 189 &mapping); |
196 tmp16 = base::UTF8ToUTF16(mapping); | 190 tmp16 = base::UTF8ToUTF16(mapping); |
197 memset(pad.mapping, 0, sizeof(pad.mapping)); | 191 memset(pad.mapping, 0, sizeof(pad.mapping)); |
198 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); | 192 tmp16.copy(pad.mapping, arraysize(pad.mapping) - 1); |
199 } else { | 193 } else { |
200 pad.mapping[0] = 0; | 194 pad.mapping[0] = 0; |
201 } | 195 } |
202 | 196 |
203 pad_state_[index].axis_mask = 0; | 197 pad_state_[index].axis_mask = 0; |
204 pad_state_[index].button_mask = 0; | 198 pad_state_[index].button_mask = 0; |
205 | 199 |
206 pad.connected = true; | 200 pad.connected = true; |
207 } | 201 } |
208 } | 202 } |
209 | 203 |
210 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { | 204 void GamepadPlatformDataFetcherLinux::EnumerateDevices() { |
211 device::ScopedUdevEnumeratePtr enumerate( | 205 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_->udev_handle())); |
212 device::udev_enumerate_new(udev_->udev_handle())); | |
213 if (!enumerate) | 206 if (!enumerate) |
214 return; | 207 return; |
215 int ret = device::udev_enumerate_add_match_subsystem(enumerate.get(), | 208 int ret = |
216 kInputSubsystem); | 209 udev_enumerate_add_match_subsystem(enumerate.get(), kInputSubsystem); |
217 if (ret != 0) | 210 if (ret != 0) |
218 return; | 211 return; |
219 ret = device::udev_enumerate_scan_devices(enumerate.get()); | 212 ret = udev_enumerate_scan_devices(enumerate.get()); |
220 if (ret != 0) | 213 if (ret != 0) |
221 return; | 214 return; |
222 | 215 |
223 udev_list_entry* devices = | 216 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); |
224 device::udev_enumerate_get_list_entry(enumerate.get()); | |
225 for (udev_list_entry* dev_list_entry = devices; dev_list_entry != NULL; | 217 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)) { | 218 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { |
227 // Get the filename of the /sys entry for the device and create a | 219 // Get the filename of the /sys entry for the device and create a |
228 // udev_device object (dev) representing it | 220 // udev_device object (dev) representing it |
229 const char* path = device::udev_list_entry_get_name(dev_list_entry); | 221 const char* path = udev_list_entry_get_name(dev_list_entry); |
230 device::ScopedUdevDevicePtr dev( | 222 ScopedUdevDevicePtr dev( |
231 device::udev_device_new_from_syspath(udev_->udev_handle(), path)); | 223 udev_device_new_from_syspath(udev_->udev_handle(), path)); |
232 if (!dev) | 224 if (!dev) |
233 continue; | 225 continue; |
234 RefreshDevice(dev.get()); | 226 RefreshDevice(dev.get()); |
235 } | 227 } |
236 } | 228 } |
237 | 229 |
238 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { | 230 void GamepadPlatformDataFetcherLinux::ReadDeviceData(size_t index) { |
239 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ | 231 // Linker does not like CHECK_LT(index, WebGamepads::itemsLengthCap). =/ |
240 if (index >= WebGamepads::itemsLengthCap) { | 232 if (index >= WebGamepads::itemsLengthCap) { |
241 CHECK(false); | 233 CHECK(false); |
(...skipping 22 matching lines...) Expand all Loading... |
264 pad.buttons[item].pressed = event.value; | 256 pad.buttons[item].pressed = event.value; |
265 pad.buttons[item].value = event.value ? 1.0 : 0.0; | 257 pad.buttons[item].value = event.value ? 1.0 : 0.0; |
266 | 258 |
267 if (item >= pad.buttonsLength) | 259 if (item >= pad.buttonsLength) |
268 pad.buttonsLength = item + 1; | 260 pad.buttonsLength = item + 1; |
269 } | 261 } |
270 pad.timestamp = event.time; | 262 pad.timestamp = event.time; |
271 } | 263 } |
272 } | 264 } |
273 | 265 |
274 } // namespace content | 266 } // namespace device |
OLD | NEW |