From d17c538ccbec5fe4bb365be7eb5a2a3ef52ae9c2 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Thu, 17 Oct 2024 22:32:51 +0200 Subject: [PATCH] vapi changes --- src/playbin.vala | 61 ++++++++++++++--------------------------------- src/vapi/mpv.vapi | 38 +++++++++++++++++------------ 2 files changed, 41 insertions(+), 58 deletions(-) diff --git a/src/playbin.vala b/src/playbin.vala index e0c7ce4..fb0875f 100644 --- a/src/playbin.vala +++ b/src/playbin.vala @@ -55,16 +55,20 @@ class Playbin : GLib.Object { } private void on_play_queue_items_changed (ListModel play_queue, uint position, uint removed, uint added) { - try { - for (uint i = 0; i < removed; i += 1) { - assert (this.mpv.command ({"playlist-remove", position.to_string ()}) >= 0); - } + for (uint i = 0; i < removed; i += 1) { + assert (this.mpv.command ({ + "playlist-remove", + position.to_string (), + }) >= 0); + } - for (uint i = 0; i < added; i += 1) { - assert (this.mpv.command ({"loadfile", this.api.stream_uri (((Subsonic.Song) play_queue.get_item (position+i)).id), "insert-at-play", (position+i).to_string ()}) >= 0); - } - } catch (Error e) { - error ("could not update mpv playlist: %s\n", e.message); + for (uint i = 0; i < added; i += 1) { + assert (this.mpv.command ({ + "loadfile", + this.api.stream_uri (((Subsonic.Song) play_queue.get_item (position+i)).id), + "insert-at-play", + (position+i).to_string (), + }) >= 0); } } @@ -77,7 +81,6 @@ class Playbin : GLib.Object { assert (this.mpv.observe_property (0, "time-pos", Mpv.Format.DOUBLE) >= 0); assert (this.mpv.observe_property (1, "duration", Mpv.Format.DOUBLE) >= 0); assert (this.mpv.observe_property (2, "playlist-pos", Mpv.Format.INT64) >= 0); - this.mpv.wakeup_callback = () => { Idle.add (() => { @@ -87,37 +90,32 @@ class Playbin : GLib.Object { switch (event.event_id) { case Mpv.EventId.PROPERTY_CHANGE: + var data = event.parse_property (); switch (event.reply_userdata) { case 0: - var data = (Mpv.EventProperty *) event.data; assert (data.name == "time-pos"); if (data.format == Mpv.Format.NONE) { this.position = 0.0; } else { - assert (data.format == Mpv.Format.DOUBLE); - this.position = * (double *) data.data; + this.position = data.parse_double (); } break; case 1: - var data = (Mpv.EventProperty *) event.data; assert (data.name == "duration"); if (data.format == Mpv.Format.NONE) { this.duration = 0.0; } else { - assert (data.format == Mpv.Format.DOUBLE); - this.duration = * (double *) data.data; + this.duration = data.parse_double (); } break; case 2: - var data = (Mpv.EventProperty *) event.data; assert (data.name == "playlist-pos"); if (data.format == Mpv.Format.NONE) { this.play_queue_position = 0; } else { - assert (data.format == Mpv.Format.INT64); - this.play_queue_position = (uint) * (int64 *) data.data; + this.play_queue_position = (uint) data.parse_int64 (); } break; @@ -127,36 +125,13 @@ class Playbin : GLib.Object { } break; - case Mpv.EventId.START_FILE: - // ignore - break; - case Mpv.EventId.FILE_LOADED: this.song = (Subsonic.Song) this.play_queue.get_item (this.play_queue_position); this.now_playing (); break; - - case Mpv.EventId.PLAYBACK_RESTART: - // ignore - break; - - case Mpv.EventId.SEEK: - // ignore - break; - - case Mpv.EventId.END_FILE: - // ignore - break; - - // deprecated, ignore - case Mpv.EventId.IDLE: - case Mpv.EventId.TICK: - // uninteresting, ignore - case Mpv.EventId.AUDIO_RECONFIG: - break; default: - print ("got unimplemented %s\n", event.event_id.to_string ()); + // ignore by default break; } } diff --git a/src/vapi/mpv.vapi b/src/vapi/mpv.vapi index 1c9bfa4..dce8785 100644 --- a/src/vapi/mpv.vapi +++ b/src/vapi/mpv.vapi @@ -41,7 +41,7 @@ namespace Mpv { public Error initialize (); [CCode (cname = "mpv_wait_event")] - public unowned Event *wait_event (double timeout); + public unowned Event? wait_event (double timeout); public WakeupCallback wakeup_callback { [CCode (cname = "mpv_set_wakeup_callback")] set; @@ -63,9 +63,7 @@ namespace Mpv { } [CCode (cname = "mpv_command")] - public Error command ( - [CCode (array_length = false)] - string[] args); + public Error command ([CCode (array_length = false)] string[] args); [CCode (cname = "mpv_observe_property")] public Error observe_property (uint64 reply_userdata, string name, Format format); @@ -104,30 +102,40 @@ namespace Mpv { PROPERTY_CHANGE, QUEUE_OVERFLOW, HOOK, - - // deprecated - IDLE, - TICK, } - [CCode (cname = "mpv_event")] + [CCode (cname = "mpv_event", destroy_function = "", has_type_id = false, has_copy_function = false)] public struct Event { EventId event_id; Error error; uint64 reply_userdata; void *data; + + public unowned EventProperty? parse_property () + requires (event_id == EventId.PROPERTY_CHANGE) + requires (error >= 0) + { + return (Mpv.EventProperty?) data; + } } - [CCode (cname = "mpv_event_start_file")] - public struct EventStartFile { - int64 playlist_entry_id; - } - - [CCode (cname = "mpv_event_property")] + [CCode (cname = "mpv_event_property", destroy_function = "", has_type_id = false, has_copy_function = false)] public struct EventProperty { string name; Format format; void *data; + + public int64 parse_int64 () + requires (format == Format.INT64) + { + return * (int64 *) data; + } + + public double parse_double () + requires (format == Format.DOUBLE) + { + return * (double *) data; + } } }