forge
Explore
Log in

Don't you dare go Hollow

Implement fetching xkb options from org.freedesktop.locale1

Browse files at this commit

0e3d078a85cec867aa3f49b9fe3d1261e36eb901Ivan MolodetskikhParent: 36efd6e
resources/default-config.kdlmodified+3 -0
@@ -15,6 +15,9 @@ input {
1515 // For example:
1616 // layout "us,ru"
1717 // 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.
1821 }
1922
2023 // 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;
33
44 use crate::niri::State;
55
6+pub mod freedesktop_locale1;
67 pub mod freedesktop_screensaver;
78 pub mod gnome_shell_introspect;
89 pub mod gnome_shell_screenshot;
@@ -32,6 +33,7 @@ pub struct DBusServers {
3233 pub conn_introspect: Option<Connection>,
3334 #[cfg(feature = "xdp-gnome-screencast")]
3435 pub conn_screen_cast: Option<Connection>,
36+ pub conn_locale1: Option<Connection>,
3537 }
3638
3739 impl DBusServers {
@@ -125,6 +127,22 @@ impl DBusServers {
125127 }
126128 }
127129
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+
128146 niri.dbus = Some(dbus);
129147 }
130148 }
src/niri.rsmodified+37 -1
@@ -15,7 +15,7 @@ use anyhow::{bail, ensure, Context};
1515 use calloop::futures::Scheduler;
1616 use niri_config::{
1717 Config, FloatOrInt, Key, Modifiers, OutputName, PreviewRender, TrackLayout,
18- WarpMouseToFocusMode, WorkspaceReference,
18+ WarpMouseToFocusMode, WorkspaceReference, Xkb,
1919 };
2020 use smithay::backend::allocator::Fourcc;
2121 use smithay::backend::input::Keycode;
@@ -115,6 +115,8 @@ use crate::backend::tty::SurfaceDmabufFeedback;
115115 use crate::backend::{Backend, Headless, RenderResult, Tty, Winit};
116116 use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor};
117117 #[cfg(feature = "dbus")]
118+use crate::dbus::freedesktop_locale1::Locale1ToNiri;
119+#[cfg(feature = "dbus")]
118120 use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect};
119121 #[cfg(feature = "dbus")]
120122 use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};
@@ -319,6 +321,9 @@ pub struct Niri {
319321 pub is_fdo_idle_inhibited: Arc<AtomicBool>,
320322 pub keyboard_shortcuts_inhibiting_surfaces: HashMap<WlSurface, KeyboardShortcutsInhibitor>,
321323
324+ /// Most recent XKB settings from org.freedesktop.locale1.
325+ pub xkb_from_locale1: Option<Xkb>,
326+
322327 pub cursor_manager: CursorManager,
323328 pub cursor_texture_cache: CursorTextureCache,
324329 pub cursor_shape_manager_state: CursorShapeManagerState,
@@ -1490,6 +1495,12 @@ impl State {
14901495 }
14911496
14921497 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+
14931504 let keyboard = self.niri.seat.get_keyboard().unwrap();
14941505 if let Err(err) = keyboard.set_xkb_config(self, xkb.to_xkb_config()) {
14951506 warn!("error updating xkb config: {err:?}");
@@ -2226,6 +2237,30 @@ impl State {
22262237 warn!("error sending windows to introspect: {err:?}");
22272238 }
22282239 }
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+ }
22292264 }
22302265
22312266 impl Niri {
@@ -2583,6 +2618,7 @@ impl Niri {
25832618 idle_inhibiting_surfaces: HashSet::new(),
25842619 is_fdo_idle_inhibited: Arc::new(AtomicBool::new(false)),
25852620 keyboard_shortcuts_inhibiting_surfaces: HashMap::new(),
2621+ xkb_from_locale1: None,
25862622 cursor_manager,
25872623 cursor_texture_cache: Default::default(),
25882624 cursor_shape_manager_state,
wiki/Configuration:-Input.mdmodified+7 -0
@@ -143,6 +143,13 @@ input {
143143 > }
144144 > ```
145145
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+
146153 When using multiple layouts, niri can remember the current layout globally (the default) or per-window.
147154 You can control this with the `track-layout` option.
148155