Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/lv2/namix_lv2_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
// WHAT AN LV2 HOST DOES WITH THE WINDOW, verified against suil rather than assumed: suil's
// x11_in_gtk3.c forward_size_request() reads the child's XSizeHints with XGetNormalHints, clamps
// its allocation to them, and XResizeWindows the child. So the hints are how a UI states its
// limits. This editor is a fixed size — X11PlugView does not override canResize — so the hints it
// sets are min == max, which is the strongest thing it can say.
// limits. The editor publishes its initial size as a minimum and leaves the maximum open, so a
// host can expand the X11 child and the shared view can resize its drawing surfaces accordingly.

#include "lv2message.h"
#include "namixlv2.h"
Expand Down Expand Up @@ -444,9 +444,8 @@ void NamixUi::close()
// XSizeHints and clamps its allocation to them before resizing us (src/x11_in_gtk3.c,
// forward_size_request).
//
// This editor is a fixed size, so min == max == the editor's own rectangle. X11PlugView does not
// override canResize, which means CPluginView::canResize returns kResultFalse and the VST3 build
// tells its host exactly the same thing.
// The editor can grow past its initial size. Keep a 1x canvas as the minimum, but omit a maximum
// so LV2 hosts such as REAPER can allocate the UI to a larger FX window.
void NamixUi::applySizeHints()
{
if (!mHintDisplay || !mView)
Expand All @@ -460,9 +459,9 @@ void NamixUi::applySizeHints()
return;

XSizeHints hints = {};
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = size.getWidth();
hints.min_height = hints.max_height = size.getHeight();
hints.flags = PMinSize;
hints.min_width = geo::kWinW;
hints.min_height = geo::kWinH;
XSetWMNormalHints(mHintDisplay, window, &hints);
XFlush(mHintDisplay);
}
Expand Down
18 changes: 18 additions & 0 deletions src/namgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

#include "pluginterfaces/vst/vsttypes.h"

#include <cmath>
#include <cstdlib>

namespace NAMix
{
namespace geo
Expand All @@ -21,6 +24,21 @@ namespace geo
constexpr int kWinW = 600;
constexpr int kWinH = 400;

// The editor uses logical 600x400 coordinates. The host window and Cairo
// device space are enlarged together; NAMIX_UI_SCALE can override this for
// HiDPI setups (valid range 1.0..3.0).
constexpr float kDefaultUiScale = 1.5f;
inline float uiScale()
{
const char *value = std::getenv("NAMIX_UI_SCALE");
if (!value || !*value)
return kDefaultUiScale;
char *end = nullptr;
const float scale = std::strtof(value, &end);
return end != value && *end == '\0' && std::isfinite(scale) && scale >= 1.0f && scale <= 3.0f
? scale
: kDefaultUiScale;
}
// Palette (geometry.sh; from the original Colors.h). 0xRRGGBB.
constexpr uint32_t kBgColor = 0x1D1A1F; // raisin-black backdrop
constexpr uint32_t kAzure = 0x5085E8; // theme accent
Expand Down
30 changes: 24 additions & 6 deletions src/namview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ const char *baseName(const std::string &path)
NamEditorView::NamEditorView(NamController *controller)
: X11PlugView(static_cast<Vst::EditController *>(controller)), mController(controller)
{
ViewRect size(0, 0, geo::kWinW, geo::kWinH);
mUiScale = geo::uiScale();
ViewRect size(0, 0, static_cast<int>(std::lround(geo::kWinW * mUiScale)),
static_cast<int>(std::lround(geo::kWinH * mUiScale)));
setRect(size);
}

Expand All @@ -111,6 +113,18 @@ void NamEditorView::onAttached()
mResourcesLoaded = true;
}

// Fit the 3:2 logical canvas into the current host allocation without
// distorting the panel. Any extra area becomes a clean black letterbox.
void NamEditorView::onResize(int width, int height)
{
if (width <= 0 || height <= 0)
return;
mUiScale = std::min(static_cast<float>(width) / geo::kWinW,
static_cast<float>(height) / geo::kWinH);
mUiOffsetX = (width - geo::kWinW * mUiScale) * 0.5f;
mUiOffsetY = (height - geo::kWinH * mUiScale) * 0.5f;
}

