Skip to content

Commit 6aa017f

Browse files
t3dotggclaude
andcommitted
fix(native): KDE SnapShot no longer loses the first reply from KWin
Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent f313d0a commit 6aa017f

2 files changed

Lines changed: 28 additions & 22 deletions

File tree

‎native/kde-snap-shot/src/main.rs‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,42 +71,54 @@ struct Bounds {
7171
height: u32,
7272
}
7373

74-
struct ScriptReply {
75-
owner: String,
76-
result: mpsc::SyncSender<String>,
77-
}
74+
const REPLY: &str = "/com/t3tools/KdeCapture";
75+
76+
/// KWin's bus name and the channel of the script call in flight. `None` between calls.
77+
#[derive(Default)]
78+
struct ScriptReply(Option<(String, mpsc::SyncSender<String>)>);
7879
#[zbus::interface(name = "com.t3tools.KdeCapture")]
7980
impl ScriptReply {
8081
fn reply(
8182
&self,
8283
value: &str,
8384
#[zbus(header)] header: zbus::message::Header<'_>,
8485
) -> zbus::fdo::Result<()> {
85-
if header.sender().map(|name| name.as_str()) != Some(self.owner.as_str()) {
86+
let Some((owner, result)) = &self.0 else {
87+
return Err(zbus::fdo::Error::AccessDenied(
88+
"No script is running".into(),
89+
));
90+
};
91+
if header.sender().map(|name| name.as_str()) != Some(owner.as_str()) {
8692
return Err(zbus::fdo::Error::AccessDenied("Not KWin".into()));
8793
}
8894
if value.len() > 64 * 1024 {
8995
return Err(zbus::fdo::Error::InvalidArgs(
9096
"Oversized window metadata".into(),
9197
));
9298
}
93-
let _ = self.result.try_send(value.to_owned());
99+
let _ = result.try_send(value.to_owned());
94100
Ok(())
95101
}
96102
}
97103

104+
/// Serves the reply object from the start, so zbus waits for its method-call listener.
105+
/// A listener started after connecting can miss the first call from KWin.
106+
fn connect(builder: Builder<'_>) -> zbus::Result<Connection> {
107+
builder
108+
.method_timeout(DEADLINE)
109+
.serve_at(REPLY, ScriptReply::default())?
110+
.build()
111+
}
112+
98113
/// Scripts exist only for this operation. Unload even when evaluation/callback fails.
99114
fn script(connection: &Connection, directory: &Path, body: &str) -> Result<String> {
100115
let dbus = zbus::blocking::fdo::DBusProxy::new(connection)?;
101116
let owner = dbus.get_name_owner("org.kde.KWin".try_into()?)?.to_string();
102117
let (send, receive) = mpsc::sync_channel(1);
103-
connection.object_server().at(
104-
"/com/t3tools/KdeCapture",
105-
ScriptReply {
106-
owner,
107-
result: send,
108-
},
109-
)?;
118+
let reply = connection
119+
.object_server()
120+
.interface::<_, ScriptReply>(REPLY)?;
121+
reply.get_mut().0 = Some((owner, send));
110122
let path = directory.join("window.js");
111123
let name = format!("t3-capture-{}", std::process::id());
112124
let destination = serde_json::to_string(
@@ -147,9 +159,7 @@ fn script(connection: &Connection, directory: &Path, body: &str) -> Result<Strin
147159
let _: std::result::Result<bool, _> = scripting.call("unloadScript", &(&name,));
148160
result
149161
})();
150-
connection
151-
.object_server()
152-
.remove::<ScriptReply, _>("/com/t3tools/KdeCapture")?;
162+
reply.get_mut().0 = None;
153163
let _ = std::fs::remove_file(path);
154164
result
155165
}
@@ -336,7 +346,7 @@ fn capture(connection: &Connection, directory: &Path) -> Result<Option<Window>>
336346

337347
fn run() -> Result<()> {
338348
let args: Vec<String> = std::env::args().collect();
339-
let connection = Builder::session()?.method_timeout(DEADLINE).build()?;
349+
let connection = connect(Builder::session()?)?;
340350
match args.get(1).map(String::as_str) {
341351
Some("check") => {
342352
check(&connection)?;

‎native/kde-snap-shot/src/transport_tests.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,7 @@ impl Fixture {
183183
.unwrap()
184184
.build()
185185
.unwrap();
186-
let client = Builder::address(address.trim())
187-
.unwrap()
188-
.method_timeout(DEADLINE)
189-
.build()
190-
.unwrap();
186+
let client = connect(Builder::address(address.trim()).unwrap()).unwrap();
191187
Self {
192188
daemon,
193189
directory,

0 commit comments

Comments
 (0)