summaryrefslogtreecommitdiff
path: root/src/socket.rs
blob: b3bb9cc8ccdb532eb4ff7c06fcbfa5bae762c70e (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// See LICENSE file for copyright and license details.

use std::io::{Read,Write};
use std::sync::mpsc::{Receiver, TryRecvError};

use unix_socket::UnixStream;

use dotwm::DotWM;
use command::*;

#[allow(unused_variables)]
fn handle_socket(dotwm: &mut DotWM, stream: UnixStream) {
    let mut s = stream.try_clone().unwrap();
    let mut buf = String::new();
    s.read_to_string(&mut buf).unwrap();
    println!("buf: {}", buf);

    let _ = write!(s, "ok");
}

#[allow(unused_variables)]
/// Handles the configuration and returns true when we're finished.
fn handle_configure(dotwm: &mut DotWM, s: UnixStream) -> bool {
    let mut stream = match s.try_clone() {
        Ok(s) => s,
        Err(_) => { println!("Error"); return true },
    };
    
    let mut input = String::new();
    stream.read_to_string(&mut input).unwrap();

    if input != "done" {
        let _ = write!(stream, "ok");
        false
    } else {
        let _ = write!(stream, "exit");
        true
    }
}

pub fn configure_wm(dotwm: &mut DotWM, rx: &Receiver<UnixStream>) {
    exec_cmd("./autostart", &[]).unwrap();
    loop {
        match rx.recv() {
            Ok(stream) => {
                if handle_configure(dotwm, stream) {
                    break;
                }
            },
            Err(e) => panic!("Socket: {}", e),
        }
    }
}

pub fn listen_socket(dotwm: &mut DotWM, rx: &Receiver<UnixStream>) {
    loop {
        match rx.try_recv() {
            Ok(stream) => handle_socket(dotwm, stream) ,
            Err(TryRecvError::Empty) => break,
            Err(TryRecvError::Disconnected) => panic!("Socket disconnected"),
        }
    }
}