aboutsummaryrefslogtreecommitdiff
path: root/src/socket/mod.rs
blob: c29db2b70a65209c9d428b3f9343b9850f3dbec8 (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
// 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) {
        if 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"),
    }
}