summaryrefslogtreecommitdiff
path: root/src/safe_x11/window.rs
blob: 6d1e54e8c47b8d018cdb5aab893a8d48c4dfee3c (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// See LICENSE file for copyright and license details.

use x11::xlib;
use x11::xlib::{
    Window,
    XWindowAttributes,
    XGetWindowAttributes,
    XDefaultScreenOfDisplay,
    XMoveWindow, XMoveResizeWindow,

    XDisplayHeight, XDisplayWidth, XDefaultScreen,
    XA_ATOM, Atom, PropModeReplace,

    XSetInputFocus,
    XSetWindowBorder,

    IsViewable, RevertToParent, CurrentTime,

    XEvent, XSendEvent, XClientMessageEvent,
    ClientMessage,ClientMessageData, NoEventMask,

    XChangeProperty,
    XChangeWindowAttributes, XSetWindowAttributes,

    XMapWindow, XUnmapWindow,

    XBlackPixel,XWhitePixel,  XDrawString,
};

use std::ptr;
use std::mem::uninitialized;
use std::mem::transmute;
use std::cmp::{max};
use std::ffi::CString;

use safe_x11::{screen_ratio,get_color};

/// Representation of [`xlib::Window`](../../x11/xlib/type.Window.html)
pub struct XWindow {
    display: *mut xlib::Display,
    // The Application window.
    pub inner: Window,
    fullscreen: bool,
    prev_attribs: XWindowAttributes,
}

fn fixed_with_ratio(x: i32, ratio: f32) -> i32 {
    let fx = x as f32;
    let fixed = fx * ratio;
    fixed as i32
}

fn window_attribs(display: *mut xlib::Display, w: Window) -> XWindowAttributes {
    unsafe {
        let mut attrptr: XWindowAttributes = uninitialized();
        XGetWindowAttributes(display, w, &mut attrptr);
        attrptr
    }
}

impl XWindow {
    /// Generate a reference to the window in certain display. This will return
    /// `None` if the window is the root window.
    pub fn new(d: *mut xlib::Display, w: Window) -> Option<XWindow> {
        if w != 0 {
            let attrs: XWindowAttributes = unsafe { uninitialized() };
            println!("Window created!");
            Some(XWindow {
                display: d,
                inner: w,
                fullscreen: false,
                prev_attribs: attrs,
            })
        } else {
            None
        }
    }

    /// Set the border width To the window.
    pub fn set_border_width(&self, size: u32) {
        unsafe { xlib::XSetWindowBorderWidth(self.display, self.inner, size) };
    }

    /// clients receiving a WM_DELETE_WINDOW message should behave as if
    /// the user selected "delete window" from a hypothetical menu and
    /// also perform any confirmation dialog with the user.
    pub fn delete(&self) {
        // First delete the border window and then the main window.
        let mut ev: XClientMessageEvent = unsafe { uninitialized() };
        ev.type_ = ClientMessage;
        ev.window = self.inner;
        ev.format = 32;
        ev.message_type = 0; // WM_PROTOCOLS
        ev.data = ClientMessageData::new();
        ev.data.set_long(0, 0); // WM_DELETE_WINDOW
        ev.data.set_long(1, CurrentTime as i64);

        let mut event = XEvent::from(ev);

        unsafe { XSendEvent(self.display, self.inner, 0, NoEventMask, &mut event); }
    }

    /// Sets the border color to the selected window
    pub fn set_border_color<T: Into<Vec<u8>>>(&self, color: T) {
        let c = get_color(self.display, color);
        unsafe { XSetWindowBorder(self.display, self.inner, c); }
    }

    /// Raises a window.
    pub fn raise(&self) {
        unsafe {
            xlib::XRaiseWindow(self.display, self.inner);
        }
    }

    /// Register input events on some [`xlib::Window`](../../x11/xlib/type.Window.html)
    ///
    /// # Example
    ///
    /// if we want to handle when a pointer enter into a window rectangle, we
    /// should do something like:
    ///
    /// ```
    /// use x11::xlib;
    ///
    /// let display = open_display("").unwrap();
    /// // Raw window
    /// let window = 0xc00022;
    ///
    /// window.select_input(xlib::EnterWindowMask);
    /// ```
    pub fn select_input(&self, mask: i64) {
        unsafe { xlib::XSelectInput(self.display, self.inner, mask); }
    }

    /// Returns the window attributes from certain window.
    pub fn attributes(&self) -> XWindowAttributes {
        window_attribs(self.display, self.inner)
    }

    /// Moves the window given an offset from where it is.
    pub fn move_offset(&self, xoffset: i32, yoffset: i32) {
        unsafe {
            let mut attributes: XWindowAttributes = uninitialized();
            XGetWindowAttributes(self.display, self.inner, &mut attributes);
            XMoveWindow(self.display, self.inner,
                        attributes.x + xoffset,
                        attributes.y + yoffset);
        }
    }

    /// Moves the window to a particular coord in the screen.
    pub fn move_to(&self, x: i32, y: i32) -> Result<(), &str> {
        let screen = unsafe {
            let s = XDefaultScreenOfDisplay(self.display);
            ptr::read(s)
        };
        
        if 0 > x && x <= screen.width || 0 > y && y <= screen.height {
            Err("Cannot move the window outside the screen!")
        } else {
            unsafe {
                XMoveWindow(self.display, self.inner, x, y);
            };
            Ok(())
        }
    }

    // Some functions for moving within some boundaries.

    /// Move sticky to the left.
    pub fn move_sticky_left(&self, boundaries: &[i32]) {
        let attrs = self.attributes();
        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a >= attrs.x)
                                   .next() {
            let _ = self.move_to(*v, attrs.y);
        }
    }

    /// Move sticky to the right.
    pub fn move_sticky_right(&self, boundaries: &[i32]) {
        let attrs = self.attributes();
        let win_bound = attrs.x + attrs.width;

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a <= win_bound)
                                   .next() {
            let _ = self.move_to(*v - attrs.width, attrs.y);
        }
    }

    /// Move up sticky.
    pub fn move_sticky_up(&self, boundaries: &[i32]) {
        let attrs = self.attributes();

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a >= attrs.y)
                                   .next() {
            let _ = self.move_to(attrs.x, *v);
        }
    }

    /// Move down sticky.
    pub fn move_sticky_down(&self, boundaries: &[i32]) {
        let attrs = self.attributes();
        let win_bound = attrs.y + attrs.height;

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a <= win_bound)
                                   .next() {
            let _ = self.move_to(attrs.x, *v - attrs.height);
        }
    }

    /// Move and resize the window in one step
    pub fn move_resize(&self, x:i32, y: i32, w: u32, h: u32) {
        unsafe {
            XMoveResizeWindow(self.display, self.inner, x, y, w, h as u32);
        }
    }
    
    /// Resizes the window a fixed amount within the height and width.
    pub fn resize(&self, w: i32, h: i32) {
        let ratio = screen_ratio(self.display);
        let attrs = self.attributes();
        unsafe {
            let ww: u32 = max(1, (attrs.width + fixed_with_ratio(w, ratio)) as u32);
            let wh: u32 = max(1, (attrs.height + fixed_with_ratio(h, ratio)) as u32);
            xlib::XResizeWindow(self.display, self.inner,
                                ww, wh as u32);
        }
    }

    /// Resizes the window an absolute amount within the height and width.
    pub fn resize_abs(&self, w: i32, h: i32) {
        let attrs = self.attributes();
        unsafe {
            let ww: u32 = max(1, (attrs.width + w) as u32);
            let wh: u32 = max(1, (attrs.height + h) as u32);
            xlib::XResizeWindow(self.display, self.inner,
                                ww, wh);
        }
    }

    pub fn resize_to(&self, w: i32, h: i32) {
        unsafe {
            let ww: u32 = max(1, w as u32);
            let wh: u32 = max(1, h as u32);
            xlib::XResizeWindow(self.display, self.inner,
                                ww, wh);
        }
    }

    pub fn resize_sticky_left(&self, boundaries: &[i32]) {
        let attrs = self.attributes();

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a >= attrs.x + attrs.width)
                                   .next() {
            let diff = attrs.width - (*v - attrs.x);
            if *v - attrs.x > 0 {
                let _ = self.resize_to(attrs.width - diff, attrs.height);
            }
       }
    }

    pub fn resize_sticky_right(&self, boundaries: &[i32]) {
        let attrs = self.attributes();
        let win_bound = attrs.x + attrs.width;

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a <= win_bound)
                                   .next() {
            let new_width = *v - attrs.x;
            let _ = self.resize_to(new_width, attrs.height);
       }
    }

    pub fn resize_sticky_up(&self, boundaries: &[i32]) {
        let attrs = self.attributes();

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a >= attrs.y + attrs.height)
                                   .next() {
            let diff = attrs.height - (*v - attrs.y);
            if *v - attrs.y > 0 {
                let _ = self.resize_to(attrs.width, attrs.height - diff);
            }
       }
    }

    pub fn resize_sticky_down(&self, boundaries: &[i32]) {
        let attrs = self.attributes();
        let win_bound = attrs.y + attrs.height;

        if let Some(v) = boundaries.iter()
                                   .skip_while(|&a| *a <= win_bound)
                                   .next() {
            let new_height = *v - attrs.y;
            let _ = self.resize_to(attrs.width, new_height);
       }
    }

    /// Raise the focus of the window and set a border.
    pub fn focus(&self) {
        let attrs = self.attributes();
        if attrs.map_state == IsViewable {
            unsafe {
                XSetInputFocus(self.display, self.inner,
                               RevertToParent, CurrentTime)
            };
        }
        self.raise();
        self.set_border_width(1);
        self.set_border_color("red");
    }

    /// Give a black border.
    pub fn unfocus(&self) {
        self.set_border_width(1);
        self.set_border_color("black");
    }

    /// Fullscreen toggle
    pub fn fullscreen(&mut self, wm_state_atom: Atom, fs_atom: Atom) {
        let screen = unsafe { XDefaultScreen(self.display) };
        let dh: u32 = unsafe { XDisplayHeight(self.display, screen) as u32 };
        let dw: u32 = unsafe { XDisplayWidth(self.display, screen) as u32 };

        if !self.fullscreen {
            // First, store the XWindowAttributes to query later.
            self.prev_attribs = self.attributes();

            self.fullscreen = true;
            self.move_resize(0, 0, dw, dh);
            self.set_border_width(0);
        } else {
            self.fullscreen = false;
            self.move_resize(self.prev_attribs.x,
                             self.prev_attribs.y,
                             self.prev_attribs.width as u32,
                             self.prev_attribs.height as u32);
            self.set_border_width(1);
        }

        // EWMH for fullscreen (Don't know how to test it.)
        let fs_atom_slice: &[Atom; 1]  = &[fs_atom];
        let fs_atom_ptr: *const u8 = unsafe { transmute(fs_atom_slice) };

        unsafe {
            XChangeProperty(self.display, self.inner, wm_state_atom,
                            XA_ATOM, 32, PropModeReplace, fs_atom_ptr,
                            self.fullscreen as i32);
        }
    }

    pub fn draw_string<T: Into<Vec<u8>>>(&self, s: T) {
        // Title :D
        println!("Drawing string for {}", self.inner);
        let cstr = CString::new(s).unwrap();
        unsafe {
            let gc = xlib::XCreateGC(self.display, self.inner, 0, ptr::null_mut());
            xlib::XSetForeground(self.display, gc, XWhitePixel(self.display, 0));
            xlib::XSetBackground(self.display, gc, XBlackPixel(self.display, 0));
            println!("Loading fixed");
            let font: xlib::XFontStruct = ptr::read(xlib::XLoadQueryFont(self.display, CString::new("fixed").unwrap().as_ptr()));
            xlib::XSetFont(self.display, gc, font.fid);
            xlib::XClearWindow(self.display, self.inner);
            XDrawString(self.display, self.inner, gc,
                        3, 10, cstr.as_ptr(), cstr.to_bytes().len() as i32);
        }
    }

    pub fn map(&self) {
        unsafe { XMapWindow(self.display, self.inner); }
    }

    pub fn unmap(&self) {
        unsafe { XUnmapWindow(self.display, self.inner); }
    }
}

pub fn change_window_attributes(display: *mut xlib::Display, win: Window, mask: u64,
                                window_attribs: *mut XSetWindowAttributes) {
    unsafe {
        XChangeWindowAttributes(display, win, mask, window_attribs);
    }
}