i3status_rs/blocks/keyboard_layout/
kbdd_bus.rs1use super::*;
2
3pub(super) struct KbddBus {
4 stream: layoutNameChangedStream,
5 info: Info,
6}
7
8impl KbddBus {
9 pub(super) async fn new() -> Result<Self> {
10 let conn = new_dbus_connection().await?;
11 let proxy = KbddBusInterfaceProxy::builder(&conn)
12 .cache_properties(zbus::proxy::CacheProperties::No)
13 .build()
14 .await
15 .error("Failed to create KbddBusInterfaceProxy")?;
16 let stream = proxy
17 .receive_layout_updated()
18 .await
19 .error("Failed to monitor kbdd interface")?;
20 let layout_index = proxy
21 .current_layout_index()
22 .await
23 .error("Failed to get current layout index from kbdd")?;
24 let current_layout = proxy
25 .current_layout(layout_index)
26 .await
27 .error("Failed to get current layout from kbdd")?;
28 let info = Info::from_layout_variant_str(¤t_layout);
29 Ok(Self { stream, info })
30 }
31}
32
33#[async_trait]
34impl Backend for KbddBus {
35 async fn get_info(&mut self) -> Result<Info> {
36 Ok(self.info.clone())
37 }
38
39 async fn wait_for_change(&mut self) -> Result<()> {
40 let event = self
41 .stream
42 .next()
43 .await
44 .error("Failed to receive kbdd event from dbus")?;
45 let args = event
46 .args()
47 .error("Failed to get the args from kbdd message")?;
48 self.info = Info::from_layout_variant_str(args.layout());
49 Ok(())
50 }
51}
52
53#[zbus::proxy(
54 interface = "ru.gentoo.kbdd",
55 default_service = "ru.gentoo.KbddService",
56 default_path = "/ru/gentoo/KbddService"
57)]
58trait KbddBusInterface {
59 #[zbus(signal, name = "layoutNameChanged")]
60 fn layout_updated(&self, layout: String) -> zbus::Result<()>;
61
62 #[zbus(name = "getCurrentLayout")]
63 fn current_layout_index(&self) -> zbus::Result<u32>;
64
65 #[zbus(name = "getLayoutName")]
66 fn current_layout(&self, layout_id: u32) -> zbus::Result<String>;
67}