From 8f9223464bbc0e6b1d163a760491305ce447834a Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Thu, 5 Dec 2024 18:29:33 -0500 Subject: [PATCH] move SW into state and finally have to pass &mut State everywhere --- src/config.rs | 2 +- src/config/key.rs | 8 +-- src/handlers.rs | 38 +++++------ src/key_handlers.rs | 44 ++++++------- src/layouts.rs | 4 +- src/lib.rs | 8 ++- src/main.rs | 154 +++++++++++++++++++++++--------------------- src/tests.rs | 36 +++++------ 8 files changed, 152 insertions(+), 142 deletions(-) diff --git a/src/config.rs b/src/config.rs index 451ba1d..3e509b8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -284,7 +284,7 @@ fn get_layouts( // LEAK but okay since this should hang around for the whole program let symbol = symbol.into_raw(); - type F = fn(&State, *mut Monitor); + type F = fn(&mut State, *mut Monitor); let arrange = match &layout[1] { Value::Str(s) if s == "tile" => Some(tile as F), Value::Str(s) if s == "monocle" => Some(monocle as F), diff --git a/src/config/key.rs b/src/config/key.rs index 94a0fed..ebbb7d4 100644 --- a/src/config/key.rs +++ b/src/config/key.rs @@ -9,7 +9,7 @@ use x11::xlib::KeySym; pub struct Key { pub mod_: c_uint, pub keysym: KeySym, - pub func: Option, + pub func: Option, pub arg: Arg, } @@ -17,7 +17,7 @@ impl Key { pub const fn new( mod_: c_uint, keysym: u32, - func: fn(&State, *const Arg), + func: fn(&mut State, *const Arg), arg: Arg, ) -> Self { Self { mod_, keysym: keysym as KeySym, func: Some(func), arg } @@ -34,10 +34,10 @@ pub(crate) fn conv(opt: Option<&T>) -> Result { } } -type FnMap = HashMap<&'static str, fn(&State, *const Arg)>; +type FnMap = HashMap<&'static str, fn(&mut State, *const Arg)>; pub(super) static FUNC_MAP: LazyLock = LazyLock::new(|| { use crate::key_handlers::*; - type FN = fn(&State, *const Arg); + type FN = fn(&mut State, *const Arg); HashMap::from([ ("focusmon", focusmon as FN), ("focusstack", focusstack as FN), diff --git a/src/handlers.rs b/src/handlers.rs index faef464..41bb1bd 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -38,11 +38,11 @@ use crate::{ XEMBED_EMBEDDED_VERSION, XEMBED_FOCUS_IN, XEMBED_MODALITY_ON, XEMBED_WINDOW_ACTIVATE, }, - DRW, MONS, NORMAL_STATE, ROOT, SCHEME, SELMON, SH, STEXT, SW, SYSTRAY, + DRW, MONS, NORMAL_STATE, ROOT, SCHEME, SELMON, SH, STEXT, SYSTRAY, WITHDRAWN_STATE, }; -pub(crate) fn buttonpress(state: &State, e: *mut XEvent) { +pub(crate) fn buttonpress(state: &mut State, e: *mut XEvent) { unsafe { let mut arg = Arg::I(0); let ev = &(*e).button; @@ -111,7 +111,7 @@ pub(crate) fn buttonpress(state: &State, e: *mut XEvent) { } } -pub(crate) fn clientmessage(state: &State, e: *mut XEvent) { +pub(crate) fn clientmessage(state: &mut State, e: *mut XEvent) { unsafe { let cme = &(*e).client_message; let mut c = wintoclient(cme.window); @@ -279,7 +279,7 @@ pub(crate) fn clientmessage(state: &State, e: *mut XEvent) { } } -pub(crate) fn configurerequest(state: &State, e: *mut XEvent) { +pub(crate) fn configurerequest(state: &mut State, e: *mut XEvent) { unsafe { let ev = &(*e).configure_request; let c = wintoclient(ev.window); @@ -357,16 +357,16 @@ pub(crate) fn configurerequest(state: &State, e: *mut XEvent) { } } -pub(crate) fn configurenotify(state: &State, e: *mut XEvent) { +pub(crate) fn configurenotify(state: &mut State, e: *mut XEvent) { unsafe { let ev = &mut (*e).configure; /* TODO: updategeom handling sucks, needs to be simplified */ if ev.window == ROOT { - let dirty = SW != ev.width || SH != ev.height; - SW = ev.width; + let dirty = state.sw != ev.width || SH != ev.height; + state.sw = ev.width; SH = ev.height; if updategeom(state) != 0 || dirty { - drw::resize(DRW, SW as c_uint, state.bh as c_uint); + drw::resize(DRW, state.sw as c_uint, state.bh as c_uint); updatebars(state); let mut m = MONS; while !m.is_null() { @@ -394,7 +394,7 @@ pub(crate) fn configurenotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn destroynotify(state: &State, e: *mut XEvent) { +pub(crate) fn destroynotify(state: &mut State, e: *mut XEvent) { unsafe { let ev = &(*e).destroy_window; let mut c = wintoclient(ev.window); @@ -416,7 +416,7 @@ pub(crate) fn destroynotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn enternotify(state: &State, e: *mut XEvent) { +pub(crate) fn enternotify(state: &mut State, e: *mut XEvent) { log::trace!("enternotify"); unsafe { let ev = &mut (*e).crossing; @@ -438,7 +438,7 @@ pub(crate) fn enternotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn expose(state: &State, e: *mut XEvent) { +pub(crate) fn expose(state: &mut State, e: *mut XEvent) { unsafe { let ev = &(*e).expose; if ev.count == 0 { @@ -454,7 +454,7 @@ pub(crate) fn expose(state: &State, e: *mut XEvent) { } /* there are some broken focus acquiring clients needing extra handling */ -pub(crate) fn focusin(state: &State, e: *mut XEvent) { +pub(crate) fn focusin(state: &mut State, e: *mut XEvent) { unsafe { let ev = &(*e).focus_change; if !(*SELMON).sel.is_null() && ev.window != (*(*SELMON).sel).win { @@ -463,7 +463,7 @@ pub(crate) fn focusin(state: &State, e: *mut XEvent) { } } -pub(crate) fn keypress(state: &State, e: *mut XEvent) { +pub(crate) fn keypress(state: &mut State, e: *mut XEvent) { unsafe { let ev = &mut (*e).key; let keysym = @@ -479,7 +479,7 @@ pub(crate) fn keypress(state: &State, e: *mut XEvent) { } } -pub(crate) fn mappingnotify(state: &State, e: *mut XEvent) { +pub(crate) fn mappingnotify(state: &mut State, e: *mut XEvent) { unsafe { let ev = &mut (*e).mapping; xlib::XRefreshKeyboardMapping(ev); @@ -489,7 +489,7 @@ pub(crate) fn mappingnotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn maprequest(state: &State, e: *mut XEvent) { +pub(crate) fn maprequest(state: &mut State, e: *mut XEvent) { static mut WA: XWindowAttributes = XWindowAttributes { x: 0, y: 0, @@ -551,7 +551,7 @@ pub(crate) fn maprequest(state: &State, e: *mut XEvent) { } } -pub(crate) fn motionnotify(state: &State, e: *mut XEvent) { +pub(crate) fn motionnotify(state: &mut State, e: *mut XEvent) { log::trace!("motionnotify"); static mut MON: *mut Monitor = null_mut(); unsafe { @@ -569,7 +569,7 @@ pub(crate) fn motionnotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn propertynotify(state: &State, e: *mut XEvent) { +pub(crate) fn propertynotify(state: &mut State, e: *mut XEvent) { log::trace!("propertynotify"); unsafe { let mut trans: Window = 0; @@ -634,7 +634,7 @@ pub(crate) fn propertynotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn unmapnotify(state: &State, e: *mut XEvent) { +pub(crate) fn unmapnotify(state: &mut State, e: *mut XEvent) { log::trace!("unmapnotify"); unsafe { let ev = &(*e).unmap; @@ -658,7 +658,7 @@ pub(crate) fn unmapnotify(state: &State, e: *mut XEvent) { } } -pub(crate) fn resizerequest(state: &State, e: *mut XEvent) { +pub(crate) fn resizerequest(state: &mut State, e: *mut XEvent) { log::trace!("resizerequest"); unsafe { let ev = &(*e).resize_request; diff --git a/src/key_handlers.rs b/src/key_handlers.rs index 8003218..68a4c15 100644 --- a/src/key_handlers.rs +++ b/src/key_handlers.rs @@ -24,7 +24,7 @@ use crate::{ use rwm::State; use rwm::{Arg, Client, Monitor}; -pub(crate) fn togglebar(state: &State, _arg: *const Arg) { +pub(crate) fn togglebar(state: &mut State, _arg: *const Arg) { unsafe { (*(*SELMON).pertag).showbars[(*(*SELMON).pertag).curtag as usize] = !((*SELMON).showbar); @@ -56,7 +56,7 @@ pub(crate) fn togglebar(state: &State, _arg: *const Arg) { } } -pub(crate) fn focusstack(state: &State, arg: *const Arg) { +pub(crate) fn focusstack(state: &mut State, arg: *const Arg) { unsafe { let mut c: *mut Client = null_mut(); let mut i: *mut Client; @@ -93,7 +93,7 @@ pub(crate) fn focusstack(state: &State, arg: *const Arg) { } /// Increase the number of windows in the master area. -pub(crate) fn incnmaster(state: &State, arg: *const Arg) { +pub(crate) fn incnmaster(state: &mut State, arg: *const Arg) { unsafe { (*(*SELMON).pertag).nmasters[(*(*SELMON).pertag).curtag as usize] = std::cmp::max((*SELMON).nmaster + (*arg).i(), 0); @@ -107,7 +107,7 @@ pub(crate) fn incnmaster(state: &State, arg: *const Arg) { /// greater than 1.0 sets the fraction absolutely, while fractional values add /// to the current value. Total values are restricted to the range [0.05, 0.95] /// to leave at least 5% of the screen for other windows. -pub(crate) fn setmfact(state: &State, arg: *const Arg) { +pub(crate) fn setmfact(state: &mut State, arg: *const Arg) { unsafe { if arg.is_null() || (*(*SELMON).lt[(*SELMON).sellt as usize]).arrange.is_none() @@ -131,7 +131,7 @@ pub(crate) fn setmfact(state: &State, arg: *const Arg) { /// Move the selected window to the master area. The current master is pushed to /// the top of the stack. -pub(crate) fn zoom(state: &State, _arg: *const Arg) { +pub(crate) fn zoom(state: &mut State, _arg: *const Arg) { unsafe { let mut c = (*SELMON).sel; if (*(*SELMON).lt[(*SELMON).sellt as usize]).arrange.is_none() @@ -151,7 +151,7 @@ pub(crate) fn zoom(state: &State, _arg: *const Arg) { } /// View the tag identified by `arg.ui`. -pub(crate) fn view(state: &State, arg: *const Arg) { +pub(crate) fn view(state: &mut State, arg: *const Arg) { log::trace!("view"); unsafe { if (*arg).ui() & *TAGMASK @@ -196,7 +196,7 @@ pub(crate) fn view(state: &State, arg: *const Arg) { } } -pub(crate) fn killclient(state: &State, _arg: *const Arg) { +pub(crate) fn killclient(state: &mut State, _arg: *const Arg) { unsafe { if (*SELMON).sel.is_null() { return; @@ -225,7 +225,7 @@ pub(crate) fn killclient(state: &State, _arg: *const Arg) { } } -pub(crate) fn setlayout(state: &State, arg: *const Arg) { +pub(crate) fn setlayout(state: &mut State, arg: *const Arg) { log::trace!("setlayout: {arg:?}"); unsafe { if arg.is_null() @@ -261,7 +261,7 @@ pub(crate) fn setlayout(state: &State, arg: *const Arg) { } } -pub(crate) fn togglefloating(state: &State, _arg: *const Arg) { +pub(crate) fn togglefloating(state: &mut State, _arg: *const Arg) { log::trace!("togglefloating: {_arg:?}"); unsafe { if (*SELMON).sel.is_null() { @@ -286,7 +286,7 @@ pub(crate) fn togglefloating(state: &State, _arg: *const Arg) { /// From the [stacker patch](https://dwm.suckless.org/patches/stacker/). This /// should only be called with an ISINC arg, in their parlance, so also inline /// their stackpos function, in the branch where this is true -pub(crate) fn pushstack(state: &State, arg: *const Arg) { +pub(crate) fn pushstack(state: &mut State, arg: *const Arg) { fn modulo(n: c_int, m: c_int) -> c_int { if n % m < 0 { (n % m) + m @@ -341,7 +341,7 @@ pub(crate) fn pushstack(state: &State, arg: *const Arg) { } } -pub(crate) fn tag(state: &State, arg: *const Arg) { +pub(crate) fn tag(state: &mut State, arg: *const Arg) { unsafe { if !(*SELMON).sel.is_null() && (*arg).ui() & *TAGMASK != 0 { (*(*SELMON).sel).tags = (*arg).ui() & *TAGMASK; @@ -369,7 +369,7 @@ fn dirtomon(dir: i32) -> *mut Monitor { } } -pub(crate) fn focusmon(state: &State, arg: *const Arg) { +pub(crate) fn focusmon(state: &mut State, arg: *const Arg) { unsafe { if (*MONS).next.is_null() { return; @@ -384,7 +384,7 @@ pub(crate) fn focusmon(state: &State, arg: *const Arg) { } } -fn sendmon(state: &State, c: *mut Client, m: *mut Monitor) { +fn sendmon(state: &mut State, c: *mut Client, m: *mut Monitor) { unsafe { if (*c).mon == m { return; @@ -403,7 +403,7 @@ fn sendmon(state: &State, c: *mut Client, m: *mut Monitor) { } } -pub(crate) fn tagmon(state: &State, arg: *const Arg) { +pub(crate) fn tagmon(state: &mut State, arg: *const Arg) { unsafe { if (*SELMON).sel.is_null() || (*MONS).next.is_null() { return; @@ -412,7 +412,7 @@ pub(crate) fn tagmon(state: &State, arg: *const Arg) { } } -pub(crate) fn toggleview(state: &State, arg: *const Arg) { +pub(crate) fn toggleview(state: &mut State, arg: *const Arg) { unsafe { let newtagset = (*SELMON).tagset[(*SELMON).seltags as usize] ^ ((*arg).ui() & *TAGMASK); @@ -460,7 +460,7 @@ pub(crate) fn toggleview(state: &State, arg: *const Arg) { } } -pub(crate) fn quit(_state: &State, _arg: *const Arg) { +pub(crate) fn quit(_state: &mut State, _arg: *const Arg) { unsafe { crate::RUNNING = false; } @@ -472,7 +472,7 @@ const EXPOSE: i32 = Expose; const MAP_REQUEST: i32 = MapRequest; const MOTION_NOTIFY: i32 = MotionNotify; -pub(crate) fn movemouse(state: &State, _arg: *const Arg) { +pub(crate) fn movemouse(state: &mut State, _arg: *const Arg) { log::trace!("movemouse"); unsafe { let c = (*SELMON).sel; @@ -576,7 +576,7 @@ pub(crate) fn movemouse(state: &State, _arg: *const Arg) { } } -pub(crate) fn resizemouse(state: &State, _arg: *const Arg) { +pub(crate) fn resizemouse(state: &mut State, _arg: *const Arg) { log::trace!("resizemouse"); unsafe { let c = (*SELMON).sel; @@ -687,7 +687,7 @@ pub(crate) fn resizemouse(state: &State, _arg: *const Arg) { } } -pub(crate) fn spawn(_state: &State, arg: *const Arg) { +pub(crate) fn spawn(_state: &mut State, arg: *const Arg) { unsafe { let mut argv = (*arg).v(); if argv == *CONFIG.dmenucmd { @@ -708,7 +708,7 @@ pub(crate) fn spawn(_state: &State, arg: *const Arg) { } /// Move the current window to the tag specified by `arg.ui`. -pub(crate) fn toggletag(state: &State, arg: *const Arg) { +pub(crate) fn toggletag(state: &mut State, arg: *const Arg) { unsafe { if (*SELMON).sel.is_null() { return; @@ -726,7 +726,7 @@ pub(crate) fn toggletag(state: &State, arg: *const Arg) { /// /// adapted from: https://old.reddit.com/r/dwm/comments/avhkgb/fullscreen_mode/ /// for fixing problems with steam games -pub(crate) fn fullscreen(state: &State, _: *const Arg) { +pub(crate) fn fullscreen(state: &mut State, _: *const Arg) { unsafe { if (*SELMON).sel.is_null() { return; @@ -735,7 +735,7 @@ pub(crate) fn fullscreen(state: &State, _: *const Arg) { } } -pub(crate) fn togglescratch(state: &State, arg: *const Arg) { +pub(crate) fn togglescratch(state: &mut State, arg: *const Arg) { unsafe { let mut c: *mut Client; let mut found = false; diff --git a/src/layouts.rs b/src/layouts.rs index de80790..813be79 100644 --- a/src/layouts.rs +++ b/src/layouts.rs @@ -5,7 +5,7 @@ use libc::c_int; use crate::{height, is_visible, nexttiled, resize}; use rwm::{Monitor, State}; -pub(crate) fn monocle(state: &State, m: *mut Monitor) { +pub(crate) fn monocle(state: &mut State, m: *mut Monitor) { unsafe { let mut n = 0; let mut c; @@ -29,7 +29,7 @@ pub(crate) fn monocle(state: &State, m: *mut Monitor) { } } -pub(crate) fn tile(state: &State, m: *mut Monitor) { +pub(crate) fn tile(state: &mut State, m: *mut Monitor) { log::trace!("tile"); unsafe { let mut i; diff --git a/src/lib.rs b/src/lib.rs index 7b08c12..215c104 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,7 @@ pub struct Button { pub click: c_uint, pub mask: c_uint, pub button: c_uint, - pub func: Option, + pub func: Option, pub arg: Arg, } @@ -63,7 +63,7 @@ impl Button { click: Clk, mask: c_uint, button: c_uint, - func: fn(&State, *const Arg), + func: fn(&mut State, *const Arg), arg: Arg, ) -> Self { Self { click: click as c_uint, mask, button, func: Some(func), arg } @@ -99,7 +99,7 @@ pub struct Systray { #[derive(Debug, Copy, Clone)] pub struct Layout { pub symbol: *const c_char, - pub arrange: Option, + pub arrange: Option, } pub struct Pertag { @@ -200,6 +200,8 @@ pub struct Cursors { pub struct State { /// Bar height pub bh: c_int, + /// X display screen geometry width + pub sw: c_int, pub wmatom: [Atom; WM::Last as usize], pub netatom: [Atom; Net::Last as usize], pub xatom: [Atom; XEmbed::Last as usize], diff --git a/src/main.rs b/src/main.rs index ff1c0b0..b7dd4fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,9 +152,6 @@ const BROKEN: &CStr = c"broken"; static mut STEXT: [c_char; 256] = ['\0' as c_char; 256]; -/// X display screen geometry width -static mut SW: c_int = 0; - /// X display screen geometry height static mut SH: c_int = 0; @@ -262,10 +259,10 @@ fn setup(dpy: *mut Display) -> State { while libc::waitpid(-1, null_mut(), libc::WNOHANG) > 0 {} SCREEN = xlib::XDefaultScreen(dpy); - SW = xlib::XDisplayWidth(dpy, SCREEN); SH = xlib::XDisplayHeight(dpy, SCREEN); ROOT = xlib::XRootWindow(dpy, SCREEN); - DRW = drw::create(dpy, SCREEN, ROOT, SW as u32, SH as u32); + let sw = xlib::XDisplayWidth(dpy, SCREEN); + DRW = drw::create(dpy, SCREEN, ROOT, sw as u32, SH as u32); if drw::fontset_create(DRW, &CONFIG.fonts).is_null() { panic!("no fonts could be loaded"); } @@ -279,6 +276,7 @@ fn setup(dpy: *mut Display) -> State { }; let mut state = State { bh: (*(*DRW).fonts).h as i32 + 2, + sw, cursors, wmatom: Default::default(), netatom: Default::default(), @@ -286,7 +284,7 @@ fn setup(dpy: *mut Display) -> State { dpy, }; - updategeom(&state); + updategeom(&mut state); /* init atoms */ let utf8string = XInternAtom(state.dpy, c"UTF8_STRING".as_ptr(), False); @@ -352,11 +350,11 @@ fn setup(dpy: *mut Display) -> State { } // init system tray - updatesystray(&state); + updatesystray(&mut state); /* init bars */ - updatebars(&state); - updatestatus(&state); + updatebars(&mut state); + updatestatus(&mut state); /* supporting window for NetWMCheck */ WMCHECKWIN = @@ -425,14 +423,14 @@ fn setup(dpy: *mut Display) -> State { &mut wa, ); xlib::XSelectInput(state.dpy, ROOT, wa.event_mask); - grabkeys(&state); - focus(&state, null_mut()); + grabkeys(&mut state); + focus(&mut state, null_mut()); state } } -fn focus(state: &State, mut c: *mut Client) { +fn focus(state: &mut State, mut c: *mut Client) { log::trace!("focus: c = {c:?}"); unsafe { if c.is_null() || !is_visible(c) { @@ -477,7 +475,7 @@ fn focus(state: &State, mut c: *mut Client) { } } -fn drawbars(state: &State) { +fn drawbars(state: &mut State) { log::trace!("drawbars"); unsafe { let mut m = MONS; @@ -488,7 +486,7 @@ fn drawbars(state: &State) { } } -fn setfocus(state: &State, c: *mut Client) { +fn setfocus(state: &mut State, c: *mut Client) { log::trace!("setfocus"); unsafe { if (*c).neverfocus == 0 { @@ -525,7 +523,7 @@ fn setfocus(state: &State, c: *mut Client) { #[allow(clippy::too_many_arguments)] fn sendevent( - state: &State, + state: &mut State, w: Window, proto: Atom, mask: c_int, @@ -573,7 +571,7 @@ fn sendevent( } } -fn grabbuttons(state: &State, c: *mut Client, focused: bool) { +fn grabbuttons(state: &mut State, c: *mut Client, focused: bool) { log::trace!("grabbuttons"); unsafe { updatenumlockmask(state); @@ -614,7 +612,7 @@ fn grabbuttons(state: &State, c: *mut Client, focused: bool) { } } -fn arrange(state: &State, mut m: *mut Monitor) { +fn arrange(state: &mut State, mut m: *mut Monitor) { log::trace!("arrange"); unsafe { if !m.is_null() { @@ -640,7 +638,7 @@ fn arrange(state: &State, mut m: *mut Monitor) { } } -fn arrangemon(state: &State, m: *mut Monitor) { +fn arrangemon(state: &mut State, m: *mut Monitor) { log::trace!("arrangemon"); unsafe { libc::strncpy( @@ -655,7 +653,7 @@ fn arrangemon(state: &State, m: *mut Monitor) { } } -fn restack(state: &State, m: *mut Monitor) { +fn restack(state: &mut State, m: *mut Monitor) { log::trace!("restack"); drawbar(state, m); unsafe { @@ -697,7 +695,7 @@ fn restack(state: &State, m: *mut Monitor) { } } -fn showhide(state: &State, c: *mut Client) { +fn showhide(state: &mut State, c: *mut Client) { log::trace!("showhide"); unsafe { if c.is_null() { @@ -724,7 +722,7 @@ fn showhide(state: &State, c: *mut Client) { } fn resize( - state: &State, + state: &mut State, c: *mut Client, mut x: i32, mut y: i32, @@ -738,7 +736,14 @@ fn resize( } } -fn resizeclient(state: &State, c: *mut Client, x: i32, y: i32, w: i32, h: i32) { +fn resizeclient( + state: &mut State, + c: *mut Client, + x: i32, + y: i32, + w: i32, + h: i32, +) { log::trace!("resizeclient"); unsafe { (*c).oldx = (*c).x; @@ -769,7 +774,7 @@ fn resizeclient(state: &State, c: *mut Client, x: i32, y: i32, w: i32, h: i32) { } } -fn resizebarwin(state: &State, m: *mut Monitor) { +fn resizebarwin(state: &mut State, m: *mut Monitor) { unsafe { let mut w = (*m).ww; if CONFIG.showsystray && m == systraytomon(m) && !CONFIG.systrayonleft { @@ -786,7 +791,7 @@ fn resizebarwin(state: &State, m: *mut Monitor) { } } -fn configure(state: &State, c: *mut Client) { +fn configure(state: &mut State, c: *mut Client) { log::trace!("configure"); unsafe { let mut ce = xlib::XConfigureEvent { @@ -815,7 +820,7 @@ fn configure(state: &State, c: *mut Client) { } fn applysizehints( - state: &State, + state: &mut State, c: *mut Client, x: &mut i32, y: &mut i32, @@ -831,8 +836,8 @@ fn applysizehints( *w = 1.max(*w); *h = 1.max(*h); if interact { - if *x > SW { - *x = SW - width(c); + if *x > state.sw { + *x = state.sw - width(c); } if *y > SH { *y = SH - height(c); @@ -913,7 +918,7 @@ fn applysizehints( } } -fn updatesizehints(state: &State, c: *mut Client) { +fn updatesizehints(state: &mut State, c: *mut Client) { log::trace!("updatesizehints"); let mut msize: i64 = 0; let mut size = xlib::XSizeHints { @@ -995,7 +1000,7 @@ fn updatesizehints(state: &State, c: *mut Client) { } } -fn pop(state: &State, c: *mut Client) { +fn pop(state: &mut State, c: *mut Client) { log::trace!("pop"); detach(c); attach(c); @@ -1026,7 +1031,7 @@ fn nexttiled(mut c: *mut Client) -> *mut Client { } } -fn grabkeys(state: &State) { +fn grabkeys(state: &mut State) { log::trace!("grabkeys"); unsafe { updatenumlockmask(state); @@ -1067,7 +1072,7 @@ fn grabkeys(state: &State) { } } -fn updatenumlockmask(state: &State) { +fn updatenumlockmask(state: &mut State) { log::trace!("updatenumlockmask"); unsafe { NUMLOCKMASK = 0; @@ -1087,7 +1092,7 @@ fn updatenumlockmask(state: &State) { } } -fn seturgent(state: &State, c: *mut Client, urg: bool) { +fn seturgent(state: &mut State, c: *mut Client, urg: bool) { log::trace!("seturgent"); unsafe { (*c).isurgent = urg as c_int; @@ -1105,7 +1110,7 @@ fn seturgent(state: &State, c: *mut Client, urg: bool) { } } -fn unfocus(state: &State, c: *mut Client, setfocus: bool) { +fn unfocus(state: &mut State, c: *mut Client, setfocus: bool) { log::trace!("unfocus"); if c.is_null() { return; @@ -1133,7 +1138,7 @@ fn unfocus(state: &State, c: *mut Client, setfocus: bool) { } } -fn updatestatus(state: &State) { +fn updatestatus(state: &mut State) { log::trace!("updatestatus"); unsafe { if gettextprop( @@ -1155,7 +1160,12 @@ fn updatestatus(state: &State) { } } -fn updatesystrayicongeom(state: &State, i: *mut Client, w: c_int, h: c_int) { +fn updatesystrayicongeom( + state: &mut State, + i: *mut Client, + w: c_int, + h: c_int, +) { if i.is_null() { return; } @@ -1183,7 +1193,7 @@ fn updatesystrayicongeom(state: &State, i: *mut Client, w: c_int, h: c_int) { } fn updatesystrayiconstate( - state: &State, + state: &mut State, i: *mut Client, ev: *mut XPropertyEvent, ) { @@ -1248,7 +1258,7 @@ const fn default_window_attributes() -> XSetWindowAttributes { } } -fn updatesystray(state: &State) { +fn updatesystray(state: &mut State) { unsafe { let mut wa = default_window_attributes(); let mut wc: XWindowChanges; @@ -1450,7 +1460,7 @@ fn textw(x: *const c_char) -> c_int { unsafe { drw::fontset_getwidth(DRW, x) as c_int + LRPAD } } -fn drawbar(state: &State, m: *mut Monitor) { +fn drawbar(state: &mut State, m: *mut Monitor) { log::trace!("drawbar"); unsafe { let mut tw = 0; @@ -1599,7 +1609,7 @@ fn drawbar(state: &State, m: *mut Monitor) { } fn gettextprop( - state: &State, + state: &mut State, w: Window, atom: Atom, text: *mut i8, @@ -1645,7 +1655,7 @@ fn gettextprop( 1 } -fn updatebars(state: &State) { +fn updatebars(state: &mut State) { log::trace!("updatebars"); let mut wa = xlib::XSetWindowAttributes { override_redirect: True, @@ -1709,7 +1719,7 @@ fn updatebars(state: &State) { } } -fn updategeom(state: &State) -> i32 { +fn updategeom(state: &mut State) -> i32 { log::trace!("updategeom"); unsafe { let mut dirty = 0; @@ -1834,10 +1844,10 @@ fn updategeom(state: &State) -> i32 { if MONS.is_null() { MONS = createmon(); } - if (*MONS).mw != SW || (*MONS).mh != SH { + if (*MONS).mw != state.sw || (*MONS).mh != SH { dirty = 1; - (*MONS).mw = SW; - (*MONS).ww = SW; + (*MONS).mw = state.sw; + (*MONS).ww = state.sw; (*MONS).mh = SH; (*MONS).wh = SH; updatebarpos(state, MONS); @@ -1851,7 +1861,7 @@ fn updategeom(state: &State) -> i32 { } } -fn wintomon(state: &State, w: Window) -> *mut Monitor { +fn wintomon(state: &mut State, w: Window) -> *mut Monitor { log::trace!("wintomon"); unsafe { let mut x = 0; @@ -1962,7 +1972,7 @@ fn cleanmask(mask: u32) -> u32 { } } -fn getrootptr(state: &State, x: *mut c_int, y: *mut c_int) -> c_int { +fn getrootptr(state: &mut State, x: *mut c_int, y: *mut c_int) -> c_int { unsafe { let mut di = 0; let mut dui = 0; @@ -1975,7 +1985,7 @@ fn getrootptr(state: &State, x: *mut c_int, y: *mut c_int) -> c_int { } /// remove `mon` from the linked list of `Monitor`s in `MONS` and free it. -fn cleanupmon(state: &State, mon: *mut Monitor) { +fn cleanupmon(state: &mut State, mon: *mut Monitor) { unsafe { if mon == MONS { MONS = (*MONS).next; @@ -2036,7 +2046,7 @@ fn is_visible(c: *const Client) -> bool { } } -fn updatebarpos(state: &State, m: *mut Monitor) { +fn updatebarpos(state: &mut State, m: *mut Monitor) { log::trace!("updatebarpos"); unsafe { @@ -2075,7 +2085,7 @@ fn isuniquegeom( } } -fn cleanup(state: &State) { +fn cleanup(state: &mut State) { log::trace!("entering cleanup"); unsafe { @@ -2129,7 +2139,7 @@ fn cleanup(state: &State) { log::trace!("finished cleanup"); } -fn unmanage(state: &State, c: *mut Client, destroyed: c_int) { +fn unmanage(state: &mut State, c: *mut Client, destroyed: c_int) { log::trace!("unmanage"); unsafe { let m = (*c).mon; @@ -2328,7 +2338,7 @@ fn swallowingclient(w: Window) -> *mut Client { } } -fn updateclientlist(state: &State) { +fn updateclientlist(state: &mut State) { unsafe { xlib::XDeleteProperty( state.dpy, @@ -2356,7 +2366,7 @@ fn updateclientlist(state: &State) { } } -fn setclientstate(s: &State, c: *mut Client, state: usize) { +fn setclientstate(s: &mut State, c: *mut Client, state: usize) { let mut data: [c_long; 2] = [state as c_long, XNONE as c_long]; let ptr: *mut c_uchar = data.as_mut_ptr().cast(); unsafe { @@ -2373,12 +2383,12 @@ fn setclientstate(s: &State, c: *mut Client, state: usize) { } } -type HandlerFn = fn(&State, *mut xlib::XEvent); +type HandlerFn = fn(&mut State, *mut xlib::XEvent); static HANDLER: LazyLock<[HandlerFn; x11::xlib::LASTEvent as usize]> = LazyLock::new(|| { - fn dh(_state: &State, _ev: *mut xlib::XEvent) {} - let mut ret = [dh as fn(state: &State, *mut xlib::XEvent); + fn dh(_state: &mut State, _ev: *mut xlib::XEvent) {} + let mut ret = [dh as fn(state: &mut State, *mut xlib::XEvent); x11::xlib::LASTEvent as usize]; ret[x11::xlib::ButtonPress as usize] = handlers::buttonpress; ret[x11::xlib::ClientMessage as usize] = handlers::clientmessage; @@ -2399,7 +2409,7 @@ static HANDLER: LazyLock<[HandlerFn; x11::xlib::LASTEvent as usize]> = }); /// main event loop -fn run(state: &State) { +fn run(state: &mut State) { unsafe { xlib::XSync(state.dpy, False); let mut ev: MaybeUninit = MaybeUninit::uninit(); @@ -2412,7 +2422,7 @@ fn run(state: &State) { } } -fn scan(state: &State) { +fn scan(state: &mut State) { let mut num = 0; let mut d1 = 0; let mut d2 = 0; @@ -2479,7 +2489,7 @@ fn scan(state: &State) { } } -fn manage(state: &State, w: Window, wa: *mut xlib::XWindowAttributes) { +fn manage(state: &mut State, w: Window, wa: *mut xlib::XWindowAttributes) { log::trace!("manage"); let mut trans = 0; unsafe { @@ -2606,7 +2616,7 @@ fn manage(state: &State, w: Window, wa: *mut xlib::XWindowAttributes) { xlib::XMoveResizeWindow( state.dpy, (*c).win, - (*c).x + 2 * SW, + (*c).x + 2 * state.sw, (*c).y, (*c).w as u32, (*c).h as u32, @@ -2625,7 +2635,7 @@ fn manage(state: &State, w: Window, wa: *mut xlib::XWindowAttributes) { } } -fn updatewmhints(state: &State, c: *mut Client) { +fn updatewmhints(state: &mut State, c: *mut Client) { log::trace!("updatewmhints"); const URGENT: i64 = xlib::XUrgencyHint; unsafe { @@ -2647,7 +2657,7 @@ fn updatewmhints(state: &State, c: *mut Client) { } } -fn updatewindowtype(state: &State, c: *mut Client) { +fn updatewindowtype(state: &mut State, c: *mut Client) { log::trace!("updatewindowtype"); unsafe { let s = getatomprop(state, c, state.netatom[Net::WMState as usize]); @@ -2662,7 +2672,7 @@ fn updatewindowtype(state: &State, c: *mut Client) { } } -fn setfullscreen(state: &State, c: *mut Client, fullscreen: bool) { +fn setfullscreen(state: &mut State, c: *mut Client, fullscreen: bool) { unsafe { if fullscreen && !(*c).isfullscreen { xlib::XChangeProperty( @@ -2716,7 +2726,7 @@ fn setfullscreen(state: &State, c: *mut Client, fullscreen: bool) { } } -fn getatomprop(state: &State, c: *mut Client, prop: Atom) -> Atom { +fn getatomprop(state: &mut State, c: *mut Client, prop: Atom) -> Atom { let mut di = 0; let mut dl = 0; let mut p = std::ptr::null_mut(); @@ -2776,7 +2786,7 @@ fn getsystraywidth() -> c_uint { } } -fn applyrules(state: &State, c: *mut Client) { +fn applyrules(state: &mut State, c: *mut Client) { log::trace!("applyrules"); unsafe { let mut ch = xlib::XClassHint { @@ -2833,7 +2843,7 @@ fn applyrules(state: &State, c: *mut Client) { } } -fn swallow(state: &State, p: *mut Client, c: *mut Client) { +fn swallow(state: &mut State, p: *mut Client, c: *mut Client) { unsafe { let c = &mut *c; if c.noswallow || c.isterminal { @@ -2860,7 +2870,7 @@ fn swallow(state: &State, p: *mut Client, c: *mut Client) { } } -fn unswallow(state: &State, c: *mut Client) { +fn unswallow(state: &mut State, c: *mut Client) { unsafe { let c = &mut *c; @@ -2888,7 +2898,7 @@ const MOUSEMASK: i64 = BUTTONMASK | PointerMotionMask; static SCRATCHTAG: LazyLock = LazyLock::new(|| 1 << CONFIG.tags.len()); -fn updatetitle(state: &State, c: *mut Client) { +fn updatetitle(state: &mut State, c: *mut Client) { log::trace!("updatetitle"); unsafe { if gettextprop( @@ -2917,7 +2927,7 @@ fn updatetitle(state: &State, c: *mut Client) { } } -fn getstate(state: &State, w: Window) -> c_long { +fn getstate(state: &mut State, w: Window) -> c_long { let mut format = 0; let mut result: c_long = -1; let mut p: *mut c_uchar = std::ptr::null_mut(); @@ -2981,10 +2991,10 @@ fn main() { } checkotherwm(dpy); - let state = setup(dpy); - scan(&state); - run(&state); - cleanup(&state); + let mut state = setup(dpy); + scan(&mut state); + run(&mut state); + cleanup(&mut state); unsafe { let State { dpy, cursors, .. } = state; diff --git a/src/tests.rs b/src/tests.rs index 8e0e4c8..0e269e1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -34,36 +34,34 @@ fn main() { XCON = Box::into_raw(Box::new(xcon)); } checkotherwm(dpy); - let state = setup(dpy); - scan(&state); + let mut state = setup(dpy); + scan(&mut state); // instead of calling `run`, manually send some XEvents // test that a mouse click on the initial (tiling) layout icon // switches to floating mode - handlers::buttonpress( - &state, - &mut Event::button( - (unsafe { *SELMON }).barwin, - Button1, - CONFIG - .tags - .iter() - .map(|tag| textw(tag.as_ptr())) - .sum::() - + 5, - state.dpy, - 0, - ) - .into_button(), - ); + let mut button = Event::button( + (unsafe { *SELMON }).barwin, + Button1, + CONFIG + .tags + .iter() + .map(|tag| textw(tag.as_ptr())) + .sum::() + + 5, + state.dpy, + 0, + ) + .into_button(); + handlers::buttonpress(&mut state, &mut button); unsafe { assert!((*(*SELMON).lt[(*SELMON).sellt as usize]) .arrange .is_none()); } - cleanup(&state); + cleanup(&mut state); unsafe { let State { dpy, cursors, .. } = state;