Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DRW an option instead of raw pointer #38

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ifdef FIX
endif

clippy:
cargo clippy --workspace --all-targets --all-features $(clippy_args)
cargo clippy --workspace --all-targets --all-features --tests $(clippy_args)

doc:
cargo doc --open
Expand Down
180 changes: 80 additions & 100 deletions src/drw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Fnt {
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Clone)]
pub struct Drw {
pub w: c_uint,
pub h: c_uint,
Expand Down Expand Up @@ -119,45 +119,42 @@ pub(crate) fn create(
root: Window,
w: c_uint,
h: c_uint,
) -> *mut Drw {
) -> Drw {
unsafe {
let drw: *mut Drw = crate::util::ecalloc(1, size_of::<Drw>()).cast();
(*drw).dpy = dpy;
(*drw).screen = screen;
(*drw).root = root;
(*drw).w = w;
(*drw).h = h;
(*drw).drawable = xlib::XCreatePixmap(
dpy,
root,
let drw = Drw {
drawable: xlib::XCreatePixmap(
dpy,
root,
w,
h,
xlib::XDefaultDepth(dpy, screen) as u32,
),
w,
h,
xlib::XDefaultDepth(dpy, screen) as u32,
);
(*drw).gc = xlib::XCreateGC(dpy, root, 0, std::ptr::null_mut());
xlib::XSetLineAttributes(
dpy,
(*drw).gc,
1,
LineSolid,
CapButt,
JoinMiter,
);
screen,
root,
gc: xlib::XCreateGC(dpy, root, 0, null_mut()),
scheme: null_mut(),
fonts: null_mut(),
};
xlib::XSetLineAttributes(dpy, drw.gc, 1, LineSolid, CapButt, JoinMiter);
drw
}
}

pub(crate) fn free(drw: *mut Drw) {
unsafe {
xlib::XFreePixmap((*drw).dpy, (*drw).drawable);
xlib::XFreeGC((*drw).dpy, (*drw).gc);
fontset_free((*drw).fonts);
libc::free(drw.cast());
impl Drop for Drw {
fn drop(&mut self) {
unsafe {
xlib::XFreePixmap(self.dpy, self.drawable);
xlib::XFreeGC(self.dpy, self.gc);
fontset_free(self.fonts);
}
}
}

pub(crate) fn rect(
drw: *mut Drw,
drw: Option<&mut Drw>,
x: c_int,
y: c_int,
w: c_uint,
Expand All @@ -166,33 +163,28 @@ pub(crate) fn rect(
invert: c_int,
) {
unsafe {
if drw.is_null() || (*drw).scheme.is_null() {
let Some(drw) = drw else {
return;
};
if drw.scheme.is_null() {
return;
}
xlib::XSetForeground(
(*drw).dpy,
(*drw).gc,
drw.dpy,
drw.gc,
if invert != 0 {
(*(*drw).scheme.offset(Col::Bg as isize)).pixel
(*drw.scheme.offset(Col::Bg as isize)).pixel
} else {
(*(*drw).scheme.offset(Col::Fg as isize)).pixel
(*drw.scheme.offset(Col::Fg as isize)).pixel
},
);
if filled != 0 {
xlib::XFillRectangle(
(*drw).dpy,
(*drw).drawable,
(*drw).gc,
x,
y,
w,
h,
);
xlib::XFillRectangle(drw.dpy, drw.drawable, drw.gc, x, y, w, h);
} else {
xlib::XDrawRectangle(
(*drw).dpy,
(*drw).drawable,
(*drw).gc,
drw.dpy,
drw.drawable,
drw.gc,
x,
y,
w - 1,
Expand All @@ -202,46 +194,41 @@ pub(crate) fn rect(
}
}

pub(crate) fn cur_create(drw: *mut Drw, shape: c_int) -> *mut Cur {
if drw.is_null() {
return std::ptr::null_mut();
}
pub(crate) fn cur_create(drw: &mut Drw, shape: c_int) -> *mut Cur {
unsafe {
let cur: *mut Cur = crate::util::ecalloc(1, size_of::<Cur>()).cast();
if cur.is_null() {
return std::ptr::null_mut();
}
(*cur).cursor = xlib::XCreateFontCursor((*drw).dpy, shape as c_uint);
(*cur).cursor = xlib::XCreateFontCursor(drw.dpy, shape as c_uint);
cur
}
}

pub(crate) fn cur_free(drw: *mut Drw, cursor: *mut Cur) {
pub(crate) fn cur_free(drw: &mut Drw, cursor: *mut Cur) {
if cursor.is_null() {
return;
}

unsafe {
xlib::XFreeCursor((*drw).dpy, (*cursor).cursor);
xlib::XFreeCursor(drw.dpy, (*cursor).cursor);
libc::free(cursor.cast());
}
}

pub(crate) fn setscheme(drw: *mut Drw, scm: *mut Clr) {
if !drw.is_null() {
unsafe {
(*drw).scheme = scm;
}
pub(crate) fn setscheme(drw: Option<&mut Drw>, scm: *mut Clr) {
if let Some(drw) = drw {
drw.scheme = scm;
}
}

pub(crate) fn fontset_create(drw: *mut Drw, fonts: &[CString]) -> *mut Fnt {
pub(crate) fn fontset_create(drw: &mut Drw, fonts: &[CString]) -> *mut Fnt {
log::trace!("fontset_create");
unsafe {
let mut ret: *mut Fnt = null_mut();

// since fonts is a & not a *, it can't be null, but it could be empty
if drw.is_null() || fonts.is_empty() {
if fonts.is_empty() {
return null_mut();
}

Expand All @@ -252,7 +239,7 @@ pub(crate) fn fontset_create(drw: *mut Drw, fonts: &[CString]) -> *mut Fnt {
ret = cur;
}
}
(*drw).fonts = ret;
drw.fonts = ret;
ret
}
}
Expand Down Expand Up @@ -366,11 +353,11 @@ fn clr_create(drw: *mut Drw, dest: *mut Clr, clrname: *const c_char) {
}

pub(crate) fn scm_create(
drw: *mut Drw,
drw: &mut Drw,
clrnames: &[CString],
clrcount: usize,
) -> *mut Clr {
if drw.is_null() || clrnames.is_empty() || clrcount < 2 {
if clrnames.is_empty() || clrcount < 2 {
return null_mut();
}
let ret: *mut Clr = ecalloc(clrcount, size_of::<xft::XftColor>()).cast();
Expand All @@ -385,18 +372,19 @@ pub(crate) fn scm_create(
ret
}

pub(crate) fn fontset_getwidth(drw: *mut Drw, text: *const c_char) -> c_uint {
unsafe {
if drw.is_null() || (*drw).fonts.is_null() || text.is_null() {
return 0;
}
pub(crate) fn fontset_getwidth(
drw: Option<&mut Drw>,
text: *const c_char,
) -> c_uint {
if drw.as_ref().is_none_or(|drw| drw.fonts.is_null()) || text.is_null() {
return 0;
}
self::text(drw, 0, 0, 0, 0, 0, text, 0) as c_uint
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn text(
drw: *mut Drw,
drw: Option<&mut Drw>,
mut x: c_int,
y: c_int,
mut w: c_uint,
Expand Down Expand Up @@ -454,10 +442,13 @@ pub(crate) fn text(
NoMatches { codepoint: [0; NOMATCHES_LEN], idx: 0 };
static mut ELLIPSIS_WIDTH: c_uint = 0;

if drw.is_null()
|| (render != 0 && ((*drw).scheme.is_null() || w == 0))
let Some(drw) = drw else {
return 0;
};

if (render != 0 && (drw.scheme.is_null() || w == 0))
|| text.is_null()
|| (*drw).fonts.is_null()
|| drw.fonts.is_null()
{
return 0;
}
Expand Down Expand Up @@ -490,7 +481,7 @@ pub(crate) fn text(

usedfont = drw.fonts;
if ELLIPSIS_WIDTH == 0 && render != 0 {
ELLIPSIS_WIDTH = fontset_getwidth(drw, c"...".as_ptr());
ELLIPSIS_WIDTH = fontset_getwidth(Some(drw), c"...".as_ptr());
}
log::trace!("text: entering loop");
'no_match: loop {
Expand Down Expand Up @@ -592,7 +583,7 @@ pub(crate) fn text(
}
if render != 0 && overflow != 0 {
self::text(
drw,
Some(drw),
ellipsis_x,
y,
ellipsis_w,
Expand Down Expand Up @@ -734,49 +725,38 @@ fn font_getexts(
}

pub(crate) fn map(
drw: *mut Drw,
drw: Option<&mut Drw>,
win: Window,
x: c_int,
y: c_int,
w: c_uint,
h: c_uint,
) {
if drw.is_null() {
let Some(drw) = drw else {
return;
}
};
unsafe {
xlib::XCopyArea(
(*drw).dpy,
(*drw).drawable,
win,
(*drw).gc,
x,
y,
w,
h,
x,
y,
);
xlib::XSync((*drw).dpy, False);
xlib::XCopyArea(drw.dpy, drw.drawable, win, drw.gc, x, y, w, h, x, y);
xlib::XSync(drw.dpy, False);
}
}

pub(crate) fn resize(drw: *mut Drw, w: c_uint, h: c_uint) {
pub(crate) fn resize(drw: Option<&mut Drw>, w: c_uint, h: c_uint) {
unsafe {
if drw.is_null() {
let Some(drw) = drw else {
return;
};
drw.w = w;
drw.h = h;
if drw.drawable != 0 {
xlib::XFreePixmap(drw.dpy, drw.drawable);
}
(*drw).w = w;
(*drw).h = h;
if (*drw).drawable != 0 {
xlib::XFreePixmap((*drw).dpy, (*drw).drawable);
}
(*drw).drawable = xlib::XCreatePixmap(
(*drw).dpy,
(*drw).root,
drw.drawable = xlib::XCreatePixmap(
drw.dpy,
drw.root,
w,
h,
xlib::XDefaultDepth((*drw).dpy, (*drw).screen) as c_uint,
xlib::XDefaultDepth(drw.dpy, drw.screen) as c_uint,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
SW = ev.width;
SH = ev.height;
if updategeom() != 0 || dirty {
drw::resize(DRW, SW as c_uint, BH as c_uint);
drw::resize(DRW.as_mut(), SW as c_uint, BH as c_uint);

Check failure on line 363 in src/handlers.rs

View workflow job for this annotation

GitHub Actions / clippy

creating a mutable reference to mutable static is discouraged

error: creating a mutable reference to mutable static is discouraged --> src/handlers.rs:363:29 | 363 | drw::resize(DRW.as_mut(), SW as c_uint, BH as c_uint); | ^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html> = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

Check failure on line 363 in src/handlers.rs

View workflow job for this annotation

GitHub Actions / clippy

creating a mutable reference to mutable static is discouraged

error: creating a mutable reference to mutable static is discouraged --> src/handlers.rs:363:29 | 363 | drw::resize(DRW.as_mut(), SW as c_uint, BH as c_uint); | ^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html> = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
updatebars();
let mut m = MONS;
while !m.is_null() {
Expand Down
Loading
Loading