deviceActivity.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #include "main.h"
  2. #include "./deviceActivity.h"
  3. #include <strsafe.h>
  4. typedef struct DeviceActivityThreadParam
  5. {
  6. DeviceActivity *activity;
  7. unsigned int duration;
  8. unsigned int interval;
  9. HANDLE readyEvent;
  10. }DeviceActivityThreadParam;
  11. DeviceActivity::DeviceActivity(DeviceActivityFlags flags,
  12. DeviceActivityCallback startCb, DeviceActivityCallback finishCb,
  13. DeviceActivityProgressCallback progressCb, void *user)
  14. : ref(1), displayName(NULL), status(NULL), activityThread(FALSE), cancelEvent(NULL), progress(0)
  15. {
  16. InitializeCriticalSection(&lock);
  17. this->flags = flags;
  18. callbackStart = startCb;
  19. callbackFinish = finishCb;
  20. callbackProgress = progressCb;
  21. this->user = user;
  22. }
  23. DeviceActivity::~DeviceActivity()
  24. {
  25. Stop();
  26. String_Free(displayName);
  27. String_Free(status);
  28. DeleteCriticalSection(&lock);
  29. }
  30. HRESULT DeviceActivity::CreateInstance(DeviceActivityFlags flags,
  31. DeviceActivityCallback startCb, DeviceActivityCallback finishCb,
  32. DeviceActivityProgressCallback progressCb, void *user,
  33. DeviceActivity **instance)
  34. {
  35. DeviceActivity *self;
  36. if (NULL == instance)
  37. return E_POINTER;
  38. *instance = NULL;
  39. self = new DeviceActivity(flags, startCb, finishCb, progressCb, user);
  40. if (NULL == self)
  41. return E_OUTOFMEMORY;
  42. *instance = self;
  43. return S_OK;
  44. }
  45. size_t DeviceActivity::AddRef()
  46. {
  47. return InterlockedIncrement((LONG*)&ref);
  48. }
  49. size_t DeviceActivity::Release()
  50. {
  51. if (0 == ref)
  52. return ref;
  53. LONG r = InterlockedDecrement((LONG*)&ref);
  54. if (0 == r)
  55. delete(this);
  56. return r;
  57. }
  58. int DeviceActivity::QueryInterface(GUID interface_guid, void **object)
  59. {
  60. if (NULL == object)
  61. return E_POINTER;
  62. if (IsEqualIID(interface_guid, IFC_DeviceActivity))
  63. *object = static_cast<ifc_deviceactivity*>(this);
  64. else
  65. {
  66. *object = NULL;
  67. return E_NOINTERFACE;
  68. }
  69. if (NULL == *object)
  70. return E_UNEXPECTED;
  71. AddRef();
  72. return S_OK;
  73. }
  74. void DeviceActivity::Lock()
  75. {
  76. EnterCriticalSection(&lock);
  77. }
  78. void DeviceActivity::Unlock()
  79. {
  80. LeaveCriticalSection(&lock);
  81. }
  82. BOOL DeviceActivity::GetActive()
  83. {
  84. BOOL running;
  85. Lock();
  86. running = (NULL != activityThread);
  87. Unlock();
  88. return running;
  89. }
  90. BOOL DeviceActivity::GetCancelable()
  91. {
  92. BOOL cancelable;
  93. Lock();
  94. cancelable = (0 != (DeviceActivityFlag_Cancelable & flags));
  95. Unlock();
  96. return cancelable;
  97. }
  98. HRESULT DeviceActivity::GetProgress(unsigned int *percentCompleted)
  99. {
  100. if (NULL == percentCompleted)
  101. return E_POINTER;
  102. Lock();
  103. *percentCompleted = progress;
  104. Unlock();
  105. return S_OK;
  106. }
  107. HRESULT DeviceActivity::GetDisplayName(wchar_t *buffer, size_t bufferMax)
  108. {
  109. HRESULT hr;
  110. if (NULL == buffer)
  111. return E_POINTER;
  112. Lock();
  113. if (0 == String_CopyTo(buffer, displayName, bufferMax) &&
  114. FALSE == IS_STRING_EMPTY(displayName))
  115. {
  116. hr = E_FAIL;
  117. }
  118. else
  119. hr = S_OK;
  120. Unlock();
  121. return hr;
  122. }
  123. HRESULT DeviceActivity::GetStatus(wchar_t *buffer, size_t bufferMax)
  124. {
  125. HRESULT hr;
  126. if (NULL == buffer)
  127. return E_POINTER;
  128. Lock();
  129. if (0 == String_CopyTo(buffer, status, bufferMax) &&
  130. FALSE == IS_STRING_EMPTY(status))
  131. {
  132. hr = E_FAIL;
  133. }
  134. else
  135. hr = S_OK;
  136. Unlock();
  137. return hr;
  138. }
  139. HRESULT DeviceActivity::Cancel(HWND hostWindow)
  140. {
  141. HRESULT hr;
  142. Lock();
  143. if (0 == (DeviceActivityFlag_Cancelable & flags))
  144. hr = E_NOTIMPL;
  145. else
  146. hr = E_FAIL;
  147. Unlock();
  148. return hr;
  149. }
  150. HRESULT DeviceActivity::Start(unsigned int duration, unsigned int interval)
  151. {
  152. HRESULT hr;
  153. Lock();
  154. if (NULL != activityThread)
  155. hr = E_PENDING;
  156. else
  157. {
  158. hr = S_OK;
  159. if (NULL == cancelEvent)
  160. {
  161. cancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  162. if (NULL == cancelEvent)
  163. hr = E_FAIL;
  164. }
  165. if (SUCCEEDED(hr))
  166. {
  167. DeviceActivityThreadParam param;
  168. param.activity = this;
  169. param.duration = duration;
  170. param.interval = interval;
  171. param.readyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  172. if (NULL == param.readyEvent)
  173. hr = E_FAIL;
  174. else
  175. {
  176. DWORD threadId;
  177. activityThread = CreateThread(NULL, 0, DeviceActivity_ActivityThreadStarter,
  178. &param, 0, &threadId);
  179. if (NULL == activityThread)
  180. hr = E_FAIL;
  181. else
  182. WaitForSingleObject(param.readyEvent, INFINITE);
  183. CloseHandle(param.readyEvent);
  184. }
  185. }
  186. if (FAILED(hr))
  187. Stop();
  188. }
  189. Unlock();
  190. return hr;
  191. }
  192. HRESULT DeviceActivity::Stop()
  193. {
  194. HRESULT hr;
  195. HANDLE threadHandle, eventHandle;
  196. Lock();
  197. threadHandle = activityThread;
  198. eventHandle = cancelEvent;
  199. activityThread = NULL;
  200. cancelEvent = NULL;
  201. Unlock();
  202. if (NULL != threadHandle)
  203. {
  204. if (NULL != eventHandle)
  205. SetEvent(eventHandle);
  206. WaitForSingleObject(threadHandle, INFINITE);
  207. CloseHandle(threadHandle);
  208. hr = S_OK;
  209. }
  210. else
  211. hr = S_FALSE;
  212. if (NULL != eventHandle)
  213. CloseHandle(eventHandle);
  214. return hr;
  215. }
  216. HRESULT DeviceActivity::SetDisplayName(const wchar_t *name)
  217. {
  218. HRESULT hr;
  219. Lock();
  220. if (NULL == name && NULL == displayName)
  221. hr = S_FALSE;
  222. else
  223. {
  224. if (NULL != displayName &&
  225. CSTR_EQUAL == CompareString(LOCALE_USER_DEFAULT, 0, name, -1, displayName, -1))
  226. {
  227. hr = S_FALSE;
  228. }
  229. else
  230. {
  231. wchar_t *string;
  232. string = String_Duplicate(name);
  233. if (NULL == string && NULL != name)
  234. hr = E_FAIL;
  235. else
  236. {
  237. String_Free(displayName);
  238. displayName = string;
  239. hr = S_OK;
  240. }
  241. }
  242. }
  243. Unlock();
  244. return hr;
  245. }
  246. HRESULT DeviceActivity::SetStatus(const wchar_t *newStatus)
  247. {
  248. HRESULT hr;
  249. if (NULL == newStatus && NULL == status)
  250. return S_FALSE;
  251. Lock();
  252. if (NULL != status &&
  253. CSTR_EQUAL == CompareString(LOCALE_USER_DEFAULT, 0, newStatus, -1, status, -1))
  254. {
  255. hr = S_FALSE;
  256. }
  257. else
  258. {
  259. wchar_t *string;
  260. string = String_Duplicate(newStatus);
  261. if (NULL == string && NULL != newStatus)
  262. hr = E_FAIL;
  263. else
  264. {
  265. String_Free(status);
  266. status = string;
  267. hr = S_OK;
  268. }
  269. }
  270. Unlock();
  271. return hr;
  272. }
  273. HRESULT DeviceActivity::SetUser(void *data)
  274. {
  275. Lock();
  276. user = data;
  277. Unlock();
  278. return S_OK;
  279. }
  280. HRESULT DeviceActivity::GetUser(void **data)
  281. {
  282. if (NULL == data)
  283. return E_POINTER;
  284. Lock();
  285. *data = user;
  286. Unlock();
  287. return S_OK;
  288. }
  289. DWORD DeviceActivity::ActivityThread(unsigned int duration, unsigned int interval)
  290. {
  291. DWORD waitResult, waitTime;
  292. HANDLE cancelEventCopy;
  293. unsigned int position;
  294. if (interval > duration)
  295. interval = duration;
  296. position = 0;
  297. Lock();
  298. progress = 0;
  299. if (NULL == cancelEvent ||
  300. 0 == DuplicateHandle(GetCurrentProcess(), cancelEvent, GetCurrentProcess(),
  301. &cancelEventCopy, 0, FALSE, DUPLICATE_SAME_ACCESS))
  302. {
  303. cancelEventCopy = NULL;
  304. }
  305. Unlock();
  306. if(NULL == cancelEventCopy)
  307. return -3;
  308. Lock();
  309. if (NULL != callbackStart)
  310. callbackStart(this);
  311. if (NULL != callbackProgress)
  312. callbackProgress(this, position, duration);
  313. Unlock();
  314. for(;;)
  315. {
  316. waitTime = interval;
  317. if ((position + waitTime) > duration)
  318. waitTime = duration - position;
  319. waitResult = WaitForSingleObject(cancelEventCopy, waitTime);
  320. if (WAIT_TIMEOUT == waitResult)
  321. {
  322. position += waitTime;
  323. Lock();
  324. if (duration != 0)
  325. {
  326. progress = 100 * position / duration;
  327. if (progress > 100)
  328. progress = 100;
  329. }
  330. else
  331. progress = 100;
  332. if (NULL != callbackProgress)
  333. callbackProgress(this, position, duration);
  334. Unlock();
  335. if (position >= duration)
  336. break;
  337. }
  338. else
  339. break;
  340. }
  341. AddRef();
  342. Lock();
  343. if (NULL != activityThread)
  344. {
  345. CloseHandle(activityThread);
  346. activityThread = NULL;
  347. }
  348. if (NULL != cancelEvent)
  349. {
  350. CloseHandle(cancelEvent);
  351. cancelEvent = NULL;
  352. }
  353. if (NULL != callbackFinish)
  354. callbackFinish(this);
  355. Unlock();
  356. Release();
  357. return 0;
  358. }
  359. static DWORD CALLBACK
  360. DeviceActivity_ActivityThreadStarter(void *user)
  361. {
  362. DeviceActivityThreadParam *param;
  363. DeviceActivity *activity;
  364. unsigned int duration, interval;
  365. DWORD result;
  366. param = (DeviceActivityThreadParam*)user;
  367. activity = param->activity;
  368. duration = param->duration;
  369. interval = param->interval;
  370. if (NULL != param->readyEvent)
  371. SetEvent(param->readyEvent);
  372. if (NULL != activity)
  373. result = activity->ActivityThread(duration, interval);
  374. else
  375. result = -2;
  376. return result;
  377. }
  378. #define CBCLASS DeviceActivity
  379. START_DISPATCH;
  380. CB(ADDREF, AddRef)
  381. CB(RELEASE, Release)
  382. CB(QUERYINTERFACE, QueryInterface)
  383. CB(API_GETACTIVE, GetActive)
  384. CB(API_GETCANCELABLE, GetCancelable)
  385. CB(API_GETPROGRESS, GetProgress)
  386. CB(API_GETDISPLAYNAME, GetDisplayName)
  387. CB(API_GETSTATUS, GetStatus)
  388. CB(API_CANCEL, Cancel)
  389. END_DISPATCH;
  390. #undef CBCLASS