From 723645ebb771f078108548a9f27e07e72160ad2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Costa?= Date: Wed, 24 Jul 2024 12:00:53 +0100 Subject: [PATCH] Mpris blacklist support (#390) * Initial mpris blacklist support * Fix merge conflict * Simplify blacklist type and parse * Fix man * Trim whitespaces * Fix blacklisting when a new player appears * Remove flake.nix * Remove log * Remove comment * Address review suggestions * Fixed linter errors --------- Co-authored-by: 12thgenpenguin Co-authored-by: MrPenguin07 <127086564+MrPenguin07@users.noreply.github.com> Co-authored-by: Erik Reider <35975961+ErikReider@users.noreply.github.com> --- man/swaync.5.scd | 12 +++++++++- src/config.json.in | 1 + src/controlCenter/widgets/mpris/mpris.vala | 28 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/man/swaync.5.scd b/man/swaync.5.scd index ada6f9b..effcbc6 100644 --- a/man/swaync.5.scd +++ b/man/swaync.5.scd @@ -330,6 +330,15 @@ config file to be able to detect config errors optional: true ++ default: 12 ++ description: The border radius of the album art. ++ + blacklist: ++ + type: array ++ + optional: true ++ + default: [] ++ + description: Audio sources for the mpris widget to ignore. ++ + Valid array values: ++ + type: string ++ + description: Audio source/app name. Regex alowed. Hint ++ + `$ qdbus | grep mpris` to find source names. ++ description: A widget that displays multiple music players. ++ *menubar*++ type: object ++ @@ -595,7 +604,8 @@ config file to be able to detect config errors }, "mpris": { "image-size": 96, - "image-radius": 12 + "image-radius": 12, + "blacklist": ["playerctld"] }, "menubar": { "menu#power": { diff --git a/src/config.json.in b/src/config.json.in index 14f4f1c..ddfac1c 100644 --- a/src/config.json.in +++ b/src/config.json.in @@ -74,6 +74,7 @@ "mpris": { "image-size": 96, "image-radius": 12 + "blacklist": [] }, "buttons-grid": { "actions": [ diff --git a/src/controlCenter/widgets/mpris/mpris.vala b/src/controlCenter/widgets/mpris/mpris.vala index 4c4cb98..041c58a 100644 --- a/src/controlCenter/widgets/mpris/mpris.vala +++ b/src/controlCenter/widgets/mpris/mpris.vala @@ -3,6 +3,7 @@ namespace SwayNotificationCenter.Widgets.Mpris { int image_size; int image_radius; bool blur; + string[] blacklist; } public class Mpris : BaseWidget { @@ -99,6 +100,18 @@ namespace SwayNotificationCenter.Widgets.Mpris { bool blur_found; bool? blur = get_prop (config, "blur", out blur_found); if (blur_found) mpris_config.blur = blur; + + Json.Array ? blacklist = get_prop_array (config, "blacklist"); + if (blacklist != null) { + mpris_config.blacklist = new string[blacklist.get_length ()]; + for (int i = 0; i < blacklist.get_length (); i++) { + if (blacklist.get_element (i).get_node_type () != Json.NodeType.VALUE) { + warning ("Blacklist entries should be strings"); + continue; + } + mpris_config.blacklist[i] = blacklist.get_string_element (i); + } + } } hide (); @@ -174,6 +187,7 @@ namespace SwayNotificationCenter.Widgets.Mpris { string[] names = dbus_iface.list_names (); foreach (string name in names) { if (!name.has_prefix (MPRIS_PREFIX)) continue; + if (is_blacklisted (name)) continue; if (check_player_exists (name)) return; MprisSource ? source = MprisSource.get_player (name); if (source != null) add_player (name, source); @@ -185,6 +199,7 @@ namespace SwayNotificationCenter.Widgets.Mpris { remove_player (name); return; } + if (is_blacklisted (name)) return; if (check_player_exists (name)) return; MprisSource ? source = MprisSource.get_player (name); if (source != null) add_player (name, source); @@ -243,5 +258,18 @@ namespace SwayNotificationCenter.Widgets.Mpris { 0, children_length - 1); carousel.scroll_to (children.nth_data (position)); } + + private bool is_blacklisted (string name) { + foreach (string blacklistedPattern in mpris_config.blacklist) { + if (blacklistedPattern == null || blacklistedPattern.length == 0) { + continue; + } + if (GLib.Regex.match_simple (blacklistedPattern, name, GLib.RegexCompileFlags.JAVASCRIPT_COMPAT, 0)) { + message ("\"%s\" is blacklisted", name); + return true; + } + } + return false; + } } }