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