Skip to content

Commit

Permalink
[HOTPLUG] **WIP** Implement confirm devices list
Browse files Browse the repository at this point in the history
  • Loading branch information
TAN-Gaming committed Oct 29, 2023
1 parent ba889e1 commit f170e44
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 21 deletions.
134 changes: 131 additions & 3 deletions dll/cpl/hotplug/eject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,117 @@

#include "hotplug.h"

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

static
DEVINST
GetDeviceInstForRemoval(
_In_ PHOTPLUG_DATA pHotplugData)
{
HTREEITEM hTreeItem;
TVITEMW tvItem;

hTreeItem = TreeView_GetSelection(pHotplugData->hwndDeviceTree);
if (!hTreeItem)
return 0;

/* Find top-level parent item */
while (TreeView_GetParent(pHotplugData->hwndDeviceTree, hTreeItem))
{
hTreeItem = TreeView_GetParent(pHotplugData->hwndDeviceTree, hTreeItem);
}

ZeroMemory(&tvItem, sizeof(tvItem));
tvItem.mask = TVIF_PARAM;
tvItem.hItem = hTreeItem;

TreeView_GetItem(pHotplugData->hwndDeviceTree, &tvItem);

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;
lvItem.iItem = ListView_GetItemCount(hwndCfmDeviceList);
lvItem.pszText = szDisplayName;
lvItem.iImage = nClassImage;

ListView_InsertItem(hwndCfmDeviceList, &lvItem);
}

static
VOID
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);

ListView_SetColumnWidth(hwndCfmDeviceList, 0, LVSCW_AUTOSIZE_USEHEADER);
}

static
VOID
SafeRemoveDevice(
Expand All @@ -32,20 +143,33 @@ ConfirmRemovalDlgProc(
WPARAM wParam,
LPARAM lParam)
{
static DEVINST selectedDev;
PHOTPLUG_DATA pHotplugData;

pHotplugData = (PHOTPLUG_DATA)GetWindowLongPtrW(hwndDlg, DWLP_USER);

switch (uMsg)
{
case WM_INITDIALOG:
selectedDev = lParam;
{
pHotplugData = (PHOTPLUG_DATA)lParam;
SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)pHotplugData);

ListView_SetImageList(GetDlgItem(hwndDlg, IDC_CONFIRM_STOP_DEVICE_LIST),
pHotplugData->ImageListData.ImageList,
LVSIL_SMALL);

FillConfirmDeviceList(GetDlgItem(hwndDlg, IDC_CONFIRM_STOP_DEVICE_LIST),
pHotplugData);

return TRUE;
}

case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
SafeRemoveDevice(selectedDev);
SafeRemoveDevice(GetDeviceInstForRemoval(pHotplugData));
EndDialog(hwndDlg, TRUE);
break;

Expand All @@ -56,6 +180,10 @@ ConfirmRemovalDlgProc(

break;
}

case WM_DESTROY:
SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)NULL);
break;
}

return FALSE;
Expand Down
28 changes: 10 additions & 18 deletions dll/cpl/hotplug/hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@
#define NDEBUG
#include <debug.h>


typedef struct _HOTPLUG_DATA
{
HICON hIcon;
HICON hIconSm;
SP_CLASSIMAGELIST_DATA ImageListData;
HMENU hPopupMenu;
DWORD dwFlags;
} HOTPLUG_DATA, *PHOTPLUG_DATA;


// globals
HINSTANCE hApplet = 0;

Expand Down Expand Up @@ -456,7 +445,7 @@ SafeRemovalDlgProc(
SetupDiGetClassImageList(&pHotplugData->ImageListData);

pHotplugData->hPopupMenu = LoadMenu(hApplet, MAKEINTRESOURCE(IDM_POPUP_DEVICE_TREE));

pHotplugData->hwndDeviceTree = GetDlgItem(hwndDlg, IDC_SAFE_REMOVE_DEVICE_TREE);
pHotplugData->dwFlags = GetHotPlugFlags();

if (pHotplugData->dwFlags & HOTPLUG_DISPLAY_DEVICE_COMPONENTS)
Expand Down Expand Up @@ -509,12 +498,15 @@ SafeRemovalDlgProc(
case IDC_SAFE_REMOVE_STOP:
case IDM_STOP:
{
HWND hwndDevTree = GetDlgItem(hwndDlg, IDC_SAFE_REMOVE_DEVICE_TREE);
DialogBoxParamW(hApplet,
MAKEINTRESOURCEW(IDD_CONFIRM_STOP_HARDWARE_DIALOG),
hwndDlg,
ConfirmRemovalDlgProc,
(LPARAM)GetSelectedDeviceInst(hwndDevTree));
if (pHotplugData != NULL)
{
DialogBoxParamW(hApplet,
MAKEINTRESOURCEW(IDD_CONFIRM_STOP_HARDWARE_DIALOG),
hwndDlg,
ConfirmRemovalDlgProc,
(LPARAM)pHotplugData);
}

break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions dll/cpl/hotplug/hotplug.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ typedef struct
APPLET_PROC AppletProc;
}APPLET, *PAPPLET;

typedef struct _HOTPLUG_DATA
{
HICON hIcon;
HICON hIconSm;
SP_CLASSIMAGELIST_DATA ImageListData;
HMENU hPopupMenu;
HWND hwndDeviceTree;
DWORD dwFlags;
} HOTPLUG_DATA, *PHOTPLUG_DATA;

// eject.c
INT_PTR
Expand Down

0 comments on commit f170e44

Please sign in to comment.