relocate setup functions to module

main
Zynh0722 2024-01-31 18:28:54 -08:00
parent 68c03caaee
commit f6654ed34a
2 changed files with 39 additions and 36 deletions

View File

@ -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<C: Connection>(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();

36
src/setup.rs Normal file
View File

@ -0,0 +1,36 @@
use anyhow::anyhow;
use x11rb::{connection::Connection, protocol::xproto::{ConnectionExt, Screen}};
pub fn become_wm<C: Connection>(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);
}
}
}