This commit is contained in:
falkTX 2021-10-08 10:33:14 +01:00
parent ee3f3771a4
commit 0845a0ffa2
17 changed files with 86 additions and 71 deletions

View file

@ -0,0 +1,249 @@
/*
* DISTRHO Cardinal Plugin
* Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
*
* 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 3 of
* the License, or 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.
*
* For a full copy of the GNU General Public License see the LICENSE file.
*/
#include <asset.hpp>
#include <audio.hpp>
#include <context.hpp>
#include <library.hpp>
#include <keyboard.hpp>
#include <midi.hpp>
#include <plugin.hpp>
#include <random.hpp>
#include <settings.hpp>
#include <system.hpp>
#include <ui/common.hpp>
#include <window/Window.hpp>
#include <osdialog.h>
#include "DistrhoPlugin.hpp"
START_NAMESPACE_DISTRHO
// -----------------------------------------------------------------------------------------------------------
// The following code was based from VCVRack adapters/standalone.cpp
/*
Copyright (C) 2016-2021 VCV
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 3 of the
License, or (at your option) any later version.
*/
struct Initializer {
Initializer()
{
using namespace rack;
settings::devMode = true;
system::init();
asset::init();
logger::init();
random::init();
// Log environment
INFO("%s %s v%s", APP_NAME.c_str(), APP_EDITION.c_str(), APP_VERSION.c_str());
INFO("%s", system::getOperatingSystemInfo().c_str());
INFO("System directory: %s", asset::systemDir.c_str());
INFO("User directory: %s", asset::userDir.c_str());
INFO("System time: %s", string::formatTimeISO(system::getUnixTime()).c_str());
// Load settings
settings::init();
#if 0
try {
settings::load();
}
catch (Exception& e) {
std::string message = e.what();
message += "\n\nResetting settings to default";
d_stdout(message.c_str());
/*
if (!osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK_CANCEL, msg.c_str())) {
exit(1);
}
*/
}
#endif
// Check existence of the system res/ directory
std::string resDir = asset::system("res");
if (!system::isDirectory(resDir)) {
std::string message = string::f("Rack's resource directory \"%s\" does not exist. Make sure Rack is correctly installed and launched.", resDir.c_str());
d_stderr2(message.c_str());
/*
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, message.c_str());
*/
exit(1);
}
INFO("Initializing environment");
// network::init();
audio::init();
// rtaudioInit();
midi::init();
// rtmidiInit();
keyboard::init();
plugin::init();
library::init();
// discord::init();
ui::init();
window::init();
}
~Initializer()
{
using namespace rack;
window::destroy();
ui::destroy();
// discord::destroy();
library::destroy();
midi::destroy();
audio::destroy();
plugin::destroy();
INFO("Destroying logger");
logger::destroy();
}
};
static const Initializer& getInitializerInstance()
{
static const Initializer init;
return init;
}
// -----------------------------------------------------------------------------------------------------------
class CardinalPlugin : public Plugin
{
public:
CardinalPlugin()
: Plugin(0, 0, 0)
{
}
protected:
/* --------------------------------------------------------------------------------------------------------
* Information */
/**
Get the plugin label.
A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
*/
const char* getLabel() const override
{
return "Cardinal";
}
/**
Get an extensive comment/description about the plugin.
*/
const char* getDescription() const override
{
return "...";
}
/**
Get the plugin author/maker.
*/
const char* getMaker() const override
{
return "DISTRHO";
}
/**
Get the plugin homepage.
*/
const char* getHomePage() const override
{
return "https://github.com/DISTRHO/Cardinal";
}
/**
Get the plugin license name (a single line of text).
For commercial plugins this should return some short copyright information.
*/
const char* getLicense() const override
{
return "ISC";
}
/**
Get the plugin version, in hexadecimal.
*/
uint32_t getVersion() const override
{
return d_version(1, 0, 0);
}
/**
Get the plugin unique Id.
This value is used by LADSPA, DSSI and VST plugin formats.
*/
int64_t getUniqueId() const override
{
return d_cconst('d', 'C', 'd', 'n');
}
/* --------------------------------------------------------------------------------------------------------
* Init */
/* --------------------------------------------------------------------------------------------------------
* Internal data */
/* --------------------------------------------------------------------------------------------------------
* Process */
/**
Run/process function for plugins without MIDI input.
*/
void run(const float** inputs, float** outputs, uint32_t frames) override
{
// copy inputs over outputs if needed
if (outputs[0] != inputs[0])
std::memcpy(outputs[0], inputs[0], sizeof(float)*frames);
if (outputs[1] != inputs[1])
std::memcpy(outputs[1], inputs[1], sizeof(float)*frames);
}
// -------------------------------------------------------------------------------------------------------
private:
/**
Set our plugin class as non-copyable and add a leak detector just in case.
*/
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CardinalPlugin)
};
/* ------------------------------------------------------------------------------------------------------------
* Plugin entry point, called by DPF to create a new plugin instance. */
Plugin* createPlugin()
{
getInitializerInstance();
return new CardinalPlugin();
}
// -----------------------------------------------------------------------------------------------------------
END_NAMESPACE_DISTRHO

