diff options
author | Aylur <[email protected]> | 2024-07-14 20:58:16 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-07-14 20:58:16 +0200 |
commit | 6b63c58c3cba4559cb0a38cadbb1bac80d62e108 (patch) | |
tree | 5b3109c21459f97d4b04f6c1349ca44111ed7586 | |
parent | 0e1008aaa2335d7fe0a17315e32c171ef5894efd (diff) |
fix position getter
-rw-r--r-- | flake.lock | 6 | ||||
-rw-r--r-- | flake.nix | 1 | ||||
-rw-r--r-- | src/mpris.vala | 1 | ||||
-rw-r--r-- | src/player.vala | 73 |
4 files changed, 39 insertions, 42 deletions
@@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1718895438, - "narHash": "sha256-k3JqJrkdoYwE3fHE6xGDY676AYmyh4U2Zw+0Bwe5DLU=", + "lastModified": 1720768451, + "narHash": "sha256-EYekUHJE2gxeo2pM/zM9Wlqw1Uw2XTJXOSAO79ksc4Y=", "owner": "nixos", "repo": "nixpkgs", - "rev": "d603719ec6e294f034936c0d0dc06f689d91b6c3", + "rev": "7e7c39ea35c5cdd002cd4588b03a3fb9ece6fad9", "type": "github" }, "original": { @@ -17,6 +17,7 @@ ]; buildInputs = with pkgs; [ + gvfs glib json-glib ]; diff --git a/src/mpris.vala b/src/mpris.vala index 795cfab..0e55a2e 100644 --- a/src/mpris.vala +++ b/src/mpris.vala @@ -20,7 +20,6 @@ public class Mpris : Object { new HashTable<string, Player> (str_hash, str_equal); public List<weak Player> players { owned get { return _players.get_values(); } } - public bool poll_position { get; set; } public signal void player_added (Player player); public signal void player_closed (Player player); diff --git a/src/player.vala b/src/player.vala index e377bae..297fb6e 100644 --- a/src/player.vala +++ b/src/player.vala @@ -11,8 +11,7 @@ public class Player : Object { public string bus_name { owned get; construct set; } public bool available { get; private set; } - // settings - public bool poll_position { get; construct set; } + // periodically notify position private uint pollid; // mpris @@ -94,19 +93,26 @@ public class Player : Object { public signal void seeked (int64 position); - // dbus does not refresh position value - // as a workaround we need to create a proxy each time - private double _get_position() { + public double _get_position() { try { - IPlayer p = Bus.get_proxy_sync( - BusType.SESSION, - bus_name, - "/org/mpris/MediaPlayer2" + var reply = proxy.call_sync( + "org.freedesktop.DBus.Properties.Get", + new Variant("(ss)", + "org.mpris.MediaPlayer2.Player", + "Position" + ), + DBusCallFlags.NONE, + -1, + null ); - return (double)p.position / 1000000; - } catch (Error error) { - critical(error.message); + var body = reply.get_child_value(0); + if (body.classify() == Variant.Class.STRING) { + return -1; // Position not supported + } + + return (double)body.get_variant().get_int64() / 1000000; + } catch (Error err) { return -1; } } @@ -116,7 +122,6 @@ public class Player : Object { proxy.set_position(new ObjectPath(get_str("mpris:trackid")), (int64)(pos * 1000000)); } catch (Error error) { critical(error.message); - print("hello\n"); } } @@ -124,7 +129,6 @@ public class Player : Object { private double _rate; private Shuffle _shuffle_status = Shuffle.UNSUPPORTED; private double _volume = -1; - private double _position = -1; public Loop loop_status { get { return _loop_status; } @@ -147,7 +151,7 @@ public class Player : Object { } public double position { - get { return _position; } + get { return _get_position(); } set { _set_position(value); } } @@ -197,6 +201,9 @@ public class Player : Object { supported_uri_schemas = proxy.supported_uri_schemas; supported_mime_types = proxy.supported_mime_types; + if (position >= 0) + notify_property("position"); + // LoopStatus and Shuffle are optional props var props = proxy.get_all("org.mpris.MediaPlayer2.Player"); @@ -225,12 +232,6 @@ public class Player : Object { notify_property("volume"); } - var pos = _get_position(); - if (position != pos) { - _position = pos; - notify_property("position"); - } - playback_status = PlaybackStatus.from_string(proxy.playback_status); minimum_rate = proxy.minimum_rate; maximum_rate = proxy.maximum_rate; @@ -307,11 +308,12 @@ public class Player : Object { return metadata.lookup(key); } - private string? get_str(string key) { + private string get_str(string key) { if (metadata.get(key) == null) - return null; + return ""; - return metadata.get(key).get_string(null); + var str = metadata.get(key).get_string(null); + return str == null ? "" : str; } private string? join_strv(string key, string sep) { @@ -363,24 +365,19 @@ public class Player : Object { proxy.g_properties_changed.connect(sync); - if (poll_position) { - pollid = Timeout.add_seconds(1, () => { - if (!available) - return Source.CONTINUE; - - var pos = _get_position(); - if (position != pos) { - _position = pos; - notify_property("position"); - } + pollid = Timeout.add_seconds(1, () => { + if (!available) return Source.CONTINUE; - }, Priority.DEFAULT); - } + + if (position >= 0) { + notify_property("position"); + } + return Source.CONTINUE; + }, Priority.DEFAULT); } ~Player() { - if (poll_position) - Source.remove(pollid); + Source.remove(pollid); } } |