forge
Explore
Log in

Don't you dare go Hollow

add option to set xkb config from file (#1062)

Browse files at this commit

e412a0fc6bc0e04b4238b3d6de4962c2e19ea4ecUnverifiedmay and Ivan MolodetskikhParent: fb5fedb
* add option to set xkb config from file

* Apply suggestions from code review

---------
niri-config/src/lib.rsmodified+2 -0
@@ -136,6 +136,8 @@ pub struct Xkb {
136 pub variant: String,136 pub variant: String,
137 #[knuffel(child, unwrap(argument))]137 #[knuffel(child, unwrap(argument))]
138 pub options: Option<String>,138 pub options: Option<String>,
139+ #[knuffel(child, unwrap(argument))]
140+ pub file: Option<String>,
139 }141 }
140 142
141 impl Xkb {143 impl Xkb {
src/niri.rsmodified+47 -5
@@ -155,7 +155,7 @@ use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRende
155 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};155 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};
156 use crate::utils::spawning::CHILD_ENV;156 use crate::utils::spawning::CHILD_ENV;
157 use crate::utils::{157 use crate::utils::{
158- center, center_f64, get_monotonic_time, ipc_transform_to_smithay, logical_output,158+ center, center_f64, expand_home, get_monotonic_time, ipc_transform_to_smithay, logical_output,
159 make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8,159 make_screenshot_path, output_matches_name, output_size, send_scale_transform, write_png_rgba8,
160 };160 };
161 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef};161 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef};
@@ -588,6 +588,8 @@ impl State {
588 588
589 let mut state = Self { backend, niri };589 let mut state = Self { backend, niri };
590 590
591+ // Load the xkb_file config option if set by the user.
592+ state.load_xkb_file();
591 // Initialize some IPC server state.593 // Initialize some IPC server state.
592 state.ipc_keyboard_layouts_changed();594 state.ipc_keyboard_layouts_changed();
593 595
@@ -1083,6 +1085,31 @@ impl State {
1083 }1085 }
1084 }1086 }
1085 1087
1088+ /// Loads the xkb keymap from a file config setting.
1089+ fn set_xkb_file(&mut self, xkb_file: String) -> anyhow::Result<()> {
1090+ let xkb_file = PathBuf::from(xkb_file);
1091+ let xkb_file = expand_home(&xkb_file)
1092+ .context("failed to expand ~")?
1093+ .unwrap_or(xkb_file);
1094+
1095+ let keymap = std::fs::read_to_string(xkb_file).context("failed to read xkb_file")?;
1096+
1097+ let xkb = self.niri.seat.get_keyboard().unwrap();
1098+ xkb.set_keymap_from_string(self, keymap)
1099+ .context("failed to set keymap")?;
1100+
1101+ Ok(())
1102+ }
1103+
1104+ fn load_xkb_file(&mut self) {
1105+ let xkb_file = self.niri.config.borrow().input.keyboard.xkb.file.clone();
1106+ if let Some(xkb_file) = xkb_file {
1107+ if let Err(err) = self.set_xkb_file(xkb_file) {
1108+ warn!("error loading xkb_file: {err:?}");
1109+ }
1110+ }
1111+ }
1112+
1086 pub fn reload_config(&mut self, path: PathBuf) {1113 pub fn reload_config(&mut self, path: PathBuf) {
1087 let _span = tracy_client::span!("State::reload_config");1114 let _span = tracy_client::span!("State::reload_config");
1088 1115
@@ -1247,10 +1274,25 @@ impl State {
1247 drop(old_config);1274 drop(old_config);
1248 1275
1249 // Now with a &mut self we can reload the xkb config.1276 // Now with a &mut self we can reload the xkb config.
1250- if let Some(xkb) = reload_xkb {1277+ if let Some(mut xkb) = reload_xkb {
1251- let keyboard = self.niri.seat.get_keyboard().unwrap();1278+ let mut set_xkb_config = true;
1252- if let Err(err) = keyboard.set_xkb_config(self, xkb.to_xkb_config()) {1279+
1253- warn!("error updating xkb config: {err:?}");1280+ // It's fine to .take() the xkb file, as this is a
1281+ // clone and the file field is not used in the XkbConfig.
1282+ if let Some(xkb_file) = xkb.file.take() {
1283+ if let Err(err) = self.set_xkb_file(xkb_file) {
1284+ warn!("error reloading xkb_file: {err:?}");
1285+ } else {
1286+ // We successfully set xkb file so we don't need to fallback to XkbConfig.
1287+ set_xkb_config = false;
1288+ }
1289+ }
1290+
1291+ if set_xkb_config {
1292+ let keyboard = self.niri.seat.get_keyboard().unwrap();
1293+ if let Err(err) = keyboard.set_xkb_config(self, xkb.to_xkb_config()) {
1294+ warn!("error updating xkb config: {err:?}");
1295+ }
1254 }1296 }
1255 1297
1256 self.ipc_keyboard_layouts_changed();1298 self.ipc_keyboard_layouts_changed();
wiki/Configuration:-Input.mdmodified+16 -0
@@ -17,6 +17,7 @@ input {
17 // options "compose:ralt,ctrl:nocaps"17 // options "compose:ralt,ctrl:nocaps"
18 // model ""18 // model ""
19 // rules ""19 // rules ""
20+ // file "~/.config/keymap.xkb"
20 }21 }
21 22
22 // repeat-delay 60023 // repeat-delay 600
@@ -112,6 +113,21 @@ input {
112 }113 }
113 ```114 ```
114 115
116+> [!TIP]
117+>
118+> Alternatively you can directly set a path to a .xkb file containing an xkb keymap.
119+> This overrides all other xkb settings
120+>
121+> ```kdl
122+> input {
123+> keyboard {
124+> xkb {
125+> file "~/.config/keymap.xkb"
126+> }
127+> }
128+> }
129+> ```
130+
115 When using multiple layouts, niri can remember the current layout globally (the default) or per-window.131 When using multiple layouts, niri can remember the current layout globally (the default) or per-window.
116 You can control this with the `track-layout` option.132 You can control this with the `track-layout` option.
117 133