// The window is going: drop an open field without applying it, and hand the
// keyboard back while the window it was given to still exists. A drag cannot
// outlive the editor either, so its edit gesture is closed for the host.
Expand Down Expand Up @@ -165,6 +179,8 @@ bool NamEditorView::hitCircle(float x, float y, float cx, float cy, float r)
//------------------------------------------------------------------------
void NamEditorView::onDraw(cairo_t *cr)
{
cairo_translate(cr, mUiOffsetX, mUiOffsetY);
cairo_scale(cr, mUiScale, mUiScale);
Canvas c(cr, &mFonts, geo::kWinW, geo::kWinH);
compose(c);
}
Expand Down Expand Up @@ -636,7 +652,8 @@ void NamEditorView::onMouseDown(int x, int y, int button, unsigned /*mods*/, boo
{
if (button != 1) // left button only, as on the original
return;
const float fx = static_cast<float>(x), fy = static_cast<float>(y);
const float fx = (static_cast<float>(x) - mUiOffsetX) / mUiScale;
const float fy = (static_cast<float>(y) - mUiOffsetY) / mUiScale;
mMouseX = fx;
mMouseY = fy;

Expand Down Expand Up @@ -742,11 +759,11 @@ void NamEditorView::onMouseDown(int x, int y, int button, unsigned /*mods*/, boo
// the way back.
void NamEditorView::onMouseMove(int x, int y, unsigned mods)
{
mMouseX = static_cast<float>(x);
mMouseY = static_cast<float>(y);
mMouseX = (static_cast<float>(x) - mUiOffsetX) / mUiScale;
mMouseY = (static_cast<float>(y) - mUiOffsetY) / mUiScale;
if (!mDragParam || !mController)
return;
const float fy = static_cast<float>(y);
const float fy = (static_cast<float>(y) - mUiOffsetY) / mUiScale;
const float range = mDragRange * (isFine(mods) ? kFineDragFactor : 1.0f);
mDragNorm += static_cast<double>(mDragLastY - fy) / range;
mDragNorm = mDragNorm < 0.0 ? 0.0 : (mDragNorm > 1.0 ? 1.0 : mDragNorm);
Expand All @@ -770,7 +787,8 @@ void NamEditorView::onMouseUp(int /*x*/, int /*y*/, int button)
// Wheel adjusts whatever is under the cursor (wheel up = increase).
void NamEditorView::onMouseWheel(int x, int y, int delta, unsigned mods)
{
const float fx = static_cast<float>(x), fy = static_cast<float>(y);
const float fx = (static_cast<float>(x) - mUiOffsetX) / mUiScale;
const float fy = (static_cast<float>(y) - mUiOffsetY) / mUiScale;
const bool fine = isFine(mods);
const double step = delta * (fine ? kFineWheelStep : kWheelStep);

Expand Down
3 changes: 3 additions & 0 deletions src/namview.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class NamEditorView : public Steinberg::X11PlugView

//---from X11PlugView-------------
void onAttached() SMTG_OVERRIDE;
void onResize(int width, int height) SMTG_OVERRIDE;
void onRemoved() SMTG_OVERRIDE;
void onDraw(cairo_t *cr) SMTG_OVERRIDE;
void onMouseDown(int x, int y, int button, unsigned mods, bool doubleClick) SMTG_OVERRIDE;
Expand Down Expand Up @@ -123,6 +124,8 @@ class NamEditorView : public Steinberg::X11PlugView
}

NamController *mController = nullptr;
float mUiScale = geo::kDefaultUiScale;
float mUiOffsetX = 0, mUiOffsetY = 0;

FontStack mFonts;
ImageCache mImages;
Expand Down
85 changes: 84 additions & 1 deletion src/platform/x11plugview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <X11/keysym.h>

#include <atomic>
#include <cmath>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -260,6 +261,41 @@ tresult PLUGIN_API X11PlugView::attached(void *parent, FIDString type)
return isWindowOpen() ? kResultOk : kResultFalse;
}

//------------------------------------------------------------------------
tresult PLUGIN_API X11PlugView::onSize(ViewRect *newSize)
{
if (!newSize || newSize->getWidth() <= 0 || newSize->getHeight() <= 0)
return kInvalidArgument;
const tresult result = Vst::EditorView::onSize(newSize);
if (result != kResultTrue && result != kResultOk)
return result;
resizeSurfaces(static_cast<unsigned>(newSize->getWidth()),
static_cast<unsigned>(newSize->getHeight()));
return kResultTrue;
}

// Keep the panel's 3:2 layout intact while allowing the host to resize the
// editor. Whichever dimension changed less determines the resulting size.
tresult PLUGIN_API X11PlugView::checkSizeConstraint(ViewRect *requested)
{
if (!requested)
return kInvalidArgument;
constexpr double aspect = 1.5;
const int width = requested->getWidth();
const int height = requested->getHeight();
if (width <= 0 || height <= 0)
return kResultFalse;

const int constrainedWidth = static_cast<int>(std::lround(height * aspect));
const int constrainedHeight = static_cast<int>(std::lround(width / aspect));
const ViewRect &current = getRect();
if (std::abs(width - current.getWidth()) >= std::abs(height - current.getHeight()))
requested->right = requested->left + constrainedWidth;
else
requested->bottom = requested->top + constrainedHeight;
return kResultTrue;
}

//------------------------------------------------------------------------
void X11PlugView::trace(const char *fmt, ...) const
{
Expand Down Expand Up @@ -502,6 +538,9 @@ bool X11PlugView::openWindow(::Window parent)
closeWindow();
return false;
}
mSurfaceWidth = width;
mSurfaceHeight = height;
onResize(static_cast<int>(width), static_cast<int>(height));

return true;
}
Expand Down Expand Up @@ -714,6 +753,16 @@ void X11PlugView::drainEvents()
continue;

switch (event.type) {
case ConfigureNotify:
if (event.xconfigure.width > 0 && event.xconfigure.height > 0 &&
(static_cast<unsigned>(event.xconfigure.width) != mSurfaceWidth ||
static_cast<unsigned>(event.xconfigure.height) != mSurfaceHeight)) {
resizeSurfaces(static_cast<unsigned>(event.xconfigure.width),
static_cast<unsigned>(event.xconfigure.height));
mDirty = true;
}
break;

case Expose:
// Coalesce: only the last expose in a burst needs a repaint,
// and the repaint itself happens on the next tick.
Expand Down Expand Up @@ -876,6 +925,35 @@ void X11PlugView::ensureMapped()
mapWindow();
}

//------------------------------------------------------------------------
void X11PlugView::resizeSurfaces(unsigned width, unsigned height)
{
if (!mDisplay || !mWindow || width == 0 || height == 0 ||
(width == mSurfaceWidth && height == mSurfaceHeight))
return;

// The host has already resized the embedded X window before onSize() or
// ConfigureNotify arrives. Sending another XResizeWindow here feeds a
// second resize back into the host while it is dragging the frame and can
// leave stale/interleaved ConfigureNotify events behind.
if (mTarget)
cairo_xlib_surface_set_size(mTarget, static_cast<int>(width), static_cast<int>(height));
if (mBuffer)
cairo_surface_destroy(mBuffer);
mBuffer = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, static_cast<int>(width),
static_cast<int>(height));
if (cairo_surface_status(mBuffer) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "NAMix: cannot resize the editor drawing buffer\n");
cairo_surface_destroy(mBuffer);
mBuffer = nullptr;
return;
}
mSurfaceWidth = width;
mSurfaceHeight = height;
onResize(static_cast<int>(width), static_cast<int>(height));
mDirty = true;
}

