Create IPC socket on UNIX platforms (#1018)

* Create IPC socket on UNIX platforms

Creates an IPC socket which remote programs/scripts can connect to. This
can be used to control ncspot or fetch the current playback status.

At the moment, only remote control is implemented. Next step is to send
the current player status as a JSON object.

Fixes #524

* Publish status changes to connected sockets

Whenever the playback mode (playing, paused, stopped) or the track changes, all
socket listeners will be notified.

Fixes #924, fixes #1019

* Document IPC feature
This commit is contained in:
Henrik Friedrichsen
2022-12-28 19:01:59 +01:00
committed by GitHub
parent 93816af654
commit ec4b7c209a
7 changed files with 168 additions and 4 deletions

View File

@@ -43,12 +43,15 @@ mod traits;
mod ui;
mod utils;
#[cfg(unix)]
mod ipc;
#[cfg(feature = "mpris")]
mod mpris;
use crate::command::{Command, JumpMode};
use crate::commands::CommandManager;
use crate::config::Config;
use crate::config::{cache_path, Config};
use crate::events::{Event, EventManager};
use crate::ext_traits::CursiveExt;
use crate::library::Library;
@@ -351,6 +354,16 @@ fn main() -> Result<(), String> {
#[cfg(unix)]
let mut signals = Signals::new([SIGTERM, SIGHUP]).expect("could not register signal handler");
#[cfg(unix)]
let ipc = {
ipc::IpcSocket::new(
ASYNC_RUNTIME.handle(),
cache_path("ncspot.sock"),
event_manager.clone(),
)
.map_err(|e| e.to_string())?
};
// cursive event loop
while cursive.is_running() {
cursive.step();
@@ -372,6 +385,9 @@ fn main() -> Result<(), String> {
#[cfg(feature = "mpris")]
mpris_manager.update();
#[cfg(unix)]
ipc.publish(&state, queue.get_current());
if state == PlayerEvent::FinishedTrack {
queue.next(false);
}
@@ -380,6 +396,17 @@ fn main() -> Result<(), String> {
queue.handle_event(event);
}
Event::SessionDied => spotify.start_worker(None),
Event::IpcInput(input) => match command::parse(&input) {
Ok(commands) => {
if let Some(data) = cursive.user_data::<UserData>().cloned() {
for cmd in commands {
info!("Executing command from IPC: {cmd}");
data.cmd.handle(&mut cursive, cmd);
}
}
}
Err(e) => error!("Parsing error: {e}"),
},
}
}
}