Skip to content

Commit

Permalink
feat: Add geoclue.timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
bcyran committed Jan 23, 2025
1 parent 19a0f4c commit b31a60d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ You can disable GeoClue by setting `geoclue.enable` to `false`.

The `geoclue.prefer` setting specifies whether GeoClue should be prioritized over manual location when both are available.
This can be useful if you prefer using automatic detection but want to fall back to manual configuration rather than encountering an error if GeoClue is unavailable (e.g. due to no internet connection).

The `geoclue.timeout` option specifies the maximum time (in milliseconds) that `timewall` will wait to obtain a location from GeoClue.
If this time period elapses, `timewall` will either fall back to manual location or fail, depending on the value of `geoclue.prefer`.
```toml
[geoclue]
enable = true
prefer = false
timeout = 1000
```

> [!IMPORTANT]
Expand Down
9 changes: 9 additions & 0 deletions nix/hm-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ in {
default = false;
description = "Prefer GeoClue 2 over manual location configuration.";
};
timeout = lib.mkOption {
type = lib.types.int;
default = null;
description = ''
Time in milliseconds to wait for GeoClue 2 to return a location.
After this time `timewall` will fallback to manual location configuration
or fail, depending on the `prefer` option.
'';
};
};

location = {
Expand Down
8 changes: 4 additions & 4 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ use crate::signals::interruptible_sleep;
use crate::wallpaper::{self, properties::Properties, Wallpaper};
use crate::{cache::LastWallpaper, schedule::current_image_index_appearance};

const GEOCLUE_TIMEOUT: Duration = Duration::from_secs(1);

pub fn info<P: AsRef<Path>>(path: P) -> Result<()> {
validate_wallpaper_file(&path)?;
print!("{}", ImageInfo::from_image(&path)?);
Expand Down Expand Up @@ -201,8 +199,10 @@ fn current_image_index(
}

fn try_get_location(config: &Config) -> Result<Coords> {
let geoclue_timeout = Duration::from_millis(config.geoclue_timeout());

let maybe_location = match (config.geoclue_enabled(), config.geoclue_preferred()) {
(true, true) => match geoclue::get_location(GEOCLUE_TIMEOUT) {
(true, true) => match geoclue::get_location(geoclue_timeout) {
geoclue_ok @ Ok(_) => geoclue_ok,
Err(e) => {
debug!("GeoClue failed, falling back to config location: {}", e);
Expand All @@ -216,7 +216,7 @@ fn try_get_location(config: &Config) -> Result<Coords> {
config_ok @ Ok(_) => config_ok,
Err(e) => {
debug!("Config location failed, falling back to GeoClue: {}", e);
match geoclue::get_location(GEOCLUE_TIMEOUT) {
match geoclue::get_location(geoclue_timeout) {
goeclue_ok @ Ok(_) => goeclue_ok,
geoclue_err @ Err(_) => {
geoclue_err.context("failed to get location from config and GeoClue")
Expand Down
12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DEFAULT_CONFIG_FILE_CONTENT: &str = "\
# [geoclue]
# enable = true
# prefer = false
# timeout = 1000
# Set your geographical location coordinates here
# [location]
Expand Down Expand Up @@ -75,6 +76,8 @@ pub struct Geoclue {
pub enable: bool,
#[serde(default = "Geoclue::prefer_default_value")]
pub prefer: bool,
#[serde(default = "Geoclue::timeout_default_value")]
pub timeout: u64,
}

impl Geoclue {
Expand All @@ -85,13 +88,18 @@ impl Geoclue {
const fn prefer_default_value() -> bool {
false
}

const fn timeout_default_value() -> u64 {
1000
}
}

impl Default for Geoclue {
fn default() -> Self {
Self {
enable: Self::enable_default_value(),
prefer: Self::prefer_default_value(),
timeout: Self::timeout_default_value(),
}
}
}
Expand Down Expand Up @@ -177,6 +185,10 @@ impl Config {
self.geoclue.unwrap_or_default().prefer
}

pub fn geoclue_timeout(&self) -> u64 {
self.geoclue.unwrap_or_default().timeout
}

pub fn try_get_location(&self) -> Result<Coords> {
self.location
.ok_or_else(|| anyhow!("location not set in the configuration"))
Expand Down

0 comments on commit b31a60d

Please sign in to comment.