From a21473f60c6cfad6335db3b0b45cdff23f8b3a89 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Thu, 19 Nov 2015 21:11:34 -0300 Subject: Implemented socket handler. First steps on the socket handling. Dotwm will use this for communication between different clients trying to modify the behaviour (just take a look into hlwm's herbstclient program). It's needed a parser for the configuration, make the protocol and get it working better. For now I'm checking if it's anything into the socket every time I've a XEvent. But it shouldn't be this way. --- src/main.rs | 107 ++++++++++++++---------------------------------------------- 1 file changed, 25 insertions(+), 82 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index f00523d..e652dca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,103 +2,28 @@ extern crate libc; extern crate x11; +extern crate unix_socket; pub mod safe_x11; pub mod command; pub mod dotwm; pub mod event; +pub mod socket; use dotwm::DotWM; -use command::{exec_cmd,collect_zombies}; +use command::*; use event::{Event,next_event}; +use socket::{configure_wm, listen_socket}; -use safe_x11::grab_key; - -use std::ops::Deref; -use std::process; use std::collections::HashMap; use x11::xlib; -use x11::xlib::GrabModeAsync; use x11::xlib::XEvent; use x11::keysym; -/// Defines a callback to call no certains events. -pub type ExecFn = fn(&mut DotWM, XEvent, &[String]) -> bool; - -/// Map for keys => functions -pub type BindingHash = HashMap<(u32, u32), (ExecFn, Vec)>; - -fn exec_func(wm: &mut DotWM, bindings: &mut BindingHash, key: u32, modifiers: u32, ev: xlib::XEvent) { - if let Some(&(func, ref args)) = bindings.get(&(key, modifiers)) { - let v = args.clone(); - func(wm, ev, &v); - } -} - -fn exec(_: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { - if let Some(program) = args.first() { - let mut prog_args = vec![]; - for arg in args[1..].iter() { - prog_args.push(arg); - } - exec_cmd(program, prog_args.deref()).unwrap(); - } - true -} - -fn move_win(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { - let x = args[0].parse::().unwrap(); - let y = args[1].parse::().unwrap(); - if let Some(ref win) = wm.current_window() { - win.move_offset(x, y); - }; - true -} - -fn resize_win(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { - let w = args[0].parse::().unwrap(); - let h = args[1].parse::().unwrap(); - - if let Some(ref win) = wm.current_window() { - win.resize(w, h); - }; - - true -} - -fn focus_next(wm: &mut DotWM, _: xlib::XEvent, _: &[String]) -> bool { - wm.focus_next(); - true -} - -fn quit_dotwm(_: &mut DotWM, _: xlib::XEvent, _: &[String]) -> bool { - process::exit(0); -} - -/// Add a binding to the WM. -/// -/// # Example -/// -/// ``` -/// fn quit_dotwm(_: &mut DotWM, _: xlib::XEvent, _: &[String]) -> bool { -/// process::exit(0); -/// } -/// -/// // ... -/// -/// add_binding(keysym::XK_Return, xlib::Mod4Mask, exec, -/// &["xterm"]); -/// ``` -fn add_binding(wm: &mut DotWM, bindings: &mut BindingHash, - key: u32, modifiers: u32, func: ExecFn, args: &[&str]) { - grab_key(wm.display, key, modifiers, true, GrabModeAsync, GrabModeAsync); - let mut v = vec![]; - for arg in args { - v.push(arg.to_string()); - } - bindings.insert((key, modifiers), (func, v)); -} +use std::thread; +use std::sync::mpsc::channel; +use unix_socket::UnixListener; fn main() { println!("Creating dotwm"); @@ -127,8 +52,26 @@ fn main() { add_binding(&mut dotwm, &mut bindings, keysym::XK_q, xlib::Mod4Mask | xlib::ShiftMask, quit_dotwm, &[]); + let (transmission, receiver) = channel(); + + thread::spawn(move || { + println!("Creating socket"); + let listener = UnixListener::bind("./dotwm.sock").unwrap(); + + for stream in listener.incoming() { + match stream { + Ok(stream) => transmission.send(stream).unwrap() , + Err(e) => println!("{}", e), + } + } + }); + + configure_wm(&mut dotwm, &receiver); + // Main loop loop { + listen_socket(&mut dotwm, &receiver); + // Event handling. let event = next_event(dotwm.display); println!("Event {:?}", event); match event { -- cgit v1.2.3-54-g00ecf