diff --git a/src/drw.rs b/src/drw.rs index ae7cdaf..d677786 100644 --- a/src/drw.rs +++ b/src/drw.rs @@ -191,13 +191,15 @@ impl Drw { self.scheme = scm.as_mut_ptr(); } - pub(crate) fn textw(&self, stext: &str) -> usize { + pub(crate) fn textw(&self, stext: *const String) -> usize { unsafe { self.fontset_getwidth(stext) + LRPAD } } - fn fontset_getwidth(&self, text: &str) -> usize { - if self.fonts.is_null() || text.is_empty() { - return 0; + fn fontset_getwidth(&self, text: *const String) -> usize { + unsafe { + if self.fonts.is_null() || (*text).is_empty() { + return 0; + } } self.text(0, 0, 0, 0, 0, text, 0) } @@ -223,7 +225,7 @@ impl Drw { mut w: usize, h: usize, lpad: usize, - text: &str, + text: *const String, invert: c_int, ) -> usize { let mut ty; @@ -248,14 +250,16 @@ impl Drw { let mut charexists = false; let mut overflow = false; - if (render && (self.scheme.is_null() || w == 0)) - || text.is_empty() - || self.fonts.is_null() - { - return 0; - } + let ellipsis = String::from("..."); unsafe { + if (render && (self.scheme.is_null() || w == 0)) + || (*text).is_empty() + || self.fonts.is_null() + { + return 0; + } + if !render { w = if invert != 0 { invert } else { !invert } as usize; } else { @@ -290,7 +294,7 @@ impl Drw { usedfont = self.fonts; if ELLIPSIS_WIDTH == 0 && render { - ELLIPSIS_WIDTH = self.fontset_getwidth("..."); + ELLIPSIS_WIDTH = self.fontset_getwidth(&ellipsis); } let mut text_idx = 0; @@ -300,7 +304,7 @@ impl Drw { utf8strlen = 0; utf8str = text; nextfont = std::ptr::null_mut(); - for ch in text.chars() { + for ch in (*text).chars() { utf8charlen = ch.len_utf8(); utf8codepoint = ch as usize; curfont = self.fonts; @@ -312,9 +316,10 @@ impl Drw { utf8codepoint as u32, ) != 0; if charexists { + let text = String::from((*text).clone()); self.font_getexts( curfont, - text, + &text, utf8charlen, &mut tmpw, std::ptr::null_mut(), @@ -359,7 +364,7 @@ impl Drw { ty = y as usize + (h - (*usedfont).h) / 2 + (*(*usedfont).xfont).ascent as usize; - let s = CString::new(utf8str).unwrap(); + let s = CString::new((*utf8str).clone()).unwrap(); XftDrawStringUtf8( d, self.scheme.offset(if invert != 0 { @@ -380,10 +385,12 @@ impl Drw { } if render && overflow { - self.text(ellipsis_x, y, ellipsis_w, h, 0, "...", invert); + self.text( + ellipsis_x, y, ellipsis_w, h, 0, &ellipsis, invert, + ); } - if text_idx >= text.len() || overflow { + if text_idx >= (*text).len() || overflow { break; } else if !nextfont.is_null() { charexists = false; diff --git a/src/main.rs b/src/main.rs index 2b97746..6157d25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use std::mem::{size_of, MaybeUninit}; use std::os::fd::AsRawFd; use std::path::Path; use std::process::Command; -use std::ptr::null_mut; +use std::ptr::{addr_of, addr_of_mut, null_mut}; use config::{ COLORS, DMENUMON, FONTS, KEYS, LAYOUTS, MFACT, NMASTER, SHOWBAR, TOPBAR, @@ -1259,7 +1259,7 @@ pub fn spawn(_dpy: &Display, arg: Arg) { let r: &'static str = format!("{}", (*SELMON).num).leak(); let r: Box<&'static str> = Box::new(r); let mut r: &'static &'static str = Box::leak(r); - std::mem::swap(&mut DMENUMON, &mut r); + std::ptr::swap(addr_of_mut!(DMENUMON), &mut r); } Command::new(s[0]) .args(&s[1..]) @@ -1840,7 +1840,7 @@ fn unfocus(dpy: &Display, c: *mut Client, setfocus: bool) { fn updatestatus(dpy: &Display) { unsafe { - let c = gettextprop(dpy, ROOT, XA_WM_NAME, &mut STEXT); + let c = gettextprop(dpy, ROOT, XA_WM_NAME, addr_of_mut!(STEXT)); if !c { STEXT = "rwm-0.0.1".to_owned(); } @@ -1868,14 +1868,14 @@ fn drawbar(m: *mut Monitor) { if m == SELMON { // status is only drawn on selected monitor drw.setscheme(&mut SCHEME[Scheme::Norm as usize]); - tw = drw.textw(&STEXT) - LRPAD + 2; // 2px right padding + tw = drw.textw(addr_of!(STEXT)) - LRPAD + 2; // 2px right padding drw.text( ((*m).ww - tw as i16) as i32, 0, tw, BH as usize, 0, - &STEXT, + addr_of!(STEXT), 0, ); } @@ -1890,7 +1890,8 @@ fn drawbar(m: *mut Monitor) { } for i in 0..TAGS.len() { - w = drw.textw(TAGS[i]); + let text = TAGS[i].to_owned(); + w = drw.textw(&text); drw.setscheme( &mut SCHEME[if ((*m).tagset[(*m).seltags] & 1 << i) != 0 { Scheme::Sel as usize @@ -1904,7 +1905,7 @@ fn drawbar(m: *mut Monitor) { w, BH as usize, LRPAD / 2, - TAGS[i], + &text, (urg as i32) & 1 << i, ); @@ -1961,12 +1962,12 @@ fn gettextprop( dpy: &Display, w: Window, atom: Atom, - text: &mut String, + text: *mut String, ) -> bool { - if text.is_empty() { - return false; - } unsafe { + if (*text).is_empty() { + return false; + } let mut name = MaybeUninit::uninit(); let c = XGetTextProperty(dpy.inner, w, name.as_mut_ptr(), atom); let name = name.assume_init(); @@ -2867,10 +2868,12 @@ fn buttonpress(dpy: &Display, e: *mut XEvent) { let mut i = 0; // do while with ++i in condition let drw = &DRW.as_ref().unwrap(); - x += drw.textw(TAGS[i]); + let text = TAGS[i].to_owned(); + x += drw.textw(&text); i += 1; while ev.x >= x as i32 && i < TAGS.len() { - x += drw.textw(TAGS[i]); + let text = TAGS[i].to_owned(); + x += drw.textw(&text); i += 1; } if i < TAGS.len() { @@ -2878,7 +2881,8 @@ fn buttonpress(dpy: &Display, e: *mut XEvent) { arg = Arg::Uint(1 << i); } else if ev.x < (x + drw.textw(&(*SELMON).ltsymbol)) as i32 { click = Clk::LtSymbol; - } else if ev.x > ((*SELMON).ww as usize - drw.textw(&STEXT)) as i32 + } else if ev.x + > ((*SELMON).ww as usize - drw.textw(addr_of!(STEXT))) as i32 { click = Clk::StatusText; } else {