From e1ec354306742a5804a20e2651cf49945cd17287 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sat, 28 Nov 2015 22:16:43 -0300 Subject: Better socket handling The socket interface now allows almost everything that vould be done by the internals of the window manager. Also the Window manager now closes fine (on a success exit). --- src/socket/mod.rs | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'src/socket/mod.rs') diff --git a/src/socket/mod.rs b/src/socket/mod.rs index e260284..667969b 100644 --- a/src/socket/mod.rs +++ b/src/socket/mod.rs @@ -2,7 +2,6 @@ pub mod parser; -use std::io::{Read,Write}; use std::sync::mpsc::{Receiver, TryRecvError}; use unix_socket::UnixStream; @@ -10,6 +9,34 @@ use unix_socket::UnixStream; use dotwm::DotWM; use command::*; +#[derive(Debug,PartialEq)] +pub enum FnType { + Bind, + Exec, +} + +pub struct ParsedCmd<'a> { + pub f: FnType, + pub modifiers: Vec, + pub key: u32, + pub args: Vec<&'a str>, + pub func: ExecFn, +} + +impl<'a> ParsedCmd<'a> { + pub fn handle(self, wm: &mut DotWM, bindings: &mut BindingHash) { + match self.f { + FnType::Bind => { + let modifier = self.modifiers.iter() + .fold(0, |acc, x| acc | x ); + add_binding(wm, bindings, + self.key, modifier, self.func, &self.args); + }, + _ => (), + } + } +} + pub fn next_socket_event(rx: &Receiver) -> Option { match rx.try_recv() { Ok(stream) => Some(stream), @@ -17,23 +44,3 @@ pub fn next_socket_event(rx: &Receiver) -> Option { Err(TryRecvError::Disconnected) => panic!("Socket disconnected"), } } - -/// 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-54-g00ecf