@@ -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" ) ]
7980impl 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.
99114fn 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
337347fn 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) ?;
0 commit comments