Monitor config as calloop source; use for screen_off_time

Still not sure best way to configure multiple idle times.
This commit is contained in:
Ian Douglas Scott 2024-10-17 18:51:29 -07:00
parent 6d15d0f3c9
commit f2c83714c0
4 changed files with 112 additions and 61 deletions

115
Cargo.lock generated
View file

@ -14,9 +14,9 @@ dependencies = [
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.3.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]] [[package]]
name = "base64" name = "base64"
@ -65,13 +65,38 @@ dependencies = [
"thiserror", "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]] [[package]]
name = "calloop-wayland-source" name = "calloop-wayland-source"
version = "0.3.0" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20" checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20"
dependencies = [ 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", "rustix",
"wayland-backend", "wayland-backend",
"wayland-client", "wayland-client",
@ -79,9 +104,9 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.1.13" version = "1.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48" checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@ -142,9 +167,10 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-config" name = "cosmic-config"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944"
dependencies = [ dependencies = [
"atomicwrites", "atomicwrites",
"calloop 0.14.1",
"cosmic-config-derive", "cosmic-config-derive",
"dirs", "dirs",
"iced_futures", "iced_futures",
@ -160,7 +186,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-config-derive" name = "cosmic-config-derive"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944"
dependencies = [ dependencies = [
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
@ -170,8 +196,8 @@ dependencies = [
name = "cosmic-idle" name = "cosmic-idle"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"calloop", "calloop 0.14.1",
"calloop-wayland-source", "calloop-wayland-source 0.4.0",
"cosmic-config", "cosmic-config",
"cosmic-idle-config", "cosmic-idle-config",
"keyframe", "keyframe",
@ -420,7 +446,7 @@ checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc"
[[package]] [[package]]
name = "iced_core" name = "iced_core"
version = "0.12.0" version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"dnd", "dnd",
@ -438,7 +464,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_futures" name = "iced_futures"
version = "0.12.0" version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#2006d138eb87d080b6c9886ef9aa8079ff533688" source = "git+https://github.com/pop-os/libcosmic#9c62f19e4b80b6bcffde024698015d3a533cb944"
dependencies = [ dependencies = [
"futures", "futures",
"iced_core", "iced_core",
@ -526,9 +552,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.158" version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -738,9 +764,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.30" version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]] [[package]]
name = "polling" name = "polling"
@ -759,27 +785,27 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.86" version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.34.0" version = "0.36.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f24d770aeca0eacb81ac29dfbc55ebcc09312fdd1f8bbecdc7e4a84e000e3b4" checksum = "f7649a7b4df05aed9ea7ec6f628c67c9953a43869b8bc50929569b2999d443fe"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.36" version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -833,9 +859,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.34" version = "0.38.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"errno", "errno",
@ -913,8 +939,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"calloop", "calloop 0.13.0",
"calloop-wayland-source", "calloop-wayland-source 0.3.0",
"cursor-icon", "cursor-icon",
"libc", "libc",
"log", "log",
@ -975,9 +1001,9 @@ dependencies = [
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.12.0" version = "3.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"fastrand", "fastrand",
@ -1012,6 +1038,7 @@ version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [ dependencies = [
"log",
"pin-project-lite", "pin-project-lite",
"tracing-attributes", "tracing-attributes",
"tracing-core", "tracing-core",
@ -1039,9 +1066,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
@ -1143,9 +1170,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-backend" name = "wayland-backend"
version = "0.3.6" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90e11ce2ca99c97b940ee83edbae9da2d56a08f9ea8158550fd77fa31722993" checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6"
dependencies = [ dependencies = [
"cc", "cc",
"downcast-rs", "downcast-rs",
@ -1157,9 +1184,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-client" name = "wayland-client"
version = "0.31.5" version = "0.31.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e321577a0a165911bdcfb39cf029302479d7527b517ee58ab0f6ad09edf0943" checksum = "e3f45d1222915ef1fd2057220c1d9d9624b7654443ea35c3877f7a52bd0a5a2d"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"rustix", "rustix",
@ -1180,9 +1207,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-cursor" name = "wayland-cursor"
version = "0.31.5" version = "0.31.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ef9489a8df197ebf3a8ce8a7a7f0a2320035c3743f3c1bd0bdbccf07ce64f95" checksum = "3a94697e66e76c85923b0d28a0c251e8f0666f58fc47d316c0f4da6da75d37cb"
dependencies = [ dependencies = [
"rustix", "rustix",
"wayland-client", "wayland-client",
@ -1191,9 +1218,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-protocols" name = "wayland-protocols"
version = "0.32.3" version = "0.32.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62989625a776e827cc0f15d41444a3cea5205b963c3a25be48ae1b52d6b4daaa" checksum = "2b5755d77ae9040bb872a25026555ce4cb0ae75fd923e90d25fba07d81057de0"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"wayland-backend", "wayland-backend",
@ -1203,9 +1230,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-protocols-wlr" name = "wayland-protocols-wlr"
version = "0.3.3" version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd993de54a40a40fbe5601d9f1fbcaef0aebcc5fda447d7dc8f6dcbaae4f8953" checksum = "dad87b5fd1b1d3ca2f792df8f686a2a11e3fe1077b71096f7a175ab699f89109"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"wayland-backend", "wayland-backend",
@ -1216,9 +1243,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-scanner" name = "wayland-scanner"
version = "0.31.4" version = "0.31.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7b56f89937f1cf2ee1f1259cf2936a17a1f45d8f0aa1019fae6d470d304cfa6" checksum = "597f2001b2e5fc1121e3d5b9791d3e78f05ba6bfa4641053846248e3a13661c3"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quick-xml", "quick-xml",
@ -1227,9 +1254,9 @@ dependencies = [
[[package]] [[package]]
name = "wayland-sys" name = "wayland-sys"
version = "0.31.4" version = "0.31.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43676fe2daf68754ecf1d72026e4e6c15483198b5d24e888b74d3f22f887a148" checksum = "efa8ac0d8e8ed3e3b5c9fc92c7881406a268e11555abe36493efabe649a29e09"
dependencies = [ dependencies = [
"dlib", "dlib",
"log", "log",
@ -1279,7 +1306,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]

View file

@ -9,10 +9,10 @@ rustix = { version = "0.38.34", features = ["fs"] }
wayland-client = "0.31.5" wayland-client = "0.31.5"
wayland-protocols = { version = "0.32.3", features = ["client", "staging"] } wayland-protocols = { version = "0.32.3", features = ["client", "staging"] }
wayland-protocols-wlr = { version = "0.3.3", features = ["client"] } 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" } cosmic-idle-config = { path = "./cosmic-idle-config" }
calloop = "0.13.0" calloop = "0.14.0"
calloop-wayland-source = "0.3.0" calloop-wayland-source = "0.4.0"
[workspace] [workspace]
members = [ members = [

View file

@ -1,23 +1,16 @@
use cosmic_config::{cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry}; use cosmic_config::{cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub enum IdleAction {
ScreenOff,
Command(Vec<String>),
}
#[derive(Debug, Deserialize, Serialize, Clone, CosmicConfigEntry)] #[derive(Debug, Deserialize, Serialize, Clone, CosmicConfigEntry)]
pub struct CosmicIdleConfig { pub struct CosmicIdleConfig {
pub time: u32, /// Screen off idle time, in ms
pub action: IdleAction, pub screen_off_time: u32,
} }
impl Default for CosmicIdleConfig { impl Default for CosmicIdleConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
time: 60 * 10, screen_off_time: 10 * 60 * 1000,
action: IdleAction::ScreenOff,
} }
} }
} }

View file

@ -2,6 +2,8 @@
use calloop::EventLoop; use calloop::EventLoop;
use calloop_wayland_source::WaylandSource; use calloop_wayland_source::WaylandSource;
use cosmic_config::{calloop::ConfigWatchSource, CosmicConfigEntry};
use cosmic_idle_config::CosmicIdleConfig;
use keyframe::{ease, functions::EaseInOut}; use keyframe::{ease, functions::EaseInOut};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use wayland_client::{ 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}, 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); const FADE_TIME: Duration = Duration::from_millis(2000);
#[derive(Debug)] #[derive(Debug)]
@ -110,13 +111,16 @@ struct StateInner {
layer_shell: zwlr_layer_shell_v1::ZwlrLayerShellV1, layer_shell: zwlr_layer_shell_v1::ZwlrLayerShellV1,
viewporter: wp_viewporter::WpViewporter, viewporter: wp_viewporter::WpViewporter,
single_pixel_buffer_manager: wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1, single_pixel_buffer_manager: wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1,
idle_notifier: ext_idle_notifier_v1::ExtIdleNotifierV1,
seat: wl_seat::WlSeat,
qh: QueueHandle<State>, qh: QueueHandle<State>,
} }
struct State { struct State {
inner: StateInner, inner: StateInner,
outputs: Vec<Output>, outputs: Vec<Output>,
_idle_notification: ext_idle_notification_v1::ExtIdleNotificationV1, conf: CosmicIdleConfig,
idle_notification: ext_idle_notification_v1::ExtIdleNotificationV1,
} }
impl State { 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() { fn main() {
@ -188,7 +203,11 @@ fn main() {
.collect() .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 { let mut state = State {
inner: StateInner { inner: StateInner {
@ -197,16 +216,28 @@ fn main() {
layer_shell, layer_shell,
viewporter, viewporter,
single_pixel_buffer_manager, single_pixel_buffer_manager,
idle_notifier,
seat,
qh, qh,
}, },
_idle_notification, idle_notification,
outputs, outputs,
conf,
}; };
let mut event_loop: EventLoop<State> = EventLoop::try_new().unwrap(); let mut event_loop: EventLoop<State> = EventLoop::try_new().unwrap();
WaylandSource::new(connection, event_queue) WaylandSource::new(connection, event_queue)
.insert(event_loop.handle()) .insert(event_loop.handle())
.unwrap(); .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) {} while let Ok(_) = event_loop.dispatch(None, &mut state) {}
} }