Diff
Modified: trunk/Source/WebCore/ChangeLog (265078 => 265079)
--- trunk/Source/WebCore/ChangeLog 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/ChangeLog 2020-07-30 07:21:44 UTC (rev 265079)
@@ -1,3 +1,132 @@
+2020-07-30 Brady Eidson <[email protected]>
+
+ Refactor HID gamepad code to be much less fragile and much easier to hack on.
+ https://bugs.webkit.org/show_bug.cgi?id=214910
+
+ Reviewed by Darin Adler.
+
+ Covered by API tests.
+
+ Currently - for HID device gamepads - we throw all buttons and axes against the wall and see what sticks.
+ Instead, for specific popular devices, we'd like to have device-specific mappings so the representation makes sense.
+
+ To support that work, this is a major refactoring of the HID gamepad code. It does the following:
+
+ - Breaks out logic specific to IOHIDDeviceRef into HIDDevice
+
+ - Breaks out logic specific to IOHIDElementRef into HIDElement
+
+ - Moves responsibities of managing things to the right places (e.g. HIDElement manages its own current value directly)
+
+ - Makes HIDGamepadElement derive from HIDElement directly
+
+ - Moves device specific logic from HIDGamepad into a subclass of HID gamepad. Currently the only subclass is "generic"
+ which encompasses HIDGamepad's old behavior
+
+ - Changes button/value vectors from Vector<double> to Vector<SharedGamepadValue> so multiple objects can reference
+ the value at once.
+ e.g. HIDGamepadButton can simply update it's own SharedGamepadValue without knowing which HIDGamepad owns it or which index
+ into the gamepad's value vector it should be mutating.
+
+ This will be critical in the "specific device mapping" work as some types of HIDElements actually drive more than one
+ gamepad button value.
+ e.g. a "direction pad axis" actually manages 2 button values, or a "hat-switch" direction pad manages 4 button values
+
+ This patch doesn't change any current behavior.
+
+ * Modules/gamepad/Gamepad.cpp:
+ (WebCore::Gamepad::updateFromPlatformGamepad):
+
+ * Sources.txt:
+ * SourcesCocoa.txt:
+ * WebCore.xcodeproj/project.pbxproj:
+
+ * platform/Logging.h:
+
+ * platform/gamepad/PlatformGamepad.h:
+
+ * platform/gamepad/SharedGamepadValue.h: Copied from Source/WebKit/UIProcess/Gamepad/UIGamepad.h.
+ (WebCore::SharedGamepadValue::SharedGamepadValue):
+ (WebCore::SharedGamepadValue::setValue):
+ (WebCore::SharedGamepadValue::value const):
+ (WebCore::SharedGamepadValue::Data::Data):
+
+ * platform/gamepad/cocoa/GameControllerGamepad.h:
+ * platform/gamepad/cocoa/GameControllerGamepad.mm:
+ (WebCore::GameControllerGamepad::setupAsExtendedGamepad):
+ (WebCore::GameControllerGamepad::setupAsGamepad):
+
+ * platform/gamepad/mac/GenericHIDGamepad.cpp: Added.
+ (WebCore::GenericHIDGamepad::GenericHIDGamepad):
+ (WebCore::GenericHIDGamepad::id):
+ (WebCore::GenericHIDGamepad::maybeAddGenericDesktopElement):
+ (WebCore::GenericHIDGamepad::maybeAddButtonElement):
+ * platform/gamepad/mac/GenericHIDGamepad.h: Copied from Source/WebKit/WebProcess/Gamepad/WebGamepad.h.
+
+ * platform/gamepad/mac/HIDGamepad.cpp:
+ (WebCore::HIDGamepad::create):
+ (WebCore::HIDGamepad::HIDGamepad):
+ (WebCore::HIDGamepad::initialize):
+ (WebCore::HIDGamepad::valueChanged):
+ (WebCore::HIDGamepad::getCurrentValueForElement): Deleted.
+ (WebCore::HIDGamepad::initElements): Deleted.
+ (WebCore::HIDGamepad::initElementsFromArray): Deleted.
+ (WebCore::HIDGamepad::maybeAddButton): Deleted.
+ (WebCore::HIDGamepad::maybeAddAxis): Deleted.
+ * platform/gamepad/mac/HIDGamepad.h:
+ (WebCore::HIDGamepad::hidDevice const):
+ (WebCore::HIDGamepadElement::HIDGamepadElement): Deleted.
+ (WebCore::HIDGamepadElement::~HIDGamepadElement): Deleted.
+ (WebCore::HIDGamepadElement::isButton const): Deleted.
+ (WebCore::HIDGamepadElement::isAxis const): Deleted.
+ (WebCore::HIDGamepadButton::HIDGamepadButton): Deleted.
+ (WebCore::HIDGamepadAxis::HIDGamepadAxis): Deleted.
+
+ * platform/gamepad/mac/HIDGamepadElement.cpp: Added.
+ (WebCore::HIDGamepadElement::HIDGamepadElement):
+ (WebCore::HIDGamepadElement::refreshCurrentValue):
+ (WebCore::HIDGamepadElement::normalizedValue):
+ (WebCore::HIDGamepadButton::gamepadValueChanged):
+ (WebCore::HIDGamepadAxis::gamepadValueChanged):
+ (WebCore::HIDGamepadAxis::normalizedValue):
+ * platform/gamepad/mac/HIDGamepadElement.h: Copied from Source/WebCore/platform/gamepad/PlatformGamepad.h.
+ (WebCore::HIDGamepadElement::~HIDGamepadElement):
+ (WebCore::HIDGamepadElement::isButton const):
+ (WebCore::HIDGamepadElement::isAxis const):
+
+ * platform/gamepad/mac/HIDGamepadProvider.mm:
+ (WebCore::HIDGamepadProvider::deviceAdded):
+ * platform/gamepad/mac/MultiGamepadProvider.h:
+
+ * platform/graphics/cocoa/SourceBufferParserWebM.cpp:
+
+ * platform/mac/HIDDevice.cpp: Added.
+ (WebCore::HIDDevice::HIDDevice):
+ (WebCore::HIDDevice::uniqueInputElementsInDeviceTreeOrder const):
+ * platform/mac/HIDDevice.h: Copied from Source/WebCore/platform/gamepad/PlatformGamepad.h.
+ (WebCore::HIDDevice::rawElement const):
+ (WebCore::HIDDevice::vendorID const):
+ (WebCore::HIDDevice::productID const):
+ (WebCore::HIDDevice::productName const):
+
+ * platform/mac/HIDElement.cpp: Copied from Source/WebKit/UIProcess/Gamepad/UIGamepad.h.
+ (WebCore::HIDElement::HIDElement):
+ (WebCore::HIDElement::valueChanged):
+ * platform/mac/HIDElement.h: Copied from Source/WebKit/UIProcess/Gamepad/UIGamepad.h.
+ (WebCore::HIDElement::rawElement const):
+ (WebCore::HIDElement::physicalMin const):
+ (WebCore::HIDElement::physicalMax const):
+ (WebCore::HIDElement::physicalValue const):
+ (WebCore::HIDElement::usage const):
+ (WebCore::HIDElement::usagePage const):
+ (WebCore::HIDElement::cookie const):
+
+ * testing/MockGamepad.cpp:
+ (WebCore::MockGamepad::updateDetails):
+ (WebCore::MockGamepad::setAxisValue):
+ (WebCore::MockGamepad::setButtonValue):
+ * testing/MockGamepad.h:
+
2020-07-29 Joonghun Park <[email protected]>
Unreviewed. Remove the build warning below since r265062.
Modified: trunk/Source/WebCore/Modules/gamepad/Gamepad.cpp (265078 => 265079)
--- trunk/Source/WebCore/Modules/gamepad/Gamepad.cpp 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/Modules/gamepad/Gamepad.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -62,9 +62,9 @@
void Gamepad::updateFromPlatformGamepad(const PlatformGamepad& platformGamepad)
{
for (unsigned i = 0; i < m_axes.size(); ++i)
- m_axes[i] = platformGamepad.axisValues()[i];
+ m_axes[i] = platformGamepad.axisValues()[i].value();
for (unsigned i = 0; i < m_buttons.size(); ++i)
- m_buttons[i]->setValue(platformGamepad.buttonValues()[i]);
+ m_buttons[i]->setValue(platformGamepad.buttonValues()[i].value());
m_timestamp = platformGamepad.lastUpdateTime();
}
Modified: trunk/Source/WebCore/Sources.txt (265078 => 265079)
--- trunk/Source/WebCore/Sources.txt 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/Sources.txt 2020-07-30 07:21:44 UTC (rev 265079)
@@ -1815,6 +1815,8 @@
platform/encryptedmedia/CDMLogging.cpp
platform/encryptedmedia/CDMPrivate.cpp
platform/encryptedmedia/CDMProxy.cpp
+platform/gamepad/EmptyGamepadProvider.cpp
+platform/gamepad/GamepadProvider.cpp
platform/graphics/ANGLEWebKitBridge.cpp
platform/graphics/BitmapImage.cpp
platform/graphics/Color.cpp
Modified: trunk/Source/WebCore/SourcesCocoa.txt (265078 => 265079)
--- trunk/Source/WebCore/SourcesCocoa.txt 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/SourcesCocoa.txt 2020-07-30 07:21:44 UTC (rev 265079)
@@ -257,7 +257,9 @@
platform/cocoa/WebCoreNSErrorExtras.mm
platform/gamepad/cocoa/GameControllerGamepad.mm
platform/gamepad/cocoa/GameControllerGamepadProvider.mm
+platform/gamepad/mac/GenericHIDGamepad.cpp
platform/gamepad/mac/HIDGamepad.cpp
+platform/gamepad/mac/HIDGamepadElement.cpp
platform/gamepad/mac/HIDGamepadProvider.mm
platform/gamepad/mac/MultiGamepadProvider.mm
platform/graphics/MediaPlaybackTargetPicker.cpp
@@ -468,6 +470,8 @@
platform/ios/wak/WebCoreThreadSystemInterface.cpp
platform/mac/BlocklistUpdater.mm
platform/mac/CursorMac.mm @no-unify
+platform/mac/HIDDevice.cpp
+platform/mac/HIDElement.cpp
platform/mac/KeyEventMac.mm @no-unify
platform/mac/LocalCurrentGraphicsContextMac.mm
platform/mac/LocalDefaultSystemAppearance.mm
Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (265078 => 265079)
--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj 2020-07-30 07:21:44 UTC (rev 265079)
@@ -1398,6 +1398,10 @@
5105F0691D4BA9D900FB80BC /* IDBGetRecordData.h in Headers */ = {isa = PBXBuildFile; fileRef = 5105F0681D4BA54100FB80BC /* IDBGetRecordData.h */; settings = {ATTRIBUTES = (Private, ); }; };
5106D7BE18BDB76F000AB166 /* ContextMenuContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 5106D7BC18BDB76F000AB166 /* ContextMenuContext.h */; settings = {ATTRIBUTES = (Private, ); }; };
510A58FA1BACC7F200C19282 /* IDBRequestData.h in Headers */ = {isa = PBXBuildFile; fileRef = 510A58F61BACC4A500C19282 /* IDBRequestData.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 510A91DA24CF3D8C00BFD89C /* HIDGamepadElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 510A91D624CE8EDE00BFD89C /* HIDGamepadElement.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 510A91DC24CF46FE00BFD89C /* SharedGamepadValue.h in Headers */ = {isa = PBXBuildFile; fileRef = 510A91DB24CF441800BFD89C /* SharedGamepadValue.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 510A91EA24D004C300BFD89C /* HIDElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 510A91E324CFCC1800BFD89C /* HIDElement.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 510A91EB24D004C800BFD89C /* HIDDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 510A91E224CFCC1800BFD89C /* HIDDevice.h */; settings = {ATTRIBUTES = (Private, ); }; };
510D4A34103165EE0049EA54 /* SocketStreamError.h in Headers */ = {isa = PBXBuildFile; fileRef = 510D4A2E103165EE0049EA54 /* SocketStreamError.h */; settings = {ATTRIBUTES = (Private, ); }; };
510D4A37103165EE0049EA54 /* SocketStreamHandle.h in Headers */ = {isa = PBXBuildFile; fileRef = 510D4A31103165EE0049EA54 /* SocketStreamHandle.h */; settings = {ATTRIBUTES = (Private, ); }; };
510D4A38103165EE0049EA54 /* SocketStreamHandleClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 510D4A32103165EE0049EA54 /* SocketStreamHandleClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -1432,9 +1436,7 @@
514C767B0CE923A1007EF3CD /* ResourceHandleInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 514C76660CE923A1007EF3CD /* ResourceHandleInternal.h */; };
514C767D0CE923A1007EF3CD /* ResourceRequestBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 514C76680CE923A1007EF3CD /* ResourceRequestBase.h */; settings = {ATTRIBUTES = (Private, ); }; };
514C767F0CE923A1007EF3CD /* ResourceResponseBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 514C766A0CE923A1007EF3CD /* ResourceResponseBase.h */; settings = {ATTRIBUTES = (Private, ); }; };
- 515BE18F1D54F5FB00DD7C68 /* EmptyGamepadProvider.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 515BE1881D54F5F600DD7C68 /* EmptyGamepadProvider.cpp */; };
515BE1901D54F5FB00DD7C68 /* EmptyGamepadProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 515BE1891D54F5F600DD7C68 /* EmptyGamepadProvider.h */; };
- 515BE1911D54F5FB00DD7C68 /* GamepadProvider.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 515BE18A1D54F5F600DD7C68 /* GamepadProvider.cpp */; };
515BE1921D54F5FB00DD7C68 /* GamepadProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 515BE18B1D54F5F600DD7C68 /* GamepadProvider.h */; settings = {ATTRIBUTES = (Private, ); }; };
515BE1931D54F5FB00DD7C68 /* GamepadProviderClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 515BE18C1D54F5F600DD7C68 /* GamepadProviderClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
515BE1951D54F5FB00DD7C68 /* PlatformGamepad.h in Headers */ = {isa = PBXBuildFile; fileRef = 515BE18E1D54F5F600DD7C68 /* PlatformGamepad.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -8214,6 +8216,15 @@
510A58F61BACC4A500C19282 /* IDBRequestData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDBRequestData.h; sourceTree = "<group>"; };
510A58FD1BB07A9600C19282 /* IDBConnectionToServer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDBConnectionToServer.h; sourceTree = "<group>"; };
510A58FE1BB07AA500C19282 /* IDBConnectionToClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDBConnectionToClient.h; sourceTree = "<group>"; };
+ 510A91D524CE8EDE00BFD89C /* HIDGamepadElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HIDGamepadElement.cpp; sourceTree = "<group>"; };
+ 510A91D624CE8EDE00BFD89C /* HIDGamepadElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HIDGamepadElement.h; sourceTree = "<group>"; };
+ 510A91DB24CF441800BFD89C /* SharedGamepadValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SharedGamepadValue.h; sourceTree = "<group>"; };
+ 510A91E224CFCC1800BFD89C /* HIDDevice.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HIDDevice.h; sourceTree = "<group>"; };
+ 510A91E324CFCC1800BFD89C /* HIDElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HIDElement.h; sourceTree = "<group>"; };
+ 510A91E424CFCC1800BFD89C /* HIDElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HIDElement.cpp; sourceTree = "<group>"; };
+ 510A91E524CFCC1800BFD89C /* HIDDevice.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HIDDevice.cpp; sourceTree = "<group>"; };
+ 510A91E724CFEA1700BFD89C /* GenericHIDGamepad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GenericHIDGamepad.cpp; sourceTree = "<group>"; };
+ 510A91E924CFEA3200BFD89C /* GenericHIDGamepad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GenericHIDGamepad.h; sourceTree = "<group>"; };
510D4A2E103165EE0049EA54 /* SocketStreamError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SocketStreamError.h; sourceTree = "<group>"; };
510D4A30103165EE0049EA54 /* SocketStreamHandle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SocketStreamHandle.cpp; sourceTree = "<group>"; };
510D4A31103165EE0049EA54 /* SocketStreamHandle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SocketStreamHandle.h; sourceTree = "<group>"; };
@@ -8369,6 +8380,8 @@
51714EAB1CF65899004723C4 /* GCObservation.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GCObservation.idl; sourceTree = "<group>"; };
51714EAE1CF6654A004723C4 /* JSGCObservation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSGCObservation.cpp; sourceTree = "<group>"; };
51714EAF1CF6654A004723C4 /* JSGCObservation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSGCObservation.h; sourceTree = "<group>"; };
+ 51715FE824CD05DD002C97E6 /* GamepadConstants.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GamepadConstants.cpp; sourceTree = "<group>"; };
+ 51715FE924CD05DD002C97E6 /* GamepadConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GamepadConstants.h; sourceTree = "<group>"; };
51741D0B0B07259A00ED442C /* BackForwardClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BackForwardClient.h; sourceTree = "<group>"; };
51741D0D0B07259A00ED442C /* HistoryItem.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = HistoryItem.h; sourceTree = "<group>"; };
51741D0E0B07259A00ED442C /* HistoryItem.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = HistoryItem.cpp; sourceTree = "<group>"; };
@@ -19643,10 +19656,13 @@
515BE1961D54F64500DD7C68 /* mac */,
515BE1881D54F5F600DD7C68 /* EmptyGamepadProvider.cpp */,
515BE1891D54F5F600DD7C68 /* EmptyGamepadProvider.h */,
+ 51715FE824CD05DD002C97E6 /* GamepadConstants.cpp */,
+ 51715FE924CD05DD002C97E6 /* GamepadConstants.h */,
515BE18A1D54F5F600DD7C68 /* GamepadProvider.cpp */,
515BE18B1D54F5F600DD7C68 /* GamepadProvider.h */,
515BE18C1D54F5F600DD7C68 /* GamepadProviderClient.h */,
515BE18E1D54F5F600DD7C68 /* PlatformGamepad.h */,
+ 510A91DB24CF441800BFD89C /* SharedGamepadValue.h */,
);
path = gamepad;
sourceTree = "<group>";
@@ -19654,8 +19670,12 @@
515BE1961D54F64500DD7C68 /* mac */ = {
isa = PBXGroup;
children = (
+ 510A91E724CFEA1700BFD89C /* GenericHIDGamepad.cpp */,
+ 510A91E924CFEA3200BFD89C /* GenericHIDGamepad.h */,
515BE1971D54F6BD00DD7C68 /* HIDGamepad.cpp */,
515BE1981D54F6BD00DD7C68 /* HIDGamepad.h */,
+ 510A91D524CE8EDE00BFD89C /* HIDGamepadElement.cpp */,
+ 510A91D624CE8EDE00BFD89C /* HIDGamepadElement.h */,
515BE19A1D54F6BD00DD7C68 /* HIDGamepadProvider.h */,
515BE1991D54F6BD00DD7C68 /* HIDGamepadProvider.mm */,
517E03F424B6B9630054895A /* MultiGamepadProvider.h */,
@@ -21051,6 +21071,10 @@
1AFFC44E1D5E7EC700267A66 /* BlocklistUpdater.mm */,
F58784F002DE375901EA4122 /* CursorMac.mm */,
E1BA66F01742BD8600C20251 /* DynamicLinkerInterposing.h */,
+ 510A91E524CFCC1800BFD89C /* HIDDevice.cpp */,
+ 510A91E224CFCC1800BFD89C /* HIDDevice.h */,
+ 510A91E424CFCC1800BFD89C /* HIDElement.cpp */,
+ 510A91E324CFCC1800BFD89C /* HIDElement.h */,
935C476E09AC4D7300A6AAB4 /* KeyEventMac.mm */,
F44A5F571FED3830007F5944 /* LegacyNSPasteboardTypes.h */,
06E81EEB0AB5DA9700C87837 /* LocalCurrentGraphicsContextMac.mm */,
@@ -31030,7 +31054,10 @@
CDA595932146DEC300A84185 /* HEVCUtilities.h in Headers */,
CDA595982146DF7800A84185 /* HEVCUtilitiesCocoa.h in Headers */,
F55B3DC01251F12D003EF269 /* HiddenInputType.h in Headers */,
+ 510A91EB24D004C800BFD89C /* HIDDevice.h in Headers */,
+ 510A91EA24D004C300BFD89C /* HIDElement.h in Headers */,
515BE19C1D54F6C100DD7C68 /* HIDGamepad.h in Headers */,
+ 510A91DA24CF3D8C00BFD89C /* HIDGamepadElement.h in Headers */,
515BE19E1D54F6C100DD7C68 /* HIDGamepadProvider.h in Headers */,
CDCFABBD18C0AF78006F8450 /* HighlightData.h in Headers */,
BC94D1540C275C8B006BC617 /* History.h in Headers */,
@@ -33308,6 +33335,7 @@
FD1AF1501656F15100C6D4F7 /* ShapeValue.h in Headers */,
1D9F0FC12122029B005D8FD4 /* ShareData.h in Headers */,
1A4A954E0B4EDCCB002D8C3C /* SharedBuffer.h in Headers */,
+ 510A91DC24CF46FE00BFD89C /* SharedGamepadValue.h in Headers */,
834DFAD01F7DAE5D00C2725B /* SharedStringHash.h in Headers */,
93309EA3099EB78C0056E581 /* SharedTimer.h in Headers */,
DF19E2AC24772BC1007BDACB /* ShouldRelaxThirdPartyCookieBlocking.h in Headers */,
@@ -34618,7 +34646,6 @@
2D22830323A8470700364B7E /* CursorMac.mm in Sources */,
5CBD59592280E926002B22AA /* CustomHeaderFields.cpp in Sources */,
4463CF682212FA68001A8577 /* DataDetectorsCoreSoftLink.mm in Sources */,
- 515BE18F1D54F5FB00DD7C68 /* EmptyGamepadProvider.cpp in Sources */,
6E72F54C229DCD0C00B3E151 /* ExtensionsGLANGLE.cpp in Sources */,
7CE6CBFD187F394900D46BF5 /* FormatConverter.cpp in Sources */,
5130F2F624AEA60A00E1D0A0 /* GameControllerSoftLink.mm in Sources */,
@@ -34626,7 +34653,6 @@
518F4FF6194CA4E60081BAAE /* GamepadButton.cpp in Sources */,
516C62201950D48700337E75 /* GamepadEvent.cpp in Sources */,
51A9D9E9195B931F001B2B5C /* GamepadManager.cpp in Sources */,
- 515BE1911D54F5FB00DD7C68 /* GamepadProvider.cpp in Sources */,
837964CF1F8DB69D00218EA0 /* GeolocationPositionDataIOS.mm in Sources */,
6E72F54E229DCD1000B3E151 /* GraphicsContextGLANGLE.cpp in Sources */,
7C3E510B18DF8F3500C112F7 /* HTMLConverter.mm in Sources */,
Modified: trunk/Source/WebCore/platform/Logging.h (265078 => 265079)
--- trunk/Source/WebCore/platform/Logging.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/Logging.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2017 Apple Inc. All rights reserved.
+ * Copyright (C) 2003-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -63,6 +63,7 @@
M(FTP) \
M(Fullscreen) \
M(Gamepad) \
+ M(HID) \
M(History) \
M(IOSurface) \
M(IconDatabase) \
Modified: trunk/Source/WebCore/platform/gamepad/PlatformGamepad.h (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/PlatformGamepad.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/PlatformGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -27,6 +27,7 @@
#if ENABLE(GAMEPAD)
+#include "SharedGamepadValue.h"
#include <wtf/Forward.h>
#include <wtf/MonotonicTime.h>
#include <wtf/text/WTFString.h>
@@ -43,8 +44,9 @@
unsigned index() const { return m_index; }
virtual MonotonicTime lastUpdateTime() const { return m_lastUpdateTime; }
MonotonicTime connectTime() const { return m_connectTime; }
- virtual const Vector<double>& axisValues() const = 0;
- virtual const Vector<double>& buttonValues() const = 0;
+
+ virtual const Vector<SharedGamepadValue>& axisValues() const = 0;
+ virtual const Vector<SharedGamepadValue>& buttonValues() const = 0;
virtual const char* source() const { return "Unknown"_s; }
Copied: trunk/Source/WebCore/platform/gamepad/SharedGamepadValue.h (from rev 265078, trunk/Source/WebKit/UIProcess/Gamepad/UIGamepad.h) (0 => 265079)
--- trunk/Source/WebCore/platform/gamepad/SharedGamepadValue.h (rev 0)
+++ trunk/Source/WebCore/platform/gamepad/SharedGamepadValue.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(GAMEPAD)
+
+#include <wtf/FastMalloc.h>
+#include <wtf/Nonmovable.h>
+#include <wtf/Ref.h>
+#include <wtf/RefCounted.h>
+
+namespace WebCore {
+
+class SharedGamepadValue {
+public:
+ SharedGamepadValue()
+ : m_data(adoptRef(*new Data(0.0)))
+ {
+ }
+
+ explicit SharedGamepadValue(double value)
+ : m_data(adoptRef(*new Data(value)))
+ {
+ }
+
+ void setValue(double value) { m_data->value = value; }
+ double value() const { return m_data->value; }
+
+private:
+ struct Data : RefCounted<Data> {
+ WTF_MAKE_STRUCT_FAST_ALLOCATED;
+
+ Data(double theValue)
+ : value(theValue)
+ {
+ }
+
+ double value;
+ };
+
+ Ref<Data> m_data;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(GAMEPAD)
Modified: trunk/Source/WebCore/platform/gamepad/cocoa/GameControllerGamepad.h (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/cocoa/GameControllerGamepad.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/cocoa/GameControllerGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -44,8 +44,8 @@
public:
GameControllerGamepad(GCController *, unsigned index);
- const Vector<double>& axisValues() const final { return m_axisValues; }
- const Vector<double>& buttonValues() const final { return m_buttonValues; }
+ const Vector<SharedGamepadValue>& axisValues() const final { return m_axisValues; }
+ const Vector<SharedGamepadValue>& buttonValues() const final { return m_buttonValues; }
const char* source() const final { return "GameController"_s; }
@@ -55,8 +55,8 @@
RetainPtr<GCController> m_gcController;
- Vector<double> m_axisValues;
- Vector<double> m_buttonValues;
+ Vector<SharedGamepadValue> m_axisValues;
+ Vector<SharedGamepadValue> m_buttonValues;
RetainPtr<GCGamepad> m_gamepad;
RetainPtr<GCExtendedGamepad> m_extendedGamepad;
Modified: trunk/Source/WebCore/platform/gamepad/cocoa/GameControllerGamepad.mm (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/cocoa/GameControllerGamepad.mm 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/cocoa/GameControllerGamepad.mm 2020-07-30 07:21:44 UTC (rev 265079)
@@ -110,9 +110,9 @@
};
auto bindButton = ^(GCControllerButtonInput *button, GamepadButtons index) {
- m_buttonValues[(size_t)index] = button.value;
+ m_buttonValues[(size_t)index].setValue(button.value);
button.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[(size_t)index] = value;
+ m_buttonValues[(size_t)index].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
@@ -158,22 +158,22 @@
#endif
m_axisValues.resize(4);
- m_axisValues[0] = m_extendedGamepad.get().leftThumbstick.xAxis.value;
- m_axisValues[1] = -m_extendedGamepad.get().leftThumbstick.yAxis.value;
- m_axisValues[2] = m_extendedGamepad.get().rightThumbstick.xAxis.value;
- m_axisValues[3] = -m_extendedGamepad.get().rightThumbstick.yAxis.value;
+ m_axisValues[0].setValue(m_extendedGamepad.get().leftThumbstick.xAxis.value);
+ m_axisValues[1].setValue(-m_extendedGamepad.get().leftThumbstick.yAxis.value);
+ m_axisValues[2].setValue(m_extendedGamepad.get().rightThumbstick.xAxis.value);
+ m_axisValues[3].setValue(-m_extendedGamepad.get().rightThumbstick.yAxis.value);
m_extendedGamepad.get().leftThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *, float value) {
- m_axisValues[0] = value;
+ m_axisValues[0].setValue(value);
};
m_extendedGamepad.get().leftThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *, float value) {
- m_axisValues[1] = -value;
+ m_axisValues[1].setValue(-value);
};
m_extendedGamepad.get().rightThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *, float value) {
- m_axisValues[2] = value;
+ m_axisValues[2].setValue(value);
};
m_extendedGamepad.get().rightThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *, float value) {
- m_axisValues[3] = -value;
+ m_axisValues[3].setValue(-value);
};
}
@@ -192,53 +192,53 @@
ALLOW_DEPRECATED_DECLARATIONS_END
m_buttonValues.resize(6);
- m_buttonValues[0] = m_extendedGamepad.get().buttonA.value;
- m_buttonValues[1] = m_extendedGamepad.get().buttonB.value;
- m_buttonValues[2] = m_extendedGamepad.get().buttonX.value;
- m_buttonValues[3] = m_extendedGamepad.get().buttonY.value;
- m_buttonValues[4] = m_extendedGamepad.get().leftShoulder.value;
- m_buttonValues[5] = m_extendedGamepad.get().rightShoulder.value;
+ m_buttonValues[0].setValue(m_extendedGamepad.get().buttonA.value);
+ m_buttonValues[1].setValue(m_extendedGamepad.get().buttonB.value);
+ m_buttonValues[2].setValue(m_extendedGamepad.get().buttonX.value);
+ m_buttonValues[3].setValue(m_extendedGamepad.get().buttonY.value);
+ m_buttonValues[4].setValue(m_extendedGamepad.get().leftShoulder.value);
+ m_buttonValues[5].setValue(m_extendedGamepad.get().rightShoulder.value);
m_axisValues.resize(2);
- m_axisValues[0] = m_extendedGamepad.get().dpad.xAxis.value;
- m_axisValues[1] = m_extendedGamepad.get().dpad.yAxis.value;
+ m_axisValues[0].setValue(m_extendedGamepad.get().dpad.xAxis.value);
+ m_axisValues[1].setValue(m_extendedGamepad.get().dpad.yAxis.value);
m_extendedGamepad.get().buttonA.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[0] = value;
+ m_buttonValues[0].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
m_extendedGamepad.get().buttonB.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[1] = value;
+ m_buttonValues[1].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
m_extendedGamepad.get().buttonX.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[2] = value;
+ m_buttonValues[2].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
m_extendedGamepad.get().buttonY.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[3] = value;
+ m_buttonValues[3].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
m_extendedGamepad.get().leftShoulder.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[4] = value;
+ m_buttonValues[4].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
m_extendedGamepad.get().rightShoulder.valueChangedHandler = ^(GCControllerButtonInput *, float value, BOOL pressed) {
- m_buttonValues[5] = value;
+ m_buttonValues[5].setValue(value);
if (pressed)
m_hadButtonPresses = true;
};
m_extendedGamepad.get().leftThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *, float value) {
- m_axisValues[0] = value;
+ m_axisValues[0].setValue(value);
};
m_extendedGamepad.get().leftThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *, float value) {
- m_axisValues[1] = value;
+ m_axisValues[1].setValue(value);
};
}
Added: trunk/Source/WebCore/platform/gamepad/mac/GenericHIDGamepad.cpp (0 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/GenericHIDGamepad.cpp (rev 0)
+++ trunk/Source/WebCore/platform/gamepad/mac/GenericHIDGamepad.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "GenericHIDGamepad.h"
+
+#if ENABLE(GAMEPAD) && PLATFORM(MAC)
+
+#include <IOKit/hid/IOHIDUsageTables.h>
+#include <wtf/HexNumber.h>
+
+namespace WebCore {
+
+GenericHIDGamepad::GenericHIDGamepad(HIDDevice&& device, unsigned index)
+ : HIDGamepad(WTFMove(device), index)
+{
+ auto inputElements = hidDevice().uniqueInputElementsInDeviceTreeOrder();
+
+ for (auto& element : inputElements) {
+ switch (element.usagePage()) {
+ case kHIDPage_GenericDesktop:
+ maybeAddGenericDesktopElement(element);
+ continue;
+ case kHIDPage_Button:
+ maybeAddButtonElement(element);
+ continue;
+ default:
+ continue;
+ }
+ }
+}
+
+String GenericHIDGamepad::id()
+{
+ // Currently the spec has no formatting for the id string.
+ // This string formatting matches Firefox.
+ return makeString(hex(hidDevice().vendorID(), Lowercase), '-', hex(hidDevice().productID(), Lowercase), '-', hidDevice().productName());
+}
+
+void GenericHIDGamepad::maybeAddGenericDesktopElement(HIDElement& element)
+{
+ switch (element.usage()) {
+ case kHIDUsage_GD_X:
+ case kHIDUsage_GD_Y:
+ case kHIDUsage_GD_Z:
+ case kHIDUsage_GD_Rx:
+ case kHIDUsage_GD_Ry:
+ case kHIDUsage_GD_Rz:
+ m_axisValues.append(0.0);
+ m_elementMap.set(element.cookie(), makeUnique<HIDGamepadAxis>(element, m_axisValues.last()));
+ break;
+ case kHIDUsage_GD_DPadUp:
+ case kHIDUsage_GD_DPadDown:
+ case kHIDUsage_GD_DPadRight:
+ case kHIDUsage_GD_DPadLeft:
+ m_buttonValues.append(0.0);
+ m_elementMap.set(element.cookie(), makeUnique<HIDGamepadButton>(element, m_buttonValues.last()));
+ break;
+ default:
+ break;
+ }
+}
+
+void GenericHIDGamepad::maybeAddButtonElement(HIDElement& element)
+{
+ // If it's in the button page, we assume it's actually a button.
+ m_buttonValues.append(0.0);
+ m_elementMap.set(element.cookie(), makeUnique<HIDGamepadButton>(element, m_buttonValues.last()));
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(GAMEPAD) && PLATFORM(MAC)
Copied: trunk/Source/WebCore/platform/gamepad/mac/GenericHIDGamepad.h (from rev 265078, trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.h) (0 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/GenericHIDGamepad.h (rev 0)
+++ trunk/Source/WebCore/platform/gamepad/mac/GenericHIDGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(GAMEPAD) && PLATFORM(MAC)
+
+#include "HIDGamepad.h"
+
+namespace WebCore {
+
+class GenericHIDGamepad final : public HIDGamepad {
+public:
+ GenericHIDGamepad(HIDDevice&&, unsigned index);
+
+private:
+ String id() final;
+
+ void maybeAddGenericDesktopElement(HIDElement&);
+ void maybeAddButtonElement(HIDElement&);
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(GAMEPAD) && PLATFORM(MAC)
Modified: trunk/Source/WebCore/platform/gamepad/mac/HIDGamepad.cpp (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/HIDGamepad.cpp 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/mac/HIDGamepad.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -28,147 +28,43 @@
#if ENABLE(GAMEPAD) && PLATFORM(MAC)
+#include "GenericHIDGamepad.h"
+#include "Logging.h"
#include <IOKit/hid/IOHIDElement.h>
#include <IOKit/hid/IOHIDUsageTables.h>
#include <IOKit/hid/IOHIDValue.h>
-#include <wtf/HexNumber.h>
#include <wtf/cf/TypeCastsCF.h>
#include <wtf/text/CString.h>
-WTF_DECLARE_CF_TYPE_TRAIT(IOHIDElement);
-
namespace WebCore {
-HIDGamepad::HIDGamepad(IOHIDDeviceRef hidDevice, unsigned index)
- : PlatformGamepad(index)
- , m_hidDevice(hidDevice)
+std::unique_ptr<HIDGamepad> HIDGamepad::create(IOHIDDeviceRef rawDevice, unsigned index)
{
- m_connectTime = m_lastUpdateTime = MonotonicTime::now();
+ auto device = HIDDevice { rawDevice };
- CFNumberRef cfVendorID = (CFNumberRef)IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
- CFNumberRef cfProductID = (CFNumberRef)IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey));
+ // When we support specific mapping for a particular device, here is where we'll decide what kind of HIDGamepad to make.
+ // For now, it's only the GenericHIDGamepad
+ auto newGamepad = makeUnique<GenericHIDGamepad>(WTFMove(device), index);
- int vendorID, productID;
- CFNumberGetValue(cfVendorID, kCFNumberIntType, &vendorID);
- CFNumberGetValue(cfProductID, kCFNumberIntType, &productID);
-
- CFStringRef cfProductName = (CFStringRef)IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
- String productName(cfProductName);
-
- // Currently the spec has no formatting for the id string.
- // This string formatting matches Firefox.
- m_id = makeString(hex(vendorID, Lowercase), '-', hex(productID, Lowercase), '-', productName);
-
- initElements();
+ newGamepad->initialize();
+ return WTFMove(newGamepad);
}
-void HIDGamepad::getCurrentValueForElement(const HIDGamepadElement& gamepadElement)
+HIDGamepad::HIDGamepad(HIDDevice&& device, unsigned index)
+ : PlatformGamepad(index)
+ , m_device(WTFMove(device))
{
- IOHIDElementRef element = gamepadElement.iohidElement.get();
- IOHIDValueRef value;
- if (IOHIDDeviceGetValue(IOHIDElementGetDevice(element), element, &value) == kIOReturnSuccess)
- valueChanged(value);
+ m_connectTime = m_lastUpdateTime = MonotonicTime::now();
}
-void HIDGamepad::initElements()
+void HIDGamepad::initialize()
{
- RetainPtr<CFArrayRef> elements = adoptCF(IOHIDDeviceCopyMatchingElements(m_hidDevice.get(), NULL, kIOHIDOptionsTypeNone));
- initElementsFromArray(elements.get());
+ m_id = id();
- // Buttons are specified to appear highest priority first in the array.
- std::sort(m_buttons.begin(), m_buttons.end(), [](auto& a, auto& b) {
- return a->priority < b->priority;
- });
-
- LOG(Gamepad, "HIDGamepad initialized with %zu buttons and %zu axes", m_buttons.size(), m_axes.size());
-
- m_axisValues.resize(m_axes.size());
- m_buttonValues.resize(m_buttons.size());
-
- for (auto& button : m_buttons)
- getCurrentValueForElement(button.get());
-
- for (auto& axis : m_axes)
- getCurrentValueForElement(axis.get());
+ for (auto& element : m_elementMap.values())
+ element->refreshCurrentValue();
}
-void HIDGamepad::initElementsFromArray(CFArrayRef elements)
-{
- for (CFIndex i = 0, count = CFArrayGetCount(elements); i < count; ++i) {
- IOHIDElementRef element = checked_cf_cast<IOHIDElementRef>(CFArrayGetValueAtIndex(elements, i));
- if (CFGetTypeID(element) != IOHIDElementGetTypeID())
- continue;
-
- // As a physical element can appear in the device twice (in different collections) and can be
- // represented by different IOHIDElementRef objects, we look at the IOHIDElementCookie which
- // is meant to be unique for each physical element.
- IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
- if (m_elementMap.contains(cookie))
- continue;
-
- IOHIDElementType type = IOHIDElementGetType(element);
-
- if ((type == kIOHIDElementTypeInput_Misc || type == kIOHIDElementTypeInput_Button)) {
- if (maybeAddButton(element))
- continue;
- }
-
- if ((type == kIOHIDElementTypeInput_Misc || type == kIOHIDElementTypeInput_Axis) && maybeAddAxis(element))
- continue;
-
- if (type == kIOHIDElementTypeCollection)
- initElementsFromArray(IOHIDElementGetChildren(element));
- }
-}
-
-bool HIDGamepad::maybeAddButton(IOHIDElementRef element)
-{
- uint32_t usagePage = IOHIDElementGetUsagePage(element);
- if (usagePage != kHIDPage_Button && usagePage != kHIDPage_GenericDesktop)
- return false;
-
- uint32_t usage = IOHIDElementGetUsage(element);
-
- if (usagePage == kHIDPage_GenericDesktop) {
- if (usage < kHIDUsage_GD_DPadUp || usage > kHIDUsage_GD_DPadLeft)
- return false;
- usage = std::numeric_limits<uint32_t>::max();
- } else if (!usage)
- return false;
-
- CFIndex min = IOHIDElementGetLogicalMin(element);
- CFIndex max = IOHIDElementGetLogicalMax(element);
-
- m_buttons.append(makeUniqueRef<HIDGamepadButton>(usage, min, max, element));
-
- IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
- m_elementMap.set(cookie, &m_buttons.last().get());
-
- return true;
-}
-
-bool HIDGamepad::maybeAddAxis(IOHIDElementRef element)
-{
- uint32_t usagePage = IOHIDElementGetUsagePage(element);
- if (usagePage != kHIDPage_GenericDesktop)
- return false;
-
- uint32_t usage = IOHIDElementGetUsage(element);
- // This range covers the standard axis usages.
- if (usage < kHIDUsage_GD_X || usage > kHIDUsage_GD_Rz)
- return false;
-
- CFIndex min = IOHIDElementGetPhysicalMin(element);
- CFIndex max = IOHIDElementGetPhysicalMax(element);
-
- m_axes.append(makeUniqueRef<HIDGamepadAxis>(min, max, element));
-
- IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
- m_elementMap.set(cookie, &m_axes.last().get());
-
- return true;
-}
-
HIDInputType HIDGamepad::valueChanged(IOHIDValueRef value)
{
IOHIDElementCookie cookie = IOHIDElementGetCookie(IOHIDValueGetElement(value));
@@ -178,28 +74,9 @@
if (!element)
return HIDInputType::NotAButtonPress;
- element->rawValue = IOHIDValueGetScaledValue(value, kIOHIDValueScaleTypePhysical);
-
- if (element->isButton()) {
- for (unsigned i = 0; i < m_buttons.size(); ++i) {
- if (&m_buttons[i].get() == element) {
- m_buttonValues[i] = element->normalizedValue();
- break;
- }
- }
- } else if (element->isAxis()) {
- for (unsigned i = 0; i < m_axes.size(); ++i) {
- if (&m_axes[i].get() == element) {
- m_axisValues[i] = element->normalizedValue();
- break;
- }
- }
- } else
- ASSERT_NOT_REACHED();
-
m_lastUpdateTime = MonotonicTime::now();
- return element->isButton() ? HIDInputType::ButtonPress : HIDInputType::NotAButtonPress;
+ return element->gamepadValueChanged(value);
}
} // namespace WebCore
Modified: trunk/Source/WebCore/platform/gamepad/mac/HIDGamepad.h (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/HIDGamepad.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/mac/HIDGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -27,107 +27,39 @@
#if ENABLE(GAMEPAD) && PLATFORM(MAC)
+#include "HIDDevice.h"
+#include "HIDGamepadElement.h"
#include "PlatformGamepad.h"
#include <IOKit/hid/IOHIDDevice.h>
#include <wtf/HashMap.h>
-#include <wtf/RetainPtr.h>
-#include <wtf/UniqueRef.h>
namespace WebCore {
-struct HIDGamepadElement {
- WTF_MAKE_STRUCT_FAST_ALLOCATED;
- HIDGamepadElement(double theMin, double theMax, IOHIDElementRef element)
- : min(theMin)
- , max(theMax)
- , rawValue(theMin)
- , iohidElement(element)
- {
- }
-
- virtual ~HIDGamepadElement()
- {
- }
-
- double min;
- double max;
- double rawValue;
- RetainPtr<IOHIDElementRef> iohidElement;
-
- virtual bool isButton() const { return false; }
- virtual bool isAxis() const { return false; }
-
- virtual double normalizedValue() = 0;
-};
-
-struct HIDGamepadButton : HIDGamepadElement {
- HIDGamepadButton(uint32_t thePriority, double min, double max, IOHIDElementRef element)
- : HIDGamepadElement(min, max, element)
- , priority(thePriority)
- {
- }
-
- uint32_t priority;
-
- bool isButton() const final { return true; }
-
- // Buttons normalize to the range (0.0) - (1.0)
- double normalizedValue() override
- {
- return (rawValue - min) / (max - min);
- }
-};
-
-struct HIDGamepadAxis : HIDGamepadElement {
- HIDGamepadAxis(double min, double max, IOHIDElementRef element)
- : HIDGamepadElement(min, max, element)
- {
- }
-
- bool isAxis() const final { return true; }
-
- // Axes normalize to the range (-1.0) - (1.0)
- double normalizedValue() override
- {
- return (((rawValue - min) / (max - min)) * 2) - 1;
- }
-};
-
-enum class HIDInputType {
- ButtonPress,
- NotAButtonPress,
-};
-
class HIDGamepad : public PlatformGamepad {
public:
- HIDGamepad(IOHIDDeviceRef, unsigned index);
+ static std::unique_ptr<HIDGamepad> create(IOHIDDeviceRef, unsigned index);
- IOHIDDeviceRef hidDevice() const { return m_hidDevice.get(); }
+ const HIDDevice& hidDevice() const { return m_device; }
+ void initialize();
HIDInputType valueChanged(IOHIDValueRef);
- const Vector<double>& axisValues() const final { return m_axisValues; }
- const Vector<double>& buttonValues() const final { return m_buttonValues; }
+ const Vector<SharedGamepadValue>& axisValues() const final { return m_axisValues; }
+ const Vector<SharedGamepadValue>& buttonValues() const final { return m_buttonValues; }
const char* source() const final { return "HID"_s; }
-private:
- void initElements();
- void initElementsFromArray(CFArrayRef);
+protected:
+ HIDGamepad(HIDDevice&&, unsigned index);
- bool maybeAddButton(IOHIDElementRef);
- bool maybeAddAxis(IOHIDElementRef);
+ virtual String id() = 0;
- void getCurrentValueForElement(const HIDGamepadElement&);
+ HashMap<IOHIDElementCookie, std::unique_ptr<HIDGamepadElement>> m_elementMap;
+ Vector<SharedGamepadValue> m_buttonValues;
+ Vector<SharedGamepadValue> m_axisValues;
- RetainPtr<IOHIDDeviceRef> m_hidDevice;
-
- HashMap<IOHIDElementCookie, HIDGamepadElement*> m_elementMap;
-
- Vector<UniqueRef<HIDGamepadButton>> m_buttons;
- Vector<UniqueRef<HIDGamepadAxis>> m_axes;
- Vector<double> m_buttonValues;
- Vector<double> m_axisValues;
+private:
+ HIDDevice m_device;
};
} // namespace WebCore
Added: trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadElement.cpp (0 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadElement.cpp (rev 0)
+++ trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadElement.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "HIDGamepadElement.h"
+
+#if ENABLE(GAMEPAD) && PLATFORM(MAC)
+
+namespace WebCore {
+
+#pragma mark HIDGamepadElement
+
+HIDGamepadElement::HIDGamepadElement(const HIDElement& element, SharedGamepadValue& value)
+ : HIDElement(element)
+ , m_value(value)
+{
+}
+
+void HIDGamepadElement::refreshCurrentValue()
+{
+ IOHIDValueRef value;
+ if (IOHIDDeviceGetValue(IOHIDElementGetDevice(rawElement()), rawElement(), &value) == kIOReturnSuccess)
+ gamepadValueChanged(value);
+}
+
+double HIDGamepadElement::normalizedValue()
+{
+ // Default normalization (and the normalization buttons will use) is 0 to 1.0
+ return (double)(physicalValue() - physicalMin()) / (double)(physicalMax() - physicalMin());
+}
+
+#pragma mark HIDGamepadButton
+
+HIDInputType HIDGamepadButton::gamepadValueChanged(IOHIDValueRef value)
+{
+ valueChanged(value);
+ m_value.setValue(normalizedValue());
+ return m_value.value() > 0.5 ? HIDInputType::ButtonPress : HIDInputType::NotAButtonPress;
+}
+
+#pragma mark HIDGamepadAxis
+
+HIDInputType HIDGamepadAxis::gamepadValueChanged(IOHIDValueRef value)
+{
+ valueChanged(value);
+ m_value.setValue(normalizedValue());
+ return HIDInputType::NotAButtonPress;
+}
+
+double HIDGamepadAxis::normalizedValue()
+{
+ // Web Gamepad axes have a value of -1.0 to 1.0
+ return (HIDGamepadElement::normalizedValue() * 2.0) - 1.0;
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(GAMEPAD) && PLATFORM(MAC)
Copied: trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadElement.h (from rev 265078, trunk/Source/WebCore/platform/gamepad/PlatformGamepad.h) (0 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadElement.h (rev 0)
+++ trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadElement.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(GAMEPAD) && PLATFORM(MAC)
+
+#include "HIDElement.h"
+#include "SharedGamepadValue.h"
+#include <wtf/HashMap.h>
+#include <wtf/RetainPtr.h>
+#include <wtf/UniqueRef.h>
+
+namespace WebCore {
+
+enum class HIDInputType {
+ ButtonPress,
+ NotAButtonPress,
+};
+
+class HIDGamepadElement : public HIDElement {
+ WTF_MAKE_FAST_ALLOCATED;
+public:
+ virtual ~HIDGamepadElement() { }
+
+ virtual double normalizedValue();
+ virtual HIDInputType gamepadValueChanged(IOHIDValueRef) = 0;
+
+ void refreshCurrentValue();
+
+protected:
+ HIDGamepadElement(const HIDElement&, SharedGamepadValue&);
+
+ virtual bool isButton() const { return false; }
+ virtual bool isAxis() const { return false; }
+
+ SharedGamepadValue m_value;
+};
+
+class HIDGamepadButton final : public HIDGamepadElement {
+public:
+ HIDGamepadButton(const HIDElement& element, SharedGamepadValue& value)
+ : HIDGamepadElement(element, value)
+ {
+ }
+
+ bool isButton() const final { return true; }
+
+private:
+ HIDInputType gamepadValueChanged(IOHIDValueRef) override;
+};
+
+class HIDGamepadAxis final : public HIDGamepadElement {
+public:
+ HIDGamepadAxis(const HIDElement& element, SharedGamepadValue& value)
+ : HIDGamepadElement(element, value)
+ {
+ }
+
+ bool isAxis() const final { return true; }
+ double normalizedValue() final;
+
+private:
+ HIDInputType gamepadValueChanged(IOHIDValueRef) override;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(GAMEPAD) && PLATFORM(MAC)
Modified: trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadProvider.mm (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadProvider.mm 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/mac/HIDGamepadProvider.mm 2020-07-30 07:21:44 UTC (rev 265079)
@@ -235,7 +235,7 @@
LOG(Gamepad, "HIDGamepadProvider device %p added", device);
unsigned index = indexForNewlyConnectedDevice();
- std::unique_ptr<HIDGamepad> gamepad = makeUnique<HIDGamepad>(device, index);
+ auto gamepad = HIDGamepad::create(device, index);
if (m_gamepadVector.size() <= index)
m_gamepadVector.grow(index + 1);
Modified: trunk/Source/WebCore/platform/gamepad/mac/MultiGamepadProvider.h (265078 => 265079)
--- trunk/Source/WebCore/platform/gamepad/mac/MultiGamepadProvider.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/gamepad/mac/MultiGamepadProvider.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -63,7 +63,7 @@
size_t m_initialGamepadsCount { 0 };
Vector<PlatformGamepad*> m_gamepadVector;
- // We create our own Gamepad type - to wrap both HID and GameControler gamepads -
+ // We create our own Gamepad type - to wrap both HID and GameController gamepads -
// because MultiGamepadProvider needs to manage the indexes of its own gamepads
// no matter what the HID or GameController index is.
class PlatformGamepadWrapper : public PlatformGamepad {
@@ -78,8 +78,8 @@
}
MonotonicTime lastUpdateTime() const final { return m_platformGamepad->lastUpdateTime(); }
- const Vector<double>& axisValues() const final { return m_platformGamepad->axisValues(); }
- const Vector<double>& buttonValues() const final { return m_platformGamepad->buttonValues(); }
+ const Vector<SharedGamepadValue>& axisValues() const final { return m_platformGamepad->axisValues(); }
+ const Vector<SharedGamepadValue>& buttonValues() const final { return m_platformGamepad->buttonValues(); }
const char* source() const final { return m_platformGamepad->source(); }
Modified: trunk/Source/WebCore/platform/graphics/cocoa/SourceBufferParserWebM.cpp (265078 => 265079)
--- trunk/Source/WebCore/platform/graphics/cocoa/SourceBufferParserWebM.cpp 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/platform/graphics/cocoa/SourceBufferParserWebM.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -38,6 +38,7 @@
#include "SharedBuffer.h"
#include "VP9UtilitiesCocoa.h"
#include "VideoTrackPrivateWebM.h"
+#include "VideoToolboxSoftLink.h"
#include <_javascript_Core/DataView.h>
#include <pal/cf/CoreMediaSoftLink.h>
#include <webm/webm_parser.h>
Added: trunk/Source/WebCore/platform/mac/HIDDevice.cpp (0 => 265079)
--- trunk/Source/WebCore/platform/mac/HIDDevice.cpp (rev 0)
+++ trunk/Source/WebCore/platform/mac/HIDDevice.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "HIDDevice.h"
+
+#if PLATFORM(MAC)
+
+#include "HIDElement.h"
+#include "Logging.h"
+#include <IOKit/hid/IOHIDElement.h>
+#include <wtf/Deque.h>
+#include <wtf/HashSet.h>
+#include <wtf/cf/TypeCastsCF.h>
+
+WTF_DECLARE_CF_TYPE_TRAIT(IOHIDElement);
+
+namespace WebCore {
+
+HIDDevice::HIDDevice(IOHIDDeviceRef device)
+ : m_rawDevice(device)
+{
+ CFNumberRef cfVendorID = checked_cf_cast<CFNumberRef>(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)));
+ CFNumberRef cfProductID = checked_cf_cast<CFNumberRef>(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)));
+
+ int vendorID, productID;
+ CFNumberGetValue(cfVendorID, kCFNumberIntType, &vendorID);
+ CFNumberGetValue(cfProductID, kCFNumberIntType, &productID);
+
+ if (vendorID < 0 || vendorID > std::numeric_limits<uint16_t>::max()) {
+ LOG(HID, "Device attached with malformed vendor ID 0x%x. Resetting to 0.", vendorID);
+ vendorID = 0;
+ }
+ if (productID < 0 || productID > std::numeric_limits<uint16_t>::max()) {
+ LOG(HID, "Device attached with malformed product ID 0x%x. Resetting to 0.", productID);
+ productID = 0;
+ }
+
+ m_vendorID = (uint16_t)vendorID;
+ m_productID = (uint16_t)productID;
+
+ CFStringRef cfProductName = checked_cf_cast<CFStringRef>(IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)));
+ m_productName = cfProductName ? String(cfProductName) : String("Unknown"_s);
+}
+
+Vector<HIDElement> HIDDevice::uniqueInputElementsInDeviceTreeOrder() const
+{
+ HashSet<IOHIDElementCookie> encounteredCookies;
+ Deque<IOHIDElementRef> elementQueue;
+
+ RetainPtr<CFArrayRef> elements = adoptCF(IOHIDDeviceCopyMatchingElements(m_rawDevice.get(), NULL, kIOHIDOptionsTypeNone));
+ for (CFIndex i = 0; i < CFArrayGetCount(elements.get()); ++i)
+ elementQueue.append(checked_cf_cast<IOHIDElementRef>(CFArrayGetValueAtIndex(elements.get(), i)));
+
+ Vector<HIDElement> result;
+
+ while (!elementQueue.isEmpty()) {
+ auto element = elementQueue.takeFirst();
+ IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
+ if (encounteredCookies.contains(cookie))
+ continue;
+
+ switch (IOHIDElementGetType(element)) {
+ case kIOHIDElementTypeCollection: {
+ auto children = IOHIDElementGetChildren(element);
+ for (CFIndex i = CFArrayGetCount(children) - 1; i >= 0; --i)
+ elementQueue.prepend(checked_cf_cast<IOHIDElementRef>(CFArrayGetValueAtIndex(children, i)));
+ continue;
+ }
+ case kIOHIDElementTypeInput_Misc:
+ case kIOHIDElementTypeInput_Button:
+ case kIOHIDElementTypeInput_Axis:
+ encounteredCookies.add(cookie);
+ result.append(element);
+ continue;
+ default:
+ continue;
+ }
+ }
+
+ return result;
+}
+
+} // namespace WebCore
+
+#endif // PLATFORM(MAC)
Copied: trunk/Source/WebCore/platform/mac/HIDDevice.h (from rev 265078, trunk/Source/WebCore/platform/gamepad/PlatformGamepad.h) (0 => 265079)
--- trunk/Source/WebCore/platform/mac/HIDDevice.h (rev 0)
+++ trunk/Source/WebCore/platform/mac/HIDDevice.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if PLATFORM(MAC)
+
+#include <wtf/Forward.h>
+#include <wtf/RetainPtr.h>
+
+typedef struct CF_BRIDGED_TYPE(id) __IOHIDDevice * IOHIDDeviceRef;
+
+namespace WebCore {
+
+class HIDElement;
+
+class HIDDevice {
+ WTF_MAKE_FAST_ALLOCATED;
+public:
+ explicit HIDDevice(IOHIDDeviceRef);
+
+ IOHIDDeviceRef rawElement() const { return m_rawDevice.get(); }
+
+ // Walks the collection tree of all elements in the device as presented by IOKit.
+ // Adds each unique input element to the vector in the tree traversal order it was encountered.
+ // "Unique" is defined as "having a different IOHIDElementCookie from any previously added element"
+ Vector<HIDElement> uniqueInputElementsInDeviceTreeOrder() const;
+
+ uint16_t vendorID() const { return m_vendorID; }
+ uint16_t productID() const { return m_productID; }
+ const String& productName() const { return m_productName; }
+
+private:
+ RetainPtr<IOHIDDeviceRef> m_rawDevice;
+
+ uint16_t m_vendorID;
+ uint16_t m_productID;
+ String m_productName;
+};
+
+} // namespace WebCore
+
+#endif // PLATFORM(MAC)
Copied: trunk/Source/WebCore/platform/mac/HIDElement.cpp (from rev 265078, trunk/Source/WebKit/UIProcess/Gamepad/UIGamepad.h) (0 => 265079)
--- trunk/Source/WebCore/platform/mac/HIDElement.cpp (rev 0)
+++ trunk/Source/WebCore/platform/mac/HIDElement.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "HIDElement.h"
+
+#if PLATFORM(MAC)
+
+#include <IOKit/hid/IOHIDValue.h>
+
+namespace WebCore {
+
+HIDElement::HIDElement(IOHIDElementRef element)
+ : m_physicalMin(IOHIDElementGetPhysicalMin(element))
+ , m_physicalMax(IOHIDElementGetPhysicalMax(element))
+ , m_physicalValue(m_physicalMin)
+ , m_usage(IOHIDElementGetUsage(element))
+ , m_usagePage(IOHIDElementGetUsagePage(element))
+ , m_cookie(IOHIDElementGetCookie(element))
+ , m_rawElement(element)
+{
+}
+
+void HIDElement::valueChanged(IOHIDValueRef value)
+{
+ if (IOHIDValueGetElement(value) != m_rawElement.get()) {
+ LOG(HID, "HIDElement: Changed value whose IOHIDElement %p doesn't match %p", IOHIDValueGetElement(value), m_rawElement.get());
+ return;
+ }
+
+ m_physicalValue = IOHIDValueGetScaledValue(value, kIOHIDValueScaleTypePhysical);
+}
+
+} // namespace WebCore
+
+#endif // PLATFORM(MAC)
Copied: trunk/Source/WebCore/platform/mac/HIDElement.h (from rev 265078, trunk/Source/WebKit/UIProcess/Gamepad/UIGamepad.h) (0 => 265079)
--- trunk/Source/WebCore/platform/mac/HIDElement.h (rev 0)
+++ trunk/Source/WebCore/platform/mac/HIDElement.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if PLATFORM(MAC)
+
+#include <IOKit/hid/IOHIDDevice.h>
+#include <wtf/RetainPtr.h>
+
+namespace WebCore {
+
+class HIDElement {
+ WTF_MAKE_FAST_ALLOCATED;
+public:
+ explicit HIDElement(IOHIDElementRef);
+
+ IOHIDElementRef rawElement() const { return m_rawElement.get(); }
+
+ CFIndex physicalMin() const { return m_physicalMin; }
+ CFIndex physicalMax() const { return m_physicalMax; }
+ CFIndex physicalValue() const { return m_physicalValue; }
+ uint32_t usage() const { return m_usage; }
+ uint32_t usagePage() const { return m_usagePage; }
+ IOHIDElementCookie cookie() const { return m_cookie; }
+
+ void valueChanged(IOHIDValueRef);
+
+private:
+ CFIndex m_physicalMin;
+ CFIndex m_physicalMax;
+ CFIndex m_physicalValue;
+ uint32_t m_usage;
+ uint32_t m_usagePage;
+ IOHIDElementCookie m_cookie;
+ RetainPtr<IOHIDElementRef> m_rawElement;
+};
+
+
+} // namespace WebCore
+
+#endif // PLATFORM(MAC)
Modified: trunk/Source/WebCore/testing/MockGamepad.cpp (265078 => 265079)
--- trunk/Source/WebCore/testing/MockGamepad.cpp 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/testing/MockGamepad.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -41,8 +41,12 @@
{
m_id = gamepadID;
m_mapping = mapping;
- m_axisValues = Vector<double>(axisCount, 0.0);
- m_buttonValues = Vector<double>(buttonCount, 0.0);
+ m_axisValues.clear();
+ for (size_t i = 0; i < axisCount; ++i)
+ m_axisValues.append({ });
+ m_buttonValues.clear();
+ for (size_t i = 0; i < buttonCount; ++i)
+ m_buttonValues.append({ });
m_lastUpdateTime = MonotonicTime::now();
}
@@ -53,7 +57,7 @@
return false;
}
- m_axisValues[index] = value;
+ m_axisValues[index].setValue(value);
m_lastUpdateTime = MonotonicTime::now();
return true;
}
@@ -65,7 +69,7 @@
return false;
}
- m_buttonValues[index] = value;
+ m_buttonValues[index].setValue(value);
m_lastUpdateTime = MonotonicTime::now();
return true;
}
Modified: trunk/Source/WebCore/testing/MockGamepad.h (265078 => 265079)
--- trunk/Source/WebCore/testing/MockGamepad.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebCore/testing/MockGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -35,8 +35,8 @@
public:
MockGamepad(unsigned index, const String& gamepadID, const String& mapping, unsigned axisCount, unsigned buttonCount);
- const Vector<double>& axisValues() const final { return m_axisValues; }
- const Vector<double>& buttonValues() const final { return m_buttonValues; }
+ const Vector<SharedGamepadValue>& axisValues() const final { return m_axisValues; }
+ const Vector<SharedGamepadValue>& buttonValues() const final { return m_buttonValues; }
void updateDetails(const String& gamepadID, const String& mapping, unsigned axisCount, unsigned buttonCount);
bool setAxisValue(unsigned index, double value);
@@ -43,8 +43,8 @@
bool setButtonValue(unsigned index, double value);
private:
- Vector<double> m_axisValues;
- Vector<double> m_buttonValues;
+ Vector<SharedGamepadValue> m_axisValues;
+ Vector<SharedGamepadValue> m_buttonValues;
};
}
Modified: trunk/Source/WebKit/ChangeLog (265078 => 265079)
--- trunk/Source/WebKit/ChangeLog 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebKit/ChangeLog 2020-07-30 07:21:44 UTC (rev 265079)
@@ -1,3 +1,22 @@
+2020-07-30 Brady Eidson <[email protected]>
+
+ Refactor HID gamepad code to be much less fragile and much easier to hack on.
+ https://bugs.webkit.org/show_bug.cgi?id=214910
+
+ Reviewed by Darin Adler.
+
+ * Shared/Gamepad/GamepadData.cpp:
+ (WebKit::GamepadData::GamepadData):
+ * Shared/Gamepad/GamepadData.h:
+
+ * UIProcess/Gamepad/UIGamepad.h:
+
+ * WebProcess/Gamepad/WebGamepad.cpp:
+ (WebKit::WebGamepad::axisValues const):
+ (WebKit::WebGamepad::buttonValues const):
+ (WebKit::WebGamepad::updateValues):
+ * WebProcess/Gamepad/WebGamepad.h:
+
2020-07-30 Commit Queue <[email protected]>
Unreviewed, reverting r265043.
Modified: trunk/Source/WebKit/Shared/Gamepad/GamepadData.cpp (265078 => 265079)
--- trunk/Source/WebKit/Shared/Gamepad/GamepadData.cpp 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebKit/Shared/Gamepad/GamepadData.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -31,22 +31,24 @@
#include "ArgumentCoders.h"
#include <wtf/text/StringBuilder.h>
+using WebCore::SharedGamepadValue;
+
namespace WebKit {
-GamepadData::GamepadData(unsigned index, const Vector<double>& axisValues, const Vector<double>& buttonValues, MonotonicTime lastUpdateTime)
+GamepadData::GamepadData(unsigned index, const Vector<SharedGamepadValue>& axisValues, const Vector<SharedGamepadValue>& buttonValues, MonotonicTime lastUpdateTime)
: m_index(index)
- , m_axisValues(axisValues)
- , m_buttonValues(buttonValues)
+ , m_axisValues(WTF::map(axisValues, [](const auto& value) { return value.value(); }))
+ , m_buttonValues(WTF::map(buttonValues, [](const auto& value) { return value.value(); }))
, m_lastUpdateTime(lastUpdateTime)
{
}
-GamepadData::GamepadData(unsigned index, const String& id, const String& mapping, const Vector<double>& axisValues, const Vector<double>& buttonValues, MonotonicTime lastUpdateTime)
+GamepadData::GamepadData(unsigned index, const String& id, const String& mapping, const Vector<SharedGamepadValue>& axisValues, const Vector<SharedGamepadValue>& buttonValues, MonotonicTime lastUpdateTime)
: m_index(index)
, m_id(id)
, m_mapping(mapping)
- , m_axisValues(axisValues)
- , m_buttonValues(buttonValues)
+ , m_axisValues(WTF::map(axisValues, [](const auto& value) { return value.value(); }))
+ , m_buttonValues(WTF::map(buttonValues, [](const auto& value) { return value.value(); }))
, m_lastUpdateTime(lastUpdateTime)
{
}
Modified: trunk/Source/WebKit/Shared/Gamepad/GamepadData.h (265078 => 265079)
--- trunk/Source/WebKit/Shared/Gamepad/GamepadData.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebKit/Shared/Gamepad/GamepadData.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -27,6 +27,7 @@
#if ENABLE(GAMEPAD)
+#include <WebCore/SharedGamepadValue.h>
#include <wtf/MonotonicTime.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
@@ -45,8 +46,8 @@
{
}
- GamepadData(unsigned index, const Vector<double>& axisValues, const Vector<double>& buttonValues, MonotonicTime lastUpdateTime);
- GamepadData(unsigned index, const String& id, const String& mapping, const Vector<double>& axisValues, const Vector<double>& buttonValues, MonotonicTime lastUpdateTime);
+ GamepadData(unsigned index, const Vector<WebCore::SharedGamepadValue>& axisValues, const Vector<WebCore::SharedGamepadValue>& buttonValues, MonotonicTime lastUpdateTime);
+ GamepadData(unsigned index, const String& id, const String& mapping, const Vector<WebCore::SharedGamepadValue>& axisValues, const Vector<WebCore::SharedGamepadValue>& buttonValues, MonotonicTime lastUpdateTime);
void encode(IPC::Encoder&) const;
static Optional<GamepadData> decode(IPC::Decoder&);
Modified: trunk/Source/WebKit/UIProcess/Gamepad/UIGamepad.h (265078 => 265079)
--- trunk/Source/WebKit/UIProcess/Gamepad/UIGamepad.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebKit/UIProcess/Gamepad/UIGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -27,6 +27,7 @@
#if ENABLE(GAMEPAD)
+#include <WebCore/SharedGamepadValue.h>
#include <wtf/MonotonicTime.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
@@ -55,8 +56,8 @@
unsigned m_index;
String m_id;
String m_mapping;
- Vector<double> m_axisValues;
- Vector<double> m_buttonValues;
+ Vector<WebCore::SharedGamepadValue> m_axisValues;
+ Vector<WebCore::SharedGamepadValue> m_buttonValues;
MonotonicTime m_lastUpdateTime;
};
Modified: trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.cpp (265078 => 265079)
--- trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.cpp 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.cpp 2020-07-30 07:21:44 UTC (rev 265079)
@@ -31,6 +31,7 @@
#include "GamepadData.h"
#include "Logging.h"
+using WebCore::SharedGamepadValue;
namespace WebKit {
@@ -47,12 +48,12 @@
updateValues(gamepadData);
}
-const Vector<double>& WebGamepad::axisValues() const
+const Vector<SharedGamepadValue>& WebGamepad::axisValues() const
{
return m_axisValues;
}
-const Vector<double>& WebGamepad::buttonValues() const
+const Vector<SharedGamepadValue>& WebGamepad::buttonValues() const
{
return m_buttonValues;
}
@@ -64,9 +65,9 @@
ASSERT(m_axisValues.size() == gamepadData.axisValues().size());
ASSERT(m_buttonValues.size() == gamepadData.buttonValues().size());
- m_axisValues = gamepadData.axisValues();
- m_buttonValues = gamepadData.buttonValues();
+ m_axisValues = WTF::map(gamepadData.axisValues(), [](auto value) { return SharedGamepadValue(value); });
+ m_buttonValues = WTF::map(gamepadData.buttonValues(), [](auto value) { return SharedGamepadValue(value); });
m_lastUpdateTime = gamepadData.lastUpdateTime();
}
Modified: trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.h (265078 => 265079)
--- trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.h 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Source/WebKit/WebProcess/Gamepad/WebGamepad.h 2020-07-30 07:21:44 UTC (rev 265079)
@@ -39,14 +39,14 @@
public:
WebGamepad(const GamepadData&);
- const Vector<double>& axisValues() const override;
- const Vector<double>& buttonValues() const override;
+ const Vector<WebCore::SharedGamepadValue>& axisValues() const override;
+ const Vector<WebCore::SharedGamepadValue>& buttonValues() const override;
void updateValues(const GamepadData&);
private:
- Vector<double> m_axisValues;
- Vector<double> m_buttonValues;
+ Vector<WebCore::SharedGamepadValue> m_axisValues;
+ Vector<WebCore::SharedGamepadValue> m_buttonValues;
};
}
Modified: trunk/Tools/ChangeLog (265078 => 265079)
--- trunk/Tools/ChangeLog 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Tools/ChangeLog 2020-07-30 07:21:44 UTC (rev 265079)
@@ -1,3 +1,14 @@
+2020-07-30 Brady Eidson <[email protected]>
+
+ Refactor HID gamepad code to be much less fragile and much easier to hack on.
+ https://bugs.webkit.org/show_bug.cgi?id=214910
+
+ Reviewed by Darin Adler.
+
+ Add a test that verifies connect, basic button/axis changes, and disconnect of a HID gamepad.
+
+ * TestWebKitAPI/Tests/mac/HIDGamepads.mm:
+
2020-07-29 Fujii Hironori <[email protected]>
Replace 'http://svn.webkit.org' with 'https://svn.webkit.org' in webkitpy scripts
Modified: trunk/Tools/TestWebKitAPI/Tests/mac/HIDGamepads.mm (265078 => 265079)
--- trunk/Tools/TestWebKitAPI/Tests/mac/HIDGamepads.mm 2020-07-30 07:17:59 UTC (rev 265078)
+++ trunk/Tools/TestWebKitAPI/Tests/mac/HIDGamepads.mm 2020-07-30 07:21:44 UTC (rev 265079)
@@ -29,6 +29,7 @@
#import "InstanceMethodSwizzler.h"
#import "PlatformUtilities.h"
+#import "Test.h"
#import "TestURLSchemeHandler.h"
#import "TestWKWebView.h"
#import "VirtualGamepad.h"
@@ -59,7 +60,13 @@
window.webkit.messageHandlers.gamepad.postMessage(JSON.stringify(evt.gamepad.id));
}
+function handleGamepadDisconnect(evt)
+{
+ window.webkit.messageHandlers.gamepad.postMessage("Disconnect: " + JSON.stringify(evt.gamepad.id));
+}
+
addEventListener("gamepadconnected", handleGamepadConnect);
+addEventListener("gamepaddisconnected", handleGamepadDisconnect);
</script>
)GAMEPADRESOURCE";
@@ -196,6 +203,161 @@
EXPECT_EQ([webView.get().configuration.processPool _numberOfConnectedGameControllerFrameworkGamepadsForTesting], 3u);
}
+static const char* pollGamepadStateFunction = R"GAMEPADRESOURCE(
+var result = new Object();
+var gamepads = navigator.getGamepads();
+result.gamepadCount = gamepads.length;
+result.gamepadButtons = new Array;
+result.gamepadAxes = new Array;
+
+for (var i = 0; i < gamepads.length; ++i) {
+ result.gamepadButtons[i] = new Array;
+ for (var j = 0; j < gamepads[i].buttons.length; ++j)
+ result.gamepadButtons[i][j] = gamepads[i].buttons[j].value;
+
+ result.gamepadAxes[i] = new Array;
+ for (var j = 0; j < gamepads[i].axes.length; ++j)
+ result.gamepadAxes[i][j] = gamepads[i].axes[j];
+}
+
+return result;
+
+)GAMEPADRESOURCE";
+
+TEST(Gamepad, GamepadState)
+{
+ auto keyWindowSwizzler = makeUnique<InstanceMethodSwizzler>([NSApplication class], @selector(keyWindow), reinterpret_cast<IMP>(getKeyWindowForTesting));
+
+ auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+ auto messageHandler = adoptNS([[GamepadMessageHandler alloc] init]);
+ [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"gamepad"];
+
+ auto schemeHandler = adoptNS([[TestURLSchemeHandler alloc] init]);
+ [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"gamepad"];
+
+ [schemeHandler setStartURLSchemeTaskHandler:^(WKWebView *, id<WKURLSchemeTask> task) {
+ auto response = adoptNS([[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:0 textEncodingName:nil]);
+ [task didReceiveResponse:response.get()];
+ [task didReceiveData:[NSData dataWithBytes:mainBytes length:strlen(mainBytes)]];
+ [task didFinish];
+ }];
+
+ auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+ keyWindowForTesting = [webView window];
+ [webView synchronouslyLoadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"gamepad://host/main.html"]]];
+
+ [[webView window] makeFirstResponder:webView.get()];
+
+ // Resigning/reinstating the key window state triggers the "key window did change" notification that WKWebView currently
+ // needs to convince it to monitor gamepad devices
+ [[webView window] resignKeyWindow];
+ [[webView window] makeKeyWindow];
+
+ // Connect a gamepad and make it visible to the page
+ auto gamepad = makeUnique<VirtualGamepad>(VirtualGamepad::shenzhenLongshengweiTechnologyGamepadMapping());
+ while (![webView.get().configuration.processPool _numberOfConnectedGamepadsForTesting])
+ Util::sleep(0.01);
+
+ Vector<double> expectedButtons = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
+ Vector<double> expectedAxes = { -0.9921568627450981, -0.003921568627450966, -0.003921568627450966, -0.003921568627450966, -0.003921568627450966 };
+
+ auto updateStateAndPublish = [&] {
+ for (size_t i = 0; i < expectedButtons.size(); ++i)
+ gamepad->setButtonValue(i, expectedButtons[i]);
+ for (size_t i = 0; i < 5; ++i)
+ gamepad->setAxisValue(i, expectedAxes[i]);
+ gamepad->publishReport();
+ };
+
+ updateStateAndPublish();
+
+ // Wait for the page to tell us a gamepad connected
+ Util::run(&didReceiveMessage);
+ didReceiveMessage = false;
+
+ EXPECT_EQ(messageHandler.get().messages.size(), 1u);
+ EXPECT_TRUE([messageHandler.get().messages[0] isEqualToString:@"\"79-11-Virtual Shenzhen Longshengwei Technology Gamepad\""]);
+
+ bool done = false;
+ bool gotNewValues = false;
+ auto resultBlock = [&] (id result, NSError *error) {
+ EXPECT_NULL(error);
+ EXPECT_TRUE([result[@"gamepadCount"] isEqualToNumber:@(1)]);
+
+ bool areEqual = true;
+
+ for (size_t i = 0; i < 10; ++i) {
+ if (!WTF::areEssentiallyEqual([(NSNumber *)result[@"gamepadButtons"][0][i] doubleValue], expectedButtons[i]))
+ areEqual = false;
+ }
+
+ for (size_t i = 0; i < 5; ++i) {
+ if (!WTF::areEssentiallyEqual([(NSNumber *)result[@"gamepadAxes"][0][i] doubleValue], expectedAxes[i]))
+ areEqual = false;
+ }
+
+ if (areEqual)
+ gotNewValues = true;
+
+ done = true;
+ };
+
+ // Change some buttons, polling state to confirm
+ NSDate *start = [NSDate date];
+ while (!gotNewValues) {
+ [webView callAsyncJavaScript:@(pollGamepadStateFunction) arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:resultBlock];
+ Util::run(&done);
+ done = false;
+
+ if ([[NSDate date] timeIntervalSinceDate:start] > 1.0)
+ break;
+ }
+ EXPECT_TRUE(gotNewValues);
+ gotNewValues = false;
+
+ expectedButtons = { 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0 };
+ updateStateAndPublish();
+
+ start = [NSDate date];
+ while (!gotNewValues) {
+ [webView callAsyncJavaScript:@(pollGamepadStateFunction) arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:resultBlock];
+ Util::run(&done);
+ done = false;
+
+ if ([[NSDate date] timeIntervalSinceDate:start] > 1.0)
+ break;
+ }
+ EXPECT_TRUE(gotNewValues);
+ gotNewValues = false;
+
+ expectedAxes = { -1.0, -1.0, -1.0, -1.0, 1.0 };
+ updateStateAndPublish();
+
+ // Even though we set -1.0 for each "X" axis, the first 3 axes on this controller are always constant
+ expectedAxes = { -0.9921568627450981, -0.003921568627450966, -0.003921568627450966, -1.0, 1.0 };
+
+ start = [NSDate date];
+ while (!gotNewValues) {
+ [webView callAsyncJavaScript:@(pollGamepadStateFunction) arguments:nil inFrame:nil inContentWorld:WKContentWorld.pageWorld completionHandler:resultBlock];
+ Util::run(&done);
+ done = false;
+
+ if ([[NSDate date] timeIntervalSinceDate:start] > 1.0)
+ break;
+ }
+ EXPECT_TRUE(gotNewValues);
+ gotNewValues = false;
+
+ // Disconnect the gamepad
+ gamepad = nullptr;
+
+ Util::run(&didReceiveMessage);
+ didReceiveMessage = false;
+
+ EXPECT_EQ(messageHandler.get().messages.size(), 2u);
+ EXPECT_TRUE([messageHandler.get().messages[1] isEqualToString:@"Disconnect: \"79-11-Virtual Shenzhen Longshengwei Technology Gamepad\""]);
+}
+
} // namespace TestWebKitAPI
#endif // HAVE(HID_FRAMEWORK) && USE(APPLE_INTERNAL_SDK)