@@ -114,50 +114,80 @@ def getDriversForConnectedUsbDevices():
114114 @return: Pairs of drivers and device information.
115115 @rtype: generator of (str, L{DeviceMatch}) tuples
116116 """
117- usbDevs = itertools .chain (
118- (DeviceMatch (KEY_CUSTOM , port ["usbID" ], port ["devicePath" ], port )
119- for port in deviceInfoFetcher .usbDevices ),
120- (DeviceMatch (KEY_HID , port ["usbID" ], port ["devicePath" ], port )
121- for port in deviceInfoFetcher .hidDevices if port ["provider" ]== "usb" ),
122- (DeviceMatch (KEY_SERIAL , port ["usbID" ], port ["port" ], port )
123- for port in deviceInfoFetcher .comPorts if "usbID" in port )
117+ usbCustomDeviceMatches = (
118+ DeviceMatch (KEY_CUSTOM , port ["usbID" ], port ["devicePath" ], port )
119+ for port in deviceInfoFetcher .usbDevices
124120 )
125- for match in usbDevs :
121+ usbComDeviceMatches = (
122+ DeviceMatch (KEY_SERIAL , port ["usbID" ], port ["port" ], port )
123+ for port in deviceInfoFetcher .comPorts
124+ if "usbID" in port
125+ )
126+ usbHidDeviceMatches , usbHidDeviceMatchesForCustom = itertools .tee ((
127+ DeviceMatch (KEY_HID , port ["usbID" ], port ["devicePath" ], port )
128+ for port in deviceInfoFetcher .hidDevices
129+ if port ["provider" ] == "usb"
130+ ))
131+ for match in itertools .chain (usbCustomDeviceMatches , usbHidDeviceMatchesForCustom , usbComDeviceMatches ):
126132 for driver , devs in _driverDevices .items ():
127133 for type , ids in devs .items ():
128134 if match .type == type and match .id in ids :
129135 yield driver , match
136+
137+ for match in usbHidDeviceMatches :
130138 # Check for the Braille HID protocol after any other device matching.
131139 # This ensures that a vendor specific driver is preferred over the braille HID protocol.
132140 # This preference may change in the future.
133- if match .type == KEY_HID and match .deviceInfo .get ('HIDUsagePage' ) == HID_USAGE_PAGE_BRAILLE :
134- yield ("hid" , match )
141+ if _isHIDBrailleMatch (match ):
142+ yield (
143+ _getStandardHidDriverName (),
144+ match
145+ )
146+
147+
148+ def _getStandardHidDriverName () -> str :
149+ """Return the name of the standard HID Braille device driver
150+ """
151+ import brailleDisplayDrivers .hidBrailleStandard
152+ return brailleDisplayDrivers .hidBrailleStandard .HidBrailleDriver .name
153+
154+
155+ def _isHIDBrailleMatch (match : DeviceMatch ) -> bool :
156+ return match .type == KEY_HID and match .deviceInfo .get ('HIDUsagePage' ) == HID_USAGE_PAGE_BRAILLE
135157
136158
137159def getDriversForPossibleBluetoothDevices ():
138160 """Get any matching drivers for possible Bluetooth devices.
139161 @return: Pairs of drivers and port information.
140162 @rtype: generator of (str, L{DeviceMatch}) tuples
141163 """
142- btDevs = itertools .chain (
143- (DeviceMatch (KEY_SERIAL , port ["bluetoothName" ], port ["port" ], port )
144- for port in deviceInfoFetcher .comPorts
145- if "bluetoothName" in port ),
146- (DeviceMatch (KEY_HID , port ["hardwareID" ], port ["devicePath" ], port )
147- for port in deviceInfoFetcher .hidDevices if port ["provider" ]== "bluetooth" ),
164+ btSerialMatchesForCustom = (
165+ DeviceMatch (KEY_SERIAL , port ["bluetoothName" ], port ["port" ], port )
166+ for port in deviceInfoFetcher .comPorts
167+ if "bluetoothName" in port
148168 )
149- for match in btDevs :
169+ btHidDevMatchesForHid , btHidDevMatchesForCustom = itertools .tee ((
170+ DeviceMatch (KEY_HID , port ["hardwareID" ], port ["devicePath" ], port )
171+ for port in deviceInfoFetcher .hidDevices
172+ if port ["provider" ] == "bluetooth"
173+ ))
174+ for match in itertools .chain (btSerialMatchesForCustom , btHidDevMatchesForCustom ):
150175 for driver , devs in _driverDevices .items ():
151176 matchFunc = devs [KEY_BLUETOOTH ]
152177 if not callable (matchFunc ):
153178 continue
154179 if matchFunc (match ):
155180 yield driver , match
181+
182+ for match in btHidDevMatchesForHid :
156183 # Check for the Braille HID protocol after any other device matching.
157184 # This ensures that a vendor specific driver is preferred over the braille HID protocol.
158185 # This preference may change in the future.
159- if match .type == KEY_HID and match .deviceInfo .get ('HIDUsagePage' ) == HID_USAGE_PAGE_BRAILLE :
160- yield ("hid" , match )
186+ if _isHIDBrailleMatch (match ):
187+ yield (
188+ _getStandardHidDriverName (),
189+ match
190+ )
161191
162192
163193class _DeviceInfoFetcher (AutoPropertyObject ):
@@ -349,8 +379,8 @@ def getConnectedUsbDevicesForDriver(driver) -> Iterable[DeviceMatch]:
349379 for port in deviceInfoFetcher .comPorts if "usbID" in port )
350380 )
351381 for match in usbDevs :
352- if driver == "hid" :
353- if match . type == KEY_HID and match . deviceInfo . get ( 'HIDUsagePage' ) == HID_USAGE_PAGE_BRAILLE :
382+ if driver == _getStandardHidDriverName () :
383+ if _isHIDBrailleMatch ( match ) :
354384 yield match
355385 else :
356386 devs = _driverDevices [driver ]
@@ -365,9 +395,9 @@ def getPossibleBluetoothDevicesForDriver(driver) -> Iterable[DeviceMatch]:
365395 @return: Port information for each port.
366396 @raise LookupError: If there is no detection data for this driver.
367397 """
368- if driver == "hid" :
398+ if driver == _getStandardHidDriverName () :
369399 def matchFunc (match ):
370- return match . type == KEY_HID and match . deviceInfo . get ( 'HIDUsagePage' ) == HID_USAGE_PAGE_BRAILLE
400+ return _isHIDBrailleMatch ( match )
371401 else :
372402 matchFunc = _driverDevices [driver ][KEY_BLUETOOTH ]
373403 if not callable (matchFunc ):
0 commit comments