Skip to content

Commit

Permalink
Added a hotplug cleanup procedure in hid_exit()
Browse files Browse the repository at this point in the history
  • Loading branch information
k1-801 committed Nov 21, 2023
1 parent 35a5253 commit ea5182d
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,36 @@ int HID_API_EXPORT hid_init(void)
return 0;
}

static void hid_internal_cleanup_hotplugs()
{
/* Unregister a HID device connection notification when removing the last callback */
/* This function is always called inside a locked mutex */
if (hid_hotplug_context.hotplug_cbs == NULL) {
if(hid_hotplug_context.devs) {
/* Cleanup connected device list */
hid_free_enumeration(hid_hotplug_context.devs);
hid_hotplug_context.devs = NULL;
}

if (CM_Unregister_Notification(hid_hotplug_context.notify_handle) != CR_SUCCESS) {
register_global_error(L"hid_hotplug_deregister_callback/CM_Unregister_Notification");
return;
}

hid_hotplug_context.notify_handle = NULL;
}
}

int HID_API_EXPORT hid_exit(void)
{
#ifndef HIDAPI_USE_DDK
free_library_handles();
hidapi_initialized = FALSE;
#endif
WaitForSingleObject(hid_hotplug_context.mutex, INFINITE);
hid_internal_cleanup_hotplugs();
ReleaseMutex(hid_hotplug_context.mutex);
CloseHandle(hid_hotplug_context.mutex);
register_global_error(NULL);
return 0;
}
Expand Down Expand Up @@ -923,29 +947,6 @@ struct hid_hotplug_callback {
struct hid_hotplug_callback *next;
};

static int hid_internal_cleanup_hotplugs()
{
/* Unregister a HID device connection notification when removing the last callback */
if (hid_hotplug_context.hotplug_cbs == NULL) {
/* Cleanup connected device list */
hid_free_enumeration(hid_hotplug_context.devs);
hid_hotplug_context.devs = NULL;

if (hid_hotplug_context.notify_handle == NULL) {
register_global_error(L"Device notification have already been unregistered");
return -1;
}

if (CM_Unregister_Notification(hid_hotplug_context.notify_handle) != CR_SUCCESS) {
register_global_error(L"hid_hotplug_deregister_callback/CM_Unregister_Notification");
return -1;
}

hid_hotplug_context.notify_handle = NULL;
}
return 0;
}

DWORD WINAPI hid_internal_notify_callback(HCMNOTIFICATION notify, PVOID context, CM_NOTIFY_ACTION action, PCM_NOTIFY_EVENT_DATA event_data, DWORD event_data_size)
{
struct hid_device_info* device = NULL;
Expand Down

0 comments on commit ea5182d

Please sign in to comment.