Initial commit
This commit is contained in:
+393
@@ -0,0 +1,393 @@
|
||||
use std::error::Error;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::net::Shutdown;
|
||||
use std::iter::Iterator;
|
||||
use std::collections::HashMap;
|
||||
use serde_json::{self, Value};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PlaylistEntry {
|
||||
pub id: usize,
|
||||
pub filename: String,
|
||||
pub title: String,
|
||||
pub current: bool,
|
||||
}
|
||||
|
||||
pub trait TypeHandler: Sized {
|
||||
fn get_value(value: Value) -> Result<Self, String>;
|
||||
fn as_string(&self) -> String;
|
||||
}
|
||||
|
||||
impl TypeHandler for String {
|
||||
fn get_value(value: Value) -> Result<String, String> {
|
||||
if let Value::Object(map) = value {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
if let Value::String(ref s) = map["data"] {
|
||||
Ok(s.to_string())
|
||||
} else {
|
||||
Err("Value did not contain a String".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn as_string(&self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeHandler for bool {
|
||||
fn get_value(value: Value) -> Result<bool, String> {
|
||||
if let Value::Object(map) = value {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
if let Value::Bool(ref b) = map["data"] {
|
||||
Ok(*b)
|
||||
} else {
|
||||
Err("Value did not contain a bool".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
fn as_string(&self) -> String {
|
||||
if *self {
|
||||
"true".to_string()
|
||||
} else {
|
||||
"false".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeHandler for f64 {
|
||||
fn get_value(value: Value) -> Result<f64, String> {
|
||||
if let Value::Object(map) = value {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
if let Value::Number(ref num) = map["data"] {
|
||||
Ok(num.as_f64().unwrap())
|
||||
} else {
|
||||
Err("Value did not contain a f64".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn as_string(&self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeHandler for usize {
|
||||
fn get_value(value: Value) -> Result<usize, String> {
|
||||
if let Value::Object(map) = value {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
if let Value::Number(ref num) = map["data"] {
|
||||
Ok(num.as_u64().unwrap() as usize)
|
||||
} else {
|
||||
Err("Value did not contain an usize".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn as_string(&self) -> String {
|
||||
self.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeHandler for HashMap<String, String> {
|
||||
fn get_value(value: Value) -> Result<HashMap<String, String>, String> {
|
||||
if let Value::Object(map) = value {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
if let Value::Object(ref inner_map) = map["data"] {
|
||||
let mut output_map: HashMap<String, String> = HashMap::new();
|
||||
for (ref key, ref value) in inner_map.iter() {
|
||||
if let Value::String(ref val) = **value {
|
||||
output_map.insert(key.to_string(), val.to_string());
|
||||
}
|
||||
}
|
||||
let output_map = output_map;
|
||||
Ok(output_map)
|
||||
} else {
|
||||
Err("Value did not contain a HashMap".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn as_string(&self) -> String {
|
||||
format!("{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeHandler for Vec<PlaylistEntry> {
|
||||
fn get_value(value: Value) -> Result<Vec<PlaylistEntry>, String> {
|
||||
if let Value::Object(map) = value {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
if let Value::Array(ref playlist_vec) = map["data"] {
|
||||
let mut output: Vec<PlaylistEntry> = Vec::new();
|
||||
for (id, entry) in playlist_vec.iter().enumerate() {
|
||||
let mut filename: String = String::new();
|
||||
let mut title: String = String::new();
|
||||
let mut current: bool = false;
|
||||
if let Value::String(ref f) = entry["filename"] {
|
||||
filename = f.to_string();
|
||||
}
|
||||
if let Value::String(ref t) = entry["title"] {
|
||||
title = t.to_string();
|
||||
}
|
||||
if let Value::Bool(ref b) = entry["current"] {
|
||||
current = *b;
|
||||
}
|
||||
output.push(PlaylistEntry {
|
||||
id: id,
|
||||
filename: filename,
|
||||
title: title,
|
||||
current: current,
|
||||
});
|
||||
}
|
||||
let output = output;
|
||||
Ok(output)
|
||||
} else {
|
||||
Err("Value did not contain a playlist".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn as_string(&self) -> String {
|
||||
format!("{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mpv_property<T: TypeHandler>(socket: &str, property: &str) -> Result<T, String> {
|
||||
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
|
||||
|
||||
match serde_json::from_str::<Value>(&send_command_sync(socket, &ipc_string)) {
|
||||
Ok(val) => T::get_value(val),
|
||||
Err(why) => Err(format!("Error while getting property: {}", why)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mpv_property_string(socket: &str, property: &str) -> Result<String, String> {
|
||||
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
|
||||
match serde_json::from_str::<Value>(&send_command_sync(socket, &ipc_string)) {
|
||||
Ok(val) => {
|
||||
if let Value::Object(map) = val {
|
||||
if let Value::String(ref error) = map["error"] {
|
||||
if error == "success" && map.contains_key("data") {
|
||||
match map["data"] {
|
||||
Value::Bool(b) => Ok(b.to_string()),
|
||||
Value::Number(ref n) => Ok(n.to_string()),
|
||||
Value::String(ref s) => Ok(s.to_string()),
|
||||
Value::Array(ref array) => Ok(format!("{:?}", array)),
|
||||
Value::Object(ref map) => Ok(format!("{:?}", map)),
|
||||
_ => Err("Value contains an unsupported type".to_string()),
|
||||
}
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Unexpected value received".to_string())
|
||||
}
|
||||
}
|
||||
Err(why) => Err(format!("Error while getting property: {}", why)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_mpv_property<T: TypeHandler>(socket: &str,
|
||||
property: &str,
|
||||
value: T)
|
||||
-> Result<(), String> {
|
||||
let ipc_string = format!("{{ \"command\": [\"set_property\", \"{}\", {}] }}\n",
|
||||
property,
|
||||
value.as_string());
|
||||
match serde_json::from_str::<Value>(&send_command_sync(socket, &ipc_string)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(why) => Err(why.description().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_mpv_command(socket: &str, command: &str, args: &Vec<&str>) -> Result<(), String> {
|
||||
let mut ipc_string = format!("{{ \"command\": [\"{}\"", command);
|
||||
if args.len() > 0 {
|
||||
for arg in args.iter() {
|
||||
ipc_string.push_str(&format!(", \"{}\"", arg));
|
||||
}
|
||||
}
|
||||
ipc_string.push_str("] }\n");
|
||||
ipc_string = ipc_string;
|
||||
match serde_json::from_str::<Value>(&send_command_sync(socket, &ipc_string)) {
|
||||
Ok(feedback) => {
|
||||
if let Value::String(ref error) = feedback["error"] {
|
||||
if error == "success" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(error.to_string())
|
||||
}
|
||||
} else {
|
||||
Err("Error: Unexpected result received".to_string())
|
||||
}
|
||||
}
|
||||
Err(why) => Err(why.description().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// #Description
|
||||
///
|
||||
/// Listens on socket <socket> for events and prints them in real-time to stdout.
|
||||
/// This function contains an infinite-loop which keeps the application open indefinitely.
|
||||
///
|
||||
/// #Example
|
||||
/// ```
|
||||
/// listen("/tmp/mpvsocket");
|
||||
/// ```
|
||||
pub fn listen(socket: &str) {
|
||||
match UnixStream::connect(socket) {
|
||||
Ok(stream) => {
|
||||
let mut response = String::new();
|
||||
let mut reader = BufReader::new(&stream);
|
||||
loop {
|
||||
reader.read_line(&mut response).unwrap();
|
||||
match serde_json::from_str::<Value>(&response) {
|
||||
Ok(e) => {
|
||||
if let Value::String(ref name) = e["event"] {
|
||||
println!("{}", name);
|
||||
}
|
||||
}
|
||||
Err(why) => panic!("{}", why.description().to_string()),
|
||||
}
|
||||
response.clear();
|
||||
}
|
||||
}
|
||||
Err(why) => panic!("Error: Could not connect to socket: {}", why.description()),
|
||||
}
|
||||
}
|
||||
|
||||
/// #Description
|
||||
///
|
||||
/// Listens on socket <socket> for events quits as soon as event <event> occurs.
|
||||
///
|
||||
/// #Example
|
||||
/// ```
|
||||
/// wait_for_event("/tmp/mpvsocket", "pause");
|
||||
/// ```
|
||||
pub fn wait_for_event(socket: &str, event: &str) {
|
||||
match UnixStream::connect(socket) {
|
||||
Ok(stream) => {
|
||||
let mut response = String::new();
|
||||
let mut reader = BufReader::new(&stream);
|
||||
loop {
|
||||
reader.read_line(&mut response).unwrap();
|
||||
match serde_json::from_str::<Value>(&response) {
|
||||
Ok(e) => {
|
||||
if let Value::String(ref name) = e["event"] {
|
||||
if name.as_str() == event {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(why) => panic!("{}", why.description().to_string()),
|
||||
}
|
||||
response.clear();
|
||||
}
|
||||
stream.shutdown(Shutdown::Both).expect("socket shutdown");
|
||||
}
|
||||
Err(why) => panic!("Error: Could not connect to socket: {}", why.description()),
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn observe_property(socket: &str, property: &str) -> String {
|
||||
// match UnixStream::connect(socket) {
|
||||
// Ok(mut stream) => {
|
||||
// let command = format!("{{ \"command\": [\"observe_property\", 1, \"{}\"] }}\n",
|
||||
// property);
|
||||
// match stream.write_all(command.as_bytes()) {
|
||||
// Err(why) => error!("Error: Could not write to socket: {}", why.description()),
|
||||
// Ok(_) => {
|
||||
// let mut response = String::new();
|
||||
// let mut reader = BufReader::new(&stream);
|
||||
// loop {
|
||||
// reader.read_line(&mut response).unwrap();
|
||||
// println!("{}", response);
|
||||
// response.clear();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Err(why) => error!("Error: Could not connect to socket: {}", why.description()),
|
||||
// }
|
||||
// }
|
||||
|
||||
fn send_command_sync(socket: &str, command: &str) -> String {
|
||||
match UnixStream::connect(socket) {
|
||||
Ok(mut stream) => {
|
||||
match stream.write_all(command.as_bytes()) {
|
||||
Err(why) => panic!("Error: Could not write to socket: {}", why.description()),
|
||||
Ok(_) => {
|
||||
let mut response = String::new();
|
||||
{
|
||||
let mut reader = BufReader::new(&stream);
|
||||
reader.read_line(&mut response).unwrap();
|
||||
}
|
||||
stream
|
||||
.shutdown(Shutdown::Both)
|
||||
.expect("shutdown function failed");
|
||||
response
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(why) => panic!("Error: Could not connect to socket: {}", why.description()),
|
||||
}
|
||||
}
|
||||
+529
@@ -0,0 +1,529 @@
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
pub mod ipc;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use ipc::*;
|
||||
|
||||
pub type Socket = String;
|
||||
|
||||
pub enum NumberChangeOptions {
|
||||
Absolute,
|
||||
Increase,
|
||||
Decrease,
|
||||
}
|
||||
|
||||
pub enum SeekOptions {
|
||||
Relative,
|
||||
Absolute,
|
||||
RelativePercent,
|
||||
AbsolutePercent,
|
||||
}
|
||||
|
||||
pub enum PlaylistAddOptions {
|
||||
Replace,
|
||||
Append,
|
||||
AppendPlay,
|
||||
}
|
||||
|
||||
pub enum Switch {
|
||||
On,
|
||||
Off,
|
||||
Toggle,
|
||||
}
|
||||
|
||||
pub struct Playlist {
|
||||
pub socket: Socket,
|
||||
pub entries: Vec<PlaylistEntry>,
|
||||
}
|
||||
|
||||
pub trait GetPropertyTypeHandler: Sized {
|
||||
fn get_property_generic(socket: &str, property: &str) -> Result<Self, String>;
|
||||
}
|
||||
|
||||
impl GetPropertyTypeHandler for bool {
|
||||
fn get_property_generic(socket: &str, property: &str) -> Result<bool, String> {
|
||||
get_mpv_property::<bool>(socket, property)
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPropertyTypeHandler for String {
|
||||
fn get_property_generic(socket: &str, property: &str) -> Result<String, String> {
|
||||
get_mpv_property::<String>(socket, property)
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPropertyTypeHandler for f64 {
|
||||
fn get_property_generic(socket: &str, property: &str) -> Result<f64, String> {
|
||||
get_mpv_property::<f64>(socket, property)
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPropertyTypeHandler for usize {
|
||||
fn get_property_generic(socket: &str, property: &str) -> Result<usize, String> {
|
||||
get_mpv_property::<usize>(socket, property)
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPropertyTypeHandler for Vec<PlaylistEntry> {
|
||||
fn get_property_generic(socket: &str, property: &str) -> Result<Vec<PlaylistEntry>, String> {
|
||||
get_mpv_property::<Vec<PlaylistEntry>>(socket, property)
|
||||
}
|
||||
}
|
||||
|
||||
impl GetPropertyTypeHandler for HashMap<String, String> {
|
||||
fn get_property_generic(socket: &str,
|
||||
property: &str)
|
||||
-> Result<HashMap<String, String>, String> {
|
||||
get_mpv_property::<HashMap<String, String>>(socket, property)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SetPropertyTypeHandler<T> {
|
||||
fn set_property_generic(socket: &str, property: &str, value: T) -> Result<(), String>;
|
||||
}
|
||||
|
||||
impl SetPropertyTypeHandler<bool> for bool {
|
||||
fn set_property_generic(socket: &str, property: &str, value: bool) -> Result<(), String> {
|
||||
set_mpv_property::<bool>(socket, property, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl SetPropertyTypeHandler<String> for String {
|
||||
fn set_property_generic(socket: &str, property: &str, value: String) -> Result<(), String> {
|
||||
set_mpv_property::<String>(socket, property, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl SetPropertyTypeHandler<f64> for f64 {
|
||||
fn set_property_generic(socket: &str, property: &str, value: f64) -> Result<(), String> {
|
||||
set_mpv_property::<f64>(socket, property, value)
|
||||
}
|
||||
}
|
||||
|
||||
impl SetPropertyTypeHandler<usize> for usize {
|
||||
fn set_property_generic(socket: &str, property: &str, value: usize) -> Result<(), String> {
|
||||
set_mpv_property::<usize>(socket, property, value)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PlaylistHandler {
|
||||
fn get_from(socket: Socket) -> Result<Playlist, String>;
|
||||
fn shuffle(&mut self) -> &mut Playlist;
|
||||
fn remove_id(&mut self, id: usize) -> &mut Playlist;
|
||||
fn move_entry(&mut self, from: usize, to: usize) -> &mut Playlist;
|
||||
fn current_id(&self) -> Option<usize>;
|
||||
}
|
||||
|
||||
impl PlaylistHandler for Playlist {
|
||||
fn get_from(socket: Socket) -> Result<Playlist, String> {
|
||||
match get_mpv_property(&socket, "playlist") {
|
||||
Ok(playlist) => {
|
||||
Ok(Playlist {
|
||||
socket: socket,
|
||||
entries: playlist,
|
||||
})
|
||||
}
|
||||
Err(why) => Err(why),
|
||||
}
|
||||
}
|
||||
|
||||
fn shuffle(&mut self) -> &mut Playlist {
|
||||
if let Err(error_msg) = run_mpv_command(&self.socket, "playlist-shuffle", &vec![]) {
|
||||
panic!("Error: {}", error_msg);
|
||||
}
|
||||
if let Ok(mut playlist_entries) =
|
||||
get_mpv_property::<Vec<PlaylistEntry>>(&self.socket, "playlist") {
|
||||
if self.entries.len() == playlist_entries.len() {
|
||||
for (i, entry) in playlist_entries.drain(0..).enumerate() {
|
||||
self.entries[i] = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn remove_id(&mut self, id: usize) -> &mut Playlist {
|
||||
self.entries.remove(id);
|
||||
if let Err(error_msg) = run_mpv_command(&self.socket,
|
||||
"playlist-remove",
|
||||
&vec![&id.to_string()]) {
|
||||
panic!("Error: {}", error_msg);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn move_entry(&mut self, from: usize, to: usize) -> &mut Playlist {
|
||||
if from != to {
|
||||
if let Err(error_msg) = run_mpv_command(&self.socket,
|
||||
"playlist-move",
|
||||
&vec![&from.to_string(), &to.to_string()]) {
|
||||
panic!("Error: {}", error_msg);
|
||||
}
|
||||
if from < to {
|
||||
self.entries[from].id = to - 1;
|
||||
self.entries[to].id = to - 2;
|
||||
for i in from..to - 2 {
|
||||
self.entries[i + 1].id = i;
|
||||
}
|
||||
self.entries.sort_by_key(|entry| entry.id);
|
||||
} else if from > to {
|
||||
self.entries[from].id = to;
|
||||
for i in to..from - 1 {
|
||||
self.entries[i].id = i + 1;
|
||||
}
|
||||
self.entries.sort_by_key(|entry| entry.id);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn current_id(&self) -> Option<usize> {
|
||||
for entry in self.entries.iter() {
|
||||
if entry.current {
|
||||
return Some(entry.id);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Commands {
|
||||
fn get_metadata(&self) -> Result<HashMap<String, String>, String>;
|
||||
fn get_playlist(&self) -> Result<Playlist, String>;
|
||||
fn get_property<T: GetPropertyTypeHandler>(&self, property: &str) -> Result<T, String>;
|
||||
fn get_property_string(&self, property: &str) -> Result<String, String>;
|
||||
fn kill(&self) -> Result<(), String>;
|
||||
fn next(&self) -> Result<(), String>;
|
||||
fn pause(&self) -> Result<(), String>;
|
||||
fn playlist_add(&self, file: &str, option: PlaylistAddOptions) -> Result<(), String>;
|
||||
fn playlist_clear(&self) -> Result<(), String>;
|
||||
fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), String>;
|
||||
fn playlist_play_id(&self, id: usize) -> Result<(), String>;
|
||||
fn playlist_play_next(&self, id: usize) -> Result<(), String>;
|
||||
fn playlist_shuffle(&self) -> Result<(), String>;
|
||||
fn playlist_remove_id(&self, id: usize) -> Result<(), String>;
|
||||
fn prev(&self) -> Result<(), String>;
|
||||
fn restart(&self) -> Result<(), String>;
|
||||
fn run_command(&self, command: &str, args: &Vec<&str>) -> Result<(), String>;
|
||||
fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), String>;
|
||||
fn set_loop_file(&self, option: Switch) -> Result<(), String>;
|
||||
fn set_loop_playlist(&self, option: Switch) -> Result<(), String>;
|
||||
fn set_mute(&self, option: Switch) -> Result<(), String>;
|
||||
fn set_property<T: SetPropertyTypeHandler<T>>(&self,
|
||||
property: &str,
|
||||
value: T)
|
||||
-> Result<(), String>;
|
||||
fn set_speed(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), String>;
|
||||
fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), String>;
|
||||
fn stop(&self) -> Result<(), String>;
|
||||
fn toggle(&self) -> Result<(), String>;
|
||||
}
|
||||
|
||||
impl Commands for Socket {
|
||||
fn get_metadata(&self) -> Result<HashMap<String, String>, String> {
|
||||
match get_mpv_property(self, "metadata") {
|
||||
Ok(map) => Ok(map),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_playlist(&self) -> Result<Playlist, String> {
|
||||
Playlist::get_from(self.to_string())
|
||||
}
|
||||
|
||||
/// #Description
|
||||
///
|
||||
/// Retrieves the property value from mpv.
|
||||
///
|
||||
/// ##Supported types
|
||||
/// - String
|
||||
/// - bool
|
||||
/// - HashMap<String, String> (e.g. for the 'metadata' property)
|
||||
/// - Vec<PlaylistEntry> (for the 'playlist' property)
|
||||
///
|
||||
/// ##Input arguments
|
||||
///
|
||||
/// - **socket** defines the socket that ipc connects to
|
||||
/// - **property** defines the mpv property that should be retrieved
|
||||
///
|
||||
/// #Example
|
||||
/// ```
|
||||
/// let mpv: Socket = String::from(matches.value_of("socket").unwrap());
|
||||
/// let paused: bool = mpv.get_property("pause").unwrap();
|
||||
/// let title: String = mpv.get_property("media-title").unwrap();
|
||||
/// ```
|
||||
fn get_property<T: GetPropertyTypeHandler>(&self, property: &str) -> Result<T, String> {
|
||||
T::get_property_generic(self, property)
|
||||
}
|
||||
|
||||
/// #Description
|
||||
///
|
||||
/// Retrieves the property value from mpv. Implemented for the following types:
|
||||
/// The result is always of type String, regardless of the type of the value of the mpv property
|
||||
///
|
||||
/// ##Input arguments
|
||||
///
|
||||
/// - **socket** defines the socket that ipc connects to
|
||||
/// - **property** defines the mpv property that should be retrieved
|
||||
///
|
||||
/// #Example
|
||||
///
|
||||
/// ```
|
||||
/// let mpv: Socket = String::from(matches.value_of("socket").unwrap());
|
||||
/// let title = mpv.get_property_string("media-title").unwrap();
|
||||
/// ```
|
||||
fn get_property_string(&self, property: &str) -> Result<String, String> {
|
||||
get_mpv_property_string(self, property)
|
||||
}
|
||||
|
||||
fn kill(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "quit", &vec![])
|
||||
}
|
||||
|
||||
fn next(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "playlist-next", &vec![])
|
||||
}
|
||||
|
||||
fn pause(&self) -> Result<(), String> {
|
||||
set_mpv_property(self, "pause", true)
|
||||
}
|
||||
|
||||
fn prev(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "playlist-prev", &vec![])
|
||||
}
|
||||
|
||||
fn restart(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "seek", &vec!["0", "absolute"])
|
||||
}
|
||||
|
||||
/// #Description
|
||||
///
|
||||
/// Runs mpv commands. The arguments are passed as a String-Vector reference:
|
||||
///
|
||||
/// #Example
|
||||
/// ```
|
||||
/// let mpv: Socket = String::from(matches.value_of("socket").unwrap());
|
||||
///
|
||||
/// //Run command 'playlist-shuffle' which takes no arguments
|
||||
/// mpv.run_command("playlist-shuffle", &vec![]);
|
||||
///
|
||||
/// //Run command 'seek' which in this case takes two arguments
|
||||
/// mpv.run_command("seek", &vec!["0", "absolute"]);
|
||||
/// ```
|
||||
fn run_command(&self, command: &str, args: &Vec<&str>) -> Result<(), String> {
|
||||
run_mpv_command(self, command, args)
|
||||
}
|
||||
|
||||
fn playlist_add(&self, file: &str, option: PlaylistAddOptions) -> Result<(), String> {
|
||||
match option {
|
||||
PlaylistAddOptions::Replace => {
|
||||
run_mpv_command(self, "loadfile", &vec![file, "replace"])
|
||||
}
|
||||
PlaylistAddOptions::Append => run_mpv_command(self, "loadfile", &vec![file, "append"]),
|
||||
PlaylistAddOptions::AppendPlay => {
|
||||
run_mpv_command(self, "loadfile", &vec![file, "append-play"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn playlist_clear(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "playlist-clear", &vec![])
|
||||
}
|
||||
|
||||
fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), String> {
|
||||
run_mpv_command(self,
|
||||
"playlist-remove",
|
||||
&vec![&from.to_string(), &to.to_string()])
|
||||
}
|
||||
|
||||
fn playlist_play_id(&self, id: usize) -> Result<(), String> {
|
||||
set_mpv_property(self, "playlist-pos", id)
|
||||
}
|
||||
|
||||
fn playlist_play_next(&self, id: usize) -> Result<(), String> {
|
||||
match Playlist::get_from(self.to_string()) {
|
||||
Ok(playlist) => {
|
||||
if let Some(current_id) = playlist.current_id() {
|
||||
run_mpv_command(self,
|
||||
"playlist-move",
|
||||
&vec![&id.to_string(), &(current_id + 1).to_string()])
|
||||
} else {
|
||||
Err("There is no file playing at the moment.".to_string())
|
||||
}
|
||||
}
|
||||
Err(why) => Err(why),
|
||||
}
|
||||
}
|
||||
|
||||
fn playlist_remove_id(&self, id: usize) -> Result<(), String> {
|
||||
run_mpv_command(self, "playlist-remove", &vec![&id.to_string()])
|
||||
}
|
||||
|
||||
fn playlist_shuffle(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "playlist-shuffle", &vec![])
|
||||
}
|
||||
|
||||
fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), String> {
|
||||
match option {
|
||||
SeekOptions::Absolute => {
|
||||
run_mpv_command(self, "seek", &vec![&seconds.to_string(), "absolute"])
|
||||
}
|
||||
SeekOptions::AbsolutePercent => {
|
||||
run_mpv_command(self,
|
||||
"seek",
|
||||
&vec![&seconds.to_string(), "absolute-percent"])
|
||||
}
|
||||
SeekOptions::Relative => {
|
||||
run_mpv_command(self, "seek", &vec![&seconds.to_string(), "relative"])
|
||||
}
|
||||
SeekOptions::RelativePercent => {
|
||||
run_mpv_command(self,
|
||||
"seek",
|
||||
&vec![&seconds.to_string(), "relative-percent"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_loop_file(&self, option: Switch) -> Result<(), String> {
|
||||
let mut enabled = false;
|
||||
match option {
|
||||
Switch::On => enabled = true,
|
||||
Switch::Off => {}
|
||||
Switch::Toggle => {
|
||||
match get_mpv_property_string(self, "loop-file") {
|
||||
Ok(value) => {
|
||||
match value.as_ref() {
|
||||
"false" => {
|
||||
enabled = true;
|
||||
}
|
||||
_ => {
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(msg) => return Err(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
set_mpv_property(self, "loop-file", enabled)
|
||||
}
|
||||
|
||||
fn set_loop_playlist(&self, option: Switch) -> Result<(), String> {
|
||||
let mut enabled = false;
|
||||
match option {
|
||||
Switch::On => enabled = true,
|
||||
Switch::Off => {}
|
||||
Switch::Toggle => {
|
||||
match get_mpv_property_string(self, "loop-playlist") {
|
||||
Ok(value) => {
|
||||
match value.as_ref() {
|
||||
"false" => {
|
||||
enabled = true;
|
||||
}
|
||||
_ => {
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(msg) => return Err(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
set_mpv_property(self, "loop-playlist", enabled)
|
||||
}
|
||||
|
||||
fn set_mute(&self, option: Switch) -> Result<(), String> {
|
||||
let mut enabled = false;
|
||||
match option {
|
||||
Switch::On => enabled = true,
|
||||
Switch::Off => {}
|
||||
Switch::Toggle => {
|
||||
match get_mpv_property::<bool>(self, "mute") {
|
||||
Ok(value) => {
|
||||
enabled = !value;
|
||||
}
|
||||
Err(msg) => return Err(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
set_mpv_property(self, "mute", enabled)
|
||||
}
|
||||
|
||||
/// #Description
|
||||
///
|
||||
/// Sets the mpv property _<property>_ to _<value>_.
|
||||
///
|
||||
/// ##Supported types
|
||||
/// - String
|
||||
/// - bool
|
||||
/// - f64
|
||||
/// - usize
|
||||
///
|
||||
/// ##Input arguments
|
||||
///
|
||||
/// - **property** defines the mpv property that should be retrieved
|
||||
/// - **value** defines the value of the given mpv property _<property>_
|
||||
///
|
||||
/// #Example
|
||||
/// ```
|
||||
/// let mpv: Socket = String::from(matches.value_of("socket").unwrap());
|
||||
/// mpv.set_property("pause", true);
|
||||
/// ```
|
||||
fn set_property<T: SetPropertyTypeHandler<T>>(&self,
|
||||
property: &str,
|
||||
value: T)
|
||||
-> Result<(), String> {
|
||||
T::set_property_generic(self, property, value)
|
||||
}
|
||||
|
||||
fn set_speed(&self, input_speed: f64, option: NumberChangeOptions) -> Result<(), String> {
|
||||
match get_mpv_property::<f64>(self, "speed") {
|
||||
Ok(speed) => {
|
||||
match option {
|
||||
NumberChangeOptions::Increase => {
|
||||
set_mpv_property(self, "speed", speed + input_speed)
|
||||
}
|
||||
|
||||
NumberChangeOptions::Decrease => {
|
||||
set_mpv_property(self, "speed", speed - input_speed)
|
||||
}
|
||||
|
||||
NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed),
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_volume(&self, input_volume: f64, option: NumberChangeOptions) -> Result<(), String> {
|
||||
match get_mpv_property::<f64>(self, "volume") {
|
||||
Ok(volume) => {
|
||||
match option {
|
||||
NumberChangeOptions::Increase => {
|
||||
set_mpv_property(self, "volume", volume + input_volume)
|
||||
}
|
||||
|
||||
NumberChangeOptions::Decrease => {
|
||||
set_mpv_property(self, "volume", volume - input_volume)
|
||||
}
|
||||
|
||||
NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume),
|
||||
}
|
||||
}
|
||||
Err(msg) => Err(msg),
|
||||
}
|
||||
}
|
||||
|
||||
fn stop(&self) -> Result<(), String> {
|
||||
run_mpv_command(self, "stop", &vec![])
|
||||
}
|
||||
|
||||
fn toggle(&self) -> Result<(), String> {
|
||||
match get_mpv_property::<bool>(self, "pause") {
|
||||
Ok(paused) => set_mpv_property(self, "pause", !paused),
|
||||
Err(msg) => Err(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user