query fcitx at start

This commit is contained in:
galister
2025-12-16 12:09:54 +09:00
parent 2b3cdc3c8b
commit 9f5c0b9049
2 changed files with 21 additions and 30 deletions

View File

@@ -1,8 +1,8 @@
use std::{ use std::{
cell::{Cell, LazyCell}, cell::Cell,
collections::HashMap, collections::HashMap,
process::{Child, Command}, process::{Child, Command},
sync::{LazyLock, atomic::Ordering}, sync::atomic::Ordering,
}; };
use crate::{ use crate::{
@@ -67,7 +67,7 @@ pub fn create_keyboard(app: &mut AppState, wayland: bool) -> anyhow::Result<Over
}; };
let mut maybe_keymap = backend let mut maybe_keymap = backend
.get_system_keymap() .get_effective_keymap(app)
.inspect_err(|e| log::warn!("{e:?}")) .inspect_err(|e| log::warn!("{e:?}"))
.ok(); .ok();
@@ -169,54 +169,45 @@ impl KeyboardBackend {
.state = state_from; .state = state_from;
} }
fn get_system_keymap(&mut self) -> anyhow::Result<XkbKeymap> { fn get_effective_keymap(&mut self, app: &mut AppState) -> anyhow::Result<XkbKeymap> {
#[cfg(feature = "wayland")] fn get_system_keymap(wayland: bool) -> anyhow::Result<XkbKeymap> {
if self.wayland { if wayland {
return get_keymap_wl(); get_keymap_wl()
} else {
get_keymap_x11()
}
} }
#[cfg(feature = "x11")]
if !self.wayland {
return get_keymap_x11();
}
unreachable!();
}
fn auto_switch_keymap(&mut self, app: &mut AppState) -> anyhow::Result<bool> {
let Ok(fcitx_layout) = app let Ok(fcitx_layout) = app
.dbus .dbus
.fcitx_keymap() .fcitx_keymap()
.context("Could not keymap via fcitx5, falling back to wayland") .context("Could not keymap via fcitx5, falling back to wayland")
.inspect_err(|e| log::warn!("{e:?}")) .inspect_err(|e| log::warn!("{e:?}"))
else { else {
let keymap = self.get_system_keymap()?; return get_system_keymap(self.wayland);
app.hid_provider.keymap_changed(&keymap);
return self.switch_keymap(&keymap, app);
}; };
if let Some(captures) = self.re_fcitx.captures(&fcitx_layout) { if let Some(captures) = self.re_fcitx.captures(&fcitx_layout) {
let keymap = XkbKeymap::from_layout_variant( XkbKeymap::from_layout_variant(
captures.get(1).map(|g| g.as_str()).unwrap_or(""), captures.get(1).map(|g| g.as_str()).unwrap_or(""),
captures.get(2).map(|g| g.as_str()).unwrap_or(""), captures.get(2).map(|g| g.as_str()).unwrap_or(""),
) )
.context("layout/variant is invalid") .context("layout/variant is invalid")
.inspect_err(|e| log::warn!("fcitx layout {fcitx_layout}: {e:?}"))?;
app.hid_provider.keymap_changed(&keymap);
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()) {
log::debug!("{fcitx_layout} is an IME, switching to system layout."); log::debug!("{fcitx_layout} is an IME, switching to system layout.");
let keymap = self.get_system_keymap()?; get_system_keymap(self.wayland)
app.hid_provider.keymap_changed(&keymap);
self.switch_keymap(&keymap, app)
} else { } else {
log::warn!("Unknown layout or IME '{fcitx_layout}', using system layout"); log::warn!("Unknown layout or IME '{fcitx_layout}', using system layout");
let keymap = self.get_system_keymap()?; get_system_keymap(self.wayland)
app.hid_provider.keymap_changed(&keymap);
return self.switch_keymap(&keymap, app);
} }
} }
fn auto_switch_keymap(&mut self, app: &mut AppState) -> anyhow::Result<bool> {
let keymap = self.get_effective_keymap(app)?;
app.hid_provider.keymap_changed(&keymap);
self.switch_keymap(&keymap, app)
}
fn panel(&mut self) -> &mut GuiPanel<KeyboardState> { fn panel(&mut self) -> &mut GuiPanel<KeyboardState> {
self.layout_panels.get_mut(self.active_layout).unwrap() // want panic self.layout_panels.get_mut(self.active_layout).unwrap() // want panic
} }

View File

@@ -6,7 +6,7 @@ use wlx_common::windowing::{OverlayWindowState, Positioning};
use crate::{ use crate::{
state::{AppSession, AppState, ScreenMeta}, state::{AppSession, AppState, ScreenMeta},
subsystem::{hid::XkbKeymap, input::KeyboardFocus}, subsystem::input::KeyboardFocus,
windowing::{ windowing::{
backend::OverlayBackend, backend::OverlayBackend,
window::{OverlayCategory, OverlayWindowConfig}, window::{OverlayCategory, OverlayWindowConfig},