tony / oxwm

Added fonts.. (bloatware), updated devshell accordingly Browse files
tonybtw committed 2025-10-03 parent da4d6dc commit fc9864b107552746b147d939f497a47f3b7d33d0
Showing 8 changed files with 224 additions and 43 deletions. UnifiedSplit
+17 −0 Cargo.lock
@@ -51,9 +51,16 @@ name = "oxwm"
51 51 version = "0.1.0"
52 52 dependencies = [
53 53 "anyhow",
54 + "x11",
54 55 "x11rb",
55 56 ]
56 57
58 +[[package]]
59 +name = "pkg-config"
60 +version = "0.3.32"
61 +source = "registry+https://github.com/rust-lang/crates.io-index"
62 +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
63 +
57 64 [[package]]
58 65 name = "rustix"
59 66 version = "1.1.2"
@@ -146,6 +153,16 @@ version = "0.52.6"
146 153 source = "registry+https://github.com/rust-lang/crates.io-index"
147 154 checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
148 155
156 +[[package]]
157 +name = "x11"
158 +version = "2.21.0"
159 +source = "registry+https://github.com/rust-lang/crates.io-index"
160 +checksum = "502da5464ccd04011667b11c435cb992822c2c0dbde1770c988480d312a0db2e"
161 +dependencies = [
162 + "libc",
163 + "pkg-config",
164 +]
165 +
149 166 [[package]]
150 167 name = "x11rb"
151 168 version = "0.13.2"
+1 −0 Cargo.toml
@@ -4,6 +4,7 @@ version = "0.1.0"
4 4 edition = "2024"
5 5
6 6 [dependencies]
7 +x11 = { version = "2.21", features = ["xlib", "xft"] }
7 8 x11rb = "0.13"
8 9 anyhow = "1"
9 10
+33 −23 flake.nix
@@ -3,29 +3,39 @@
3 3
4 4 inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
5 5
6 - outputs = {
7 - self,
8 - nixpkgs,
9 - }: let
10 - system = "x86_64-linux";
11 - pkgs = import nixpkgs {inherit system;};
12 - in {
13 - devShells.${system}.default = pkgs.mkShell {
14 - buildInputs = [
15 - pkgs.rustc
16 - pkgs.cargo
17 - pkgs.xorg.xclock
18 - pkgs.xterm
19 - pkgs.alacritty
20 - pkgs.just
21 - ];
22 - shellHook = ''
23 - export PS1="(oxwm-dev) $PS1"
24 - '';
6 + outputs =
7 + { self
8 + , nixpkgs
9 + ,
10 + }:
11 + let
12 + system = "x86_64-linux";
13 + pkgs = import nixpkgs { inherit system; };
14 + in
15 + {
16 + devShells.${system}.default = pkgs.mkShell {
17 + buildInputs = [
18 + pkgs.rustc
19 + pkgs.cargo
20 + pkgs.alacritty
21 + pkgs.just
22 + pkgs.xorg.libX11
23 + pkgs.xorg.libXft
24 + pkgs.xorg.libXrender
25 + pkgs.freetype
26 + pkgs.fontconfig
27 + pkgs.pkg-config
28 + ];
25 29
26 - RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}";
27 - };
30 + shellHook = ''
31 + export PKG_CONFIG_PATH="${pkgs.xorg.libX11.dev}/lib/pkgconfig:${pkgs.xorg.libXft.dev}/lib/pkgconfig:${pkgs.xorg.libXrender.dev}/lib/pkgconfig:${pkgs.freetype.dev}/lib/pkgconfig:${pkgs.fontconfig.dev}/lib/pkgconfig:$PKG_CONFIG_PATH"
32 +
33 + export PS1="(oxwm-dev) $PS1"
34 + '';
28 35
29 - formatter.${system} = pkgs.alejandra;
30 - };
36 + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}";
37 + };
38 +
39 + formatter.${system} = pkgs.alejandra;
40 + };
31 41 }
+34 −19 src/bar/bar.rs
@@ -1,9 +1,11 @@
1 1 use super::BAR_HEIGHT;
2 -use crate::config::{SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, TAGS};
2 +use super::font::{Font, FontDraw};
3 +use crate::config::{FONT, SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, TAGS};
3 4 use anyhow::Result;
4 5 use x11rb::COPY_DEPTH_FROM_PARENT;
5 6 use x11rb::connection::Connection;
6 7 use x11rb::protocol::xproto::*;
8 +use x11rb::rust_connection::RustConnection;
7 9
8 10 pub struct Bar {
9 11 window: Window,
@@ -11,12 +13,16 @@ pub struct Bar {
11 13 height: u16,
12 14 graphics_context: Gcontext,
13 15
16 + font: Font,
17 + font_draw: FontDraw,
18 + display: *mut x11::xlib::Display,
19 +
14 20 tag_widths: Vec<u16>,
15 21 needs_redraw: bool,
16 22 }
17 23
18 24 impl Bar {
19 - pub fn new<C: Connection>(connection: &C, screen: &Screen) -> Result<Self> {
25 + pub fn new(connection: &RustConnection, screen: &Screen, screen_num: usize) -> Result<Self> {
20 26 let window = connection.generate_id()?;
21 27 let graphics_context = connection.generate_id()?;
22 28
@@ -51,14 +57,28 @@ impl Bar {
51 57 connection.map_window(window)?;
52 58 connection.flush()?;
53 59
54 - // TODO: actual text width calculation when we add fonts
55 - let tag_widths = TAGS.iter().map(|tag| (tag.len() as u16 * 8) + 10).collect();
60 + let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
61 + if display.is_null() {
62 + anyhow::bail!("Failed to open X11 display for XFT");
63 + }
64 +
65 + let font = Font::new(display, screen_num as i32, FONT)?;
66 +
67 + let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
68 + let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
69 +
70 + let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?;
71 +
72 + let tag_widths = TAGS.iter().map(|tag| font.text_width(tag) + 10).collect();
56 73
57 74 Ok(Bar {
58 75 window,
59 76 width,
60 77 height,
61 78 graphics_context,
79 + font,
80 + font_draw,
81 + display,
62 82 tag_widths,
63 83 needs_redraw: true,
64 84 })
@@ -76,9 +96,9 @@ impl Bar {
76 96 self.needs_redraw = true;
77 97 }
78 98
79 - pub fn draw<C: Connection>(
99 + pub fn draw(
80 100 &mut self,
81 - connection: &C,
101 + connection: &RustConnection,
82 102 current_tags: u32,
83 103 occupied_tags: u32,
84 104 ) -> Result<()> {
@@ -150,24 +170,19 @@ impl Bar {
150 170 )?;
151 171 }
152 172
153 - connection.change_gc(
154 - self.graphics_context,
155 - &ChangeGCAux::new().foreground(scheme.foreground),
156 - )?;
157 -
158 - // TODO: Replace with actual font rendering later
159 - connection.image_text8(
160 - self.window,
161 - self.graphics_context,
162 - x_position + 5,
163 - self.height as i16 - 5,
164 - tag.as_bytes(),
165 - )?;
173 + let text_y = (self.height as i16 / 2) + (self.font.ascent() / 2);
174 + self.font_draw
175 + .draw_text(&self.font, scheme.foreground, x_position + 5, text_y, tag);
166 176
167 177 x_position += tag_width as i16;
168 178 }
169 179
170 180 connection.flush()?;
181 +
182 + unsafe {
183 + x11::xlib::XFlush(self.display);
184 + }
185 +
171 186 self.needs_redraw = false;
172 187
173 188 Ok(())
+136 −0 src/bar/font.rs new file mode 100644
@@ -0,0 +1,136 @@
1 +use anyhow::Result;
2 +use std::ffi::CString;
3 +use std::ptr;
4 +use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName};
5 +use x11::xlib::{Colormap, Display, Drawable, Visual};
6 +use x11::xrender::XRenderColor;
7 +
8 +pub struct Font {
9 + xft_font: *mut XftFont,
10 + display: *mut Display,
11 +}
12 +
13 +impl Font {
14 + pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self> {
15 + let font_name_cstr = CString::new(font_name)?;
16 +
17 + let xft_font = unsafe { XftFontOpenName(display, screen, font_name_cstr.as_ptr()) };
18 +
19 + if xft_font.is_null() {
20 + anyhow::bail!("Failed to load font: {}", font_name);
21 + }
22 +
23 + Ok(Font { xft_font, display })
24 + }
25 +
26 + pub fn height(&self) -> u16 {
27 + unsafe {
28 + let font = &*self.xft_font;
29 + font.height as u16
30 + }
31 + }
32 +
33 + pub fn ascent(&self) -> i16 {
34 + unsafe {
35 + let font = &*self.xft_font;
36 + font.ascent as i16
37 + }
38 + }
39 +
40 + pub fn text_width(&self, text: &str) -> u16 {
41 + unsafe {
42 + let mut extents = std::mem::zeroed();
43 + x11::xft::XftTextExtentsUtf8(
44 + self.display,
45 + self.xft_font,
46 + text.as_ptr(),
47 + text.len() as i32,
48 + &mut extents,
49 + );
50 + extents.width as u16
51 + }
52 + }
53 +}
54 +
55 +impl Drop for Font {
56 + fn drop(&mut self) {
57 + unsafe {
58 + if !self.xft_font.is_null() {
59 + x11::xft::XftFontClose(self.display, self.xft_font);
60 + }
61 + }
62 + }
63 +}
64 +
65 +pub struct FontDraw {
66 + xft_draw: *mut XftDraw,
67 +}
68 +
69 +impl FontDraw {
70 + pub fn new(
71 + display: *mut Display,
72 + drawable: Drawable,
73 + visual: *mut Visual,
74 + colormap: Colormap,
75 + ) -> Result<Self> {
76 + let xft_draw = unsafe { x11::xft::XftDrawCreate(display, drawable, visual, colormap) };
77 +
78 + if xft_draw.is_null() {
79 + anyhow::bail!("Failed to create XftDraw");
80 + }
81 +
82 + Ok(FontDraw { xft_draw })
83 + }
84 +
85 + pub fn draw_text(&self, font: &Font, color: u32, x: i16, y: i16, text: &str) {
86 + let red = ((color >> 16) & 0xFF) as u16;
87 + let green = ((color >> 8) & 0xFF) as u16;
88 + let blue = (color & 0xFF) as u16;
89 +
90 + let render_color = XRenderColor {
91 + red: red << 8 | red,
92 + green: green << 8 | green,
93 + blue: blue << 8 | blue,
94 + alpha: 0xFFFF,
95 + };
96 +
97 + let mut xft_color: XftColor = unsafe { std::mem::zeroed() };
98 +
99 + unsafe {
100 + x11::xft::XftColorAllocValue(
101 + x11::xft::XftDrawDisplay(self.xft_draw),
102 + x11::xft::XftDrawVisual(self.xft_draw),
103 + x11::xft::XftDrawColormap(self.xft_draw),
104 + &render_color,
105 + &mut xft_color,
106 + );
107 +
108 + XftDrawStringUtf8(
109 + self.xft_draw,
110 + &xft_color,
111 + font.xft_font,
112 + x as i32,
113 + y as i32,
114 + text.as_ptr(),
115 + text.len() as i32,
116 + );
117 +
118 + x11::xft::XftColorFree(
119 + x11::xft::XftDrawDisplay(self.xft_draw),
120 + x11::xft::XftDrawVisual(self.xft_draw),
121 + x11::xft::XftDrawColormap(self.xft_draw),
122 + &mut xft_color,
123 + );
124 + }
125 + }
126 +}
127 +
128 +impl Drop for FontDraw {
129 + fn drop(&mut self) {
130 + unsafe {
131 + if !self.xft_draw.is_null() {
132 + x11::xft::XftDrawDestroy(self.xft_draw);
133 + }
134 + }
135 + }
136 +}
+1 −0 src/bar/mod.rs
@@ -1,4 +1,5 @@
1 1 mod bar;
2 +mod font;
2 3 // mod widgets; // TODO: implement later
3 4
4 5 pub use bar::Bar;
+1 −0 src/config.rs
@@ -8,6 +8,7 @@ use x11rb::protocol::xproto::KeyButMask;
8 8 pub const BORDER_WIDTH: u32 = 1;
9 9 pub const BORDER_FOCUSED: u32 = 0x6dade3;
10 10 pub const BORDER_UNFOCUSED: u32 = 0xbbbbbb;
11 +pub const FONT: &str = "JetBrainsMono Nerd Font:size=12";
11 12
12 13 // ========================================
13 14 // DEFAULTS
+1 −1 src/window_manager.rs
@@ -46,7 +46,7 @@ impl WindowManager {
46 46 )?
47 47 .check()?;
48 48
49 - let bar = Bar::new(&connection, &screen)?;
49 + let bar = Bar::new(&connection, &screen, screen_number)?;
50 50
51 51 return Ok(Self {
52 52 connection,