forge
Explore
Log in

Don't you dare go Hollow

Add 'input.keyboard.track_layout' config option

Browse files at this commit

907e0a03efcce6ba35aa6405e130f316bc217c5aKirill ChibisovParent: eb7d20c
Track layout for each window individually when user requested `Window`
tracking mode, keeping the default as global.
resources/default-config.kdlmodified+5 -0
@@ -16,6 +16,11 @@ input {
1616 // Delay is in milliseconds before the repeat starts. Rate is in characters per second.
1717 // repeat-delay 600
1818 // repeat-rate 25
19+
20+ // Niri can remember the keyboard layout globally (the default) or per-window.
21+ // - "global" - layout change is global for all windows.
22+ // - "window" - layout is tracked for each window individually.
23+ // track-layout "global"
1924 }
2025
2126 // Next sections include libinput settings.
src/config.rsmodified+13 -0
@@ -62,6 +62,8 @@ pub struct Keyboard {
6262 pub repeat_delay: u16,
6363 #[knuffel(child, unwrap(argument), default = 25)]
6464 pub repeat_rate: u8,
65+ #[knuffel(child, unwrap(argument), default)]
66+ pub track_layout: TrackLayout,
6567 }
6668
6769 #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)]
@@ -78,6 +80,15 @@ pub struct Xkb {
7880 pub options: Option<String>,
7981 }
8082
83+#[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq)]
84+pub enum TrackLayout {
85+ /// The layout change is global.
86+ #[default]
87+ Global,
88+ /// The layout change is window local.
89+ Window,
90+}
91+
8192 // FIXME: Add the rest of the settings.
8293 #[derive(knuffel::Decode, Debug, Default, PartialEq)]
8394 pub struct Touchpad {
@@ -514,6 +525,7 @@ mod tests {
514525 keyboard {
515526 repeat-delay 600
516527 repeat-rate 25
528+ track-layout "window"
517529 xkb {
518530 layout "us,ru"
519531 options "grp:win_space_toggle"
@@ -587,6 +599,7 @@ mod tests {
587599 },
588600 repeat_delay: 600,
589601 repeat_rate: 25,
602+ track_layout: TrackLayout::Window,
590603 },
591604 touchpad: Touchpad {
592605 tap: true,
src/niri.rsmodified+39 -4
@@ -1,4 +1,4 @@
1-use std::cell::RefCell;
1+use std::cell::{Cell, RefCell};
22 use std::collections::{HashMap, HashSet};
33 use std::ffi::OsString;
44 use std::path::PathBuf;
@@ -29,7 +29,7 @@ use smithay::desktop::utils::{
2929 under_from_surface_tree, update_surface_primary_scanout_output, OutputPresentationFeedback,
3030 };
3131 use smithay::desktop::{layer_map_for_output, PopupManager, Space, Window, WindowSurfaceType};
32-use smithay::input::keyboard::XkbConfig;
32+use smithay::input::keyboard::{Layout as KeyboardLayout, XkbConfig, XkbContextHandler};
3333 use smithay::input::pointer::{CursorIcon, CursorImageAttributes, CursorImageStatus, MotionEvent};
3434 use smithay::input::{Seat, SeatState};
3535 use smithay::output::Output;
@@ -76,7 +76,7 @@ use smithay::wayland::text_input::TextInputManagerState;
7676 use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState;
7777
7878 use crate::backend::{Backend, RenderResult, Tty, Winit};
79-use crate::config::Config;
79+use crate::config::{Config, TrackLayout};
8080 use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor};
8181 #[cfg(feature = "dbus")]
8282 use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};
@@ -382,8 +382,43 @@ impl State {
382382 };
383383
384384 let keyboard = self.niri.seat.get_keyboard().unwrap();
385- if keyboard.current_focus() != focus {
385+ let current_focus = keyboard.current_focus();
386+ if current_focus != focus {
387+ if self.niri.config.borrow().input.keyboard.track_layout == TrackLayout::Window {
388+ let current_layout =
389+ keyboard.with_kkb_state(self, |context| context.active_layout());
390+
391+ let mut new_layout = current_layout;
392+ // Store the currently active layout for the surface.
393+ if let Some(current_focus) = current_focus.as_ref() {
394+ with_states(current_focus, |data| {
395+ let cell = data
396+ .data_map
397+ .get_or_insert::<Cell<KeyboardLayout>, _>(Cell::default);
398+ cell.set(current_layout);
399+ });
400+ }
401+
402+ if let Some(focus) = focus.as_ref() {
403+ new_layout = with_states(focus, |data| {
404+ let cell = data.data_map.get_or_insert::<Cell<KeyboardLayout>, _>(|| {
405+ // The default layout is effectively the first layout in the
406+ // keymap, so use it for new windows.
407+ Cell::new(KeyboardLayout::default())
408+ });
409+ cell.get()
410+ });
411+ }
412+ if new_layout != current_layout && focus.is_some() {
413+ keyboard.set_focus(self, None, SERIAL_COUNTER.next_serial());
414+ keyboard.with_kkb_state(self, |mut context| {
415+ context.set_layout(new_layout);
416+ });
417+ }
418+ }
419+
386420 keyboard.set_focus(self, focus, SERIAL_COUNTER.next_serial());
421+
387422 // FIXME: can be more granular.
388423 self.niri.queue_redraw_all();
389424 }