Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Config structs according to features #800

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ xdg = "2.2"
librespot = { version = "0.1.1", default-features = false, features = ["with-tremor"] }
toml = "0.5.8"
color-eyre = "0.5"
cfg-if = "1"
robinvd marked this conversation as resolved.
Show resolved Hide resolved

[target."cfg(target_os = \"macos\")".dependencies]
whoami = "0.9.0"
Expand Down
110 changes: 74 additions & 36 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,19 @@ pub struct SharedConfigValues {
volume_controller: Option<VolumeController>,

/// The audio device
#[cfg(feature = "alsa-backend")]
#[structopt(long, value_name = "string")]
device: Option<String>,
device: String,

/// The control device
#[cfg(feature = "alsa-backend")]
#[structopt(long, value_name = "string")]
control: Option<String>,
control: String,

/// The mixer to use
#[cfg(feature = "alsa-backend")]
#[structopt(long, value_name = "string")]
mixer: Option<String>,
mixer: String,

/// The device name displayed in Spotify
#[structopt(long, short, value_name = "string")]
Expand Down Expand Up @@ -429,7 +432,8 @@ impl fmt::Debug for SharedConfigValues {
None
};

f.debug_struct("SharedConfigValues")
let mut deb_struct = f.debug_struct("SharedConfigValues");
deb_struct
.field("username", &username_value)
.field("username_cmd", &username_cmd_value)
.field("password", &password_value)
Expand All @@ -441,18 +445,25 @@ impl fmt::Debug for SharedConfigValues {
.field("no-audio-cache", &self.no_audio_cache)
.field("backend", &self.backend)
.field("volume_controller", &self.volume_controller)
.field("device", &self.device)
.field("control", &self.control)
.field("mixer", &self.mixer)
.field("device_name", &self.device_name)
.field("bitrate", &self.bitrate)
.field("initial_volume", &self.initial_volume)
.field("volume_normalisation", &self.volume_normalisation)
.field("normalisation_pregain", &self.normalisation_pregain)
.field("zeroconf_port", &self.zeroconf_port)
.field("proxy", &self.proxy)
.field("device_type", &self.device_type)
.finish()
.field("device_type", &self.device_type);
cfg_if::cfg_if! {
if #[cfg(feature = "alsa-backend")] {
deb_struct
.field("device", &self.device)
.field("control", &self.control)
.field("mixer", &self.mixer)
.finish()
} else {
deb_struct.finish()
}
}
}
}

Expand Down Expand Up @@ -494,28 +505,52 @@ impl SharedConfigValues {
}
}

cfg_if::cfg_if! {
Copy link
Contributor

@robinvd robinvd Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this might work as well (and be nicer)

merge!(general fields)

#[cfg(alsa)]
merge!(alsa fields)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. That's a good one.

if #[cfg(feature = "alsa-backend")] {
merge!(
backend,
username,
username_cmd,
password,
password_cmd,
normalisation_pregain,
bitrate,
initial_volume,
device_name,
mixer,
control,
device,
volume_controller,
cache_path,
on_song_change_hook,
zeroconf_port,
proxy,
device_type,
use_mpris
);
} else {
merge!(
backend,
username,
username_cmd,
password,
password_cmd,
normalisation_pregain,
bitrate,
initial_volume,
device_name,
volume_controller,
cache_path,
on_song_change_hook,
zeroconf_port,
proxy,
device_type,
use_mpris
);
}
}

// Handles Option<T> merging.
merge!(
backend,
username,
username_cmd,
password,
password_cmd,
normalisation_pregain,
bitrate,
initial_volume,
device_name,
mixer,
control,
device,
volume_controller,
cache_path,
on_song_change_hook,
zeroconf_port,
proxy,
device_type,
use_mpris
);

