From fd7a358c85fd9955296f92580dbbb151745d61fc Mon Sep 17 00:00:00 2001 From: sachahjkl Date: Fri, 13 Nov 2026 01:51:24 +0100 Subject: [PATCH] input: keyboard.numLock (on, off, restore) The compositor owns the xkb state and never set Num Lock, so every session started with it off. The global keyboard config's numLock sets it once when the session starts (on, off), or puts back the state saved at the end of the last session (restore: $XDG_STATE_HOME/shoji_wm/num-lock, read and written off the event loop). A keymap change keeps the Num Lock state instead of resetting it, and setting it updates the LEDs and the focused client. --- docs/docs/configuration/input.md | 24 ++ .../current/configuration/input.md | 24 ++ packages/shoji_wm/src/index.ts | 2 + packages/shoji_wm/src/types.ts | 16 + src/shojiwm_lib/src/keyboard_layout.rs | 1 + src/shojiwm_lib/src/lib.rs | 1 + src/shojiwm_lib/src/num_lock.rs | 368 ++++++++++++++++++ src/shojiwm_lib/src/runtime_input.rs | 60 +++ src/shojiwm_lib/src/state.rs | 3 + src/shojiwm_lib/src/virtual_keyboard.rs | 5 + 10 files changed, 504 insertions(+) create mode 100644 src/shojiwm_lib/src/num_lock.rs diff --git a/docs/docs/configuration/input.md b/docs/docs/configuration/input.md index 0b78a8917..f1eefaa9c 100644 --- a/docs/docs/configuration/input.md +++ b/docs/docs/configuration/input.md @@ -56,6 +56,7 @@ override). Each value is an `InputDeviceConfig` with optional `keyboard`, | `model` | `string` | Keyboard model | | `repeatRate` | `number` | Key repeats per second | | `repeatDelay` | `number` | Delay (ms) before key repeat starts | +| `numLock` | `"on" \| "off" \| "restore"` | Num Lock state at session start (see below) | ```ts input.global = { @@ -63,6 +64,29 @@ input.global = { }; ``` +### Num Lock at session start + +`numLock` sets the Num Lock state once per session, when the compositor first +applies a config with it. Later config reloads and new keyboards do not change +it, so a toggle you make with the key stays. A keymap change (a reload, or +another `layout`) keeps the current state. + +- `"on"` / `"off"`: start with Num Lock on or off. +- `"restore"`: start with the state the previous session ended with. The + compositor saves each change to `$XDG_STATE_HOME/shoji_wm/num-lock` + (`~/.local/state/shoji_wm/num-lock` when `XDG_STATE_HOME` is unset). If the + file does not exist yet, Num Lock stays as it is. + +Without `numLock`, the compositor does not touch Num Lock. Only +`input.global.keyboard.numLock` is read: all keyboards share one Num Lock +state, so a per-device value has no effect. + +```ts +input.global = { + keyboard: {numLock: 'restore'}, +}; +``` + ## Reading the active keyboard layout Subscribe to the seat's actual XKB layout from TypeScript: diff --git a/docs/i18n/ja/docusaurus-plugin-content-docs/current/configuration/input.md b/docs/i18n/ja/docusaurus-plugin-content-docs/current/configuration/input.md index 9c0d8f9f0..7c5c75b44 100644 --- a/docs/i18n/ja/docusaurus-plugin-content-docs/current/configuration/input.md +++ b/docs/i18n/ja/docusaurus-plugin-content-docs/current/configuration/input.md @@ -55,6 +55,7 @@ COMPOSITOR.input.configure((input, context) => { | `model` | `string` | キーボードモデル | | `repeatRate` | `number` | 1秒あたりのキーリピート回数 | | `repeatDelay` | `number` | キーリピート開始までの遅延(ミリ秒) | +| `numLock` | `"on" \| "off" \| "restore"` | セッション開始時の Num Lock の状態(下記参照) | ```ts input.global = { @@ -62,6 +63,29 @@ input.global = { }; ``` +### セッション開始時の Num Lock + +`numLock` は、コンポジターがこの設定を含む config を最初に適用したときに、セッションごとに +1回だけ Num Lock の状態を設定します。その後の config の再読み込みやキーボードの追加では +変更しないため、キーで切り替えた状態はそのまま残ります。キーマップの変更(再読み込みや +別の `layout`)でも現在の状態は保たれます。 + +- `"on"`/`"off"`: Num Lock をオン/オフで開始します。 +- `"restore"`: 前回のセッション終了時の状態で開始します。コンポジターは変更のたびに + `$XDG_STATE_HOME/shoji_wm/num-lock`(`XDG_STATE_HOME` が未設定なら + `~/.local/state/shoji_wm/num-lock`)に保存します。ファイルがまだ無い場合、Num Lock は + そのままです。 + +`numLock` を指定しない場合、コンポジターは Num Lock に触れません。読み取るのは +`input.global.keyboard.numLock` だけです。すべてのキーボードが1つの Num Lock の状態を +共有するため、デバイスごとの値は効果がありません。 + +```ts +input.global = { + keyboard: {numLock: 'restore'}, +}; +``` + ## ノートパソコンの蓋 `COMPOSITOR.event.onLidSwitch` は蓋の開閉を通知します。蓋を閉じている間だけ diff --git a/packages/shoji_wm/src/index.ts b/packages/shoji_wm/src/index.ts index 1d4e3c45f..6b8647b54 100644 --- a/packages/shoji_wm/src/index.ts +++ b/packages/shoji_wm/src/index.ts @@ -113,6 +113,7 @@ import type { InputScrollMethod, InputTapButtonMap, KeyboardInputConfig, + KeyboardNumLock, PointerInputConfig, TouchpadInputConfig, ProcessController, @@ -839,6 +840,7 @@ export type { InputScrollMethod, InputTapButtonMap, KeyboardInputConfig, + KeyboardNumLock, PointerInputConfig, TouchpadInputConfig, CursorConfig, diff --git a/packages/shoji_wm/src/types.ts b/packages/shoji_wm/src/types.ts index eed2d6c9d..473995c18 100644 --- a/packages/shoji_wm/src/types.ts +++ b/packages/shoji_wm/src/types.ts @@ -2370,6 +2370,7 @@ export type InputMotionSpace = "logical" | "physical"; export type InputClickMethod = "buttonAreas" | "clickfinger"; export type InputScrollMethod = "none" | "twoFinger" | "edge" | "onButtonDown"; export type InputTapButtonMap = "leftRightMiddle" | "leftMiddleRight"; +export type KeyboardNumLock = "on" | "off" | "restore"; export interface KeyboardInputConfig { repeatRate?: number; @@ -2379,6 +2380,21 @@ export interface KeyboardInputConfig { layout?: string; variant?: string; options?: string; + /** + * Num Lock state at session start. Set once per session, when a config + * with it is first applied: later reloads and new keyboards do not undo a + * toggle made with the key. `"restore"` starts with the state the previous + * session ended with, saved in `$XDG_STATE_HOME/shoji_wm/num-lock`. + * Read from `input.global` only: all keyboards share one Num Lock state. + * Unset: the compositor does not touch Num Lock. + * セッション開始時の Num Lock の状態です。この設定を含む config を最初に適用したときに + * セッションごとに1回だけ設定し、その後の再読み込みやキーボードの追加ではキーで切り替えた + * 状態を戻しません。`"restore"` は前回のセッション終了時の状態 + * (`$XDG_STATE_HOME/shoji_wm/num-lock` に保存)で開始します。 + * すべてのキーボードが1つの Num Lock の状態を共有するため、`input.global` の値だけを + * 読み取ります。未指定の場合、コンポジターは Num Lock に触れません。 + */ + numLock?: KeyboardNumLock; } export interface PointerInputConfig { diff --git a/src/shojiwm_lib/src/keyboard_layout.rs b/src/shojiwm_lib/src/keyboard_layout.rs index 1451b798e..99a1a0a0b 100644 --- a/src/shojiwm_lib/src/keyboard_layout.rs +++ b/src/shojiwm_lib/src/keyboard_layout.rs @@ -32,6 +32,7 @@ impl ShojiWM { caps_lock: modifiers.caps_lock, num_lock: modifiers.num_lock, }; + self.num_lock_observed(locks.num_lock); let layout = keyboard.with_xkb_state(self, |context| { let xkb = context.xkb().lock().unwrap(); let layout = xkb.active_layout(); diff --git a/src/shojiwm_lib/src/lib.rs b/src/shojiwm_lib/src/lib.rs index 879604076..047526ae6 100644 --- a/src/shojiwm_lib/src/lib.rs +++ b/src/shojiwm_lib/src/lib.rs @@ -46,6 +46,7 @@ pub mod keyboard_layout; pub mod lid_switch; pub mod log_rate_limit; pub mod modifier_tap; +pub mod num_lock; pub mod output_power; pub mod plasma_virtual_desktop; pub mod plasma_window_management; diff --git a/src/shojiwm_lib/src/num_lock.rs b/src/shojiwm_lib/src/num_lock.rs new file mode 100644 index 000000000..89dcf57b0 --- /dev/null +++ b/src/shojiwm_lib/src/num_lock.rs @@ -0,0 +1,368 @@ +//! The Num Lock state a session starts with (`keyboard.numLock`), and the +//! state file that carries it from one session to the next for `"restore"`. +//! +//! The compositor owns the xkb state, so nothing else sets Num Lock at login: +//! without this every session starts with it off. +use std::{ + ffi::OsString, + io::Write, + path::{Path, PathBuf}, + sync::mpsc, +}; + +use smithay::{input::SeatHandler, reexports::calloop::channel}; +use tracing::warn; + +use crate::{runtime_input::RuntimeNumLock, state::ShojiWM}; + +/// Session bookkeeping for `keyboard.numLock`. +#[derive(Debug, Default)] +pub struct NumLockState { + /// The startup state was set (or asked for, with `"restore"`) in this + /// session. It is set once only, so that a config reload or a new + /// keyboard does not undo what the user did with the key since. + started: bool, + /// A `"restore"` read of the state file is in flight. A change seen + /// before it comes back is the user's, and wins over the file. + restore_pending: bool, + /// The last Num Lock state seen on the session keymap (never a virtual + /// keyboard's), to see changes and to put it back after a keymap change. + last: Option, + /// The thread that writes the state file, started at the first save. + writer: Option>, +} + +/// What the first config with `numLock` does. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum StartupAction { + Set(bool), + ReadSaved, +} + +fn startup_action(mode: Option, started: bool) -> Option { + if started { + return None; + } + match mode? { + RuntimeNumLock::On => Some(StartupAction::Set(true)), + RuntimeNumLock::Off => Some(StartupAction::Set(false)), + RuntimeNumLock::Restore => Some(StartupAction::ReadSaved), + } +} + +/// Whether an observed state is a change to save. The first state seen is +/// where the session started, not a change, and must not overwrite the file +/// before it was read. +fn should_save(mode: Option, previous: Option, current: bool) -> bool { + mode == Some(RuntimeNumLock::Restore) && previous.is_some_and(|previous| previous != current) +} + +fn state_file_path() -> Option { + resolve_state_file_path(std::env::var_os("XDG_STATE_HOME"), std::env::var_os("HOME")) +} + +/// `$XDG_STATE_HOME/shoji_wm/num-lock`, else `~/.local/state/shoji_wm/num-lock`. +/// A relative `XDG_STATE_HOME` is invalid per the XDG spec and is ignored. +fn resolve_state_file_path( + state_home: Option, + home: Option, +) -> Option { + let state_home = state_home + .map(PathBuf::from) + .filter(|path| path.is_absolute()) + .or_else(|| { + home.map(PathBuf::from) + .filter(|path| path.is_absolute()) + .map(|home| home.join(".local").join("state")) + })?; + Some(state_home.join("shoji_wm").join("num-lock")) +} + +fn parse_state(text: &str) -> Option { + match text.trim() { + "on" => Some(true), + "off" => Some(false), + _ => None, + } +} + +/// A missing or unreadable file is no saved state. +fn read_state(path: &Path) -> Option { + parse_state(&std::fs::read_to_string(path).ok()?) +} + +/// Write through a temporary file and a rename, so that a crash or a power +/// cut mid-write leaves the previous state rather than a truncated file. +fn write_state(path: &Path, on: bool) -> std::io::Result<()> { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir)?; + } + let temporary = path.with_extension("tmp"); + { + let mut file = std::fs::File::create(&temporary)?; + file.write_all(if on { b"on\n" } else { b"off\n" })?; + file.sync_all()?; + } + std::fs::rename(&temporary, path) +} + +/// The writer thread keeps the event loop off the disk. It writes only the +/// latest of the states queued while it was busy, so a burst of toggles is +/// one write. +fn spawn_writer() -> Option> { + let (sender, receiver) = mpsc::channel::(); + let spawned = std::thread::Builder::new() + .name("shojiwm-num-lock".into()) + .spawn(move || { + let Some(path) = state_file_path() else { + warn!("num lock: no XDG_STATE_HOME or HOME; the state is not saved"); + return; + }; + while let Ok(mut on) = receiver.recv() { + while let Ok(next) = receiver.try_recv() { + on = next; + } + if let Err(error) = write_state(&path, on) { + warn!(?error, ?path, "num lock: could not save the state"); + } + } + }); + match spawned { + Ok(_) => Some(sender), + Err(error) => { + warn!(?error, "num lock: could not start the writer thread"); + None + } + } +} + +impl ShojiWM { + /// `keyboard.numLock` of the global keyboard config. There is one seat + /// keyboard state for all devices, so a per-device value has no meaning. + fn num_lock_mode(&self) -> Option { + self.runtime_input_config + .global + .as_ref()? + .keyboard + .as_ref()? + .num_lock + } + + /// Set the session's startup Num Lock state, the first time a config + /// with `numLock` is applied. Call it after the keymap is in place: a + /// new keymap starts with no lock set. + pub fn apply_num_lock_startup(&mut self) { + if self.virtual_keymap_active() || self.seat.get_keyboard().is_none() { + return; + } + let Some(action) = startup_action(self.num_lock_mode(), self.num_lock.started) else { + return; + }; + self.num_lock.started = true; + match action { + StartupAction::Set(on) => self.set_num_lock(on), + StartupAction::ReadSaved => self.read_saved_num_lock(), + } + } + + /// Read the state file on a thread; the result comes back to the event + /// loop through a channel. + fn read_saved_num_lock(&mut self) { + let (sender, receiver) = channel::channel::>(); + // The channel removes itself once the thread drops its sender. + let inserted = self + .loop_handle + .insert_source(receiver, |event, _, state: &mut ShojiWM| { + if let channel::Event::Msg(saved) = event { + state.saved_num_lock_read(saved); + } + }); + if let Err(error) = inserted { + warn!(?error, "num lock: could not watch the state file read"); + return; + } + self.num_lock.restore_pending = true; + let spawned = std::thread::Builder::new() + .name("shojiwm-num-lock-read".into()) + .spawn(move || { + let _ = sender.send(state_file_path().and_then(|path| read_state(&path))); + }); + if let Err(error) = spawned { + warn!(?error, "num lock: could not start the state file read"); + self.num_lock.restore_pending = false; + } + } + + fn saved_num_lock_read(&mut self, saved: Option) { + if !std::mem::take(&mut self.num_lock.restore_pending) { + return; + } + // No saved state: leave Num Lock as the session has it. + if let Some(on) = saved + && !self.virtual_keymap_active() + { + self.set_num_lock(on); + } + } + + /// The Num Lock state of the session keymap. While a virtual keyboard's + /// keymap runs, that is the last one seen before it. + pub fn session_num_lock(&self) -> Option { + if self.virtual_keymap_active() { + return self.num_lock.last; + } + self.seat + .get_keyboard() + .map(|keyboard| keyboard.modifier_state().num_lock) + } + + /// A new keymap starts with no lock set (smithay builds a new xkb state). + /// With `numLock` set, put back the state from before the change, so a + /// config reload or a layout change does not turn Num Lock off. + pub fn keep_num_lock_after_keymap_change(&mut self, before: Option) { + if self.num_lock_mode().is_none() { + return; + } + if let Some(on) = before { + self.set_num_lock(on); + } + } + + /// Called with each session keyboard state the config is sent. + pub fn num_lock_observed(&mut self, on: bool) { + let previous = self.num_lock.last.replace(on); + if previous.is_some_and(|previous| previous != on) { + // The user's own toggle wins over a saved state still on its way. + self.num_lock.restore_pending = false; + } + if should_save(self.num_lock_mode(), previous, on) { + self.save_num_lock(on); + } + } + + fn save_num_lock(&mut self, on: bool) { + if self.num_lock.writer.is_none() { + self.num_lock.writer = spawn_writer(); + } + let Some(writer) = &self.num_lock.writer else { + return; + }; + if writer.send(on).is_err() { + // The thread ended (no state directory); do not restart it. + warn!("num lock: the writer thread stopped; the state is not saved"); + } + } + + fn set_num_lock(&mut self, on: bool) { + let Some(keyboard) = self.seat.get_keyboard() else { + return; + }; + let mut modifiers = keyboard.modifier_state(); + if modifiers.num_lock != on { + modifiers.num_lock = on; + let leds = keyboard.led_state(); + let layout = keyboard.with_xkb_state(self, |context| { + let xkb = context.xkb().lock().unwrap(); + xkb.active_layout() + }); + if keyboard.set_modifier_state(modifiers) != 0 { + // set_modifier_state drops the locked layout (it masks the + // layout index with a state flag): put the active layout back. + keyboard.with_xkb_state(self, |mut context| context.set_layout(layout)); + // set_modifier_state tells neither the focused client nor + // the keyboards' LEDs. + keyboard.advertise_modifier_state(self); + let new_leds = keyboard.led_state(); + if new_leds != leds { + let seat = self.seat.clone(); + self.led_state_changed(&seat, new_leds); + } + self.current_keyboard_modifiers = keyboard.modifier_state(); + } + } + // Set by the compositor, not toggled by the user: not a change to + // save. The config and panels see it through sync_keyboard_state. + self.num_lock.last = Some(on); + self.sync_keyboard_state(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn startup_sets_the_configured_state_once() { + assert_eq!( + startup_action(Some(RuntimeNumLock::On), false), + Some(StartupAction::Set(true)) + ); + assert_eq!( + startup_action(Some(RuntimeNumLock::Off), false), + Some(StartupAction::Set(false)) + ); + assert_eq!( + startup_action(Some(RuntimeNumLock::Restore), false), + Some(StartupAction::ReadSaved) + ); + assert_eq!(startup_action(Some(RuntimeNumLock::On), true), None); + assert_eq!(startup_action(Some(RuntimeNumLock::Restore), true), None); + assert_eq!(startup_action(None, false), None); + } + + #[test] + fn only_restore_saves_and_only_changes() { + let restore = Some(RuntimeNumLock::Restore); + assert!(should_save(restore, Some(false), true)); + assert!(should_save(restore, Some(true), false)); + assert!(!should_save(restore, Some(true), true)); + // The first state seen must not overwrite the saved one. + assert!(!should_save(restore, None, false)); + assert!(!should_save(Some(RuntimeNumLock::On), Some(false), true)); + assert!(!should_save(None, Some(false), true)); + } + + #[test] + fn state_file_path_follows_xdg_state_home() { + assert_eq!( + resolve_state_file_path(Some("/state".into()), Some("/home/user".into())), + Some(PathBuf::from("/state/shoji_wm/num-lock")) + ); + assert_eq!( + resolve_state_file_path(Some("relative".into()), Some("/home/user".into())), + Some(PathBuf::from("/home/user/.local/state/shoji_wm/num-lock")) + ); + assert_eq!( + resolve_state_file_path(None, Some("/home/user".into())), + Some(PathBuf::from("/home/user/.local/state/shoji_wm/num-lock")) + ); + assert_eq!(resolve_state_file_path(None, None), None); + } + + #[test] + fn parse_state_accepts_on_and_off_only() { + assert_eq!(parse_state("on\n"), Some(true)); + assert_eq!(parse_state(" off "), Some(false)); + assert_eq!(parse_state("ON"), None); + assert_eq!(parse_state(""), None); + } + + #[test] + fn state_file_round_trip() { + let dir = std::env::temp_dir().join(format!("shoji-num-lock-test-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let path = dir.join("shoji_wm").join("num-lock"); + + assert_eq!(read_state(&path), None); + write_state(&path, true).unwrap(); + assert_eq!(read_state(&path), Some(true)); + write_state(&path, false).unwrap(); + assert_eq!(read_state(&path), Some(false)); + assert!(!path.with_extension("tmp").exists()); + + std::fs::write(&path, "garbage").unwrap(); + assert_eq!(read_state(&path), None); + + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/src/shojiwm_lib/src/runtime_input.rs b/src/shojiwm_lib/src/runtime_input.rs index c61e15684..9414926e6 100644 --- a/src/shojiwm_lib/src/runtime_input.rs +++ b/src/shojiwm_lib/src/runtime_input.rs @@ -48,6 +48,19 @@ pub struct RuntimeKeyboardInputConfig { pub layout: Option, pub variant: Option, pub options: Option, + /// The Num Lock state a session starts with. Read from the global + /// keyboard config only: all keyboards share one seat keyboard state. + pub num_lock: Option, +} + +/// The Num Lock state a session starts with (see `crate::num_lock`). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum RuntimeNumLock { + On, + Off, + /// The state the previous session ended with, kept in a state file. + Restore, } #[derive(Debug, Clone, Default, PartialEq, Deserialize)] @@ -193,6 +206,7 @@ impl RuntimeKeyboardInputConfig { self.layout = other.layout.clone().or_else(|| self.layout.clone()); self.variant = other.variant.clone().or_else(|| self.variant.clone()); self.options = other.options.clone().or_else(|| self.options.clone()); + self.num_lock = other.num_lock.or(self.num_lock); } fn has_xkb_config(&self) -> bool { @@ -458,6 +472,8 @@ pub fn apply_keyboard_config(state: &mut ShojiWM) { let keyboard = active_keyboard.or(global_keyboard); apply_keyboard_xkb_config(state, keyboard, "runtime keyboard xkb config"); + // After the keymap: a new keymap starts with no lock set. + state.apply_num_lock_startup(); } pub fn apply_keyboard_config_for_backend_device( @@ -502,6 +518,9 @@ fn apply_keyboard_xkb_config( state.restore_session_keymap(); return; } + // Read before the virtual keymap is dropped: while it runs, the session + // state is the one seen before it. + let num_lock = state.session_num_lock(); state.active_virtual_keymap = None; let Some(handle) = state.seat.get_keyboard() else { return; @@ -520,6 +539,7 @@ fn apply_keyboard_xkb_config( Ok(()) => { state.runtime_applied_xkb_config = keyboard; state.refresh_keyboard_focus_for_keymap_change(); + state.keep_num_lock_after_keymap_change(num_lock); } Err(error) => { warn!(?error, "{log_label}: failed to apply"); @@ -660,6 +680,46 @@ impl ShojiWM { } } +#[cfg(test)] +mod num_lock_tests { + use super::*; + + #[test] + fn num_lock_parses_from_the_config() { + let config: RuntimeInputConfig = + serde_json::from_str(r#"{"global":{"keyboard":{"numLock":"restore"}},"device":{}}"#) + .unwrap(); + let keyboard = config.global.unwrap().keyboard.unwrap(); + assert_eq!(keyboard.num_lock, Some(RuntimeNumLock::Restore)); + + for (text, value) in [("on", RuntimeNumLock::On), ("off", RuntimeNumLock::Off)] { + let keyboard: RuntimeKeyboardInputConfig = + serde_json::from_str(&format!(r#"{{"numLock":"{text}"}}"#)).unwrap(); + assert_eq!(keyboard.num_lock, Some(value)); + } + assert!( + serde_json::from_str::(r#"{"numLock":"leave"}"#).is_err() + ); + } + + #[test] + fn num_lock_merges_like_the_other_keyboard_options() { + let mut keyboard = RuntimeKeyboardInputConfig { + num_lock: Some(RuntimeNumLock::On), + ..Default::default() + }; + keyboard.merge_from(&RuntimeKeyboardInputConfig::default()); + assert_eq!(keyboard.num_lock, Some(RuntimeNumLock::On)); + keyboard.merge_from(&RuntimeKeyboardInputConfig { + num_lock: Some(RuntimeNumLock::Off), + ..Default::default() + }); + assert_eq!(keyboard.num_lock, Some(RuntimeNumLock::Off)); + // Num Lock alone is no keymap to compile. + assert!(!keyboard.has_xkb_config()); + } +} + #[cfg(test)] mod motion_space_tests { use super::*; diff --git a/src/shojiwm_lib/src/state.rs b/src/shojiwm_lib/src/state.rs index dd32d6d57..1ae4271b4 100644 --- a/src/shojiwm_lib/src/state.rs +++ b/src/shojiwm_lib/src/state.rs @@ -561,6 +561,8 @@ pub struct ShojiWM { /// A key the grab took inside the keyboard filter, dispatched right after. pub pending_input_grab_key_event: Option, pub current_keyboard_modifiers: ModifiersState, + /// `keyboard.numLock`: the startup state and its state file. + pub num_lock: crate::num_lock::NumLockState, pub consumed_pointer_buttons: HashSet<(String, u32)>, pub modifier_tap: crate::modifier_tap::ModifierTapTracker, pub pending_tap_binding_ids: Vec, @@ -2040,6 +2042,7 @@ impl ShojiWM { runtime_input_grab: None, pending_input_grab_key_event: None, current_keyboard_modifiers: ModifiersState::default(), + num_lock: Default::default(), consumed_pointer_buttons: HashSet::new(), modifier_tap: Default::default(), pending_tap_binding_ids: Vec::new(), diff --git a/src/shojiwm_lib/src/virtual_keyboard.rs b/src/shojiwm_lib/src/virtual_keyboard.rs index ced56630d..69780656e 100644 --- a/src/shojiwm_lib/src/virtual_keyboard.rs +++ b/src/shojiwm_lib/src/virtual_keyboard.rs @@ -92,6 +92,9 @@ impl ShojiWM { /// Put the session's keymap and layout back after a virtual keyboard. /// The focused client is sent the keymap; focus stays where it is. pub fn restore_session_keymap(&mut self) { + // Read while the virtual keymap is still marked active: it is the + // session's state from before the virtual keyboard. + let num_lock = self.session_num_lock(); let Some(active) = self.active_virtual_keymap.take() else { return; }; @@ -111,6 +114,8 @@ impl ShojiWM { }); self.set_keyboard_modifiers(seat_keyboard.modifier_state()); self.run_pending_modifier_bindings(); + // The session keymap came back with a new xkb state and no lock set. + self.keep_num_lock_after_keymap_change(num_lock); } /// Whether the seat runs a virtual keyboard's keymap, whose layout is not