From b21f0c804b925629ba47d024a18dc1c048f2b2c6 Mon Sep 17 00:00:00 2001 From: Jonas Frei Date: Sun, 10 Jul 2022 15:16:11 +0200 Subject: [PATCH] Added Observe/Unobserve MpvCommand. Closes #2 --- src/ipc.rs | 26 ++++++++++++++++++++++---- src/lib.rs | 24 +++++++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 3d9f0da..efecb7c 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -257,7 +257,7 @@ pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<( } } -pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Result<(), Error> { +pub fn observe_mpv_property(instance: &Mpv, id: &usize, property: &str) -> Result<(), Error> { let ipc_string = format!( "{{ \"command\": [\"observe_property\", {}, \"{}\"] }}\n", id, property @@ -278,7 +278,25 @@ pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Resul } } -fn try_convert_property(name: &str, id: isize, data: MpvDataType) -> Event { +pub fn unobserve_mpv_property(instance: &Mpv, id: &usize) -> Result<(), Error> { + let ipc_string = format!("{{ \"command\": [\"unobserve_property\", {}] }}\n", id); + match serde_json::from_str::(&send_command_sync(instance, &ipc_string)) { + Ok(feedback) => { + if let Value::String(ref error) = feedback["error"] { + if error == "success" { + Ok(()) + } else { + Err(Error(ErrorCode::MpvError(error.to_string()))) + } + } else { + Err(Error(ErrorCode::UnexpectedResult)) + } + } + Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))), + } +} + +fn try_convert_property(name: &str, id: usize, data: MpvDataType) -> Event { let property = match name { "path" => match data { MpvDataType::String(value) => Property::Path(Some(value)), @@ -375,7 +393,7 @@ pub fn listen(instance: &mut Mpv) -> Result { } "property-change" => { let name: String; - let id: isize; + let id: usize; let data: MpvDataType; if let Value::String(ref n) = e["name"] { @@ -385,7 +403,7 @@ pub fn listen(instance: &mut Mpv) -> Result { } if let Value::Number(ref n) = e["id"] { - id = n.as_i64().unwrap() as isize; + id = n.as_i64().unwrap() as usize; } else { id = 0; } diff --git a/src/lib.rs b/src/lib.rs index 9e1bdf2..c2eaa14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub enum Event { MetadataUpdate, Seek, PlaybackRestart, - PropertyChange { id: isize, property: Property }, + PropertyChange { id: usize, property: Property }, ChapterChange, Unimplemented, } @@ -52,6 +52,10 @@ pub enum MpvCommand { from: usize, to: usize, }, + Observe { + id: usize, + property: String + }, PlaylistNext, PlaylistPrev, PlaylistRemove(usize), @@ -62,6 +66,7 @@ pub enum MpvCommand { option: SeekOptions, }, Stop, + Unobserve(usize), } #[derive(Debug)] @@ -405,8 +410,15 @@ impl Mpv { self.run_command(MpvCommand::PlaylistNext) } - pub fn observe_property(&self, id: &isize, property: &str) -> Result<(), Error> { - observe_mpv_property(self, id, property) + pub fn observe_property(&self, id: usize, property: &str) -> Result<(), Error> { + self.run_command(MpvCommand::Observe { + id: id, + property: property.to_string(), + }) + } + + pub fn unobserve_property(&self, id: usize) -> Result<(), Error> { + self.run_command(MpvCommand::Unobserve(id)) } pub fn pause(&self) -> Result<(), Error> { @@ -474,6 +486,9 @@ impl Mpv { }, ], ), + MpvCommand::Observe { id, property } => { + observe_mpv_property(self, &id, &property) + } MpvCommand::PlaylistClear => run_mpv_command(self, "playlist-clear", &[]), MpvCommand::PlaylistMove { from, to } => { run_mpv_command(self, "playlist-move", &[&from.to_string(), &to.to_string()]) @@ -499,6 +514,9 @@ impl Mpv { ], ), MpvCommand::Stop => run_mpv_command(self, "stop", &[]), + MpvCommand::Unobserve(id) => { + unobserve_mpv_property(self, &id) + } } }