1
0

Progress.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "main.h"
  2. #include "resource.h"
  3. #include"api__ml_rg.h"
  4. #include <strsafe.h>
  5. struct Progress
  6. {
  7. Progress()
  8. {
  9. processedFiles = 0;
  10. currentBytes = 0;
  11. totalBytes = 0;
  12. activeHWND = 0;
  13. threadHandle = 0;
  14. openDialogs = 0;
  15. done = false;
  16. killSwitch = 0;
  17. }
  18. size_t processedFiles;
  19. uint64_t currentBytes;
  20. uint32_t totalBytes;
  21. WorkQueue activeQueue;
  22. HWND activeHWND;
  23. HANDLE threadHandle;
  24. size_t openDialogs;
  25. bool done;
  26. int killSwitch;
  27. };
  28. DWORD WINAPI ThreadProc(void *param)
  29. {
  30. Progress *progress = (Progress *)param;
  31. ProgressCallback callback(progress->activeHWND);
  32. progress->activeQueue.Calculate(&callback, &progress->killSwitch);
  33. PostMessage(progress->activeHWND, WM_USER + 2, 0, 0);
  34. return 0;
  35. }
  36. void getViewport(RECT *r, HWND wnd, int full, RECT *sr)
  37. {
  38. POINT *p = NULL;
  39. if (p || sr || wnd)
  40. {
  41. HMONITOR hm = NULL;
  42. if (sr)
  43. hm = MonitorFromRect(sr, MONITOR_DEFAULTTONEAREST);
  44. else if (wnd)
  45. hm = MonitorFromWindow(wnd, MONITOR_DEFAULTTONEAREST);
  46. else if (p)
  47. hm = MonitorFromPoint(*p, MONITOR_DEFAULTTONEAREST);
  48. if (hm)
  49. {
  50. MONITORINFOEXW mi;
  51. memset(&mi, 0, sizeof(mi));
  52. mi.cbSize = sizeof(mi);
  53. if (GetMonitorInfoW(hm, &mi))
  54. {
  55. if (!full)
  56. *r = mi.rcWork;
  57. else
  58. *r = mi.rcMonitor;
  59. return ;
  60. }
  61. }
  62. }
  63. if (full)
  64. { // this might be borked =)
  65. r->top = r->left = 0;
  66. r->right = GetSystemMetrics(SM_CXSCREEN);
  67. r->bottom = GetSystemMetrics(SM_CYSCREEN);
  68. }
  69. else
  70. {
  71. SystemParametersInfoW(SPI_GETWORKAREA, 0, r, 0);
  72. }
  73. }
  74. BOOL windowOffScreen(HWND hwnd, POINT pt)
  75. {
  76. RECT r = {0}, wnd = {0}, sr = {0};
  77. GetWindowRect(hwnd, &wnd);
  78. sr.left = pt.x;
  79. sr.top = pt.y;
  80. sr.right = sr.left + (wnd.right - wnd.left);
  81. sr.bottom = sr.top + (wnd.bottom - wnd.top);
  82. getViewport(&r, hwnd, 0, &sr);
  83. return !PtInRect(&r, pt);
  84. }
  85. void SaveWindowPos(HWND hwnd)
  86. {
  87. RECT rect = {0};
  88. GetWindowRect(hwnd, &rect);
  89. char buf[16] = {0};
  90. StringCchPrintfA(buf, 16, "%d", rect.left);
  91. WritePrivateProfileStringA("ml_rg", "prog_x", buf, iniFile);
  92. StringCchPrintfA(buf, 16, "%d", rect.top);
  93. WritePrivateProfileStringA("ml_rg", "prog_y", buf, iniFile);
  94. }
  95. INT_PTR WINAPI ReplayGainProgressProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  96. {
  97. switch (msg)
  98. {
  99. case WM_INITDIALOG:
  100. {
  101. Progress *progress = (Progress *)lParam;
  102. progress->killSwitch = 0;
  103. progress->done = false;
  104. progress->openDialogs = 0;
  105. progress->processedFiles = 0;
  106. progress->activeHWND = hwndDlg;
  107. wchar_t dummy[64] = {0};
  108. StringCchPrintfW(dummy, 64, WASABI_API_LNGSTRINGW(IDS_1_OF_X_FILES), progress->activeQueue.totalFiles);
  109. SetDlgItemTextW(hwndDlg, IDC_PROGRESS_FILES, dummy);
  110. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)progress);
  111. DWORD threadId;
  112. progress->threadHandle = CreateThread(NULL, 0, ThreadProc, (void *)progress, CREATE_SUSPENDED, &threadId);
  113. SetThreadPriority(progress->threadHandle, THREAD_PRIORITY_IDLE);
  114. ResumeThread(progress->threadHandle);
  115. POINT pt = {(LONG)GetPrivateProfileIntA("ml_rg", "prog_x", -1, iniFile),
  116. (LONG)GetPrivateProfileIntA("ml_rg", "prog_y", -1, iniFile)};
  117. if (!windowOffScreen(hwndDlg, pt))
  118. SetWindowPos(hwndDlg, HWND_TOP, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOSENDCHANGING);
  119. else
  120. ShowWindow(hwndDlg, SW_SHOW);
  121. }
  122. break;
  123. case WM_DESTROY:
  124. {
  125. SaveWindowPos(hwndDlg);
  126. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  127. CloseHandle(progress->threadHandle);
  128. progress->activeHWND = 0;
  129. delete progress;
  130. }
  131. break;
  132. case WM_USER: // file finished
  133. {
  134. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  135. progress->processedFiles++;
  136. if (progress->processedFiles + 1 > progress->activeQueue.totalFiles)
  137. SetDlgItemTextW(hwndDlg, IDC_PROGRESS_FILES, WASABI_API_LNGSTRINGW(IDS_FINISHED));
  138. else
  139. {
  140. wchar_t dummy[64] = {0};
  141. StringCchPrintfW(dummy, 64,
  142. WASABI_API_LNGSTRINGW(IDS_X_OF_X_FILES),
  143. progress->processedFiles + 1, progress->activeQueue.totalFiles);
  144. SetDlgItemTextW(hwndDlg, IDC_PROGRESS_FILES, dummy);
  145. }
  146. }
  147. break;
  148. case WM_USER + 1: // album done
  149. {
  150. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  151. WorkQueue::RGWorkQueue *queue = (WorkQueue::RGWorkQueue *)lParam;
  152. SaveWindowPos(hwndDlg);
  153. if (config_ask && config_ask_each_album)
  154. {
  155. progress->openDialogs++;
  156. DoResults(*queue);
  157. progress->openDialogs--;
  158. if (!progress->openDialogs && progress->done)
  159. DestroyWindow(hwndDlg);
  160. }
  161. else if (config_ask == 0)
  162. {
  163. WriteAlbum(*queue);
  164. }
  165. }
  166. break;
  167. case WM_USER + 2: // all tracks done
  168. {
  169. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  170. ShowWindow(hwndDlg, SW_HIDE);
  171. SaveWindowPos(hwndDlg);
  172. if (config_ask && config_ask_each_album == 0)
  173. {
  174. DoResults(progress->activeQueue);
  175. }
  176. progress->killSwitch = 1;
  177. WaitForSingleObject(progress->threadHandle, INFINITE);
  178. progress->done = true;
  179. if (!progress->openDialogs)
  180. DestroyWindow(hwndDlg);
  181. }
  182. break;
  183. case WM_USER + 3: // total bytes of current file
  184. {
  185. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  186. progress->currentBytes = 0;
  187. progress->totalBytes = (uint32_t)lParam;
  188. if (progress->totalBytes == 0)
  189. {
  190. SetDlgItemTextW(hwndDlg, IDC_FILE_PROGRESS, WASABI_API_LNGSTRINGW(IDS_PROCESSING));
  191. }
  192. else
  193. {
  194. wchar_t dummy[64] = {0};
  195. StringCchPrintfW(dummy, 64, L"%u%%", (progress->currentBytes * 100) / progress->totalBytes);
  196. SetDlgItemTextW(hwndDlg, IDC_FILE_PROGRESS, dummy);
  197. }
  198. }
  199. break;
  200. case WM_USER + 4: // more bytes read
  201. {
  202. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  203. progress->currentBytes += lParam;
  204. if (progress->totalBytes == 0)
  205. {
  206. SetDlgItemTextW(hwndDlg, IDC_FILE_PROGRESS, WASABI_API_LNGSTRINGW(IDS_PROCESSING));
  207. }
  208. else
  209. {
  210. wchar_t dummy[64] = {0};
  211. StringCchPrintfW(dummy, 64, L"%u%%", (progress->currentBytes * 100) / progress->totalBytes);
  212. SetDlgItemTextW(hwndDlg, IDC_FILE_PROGRESS, dummy);
  213. }
  214. }
  215. break;
  216. case WM_COMMAND:
  217. switch (LOWORD(wParam))
  218. {
  219. case IDCANCEL:
  220. {
  221. Progress *progress = (Progress *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  222. progress->killSwitch = 1;
  223. break;
  224. }
  225. }
  226. break;
  227. }
  228. return 0;
  229. }
  230. void DoProgress(WorkQueue &workQueue)
  231. {
  232. Progress *progress = new Progress;
  233. progress->activeQueue = workQueue; // this is a huge slow copy, but I don't care at the moment
  234. WASABI_API_CREATEDIALOGPARAMW(IDD_PROGRESS, GetDialogBoxParent(), ReplayGainProgressProc, (LPARAM)progress);
  235. }
  236. HWND GetDialogBoxParent()
  237. {
  238. HWND parent = (HWND)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETDIALOGBOXPARENT);
  239. if (!parent || parent == (HWND)1)
  240. return plugin.hwndWinampParent;
  241. return parent;
  242. }