summaryrefslogtreecommitdiff
path: root/src/dotwm.rs
blob: b3ed7826a2275b5bc53cd31c2aa3da6a0f49f60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// See LICENSE file for copyright and license details.

use x11::xlib;
use x11::xlib::{
    Display,
    XErrorEvent,
    Atom,
    XChangeProperty,
};

use safe_x11::{
    open_display,
    close_display,
    atom,
    screen_size,
};

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)]
unsafe extern "C" fn error_handler(d: *mut Display, evptr: *mut XErrorEvent) -> c_int {
    println!("ERRORR");
    let ev = ptr::read(evptr);
    if ev.error_code == xlib::BadAccess {
        println!("Another widnow manager is running :C");
        exit(1);
    }

    0
}

#[derive(Hash,Eq,PartialEq)]
pub enum WmAtom {
    Protocols,
    DeleteWindow,
}

#[derive(Hash,Eq,PartialEq)]
pub enum NetAtom {
    NetSupported,
    NetFullscreen,
    NetWmState,
    NetActive,
}

/// State of the window manager.
pub struct DotWM {
    /// Reference to the X display.
    pub display: *mut Display,
    /// References to all the windows.
    pub window_list: Vec<XWindow>,
    /// Check if the window manager needs to end.
    pub finish: bool,
    /// Vec holding atoms for ICCCM support
    pub wmatoms: HashMap<WmAtom, Atom>,
    /// Vec holding atoms for EWHM support
    pub netatoms: HashMap<NetAtom, Atom>,
    /// Number of the active desctop
    current_desktop: usize,
    cw_idx: usize,
}

/// DotWM state.
impl DotWM {
    /// Create a state of the Dot Window Manager
    pub fn new() -> DotWM {
        let d = open_display("").unwrap();
        unsafe { xlib::XSetErrorHandler(Some(error_handler)) };

        // Add substructure notify mask
        let root = unsafe { xlib::XDefaultRootWindow(d) };

        unsafe {
            let mut attrs: xlib::XSetWindowAttributes = uninitialized();
            attrs.event_mask = xlib::SubstructureNotifyMask;
            xlib::XChangeWindowAttributes(d, root, xlib::CWEventMask, &mut attrs);
        }

        // Get the wmatoms
        let mut wmatoms = HashMap::new();
        wmatoms.insert(WmAtom::Protocols, atom(d, "WM_PROTOCOLS"));
        wmatoms.insert(WmAtom::DeleteWindow, atom(d, "WM_DELETE_WINDOW"));

        // Get the netatoms
        let mut netatoms = HashMap::new();
        netatoms.insert(NetAtom::NetSupported, atom(d, "_NET_SUPPORTED"));
        netatoms.insert(NetAtom::NetFullscreen, atom(d, "_NET_WM_STATE_FULLSCREEN"));
        netatoms.insert(NetAtom::NetWmState, atom(d, "_NET_ACTIVE_WINDOW"));
        netatoms.insert(NetAtom::NetActive, atom(d, "_NET_WM_STATE"));

        // Propagate EWHM support
        unsafe {
            let atomvec: Vec<&Atom> = netatoms.values().collect();
            let len: i32 = netatoms.len() as i32;
            let atomptr = atomvec.as_ptr();

            XChangeProperty(d, root, netatoms[&NetAtom::NetSupported], xlib::XA_ATOM,
                            32, xlib::PropModeReplace, atomptr as *const u8, len);
        }

        DotWM {
            display: d,
            cw_idx: 0,
            finish: false,
            wmatoms: wmatoms,
            netatoms: netatoms,
            current_desktop: 0,
            window_list: vec![],
        }
    }

    pub fn add_window(&mut self, w: xlib::Window) {
        self.unfocus_current_window();
        if let Some(w) = XWindow::new(self.display, w) {
            w.select_input(xlib::EnterWindowMask);
            self.window_list.push(w);
            // Last windows get focus.
            self.cw_idx = self.window_list.len() - 1;
            self.focus_current_window();
        }
    }

    pub fn current_window(&self) -> Option<&XWindow> {
        if self.cw_idx < self.window_list.len() {
            self.window_list.get(self.cw_idx)
        } else {
            None
        }
    }

    pub fn current_window_mut(&mut self) -> Option<&mut XWindow> {
        if self.cw_idx < self.window_list.len() {
            self.window_list.get_mut(self.cw_idx)
        } else {
            None
        }
    }

    pub fn remove_window(&mut self, w: xlib::Window) {
        let pos = self.window_list.iter().position(|xw| xw.inner == w);
        
        match pos {
            Some(idx) => { 
                let new_idx = if idx == 0 { usize::max_value() } else { idx - 1 };
                self.window_list.remove(idx);
                self.cw_idx = new_idx;
                self.focus_current_window();
            },
            None => (),
        }
    }

    pub fn change_focus_of(&mut self, idx: usize) {
        let w = &self.window_list[idx];
        self.unfocus_current_window();
        self.cw_idx = idx;
        w.set_border_width(1);
        w.set_border_color("red");
        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) {
        self.current_window().map(|x| x.focus());
    }

    fn unfocus_current_window(&self) {
        self.current_window().map(|x| x.unfocus());
    }

    /// Get a list of the geometries for every window in the screen (including
    /// the screen borders. The form is (x, y).
    ///
    /// # Example
    ///
    /// Having a screen of 800x600, and a window on 10(x), 10(y) with dimensions 
    /// 400(width) and 300(height):
    ///
    /// ```
    /// for (x, y) in dotwm.geometries() {
    ///     println!("x: {} - y: {}", x, y);
    /// }
    /// ```
    ///
    /// The output will be:
    ///
    /// ```
    /// (0,0)
    /// (800, 600)
    /// (10, 10)
    /// (410, 310)
    /// ```
    pub fn geometries(&self) -> Vec<(i32,i32)> {
        let (screen_width, screen_height) = screen_size(self.display);
        let mut result: Vec<(i32,i32)> = vec![
            (0, 0),
            (screen_width as i32, screen_height as i32),
        ];

        for w in self.window_list.iter() {
            let attrs = w.attributes();
            result.push((attrs.x, attrs.y));
            result.push((attrs.x + attrs.width, attrs.y + attrs.height));
        }

        result
    }

    pub fn x_geometries(&self) -> Vec<i32> {
        self.geometries().iter().map(|x| x.0).collect()
    }

    pub fn y_geometries(&self) -> Vec<i32> {
        self.geometries().iter().map(|x| x.1).collect()
    }
}

impl Drop for DotWM {
    fn drop(&mut self) {
        println!("Closing display");
        close_display(self.display);
    }
}