deviceManagerHandler.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "main.h"
  2. #include "./deviceManagerHandler.h"
  3. DeviceManagerHandler::DeviceManagerHandler()
  4. : ref(1), relayWindow(NULL)
  5. {
  6. }
  7. DeviceManagerHandler::~DeviceManagerHandler()
  8. {
  9. }
  10. HRESULT DeviceManagerHandler::CreateInstance(DeviceManagerHandler **instance)
  11. {
  12. if (NULL == instance)
  13. return E_POINTER;
  14. *instance = new DeviceManagerHandler();
  15. if (NULL == *instance)
  16. return E_OUTOFMEMORY;
  17. return S_OK;
  18. }
  19. size_t DeviceManagerHandler::AddRef()
  20. {
  21. return InterlockedIncrement((LONG*)&ref);
  22. }
  23. size_t DeviceManagerHandler::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 DeviceManagerHandler::QueryInterface(GUID interface_guid, void **object)
  33. {
  34. if (NULL == object) return E_POINTER;
  35. if (IsEqualIID(interface_guid, IFC_DeviceManagerEvent))
  36. *object = static_cast<ifc_devicemanagerevent*>(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 DeviceManagerHandler::TypeAdded(api_devicemanager *manager, ifc_devicetype *type)
  48. {
  49. if (NULL != relayWindow)
  50. EVENTRELAY_NOTIFY_TYPE(relayWindow, type, Event_TypeRegistered);
  51. }
  52. void DeviceManagerHandler::TypeRemoved(api_devicemanager *manager, ifc_devicetype *type)
  53. {
  54. if (NULL != relayWindow)
  55. EVENTRELAY_NOTIFY_TYPE(relayWindow, type, Event_TypeUnregistered);
  56. }
  57. void DeviceManagerHandler::ConnectionAdded(api_devicemanager *manager, ifc_deviceconnection *connection)
  58. {
  59. if (NULL != relayWindow)
  60. EVENTRELAY_NOTIFY_CONNECTION(relayWindow, connection, Event_ConnectionRegistered);
  61. }
  62. void DeviceManagerHandler::ConnectionRemoved(api_devicemanager *manager, ifc_deviceconnection *connection)
  63. {
  64. if (NULL != relayWindow)
  65. EVENTRELAY_NOTIFY_CONNECTION(relayWindow, connection, Event_ConnectionUnregistered);
  66. }
  67. void DeviceManagerHandler::CommandAdded(api_devicemanager *manager, ifc_devicecommand *command)
  68. {
  69. if (NULL != relayWindow)
  70. EVENTRELAY_NOTIFY_COMMAND(relayWindow, command, Event_CommandRegistered);
  71. }
  72. void DeviceManagerHandler::CommandRemoved(api_devicemanager *manager, ifc_devicecommand *command)
  73. {
  74. if (NULL != relayWindow)
  75. EVENTRELAY_NOTIFY_COMMAND(relayWindow, command, Event_CommandUnregistered);
  76. }
  77. void DeviceManagerHandler::DeviceAdded(api_devicemanager *manager, ifc_device *device)
  78. {
  79. if (NULL != relayWindow)
  80. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceAdded);
  81. }
  82. void DeviceManagerHandler::DeviceRemoved(api_devicemanager *manager, ifc_device *device)
  83. {
  84. if (NULL != relayWindow)
  85. EVENTRELAY_NOTIFY_DEVICE(relayWindow, device, Event_DeviceRemoved);
  86. }
  87. void DeviceManagerHandler::DiscoveryStarted(api_devicemanager *manager)
  88. {
  89. if (NULL != relayWindow)
  90. EVENTRELAY_NOTIFY_DISCOVERY(relayWindow, manager, Event_DiscoveryStarted);
  91. }
  92. void DeviceManagerHandler::DiscoveryFinished(api_devicemanager *manager)
  93. {
  94. if (NULL != relayWindow)
  95. EVENTRELAY_NOTIFY_DISCOVERY(relayWindow, manager, Event_DiscoveryFinished);
  96. }
  97. HRESULT DeviceManagerHandler::SetRelayWindow(HWND hwnd)
  98. {
  99. relayWindow = hwnd;
  100. return S_OK;
  101. }
  102. HRESULT DeviceManagerHandler::Advise(api_devicemanager *manager)
  103. {
  104. HRESULT hr;
  105. if (NULL == manager)
  106. return E_INVALIDARG;
  107. hr = manager->Advise(this);
  108. if (FAILED(hr))
  109. return hr;
  110. return hr;
  111. }
  112. HRESULT DeviceManagerHandler::Unadvise(api_devicemanager *manager)
  113. {
  114. HRESULT hr;
  115. if (NULL == manager)
  116. return E_INVALIDARG;
  117. hr = manager->Unadvise(this);
  118. if (FAILED(hr))
  119. return hr;
  120. return hr;
  121. }
  122. #define CBCLASS DeviceManagerHandler
  123. START_DISPATCH;
  124. CB(ADDREF, AddRef)
  125. CB(RELEASE, Release)
  126. CB(QUERYINTERFACE, QueryInterface)
  127. VCB(API_TYPEADDED, TypeAdded)
  128. VCB(API_TYPEREMOVED, TypeRemoved)
  129. VCB(API_CONNECTIONADDED, ConnectionAdded)
  130. VCB(API_CONNECTIONREMOVED, ConnectionRemoved)
  131. VCB(API_COMMANDADDED, CommandAdded)
  132. VCB(API_COMMANDREMOVED, CommandRemoved)
  133. VCB(API_DEVICEADDED, DeviceAdded)
  134. VCB(API_DEVICEREMOVED, DeviceRemoved)
  135. VCB(API_DISCOVERYSTARTED, DiscoveryStarted)
  136. VCB(API_DISCOVERYFINISHED, DiscoveryFinished)
  137. END_DISPATCH;
  138. #undef CBCLASS