diff options
Diffstat (limited to 'src/socket/mod.rs')
-rw-r--r-- | src/socket/mod.rs | 49 |
1 files changed, 28 insertions, 21 deletions
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<u32>, + 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<UnixStream>) -> Option<UnixStream> { match rx.try_recv() { Ok(stream) => Some(stream), @@ -17,23 +44,3 @@ pub fn next_socket_event(rx: &Receiver<UnixStream>) -> Option<UnixStream> { 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<UnixStream>) { - 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"), - } - } -} |