fcitx: capture variant by regex

This commit is contained in:
galister
2025-12-16 11:21:56 +09:00
parent c19c9da5f0
commit 2b3cdc3c8b
2 changed files with 23 additions and 9 deletions

View File

@@ -1,8 +1,8 @@
use std::{ use std::{
cell::Cell, cell::{Cell, LazyCell},
collections::HashMap, collections::HashMap,
process::{Child, Command}, process::{Child, Command},
sync::atomic::Ordering, sync::{LazyLock, atomic::Ordering},
}; };
use crate::{ use crate::{
@@ -22,6 +22,7 @@ use crate::{
}; };
use anyhow::Context; use anyhow::Context;
use glam::{Affine3A, Quat, Vec3, vec3}; use glam::{Affine3A, Quat, Vec3, vec3};
use regex::Regex;
use slotmap::{SlotMap, new_key_type}; use slotmap::{SlotMap, new_key_type};
use wgui::{ use wgui::{
drawing, drawing,
@@ -62,6 +63,7 @@ pub fn create_keyboard(app: &mut AppState, wayland: bool) -> anyhow::Result<Over
default_state, default_state,
wlx_layout: layout, wlx_layout: layout,
wayland, wayland,
re_fcitx: Regex::new(r"^keyboard-([^-]+)(?:-([^-]+))?$").unwrap(),
}; };
let mut maybe_keymap = backend let mut maybe_keymap = backend
@@ -108,6 +110,7 @@ struct KeyboardBackend {
default_state: KeyboardState, default_state: KeyboardState,
wlx_layout: layout::Layout, wlx_layout: layout::Layout,
wayland: bool, wayland: bool,
re_fcitx: Regex,
} }
impl KeyboardBackend { impl KeyboardBackend {
@@ -192,10 +195,13 @@ impl KeyboardBackend {
return self.switch_keymap(&keymap, app); return self.switch_keymap(&keymap, app);
}; };
if fcitx_layout.starts_with("keyboard-") { if let Some(captures) = self.re_fcitx.captures(&fcitx_layout) {
let keymap = XkbKeymap::from_layout_str(&fcitx_layout[9..]) let keymap = XkbKeymap::from_layout_variant(
.context("layout is invalid") captures.get(1).map(|g| g.as_str()).unwrap_or(""),
.inspect_err(|e| log::warn!("fcitx layout {fcitx_layout}: {e:?}"))?; captures.get(2).map(|g| g.as_str()).unwrap_or(""),
)
.context("layout/variant is invalid")
.inspect_err(|e| log::warn!("fcitx layout {fcitx_layout}: {e:?}"))?;
app.hid_provider.keymap_changed(&keymap); app.hid_provider.keymap_changed(&keymap);
self.switch_keymap(&keymap, app) self.switch_keymap(&keymap, app)
} else if SYSTEM_LAYOUT_ALIASES.contains(&fcitx_layout.as_str()) { } else if SYSTEM_LAYOUT_ALIASES.contains(&fcitx_layout.as_str()) {

View File

@@ -578,10 +578,18 @@ pub struct XkbKeymap {
} }
impl XkbKeymap { impl XkbKeymap {
pub fn from_layout_str(layout: &str) -> Option<Self> { pub fn from_layout_variant(layout: &str, variant: &str) -> Option<Self> {
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS); let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
xkb::Keymap::new_from_names(&context, "", "", layout, "", None, xkb::COMPILE_NO_FLAGS) xkb::Keymap::new_from_names(
.map(|inner| XkbKeymap { inner }) &context,
"",
"",
layout,
variant,
None,
xkb::COMPILE_NO_FLAGS,
)
.map(|inner| XkbKeymap { inner })
} }
pub fn get_name(&self) -> Option<&str> { pub fn get_name(&self) -> Option<&str> {