aboutsummaryrefslogtreecommitdiff
path: root/src/socket
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2015-11-22 04:02:19 -0300
committerMatias Linares <matiaslina@openmailbox.org>2015-11-22 04:02:19 -0300
commit13310ee7dce177a24252970f607c08f3d658e676 (patch)
tree225d8c3fff3f033cdd2584e408b19aade31affba /src/socket
parent5da38341bc54f24c8cad41f1b63405d8cc955fe7 (diff)
downloaddotwm-13310ee7dce177a24252970f607c08f3d658e676.tar.gz
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.
Diffstat (limited to 'src/socket')
-rw-r--r--src/socket/mod.rs31
-rw-r--r--src/socket/parser.rs70
2 files changed, 101 insertions, 0 deletions
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<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"),
+ }
+ }
+}
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<u32, ()> {
+ 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<u32, ()> {
+ 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<ExecFn,()> {
+ 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"
+}