tony / oxwm

Rust is memory safe. Browse files
tonybanters committed 2025-12-20 parent fa421d2 commit 8e7c5c62ef1d2d4ff41c24bca96fff773e99f6b1
Showing 3 changed files with 99 additions and 86 deletions. UnifiedSplit
+24 −39 src/bar/bar.rs
@@ -1,5 +1,5 @@
1 use super::blocks::Block; 1 use super::blocks::Block;
2 -use super::font::{Font, FontDraw}; 2 +use super::font::{DrawingSurface, Font};
3 use crate::Config; 3 use crate::Config;
4 use crate::errors::X11Error; 4 use crate::errors::X11Error;
5 use std::time::Instant; 5 use std::time::Instant;
@@ -13,10 +13,7 @@ pub struct Bar {
13 width: u16, 13 width: u16,
14 height: u16, 14 height: u16,
15 graphics_context: Gcontext, 15 graphics_context: Gcontext,
16 - pixmap: x11::xlib::Pixmap, 16 + surface: DrawingSurface,
17 - display: *mut x11::xlib::Display,
18 -
19 - font_draw: FontDraw,
20 17
21 tag_widths: Vec<u16>, 18 tag_widths: Vec<u16>,
22 needs_redraw: bool, 19 needs_redraw: bool,
@@ -80,19 +77,15 @@ impl Bar {
80 77
81 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; 78 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
82 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; 79 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
83 - let depth = unsafe { x11::xlib::XDefaultDepth(display, screen_num as i32) };
84 -
85 - let pixmap = unsafe {
86 - x11::xlib::XCreatePixmap(
87 - display,
88 - window as x11::xlib::Drawable,
89 - width as u32,
90 - height as u32,
91 - depth as u32,
92 - )
93 - };
94 80
95 - let font_draw = FontDraw::new(display, pixmap, visual, colormap)?; 81 + let surface = DrawingSurface::new(
82 + display,
83 + window as x11::xlib::Drawable,
84 + width as u32,
85 + height as u32,
86 + visual,
87 + colormap,
88 + )?;
96 89
97 let horizontal_padding = (font.height() as f32 * 0.4) as u16; 90 let horizontal_padding = (font.height() as f32 * 0.4) as u16;
98 91
@@ -124,9 +117,7 @@ impl Bar {
124 width, 117 width,
125 height, 118 height,
126 graphics_context, 119 graphics_context,
127 - pixmap, 120 + surface,
128 - display,
129 - font_draw,
130 tag_widths, 121 tag_widths,
131 needs_redraw: true, 122 needs_redraw: true,
132 blocks, 123 blocks,
@@ -201,11 +192,11 @@ impl Bar {
201 connection.flush()?; 192 connection.flush()?;
202 193
203 unsafe { 194 unsafe {
204 - let gc = x11::xlib::XCreateGC(display, self.pixmap, 0, std::ptr::null_mut()); 195 + let gc = x11::xlib::XCreateGC(display, self.surface.pixmap(), 0, std::ptr::null_mut());
205 x11::xlib::XSetForeground(display, gc, self.scheme_normal.background as u64); 196 x11::xlib::XSetForeground(display, gc, self.scheme_normal.background as u64);
206 x11::xlib::XFillRectangle( 197 x11::xlib::XFillRectangle(
207 display, 198 display,
208 - self.pixmap, 199 + self.surface.pixmap(),
209 gc, 200 gc,
210 0, 201 0,
211 0, 202 0,
@@ -241,7 +232,8 @@ impl Bar {
241 let top_padding = 4; 232 let top_padding = 4;
242 let text_y = top_padding + font.ascent(); 233 let text_y = top_padding + font.ascent();
243 234
244 - self.font_draw 235 + self.surface
236 + .font_draw()
245 .draw_text(font, scheme.foreground, text_x, text_y, tag); 237 .draw_text(font, scheme.foreground, text_x, text_y, tag);
246 238
247 if is_selected || is_urgent { 239 if is_selected || is_urgent {
@@ -255,11 +247,11 @@ impl Bar {
255 let underline_x = x_position + (underline_padding / 2) as i16; 247 let underline_x = x_position + (underline_padding / 2) as i16;
256 248
257 unsafe { 249 unsafe {
258 - let gc = x11::xlib::XCreateGC(display, self.pixmap, 0, std::ptr::null_mut()); 250 + let gc = x11::xlib::XCreateGC(display, self.surface.pixmap(), 0, std::ptr::null_mut());
259 x11::xlib::XSetForeground(display, gc, scheme.underline as u64); 251 x11::xlib::XSetForeground(display, gc, scheme.underline as u64);
260 x11::xlib::XFillRectangle( 252 x11::xlib::XFillRectangle(
261 display, 253 display,
262 - self.pixmap, 254 + self.surface.pixmap(),
263 gc, 255 gc,
264 underline_x as i32, 256 underline_x as i32,
265 underline_y as i32, 257 underline_y as i32,
@@ -279,7 +271,7 @@ impl Bar {
279 let top_padding = 4; 271 let top_padding = 4;
280 let text_y = top_padding + font.ascent(); 272 let text_y = top_padding + font.ascent();
281 273
282 - self.font_draw.draw_text( 274 + self.surface.font_draw().draw_text(
283 font, 275 font,
284 self.scheme_normal.foreground, 276 self.scheme_normal.foreground,
285 text_x, 277 text_x,
@@ -295,7 +287,7 @@ impl Bar {
295 let text_x = x_position; 287 let text_x = x_position;
296 let text_y = top_padding + font.ascent(); 288 let text_y = top_padding + font.ascent();
297 289
298 - self.font_draw.draw_text( 290 + self.surface.font_draw().draw_text(
299 font, 291 font,
300 self.scheme_selected.foreground, 292 self.scheme_selected.foreground,
301 text_x, 293 text_x,
@@ -316,7 +308,8 @@ impl Bar {
316 let top_padding = 4; 308 let top_padding = 4;
317 let text_y = top_padding + font.ascent(); 309 let text_y = top_padding + font.ascent();
318 310
319 - self.font_draw 311 + self.surface
312 + .font_draw()
320 .draw_text(font, block.color(), x_position, text_y, &text); 313 .draw_text(font, block.color(), x_position, text_y, &text);
321 314
322 if self.block_underlines[i] { 315 if self.block_underlines[i] {
@@ -331,11 +324,11 @@ impl Bar {
331 324
332 unsafe { 325 unsafe {
333 let gc = 326 let gc =
334 - x11::xlib::XCreateGC(display, self.pixmap, 0, std::ptr::null_mut()); 327 + x11::xlib::XCreateGC(display, self.surface.pixmap(), 0, std::ptr::null_mut());
335 x11::xlib::XSetForeground(display, gc, block.color() as u64); 328 x11::xlib::XSetForeground(display, gc, block.color() as u64);
336 x11::xlib::XFillRectangle( 329 x11::xlib::XFillRectangle(
337 display, 330 display,
338 - self.pixmap, 331 + self.surface.pixmap(),
339 gc, 332 gc,
340 underline_x as i32, 333 underline_x as i32,
341 underline_y as i32, 334 underline_y as i32,
@@ -358,7 +351,7 @@ impl Bar {
358 ); 351 );
359 x11::xlib::XCopyArea( 352 x11::xlib::XCopyArea(
360 display, 353 display,
361 - self.pixmap, 354 + self.surface.pixmap(),
362 self.window as x11::xlib::Drawable, 355 self.window as x11::xlib::Drawable,
363 gc, 356 gc,
364 0, 357 0,
@@ -418,11 +411,3 @@ impl Bar {
418 self.needs_redraw = true; 411 self.needs_redraw = true;
419 } 412 }
420 } 413 }
421 -
422 -impl Drop for Bar {
423 - fn drop(&mut self) {
424 - unsafe {
425 - x11::xlib::XFreePixmap(self.display, self.pixmap);
426 - }
427 - }
428 -}
+48 −0 src/bar/font.rs
@@ -149,3 +149,51 @@ impl Drop for FontDraw {
149 } 149 }
150 } 150 }
151 } 151 }
152 +
153 +pub struct DrawingSurface {
154 + font_draw: FontDraw,
155 + pixmap: x11::xlib::Pixmap,
156 + display: *mut Display,
157 +}
158 +
159 +impl DrawingSurface {
160 + pub fn new(
161 + display: *mut Display,
162 + window: x11::xlib::Drawable,
163 + width: u32,
164 + height: u32,
165 + visual: *mut Visual,
166 + colormap: Colormap,
167 + ) -> Result<Self, crate::errors::X11Error> {
168 + let depth = unsafe { x11::xlib::XDefaultDepth(display, 0) };
169 + let pixmap = unsafe {
170 + x11::xlib::XCreatePixmap(display, window, width, height, depth as u32)
171 + };
172 +
173 + let font_draw = FontDraw::new(display, pixmap, visual, colormap)?;
174 +
175 + Ok(Self {
176 + font_draw,
177 + pixmap,
178 + display,
179 + })
180 + }
181 +
182 + pub fn pixmap(&self) -> x11::xlib::Pixmap {
183 + self.pixmap
184 + }
185 +
186 + pub fn font_draw(&self) -> &FontDraw {
187 + &self.font_draw
188 + }
189 +}
190 +
191 +impl Drop for DrawingSurface {
192 + fn drop(&mut self) {
193 + unsafe {
194 + x11::xft::XftDrawDestroy(self.font_draw.xft_draw);
195 + self.font_draw.xft_draw = std::ptr::null_mut();
196 + x11::xlib::XFreePixmap(self.display, self.pixmap);
197 + }
198 + }
199 +}
+27 −47 src/tab_bar.rs
@@ -1,5 +1,5 @@
1 use crate::ColorScheme; 1 use crate::ColorScheme;
2 -use crate::bar::font::{Font, FontDraw}; 2 +use crate::bar::font::{DrawingSurface, Font};
3 use crate::errors::X11Error; 3 use crate::errors::X11Error;
4 use crate::layout::tabbed::TAB_BAR_HEIGHT; 4 use crate::layout::tabbed::TAB_BAR_HEIGHT;
5 use x11rb::COPY_DEPTH_FROM_PARENT; 5 use x11rb::COPY_DEPTH_FROM_PARENT;
@@ -14,9 +14,8 @@ pub struct TabBar {
14 x_offset: i16, 14 x_offset: i16,
15 y_offset: i16, 15 y_offset: i16,
16 graphics_context: Gcontext, 16 graphics_context: Gcontext,
17 - pixmap: x11::xlib::Pixmap,
18 display: *mut x11::xlib::Display, 17 display: *mut x11::xlib::Display,
19 - font_draw: FontDraw, 18 + surface: DrawingSurface,
20 scheme_normal: ColorScheme, 19 scheme_normal: ColorScheme,
21 scheme_selected: ColorScheme, 20 scheme_selected: ColorScheme,
22 } 21 }
@@ -69,19 +68,15 @@ impl TabBar {
69 68
70 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; 69 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
71 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; 70 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
72 - let depth = unsafe { x11::xlib::XDefaultDepth(display, screen_num as i32) };
73 71
74 - let pixmap = unsafe { 72 + let surface = DrawingSurface::new(
75 - x11::xlib::XCreatePixmap( 73 + display,
76 - display, 74 + window as x11::xlib::Drawable,
77 - window as x11::xlib::Drawable, 75 + width as u32,
78 - width as u32, 76 + height as u32,
79 - height as u32, 77 + visual,
80 - depth as u32, 78 + colormap,
81 - ) 79 + )?;
82 - };
83 -
84 - let font_draw = FontDraw::new(display, pixmap, visual, colormap)?;
85 80
86 Ok(Self { 81 Ok(Self {
87 window, 82 window,
@@ -90,9 +85,8 @@ impl TabBar {
90 x_offset: x, 85 x_offset: x,
91 y_offset: y, 86 y_offset: y,
92 graphics_context, 87 graphics_context,
93 - pixmap,
94 display, 88 display,
95 - font_draw, 89 + surface,
96 scheme_normal, 90 scheme_normal,
97 scheme_selected, 91 scheme_selected,
98 }) 92 })
@@ -116,11 +110,11 @@ impl TabBar {
116 connection.flush()?; 110 connection.flush()?;
117 111
118 unsafe { 112 unsafe {
119 - let gc = x11::xlib::XCreateGC(self.display, self.pixmap, 0, std::ptr::null_mut()); 113 + let gc = x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut());
120 x11::xlib::XSetForeground(self.display, gc, self.scheme_normal.background as u64); 114 x11::xlib::XSetForeground(self.display, gc, self.scheme_normal.background as u64);
121 x11::xlib::XFillRectangle( 115 x11::xlib::XFillRectangle(
122 self.display, 116 self.display,
123 - self.pixmap, 117 + self.surface.pixmap(),
124 gc, 118 gc,
125 0, 119 0,
126 0, 120 0,
@@ -158,7 +152,8 @@ impl TabBar {
158 let top_padding = 6; 152 let top_padding = 6;
159 let text_y = top_padding + font.ascent(); 153 let text_y = top_padding + font.ascent();
160 154
161 - self.font_draw 155 + self.surface
156 + .font_draw()
162 .draw_text(font, scheme.foreground, text_x, text_y, &display_title); 157 .draw_text(font, scheme.foreground, text_x, text_y, &display_title);
163 158
164 if is_focused { 159 if is_focused {
@@ -167,11 +162,11 @@ impl TabBar {
167 162
168 unsafe { 163 unsafe {
169 let gc = 164 let gc =
170 - x11::xlib::XCreateGC(self.display, self.pixmap, 0, std::ptr::null_mut()); 165 + x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut());
171 x11::xlib::XSetForeground(self.display, gc, scheme.underline as u64); 166 x11::xlib::XSetForeground(self.display, gc, scheme.underline as u64);
172 x11::xlib::XFillRectangle( 167 x11::xlib::XFillRectangle(
173 self.display, 168 self.display,
174 - self.pixmap, 169 + self.surface.pixmap(),
175 gc, 170 gc,
176 x_position as i32, 171 x_position as i32,
177 underline_y as i32, 172 underline_y as i32,
@@ -195,7 +190,7 @@ impl TabBar {
195 x11::xlib::XCreateGC(self.display, self.window as u64, 0, std::ptr::null_mut()); 190 x11::xlib::XCreateGC(self.display, self.window as u64, 0, std::ptr::null_mut());
196 x11::xlib::XCopyArea( 191 x11::xlib::XCopyArea(
197 self.display, 192 self.display,
198 - self.pixmap, 193 + self.surface.pixmap(),
199 self.window as u64, 194 self.window as u64,
200 gc, 195 gc,
201 0, 196 0,
@@ -239,24 +234,17 @@ impl TabBar {
239 .width(width as u32), 234 .width(width as u32),
240 )?; 235 )?;
241 236
242 - unsafe {
243 - x11::xlib::XFreePixmap(self.display, self.pixmap);
244 - }
245 -
246 - let depth = unsafe { x11::xlib::XDefaultDepth(self.display, 0) };
247 - self.pixmap = unsafe {
248 - x11::xlib::XCreatePixmap(
249 - self.display,
250 - self.window as x11::xlib::Drawable,
251 - width as u32,
252 - self.height as u32,
253 - depth as u32,
254 - )
255 - };
256 -
257 let visual = unsafe { x11::xlib::XDefaultVisual(self.display, 0) }; 237 let visual = unsafe { x11::xlib::XDefaultVisual(self.display, 0) };
258 let colormap = unsafe { x11::xlib::XDefaultColormap(self.display, 0) }; 238 let colormap = unsafe { x11::xlib::XDefaultColormap(self.display, 0) };
259 - self.font_draw = FontDraw::new(self.display, self.pixmap, visual, colormap)?; 239 +
240 + self.surface = DrawingSurface::new(
241 + self.display,
242 + self.window as x11::xlib::Drawable,
243 + width as u32,
244 + self.height as u32,
245 + visual,
246 + colormap,
247 + )?;
260 248
261 connection.flush()?; 249 connection.flush()?;
262 Ok(()) 250 Ok(())
@@ -274,11 +262,3 @@ impl TabBar {
274 Ok(()) 262 Ok(())
275 } 263 }
276 } 264 }
277 -
278 -impl Drop for TabBar {
279 - fn drop(&mut self) {
280 - unsafe {
281 - x11::xlib::XFreePixmap(self.display, self.pixmap);
282 - }
283 - }
284 -}