1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
use futures_util::StreamExt;
use niri_config::Xkb;
use zbus::names::InterfaceName;
use zbus::{fdo, zvariant};
pub enum Locale1ToNiri {
XkbChanged(Xkb),
}
pub fn start(
to_niri: calloop::channel::Sender<Locale1ToNiri>,
) -> anyhow::Result<zbus::blocking::Connection> {
let conn = zbus::blocking::Connection::system()?;
let async_conn = conn.inner().clone();
let future = async move {
let proxy = fdo::PropertiesProxy::new(
&async_conn,
"org.freedesktop.locale1",
"/org/freedesktop/locale1",
)
.await;
let proxy = match proxy {
Ok(x) => x,
Err(err) => {
warn!("error creating PropertiesProxy: {err:?}");
return;
}
};
let mut props_changed = match proxy.receive_properties_changed().await {
Ok(x) => x,
Err(err) => {
warn!("error subscribing to PropertiesChanged: {err:?}");
return;
}
};
let props = proxy
.get_all(InterfaceName::try_from("org.freedesktop.locale1").unwrap())
.await;
let mut props = match props {
Ok(x) => x,
Err(err) => {
warn!("error receiving initial properties: {err:?}");
return;
}
};
trace!("initial properties: {props:?}");
let mut get = |name| {
props
.remove(name)
.and_then(|x| String::try_from(x).ok())
.unwrap_or_default()
};
let mut xkb = Xkb {
rules: String::new(),
model: get("X11Model"),
layout: get("X11Layout"),
variant: get("X11Variant"),
options: match get("X11Options") {
x if x.is_empty() => None,
x => Some(x),
},
file: None,
};
// Send the initial properties.
if let Err(err) = to_niri.send(Locale1ToNiri::XkbChanged(xkb.clone())) {
warn!("error sending message to niri: {err:?}");
return;
};
while let Some(changed) = props_changed.next().await {
let args = match changed.args() {
Ok(args) => args,
Err(err) => {
warn!("error parsing locale1 PropertiesChanged args: {err:?}");
return;
}
};
let mut changed = false;
for (name, value) in args.changed_properties() {
trace!("changed property: {name} => {value:?}");
let value = zvariant::Str::try_from(value).unwrap_or_default();
let value = value.as_str();
match *name {
"X11Model" => {
if xkb.model != value {
xkb.model = String::from(value);
changed = true;
}
}
"X11Layout" => {
if xkb.layout != value {
xkb.layout = String::from(value);
changed = true;
}
}
"X11Variant" => {
if xkb.variant != value {
xkb.variant = String::from(value);
changed = true;
}
}
"X11Options" => {
let value = match value {
"" => None,
x => Some(x),
};
if xkb.options.as_deref() != value {
xkb.options = value.map(String::from);
changed = true;
}
}
_ => (),
}
}
if !changed {
continue;
}
if let Err(err) = to_niri.send(Locale1ToNiri::XkbChanged(xkb.clone())) {
warn!("error sending message to niri: {err:?}");
return;
};
}
};
let task = conn
.inner()
.executor()
.spawn(future, "monitor locale1 property changes");
task.detach();
Ok(conn)
}
|