| 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_mac.h" | 5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_mac.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 InstanceFromContext(context)->DeviceRemove(ref); | 149 InstanceFromContext(context)->DeviceRemove(ref); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void GamepadPlatformDataFetcherMac::ValueChangedCallback(void* context, | 152 void GamepadPlatformDataFetcherMac::ValueChangedCallback(void* context, |
| 153 IOReturn result, | 153 IOReturn result, |
| 154 void* sender, | 154 void* sender, |
| 155 IOHIDValueRef ref) { | 155 IOHIDValueRef ref) { |
| 156 InstanceFromContext(context)->ValueChanged(ref); | 156 InstanceFromContext(context)->ValueChanged(ref); |
| 157 } | 157 } |
| 158 | 158 |
| 159 bool GamepadPlatformDataFetcherMac::CheckCollection(IOHIDElementRef element) { |
| 160 // Check that a parent collection of this element matches one of the usage |
| 161 // numbers that we are looking for. |
| 162 while ((element = IOHIDElementGetParent(element)) != NULL) { |
| 163 uint32_t usage_page = IOHIDElementGetUsagePage(element); |
| 164 uint32_t usage = IOHIDElementGetUsage(element); |
| 165 if (usage_page == kGenericDesktopUsagePage) { |
| 166 if (usage == kJoystickUsageNumber || |
| 167 usage == kGameUsageNumber || |
| 168 usage == kMultiAxisUsageNumber) { |
| 169 return true; |
| 170 } |
| 171 } |
| 172 } |
| 173 return false; |
| 174 } |
| 175 |
| 159 bool GamepadPlatformDataFetcherMac::AddButtonsAndAxes(NSArray* elements, | 176 bool GamepadPlatformDataFetcherMac::AddButtonsAndAxes(NSArray* elements, |
| 160 size_t slot) { | 177 size_t slot) { |
| 161 WebGamepad& pad = pad_state_[slot].data; | 178 WebGamepad& pad = pad_state_[slot].data; |
| 162 AssociatedData& associated = associated_[slot]; | 179 AssociatedData& associated = associated_[slot]; |
| 163 CHECK(!associated.is_xbox); | 180 CHECK(!associated.is_xbox); |
| 164 | 181 |
| 165 pad.axesLength = 0; | 182 pad.axesLength = 0; |
| 166 pad.buttonsLength = 0; | 183 pad.buttonsLength = 0; |
| 167 pad.timestamp = 0; | 184 pad.timestamp = 0; |
| 168 memset(pad.axes, 0, sizeof(pad.axes)); | 185 memset(pad.axes, 0, sizeof(pad.axes)); |
| 169 memset(pad.buttons, 0, sizeof(pad.buttons)); | 186 memset(pad.buttons, 0, sizeof(pad.buttons)); |
| 170 | 187 |
| 171 bool mapped_all_axes = true; | 188 bool mapped_all_axes = true; |
| 172 | 189 |
| 173 for (id elem in elements) { | 190 for (id elem in elements) { |
| 174 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); | 191 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); |
| 192 if (!CheckCollection(element)) |
| 193 continue; |
| 194 |
| 175 uint32_t usage_page = IOHIDElementGetUsagePage(element); | 195 uint32_t usage_page = IOHIDElementGetUsagePage(element); |
| 176 uint32_t usage = IOHIDElementGetUsage(element); | 196 uint32_t usage = IOHIDElementGetUsage(element); |
| 177 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button && | 197 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button && |
| 178 usage_page == kButtonUsagePage) { | 198 usage_page == kButtonUsagePage) { |
| 179 uint32_t button_index = usage - 1; | 199 uint32_t button_index = usage - 1; |
| 180 if (button_index < WebGamepad::buttonsLengthCap) { | 200 if (button_index < WebGamepad::buttonsLengthCap) { |
| 181 associated.hid.button_elements[button_index] = element; | 201 associated.hid.button_elements[button_index] = element; |
| 182 pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1); | 202 pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1); |
| 183 } | 203 } |
| 184 } | 204 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 195 mapped_all_axes = false; | 215 mapped_all_axes = false; |
| 196 } | 216 } |
| 197 } | 217 } |
| 198 } | 218 } |
| 199 | 219 |
| 200 if (!mapped_all_axes) { | 220 if (!mapped_all_axes) { |
| 201 // For axes who's usage puts them outside the standard axesLengthCap range. | 221 // For axes who's usage puts them outside the standard axesLengthCap range. |
| 202 uint32_t next_index = 0; | 222 uint32_t next_index = 0; |
| 203 for (id elem in elements) { | 223 for (id elem in elements) { |
| 204 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); | 224 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); |
| 225 if (!CheckCollection(element)) |
| 226 continue; |
| 227 |
| 205 uint32_t usage_page = IOHIDElementGetUsagePage(element); | 228 uint32_t usage_page = IOHIDElementGetUsagePage(element); |
| 206 uint32_t usage = IOHIDElementGetUsage(element); | 229 uint32_t usage = IOHIDElementGetUsage(element); |
| 207 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc && | 230 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc && |
| 208 usage - kAxisMinimumUsageNumber >= WebGamepad::axesLengthCap && | 231 usage - kAxisMinimumUsageNumber >= WebGamepad::axesLengthCap && |
| 209 usage_page <= kGameControlsUsagePage) { | 232 usage_page <= kGameControlsUsagePage) { |
| 210 for (; next_index < WebGamepad::axesLengthCap; ++next_index) { | 233 for (; next_index < WebGamepad::axesLengthCap; ++next_index) { |
| 211 if (associated.hid.axis_elements[next_index] == NULL) | 234 if (associated.hid.axis_elements[next_index] == NULL) |
| 212 break; | 235 break; |
| 213 } | 236 } |
| 214 if (next_index < WebGamepad::axesLengthCap) { | 237 if (next_index < WebGamepad::axesLengthCap) { |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 pads->length = 0; | 524 pads->length = 0; |
| 502 return; | 525 return; |
| 503 } | 526 } |
| 504 | 527 |
| 505 pads->length = WebGamepads::itemsLengthCap; | 528 pads->length = WebGamepads::itemsLengthCap; |
| 506 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) | 529 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) |
| 507 MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]); | 530 MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]); |
| 508 } | 531 } |
| 509 | 532 |
| 510 } // namespace content | 533 } // namespace content |
| OLD | NEW |