-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathRemoteVstPlugin.cpp
More file actions
2640 lines (2151 loc) · 58.5 KB
/
Copy pathRemoteVstPlugin.cpp
File metadata and controls
2640 lines (2151 loc) · 58.5 KB
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
/*
* RemoteVstPlugin.cpp - LMMS VST Support Layer (RemotePlugin client)
*
* Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of LMMS - https://lmms.io
*
* Code partly taken from (X)FST:
* Copyright (c) 2004 Paul Davis
* Copyright (c) 2004 Torben Hohn
* Copyright (c) 2002 Kjetil S. Matheussen
*
* X11 code partly taken from https://github.com/ekenberg/vstminihost:
* Copyright (c) 2012 Johan Ekenberg
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <cmath>
#include "lmmsconfig.h"
#include "RemotePluginClient.h"
#ifdef LMMS_HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef LMMS_BUILD_LINUX
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifdef LMMS_HAVE_SCHED_H
#include <sched.h>
#endif
#ifndef NATIVE_LINUX_VST
#include <wine/exception.h>
#endif
#endif // LMMS_BUILD_LINUX
#ifndef NATIVE_LINUX_VST
#define USE_WS_PREFIX
#include <windows.h>
#else
#include <dlfcn.h>
#include <pthread.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <sstream>
#include <time.h>
// https://stackoverflow.com/questions/22476110/c-compiling-error-including-x11-x-h-x11-xlib-h
#undef Bool
#undef CursorShape
#undef Expose
#undef KeyPress
#undef KeyRelease
#undef FocusIn
#undef FocusOut
#undef FontChange
#undef None
#undef Status
#undef Unsorted
#endif
#include <mutex>
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <cassert>
#include <aeffectx.h>
#if kVstVersion < 2400
#define OLD_VST_SDK
struct ERect
{
std::int16_t top;
std::int16_t left;
std::int16_t bottom;
std::int16_t right;
};
#endif
#include "LmmsTypes.h"
#include "Midi.h"
#include "communication.h"
#include "IoHelper.h"
#include "VstSyncData.h"
using namespace std;
static lmms::VstHostLanguage hlang = lmms::VstHostLanguage::English;
static bool EMBED = false;
static bool EMBED_X11 = false;
static bool EMBED_WIN32 = false;
static bool HEADLESS = false;
namespace lmms
{
class RemoteVstPlugin;
}
lmms::RemoteVstPlugin * __plugin = nullptr;
#ifndef NATIVE_LINUX_VST
HWND __MessageHwnd = nullptr;
DWORD __processingThreadId = 0;
#else
pthread_t __processingThreadId = 0;
#endif
namespace lmms
{
#ifdef _WIN32
//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetErrorAsString(DWORD errorMessageID)
{
//Get the error message, if any.
if(errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
#endif
class RemoteVstPlugin : public RemotePluginClient
{
public:
#ifdef SYNC_WITH_SHM_FIFO
RemoteVstPlugin( const std::string& _shm_in, const std::string& _shm_out );
#else
RemoteVstPlugin( const char * socketPath );
#endif
virtual ~RemoteVstPlugin();
virtual bool processMessage( const message & _m );
void init( const std::string & _plugin_file );
void initEditor();
void showEditor();
void hideEditor();
void destroyEditor();
virtual void process( const SampleFrame* _in, SampleFrame* _out );
virtual void processMidiEvent( const MidiEvent& event, const f_cnt_t offset );
// set given sample-rate for plugin
virtual void updateSampleRate()
{
SuspendPlugin suspend( this );
pluginDispatch( effSetSampleRate, 0, 0,
nullptr, (float) sampleRate() );
}
// set given buffer-size for plugin
virtual void updateBufferSize()
{
SuspendPlugin suspend( this );
pluginDispatch( effSetBlockSize, 0, bufferSize() );
}
void setResumed( bool resumed )
{
m_resumed = resumed;
pluginDispatch( effMainsChanged, 0, resumed ? 1 : 0 );
}
inline bool isResumed() const
{
return m_resumed;
}
inline bool isInitialized() const
{
return m_initialized;
}
// set given tempo
void setBPM( const bpm_t _bpm )
{
m_bpm = _bpm;
}
// determine VST-version the plugin uses
inline int pluginVersion()
{
return pluginDispatch( effGetVendorVersion );
}
// determine name of plugin
const char * pluginName();
// determine vendor of plugin
const char * pluginVendorString();
// determine product-string of plugin
const char * pluginProductString();
// determine name of current program
const char * programName();
void loadAllParameterDisplays();
void loadAllParameterLabels();
//! Updates a parameter display that has already been loaded
void updateParameterDisplay(int index);
//! Updates a parameter label that has already been loaded
void updateParameterLabel(int index);
// send name of current program back to host
void sendCurrentProgramName();
// do a complete parameter-dump and post it
void getParameterDump();
// read parameter-dump and set it for plugin
void setParameterDump( const message & _m );
// save settings chunk of plugin into file
void saveChunkToFile( const std::string & _file );
// restore settings chunk of plugin from file
void loadChunkFromFile(const std::string& _file, std::size_t _len);
// restore settings chunk of plugin from file
void loadPresetFile( const std::string & _file );
// sets given program index
void setProgram( int index );
// rotate current program by given offset
void rotateProgram( int offset );
// Load names of presets/programs
void getProgramNames();
// Save presets/programs
void savePreset( const std::string & _file );
// number of inputs
virtual int inputCount() const
{
if( m_plugin )
{
return m_plugin->numInputs;
}
return 0;
}
// number of outputs
virtual int outputCount() const
{
if( m_plugin )
{
return m_plugin->numOutputs;
}
return 0;
}
// has to be called as soon as input- or output-count changes
int updateInOutCount();
inline void lockShm()
{
m_shmLock.lock();
}
inline bool tryLockShm()
{
return m_shmLock.try_lock();
}
inline void unlockShm()
{
m_shmLock.unlock();
}
inline bool isShmValid()
{
return m_shmValid;
}
inline void setShmIsValid( bool valid )
{
m_shmValid = valid;
}
inline bool isProcessing() const
{
return m_processing;
}
inline void setProcessing( bool processing )
{
m_processing = processing;
}
inline void queueMessage( const message & m ) {
#ifdef NATIVE_LINUX_VST
pthread_mutex_lock(&message_mutex);
#endif
m_messageList.push( m );
#ifdef NATIVE_LINUX_VST
pthread_mutex_unlock(&message_mutex);
#endif
}
inline bool shouldGiveIdle() const
{
return m_shouldGiveIdle;
}
inline void setShouldGiveIdle( bool shouldGiveIdle )
{
m_shouldGiveIdle = shouldGiveIdle;
}
void idle();
void processUIThreadMessages();
#ifdef NATIVE_LINUX_VST
void sendX11Idle();
#endif
#ifndef NATIVE_LINUX_VST
static DWORD WINAPI processingThread( LPVOID _param );
#else
static void * processingThread( void * _param );
#endif
static bool setupMessageWindow();
#ifndef NATIVE_LINUX_VST
static DWORD WINAPI guiEventLoop();
#else
void guiEventLoop();
#endif
#ifndef NATIVE_LINUX_VST
static LRESULT CALLBACK wndProc( HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam );
#endif
private:
enum class GuiThreadMessage
{
None,
ProcessPluginMessage,
GiveIdle,
ClosePlugin
} ;
struct SuspendPlugin {
SuspendPlugin( RemoteVstPlugin * plugin ) :
m_plugin( plugin ),
m_resumed( plugin->isResumed() )
{
if( m_resumed ) { m_plugin->setResumed( false ); }
}
~SuspendPlugin()
{
if( m_resumed ) { m_plugin->setResumed( true ); }
}
private:
RemoteVstPlugin * m_plugin;
bool m_resumed;
};
// callback used by plugin for being able to communicate with it's host
static intptr_t VST_CALL_CONV hostCallback( AEffect * _effect, int32_t _opcode,
int32_t _index, intptr_t _value,
void * _ptr, float _opt );
bool load( const std::string & _plugin_file );
int pluginDispatch( int cmd, int param1 = 0, int param2 = 0,
void * p = nullptr, float f = 0 )
{
if( m_plugin )
{
return m_plugin->dispatcher( m_plugin, cmd, param1, param2, p, f );
}
return 0;
}
std::string m_shortName;
#ifndef NATIVE_LINUX_VST
HINSTANCE m_libInst;
#else
void* m_libInst = nullptr;
#endif
AEffect * m_plugin;
#ifndef NATIVE_LINUX_VST
HWND m_window = nullptr;
#else
Window m_window = 0;
Display* m_display = nullptr;
Atom m_wmDeleteMessage;
bool m_x11WindowVisible = false;
#endif
intptr_t m_windowID;
int m_windowWidth;
int m_windowHeight;
bool m_initialized;
bool m_resumed;
bool m_processing;
#ifdef NATIVE_LINUX_VST
pthread_mutex_t message_mutex = PTHREAD_MUTEX_INITIALIZER;
bool m_shouldQuit = false;
#endif
std::queue<message> m_messageList;
bool m_shouldGiveIdle;
float * * m_inputs;
float * * m_outputs;
std::mutex m_shmLock;
bool m_shmValid;
using VstMidiEventList = std::vector<VstMidiEvent>;
VstMidiEventList m_midiEvents;
bpm_t m_bpm;
double m_currentSamplePos;
int m_currentProgram;
//! Host to plugin synchronisation data structure
struct Sync
{
double lastppqPos = 0;
double timestamp = -1;
std::int32_t lastFlags = 0;
};
Sync m_sync;
};
#ifdef SYNC_WITH_SHM_FIFO
RemoteVstPlugin::RemoteVstPlugin( const std::string& _shm_in, const std::string& _shm_out ) :
RemotePluginClient( _shm_in, _shm_out ),
#else
RemoteVstPlugin::RemoteVstPlugin( const char * socketPath ) :
RemotePluginClient( socketPath ),
#endif
m_libInst( nullptr ),
m_plugin( nullptr ),
m_windowID( 0 ),
m_windowWidth( 0 ),
m_windowHeight( 0 ),
m_initialized( false ),
m_resumed( false ),
m_processing( false ),
m_messageList(),
m_shouldGiveIdle( false ),
m_inputs( nullptr ),
m_outputs( nullptr ),
m_shmValid( false ),
m_midiEvents(),
m_bpm( 0 ),
m_currentSamplePos( 0 ),
m_currentProgram(-1)
{
__plugin = this;
// process until we have loaded the plugin
while( 1 )
{
message m = receiveMessage();
processMessage( m );
if( m.id == IdVstLoadPlugin || m.id == IdQuit )
{
break;
}
}
}
RemoteVstPlugin::~RemoteVstPlugin()
{
destroyEditor();
setResumed( false );
pluginDispatch( effClose );
if( m_libInst != nullptr )
{
#ifndef NATIVE_LINUX_VST
FreeLibrary( m_libInst );
#else
dlclose(m_libInst);
#endif
m_libInst = nullptr;
}
delete[] m_inputs;
delete[] m_outputs;
}
bool RemoteVstPlugin::processMessage( const message & _m )
{
if (! EMBED)
{
switch( _m.id )
{
case IdShowUI:
showEditor();
return true;
case IdHideUI:
hideEditor();
return true;
case IdToggleUI:
#ifndef NATIVE_LINUX_VST
if( m_window && IsWindowVisible( m_window ) )
#else
if (m_window && m_x11WindowVisible)
#endif
{
hideEditor();
}
else
{
showEditor();
}
return true;
case IdIsUIVisible:
#ifndef NATIVE_LINUX_VST
bool visible = m_window && IsWindowVisible( m_window );
#else
bool visible = m_window && m_x11WindowVisible;
#endif
sendMessage( message( IdIsUIVisible )
.addInt( visible ? 1 : 0 ) );
return true;
}
}
else if (EMBED && _m.id == IdShowUI)
{
#ifndef NATIVE_LINUX_VST
ShowWindow( m_window, SW_SHOWNORMAL );
UpdateWindow( m_window );
#endif
return true;
}
switch( _m.id )
{
case IdVstLoadPlugin:
init( _m.getString() );
break;
case IdVstSetTempo:
setBPM( _m.getInt() );
break;
case IdVstSetLanguage:
hlang = static_cast<VstHostLanguage>( _m.getInt() );
break;
case IdVstGetParameterDump:
getParameterDump();
break;
case IdVstSetParameterDump:
setParameterDump( _m );
break;
case IdSaveSettingsToFile:
saveChunkToFile( _m.getString() );
sendMessage( IdSaveSettingsToFile );
break;
case IdLoadSettingsFromFile:
loadChunkFromFile( _m.getString( 0 ), _m.getInt( 1 ) );
sendMessage( IdLoadSettingsFromFile );
break;
case IdLoadPresetFile:
loadPresetFile( _m.getString( 0 ) );
sendMessage( IdLoadPresetFile );
break;
case IdVstSetProgram:
setProgram( _m.getInt( 0 ) );
sendMessage( IdVstSetProgram );
break;
case IdVstCurrentProgram:
sendMessage( message( IdVstCurrentProgram ).addInt( m_currentProgram ) );
break;
case IdVstRotateProgram:
rotateProgram( _m.getInt( 0 ) );
sendMessage( IdVstRotateProgram );
break;
case IdVstProgramNames:
getProgramNames();
break;
case IdSavePresetFile:
savePreset( _m.getString( 0 ) );
sendMessage( IdSavePresetFile );
break;
case IdVstSetParameter:
m_plugin->setParameter( m_plugin, _m.getInt( 0 ), _m.getFloat( 1 ) );
//sendMessage( IdVstSetParameter );
break;
case IdVstLoadAllParameterDisplays:
loadAllParameterDisplays();
break;
case IdVstLoadAllParameterLabels:
loadAllParameterLabels();
break;
case IdVstUpdateParameterDisplay:
updateParameterDisplay(_m.getInt());
break;
case IdVstUpdateParameterLabel:
updateParameterLabel(_m.getInt());
break;
case IdVstIdleUpdate:
{
int newCurrentProgram = pluginDispatch( effGetProgram );
if( newCurrentProgram != m_currentProgram )
{
m_currentProgram = newCurrentProgram;
sendCurrentProgramName();
}
break;
}
case IdIdle:
{
#ifdef NATIVE_LINUX_VST
idle();
#endif
break;
}
#ifdef NATIVE_LINUX_VST
case IdQuit:
{
m_shouldQuit = true;
break;
}
#endif
default:
return RemotePluginClient::processMessage( _m );
}
return true;
}
void RemoteVstPlugin::init( const std::string & _plugin_file )
{
if( load( _plugin_file ) == false )
{
sendMessage( IdVstFailedLoadingPlugin );
return;
}
updateInOutCount();
updateBufferSize();
updateSampleRate();
/* set program to zero */
/* i comment this out because it breaks dfx Geometer
* looks like we cant set programs for it
*
pluginDispatch( effSetProgram, 0, 0 ); */
// request rate and blocksize
setResumed( true );
debugMessage( "creating editor\n" );
initEditor();
debugMessage( "editor successfully created\n" );
// now post some information about our plugin
sendMessage( message( IdVstPluginWindowID ).addInt( m_windowID ) );
sendMessage( message( IdVstPluginEditorGeometry ).
addInt( m_windowWidth ).
addInt( m_windowHeight ) );
sendMessage( message( IdVstPluginName ).addString( pluginName() ) );
debugMessage( std::string("plugin name: ") + pluginName() + "\n" );
sendMessage( message( IdVstPluginVersion ).addInt( pluginVersion() ) );
sendMessage( message( IdVstPluginVendorString ).
addString( pluginVendorString() ) );
sendMessage( message( IdVstPluginProductString ).
addString( pluginProductString() ) );
sendMessage( message( IdVstParameterCount ).
addInt( m_plugin->numParams ) );
sendMessage( IdInitDone );
debugMessage( "initialization done\n" );
m_initialized = true;
}
static void close_check( FILE* fp )
{
if (!fp) {return;}
if( fclose( fp ) )
{
perror( "fclose" );
}
}
void RemoteVstPlugin::initEditor()
{
if( HEADLESS || m_window || !( m_plugin->flags & effFlagsHasEditor ) )
{
return;
}
#ifndef NATIVE_LINUX_VST
HMODULE hInst = GetModuleHandle( nullptr );
if( hInst == nullptr )
{
debugMessage( "initEditor(): can't get module handle\n" );
return;
}
DWORD dwStyle;
if (EMBED) {
dwStyle = WS_POPUP | WS_SYSMENU | WS_BORDER;
} else {
dwStyle = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX;
}
m_window = CreateWindowEx( WS_EX_APPWINDOW, "LVSL", pluginName(),
dwStyle,
0, 0, 10, 10, nullptr, nullptr, hInst, nullptr );
if( m_window == nullptr )
{
debugMessage( "initEditor(): cannot create editor window\n" );
return;
}
pluginDispatch( effEditOpen, 0, 0, m_window );
ERect* er = nullptr;
pluginDispatch(effEditGetRect, 0, 0, &er);
if (er)
{
assert(er->right > er->left);
m_windowWidth = er->right - er->left;
assert(er->bottom > er->top);
m_windowHeight = er->bottom - er->top;
RECT windowSize = { 0, 0, m_windowWidth, m_windowHeight };
AdjustWindowRect(&windowSize, dwStyle, false);
SetWindowPos(m_window, 0, 0, 0,
windowSize.right - windowSize.left,
windowSize.bottom - windowSize.top,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER
);
}
pluginDispatch( effEditTop );
#ifdef LMMS_BUILD_LINUX
m_windowID = (intptr_t) GetProp( m_window, "__wine_x11_whole_window" );
#else
// 64-bit versions of Windows use 32-bit handles for interoperability
m_windowID = (intptr_t) m_window;
#endif
#else
Atom prop_atom, val_atom;
if (m_display == nullptr)
{
m_display = XOpenDisplay(nullptr);
}
m_window = XCreateSimpleWindow(m_display, DefaultRootWindow(m_display), 0, 0, 400, 400, 0, 0, 0);
m_wmDeleteMessage = XInternAtom(m_display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(m_display, m_window, &m_wmDeleteMessage, 1);
// make tool window
prop_atom = XInternAtom(m_display, "_NET_WM_WINDOW_TYPE", False);
val_atom = XInternAtom(m_display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
XChangeProperty(m_display, m_window, prop_atom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&val_atom, 1);
// change name
XStoreName(m_display, m_window, pluginName());
ERect* er = nullptr;
pluginDispatch(effEditGetRect, 0, 0, &er);
if (er)
{
assert(er->right > er->left);
m_windowWidth = er->right - er->left;
assert(er->bottom > er->top);
m_windowHeight = er->bottom - er->top;
XResizeWindow(m_display, m_window,
static_cast<unsigned int>(m_windowWidth), static_cast<unsigned int>(m_windowHeight));
}
XMapWindow(m_display, m_window);
XFlush(m_display);
pluginDispatch(effEditOpen, 0, (intptr_t) m_display, (void*) m_window);
XSelectInput(m_display, m_window, SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask
| ButtonMotionMask | ExposureMask | KeyPressMask);
pluginDispatch(effEditTop);
m_x11WindowVisible = true;
#endif // NATIVE_LINUX_VST
}
void RemoteVstPlugin::showEditor() {
if( !EMBED && !HEADLESS && m_window )
{
#ifndef NATIVE_LINUX_VST
ShowWindow( m_window, SW_SHOWNORMAL );
#else
if (!m_x11WindowVisible)
{
XMapWindow(m_display, m_window);
XFlush(m_display);
m_x11WindowVisible = true;
}
#endif
}
}
void RemoteVstPlugin::hideEditor() {
if( !EMBED && !HEADLESS && m_window )
{
#ifndef NATIVE_LINUX_VST
ShowWindow( m_window, SW_HIDE );
#else
if (m_x11WindowVisible)
{
XUnmapWindow(m_display, m_window);
XFlush(m_display);
m_x11WindowVisible = false;
}
#endif
}
}
void RemoteVstPlugin::destroyEditor()
{
if( !m_window )
{
return;
}
pluginDispatch( effEditClose );
#ifndef NATIVE_LINUX_VST
// Destroying the window takes some time in Wine 1.8.5
DestroyWindow( m_window );
m_window = nullptr;
#else
if (m_display)
{
XCloseDisplay(m_display);
}
m_display = nullptr;
m_window = 0;
#endif
}
bool RemoteVstPlugin::load( const std::string & _plugin_file )
{
#ifndef NATIVE_LINUX_VST
if ((m_libInst = LoadLibraryW(toWString(_plugin_file).get())) == nullptr)
{
DWORD error = GetLastError();
debugMessage( "LoadLibrary failed: " + GetErrorAsString(error) );
return false;
}
#else
m_libInst = dlopen(_plugin_file.c_str(), RTLD_LAZY);
if ( m_libInst == nullptr) {
debugMessage( std::string("LoadLibrary failed: ") + dlerror() );
return false;
}
#endif
using mainEntryPointer = AEffect* (VST_CALL_CONV*) (audioMasterCallback);
#ifndef NATIVE_LINUX_VST
mainEntryPointer mainEntry = (mainEntryPointer)
GetProcAddress( m_libInst, "VSTPluginMain" );
if( mainEntry == nullptr )
{
mainEntry = (mainEntryPointer)
GetProcAddress( m_libInst, "VstPluginMain" );
}
if( mainEntry == nullptr )
{
mainEntry = (mainEntryPointer)
GetProcAddress( m_libInst, "main" );
}
#else