/
DistrhoUIVST3.cpp
1646 lines (1337 loc) · 55.6 KB
/
DistrhoUIVST3.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
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "DistrhoUIInternal.hpp"
#include "travesty/base.h"
#include "travesty/edit_controller.h"
#include "travesty/host.h"
#include "travesty/view.h"
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
# if defined(DISTRHO_OS_MAC)
# include <CoreFoundation/CoreFoundation.h>
# elif defined(DISTRHO_OS_WINDOWS)
# include <winuser.h>
# define DPF_VST3_WIN32_TIMER_ID 1
# endif
#endif
/* TODO items:
* - mousewheel event
* - file request?
*/
#if !(defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS))
# define DPF_VST3_USING_HOST_RUN_LOOP 1
#else
# define DPF_VST3_USING_HOST_RUN_LOOP 0
#endif
#ifndef DPF_VST3_TIMER_INTERVAL
# define DPF_VST3_TIMER_INTERVAL 16 /* ~60 fps */
#endif
START_NAMESPACE_DISTRHO
// --------------------------------------------------------------------------------------------------------------------
#if ! DISTRHO_PLUGIN_WANT_STATE
static constexpr const setStateFunc setStateCallback = nullptr;
#endif
#if ! DISTRHO_PLUGIN_WANT_MIDI_INPUT
static constexpr const sendNoteFunc sendNoteCallback = nullptr;
#endif
// --------------------------------------------------------------------------------------------------------------------
// Static data, see DistrhoPlugin.cpp
extern const char* d_nextBundlePath;
// --------------------------------------------------------------------------------------------------------------------
// Utility functions (defined on plugin side)
const char* tuid2str(const v3_tuid iid);
// --------------------------------------------------------------------------------------------------------------------
static void applyGeometryConstraints(const uint minimumWidth,
const uint minimumHeight,
const bool keepAspectRatio,
v3_view_rect* const rect)
{
d_debug("applyGeometryConstraints %u %u %d {%d,%d,%d,%d} | BEFORE",
minimumWidth, minimumHeight, keepAspectRatio, rect->top, rect->left, rect->right, rect->bottom);
const int32_t minWidth = static_cast<int32_t>(minimumWidth);
const int32_t minHeight = static_cast<int32_t>(minimumHeight);
if (keepAspectRatio)
{
if (rect->right < 1)
rect->right = 1;
if (rect->bottom < 1)
rect->bottom = 1;
const double ratio = static_cast<double>(minWidth) / static_cast<double>(minHeight);
const double reqRatio = static_cast<double>(rect->right) / static_cast<double>(rect->bottom);
if (d_isNotEqual(ratio, reqRatio))
{
// fix width
if (reqRatio > ratio)
rect->right = d_roundToIntPositive(rect->bottom * ratio);
// fix height
else
rect->bottom = d_roundToIntPositive(static_cast<double>(rect->right) / ratio);
}
}
if (minWidth > rect->right)
rect->right = minWidth;
if (minHeight > rect->bottom)
rect->bottom = minHeight;
d_debug("applyGeometryConstraints %u %u %d {%d,%d,%d,%d} | AFTER",
minimumWidth, minimumHeight, keepAspectRatio, rect->top, rect->left, rect->right, rect->bottom);
}
// --------------------------------------------------------------------------------------------------------------------
#if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
static uint translateVST3Modifiers(const int64_t modifiers) noexcept
{
using namespace DGL_NAMESPACE;
uint dglmods = 0;
if (modifiers & (1 << 0))
dglmods |= kModifierShift;
if (modifiers & (1 << 1))
dglmods |= kModifierAlt;
#ifdef DISTRHO_OS_MAC
if (modifiers & (1 << 2))
dglmods |= kModifierSuper;
if (modifiers & (1 << 3))
dglmods |= kModifierControl;
#else
if (modifiers & (1 << 2))
dglmods |= kModifierControl;
if (modifiers & (1 << 3))
dglmods |= kModifierSuper;
#endif
return dglmods;
}
#endif
// --------------------------------------------------------------------------------------------------------------------
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI && !DPF_VST3_USING_HOST_RUN_LOOP
/**
* Helper class for getting a native idle timer via native APIs.
*/
class NativeIdleHelper
{
public:
NativeIdleHelper(IdleCallback* const callback)
: fCallback(callback),
#ifdef DISTRHO_OS_MAC
fTimerRef(nullptr)
#else
fTimerWindow(nullptr),
fTimerWindowClassName()
#endif
{
}
void registerNativeIdleCallback()
{
#ifdef DISTRHO_OS_MAC
constexpr const CFTimeInterval interval = DPF_VST3_TIMER_INTERVAL * 0.0001;
CFRunLoopTimerContext context = {};
context.info = this;
fTimerRef = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0,
platformIdleTimerCallback, &context);
DISTRHO_SAFE_ASSERT_RETURN(fTimerRef != nullptr,);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), fTimerRef, kCFRunLoopCommonModes);
#else
/*
* Create an invisible window to handle a timer.
* There is no need for implementing a window proc because DefWindowProc already calls the
* callback function when processing WM_TIMER messages.
*/
fTimerWindowClassName = (
#ifdef DISTRHO_PLUGIN_BRAND
DISTRHO_PLUGIN_BRAND
#else
DISTRHO_MACRO_AS_STRING(DISTRHO_NAMESPACE)
#endif
"-" DISTRHO_PLUGIN_NAME "-"
);
char suffix[9];
std::snprintf(suffix, sizeof(suffix), "%08x", std::rand());
suffix[sizeof(suffix)-1] = '\0';
fTimerWindowClassName += suffix;
WNDCLASSEX cls;
ZeroMemory(&cls, sizeof(cls));
cls.cbSize = sizeof(WNDCLASSEX);
cls.cbWndExtra = sizeof(LONG_PTR);
cls.lpszClassName = fTimerWindowClassName.buffer();
cls.lpfnWndProc = DefWindowProc;
RegisterClassEx(&cls);
fTimerWindow = CreateWindowEx(0, cls.lpszClassName, "DPF Timer Helper",
0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, nullptr, nullptr);
DISTRHO_SAFE_ASSERT_RETURN(fTimerWindow != nullptr,);
SetWindowLongPtr(fTimerWindow, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(static_cast<void*>(this)));
SetTimer(fTimerWindow, DPF_VST3_WIN32_TIMER_ID, DPF_VST3_TIMER_INTERVAL,
static_cast<TIMERPROC>(platformIdleTimerCallback));
#endif
}
void unregisterNativeIdleCallback()
{
#ifdef DISTRHO_OS_MAC
CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), fTimerRef, kCFRunLoopCommonModes);
CFRelease(fTimerRef);
#else
DISTRHO_SAFE_ASSERT_RETURN(fTimerWindow != nullptr,);
KillTimer(fTimerWindow, DPF_VST3_WIN32_TIMER_ID);
DestroyWindow(fTimerWindow);
UnregisterClass(fTimerWindowClassName, nullptr);
#endif
}
private:
IdleCallback* const fCallback;
#ifdef DISTRHO_OS_MAC
CFRunLoopTimerRef fTimerRef;
static void platformIdleTimerCallback(CFRunLoopTimerRef, void* const info)
{
static_cast<NativeIdleHelper*>(info)->fCallback->idleCallback();
}
#else
HWND fTimerWindow;
String fTimerWindowClassName;
static void WINAPI platformIdleTimerCallback(const HWND hwnd, UINT, UINT_PTR, DWORD)
{
reinterpret_cast<NativeIdleHelper*>(GetWindowLongPtr(hwnd, GWLP_USERDATA))->fCallback->idleCallback();
}
#endif
};
#endif
/**
* Helper class for getting a native idle timer, either through pugl or via native APIs.
*/
#if !DPF_VST3_USING_HOST_RUN_LOOP
class NativeIdleCallback : public IdleCallback
{
public:
NativeIdleCallback(UIExporter& ui)
: fCallbackRegistered(false),
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
fIdleHelper(this)
#else
fUI(ui)
#endif
{
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
// unused
(void)ui;
#endif
}
void registerNativeIdleCallback()
{
DISTRHO_SAFE_ASSERT_RETURN(!fCallbackRegistered,);
fCallbackRegistered = true;
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
fIdleHelper.registerNativeIdleCallback();
#else
fUI.addIdleCallbackForNativeIdle(this, DPF_VST3_TIMER_INTERVAL);
#endif
}
void unregisterNativeIdleCallback()
{
DISTRHO_SAFE_ASSERT_RETURN(fCallbackRegistered,);
fCallbackRegistered = false;
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
fIdleHelper.unregisterNativeIdleCallback();
#else
fUI.removeIdleCallbackForNativeIdle(this);
#endif
}
private:
bool fCallbackRegistered;
#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
NativeIdleHelper fIdleHelper;
#else
UIExporter& fUI;
#endif
};
#endif
// --------------------------------------------------------------------------------------------------------------------
/**
* VST3 UI class.
*
* All the dynamic things from VST3 get implemented here, free of complex low-level VST3 pointer things.
* The UI is created during the "attach" view event, and destroyed during "removed".
*
* The low-level VST3 stuff comes after.
*/
class UIVst3
#if !DPF_VST3_USING_HOST_RUN_LOOP
: public NativeIdleCallback
#endif
{
public:
UIVst3(v3_plugin_view** const view,
v3_host_application** const host,
v3_connection_point** const connection,
v3_plugin_frame** const frame,
const intptr_t winId,
const float scaleFactor,
const double sampleRate,
void* const instancePointer,
const bool willResizeFromHost,
const bool needsResizeFromPlugin)
:
#if !DPF_VST3_USING_HOST_RUN_LOOP
NativeIdleCallback(fUI),
#endif
fView(view),
fHostApplication(host),
fConnection(connection),
fFrame(frame),
fScaleFactor(scaleFactor),
fReadyForPluginData(false),
fIsResizingFromPlugin(false),
fIsResizingFromHost(willResizeFromHost),
fNeedsResizeFromPlugin(needsResizeFromPlugin),
fNextPluginRect(),
fUI(this, winId, sampleRate,
editParameterCallback,
setParameterCallback,
setStateCallback,
sendNoteCallback,
setSizeCallback,
nullptr, // TODO file request
d_nextBundlePath,
instancePointer,
scaleFactor)
{
}
~UIVst3()
{
#if !DPF_VST3_USING_HOST_RUN_LOOP
unregisterNativeIdleCallback();
#endif
if (fConnection != nullptr)
disconnect();
}
void postInit(uint32_t nextWidth, uint32_t nextHeight)
{
if (fIsResizingFromHost && nextWidth > 0 && nextHeight > 0)
{
#ifdef DISTRHO_OS_MAC
const double scaleFactor = fUI.getScaleFactor();
nextWidth *= scaleFactor;
nextHeight *= scaleFactor;
#endif
if (fUI.getWidth() != nextWidth || fUI.getHeight() != nextHeight)
{
d_debug("postInit sets new size as %u %u", nextWidth, nextHeight);
fUI.setWindowSizeFromHost(nextWidth, nextHeight);
}
}
else if (fNeedsResizeFromPlugin)
{
d_debug("postInit forcely sets size from plugin as %u %u", fUI.getWidth(), fUI.getHeight());
setSize(fUI.getWidth(), fUI.getHeight());
}
if (fConnection != nullptr)
connect(fConnection);
#if !DPF_VST3_USING_HOST_RUN_LOOP
registerNativeIdleCallback();
#endif
}
// ----------------------------------------------------------------------------------------------------------------
// v3_plugin_view interface calls
#if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
v3_result onWheel(float /*distance*/)
{
// TODO
return V3_NOT_IMPLEMENTED;
}
v3_result onKeyDown(const int16_t keychar, const int16_t keycode, const int16_t modifiers)
{
DISTRHO_SAFE_ASSERT_INT_RETURN(keychar >= 0 && keychar < 0x7f, keychar, V3_FALSE);
bool special;
const uint key = translateVstKeyCode(special, keychar, keycode);
d_debug("onKeyDown %d %d %x -> %d %d", keychar, keycode, modifiers, special, key);
return fUI.handlePluginKeyboardVST(true, special, key,
keycode >= 0 ? static_cast<uint>(keycode) : 0,
translateVST3Modifiers(modifiers)) ? V3_TRUE : V3_FALSE;
}
v3_result onKeyUp(const int16_t keychar, const int16_t keycode, const int16_t modifiers)
{
DISTRHO_SAFE_ASSERT_INT_RETURN(keychar >= 0 && keychar < 0x7f, keychar, V3_FALSE);
bool special;
const uint key = translateVstKeyCode(special, keychar, keycode);
d_debug("onKeyUp %d %d %x -> %d %d", keychar, keycode, modifiers, special, key);
return fUI.handlePluginKeyboardVST(false, special, key,
keycode >= 0 ? static_cast<uint>(keycode) : 0,
translateVST3Modifiers(modifiers)) ? V3_TRUE : V3_FALSE;
}
v3_result onFocus(const bool state)
{
if (state)
fUI.focus();
fUI.notifyFocusChanged(state);
return V3_OK;
}
#endif
v3_result getSize(v3_view_rect* const rect) const noexcept
{
if (fIsResizingFromPlugin)
{
*rect = fNextPluginRect;
}
else
{
rect->left = rect->top = 0;
rect->right = fUI.getWidth();
rect->bottom = fUI.getHeight();
#ifdef DISTRHO_OS_MAC
const double scaleFactor = fUI.getScaleFactor();
rect->right /= scaleFactor;
rect->bottom /= scaleFactor;
#endif
}
d_debug("getSize request returning %i %i", rect->right, rect->bottom);
return V3_OK;
}
v3_result onSize(v3_view_rect* const orect)
{
v3_view_rect rect = *orect;
#ifdef DISTRHO_OS_MAC
const double scaleFactor = fUI.getScaleFactor();
rect.top *= scaleFactor;
rect.left *= scaleFactor;
rect.right *= scaleFactor;
rect.bottom *= scaleFactor;
#endif
if (fIsResizingFromPlugin)
{
d_debug("host->plugin onSize request %i %i (plugin resize was active, unsetting now)",
rect.right - rect.left, rect.bottom - rect.top);
fIsResizingFromPlugin = false;
}
else
{
d_debug("host->plugin onSize request %i %i (OK)", rect.right - rect.left, rect.bottom - rect.top);
}
fIsResizingFromHost = true;
fUI.setWindowSizeFromHost(rect.right - rect.left, rect.bottom - rect.top);
return V3_OK;
}
v3_result setFrame(v3_plugin_frame** const frame) noexcept
{
fFrame = frame;
return V3_OK;
}
v3_result canResize() noexcept
{
return fUI.isResizable() ? V3_TRUE : V3_FALSE;
}
v3_result checkSizeConstraint(v3_view_rect* const rect)
{
uint minimumWidth, minimumHeight;
bool keepAspectRatio;
fUI.getGeometryConstraints(minimumWidth, minimumHeight, keepAspectRatio);
#ifdef DISTRHO_OS_MAC
const double scaleFactor = fUI.getScaleFactor();
minimumWidth /= scaleFactor;
minimumHeight /= scaleFactor;
#endif
applyGeometryConstraints(minimumWidth, minimumHeight, keepAspectRatio, rect);
return V3_TRUE;
}
// ----------------------------------------------------------------------------------------------------------------
// v3_connection_point interface calls
void connect(v3_connection_point** const point) noexcept
{
DISTRHO_SAFE_ASSERT_RETURN(point != nullptr,);
fConnection = point;
d_debug("requesting current plugin state");
v3_message** const message = createMessage("init");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
}
void disconnect() noexcept
{
DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
d_debug("reporting UI closed");
fReadyForPluginData = false;
v3_message** const message = createMessage("close");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
fConnection = nullptr;
}
v3_result notify(v3_message** const message)
{
const char* const msgid = v3_cpp_obj(message)->get_message_id(message);
DISTRHO_SAFE_ASSERT_RETURN(msgid != nullptr, V3_INVALID_ARG);
v3_attribute_list** const attrs = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrs != nullptr, V3_INVALID_ARG);
if (std::strcmp(msgid, "ready") == 0)
{
DISTRHO_SAFE_ASSERT_RETURN(! fReadyForPluginData, V3_INTERNAL_ERR);
fReadyForPluginData = true;
return V3_OK;
}
if (std::strcmp(msgid, "parameter-set") == 0)
{
int64_t rindex;
double value;
v3_result res;
res = v3_cpp_obj(attrs)->get_int(attrs, "rindex", &rindex);
DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
res = v3_cpp_obj(attrs)->get_float(attrs, "value", &value);
DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
if (rindex < kVst3InternalParameterBaseCount)
{
switch (rindex)
{
#if DPF_VST3_USES_SEPARATE_CONTROLLER
case kVst3InternalParameterSampleRate:
DISTRHO_SAFE_ASSERT_RETURN(value >= 0.0, V3_INVALID_ARG);
fUI.setSampleRate(value, true);
break;
#endif
#if DISTRHO_PLUGIN_WANT_PROGRAMS
case kVst3InternalParameterProgram:
DISTRHO_SAFE_ASSERT_RETURN(value >= 0.0, V3_INVALID_ARG);
fUI.programLoaded(static_cast<uint32_t>(value + 0.5));
break;
#endif
}
// others like latency and buffer-size do not matter on UI side
return V3_OK;
}
DISTRHO_SAFE_ASSERT_UINT2_RETURN(rindex >= kVst3InternalParameterCount, rindex, kVst3InternalParameterCount, V3_INVALID_ARG);
const uint32_t index = static_cast<uint32_t>(rindex - kVst3InternalParameterCount);
fUI.parameterChanged(index, value);
return V3_OK;
}
#if DISTRHO_PLUGIN_WANT_STATE
if (std::strcmp(msgid, "state-set") == 0)
{
int64_t keyLength = -1;
int64_t valueLength = -1;
v3_result res;
res = v3_cpp_obj(attrs)->get_int(attrs, "key:length", &keyLength);
DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
DISTRHO_SAFE_ASSERT_INT_RETURN(keyLength >= 0, keyLength, V3_INTERNAL_ERR);
res = v3_cpp_obj(attrs)->get_int(attrs, "value:length", &valueLength);
DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_OK, res, res);
DISTRHO_SAFE_ASSERT_INT_RETURN(valueLength >= 0, valueLength, V3_INTERNAL_ERR);
int16_t* const key16 = (int16_t*)std::malloc(sizeof(int16_t)*(keyLength + 1));
DISTRHO_SAFE_ASSERT_RETURN(key16 != nullptr, V3_NOMEM);
int16_t* const value16 = (int16_t*)std::malloc(sizeof(int16_t)*(valueLength + 1));
DISTRHO_SAFE_ASSERT_RETURN(value16 != nullptr, V3_NOMEM);
res = v3_cpp_obj(attrs)->get_string(attrs, "key", key16, sizeof(int16_t)*(keyLength+1));
DISTRHO_SAFE_ASSERT_INT2_RETURN(res == V3_OK, res, keyLength, res);
if (valueLength != 0)
{
res = v3_cpp_obj(attrs)->get_string(attrs, "value", value16, sizeof(int16_t)*(valueLength+1));
DISTRHO_SAFE_ASSERT_INT2_RETURN(res == V3_OK, res, valueLength, res);
}
// do cheap inline conversion
char* const key = (char*)key16;
char* const value = (char*)value16;
for (int64_t i=0; i<keyLength; ++i)
key[i] = key16[i];
for (int64_t i=0; i<valueLength; ++i)
value[i] = value16[i];
key[keyLength] = '\0';
value[valueLength] = '\0';
fUI.stateChanged(key, value);
std::free(key16);
std::free(value16);
return V3_OK;
}
#endif
d_stderr("UIVst3 received unknown msg '%s'", msgid);
return V3_NOT_IMPLEMENTED;
}
// ----------------------------------------------------------------------------------------------------------------
// v3_plugin_view_content_scale_steinberg interface calls
v3_result setContentScaleFactor(const float factor)
{
if (d_isEqual(fScaleFactor, factor))
return V3_OK;
fScaleFactor = factor;
fUI.notifyScaleFactorChanged(factor);
return V3_OK;
}
#if DPF_VST3_USING_HOST_RUN_LOOP
// ----------------------------------------------------------------------------------------------------------------
// v3_timer_handler interface calls
void onTimer()
{
fUI.plugin_idle();
doIdleStuff();
}
#else
// ----------------------------------------------------------------------------------------------------------------
// special idle callback without v3_timer_handler
void idleCallback() override
{
fUI.idleFromNativeIdle();
doIdleStuff();
}
#endif
void doIdleStuff()
{
if (fReadyForPluginData)
{
fReadyForPluginData = false;
requestMorePluginData();
}
if (fNeedsResizeFromPlugin)
{
fNeedsResizeFromPlugin = false;
d_debug("first resize forced behaviour is now stopped");
}
if (fIsResizingFromHost)
{
fIsResizingFromHost = false;
d_debug("was resizing from host, now stopped");
}
if (fIsResizingFromPlugin)
{
fIsResizingFromPlugin = false;
d_debug("was resizing from plugin, now stopped");
}
}
// ----------------------------------------------------------------------------------------------------------------
private:
// VST3 stuff
v3_plugin_view** const fView;
v3_host_application** const fHostApplication;
v3_connection_point** fConnection;
v3_plugin_frame** fFrame;
// Temporary data
float fScaleFactor;
bool fReadyForPluginData;
bool fIsResizingFromPlugin;
bool fIsResizingFromHost;
bool fNeedsResizeFromPlugin;
v3_view_rect fNextPluginRect; // for when plugin requests a new size
// Plugin UI (after VST3 stuff so the UI can call into us during its constructor)
UIExporter fUI;
// ----------------------------------------------------------------------------------------------------------------
// helper functions called during message passing
v3_message** createMessage(const char* const id) const
{
DISTRHO_SAFE_ASSERT_RETURN(fHostApplication != nullptr, nullptr);
v3_tuid iid;
std::memcpy(iid, v3_message_iid, sizeof(v3_tuid));
v3_message** msg = nullptr;
const v3_result res = v3_cpp_obj(fHostApplication)->create_instance(fHostApplication, iid, iid, (void**)&msg);
DISTRHO_SAFE_ASSERT_INT_RETURN(res == V3_TRUE, res, nullptr);
DISTRHO_SAFE_ASSERT_RETURN(msg != nullptr, nullptr);
v3_cpp_obj(msg)->set_message_id(msg, id);
return msg;
}
void requestMorePluginData() const
{
DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
v3_message** const message = createMessage("idle");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
}
// ----------------------------------------------------------------------------------------------------------------
// DPF callbacks
void editParameter(const uint32_t rindex, const bool started) const
{
DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
v3_message** const message = createMessage("parameter-edit");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(attrlist)->set_int(attrlist, "rindex", rindex);
v3_cpp_obj(attrlist)->set_int(attrlist, "started", started ? 1 : 0);
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
}
static void editParameterCallback(void* const ptr, const uint32_t rindex, const bool started)
{
static_cast<UIVst3*>(ptr)->editParameter(rindex, started);
}
void setParameterValue(const uint32_t rindex, const float realValue)
{
DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
v3_message** const message = createMessage("parameter-set");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(attrlist)->set_int(attrlist, "rindex", rindex);
v3_cpp_obj(attrlist)->set_float(attrlist, "value", realValue);
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
}
static void setParameterCallback(void* const ptr, const uint32_t rindex, const float value)
{
static_cast<UIVst3*>(ptr)->setParameterValue(rindex, value);
}
#if DISTRHO_PLUGIN_WANT_STATE
void setState(const char* const key, const char* const value)
{
DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
v3_message** const message = createMessage("state-set");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(attrlist)->set_int(attrlist, "key:length", std::strlen(key));
v3_cpp_obj(attrlist)->set_int(attrlist, "value:length", std::strlen(value));
v3_cpp_obj(attrlist)->set_string(attrlist, "key", ScopedUTF16String(key));
v3_cpp_obj(attrlist)->set_string(attrlist, "value", ScopedUTF16String(value));
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
}
static void setStateCallback(void* const ptr, const char* const key, const char* const value)
{
static_cast<UIVst3*>(ptr)->setState(key, value);
}
#endif
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
void sendNote(const uint8_t channel, const uint8_t note, const uint8_t velocity)
{
DISTRHO_SAFE_ASSERT_RETURN(fConnection != nullptr,);
v3_message** const message = createMessage("midi");
DISTRHO_SAFE_ASSERT_RETURN(message != nullptr,);
v3_attribute_list** const attrlist = v3_cpp_obj(message)->get_attributes(message);
DISTRHO_SAFE_ASSERT_RETURN(attrlist != nullptr,);
uint8_t midiData[3];
midiData[0] = (velocity != 0 ? 0x90 : 0x80) | channel;
midiData[1] = note;
midiData[2] = velocity;
v3_cpp_obj(attrlist)->set_int(attrlist, "__dpf_msg_target__", 1);
v3_cpp_obj(attrlist)->set_binary(attrlist, "data", midiData, sizeof(midiData));
v3_cpp_obj(fConnection)->notify(fConnection, message);
v3_cpp_obj_unref(message);
}
static void sendNoteCallback(void* const ptr, const uint8_t channel, const uint8_t note, const uint8_t velocity)
{
static_cast<UIVst3*>(ptr)->sendNote(channel, note, velocity);
}
#endif
void setSize(uint width, uint height)
{
DISTRHO_SAFE_ASSERT_RETURN(fView != nullptr,);
DISTRHO_SAFE_ASSERT_RETURN(fFrame != nullptr,);
DISTRHO_SAFE_ASSERT_RETURN(width != 0 && height != 0,);
#ifdef DISTRHO_OS_MAC
const double scaleFactor = fUI.getScaleFactor();
width /= scaleFactor;
height /= scaleFactor;
#endif
if (fIsResizingFromHost)
{
if (fNeedsResizeFromPlugin)
{
d_debug("plugin->host setSize %u %u (FORCED, exception for first resize)", width, height);
}
else
{
d_debug("plugin->host setSize %u %u (IGNORED, host resize active)", width, height);
return;
}
}
else
{
d_debug("plugin->host setSize %u %u (OK)", width, height);
}
fIsResizingFromPlugin = true;
v3_view_rect rect;
rect.left = rect.top = 0;
rect.right = width;
rect.bottom = height;
fNextPluginRect = rect;
v3_cpp_obj(fFrame)->resize_view(fFrame, fView, &rect);
}
static void setSizeCallback(void* const ptr, const uint width, const uint height)
{
static_cast<UIVst3*>(ptr)->setSize(width, height);
}
};
// --------------------------------------------------------------------------------------------------------------------
/**
* VST3 low-level pointer thingies follow, proceed with care.
*/
// --------------------------------------------------------------------------------------------------------------------
// v3_funknown for classes with a single instance
template<class T>
static uint32_t V3_API dpf_single_instance_ref(void* const self)
{
return ++(*static_cast<T**>(self))->refcounter;
}
template<class T>
static uint32_t V3_API dpf_single_instance_unref(void* const self)
{
return --(*static_cast<T**>(self))->refcounter;
}
// --------------------------------------------------------------------------------------------------------------------
// dpf_ui_connection_point
struct dpf_ui_connection_point : v3_connection_point_cpp {
std::atomic_int refcounter;
ScopedPointer<UIVst3>& uivst3;
v3_connection_point** other;
dpf_ui_connection_point(ScopedPointer<UIVst3>& v)
: refcounter(1),
uivst3(v),
other(nullptr)
{
// v3_funknown, single instance
query_interface = query_interface_connection_point;
ref = dpf_single_instance_ref<dpf_ui_connection_point>;
unref = dpf_single_instance_unref<dpf_ui_connection_point>;
// v3_connection_point
point.connect = connect;
point.disconnect = disconnect;
point.notify = notify;
}
// ----------------------------------------------------------------------------------------------------------------
// v3_funknown
static v3_result V3_API query_interface_connection_point(void* const self, const v3_tuid iid, void** const iface)
{
dpf_ui_connection_point* const point = *static_cast<dpf_ui_connection_point**>(self);
if (v3_tuid_match(iid, v3_funknown_iid) ||
v3_tuid_match(iid, v3_connection_point_iid))
{
d_debug("UI|query_interface_connection_point => %p %s %p | OK", self, tuid2str(iid), iface);
++point->refcounter;
*iface = self;
return V3_OK;
}
d_debug("DSP|query_interface_connection_point => %p %s %p | WARNING UNSUPPORTED", self, tuid2str(iid), iface);
*iface = NULL;
return V3_NO_INTERFACE;
}
// ----------------------------------------------------------------------------------------------------------------