aboutsummaryrefslogtreecommitdiff
path: root/src/socket/mod.rs
blob: 66dae1658eb7dc7c5b77356a64d2e3539370aefb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// See LICENSE file for copyright and license details.

pub mod parser;

use std::sync::mpsc::{Receiver, TryRecvError};

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: u32 = 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),
        Err(TryRecvError::Empty) => None,
        Err(TryRecvError::Disconnected) => panic!("Socket disconnected"),
    }
}