forge
Explore
Log in

Don't you dare go Hollow

Add niri msg keyboard-layouts

Browse files at this commit

f19e1711a7999f9b4d97cf1a1b2e03f273b2f120Ivan MolodetskikhParent: 20cd4f5
niri-ipc/src/lib.rsmodified+14 -0
@@ -36,6 +36,8 @@ pub enum Request {
3636 Workspaces,
3737 /// Request information about the focused output.
3838 FocusedOutput,
39+ /// Request information about the keyboard layout.
40+ KeyboardLayouts,
3941 /// Respond with an error (for testing error handling).
4042 ReturnError,
4143 }
@@ -70,6 +72,8 @@ pub enum Response {
7072 Workspaces(Vec<Workspace>),
7173 /// Information about the focused output.
7274 FocusedOutput(Option<Output>),
75+ /// Information about the keyboard layout.
76+ KeyboardLayouts(KeyboardLayouts),
7377 }
7478
7579 /// Actions that niri can perform.
@@ -566,6 +570,16 @@ pub struct Workspace {
566570 pub is_active: bool,
567571 }
568572
573+/// Configured keyboard layouts.
574+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
575+#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
576+pub struct KeyboardLayouts {
577+ /// XKB names of the configured layouts.
578+ pub names: Vec<String>,
579+ /// Index of the currently active layout in `names`.
580+ pub current_idx: u8,
581+}
582+
569583 impl FromStr for WorkspaceReferenceArg {
570584 type Err = &'static str;
571585
src/cli.rsmodified+2 -0
@@ -86,6 +86,8 @@ pub enum Msg {
8686 #[command(subcommand)]
8787 action: OutputAction,
8888 },
89+ /// Get the configured keyboard layouts.
90+ KeyboardLayouts,
8991 /// Print the version of the running niri instance.
9092 Version,
9193 /// Request an error from the running niri instance.
src/ipc/client.rsmodified+24 -1
@@ -1,6 +1,7 @@
11 use anyhow::{anyhow, bail, Context};
22 use niri_ipc::{
3- LogicalOutput, Mode, Output, OutputConfigChanged, Request, Response, Socket, Transform,
3+ KeyboardLayouts, LogicalOutput, Mode, Output, OutputConfigChanged, Request, Response, Socket,
4+ Transform,
45 };
56 use serde_json::json;
67
@@ -19,6 +20,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
1920 action: action.clone(),
2021 },
2122 Msg::Workspaces => Request::Workspaces,
23+ Msg::KeyboardLayouts => Request::KeyboardLayouts,
2224 Msg::RequestError => Request::ReturnError,
2325 };
2426
@@ -238,6 +240,27 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> {
238240 println!("{is_active}{idx}{name}");
239241 }
240242 }
243+ Msg::KeyboardLayouts => {
244+ let Response::KeyboardLayouts(response) = response else {
245+ bail!("unexpected response: expected KeyboardLayouts, got {response:?}");
246+ };
247+
248+ if json {
249+ let response =
250+ serde_json::to_string(&response).context("error formatting response")?;
251+ println!("{response}");
252+ return Ok(());
253+ }
254+
255+ let KeyboardLayouts { names, current_idx } = response;
256+ let current_idx = usize::from(current_idx);
257+
258+ println!("Keyboard layouts:");
259+ for (idx, name) in names.iter().enumerate() {
260+ let is_active = if idx == current_idx { " * " } else { " " };
261+ println!("{is_active}{idx} {name}");
262+ }
263+ }
241264 }
242265
243266 Ok(())
src/ipc/server.rsmodified+19 -1
@@ -8,8 +8,9 @@ use calloop::io::Async;
88 use directories::BaseDirs;
99 use futures_util::io::{AsyncReadExt, BufReader};
1010 use futures_util::{AsyncBufReadExt, AsyncWriteExt};
11-use niri_ipc::{OutputConfigChanged, Reply, Request, Response};
11+use niri_ipc::{KeyboardLayouts, OutputConfigChanged, Reply, Request, Response};
1212 use smithay::desktop::Window;
13+use smithay::input::keyboard::XkbContextHandler;
1314 use smithay::reexports::calloop::generic::Generic;
1415 use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction};
1516 use smithay::reexports::rustix::fs::unlink;
@@ -235,6 +236,23 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply {
235236 let output = result.map_err(|_| String::from("error getting active output info"))?;
236237 Response::FocusedOutput(output)
237238 }
239+ Request::KeyboardLayouts => {
240+ let (tx, rx) = async_channel::bounded(1);
241+ ctx.event_loop.insert_idle(move |state| {
242+ let keyboard = state.niri.seat.get_keyboard().unwrap();
243+ let layout = keyboard.with_xkb_state(state, |context| {
244+ let layouts = context.keymap().layouts();
245+ KeyboardLayouts {
246+ names: layouts.map(str::to_owned).collect(),
247+ current_idx: context.active_layout().0 as u8,
248+ }
249+ });
250+ let _ = tx.send_blocking(layout);
251+ });
252+ let result = rx.recv().await;
253+ let layout = result.map_err(|_| String::from("error getting layout info"))?;
254+ Response::KeyboardLayouts(layout)
255+ }
238256 };
239257
240258 Ok(response)