summaryrefslogtreecommitdiff
path: root/src/conn_info/colours.rs
blob: d678bdb87b72f21973dd3fe688130977a77cf79d (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
use crate::error::Result;
use xcb::{
    x::{AllocColor, Colormap, FreeColors},
    Connection as RawConnection,
};

/// Caches colours in an X11 color map.
pub struct Colours {
    #[allow(unused)] // Make sure the colour map we're using doesn't go anywhere
    cmap: Colormap,
    border_normal: u32,
    border_focused: u32,
}

impl Colours {
    /// Load the colours into the given colour map
    pub fn new_with(conn: &RawConnection, cmap: Colormap) -> Result<Self> {
        // TODO: Move these colours out to config.rs
        let (border_normal, border_focused) = (
            conn.wait_for_reply(conn.send_request(&AllocColor {
                cmap,
                red: 0,
                green: 0,
                blue: 0,
            }))?,
            conn.wait_for_reply(conn.send_request(&AllocColor {
                cmap,
                red: u16::MAX,
                green: 0,
                blue: u16::MAX,
            }))?,
        );

        Ok(Self {
            cmap,
            border_normal: border_normal.pixel(),
            border_focused: border_focused.pixel(),
        })
    }

    /// Get the pixel ID of the colour for an unfocused window's border.
    pub const fn border_normal(&self) -> u32 {
        self.border_normal
    }

    /// Get the pixel ID of the colour for a focused window's border.
    pub const fn border_focused(&self) -> u32 {
        self.border_focused
    }

    /// Free the associated resources to avoid leaking them.
    /// This object must not be used again after this method is called, as all resources will be invalid.
    pub unsafe fn free(&self, conn: &RawConnection) {
        conn.send_request(&FreeColors {
            cmap: self.cmap,
            plane_mask: 0,
            pixels: &[self.border_normal, self.border_focused],
        });
    }
}