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