//------------------------------------------------------------------------
void X11PlugView::redraw()
{
Expand All @@ -891,8 +969,13 @@ void X11PlugView::redraw()

// Compose into the offscreen buffer...
cairo_t *cr = cairo_create(mBuffer);
if (cairo_status(cr) == CAIRO_STATUS_SUCCESS)
if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
onDraw(cr);
}
cairo_destroy(cr);

// ...then blit it to the window in one operation, so no partially drawn
Expand Down
11 changes: 11 additions & 0 deletions src/platform/x11plugview.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public
//---from CPluginView-------------
tresult PLUGIN_API isPlatformTypeSupported(FIDString type) SMTG_OVERRIDE;
tresult PLUGIN_API attached(void *parent, FIDString type) SMTG_OVERRIDE;
tresult PLUGIN_API onSize(ViewRect *newSize) SMTG_OVERRIDE;
tresult PLUGIN_API canResize() SMTG_OVERRIDE { return kResultTrue; }
tresult PLUGIN_API checkSizeConstraint(ViewRect *rect) SMTG_OVERRIDE;
void attachedToParent() SMTG_OVERRIDE;
void removedFromParent() SMTG_OVERRIDE;

Expand Down Expand Up @@ -92,6 +95,12 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public
// Paint the whole editor. `cr` targets the offscreen buffer.
virtual void onDraw(cairo_t *cr) = 0;