View file

@ -0,0 +1,210 @@
/*
* DISTRHO Cardinal Plugin
* Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
*
* 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 3 of
* the License, or 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.
*
* For a full copy of the GNU General Public License see the LICENSE file.
*/
#include <app/common.hpp>
#include <app/Scene.hpp>
#include <context.hpp>
#include <engine/Engine.hpp>
#include <patch.hpp>
#include <ui/common.hpp>
#include <window/Window.hpp>
#include "DistrhoUI.hpp"
#include "ResizeHandle.hpp"
GLFWAPI const char* glfwGetClipboardString(GLFWwindow* window) { return nullptr; }
GLFWAPI void glfwSetClipboardString(GLFWwindow* window, const char*) {}
GLFWAPI const char* glfwGetKeyName(int key, int scancode) { return nullptr; }
GLFWAPI int glfwGetKeyScancode(int key) { return 0; }
namespace rack {
namespace window {
DISTRHO_NAMESPACE::UI* lastUI = nullptr;
void mouseButtonCallback(Window* win, int button, int action, int mods);
void cursorPosCallback(Window* win, double xpos, double ypos);
void scrollCallback(Window* win, double x, double y);
}
}
START_NAMESPACE_DISTRHO
// -----------------------------------------------------------------------------------------------------------
struct Initializer2 {
Initializer2()
{
using namespace rack;
}
~Initializer2()
{
using namespace rack;
}
};
static const Initializer2& getInitializer2Instance()
{
static const Initializer2 init;
return init;
}
class CardinalUI : public UI
{
ResizeHandle fResizeHandle;
public:
CardinalUI()
: UI(1280, 720),
fResizeHandle(this)
{
using namespace rack;
/*
The following code was based from VCVRack adapters/standalone.cpp
Copyright (C) 2016-2021 VCV
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 3 of the
License, or (at your option) any later version.
*/
// Initialize context
INFO("Initializing context");
window::lastUI = this;
contextSet(new Context);
APP->engine = new engine::Engine;
APP->history = new history::State;
APP->event = new widget::EventState;
APP->scene = new app::Scene;
APP->event->rootWidget = APP->scene;
APP->patch = new patch::Manager;
/*if (!settings::headless)*/ {
APP->window = new window::Window;
}
window::lastUI = nullptr;
APP->engine->startFallbackThread();
}
~CardinalUI() override
{
using namespace rack;
delete APP;
contextSet(NULL);
}
void onDisplay() override
{
APP->window->step();
}
void uiIdle() override
{
repaint();
}
protected:
/* --------------------------------------------------------------------------------------------------------
* DSP/Plugin Callbacks */
/**
A parameter has changed on the plugin side.
This is called by the host to inform the UI about parameter changes.
*/
void parameterChanged(uint32_t index, float value) override
{
}
// -------------------------------------------------------------------------------------------------------
bool onMouse(const MouseEvent& ev) override
{
int button;
int mods = 0;
int action = ev.press;
if (ev.mod & kModifierControl)
mods |= GLFW_MOD_CONTROL;
if (ev.mod & kModifierShift)
mods |= GLFW_MOD_SHIFT;
if (ev.mod & kModifierAlt)
mods |= GLFW_MOD_ALT;
switch (ev.button)
{
case 0:
button = GLFW_MOUSE_BUTTON_MIDDLE;
break;
case 1:
button = GLFW_MOUSE_BUTTON_LEFT;
break;
case 2:
button = GLFW_MOUSE_BUTTON_RIGHT;
break;
default:
button = 0;
break;
}
mouseButtonCallback(APP->window, button, action, mods);
return true;
}
bool onMotion(const MotionEvent& ev) override
{
cursorPosCallback(APP->window, ev.pos.getX(), ev.pos.getY());
return true;
}
bool onScroll(const ScrollEvent& ev) override
{
scrollCallback(APP->window, ev.delta.getX(), ev.delta.getY());
return true;
}
#if 0
void onResize(const ResizeEvent& ev) override
{
UI::onResize(ev);
// APP->window->setSize(rack::math::Vec(ev.size.getWidth(), ev.size.getHeight()));
}
#endif
// TODO uiFocus
private:
/**
Set our UI class as non-copyable and add a leak detector just in case.
*/
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CardinalUI)
};
/* ------------------------------------------------------------------------------------------------------------
* UI entry point, called by DPF to create a new UI instance. */
UI* createUI()
{
getInitializer2Instance();
return new CardinalUI();
}
// -----------------------------------------------------------------------------------------------------------
END_NAMESPACE_DISTRHO

