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