summaryrefslogtreecommitdiff
path: root/src/cursors.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cursors.rs')
-rw-r--r--src/cursors.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/cursors.rs b/src/cursors.rs
new file mode 100644
index 0000000..1e5fbd3
--- /dev/null
+++ b/src/cursors.rs
@@ -0,0 +1,53 @@
+use crate::Result;
+use xcb::{
+ x::{CreateGlyphCursor, Cursor, Font, OpenFont},
+ Connection,
+};
+
+// https://tronche.com/gui/x/xlib/appendix/b/
+const XC_LEFT_PTR: u16 = 68;
+
+#[derive(Debug)]
+pub struct Cursors {
+ font: Font,
+
+ pub normal: Cursor,
+ // TODO: ...
+}
+
+impl Cursors {
+ pub fn new_with(conn: &Connection) -> Result<Self> {
+ // Open cursor font
+ let font = conn.generate_id();
+ conn.check_request(conn.send_request_checked(&OpenFont {
+ fid: font,
+ name: "cursor".as_bytes(),
+ }))?;
+
+ Ok(Self {
+ normal: Self::load_cursor(conn, font, XC_LEFT_PTR)?,
+ font,
+ })
+ }
+
+ /// Load the cursor with the given id from `font`
+ fn load_cursor(conn: &Connection, font: Font, id: u16) -> Result<Cursor> {
+ let cid = conn.generate_id();
+ // https://github.com/mirror/libX11/blob/ff8706a5eae25b8bafce300527079f68a201d27f/src/Cursor.c#L34
+ conn.check_request(conn.send_request_checked(&CreateGlyphCursor {
+ cid,
+ source_font: font,
+ mask_font: font,
+ source_char: id,
+ mask_char: id + 1,
+ fore_red: 0,
+ fore_green: 0,
+ fore_blue: 0,
+ back_red: u16::MAX,
+ back_green: u16::MAX,
+ back_blue: u16::MAX,
+ }))?;
+
+ Ok(cid)
+ }
+}