diff --git a/src/bin/cli.rs b/src/bin/cli.rs index adbae5b..b01849b 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -14,17 +14,17 @@ struct Cli { #[derive(Subcommand, Debug)] enum Commands { - /// Perform an action (ping, pause, resume, stop, play) + /// Perform an action (ping, pause, resume, toggle-pause, stop, play) Action { #[clap(subcommand)] action: Actions, }, - /// Get information from the player (is paused, volume, position, state) + /// Get information from the player (is paused, volume, position, duration, state, current-file-path, input, inputs) Get { #[clap(subcommand)] parameter: GetCommands, }, - /// Set information in the player (volume, position) + /// Set information in the player (volume, position, input) Set { #[clap(subcommand)] parameter: SetCommands, @@ -39,6 +39,8 @@ enum Actions { Pause, /// Resume audio playback Resume, + /// Toggle pause + TogglePause, /// Stop audio playback and clear the queue Stop, /// Play a file @@ -51,11 +53,11 @@ enum GetCommands { IsPaused, /// Playback volume Volume, - /// Playback position + /// Playback position (in seconds) Position, /// Duration of the current file Duration, - /// Player state + /// Player state (Playing, Paused or Stopped) State, /// Current playing file path CurrentFilePath, @@ -69,9 +71,9 @@ enum GetCommands { enum SetCommands { /// Playback volume Volume { volume: f32 }, - /// Playback position + /// Playback position (in seconds) Position { position: f32 }, - /// Input + /// Audio input id (see pwsp-cli get inputs) Input { name: String }, } @@ -86,6 +88,7 @@ async fn main() -> Result<(), Box> { Actions::Ping => Request::ping(), Actions::Pause => Request::pause(), Actions::Resume => Request::resume(), + Actions::TogglePause => Request::toggle_pause(), Actions::Stop => Request::stop(), Actions::Play { file_path } => Request::play(file_path.to_str().unwrap()), }, diff --git a/src/types/commands.rs b/src/types/commands.rs index 3ac6801..d8f3d1a 100644 --- a/src/types/commands.rs +++ b/src/types/commands.rs @@ -1,5 +1,5 @@ use crate::{ - types::socket::Response, + types::{audio_player::PlayerState, socket::Response}, utils::{daemon::get_audio_player, pipewire::get_all_devices}, }; use async_trait::async_trait; @@ -16,6 +16,8 @@ pub struct PauseCommand {} pub struct ResumeCommand {} +pub struct TogglePauseCommand {} + pub struct StopCommand {} pub struct IsPausedCommand {} @@ -75,6 +77,25 @@ impl Executable for ResumeCommand { } } +#[async_trait] +impl Executable for TogglePauseCommand { + async fn execute(&self) -> Response { + let mut audio_player = get_audio_player().await.lock().await; + + if audio_player.get_state() == PlayerState::Stopped { + return Response::new(false, "Audio is not playing"); + } + + if audio_player.is_paused() { + audio_player.resume(); + Response::new(true, "Audio was resumed") + } else { + audio_player.pause(); + Response::new(true, "Audio was paused") + } + } +} + #[async_trait] impl Executable for StopCommand { async fn execute(&self) -> Response { diff --git a/src/types/socket.rs b/src/types/socket.rs index b3bb072..62ae61f 100644 --- a/src/types/socket.rs +++ b/src/types/socket.rs @@ -32,6 +32,10 @@ impl Request { Request::new("resume", vec![]) } + pub fn toggle_pause() -> Self { + Request::new("toggle_pause", vec![]) + } + pub fn stop() -> Self { Request::new("stop", vec![]) } diff --git a/src/utils/commands.rs b/src/utils/commands.rs index 7552fb5..8bcbe88 100644 --- a/src/utils/commands.rs +++ b/src/utils/commands.rs @@ -7,6 +7,7 @@ pub fn parse_command(request: &Request) -> Option> { "ping" => Some(Box::new(PingCommand {})), "pause" => Some(Box::new(PauseCommand {})), "resume" => Some(Box::new(ResumeCommand {})), + "toggle_pause" => Some(Box::new(TogglePauseCommand {})), "stop" => Some(Box::new(StopCommand {})), "is_paused" => Some(Box::new(IsPausedCommand {})), "get_state" => Some(Box::new(GetStateCommand {})),