Open Unix socket for handling clients

timeout_old
Tassilo Horn 4 years ago
parent 25eaa19998
commit 94cb31baa5
  1. 2
      src/bin/swayrd.rs
  2. 36
      src/demon.rs
  3. 4
      src/ipc.rs

@ -16,6 +16,8 @@ fn main() {
let subscriber_handle =
thread::spawn(move || demon::monitor_window_events(win_props_for_ev_handler));
demon::serve_client_requests(win_props);
let subscriber_result = subscriber_handle.join();
match subscriber_result {
Ok(()) => println!("Subscriber thread shut down cleanly."),

@ -1,9 +1,12 @@
use crate::ipc;
use serde_json::Deserializer;
use std::collections::HashMap;
use std::io::Write;
use std::os::unix::net::{UnixListener, UnixStream};
use std::process as proc;
use std::sync::Arc;
use std::sync::RwLock;
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn monitor_window_events(win_props: Arc<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>) {
@ -57,3 +60,36 @@ fn get_epoch_time_as_millis() -> u128 {
.expect("Couldn't get epoch time!")
.as_millis()
}
pub fn serve_client_requests(
win_props: Arc<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>,
) -> std::io::Result<()> {
// FIXME: Use sensible path.
let listener = UnixListener::bind("/home/horn/tmp/SWAYR_SOCKET")?;
for stream in listener.incoming() {
match stream {
Ok(stream) => {
/* connection succeeded */
let wp_clone = win_props.clone();
thread::spawn(move || handle_client_request(stream, wp_clone));
}
Err(err) => {
/* connection failed */
eprintln!("Could not accept client request: {:?}", err);
break;
}
}
}
Ok(())
}
fn handle_client_request(
mut stream: UnixStream,
win_props: Arc<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>,
) {
let json = serde_json::to_string(&*win_props.read().unwrap()).unwrap();
if let Err(err) = stream.write_all(json.as_bytes()) {
eprintln!("Error writing to client: {:?}", err);
}
}

@ -1,7 +1,7 @@
extern crate serde;
extern crate serde_json;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::process as proc;
@ -215,7 +215,7 @@ pub struct WindowEvent {
pub container: Node,
}
#[derive(Debug)]
#[derive(Debug, Deserialize, Serialize)]
pub struct WindowProps {
/// Milliseconds since UNIX epoch.
pub last_focus_time: u128,

Loading…
Cancel
Save