Make location configurable

This commit is contained in:
2022-05-21 06:29:39 +02:00
parent bd521ac62d
commit 98e5f3c06d
3 changed files with 165 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
extern crate smithay_client_toolkit as sctk;
use std::fmt;
use std::fs;
use std::time::SystemTime;
use chrono::prelude::{Local, Utc};
@@ -9,6 +10,7 @@ use chrono::Timelike;
use sctk::reexports::client::protocol::{wl_shm, wl_surface};
use sctk::shm::AutoMemPool;
use sctk::window::{Event as WEvent, FallbackFrame};
use serde::Deserialize;
use svg::node::element::path::Data as PathData;
use svg::node::element::{
Circle, Definitions, Group, Line, Path, Rectangle, Style, Text, TextPath,
@@ -23,9 +25,6 @@ const HOUR_NAMES: [&str; 24] = [
"Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow",
"Mushroom", "Thunder", "Frost", "Lantern",
];
// TODO: Make these configurable
const LATITUDE: f64 = 47.655235;
const LONGITUDE: f64 = 19.2868815;
enum Season {
Spring,
@@ -49,6 +48,18 @@ impl fmt::Display for Season {
}
}
#[derive(Deserialize)]
struct Config {
latitude: f64,
longitude: f64,
}
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct CompleteConfig {
seasonal_clock: Config,
}
fn seconds_to_degrees(seconds: i32) -> f32 {
seconds as f32 * 360.0 / 86400.0
}
@@ -245,7 +256,7 @@ fn get_moon_path(image_width: u32, radius: f32, moon_phase: f64) -> Path {
Path::new().set("class", "moon").set("d", path_data)
}
fn gen_svg() -> Document {
fn gen_svg(config: &Config) -> Document {
let local_timestamp = Local::now();
let utc_hour = local_timestamp.with_timezone(&Utc).time().hour();
let local_hour = local_timestamp.time().hour();
@@ -265,7 +276,7 @@ fn gen_svg() -> Document {
let moon_radius = image_width as f32 * 0.071428571;
let moon_phase = moon_illumination.phase * 28.0;
let sun_times = suncalc::get_times(unixtime, LATITUDE, LONGITUDE, None);
let sun_times = suncalc::get_times(unixtime, config.latitude, config.longitude, None);
let noon = Utc
.timestamp_millis(sun_times.solar_noon.0)
.time()
@@ -594,6 +605,13 @@ fn gen_svg() -> Document {
}
fn main() {
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
let config_path = xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory");
let data = fs::read_to_string(config_path).expect("Unable to read file");
let config: CompleteConfig = toml::from_str(&data).unwrap();
let (env, display, mut queue) = sctk::new_default_environment!(SeasonalClock, desktop)
.expect("Unable to connect to a Wayland compositor");
@@ -636,7 +654,13 @@ fn main() {
let mut dimensions = (700, 700);
if !env.get_shell().unwrap().needs_configure() {
redraw(&mut pool, window.surface(), dimensions).expect("Failed to draw");
redraw(
&mut pool,
window.surface(),
dimensions,
&config.seasonal_clock,
)
.expect("Failed to draw");
window.refresh()
}
@@ -684,7 +708,13 @@ fn main() {
if need_redraw {
need_redraw = false;
redraw(&mut pool, window.surface(), dimensions).expect("Failed to draw")
redraw(
&mut pool,
window.surface(),
dimensions,
&config.seasonal_clock,
)
.expect("Failed to draw")
}
if let Err(e) = display.flush() {
@@ -714,8 +744,9 @@ fn redraw(
pool: &mut AutoMemPool,
surface: &wl_surface::WlSurface,
(buf_x, buf_y): (u32, u32),
config: &Config,
) -> Result<(), ::std::io::Error> {
let document = gen_svg();
let document = gen_svg(config);
let bytes = document.to_string();
let mut opt = usvg::Options::default();