Skip to content

Commit

Permalink
address @tormodvolden comments. TO BE SQUASHED WITH PREVIOUS COMMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
sonatique committed Feb 7, 2024
1 parent a70c29c commit 26b9eca
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 52 deletions.
6 changes: 3 additions & 3 deletions libusb/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ OS_OPENBSD_SRC = os/openbsd_usb.c
OS_SUNOS_SRC = os/sunos_usb.h os/sunos_usb.c
OS_WINDOWS_SRC = libusb-1.0.def libusb-1.0.rc \
os/windows_common.h os/windows_common.c \
os/windows_hotplug.h os/windows_hotplug.c \
os/windows_usbdk.h os/windows_usbdk.c \
os/windows_winusb.h os/windows_winusb.c \
os/windows_hotplug.h os/windows_hotplug.c

os/windows_winusb.h os/windows_winusb.c

if OS_DARWIN
OS_SRC = $(OS_DARWIN_SRC)
endif
Expand Down
2 changes: 1 addition & 1 deletion libusb/hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static void usbi_recursively_remove_parents(struct libusb_device *dev, struct li
* equal to next_dev given we know at this point that it was
* previously seen in the list. */
assert (dev->parent_dev != next_dev);
if(dev->parent_dev->list.next && dev->parent_dev->list.prev) {
if (dev->parent_dev->list.next && dev->parent_dev->list.prev) {
list_del(&dev->parent_dev->list);
}
}
Expand Down
14 changes: 5 additions & 9 deletions libusb/os/windows_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,13 @@ static int windows_init(struct libusb_context *ctx)
r = LIBUSB_SUCCESS;

if (init_count == 1) {
r = windows_start_event_monitor();
}
if (r != LIBUSB_SUCCESS) {
usbi_err(ctx, "error starting hotplug event monitor");

if (init_count == 1) {
windows_stop_event_monitor();
r = windows_start_event_monitor(); // Start up hotplug event handler
if (r != LIBUSB_SUCCESS) {
usbi_err(ctx, "error starting hotplug event monitor");
goto init_exit;
}
goto init_exit;
}

windows_initial_scan_devices(ctx);

init_exit: // Holds semaphore here
Expand Down
77 changes: 40 additions & 37 deletions libusb/os/windows_hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* This updates the hotplug status of each device to one of three values {UNCHANGED, ARRIVED, LEFT}.
* 3. According to the value, we generate events to libusb client via hotplug callbacks. */

static HWND windows_event_hWnd;
static HWND windows_event_hwnd;
static HANDLE windows_event_thread_handle;
static DWORD WINAPI windows_event_thread_main(LPVOID lpParam);
static LRESULT CALLBACK windows_proc_callback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK windows_proc_callback(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

#define log_error(operation) do { \
usbi_err(NULL, "%s failed with error: %s", operation, windows_error_str(0)); \
Expand All @@ -26,15 +26,15 @@ static LRESULT CALLBACK windows_proc_callback(HWND hWnd, UINT message, WPARAM wP
int windows_start_event_monitor(void)
{
windows_event_thread_handle = CreateThread(
NULL,
0,
NULL, // Default security descriptor
0, // Default stack size
windows_event_thread_main,
NULL,
0,
NULL
NULL, // No parameters to pass to the thread
0, // Start immediately
NULL // No need to keep track of thread ID
);

if(windows_event_thread_handle == NULL)
if (windows_event_thread_handle == NULL)
{
log_error("CreateThread");
return LIBUSB_ERROR_OTHER;
Expand All @@ -45,27 +45,27 @@ int windows_start_event_monitor(void)

int windows_stop_event_monitor(void)
{
if(windows_event_hWnd == NULL)
if (windows_event_hwnd == NULL)
{
return LIBUSB_SUCCESS;
}

if(!SUCCEEDED(SendMessage(windows_event_hWnd, WM_CLOSE, 0, 0)))
if (!SUCCEEDED(SendMessage(windows_event_hwnd, WM_CLOSE, 0, 0)))
{
log_error("SendMessage");
}

int ret = LIBUSB_SUCCESS;

if(WaitForSingleObject(windows_event_thread_handle, INFINITE) != WAIT_OBJECT_0)
if (WaitForSingleObject(windows_event_thread_handle, INFINITE) != WAIT_OBJECT_0)
{
log_error("WaitForSingleObject");
ret = LIBUSB_ERROR_OTHER;
}

if(!CloseHandle(windows_event_thread_handle))
if (!CloseHandle(windows_event_thread_handle))
{
log_error("WaitForSingleObject");
log_error("CloseHandle");
ret = LIBUSB_ERROR_OTHER;
}

Expand All @@ -89,20 +89,22 @@ static int windows_get_device_list(struct libusb_context *ctx)
return ((struct windows_context_priv *)usbi_get_context_priv(ctx))->backend->get_device_list(ctx);
}

int windows_initial_scan_devices(struct libusb_context *ctx)
void windows_initial_scan_devices(struct libusb_context *ctx)
{
usbi_mutex_static_lock(&active_contexts_lock);

int ret = windows_get_device_list(ctx);


const int ret = windows_get_device_list(ctx);
if (ret != LIBUSB_SUCCESS)
{
usbi_err(ctx, "hotplug failed to retrieve initial list with error: %s", libusb_error_name(ret));
}
usbi_mutex_static_unlock(&active_contexts_lock);
return ret;
}

static void windows_refresh_device_list(struct libusb_context *ctx)
{
int ret = windows_get_device_list(ctx);
if(ret != LIBUSB_SUCCESS)
const int ret = windows_get_device_list(ctx);
if (ret != LIBUSB_SUCCESS)
{
usbi_err(ctx, "hotplug failed to retrieve current list with error: %s", libusb_error_name(ret));
return;
Expand All @@ -114,12 +116,12 @@ static void windows_refresh_device_list(struct libusb_context *ctx)
for_each_device_safe(ctx, dev, next_dev)
{
priv = usbi_get_device_priv(dev);
if(priv->hotplug_status != LEFT)
if (priv->hotplug_status != LEFT)
{
continue;
}

if(priv->initialized)
if (priv->initialized)
{
usbi_disconnect_device(dev);
}
Expand All @@ -133,7 +135,7 @@ static void windows_refresh_device_list(struct libusb_context *ctx)
{
priv = usbi_get_device_priv(dev);

if(priv->hotplug_status != ARRIVED)
if (priv->hotplug_status != ARRIVED)
{
continue;
}
Expand All @@ -151,6 +153,7 @@ static void windows_refresh_device_list_for_all_ctx(void)
{
windows_refresh_device_list(ctx);
}

usbi_mutex_static_unlock(&active_contexts_lock);
}

Expand Down Expand Up @@ -183,7 +186,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam)
return (DWORD)-1;
}

windows_event_hWnd = CreateWindow(
windows_event_hwnd = CreateWindow(
WND_CLASS_NAME,
TEXT(""),
0,
Expand All @@ -192,7 +195,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam)
GetModuleHandle(NULL),
NULL);

if (windows_event_hWnd == NULL)
if (windows_event_hwnd == NULL)
{
log_error("event thread: CreateWindow");
return (DWORD)-1;
Expand All @@ -201,20 +204,20 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam)
MSG msg;
BOOL ret_val;

while ((ret_val = GetMessage(&msg, windows_event_hWnd, 0, 0)) != 0)
while ((ret_val = GetMessage(&msg, windows_event_hwnd, 0, 0)) != 0)
{
if (ret_val == -1)
{
log_error("event thread: GetMessage");
break;
}

if(!SUCCEEDED(TranslateMessage(&msg)))
if (!SUCCEEDED(TranslateMessage(&msg)))
{
log_error("event thread: TranslateMessage");
}

if(!SUCCEEDED(DispatchMessage(&msg)))
if (!SUCCEEDED(DispatchMessage(&msg)))
{
log_error("event thread: DispatchMessage");
}
Expand All @@ -227,7 +230,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam)

static bool register_device_interface_to_window_handle(
IN GUID interface_class_guid,
IN HWND hWnd,
IN HWND hwnd,
OUT HDEVNOTIFY* device_notify_handle)
{
DEV_BROADCAST_DEVICEINTERFACE notificationFilter = { 0 };
Expand All @@ -236,9 +239,9 @@ static bool register_device_interface_to_window_handle(
notificationFilter.dbcc_classguid = interface_class_guid;

*device_notify_handle = RegisterDeviceNotification(
hWnd,
hwnd,
&notificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE// | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES// ?? for the ALL?
DEVICE_NOTIFY_WINDOW_HANDLE
);

if (*device_notify_handle == NULL)
Expand All @@ -251,21 +254,21 @@ static bool register_device_interface_to_window_handle(
}

static LRESULT CALLBACK windows_proc_callback(
HWND hWnd,
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
UNUSED(lParam);

static HDEVNOTIFY device_notify_handle;

switch (message)
{
case WM_CREATE:
if (!register_device_interface_to_window_handle(
GUID_DEVINTERFACE_USB_DEVICE,
hWnd,
hwnd,
&device_notify_handle))
{
return -1;
Expand All @@ -291,7 +294,7 @@ static LRESULT CALLBACK windows_proc_callback(
{
log_error("UnregisterDeviceNotification");
}
if(!DestroyWindow(hWnd))
if (!DestroyWindow(hwnd))
{
log_error("DestroyWindow");
}
Expand All @@ -302,6 +305,6 @@ static LRESULT CALLBACK windows_proc_callback(
return 0;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
2 changes: 1 addition & 1 deletion libusb/os/windows_hotplug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
int windows_start_event_monitor(void);
int windows_stop_event_monitor(void);

int windows_initial_scan_devices(struct libusb_context *ctx);
void windows_initial_scan_devices(struct libusb_context *ctx);

#endif
2 changes: 1 addition & 1 deletion libusb/os/windows_winusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ static int winusb_get_device_list(struct libusb_context *ctx)
break;
case GEN_PASS:
port_nr = 0;
if(priv->initialized) {
if (priv->initialized) {
libusb_unref_device(parent_dev);
r = LIBUSB_SUCCESS;
priv->hotplug_status = UNCHANGED;
Expand Down

0 comments on commit 26b9eca

Please sign in to comment.