Merge pull request #889 from jpcima/misc

Eliminate the glib2 requirement for UI-less plugins (LV2)
This commit is contained in:
JP Cimalando 2021-05-08 16:33:15 +02:00 committed by GitHub
commit a9b75c0091
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 68 deletions

View file

@ -41,10 +41,6 @@ elseif(APPLE)
target_link_libraries(plugins-common PRIVATE
"${APPLE_FOUNDATION_LIBRARY}")
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)
target_include_directories(plugins-common PRIVATE ${GLIB_INCLUDE_DIRS})
target_link_libraries(plugins-common PRIVATE ${GLIB_LIBRARIES})
endif()
if((SFIZZ_LV2 AND SFIZZ_LV2_UI) OR SFIZZ_VST OR SFIZZ_AU OR SFIZZ_VST2)

View file

@ -5,6 +5,9 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "NativeHelpers.h"
#include <absl/strings/match.h>
#include <absl/strings/strip.h>
#include <absl/strings/string_view.h>
#include <stdexcept>
#include <cstdlib>
@ -22,24 +25,119 @@ const fs::path& getUserDocumentsDirectory()
}();
return directory;
}
wchar_t *stringToWideChar(const char *str, int strCch)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<wchar_t[]> strW(new wchar_t[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
char* stringToUTF8(const wchar_t *strW, int strWCch)
{
unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr);
if (strSize == 0)
return {};
std::unique_ptr<char[]> str(new char[strSize]);
if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0)
return {};
return str.release();
}
#elif defined(__APPLE__)
// implemented in NativeHelpers.mm
#else
#include <glib.h>
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)
return fs::path(path);
else {
const char* home = getenv("HOME");
if (home && home[0] == '/')
return fs::path(home) / "Documents";
throw std::runtime_error("Cannot get the document directory.");
for (const XdgUserDirsEntry& ent :
parseXdgUserDirs(getXdgConfigHome() / "user-dirs.dirs")) {
if (ent.name == "XDG_DOCUMENTS_DIR")
return ent.value;
}
return getUserHomeDirectory() / "Documents";
}();
return directory;
}
const fs::path& getUserHomeDirectory()
{
static const fs::path directory = []() -> fs::path {
const char* home = getenv("HOME");
if (home && home[0] == '/')
return fs::u8path(home);
else
throw std::runtime_error("Cannot get the home directory.");
}();
return directory;
}
const fs::path& getXdgConfigHome()
{
static const fs::path directory = []() -> fs::path {
const char* config = getenv("XDG_CONFIG_HOME");
if (config && config[0] == '/')
return fs::u8path(config);
else
return getUserHomeDirectory() / ".config";
}();
return directory;
}
std::vector<XdgUserDirsEntry> parseXdgUserDirs(const fs::path& userDirsPath)
{
// from user-dirs.dirs(5)
// This file contains lines of the form `XDG_NAME_DIR=VALUE`
// VALUE must be of the form "$HOME/Path" or "/Path".
// Lines beginning with a # character are ignored.
std::vector<XdgUserDirsEntry> ents;
const fs::path& home = getUserHomeDirectory();
fs::ifstream in(userDirsPath);
std::string lineBuf;
lineBuf.reserve(256);
while (std::getline(in, lineBuf)) {
absl::string_view line(lineBuf);
line = absl::StripLeadingAsciiWhitespace(line);
if (line.empty() || line.front() == '#')
continue;
size_t pos = line.find('=');
if (pos == line.npos)
continue;
XdgUserDirsEntry ent;
ent.name = std::string(line.substr(0, pos));
absl::string_view rawValue = line.substr(pos + 1);
rawValue = absl::StripTrailingAsciiWhitespace(rawValue);
if (rawValue.size() < 2 || rawValue.front() != '"' || rawValue.back() != '"')
continue;
rawValue.remove_prefix(1);
rawValue.remove_suffix(1);
if (!rawValue.empty() && rawValue.front() == '/')
ent.value = fs::u8path(rawValue.begin(), rawValue.end());
else if (absl::StartsWith(rawValue, "$HOME")) {
absl::string_view part = rawValue.substr(5);
ent.value = home / fs::u8path(part.begin(), part.end()).relative_path();
}
else
continue;
ents.push_back(std::move(ent));
}
return ents;
}
#endif

View file

@ -6,5 +6,21 @@
#pragma once
#include <ghc/fs_std.hpp>
#include <vector>
#if defined(_WIN32)
#include <cwchar>
#endif
const fs::path& getUserDocumentsDirectory();
#if !defined(_WIN32) && !defined(__APPLE__)
const fs::path& getUserHomeDirectory();
const fs::path& getXdgConfigHome();
struct XdgUserDirsEntry { std::string name; fs::path value; };
std::vector<XdgUserDirsEntry> parseXdgUserDirs(const fs::path& userDirsPath);
#endif
#if defined(_WIN32)
wchar_t *stringToWideChar(const char *str, int strCch = -1);
char* stringToUTF8(const wchar_t *strW, int strWCch = -1);
#endif

View file

@ -5,6 +5,7 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "SfizzSettings.h"
#include "NativeHelpers.h"
#include <memory>
#include <cstdlib>
@ -36,28 +37,6 @@ static HKEY openRegistryKey()
return key;
}
static WCHAR* stringToWideChar(const char *str, int strCch = -1)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<WCHAR[]> strW(new WCHAR[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
static char* stringToUTF8(const wchar_t *strW, int strWCch = -1)
{
unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr);
if (strSize == 0)
return {};
std::unique_ptr<char[]> str(new char[strSize]);
if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0)
return {};
return str.release();
}
absl::optional<std::string> SfizzSettings::load(const char* name)
{
std::unique_ptr<WCHAR[]> nameW { stringToWideChar(name) };
@ -112,16 +91,7 @@ bool SfizzSettings::store(const char* name, absl::string_view value)
static const fs::path getSettingsPath()
{
fs::path dirPath;
const char* env;
if ((env = getenv("XDG_CONFIG_HOME")) && env[0] == '/')
dirPath = fs::path(env);
else if ((env = getenv("HOME")) && env[0] == '/')
dirPath = fs::path(env) / ".config";
else
return {};
dirPath /= "SFZTools";
dirPath /= "sfizz";
const fs::path dirPath = getXdgConfigHome() / "SFZTools" / "sfizz";
std::error_code ec;
fs::create_directories(dirPath, ec);
if (ec)

View file

@ -5,34 +5,13 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "NativeHelpers.h"
#include "plugin/NativeHelpers.h"
#if defined(_WIN32)
#include "ghc/fs_std.hpp"
#include <windows.h>
#include <cstring>
static WCHAR *stringToWideChar(const char *str, int strCch = -1)
{
unsigned strSize = MultiByteToWideChar(CP_UTF8, 0, str, strCch, nullptr, 0);
if (strSize == 0)
return {};
std::unique_ptr<WCHAR[]> strW(new WCHAR[strSize]);
if (MultiByteToWideChar(CP_UTF8, 0, str, strCch, strW.get(), strSize) == 0)
return {};
return strW.release();
}
static char* stringToUTF8(const wchar_t *strW, int strWCch = -1)
{
unsigned strSize = WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, nullptr, 0, nullptr, nullptr);
if (strSize == 0)
return {};
std::unique_ptr<char[]> str(new char[strSize]);
if (WideCharToMultiByte(CP_UTF8, 0, strW, strWCch, str.get(), strSize, nullptr, nullptr) == 0)
return {};
return str.release();
}
bool openFileInExternalEditor(const char *filename)
{
std::wstring path = stringToWideChar(filename);