diff --git a/src/main.rs b/src/main.rs index 250129f..e0cecfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,44 +1,11 @@ -use std::process::exit; +mod setup; -use anyhow::anyhow; +use setup::{become_wm, handle_args}; use x11rb::{ - connect, connection::Connection, protocol::xproto::{ConnectionExt, Screen}, + connect, connection::Connection, }; -fn become_wm(conn: &C, screen: &Screen) -> anyhow::Result<()> { - use x11rb::{protocol::xproto::{ChangeWindowAttributesAux, EventMask}, xcb_ffi::ReplyError}; - - let change = ChangeWindowAttributesAux::default() - .event_mask(EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY); - - let res = conn.change_window_attributes(screen.root, &change)?.check(); - - if let Err(ReplyError::X11Error(ref error)) = res { - use x11rb::protocol::ErrorKind; - - if error.error_kind == ErrorKind::Access { - eprintln!("rswm: another window manager is already running"); - exit(1); - } - } - - res.map_err(|e| anyhow!(e)) -} - -fn handle_args() { - let mut args = std::env::args().skip(1); - if let Some(arg) = args.next() { - if arg == "-v" { - println!("rswm-{}", env!("CARGO_PKG_VERSION")); - exit(0); - } else { - eprintln!("usage: rswm [-v]"); - exit(1); - } - } -} - fn main() -> anyhow::Result<()> { handle_args(); diff --git a/src/setup.rs b/src/setup.rs new file mode 100644 index 0000000..5ee7cbe --- /dev/null +++ b/src/setup.rs @@ -0,0 +1,36 @@ +use anyhow::anyhow; + +use x11rb::{connection::Connection, protocol::xproto::{ConnectionExt, Screen}}; + +pub fn become_wm(conn: &C, screen: &Screen) -> anyhow::Result<()> { + use x11rb::{protocol::xproto::{ChangeWindowAttributesAux, EventMask}, xcb_ffi::ReplyError}; + + let change = ChangeWindowAttributesAux::default() + .event_mask(EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY); + + let res = conn.change_window_attributes(screen.root, &change)?.check(); + + if let Err(ReplyError::X11Error(ref error)) = res { + use x11rb::protocol::ErrorKind; + + if error.error_kind == ErrorKind::Access { + eprintln!("rswm: another window manager is already running"); + std::process::exit(1); + } + } + + res.map_err(|e| anyhow!(e)) +} + +pub fn handle_args() { + let mut args = std::env::args().skip(1); + if let Some(arg) = args.next() { + if arg == "-v" { + println!("rswm-{}", env!("CARGO_PKG_VERSION")); + std::process::exit(0); + } else { + eprintln!("usage: rswm [-v]"); + std::process::exit(1); + } + } +}