From 9016c0b85dcfd64991d81bb892615d8fa447c718 Mon Sep 17 00:00:00 2001 From: Davemane42 Date: Mon, 23 Mar 2026 01:17:46 -0400 Subject: [PATCH 1/2] WiFi provider socket --- src/data.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/data.rs b/src/data.rs index faaaa7bb..af21c837 100644 --- a/src/data.rs +++ b/src/data.rs @@ -35,6 +35,7 @@ use std::{env, thread}; static CONN: Mutex> = Mutex::new(None); static MENUCONN: Mutex> = Mutex::new(None); static BLUETOOTHCONN: Mutex> = Mutex::new(None); +static WIFICONN: Mutex> = Mutex::new(None); pub fn input_changed(text: &str) { set_current_prefix(String::new()); @@ -208,6 +209,21 @@ pub fn init_socket() -> Result<(), Box> { subscribe_bluetooth().unwrap(); } + if PROVIDERS.get().unwrap().get("wifi").is_some() { + let wificonn = loop { + match UnixStream::connect(&socket_path) { + Ok(conn) => break conn, + Err(e) => { + println!("Failed to connect to wifi: {e}. Retrying in 1 second..."); + thread::sleep(Duration::from_secs(1)); + } + } + }; + + *WIFICONN.lock().unwrap() = Some(wificonn); + subscribe_wifi().unwrap(); + } + subscribe_menu().unwrap(); start_listening(); @@ -264,6 +280,15 @@ fn start_listening() { } }); } + + if PROVIDERS.get().unwrap().get("wifi").is_some() { + thread::spawn(|| { + if let Err(e) = listen_wifi_loop() { + eprintln!("Listen wifi_loop error: {e}"); + handle_disconnect(); + } + }); + } } fn listen_bluetooth_loop() -> Result<(), Box> { @@ -844,6 +869,81 @@ fn subscribe_bluetooth() -> Result<(), Box> { Ok(()) } +fn subscribe_wifi() -> Result<(), Box> { + let mut req = SubscribeRequest::new(); + req.provider = "wifi".to_string(); + + let mut buffer = vec![2, 0]; + let length = req.compute_size() as u32; + buffer.extend_from_slice(&length.to_be_bytes()); + req.write_to_vec(&mut buffer).unwrap(); + + { + let mut conn_guard = WIFICONN.lock().unwrap(); + + if let Some(conn) = conn_guard.as_mut() { + conn.write_all(&buffer)?; + } else { + return Err("Connection not available".into()); + } + } + + Ok(()) +} + +fn listen_wifi_loop() -> Result<(), Box> { + let mut conn_guard = WIFICONN.lock().unwrap(); + let conn = conn_guard.as_mut().ok_or("Connection not initialized")?; + + let mut conn_clone = conn.try_clone()?; + drop(conn_guard); + + let mut reader = BufReader::new(&mut conn_clone); + + loop { + let mut header = [0u8; 5]; + reader.read_exact(&mut header)?; + + match header[0] { + 0 => { + let length = u32::from_be_bytes(header[1..].try_into().unwrap()); + + let mut payload = vec![0u8; length as usize]; + reader.read_exact(&mut payload)?; + + let mut resp = SubscribeResponse::new(); + resp.merge_from_bytes(&payload)?; + + glib::idle_add_once(move || { + with_window(|w| { + if let Some(p) = &w.placeholder { + match resp.value.as_str() { + "wifi:wifion" => p.set_text("Turning WiFi on..."), + "wifi:wifioff" => p.set_text("Turning WiFi off..."), + "wifi:connect" => p.set_text("Connecting..."), + "wifi:disconnect" => p.set_text("Disconnecting..."), + "wifi:forget" => p.set_text("Forgetting network..."), + "wifi:scan" => p.set_text("Scanning..."), + "wifi:password_required" => { + p.set_text("Password required. Type password after #"); + } + "wifi:connect_failed" => { + p.set_text("Connection failed. Wrong password?"); + } + _ => (), + } + + p.set_visible(true); + w.scroll.set_visible(false); + } + }); + }); + } + _ => continue, + } + } +} + fn wait_for_file(path: &str) { let mut handled = false; From 88e4ecbc65037831e0b6e9e3e3954f1e1a861080 Mon Sep 17 00:00:00 2001 From: Davemane42 Date: Mon, 23 Mar 2026 05:35:53 -0400 Subject: [PATCH 2/2] add wifi:reset socket command --- src/data.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/data.rs b/src/data.rs index af21c837..4dfb163f 100644 --- a/src/data.rs +++ b/src/data.rs @@ -924,12 +924,14 @@ fn listen_wifi_loop() -> Result<(), Box> { "wifi:disconnect" => p.set_text("Disconnecting..."), "wifi:forget" => p.set_text("Forgetting network..."), "wifi:scan" => p.set_text("Scanning..."), - "wifi:password_required" => { - p.set_text("Password required. Type password after #"); - } "wifi:connect_failed" => { p.set_text("Connection failed. Wrong password?"); } + "wifi:reset" => { + p.set_visible(false); + w.scroll.set_visible(true); + return; + } _ => (), }