From a34ac38aec220719a155daa554f6a1ac78829e07 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Thu, 29 Oct 2020 08:35:43 +0100 Subject: [PATCH] Add helpers to identify the documents directory --- vst/CMakeLists.txt | 20 ++++++++++++++++++-- vst/NativeHelpers.cpp | 39 +++++++++++++++++++++++++++++++++++++++ vst/NativeHelpers.h | 10 ++++++++++ vst/NativeHelpers.mm | 26 ++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 vst/NativeHelpers.cpp create mode 100644 vst/NativeHelpers.h create mode 100644 vst/NativeHelpers.mm diff --git a/vst/CMakeLists.txt b/vst/CMakeLists.txt index c6f573fb..145fa099 100644 --- a/vst/CMakeLists.txt +++ b/vst/CMakeLists.txt @@ -18,14 +18,20 @@ set(VSTPLUGIN_SOURCES SfizzVstEditor.cpp SfizzVstState.cpp VstPluginFactory.cpp - X11RunLoop.cpp) + X11RunLoop.cpp + NativeHelpers.cpp) set(VSTPLUGIN_HEADERS SfizzVstProcessor.h SfizzVstController.h SfizzVstEditor.h SfizzVstState.h - X11RunLoop.h) + X11RunLoop.h + NativeHelpers.h) + +if(APPLE) + list(APPEND VSTPLUGIN_SOURCES NativeHelpers.mm) +endif() add_library(${VSTPLUGIN_PRJ_NAME} MODULE ${VSTPLUGIN_HEADERS} @@ -65,6 +71,16 @@ if (MINGW) set_target_properties (${VSTPLUGIN_PRJ_NAME} PROPERTIES LINK_FLAGS "-static") endif() +# Link system dependencies +if(WIN32) +elseif(APPLE) + target_link_libraries(${VSTPLUGIN_PRJ_NAME} PRIVATE ${APPLE_FOUNDATION_LIBRARY}) +else() + pkg_check_modules(GLIB REQUIRED glib-2.0) + target_include_directories(${VSTPLUGIN_PRJ_NAME} PRIVATE ${GLIB_INCLUDE_DIRS}) + target_link_libraries(${VSTPLUGIN_PRJ_NAME} PRIVATE ${GLIB_LIBRARIES}) +endif() + # Create the bundle (see "VST 3 Locations / Format") execute_process ( COMMAND "${CMAKE_COMMAND}" -E make_directory "${PROJECT_BINARY_DIR}/${VSTPLUGIN_BUNDLE_NAME}/Contents/Resources") diff --git a/vst/NativeHelpers.cpp b/vst/NativeHelpers.cpp new file mode 100644 index 00000000..3e373a63 --- /dev/null +++ b/vst/NativeHelpers.cpp @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#include "NativeHelpers.h" +#include + +#if defined(_WIN32) +#include +#include + +const fs::path& getUserDocumentsDirectory() +{ + static const fs::path directory = []() -> fs::path { + std::unique_ptr path(new WCHAR[32768]); + if (SHGetFolderPathW(nullptr, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path.get()) != S_OK) + throw std::runtime_error("Cannot get the document directory."); + return fs::path(path.get()); + }(); + return directory; +} +#elif defined(__APPLE__) + // implemented in NativeHelpers.mm +#else +#include + +const fs::path& getUserDocumentsDirectory() +{ + static const fs::path directory = []() -> fs::path { + const gchar *path = g_get_user_special_dir(G_USER_DIRECTORY_DOCUMENTS); + if (!path) + throw std::runtime_error("Cannot get the document directory."); + return fs::path(path); + }(); + return directory; +} +#endif diff --git a/vst/NativeHelpers.h b/vst/NativeHelpers.h new file mode 100644 index 00000000..766d01d7 --- /dev/null +++ b/vst/NativeHelpers.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once +#include + +const fs::path& getUserDocumentsDirectory(); diff --git a/vst/NativeHelpers.mm b/vst/NativeHelpers.mm new file mode 100644 index 00000000..85f924f1 --- /dev/null +++ b/vst/NativeHelpers.mm @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#include "NativeHelpers.h" +#import +#include + +#if defined(__APPLE__) +const fs::path& getUserDocumentsDirectory() +{ + static const fs::path directory = []() -> fs::path { + NSFileManager *fm = [NSFileManager defaultManager]; + NSArray *urls = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; + for (NSUInteger i = 0, n = [urls count]; i < n; ++i) { + NSURL *url = [urls objectAtIndex:i]; + if ([url isFileURL]) + return fs::path([url path].UTF8String); + } + throw std::runtime_error("Cannot get the document directory."); + }(); + return directory; +} +#endif