From 8e3590fb4df997d1bb44ad935cd02c06341b10cc Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 13 Jun 2025 12:28:38 -0400 Subject: [PATCH] feat: power button handling --- Cargo.lock | 10 +++++----- src/config/mod.rs | 6 +++--- src/input/mod.rs | 25 +++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff0eb732..e491e74e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -888,7 +888,7 @@ dependencies = [ [[package]] name = "cosmic-config" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic/#b61a7ebd5fedf2621c39f2547dd007722477cfe2" +source = "git+https://github.com/pop-os/libcosmic/#12317d8103c34d66f005ee868df578abe693c84a" dependencies = [ "atomicwrites", "calloop 0.14.2", @@ -907,7 +907,7 @@ dependencies = [ [[package]] name = "cosmic-config-derive" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic/#b61a7ebd5fedf2621c39f2547dd007722477cfe2" +source = "git+https://github.com/pop-os/libcosmic/#12317d8103c34d66f005ee868df578abe693c84a" dependencies = [ "quote", "syn 1.0.109", @@ -943,7 +943,7 @@ dependencies = [ [[package]] name = "cosmic-settings-config" version = "0.1.0" -source = "git+https://github.com/pop-os/cosmic-settings-daemon#ed1bc9e39e0380ea7e40233bb63960eeb674be34" +source = "git+https://github.com/pop-os/cosmic-settings-daemon#243c36e9e66dcbec83fa63cb3adaceb3bea166a4" dependencies = [ "cosmic-config", "ron", @@ -2279,7 +2279,7 @@ dependencies = [ [[package]] name = "iced_core" version = "0.14.0-dev" -source = "git+https://github.com/pop-os/libcosmic/#b61a7ebd5fedf2621c39f2547dd007722477cfe2" +source = "git+https://github.com/pop-os/libcosmic/#12317d8103c34d66f005ee868df578abe693c84a" dependencies = [ "bitflags 2.9.1", "bytes", @@ -2302,7 +2302,7 @@ dependencies = [ [[package]] name = "iced_futures" version = "0.14.0-dev" -source = "git+https://github.com/pop-os/libcosmic/#b61a7ebd5fedf2621c39f2547dd007722477cfe2" +source = "git+https://github.com/pop-os/libcosmic/#12317d8103c34d66f005ee868df578abe693c84a" dependencies = [ "futures", "iced_core", diff --git a/src/config/mod.rs b/src/config/mod.rs index d6dba891..56425648 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -49,6 +49,9 @@ use cosmic_comp_config::{ XkbConfig, XwaylandDescaling, XwaylandEavesdropping, ZoomConfig, }; +/// Offset used to convert Linux scancode to X11 keycode. +pub(crate) const X11_KEYCODE_OFFSET: u32 = 8; + #[derive(Debug)] pub struct Config { pub dynamic_conf: DynamicConfig, @@ -776,9 +779,6 @@ pub fn change_modifier_state( scan_code: u32, state: &mut State, ) { - /// Offset used to convert Linux scancode to X11 keycode. - const X11_KEYCODE_OFFSET: u32 = 8; - let mut input = |key_state, scan_code| { let time = state.common.clock.now().as_millis(); let _ = keyboard.input( diff --git a/src/input/mod.rs b/src/input/mod.rs index e7aef5cf..66c31a93 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -7,7 +7,7 @@ use crate::{ cosmic_keystate_from_smithay, cosmic_modifiers_eq_smithay, cosmic_modifiers_from_smithay, }, - Action, Config, PrivateAction, + Action, Config, PrivateAction, X11_KEYCODE_OFFSET, }, input::gestures::{GestureState, SwipeAction}, shell::{ @@ -35,8 +35,8 @@ use calloop::{ RegistrationToken, }; use cosmic_comp_config::{workspace::WorkspaceLayout, NumlockState}; -use cosmic_settings_config::shortcuts; use cosmic_settings_config::shortcuts::action::{Direction, ResizeDirection}; +use cosmic_settings_config::{shortcuts, Binding}; use smithay::{ backend::input::{ AbsolutePositionEvent, Axis, AxisSource, Device, DeviceCapability, GestureBeginEvent, @@ -213,11 +213,32 @@ impl State { self.common.idle_notifier_state.notify_activity(&seat); let keycode = event.key_code(); + let state = event.state(); trace!(?keycode, ?state, "key"); let serial = SERIAL_COUNTER.next_serial(); let time = Event::time_msec(&event); + + const POWER_OFF: u32 = 116; + // if power key is pressed, just use the system action for power off + // this is a workaround for the fact that the power key is not + // available in the keymap, so we cannot use the keymap to determine + // if the key is pressed + if keycode.raw() == POWER_OFF + X11_KEYCODE_OFFSET && state == KeyState::Pressed + { + self.handle_action( + Action::Shortcut(shortcuts::Action::System( + shortcuts::action::System::PowerOff, + )), + &seat, + serial, + time, + Binding::default(), + None, + ); + } + let keyboard = seat.get_keyboard().unwrap(); let previous_modifiers = keyboard.modifier_state(); if let Some((action, pattern)) = keyboard