View file

@ -0,0 +1,40 @@
/*
* DISTRHO Cardinal Plugin
* Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
*
* 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 3 of
* the License, or 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.
*
* For a full copy of the GNU General Public License see the LICENSE file.
*/
#ifndef DISTRHO_PLUGIN_INFO_H_INCLUDED
#define DISTRHO_PLUGIN_INFO_H_INCLUDED
#define DISTRHO_PLUGIN_BRAND "DISTRHO"
#define DISTRHO_PLUGIN_NAME "Cardinal"
#define DISTRHO_PLUGIN_URI "https://distrho.kx.studio/plugins/cardinal"
#define DISTRHO_PLUGIN_HAS_UI 1
#define DISTRHO_PLUGIN_NUM_INPUTS 2
#define DISTRHO_PLUGIN_NUM_OUTPUTS 2
#define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 1
// #define DISTRHO_PLUGIN_LV2_CATEGORY "lv2:AnalyserPlugin"
// #define DISTRHO_PLUGIN_VST3_CATEGORIES "Fx|Analyzer"
// #define DISTRHO_PLUGIN_HAS_EMBED_UI 1
// #define DISTRHO_PLUGIN_HAS_EXTERNAL_UI 1
// #define DISTRHO_UI_USE_NANOVG 1
#define DISTRHO_UI_USER_RESIZABLE 0
enum Parameters {
kParameterCount
};
#endif // DISTRHO_PLUGIN_INFO_H_INCLUDED

141
plugins/Cardinal/Makefile Normal file
View file

