Skip to content

Commit 8e3590f

Browse files
wash2Drakulix
authored andcommitted
feat: power button handling
1 parent 5eeff37 commit 8e3590f

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ use cosmic_comp_config::{
4949
XkbConfig, XwaylandDescaling, XwaylandEavesdropping, ZoomConfig,
5050
};
5151

52+
/// Offset used to convert Linux scancode to X11 keycode.
53+
pub(crate) const X11_KEYCODE_OFFSET: u32 = 8;
54+
5255
#[derive(Debug)]
5356
pub struct Config {
5457
pub dynamic_conf: DynamicConfig,
@@ -776,9 +779,6 @@ pub fn change_modifier_state(
776779
scan_code: u32,
777780
state: &mut State,
778781
) {
779-
/// Offset used to convert Linux scancode to X11 keycode.
780-
const X11_KEYCODE_OFFSET: u32 = 8;
781-
782782
let mut input = |key_state, scan_code| {
783783
let time = state.common.clock.now().as_millis();
784784
let _ = keyboard.input(

src/input/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
cosmic_keystate_from_smithay, cosmic_modifiers_eq_smithay,
88
cosmic_modifiers_from_smithay,
99
},
10-
Action, Config, PrivateAction,
10+
Action, Config, PrivateAction, X11_KEYCODE_OFFSET,
1111
},
1212
input::gestures::{GestureState, SwipeAction},
1313
shell::{
@@ -35,8 +35,8 @@ use calloop::{
3535
RegistrationToken,
3636
};
3737
use cosmic_comp_config::{workspace::WorkspaceLayout, NumlockState};
38-
use cosmic_settings_config::shortcuts;
3938
use cosmic_settings_config::shortcuts::action::{Direction, ResizeDirection};
39+
use cosmic_settings_config::{shortcuts, Binding};
4040
use smithay::{
4141
backend::input::{
4242
AbsolutePositionEvent, Axis, AxisSource, Device, DeviceCapability, GestureBeginEvent,
@@ -213,11 +213,32 @@ impl State {
213213
self.common.idle_notifier_state.notify_activity(&seat);
214214

215215
let keycode = event.key_code();
216+
216217
let state = event.state();
217218
trace!(?keycode, ?state, "key");
218219

219220
let serial = SERIAL_COUNTER.next_serial();
220221
let time = Event::time_msec(&event);
222+
223+
const POWER_OFF: u32 = 116;
224+
// if power key is pressed, just use the system action for power off
225+
// this is a workaround for the fact that the power key is not
226+
// available in the keymap, so we cannot use the keymap to determine
227+
// if the key is pressed
228+
if keycode.raw() == POWER_OFF + X11_KEYCODE_OFFSET && state == KeyState::Pressed
229+
{
230+
self.handle_action(
231+
Action::Shortcut(shortcuts::Action::System(
232+
shortcuts::action::System::PowerOff,
233+
)),
234+
&seat,
235+
serial,
236+
time,
237+
Binding::default(),
238+
None,
239+
);
240+
}
241+
221242
let keyboard = seat.get_keyboard().unwrap();
222243
let previous_modifiers = keyboard.modifier_state();
223244
if let Some((action, pattern)) = keyboard

0 commit comments

Comments
 (0)