Skip to content

Commit

Permalink
Use std::io::Error for error results, except get_planar_framebuffer
Browse files Browse the repository at this point in the history
Based on discussion on #173. Which
this supersedes.

All functions in `drm` and `drm_ffi` returning `SystemError`, except
`get_planar_frambuffer`, only use it for representing standard Unix
error kinds. So `std::io::Error` can be used instead.

For `get_planar_framebuffer`, a `GetPlanarFramebufferError` error is
defined. It might be nice if Rust has anonymous sum types for this sort
of thing, but this seems like the typical pattern. And better than using
the same error kind everywhere if `UnrecognizedFourcc` isn't possible in
any other function.

This should remove `nix` from the public API of the crate. This makes
errors a bit simpler to handle for users of the library using `rustix`
or a different version of `nix`, and allows `drm` to change which it
uses without a breaking API change.
  • Loading branch information
ids1024 committed Nov 7, 2023
1 parent 0c5c8fa commit 34fcd78
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 265 deletions.
22 changes: 8 additions & 14 deletions drm-ffi/src/gem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use crate::ioctl;
use drm_sys::*;

use crate::result::SystemError as Error;

use std::os::unix::io::{AsRawFd, BorrowedFd};
use std::{
io,
os::unix::io::{AsRawFd, BorrowedFd},
};