@ -0,0 +1,141 @@
#!/usr/bin/make -f
# Makefile for DISTRHO Plugins #
# ---------------------------- #
# Created by falkTX
#
# --------------------------------------------------------------
# Project name, used for binaries
NAME = Cardinal
# --------------------------------------------------------------
# Files to build (DPF stuff)
FILES_DSP = \
CardinalPlugin.cpp
FILES_UI = \
CardinalUI.cpp \
Window.cpp
# --------------------------------------------------------------
# Import base definitions
# UI_TYPE = external
SKIP_NANOVG = true
include ../../dpf/Makefile.base.mk
# --------------------------------------------------------------
# Files to build (VCV stuff)
FILES_DSP += Rack/dep/pffft/pffft.c
FILES_DSP += Rack/dep/pffft/fftpack.c
FILES_UI += Rack/dep/oui-blendish/blendish.c
FILES_UI += Rack/dep/nanovg/src/nanovg.c
# FIXME dont use this
FILES_UI += Rack/dep/osdialog/osdialog.c
ifeq ($(MACOS),true)
FILES_UI += Rack/dep/osdialog/osdialog_mac.m
else ifeq ($(WINDOWS),true)
FILES_UI += Rack/dep/osdialog/osdialog_win.c
else
FILES_UI += Rack/dep/osdialog/osdialog_zenity.c
endif
FILES_DSP += $(wildcard Rack/src/*.c)
FILES_DSP += $(wildcard Rack/src/*/*.c)
FILES_DSP += $(filter-out Rack/src/gamepad.cpp Rack/src/rtaudio.cpp Rack/src/rtmidi.cpp, $(wildcard Rack/src/*.cpp))
FILES_DSP += $(filter-out Rack/src/window/Window.cpp, $(wildcard Rack/src/*/*.cpp))
EXTRA_LIBS = Rack/dep/lib/libcrypto.a
EXTRA_LIBS += Rack/dep/lib/libcurl.a
# EXTRA_LIBS += Rack/dep/lib/libglfw3.a
EXTRA_LIBS += Rack/dep/lib/libjansson.a
EXTRA_LIBS += Rack/dep/lib/libspeexdsp.a
EXTRA_LIBS += Rack/dep/lib/libssl.a
EXTRA_LIBS += Rack/dep/lib/libzstd.a
ifeq ($(WINDOWS),true)
EXTRA_LIBS += Rack/dep/lib/libarchive_static.a
EXTRA_LIBS += Rack/dep/lib/libglew32.a
else
EXTRA_LIBS += Rack/dep/lib/libarchive.a
EXTRA_LIBS += Rack/dep/lib/libGLEW.a
endif
# --------------------------------------------------------------
# Do some magic
include ../../dpf/Makefile.plugins.mk
ifeq ($(WINDOWS),true)
CMAKE := cmake -G 'Unix Makefiles' -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_RC_COMPILER=$(subst gcc,windres,$(CC))
else
CMAKE := cmake
endif
Rack/dep/lib/%.a:
$(MAKE) CMAKE="$(CMAKE) -DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_INSTALL_PREFIX='$(abspath Rack/dep)'" -C Rack/dep lib/$*.a
Rack/dep/lib/libcrypto.a: Rack/dep/lib/libssl.a
# --------------------------------------------------------------
# Extra flags for VCV stuff
BASE_FLAGS += -D_APP_VERSION=2.git.0
# BASE_FLAGS += -I$(DPF_PATH)/dgl/src/nanovg
BASE_FLAGS += -IRack/include
BASE_FLAGS += -IRack/dep/include
BASE_FLAGS += -IRack/dep/filesystem/include
BASE_FLAGS += -IRack/dep/fuzzysearchdatabase/src
BASE_FLAGS += -IRack/dep/glfw/deps
BASE_FLAGS += -IRack/dep/glfw/include
BASE_FLAGS += -IRack/dep/nanovg/src
BASE_FLAGS += -IRack/dep/nanosvg/src
BASE_FLAGS += -IRack/dep/osdialog
BASE_FLAGS += -IRack/dep/oui-blendish
BASE_FLAGS += -IRack/dep/pffft
ifeq ($(WINDOWS),true)
BASE_FLAGS += -Imingw-compat
BUILD_CXX_FLAGS += -Imingw-std-threads
endif
ifeq ($(MACOS),true)
BASE_FLAGS += -DARCH_MAC
else ifeq ($(WINDOWS),true)
BASE_FLAGS += -DARCH_WIN
else
BASE_FLAGS += -DARCH_LIN
endif
# FIXME lots of warnings from VCV side
BASE_FLAGS += -Wno-unused-parameter
BASE_FLAGS += -Wno-unused-variable
# extra linker flags
ifneq ($(HAIKU_OR_MACOS_OR_WINDOWS),true)
LINK_FLAGS += -ldl
endif
ifeq ($(MACOS),true)
LINK_FLAGS += -framework IOKit
# LINK_FLAGS += -Wl,-all_load
endif
# LINK_FLAGS += $(OPENGL_LIBS)
# TODO needed on windows? need to check
LINK_FLAGS += -lpthread
# --------------------------------------------------------------
# Enable all possible plugin types
TARGETS = jack lv2 vst2 vst3
all: $(TARGETS)
# --------------------------------------------------------------

1
plugins/Cardinal/Rack Submodule

@ -0,0 +1 @@
Subproject commit 042a9ce026d253700ea14e340182900162ab4653

View file

