parent
ff7a7b7c9d
commit
9e3ec12894
65
src/ipc.rs
65
src/ipc.rs
|
@ -1,6 +1,6 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
use log::{debug, warn};
|
use log::{debug, warn};
|
||||||
use serde_json::{self, Value};
|
use serde_json::json;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
@ -179,17 +179,16 @@ impl TypeHandler for Vec<PlaylistEntry> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mpv_property<T: TypeHandler>(instance: &Mpv, property: &str) -> Result<T, Error> {
|
pub fn get_mpv_property<T: TypeHandler>(instance: &Mpv, property: &str) -> Result<T, Error> {
|
||||||
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
|
let ipc_string = json!({"command": ["get_property", property]});
|
||||||
|
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
|
||||||
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
|
||||||
Ok(val) => T::get_value(val),
|
Ok(val) => T::get_value(val),
|
||||||
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
|
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String, Error> {
|
pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String, Error> {
|
||||||
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
|
let ipc_string = json!({"command": ["get_property", property]});
|
||||||
let val = serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string))
|
let val = serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string))
|
||||||
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))?;
|
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))?;
|
||||||
|
|
||||||
let map = if let Value::Object(map) = val {
|
let map = if let Value::Object(map) = val {
|
||||||
|
@ -220,29 +219,27 @@ pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_mpv_property<T: TypeHandler>(
|
pub fn set_mpv_property(instance: &Mpv, property: &str, value: Value) -> Result<(), Error> {
|
||||||
instance: &Mpv,
|
let ipc_string = json!({
|
||||||
property: &str,
|
"command": ["set_property", property, value]
|
||||||
value: T,
|
});
|
||||||
) -> Result<(), Error> {
|
|
||||||
let ipc_string = format!(
|
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
|
||||||
"{{ \"command\": [\"set_property\", \"{}\", {}] }}\n",
|
|
||||||
property,
|
|
||||||
value.as_string()
|
|
||||||
);
|
|
||||||
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
|
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<(), Error> {
|
pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<(), Error> {
|
||||||
let mut ipc_string = format!(r#"{{ "command": ["{}""#, command);
|
let mut ipc_string = json!({
|
||||||
for arg in args {
|
"command": [command]
|
||||||
ipc_string.push_str(&format!(r#", "{}""#, arg));
|
});
|
||||||
|
if let Value::Array(args_array) = &mut ipc_string["command"] {
|
||||||
|
for arg in args {
|
||||||
|
args_array.push(json!(arg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ipc_string.push_str("] }\n");
|
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
|
||||||
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
|
||||||
Ok(feedback) => {
|
Ok(feedback) => {
|
||||||
if let Value::String(ref error) = feedback["error"] {
|
if let Value::String(ref error) = feedback["error"] {
|
||||||
if error == "success" {
|
if error == "success" {
|
||||||
|
@ -259,11 +256,10 @@ 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: &isize, property: &str) -> Result<(), Error> {
|
||||||
let ipc_string = format!(
|
let ipc_string = json!({
|
||||||
"{{ \"command\": [\"observe_property\", {}, \"{}\"] }}\n",
|
"command": ["observe_property", id, property]
|
||||||
id, property
|
});
|
||||||
);
|
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
|
||||||
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
|
||||||
Ok(feedback) => {
|
Ok(feedback) => {
|
||||||
if let Value::String(ref error) = feedback["error"] {
|
if let Value::String(ref error) = feedback["error"] {
|
||||||
if error == "success" {
|
if error == "success" {
|
||||||
|
@ -280,8 +276,10 @@ pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Resul
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unobserve_mpv_property(instance: &Mpv, id: &isize) -> Result<(), Error> {
|
pub fn unobserve_mpv_property(instance: &Mpv, id: &isize) -> Result<(), Error> {
|
||||||
let ipc_string = format!("{{ \"command\": [\"unobserve_property\", {}] }}\n", id);
|
let ipc_string = json!({
|
||||||
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
|
"command": ["unobserve_property", id]
|
||||||
|
});
|
||||||
|
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
|
||||||
Ok(feedback) => {
|
Ok(feedback) => {
|
||||||
if let Value::String(ref error) = feedback["error"] {
|
if let Value::String(ref error) = feedback["error"] {
|
||||||
if error == "success" {
|
if error == "success" {
|
||||||
|
@ -438,12 +436,13 @@ pub fn listen_raw(instance: &mut Mpv) -> String {
|
||||||
response.trim_end().to_string()
|
response.trim_end().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_command_sync(instance: &Mpv, command: &str) -> String {
|
fn send_command_sync(instance: &Mpv, command: Value) -> String {
|
||||||
let mut stream = &instance.stream;
|
let stream = &instance.stream;
|
||||||
match stream.write_all(command.as_bytes()) {
|
match serde_json::to_writer(stream, &command) {
|
||||||
Err(why) => panic!("Error: Could not write to socket: {}", why),
|
Err(why) => panic!("Error: Could not write to socket: {}", why),
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
debug!("Command: {}", command.trim_end());
|
let mut stream = stream;
|
||||||
|
stream.write_all(b"\n").unwrap();
|
||||||
let mut response = String::new();
|
let mut response = String::new();
|
||||||
{
|
{
|
||||||
let mut reader = BufReader::new(stream);
|
let mut reader = BufReader::new(stream);
|
||||||
|
|
35
src/lib.rs
35
src/lib.rs
|
@ -1,6 +1,7 @@
|
||||||
pub mod ipc;
|
pub mod ipc;
|
||||||
|
|
||||||
use ipc::*;
|
use ipc::*;
|
||||||
|
use serde_json::{json, Value};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::io::{BufReader, Read};
|
use std::io::{BufReader, Read};
|
||||||
|
@ -270,25 +271,25 @@ pub trait SetPropertyTypeHandler<T> {
|
||||||
|
|
||||||
impl SetPropertyTypeHandler<bool> for bool {
|
impl SetPropertyTypeHandler<bool> for bool {
|
||||||
fn set_property_generic(instance: &Mpv, property: &str, value: bool) -> Result<(), Error> {
|
fn set_property_generic(instance: &Mpv, property: &str, value: bool) -> Result<(), Error> {
|
||||||
set_mpv_property::<bool>(instance, property, value)
|
set_mpv_property(instance, property, json!(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SetPropertyTypeHandler<String> for String {
|
impl SetPropertyTypeHandler<String> for String {
|
||||||
fn set_property_generic(instance: &Mpv, property: &str, value: String) -> Result<(), Error> {
|
fn set_property_generic(instance: &Mpv, property: &str, value: String) -> Result<(), Error> {
|
||||||
set_mpv_property::<String>(instance, property, value)
|
set_mpv_property(instance, property, json!(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SetPropertyTypeHandler<f64> for f64 {
|
impl SetPropertyTypeHandler<f64> for f64 {
|
||||||
fn set_property_generic(instance: &Mpv, property: &str, value: f64) -> Result<(), Error> {
|
fn set_property_generic(instance: &Mpv, property: &str, value: f64) -> Result<(), Error> {
|
||||||
set_mpv_property::<f64>(instance, property, value)
|
set_mpv_property(instance, property, json!(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SetPropertyTypeHandler<usize> for usize {
|
impl SetPropertyTypeHandler<usize> for usize {
|
||||||
fn set_property_generic(instance: &Mpv, property: &str, value: usize) -> Result<(), Error> {
|
fn set_property_generic(instance: &Mpv, property: &str, value: usize) -> Result<(), Error> {
|
||||||
set_mpv_property::<usize>(instance, property, value)
|
set_mpv_property(instance, property, json!(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -430,7 +431,7 @@ impl Mpv {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pause(&self) -> Result<(), Error> {
|
pub fn pause(&self) -> Result<(), Error> {
|
||||||
set_mpv_property(self, "pause", true)
|
set_mpv_property(self, "pause", json!(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prev(&self) -> Result<(), Error> {
|
pub fn prev(&self) -> Result<(), Error> {
|
||||||
|
@ -569,7 +570,7 @@ impl Mpv {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn playlist_play_id(&self, id: usize) -> Result<(), Error> {
|
pub fn playlist_play_id(&self, id: usize) -> Result<(), Error> {
|
||||||
set_mpv_property(self, "playlist-pos", id)
|
set_mpv_property(self, "playlist-pos", json!(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
|
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
|
||||||
|
@ -611,7 +612,7 @@ impl Mpv {
|
||||||
Err(msg) => return Err(msg),
|
Err(msg) => return Err(msg),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
set_mpv_property(self, "loop-file", enabled)
|
set_mpv_property(self, "loop-file", json!(enabled))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_loop_playlist(&self, option: Switch) -> Result<(), Error> {
|
pub fn set_loop_playlist(&self, option: Switch) -> Result<(), Error> {
|
||||||
|
@ -631,7 +632,7 @@ impl Mpv {
|
||||||
Err(msg) => return Err(msg),
|
Err(msg) => return Err(msg),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
set_mpv_property(self, "loop-playlist", enabled)
|
set_mpv_property(self, "loop-playlist", json!(enabled))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_mute(&self, option: Switch) -> Result<(), Error> {
|
pub fn set_mute(&self, option: Switch) -> Result<(), Error> {
|
||||||
|
@ -646,7 +647,7 @@ impl Mpv {
|
||||||
Err(msg) => return Err(msg),
|
Err(msg) => return Err(msg),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
set_mpv_property(self, "mute", enabled)
|
set_mpv_property(self, "mute", json!(enabled))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Description
|
/// # Description
|
||||||
|
@ -685,14 +686,16 @@ impl Mpv {
|
||||||
match get_mpv_property::<f64>(self, "speed") {
|
match get_mpv_property::<f64>(self, "speed") {
|
||||||
Ok(speed) => match option {
|
Ok(speed) => match option {
|
||||||
NumberChangeOptions::Increase => {
|
NumberChangeOptions::Increase => {
|
||||||
set_mpv_property(self, "speed", speed + input_speed)
|
set_mpv_property(self, "speed", json!(speed + input_speed))
|
||||||
}
|
}
|
||||||
|
|
||||||
NumberChangeOptions::Decrease => {
|
NumberChangeOptions::Decrease => {
|
||||||
set_mpv_property(self, "speed", speed - input_speed)
|
set_mpv_property(self, "speed", json!(speed - input_speed))
|
||||||
}
|
}
|
||||||
|
|
||||||
NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed),
|
NumberChangeOptions::Absolute => {
|
||||||
|
set_mpv_property(self, "speed", json!(input_speed))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Err(msg) => Err(msg),
|
Err(msg) => Err(msg),
|
||||||
}
|
}
|
||||||
|
@ -702,14 +705,16 @@ impl Mpv {
|
||||||
match get_mpv_property::<f64>(self, "volume") {
|
match get_mpv_property::<f64>(self, "volume") {
|
||||||
Ok(volume) => match option {
|
Ok(volume) => match option {
|
||||||
NumberChangeOptions::Increase => {
|
NumberChangeOptions::Increase => {
|
||||||
set_mpv_property(self, "volume", volume + input_volume)
|
set_mpv_property(self, "volume", json!(volume + input_volume))
|
||||||
}
|
}
|
||||||
|
|
||||||
NumberChangeOptions::Decrease => {
|
NumberChangeOptions::Decrease => {
|
||||||
set_mpv_property(self, "volume", volume - input_volume)
|
set_mpv_property(self, "volume", json!(volume - input_volume))
|
||||||
}
|
}
|
||||||
|
|
||||||
NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume),
|
NumberChangeOptions::Absolute => {
|
||||||
|
set_mpv_property(self, "volume", json!(input_volume))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Err(msg) => Err(msg),
|
Err(msg) => Err(msg),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue