Move desktop notifications to its own header

We'll need to use this from the chainloader.
This commit is contained in:
Robbert van der Helm
2022-04-16 14:57:26 +02:00
parent fde90d7bc3
commit 46af07748b
12 changed files with 246 additions and 193 deletions
+177
View File
@@ -0,0 +1,177 @@
// yabridge: a Wine plugin bridge
// Copyright (C) 2020-2022 Robbert van der Helm
//
// 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 3 of the License, or
// (at your option) 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.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "notifications.h"
#include <sstream>
#include "process.h"
#include "utils.h"
bool send_notification(const std::string& title,
const std::string body,
std::optional<ghc::filesystem::path> origin) {
// I think there's a zero chance that we're going to call this function with
// anything that even somewhat resembles HTML, but we should still do a
// basic XML escape anyways.
std::ostringstream formatted_body;
formatted_body << xml_escape(body);
// If the path to the current library file is provided, then we'll append
// the path to that library file to the message. In earlier versions we
// would detect the library path right here, but that will not work with
// chainloaded plugins as they will load the actual plugin libraries from
// fixed locations.
if (origin) {
try {
formatted_body << "\n"
<< "Source: <a href=\"file://"
<< url_encode_path(origin->parent_path().string())
<< "\">" << xml_escape(origin->filename().string())
<< "</a>";
} catch (const std::system_error&) {
// I don't think this can fail in the way we're using it, but the
// last thing we want is our notification informing the user of an
// exception to trigger another exception
}
}
Process process("notify-send");
process.arg("--urgency=normal");
process.arg("--app-name=yabridge");
process.arg(title);
process.arg(formatted_body.str());
// We will have printed the message to the terminal anyways, so if the user
// doesn't have libnotify installed we'll just fail silently
const auto result = process.spawn_get_status();
return std::visit(
overload{
[](int status) -> bool { return status == EXIT_SUCCESS; },
[](const Process::CommandNotFound&) -> bool { return false; },
[](const std::error_code&) -> bool { return false; },
},
result);
}
std::string xml_escape(std::string string) {
// Implementation idea stolen from https://stackoverflow.com/a/5665377
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(string.size()) * 1.1));
for (const char& character : string) {
switch (character) {
case '&':
escaped.append("&amp;");
break;
case '\"':
escaped.append("&quot;");
break;
case '\'':
escaped.append("&apos;");
break;
case '<':
escaped.append("&lt;");
break;
case '>':
escaped.append("&gt;");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}
std::string url_encode_path(std::string path) {
// We only need to escape a couple of special characters here. This is used
// in the notifications as well as in the XDND proxy. We encode the reserved
// characters mentioned here, with the exception of the forward slash:
// https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(path.size()) * 1.1));
for (const char& character : path) {
switch (character) {
// Spaces are somehow in the above list, but Bitwig Studio requires
// spaces to be escaped in the `text/uri-list` format
case ' ':
escaped.append("%20");
break;
case '!':
escaped.append("%21");
break;
case '#':
escaped.append("%23");
break;
case '$':
escaped.append("%24");
break;
case '%':
escaped.append("%25");
break;
case '&':
escaped.append("%26");
break;
case '\'':
escaped.append("%27");
break;
case '(':
escaped.append("%28");
break;
case ')':
escaped.append("%29");
break;
case '*':
escaped.append("%2A");
break;
case '+':
escaped.append("%2B");
break;
case ',':
escaped.append("%2C");
break;
case ':':
escaped.append("%3A");
break;
case ';':
escaped.append("%3B");
break;
case '=':
escaped.append("%3D");
break;
case '?':
escaped.append("%3F");
break;
case '@':
escaped.append("%40");
break;
case '[':
escaped.append("%5B");
break;
case ']':
escaped.append("%5D");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}
+58
View File
@@ -0,0 +1,58 @@
// yabridge: a Wine plugin bridge
// Copyright (C) 2020-2022 Robbert van der Helm
//
// 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 3 of the License, or
// (at your option) 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.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
// This header is used by the plugins and the chainloaders to send desktop
// notifications when something goes wrong
#include <ghc/filesystem.hpp>
#include <optional>
// TODO: At some point, provide an alternative to notify-send by dlopen()-ing
// libdbus instead. Some more obscure distros won't have notify-send
// available.
/**
* Send a desktop notification using `notify-send`. Used for diagnostics when a
* plugin fails to load since the user may not be checking the output in a
* terminal.
*
* @param title The title (or technically, summary) of the notification.
* @param body The message to display. This can contain line feeds, and it any
* HTML tags and XML escape sequences will be automatically escaped. The
* message can also be empty.
* @param origin If this is set to the current plugin's path, then the
* notification will append a 'Source: <XXX.so>' hyperlink to the body so the
* user can more easily navigate to the plugin's path.
*
* @return Whether the notification was sent. This will be false if
* `notify-send` is not available.
*/
bool send_notification(const std::string& title,
const std::string body,
std::optional<ghc::filesystem::path> origin);
/**
* Escape XML entities within a string. Used inside of desktop notifications.
*/
std::string xml_escape(std::string string);
/**
* URL encode a file path. We won't escape forward slashes, and `path` should
* not yet include the `file://` prefix.
*/
std::string url_encode_path(std::string path);
-109
View File
@@ -94,115 +94,6 @@ bool is_watchdog_timer_disabled() {
return disable_watchdog_env && disable_watchdog_env == "1"sv; return disable_watchdog_env && disable_watchdog_env == "1"sv;
} }
std::string url_encode_path(std::string path) {
// We only need to escape a couple of special characters here. This is used
// in the notifications as well as in the XDND proxy. We encode the reserved
// characters mentioned here, with the exception of the forward slash:
// https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(path.size()) * 1.1));
for (const char& character : path) {
switch (character) {
// Spaces are somehow in the above list, but Bitwig Studio requires
// spaces to be escaped in the `text/uri-list` format
case ' ':
escaped.append("%20");
break;
case '!':
escaped.append("%21");
break;
case '#':
escaped.append("%23");
break;
case '$':
escaped.append("%24");
break;
case '%':
escaped.append("%25");
break;
case '&':
escaped.append("%26");
break;
case '\'':
escaped.append("%27");
break;
case '(':
escaped.append("%28");
break;
case ')':
escaped.append("%29");
break;
case '*':
escaped.append("%2A");
break;
case '+':
escaped.append("%2B");
break;
case ',':
escaped.append("%2C");
break;
case ':':
escaped.append("%3A");
break;
case ';':
escaped.append("%3B");
break;
case '=':
escaped.append("%3D");
break;
case '?':
escaped.append("%3F");
break;
case '@':
escaped.append("%40");
break;
case '[':
escaped.append("%5B");
break;
case ']':
escaped.append("%5D");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}
std::string xml_escape(std::string string) {
// Implementation idea stolen from https://stackoverflow.com/a/5665377
std::string escaped;
escaped.reserve(
static_cast<size_t>(static_cast<double>(string.size()) * 1.1));
for (const char& character : string) {
switch (character) {
case '&':
escaped.append("&amp;");
break;
case '\"':
escaped.append("&quot;");
break;
case '\'':
escaped.append("&apos;");
break;
case '<':
escaped.append("&lt;");
break;
case '>':
escaped.append("&gt;");
break;
default:
escaped.push_back(character);
break;
}
}
return escaped;
}
ScopedFlushToZero::ScopedFlushToZero() noexcept { ScopedFlushToZero::ScopedFlushToZero() noexcept {
old_ftz_mode_ = _MM_GET_FLUSH_ZERO_MODE(); old_ftz_mode_ = _MM_GET_FLUSH_ZERO_MODE();
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
-11
View File
@@ -140,17 +140,6 @@ std::optional<rlim_t> get_rttime_limit() noexcept;
*/ */
bool is_watchdog_timer_disabled(); bool is_watchdog_timer_disabled();
/**
* URL encode a file path. We won't escape forward slashes, and `path` should
* not yet include the `file://` prefix.
*/
std::string url_encode_path(std::string path);
/**
* Escape XML entities within a string. Used inside of desktop notifications.
*/
std::string xml_escape(std::string string);
/** /**
* A RAII wrapper that will temporarily enable the FTZ flag so that denormals * A RAII wrapper that will temporarily enable the FTZ flag so that denormals
* are automatically flushed to zero, returning to whatever the flag was * are automatically flushed to zero, returning to whatever the flag was
+1 -3
View File
@@ -26,12 +26,10 @@
#include <version.h> #include <version.h>
#include "../../common/configuration.h" #include "../../common/configuration.h"
#include "../../common/notifications.h"
#include "../../common/utils.h" #include "../../common/utils.h"
#include "../host-process.h" #include "../host-process.h"
// FIXME: This should be passed as an argument instead
#include "../../common/linking.h"
/** /**
* If the amount of lockable memory is below this, then we'll warn about it * If the amount of lockable memory is below this, then we'll warn about it
* during startup. Otherwise we may run into issues when mapping shared memory * during startup. Otherwise we may run into issues when mapping shared memory
+2
View File
@@ -10,6 +10,7 @@ vst2_plugin_sources = files(
'../common/logging/vst2.cpp', '../common/logging/vst2.cpp',
'../common/audio-shm.cpp', '../common/audio-shm.cpp',
'../common/linking.cpp', '../common/linking.cpp',
'../common/notifications.cpp',
'../common/plugins.cpp', '../common/plugins.cpp',
'../common/process.cpp', '../common/process.cpp',
'../common/utils.cpp', '../common/utils.cpp',
@@ -82,6 +83,7 @@ vst3_plugin_sources = files(
'../common/audio-shm.cpp', '../common/audio-shm.cpp',
'../common/configuration.cpp', '../common/configuration.cpp',
'../common/linking.cpp', '../common/linking.cpp',
'../common/notifications.cpp',
'../common/plugins.cpp', '../common/plugins.cpp',
'../common/process.cpp', '../common/process.cpp',
'../common/utils.cpp', '../common/utils.cpp',
-46
View File
@@ -422,49 +422,3 @@ Configuration load_config_for(const fs::path& yabridge_path) {
return Configuration(*config_file, yabridge_path); return Configuration(*config_file, yabridge_path);
} }
bool send_notification(const std::string& title,
const std::string body,
std::optional<ghc::filesystem::path> origin) {
// I think there's a zero chance that we're going to call this function with
// anything that even somewhat resembles HTML, but we should still do a
// basic XML escape anyways.
std::ostringstream formatted_body;
formatted_body << xml_escape(body);
// If the path to the current library file is provided, then we'll append
// the path to that library file to the message. In earlier versions we
// would detect the library path right here, but that will not work with
// chainloaded plugins as they will load the actual plugin libraries from
// fixed locations.
if (origin) {
try {
formatted_body << "\n"
<< "Source: <a href=\"file://"
<< url_encode_path(origin->parent_path().string())
<< "\">" << xml_escape(origin->filename().string())
<< "</a>";
} catch (const std::system_error&) {
// I don't think this can fail in the way we're using it, but the
// last thing we want is our notification informing the user of an
// exception to trigger another exception
}
}
Process process("notify-send");
process.arg("--urgency=normal");
process.arg("--app-name=yabridge");
process.arg(title);
process.arg(formatted_body.str());
// We will have printed the message to the terminal anyways, so if the user
// doesn't have libnotify installed we'll just fail silently
const auto result = process.spawn_get_status();
return std::visit(
overload{
[](int status) -> bool { return status == EXIT_SUCCESS; },
[](const Process::CommandNotFound&) -> bool { return false; },
[](const std::error_code&) -> bool { return false; },
},
result);
}
-20
View File
@@ -263,26 +263,6 @@ std::vector<ghc::filesystem::path> get_augmented_search_path();
*/ */
Configuration load_config_for(const ghc::filesystem::path& yabridge_path); Configuration load_config_for(const ghc::filesystem::path& yabridge_path);
/**
* Send a desktop notification using `notify-send`. Used for diagnostics when a
* plugin fails to load since the user may not be checking the output in a
* terminal.
*
* @param title The title (or technically, summary) of the notification.
* @param body The message to display. This can contain line feeds, and it any
* HTML tags and XML escape sequences will be automatically escaped. The
* message can also be empty.
* @param origin If this is set to the current plugin's path, then the
* notification will append a 'Source: <XXX.so>' hyperlink to the body so the
* user can more easily navigate to the plugin's path.
*
* @return Whether the notification was sent. This will be false if
* `notify-send` is not available.
*/
bool send_notification(const std::string& title,
const std::string body,
std::optional<ghc::filesystem::path> origin);
/** /**
* Starting from the starting file or directory, go up in the directory * Starting from the starting file or directory, go up in the directory
* hierarchy until we find a file named `filename`. * hierarchy until we find a file named `filename`.
+1
View File
@@ -19,6 +19,7 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include "../common/linking.h"
#include "../common/logging/common.h" #include "../common/logging/common.h"
#include "bridges/vst2.h" #include "bridges/vst2.h"
+5 -4
View File
@@ -14,10 +14,6 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "bridges/vst3.h"
using namespace std::literals::string_literals;
// FIXME: The VST3 SDK as of version 3.7.2 now includes multiple local functions // FIXME: The VST3 SDK as of version 3.7.2 now includes multiple local functions
// called `InitModule` and `DeinitModule`: one in the new // called `InitModule` and `DeinitModule`: one in the new
// `public.sdk/source/main/initmodule.cpp`, and the existing ones in the // `public.sdk/source/main/initmodule.cpp`, and the existing ones in the
@@ -29,6 +25,9 @@ using namespace std::literals::string_literals;
// NOLINTNEXTLINE(bugprone-suspicious-include) // NOLINTNEXTLINE(bugprone-suspicious-include)
#include <public.sdk/source/main/linuxmain.cpp> #include <public.sdk/source/main/linuxmain.cpp>
#include "../common/linking.h"
#include "bridges/vst3.h"
using namespace std::literals::string_literals; using namespace std::literals::string_literals;
namespace fs = ghc::filesystem; namespace fs = ghc::filesystem;
@@ -49,6 +48,8 @@ namespace fs = ghc::filesystem;
std::unique_ptr<Vst3PluginBridge> bridge; std::unique_ptr<Vst3PluginBridge> bridge;
// These functions are called by the `ModuleEntry` and `ModuleExit` functions on
// the first load and load unload
bool InitModule() { bool InitModule() {
assert(!bridge); assert(!bridge);
+1
View File
@@ -61,6 +61,7 @@ host_common_sources = files(
'../common/logging/common.cpp', '../common/logging/common.cpp',
'../common/logging/vst2.cpp', '../common/logging/vst2.cpp',
'../common/audio-shm.cpp', '../common/audio-shm.cpp',
'../common/notifications.cpp',
'../common/plugins.cpp', '../common/plugins.cpp',
'../common/process.cpp', '../common/process.cpp',
'../common/utils.cpp', '../common/utils.cpp',
+1
View File
@@ -19,6 +19,7 @@
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>
#include "../common/notifications.h"
#include "editor.h" #include "editor.h"
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;