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
use std::ffi::CString;

use libc;
use x11::xlib as x;

use {Cursor, Event, Visual, Window, X11Error};

/// A connection to an X server.
pub struct Display {
    inner: *mut x::Display,
}

pub struct Grab(pub(crate) *mut x::Display);

/// Something that's part of a display.
pub trait GetDisplay {
    /// Get the current display
    fn get_display(&self) -> *mut x::Display;
}

impl Display {
    /// Opens a new connection to an X server.
    ///
    /// On POSIX-conformant systems, the display name or DISPLAY environment variable can be a string in the format:
    ///     hostname:number.screen_number
    pub fn connect(display_name: impl AsRef<str>) -> Result<Display, X11Error> {
        let display_name = CString::new(display_name.as_ref()).unwrap();
        let inner = unsafe { x::XOpenDisplay(display_name.as_ptr()) };
        if inner.is_null() {
            return Err(X11Error::DisplayOpenError);
        }
        Ok(Display { inner })
    }

    /// Create a Display for an existing connection
    pub fn from_handle(handle: u64) -> Self {
        Display {
            inner: handle as *mut x::Display,
        }
    }

    /// Grab
    pub fn grab(&self) -> Grab {
        unsafe { x::XGrabServer(self.inner) };
        Grab(self.inner)
    }

    /// Wrapper around XCreateFontCursor
    pub fn create_font_cursor(&self, shape: u32) -> Result<Cursor, X11Error> {
        let cursor = unsafe { x::XCreateFontCursor(self.inner, shape) as x::Cursor };
        if cursor == 0 {
            return Err(X11Error::CreateCursorError);
        }
        Ok(Cursor {
            display: self.inner,
            inner: cursor,
        })
    }

    /// Get the next event, blocks until an event is reached.
    pub fn next_event(&self) -> Result<Event, X11Error> {
        let event =
            unsafe { libc::malloc(::std::mem::size_of::<x::XAnyEvent>()) as *mut x::XAnyEvent };
        Event::from_raw(event)
    }

    /// Returns the number of events that are still pending
    pub fn pending(&self) -> Result<i32, X11Error> {
        Ok(unsafe { x::XPending(self.inner) })
    }

    /// Gets the raw X Display handle
    pub fn as_raw(&self) -> *mut x::Display {
        self.inner
    }

    /// Gets the default visual
    pub fn default_visual(&self, screen: i32) -> Visual {
        let visual = unsafe { x::XDefaultVisual(self.inner, screen) };
        Visual { inner: visual }
    }

    /// Returns the root window for the given screen.
    pub fn get_root_window(&self, screen: i32) -> Result<Window, X11Error> {
        let inner = unsafe { x::XRootWindow(self.inner, screen) };
        if inner == 0 {
            return Err(X11Error::GetWindowError);
        }
        let window = Window {
            display: self.inner,
            inner,
        };
        Ok(window)
    }

    /// Returns the root window for the default screen.
    pub fn get_default_root_window(&self) -> Result<Window, X11Error> {
        let inner = unsafe { x::XDefaultRootWindow(self.inner) };
        if inner == 0 {
            return Err(X11Error::GetWindowError);
        }
        let window = Window {
            display: self.inner,
            inner,
        };
        Ok(window)
    }

    /// Translate coordinates relative to w1 to coordinates relative to w2.
    /// If the coordinates are contained in a mapped child of the destination window, the third return
    /// value will hold that child window.
    pub fn translate_coordinates(
        &self,
        w1: Window,
        x: i32,
        y: i32,
        w2: Window,
    ) -> Result<(i32, i32, Option<Window>), X11Error> {
        let mut rx = 0;
        let mut ry = 0;
        let mut child_return: x::Window = 0;
        let status = unsafe {
            x::XTranslateCoordinates(
                self.inner,
                w1.inner,
                w2.inner,
                x,
                y,
                &mut rx,
                &mut ry,
                &mut child_return,
            )
        };
        if status == 0 {
            return Err(X11Error::TranslateCoordinatesError);
        }
        let child = match child_return {
            0 => None,
            val => Some(Window {
                display: self.inner,
                inner: val,
            }),
        };
        Ok((rx, ry, child))
    }

    /// Sync
    pub fn sync(&self, discard: bool) {
        unsafe { x::XSync(self.inner, if discard { 1 } else { 0 }) };
    }

    /// eturns the focus window and the current focus state.
    pub fn get_input_focus(&self) -> Result<(Window, i32), X11Error> {
        let mut focus_return: x::Window = 0;
        let mut revert_to_return = 0;
        unsafe { x::XGetInputFocus(self.inner, &mut focus_return, &mut revert_to_return) };
        let window = Window {
            display: self.inner,
            inner: focus_return,
        };
        return Ok((window, revert_to_return));
    }
}

impl Drop for Display {
    fn drop(&mut self) {
        unsafe { x::XCloseDisplay(self.inner) };
    }
}

impl Drop for Grab {
    fn drop(&mut self) {
        unsafe { x::XUngrabServer(self.0) };
    }
}