@ -0,0 +1,185 @@
/*
* Resize handle for DPF
* Copyright (C) 2021 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.
*/
#pragma once
#include "TopLevelWidget.hpp"
#include "../dgl/Color.hpp"
START_NAMESPACE_DGL
/** Resize handle for DPF windows, will sit on bottom-right. */
class ResizeHandle : public TopLevelWidget
{
public:
/** Constructor for placing this handle on top of a window. */
explicit ResizeHandle(Window& window)
: TopLevelWidget(window),
handleSize(16),
resizing(false)
{
resetArea();
}
/** Overloaded constructor, will fetch the window from an existing top-level widget. */
explicit ResizeHandle(TopLevelWidget* const tlw)
: TopLevelWidget(tlw->getWindow()),
handleSize(16),
resizing(false)
{
resetArea();
}
/** Set the handle size, minimum 16. */
void setHandleSize(const uint size)
{
handleSize = std::max(16u, size);
resetArea();
}
protected:
void onDisplay() override
{
const GraphicsContext& context(getGraphicsContext());
const double lineWidth = 1.0 * getScaleFactor();
#ifdef DGL_OPENGL
glUseProgram(0);
glMatrixMode(GL_MODELVIEW);
#endif
// draw white lines, 1px wide
Color(1.0f, 1.0f, 1.0f).setFor(context);
l1.draw(context, lineWidth);
l2.draw(context, lineWidth);
l3.draw(context, lineWidth);
// draw black lines, offset by 1px and 1px wide
Color(0.0f, 0.0f, 0.0f).setFor(context);
Line<double> l1b(l1), l2b(l2), l3b(l3);
l1b.moveBy(lineWidth, lineWidth);
l2b.moveBy(lineWidth, lineWidth);
l3b.moveBy(lineWidth, lineWidth);
l1b.draw(context, lineWidth);
l2b.draw(context, lineWidth);
l3b.draw(context, lineWidth);
}
bool onMouse(const MouseEvent& ev) override
{
if (ev.button != 1)
return false;
if (ev.press && area.contains(ev.pos))
{
resizing = true;
resizingSize = Size<double>(getWidth(), getHeight());
lastResizePoint = ev.pos;
return true;
}
if (resizing && ! ev.press)
{
resizing = false;
return true;
}
return false;
}
bool onMotion(const MotionEvent& ev) override
{
if (! resizing)
return false;
const Size<double> offset(ev.pos.getX() - lastResizePoint.getX(),
ev.pos.getY() - lastResizePoint.getY());
resizingSize += offset;
lastResizePoint = ev.pos;
// TODO min width, min height
const uint minWidth = 16;
const uint minHeight = 16;
if (resizingSize.getWidth() < minWidth)
resizingSize.setWidth(minWidth);
if (resizingSize.getWidth() > 16384)
resizingSize.setWidth(16384);
if (resizingSize.getHeight() < minHeight)
resizingSize.setHeight(minHeight);
if (resizingSize.getHeight() > 16384)
resizingSize.setHeight(16384);
setSize(resizingSize.getWidth(), resizingSize.getHeight());
return true;
}
void onResize(const ResizeEvent& ev) override
{
TopLevelWidget::onResize(ev);
resetArea();
}
private:
Rectangle<uint> area;
Line<double> l1, l2, l3;
uint handleSize;
// event handling state
bool resizing;
Point<double> lastResizePoint;
Size<double> resizingSize;
void resetArea()
{
const double scaleFactor = getScaleFactor();
const uint margin = 0.0 * scaleFactor;
const uint size = handleSize * scaleFactor;
area = Rectangle<uint>(getWidth() - size - margin,
getHeight() - size - margin,
size, size);
recreateLines(area.getX(), area.getY(), size);
}
void recreateLines(const uint x, const uint y, const uint size)
{
uint linesize = size;
uint offset = 0;
// 1st line, full diagonal size
l1.setStartPos(x + size, y);
l1.setEndPos(x, y + size);
// 2nd line, bit more to the right and down, cropped
offset += size / 3;
linesize -= size / 3;
l2.setStartPos(x + linesize + offset, y + offset);
l2.setEndPos(x + offset, y + linesize + offset);
// 3rd line, even more right and down
offset += size / 3;
linesize -= size / 3;
l3.setStartPos(x + linesize + offset, y + offset);
l3.setEndPos(x + offset, y + linesize + offset);
}
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ResizeHandle)
};
END_NAMESPACE_DGL

457
plugins/Cardinal/Window.cpp Normal file
View file

