From 2b2ace0f5c54c54879fb1b4bc5e61b35f29de024 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Fri, 25 Oct 2024 22:09:57 +0200 Subject: [PATCH] some async --- src/playbin.vala | 35 ++++++++++++++++++++++++++++++++++- src/subsonic.vala | 3 +++ src/vapi/mpv.vapi | 3 +++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/playbin.vala b/src/playbin.vala index af4603e..9f1145d 100644 --- a/src/playbin.vala +++ b/src/playbin.vala @@ -4,6 +4,11 @@ public enum PlaybinState { PLAYING, } +private struct CommandCallback { + unowned SourceFunc callback; + int error; +} + public class Playbin : GLib.Object { private Mpv.Handle mpv = new Mpv.Handle (); @@ -55,6 +60,16 @@ public class Playbin : GLib.Object { // try to prevent wait_event to be called twice private bool is_handling_event = false; + private async Mpv.Error mpv_command_async (string[] args) { + CommandCallback cc = {}; + + this.mpv.command_async ((uint64) &cc, args); + + cc.callback = this.mpv_command_async.callback; + yield; + return cc.error; + } + public Playbin () { this._play_queue = new ListStore (typeof (Subsonic.Song)); @@ -168,6 +183,12 @@ public class Playbin : GLib.Object { } break; + + case Mpv.EventId.COMMAND_REPLY: + unowned CommandCallback *cc = (CommandCallback *) event.reply_userdata; + cc.error = event.error; + cc.callback (); + break; default: // ignore by default @@ -268,7 +289,7 @@ public class Playbin : GLib.Object { } public void append_track (Subsonic.Song song) { - assert (this.mpv.command({ + assert (this.mpv.command ({ "loadfile", this.api.stream_uri (song.id), "append", @@ -277,6 +298,18 @@ public class Playbin : GLib.Object { this._play_queue.append (song); } + public async void append_track_async (Subsonic.Song song) { + var err = yield this.mpv_command_async ({ + "loadfile", + this.api.stream_uri (song.id), + "append", + }); + assert (err >= 0); + + if (this.state == STOPPED) this.play_queue_position += 1; + this._play_queue.append (song); + } + public void move_track (uint from, uint to) requires (from < this._play_queue.get_n_items ()) requires (to < this._play_queue.get_n_items ()) diff --git a/src/subsonic.vala b/src/subsonic.vala index 0bc7b4c..14fcb7a 100644 --- a/src/subsonic.vala +++ b/src/subsonic.vala @@ -224,6 +224,9 @@ public class Subsonic.Client : Object { reader.read_element (i); callback (new Song (reader)); reader.end_element (); + + Idle.add (this.get_random_songs.callback); + yield; } assert (reader.get_error () == null); diff --git a/src/vapi/mpv.vapi b/src/vapi/mpv.vapi index e397fa9..31a3b2c 100644 --- a/src/vapi/mpv.vapi +++ b/src/vapi/mpv.vapi @@ -65,6 +65,9 @@ namespace Mpv { [CCode (cname = "mpv_command")] public Error command ([CCode (array_length = false)] string[] args); + [CCode (cname = "mpv_command_async")] + public Error command_async (uint64 reply_userdata, [CCode (array_length = false)] string[] args); + [CCode (cname = "mpv_observe_property")] public Error observe_property (uint64 reply_userdata, string name, Format format); }