cli: let devices refine the product name via getMetadata() - #575
Merged
Conversation
getMetadata() existed in the device API but was never called from the CLI, so a device that knows more than the USB strings (for example which headset is currently paired to a generic dongle) could not show it. Ask the device once, on the handle already opened for the first feature request; no extra device opens, and the default implementation returns the same HID strings, so other devices are unaffected.
…locale refineProductName() widened the device-supplied name with std::wstring(s.begin(), s.end()). That is wrong in two ways: char is signed on most platforms, so a byte above 0x7F sign-extends into a negative wchar_t, and a multi-byte UTF-8 sequence becomes one wide character per byte rather than one per character. It happens to survive on macOS because wcstombs() masks the value back on the way out, but on Windows the same bytes come out as mojibake and on glibc they hit the '?' fallback. Decoding it properly is not enough on its own: the value is converted straight back with wstring_to_string(), which goes through wcstombs() and so can only represent what the current locale encodes. Nothing calls setlocale(), so in the default C locale every non-ASCII character became '?'. DeviceMetadata and the HID descriptor strings are defined to be UTF-8, so this adds an explicit, locale-independent pair for them - string_to_wstring() and wstring_to_utf8() - and uses it wherever those strings are converted. The pair is an exact inverse, verified in C, en_US.UTF-8 and de_DE.UTF-8. Surrogate pairs are handled for the 16-bit wchar_t on Windows, and malformed input becomes U+FFFD rather than discarding the rest of the name. hid_error() keeps using wstring_to_string(): those really are platform strings in the local encoding, not UTF-8. This also fixes non-ASCII manufacturer and product strings read from the HID descriptor, which were already being mangled before any of this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes made
For Dongles which don't create sub-devices for their paired headsets the HID metadata can give more information about the the headset model.
Checklist