| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510 |
- #include "StdAfx.h"
- #include "RvcDevMnt.h"
- #include "FreeRDPWrapFSM.h"
- #include <tchar.h>
- #include <strsafe.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #pragma comment(lib, "SetupAPI.lib")
- #pragma comment(lib, "Newdev.lib")
- //
- // Copied Macros from ntddk.h
- //
- #define CONTAINING_RECORD(address, type, field) ((type *)( \
- (PCHAR)(address) - \
- (ULONG_PTR)(&((type *)0)->field)))
- #define InitializeListHead(ListHead) (\
- (ListHead)->Flink = (ListHead)->Blink = (ListHead))
- #define RemoveHeadList(ListHead) \
- (ListHead)->Flink;\
- {RemoveEntryList((ListHead)->Flink)}
- #define IsListEmpty(ListHead) \
- ((ListHead)->Flink == (ListHead))
- #define RemoveEntryList(Entry) {\
- PLIST_ENTRY _EX_Blink;\
- PLIST_ENTRY _EX_Flink;\
- _EX_Flink = (Entry)->Flink;\
- _EX_Blink = (Entry)->Blink;\
- _EX_Blink->Flink = _EX_Flink;\
- _EX_Flink->Blink = _EX_Blink;\
- }
- #define InsertTailList(ListHead,Entry) {\
- PLIST_ENTRY _EX_Blink;\
- PLIST_ENTRY _EX_ListHead;\
- _EX_ListHead = (ListHead);\
- _EX_Blink = _EX_ListHead->Blink;\
- (Entry)->Flink = _EX_ListHead;\
- (Entry)->Blink = _EX_Blink;\
- _EX_Blink->Flink = (Entry);\
- _EX_ListHead->Blink = (Entry);\
- }
- //
- // UpdateDriverForPlugAndPlayDevices
- //
- typedef BOOL(WINAPI *UpdateDriverForPlugAndPlayDevicesProto)(_In_opt_ HWND hwndParent,
- _In_ LPCTSTR HardwareId,
- _In_ LPCTSTR FullInfPath,
- _In_ DWORD InstallFlags,
- _Out_opt_ PBOOL bRebootRequired
- );
- typedef BOOL(WINAPI *SetupSetNonInteractiveModeProto)(_In_ BOOL NonInteractiveFlag
- );
- typedef BOOL(WINAPI *SetupUninstallOEMInfProto)(_In_ LPCTSTR InfFileName,
- _In_ DWORD Flags,
- PVOID Reserved
- );
- typedef BOOL(WINAPI *DiShowUpdateDeviceProto)(
- _In_opt_ HWND hwndParent,
- _In_ HDEVINFO DeviceInfoSet,
- _In_ PSP_DEVINFO_DATA DeviceInfoData,
- _In_ DWORD Flags,
- _Out_opt_ PBOOL NeedReboot
- );
- typedef BOOL(WINAPI *DiInstallDriverProto)(
- _In_opt_ HWND hwndParent,
- _In_ LPCTSTR FullInfPath,
- _In_ DWORD Flags,
- _Out_opt_ PBOOL NeedReboot
- );
- typedef BOOL(WINAPI *DiRollbackDriverProto)(
- _In_ HDEVINFO DeviceInfoSet,
- _In_ PSP_DEVINFO_DATA DeviceInfoData,
- _In_opt_ HWND hwndParent,
- _In_ DWORD Flags,
- _Out_opt_ PBOOL NeedReboot
- );
- typedef BOOL(WINAPI *InstallSelectedDriverProto)(
- _In_ HWND hwndParent,
- _In_ HDEVINFO DeviceInfoSet,
- _In_ LPCTSTR Reserved,
- _In_ BOOL Backup,
- _Out_ PDWORD bReboot
- );
- #if _SETUPAPI_VER >= _WIN32_WINNT_WINXP
- typedef BOOL(WINAPI *SetupVerifyInfFileProto)(_In_ LPCTSTR InfName,
- _In_opt_ PSP_ALTPLATFORM_INFO_V2 AltPlatformInfo,
- _Inout_ PSP_INF_SIGNER_INFO InfSignerInfo);
- #endif
- #ifdef _UNICODE
- #define UPDATEDRIVERFORPLUGANDPLAYDEVICES "UpdateDriverForPlugAndPlayDevicesW"
- #define SETUPUNINSTALLOEMINF "SetupUninstallOEMInfW"
- #else
- #define UPDATEDRIVERFORPLUGANDPLAYDEVICES "UpdateDriverForPlugAndPlayDevicesA"
- #define SETUPUNINSTALLOEMINF "SetupUninstallOEMInfA"
- #endif
- #define SETUPSETNONINTERACTIVEMODE "SetupSetNonInteractiveMode"
- #define SETUPVERIFYINFFILE "SetupVerifyInfFile"
- #define DISHOWUPDATEDEVICE "DiShowUpdateDevice"
- #define DIINSTALLDRIVER "DiInstallDriver"
- #define INSTALLSELECTEDDRIVER "InstallSelectedDriver"
- TCHAR DRIVER_NAME[] = "RvcHIDFiltr";
- const int DRIVER_NAME_LEN = strlen(DRIVER_NAME);
- HINSTANCE hInst;
- HWND ghWnd;
- LIST_ENTRY ListHead;
- LIST_ENTRY ListHeadForMonitor;
- HDEVNOTIFY hInterfaceNotification;
- HDEVNOTIFY hInterfaceNotificationForMonitor;
- static BOOL gbKbdAspire = FALSE;
- static BOOL gbMouAspire = FALSE;
- static CRITICAL_SECTION csListEntry;
- static CRITICAL_SECTION csListEntryForMonitor;
- #define Display2(fmt, ...) \
- Dbg("< RvcDevMnt> :" fmt, ##__VA_ARGS__)/*; \
- printf("< RvcDevMnt> :" fmt "\n", ##__VA_ARGS__)*/
- CFreeRDPWrapFSM * pMntFsm = NULL;
- void DevMnt_SetReference(PVOID param) {
- pMntFsm = reinterpret_cast<CFreeRDPWrapFSM*>(param);
- }
- UINT ToLowerCase(TCHAR *szChars)
- {
- if (szChars == NULL) {
- return 0;
- }
- UINT charsLen = lstrlen(szChars);
- UINT uCount = 0;
- for (UINT i = 0; i < charsLen; ++i) {
- if (szChars[i] >= 'A' && szChars[i] <= 'Z') {
- szChars[i] = szChars[i] + ('a' - 'A');
- uCount++;
- }
- }
- return uCount;
- }
- UINT ToUpperCase(TCHAR *szChars)
- {
- if (szChars == NULL) {
- return 0;
- }
- UINT charsLen = lstrlen(szChars);
- UINT uCount = 0;
- for (UINT i = 0; i < charsLen; ++i) {
- if (szChars[i] >= 'a' && szChars[i] <= 'z') {
- szChars[i] = szChars[i] + ('A' - 'a');
- uCount++;
- }
- }
- return uCount;
- }
- GUID* StrToGUID(const WCHAR *guid_string)
- {
- char buffer[64];
- GUID* guid = (GUID*)buffer;
- WCHAR upperGuid[40 + 1] = { 0 };
- for (auto i = 0; i < 40 + 1; i++)
- {
- upperGuid[i] = guid_string[i];
- if (upperGuid[i] >= L'a' && upperGuid[i] <= L'z')
- {
- upperGuid[i] += L'A' - L'a';
- }
- }
- swscanf_s(upperGuid, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
- &guid->Data1,
- &guid->Data2,
- &guid->Data3,
- &guid->Data4[0],
- &guid->Data4[1],
- &guid->Data4[2],
- &guid->Data4[3],
- &guid->Data4[4],
- &guid->Data4[5],
- &guid->Data4[6],
- &guid->Data4[7]);
- return guid;
- }
- static GUID InterfaceGuid = *StrToGUID(L"{4D1E55B2-F16F-11CF-88CB-001111000030}");
- static GUID CCDGGuid = *StrToGUID(L"{c8213acc-9fde-4bd3-835a-0e35086c1ecc}");
- static GUID KeyboardGuid = *StrToGUID(L"{884b96c3-56ef-11d1-bc8c-00a0c91405dd}");
- static GUID MouseGuid = *StrToGUID(L"{378DE44C-56EF-11D1-BC8C-00A0C91405DD}");
- static const GUID MONITOR_GUID = {0xE6F07B5F, 0xEE97, 0x4a90,
- 0xB0, 0x76, 0x33, 0xF5, 0x7B, 0xF4, 0xEA, 0xA7 };
- TCHAR MouseDriverFileName[MAX_PATH] = "D:\\HIDFilter\\RvcMouFiltr.inf";
- TCHAR KeyboardDriverFileName[MAX_PATH] = "D:\\HIDFilter\\RvcKbdFiltr.inf";
- #define SYS_MOUSE_DRIVERNAME "RvcMouFiltr.inf"
- #define SYS_KEYBOARD_DRIVERNAME "RvcKbdFiltr.inf"
- void OnDevListChange(BOOL fIncreased);
- void OnMonitorChange(int currentCoun);
- UINT GetMonitorCount();
- BOOL IsDevDeficient(
- BOOL& bMouRequired,
- BOOL& bKbdRequired,
- USHORT* pUsMouCount,
- USHORT* pUsKbdCount);
- BOOLEAN DeviceNameMatches(LPTSTR DeviceName)
- {
- BOOLEAN matching = FALSE;
- matching = (_tcscmp(KEYBOARD_FRIENDLYNAME, DeviceName) == 0) ? TRUE : FALSE;
- if (!matching) {
- matching = (_tcscmp(MOUSE_FRIENDLYNAME, DeviceName) == 0) ? TRUE : FALSE;
- }
- return matching;
- }
- HANDLE GetDevTypeFromRawInfo(DEVTYPE* type, char* aimDevName)
- {
- HANDLE hDevice = NULL;
- PRAWINPUTDEVICELIST pRawDevList = NULL;
- UINT rawDevCount = 0;
- UINT uByteSize = 0;
- UINT ValidLen;
- char* ptGuid = NULL;
- if (type == NULL || aimDevName == NULL || strlen(aimDevName) == 0) {
- return NULL;
- }
- ValidLen = strlen(aimDevName);
- ptGuid = strchr(aimDevName, '{');
- if (ptGuid != NULL) {
- if (ptGuid - aimDevName <= 0) {
- return NULL;
- }
- ValidLen = ptGuid - aimDevName;
- }
- if (GetRawInputDeviceList(NULL, &rawDevCount, sizeof(RAWINPUTDEVICELIST)) != 0) {
- Display2("GetRawInputDeviceList error, GLE=%u", GetLastError());
- return NULL;
- }
- pRawDevList = static_cast<PRAWINPUTDEVICELIST>(HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY, sizeof(RAWINPUTDEVICELIST)*rawDevCount));
- if (pRawDevList == NULL)
- {
- return NULL;
- }
- *type = DEV_UNSUPPORT;
- UINT tmpDevCount = 0;
- if ((tmpDevCount = GetRawInputDeviceList(pRawDevList, &rawDevCount, sizeof(RAWINPUTDEVICELIST)))
- == static_cast<UINT>(-1)) {
- HeapFree(GetProcessHeap(), 0, pRawDevList);
- return NULL;
- }
- rawDevCount = tmpDevCount;
- for (UINT i = 0; i<rawDevCount; ++i) {
- UINT uInSize = MAX_PATH * 2;
- char devName[MAX_PATH * 2] = { 0 };
- UINT uRes1 = GetRawInputDeviceInfo(pRawDevList[i].hDevice,
- RIDI_DEVICENAME, devName, &uInSize);
- //the item which Conatins "ROOT" seems as combined device ??
- if (strstr(devName, "ROOT") != NULL) {
- //Display2("Combined device");
- continue;
- }
- ptGuid = strchr(devName, '{');
- if (ptGuid != NULL && ptGuid - devName > 0) {
- int len = ptGuid - devName;
- if (len == ValidLen) {
- if (!_strnicmp(devName, aimDevName, len)) {
- //Display2("%s(%04x):", devName, pRawDevList[i].hDevice);
- RID_DEVICE_INFO devInfo = { 0 };
- uInSize = sizeof(RID_DEVICE_INFO);
- UINT uRes2 = GetRawInputDeviceInfo(pRawDevList[i].hDevice,
- RIDI_DEVICEINFO, &devInfo, &uInSize);
- if (devInfo.dwType == RIM_TYPEHID) {
- *type = DEV_UNSUPPORT;
- }
- else if (devInfo.dwType == RIM_TYPEKEYBOARD) {
- *type = DEV_KEYBOARD;
- }
- else if (devInfo.dwType == RIM_TYPEMOUSE) {
- *type = DEV_MOUSE;
- }
- hDevice = pRawDevList[i].hDevice;
- break;
- }
- }
- }
- }
- HeapFree(GetProcessHeap(), 0, pRawDevList);
- return hDevice;
- }
- BOOL DetectAndSetDeviceType(PDEVICE_INFO devInfo)
- {
- if (devInfo == NULL) {
- return FALSE;
- }
- devInfo->Drivered = FALSE;
- devInfo->DevType = DEV_UNSUPPORT;
- devInfo->hRawDevice = GetDevTypeFromRawInfo(&devInfo->DevType, devInfo->DevicePath);
- if (strlen((LPCTSTR)devInfo->DeviceName) == strlen(KEYBOARD_FRIENDLYNAME)
- &&
- !memcmp(devInfo->DeviceName, KEYBOARD_FRIENDLYNAME, strlen(KEYBOARD_FRIENDLYNAME))) {
- devInfo->Drivered = TRUE;
- devInfo->DevType = DEV_KEYBOARD;
- }
- else if (strlen((LPCTSTR)devInfo->DeviceName) == strlen(MOUSE_FRIENDLYNAME)
- &&
- !memcmp(devInfo->DeviceName, MOUSE_FRIENDLYNAME, strlen(MOUSE_FRIENDLYNAME))) {
- devInfo->Drivered = TRUE;
- devInfo->DevType = DEV_MOUSE;
- }
- return TRUE;
- }
- BOOL HandleDeviceInterfaceChange(HWND hWnd, DWORD evtype, PDEV_BROADCAST_DEVICEINTERFACE dip)
- {
- DEV_BROADCAST_HANDLE filter;
- PDEVICE_INFO deviceInfo = NULL;
- HRESULT hr;
- BOOL fNeedOpen = FALSE;
- switch (evtype)
- {
- case DBT_DEVICEARRIVAL:
- //
- // New device arrived. Open handle to the device
- // and register notification of type DBT_DEVTYP_HANDLE
- //
- deviceInfo = static_cast<PDEVICE_INFO>(HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY, sizeof(DEVICE_INFO)));
- if (!deviceInfo)
- return FALSE;
- fNeedOpen = FALSE;
- if (!GetDeviceDescription(dip->dbcc_name,
- (PBYTE)deviceInfo->DeviceName,
- sizeof(deviceInfo->DeviceName),
- (PBYTE)deviceInfo->DeviceInstance,
- sizeof(deviceInfo->DeviceInstance), &fNeedOpen)) {
- //MessageBox(TEXT("GetDeviceDescription failed"), TEXT("Error!"), MB_OK);
- }
- Display2(TEXT("New device Arrived (Interface Change Notification): %s"),
- deviceInfo->DeviceName);
- hr = StringCchCopy(deviceInfo->DevicePath, MAX_PATH, dip->dbcc_name);
- if (FAILED(hr)){
- // DeviceInfo will be freed later by the cleanup routine.
- break;
- }
- if (!DetectAndSetDeviceType(deviceInfo)) {
- break;
- }
- if(deviceInfo->DevType == DEV_UNSUPPORT) {
- Display2(TEXT("Needless to the device: %s"), deviceInfo->DeviceName);
- break;
- }
- InitializeListHead(&deviceInfo->ListEntry);
- EnterCriticalSection(&csListEntry);
- InsertTailList(&ListHead, &deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntry);
- OnDevListChange(TRUE);
- deviceInfo->hDevice = CreateFile(dip->dbcc_name,
- /*GENERIC_READ | GENERIC_WRITE*/0,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING, 0, NULL);
- if (deviceInfo->hDevice == INVALID_HANDLE_VALUE) {
- Display2(TEXT("Failed to open the device: %s"), deviceInfo->DeviceName);
- break;
- }
- Display2(TEXT("Opened handled to the device: %s"), deviceInfo->DeviceName);
- memset(&filter, 0, sizeof(filter)); //zero the structure
- filter.dbch_size = sizeof(filter);
- filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
- filter.dbch_handle = deviceInfo->hDevice;
- deviceInfo->hHandleNotification =
- RegisterDeviceNotification(hWnd, &filter, 0);
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- Display2(TEXT("Remove Complete (Interface Change Notification)"));
- break;
- //
- // Device Removed.
- //
- default:
- Display2(TEXT("Unknown (Interface Change Notification)"));
- break;
- }
- return TRUE;
- }
- BOOL HandleDeviceChange(HWND hWnd, DWORD evtype, PDEV_BROADCAST_HANDLE dhp)
- {
- DEV_BROADCAST_HANDLE filter;
- PDEVICE_INFO deviceInfo = NULL;
- PLIST_ENTRY thisEntry;
- //
- // Walk the list to get the deviceInfo for this device
- // by matching the handle given in the notification.
- //
- for (thisEntry = ListHead.Flink; thisEntry != &ListHead;
- thisEntry = thisEntry->Flink)
- {
- deviceInfo = CONTAINING_RECORD(thisEntry, DEVICE_INFO, ListEntry);
- if (dhp->dbch_hdevnotify == deviceInfo->hHandleNotification) {
- break;
- }
- deviceInfo = NULL;
- }
- if (!deviceInfo) {
- Display2(TEXT("Error: spurious message, Event Type %x, Device Type %x"),
- evtype, dhp->dbch_devicetype);
- return FALSE;
- }
- switch (evtype)
- {
- case DBT_DEVICEQUERYREMOVE:
- Display2(TEXT("Query Remove (Handle Notification) %s"), deviceInfo->DeviceName);
- // User is trying to disable, uninstall, or eject our device.
- // Close the handle to the device so that the target device can
- // get removed. Do not unregister the notification
- // at this point, because we want to know whether
- // the device is successfully removed or not.
- //
- if (deviceInfo->hDevice != INVALID_HANDLE_VALUE) {
- CloseHandle(deviceInfo->hDevice);
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- Display2(TEXT("Closed handle to device %s"), deviceInfo->DeviceName);
- }
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- Display2(TEXT("Remove Complete (Handle Notification):%s"),
- deviceInfo->DeviceName);
- //
- // Device is getting surprise removed. So close
- // the handle to device and unregister the PNP notification.
- //
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- }
- if (deviceInfo->hDevice != INVALID_HANDLE_VALUE) {
- CloseHandle(deviceInfo->hDevice);
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- Display2(TEXT("Closed handle to device %s"), deviceInfo->DeviceName);
- }
- //
- // Unlink this deviceInfo from the list and free the memory
- //
- EnterCriticalSection(&csListEntry);
- RemoveEntryList(&deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntry);
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- OnDevListChange(FALSE);
- break;
- case DBT_DEVICEREMOVEPENDING:
- Display2(TEXT("Remove Pending (Handle Notification):%s"),
- deviceInfo->DeviceName);
- //
- // Device is successfully removed so unregister the notification
- // and free the memory.
- //
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- }
- //
- // Unlink this deviceInfo from the list and free the memory
- //
- EnterCriticalSection(&csListEntry);
- RemoveEntryList(&deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntry);
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- OnDevListChange(FALSE);
- break;
- case DBT_DEVICEQUERYREMOVEFAILED:
- Display2(TEXT("Remove failed (Handle Notification):%s"), deviceInfo->DeviceName);
- //
- // Remove failed. So reopen the device and register for
- // notification on the new handle. But first we should unregister
- // the previous notification.
- //
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- }
- deviceInfo->hDevice = CreateFile(deviceInfo->DevicePath,
- /*GENERIC_READ | GENERIC_WRITE*/0,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, OPEN_EXISTING, 0, NULL);
- if (deviceInfo->hDevice == INVALID_HANDLE_VALUE) {
- Display2(TEXT("Failed to reopen the device: %s"),
- deviceInfo->DeviceName);
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- break;
- }
- //
- // Register handle based notification to receive pnp
- // device change notification on the handle.
- //
- memset(&filter, 0, sizeof(filter)); //zero the structure
- filter.dbch_size = sizeof(filter);
- filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
- filter.dbch_handle = deviceInfo->hDevice;
- deviceInfo->hHandleNotification =
- RegisterDeviceNotification(hWnd, &filter, 0);
- Display2(TEXT("Reopened device %s"), deviceInfo->DeviceName);
- break;
- default:
- Display2(TEXT("Unknown (Handle Notification)"), deviceInfo->DeviceName);
- break;
- }
- return TRUE;
- }
- BOOL HandleMonitorInterfaceChange(HWND hWnd, DWORD evType, PDEV_BROADCAST_DEVICEINTERFACE dip)
- {
- DEV_BROADCAST_HANDLE filter;
- PDEVICE_INFO deviceInfo = NULL;
- HRESULT hr;
- BOOL fNoUsed = FALSE;
- switch (evType)
- {
- case DBT_DEVICEARRIVAL:
- deviceInfo = static_cast<PDEVICE_INFO>(HeapAlloc(
- GetProcessHeap(),
- HEAP_ZERO_MEMORY, sizeof(DEVICE_INFO)));
- if (!deviceInfo)
- {
- return FALSE;
- }
- GetDeviceDescription(dip->dbcc_name,
- (PBYTE)deviceInfo->DeviceName,
- sizeof(deviceInfo->DeviceName),
- (PBYTE)deviceInfo->DeviceInstance,
- sizeof(deviceInfo->DeviceInstance), &fNoUsed);
- Display2(TEXT("New Monitor Arrived (Interface Change Notification): %s"),
- deviceInfo->DeviceName);
- hr = StringCchCopy(deviceInfo->DevicePath, MAX_PATH, dip->dbcc_name);
- if (FAILED(hr)) {
- break;
- }
- InitializeListHead(&deviceInfo->ListEntry);
- EnterCriticalSection(&csListEntryForMonitor);
- InsertTailList(&ListHeadForMonitor, &deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntryForMonitor);
- OnMonitorChange(GetMonitorCount());
-
- deviceInfo->hDevice = CreateFile(dip->dbcc_name,
- /*GENERIC_READ | GENERIC_WRITE*/0,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING, 0, NULL);
- if (deviceInfo->hDevice == INVALID_HANDLE_VALUE) {
- Display2(TEXT("Failed to open the device: %s"), deviceInfo->DeviceName);
- break;
- }
- Display2(TEXT("Opened handled to the Monitor device: %s"), deviceInfo->DeviceName);
- memset(&filter, 0, sizeof(filter)); //zero the structure
- filter.dbch_size = sizeof(filter);
- filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
- filter.dbch_handle = deviceInfo->hDevice;
- deviceInfo->hHandleNotification =
- RegisterDeviceNotification(hWnd, &filter, 0);
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- Display2(TEXT("Monitor Remove Complete (Interface Change Notification)"));
- break;
- default:
- Display2(TEXT("Monitor Unknown (Interface Change Notification)"));
- break;
- }
- return TRUE;
- }
- BOOL HandleMonitorChange(HWND hWnd, DWORD evType, PDEV_BROADCAST_HANDLE dhp)
- {
- DEV_BROADCAST_HANDLE filter;
- PDEVICE_INFO deviceInfo = NULL;
- PLIST_ENTRY thisEntry;
- for (thisEntry = ListHeadForMonitor.Flink; thisEntry != &ListHeadForMonitor;
- thisEntry = thisEntry->Flink)
- {
- deviceInfo = CONTAINING_RECORD(thisEntry, DEVICE_INFO, ListEntry);
- if (dhp->dbch_hdevnotify == deviceInfo->hHandleNotification) {
- break;
- }
- deviceInfo = NULL;
- }
- if (!deviceInfo) {
- Display2(TEXT("Monitor Error: spurious message, Event Type %x, Device Type %x"),
- evType, dhp->dbch_devicetype);
- return FALSE;
- }
- switch (evType)
- {
- case DBT_DEVICEQUERYREMOVE:
- Display2(TEXT("Monitor Remove (Handle Notification) %s"), deviceInfo->DeviceName);
- if (deviceInfo->hDevice != INVALID_HANDLE_VALUE) {
- CloseHandle(deviceInfo->hDevice);
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- Display2(TEXT("Closed handle to device %s"), deviceInfo->DeviceName);
- }
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- Display2(TEXT("Monitor Remove Complete (Handle Notification):%s"),
- deviceInfo->DeviceName);
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- }
- if (deviceInfo->hDevice != INVALID_HANDLE_VALUE) {
- CloseHandle(deviceInfo->hDevice);
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- Display2(TEXT("Closed handle to device %s"), deviceInfo->DeviceName);
- }
- EnterCriticalSection(&csListEntryForMonitor);
- RemoveEntryList(&deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntryForMonitor);
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- OnMonitorChange(GetMonitorCount());
- break;
- case DBT_DEVICEREMOVEPENDING:
- Display2(TEXT("Monitor Remove Pending (Handle Notification):%s"),
- deviceInfo->DeviceName);
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- }
- EnterCriticalSection(&csListEntryForMonitor);
- RemoveEntryList(&deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntryForMonitor);
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- OnMonitorChange(GetMonitorCount());
- break;
- case DBT_DEVICEQUERYREMOVEFAILED:
- Display2(TEXT("Monitor Remove failed (Handle Notification):%s"), deviceInfo->DeviceName);
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- }
- deviceInfo->hDevice = CreateFile(deviceInfo->DevicePath,
- /*GENERIC_READ | GENERIC_WRITE*/0,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, OPEN_EXISTING, 0, NULL);
- if (deviceInfo->hDevice == INVALID_HANDLE_VALUE) {
- Display2(TEXT("Failed to reopen the device: %s"),
- deviceInfo->DeviceName);
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- break;
- }
- memset(&filter, 0, sizeof(filter)); //zero the structure
- filter.dbch_size = sizeof(filter);
- filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
- filter.dbch_handle = deviceInfo->hDevice;
- deviceInfo->hHandleNotification =
- RegisterDeviceNotification(hWnd, &filter, 0);
- Display2(TEXT("Reopened Monitor device %s"), deviceInfo->DeviceName);
- break;
- default:
- Display2(TEXT("Monitor Unknown (Handle Notification)"), deviceInfo->DeviceName);
- break;
- }
- return TRUE;
- }
- BOOLEAN Cleanup(HWND hWnd)
- {
- PDEVICE_INFO deviceInfo = NULL;
- PLIST_ENTRY thisEntry;
- EnterCriticalSection(&csListEntry);
- while (!IsListEmpty(&ListHead)) {
- thisEntry = RemoveHeadList(&ListHead);
- deviceInfo = CONTAINING_RECORD(thisEntry, DEVICE_INFO, ListEntry);
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- }
- if (deviceInfo->hDevice != INVALID_HANDLE_VALUE &&
- deviceInfo->hDevice != NULL) {
- CloseHandle(deviceInfo->hDevice);
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- Display2(TEXT("Closed handle to device %s"), deviceInfo->DeviceName);
- }
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- }
- LeaveCriticalSection(&csListEntry);
- EnterCriticalSection(&csListEntryForMonitor);
- while (!IsListEmpty(&ListHeadForMonitor)) {
- thisEntry = RemoveHeadList(&ListHeadForMonitor);
- deviceInfo = CONTAINING_RECORD(thisEntry, DEVICE_INFO, ListEntry);
- if (deviceInfo->hHandleNotification) {
- UnregisterDeviceNotification(deviceInfo->hHandleNotification);
- deviceInfo->hHandleNotification = NULL;
- }
- if (deviceInfo->hDevice != INVALID_HANDLE_VALUE &&
- deviceInfo->hDevice != NULL) {
- CloseHandle(deviceInfo->hDevice);
- deviceInfo->hDevice = INVALID_HANDLE_VALUE;
- Display2(TEXT("Closed handle to device %s"), deviceInfo->DeviceName);
- }
- HeapFree(GetProcessHeap(), 0, deviceInfo);
- }
- LeaveCriticalSection(&csListEntryForMonitor);
- return TRUE;
- }
- DWORD WINAPI DevMnt_MainWndStartAndLoop(LPVOID param)
- {
- static TCHAR szAppName[]=TEXT("Rvc InOut Device Management");
- static TCHAR szTitle[] =TEXT("Rvc InOut Device Application");
- HWND hWnd;
- MSG msg;
- WNDCLASS wndclass;
- HINSTANCE hInstance = GetModuleHandle (NULL);
- BOOL fGotMessage;
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = NULL;
- wndclass.hCursor = NULL;
- wndclass.hbrBackground= NULL;
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName= szAppName;
- RegisterClass(&wndclass);
- hWnd = CreateWindow (szAppName,
- szTitle,
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL);
- if(!hWnd) {
- return (0);
- }
- ghWnd = hWnd;
- ShowWindow (hWnd, SW_HIDE);
- UpdateWindow(hWnd);
- while ((fGotMessage = GetMessage (&msg, NULL, 0,0)) != 0 && fGotMessage != -1)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (0);
- }
- void DevMnt_Terminate()
- {
- SendMessage(ghWnd, WM_CLOSE, 0, 0);
- return;
- }
- BOOL CALLBACK MyInfoEnumProc(
- HMONITOR hMonitor,
- HDC hdcMonitor,
- LPRECT lprcMonitor,
- LPARAM dwData
- )
- {
- MONITORINFOEX mi;
- ZeroMemory(&mi, sizeof(mi));
- mi.cbSize = sizeof(mi);
- GetMonitorInfo(hMonitor, &mi);
- Display2(TEXT("DisplayDevice %s"), mi.szDevice);
- return TRUE;
- }
- UINT GetMonitorCount()
- {
- PLIST_ENTRY thisEntry;
- PDEVICE_INFO deviceInfo = NULL;
- UINT result = 0;
- EnterCriticalSection(&csListEntryForMonitor);
- for (thisEntry = ListHeadForMonitor.Flink; thisEntry != &ListHeadForMonitor;
- thisEntry = thisEntry->Flink)
- {
- deviceInfo = CONTAINING_RECORD(thisEntry, DEVICE_INFO, ListEntry);
- if (deviceInfo != NULL) {
- result++;
- }
- }
- LeaveCriticalSection(&csListEntryForMonitor);
- UINT screenNum = GetSystemMetrics(SM_CMONITORS);
- if(screenNum != result)
- {
- Display2(TEXT("WARNING: ListEntry'Count: %d, GetSystemMetrics'Count %d"), result, screenNum);
- }
- return result;
- }
- BOOL IsDevDeficient(
- BOOL& bMouRequired,
- BOOL& bKbdRequired,
- USHORT* pUsMouCount,
- USHORT* pUsKbdCount)
- {
- PLIST_ENTRY thisEntry;
- PDEVICE_INFO deviceInfo = NULL;
- USHORT kbdCount, mouCount, kbdDrvdCount, mouDrvdCount;
- bMouRequired = bKbdRequired = FALSE;
- kbdCount = mouCount = kbdDrvdCount = mouDrvdCount = 0;
- EnterCriticalSection(&csListEntry);
- for (thisEntry = ListHead.Flink; thisEntry != &ListHead;
- thisEntry = thisEntry->Flink)
- {
- deviceInfo = CONTAINING_RECORD(thisEntry, DEVICE_INFO, ListEntry);
- if (deviceInfo && deviceInfo->DevType != DEV_UNSUPPORT) {
- if (deviceInfo->DevType == DEV_MOUSE) {
- if(deviceInfo->Drivered)
- {mouDrvdCount++;}
- else
- {mouCount++;}
- }else if(deviceInfo->DevType == DEV_KEYBOARD) {
- if(deviceInfo->Drivered)
- {kbdDrvdCount++;}
- else
- {kbdCount++;}
- }
- }
- }
- LeaveCriticalSection(&csListEntry);
- if(pUsKbdCount != NULL) {
- *pUsKbdCount = kbdCount + kbdDrvdCount;
- }
- if(pUsMouCount != NULL) {
- *pUsMouCount = mouCount + mouDrvdCount;
- }
- if(mouDrvdCount == 0) {
- bMouRequired = TRUE;
- }
- if(kbdDrvdCount == 0) {
- bKbdRequired = TRUE;
- }
- return (bMouRequired || bKbdRequired);
- }
- LRESULT
- FAR PASCAL
- WndProc (
- HWND hWnd,
- UINT message,
- WPARAM wParam,
- LPARAM lParam
- )
- {
- DWORD nEventType = (DWORD)wParam;
- PDEV_BROADCAST_HDR p = (PDEV_BROADCAST_HDR) lParam;
- DEV_BROADCAST_DEVICEINTERFACE filter;
- switch (message)
- {
- case WM_CREATE:
- filter.dbcc_size = sizeof(filter);
- filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
- filter.dbcc_classguid = InterfaceGuid;
- hInterfaceNotification = RegisterDeviceNotification(hWnd, &filter, 0);
- filter.dbcc_size = sizeof(filter);
- filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
- filter.dbcc_classguid = MONITOR_GUID;
- hInterfaceNotificationForMonitor = RegisterDeviceNotification(hWnd, &filter, 0);
- if(hInterfaceNotificationForMonitor == NULL)
- {
- Display2("RegisterDeviceNotification for monitor failed, GLE=%u", GetLastError());
- }
- InitializeCriticalSection(&csListEntry);
- InitializeListHead(&ListHead);
- InitializeCriticalSection(&csListEntryForMonitor);
- InitializeListHead(&ListHeadForMonitor);
- if(RegisterHIDDeviceNotification(hWnd)) {
- OnDevListChange(TRUE);
- }
- RegisterMonitorDeviceNotification(hWnd);
- #ifdef TWINKLE_LOCAL_DEBUG
- GetMonitorCount();
- #endif // TWINKLE_LOCAL_DEBUG
- return 0;
- case WM_DEVICECHANGE:
- //
- // The DBT_DEVNODES_CHANGED broadcast message is sent
- // everytime a device is added or removed. This message
- // is typically handled by Device Manager kind of apps,
- // which uses it to refresh window whenever something changes.
- // The lParam is always NULL in this case.
- //
- if (DBT_DEVNODES_CHANGED == wParam) {
- //Display2(TEXT("Received DBT_DEVNODES_CHANGED broadcast message"));
- return 0;
- }
- //
- // All the events we're interested in come with lParam pointing to
- // a structure headed by a DEV_BROADCAST_HDR. This is denoted by
- // bit 15 of wParam being set, and bit 14 being clear.
- //
- if ((wParam & 0xC000) == 0x8000) {
- if (!p)
- return 0;
- if (p->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
- PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;
- if (IsEqualGUID(b->dbcc_classguid, MONITOR_GUID)) {
- Display2(TEXT("Received DBT_DEVTYP_DEVICEINTERFACE for monitor..."));
- HandleMonitorInterfaceChange(hWnd, nEventType, (PDEV_BROADCAST_DEVICEINTERFACE)p);
- return 0;
- }
- HandleDeviceInterfaceChange(hWnd, nEventType, (PDEV_BROADCAST_DEVICEINTERFACE)p);
-
- } else if (p->dbch_devicetype == DBT_DEVTYP_HANDLE) {
- if(!HandleDeviceChange(hWnd, nEventType, (PDEV_BROADCAST_HANDLE)p))
- {
- HandleMonitorChange(hWnd, nEventType, (PDEV_BROADCAST_HANDLE)p);
- }
- }
- }
- return 0;
- case WM_POWERBROADCAST:
- HandlePowerBroadcast(hWnd, wParam, lParam);
- return 0;
- case WM_CLOSE:
- Cleanup(hWnd);
- UnregisterDeviceNotification(hInterfaceNotification);
- UnregisterDeviceNotification(hInterfaceNotificationForMonitor);
- DeleteCriticalSection(&csListEntry);
- DeleteCriticalSection(&csListEntryForMonitor);
- return DefWindowProc(hWnd, message, wParam, lParam);
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- BOOLEAN RegisterDevNotification(HWND hWnd, BOOL bHID)
- {
- HDEVINFO hardwareDeviceInfo;
- SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
- PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;
- SP_CLASSIMAGELIST_DATA deviceClassImageData;
- SP_DEVINFO_DATA devInfo;
- ULONG predictedLength = 0;
- ULONG requiredLength = 0;
- DWORD error;
- DEV_BROADCAST_HANDLE filter;
- PDEVICE_INFO deviceInfo = NULL;
- UINT i = 0;
- HRESULT hr;
- hardwareDeviceInfo = SetupDiGetClassDevs(
- (bHID ? (LPGUID)&InterfaceGuid : (LPGUID)&MONITOR_GUID),
- NULL, // Define no enumerator (global)
- NULL, // Define no
- (DIGCF_PRESENT | // Only Devices present
- DIGCF_DEVICEINTERFACE)); // Function class devices.
- if (INVALID_HANDLE_VALUE == hardwareDeviceInfo)
- {
- goto Error;
- }
- //
- // Enumerate devices of toaster class
- //
- deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
- deviceClassImageData.cbSize = sizeof(deviceClassImageData);
- SetupDiGetClassImageList(&deviceClassImageData);
- ZeroMemory(&devInfo, sizeof(SP_DEVINFO_DATA));
- devInfo.cbSize = sizeof(SP_DEVINFO_DATA);
- for (i = 0; SetupDiEnumDeviceInterfaces(hardwareDeviceInfo,
- 0, // No care about specific PDOs
- (bHID ? (LPGUID)&InterfaceGuid : (LPGUID)&MONITOR_GUID),
- i, //
- &deviceInterfaceData); i++) {
- //
- // Allocate a function class device data structure to
- // receive the information about this particular device.
- //
- //
- // First find out required length of the buffer
- //
- if (deviceInterfaceDetailData)
- {
- HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData);
- deviceInterfaceDetailData = NULL;
- }
- if (!SetupDiGetDeviceInterfaceDetail(
- hardwareDeviceInfo,
- &deviceInterfaceData,
- NULL, // probing so no output buffer yet
- 0, // probing so output buffer length of zero
- &requiredLength,
- NULL) && (error = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
- {
- goto Error;
- }
- predictedLength = requiredLength;
- deviceInterfaceDetailData = static_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(
- HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
- predictedLength));
- if (deviceInterfaceDetailData == NULL) {
- goto Error;
- }
- deviceInterfaceDetailData->cbSize =
- sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- if (!SetupDiGetDeviceInterfaceDetail(
- hardwareDeviceInfo,
- &deviceInterfaceData,
- deviceInterfaceDetailData,
- predictedLength,
- &requiredLength,
- NULL))
- {
- goto Error;
- }
- deviceInfo = static_cast<PDEVICE_INFO>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
- sizeof(DEVICE_INFO)));
- if (deviceInfo == NULL)
- {
- goto Error;
- }
- //
- // Get the device details such as friendly name and SerialNo
- //
- BOOL fNeedOpen = FALSE;
- if (!GetDeviceDescription(deviceInterfaceDetailData->DevicePath,
- (PBYTE)deviceInfo->DeviceName,
- sizeof(deviceInfo->DeviceName),
- (PBYTE)deviceInfo->DeviceInstance,
- sizeof(deviceInfo->DeviceInstance), &fNeedOpen) && bHID)
- {
- goto Error;
- }
- Display2(TEXT("Device: %s, %s"), deviceInfo->DeviceName, deviceInfo->DeviceInstance);
- hr = StringCchCopy(deviceInfo->DevicePath, MAX_PATH, deviceInterfaceDetailData->DevicePath);
- if (FAILED(hr))
- {
- goto Error;
- }
- if (bHID && !DetectAndSetDeviceType(deviceInfo)) {
- goto Error;
- }
- if(bHID && deviceInfo->DevType == DEV_UNSUPPORT) {
- Display2(TEXT("Needless to the device: %s"), deviceInfo->DeviceName);
- continue;
- }
- //Display2(TEXT("==:%s"), deviceInterfaceDetailData->DevicePath);
- //Display2(TEXT("==:%s"), deviceInfo->DeviceName);
- //Display2(TEXT("==:%s"), deviceInfo->DeviceInstance);
- //Display2(TEXT("==:%d"), deviceInfo->DevType);
- InitializeListHead(&deviceInfo->ListEntry);
- if(bHID)
- {
- EnterCriticalSection(&csListEntry);
- InsertTailList(&ListHead, &deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntry);
- }
- else
- {
- EnterCriticalSection(&csListEntryForMonitor);
- InsertTailList(&ListHeadForMonitor, &deviceInfo->ListEntry);
- LeaveCriticalSection(&csListEntryForMonitor);
- }
- //
- // Open an handle to the device.
- //
- deviceInfo->hDevice = CreateFile(
- deviceInterfaceDetailData->DevicePath,
- /*GENERIC_READ | GENERIC_WRITE*/0,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, // no SECURITY_ATTRIBUTES structure
- OPEN_EXISTING, // No special create flags
- 0, // No special attributes
- NULL);
- if (INVALID_HANDLE_VALUE == deviceInfo->hDevice) {
- Display2(TEXT("Failed to open the device: %s"), deviceInfo->DeviceName);
- continue;
- }
- Display2(TEXT("Opened handled to the device: %s"), deviceInfo->DeviceName);
- //
- // Register handle based notification to receive pnp
- // device change notification on the handle.
- //
- memset(&filter, 0, sizeof(filter)); //zero the structure
- filter.dbch_size = sizeof(filter);
- filter.dbch_devicetype = DBT_DEVTYP_HANDLE;
- filter.dbch_handle = deviceInfo->hDevice;
- deviceInfo->hHandleNotification = RegisterDeviceNotification(hWnd, &filter, 0);
- }
- if (deviceInterfaceDetailData)
- HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData);
- SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
- return 1;
- Error:
- if (deviceInterfaceDetailData)
- HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData);
- SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
- Cleanup(hWnd);
- return 0;
- }
- BOOLEAN RegisterHIDDeviceNotification(HWND hWnd)
- {
- return RegisterDevNotification(hWnd, TRUE);
- }
- BOOLEAN RegisterMonitorDeviceNotification(HWND hWnd)
- {
- return RegisterDevNotification(hWnd, FALSE);
- }
- BOOL GetDeviceDescription(
- LPTSTR DevPath,
- PBYTE DevName, ULONG DevNameLen,
- PBYTE DevInstance, ULONG InstanceLen, BOOL* pNeedOpen)
- {
- HDEVINFO hardwareDeviceInfo;
- SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
- SP_DEVINFO_DATA deviceInfoData;
- DWORD dwRegType, error;
- //hardwareDeviceInfo = SetupDiCreateDeviceInfoList(NULL, NULL);
- //if (INVALID_HANDLE_VALUE == hardwareDeviceInfo)
- //{
- // goto Error;
- //}
- hardwareDeviceInfo = SetupDiGetClassDevs(&InterfaceGuid,
- NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- if (INVALID_HANDLE_VALUE == hardwareDeviceInfo) {
- goto Error;
- }
- ZeroMemory(&deviceInterfaceData, sizeof(deviceInterfaceData));
- deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
- SetupDiOpenDeviceInterface(hardwareDeviceInfo, DevPath,
- 0, //
- &deviceInterfaceData);
- ZeroMemory(&deviceInfoData, sizeof(deviceInfoData));
- deviceInfoData.cbSize = sizeof(deviceInfoData);
- if (!SetupDiGetDeviceInterfaceDetail(
- hardwareDeviceInfo,
- &deviceInterfaceData,
- NULL, // probing so no output buffer yet
- 0, // probing so output buffer length of zero
- NULL,
- &deviceInfoData) && (error = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) {
- goto Error;
- }
- //
- // Get the friendly name for this instance, if that fails
- // try to get the device description.
- //
- if (!SetupDiGetDeviceRegistryProperty(hardwareDeviceInfo, &deviceInfoData,
- SPDRP_FRIENDLYNAME,
- &dwRegType,
- DevName,
- DevNameLen,
- NULL)) {
- if (!SetupDiGetDeviceRegistryProperty(hardwareDeviceInfo, &deviceInfoData,
- SPDRP_DEVICEDESC,
- &dwRegType,
- DevName,
- DevNameLen,
- NULL)){
- goto Error;
- }
- }
- if (DeviceNameMatches((LPTSTR)DevName)) {
- //PrintFilters(hardwareDeviceInfo, &deviceInfoData);
- }
- //retrieve unique device instance identifiers (IDs).
- if (!SetupDiGetDeviceInstanceId(hardwareDeviceInfo, &deviceInfoData,
- (PSTR)DevInstance,
- InstanceLen,
- NULL)) {
- Display2(TEXT("Instance id is not available for device: %s"), (LPCTSTR)DevName);
- goto Error;
- }
- ZeroMemory(&deviceInfoData, sizeof(deviceInfoData));
- deviceInfoData.cbSize = sizeof(deviceInfoData);
- if (!SetupDiOpenDeviceInfo(hardwareDeviceInfo, (PCSTR)DevInstance, NULL, 0, &deviceInfoData))
- {
- Display2(TEXT("open deivce info failed %u for device: %s"), GetLastError(), (LPCTSTR)DevName);
- goto Error;
- }
- DWORD dwDevType = 0;
- DWORD dwRequired = 0;
- DWORD dwSize = 0;
- if (SetupDiGetDeviceRegistryProperty(
- hardwareDeviceInfo, &deviceInfoData, SPDRP_DEVTYPE, NULL, NULL, 0, &dwRequired)) {
- // we should not be successful at this point, so this call succeeding
- // is an error condition
- Display2(TEXT("in GetDeviceRegistryProperty() : call SetupDiGetDeviceRegistryProperty did not fail? (%x)"),
- GetLastError());
- goto Error;
- }
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER && dwRequired > 0)
- {
- LPBYTE btData = static_cast<PBYTE>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
- sizeof(BYTE)*dwRequired));
- dwSize = dwRequired;
- if (!SetupDiGetDeviceRegistryProperty(
- hardwareDeviceInfo, &deviceInfoData, SPDRP_DEVTYPE, NULL, btData, dwSize, &dwRequired)) {
- HeapFree(GetProcessHeap(), 0, btData);
- btData = NULL;
- DWORD dwError = GetLastError();
- Display2(TEXT("device type is not available for device: %s, GLE=%u"),
- (LPCTSTR)DevName, GetLastError());
- if (dwError != ERROR_INVALID_DATA)
- {
- goto Error;
- }
- } else {
- dwDevType = *reinterpret_cast<PDWORD>(btData);
- if (dwDevType == FILE_DEVICE_MOUSE)
- {
- *pNeedOpen = TRUE;
-
- } else if (dwDevType == FILE_DEVICE_KEYBOARD)
- {
- *pNeedOpen = TRUE;
- }
- }
- HeapFree(GetProcessHeap(), 0, btData);
- btData = NULL;
- }
- SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
- return TRUE;
- Error:
- SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
- return FALSE;
- }
- BOOL HandlePowerBroadcast(HWND hWnd, WPARAM wParam, ::LPARAM lParam)
- {
- BOOL fRet = TRUE;
- UNREFERENCED_PARAMETER(hWnd);
- UNREFERENCED_PARAMETER(lParam);
- switch (wParam)
- {
- case PBT_APMQUERYSTANDBY:
- Display2(TEXT("PBT_APMQUERYSTANDBY"));
- break;
- case PBT_APMQUERYSUSPEND:
- Display2(TEXT("PBT_APMQUERYSUSPEND"));
- break;
- case PBT_APMSTANDBY:
- Display2(TEXT("PBT_APMSTANDBY"));
- break;
- case PBT_APMSUSPEND:
- Display2(TEXT("PBT_APMSUSPEND"));
- break;
- case PBT_APMQUERYSTANDBYFAILED:
- Display2(TEXT("PBT_APMQUERYSTANDBYFAILED"));
- break;
- case PBT_APMRESUMESTANDBY:
- Display2(TEXT("PBT_APMRESUMESTANDBY"));
- break;
- case PBT_APMQUERYSUSPENDFAILED:
- Display2(TEXT("PBT_APMQUERYSUSPENDFAILED"));
- break;
- case PBT_APMRESUMESUSPEND:
- Display2(TEXT("PBT_APMRESUMESUSPEND"));
- break;
- case PBT_APMBATTERYLOW:
- Display2(TEXT("PBT_APMBATTERYLOW"));
- break;
- case PBT_APMOEMEVENT:
- Display2(TEXT("PBT_APMOEMEVENT"));
- break;
- case PBT_APMRESUMEAUTOMATIC:
- Display2(TEXT("PBT_APMRESUMEAUTOMATIC"));
- break;
- case PBT_APMRESUMECRITICAL:
- Display2(TEXT("PBT_APMRESUMECRITICAL"));
- break;
- case PBT_APMPOWERSTATUSCHANGE:
- Display2(TEXT("PBT_APMPOWERSTATUSCHANGE"));
- break;
- default:
- Display2(TEXT("Default"));
- break;
- }
- return fRet;
- }
- void OnMonitorChange(int currentCoun)
- {
- if(pMntFsm != NULL)
- {
- pMntFsm->BroadMonitorNumChange(currentCoun);
- }
- }
- void OnDevListChange(BOOL fIncreased)
- {
- UNREFERENCED_PARAMETER(fIncreased);
- BOOL kbd = FALSE, mou = FALSE;
- USHORT mouCnt, kbdCnt;
-
- gbMouAspire = gbKbdAspire = FALSE;
- if(IsDevDeficient(mou, kbd, &mouCnt, &kbdCnt)) {
- if(mou && mouCnt > 0) {
- gbMouAspire = TRUE;
- }
- if(kbd && kbdCnt > 0) {
- gbKbdAspire = TRUE;
- }
- }
- if(fIncreased && pMntFsm && (gbKbdAspire || gbMouAspire)) {
- pMntFsm->RequireInstallDrv(gbKbdAspire, gbMouAspire);
- }
- }
- BOOL DevMnt_IsNeedInstall(
- BOOL& bMouRequired,
- BOOL& bKbdRequired,
- USHORT* pUsMouCount,
- USHORT* pUsKbdCount)
- {
- return IsDevDeficient(bMouRequired, bKbdRequired, pUsMouCount, pUsKbdCount);
- }
- CRvcDevMnt::CRvcDevMnt(void)
- {
- }
- CRvcDevMnt::~CRvcDevMnt(void)
- {
- }
|