Supersedes https://gitlab.winehq.org/wine/wine/-/merge_requests/7559 with couple of additional tests and fixes.
From: Vibhav Pant vibhavp@gmail.com
--- dlls/cfgmgr32/cfgmgr32.spec | 1 + dlls/cfgmgr32/main.c | 10 ++++++++++ include/cfgmgr32.h | 1 + 3 files changed, 12 insertions(+)
diff --git a/dlls/cfgmgr32/cfgmgr32.spec b/dlls/cfgmgr32/cfgmgr32.spec index a990b2538eb..d940bb63427 100644 --- a/dlls/cfgmgr32/cfgmgr32.spec +++ b/dlls/cfgmgr32/cfgmgr32.spec @@ -187,3 +187,4 @@ @ stub CM_Unregister_Device_InterfaceW @ stub CM_Unregister_Device_Interface_ExA @ stub CM_Unregister_Device_Interface_ExW +@ stdcall CM_Unregister_Notification(ptr) diff --git a/dlls/cfgmgr32/main.c b/dlls/cfgmgr32/main.c index b545d761f68..3206cd2ed7a 100644 --- a/dlls/cfgmgr32/main.c +++ b/dlls/cfgmgr32/main.c @@ -69,6 +69,16 @@ CONFIGRET WINAPI CM_Register_Notification( CM_NOTIFY_FILTER *filter, void *conte return CR_CALL_NOT_IMPLEMENTED; }
+/*********************************************************************** + * CM_Unregister_Notification (cfgmgr32.@) + */ +CONFIGRET WINAPI CM_Unregister_Notification( HCMNOTIFICATION notify ) +{ + FIXME( "(%p) stub!\n", notify ); + + return CR_CALL_NOT_IMPLEMENTED; +} + /*********************************************************************** * CM_Get_Device_Interface_PropertyW (cfgmgr32.@) */ diff --git a/include/cfgmgr32.h b/include/cfgmgr32.h index 7f1036f3510..269aec9873c 100644 --- a/include/cfgmgr32.h +++ b/include/cfgmgr32.h @@ -330,6 +330,7 @@ CMAPI DWORD WINAPI CM_MapCrToWin32Err(CONFIGRET,DWORD); CMAPI CONFIGRET WINAPI CM_Open_DevNode_Key(DEVINST dnDevInst, REGSAM access, ULONG ulHardwareProfile, REGDISPOSITION disposition, PHKEY phkDevice, ULONG ulFlags); CMAPI CONFIGRET WINAPI CM_Register_Notification(PCM_NOTIFY_FILTER,PVOID,PCM_NOTIFY_CALLBACK,PHCMNOTIFICATION); +CMAPI CONFIGRET WINAPI CM_Unregister_Notification(HCMNOTIFICATION); CMAPI CONFIGRET WINAPI CM_Request_Device_EjectA(DEVINST dev, PPNP_VETO_TYPE type, LPSTR name, ULONG length, ULONG flags); CMAPI CONFIGRET WINAPI CM_Request_Device_EjectW(DEVINST dev, PPNP_VETO_TYPE type, LPWSTR name, ULONG length, ULONG flags); #define CM_Request_Device_Eject WINELIB_NAME_AW(CM_Get_Device_ID_List_Ex)
From: Vibhav Pant vibhavp@gmail.com
--- dlls/cfgmgr32/tests/cfgmgr32.c | 67 ++++++++++++++++++++++++++++++++++ include/cfgmgr32.h | 1 + 2 files changed, 68 insertions(+)
diff --git a/dlls/cfgmgr32/tests/cfgmgr32.c b/dlls/cfgmgr32/tests/cfgmgr32.c index 1e949093689..a964b30b2d7 100644 --- a/dlls/cfgmgr32/tests/cfgmgr32.c +++ b/dlls/cfgmgr32/tests/cfgmgr32.c @@ -282,8 +282,75 @@ static void test_CM_Get_Device_ID_List(void) free(buf); }
+DWORD WINAPI notify_callback( HCMNOTIFICATION notify, void *ctx, CM_NOTIFY_ACTION action, + CM_NOTIFY_EVENT_DATA *data, DWORD size ) +{ + return ERROR_SUCCESS; +} + +static void test_CM_Register_Notification( void ) +{ + struct + { + CM_NOTIFY_FILTER filter; + CONFIGRET ret; + } test_cases[] = { + { + { 0, CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES, CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, 0 }, + CR_INVALID_DATA + }, + { + { sizeof( CM_NOTIFY_FILTER ) + 1, 0, CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, 0, + .u.DeviceInterface = { GUID_DEVINTERFACE_DISPLAY_ADAPTER } }, + CR_INVALID_DATA + }, + { + { sizeof( CM_NOTIFY_FILTER ), CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES, + CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, 0, .u.DeviceInterface = { GUID_DEVINTERFACE_DISPLAY_ADAPTER } }, + CR_INVALID_DATA + }, + { + { sizeof( CM_NOTIFY_FILTER ), CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES, + CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, 0 }, + CR_SUCCESS + }, + { + { sizeof( CM_NOTIFY_FILTER ), 0, CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, 0, + .u.DeviceInterface = { GUID_DEVINTERFACE_DISPLAY_ADAPTER } }, + CR_SUCCESS + } + }; + DWORD i; + HCMNOTIFICATION notify = NULL; + CONFIGRET ret; + + ret = CM_Register_Notification( NULL, NULL, NULL, NULL ); + todo_wine ok( ret == CR_FAILURE, "Expected 0x13, got %#lx.\n", ret ); + + ret = CM_Register_Notification( NULL, NULL, NULL, ¬ify ); + todo_wine ok( ret == CR_INVALID_DATA, "Expected 0x1f, got %#lx.\n", ret ); + ok( !notify, "Expected handle to be NULL, got %p\n", notify ); + + for (i = 0; i < ARRAY_SIZE( test_cases ); i++) + { + notify = NULL; + winetest_push_context( "test_cases %lu", i ); + ret = CM_Register_Notification( &test_cases[i].filter, NULL, notify_callback, ¬ify ); + todo_wine ok( test_cases[i].ret == ret, "Expected %#lx, got %#lx\n", test_cases[i].ret, ret ); + if (test_cases[i].ret) + ok( !notify, "Expected handle to be NULL, got %p\n", notify ); + if (notify) + { + ret = CM_Unregister_Notification( notify ); + ok( !ret, "Expected 0, got %#lx\n", ret ); + } + winetest_pop_context(); + } +} + START_TEST(cfgmgr32) { test_CM_MapCrToWin32Err(); test_CM_Get_Device_ID_List(); + test_CM_Register_Notification(); } diff --git a/include/cfgmgr32.h b/include/cfgmgr32.h index 269aec9873c..3e814757e2b 100644 --- a/include/cfgmgr32.h +++ b/include/cfgmgr32.h @@ -204,6 +204,7 @@ typedef CHAR *DEVNODEID_A, *DEVINSTID_A; typedef WCHAR *DEVNODEID_W, *DEVINSTID_W; typedef ULONG REGDISPOSITION;
+#define CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES 0x0001 typedef enum _CM_NOTIFY_FILTER_TYPE { CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE,
From: Vibhav Pant vibhavp@gmail.com
--- dlls/dinput/tests/Makefile.in | 2 +- dlls/dinput/tests/hotplug.c | 203 +++++++++++++++++++++++++++++++++- 2 files changed, 203 insertions(+), 2 deletions(-)
diff --git a/dlls/dinput/tests/Makefile.in b/dlls/dinput/tests/Makefile.in index 608b817e0f4..c40300e619b 100644 --- a/dlls/dinput/tests/Makefile.in +++ b/dlls/dinput/tests/Makefile.in @@ -1,5 +1,5 @@ TESTDLL = dinput.dll -IMPORTS = dinput dinput8 ole32 version user32 advapi32 hid uuid crypt32 newdev setupapi wintrust winmm +IMPORTS = dinput dinput8 ole32 version user32 advapi32 hid uuid crypt32 newdev setupapi wintrust winmm cfgmgr32
driver_bus_IMPORTS = winecrt0 ntoskrnl hal driver_bus_EXTRADLLFLAGS = -nodefaultlibs -nostartfiles -Wl,--subsystem,native diff --git a/dlls/dinput/tests/hotplug.c b/dlls/dinput/tests/hotplug.c index f7199d2b17e..0c068d04b04 100644 --- a/dlls/dinput/tests/hotplug.c +++ b/dlls/dinput/tests/hotplug.c @@ -34,6 +34,7 @@ #include "dbt.h" #include "unknwn.h" #include "winstring.h" +#include "cfgmgr32.h"
#include "wine/hid.h"
@@ -279,6 +280,7 @@ static const union device_change_event static const union device_change_event *device_change_expect_event; static HANDLE device_change_expect_handle; static HDEVNOTIFY handle_devnotify; +static HCMNOTIFICATION handle_cmnotify;
static LRESULT CALLBACK devnotify_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) { @@ -365,6 +367,110 @@ static LRESULT CALLBACK devnotify_wndproc( HWND hwnd, UINT msg, WPARAM wparam, L return DefWindowProcW( hwnd, msg, wparam, lparam ); }
+struct cm_notify_callback_data +{ + HCMNOTIFICATION hnotify; + DWORD device_change_expect; + DWORD device_change_expect_custom; + BOOL device_change_all; + + DWORD device_change_count; + HANDLE device_change_sem; + + BOOL received_deviceremovalcomplete; +}; + +static CALLBACK DWORD cm_notify_callback( HCMNOTIFICATION hnotify, void *ctx, CM_NOTIFY_ACTION action, + CM_NOTIFY_EVENT_DATA *data, DWORD size ) +{ + struct cm_notify_callback_data *cb_data = ctx; + + switch (action) + { + case CM_NOTIFY_ACTION_DEVICECUSTOMEVENT: + { + const union device_change_event *event; + SIZE_T idx; + + ok( hnotify == handle_cmnotify, "%p != %p\n", hnotify, handle_cmnotify ); + ok( !!cb_data->device_change_expect_custom, "unexpected event\n" ); + idx = (ARRAY_SIZE(device_change_events) + 1) - cb_data->device_change_expect_custom; + event = &device_change_events[idx]; + + winetest_push_context( "%Id", idx ); + ok( size == offsetof( CM_NOTIFY_EVENT_DATA, u.DeviceHandle.Data[data->u.DeviceHandle.DataSize] ), + "got %#lx\n", size ); + ok( IsEqualGUID( &data->u.DeviceHandle.EventGuid, &event->Event ), "got DeviceHandle.EventGuid %s\n", + debugstr_guid( &data->u.DeviceHandle.EventGuid ) ); + ok( data->u.DeviceHandle.NameOffset == event->NameBufferOffset, "got DeviceHandle.NameOffset %ld\n", + data->u.DeviceHandle.NameOffset ); + ok( data->u.DeviceHandle.DataSize == event->Size - offsetof( TARGET_DEVICE_CUSTOM_NOTIFICATION, CustomDataBuffer[0] ), + "got DeviceHandle.DataSize %ld\n", data->u.DeviceHandle.DataSize ); + ok( !memcmp( data->u.DeviceHandle.Data, event->CustomDataBuffer, data->u.DeviceHandle.DataSize ), + "Unexpected DeviceHandle.Data\n" ); + winetest_pop_context(); + + cb_data->device_change_expect_custom--; + cb_data->device_change_count++; + ReleaseSemaphore( cb_data->device_change_sem, 1, NULL ); + break; + } + case CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE: + ok( size == sizeof(*data), "got %#lx\n", size ); + ok( cb_data->device_change_expect_custom, "Unexpected CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE\n" ); + cb_data->received_deviceremovalcomplete = TRUE; + cb_data->device_change_count++; + ReleaseSemaphore( cb_data->device_change_sem, 1, NULL ); + break; + case CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL: + case CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL: + { + const WCHAR *expect_prefix, *name = data->u.DeviceInterface.SymbolicLink, *upper_end, *name_end; + GUID expect_guid; + + ok( hnotify == cb_data->hnotify, "%p != %p\n", hnotify, cb_data->hnotify ); + ok( size == offsetof( CM_NOTIFY_EVENT_DATA, u.DeviceInterface.SymbolicLink[wcslen( name ) + 1] ), "got %#lx\n", size ); + if (cb_data->device_change_all && (!cb_data->device_change_count || cb_data->device_change_count == 3)) + { + expect_guid = control_class; + expect_prefix = L"\\?\WINETEST#"; + } + else + { + expect_guid = GUID_DEVINTERFACE_HID; + expect_prefix = L"\\?\HID#"; + } + + winetest_push_context( "%lu", cb_data->device_change_count ); + ok( IsEqualGUID( &data->u.DeviceInterface.ClassGuid, &expect_guid ), "got u.DeviceInterface.ClassGuid %s\n", + debugstr_guid( &data->u.DeviceInterface.ClassGuid ) ); + ok( !wcsncmp( name, expect_prefix, wcslen( expect_prefix ) ), "got u.DeviceInterface.SymbolicLink %s\n", + debugstr_w( name ) ); + upper_end = wcschr( name + wcslen( expect_prefix ), '#' ); + name_end = name + wcslen( name ) + 1; + ok( !!upper_end, "got u.DeviceInterface.SymbolicLink %s\n", debugstr_w( name ) ); + ok( all_upper( name, upper_end ), "got u.DeviceInterface.SymbolicLink %s\n", debugstr_w( name ) ); + ok( all_lower( upper_end, name_end ), "got u.DeviceInterface.SymbolicLink %s\n", debugstr_w( name ) ); + + if (cb_data->device_change_count++ >= cb_data->device_change_expect / 2) + ok( action == CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL, "got action %d\n", action ); + else if (cb_data->device_change_expect_custom) + ok( action == CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL, "got action %d\n", action ); + else + ok( action == CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL, "got action %d\n", action ); + + winetest_pop_context(); + ReleaseSemaphore( cb_data->device_change_sem, 1, NULL ); + break; + } + default: + ok( 0, "Unexpected CM_NOTIFY_ACTION %d\n", action ); + break; + } + + return ERROR_SUCCESS; +} + static void test_RegisterDeviceNotification(void) { DEV_BROADCAST_DEVICEINTERFACE_A iface_filter_a = @@ -385,11 +491,32 @@ static void test_RegisterDeviceNotification(void) .lpszClassName = L"devnotify", .lpfnWndProc = devnotify_wndproc, }; + CM_NOTIFY_FILTER cm_iface_filter = + {.cbSize = sizeof(CM_NOTIFY_FILTER), + .Flags = 0, + .FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, + .Reserved = 0, + .u = {.DeviceInterface = {.ClassGuid = GUID_DEVINTERFACE_HID}}}; + CM_NOTIFY_FILTER cm_all_ifaces_filter = + { + .cbSize = sizeof(CM_NOTIFY_FILTER), + .Flags = CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES, + .FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, + .Reserved = 0, + }; + CM_NOTIFY_FILTER cm_handle_filter = + { + .cbSize = sizeof(CM_NOTIFY_FILTER), + .Flags = 0, + .FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE, + .Reserved = 0, + }; char buffer[1024] = {0}; DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)buffer; + struct cm_notify_callback_data cm_ctx = {0}; HANDLE hwnd, thread, stop_event; HDEVNOTIFY devnotify; - DWORD ret; + DWORD i, ret; MSG msg;
RegisterClassExW( &class ); @@ -461,6 +588,16 @@ static void test_RegisterDeviceNotification(void) ok( !!devnotify, "RegisterDeviceNotificationA failed, error %lu\n", GetLastError() ); while (PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
+ cm_ctx.device_change_sem = CreateSemaphoreW( NULL, 0, 2 + ARRAY_SIZE(device_change_events), NULL ); + ok( !!cm_ctx.device_change_sem, "CreateSemaphoreW failed, error %lu\n", GetLastError() ); + + cm_ctx.device_change_all = FALSE; + cm_ctx.device_change_expect = 2; + cm_ctx.device_change_count = 0; + + ret = CM_Register_Notification( &cm_iface_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); + todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + device_change_count = 0; device_change_expect = 2; device_change_hwnd = hwnd; @@ -483,6 +620,13 @@ static void test_RegisterDeviceNotification(void) } if (device_change_count == device_change_expect / 2) SetEvent( stop_event ); } + for (i = 0; i < cm_ctx.device_change_expect; i++) + { + ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); + todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + } + todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -490,6 +634,7 @@ static void test_RegisterDeviceNotification(void) CloseHandle( stop_event );
UnregisterDeviceNotification( devnotify ); + CM_Unregister_Notification( cm_ctx.hnotify );
memcpy( buffer, &iface_filter_a, sizeof(iface_filter_a) ); strcpy( ((DEV_BROADCAST_DEVICEINTERFACE_A *)buffer)->dbcc_name, "device name" ); @@ -498,6 +643,13 @@ static void test_RegisterDeviceNotification(void) ok( !!devnotify, "RegisterDeviceNotificationA failed, error %lu\n", GetLastError() ); while (PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
+ cm_ctx.device_change_all = FALSE; + cm_ctx.device_change_expect = 2; + cm_ctx.device_change_count = 0; + + ret = CM_Register_Notification( &cm_iface_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); + todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + device_change_count = 0; device_change_expect = 2; device_change_hwnd = hwnd; @@ -520,6 +672,13 @@ static void test_RegisterDeviceNotification(void) } if (device_change_count == device_change_expect / 2) SetEvent( stop_event ); } + for (i = 0; i < cm_ctx.device_change_expect; i++) + { + ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); + todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + } + todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -527,11 +686,19 @@ static void test_RegisterDeviceNotification(void) CloseHandle( stop_event );
UnregisterDeviceNotification( devnotify ); + CM_Unregister_Notification( cm_ctx.hnotify );
devnotify = RegisterDeviceNotificationA( hwnd, &iface_filter_a, DEVICE_NOTIFY_ALL_INTERFACE_CLASSES ); ok( !!devnotify, "RegisterDeviceNotificationA failed, error %lu\n", GetLastError() ); while (PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
+ cm_ctx.device_change_all = TRUE; + cm_ctx.device_change_expect = 4; + cm_ctx.device_change_count = 0; + ret = CM_Register_Notification( &cm_all_ifaces_filter, &cm_ctx, cm_notify_callback, + &cm_ctx.hnotify ); + todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + device_change_count = 0; device_change_expect = 4; device_change_hwnd = hwnd; @@ -554,6 +721,13 @@ static void test_RegisterDeviceNotification(void) } if (device_change_count == device_change_expect / 2) SetEvent( stop_event ); } + for (i = 0; i < cm_ctx.device_change_expect; i++) + { + ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); + todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + } + todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -561,11 +735,19 @@ static void test_RegisterDeviceNotification(void) CloseHandle( stop_event );
UnregisterDeviceNotification( devnotify ); + CM_Unregister_Notification( cm_ctx.hnotify );
devnotify = RegisterDeviceNotificationA( hwnd, &iface_filter_a, DEVICE_NOTIFY_WINDOW_HANDLE ); ok( !!devnotify, "RegisterDeviceNotificationA failed, error %lu\n", GetLastError() ); while (PeekMessageW( &msg, hwnd, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
+ cm_ctx.device_change_all = FALSE; + cm_ctx.device_change_expect = 2 + ARRAY_SIZE(device_change_events); + cm_ctx.device_change_count = 0; + cm_iface_filter.Flags = 0; + ret = CM_Register_Notification( &cm_iface_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); + todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + device_change_count = 0; device_change_expect = 2; device_change_hwnd = hwnd; @@ -592,6 +774,9 @@ static void test_RegisterDeviceNotification(void) HANDLE file; UINT i;
+ ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); + todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + swprintf( device_path, MAX_PATH, L"\\?\hid#vid_%04x&pid_%04x", LOWORD(EXPECT_VIDPID), HIWORD(EXPECT_VIDPID) ); ret = find_hid_device_path( device_path ); ok( ret, "Failed to find HID device matching %s\n", debugstr_w( device_path ) ); @@ -604,8 +789,13 @@ static void test_RegisterDeviceNotification(void) handle_devnotify = RegisterDeviceNotificationA( hwnd, &handle_filter_a, DEVICE_NOTIFY_WINDOW_HANDLE ); ok( !!handle_devnotify, "RegisterDeviceNotificationA failed, error %lu\n", GetLastError() );
+ cm_handle_filter.u.DeviceHandle.hTarget = file; + ret = CM_Register_Notification( &cm_handle_filter, &cm_ctx, cm_notify_callback, &handle_cmnotify ); + todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + device_change_expect_handle = file; device_change_expect_event = device_change_events; + cm_ctx.device_change_expect_custom = ARRAY_SIZE(device_change_events) + 1; for (i = 0; i < ARRAY_SIZE(device_change_events) - 1; i++) { ret = sync_ioctl( file, IOCTL_WINETEST_DEVICE_CHANGE, (BYTE *)&device_change_events[i].notif, @@ -619,6 +809,14 @@ static void test_RegisterDeviceNotification(void) } if (device_change_count == 1 && device_change_expect_custom == 1) SetEvent( stop_event ); } + for (i = 1; i < cm_ctx.device_change_expect; i++) + { + ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); + todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + } + todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect ); + todo_wine ok( cm_ctx.received_deviceremovalcomplete, "%d != 0\n", cm_ctx.received_deviceremovalcomplete );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -626,9 +824,12 @@ static void test_RegisterDeviceNotification(void) CloseHandle( stop_event );
if (handle_devnotify) UnregisterDeviceNotification( handle_devnotify ); + if (handle_cmnotify) CM_Unregister_Notification( handle_cmnotify ); UnregisterDeviceNotification( devnotify ); + CM_Unregister_Notification( cm_ctx.hnotify ); device_change_expect_event = NULL; handle_devnotify = 0; + CloseHandle( cm_ctx.device_change_sem );
DestroyWindow( hwnd ); UnregisterClassW( class.lpszClassName, class.hInstance );
From: Vibhav Pant vibhavp@gmail.com
--- dlls/cfgmgr32/Makefile.in | 2 +- dlls/cfgmgr32/main.c | 172 ++++++++++++++++++++++++++++++++- dlls/cfgmgr32/tests/cfgmgr32.c | 6 +- dlls/dinput/tests/hotplug.c | 33 ++++--- 4 files changed, 189 insertions(+), 24 deletions(-)
diff --git a/dlls/cfgmgr32/Makefile.in b/dlls/cfgmgr32/Makefile.in index 1ff06a63b5f..e8d9fecdcdd 100644 --- a/dlls/cfgmgr32/Makefile.in +++ b/dlls/cfgmgr32/Makefile.in @@ -1,6 +1,6 @@ MODULE = cfgmgr32.dll IMPORTLIB = cfgmgr32 -IMPORTS = setupapi +IMPORTS = setupapi sechost
SOURCES = \ main.c diff --git a/dlls/cfgmgr32/main.c b/dlls/cfgmgr32/main.c index 3206cd2ed7a..fbe39365975 100644 --- a/dlls/cfgmgr32/main.c +++ b/dlls/cfgmgr32/main.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2023 Mohamad Al-Jaf + * Copyright (C) 2025 Vibhav Pant * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,6 +20,9 @@ #include "wine/debug.h" #include "winreg.h" #include "cfgmgr32.h" +#include "winuser.h" +#include "dbt.h" +#include "wine/plugplay.h"
WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
@@ -58,15 +62,168 @@ DWORD WINAPI CM_MapCrToWin32Err( CONFIGRET code, DWORD default_error ) return default_error; }
+struct cm_notify_context +{ + HDEVNOTIFY notify; + void *user_data; + PCM_NOTIFY_CALLBACK callback; +}; + +CALLBACK DWORD devnotify_callback( HANDLE handle, DWORD flags, DEV_BROADCAST_HDR *header ) +{ + struct cm_notify_context *ctx = handle; + CM_NOTIFY_EVENT_DATA *event_data; + CM_NOTIFY_ACTION action; + DWORD size, ret; + + TRACE( "(%p, %#lx, %p)\n", handle, flags, header ); + + switch (flags) + { + case DBT_DEVICEARRIVAL: + action = CM_NOTIFY_ACTION_DEVICEINTERFACEARRIVAL; + break; + case DBT_DEVICEREMOVECOMPLETE: + FIXME( "CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE not implemented\n" ); + action = CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL; + break; + case DBT_CUSTOMEVENT: + action = CM_NOTIFY_ACTION_DEVICECUSTOMEVENT; + break; + default: + FIXME( "Unexpected flags value: %#lx\n", flags ); + return 0; + } + + switch (header->dbch_devicetype) + { + case DBT_DEVTYP_DEVICEINTERFACE: + { + const DEV_BROADCAST_DEVICEINTERFACE_W *iface = (DEV_BROADCAST_DEVICEINTERFACE_W *)header; + UINT data_size = wcslen( iface->dbcc_name ) + 1; + + size = offsetof( CM_NOTIFY_EVENT_DATA, u.DeviceInterface.SymbolicLink[data_size] ); + if (!(event_data = calloc( 1, size ))) return 0; + + event_data->FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE; + event_data->u.DeviceInterface.ClassGuid = iface->dbcc_classguid; + memcpy( event_data->u.DeviceInterface.SymbolicLink, iface->dbcc_name, data_size * sizeof(WCHAR) ); + break; + } + case DBT_DEVTYP_HANDLE: + { + const DEV_BROADCAST_HANDLE *handle = (DEV_BROADCAST_HANDLE *)header; + UINT data_size = handle->dbch_size - 2 * sizeof(WCHAR) - offsetof( DEV_BROADCAST_HANDLE, dbch_data ); + + size = offsetof( CM_NOTIFY_EVENT_DATA, u.DeviceHandle.Data[data_size] ); + if (!(event_data = calloc( 1, size ))) return 0; + + event_data->FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE; + event_data->u.DeviceHandle.EventGuid = handle->dbch_eventguid; + event_data->u.DeviceHandle.NameOffset = handle->dbch_nameoffset; + event_data->u.DeviceHandle.DataSize = data_size; + memcpy( event_data->u.DeviceHandle.Data, handle->dbch_data, data_size ); + break; + } + default: + FIXME( "Unexpected devicetype value: %#lx\n", header->dbch_devicetype ); + return 0; + } + + ret = ctx->callback( ctx, ctx->user_data, action, event_data, size ); + free( event_data ); + return ret; +} + +static const char *debugstr_CM_NOTIFY_FILTER( const CM_NOTIFY_FILTER *filter ) +{ + switch (filter->FilterType) + { + case CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE: + return wine_dbg_sprintf( "{%#lx %lx CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE %lu {{%s}}}", filter->cbSize, + filter->Flags, filter->Reserved, + debugstr_guid( &filter->u.DeviceInterface.ClassGuid ) ); + case CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE: + return wine_dbg_sprintf( "{%#lx %lx CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE %lu {{%p}}}", filter->cbSize, + filter->Flags, filter->Reserved, filter->u.DeviceHandle.hTarget ); + case CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE: + return wine_dbg_sprintf( "{%#lx %lx CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE %lu {{%s}}}", filter->cbSize, + filter->Flags, filter->Reserved, debugstr_w( filter->u.DeviceInstance.InstanceId ) ); + default: + return wine_dbg_sprintf( "{%#lx %lx (unknown FilterType %d) %lu}", filter->cbSize, filter->Flags, + filter->FilterType, filter->Reserved ); + } +} + +static CONFIGRET create_notify_context( const CM_NOTIFY_FILTER *filter, HCMNOTIFICATION *notify_handle, + PCM_NOTIFY_CALLBACK callback, void *user_data ) +{ + union + { + DEV_BROADCAST_HDR header; + DEV_BROADCAST_DEVICEINTERFACE_W iface; + DEV_BROADCAST_HANDLE handle; + } notify_filter = {0}; + struct cm_notify_context *ctx; + static const GUID GUID_NULL; + + switch (filter->FilterType) + { + case CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE: + notify_filter.iface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; + if (filter->Flags & CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES) + { + if (!IsEqualGUID( &filter->u.DeviceInterface.ClassGuid, &GUID_NULL )) return CR_INVALID_DATA; + notify_filter.iface.dbcc_size = offsetof( DEV_BROADCAST_DEVICEINTERFACE_W, dbcc_classguid ); + } + else + { + notify_filter.iface.dbcc_size = offsetof( DEV_BROADCAST_DEVICEINTERFACE_W, dbcc_name ); + notify_filter.iface.dbcc_classguid = filter->u.DeviceInterface.ClassGuid; + } + break; + case CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE: + notify_filter.handle.dbch_devicetype = DBT_DEVTYP_HANDLE; + notify_filter.handle.dbch_size = sizeof(notify_filter.handle); + notify_filter.handle.dbch_handle = filter->u.DeviceHandle.hTarget; + break; + case CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE: + FIXME( "CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE is not supported!\n" ); + return CR_CALL_NOT_IMPLEMENTED; + default: + return CR_INVALID_DATA; + } + + if (!(ctx = calloc( 1, sizeof(*ctx) ))) return CR_OUT_OF_MEMORY; + + ctx->user_data = user_data; + ctx->callback = callback; + if (!(ctx->notify = I_ScRegisterDeviceNotification( ctx, ¬ify_filter.header, devnotify_callback ))) + { + free( ctx ); + switch (GetLastError()) + { + case ERROR_NOT_ENOUGH_MEMORY: return CR_OUT_OF_MEMORY; + case ERROR_INVALID_PARAMETER: return CR_INVALID_DATA; + default: return CR_FAILURE; + } + } + *notify_handle = ctx; + return CR_SUCCESS; +} + /*********************************************************************** * CM_Register_Notification (cfgmgr32.@) */ CONFIGRET WINAPI CM_Register_Notification( CM_NOTIFY_FILTER *filter, void *context, PCM_NOTIFY_CALLBACK callback, HCMNOTIFICATION *notify_context ) { - FIXME("%p %p %p %p stub!\n", filter, context, callback, notify_context); + TRACE( "(%s %p %p %p)\n", debugstr_CM_NOTIFY_FILTER( filter ), context, callback, notify_context );
- return CR_CALL_NOT_IMPLEMENTED; + if (!notify_context) return CR_FAILURE; + if (!filter || !callback || filter->cbSize != sizeof(*filter)) return CR_INVALID_DATA; + + return create_notify_context( filter, notify_context, callback, context ); }
/*********************************************************************** @@ -74,9 +231,16 @@ CONFIGRET WINAPI CM_Register_Notification( CM_NOTIFY_FILTER *filter, void *conte */ CONFIGRET WINAPI CM_Unregister_Notification( HCMNOTIFICATION notify ) { - FIXME( "(%p) stub!\n", notify ); + struct cm_notify_context *ctx = notify;
- return CR_CALL_NOT_IMPLEMENTED; + TRACE( "(%p)\n", notify ); + + if (!notify) return CR_INVALID_DATA; + + I_ScUnregisterDeviceNotification( ctx->notify ); + free( ctx ); + + return CR_SUCCESS; }
/*********************************************************************** diff --git a/dlls/cfgmgr32/tests/cfgmgr32.c b/dlls/cfgmgr32/tests/cfgmgr32.c index a964b30b2d7..f45c0641ec1 100644 --- a/dlls/cfgmgr32/tests/cfgmgr32.c +++ b/dlls/cfgmgr32/tests/cfgmgr32.c @@ -325,10 +325,10 @@ static void test_CM_Register_Notification( void ) CONFIGRET ret;
ret = CM_Register_Notification( NULL, NULL, NULL, NULL ); - todo_wine ok( ret == CR_FAILURE, "Expected 0x13, got %#lx.\n", ret ); + ok( ret == CR_FAILURE, "Expected 0x13, got %#lx.\n", ret );
ret = CM_Register_Notification( NULL, NULL, NULL, ¬ify ); - todo_wine ok( ret == CR_INVALID_DATA, "Expected 0x1f, got %#lx.\n", ret ); + ok( ret == CR_INVALID_DATA, "Expected 0x1f, got %#lx.\n", ret ); ok( !notify, "Expected handle to be NULL, got %p\n", notify );
for (i = 0; i < ARRAY_SIZE( test_cases ); i++) @@ -336,7 +336,7 @@ static void test_CM_Register_Notification( void ) notify = NULL; winetest_push_context( "test_cases %lu", i ); ret = CM_Register_Notification( &test_cases[i].filter, NULL, notify_callback, ¬ify ); - todo_wine ok( test_cases[i].ret == ret, "Expected %#lx, got %#lx\n", test_cases[i].ret, ret ); + ok( test_cases[i].ret == ret, "Expected %#lx, got %#lx\n", test_cases[i].ret, ret ); if (test_cases[i].ret) ok( !notify, "Expected handle to be NULL, got %p\n", notify ); if (notify) diff --git a/dlls/dinput/tests/hotplug.c b/dlls/dinput/tests/hotplug.c index 0c068d04b04..73a8bc8057c 100644 --- a/dlls/dinput/tests/hotplug.c +++ b/dlls/dinput/tests/hotplug.c @@ -596,7 +596,7 @@ static void test_RegisterDeviceNotification(void) cm_ctx.device_change_count = 0;
ret = CM_Register_Notification( &cm_iface_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); - todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + ok( !ret, "CM_Register_Notification failed, error %lu\n", ret );
device_change_count = 0; device_change_expect = 2; @@ -623,10 +623,10 @@ static void test_RegisterDeviceNotification(void) for (i = 0; i < cm_ctx.device_change_expect; i++) { ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); - todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); } - todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", - cm_ctx.device_change_count, cm_ctx.device_change_expect ); + ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -648,7 +648,7 @@ static void test_RegisterDeviceNotification(void) cm_ctx.device_change_count = 0;
ret = CM_Register_Notification( &cm_iface_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); - todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + ok( !ret, "CM_Register_Notification failed, error %lu\n", ret );
device_change_count = 0; device_change_expect = 2; @@ -675,10 +675,10 @@ static void test_RegisterDeviceNotification(void) for (i = 0; i < cm_ctx.device_change_expect; i++) { ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); - todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); } - todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", - cm_ctx.device_change_count, cm_ctx.device_change_expect ); + ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -697,7 +697,7 @@ static void test_RegisterDeviceNotification(void) cm_ctx.device_change_count = 0; ret = CM_Register_Notification( &cm_all_ifaces_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); - todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + ok( !ret, "CM_Register_Notification failed, error %lu\n", ret );
device_change_count = 0; device_change_expect = 4; @@ -724,10 +724,10 @@ static void test_RegisterDeviceNotification(void) for (i = 0; i < cm_ctx.device_change_expect; i++) { ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); - todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); } - todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", - cm_ctx.device_change_count, cm_ctx.device_change_expect ); + ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", + cm_ctx.device_change_count, cm_ctx.device_change_expect );
ret = WaitForSingleObject( thread, 5000 ); ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); @@ -746,7 +746,7 @@ static void test_RegisterDeviceNotification(void) cm_ctx.device_change_count = 0; cm_iface_filter.Flags = 0; ret = CM_Register_Notification( &cm_iface_filter, &cm_ctx, cm_notify_callback, &cm_ctx.hnotify ); - todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + ok( !ret, "CM_Register_Notification failed, error %lu\n", ret );
device_change_count = 0; device_change_expect = 2; @@ -775,7 +775,7 @@ static void test_RegisterDeviceNotification(void) UINT i;
ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); - todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + ok( !ret, "WaitForSingleObject returned %#lx\n", ret );
swprintf( device_path, MAX_PATH, L"\\?\hid#vid_%04x&pid_%04x", LOWORD(EXPECT_VIDPID), HIWORD(EXPECT_VIDPID) ); ret = find_hid_device_path( device_path ); @@ -791,7 +791,7 @@ static void test_RegisterDeviceNotification(void)
cm_handle_filter.u.DeviceHandle.hTarget = file; ret = CM_Register_Notification( &cm_handle_filter, &cm_ctx, cm_notify_callback, &handle_cmnotify ); - todo_wine ok( !ret, "CM_Register_Notification failed, error %lu\n", ret ); + ok( !ret, "CM_Register_Notification failed, error %lu\n", ret );
device_change_expect_handle = file; device_change_expect_event = device_change_events; @@ -812,7 +812,8 @@ static void test_RegisterDeviceNotification(void) for (i = 1; i < cm_ctx.device_change_expect; i++) { ret = WaitForSingleObject( cm_ctx.device_change_sem, 100 ); - todo_wine ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); + todo_wine_if(i == cm_ctx.device_change_expect - 1) + ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); } todo_wine ok( cm_ctx.device_change_count == cm_ctx.device_change_expect, "%lu != %lu\n", cm_ctx.device_change_count, cm_ctx.device_change_expect );
From: Vibhav Pant vibhavp@gmail.com
--- dlls/user32/input.c | 3 --- 1 file changed, 3 deletions(-)
diff --git a/dlls/user32/input.c b/dlls/user32/input.c index 9e87d6664f5..d54d6abc0ab 100644 --- a/dlls/user32/input.c +++ b/dlls/user32/input.c @@ -577,10 +577,7 @@ HDEVNOTIFY WINAPI RegisterDeviceNotificationW( HANDLE handle, void *filter, DWOR return I_ScRegisterDeviceNotification( handle, (DEV_BROADCAST_HDR *)&iface, callback ); } if (header->dbch_devicetype == DBT_DEVTYP_HANDLE) - { - FIXME( "DBT_DEVTYP_HANDLE not implemented\n" ); return I_ScRegisterDeviceNotification( handle, header, callback ); - }
FIXME( "type %#lx not implemented\n", header->dbch_devicetype ); SetLastError( ERROR_INVALID_DATA );
This merge request was approved by Rémi Bernon.
This merge request was approved by Vibhav Pant.
@vibhavp FWIW I added a FIXME in DBT_DEVICEREMOVECOMPLETE, for the CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE case because it looks like we're not doing exactly the right event sequence, and CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE is missing in the last test?
I'm not sure if both CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL and CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE should be generated from DBT_DEVICEREMOVECOMPLETE or if there's separate notifications, maybe worth investigating.
On Wed Mar 19 12:58:50 2025 +0000, Rémi Bernon wrote:
@vibhavp FWIW I added a FIXME in DBT_DEVICEREMOVECOMPLETE, for the CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE case because it looks like we're not doing exactly the right event sequence, and CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE is missing in the last test? I'm not sure if both CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL and CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE should be generated from DBT_DEVICEREMOVECOMPLETE or if there's separate notifications, maybe worth investigating.
Thank you!
I'm not sure if both CM_NOTIFY_ACTION_DEVICEINTERFACEREMOVAL and CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE should be generated from DBT_DEVICEREMOVECOMPLETE or if there's separate notifications, maybe worth investigating.
`CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE` is only generated for `CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE` filters, so we would have to first implement broadcasting `DBT_DEVICEREMOVECOMPLETE` events for device objects (I believe there's a TODO in the `hotplug.c` tests for this as well?).
Microsoft documents [in this example ](https://learn.microsoft.com/en-us/windows/win32/devio/processing-a-request-t... `DBT_DEVICEREMOVECOMPLETE` events are sent for `DBT_DEVTYP_HANDLE` event as well, so once we do support them, we could add a simple check to `devnotify_callback` to set the action to `CM_NOTIFY_ACTION_DEVICEREMOVECOMPLETE` if when `flags == DBT_DEVICEREMOVECOMPLETE && dbch_devicetype == DBT_DEVTYP_HANDLE`.
A quick look at the code suggests that we could possibly do this in `IoDeleteDevice`?