Implement fetching xkb options from org.freedesktop.locale1
Browse files at this commit0e3d078a85cec867aa3f49b9fe3d1261e36eb901Parent: 36efd6e5 changed files +209 -1
- resources/default-config.kdl +3 -0
- src/dbus/freedesktop_locale1.rs +144 -0
- src/dbus/mod.rs +18 -0
- src/niri.rs +37 -1
- wiki/Configuration:-Input.md +7 -0
resources/default-config.kdlmodified+3 -0
| @@ -15,6 +15,9 @@ input { | ||
| 15 | 15 | // For example: |
| 16 | 16 | // layout "us,ru" |
| 17 | 17 | // options "grp:win_space_toggle,compose:ralt,ctrl:nocaps" |
| 18 | + | |
| 19 | + // If this section is empty, niri will fetch xkb settings | |
| 20 | + // from org.freedesktop.locale1. | |
| 18 | 21 | } |
| 19 | 22 | |
| 20 | 23 | // Enable numlock on startup, omitting this setting disables it. |
src/dbus/freedesktop_locale1.rsadded+144 -0
| @@ -0,0 +1,144 @@ | ||
| 1 | +use futures_util::StreamExt; | |
| 2 | +use niri_config::Xkb; | |
| 3 | +use zbus::names::InterfaceName; | |
| 4 | +use zbus::{fdo, zvariant}; | |
| 5 | + | |
| 6 | +pub enum Locale1ToNiri { | |
| 7 | + XkbChanged(Xkb), | |
| 8 | +} | |
| 9 | + | |
| 10 | +pub fn start( | |
| 11 | + to_niri: calloop::channel::Sender<Locale1ToNiri>, | |
| 12 | +) -> anyhow::Result<zbus::blocking::Connection> { | |
| 13 | + let conn = zbus::blocking::Connection::system()?; | |
| 14 | + | |
| 15 | + let async_conn = conn.inner().clone(); | |
| 16 | + let future = async move { | |
| 17 | + let proxy = fdo::PropertiesProxy::new( | |
| 18 | + &async_conn, | |
| 19 | + "org.freedesktop.locale1", | |
| 20 | + "/org/freedesktop/locale1", | |
| 21 | + ) | |
| 22 | + .await; | |
| 23 | + let proxy = match proxy { | |
| 24 | + Ok(x) => x, | |
| 25 | + Err(err) => { | |
| 26 | + warn!("error creating PropertiesProxy: {err:?}"); | |
| 27 | + return; | |
| 28 | + } | |
| 29 | + }; | |
| 30 | + | |
| 31 | + let mut props_changed = match proxy.receive_properties_changed().await { | |
| 32 | + Ok(x) => x, | |
| 33 | + Err(err) => { | |
| 34 | + warn!("error subscribing to PropertiesChanged: {err:?}"); | |
| 35 | + return; | |
| 36 | + } | |
| 37 | + }; | |
| 38 | + | |
| 39 | + let props = proxy | |
| 40 | + .get_all(InterfaceName::try_from("org.freedesktop.locale1").unwrap()) | |
| 41 | + .await; | |
| 42 | + let mut props = match props { | |
| 43 | + Ok(x) => x, | |
| 44 | + Err(err) => { | |
| 45 | + warn!("error receiving initial properties: {err:?}"); | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + }; | |
| 49 | + | |
| 50 | + trace!("initial properties: {props:?}"); | |
| 51 | + | |
| 52 | + let mut get = |name| { | |
| 53 | + props | |
| 54 | + .remove(name) | |
| 55 | + .and_then(|x| String::try_from(x).ok()) | |
| 56 | + .unwrap_or_default() | |
| 57 | + }; | |
| 58 | + | |
| 59 | + let mut xkb = Xkb { | |
| 60 | + rules: String::new(), | |
| 61 | + model: get("X11Model"), | |
| 62 | + layout: get("X11Layout"), | |
| 63 | + variant: get("X11Variant"), | |
| 64 | + options: match get("X11Options") { | |
| 65 | + x if x.is_empty() => None, | |
| 66 | + x => Some(x), | |
| 67 | + }, | |
| 68 | + file: None, | |
| 69 | + }; | |
| 70 | + | |
| 71 | + // Send the initial properties. | |
| 72 | + if let Err(err) = to_niri.send(Locale1ToNiri::XkbChanged(xkb.clone())) { | |
| 73 | + warn!("error sending message to niri: {err:?}"); | |
| 74 | + return; | |
| 75 | + }; | |
| 76 | + | |
| 77 | + while let Some(changed) = props_changed.next().await { | |
| 78 | + let args = match changed.args() { | |
| 79 | + Ok(args) => args, | |
| 80 | + Err(err) => { | |
| 81 | + warn!("error parsing locale1 PropertiesChanged args: {err:?}"); | |
| 82 | + return; | |
| 83 | + } | |
| 84 | + }; | |
| 85 | + | |
| 86 | + let mut changed = false; | |
| 87 | + for (name, value) in args.changed_properties() { | |
| 88 | + trace!("changed property: {name} => {value:?}"); | |
| 89 | + | |
| 90 | + let value = zvariant::Str::try_from(value).unwrap_or_default(); | |
| 91 | + let value = value.as_str(); | |
| 92 | + | |
| 93 | + match *name { | |
| 94 | + "X11Model" => { | |
| 95 | + if xkb.model != value { | |
| 96 | + xkb.model = String::from(value); | |
| 97 | + changed = true; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + "X11Layout" => { | |
| 101 | + if xkb.layout != value { | |
| 102 | + xkb.layout = String::from(value); | |
| 103 | + changed = true; | |
| 104 | + } | |
| 105 | + } | |
| 106 | + "X11Variant" => { | |
| 107 | + if xkb.variant != value { | |
| 108 | + xkb.variant = String::from(value); | |
| 109 | + changed = true; | |
| 110 | + } | |
| 111 | + } | |
| 112 | + "X11Options" => { | |
| 113 | + let value = match value { | |
| 114 | + "" => None, | |
| 115 | + x => Some(x), | |
| 116 | + }; | |
| 117 | + if xkb.options.as_deref() != value { | |
| 118 | + xkb.options = value.map(String::from); | |
| 119 | + changed = true; | |
| 120 | + } | |
| 121 | + } | |
| 122 | + _ => (), | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + if !changed { | |
| 127 | + continue; | |
| 128 | + } | |
| 129 | + | |
| 130 | + if let Err(err) = to_niri.send(Locale1ToNiri::XkbChanged(xkb.clone())) { | |
| 131 | + warn!("error sending message to niri: {err:?}"); | |
| 132 | + return; | |
| 133 | + }; | |
| 134 | + } | |
| 135 | + }; | |
| 136 | + | |
| 137 | + let task = conn | |
| 138 | + .inner() | |
| 139 | + .executor() | |
| 140 | + .spawn(future, "monitor locale1 property changes"); | |
| 141 | + task.detach(); | |
| 142 | + | |
| 143 | + Ok(conn) | |
| 144 | +} |
src/dbus/mod.rsmodified+18 -0
| @@ -3,6 +3,7 @@ use zbus::object_server::Interface; | ||
| 3 | 3 | |
| 4 | 4 | use crate::niri::State; |
| 5 | 5 | |
| 6 | +pub mod freedesktop_locale1; | |
| 6 | 7 | pub mod freedesktop_screensaver; |
| 7 | 8 | pub mod gnome_shell_introspect; |
| 8 | 9 | pub mod gnome_shell_screenshot; |
| @@ -32,6 +33,7 @@ pub struct DBusServers { | ||
| 32 | 33 | pub conn_introspect: Option<Connection>, |
| 33 | 34 | #[cfg(feature = "xdp-gnome-screencast")] |
| 34 | 35 | pub conn_screen_cast: Option<Connection>, |
| 36 | + pub conn_locale1: Option<Connection>, | |
| 35 | 37 | } |
| 36 | 38 | |
| 37 | 39 | impl DBusServers { |
| @@ -125,6 +127,22 @@ impl DBusServers { | ||
| 125 | 127 | } |
| 126 | 128 | } |
| 127 | 129 | |
| 130 | + let (to_niri, from_locale1) = calloop::channel::channel(); | |
| 131 | + niri.event_loop | |
| 132 | + .insert_source(from_locale1, move |event, _, state| match event { | |
| 133 | + calloop::channel::Event::Msg(msg) => state.on_locale1_msg(msg), | |
| 134 | + calloop::channel::Event::Closed => (), | |
| 135 | + }) | |
| 136 | + .unwrap(); | |
| 137 | + match freedesktop_locale1::start(to_niri) { | |
| 138 | + Ok(conn) => { | |
| 139 | + dbus.conn_locale1 = Some(conn); | |
| 140 | + } | |
| 141 | + Err(err) => { | |
| 142 | + warn!("error starting locale1 watcher: {err:?}"); | |
| 143 | + } | |
| 144 | + } | |
| 145 | + | |
| 128 | 146 | niri.dbus = Some(dbus); |
| 129 | 147 | } |
| 130 | 148 | } |
src/niri.rsmodified+37 -1
| @@ -15,7 +15,7 @@ use anyhow::{bail, ensure, Context}; | ||
| 15 | 15 | use calloop::futures::Scheduler; |
| 16 | 16 | use niri_config::{ |
| 17 | 17 | Config, FloatOrInt, Key, Modifiers, OutputName, PreviewRender, TrackLayout, |
| 18 | - WarpMouseToFocusMode, WorkspaceReference, | |
| 18 | + WarpMouseToFocusMode, WorkspaceReference, Xkb, | |
| 19 | 19 | }; |
| 20 | 20 | use smithay::backend::allocator::Fourcc; |
| 21 | 21 | use smithay::backend::input::Keycode; |
| @@ -115,6 +115,8 @@ use crate::backend::tty::SurfaceDmabufFeedback; | ||
| 115 | 115 | use crate::backend::{Backend, Headless, RenderResult, Tty, Winit}; |
| 116 | 116 | use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; |
| 117 | 117 | #[cfg(feature = "dbus")] |
| 118 | +use crate::dbus::freedesktop_locale1::Locale1ToNiri; | |
| 119 | +#[cfg(feature = "dbus")] | |
| 118 | 120 | use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect}; |
| 119 | 121 | #[cfg(feature = "dbus")] |
| 120 | 122 | use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; |
| @@ -319,6 +321,9 @@ pub struct Niri { | ||
| 319 | 321 | pub is_fdo_idle_inhibited: Arc<AtomicBool>, |
| 320 | 322 | pub keyboard_shortcuts_inhibiting_surfaces: HashMap<WlSurface, KeyboardShortcutsInhibitor>, |
| 321 | 323 | |
| 324 | + /// Most recent XKB settings from org.freedesktop.locale1. | |
| 325 | + pub xkb_from_locale1: Option<Xkb>, | |
| 326 | + | |
| 322 | 327 | pub cursor_manager: CursorManager, |
| 323 | 328 | pub cursor_texture_cache: CursorTextureCache, |
| 324 | 329 | pub cursor_shape_manager_state: CursorShapeManagerState, |
| @@ -1490,6 +1495,12 @@ impl State { | ||
| 1490 | 1495 | } |
| 1491 | 1496 | |
| 1492 | 1497 | if set_xkb_config { |
| 1498 | + // If xkb is unset in the niri config, use settings from locale1. | |
| 1499 | + if xkb == Xkb::default() { | |
| 1500 | + trace!("using xkb from locale1"); | |
| 1501 | + xkb = self.niri.xkb_from_locale1.clone().unwrap_or_default(); | |
| 1502 | + } | |
| 1503 | + | |
| 1493 | 1504 | let keyboard = self.niri.seat.get_keyboard().unwrap(); |
| 1494 | 1505 | if let Err(err) = keyboard.set_xkb_config(self, xkb.to_xkb_config()) { |
| 1495 | 1506 | warn!("error updating xkb config: {err:?}"); |
| @@ -2226,6 +2237,30 @@ impl State { | ||
| 2226 | 2237 | warn!("error sending windows to introspect: {err:?}"); |
| 2227 | 2238 | } |
| 2228 | 2239 | } |
| 2240 | + | |
| 2241 | + #[cfg(feature = "dbus")] | |
| 2242 | + pub fn on_locale1_msg(&mut self, msg: Locale1ToNiri) { | |
| 2243 | + let Locale1ToNiri::XkbChanged(xkb) = msg; | |
| 2244 | + | |
| 2245 | + trace!("locale1 xkb settings changed: {xkb:?}"); | |
| 2246 | + let xkb = self.niri.xkb_from_locale1.insert(xkb); | |
| 2247 | + | |
| 2248 | + { | |
| 2249 | + let config = self.niri.config.borrow(); | |
| 2250 | + if config.input.keyboard.xkb != Xkb::default() { | |
| 2251 | + trace!("ignoring locale1 xkb change because niri config has xkb settings"); | |
| 2252 | + return; | |
| 2253 | + } | |
| 2254 | + } | |
| 2255 | + | |
| 2256 | + let xkb = xkb.clone(); | |
| 2257 | + let keyboard = self.niri.seat.get_keyboard().unwrap(); | |
| 2258 | + if let Err(err) = keyboard.set_xkb_config(self, xkb.to_xkb_config()) { | |
| 2259 | + warn!("error updating xkb config: {err:?}"); | |
| 2260 | + } | |
| 2261 | + | |
| 2262 | + self.ipc_keyboard_layouts_changed(); | |
| 2263 | + } | |
| 2229 | 2264 | } |
| 2230 | 2265 | |
| 2231 | 2266 | impl Niri { |
| @@ -2583,6 +2618,7 @@ impl Niri { | ||
| 2583 | 2618 | idle_inhibiting_surfaces: HashSet::new(), |
| 2584 | 2619 | is_fdo_idle_inhibited: Arc::new(AtomicBool::new(false)), |
| 2585 | 2620 | keyboard_shortcuts_inhibiting_surfaces: HashMap::new(), |
| 2621 | + xkb_from_locale1: None, | |
| 2586 | 2622 | cursor_manager, |
| 2587 | 2623 | cursor_texture_cache: Default::default(), |
| 2588 | 2624 | cursor_shape_manager_state, |
wiki/Configuration:-Input.mdmodified+7 -0
| @@ -143,6 +143,13 @@ input { | ||
| 143 | 143 | > } |
| 144 | 144 | > ``` |
| 145 | 145 | |
| 146 | +> [!NOTE] | |
| 147 | +> | |
| 148 | +> <sup>Since: next release</sup> | |
| 149 | +> | |
| 150 | +> If the `xkb` section is empty (like it is by default), niri will fetch xkb settings from systemd-localed at `org.freedesktop.locale1` over D-Bus. | |
| 151 | +> This way, for example, system installers can dynamically set the niri keyboard layout. | |
| 152 | + | |
| 146 | 153 | When using multiple layouts, niri can remember the current layout globally (the default) or per-window. |
| 147 | 154 | You can control this with the `track-layout` option. |
| 148 | 155 |