Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions contrib/Settings-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ auto-share = false
# This will also set the correct timezone, it uses
# time.cloudflare.com and ipapi.co
auto-time = false
# Automatically adjust frontlight warmth based on sun position.
# Coordinates are auto-detected during time sync (via ipapi.co)
# unless manually specified below.
auto-frontlight = false
# Night brightness level when the sun is down (0.0 to 100.0).
auto-frontlight-night-brightness = 10
# Manual GPS coordinates override (latitude, longitude).
# auto-frontlight-manual-coordinates = [51.5074, -0.1278]
# Last auto-detected coordinates from time sync (written automatically).
# auto-frontlight-last-coordinates = [48.8566, 2.3522]
# Defines how the back and forward buttons are mapped to the
# *page forward* and *page backward* actions.
# Possible values: "natural", "inverted".
Expand Down
75 changes: 57 additions & 18 deletions crates/cadmus/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ fn resume(
debug!(task = ?id, "resume called");
if id == TaskId::Suspend {
tasks.retain(|task| task.id != TaskId::Suspend);
if context.settings.frontlight {
let levels = context.settings.frontlight_levels;
context.frontlight.set_warmth(levels.warmth);
context.frontlight.set_intensity(levels.intensity);
}
context.set_frontlight(context.settings.frontlight);
if context.settings.wifi {
if let Ok(wifi) = CURRENT_DEVICE.wifi_manager() {
thread::spawn(move || {
Expand Down Expand Up @@ -367,9 +363,7 @@ fn prepare_share_for_usb(
context.database.close();

if context.settings.frontlight {
context.settings.frontlight_levels = context.frontlight.levels();
context.frontlight.set_intensity(0.0);
context.frontlight.set_warmth(0.0);
context.set_frontlight(false);
}
#[cfg(not(feature = "test"))]
if context.settings.wifi {
Expand Down Expand Up @@ -724,14 +718,7 @@ pub fn run() -> Result<(), Error> {
}
}

if context.settings.frontlight {
let levels = context.settings.frontlight_levels;
context.frontlight.set_warmth(levels.warmth);
context.frontlight.set_intensity(levels.intensity);
} else {
context.frontlight.set_intensity(0.0);
context.frontlight.set_warmth(0.0);
}
context.set_frontlight(context.settings.frontlight);

let mut tasks: Vec<Task> = Vec::new();
let mut background_tasks = TaskManager::new();
Expand Down Expand Up @@ -1106,8 +1093,9 @@ pub fn run() -> Result<(), Error> {

if context.settings.frontlight {
context.settings.frontlight_levels = context.frontlight.levels();
context.frontlight.set_intensity(0.0);
context.frontlight.set_warmth(0.0);
if let Err(error) = context.frontlight.turn_off() {
tracing::error!(error = %error, "failed to turn off frontlight for suspend");
}
}
if context.settings.wifi {
if let Ok(wifi) = CURRENT_DEVICE.wifi_manager() {
Expand Down Expand Up @@ -1324,6 +1312,57 @@ pub fn run() -> Result<(), Error> {
&mut context,
);
}
Event::SetFrontlightLevels(levels) => {
#[cfg(feature = "kobo")]
if let Err(e) = background_tasks.stop(&cadmus_core::task::TaskId::AutoFrontlight)
&& !matches!(
e,
cadmus_core::task::TaskError::NotRunning(
cadmus_core::task::TaskId::AutoFrontlight
)
)
{
tracing::warn!(error = %e, "failed to stop auto_frontlight task after manual adjustment");
}

if let Err(error) = context.frontlight.set_intensity(levels.intensity) {
tracing::error!(error = %error, "failed to set frontlight intensity");
}
if let Err(error) = context.frontlight.set_warmth(levels.warmth) {
tracing::error!(error = %error, "failed to set frontlight warmth");
}
context.settings.frontlight_levels = levels;
}
// TODO(OGKevin): once https://github.com/OGKevin/cadmus/issues/582 is done
// this shall be refactored accordingly.
Event::UpdateAutoFrontlight => {
if context.settings.auto_frontlight && context.settings.frontlight {
if let Some(coords) =
cadmus_core::settings::resolve_coordinates(&context.settings)
{
let night_brightness = context
.settings
.auto_frontlight_night_brightness
.unwrap_or_default();
let current_intensity = context.frontlight.levels().intensity;
let levels = cadmus_core::frontlight::auto::compute_auto_frontlight_levels(
Local::now(),
coords,
night_brightness,
current_intensity,
);
if let Err(error) = context.frontlight.set_intensity(levels.intensity) {
tracing::error!(error = %error, "failed to set auto frontlight intensity");
}
if let Err(error) = context.frontlight.set_warmth(levels.warmth) {
tracing::error!(error = %error, "failed to set auto frontlight warmth");
}
context.settings.frontlight_levels = levels;
} else {
tracing::debug!("no coordinates available for auto-frontlight");
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Event::Open(info) => {
let rotation = context.display.rotation;
let dithered = context.fb.dithered();
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ chrono = { version = "0.4.42", features = [
"clock",
], default-features = false }
chrono-tz = "0.8"
sunrise = "1.2"
reqwest = { version = "0.13.1", default-features = false, features = [
"blocking",
"form",
Expand Down
5 changes: 5 additions & 0 deletions crates/core/i18n/en-GB/cadmus_core.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ settings-finished-action-close = Close
settings-finished-action-goto-next = Go to Next

# Settings - General
settings-general-auto-frontlight = Automatic Frontlight
settings-general-auto-frontlight-brightness = Auto Night Brightness
settings-general-auto-frontlight-brightness-input = Night Brightness (% 0-100)
settings-general-auto-frontlight-manual-coordinates = Manual Coordinates
settings-general-auto-frontlight-manual-coordinates-input = lat,lon | coordinates
Comment thread
coderabbitai[bot] marked this conversation as resolved.
settings-general-auto-power-off = Auto Power Off (days)
settings-general-auto-power-off-input = Auto Power Off (days, 0 = never)
settings-general-auto-suspend = Auto Suspend (minutes)
Expand Down
40 changes: 35 additions & 5 deletions crates/core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Context {
pub dictionaries: BTreeMap<String, Dictionary>,
pub keyboard_layouts: BTreeMap<String, Layout>,
pub input_history: FxHashMap<ViewId, VecDeque<String>>,
// TODO(OGKevin): this shall be on the device struct, instead of on context
pub frontlight: Box<dyn Frontlight>,
pub battery: Box<dyn Battery>,
pub lightsensor: Box<dyn LightSensor>,
Expand Down Expand Up @@ -209,17 +210,46 @@ impl Context {
}
}

/// Enables or disables the device frontlight and keeps the persisted
/// frontlight settings in sync.
///
/// When automatic frontlight is enabled and coordinates are available,
/// turning the light on recomputes the effective levels for the current
/// time before applying them.
pub fn set_frontlight(&mut self, enable: bool) {
self.settings.frontlight = enable;

if enable {
let levels = self.settings.frontlight_levels;
self.frontlight.set_warmth(levels.warmth);
self.frontlight.set_intensity(levels.intensity);
let levels = if self.settings.auto_frontlight {
if let Some(coords) = crate::settings::resolve_coordinates(&self.settings) {
let night_brightness = self
.settings
.auto_frontlight_night_brightness
.unwrap_or_default();
crate::frontlight::auto::compute_auto_frontlight_levels(
Local::now(),
coords,
night_brightness,
self.settings.frontlight_levels.intensity,
)
} else {
self.settings.frontlight_levels
}
} else {
self.settings.frontlight_levels
};
if let Err(error) = self.frontlight.set_warmth(levels.warmth) {
tracing::error!(error = %error, "failed to set frontlight warmth");
}
if let Err(error) = self.frontlight.set_intensity(levels.intensity) {
tracing::error!(error = %error, "failed to set frontlight intensity");
}
self.settings.frontlight_levels = levels;
} else {
self.settings.frontlight_levels = self.frontlight.levels();
self.frontlight.set_intensity(0.0);
self.frontlight.set_warmth(0.0);
if let Err(error) = self.frontlight.turn_off() {
tracing::error!(error = %error, "failed to turn off frontlight");
}
}
}
}
Expand Down
Loading
Loading