Make numlock state on boot configurable

Make numlock state on boot configurable

This will expose 3 settings for numlock behavior:
1. Numlock is off on boot (this is the current default behavior)
2. Numlock is on on boot
3. Numlock will restore the state from the last boot

Fixes #369

* Address comments:

Get keyboard after create_seat called rather than returning from
create_seat.
Use constants rather than magic numbers for keypress.
Only save updated modifier state after keypresses are handled/skipped.

* Remove unused import, fold other into existing use block.
This commit is contained in:
Paul Daniel Faria 2025-02-12 05:35:22 -08:00 committed by GitHub
parent ec1026d9b9
commit f1f9d205be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 133 additions and 11 deletions

View file

@ -12,6 +12,8 @@ use cosmic_config::{ConfigGet, CosmicConfigEntry};
use cosmic_settings_config::window_rules::ApplicationException;
use cosmic_settings_config::{shortcuts, window_rules, Shortcuts};
use serde::{Deserialize, Serialize};
use smithay::utils::{Clock, Monotonic};
use smithay::wayland::xdg_activation::XdgActivationState;
pub use smithay::{
backend::input::KeyState,
input::keyboard::{keysyms as KeySyms, Keysym, ModifiersState},
@ -25,10 +27,6 @@ pub use smithay::{
},
utils::{Logical, Physical, Point, Size, Transform},
};
use smithay::{
utils::{Clock, Monotonic},
wayland::xdg_activation::XdgActivationState,
};
use std::{
cell::RefCell,
collections::{BTreeMap, HashMap},
@ -46,7 +44,8 @@ mod types;
pub use self::types::*;
use cosmic::config::CosmicTk;
use cosmic_comp_config::{
input::InputConfig, workspace::WorkspaceConfig, CosmicCompConfig, TileBehavior, XkbConfig,
input::InputConfig, workspace::WorkspaceConfig, CosmicCompConfig, KeyboardConfig, TileBehavior,
XkbConfig,
};
#[derive(Debug)]
@ -68,6 +67,7 @@ pub struct Config {
#[derive(Debug)]
pub struct DynamicConfig {
outputs: (Option<PathBuf>, OutputsConfig),
numlock: (Option<PathBuf>, NumlockStateConfig),
}
#[derive(Debug, Deserialize, Serialize)]
@ -93,6 +93,11 @@ impl From<Output> for OutputInfo {
}
}
#[derive(Default, Debug, Deserialize, Serialize)]
pub struct NumlockStateConfig {
pub last_state: bool,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum OutputState {
@ -323,9 +328,13 @@ impl Config {
let output_path =
xdg.and_then(|base| base.place_state_file("cosmic-comp/outputs.ron").ok());
let outputs = Self::load_outputs(&output_path);
let numlock_path =
xdg.and_then(|base| base.place_state_file("cosmic-comp/numlock.ron").ok());
let numlock = Self::load_numlock(&numlock_path);
DynamicConfig {
outputs: (output_path, outputs),
numlock: (numlock_path, numlock),
}
}
@ -373,6 +382,24 @@ impl Config {
}
}
fn load_numlock(path: &Option<PathBuf>) -> NumlockStateConfig {
path.as_deref()
.filter(|path| path.exists())
.and_then(|path| {
ron::de::from_reader::<_, NumlockStateConfig>(
OpenOptions::new().read(true).open(path).unwrap(),
)
.map_err(|err| {
warn!(?err, "Failed to read numlock.ron, resetting..");
if let Err(err) = std::fs::remove_file(path) {
error!(?err, "Failed to remove numlock.ron.");
}
})
.ok()
})
.unwrap_or_default()
}
pub fn shortcut_for_action(&self, action: &shortcuts::Action) -> Option<String> {
self.shortcuts.shortcut_for_action(action)
}
@ -639,6 +666,14 @@ impl DynamicConfig {
pub fn outputs_mut(&mut self) -> PersistenceGuard<'_, OutputsConfig> {
PersistenceGuard(self.outputs.0.clone(), &mut self.outputs.1)
}
pub fn numlock(&self) -> &NumlockStateConfig {
&self.numlock.1
}
pub fn numlock_mut(&mut self) -> PersistenceGuard<'_, NumlockStateConfig> {
PersistenceGuard(self.numlock.0.clone(), &mut self.numlock.1)
}
}
fn get_config<T: Default + serde::de::DeserializeOwned>(
@ -688,6 +723,14 @@ fn config_changed(config: cosmic_config::Config, keys: Vec<String>, state: &mut
state.common.atspi_ei.update_keymap(value.clone());
state.common.config.cosmic_conf.xkb_config = value;
}
"keyboard_config" => {
let value = get_config::<KeyboardConfig>(&config, "keyboard_config");
state.common.config.cosmic_conf.keyboard_config = value;
let shell = state.common.shell.read().unwrap();
let seat = shell.seats.last_active();
state.common.config.dynamic_conf.numlock_mut().last_state =
seat.get_keyboard().unwrap().modifier_state().num_lock;
}
"input_default" => {
let value = get_config::<InputConfig>(&config, "input_default");
state.common.config.cosmic_conf.input_default = value;