From 13310ee7dce177a24252970f607c08f3d658e676 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 22 Nov 2015 04:02:19 -0300 Subject: Add socket functionality. Something simple, but it works pretty well. The mapping between the strings that comes from the socket and functions, modifiers, keys, etc. needs some rewrite because now there's a manual mapping. The autostart file shows some functionality on how it will work. Since the next_xevent is ticking on 1s, it's preferible to make the writes on the socket all together, otherwise will pass 1 sec between two calls. --- src/socket/mod.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/socket/mod.rs (limited to 'src/socket/mod.rs') diff --git a/src/socket/mod.rs b/src/socket/mod.rs new file mode 100644 index 0000000..804f7ac --- /dev/null +++ b/src/socket/mod.rs @@ -0,0 +1,31 @@ +// See LICENSE file for copyright and license details. + +pub mod parser; + +use std::io::{Read,Write}; +use std::sync::mpsc::{Receiver, TryRecvError}; + +use unix_socket::UnixStream; + +use dotwm::DotWM; +use command::*; + +/// Listen a socket parsing and executing all the commands. +pub fn listen_socket(dotwm: &mut DotWM, bindings: &mut BindingHash, rx: &Receiver) { + loop { + match rx.try_recv() { + Ok(stream) => { + let mut s = stream.try_clone().unwrap(); + let mut buf = String::new(); + s.read_to_string(&mut buf).unwrap(); + + for line in buf.lines() { + let result = parser::parse(dotwm, bindings, &line); + let _ = write!(s, "{}", result); + } + }, + Err(TryRecvError::Empty) => break, + Err(TryRecvError::Disconnected) => panic!("Socket disconnected"), + } + } +} -- cgit v1.2.3-70-g09d2