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 for _IO generator macros #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository= "https://github.com/raymanfx/libv4l-rs"
[dependencies]
bitflags = "2"
libc = "0.2"
rustix = "0.38.37"
v4l-sys = { path = "v4l-sys", version = "0.3.0", optional = true }
v4l2-sys = { path = "v4l2-sys", version = "0.3.0", package="v4l2-sys-mit", optional = true }

Expand Down
67 changes: 5 additions & 62 deletions src/v4l2/vidioc.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,29 @@
use crate::v4l_sys::*;

#[cfg(not(target_env = "musl"))]
#[allow(non_camel_case_types)]
pub type _IOC_TYPE = std::os::raw::c_ulong;
#[cfg(target_env = "musl")]
#[allow(non_camel_case_types)]
pub type _IOC_TYPE = std::os::raw::c_int;

// linux ioctl.h
const _IOC_NRBITS: u8 = 8;
const _IOC_TYPEBITS: u8 = 8;

const _IOC_SIZEBITS: u8 = 14;

const _IOC_NRSHIFT: u8 = 0;
const _IOC_TYPESHIFT: u8 = _IOC_NRSHIFT + _IOC_NRBITS;
const _IOC_SIZESHIFT: u8 = _IOC_TYPESHIFT + _IOC_TYPEBITS;
const _IOC_DIRSHIFT: u8 = _IOC_SIZESHIFT + _IOC_SIZEBITS;

const _IOC_NONE: u8 = 0;

#[cfg(any(target_os = "linux", target_os = "android"))]
const _IOC_WRITE: u8 = 1;
#[cfg(target_os = "freebsd")]
const _IOC_WRITE: u8 = 2;

#[cfg(any(target_os = "linux", target_os = "android"))]
const _IOC_READ: u8 = 2;
#[cfg(target_os = "freebsd")]
const _IOC_READ: u8 = 1;

macro_rules! _IOC_TYPECHECK {
($type:ty) => {
std::mem::size_of::<$type>()
};
}

macro_rules! _IOC {
($dir:expr, $type:expr, $nr:expr, $size:expr) => {
(($dir as _IOC_TYPE) << $crate::v4l2::vidioc::_IOC_DIRSHIFT)
| (($type as _IOC_TYPE) << $crate::v4l2::vidioc::_IOC_TYPESHIFT)
| (($nr as _IOC_TYPE) << $crate::v4l2::vidioc::_IOC_NRSHIFT)
| (($size as _IOC_TYPE) << $crate::v4l2::vidioc::_IOC_SIZESHIFT)
};
}
pub type _IOC_TYPE = rustix::ioctl::Opcode;

macro_rules! _IO {
($type:expr, $nr:expr) => {
_IOC!($crate::v4l2::vidioc::_IOC_NONE, $type, $nr, 0)
rustix::ioctl::Opcode::none::<()>($type, $nr)
};
}

macro_rules! _IOR {
($type:expr, $nr:expr, $size:ty) => {
_IOC!(
$crate::v4l2::vidioc::_IOC_READ,
$type,
$nr,
_IOC_TYPECHECK!($size)
)
rustix::ioctl::Opcode::read::<$size>($type, $nr)
};
}

macro_rules! _IOW {
($type:expr, $nr:expr, $size:ty) => {
_IOC!(
$crate::v4l2::vidioc::_IOC_WRITE,
$type,
$nr,
_IOC_TYPECHECK!($size)
)
rustix::ioctl::Opcode::write::<$size>($type, $nr)
};
}

macro_rules! _IOWR {
($type:expr, $nr:expr, $size:ty) => {
_IOC!(
$crate::v4l2::vidioc::_IOC_READ | $crate::v4l2::vidioc::_IOC_WRITE,
$type,
$nr,
_IOC_TYPECHECK!($size)
)
rustix::ioctl::Opcode::read_write::<$size>($type, $nr)
};
}

Expand Down