Make location configurable

This commit is contained in:
Gergely Polonkai 2022-05-21 06:29:39 +02:00
parent bd521ac62d
commit 98e5f3c06d
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
3 changed files with 165 additions and 8 deletions

123
Cargo.lock generated
View File

@ -124,6 +124,26 @@ dependencies = [
"adler32", "adler32",
] ]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]] [[package]]
name = "dlib" name = "dlib"
version = "0.5.0" version = "0.5.0"
@ -178,6 +198,17 @@ dependencies = [
"ttf-parser", "ttf-parser",
] ]
[[package]]
name = "getrandom"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "gif" name = "gif"
version = "0.11.3" version = "0.11.3"
@ -384,6 +415,26 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ae028b272a6e99d9f8260ceefa3caa09300a8d6c8d2b2001316474bc52122e9" checksum = "9ae028b272a6e99d9f8260ceefa3caa09300a8d6c8d2b2001316474bc52122e9"
[[package]]
name = "redox_syscall"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall",
"thiserror",
]
[[package]] [[package]]
name = "resvg" name = "resvg"
version = "0.22.0" version = "0.22.0"
@ -451,6 +502,26 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
[[package]]
name = "serde"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f26faba0c3959972377d3b2d306ee9f71faee9714294e41bb777f83f88578be"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "simplecss" name = "simplecss"
version = "0.2.1" version = "0.2.1"
@ -522,6 +593,37 @@ dependencies = [
"siphasher", "siphasher",
] ]
[[package]]
name = "syn"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a07e33e919ebcd69113d5be0e4d70c5707004ff45188910106854f38b960df4a"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "thiserror"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"
@ -547,6 +649,15 @@ dependencies = [
"safe_arch", "safe_arch",
] ]
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "ttf-parser" name = "ttf-parser"
version = "0.15.0" version = "0.15.0"
@ -735,11 +846,14 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"resvg", "resvg",
"serde",
"smithay-client-toolkit", "smithay-client-toolkit",
"suncalc", "suncalc",
"svg", "svg",
"tiny-skia", "tiny-skia",
"toml",
"usvg", "usvg",
"xdg",
] ]
[[package]] [[package]]
@ -751,6 +865,15 @@ dependencies = [
"nom", "nom",
] ]
[[package]]
name = "xdg"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4583db5cbd4c4c0303df2d15af80f0539db703fa1c68802d4cbbd2dd0f88f6"
dependencies = [
"dirs",
]
[[package]] [[package]]
name = "xml-rs" name = "xml-rs"
version = "0.8.4" version = "0.8.4"

View File

@ -11,3 +11,6 @@ usvg = "0.22"
tiny-skia = "0.6" tiny-skia = "0.6"
resvg = "0.22" resvg = "0.22"
suncalc = "0.4" suncalc = "0.4"
toml = "0.5"
serde = { version = "1.0", features = ["derive"] }
xdg = "2.4"

View File

@ -1,6 +1,7 @@
extern crate smithay_client_toolkit as sctk; extern crate smithay_client_toolkit as sctk;
use std::fmt; use std::fmt;
use std::fs;
use std::time::SystemTime; use std::time::SystemTime;
use chrono::prelude::{Local, Utc}; use chrono::prelude::{Local, Utc};
@ -9,6 +10,7 @@ use chrono::Timelike;
use sctk::reexports::client::protocol::{wl_shm, wl_surface}; use sctk::reexports::client::protocol::{wl_shm, wl_surface};
use sctk::shm::AutoMemPool; use sctk::shm::AutoMemPool;
use sctk::window::{Event as WEvent, FallbackFrame}; use sctk::window::{Event as WEvent, FallbackFrame};
use serde::Deserialize;
use svg::node::element::path::Data as PathData; use svg::node::element::path::Data as PathData;
use svg::node::element::{ use svg::node::element::{
Circle, Definitions, Group, Line, Path, Rectangle, Style, Text, TextPath, 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", "Blossom", "Ladybug", "Geese", "Dust", "Peach", "Fog", "Acorn", "Gourd", "Soup", "Crow",
"Mushroom", "Thunder", "Frost", "Lantern", "Mushroom", "Thunder", "Frost", "Lantern",
]; ];
// TODO: Make these configurable
const LATITUDE: f64 = 47.655235;
const LONGITUDE: f64 = 19.2868815;
enum Season { enum Season {
Spring, 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 { fn seconds_to_degrees(seconds: i32) -> f32 {
seconds as f32 * 360.0 / 86400.0 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) 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 local_timestamp = Local::now();
let utc_hour = local_timestamp.with_timezone(&Utc).time().hour(); let utc_hour = local_timestamp.with_timezone(&Utc).time().hour();
let local_hour = local_timestamp.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_radius = image_width as f32 * 0.071428571;
let moon_phase = moon_illumination.phase * 28.0; 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 let noon = Utc
.timestamp_millis(sun_times.solar_noon.0) .timestamp_millis(sun_times.solar_noon.0)
.time() .time()
@ -594,6 +605,13 @@ fn gen_svg() -> Document {
} }
fn main() { 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) let (env, display, mut queue) = sctk::new_default_environment!(SeasonalClock, desktop)
.expect("Unable to connect to a Wayland compositor"); .expect("Unable to connect to a Wayland compositor");
@ -636,7 +654,13 @@ fn main() {
let mut dimensions = (700, 700); let mut dimensions = (700, 700);
if !env.get_shell().unwrap().needs_configure() { 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() window.refresh()
} }
@ -684,7 +708,13 @@ fn main() {
if need_redraw { if need_redraw {
need_redraw = false; 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() { if let Err(e) = display.flush() {
@ -714,8 +744,9 @@ fn redraw(
pool: &mut AutoMemPool, pool: &mut AutoMemPool,
surface: &wl_surface::WlSurface, surface: &wl_surface::WlSurface,
(buf_x, buf_y): (u32, u32), (buf_x, buf_y): (u32, u32),
config: &Config,
) -> Result<(), ::std::io::Error> { ) -> Result<(), ::std::io::Error> {
let document = gen_svg(); let document = gen_svg(config);
let bytes = document.to_string(); let bytes = document.to_string();
let mut opt = usvg::Options::default(); let mut opt = usvg::Options::default();