From f2c83714c0a26b6152204a6ea3dd482ad0e08ded Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 17 Oct 2024 18:51:29 -0700 Subject: [PATCH] Monitor config as calloop source; use for `screen_off_time` Still not sure best way to configure multiple idle times. --- Cargo.lock | 115 +++++++++++++++++++++------------- Cargo.toml | 6 +- cosmic-idle-config/src/lib.rs | 13 +--- src/main.rs | 39 ++++++++++-- 4 files changed, 112 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee28827..13c3784 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,9 +14,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "base64" @@ -65,13 +65,38 @@ dependencies = [ "thiserror", ] +[[package]] +name = "calloop" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1ead1e1514bce44c0f40e027899fbc595907fc112635bed21b3b5d975c0a5e7" +dependencies = [ + "bitflags 2.6.0", + "polling", + "rustix", + "slab", + "tracing", +] + [[package]] name = "calloop-wayland-source" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20" dependencies = [ - "calloop", + "calloop 0.13.0", + "rustix", + "wayland-backend", + "wayland-client", +] + +[[package]] +name = "calloop-wayland-source" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "876a7a1dbbe026a55ef47a500b123af5a9a0914520f061d467914cf21be95daf" +dependencies = [ + "calloop 0.14.1", "rustix", "wayland-backend", "wayland-client", @@ -79,9 +104,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.13" +version = "1.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48" +checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" dependencies = [ "shlex", ] @@ -142,9 +167,10 @@ dependencies = [ [[package]] name = "cosmic-config" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" +source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944" dependencies = [ "atomicwrites", + "calloop 0.14.1", "cosmic-config-derive", "dirs", "iced_futures", @@ -160,7 +186,7 @@ dependencies = [ [[package]] name = "cosmic-config-derive" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" +source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944" dependencies = [ "quote", "syn 1.0.109", @@ -170,8 +196,8 @@ dependencies = [ name = "cosmic-idle" version = "0.1.0" dependencies = [ - "calloop", - "calloop-wayland-source", + "calloop 0.14.1", + "calloop-wayland-source 0.4.0", "cosmic-config", "cosmic-idle-config", "keyframe", @@ -420,7 +446,7 @@ checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" [[package]] name = "iced_core" version = "0.12.0" -source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" +source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944" dependencies = [ "bitflags 2.6.0", "dnd", @@ -438,7 +464,7 @@ dependencies = [ [[package]] name = "iced_futures" version = "0.12.0" -source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" +source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944" dependencies = [ "futures", "iced_core", @@ -526,9 +552,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.158" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "libloading" @@ -738,9 +764,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "polling" @@ -759,27 +785,27 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" dependencies = [ "unicode-ident", ] [[package]] name = "quick-xml" -version = "0.34.0" +version = "0.36.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f24d770aeca0eacb81ac29dfbc55ebcc09312fdd1f8bbecdc7e4a84e000e3b4" +checksum = "f7649a7b4df05aed9ea7ec6f628c67c9953a43869b8bc50929569b2999d443fe" dependencies = [ "memchr", ] [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -833,9 +859,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.34" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags 2.6.0", "errno", @@ -913,8 +939,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" dependencies = [ "bitflags 2.6.0", - "calloop", - "calloop-wayland-source", + "calloop 0.13.0", + "calloop-wayland-source 0.3.0", "cursor-icon", "libc", "log", @@ -975,9 +1001,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.12.0" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", "fastrand", @@ -1012,6 +1038,7 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -1039,9 +1066,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "walkdir" @@ -1143,9 +1170,9 @@ dependencies = [ [[package]] name = "wayland-backend" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f90e11ce2ca99c97b940ee83edbae9da2d56a08f9ea8158550fd77fa31722993" +checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6" dependencies = [ "cc", "downcast-rs", @@ -1157,9 +1184,9 @@ dependencies = [ [[package]] name = "wayland-client" -version = "0.31.5" +version = "0.31.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e321577a0a165911bdcfb39cf029302479d7527b517ee58ab0f6ad09edf0943" +checksum = "e3f45d1222915ef1fd2057220c1d9d9624b7654443ea35c3877f7a52bd0a5a2d" dependencies = [ "bitflags 2.6.0", "rustix", @@ -1180,9 +1207,9 @@ dependencies = [ [[package]] name = "wayland-cursor" -version = "0.31.5" +version = "0.31.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ef9489a8df197ebf3a8ce8a7a7f0a2320035c3743f3c1bd0bdbccf07ce64f95" +checksum = "3a94697e66e76c85923b0d28a0c251e8f0666f58fc47d316c0f4da6da75d37cb" dependencies = [ "rustix", "wayland-client", @@ -1191,9 +1218,9 @@ dependencies = [ [[package]] name = "wayland-protocols" -version = "0.32.3" +version = "0.32.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62989625a776e827cc0f15d41444a3cea5205b963c3a25be48ae1b52d6b4daaa" +checksum = "2b5755d77ae9040bb872a25026555ce4cb0ae75fd923e90d25fba07d81057de0" dependencies = [ "bitflags 2.6.0", "wayland-backend", @@ -1203,9 +1230,9 @@ dependencies = [ [[package]] name = "wayland-protocols-wlr" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd993de54a40a40fbe5601d9f1fbcaef0aebcc5fda447d7dc8f6dcbaae4f8953" +checksum = "dad87b5fd1b1d3ca2f792df8f686a2a11e3fe1077b71096f7a175ab699f89109" dependencies = [ "bitflags 2.6.0", "wayland-backend", @@ -1216,9 +1243,9 @@ dependencies = [ [[package]] name = "wayland-scanner" -version = "0.31.4" +version = "0.31.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7b56f89937f1cf2ee1f1259cf2936a17a1f45d8f0aa1019fae6d470d304cfa6" +checksum = "597f2001b2e5fc1121e3d5b9791d3e78f05ba6bfa4641053846248e3a13661c3" dependencies = [ "proc-macro2", "quick-xml", @@ -1227,9 +1254,9 @@ dependencies = [ [[package]] name = "wayland-sys" -version = "0.31.4" +version = "0.31.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43676fe2daf68754ecf1d72026e4e6c15483198b5d24e888b74d3f22f887a148" +checksum = "efa8ac0d8e8ed3e3b5c9fc92c7881406a268e11555abe36493efabe649a29e09" dependencies = [ "dlib", "log", @@ -1279,7 +1306,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b5a7f25..be0a5df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,10 +9,10 @@ rustix = { version = "0.38.34", features = ["fs"] } wayland-client = "0.31.5" wayland-protocols = { version = "0.32.3", features = ["client", "staging"] } wayland-protocols-wlr = { version = "0.3.3", features = ["client"] } -cosmic-config = { git = "https://github.com/pop-os/libcosmic" } +cosmic-config = { git = "https://github.com/pop-os/libcosmic", features = ["calloop"] } cosmic-idle-config = { path = "./cosmic-idle-config" } -calloop = "0.13.0" -calloop-wayland-source = "0.3.0" +calloop = "0.14.0" +calloop-wayland-source = "0.4.0" [workspace] members = [ diff --git a/cosmic-idle-config/src/lib.rs b/cosmic-idle-config/src/lib.rs index 434f175..ce1ae96 100644 --- a/cosmic-idle-config/src/lib.rs +++ b/cosmic-idle-config/src/lib.rs @@ -1,23 +1,16 @@ use cosmic_config::{cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry}; use serde::{Deserialize, Serialize}; -#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] -pub enum IdleAction { - ScreenOff, - Command(Vec), -} - #[derive(Debug, Deserialize, Serialize, Clone, CosmicConfigEntry)] pub struct CosmicIdleConfig { - pub time: u32, - pub action: IdleAction, + /// Screen off idle time, in ms + pub screen_off_time: u32, } impl Default for CosmicIdleConfig { fn default() -> Self { Self { - time: 60 * 10, - action: IdleAction::ScreenOff, + screen_off_time: 10 * 60 * 1000, } } } diff --git a/src/main.rs b/src/main.rs index d263913..5ceeb7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use calloop::EventLoop; use calloop_wayland_source::WaylandSource; +use cosmic_config::{calloop::ConfigWatchSource, CosmicConfigEntry}; +use cosmic_idle_config::CosmicIdleConfig; use keyframe::{ease, functions::EaseInOut}; use std::time::{Duration, Instant}; use wayland_client::{ @@ -25,7 +27,6 @@ use wayland_protocols_wlr::{ output_power_management::v1::client::{zwlr_output_power_manager_v1, zwlr_output_power_v1}, }; -const IDLE_TIME: u32 = 3000; const FADE_TIME: Duration = Duration::from_millis(2000); #[derive(Debug)] @@ -110,13 +111,16 @@ struct StateInner { layer_shell: zwlr_layer_shell_v1::ZwlrLayerShellV1, viewporter: wp_viewporter::WpViewporter, single_pixel_buffer_manager: wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1, + idle_notifier: ext_idle_notifier_v1::ExtIdleNotifierV1, + seat: wl_seat::WlSeat, qh: QueueHandle, } struct State { inner: StateInner, outputs: Vec, - _idle_notification: ext_idle_notification_v1::ExtIdleNotificationV1, + conf: CosmicIdleConfig, + idle_notification: ext_idle_notification_v1::ExtIdleNotificationV1, } impl State { @@ -130,6 +134,17 @@ impl State { } } } + + fn recreate_notification(&mut self) { + self.idle_notification.destroy(); + self.idle_notification = self.inner.idle_notifier.get_idle_notification( + self.conf.screen_off_time, + &self.inner.seat, + &self.inner.qh, + (), + ); + self.update_idle(false); + } } fn main() { @@ -188,7 +203,11 @@ fn main() { .collect() }); - let _idle_notification = idle_notifier.get_idle_notification(IDLE_TIME, &seat, &qh, ()); + let config = cosmic_config::Config::new("com.system76.CosmicIdle", 1).unwrap(); + let conf = CosmicIdleConfig::get_entry(&config).unwrap_or_else(|(_, conf)| conf); + + let idle_notification = + idle_notifier.get_idle_notification(conf.screen_off_time, &seat, &qh, ()); let mut state = State { inner: StateInner { @@ -197,16 +216,28 @@ fn main() { layer_shell, viewporter, single_pixel_buffer_manager, + idle_notifier, + seat, qh, }, - _idle_notification, + idle_notification, outputs, + conf, }; let mut event_loop: EventLoop = EventLoop::try_new().unwrap(); WaylandSource::new(connection, event_queue) .insert(event_loop.handle()) .unwrap(); + if let Ok(source) = ConfigWatchSource::new(&config) { + event_loop + .handle() + .insert_source(source, |(config, keys), _, state| { + state.conf.update_keys(&config, &keys); + state.recreate_notification(); + }) + .unwrap(); + } while let Ok(_) = event_loop.dispatch(None, &mut state) {} }