diff options
author | Matias Linares <matiaslina@openmailbox.org> | 2015-11-19 21:11:34 -0300 |
---|---|---|
committer | Matias Linares <matiaslina@openmailbox.org> | 2015-11-19 21:11:34 -0300 |
commit | a21473f60c6cfad6335db3b0b45cdff23f8b3a89 (patch) | |
tree | b5e37b1cd9d2146c5930c75aca84c74a9fbcc57d /src/socket.rs | |
parent | 27d0eddd4c1cc275d90938c350a3563428ec5b4f (diff) | |
download | dotwm-a21473f60c6cfad6335db3b0b45cdff23f8b3a89.tar.gz |
Implemented socket handler.
First steps on the socket handling. Dotwm will use this for communication between
different clients trying to modify the behaviour (just take a look into hlwm's
herbstclient program).
It's needed a parser for the configuration, make the protocol and get it working
better. For now I'm checking if it's anything into the socket every time I've a
XEvent. But it shouldn't be this way.
Diffstat (limited to 'src/socket.rs')
-rw-r--r-- | src/socket.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/socket.rs b/src/socket.rs new file mode 100644 index 0000000..b3bb9cc --- /dev/null +++ b/src/socket.rs @@ -0,0 +1,63 @@ +// See LICENSE file for copyright and license details. + +use std::io::{Read,Write}; +use std::sync::mpsc::{Receiver, TryRecvError}; + +use unix_socket::UnixStream; + +use dotwm::DotWM; +use command::*; + +#[allow(unused_variables)] +fn handle_socket(dotwm: &mut DotWM, stream: UnixStream) { + let mut s = stream.try_clone().unwrap(); + let mut buf = String::new(); + s.read_to_string(&mut buf).unwrap(); + println!("buf: {}", buf); + + let _ = write!(s, "ok"); +} + +#[allow(unused_variables)] +/// Handles the configuration and returns true when we're finished. +fn handle_configure(dotwm: &mut DotWM, s: UnixStream) -> bool { + let mut stream = match s.try_clone() { + Ok(s) => s, + Err(_) => { println!("Error"); return true }, + }; + + let mut input = String::new(); + stream.read_to_string(&mut input).unwrap(); + + if input != "done" { + let _ = write!(stream, "ok"); + false + } else { + let _ = write!(stream, "exit"); + true + } +} + +pub fn configure_wm(dotwm: &mut DotWM, rx: &Receiver<UnixStream>) { + exec_cmd("./autostart", &[]).unwrap(); + loop { + match rx.recv() { + Ok(stream) => { + if handle_configure(dotwm, stream) { + break; + } + }, + Err(e) => panic!("Socket: {}", e), + } + } +} + +pub fn listen_socket(dotwm: &mut DotWM, rx: &Receiver<UnixStream>) { + loop { + match rx.try_recv() { + Ok(stream) => handle_socket(dotwm, stream) , + Err(TryRecvError::Empty) => break, + Err(TryRecvError::Disconnected) => panic!("Socket disconnected"), + } + } +} |