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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
|
/*
* Bridge.cpp
* ----------
* Purpose: VST plugin bridge (plugin side)
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
// TODO: Translate pointer-sized members in remaining structs: VstVariableIo, VstOfflineTask, VstAudioFile, VstWindow (all these are currently not supported by OpenMPT, so not urgent at all)
#include "openmpt/all/BuildSettings.hpp"
#include "../common/mptBaseMacros.h"
#include "../common/mptBaseTypes.h"
#include "../common/mptBaseUtils.h"
#include <Windows.h>
#include <ShellAPI.h>
#include <ShlObj.h>
#include <CommDlg.h>
#include <tchar.h>
#include <algorithm>
#include <string>
#if defined(MPT_BUILD_MSVC)
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "shell32.lib")
#endif
#if MPT_BUILD_DEBUG
#include <intrin.h>
#define MPT_ASSERT(x) \
MPT_MAYBE_CONSTANT_IF(!(x)) \
{ \
if(IsDebuggerPresent()) \
__debugbreak(); \
::MessageBoxA(nullptr, "Debug Assertion Failed:\n\n" #x, "OpenMPT Plugin Bridge", MB_ICONERROR); \
}
#else
#define MPT_ASSERT(x)
#endif
#include "../misc/WriteMemoryDump.h"
#include "Bridge.h"
// Crash handler for writing memory dumps
static LONG WINAPI CrashHandler(_EXCEPTION_POINTERS *pExceptionInfo)
{
WCHAR tempPath[MAX_PATH + 2];
DWORD result = GetTempPathW(MAX_PATH + 1, tempPath);
if(result > 0 && result <= MAX_PATH + 1)
{
std::wstring filename = tempPath;
filename += L"OpenMPT Crash Files\\";
CreateDirectoryW(filename.c_str(), nullptr);
tempPath[0] = 0;
const int ch = GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, nullptr, L"'PluginBridge 'yyyy'-'MM'-'dd ", tempPath, mpt::saturate_cast<int>(std::size(tempPath)));
if(ch)
GetTimeFormatW(LOCALE_SYSTEM_DEFAULT, 0, nullptr, L"HH'.'mm'.'ss'.dmp'", tempPath + ch - 1, mpt::saturate_cast<int>(std::size(tempPath)) - ch + 1);
filename += tempPath;
OPENMPT_NAMESPACE::WriteMemoryDump(pExceptionInfo, filename.c_str(), OPENMPT_NAMESPACE::PluginBridge::m_fullMemDump);
}
// Let Windows handle the exception...
return EXCEPTION_CONTINUE_SEARCH;
}
int _tmain(int argc, TCHAR *argv[])
{
if(argc != 2)
{
MessageBox(nullptr, _T("This executable is part of OpenMPT. You do not need to run it by yourself."), _T("OpenMPT Plugin Bridge"), 0);
return -1;
}
::SetUnhandledExceptionFilter(CrashHandler);
// We don't need COM, but some plugins do and don't initialize it themselves.
// Note 1: Which plugins? This was added in r6459 on 2016-05-31 but with no remark whether it fixed a specific plugin,
// but the fix doesn't seem to make a lot of sense since back then no plugin code was ever running on the main thread.
// Could it have been for file dialogs, which were added a while before?
// Note 2: M1 editor crashes if it runs on this thread and it was initialized with COINIT_MULTITHREADED
const bool comInitialized = SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED));
OPENMPT_NAMESPACE::PluginBridge::MainLoop(argv);
if(comInitialized)
CoUninitialize();
return 0;
}
int WINAPI WinMain(_In_ HINSTANCE /*hInstance*/, _In_opt_ HINSTANCE /*hPrevInstance*/, _In_ LPSTR /*lpCmdLine*/, _In_ int /*nCmdShow*/)
{
int argc = 0;
auto argv = CommandLineToArgvW(GetCommandLineW(), &argc);
return _tmain(argc, argv);
}
OPENMPT_NAMESPACE_BEGIN
using namespace Vst;
std::vector<BridgeCommon *> BridgeCommon::m_plugins;
HWND BridgeCommon::m_communicationWindow = nullptr;
int BridgeCommon::m_instanceCount = 0;
thread_local bool BridgeCommon::m_isAudioThread = false;
// This is kind of a back-up pointer in case we couldn't sneak our pointer into the AEffect struct yet.
// It always points to the last initialized PluginBridge object.
PluginBridge *PluginBridge::m_latestInstance = nullptr;
ATOM PluginBridge::m_editorClassAtom = 0;
bool PluginBridge::m_fullMemDump = false;
void PluginBridge::MainLoop(TCHAR *argv[])
{
WNDCLASSEX editorWndClass;
editorWndClass.cbSize = sizeof(WNDCLASSEX);
editorWndClass.style = CS_HREDRAW | CS_VREDRAW;
editorWndClass.lpfnWndProc = WindowProc;
editorWndClass.cbClsExtra = 0;
editorWndClass.cbWndExtra = 0;
editorWndClass.hInstance = GetModuleHandle(nullptr);
editorWndClass.hIcon = nullptr;
editorWndClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
editorWndClass.hbrBackground = nullptr;
editorWndClass.lpszMenuName = nullptr;
editorWndClass.lpszClassName = _T("OpenMPTPluginBridgeEditor");
editorWndClass.hIconSm = nullptr;
m_editorClassAtom = RegisterClassEx(&editorWndClass);
CreateCommunicationWindow(WindowProc);
SetTimer(m_communicationWindow, TIMER_IDLE, 20, IdleTimerProc);
uint32 parentProcessId = _ttoi(argv[1]);
new PluginBridge(argv[0], OpenProcess(SYNCHRONIZE, FALSE, parentProcessId));
MSG msg;
while(::GetMessage(&msg, nullptr, 0, 0))
{
// Let host pre-process key messages like it does for non-bridged plugins
if(msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST)
{
HWND owner = nullptr;
for(HWND hwnd = msg.hwnd; hwnd != nullptr; hwnd = GetParent(hwnd))
{
// Does it come from a child window? (e.g. Kirnu editor)
if(GetClassWord(hwnd, GCW_ATOM) == m_editorClassAtom)
{
owner = GetParent(GetParent(hwnd));
break;
}
// Does the message come from a top-level window? This is required e.g. for the slider pop-up windows and patch browser in Synth1.
if(!(GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD))
{
owner = GetWindow(hwnd, GW_OWNER);
break;
}
}
// Send to top-level VST editor window in host
if(owner && SendMessage(owner, msg.message + WM_BRIDGE_KEYFIRST - WM_KEYFIRST, msg.wParam, msg.lParam))
continue;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyWindow(m_communicationWindow);
}
PluginBridge::PluginBridge(const wchar_t *memName, HANDLE otherProcess)
{
PluginBridge::m_latestInstance = this;
m_thisPluginID = static_cast<int32>(m_plugins.size());
m_plugins.push_back(this);
if(!m_queueMem.Open(memName)
|| !CreateSignals(memName))
{
MessageBox(nullptr, _T("Could not connect to OpenMPT."), _T("OpenMPT Plugin Bridge"), 0);
delete this;
return;
}
m_sharedMem = m_queueMem.Data<SharedMemLayout>();
// Store parent process handle so that we can terminate the bridge process when OpenMPT closes (e.g. through a crash).
m_otherProcess.DuplicateFrom(otherProcess);
m_sigThreadExit.Create(true);
DWORD dummy = 0; // For Win9x
m_audioThread = CreateThread(NULL, 0, &PluginBridge::AudioThread, this, 0, &dummy);
m_sharedMem->bridgeCommWindow = m_communicationWindow;
m_sharedMem->bridgePluginID = m_thisPluginID;
m_sigBridgeReady.Trigger();
}
PluginBridge::~PluginBridge()
{
SignalObjectAndWait(m_sigThreadExit, m_audioThread, INFINITE, FALSE);
CloseHandle(m_audioThread);
BridgeMessage dispatchMsg;
dispatchMsg.Dispatch(effClose, 0, 0, 0, 0.0f, 0);
DispatchToPlugin(dispatchMsg.dispatch);
m_plugins[m_thisPluginID] = nullptr;
if(m_instanceCount == 1)
PostQuitMessage(0);
}
void PluginBridge::RequestDelete()
{
PostMessage(m_communicationWindow, WM_BRIDGE_DELETE_PLUGIN, m_thisPluginID, 0);
}
// Send an arbitrary message to the host.
// Returns true if the message was processed by the host.
bool PluginBridge::SendToHost(BridgeMessage &sendMsg)
{
auto &messages = m_sharedMem->ipcMessages;
const auto msgID = CopyToSharedMemory(sendMsg, messages);
if(msgID < 0)
return false;
BridgeMessage &sharedMsg = messages[msgID];
if(!m_isAudioThread)
{
if(SendMessage(m_sharedMem->hostCommWindow, WM_BRIDGE_MESSAGE_TO_HOST, m_otherPluginID, msgID) == WM_BRIDGE_SUCCESS)
{
sharedMsg.CopyTo(sendMsg);
return true;
}
return false;
}
// Audio thread: Use signals instead of window messages
m_sharedMem->audioThreadToHostMsgID = msgID;
m_sigToHostAudio.Send();
// Wait until we get the result from the host.
DWORD result;
const HANDLE objects[] = {m_sigToHostAudio.confirm, m_sigToBridgeAudio.send, m_otherProcess};
do
{
result = WaitForMultipleObjects(mpt::saturate_cast<DWORD>(std::size(objects)), objects, FALSE, INFINITE);
if(result == WAIT_OBJECT_0)
{
// Message got answered
sharedMsg.CopyTo(sendMsg);
break;
} else if(result == WAIT_OBJECT_0 + 1)
{
ParseNextMessage(m_sharedMem->audioThreadToBridgeMsgID);
m_sigToBridgeAudio.Confirm();
}
} while(result != WAIT_OBJECT_0 + 2 && result != WAIT_FAILED);
return (result == WAIT_OBJECT_0);
}
// Copy AEffect to shared memory.
void PluginBridge::UpdateEffectStruct()
{
if(m_nativeEffect == nullptr)
return;
else if(m_otherPtrSize == 4)
m_sharedMem->effect32.FromNative(*m_nativeEffect);
else if(m_otherPtrSize == 8)
m_sharedMem->effect64.FromNative(*m_nativeEffect);
else
MPT_ASSERT(false);
}
// Create the memory-mapped file containing the processing message and audio buffers
void PluginBridge::CreateProcessingFile(std::vector<char> &dispatchData)
{
static uint32 plugId = 0;
wchar_t mapName[64];
swprintf(mapName, std::size(mapName), L"Local\\openmpt-%u-%u", GetCurrentProcessId(), plugId++);
PushToVector(dispatchData, mapName[0], sizeof(mapName));
if(!m_processMem.Create(mapName, sizeof(ProcessMsg) + m_mixBufSize * (m_nativeEffect->numInputs + m_nativeEffect->numOutputs) * sizeof(double)))
{
SendErrorMessage(L"Could not initialize plugin bridge audio memory.");
return;
}
}
// Receive a message from the host and translate it.
void PluginBridge::ParseNextMessage(int msgID)
{
auto &msg = m_sharedMem->ipcMessages[msgID];
switch(msg.header.type)
{
case MsgHeader::newInstance:
NewInstance(msg.newInstance);
break;
case MsgHeader::init:
InitBridge(msg.init);
break;
case MsgHeader::dispatch:
DispatchToPlugin(msg.dispatch);
break;
case MsgHeader::setParameter:
SetParameter(msg.parameter);
break;
case MsgHeader::getParameter:
GetParameter(msg.parameter);
break;
case MsgHeader::automate:
AutomateParameters();
break;
}
}
// Create a new bridge instance within this one (creates a new thread).
void PluginBridge::NewInstance(NewInstanceMsg &msg)
{
msg.memName[mpt::array_size<decltype(msg.memName)>::size - 1] = 0;
new PluginBridge(msg.memName, m_otherProcess);
}
// Load the plugin.
void PluginBridge::InitBridge(InitMsg &msg)
{
m_otherPtrSize = msg.hostPtrSize;
m_mixBufSize = msg.mixBufSize;
m_otherPluginID = msg.pluginID;
m_fullMemDump = msg.fullMemDump != 0;
msg.result = 0;
msg.str[mpt::array_size<decltype(msg.str)>::size - 1] = 0;
#ifdef _CONSOLE
SetConsoleTitleW(msg->str);
#endif
m_nativeEffect = nullptr;
__try
{
m_library = LoadLibraryW(msg.str);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
m_library = nullptr;
}
if(m_library == nullptr)
{
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg.str, mpt::saturate_cast<DWORD>(std::size(msg.str)), nullptr);
RequestDelete();
return;
}
auto mainProc = (Vst::MainProc)GetProcAddress(m_library, "VSTPluginMain");
if(mainProc == nullptr)
{
mainProc = (Vst::MainProc)GetProcAddress(m_library, "main");
}
if(mainProc != nullptr)
{
__try
{
m_nativeEffect = mainProc(MasterCallback);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
m_nativeEffect = nullptr;
}
}
if(m_nativeEffect == nullptr || m_nativeEffect->dispatcher == nullptr || m_nativeEffect->magic != kEffectMagic)
{
FreeLibrary(m_library);
m_library = nullptr;
wcscpy(msg.str, L"File is not a valid plugin");
RequestDelete();
return;
}
m_nativeEffect->reservedForHost1 = this;
msg.result = 1;
UpdateEffectStruct();
// Init process buffer
DispatchToHost(audioMasterVendorSpecific, kVendorOpenMPT, kUpdateProcessingBuffer, nullptr, 0.0f);
}
void PluginBridge::SendErrorMessage(const wchar_t *str)
{
BridgeMessage msg;
msg.Error(str);
SendToHost(msg);
}
// Wrapper for VST dispatch call with structured exception handling.
static intptr_t DispatchSEH(AEffect *effect, VstOpcodeToPlugin opCode, int32 index, intptr_t value, void *ptr, float opt, bool &exception)
{
__try
{
if(effect->dispatcher != nullptr)
{
return effect->dispatcher(effect, opCode, index, value, ptr, opt);
}
} __except(EXCEPTION_EXECUTE_HANDLER)
{
exception = true;
}
return 0;
}
// Host-to-plugin opcode dispatcher
void PluginBridge::DispatchToPlugin(DispatchMsg &msg)
{
if(m_nativeEffect == nullptr)
{
return;
}
// Various dispatch data - depending on the opcode, one of those might be used.
std::vector<char> extraData;
size_t extraDataSize = 0;
MappedMemory auxMem;
// Content of ptr is usually stored right after the message header, ptr field indicates size.
void *ptr = (msg.ptr != 0) ? (&msg + 1) : nullptr;
if(msg.size > sizeof(BridgeMessage))
{
if(!auxMem.Open(static_cast<const wchar_t *>(ptr)))
{
return;
}
ptr = auxMem.Data();
}
void *origPtr = ptr;
switch(msg.opcode)
{
case effGetProgramName:
case effGetParamLabel:
case effGetParamDisplay:
case effGetParamName:
case effString2Parameter:
case effGetProgramNameIndexed:
case effGetEffectName:
case effGetErrorText:
case effGetVendorString:
case effGetProductString:
case effShellGetNextPlugin:
// Name in [ptr]
extraDataSize = 256;
break;
case effMainsChanged:
// [value]: 0 means "turn off", 1 means "turn on"
::SetThreadPriority(m_audioThread, msg.value ? THREAD_PRIORITY_ABOVE_NORMAL : THREAD_PRIORITY_NORMAL);
m_sharedMem->tailSize = static_cast<int32>(Dispatch(effGetTailSize, 0, 0, nullptr, 0.0f));
break;
case effEditGetRect:
// ERect** in [ptr]
extraDataSize = sizeof(void *);
break;
case effEditOpen:
// HWND in [ptr] - Note: Window handles are interoperable between 32-bit and 64-bit applications in Windows (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx)
{
TCHAR str[_MAX_PATH];
GetModuleFileName(m_library, str, mpt::saturate_cast<DWORD>(std::size(str)));
const auto parentWindow = reinterpret_cast<HWND>(msg.ptr);
ptr = m_window = CreateWindow(
MAKEINTATOM(m_editorClassAtom),
str,
WS_CHILD | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
1, 1,
parentWindow,
nullptr,
GetModuleHandle(nullptr),
nullptr);
SetWindowLongPtr(m_window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
}
break;
case effGetChunk:
// void** in [ptr] for chunk data address
extraDataSize = sizeof(void *);
break;
case effProcessEvents:
// VstEvents* in [ptr]
TranslateBridgeToVstEvents(m_eventCache, ptr);
ptr = m_eventCache.data();
break;
case effOfflineNotify:
// VstAudioFile* in [ptr]
extraData.resize(sizeof(VstAudioFile *) * static_cast<size_t>(msg.value));
ptr = extraData.data();
for(int64 i = 0; i < msg.value; i++)
{
// TODO create pointers
}
break;
case effOfflinePrepare:
case effOfflineRun:
// VstOfflineTask* in [ptr]
extraData.resize(sizeof(VstOfflineTask *) * static_cast<size_t>(msg.value));
ptr = extraData.data();
for(int64 i = 0; i < msg.value; i++)
{
// TODO create pointers
}
break;
case effSetSpeakerArrangement:
case effGetSpeakerArrangement:
// VstSpeakerArrangement* in [value] and [ptr]
msg.value = reinterpret_cast<int64>(ptr) + sizeof(VstSpeakerArrangement);
break;
case effVendorSpecific:
// Let's implement some custom opcodes!
if(msg.index == kVendorOpenMPT)
{
msg.result = 1;
switch(msg.value)
{
case kUpdateEffectStruct:
UpdateEffectStruct();
break;
case kUpdateEventMemName:
if(ptr)
m_eventMem.Open(static_cast<const wchar_t *>(ptr));
break;
case kCacheProgramNames:
if(ptr)
{
int32 progMin = static_cast<const int32 *>(ptr)[0];
int32 progMax = static_cast<const int32 *>(ptr)[1];
char *name = static_cast<char *>(ptr);
for(int32 i = progMin; i < progMax; i++)
{
strcpy(name, "");
if(m_nativeEffect->numPrograms <= 0 || Dispatch(effGetProgramNameIndexed, i, -1, name, 0) != 1)
{
// Fallback: Try to get current program name.
strcpy(name, "");
int32 curProg = static_cast<int32>(Dispatch(effGetProgram, 0, 0, nullptr, 0.0f));
if(i != curProg)
{
Dispatch(effSetProgram, 0, i, nullptr, 0.0f);
}
Dispatch(effGetProgramName, 0, 0, name, 0);
if(i != curProg)
{
Dispatch(effSetProgram, 0, curProg, nullptr, 0.0f);
}
}
name[kCachedProgramNameLength - 1] = '\0';
name += kCachedProgramNameLength;
}
}
break;
case kCacheParameterInfo:
if(ptr)
{
int32 paramMin = static_cast<const int32 *>(ptr)[0];
int32 paramMax = static_cast<const int32 *>(ptr)[1];
ParameterInfo *param = static_cast<ParameterInfo *>(ptr);
for(int32 i = paramMin; i < paramMax; i++, param++)
{
strcpy(param->name, "");
strcpy(param->label, "");
strcpy(param->display, "");
Dispatch(effGetParamName, i, 0, param->name, 0.0f);
Dispatch(effGetParamLabel, i, 0, param->label, 0.0f);
Dispatch(effGetParamDisplay, i, 0, param->display, 0.0f);
param->name[mpt::array_size<decltype(param->label)>::size - 1] = '\0';
param->label[mpt::array_size<decltype(param->label)>::size - 1] = '\0';
param->display[mpt::array_size<decltype(param->display)>::size - 1] = '\0';
if(Dispatch(effGetParameterProperties, i, 0, ¶m->props, 0.0f) != 1)
{
memset(¶m->props, 0, sizeof(param->props));
strncpy(param->props.label, param->name, std::size(param->props.label));
}
}
}
break;
case kBeginGetProgram:
if(ptr)
{
int32 numParams = static_cast<int32>((msg.size - sizeof(DispatchMsg)) / sizeof(float));
float *params = static_cast<float *>(ptr);
for(int32 i = 0; i < numParams; i++)
{
params[i] = m_nativeEffect->getParameter(m_nativeEffect, i);
}
}
break;
default:
msg.result = 0;
}
return;
}
break;
}
if(extraDataSize != 0)
{
extraData.resize(extraDataSize, 0);
ptr = extraData.data();
}
//std::cout << "about to dispatch " << msg.opcode << " to effect...";
//std::flush(std::cout);
bool exception = false;
msg.result = static_cast<int32>(DispatchSEH(m_nativeEffect, static_cast<VstOpcodeToPlugin>(msg.opcode), msg.index, static_cast<intptr_t>(msg.value), ptr, msg.opt, exception));
if(exception && msg.opcode != effClose)
{
msg.type = MsgHeader::exceptionMsg;
return;
}
//std::cout << "done" << std::endl;
// Post-fix some opcodes
switch(msg.opcode)
{
case effClose:
m_nativeEffect = nullptr;
FreeLibrary(m_library);
m_library = nullptr;
RequestDelete();
return;
case effGetProgramName:
case effGetParamLabel:
case effGetParamDisplay:
case effGetParamName:
case effString2Parameter:
case effGetProgramNameIndexed:
case effGetEffectName:
case effGetErrorText:
case effGetVendorString:
case effGetProductString:
case effShellGetNextPlugin:
// Name in [ptr]
{
extraData.back() = 0;
char *dst = static_cast<char *>(origPtr);
size_t length = static_cast<size_t>(msg.ptr - 1);
strncpy(dst, extraData.data(), length);
dst[length] = 0;
break;
}
case effEditGetRect:
// ERect** in [ptr]
{
ERect *rectPtr = *reinterpret_cast<ERect **>(extraData.data());
if(rectPtr != nullptr && origPtr != nullptr)
{
MPT_ASSERT(static_cast<size_t>(msg.ptr) >= sizeof(ERect));
std::memcpy(origPtr, rectPtr, std::min(sizeof(ERect), static_cast<size_t>(msg.ptr)));
m_windowWidth = rectPtr->right - rectPtr->left;
m_windowHeight = rectPtr->bottom - rectPtr->top;
// For plugins that don't know their size until after effEditOpen is done.
if(m_window)
{
SetWindowPos(m_window, nullptr, 0, 0, m_windowWidth, m_windowHeight, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
break;
}
case effEditClose:
DestroyWindow(m_window);
m_window = nullptr;
break;
case effGetChunk:
// void** in [ptr] for chunk data address
if(m_getChunkMem.Create(static_cast<const wchar_t *>(origPtr), msg.result))
{
std::memcpy(m_getChunkMem.Data(), *reinterpret_cast<void **>(extraData.data()), msg.result);
}
break;
}
UpdateEffectStruct(); // Regularly update the struct
}
intptr_t PluginBridge::Dispatch(VstOpcodeToPlugin opcode, int32 index, intptr_t value, void *ptr, float opt)
{
__try
{
return m_nativeEffect->dispatcher(m_nativeEffect, opcode, index, value, ptr, opt);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
SendErrorMessage(L"Exception in dispatch()!");
}
return 0;
}
// Set a plugin parameter.
void PluginBridge::SetParameter(ParameterMsg &msg)
{
__try
{
m_nativeEffect->setParameter(m_nativeEffect, msg.index, msg.value);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
msg.type = MsgHeader::exceptionMsg;
}
}
// Get a plugin parameter.
void PluginBridge::GetParameter(ParameterMsg &msg)
{
__try
{
msg.value = m_nativeEffect->getParameter(m_nativeEffect, msg.index);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
msg.type = MsgHeader::exceptionMsg;
}
}
// Execute received parameter automation messages
void PluginBridge::AutomateParameters()
{
__try
{
const AutomationQueue::Parameter *param = m_sharedMem->automationQueue.params;
const AutomationQueue::Parameter *paramEnd = param + std::min(m_sharedMem->automationQueue.pendingEvents.exchange(0), static_cast<int32>(std::size(m_sharedMem->automationQueue.params)));
while(param != paramEnd)
{
m_nativeEffect->setParameter(m_nativeEffect, param->index, param->value);
param++;
}
} __except(EXCEPTION_EXECUTE_HANDLER)
{
SendErrorMessage(L"Exception in setParameter()!");
}
}
// Audio rendering thread
DWORD WINAPI PluginBridge::AudioThread(LPVOID param)
{
static_cast<PluginBridge*>(param)->AudioThread();
return 0;
}
void PluginBridge::AudioThread()
{
m_isAudioThread = true;
const HANDLE objects[] = {m_sigProcessAudio.send, m_sigToBridgeAudio.send, m_sigThreadExit, m_otherProcess};
DWORD result = 0;
do
{
result = WaitForMultipleObjects(mpt::saturate_cast<DWORD>(std::size(objects)), objects, FALSE, INFINITE);
if(result == WAIT_OBJECT_0)
{
ProcessMsg *msg = m_processMem.Data<ProcessMsg>();
AutomateParameters();
m_sharedMem->tailSize = static_cast<int32>(Dispatch(effGetTailSize, 0, 0, nullptr, 0.0f));
m_isProcessing = true;
// Prepare VstEvents.
if(auto *events = m_eventMem.Data<int32>(); events != nullptr && *events != 0)
{
TranslateBridgeToVstEvents(m_eventCache, events);
*events = 0;
Dispatch(effProcessEvents, 0, 0, m_eventCache.data(), 0.0f);
}
switch(msg->processType)
{
case ProcessMsg::process:
Process();
break;
case ProcessMsg::processReplacing:
ProcessReplacing();
break;
case ProcessMsg::processDoubleReplacing:
ProcessDoubleReplacing();
break;
}
m_isProcessing = false;
m_sigProcessAudio.Confirm();
} else if(result == WAIT_OBJECT_0 + 1)
{
ParseNextMessage(m_sharedMem->audioThreadToBridgeMsgID);
m_sigToBridgeAudio.Confirm();
} else if(result == WAIT_OBJECT_0 + 2)
{
// Main thread asked for termination
break;
} else if(result == WAIT_OBJECT_0 + 3)
{
// Host process died
RequestDelete();
break;
}
} while(result != WAIT_FAILED);
}
// Process audio.
void PluginBridge::Process()
{
if(m_nativeEffect->process)
{
float **inPointers, **outPointers;
int32 sampleFrames = BuildProcessPointers(inPointers, outPointers);
__try
{
m_nativeEffect->process(m_nativeEffect, inPointers, outPointers, sampleFrames);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
SendErrorMessage(L"Exception in process()!");
}
}
}
// Process audio.
void PluginBridge::ProcessReplacing()
{
if(m_nativeEffect->processReplacing)
{
float **inPointers, **outPointers;
int32 sampleFrames = BuildProcessPointers(inPointers, outPointers);
__try
{
m_nativeEffect->processReplacing(m_nativeEffect, inPointers, outPointers, sampleFrames);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
SendErrorMessage(L"Exception in processReplacing()!");
}
}
}
// Process audio.
void PluginBridge::ProcessDoubleReplacing()
{
if(m_nativeEffect->processDoubleReplacing)
{
double **inPointers, **outPointers;
int32 sampleFrames = BuildProcessPointers(inPointers, outPointers);
__try
{
m_nativeEffect->processDoubleReplacing(m_nativeEffect, inPointers, outPointers, sampleFrames);
} __except(EXCEPTION_EXECUTE_HANDLER)
{
SendErrorMessage(L"Exception in processDoubleReplacing()!");
}
}
}
// Helper function to build the pointer arrays required by the VST process functions.
template <typename buf_t>
int32 PluginBridge::BuildProcessPointers(buf_t **(&inPointers), buf_t **(&outPointers))
{
MPT_ASSERT(m_processMem.Good());
ProcessMsg &msg = *m_processMem.Data<ProcessMsg>();
const size_t numPtrs = msg.numInputs + msg.numOutputs;
m_sampleBuffers.resize(numPtrs, 0);
if(numPtrs)
{
buf_t *offset = reinterpret_cast<buf_t *>(&msg + 1);
for(size_t i = 0; i < numPtrs; i++)
{
m_sampleBuffers[i] = offset;
offset += msg.sampleFrames;
}
inPointers = reinterpret_cast<buf_t **>(m_sampleBuffers.data());
outPointers = inPointers + msg.numInputs;
}
return msg.sampleFrames;
}
// Send a message to the host.
intptr_t PluginBridge::DispatchToHost(VstOpcodeToHost opcode, int32 index, intptr_t value, void *ptr, float opt)
{
std::vector<char> dispatchData(sizeof(DispatchMsg), 0);
int64 ptrOut = 0;
char *ptrC = static_cast<char *>(ptr);
switch(opcode)
{
case audioMasterAutomate:
case audioMasterVersion:
case audioMasterCurrentId:
case audioMasterIdle:
case audioMasterPinConnected:
break;
case audioMasterWantMidi:
return 1;
case audioMasterGetTime:
// VstTimeInfo* in [return value]
if(m_isProcessing)
{
// During processing, read the cached time info. It won't change during the call
// and we can save some valuable inter-process calls that way.
return ToIntPtr<VstTimeInfo>(&m_sharedMem->timeInfo);
}
break;
case audioMasterProcessEvents:
// VstEvents* in [ptr]
if(ptr == nullptr)
return 0;
TranslateVstEventsToBridge(dispatchData, *static_cast<VstEvents *>(ptr), m_otherPtrSize);
ptrOut = dispatchData.size() - sizeof(DispatchMsg);
// If we are currently processing, try to return the events as part of the process call
if(m_isAudioThread && m_isProcessing && m_eventMem.Good())
{
auto *memBytes = m_eventMem.Data<char>();
auto *eventBytes = m_eventMem.Data<char>() + sizeof(int32);
int32 &memNumEvents = *m_eventMem.Data<int32>();
const auto memEventsSize = BridgeVstEventsSize(memBytes);
if(m_eventMem.Size() >= static_cast<size_t>(ptrOut) + memEventsSize)
{
// Enough shared memory for possibly pre-existing and new events; add new events at the end
memNumEvents += static_cast<VstEvents *>(ptr)->numEvents;
std::memcpy(eventBytes + memEventsSize, dispatchData.data() + sizeof(DispatchMsg) + sizeof(int32), static_cast<size_t>(ptrOut) - sizeof(int32));
return 1;
} else if(memNumEvents)
{
// Not enough memory; merge what we have and what we want to add so that it arrives in the correct order
dispatchData.insert(dispatchData.begin() + sizeof(DispatchMsg) + sizeof(int32), eventBytes, eventBytes + memEventsSize);
*reinterpret_cast<int32 *>(dispatchData.data() + sizeof(DispatchMsg)) += memNumEvents;
memNumEvents = 0;
ptrOut += memEventsSize;
}
}
break;
case audioMasterSetTime:
case audioMasterTempoAt:
case audioMasterGetNumAutomatableParameters:
case audioMasterGetParameterQuantization:
break;
case audioMasterVendorSpecific:
if(index != kVendorOpenMPT || value != kUpdateProcessingBuffer)
{
if(ptr != 0)
{
// Cannot translate this.
return 0;
}
break;
}
[[fallthrough]];
case audioMasterIOChanged:
// We need to be sure that the new values are known to the master.
if(m_nativeEffect != nullptr)
{
UpdateEffectStruct();
CreateProcessingFile(dispatchData);
ptrOut = dispatchData.size() - sizeof(DispatchMsg);
}
break;
case audioMasterNeedIdle:
m_needIdle = true;
return 1;
case audioMasterSizeWindow:
if(m_window)
{
SetWindowPos(m_window, nullptr, 0, 0, index, static_cast<int>(value), SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
break;
case audioMasterGetSampleRate:
case audioMasterGetBlockSize:
case audioMasterGetInputLatency:
case audioMasterGetOutputLatency:
break;
case audioMasterGetPreviousPlug:
case audioMasterGetNextPlug:
// Don't even bother, this would explode :)
return 0;
case audioMasterWillReplaceOrAccumulate:
case audioMasterGetCurrentProcessLevel:
case audioMasterGetAutomationState:
break;
case audioMasterOfflineStart:
case audioMasterOfflineRead:
case audioMasterOfflineWrite:
case audioMasterOfflineGetCurrentPass:
case audioMasterOfflineGetCurrentMetaPass:
// Currently not supported in OpenMPT
return 0;
case audioMasterSetOutputSampleRate:
break;
case audioMasterGetOutputSpeakerArrangement:
case audioMasterGetInputSpeakerArrangement:
// VstSpeakerArrangement* in [return value]
ptrOut = sizeof(VstSpeakerArrangement);
break;
case audioMasterGetVendorString:
case audioMasterGetProductString:
// Name in [ptr]
ptrOut = 256;
break;
case audioMasterGetVendorVersion:
case audioMasterSetIcon:
break;
case audioMasterCanDo:
// Name in [ptr]
ptrOut = strlen(ptrC) + 1;
dispatchData.insert(dispatchData.end(), ptrC, ptrC + ptrOut);
break;
case audioMasterGetLanguage:
case audioMasterOpenWindow:
case audioMasterCloseWindow:
break;
case audioMasterGetDirectory:
// Name in [return value]
ptrOut = 256;
break;
case audioMasterUpdateDisplay:
case audioMasterBeginEdit:
case audioMasterEndEdit:
break;
case audioMasterOpenFileSelector:
// VstFileSelect* in [ptr]
if(ptr != nullptr)
{
auto &fileSel = *static_cast<VstFileSelect *>(ptr);
fileSel.returnMultiplePaths = nullptr;
fileSel.numReturnPaths = 0;
TranslateVstFileSelectToBridge(dispatchData, fileSel, m_otherPtrSize);
ptrOut = dispatchData.size() - sizeof(DispatchMsg) + 65536; // enough space for return paths
}
break;
case audioMasterCloseFileSelector:
// VstFileSelect* in [ptr]
if(auto *fileSel = static_cast<VstFileSelect *>(ptr); fileSel != nullptr && fileSel->reserved == 1)
{
fileSel->returnPath = nullptr;
fileSel->returnMultiplePaths = nullptr;
}
m_fileSelectCache.clear();
m_fileSelectCache.shrink_to_fit();
return 1;
case audioMasterEditFile:
break;
case audioMasterGetChunkFile:
// Name in [ptr]
ptrOut = 256;
dispatchData.insert(dispatchData.end(), ptrC, ptrC + ptrOut);
break;
default:
#ifdef MPT_BUILD_DEBUG
if(ptr != nullptr)
__debugbreak();
#endif
break;
}
if(ptrOut != 0)
{
// In case we only reserve space and don't copy stuff over...
dispatchData.resize(sizeof(DispatchMsg) + static_cast<size_t>(ptrOut), 0);
}
uint32 extraSize = static_cast<uint32>(dispatchData.size() - sizeof(DispatchMsg));
// Create message header
BridgeMessage *msg = reinterpret_cast<BridgeMessage *>(dispatchData.data());
msg->Dispatch(opcode, index, value, ptrOut, opt, extraSize);
const bool useAuxMem = dispatchData.size() > sizeof(BridgeMessage);
MappedMemory auxMem;
if(useAuxMem)
{
// Extra data doesn't fit in message - use secondary memory
wchar_t auxMemName[64];
static_assert(sizeof(DispatchMsg) + sizeof(auxMemName) <= sizeof(BridgeMessage), "Check message sizes, this will crash!");
swprintf(auxMemName, std::size(auxMemName), L"Local\\openmpt-%u-auxmem-%u", GetCurrentProcessId(), GetCurrentThreadId());
if(auxMem.Create(auxMemName, extraSize))
{
// Move message data to shared memory and then move shared memory name to message data
std::memcpy(auxMem.Data(), &dispatchData[sizeof(DispatchMsg)], extraSize);
std::memcpy(&dispatchData[sizeof(DispatchMsg)], auxMemName, sizeof(auxMemName));
} else
{
return 0;
}
}
//std::cout << "about to dispatch " << opcode << " to host...";
//std::flush(std::cout);
if(!SendToHost(*msg))
{
return 0;
}
//std::cout << "done." << std::endl;
const DispatchMsg *resultMsg = &msg->dispatch;
const char *extraData = useAuxMem ? auxMem.Data<const char>() : reinterpret_cast<const char *>(resultMsg + 1);
// Post-fix some opcodes
switch(opcode)
{
case audioMasterGetTime:
// VstTimeInfo* in [return value]
return ToIntPtr<VstTimeInfo>(&m_sharedMem->timeInfo);
case audioMasterGetOutputSpeakerArrangement:
case audioMasterGetInputSpeakerArrangement:
// VstSpeakerArrangement* in [return value]
std::memcpy(&m_host2PlugMem.speakerArrangement, extraData, sizeof(VstSpeakerArrangement));
return ToIntPtr<VstSpeakerArrangement>(&m_host2PlugMem.speakerArrangement);
case audioMasterGetVendorString:
case audioMasterGetProductString:
// Name in [ptr]
strcpy(ptrC, extraData);
break;
case audioMasterGetDirectory:
// Name in [return value]
strncpy(m_host2PlugMem.name, extraData, std::size(m_host2PlugMem.name) - 1);
m_host2PlugMem.name[std::size(m_host2PlugMem.name) - 1] = 0;
return ToIntPtr<char>(m_host2PlugMem.name);
case audioMasterOpenFileSelector:
if(resultMsg->result != 0 && ptr != nullptr)
{
TranslateBridgeToVstFileSelect(m_fileSelectCache, extraData, static_cast<size_t>(ptrOut));
auto &fileSel = *static_cast<VstFileSelect *>(ptr);
const auto &fileSelResult = *reinterpret_cast<const VstFileSelect *>(m_fileSelectCache.data());
if((fileSel.command == kVstFileLoad || fileSel.command == kVstFileSave))
{
if(FourCC("VOPM") == m_nativeEffect->uniqueID)
{
fileSel.sizeReturnPath = _MAX_PATH;
}
} else if(fileSel.command == kVstDirectorySelect)
{
if(FourCC("VSTr") == m_nativeEffect->uniqueID && fileSel.returnPath != nullptr && fileSel.sizeReturnPath == 0)
{
// Old versions of reViSiT (which still relied on the host's file selector) seem to be dodgy.
// They report a path size of 0, but when using an own buffer, they will crash.
// So we'll just assume that reViSiT can handle long enough (_MAX_PATH) paths here.
fileSel.sizeReturnPath = static_cast<int32>(strlen(fileSelResult.returnPath) + 1);
}
}
fileSel.numReturnPaths = fileSelResult.numReturnPaths;
fileSel.reserved = 1;
if(fileSel.command == kVstMultipleFilesLoad)
{
fileSel.returnMultiplePaths = fileSelResult.returnMultiplePaths;
} else if(fileSel.returnPath != nullptr && fileSel.sizeReturnPath != 0)
{
// Plugin provides memory
const auto len = strnlen(fileSelResult.returnPath, fileSel.sizeReturnPath - 1);
strncpy(fileSel.returnPath, fileSelResult.returnPath, len);
fileSel.returnPath[len] = '\0';
fileSel.reserved = 0;
} else
{
fileSel.returnPath = fileSelResult.returnPath;
fileSel.sizeReturnPath = fileSelResult.sizeReturnPath;
}
}
break;
case audioMasterGetChunkFile:
// Name in [ptr]
strcpy(ptrC, extraData);
break;
}
return static_cast<intptr_t>(resultMsg->result);
}
// Helper function for sending messages to the host.
intptr_t VSTCALLBACK PluginBridge::MasterCallback(AEffect *effect, VstOpcodeToHost opcode, int32 index, intptr_t value, void *ptr, float opt)
{
PluginBridge *instance = (effect != nullptr && effect->reservedForHost1 != nullptr) ? static_cast<PluginBridge *>(effect->reservedForHost1) : PluginBridge::m_latestInstance;
return instance->DispatchToHost(opcode, index, value, ptr, opt);
}
LRESULT CALLBACK PluginBridge::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PluginBridge *that = nullptr;
if(hwnd == m_communicationWindow && wParam < m_plugins.size())
that = static_cast<PluginBridge *>(m_plugins[wParam]);
else // Editor windows
that = reinterpret_cast<PluginBridge *>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if(that == nullptr)
return DefWindowProc(hwnd, uMsg, wParam, lParam);
switch(uMsg)
{
case WM_ERASEBKGND:
// Pretend that we erased the background
return 1;
case WM_SIZE:
{
// For plugins that change their size but do not notify the host, e.g. Roland D-50
RECT rect{0, 0, 0, 0};
GetClientRect(hwnd, &rect);
const int width = rect.right - rect.left, height = rect.bottom - rect.top;
if(width > 0 && height > 0 && (width != that->m_windowWidth || height != that->m_windowHeight))
{
that->m_windowWidth = width;
that->m_windowHeight = height;
that->DispatchToHost(audioMasterSizeWindow, width, height, nullptr, 0.0f);
}
break;
}
case WM_BRIDGE_MESSAGE_TO_BRIDGE:
that->ParseNextMessage(static_cast<int>(lParam));
return WM_BRIDGE_SUCCESS;
case WM_BRIDGE_DELETE_PLUGIN:
delete that;
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void PluginBridge::IdleTimerProc(HWND, UINT, UINT_PTR idTimer, DWORD)
{
if(idTimer != TIMER_IDLE)
return;
for(auto *plugin : m_plugins)
{
auto *that = static_cast<PluginBridge *>(plugin);
if(that == nullptr)
continue;
if(that->m_needIdle)
{
that->Dispatch(effIdle, 0, 0, nullptr, 0.0f);
that->m_needIdle = false;
}
if(that->m_window)
{
that->Dispatch(effEditIdle, 0, 0, nullptr, 0.0f);
}
}
}
OPENMPT_NAMESPACE_END
|