turn playbin.play_queue_position into an int

This commit is contained in:
Erica Z 2024-10-28 10:03:45 +01:00
parent 713b3d8842
commit dfe4a24c5b

View file

@ -40,8 +40,8 @@ public class Playbin : GLib.Object {
} }
} }
// invariant: equal to play queue length iff state is STOPPED // invariant: negative iff stopped, otherwise < play queue length
public uint play_queue_position { get; private set; } public int play_queue_position { get; private set; default = -1; }
// signalled when a new track is current // signalled when a new track is current
public signal void new_track (); public signal void new_track ();
@ -125,15 +125,15 @@ public class Playbin : GLib.Object {
int64 playlist_pos = data.parse_int64 (); int64 playlist_pos = data.parse_int64 ();
if (playlist_pos < 0) { if (playlist_pos < 0) {
if (this.state != PlaybinState.STOPPED) { if (this.state != PlaybinState.STOPPED) {
error ("mpv has no current playlist entry, but we think it's index %u", this.play_queue_position); error ("mpv has no current playlist entry, but we think it's index %d", this.play_queue_position);
} }
assert (this.play_queue_position == this.play_queue.get_n_items ()); assert (this.play_queue_position < 0);
} else { } else {
if (this.state == PlaybinState.STOPPED) { if (this.state == PlaybinState.STOPPED) {
error ("mpv is at playlist entry %u, but we're stopped", (uint) playlist_pos); error ("mpv is at playlist entry %d, but we're stopped", (int) playlist_pos);
} }
if (this.play_queue_position != (uint) playlist_pos) { if (this.play_queue_position != (int) playlist_pos) {
error ("mpv is at playlist entry %u, but we think it's %u", (uint) playlist_pos, this.play_queue_position); error ("mpv is at playlist entry %d, but we think it's %d", (int) playlist_pos, this.play_queue_position);
} }
} }
break; break;
@ -182,6 +182,7 @@ public class Playbin : GLib.Object {
if (this.play_queue_position == this._play_queue.get_n_items ()) { if (this.play_queue_position == this._play_queue.get_n_items ()) {
// reached the end (?) // reached the end (?)
this.state = PlaybinState.STOPPED; this.state = PlaybinState.STOPPED;
this.play_queue_position = -1;
this.stopped (); this.stopped ();
} }
} }
@ -220,7 +221,7 @@ public class Playbin : GLib.Object {
requires (position < this.play_queue.get_n_items ()) requires (position < this.play_queue.get_n_items ())
{ {
assert (this.mpv.command ({"playlist-play-index", position.to_string ()}) >= 0); assert (this.mpv.command ({"playlist-play-index", position.to_string ()}) >= 0);
this.play_queue_position = position; this.play_queue_position = (int) position;
this.state = PlaybinState.PLAYING; this.state = PlaybinState.PLAYING;
this.play (); // make sure mpv actually starts playing the track this.play (); // make sure mpv actually starts playing the track
} }
@ -283,6 +284,7 @@ public class Playbin : GLib.Object {
if (this.play_queue_position == this._play_queue.get_n_items ()) { if (this.play_queue_position == this._play_queue.get_n_items ()) {
// we just killed the last track // we just killed the last track
this.state = PlaybinState.STOPPED; this.state = PlaybinState.STOPPED;
this.play_queue_position = -1;
this.stopped (); this.stopped ();
} }
} }
@ -294,7 +296,7 @@ public class Playbin : GLib.Object {
} }
this.state = PlaybinState.STOPPED; this.state = PlaybinState.STOPPED;
this._play_queue.remove_all (); this._play_queue.remove_all ();
this.play_queue_position = 0; this.play_queue_position = -1;
this.stopped (); this.stopped ();
} }
@ -305,7 +307,6 @@ public class Playbin : GLib.Object {
this.api.stream_uri (song.id), this.api.stream_uri (song.id),
"append", "append",
}) >= 0); }) >= 0);
if (this.state == STOPPED) this.play_queue_position += 1;
this._play_queue.append (song); this._play_queue.append (song);
} }
@ -317,7 +318,6 @@ public class Playbin : GLib.Object {
}); });
assert (err >= 0); assert (err >= 0);
if (this.state == STOPPED) this.play_queue_position += 1;
this._play_queue.append (song); this._play_queue.append (song);
} }
@ -348,8 +348,8 @@ public class Playbin : GLib.Object {
additions[to-from] = this._play_queue.get_item (from); additions[to-from] = this._play_queue.get_item (from);
this._play_queue.splice(from, to-from+1, additions); this._play_queue.splice(from, to-from+1, additions);
if (this.play_queue_position == from) this.play_queue_position = to; if (this.play_queue_position == (int) from) this.play_queue_position = (int) to;
else if (this.play_queue_position > from && this.play_queue_position <= to) this.play_queue_position -= 1; else if (this.play_queue_position > (int) from && this.play_queue_position <= (int) to) this.play_queue_position -= 1;
} else if (from > to) { } else if (from > to) {
assert(this.mpv.command({ assert(this.mpv.command({
"playlist-move", "playlist-move",
@ -365,8 +365,8 @@ public class Playbin : GLib.Object {
} }
this._play_queue.splice (to, from-to+1, additions); this._play_queue.splice (to, from-to+1, additions);
if (this.play_queue_position == from) this.play_queue_position = to; if (this.play_queue_position == (int) from) this.play_queue_position = (int) to;
else if (this.play_queue_position >= to && this.play_queue_position < from) this.play_queue_position += 1; else if (this.play_queue_position >= (int) to && this.play_queue_position < (int) from) this.play_queue_position += 1;
} }
} }
@ -375,7 +375,7 @@ public class Playbin : GLib.Object {
// don't clear the playlist, just in case (less state changes to sync) // don't clear the playlist, just in case (less state changes to sync)
assert(this.mpv.command({"stop", "keep-playlist"}) >= 0); assert(this.mpv.command({"stop", "keep-playlist"}) >= 0);
this.state = PlaybinState.STOPPED; this.state = PlaybinState.STOPPED;
this.play_queue_position = this._play_queue.get_n_items (); this.play_queue_position = -1;
this.stopped (); this.stopped ();
} }