1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __plugguieditor__
6 #include "plugguieditor.h"
7 #endif
8 
9 #define kIdleRate    100 // host idle rate in ms
10 #define kIdleRate2    50
11 #define kIdleRateMin   4 // minimum time between 2 idles in ms
12 
13 #if WINDOWS
14 #include <windows.h>
15 #endif
16 
17 #if MAC
18 #include <Carbon/Carbon.h>
19 #include "getpluginbundle.h"
20 
21 namespace VSTGUI {
22 static void InitMachOLibrary ();
23 static void ExitMachOLibrary ();
24 } // VSTGUI
25 #endif
26 
27 namespace VSTGUI {
28 
29 //-----------------------------------------------------------------------------
30 // PluginGUIEditor Implementation
31 //-----------------------------------------------------------------------------
32 /*! @class PluginGUIEditor
33 This is the same as the AEffGUIEditor class except that this one allows
34 the VSTGUI lib to build without VST dependencies.
35 */
PluginGUIEditor(void * pEffect)36 PluginGUIEditor::PluginGUIEditor (void *pEffect)
37 	: effect (pEffect)
38 {
39 	systemWindow = nullptr;
40 	lLastTicks   = getTicks ();
41 
42 	#if WINDOWS
43 	OleInitialize (nullptr);
44 	#endif
45 	#if MAC
46 	InitMachOLibrary ();
47 	#endif
48 }
49 
50 //-----------------------------------------------------------------------------
~PluginGUIEditor()51 PluginGUIEditor::~PluginGUIEditor ()
52 {
53 	#if WINDOWS
54 	OleUninitialize ();
55 	#endif
56 	#if MAC
57 	ExitMachOLibrary ();
58 	#endif
59 }
60 
61 //-----------------------------------------------------------------------------
draw(ERect * ppErect)62 void PluginGUIEditor::draw (ERect *ppErect)
63 {
64 }
65 
66 //-----------------------------------------------------------------------------
open(void * ptr)67 bool PluginGUIEditor::open (void *ptr)
68 {
69 	systemWindow = ptr;
70 	return true;
71 }
72 
73 //-----------------------------------------------------------------------------
idle()74 void PluginGUIEditor::idle ()
75 {
76 	if (frame)
77 		frame->idle ();
78 }
79 
80 //-----------------------------------------------------------------------------
81 int32_t PluginGUIEditor::knobMode = kCircularMode;
82 
83 //-----------------------------------------------------------------------------
setKnobMode(int32_t val)84 int32_t PluginGUIEditor::setKnobMode (int32_t val)
85 {
86 	PluginGUIEditor::knobMode = val;
87 	return 1;
88 }
89 
90 //-----------------------------------------------------------------------------
wait(uint32_t ms)91 void PluginGUIEditor::wait (uint32_t ms)
92 {
93 	#if MAC
94 	RunCurrentEventLoop (kEventDurationMillisecond * ms);
95 
96 	#elif WINDOWS
97 	Sleep (ms);
98 
99 	#endif
100 }
101 
102 //-----------------------------------------------------------------------------
getTicks()103 uint32_t PluginGUIEditor::getTicks ()
104 {
105 	#if MAC
106 	return (TickCount () * 1000) / 60;
107 
108 	#elif WINDOWS
109 	return (uint32_t)GetTickCount ();
110 
111 	#endif
112 
113 	return 0;
114 }
115 
116 //-----------------------------------------------------------------------------
doIdleStuff()117 void PluginGUIEditor::doIdleStuff ()
118 {
119 	// get the current time
120 	uint32_t currentTicks = getTicks ();
121 
122 	// YG TEST idle ();
123 	if (currentTicks < lLastTicks)
124 	{
125 		#if (MAC && TARGET_API_MAC_CARBON)
126 		RunCurrentEventLoop (kEventDurationMillisecond * kIdleRateMin);
127 		#else
128 		wait (kIdleRateMin);
129 		#endif
130 		currentTicks += kIdleRateMin;
131 		if (currentTicks < lLastTicks - kIdleRate2)
132 			return;
133 	}
134 	idle (); // TEST
135 
136 	#if WINDOWS
137 	struct tagMSG windowsMessage;
138 	if (PeekMessage (&windowsMessage, nullptr, WM_PAINT, WM_PAINT, PM_REMOVE))
139 		DispatchMessage (&windowsMessage);
140 
141 	#elif MAC && !__LP64__
142 	EventRef event;
143 	EventTypeSpec eventTypes[] = { {kEventClassWindow, kEventWindowUpdate}, {kEventClassWindow, kEventWindowDrawContent} };
144 	if (ReceiveNextEvent (GetEventTypeCount (eventTypes), eventTypes, kEventDurationNoWait, true, &event) == noErr)
145 	{
146 		SendEventToEventTarget (event, GetEventDispatcherTarget ());
147 		ReleaseEvent (event);
148 	}
149 	#endif
150 
151 	// save the next time
152  	lLastTicks = currentTicks + kIdleRate;
153 }
154 
155 //-----------------------------------------------------------------------------
getRect(ERect ** ppErect)156 bool PluginGUIEditor::getRect (ERect **ppErect)
157 {
158 	*ppErect = &rect;
159 	return true;
160 }
161 
162 #if MAC
163 // -----------------------------------------------------------------------------
164 // -----------------------------------------------------------------------------
165 
166 void* gBundleRef = 0;
167 
168 // -----------------------------------------------------------------------------
InitMachOLibrary()169 void InitMachOLibrary ()
170 {
171 	VSTGUI::gBundleRef = GetPluginBundle ();
172 }
173 
174 // -----------------------------------------------------------------------------
ExitMachOLibrary()175 void ExitMachOLibrary ()
176 {
177 	if (VSTGUI::gBundleRef)
178 		CFRelease (VSTGUI::gBundleRef);
179 }
180 
181 #endif
182 
183 } // VSTGUI
184