tony / oxwm

Merge branch 'master' into floating Browse files
Emzy committed 2025-10-14 parents 7efd8c0 fe6d8c8 commit 246177aa3b75aa4efb91c0122cc7536e8a3b504d
Showing 6 changed files with 142 additions and 51 deletions. UnifiedSplit
+6 −7 src/bar/bar.rs
@@ -1,7 +1,7 @@
1 1 use super::blocks::Block;
2 2 use super::font::{Font, FontDraw};
3 3 use crate::Config;
4 -use anyhow::Result;
4 +use crate::errors::X11Error;
5 5 use std::time::Instant;
6 6 use x11rb::COPY_DEPTH_FROM_PARENT;
7 7 use x11rb::connection::Connection;
@@ -38,7 +38,7 @@ impl Bar {
38 38 screen: &Screen,
39 39 screen_num: usize,
40 40 config: &Config,
41 - ) -> Result<Self> {
41 + ) -> Result<Self, X11Error> {
42 42 let window = connection.generate_id()?;
43 43 let graphics_context = connection.generate_id()?;
44 44
@@ -46,8 +46,9 @@ impl Bar {
46 46
47 47 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
48 48 if display.is_null() {
49 - anyhow::bail!("Failed to open X11 display for XFT");
49 + return Err(X11Error::DisplayOpenFailed.into());
50 50 }
51 +
51 52 let font = Font::new(display, screen_num as i32, &config.font)?;
52 53
53 54 let height = (font.height() as f32 * 1.4) as u16;
@@ -143,7 +144,7 @@ impl Bar {
143 144 self.needs_redraw = true;
144 145 }
145 146
146 - pub fn update_blocks(&mut self) -> Result<()> {
147 + pub fn update_blocks(&mut self) {
147 148 let now = Instant::now();
148 149 let mut changed = false;
149 150
@@ -168,8 +169,6 @@ impl Bar {
168 169 self.status_text = parts.join("");
169 170 self.needs_redraw = true;
170 171 }
171 -
172 - Ok(())
173 172 }
174 173
175 174 pub fn draw(
@@ -177,7 +176,7 @@ impl Bar {
177 176 connection: &RustConnection,
178 177 current_tags: u32,
179 178 occupied_tags: u32,
180 - ) -> Result<()> {
179 + ) -> Result<(), X11Error> {
181 180 if !self.needs_redraw {
182 181 return Ok(());
183 182 }
+8 −6 src/bar/font.rs
@@ -1,22 +1,24 @@
1 -use anyhow::Result;
2 1 use std::ffi::CString;
3 2 use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName};
4 3 use x11::xlib::{Colormap, Display, Drawable, Visual};
5 4 use x11::xrender::XRenderColor;
6 5
6 +use crate::errors::X11Error;
7 +
7 8 pub struct Font {
8 9 xft_font: *mut XftFont,
9 10 display: *mut Display,
10 11 }
11 12
12 13 impl Font {
13 - pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self> {
14 - let font_name_cstr = CString::new(font_name)?;
14 + pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self, X11Error> {
15 + let font_name_cstr =
16 + CString::new(font_name).map_err(|_| X11Error::FontLoadFailed(font_name.to_string()))?;
15 17
16 18 let xft_font = unsafe { XftFontOpenName(display, screen, font_name_cstr.as_ptr()) };
17 19
18 20 if xft_font.is_null() {
19 - anyhow::bail!("Failed to load font: {}", font_name);
21 + return Err(X11Error::FontLoadFailed(font_name.to_string()));
20 22 }
21 23
22 24 Ok(Font { xft_font, display })
@@ -71,11 +73,11 @@ impl FontDraw {
71 73 drawable: Drawable,
72 74 visual: *mut Visual,
73 75 colormap: Colormap,
74 - ) -> Result<Self> {
76 + ) -> Result<Self, X11Error> {
75 77 let xft_draw = unsafe { x11::xft::XftDrawCreate(display, drawable, visual, colormap) };
76 78
77 79 if xft_draw.is_null() {
78 - anyhow::bail!("Failed to create XftDraw");
80 + return Err(X11Error::DrawCreateFailed);
79 81 }
80 82
81 83 Ok(FontDraw { xft_draw })
+87 −0 src/errors.rs new file mode 100644
@@ -0,0 +1,87 @@
1 +#[derive(Debug)]
2 +pub enum WmError {
3 + X11(X11Error),
4 + Io(std::io::Error),
5 + Anyhow(anyhow::Error),
6 +}
7 +
8 +#[derive(Debug)]
9 +pub enum X11Error {
10 + ConnectError(x11rb::errors::ConnectError),
11 + ConnectionError(x11rb::errors::ConnectionError),
12 + ReplyError(x11rb::errors::ReplyError),
13 + ReplyOrIdError(x11rb::errors::ReplyOrIdError),
14 + DisplayOpenFailed,
15 + FontLoadFailed(String),
16 + DrawCreateFailed,
17 +}
18 +
19 +impl std::fmt::Display for WmError {
20 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 + match self {
22 + Self::X11(error) => write!(f, "{}", error),
23 + Self::Io(error) => write!(f, "{}", error),
24 + Self::Anyhow(error) => write!(f, "{}", error),
25 + }
26 + }
27 +}
28 +
29 +impl std::error::Error for WmError {}
30 +
31 +impl std::fmt::Display for X11Error {
32 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 + match self {
34 + Self::ConnectError(err) => write!(f, "{}", err),
35 + Self::ConnectionError(err) => write!(f, "{}", err),
36 + Self::ReplyError(err) => write!(f, "{}", err),
37 + Self::ReplyOrIdError(err) => write!(f, "{}", err),
38 + Self::DisplayOpenFailed => write!(f, "failed to open X11 display"),
39 + Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name),
40 + Self::DrawCreateFailed => write!(f, "failed to create XftDraw"),
41 + }
42 + }
43 +}
44 +
45 +impl std::error::Error for X11Error {}
46 +
47 +impl<T: Into<X11Error>> From<T> for WmError {
48 + fn from(value: T) -> Self {
49 + Self::X11(value.into())
50 + }
51 +}
52 +
53 +impl From<std::io::Error> for WmError {
54 + fn from(value: std::io::Error) -> Self {
55 + Self::Io(value)
56 + }
57 +}
58 +
59 +impl From<anyhow::Error> for WmError {
60 + fn from(value: anyhow::Error) -> Self {
61 + Self::Anyhow(value)
62 + }
63 +}
64 +
65 +impl From<x11rb::errors::ConnectError> for X11Error {
66 + fn from(value: x11rb::errors::ConnectError) -> Self {
67 + X11Error::ConnectError(value)
68 + }
69 +}
70 +
71 +impl From<x11rb::errors::ConnectionError> for X11Error {
72 + fn from(value: x11rb::errors::ConnectionError) -> Self {
73 + X11Error::ConnectionError(value)
74 + }
75 +}
76 +
77 +impl From<x11rb::errors::ReplyError> for X11Error {
78 + fn from(value: x11rb::errors::ReplyError) -> Self {
79 + X11Error::ReplyError(value)
80 + }
81 +}
82 +
83 +impl From<x11rb::errors::ReplyOrIdError> for X11Error {
84 + fn from(value: x11rb::errors::ReplyOrIdError) -> Self {
85 + X11Error::ReplyOrIdError(value)
86 + }
87 +}
+12 −11 src/keyboard/handlers.rs
@@ -1,9 +1,11 @@
1 +use std::io;
1 2 use std::process::Command;
2 3
3 -use anyhow::Result;
4 4 use x11rb::connection::Connection;
5 5 use x11rb::protocol::xproto::*;
6 6
7 +use crate::errors::X11Error;
8 +
7 9 #[derive(Debug, Copy, Clone)]
8 10 pub enum KeyAction {
9 11 Spawn,
@@ -68,7 +70,7 @@ pub fn setup_keybinds(
68 70 connection: &impl Connection,
69 71 root: Window,
70 72 keybindings: &[Key],
71 -) -> Result<()> {
73 +) -> Result<(), X11Error> {
72 74 for keybinding in keybindings {
73 75 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
74 76
@@ -84,22 +86,22 @@ pub fn setup_keybinds(
84 86 Ok(())
85 87 }
86 88
87 -pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> Result<(KeyAction, Arg)> {
89 +pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> (KeyAction, Arg) {
88 90 for keybinding in keybindings {
89 91 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
90 92
91 93 if event.detail == keybinding.key && event.state == modifier_mask.into() {
92 - return Ok((keybinding.func, keybinding.arg.clone()));
94 + return (keybinding.func, keybinding.arg.clone());
93 95 }
94 96 }
95 97
96 - Ok((KeyAction::None, Arg::None))
98 + (KeyAction::None, Arg::None)
97 99 }
98 100
99 -pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> Result<()> {
100 - use std::io::ErrorKind;
101 - match action {
102 - KeyAction::Spawn => match arg {
101 +pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> io::Result<()> {
102 + use io::ErrorKind;
103 + if let KeyAction::Spawn = action {
104 + match arg {
103 105 Arg::Str(command) => match Command::new(command).spawn() {
104 106 Err(err) if err.kind() == ErrorKind::NotFound => {
105 107 eprintln!(
@@ -127,8 +129,7 @@ pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> Result<()> {
127 129 }
128 130 }
129 131 _ => {}
130 - },
131 - _ => {}
132 + }
132 133 }
133 134
134 135 Ok(())
+1 −0 src/lib.rs
@@ -1,4 +1,5 @@
1 1 pub mod bar;
2 +pub mod errors;
2 3 pub mod keyboard;
3 4 pub mod layout;
4 5 pub mod window_manager;
+28 −27 src/window_manager.rs
@@ -1,12 +1,11 @@
1 1 use crate::Config;
2 2 use crate::bar::Bar;
3 +use crate::errors::WmError;
3 4 use crate::keyboard::{self, Arg, KeyAction, handlers};
4 5 use crate::layout::GapConfig;
5 6 use crate::layout::Layout;
6 7 use crate::layout::tiling::TilingLayout;
7 -use anyhow::Result;
8 8 use std::collections::HashSet;
9 -use std::path::PathBuf;
10 9 use x11rb::cursor::Handle as CursorHandle;
11 10
12 11 use x11rb::connection::Connection;
@@ -36,8 +35,10 @@ pub struct WindowManager {
36 35 bar: Bar,
37 36 }
38 37
38 +type WmResult<T> = Result<T, WmError>;
39 +
39 40 impl WindowManager {
40 - pub fn new(config: Config) -> Result<Self> {
41 + pub fn new(config: Config) -> WmResult<Self> {
41 42 let (connection, screen_number) = x11rb::connect(None)?;
42 43 let root = connection.setup().roots[screen_number].root;
43 44 let screen = connection.setup().roots[screen_number].clone();
@@ -121,7 +122,7 @@ impl WindowManager {
121 122 connection: &RustConnection,
122 123 root: Window,
123 124 tag_count: usize,
124 - ) -> Result<TagMask> {
125 + ) -> WmResult<TagMask> {
125 126 let net_current_desktop = connection
126 127 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
127 128 .reply()?
@@ -151,7 +152,7 @@ impl WindowManager {
151 152 Ok(tag_mask(0))
152 153 }
153 154
154 - fn scan_existing_windows(&mut self) -> Result<()> {
155 + fn scan_existing_windows(&mut self) -> WmResult<()> {
155 156 let tree = self.connection.query_tree(self.root)?.reply()?;
156 157 let net_client_info = self
157 158 .connection
@@ -218,7 +219,7 @@ impl WindowManager {
218 219 Ok(())
219 220 }
220 221
221 - fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> Result<TagMask> {
222 + fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> {
222 223 match self
223 224 .connection
224 225 .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)?
@@ -245,7 +246,7 @@ impl WindowManager {
245 246 Ok(self.selected_tags)
246 247 }
247 248
248 - fn save_client_tag(&self, window: Window, tag: TagMask) -> Result<()> {
249 + fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> {
249 250 let net_client_info = self
250 251 .connection
251 252 .intern_atom(false, b"_NET_CLIENT_INFO")?
@@ -268,7 +269,7 @@ impl WindowManager {
268 269 Ok(())
269 270 }
270 271
271 - fn set_wm_state(&self, window: Window, state: u32) -> Result<()> {
272 + fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> {
272 273 let wm_state_atom = self
273 274 .connection
274 275 .intern_atom(false, b"WM_STATE")?
@@ -292,14 +293,14 @@ impl WindowManager {
292 293 Ok(())
293 294 }
294 295
295 - pub fn run(&mut self) -> Result<bool> {
296 + pub fn run(&mut self) -> WmResult<bool> {
296 297 println!("oxwm started on display {}", self.screen_number);
297 298
298 299 keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?;
299 300 self.update_bar()?;
300 301
301 302 loop {
302 - self.bar.update_blocks()?;
303 + self.bar.update_blocks();
303 304
304 305 if let Ok(Some(event)) = self.connection.poll_for_event() {
305 306 if let Some(should_restart) = self.handle_event(event)? {
@@ -315,7 +316,7 @@ impl WindowManager {
315 316 }
316 317 }
317 318
318 - fn toggle_floating(&mut self) -> Result<()> {
319 + fn toggle_floating(&mut self) -> WmResult<()> {
319 320 if let Some(focused) = self.focused_window {
320 321 if self.floating_windows.contains(&focused) {
321 322 self.floating_windows.remove(&focused);
@@ -333,7 +334,7 @@ impl WindowManager {
333 334 Ok(())
334 335 }
335 336
336 - fn toggle_fullscreen(&mut self) -> Result<()> {
337 + fn toggle_fullscreen(&mut self) -> WmResult<()> {
337 338 if let Some(focused) = self.focused_window {
338 339 if self.fullscreen_window == Some(focused) {
339 340 self.fullscreen_window = None;
@@ -367,7 +368,7 @@ impl WindowManager {
367 368 Ok(())
368 369 }
369 370
370 - fn update_bar(&mut self) -> Result<()> {
371 + fn update_bar(&mut self) -> WmResult<()> {
371 372 let mut occupied_tags: TagMask = 0;
372 373 for &tags in self.window_tags.values() {
373 374 occupied_tags |= tags;
@@ -379,7 +380,7 @@ impl WindowManager {
379 380 Ok(())
380 381 }
381 382
382 - fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
383 + fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> {
383 384 match action {
384 385 KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?,
385 386 KeyAction::KillClient => {
@@ -453,7 +454,7 @@ impl WindowManager {
453 454 .collect()
454 455 }
455 456
456 - fn update_window_visibility(&self) -> Result<()> {
457 + fn update_window_visibility(&self) -> WmResult<()> {
457 458 for &window in &self.windows {
458 459 if self.is_window_visible(window) {
459 460 self.connection.map_window(window)?;
@@ -465,7 +466,7 @@ impl WindowManager {
465 466 Ok(())
466 467 }
467 468
468 - pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
469 + pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> {
469 470 if tag_index >= self.config.tags.len() {
470 471 return Ok(());
471 472 }
@@ -489,7 +490,7 @@ impl WindowManager {
489 490 Ok(())
490 491 }
491 492
492 - fn save_selected_tags(&self) -> Result<()> {
493 + fn save_selected_tags(&self) -> WmResult<()> {
493 494 let net_current_desktop = self
494 495 .connection
495 496 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
@@ -513,7 +514,7 @@ impl WindowManager {
513 514 Ok(())
514 515 }
515 516
516 - pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
517 + pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> {
517 518 if tag_index >= self.config.tags.len() {
518 519 return Ok(());
519 520 }
@@ -532,7 +533,7 @@ impl WindowManager {
532 533 Ok(())
533 534 }
534 535
535 - pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
536 + pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> {
536 537 let visible = self.visible_windows();
537 538
538 539 if visible.is_empty() {
@@ -558,7 +559,7 @@ impl WindowManager {
558 559 Ok(())
559 560 }
560 561
561 - pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
562 + pub fn set_focus(&mut self, window: Option<Window>) -> WmResult<()> {
562 563 self.focused_window = window;
563 564
564 565 if let Some(win) = window {
@@ -571,7 +572,7 @@ impl WindowManager {
571 572 Ok(())
572 573 }
573 574
574 - fn update_focus_visuals(&self) -> Result<()> {
575 + fn update_focus_visuals(&self) -> WmResult<()> {
575 576 for &window in &self.windows {
576 577 let (border_color, border_width) = if self.focused_window == Some(window) {
577 578 (self.config.border_focused, self.config.border_width)
@@ -593,7 +594,7 @@ impl WindowManager {
593 594 Ok(())
594 595 }
595 596
596 - fn move_mouse(&mut self, window: Window) -> Result<()> {
597 + fn move_mouse(&mut self, window: Window) -> WmResult<()> {
597 598 self.floating_windows.insert(window);
598 599
599 600 let geometry = self.connection.get_geometry(window)?.reply()?;
@@ -646,7 +647,7 @@ impl WindowManager {
646 647 Ok(())
647 648 }
648 649
649 - fn resize_mouse(&mut self, window: Window) -> Result<()> {
650 + fn resize_mouse(&mut self, window: Window) -> WmResult<()> {
650 651 self.floating_windows.insert(window);
651 652
652 653 let geometry = self.connection.get_geometry(window)?.reply()?;
@@ -710,7 +711,7 @@ impl WindowManager {
710 711 Ok(())
711 712 }
712 713
713 - fn handle_event(&mut self, event: Event) -> Result<Option<bool>> {
714 + fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> {
714 715 match event {
715 716 Event::MapRequest(event) => {
716 717 let attrs = match self.connection.get_window_attributes(event.window)?.reply() {
@@ -760,7 +761,7 @@ impl WindowManager {
760 761 }
761 762 }
762 763 Event::KeyPress(event) => {
763 - let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings)?;
764 + let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings);
764 765 match action {
765 766 KeyAction::Quit => return Ok(Some(false)),
766 767 KeyAction::Restart => return Ok(Some(true)),
@@ -793,7 +794,7 @@ impl WindowManager {
793 794 Ok(None)
794 795 }
795 796
796 - fn apply_layout(&self) -> Result<()> {
797 + fn apply_layout(&self) -> WmResult<()> {
797 798 if self.fullscreen_window.is_some() {
798 799 return Ok(());
799 800 }
@@ -849,7 +850,7 @@ impl WindowManager {
849 850 Ok(())
850 851 }
851 852
852 - fn remove_window(&mut self, window: Window) -> Result<()> {
853 + fn remove_window(&mut self, window: Window) -> WmResult<()> {
853 854 let initial_count = self.windows.len();
854 855 self.windows.retain(|&w| w != window);
855 856 self.window_tags.remove(&window);