diff --git a/Cargo.lock b/Cargo.lock index 986093f..2f1c9c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -844,6 +844,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" name = "wl-seasonal-hours-clock" version = "0.1.0" dependencies = [ + "calloop", "chrono", "resvg", "serde", diff --git a/Cargo.toml b/Cargo.toml index 7b5e05c..5275d8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ resvg = "0.22" suncalc = "0.4" toml = "0.5" serde = { version = "1.0", features = ["derive"] } -xdg = "2.4" \ No newline at end of file +xdg = "2.4" +calloop = "0.9" diff --git a/src/main.rs b/src/main.rs index 439bd4d..4c66ecf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ extern crate smithay_client_toolkit as sctk; use std::fmt; use std::fs; -use std::time::SystemTime; +use calloop::{timer::Timer, EventLoop}; use chrono::prelude::{Local, Utc}; use chrono::TimeZone; use chrono::Timelike; @@ -633,7 +633,7 @@ fn main() { None }; - let (env, display, mut queue) = sctk::new_default_environment!(SeasonalClock, desktop) + let (env, display, queue) = sctk::new_default_environment!(SeasonalClock, desktop) .expect("Unable to connect to a Wayland compositor"); let surface = env @@ -679,28 +679,31 @@ fn main() { window.refresh() } - let now = SystemTime::now(); - let mut last_elapsed = 0; + let mut event_loop = EventLoop::>::try_new().unwrap(); + let handle = event_loop.handle(); + let source = Timer::new().expect("Failed to create timer event source!"); + let timer_handle = source.handle(); + timer_handle.add_timeout(std::time::Duration::from_secs(1), ""); + + handle + .insert_source(source, |_, timer_handle, event| { + timer_handle.add_timeout(std::time::Duration::from_secs(1), ""); + if event.is_none() { + *event = Some(sctk::window::Event::Refresh); + } + }) + .unwrap(); + + sctk::WaylandSource::new(queue) + .quick_insert(handle) + .unwrap(); loop { // Update every second - // TODO: There’s probably a better way to do this… - match now.elapsed() { - Ok(elapsed) => { - let new_elapsed = elapsed.as_secs(); - - if new_elapsed != last_elapsed { - need_redraw = true; - last_elapsed = new_elapsed; - } - } - Err(e) => { - println!("Error: {:?}", e); - } - } match next_action.take() { Some(WEvent::Close) => break, Some(WEvent::Refresh) => { + redraw(&mut pool, window.surface(), dimensions, &config).expect("Failed to draw"); window.refresh(); window.surface().commit(); } @@ -732,20 +735,7 @@ fn main() { } } - if let Some(guard) = queue.prepare_read() { - if let Err(e) = guard.read_events() { - if e.kind() != ::std::io::ErrorKind::WouldBlock { - eprintln!( - "Error while trying to read from the wayland socked: {:?}", - e - ); - } - } - } - - queue - .dispatch_pending(&mut next_action, |_, _, _| {}) - .expect("Failed to dispatch all messages."); + event_loop.dispatch(None, &mut next_action).unwrap(); } }