10 Commits
Author SHA1 Message Date
trurl 7457749177 Fix segfault from incorrect iteration over command line tokens. 2026-10-09 13:47:10 -04:00
trurl c09f029f83 Hit wings-rs-tests with rustfmt. 2026-10-08 14:51:31 -04:00
trurl 404e8736e4 Use new verify_png_snapshot! macro in insta-image for cleaner tests. 2026-10-08 14:50:14 -04:00
trurl e74328cc8e Delete transient snapshot files that should not have been pushed. 2026-10-08 14:50:02 -04:00
trurl 7915f964e4 Return a Result from tests that run Xvfb instead of panicking.
A test that runs Xvfb may not clean up properly if it panics. Returning a Result
instead of panicking allows drop-based cleanup to kick in.
2026-10-08 13:50:50 -04:00
trurl 1db8322b80 Check pointers that should be set by FontConfig for nullity. 2026-10-07 21:19:22 -04:00
trurl fafa459e90 Increase sleep delay slightly to run on wider range of hardware.
These tests failed when running on a slightly older desktop. Sleeping for longer
before taking the screenshot seems to fix things.
2026-10-07 21:19:18 -04:00
trurl 487ecd9ff8 Fiddle with pointer types to build on x86_64. 2026-10-07 21:19:18 -04:00
trurl ea304394a8 Fix build errors on some platforms by using c_char instead of u8 or i8.
We had previously only been building on ARM (an M1 MBP running Linux), which led
to code that didn't build on platforms with a differently signed c_char (like a
little-known platform called x86_64). This should now build both on ARM and on
x86_64.
2026-09-25 22:47:23 -04:00
trurl e54214df6d Use insta-image 1.0.1 to deal with breakage from changes in insta. 2026-09-25 22:44:33 -04:00
13 changed files with 147 additions and 132 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@ edition = "2024"
[dependencies]
libc = "0.2"
insta = "1.47"
insta-image = { version = "1.0", features = ["png"] }
insta-image = { version = "1.1.0", features = ["png"] }
png = "0.18"
tempdir = "0.3.7"
wings-rs = { path = "../wings-rs" }
+1 -1
View File
@@ -1,4 +1,4 @@
use wings_rs::{font_panel::FontPanel, WINGsP::WMScreenMainLoop};
use wings_rs::{WINGsP::WMScreenMainLoop, font_panel::FontPanel};
use wings_rs_tests::LiveApplication;
fn main() {
+11 -12
View File
@@ -16,8 +16,7 @@
//! number is needed.
use std::{
fs,
io,
fs, io,
path::PathBuf,
sync::atomic::{self, AtomicU16},
};
@@ -39,10 +38,7 @@ pub enum LockError {
impl Lock {
pub(crate) fn new(display: u16, path: PathBuf) -> Self {
Lock {
display,
path
}
Lock { display, path }
}
/// Returns the locked `DISPLAY` value.
@@ -97,18 +93,21 @@ impl DisplayNumberRegistry {
loop {
let prev = NEXT_DISPLAY.fetch_add(1, atomic::Ordering::SeqCst);
if prev == u16::MAX {
return Err(io::Error::other("display numbers exhausted; check /tmp/.X{n}-lock"));
return Err(io::Error::other(
"display numbers exhausted; check /tmp/.X{n}-lock",
));
}
let next = prev + 1;
let path = PathBuf::from(format!("/tmp/.X{}-lock", next));
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path) {
Ok(_) => return Ok(Lock::new(next, path)),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
.open(&path)
{
Ok(_) => return Ok(Lock::new(next, path)),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
}
}
}
+17 -17
View File
@@ -16,8 +16,8 @@ use std::{
};
use tempdir::TempDir;
use super::xwd;
use super::Lock;
use super::xwd;
/// Arguments for `Xvfb`. Use `XvfbArgs::default()` for default values.
#[derive(Clone, Copy, Debug)]
@@ -100,9 +100,8 @@ impl XvfbProcess {
};
let now = Instant::now();
let display_name = CString::new(
format!(":{}", process.lock.display)
).expect("could not construct display name");
let display_name = CString::new(format!(":{}", process.lock.display))
.expect("could not construct display name");
while now.elapsed() < Duration::from_secs(1) {
unsafe {
let display = x11::xlib::XOpenDisplay(display_name.as_ptr());
@@ -381,13 +380,10 @@ impl SubprocessMonitor {
#[cfg(test)]
mod tests {
use crate::headless::DisplayNumberRegistry;
use super::XvfbProcess;
use insta_image::assert_png_snapshot;
use std::{
thread,
time::Duration,
};
use crate::headless::DisplayNumberRegistry;
use insta_image::verify_png_snapshot;
use std::{error::Error, thread, time::Duration};
/// Starts Xvfb and returns a managing object, or panics.
fn start_xvfb() -> XvfbProcess {
@@ -423,7 +419,7 @@ mod tests {
}
#[test]
fn png_blank_screenshot() {
fn png_blank_screenshot() -> Result<(), Box<dyn Error>> {
let xvfb = start_xvfb();
let xwd = xvfb.xwd_screenshot().unwrap();
@@ -437,13 +433,15 @@ mod tests {
assert_eq!(xwd.header.width, args.width);
assert_eq!(xwd.header.height, args.height);
assert_png_snapshot!("blank_screen", xwd.into_png().unwrap());
verify_png_snapshot!("blank_screen", xwd.into_png().unwrap())?;
Ok(())
}
#[test]
fn png_xeyes_screenshot() {
fn png_xeyes_screenshot() -> Result<(), Box<dyn Error>> {
let mut xvfb = start_xvfb();
xeyes_color(&mut xvfb);
thread::sleep(Duration::from_millis(250));
let compressed = xvfb.png_screenshot();
let image_data = read_png_metadata(&compressed);
@@ -452,20 +450,22 @@ mod tests {
assert_eq!(image_data.width, args.width);
assert_eq!(image_data.height, args.height);
assert_png_snapshot!("xeyes", compressed);
verify_png_snapshot!("xeyes", compressed)?;
Ok(())
}
#[test]
fn png_lamp_image() {
fn png_lamp_image() -> Result<(), Box<dyn Error>> {
let mut xvfb = start_xvfb();
xvfb.run_args("xwud", "xwud", |c| {
c.arg("-in").arg("src/headless/snowlamp.xwd");
})
.unwrap();
thread::sleep(Duration::from_millis(100));
thread::sleep(Duration::from_millis(250));
let compressed = xvfb.png_screenshot();
assert_png_snapshot!("snowlamp_in_window", compressed);
verify_png_snapshot!("snowlamp_in_window", compressed)?;
Ok(())
}
}
+1 -5
View File
@@ -1,10 +1,6 @@
//! Provides basic support for rendering dumps of X11 windows made with [the `xwud` utility](https://gitlab.freedesktop.org/xorg/app/xwud/-/blob/master/xwud.c?ref_type=heads).
use std::{
ffi::CString,
io::Cursor,
mem,
};
use std::{ffi::CString, io::Cursor, mem};
const SUPPORTED_XWD_VERSION: u32 = 7;
+2 -2
View File
@@ -1,9 +1,9 @@
pub mod headless;
use headless::{xvfb::XvfbProcess, DisplayNumberRegistry};
use headless::{DisplayNumberRegistry, xvfb::XvfbProcess};
use std::{
env,
ffi::{c_char, c_int, CStr, CString},
ffi::{CStr, CString, c_char, c_int},
ptr::{self, NonNull},
sync::Mutex,
};
@@ -1,14 +1,15 @@
use insta_image::assert_png_snapshot;
use std::time::Instant;
use insta_image::verify_png_snapshot;
use std::{error::Error, time::Instant};
use wings_rs::font_panel::FontPanel;
use wings_rs_tests::HeadlessApplication;
#[test]
fn show_font_panel() {
fn show_font_panel() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
let mut font_panel =
FontPanel::new(app.screen.as_ptr()).expect("could not construct font panel");
font_panel.show();
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("font_panel", app.xvfb.png_screenshot());
let screenshot = app.xvfb.png_screenshot();
verify_png_snapshot!("font_panel", screenshot)
}
+59 -48
View File
@@ -1,5 +1,6 @@
use insta_image::assert_png_snapshot;
use insta_image::verify_png_snapshot;
use std::{
error::Error,
ptr::{self, NonNull},
time::Instant,
};
@@ -12,7 +13,7 @@ use wings_rs_tests::HeadlessApplication;
use wrlib_rs::ffi::{RColor, RLoadImage, RReleaseImage};
#[test]
fn draw_blank_pixmap() {
fn draw_blank_pixmap() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -29,28 +30,30 @@ fn draw_blank_pixmap() {
);
x11::xlib::XMapWindow(display, win);
let pixmap = NonNull::new(WMCreatePixmap(app.screen.as_ptr(), 128, 196, 24, 1)).unwrap();
let pixmap = NonNull::new(WMCreatePixmap(app.screen.as_ptr(), 128, 196, 24, 1))
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("empty_window", app.xvfb.png_screenshot());
verify_png_snapshot!("empty_window", app.xvfb.png_screenshot())?;
let gc = x11::xlib::XCreateGC(display, (*pixmap.as_ptr()).pixmap, 0, ptr::null_mut());
x11::xlib::XSetForeground(display, gc, 0);
x11::xlib::XFillRectangle(display, (*pixmap.as_ptr()).pixmap, gc, 0, 0, 128, 196);
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("empty_pixmap_on_window", app.xvfb.png_screenshot());
verify_png_snapshot!("empty_pixmap_on_window", app.xvfb.png_screenshot())?;
x11::xlib::XFreeGC(display, gc);
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
// WMCreatePixmapFromXPixmaps is not tested because it simply fills in struct
// fields. Coverage from other tests in this file should be adequate.
#[test]
fn draw_pixmap_from_file() {
fn draw_pixmap_from_file() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -71,17 +74,18 @@ fn draw_pixmap_from_file() {
app.screen.as_ptr(),
c"tests/image_128x120.png".as_ptr(),
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("from_file_pixmap", app.xvfb.png_screenshot());
verify_png_snapshot!("from_file_pixmap", app.xvfb.png_screenshot())?;
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
#[test]
fn draw_pixmap_from_r_image_no_mask() {
fn draw_pixmap_from_r_image_no_mask() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -103,18 +107,19 @@ fn draw_pixmap_from_r_image_no_mask() {
c"tests/image_128x120.png".as_ptr(),
0,
);
let pixmap =
NonNull::new(WMCreatePixmapFromRImage(app.screen.as_ptr(), rimage, 255)).unwrap();
let pixmap = NonNull::new(WMCreatePixmapFromRImage(app.screen.as_ptr(), rimage, 255))
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 70, 74);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("from_r_image_no_mask", app.xvfb.png_screenshot());
verify_png_snapshot!("from_r_image_no_mask", app.xvfb.png_screenshot())?;
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
#[test]
fn draw_pixmap_from_r_image_with_mask() {
fn draw_pixmap_from_r_image_with_mask() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -136,7 +141,7 @@ fn draw_pixmap_from_r_image_with_mask() {
c"tests/image_128x120_varying_alpha.png".as_ptr(),
255,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
// The threshold given to WMCreatePixmapFromRImage is compared against
// the image's alpha channel. Pixels whose transparency are below the
@@ -149,7 +154,7 @@ fn draw_pixmap_from_r_image_with_mask() {
rimage.as_ptr(),
255,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap_255.as_ptr(), win, 70, 74);
// Three bubbles masked.
let pixmap_200 = NonNull::new(WMCreatePixmapFromRImage(
@@ -157,7 +162,7 @@ fn draw_pixmap_from_r_image_with_mask() {
rimage.as_ptr(),
200,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap_200.as_ptr(), win, 200, 74);
// Two bubbles masked.
let pixmap_128 = NonNull::new(WMCreatePixmapFromRImage(
@@ -165,7 +170,7 @@ fn draw_pixmap_from_r_image_with_mask() {
rimage.as_ptr(),
128,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap_128.as_ptr(), win, 70, 200);
// Nothing masked.
let pixmap_0 = NonNull::new(WMCreatePixmapFromRImage(
@@ -173,21 +178,22 @@ fn draw_pixmap_from_r_image_with_mask() {
rimage.as_ptr(),
0,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap_0.as_ptr(), win, 200, 200);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("from_r_image_with_mask", app.xvfb.png_screenshot());
verify_png_snapshot!("from_r_image_with_mask", app.xvfb.png_screenshot())?;
WMReleasePixmap(pixmap_255.as_ptr());
WMReleasePixmap(pixmap_200.as_ptr());
WMReleasePixmap(pixmap_128.as_ptr());
WMReleasePixmap(pixmap_0.as_ptr());
RReleaseImage(rimage.as_ptr());
}
Ok(())
}
#[test]
fn draw_blended_pixmap_from_r_image() {
fn draw_blended_pixmap_from_r_image() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -219,17 +225,18 @@ fn draw_blended_pixmap_from_r_image() {
alpha: 255,
} as *const _,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 70, 74);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("from_r_image_no_mask", app.xvfb.png_screenshot());
verify_png_snapshot!("from_r_image_no_mask", app.xvfb.png_screenshot())?;
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
#[test]
fn draw_blended_pixmap_from_file() {
fn draw_blended_pixmap_from_file() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -250,10 +257,10 @@ fn draw_blended_pixmap_from_file() {
app.screen.as_ptr(),
c"tests/image_with_transparent_dot_128x120.png".as_ptr(),
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("from_file_blended_base_pixmap", app.xvfb.png_screenshot());
verify_png_snapshot!("from_file_blended_base_pixmap", app.xvfb.png_screenshot())?;
x11::xlib::XUnmapWindow(display, win);
WMReleasePixmap(pixmap.as_ptr());
@@ -271,10 +278,10 @@ fn draw_blended_pixmap_from_file() {
);
x11::xlib::XMapWindow(display, win);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_blended_cleared_window",
app.xvfb.png_screenshot()
);
)?;
let pixmap = NonNull::new(WMCreateBlendedPixmapFromFile(
app.screen.as_ptr(),
@@ -286,17 +293,19 @@ fn draw_blended_pixmap_from_file() {
alpha: 255,
} as *const _,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!("from_file_blended_with_red", app.xvfb.png_screenshot());
verify_png_snapshot!("from_file_blended_with_red", app.xvfb.png_screenshot())?;
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
#[test]
fn draw_scaled_blended_pixmap_from_file() {
fn draw_scaled_blended_pixmap_from_file() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -317,13 +326,13 @@ fn draw_scaled_blended_pixmap_from_file() {
app.screen.as_ptr(),
c"tests/image_with_transparent_dot_128x120.png".as_ptr(),
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_scaled_blended_base_pixmap",
app.xvfb.png_screenshot()
);
)?;
x11::xlib::XUnmapWindow(display, win);
WMReleasePixmap(pixmap.as_ptr());
@@ -341,10 +350,10 @@ fn draw_scaled_blended_pixmap_from_file() {
);
x11::xlib::XMapWindow(display, win);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_scaled_blended_cleared_window",
app.xvfb.png_screenshot()
);
)?;
let pixmap = NonNull::new(WMCreateScaledBlendedPixmapFromFile(
app.screen.as_ptr(),
@@ -358,20 +367,21 @@ fn draw_scaled_blended_pixmap_from_file() {
60,
64,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_scaled_blended_with_red",
app.xvfb.png_screenshot()
);
)?;
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
#[test]
fn draw_scaled_blended_pixmap_from_file_non_integral_scaling() {
fn draw_scaled_blended_pixmap_from_file_non_integral_scaling() -> Result<(), Box<dyn Error>> {
let mut app = HeadlessApplication::new();
unsafe {
let display = (*app.screen.as_ptr()).display;
@@ -392,13 +402,13 @@ fn draw_scaled_blended_pixmap_from_file_non_integral_scaling() {
app.screen.as_ptr(),
c"tests/image_128x120.png".as_ptr(),
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_scaled_blended_non_integral_base_pixmap",
app.xvfb.png_screenshot()
);
)?;
x11::xlib::XUnmapWindow(display, win);
WMReleasePixmap(pixmap.as_ptr());
@@ -416,10 +426,10 @@ fn draw_scaled_blended_pixmap_from_file_non_integral_scaling() {
);
x11::xlib::XMapWindow(display, win);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_scaled_blended_non_integral_cleared_window",
app.xvfb.png_screenshot()
);
)?;
let pixmap = NonNull::new(WMCreateScaledBlendedPixmapFromFile(
app.screen.as_ptr(),
@@ -437,14 +447,15 @@ fn draw_scaled_blended_pixmap_from_file_non_integral_scaling() {
110,
114,
))
.unwrap();
.ok_or_else(|| Box::<dyn Error>::from("could not allocate pixmap resource"))?;
WMDrawPixmap(pixmap.as_ptr(), win, 64, 96);
while app.pump_event_queue(Instant::now()) {}
assert_png_snapshot!(
verify_png_snapshot!(
"from_file_scaled_blended_non_integral_scaled",
app.xvfb.png_screenshot()
);
)?;
WMReleasePixmap(pixmap.as_ptr());
}
Ok(())
}
+3 -3
View File
@@ -1,6 +1,6 @@
use crate::WINGsP::WMScreen;
use std::cell::RefCell;
use std::ffi::c_ulong;
use std::ffi::{c_char, c_ulong};
use std::ptr::{self, NonNull};
use wrlib_rs::ffi::{RColor, RGetClosestXColor};
@@ -46,10 +46,10 @@ pub struct Rgba {
impl Color {
const LIGHT_STIPPLE_WIDTH: u32 = 4;
const LIGHT_STIPPLE_HEIGHT: u32 = 4;
const LIGHT_STIPPLE_BITS: [u8; 4] = [0x05, 0x0a, 0x05, 0x0a];
const LIGHT_STIPPLE_BITS: [c_char; 4] = [0x05, 0x0a, 0x05, 0x0a];
const DARK_STIPPLE_WIDTH: u32 = 4;
const DARK_STIPPLE_HEIGHT: u32 = 4;
const DARK_STIPPLE_BITS: [u8; 4] = [0x0a, 0x04, 0x0a, 0x01];
const DARK_STIPPLE_BITS: [c_char; 4] = [0x0a, 0x04, 0x0a, 0x01];
pub fn new_rgb(screen: NonNull<WMScreen>, rgb: Rgb, mtch: ColorMatch) -> Option<Self> {
Self::new_rgba(screen, rgb.with_alpha(0xffff), mtch)
+10 -10
View File
@@ -1,7 +1,7 @@
use crate::pango_extras;
use std::{
ffi::{CStr, CString, c_double, c_int, c_uint, c_void},
ffi::{CStr, CString, c_char, c_double, c_int, c_uint, c_void},
ptr::NonNull,
rc::Rc,
};
@@ -33,7 +33,7 @@ pub struct Font {
fn xlfd_to_fc_pattern(xlfd: &str) -> Option<NonNull<x11::xft::FcPattern>> {
if xlfd.contains('%') {
return NonNull::new(unsafe {
fontconfig_sys::FcNameParse(DEFAULT_FONT_CSTR.as_ptr()).cast::<x11::xft::FcPattern>()
fontconfig_sys::FcNameParse(DEFAULT_FONT_CSTR.as_ptr().cast()).cast::<x11::xft::FcPattern>()
});
}
@@ -42,7 +42,7 @@ fn xlfd_to_fc_pattern(xlfd: &str) -> Option<NonNull<x11::xft::FcPattern>> {
let complete = 0;
NonNull::new(unsafe {
x11::xft::XftXlfdParse(
name.as_ptr(),
name.as_ptr().cast(),
ignore_scalable,
complete,
)
@@ -78,7 +78,7 @@ impl FontName {
let pattern = xlfd_to_fc_pattern(name)?;
let name = unsafe { fontconfig_sys::FcNameUnparse(pattern.as_ptr().cast::<c_void>()) };
let name = NonNull::new(name)?;
let result = unsafe { CStr::from_ptr(name.as_ptr()) }.to_str().ok()?;
let result = unsafe { CStr::from_ptr(name.as_ptr().cast()) }.to_str().ok()?;
unsafe {
libc::free(name.as_ptr().cast::<c_void>());
fontconfig_sys::FcPatternDestroy(pattern.as_ptr().cast::<c_void>());
@@ -103,7 +103,7 @@ impl FontName {
/// Returns a C-style string owned by this object that names the font in a
/// way that is suitable for `x11::xft::XftFontOpenName` and other Xft
/// functions.
pub fn as_xft_name(&self) -> *const u8 {
pub fn as_xft_name(&self) -> *const c_char {
self.0.as_ptr()
}
@@ -125,7 +125,7 @@ impl Font {
let fontmap = unsafe { pango_extras::pango_xft_get_font_map(display, screen) };
let context = unsafe { pango_sys::pango_font_map_create_context(fontmap) };
let layout = NonNull::new(unsafe { pango_sys::pango_layout_new(context) })?;
let pattern = unsafe { fontconfig_sys::FcNameParse(name.as_xft_name()) };
let pattern = unsafe { fontconfig_sys::FcNameParse(name.as_xft_name().cast()) };
let description =
unsafe { pango_extras::pango_fc_font_description_from_pattern(pattern.cast(), 0) };
@@ -166,7 +166,7 @@ impl Font {
name: &FontName,
size: u32,
) -> Option<Self> {
let pattern = unsafe { fontconfig_sys::FcNameParse(name.as_xft_name()) };
let pattern = unsafe { fontconfig_sys::FcNameParse(name.as_xft_name().cast()) };
unsafe {
fontconfig_sys::FcPatternDel(
pattern,
@@ -184,7 +184,7 @@ impl Font {
fontconfig_sys::FcPatternDestroy(pattern);
}
let name = NonNull::new(name)?;
let font_name = FontName(Rc::from(unsafe { CStr::from_ptr(name.as_ptr()) }));
let font_name = FontName(Rc::from(unsafe { CStr::from_ptr(name.as_ptr().cast()) }));
unsafe {
libc::free(name.as_ptr().cast::<c_void>());
}
@@ -438,10 +438,10 @@ pub mod ffi {
} else {
let length = length as usize;
let previous_text = unsafe { CStr::from_ptr(previous_text).to_bytes() };
let text = unsafe { std::slice::from_raw_parts(text, length) };
let text = unsafe { std::slice::from_raw_parts(text.cast::<u8>(), length) };
if previous_text.len() < length || previous_text[0..length] != text[0..length] {
unsafe {
pango_sys::pango_layout_set_text(layout, text.as_ptr(), length as i32);
pango_sys::pango_layout_set_text(layout, text.as_ptr().cast(), length as i32);
}
}
}
+17 -9
View File
@@ -8,7 +8,7 @@ use crate::{
use std::{
collections::{BTreeMap, btree_map::Entry},
ffi::{CStr, CString, c_int, c_void},
ffi::{CStr, CString, c_char, c_int, c_void},
pin::Pin,
ptr::{self, NonNull},
rc::Rc,
@@ -51,7 +51,7 @@ impl FontLibrary {
}
let object_set = unsafe {
fontconfig_sys::FcObjectSetBuild(
fontconfig_sys::constants::FC_FAMILY.as_ptr() as *mut u8,
fontconfig_sys::constants::FC_FAMILY.as_ptr() as *mut c_char,
fontconfig_sys::constants::FC_STYLE.as_ptr(),
ptr::null_mut::<c_void>(),
)
@@ -82,16 +82,20 @@ impl FontLibrary {
for font in
unsafe { std::slice::from_raw_parts_mut((*fonts).fonts, (*fonts).nfont as usize) }
{
let mut family = ptr::null_mut();
let mut style = ptr::null_mut();
let family: *mut c_char = ptr::null_mut();
let style: *mut c_char = ptr::null_mut();
unsafe {
if fontconfig_sys::FcPatternGetString(
*font,
fontconfig_sys::constants::FC_FAMILY.as_ptr(),
0,
&mut family as *mut _,
&mut family.cast() as *mut _,
) == fontconfig_sys::FcResultMatch
{
if family.is_null() {
// TODO: complain.
continue;
}
let Ok(family) = CStr::from_ptr(family).to_str().map(String::from) else {
// TODO: complain.
continue;
@@ -100,9 +104,13 @@ impl FontLibrary {
*font,
fontconfig_sys::constants::FC_STYLE.as_ptr(),
0,
&mut style as *mut _,
&mut style.cast() as *mut _,
) == fontconfig_sys::FcResultMatch
{
if style.is_null() {
// TODO: complain.
continue;
}
let Ok(style) = CStr::from_ptr(style).to_str().map(String::from) else {
// TODO: complain.
continue;
@@ -668,7 +676,7 @@ impl FontPanel {
let face_index = selected_type
.map(|x| unsafe {
WMFindRowOfListItemWithTitle(self.type_list.as_ptr(), x.name.as_ptr())
WMFindRowOfListItemWithTitle(self.type_list.as_ptr(), x.name.as_ptr().cast())
})
.unwrap_or(0);
@@ -973,7 +981,7 @@ pub mod ffi {
};
unsafe {
let pattern = fontconfig_sys::FcNameParse(name.as_xft_name());
let pattern = fontconfig_sys::FcNameParse(name.as_xft_name().cast());
if pattern.is_null() {
return;
}
@@ -1001,7 +1009,7 @@ pub mod ffi {
&mut size,
) == fontconfig_sys::FcResultMatch
{
panel.set_font_name(CStr::from_ptr(family), CStr::from_ptr(style), size);
panel.set_font_name(CStr::from_ptr(family.cast()), CStr::from_ptr(style.cast()), size);
}
}
}
+15 -15
View File
@@ -1,14 +1,14 @@
use hashbrown::hash_map::{self, HashMap};
use std::{
ffi::CStr,
ffi::{CStr, c_char},
hash::{Hash, Hasher},
mem,
};
pub enum HashTable {
PointerKeyed(HashMap<*mut u8, *mut u8>),
StringKeyed(HashMap<StringKey, *mut u8>),
PointerKeyed(HashMap<*mut c_char, *mut c_char>),
StringKeyed(HashMap<StringKey, *mut c_char>),
}
impl HashTable {
@@ -34,7 +34,7 @@ impl HashTable {
}
}
pub unsafe fn get(&self, key: *const u8) -> Option<*mut u8> {
pub unsafe fn get(&self, key: *const c_char) -> Option<*mut c_char> {
match self {
HashTable::PointerKeyed(m) => {
let key = key.cast_mut();
@@ -47,14 +47,14 @@ impl HashTable {
}
}
pub unsafe fn insert(&mut self, key: *mut u8, data: *mut u8) -> Option<*mut u8> {
pub unsafe fn insert(&mut self, key: *mut c_char, data: *mut c_char) -> Option<*mut c_char> {
match self {
HashTable::PointerKeyed(m) => m.insert(key, data),
HashTable::StringKeyed(m) => m.insert(StringKey(key), data),
}
}
pub unsafe fn remove(&mut self, key: *const u8) {
pub unsafe fn remove(&mut self, key: *const c_char) {
match self {
HashTable::PointerKeyed(m) => {
let key = key.cast_mut();
@@ -70,7 +70,7 @@ impl HashTable {
}
#[repr(transparent)]
pub struct StringKey(*const u8);
pub struct StringKey(*const c_char);
impl PartialEq for StringKey {
fn eq(&self, other: &Self) -> bool {
@@ -101,13 +101,13 @@ impl Hash for StringKey {
}
pub enum Enumerator<'a> {
PointerKeyed(hash_map::IterMut<'a, *mut u8, *mut u8>),
StringKeyed(hash_map::IterMut<'a, StringKey, *mut u8>),
PointerKeyed(hash_map::IterMut<'a, *mut c_char, *mut c_char>),
StringKeyed(hash_map::IterMut<'a, StringKey, *mut c_char>),
}
pub mod ffi {
use std::{
ffi::{c_int, c_uint, c_void},
ffi::{c_char, c_int, c_uint, c_void},
mem, ptr,
};
@@ -159,7 +159,7 @@ pub mod ffi {
if table.is_null() {
return ptr::null_mut();
}
let key = key.cast::<u8>();
let key = key.cast::<c_char>();
(unsafe { (*table).get(key) })
.map(|v| v.cast::<c_void>())
.unwrap_or(ptr::null_mut())
@@ -178,7 +178,7 @@ pub mod ffi {
let table = unsafe { &mut *table };
match table {
HashTable::PointerKeyed(m) => {
let result = match m.get_key_value_mut(&key.cast::<u8>().cast_mut()) {
let result = match m.get_key_value_mut(&key.cast::<c_char>().cast_mut()) {
Some((k, v)) => {
unsafe {
*key_dest = k.cast::<c_void>();
@@ -191,7 +191,7 @@ pub mod ffi {
result
}
HashTable::StringKeyed(m) => {
let key = StringKey(key.cast::<u8>().cast_mut());
let key = StringKey(key.cast::<c_char>().cast_mut());
let result = match m.get_key_value_mut(&key) {
Some((k, v)) => {
unsafe {
@@ -218,7 +218,7 @@ pub mod ffi {
if table.is_null() {
return ptr::null_mut();
}
match unsafe { (*table).insert(key.cast::<u8>(), data.cast::<u8>()) } {
match unsafe { (*table).insert(key.cast::<c_char>(), data.cast::<c_char>()) } {
Some(v) => v.cast::<c_void>(),
None => ptr::null_mut(),
}
@@ -231,7 +231,7 @@ pub mod ffi {
return;
}
unsafe {
(*table).remove(key.cast::<u8>());
(*table).remove(key.cast::<c_char>());
}
}
+5 -5
View File
@@ -109,7 +109,7 @@ pub unsafe extern "C" fn wtrimspace(s: *const c_char) -> *mut c_char {
return ptr::null_mut();
};
let trimmed = s.trim();
let ptr = trimmed.as_ptr();
let ptr = trimmed.as_ptr().cast::<c_char>();
let len = trimmed.len();
unsafe { wstrndup(ptr, len) }
}
@@ -221,10 +221,10 @@ pub unsafe extern "C" fn wtokenfree(tokens: *mut *mut c_char, count: c_int) {
return;
}
if count > 0 {
let tokens = unsafe { slice::from_raw_parts_mut(tokens, count as usize) };
for token in tokens {
let tokens: &[*mut c_char] = unsafe { slice::from_raw_parts_mut(tokens, count as usize) };
for i in 0..count {
unsafe {
free_bytes(*token);
free_bytes(tokens[i as usize].cast());
}
}
}
@@ -249,7 +249,7 @@ pub unsafe extern "C" fn wtokenjoin(list: *const *const char, count: c_int) -> *
}
let list = unsafe {
slice::from_raw_parts(list.cast::<*const u8>(), count as usize)
slice::from_raw_parts(list.cast::<*const c_char>(), count as usize)
};
let mut buffer = Vec::new();