// The host changed the embedded window's physical pixel dimensions.
virtual void onResize(int width, int height)
{
(void)width, (void)height;
}

// Pointer input. Buttons are X button numbers (1 = left, 2 = middle,
// 3 = right). Wheel steps arrive as delta +1 (up) or -1 (down). `mods`
// is a PointerMod mask. `doubleClick` is set on the second press of the
Expand Down Expand Up @@ -173,6 +182,7 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public
void redraw();
void mapWindow();
void ensureMapped();
void resizeSurfaces(unsigned width, unsigned height);

// Diagnostics, enabled by setting NAMIX_X11_TRACE in the environment. They
// exist because the embedding contract is host-specific and only partly
Expand All @@ -191,6 +201,7 @@ class X11PlugView : public Vst::EditorView, public Linux::IEventHandler, public
Atom mXEmbedInfoAtom = None;
cairo_surface_t *mTarget = nullptr; // xlib surface for mWindow
cairo_surface_t *mBuffer = nullptr; // offscreen image surface
unsigned mSurfaceWidth = 0, mSurfaceHeight = 0;

IPtr<Linux::IRunLoop> mRunLoop;
bool mEventHandlerRegistered = false;
Expand Down
10 changes: 4 additions & 6 deletions tools/namix_ttlgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,9 @@ bool writePluginTtl(const std::string &path, const std::vector<PortInfo> &ports)
"# capabilities and the plug-in's state to the editor.\n"
"#\n"
"# ui:resize is deprecated in the specification and is still the only way a UI can\n"
"# state its size, so it is declared OPTIONAL: the editor asks once at startup and\n"
"# carries on without it. ui:noUserResize IS declared, because this editor is a fixed\n"
"# size — X11PlugView does not override canResize, so the VST3 build tells a host the\n"
"# same thing.\n",
"# state its preferred initial size, so it is declared OPTIONAL: the editor asks once at\n"
"# startup and carries on without it. User resizing is supported, so ui:noUserResize is\n"
"# intentionally omitted.\n",
kPortFeedbackFirst, kPortFeedbackFirst + kFeedbackCount - 1, kPortAtomOut);
fprintf(f,
"<%s>\n\ta ui:X11UI ;\n\tlv2:binary <NAMix_ui.so> ;\n\tui:binary <NAMix_ui.so> ;\n"
Expand All @@ -339,8 +338,7 @@ bool writePluginTtl(const std::string &path, const std::vector<PortInfo> &ports)
"\t\tui:protocol ui:floatProtocol\n\t]",
kPluginUri, kPortFeedbackFirst + static_cast<std::uint32_t>(i));
}
fprintf(f, " ;\n\tlv2:extensionData ui:idleInterface ;\n"
"\tlv2:portProperty ui:noUserResize .\n\n");
fprintf(f, " ;\n\tlv2:extensionData ui:idleInterface .\n\n");

// --- the plug-in -----------------------------------------------------
//
Expand Down
9 changes: 6 additions & 3 deletions tools/panelrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ int main(int argc, char **argv)
SvgCache svgs;
svgs.setResourceDir(res);

cairo_surface_t *surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, geo::kWinW, geo::kWinH);
const float scale = geo::uiScale();
const int width = static_cast<int>(std::lround(geo::kWinW * scale));
const int height = static_cast<int>(std::lround(geo::kWinH * scale));
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cr = cairo_create(surface);
cairo_scale(cr, scale, scale);
Canvas c(cr, &fonts, geo::kWinW, geo::kWinH);

// --- backdrop + line overlay ---
Expand Down Expand Up @@ -267,7 +270,7 @@ int main(int argc, char **argv)
if (!fontsOk)
++missing;

printf("panelrender: wrote %s (%dx%d)\n", outPath, geo::kWinW, geo::kWinH);
printf("panelrender: wrote %s (%dx%d)\n", outPath, width, height);
if (missing) {
fprintf(stderr, "panelrender: %d asset(s) failed to load\n", missing);
return 1;
Expand Down