diff options
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..5aae29d --- /dev/null +++ b/src/utils.rs | |||
@@ -0,0 +1,71 @@ | |||
1 | use ::{Filter, NoEqualizedSink}; | ||
2 | |||
3 | use parsing::EqualizerApoParser; | ||
4 | |||
5 | use ::dbus_api::equalizing_manager::OrgPulseAudioExtEqualizing1Manager; | ||
6 | use ::dbus_api::server_lookup::OrgPulseAudioServerLookup1; | ||
7 | use ::dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer; | ||
8 | |||
9 | use dbus::{BusType, Connection, ConnPath}; | ||
10 | use failure::{Error, ResultExt}; | ||
11 | |||
12 | use std::io::{self, Read}; | ||
13 | |||
14 | pub fn connect() -> Result<Connection, Error> { | ||
15 | let pulse_sock_path = | ||
16 | get_pulse_dbus_sock().context("While looking up PulseAudio's D-Bus socket path")?; | ||
17 | info!("PulseAudio's D-Bus socket path is: {}", pulse_sock_path); | ||
18 | |||
19 | trace!("Connecting to PulseAudio's D-Bus socket"); | ||
20 | Ok(Connection::open_private(&pulse_sock_path)?) | ||
21 | } | ||
22 | |||
23 | pub fn get_equalized_sink<'a>(conn: &'a Connection) -> Result<ConnPath<'a, &'a Connection>, Error> { | ||
24 | let conn_manager = conn.with_path("org.PulseAudio.Core1", "/org/pulseaudio/equalizing1", 2000); | ||
25 | |||
26 | // TODO: make that a command-line option | ||
27 | trace!("Getting (one of) the equalized sink(s)"); | ||
28 | let mut sinks = conn_manager.get_equalized_sinks()?; | ||
29 | let sink_path = sinks.pop().ok_or(NoEqualizedSink {})?; | ||
30 | info!("Using equalized sink: {:?}", sink_path.as_cstr()); | ||
31 | |||
32 | trace!("Connecting to equalized sink"); | ||
33 | Ok(conn.with_path("org.PulseAudio.Core1", sink_path, 2000)) | ||
34 | } | ||
35 | |||
36 | pub fn send_filter(conn_sink: &ConnPath<&Connection>, filter: Filter) -> Result<(), Error> { | ||
37 | let channel = conn_sink.get_nchannels()?; | ||
38 | info!("Using channel: {}", channel); | ||
39 | trace!("Sending filter"); | ||
40 | conn_sink.seed_filter( | ||
41 | channel, | ||
42 | filter.frequencies, | ||
43 | filter.coefficients, | ||
44 | filter.preamp, | ||
45 | )?; | ||
46 | Ok(()) | ||
47 | } | ||
48 | |||
49 | pub fn read_filter() -> Result<Filter, Error> { | ||
50 | let mut buffer = String::new(); | ||
51 | let stdin = io::stdin(); | ||
52 | let mut handle = stdin.lock(); | ||
53 | |||
54 | info!("Reading filter in GraphicEQ format from the command line"); | ||
55 | handle.read_to_string(&mut buffer)?; | ||
56 | |||
57 | // TODO: lifetime issue when "throwing" parse error | ||
58 | let filter = EqualizerApoParser::new().parse(&buffer).unwrap(); | ||
59 | info!("Parsed filter: {:?}", filter); | ||
60 | |||
61 | Ok(filter) | ||
62 | } | ||
63 | |||
64 | fn get_pulse_dbus_sock() -> Result<String, Error> { | ||
65 | trace!("Connecting to the D-Bus' session bus"); | ||
66 | let conn = Connection::get_private(BusType::Session)?; | ||
67 | let conn = conn.with_path("org.PulseAudio1", "/org/pulseaudio/server_lookup1", 2000); | ||
68 | |||
69 | trace!("Checking PulseAudio's D-Bus socket path"); | ||
70 | Ok(conn.get_address()?) | ||
71 | } | ||