mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-27 22:11:22 +00:00
1.0.0 rewrite
This commit is contained in:
Generated
+4676
File diff suppressed because it is too large
Load Diff
+52
-3
@@ -3,15 +3,44 @@ name = "pwsp"
|
||||
version = "1.0.0"
|
||||
edition = "2024"
|
||||
authors = ["arabian"]
|
||||
description = "A simple soundpad application written in Rust using egui for the GUI, pipewire for audio input/output, and rodio for audio decoding."
|
||||
description = "PWSP lets you play audio files through your microphone. Has both CLI and GUI clients."
|
||||
readme = "README.md"
|
||||
homepage = "https://github.com/arabianq/pipewire-soundpad"
|
||||
homepage = "https://pwsp.arabianq.ru"
|
||||
repository = "https://github.com/arabianq/pipewire-soundpad"
|
||||
license = "MIT"
|
||||
keywords = ["soundpad", "pipewire"]
|
||||
keywords = ["soundpad", "pipewire", "linux", "cli", "gui"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.47.1", features = ["full"] }
|
||||
futures = { version = "0.3.31", features = ["thread-pool"] }
|
||||
async-trait = "0.1.89"
|
||||
|
||||
serde = { version = "1.0.226", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
|
||||
clap = { version = "4.5.48", default-features = false, features = ["std", "suggestions", "help", "usage", "error-context", "derive"] }
|
||||
dirs = "6.0.0"
|
||||
|
||||
rodio = { version = "0.21.1", default-features = false, features = ["symphonia-all", "playback"] }
|
||||
pipewire = "0.8.0"
|
||||
rfd = "0.15.4"
|
||||
|
||||
egui = { version = "0.32.3", default-features = false, features = ["default_fonts", "rayon"] }
|
||||
eframe = { version = "0.32.3", default-features = false, features = ["default_fonts", "glow", "x11", "wayland"] }
|
||||
egui_material_icons = "0.4.0"
|
||||
|
||||
[[bin]]
|
||||
name = "pwsp-daemon"
|
||||
path = "src/bin/daemon.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "pwsp-cli"
|
||||
path = "src/bin/cli.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "pwsp-gui"
|
||||
path = "src/main.rs"
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
@@ -19,3 +48,23 @@ lto = true
|
||||
codegen-units = 1
|
||||
opt-level = "z"
|
||||
panic = "abort"
|
||||
|
||||
[package.metadata.generate-rpm]
|
||||
assets = [
|
||||
{ source = "target/release/pwsp-daemon", dest = "/usr/bin/pwsp-daemon", mode = "755" },
|
||||
{ source = "target/release/pwsp-cli", dest = "/usr/bin/pwsp-cli", mode = "755" },
|
||||
{ source = "target/release/pwsp-gui", dest = "/usr/bin/pwsp-gui", mode = "755" },
|
||||
{ source = "pwsp-gui.desktop", dest = "/usr/share/applications/pwsp.desktop", mode = "644" },
|
||||
{ source = "icon.png", dest = "/usr/share/icons/hicolor/256x256/apps/pwsp.png", mode = "644" },
|
||||
{ source = "pwsp-daemon.service", dest = "/usr/lib/systemd/user/pwsp-daemon.service", mode = "644" },
|
||||
]
|
||||
|
||||
[package.metadata.deb]
|
||||
assets = [
|
||||
["target/release/pwsp-daemon", "usr/bin/", "755"],
|
||||
["target/release/pwsp-cli", "usr/bin/", "755"],
|
||||
["target/release/pwsp-gui", "usr/bin/", "755"],
|
||||
["assets/pwsp-gui.desktop", "usr/share/applications/pwsp.desktop", "644"],
|
||||
["assets/icon.png", "usr/share/icons/hicolor/256x256/apps/pwsp.png", "644"],
|
||||
["assets/pwsp-daemon.service", "usr/lib/systemd/user/pwsp-daemon.service", "644"],
|
||||
]
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=Pipewire Soundpad Daemon
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/bin/pwsp-daemon
|
||||
Restart=no
|
||||
RuntimeDirectory=pwsp
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
@@ -1,7 +1,8 @@
|
||||
[Desktop Entry]
|
||||
Name=PWSP (Soundpad)
|
||||
Exec=pwsp %u
|
||||
Icon=
|
||||
Comment=Let's you play audio files through you microphone
|
||||
Exec=pwsp-gui %u
|
||||
Icon=pwsp
|
||||
Terminal=false
|
||||
Type=Application
|
||||
Categories=Audio
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 200 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 246 KiB |
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$(realpath "$0")")/.." || exit
|
||||
|
||||
cargo build --release
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$(realpath "$0")")/.." || exit
|
||||
|
||||
bash ./scripts/build.sh
|
||||
|
||||
bash ./scripts/build_rpm.sh
|
||||
bash ./scripts/build_deb.sh
|
||||
|
||||
if command -v upx >/dev/null 2>&1; then
|
||||
upx --best ./target/release/pwsp-gui
|
||||
upx --best ./target/release/pwsp-cli
|
||||
upx --best ./target/release/pwsp-daemon
|
||||
upx -t ./target/release/pwsp-gui
|
||||
upx -t ./target/release/pwsp-cli
|
||||
upx -t ./target/release/pwsp-daemon
|
||||
fi
|
||||
|
||||
rm -rf ./target/for_github_release
|
||||
mkdir ./target/for_github_release
|
||||
|
||||
cp "$(find ./target/debian/pwsp_*_amd64.deb | sort -V | tail -n 1)" ./target/for_github_release/
|
||||
cp "$(find ./target/generate-rpm/pwsp-*.x86_64.rpm | sort -V | tail -n 1)" ./target/for_github_release/
|
||||
zip -9j ./target/for_github_release/pwsp-x86_64-linux.zip ./target/release/pwsp-gui ./target/release/pwsp-cli ./target/release/pwsp-daemon
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$(realpath "$0")")/.." || exit
|
||||
|
||||
rm -rf ./target/debian
|
||||
|
||||
cargo install cargo-deb
|
||||
cargo-deb
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
cd "$(dirname "$(realpath "$0")")/.." || exit
|
||||
|
||||
rm -rf ./target/cargo-generate-rpm
|
||||
|
||||
cargo install cargo-generate-rpm
|
||||
cargo-generate-rpm
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
use clap::{Parser, Subcommand};
|
||||
use pwsp::{
|
||||
types::socket::Request,
|
||||
utils::daemon::{make_request, wait_for_daemon},
|
||||
};
|
||||
use std::{error::Error, path::PathBuf};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Cli {
|
||||
#[clap(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum Commands {
|
||||
/// Perform an action (ping, pause, resume, stop, play)
|
||||
Action {
|
||||
#[clap(subcommand)]
|
||||
action: Actions,
|
||||
},
|
||||
/// Get information from the player (is paused, volume, position, state)
|
||||
Get {
|
||||
#[clap(subcommand)]
|
||||
parameter: GetCommands,
|
||||
},
|
||||
/// Set information in the player (volume, position)
|
||||
Set {
|
||||
#[clap(subcommand)]
|
||||
parameter: SetCommands,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum Actions {
|
||||
/// Ping the daemon
|
||||
Ping,
|
||||
/// Pause audio playback
|
||||
Pause,
|
||||
/// Resume audio playback
|
||||
Resume,
|
||||
/// Stop audio playback and clear the queue
|
||||
Stop,
|
||||
/// Play a file
|
||||
Play { file_path: PathBuf },
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum GetCommands {
|
||||
/// Check if the player is paused
|
||||
IsPaused,
|
||||
/// Playback volume
|
||||
Volume,
|
||||
/// Playback position
|
||||
Position,
|
||||
/// Duration of the current file
|
||||
Duration,
|
||||
/// Player state
|
||||
State,
|
||||
/// Current playing file path
|
||||
CurrentFilePath,
|
||||
/// Current audio input
|
||||
Input,
|
||||
/// All audio inputs
|
||||
Inputs,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
enum SetCommands {
|
||||
/// Playback volume
|
||||
Volume { volume: f32 },
|
||||
/// Playback position
|
||||
Position { position: f32 },
|
||||
/// Input
|
||||
Input { id: u32 },
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
wait_for_daemon().await?;
|
||||
|
||||
let request = match cli.command {
|
||||
Commands::Action { action } => match action {
|
||||
Actions::Ping => Request::ping(),
|
||||
Actions::Pause => Request::pause(),
|
||||
Actions::Resume => Request::resume(),
|
||||
Actions::Stop => Request::stop(),
|
||||
Actions::Play { file_path } => Request::play(file_path.to_str().unwrap()),
|
||||
},
|
||||
Commands::Get { parameter } => match parameter {
|
||||
GetCommands::IsPaused => Request::get_is_paused(),
|
||||
GetCommands::Volume => Request::get_volume(),
|
||||
GetCommands::Position => Request::get_position(),
|
||||
GetCommands::Duration => Request::get_duration(),
|
||||
GetCommands::State => Request::get_state(),
|
||||
GetCommands::CurrentFilePath => Request::get_current_file_path(),
|
||||
GetCommands::Input => Request::get_input(),
|
||||
GetCommands::Inputs => Request::get_inputs(),
|
||||
},
|
||||
Commands::Set { parameter } => match parameter {
|
||||
SetCommands::Volume { volume } => Request::set_volume(volume),
|
||||
SetCommands::Position { position } => Request::seek(position),
|
||||
SetCommands::Input { id } => Request::set_input(id),
|
||||
},
|
||||
};
|
||||
|
||||
let response = make_request(request).await?;
|
||||
println!("{} : {}", response.status, response.message);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
use pwsp::{
|
||||
types::socket::{Request, Response},
|
||||
utils::{
|
||||
commands::parse_command,
|
||||
daemon::{
|
||||
create_runtime_dir, get_audio_player, get_daemon_config, get_runtime_dir,
|
||||
is_daemon_running, link_player_to_virtual_mic,
|
||||
},
|
||||
pipewire::create_virtual_mic,
|
||||
},
|
||||
};
|
||||
use std::{error::Error, fs};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::UnixListener,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
create_runtime_dir()?;
|
||||
|
||||
if is_daemon_running()? {
|
||||
return Err("Another instance is already running.".into());
|
||||
}
|
||||
|
||||
get_daemon_config(); // Initialize daemon config
|
||||
create_virtual_mic()?;
|
||||
get_audio_player().await; // Initialize audio player
|
||||
link_player_to_virtual_mic().await?;
|
||||
|
||||
let runtime_dir = get_runtime_dir();
|
||||
|
||||
let lock_file = fs::File::create(runtime_dir.join("daemon.lock"))?;
|
||||
lock_file.lock()?;
|
||||
|
||||
let socket_path = runtime_dir.join("daemon.sock");
|
||||
if fs::metadata(&socket_path).is_ok() {
|
||||
fs::remove_file(&socket_path)?;
|
||||
}
|
||||
|
||||
let listener = UnixListener::bind(&socket_path)?;
|
||||
println!(
|
||||
"Daemon started. Listening on {}",
|
||||
socket_path.to_str().unwrap_or_default()
|
||||
);
|
||||
|
||||
loop {
|
||||
let (mut stream, _addr) = listener.accept().await?;
|
||||
|
||||
tokio::spawn(async move {
|
||||
// ---------- Read request (start) ----------
|
||||
let mut len_bytes = [0u8; 4];
|
||||
if stream.read_exact(&mut len_bytes).await.is_err() {
|
||||
eprintln!("Failed to read message length from client!");
|
||||
return;
|
||||
}
|
||||
|
||||
let request_len = u32::from_le_bytes(len_bytes) as usize;
|
||||
|
||||
let mut buffer = vec![0u8; request_len];
|
||||
if stream.read_exact(&mut buffer).await.is_err() {
|
||||
eprintln!("Failed to read message from client!");
|
||||
return;
|
||||
}
|
||||
|
||||
let request: Request = serde_json::from_slice(&buffer).unwrap();
|
||||
println!("Received request: {:?}", request);
|
||||
// ---------- Read request (end) ----------
|
||||
|
||||
// ---------- Generate response (start) ----------
|
||||
let command = parse_command(&request);
|
||||
let response: Response;
|
||||
if let Some(command) = command {
|
||||
response = command.execute().await;
|
||||
} else {
|
||||
response = Response::new(false, "Unknown command");
|
||||
}
|
||||
// ---------- Generate response (end) ----------
|
||||
|
||||
// ---------- Send response (start) ----------
|
||||
let response_data = serde_json::to_vec(&response).unwrap();
|
||||
let response_len = response_data.len() as u32;
|
||||
|
||||
if stream.write_all(&response_len.to_le_bytes()).await.is_err() {
|
||||
eprintln!("Failed to write response length to client!");
|
||||
return;
|
||||
}
|
||||
if stream.write_all(&response_data).await.is_err() {
|
||||
eprintln!("Failed to write response to client!");
|
||||
return;
|
||||
}
|
||||
println!("Sent response: {:?}", response);
|
||||
// ---------- Send response (end) ----------
|
||||
});
|
||||
}
|
||||
}
|
||||
+320
@@ -0,0 +1,320 @@
|
||||
use crate::gui::SoundpadGui;
|
||||
use egui::{
|
||||
Button, Color32, ComboBox, FontFamily, Label, RichText, ScrollArea, Slider, TextEdit, Ui, Vec2,
|
||||
};
|
||||
use egui_material_icons::icons;
|
||||
use pwsp::types::audio_player::PlayerState;
|
||||
use pwsp::utils::gui::format_time_pair;
|
||||
use std::{error::Error, path::PathBuf};
|
||||
|
||||
impl SoundpadGui {
|
||||
pub fn draw_waiting_for_daemon(&mut self, ui: &mut Ui) {
|
||||
ui.centered_and_justified(|ui| {
|
||||
ui.label(
|
||||
RichText::new("Waiting for PWSP daemon to start...")
|
||||
.size(34.0)
|
||||
.monospace(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn draw_settings(&mut self, ui: &mut Ui) {
|
||||
ui.vertical(|ui| {
|
||||
ui.spacing_mut().item_spacing.y = 5.0;
|
||||
// --------- Back Button and Title ----------
|
||||
ui.horizontal_top(|ui| {
|
||||
let back_button = Button::new(icons::ICON_ARROW_BACK).frame(false);
|
||||
let back_button_response = ui.add(back_button);
|
||||
if back_button_response.clicked() {
|
||||
self.app_state.show_settings = false;
|
||||
}
|
||||
|
||||
ui.add_space(ui.available_width() / 2.0 - 40.0);
|
||||
|
||||
ui.label(RichText::new("Settings").color(Color32::WHITE).monospace());
|
||||
});
|
||||
// --------------------------------
|
||||
|
||||
ui.separator();
|
||||
ui.add_space(20.0);
|
||||
|
||||
// --------- Checkboxes ----------
|
||||
let save_volume_response =
|
||||
ui.checkbox(&mut self.config.save_volume, "Always remember volume");
|
||||
let save_input_response =
|
||||
ui.checkbox(&mut self.config.save_input, "Always remember microphone");
|
||||
let save_scale_response = ui.checkbox(
|
||||
&mut self.config.save_scale_factor,
|
||||
"Always remember UI scale factor",
|
||||
);
|
||||
|
||||
if save_volume_response.changed()
|
||||
|| save_input_response.changed()
|
||||
|| save_scale_response.changed()
|
||||
{
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
// --------------------------------
|
||||
});
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, ui: &mut Ui) -> Result<(), Box<dyn Error>> {
|
||||
self.draw_header(ui);
|
||||
self.draw_body(ui);
|
||||
ui.separator();
|
||||
self.draw_footer(ui);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_header(&mut self, ui: &mut Ui) {
|
||||
ui.vertical_centered_justified(|ui| {
|
||||
// Current file name
|
||||
ui.label(
|
||||
RichText::new(
|
||||
self.audio_player_state
|
||||
.current_file_path
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
)
|
||||
.color(Color32::WHITE)
|
||||
.family(FontFamily::Monospace),
|
||||
);
|
||||
// Media controls
|
||||
self.draw_controls(ui);
|
||||
ui.separator();
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_controls(&mut self, ui: &mut Ui) {
|
||||
ui.horizontal_top(|ui| {
|
||||
// ---------- Play Button ----------
|
||||
let play_button = Button::new(match self.audio_player_state.state {
|
||||
PlayerState::Playing => icons::ICON_PAUSE,
|
||||
PlayerState::Paused | PlayerState::Stopped => icons::ICON_PLAY_ARROW,
|
||||
})
|
||||
.corner_radius(15.0);
|
||||
|
||||
let play_button_response = ui.add_sized([30.0, 30.0], play_button);
|
||||
if play_button_response.clicked() {
|
||||
self.play_toggle();
|
||||
}
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Position Slider ----------
|
||||
let position_slider = Slider::new(
|
||||
&mut self.app_state.position_slider_value,
|
||||
0.0..=self.audio_player_state.duration,
|
||||
)
|
||||
.show_value(false)
|
||||
.step_by(1.0);
|
||||
|
||||
let default_slider_width = ui.spacing().slider_width;
|
||||
let position_slider_width = ui.available_width()
|
||||
- (30.0 * 3.0)
|
||||
- default_slider_width
|
||||
- (ui.spacing().item_spacing.x * 5.0);
|
||||
ui.spacing_mut().slider_width = position_slider_width;
|
||||
let position_slider_response = ui.add_sized([30.0, 30.0], position_slider);
|
||||
if position_slider_response.drag_stopped() {
|
||||
self.app_state.position_dragged = true;
|
||||
}
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Time Label ----------
|
||||
let time_label = Label::new(
|
||||
RichText::new(format_time_pair(
|
||||
self.audio_player_state.position,
|
||||
self.audio_player_state.duration,
|
||||
))
|
||||
.monospace(),
|
||||
);
|
||||
ui.add_sized([30.0, 30.0], time_label);
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Volume Icon ----------
|
||||
let volume_icon = if self.audio_player_state.volume > 0.7 {
|
||||
icons::ICON_VOLUME_UP
|
||||
} else if self.audio_player_state.volume == 0.0 {
|
||||
icons::ICON_VOLUME_OFF
|
||||
} else if self.audio_player_state.volume < 0.3 {
|
||||
icons::ICON_VOLUME_MUTE
|
||||
} else {
|
||||
icons::ICON_VOLUME_DOWN
|
||||
};
|
||||
let volume_icon = Label::new(RichText::new(volume_icon).size(18.0));
|
||||
ui.add_sized([30.0, 25.0], volume_icon);
|
||||
// --------------------------------
|
||||
|
||||
// ---------- Volume Slider ----------
|
||||
let volume_slider = Slider::new(&mut self.app_state.volume_slider_value, 0.0..=1.0)
|
||||
.show_value(false)
|
||||
.step_by(0.01);
|
||||
|
||||
ui.spacing_mut().slider_width = default_slider_width;
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
|
||||
let volume_slider_response = ui.add_sized([30.0, 30.0], volume_slider);
|
||||
if volume_slider_response.drag_stopped() {
|
||||
self.app_state.volume_dragged = true;
|
||||
}
|
||||
// --------------------------------
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_body(&mut self, ui: &mut Ui) {
|
||||
let dirs_size = Vec2::new(ui.available_width() / 4.0, ui.available_height() - 40.0);
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
self.draw_dirs(ui, dirs_size);
|
||||
ui.separator();
|
||||
|
||||
let files_size = Vec2::new(ui.available_width(), ui.available_height() - 40.0);
|
||||
self.draw_files(ui, files_size);
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_dirs(&mut self, ui: &mut Ui, area_size: Vec2) {
|
||||
ui.vertical(|ui| {
|
||||
ui.set_min_width(area_size.x);
|
||||
ui.set_min_height(area_size.y);
|
||||
|
||||
ScrollArea::vertical().id_salt(0).show(ui, |ui| {
|
||||
let mut dirs: Vec<PathBuf> = self.app_state.dirs.iter().cloned().collect();
|
||||
dirs.sort();
|
||||
for path in dirs.iter() {
|
||||
ui.horizontal(|ui| {
|
||||
let name = path
|
||||
.file_name()
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
||||
|
||||
let dir_button = Button::new(name).frame(false);
|
||||
let dir_button_response = ui.add(dir_button);
|
||||
if dir_button_response.clicked() {
|
||||
self.app_state.current_dir = Some(path.clone());
|
||||
}
|
||||
|
||||
let delete_dir_button = Button::new(icons::ICON_DELETE).frame(false);
|
||||
let delete_dir_button_response =
|
||||
ui.add_sized([18.0, 18.0], delete_dir_button);
|
||||
if delete_dir_button_response.clicked() {
|
||||
self.remove_dir(path.clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let add_dir_button = egui::Button::new(icons::ICON_ADD).frame(false);
|
||||
let add_dir_button_response = ui.add_sized([18.0, 18.0], add_dir_button);
|
||||
if add_dir_button_response.clicked() {
|
||||
self.add_dir();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_files(&mut self, ui: &mut Ui, area_size: Vec2) {
|
||||
let extensions = [
|
||||
"mp3", "wav", "ogg", "flac", "mp4", "m4a", "aac", "mov", "mkv", "webm", "avi",
|
||||
];
|
||||
|
||||
ui.vertical(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_sized(
|
||||
[ui.available_width(), 22.0],
|
||||
TextEdit::singleline(&mut self.app_state.search_query).hint_text("Search..."),
|
||||
);
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
ScrollArea::vertical().id_salt(1).show(ui, |ui| {
|
||||
ui.set_min_width(area_size.x);
|
||||
ui.set_min_height(area_size.y);
|
||||
|
||||
ui.vertical(|ui| {
|
||||
if let Some(path) = self.app_state.current_dir.clone() {
|
||||
for entry in path.read_dir().unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let entry_path = entry.path();
|
||||
|
||||
if entry_path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !extensions.contains(
|
||||
&entry_path.extension().unwrap_or_default().to_str().unwrap(),
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_name = entry_path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
let search_query = self
|
||||
.app_state
|
||||
.search_query
|
||||
.to_lowercase()
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
if !file_name.to_lowercase().contains(search_query.as_str()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let file_button = Button::new(file_name).frame(false);
|
||||
let file_button_response = ui.add(file_button);
|
||||
if file_button_response.clicked() {
|
||||
self.play_file(entry_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_footer(&mut self, ui: &mut Ui) {
|
||||
ui.add_space(5.0);
|
||||
ui.horizontal_top(|ui| {
|
||||
// ---------- Microphone selection ----------
|
||||
let mut mics: Vec<(&u32, &String)> =
|
||||
self.audio_player_state.all_inputs.iter().collect();
|
||||
mics.sort_by_key(|(k, _)| *k);
|
||||
|
||||
let mut selected_input = self.audio_player_state.current_input.to_owned();
|
||||
let prev_input = selected_input.to_owned();
|
||||
ComboBox::from_label("Choose microphone")
|
||||
.selected_text(
|
||||
self.audio_player_state
|
||||
.all_inputs
|
||||
.get(&selected_input)
|
||||
.unwrap_or(&String::new()),
|
||||
)
|
||||
.show_ui(ui, |ui| {
|
||||
for (index, device) in mics {
|
||||
ui.selectable_value(&mut selected_input, index.to_owned(), device);
|
||||
}
|
||||
});
|
||||
|
||||
if selected_input != prev_input {
|
||||
self.set_input(selected_input);
|
||||
}
|
||||
// --------------------------------
|
||||
|
||||
ui.add_space(ui.available_width() - 18.0 - ui.spacing().item_spacing.x);
|
||||
|
||||
// ---------- Settings button ----------
|
||||
let settings_button = Button::new(icons::ICON_SETTINGS).frame(false);
|
||||
let settings_button_response = ui.add_sized([18.0, 18.0], settings_button);
|
||||
if settings_button_response.clicked() {
|
||||
self.app_state.show_settings = true;
|
||||
}
|
||||
// --------------------------------
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
use crate::gui::SoundpadGui;
|
||||
use egui::{Context, Key};
|
||||
|
||||
impl SoundpadGui {
|
||||
pub fn handle_input(&mut self, ctx: &Context) {
|
||||
ctx.input(|i| {
|
||||
if i.key_pressed(Key::Escape) {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
if !self.app_state.show_settings && i.key_pressed(Key::Space) {
|
||||
self.play_toggle();
|
||||
}
|
||||
|
||||
if i.key_pressed(Key::Slash) {
|
||||
self.app_state.show_settings = !self.app_state.show_settings;
|
||||
}
|
||||
|
||||
if self.app_state.show_settings && i.key_pressed(Key::Backspace) {
|
||||
self.app_state.show_settings = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
mod draw;
|
||||
mod input;
|
||||
mod update;
|
||||
|
||||
use eframe::{HardwareAcceleration, NativeOptions, icon_data::from_png_bytes, run_native};
|
||||
use egui::{Context, Vec2, ViewportBuilder};
|
||||
use pwsp::{
|
||||
types::{
|
||||
audio_player::PlayerState,
|
||||
config::GuiConfig,
|
||||
gui::{AppState, AudioPlayerState},
|
||||
socket::Request,
|
||||
},
|
||||
utils::{
|
||||
daemon::get_daemon_config,
|
||||
gui::{get_gui_config, make_request_sync, start_app_state_thread},
|
||||
},
|
||||
};
|
||||
use rfd::FileDialog;
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
struct SoundpadGui {
|
||||
pub app_state: AppState,
|
||||
pub config: GuiConfig,
|
||||
pub audio_player_state: AudioPlayerState,
|
||||
pub audio_player_state_shared: Arc<Mutex<AudioPlayerState>>,
|
||||
}
|
||||
|
||||
impl SoundpadGui {
|
||||
fn new(ctx: &Context) -> Self {
|
||||
let audio_player_state = Arc::new(Mutex::new(AudioPlayerState::default()));
|
||||
start_app_state_thread(audio_player_state.clone());
|
||||
|
||||
let config = get_gui_config();
|
||||
|
||||
ctx.set_zoom_factor(config.scale_factor);
|
||||
|
||||
let mut soundpad_gui = SoundpadGui {
|
||||
app_state: AppState::default(),
|
||||
config: config.clone(),
|
||||
audio_player_state: AudioPlayerState::default(),
|
||||
audio_player_state_shared: audio_player_state.clone(),
|
||||
};
|
||||
|
||||
soundpad_gui.app_state.dirs = config.dirs;
|
||||
|
||||
soundpad_gui
|
||||
}
|
||||
|
||||
pub fn play_toggle(&mut self) {
|
||||
let mut guard = self.audio_player_state_shared.lock().unwrap();
|
||||
guard.state = match guard.state {
|
||||
PlayerState::Playing => {
|
||||
make_request_sync(Request::pause()).ok();
|
||||
guard.new_state = Some(PlayerState::Paused);
|
||||
PlayerState::Paused
|
||||
}
|
||||
PlayerState::Paused => {
|
||||
make_request_sync(Request::resume()).ok();
|
||||
guard.new_state = Some(PlayerState::Playing);
|
||||
PlayerState::Playing
|
||||
}
|
||||
PlayerState::Stopped => PlayerState::Stopped,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn add_dir(&mut self) {
|
||||
let file_dialog = FileDialog::new();
|
||||
if let Some(path) = file_dialog.pick_folder() {
|
||||
self.app_state.dirs.insert(path);
|
||||
self.config.dirs = self.app_state.dirs.clone();
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_dir(&mut self, path: PathBuf) {
|
||||
self.app_state.dirs.remove(&path);
|
||||
if let Some(current_dir) = &self.app_state.current_dir
|
||||
&& current_dir == &path
|
||||
{
|
||||
self.app_state.current_dir = None;
|
||||
}
|
||||
self.config.dirs = self.app_state.dirs.clone();
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
|
||||
pub fn play_file(&mut self, path: PathBuf) {
|
||||
make_request_sync(Request::play(path.to_str().unwrap())).ok();
|
||||
}
|
||||
|
||||
pub fn set_input(&mut self, id: u32) {
|
||||
make_request_sync(Request::set_input(id)).ok();
|
||||
|
||||
if self.config.save_input {
|
||||
let mut daemon_config = get_daemon_config();
|
||||
daemon_config.default_input_id = Some(id);
|
||||
daemon_config.save_to_file().ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run() -> Result<(), Box<dyn Error>> {
|
||||
const ICON: &[u8] = include_bytes!("../../assets/icon.png");
|
||||
|
||||
let options = NativeOptions {
|
||||
vsync: true,
|
||||
centered: true,
|
||||
hardware_acceleration: HardwareAcceleration::Preferred,
|
||||
|
||||
viewport: ViewportBuilder::default()
|
||||
.with_app_id("ru.arabianq.pwsp")
|
||||
.with_inner_size(Vec2::new(1200.0, 800.0))
|
||||
.with_min_inner_size(Vec2::new(800.0, 600.0))
|
||||
.with_icon(from_png_bytes(ICON)?),
|
||||
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
match run_native(
|
||||
"Pipewire Soundpad",
|
||||
options,
|
||||
Box::new(|cc| {
|
||||
egui_material_icons::initialize(&cc.egui_ctx);
|
||||
Ok(Box::new(SoundpadGui::new(&cc.egui_ctx)))
|
||||
}),
|
||||
) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
use crate::gui::SoundpadGui;
|
||||
use eframe::{App, Frame as EFrame};
|
||||
use egui::{CentralPanel, Context};
|
||||
use pwsp::{
|
||||
types::socket::Request,
|
||||
utils::{
|
||||
daemon::{get_daemon_config, is_daemon_running},
|
||||
gui::make_request_sync,
|
||||
},
|
||||
};
|
||||
|
||||
impl App for SoundpadGui {
|
||||
fn update(&mut self, ctx: &Context, _frame: &mut EFrame) {
|
||||
{
|
||||
let guard = self.audio_player_state_shared.lock().unwrap();
|
||||
self.audio_player_state = guard.clone();
|
||||
}
|
||||
|
||||
let old_scale_factor = self.config.scale_factor;
|
||||
let new_scale_factor = ctx.zoom_factor().clamp(0.5, 2.0);
|
||||
|
||||
ctx.set_zoom_factor(new_scale_factor);
|
||||
self.config.scale_factor = new_scale_factor;
|
||||
|
||||
if new_scale_factor != old_scale_factor && self.config.save_scale_factor {
|
||||
self.config.save_to_file().ok();
|
||||
}
|
||||
|
||||
self.handle_input(ctx);
|
||||
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
if !is_daemon_running().unwrap() {
|
||||
self.draw_waiting_for_daemon(ui);
|
||||
return;
|
||||
}
|
||||
|
||||
if self.app_state.show_settings {
|
||||
self.draw_settings(ui);
|
||||
return;
|
||||
}
|
||||
|
||||
self.draw(ui).ok();
|
||||
});
|
||||
|
||||
if self.app_state.position_dragged {
|
||||
make_request_sync(Request::seek(self.app_state.position_slider_value)).ok();
|
||||
let mut guard = self.audio_player_state_shared.lock().unwrap();
|
||||
guard.new_position = Some(self.app_state.position_slider_value);
|
||||
guard.position = self.app_state.position_slider_value;
|
||||
self.app_state.position_dragged = false;
|
||||
} else {
|
||||
self.app_state.position_slider_value = self.audio_player_state.position;
|
||||
}
|
||||
|
||||
if self.app_state.volume_dragged {
|
||||
let new_volume = self.app_state.volume_slider_value;
|
||||
|
||||
make_request_sync(Request::set_volume(new_volume)).ok();
|
||||
|
||||
let mut guard = self.audio_player_state_shared.lock().unwrap();
|
||||
guard.new_volume = Some(self.app_state.volume_slider_value);
|
||||
guard.volume = self.app_state.volume_slider_value;
|
||||
|
||||
self.app_state.volume_dragged = false;
|
||||
|
||||
if self.config.save_volume {
|
||||
let mut daemon_config = get_daemon_config();
|
||||
daemon_config.default_volume = Some(new_volume);
|
||||
daemon_config.save_to_file().ok();
|
||||
}
|
||||
} else {
|
||||
self.app_state.volume_slider_value = self.audio_player_state.volume;
|
||||
}
|
||||
|
||||
ctx.request_repaint_after_secs(1.0 / 60.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod types;
|
||||
pub mod utils;
|
||||
+7
-2
@@ -1,3 +1,8 @@
|
||||
fn main () {
|
||||
mod gui;
|
||||
|
||||
}
|
||||
use std::error::Error;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
gui::run().await
|
||||
}
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
use crate::{
|
||||
types::pipewire::{AudioDevice, DeviceType, Terminate},
|
||||
utils::{
|
||||
daemon::get_daemon_config,
|
||||
pipewire::{create_link, get_all_devices, get_device},
|
||||
},
|
||||
};
|
||||
use rodio::{Decoder, OutputStream, OutputStreamBuilder, Sink, Source};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
error::Error,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Default, Clone, Serialize, Deserialize)]
|
||||
pub enum PlayerState {
|
||||
#[default]
|
||||
Stopped,
|
||||
Paused,
|
||||
Playing,
|
||||
}
|
||||
|
||||
pub struct AudioPlayer {
|
||||
_stream_handle: OutputStream,
|
||||
sink: Sink,
|
||||
|
||||
input_link_sender: Option<pipewire::channel::Sender<Terminate>>,
|
||||
pub current_input_device: Option<AudioDevice>,
|
||||
|
||||
pub volume: f32,
|
||||
pub duration: Option<f32>,
|
||||
|
||||
pub current_file_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl AudioPlayer {
|
||||
pub async fn new() -> Result<Self, Box<dyn Error>> {
|
||||
let daemon_config = get_daemon_config();
|
||||
let default_volume = daemon_config.default_volume.unwrap_or(1.0);
|
||||
let mut default_input_device: Option<AudioDevice> = None;
|
||||
if let Some(id) = daemon_config.default_input_id
|
||||
&& let Ok(device) = get_device(id).await
|
||||
&& device.device_type == DeviceType::Input
|
||||
{
|
||||
default_input_device = Some(device);
|
||||
}
|
||||
|
||||
let stream_handle = OutputStreamBuilder::open_default_stream()?;
|
||||
let sink = Sink::connect_new(stream_handle.mixer());
|
||||
sink.set_volume(default_volume);
|
||||
|
||||
let mut audio_player = AudioPlayer {
|
||||
_stream_handle: stream_handle,
|
||||
sink,
|
||||
|
||||
input_link_sender: None,
|
||||
current_input_device: default_input_device.clone(),
|
||||
|
||||
volume: default_volume,
|
||||
duration: None,
|
||||
|
||||
current_file_path: None,
|
||||
};
|
||||
|
||||
if default_input_device.is_some() {
|
||||
audio_player.link_devices().await?;
|
||||
}
|
||||
|
||||
Ok(audio_player)
|
||||
}
|
||||
|
||||
fn abort_link_thread(&mut self) {
|
||||
if let Some(sender) = &self.input_link_sender {
|
||||
match sender.send(Terminate {}) {
|
||||
Ok(_) => println!("Sent terminate signal to link thread"),
|
||||
Err(_) => println!("Failed to send terminate signal to link thread"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn link_devices(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
self.abort_link_thread();
|
||||
|
||||
if self.current_input_device.is_none() {
|
||||
println!("No input device selected, skipping device linking");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let (input_devices, _) = get_all_devices().await?;
|
||||
|
||||
let mut pwsp_daemon_input: Option<AudioDevice> = None;
|
||||
for input_device in input_devices {
|
||||
if input_device.name == "pwsp-virtual-mic" {
|
||||
pwsp_daemon_input = Some(input_device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if pwsp_daemon_input.is_none() {
|
||||
println!("Could not find pwsp-daemon input device, skipping device linking");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pwsp_daemon_input = pwsp_daemon_input.unwrap();
|
||||
|
||||
let current_input_device = self.current_input_device.clone().unwrap();
|
||||
let output_fl = current_input_device
|
||||
.clone()
|
||||
.output_fl
|
||||
.expect("Failed to get pwsp-daemon output_fl");
|
||||
let output_fr = current_input_device
|
||||
.clone()
|
||||
.output_fr
|
||||
.expect("Failed to get pwsp-daemon output_fl");
|
||||
let input_fl = pwsp_daemon_input
|
||||
.clone()
|
||||
.input_fl
|
||||
.expect("Failed to get pwsp-daemon input_fl");
|
||||
let input_fr = pwsp_daemon_input
|
||||
.clone()
|
||||
.input_fr
|
||||
.expect("Failed to get pwsp-daemon input_fr");
|
||||
self.input_link_sender = Some(create_link(output_fl, output_fr, input_fl, input_fr)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn pause(&mut self) {
|
||||
if self.get_state() == PlayerState::Playing {
|
||||
self.sink.pause();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resume(&mut self) {
|
||||
if self.get_state() == PlayerState::Paused {
|
||||
self.sink.play();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
self.sink.stop();
|
||||
}
|
||||
|
||||
pub fn is_paused(&self) -> bool {
|
||||
self.sink.is_paused()
|
||||
}
|
||||
|
||||
pub fn get_state(&mut self) -> PlayerState {
|
||||
if self.sink.len() == 0 {
|
||||
return PlayerState::Stopped;
|
||||
}
|
||||
|
||||
if self.sink.is_paused() {
|
||||
return PlayerState::Paused;
|
||||
}
|
||||
|
||||
PlayerState::Playing
|
||||
}
|
||||
|
||||
pub fn set_volume(&mut self, volume: f32) {
|
||||
self.volume = volume;
|
||||
self.sink.set_volume(volume);
|
||||
}
|
||||
|
||||
pub fn get_position(&mut self) -> f32 {
|
||||
if self.get_state() == PlayerState::Stopped {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
self.sink.get_pos().as_secs_f32()
|
||||
}
|
||||
|
||||
pub fn seek(&mut self, position: f32) -> Result<(), Box<dyn Error>> {
|
||||
match self.sink.try_seek(Duration::from_secs_f32(position)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_duration(&mut self) -> Result<f32, Box<dyn Error>> {
|
||||
if self.get_state() == PlayerState::Stopped {
|
||||
Err("Nothing is playing right now".into())
|
||||
} else {
|
||||
match self.duration {
|
||||
Some(duration) => Ok(duration),
|
||||
None => Err("Couldn't determine duration for current file".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn play(&mut self, file_path: &Path) -> Result<(), Box<dyn Error>> {
|
||||
if !file_path.exists() {
|
||||
return Err(format!("File does not exist: {}", file_path.display()).into());
|
||||
}
|
||||
|
||||
let file = fs::File::open(file_path)?;
|
||||
match Decoder::try_from(file) {
|
||||
Ok(source) => {
|
||||
self.current_file_path = Some(file_path.to_path_buf());
|
||||
|
||||
if let Some(duration) = source.total_duration() {
|
||||
self.duration = Some(duration.as_secs_f32());
|
||||
} else {
|
||||
self.duration = None;
|
||||
}
|
||||
|
||||
self.sink.stop();
|
||||
self.sink.append(source);
|
||||
self.sink.play();
|
||||
self.link_devices().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => Err(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_file_path(&mut self) -> &Option<PathBuf> {
|
||||
if self.get_state() == PlayerState::Stopped {
|
||||
self.current_file_path = None;
|
||||
}
|
||||
&self.current_file_path
|
||||
}
|
||||
|
||||
pub async fn set_current_input_device(&mut self, id: u32) -> Result<(), Box<dyn Error>> {
|
||||
let input_device = get_device(id).await?;
|
||||
|
||||
if input_device.device_type != DeviceType::Input {
|
||||
return Err("Selected device is not an input device".into());
|
||||
}
|
||||
|
||||
self.current_input_device = Some(input_device);
|
||||
|
||||
self.link_devices().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
use crate::{
|
||||
types::socket::Response,
|
||||
utils::{daemon::get_audio_player, pipewire::get_all_devices},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Executable {
|
||||
async fn execute(&self) -> Response;
|
||||
}
|
||||
|
||||
pub struct PingCommand {}
|
||||
|
||||
pub struct PauseCommand {}
|
||||
|
||||
pub struct ResumeCommand {}
|
||||
|
||||
pub struct StopCommand {}
|
||||
|
||||
pub struct IsPausedCommand {}
|
||||
|
||||
pub struct GetStateCommand {}
|
||||
|
||||
pub struct GetVolumeCommand {}
|
||||
|
||||
pub struct SetVolumeCommand {
|
||||
pub volume: Option<f32>,
|
||||
}
|
||||
|
||||
pub struct GetPositionCommand {}
|
||||
|
||||
pub struct SeekCommand {
|
||||
pub position: Option<f32>,
|
||||
}
|
||||
|
||||
pub struct GetDurationCommand {}
|
||||
|
||||
pub struct PlayCommand {
|
||||
pub file_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
pub struct GetCurrentFilePathCommand {}
|
||||
|
||||
pub struct GetCurrentInputCommand {}
|
||||
|
||||
pub struct GetAllInputsCommand {}
|
||||
|
||||
pub struct SetCurrentInputCommand {
|
||||
pub id: Option<u32>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for PingCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
Response::new(true, "pong")
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for PauseCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
audio_player.pause();
|
||||
Response::new(true, "Audio was paused")
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for ResumeCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
audio_player.resume();
|
||||
Response::new(true, "Audio was resumed")
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for StopCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
audio_player.stop();
|
||||
Response::new(true, "Audio was stopped")
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for IsPausedCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
let is_paused = audio_player.is_paused().to_string();
|
||||
Response::new(true, is_paused)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetStateCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
let state = audio_player.get_state();
|
||||
Response::new(true, serde_json::to_string(&state).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetVolumeCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
let volume = audio_player.volume;
|
||||
Response::new(true, volume.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for SetVolumeCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
if let Some(volume) = self.volume {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
audio_player.set_volume(volume);
|
||||
Response::new(true, format!("Audio volume was set to {}", volume))
|
||||
} else {
|
||||
Response::new(false, "Invalid volume value")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetPositionCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
let position = audio_player.get_position();
|
||||
Response::new(true, position.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for SeekCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
if let Some(position) = self.position {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
match audio_player.seek(position) {
|
||||
Ok(_) => Response::new(true, format!("Audio position was set to {}", position)),
|
||||
Err(err) => Response::new(false, err.to_string()),
|
||||
}
|
||||
} else {
|
||||
Response::new(false, "Invalid position value")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetDurationCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
match audio_player.get_duration() {
|
||||
Ok(duration) => Response::new(true, duration.to_string()),
|
||||
Err(err) => Response::new(false, err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for PlayCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
if let Some(file_path) = &self.file_path {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
match audio_player.play(file_path).await {
|
||||
Ok(_) => Response::new(true, format!("Now playing {}", file_path.display())),
|
||||
Err(err) => Response::new(false, err.to_string()),
|
||||
}
|
||||
} else {
|
||||
Response::new(false, "Invalid file path")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetCurrentFilePathCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
let current_file_path = audio_player.get_current_file_path();
|
||||
if let Some(current_file_path) = current_file_path {
|
||||
Response::new(true, current_file_path.to_str().unwrap())
|
||||
} else {
|
||||
Response::new(false, "No file is playing")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetCurrentInputCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let audio_player = get_audio_player().await.lock().await;
|
||||
if let Some(input_device) = &audio_player.current_input_device {
|
||||
Response::new(true, format!("{} - {}", input_device.id, input_device.nick))
|
||||
} else {
|
||||
Response::new(false, "No input device selected")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for GetAllInputsCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
let (input_devices, _output_devices) = get_all_devices().await.unwrap();
|
||||
let mut input_devices_strings = vec![];
|
||||
for device in input_devices {
|
||||
if device.name == "pwsp-virtual-mic" {
|
||||
continue;
|
||||
}
|
||||
|
||||
let string = format!("{} - {}", device.id, device.nick);
|
||||
input_devices_strings.push(string);
|
||||
}
|
||||
let response_message = input_devices_strings.join("; ");
|
||||
|
||||
Response::new(true, response_message)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Executable for SetCurrentInputCommand {
|
||||
async fn execute(&self) -> Response {
|
||||
if let Some(id) = self.id {
|
||||
let mut audio_player = get_audio_player().await.lock().await;
|
||||
match audio_player.set_current_input_device(id).await {
|
||||
Ok(_) => Response::new(true, "Input device was set"),
|
||||
Err(err) => Response::new(false, err.to_string()),
|
||||
}
|
||||
} else {
|
||||
Response::new(false, "Invalid index value")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
use crate::utils::config::get_config_path;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashSet, error::Error, fs, path::PathBuf};
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct DaemonConfig {
|
||||
pub default_input_id: Option<u32>,
|
||||
pub default_volume: Option<f32>,
|
||||
}
|
||||
|
||||
impl DaemonConfig {
|
||||
pub fn save_to_file(&self) -> Result<(), Box<dyn Error>> {
|
||||
let config_path = get_config_path()?.join("daemon.json");
|
||||
let config_dir = config_path.parent().unwrap();
|
||||
|
||||
if !config_path.exists() {
|
||||
fs::create_dir_all(config_dir)?;
|
||||
}
|
||||
|
||||
let config_json = serde_json::to_string_pretty(self)?;
|
||||
fs::write(config_path, config_json.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_from_file() -> Result<DaemonConfig, Box<dyn Error>> {
|
||||
let config_path = get_config_path()?.join("daemon.json");
|
||||
let bytes = fs::read(config_path)?;
|
||||
Ok(serde_json::from_slice::<DaemonConfig>(&bytes)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct GuiConfig {
|
||||
pub scale_factor: f32,
|
||||
|
||||
pub save_volume: bool,
|
||||
pub save_input: bool,
|
||||
pub save_scale_factor: bool,
|
||||
|
||||
pub dirs: HashSet<PathBuf>,
|
||||
}
|
||||
|
||||
impl Default for GuiConfig {
|
||||
fn default() -> Self {
|
||||
GuiConfig {
|
||||
scale_factor: 1.0,
|
||||
|
||||
save_volume: false,
|
||||
save_input: false,
|
||||
save_scale_factor: false,
|
||||
|
||||
dirs: HashSet::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GuiConfig {
|
||||
pub fn save_to_file(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
let config_path = get_config_path()?.join("gui.json");
|
||||
let config_dir = config_path.parent().unwrap();
|
||||
|
||||
if !config_path.exists() {
|
||||
fs::create_dir_all(config_dir)?;
|
||||
}
|
||||
|
||||
// Do not save scale factor if user does not want to
|
||||
if !self.save_scale_factor {
|
||||
self.scale_factor = 1.0;
|
||||
}
|
||||
|
||||
let config_json = serde_json::to_string_pretty(self)?;
|
||||
fs::write(config_path, config_json.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_from_file() -> Result<GuiConfig, Box<dyn Error>> {
|
||||
let config_path = get_config_path()?.join("gui.json");
|
||||
let bytes = fs::read(config_path)?;
|
||||
Ok(serde_json::from_slice::<GuiConfig>(&bytes)?)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
use crate::types::audio_player::PlayerState;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct AppState {
|
||||
pub search_query: String,
|
||||
|
||||
pub position_slider_value: f32,
|
||||
pub volume_slider_value: f32,
|
||||
|
||||
pub position_dragged: bool,
|
||||
pub volume_dragged: bool,
|
||||
|
||||
pub show_settings: bool,
|
||||
|
||||
pub current_dir: Option<PathBuf>,
|
||||
pub dirs: HashSet<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct AudioPlayerState {
|
||||
pub state: PlayerState,
|
||||
pub new_state: Option<PlayerState>,
|
||||
pub current_file_path: PathBuf,
|
||||
|
||||
pub is_paused: bool,
|
||||
|
||||
pub volume: f32,
|
||||
pub new_volume: Option<f32>,
|
||||
pub position: f32,
|
||||
pub new_position: Option<f32>,
|
||||
pub duration: f32,
|
||||
|
||||
pub current_input: u32,
|
||||
pub all_inputs: HashMap<u32, String>,
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod audio_player;
|
||||
pub mod commands;
|
||||
pub mod config;
|
||||
pub mod gui;
|
||||
pub mod pipewire;
|
||||
pub mod socket;
|
||||
@@ -0,0 +1,31 @@
|
||||
#[derive(Debug)]
|
||||
pub struct Terminate {}
|
||||
|
||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct Port {
|
||||
pub node_id: u32,
|
||||
pub port_id: u32,
|
||||
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub enum DeviceType {
|
||||
Input,
|
||||
Output,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub struct AudioDevice {
|
||||
pub id: u32,
|
||||
|
||||
pub nick: String,
|
||||
pub name: String,
|
||||
|
||||
pub device_type: DeviceType,
|
||||
|
||||
pub input_fl: Option<Port>,
|
||||
pub input_fr: Option<Port>,
|
||||
pub output_fl: Option<Port>,
|
||||
pub output_fr: Option<Port>,
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Request {
|
||||
pub name: String,
|
||||
pub args: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub fn new<T: AsRef<str>>(function_name: T, data: Vec<(T, T)>) -> Self {
|
||||
let hashmap_data: HashMap<String, String> = data
|
||||
.into_iter()
|
||||
.map(|(key, value)| (key.as_ref().to_string(), value.as_ref().to_string()))
|
||||
.collect();
|
||||
|
||||
Request {
|
||||
name: function_name.as_ref().to_string(),
|
||||
args: hashmap_data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ping() -> Self {
|
||||
Request::new("ping", vec![])
|
||||
}
|
||||
|
||||
pub fn pause() -> Self {
|
||||
Request::new("pause", vec![])
|
||||
}
|
||||
|
||||
pub fn resume() -> Self {
|
||||
Request::new("resume", vec![])
|
||||
}
|
||||
|
||||
pub fn stop() -> Self {
|
||||
Request::new("stop", vec![])
|
||||
}
|
||||
|
||||
pub fn play(file_path: &str) -> Self {
|
||||
Request::new("play", vec![("file_path", file_path)])
|
||||
}
|
||||
|
||||
pub fn get_is_paused() -> Self {
|
||||
Request::new("is_paused", vec![])
|
||||
}
|
||||
|
||||
pub fn get_volume() -> Self {
|
||||
Request::new("get_volume", vec![])
|
||||
}
|
||||
|
||||
pub fn get_position() -> Self {
|
||||
Request::new("get_position", vec![])
|
||||
}
|
||||
|
||||
pub fn get_duration() -> Self {
|
||||
Request::new("get_duration", vec![])
|
||||
}
|
||||
|
||||
pub fn get_state() -> Self {
|
||||
Request::new("get_state", vec![])
|
||||
}
|
||||
|
||||
pub fn get_current_file_path() -> Self {
|
||||
Request::new("get_current_file_path", vec![])
|
||||
}
|
||||
|
||||
pub fn get_input() -> Self {
|
||||
Request::new("get_input", vec![])
|
||||
}
|
||||
|
||||
pub fn get_inputs() -> Self {
|
||||
Request::new("get_inputs", vec![])
|
||||
}
|
||||
|
||||
pub fn set_volume(volume: f32) -> Self {
|
||||
Request::new("set_volume", vec![("volume", &volume.to_string())])
|
||||
}
|
||||
|
||||
pub fn seek(position: f32) -> Self {
|
||||
Request::new("seek", vec![("position", &position.to_string())])
|
||||
}
|
||||
|
||||
pub fn set_input(id: u32) -> Self {
|
||||
Request::new("set_input", vec![("input_id", &id.to_string())])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Response {
|
||||
pub status: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new<T: AsRef<str>>(status: bool, message: T) -> Self {
|
||||
Response {
|
||||
status,
|
||||
message: message.as_ref().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
use crate::types::{commands::*, socket::Request};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
|
||||
match request.name.as_str() {
|
||||
"ping" => Some(Box::new(PingCommand {})),
|
||||
"pause" => Some(Box::new(PauseCommand {})),
|
||||
"resume" => Some(Box::new(ResumeCommand {})),
|
||||
"stop" => Some(Box::new(StopCommand {})),
|
||||
"is_paused" => Some(Box::new(IsPausedCommand {})),
|
||||
"get_state" => Some(Box::new(GetStateCommand {})),
|
||||
"get_volume" => Some(Box::new(GetVolumeCommand {})),
|
||||
"set_volume" => {
|
||||
let volume = request
|
||||
.args
|
||||
.get("volume")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<f32>()
|
||||
.ok();
|
||||
Some(Box::new(SetVolumeCommand { volume }))
|
||||
}
|
||||
"get_position" => Some(Box::new(GetPositionCommand {})),
|
||||
"seek" => {
|
||||
let position = request
|
||||
.args
|
||||
.get("position")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<f32>()
|
||||
.ok();
|
||||
Some(Box::new(SeekCommand { position }))
|
||||
}
|
||||
"get_duration" => Some(Box::new(GetDurationCommand {})),
|
||||
"play" => {
|
||||
let file_path = request
|
||||
.args
|
||||
.get("file_path")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<PathBuf>()
|
||||
.ok();
|
||||
Some(Box::new(PlayCommand { file_path }))
|
||||
}
|
||||
"get_current_file_path" => Some(Box::new(GetCurrentFilePathCommand {})),
|
||||
"get_input" => Some(Box::new(GetCurrentInputCommand {})),
|
||||
"get_inputs" => Some(Box::new(GetAllInputsCommand {})),
|
||||
"set_input" => {
|
||||
let id = request
|
||||
.args
|
||||
.get("input_id")
|
||||
.unwrap_or(&String::new())
|
||||
.parse::<u32>()
|
||||
.ok();
|
||||
Some(Box::new(SetCurrentInputCommand { id }))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use std::{error::Error, path::PathBuf};
|
||||
|
||||
pub fn get_config_path() -> Result<PathBuf, Box<dyn Error>> {
|
||||
let config_path = dirs::config_dir().expect("Failed to obtain config dir");
|
||||
Ok(config_path.join("pwsp"))
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
use crate::{
|
||||
types::{
|
||||
audio_player::AudioPlayer,
|
||||
config::DaemonConfig,
|
||||
pipewire::AudioDevice,
|
||||
socket::{Request, Response},
|
||||
},
|
||||
utils::pipewire::{create_link, get_all_devices},
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::{error::Error, fs};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::UnixStream,
|
||||
sync::{Mutex, OnceCell},
|
||||
time::{Duration, sleep},
|
||||
};
|
||||
|
||||
static AUDIO_PLAYER: OnceCell<Mutex<AudioPlayer>> = OnceCell::const_new();
|
||||
|
||||
pub async fn get_audio_player() -> &'static Mutex<AudioPlayer> {
|
||||
AUDIO_PLAYER
|
||||
.get_or_init(|| async {
|
||||
println!("Initializing audio player");
|
||||
Mutex::new(AudioPlayer::new().await.unwrap())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn get_daemon_config() -> DaemonConfig {
|
||||
DaemonConfig::load_from_file().unwrap_or_else(|_| {
|
||||
let config = DaemonConfig::default();
|
||||
config.save_to_file().ok();
|
||||
config
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn link_player_to_virtual_mic() -> Result<(), Box<dyn Error>> {
|
||||
let (input_devices, output_devices) = get_all_devices().await?;
|
||||
|
||||
let mut pwsp_daemon_output: Option<AudioDevice> = None;
|
||||
for output_device in output_devices {
|
||||
if output_device.name == "alsa_playback.pwsp-daemon" {
|
||||
pwsp_daemon_output = Some(output_device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if pwsp_daemon_output.is_none() {
|
||||
println!("Could not find pwsp-daemon output device, skipping device linking");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut pwsp_daemon_input: Option<AudioDevice> = None;
|
||||
for input_device in input_devices {
|
||||
if input_device.name == "pwsp-virtual-mic" {
|
||||
pwsp_daemon_input = Some(input_device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if pwsp_daemon_input.is_none() {
|
||||
println!("Could not find pwsp-daemon input device, skipping device linking");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pwsp_daemon_output = pwsp_daemon_output.unwrap();
|
||||
let pwsp_daemon_input = pwsp_daemon_input.unwrap();
|
||||
|
||||
let output_fl = pwsp_daemon_output
|
||||
.clone()
|
||||
.output_fl
|
||||
.expect("Failed to get pwsp-daemon output_fl");
|
||||
let output_fr = pwsp_daemon_output
|
||||
.clone()
|
||||
.output_fr
|
||||
.expect("Failed to get pwsp-daemon output_fl");
|
||||
let input_fl = pwsp_daemon_input
|
||||
.clone()
|
||||
.input_fl
|
||||
.expect("Failed to get pwsp-daemon input_fl");
|
||||
let input_fr = pwsp_daemon_input
|
||||
.clone()
|
||||
.input_fr
|
||||
.expect("Failed to get pwsp-daemon input_fr");
|
||||
create_link(output_fl, output_fr, input_fl, input_fr)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_runtime_dir() -> PathBuf {
|
||||
dirs::runtime_dir().unwrap_or(PathBuf::from("/run/pwsp"))
|
||||
}
|
||||
|
||||
pub fn create_runtime_dir() -> Result<(), Box<dyn Error>> {
|
||||
let runtime_dir = get_runtime_dir();
|
||||
if !runtime_dir.exists() {
|
||||
fs::create_dir_all(&runtime_dir)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_daemon_running() -> Result<bool, Box<dyn Error>> {
|
||||
let lock_file = fs::File::create(get_runtime_dir().join("daemon.lock"))?;
|
||||
match lock_file.try_lock() {
|
||||
Ok(_) => Ok(false),
|
||||
Err(_) => Ok(true),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait_for_daemon() -> Result<(), Box<dyn Error>> {
|
||||
if is_daemon_running()? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("Daemon not found, waiting for it...");
|
||||
while !is_daemon_running()? {
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
println!("Found running daemon");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn make_request(request: Request) -> Result<Response, Box<dyn Error>> {
|
||||
let socket_path = get_runtime_dir().join("daemon.sock");
|
||||
let mut stream = UnixStream::connect(socket_path).await?;
|
||||
|
||||
// ---------- Send request (start) ----------
|
||||
let request_data = serde_json::to_vec(&request)?;
|
||||
let request_len = request_data.len() as u32;
|
||||
if stream.write_all(&request_len.to_le_bytes()).await.is_err() {
|
||||
return Err("Failed to send request length".into());
|
||||
};
|
||||
if stream.write_all(&request_data).await.is_err() {
|
||||
return Err("Failed to send request".into());
|
||||
}
|
||||
// ---------- Send request (end) ----------
|
||||
|
||||
// ---------- Read response (start) ----------
|
||||
let mut len_bytes = [0u8; 4];
|
||||
if stream.read_exact(&mut len_bytes).await.is_err() {
|
||||
return Err("Failed to read response length".into());
|
||||
}
|
||||
let response_len = u32::from_le_bytes(len_bytes) as usize;
|
||||
|
||||
let mut buffer = vec![0u8; response_len];
|
||||
if stream.read_exact(&mut buffer).await.is_err() {
|
||||
return Err("Failed to read response".into());
|
||||
};
|
||||
// ---------- Read response (end) ----------
|
||||
|
||||
Ok(serde_json::from_slice(&buffer)?)
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
use crate::{
|
||||
types::{
|
||||
audio_player::PlayerState,
|
||||
config::GuiConfig,
|
||||
gui::AudioPlayerState,
|
||||
socket::{Request, Response},
|
||||
},
|
||||
utils::daemon::{make_request, wait_for_daemon},
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
error::Error,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tokio::time::{Duration, sleep};
|
||||
|
||||
pub fn get_gui_config() -> GuiConfig {
|
||||
GuiConfig::load_from_file().unwrap_or_else(|_| {
|
||||
let mut config = GuiConfig::default();
|
||||
config.save_to_file().ok();
|
||||
config
|
||||
})
|
||||
}
|
||||
|
||||
pub fn make_request_sync(request: Request) -> Result<Response, Box<dyn Error>> {
|
||||
futures::executor::block_on(make_request(request))
|
||||
}
|
||||
|
||||
pub fn format_time_pair(position: f32, duration: f32) -> String {
|
||||
fn format_time(seconds: f32) -> String {
|
||||
let total_seconds = seconds.round() as u32;
|
||||
let minutes = total_seconds / 60;
|
||||
let secs = total_seconds % 60;
|
||||
format!("{:02}:{:02}", minutes, secs)
|
||||
}
|
||||
|
||||
format!("{}/{}", format_time(position), format_time(duration))
|
||||
}
|
||||
|
||||
pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerState>>) {
|
||||
tokio::spawn(async move {
|
||||
let sleep_duration = Duration::from_millis(100);
|
||||
|
||||
loop {
|
||||
wait_for_daemon().await.ok();
|
||||
|
||||
let state_req = Request::get_state();
|
||||
let file_path_req = Request::get_current_file_path();
|
||||
let is_paused_req = Request::get_is_paused();
|
||||
let volume_req = Request::get_volume();
|
||||
let position_req = Request::get_position();
|
||||
let duration_req = Request::get_duration();
|
||||
let current_input_req = Request::get_input();
|
||||
let all_inputs_req = Request::get_inputs();
|
||||
|
||||
let state_res = make_request(state_req).await.unwrap_or_default();
|
||||
let file_path_res = make_request(file_path_req).await.unwrap_or_default();
|
||||
let is_paused_res = make_request(is_paused_req).await.unwrap_or_default();
|
||||
let volume_res = make_request(volume_req).await.unwrap_or_default();
|
||||
let position_res = make_request(position_req).await.unwrap_or_default();
|
||||
let duration_res = make_request(duration_req).await.unwrap_or_default();
|
||||
let current_input_res = make_request(current_input_req).await.unwrap_or_default();
|
||||
let all_inputs_res = make_request(all_inputs_req).await.unwrap_or_default();
|
||||
|
||||
let state = match state_res.status {
|
||||
true => serde_json::from_str::<PlayerState>(&state_res.message).unwrap(),
|
||||
false => PlayerState::default(),
|
||||
};
|
||||
|
||||
let file_path = match file_path_res.status {
|
||||
true => PathBuf::from(file_path_res.message),
|
||||
false => PathBuf::new(),
|
||||
};
|
||||
let is_paused = match is_paused_res.status {
|
||||
true => is_paused_res.message == "true",
|
||||
false => false,
|
||||
};
|
||||
let volume = match volume_res.status {
|
||||
true => volume_res.message.parse::<f32>().unwrap(),
|
||||
false => 0.0,
|
||||
};
|
||||
let position = match position_res.status {
|
||||
true => position_res.message.parse::<f32>().unwrap(),
|
||||
false => 0.0,
|
||||
};
|
||||
let duration = match duration_res.status {
|
||||
true => duration_res.message.parse::<f32>().unwrap(),
|
||||
false => 0.0,
|
||||
};
|
||||
let current_input = match current_input_res.status {
|
||||
true => current_input_res
|
||||
.message
|
||||
.as_str()
|
||||
.split(" - ")
|
||||
.collect::<Vec<&str>>()
|
||||
.first()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.parse::<u32>()
|
||||
.unwrap_or_default(),
|
||||
false => 0,
|
||||
};
|
||||
let all_inputs = match all_inputs_res.status {
|
||||
true => all_inputs_res
|
||||
.message
|
||||
.as_str()
|
||||
.split(';')
|
||||
.filter_map(|entry| {
|
||||
let entry = entry.trim();
|
||||
if entry.is_empty() {
|
||||
return None;
|
||||
}
|
||||
entry.split_once(" - ").and_then(|(k, v)| {
|
||||
k.trim()
|
||||
.parse::<u32>()
|
||||
.ok()
|
||||
.map(|key| (key, v.trim().to_string()))
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
false => HashMap::new(),
|
||||
};
|
||||
|
||||
{
|
||||
let mut guard = audio_player_state_shared.lock().unwrap();
|
||||
|
||||
guard.state = match guard.new_state.clone() {
|
||||
Some(new_state) => {
|
||||
guard.new_state = None;
|
||||
new_state
|
||||
}
|
||||
None => state,
|
||||
};
|
||||
guard.current_file_path = file_path;
|
||||
guard.is_paused = is_paused;
|
||||
guard.volume = match guard.new_volume {
|
||||
Some(new_volume) => {
|
||||
guard.new_volume = None;
|
||||
new_volume
|
||||
}
|
||||
None => volume,
|
||||
};
|
||||
guard.position = match guard.new_position {
|
||||
Some(new_position) => {
|
||||
guard.new_position = None;
|
||||
new_position
|
||||
}
|
||||
None => position,
|
||||
};
|
||||
guard.duration = if duration > 0.0 { duration } else { 1.0 };
|
||||
guard.current_input = current_input;
|
||||
guard.all_inputs = all_inputs;
|
||||
}
|
||||
|
||||
sleep(sleep_duration).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod commands;
|
||||
pub mod config;
|
||||
pub mod daemon;
|
||||
pub mod gui;
|
||||
pub mod pipewire;
|
||||
@@ -0,0 +1,299 @@
|
||||
use crate::types::pipewire::{AudioDevice, DeviceType, Port, Terminate};
|
||||
use pipewire::{
|
||||
context::Context, link::Link, main_loop::MainLoop, properties::properties,
|
||||
registry::GlobalObject, spa::utils::dict::DictRef,
|
||||
};
|
||||
use std::{collections::HashMap, error::Error, thread};
|
||||
use tokio::{
|
||||
sync::mpsc,
|
||||
time::{Duration, timeout},
|
||||
};
|
||||
|
||||
fn parse_global_object(
|
||||
global_object: &GlobalObject<&DictRef>,
|
||||
) -> (Option<AudioDevice>, Option<Port>) {
|
||||
// Only objects with props can be devices/ports
|
||||
if let Some(props) = global_object.props {
|
||||
// Only objects with media.class can be devices
|
||||
if let Some(media_class) = props.get("media.class") {
|
||||
let node_id = global_object.id;
|
||||
let node_nick = props.get("node.nick");
|
||||
let node_name = props.get("node.name");
|
||||
let node_description = props.get("node.description");
|
||||
|
||||
// Check if the device is an input or output
|
||||
return if media_class.starts_with("Audio/Source") {
|
||||
let input_device = AudioDevice {
|
||||
id: node_id,
|
||||
nick: node_nick
|
||||
.unwrap_or(node_description.unwrap_or(node_name.unwrap_or_default()))
|
||||
.to_string(),
|
||||
name: node_name.unwrap_or_default().to_string(),
|
||||
device_type: DeviceType::Input,
|
||||
|
||||
input_fl: None,
|
||||
input_fr: None,
|
||||
output_fl: None,
|
||||
output_fr: None,
|
||||
};
|
||||
(Some(input_device), None)
|
||||
} else if media_class.starts_with("Stream/Output/Audio") {
|
||||
let output_device = AudioDevice {
|
||||
id: node_id,
|
||||
nick: node_nick
|
||||
.unwrap_or(node_description.unwrap_or(node_name.unwrap_or_default()))
|
||||
.to_string(),
|
||||
name: node_name.unwrap_or_default().to_string(),
|
||||
device_type: DeviceType::Output,
|
||||
|
||||
input_fl: None,
|
||||
input_fr: None,
|
||||
output_fl: None,
|
||||
output_fr: None,
|
||||
};
|
||||
(Some(output_device), None)
|
||||
} else {
|
||||
(None, None)
|
||||
};
|
||||
// Check if the object is a port
|
||||
} else if props.get("port.direction").is_some() {
|
||||
let node_id = props.get("node.id").unwrap().parse::<u32>().unwrap();
|
||||
let port_id = props.get("port.id").unwrap().parse::<u32>().unwrap();
|
||||
let port_name = props.get("port.name").unwrap();
|
||||
|
||||
let port = Port {
|
||||
node_id,
|
||||
port_id,
|
||||
name: port_name.to_string(),
|
||||
};
|
||||
|
||||
return (None, Some(port));
|
||||
}
|
||||
}
|
||||
(None, None)
|
||||
}
|
||||
|
||||
async fn pw_get_global_objects_thread(
|
||||
main_sender: mpsc::Sender<(Option<AudioDevice>, Option<Port>)>,
|
||||
pw_receiver: pipewire::channel::Receiver<Terminate>,
|
||||
) {
|
||||
let main_loop = MainLoop::new(None).expect("Failed to initialize pipewire main loop");
|
||||
|
||||
// Stop main loop on Terminate message
|
||||
let _receiver = pw_receiver.attach(main_loop.loop_(), {
|
||||
let _main_loop = main_loop.clone();
|
||||
move |_| _main_loop.quit()
|
||||
});
|
||||
|
||||
let context = Context::new(&main_loop).expect("Failed to create pipewire context");
|
||||
let core = context
|
||||
.connect(None)
|
||||
.expect("Failed to connect to pipewire context");
|
||||
let registry = core
|
||||
.get_registry()
|
||||
.expect("Failed to get registry from pipewire context");
|
||||
|
||||
let _listener = registry
|
||||
.add_listener_local()
|
||||
.global(move |global| {
|
||||
// Try to parse every global object pipewire finds
|
||||
let (device, port) = parse_global_object(global);
|
||||
|
||||
// Send message to the main thread
|
||||
let sender_clone = main_sender.clone();
|
||||
tokio::task::spawn(async move {
|
||||
sender_clone.send((device, port)).await.ok();
|
||||
});
|
||||
})
|
||||
.register();
|
||||
|
||||
main_loop.run();
|
||||
}
|
||||
|
||||
pub async fn get_all_devices() -> Result<(Vec<AudioDevice>, Vec<AudioDevice>), Box<dyn Error>> {
|
||||
// Channels to communicate with pipewire thread
|
||||
let (main_sender, mut main_receiver) = mpsc::channel(10);
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel();
|
||||
|
||||
// Spawn pipewire thread in background
|
||||
let _pw_thread =
|
||||
tokio::spawn(async move { pw_get_global_objects_thread(main_sender, pw_receiver).await });
|
||||
|
||||
let mut input_devices: HashMap<u32, AudioDevice> = HashMap::new();
|
||||
let mut output_devices: HashMap<u32, AudioDevice> = HashMap::new();
|
||||
let mut ports: Vec<Port> = vec![];
|
||||
|
||||
loop {
|
||||
// If we don't receive a message in 100ms, we can assume that pipewire thread is finished
|
||||
match timeout(Duration::from_millis(100), main_receiver.recv()).await {
|
||||
Ok(Some((device, port))) => {
|
||||
if let Some(device) = device {
|
||||
match device.device_type {
|
||||
DeviceType::Input => {
|
||||
input_devices.insert(device.id, device);
|
||||
}
|
||||
DeviceType::Output => {
|
||||
output_devices.insert(device.id, device);
|
||||
}
|
||||
}
|
||||
} else if let Some(port) = port {
|
||||
ports.push(port);
|
||||
}
|
||||
}
|
||||
Ok(None) | Err(_) => {
|
||||
// Pipewire thread is finished and we can collect our devices
|
||||
pw_sender
|
||||
.send(Terminate {})
|
||||
.expect("Failed to terminate pipewire thread");
|
||||
|
||||
for port in ports {
|
||||
let node_id = port.node_id;
|
||||
|
||||
if input_devices.contains_key(&node_id) {
|
||||
let input_device = input_devices.get_mut(&node_id).unwrap();
|
||||
match port.name.as_str() {
|
||||
"input_FL" => input_device.input_fl = Some(port),
|
||||
"input_FR" => input_device.input_fr = Some(port),
|
||||
"output_FL" => input_device.output_fl = Some(port),
|
||||
"output_FR" => input_device.output_fr = Some(port),
|
||||
"capture_FL" => input_device.output_fl = Some(port),
|
||||
"capture_FR" => input_device.output_fr = Some(port),
|
||||
"input_MONO" => {
|
||||
input_device.input_fl = Some(port.clone());
|
||||
input_device.input_fr = Some(port)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if output_devices.contains_key(&node_id) {
|
||||
let output_device = output_devices.get_mut(&node_id).unwrap();
|
||||
match port.name.as_str() {
|
||||
"input_FL" => output_device.input_fl = Some(port),
|
||||
"input_FR" => output_device.input_fr = Some(port),
|
||||
"output_FL" => output_device.output_fl = Some(port),
|
||||
"output_FR" => output_device.output_fr = Some(port),
|
||||
"capture_FL" => output_device.output_fl = Some(port),
|
||||
"capture_FR" => output_device.output_fr = Some(port),
|
||||
"output_MONO" => {
|
||||
output_device.output_fl = Some(port.clone());
|
||||
output_device.output_fr = Some(port)
|
||||
}
|
||||
"capture_MONO" => {
|
||||
output_device.output_fl = Some(port.clone());
|
||||
output_device.output_fr = Some(port)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut input_devices: Vec<AudioDevice> = input_devices.values().cloned().collect();
|
||||
let mut output_devices: Vec<AudioDevice> =
|
||||
output_devices.values().cloned().collect();
|
||||
|
||||
input_devices.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
output_devices.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
|
||||
return Ok((input_devices, output_devices));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_device(node_id: u32) -> Result<AudioDevice, Box<dyn Error>> {
|
||||
let (mut input_devices, output_devices) = get_all_devices().await?;
|
||||
input_devices.extend(output_devices);
|
||||
|
||||
for device in input_devices {
|
||||
if device.id == node_id {
|
||||
return Ok(device);
|
||||
}
|
||||
}
|
||||
|
||||
Err("Device not found".into())
|
||||
}
|
||||
|
||||
pub fn create_virtual_mic() -> Result<pipewire::channel::Sender<Terminate>, Box<dyn Error>> {
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel::<Terminate>();
|
||||
|
||||
let _pw_thread = thread::spawn(move || {
|
||||
let main_loop = MainLoop::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = Context::new(&main_loop).expect("Failed to create pipewire context");
|
||||
let core = context
|
||||
.connect(None)
|
||||
.expect("Failed to connect to pipewire context");
|
||||
|
||||
let props = properties!(
|
||||
"factory.name" => "support.null-audio-sink",
|
||||
"node.name" => "pwsp-virtual-mic",
|
||||
"node.description" => "PWSP Virtual Mic",
|
||||
"media.class" => "Audio/Source/Virtual",
|
||||
"audio.position" => "[ FL FR ]",
|
||||
"audio.channels" => "2",
|
||||
"object.linger" => "false", // Destroy the node on app exit
|
||||
);
|
||||
|
||||
let _node = core
|
||||
.create_object::<pipewire::node::Node>("adapter", &props)
|
||||
.expect("Failed to create virtual mic");
|
||||
|
||||
let _receiver = pw_receiver.attach(main_loop.loop_(), {
|
||||
let _main_loop = main_loop.clone();
|
||||
move |_| _main_loop.quit()
|
||||
});
|
||||
|
||||
println!("Virtual mic created");
|
||||
main_loop.run();
|
||||
});
|
||||
|
||||
Ok(pw_sender)
|
||||
}
|
||||
|
||||
pub fn create_link(
|
||||
output_fl: Port,
|
||||
output_fr: Port,
|
||||
input_fl: Port,
|
||||
input_fr: Port,
|
||||
) -> Result<pipewire::channel::Sender<Terminate>, Box<dyn Error>> {
|
||||
let (pw_sender, pw_receiver) = pipewire::channel::channel::<Terminate>();
|
||||
|
||||
let _pw_thread = thread::spawn(move || {
|
||||
let main_loop = MainLoop::new(None).expect("Failed to initialize pipewire main loop");
|
||||
let context = Context::new(&main_loop).expect("Failed to create pipewire context");
|
||||
let core = context
|
||||
.connect(None)
|
||||
.expect("Failed to connect to pipewire context");
|
||||
|
||||
let props_fl = properties! {
|
||||
"link.output.node" => format!("{}", output_fl.node_id).as_str(),
|
||||
"link.output.port" => format!("{}", output_fl.port_id).as_str(),
|
||||
"link.input.node" => format!("{}", input_fl.node_id).as_str(),
|
||||
"link.input.port" => format!("{}", input_fl.port_id).as_str(),
|
||||
};
|
||||
let props_fr = properties! {
|
||||
"link.output.node" => format!("{}", output_fr.node_id).as_str(),
|
||||
"link.output.port" => format!("{}", output_fr.port_id).as_str(),
|
||||
"link.input.node" => format!("{}", input_fr.node_id).as_str(),
|
||||
"link.input.port" => format!("{}", input_fr.port_id).as_str(),
|
||||
};
|
||||
|
||||
let _link_fl = core
|
||||
.create_object::<Link>("link-factory", &props_fl)
|
||||
.expect("Failed to create link FL");
|
||||
let _link_fr = core
|
||||
.create_object::<Link>("link-factory", &props_fr)
|
||||
.expect("Failed to create link FR");
|
||||
|
||||
let _receiver = pw_receiver.attach(main_loop.loop_(), {
|
||||
let _main_loop = main_loop.clone();
|
||||
move |_| _main_loop.quit()
|
||||
});
|
||||
|
||||
println!(
|
||||
"Link created: FL: {}-{} FR: {}-{}",
|
||||
output_fl.node_id, input_fl.node_id, output_fr.node_id, input_fr.node_id
|
||||
);
|
||||
main_loop.run();
|
||||
});
|
||||
|
||||
Ok(pw_sender)
|
||||
}
|
||||
Reference in New Issue
Block a user