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

Use Rustix #180

Merged
merged 4 commits into from
Nov 14, 2023
Merged
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
12 changes: 2 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 1 addition & 5 deletions drm-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
8 changes: 4 additions & 4 deletions drm-ffi/src/gem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn open(fd: BorrowedFd<'_>, name: u32) -> io::Result<drm_gem_open> {
};

unsafe {
ioctl::gem::open(fd.as_raw_fd(), &mut gem)?;
ioctl::gem::open(fd, &mut gem)?;
}

Ok(gem)
Expand All @@ -32,7 +32,7 @@ pub fn close(fd: BorrowedFd<'_>, handle: u32) -> io::Result<drm_gem_close> {
};

unsafe {
ioctl::gem::close(fd.as_raw_fd(), &gem)?;
ioctl::gem::close(fd, &gem)?;
}

Ok(gem)
Expand All @@ -47,7 +47,7 @@ pub fn handle_to_fd(fd: BorrowedFd<'_>, handle: u32, flags: u32) -> io::Result<d
};

unsafe {
ioctl::gem::prime_handle_to_fd(fd.as_raw_fd(), &mut prime)?;
ioctl::gem::prime_handle_to_fd(fd, &mut prime)?;
}

Ok(prime)
Expand All @@ -61,7 +61,7 @@ pub fn fd_to_handle(fd: BorrowedFd<'_>, primefd: BorrowedFd<'_>) -> io::Result<d
};

unsafe {
ioctl::gem::prime_fd_to_handle(fd.as_raw_fd(), &mut prime)?;
ioctl::gem::prime_fd_to_handle(fd, &mut prime)?;
}

Ok(prime)
Expand Down
55 changes: 43 additions & 12 deletions drm-ffi/src/ioctl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
#![allow(missing_docs)]
use std::{ffi::c_uint, io, os::unix::io::BorrowedFd};

use drm_sys::*;
use rustix::ioctl::{
ioctl, Getter, NoArg, NoneOpcode, ReadOpcode, ReadWriteOpcode, Setter, Updater, WriteOpcode,
};

macro_rules! ioctl_readwrite {
($name:ident, $ioty:expr, $nr:expr, $ty:ty) => {
pub unsafe fn $name(fd: BorrowedFd, data: &mut $ty) -> io::Result<()> {
type Opcode = ReadWriteOpcode<$ioty, $nr, $ty>;
Ok(ioctl(fd, Updater::<Opcode, $ty>::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::<Opcode, $ty>::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::<Opcode, $ty>::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::<Opcode>::new())?)
}
};
}

/// Gets the bus ID of the device
///
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
57 changes: 21 additions & 36 deletions drm-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

pub use drm_sys::{self, *};

#[macro_use]
extern crate nix;

#[macro_use]
pub(crate) mod utils;

Expand All @@ -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,
};

///
Expand All @@ -31,57 +28,40 @@ 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<drm_auth> {
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'.
pub fn auth_magic_token(fd: BorrowedFd<'_>, auth: u32) -> io::Result<drm_auth> {
let token = drm_auth { magic: auth };

unsafe {
ioctl::auth_token(fd.as_raw_fd(), &token)?;
ioctl::auth_token(fd, &token)?;
}

Ok(token)
}

/// 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) }
}
}

/// Load this device's Bus ID into a buffer.
pub fn get_bus_id(fd: BorrowedFd<'_>, mut buf: Option<&mut Vec<u8>>) -> io::Result<drm_unique> {
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() {
Expand All @@ -96,7 +76,7 @@ pub fn get_bus_id(fd: BorrowedFd<'_>, mut buf: Option<&mut Vec<u8>>) -> 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);
Expand All @@ -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)
Expand All @@ -133,7 +113,7 @@ pub fn get_client(fd: BorrowedFd<'_>, idx: c_int) -> io::Result<drm_client> {
};

unsafe {
ioctl::get_client(fd.as_raw_fd(), &mut client)?;
ioctl::get_client(fd, &mut client)?;
}

Ok(client)
Expand All @@ -147,7 +127,7 @@ pub fn get_capability(fd: BorrowedFd<'_>, cty: u64) -> io::Result<drm_get_cap> {
};

unsafe {
ioctl::get_cap(fd.as_raw_fd(), &mut cap)?;
ioctl::get_cap(fd, &mut cap)?;
}

Ok(cap)
Expand All @@ -161,12 +141,17 @@ pub fn set_capability(fd: BorrowedFd<'_>, cty: u64, val: bool) -> io::Result<drm
};

unsafe {
ioctl::set_cap(fd.as_raw_fd(), &cap)?;
ioctl::set_cap(fd, &cap)?;
}

Ok(cap)
}

/// Sets the requested interface version
pub fn set_version(fd: BorrowedFd<'_>, 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<'_>,
Expand All @@ -176,7 +161,7 @@ pub fn get_version(
) -> io::Result<drm_version> {
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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 })
Expand Down
Loading
Loading