1
0

deviceHandler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "main.h"
  2. #include "./deviceHandler.h"
  3. DeviceHandler::DeviceHandler()
  4. : ref(1), relayWindow(NULL)
  5. {
  6. }
  7. DeviceHandler::~DeviceHandler()
  8. {
  9. }
  10. HRESULT DeviceHandler::CreateInstance(DeviceHandler **instance)
  11. {
  12. if (NULL == instance)
  13. return E_POINTER;
  14. *instance = new DeviceHandler();
  15. if (NULL == *instance)
  16. return E_OUTOFMEMORY;
  17. return S_OK;
  18. }
  19. size_t DeviceHandler::AddRef()
  20. {
  21. return InterlockedIncrement((LONG*)&ref);
  22. }
  23. size_t DeviceHandler::Release()
  24. {
  25. if (0 == ref)
  26. return ref;
  27. LONG r = InterlockedDecrement((LONG*)&ref);
  28. if (0 == r)
  29. delete(this);
  30. return r;
  31. }
  32. int DeviceHandler::QueryInterface(GUID interface_guid, void **object)
  33. {
  34. if (NULL == object) return E_POINTER;
  35. if (IsEqualIID(interface_guid, IFC_DeviceEvent))
  36. *object = static_cast<ifc_deviceevent*>(this);
  37. else
  38. {
  39. *object = NULL;
  40. return E_NOINTERFACE;
  41. }
  42. if (NULL == *object)
  43. return E_UNEXPECTED;
  44. AddRef();
  45. return S_OK;
  46. }
  47. void DeviceHandler::IconChanged(ifc_device *device)
  48. {
  49. if (NULL != relayWindow)
  50. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceIconChanged);
  51. }
  52. void DeviceHandler::DisplayNameChanged(ifc_device *device, const wchar_t *displayName)
  53. {
  54. if (NULL != relayWindow)
  55. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceDisplayNameChanged);
  56. }
  57. void DeviceHandler::AttachmentChanged(ifc_device *device, BOOL attached)
  58. {
  59. if (NULL != relayWindow)
  60. {
  61. if (FALSE != attached)
  62. {
  63. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceAttached);
  64. }
  65. else
  66. {
  67. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceDetached);
  68. }
  69. }
  70. }
  71. void DeviceHandler::VisibilityChanged(ifc_device *device, BOOL visible)
  72. {
  73. if (NULL != relayWindow)
  74. {
  75. if (FALSE == visible)
  76. {
  77. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceHidden);
  78. }
  79. else
  80. {
  81. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceShown);
  82. }
  83. }
  84. }
  85. void DeviceHandler::TotalSpaceChanged(ifc_device *device, size_t space)
  86. {
  87. if (NULL != relayWindow)
  88. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceTotalSpaceChanged);
  89. }
  90. void DeviceHandler::UsedSpaceChanged(ifc_device *device, size_t space)
  91. {
  92. if (NULL != relayWindow)
  93. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceUsedSpaceChanged);
  94. }
  95. void DeviceHandler::CommandChanged(ifc_device *device)
  96. {
  97. if (NULL != relayWindow)
  98. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceCommandChanged);
  99. }
  100. void DeviceHandler::ActivityStarted(ifc_device *device, ifc_deviceactivity *activity)
  101. {
  102. if (NULL != relayWindow)
  103. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceActivityStarted);
  104. }
  105. void DeviceHandler::ActivityFinished(ifc_device *device, ifc_deviceactivity *activity)
  106. {
  107. if (NULL != relayWindow)
  108. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceActivityFinished);
  109. }
  110. void DeviceHandler::ActivityChanged(ifc_device *device, ifc_deviceactivity *activity)
  111. {
  112. if (NULL != relayWindow)
  113. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceActivityChanged);
  114. }
  115. void DeviceHandler::ModelChanged(ifc_device *device, const wchar_t *model)
  116. {
  117. if (NULL != relayWindow)
  118. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceModelChanged);
  119. }
  120. void DeviceHandler::StatusChanged(ifc_device *device, const wchar_t *status)
  121. {
  122. if (NULL != relayWindow)
  123. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceStatusChanged);
  124. }
  125. HRESULT DeviceHandler::SetRelayWindow(HWND hwnd)
  126. {
  127. relayWindow = hwnd;
  128. return S_OK;
  129. }
  130. HRESULT DeviceHandler::Advise(ifc_device *device)
  131. {
  132. HRESULT hr;
  133. if (NULL == device)
  134. return E_INVALIDARG;
  135. hr = device->Advise(this);
  136. if (FAILED(hr))
  137. return hr;
  138. return hr;
  139. }
  140. HRESULT DeviceHandler::Unadvise(ifc_device *device)
  141. {
  142. HRESULT hr;
  143. if (NULL == device)
  144. return E_INVALIDARG;
  145. hr = device->Unadvise(this);
  146. if (FAILED(hr))
  147. return hr;
  148. return hr;
  149. }
  150. #define CBCLASS DeviceHandler
  151. START_DISPATCH;
  152. CB(ADDREF, AddRef)
  153. CB(RELEASE, Release)
  154. CB(QUERYINTERFACE, QueryInterface)
  155. VCB(API_ICONCHANGED, IconChanged)
  156. VCB(API_DISPLAYNAMECHANGED, DisplayNameChanged)
  157. VCB(API_ATTACHMENTCHANGED, AttachmentChanged)
  158. VCB(API_VISIBILITYCHANGED, VisibilityChanged)
  159. VCB(API_TOTALSPACECHANGED, TotalSpaceChanged)
  160. VCB(API_USEDSPACECHANGED, UsedSpaceChanged)
  161. VCB(API_COMMANDCHANGED, CommandChanged)
  162. VCB(API_ACTIVITYSTARTED, ActivityStarted)
  163. VCB(API_ACTIVITYFINISHED, ActivityFinished)
  164. VCB(API_ACTIVITYCHANGED, ActivityChanged)
  165. VCB(API_MODELCHANGED, ModelChanged)
  166. VCB(API_STATUSCHANGED, StatusChanged)
  167. END_DISPATCH;
  168. #undef CBCLASS