@@ -2,14 +2,15 @@ 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;
99use imgproc:: * ;
1010
1111mod cli;
1212use cli:: { CliImage , Filter , ResizeStrategy , Swww } ;
13+ use json:: stringify_pretty;
1314
1415fn main ( ) -> Result < ( ) , String > {
1516 let swww = Swww :: parse ( ) ;
@@ -42,8 +43,9 @@ fn main() -> Result<(), String> {
4243 }
4344 } ;
4445
45- for namespace in namespaces {
46- let socket = IpcSocket :: connect ( & namespace) . map_err ( |err| err. to_string ( ) ) ?;
46+ let mut infos = Vec :: new ( ) ;
47+ for namespace in & namespaces {
48+ let socket = IpcSocket :: connect ( namespace) . map_err ( |err| err. to_string ( ) ) ?;
4749 loop {
4850 RequestSend :: Ping . send ( & socket) ?;
4951 let bytes = socket. recv ( ) . map_err ( |err| err. to_string ( ) ) ?;
@@ -58,22 +60,66 @@ fn main() -> Result<(), String> {
5860 std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
5961 }
6062
61- process_swww_args ( & swww, & namespace) ?;
63+ if let Some ( info) = process_swww_args ( & swww, namespace) ? {
64+ infos. push ( info) ;
65+ }
66+ }
67+
68+ if !infos. is_empty ( ) {
69+ if let Swww :: Query ( query) = swww
70+ && query. json
71+ {
72+ use json:: object;
73+ let mut buf = String :: new ( ) ;
74+ for ( namespace, infos) in namespaces. iter ( ) . zip ( infos) {
75+ let mut arr = json:: JsonValue :: new_array ( ) ;
76+ for info in infos {
77+ let displaying = match info. img {
78+ ipc:: BgImg :: Color ( color) => {
79+ object ! { color: format!( "#{:x}" , u32 :: from_ne_bytes( color) ) }
80+ }
81+ ipc:: BgImg :: Img ( img) => {
82+ object ! { image: img. as_ref( ) }
83+ }
84+ } ;
85+ _ = arr. push ( object ! {
86+ name: info. name. as_ref( ) ,
87+ width: info. dim. 0 ,
88+ height: info. dim. 1 ,
89+ scale: info. scale_factor. to_f32( ) ,
90+ displaying: displaying
91+ } ) ;
92+ }
93+ buf = format ! ( "{buf}\n \" {namespace}\" : {}," , stringify_pretty( arr, 4 ) ) ;
94+ }
95+ buf. pop ( ) ; // delete trailing comma
96+ println ! ( "{{{buf}\n }}" ) ;
97+ } else {
98+ for ( namespace, infos) in namespaces. iter ( ) . zip ( infos) {
99+ for info in infos {
100+ println ! ( "{namespace}: {info}" ) ;
101+ }
102+ }
103+ }
62104 }
63105 Ok ( ( ) )
64106}
65107
66- fn process_swww_args ( args : & Swww , namespace : & str ) -> Result < ( ) , String > {
108+ fn process_swww_args ( args : & Swww , namespace : & str ) -> Result < Option < Box < [ BgInfo ] > > , String > {
67109 let request = match make_request ( args, namespace) ? {
68110 Some ( request) => request,
69- None => return Ok ( ( ) ) ,
111+ None => return Ok ( None ) ,
70112 } ;
71113 let socket = IpcSocket :: connect ( namespace) . map_err ( |err| err. to_string ( ) ) ?;
72114 request. send ( & socket) ?;
73115 let bytes = socket. recv ( ) . map_err ( |err| err. to_string ( ) ) ?;
74116 drop ( socket) ;
75117 match Answer :: receive ( bytes) {
76- Answer :: Info ( info) => info. iter ( ) . for_each ( |i| println ! ( "{namespace}: {i}" ) ) ,
118+ Answer :: Info ( infos) => {
119+ if let Swww :: Query ( _) = args {
120+ return Ok ( Some ( infos) ) ;
121+ }
122+ }
77123 Answer :: Ok => {
78124 if let Swww :: Kill ( _) = args {
79125 #[ cfg( debug_assertions) ]
@@ -83,7 +129,7 @@ fn process_swww_args(args: &Swww, namespace: &str) -> Result<(), String> {
83129 let path = IpcSocket :: < Client > :: path ( namespace) ;
84130 for _ in 0 ..tries {
85131 if rustix:: fs:: access ( & path, rustix:: fs:: Access :: EXISTS ) . is_err ( ) {
86- return Ok ( ( ) ) ;
132+ return Ok ( None ) ;
87133 }
88134 std:: thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
89135 }
@@ -94,10 +140,10 @@ fn process_swww_args(args: &Swww, namespace: &str) -> Result<(), String> {
94140 }
95141 }
96142 Answer :: Ping ( _) => {
97- return Ok ( ( ) ) ;
143+ return Ok ( None ) ;
98144 }
99145 }
100- Ok ( ( ) )
146+ Ok ( None )
101147}
102148
103149fn make_request ( args : & Swww , namespace : & str ) -> Result < Option < RequestSend > , String > {
@@ -405,5 +451,6 @@ fn restore_output(output: &str, namespace: &str) -> Result<(), String> {
405451 transition_wave : ( 0.0 , 0.0 ) ,
406452 } ) ,
407453 namespace,
408- )
454+ ) ?;
455+ Ok ( ( ) )
409456}
0 commit comments