sequence numbers

This commit is contained in:
Mark
2024-12-08 12:03:24 +01:00
parent d02646406d
commit c3622aca30
18 changed files with 429 additions and 257 deletions

View File

@@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
musicdb-lib = { path = "../musicdb-lib" }
musicdb-lib = { path = "../musicdb-lib", default-features = false }
clap = { version = "4.4.6", features = ["derive"] }
directories = "5.0.1"
regex = "1.9.3"
@@ -16,7 +16,7 @@ musicdb-mers = { version = "0.1.0", path = "../musicdb-mers", optional = true }
uianimator = "0.1.1"
[features]
default = ["gui", "playback", "merscfg"]
default = ["gui", "default-playback"]
# gui:
# enables the gui modes
# merscfg:
@@ -26,6 +26,9 @@ default = ["gui", "playback", "merscfg"]
# playback:
# enables syncplayer modes, where the client mirrors the server's playback
gui = ["speedy2d"]
merscfg = ["mers", "speedy2d"]
merscfg = ["mers", "gui"]
mers = ["musicdb-mers"]
playback = ["musicdb-lib/playback"]
playback = []
default-playback = ["playback", "musicdb-lib/default-playback"]
playback-via-playback-rs = ["playback", "musicdb-lib/playback-via-playback-rs"]
playback-via-rodio = ["playback", "musicdb-lib/playback-via-rodio"]

View File

