summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs107
1 files changed, 25 insertions, 82 deletions
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<String>)>;
-
-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::<i32>().unwrap();
- let y = args[1].parse::<i32>().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::<i32>().unwrap();
- let h = args[1].parse::<i32>().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 {