From ac84fb62527dfc3b0de1f0066fb8c8f89d96d2e0 Mon Sep 17 00:00:00 2001 From: Iakov Kirilenko Date: Tue, 4 Jun 2019 06:24:26 +0300 Subject: [PATCH 1/6] Explicit cast from void* to corresponding type Fix compilation without -fpermissive --- libusb/hid.c | 14 +++++++------- linux/hid.c | 6 +++--- mac/hid.c | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/libusb/hid.c b/libusb/hid.c index 3c6d877f8..aaf3231df 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -182,7 +182,7 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length); static hid_device *new_hid_device(void) { - hid_device *dev = calloc(1, sizeof(hid_device)); + hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); dev->blocking = 1; pthread_mutex_init(&dev->mutex, NULL); @@ -430,7 +430,7 @@ static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx) Skip over the first character (2-bytes). */ len -= 2; - str = malloc((len / 2 + 1) * sizeof(wchar_t)); + str = (wchar_t*) malloc((len / 2 + 1) * sizeof(wchar_t)); int i; for (i = 0; i < len / 2; i++) { str[i] = buf[i * 2 + 2] | (buf[i * 2 + 3] << 8); @@ -563,7 +563,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, struct hid_device_info *tmp; /* VID/PID match. Create the record. */ - tmp = calloc(1, sizeof(struct hid_device_info)); + tmp = (struct hid_device_info*) calloc(1, sizeof(struct hid_device_info)); if (cur_dev) { cur_dev->next = tmp; } @@ -736,8 +736,8 @@ static void read_callback(struct libusb_transfer *transfer) if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { - struct input_report *rpt = malloc(sizeof(*rpt)); - rpt->data = malloc(transfer->actual_length); + struct input_report *rpt = (struct input_report*) malloc(sizeof(*rpt)); + rpt->data = (uint8_t *) malloc(transfer->actual_length); memcpy(rpt->data, transfer->buffer, transfer->actual_length); rpt->len = transfer->actual_length; rpt->next = NULL; @@ -799,11 +799,11 @@ static void read_callback(struct libusb_transfer *transfer) static void *read_thread(void *param) { hid_device *dev = param; - unsigned char *buf; + uint8_t *buf; const size_t length = dev->input_ep_max_packet_size; /* Set up the transfer object. */ - buf = malloc(length); + buf = (uint8_t*) malloc(length); dev->transfer = libusb_alloc_transfer(0); libusb_fill_interrupt_transfer(dev->transfer, dev->device_handle, diff --git a/linux/hid.c b/linux/hid.c index 56dac0fab..93beda040 100644 --- a/linux/hid.c +++ b/linux/hid.c @@ -103,7 +103,7 @@ static __u32 detect_kernel_version(void) static hid_device *new_hid_device(void) { - hid_device *dev = calloc(1, sizeof(hid_device)); + hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); dev->device_handle = -1; dev->blocking = 1; dev->uses_numbered_reports = 0; @@ -122,7 +122,7 @@ static wchar_t *utf8_to_wchar_t(const char *utf8) if ((size_t) -1 == wlen) { return wcsdup(L""); } - ret = calloc(wlen+1, sizeof(wchar_t)); + ret = (wchar_t*) calloc(wlen+1, sizeof(wchar_t)); mbstowcs(ret, utf8, wlen+1); ret[wlen] = 0x0000; } @@ -461,7 +461,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, struct hid_device_info *tmp; /* VID/PID match. Create the record. */ - tmp = malloc(sizeof(struct hid_device_info)); + tmp = (struct hid_device_info*) malloc(sizeof(struct hid_device_info)); if (cur_dev) { cur_dev->next = tmp; } diff --git a/mac/hid.c b/mac/hid.c index e0756a158..722d3d58a 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -124,7 +124,7 @@ struct hid_device_ { static hid_device *new_hid_device(void) { - hid_device *dev = calloc(1, sizeof(hid_device)); + hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device)); dev->device_handle = NULL; dev->blocking = 1; dev->uses_numbered_reports = 0; @@ -281,7 +281,7 @@ static int get_product_string(IOHIDDeviceRef device, wchar_t *buf, size_t len) static wchar_t *dup_wcs(const wchar_t *s) { size_t len = wcslen(s); - wchar_t *ret = malloc((len+1)*sizeof(wchar_t)); + wchar_t *ret = (wchar_t*) malloc((len+1)*sizeof(wchar_t)); wcscpy(ret, s); return ret; @@ -410,7 +410,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, /* Convert the list into a C array so we can iterate easily. */ num_devices = CFSetGetCount(device_set); - IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef)); + IOHIDDeviceRef *device_array = (IOHIDDeviceRef*) calloc(num_devices, sizeof(IOHIDDeviceRef)); CFSetGetValues(device_set, (const void **) device_array); /* Iterate over each device, making an entry for it. */ @@ -437,7 +437,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, io_string_t path; /* VID/PID match. Create the record. */ - tmp = malloc(sizeof(struct hid_device_info)); + tmp = (struct hid_device_info*) malloc(sizeof(struct hid_device_info)); if (cur_dev) { cur_dev->next = tmp; } @@ -561,8 +561,8 @@ static void hid_report_callback(void *context, IOReturn result, void *sender, hid_device *dev = context; /* Make a new Input Report object */ - rpt = calloc(1, sizeof(struct input_report)); - rpt->data = calloc(1, report_length); + rpt = (input_report*) calloc(1, sizeof(struct input_report)); + rpt->data = (uint8_t*) calloc(1, report_length); memcpy(rpt->data, report, report_length); rpt->len = report_length; rpt->next = NULL; @@ -710,7 +710,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) /* Create the buffers for receiving data */ dev->max_input_report_len = (CFIndex) get_max_report_length(dev->device_handle); - dev->input_report_buf = calloc(dev->max_input_report_len, sizeof(uint8_t)); + dev->input_report_buf = (uint8_t*) calloc(dev->max_input_report_len, sizeof(uint8_t)); /* Create the Run Loop Mode for this device. printing the reference seems to work. */ From d3af10b27b2331e53a7220cb160b6aff74b5b5ae Mon Sep 17 00:00:00 2001 From: Iakov Kirilenko Date: Thu, 6 Jun 2019 00:57:33 +0300 Subject: [PATCH 2/6] More rigorous & even C++ compatible code for macOS --- mac/hid.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mac/hid.c b/mac/hid.c index 722d3d58a..e780d7971 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -226,7 +226,7 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t if (!len) return 0; - str = IOHIDDeviceGetProperty(device, prop); + str = (CFStringRef)IOHIDDeviceGetProperty(device, prop); buf[0] = 0; @@ -296,7 +296,8 @@ static wchar_t *dup_wcs(const wchar_t *s) static io_service_t hidapi_IOHIDDeviceGetService(IOHIDDeviceRef device) { static void *iokit_framework = NULL; - static io_service_t (*dynamic_IOHIDDeviceGetService)(IOHIDDeviceRef device) = NULL; + typedef io_service_t (*dynamic_IOHIDDeviceGetService_t)(IOHIDDeviceRef device); + static dynamic_IOHIDDeviceGetService_t dynamic_IOHIDDeviceGetService; /* Use dlopen()/dlsym() to get a pointer to IOHIDDeviceGetService() if it exists. * If any of these steps fail, dynamic_IOHIDDeviceGetService will be left NULL @@ -306,7 +307,7 @@ static io_service_t hidapi_IOHIDDeviceGetService(IOHIDDeviceRef device) iokit_framework = dlopen("/System/Library/IOKit.framework/IOKit", RTLD_LAZY); if (iokit_framework != NULL) - dynamic_IOHIDDeviceGetService = dlsym(iokit_framework, "IOHIDDeviceGetService"); + dynamic_IOHIDDeviceGetService = (dynamic_IOHIDDeviceGetService_t)dlsym(iokit_framework, "IOHIDDeviceGetService"); } if (dynamic_IOHIDDeviceGetService != NULL) { @@ -544,7 +545,7 @@ static void hid_device_removal_callback(void *context, IOReturn result, void *sender) { /* Stop the Run Loop for this device. */ - hid_device *d = context; + hid_device *d = (hid_device*)context; d->disconnected = 1; CFRunLoopStop(d->run_loop); @@ -558,7 +559,7 @@ static void hid_report_callback(void *context, IOReturn result, void *sender, uint8_t *report, CFIndex report_length) { struct input_report *rpt; - hid_device *dev = context; + hid_device *dev = (hid_device*)context; /* Make a new Input Report object */ rpt = (input_report*) calloc(1, sizeof(struct input_report)); @@ -605,13 +606,13 @@ static void hid_report_callback(void *context, IOReturn result, void *sender, hid_close(), and serves to stop the read_thread's run loop. */ static void perform_signal_callback(void *context) { - hid_device *dev = context; + hid_device *dev = (hid_device*)context; CFRunLoopStop(dev->run_loop); /*TODO: CFRunLoopGetCurrent()*/ } static void *read_thread(void *param) { - hid_device *dev = param; + hid_device *dev = (hid_device*)param; SInt32 code; /* Move the device's run loop to this thread. */ @@ -682,6 +683,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) { hid_device *dev = NULL; io_registry_entry_t entry = MACH_PORT_NULL; + IOReturn ret; dev = new_hid_device(); @@ -704,7 +706,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) } /* Open the IOHIDDevice */ - IOReturn ret = IOHIDDeviceOpen(dev->device_handle, kIOHIDOptionsTypeSeizeDevice); + ret = IOHIDDeviceOpen(dev->device_handle, kIOHIDOptionsTypeSeizeDevice); if (ret == kIOReturnSuccess) { char str[32]; From 03db861bf5720c75cb4ee6cdcc592c6922281ec7 Mon Sep 17 00:00:00 2001 From: Iakov Kirilenko Date: Thu, 6 Jun 2019 11:11:44 +0300 Subject: [PATCH 3/6] Codestyle conformance: add spaces after type-case --- mac/hid.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mac/hid.c b/mac/hid.c index e780d7971..5a61faa1a 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -226,7 +226,7 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t if (!len) return 0; - str = (CFStringRef)IOHIDDeviceGetProperty(device, prop); + str = (CFStringRef) IOHIDDeviceGetProperty(device, prop); buf[0] = 0; @@ -239,11 +239,11 @@ static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t len --; range.location = 0; - range.length = ((size_t)str_len > len)? len: (size_t)str_len; + range.length = ((size_t) str_len > len)? len: (size_t) str_len; chars_copied = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, - (char)'?', + (char) '?', FALSE, (UInt8*)buf, len * sizeof(wchar_t), @@ -307,7 +307,7 @@ static io_service_t hidapi_IOHIDDeviceGetService(IOHIDDeviceRef device) iokit_framework = dlopen("/System/Library/IOKit.framework/IOKit", RTLD_LAZY); if (iokit_framework != NULL) - dynamic_IOHIDDeviceGetService = (dynamic_IOHIDDeviceGetService_t)dlsym(iokit_framework, "IOHIDDeviceGetService"); + dynamic_IOHIDDeviceGetService = (dynamic_IOHIDDeviceGetService_t) dlsym(iokit_framework, "IOHIDDeviceGetService"); } if (dynamic_IOHIDDeviceGetService != NULL) { @@ -339,7 +339,7 @@ static io_service_t hidapi_IOHIDDeviceGetService(IOHIDDeviceRef device) #endif io_service_t service; }; - struct IOHIDDevice_internal *tmp = (struct IOHIDDevice_internal *)device; + struct IOHIDDevice_internal *tmp = (struct IOHIDDevice_internal *) device; return tmp->service; } @@ -545,7 +545,7 @@ static void hid_device_removal_callback(void *context, IOReturn result, void *sender) { /* Stop the Run Loop for this device. */ - hid_device *d = (hid_device*)context; + hid_device *d = (hid_device*) context; d->disconnected = 1; CFRunLoopStop(d->run_loop); @@ -559,7 +559,7 @@ static void hid_report_callback(void *context, IOReturn result, void *sender, uint8_t *report, CFIndex report_length) { struct input_report *rpt; - hid_device *dev = (hid_device*)context; + hid_device *dev = (hid_device*) context; /* Make a new Input Report object */ rpt = (input_report*) calloc(1, sizeof(struct input_report)); @@ -606,13 +606,13 @@ static void hid_report_callback(void *context, IOReturn result, void *sender, hid_close(), and serves to stop the read_thread's run loop. */ static void perform_signal_callback(void *context) { - hid_device *dev = (hid_device*)context; + hid_device *dev = (hid_device*) context; CFRunLoopStop(dev->run_loop); /*TODO: CFRunLoopGetCurrent()*/ } static void *read_thread(void *param) { - hid_device *dev = (hid_device*)param; + hid_device *dev = (hid_device*) param; SInt32 code; /* Move the device's run loop to this thread. */ From c73a9fa2a689724419e0133b38f92cd33160ea51 Mon Sep 17 00:00:00 2001 From: iakov Date: Sat, 8 Jun 2019 16:32:56 +0300 Subject: [PATCH 4/6] Add missed NULL initialisation to local variable Co-Authored-By: Ihor Dutchak --- mac/hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mac/hid.c b/mac/hid.c index 5a61faa1a..6f72c0f8f 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -297,7 +297,7 @@ static io_service_t hidapi_IOHIDDeviceGetService(IOHIDDeviceRef device) { static void *iokit_framework = NULL; typedef io_service_t (*dynamic_IOHIDDeviceGetService_t)(IOHIDDeviceRef device); - static dynamic_IOHIDDeviceGetService_t dynamic_IOHIDDeviceGetService; + static dynamic_IOHIDDeviceGetService_t dynamic_IOHIDDeviceGetService = NULL; /* Use dlopen()/dlsym() to get a pointer to IOHIDDeviceGetService() if it exists. * If any of these steps fail, dynamic_IOHIDDeviceGetService will be left NULL From 139a79d61700e40ed137d9d06e7d992544275c17 Mon Sep 17 00:00:00 2001 From: Iakov 'Jake' Kirilenko Date: Sat, 8 Jun 2019 16:41:11 +0300 Subject: [PATCH 5/6] Initialize local variable with smth meaningful I prefer explicit INVALID rather than possible compiler warning about uninitialized var sometimes later --- mac/hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mac/hid.c b/mac/hid.c index 6f72c0f8f..7d13cd339 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -683,7 +683,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) { hid_device *dev = NULL; io_registry_entry_t entry = MACH_PORT_NULL; - IOReturn ret; + IOReturn ret = kIOReturnInvalid; dev = new_hid_device(); From 5326fa0c99fd0ab614e94f913c68e1951bb9b274 Mon Sep 17 00:00:00 2001 From: iakov Date: Wed, 19 Jun 2019 13:19:13 +0300 Subject: [PATCH 6/6] Remove excess incorrect space Co-Authored-By: Filip Kubicz --- libusb/hid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libusb/hid.c b/libusb/hid.c index aaf3231df..28fb8fe5a 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -737,7 +737,7 @@ static void read_callback(struct libusb_transfer *transfer) if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { struct input_report *rpt = (struct input_report*) malloc(sizeof(*rpt)); - rpt->data = (uint8_t *) malloc(transfer->actual_length); + rpt->data = (uint8_t*) malloc(transfer->actual_length); memcpy(rpt->data, transfer->buffer, transfer->actual_length); rpt->len = transfer->actual_length; rpt->next = NULL;