@@ -16,7 +16,7 @@ use musicdb_lib::{
AlbumId, ArtistId, CoverId, SongId,
},
load::ToFromBytes,
server::{get, Command},
server::{get, Action},
};
use speedy2d::{
color::Color,
@@ -363,84 +363,86 @@ impl Gui {
Ok(Ok(Ok(()))) => eprintln!("Info: using merscfg"),
}
database.lock().unwrap().update_endpoints.push(
musicdb_lib::data::database::UpdateEndpoint::Custom(Box::new(move |cmd| match cmd {
Command::Resume
| Command::Pause
| Command::Stop
| Command::Save
| Command::InitComplete => {}
Command::NextSong
| Command::QueueUpdate(..)
| Command::QueueAdd(..)
| Command::QueueInsert(..)
| Command::QueueRemove(..)
| Command::QueueMove(..)
| Command::QueueMoveInto(..)
| Command::QueueGoto(..)
| Command::QueueShuffle(..)
| Command::QueueSetShuffle(..)
| Command::QueueUnshuffle(..) => {
if let Some(s) = &*event_sender_arc.lock().unwrap() {
_ = s.send_event(GuiEvent::UpdatedQueue);
musicdb_lib::data::database::UpdateEndpoint::Custom(Box::new(move |cmd| {
match &cmd.action {
Action::Resume
| Action::Pause
| Action::Stop
| Action::Save
| Action::InitComplete => {}
Action::NextSong
| Action::QueueUpdate(..)
| Action::QueueAdd(..)
| Action::QueueInsert(..)
| Action::QueueRemove(..)
| Action::QueueMove(..)
| Action::QueueMoveInto(..)
| Action::QueueGoto(..)
| Action::QueueShuffle(..)
| Action::QueueSetShuffle(..)
| Action::QueueUnshuffle(..) => {
if let Some(s) = &*event_sender_arc.lock().unwrap() {
_ = s.send_event(GuiEvent::UpdatedQueue);
}
}
}
Command::SyncDatabase(..)
| Command::AddSong(_)
| Command::AddAlbum(_)
| Command::AddArtist(_)
| Command::AddCover(_)
| Command::ModifySong(_)
| Command::ModifyAlbum(_)
| Command::ModifyArtist(_)
| Command::RemoveSong(_)
| Command::RemoveAlbum(_)
| Command::RemoveArtist(_)
| Command::TagSongFlagSet(..)
| Command::TagSongFlagUnset(..)
| Command::TagAlbumFlagSet(..)
| Command::TagAlbumFlagUnset(..)
| Command::TagArtistFlagSet(..)
| Command::TagArtistFlagUnset(..)
| Command::TagSongPropertySet(..)
| Command::TagSongPropertyUnset(..)
| Command::TagAlbumPropertySet(..)
| Command::TagAlbumPropertyUnset(..)
| Command::TagArtistPropertySet(..)
| Command::TagArtistPropertyUnset(..)
| Command::SetSongDuration(..) => {
if let Some(s) = &*event_sender_arc.lock().unwrap() {
_ = s.send_event(GuiEvent::UpdatedLibrary);
Action::SyncDatabase(..)
| Action::AddSong(_)
| Action::AddAlbum(_)
| Action::AddArtist(_)
| Action::AddCover(_)
| Action::ModifySong(_)
| Action::ModifyAlbum(_)
| Action::ModifyArtist(_)
| Action::RemoveSong(_)
| Action::RemoveAlbum(_)
| Action::RemoveArtist(_)
| Action::TagSongFlagSet(..)
| Action::TagSongFlagUnset(..)
| Action::TagAlbumFlagSet(..)
| Action::TagAlbumFlagUnset(..)
| Action::TagArtistFlagSet(..)
| Action::TagArtistFlagUnset(..)
| Action::TagSongPropertySet(..)
| Action::TagSongPropertyUnset(..)
| Action::TagAlbumPropertySet(..)
| Action::TagAlbumPropertyUnset(..)
| Action::TagArtistPropertySet(..)
| Action::TagArtistPropertyUnset(..)
| Action::SetSongDuration(..) => {
if let Some(s) = &*event_sender_arc.lock().unwrap() {
_ = s.send_event(GuiEvent::UpdatedLibrary);
}
}
}
Command::ErrorInfo(t, d) => {
let (t, d) = (t.clone(), d.clone());
notif_sender_two
.send(Box::new(move |_| {
(
Box::new(Panel::with_background(
GuiElemCfg::default(),
[Label::new(
Action::ErrorInfo(t, d) => {
let (t, d) = (t.clone(), d.clone());
notif_sender_two
.send(Box::new(move |_| {
(
Box::new(Panel::with_background(
GuiElemCfg::default(),
if t.is_empty() {
format!("Server message\n{d}")
} else {
format!("Server error ({t})\n{d}")
},
Color::WHITE,
None,
Vec2::new(0.5, 0.5),
)],
Color::from_rgba(0.0, 0.0, 0.0, 0.8),
)),
if t.is_empty() {
NotifInfo::new(Duration::from_secs(2))
} else {
NotifInfo::new(Duration::from_secs(5))
.with_highlight(Color::RED)
},
)
}))
.unwrap();
[Label::new(
GuiElemCfg::default(),
if t.is_empty() {
format!("Server message\n{d}")
} else {
format!("Server error ({t})\n{d}")
},
Color::WHITE,
None,
Vec2::new(0.5, 0.5),
)],
Color::from_rgba(0.0, 0.0, 0.0, 0.8),
)),
if t.is_empty() {
NotifInfo::new(Duration::from_secs(2))
} else {
NotifInfo::new(Duration::from_secs(5))
.with_highlight(Color::RED)
},
)
}))
.unwrap();
}
}
})),
);
@@ -1191,7 +1193,7 @@ pub enum GuiAction {
ShowNotification(Box<dyn FnOnce(&NotifOverlay) -> (Box<dyn GuiElem>, NotifInfo) + Send>),
/// Build the GuiAction(s) later, when we have access to the Database (can turn an AlbumId into a QueueContent::Folder, etc)
Build(Box<dyn FnOnce(&mut Database) -> Vec<Self>>),
SendToServer(Command),
SendToServer(Action),
ContextMenu(Option<(Vec<Box<dyn GuiElem>>)>),
/// unfocuses all gui elements, then assigns keyboard focus to one with config().request_keyboard_focus == true if there is one.
ResetKeyboardFocus,
@@ -1304,10 +1306,17 @@ impl Gui {
self.keybinds.insert(bind, action.with_priority(priority));
}
}
GuiAction::SendToServer(cmd) => {
GuiAction::SendToServer(action) => {
#[cfg(debug_assertions)]
eprintln!("[DEBUG] Sending command to server: {cmd:?}");
if let Err(e) = cmd.to_bytes(&mut self.connection) {
eprintln!("[DEBUG] Sending command to server: {action:?}");
if let Err(e) = self
.database
.lock()
.unwrap()
.seq
.pack(action)
.to_bytes(&mut self.connection)
{
eprintln!("Error sending command to server: {e}");
}
}
@@ -1551,7 +1560,7 @@ impl WindowHandler<GuiEvent> for Gui {
| Dragging::Queue(Ok(_))
| Dragging::Queues(_) => (),
Dragging::Queue(Err(path)) => {
self.exec_gui_action(GuiAction::SendToServer(Command::QueueRemove(path)))
self.exec_gui_action(GuiAction::SendToServer(Action::QueueRemove(path)))
}
}
}

View File

@@ -2,7 +2,7 @@ use std::time::Instant;
use musicdb_lib::{
data::{song::Song, ArtistId},
server::Command,
server::Action,
};
use speedy2d::{color::Color, dimen::Vec2, shape::Rectangle};
@@ -189,7 +189,7 @@ impl GuiElem for EditorForSongs {
song.album = None;
}
info.actions
.push(GuiAction::SendToServer(Command::ModifySong(song)));
.push(GuiAction::SendToServer(Action::ModifySong(song)));
}
}
Event::SetArtist(name, id) => {

View File

@@ -1,6 +1,6 @@
use std::sync::{atomic::AtomicBool, Arc};
use musicdb_lib::server::Command;
use musicdb_lib::server::Action;
use speedy2d::{color::Color, dimen::Vec2, shape::Rectangle, Graphics2D};
use crate::{
@@ -28,9 +28,9 @@ impl PlayPause {
if let Some(song) = db.get_song(song_id) {
vec![GuiAction::SendToServer(
if song.general.tags.iter().any(|v| v == "Fav") {
Command::TagSongFlagUnset(*song_id, "Fav".to_owned())
Action::TagSongFlagUnset(*song_id, "Fav".to_owned())
} else {
Command::TagSongFlagSet(*song_id, "Fav".to_owned())
Action::TagSongFlagSet(*song_id, "Fav".to_owned())
},
)]
} else {
@@ -48,7 +48,7 @@ impl PlayPause {
),
to_zero: Button::new(
GuiElemCfg::at(Rectangle::from_tuples((0.26, 0.01), (0.49, 0.99))),
|_| vec![GuiAction::SendToServer(Command::Stop)],
|_| vec![GuiAction::SendToServer(Action::Stop)],
[Panel::with_background(
GuiElemCfg::at(Rectangle::from_tuples((0.2, 0.2), (0.8, 0.8))),
(),
@@ -59,9 +59,9 @@ impl PlayPause {
GuiElemCfg::at(Rectangle::from_tuples((0.51, 0.01), (0.74, 0.99))),
|btn| {
vec![GuiAction::SendToServer(if btn.children[0].is_playing {
Command::Pause
Action::Pause
} else {
Command::Resume
Action::Resume
})]
},
[PlayPauseDisplay::new(GuiElemCfg::at(
@@ -70,7 +70,7 @@ impl PlayPause {
),
to_end: Button::new(
GuiElemCfg::at(Rectangle::from_tuples((0.76, 0.01), (0.99, 0.99))),
|_| vec![GuiAction::SendToServer(Command::NextSong)],
|_| vec![GuiAction::SendToServer(Action::NextSong)],
[NextSongShape::new(GuiElemCfg::at(Rectangle::from_tuples(
(0.2, 0.2),
(0.8, 0.8),

View File

@@ -5,7 +5,7 @@ use musicdb_lib::{
song::Song,
AlbumId, ArtistId,
},
server::Command,
server::Action,
};
use speedy2d::{
color::Color,
@@ -404,8 +404,8 @@ impl GuiElem for QueueEmptySpaceDragHandler {
dragged_add_to_queue(
dragged,
(),
|_, q| Command::QueueAdd(vec![], q),
|_, q| Command::QueueMoveInto(q, vec![]),
|_, q| Action::QueueAdd(vec![], q),
|_, q| Action::QueueMoveInto(q, vec![]),
)
}
}
@@ -563,7 +563,7 @@ impl GuiElem for QueueSong {
if self.mouse && button == MouseButton::Left {
self.mouse = false;
if e.take() && !self.always_copy {
vec![GuiAction::SendToServer(Command::QueueGoto(
vec![GuiAction::SendToServer(Action::QueueGoto(
self.path.clone(),
))]
} else {
@@ -631,9 +631,9 @@ impl GuiElem for QueueSong {
self.path.clone(),
move |mut p: Vec<usize>, q| {
if let Some(j) = p.pop() {
Command::QueueInsert(p, if insert_below { j + 1 } else { j }, q)
Action::QueueInsert(p, if insert_below { j + 1 } else { j }, q)
} else {
Command::QueueAdd(p, q)
Action::QueueAdd(p, q)
}
},
move |mut p, q| {
@@ -642,7 +642,7 @@ impl GuiElem for QueueSong {
*l += 1;
}
}
Command::QueueMove(q, p)
Action::QueueMove(q, p)
},
)
} else {
@@ -787,9 +787,9 @@ impl GuiElem for QueueFolder {
// Panel::with_background(GuiElemCfg::default(), (), Color::DARK_GRAY),
// )]))];
return vec![GuiAction::SendToServer(if self.queue.order.is_some() {
Command::QueueUnshuffle(self.path.clone())
Action::QueueUnshuffle(self.path.clone())
} else {
Command::QueueShuffle(self.path.clone())
Action::QueueShuffle(self.path.clone())
})];
}
vec![]
@@ -798,7 +798,7 @@ impl GuiElem for QueueFolder {
if self.mouse && button == MouseButton::Left {
self.mouse = false;
if e.take() && !self.always_copy {
vec![GuiAction::SendToServer(Command::QueueGoto(
vec![GuiAction::SendToServer(Action::QueueGoto(
self.path.clone(),
))]
} else {
@@ -826,8 +826,8 @@ impl GuiElem for QueueFolder {
dragged_add_to_queue(
dragged,
self.path.clone(),
|p, q| Command::QueueAdd(p, q),
|p, q| Command::QueueMoveInto(q, p),
|p, q| Action::QueueAdd(p, q),
|p, q| Action::QueueMoveInto(q, p),
)
} else {
dragged_add_to_queue(
@@ -835,9 +835,9 @@ impl GuiElem for QueueFolder {
self.path.clone(),
|mut p, q| {
let j = p.pop().unwrap_or(0);
Command::QueueInsert(p, j, q)
Action::QueueInsert(p, j, q)
},
|p, q| Command::QueueMove(q, p),
|p, q| Action::QueueMove(q, p),
)
}
} else {
@@ -903,10 +903,10 @@ impl GuiElem for QueueIndentEnd {
dragged_add_to_queue(
dragged,
self.path_insert.clone(),
|(p, j), q| Command::QueueInsert(p, j, q),
|(p, j), q| Action::QueueInsert(p, j, q),
|(mut p, j), q| {
p.push(j);
Command::QueueMove(q, p)
Action::QueueMove(q, p)
},
)
}
@@ -1037,7 +1037,7 @@ impl GuiElem for QueueLoop {
if self.mouse && button == MouseButton::Left {
self.mouse = false;
if e.take() && !self.always_copy {
vec![GuiAction::SendToServer(Command::QueueGoto(
vec![GuiAction::SendToServer(Action::QueueGoto(
self.path.clone(),
))]
} else {
@@ -1066,8 +1066,8 @@ impl GuiElem for QueueLoop {
dragged_add_to_queue(
dragged,
p,
|p, q| Command::QueueAdd(p, q),
|p, q| Command::QueueMoveInto(q, p),
|p, q| Action::QueueAdd(p, q),
|p, q| Action::QueueMoveInto(q, p),
)
} else {
vec![]
@@ -1078,8 +1078,8 @@ impl GuiElem for QueueLoop {
fn dragged_add_to_queue<T: 'static>(
dragged: Dragging,
data: T,
f_queues: impl FnOnce(T, Vec<Queue>) -> Command + 'static,
f_queue_by_path: impl FnOnce(T, Vec<usize>) -> Command + 'static,
f_queues: impl FnOnce(T, Vec<Queue>) -> Action + 'static,
f_queue_by_path: impl FnOnce(T, Vec<usize>) -> Action + 'static,
) -> Vec<GuiAction> {
match dragged {
Dragging::Artist(id) => {

View File

@@ -2,7 +2,7 @@ use std::time::Instant;
use musicdb_lib::{
data::queue::{QueueContent, QueueFolder},
server::Command,
server::Action,
};
use speedy2d::{color::Color, dimen::Vec2, shape::Rectangle, window::VirtualKeyCode, Graphics2D};
use uianimator::{default_animator_f64_quadratic::DefaultAnimatorF64Quadratic, Animator};
@@ -120,15 +120,13 @@ impl GuiScreen {
button_clear_queue: Button::new(
GuiElemCfg::at(Rectangle::from_tuples((0.5, 0.0), (0.75, 0.03))),
|_| {
vec![GuiAction::SendToServer(
musicdb_lib::server::Command::QueueUpdate(
vec![],
musicdb_lib::data::queue::QueueContent::Folder(
musicdb_lib::data::queue::QueueFolder::default(),
)
.into(),
),
)]
vec![GuiAction::SendToServer(Action::QueueUpdate(
vec![],
musicdb_lib::data::queue::QueueContent::Folder(
musicdb_lib::data::queue::QueueFolder::default(),
)
.into(),
))]
},
[Label::new(
GuiElemCfg::default(),
@@ -305,9 +303,9 @@ impl GuiElem for GuiScreen {
if key == ' ' && !(modifiers.ctrl() || modifiers.alt() || modifiers.logo()) && e.take() {
vec![GuiAction::Build(Box::new(|db| {
vec![GuiAction::SendToServer(if db.playing {
Command::Pause
Action::Pause
} else {
Command::Resume
Action::Resume
})]
}))]
} else {

View File

@@ -1,6 +1,6 @@
use std::sync::{atomic::AtomicBool, Arc, Mutex};
use musicdb_lib::server::Command;
use musicdb_lib::server::Action;
use speedy2d::{
color::Color,
dimen::Vec2,
@@ -450,7 +450,7 @@ impl SettingsContent {
),
save_button: Button::new(
GuiElemCfg::default(),
|_| vec![GuiAction::SendToServer(Command::Save)],
|_| vec![GuiAction::SendToServer(Action::Save)],
[Label::new(
GuiElemCfg::default(),
"Server: Save Changes".to_string(),

View File

@@ -1,3 +1,5 @@
// #![allow(unused)]
use std::{
io::{BufReader, Write},
net::{SocketAddr, TcpStream},
@@ -12,7 +14,7 @@ use gui::GuiEvent;
#[cfg(feature = "playback")]
use musicdb_lib::data::cache_manager::CacheManager;
#[cfg(feature = "playback")]
use musicdb_lib::player::{playback_rs::PlayerBackendPlaybackRs, Player};
use musicdb_lib::player::{Player, PlayerBackendFeat};
use musicdb_lib::{
data::{
database::{ClientIo, Database},
@@ -152,7 +154,7 @@ fn main() {
cm.set_cache_songs_count(20);
cache_manager = Some(cm);
Some(Player::new_client(
PlayerBackendPlaybackRs::new_without_command_sending().unwrap(),
PlayerBackendFeat::new_without_command_sending().unwrap(),
))
} else {
None
@@ -186,21 +188,22 @@ fn main() {
)));
}
loop {
let update = Command::from_bytes(&mut con).unwrap();
let command = Command::from_bytes(&mut con).unwrap();
let mut db = database.lock().unwrap();
let action = db.seq.recv(command);
#[cfg(feature = "playback")]
if let Some(player) = &mut player {
player.handle_command(&update);
player.handle_action(&action);
}
#[allow(unused_labels)]
'feature_if: {
#[cfg(any(feature = "mers", feature = "merscfg"))]
if let Some(action) = &mut *mers_after_db_updated_action.lock().unwrap() {
db.apply_command(update.clone());
action(update);
db.apply_command(action.clone());
action(action);
break 'feature_if;
}
db.apply_command(update);
db.apply_action_unchecked_seq(action);
}
#[cfg(feature = "playback")]
if let Some(player) = &mut player {