Class cleanups.
@@ -229,19 +229,16 @@ class HIDValue:
# e.g. the PS3 controller has a 39-byte HIDValue element.
# We currently do not try to handle these cases.
self.intvalue = None
- elementRef = c_void_p(iokit.IOHIDValueGetElement(valueRef))
- self.element = HIDDeviceElement.get_element(elementRef)
+ element_ref = c_void_p(iokit.IOHIDValueGetElement(valueRef))
-class HIDDevice:
- @classmethod
- def get_device(cls, deviceRef):
- # deviceRef is a c_void_p pointing to an IOHIDDeviceRef
- if deviceRef.value in _device_lookup:
- return _device_lookup[deviceRef.value]
+ if element_ref.value in _element_lookup:
+ self.element = _element_lookup[element_ref.value]
else:
- device = HIDDevice(deviceRef)
- return device
+ self.element = HIDDeviceElement(element_ref)
+
+
+class HIDDevice:
def __init__(self, deviceRef):
# Check that we've got a valid IOHIDDevice.
@@ -403,16 +400,11 @@ class HIDDevice:
else:
return None
+ def __repr__(self):
+ return f"{self.__class__.__name__}(name={self.product})"
+
class HIDDeviceElement:
- @classmethod
- def get_element(cls, elementRef):
- # elementRef is a c_void_p pointing to an IOHIDDeviceElementRef
- if elementRef.value in _element_lookup:
- return _element_lookup[elementRef.value]
- else:
- element = HIDDeviceElement(elementRef)
- return element
def __init__(self, elementRef):
# Check that we've been passed a valid IOHIDElement.
@@ -491,7 +483,13 @@ class HIDManager(EventDispatcher):
kCFRunLoopDefaultMode)
def _py_matching_callback(self, context, result, sender, device):
- d = HIDDevice.get_device(c_void_p(device))
+ device_ref = c_void_p(device)
+
+ if device_ref.value in _device_lookup:
+ d = _device_lookup[device_ref.value]
+ else:
+ d = HIDDevice(device_ref)
+
if d not in self.devices:
self.devices.add(d)
for x in self.matching_observers:
@@ -508,13 +506,6 @@ HIDManager.register_event_type('on_connect')
HIDManager.register_event_type('on_disconnect')
-######################################################################
-# Add conversion methods for IOHIDDevices and IOHIDDeviceElements
-# to the list of known types used by cftype_to_value.
-known_cftypes[iokit.IOHIDDeviceGetTypeID()] = HIDDevice.get_device
-known_cftypes[iokit.IOHIDElementGetTypeID()] = HIDDeviceElement.get_element
-######################################################################
-
# Pyglet interface to HID
@@ -691,9 +682,12 @@ def get_apple_remote(display=None):
def _create_controller(device, display):
- if device.transport and device.transport.upper() in ('USB', 'BLUETOOTH'):
- mapping = get_mapping(device.get_guid())
- if mapping:
+ if not device.transport and device.transport.upper() in ('USB', 'BLUETOOTH'):
+ return
+
+ if device.is_joystick() or device.is_gamepad() or device.is_multi_axis():
+
+ if mapping := get_mapping(device.get_guid()):
return Controller(PygletDevice(display, device, _hid_manager), mapping)
else:
warnings.warn(f"Warning: {device} (GUID: {device.get_guid()}) "