/// Open a GEM object given it's 32-bit name, returning the handle.
pub fn open(fd: BorrowedFd<'_>, name: u32) -> Result<drm_gem_open, Error> {
pub fn open(fd: BorrowedFd<'_>, name: u32) -> io::Result<drm_gem_open> {
let mut gem = drm_gem_open {
name,
..Default::default()
Expand All @@ -24,7 +25,7 @@ pub fn open(fd: BorrowedFd<'_>, name: u32) -> Result<drm_gem_open, Error> {
}

/// Closes a GEM object given it's handle.
pub fn close(fd: BorrowedFd<'_>, handle: u32) -> Result<drm_gem_close, Error> {
pub fn close(fd: BorrowedFd<'_>, handle: u32) -> io::Result<drm_gem_close> {
let gem = drm_gem_close {
handle,
..Default::default()
Expand All @@ -38,11 +39,7 @@ pub fn close(fd: BorrowedFd<'_>, handle: u32) -> Result<drm_gem_close, Error> {
}

/// Converts a GEM object's handle to a PRIME file descriptor.
pub fn handle_to_fd(
fd: BorrowedFd<'_>,
handle: u32,
flags: u32,
) -> Result<drm_prime_handle, Error> {
pub fn handle_to_fd(fd: BorrowedFd<'_>, handle: u32, flags: u32) -> io::Result<drm_prime_handle> {
let mut prime = drm_prime_handle {
handle,
flags,
Expand All @@ -57,10 +54,7 @@ pub fn handle_to_fd(
}

/// Converts a PRIME file descriptor to a GEM object's handle.
pub fn fd_to_handle(
fd: BorrowedFd<'_>,
primefd: BorrowedFd<'_>,
) -> Result<drm_prime_handle, Error> {
pub fn fd_to_handle(fd: BorrowedFd<'_>, primefd: BorrowedFd<'_>) -> io::Result<drm_prime_handle> {
let mut prime = drm_prime_handle {
fd: primefd.as_raw_fd(),
..Default::default()
Expand Down
39 changes: 19 additions & 20 deletions drm-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ extern crate nix;
#[macro_use]
pub(crate) mod utils;

use crate::result::SystemError as Error;
pub mod gem;
mod ioctl;
pub mod mode;
pub mod result;
pub mod syncobj;

use nix::libc::{c_int, c_ulong};
use std::os::unix::io::{AsRawFd, BorrowedFd};
use std::{
io,
os::unix::io::{AsRawFd, BorrowedFd},
};

///
/// Bindings to the methods of authentication the DRM provides.
Expand All @@ -30,11 +31,13 @@ pub mod auth {
use crate::ioctl;
use drm_sys::*;

use nix::Error;
use std::os::unix::io::{AsRawFd, BorrowedFd};
use std::{
io,
os::unix::io::{AsRawFd, BorrowedFd},
};

/// Get the 'Magic Authentication Token' for this file descriptor.
pub fn get_magic_token(fd: BorrowedFd<'_>) -> Result<drm_auth, Error> {
pub fn get_magic_token(fd: BorrowedFd<'_>) -> io::Result<drm_auth> {
let mut auth = drm_auth::default();

unsafe {
Expand All @@ -45,7 +48,7 @@ pub mod auth {
}

/// Authorize another process' 'Magic Authentication Token'.
pub fn auth_magic_token(fd: BorrowedFd<'_>, auth: u32) -> Result<drm_auth, Error> {
pub fn auth_magic_token(fd: BorrowedFd<'_>, auth: u32) -> io::Result<drm_auth> {
let token = drm_auth { magic: auth };

unsafe {
Expand All @@ -56,7 +59,7 @@ pub mod auth {
}

/// Acquire the 'Master DRM Lock' for this file descriptor.
pub fn acquire_master(fd: BorrowedFd<'_>) -> Result<(), Error> {
pub fn acquire_master(fd: BorrowedFd<'_>) -> io::Result<()> {
unsafe {
ioctl::acquire_master(fd.as_raw_fd())?;
}
Expand All @@ -65,7 +68,7 @@ pub mod auth {
}

/// Release the 'Master DRM Lock' for this file descriptor.
pub fn release_master(fd: BorrowedFd<'_>) -> Result<(), Error> {
pub fn release_master(fd: BorrowedFd<'_>) -> io::Result<()> {
unsafe {
ioctl::release_master(fd.as_raw_fd())?;
}
Expand All @@ -75,7 +78,7 @@ pub mod auth {
}

/// Load this device's Bus ID into a buffer.
pub fn get_bus_id(fd: BorrowedFd<'_>, mut buf: Option<&mut Vec<u8>>) -> Result<drm_unique, Error> {
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)?;
Expand Down Expand Up @@ -107,7 +110,7 @@ pub fn get_interrupt_from_bus_id(
bus: c_int,
dev: c_int,
func: c_int,
) -> Result<drm_irq_busid, Error> {
) -> io::Result<drm_irq_busid> {
let mut irq = drm_irq_busid {
busnum: bus,
devnum: dev,
Expand All @@ -123,7 +126,7 @@ pub fn get_interrupt_from_bus_id(
}

/// Get client information given a client's ID.
pub fn get_client(fd: BorrowedFd<'_>, idx: c_int) -> Result<drm_client, Error> {
pub fn get_client(fd: BorrowedFd<'_>, idx: c_int) -> io::Result<drm_client> {
let mut client = drm_client {
idx,
..Default::default()
Expand All @@ -137,7 +140,7 @@ pub fn get_client(fd: BorrowedFd<'_>, idx: c_int) -> Result<drm_client, Error> {
}

/// Check if a capability is set.
pub fn get_capability(fd: BorrowedFd<'_>, cty: u64) -> Result<drm_get_cap, Error> {
pub fn get_capability(fd: BorrowedFd<'_>, cty: u64) -> io::Result<drm_get_cap> {
let mut cap = drm_get_cap {
capability: cty,
..Default::default()
Expand All @@ -151,11 +154,7 @@ pub fn get_capability(fd: BorrowedFd<'_>, cty: u64) -> Result<drm_get_cap, Error
}

/// Attempt to enable/disable a client's capability.
pub fn set_capability(
fd: BorrowedFd<'_>,
cty: u64,
val: bool,
) -> Result<drm_set_client_cap, Error> {
pub fn set_capability(fd: BorrowedFd<'_>, cty: u64, val: bool) -> io::Result<drm_set_client_cap> {
let cap = drm_set_client_cap {
capability: cty,
value: val as u64,
Expand All @@ -174,7 +173,7 @@ pub fn get_version(
mut name_buf: Option<&mut Vec<i8>>,
mut date_buf: Option<&mut Vec<i8>>,
mut desc_buf: Option<&mut Vec<i8>>,
) -> Result<drm_version, Error> {
) -> io::Result<drm_version> {
let mut sizes = drm_version::default();
unsafe {
ioctl::get_version(fd.as_raw_fd(), &mut sizes)?;
Expand Down Expand Up @@ -211,7 +210,7 @@ pub fn wait_vblank(
type_: u32,
sequence: u32,
signal: usize,
) -> Result<drm_wait_vblank_reply, Error> {
) -> io::Result<drm_wait_vblank_reply> {
// We can't assume the kernel will completely fill the reply in the union
// with valid data (it won't populate the timestamp if the event flag is
// set, for example), so use `default` to ensure the structure is completely
Expand Down
Loading

0 comments on commit 34fcd78

Please sign in to comment.