// Handles boolean merging.
self.use_keyring |= other.use_keyring;
Expand Down Expand Up @@ -550,12 +585,12 @@ pub(crate) struct SpotifydConfig {
pub(crate) use_mpris: bool,
pub(crate) cache: Option<Cache>,
pub(crate) backend: Option<String>,
pub(crate) audio_device: Option<String>,
#[allow(unused)]
pub(crate) control_device: Option<String>,
#[allow(unused)]
pub(crate) mixer: Option<String>,
#[allow(unused)]
#[cfg(feature = "alsa-backend")]
pub(crate) audio_device: String,
#[cfg(feature = "alsa-backend")]
pub(crate) control_device: String,
#[cfg(feature = "alsa-backend")]
pub(crate) mixer: String,
pub(crate) volume_controller: VolumeController,
pub(crate) initial_volume: Option<u16>,
pub(crate) device_name: String,
Expand Down Expand Up @@ -683,8 +718,11 @@ pub(crate) fn get_internal_config(config: CliConfig) -> SpotifydConfig {
use_mpris: config.shared_config.use_mpris.unwrap_or(true),
cache,
backend: Some(backend),
#[cfg(feature = "alsa-backend")]
audio_device: config.shared_config.device,
#[cfg(feature = "alsa-backend")]
control_device: config.shared_config.control,
#[cfg(feature = "alsa-backend")]
mixer: config.shared_config.mixer,
volume_controller,
initial_volume,
Expand Down
30 changes: 17 additions & 13 deletions src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "alsa_backend")]
use crate::alsa_mixer;
use crate::{config, main_loop};
use futures::{self, Future};
#[cfg(feature = "dbus_keyring")]
Expand Down Expand Up @@ -27,8 +25,9 @@ pub(crate) fn initial_state(
handle: Handle,
config: config::SpotifydConfig,
) -> main_loop::MainLoopState {
#[cfg(feature = "alsa_backend")]
let mut mixer = {
let mut mixer;
cfg_if::cfg_if! {
if #[cfg(feature = "alsa-backend")] {
let local_audio_device = config.audio_device.clone();
let local_control_device = config.control_device.clone();
let local_mixer = config.mixer.clone();
Expand All @@ -44,7 +43,7 @@ pub(crate) fn initial_state(
config.volume_controller,
config::VolumeController::AlsaLinear
);
Box::new(move || {
mixer =
Box::new(alsa_mixer::AlsaMixer {
device: local_control_device
.clone()
Expand All @@ -53,16 +52,14 @@ pub(crate) fn initial_state(
mixer: local_mixer.clone().unwrap_or_else(|| "Master".to_string()),
linear_scaling: linear,
}) as Box<dyn mixer::Mixer>
}) as Box<dyn FnMut() -> Box<dyn Mixer>>

}
}
};

#[cfg(not(feature = "alsa_backend"))]
let mut mixer = {
info!("Using software volume controller.");
Box::new(|| Box::new(mixer::softmixer::SoftMixer::open(None)) as Box<dyn Mixer>)
} else {
info!("Using software volume controller.");
mixer = Box::new(|| Box::new(mixer::softmixer::SoftMixer::open(None)) as Box<dyn Mixer>)
as Box<dyn FnMut() -> Box<dyn Mixer>>
}
};

let cache = config.cache;
Expand Down Expand Up @@ -136,12 +133,19 @@ pub(crate) fn initial_state(
};

let backend = find_backend(backend.as_ref().map(String::as_ref));
let audio_device;
cfg_if::cfg_if! {
if #[cfg(feature = "alsa-backend")] {
audio_device = Some(config.audio_device.clone())
}else {
audio_device = None
}};
main_loop::MainLoopState {
librespot_connection: main_loop::LibreSpotConnection::new(connection, discovery_stream),
audio_setup: main_loop::AudioSetup {
mixer,
backend,
audio_device: config.audio_device.clone(),
audio_device,
},
spotifyd_state: main_loop::SpotifydState {
ctrl_c_stream: Box::new(ctrl_c(&handle).flatten_stream()),
Expand Down