tony / oxwm

Added hot reload functionality, and it works.' Browse files
tonybtw committed 2025-10-04 parent 06c6741 commit 6f540a5d646224005f531ba137f841afd9c8da24
Showing 5 changed files with 61 additions and 28 deletions. UnifiedSplit
+2 −0 src/config.rs
@@ -84,6 +84,7 @@ pub const KEYBINDINGS: &[Key] = &[
84 84 Key::new(&[MODKEY], keycodes::D, KeyAction::Spawn, Arg::Array(DMENU_CMD)),
85 85 Key::new(&[MODKEY], keycodes::Q, KeyAction::KillClient, Arg::None),
86 86 Key::new(&[MODKEY, SHIFT], keycodes::Q, KeyAction::Quit, Arg::None),
87 + Key::new(&[MODKEY, SHIFT], keycodes::R, KeyAction::Restart, Arg::None),
87 88 Key::new(&[MODKEY], keycodes::J, KeyAction::FocusStack, Arg::Int(-1)),
88 89 Key::new(&[MODKEY], keycodes::K, KeyAction::FocusStack, Arg::Int(1)),
89 90
@@ -149,3 +150,4 @@ pub const STATUS_BLOCKS: &[BlockConfig] = &[
149 150 ];
150 151
151 152 const SHIFT: KeyButMask = KeyButMask::SHIFT;
153 +pub const WM_BINARY: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/target/release/oxwm");
+1 −0 src/keyboard/handlers.rs
@@ -9,6 +9,7 @@ pub enum KeyAction {
9 9 KillClient,
10 10 FocusStack,
11 11 Quit,
12 + Restart,
12 13 ViewTag,
13 14 MoveToTag,
14 15 None,
+1 −0 src/keyboard/keycodes.rs
@@ -21,6 +21,7 @@ pub const F: u8 = 41;
21 21 pub const J: u8 = 44;
22 22 pub const K: u8 = 45;
23 23 pub const L: u8 = 46;
24 +pub const R: u8 = 27;
24 25
25 26 pub const KEY_1: u8 = 10;
26 27 pub const KEY_2: u8 = 11;
+15 −2 src/main.rs
@@ -6,6 +6,19 @@ mod layout;
6 6 mod window_manager;
7 7
8 8 fn main() -> Result<()> {
9 - let mut window_manager = window_manager::WindowManager::new()?;
10 - return window_manager.run();
9 + loop {
10 + let mut window_manager = window_manager::WindowManager::new()?;
11 + let should_restart = window_manager.run()?;
12 +
13 + if !should_restart {
14 + break;
15 + }
16 +
17 + use std::os::unix::process::CommandExt;
18 + let err = std::process::Command::new(config::WM_BINARY).exec();
19 + eprintln!("Failed to restart: {}", err);
20 + break;
21 + }
22 +
23 + Ok(())
11 24 }
+42 −26 src/window_manager.rs
@@ -1,13 +1,13 @@
1 1 use crate::bar::Bar;
2 2 use crate::config::{BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, TAG_COUNT};
3 3 use crate::keyboard::{self, Arg, KeyAction};
4 -use crate::layout::tiling::TilingLayout;
5 4 use crate::layout::Layout;
5 +use crate::layout::tiling::TilingLayout;
6 6 use anyhow::Result;
7 7
8 8 use x11rb::connection::Connection;
9 -use x11rb::protocol::xproto::*;
10 9 use x11rb::protocol::Event;
10 +use x11rb::protocol::xproto::*;
11 11 use x11rb::rust_connection::RustConnection;
12 12
13 13 pub type TagMask = u32;
@@ -48,7 +48,7 @@ impl WindowManager {
48 48
49 49 let bar = Bar::new(&connection, &screen, screen_number)?;
50 50
51 - return Ok(Self {
51 + let mut window_manger = Self {
52 52 connection,
53 53 screen_number,
54 54 root,
@@ -59,21 +59,46 @@ impl WindowManager {
59 59 window_tags: std::collections::HashMap::new(),
60 60 selected_tags: tag_mask(0),
61 61 bar,
62 - });
62 + };
63 +
64 + window_manger.scan_existing_windows()?;
65 +
66 + Ok(window_manger)
67 + }
68 +
69 + fn scan_existing_windows(&mut self) -> Result<()> {
70 + let tree = self.connection.query_tree(self.root)?.reply()?;
71 +
72 + for &window in &tree.children {
73 + if let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() {
74 + if window != self.bar.window()
75 + && attrs.map_state == MapState::VIEWABLE
76 + && !attrs.override_redirect
77 + {
78 + self.windows.push(window);
79 + self.window_tags.insert(window, self.selected_tags);
80 + }
81 + }
82 + }
83 + if let Some(&first) = self.windows.first() {
84 + self.set_focus(Some(first))?;
85 + }
86 + Ok(())
63 87 }
64 88
65 - pub fn run(&mut self) -> Result<()> {
89 + pub fn run(&mut self) -> Result<bool> {
66 90 println!("oxwm started on display {}", self.screen_number);
67 91
68 92 keyboard::setup_keybinds(&self.connection, self.root)?;
69 -
70 93 self.update_bar()?;
71 94
72 95 loop {
73 96 self.bar.update_blocks()?;
74 97
75 98 if let Ok(Some(event)) = self.connection.poll_for_event() {
76 - self.handle_event(event)?;
99 + if let Some(should_restart) = self.handle_event(event)? {
100 + return Ok(should_restart);
101 + }
77 102 }
78 103
79 104 if self.bar.needs_redraw() {
@@ -84,19 +109,6 @@ impl WindowManager {
84 109 }
85 110 }
86 111
87 - // pub fn run(&mut self) -> Result<()> {
88 - // println!("oxwm started on display {}", self.screen_number);
89 - //
90 - // keyboard::setup_keybinds(&self.connection, self.root)?;
91 - //
92 - // self.update_bar()?;
93 - //
94 - // loop {
95 - // let event = self.connection.wait_for_event()?;
96 - // self.handle_event(event)?;
97 - // }
98 - // }
99 -
100 112 fn update_bar(&mut self) -> Result<()> {
101 113 let mut occupied_tags: TagMask = 0;
102 114 for &tags in self.window_tags.values() {
@@ -139,8 +151,8 @@ impl WindowManager {
139 151 self.cycle_focus(*direction)?;
140 152 }
141 153 }
142 - KeyAction::Quit => {
143 - std::process::exit(0);
154 + KeyAction::Quit | KeyAction::Restart => {
155 + //no-op
144 156 }
145 157 KeyAction::ViewTag => {
146 158 if let Arg::Int(tag_index) = arg {
@@ -280,7 +292,7 @@ impl WindowManager {
280 292 Ok(())
281 293 }
282 294
283 - fn handle_event(&mut self, event: Event) -> Result<()> {
295 + fn handle_event(&mut self, event: Event) -> Result<Option<bool>> {
284 296 match event {
285 297 Event::MapRequest(event) => {
286 298 self.connection.map_window(event.window)?;
@@ -302,7 +314,12 @@ impl WindowManager {
302 314 }
303 315 Event::KeyPress(event) => {
304 316 let (action, arg) = keyboard::handle_key_press(event)?;
305 - self.handle_key_action(action, arg)?;
317 +
318 + match action {
319 + KeyAction::Quit => return Ok(Some(false)),
320 + KeyAction::Restart => return Ok(Some(true)),
321 + _ => self.handle_key_action(action, arg)?,
322 + }
306 323 }
307 324 Event::ButtonPress(event) => {
308 325 if event.event == self.bar.window() {
@@ -319,9 +336,8 @@ impl WindowManager {
319 336 }
320 337 _ => {}
321 338 }
322 - Ok(())
339 + Ok(None)
323 340 }
324 -
325 341 fn apply_layout(&self) -> Result<()> {
326 342 let screen_width = self.screen.width_in_pixels as u32;
327 343 let screen_height = self.screen.height_in_pixels as u32;