@ -0,0 +1,457 @@
#include <map>
#include <queue>
#include <thread>
#include <stb_image_write.h>
#include <osdialog.h>
#include <window/Window.hpp>
#include <asset.hpp>
#include <widget/Widget.hpp>
#include <app/Scene.hpp>
#include <keyboard.hpp>
#include <context.hpp>
#include <patch.hpp>
#include <settings.hpp>
#include <plugin.hpp> // used in Window::screenshot
#include <system.hpp> // used in Window::screenshot
#include "DistrhoUI.hpp"
namespace rack {
namespace window {
extern DISTRHO_NAMESPACE::UI* lastUI;
static const math::Vec minWindowSize = math::Vec(640, 480);
void Font::loadFile(const std::string& filename, NVGcontext* vg) {
this->vg = vg;
handle = nvgCreateFont(vg, filename.c_str(), filename.c_str());
if (handle < 0)
throw Exception("Failed to load font %s", filename.c_str());
INFO("Loaded font %s", filename.c_str());
}
Font::~Font() {
// There is no NanoVG deleteFont() function yet, so do nothing
}
std::shared_ptr<Font> Font::load(const std::string& filename) {
return APP->window->loadFont(filename);
}
void Image::loadFile(const std::string& filename, NVGcontext* vg) {
this->vg = vg;
handle = nvgCreateImage(vg, filename.c_str(), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
if (handle <= 0)
throw Exception("Failed to load image %s", filename.c_str());
INFO("Loaded image %s", filename.c_str());
}
Image::~Image() {
// TODO What if handle is invalid?
if (handle >= 0)
nvgDeleteImage(vg, handle);
}
std::shared_ptr<Image> Image::load(const std::string& filename) {
return APP->window->loadImage(filename);
}
struct Window::Internal {
DISTRHO_NAMESPACE::UI* ui;
math::Vec size;
std::string lastWindowTitle;
int frame = 0;
bool ignoreNextMouseDelta = false;
int frameSwapInterval = -1;
double monitorRefreshRate = 60.0; // FIXME
double frameTime = 0.0;
double lastFrameDuration = 0.0;
math::Vec lastMousePos;
std::map<std::string, std::shared_ptr<Font>> fontCache;
std::map<std::string, std::shared_ptr<Image>> imageCache;
bool fbDirtyOnSubpixelChange = true;
};
Window::Window() {
internal = new Internal;
internal->ui = lastUI;
internal->size = minWindowSize;
int err;
// Set up GLEW
glewExperimental = GL_TRUE;
err = glewInit();
if (err != GLEW_OK) {
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Could not initialize GLEW. Does your graphics card support OpenGL 2.0 or greater? If so, make sure you have the latest graphics drivers installed.");
exit(1);
}
const GLubyte* vendor = glGetString(GL_VENDOR);
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
INFO("Renderer: %s %s", vendor, renderer);
INFO("OpenGL: %s", version);
INFO("UI pointer: %p", lastUI);
// GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
glGetError();
// Set up NanoVG
int nvgFlags = NVG_ANTIALIAS;
#if defined NANOVG_GL2
vg = nvgCreateGL2(nvgFlags);
fbVg = nvgCreateSharedGL2(vg, nvgFlags);
#elif defined NANOVG_GL3
vg = nvgCreateGL3(nvgFlags);
#elif defined NANOVG_GLES2
vg = nvgCreateGLES2(nvgFlags);
#endif
if (!vg) {
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Could not initialize NanoVG. Does your graphics card support OpenGL 2.0 or greater? If so, make sure you have the latest graphics drivers installed.");
exit(1);
}
d_stderr2("framebuffer is %p", fbVg);
// Load default Blendish font
uiFont = loadFont(asset::system("res/fonts/DejaVuSans.ttf"));
bndSetFont(uiFont->handle);
if (APP->scene) {
widget::Widget::ContextCreateEvent e;
APP->scene->onContextCreate(e);
}
}
Window::~Window() {
if (APP->scene) {
widget::Widget::ContextDestroyEvent e;
APP->scene->onContextDestroy(e);
}
// Fonts and Images in the cache must be deleted before the NanoVG context is deleted
internal->fontCache.clear();
internal->imageCache.clear();
// nvgDeleteClone(fbVg);
#if defined NANOVG_GL2
nvgDeleteGL2(vg);
nvgDeleteGL2(fbVg);
#elif defined NANOVG_GL3
nvgDeleteGL3(vg);
#elif defined NANOVG_GLES2
nvgDeleteGLES2(vg);
#endif
delete internal;
}
math::Vec Window::getSize() {
return internal->size;
}
void Window::setSize(math::Vec size) {
internal->size = size.max(minWindowSize);
}
void Window::run() {
internal->frame = 0;
}
void Window::step() {
double frameTime = system::getTime();
double lastFrameTime = internal->frameTime;
internal->frameTime = frameTime;
internal->lastFrameDuration = frameTime - lastFrameTime;
// DEBUG("%.2lf Hz", 1.0 / internal->lastFrameDuration);
double t1 = 0.0, t2 = 0.0, t3 = 0.0, t4 = 0.0, t5 = 0.0;
// Make event handlers and step() have a clean NanoVG context
nvgReset(vg);
bndSetFont(uiFont->handle);
// Poll events
// Save and restore context because event handler set their own context based on which window they originate from.
// Context* context = contextGet();
// glfwPollEvents();
// contextSet(context);
// Set window title
std::string windowTitle = APP_NAME + " " + APP_EDITION_NAME + " " + APP_VERSION;
if (APP->patch->path != "") {
windowTitle += " - ";
if (!APP->history->isSaved())
windowTitle += "*";
windowTitle += system::getFilename(APP->patch->path);
}
if (windowTitle != internal->lastWindowTitle) {
internal->ui->getWindow().setTitle(windowTitle.c_str());
internal->lastWindowTitle = windowTitle;
}
// Get desired pixel ratio
float newPixelRatio = internal->ui->getScaleFactor();
if (newPixelRatio != pixelRatio) {
pixelRatio = newPixelRatio;
APP->event->handleDirty();
}
// Get framebuffer/window ratio
int winWidth = internal->ui->getWidth();
int winHeight = internal->ui->getHeight();
int fbWidth = winWidth;// * newPixelRatio;
int fbHeight = winHeight;// * newPixelRatio;
windowRatio = (float)fbWidth / winWidth;
t1 = system::getTime();
if (APP->scene) {
// DEBUG("%f %f %d %d", pixelRatio, windowRatio, fbWidth, winWidth);
// Resize scene
APP->scene->box.size = math::Vec(fbWidth, fbHeight).div(pixelRatio);
// Step scene
APP->scene->step();
t2 = system::getTime();
// Render scene
bool visible = true;
if (visible) {
// Update and render
nvgBeginFrame(vg, fbWidth, fbHeight, pixelRatio);
nvgScale(vg, pixelRatio, pixelRatio);
// Draw scene
widget::Widget::DrawArgs args;
args.vg = vg;
args.clipBox = APP->scene->box.zeroPos();
APP->scene->draw(args);
t3 = system::getTime();
glViewport(0, 0, fbWidth, fbHeight);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgEndFrame(vg);
t4 = system::getTime();
}
}
t5 = system::getTime();
// DEBUG("pre-step %6.1f step %6.1f draw %6.1f nvgEndFrame %6.1f glfwSwapBuffers %6.1f total %6.1f",
// (t1 - frameTime) * 1e3f,
// (t2 - t1) * 1e3f,
// (t3 - t2) * 1e3f,
// (t4 - t2) * 1e3f,
// (t5 - t4) * 1e3f,
// (t5 - frameTime) * 1e3f
// );
internal->frame++;
}
void Window::screenshot(const std::string&) {
}
void Window::screenshotModules(const std::string&, float) {
}
void Window::close() {
internal->ui->getWindow().close();
}
void Window::cursorLock() {
if (!settings::allowCursorLock)
return;
internal->ignoreNextMouseDelta = true;
}
void Window::cursorUnlock() {
if (!settings::allowCursorLock)
return;
internal->ignoreNextMouseDelta = true;
}
bool Window::isCursorLocked() {
return internal->ignoreNextMouseDelta;
}
int Window::getMods() {
int mods = 0;
/*
if (glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)
mods |= GLFW_MOD_SHIFT;
if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)
mods |= GLFW_MOD_CONTROL;
if (glfwGetKey(win, GLFW_KEY_LEFT_ALT) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)
mods |= GLFW_MOD_ALT;
if (glfwGetKey(win, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS || glfwGetKey(win, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)
mods |= GLFW_MOD_SUPER;
*/
return mods;
}
void Window::setFullScreen(bool) {
}
bool Window::isFullScreen() {
return false;
}
double Window::getMonitorRefreshRate() {
return internal->monitorRefreshRate;
}
double Window::getFrameTime() {
return internal->frameTime;
}
double Window::getLastFrameDuration() {
return internal->lastFrameDuration;
}
double Window::getFrameDurationRemaining() {
double frameDurationDesired = internal->frameSwapInterval / internal->monitorRefreshRate;
return frameDurationDesired - (system::getTime() - internal->frameTime);
}
std::shared_ptr<Font> Window::loadFont(const std::string& filename) {
const auto& pair = internal->fontCache.find(filename);
if (pair != internal->fontCache.end())
return pair->second;
// Load font
std::shared_ptr<Font> font;
try {
font = std::make_shared<Font>();
font->loadFile(filename, vg);
}
catch (Exception& e) {
WARN("%s", e.what());
font = NULL;
}
internal->fontCache[filename] = font;
return font;
}
std::shared_ptr<Image> Window::loadImage(const std::string& filename) {
const auto& pair = internal->imageCache.find(filename);
if (pair != internal->imageCache.end())
return pair->second;
// Load image
std::shared_ptr<Image> image;
try {
image = std::make_shared<Image>();
image->loadFile(filename, vg);
}
catch (Exception& e) {
WARN("%s", e.what());
image = NULL;
}
internal->imageCache[filename] = image;
return image;
}
bool& Window::fbDirtyOnSubpixelChange() {
return internal->fbDirtyOnSubpixelChange;
}
void mouseButtonCallback(Window* win, int button, int action, int mods) {
/*
#if defined ARCH_MAC
// Remap Ctrl-left click to right click on Mac
if (button == GLFW_MOUSE_BUTTON_LEFT && (mods & RACK_MOD_MASK) == GLFW_MOD_CONTROL) {
button = GLFW_MOUSE_BUTTON_RIGHT;
mods &= ~GLFW_MOD_CONTROL;
}
// Remap Ctrl-shift-left click to middle click on Mac
if (button == GLFW_MOUSE_BUTTON_LEFT && (mods & RACK_MOD_MASK) == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT)) {
button = GLFW_MOUSE_BUTTON_MIDDLE;
mods &= ~(GLFW_MOD_CONTROL | GLFW_MOD_SHIFT);
}
#endif
*/
APP->event->handleButton(win->internal->lastMousePos, button, action, mods);
}
void cursorPosCallback(Window* win, double xpos, double ypos) {
math::Vec mousePos = math::Vec(xpos, ypos).div(win->pixelRatio / win->windowRatio).round();
math::Vec mouseDelta = mousePos.minus(win->internal->lastMousePos);
// Workaround for GLFW warping mouse to a different position when the cursor is locked or unlocked.
if (win->internal->ignoreNextMouseDelta) {
win->internal->ignoreNextMouseDelta = false;
mouseDelta = math::Vec();
}
win->internal->lastMousePos = mousePos;
APP->event->handleHover(mousePos, mouseDelta);
// Keyboard/mouse MIDI driver
math::Vec scaledPos(xpos / win->internal->ui->getWidth(), ypos / win->internal->ui->getHeight());
keyboard::mouseMove(scaledPos);
}
void scrollCallback(Window* win, double x, double y) {
math::Vec scrollDelta = math::Vec(x, y);
#if defined ARCH_MAC
scrollDelta = scrollDelta.mult(10.0);
#else
scrollDelta = scrollDelta.mult(50.0);
#endif
APP->event->handleScroll(win->internal->lastMousePos, scrollDelta);
}
void init() {
}
void destroy() {
}
} // namespace window
} // namespace rack

View file

@ -0,0 +1 @@
#include <shlobj.h>

View file

@ -0,0 +1 @@
#include <shlwapi.h>

View file

@ -0,0 +1 @@
#include <windows.h>

View file

@ -0,0 +1,3 @@
#pragma once
#include_next <condition_variable>
#include "mingw.condition_variable.h"

View file

@ -0,0 +1,3 @@
#pragma once
#include_next <mutex>
#include "mingw.mutex.h"

View file

@ -0,0 +1,3 @@
#pragma once
#include_next <thread>
#include "mingw.thread.h"

@ -0,0 +1 @@
Subproject commit f6365f900fb9b1cd6014c8d1cf13ceacf8faf3de