summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs52
1 files changed, 50 insertions, 2 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 24af366..de9ed3c 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,4 +1,4 @@
1use {Filter, NoEqualizedSink}; 1use {EqualizerConfFormat, Filter};
2 2
3use parsing::EqualizerApoParser; 3use parsing::EqualizerApoParser;
4 4
@@ -8,10 +8,33 @@ use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer;
8 8
9use dbus::{BusType, ConnPath, Connection}; 9use dbus::{BusType, ConnPath, Connection};
10use failure::{Error, ResultExt}; 10use failure::{Error, ResultExt};
11use lalrpop_util;
11 12
13use std::fmt;
12use std::io; 14use std::io;
13 15
16#[derive(Fail, Debug)]
17#[fail(display = "No equalized sink found")]
18struct NoEqualizedSink;
19
20#[derive(Fail, Debug)]
21#[fail(
22 display = "Could not parse using the {} format: {}",
23 format,
24 message
25)]
26struct ParseError {
27 format: EqualizerConfFormat,
28 message: String,
29}
30
14pub fn connect() -> Result<Connection, Error> { 31pub fn connect() -> Result<Connection, Error> {
32 Ok(connect_impl().context(
33 "Could not connect to PulseAudio's D-Bus socket. Have you loaded the 'module-dbus-protocol' module?"
34 )?)
35}
36
37fn connect_impl() -> Result<Connection, Error> {
15 let pulse_sock_path = get_pulse_dbus_sock()?; 38 let pulse_sock_path = get_pulse_dbus_sock()?;
16 info!("PulseAudio's D-Bus socket path is: {}", pulse_sock_path); 39 info!("PulseAudio's D-Bus socket path is: {}", pulse_sock_path);
17 40
@@ -20,6 +43,14 @@ pub fn connect() -> Result<Connection, Error> {
20} 43}
21 44
22pub fn get_equalized_sink<'a>(conn: &'a Connection) -> Result<ConnPath<'a, &'a Connection>, Error> { 45pub fn get_equalized_sink<'a>(conn: &'a Connection) -> Result<ConnPath<'a, &'a Connection>, Error> {
46 Ok(get_equalized_sink_impl(conn).context(
47 "Could not find an equalized sink. Have you loaded the 'module-equalizer-sink' module?",
48 )?)
49}
50
51fn get_equalized_sink_impl<'a>(
52 conn: &'a Connection,
53) -> Result<ConnPath<'a, &'a Connection>, Error> {
23 let conn_manager = conn.with_path("org.PulseAudio.Core1", "/org/pulseaudio/equalizing1", 2000); 54 let conn_manager = conn.with_path("org.PulseAudio.Core1", "/org/pulseaudio/equalizing1", 2000);
24 55
25 // TODO: make that a command-line option 56 // TODO: make that a command-line option
@@ -55,12 +86,29 @@ where
55 file.read_to_string(&mut buffer)?; 86 file.read_to_string(&mut buffer)?;
56 87
57 // TODO: lifetime issue when "throwing" parse error 88 // TODO: lifetime issue when "throwing" parse error
58 let filter = EqualizerApoParser::new().parse(&buffer).unwrap(); 89 let filter = EqualizerApoParser::new()
90 .parse(&buffer)
91 .map_err(|e| convert_parse_error(EqualizerConfFormat::EqualizerAPO, e))?;
59 trace!("Parsed filter: {:?}", filter); 92 trace!("Parsed filter: {:?}", filter);
60 93
61 Ok(filter) 94 Ok(filter)
62} 95}
63 96
97fn convert_parse_error<L, T, E>(
98 format: EqualizerConfFormat,
99 error: lalrpop_util::ParseError<L, T, E>,
100) -> ParseError
101where
102 L: fmt::Display,
103 T: fmt::Display,
104 E: fmt::Display,
105{
106 ParseError {
107 format,
108 message: format!("{}", error),
109 }
110}
111
64fn get_pulse_dbus_sock() -> Result<String, Error> { 112fn get_pulse_dbus_sock() -> Result<String, Error> {
65 trace!("Connecting to the D-Bus' session bus"); 113 trace!("Connecting to the D-Bus' session bus");
66 let conn = Connection::get_private(BusType::Session)?; 114 let conn = Connection::get_private(BusType::Session)?;