diff --git a/Cargo.toml b/Cargo.toml index 7e69b38..c6b3d3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,19 +14,11 @@ bitflags = "2" bytemuck = { version = "1.12", features = ["extern_crate_alloc", "derive"] } drm-ffi = { path = "drm-ffi", version = "0.6.0" } drm-fourcc = "^2.2.0" - -[dependencies.nix] -version = "0.27" -default-features = false -features = ["mman", "fs"] - -[dev-dependencies.nix] -version = "0.27" -default-features = false -features = ["mman", "poll"] +rustix = { version = "0.38.22", features = ["mm", "fs"] } [dev-dependencies] image = { version = "0.24", default-features = false, features = ["png"] } +rustix = { version = "0.38.22", features = ["event", "mm"] } rustyline = "12" [features] diff --git a/drm-ffi/Cargo.toml b/drm-ffi/Cargo.toml index d57c237..da2ad1b 100644 --- a/drm-ffi/Cargo.toml +++ b/drm-ffi/Cargo.toml @@ -10,11 +10,7 @@ edition = "2021" [dependencies] drm-sys = { path = "drm-sys", version = "0.5.0" } - -[dependencies.nix] -version = "0.27" -default-features = false -features = ["ioctl"] +rustix = { version = "0.38.22" } [features] use_bindgen = ["drm-sys/use_bindgen"] diff --git a/drm-ffi/src/gem.rs b/drm-ffi/src/gem.rs index 4d330d6..01b7f0b 100644 --- a/drm-ffi/src/gem.rs +++ b/drm-ffi/src/gem.rs @@ -18,7 +18,7 @@ pub fn open(fd: BorrowedFd<'_>, name: u32) -> io::Result { }; unsafe { - ioctl::gem::open(fd.as_raw_fd(), &mut gem)?; + ioctl::gem::open(fd, &mut gem)?; } Ok(gem) @@ -32,7 +32,7 @@ pub fn close(fd: BorrowedFd<'_>, handle: u32) -> io::Result { }; unsafe { - ioctl::gem::close(fd.as_raw_fd(), &gem)?; + ioctl::gem::close(fd, &gem)?; } Ok(gem) @@ -47,7 +47,7 @@ pub fn handle_to_fd(fd: BorrowedFd<'_>, handle: u32, flags: u32) -> io::Result, primefd: BorrowedFd<'_>) -> io::Result { + pub unsafe fn $name(fd: BorrowedFd, data: &mut $ty) -> io::Result<()> { + type Opcode = ReadWriteOpcode<$ioty, $nr, $ty>; + Ok(ioctl(fd, Updater::::new(data))?) + } + }; +} + +macro_rules! ioctl_read { + ($name:ident, $ioty:expr, $nr:expr, $ty:ty) => { + pub unsafe fn $name(fd: BorrowedFd) -> io::Result<$ty> { + type Opcode = ReadOpcode<$ioty, $nr, $ty>; + Ok(ioctl(fd, Getter::::new())?) + } + }; +} + +macro_rules! ioctl_write_ptr { + ($name:ident, $ioty:expr, $nr:expr, $ty:ty) => { + pub unsafe fn $name(fd: BorrowedFd, data: &$ty) -> io::Result<()> { + type Opcode = WriteOpcode<$ioty, $nr, $ty>; + Ok(ioctl(fd, Setter::::new(*data))?) + } + }; +} + +macro_rules! ioctl_none { + ($name:ident, $ioty:expr, $nr:expr) => { + pub unsafe fn $name(fd: BorrowedFd) -> io::Result<()> { + type Opcode = NoneOpcode<$ioty, $nr, ()>; + Ok(ioctl(fd, NoArg::::new())?) + } + }; +} /// Gets the bus ID of the device /// @@ -16,13 +55,6 @@ ioctl_readwrite!(get_bus_id, DRM_IOCTL_BASE, 0x01, drm_unique); /// # Nodes: Primary ioctl_readwrite!(get_client, DRM_IOCTL_BASE, 0x05, drm_client); -/// Gets statistical information from the device -/// -/// # Locks DRM mutex: No -/// # Permissions: None -/// # Nodes: Primary -ioctl_read!(get_stats, DRM_IOCTL_BASE, 0x06, drm_stats); - /// Get capabilities of the device. /// /// # Locks DRM mutex: No @@ -94,8 +126,7 @@ ioctl_readwrite!(get_irq_from_bus_id, DRM_IOCTL_BASE, 0x03, drm_irq_busid); ioctl_readwrite!(wait_vblank, DRM_IOCTL_BASE, 0x3a, drm_wait_vblank); pub(crate) mod mode { - use drm_sys::*; - use nix::libc::c_uint; + use super::*; /// Modesetting resources ioctl_readwrite!(get_resources, DRM_IOCTL_BASE, 0xA0, drm_mode_card_res); @@ -198,7 +229,7 @@ pub(crate) mod mode { } pub(crate) mod gem { - use drm_sys::*; + use super::*; /// GEM related functions ioctl_readwrite!(open, DRM_IOCTL_BASE, 0x0b, drm_gem_open); @@ -212,7 +243,7 @@ pub(crate) mod gem { } pub(crate) mod syncobj { - use drm_sys::*; + use super::*; /// Creates a syncobj. ioctl_readwrite!(create, DRM_IOCTL_BASE, 0xBF, drm_syncobj_create); diff --git a/drm-ffi/src/lib.rs b/drm-ffi/src/lib.rs index c63c955..4be9614 100644 --- a/drm-ffi/src/lib.rs +++ b/drm-ffi/src/lib.rs @@ -7,9 +7,6 @@ pub use drm_sys::{self, *}; -#[macro_use] -extern crate nix; - #[macro_use] pub(crate) mod utils; @@ -18,10 +15,10 @@ mod ioctl; pub mod mode; pub mod syncobj; -use nix::libc::{c_int, c_ulong}; use std::{ + ffi::{c_int, c_ulong}, io, - os::unix::io::{AsRawFd, BorrowedFd}, + os::unix::io::BorrowedFd, }; /// @@ -31,20 +28,11 @@ pub mod auth { use crate::ioctl; use drm_sys::*; - use std::{ - io, - os::unix::io::{AsRawFd, BorrowedFd}, - }; + use std::{io, os::unix::io::BorrowedFd}; /// Get the 'Magic Authentication Token' for this file descriptor. pub fn get_magic_token(fd: BorrowedFd<'_>) -> io::Result { - let mut auth = drm_auth::default(); - - unsafe { - ioctl::get_token(fd.as_raw_fd(), &mut auth)?; - } - - Ok(auth) + unsafe { ioctl::get_token(fd) } } /// Authorize another process' 'Magic Authentication Token'. @@ -52,7 +40,7 @@ pub mod auth { let token = drm_auth { magic: auth }; unsafe { - ioctl::auth_token(fd.as_raw_fd(), &token)?; + ioctl::auth_token(fd, &token)?; } Ok(token) @@ -60,20 +48,12 @@ pub mod auth { /// Acquire the 'Master DRM Lock' for this file descriptor. pub fn acquire_master(fd: BorrowedFd<'_>) -> io::Result<()> { - unsafe { - ioctl::acquire_master(fd.as_raw_fd())?; - } - - Ok(()) + unsafe { ioctl::acquire_master(fd) } } /// Release the 'Master DRM Lock' for this file descriptor. pub fn release_master(fd: BorrowedFd<'_>) -> io::Result<()> { - unsafe { - ioctl::release_master(fd.as_raw_fd())?; - } - - Ok(()) + unsafe { ioctl::release_master(fd) } } } @@ -81,7 +61,7 @@ pub mod auth { pub fn get_bus_id(fd: BorrowedFd<'_>, mut buf: Option<&mut Vec>) -> io::Result { let mut sizes = drm_unique::default(); unsafe { - ioctl::get_bus_id(fd.as_raw_fd(), &mut sizes)?; + ioctl::get_bus_id(fd, &mut sizes)?; } if buf.is_none() { @@ -96,7 +76,7 @@ pub fn get_bus_id(fd: BorrowedFd<'_>, mut buf: Option<&mut Vec>) -> io::Resu }; unsafe { - ioctl::get_bus_id(fd.as_raw_fd(), &mut busid)?; + ioctl::get_bus_id(fd, &mut busid)?; } map_set!(buf, busid.unique_len as usize); @@ -119,7 +99,7 @@ pub fn get_interrupt_from_bus_id( }; unsafe { - ioctl::get_irq_from_bus_id(fd.as_raw_fd(), &mut irq)?; + ioctl::get_irq_from_bus_id(fd, &mut irq)?; } Ok(irq) @@ -133,7 +113,7 @@ pub fn get_client(fd: BorrowedFd<'_>, idx: c_int) -> io::Result { }; unsafe { - ioctl::get_client(fd.as_raw_fd(), &mut client)?; + ioctl::get_client(fd, &mut client)?; } Ok(client) @@ -147,7 +127,7 @@ pub fn get_capability(fd: BorrowedFd<'_>, cty: u64) -> io::Result { }; unsafe { - ioctl::get_cap(fd.as_raw_fd(), &mut cap)?; + ioctl::get_cap(fd, &mut cap)?; } Ok(cap) @@ -161,12 +141,17 @@ pub fn set_capability(fd: BorrowedFd<'_>, cty: u64, val: bool) -> io::Result, version: &mut drm_set_version) -> io::Result<()> { + unsafe { ioctl::set_version(fd, version) } +} + /// Gets the driver version for this device. pub fn get_version( fd: BorrowedFd<'_>, @@ -176,7 +161,7 @@ pub fn get_version( ) -> io::Result { let mut sizes = drm_version::default(); unsafe { - ioctl::get_version(fd.as_raw_fd(), &mut sizes)?; + ioctl::get_version(fd, &mut sizes)?; } map_reserve!(name_buf, sizes.name_len as usize); @@ -194,7 +179,7 @@ pub fn get_version( }; unsafe { - ioctl::get_version(fd.as_raw_fd(), &mut version)?; + ioctl::get_version(fd, &mut version)?; } map_set!(name_buf, version.name_len as usize); @@ -223,7 +208,7 @@ pub fn wait_vblank( }; unsafe { - ioctl::wait_vblank(fd.as_raw_fd(), &mut wait_vblank)?; + ioctl::wait_vblank(fd, &mut wait_vblank)?; }; Ok(unsafe { wait_vblank.reply }) diff --git a/drm-ffi/src/mode.rs b/drm-ffi/src/mode.rs index 49c2d5a..1b30b8f 100644 --- a/drm-ffi/src/mode.rs +++ b/drm-ffi/src/mode.rs @@ -7,10 +7,7 @@ use crate::ioctl; use drm_sys::*; -use std::{ - io, - os::unix::io::{AsRawFd, BorrowedFd}, -}; +use std::{io, os::unix::io::BorrowedFd}; /// Enumerate most card resources. pub fn get_resources( @@ -22,7 +19,7 @@ pub fn get_resources( ) -> io::Result { let mut sizes = drm_mode_card_res::default(); unsafe { - ioctl::mode::get_resources(fd.as_raw_fd(), &mut sizes)?; + ioctl::mode::get_resources(fd, &mut sizes)?; } map_reserve!(fbs, sizes.count_fbs as usize); @@ -43,7 +40,7 @@ pub fn get_resources( }; unsafe { - ioctl::mode::get_resources(fd.as_raw_fd(), &mut res)?; + ioctl::mode::get_resources(fd, &mut res)?; } map_set!(fbs, res.count_fbs as usize); @@ -61,7 +58,7 @@ pub fn get_plane_resources( ) -> io::Result { let mut sizes = drm_mode_get_plane_res::default(); unsafe { - ioctl::mode::get_plane_resources(fd.as_raw_fd(), &mut sizes)?; + ioctl::mode::get_plane_resources(fd, &mut sizes)?; } if planes.is_none() { @@ -76,7 +73,7 @@ pub fn get_plane_resources( }; unsafe { - ioctl::mode::get_plane_resources(fd.as_raw_fd(), &mut res)?; + ioctl::mode::get_plane_resources(fd, &mut res)?; } map_set!(planes, res.count_planes as usize); @@ -92,7 +89,7 @@ pub fn get_framebuffer(fd: BorrowedFd<'_>, fb_id: u32) -> io::Result, fb_id: u32) -> io::Result, mut id: u32) -> io::Result<()> { unsafe { - ioctl::mode::rm_fb(fd.as_raw_fd(), &mut id)?; + ioctl::mode::rm_fb(fd, &mut id)?; } Ok(()) @@ -193,7 +190,7 @@ pub fn dirty_fb( }; unsafe { - ioctl::mode::dirty_fb(fd.as_raw_fd(), &mut dirty)?; + ioctl::mode::dirty_fb(fd, &mut dirty)?; } Ok(dirty) @@ -207,7 +204,7 @@ pub fn get_crtc(fd: BorrowedFd<'_>, crtc_id: u32) -> io::Result { }; unsafe { - ioctl::mode::get_crtc(fd.as_raw_fd(), &mut info)?; + ioctl::mode::get_crtc(fd, &mut info)?; } Ok(info) @@ -239,7 +236,7 @@ pub fn set_crtc( }; unsafe { - ioctl::mode::set_crtc(fd.as_raw_fd(), &mut crtc)?; + ioctl::mode::set_crtc(fd, &mut crtc)?; } Ok(crtc) @@ -263,7 +260,7 @@ pub fn get_gamma( }; unsafe { - ioctl::mode::get_gamma(fd.as_raw_fd(), &mut lut)?; + ioctl::mode::get_gamma(fd, &mut lut)?; } Ok(lut) @@ -287,7 +284,7 @@ pub fn set_gamma( }; unsafe { - ioctl::mode::set_gamma(fd.as_raw_fd(), &mut lut)?; + ioctl::mode::set_gamma(fd, &mut lut)?; } Ok(lut) @@ -315,7 +312,7 @@ pub fn set_cursor( }; unsafe { - ioctl::mode::cursor(fd.as_raw_fd(), &mut cursor)?; + ioctl::mode::cursor(fd, &mut cursor)?; } Ok(cursor) @@ -350,7 +347,7 @@ pub fn set_cursor2( }; unsafe { - ioctl::mode::cursor2(fd.as_raw_fd(), &mut cursor)?; + ioctl::mode::cursor2(fd, &mut cursor)?; } Ok(cursor) @@ -373,7 +370,7 @@ pub fn move_cursor( }; unsafe { - ioctl::mode::cursor(fd.as_raw_fd(), &mut cursor)?; + ioctl::mode::cursor(fd, &mut cursor)?; } Ok(cursor) @@ -404,7 +401,7 @@ pub fn get_connector( }; unsafe { - ioctl::mode::get_connector(fd.as_raw_fd(), &mut sizes)?; + ioctl::mode::get_connector(fd, &mut sizes)?; } let info = loop { @@ -444,7 +441,7 @@ pub fn get_connector( }; unsafe { - ioctl::mode::get_connector(fd.as_raw_fd(), &mut info)?; + ioctl::mode::get_connector(fd, &mut info)?; } if info.count_modes == sizes.count_modes @@ -473,7 +470,7 @@ pub fn get_encoder(fd: BorrowedFd<'_>, encoder_id: u32) -> io::Result, id: u32) -> io::Result, lessee_id: u32) -> io::Result<()> { let mut data = drm_mode_revoke_lease { lessee_id }; unsafe { - ioctl::mode::revoke_lease(fd.as_raw_fd(), &mut data)?; + ioctl::mode::revoke_lease(fd, &mut data)?; } Ok(()) @@ -883,10 +880,7 @@ pub mod dumbbuffer { use crate::ioctl; use drm_sys::*; - use std::{ - io, - os::unix::io::{AsRawFd, BorrowedFd}, - }; + use std::{io, os::unix::io::BorrowedFd}; /// Create a dumb buffer pub fn create( @@ -905,7 +899,7 @@ pub mod dumbbuffer { }; unsafe { - ioctl::mode::create_dumb(fd.as_raw_fd(), &mut db)?; + ioctl::mode::create_dumb(fd, &mut db)?; } Ok(db) @@ -916,7 +910,7 @@ pub mod dumbbuffer { let mut db = drm_mode_destroy_dumb { handle }; unsafe { - ioctl::mode::destroy_dumb(fd.as_raw_fd(), &mut db)?; + ioctl::mode::destroy_dumb(fd, &mut db)?; } Ok(db) @@ -936,7 +930,7 @@ pub mod dumbbuffer { }; unsafe { - ioctl::mode::map_dumb(fd.as_raw_fd(), &mut map)?; + ioctl::mode::map_dumb(fd, &mut map)?; } Ok(map) diff --git a/drm-ffi/src/syncobj.rs b/drm-ffi/src/syncobj.rs index 8dd9b5d..6cb45a9 100644 --- a/drm-ffi/src/syncobj.rs +++ b/drm-ffi/src/syncobj.rs @@ -22,7 +22,7 @@ pub fn create(fd: BorrowedFd<'_>, signaled: bool) -> io::Result, handle: u32) -> io::Result, handles: &[u32]) -> io::Result, handles: &[u32]) -> io::Result) { println!("{:#?}", token); } -/* -fn print_stats(fd: BorrowedFd<'_>) { - let stats = ffi::basic::get_stats(fd); - println!("{:#?}", stats); -} -*/ - fn main() { let card = Card::open_global(); let fd = card.as_fd(); diff --git a/examples/syncobj.rs b/examples/syncobj.rs index 0528248..05fbc50 100644 --- a/examples/syncobj.rs +++ b/examples/syncobj.rs @@ -2,7 +2,7 @@ pub mod utils; use crate::utils::*; -use nix::poll::PollFlags; +use rustix::event::PollFlags; use std::{ io, os::unix::io::{AsFd, OwnedFd}, @@ -48,6 +48,6 @@ fn main() { // let afd = AsyncFd::with_interest(sync_file, Interest::READABLE).unwrap(); // let future = async move { afd.readable().await.unwrap().retain_ready() }; // future.await; - let mut poll_fds = [nix::poll::PollFd::new(&fd, PollFlags::POLLIN)]; - nix::poll::poll(&mut poll_fds, -1).unwrap(); + let mut poll_fds = [rustix::event::PollFd::new(&fd, PollFlags::IN)]; + rustix::event::poll(&mut poll_fds, -1).unwrap(); } diff --git a/src/control/dumbbuffer.rs b/src/control/dumbbuffer.rs index a4f6603..d4a7ada 100644 --- a/src/control/dumbbuffer.rs +++ b/src/control/dumbbuffer.rs @@ -65,10 +65,9 @@ impl DerefMut for DumbMapping<'_> { impl<'a> Drop for DumbMapping<'a> { fn drop(&mut self) { - use nix::sys::mman; - unsafe { - mman::munmap(self.map.as_mut_ptr() as *mut _, self.map.len()).expect("Unmap failed"); + rustix::mm::munmap(self.map.as_mut_ptr() as *mut _, self.map.len()) + .expect("Unmap failed"); } } } diff --git a/src/control/mod.rs b/src/control/mod.rs index 8b67b2d..2b6f946 100644 --- a/src/control/mod.rs +++ b/src/control/mod.rs @@ -32,7 +32,7 @@ use drm_ffi as ffi; use drm_fourcc::{DrmFourcc, DrmModifier, UnrecognizedFourcc}; use bytemuck::allocation::TransparentWrapperAlloc; -use nix::libc::EINVAL; +use rustix::io::Errno; pub mod atomic; pub mod connector; @@ -57,15 +57,12 @@ use std::fmt; use std::io; use std::iter::Zip; use std::mem; -use std::num::NonZeroUsize; use std::ops::RangeBounds; -use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd}; +use std::os::unix::io::{AsFd, BorrowedFd, FromRawFd, OwnedFd, RawFd}; use std::time::Duration; use core::num::NonZeroU32; -pub use nix::fcntl::OFlag; - /// Raw handle for a drm resource pub type RawResourceHandle = NonZeroU32; @@ -609,7 +606,7 @@ pub trait Device: super::Device { || crtc_info.gamma_length as usize > green.len() || crtc_info.gamma_length as usize > blue.len() { - return Err(io::Error::from_raw_os_error(EINVAL)); + return Err(Errno::INVAL.into()); } ffi::mode::get_gamma( @@ -637,7 +634,7 @@ pub trait Device: super::Device { || crtc_info.gamma_length as usize > green.len() || crtc_info.gamma_length as usize > blue.len() { - return Err(io::Error::from_raw_os_error(EINVAL)); + return Err(Errno::INVAL.into()); } ffi::mode::set_gamma( @@ -688,19 +685,17 @@ pub trait Device: super::Device { let info = drm_ffi::mode::dumbbuffer::map(self.as_fd(), buffer.handle.into(), 0, 0)?; let map = { - use nix::sys::mman; - let prot = mman::ProtFlags::PROT_READ | mman::ProtFlags::PROT_WRITE; - let flags = mman::MapFlags::MAP_SHARED; - let length = NonZeroUsize::new(buffer.length) - .ok_or_else(|| io::Error::from_raw_os_error(EINVAL))?; + use rustix::mm; + let prot = mm::ProtFlags::READ | mm::ProtFlags::WRITE; + let flags = mm::MapFlags::SHARED; let fd = self.as_fd(); let offset = info.offset as _; - unsafe { mman::mmap(None, length, prot, flags, Some(fd), offset)? } + unsafe { mm::mmap(std::ptr::null_mut(), buffer.length, prot, flags, fd, offset)? } }; let mapping = DumbMapping { - _phantom: ::std::marker::PhantomData, - map: unsafe { ::std::slice::from_raw_parts_mut(map as *mut _, buffer.length) }, + _phantom: std::marker::PhantomData, + map: unsafe { std::slice::from_raw_parts_mut(map as *mut _, buffer.length) }, }; Ok(mapping) @@ -961,13 +956,9 @@ pub trait Device: super::Device { fn create_lease( &self, objects: &[RawResourceHandle], - flags: OFlag, + flags: u32, ) -> io::Result<(LeaseId, OwnedFd)> { - let lease = ffi::mode::create_lease( - self.as_fd(), - bytemuck::cast_slice(objects), - flags.bits() as u32, - )?; + let lease = ffi::mode::create_lease(self.as_fd(), bytemuck::cast_slice(objects), flags)?; Ok(( unsafe { NonZeroU32::new_unchecked(lease.lessee_id) }, unsafe { OwnedFd::from_raw_fd(lease.fd as RawFd) }, @@ -992,7 +983,7 @@ pub trait Device: super::Device { Self: Sized, { let mut event_buf: [u8; 1024] = [0; 1024]; - let amount = ::nix::unistd::read(self.as_fd().as_raw_fd(), &mut event_buf)?; + let amount = rustix::io::read(self.as_fd(), &mut event_buf)?; Ok(Events::with_event_buf(event_buf, amount)) } diff --git a/src/lib.rs b/src/lib.rs index de08b66..c60092f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ use std::{ os::unix::{ffi::OsStringExt, io::AsFd}, }; -use nix::libc::EINVAL; +use rustix::io::Errno; use crate::util::*; @@ -192,7 +192,7 @@ pub trait Device: AsFd { let high_crtc_mask = _DRM_VBLANK_HIGH_CRTC_MASK >> _DRM_VBLANK_HIGH_CRTC_SHIFT; if (high_crtc & !high_crtc_mask) != 0 { - return Err(io::Error::from_raw_os_error(EINVAL)); + return Err(Errno::INVAL.into()); } let (sequence, wait_type) = match target_sequence {