Skip to content

Commit

Permalink
[HOTPLUG] Move device enumeration code to enum.c
Browse files Browse the repository at this point in the history
  • Loading branch information
TAN-Gaming committed Oct 30, 2023
1 parent 6acf1cb commit ae92a26
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 325 deletions.
124 changes: 11 additions & 113 deletions dll/cpl/hotplug/eject.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/*
* PROJECT: ReactOS Safely Remove Hardware Applet
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Confirm Removal Dialog
* PURPOSE: Device Removal Support
* COPYRIGHT: Copyright 2023 Thamatip Chitpong <[email protected]>
*/

#include "hotplug.h"

#include <initguid.h>
#include <devguid.h>

static
DEVINST
GetDeviceInstForRemoval(
_In_ PHOTPLUG_DATA pHotplugData)
Expand All @@ -37,90 +33,19 @@ GetDeviceInstForRemoval(
return tvItem.lParam;
}

static
VOID
InsertConfirmDeviceListItem(
_In_ HWND hwndCfmDeviceList,
_In_ DEVINST DevInst,
_In_ PHOTPLUG_DATA pHotplugData)
{
WCHAR szDisplayName[40];
WCHAR szGuidString[MAX_GUID_STRING_LEN];
DWORD dwSize;
GUID ClassGuid;
INT nClassImage;
CONFIGRET cr;
LVITEMW lvItem;

/* Get the device description */
dwSize = sizeof(szDisplayName);
cr = CM_Get_DevNode_Registry_Property(DevInst,
CM_DRP_DEVICEDESC,
NULL,
szDisplayName,
&dwSize,
0);
if (cr != CR_SUCCESS)
wcscpy(szDisplayName, L"Unknown Device");

/* Get the class GUID */
dwSize = sizeof(szGuidString);
cr = CM_Get_DevNode_Registry_Property(DevInst,
CM_DRP_CLASSGUID,
NULL,
szGuidString,
&dwSize,
0);
if (cr == CR_SUCCESS)
{
pSetupGuidFromString(szGuidString, &ClassGuid);
}
else
{
memcpy(&ClassGuid, &GUID_DEVCLASS_UNKNOWN, sizeof(GUID));
}

/* Get the image for the class this device is in */
SetupDiGetClassImageIndex(&pHotplugData->ImageListData,
&ClassGuid,
&nClassImage);

/* Add it to the confirm device list */
ZeroMemory(&lvItem, sizeof(lvItem));
lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
lvItem.iItem = ListView_GetItemCount(hwndCfmDeviceList);
lvItem.pszText = szDisplayName;
lvItem.iImage = nClassImage;
lvItem.lParam = (LPARAM)DevInst;

ListView_InsertItem(hwndCfmDeviceList, &lvItem);
}

static
VOID
CfmListRecursiveInsertSubDevices(
_In_ HWND hwndCfmDeviceList,
_In_ DEVINST ParentDevInst,
_In_ PHOTPLUG_DATA pHotplugData)
SafeRemoveDevice(
_In_ DEVINST DevInst)
{
DEVINST ChildDevInst;
PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown;
CONFIGRET cr;

cr = CM_Get_Child(&ChildDevInst, ParentDevInst, 0);
if (cr != CR_SUCCESS)
return;

InsertConfirmDeviceListItem(hwndCfmDeviceList, ChildDevInst, pHotplugData);
CfmListRecursiveInsertSubDevices(hwndCfmDeviceList, ChildDevInst, pHotplugData);

for (;;)
cr = CM_Request_Device_EjectW(DevInst, &VetoType, NULL, 0, 0);
if (cr != CR_SUCCESS && VetoType == PNP_VetoTypeUnknown)
{
cr = CM_Get_Sibling(&ChildDevInst, ChildDevInst, 0);
if (cr != CR_SUCCESS)
return;

InsertConfirmDeviceListItem(hwndCfmDeviceList, ChildDevInst, pHotplugData);
CfmListRecursiveInsertSubDevices(hwndCfmDeviceList, ChildDevInst, pHotplugData);
WCHAR szError[64];
swprintf(szError, L"Failed to remove device (0x%x)", cr);
MessageBoxW(NULL, szError, NULL, MB_ICONEXCLAMATION | MB_OK);
}
}

Expand All @@ -130,41 +55,18 @@ FillConfirmDeviceList(
_In_ HWND hwndCfmDeviceList,
_In_ PHOTPLUG_DATA pHotplugData)
{
DEVINST DevInst;
LVCOLUMNW lvColumn;

DevInst = GetDeviceInstForRemoval(pHotplugData);
if (DevInst == 0)
return;

ZeroMemory(&lvColumn, sizeof(lvColumn));
lvColumn.mask = LVCF_FMT;
lvColumn.fmt = LVCFMT_LEFT | LVCFMT_IMAGE;
ListView_InsertColumn(hwndCfmDeviceList, 0, &lvColumn);

InsertConfirmDeviceListItem(hwndCfmDeviceList, DevInst, pHotplugData);
CfmListRecursiveInsertSubDevices(hwndCfmDeviceList, DevInst, pHotplugData);
CfmListEnumDevices(hwndCfmDeviceList, pHotplugData);

ListView_SetColumnWidth(hwndCfmDeviceList, 0, LVSCW_AUTOSIZE_USEHEADER);
}

static
VOID
SafeRemoveDevice(
_In_ DEVINST DevInst)
{
PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown;
CONFIGRET cr;

cr = CM_Request_Device_EjectW(DevInst, &VetoType, NULL, 0, 0);
if (cr != CR_SUCCESS && VetoType == PNP_VetoTypeUnknown)
{
WCHAR szError[64];
swprintf(szError, L"Failed to remove device (0x%x)", cr);
MessageBoxW(NULL, szError, NULL, MB_ICONEXCLAMATION | MB_OK);
}
}

INT_PTR
CALLBACK
ConfirmRemovalDlgProc(
Expand Down Expand Up @@ -199,12 +101,8 @@ ConfirmRemovalDlgProc(
switch (LOWORD(wParam))
{
case IDOK:
SafeRemoveDevice(GetDeviceInstForRemoval(pHotplugData));
EndDialog(hwndDlg, 0);
break;

case IDCANCEL:
EndDialog(hwndDlg, 0);
EndDialog(hwndDlg, LOWORD(wParam));
break;
}

Expand Down
Loading

0 comments on commit ae92a26

Please sign in to comment.