-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
1504 lines (1325 loc) · 45.2 KB
/
main.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <windowsx.h>
#include <shlwapi.h>
#include <winamp/in2.H>
#include <winamp/gen.h>
#include <winamp/wa_ipc.h>
#include <winamp/ipc_pe.h>
#include <gen_ml/ml.h>
#include <gen_ml/ml_ipc_0313.h>
#include <nu/AutoWide.h>
#include <nu/AutoChar.h>
#include <nu/AutoCharFn.h>
#include <nu/ServiceBuilder.h>
#include "resource.h"
#include "api.h"
#include "gen_waveseek.h"
#include "embedwnd.h"
#ifdef WACUP_BUILD
#include "../../loader/hook/get_api_service.h"
#include "../../loader/loader/paths.h"
#endif
#define PLUGIN_VERSION "2.3.1"
#ifdef GetPrivateProfileInt
#undef GetPrivateProfileInt
#define GetPrivateProfileInt(field,param) GetPrivateProfileIntW(INI_FILE_SECTION,field,param,ini_file)
#endif
#define WA_DLG_IMPLEMENT
#include <winamp/wa_dlg.h>
#include <strsafe.h>
#ifndef ID_PE_SCUP
#define ID_PE_SCUP 40289
#endif
#ifndef ID_PE_SCDOWN
#define ID_PE_SCDOWN 40290
#endif
// this is used to identify the skinned frame to allow for embedding/control by modern skins if needed
// {1C2F2C09-4F43-4CFF-9DE7-32E014638DFC}
static const GUID embed_guid =
{ 0x1c2f2c09, 0x4f43, 0x4cff, { 0x9d, 0xe7, 0x32, 0xe0, 0x14, 0x63, 0x8d, 0xfc } };
HWND hWndWaveseek = NULL, hWndToolTip = NULL, hWndInner = NULL;
WNDPROC oldPlaylistWndProc = NULL;
embedWindowState embed = {0};
TOOLINFO ti = {0};
int on_click = 0, clickTrack = 1, showCuePoints = 0, hideTooltip = 0;
UINT WINAMP_WAVEFORM_SEEK_MENUID = 0xa1bb;
api_service* WASABI_API_SVC = NULL;
api_application *WASABI_API_APP = NULL;
api_language *WASABI_API_LNG = NULL;
// these two must be declared as they're used by the language api's
// when the system is comparing/loading the different resources
HINSTANCE WASABI_API_LNG_HINST = NULL, WASABI_API_ORIG_HINST = NULL;
void DummySAVSAInit(int maxlatency_in_ms, int srate) {}
void DummySAVSADeInit() {}
void DummySAAddPCMData(void *PCMData, int nch, int bps, int timestamp) {}
int DummySAGetMode() { return 0; }
int DummySAAdd(void *data, int timestamp, int csa) { return 0; }
void DummyVSAAddPCMData(void *PCMData, int nch, int bps, int timestamp) {}
int DummyVSAGetMode(int *specNch, int *waveNch) { return 0; }
int DummyVSAAdd(void *data, int timestamp) { return 0; }
void DummyVSASetInfo(int srate, int nch) {}
void DummySetInfo(int bitrate, int srate, int stereo, int synched) {}
WNDPROC lpWndProcOld = NULL, lpWndProc = NULL;
typedef In_Module *(*PluginGetter)();
#define TIMER_ID 31337
#define TIMER_FREQ 100
wchar_t *szDLLPath = 0, *ini_file = 0,
szFilename[MAX_PATH] = {0},
szWaveCacheDir[MAX_PATH] = {0},
szWaveCacheFile[MAX_PATH] = {0},
szTempDLLDestination[MAX_PATH] = {0},
szUnavailable[128] = {0},
szBadPlugin[128] = {0},
szStreamsNotSupported[128] = {0},
pluginTitleW[256] = {0};
In_Module * pModule = NULL;
int nLengthInMS = 0, no_uninstall = 1, delay_load = -1;
bool bIsCurrent = false, bIsLoaded = false, bIsProcessing = false;
int bUnsupported = 0;
DWORD delay_ipc = (DWORD)-1;
HFONT hFont = NULL;
HBRUSH hbrBackground = NULL;
COLORREF clrWaveform = RGB(0, 255, 0),
clrBackground = RGB(0, 0, 0),
clrCuePoint = RGB(117, 116, 139),
clrWaveformPlayed = RGB(0, 128, 0),
clrGeneratingText = RGB(0, 128, 0);
void PluginConfig();
void GetFilePaths()
{
if (!ini_file)
{
// find the winamp.ini for the Winamp install being used
#ifdef WACUP_BUILD
ini_file = (wchar_t *)get_paths()->winamp_ini_file;
lstrcpyn(szWaveCacheDir, get_paths()->settings_dir, ARRAYSIZE(szWaveCacheDir));
#else
ini_file = (wchar_t *)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GETINIFILEW);
lstrcpyn(szWaveCacheDir, (wchar_t *)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GETINIDIRECTORYW), ARRAYSIZE(szWaveCacheDir));
#endif
// make the cache folder in the user's settings folder e.g. %APPDATA%\Winamp\Plugins\wavecache
// which will better ensure that the cache will be correctly generated though it will fallback
// to %PROGRAMFILES(x86)%\Winamp\Plugins\wavecache or %PROGRAMFILES%\Winamp\Plugins\wavecache
// as applicable to the Windows and Winamp version being used (more so with pre v5.11 clients)
PathAppend(szWaveCacheDir, L"Plugins\\wavecache");
CreateDirectory(szWaveCacheDir, NULL);
// find the correct Winamp\Plugins folder (using native api before making a good guess at it)
szDLLPath = (wchar_t *)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GETPLUGINDIRECTORYW);
}
}
int GetFileInfo(const bool unicode, char* szFn, char szFile[MAX_PATH])
{
wchar_t szTitle[GETFILEINFO_TITLE_LENGTH] = {0};
int lengthInMS = -1;
if (!unicode)
{
lstrcpynA(szFile, AutoCharFn(szFilename), MAX_PATH);
}
pModule->GetFileInfo((unicode ? (char*)szFn : szFile), (char*)szTitle, &lengthInMS);
return lengthInMS;
}
void MPG123HotPatch(HINSTANCE module)
{
struct
{
int offset;
int change_len;
int adjust;
char code[9];
char change[6];
}
blocks[] =
{
{0x7740, 2, 0, "\x75\x05\xE8\x89\xFD\xFF\xFF\x6A", "\xEB\x00"}, // 109.2 SSE2
{0x76CB, 2, 0, "\x75\x05\xE8\x8E\xFD\xFF\xFF\x6A", "\xEB\x00"}, // 109.2 normal
{0x7290, 2, 0, "\x75\x05\xE8\x89\xFE\xFF\xFF\x53", "\xEB\x00"}, // 112.1 SSE2
{0x725B, 2, 0, "\x75\x05\xE8\x8E\xFE\xFF\xFF\x53", "\xEB\x00"}, // 112.1 normal
};
HANDLE curproc = GetCurrentProcess();
for (int i = 0; i < ARRAYSIZE(blocks); i++)
{
// offset in gen_ml.dll when loaded
char* p = (char*)((int)module + blocks[i].offset);
if (!memcmp((LPCVOID)p, blocks[i].code, 8))
{
// nudge the start position for the code we want to patch as needed
p += blocks[i].adjust;
DWORD flOldProtect = 0, flDontCare = 0;
if (VirtualProtect((LPVOID)p, blocks[i].change_len, PAGE_EXECUTE_READWRITE, &flOldProtect))
{
// we now write a short jump which skips over the
// native call which is done for LayoutWindows(..)
// so our version is used without conflict from it
DWORD written = 0;
WriteProcessMemory(curproc, (LPVOID)p, blocks[i].change, blocks[i].change_len, &written);
VirtualProtect((LPVOID)p, blocks[i].change_len, flOldProtect, &flDontCare);
}
}
}
}
void StartProcessingFile(const wchar_t * szFn, BOOL start_playing)
{
HMODULE hDLL = NULL;
pModule = NULL;
// we use Winamp's own checking to more reliably ensure that we'll
// get which plug-in is actually responsible for the file handling
In_Module *in_mod = (In_Module*)SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)szFn, IPC_CANPLAY);
if (in_mod && (in_mod != (In_Module*)1))
{
// try to not do the duplicated plug-in method if
// there is nothing playing at the current time
// TODO need to be smarter to check if it's the
// same as the plug-in which has been found
/*if (!start_playing && !SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_ISPLAYING))
{
pModule = in_mod;
}
else*/
{
wchar_t szSource[MAX_PATH] = {0};
GetModuleFileName(in_mod->hDllInstance, szSource, ARRAYSIZE(szSource));
// we got a valid In_Module * so make a temp copy
// which we'll then be calling for the processing
wchar_t *filename = PathFindFileName(szSource);
if (StrStrI(filename, L"in_midi.dll") ||
StrStrI(filename, L"in_wm.dll"))
{
// these native nullsoft plug-in really
// really really doesn't like to work as a
// multi-instance plug-in so best to just
// not attempt to use it for processing :(
//
// somehow leveraging the transcoding api
// might be enough to re-enable in_wm.dll
bUnsupported = 2;
return;
}
PathCombine(szTempDLLDestination, szWaveCacheDir, L"waveseek_");
PathAddExtension(szTempDLLDestination, filename);
// if not there then copy it
if (!PathFileExists(szTempDLLDestination))
{
CopyFile(szSource, szTempDLLDestination, FALSE);
}
// otherwise check there's not a difference in the
// file times which means if the original plug-in
// gets updated then our in-cache copy gets updated
// and this way we're only copying the plug-in once
else
{
HANDLE sourceFile = CreateFile(szSource, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL),
destFile = CreateFile(szTempDLLDestination, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
FILETIME sourceTime = {0}, destTime = {0};
GetFileTime(sourceFile, NULL, NULL, &sourceTime);
GetFileTime(destFile, NULL, NULL, &destTime);
CloseHandle(sourceFile);
CloseHandle(destFile);
// there's a difference so we will re-copy
if (CompareFileTime(&sourceTime, &destTime))
{
CopyFile(szSource, szTempDLLDestination, FALSE);
}
}
if (PathFileExists(szTempDLLDestination))
{
hDLL = GetModuleHandle(szTempDLLDestination);
if (!hDLL)
{
hDLL = LoadLibrary(szTempDLLDestination);
}
if (!hDLL)
{
return;
}
if (StrStrI(filename, L"mpg123"))
{
// this allows us to patch the mpg123 based input plug-in
// (different versions) so we can correct an issue it has
// with the use of the fake output not being called once
// the first file has been processed which was ok when we
// unloaded the input plug-in after use (which isn't good
// for most other plug-ins out there i.e. hang on close!)
MPG123HotPatch(hDLL);
}
PluginGetter pluginGetter = (PluginGetter)GetProcAddress(hDLL, "winampGetInModule2");
if (!pluginGetter)
{
return;
}
pModule = pluginGetter();
}
}
}
if (pModule)
{
pModule->hMainWindow = hWndWaveseek;
pModule->hDllInstance = hDLL;
pModule->dsp_isactive = DummyDSPIsActive;
pModule->dsp_dosamples = DummyDSPDoSamples;
pModule->SAVSAInit = DummySAVSAInit;
pModule->SAVSADeInit = DummySAVSADeInit;
pModule->SAAddPCMData = DummySAAddPCMData;
pModule->SAGetMode = DummySAGetMode;
pModule->SAAdd = DummySAAdd;
pModule->VSAAddPCMData = DummyVSAAddPCMData;
pModule->VSAGetMode = DummyVSAGetMode;
pModule->VSAAdd = DummyVSAAdd;
pModule->VSASetInfo = DummyVSASetInfo;
pModule->SetInfo = DummySetInfo;
// if a v5.66x+ input plug-in then fill the 'service' member
if (((pModule->version & ~IN_UNICODE) & ~IN_INIT_RET) == 0x101)
{
pModule->service = WASABI_API_SVC;
}
const bool has_ret = !!(pModule->version & IN_INIT_RET);
const int ret = pModule->Init();
if ((has_ret && (ret == IN_INIT_SUCCESS)) || !has_ret)
{
if (pModule->UsesOutputPlug & IN_MODULE_FLAG_USES_OUTPUT_PLUGIN)
{
char szFile[MAX_PATH] = {0};
const bool unicode = !!(pModule->version & IN_UNICODE);
nLengthInMS = GetFileInfo(unicode, (char*)szFn, szFile);
if (nLengthInMS <= 0)
{
return;
}
pModule->outMod = CreateOutput(hWndWaveseek, hDLL);
if (pModule->Play((unicode ? (char*)szFn : szFile)) != 0)
{
return;
}
bIsProcessing = true;
}
}
}
}
void ProcessStop()
{
if (pModule)
{
if (bIsProcessing)
{
pModule->Stop();
}
pModule->Quit();
//FreeLibrary(pModule->hDllInstance);
pModule = NULL;
}
bIsProcessing = false;
}
void FinishProcessingFile()
{
HANDLE h = CreateFile(szWaveCacheFile, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, NULL, NULL);
if (h != INVALID_HANDLE_VALUE)
{
DWORD dw = 0;
WriteFile(h, pSampleBuffer, SAMPLE_BUFFER_SIZE * sizeof(unsigned short), &dw, NULL);
CloseHandle(h);
if (dw > 0)
{
bIsLoaded = true;
}
}
ProcessStop();
}
typedef struct {
wchar_t szPerformer[256];
wchar_t szTitle[256];
unsigned int nMillisec;
bool bDrawn;
} CUETRACK;
int nCueTracks = 0;
CUETRACK pCueTracks[256] = {0};
void LoadCUE(wchar_t * szFn)
{
FILE * f = _wfopen( szFn, L"rt" );
if (!f)
{
return;
}
nCueTracks = 0;
int nCurrentTrack = 0;
wchar_t strs[256] = {0};
while (fgetws(strs, 256, f))
{
wchar_t *str = strs;
while (iswspace(*str))
{
++str;
}
if (wcsstr(str, L"TRACK") == str)
{
swscanf(str, L"TRACK %d AUDIO", &nCurrentTrack);
nCurrentTrack = min(nCurrentTrack, 255);
nCueTracks = max(nCueTracks, nCurrentTrack);
pCueTracks[nCurrentTrack - 1].szPerformer[0] = 0;
pCueTracks[nCurrentTrack - 1].szTitle[0] = 0;
}
if (nCurrentTrack > 0)
{
CUETRACK & track = pCueTracks[ nCurrentTrack - 1];
if (wcsstr(str, L"PERFORMER") == str)
{
swscanf(str, L"PERFORMER \"%[^\"]\"", track.szPerformer);
}
if (wcsstr(str, L"TITLE") == str)
{
swscanf(str, L"TITLE \"%[^\"]\"", track.szTitle);
}
if (wcsstr(str, L"INDEX") == str)
{
int m = 0, s = 0, f = 0;
swscanf(str, L"INDEX %*d %d:%d:%d", &m, &s, &f);
track.nMillisec = m * 60 * 1000 + s * 1000 + (f * 1000) / 75;
}
}
}
fclose(f);
}
LPWSTR GetTooltipText(HWND hWnd, int pos, int lengthInMS)
{
static wchar_t coords[256] = {0};
RECT rc = {0};
GetClientRect(hWnd, &rc);
// adjust width down by 1px so that the tooltip should then
// appear when the mouse is at the far right of the window.
unsigned int cur_ms = MulDiv(pos, lengthInMS, (rc.right - rc.left) - 1),
sec = (cur_ms > 0 ? (cur_ms / 1000) : 0),
total_sec = (lengthInMS > 0 ? (lengthInMS / 1000) : 0);
int nTrack = -1;
for (int i = 0; i < nCueTracks; i++)
{
if (i < nCueTracks - 1)
{
if (pCueTracks[i].nMillisec <= cur_ms && cur_ms < pCueTracks[i + 1].nMillisec)
{
nTrack = i;
}
}
else
{
if (pCueTracks[i].nMillisec <= cur_ms)
{
nTrack = i;
}
}
}
if (nTrack >= 0)
{
if (pCueTracks[nTrack].szPerformer[0])
{
StringCchPrintf(coords, ARRAYSIZE(coords), L"%s - %s [%d:%02d / %d:%02d]",
pCueTracks[nTrack].szPerformer, pCueTracks[nTrack].szTitle,
sec / 60, sec % 60, total_sec / 60, total_sec % 60);
}
else
{
StringCchPrintf(coords, ARRAYSIZE(coords), L"%s [%d:%02d / %d:%02d]",
pCueTracks[nTrack].szTitle,
sec / 60, sec % 60, total_sec / 60, total_sec % 60);
}
}
else
{
StringCchPrintf(coords, ARRAYSIZE(coords), L"%d:%02d / %d:%02d",
sec / 60, sec % 60, total_sec / 60, total_sec % 60);
}
return coords;
}
int GetFileLength()
{
basicFileInfoStructW bfiW = {0};
bfiW.filename = szFilename;
if (!SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)&bfiW, IPC_GET_BASIC_FILE_INFOW))
{
if (bfiW.length != -1)
{
return (bfiW.length * 1000);
}
}
return -1000;
}
void ProcessFilePlayback(const wchar_t * szFn, BOOL start_playing)
{
if (StrStrI(szFilename, szFn) && bIsProcessing)
{
// if we're already processing and we're asked
// to re-process (e.g. multiple clicks in the
// main playlist editor) then we try to filter
// out and keep going if it's the same file.
return;
}
ProcessStop();
bIsProcessing = bIsLoaded = bUnsupported = false;
bIsCurrent = !lstrcmpi(szFn, (wchar_t*)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GET_PLAYING_FILENAME));
lstrcpyn(szFilename, szFn, MAX_PATH);
nCueTracks = 0;
// make sure that it's valid and something we can process
if (szFilename[0] && !PathIsURL(szFn))
{
wchar_t szCue[MAX_PATH] = {0};
lstrcpyn(szCue, szFn, MAX_PATH);
PathRenameExtension(szCue, L".cue");
if (PathFileExists(szCue))
{
LoadCUE(szCue);
}
PathCombine(szWaveCacheFile, szWaveCacheDir, PathFindFileName(szFn));
StringCchCat(szWaveCacheFile, MAX_PATH, L".cache");
if (!PathFileExists(szWaveCacheFile))
{
StartProcessingFile(szFn, start_playing);
}
else
{
HANDLE h = CreateFile(szWaveCacheFile, GENERIC_READ, NULL, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (h != INVALID_HANDLE_VALUE)
{
DWORD dw = 0;
ReadFile(h, pSampleBuffer, SAMPLE_BUFFER_SIZE * sizeof(unsigned short), &dw, NULL);
CloseHandle(h);
if (dw > 0)
{
bIsLoaded = true;
}
}
}
}
// if the tooltip is being shown then we need to try
// & update it so that it reflects the current view.
if (!hideTooltip && IsWindowVisible(hWndToolTip))
{
const int lengthInMS = (bIsCurrent ? SendMessage(plugin.hwndParent, WM_WA_IPC, 2, IPC_GETOUTPUTTIME) : GetFileLength());
POINT pt = {0};
GetCursorPos(&pt);
ScreenToClient(hWndInner, &pt);
ti.lpszText = GetTooltipText(hWndInner, pt.x, lengthInMS);
SendMessage(hWndToolTip, TTM_SETTOOLINFO, 0, (LPARAM)&ti);
}
}
HFONT GetWaveformFont()
{
if (!hFont)
{
INT height = (INT)SendMessageW(plugin.hwndParent, WM_WA_IPC, 3, IPC_GET_GENSKINBITMAP);
DWORD charset = (DWORD)SendMessageW(plugin.hwndParent, WM_WA_IPC, 2, IPC_GET_GENSKINBITMAP);
char *fontname = (char*)SendMessageW(plugin.hwndParent, WM_WA_IPC, 1, IPC_GET_GENSKINBITMAP);
hFont = CreateFontA(-height, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, charset,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, fontname);
}
return hFont;
}
void ProcessSkinChange()
{
WADlg_init(plugin.hwndParent);
clrBackground = WADlg_getColor(WADLG_ITEMBG);
clrCuePoint = WADlg_getColor(WADLG_HILITE);
clrGeneratingText = (COLORREF)SendMessage(plugin.hwndParent, WM_WA_IPC, 4, IPC_GET_GENSKINBITMAP);
// get the current skin and use that as a
// means to control the colouring used
wchar_t szBuffer[MAX_PATH] = {0};
SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)szBuffer, IPC_GETSKINW);
if (szBuffer[0])
{
// use the skin provided value
clrWaveformPlayed = clrGeneratingText;
}
else
{
// use a default value as the default
// colour from the classic base skin
// is almost white and looks wrong.
clrWaveformPlayed = RGB(0, 178, 0);
}
clrWaveform = WADlg_getColor(WADLG_ITEMFG);
// try to ensure that things can be seen in-case of
// close colouring by the skin (matches what the ML
// tries to do to ensure some form of visibility).
if (abs(GetColorDistance(clrWaveformPlayed, clrWaveform)) < 70)
{
clrWaveformPlayed = BlendColors(clrBackground, clrWaveform, (COLORREF)77);
}
if (hbrBackground)
{
DeleteBrush(hbrBackground);
}
hbrBackground = CreateSolidBrush(clrBackground);
if (hFont)
{
DeleteFont(hFont);
hFont = NULL;
}
GetWaveformFont();
}
void PaintWaveform(HDC hdc, RECT rc)
{
int nSongPos = (bIsCurrent ? SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GETOUTPUTTIME) : 0),
nSongLen = (bIsCurrent ? SendMessage(plugin.hwndParent, WM_WA_IPC, 2, IPC_GETOUTPUTTIME) : GetFileLength()),
nBufPos = (nSongLen != -1 ? MulDiv(nSongPos, SAMPLE_BUFFER_SIZE, nSongLen) : 0),
w = (rc.right - rc.left), h = (rc.bottom - rc.top);
const HDC hdcMem = CreateCompatibleDC(hdc);
const HBITMAP hbmMem = CreateCompatibleBitmap(hdc, w, h),
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
SelectObject(hdcMem, GetWaveformFont());
FillRect(hdcMem, &rc, hbrBackground);
if ((bIsLoaded || bIsProcessing) && !bUnsupported)
{
// make the width a bit less so the right-edge
// can allow for the end of the track to be
// correctly selected or the tooltip show up
--w;
SelectObject(hdcMem, GetStockObject(DC_PEN));
for (int i = 0; i < w; i++)
{
const int nBufLoc0 = ((i * SAMPLE_BUFFER_SIZE) / w),
nBufLoc1 = min((((i + 1) * SAMPLE_BUFFER_SIZE) / w), SAMPLE_BUFFER_SIZE);
SetDCPenColor(hdcMem, ((nBufLoc0 < nBufPos) ? clrWaveformPlayed : clrWaveform));
unsigned short nSample = 0;
for (int j = nBufLoc0; j < nBufLoc1; j++)
{
nSample = max(pSampleBuffer[j], nSample);
}
const unsigned short sh = ((nSample * h) / 32767);
const int y = (h - sh) / 2;
MoveToEx(hdcMem, i, y, NULL);
LineTo(hdcMem, i, y + sh);
if (showCuePoints && nCueTracks > 0)
{
unsigned int ms = MulDiv(i, nSongLen, w);
if (ms > 0)
{
for (int k = 0; k <= nCueTracks; k++)
{
if (!pCueTracks[k].bDrawn && (pCueTracks[k].nMillisec > 0) && (pCueTracks[k].nMillisec < ms))
{
pCueTracks[k].bDrawn = true;
SetDCPenColor(hdcMem, clrCuePoint);
MoveToEx(hdcMem, i, y, NULL);
LineTo(hdcMem, i, h);
MoveToEx(hdcMem, i, y + sh, NULL);
break;
}
}
}
}
}
for (int k = 0; k < nCueTracks; k++)
{
pCueTracks[k].bDrawn = false;
}
// and now restore so that we get drawn correctly
++w;
}
else
{
SetBkColor(hdcMem, clrBackground);
SetTextColor(hdcMem, clrGeneratingText);
DrawText(hdcMem, (!PathIsURL(szFilename) ? (bUnsupported == 2 ? szBadPlugin :
szUnavailable) : szStreamsNotSupported),
-1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
}
BitBlt(hdc, 0, 0, w, h, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteObject(hbmMem);
DeleteDC(hdcMem);
}
LRESULT sendMlIpc(int msg, WPARAM param)
{
static LRESULT IPC_GETMLWINDOW;
if (!IPC_GETMLWINDOW)
{
IPC_GETMLWINDOW = SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)&"LibraryGetWnd", IPC_REGISTER_WINAMP_IPCMESSAGE);
}
HWND mlwnd = (HWND)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GETMLWINDOW);
if ((param == 0) && (msg == 0))
{
return (LRESULT)mlwnd;
}
if (IsWindow(mlwnd))
{
return SendMessage(mlwnd, WM_ML_IPC, param, msg);
}
return 0;
}
BOOL Menu_TrackPopup(HMENU hMenu, UINT fuFlags, int x, int y, HWND hwnd)
{
if (hMenu == NULL)
{
return NULL;
}
if (IsWindow((HWND)sendMlIpc(0, 0)))
{
MLSKINNEDPOPUP popup = {sizeof(MLSKINNEDPOPUP)};
popup.hmenu = hMenu;
popup.fuFlags = fuFlags;
popup.x = x;
popup.y = y;
popup.hwnd = hwnd;
popup.skinStyle = SMS_USESKINFONT;
return (INT)sendMlIpc(ML_IPC_TRACKSKINNEDPOPUPEX, (WPARAM)&popup);
}
return TrackPopupMenu(hMenu, fuFlags, x, y, 0, hwnd, NULL);
}
bool ProcessMenuResult(UINT command, HWND parent)
{
switch (LOWORD(command))
{
case ID_SUBMENU_VIEWFILEINFO:
{
infoBoxParamW infoBoxW = {hWndWaveseek, szFilename};
SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)&infoBoxW, IPC_INFOBOXW);
break;
}
case ID_SUBMENU_CLEARWAVCACHE:
{
if (MessageBox(parent, WASABI_API_LNGSTRINGW(IDS_CLEAR_CACHE),
(LPWSTR)plugin.description, MB_YESNO | MB_ICONQUESTION) != IDYES)
{
break;
}
// remove all *.cache files in the users current cache folder.
//
// removing the waveseek_in_*.dll isn't covered as they are
// still loaded and the unload / force delete will cause a
// crash and so is not worth the hassle to do it. plus it
// updates automatically as needed with the copy file checks :)
WIN32_FIND_DATAW wfd = {0};
wchar_t szFnFind[MAX_PATH] = {0};
PathCombine(szFnFind, szWaveCacheDir, L"*.cache");
HANDLE hFind = FindFirstFile(szFnFind, &wfd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
PathCombine(szFnFind, szWaveCacheDir, wfd.cFileName);
DeleteFile(szFnFind);
}
while (FindNextFile(hFind, &wfd));
FindClose(hFind);
}
// we will want to fall through so we
// can then re-render the current file
}
case ID_SUBMENU_RERENDER:
{
if (!PathIsURL(szFilename))
{
wchar_t cacheFile[MAX_PATH] = {0};
PathCombine(cacheFile, szWaveCacheDir, PathFindFileName(szFilename));
StringCchCat(cacheFile, MAX_PATH, L".cache");
if (PathFileExists(cacheFile))
{
DeleteFile(cacheFile);
}
ProcessFilePlayback(szFilename, TRUE);
}
break;
}
case ID_CONTEXTMENU_CLICKTRACK:
{
clickTrack = (!clickTrack);
WritePrivateProfileInt(L"Waveseek", L"clickTrack", clickTrack, ini_file);
// update as needed to match the new setting
// with fallback to the current playing if
// there's no selection or it's been disabled
int index = SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GETLISTPOS);
if (clickTrack)
{
if (SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_PLAYLIST_GET_SELECTED_COUNT))
{
const int sel = (int)SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)-1, IPC_PLAYLIST_GET_NEXT_SELECTED);
if (sel != -1)
{
index = sel;
}
}
}
ProcessFilePlayback((const wchar_t *)SendMessage(plugin.hwndParent, WM_WA_IPC, index, IPC_GETPLAYLISTFILEW), FALSE);
break;
}
case ID_SUBMENU_SHOWCUEPOINTS:
{
showCuePoints = (!showCuePoints);
WritePrivateProfileInt(L"Waveseek", L"showCuePoints", showCuePoints, ini_file);
break;
}
case ID_SUBMENU_HIDEWAVEFORMTOOLTIP:
{
hideTooltip = (!hideTooltip);
WritePrivateProfileInt(L"Waveseek", L"hideTooltip", hideTooltip, ini_file);
break;
}
case ID_SUBMENU_ABOUT:
{
wchar_t message[512] = {0};
StringCchPrintf(message, ARRAYSIZE(message), WASABI_API_LNGSTRINGW(IDS_ABOUT_STRING), TEXT(__DATE__));
MessageBox(plugin.hwndParent, message, pluginTitleW, 0);
break;
}
default:
{
return false;
}
}
return true;
}
INT_PTR CALLBACK EmdedWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_WA_MPEG_EOF:
{
FinishProcessingFile();
break;
}
case WM_WA_IPC:
{
// this is used to pass on some of the messages but not all
// since we use this window as a fake 'main' window so some
// of the messages like WM_WA_MPEG_EOF are blocked from the
// real main window when using the dll re-use hack in place
return SendMessage(plugin.hwndParent, uMsg, wParam, lParam);
}
case WM_COMMAND: // for what's handled from the accel table
{
if (ProcessMenuResult(wParam, hWnd))
{
break;
}
}
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_MOUSEWHEEL:
{
PostMessage(plugin.hwndParent, uMsg, wParam, lParam);
break;
}
case WM_CONTEXTMENU:
{
short xPos = GET_X_LPARAM(lParam);
short yPos = GET_Y_LPARAM(lParam);
HMENU hMenu = WASABI_API_LOADMENUW(IDR_CONTEXTMENU);
HMENU popup = GetSubMenu(hMenu, 0);
CheckMenuItem(popup, ID_CONTEXTMENU_CLICKTRACK, MF_BYCOMMAND | (clickTrack ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(popup, ID_SUBMENU_SHOWCUEPOINTS, MF_BYCOMMAND | (showCuePoints ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(popup, ID_SUBMENU_HIDEWAVEFORMTOOLTIP, MF_BYCOMMAND | (hideTooltip ? MF_CHECKED : MF_UNCHECKED));
// this will handle the menu being shown not via the mouse actions
// so is positioned just below the header if no selection but there's a queue
// or below the item selected (or the no files in queue entry)
if (xPos == -1 || yPos == -1)
{
RECT rc = {0};
GetWindowRect(GetWindow(hWnd, GW_CHILD), &rc);
xPos = (short)rc.left;
yPos = (short)rc.top;
}
ProcessMenuResult(Menu_TrackPopup(popup, TPM_LEFTALIGN | TPM_LEFTBUTTON |
TPM_RIGHTBUTTON | TPM_RETURNCMD, xPos, yPos, hWnd), hWnd);
DestroyMenu(hMenu);
break;
}
}
return CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam);
}
INT_PTR CALLBACK InnerWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// if you need to do other message handling then you can just place this first and
// process the messages you need to afterwards. note this is passing the frame and
// its id so if you have a few embedded windows you need to do this with each child
if (HandleEmbeddedWindowChildMessages(hWndWaveseek, WINAMP_WAVEFORM_SEEK_MENUID,
hWnd, uMsg, wParam, lParam))
{
return 0;
}
bool bForceJump = false;
switch (uMsg)
{
case WM_INITDIALOG:
{
hWndToolTip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, (HWND)lParam, NULL,
plugin.hDllInstance, NULL);
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
ti.hwnd = (HWND)lParam;
ti.hinst = plugin.hDllInstance;
ti.uId = (UINT_PTR)lParam;
PostMessage(hWndToolTip, TTM_ADDTOOL, NULL, (LPARAM)&ti);
SetTimer(hWnd, TIMER_ID, TIMER_FREQ, NULL);
SetWindowLongPtr(hWnd, GWLP_ID, 101);
break;
}
case WM_TIMER:
{
if (wParam == TIMER_ID)
{
InvalidateRect(hWnd, NULL, TRUE);
}
break;
}
case WM_ERASEBKGND:
{
RECT rc = {0};
GetClientRect(hWnd, &rc);
PaintWaveform((HDC)wParam, rc);
return 1;
}
case WM_LBUTTONDBLCLK:
{
// start playing current or selection as needed
// this also jumps to the time point where the click
// happened as a coincidence of the mesages received
if (bIsCurrent)
{
if (!SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_ISPLAYING))