aboutsummaryrefslogtreecommitdiff
path: root/src/dotwm.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/dotwm.rs')
-rw-r--r--src/dotwm.rs49
1 files changed, 29 insertions, 20 deletions
diff --git a/src/dotwm.rs b/src/dotwm.rs
index 77f09b3..952fc8c 100644
--- a/src/dotwm.rs
+++ b/src/dotwm.rs
@@ -2,15 +2,11 @@ use x11::xlib;
use x11::xlib::{
Display,
XErrorEvent,
- XEvent,
-
- GrabModeAsync,
};
use safe_x11::{
open_display,
close_display,
- grab_key,
};
use safe_x11::window::XWindow;
@@ -18,7 +14,6 @@ use safe_x11::window::XWindow;
use std::ptr;
use std::process::exit;
use std::mem::uninitialized;
-use std::collections::HashMap;
use libc::c_int;
#[allow(unused_variables)]
@@ -33,13 +28,9 @@ unsafe extern "C" fn error_handler(d: *mut Display, evptr: *mut XErrorEvent) ->
0
}
-/// Defines a callback to call no certains events.
-pub type ExecFn = fn(&DotWM, XEvent, &[String]) -> bool;
-
pub struct DotWM {
pub display: *mut Display,
// Map with the keys as (key, mod)
- bindings: HashMap<(u32, u32), (ExecFn, Vec<String>)>,
cw_idx: usize,
pub window_list: Vec<XWindow>,
}
@@ -60,7 +51,6 @@ impl DotWM {
DotWM {
display: d,
- bindings: HashMap::new(),
cw_idx: 0,
window_list: vec![],
}
@@ -87,6 +77,7 @@ impl DotWM {
/// dotwm.add_binding(keysym::XK_Return, xlib::Mod4Mask, exec,
/// &["xterm"]);
/// ```
+ /*
pub fn add_binding(&mut self, key: u32, modifiers: u32, func: ExecFn, args: &[&str]) {
grab_key(self.display, key, modifiers, true, GrabModeAsync, GrabModeAsync);
let mut v = vec![];
@@ -95,13 +86,16 @@ impl DotWM {
}
self.bindings.insert((key, modifiers), (func, v));
}
+ */
+ /*
pub fn exec_func(&mut self, key: u32, modifiers: u32, ev: xlib::XEvent) {
if let Some(&(func, ref args)) = self.bindings.get(&(key, modifiers)) {
let v = args.clone();
func(&self, ev, &v);
}
}
+ */
pub fn add_window(&mut self, w: xlib::Window) {
self.unfocus_current_window();
@@ -115,7 +109,6 @@ impl DotWM {
}
pub fn current_window(&self) -> Option<&XWindow> {
- println!("trying to get {} from list of len {}", self.cw_idx, self.window_list.len());
if self.cw_idx < self.window_list.len() {
self.window_list.get(self.cw_idx)
} else {
@@ -125,7 +118,6 @@ impl DotWM {
pub fn remove_window(&mut self, w: xlib::Window) {
let pos = self.window_list.iter().position(|xw| xw.inner == w);
- println!("Removing widnow {}: pos: {:?}", w, pos);
match pos {
Some(idx) => {
@@ -147,23 +139,40 @@ impl DotWM {
w.raise();
}
+ /// Find a given window and returns it's position on the window_list.
pub fn find_window(&self, w: xlib::Window) -> Option<usize> {
self.window_list.iter().position(|xw| xw.inner == w)
}
+ /// Focus the next window.
+ ///
+ /// There're 3 posibilities. There's no window, there's one window or there
+ /// are more windows.
+ pub fn focus_next(&mut self) {
+ if self.window_list.len() > 0 {
+ // Unfocus the current window.
+ let prev_win = &self.window_list[self.cw_idx];
+ prev_win.unfocus();
+
+ // And give focus to the next one.
+ let next_win_idx = if self.cw_idx < self.window_list.len() - 1 {
+ self.cw_idx + 1
+ } else {
+ 0
+ };
+
+ let curr_win = &self.window_list[next_win_idx];
+ curr_win.focus();
+ self.cw_idx = next_win_idx;
+ }
+ }
fn focus_current_window(&self) {
- if let Some(win) = self.current_window() {
- win.set_border_width(1);
- win.set_border_color("red");
- }
+ self.current_window().map(|x| x.focus());
}
fn unfocus_current_window(&self) {
- if let Some(win) = self.current_window() {
- win.set_border_width(1);
- win.set_border_color("black");
- }
+ self.current_window().map(|x| x.unfocus());
}
}