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

Side by Side Diff: content/browser/gamepad/data_fetcher_mac.mm

Issue 8899017: Add gamepad data fetcher for Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again, bots all updated Created 8 years, 11 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 | « content/browser/gamepad/data_fetcher_mac.h ('k') | content/browser/gamepad/data_fetcher_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/data_fetcher_mac.h"
6
7 #include "base/mac/foundation_util.h"
8 #include "base/memory/scoped_nsobject.h"
9 #include "base/string16.h"
10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h"
12
13 #include <IOKit/hid/IOHIDKeys.h>
14 #import <Foundation/Foundation.h>
15
16 namespace content {
17
18 namespace {
19
20 NSDictionary* DeviceMatching(uint32_t usage_page, uint32_t usage) {
21 return [NSDictionary dictionaryWithObjectsAndKeys:
22 [NSNumber numberWithUnsignedInt:usage_page],
23 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsagePageKey)),
24 [NSNumber numberWithUnsignedInt:usage],
25 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsageKey)),
26 nil];
27 }
28
29 float NormalizeAxis(CFIndex value, CFIndex min, CFIndex max) {
30 return (2.f * (value - min) / static_cast<float>(max - min)) - 1.f;
31 }
32
33 // http://www.usb.org/developers/hidpage
34 const uint32_t kGenericDesktopUsagePage = 0x01;
35 const uint32_t kButtonUsagePage = 0x09;
36 const uint32_t kJoystickUsageNumber = 0x04;
37 const uint32_t kGameUsageNumber = 0x05;
38 const uint32_t kMultiAxisUsageNumber = 0x08;
39 const uint32_t kAxisMinimumUsageNumber = 0x30;
40 const uint32_t kAxisMaximumUsageNumber = 0x35;
41
42 } // namespace
43
44 GamepadDataFetcherMac::GamepadDataFetcherMac() : enabled_(true) {
45 hid_manager_ref_.reset(IOHIDManagerCreate(kCFAllocatorDefault,
46 kIOHIDOptionsTypeNone));
47 if (CFGetTypeID(hid_manager_ref_) != IOHIDManagerGetTypeID()) {
48 enabled_ = false;
49 return;
50 }
51
52 scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects:
53 DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber),
54 DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber),
55 DeviceMatching(kGenericDesktopUsagePage, kMultiAxisUsageNumber),
56 nil]);
57 IOHIDManagerSetDeviceMatchingMultiple(
58 hid_manager_ref_,
59 base::mac::NSToCFCast(criteria));
60
61 RegisterForNotifications();
62 }
63
64 void GamepadDataFetcherMac::RegisterForNotifications() {
65 // Register for plug/unplug notifications.
66 IOHIDManagerRegisterDeviceMatchingCallback(
67 hid_manager_ref_,
68 &DeviceAddCallback,
69 this);
70 IOHIDManagerRegisterDeviceRemovalCallback(
71 hid_manager_ref_,
72 DeviceRemoveCallback,
73 this);
74
75 // Register for value change notifications.
76 IOHIDManagerRegisterInputValueCallback(
77 hid_manager_ref_,
78 ValueChangedCallback,
79 this);
80
81 IOHIDManagerScheduleWithRunLoop(
82 hid_manager_ref_,
83 CFRunLoopGetMain(),
84 kCFRunLoopDefaultMode);
85
86 enabled_ = IOHIDManagerOpen(hid_manager_ref_,
87 kIOHIDOptionsTypeSeizeDevice) == kIOReturnSuccess;
88 }
89
90 void GamepadDataFetcherMac::UnregisterFromNotifications() {
91 IOHIDManagerUnscheduleFromRunLoop(
92 hid_manager_ref_,
93 CFRunLoopGetCurrent(),
94 kCFRunLoopDefaultMode);
95 IOHIDManagerClose(hid_manager_ref_, kIOHIDOptionsTypeNone);
96 }
97
98 void GamepadDataFetcherMac::PauseHint(bool pause) {
99 if (pause)
100 UnregisterFromNotifications();
101 else
102 RegisterForNotifications();
103 }
104
105 GamepadDataFetcherMac::~GamepadDataFetcherMac() {
106 UnregisterFromNotifications();
107 }
108
109 GamepadDataFetcherMac* GamepadDataFetcherMac::InstanceFromContext(
110 void* context) {
111 return reinterpret_cast<GamepadDataFetcherMac*>(context);
112 }
113
114 void GamepadDataFetcherMac::DeviceAddCallback(void* context,
115 IOReturn result,
116 void* sender,
117 IOHIDDeviceRef ref) {
118 InstanceFromContext(context)->DeviceAdd(ref);
119 }
120
121 void GamepadDataFetcherMac::DeviceRemoveCallback(void* context,
122 IOReturn result,
123 void* sender,
124 IOHIDDeviceRef ref) {
125 InstanceFromContext(context)->DeviceRemove(ref);
126 }
127
128 void GamepadDataFetcherMac::ValueChangedCallback(void* context,
129 IOReturn result,
130 void* sender,
131 IOHIDValueRef ref) {
132 InstanceFromContext(context)->ValueChanged(ref);
133 }
134
135 void GamepadDataFetcherMac::AddButtonsAndAxes(NSArray* elements,
136 size_t slot) {
137 WebKit::WebGamepad& pad = data_.items[slot];
138 AssociatedData& associated = associated_[slot];
139
140 pad.axesLength = 0;
141 pad.buttonsLength = 0;
142 memset(pad.axes, 0, sizeof(pad.axes));
143 memset(pad.buttons, 0, sizeof(pad.buttons));
144
145 for (id elem in elements) {
146 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem);
147 uint32_t usagePage = IOHIDElementGetUsagePage(element);
148 uint32_t usage = IOHIDElementGetUsage(element);
149 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button &&
150 usagePage == kButtonUsagePage) {
151 uint32_t button_index = usage - 1;
152 if (button_index < WebKit::WebGamepad::buttonsLengthCap) {
153 associated.button_elements[button_index] = element;
154 pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1);
155 }
156 }
157 else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) {
158 uint32_t axis_index = usage - kAxisMinimumUsageNumber;
159 if (axis_index < WebKit::WebGamepad::axesLengthCap) {
160 associated.axis_minimums[axis_index] =
161 IOHIDElementGetLogicalMin(element);
162 associated.axis_maximums[axis_index] =
163 IOHIDElementGetLogicalMax(element);
164 associated.axis_elements[axis_index] = element;
165 pad.axesLength = std::max(pad.axesLength, axis_index + 1);
166 }
167 }
168 }
169 }
170
171 void GamepadDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) {
172 using WebKit::WebGamepad;
173 using WebKit::WebGamepads;
174 using base::mac::CFToNSCast;
175 using base::mac::CFCastStrict;
176 size_t slot;
177
178 if (!enabled_)
179 return;
180
181 // Find an index for this device.
182 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
183 // If we already have this device, and it's already connected, don't do
184 // anything now.
185 if (associated_[slot].device_ref == device && data_.items[slot].connected)
186 return;
187 if (!data_.items[slot].connected)
188 break;
189 }
190
191 // We can't handle this many connected devices.
192 if (slot == WebGamepads::itemsLengthCap)
193 return;
194
195 NSNumber* vendor_id = CFToNSCast(CFCastStrict<CFNumberRef>(
196 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey))));
197 NSNumber* product_id = CFToNSCast(CFCastStrict<CFNumberRef>(
198 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey))));
199 NSString* product = CFToNSCast(CFCastStrict<CFStringRef>(
200 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey))));
201
202 NSString* ident([NSString stringWithFormat:
203 @"%@ (Vendor: %04x Product: %04x)",
204 product,
205 [vendor_id intValue],
206 [product_id intValue]]);
207 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding];
208
209 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id);
210 memset(&data_.items[slot].id, 0, kOutputLengthBytes);
211 [as16 getBytes:data_.items[slot].id
212 length:kOutputLengthBytes - sizeof(WebKit::WebUChar)];
213
214 base::mac::ScopedCFTypeRef<CFArrayRef> elements(
215 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone));
216 AddButtonsAndAxes(CFToNSCast(elements), slot);
217
218 associated_[slot].device_ref = device;
219 data_.items[slot].connected = true;
220 if (slot >= data_.length)
221 data_.length = slot + 1;
222 }
223
224 void GamepadDataFetcherMac::DeviceRemove(IOHIDDeviceRef device) {
225 using WebKit::WebGamepads;
226 size_t slot;
227 if (!enabled_)
228 return;
229
230 // Find the index for this device.
231 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
232 if (associated_[slot].device_ref == device && data_.items[slot].connected)
233 break;
234 }
235 DCHECK(slot < WebGamepads::itemsLengthCap);
236 // Leave associated device_ref so that it will be reconnected in the same
237 // location. Simply mark it as disconnected.
238 data_.items[slot].connected = false;
239 }
240
241 void GamepadDataFetcherMac::ValueChanged(IOHIDValueRef value) {
242 if (!enabled_)
243 return;
244
245 IOHIDElementRef element = IOHIDValueGetElement(value);
246 IOHIDDeviceRef device = IOHIDElementGetDevice(element);
247
248 // Find device slot.
249 size_t slot;
250 for (slot = 0; slot < data_.length; ++slot) {
251 if (data_.items[slot].connected && associated_[slot].device_ref == device)
252 break;
253 }
254 if (slot == data_.length)
255 return;
256
257 WebKit::WebGamepad& pad = data_.items[slot];
258 AssociatedData& associated = associated_[slot];
259
260 // Find and fill in the associated button event, if any.
261 for (size_t i = 0; i < pad.buttonsLength; ++i) {
262 if (associated.button_elements[i] == element) {
263 pad.buttons[i] = IOHIDValueGetIntegerValue(value) ? 1.f : 0.f;
264 return;
265 }
266 }
267
268 // Find and fill in the associated axis event, if any.
269 for (size_t i = 0; i < pad.axesLength; ++i) {
270 if (associated.axis_elements[i] == element) {
271 pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value),
272 associated.axis_minimums[i],
273 associated.axis_maximums[i]);
274 return;
275 }
276 }
277 }
278
279 void GamepadDataFetcherMac::GetGamepadData(WebKit::WebGamepads* pads, bool) {
280 if (!enabled_) {
281 pads->length = 0;
282 return;
283 }
284 memcpy(pads, &data_, sizeof(WebKit::WebGamepads));
285 }
286
287 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/gamepad/data_fetcher_mac.h ('k') | content/browser/gamepad/data_fetcher_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698