tony / oxwm

Replace should_restart bool with Control enum and remove unused binary re-exec logic Browse files
tonybtw committed 2025-12-24 parent aaad324 commit 05d724f9a7a2c0d1380b49fc5c1de872d7b9a37a
Showing 4 changed files with 31 additions and 37 deletions. UnifiedSplit
+0 −1 resources/test-config.lua
@@ -28,7 +28,6 @@ local colors = {
28 blue = 0x6dade3, 28 blue = 0x6dade3,
29 } 29 }
30 30
31 -asdf
32 31
33 local modkey = "Mod1" 32 local modkey = "Mod1"
34 33
+3 −13 src/bin/main.rs
@@ -30,20 +30,10 @@ fn main() -> Result<(), MainError> {
30 window_manager.show_startup_config_error(warning); 30 window_manager.show_startup_config_error(warning);
31 } 31 }
32 32
33 - let should_restart = match window_manager.run() { 33 + if let Err(e) = window_manager.run() {
34 - Ok(sr) => sr, 34 + return Err(MainError::WmError(e));
35 - Err(e) => return Err(MainError::BadRestartStatus(e)),
36 - };
37 -
38 - drop(window_manager);
39 -
40 - if should_restart {
41 - use std::os::unix::process::CommandExt;
42 - let error = std::process::Command::new(&arguments[0])
43 - .args(&arguments[1..])
44 - .exec();
45 - eprintln!("Error: Failed to restart: {}", error);
46 } 35 }
36 +
47 Ok(()) 37 Ok(())
48 } 38 }
49 39
+2 −2 src/errors.rs
@@ -47,7 +47,7 @@ pub enum MainError {
47 FailedReadConfig(std::io::Error), 47 FailedReadConfig(std::io::Error),
48 FailedReadConfigTemplate(ConfigError), 48 FailedReadConfigTemplate(ConfigError),
49 CouldNotStartWm(WmError), 49 CouldNotStartWm(WmError),
50 - BadRestartStatus(WmError), 50 + WmError(WmError),
51 BadConfigPath, 51 BadConfigPath,
52 NoConfigPath, 52 NoConfigPath,
53 InvalidArguments, 53 InvalidArguments,
@@ -201,7 +201,7 @@ impl std::fmt::Debug for MainError {
201 write!(f, "{e}") 201 write!(f, "{e}")
202 } 202 }
203 FailedReadConfigTemplate(e) => write!(f, "{e}"), 203 FailedReadConfigTemplate(e) => write!(f, "{e}"),
204 - CouldNotStartWm(e) | BadRestartStatus(e) => write!(f, "{e}"), 204 + CouldNotStartWm(e) | WmError(e) => write!(f, "{e}"),
205 BadConfigPath => write!(f, "Given config path does not exist"), 205 BadConfigPath => write!(f, "Given config path does not exist"),
206 NoConfigPath => write!(f, "The --config switch requires a path value"), 206 NoConfigPath => write!(f, "The --config switch requires a path value"),
207 InvalidArguments => write!(f, "The arguments given are invalid try --help"), 207 InvalidArguments => write!(f, "The arguments given are invalid try --help"),
+26 −21 src/window_manager.rs
@@ -16,6 +16,11 @@ use x11rb::protocol::Event;
16 use x11rb::protocol::xproto::*; 16 use x11rb::protocol::xproto::*;
17 use x11rb::rust_connection::RustConnection; 17 use x11rb::rust_connection::RustConnection;
18 18
19 +enum Control {
20 + Continue,
21 + Quit,
22 +}
23 +
19 pub fn tag_mask(tag: usize) -> TagMask { 24 pub fn tag_mask(tag: usize) -> TagMask {
20 1 << tag 25 1 << tag
21 } 26 }
@@ -491,7 +496,7 @@ impl WindowManager {
491 Ok(()) 496 Ok(())
492 } 497 }
493 498
494 - pub fn run(&mut self) -> WmResult<bool> { 499 + pub fn run(&mut self) -> WmResult<()> {
495 println!("oxwm started on display {}", self.screen_number); 500 println!("oxwm started on display {}", self.screen_number);
496 501
497 self.grab_keys()?; 502 self.grab_keys()?;
@@ -503,8 +508,8 @@ impl WindowManager {
503 loop { 508 loop {
504 match self.connection.poll_for_event_with_sequence()? { 509 match self.connection.poll_for_event_with_sequence()? {
505 Some((event, _sequence)) => { 510 Some((event, _sequence)) => {
506 - if let Some(should_restart) = self.handle_event(event)? { 511 + if matches!(self.handle_event(event)?, Control::Quit) {
507 - return Ok(should_restart); 512 + return Ok(());
508 } 513 }
509 } 514 }
510 None => { 515 None => {
@@ -2678,7 +2683,7 @@ impl WindowManager {
2678 Ok(()) 2683 Ok(())
2679 } 2684 }
2680 2685
2681 - fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { 2686 + fn handle_event(&mut self, event: Event) -> WmResult<Control> {
2682 match event { 2687 match event {
2683 Event::KeyPress(ref key_event) if key_event.event == self.overlay.window() => { 2688 Event::KeyPress(ref key_event) if key_event.event == self.overlay.window() => {
2684 if self.overlay.is_visible() 2689 if self.overlay.is_visible()
@@ -2686,7 +2691,7 @@ impl WindowManager {
2686 { 2691 {
2687 eprintln!("Failed to hide overlay: {:?}", error); 2692 eprintln!("Failed to hide overlay: {:?}", error);
2688 } 2693 }
2689 - return Ok(None); 2694 + return Ok(Control::Continue);
2690 } 2695 }
2691 Event::ButtonPress(ref button_event) if button_event.event == self.overlay.window() => { 2696 Event::ButtonPress(ref button_event) if button_event.event == self.overlay.window() => {
2692 if self.overlay.is_visible() 2697 if self.overlay.is_visible()
@@ -2694,7 +2699,7 @@ impl WindowManager {
2694 { 2699 {
2695 eprintln!("Failed to hide overlay: {:?}", error); 2700 eprintln!("Failed to hide overlay: {:?}", error);
2696 } 2701 }
2697 - return Ok(None); 2702 + return Ok(Control::Continue);
2698 } 2703 }
2699 Event::Expose(ref expose_event) if expose_event.window == self.overlay.window() => { 2704 Event::Expose(ref expose_event) if expose_event.window == self.overlay.window() => {
2700 if self.overlay.is_visible() 2705 if self.overlay.is_visible()
@@ -2702,7 +2707,7 @@ impl WindowManager {
2702 { 2707 {
2703 eprintln!("Failed to draw overlay: {:?}", error); 2708 eprintln!("Failed to draw overlay: {:?}", error);
2704 } 2709 }
2705 - return Ok(None); 2710 + return Ok(Control::Continue);
2706 } 2711 }
2707 Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => { 2712 Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => {
2708 if self.keybind_overlay.is_visible() 2713 if self.keybind_overlay.is_visible()
@@ -2720,12 +2725,12 @@ impl WindowManager {
2720 } 2725 }
2721 } 2726 }
2722 } 2727 }
2723 - return Ok(None); 2728 + return Ok(Control::Continue);
2724 } 2729 }
2725 Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => { 2730 Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => {
2726 self.connection 2731 self.connection
2727 .allow_events(Allow::REPLAY_POINTER, e.time)?; 2732 .allow_events(Allow::REPLAY_POINTER, e.time)?;
2728 - return Ok(None); 2733 + return Ok(Control::Continue);
2729 } 2734 }
2730 Event::Expose(ref expose_event) 2735 Event::Expose(ref expose_event)
2731 if expose_event.window == self.keybind_overlay.window() => 2736 if expose_event.window == self.keybind_overlay.window() =>
@@ -2735,16 +2740,16 @@ impl WindowManager {
2735 { 2740 {
2736 eprintln!("Failed to draw keybind overlay: {:?}", error); 2741 eprintln!("Failed to draw keybind overlay: {:?}", error);
2737 } 2742 }
2738 - return Ok(None); 2743 + return Ok(Control::Continue);
2739 } 2744 }
2740 Event::MapRequest(event) => { 2745 Event::MapRequest(event) => {
2741 let attrs = match self.connection.get_window_attributes(event.window)?.reply() { 2746 let attrs = match self.connection.get_window_attributes(event.window)?.reply() {
2742 Ok(attrs) => attrs, 2747 Ok(attrs) => attrs,
2743 - Err(_) => return Ok(None), 2748 + Err(_) => return Ok(Control::Continue),
2744 }; 2749 };
2745 2750
2746 if attrs.override_redirect { 2751 if attrs.override_redirect {
2747 - return Ok(None); 2752 + return Ok(Control::Continue);
2748 } 2753 }
2749 2754
2750 if !self.windows.contains(&event.window) { 2755 if !self.windows.contains(&event.window) {
@@ -2763,11 +2768,11 @@ impl WindowManager {
2763 } 2768 }
2764 Event::PropertyNotify(event) => { 2769 Event::PropertyNotify(event) => {
2765 if event.state == Property::DELETE { 2770 if event.state == Property::DELETE {
2766 - return Ok(None); 2771 + return Ok(Control::Continue);
2767 } 2772 }
2768 2773
2769 if !self.clients.contains_key(&event.window) { 2774 if !self.clients.contains_key(&event.window) {
2770 - return Ok(None); 2775 + return Ok(Control::Continue);
2771 } 2776 }
2772 2777
2773 if event.atom == AtomEnum::WM_TRANSIENT_FOR.into() { 2778 if event.atom == AtomEnum::WM_TRANSIENT_FOR.into() {
@@ -2808,7 +2813,7 @@ impl WindowManager {
2808 } 2813 }
2809 Event::EnterNotify(event) => { 2814 Event::EnterNotify(event) => {
2810 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { 2815 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL {
2811 - return Ok(None); 2816 + return Ok(Control::Continue);
2812 } 2817 }
2813 if self.windows.contains(&event.event) { 2818 if self.windows.contains(&event.event) {
2814 if let Some(client) = self.clients.get(&event.event) 2819 if let Some(client) = self.clients.get(&event.event)
@@ -2823,7 +2828,7 @@ impl WindowManager {
2823 } 2828 }
2824 Event::MotionNotify(event) => { 2829 Event::MotionNotify(event) => {
2825 if event.event != self.root { 2830 if event.event != self.root {
2826 - return Ok(None); 2831 + return Ok(Control::Continue);
2827 } 2832 }
2828 2833
2829 if let Some(monitor_index) = 2834 if let Some(monitor_index) =
@@ -2842,7 +2847,7 @@ impl WindowManager {
2842 } 2847 }
2843 Event::KeyPress(event) => { 2848 Event::KeyPress(event) => {
2844 let Some(mapping) = &self.keyboard_mapping else { 2849 let Some(mapping) = &self.keyboard_mapping else {
2845 - return Ok(None); 2850 + return Ok(Control::Continue);
2846 }; 2851 };
2847 2852
2848 let result = keyboard::handle_key_press( 2853 let result = keyboard::handle_key_press(
@@ -2860,7 +2865,7 @@ impl WindowManager {
2860 self.update_bar()?; 2865 self.update_bar()?;
2861 2866
2862 match action { 2867 match action {
2863 - KeyAction::Quit => return Ok(Some(false)), 2868 + KeyAction::Quit => return Ok(Control::Quit),
2864 KeyAction::Restart => match self.try_reload_config() { 2869 KeyAction::Restart => match self.try_reload_config() {
2865 Ok(()) => { 2870 Ok(()) => {
2866 self.gaps_enabled = self.config.gaps_enabled; 2871 self.gaps_enabled = self.config.gaps_enabled;
@@ -3162,7 +3167,7 @@ impl WindowManager {
3162 } 3167 }
3163 Event::ClientMessage(event) => { 3168 Event::ClientMessage(event) => {
3164 if !self.clients.contains_key(&event.window) { 3169 if !self.clients.contains_key(&event.window) {
3165 - return Ok(None); 3170 + return Ok(Control::Continue);
3166 } 3171 }
3167 3172
3168 if event.type_ == self.atoms.net_wm_state { 3173 if event.type_ == self.atoms.net_wm_state {
@@ -3174,7 +3179,7 @@ impl WindowManager {
3174 1 => true, 3179 1 => true,
3175 0 => false, 3180 0 => false,
3176 2 => !self.fullscreen_windows.contains(&event.window), 3181 2 => !self.fullscreen_windows.contains(&event.window),
3177 - _ => return Ok(None), 3182 + _ => return Ok(Control::Continue),
3178 }; 3183 };
3179 self.set_window_fullscreen(event.window, fullscreen)?; 3184 self.set_window_fullscreen(event.window, fullscreen)?;
3180 } 3185 }
@@ -3225,7 +3230,7 @@ impl WindowManager {
3225 } 3230 }
3226 _ => {} 3231 _ => {}
3227 } 3232 }
3228 - Ok(None) 3233 + Ok(Control::Continue)
3229 } 3234 }
3230 3235
3231 fn apply_layout(&mut self) -> WmResult<()> { 3236 fn apply_layout(&mut self) -> WmResult<()> {