From 13310ee7dce177a24252970f607c08f3d658e676 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 22 Nov 2015 04:02:19 -0300 Subject: Add socket functionality. Something simple, but it works pretty well. The mapping between the strings that comes from the socket and functions, modifiers, keys, etc. needs some rewrite because now there's a manual mapping. The autostart file shows some functionality on how it will work. Since the next_xevent is ticking on 1s, it's preferible to make the writes on the socket all together, otherwise will pass 1 sec between two calls. --- src/main.rs | 27 +++++++++++--------- src/safe_x11/event.rs | 2 +- src/socket.rs | 63 ---------------------------------------------- src/socket/mod.rs | 31 +++++++++++++++++++++++ src/socket/parser.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 76 deletions(-) delete mode 100644 src/socket.rs create mode 100644 src/socket/mod.rs create mode 100644 src/socket/parser.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 8bf3caa..a7d8077 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate libc; extern crate x11; extern crate unix_socket; +#[macro_use] +extern crate nom; pub mod safe_x11; pub mod command; @@ -13,7 +15,7 @@ pub mod socket; use dotwm::DotWM; use command::*; use event::{Event,next_event}; -use socket::{configure_wm, listen_socket}; +use socket::listen_socket; use safe_x11::event as x11_event; use std::collections::HashMap; @@ -32,15 +34,14 @@ fn main() { let mut bindings: BindingHash = HashMap::new(); let x11_fd = x11_event::x11_fd(dotwm.display); - add_binding(&mut dotwm,&mut bindings, keysym::XK_Return, xlib::Mod4Mask, exec, - &["xterm"]); - add_binding(&mut dotwm,&mut bindings, keysym::XK_p, xlib::Mod4Mask, exec, - &["dmenu_run"]); - add_binding(&mut dotwm,&mut bindings, keysym::XK_Tab, xlib::Mod4Mask, focus_next, &[]); - add_binding(&mut dotwm,&mut bindings, keysym::XK_h, xlib::Mod4Mask, move_win, &["-10", "0"]); - add_binding(&mut dotwm,&mut bindings, keysym::XK_j, xlib::Mod4Mask, move_win, &["0", "10"]); - add_binding(&mut dotwm,&mut bindings, keysym::XK_k, xlib::Mod4Mask, move_win, &["0", "-10"]); - add_binding(&mut dotwm,&mut bindings, keysym::XK_l, xlib::Mod4Mask, move_win, &["10", "0"]); + //add_binding(&mut dotwm,&mut bindings, keysym::XK_Return, xlib::Mod4Mask, exec, + // &["xterm"]); + //add_binding(&mut dotwm,&mut bindings, keysym::XK_p, xlib::Mod4Mask, exec, + // &["dmenu_run"]); + //add_binding(&mut dotwm,&mut bindings, keysym::XK_h, xlib::Mod4Mask, move_win, &["-10", "0"]); + //add_binding(&mut dotwm,&mut bindings, keysym::XK_j, xlib::Mod4Mask, move_win, &["0", "10"]); + //add_binding(&mut dotwm,&mut bindings, keysym::XK_k, xlib::Mod4Mask, move_win, &["0", "-10"]); + //add_binding(&mut dotwm,&mut bindings, keysym::XK_l, xlib::Mod4Mask, move_win, &["10", "0"]); // Resize add_binding(&mut dotwm,&mut bindings, keysym::XK_h, xlib::Mod4Mask | xlib::ControlMask, @@ -52,6 +53,7 @@ fn main() { add_binding(&mut dotwm,&mut bindings, keysym::XK_l, xlib::Mod4Mask | xlib::ControlMask, resize_win, &["10", "0"]); + add_binding(&mut dotwm,&mut bindings, keysym::XK_Tab, xlib::Mod4Mask, focus_next, &[]); add_binding(&mut dotwm, &mut bindings, keysym::XK_q, xlib::Mod4Mask | xlib::ShiftMask, quit_dotwm, &[]); let (transmission, receiver) = channel(); @@ -67,12 +69,13 @@ fn main() { } } }); - configure_wm(&mut dotwm, &receiver); + + exec_cmd("./autostart", &[]).unwrap(); // Main loop loop { println!("Listening socket"); - listen_socket(&mut dotwm, &receiver); + listen_socket(&mut dotwm, &mut bindings, &receiver); // Event handling. println!("Waiting event"); let event = next_event(dotwm.display, x11_fd); diff --git a/src/safe_x11/event.rs b/src/safe_x11/event.rs index 2519bad..8a4bc32 100644 --- a/src/safe_x11/event.rs +++ b/src/safe_x11/event.rs @@ -26,7 +26,7 @@ pub fn x11_fd(display: *mut Display) -> c_int { pub fn next_xevent(display: *mut Display, fd: c_int) -> XEvent { unsafe { let mut last_event: XEvent = uninitialized(); - let retval = select_next_event(display, fd, &mut last_event) as i32; + select_next_event(display, fd, &mut last_event) as i32; last_event } } diff --git a/src/socket.rs b/src/socket.rs deleted file mode 100644 index b3bb9cc..0000000 --- a/src/socket.rs +++ /dev/null @@ -1,63 +0,0 @@ -// 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) { - 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) { - loop { - match rx.try_recv() { - Ok(stream) => handle_socket(dotwm, stream) , - Err(TryRecvError::Empty) => break, - Err(TryRecvError::Disconnected) => panic!("Socket disconnected"), - } - } -} diff --git a/src/socket/mod.rs b/src/socket/mod.rs new file mode 100644 index 0000000..804f7ac --- /dev/null +++ b/src/socket/mod.rs @@ -0,0 +1,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) { + 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"), + } + } +} diff --git a/src/socket/parser.rs b/src/socket/parser.rs new file mode 100644 index 0000000..3cfc15d --- /dev/null +++ b/src/socket/parser.rs @@ -0,0 +1,70 @@ +// See LICENSE file for copyright and license details. + +//! Parse the socket info. +//! +//! For now this module will have 2 methods. One for inmediate execution and +//! other for add a bindind. + +use x11::xlib; +use x11::keysym; + +use command::*; +use dotwm::DotWM; + +fn str_to_modifier<'a>(s: &'a str) -> Result { + match s { + "Mod1Mask" => Ok(xlib::Mod4Mask), + "Mod2Mask" => Ok(xlib::Mod4Mask), + "Mod3Mask" => Ok(xlib::Mod4Mask), + "Mod4Mask" => Ok(xlib::Mod4Mask), + "ControlMask" => Ok(xlib::ControlMask), + "ShiftMask" => Ok(xlib::ShiftMask), + _ => Err(()), + } +} + +fn str_to_key<'a>(s: &'a str) -> Result { + match s { + "h" => Ok(keysym::XK_h), + "j" => Ok(keysym::XK_j), + "k" => Ok(keysym::XK_k), + "l" => Ok(keysym::XK_l), + "Tab" => Ok(keysym::XK_Tab), + "p" => Ok(keysym::XK_p), + "Return" => Ok(keysym::XK_Return), + "q" => Ok(keysym::XK_q), + _ => Err(()), + } +} + +fn str_to_func<'a>(s: &'a str) -> Result { + match s { + "exec" => Ok(exec), + "move_win" => Ok(move_win), + "resize_win" => Ok(resize_win), + _ => Err(()), + } +} + +pub fn parse<'a>(dotwm: &mut DotWM, bindings: &mut BindingHash, input: &'a str) -> &'a str { + let args: Vec<&str> = input.split_whitespace().collect(); + + match args.first() { + Some(cmd) => { + match cmd { + &"add-binding" => { + let modifier = str_to_modifier(args[1]).unwrap(); + let key = str_to_key(args[2]).unwrap(); + let func = str_to_func(args[3]).unwrap(); + let arguments = &args[4..]; + + add_binding(dotwm, bindings, key, modifier, func, arguments); + }, + &"exec" => { println!("exec"); }, + _ => { println!("anotherthing ._."); }, + } + }, + None => println!("error"), + } + "ok" +} -- cgit v1.2.3-54-g00ecf