@@ -2,7 +2,7 @@ use std::{str::FromStr, time::Duration};
22
33use clap:: Parser ;
44use common:: cache;
5- use common:: ipc:: { self , Answer , Client , IpcSocket , RequestSend } ;
5+ use common:: ipc:: { self , Answer , BgInfo , Client , IpcSocket , RequestSend } ;
66use common:: mmap:: Mmap ;
77
88mod imgproc;
@@ -42,8 +42,9 @@ fn main() -> Result<(), String> {
4242 }
4343 } ;
4444
45- for namespace in namespaces {
46- let socket = IpcSocket :: connect ( & namespace) . map_err ( |err| err. to_string ( ) ) ?;
45+ let mut infos = Vec :: new ( ) ;
46+ for namespace in & namespaces {
47+ let socket = IpcSocket :: connect ( namespace) . map_err ( |err| err. to_string ( ) ) ?;
4748 loop {
4849 RequestSend :: Ping . send ( & socket) ?;
4950 let bytes = socket. recv ( ) . map_err ( |err| err. to_string ( ) ) ?;
@@ -58,22 +59,66 @@ fn main() -> Result<(), String> {
5859 std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
5960 }
6061
61- process_swww_args ( & swww, & namespace) ?;
62+ if let Some ( info) = process_swww_args ( & swww, namespace) ? {
63+ infos. push ( info) ;
64+ }
65+ }
66+
67+ if !infos. is_empty ( ) {
68+ if let Swww :: Query ( query) = swww
69+ && query. json
70+ {
71+ use jzon:: { JsonValue , object, stringify_pretty} ;
72+ let mut buf = String :: new ( ) ;
73+ for ( namespace, infos) in namespaces. iter ( ) . zip ( infos) {
74+ let mut arr = JsonValue :: new_array ( ) ;
75+ for info in infos {
76+ let displaying = match info. img {
77+ ipc:: BgImg :: Color ( color) => {
78+ object ! { color: format!( "#{:x}" , u32 :: from_ne_bytes( color) ) }
79+ }
80+ ipc:: BgImg :: Img ( img) => {
81+ object ! { image: img. as_ref( ) }
82+ }
83+ } ;
84+ _ = arr. push ( object ! {
85+ name: info. name. as_ref( ) ,
86+ width: info. dim. 0 ,
87+ height: info. dim. 1 ,
88+ scale: info. scale_factor. to_f32( ) ,
89+ displaying: displaying
90+ } ) ;
91+ }
92+ buf = format ! ( "{buf}\n \" {namespace}\" : {}," , stringify_pretty( arr, 4 ) ) ;
93+ }
94+ buf. pop ( ) ; // delete trailing comma
95+ println ! ( "{{{buf}\n }}" ) ;
96+ } else {
97+ for ( namespace, infos) in namespaces. iter ( ) . zip ( infos) {
98+ for info in infos {
99+ println ! ( "{namespace}: {info}" ) ;
100+ }
101+ }
102+ }
62103 }
63104 Ok ( ( ) )
64105}
65106
66- fn process_swww_args ( args : & Swww , namespace : & str ) -> Result < ( ) , String > {
107+ fn process_swww_args ( args : & Swww , namespace : & str ) -> Result < Option < Box < [ BgInfo ] > > , String > {
67108 let request = match make_request ( args, namespace) ? {
68109 Some ( request) => request,
69- None => return Ok ( ( ) ) ,
110+ None => return Ok ( None ) ,
70111 } ;
71112 let socket = IpcSocket :: connect ( namespace) . map_err ( |err| err. to_string ( ) ) ?;
72113 request. send ( & socket) ?;
73114 let bytes = socket. recv ( ) . map_err ( |err| err. to_string ( ) ) ?;
74115 drop ( socket) ;
75116 match Answer :: receive ( bytes) {
76- Answer :: Info ( info) => info. iter ( ) . for_each ( |i| println ! ( "{namespace}: {i}" ) ) ,
117+ Answer :: Info ( infos) => {
118+ if let Swww :: Query ( _) = args {
119+ return Ok ( Some ( infos) ) ;
120+ }
121+ }
77122 Answer :: Ok => {
78123 if let Swww :: Kill ( _) = args {
79124 #[ cfg( debug_assertions) ]
@@ -83,7 +128,7 @@ fn process_swww_args(args: &Swww, namespace: &str) -> Result<(), String> {
83128 let path = IpcSocket :: < Client > :: path ( namespace) ;
84129 for _ in 0 ..tries {
85130 if rustix:: fs:: access ( & path, rustix:: fs:: Access :: EXISTS ) . is_err ( ) {
86- return Ok ( ( ) ) ;
131+ return Ok ( None ) ;
87132 }
88133 std:: thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
89134 }
@@ -94,10 +139,10 @@ fn process_swww_args(args: &Swww, namespace: &str) -> Result<(), String> {
94139 }
95140 }
96141 Answer :: Ping ( _) => {
97- return Ok ( ( ) ) ;
142+ return Ok ( None ) ;
98143 }
99144 }
100- Ok ( ( ) )
145+ Ok ( None )
101146}
102147
103148fn make_request ( args : & Swww , namespace : & str ) -> Result < Option < RequestSend > , String > {
@@ -405,5 +450,6 @@ fn restore_output(output: &str, namespace: &str) -> Result<(), String> {
405450 transition_wave : ( 0.0 , 0.0 ) ,
406451 } ) ,
407452 namespace,
408- )
453+ ) ?;
454+ Ok ( ( ) )
409455}
0 commit comments