Skip to content

Commit f37c561

Browse files
committed
Convert the the USB device name UTF8
The USB device name is a UTF16 string but the `spndev.name` is a `char *`. This causes confusion when someone tries to `printf` the device name and gets only the first character, because the second byte is 0. To avoid this convert the device name to UTF8. For devices whose names are ASCII (most cases) this will result in a plain ASCIIZ string and avoid confusion. At the same time the driver API remains "simple" for embedded platforms where the USB HID implementation will not be HIDAPI and Unicode support may be missing (or too large to compile in). Fixes #6
1 parent 8b6be39 commit f37c561

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/usbdev.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,21 @@ int spndev_usb_open(struct spndev *dev, const char *devstr, uint16_t vend, uint1
145145
pidmatch = 1;
146146
hid_device* hiddev = hid_open_path(cinfo->path);
147147
if (hiddev) {
148+
size_t name_length;
149+
148150
if (!(dev->path = _strdup(cinfo->path))) {
149151
fprintf(stderr, "spndev_open: Failed to allocate device path\n");
150152
goto cleanup;
151153
}
152-
if (!(dev->name = (char*)_wcsdup(cinfo->product_string))) {
154+
155+
// The length of the UTF8 encoding of the UTF16 device name + 1 fot the 0
156+
name_length = 1 + wcsrtombs(0, &cinfo->product_string, 0, 0);
157+
if (!(dev->name = (char*)calloc(1, name_length))) {
153158
fprintf(stderr, "spndev_open: Failed to allocate device name\n");
154159
goto cleanup;
155160
}
161+
name_length = wcsrtombs(dev->name, &cinfo->product_string, name_length, 0);
162+
156163
if (-1 == hid_set_nonblocking(hiddev, 1)) {
157164
fprintf(stderr, "spndev_open: Failed to set non-blocking HID mode.\n");
158165
goto cleanup;

0 commit comments

Comments
 (0)