From 06f2cfe679528fb1dafc417142c745d3cdb3b997 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 27 Jul 2014 14:07:56 +0100 Subject: [PATCH] Center external windows when shown, if possible --- source/Makefile.deps | 2 +- source/backend/Makefile | 2 +- source/utils/CarlaPluginUI.cpp | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/source/Makefile.deps b/source/Makefile.deps index 5892b6d32..698d8e8d6 100644 --- a/source/Makefile.deps +++ b/source/Makefile.deps @@ -166,6 +166,6 @@ VST3_PLUGIN_CPP = $(CWD)/backend/plugin/Vst3Plugin.cpp $ # backend/standalone CARLA_STANDALONE_CPP_DEPS = $(CARLA_HOST_H) $(CARLA_MIDI_H) $(CARLA_NATIVE_H) $(CARLA_ENGINE_HPP) $(CARLA_PLUGIN_HPP) $(CARLA_BACKEND_UTILS_HPP) $(CARLA_OSC_UTILS_HPP) -CARLA_STANDALONE_CPP = $(CWD)/backend/standalone/CarlaStandalone.cpp $(CARLA_STANDALONE_CPP_DEPS) $(CARLA_PLUGIN_UI_CPP) $(CARLA_DSSI_UTILS_CPP) $(CARLA_STATE_UTILS_CPP) +CARLA_STANDALONE_CPP = $(CWD)/backend/CarlaStandalone.cpp $(CARLA_STANDALONE_CPP_DEPS) $(CARLA_PLUGIN_UI_CPP) $(CARLA_DSSI_UTILS_CPP) $(CARLA_STATE_UTILS_CPP) # ---------------------------------------------------------------------------------------------------------------------------- diff --git a/source/backend/Makefile b/source/backend/Makefile index 4c9314c54..276c792b3 100644 --- a/source/backend/Makefile +++ b/source/backend/Makefile @@ -101,7 +101,7 @@ doxygen: CarlaBackend.doxygen # -------------------------------------------------------------- -CarlaStandalone.cpp.o: CarlaStandalone.cpp $(CARLA_STANDALONE_CPP_DEPS) +CarlaStandalone.cpp.o: $(CARLA_STANDALONE_CPP) $(CXX) $< $(BUILD_CXX_FLAGS) -c -o $@ # -------------------------------------------------------------- diff --git a/source/utils/CarlaPluginUI.cpp b/source/utils/CarlaPluginUI.cpp index f9f7ad300..d1f28e9d7 100644 --- a/source/utils/CarlaPluginUI.cpp +++ b/source/utils/CarlaPluginUI.cpp @@ -328,6 +328,8 @@ bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* cons const ScopedDisplay sd; CARLA_SAFE_ASSERT_RETURN(sd.display != nullptr, true); + const Window rootWindow(DefaultRootWindow(sd.display)); + const Atom _ncl = XInternAtom(sd.display, "_NET_CLIENT_LIST" , False); const Atom _nwn = XInternAtom(sd.display, "_NET_WM_NAME", False); const Atom _nwp = XInternAtom(sd.display, "_NET_WM_PID", False); @@ -338,7 +340,7 @@ bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* cons ulong numWindows, bytesAfter; uchar* data = nullptr; - int status = XGetWindowProperty(sd.display, DefaultRootWindow(sd.display), _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data); + int status = XGetWindowProperty(sd.display, rootWindow, _ncl, 0L, (~0L), False, AnyPropertyType, &actualType, &actualFormat, &numWindows, &bytesAfter, &data); CARLA_SAFE_ASSERT_RETURN(data != nullptr, true); const ScopedFreeData sfd(data); @@ -440,7 +442,33 @@ bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* cons const Atom _nwi = XInternAtom(sd.display, "_NET_WM_ICON", False); XChangeProperty(sd.display, lastGoodWindow, _nwi, XA_CARDINAL, 32, PropModeReplace, (const uchar*)sCarlaX11Icon, sCarlaX11IconSize); - XSetTransientForHint(sd.display, lastGoodWindow, (Window)winId); + const Window hostWinId((Window)winId); + + XSetTransientForHint(sd.display, lastGoodWindow, hostWinId); + + // center the plugin UI + { + int hostX, hostY, pluginX, pluginY; + uint hostWidth, hostHeight, pluginWidth, pluginHeight, border, depth; + Window retWindow; + + if (XGetGeometry(sd.display, hostWinId, &retWindow, &hostX, &hostY, &hostWidth, &hostHeight, &border, &depth) != 0 && + XGetGeometry(sd.display, lastGoodWindow, &retWindow, &pluginX, &pluginY, &pluginWidth, &pluginHeight, &border, &depth) != 0) + { + if (XTranslateCoordinates(sd.display, hostWinId, rootWindow, hostX, hostY, &hostX, &hostY, &retWindow) == True && + XTranslateCoordinates(sd.display, lastGoodWindow, rootWindow, pluginX, pluginY, &pluginX, &pluginY, &retWindow) == True) + { + const int newX = hostX + int(hostWidth/2 - pluginWidth/2); + const int newY = hostY + int(hostHeight/2 - pluginHeight/2); + + XMoveWindow(sd.display, lastGoodWindow, newX, newY); + } + } + } + + // focusing the host UI and then the plugin UI forces the WM to repaint the plugin window icon + XRaiseWindow(sd.display, hostWinId); + XSetInputFocus(sd.display, hostWinId, RevertToPointerRoot, CurrentTime); XRaiseWindow(sd.display, lastGoodWindow); XSetInputFocus(sd.display, lastGoodWindow, RevertToPointerRoot, CurrentTime);