Skip to content

Commit

Permalink
move SW into state and finally have to pass &mut State everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Dec 5, 2024
1 parent 0c0ffe7 commit 8f92234
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions src/config/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use x11::xlib::KeySym;
pub struct Key {
pub mod_: c_uint,
pub keysym: KeySym,
pub func: Option<fn(&State, *const Arg)>,
pub func: Option<fn(&mut State, *const Arg)>,
pub arg: Arg,
}

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 }
Expand All @@ -34,10 +34,10 @@ pub(crate) fn conv<T: Clone>(opt: Option<&T>) -> Result<T, FigError> {
}
}

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<FnMap> = 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),
Expand Down
38 changes: 19 additions & 19 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 =
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
44 changes: 22 additions & 22 deletions src/key_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 8f92234

Please sign in to comment.