From 5bcdb566c69523edb1ceb857cbf6d0a676cc318d Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 4 Sep 2016 14:01:37 -0300 Subject: Add Mouse drag support. This concludes the first 'release' :p. --- src/command.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/command.rs') diff --git a/src/command.rs b/src/command.rs index f5e3e83..eaadf77 100644 --- a/src/command.rs +++ b/src/command.rs @@ -17,7 +17,7 @@ use x11::xlib::{XEvent, GrabModeAsync}; use dotwm::DotWM; use dotwm::NetAtom; -use safe_x11::{grab_key,ungrab_key}; +use safe_x11::{grab_key,ungrab_key,grab_button,ungrab_button}; const WNOHANG: c_int = 0x00000001; @@ -132,6 +132,25 @@ pub fn move_win_sticky(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool true } +/// Drag the window. for now the dragging is relative to the center of the +/// window because win.cursor_coords is always `None`. Later we should +/// be able to `XGrabButton` on the child windows (not only on the root) and +/// set `win.cursor_coords` respectively. +pub fn move_win_drag(wm: &mut DotWM, e: xlib::XEvent, _: &[String]) -> bool { + let ev = xlib::XMotionEvent::from(e); + if let Some(ref win) = wm.current_window() { + let attr = win.attributes(); + let (x0, y0) = win.cursor_coords.unwrap_or((attr.width/2, attr.height/2)); + // Move around the center of the cursor. + let x = ev.x - x0; + let y = ev.y - y0; + win.move_to(x, y).is_ok() + } else { + println!("move_win_drag - nop"); + false + } +} + /// Resize the window certain amount in x and y. pub fn resize_win(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { let w = args[0].parse::().unwrap(); @@ -222,6 +241,23 @@ pub fn add_binding(wm: &mut DotWM, bindings: &mut BindingHash, bindings.insert((key, modifiers), (func, v)); } +/// Add a button binding to the WM. +pub fn add_button_binding(wm: &mut DotWM, bindings: &mut BindingHash, + button: u32, modifiers: u32, func: ExecFn, args: &[&str]) { + ungrab_button(wm.display, button, modifiers); + grab_button(wm.display, button, modifiers, true, + xlib::ButtonPressMask|xlib::ButtonReleaseMask|xlib::PointerMotionMask, + GrabModeAsync, GrabModeAsync, 0, 0); + let mut v = vec![]; + for arg in args { + v.push(arg.to_string()); + } + // for now we need the button1Mask here + bindings.insert((button, modifiers | xlib::Button1Mask), (func, v)); +} + +/// Change the desktop, args parameter only have one usize on it that is the +/// index of the desktop (i.e.: 0 for the first, 1 for the second, etc). pub fn change_desktop(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { let num = args[0].parse::().unwrap(); wm.change_desktop(num); -- cgit v1.2.3-70-g09d2