diff --git a/src/plugin/utils.cpp b/src/plugin/utils.cpp index 6cba591d..a09ff04d 100644 --- a/src/plugin/utils.cpp +++ b/src/plugin/utils.cpp @@ -391,3 +391,15 @@ Configuration load_config_for(const fs::path& yabridge_path) { return Configuration(*config_file, yabridge_path); } + +bool send_notification(const std::string& title, const std::string body) { + try { + return bp::system(bp::search_path("notify-send"), "--urgency=critical", + "--expire-time=20000", "--app-name=yabridge", title, + body) == EXIT_SUCCESS; + } catch (const boost::process::process_error&) { + // We will have printed the message to the terminal anyways, so if the + // user doesn't have libnotify installed we'll just fail silently + return false; + } +} diff --git a/src/plugin/utils.h b/src/plugin/utils.h index 1022ce05..a64b5a80 100644 --- a/src/plugin/utils.h +++ b/src/plugin/utils.h @@ -264,6 +264,21 @@ boost::filesystem::path get_this_file_location(); */ Configuration load_config_for(const boost::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 also + * basic HTML-like formatting, so special characters may need to be escaped. + * The message can also be empty. + * + * @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); + /** * Starting from the starting file or directory, go up in the directory * hierarchy until we find a file named `filename`.