Skip to content

Commit

Permalink
Mpris blacklist support (#390)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: MrPenguin07 <[email protected]>
Co-authored-by: Erik Reider <[email protected]>
  • Loading branch information
4 people authored Jul 24, 2024
1 parent 653058b commit 723645e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion man/swaync.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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 ++
Expand Down Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/config.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"mpris": {
"image-size": 96,
"image-radius": 12
"blacklist": []
},
"buttons-grid": {
"actions": [
Expand Down
28 changes: 28 additions & 0 deletions src/controlCenter/widgets/mpris/mpris.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace SwayNotificationCenter.Widgets.Mpris {
int image_size;
int image_radius;
bool blur;
string[] blacklist;
}

public class Mpris : BaseWidget {
Expand Down Expand Up @@ -99,6 +100,18 @@ namespace SwayNotificationCenter.Widgets.Mpris {
bool blur_found;
bool? blur = get_prop<bool> (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 ();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 723645e

Please sign in to comment.