From 027ec425b40e731a9a9641013b7a938410803433 Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 30 Nov 2017 21:12:07 +0000 Subject: [PATCH] Initial Cocoa Plugin UI support --- source/backend/Makefile | 7 ++ source/backend/engine/Makefile | 7 ++ source/bridges-plugin/Makefile | 7 ++ source/utils/CarlaPluginUI.cpp | 181 +++++++++++++++++++++++++++++---- 4 files changed, 185 insertions(+), 17 deletions(-) diff --git a/source/backend/Makefile b/source/backend/Makefile index 3ccd7718f..cb8953163 100644 --- a/source/backend/Makefile +++ b/source/backend/Makefile @@ -105,6 +105,13 @@ $(BINDIR)/libcarla_utils$(LIB_EXT): $(OBJS_utils) $(UTILS_LIBS) # ---------------------------------------------------------------------------------------------------------------------------- +ifeq ($(MACOS),true) +$(OBJDIR)/CarlaStandalone.cpp.o: CarlaStandalone.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling $<" + @$(CXX) $< $(BUILD_CXX_FLAGS) -ObjC++ -c -o $@ +endif + $(OBJDIR)/%.cpp.o: %.cpp -@mkdir -p $(OBJDIR) @echo "Compiling $<" diff --git a/source/backend/engine/Makefile b/source/backend/engine/Makefile index 45a0381b8..60ad9f1f2 100644 --- a/source/backend/engine/Makefile +++ b/source/backend/engine/Makefile @@ -65,10 +65,17 @@ $(OBJDIR)/CarlaEngineRtAudio.cpp.o: CarlaEngineRtAudio.cpp @echo "Compiling CarlaEngineRtAudio.cpp" @$(CXX) $< $(BUILD_CXX_FLAGS) $(RTAUDIO_FLAGS) $(RTMIDI_FLAGS) -c -o $@ +ifeq ($(MACOS),true) +$(OBJDIR)/CarlaEngineNative.cpp.exp.o: CarlaEngineNative.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling CarlaEngineNative.cpp (plugin)" + @$(CXX) $< $(BUILD_CXX_FLAGS) -DCARLA_PLUGIN_EXPORT -ObjC++ -c -o $@ +else $(OBJDIR)/CarlaEngineNative.cpp.exp.o: CarlaEngineNative.cpp -@mkdir -p $(OBJDIR) @echo "Compiling CarlaEngineNative.cpp (plugin)" @$(CXX) $< $(BUILD_CXX_FLAGS) -DCARLA_PLUGIN_EXPORT -c -o $@ +endif $(OBJDIR)/%.cpp.o: %.cpp -@mkdir -p $(OBJDIR) diff --git a/source/bridges-plugin/Makefile b/source/bridges-plugin/Makefile index 83e8ee619..eadf51dcc 100644 --- a/source/bridges-plugin/Makefile +++ b/source/bridges-plugin/Makefile @@ -233,10 +233,17 @@ $(OBJDIR)/CarlaPlug%.cpp.o: $(CWD)/backend/plugin/CarlaPlug%.cpp @echo "Compiling CarlaPlug$*.cpp (bridge)" @$(CXX) $< $(BUILD_CXX_FLAGS) $(NATIVE_BUILD_FLAGS) -c -o $@ +ifeq ($(MACOS),true) +$(OBJDIR)/CarlaStandalone.cpp.o: $(CWD)/backend/CarlaStandalone.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling CarlaStandalone.cpp (bridge)" + @$(CXX) $< $(BUILD_CXX_FLAGS) $(NATIVE_BUILD_FLAGS) -ObjC++ -c -o $@ +else $(OBJDIR)/CarlaStandalone.cpp.o: $(CWD)/backend/CarlaStandalone.cpp -@mkdir -p $(OBJDIR) @echo "Compiling CarlaStandalone.cpp (bridge)" @$(CXX) $< $(BUILD_CXX_FLAGS) $(NATIVE_BUILD_FLAGS) -c -o $@ +endif # ---------------------------------------------------------------------------------------------------------------------- # posix32 diff --git a/source/utils/CarlaPluginUI.cpp b/source/utils/CarlaPluginUI.cpp index dcec294bf..991a202bf 100644 --- a/source/utils/CarlaPluginUI.cpp +++ b/source/utils/CarlaPluginUI.cpp @@ -25,10 +25,12 @@ # include #endif +#ifdef CARLA_OS_MAC +# import +#endif + #ifdef CARLA_OS_WIN # include -// # include -// # include #endif // --------------------------------------------------------------------------------------------------------------------- @@ -320,6 +322,157 @@ private: }; #endif // HAVE_X11 +// --------------------------------------------------------------------------------------------------------------------- +// MacOS / Cocoa + +#ifdef CARLA_OS_MAC + +class CocoaPluginUI : public CarlaPluginUI +{ +public: + CocoaPluginUI(CloseCallback* const cb, const uintptr_t parentId, const bool isResizable) noexcept + : CarlaPluginUI(cb, isResizable), + fView(nullptr), + fWindow(0) + { + [NSAutoreleasePool new]; + [NSApplication sharedApplication]; + + fView = [NSView new]; + CARLA_SAFE_ASSERT_RETURN(fView != nullptr,) + + if (isResizable) + [fView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; + + fWindow = [[NSWindow new]retain]; + + if (fWindow == 0) + { + [fView release]; + fView = nullptr; + return; + } + + [fWindow setIsVisible:NO]; + [fWindow setContentView:fView]; + [fWindow makeFirstResponder:fView]; + [fWindow makeKeyAndOrderFront:fWindow]; + + [NSApp activateIgnoringOtherApps:YES]; + [fWindow center]; + +#if 0 + uint styleMask = NSClosableWindowMask | NSTitledWindowMask; + + if (isResizable) + styleMask |= NSResizableWindowMask; + + fWindow = [NSWindow initWithContentRect:contentRect + styleMask:flags + backing:NSBackingStoreBuffered defer:NO]; + +#endif + } + + ~CocoaPluginUI() override + { + if (fView == nullptr) + return; + + [fWindow close]; + [fView release]; + [fWindow release]; + } + + void show() override + { + CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,); + + [fWindow setIsVisible:YES]; + } + + void hide() override + { + CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,); + + [fWindow setIsVisible:NO]; + } + + void idle() override + { + } + + void focus() override + { + CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,); + + // TODO + } + + void setSize(const uint width, const uint height, const bool forceUpdate) override + { + CARLA_SAFE_ASSERT_RETURN(fWindow != nullptr,); + CARLA_SAFE_ASSERT_RETURN(fView != nullptr,); + + [fView setFrame:NSMakeRect(0, 0, width, height)]; + + const NSSize size = NSMakeSize(width, height); + [fWindow setContentSize:size]; + +#if 0 + if (fResizable) + { + [fWindow setContentMinSize:NSMakeSize(1, 1)]; + [fWindow setContentMaxSize:NSMakeSize(99999, 99999)]; + [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:NO]; + } + else + { + [fWindow setContentMinSize:size]; + [fWindow setContentMaxSize:size]; + [[fWindow standardWindowButton:NSWindowZoomButton] setHidden:YES]; + } +#endif + } + + void setTitle(const char* const title) override + { + CARLA_SAFE_ASSERT_RETURN(fWindow != 0,); + + NSString* titleString = [[NSString alloc] + initWithBytes:title + length:strlen(title) + encoding:NSUTF8StringEncoding]; + + [fWindow setTitle:titleString]; + } + + void setTransientWinId(const uintptr_t winId) override + { + CARLA_SAFE_ASSERT_RETURN(fWindow != 0,); + + // TODO + } + + void* getPtr() const noexcept override + { + return (void*)fView; + } + + void* getDisplay() const noexcept + { + return (void*)fWindow; + } + +private: + NSView* fView; + id fWindow; + + CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CocoaPluginUI) +}; + +#endif // CARLA_OS_MAC + // --------------------------------------------------------------------------------------------------------------------- // Windows @@ -549,13 +702,7 @@ bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* cons CARLA_SAFE_ASSERT_RETURN(uiTitle != nullptr && uiTitle[0] != '\0', true); CARLA_SAFE_ASSERT_RETURN(winId != 0, true); -#if defined(CARLA_OS_MAC) - return true; - (void)pid; (void)centerUI; -#elif defined(CARLA_OS_WIN) - return true; - (void)pid; (void)centerUI; -#elif defined(HAVE_X11) +#if defined(HAVE_X11) struct ScopedDisplay { Display* display; ScopedDisplay() : display(XOpenDisplay(nullptr)) {} @@ -762,10 +909,17 @@ bool CarlaPluginUI::tryTransientWinIdMatch(const uintptr_t pid, const char* cons // ----------------------------------------------------- +#ifdef HAVE_X11 +CarlaPluginUI* CarlaPluginUI::newX11(CloseCallback* cb, uintptr_t parentId, bool isResizable) +{ + return new X11PluginUI(cb, parentId, isResizable); +} +#endif + #ifdef CARLA_OS_MAC CarlaPluginUI* CarlaPluginUI::newCocoa(CloseCallback* cb, uintptr_t parentId, bool isResizable) { - return nullptr; + return new CocoaPluginUI(cb, parentId, isResizable); } #endif @@ -776,11 +930,4 @@ CarlaPluginUI* CarlaPluginUI::newWindows(CloseCallback* cb, uintptr_t parentId, } #endif -#ifdef HAVE_X11 -CarlaPluginUI* CarlaPluginUI::newX11(CloseCallback* cb, uintptr_t parentId, bool isResizable) -{ - return new X11PluginUI(cb, parentId, isResizable); -} -#endif - // -----------------------------------------------------