summary refs log tree commit diff
path: root/src/x
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-12 21:38:14 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-12 21:38:14 -0700
commit9f61e842d01f910349458466c3749d92561f5cde (patch)
tree595153f4d77646dfe2f6841292a084b78a844d39 /src/x
parent7138ace1c1f97f85509278e50b72eb62ed0fd315 (diff)
move XftDraw pointers into a new wrapper FontGraphicContext
impl Drop :)

Force-Push: yes
Change-Id: I2a2a488819c9f03205aade5dd5d119c02dd8a3c2
Diffstat (limited to 'src/x')
-rw-r--r--src/x/font.rs5
-rw-r--r--src/x/font_context.rs46
2 files changed, 49 insertions, 2 deletions
diff --git a/src/x/font.rs b/src/x/font.rs
index 20ae7e4..d12cbe8 100644
--- a/src/x/font.rs
+++ b/src/x/font.rs
@@ -30,13 +30,14 @@ impl Font {
     Ok(Font { display, font_state })
   }
 
-  pub fn draw(&self, xft_context: *mut XftDraw, xft_color: &XftColor,
+  pub fn draw(&self, context: &FontGraphicContext, xft_color: &XftColor,
 	      x: i32, y: i32, text: &str)
     -> xcb::Result<()>
   {
+    let xft_draw = context.get_xft_draw();
     let text = CString::new(text).unwrap();
     unsafe {
-      XftDrawStringUtf8(xft_context, xft_color, self.font_state, x, y,
+      XftDrawStringUtf8(xft_draw, xft_color, self.font_state, x, y,
                         text.as_ptr().cast(), text.count_bytes() as i32)
     };
 
diff --git a/src/x/font_context.rs b/src/x/font_context.rs
new file mode 100644
index 0000000..30e5165
--- /dev/null
+++ b/src/x/font_context.rs
@@ -0,0 +1,46 @@
+use crate::x::*;
+
+use std::sync::Arc;
+
+use x11::xft::{ XftDraw, XftDrawCreate, XftDrawDestroy };
+use xcb::{ Xid };
+
+
+pub struct FontGraphicContext {
+  display: Arc<Display>,
+  context_state: *mut XftDraw,
+}
+
+
+impl FontGraphicContext {
+  pub fn new(window: &Window) -> xcb::Result<FontGraphicContext> {
+    let display = Arc::clone(window.display());
+    let xlib_display = display.connection().get_xlib_display();
+
+    let x_visual = unsafe {
+      x11::xlib::XDefaultVisual(xlib_display, display.screen_index())
+    };
+
+    let colormap = window.screen().default_colormap();
+
+    let context_state = unsafe {
+      XftDrawCreate(xlib_display,
+                    window.window_id().resource_id() as u64,
+                    x_visual,
+                    colormap.resource_id() as u64)
+    };
+
+    Ok(FontGraphicContext { display, context_state })
+  }
+
+  pub fn get_xft_draw(&self) -> *mut XftDraw {
+    self.context_state
+  }
+}
+
+impl Drop for FontGraphicContext {
+  fn drop(&mut self) {
+    unsafe { XftDrawDestroy(self.context_state) };
+  }
+}
+