Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirasawaSama committed Nov 19, 2023
1 parent c110d10 commit 5117379
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
File renamed without changes.
51 changes: 39 additions & 12 deletions src/plugin_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu
juce::MidiBuffer midiBuffer;
juce::AudioBuffer<float> buffer;
std::unique_ptr<jshm::shared_memory> shm;
std::unique_ptr<PluginWindow> window;
std::unique_ptr<plugin_window> window;
std::unique_ptr<juce::AudioPluginInstance> processor;
juce::AudioPlayHead::PositionInfo positionInfo;
juce::Array<juce::AudioProcessorParameter*> parameters;
Expand Down Expand Up @@ -111,12 +111,14 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu
processor->enableAllBuses();
processor->setPlayHead(this);
processor->addListener(this);

bool isWindowOpen = true;
if (args->containsOption("-P|--preset")) {
auto preset = args->getValueForOption("-P|--preset");
loadState(preset == "#" ? streams::in.readString() : preset);
isWindowOpen = loadState(preset == "#" ? streams::in.readString() : preset);
}

createEditorWindow();
if (isWindowOpen) createEditorWindow();
writeInitInformation();

startThread();
Expand Down Expand Up @@ -228,12 +230,7 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu
break;
}
case 3: {
juce::MessageManagerLock mml(Thread::getCurrentThread());
if (!mml.lockWasGained()) return;
juce::MemoryBlock memory;
processor->getStateInformation(memory);
streams::out.write((bool)juce::File(streams::in.readString())
.replaceWithData(memory.getData(), memory.getSize()));
streams::out.write(saveState(streams::in.readString()));
streams::out.flush();
break;
}
Expand All @@ -257,21 +254,51 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu
#ifdef JUCE_WINDOWS
parentHandle = args->containsOption("-H|--handle") ? args->getValueForOption("-H|--handle").getLargeIntValue() : 0;
#endif
window = std::make_unique<PluginWindow>("[EIMHost] " + processor->getName() + " (" +
window = std::make_unique<plugin_window>("[EIMHost] " + processor->getName() + " (" +
processor->getPluginDescription().pluginFormatName + ")", component, window,
processor->wrapperType != juce::AudioProcessor::wrapperType_VST, parentHandle);
}

void loadState(const juce::String& file) {
bool loadState(const juce::String& file) {
juce::FileInputStream stream(file);
if (!stream.openedOk()) {
std::cerr << "Failed to open file: " << file << '\n';
fflush(stderr);
return;
return true;
}
bool isWindowOpen = false;
juce::MemoryBlock memory;
switch (stream.readByte()) {
case 0:
isWindowOpen = stream.readBool();
plugin_window::x_ = stream.readInt();
plugin_window::y_ = stream.readInt();
plugin_window::width_ = stream.readInt();
plugin_window::height_ = stream.readInt();
}
stream.readIntoMemoryBlock(memory);
processor->setStateInformation(memory.getData(), (int)memory.getSize());
return isWindowOpen;
}

bool saveState(const juce::String& file) {
juce::MessageManagerLock mml(Thread::getCurrentThread());
if (!mml.lockWasGained()) return false;
juce::MemoryBlock memory;
processor->getStateInformation(memory);
if (auto stream = juce::File(file).createOutputStream()) {
stream->setPosition(0);
stream->truncate();
stream->writeByte(0); // version
stream->writeBool(window != nullptr);
stream->writeInt(plugin_window::x_);
stream->writeInt(plugin_window::y_);
stream->writeInt(plugin_window::width_);
stream->writeInt(plugin_window::height_);
stream->write(memory.getData(), memory.getSize());
return true;
}
return false;
}

void writeInitInformation() {
Expand Down
26 changes: 17 additions & 9 deletions src/plugin_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <Windows.h>
#endif

int width_ = 0, height_ = 0, x_ = 0, y_ = 0;

class PluginWindow : public juce::ResizableWindow {
class plugin_window : public juce::ResizableWindow {
public:
PluginWindow(const juce::String& title, juce::AudioProcessorEditor* component, std::unique_ptr<PluginWindow>& ptr,
static int width_, height_, x_, y_;

plugin_window(const juce::String& title, juce::AudioProcessorEditor* component, std::unique_ptr<plugin_window>& ptr,
bool resizable, long long parentHandle)
: ResizableWindow(title, !parentHandle), thisWindow(ptr) {
setUsingNativeTitleBar(true);
Expand All @@ -20,10 +20,13 @@ class PluginWindow : public juce::ResizableWindow {
auto scaleFactor = juce::jmin((screenBounds.getWidth() - 50) / (float)getWidth(), (screenBounds.getHeight() - 50) / (float)getHeight());
auto trueWidth = scaleFactor < 1.0f ? (int)((float)width_ * scaleFactor) : width_;
auto trueHeight = scaleFactor < 1.0f ? (int)((float)height_ * scaleFactor) : height_;
if (x_ > 20 && y_ > 20) setBounds(x_, y_, trueWidth, trueHeight); else centreWithSize(trueWidth, trueHeight);

if (x_ > 20 && y_ > 20 && x_ <= screenBounds.getWidth() - 20.0 && y_ <= screenBounds.getHeight() - 20.0) {
setBounds(x_, y_, trueWidth, trueHeight);
} else centreWithSize(trueWidth, trueHeight);

setContentOwned(component, true);
PluginWindow::setVisible(true);
plugin_window::setVisible(true);

#ifdef JUCE_WINDOWS
if (parentHandle) addToDesktop(getDesktopWindowStyleFlags(), (HWND)(LONG_PTR)parentHandle);
Expand All @@ -32,7 +35,7 @@ class PluginWindow : public juce::ResizableWindow {
#endif
}

~PluginWindow() override {
~plugin_window() override {
clearContentComponent();
}

Expand All @@ -59,9 +62,14 @@ class PluginWindow : public juce::ResizableWindow {
}

private:
std::unique_ptr<PluginWindow>& thisWindow;
std::unique_ptr<plugin_window>& thisWindow;

[[nodiscard]] float getDesktopScaleFactor() const override { return 1.0f; }

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginWindow)
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(plugin_window)
};

int plugin_window::height_ = 0;
int plugin_window::width_ = 0;
int plugin_window::x_ = 0;
int plugin_window::y_ = 0;

0 comments on commit 5117379

Please sign in to comment.