Added Observe/Unobserve MpvCommand.

Closes #2
This commit is contained in:
Jonas Frei 2022-07-10 15:16:11 +02:00
parent ea941433b0
commit b21f0c804b
2 changed files with 43 additions and 7 deletions

View File

@ -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::<Value>(&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<Event, Error> {
}
"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<Event, Error> {
}
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;
}

View File

@ -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)
}
}
}