Add niri msg keyboard-layouts
Browse files at this commitf19e1711a7999f9b4d97cf1a1b2e03f273b2f120Parent: 20cd4f54 changed files +59 -2
- niri-ipc/src/lib.rs +14 -0
- src/cli.rs +2 -0
- src/ipc/client.rs +24 -1
- src/ipc/server.rs +19 -1
niri-ipc/src/lib.rsmodified+14 -0
| @@ -36,6 +36,8 @@ pub enum Request { | ||
| 36 | 36 | Workspaces, |
| 37 | 37 | /// Request information about the focused output. |
| 38 | 38 | FocusedOutput, |
| 39 | + /// Request information about the keyboard layout. | |
| 40 | + KeyboardLayouts, | |
| 39 | 41 | /// Respond with an error (for testing error handling). |
| 40 | 42 | ReturnError, |
| 41 | 43 | } |
| @@ -70,6 +72,8 @@ pub enum Response { | ||
| 70 | 72 | Workspaces(Vec<Workspace>), |
| 71 | 73 | /// Information about the focused output. |
| 72 | 74 | FocusedOutput(Option<Output>), |
| 75 | + /// Information about the keyboard layout. | |
| 76 | + KeyboardLayouts(KeyboardLayouts), | |
| 73 | 77 | } |
| 74 | 78 | |
| 75 | 79 | /// Actions that niri can perform. |
| @@ -566,6 +570,16 @@ pub struct Workspace { | ||
| 566 | 570 | pub is_active: bool, |
| 567 | 571 | } |
| 568 | 572 | |
| 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 | + | |
| 569 | 583 | impl FromStr for WorkspaceReferenceArg { |
| 570 | 584 | type Err = &'static str; |
| 571 | 585 |
src/cli.rsmodified+2 -0
| @@ -86,6 +86,8 @@ pub enum Msg { | ||
| 86 | 86 | #[command(subcommand)] |
| 87 | 87 | action: OutputAction, |
| 88 | 88 | }, |
| 89 | + /// Get the configured keyboard layouts. | |
| 90 | + KeyboardLayouts, | |
| 89 | 91 | /// Print the version of the running niri instance. |
| 90 | 92 | Version, |
| 91 | 93 | /// Request an error from the running niri instance. |
src/ipc/client.rsmodified+24 -1
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | use anyhow::{anyhow, bail, Context}; |
| 2 | 2 | use niri_ipc::{ |
| 3 | - LogicalOutput, Mode, Output, OutputConfigChanged, Request, Response, Socket, Transform, | |
| 3 | + KeyboardLayouts, LogicalOutput, Mode, Output, OutputConfigChanged, Request, Response, Socket, | |
| 4 | + Transform, | |
| 4 | 5 | }; |
| 5 | 6 | use serde_json::json; |
| 6 | 7 | |
| @@ -19,6 +20,7 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | ||
| 19 | 20 | action: action.clone(), |
| 20 | 21 | }, |
| 21 | 22 | Msg::Workspaces => Request::Workspaces, |
| 23 | + Msg::KeyboardLayouts => Request::KeyboardLayouts, | |
| 22 | 24 | Msg::RequestError => Request::ReturnError, |
| 23 | 25 | }; |
| 24 | 26 | |
| @@ -238,6 +240,27 @@ pub fn handle_msg(msg: Msg, json: bool) -> anyhow::Result<()> { | ||
| 238 | 240 | println!("{is_active}{idx}{name}"); |
| 239 | 241 | } |
| 240 | 242 | } |
| 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 | + } | |
| 241 | 264 | } |
| 242 | 265 | |
| 243 | 266 | Ok(()) |
src/ipc/server.rsmodified+19 -1
| @@ -8,8 +8,9 @@ use calloop::io::Async; | ||
| 8 | 8 | use directories::BaseDirs; |
| 9 | 9 | use futures_util::io::{AsyncReadExt, BufReader}; |
| 10 | 10 | use futures_util::{AsyncBufReadExt, AsyncWriteExt}; |
| 11 | -use niri_ipc::{OutputConfigChanged, Reply, Request, Response}; | |
| 11 | +use niri_ipc::{KeyboardLayouts, OutputConfigChanged, Reply, Request, Response}; | |
| 12 | 12 | use smithay::desktop::Window; |
| 13 | +use smithay::input::keyboard::XkbContextHandler; | |
| 13 | 14 | use smithay::reexports::calloop::generic::Generic; |
| 14 | 15 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 15 | 16 | use smithay::reexports::rustix::fs::unlink; |
| @@ -235,6 +236,23 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply { | ||
| 235 | 236 | let output = result.map_err(|_| String::from("error getting active output info"))?; |
| 236 | 237 | Response::FocusedOutput(output) |
| 237 | 238 | } |
| 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 | + } | |
| 238 | 256 | }; |
| 239 | 257 | |
| 240 | 258 | Ok(response) |