From 1b67a0ea0a06611fee018021c7c6b99a807dc71f Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 7 Oct 2017 14:40:58 +0200 Subject: [PATCH] Add new interposer to make sure libjack is loaded on init --- source/backend/plugin/CarlaPluginJack.cpp | 6 +- source/interposer/Makefile | 12 +++ source/interposer/interposer-jack-x11.cpp | 110 ++++++++++++++++++++++ source/interposer/interposer-x11.cpp | 11 +-- 4 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 source/interposer/interposer-jack-x11.cpp diff --git a/source/backend/plugin/CarlaPluginJack.cpp b/source/backend/plugin/CarlaPluginJack.cpp index 8defbc5c1..fc0a515f0 100644 --- a/source/backend/plugin/CarlaPluginJack.cpp +++ b/source/backend/plugin/CarlaPluginJack.cpp @@ -121,13 +121,13 @@ protected: CarlaString ldpreload; - /* +#ifdef HAVE_X11 if (options.frontendWinId != 0) { ldpreload = (CarlaString(options.binaryDir) - + "/libcarla_interposer-x11.so"); + + "/libcarla_interposer-jack-x11.so"); } - */ +#endif const ScopedEngineEnvironmentLocker _seel(kEngine); diff --git a/source/interposer/Makefile b/source/interposer/Makefile index f6f28618a..cf2867ed7 100644 --- a/source/interposer/Makefile +++ b/source/interposer/Makefile @@ -37,7 +37,9 @@ TARGETS += $(BINDIR)/libcarla_interposer-safe.so ifeq ($(HAVE_X11),true) OBJS += $(OBJDIR)/interposer-x11.cpp.o +OBJS += $(OBJDIR)/interposer-jack-x11.cpp.o TARGETS += $(BINDIR)/libcarla_interposer-x11.so +TARGETS += $(BINDIR)/libcarla_interposer-jack-x11.so endif endif @@ -65,6 +67,11 @@ $(BINDIR)/libcarla_interposer-x11.so: $(OBJDIR)/interposer-x11.cpp.o @echo "Linking libcarla_interposer-x11.so" @$(CXX) $< $(SHARED) $(LINK_FLAGS) $(INTERPOSER_X11_LIBS) -o $@ +$(BINDIR)/libcarla_interposer-jack-x11.so: $(OBJDIR)/interposer-jack-x11.cpp.o + -@mkdir -p $(BINDIR) + @echo "Linking libcarla_interposer-jack-x11.so" + @$(CXX) $< $(SHARED) $(LINK_FLAGS) $(INTERPOSER_X11_LIBS) -o $@ + # ---------------------------------------------------------------------------------------------------------------------- $(OBJDIR)/interposer-safe.cpp.o: interposer-safe.cpp @@ -77,6 +84,11 @@ $(OBJDIR)/interposer-x11.cpp.o: interposer-x11.cpp @echo "Compiling $<" @$(CXX) $< $(BUILD_CXX_FLAGS) $(X11_FLAGS) -c -o $@ +$(OBJDIR)/interposer-jack-x11.cpp.o: interposer-jack-x11.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling $<" + @$(CXX) $< $(BUILD_CXX_FLAGS) $(X11_FLAGS) -c -o $@ + # ---------------------------------------------------------------------------------------------------------------------- -include $(OBJS:%.o=%.d) diff --git a/source/interposer/interposer-jack-x11.cpp b/source/interposer/interposer-jack-x11.cpp new file mode 100644 index 000000000..da64d6a0c --- /dev/null +++ b/source/interposer/interposer-jack-x11.cpp @@ -0,0 +1,110 @@ +/* + * Carla Interposer for X11 Window Mapping + * Copyright (C) 2014-2017 Filipe Coelho + * + * 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 2 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 doc/GPL.txt file. + */ + +#include "CarlaUtils.hpp" + +#include +#include + +struct ScopedLibOpen { + void* handle; + + ScopedLibOpen() + : handle(dlopen("libjack.so.0", RTLD_NOW|RTLD_LOCAL)) {} + + ~ScopedLibOpen() + { + if (handle != nullptr) + dlclose(handle); + } +}; + +// ----------------------------------------------------------------------- +// Function typedefs + +typedef int (*XMapWindowFunc)(Display*, Window); +typedef int (*XUnmapWindowFunc)(Display*, Window); + +// ----------------------------------------------------------------------- +// Global counter so we only map the first (hopefully main) window + +static int sMapWindowCounter = 0; + +// ----------------------------------------------------------------------- +// Calling the real functions + +static int real_XMapWindow(Display* display, Window w) +{ + static const XMapWindowFunc func = (XMapWindowFunc)::dlsym(RTLD_NEXT, "XMapWindow"); + CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0); + + return func(display, w); +} + +static int real_XUnmapWindow(Display* display, Window w) +{ + static const XUnmapWindowFunc func = (XUnmapWindowFunc)::dlsym(RTLD_NEXT, "XUnmapWindow"); + CARLA_SAFE_ASSERT_RETURN(func != nullptr, 0); + + return func(display, w); +} + +// ----------------------------------------------------------------------- +// Our custom functions + +CARLA_EXPORT +int XMapWindow(Display* display, Window w) +{ + carla_stdout("------------------------------- XMapWindow called"); + + for (;;) + { + if (++sMapWindowCounter != 1) + break; + + static const ScopedLibOpen slo; + + if (const char* const winIdStr = std::getenv("CARLA_FRONTEND_WIN_ID")) + { + CARLA_SAFE_ASSERT_BREAK(winIdStr[0] != '\0'); + + const long long winIdLL(std::strtoll(winIdStr, nullptr, 16)); + CARLA_SAFE_ASSERT_BREAK(winIdLL > 0); + + const Window winId(static_cast(winIdLL)); + XSetTransientForHint(display, w, static_cast(winId)); + + carla_stdout("Transient hint correctly applied before mapping window"); + } + + break; + } + + return real_XMapWindow(display, w); +} + +CARLA_EXPORT +int XUnmapWindow(Display* display, Window w) +{ + carla_stdout("------------------------------- XUnmapWindow called"); + + --sMapWindowCounter; + + return real_XUnmapWindow(display, w); +} + +// ----------------------------------------------------------------------- diff --git a/source/interposer/interposer-x11.cpp b/source/interposer/interposer-x11.cpp index 39576f94d..21e48bb13 100644 --- a/source/interposer/interposer-x11.cpp +++ b/source/interposer/interposer-x11.cpp @@ -1,6 +1,6 @@ /* * Carla Interposer for X11 Window Mapping - * Copyright (C) 2014-2015 Filipe Coelho + * Copyright (C) 2014-2017 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -63,15 +63,6 @@ int XMapWindow(Display* display, Window w) if (++sMapWindowCounter != 1) break; - static bool libjackopen = false; - - if (! libjackopen && std::getenv("CARLA_LIBJACK_SETUP") != nullptr) - { - libjackopen = true; - dlopen("libjack.so.0", RTLD_NOW|RTLD_LOCAL); - carla_stdout("------------------------------- libjack opened"); - } - if (const char* const winIdStr = std::getenv("CARLA_ENGINE_OPTION_FRONTEND_WIN_ID")) { CARLA_SAFE_ASSERT_BREAK(winIdStr[0] != '\0');