-
Notifications
You must be signed in to change notification settings - Fork 19
/
DeviceManager.h
131 lines (119 loc) · 4.58 KB
/
DeviceManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* File: DeviceManager.h
*
* Copyright (c) 2010 Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
DEFINE_GUID(CLSID_CANCEL_AUTOPLAY, 0x66a32fe6, 0x229d, 0x427b, 0xa6, 0x8, 0xd2, 0x73, 0xf4, 0xc, 0x3, 0x4c);
//////////////////////////////////////////////////////////////////////
//
// DeviceManager
//
//////////////////////////////////////////////////////////////////////
class DeviceManager : public CWinThread
{
DECLARE_DYNCREATE(DeviceManager)
public:
//! \brief Device event types determined in DeviceManager::OnMsgDeviceEvent(WPARAM eventType, LPARAM msg).
typedef enum DevChangeEvent
{
UNKNOWN_EVT = 100, EVENT_KILL,
HUB_ARRIVAL_EVT, HUB_REMOVAL_EVT,
DEVICE_ARRIVAL_EVT, DEVICE_REMOVAL_EVT,
VOLUME_ARRIVAL_EVT, VOLUME_REMOVAL_EVT,
WMDM_DEVICE_ARRIVAL_EVT, WMDM_DEVICE_REMOVAL_EVT,
WMDM_MEDIA_ARRIVAL_EVT, WMDM_MEDIA_REMOVAL_EVT
};
DeviceManager();
~DeviceManager();
//! \brief Start the message thread.
//! \return Status.
//! \retval ERROR_SUCCESS No error occurred.
//! \retval GetLastError() result Error has occurred.
//! \pre Can not have been called previously.
//! \post Must call Close() to release resources.
//! \note Runs in the context of the Client thread.
//! \note Function holds Client thread until the DeviceManager thread has started.
unsigned int Open();
//! \brief Gracefully stop the thread.
//! \note Runs in the context of the Client thread.
void Close();
//! \brief Calls CQueryCancelAutoplay::SetCancelAutoPlay(bool rejectAutoPlay, LPCTSTR driveList)
//! \note Runs in the context of the Client thread.
HRESULT SetCancelAutoPlay(bool rejectAutoPlay, LPCTSTR driveList = NULL)
{
return _ICancelAutoPlayCallbackObject.SetCancelAutoPlay(rejectAutoPlay, driveList);
};
bool FindHidDevice(CHidDevice* pHidDevice, int timeout);
bool WaitForChange(DevChangeEvent eventType, int timeout);
CString GetEventString(DevChangeEvent eventType);
private:
//! \brief The purpose of this hidden window is just to pass WM_DEVICECHANGE messages to the DeviceManager thread.
//! \see DeviceManager::OnMsgDeviceEvent(WPARAM eventType, LPARAM msg)
class DevChangeWnd : public CWnd
{
public:
afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD_PTR dwData);
CString DrivesFromMask(ULONG UnitMask);
DECLARE_MESSAGE_MAP()
} _DevChangeWnd;
//! \brief Use this message in the DevChangeWnd::OnDeviceChange() to post messages to the DeviceManager thread.
//! \see DeviceManager::Close()
//! \see DeviceManager::OnMsgDeviceEvent(WPARAM eventType, LPARAM msg)
const static unsigned int WM_MSG_DEV_EVENT = WM_USER + 36;
//! \brief Processes WM_MSG_DEV_EVENT messages for the DeviceManager thread.
//! \see DeviceManager::Close()
//! \see DeviceManager::DevChangeWnd::OnDeviceChange(UINT nEventType,DWORD_PTR dwData)
virtual afx_msg void OnMsgDeviceEvent(WPARAM eventType, LPARAM desc);
//! \brief Event handle used to notify Client thread about a change in the DeviceManager thread.
//! \see DeviceManager::OnMsgDeviceEvent()
//! \see DeviceManager::FindDevice()
HANDLE _hChangeEvent;
// All this is private since Singleton holder is the only one who can create, copy or assign
// a DeviceManager class.
DeviceManager(const DeviceManager&);
DeviceManager& operator=(const DeviceManager&);
virtual BOOL InitInstance();
virtual int ExitInstance();
//! \brief Used by ~DeviceManager() to tell if DeviceManager::ExitInstance() was called.
//! \see DeviceManager::Open()
//! \see DeviceManager::Close()
bool _bStopped;
//! \brief Used by Open() to syncronize DeviceManager thread start.
//! \see DeviceManager::Open()
HANDLE _hStartEvent;
DevChangeEvent _lastEventType;
CString _eventString;
// Message Support
HDEVNOTIFY _hUsbDev;
HDEVNOTIFY _hUsbHub;
// AutoPlay support
class CQueryCancelAutoplay : public IQueryCancelAutoPlay
{
public:
// IUnknown interface
STDMETHODIMP QueryInterface(REFIID riid, void** ppv);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
// IQueryCancelAutoPlay interface
STDMETHODIMP AllowAutoPlay(LPCWSTR pszPath, DWORD dwContentType,
LPCWSTR pszLabel, DWORD dwSerialNumber);
CQueryCancelAutoplay()
: _cRef(1)
, _defaultDriveList(_T("DEFGHIJKLMNOPQRSTUVWXYZ"))
, _driveList(_T("DEFGHIJKLMNOPQRSTUVWXYZ"))
, _ROT(0)
, _isRegistered(0)
{};
~CQueryCancelAutoplay() { };
HRESULT SetCancelAutoPlay(bool rejectAutoPlay, LPCTSTR driveList);
private:
HRESULT Unregister();
ULONG _cRef;
CString _driveList;
DWORD _ROT;
bool _isRegistered;
const CString _defaultDriveList;
} _ICancelAutoPlayCallbackObject;
DECLARE_MESSAGE_MAP()
};