DeviceCommands.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "main.h"
  2. #include "DeviceCommands.h"
  3. #include <strsafe.h>
  4. PortableCommand::PortableCommand(const char *name, int title, int description)
  5. : name(name), title(title), description(description)
  6. {
  7. }
  8. const char *PortableCommand::GetName()
  9. {
  10. return name;
  11. }
  12. HRESULT PortableCommand::GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
  13. {
  14. return E_NOTIMPL;
  15. }
  16. HRESULT PortableCommand::GetDisplayName(wchar_t *buffer, size_t bufferSize)
  17. {
  18. if (NULL == buffer)
  19. return E_POINTER;
  20. WASABI_API_LNGSTRINGW_BUF(title, buffer, bufferSize);
  21. return S_OK;
  22. }
  23. HRESULT PortableCommand::GetDescription(wchar_t *buffer, size_t bufferSize)
  24. {
  25. if (NULL == buffer)
  26. return E_POINTER;
  27. WASABI_API_LNGSTRINGW_BUF(description, buffer, bufferSize);
  28. return S_OK;
  29. }
  30. #define CBCLASS PortableCommand
  31. START_DISPATCH;
  32. CB(API_GETNAME, GetName);
  33. CB(API_GETICON, GetIcon);
  34. CB(API_GETDISPLAYNAME, GetDisplayName);
  35. CB(API_GETDESCRIPTION, GetDescription);
  36. END_DISPATCH;
  37. #undef CBCLASS
  38. BOOL SetDeviceCommandInfo(DeviceCommandInfo *info, const char *name, DeviceCommandFlags flags)
  39. {
  40. if (NULL == info)
  41. return FALSE;
  42. info->name = name;
  43. info->flags = flags;
  44. return TRUE;
  45. }
  46. /* -------------- */
  47. DeviceCommand::DeviceCommand(const char *name, DeviceCommandFlags flags)
  48. : name(name), flags(flags)
  49. {
  50. }
  51. DeviceCommand::DeviceCommand(const DeviceCommandInfo *commandInfo)
  52. : name(commandInfo->name), flags(commandInfo->flags)
  53. {
  54. }
  55. const char *DeviceCommand::GetName()
  56. {
  57. return name;
  58. }
  59. HRESULT DeviceCommand::GetFlags(DeviceCommandFlags *flags)
  60. {
  61. *flags = this->flags;
  62. return 0;
  63. }
  64. #define CBCLASS DeviceCommand
  65. START_DISPATCH;
  66. REFERENCE_COUNTED;
  67. CB(API_GETNAME, GetName);
  68. CB(API_GETFLAGS, GetFlags);
  69. END_DISPATCH;
  70. #undef CBCLASS
  71. DeviceCommandEnumerator::DeviceCommandEnumerator(const DeviceCommandInfo *commandInfoList, size_t listSize)
  72. : position(0), commands(NULL), count(0)
  73. {
  74. if (NULL != commandInfoList &&
  75. 0 != listSize)
  76. {
  77. commands = (DeviceCommand**)calloc(listSize, sizeof(DeviceCommand*));
  78. if (NULL != commands)
  79. {
  80. for(count = 0; count < listSize; count++)
  81. {
  82. commands[count] = new DeviceCommand(&commandInfoList[count]);
  83. }
  84. }
  85. }
  86. }
  87. DeviceCommandEnumerator::~DeviceCommandEnumerator()
  88. {
  89. if (NULL != commands)
  90. {
  91. while(count--)
  92. commands[count]->Release();
  93. free(commands);
  94. }
  95. }
  96. HRESULT DeviceCommandEnumerator::Next(ifc_devicesupportedcommand **buffer, size_t bufferMax, size_t *fetched)
  97. {
  98. size_t available, copied, index;
  99. DeviceCommand **source;
  100. if (NULL == buffer)
  101. return E_POINTER;
  102. if (0 == bufferMax)
  103. return E_INVALIDARG;
  104. if (position >= count)
  105. {
  106. if (NULL != fetched)
  107. *fetched = 0;
  108. return S_FALSE;
  109. }
  110. available = count - position;
  111. copied = ((available > bufferMax) ? bufferMax : available);
  112. source = commands + position;
  113. CopyMemory(buffer, source, copied * sizeof(ifc_devicesupportedcommand*));
  114. for(index = 0; index < copied; index++)
  115. buffer[index]->AddRef();
  116. position += copied;
  117. if (NULL != fetched)
  118. *fetched = copied;
  119. return (bufferMax == copied) ? S_OK : S_FALSE;
  120. }
  121. HRESULT DeviceCommandEnumerator::Reset(void)
  122. {
  123. position=0;
  124. return S_OK;
  125. }
  126. HRESULT DeviceCommandEnumerator::Skip(size_t count)
  127. {
  128. position += count;
  129. if (position > this->count)
  130. position = this->count;
  131. return (position < this->count) ? S_OK : S_FALSE;
  132. }
  133. HRESULT DeviceCommandEnumerator::GetCount(size_t *count)
  134. {
  135. if (NULL == count)
  136. return E_POINTER;
  137. *count = this->count;
  138. return S_OK;
  139. }
  140. #define CBCLASS DeviceCommandEnumerator
  141. START_DISPATCH;
  142. CB(API_NEXT, Next);
  143. CB(API_RESET, Reset);
  144. CB(API_SKIP, Skip);
  145. CB(API_GETCOUNT, GetCount);
  146. REFERENCE_COUNTED;
  147. END_DISPATCH;
  148. #undef CBCLASS